net: remove CVS keywords
[linux-2.6/kvm.git] / net / ipv6 / addrconf.c
blobdeb38bf0337639ddaee709afb300feddaf5ceb84
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 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
16 * Changes:
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <linux/in6.h>
47 #include <linux/netdevice.h>
48 #include <linux/if_addr.h>
49 #include <linux/if_arp.h>
50 #include <linux/if_arcnet.h>
51 #include <linux/if_infiniband.h>
52 #include <linux/route.h>
53 #include <linux/inetdevice.h>
54 #include <linux/init.h>
55 #ifdef CONFIG_SYSCTL
56 #include <linux/sysctl.h>
57 #endif
58 #include <linux/capability.h>
59 #include <linux/delay.h>
60 #include <linux/notifier.h>
61 #include <linux/string.h>
63 #include <net/net_namespace.h>
64 #include <net/sock.h>
65 #include <net/snmp.h>
67 #include <net/ipv6.h>
68 #include <net/protocol.h>
69 #include <net/ndisc.h>
70 #include <net/ip6_route.h>
71 #include <net/addrconf.h>
72 #include <net/tcp.h>
73 #include <net/ip.h>
74 #include <net/netlink.h>
75 #include <net/pkt_sched.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
81 #endif
83 #include <asm/uaccess.h>
84 #include <asm/unaligned.h>
86 #include <linux/proc_fs.h>
87 #include <linux/seq_file.h>
89 /* Set to 3 to get tracing... */
90 #define ACONF_DEBUG 2
92 #if ACONF_DEBUG >= 3
93 #define ADBG(x) printk x
94 #else
95 #define ADBG(x)
96 #endif
98 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
101 #ifdef CONFIG_SYSCTL
102 static void addrconf_sysctl_register(struct inet6_dev *idev);
103 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
104 #else
105 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
109 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
112 #endif
114 #ifdef CONFIG_IPV6_PRIVACY
115 static int __ipv6_regen_rndid(struct inet6_dev *idev);
116 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
117 static void ipv6_regen_rndid(unsigned long data);
119 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
120 #endif
122 static int ipv6_count_addresses(struct inet6_dev *idev);
125 * Configured unicast address hash table
127 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
128 static DEFINE_RWLOCK(addrconf_hash_lock);
130 static void addrconf_verify(unsigned long);
132 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
133 static DEFINE_SPINLOCK(addrconf_verify_lock);
135 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
136 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
138 static int addrconf_ifdown(struct net_device *dev, int how);
140 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
141 static void addrconf_dad_timer(unsigned long data);
142 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
143 static void addrconf_dad_run(struct inet6_dev *idev);
144 static void addrconf_rs_timer(unsigned long data);
145 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
146 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
148 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
149 struct prefix_info *pinfo);
150 static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
151 struct net_device *dev);
153 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
155 struct ipv6_devconf ipv6_devconf __read_mostly = {
156 .forwarding = 0,
157 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
158 .mtu6 = IPV6_MIN_MTU,
159 .accept_ra = 1,
160 .accept_redirects = 1,
161 .autoconf = 1,
162 .force_mld_version = 0,
163 .dad_transmits = 1,
164 .rtr_solicits = MAX_RTR_SOLICITATIONS,
165 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
166 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
167 #ifdef CONFIG_IPV6_PRIVACY
168 .use_tempaddr = 0,
169 .temp_valid_lft = TEMP_VALID_LIFETIME,
170 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
171 .regen_max_retry = REGEN_MAX_RETRY,
172 .max_desync_factor = MAX_DESYNC_FACTOR,
173 #endif
174 .max_addresses = IPV6_MAX_ADDRESSES,
175 .accept_ra_defrtr = 1,
176 .accept_ra_pinfo = 1,
177 #ifdef CONFIG_IPV6_ROUTER_PREF
178 .accept_ra_rtr_pref = 1,
179 .rtr_probe_interval = 60 * HZ,
180 #ifdef CONFIG_IPV6_ROUTE_INFO
181 .accept_ra_rt_info_max_plen = 0,
182 #endif
183 #endif
184 .proxy_ndp = 0,
185 .accept_source_route = 0, /* we do not accept RH0 by default. */
188 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
189 .forwarding = 0,
190 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
191 .mtu6 = IPV6_MIN_MTU,
192 .accept_ra = 1,
193 .accept_redirects = 1,
194 .autoconf = 1,
195 .dad_transmits = 1,
196 .rtr_solicits = MAX_RTR_SOLICITATIONS,
197 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
198 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
199 #ifdef CONFIG_IPV6_PRIVACY
200 .use_tempaddr = 0,
201 .temp_valid_lft = TEMP_VALID_LIFETIME,
202 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
203 .regen_max_retry = REGEN_MAX_RETRY,
204 .max_desync_factor = MAX_DESYNC_FACTOR,
205 #endif
206 .max_addresses = IPV6_MAX_ADDRESSES,
207 .accept_ra_defrtr = 1,
208 .accept_ra_pinfo = 1,
209 #ifdef CONFIG_IPV6_ROUTER_PREF
210 .accept_ra_rtr_pref = 1,
211 .rtr_probe_interval = 60 * HZ,
212 #ifdef CONFIG_IPV6_ROUTE_INFO
213 .accept_ra_rt_info_max_plen = 0,
214 #endif
215 #endif
216 .proxy_ndp = 0,
217 .accept_source_route = 0, /* we do not accept RH0 by default. */
220 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
221 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
222 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
223 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
224 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
226 /* Check if a valid qdisc is available */
227 static inline int addrconf_qdisc_ok(struct net_device *dev)
229 return (dev->qdisc != &noop_qdisc);
232 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
234 if (del_timer(&ifp->timer))
235 __in6_ifa_put(ifp);
238 enum addrconf_timer_t
240 AC_NONE,
241 AC_DAD,
242 AC_RS,
245 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
246 enum addrconf_timer_t what,
247 unsigned long when)
249 if (!del_timer(&ifp->timer))
250 in6_ifa_hold(ifp);
252 switch (what) {
253 case AC_DAD:
254 ifp->timer.function = addrconf_dad_timer;
255 break;
256 case AC_RS:
257 ifp->timer.function = addrconf_rs_timer;
258 break;
259 default:;
261 ifp->timer.expires = jiffies + when;
262 add_timer(&ifp->timer);
265 static int snmp6_alloc_dev(struct inet6_dev *idev)
267 if (snmp_mib_init((void **)idev->stats.ipv6,
268 sizeof(struct ipstats_mib)) < 0)
269 goto err_ip;
270 if (snmp_mib_init((void **)idev->stats.icmpv6,
271 sizeof(struct icmpv6_mib)) < 0)
272 goto err_icmp;
273 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
274 sizeof(struct icmpv6msg_mib)) < 0)
275 goto err_icmpmsg;
277 return 0;
279 err_icmpmsg:
280 snmp_mib_free((void **)idev->stats.icmpv6);
281 err_icmp:
282 snmp_mib_free((void **)idev->stats.ipv6);
283 err_ip:
284 return -ENOMEM;
287 static void snmp6_free_dev(struct inet6_dev *idev)
289 snmp_mib_free((void **)idev->stats.icmpv6msg);
290 snmp_mib_free((void **)idev->stats.icmpv6);
291 snmp_mib_free((void **)idev->stats.ipv6);
294 /* Nobody refers to this device, we may destroy it. */
296 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
298 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
299 kfree(idev);
302 void in6_dev_finish_destroy(struct inet6_dev *idev)
304 struct net_device *dev = idev->dev;
305 BUG_TRAP(idev->addr_list==NULL);
306 BUG_TRAP(idev->mc_list==NULL);
307 #ifdef NET_REFCNT_DEBUG
308 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
309 #endif
310 dev_put(dev);
311 if (!idev->dead) {
312 printk("Freeing alive inet6 device %p\n", idev);
313 return;
315 snmp6_free_dev(idev);
316 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
319 EXPORT_SYMBOL(in6_dev_finish_destroy);
321 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
323 struct inet6_dev *ndev;
325 ASSERT_RTNL();
327 if (dev->mtu < IPV6_MIN_MTU)
328 return NULL;
330 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
332 if (ndev == NULL)
333 return NULL;
335 rwlock_init(&ndev->lock);
336 ndev->dev = dev;
337 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
338 ndev->cnf.mtu6 = dev->mtu;
339 ndev->cnf.sysctl = NULL;
340 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
341 if (ndev->nd_parms == NULL) {
342 kfree(ndev);
343 return NULL;
345 /* We refer to the device */
346 dev_hold(dev);
348 if (snmp6_alloc_dev(ndev) < 0) {
349 ADBG((KERN_WARNING
350 "%s(): cannot allocate memory for statistics; dev=%s.\n",
351 __func__, dev->name));
352 neigh_parms_release(&nd_tbl, ndev->nd_parms);
353 ndev->dead = 1;
354 in6_dev_finish_destroy(ndev);
355 return NULL;
358 if (snmp6_register_dev(ndev) < 0) {
359 ADBG((KERN_WARNING
360 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
361 __func__, dev->name));
362 neigh_parms_release(&nd_tbl, ndev->nd_parms);
363 ndev->dead = 1;
364 in6_dev_finish_destroy(ndev);
365 return NULL;
368 /* One reference from device. We must do this before
369 * we invoke __ipv6_regen_rndid().
371 in6_dev_hold(ndev);
373 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
374 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
375 printk(KERN_INFO
376 "%s: Disabled Multicast RS\n",
377 dev->name);
378 ndev->cnf.rtr_solicits = 0;
380 #endif
382 #ifdef CONFIG_IPV6_PRIVACY
383 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
384 if ((dev->flags&IFF_LOOPBACK) ||
385 dev->type == ARPHRD_TUNNEL ||
386 dev->type == ARPHRD_TUNNEL6 ||
387 dev->type == ARPHRD_SIT ||
388 dev->type == ARPHRD_NONE) {
389 printk(KERN_INFO
390 "%s: Disabled Privacy Extensions\n",
391 dev->name);
392 ndev->cnf.use_tempaddr = -1;
393 } else {
394 in6_dev_hold(ndev);
395 ipv6_regen_rndid((unsigned long) ndev);
397 #endif
399 if (netif_running(dev) && addrconf_qdisc_ok(dev))
400 ndev->if_flags |= IF_READY;
402 ipv6_mc_init_dev(ndev);
403 ndev->tstamp = jiffies;
404 addrconf_sysctl_register(ndev);
405 /* protected by rtnl_lock */
406 rcu_assign_pointer(dev->ip6_ptr, ndev);
408 /* Join all-node multicast group */
409 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
411 return ndev;
414 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
416 struct inet6_dev *idev;
418 ASSERT_RTNL();
420 if ((idev = __in6_dev_get(dev)) == NULL) {
421 if ((idev = ipv6_add_dev(dev)) == NULL)
422 return NULL;
425 if (dev->flags&IFF_UP)
426 ipv6_mc_up(idev);
427 return idev;
430 #ifdef CONFIG_SYSCTL
431 static void dev_forward_change(struct inet6_dev *idev)
433 struct net_device *dev;
434 struct inet6_ifaddr *ifa;
436 if (!idev)
437 return;
438 dev = idev->dev;
439 if (dev && (dev->flags & IFF_MULTICAST)) {
440 if (idev->cnf.forwarding)
441 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
442 else
443 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
445 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
446 if (ifa->flags&IFA_F_TENTATIVE)
447 continue;
448 if (idev->cnf.forwarding)
449 addrconf_join_anycast(ifa);
450 else
451 addrconf_leave_anycast(ifa);
456 static void addrconf_forward_change(struct net *net, __s32 newf)
458 struct net_device *dev;
459 struct inet6_dev *idev;
461 read_lock(&dev_base_lock);
462 for_each_netdev(net, dev) {
463 rcu_read_lock();
464 idev = __in6_dev_get(dev);
465 if (idev) {
466 int changed = (!idev->cnf.forwarding) ^ (!newf);
467 idev->cnf.forwarding = newf;
468 if (changed)
469 dev_forward_change(idev);
471 rcu_read_unlock();
473 read_unlock(&dev_base_lock);
476 static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
478 struct net *net;
480 net = (struct net *)table->extra2;
481 if (p == &net->ipv6.devconf_dflt->forwarding)
482 return;
484 if (p == &net->ipv6.devconf_all->forwarding) {
485 __s32 newf = net->ipv6.devconf_all->forwarding;
486 net->ipv6.devconf_dflt->forwarding = newf;
487 addrconf_forward_change(net, newf);
488 } else if ((!*p) ^ (!old))
489 dev_forward_change((struct inet6_dev *)table->extra1);
491 if (*p)
492 rt6_purge_dflt_routers(net);
494 #endif
496 /* Nobody refers to this ifaddr, destroy it */
498 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
500 BUG_TRAP(ifp->if_next==NULL);
501 BUG_TRAP(ifp->lst_next==NULL);
502 #ifdef NET_REFCNT_DEBUG
503 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
504 #endif
506 in6_dev_put(ifp->idev);
508 if (del_timer(&ifp->timer))
509 printk("Timer is still running, when freeing ifa=%p\n", ifp);
511 if (!ifp->dead) {
512 printk("Freeing alive inet6 address %p\n", ifp);
513 return;
515 dst_release(&ifp->rt->u.dst);
517 kfree(ifp);
520 static void
521 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
523 struct inet6_ifaddr *ifa, **ifap;
524 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
527 * Each device address list is sorted in order of scope -
528 * global before linklocal.
530 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
531 ifap = &ifa->if_next) {
532 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
533 break;
536 ifp->if_next = *ifap;
537 *ifap = ifp;
541 * Hash function taken from net_alias.c
543 static u8 ipv6_addr_hash(const struct in6_addr *addr)
545 __u32 word;
548 * We perform the hash function over the last 64 bits of the address
549 * This will include the IEEE address token on links that support it.
552 word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]);
553 word ^= (word >> 16);
554 word ^= (word >> 8);
556 return ((word ^ (word >> 4)) & 0x0f);
559 /* On success it returns ifp with increased reference count */
561 static struct inet6_ifaddr *
562 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
563 int scope, u32 flags)
565 struct inet6_ifaddr *ifa = NULL;
566 struct rt6_info *rt;
567 int hash;
568 int err = 0;
570 rcu_read_lock_bh();
571 if (idev->dead) {
572 err = -ENODEV; /*XXX*/
573 goto out2;
576 write_lock(&addrconf_hash_lock);
578 /* Ignore adding duplicate addresses on an interface */
579 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
580 ADBG(("ipv6_add_addr: already assigned\n"));
581 err = -EEXIST;
582 goto out;
585 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
587 if (ifa == NULL) {
588 ADBG(("ipv6_add_addr: malloc failed\n"));
589 err = -ENOBUFS;
590 goto out;
593 rt = addrconf_dst_alloc(idev, addr, 0);
594 if (IS_ERR(rt)) {
595 err = PTR_ERR(rt);
596 goto out;
599 ipv6_addr_copy(&ifa->addr, addr);
601 spin_lock_init(&ifa->lock);
602 init_timer(&ifa->timer);
603 ifa->timer.data = (unsigned long) ifa;
604 ifa->scope = scope;
605 ifa->prefix_len = pfxlen;
606 ifa->flags = flags | IFA_F_TENTATIVE;
607 ifa->cstamp = ifa->tstamp = jiffies;
609 ifa->rt = rt;
612 * part one of RFC 4429, section 3.3
613 * We should not configure an address as
614 * optimistic if we do not yet know the link
615 * layer address of our nexhop router
618 if (rt->rt6i_nexthop == NULL)
619 ifa->flags &= ~IFA_F_OPTIMISTIC;
621 ifa->idev = idev;
622 in6_dev_hold(idev);
623 /* For caller */
624 in6_ifa_hold(ifa);
626 /* Add to big hash table */
627 hash = ipv6_addr_hash(addr);
629 ifa->lst_next = inet6_addr_lst[hash];
630 inet6_addr_lst[hash] = ifa;
631 in6_ifa_hold(ifa);
632 write_unlock(&addrconf_hash_lock);
634 write_lock(&idev->lock);
635 /* Add to inet6_dev unicast addr list. */
636 ipv6_link_dev_addr(idev, ifa);
638 #ifdef CONFIG_IPV6_PRIVACY
639 if (ifa->flags&IFA_F_TEMPORARY) {
640 ifa->tmp_next = idev->tempaddr_list;
641 idev->tempaddr_list = ifa;
642 in6_ifa_hold(ifa);
644 #endif
646 in6_ifa_hold(ifa);
647 write_unlock(&idev->lock);
648 out2:
649 rcu_read_unlock_bh();
651 if (likely(err == 0))
652 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
653 else {
654 kfree(ifa);
655 ifa = ERR_PTR(err);
658 return ifa;
659 out:
660 write_unlock(&addrconf_hash_lock);
661 goto out2;
664 /* This function wants to get referenced ifp and releases it before return */
666 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
668 struct inet6_ifaddr *ifa, **ifap;
669 struct inet6_dev *idev = ifp->idev;
670 int hash;
671 int deleted = 0, onlink = 0;
672 unsigned long expires = jiffies;
674 hash = ipv6_addr_hash(&ifp->addr);
676 ifp->dead = 1;
678 write_lock_bh(&addrconf_hash_lock);
679 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
680 ifap = &ifa->lst_next) {
681 if (ifa == ifp) {
682 *ifap = ifa->lst_next;
683 __in6_ifa_put(ifp);
684 ifa->lst_next = NULL;
685 break;
688 write_unlock_bh(&addrconf_hash_lock);
690 write_lock_bh(&idev->lock);
691 #ifdef CONFIG_IPV6_PRIVACY
692 if (ifp->flags&IFA_F_TEMPORARY) {
693 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
694 ifap = &ifa->tmp_next) {
695 if (ifa == ifp) {
696 *ifap = ifa->tmp_next;
697 if (ifp->ifpub) {
698 in6_ifa_put(ifp->ifpub);
699 ifp->ifpub = NULL;
701 __in6_ifa_put(ifp);
702 ifa->tmp_next = NULL;
703 break;
707 #endif
709 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
710 if (ifa == ifp) {
711 *ifap = ifa->if_next;
712 __in6_ifa_put(ifp);
713 ifa->if_next = NULL;
714 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
715 break;
716 deleted = 1;
717 continue;
718 } else if (ifp->flags & IFA_F_PERMANENT) {
719 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
720 ifp->prefix_len)) {
721 if (ifa->flags & IFA_F_PERMANENT) {
722 onlink = 1;
723 if (deleted)
724 break;
725 } else {
726 unsigned long lifetime;
728 if (!onlink)
729 onlink = -1;
731 spin_lock(&ifa->lock);
733 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
735 * Note: Because this address is
736 * not permanent, lifetime <
737 * LONG_MAX / HZ here.
739 if (time_before(expires,
740 ifa->tstamp + lifetime * HZ))
741 expires = ifa->tstamp + lifetime * HZ;
742 spin_unlock(&ifa->lock);
746 ifap = &ifa->if_next;
748 write_unlock_bh(&idev->lock);
750 ipv6_ifa_notify(RTM_DELADDR, ifp);
752 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
754 addrconf_del_timer(ifp);
757 * Purge or update corresponding prefix
759 * 1) we don't purge prefix here if address was not permanent.
760 * prefix is managed by its own lifetime.
761 * 2) if there're no addresses, delete prefix.
762 * 3) if there're still other permanent address(es),
763 * corresponding prefix is still permanent.
764 * 4) otherwise, update prefix lifetime to the
765 * longest valid lifetime among the corresponding
766 * addresses on the device.
767 * Note: subsequent RA will update lifetime.
769 * --yoshfuji
771 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
772 struct in6_addr prefix;
773 struct rt6_info *rt;
774 struct net *net = dev_net(ifp->idev->dev);
775 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
776 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
778 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
779 if (onlink == 0) {
780 ip6_del_rt(rt);
781 rt = NULL;
782 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
783 rt->rt6i_expires = expires;
784 rt->rt6i_flags |= RTF_EXPIRES;
787 dst_release(&rt->u.dst);
790 in6_ifa_put(ifp);
793 #ifdef CONFIG_IPV6_PRIVACY
794 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
796 struct inet6_dev *idev = ifp->idev;
797 struct in6_addr addr, *tmpaddr;
798 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
799 unsigned long regen_advance;
800 int tmp_plen;
801 int ret = 0;
802 int max_addresses;
803 u32 addr_flags;
805 write_lock(&idev->lock);
806 if (ift) {
807 spin_lock_bh(&ift->lock);
808 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
809 spin_unlock_bh(&ift->lock);
810 tmpaddr = &addr;
811 } else {
812 tmpaddr = NULL;
814 retry:
815 in6_dev_hold(idev);
816 if (idev->cnf.use_tempaddr <= 0) {
817 write_unlock(&idev->lock);
818 printk(KERN_INFO
819 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
820 in6_dev_put(idev);
821 ret = -1;
822 goto out;
824 spin_lock_bh(&ifp->lock);
825 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
826 idev->cnf.use_tempaddr = -1; /*XXX*/
827 spin_unlock_bh(&ifp->lock);
828 write_unlock(&idev->lock);
829 printk(KERN_WARNING
830 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
831 in6_dev_put(idev);
832 ret = -1;
833 goto out;
835 in6_ifa_hold(ifp);
836 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
837 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
838 spin_unlock_bh(&ifp->lock);
839 write_unlock(&idev->lock);
840 printk(KERN_WARNING
841 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
842 in6_ifa_put(ifp);
843 in6_dev_put(idev);
844 ret = -1;
845 goto out;
847 memcpy(&addr.s6_addr[8], idev->rndid, 8);
848 tmp_valid_lft = min_t(__u32,
849 ifp->valid_lft,
850 idev->cnf.temp_valid_lft);
851 tmp_prefered_lft = min_t(__u32,
852 ifp->prefered_lft,
853 idev->cnf.temp_prefered_lft - desync_factor / HZ);
854 tmp_plen = ifp->prefix_len;
855 max_addresses = idev->cnf.max_addresses;
856 tmp_cstamp = ifp->cstamp;
857 tmp_tstamp = ifp->tstamp;
858 spin_unlock_bh(&ifp->lock);
860 regen_advance = idev->cnf.regen_max_retry *
861 idev->cnf.dad_transmits *
862 idev->nd_parms->retrans_time / HZ;
863 write_unlock(&idev->lock);
865 /* A temporary address is created only if this calculated Preferred
866 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
867 * an implementation must not create a temporary address with a zero
868 * Preferred Lifetime.
870 if (tmp_prefered_lft <= regen_advance) {
871 in6_ifa_put(ifp);
872 in6_dev_put(idev);
873 ret = -1;
874 goto out;
877 addr_flags = IFA_F_TEMPORARY;
878 /* set in addrconf_prefix_rcv() */
879 if (ifp->flags & IFA_F_OPTIMISTIC)
880 addr_flags |= IFA_F_OPTIMISTIC;
882 ift = !max_addresses ||
883 ipv6_count_addresses(idev) < max_addresses ?
884 ipv6_add_addr(idev, &addr, tmp_plen,
885 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
886 addr_flags) : NULL;
887 if (!ift || IS_ERR(ift)) {
888 in6_ifa_put(ifp);
889 in6_dev_put(idev);
890 printk(KERN_INFO
891 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
892 tmpaddr = &addr;
893 write_lock(&idev->lock);
894 goto retry;
897 spin_lock_bh(&ift->lock);
898 ift->ifpub = ifp;
899 ift->valid_lft = tmp_valid_lft;
900 ift->prefered_lft = tmp_prefered_lft;
901 ift->cstamp = tmp_cstamp;
902 ift->tstamp = tmp_tstamp;
903 spin_unlock_bh(&ift->lock);
905 addrconf_dad_start(ift, 0);
906 in6_ifa_put(ift);
907 in6_dev_put(idev);
908 out:
909 return ret;
911 #endif
914 * Choose an appropriate source address (RFC3484)
916 enum {
917 IPV6_SADDR_RULE_INIT = 0,
918 IPV6_SADDR_RULE_LOCAL,
919 IPV6_SADDR_RULE_SCOPE,
920 IPV6_SADDR_RULE_PREFERRED,
921 #ifdef CONFIG_IPV6_MIP6
922 IPV6_SADDR_RULE_HOA,
923 #endif
924 IPV6_SADDR_RULE_OIF,
925 IPV6_SADDR_RULE_LABEL,
926 #ifdef CONFIG_IPV6_PRIVACY
927 IPV6_SADDR_RULE_PRIVACY,
928 #endif
929 IPV6_SADDR_RULE_ORCHID,
930 IPV6_SADDR_RULE_PREFIX,
931 IPV6_SADDR_RULE_MAX
934 struct ipv6_saddr_score {
935 int rule;
936 int addr_type;
937 struct inet6_ifaddr *ifa;
938 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
939 int scopedist;
940 int matchlen;
943 struct ipv6_saddr_dst {
944 const struct in6_addr *addr;
945 int ifindex;
946 int scope;
947 int label;
948 unsigned int prefs;
951 static inline int ipv6_saddr_preferred(int type)
953 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
954 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
955 return 1;
956 return 0;
959 static int ipv6_get_saddr_eval(struct ipv6_saddr_score *score,
960 struct ipv6_saddr_dst *dst,
961 int i)
963 int ret;
965 if (i <= score->rule) {
966 switch (i) {
967 case IPV6_SADDR_RULE_SCOPE:
968 ret = score->scopedist;
969 break;
970 case IPV6_SADDR_RULE_PREFIX:
971 ret = score->matchlen;
972 break;
973 default:
974 ret = !!test_bit(i, score->scorebits);
976 goto out;
979 switch (i) {
980 case IPV6_SADDR_RULE_INIT:
981 /* Rule 0: remember if hiscore is not ready yet */
982 ret = !!score->ifa;
983 break;
984 case IPV6_SADDR_RULE_LOCAL:
985 /* Rule 1: Prefer same address */
986 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
987 break;
988 case IPV6_SADDR_RULE_SCOPE:
989 /* Rule 2: Prefer appropriate scope
991 * ret
993 * -1 | d 15
994 * ---+--+-+---> scope
996 * | d is scope of the destination.
997 * B-d | \
998 * | \ <- smaller scope is better if
999 * B-15 | \ if scope is enough for destinaion.
1000 * | ret = B - scope (-1 <= scope >= d <= 15).
1001 * d-C-1 | /
1002 * |/ <- greater is better
1003 * -C / if scope is not enough for destination.
1004 * /| ret = scope - C (-1 <= d < scope <= 15).
1006 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1007 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1008 * Assume B = 0 and we get C > 29.
1010 ret = __ipv6_addr_src_scope(score->addr_type);
1011 if (ret >= dst->scope)
1012 ret = -ret;
1013 else
1014 ret -= 128; /* 30 is enough */
1015 score->scopedist = ret;
1016 break;
1017 case IPV6_SADDR_RULE_PREFERRED:
1018 /* Rule 3: Avoid deprecated and optimistic addresses */
1019 ret = ipv6_saddr_preferred(score->addr_type) ||
1020 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1021 break;
1022 #ifdef CONFIG_IPV6_MIP6
1023 case IPV6_SADDR_RULE_HOA:
1025 /* Rule 4: Prefer home address */
1026 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1027 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1028 break;
1030 #endif
1031 case IPV6_SADDR_RULE_OIF:
1032 /* Rule 5: Prefer outgoing interface */
1033 ret = (!dst->ifindex ||
1034 dst->ifindex == score->ifa->idev->dev->ifindex);
1035 break;
1036 case IPV6_SADDR_RULE_LABEL:
1037 /* Rule 6: Prefer matching label */
1038 ret = ipv6_addr_label(&score->ifa->addr, score->addr_type,
1039 score->ifa->idev->dev->ifindex) == dst->label;
1040 break;
1041 #ifdef CONFIG_IPV6_PRIVACY
1042 case IPV6_SADDR_RULE_PRIVACY:
1044 /* Rule 7: Prefer public address
1045 * Note: prefer temprary address if use_tempaddr >= 2
1047 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1048 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1049 score->ifa->idev->cnf.use_tempaddr >= 2;
1050 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1051 break;
1053 #endif
1054 case IPV6_SADDR_RULE_ORCHID:
1055 /* Rule 8-: Prefer ORCHID vs ORCHID or
1056 * non-ORCHID vs non-ORCHID
1058 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1059 ipv6_addr_orchid(dst->addr));
1060 break;
1061 case IPV6_SADDR_RULE_PREFIX:
1062 /* Rule 8: Use longest matching prefix */
1063 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1064 dst->addr);
1065 break;
1066 default:
1067 ret = 0;
1070 if (ret)
1071 __set_bit(i, score->scorebits);
1072 score->rule = i;
1073 out:
1074 return ret;
1077 int ipv6_dev_get_saddr(struct net_device *dst_dev,
1078 const struct in6_addr *daddr, unsigned int prefs,
1079 struct in6_addr *saddr)
1081 struct ipv6_saddr_score scores[2],
1082 *score = &scores[0], *hiscore = &scores[1];
1083 struct net *net = dev_net(dst_dev);
1084 struct ipv6_saddr_dst dst;
1085 struct net_device *dev;
1086 int dst_type;
1088 dst_type = __ipv6_addr_type(daddr);
1089 dst.addr = daddr;
1090 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1091 dst.scope = __ipv6_addr_src_scope(dst_type);
1092 dst.label = ipv6_addr_label(daddr, dst_type, dst.ifindex);
1093 dst.prefs = prefs;
1095 hiscore->rule = -1;
1096 hiscore->ifa = NULL;
1098 read_lock(&dev_base_lock);
1099 rcu_read_lock();
1101 for_each_netdev(net, dev) {
1102 struct inet6_dev *idev;
1104 /* Candidate Source Address (section 4)
1105 * - multicast and link-local destination address,
1106 * the set of candidate source address MUST only
1107 * include addresses assigned to interfaces
1108 * belonging to the same link as the outgoing
1109 * interface.
1110 * (- For site-local destination addresses, the
1111 * set of candidate source addresses MUST only
1112 * include addresses assigned to interfaces
1113 * belonging to the same site as the outgoing
1114 * interface.)
1116 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1117 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1118 dst.ifindex && dev->ifindex != dst.ifindex)
1119 continue;
1121 idev = __in6_dev_get(dev);
1122 if (!idev)
1123 continue;
1125 read_lock_bh(&idev->lock);
1126 for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) {
1127 int i;
1130 * - Tentative Address (RFC2462 section 5.4)
1131 * - A tentative address is not considered
1132 * "assigned to an interface" in the traditional
1133 * sense, unless it is also flagged as optimistic.
1134 * - Candidate Source Address (section 4)
1135 * - In any case, anycast addresses, multicast
1136 * addresses, and the unspecified address MUST
1137 * NOT be included in a candidate set.
1139 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1140 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1141 continue;
1143 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1145 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1146 score->addr_type & IPV6_ADDR_MULTICAST)) {
1147 LIMIT_NETDEBUG(KERN_DEBUG
1148 "ADDRCONF: unspecified / multicast address "
1149 "assigned as unicast address on %s",
1150 dev->name);
1151 continue;
1154 score->rule = -1;
1155 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1157 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1158 int minihiscore, miniscore;
1160 minihiscore = ipv6_get_saddr_eval(hiscore, &dst, i);
1161 miniscore = ipv6_get_saddr_eval(score, &dst, i);
1163 if (minihiscore > miniscore) {
1164 if (i == IPV6_SADDR_RULE_SCOPE &&
1165 score->scopedist > 0) {
1167 * special case:
1168 * each remaining entry
1169 * has too small (not enough)
1170 * scope, because ifa entries
1171 * are sorted by their scope
1172 * values.
1174 goto try_nextdev;
1176 break;
1177 } else if (minihiscore < miniscore) {
1178 struct ipv6_saddr_score *tmp;
1180 if (hiscore->ifa)
1181 in6_ifa_put(hiscore->ifa);
1183 in6_ifa_hold(score->ifa);
1185 tmp = hiscore;
1186 hiscore = score;
1187 score = tmp;
1189 /* restore our iterator */
1190 score->ifa = hiscore->ifa;
1192 break;
1196 try_nextdev:
1197 read_unlock_bh(&idev->lock);
1199 rcu_read_unlock();
1200 read_unlock(&dev_base_lock);
1202 if (!hiscore->ifa)
1203 return -EADDRNOTAVAIL;
1205 ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1206 in6_ifa_put(hiscore->ifa);
1207 return 0;
1210 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1212 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1213 unsigned char banned_flags)
1215 struct inet6_dev *idev;
1216 int err = -EADDRNOTAVAIL;
1218 rcu_read_lock();
1219 if ((idev = __in6_dev_get(dev)) != NULL) {
1220 struct inet6_ifaddr *ifp;
1222 read_lock_bh(&idev->lock);
1223 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1224 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
1225 ipv6_addr_copy(addr, &ifp->addr);
1226 err = 0;
1227 break;
1230 read_unlock_bh(&idev->lock);
1232 rcu_read_unlock();
1233 return err;
1236 static int ipv6_count_addresses(struct inet6_dev *idev)
1238 int cnt = 0;
1239 struct inet6_ifaddr *ifp;
1241 read_lock_bh(&idev->lock);
1242 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1243 cnt++;
1244 read_unlock_bh(&idev->lock);
1245 return cnt;
1248 int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1249 struct net_device *dev, int strict)
1251 struct inet6_ifaddr * ifp;
1252 u8 hash = ipv6_addr_hash(addr);
1254 read_lock_bh(&addrconf_hash_lock);
1255 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1256 if (!net_eq(dev_net(ifp->idev->dev), net))
1257 continue;
1258 if (ipv6_addr_equal(&ifp->addr, addr) &&
1259 !(ifp->flags&IFA_F_TENTATIVE)) {
1260 if (dev == NULL || ifp->idev->dev == dev ||
1261 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1262 break;
1265 read_unlock_bh(&addrconf_hash_lock);
1266 return ifp != NULL;
1268 EXPORT_SYMBOL(ipv6_chk_addr);
1270 static
1271 int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1272 struct net_device *dev)
1274 struct inet6_ifaddr * ifp;
1275 u8 hash = ipv6_addr_hash(addr);
1277 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1278 if (!net_eq(dev_net(ifp->idev->dev), net))
1279 continue;
1280 if (ipv6_addr_equal(&ifp->addr, addr)) {
1281 if (dev == NULL || ifp->idev->dev == dev)
1282 break;
1285 return ifp != NULL;
1288 int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev)
1290 struct inet6_dev *idev;
1291 struct inet6_ifaddr *ifa;
1292 int onlink;
1294 onlink = 0;
1295 rcu_read_lock();
1296 idev = __in6_dev_get(dev);
1297 if (idev) {
1298 read_lock_bh(&idev->lock);
1299 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
1300 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1301 ifa->prefix_len);
1302 if (onlink)
1303 break;
1305 read_unlock_bh(&idev->lock);
1307 rcu_read_unlock();
1308 return onlink;
1311 EXPORT_SYMBOL(ipv6_chk_prefix);
1313 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1314 struct net_device *dev, int strict)
1316 struct inet6_ifaddr * ifp;
1317 u8 hash = ipv6_addr_hash(addr);
1319 read_lock_bh(&addrconf_hash_lock);
1320 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1321 if (!net_eq(dev_net(ifp->idev->dev), net))
1322 continue;
1323 if (ipv6_addr_equal(&ifp->addr, addr)) {
1324 if (dev == NULL || ifp->idev->dev == dev ||
1325 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1326 in6_ifa_hold(ifp);
1327 break;
1331 read_unlock_bh(&addrconf_hash_lock);
1333 return ifp;
1336 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1338 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1339 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1340 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1341 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1342 int sk_ipv6only = ipv6_only_sock(sk);
1343 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1344 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1345 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1347 if (!sk2_rcv_saddr && !sk_ipv6only)
1348 return 1;
1350 if (addr_type2 == IPV6_ADDR_ANY &&
1351 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1352 return 1;
1354 if (addr_type == IPV6_ADDR_ANY &&
1355 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1356 return 1;
1358 if (sk2_rcv_saddr6 &&
1359 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1360 return 1;
1362 if (addr_type == IPV6_ADDR_MAPPED &&
1363 !sk2_ipv6only &&
1364 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1365 return 1;
1367 return 0;
1370 /* Gets referenced address, destroys ifaddr */
1372 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1374 if (ifp->flags&IFA_F_PERMANENT) {
1375 spin_lock_bh(&ifp->lock);
1376 addrconf_del_timer(ifp);
1377 ifp->flags |= IFA_F_TENTATIVE;
1378 spin_unlock_bh(&ifp->lock);
1379 in6_ifa_put(ifp);
1380 #ifdef CONFIG_IPV6_PRIVACY
1381 } else if (ifp->flags&IFA_F_TEMPORARY) {
1382 struct inet6_ifaddr *ifpub;
1383 spin_lock_bh(&ifp->lock);
1384 ifpub = ifp->ifpub;
1385 if (ifpub) {
1386 in6_ifa_hold(ifpub);
1387 spin_unlock_bh(&ifp->lock);
1388 ipv6_create_tempaddr(ifpub, ifp);
1389 in6_ifa_put(ifpub);
1390 } else {
1391 spin_unlock_bh(&ifp->lock);
1393 ipv6_del_addr(ifp);
1394 #endif
1395 } else
1396 ipv6_del_addr(ifp);
1399 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1401 if (net_ratelimit())
1402 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1403 addrconf_dad_stop(ifp);
1406 /* Join to solicited addr multicast group. */
1408 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1410 struct in6_addr maddr;
1412 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1413 return;
1415 addrconf_addr_solict_mult(addr, &maddr);
1416 ipv6_dev_mc_inc(dev, &maddr);
1419 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1421 struct in6_addr maddr;
1423 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1424 return;
1426 addrconf_addr_solict_mult(addr, &maddr);
1427 __ipv6_dev_mc_dec(idev, &maddr);
1430 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1432 struct in6_addr addr;
1433 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1434 if (ipv6_addr_any(&addr))
1435 return;
1436 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1439 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1441 struct in6_addr addr;
1442 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1443 if (ipv6_addr_any(&addr))
1444 return;
1445 __ipv6_dev_ac_dec(ifp->idev, &addr);
1448 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1450 if (dev->addr_len != ETH_ALEN)
1451 return -1;
1452 memcpy(eui, dev->dev_addr, 3);
1453 memcpy(eui + 5, dev->dev_addr + 3, 3);
1456 * The zSeries OSA network cards can be shared among various
1457 * OS instances, but the OSA cards have only one MAC address.
1458 * This leads to duplicate address conflicts in conjunction
1459 * with IPv6 if more than one instance uses the same card.
1461 * The driver for these cards can deliver a unique 16-bit
1462 * identifier for each instance sharing the same card. It is
1463 * placed instead of 0xFFFE in the interface identifier. The
1464 * "u" bit of the interface identifier is not inverted in this
1465 * case. Hence the resulting interface identifier has local
1466 * scope according to RFC2373.
1468 if (dev->dev_id) {
1469 eui[3] = (dev->dev_id >> 8) & 0xFF;
1470 eui[4] = dev->dev_id & 0xFF;
1471 } else {
1472 eui[3] = 0xFF;
1473 eui[4] = 0xFE;
1474 eui[0] ^= 2;
1476 return 0;
1479 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1481 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1482 if (dev->addr_len != ARCNET_ALEN)
1483 return -1;
1484 memset(eui, 0, 7);
1485 eui[7] = *(u8*)dev->dev_addr;
1486 return 0;
1489 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1491 if (dev->addr_len != INFINIBAND_ALEN)
1492 return -1;
1493 memcpy(eui, dev->dev_addr + 12, 8);
1494 eui[0] |= 2;
1495 return 0;
1498 int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1500 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1501 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1502 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1503 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1504 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1505 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1506 eui[1] = 0;
1507 eui[2] = 0x5E;
1508 eui[3] = 0xFE;
1509 memcpy(eui + 4, &addr, 4);
1510 return 0;
1512 EXPORT_SYMBOL(__ipv6_isatap_ifid);
1514 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1516 if (dev->priv_flags & IFF_ISATAP)
1517 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1518 return -1;
1521 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1523 switch (dev->type) {
1524 case ARPHRD_ETHER:
1525 case ARPHRD_FDDI:
1526 case ARPHRD_IEEE802_TR:
1527 return addrconf_ifid_eui48(eui, dev);
1528 case ARPHRD_ARCNET:
1529 return addrconf_ifid_arcnet(eui, dev);
1530 case ARPHRD_INFINIBAND:
1531 return addrconf_ifid_infiniband(eui, dev);
1532 case ARPHRD_SIT:
1533 return addrconf_ifid_sit(eui, dev);
1535 return -1;
1538 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1540 int err = -1;
1541 struct inet6_ifaddr *ifp;
1543 read_lock_bh(&idev->lock);
1544 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1545 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1546 memcpy(eui, ifp->addr.s6_addr+8, 8);
1547 err = 0;
1548 break;
1551 read_unlock_bh(&idev->lock);
1552 return err;
1555 #ifdef CONFIG_IPV6_PRIVACY
1556 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1557 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1559 regen:
1560 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1561 idev->rndid[0] &= ~0x02;
1564 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1565 * check if generated address is not inappropriate
1567 * - Reserved subnet anycast (RFC 2526)
1568 * 11111101 11....11 1xxxxxxx
1569 * - ISATAP (RFC4214) 6.1
1570 * 00-00-5E-FE-xx-xx-xx-xx
1571 * - value 0
1572 * - XXX: already assigned to an address on the device
1574 if (idev->rndid[0] == 0xfd &&
1575 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1576 (idev->rndid[7]&0x80))
1577 goto regen;
1578 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1579 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1580 goto regen;
1581 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1582 goto regen;
1585 return 0;
1588 static void ipv6_regen_rndid(unsigned long data)
1590 struct inet6_dev *idev = (struct inet6_dev *) data;
1591 unsigned long expires;
1593 rcu_read_lock_bh();
1594 write_lock_bh(&idev->lock);
1596 if (idev->dead)
1597 goto out;
1599 if (__ipv6_regen_rndid(idev) < 0)
1600 goto out;
1602 expires = jiffies +
1603 idev->cnf.temp_prefered_lft * HZ -
1604 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1605 if (time_before(expires, jiffies)) {
1606 printk(KERN_WARNING
1607 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1608 idev->dev->name);
1609 goto out;
1612 if (!mod_timer(&idev->regen_timer, expires))
1613 in6_dev_hold(idev);
1615 out:
1616 write_unlock_bh(&idev->lock);
1617 rcu_read_unlock_bh();
1618 in6_dev_put(idev);
1621 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1622 int ret = 0;
1624 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1625 ret = __ipv6_regen_rndid(idev);
1626 return ret;
1628 #endif
1631 * Add prefix route.
1634 static void
1635 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1636 unsigned long expires, u32 flags)
1638 struct fib6_config cfg = {
1639 .fc_table = RT6_TABLE_PREFIX,
1640 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1641 .fc_ifindex = dev->ifindex,
1642 .fc_expires = expires,
1643 .fc_dst_len = plen,
1644 .fc_flags = RTF_UP | flags,
1645 .fc_nlinfo.nl_net = dev_net(dev),
1648 ipv6_addr_copy(&cfg.fc_dst, pfx);
1650 /* Prevent useless cloning on PtP SIT.
1651 This thing is done here expecting that the whole
1652 class of non-broadcast devices need not cloning.
1654 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1655 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1656 cfg.fc_flags |= RTF_NONEXTHOP;
1657 #endif
1659 ip6_route_add(&cfg);
1662 /* Create "default" multicast route to the interface */
1664 static void addrconf_add_mroute(struct net_device *dev)
1666 struct fib6_config cfg = {
1667 .fc_table = RT6_TABLE_LOCAL,
1668 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1669 .fc_ifindex = dev->ifindex,
1670 .fc_dst_len = 8,
1671 .fc_flags = RTF_UP,
1672 .fc_nlinfo.nl_net = dev_net(dev),
1675 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1677 ip6_route_add(&cfg);
1680 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1681 static void sit_route_add(struct net_device *dev)
1683 struct fib6_config cfg = {
1684 .fc_table = RT6_TABLE_MAIN,
1685 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1686 .fc_ifindex = dev->ifindex,
1687 .fc_dst_len = 96,
1688 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1689 .fc_nlinfo.nl_net = dev_net(dev),
1692 /* prefix length - 96 bits "::d.d.d.d" */
1693 ip6_route_add(&cfg);
1695 #endif
1697 static void addrconf_add_lroute(struct net_device *dev)
1699 struct in6_addr addr;
1701 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1702 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1705 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1707 struct inet6_dev *idev;
1709 ASSERT_RTNL();
1711 if ((idev = ipv6_find_idev(dev)) == NULL)
1712 return NULL;
1714 /* Add default multicast route */
1715 addrconf_add_mroute(dev);
1717 /* Add link local route */
1718 addrconf_add_lroute(dev);
1719 return idev;
1722 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1724 struct prefix_info *pinfo;
1725 __u32 valid_lft;
1726 __u32 prefered_lft;
1727 int addr_type;
1728 struct inet6_dev *in6_dev;
1730 pinfo = (struct prefix_info *) opt;
1732 if (len < sizeof(struct prefix_info)) {
1733 ADBG(("addrconf: prefix option too short\n"));
1734 return;
1738 * Validation checks ([ADDRCONF], page 19)
1741 addr_type = ipv6_addr_type(&pinfo->prefix);
1743 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1744 return;
1746 valid_lft = ntohl(pinfo->valid);
1747 prefered_lft = ntohl(pinfo->prefered);
1749 if (prefered_lft > valid_lft) {
1750 if (net_ratelimit())
1751 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1752 return;
1755 in6_dev = in6_dev_get(dev);
1757 if (in6_dev == NULL) {
1758 if (net_ratelimit())
1759 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1760 return;
1764 * Two things going on here:
1765 * 1) Add routes for on-link prefixes
1766 * 2) Configure prefixes with the auto flag set
1769 if (pinfo->onlink) {
1770 struct rt6_info *rt;
1771 unsigned long rt_expires;
1773 /* Avoid arithmetic overflow. Really, we could
1774 * save rt_expires in seconds, likely valid_lft,
1775 * but it would require division in fib gc, that it
1776 * not good.
1778 if (HZ > USER_HZ)
1779 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1780 else
1781 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
1783 if (addrconf_finite_timeout(rt_expires))
1784 rt_expires *= HZ;
1786 rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
1787 dev->ifindex, 1);
1789 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1790 /* Autoconf prefix route */
1791 if (valid_lft == 0) {
1792 ip6_del_rt(rt);
1793 rt = NULL;
1794 } else if (addrconf_finite_timeout(rt_expires)) {
1795 /* not infinity */
1796 rt->rt6i_expires = jiffies + rt_expires;
1797 rt->rt6i_flags |= RTF_EXPIRES;
1798 } else {
1799 rt->rt6i_flags &= ~RTF_EXPIRES;
1800 rt->rt6i_expires = 0;
1802 } else if (valid_lft) {
1803 clock_t expires = 0;
1804 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1805 if (addrconf_finite_timeout(rt_expires)) {
1806 /* not infinity */
1807 flags |= RTF_EXPIRES;
1808 expires = jiffies_to_clock_t(rt_expires);
1810 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1811 dev, expires, flags);
1813 if (rt)
1814 dst_release(&rt->u.dst);
1817 /* Try to figure out our local address for this prefix */
1819 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1820 struct inet6_ifaddr * ifp;
1821 struct in6_addr addr;
1822 int create = 0, update_lft = 0;
1824 if (pinfo->prefix_len == 64) {
1825 memcpy(&addr, &pinfo->prefix, 8);
1826 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1827 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1828 in6_dev_put(in6_dev);
1829 return;
1831 goto ok;
1833 if (net_ratelimit())
1834 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1835 pinfo->prefix_len);
1836 in6_dev_put(in6_dev);
1837 return;
1841 ifp = ipv6_get_ifaddr(dev_net(dev), &addr, dev, 1);
1843 if (ifp == NULL && valid_lft) {
1844 int max_addresses = in6_dev->cnf.max_addresses;
1845 u32 addr_flags = 0;
1847 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1848 if (in6_dev->cnf.optimistic_dad &&
1849 !ipv6_devconf.forwarding)
1850 addr_flags = IFA_F_OPTIMISTIC;
1851 #endif
1853 /* Do not allow to create too much of autoconfigured
1854 * addresses; this would be too easy way to crash kernel.
1856 if (!max_addresses ||
1857 ipv6_count_addresses(in6_dev) < max_addresses)
1858 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1859 addr_type&IPV6_ADDR_SCOPE_MASK,
1860 addr_flags);
1862 if (!ifp || IS_ERR(ifp)) {
1863 in6_dev_put(in6_dev);
1864 return;
1867 update_lft = create = 1;
1868 ifp->cstamp = jiffies;
1869 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1872 if (ifp) {
1873 int flags;
1874 unsigned long now;
1875 #ifdef CONFIG_IPV6_PRIVACY
1876 struct inet6_ifaddr *ift;
1877 #endif
1878 u32 stored_lft;
1880 /* update lifetime (RFC2462 5.5.3 e) */
1881 spin_lock(&ifp->lock);
1882 now = jiffies;
1883 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1884 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1885 else
1886 stored_lft = 0;
1887 if (!update_lft && stored_lft) {
1888 if (valid_lft > MIN_VALID_LIFETIME ||
1889 valid_lft > stored_lft)
1890 update_lft = 1;
1891 else if (stored_lft <= MIN_VALID_LIFETIME) {
1892 /* valid_lft <= stored_lft is always true */
1893 /* XXX: IPsec */
1894 update_lft = 0;
1895 } else {
1896 valid_lft = MIN_VALID_LIFETIME;
1897 if (valid_lft < prefered_lft)
1898 prefered_lft = valid_lft;
1899 update_lft = 1;
1903 if (update_lft) {
1904 ifp->valid_lft = valid_lft;
1905 ifp->prefered_lft = prefered_lft;
1906 ifp->tstamp = now;
1907 flags = ifp->flags;
1908 ifp->flags &= ~IFA_F_DEPRECATED;
1909 spin_unlock(&ifp->lock);
1911 if (!(flags&IFA_F_TENTATIVE))
1912 ipv6_ifa_notify(0, ifp);
1913 } else
1914 spin_unlock(&ifp->lock);
1916 #ifdef CONFIG_IPV6_PRIVACY
1917 read_lock_bh(&in6_dev->lock);
1918 /* update all temporary addresses in the list */
1919 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1921 * When adjusting the lifetimes of an existing
1922 * temporary address, only lower the lifetimes.
1923 * Implementations must not increase the
1924 * lifetimes of an existing temporary address
1925 * when processing a Prefix Information Option.
1927 if (ifp != ift->ifpub)
1928 continue;
1930 spin_lock(&ift->lock);
1931 flags = ift->flags;
1932 if (ift->valid_lft > valid_lft &&
1933 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1934 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1935 if (ift->prefered_lft > prefered_lft &&
1936 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1937 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1938 spin_unlock(&ift->lock);
1939 if (!(flags&IFA_F_TENTATIVE))
1940 ipv6_ifa_notify(0, ift);
1943 if (create && in6_dev->cnf.use_tempaddr > 0) {
1945 * When a new public address is created as described in [ADDRCONF],
1946 * also create a new temporary address.
1948 read_unlock_bh(&in6_dev->lock);
1949 ipv6_create_tempaddr(ifp, NULL);
1950 } else {
1951 read_unlock_bh(&in6_dev->lock);
1953 #endif
1954 in6_ifa_put(ifp);
1955 addrconf_verify(0);
1958 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1959 in6_dev_put(in6_dev);
1963 * Set destination address.
1964 * Special case for SIT interfaces where we create a new "virtual"
1965 * device.
1967 int addrconf_set_dstaddr(struct net *net, void __user *arg)
1969 struct in6_ifreq ireq;
1970 struct net_device *dev;
1971 int err = -EINVAL;
1973 rtnl_lock();
1975 err = -EFAULT;
1976 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1977 goto err_exit;
1979 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
1981 err = -ENODEV;
1982 if (dev == NULL)
1983 goto err_exit;
1985 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1986 if (dev->type == ARPHRD_SIT) {
1987 struct ifreq ifr;
1988 mm_segment_t oldfs;
1989 struct ip_tunnel_parm p;
1991 err = -EADDRNOTAVAIL;
1992 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1993 goto err_exit;
1995 memset(&p, 0, sizeof(p));
1996 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1997 p.iph.saddr = 0;
1998 p.iph.version = 4;
1999 p.iph.ihl = 5;
2000 p.iph.protocol = IPPROTO_IPV6;
2001 p.iph.ttl = 64;
2002 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2004 oldfs = get_fs(); set_fs(KERNEL_DS);
2005 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2006 set_fs(oldfs);
2008 if (err == 0) {
2009 err = -ENOBUFS;
2010 dev = __dev_get_by_name(net, p.name);
2011 if (!dev)
2012 goto err_exit;
2013 err = dev_open(dev);
2016 #endif
2018 err_exit:
2019 rtnl_unlock();
2020 return err;
2024 * Manual configuration of address on an interface
2026 static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
2027 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2028 __u32 valid_lft)
2030 struct inet6_ifaddr *ifp;
2031 struct inet6_dev *idev;
2032 struct net_device *dev;
2033 int scope;
2034 u32 flags;
2035 clock_t expires;
2036 unsigned long timeout;
2038 ASSERT_RTNL();
2040 if (plen > 128)
2041 return -EINVAL;
2043 /* check the lifetime */
2044 if (!valid_lft || prefered_lft > valid_lft)
2045 return -EINVAL;
2047 dev = __dev_get_by_index(net, ifindex);
2048 if (!dev)
2049 return -ENODEV;
2051 if ((idev = addrconf_add_dev(dev)) == NULL)
2052 return -ENOBUFS;
2054 scope = ipv6_addr_scope(pfx);
2056 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2057 if (addrconf_finite_timeout(timeout)) {
2058 expires = jiffies_to_clock_t(timeout * HZ);
2059 valid_lft = timeout;
2060 flags = RTF_EXPIRES;
2061 } else {
2062 expires = 0;
2063 flags = 0;
2064 ifa_flags |= IFA_F_PERMANENT;
2067 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2068 if (addrconf_finite_timeout(timeout)) {
2069 if (timeout == 0)
2070 ifa_flags |= IFA_F_DEPRECATED;
2071 prefered_lft = timeout;
2074 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2076 if (!IS_ERR(ifp)) {
2077 spin_lock_bh(&ifp->lock);
2078 ifp->valid_lft = valid_lft;
2079 ifp->prefered_lft = prefered_lft;
2080 ifp->tstamp = jiffies;
2081 spin_unlock_bh(&ifp->lock);
2083 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2084 expires, flags);
2086 * Note that section 3.1 of RFC 4429 indicates
2087 * that the Optimistic flag should not be set for
2088 * manually configured addresses
2090 addrconf_dad_start(ifp, 0);
2091 in6_ifa_put(ifp);
2092 addrconf_verify(0);
2093 return 0;
2096 return PTR_ERR(ifp);
2099 static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
2100 unsigned int plen)
2102 struct inet6_ifaddr *ifp;
2103 struct inet6_dev *idev;
2104 struct net_device *dev;
2106 if (plen > 128)
2107 return -EINVAL;
2109 dev = __dev_get_by_index(net, ifindex);
2110 if (!dev)
2111 return -ENODEV;
2113 if ((idev = __in6_dev_get(dev)) == NULL)
2114 return -ENXIO;
2116 read_lock_bh(&idev->lock);
2117 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
2118 if (ifp->prefix_len == plen &&
2119 ipv6_addr_equal(pfx, &ifp->addr)) {
2120 in6_ifa_hold(ifp);
2121 read_unlock_bh(&idev->lock);
2123 ipv6_del_addr(ifp);
2125 /* If the last address is deleted administratively,
2126 disable IPv6 on this interface.
2128 if (idev->addr_list == NULL)
2129 addrconf_ifdown(idev->dev, 1);
2130 return 0;
2133 read_unlock_bh(&idev->lock);
2134 return -EADDRNOTAVAIL;
2138 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2140 struct in6_ifreq ireq;
2141 int err;
2143 if (!capable(CAP_NET_ADMIN))
2144 return -EPERM;
2146 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2147 return -EFAULT;
2149 rtnl_lock();
2150 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2151 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2152 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2153 rtnl_unlock();
2154 return err;
2157 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2159 struct in6_ifreq ireq;
2160 int err;
2162 if (!capable(CAP_NET_ADMIN))
2163 return -EPERM;
2165 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2166 return -EFAULT;
2168 rtnl_lock();
2169 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2170 ireq.ifr6_prefixlen);
2171 rtnl_unlock();
2172 return err;
2175 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2176 static void sit_add_v4_addrs(struct inet6_dev *idev)
2178 struct inet6_ifaddr * ifp;
2179 struct in6_addr addr;
2180 struct net_device *dev;
2181 struct net *net = dev_net(idev->dev);
2182 int scope;
2184 ASSERT_RTNL();
2186 memset(&addr, 0, sizeof(struct in6_addr));
2187 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2189 if (idev->dev->flags&IFF_POINTOPOINT) {
2190 addr.s6_addr32[0] = htonl(0xfe800000);
2191 scope = IFA_LINK;
2192 } else {
2193 scope = IPV6_ADDR_COMPATv4;
2196 if (addr.s6_addr32[3]) {
2197 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2198 if (!IS_ERR(ifp)) {
2199 spin_lock_bh(&ifp->lock);
2200 ifp->flags &= ~IFA_F_TENTATIVE;
2201 spin_unlock_bh(&ifp->lock);
2202 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2203 in6_ifa_put(ifp);
2205 return;
2208 for_each_netdev(net, dev) {
2209 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2210 if (in_dev && (dev->flags & IFF_UP)) {
2211 struct in_ifaddr * ifa;
2213 int flag = scope;
2215 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2216 int plen;
2218 addr.s6_addr32[3] = ifa->ifa_local;
2220 if (ifa->ifa_scope == RT_SCOPE_LINK)
2221 continue;
2222 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2223 if (idev->dev->flags&IFF_POINTOPOINT)
2224 continue;
2225 flag |= IFA_HOST;
2227 if (idev->dev->flags&IFF_POINTOPOINT)
2228 plen = 64;
2229 else
2230 plen = 96;
2232 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2233 IFA_F_PERMANENT);
2234 if (!IS_ERR(ifp)) {
2235 spin_lock_bh(&ifp->lock);
2236 ifp->flags &= ~IFA_F_TENTATIVE;
2237 spin_unlock_bh(&ifp->lock);
2238 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2239 in6_ifa_put(ifp);
2245 #endif
2247 static void init_loopback(struct net_device *dev)
2249 struct inet6_dev *idev;
2250 struct inet6_ifaddr * ifp;
2252 /* ::1 */
2254 ASSERT_RTNL();
2256 if ((idev = ipv6_find_idev(dev)) == NULL) {
2257 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2258 return;
2261 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2262 if (!IS_ERR(ifp)) {
2263 spin_lock_bh(&ifp->lock);
2264 ifp->flags &= ~IFA_F_TENTATIVE;
2265 spin_unlock_bh(&ifp->lock);
2266 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2267 in6_ifa_put(ifp);
2271 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2273 struct inet6_ifaddr * ifp;
2274 u32 addr_flags = IFA_F_PERMANENT;
2276 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2277 if (idev->cnf.optimistic_dad &&
2278 !ipv6_devconf.forwarding)
2279 addr_flags |= IFA_F_OPTIMISTIC;
2280 #endif
2283 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2284 if (!IS_ERR(ifp)) {
2285 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2286 addrconf_dad_start(ifp, 0);
2287 in6_ifa_put(ifp);
2291 static void addrconf_dev_config(struct net_device *dev)
2293 struct in6_addr addr;
2294 struct inet6_dev * idev;
2296 ASSERT_RTNL();
2298 if ((dev->type != ARPHRD_ETHER) &&
2299 (dev->type != ARPHRD_FDDI) &&
2300 (dev->type != ARPHRD_IEEE802_TR) &&
2301 (dev->type != ARPHRD_ARCNET) &&
2302 (dev->type != ARPHRD_INFINIBAND)) {
2303 /* Alas, we support only Ethernet autoconfiguration. */
2304 return;
2307 idev = addrconf_add_dev(dev);
2308 if (idev == NULL)
2309 return;
2311 memset(&addr, 0, sizeof(struct in6_addr));
2312 addr.s6_addr32[0] = htonl(0xFE800000);
2314 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2315 addrconf_add_linklocal(idev, &addr);
2318 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2319 static void addrconf_sit_config(struct net_device *dev)
2321 struct inet6_dev *idev;
2323 ASSERT_RTNL();
2326 * Configure the tunnel with one of our IPv4
2327 * addresses... we should configure all of
2328 * our v4 addrs in the tunnel
2331 if ((idev = ipv6_find_idev(dev)) == NULL) {
2332 printk(KERN_DEBUG "init sit: add_dev failed\n");
2333 return;
2336 if (dev->priv_flags & IFF_ISATAP) {
2337 struct in6_addr addr;
2339 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2340 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2341 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2342 addrconf_add_linklocal(idev, &addr);
2343 return;
2346 sit_add_v4_addrs(idev);
2348 if (dev->flags&IFF_POINTOPOINT) {
2349 addrconf_add_mroute(dev);
2350 addrconf_add_lroute(dev);
2351 } else
2352 sit_route_add(dev);
2354 #endif
2356 static inline int
2357 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2359 struct in6_addr lladdr;
2361 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2362 addrconf_add_linklocal(idev, &lladdr);
2363 return 0;
2365 return -1;
2368 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2370 struct net_device *link_dev;
2371 struct net *net = dev_net(idev->dev);
2373 /* first try to inherit the link-local address from the link device */
2374 if (idev->dev->iflink &&
2375 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2376 if (!ipv6_inherit_linklocal(idev, link_dev))
2377 return;
2379 /* then try to inherit it from any device */
2380 for_each_netdev(net, link_dev) {
2381 if (!ipv6_inherit_linklocal(idev, link_dev))
2382 return;
2384 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2388 * Autoconfigure tunnel with a link-local address so routing protocols,
2389 * DHCPv6, MLD etc. can be run over the virtual link
2392 static void addrconf_ip6_tnl_config(struct net_device *dev)
2394 struct inet6_dev *idev;
2396 ASSERT_RTNL();
2398 if ((idev = addrconf_add_dev(dev)) == NULL) {
2399 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2400 return;
2402 ip6_tnl_add_linklocal(idev);
2405 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2406 void * data)
2408 struct net_device *dev = (struct net_device *) data;
2409 struct inet6_dev *idev = __in6_dev_get(dev);
2410 int run_pending = 0;
2411 int err;
2413 switch(event) {
2414 case NETDEV_REGISTER:
2415 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2416 idev = ipv6_add_dev(dev);
2417 if (!idev)
2418 return notifier_from_errno(-ENOMEM);
2420 break;
2421 case NETDEV_UP:
2422 case NETDEV_CHANGE:
2423 if (dev->flags & IFF_SLAVE)
2424 break;
2426 if (event == NETDEV_UP) {
2427 if (!addrconf_qdisc_ok(dev)) {
2428 /* device is not ready yet. */
2429 printk(KERN_INFO
2430 "ADDRCONF(NETDEV_UP): %s: "
2431 "link is not ready\n",
2432 dev->name);
2433 break;
2436 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2437 idev = ipv6_add_dev(dev);
2439 if (idev)
2440 idev->if_flags |= IF_READY;
2441 } else {
2442 if (!addrconf_qdisc_ok(dev)) {
2443 /* device is still not ready. */
2444 break;
2447 if (idev) {
2448 if (idev->if_flags & IF_READY) {
2449 /* device is already configured. */
2450 break;
2452 idev->if_flags |= IF_READY;
2455 printk(KERN_INFO
2456 "ADDRCONF(NETDEV_CHANGE): %s: "
2457 "link becomes ready\n",
2458 dev->name);
2460 run_pending = 1;
2463 switch(dev->type) {
2464 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2465 case ARPHRD_SIT:
2466 addrconf_sit_config(dev);
2467 break;
2468 #endif
2469 case ARPHRD_TUNNEL6:
2470 addrconf_ip6_tnl_config(dev);
2471 break;
2472 case ARPHRD_LOOPBACK:
2473 init_loopback(dev);
2474 break;
2476 default:
2477 addrconf_dev_config(dev);
2478 break;
2480 if (idev) {
2481 if (run_pending)
2482 addrconf_dad_run(idev);
2484 /* If the MTU changed during the interface down, when the
2485 interface up, the changed MTU must be reflected in the
2486 idev as well as routers.
2488 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2489 rt6_mtu_change(dev, dev->mtu);
2490 idev->cnf.mtu6 = dev->mtu;
2492 idev->tstamp = jiffies;
2493 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2494 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2495 stop IPv6 on this interface.
2497 if (dev->mtu < IPV6_MIN_MTU)
2498 addrconf_ifdown(dev, event != NETDEV_DOWN);
2500 break;
2502 case NETDEV_CHANGEMTU:
2503 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2504 rt6_mtu_change(dev, dev->mtu);
2505 idev->cnf.mtu6 = dev->mtu;
2506 break;
2509 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2510 idev = ipv6_add_dev(dev);
2511 if (idev)
2512 break;
2515 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2517 case NETDEV_DOWN:
2518 case NETDEV_UNREGISTER:
2520 * Remove all addresses from this interface.
2522 addrconf_ifdown(dev, event != NETDEV_DOWN);
2523 break;
2525 case NETDEV_CHANGENAME:
2526 if (idev) {
2527 snmp6_unregister_dev(idev);
2528 addrconf_sysctl_unregister(idev);
2529 addrconf_sysctl_register(idev);
2530 err = snmp6_register_dev(idev);
2531 if (err)
2532 return notifier_from_errno(err);
2534 break;
2537 return NOTIFY_OK;
2541 * addrconf module should be notified of a device going up
2543 static struct notifier_block ipv6_dev_notf = {
2544 .notifier_call = addrconf_notify,
2545 .priority = 0
2548 static int addrconf_ifdown(struct net_device *dev, int how)
2550 struct inet6_dev *idev;
2551 struct inet6_ifaddr *ifa, **bifa;
2552 struct net *net = dev_net(dev);
2553 int i;
2555 ASSERT_RTNL();
2557 if ((dev->flags & IFF_LOOPBACK) && how == 1)
2558 how = 0;
2560 rt6_ifdown(net, dev);
2561 neigh_ifdown(&nd_tbl, dev);
2563 idev = __in6_dev_get(dev);
2564 if (idev == NULL)
2565 return -ENODEV;
2567 /* Step 1: remove reference to ipv6 device from parent device.
2568 Do not dev_put!
2570 if (how) {
2571 idev->dead = 1;
2573 /* protected by rtnl_lock */
2574 rcu_assign_pointer(dev->ip6_ptr, NULL);
2576 /* Step 1.5: remove snmp6 entry */
2577 snmp6_unregister_dev(idev);
2581 /* Step 2: clear hash table */
2582 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2583 bifa = &inet6_addr_lst[i];
2585 write_lock_bh(&addrconf_hash_lock);
2586 while ((ifa = *bifa) != NULL) {
2587 if (ifa->idev == idev) {
2588 *bifa = ifa->lst_next;
2589 ifa->lst_next = NULL;
2590 addrconf_del_timer(ifa);
2591 in6_ifa_put(ifa);
2592 continue;
2594 bifa = &ifa->lst_next;
2596 write_unlock_bh(&addrconf_hash_lock);
2599 write_lock_bh(&idev->lock);
2601 /* Step 3: clear flags for stateless addrconf */
2602 if (!how)
2603 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2605 /* Step 4: clear address list */
2606 #ifdef CONFIG_IPV6_PRIVACY
2607 if (how && del_timer(&idev->regen_timer))
2608 in6_dev_put(idev);
2610 /* clear tempaddr list */
2611 while ((ifa = idev->tempaddr_list) != NULL) {
2612 idev->tempaddr_list = ifa->tmp_next;
2613 ifa->tmp_next = NULL;
2614 ifa->dead = 1;
2615 write_unlock_bh(&idev->lock);
2616 spin_lock_bh(&ifa->lock);
2618 if (ifa->ifpub) {
2619 in6_ifa_put(ifa->ifpub);
2620 ifa->ifpub = NULL;
2622 spin_unlock_bh(&ifa->lock);
2623 in6_ifa_put(ifa);
2624 write_lock_bh(&idev->lock);
2626 #endif
2627 while ((ifa = idev->addr_list) != NULL) {
2628 idev->addr_list = ifa->if_next;
2629 ifa->if_next = NULL;
2630 ifa->dead = 1;
2631 addrconf_del_timer(ifa);
2632 write_unlock_bh(&idev->lock);
2634 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2635 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
2636 in6_ifa_put(ifa);
2638 write_lock_bh(&idev->lock);
2640 write_unlock_bh(&idev->lock);
2642 /* Step 5: Discard multicast list */
2644 if (how)
2645 ipv6_mc_destroy_dev(idev);
2646 else
2647 ipv6_mc_down(idev);
2649 idev->tstamp = jiffies;
2651 /* Shot the device (if unregistered) */
2653 if (how) {
2654 addrconf_sysctl_unregister(idev);
2655 neigh_parms_release(&nd_tbl, idev->nd_parms);
2656 neigh_ifdown(&nd_tbl, dev);
2657 in6_dev_put(idev);
2659 return 0;
2662 static void addrconf_rs_timer(unsigned long data)
2664 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2666 if (ifp->idev->cnf.forwarding)
2667 goto out;
2669 if (ifp->idev->if_flags & IF_RA_RCVD) {
2671 * Announcement received after solicitation
2672 * was sent
2674 goto out;
2677 spin_lock(&ifp->lock);
2678 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2679 /* The wait after the last probe can be shorter */
2680 addrconf_mod_timer(ifp, AC_RS,
2681 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2682 ifp->idev->cnf.rtr_solicit_delay :
2683 ifp->idev->cnf.rtr_solicit_interval);
2684 spin_unlock(&ifp->lock);
2686 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
2687 } else {
2688 spin_unlock(&ifp->lock);
2690 * Note: we do not support deprecated "all on-link"
2691 * assumption any longer.
2693 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2694 ifp->idev->dev->name);
2697 out:
2698 in6_ifa_put(ifp);
2702 * Duplicate Address Detection
2704 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2706 unsigned long rand_num;
2707 struct inet6_dev *idev = ifp->idev;
2709 if (ifp->flags & IFA_F_OPTIMISTIC)
2710 rand_num = 0;
2711 else
2712 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2714 ifp->probes = idev->cnf.dad_transmits;
2715 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2718 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2720 struct inet6_dev *idev = ifp->idev;
2721 struct net_device *dev = idev->dev;
2723 addrconf_join_solict(dev, &ifp->addr);
2725 net_srandom(ifp->addr.s6_addr32[3]);
2727 read_lock_bh(&idev->lock);
2728 if (ifp->dead)
2729 goto out;
2730 spin_lock_bh(&ifp->lock);
2732 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2733 !(ifp->flags&IFA_F_TENTATIVE) ||
2734 ifp->flags & IFA_F_NODAD) {
2735 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2736 spin_unlock_bh(&ifp->lock);
2737 read_unlock_bh(&idev->lock);
2739 addrconf_dad_completed(ifp);
2740 return;
2743 if (!(idev->if_flags & IF_READY)) {
2744 spin_unlock_bh(&ifp->lock);
2745 read_unlock_bh(&idev->lock);
2747 * If the defice is not ready:
2748 * - keep it tentative if it is a permanent address.
2749 * - otherwise, kill it.
2751 in6_ifa_hold(ifp);
2752 addrconf_dad_stop(ifp);
2753 return;
2757 * Optimistic nodes can start receiving
2758 * Frames right away
2760 if(ifp->flags & IFA_F_OPTIMISTIC)
2761 ip6_ins_rt(ifp->rt);
2763 addrconf_dad_kick(ifp);
2764 spin_unlock_bh(&ifp->lock);
2765 out:
2766 read_unlock_bh(&idev->lock);
2769 static void addrconf_dad_timer(unsigned long data)
2771 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2772 struct inet6_dev *idev = ifp->idev;
2773 struct in6_addr mcaddr;
2775 read_lock_bh(&idev->lock);
2776 if (idev->dead) {
2777 read_unlock_bh(&idev->lock);
2778 goto out;
2780 spin_lock_bh(&ifp->lock);
2781 if (ifp->probes == 0) {
2783 * DAD was successful
2786 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2787 spin_unlock_bh(&ifp->lock);
2788 read_unlock_bh(&idev->lock);
2790 addrconf_dad_completed(ifp);
2792 goto out;
2795 ifp->probes--;
2796 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2797 spin_unlock_bh(&ifp->lock);
2798 read_unlock_bh(&idev->lock);
2800 /* send a neighbour solicitation for our addr */
2801 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2802 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
2803 out:
2804 in6_ifa_put(ifp);
2807 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2809 struct net_device * dev = ifp->idev->dev;
2812 * Configure the address for reception. Now it is valid.
2815 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2817 /* If added prefix is link local and forwarding is off,
2818 start sending router solicitations.
2821 if (ifp->idev->cnf.forwarding == 0 &&
2822 ifp->idev->cnf.rtr_solicits > 0 &&
2823 (dev->flags&IFF_LOOPBACK) == 0 &&
2824 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2826 * If a host as already performed a random delay
2827 * [...] as part of DAD [...] there is no need
2828 * to delay again before sending the first RS
2830 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
2832 spin_lock_bh(&ifp->lock);
2833 ifp->probes = 1;
2834 ifp->idev->if_flags |= IF_RS_SENT;
2835 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2836 spin_unlock_bh(&ifp->lock);
2840 static void addrconf_dad_run(struct inet6_dev *idev) {
2841 struct inet6_ifaddr *ifp;
2843 read_lock_bh(&idev->lock);
2844 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2845 spin_lock_bh(&ifp->lock);
2846 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2847 spin_unlock_bh(&ifp->lock);
2848 continue;
2850 spin_unlock_bh(&ifp->lock);
2851 addrconf_dad_kick(ifp);
2853 read_unlock_bh(&idev->lock);
2856 #ifdef CONFIG_PROC_FS
2857 struct if6_iter_state {
2858 struct seq_net_private p;
2859 int bucket;
2862 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2864 struct inet6_ifaddr *ifa = NULL;
2865 struct if6_iter_state *state = seq->private;
2866 struct net *net = seq_file_net(seq);
2868 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2869 ifa = inet6_addr_lst[state->bucket];
2871 while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
2872 ifa = ifa->lst_next;
2873 if (ifa)
2874 break;
2876 return ifa;
2879 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2881 struct if6_iter_state *state = seq->private;
2882 struct net *net = seq_file_net(seq);
2884 ifa = ifa->lst_next;
2885 try_again:
2886 if (ifa) {
2887 if (!net_eq(dev_net(ifa->idev->dev), net)) {
2888 ifa = ifa->lst_next;
2889 goto try_again;
2893 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2894 ifa = inet6_addr_lst[state->bucket];
2895 goto try_again;
2898 return ifa;
2901 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2903 struct inet6_ifaddr *ifa = if6_get_first(seq);
2905 if (ifa)
2906 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2907 --pos;
2908 return pos ? NULL : ifa;
2911 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2912 __acquires(addrconf_hash_lock)
2914 read_lock_bh(&addrconf_hash_lock);
2915 return if6_get_idx(seq, *pos);
2918 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2920 struct inet6_ifaddr *ifa;
2922 ifa = if6_get_next(seq, v);
2923 ++*pos;
2924 return ifa;
2927 static void if6_seq_stop(struct seq_file *seq, void *v)
2928 __releases(addrconf_hash_lock)
2930 read_unlock_bh(&addrconf_hash_lock);
2933 static int if6_seq_show(struct seq_file *seq, void *v)
2935 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2936 seq_printf(seq,
2937 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2938 NIP6(ifp->addr),
2939 ifp->idev->dev->ifindex,
2940 ifp->prefix_len,
2941 ifp->scope,
2942 ifp->flags,
2943 ifp->idev->dev->name);
2944 return 0;
2947 static const struct seq_operations if6_seq_ops = {
2948 .start = if6_seq_start,
2949 .next = if6_seq_next,
2950 .show = if6_seq_show,
2951 .stop = if6_seq_stop,
2954 static int if6_seq_open(struct inode *inode, struct file *file)
2956 return seq_open_net(inode, file, &if6_seq_ops,
2957 sizeof(struct if6_iter_state));
2960 static const struct file_operations if6_fops = {
2961 .owner = THIS_MODULE,
2962 .open = if6_seq_open,
2963 .read = seq_read,
2964 .llseek = seq_lseek,
2965 .release = seq_release_net,
2968 static int if6_proc_net_init(struct net *net)
2970 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
2971 return -ENOMEM;
2972 return 0;
2975 static void if6_proc_net_exit(struct net *net)
2977 proc_net_remove(net, "if_inet6");
2980 static struct pernet_operations if6_proc_net_ops = {
2981 .init = if6_proc_net_init,
2982 .exit = if6_proc_net_exit,
2985 int __init if6_proc_init(void)
2987 return register_pernet_subsys(&if6_proc_net_ops);
2990 void if6_proc_exit(void)
2992 unregister_pernet_subsys(&if6_proc_net_ops);
2994 #endif /* CONFIG_PROC_FS */
2996 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
2997 /* Check if address is a home address configured on any interface. */
2998 int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
3000 int ret = 0;
3001 struct inet6_ifaddr * ifp;
3002 u8 hash = ipv6_addr_hash(addr);
3003 read_lock_bh(&addrconf_hash_lock);
3004 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
3005 if (!net_eq(dev_net(ifp->idev->dev), net))
3006 continue;
3007 if (ipv6_addr_equal(&ifp->addr, addr) &&
3008 (ifp->flags & IFA_F_HOMEADDRESS)) {
3009 ret = 1;
3010 break;
3013 read_unlock_bh(&addrconf_hash_lock);
3014 return ret;
3016 #endif
3019 * Periodic address status verification
3022 static void addrconf_verify(unsigned long foo)
3024 struct inet6_ifaddr *ifp;
3025 unsigned long now, next;
3026 int i;
3028 spin_lock_bh(&addrconf_verify_lock);
3029 now = jiffies;
3030 next = now + ADDR_CHECK_FREQUENCY;
3032 del_timer(&addr_chk_timer);
3034 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3036 restart:
3037 read_lock(&addrconf_hash_lock);
3038 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
3039 unsigned long age;
3040 #ifdef CONFIG_IPV6_PRIVACY
3041 unsigned long regen_advance;
3042 #endif
3044 if (ifp->flags & IFA_F_PERMANENT)
3045 continue;
3047 spin_lock(&ifp->lock);
3048 age = (now - ifp->tstamp) / HZ;
3050 #ifdef CONFIG_IPV6_PRIVACY
3051 regen_advance = ifp->idev->cnf.regen_max_retry *
3052 ifp->idev->cnf.dad_transmits *
3053 ifp->idev->nd_parms->retrans_time / HZ;
3054 #endif
3056 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3057 age >= ifp->valid_lft) {
3058 spin_unlock(&ifp->lock);
3059 in6_ifa_hold(ifp);
3060 read_unlock(&addrconf_hash_lock);
3061 ipv6_del_addr(ifp);
3062 goto restart;
3063 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3064 spin_unlock(&ifp->lock);
3065 continue;
3066 } else if (age >= ifp->prefered_lft) {
3067 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
3068 int deprecate = 0;
3070 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3071 deprecate = 1;
3072 ifp->flags |= IFA_F_DEPRECATED;
3075 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3076 next = ifp->tstamp + ifp->valid_lft * HZ;
3078 spin_unlock(&ifp->lock);
3080 if (deprecate) {
3081 in6_ifa_hold(ifp);
3082 read_unlock(&addrconf_hash_lock);
3084 ipv6_ifa_notify(0, ifp);
3085 in6_ifa_put(ifp);
3086 goto restart;
3088 #ifdef CONFIG_IPV6_PRIVACY
3089 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3090 !(ifp->flags&IFA_F_TENTATIVE)) {
3091 if (age >= ifp->prefered_lft - regen_advance) {
3092 struct inet6_ifaddr *ifpub = ifp->ifpub;
3093 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3094 next = ifp->tstamp + ifp->prefered_lft * HZ;
3095 if (!ifp->regen_count && ifpub) {
3096 ifp->regen_count++;
3097 in6_ifa_hold(ifp);
3098 in6_ifa_hold(ifpub);
3099 spin_unlock(&ifp->lock);
3100 read_unlock(&addrconf_hash_lock);
3101 spin_lock(&ifpub->lock);
3102 ifpub->regen_count = 0;
3103 spin_unlock(&ifpub->lock);
3104 ipv6_create_tempaddr(ifpub, ifp);
3105 in6_ifa_put(ifpub);
3106 in6_ifa_put(ifp);
3107 goto restart;
3109 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3110 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3111 spin_unlock(&ifp->lock);
3112 #endif
3113 } else {
3114 /* ifp->prefered_lft <= ifp->valid_lft */
3115 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3116 next = ifp->tstamp + ifp->prefered_lft * HZ;
3117 spin_unlock(&ifp->lock);
3120 read_unlock(&addrconf_hash_lock);
3123 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
3124 add_timer(&addr_chk_timer);
3125 spin_unlock_bh(&addrconf_verify_lock);
3128 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3130 struct in6_addr *pfx = NULL;
3132 if (addr)
3133 pfx = nla_data(addr);
3135 if (local) {
3136 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3137 pfx = NULL;
3138 else
3139 pfx = nla_data(local);
3142 return pfx;
3145 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3146 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3147 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3148 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3151 static int
3152 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3154 struct net *net = sock_net(skb->sk);
3155 struct ifaddrmsg *ifm;
3156 struct nlattr *tb[IFA_MAX+1];
3157 struct in6_addr *pfx;
3158 int err;
3160 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3161 if (err < 0)
3162 return err;
3164 ifm = nlmsg_data(nlh);
3165 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3166 if (pfx == NULL)
3167 return -EINVAL;
3169 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3172 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3173 u32 prefered_lft, u32 valid_lft)
3175 u32 flags;
3176 clock_t expires;
3177 unsigned long timeout;
3179 if (!valid_lft || (prefered_lft > valid_lft))
3180 return -EINVAL;
3182 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3183 if (addrconf_finite_timeout(timeout)) {
3184 expires = jiffies_to_clock_t(timeout * HZ);
3185 valid_lft = timeout;
3186 flags = RTF_EXPIRES;
3187 } else {
3188 expires = 0;
3189 flags = 0;
3190 ifa_flags |= IFA_F_PERMANENT;
3193 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3194 if (addrconf_finite_timeout(timeout)) {
3195 if (timeout == 0)
3196 ifa_flags |= IFA_F_DEPRECATED;
3197 prefered_lft = timeout;
3200 spin_lock_bh(&ifp->lock);
3201 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3202 ifp->tstamp = jiffies;
3203 ifp->valid_lft = valid_lft;
3204 ifp->prefered_lft = prefered_lft;
3206 spin_unlock_bh(&ifp->lock);
3207 if (!(ifp->flags&IFA_F_TENTATIVE))
3208 ipv6_ifa_notify(0, ifp);
3210 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3211 expires, flags);
3212 addrconf_verify(0);
3214 return 0;
3217 static int
3218 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3220 struct net *net = sock_net(skb->sk);
3221 struct ifaddrmsg *ifm;
3222 struct nlattr *tb[IFA_MAX+1];
3223 struct in6_addr *pfx;
3224 struct inet6_ifaddr *ifa;
3225 struct net_device *dev;
3226 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3227 u8 ifa_flags;
3228 int err;
3230 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3231 if (err < 0)
3232 return err;
3234 ifm = nlmsg_data(nlh);
3235 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3236 if (pfx == NULL)
3237 return -EINVAL;
3239 if (tb[IFA_CACHEINFO]) {
3240 struct ifa_cacheinfo *ci;
3242 ci = nla_data(tb[IFA_CACHEINFO]);
3243 valid_lft = ci->ifa_valid;
3244 preferred_lft = ci->ifa_prefered;
3245 } else {
3246 preferred_lft = INFINITY_LIFE_TIME;
3247 valid_lft = INFINITY_LIFE_TIME;
3250 dev = __dev_get_by_index(net, ifm->ifa_index);
3251 if (dev == NULL)
3252 return -ENODEV;
3254 /* We ignore other flags so far. */
3255 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3257 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3258 if (ifa == NULL) {
3260 * It would be best to check for !NLM_F_CREATE here but
3261 * userspace alreay relies on not having to provide this.
3263 return inet6_addr_add(net, ifm->ifa_index, pfx,
3264 ifm->ifa_prefixlen, ifa_flags,
3265 preferred_lft, valid_lft);
3268 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3269 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3270 err = -EEXIST;
3271 else
3272 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3274 in6_ifa_put(ifa);
3276 return err;
3279 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3280 u8 scope, int ifindex)
3282 struct ifaddrmsg *ifm;
3284 ifm = nlmsg_data(nlh);
3285 ifm->ifa_family = AF_INET6;
3286 ifm->ifa_prefixlen = prefixlen;
3287 ifm->ifa_flags = flags;
3288 ifm->ifa_scope = scope;
3289 ifm->ifa_index = ifindex;
3292 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3293 unsigned long tstamp, u32 preferred, u32 valid)
3295 struct ifa_cacheinfo ci;
3297 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3298 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3299 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3300 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3301 ci.ifa_prefered = preferred;
3302 ci.ifa_valid = valid;
3304 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3307 static inline int rt_scope(int ifa_scope)
3309 if (ifa_scope & IFA_HOST)
3310 return RT_SCOPE_HOST;
3311 else if (ifa_scope & IFA_LINK)
3312 return RT_SCOPE_LINK;
3313 else if (ifa_scope & IFA_SITE)
3314 return RT_SCOPE_SITE;
3315 else
3316 return RT_SCOPE_UNIVERSE;
3319 static inline int inet6_ifaddr_msgsize(void)
3321 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3322 + nla_total_size(16) /* IFA_ADDRESS */
3323 + nla_total_size(sizeof(struct ifa_cacheinfo));
3326 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3327 u32 pid, u32 seq, int event, unsigned int flags)
3329 struct nlmsghdr *nlh;
3330 u32 preferred, valid;
3332 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3333 if (nlh == NULL)
3334 return -EMSGSIZE;
3336 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3337 ifa->idev->dev->ifindex);
3339 if (!(ifa->flags&IFA_F_PERMANENT)) {
3340 preferred = ifa->prefered_lft;
3341 valid = ifa->valid_lft;
3342 if (preferred != INFINITY_LIFE_TIME) {
3343 long tval = (jiffies - ifa->tstamp)/HZ;
3344 preferred -= tval;
3345 if (valid != INFINITY_LIFE_TIME)
3346 valid -= tval;
3348 } else {
3349 preferred = INFINITY_LIFE_TIME;
3350 valid = INFINITY_LIFE_TIME;
3353 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3354 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3355 nlmsg_cancel(skb, nlh);
3356 return -EMSGSIZE;
3359 return nlmsg_end(skb, nlh);
3362 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3363 u32 pid, u32 seq, int event, u16 flags)
3365 struct nlmsghdr *nlh;
3366 u8 scope = RT_SCOPE_UNIVERSE;
3367 int ifindex = ifmca->idev->dev->ifindex;
3369 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3370 scope = RT_SCOPE_SITE;
3372 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3373 if (nlh == NULL)
3374 return -EMSGSIZE;
3376 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3377 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3378 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3379 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3380 nlmsg_cancel(skb, nlh);
3381 return -EMSGSIZE;
3384 return nlmsg_end(skb, nlh);
3387 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3388 u32 pid, u32 seq, int event, unsigned int flags)
3390 struct nlmsghdr *nlh;
3391 u8 scope = RT_SCOPE_UNIVERSE;
3392 int ifindex = ifaca->aca_idev->dev->ifindex;
3394 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3395 scope = RT_SCOPE_SITE;
3397 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3398 if (nlh == NULL)
3399 return -EMSGSIZE;
3401 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3402 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3403 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3404 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3405 nlmsg_cancel(skb, nlh);
3406 return -EMSGSIZE;
3409 return nlmsg_end(skb, nlh);
3412 enum addr_type_t
3414 UNICAST_ADDR,
3415 MULTICAST_ADDR,
3416 ANYCAST_ADDR,
3419 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3420 enum addr_type_t type)
3422 int idx, ip_idx;
3423 int s_idx, s_ip_idx;
3424 int err = 1;
3425 struct net_device *dev;
3426 struct inet6_dev *idev = NULL;
3427 struct inet6_ifaddr *ifa;
3428 struct ifmcaddr6 *ifmca;
3429 struct ifacaddr6 *ifaca;
3430 struct net *net = sock_net(skb->sk);
3432 s_idx = cb->args[0];
3433 s_ip_idx = ip_idx = cb->args[1];
3435 idx = 0;
3436 for_each_netdev(net, dev) {
3437 if (idx < s_idx)
3438 goto cont;
3439 if (idx > s_idx)
3440 s_ip_idx = 0;
3441 ip_idx = 0;
3442 if ((idev = in6_dev_get(dev)) == NULL)
3443 goto cont;
3444 read_lock_bh(&idev->lock);
3445 switch (type) {
3446 case UNICAST_ADDR:
3447 /* unicast address incl. temp addr */
3448 for (ifa = idev->addr_list; ifa;
3449 ifa = ifa->if_next, ip_idx++) {
3450 if (ip_idx < s_ip_idx)
3451 continue;
3452 err = inet6_fill_ifaddr(skb, ifa,
3453 NETLINK_CB(cb->skb).pid,
3454 cb->nlh->nlmsg_seq,
3455 RTM_NEWADDR,
3456 NLM_F_MULTI);
3458 break;
3459 case MULTICAST_ADDR:
3460 /* multicast address */
3461 for (ifmca = idev->mc_list; ifmca;
3462 ifmca = ifmca->next, ip_idx++) {
3463 if (ip_idx < s_ip_idx)
3464 continue;
3465 err = inet6_fill_ifmcaddr(skb, ifmca,
3466 NETLINK_CB(cb->skb).pid,
3467 cb->nlh->nlmsg_seq,
3468 RTM_GETMULTICAST,
3469 NLM_F_MULTI);
3471 break;
3472 case ANYCAST_ADDR:
3473 /* anycast address */
3474 for (ifaca = idev->ac_list; ifaca;
3475 ifaca = ifaca->aca_next, ip_idx++) {
3476 if (ip_idx < s_ip_idx)
3477 continue;
3478 err = inet6_fill_ifacaddr(skb, ifaca,
3479 NETLINK_CB(cb->skb).pid,
3480 cb->nlh->nlmsg_seq,
3481 RTM_GETANYCAST,
3482 NLM_F_MULTI);
3484 break;
3485 default:
3486 break;
3488 read_unlock_bh(&idev->lock);
3489 in6_dev_put(idev);
3491 if (err <= 0)
3492 break;
3493 cont:
3494 idx++;
3496 cb->args[0] = idx;
3497 cb->args[1] = ip_idx;
3498 return skb->len;
3501 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3503 enum addr_type_t type = UNICAST_ADDR;
3505 return inet6_dump_addr(skb, cb, type);
3508 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3510 enum addr_type_t type = MULTICAST_ADDR;
3512 return inet6_dump_addr(skb, cb, type);
3516 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3518 enum addr_type_t type = ANYCAST_ADDR;
3520 return inet6_dump_addr(skb, cb, type);
3523 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3524 void *arg)
3526 struct net *net = sock_net(in_skb->sk);
3527 struct ifaddrmsg *ifm;
3528 struct nlattr *tb[IFA_MAX+1];
3529 struct in6_addr *addr = NULL;
3530 struct net_device *dev = NULL;
3531 struct inet6_ifaddr *ifa;
3532 struct sk_buff *skb;
3533 int err;
3535 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3536 if (err < 0)
3537 goto errout;
3539 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3540 if (addr == NULL) {
3541 err = -EINVAL;
3542 goto errout;
3545 ifm = nlmsg_data(nlh);
3546 if (ifm->ifa_index)
3547 dev = __dev_get_by_index(net, ifm->ifa_index);
3549 if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
3550 err = -EADDRNOTAVAIL;
3551 goto errout;
3554 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
3555 err = -ENOBUFS;
3556 goto errout_ifa;
3559 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3560 nlh->nlmsg_seq, RTM_NEWADDR, 0);
3561 if (err < 0) {
3562 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3563 WARN_ON(err == -EMSGSIZE);
3564 kfree_skb(skb);
3565 goto errout_ifa;
3567 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
3568 errout_ifa:
3569 in6_ifa_put(ifa);
3570 errout:
3571 return err;
3574 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3576 struct sk_buff *skb;
3577 struct net *net = dev_net(ifa->idev->dev);
3578 int err = -ENOBUFS;
3580 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
3581 if (skb == NULL)
3582 goto errout;
3584 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3585 if (err < 0) {
3586 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3587 WARN_ON(err == -EMSGSIZE);
3588 kfree_skb(skb);
3589 goto errout;
3591 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3592 errout:
3593 if (err < 0)
3594 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
3597 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
3598 __s32 *array, int bytes)
3600 BUG_ON(bytes < (DEVCONF_MAX * 4));
3602 memset(array, 0, bytes);
3603 array[DEVCONF_FORWARDING] = cnf->forwarding;
3604 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3605 array[DEVCONF_MTU6] = cnf->mtu6;
3606 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3607 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3608 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3609 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3610 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3611 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3612 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3613 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3614 #ifdef CONFIG_IPV6_PRIVACY
3615 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3616 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3617 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3618 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3619 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3620 #endif
3621 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3622 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3623 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3624 #ifdef CONFIG_IPV6_ROUTER_PREF
3625 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
3626 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3627 #ifdef CONFIG_IPV6_ROUTE_INFO
3628 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3629 #endif
3630 #endif
3631 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
3632 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
3633 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3634 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3635 #endif
3636 #ifdef CONFIG_IPV6_MROUTE
3637 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
3638 #endif
3641 static inline size_t inet6_if_nlmsg_size(void)
3643 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3644 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3645 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3646 + nla_total_size(4) /* IFLA_MTU */
3647 + nla_total_size(4) /* IFLA_LINK */
3648 + nla_total_size( /* IFLA_PROTINFO */
3649 nla_total_size(4) /* IFLA_INET6_FLAGS */
3650 + nla_total_size(sizeof(struct ifla_cacheinfo))
3651 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3652 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3653 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3657 static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3658 int bytes)
3660 int i;
3661 int pad = bytes - sizeof(u64) * items;
3662 BUG_ON(pad < 0);
3664 /* Use put_unaligned() because stats may not be aligned for u64. */
3665 put_unaligned(items, &stats[0]);
3666 for (i = 1; i < items; i++)
3667 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3669 memset(&stats[items], 0, pad);
3672 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3673 int bytes)
3675 switch(attrtype) {
3676 case IFLA_INET6_STATS:
3677 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3678 break;
3679 case IFLA_INET6_ICMP6STATS:
3680 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3681 break;
3685 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3686 u32 pid, u32 seq, int event, unsigned int flags)
3688 struct net_device *dev = idev->dev;
3689 struct nlattr *nla;
3690 struct ifinfomsg *hdr;
3691 struct nlmsghdr *nlh;
3692 void *protoinfo;
3693 struct ifla_cacheinfo ci;
3695 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3696 if (nlh == NULL)
3697 return -EMSGSIZE;
3699 hdr = nlmsg_data(nlh);
3700 hdr->ifi_family = AF_INET6;
3701 hdr->__ifi_pad = 0;
3702 hdr->ifi_type = dev->type;
3703 hdr->ifi_index = dev->ifindex;
3704 hdr->ifi_flags = dev_get_flags(dev);
3705 hdr->ifi_change = 0;
3707 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
3709 if (dev->addr_len)
3710 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3712 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3713 if (dev->ifindex != dev->iflink)
3714 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3716 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3717 if (protoinfo == NULL)
3718 goto nla_put_failure;
3720 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
3722 ci.max_reasm_len = IPV6_MAXPLEN;
3723 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3724 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3725 ci.reachable_time = idev->nd_parms->reachable_time;
3726 ci.retrans_time = idev->nd_parms->retrans_time;
3727 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3729 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3730 if (nla == NULL)
3731 goto nla_put_failure;
3732 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
3734 /* XXX - MC not implemented */
3736 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3737 if (nla == NULL)
3738 goto nla_put_failure;
3739 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3741 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3742 if (nla == NULL)
3743 goto nla_put_failure;
3744 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
3746 nla_nest_end(skb, protoinfo);
3747 return nlmsg_end(skb, nlh);
3749 nla_put_failure:
3750 nlmsg_cancel(skb, nlh);
3751 return -EMSGSIZE;
3754 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3756 struct net *net = sock_net(skb->sk);
3757 int idx, err;
3758 int s_idx = cb->args[0];
3759 struct net_device *dev;
3760 struct inet6_dev *idev;
3762 read_lock(&dev_base_lock);
3763 idx = 0;
3764 for_each_netdev(net, dev) {
3765 if (idx < s_idx)
3766 goto cont;
3767 if ((idev = in6_dev_get(dev)) == NULL)
3768 goto cont;
3769 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3770 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3771 in6_dev_put(idev);
3772 if (err <= 0)
3773 break;
3774 cont:
3775 idx++;
3777 read_unlock(&dev_base_lock);
3778 cb->args[0] = idx;
3780 return skb->len;
3783 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3785 struct sk_buff *skb;
3786 struct net *net = dev_net(idev->dev);
3787 int err = -ENOBUFS;
3789 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
3790 if (skb == NULL)
3791 goto errout;
3793 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3794 if (err < 0) {
3795 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3796 WARN_ON(err == -EMSGSIZE);
3797 kfree_skb(skb);
3798 goto errout;
3800 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3801 errout:
3802 if (err < 0)
3803 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
3806 static inline size_t inet6_prefix_nlmsg_size(void)
3808 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3809 + nla_total_size(sizeof(struct in6_addr))
3810 + nla_total_size(sizeof(struct prefix_cacheinfo));
3813 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3814 struct prefix_info *pinfo, u32 pid, u32 seq,
3815 int event, unsigned int flags)
3817 struct prefixmsg *pmsg;
3818 struct nlmsghdr *nlh;
3819 struct prefix_cacheinfo ci;
3821 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3822 if (nlh == NULL)
3823 return -EMSGSIZE;
3825 pmsg = nlmsg_data(nlh);
3826 pmsg->prefix_family = AF_INET6;
3827 pmsg->prefix_pad1 = 0;
3828 pmsg->prefix_pad2 = 0;
3829 pmsg->prefix_ifindex = idev->dev->ifindex;
3830 pmsg->prefix_len = pinfo->prefix_len;
3831 pmsg->prefix_type = pinfo->type;
3832 pmsg->prefix_pad3 = 0;
3833 pmsg->prefix_flags = 0;
3834 if (pinfo->onlink)
3835 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3836 if (pinfo->autoconf)
3837 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3839 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3841 ci.preferred_time = ntohl(pinfo->prefered);
3842 ci.valid_time = ntohl(pinfo->valid);
3843 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3845 return nlmsg_end(skb, nlh);
3847 nla_put_failure:
3848 nlmsg_cancel(skb, nlh);
3849 return -EMSGSIZE;
3852 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3853 struct prefix_info *pinfo)
3855 struct sk_buff *skb;
3856 struct net *net = dev_net(idev->dev);
3857 int err = -ENOBUFS;
3859 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
3860 if (skb == NULL)
3861 goto errout;
3863 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3864 if (err < 0) {
3865 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3866 WARN_ON(err == -EMSGSIZE);
3867 kfree_skb(skb);
3868 goto errout;
3870 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3871 errout:
3872 if (err < 0)
3873 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
3876 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3878 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3880 switch (event) {
3881 case RTM_NEWADDR:
3883 * If the address was optimistic
3884 * we inserted the route at the start of
3885 * our DAD process, so we don't need
3886 * to do it again
3888 if (!(ifp->rt->rt6i_node))
3889 ip6_ins_rt(ifp->rt);
3890 if (ifp->idev->cnf.forwarding)
3891 addrconf_join_anycast(ifp);
3892 break;
3893 case RTM_DELADDR:
3894 if (ifp->idev->cnf.forwarding)
3895 addrconf_leave_anycast(ifp);
3896 addrconf_leave_solict(ifp->idev, &ifp->addr);
3897 dst_hold(&ifp->rt->u.dst);
3898 if (ip6_del_rt(ifp->rt))
3899 dst_free(&ifp->rt->u.dst);
3900 break;
3904 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3906 rcu_read_lock_bh();
3907 if (likely(ifp->idev->dead == 0))
3908 __ipv6_ifa_notify(event, ifp);
3909 rcu_read_unlock_bh();
3912 #ifdef CONFIG_SYSCTL
3914 static
3915 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3916 void __user *buffer, size_t *lenp, loff_t *ppos)
3918 int *valp = ctl->data;
3919 int val = *valp;
3920 int ret;
3922 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3924 if (write)
3925 addrconf_fixup_forwarding(ctl, valp, val);
3926 return ret;
3929 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3930 int __user *name, int nlen,
3931 void __user *oldval,
3932 size_t __user *oldlenp,
3933 void __user *newval, size_t newlen)
3935 int *valp = table->data;
3936 int val = *valp;
3937 int new;
3939 if (!newval || !newlen)
3940 return 0;
3941 if (newlen != sizeof(int))
3942 return -EINVAL;
3943 if (get_user(new, (int __user *)newval))
3944 return -EFAULT;
3945 if (new == *valp)
3946 return 0;
3947 if (oldval && oldlenp) {
3948 size_t len;
3949 if (get_user(len, oldlenp))
3950 return -EFAULT;
3951 if (len) {
3952 if (len > table->maxlen)
3953 len = table->maxlen;
3954 if (copy_to_user(oldval, valp, len))
3955 return -EFAULT;
3956 if (put_user(len, oldlenp))
3957 return -EFAULT;
3961 *valp = new;
3962 addrconf_fixup_forwarding(table, valp, val);
3963 return 1;
3966 static struct addrconf_sysctl_table
3968 struct ctl_table_header *sysctl_header;
3969 ctl_table addrconf_vars[DEVCONF_MAX+1];
3970 char *dev_name;
3971 } addrconf_sysctl __read_mostly = {
3972 .sysctl_header = NULL,
3973 .addrconf_vars = {
3975 .ctl_name = NET_IPV6_FORWARDING,
3976 .procname = "forwarding",
3977 .data = &ipv6_devconf.forwarding,
3978 .maxlen = sizeof(int),
3979 .mode = 0644,
3980 .proc_handler = &addrconf_sysctl_forward,
3981 .strategy = &addrconf_sysctl_forward_strategy,
3984 .ctl_name = NET_IPV6_HOP_LIMIT,
3985 .procname = "hop_limit",
3986 .data = &ipv6_devconf.hop_limit,
3987 .maxlen = sizeof(int),
3988 .mode = 0644,
3989 .proc_handler = proc_dointvec,
3992 .ctl_name = NET_IPV6_MTU,
3993 .procname = "mtu",
3994 .data = &ipv6_devconf.mtu6,
3995 .maxlen = sizeof(int),
3996 .mode = 0644,
3997 .proc_handler = &proc_dointvec,
4000 .ctl_name = NET_IPV6_ACCEPT_RA,
4001 .procname = "accept_ra",
4002 .data = &ipv6_devconf.accept_ra,
4003 .maxlen = sizeof(int),
4004 .mode = 0644,
4005 .proc_handler = &proc_dointvec,
4008 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
4009 .procname = "accept_redirects",
4010 .data = &ipv6_devconf.accept_redirects,
4011 .maxlen = sizeof(int),
4012 .mode = 0644,
4013 .proc_handler = &proc_dointvec,
4016 .ctl_name = NET_IPV6_AUTOCONF,
4017 .procname = "autoconf",
4018 .data = &ipv6_devconf.autoconf,
4019 .maxlen = sizeof(int),
4020 .mode = 0644,
4021 .proc_handler = &proc_dointvec,
4024 .ctl_name = NET_IPV6_DAD_TRANSMITS,
4025 .procname = "dad_transmits",
4026 .data = &ipv6_devconf.dad_transmits,
4027 .maxlen = sizeof(int),
4028 .mode = 0644,
4029 .proc_handler = &proc_dointvec,
4032 .ctl_name = NET_IPV6_RTR_SOLICITS,
4033 .procname = "router_solicitations",
4034 .data = &ipv6_devconf.rtr_solicits,
4035 .maxlen = sizeof(int),
4036 .mode = 0644,
4037 .proc_handler = &proc_dointvec,
4040 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
4041 .procname = "router_solicitation_interval",
4042 .data = &ipv6_devconf.rtr_solicit_interval,
4043 .maxlen = sizeof(int),
4044 .mode = 0644,
4045 .proc_handler = &proc_dointvec_jiffies,
4046 .strategy = &sysctl_jiffies,
4049 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
4050 .procname = "router_solicitation_delay",
4051 .data = &ipv6_devconf.rtr_solicit_delay,
4052 .maxlen = sizeof(int),
4053 .mode = 0644,
4054 .proc_handler = &proc_dointvec_jiffies,
4055 .strategy = &sysctl_jiffies,
4058 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
4059 .procname = "force_mld_version",
4060 .data = &ipv6_devconf.force_mld_version,
4061 .maxlen = sizeof(int),
4062 .mode = 0644,
4063 .proc_handler = &proc_dointvec,
4065 #ifdef CONFIG_IPV6_PRIVACY
4067 .ctl_name = NET_IPV6_USE_TEMPADDR,
4068 .procname = "use_tempaddr",
4069 .data = &ipv6_devconf.use_tempaddr,
4070 .maxlen = sizeof(int),
4071 .mode = 0644,
4072 .proc_handler = &proc_dointvec,
4075 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
4076 .procname = "temp_valid_lft",
4077 .data = &ipv6_devconf.temp_valid_lft,
4078 .maxlen = sizeof(int),
4079 .mode = 0644,
4080 .proc_handler = &proc_dointvec,
4083 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
4084 .procname = "temp_prefered_lft",
4085 .data = &ipv6_devconf.temp_prefered_lft,
4086 .maxlen = sizeof(int),
4087 .mode = 0644,
4088 .proc_handler = &proc_dointvec,
4091 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
4092 .procname = "regen_max_retry",
4093 .data = &ipv6_devconf.regen_max_retry,
4094 .maxlen = sizeof(int),
4095 .mode = 0644,
4096 .proc_handler = &proc_dointvec,
4099 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
4100 .procname = "max_desync_factor",
4101 .data = &ipv6_devconf.max_desync_factor,
4102 .maxlen = sizeof(int),
4103 .mode = 0644,
4104 .proc_handler = &proc_dointvec,
4106 #endif
4108 .ctl_name = NET_IPV6_MAX_ADDRESSES,
4109 .procname = "max_addresses",
4110 .data = &ipv6_devconf.max_addresses,
4111 .maxlen = sizeof(int),
4112 .mode = 0644,
4113 .proc_handler = &proc_dointvec,
4116 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
4117 .procname = "accept_ra_defrtr",
4118 .data = &ipv6_devconf.accept_ra_defrtr,
4119 .maxlen = sizeof(int),
4120 .mode = 0644,
4121 .proc_handler = &proc_dointvec,
4124 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
4125 .procname = "accept_ra_pinfo",
4126 .data = &ipv6_devconf.accept_ra_pinfo,
4127 .maxlen = sizeof(int),
4128 .mode = 0644,
4129 .proc_handler = &proc_dointvec,
4131 #ifdef CONFIG_IPV6_ROUTER_PREF
4133 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
4134 .procname = "accept_ra_rtr_pref",
4135 .data = &ipv6_devconf.accept_ra_rtr_pref,
4136 .maxlen = sizeof(int),
4137 .mode = 0644,
4138 .proc_handler = &proc_dointvec,
4141 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4142 .procname = "router_probe_interval",
4143 .data = &ipv6_devconf.rtr_probe_interval,
4144 .maxlen = sizeof(int),
4145 .mode = 0644,
4146 .proc_handler = &proc_dointvec_jiffies,
4147 .strategy = &sysctl_jiffies,
4149 #ifdef CONFIG_IPV6_ROUTE_INFO
4151 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4152 .procname = "accept_ra_rt_info_max_plen",
4153 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4154 .maxlen = sizeof(int),
4155 .mode = 0644,
4156 .proc_handler = &proc_dointvec,
4158 #endif
4159 #endif
4161 .ctl_name = NET_IPV6_PROXY_NDP,
4162 .procname = "proxy_ndp",
4163 .data = &ipv6_devconf.proxy_ndp,
4164 .maxlen = sizeof(int),
4165 .mode = 0644,
4166 .proc_handler = &proc_dointvec,
4169 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4170 .procname = "accept_source_route",
4171 .data = &ipv6_devconf.accept_source_route,
4172 .maxlen = sizeof(int),
4173 .mode = 0644,
4174 .proc_handler = &proc_dointvec,
4176 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4178 .ctl_name = CTL_UNNUMBERED,
4179 .procname = "optimistic_dad",
4180 .data = &ipv6_devconf.optimistic_dad,
4181 .maxlen = sizeof(int),
4182 .mode = 0644,
4183 .proc_handler = &proc_dointvec,
4186 #endif
4187 #ifdef CONFIG_IPV6_MROUTE
4189 .ctl_name = CTL_UNNUMBERED,
4190 .procname = "mc_forwarding",
4191 .data = &ipv6_devconf.mc_forwarding,
4192 .maxlen = sizeof(int),
4193 .mode = 0644,
4194 .proc_handler = &proc_dointvec,
4196 #endif
4198 .ctl_name = 0, /* sentinel */
4203 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4204 int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
4206 int i;
4207 struct addrconf_sysctl_table *t;
4209 #define ADDRCONF_CTL_PATH_DEV 3
4211 struct ctl_path addrconf_ctl_path[] = {
4212 { .procname = "net", .ctl_name = CTL_NET, },
4213 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4214 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4215 { /* to be set */ },
4216 { },
4220 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
4221 if (t == NULL)
4222 goto out;
4224 for (i=0; t->addrconf_vars[i].data; i++) {
4225 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4226 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4227 t->addrconf_vars[i].extra2 = net;
4231 * Make a copy of dev_name, because '.procname' is regarded as const
4232 * by sysctl and we wouldn't want anyone to change it under our feet
4233 * (see SIOCSIFNAME).
4235 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4236 if (!t->dev_name)
4237 goto free;
4239 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4240 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
4242 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
4243 t->addrconf_vars);
4244 if (t->sysctl_header == NULL)
4245 goto free_procname;
4247 p->sysctl = t;
4248 return 0;
4250 free_procname:
4251 kfree(t->dev_name);
4252 free:
4253 kfree(t);
4254 out:
4255 return -ENOBUFS;
4258 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4260 struct addrconf_sysctl_table *t;
4262 if (p->sysctl == NULL)
4263 return;
4265 t = p->sysctl;
4266 p->sysctl = NULL;
4267 unregister_sysctl_table(t->sysctl_header);
4268 kfree(t->dev_name);
4269 kfree(t);
4272 static void addrconf_sysctl_register(struct inet6_dev *idev)
4274 neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4275 NET_IPV6_NEIGH, "ipv6",
4276 &ndisc_ifinfo_sysctl_change,
4277 ndisc_ifinfo_sysctl_strategy);
4278 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4279 idev->dev->ifindex, idev, &idev->cnf);
4282 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4284 __addrconf_sysctl_unregister(&idev->cnf);
4285 neigh_sysctl_unregister(idev->nd_parms);
4289 #endif
4291 static int addrconf_init_net(struct net *net)
4293 int err;
4294 struct ipv6_devconf *all, *dflt;
4296 err = -ENOMEM;
4297 all = &ipv6_devconf;
4298 dflt = &ipv6_devconf_dflt;
4300 if (net != &init_net) {
4301 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4302 if (all == NULL)
4303 goto err_alloc_all;
4305 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4306 if (dflt == NULL)
4307 goto err_alloc_dflt;
4310 net->ipv6.devconf_all = all;
4311 net->ipv6.devconf_dflt = dflt;
4313 #ifdef CONFIG_SYSCTL
4314 err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4315 NULL, all);
4316 if (err < 0)
4317 goto err_reg_all;
4319 err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4320 NULL, dflt);
4321 if (err < 0)
4322 goto err_reg_dflt;
4323 #endif
4324 return 0;
4326 #ifdef CONFIG_SYSCTL
4327 err_reg_dflt:
4328 __addrconf_sysctl_unregister(all);
4329 err_reg_all:
4330 kfree(dflt);
4331 #endif
4332 err_alloc_dflt:
4333 kfree(all);
4334 err_alloc_all:
4335 return err;
4338 static void addrconf_exit_net(struct net *net)
4340 #ifdef CONFIG_SYSCTL
4341 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4342 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4343 #endif
4344 if (net != &init_net) {
4345 kfree(net->ipv6.devconf_dflt);
4346 kfree(net->ipv6.devconf_all);
4350 static struct pernet_operations addrconf_ops = {
4351 .init = addrconf_init_net,
4352 .exit = addrconf_exit_net,
4356 * Device notifier
4359 int register_inet6addr_notifier(struct notifier_block *nb)
4361 return atomic_notifier_chain_register(&inet6addr_chain, nb);
4364 EXPORT_SYMBOL(register_inet6addr_notifier);
4366 int unregister_inet6addr_notifier(struct notifier_block *nb)
4368 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
4371 EXPORT_SYMBOL(unregister_inet6addr_notifier);
4373 static void addrconf_net_exit(struct net *net)
4375 struct net_device *dev;
4377 rtnl_lock();
4378 /* clean dev list */
4379 for_each_netdev(net, dev) {
4380 if (__in6_dev_get(dev) == NULL)
4381 continue;
4382 addrconf_ifdown(dev, 1);
4384 addrconf_ifdown(net->loopback_dev, 2);
4385 rtnl_unlock();
4388 static struct pernet_operations addrconf_net_ops = {
4389 .exit = addrconf_net_exit,
4393 * Init / cleanup code
4396 int __init addrconf_init(void)
4398 int err;
4400 if ((err = ipv6_addr_label_init()) < 0) {
4401 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4402 err);
4403 return err;
4406 register_pernet_subsys(&addrconf_ops);
4408 /* The addrconf netdev notifier requires that loopback_dev
4409 * has it's ipv6 private information allocated and setup
4410 * before it can bring up and give link-local addresses
4411 * to other devices which are up.
4413 * Unfortunately, loopback_dev is not necessarily the first
4414 * entry in the global dev_base list of net devices. In fact,
4415 * it is likely to be the very last entry on that list.
4416 * So this causes the notifier registry below to try and
4417 * give link-local addresses to all devices besides loopback_dev
4418 * first, then loopback_dev, which cases all the non-loopback_dev
4419 * devices to fail to get a link-local address.
4421 * So, as a temporary fix, allocate the ipv6 structure for
4422 * loopback_dev first by hand.
4423 * Longer term, all of the dependencies ipv6 has upon the loopback
4424 * device and it being up should be removed.
4426 rtnl_lock();
4427 if (!ipv6_add_dev(init_net.loopback_dev))
4428 err = -ENOMEM;
4429 rtnl_unlock();
4430 if (err)
4431 goto errlo;
4433 err = register_pernet_device(&addrconf_net_ops);
4434 if (err)
4435 return err;
4437 register_netdevice_notifier(&ipv6_dev_notf);
4439 addrconf_verify(0);
4441 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4442 if (err < 0)
4443 goto errout;
4445 /* Only the first call to __rtnl_register can fail */
4446 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4447 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4448 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4449 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4450 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4452 ipv6_addr_label_rtnl_register();
4454 return 0;
4455 errout:
4456 unregister_netdevice_notifier(&ipv6_dev_notf);
4457 errlo:
4458 unregister_pernet_subsys(&addrconf_ops);
4460 return err;
4463 void addrconf_cleanup(void)
4465 struct inet6_ifaddr *ifa;
4466 int i;
4468 unregister_netdevice_notifier(&ipv6_dev_notf);
4469 unregister_pernet_device(&addrconf_net_ops);
4471 unregister_pernet_subsys(&addrconf_ops);
4473 rtnl_lock();
4476 * Check hash table.
4478 write_lock_bh(&addrconf_hash_lock);
4479 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4480 for (ifa=inet6_addr_lst[i]; ifa; ) {
4481 struct inet6_ifaddr *bifa;
4483 bifa = ifa;
4484 ifa = ifa->lst_next;
4485 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4486 /* Do not free it; something is wrong.
4487 Now we can investigate it with debugger.
4491 write_unlock_bh(&addrconf_hash_lock);
4493 del_timer(&addr_chk_timer);
4494 rtnl_unlock();
4496 unregister_pernet_subsys(&addrconf_net_ops);