[TCP] tcp_highspeed: Fix AI updates.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / addrconf.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17/*
18 * Changes:
19 *
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
26 * packets.
27 * yoshfuji@USAGI : Fixed interval between DAD
28 * packets.
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
32 * support.
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
37 * seq_file.
b1cacb68
YH
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
40 * status etc.
1da177e4
LT
41 */
42
1da177e4
LT
43#include <linux/errno.h>
44#include <linux/types.h>
45#include <linux/socket.h>
46#include <linux/sockios.h>
47#include <linux/sched.h>
48#include <linux/net.h>
49#include <linux/in6.h>
50#include <linux/netdevice.h>
51#include <linux/if_arp.h>
52#include <linux/if_arcnet.h>
53#include <linux/if_infiniband.h>
54#include <linux/route.h>
55#include <linux/inetdevice.h>
56#include <linux/init.h>
57#ifdef CONFIG_SYSCTL
58#include <linux/sysctl.h>
59#endif
4fc268d2 60#include <linux/capability.h>
1da177e4
LT
61#include <linux/delay.h>
62#include <linux/notifier.h>
543537bd 63#include <linux/string.h>
1da177e4
LT
64
65#include <net/sock.h>
66#include <net/snmp.h>
67
68#include <net/ipv6.h>
69#include <net/protocol.h>
70#include <net/ndisc.h>
71#include <net/ip6_route.h>
72#include <net/addrconf.h>
73#include <net/tcp.h>
74#include <net/ip.h>
75#include <linux/if_tunnel.h>
76#include <linux/rtnetlink.h>
77
78#ifdef CONFIG_IPV6_PRIVACY
79#include <linux/random.h>
1da177e4
LT
80#endif
81
82#include <asm/uaccess.h>
83
84#include <linux/proc_fs.h>
85#include <linux/seq_file.h>
86
87/* Set to 3 to get tracing... */
88#define ACONF_DEBUG 2
89
90#if ACONF_DEBUG >= 3
91#define ADBG(x) printk x
92#else
93#define ADBG(x)
94#endif
95
96#define INFINITY_LIFE_TIME 0xFFFFFFFF
97#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
98
99#ifdef CONFIG_SYSCTL
100static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
101static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
102#endif
103
104#ifdef CONFIG_IPV6_PRIVACY
105static int __ipv6_regen_rndid(struct inet6_dev *idev);
106static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
107static void ipv6_regen_rndid(unsigned long data);
108
109static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1da177e4
LT
110#endif
111
112static int ipv6_count_addresses(struct inet6_dev *idev);
113
114/*
115 * Configured unicast address hash table
116 */
117static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
118static DEFINE_RWLOCK(addrconf_hash_lock);
119
120/* Protects inet6 devices */
121DEFINE_RWLOCK(addrconf_lock);
122
123static void addrconf_verify(unsigned long);
124
8d06afab 125static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1da177e4
LT
126static DEFINE_SPINLOCK(addrconf_verify_lock);
127
128static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
129static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
130
131static int addrconf_ifdown(struct net_device *dev, int how);
132
e431b8c0 133static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1da177e4
LT
134static void addrconf_dad_timer(unsigned long data);
135static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
c5e33bdd 136static void addrconf_dad_run(struct inet6_dev *idev);
1da177e4
LT
137static void addrconf_rs_timer(unsigned long data);
138static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
140
141static void inet6_prefix_notify(int event, struct inet6_dev *idev,
142 struct prefix_info *pinfo);
143static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
144
e041c683 145static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1da177e4
LT
146
147struct ipv6_devconf ipv6_devconf = {
148 .forwarding = 0,
149 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
150 .mtu6 = IPV6_MIN_MTU,
151 .accept_ra = 1,
152 .accept_redirects = 1,
153 .autoconf = 1,
154 .force_mld_version = 0,
155 .dad_transmits = 1,
156 .rtr_solicits = MAX_RTR_SOLICITATIONS,
157 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
158 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
159#ifdef CONFIG_IPV6_PRIVACY
160 .use_tempaddr = 0,
161 .temp_valid_lft = TEMP_VALID_LIFETIME,
162 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
163 .regen_max_retry = REGEN_MAX_RETRY,
164 .max_desync_factor = MAX_DESYNC_FACTOR,
165#endif
166 .max_addresses = IPV6_MAX_ADDRESSES,
65f5c7c1 167 .accept_ra_defrtr = 1,
c4fd30eb 168 .accept_ra_pinfo = 1,
930d6ff2
YH
169#ifdef CONFIG_IPV6_ROUTER_PREF
170 .accept_ra_rtr_pref = 1,
52e16356 171 .rtr_probe_interval = 60 * HZ,
09c884d4
YH
172#ifdef CONFIG_IPV6_ROUTE_INFO
173 .accept_ra_rt_info_max_plen = 0,
174#endif
930d6ff2 175#endif
1da177e4
LT
176};
177
178static struct ipv6_devconf ipv6_devconf_dflt = {
179 .forwarding = 0,
180 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
181 .mtu6 = IPV6_MIN_MTU,
182 .accept_ra = 1,
183 .accept_redirects = 1,
184 .autoconf = 1,
185 .dad_transmits = 1,
186 .rtr_solicits = MAX_RTR_SOLICITATIONS,
187 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
188 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
189#ifdef CONFIG_IPV6_PRIVACY
190 .use_tempaddr = 0,
191 .temp_valid_lft = TEMP_VALID_LIFETIME,
192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
193 .regen_max_retry = REGEN_MAX_RETRY,
194 .max_desync_factor = MAX_DESYNC_FACTOR,
195#endif
196 .max_addresses = IPV6_MAX_ADDRESSES,
65f5c7c1 197 .accept_ra_defrtr = 1,
c4fd30eb 198 .accept_ra_pinfo = 1,
930d6ff2
YH
199#ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref = 1,
52e16356 201 .rtr_probe_interval = 60 * HZ,
09c884d4
YH
202#ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen = 0,
204#endif
930d6ff2 205#endif
1da177e4
LT
206};
207
208/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
209#if 0
210const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
211#endif
212const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
213
b1cacb68
YH
214#define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
215
216static inline unsigned ipv6_addr_scope2type(unsigned scope)
217{
218 switch(scope) {
219 case IPV6_ADDR_SCOPE_NODELOCAL:
220 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
221 IPV6_ADDR_LOOPBACK);
222 case IPV6_ADDR_SCOPE_LINKLOCAL:
223 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
224 IPV6_ADDR_LINKLOCAL);
225 case IPV6_ADDR_SCOPE_SITELOCAL:
226 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
227 IPV6_ADDR_SITELOCAL);
228 }
229 return IPV6_ADDR_SCOPE_TYPE(scope);
230}
231
232int __ipv6_addr_type(const struct in6_addr *addr)
1da177e4 233{
1da177e4
LT
234 u32 st;
235
236 st = addr->s6_addr32[0];
237
1da177e4 238 /* Consider all addresses with the first three bits different of
b1cacb68 239 000 and 111 as unicasts.
1da177e4
LT
240 */
241 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
242 (st & htonl(0xE0000000)) != htonl(0xE0000000))
b1cacb68
YH
243 return (IPV6_ADDR_UNICAST |
244 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
1da177e4 245
b1cacb68
YH
246 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
247 /* multicast */
248 /* addr-select 3.1 */
249 return (IPV6_ADDR_MULTICAST |
250 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
251 }
252
253 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
254 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
255 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
1da177e4 256 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
b1cacb68
YH
257 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
258 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
1da177e4
LT
259
260 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
261 if (addr->s6_addr32[2] == 0) {
262 if (addr->s6_addr32[3] == 0)
263 return IPV6_ADDR_ANY;
264
265 if (addr->s6_addr32[3] == htonl(0x00000001))
b1cacb68
YH
266 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
267 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
1da177e4 268
b1cacb68
YH
269 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
270 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
1da177e4
LT
271 }
272
273 if (addr->s6_addr32[2] == htonl(0x0000ffff))
b1cacb68
YH
274 return (IPV6_ADDR_MAPPED |
275 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
1da177e4
LT
276 }
277
b1cacb68
YH
278 return (IPV6_ADDR_RESERVED |
279 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
1da177e4
LT
280}
281
282static void addrconf_del_timer(struct inet6_ifaddr *ifp)
283{
284 if (del_timer(&ifp->timer))
285 __in6_ifa_put(ifp);
286}
287
288enum addrconf_timer_t
289{
290 AC_NONE,
291 AC_DAD,
292 AC_RS,
293};
294
295static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
296 enum addrconf_timer_t what,
297 unsigned long when)
298{
299 if (!del_timer(&ifp->timer))
300 in6_ifa_hold(ifp);
301
302 switch (what) {
303 case AC_DAD:
304 ifp->timer.function = addrconf_dad_timer;
305 break;
306 case AC_RS:
307 ifp->timer.function = addrconf_rs_timer;
308 break;
309 default:;
310 }
311 ifp->timer.expires = jiffies + when;
312 add_timer(&ifp->timer);
313}
314
315/* Nobody refers to this device, we may destroy it. */
316
317void in6_dev_finish_destroy(struct inet6_dev *idev)
318{
319 struct net_device *dev = idev->dev;
320 BUG_TRAP(idev->addr_list==NULL);
321 BUG_TRAP(idev->mc_list==NULL);
322#ifdef NET_REFCNT_DEBUG
323 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
324#endif
325 dev_put(dev);
326 if (!idev->dead) {
327 printk("Freeing alive inet6 device %p\n", idev);
328 return;
329 }
330 snmp6_free_dev(idev);
331 kfree(idev);
332}
333
334static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
335{
336 struct inet6_dev *ndev;
337
338 ASSERT_RTNL();
339
340 if (dev->mtu < IPV6_MIN_MTU)
341 return NULL;
342
322f74a4
IO
343 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
344
345 if (ndev == NULL)
346 return NULL;
347
348 rwlock_init(&ndev->lock);
349 ndev->dev = dev;
350 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
351 ndev->cnf.mtu6 = dev->mtu;
352 ndev->cnf.sysctl = NULL;
353 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
354 if (ndev->nd_parms == NULL) {
355 kfree(ndev);
356 return NULL;
357 }
358 /* We refer to the device */
359 dev_hold(dev);
1da177e4 360
322f74a4
IO
361 if (snmp6_alloc_dev(ndev) < 0) {
362 ADBG((KERN_WARNING
363 "%s(): cannot allocate memory for statistics; dev=%s.\n",
364 __FUNCTION__, dev->name));
365 neigh_parms_release(&nd_tbl, ndev->nd_parms);
366 ndev->dead = 1;
367 in6_dev_finish_destroy(ndev);
368 return NULL;
369 }
1da177e4 370
322f74a4
IO
371 if (snmp6_register_dev(ndev) < 0) {
372 ADBG((KERN_WARNING
373 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
374 __FUNCTION__, dev->name));
375 neigh_parms_release(&nd_tbl, ndev->nd_parms);
376 ndev->dead = 1;
377 in6_dev_finish_destroy(ndev);
378 return NULL;
379 }
380
381 /* One reference from device. We must do this before
382 * we invoke __ipv6_regen_rndid().
383 */
384 in6_dev_hold(ndev);
1da177e4
LT
385
386#ifdef CONFIG_IPV6_PRIVACY
322f74a4
IO
387 init_timer(&ndev->regen_timer);
388 ndev->regen_timer.function = ipv6_regen_rndid;
389 ndev->regen_timer.data = (unsigned long) ndev;
390 if ((dev->flags&IFF_LOOPBACK) ||
391 dev->type == ARPHRD_TUNNEL ||
392 dev->type == ARPHRD_NONE ||
393 dev->type == ARPHRD_SIT) {
394 printk(KERN_INFO
395 "%s: Disabled Privacy Extensions\n",
396 dev->name);
397 ndev->cnf.use_tempaddr = -1;
398 } else {
399 in6_dev_hold(ndev);
400 ipv6_regen_rndid((unsigned long) ndev);
401 }
1da177e4
LT
402#endif
403
322f74a4
IO
404 if (netif_carrier_ok(dev))
405 ndev->if_flags |= IF_READY;
3c21edbd 406
322f74a4
IO
407 write_lock_bh(&addrconf_lock);
408 dev->ip6_ptr = ndev;
409 write_unlock_bh(&addrconf_lock);
1da177e4 410
322f74a4
IO
411 ipv6_mc_init_dev(ndev);
412 ndev->tstamp = jiffies;
1da177e4 413#ifdef CONFIG_SYSCTL
322f74a4
IO
414 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
415 NET_IPV6_NEIGH, "ipv6",
416 &ndisc_ifinfo_sysctl_change,
417 NULL);
418 addrconf_sysctl_register(ndev, &ndev->cnf);
1da177e4 419#endif
1da177e4
LT
420 return ndev;
421}
422
423static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
424{
425 struct inet6_dev *idev;
426
427 ASSERT_RTNL();
428
429 if ((idev = __in6_dev_get(dev)) == NULL) {
430 if ((idev = ipv6_add_dev(dev)) == NULL)
431 return NULL;
432 }
c5e33bdd 433
1da177e4
LT
434 if (dev->flags&IFF_UP)
435 ipv6_mc_up(idev);
436 return idev;
437}
438
439#ifdef CONFIG_SYSCTL
440static void dev_forward_change(struct inet6_dev *idev)
441{
442 struct net_device *dev;
443 struct inet6_ifaddr *ifa;
444 struct in6_addr addr;
445
446 if (!idev)
447 return;
448 dev = idev->dev;
449 if (dev && (dev->flags & IFF_MULTICAST)) {
450 ipv6_addr_all_routers(&addr);
451
452 if (idev->cnf.forwarding)
453 ipv6_dev_mc_inc(dev, &addr);
454 else
455 ipv6_dev_mc_dec(dev, &addr);
456 }
457 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
458 if (idev->cnf.forwarding)
459 addrconf_join_anycast(ifa);
460 else
461 addrconf_leave_anycast(ifa);
462 }
463}
464
465
466static void addrconf_forward_change(void)
467{
468 struct net_device *dev;
469 struct inet6_dev *idev;
470
471 read_lock(&dev_base_lock);
472 for (dev=dev_base; dev; dev=dev->next) {
473 read_lock(&addrconf_lock);
474 idev = __in6_dev_get(dev);
475 if (idev) {
476 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
477 idev->cnf.forwarding = ipv6_devconf.forwarding;
478 if (changed)
479 dev_forward_change(idev);
480 }
481 read_unlock(&addrconf_lock);
482 }
483 read_unlock(&dev_base_lock);
484}
485#endif
486
487/* Nobody refers to this ifaddr, destroy it */
488
489void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
490{
491 BUG_TRAP(ifp->if_next==NULL);
492 BUG_TRAP(ifp->lst_next==NULL);
493#ifdef NET_REFCNT_DEBUG
494 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
495#endif
496
497 in6_dev_put(ifp->idev);
498
499 if (del_timer(&ifp->timer))
500 printk("Timer is still running, when freeing ifa=%p\n", ifp);
501
502 if (!ifp->dead) {
503 printk("Freeing alive inet6 address %p\n", ifp);
504 return;
505 }
506 dst_release(&ifp->rt->u.dst);
507
508 kfree(ifp);
509}
510
e55ffac6
BH
511static void
512ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
513{
514 struct inet6_ifaddr *ifa, **ifap;
515
516 /*
517 * Each device address list is sorted in order of scope -
518 * global before linklocal.
519 */
520 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
521 ifap = &ifa->if_next) {
522 if (ifp->scope > ifa->scope)
523 break;
524 }
525
526 ifp->if_next = *ifap;
527 *ifap = ifp;
528}
529
1da177e4
LT
530/* On success it returns ifp with increased reference count */
531
532static struct inet6_ifaddr *
533ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
e431b8c0 534 int scope, u32 flags)
1da177e4
LT
535{
536 struct inet6_ifaddr *ifa = NULL;
537 struct rt6_info *rt;
538 int hash;
539 int err = 0;
540
541 read_lock_bh(&addrconf_lock);
542 if (idev->dead) {
543 err = -ENODEV; /*XXX*/
544 goto out2;
545 }
546
547 write_lock(&addrconf_hash_lock);
548
549 /* Ignore adding duplicate addresses on an interface */
550 if (ipv6_chk_same_addr(addr, idev->dev)) {
551 ADBG(("ipv6_add_addr: already assigned\n"));
552 err = -EEXIST;
553 goto out;
554 }
555
322f74a4 556 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
1da177e4
LT
557
558 if (ifa == NULL) {
559 ADBG(("ipv6_add_addr: malloc failed\n"));
560 err = -ENOBUFS;
561 goto out;
562 }
563
564 rt = addrconf_dst_alloc(idev, addr, 0);
565 if (IS_ERR(rt)) {
566 err = PTR_ERR(rt);
567 goto out;
568 }
569
1da177e4
LT
570 ipv6_addr_copy(&ifa->addr, addr);
571
572 spin_lock_init(&ifa->lock);
573 init_timer(&ifa->timer);
574 ifa->timer.data = (unsigned long) ifa;
575 ifa->scope = scope;
576 ifa->prefix_len = pfxlen;
577 ifa->flags = flags | IFA_F_TENTATIVE;
578 ifa->cstamp = ifa->tstamp = jiffies;
579
580 ifa->idev = idev;
581 in6_dev_hold(idev);
582 /* For caller */
583 in6_ifa_hold(ifa);
584
585 /* Add to big hash table */
586 hash = ipv6_addr_hash(addr);
587
588 ifa->lst_next = inet6_addr_lst[hash];
589 inet6_addr_lst[hash] = ifa;
590 in6_ifa_hold(ifa);
591 write_unlock(&addrconf_hash_lock);
592
593 write_lock(&idev->lock);
594 /* Add to inet6_dev unicast addr list. */
e55ffac6 595 ipv6_link_dev_addr(idev, ifa);
1da177e4
LT
596
597#ifdef CONFIG_IPV6_PRIVACY
598 if (ifa->flags&IFA_F_TEMPORARY) {
599 ifa->tmp_next = idev->tempaddr_list;
600 idev->tempaddr_list = ifa;
601 in6_ifa_hold(ifa);
602 }
603#endif
604
605 ifa->rt = rt;
606
607 in6_ifa_hold(ifa);
608 write_unlock(&idev->lock);
609out2:
610 read_unlock_bh(&addrconf_lock);
611
fd92833a 612 if (likely(err == 0))
e041c683 613 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
1da177e4
LT
614 else {
615 kfree(ifa);
616 ifa = ERR_PTR(err);
617 }
618
619 return ifa;
620out:
621 write_unlock(&addrconf_hash_lock);
622 goto out2;
623}
624
625/* This function wants to get referenced ifp and releases it before return */
626
627static void ipv6_del_addr(struct inet6_ifaddr *ifp)
628{
629 struct inet6_ifaddr *ifa, **ifap;
630 struct inet6_dev *idev = ifp->idev;
631 int hash;
632 int deleted = 0, onlink = 0;
633 unsigned long expires = jiffies;
634
635 hash = ipv6_addr_hash(&ifp->addr);
636
637 ifp->dead = 1;
638
639 write_lock_bh(&addrconf_hash_lock);
640 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
641 ifap = &ifa->lst_next) {
642 if (ifa == ifp) {
643 *ifap = ifa->lst_next;
644 __in6_ifa_put(ifp);
645 ifa->lst_next = NULL;
646 break;
647 }
648 }
649 write_unlock_bh(&addrconf_hash_lock);
650
651 write_lock_bh(&idev->lock);
652#ifdef CONFIG_IPV6_PRIVACY
653 if (ifp->flags&IFA_F_TEMPORARY) {
654 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
655 ifap = &ifa->tmp_next) {
656 if (ifa == ifp) {
657 *ifap = ifa->tmp_next;
658 if (ifp->ifpub) {
659 in6_ifa_put(ifp->ifpub);
660 ifp->ifpub = NULL;
661 }
662 __in6_ifa_put(ifp);
663 ifa->tmp_next = NULL;
664 break;
665 }
666 }
667 }
668#endif
669
1d142804 670 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
1da177e4
LT
671 if (ifa == ifp) {
672 *ifap = ifa->if_next;
673 __in6_ifa_put(ifp);
674 ifa->if_next = NULL;
675 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
676 break;
677 deleted = 1;
1d142804 678 continue;
1da177e4
LT
679 } else if (ifp->flags & IFA_F_PERMANENT) {
680 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
681 ifp->prefix_len)) {
682 if (ifa->flags & IFA_F_PERMANENT) {
683 onlink = 1;
684 if (deleted)
685 break;
686 } else {
687 unsigned long lifetime;
688
689 if (!onlink)
690 onlink = -1;
691
692 spin_lock(&ifa->lock);
693 lifetime = min_t(unsigned long,
694 ifa->valid_lft, 0x7fffffffUL/HZ);
695 if (time_before(expires,
696 ifa->tstamp + lifetime * HZ))
697 expires = ifa->tstamp + lifetime * HZ;
698 spin_unlock(&ifa->lock);
699 }
700 }
701 }
1d142804 702 ifap = &ifa->if_next;
1da177e4
LT
703 }
704 write_unlock_bh(&idev->lock);
705
706 ipv6_ifa_notify(RTM_DELADDR, ifp);
707
e041c683 708 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
1da177e4
LT
709
710 addrconf_del_timer(ifp);
711
712 /*
713 * Purge or update corresponding prefix
714 *
715 * 1) we don't purge prefix here if address was not permanent.
716 * prefix is managed by its own lifetime.
717 * 2) if there're no addresses, delete prefix.
718 * 3) if there're still other permanent address(es),
719 * corresponding prefix is still permanent.
720 * 4) otherwise, update prefix lifetime to the
721 * longest valid lifetime among the corresponding
722 * addresses on the device.
723 * Note: subsequent RA will update lifetime.
724 *
725 * --yoshfuji
726 */
727 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
728 struct in6_addr prefix;
729 struct rt6_info *rt;
730
731 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
732 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
733
734 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
735 if (onlink == 0) {
0d51aa80 736 ip6_del_rt(rt, NULL, NULL, NULL);
1da177e4
LT
737 rt = NULL;
738 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
739 rt->rt6i_expires = expires;
740 rt->rt6i_flags |= RTF_EXPIRES;
741 }
742 }
743 dst_release(&rt->u.dst);
744 }
745
746 in6_ifa_put(ifp);
747}
748
749#ifdef CONFIG_IPV6_PRIVACY
750static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
751{
752 struct inet6_dev *idev = ifp->idev;
753 struct in6_addr addr, *tmpaddr;
754 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
755 int tmp_plen;
756 int ret = 0;
757 int max_addresses;
758
759 write_lock(&idev->lock);
760 if (ift) {
761 spin_lock_bh(&ift->lock);
762 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
763 spin_unlock_bh(&ift->lock);
764 tmpaddr = &addr;
765 } else {
766 tmpaddr = NULL;
767 }
768retry:
769 in6_dev_hold(idev);
770 if (idev->cnf.use_tempaddr <= 0) {
771 write_unlock(&idev->lock);
772 printk(KERN_INFO
773 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
774 in6_dev_put(idev);
775 ret = -1;
776 goto out;
777 }
778 spin_lock_bh(&ifp->lock);
779 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
780 idev->cnf.use_tempaddr = -1; /*XXX*/
781 spin_unlock_bh(&ifp->lock);
782 write_unlock(&idev->lock);
783 printk(KERN_WARNING
784 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
785 in6_dev_put(idev);
786 ret = -1;
787 goto out;
788 }
789 in6_ifa_hold(ifp);
790 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
791 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
792 spin_unlock_bh(&ifp->lock);
793 write_unlock(&idev->lock);
794 printk(KERN_WARNING
795 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
796 in6_ifa_put(ifp);
797 in6_dev_put(idev);
798 ret = -1;
799 goto out;
800 }
801 memcpy(&addr.s6_addr[8], idev->rndid, 8);
802 tmp_valid_lft = min_t(__u32,
803 ifp->valid_lft,
804 idev->cnf.temp_valid_lft);
805 tmp_prefered_lft = min_t(__u32,
806 ifp->prefered_lft,
807 idev->cnf.temp_prefered_lft - desync_factor / HZ);
808 tmp_plen = ifp->prefix_len;
809 max_addresses = idev->cnf.max_addresses;
810 tmp_cstamp = ifp->cstamp;
811 tmp_tstamp = ifp->tstamp;
812 spin_unlock_bh(&ifp->lock);
813
814 write_unlock(&idev->lock);
815 ift = !max_addresses ||
816 ipv6_count_addresses(idev) < max_addresses ?
817 ipv6_add_addr(idev, &addr, tmp_plen,
818 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
819 if (!ift || IS_ERR(ift)) {
820 in6_ifa_put(ifp);
821 in6_dev_put(idev);
822 printk(KERN_INFO
823 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
824 tmpaddr = &addr;
825 write_lock(&idev->lock);
826 goto retry;
827 }
828
829 spin_lock_bh(&ift->lock);
830 ift->ifpub = ifp;
831 ift->valid_lft = tmp_valid_lft;
832 ift->prefered_lft = tmp_prefered_lft;
833 ift->cstamp = tmp_cstamp;
834 ift->tstamp = tmp_tstamp;
835 spin_unlock_bh(&ift->lock);
836
837 addrconf_dad_start(ift, 0);
838 in6_ifa_put(ift);
839 in6_dev_put(idev);
840out:
841 return ret;
842}
843#endif
844
845/*
072047e4 846 * Choose an appropriate source address (RFC3484)
1da177e4 847 */
072047e4
YH
848struct ipv6_saddr_score {
849 int addr_type;
850 unsigned int attrs;
851 int matchlen;
0d27b427 852 int scope;
072047e4
YH
853 unsigned int rule;
854};
855
856#define IPV6_SADDR_SCORE_LOCAL 0x0001
857#define IPV6_SADDR_SCORE_PREFERRED 0x0004
858#define IPV6_SADDR_SCORE_HOA 0x0008
859#define IPV6_SADDR_SCORE_OIF 0x0010
860#define IPV6_SADDR_SCORE_LABEL 0x0020
861#define IPV6_SADDR_SCORE_PRIVACY 0x0040
862
863static int inline ipv6_saddr_preferred(int type)
1da177e4 864{
072047e4
YH
865 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
866 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
867 return 1;
868 return 0;
1da177e4
LT
869}
870
072047e4
YH
871/* static matching label */
872static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
873{
874 /*
875 * prefix (longest match) label
876 * -----------------------------
877 * ::1/128 0
878 * ::/0 1
879 * 2002::/16 2
880 * ::/96 3
881 * ::ffff:0:0/96 4
102128e3
ŁS
882 * fc00::/7 5
883 * 2001::/32 6
072047e4
YH
884 */
885 if (type & IPV6_ADDR_LOOPBACK)
886 return 0;
887 else if (type & IPV6_ADDR_COMPATv4)
888 return 3;
889 else if (type & IPV6_ADDR_MAPPED)
890 return 4;
102128e3
ŁS
891 else if (addr->s6_addr32[0] == htonl(0x20010000))
892 return 6;
072047e4
YH
893 else if (addr->s6_addr16[0] == htons(0x2002))
894 return 2;
102128e3
ŁS
895 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
896 return 5;
072047e4
YH
897 return 1;
898}
1da177e4 899
072047e4 900int ipv6_dev_get_saddr(struct net_device *daddr_dev,
1da177e4
LT
901 struct in6_addr *daddr, struct in6_addr *saddr)
902{
072047e4
YH
903 struct ipv6_saddr_score hiscore;
904 struct inet6_ifaddr *ifa_result = NULL;
905 int daddr_type = __ipv6_addr_type(daddr);
906 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
907 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
908 struct net_device *dev;
1da177e4 909
072047e4 910 memset(&hiscore, 0, sizeof(hiscore));
1da177e4 911
072047e4
YH
912 read_lock(&dev_base_lock);
913 read_lock(&addrconf_lock);
1da177e4 914
072047e4
YH
915 for (dev = dev_base; dev; dev=dev->next) {
916 struct inet6_dev *idev;
917 struct inet6_ifaddr *ifa;
918
919 /* Rule 0: Candidate Source Address (section 4)
920 * - multicast and link-local destination address,
921 * the set of candidate source address MUST only
922 * include addresses assigned to interfaces
923 * belonging to the same link as the outgoing
924 * interface.
925 * (- For site-local destination addresses, the
926 * set of candidate source addresses MUST only
927 * include addresses assigned to interfaces
928 * belonging to the same site as the outgoing
929 * interface.)
930 */
931 if ((daddr_type & IPV6_ADDR_MULTICAST ||
932 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
933 daddr_dev && dev != daddr_dev)
934 continue;
1da177e4 935
1da177e4 936 idev = __in6_dev_get(dev);
072047e4
YH
937 if (!idev)
938 continue;
939
940 read_lock_bh(&idev->lock);
941 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
942 struct ipv6_saddr_score score;
943
944 score.addr_type = __ipv6_addr_type(&ifa->addr);
945
6b3ae80a
YH
946 /* Rule 0:
947 * - Tentative Address (RFC2462 section 5.4)
948 * - A tentative address is not considered
949 * "assigned to an interface" in the traditional
950 * sense.
951 * - Candidate Source Address (section 4)
072047e4
YH
952 * - In any case, anycast addresses, multicast
953 * addresses, and the unspecified address MUST
954 * NOT be included in a candidate set.
955 */
6b3ae80a
YH
956 if (ifa->flags & IFA_F_TENTATIVE)
957 continue;
072047e4
YH
958 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
959 score.addr_type & IPV6_ADDR_MULTICAST)) {
960 LIMIT_NETDEBUG(KERN_DEBUG
961 "ADDRCONF: unspecified / multicast address"
962 "assigned as unicast address on %s",
963 dev->name);
964 continue;
965 }
966
967 score.attrs = 0;
968 score.matchlen = 0;
969 score.scope = 0;
970 score.rule = 0;
971
972 if (ifa_result == NULL) {
973 /* record it if the first available entry */
974 goto record_it;
975 }
976
977 /* Rule 1: Prefer same address */
978 if (hiscore.rule < 1) {
979 if (ipv6_addr_equal(&ifa_result->addr, daddr))
980 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
981 hiscore.rule++;
982 }
983 if (ipv6_addr_equal(&ifa->addr, daddr)) {
984 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
985 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
986 score.rule = 1;
987 goto record_it;
1da177e4 988 }
072047e4
YH
989 } else {
990 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
991 continue;
1da177e4 992 }
1da177e4 993
072047e4
YH
994 /* Rule 2: Prefer appropriate scope */
995 if (hiscore.rule < 2) {
996 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
997 hiscore.rule++;
998 }
999 score.scope = __ipv6_addr_src_scope(score.addr_type);
1000 if (hiscore.scope < score.scope) {
1001 if (hiscore.scope < daddr_scope) {
1002 score.rule = 2;
1003 goto record_it;
1004 } else
1005 continue;
1006 } else if (score.scope < hiscore.scope) {
1007 if (score.scope < daddr_scope)
e55ffac6 1008 break; /* addresses sorted by scope */
072047e4
YH
1009 else {
1010 score.rule = 2;
1011 goto record_it;
1012 }
1013 }
1da177e4 1014
072047e4
YH
1015 /* Rule 3: Avoid deprecated address */
1016 if (hiscore.rule < 3) {
1017 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1018 !(ifa_result->flags & IFA_F_DEPRECATED))
1019 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1020 hiscore.rule++;
1021 }
1022 if (ipv6_saddr_preferred(score.addr_type) ||
1023 !(ifa->flags & IFA_F_DEPRECATED)) {
1024 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1025 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1026 score.rule = 3;
1027 goto record_it;
1028 }
1029 } else {
1030 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1031 continue;
1032 }
1da177e4 1033
072047e4 1034 /* Rule 4: Prefer home address -- not implemented yet */
220bbd74
YH
1035 if (hiscore.rule < 4)
1036 hiscore.rule++;
072047e4
YH
1037
1038 /* Rule 5: Prefer outgoing interface */
1039 if (hiscore.rule < 5) {
1040 if (daddr_dev == NULL ||
1041 daddr_dev == ifa_result->idev->dev)
1042 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1043 hiscore.rule++;
1044 }
1045 if (daddr_dev == NULL ||
1046 daddr_dev == ifa->idev->dev) {
1047 score.attrs |= IPV6_SADDR_SCORE_OIF;
1048 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1049 score.rule = 5;
1050 goto record_it;
1051 }
1052 } else {
1053 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1054 continue;
1055 }
1056
1057 /* Rule 6: Prefer matching label */
1058 if (hiscore.rule < 6) {
1059 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1060 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1061 hiscore.rule++;
1062 }
1063 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1064 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1065 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1066 score.rule = 6;
1067 goto record_it;
1da177e4 1068 }
072047e4
YH
1069 } else {
1070 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1071 continue;
1072 }
1073
44fd0261 1074#ifdef CONFIG_IPV6_PRIVACY
072047e4
YH
1075 /* Rule 7: Prefer public address
1076 * Note: prefer temprary address if use_tempaddr >= 2
1077 */
1078 if (hiscore.rule < 7) {
1079 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1080 (ifa_result->idev->cnf.use_tempaddr >= 2))
1081 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1082 hiscore.rule++;
1083 }
1084 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1085 (ifa->idev->cnf.use_tempaddr >= 2)) {
1086 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1087 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1088 score.rule = 7;
1089 goto record_it;
1090 }
1091 } else {
1092 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1093 continue;
1094 }
5e2707fa
YH
1095#else
1096 if (hiscore.rule < 7)
1097 hiscore.rule++;
44fd0261 1098#endif
072047e4 1099 /* Rule 8: Use longest matching prefix */
12da2a43 1100 if (hiscore.rule < 8) {
072047e4 1101 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
12da2a43
YZ
1102 hiscore.rule++;
1103 }
072047e4
YH
1104 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1105 if (score.matchlen > hiscore.matchlen) {
1106 score.rule = 8;
1107 goto record_it;
1da177e4 1108 }
072047e4
YH
1109#if 0
1110 else if (score.matchlen < hiscore.matchlen)
1111 continue;
1112#endif
1113
1114 /* Final Rule: choose first available one */
1115 continue;
1116record_it:
1117 if (ifa_result)
1118 in6_ifa_put(ifa_result);
1119 in6_ifa_hold(ifa);
1120 ifa_result = ifa;
1121 hiscore = score;
1da177e4 1122 }
072047e4 1123 read_unlock_bh(&idev->lock);
1da177e4 1124 }
1da177e4
LT
1125 read_unlock(&addrconf_lock);
1126 read_unlock(&dev_base_lock);
1127
072047e4
YH
1128 if (!ifa_result)
1129 return -EADDRNOTAVAIL;
1130
1131 ipv6_addr_copy(saddr, &ifa_result->addr);
1132 in6_ifa_put(ifa_result);
1133 return 0;
1da177e4
LT
1134}
1135
1136
1137int ipv6_get_saddr(struct dst_entry *dst,
1138 struct in6_addr *daddr, struct in6_addr *saddr)
1139{
1140 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1141}
1142
1143
1144int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1145{
1146 struct inet6_dev *idev;
1147 int err = -EADDRNOTAVAIL;
1148
1149 read_lock(&addrconf_lock);
1150 if ((idev = __in6_dev_get(dev)) != NULL) {
1151 struct inet6_ifaddr *ifp;
1152
1153 read_lock_bh(&idev->lock);
1154 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1155 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1156 ipv6_addr_copy(addr, &ifp->addr);
1157 err = 0;
1158 break;
1159 }
1160 }
1161 read_unlock_bh(&idev->lock);
1162 }
1163 read_unlock(&addrconf_lock);
1164 return err;
1165}
1166
1167static int ipv6_count_addresses(struct inet6_dev *idev)
1168{
1169 int cnt = 0;
1170 struct inet6_ifaddr *ifp;
1171
1172 read_lock_bh(&idev->lock);
1173 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1174 cnt++;
1175 read_unlock_bh(&idev->lock);
1176 return cnt;
1177}
1178
1179int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1180{
1181 struct inet6_ifaddr * ifp;
1182 u8 hash = ipv6_addr_hash(addr);
1183
1184 read_lock_bh(&addrconf_hash_lock);
1185 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1186 if (ipv6_addr_equal(&ifp->addr, addr) &&
1187 !(ifp->flags&IFA_F_TENTATIVE)) {
1188 if (dev == NULL || ifp->idev->dev == dev ||
1189 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1190 break;
1191 }
1192 }
1193 read_unlock_bh(&addrconf_hash_lock);
1194 return ifp != NULL;
1195}
1196
1197static
1198int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1199{
1200 struct inet6_ifaddr * ifp;
1201 u8 hash = ipv6_addr_hash(addr);
1202
1203 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1204 if (ipv6_addr_equal(&ifp->addr, addr)) {
1205 if (dev == NULL || ifp->idev->dev == dev)
1206 break;
1207 }
1208 }
1209 return ifp != NULL;
1210}
1211
1212struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1213{
1214 struct inet6_ifaddr * ifp;
1215 u8 hash = ipv6_addr_hash(addr);
1216
1217 read_lock_bh(&addrconf_hash_lock);
1218 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1219 if (ipv6_addr_equal(&ifp->addr, addr)) {
1220 if (dev == NULL || ifp->idev->dev == dev ||
1221 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1222 in6_ifa_hold(ifp);
1223 break;
1224 }
1225 }
1226 }
1227 read_unlock_bh(&addrconf_hash_lock);
1228
1229 return ifp;
1230}
1231
1232int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1233{
1234 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
0fa1a53e 1235 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1da177e4 1236 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
8feaf0c0 1237 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1da177e4 1238 int sk_ipv6only = ipv6_only_sock(sk);
463c84b9 1239 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1da177e4
LT
1240 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1241 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1242
1243 if (!sk2_rcv_saddr && !sk_ipv6only)
1244 return 1;
1245
1246 if (addr_type2 == IPV6_ADDR_ANY &&
1247 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1248 return 1;
1249
1250 if (addr_type == IPV6_ADDR_ANY &&
1251 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1252 return 1;
1253
1254 if (sk2_rcv_saddr6 &&
1255 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1256 return 1;
1257
1258 if (addr_type == IPV6_ADDR_MAPPED &&
1259 !sk2_ipv6only &&
1260 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1261 return 1;
1262
1263 return 0;
1264}
1265
1266/* Gets referenced address, destroys ifaddr */
1267
9f5336e2 1268static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1da177e4 1269{
1da177e4
LT
1270 if (ifp->flags&IFA_F_PERMANENT) {
1271 spin_lock_bh(&ifp->lock);
1272 addrconf_del_timer(ifp);
1273 ifp->flags |= IFA_F_TENTATIVE;
1274 spin_unlock_bh(&ifp->lock);
1275 in6_ifa_put(ifp);
1276#ifdef CONFIG_IPV6_PRIVACY
1277 } else if (ifp->flags&IFA_F_TEMPORARY) {
1278 struct inet6_ifaddr *ifpub;
1279 spin_lock_bh(&ifp->lock);
1280 ifpub = ifp->ifpub;
1281 if (ifpub) {
1282 in6_ifa_hold(ifpub);
1283 spin_unlock_bh(&ifp->lock);
1284 ipv6_create_tempaddr(ifpub, ifp);
1285 in6_ifa_put(ifpub);
1286 } else {
1287 spin_unlock_bh(&ifp->lock);
1288 }
1289 ipv6_del_addr(ifp);
1290#endif
1291 } else
1292 ipv6_del_addr(ifp);
1293}
1294
3c21edbd
YH
1295void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1296{
1297 if (net_ratelimit())
1298 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1299 addrconf_dad_stop(ifp);
1300}
1da177e4
LT
1301
1302/* Join to solicited addr multicast group. */
1303
1304void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1305{
1306 struct in6_addr maddr;
1307
1308 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1309 return;
1310
1311 addrconf_addr_solict_mult(addr, &maddr);
1312 ipv6_dev_mc_inc(dev, &maddr);
1313}
1314
1315void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1316{
1317 struct in6_addr maddr;
1318
1319 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1320 return;
1321
1322 addrconf_addr_solict_mult(addr, &maddr);
1323 __ipv6_dev_mc_dec(idev, &maddr);
1324}
1325
20380731 1326static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1da177e4
LT
1327{
1328 struct in6_addr addr;
1329 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1330 if (ipv6_addr_any(&addr))
1331 return;
1332 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1333}
1334
20380731 1335static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1da177e4
LT
1336{
1337 struct in6_addr addr;
1338 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1339 if (ipv6_addr_any(&addr))
1340 return;
1341 __ipv6_dev_ac_dec(ifp->idev, &addr);
1342}
1343
073a8e0e
YH
1344static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1345{
1346 if (dev->addr_len != ETH_ALEN)
1347 return -1;
1348 memcpy(eui, dev->dev_addr, 3);
1349 memcpy(eui + 5, dev->dev_addr + 3, 3);
1350
1351 /*
1352 * The zSeries OSA network cards can be shared among various
1353 * OS instances, but the OSA cards have only one MAC address.
1354 * This leads to duplicate address conflicts in conjunction
1355 * with IPv6 if more than one instance uses the same card.
1356 *
1357 * The driver for these cards can deliver a unique 16-bit
1358 * identifier for each instance sharing the same card. It is
1359 * placed instead of 0xFFFE in the interface identifier. The
1360 * "u" bit of the interface identifier is not inverted in this
1361 * case. Hence the resulting interface identifier has local
1362 * scope according to RFC2373.
1363 */
1364 if (dev->dev_id) {
1365 eui[3] = (dev->dev_id >> 8) & 0xFF;
1366 eui[4] = dev->dev_id & 0xFF;
1367 } else {
1368 eui[3] = 0xFF;
1369 eui[4] = 0xFE;
1370 eui[0] ^= 2;
1371 }
1372 return 0;
1373}
1374
1375static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1376{
1377 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1378 if (dev->addr_len != ARCNET_ALEN)
1379 return -1;
1380 memset(eui, 0, 7);
1381 eui[7] = *(u8*)dev->dev_addr;
1382 return 0;
1383}
1384
1385static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1386{
1387 if (dev->addr_len != INFINIBAND_ALEN)
1388 return -1;
1389 memcpy(eui, dev->dev_addr + 12, 8);
1390 eui[0] |= 2;
1391 return 0;
1392}
1393
1da177e4
LT
1394static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1395{
1396 switch (dev->type) {
1397 case ARPHRD_ETHER:
1398 case ARPHRD_FDDI:
1399 case ARPHRD_IEEE802_TR:
073a8e0e 1400 return addrconf_ifid_eui48(eui, dev);
1da177e4 1401 case ARPHRD_ARCNET:
073a8e0e 1402 return addrconf_ifid_arcnet(eui, dev);
1da177e4 1403 case ARPHRD_INFINIBAND:
073a8e0e 1404 return addrconf_ifid_infiniband(eui, dev);
1da177e4
LT
1405 }
1406 return -1;
1407}
1408
1409static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1410{
1411 int err = -1;
1412 struct inet6_ifaddr *ifp;
1413
1414 read_lock_bh(&idev->lock);
1415 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1416 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1417 memcpy(eui, ifp->addr.s6_addr+8, 8);
1418 err = 0;
1419 break;
1420 }
1421 }
1422 read_unlock_bh(&idev->lock);
1423 return err;
1424}
1425
1426#ifdef CONFIG_IPV6_PRIVACY
1427/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1428static int __ipv6_regen_rndid(struct inet6_dev *idev)
1429{
1da177e4 1430regen:
955189ef 1431 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1da177e4 1432 idev->rndid[0] &= ~0x02;
1da177e4
LT
1433
1434 /*
1435 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1436 * check if generated address is not inappropriate
1437 *
1438 * - Reserved subnet anycast (RFC 2526)
1439 * 11111101 11....11 1xxxxxxx
1440 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1441 * 00-00-5E-FE-xx-xx-xx-xx
1442 * - value 0
1443 * - XXX: already assigned to an address on the device
1444 */
1445 if (idev->rndid[0] == 0xfd &&
1446 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1447 (idev->rndid[7]&0x80))
1448 goto regen;
1449 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1450 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1451 goto regen;
1452 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1453 goto regen;
1454 }
1455
1456 return 0;
1457}
1458
1459static void ipv6_regen_rndid(unsigned long data)
1460{
1461 struct inet6_dev *idev = (struct inet6_dev *) data;
1462 unsigned long expires;
1463
1464 read_lock_bh(&addrconf_lock);
1465 write_lock_bh(&idev->lock);
1466
1467 if (idev->dead)
1468 goto out;
1469
1470 if (__ipv6_regen_rndid(idev) < 0)
1471 goto out;
1472
1473 expires = jiffies +
1474 idev->cnf.temp_prefered_lft * HZ -
1475 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1476 if (time_before(expires, jiffies)) {
1477 printk(KERN_WARNING
1478 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1479 idev->dev->name);
1480 goto out;
1481 }
1482
1483 if (!mod_timer(&idev->regen_timer, expires))
1484 in6_dev_hold(idev);
1485
1486out:
1487 write_unlock_bh(&idev->lock);
1488 read_unlock_bh(&addrconf_lock);
1489 in6_dev_put(idev);
1490}
1491
1492static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1493 int ret = 0;
1494
1495 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1496 ret = __ipv6_regen_rndid(idev);
1497 return ret;
1498}
1499#endif
1500
1501/*
1502 * Add prefix route.
1503 */
1504
1505static void
1506addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
e431b8c0 1507 unsigned long expires, u32 flags)
1da177e4
LT
1508{
1509 struct in6_rtmsg rtmsg;
1510
1511 memset(&rtmsg, 0, sizeof(rtmsg));
1512 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1513 rtmsg.rtmsg_dst_len = plen;
1514 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1515 rtmsg.rtmsg_ifindex = dev->ifindex;
1516 rtmsg.rtmsg_info = expires;
1517 rtmsg.rtmsg_flags = RTF_UP|flags;
1518 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1519
1520 /* Prevent useless cloning on PtP SIT.
1521 This thing is done here expecting that the whole
1522 class of non-broadcast devices need not cloning.
1523 */
1524 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1525 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1526
0d51aa80 1527 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1da177e4
LT
1528}
1529
1530/* Create "default" multicast route to the interface */
1531
1532static void addrconf_add_mroute(struct net_device *dev)
1533{
1534 struct in6_rtmsg rtmsg;
1535
1536 memset(&rtmsg, 0, sizeof(rtmsg));
1537 ipv6_addr_set(&rtmsg.rtmsg_dst,
1538 htonl(0xFF000000), 0, 0, 0);
1539 rtmsg.rtmsg_dst_len = 8;
1540 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1541 rtmsg.rtmsg_ifindex = dev->ifindex;
1542 rtmsg.rtmsg_flags = RTF_UP;
1543 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
0d51aa80 1544 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1da177e4
LT
1545}
1546
1547static void sit_route_add(struct net_device *dev)
1548{
1549 struct in6_rtmsg rtmsg;
1550
1551 memset(&rtmsg, 0, sizeof(rtmsg));
1552
1553 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1554 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1555
1556 /* prefix length - 96 bits "::d.d.d.d" */
1557 rtmsg.rtmsg_dst_len = 96;
1558 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1559 rtmsg.rtmsg_ifindex = dev->ifindex;
1560
0d51aa80 1561 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1da177e4
LT
1562}
1563
1564static void addrconf_add_lroute(struct net_device *dev)
1565{
1566 struct in6_addr addr;
1567
1568 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1569 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1570}
1571
1572static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1573{
1574 struct inet6_dev *idev;
1575
1576 ASSERT_RTNL();
1577
1578 if ((idev = ipv6_find_idev(dev)) == NULL)
1579 return NULL;
1580
1581 /* Add default multicast route */
1582 addrconf_add_mroute(dev);
1583
1584 /* Add link local route */
1585 addrconf_add_lroute(dev);
1586 return idev;
1587}
1588
1589void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1590{
1591 struct prefix_info *pinfo;
1592 __u32 valid_lft;
1593 __u32 prefered_lft;
1594 int addr_type;
1595 unsigned long rt_expires;
1596 struct inet6_dev *in6_dev;
1597
1598 pinfo = (struct prefix_info *) opt;
1599
1600 if (len < sizeof(struct prefix_info)) {
1601 ADBG(("addrconf: prefix option too short\n"));
1602 return;
1603 }
1604
1605 /*
1606 * Validation checks ([ADDRCONF], page 19)
1607 */
1608
1609 addr_type = ipv6_addr_type(&pinfo->prefix);
1610
1611 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1612 return;
1613
1614 valid_lft = ntohl(pinfo->valid);
1615 prefered_lft = ntohl(pinfo->prefered);
1616
1617 if (prefered_lft > valid_lft) {
1618 if (net_ratelimit())
1619 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1620 return;
1621 }
1622
1623 in6_dev = in6_dev_get(dev);
1624
1625 if (in6_dev == NULL) {
1626 if (net_ratelimit())
1627 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1628 return;
1629 }
1630
1631 /*
1632 * Two things going on here:
1633 * 1) Add routes for on-link prefixes
1634 * 2) Configure prefixes with the auto flag set
1635 */
1636
1637 /* Avoid arithmetic overflow. Really, we could
1638 save rt_expires in seconds, likely valid_lft,
1639 but it would require division in fib gc, that it
1640 not good.
1641 */
1642 if (valid_lft >= 0x7FFFFFFF/HZ)
3dd4bc68 1643 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1da177e4 1644 else
3dd4bc68
YH
1645 rt_expires = valid_lft * HZ;
1646
1647 /*
1648 * We convert this (in jiffies) to clock_t later.
1649 * Avoid arithmetic overflow there as well.
1650 * Overflow can happen only if HZ < USER_HZ.
1651 */
1652 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1653 rt_expires = 0x7FFFFFFF / USER_HZ;
1da177e4
LT
1654
1655 if (pinfo->onlink) {
1656 struct rt6_info *rt;
1657 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1658
1659 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1660 if (rt->rt6i_flags&RTF_EXPIRES) {
1661 if (valid_lft == 0) {
0d51aa80 1662 ip6_del_rt(rt, NULL, NULL, NULL);
1da177e4
LT
1663 rt = NULL;
1664 } else {
3dd4bc68 1665 rt->rt6i_expires = jiffies + rt_expires;
1da177e4
LT
1666 }
1667 }
1668 } else if (valid_lft) {
1669 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
3dd4bc68 1670 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1da177e4
LT
1671 }
1672 if (rt)
1673 dst_release(&rt->u.dst);
1674 }
1675
1676 /* Try to figure out our local address for this prefix */
1677
1678 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1679 struct inet6_ifaddr * ifp;
1680 struct in6_addr addr;
1681 int create = 0, update_lft = 0;
1682
1683 if (pinfo->prefix_len == 64) {
1684 memcpy(&addr, &pinfo->prefix, 8);
1685 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1686 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1687 in6_dev_put(in6_dev);
1688 return;
1689 }
1690 goto ok;
1691 }
1692 if (net_ratelimit())
1693 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1694 pinfo->prefix_len);
1695 in6_dev_put(in6_dev);
1696 return;
1697
1698ok:
1699
1700 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1701
1702 if (ifp == NULL && valid_lft) {
1703 int max_addresses = in6_dev->cnf.max_addresses;
1704
1705 /* Do not allow to create too much of autoconfigured
1706 * addresses; this would be too easy way to crash kernel.
1707 */
1708 if (!max_addresses ||
1709 ipv6_count_addresses(in6_dev) < max_addresses)
1710 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1711 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1712
1713 if (!ifp || IS_ERR(ifp)) {
1714 in6_dev_put(in6_dev);
1715 return;
1716 }
1717
1718 update_lft = create = 1;
1719 ifp->cstamp = jiffies;
1720 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1721 }
1722
1723 if (ifp) {
1724 int flags;
1725 unsigned long now;
1726#ifdef CONFIG_IPV6_PRIVACY
1727 struct inet6_ifaddr *ift;
1728#endif
1729 u32 stored_lft;
1730
1731 /* update lifetime (RFC2462 5.5.3 e) */
1732 spin_lock(&ifp->lock);
1733 now = jiffies;
1734 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1735 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1736 else
1737 stored_lft = 0;
1738 if (!update_lft && stored_lft) {
1739 if (valid_lft > MIN_VALID_LIFETIME ||
1740 valid_lft > stored_lft)
1741 update_lft = 1;
1742 else if (stored_lft <= MIN_VALID_LIFETIME) {
1743 /* valid_lft <= stored_lft is always true */
1744 /* XXX: IPsec */
1745 update_lft = 0;
1746 } else {
1747 valid_lft = MIN_VALID_LIFETIME;
1748 if (valid_lft < prefered_lft)
1749 prefered_lft = valid_lft;
1750 update_lft = 1;
1751 }
1752 }
1753
1754 if (update_lft) {
1755 ifp->valid_lft = valid_lft;
1756 ifp->prefered_lft = prefered_lft;
1757 ifp->tstamp = now;
1758 flags = ifp->flags;
1759 ifp->flags &= ~IFA_F_DEPRECATED;
1760 spin_unlock(&ifp->lock);
1761
1762 if (!(flags&IFA_F_TENTATIVE))
1763 ipv6_ifa_notify(0, ifp);
1764 } else
1765 spin_unlock(&ifp->lock);
1766
1767#ifdef CONFIG_IPV6_PRIVACY
1768 read_lock_bh(&in6_dev->lock);
1769 /* update all temporary addresses in the list */
1770 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1771 /*
1772 * When adjusting the lifetimes of an existing
1773 * temporary address, only lower the lifetimes.
1774 * Implementations must not increase the
1775 * lifetimes of an existing temporary address
1776 * when processing a Prefix Information Option.
1777 */
1778 spin_lock(&ift->lock);
1779 flags = ift->flags;
1780 if (ift->valid_lft > valid_lft &&
1781 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1782 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1783 if (ift->prefered_lft > prefered_lft &&
1784 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1785 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1786 spin_unlock(&ift->lock);
1787 if (!(flags&IFA_F_TENTATIVE))
1788 ipv6_ifa_notify(0, ift);
1789 }
1790
1791 if (create && in6_dev->cnf.use_tempaddr > 0) {
1792 /*
1793 * When a new public address is created as described in [ADDRCONF],
1794 * also create a new temporary address.
1795 */
1796 read_unlock_bh(&in6_dev->lock);
1797 ipv6_create_tempaddr(ifp, NULL);
1798 } else {
1799 read_unlock_bh(&in6_dev->lock);
1800 }
1801#endif
1802 in6_ifa_put(ifp);
1803 addrconf_verify(0);
1804 }
1805 }
1806 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1807 in6_dev_put(in6_dev);
1808}
1809
1810/*
1811 * Set destination address.
1812 * Special case for SIT interfaces where we create a new "virtual"
1813 * device.
1814 */
1815int addrconf_set_dstaddr(void __user *arg)
1816{
1817 struct in6_ifreq ireq;
1818 struct net_device *dev;
1819 int err = -EINVAL;
1820
1821 rtnl_lock();
1822
1823 err = -EFAULT;
1824 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1825 goto err_exit;
1826
1827 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1828
1829 err = -ENODEV;
1830 if (dev == NULL)
1831 goto err_exit;
1832
1833 if (dev->type == ARPHRD_SIT) {
1834 struct ifreq ifr;
1835 mm_segment_t oldfs;
1836 struct ip_tunnel_parm p;
1837
1838 err = -EADDRNOTAVAIL;
1839 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1840 goto err_exit;
1841
1842 memset(&p, 0, sizeof(p));
1843 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1844 p.iph.saddr = 0;
1845 p.iph.version = 4;
1846 p.iph.ihl = 5;
1847 p.iph.protocol = IPPROTO_IPV6;
1848 p.iph.ttl = 64;
1849 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1850
1851 oldfs = get_fs(); set_fs(KERNEL_DS);
1852 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1853 set_fs(oldfs);
1854
1855 if (err == 0) {
1856 err = -ENOBUFS;
1857 if ((dev = __dev_get_by_name(p.name)) == NULL)
1858 goto err_exit;
1859 err = dev_open(dev);
1860 }
1861 }
1862
1863err_exit:
1864 rtnl_unlock();
1865 return err;
1866}
1867
1868/*
1869 * Manual configuration of address on an interface
1870 */
1871static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1872{
1873 struct inet6_ifaddr *ifp;
1874 struct inet6_dev *idev;
1875 struct net_device *dev;
1876 int scope;
1877
1878 ASSERT_RTNL();
1879
1880 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1881 return -ENODEV;
1882
1883 if (!(dev->flags&IFF_UP))
1884 return -ENETDOWN;
1885
1886 if ((idev = addrconf_add_dev(dev)) == NULL)
1887 return -ENOBUFS;
1888
1889 scope = ipv6_addr_scope(pfx);
1890
1891 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1892 if (!IS_ERR(ifp)) {
1893 addrconf_dad_start(ifp, 0);
1894 in6_ifa_put(ifp);
1895 return 0;
1896 }
1897
1898 return PTR_ERR(ifp);
1899}
1900
1901static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1902{
1903 struct inet6_ifaddr *ifp;
1904 struct inet6_dev *idev;
1905 struct net_device *dev;
1906
1907 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1908 return -ENODEV;
1909
1910 if ((idev = __in6_dev_get(dev)) == NULL)
1911 return -ENXIO;
1912
1913 read_lock_bh(&idev->lock);
1914 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1915 if (ifp->prefix_len == plen &&
1916 ipv6_addr_equal(pfx, &ifp->addr)) {
1917 in6_ifa_hold(ifp);
1918 read_unlock_bh(&idev->lock);
1919
1920 ipv6_del_addr(ifp);
1921
1922 /* If the last address is deleted administratively,
1923 disable IPv6 on this interface.
1924 */
1925 if (idev->addr_list == NULL)
1926 addrconf_ifdown(idev->dev, 1);
1927 return 0;
1928 }
1929 }
1930 read_unlock_bh(&idev->lock);
1931 return -EADDRNOTAVAIL;
1932}
1933
1934
1935int addrconf_add_ifaddr(void __user *arg)
1936{
1937 struct in6_ifreq ireq;
1938 int err;
1939
1940 if (!capable(CAP_NET_ADMIN))
1941 return -EPERM;
1942
1943 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1944 return -EFAULT;
1945
1946 rtnl_lock();
1947 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1948 rtnl_unlock();
1949 return err;
1950}
1951
1952int addrconf_del_ifaddr(void __user *arg)
1953{
1954 struct in6_ifreq ireq;
1955 int err;
1956
1957 if (!capable(CAP_NET_ADMIN))
1958 return -EPERM;
1959
1960 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1961 return -EFAULT;
1962
1963 rtnl_lock();
1964 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1965 rtnl_unlock();
1966 return err;
1967}
1968
1969static void sit_add_v4_addrs(struct inet6_dev *idev)
1970{
1971 struct inet6_ifaddr * ifp;
1972 struct in6_addr addr;
1973 struct net_device *dev;
1974 int scope;
1975
1976 ASSERT_RTNL();
1977
1978 memset(&addr, 0, sizeof(struct in6_addr));
1979 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1980
1981 if (idev->dev->flags&IFF_POINTOPOINT) {
1982 addr.s6_addr32[0] = htonl(0xfe800000);
1983 scope = IFA_LINK;
1984 } else {
1985 scope = IPV6_ADDR_COMPATv4;
1986 }
1987
1988 if (addr.s6_addr32[3]) {
1989 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1990 if (!IS_ERR(ifp)) {
1991 spin_lock_bh(&ifp->lock);
1992 ifp->flags &= ~IFA_F_TENTATIVE;
1993 spin_unlock_bh(&ifp->lock);
1994 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1995 in6_ifa_put(ifp);
1996 }
1997 return;
1998 }
1999
2000 for (dev = dev_base; dev != NULL; dev = dev->next) {
e5ed6399 2001 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1da177e4
LT
2002 if (in_dev && (dev->flags & IFF_UP)) {
2003 struct in_ifaddr * ifa;
2004
2005 int flag = scope;
2006
2007 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2008 int plen;
2009
2010 addr.s6_addr32[3] = ifa->ifa_local;
2011
2012 if (ifa->ifa_scope == RT_SCOPE_LINK)
2013 continue;
2014 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2015 if (idev->dev->flags&IFF_POINTOPOINT)
2016 continue;
2017 flag |= IFA_HOST;
2018 }
2019 if (idev->dev->flags&IFF_POINTOPOINT)
2020 plen = 64;
2021 else
2022 plen = 96;
2023
2024 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2025 IFA_F_PERMANENT);
2026 if (!IS_ERR(ifp)) {
2027 spin_lock_bh(&ifp->lock);
2028 ifp->flags &= ~IFA_F_TENTATIVE;
2029 spin_unlock_bh(&ifp->lock);
2030 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2031 in6_ifa_put(ifp);
2032 }
2033 }
2034 }
2035 }
2036}
2037
2038static void init_loopback(struct net_device *dev)
2039{
2040 struct inet6_dev *idev;
2041 struct inet6_ifaddr * ifp;
2042
2043 /* ::1 */
2044
2045 ASSERT_RTNL();
2046
2047 if ((idev = ipv6_find_idev(dev)) == NULL) {
2048 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2049 return;
2050 }
2051
2052 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2053 if (!IS_ERR(ifp)) {
2054 spin_lock_bh(&ifp->lock);
2055 ifp->flags &= ~IFA_F_TENTATIVE;
2056 spin_unlock_bh(&ifp->lock);
2057 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2058 in6_ifa_put(ifp);
2059 }
2060}
2061
2062static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2063{
2064 struct inet6_ifaddr * ifp;
2065
2066 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2067 if (!IS_ERR(ifp)) {
2068 addrconf_dad_start(ifp, 0);
2069 in6_ifa_put(ifp);
2070 }
2071}
2072
2073static void addrconf_dev_config(struct net_device *dev)
2074{
2075 struct in6_addr addr;
2076 struct inet6_dev * idev;
2077
2078 ASSERT_RTNL();
2079
2080 if ((dev->type != ARPHRD_ETHER) &&
2081 (dev->type != ARPHRD_FDDI) &&
2082 (dev->type != ARPHRD_IEEE802_TR) &&
2083 (dev->type != ARPHRD_ARCNET) &&
2084 (dev->type != ARPHRD_INFINIBAND)) {
2085 /* Alas, we support only Ethernet autoconfiguration. */
2086 return;
2087 }
2088
2089 idev = addrconf_add_dev(dev);
2090 if (idev == NULL)
2091 return;
2092
2093 memset(&addr, 0, sizeof(struct in6_addr));
2094 addr.s6_addr32[0] = htonl(0xFE800000);
2095
2096 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2097 addrconf_add_linklocal(idev, &addr);
2098}
2099
2100static void addrconf_sit_config(struct net_device *dev)
2101{
2102 struct inet6_dev *idev;
2103
2104 ASSERT_RTNL();
2105
2106 /*
2107 * Configure the tunnel with one of our IPv4
2108 * addresses... we should configure all of
2109 * our v4 addrs in the tunnel
2110 */
2111
2112 if ((idev = ipv6_find_idev(dev)) == NULL) {
2113 printk(KERN_DEBUG "init sit: add_dev failed\n");
2114 return;
2115 }
2116
2117 sit_add_v4_addrs(idev);
2118
2119 if (dev->flags&IFF_POINTOPOINT) {
2120 addrconf_add_mroute(dev);
2121 addrconf_add_lroute(dev);
2122 } else
2123 sit_route_add(dev);
2124}
2125
2126static inline int
2127ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2128{
2129 struct in6_addr lladdr;
2130
2131 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2132 addrconf_add_linklocal(idev, &lladdr);
2133 return 0;
2134 }
2135 return -1;
2136}
2137
2138static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2139{
2140 struct net_device *link_dev;
2141
2142 /* first try to inherit the link-local address from the link device */
2143 if (idev->dev->iflink &&
2144 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2145 if (!ipv6_inherit_linklocal(idev, link_dev))
2146 return;
2147 }
2148 /* then try to inherit it from any device */
2149 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2150 if (!ipv6_inherit_linklocal(idev, link_dev))
2151 return;
2152 }
2153 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2154}
2155
2156/*
2157 * Autoconfigure tunnel with a link-local address so routing protocols,
2158 * DHCPv6, MLD etc. can be run over the virtual link
2159 */
2160
2161static void addrconf_ip6_tnl_config(struct net_device *dev)
2162{
2163 struct inet6_dev *idev;
2164
2165 ASSERT_RTNL();
2166
2167 if ((idev = addrconf_add_dev(dev)) == NULL) {
2168 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2169 return;
2170 }
2171 ip6_tnl_add_linklocal(idev);
1da177e4
LT
2172}
2173
2174static int addrconf_notify(struct notifier_block *this, unsigned long event,
2175 void * data)
2176{
2177 struct net_device *dev = (struct net_device *) data;
2178 struct inet6_dev *idev = __in6_dev_get(dev);
c5e33bdd 2179 int run_pending = 0;
1da177e4
LT
2180
2181 switch(event) {
2182 case NETDEV_UP:
3c21edbd
YH
2183 case NETDEV_CHANGE:
2184 if (event == NETDEV_UP) {
2185 if (!netif_carrier_ok(dev)) {
2186 /* device is not ready yet. */
2187 printk(KERN_INFO
2188 "ADDRCONF(NETDEV_UP): %s: "
2189 "link is not ready\n",
2190 dev->name);
2191 break;
2192 }
99081049
KS
2193
2194 if (idev)
2195 idev->if_flags |= IF_READY;
3c21edbd
YH
2196 } else {
2197 if (!netif_carrier_ok(dev)) {
2198 /* device is still not ready. */
2199 break;
2200 }
2201
2202 if (idev) {
2203 if (idev->if_flags & IF_READY) {
2204 /* device is already configured. */
2205 break;
2206 }
2207 idev->if_flags |= IF_READY;
2208 }
2209
2210 printk(KERN_INFO
2211 "ADDRCONF(NETDEV_CHANGE): %s: "
2212 "link becomes ready\n",
2213 dev->name);
2214
c5e33bdd 2215 run_pending = 1;
3c21edbd
YH
2216 }
2217
1da177e4
LT
2218 switch(dev->type) {
2219 case ARPHRD_SIT:
2220 addrconf_sit_config(dev);
2221 break;
2222 case ARPHRD_TUNNEL6:
2223 addrconf_ip6_tnl_config(dev);
2224 break;
2225 case ARPHRD_LOOPBACK:
2226 init_loopback(dev);
2227 break;
2228
2229 default:
2230 addrconf_dev_config(dev);
2231 break;
2232 };
2233 if (idev) {
c5e33bdd
YH
2234 if (run_pending)
2235 addrconf_dad_run(idev);
2236
1da177e4
LT
2237 /* If the MTU changed during the interface down, when the
2238 interface up, the changed MTU must be reflected in the
2239 idev as well as routers.
2240 */
2241 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2242 rt6_mtu_change(dev, dev->mtu);
2243 idev->cnf.mtu6 = dev->mtu;
2244 }
2245 idev->tstamp = jiffies;
2246 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2247 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2248 stop IPv6 on this interface.
2249 */
2250 if (dev->mtu < IPV6_MIN_MTU)
2251 addrconf_ifdown(dev, event != NETDEV_DOWN);
2252 }
2253 break;
2254
2255 case NETDEV_CHANGEMTU:
2256 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2257 rt6_mtu_change(dev, dev->mtu);
2258 idev->cnf.mtu6 = dev->mtu;
2259 break;
2260 }
2261
2262 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2263
2264 case NETDEV_DOWN:
2265 case NETDEV_UNREGISTER:
2266 /*
2267 * Remove all addresses from this interface.
2268 */
2269 addrconf_ifdown(dev, event != NETDEV_DOWN);
2270 break;
3c21edbd 2271
1da177e4
LT
2272 case NETDEV_CHANGENAME:
2273#ifdef CONFIG_SYSCTL
2274 if (idev) {
2275 addrconf_sysctl_unregister(&idev->cnf);
2276 neigh_sysctl_unregister(idev->nd_parms);
2277 neigh_sysctl_register(dev, idev->nd_parms,
2278 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2279 &ndisc_ifinfo_sysctl_change,
2280 NULL);
2281 addrconf_sysctl_register(idev, &idev->cnf);
2282 }
2283#endif
2284 break;
2285 };
2286
2287 return NOTIFY_OK;
2288}
2289
2290/*
2291 * addrconf module should be notified of a device going up
2292 */
2293static struct notifier_block ipv6_dev_notf = {
2294 .notifier_call = addrconf_notify,
2295 .priority = 0
2296};
2297
2298static int addrconf_ifdown(struct net_device *dev, int how)
2299{
2300 struct inet6_dev *idev;
2301 struct inet6_ifaddr *ifa, **bifa;
2302 int i;
2303
2304 ASSERT_RTNL();
2305
2306 if (dev == &loopback_dev && how == 1)
2307 how = 0;
2308
2309 rt6_ifdown(dev);
2310 neigh_ifdown(&nd_tbl, dev);
2311
2312 idev = __in6_dev_get(dev);
2313 if (idev == NULL)
2314 return -ENODEV;
2315
2316 /* Step 1: remove reference to ipv6 device from parent device.
2317 Do not dev_put!
2318 */
2319 if (how == 1) {
2320 write_lock_bh(&addrconf_lock);
2321 dev->ip6_ptr = NULL;
2322 idev->dead = 1;
2323 write_unlock_bh(&addrconf_lock);
2324
2325 /* Step 1.5: remove snmp6 entry */
2326 snmp6_unregister_dev(idev);
2327
2328 }
2329
2330 /* Step 2: clear hash table */
2331 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2332 bifa = &inet6_addr_lst[i];
2333
2334 write_lock_bh(&addrconf_hash_lock);
2335 while ((ifa = *bifa) != NULL) {
2336 if (ifa->idev == idev) {
2337 *bifa = ifa->lst_next;
2338 ifa->lst_next = NULL;
2339 addrconf_del_timer(ifa);
2340 in6_ifa_put(ifa);
2341 continue;
2342 }
2343 bifa = &ifa->lst_next;
2344 }
2345 write_unlock_bh(&addrconf_hash_lock);
2346 }
2347
2348 write_lock_bh(&idev->lock);
2349
2350 /* Step 3: clear flags for stateless addrconf */
2351 if (how != 1)
3c21edbd 2352 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
1da177e4
LT
2353
2354 /* Step 4: clear address list */
2355#ifdef CONFIG_IPV6_PRIVACY
2356 if (how == 1 && del_timer(&idev->regen_timer))
2357 in6_dev_put(idev);
2358
2359 /* clear tempaddr list */
2360 while ((ifa = idev->tempaddr_list) != NULL) {
2361 idev->tempaddr_list = ifa->tmp_next;
2362 ifa->tmp_next = NULL;
2363 ifa->dead = 1;
2364 write_unlock_bh(&idev->lock);
2365 spin_lock_bh(&ifa->lock);
2366
2367 if (ifa->ifpub) {
2368 in6_ifa_put(ifa->ifpub);
2369 ifa->ifpub = NULL;
2370 }
2371 spin_unlock_bh(&ifa->lock);
2372 in6_ifa_put(ifa);
2373 write_lock_bh(&idev->lock);
2374 }
2375#endif
2376 while ((ifa = idev->addr_list) != NULL) {
2377 idev->addr_list = ifa->if_next;
2378 ifa->if_next = NULL;
2379 ifa->dead = 1;
2380 addrconf_del_timer(ifa);
2381 write_unlock_bh(&idev->lock);
2382
2383 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2384 in6_ifa_put(ifa);
2385
2386 write_lock_bh(&idev->lock);
2387 }
2388 write_unlock_bh(&idev->lock);
2389
2390 /* Step 5: Discard multicast list */
2391
2392 if (how == 1)
2393 ipv6_mc_destroy_dev(idev);
2394 else
2395 ipv6_mc_down(idev);
2396
2397 /* Step 5: netlink notification of this interface */
2398 idev->tstamp = jiffies;
979ad663 2399 inet6_ifinfo_notify(RTM_DELLINK, idev);
1da177e4
LT
2400
2401 /* Shot the device (if unregistered) */
2402
2403 if (how == 1) {
2404#ifdef CONFIG_SYSCTL
2405 addrconf_sysctl_unregister(&idev->cnf);
2406 neigh_sysctl_unregister(idev->nd_parms);
2407#endif
2408 neigh_parms_release(&nd_tbl, idev->nd_parms);
2409 neigh_ifdown(&nd_tbl, dev);
2410 in6_dev_put(idev);
2411 }
2412 return 0;
2413}
2414
2415static void addrconf_rs_timer(unsigned long data)
2416{
2417 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2418
2419 if (ifp->idev->cnf.forwarding)
2420 goto out;
2421
2422 if (ifp->idev->if_flags & IF_RA_RCVD) {
2423 /*
2424 * Announcement received after solicitation
2425 * was sent
2426 */
2427 goto out;
2428 }
2429
2430 spin_lock(&ifp->lock);
2431 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2432 struct in6_addr all_routers;
2433
2434 /* The wait after the last probe can be shorter */
2435 addrconf_mod_timer(ifp, AC_RS,
2436 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2437 ifp->idev->cnf.rtr_solicit_delay :
2438 ifp->idev->cnf.rtr_solicit_interval);
2439 spin_unlock(&ifp->lock);
2440
2441 ipv6_addr_all_routers(&all_routers);
2442
2443 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2444 } else {
2445 spin_unlock(&ifp->lock);
2446 /*
2447 * Note: we do not support deprecated "all on-link"
2448 * assumption any longer.
2449 */
2450 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2451 ifp->idev->dev->name);
2452 }
2453
2454out:
2455 in6_ifa_put(ifp);
2456}
2457
2458/*
2459 * Duplicate Address Detection
2460 */
3c21edbd
YH
2461static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2462{
2463 unsigned long rand_num;
2464 struct inet6_dev *idev = ifp->idev;
2465
2466 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2467 ifp->probes = idev->cnf.dad_transmits;
2468 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2469}
2470
e431b8c0 2471static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
1da177e4
LT
2472{
2473 struct inet6_dev *idev = ifp->idev;
2474 struct net_device *dev = idev->dev;
1da177e4
LT
2475
2476 addrconf_join_solict(dev, &ifp->addr);
2477
2478 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2479 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2480 flags);
2481
2482 net_srandom(ifp->addr.s6_addr32[3]);
1da177e4
LT
2483
2484 read_lock_bh(&idev->lock);
2485 if (ifp->dead)
2486 goto out;
2487 spin_lock_bh(&ifp->lock);
2488
2489 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2490 !(ifp->flags&IFA_F_TENTATIVE)) {
2491 ifp->flags &= ~IFA_F_TENTATIVE;
2492 spin_unlock_bh(&ifp->lock);
2493 read_unlock_bh(&idev->lock);
2494
2495 addrconf_dad_completed(ifp);
2496 return;
2497 }
2498
6732bade 2499 if (!(idev->if_flags & IF_READY)) {
3dd3bf83 2500 spin_unlock_bh(&ifp->lock);
6732bade 2501 read_unlock_bh(&idev->lock);
3c21edbd
YH
2502 /*
2503 * If the defice is not ready:
2504 * - keep it tentative if it is a permanent address.
2505 * - otherwise, kill it.
2506 */
2507 in6_ifa_hold(ifp);
2508 addrconf_dad_stop(ifp);
6732bade 2509 return;
3c21edbd 2510 }
6732bade
YH
2511 addrconf_dad_kick(ifp);
2512 spin_unlock_bh(&ifp->lock);
1da177e4
LT
2513out:
2514 read_unlock_bh(&idev->lock);
2515}
2516
2517static void addrconf_dad_timer(unsigned long data)
2518{
2519 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2520 struct inet6_dev *idev = ifp->idev;
2521 struct in6_addr unspec;
2522 struct in6_addr mcaddr;
2523
2524 read_lock_bh(&idev->lock);
2525 if (idev->dead) {
2526 read_unlock_bh(&idev->lock);
2527 goto out;
2528 }
2529 spin_lock_bh(&ifp->lock);
2530 if (ifp->probes == 0) {
2531 /*
2532 * DAD was successful
2533 */
2534
2535 ifp->flags &= ~IFA_F_TENTATIVE;
2536 spin_unlock_bh(&ifp->lock);
2537 read_unlock_bh(&idev->lock);
2538
2539 addrconf_dad_completed(ifp);
2540
2541 goto out;
2542 }
2543
2544 ifp->probes--;
2545 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2546 spin_unlock_bh(&ifp->lock);
2547 read_unlock_bh(&idev->lock);
2548
2549 /* send a neighbour solicitation for our addr */
2550 memset(&unspec, 0, sizeof(unspec));
2551 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2552 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2553out:
2554 in6_ifa_put(ifp);
2555}
2556
2557static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2558{
2559 struct net_device * dev = ifp->idev->dev;
2560
2561 /*
2562 * Configure the address for reception. Now it is valid.
2563 */
2564
2565 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2566
2567 /* If added prefix is link local and forwarding is off,
2568 start sending router solicitations.
2569 */
2570
2571 if (ifp->idev->cnf.forwarding == 0 &&
2572 ifp->idev->cnf.rtr_solicits > 0 &&
2573 (dev->flags&IFF_LOOPBACK) == 0 &&
2574 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2575 struct in6_addr all_routers;
2576
2577 ipv6_addr_all_routers(&all_routers);
2578
2579 /*
2580 * If a host as already performed a random delay
2581 * [...] as part of DAD [...] there is no need
2582 * to delay again before sending the first RS
2583 */
2584 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2585
2586 spin_lock_bh(&ifp->lock);
2587 ifp->probes = 1;
2588 ifp->idev->if_flags |= IF_RS_SENT;
2589 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2590 spin_unlock_bh(&ifp->lock);
2591 }
2592}
2593
c5e33bdd
YH
2594static void addrconf_dad_run(struct inet6_dev *idev) {
2595 struct inet6_ifaddr *ifp;
2596
2597 read_lock_bh(&idev->lock);
2598 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2599 spin_lock_bh(&ifp->lock);
2600 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2601 spin_unlock_bh(&ifp->lock);
2602 continue;
2603 }
2604 spin_unlock_bh(&ifp->lock);
2605 addrconf_dad_kick(ifp);
2606 }
2607 read_unlock_bh(&idev->lock);
2608}
2609
1da177e4
LT
2610#ifdef CONFIG_PROC_FS
2611struct if6_iter_state {
2612 int bucket;
2613};
2614
2615static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2616{
2617 struct inet6_ifaddr *ifa = NULL;
2618 struct if6_iter_state *state = seq->private;
2619
2620 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2621 ifa = inet6_addr_lst[state->bucket];
2622 if (ifa)
2623 break;
2624 }
2625 return ifa;
2626}
2627
2628static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2629{
2630 struct if6_iter_state *state = seq->private;
2631
2632 ifa = ifa->lst_next;
2633try_again:
2634 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2635 ifa = inet6_addr_lst[state->bucket];
2636 goto try_again;
2637 }
2638 return ifa;
2639}
2640
2641static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2642{
2643 struct inet6_ifaddr *ifa = if6_get_first(seq);
2644
2645 if (ifa)
2646 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2647 --pos;
2648 return pos ? NULL : ifa;
2649}
2650
2651static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2652{
2653 read_lock_bh(&addrconf_hash_lock);
2654 return if6_get_idx(seq, *pos);
2655}
2656
2657static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2658{
2659 struct inet6_ifaddr *ifa;
2660
2661 ifa = if6_get_next(seq, v);
2662 ++*pos;
2663 return ifa;
2664}
2665
2666static void if6_seq_stop(struct seq_file *seq, void *v)
2667{
2668 read_unlock_bh(&addrconf_hash_lock);
2669}
2670
2671static int if6_seq_show(struct seq_file *seq, void *v)
2672{
2673 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2674 seq_printf(seq,
9343e79a 2675 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
1da177e4
LT
2676 NIP6(ifp->addr),
2677 ifp->idev->dev->ifindex,
2678 ifp->prefix_len,
2679 ifp->scope,
2680 ifp->flags,
2681 ifp->idev->dev->name);
2682 return 0;
2683}
2684
2685static struct seq_operations if6_seq_ops = {
2686 .start = if6_seq_start,
2687 .next = if6_seq_next,
2688 .show = if6_seq_show,
2689 .stop = if6_seq_stop,
2690};
2691
2692static int if6_seq_open(struct inode *inode, struct file *file)
2693{
2694 struct seq_file *seq;
2695 int rc = -ENOMEM;
322f74a4 2696 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
1da177e4
LT
2697
2698 if (!s)
2699 goto out;
1da177e4
LT
2700
2701 rc = seq_open(file, &if6_seq_ops);
2702 if (rc)
2703 goto out_kfree;
2704
2705 seq = file->private_data;
2706 seq->private = s;
2707out:
2708 return rc;
2709out_kfree:
2710 kfree(s);
2711 goto out;
2712}
2713
2714static struct file_operations if6_fops = {
2715 .owner = THIS_MODULE,
2716 .open = if6_seq_open,
2717 .read = seq_read,
2718 .llseek = seq_lseek,
2719 .release = seq_release_private,
2720};
2721
2722int __init if6_proc_init(void)
2723{
2724 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2725 return -ENOMEM;
2726 return 0;
2727}
2728
2729void if6_proc_exit(void)
2730{
2731 proc_net_remove("if_inet6");
2732}
2733#endif /* CONFIG_PROC_FS */
2734
2735/*
2736 * Periodic address status verification
2737 */
2738
2739static void addrconf_verify(unsigned long foo)
2740{
2741 struct inet6_ifaddr *ifp;
2742 unsigned long now, next;
2743 int i;
2744
2745 spin_lock_bh(&addrconf_verify_lock);
2746 now = jiffies;
2747 next = now + ADDR_CHECK_FREQUENCY;
2748
2749 del_timer(&addr_chk_timer);
2750
2751 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2752
2753restart:
5d5780df 2754 read_lock(&addrconf_hash_lock);
1da177e4
LT
2755 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2756 unsigned long age;
2757#ifdef CONFIG_IPV6_PRIVACY
2758 unsigned long regen_advance;
2759#endif
2760
2761 if (ifp->flags & IFA_F_PERMANENT)
2762 continue;
2763
2764 spin_lock(&ifp->lock);
2765 age = (now - ifp->tstamp) / HZ;
2766
2767#ifdef CONFIG_IPV6_PRIVACY
2768 regen_advance = ifp->idev->cnf.regen_max_retry *
2769 ifp->idev->cnf.dad_transmits *
2770 ifp->idev->nd_parms->retrans_time / HZ;
2771#endif
2772
2773 if (age >= ifp->valid_lft) {
2774 spin_unlock(&ifp->lock);
2775 in6_ifa_hold(ifp);
5d5780df 2776 read_unlock(&addrconf_hash_lock);
1da177e4
LT
2777 ipv6_del_addr(ifp);
2778 goto restart;
2779 } else if (age >= ifp->prefered_lft) {
2780 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2781 int deprecate = 0;
2782
2783 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2784 deprecate = 1;
2785 ifp->flags |= IFA_F_DEPRECATED;
2786 }
2787
2788 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2789 next = ifp->tstamp + ifp->valid_lft * HZ;
2790
2791 spin_unlock(&ifp->lock);
2792
2793 if (deprecate) {
2794 in6_ifa_hold(ifp);
5d5780df 2795 read_unlock(&addrconf_hash_lock);
1da177e4
LT
2796
2797 ipv6_ifa_notify(0, ifp);
2798 in6_ifa_put(ifp);
2799 goto restart;
2800 }
2801#ifdef CONFIG_IPV6_PRIVACY
2802 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2803 !(ifp->flags&IFA_F_TENTATIVE)) {
2804 if (age >= ifp->prefered_lft - regen_advance) {
2805 struct inet6_ifaddr *ifpub = ifp->ifpub;
2806 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2807 next = ifp->tstamp + ifp->prefered_lft * HZ;
2808 if (!ifp->regen_count && ifpub) {
2809 ifp->regen_count++;
2810 in6_ifa_hold(ifp);
2811 in6_ifa_hold(ifpub);
2812 spin_unlock(&ifp->lock);
5d5780df 2813 read_unlock(&addrconf_hash_lock);
291d809b
HY
2814 spin_lock(&ifpub->lock);
2815 ifpub->regen_count = 0;
2816 spin_unlock(&ifpub->lock);
1da177e4
LT
2817 ipv6_create_tempaddr(ifpub, ifp);
2818 in6_ifa_put(ifpub);
2819 in6_ifa_put(ifp);
2820 goto restart;
2821 }
2822 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2823 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2824 spin_unlock(&ifp->lock);
2825#endif
2826 } else {
2827 /* ifp->prefered_lft <= ifp->valid_lft */
2828 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2829 next = ifp->tstamp + ifp->prefered_lft * HZ;
2830 spin_unlock(&ifp->lock);
2831 }
2832 }
5d5780df 2833 read_unlock(&addrconf_hash_lock);
1da177e4
LT
2834 }
2835
2836 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2837 add_timer(&addr_chk_timer);
2838 spin_unlock_bh(&addrconf_verify_lock);
2839}
2840
2841static int
2842inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2843{
2844 struct rtattr **rta = arg;
2845 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2846 struct in6_addr *pfx;
2847
2848 pfx = NULL;
2849 if (rta[IFA_ADDRESS-1]) {
2850 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2851 return -EINVAL;
2852 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2853 }
2854 if (rta[IFA_LOCAL-1]) {
2855 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2856 return -EINVAL;
2857 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2858 }
2859 if (pfx == NULL)
2860 return -EINVAL;
2861
2862 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2863}
2864
2865static int
2866inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2867{
2868 struct rtattr **rta = arg;
2869 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2870 struct in6_addr *pfx;
2871
2872 pfx = NULL;
2873 if (rta[IFA_ADDRESS-1]) {
2874 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2875 return -EINVAL;
2876 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2877 }
2878 if (rta[IFA_LOCAL-1]) {
2879 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2880 return -EINVAL;
2881 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2882 }
2883 if (pfx == NULL)
2884 return -EINVAL;
2885
2886 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2887}
2888
c5396a31
YH
2889/* Maximum length of ifa_cacheinfo attributes */
2890#define INET6_IFADDR_RTA_SPACE \
2891 RTA_SPACE(16) /* IFA_ADDRESS */ + \
2892 RTA_SPACE(sizeof(struct ifa_cacheinfo)) /* CACHEINFO */
2893
1da177e4 2894static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
b6544c0b 2895 u32 pid, u32 seq, int event, unsigned int flags)
1da177e4
LT
2896{
2897 struct ifaddrmsg *ifm;
2898 struct nlmsghdr *nlh;
2899 struct ifa_cacheinfo ci;
2900 unsigned char *b = skb->tail;
2901
b6544c0b 2902 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
1da177e4
LT
2903 ifm = NLMSG_DATA(nlh);
2904 ifm->ifa_family = AF_INET6;
2905 ifm->ifa_prefixlen = ifa->prefix_len;
2906 ifm->ifa_flags = ifa->flags;
2907 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2908 if (ifa->scope&IFA_HOST)
2909 ifm->ifa_scope = RT_SCOPE_HOST;
2910 else if (ifa->scope&IFA_LINK)
2911 ifm->ifa_scope = RT_SCOPE_LINK;
2912 else if (ifa->scope&IFA_SITE)
2913 ifm->ifa_scope = RT_SCOPE_SITE;
2914 ifm->ifa_index = ifa->idev->dev->ifindex;
2915 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2916 if (!(ifa->flags&IFA_F_PERMANENT)) {
2917 ci.ifa_prefered = ifa->prefered_lft;
2918 ci.ifa_valid = ifa->valid_lft;
2919 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2920 long tval = (jiffies - ifa->tstamp)/HZ;
2921 ci.ifa_prefered -= tval;
2922 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2923 ci.ifa_valid -= tval;
2924 }
2925 } else {
2926 ci.ifa_prefered = INFINITY_LIFE_TIME;
2927 ci.ifa_valid = INFINITY_LIFE_TIME;
2928 }
2929 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2930 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2931 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2932 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2933 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2934 nlh->nlmsg_len = skb->tail - b;
2935 return skb->len;
2936
2937nlmsg_failure:
2938rtattr_failure:
2939 skb_trim(skb, b - skb->data);
2940 return -1;
2941}
2942
2943static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
e431b8c0 2944 u32 pid, u32 seq, int event, u16 flags)
1da177e4
LT
2945{
2946 struct ifaddrmsg *ifm;
2947 struct nlmsghdr *nlh;
2948 struct ifa_cacheinfo ci;
2949 unsigned char *b = skb->tail;
2950
b6544c0b 2951 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
1da177e4
LT
2952 ifm = NLMSG_DATA(nlh);
2953 ifm->ifa_family = AF_INET6;
2954 ifm->ifa_prefixlen = 128;
2955 ifm->ifa_flags = IFA_F_PERMANENT;
2956 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2957 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2958 ifm->ifa_scope = RT_SCOPE_SITE;
2959 ifm->ifa_index = ifmca->idev->dev->ifindex;
2960 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2961 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2962 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2963 * 100 / HZ);
2964 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2965 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2966 * 100 / HZ);
2967 ci.ifa_prefered = INFINITY_LIFE_TIME;
2968 ci.ifa_valid = INFINITY_LIFE_TIME;
2969 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2970 nlh->nlmsg_len = skb->tail - b;
2971 return skb->len;
2972
2973nlmsg_failure:
2974rtattr_failure:
2975 skb_trim(skb, b - skb->data);
2976 return -1;
2977}
2978
2979static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
b6544c0b 2980 u32 pid, u32 seq, int event, unsigned int flags)
1da177e4
LT
2981{
2982 struct ifaddrmsg *ifm;
2983 struct nlmsghdr *nlh;
2984 struct ifa_cacheinfo ci;
2985 unsigned char *b = skb->tail;
2986
b6544c0b 2987 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
1da177e4
LT
2988 ifm = NLMSG_DATA(nlh);
2989 ifm->ifa_family = AF_INET6;
2990 ifm->ifa_prefixlen = 128;
2991 ifm->ifa_flags = IFA_F_PERMANENT;
2992 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2993 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2994 ifm->ifa_scope = RT_SCOPE_SITE;
2995 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2996 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2997 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2998 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2999 * 100 / HZ);
3000 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
3001 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
3002 * 100 / HZ);
3003 ci.ifa_prefered = INFINITY_LIFE_TIME;
3004 ci.ifa_valid = INFINITY_LIFE_TIME;
3005 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3006 nlh->nlmsg_len = skb->tail - b;
3007 return skb->len;
3008
3009nlmsg_failure:
3010rtattr_failure:
3011 skb_trim(skb, b - skb->data);
3012 return -1;
3013}
3014
3015enum addr_type_t
3016{
3017 UNICAST_ADDR,
3018 MULTICAST_ADDR,
3019 ANYCAST_ADDR,
3020};
3021
3022static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3023 enum addr_type_t type)
3024{
3025 int idx, ip_idx;
3026 int s_idx, s_ip_idx;
3027 int err = 1;
3028 struct net_device *dev;
3029 struct inet6_dev *idev = NULL;
3030 struct inet6_ifaddr *ifa;
3031 struct ifmcaddr6 *ifmca;
3032 struct ifacaddr6 *ifaca;
3033
3034 s_idx = cb->args[0];
3035 s_ip_idx = ip_idx = cb->args[1];
3036 read_lock(&dev_base_lock);
3037
3038 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3039 if (idx < s_idx)
3040 continue;
3041 if (idx > s_idx)
3042 s_ip_idx = 0;
3043 ip_idx = 0;
3044 if ((idev = in6_dev_get(dev)) == NULL)
3045 continue;
3046 read_lock_bh(&idev->lock);
3047 switch (type) {
3048 case UNICAST_ADDR:
ae9cda5d 3049 /* unicast address incl. temp addr */
1da177e4
LT
3050 for (ifa = idev->addr_list; ifa;
3051 ifa = ifa->if_next, ip_idx++) {
3052 if (ip_idx < s_ip_idx)
3053 continue;
3054 if ((err = inet6_fill_ifaddr(skb, ifa,
3055 NETLINK_CB(cb->skb).pid,
b6544c0b
JHS
3056 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3057 NLM_F_MULTI)) <= 0)
1da177e4
LT
3058 goto done;
3059 }
1da177e4
LT
3060 break;
3061 case MULTICAST_ADDR:
3062 /* multicast address */
3063 for (ifmca = idev->mc_list; ifmca;
3064 ifmca = ifmca->next, ip_idx++) {
3065 if (ip_idx < s_ip_idx)
3066 continue;
3067 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3068 NETLINK_CB(cb->skb).pid,
b6544c0b
JHS
3069 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3070 NLM_F_MULTI)) <= 0)
1da177e4
LT
3071 goto done;
3072 }
3073 break;
3074 case ANYCAST_ADDR:
3075 /* anycast address */
3076 for (ifaca = idev->ac_list; ifaca;
3077 ifaca = ifaca->aca_next, ip_idx++) {
3078 if (ip_idx < s_ip_idx)
3079 continue;
3080 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3081 NETLINK_CB(cb->skb).pid,
b6544c0b
JHS
3082 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3083 NLM_F_MULTI)) <= 0)
1da177e4
LT
3084 goto done;
3085 }
3086 break;
3087 default:
3088 break;
3089 }
3090 read_unlock_bh(&idev->lock);
3091 in6_dev_put(idev);
3092 }
3093done:
3094 if (err <= 0) {
3095 read_unlock_bh(&idev->lock);
3096 in6_dev_put(idev);
3097 }
3098 read_unlock(&dev_base_lock);
3099 cb->args[0] = idx;
3100 cb->args[1] = ip_idx;
3101 return skb->len;
3102}
3103
3104static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3105{
3106 enum addr_type_t type = UNICAST_ADDR;
3107 return inet6_dump_addr(skb, cb, type);
3108}
3109
3110static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3111{
3112 enum addr_type_t type = MULTICAST_ADDR;
3113 return inet6_dump_addr(skb, cb, type);
3114}
3115
3116
3117static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3118{
3119 enum addr_type_t type = ANYCAST_ADDR;
3120 return inet6_dump_addr(skb, cb, type);
3121}
3122
3123static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3124{
3125 struct sk_buff *skb;
c5396a31 3126 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + INET6_IFADDR_RTA_SPACE);
1da177e4
LT
3127
3128 skb = alloc_skb(size, GFP_ATOMIC);
3129 if (!skb) {
ac6d439d 3130 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
1da177e4
LT
3131 return;
3132 }
9ed19f33 3133 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
1da177e4 3134 kfree_skb(skb);
ac6d439d 3135 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
1da177e4
LT
3136 return;
3137 }
ac6d439d
PM
3138 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3139 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
1da177e4
LT
3140}
3141
3142static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3143 __s32 *array, int bytes)
3144{
3145 memset(array, 0, bytes);
3146 array[DEVCONF_FORWARDING] = cnf->forwarding;
3147 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3148 array[DEVCONF_MTU6] = cnf->mtu6;
3149 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3150 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3151 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3152 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3153 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3154 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3155 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3156 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3157#ifdef CONFIG_IPV6_PRIVACY
3158 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3159 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3160 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3161 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3162 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3163#endif
3164 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
65f5c7c1 3165 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
c4fd30eb 3166 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
930d6ff2
YH
3167#ifdef CONFIG_IPV6_ROUTER_PREF
3168 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
52e16356 3169 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
09c884d4
YH
3170#ifdef CONFIV_IPV6_ROUTE_INFO
3171 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3172#endif
930d6ff2 3173#endif
1da177e4
LT
3174}
3175
c5396a31
YH
3176/* Maximum length of ifinfomsg attributes */
3177#define INET6_IFINFO_RTA_SPACE \
3178 RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \
3179 RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \
3180 RTA_SPACE(sizeof(u32)) /* MTU */ + \
3181 RTA_SPACE(sizeof(int)) /* LINK */ + \
3182 RTA_SPACE(0) /* PROTINFO */ + \
3183 RTA_SPACE(sizeof(u32)) /* FLAGS */ + \
3184 RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \
3185 RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */
3186
1da177e4 3187static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
b6544c0b 3188 u32 pid, u32 seq, int event, unsigned int flags)
1da177e4
LT
3189{
3190 struct net_device *dev = idev->dev;
3191 __s32 *array = NULL;
3192 struct ifinfomsg *r;
3193 struct nlmsghdr *nlh;
3194 unsigned char *b = skb->tail;
3195 struct rtattr *subattr;
3196 __u32 mtu = dev->mtu;
3197 struct ifla_cacheinfo ci;
3198
b6544c0b 3199 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
1da177e4
LT
3200 r = NLMSG_DATA(nlh);
3201 r->ifi_family = AF_INET6;
9ef1d4c7 3202 r->__ifi_pad = 0;
1da177e4
LT
3203 r->ifi_type = dev->type;
3204 r->ifi_index = dev->ifindex;
3205 r->ifi_flags = dev_get_flags(dev);
3206 r->ifi_change = 0;
3207
3208 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3209
3210 if (dev->addr_len)
3211 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3212
3213 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3214 if (dev->ifindex != dev->iflink)
3215 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3216
3217 subattr = (struct rtattr*)skb->tail;
3218
3219 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3220
3221 /* return the device flags */
3222 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3223
3224 /* return interface cacheinfo */
3225 ci.max_reasm_len = IPV6_MAXPLEN;
3226 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3227 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3228 ci.reachable_time = idev->nd_parms->reachable_time;
3229 ci.retrans_time = idev->nd_parms->retrans_time;
3230 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3231
3232 /* return the device sysctl params */
3233 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3234 goto rtattr_failure;
3235 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3236 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3237
3238 /* XXX - Statistics/MC not implemented */
3239 subattr->rta_len = skb->tail - (u8*)subattr;
3240
3241 nlh->nlmsg_len = skb->tail - b;
3242 kfree(array);
3243 return skb->len;
3244
3245nlmsg_failure:
3246rtattr_failure:
a51482bd 3247 kfree(array);
1da177e4
LT
3248 skb_trim(skb, b - skb->data);
3249 return -1;
3250}
3251
3252static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3253{
3254 int idx, err;
3255 int s_idx = cb->args[0];
3256 struct net_device *dev;
3257 struct inet6_dev *idev;
3258
3259 read_lock(&dev_base_lock);
3260 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3261 if (idx < s_idx)
3262 continue;
3263 if ((idev = in6_dev_get(dev)) == NULL)
3264 continue;
3265 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
b6544c0b 3266 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
1da177e4
LT
3267 in6_dev_put(idev);
3268 if (err <= 0)
3269 break;
3270 }
3271 read_unlock(&dev_base_lock);
3272 cb->args[0] = idx;
3273
3274 return skb->len;
3275}
3276
3277void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3278{
3279 struct sk_buff *skb;
c5396a31 3280 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) + INET6_IFINFO_RTA_SPACE);
1da177e4
LT
3281
3282 skb = alloc_skb(size, GFP_ATOMIC);
3283 if (!skb) {
ac6d439d 3284 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
1da177e4
LT
3285 return;
3286 }
9ed19f33 3287 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
1da177e4 3288 kfree_skb(skb);
ac6d439d 3289 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
1da177e4
LT
3290 return;
3291 }
ac6d439d
PM
3292 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3293 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
1da177e4
LT
3294}
3295
c5396a31
YH
3296/* Maximum length of prefix_cacheinfo attributes */
3297#define INET6_PREFIX_RTA_SPACE \
3298 RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \
3299 RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */
3300
1da177e4 3301static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
b6544c0b
JHS
3302 struct prefix_info *pinfo, u32 pid, u32 seq,
3303 int event, unsigned int flags)
1da177e4
LT
3304{
3305 struct prefixmsg *pmsg;
3306 struct nlmsghdr *nlh;
3307 unsigned char *b = skb->tail;
3308 struct prefix_cacheinfo ci;
3309
b6544c0b 3310 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
1da177e4
LT
3311 pmsg = NLMSG_DATA(nlh);
3312 pmsg->prefix_family = AF_INET6;
8a47077a
PM
3313 pmsg->prefix_pad1 = 0;
3314 pmsg->prefix_pad2 = 0;
1da177e4
LT
3315 pmsg->prefix_ifindex = idev->dev->ifindex;
3316 pmsg->prefix_len = pinfo->prefix_len;
3317 pmsg->prefix_type = pinfo->type;
8a47077a 3318 pmsg->prefix_pad3 = 0;
1da177e4
LT
3319
3320 pmsg->prefix_flags = 0;
3321 if (pinfo->onlink)
3322 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3323 if (pinfo->autoconf)
3324 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3325
3326 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3327
3328 ci.preferred_time = ntohl(pinfo->prefered);
3329 ci.valid_time = ntohl(pinfo->valid);
3330 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3331
3332 nlh->nlmsg_len = skb->tail - b;
3333 return skb->len;
3334
3335nlmsg_failure:
3336rtattr_failure:
3337 skb_trim(skb, b - skb->data);
3338 return -1;
3339}
3340
3341static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3342 struct prefix_info *pinfo)
3343{
3344 struct sk_buff *skb;
c5396a31 3345 int size = NLMSG_SPACE(sizeof(struct prefixmsg) + INET6_PREFIX_RTA_SPACE);
1da177e4
LT
3346
3347 skb = alloc_skb(size, GFP_ATOMIC);
3348 if (!skb) {
ac6d439d 3349 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
1da177e4
LT
3350 return;
3351 }
9ed19f33 3352 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
1da177e4 3353 kfree_skb(skb);
ac6d439d 3354 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
1da177e4
LT
3355 return;
3356 }
ac6d439d
PM
3357 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3358 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
1da177e4
LT
3359}
3360
db46edc6 3361static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
1da177e4
LT
3362 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3363 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3364 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3365 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3366 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3367 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3368 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3369 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3370 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3371 .dumpit = inet6_dump_fib, },
3372};
3373
3374static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3375{
3376 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3377
3378 switch (event) {
3379 case RTM_NEWADDR:
4641e7a3 3380 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
1da177e4
LT
3381 if (ifp->idev->cnf.forwarding)
3382 addrconf_join_anycast(ifp);
3383 break;
3384 case RTM_DELADDR:
3385 if (ifp->idev->cnf.forwarding)
3386 addrconf_leave_anycast(ifp);
3387 addrconf_leave_solict(ifp->idev, &ifp->addr);
3388 dst_hold(&ifp->rt->u.dst);
0d51aa80 3389 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
1da177e4 3390 dst_free(&ifp->rt->u.dst);
1da177e4
LT
3391 break;
3392 }
3393}
3394
3395static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3396{
3397 read_lock_bh(&addrconf_lock);
3398 if (likely(ifp->idev->dead == 0))
3399 __ipv6_ifa_notify(event, ifp);
3400 read_unlock_bh(&addrconf_lock);
3401}
3402
3403#ifdef CONFIG_SYSCTL
3404
3405static
3406int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3407 void __user *buffer, size_t *lenp, loff_t *ppos)
3408{
3409 int *valp = ctl->data;
3410 int val = *valp;
3411 int ret;
3412
3413 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3414
3415 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3416 if (valp != &ipv6_devconf.forwarding) {
3417 if ((!*valp) ^ (!val)) {
3418 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3419 if (idev == NULL)
3420 return ret;
3421 dev_forward_change(idev);
3422 }
3423 } else {
3424 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3425 addrconf_forward_change();
3426 }
3427 if (*valp)
3428 rt6_purge_dflt_routers();
3429 }
3430
3431 return ret;
3432}
3433
3434static int addrconf_sysctl_forward_strategy(ctl_table *table,
3435 int __user *name, int nlen,
3436 void __user *oldval,
3437 size_t __user *oldlenp,
3438 void __user *newval, size_t newlen,
3439 void **context)
3440{
3441 int *valp = table->data;
3442 int new;
3443
3444 if (!newval || !newlen)
3445 return 0;
3446 if (newlen != sizeof(int))
3447 return -EINVAL;
3448 if (get_user(new, (int __user *)newval))
3449 return -EFAULT;
3450 if (new == *valp)
3451 return 0;
3452 if (oldval && oldlenp) {
3453 size_t len;
3454 if (get_user(len, oldlenp))
3455 return -EFAULT;
3456 if (len) {
3457 if (len > table->maxlen)
3458 len = table->maxlen;
3459 if (copy_to_user(oldval, valp, len))
3460 return -EFAULT;
3461 if (put_user(len, oldlenp))
3462 return -EFAULT;
3463 }
3464 }
3465
3466 if (valp != &ipv6_devconf_dflt.forwarding) {
3467 if (valp != &ipv6_devconf.forwarding) {
3468 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3469 int changed;
3470 if (unlikely(idev == NULL))
3471 return -ENODEV;
3472 changed = (!*valp) ^ (!new);
3473 *valp = new;
3474 if (changed)
3475 dev_forward_change(idev);
3476 } else {
3477 *valp = new;
3478 addrconf_forward_change();
3479 }
3480
3481 if (*valp)
3482 rt6_purge_dflt_routers();
3483 } else
3484 *valp = new;
3485
3486 return 1;
3487}
3488
3489static struct addrconf_sysctl_table
3490{
3491 struct ctl_table_header *sysctl_header;
3492 ctl_table addrconf_vars[__NET_IPV6_MAX];
3493 ctl_table addrconf_dev[2];
3494 ctl_table addrconf_conf_dir[2];
3495 ctl_table addrconf_proto_dir[2];
3496 ctl_table addrconf_root_dir[2];
3497} addrconf_sysctl = {
3498 .sysctl_header = NULL,
3499 .addrconf_vars = {
3500 {
3501 .ctl_name = NET_IPV6_FORWARDING,
3502 .procname = "forwarding",
3503 .data = &ipv6_devconf.forwarding,
3504 .maxlen = sizeof(int),
3505 .mode = 0644,
3506 .proc_handler = &addrconf_sysctl_forward,
3507 .strategy = &addrconf_sysctl_forward_strategy,
3508 },
3509 {
3510 .ctl_name = NET_IPV6_HOP_LIMIT,
3511 .procname = "hop_limit",
3512 .data = &ipv6_devconf.hop_limit,
3513 .maxlen = sizeof(int),
3514 .mode = 0644,
3515 .proc_handler = proc_dointvec,
3516 },
3517 {
3518 .ctl_name = NET_IPV6_MTU,
3519 .procname = "mtu",
3520 .data = &ipv6_devconf.mtu6,
3521 .maxlen = sizeof(int),
3522 .mode = 0644,
3523 .proc_handler = &proc_dointvec,
3524 },
3525 {
3526 .ctl_name = NET_IPV6_ACCEPT_RA,
3527 .procname = "accept_ra",
3528 .data = &ipv6_devconf.accept_ra,
3529 .maxlen = sizeof(int),
3530 .mode = 0644,
3531 .proc_handler = &proc_dointvec,
3532 },
3533 {
3534 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3535 .procname = "accept_redirects",
3536 .data = &ipv6_devconf.accept_redirects,
3537 .maxlen = sizeof(int),
3538 .mode = 0644,
3539 .proc_handler = &proc_dointvec,
3540 },
3541 {
3542 .ctl_name = NET_IPV6_AUTOCONF,
3543 .procname = "autoconf",
3544 .data = &ipv6_devconf.autoconf,
3545 .maxlen = sizeof(int),
3546 .mode = 0644,
3547 .proc_handler = &proc_dointvec,
3548 },
3549 {
3550 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3551 .procname = "dad_transmits",
3552 .data = &ipv6_devconf.dad_transmits,
3553 .maxlen = sizeof(int),
3554 .mode = 0644,
3555 .proc_handler = &proc_dointvec,
3556 },
3557 {
3558 .ctl_name = NET_IPV6_RTR_SOLICITS,
3559 .procname = "router_solicitations",
3560 .data = &ipv6_devconf.rtr_solicits,
3561 .maxlen = sizeof(int),
3562 .mode = 0644,
3563 .proc_handler = &proc_dointvec,
3564 },
3565 {
3566 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3567 .procname = "router_solicitation_interval",
3568 .data = &ipv6_devconf.rtr_solicit_interval,
3569 .maxlen = sizeof(int),
3570 .mode = 0644,
3571 .proc_handler = &proc_dointvec_jiffies,
3572 .strategy = &sysctl_jiffies,
3573 },
3574 {
3575 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3576 .procname = "router_solicitation_delay",
3577 .data = &ipv6_devconf.rtr_solicit_delay,
3578 .maxlen = sizeof(int),
3579 .mode = 0644,
3580 .proc_handler = &proc_dointvec_jiffies,
3581 .strategy = &sysctl_jiffies,
3582 },
3583 {
3584 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3585 .procname = "force_mld_version",
3586 .data = &ipv6_devconf.force_mld_version,
3587 .maxlen = sizeof(int),
3588 .mode = 0644,
3589 .proc_handler = &proc_dointvec,
3590 },
3591#ifdef CONFIG_IPV6_PRIVACY
3592 {
3593 .ctl_name = NET_IPV6_USE_TEMPADDR,
3594 .procname = "use_tempaddr",
3595 .data = &ipv6_devconf.use_tempaddr,
3596 .maxlen = sizeof(int),
3597 .mode = 0644,
3598 .proc_handler = &proc_dointvec,
3599 },
3600 {
3601 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3602 .procname = "temp_valid_lft",
3603 .data = &ipv6_devconf.temp_valid_lft,
3604 .maxlen = sizeof(int),
3605 .mode = 0644,
3606 .proc_handler = &proc_dointvec,
3607 },
3608 {
3609 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3610 .procname = "temp_prefered_lft",
3611 .data = &ipv6_devconf.temp_prefered_lft,
3612 .maxlen = sizeof(int),
3613 .mode = 0644,
3614 .proc_handler = &proc_dointvec,
3615 },
3616 {
3617 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3618 .procname = "regen_max_retry",
3619 .data = &ipv6_devconf.regen_max_retry,
3620 .maxlen = sizeof(int),
3621 .mode = 0644,
3622 .proc_handler = &proc_dointvec,
3623 },
3624 {
3625 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3626 .procname = "max_desync_factor",
3627 .data = &ipv6_devconf.max_desync_factor,
3628 .maxlen = sizeof(int),
3629 .mode = 0644,
3630 .proc_handler = &proc_dointvec,
3631 },
3632#endif
3633 {
3634 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3635 .procname = "max_addresses",
3636 .data = &ipv6_devconf.max_addresses,
3637 .maxlen = sizeof(int),
3638 .mode = 0644,
3639 .proc_handler = &proc_dointvec,
3640 },
65f5c7c1
YH
3641 {
3642 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3643 .procname = "accept_ra_defrtr",
3644 .data = &ipv6_devconf.accept_ra_defrtr,
3645 .maxlen = sizeof(int),
3646 .mode = 0644,
3647 .proc_handler = &proc_dointvec,
3648 },
c4fd30eb
YH
3649 {
3650 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3651 .procname = "accept_ra_pinfo",
3652 .data = &ipv6_devconf.accept_ra_pinfo,
3653 .maxlen = sizeof(int),
3654 .mode = 0644,
3655 .proc_handler = &proc_dointvec,
3656 },
930d6ff2
YH
3657#ifdef CONFIG_IPV6_ROUTER_PREF
3658 {
3659 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3660 .procname = "accept_ra_rtr_pref",
3661 .data = &ipv6_devconf.accept_ra_rtr_pref,
3662 .maxlen = sizeof(int),
3663 .mode = 0644,
3664 .proc_handler = &proc_dointvec,
3665 },
52e16356
YH
3666 {
3667 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
3668 .procname = "router_probe_interval",
3669 .data = &ipv6_devconf.rtr_probe_interval,
3670 .maxlen = sizeof(int),
3671 .mode = 0644,
3672 .proc_handler = &proc_dointvec_jiffies,
3673 .strategy = &sysctl_jiffies,
3674 },
09c884d4
YH
3675#ifdef CONFIV_IPV6_ROUTE_INFO
3676 {
3677 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
3678 .procname = "accept_ra_rt_info_max_plen",
3679 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
3680 .maxlen = sizeof(int),
3681 .mode = 0644,
3682 .proc_handler = &proc_dointvec,
3683 },
3684#endif
930d6ff2 3685#endif
1da177e4
LT
3686 {
3687 .ctl_name = 0, /* sentinel */
3688 }
3689 },
3690 .addrconf_dev = {
3691 {
3692 .ctl_name = NET_PROTO_CONF_ALL,
3693 .procname = "all",
3694 .mode = 0555,
3695 .child = addrconf_sysctl.addrconf_vars,
3696 },
3697 {
3698 .ctl_name = 0, /* sentinel */
3699 }
3700 },
3701 .addrconf_conf_dir = {
3702 {
3703 .ctl_name = NET_IPV6_CONF,
3704 .procname = "conf",
3705 .mode = 0555,
3706 .child = addrconf_sysctl.addrconf_dev,
3707 },
3708 {
3709 .ctl_name = 0, /* sentinel */
3710 }
3711 },
3712 .addrconf_proto_dir = {
3713 {
3714 .ctl_name = NET_IPV6,
3715 .procname = "ipv6",
3716 .mode = 0555,
3717 .child = addrconf_sysctl.addrconf_conf_dir,
3718 },
3719 {
3720 .ctl_name = 0, /* sentinel */
3721 }
3722 },
3723 .addrconf_root_dir = {
3724 {
3725 .ctl_name = CTL_NET,
3726 .procname = "net",
3727 .mode = 0555,
3728 .child = addrconf_sysctl.addrconf_proto_dir,
3729 },
3730 {
3731 .ctl_name = 0, /* sentinel */
3732 }
3733 },
3734};
3735
3736static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3737{
3738 int i;
3739 struct net_device *dev = idev ? idev->dev : NULL;
3740 struct addrconf_sysctl_table *t;
3741 char *dev_name = NULL;
3742
3743 t = kmalloc(sizeof(*t), GFP_KERNEL);
3744 if (t == NULL)
3745 return;
3746 memcpy(t, &addrconf_sysctl, sizeof(*t));
3747 for (i=0; t->addrconf_vars[i].data; i++) {
3748 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3749 t->addrconf_vars[i].de = NULL;
3750 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3751 }
3752 if (dev) {
3753 dev_name = dev->name;
3754 t->addrconf_dev[0].ctl_name = dev->ifindex;
3755 } else {
3756 dev_name = "default";
3757 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3758 }
3759
3760 /*
3761 * Make a copy of dev_name, because '.procname' is regarded as const
3762 * by sysctl and we wouldn't want anyone to change it under our feet
3763 * (see SIOCSIFNAME).
3764 */
543537bd 3765 dev_name = kstrdup(dev_name, GFP_KERNEL);
1da177e4
LT
3766 if (!dev_name)
3767 goto free;
3768
3769 t->addrconf_dev[0].procname = dev_name;
3770
3771 t->addrconf_dev[0].child = t->addrconf_vars;
3772 t->addrconf_dev[0].de = NULL;
3773 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3774 t->addrconf_conf_dir[0].de = NULL;
3775 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3776 t->addrconf_proto_dir[0].de = NULL;
3777 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3778 t->addrconf_root_dir[0].de = NULL;
3779
3780 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3781 if (t->sysctl_header == NULL)
3782 goto free_procname;
3783 else
3784 p->sysctl = t;
3785 return;
3786
3787 /* error path */
3788 free_procname:
3789 kfree(dev_name);
3790 free:
3791 kfree(t);
3792
3793 return;
3794}
3795
3796static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3797{
3798 if (p->sysctl) {
3799 struct addrconf_sysctl_table *t = p->sysctl;
3800 p->sysctl = NULL;
3801 unregister_sysctl_table(t->sysctl_header);
3802 kfree(t->addrconf_dev[0].procname);
3803 kfree(t);
3804 }
3805}
3806
3807
3808#endif
3809
3810/*
3811 * Device notifier
3812 */
3813
3814int register_inet6addr_notifier(struct notifier_block *nb)
3815{
e041c683 3816 return atomic_notifier_chain_register(&inet6addr_chain, nb);
1da177e4
LT
3817}
3818
3819int unregister_inet6addr_notifier(struct notifier_block *nb)
3820{
e041c683 3821 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
1da177e4
LT
3822}
3823
3824/*
3825 * Init / cleanup code
3826 */
3827
3828int __init addrconf_init(void)
3829{
3830 int err = 0;
3831
3832 /* The addrconf netdev notifier requires that loopback_dev
3833 * has it's ipv6 private information allocated and setup
3834 * before it can bring up and give link-local addresses
3835 * to other devices which are up.
3836 *
3837 * Unfortunately, loopback_dev is not necessarily the first
3838 * entry in the global dev_base list of net devices. In fact,
3839 * it is likely to be the very last entry on that list.
3840 * So this causes the notifier registry below to try and
3841 * give link-local addresses to all devices besides loopback_dev
3842 * first, then loopback_dev, which cases all the non-loopback_dev
3843 * devices to fail to get a link-local address.
3844 *
3845 * So, as a temporary fix, allocate the ipv6 structure for
3846 * loopback_dev first by hand.
3847 * Longer term, all of the dependencies ipv6 has upon the loopback
3848 * device and it being up should be removed.
3849 */
3850 rtnl_lock();
3851 if (!ipv6_add_dev(&loopback_dev))
3852 err = -ENOMEM;
3853 rtnl_unlock();
3854 if (err)
3855 return err;
3856
c62dba90
HX
3857 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3858
1da177e4
LT
3859 register_netdevice_notifier(&ipv6_dev_notf);
3860
1da177e4
LT
3861 addrconf_verify(0);
3862 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3863#ifdef CONFIG_SYSCTL
3864 addrconf_sysctl.sysctl_header =
3865 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3866 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3867#endif
3868
3869 return 0;
3870}
3871
3872void __exit addrconf_cleanup(void)
3873{
3874 struct net_device *dev;
3875 struct inet6_dev *idev;
3876 struct inet6_ifaddr *ifa;
3877 int i;
3878
3879 unregister_netdevice_notifier(&ipv6_dev_notf);
3880
3881 rtnetlink_links[PF_INET6] = NULL;
3882#ifdef CONFIG_SYSCTL
3883 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3884 addrconf_sysctl_unregister(&ipv6_devconf);
3885#endif
3886
3887 rtnl_lock();
3888
3889 /*
3890 * clean dev list.
3891 */
3892
3893 for (dev=dev_base; dev; dev=dev->next) {
3894 if ((idev = __in6_dev_get(dev)) == NULL)
3895 continue;
3896 addrconf_ifdown(dev, 1);
3897 }
3898 addrconf_ifdown(&loopback_dev, 2);
3899
3900 /*
3901 * Check hash table.
3902 */
3903
3904 write_lock_bh(&addrconf_hash_lock);
3905 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3906 for (ifa=inet6_addr_lst[i]; ifa; ) {
3907 struct inet6_ifaddr *bifa;
3908
3909 bifa = ifa;
3910 ifa = ifa->lst_next;
3911 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3912 /* Do not free it; something is wrong.
3913 Now we can investigate it with debugger.
3914 */
3915 }
3916 }
3917 write_unlock_bh(&addrconf_hash_lock);
3918
3919 del_timer(&addr_chk_timer);
3920
3921 rtnl_unlock();
3922
1da177e4
LT
3923#ifdef CONFIG_PROC_FS
3924 proc_net_remove("if_inet6");
3925#endif
3926}