Fix some cases of missed IPV6 DAD
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / addrconf.c
blobe26b4736e59cc97fa31f634a27efe44635604e0c
1 /*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
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.
18 * Changes:
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.
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
40 * status etc.
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/net.h>
48 #include <linux/in6.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_addr.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
60 #include <linux/capability.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
65 #include <net/sock.h>
66 #include <net/snmp.h>
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 <net/netlink.h>
76 #include <net/pkt_sched.h>
77 #include <linux/if_tunnel.h>
78 #include <linux/rtnetlink.h>
80 #ifdef CONFIG_IPV6_PRIVACY
81 #include <linux/random.h>
82 #endif
84 #include <asm/uaccess.h>
85 #include <asm/unaligned.h>
87 #include <linux/proc_fs.h>
88 #include <linux/seq_file.h>
90 /* Set to 3 to get tracing... */
91 #define ACONF_DEBUG 2
93 #if ACONF_DEBUG >= 3
94 #define ADBG(x) printk x
95 #else
96 #define ADBG(x)
97 #endif
99 #define INFINITY_LIFE_TIME 0xFFFFFFFF
100 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
102 #ifdef CONFIG_SYSCTL
103 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
104 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
105 #endif
107 #ifdef CONFIG_IPV6_PRIVACY
108 static int __ipv6_regen_rndid(struct inet6_dev *idev);
109 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
110 static void ipv6_regen_rndid(unsigned long data);
112 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
113 #endif
115 static int ipv6_count_addresses(struct inet6_dev *idev);
118 * Configured unicast address hash table
120 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
121 static DEFINE_RWLOCK(addrconf_hash_lock);
123 static void addrconf_verify(unsigned long);
125 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
126 static DEFINE_SPINLOCK(addrconf_verify_lock);
128 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
129 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
131 static int addrconf_ifdown(struct net_device *dev, int how);
133 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
134 static void addrconf_dad_timer(unsigned long data);
135 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
136 static void addrconf_dad_run(struct inet6_dev *idev);
137 static void addrconf_rs_timer(unsigned long data);
138 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
141 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
142 struct prefix_info *pinfo);
143 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
145 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
147 struct ipv6_devconf ipv6_devconf __read_mostly = {
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,
167 .accept_ra_defrtr = 1,
168 .accept_ra_pinfo = 1,
169 #ifdef CONFIG_IPV6_ROUTER_PREF
170 .accept_ra_rtr_pref = 1,
171 .rtr_probe_interval = 60 * HZ,
172 #ifdef CONFIG_IPV6_ROUTE_INFO
173 .accept_ra_rt_info_max_plen = 0,
174 #endif
175 #endif
176 .proxy_ndp = 0,
177 .accept_source_route = 0, /* we do not accept RH0 by default. */
180 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
181 .forwarding = 0,
182 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
183 .mtu6 = IPV6_MIN_MTU,
184 .accept_ra = 1,
185 .accept_redirects = 1,
186 .autoconf = 1,
187 .dad_transmits = 1,
188 .rtr_solicits = MAX_RTR_SOLICITATIONS,
189 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
190 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
191 #ifdef CONFIG_IPV6_PRIVACY
192 .use_tempaddr = 0,
193 .temp_valid_lft = TEMP_VALID_LIFETIME,
194 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
195 .regen_max_retry = REGEN_MAX_RETRY,
196 .max_desync_factor = MAX_DESYNC_FACTOR,
197 #endif
198 .max_addresses = IPV6_MAX_ADDRESSES,
199 .accept_ra_defrtr = 1,
200 .accept_ra_pinfo = 1,
201 #ifdef CONFIG_IPV6_ROUTER_PREF
202 .accept_ra_rtr_pref = 1,
203 .rtr_probe_interval = 60 * HZ,
204 #ifdef CONFIG_IPV6_ROUTE_INFO
205 .accept_ra_rt_info_max_plen = 0,
206 #endif
207 #endif
208 .proxy_ndp = 0,
209 .accept_source_route = 0, /* we do not accept RH0 by default. */
212 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
213 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
214 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
216 /* Check if a valid qdisc is available */
217 static inline int addrconf_qdisc_ok(struct net_device *dev)
219 return (dev->qdisc != &noop_qdisc);
222 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
224 if (del_timer(&ifp->timer))
225 __in6_ifa_put(ifp);
228 enum addrconf_timer_t
230 AC_NONE,
231 AC_DAD,
232 AC_RS,
235 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
236 enum addrconf_timer_t what,
237 unsigned long when)
239 if (!del_timer(&ifp->timer))
240 in6_ifa_hold(ifp);
242 switch (what) {
243 case AC_DAD:
244 ifp->timer.function = addrconf_dad_timer;
245 break;
246 case AC_RS:
247 ifp->timer.function = addrconf_rs_timer;
248 break;
249 default:;
251 ifp->timer.expires = jiffies + when;
252 add_timer(&ifp->timer);
255 static int snmp6_alloc_dev(struct inet6_dev *idev)
257 int err = -ENOMEM;
259 if (!idev || !idev->dev)
260 return -EINVAL;
262 if (snmp_mib_init((void **)idev->stats.ipv6,
263 sizeof(struct ipstats_mib),
264 __alignof__(struct ipstats_mib)) < 0)
265 goto err_ip;
266 if (snmp_mib_init((void **)idev->stats.icmpv6,
267 sizeof(struct icmpv6_mib),
268 __alignof__(struct icmpv6_mib)) < 0)
269 goto err_icmp;
271 return 0;
273 err_icmp:
274 snmp_mib_free((void **)idev->stats.ipv6);
275 err_ip:
276 return err;
279 static int snmp6_free_dev(struct inet6_dev *idev)
281 snmp_mib_free((void **)idev->stats.icmpv6);
282 snmp_mib_free((void **)idev->stats.ipv6);
283 return 0;
286 /* Nobody refers to this device, we may destroy it. */
288 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
290 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
291 kfree(idev);
294 void in6_dev_finish_destroy(struct inet6_dev *idev)
296 struct net_device *dev = idev->dev;
297 BUG_TRAP(idev->addr_list==NULL);
298 BUG_TRAP(idev->mc_list==NULL);
299 #ifdef NET_REFCNT_DEBUG
300 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
301 #endif
302 dev_put(dev);
303 if (!idev->dead) {
304 printk("Freeing alive inet6 device %p\n", idev);
305 return;
307 snmp6_free_dev(idev);
308 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
311 EXPORT_SYMBOL(in6_dev_finish_destroy);
313 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
315 struct inet6_dev *ndev;
316 struct in6_addr maddr;
318 ASSERT_RTNL();
320 if (dev->mtu < IPV6_MIN_MTU)
321 return NULL;
323 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
325 if (ndev == NULL)
326 return NULL;
328 rwlock_init(&ndev->lock);
329 ndev->dev = dev;
330 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
331 ndev->cnf.mtu6 = dev->mtu;
332 ndev->cnf.sysctl = NULL;
333 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
334 if (ndev->nd_parms == NULL) {
335 kfree(ndev);
336 return NULL;
338 /* We refer to the device */
339 dev_hold(dev);
341 if (snmp6_alloc_dev(ndev) < 0) {
342 ADBG((KERN_WARNING
343 "%s(): cannot allocate memory for statistics; dev=%s.\n",
344 __FUNCTION__, dev->name));
345 neigh_parms_release(&nd_tbl, ndev->nd_parms);
346 ndev->dead = 1;
347 in6_dev_finish_destroy(ndev);
348 return NULL;
351 if (snmp6_register_dev(ndev) < 0) {
352 ADBG((KERN_WARNING
353 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
354 __FUNCTION__, dev->name));
355 neigh_parms_release(&nd_tbl, ndev->nd_parms);
356 ndev->dead = 1;
357 in6_dev_finish_destroy(ndev);
358 return NULL;
361 /* One reference from device. We must do this before
362 * we invoke __ipv6_regen_rndid().
364 in6_dev_hold(ndev);
366 #ifdef CONFIG_IPV6_PRIVACY
367 init_timer(&ndev->regen_timer);
368 ndev->regen_timer.function = ipv6_regen_rndid;
369 ndev->regen_timer.data = (unsigned long) ndev;
370 if ((dev->flags&IFF_LOOPBACK) ||
371 dev->type == ARPHRD_TUNNEL ||
372 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
373 dev->type == ARPHRD_SIT ||
374 #endif
375 dev->type == ARPHRD_NONE) {
376 printk(KERN_INFO
377 "%s: Disabled Privacy Extensions\n",
378 dev->name);
379 ndev->cnf.use_tempaddr = -1;
380 } else {
381 in6_dev_hold(ndev);
382 ipv6_regen_rndid((unsigned long) ndev);
384 #endif
386 if (netif_running(dev) && addrconf_qdisc_ok(dev))
387 ndev->if_flags |= IF_READY;
389 ipv6_mc_init_dev(ndev);
390 ndev->tstamp = jiffies;
391 #ifdef CONFIG_SYSCTL
392 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
393 NET_IPV6_NEIGH, "ipv6",
394 &ndisc_ifinfo_sysctl_change,
395 NULL);
396 addrconf_sysctl_register(ndev, &ndev->cnf);
397 #endif
398 /* protected by rtnl_lock */
399 rcu_assign_pointer(dev->ip6_ptr, ndev);
401 /* Join all-node multicast group */
402 ipv6_addr_all_nodes(&maddr);
403 ipv6_dev_mc_inc(dev, &maddr);
405 return ndev;
408 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
410 struct inet6_dev *idev;
412 ASSERT_RTNL();
414 if ((idev = __in6_dev_get(dev)) == NULL) {
415 if ((idev = ipv6_add_dev(dev)) == NULL)
416 return NULL;
419 if (dev->flags&IFF_UP)
420 ipv6_mc_up(idev);
421 return idev;
424 #ifdef CONFIG_SYSCTL
425 static void dev_forward_change(struct inet6_dev *idev)
427 struct net_device *dev;
428 struct inet6_ifaddr *ifa;
429 struct in6_addr addr;
431 if (!idev)
432 return;
433 dev = idev->dev;
434 if (dev && (dev->flags & IFF_MULTICAST)) {
435 ipv6_addr_all_routers(&addr);
437 if (idev->cnf.forwarding)
438 ipv6_dev_mc_inc(dev, &addr);
439 else
440 ipv6_dev_mc_dec(dev, &addr);
442 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
443 if (ifa->flags&IFA_F_TENTATIVE)
444 continue;
445 if (idev->cnf.forwarding)
446 addrconf_join_anycast(ifa);
447 else
448 addrconf_leave_anycast(ifa);
453 static void addrconf_forward_change(void)
455 struct net_device *dev;
456 struct inet6_dev *idev;
458 read_lock(&dev_base_lock);
459 for_each_netdev(dev) {
460 rcu_read_lock();
461 idev = __in6_dev_get(dev);
462 if (idev) {
463 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
464 idev->cnf.forwarding = ipv6_devconf.forwarding;
465 if (changed)
466 dev_forward_change(idev);
468 rcu_read_unlock();
470 read_unlock(&dev_base_lock);
472 #endif
474 /* Nobody refers to this ifaddr, destroy it */
476 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
478 BUG_TRAP(ifp->if_next==NULL);
479 BUG_TRAP(ifp->lst_next==NULL);
480 #ifdef NET_REFCNT_DEBUG
481 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
482 #endif
484 in6_dev_put(ifp->idev);
486 if (del_timer(&ifp->timer))
487 printk("Timer is still running, when freeing ifa=%p\n", ifp);
489 if (!ifp->dead) {
490 printk("Freeing alive inet6 address %p\n", ifp);
491 return;
493 dst_release(&ifp->rt->u.dst);
495 kfree(ifp);
498 static void
499 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
501 struct inet6_ifaddr *ifa, **ifap;
502 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
505 * Each device address list is sorted in order of scope -
506 * global before linklocal.
508 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
509 ifap = &ifa->if_next) {
510 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
511 break;
514 ifp->if_next = *ifap;
515 *ifap = ifp;
518 /* On success it returns ifp with increased reference count */
520 static struct inet6_ifaddr *
521 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
522 int scope, u32 flags)
524 struct inet6_ifaddr *ifa = NULL;
525 struct rt6_info *rt;
526 int hash;
527 int err = 0;
529 rcu_read_lock_bh();
530 if (idev->dead) {
531 err = -ENODEV; /*XXX*/
532 goto out2;
535 write_lock(&addrconf_hash_lock);
537 /* Ignore adding duplicate addresses on an interface */
538 if (ipv6_chk_same_addr(addr, idev->dev)) {
539 ADBG(("ipv6_add_addr: already assigned\n"));
540 err = -EEXIST;
541 goto out;
544 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
546 if (ifa == NULL) {
547 ADBG(("ipv6_add_addr: malloc failed\n"));
548 err = -ENOBUFS;
549 goto out;
552 rt = addrconf_dst_alloc(idev, addr, 0);
553 if (IS_ERR(rt)) {
554 err = PTR_ERR(rt);
555 goto out;
558 ipv6_addr_copy(&ifa->addr, addr);
560 spin_lock_init(&ifa->lock);
561 init_timer(&ifa->timer);
562 ifa->timer.data = (unsigned long) ifa;
563 ifa->scope = scope;
564 ifa->prefix_len = pfxlen;
565 ifa->flags = flags | IFA_F_TENTATIVE;
566 ifa->cstamp = ifa->tstamp = jiffies;
568 ifa->rt = rt;
571 * part one of RFC 4429, section 3.3
572 * We should not configure an address as
573 * optimistic if we do not yet know the link
574 * layer address of our nexhop router
577 if (rt->rt6i_nexthop == NULL)
578 ifa->flags &= ~IFA_F_OPTIMISTIC;
580 ifa->idev = idev;
581 in6_dev_hold(idev);
582 /* For caller */
583 in6_ifa_hold(ifa);
585 /* Add to big hash table */
586 hash = ipv6_addr_hash(addr);
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);
593 write_lock(&idev->lock);
594 /* Add to inet6_dev unicast addr list. */
595 ipv6_link_dev_addr(idev, ifa);
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);
603 #endif
605 in6_ifa_hold(ifa);
606 write_unlock(&idev->lock);
607 out2:
608 rcu_read_unlock_bh();
610 if (likely(err == 0))
611 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
612 else {
613 kfree(ifa);
614 ifa = ERR_PTR(err);
617 return ifa;
618 out:
619 write_unlock(&addrconf_hash_lock);
620 goto out2;
623 /* This function wants to get referenced ifp and releases it before return */
625 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
627 struct inet6_ifaddr *ifa, **ifap;
628 struct inet6_dev *idev = ifp->idev;
629 int hash;
630 int deleted = 0, onlink = 0;
631 unsigned long expires = jiffies;
633 hash = ipv6_addr_hash(&ifp->addr);
635 ifp->dead = 1;
637 write_lock_bh(&addrconf_hash_lock);
638 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
639 ifap = &ifa->lst_next) {
640 if (ifa == ifp) {
641 *ifap = ifa->lst_next;
642 __in6_ifa_put(ifp);
643 ifa->lst_next = NULL;
644 break;
647 write_unlock_bh(&addrconf_hash_lock);
649 write_lock_bh(&idev->lock);
650 #ifdef CONFIG_IPV6_PRIVACY
651 if (ifp->flags&IFA_F_TEMPORARY) {
652 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
653 ifap = &ifa->tmp_next) {
654 if (ifa == ifp) {
655 *ifap = ifa->tmp_next;
656 if (ifp->ifpub) {
657 in6_ifa_put(ifp->ifpub);
658 ifp->ifpub = NULL;
660 __in6_ifa_put(ifp);
661 ifa->tmp_next = NULL;
662 break;
666 #endif
668 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
669 if (ifa == ifp) {
670 *ifap = ifa->if_next;
671 __in6_ifa_put(ifp);
672 ifa->if_next = NULL;
673 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
674 break;
675 deleted = 1;
676 continue;
677 } else if (ifp->flags & IFA_F_PERMANENT) {
678 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
679 ifp->prefix_len)) {
680 if (ifa->flags & IFA_F_PERMANENT) {
681 onlink = 1;
682 if (deleted)
683 break;
684 } else {
685 unsigned long lifetime;
687 if (!onlink)
688 onlink = -1;
690 spin_lock(&ifa->lock);
691 lifetime = min_t(unsigned long,
692 ifa->valid_lft, 0x7fffffffUL/HZ);
693 if (time_before(expires,
694 ifa->tstamp + lifetime * HZ))
695 expires = ifa->tstamp + lifetime * HZ;
696 spin_unlock(&ifa->lock);
700 ifap = &ifa->if_next;
702 write_unlock_bh(&idev->lock);
704 ipv6_ifa_notify(RTM_DELADDR, ifp);
706 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
708 addrconf_del_timer(ifp);
711 * Purge or update corresponding prefix
713 * 1) we don't purge prefix here if address was not permanent.
714 * prefix is managed by its own lifetime.
715 * 2) if there're no addresses, delete prefix.
716 * 3) if there're still other permanent address(es),
717 * corresponding prefix is still permanent.
718 * 4) otherwise, update prefix lifetime to the
719 * longest valid lifetime among the corresponding
720 * addresses on the device.
721 * Note: subsequent RA will update lifetime.
723 * --yoshfuji
725 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
726 struct in6_addr prefix;
727 struct rt6_info *rt;
729 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
730 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
732 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
733 if (onlink == 0) {
734 ip6_del_rt(rt);
735 rt = NULL;
736 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
737 rt->rt6i_expires = expires;
738 rt->rt6i_flags |= RTF_EXPIRES;
741 dst_release(&rt->u.dst);
744 in6_ifa_put(ifp);
747 #ifdef CONFIG_IPV6_PRIVACY
748 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
750 struct inet6_dev *idev = ifp->idev;
751 struct in6_addr addr, *tmpaddr;
752 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
753 int tmp_plen;
754 int ret = 0;
755 int max_addresses;
756 u32 addr_flags;
758 write_lock(&idev->lock);
759 if (ift) {
760 spin_lock_bh(&ift->lock);
761 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
762 spin_unlock_bh(&ift->lock);
763 tmpaddr = &addr;
764 } else {
765 tmpaddr = NULL;
767 retry:
768 in6_dev_hold(idev);
769 if (idev->cnf.use_tempaddr <= 0) {
770 write_unlock(&idev->lock);
771 printk(KERN_INFO
772 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
773 in6_dev_put(idev);
774 ret = -1;
775 goto out;
777 spin_lock_bh(&ifp->lock);
778 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
779 idev->cnf.use_tempaddr = -1; /*XXX*/
780 spin_unlock_bh(&ifp->lock);
781 write_unlock(&idev->lock);
782 printk(KERN_WARNING
783 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
784 in6_dev_put(idev);
785 ret = -1;
786 goto out;
788 in6_ifa_hold(ifp);
789 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
790 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
791 spin_unlock_bh(&ifp->lock);
792 write_unlock(&idev->lock);
793 printk(KERN_WARNING
794 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
795 in6_ifa_put(ifp);
796 in6_dev_put(idev);
797 ret = -1;
798 goto out;
800 memcpy(&addr.s6_addr[8], idev->rndid, 8);
801 tmp_valid_lft = min_t(__u32,
802 ifp->valid_lft,
803 idev->cnf.temp_valid_lft);
804 tmp_prefered_lft = min_t(__u32,
805 ifp->prefered_lft,
806 idev->cnf.temp_prefered_lft - desync_factor / HZ);
807 tmp_plen = ifp->prefix_len;
808 max_addresses = idev->cnf.max_addresses;
809 tmp_cstamp = ifp->cstamp;
810 tmp_tstamp = ifp->tstamp;
811 spin_unlock_bh(&ifp->lock);
813 write_unlock(&idev->lock);
815 addr_flags = IFA_F_TEMPORARY;
816 /* set in addrconf_prefix_rcv() */
817 if (ifp->flags & IFA_F_OPTIMISTIC)
818 addr_flags |= IFA_F_OPTIMISTIC;
820 ift = !max_addresses ||
821 ipv6_count_addresses(idev) < max_addresses ?
822 ipv6_add_addr(idev, &addr, tmp_plen,
823 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
824 addr_flags) : NULL;
825 if (!ift || IS_ERR(ift)) {
826 in6_ifa_put(ifp);
827 in6_dev_put(idev);
828 printk(KERN_INFO
829 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
830 tmpaddr = &addr;
831 write_lock(&idev->lock);
832 goto retry;
835 spin_lock_bh(&ift->lock);
836 ift->ifpub = ifp;
837 ift->valid_lft = tmp_valid_lft;
838 ift->prefered_lft = tmp_prefered_lft;
839 ift->cstamp = tmp_cstamp;
840 ift->tstamp = tmp_tstamp;
841 spin_unlock_bh(&ift->lock);
843 addrconf_dad_start(ift, 0);
844 in6_ifa_put(ift);
845 in6_dev_put(idev);
846 out:
847 return ret;
849 #endif
852 * Choose an appropriate source address (RFC3484)
854 struct ipv6_saddr_score {
855 int addr_type;
856 unsigned int attrs;
857 int matchlen;
858 int scope;
859 unsigned int rule;
862 #define IPV6_SADDR_SCORE_LOCAL 0x0001
863 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
864 #define IPV6_SADDR_SCORE_HOA 0x0008
865 #define IPV6_SADDR_SCORE_OIF 0x0010
866 #define IPV6_SADDR_SCORE_LABEL 0x0020
867 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
869 static inline int ipv6_saddr_preferred(int type)
871 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
872 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
873 return 1;
874 return 0;
877 /* static matching label */
878 static inline int ipv6_saddr_label(const struct in6_addr *addr, int type)
881 * prefix (longest match) label
882 * -----------------------------
883 * ::1/128 0
884 * ::/0 1
885 * 2002::/16 2
886 * ::/96 3
887 * ::ffff:0:0/96 4
888 * fc00::/7 5
889 * 2001::/32 6
891 if (type & IPV6_ADDR_LOOPBACK)
892 return 0;
893 else if (type & IPV6_ADDR_COMPATv4)
894 return 3;
895 else if (type & IPV6_ADDR_MAPPED)
896 return 4;
897 else if (addr->s6_addr32[0] == htonl(0x20010000))
898 return 6;
899 else if (addr->s6_addr16[0] == htons(0x2002))
900 return 2;
901 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
902 return 5;
903 return 1;
906 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
907 struct in6_addr *daddr, struct in6_addr *saddr)
909 struct ipv6_saddr_score hiscore;
910 struct inet6_ifaddr *ifa_result = NULL;
911 int daddr_type = __ipv6_addr_type(daddr);
912 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
913 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
914 struct net_device *dev;
916 memset(&hiscore, 0, sizeof(hiscore));
918 read_lock(&dev_base_lock);
919 rcu_read_lock();
921 for_each_netdev(dev) {
922 struct inet6_dev *idev;
923 struct inet6_ifaddr *ifa;
925 /* Rule 0: Candidate Source Address (section 4)
926 * - multicast and link-local destination address,
927 * the set of candidate source address MUST only
928 * include addresses assigned to interfaces
929 * belonging to the same link as the outgoing
930 * interface.
931 * (- For site-local destination addresses, the
932 * set of candidate source addresses MUST only
933 * include addresses assigned to interfaces
934 * belonging to the same site as the outgoing
935 * interface.)
937 if ((daddr_type & IPV6_ADDR_MULTICAST ||
938 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
939 daddr_dev && dev != daddr_dev)
940 continue;
942 idev = __in6_dev_get(dev);
943 if (!idev)
944 continue;
946 read_lock_bh(&idev->lock);
947 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
948 struct ipv6_saddr_score score;
950 score.addr_type = __ipv6_addr_type(&ifa->addr);
952 /* Rule 0:
953 * - Tentative Address (RFC2462 section 5.4)
954 * - A tentative address is not considered
955 * "assigned to an interface" in the traditional
956 * sense, unless it is also flagged as optimistic.
957 * - Candidate Source Address (section 4)
958 * - In any case, anycast addresses, multicast
959 * addresses, and the unspecified address MUST
960 * NOT be included in a candidate set.
962 if ((ifa->flags & IFA_F_TENTATIVE) &&
963 (!(ifa->flags & IFA_F_OPTIMISTIC)))
964 continue;
965 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
966 score.addr_type & IPV6_ADDR_MULTICAST)) {
967 LIMIT_NETDEBUG(KERN_DEBUG
968 "ADDRCONF: unspecified / multicast address"
969 "assigned as unicast address on %s",
970 dev->name);
971 continue;
974 score.attrs = 0;
975 score.matchlen = 0;
976 score.scope = 0;
977 score.rule = 0;
979 if (ifa_result == NULL) {
980 /* record it if the first available entry */
981 goto record_it;
984 /* Rule 1: Prefer same address */
985 if (hiscore.rule < 1) {
986 if (ipv6_addr_equal(&ifa_result->addr, daddr))
987 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
988 hiscore.rule++;
990 if (ipv6_addr_equal(&ifa->addr, daddr)) {
991 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
992 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
993 score.rule = 1;
994 goto record_it;
996 } else {
997 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
998 continue;
1001 /* Rule 2: Prefer appropriate scope */
1002 if (hiscore.rule < 2) {
1003 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
1004 hiscore.rule++;
1006 score.scope = __ipv6_addr_src_scope(score.addr_type);
1007 if (hiscore.scope < score.scope) {
1008 if (hiscore.scope < daddr_scope) {
1009 score.rule = 2;
1010 goto record_it;
1011 } else
1012 continue;
1013 } else if (score.scope < hiscore.scope) {
1014 if (score.scope < daddr_scope)
1015 break; /* addresses sorted by scope */
1016 else {
1017 score.rule = 2;
1018 goto record_it;
1022 /* Rule 3: Avoid deprecated and optimistic addresses */
1023 if (hiscore.rule < 3) {
1024 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1025 (((ifa_result->flags &
1026 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
1027 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1028 hiscore.rule++;
1030 if (ipv6_saddr_preferred(score.addr_type) ||
1031 (((ifa->flags &
1032 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
1033 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1034 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1035 score.rule = 3;
1036 goto record_it;
1038 } else {
1039 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1040 continue;
1043 /* Rule 4: Prefer home address */
1044 #ifdef CONFIG_IPV6_MIP6
1045 if (hiscore.rule < 4) {
1046 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1047 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1048 hiscore.rule++;
1050 if (ifa->flags & IFA_F_HOMEADDRESS) {
1051 score.attrs |= IPV6_SADDR_SCORE_HOA;
1052 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1053 score.rule = 4;
1054 goto record_it;
1056 } else {
1057 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1058 continue;
1060 #else
1061 if (hiscore.rule < 4)
1062 hiscore.rule++;
1063 #endif
1065 /* Rule 5: Prefer outgoing interface */
1066 if (hiscore.rule < 5) {
1067 if (daddr_dev == NULL ||
1068 daddr_dev == ifa_result->idev->dev)
1069 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1070 hiscore.rule++;
1072 if (daddr_dev == NULL ||
1073 daddr_dev == ifa->idev->dev) {
1074 score.attrs |= IPV6_SADDR_SCORE_OIF;
1075 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1076 score.rule = 5;
1077 goto record_it;
1079 } else {
1080 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1081 continue;
1084 /* Rule 6: Prefer matching label */
1085 if (hiscore.rule < 6) {
1086 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1087 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1088 hiscore.rule++;
1090 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1091 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1092 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1093 score.rule = 6;
1094 goto record_it;
1096 } else {
1097 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1098 continue;
1101 #ifdef CONFIG_IPV6_PRIVACY
1102 /* Rule 7: Prefer public address
1103 * Note: prefer temprary address if use_tempaddr >= 2
1105 if (hiscore.rule < 7) {
1106 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1107 (ifa_result->idev->cnf.use_tempaddr >= 2))
1108 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1109 hiscore.rule++;
1111 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1112 (ifa->idev->cnf.use_tempaddr >= 2)) {
1113 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1114 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1115 score.rule = 7;
1116 goto record_it;
1118 } else {
1119 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1120 continue;
1122 #else
1123 if (hiscore.rule < 7)
1124 hiscore.rule++;
1125 #endif
1126 /* Rule 8: Use longest matching prefix */
1127 if (hiscore.rule < 8) {
1128 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1129 hiscore.rule++;
1131 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1132 if (score.matchlen > hiscore.matchlen) {
1133 score.rule = 8;
1134 goto record_it;
1136 #if 0
1137 else if (score.matchlen < hiscore.matchlen)
1138 continue;
1139 #endif
1141 /* Final Rule: choose first available one */
1142 continue;
1143 record_it:
1144 if (ifa_result)
1145 in6_ifa_put(ifa_result);
1146 in6_ifa_hold(ifa);
1147 ifa_result = ifa;
1148 hiscore = score;
1150 read_unlock_bh(&idev->lock);
1152 rcu_read_unlock();
1153 read_unlock(&dev_base_lock);
1155 if (!ifa_result)
1156 return -EADDRNOTAVAIL;
1158 ipv6_addr_copy(saddr, &ifa_result->addr);
1159 in6_ifa_put(ifa_result);
1160 return 0;
1164 int ipv6_get_saddr(struct dst_entry *dst,
1165 struct in6_addr *daddr, struct in6_addr *saddr)
1167 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
1170 EXPORT_SYMBOL(ipv6_get_saddr);
1172 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1173 unsigned char banned_flags)
1175 struct inet6_dev *idev;
1176 int err = -EADDRNOTAVAIL;
1178 rcu_read_lock();
1179 if ((idev = __in6_dev_get(dev)) != NULL) {
1180 struct inet6_ifaddr *ifp;
1182 read_lock_bh(&idev->lock);
1183 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1184 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
1185 ipv6_addr_copy(addr, &ifp->addr);
1186 err = 0;
1187 break;
1190 read_unlock_bh(&idev->lock);
1192 rcu_read_unlock();
1193 return err;
1196 static int ipv6_count_addresses(struct inet6_dev *idev)
1198 int cnt = 0;
1199 struct inet6_ifaddr *ifp;
1201 read_lock_bh(&idev->lock);
1202 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1203 cnt++;
1204 read_unlock_bh(&idev->lock);
1205 return cnt;
1208 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1210 struct inet6_ifaddr * ifp;
1211 u8 hash = ipv6_addr_hash(addr);
1213 read_lock_bh(&addrconf_hash_lock);
1214 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1215 if (ipv6_addr_equal(&ifp->addr, addr) &&
1216 !(ifp->flags&IFA_F_TENTATIVE)) {
1217 if (dev == NULL || ifp->idev->dev == dev ||
1218 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1219 break;
1222 read_unlock_bh(&addrconf_hash_lock);
1223 return ifp != NULL;
1226 EXPORT_SYMBOL(ipv6_chk_addr);
1228 static
1229 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1231 struct inet6_ifaddr * ifp;
1232 u8 hash = ipv6_addr_hash(addr);
1234 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1235 if (ipv6_addr_equal(&ifp->addr, addr)) {
1236 if (dev == NULL || ifp->idev->dev == dev)
1237 break;
1240 return ifp != NULL;
1243 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1245 struct inet6_ifaddr * ifp;
1246 u8 hash = ipv6_addr_hash(addr);
1248 read_lock_bh(&addrconf_hash_lock);
1249 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1250 if (ipv6_addr_equal(&ifp->addr, addr)) {
1251 if (dev == NULL || ifp->idev->dev == dev ||
1252 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1253 in6_ifa_hold(ifp);
1254 break;
1258 read_unlock_bh(&addrconf_hash_lock);
1260 return ifp;
1263 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1265 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1266 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1267 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1268 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1269 int sk_ipv6only = ipv6_only_sock(sk);
1270 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1271 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1272 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1274 if (!sk2_rcv_saddr && !sk_ipv6only)
1275 return 1;
1277 if (addr_type2 == IPV6_ADDR_ANY &&
1278 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1279 return 1;
1281 if (addr_type == IPV6_ADDR_ANY &&
1282 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1283 return 1;
1285 if (sk2_rcv_saddr6 &&
1286 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1287 return 1;
1289 if (addr_type == IPV6_ADDR_MAPPED &&
1290 !sk2_ipv6only &&
1291 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1292 return 1;
1294 return 0;
1297 /* Gets referenced address, destroys ifaddr */
1299 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1301 if (ifp->flags&IFA_F_PERMANENT) {
1302 spin_lock_bh(&ifp->lock);
1303 addrconf_del_timer(ifp);
1304 ifp->flags |= IFA_F_TENTATIVE;
1305 spin_unlock_bh(&ifp->lock);
1306 in6_ifa_put(ifp);
1307 #ifdef CONFIG_IPV6_PRIVACY
1308 } else if (ifp->flags&IFA_F_TEMPORARY) {
1309 struct inet6_ifaddr *ifpub;
1310 spin_lock_bh(&ifp->lock);
1311 ifpub = ifp->ifpub;
1312 if (ifpub) {
1313 in6_ifa_hold(ifpub);
1314 spin_unlock_bh(&ifp->lock);
1315 ipv6_create_tempaddr(ifpub, ifp);
1316 in6_ifa_put(ifpub);
1317 } else {
1318 spin_unlock_bh(&ifp->lock);
1320 ipv6_del_addr(ifp);
1321 #endif
1322 } else
1323 ipv6_del_addr(ifp);
1326 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1328 if (net_ratelimit())
1329 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1330 addrconf_dad_stop(ifp);
1333 /* Join to solicited addr multicast group. */
1335 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1337 struct in6_addr maddr;
1339 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1340 return;
1342 addrconf_addr_solict_mult(addr, &maddr);
1343 ipv6_dev_mc_inc(dev, &maddr);
1346 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1348 struct in6_addr maddr;
1350 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1351 return;
1353 addrconf_addr_solict_mult(addr, &maddr);
1354 __ipv6_dev_mc_dec(idev, &maddr);
1357 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1359 struct in6_addr addr;
1360 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1361 if (ipv6_addr_any(&addr))
1362 return;
1363 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1366 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1368 struct in6_addr addr;
1369 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1370 if (ipv6_addr_any(&addr))
1371 return;
1372 __ipv6_dev_ac_dec(ifp->idev, &addr);
1375 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1377 if (dev->addr_len != ETH_ALEN)
1378 return -1;
1379 memcpy(eui, dev->dev_addr, 3);
1380 memcpy(eui + 5, dev->dev_addr + 3, 3);
1383 * The zSeries OSA network cards can be shared among various
1384 * OS instances, but the OSA cards have only one MAC address.
1385 * This leads to duplicate address conflicts in conjunction
1386 * with IPv6 if more than one instance uses the same card.
1388 * The driver for these cards can deliver a unique 16-bit
1389 * identifier for each instance sharing the same card. It is
1390 * placed instead of 0xFFFE in the interface identifier. The
1391 * "u" bit of the interface identifier is not inverted in this
1392 * case. Hence the resulting interface identifier has local
1393 * scope according to RFC2373.
1395 if (dev->dev_id) {
1396 eui[3] = (dev->dev_id >> 8) & 0xFF;
1397 eui[4] = dev->dev_id & 0xFF;
1398 } else {
1399 eui[3] = 0xFF;
1400 eui[4] = 0xFE;
1401 eui[0] ^= 2;
1403 return 0;
1406 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1408 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1409 if (dev->addr_len != ARCNET_ALEN)
1410 return -1;
1411 memset(eui, 0, 7);
1412 eui[7] = *(u8*)dev->dev_addr;
1413 return 0;
1416 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1418 if (dev->addr_len != INFINIBAND_ALEN)
1419 return -1;
1420 memcpy(eui, dev->dev_addr + 12, 8);
1421 eui[0] |= 2;
1422 return 0;
1425 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1427 switch (dev->type) {
1428 case ARPHRD_ETHER:
1429 case ARPHRD_FDDI:
1430 case ARPHRD_IEEE802_TR:
1431 return addrconf_ifid_eui48(eui, dev);
1432 case ARPHRD_ARCNET:
1433 return addrconf_ifid_arcnet(eui, dev);
1434 case ARPHRD_INFINIBAND:
1435 return addrconf_ifid_infiniband(eui, dev);
1437 return -1;
1440 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1442 int err = -1;
1443 struct inet6_ifaddr *ifp;
1445 read_lock_bh(&idev->lock);
1446 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1447 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1448 memcpy(eui, ifp->addr.s6_addr+8, 8);
1449 err = 0;
1450 break;
1453 read_unlock_bh(&idev->lock);
1454 return err;
1457 #ifdef CONFIG_IPV6_PRIVACY
1458 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1459 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1461 regen:
1462 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1463 idev->rndid[0] &= ~0x02;
1466 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1467 * check if generated address is not inappropriate
1469 * - Reserved subnet anycast (RFC 2526)
1470 * 11111101 11....11 1xxxxxxx
1471 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1472 * 00-00-5E-FE-xx-xx-xx-xx
1473 * - value 0
1474 * - XXX: already assigned to an address on the device
1476 if (idev->rndid[0] == 0xfd &&
1477 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1478 (idev->rndid[7]&0x80))
1479 goto regen;
1480 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1481 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1482 goto regen;
1483 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1484 goto regen;
1487 return 0;
1490 static void ipv6_regen_rndid(unsigned long data)
1492 struct inet6_dev *idev = (struct inet6_dev *) data;
1493 unsigned long expires;
1495 rcu_read_lock_bh();
1496 write_lock_bh(&idev->lock);
1498 if (idev->dead)
1499 goto out;
1501 if (__ipv6_regen_rndid(idev) < 0)
1502 goto out;
1504 expires = jiffies +
1505 idev->cnf.temp_prefered_lft * HZ -
1506 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1507 if (time_before(expires, jiffies)) {
1508 printk(KERN_WARNING
1509 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1510 idev->dev->name);
1511 goto out;
1514 if (!mod_timer(&idev->regen_timer, expires))
1515 in6_dev_hold(idev);
1517 out:
1518 write_unlock_bh(&idev->lock);
1519 rcu_read_unlock_bh();
1520 in6_dev_put(idev);
1523 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1524 int ret = 0;
1526 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1527 ret = __ipv6_regen_rndid(idev);
1528 return ret;
1530 #endif
1533 * Add prefix route.
1536 static void
1537 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1538 unsigned long expires, u32 flags)
1540 struct fib6_config cfg = {
1541 .fc_table = RT6_TABLE_PREFIX,
1542 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1543 .fc_ifindex = dev->ifindex,
1544 .fc_expires = expires,
1545 .fc_dst_len = plen,
1546 .fc_flags = RTF_UP | flags,
1549 ipv6_addr_copy(&cfg.fc_dst, pfx);
1551 /* Prevent useless cloning on PtP SIT.
1552 This thing is done here expecting that the whole
1553 class of non-broadcast devices need not cloning.
1555 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1556 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1557 cfg.fc_flags |= RTF_NONEXTHOP;
1558 #endif
1560 ip6_route_add(&cfg);
1563 /* Create "default" multicast route to the interface */
1565 static void addrconf_add_mroute(struct net_device *dev)
1567 struct fib6_config cfg = {
1568 .fc_table = RT6_TABLE_LOCAL,
1569 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1570 .fc_ifindex = dev->ifindex,
1571 .fc_dst_len = 8,
1572 .fc_flags = RTF_UP,
1575 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1577 ip6_route_add(&cfg);
1580 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1581 static void sit_route_add(struct net_device *dev)
1583 struct fib6_config cfg = {
1584 .fc_table = RT6_TABLE_MAIN,
1585 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1586 .fc_ifindex = dev->ifindex,
1587 .fc_dst_len = 96,
1588 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1591 /* prefix length - 96 bits "::d.d.d.d" */
1592 ip6_route_add(&cfg);
1594 #endif
1596 static void addrconf_add_lroute(struct net_device *dev)
1598 struct in6_addr addr;
1600 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1601 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1604 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1606 struct inet6_dev *idev;
1608 ASSERT_RTNL();
1610 if ((idev = ipv6_find_idev(dev)) == NULL)
1611 return NULL;
1613 /* Add default multicast route */
1614 addrconf_add_mroute(dev);
1616 /* Add link local route */
1617 addrconf_add_lroute(dev);
1618 return idev;
1621 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1623 struct prefix_info *pinfo;
1624 __u32 valid_lft;
1625 __u32 prefered_lft;
1626 int addr_type;
1627 unsigned long rt_expires;
1628 struct inet6_dev *in6_dev;
1630 pinfo = (struct prefix_info *) opt;
1632 if (len < sizeof(struct prefix_info)) {
1633 ADBG(("addrconf: prefix option too short\n"));
1634 return;
1638 * Validation checks ([ADDRCONF], page 19)
1641 addr_type = ipv6_addr_type(&pinfo->prefix);
1643 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1644 return;
1646 valid_lft = ntohl(pinfo->valid);
1647 prefered_lft = ntohl(pinfo->prefered);
1649 if (prefered_lft > valid_lft) {
1650 if (net_ratelimit())
1651 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1652 return;
1655 in6_dev = in6_dev_get(dev);
1657 if (in6_dev == NULL) {
1658 if (net_ratelimit())
1659 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1660 return;
1664 * Two things going on here:
1665 * 1) Add routes for on-link prefixes
1666 * 2) Configure prefixes with the auto flag set
1669 /* Avoid arithmetic overflow. Really, we could
1670 save rt_expires in seconds, likely valid_lft,
1671 but it would require division in fib gc, that it
1672 not good.
1674 if (valid_lft >= 0x7FFFFFFF/HZ)
1675 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1676 else
1677 rt_expires = valid_lft * HZ;
1680 * We convert this (in jiffies) to clock_t later.
1681 * Avoid arithmetic overflow there as well.
1682 * Overflow can happen only if HZ < USER_HZ.
1684 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1685 rt_expires = 0x7FFFFFFF / USER_HZ;
1687 if (pinfo->onlink) {
1688 struct rt6_info *rt;
1689 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1691 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1692 if (rt->rt6i_flags&RTF_EXPIRES) {
1693 if (valid_lft == 0) {
1694 ip6_del_rt(rt);
1695 rt = NULL;
1696 } else {
1697 rt->rt6i_expires = jiffies + rt_expires;
1700 } else if (valid_lft) {
1701 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1702 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1704 if (rt)
1705 dst_release(&rt->u.dst);
1708 /* Try to figure out our local address for this prefix */
1710 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1711 struct inet6_ifaddr * ifp;
1712 struct in6_addr addr;
1713 int create = 0, update_lft = 0;
1715 if (pinfo->prefix_len == 64) {
1716 memcpy(&addr, &pinfo->prefix, 8);
1717 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1718 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1719 in6_dev_put(in6_dev);
1720 return;
1722 goto ok;
1724 if (net_ratelimit())
1725 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1726 pinfo->prefix_len);
1727 in6_dev_put(in6_dev);
1728 return;
1732 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1734 if (ifp == NULL && valid_lft) {
1735 int max_addresses = in6_dev->cnf.max_addresses;
1736 u32 addr_flags = 0;
1738 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1739 if (in6_dev->cnf.optimistic_dad &&
1740 !ipv6_devconf.forwarding)
1741 addr_flags = IFA_F_OPTIMISTIC;
1742 #endif
1744 /* Do not allow to create too much of autoconfigured
1745 * addresses; this would be too easy way to crash kernel.
1747 if (!max_addresses ||
1748 ipv6_count_addresses(in6_dev) < max_addresses)
1749 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1750 addr_type&IPV6_ADDR_SCOPE_MASK,
1751 addr_flags);
1753 if (!ifp || IS_ERR(ifp)) {
1754 in6_dev_put(in6_dev);
1755 return;
1758 update_lft = create = 1;
1759 ifp->cstamp = jiffies;
1760 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1763 if (ifp) {
1764 int flags;
1765 unsigned long now;
1766 #ifdef CONFIG_IPV6_PRIVACY
1767 struct inet6_ifaddr *ift;
1768 #endif
1769 u32 stored_lft;
1771 /* update lifetime (RFC2462 5.5.3 e) */
1772 spin_lock(&ifp->lock);
1773 now = jiffies;
1774 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1775 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1776 else
1777 stored_lft = 0;
1778 if (!update_lft && stored_lft) {
1779 if (valid_lft > MIN_VALID_LIFETIME ||
1780 valid_lft > stored_lft)
1781 update_lft = 1;
1782 else if (stored_lft <= MIN_VALID_LIFETIME) {
1783 /* valid_lft <= stored_lft is always true */
1784 /* XXX: IPsec */
1785 update_lft = 0;
1786 } else {
1787 valid_lft = MIN_VALID_LIFETIME;
1788 if (valid_lft < prefered_lft)
1789 prefered_lft = valid_lft;
1790 update_lft = 1;
1794 if (update_lft) {
1795 ifp->valid_lft = valid_lft;
1796 ifp->prefered_lft = prefered_lft;
1797 ifp->tstamp = now;
1798 flags = ifp->flags;
1799 ifp->flags &= ~IFA_F_DEPRECATED;
1800 spin_unlock(&ifp->lock);
1802 if (!(flags&IFA_F_TENTATIVE))
1803 ipv6_ifa_notify(0, ifp);
1804 } else
1805 spin_unlock(&ifp->lock);
1807 #ifdef CONFIG_IPV6_PRIVACY
1808 read_lock_bh(&in6_dev->lock);
1809 /* update all temporary addresses in the list */
1810 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1812 * When adjusting the lifetimes of an existing
1813 * temporary address, only lower the lifetimes.
1814 * Implementations must not increase the
1815 * lifetimes of an existing temporary address
1816 * when processing a Prefix Information Option.
1818 spin_lock(&ift->lock);
1819 flags = ift->flags;
1820 if (ift->valid_lft > valid_lft &&
1821 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1822 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1823 if (ift->prefered_lft > prefered_lft &&
1824 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1825 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1826 spin_unlock(&ift->lock);
1827 if (!(flags&IFA_F_TENTATIVE))
1828 ipv6_ifa_notify(0, ift);
1831 if (create && in6_dev->cnf.use_tempaddr > 0) {
1833 * When a new public address is created as described in [ADDRCONF],
1834 * also create a new temporary address.
1836 read_unlock_bh(&in6_dev->lock);
1837 ipv6_create_tempaddr(ifp, NULL);
1838 } else {
1839 read_unlock_bh(&in6_dev->lock);
1841 #endif
1842 in6_ifa_put(ifp);
1843 addrconf_verify(0);
1846 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1847 in6_dev_put(in6_dev);
1851 * Set destination address.
1852 * Special case for SIT interfaces where we create a new "virtual"
1853 * device.
1855 int addrconf_set_dstaddr(void __user *arg)
1857 struct in6_ifreq ireq;
1858 struct net_device *dev;
1859 int err = -EINVAL;
1861 rtnl_lock();
1863 err = -EFAULT;
1864 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1865 goto err_exit;
1867 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1869 err = -ENODEV;
1870 if (dev == NULL)
1871 goto err_exit;
1873 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1874 if (dev->type == ARPHRD_SIT) {
1875 struct ifreq ifr;
1876 mm_segment_t oldfs;
1877 struct ip_tunnel_parm p;
1879 err = -EADDRNOTAVAIL;
1880 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1881 goto err_exit;
1883 memset(&p, 0, sizeof(p));
1884 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1885 p.iph.saddr = 0;
1886 p.iph.version = 4;
1887 p.iph.ihl = 5;
1888 p.iph.protocol = IPPROTO_IPV6;
1889 p.iph.ttl = 64;
1890 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1892 oldfs = get_fs(); set_fs(KERNEL_DS);
1893 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1894 set_fs(oldfs);
1896 if (err == 0) {
1897 err = -ENOBUFS;
1898 if ((dev = __dev_get_by_name(p.name)) == NULL)
1899 goto err_exit;
1900 err = dev_open(dev);
1903 #endif
1905 err_exit:
1906 rtnl_unlock();
1907 return err;
1911 * Manual configuration of address on an interface
1913 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
1914 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
1916 struct inet6_ifaddr *ifp;
1917 struct inet6_dev *idev;
1918 struct net_device *dev;
1919 int scope;
1920 u32 flags = RTF_EXPIRES;
1922 ASSERT_RTNL();
1924 /* check the lifetime */
1925 if (!valid_lft || prefered_lft > valid_lft)
1926 return -EINVAL;
1928 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1929 return -ENODEV;
1931 if ((idev = addrconf_add_dev(dev)) == NULL)
1932 return -ENOBUFS;
1934 scope = ipv6_addr_scope(pfx);
1936 if (valid_lft == INFINITY_LIFE_TIME) {
1937 ifa_flags |= IFA_F_PERMANENT;
1938 flags = 0;
1939 } else if (valid_lft >= 0x7FFFFFFF/HZ)
1940 valid_lft = 0x7FFFFFFF/HZ;
1942 if (prefered_lft == 0)
1943 ifa_flags |= IFA_F_DEPRECATED;
1944 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1945 (prefered_lft != INFINITY_LIFE_TIME))
1946 prefered_lft = 0x7FFFFFFF/HZ;
1948 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1950 if (!IS_ERR(ifp)) {
1951 spin_lock_bh(&ifp->lock);
1952 ifp->valid_lft = valid_lft;
1953 ifp->prefered_lft = prefered_lft;
1954 ifp->tstamp = jiffies;
1955 spin_unlock_bh(&ifp->lock);
1957 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1958 jiffies_to_clock_t(valid_lft * HZ), flags);
1960 * Note that section 3.1 of RFC 4429 indicates
1961 * that the Optimistic flag should not be set for
1962 * manually configured addresses
1964 addrconf_dad_start(ifp, 0);
1965 in6_ifa_put(ifp);
1966 addrconf_verify(0);
1967 return 0;
1970 return PTR_ERR(ifp);
1973 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1975 struct inet6_ifaddr *ifp;
1976 struct inet6_dev *idev;
1977 struct net_device *dev;
1979 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1980 return -ENODEV;
1982 if ((idev = __in6_dev_get(dev)) == NULL)
1983 return -ENXIO;
1985 read_lock_bh(&idev->lock);
1986 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1987 if (ifp->prefix_len == plen &&
1988 ipv6_addr_equal(pfx, &ifp->addr)) {
1989 in6_ifa_hold(ifp);
1990 read_unlock_bh(&idev->lock);
1992 ipv6_del_addr(ifp);
1994 /* If the last address is deleted administratively,
1995 disable IPv6 on this interface.
1997 if (idev->addr_list == NULL)
1998 addrconf_ifdown(idev->dev, 1);
1999 return 0;
2002 read_unlock_bh(&idev->lock);
2003 return -EADDRNOTAVAIL;
2007 int addrconf_add_ifaddr(void __user *arg)
2009 struct in6_ifreq ireq;
2010 int err;
2012 if (!capable(CAP_NET_ADMIN))
2013 return -EPERM;
2015 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2016 return -EFAULT;
2018 rtnl_lock();
2019 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
2020 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2021 rtnl_unlock();
2022 return err;
2025 int addrconf_del_ifaddr(void __user *arg)
2027 struct in6_ifreq ireq;
2028 int err;
2030 if (!capable(CAP_NET_ADMIN))
2031 return -EPERM;
2033 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2034 return -EFAULT;
2036 rtnl_lock();
2037 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2038 rtnl_unlock();
2039 return err;
2042 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2043 static void sit_add_v4_addrs(struct inet6_dev *idev)
2045 struct inet6_ifaddr * ifp;
2046 struct in6_addr addr;
2047 struct net_device *dev;
2048 int scope;
2050 ASSERT_RTNL();
2052 memset(&addr, 0, sizeof(struct in6_addr));
2053 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2055 if (idev->dev->flags&IFF_POINTOPOINT) {
2056 addr.s6_addr32[0] = htonl(0xfe800000);
2057 scope = IFA_LINK;
2058 } else {
2059 scope = IPV6_ADDR_COMPATv4;
2062 if (addr.s6_addr32[3]) {
2063 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2064 if (!IS_ERR(ifp)) {
2065 spin_lock_bh(&ifp->lock);
2066 ifp->flags &= ~IFA_F_TENTATIVE;
2067 spin_unlock_bh(&ifp->lock);
2068 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2069 in6_ifa_put(ifp);
2071 return;
2074 for_each_netdev(dev) {
2075 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2076 if (in_dev && (dev->flags & IFF_UP)) {
2077 struct in_ifaddr * ifa;
2079 int flag = scope;
2081 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2082 int plen;
2084 addr.s6_addr32[3] = ifa->ifa_local;
2086 if (ifa->ifa_scope == RT_SCOPE_LINK)
2087 continue;
2088 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2089 if (idev->dev->flags&IFF_POINTOPOINT)
2090 continue;
2091 flag |= IFA_HOST;
2093 if (idev->dev->flags&IFF_POINTOPOINT)
2094 plen = 64;
2095 else
2096 plen = 96;
2098 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2099 IFA_F_PERMANENT);
2100 if (!IS_ERR(ifp)) {
2101 spin_lock_bh(&ifp->lock);
2102 ifp->flags &= ~IFA_F_TENTATIVE;
2103 spin_unlock_bh(&ifp->lock);
2104 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2105 in6_ifa_put(ifp);
2111 #endif
2113 static void init_loopback(struct net_device *dev)
2115 struct inet6_dev *idev;
2116 struct inet6_ifaddr * ifp;
2118 /* ::1 */
2120 ASSERT_RTNL();
2122 if ((idev = ipv6_find_idev(dev)) == NULL) {
2123 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2124 return;
2127 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2128 if (!IS_ERR(ifp)) {
2129 spin_lock_bh(&ifp->lock);
2130 ifp->flags &= ~IFA_F_TENTATIVE;
2131 spin_unlock_bh(&ifp->lock);
2132 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2133 in6_ifa_put(ifp);
2137 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2139 struct inet6_ifaddr * ifp;
2140 u32 addr_flags = IFA_F_PERMANENT;
2142 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2143 if (idev->cnf.optimistic_dad &&
2144 !ipv6_devconf.forwarding)
2145 addr_flags |= IFA_F_OPTIMISTIC;
2146 #endif
2149 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2150 if (!IS_ERR(ifp)) {
2151 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2152 addrconf_dad_start(ifp, 0);
2153 in6_ifa_put(ifp);
2157 static void addrconf_dev_config(struct net_device *dev)
2159 struct in6_addr addr;
2160 struct inet6_dev * idev;
2162 ASSERT_RTNL();
2164 if ((dev->type != ARPHRD_ETHER) &&
2165 (dev->type != ARPHRD_FDDI) &&
2166 (dev->type != ARPHRD_IEEE802_TR) &&
2167 (dev->type != ARPHRD_ARCNET) &&
2168 (dev->type != ARPHRD_INFINIBAND)) {
2169 /* Alas, we support only Ethernet autoconfiguration. */
2170 return;
2173 idev = addrconf_add_dev(dev);
2174 if (idev == NULL)
2175 return;
2177 memset(&addr, 0, sizeof(struct in6_addr));
2178 addr.s6_addr32[0] = htonl(0xFE800000);
2180 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2181 addrconf_add_linklocal(idev, &addr);
2184 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2185 static void addrconf_sit_config(struct net_device *dev)
2187 struct inet6_dev *idev;
2189 ASSERT_RTNL();
2192 * Configure the tunnel with one of our IPv4
2193 * addresses... we should configure all of
2194 * our v4 addrs in the tunnel
2197 if ((idev = ipv6_find_idev(dev)) == NULL) {
2198 printk(KERN_DEBUG "init sit: add_dev failed\n");
2199 return;
2202 sit_add_v4_addrs(idev);
2204 if (dev->flags&IFF_POINTOPOINT) {
2205 addrconf_add_mroute(dev);
2206 addrconf_add_lroute(dev);
2207 } else
2208 sit_route_add(dev);
2210 #endif
2212 static inline int
2213 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2215 struct in6_addr lladdr;
2217 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2218 addrconf_add_linklocal(idev, &lladdr);
2219 return 0;
2221 return -1;
2224 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2226 struct net_device *link_dev;
2228 /* first try to inherit the link-local address from the link device */
2229 if (idev->dev->iflink &&
2230 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2231 if (!ipv6_inherit_linklocal(idev, link_dev))
2232 return;
2234 /* then try to inherit it from any device */
2235 for_each_netdev(link_dev) {
2236 if (!ipv6_inherit_linklocal(idev, link_dev))
2237 return;
2239 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2243 * Autoconfigure tunnel with a link-local address so routing protocols,
2244 * DHCPv6, MLD etc. can be run over the virtual link
2247 static void addrconf_ip6_tnl_config(struct net_device *dev)
2249 struct inet6_dev *idev;
2251 ASSERT_RTNL();
2253 if ((idev = addrconf_add_dev(dev)) == NULL) {
2254 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2255 return;
2257 ip6_tnl_add_linklocal(idev);
2260 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2261 void * data)
2263 struct net_device *dev = (struct net_device *) data;
2264 struct inet6_dev *idev = __in6_dev_get(dev);
2265 int run_pending = 0;
2267 switch(event) {
2268 case NETDEV_REGISTER:
2269 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2270 idev = ipv6_add_dev(dev);
2271 if (!idev)
2272 printk(KERN_WARNING "IPv6: add_dev failed for %s\n",
2273 dev->name);
2275 break;
2276 case NETDEV_UP:
2277 case NETDEV_CHANGE:
2278 if (event == NETDEV_UP) {
2279 if (!addrconf_qdisc_ok(dev)) {
2280 /* device is not ready yet. */
2281 printk(KERN_INFO
2282 "ADDRCONF(NETDEV_UP): %s: "
2283 "link is not ready\n",
2284 dev->name);
2285 break;
2288 if (idev)
2289 idev->if_flags |= IF_READY;
2290 } else {
2291 if (!addrconf_qdisc_ok(dev)) {
2292 /* device is still not ready. */
2293 break;
2296 if (idev) {
2297 if (idev->if_flags & IF_READY) {
2298 /* device is already configured. */
2299 break;
2301 idev->if_flags |= IF_READY;
2304 printk(KERN_INFO
2305 "ADDRCONF(NETDEV_CHANGE): %s: "
2306 "link becomes ready\n",
2307 dev->name);
2309 run_pending = 1;
2312 switch(dev->type) {
2313 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2314 case ARPHRD_SIT:
2315 addrconf_sit_config(dev);
2316 break;
2317 #endif
2318 case ARPHRD_TUNNEL6:
2319 addrconf_ip6_tnl_config(dev);
2320 break;
2321 case ARPHRD_LOOPBACK:
2322 init_loopback(dev);
2323 break;
2325 default:
2326 addrconf_dev_config(dev);
2327 break;
2329 if (idev) {
2330 if (run_pending)
2331 addrconf_dad_run(idev);
2333 /* If the MTU changed during the interface down, when the
2334 interface up, the changed MTU must be reflected in the
2335 idev as well as routers.
2337 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2338 rt6_mtu_change(dev, dev->mtu);
2339 idev->cnf.mtu6 = dev->mtu;
2341 idev->tstamp = jiffies;
2342 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2343 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2344 stop IPv6 on this interface.
2346 if (dev->mtu < IPV6_MIN_MTU)
2347 addrconf_ifdown(dev, event != NETDEV_DOWN);
2349 break;
2351 case NETDEV_CHANGEMTU:
2352 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2353 rt6_mtu_change(dev, dev->mtu);
2354 idev->cnf.mtu6 = dev->mtu;
2355 break;
2358 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2360 case NETDEV_DOWN:
2361 case NETDEV_UNREGISTER:
2363 * Remove all addresses from this interface.
2365 addrconf_ifdown(dev, event != NETDEV_DOWN);
2366 break;
2368 case NETDEV_CHANGENAME:
2369 if (idev) {
2370 snmp6_unregister_dev(idev);
2371 #ifdef CONFIG_SYSCTL
2372 addrconf_sysctl_unregister(&idev->cnf);
2373 neigh_sysctl_unregister(idev->nd_parms);
2374 neigh_sysctl_register(dev, idev->nd_parms,
2375 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2376 &ndisc_ifinfo_sysctl_change,
2377 NULL);
2378 addrconf_sysctl_register(idev, &idev->cnf);
2379 #endif
2380 snmp6_register_dev(idev);
2382 break;
2385 return NOTIFY_OK;
2389 * addrconf module should be notified of a device going up
2391 static struct notifier_block ipv6_dev_notf = {
2392 .notifier_call = addrconf_notify,
2393 .priority = 0
2396 static int addrconf_ifdown(struct net_device *dev, int how)
2398 struct inet6_dev *idev;
2399 struct inet6_ifaddr *ifa, **bifa;
2400 int i;
2402 ASSERT_RTNL();
2404 if (dev == &loopback_dev && how == 1)
2405 how = 0;
2407 rt6_ifdown(dev);
2408 neigh_ifdown(&nd_tbl, dev);
2410 idev = __in6_dev_get(dev);
2411 if (idev == NULL)
2412 return -ENODEV;
2414 /* Step 1: remove reference to ipv6 device from parent device.
2415 Do not dev_put!
2417 if (how == 1) {
2418 idev->dead = 1;
2420 /* protected by rtnl_lock */
2421 rcu_assign_pointer(dev->ip6_ptr, NULL);
2423 /* Step 1.5: remove snmp6 entry */
2424 snmp6_unregister_dev(idev);
2428 /* Step 2: clear hash table */
2429 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2430 bifa = &inet6_addr_lst[i];
2432 write_lock_bh(&addrconf_hash_lock);
2433 while ((ifa = *bifa) != NULL) {
2434 if (ifa->idev == idev) {
2435 *bifa = ifa->lst_next;
2436 ifa->lst_next = NULL;
2437 addrconf_del_timer(ifa);
2438 in6_ifa_put(ifa);
2439 continue;
2441 bifa = &ifa->lst_next;
2443 write_unlock_bh(&addrconf_hash_lock);
2446 write_lock_bh(&idev->lock);
2448 /* Step 3: clear flags for stateless addrconf */
2449 if (how != 1)
2450 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2452 /* Step 4: clear address list */
2453 #ifdef CONFIG_IPV6_PRIVACY
2454 if (how == 1 && del_timer(&idev->regen_timer))
2455 in6_dev_put(idev);
2457 /* clear tempaddr list */
2458 while ((ifa = idev->tempaddr_list) != NULL) {
2459 idev->tempaddr_list = ifa->tmp_next;
2460 ifa->tmp_next = NULL;
2461 ifa->dead = 1;
2462 write_unlock_bh(&idev->lock);
2463 spin_lock_bh(&ifa->lock);
2465 if (ifa->ifpub) {
2466 in6_ifa_put(ifa->ifpub);
2467 ifa->ifpub = NULL;
2469 spin_unlock_bh(&ifa->lock);
2470 in6_ifa_put(ifa);
2471 write_lock_bh(&idev->lock);
2473 #endif
2474 while ((ifa = idev->addr_list) != NULL) {
2475 idev->addr_list = ifa->if_next;
2476 ifa->if_next = NULL;
2477 ifa->dead = 1;
2478 addrconf_del_timer(ifa);
2479 write_unlock_bh(&idev->lock);
2481 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2482 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
2483 in6_ifa_put(ifa);
2485 write_lock_bh(&idev->lock);
2487 write_unlock_bh(&idev->lock);
2489 /* Step 5: Discard multicast list */
2491 if (how == 1)
2492 ipv6_mc_destroy_dev(idev);
2493 else
2494 ipv6_mc_down(idev);
2496 /* Step 5: netlink notification of this interface */
2497 idev->tstamp = jiffies;
2498 inet6_ifinfo_notify(RTM_DELLINK, idev);
2500 /* Shot the device (if unregistered) */
2502 if (how == 1) {
2503 #ifdef CONFIG_SYSCTL
2504 addrconf_sysctl_unregister(&idev->cnf);
2505 neigh_sysctl_unregister(idev->nd_parms);
2506 #endif
2507 neigh_parms_release(&nd_tbl, idev->nd_parms);
2508 neigh_ifdown(&nd_tbl, dev);
2509 in6_dev_put(idev);
2511 return 0;
2514 static void addrconf_rs_timer(unsigned long data)
2516 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2518 if (ifp->idev->cnf.forwarding)
2519 goto out;
2521 if (ifp->idev->if_flags & IF_RA_RCVD) {
2523 * Announcement received after solicitation
2524 * was sent
2526 goto out;
2529 spin_lock(&ifp->lock);
2530 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2531 struct in6_addr all_routers;
2533 /* The wait after the last probe can be shorter */
2534 addrconf_mod_timer(ifp, AC_RS,
2535 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2536 ifp->idev->cnf.rtr_solicit_delay :
2537 ifp->idev->cnf.rtr_solicit_interval);
2538 spin_unlock(&ifp->lock);
2540 ipv6_addr_all_routers(&all_routers);
2542 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2543 } else {
2544 spin_unlock(&ifp->lock);
2546 * Note: we do not support deprecated "all on-link"
2547 * assumption any longer.
2549 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2550 ifp->idev->dev->name);
2553 out:
2554 in6_ifa_put(ifp);
2558 * Duplicate Address Detection
2560 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2562 unsigned long rand_num;
2563 struct inet6_dev *idev = ifp->idev;
2565 if (ifp->flags & IFA_F_OPTIMISTIC)
2566 rand_num = 0;
2567 else
2568 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2570 ifp->probes = idev->cnf.dad_transmits;
2571 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2574 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2576 struct inet6_dev *idev = ifp->idev;
2577 struct net_device *dev = idev->dev;
2579 addrconf_join_solict(dev, &ifp->addr);
2581 net_srandom(ifp->addr.s6_addr32[3]);
2583 read_lock_bh(&idev->lock);
2584 if (ifp->dead)
2585 goto out;
2586 spin_lock_bh(&ifp->lock);
2588 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2589 !(ifp->flags&IFA_F_TENTATIVE) ||
2590 ifp->flags & IFA_F_NODAD) {
2591 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2592 spin_unlock_bh(&ifp->lock);
2593 read_unlock_bh(&idev->lock);
2595 addrconf_dad_completed(ifp);
2596 return;
2599 if (!(idev->if_flags & IF_READY)) {
2600 spin_unlock_bh(&ifp->lock);
2601 read_unlock_bh(&idev->lock);
2603 * If the defice is not ready:
2604 * - keep it tentative if it is a permanent address.
2605 * - otherwise, kill it.
2607 in6_ifa_hold(ifp);
2608 addrconf_dad_stop(ifp);
2609 return;
2613 * Optimistic nodes can start receiving
2614 * Frames right away
2616 if(ifp->flags & IFA_F_OPTIMISTIC)
2617 ip6_ins_rt(ifp->rt);
2619 addrconf_dad_kick(ifp);
2620 spin_unlock_bh(&ifp->lock);
2621 out:
2622 read_unlock_bh(&idev->lock);
2625 static void addrconf_dad_timer(unsigned long data)
2627 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2628 struct inet6_dev *idev = ifp->idev;
2629 struct in6_addr unspec;
2630 struct in6_addr mcaddr;
2632 read_lock_bh(&idev->lock);
2633 if (idev->dead) {
2634 read_unlock_bh(&idev->lock);
2635 goto out;
2637 spin_lock_bh(&ifp->lock);
2638 if (ifp->probes == 0) {
2640 * DAD was successful
2643 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2644 spin_unlock_bh(&ifp->lock);
2645 read_unlock_bh(&idev->lock);
2647 addrconf_dad_completed(ifp);
2649 goto out;
2652 ifp->probes--;
2653 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2654 spin_unlock_bh(&ifp->lock);
2655 read_unlock_bh(&idev->lock);
2657 /* send a neighbour solicitation for our addr */
2658 memset(&unspec, 0, sizeof(unspec));
2659 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2660 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2661 out:
2662 in6_ifa_put(ifp);
2665 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2667 struct net_device * dev = ifp->idev->dev;
2670 * Configure the address for reception. Now it is valid.
2673 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2675 /* If added prefix is link local and forwarding is off,
2676 start sending router solicitations.
2679 if (ifp->idev->cnf.forwarding == 0 &&
2680 ifp->idev->cnf.rtr_solicits > 0 &&
2681 (dev->flags&IFF_LOOPBACK) == 0 &&
2682 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2683 struct in6_addr all_routers;
2685 ipv6_addr_all_routers(&all_routers);
2688 * If a host as already performed a random delay
2689 * [...] as part of DAD [...] there is no need
2690 * to delay again before sending the first RS
2692 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2694 spin_lock_bh(&ifp->lock);
2695 ifp->probes = 1;
2696 ifp->idev->if_flags |= IF_RS_SENT;
2697 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2698 spin_unlock_bh(&ifp->lock);
2702 static void addrconf_dad_run(struct inet6_dev *idev) {
2703 struct inet6_ifaddr *ifp;
2705 read_lock_bh(&idev->lock);
2706 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2707 spin_lock_bh(&ifp->lock);
2708 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2709 spin_unlock_bh(&ifp->lock);
2710 continue;
2712 spin_unlock_bh(&ifp->lock);
2713 addrconf_dad_kick(ifp);
2715 read_unlock_bh(&idev->lock);
2718 #ifdef CONFIG_PROC_FS
2719 struct if6_iter_state {
2720 int bucket;
2723 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2725 struct inet6_ifaddr *ifa = NULL;
2726 struct if6_iter_state *state = seq->private;
2728 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2729 ifa = inet6_addr_lst[state->bucket];
2730 if (ifa)
2731 break;
2733 return ifa;
2736 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2738 struct if6_iter_state *state = seq->private;
2740 ifa = ifa->lst_next;
2741 try_again:
2742 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2743 ifa = inet6_addr_lst[state->bucket];
2744 goto try_again;
2746 return ifa;
2749 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2751 struct inet6_ifaddr *ifa = if6_get_first(seq);
2753 if (ifa)
2754 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2755 --pos;
2756 return pos ? NULL : ifa;
2759 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2761 read_lock_bh(&addrconf_hash_lock);
2762 return if6_get_idx(seq, *pos);
2765 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2767 struct inet6_ifaddr *ifa;
2769 ifa = if6_get_next(seq, v);
2770 ++*pos;
2771 return ifa;
2774 static void if6_seq_stop(struct seq_file *seq, void *v)
2776 read_unlock_bh(&addrconf_hash_lock);
2779 static int if6_seq_show(struct seq_file *seq, void *v)
2781 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2782 seq_printf(seq,
2783 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2784 NIP6(ifp->addr),
2785 ifp->idev->dev->ifindex,
2786 ifp->prefix_len,
2787 ifp->scope,
2788 ifp->flags,
2789 ifp->idev->dev->name);
2790 return 0;
2793 static struct seq_operations if6_seq_ops = {
2794 .start = if6_seq_start,
2795 .next = if6_seq_next,
2796 .show = if6_seq_show,
2797 .stop = if6_seq_stop,
2800 static int if6_seq_open(struct inode *inode, struct file *file)
2802 struct seq_file *seq;
2803 int rc = -ENOMEM;
2804 struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2806 if (!s)
2807 goto out;
2809 rc = seq_open(file, &if6_seq_ops);
2810 if (rc)
2811 goto out_kfree;
2813 seq = file->private_data;
2814 seq->private = s;
2815 out:
2816 return rc;
2817 out_kfree:
2818 kfree(s);
2819 goto out;
2822 static const struct file_operations if6_fops = {
2823 .owner = THIS_MODULE,
2824 .open = if6_seq_open,
2825 .read = seq_read,
2826 .llseek = seq_lseek,
2827 .release = seq_release_private,
2830 int __init if6_proc_init(void)
2832 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2833 return -ENOMEM;
2834 return 0;
2837 void if6_proc_exit(void)
2839 proc_net_remove("if_inet6");
2841 #endif /* CONFIG_PROC_FS */
2843 #ifdef CONFIG_IPV6_MIP6
2844 /* Check if address is a home address configured on any interface. */
2845 int ipv6_chk_home_addr(struct in6_addr *addr)
2847 int ret = 0;
2848 struct inet6_ifaddr * ifp;
2849 u8 hash = ipv6_addr_hash(addr);
2850 read_lock_bh(&addrconf_hash_lock);
2851 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2852 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2853 (ifp->flags & IFA_F_HOMEADDRESS)) {
2854 ret = 1;
2855 break;
2858 read_unlock_bh(&addrconf_hash_lock);
2859 return ret;
2861 #endif
2864 * Periodic address status verification
2867 static void addrconf_verify(unsigned long foo)
2869 struct inet6_ifaddr *ifp;
2870 unsigned long now, next;
2871 int i;
2873 spin_lock_bh(&addrconf_verify_lock);
2874 now = jiffies;
2875 next = now + ADDR_CHECK_FREQUENCY;
2877 del_timer(&addr_chk_timer);
2879 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2881 restart:
2882 read_lock(&addrconf_hash_lock);
2883 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2884 unsigned long age;
2885 #ifdef CONFIG_IPV6_PRIVACY
2886 unsigned long regen_advance;
2887 #endif
2889 if (ifp->flags & IFA_F_PERMANENT)
2890 continue;
2892 spin_lock(&ifp->lock);
2893 age = (now - ifp->tstamp) / HZ;
2895 #ifdef CONFIG_IPV6_PRIVACY
2896 regen_advance = ifp->idev->cnf.regen_max_retry *
2897 ifp->idev->cnf.dad_transmits *
2898 ifp->idev->nd_parms->retrans_time / HZ;
2899 #endif
2901 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2902 age >= ifp->valid_lft) {
2903 spin_unlock(&ifp->lock);
2904 in6_ifa_hold(ifp);
2905 read_unlock(&addrconf_hash_lock);
2906 ipv6_del_addr(ifp);
2907 goto restart;
2908 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2909 spin_unlock(&ifp->lock);
2910 continue;
2911 } else if (age >= ifp->prefered_lft) {
2912 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2913 int deprecate = 0;
2915 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2916 deprecate = 1;
2917 ifp->flags |= IFA_F_DEPRECATED;
2920 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2921 next = ifp->tstamp + ifp->valid_lft * HZ;
2923 spin_unlock(&ifp->lock);
2925 if (deprecate) {
2926 in6_ifa_hold(ifp);
2927 read_unlock(&addrconf_hash_lock);
2929 ipv6_ifa_notify(0, ifp);
2930 in6_ifa_put(ifp);
2931 goto restart;
2933 #ifdef CONFIG_IPV6_PRIVACY
2934 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2935 !(ifp->flags&IFA_F_TENTATIVE)) {
2936 if (age >= ifp->prefered_lft - regen_advance) {
2937 struct inet6_ifaddr *ifpub = ifp->ifpub;
2938 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2939 next = ifp->tstamp + ifp->prefered_lft * HZ;
2940 if (!ifp->regen_count && ifpub) {
2941 ifp->regen_count++;
2942 in6_ifa_hold(ifp);
2943 in6_ifa_hold(ifpub);
2944 spin_unlock(&ifp->lock);
2945 read_unlock(&addrconf_hash_lock);
2946 spin_lock(&ifpub->lock);
2947 ifpub->regen_count = 0;
2948 spin_unlock(&ifpub->lock);
2949 ipv6_create_tempaddr(ifpub, ifp);
2950 in6_ifa_put(ifpub);
2951 in6_ifa_put(ifp);
2952 goto restart;
2954 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2955 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2956 spin_unlock(&ifp->lock);
2957 #endif
2958 } else {
2959 /* ifp->prefered_lft <= ifp->valid_lft */
2960 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2961 next = ifp->tstamp + ifp->prefered_lft * HZ;
2962 spin_unlock(&ifp->lock);
2965 read_unlock(&addrconf_hash_lock);
2968 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2969 add_timer(&addr_chk_timer);
2970 spin_unlock_bh(&addrconf_verify_lock);
2973 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2975 struct in6_addr *pfx = NULL;
2977 if (addr)
2978 pfx = nla_data(addr);
2980 if (local) {
2981 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2982 pfx = NULL;
2983 else
2984 pfx = nla_data(local);
2987 return pfx;
2990 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
2991 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2992 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2993 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2996 static int
2997 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2999 struct ifaddrmsg *ifm;
3000 struct nlattr *tb[IFA_MAX+1];
3001 struct in6_addr *pfx;
3002 int err;
3004 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3005 if (err < 0)
3006 return err;
3008 ifm = nlmsg_data(nlh);
3009 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3010 if (pfx == NULL)
3011 return -EINVAL;
3013 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3016 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3017 u32 prefered_lft, u32 valid_lft)
3019 u32 flags = RTF_EXPIRES;
3021 if (!valid_lft || (prefered_lft > valid_lft))
3022 return -EINVAL;
3024 if (valid_lft == INFINITY_LIFE_TIME) {
3025 ifa_flags |= IFA_F_PERMANENT;
3026 flags = 0;
3027 } else if (valid_lft >= 0x7FFFFFFF/HZ)
3028 valid_lft = 0x7FFFFFFF/HZ;
3030 if (prefered_lft == 0)
3031 ifa_flags |= IFA_F_DEPRECATED;
3032 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3033 (prefered_lft != INFINITY_LIFE_TIME))
3034 prefered_lft = 0x7FFFFFFF/HZ;
3036 spin_lock_bh(&ifp->lock);
3037 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3038 ifp->tstamp = jiffies;
3039 ifp->valid_lft = valid_lft;
3040 ifp->prefered_lft = prefered_lft;
3042 spin_unlock_bh(&ifp->lock);
3043 if (!(ifp->flags&IFA_F_TENTATIVE))
3044 ipv6_ifa_notify(0, ifp);
3046 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3047 jiffies_to_clock_t(valid_lft * HZ), flags);
3048 addrconf_verify(0);
3050 return 0;
3053 static int
3054 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3056 struct ifaddrmsg *ifm;
3057 struct nlattr *tb[IFA_MAX+1];
3058 struct in6_addr *pfx;
3059 struct inet6_ifaddr *ifa;
3060 struct net_device *dev;
3061 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3062 u8 ifa_flags;
3063 int err;
3065 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3066 if (err < 0)
3067 return err;
3069 ifm = nlmsg_data(nlh);
3070 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3071 if (pfx == NULL)
3072 return -EINVAL;
3074 if (tb[IFA_CACHEINFO]) {
3075 struct ifa_cacheinfo *ci;
3077 ci = nla_data(tb[IFA_CACHEINFO]);
3078 valid_lft = ci->ifa_valid;
3079 preferred_lft = ci->ifa_prefered;
3080 } else {
3081 preferred_lft = INFINITY_LIFE_TIME;
3082 valid_lft = INFINITY_LIFE_TIME;
3085 dev = __dev_get_by_index(ifm->ifa_index);
3086 if (dev == NULL)
3087 return -ENODEV;
3089 /* We ignore other flags so far. */
3090 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3092 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3093 if (ifa == NULL) {
3095 * It would be best to check for !NLM_F_CREATE here but
3096 * userspace alreay relies on not having to provide this.
3098 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
3099 ifa_flags, preferred_lft, valid_lft);
3102 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3103 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3104 err = -EEXIST;
3105 else
3106 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3108 in6_ifa_put(ifa);
3110 return err;
3113 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3114 u8 scope, int ifindex)
3116 struct ifaddrmsg *ifm;
3118 ifm = nlmsg_data(nlh);
3119 ifm->ifa_family = AF_INET6;
3120 ifm->ifa_prefixlen = prefixlen;
3121 ifm->ifa_flags = flags;
3122 ifm->ifa_scope = scope;
3123 ifm->ifa_index = ifindex;
3126 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3127 unsigned long tstamp, u32 preferred, u32 valid)
3129 struct ifa_cacheinfo ci;
3131 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3132 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3133 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3134 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3135 ci.ifa_prefered = preferred;
3136 ci.ifa_valid = valid;
3138 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3141 static inline int rt_scope(int ifa_scope)
3143 if (ifa_scope & IFA_HOST)
3144 return RT_SCOPE_HOST;
3145 else if (ifa_scope & IFA_LINK)
3146 return RT_SCOPE_LINK;
3147 else if (ifa_scope & IFA_SITE)
3148 return RT_SCOPE_SITE;
3149 else
3150 return RT_SCOPE_UNIVERSE;
3153 static inline int inet6_ifaddr_msgsize(void)
3155 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3156 + nla_total_size(16) /* IFA_ADDRESS */
3157 + nla_total_size(sizeof(struct ifa_cacheinfo));
3160 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3161 u32 pid, u32 seq, int event, unsigned int flags)
3163 struct nlmsghdr *nlh;
3164 u32 preferred, valid;
3166 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3167 if (nlh == NULL)
3168 return -EMSGSIZE;
3170 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3171 ifa->idev->dev->ifindex);
3173 if (!(ifa->flags&IFA_F_PERMANENT)) {
3174 preferred = ifa->prefered_lft;
3175 valid = ifa->valid_lft;
3176 if (preferred != INFINITY_LIFE_TIME) {
3177 long tval = (jiffies - ifa->tstamp)/HZ;
3178 preferred -= tval;
3179 if (valid != INFINITY_LIFE_TIME)
3180 valid -= tval;
3182 } else {
3183 preferred = INFINITY_LIFE_TIME;
3184 valid = INFINITY_LIFE_TIME;
3187 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3188 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3189 nlmsg_cancel(skb, nlh);
3190 return -EMSGSIZE;
3193 return nlmsg_end(skb, nlh);
3196 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3197 u32 pid, u32 seq, int event, u16 flags)
3199 struct nlmsghdr *nlh;
3200 u8 scope = RT_SCOPE_UNIVERSE;
3201 int ifindex = ifmca->idev->dev->ifindex;
3203 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3204 scope = RT_SCOPE_SITE;
3206 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3207 if (nlh == NULL)
3208 return -EMSGSIZE;
3210 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3211 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3212 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3213 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3214 nlmsg_cancel(skb, nlh);
3215 return -EMSGSIZE;
3218 return nlmsg_end(skb, nlh);
3221 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3222 u32 pid, u32 seq, int event, unsigned int flags)
3224 struct nlmsghdr *nlh;
3225 u8 scope = RT_SCOPE_UNIVERSE;
3226 int ifindex = ifaca->aca_idev->dev->ifindex;
3228 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3229 scope = RT_SCOPE_SITE;
3231 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3232 if (nlh == NULL)
3233 return -EMSGSIZE;
3235 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3236 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3237 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3238 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3239 nlmsg_cancel(skb, nlh);
3240 return -EMSGSIZE;
3243 return nlmsg_end(skb, nlh);
3246 enum addr_type_t
3248 UNICAST_ADDR,
3249 MULTICAST_ADDR,
3250 ANYCAST_ADDR,
3253 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3254 enum addr_type_t type)
3256 int idx, ip_idx;
3257 int s_idx, s_ip_idx;
3258 int err = 1;
3259 struct net_device *dev;
3260 struct inet6_dev *idev = NULL;
3261 struct inet6_ifaddr *ifa;
3262 struct ifmcaddr6 *ifmca;
3263 struct ifacaddr6 *ifaca;
3265 s_idx = cb->args[0];
3266 s_ip_idx = ip_idx = cb->args[1];
3268 idx = 0;
3269 for_each_netdev(dev) {
3270 if (idx < s_idx)
3271 goto cont;
3272 if (idx > s_idx)
3273 s_ip_idx = 0;
3274 ip_idx = 0;
3275 if ((idev = in6_dev_get(dev)) == NULL)
3276 goto cont;
3277 read_lock_bh(&idev->lock);
3278 switch (type) {
3279 case UNICAST_ADDR:
3280 /* unicast address incl. temp addr */
3281 for (ifa = idev->addr_list; ifa;
3282 ifa = ifa->if_next, ip_idx++) {
3283 if (ip_idx < s_ip_idx)
3284 continue;
3285 if ((err = inet6_fill_ifaddr(skb, ifa,
3286 NETLINK_CB(cb->skb).pid,
3287 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3288 NLM_F_MULTI)) <= 0)
3289 goto done;
3291 break;
3292 case MULTICAST_ADDR:
3293 /* multicast address */
3294 for (ifmca = idev->mc_list; ifmca;
3295 ifmca = ifmca->next, ip_idx++) {
3296 if (ip_idx < s_ip_idx)
3297 continue;
3298 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3299 NETLINK_CB(cb->skb).pid,
3300 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3301 NLM_F_MULTI)) <= 0)
3302 goto done;
3304 break;
3305 case ANYCAST_ADDR:
3306 /* anycast address */
3307 for (ifaca = idev->ac_list; ifaca;
3308 ifaca = ifaca->aca_next, ip_idx++) {
3309 if (ip_idx < s_ip_idx)
3310 continue;
3311 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3312 NETLINK_CB(cb->skb).pid,
3313 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3314 NLM_F_MULTI)) <= 0)
3315 goto done;
3317 break;
3318 default:
3319 break;
3321 read_unlock_bh(&idev->lock);
3322 in6_dev_put(idev);
3323 cont:
3324 idx++;
3326 done:
3327 if (err <= 0) {
3328 read_unlock_bh(&idev->lock);
3329 in6_dev_put(idev);
3331 cb->args[0] = idx;
3332 cb->args[1] = ip_idx;
3333 return skb->len;
3336 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3338 enum addr_type_t type = UNICAST_ADDR;
3339 return inet6_dump_addr(skb, cb, type);
3342 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3344 enum addr_type_t type = MULTICAST_ADDR;
3345 return inet6_dump_addr(skb, cb, type);
3349 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3351 enum addr_type_t type = ANYCAST_ADDR;
3352 return inet6_dump_addr(skb, cb, type);
3355 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3356 void *arg)
3358 struct ifaddrmsg *ifm;
3359 struct nlattr *tb[IFA_MAX+1];
3360 struct in6_addr *addr = NULL;
3361 struct net_device *dev = NULL;
3362 struct inet6_ifaddr *ifa;
3363 struct sk_buff *skb;
3364 int err;
3366 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3367 if (err < 0)
3368 goto errout;
3370 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3371 if (addr == NULL) {
3372 err = -EINVAL;
3373 goto errout;
3376 ifm = nlmsg_data(nlh);
3377 if (ifm->ifa_index)
3378 dev = __dev_get_by_index(ifm->ifa_index);
3380 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3381 err = -EADDRNOTAVAIL;
3382 goto errout;
3385 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
3386 err = -ENOBUFS;
3387 goto errout_ifa;
3390 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3391 nlh->nlmsg_seq, RTM_NEWADDR, 0);
3392 if (err < 0) {
3393 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3394 WARN_ON(err == -EMSGSIZE);
3395 kfree_skb(skb);
3396 goto errout_ifa;
3398 err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
3399 errout_ifa:
3400 in6_ifa_put(ifa);
3401 errout:
3402 return err;
3405 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3407 struct sk_buff *skb;
3408 int err = -ENOBUFS;
3410 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
3411 if (skb == NULL)
3412 goto errout;
3414 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3415 if (err < 0) {
3416 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3417 WARN_ON(err == -EMSGSIZE);
3418 kfree_skb(skb);
3419 goto errout;
3421 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3422 errout:
3423 if (err < 0)
3424 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
3427 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
3428 __s32 *array, int bytes)
3430 BUG_ON(bytes < (DEVCONF_MAX * 4));
3432 memset(array, 0, bytes);
3433 array[DEVCONF_FORWARDING] = cnf->forwarding;
3434 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3435 array[DEVCONF_MTU6] = cnf->mtu6;
3436 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3437 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3438 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3439 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3440 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3441 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3442 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3443 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3444 #ifdef CONFIG_IPV6_PRIVACY
3445 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3446 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3447 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3448 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3449 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3450 #endif
3451 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3452 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3453 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3454 #ifdef CONFIG_IPV6_ROUTER_PREF
3455 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
3456 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3457 #ifdef CONFIG_IPV6_ROUTE_INFO
3458 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3459 #endif
3460 #endif
3461 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
3462 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
3463 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3464 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3465 #endif
3468 static inline size_t inet6_if_nlmsg_size(void)
3470 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3471 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3472 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3473 + nla_total_size(4) /* IFLA_MTU */
3474 + nla_total_size(4) /* IFLA_LINK */
3475 + nla_total_size( /* IFLA_PROTINFO */
3476 nla_total_size(4) /* IFLA_INET6_FLAGS */
3477 + nla_total_size(sizeof(struct ifla_cacheinfo))
3478 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3479 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3480 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3484 static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3485 int bytes)
3487 int i;
3488 int pad = bytes - sizeof(u64) * items;
3489 BUG_ON(pad < 0);
3491 /* Use put_unaligned() because stats may not be aligned for u64. */
3492 put_unaligned(items, &stats[0]);
3493 for (i = 1; i < items; i++)
3494 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3496 memset(&stats[items], 0, pad);
3499 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3500 int bytes)
3502 switch(attrtype) {
3503 case IFLA_INET6_STATS:
3504 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3505 break;
3506 case IFLA_INET6_ICMP6STATS:
3507 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3508 break;
3512 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3513 u32 pid, u32 seq, int event, unsigned int flags)
3515 struct net_device *dev = idev->dev;
3516 struct nlattr *nla;
3517 struct ifinfomsg *hdr;
3518 struct nlmsghdr *nlh;
3519 void *protoinfo;
3520 struct ifla_cacheinfo ci;
3522 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3523 if (nlh == NULL)
3524 return -EMSGSIZE;
3526 hdr = nlmsg_data(nlh);
3527 hdr->ifi_family = AF_INET6;
3528 hdr->__ifi_pad = 0;
3529 hdr->ifi_type = dev->type;
3530 hdr->ifi_index = dev->ifindex;
3531 hdr->ifi_flags = dev_get_flags(dev);
3532 hdr->ifi_change = 0;
3534 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
3536 if (dev->addr_len)
3537 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3539 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3540 if (dev->ifindex != dev->iflink)
3541 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3543 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3544 if (protoinfo == NULL)
3545 goto nla_put_failure;
3547 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
3549 ci.max_reasm_len = IPV6_MAXPLEN;
3550 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3551 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3552 ci.reachable_time = idev->nd_parms->reachable_time;
3553 ci.retrans_time = idev->nd_parms->retrans_time;
3554 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3556 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3557 if (nla == NULL)
3558 goto nla_put_failure;
3559 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
3561 /* XXX - MC not implemented */
3563 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3564 if (nla == NULL)
3565 goto nla_put_failure;
3566 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3568 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3569 if (nla == NULL)
3570 goto nla_put_failure;
3571 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
3573 nla_nest_end(skb, protoinfo);
3574 return nlmsg_end(skb, nlh);
3576 nla_put_failure:
3577 nlmsg_cancel(skb, nlh);
3578 return -EMSGSIZE;
3581 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3583 int idx, err;
3584 int s_idx = cb->args[0];
3585 struct net_device *dev;
3586 struct inet6_dev *idev;
3588 read_lock(&dev_base_lock);
3589 idx = 0;
3590 for_each_netdev(dev) {
3591 if (idx < s_idx)
3592 goto cont;
3593 if ((idev = in6_dev_get(dev)) == NULL)
3594 goto cont;
3595 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3596 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3597 in6_dev_put(idev);
3598 if (err <= 0)
3599 break;
3600 cont:
3601 idx++;
3603 read_unlock(&dev_base_lock);
3604 cb->args[0] = idx;
3606 return skb->len;
3609 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3611 struct sk_buff *skb;
3612 int err = -ENOBUFS;
3614 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
3615 if (skb == NULL)
3616 goto errout;
3618 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3619 if (err < 0) {
3620 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3621 WARN_ON(err == -EMSGSIZE);
3622 kfree_skb(skb);
3623 goto errout;
3625 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3626 errout:
3627 if (err < 0)
3628 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
3631 static inline size_t inet6_prefix_nlmsg_size(void)
3633 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3634 + nla_total_size(sizeof(struct in6_addr))
3635 + nla_total_size(sizeof(struct prefix_cacheinfo));
3638 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3639 struct prefix_info *pinfo, u32 pid, u32 seq,
3640 int event, unsigned int flags)
3642 struct prefixmsg *pmsg;
3643 struct nlmsghdr *nlh;
3644 struct prefix_cacheinfo ci;
3646 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3647 if (nlh == NULL)
3648 return -EMSGSIZE;
3650 pmsg = nlmsg_data(nlh);
3651 pmsg->prefix_family = AF_INET6;
3652 pmsg->prefix_pad1 = 0;
3653 pmsg->prefix_pad2 = 0;
3654 pmsg->prefix_ifindex = idev->dev->ifindex;
3655 pmsg->prefix_len = pinfo->prefix_len;
3656 pmsg->prefix_type = pinfo->type;
3657 pmsg->prefix_pad3 = 0;
3658 pmsg->prefix_flags = 0;
3659 if (pinfo->onlink)
3660 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3661 if (pinfo->autoconf)
3662 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3664 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3666 ci.preferred_time = ntohl(pinfo->prefered);
3667 ci.valid_time = ntohl(pinfo->valid);
3668 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3670 return nlmsg_end(skb, nlh);
3672 nla_put_failure:
3673 nlmsg_cancel(skb, nlh);
3674 return -EMSGSIZE;
3677 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3678 struct prefix_info *pinfo)
3680 struct sk_buff *skb;
3681 int err = -ENOBUFS;
3683 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
3684 if (skb == NULL)
3685 goto errout;
3687 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3688 if (err < 0) {
3689 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3690 WARN_ON(err == -EMSGSIZE);
3691 kfree_skb(skb);
3692 goto errout;
3694 err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3695 errout:
3696 if (err < 0)
3697 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
3700 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3702 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3704 switch (event) {
3705 case RTM_NEWADDR:
3707 * If the address was optimistic
3708 * we inserted the route at the start of
3709 * our DAD process, so we don't need
3710 * to do it again
3712 if (!(ifp->rt->rt6i_node))
3713 ip6_ins_rt(ifp->rt);
3714 if (ifp->idev->cnf.forwarding)
3715 addrconf_join_anycast(ifp);
3716 break;
3717 case RTM_DELADDR:
3718 if (ifp->idev->cnf.forwarding)
3719 addrconf_leave_anycast(ifp);
3720 addrconf_leave_solict(ifp->idev, &ifp->addr);
3721 dst_hold(&ifp->rt->u.dst);
3722 if (ip6_del_rt(ifp->rt))
3723 dst_free(&ifp->rt->u.dst);
3724 break;
3728 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3730 rcu_read_lock_bh();
3731 if (likely(ifp->idev->dead == 0))
3732 __ipv6_ifa_notify(event, ifp);
3733 rcu_read_unlock_bh();
3736 #ifdef CONFIG_SYSCTL
3738 static
3739 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3740 void __user *buffer, size_t *lenp, loff_t *ppos)
3742 int *valp = ctl->data;
3743 int val = *valp;
3744 int ret;
3746 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3748 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3749 if (valp != &ipv6_devconf.forwarding) {
3750 if ((!*valp) ^ (!val)) {
3751 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3752 if (idev == NULL)
3753 return ret;
3754 dev_forward_change(idev);
3756 } else {
3757 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3758 addrconf_forward_change();
3760 if (*valp)
3761 rt6_purge_dflt_routers();
3764 return ret;
3767 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3768 int __user *name, int nlen,
3769 void __user *oldval,
3770 size_t __user *oldlenp,
3771 void __user *newval, size_t newlen)
3773 int *valp = table->data;
3774 int new;
3776 if (!newval || !newlen)
3777 return 0;
3778 if (newlen != sizeof(int))
3779 return -EINVAL;
3780 if (get_user(new, (int __user *)newval))
3781 return -EFAULT;
3782 if (new == *valp)
3783 return 0;
3784 if (oldval && oldlenp) {
3785 size_t len;
3786 if (get_user(len, oldlenp))
3787 return -EFAULT;
3788 if (len) {
3789 if (len > table->maxlen)
3790 len = table->maxlen;
3791 if (copy_to_user(oldval, valp, len))
3792 return -EFAULT;
3793 if (put_user(len, oldlenp))
3794 return -EFAULT;
3798 if (valp != &ipv6_devconf_dflt.forwarding) {
3799 if (valp != &ipv6_devconf.forwarding) {
3800 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3801 int changed;
3802 if (unlikely(idev == NULL))
3803 return -ENODEV;
3804 changed = (!*valp) ^ (!new);
3805 *valp = new;
3806 if (changed)
3807 dev_forward_change(idev);
3808 } else {
3809 *valp = new;
3810 addrconf_forward_change();
3813 if (*valp)
3814 rt6_purge_dflt_routers();
3815 } else
3816 *valp = new;
3818 return 1;
3821 static struct addrconf_sysctl_table
3823 struct ctl_table_header *sysctl_header;
3824 ctl_table addrconf_vars[__NET_IPV6_MAX];
3825 ctl_table addrconf_dev[2];
3826 ctl_table addrconf_conf_dir[2];
3827 ctl_table addrconf_proto_dir[2];
3828 ctl_table addrconf_root_dir[2];
3829 } addrconf_sysctl __read_mostly = {
3830 .sysctl_header = NULL,
3831 .addrconf_vars = {
3833 .ctl_name = NET_IPV6_FORWARDING,
3834 .procname = "forwarding",
3835 .data = &ipv6_devconf.forwarding,
3836 .maxlen = sizeof(int),
3837 .mode = 0644,
3838 .proc_handler = &addrconf_sysctl_forward,
3839 .strategy = &addrconf_sysctl_forward_strategy,
3842 .ctl_name = NET_IPV6_HOP_LIMIT,
3843 .procname = "hop_limit",
3844 .data = &ipv6_devconf.hop_limit,
3845 .maxlen = sizeof(int),
3846 .mode = 0644,
3847 .proc_handler = proc_dointvec,
3850 .ctl_name = NET_IPV6_MTU,
3851 .procname = "mtu",
3852 .data = &ipv6_devconf.mtu6,
3853 .maxlen = sizeof(int),
3854 .mode = 0644,
3855 .proc_handler = &proc_dointvec,
3858 .ctl_name = NET_IPV6_ACCEPT_RA,
3859 .procname = "accept_ra",
3860 .data = &ipv6_devconf.accept_ra,
3861 .maxlen = sizeof(int),
3862 .mode = 0644,
3863 .proc_handler = &proc_dointvec,
3866 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3867 .procname = "accept_redirects",
3868 .data = &ipv6_devconf.accept_redirects,
3869 .maxlen = sizeof(int),
3870 .mode = 0644,
3871 .proc_handler = &proc_dointvec,
3874 .ctl_name = NET_IPV6_AUTOCONF,
3875 .procname = "autoconf",
3876 .data = &ipv6_devconf.autoconf,
3877 .maxlen = sizeof(int),
3878 .mode = 0644,
3879 .proc_handler = &proc_dointvec,
3882 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3883 .procname = "dad_transmits",
3884 .data = &ipv6_devconf.dad_transmits,
3885 .maxlen = sizeof(int),
3886 .mode = 0644,
3887 .proc_handler = &proc_dointvec,
3890 .ctl_name = NET_IPV6_RTR_SOLICITS,
3891 .procname = "router_solicitations",
3892 .data = &ipv6_devconf.rtr_solicits,
3893 .maxlen = sizeof(int),
3894 .mode = 0644,
3895 .proc_handler = &proc_dointvec,
3898 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3899 .procname = "router_solicitation_interval",
3900 .data = &ipv6_devconf.rtr_solicit_interval,
3901 .maxlen = sizeof(int),
3902 .mode = 0644,
3903 .proc_handler = &proc_dointvec_jiffies,
3904 .strategy = &sysctl_jiffies,
3907 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3908 .procname = "router_solicitation_delay",
3909 .data = &ipv6_devconf.rtr_solicit_delay,
3910 .maxlen = sizeof(int),
3911 .mode = 0644,
3912 .proc_handler = &proc_dointvec_jiffies,
3913 .strategy = &sysctl_jiffies,
3916 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3917 .procname = "force_mld_version",
3918 .data = &ipv6_devconf.force_mld_version,
3919 .maxlen = sizeof(int),
3920 .mode = 0644,
3921 .proc_handler = &proc_dointvec,
3923 #ifdef CONFIG_IPV6_PRIVACY
3925 .ctl_name = NET_IPV6_USE_TEMPADDR,
3926 .procname = "use_tempaddr",
3927 .data = &ipv6_devconf.use_tempaddr,
3928 .maxlen = sizeof(int),
3929 .mode = 0644,
3930 .proc_handler = &proc_dointvec,
3933 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3934 .procname = "temp_valid_lft",
3935 .data = &ipv6_devconf.temp_valid_lft,
3936 .maxlen = sizeof(int),
3937 .mode = 0644,
3938 .proc_handler = &proc_dointvec,
3941 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3942 .procname = "temp_prefered_lft",
3943 .data = &ipv6_devconf.temp_prefered_lft,
3944 .maxlen = sizeof(int),
3945 .mode = 0644,
3946 .proc_handler = &proc_dointvec,
3949 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3950 .procname = "regen_max_retry",
3951 .data = &ipv6_devconf.regen_max_retry,
3952 .maxlen = sizeof(int),
3953 .mode = 0644,
3954 .proc_handler = &proc_dointvec,
3957 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3958 .procname = "max_desync_factor",
3959 .data = &ipv6_devconf.max_desync_factor,
3960 .maxlen = sizeof(int),
3961 .mode = 0644,
3962 .proc_handler = &proc_dointvec,
3964 #endif
3966 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3967 .procname = "max_addresses",
3968 .data = &ipv6_devconf.max_addresses,
3969 .maxlen = sizeof(int),
3970 .mode = 0644,
3971 .proc_handler = &proc_dointvec,
3974 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3975 .procname = "accept_ra_defrtr",
3976 .data = &ipv6_devconf.accept_ra_defrtr,
3977 .maxlen = sizeof(int),
3978 .mode = 0644,
3979 .proc_handler = &proc_dointvec,
3982 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3983 .procname = "accept_ra_pinfo",
3984 .data = &ipv6_devconf.accept_ra_pinfo,
3985 .maxlen = sizeof(int),
3986 .mode = 0644,
3987 .proc_handler = &proc_dointvec,
3989 #ifdef CONFIG_IPV6_ROUTER_PREF
3991 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3992 .procname = "accept_ra_rtr_pref",
3993 .data = &ipv6_devconf.accept_ra_rtr_pref,
3994 .maxlen = sizeof(int),
3995 .mode = 0644,
3996 .proc_handler = &proc_dointvec,
3999 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4000 .procname = "router_probe_interval",
4001 .data = &ipv6_devconf.rtr_probe_interval,
4002 .maxlen = sizeof(int),
4003 .mode = 0644,
4004 .proc_handler = &proc_dointvec_jiffies,
4005 .strategy = &sysctl_jiffies,
4007 #ifdef CONFIG_IPV6_ROUTE_INFO
4009 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4010 .procname = "accept_ra_rt_info_max_plen",
4011 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4012 .maxlen = sizeof(int),
4013 .mode = 0644,
4014 .proc_handler = &proc_dointvec,
4016 #endif
4017 #endif
4019 .ctl_name = NET_IPV6_PROXY_NDP,
4020 .procname = "proxy_ndp",
4021 .data = &ipv6_devconf.proxy_ndp,
4022 .maxlen = sizeof(int),
4023 .mode = 0644,
4024 .proc_handler = &proc_dointvec,
4027 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4028 .procname = "accept_source_route",
4029 .data = &ipv6_devconf.accept_source_route,
4030 .maxlen = sizeof(int),
4031 .mode = 0644,
4032 .proc_handler = &proc_dointvec,
4034 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4036 .ctl_name = CTL_UNNUMBERED,
4037 .procname = "optimistic_dad",
4038 .data = &ipv6_devconf.optimistic_dad,
4039 .maxlen = sizeof(int),
4040 .mode = 0644,
4041 .proc_handler = &proc_dointvec,
4044 #endif
4046 .ctl_name = 0, /* sentinel */
4049 .addrconf_dev = {
4051 .ctl_name = NET_PROTO_CONF_ALL,
4052 .procname = "all",
4053 .mode = 0555,
4054 .child = addrconf_sysctl.addrconf_vars,
4057 .ctl_name = 0, /* sentinel */
4060 .addrconf_conf_dir = {
4062 .ctl_name = NET_IPV6_CONF,
4063 .procname = "conf",
4064 .mode = 0555,
4065 .child = addrconf_sysctl.addrconf_dev,
4068 .ctl_name = 0, /* sentinel */
4071 .addrconf_proto_dir = {
4073 .ctl_name = NET_IPV6,
4074 .procname = "ipv6",
4075 .mode = 0555,
4076 .child = addrconf_sysctl.addrconf_conf_dir,
4079 .ctl_name = 0, /* sentinel */
4082 .addrconf_root_dir = {
4084 .ctl_name = CTL_NET,
4085 .procname = "net",
4086 .mode = 0555,
4087 .child = addrconf_sysctl.addrconf_proto_dir,
4090 .ctl_name = 0, /* sentinel */
4095 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
4097 int i;
4098 struct net_device *dev = idev ? idev->dev : NULL;
4099 struct addrconf_sysctl_table *t;
4100 char *dev_name = NULL;
4102 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
4103 if (t == NULL)
4104 return;
4105 for (i=0; t->addrconf_vars[i].data; i++) {
4106 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4107 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4109 if (dev) {
4110 dev_name = dev->name;
4111 t->addrconf_dev[0].ctl_name = dev->ifindex;
4112 } else {
4113 dev_name = "default";
4114 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
4118 * Make a copy of dev_name, because '.procname' is regarded as const
4119 * by sysctl and we wouldn't want anyone to change it under our feet
4120 * (see SIOCSIFNAME).
4122 dev_name = kstrdup(dev_name, GFP_KERNEL);
4123 if (!dev_name)
4124 goto free;
4126 t->addrconf_dev[0].procname = dev_name;
4128 t->addrconf_dev[0].child = t->addrconf_vars;
4129 t->addrconf_conf_dir[0].child = t->addrconf_dev;
4130 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
4131 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
4133 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
4134 if (t->sysctl_header == NULL)
4135 goto free_procname;
4136 else
4137 p->sysctl = t;
4138 return;
4140 /* error path */
4141 free_procname:
4142 kfree(dev_name);
4143 free:
4144 kfree(t);
4146 return;
4149 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4151 if (p->sysctl) {
4152 struct addrconf_sysctl_table *t = p->sysctl;
4153 p->sysctl = NULL;
4154 unregister_sysctl_table(t->sysctl_header);
4155 kfree(t->addrconf_dev[0].procname);
4156 kfree(t);
4161 #endif
4164 * Device notifier
4167 int register_inet6addr_notifier(struct notifier_block *nb)
4169 return atomic_notifier_chain_register(&inet6addr_chain, nb);
4172 EXPORT_SYMBOL(register_inet6addr_notifier);
4174 int unregister_inet6addr_notifier(struct notifier_block *nb)
4176 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
4179 EXPORT_SYMBOL(unregister_inet6addr_notifier);
4182 * Init / cleanup code
4185 int __init addrconf_init(void)
4187 int err = 0;
4189 /* The addrconf netdev notifier requires that loopback_dev
4190 * has it's ipv6 private information allocated and setup
4191 * before it can bring up and give link-local addresses
4192 * to other devices which are up.
4194 * Unfortunately, loopback_dev is not necessarily the first
4195 * entry in the global dev_base list of net devices. In fact,
4196 * it is likely to be the very last entry on that list.
4197 * So this causes the notifier registry below to try and
4198 * give link-local addresses to all devices besides loopback_dev
4199 * first, then loopback_dev, which cases all the non-loopback_dev
4200 * devices to fail to get a link-local address.
4202 * So, as a temporary fix, allocate the ipv6 structure for
4203 * loopback_dev first by hand.
4204 * Longer term, all of the dependencies ipv6 has upon the loopback
4205 * device and it being up should be removed.
4207 rtnl_lock();
4208 if (!ipv6_add_dev(&loopback_dev))
4209 err = -ENOMEM;
4210 rtnl_unlock();
4211 if (err)
4212 return err;
4214 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4215 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4216 ip6_prohibit_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4217 ip6_blk_hole_entry.rt6i_idev = in6_dev_get(&loopback_dev);
4218 #endif
4220 register_netdevice_notifier(&ipv6_dev_notf);
4222 addrconf_verify(0);
4224 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4225 if (err < 0)
4226 goto errout;
4228 /* Only the first call to __rtnl_register can fail */
4229 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4230 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4231 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4232 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4233 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4235 #ifdef CONFIG_SYSCTL
4236 addrconf_sysctl.sysctl_header =
4237 register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
4238 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4239 #endif
4241 return 0;
4242 errout:
4243 unregister_netdevice_notifier(&ipv6_dev_notf);
4245 return err;
4248 void __exit addrconf_cleanup(void)
4250 struct net_device *dev;
4251 struct inet6_dev *idev;
4252 struct inet6_ifaddr *ifa;
4253 int i;
4255 unregister_netdevice_notifier(&ipv6_dev_notf);
4257 #ifdef CONFIG_SYSCTL
4258 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4259 addrconf_sysctl_unregister(&ipv6_devconf);
4260 #endif
4262 rtnl_lock();
4265 * clean dev list.
4268 for_each_netdev(dev) {
4269 if ((idev = __in6_dev_get(dev)) == NULL)
4270 continue;
4271 addrconf_ifdown(dev, 1);
4273 addrconf_ifdown(&loopback_dev, 2);
4276 * Check hash table.
4279 write_lock_bh(&addrconf_hash_lock);
4280 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4281 for (ifa=inet6_addr_lst[i]; ifa; ) {
4282 struct inet6_ifaddr *bifa;
4284 bifa = ifa;
4285 ifa = ifa->lst_next;
4286 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4287 /* Do not free it; something is wrong.
4288 Now we can investigate it with debugger.
4292 write_unlock_bh(&addrconf_hash_lock);
4294 del_timer(&addr_chk_timer);
4296 rtnl_unlock();
4298 #ifdef CONFIG_PROC_FS
4299 proc_net_remove("if_inet6");
4300 #endif