[PATCH] Kprobes: preempt_disable/enable() simplification
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / addrconf.c
blob2c5f57299d63b1f2badbd056286513f7ef51e646
1 /*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
18 * Changes:
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
26 * packets.
27 * yoshfuji@USAGI : Fixed interval between DAD
28 * packets.
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
32 * support.
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
37 * seq_file.
40 #include <linux/config.h>
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/sched.h>
46 #include <linux/net.h>
47 #include <linux/in6.h>
48 #include <linux/netdevice.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/delay.h>
59 #include <linux/notifier.h>
60 #include <linux/string.h>
62 #include <net/sock.h>
63 #include <net/snmp.h>
65 #include <net/ipv6.h>
66 #include <net/protocol.h>
67 #include <net/ndisc.h>
68 #include <net/ip6_route.h>
69 #include <net/addrconf.h>
70 #include <net/tcp.h>
71 #include <net/ip.h>
72 #include <linux/if_tunnel.h>
73 #include <linux/rtnetlink.h>
75 #ifdef CONFIG_IPV6_PRIVACY
76 #include <linux/random.h>
77 #include <linux/crypto.h>
78 #include <linux/scatterlist.h>
79 #endif
81 #include <asm/uaccess.h>
83 #include <linux/proc_fs.h>
84 #include <linux/seq_file.h>
86 /* Set to 3 to get tracing... */
87 #define ACONF_DEBUG 2
89 #if ACONF_DEBUG >= 3
90 #define ADBG(x) printk x
91 #else
92 #define ADBG(x)
93 #endif
95 #define INFINITY_LIFE_TIME 0xFFFFFFFF
96 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
98 #ifdef CONFIG_SYSCTL
99 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
100 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
101 #endif
103 #ifdef CONFIG_IPV6_PRIVACY
104 static int __ipv6_regen_rndid(struct inet6_dev *idev);
105 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
106 static void ipv6_regen_rndid(unsigned long data);
108 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
109 static struct crypto_tfm *md5_tfm;
110 static DEFINE_SPINLOCK(md5_tfm_lock);
111 #endif
113 static int ipv6_count_addresses(struct inet6_dev *idev);
116 * Configured unicast address hash table
118 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
119 static DEFINE_RWLOCK(addrconf_hash_lock);
121 /* Protects inet6 devices */
122 DEFINE_RWLOCK(addrconf_lock);
124 static void addrconf_verify(unsigned long);
126 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
127 static DEFINE_SPINLOCK(addrconf_verify_lock);
129 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
130 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
132 static int addrconf_ifdown(struct net_device *dev, int how);
134 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
135 static void addrconf_dad_timer(unsigned long data);
136 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
137 static void addrconf_rs_timer(unsigned long data);
138 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
141 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
142 struct prefix_info *pinfo);
143 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
145 static struct notifier_block *inet6addr_chain;
147 struct ipv6_devconf ipv6_devconf = {
148 .forwarding = 0,
149 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
150 .mtu6 = IPV6_MIN_MTU,
151 .accept_ra = 1,
152 .accept_redirects = 1,
153 .autoconf = 1,
154 .force_mld_version = 0,
155 .dad_transmits = 1,
156 .rtr_solicits = MAX_RTR_SOLICITATIONS,
157 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
158 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
159 #ifdef CONFIG_IPV6_PRIVACY
160 .use_tempaddr = 0,
161 .temp_valid_lft = TEMP_VALID_LIFETIME,
162 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
163 .regen_max_retry = REGEN_MAX_RETRY,
164 .max_desync_factor = MAX_DESYNC_FACTOR,
165 #endif
166 .max_addresses = IPV6_MAX_ADDRESSES,
169 static struct ipv6_devconf ipv6_devconf_dflt = {
170 .forwarding = 0,
171 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
172 .mtu6 = IPV6_MIN_MTU,
173 .accept_ra = 1,
174 .accept_redirects = 1,
175 .autoconf = 1,
176 .dad_transmits = 1,
177 .rtr_solicits = MAX_RTR_SOLICITATIONS,
178 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
179 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
180 #ifdef CONFIG_IPV6_PRIVACY
181 .use_tempaddr = 0,
182 .temp_valid_lft = TEMP_VALID_LIFETIME,
183 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
184 .regen_max_retry = REGEN_MAX_RETRY,
185 .max_desync_factor = MAX_DESYNC_FACTOR,
186 #endif
187 .max_addresses = IPV6_MAX_ADDRESSES,
190 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
191 #if 0
192 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
193 #endif
194 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
196 int ipv6_addr_type(const struct in6_addr *addr)
198 int type;
199 u32 st;
201 st = addr->s6_addr32[0];
203 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
204 type = IPV6_ADDR_MULTICAST;
206 switch((st & htonl(0x00FF0000))) {
207 case __constant_htonl(0x00010000):
208 type |= IPV6_ADDR_LOOPBACK;
209 break;
211 case __constant_htonl(0x00020000):
212 type |= IPV6_ADDR_LINKLOCAL;
213 break;
215 case __constant_htonl(0x00050000):
216 type |= IPV6_ADDR_SITELOCAL;
217 break;
219 return type;
222 type = IPV6_ADDR_UNICAST;
224 /* Consider all addresses with the first three bits different of
225 000 and 111 as finished.
227 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
228 (st & htonl(0xE0000000)) != htonl(0xE0000000))
229 return type;
231 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
232 return (IPV6_ADDR_LINKLOCAL | type);
234 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
235 return (IPV6_ADDR_SITELOCAL | type);
237 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
238 if (addr->s6_addr32[2] == 0) {
239 if (addr->s6_addr32[3] == 0)
240 return IPV6_ADDR_ANY;
242 if (addr->s6_addr32[3] == htonl(0x00000001))
243 return (IPV6_ADDR_LOOPBACK | type);
245 return (IPV6_ADDR_COMPATv4 | type);
248 if (addr->s6_addr32[2] == htonl(0x0000ffff))
249 return IPV6_ADDR_MAPPED;
252 st &= htonl(0xFF000000);
253 if (st == 0)
254 return IPV6_ADDR_RESERVED;
255 st &= htonl(0xFE000000);
256 if (st == htonl(0x02000000))
257 return IPV6_ADDR_RESERVED; /* for NSAP */
258 if (st == htonl(0x04000000))
259 return IPV6_ADDR_RESERVED; /* for IPX */
260 return type;
263 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
265 if (del_timer(&ifp->timer))
266 __in6_ifa_put(ifp);
269 enum addrconf_timer_t
271 AC_NONE,
272 AC_DAD,
273 AC_RS,
276 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
277 enum addrconf_timer_t what,
278 unsigned long when)
280 if (!del_timer(&ifp->timer))
281 in6_ifa_hold(ifp);
283 switch (what) {
284 case AC_DAD:
285 ifp->timer.function = addrconf_dad_timer;
286 break;
287 case AC_RS:
288 ifp->timer.function = addrconf_rs_timer;
289 break;
290 default:;
292 ifp->timer.expires = jiffies + when;
293 add_timer(&ifp->timer);
296 /* Nobody refers to this device, we may destroy it. */
298 void in6_dev_finish_destroy(struct inet6_dev *idev)
300 struct net_device *dev = idev->dev;
301 BUG_TRAP(idev->addr_list==NULL);
302 BUG_TRAP(idev->mc_list==NULL);
303 #ifdef NET_REFCNT_DEBUG
304 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
305 #endif
306 dev_put(dev);
307 if (!idev->dead) {
308 printk("Freeing alive inet6 device %p\n", idev);
309 return;
311 snmp6_free_dev(idev);
312 kfree(idev);
315 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
317 struct inet6_dev *ndev;
319 ASSERT_RTNL();
321 if (dev->mtu < IPV6_MIN_MTU)
322 return NULL;
324 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
326 if (ndev) {
327 memset(ndev, 0, sizeof(struct inet6_dev));
329 rwlock_init(&ndev->lock);
330 ndev->dev = dev;
331 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
332 ndev->cnf.mtu6 = dev->mtu;
333 ndev->cnf.sysctl = NULL;
334 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
335 if (ndev->nd_parms == NULL) {
336 kfree(ndev);
337 return NULL;
339 /* We refer to the device */
340 dev_hold(dev);
342 if (snmp6_alloc_dev(ndev) < 0) {
343 ADBG((KERN_WARNING
344 "%s(): cannot allocate memory for statistics; dev=%s.\n",
345 __FUNCTION__, dev->name));
346 neigh_parms_release(&nd_tbl, ndev->nd_parms);
347 ndev->dead = 1;
348 in6_dev_finish_destroy(ndev);
349 return NULL;
352 if (snmp6_register_dev(ndev) < 0) {
353 ADBG((KERN_WARNING
354 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
355 __FUNCTION__, dev->name));
356 neigh_parms_release(&nd_tbl, ndev->nd_parms);
357 ndev->dead = 1;
358 in6_dev_finish_destroy(ndev);
359 return NULL;
362 /* One reference from device. We must do this before
363 * we invoke __ipv6_regen_rndid().
365 in6_dev_hold(ndev);
367 #ifdef CONFIG_IPV6_PRIVACY
368 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
369 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
370 init_timer(&ndev->regen_timer);
371 ndev->regen_timer.function = ipv6_regen_rndid;
372 ndev->regen_timer.data = (unsigned long) ndev;
373 if ((dev->flags&IFF_LOOPBACK) ||
374 dev->type == ARPHRD_TUNNEL ||
375 dev->type == ARPHRD_NONE ||
376 dev->type == ARPHRD_SIT) {
377 printk(KERN_INFO
378 "Disabled Privacy Extensions on device %p(%s)\n",
379 dev, dev->name);
380 ndev->cnf.use_tempaddr = -1;
381 } else {
382 in6_dev_hold(ndev);
383 ipv6_regen_rndid((unsigned long) ndev);
385 #endif
387 write_lock_bh(&addrconf_lock);
388 dev->ip6_ptr = ndev;
389 write_unlock_bh(&addrconf_lock);
391 ipv6_mc_init_dev(ndev);
392 ndev->tstamp = jiffies;
393 #ifdef CONFIG_SYSCTL
394 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
395 NET_IPV6_NEIGH, "ipv6",
396 &ndisc_ifinfo_sysctl_change,
397 NULL);
398 addrconf_sysctl_register(ndev, &ndev->cnf);
399 #endif
401 return ndev;
404 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
406 struct inet6_dev *idev;
408 ASSERT_RTNL();
410 if ((idev = __in6_dev_get(dev)) == NULL) {
411 if ((idev = ipv6_add_dev(dev)) == NULL)
412 return NULL;
414 if (dev->flags&IFF_UP)
415 ipv6_mc_up(idev);
416 return idev;
419 #ifdef CONFIG_SYSCTL
420 static void dev_forward_change(struct inet6_dev *idev)
422 struct net_device *dev;
423 struct inet6_ifaddr *ifa;
424 struct in6_addr addr;
426 if (!idev)
427 return;
428 dev = idev->dev;
429 if (dev && (dev->flags & IFF_MULTICAST)) {
430 ipv6_addr_all_routers(&addr);
432 if (idev->cnf.forwarding)
433 ipv6_dev_mc_inc(dev, &addr);
434 else
435 ipv6_dev_mc_dec(dev, &addr);
437 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
438 if (idev->cnf.forwarding)
439 addrconf_join_anycast(ifa);
440 else
441 addrconf_leave_anycast(ifa);
446 static void addrconf_forward_change(void)
448 struct net_device *dev;
449 struct inet6_dev *idev;
451 read_lock(&dev_base_lock);
452 for (dev=dev_base; dev; dev=dev->next) {
453 read_lock(&addrconf_lock);
454 idev = __in6_dev_get(dev);
455 if (idev) {
456 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
457 idev->cnf.forwarding = ipv6_devconf.forwarding;
458 if (changed)
459 dev_forward_change(idev);
461 read_unlock(&addrconf_lock);
463 read_unlock(&dev_base_lock);
465 #endif
467 /* Nobody refers to this ifaddr, destroy it */
469 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
471 BUG_TRAP(ifp->if_next==NULL);
472 BUG_TRAP(ifp->lst_next==NULL);
473 #ifdef NET_REFCNT_DEBUG
474 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
475 #endif
477 in6_dev_put(ifp->idev);
479 if (del_timer(&ifp->timer))
480 printk("Timer is still running, when freeing ifa=%p\n", ifp);
482 if (!ifp->dead) {
483 printk("Freeing alive inet6 address %p\n", ifp);
484 return;
486 dst_release(&ifp->rt->u.dst);
488 kfree(ifp);
491 /* On success it returns ifp with increased reference count */
493 static struct inet6_ifaddr *
494 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
495 int scope, u32 flags)
497 struct inet6_ifaddr *ifa = NULL;
498 struct rt6_info *rt;
499 int hash;
500 int err = 0;
502 read_lock_bh(&addrconf_lock);
503 if (idev->dead) {
504 err = -ENODEV; /*XXX*/
505 goto out2;
508 write_lock(&addrconf_hash_lock);
510 /* Ignore adding duplicate addresses on an interface */
511 if (ipv6_chk_same_addr(addr, idev->dev)) {
512 ADBG(("ipv6_add_addr: already assigned\n"));
513 err = -EEXIST;
514 goto out;
517 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
519 if (ifa == NULL) {
520 ADBG(("ipv6_add_addr: malloc failed\n"));
521 err = -ENOBUFS;
522 goto out;
525 rt = addrconf_dst_alloc(idev, addr, 0);
526 if (IS_ERR(rt)) {
527 err = PTR_ERR(rt);
528 goto out;
531 memset(ifa, 0, sizeof(struct inet6_ifaddr));
532 ipv6_addr_copy(&ifa->addr, addr);
534 spin_lock_init(&ifa->lock);
535 init_timer(&ifa->timer);
536 ifa->timer.data = (unsigned long) ifa;
537 ifa->scope = scope;
538 ifa->prefix_len = pfxlen;
539 ifa->flags = flags | IFA_F_TENTATIVE;
540 ifa->cstamp = ifa->tstamp = jiffies;
542 ifa->idev = idev;
543 in6_dev_hold(idev);
544 /* For caller */
545 in6_ifa_hold(ifa);
547 /* Add to big hash table */
548 hash = ipv6_addr_hash(addr);
550 ifa->lst_next = inet6_addr_lst[hash];
551 inet6_addr_lst[hash] = ifa;
552 in6_ifa_hold(ifa);
553 write_unlock(&addrconf_hash_lock);
555 write_lock(&idev->lock);
556 /* Add to inet6_dev unicast addr list. */
557 ifa->if_next = idev->addr_list;
558 idev->addr_list = ifa;
560 #ifdef CONFIG_IPV6_PRIVACY
561 if (ifa->flags&IFA_F_TEMPORARY) {
562 ifa->tmp_next = idev->tempaddr_list;
563 idev->tempaddr_list = ifa;
564 in6_ifa_hold(ifa);
566 #endif
568 ifa->rt = rt;
570 in6_ifa_hold(ifa);
571 write_unlock(&idev->lock);
572 out2:
573 read_unlock_bh(&addrconf_lock);
575 if (likely(err == 0))
576 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
577 else {
578 kfree(ifa);
579 ifa = ERR_PTR(err);
582 return ifa;
583 out:
584 write_unlock(&addrconf_hash_lock);
585 goto out2;
588 /* This function wants to get referenced ifp and releases it before return */
590 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
592 struct inet6_ifaddr *ifa, **ifap;
593 struct inet6_dev *idev = ifp->idev;
594 int hash;
595 int deleted = 0, onlink = 0;
596 unsigned long expires = jiffies;
598 hash = ipv6_addr_hash(&ifp->addr);
600 ifp->dead = 1;
602 write_lock_bh(&addrconf_hash_lock);
603 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
604 ifap = &ifa->lst_next) {
605 if (ifa == ifp) {
606 *ifap = ifa->lst_next;
607 __in6_ifa_put(ifp);
608 ifa->lst_next = NULL;
609 break;
612 write_unlock_bh(&addrconf_hash_lock);
614 write_lock_bh(&idev->lock);
615 #ifdef CONFIG_IPV6_PRIVACY
616 if (ifp->flags&IFA_F_TEMPORARY) {
617 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
618 ifap = &ifa->tmp_next) {
619 if (ifa == ifp) {
620 *ifap = ifa->tmp_next;
621 if (ifp->ifpub) {
622 in6_ifa_put(ifp->ifpub);
623 ifp->ifpub = NULL;
625 __in6_ifa_put(ifp);
626 ifa->tmp_next = NULL;
627 break;
631 #endif
633 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
634 ifap = &ifa->if_next) {
635 if (ifa == ifp) {
636 *ifap = ifa->if_next;
637 __in6_ifa_put(ifp);
638 ifa->if_next = NULL;
639 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
640 break;
641 deleted = 1;
642 } else if (ifp->flags & IFA_F_PERMANENT) {
643 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
644 ifp->prefix_len)) {
645 if (ifa->flags & IFA_F_PERMANENT) {
646 onlink = 1;
647 if (deleted)
648 break;
649 } else {
650 unsigned long lifetime;
652 if (!onlink)
653 onlink = -1;
655 spin_lock(&ifa->lock);
656 lifetime = min_t(unsigned long,
657 ifa->valid_lft, 0x7fffffffUL/HZ);
658 if (time_before(expires,
659 ifa->tstamp + lifetime * HZ))
660 expires = ifa->tstamp + lifetime * HZ;
661 spin_unlock(&ifa->lock);
666 write_unlock_bh(&idev->lock);
668 ipv6_ifa_notify(RTM_DELADDR, ifp);
670 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
672 addrconf_del_timer(ifp);
675 * Purge or update corresponding prefix
677 * 1) we don't purge prefix here if address was not permanent.
678 * prefix is managed by its own lifetime.
679 * 2) if there're no addresses, delete prefix.
680 * 3) if there're still other permanent address(es),
681 * corresponding prefix is still permanent.
682 * 4) otherwise, update prefix lifetime to the
683 * longest valid lifetime among the corresponding
684 * addresses on the device.
685 * Note: subsequent RA will update lifetime.
687 * --yoshfuji
689 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
690 struct in6_addr prefix;
691 struct rt6_info *rt;
693 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
694 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
696 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
697 if (onlink == 0) {
698 ip6_del_rt(rt, NULL, NULL, NULL);
699 rt = NULL;
700 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
701 rt->rt6i_expires = expires;
702 rt->rt6i_flags |= RTF_EXPIRES;
705 dst_release(&rt->u.dst);
708 in6_ifa_put(ifp);
711 #ifdef CONFIG_IPV6_PRIVACY
712 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
714 struct inet6_dev *idev = ifp->idev;
715 struct in6_addr addr, *tmpaddr;
716 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
717 int tmp_plen;
718 int ret = 0;
719 int max_addresses;
721 write_lock(&idev->lock);
722 if (ift) {
723 spin_lock_bh(&ift->lock);
724 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
725 spin_unlock_bh(&ift->lock);
726 tmpaddr = &addr;
727 } else {
728 tmpaddr = NULL;
730 retry:
731 in6_dev_hold(idev);
732 if (idev->cnf.use_tempaddr <= 0) {
733 write_unlock(&idev->lock);
734 printk(KERN_INFO
735 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
736 in6_dev_put(idev);
737 ret = -1;
738 goto out;
740 spin_lock_bh(&ifp->lock);
741 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
742 idev->cnf.use_tempaddr = -1; /*XXX*/
743 spin_unlock_bh(&ifp->lock);
744 write_unlock(&idev->lock);
745 printk(KERN_WARNING
746 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
747 in6_dev_put(idev);
748 ret = -1;
749 goto out;
751 in6_ifa_hold(ifp);
752 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
753 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
754 spin_unlock_bh(&ifp->lock);
755 write_unlock(&idev->lock);
756 printk(KERN_WARNING
757 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
758 in6_ifa_put(ifp);
759 in6_dev_put(idev);
760 ret = -1;
761 goto out;
763 memcpy(&addr.s6_addr[8], idev->rndid, 8);
764 tmp_valid_lft = min_t(__u32,
765 ifp->valid_lft,
766 idev->cnf.temp_valid_lft);
767 tmp_prefered_lft = min_t(__u32,
768 ifp->prefered_lft,
769 idev->cnf.temp_prefered_lft - desync_factor / HZ);
770 tmp_plen = ifp->prefix_len;
771 max_addresses = idev->cnf.max_addresses;
772 tmp_cstamp = ifp->cstamp;
773 tmp_tstamp = ifp->tstamp;
774 spin_unlock_bh(&ifp->lock);
776 write_unlock(&idev->lock);
777 ift = !max_addresses ||
778 ipv6_count_addresses(idev) < max_addresses ?
779 ipv6_add_addr(idev, &addr, tmp_plen,
780 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
781 if (!ift || IS_ERR(ift)) {
782 in6_ifa_put(ifp);
783 in6_dev_put(idev);
784 printk(KERN_INFO
785 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
786 tmpaddr = &addr;
787 write_lock(&idev->lock);
788 goto retry;
791 spin_lock_bh(&ift->lock);
792 ift->ifpub = ifp;
793 ift->valid_lft = tmp_valid_lft;
794 ift->prefered_lft = tmp_prefered_lft;
795 ift->cstamp = tmp_cstamp;
796 ift->tstamp = tmp_tstamp;
797 spin_unlock_bh(&ift->lock);
799 addrconf_dad_start(ift, 0);
800 in6_ifa_put(ift);
801 in6_dev_put(idev);
802 out:
803 return ret;
805 #endif
808 * Choose an appropriate source address
809 * should do:
810 * i) get an address with an appropriate scope
811 * ii) see if there is a specific route for the destination and use
812 * an address of the attached interface
813 * iii) don't use deprecated addresses
815 static int inline ipv6_saddr_pref(const struct inet6_ifaddr *ifp, u8 invpref)
817 int pref;
818 pref = ifp->flags&IFA_F_DEPRECATED ? 0 : 2;
819 #ifdef CONFIG_IPV6_PRIVACY
820 pref |= (ifp->flags^invpref)&IFA_F_TEMPORARY ? 0 : 1;
821 #endif
822 return pref;
825 #ifdef CONFIG_IPV6_PRIVACY
826 #define IPV6_GET_SADDR_MAXSCORE(score) ((score) == 3)
827 #else
828 #define IPV6_GET_SADDR_MAXSCORE(score) (score)
829 #endif
831 int ipv6_dev_get_saddr(struct net_device *dev,
832 struct in6_addr *daddr, struct in6_addr *saddr)
834 struct inet6_ifaddr *ifp = NULL;
835 struct inet6_ifaddr *match = NULL;
836 struct inet6_dev *idev;
837 int scope;
838 int err;
839 int hiscore = -1, score;
841 scope = ipv6_addr_scope(daddr);
844 * known dev
845 * search dev and walk through dev addresses
848 if (dev) {
849 if (dev->flags & IFF_LOOPBACK)
850 scope = IFA_HOST;
852 read_lock(&addrconf_lock);
853 idev = __in6_dev_get(dev);
854 if (idev) {
855 read_lock_bh(&idev->lock);
856 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
857 if (ifp->scope == scope) {
858 if (ifp->flags&IFA_F_TENTATIVE)
859 continue;
860 #ifdef CONFIG_IPV6_PRIVACY
861 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
862 #else
863 score = ipv6_saddr_pref(ifp, 0);
864 #endif
865 if (score <= hiscore)
866 continue;
868 if (match)
869 in6_ifa_put(match);
870 match = ifp;
871 hiscore = score;
872 in6_ifa_hold(ifp);
874 if (IPV6_GET_SADDR_MAXSCORE(score)) {
875 read_unlock_bh(&idev->lock);
876 read_unlock(&addrconf_lock);
877 goto out;
881 read_unlock_bh(&idev->lock);
883 read_unlock(&addrconf_lock);
886 if (scope == IFA_LINK)
887 goto out;
890 * dev == NULL or search failed for specified dev
893 read_lock(&dev_base_lock);
894 read_lock(&addrconf_lock);
895 for (dev = dev_base; dev; dev=dev->next) {
896 idev = __in6_dev_get(dev);
897 if (idev) {
898 read_lock_bh(&idev->lock);
899 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
900 if (ifp->scope == scope) {
901 if (ifp->flags&IFA_F_TENTATIVE)
902 continue;
903 #ifdef CONFIG_IPV6_PRIVACY
904 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
905 #else
906 score = ipv6_saddr_pref(ifp, 0);
907 #endif
908 if (score <= hiscore)
909 continue;
911 if (match)
912 in6_ifa_put(match);
913 match = ifp;
914 hiscore = score;
915 in6_ifa_hold(ifp);
917 if (IPV6_GET_SADDR_MAXSCORE(score)) {
918 read_unlock_bh(&idev->lock);
919 goto out_unlock_base;
923 read_unlock_bh(&idev->lock);
927 out_unlock_base:
928 read_unlock(&addrconf_lock);
929 read_unlock(&dev_base_lock);
931 out:
932 err = -EADDRNOTAVAIL;
933 if (match) {
934 ipv6_addr_copy(saddr, &match->addr);
935 err = 0;
936 in6_ifa_put(match);
939 return err;
943 int ipv6_get_saddr(struct dst_entry *dst,
944 struct in6_addr *daddr, struct in6_addr *saddr)
946 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
950 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
952 struct inet6_dev *idev;
953 int err = -EADDRNOTAVAIL;
955 read_lock(&addrconf_lock);
956 if ((idev = __in6_dev_get(dev)) != NULL) {
957 struct inet6_ifaddr *ifp;
959 read_lock_bh(&idev->lock);
960 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
961 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
962 ipv6_addr_copy(addr, &ifp->addr);
963 err = 0;
964 break;
967 read_unlock_bh(&idev->lock);
969 read_unlock(&addrconf_lock);
970 return err;
973 static int ipv6_count_addresses(struct inet6_dev *idev)
975 int cnt = 0;
976 struct inet6_ifaddr *ifp;
978 read_lock_bh(&idev->lock);
979 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
980 cnt++;
981 read_unlock_bh(&idev->lock);
982 return cnt;
985 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
987 struct inet6_ifaddr * ifp;
988 u8 hash = ipv6_addr_hash(addr);
990 read_lock_bh(&addrconf_hash_lock);
991 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
992 if (ipv6_addr_equal(&ifp->addr, addr) &&
993 !(ifp->flags&IFA_F_TENTATIVE)) {
994 if (dev == NULL || ifp->idev->dev == dev ||
995 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
996 break;
999 read_unlock_bh(&addrconf_hash_lock);
1000 return ifp != NULL;
1003 static
1004 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1006 struct inet6_ifaddr * ifp;
1007 u8 hash = ipv6_addr_hash(addr);
1009 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1010 if (ipv6_addr_equal(&ifp->addr, addr)) {
1011 if (dev == NULL || ifp->idev->dev == dev)
1012 break;
1015 return ifp != NULL;
1018 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1020 struct inet6_ifaddr * ifp;
1021 u8 hash = ipv6_addr_hash(addr);
1023 read_lock_bh(&addrconf_hash_lock);
1024 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1025 if (ipv6_addr_equal(&ifp->addr, addr)) {
1026 if (dev == NULL || ifp->idev->dev == dev ||
1027 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1028 in6_ifa_hold(ifp);
1029 break;
1033 read_unlock_bh(&addrconf_hash_lock);
1035 return ifp;
1038 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1040 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1041 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
1042 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1043 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1044 int sk_ipv6only = ipv6_only_sock(sk);
1045 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1046 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1047 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1049 if (!sk2_rcv_saddr && !sk_ipv6only)
1050 return 1;
1052 if (addr_type2 == IPV6_ADDR_ANY &&
1053 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1054 return 1;
1056 if (addr_type == IPV6_ADDR_ANY &&
1057 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1058 return 1;
1060 if (sk2_rcv_saddr6 &&
1061 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1062 return 1;
1064 if (addr_type == IPV6_ADDR_MAPPED &&
1065 !sk2_ipv6only &&
1066 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1067 return 1;
1069 return 0;
1072 /* Gets referenced address, destroys ifaddr */
1074 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1076 if (net_ratelimit())
1077 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1078 if (ifp->flags&IFA_F_PERMANENT) {
1079 spin_lock_bh(&ifp->lock);
1080 addrconf_del_timer(ifp);
1081 ifp->flags |= IFA_F_TENTATIVE;
1082 spin_unlock_bh(&ifp->lock);
1083 in6_ifa_put(ifp);
1084 #ifdef CONFIG_IPV6_PRIVACY
1085 } else if (ifp->flags&IFA_F_TEMPORARY) {
1086 struct inet6_ifaddr *ifpub;
1087 spin_lock_bh(&ifp->lock);
1088 ifpub = ifp->ifpub;
1089 if (ifpub) {
1090 in6_ifa_hold(ifpub);
1091 spin_unlock_bh(&ifp->lock);
1092 ipv6_create_tempaddr(ifpub, ifp);
1093 in6_ifa_put(ifpub);
1094 } else {
1095 spin_unlock_bh(&ifp->lock);
1097 ipv6_del_addr(ifp);
1098 #endif
1099 } else
1100 ipv6_del_addr(ifp);
1104 /* Join to solicited addr multicast group. */
1106 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1108 struct in6_addr maddr;
1110 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1111 return;
1113 addrconf_addr_solict_mult(addr, &maddr);
1114 ipv6_dev_mc_inc(dev, &maddr);
1117 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1119 struct in6_addr maddr;
1121 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1122 return;
1124 addrconf_addr_solict_mult(addr, &maddr);
1125 __ipv6_dev_mc_dec(idev, &maddr);
1128 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1130 struct in6_addr addr;
1131 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1132 if (ipv6_addr_any(&addr))
1133 return;
1134 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1137 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1139 struct in6_addr addr;
1140 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1141 if (ipv6_addr_any(&addr))
1142 return;
1143 __ipv6_dev_ac_dec(ifp->idev, &addr);
1146 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1148 switch (dev->type) {
1149 case ARPHRD_ETHER:
1150 case ARPHRD_FDDI:
1151 case ARPHRD_IEEE802_TR:
1152 if (dev->addr_len != ETH_ALEN)
1153 return -1;
1154 memcpy(eui, dev->dev_addr, 3);
1155 memcpy(eui + 5, dev->dev_addr + 3, 3);
1158 * The zSeries OSA network cards can be shared among various
1159 * OS instances, but the OSA cards have only one MAC address.
1160 * This leads to duplicate address conflicts in conjunction
1161 * with IPv6 if more than one instance uses the same card.
1163 * The driver for these cards can deliver a unique 16-bit
1164 * identifier for each instance sharing the same card. It is
1165 * placed instead of 0xFFFE in the interface identifier. The
1166 * "u" bit of the interface identifier is not inverted in this
1167 * case. Hence the resulting interface identifier has local
1168 * scope according to RFC2373.
1170 if (dev->dev_id) {
1171 eui[3] = (dev->dev_id >> 8) & 0xFF;
1172 eui[4] = dev->dev_id & 0xFF;
1173 } else {
1174 eui[3] = 0xFF;
1175 eui[4] = 0xFE;
1176 eui[0] ^= 2;
1178 return 0;
1179 case ARPHRD_ARCNET:
1180 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1181 if (dev->addr_len != ARCNET_ALEN)
1182 return -1;
1183 memset(eui, 0, 7);
1184 eui[7] = *(u8*)dev->dev_addr;
1185 return 0;
1186 case ARPHRD_INFINIBAND:
1187 if (dev->addr_len != INFINIBAND_ALEN)
1188 return -1;
1189 memcpy(eui, dev->dev_addr + 12, 8);
1190 eui[0] |= 2;
1191 return 0;
1193 return -1;
1196 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1198 int err = -1;
1199 struct inet6_ifaddr *ifp;
1201 read_lock_bh(&idev->lock);
1202 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1203 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1204 memcpy(eui, ifp->addr.s6_addr+8, 8);
1205 err = 0;
1206 break;
1209 read_unlock_bh(&idev->lock);
1210 return err;
1213 #ifdef CONFIG_IPV6_PRIVACY
1214 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1215 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1217 struct net_device *dev;
1218 struct scatterlist sg[2];
1220 sg_set_buf(&sg[0], idev->entropy, 8);
1221 sg_set_buf(&sg[1], idev->work_eui64, 8);
1223 dev = idev->dev;
1225 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1226 printk(KERN_INFO
1227 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1228 idev);
1229 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1231 regen:
1232 spin_lock(&md5_tfm_lock);
1233 if (unlikely(md5_tfm == NULL)) {
1234 spin_unlock(&md5_tfm_lock);
1235 return -1;
1237 crypto_digest_init(md5_tfm);
1238 crypto_digest_update(md5_tfm, sg, 2);
1239 crypto_digest_final(md5_tfm, idev->work_digest);
1240 spin_unlock(&md5_tfm_lock);
1242 memcpy(idev->rndid, &idev->work_digest[0], 8);
1243 idev->rndid[0] &= ~0x02;
1244 memcpy(idev->entropy, &idev->work_digest[8], 8);
1247 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1248 * check if generated address is not inappropriate
1250 * - Reserved subnet anycast (RFC 2526)
1251 * 11111101 11....11 1xxxxxxx
1252 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1253 * 00-00-5E-FE-xx-xx-xx-xx
1254 * - value 0
1255 * - XXX: already assigned to an address on the device
1257 if (idev->rndid[0] == 0xfd &&
1258 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1259 (idev->rndid[7]&0x80))
1260 goto regen;
1261 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1262 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1263 goto regen;
1264 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1265 goto regen;
1268 return 0;
1271 static void ipv6_regen_rndid(unsigned long data)
1273 struct inet6_dev *idev = (struct inet6_dev *) data;
1274 unsigned long expires;
1276 read_lock_bh(&addrconf_lock);
1277 write_lock_bh(&idev->lock);
1279 if (idev->dead)
1280 goto out;
1282 if (__ipv6_regen_rndid(idev) < 0)
1283 goto out;
1285 expires = jiffies +
1286 idev->cnf.temp_prefered_lft * HZ -
1287 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1288 if (time_before(expires, jiffies)) {
1289 printk(KERN_WARNING
1290 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1291 idev->dev->name);
1292 goto out;
1295 if (!mod_timer(&idev->regen_timer, expires))
1296 in6_dev_hold(idev);
1298 out:
1299 write_unlock_bh(&idev->lock);
1300 read_unlock_bh(&addrconf_lock);
1301 in6_dev_put(idev);
1304 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1305 int ret = 0;
1307 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1308 ret = __ipv6_regen_rndid(idev);
1309 return ret;
1311 #endif
1314 * Add prefix route.
1317 static void
1318 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1319 unsigned long expires, u32 flags)
1321 struct in6_rtmsg rtmsg;
1323 memset(&rtmsg, 0, sizeof(rtmsg));
1324 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1325 rtmsg.rtmsg_dst_len = plen;
1326 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1327 rtmsg.rtmsg_ifindex = dev->ifindex;
1328 rtmsg.rtmsg_info = expires;
1329 rtmsg.rtmsg_flags = RTF_UP|flags;
1330 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1332 /* Prevent useless cloning on PtP SIT.
1333 This thing is done here expecting that the whole
1334 class of non-broadcast devices need not cloning.
1336 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1337 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1339 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1342 /* Create "default" multicast route to the interface */
1344 static void addrconf_add_mroute(struct net_device *dev)
1346 struct in6_rtmsg rtmsg;
1348 memset(&rtmsg, 0, sizeof(rtmsg));
1349 ipv6_addr_set(&rtmsg.rtmsg_dst,
1350 htonl(0xFF000000), 0, 0, 0);
1351 rtmsg.rtmsg_dst_len = 8;
1352 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1353 rtmsg.rtmsg_ifindex = dev->ifindex;
1354 rtmsg.rtmsg_flags = RTF_UP;
1355 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1356 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1359 static void sit_route_add(struct net_device *dev)
1361 struct in6_rtmsg rtmsg;
1363 memset(&rtmsg, 0, sizeof(rtmsg));
1365 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1366 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1368 /* prefix length - 96 bits "::d.d.d.d" */
1369 rtmsg.rtmsg_dst_len = 96;
1370 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1371 rtmsg.rtmsg_ifindex = dev->ifindex;
1373 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1376 static void addrconf_add_lroute(struct net_device *dev)
1378 struct in6_addr addr;
1380 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1381 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1384 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1386 struct inet6_dev *idev;
1388 ASSERT_RTNL();
1390 if ((idev = ipv6_find_idev(dev)) == NULL)
1391 return NULL;
1393 /* Add default multicast route */
1394 addrconf_add_mroute(dev);
1396 /* Add link local route */
1397 addrconf_add_lroute(dev);
1398 return idev;
1401 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1403 struct prefix_info *pinfo;
1404 __u32 valid_lft;
1405 __u32 prefered_lft;
1406 int addr_type;
1407 unsigned long rt_expires;
1408 struct inet6_dev *in6_dev;
1410 pinfo = (struct prefix_info *) opt;
1412 if (len < sizeof(struct prefix_info)) {
1413 ADBG(("addrconf: prefix option too short\n"));
1414 return;
1418 * Validation checks ([ADDRCONF], page 19)
1421 addr_type = ipv6_addr_type(&pinfo->prefix);
1423 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1424 return;
1426 valid_lft = ntohl(pinfo->valid);
1427 prefered_lft = ntohl(pinfo->prefered);
1429 if (prefered_lft > valid_lft) {
1430 if (net_ratelimit())
1431 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1432 return;
1435 in6_dev = in6_dev_get(dev);
1437 if (in6_dev == NULL) {
1438 if (net_ratelimit())
1439 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1440 return;
1444 * Two things going on here:
1445 * 1) Add routes for on-link prefixes
1446 * 2) Configure prefixes with the auto flag set
1449 /* Avoid arithmetic overflow. Really, we could
1450 save rt_expires in seconds, likely valid_lft,
1451 but it would require division in fib gc, that it
1452 not good.
1454 if (valid_lft >= 0x7FFFFFFF/HZ)
1455 rt_expires = 0;
1456 else
1457 rt_expires = jiffies + valid_lft * HZ;
1459 if (pinfo->onlink) {
1460 struct rt6_info *rt;
1461 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1463 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1464 if (rt->rt6i_flags&RTF_EXPIRES) {
1465 if (valid_lft == 0) {
1466 ip6_del_rt(rt, NULL, NULL, NULL);
1467 rt = NULL;
1468 } else {
1469 rt->rt6i_expires = rt_expires;
1472 } else if (valid_lft) {
1473 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1474 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1476 if (rt)
1477 dst_release(&rt->u.dst);
1480 /* Try to figure out our local address for this prefix */
1482 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1483 struct inet6_ifaddr * ifp;
1484 struct in6_addr addr;
1485 int create = 0, update_lft = 0;
1487 if (pinfo->prefix_len == 64) {
1488 memcpy(&addr, &pinfo->prefix, 8);
1489 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1490 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1491 in6_dev_put(in6_dev);
1492 return;
1494 goto ok;
1496 if (net_ratelimit())
1497 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1498 pinfo->prefix_len);
1499 in6_dev_put(in6_dev);
1500 return;
1504 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1506 if (ifp == NULL && valid_lft) {
1507 int max_addresses = in6_dev->cnf.max_addresses;
1509 /* Do not allow to create too much of autoconfigured
1510 * addresses; this would be too easy way to crash kernel.
1512 if (!max_addresses ||
1513 ipv6_count_addresses(in6_dev) < max_addresses)
1514 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1515 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1517 if (!ifp || IS_ERR(ifp)) {
1518 in6_dev_put(in6_dev);
1519 return;
1522 update_lft = create = 1;
1523 ifp->cstamp = jiffies;
1524 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1527 if (ifp) {
1528 int flags;
1529 unsigned long now;
1530 #ifdef CONFIG_IPV6_PRIVACY
1531 struct inet6_ifaddr *ift;
1532 #endif
1533 u32 stored_lft;
1535 /* update lifetime (RFC2462 5.5.3 e) */
1536 spin_lock(&ifp->lock);
1537 now = jiffies;
1538 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1539 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1540 else
1541 stored_lft = 0;
1542 if (!update_lft && stored_lft) {
1543 if (valid_lft > MIN_VALID_LIFETIME ||
1544 valid_lft > stored_lft)
1545 update_lft = 1;
1546 else if (stored_lft <= MIN_VALID_LIFETIME) {
1547 /* valid_lft <= stored_lft is always true */
1548 /* XXX: IPsec */
1549 update_lft = 0;
1550 } else {
1551 valid_lft = MIN_VALID_LIFETIME;
1552 if (valid_lft < prefered_lft)
1553 prefered_lft = valid_lft;
1554 update_lft = 1;
1558 if (update_lft) {
1559 ifp->valid_lft = valid_lft;
1560 ifp->prefered_lft = prefered_lft;
1561 ifp->tstamp = now;
1562 flags = ifp->flags;
1563 ifp->flags &= ~IFA_F_DEPRECATED;
1564 spin_unlock(&ifp->lock);
1566 if (!(flags&IFA_F_TENTATIVE))
1567 ipv6_ifa_notify(0, ifp);
1568 } else
1569 spin_unlock(&ifp->lock);
1571 #ifdef CONFIG_IPV6_PRIVACY
1572 read_lock_bh(&in6_dev->lock);
1573 /* update all temporary addresses in the list */
1574 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1576 * When adjusting the lifetimes of an existing
1577 * temporary address, only lower the lifetimes.
1578 * Implementations must not increase the
1579 * lifetimes of an existing temporary address
1580 * when processing a Prefix Information Option.
1582 spin_lock(&ift->lock);
1583 flags = ift->flags;
1584 if (ift->valid_lft > valid_lft &&
1585 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1586 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1587 if (ift->prefered_lft > prefered_lft &&
1588 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1589 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1590 spin_unlock(&ift->lock);
1591 if (!(flags&IFA_F_TENTATIVE))
1592 ipv6_ifa_notify(0, ift);
1595 if (create && in6_dev->cnf.use_tempaddr > 0) {
1597 * When a new public address is created as described in [ADDRCONF],
1598 * also create a new temporary address.
1600 read_unlock_bh(&in6_dev->lock);
1601 ipv6_create_tempaddr(ifp, NULL);
1602 } else {
1603 read_unlock_bh(&in6_dev->lock);
1605 #endif
1606 in6_ifa_put(ifp);
1607 addrconf_verify(0);
1610 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1611 in6_dev_put(in6_dev);
1615 * Set destination address.
1616 * Special case for SIT interfaces where we create a new "virtual"
1617 * device.
1619 int addrconf_set_dstaddr(void __user *arg)
1621 struct in6_ifreq ireq;
1622 struct net_device *dev;
1623 int err = -EINVAL;
1625 rtnl_lock();
1627 err = -EFAULT;
1628 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1629 goto err_exit;
1631 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1633 err = -ENODEV;
1634 if (dev == NULL)
1635 goto err_exit;
1637 if (dev->type == ARPHRD_SIT) {
1638 struct ifreq ifr;
1639 mm_segment_t oldfs;
1640 struct ip_tunnel_parm p;
1642 err = -EADDRNOTAVAIL;
1643 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1644 goto err_exit;
1646 memset(&p, 0, sizeof(p));
1647 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1648 p.iph.saddr = 0;
1649 p.iph.version = 4;
1650 p.iph.ihl = 5;
1651 p.iph.protocol = IPPROTO_IPV6;
1652 p.iph.ttl = 64;
1653 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1655 oldfs = get_fs(); set_fs(KERNEL_DS);
1656 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1657 set_fs(oldfs);
1659 if (err == 0) {
1660 err = -ENOBUFS;
1661 if ((dev = __dev_get_by_name(p.name)) == NULL)
1662 goto err_exit;
1663 err = dev_open(dev);
1667 err_exit:
1668 rtnl_unlock();
1669 return err;
1673 * Manual configuration of address on an interface
1675 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1677 struct inet6_ifaddr *ifp;
1678 struct inet6_dev *idev;
1679 struct net_device *dev;
1680 int scope;
1682 ASSERT_RTNL();
1684 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1685 return -ENODEV;
1687 if (!(dev->flags&IFF_UP))
1688 return -ENETDOWN;
1690 if ((idev = addrconf_add_dev(dev)) == NULL)
1691 return -ENOBUFS;
1693 scope = ipv6_addr_scope(pfx);
1695 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1696 if (!IS_ERR(ifp)) {
1697 addrconf_dad_start(ifp, 0);
1698 in6_ifa_put(ifp);
1699 return 0;
1702 return PTR_ERR(ifp);
1705 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1707 struct inet6_ifaddr *ifp;
1708 struct inet6_dev *idev;
1709 struct net_device *dev;
1711 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1712 return -ENODEV;
1714 if ((idev = __in6_dev_get(dev)) == NULL)
1715 return -ENXIO;
1717 read_lock_bh(&idev->lock);
1718 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1719 if (ifp->prefix_len == plen &&
1720 ipv6_addr_equal(pfx, &ifp->addr)) {
1721 in6_ifa_hold(ifp);
1722 read_unlock_bh(&idev->lock);
1724 ipv6_del_addr(ifp);
1726 /* If the last address is deleted administratively,
1727 disable IPv6 on this interface.
1729 if (idev->addr_list == NULL)
1730 addrconf_ifdown(idev->dev, 1);
1731 return 0;
1734 read_unlock_bh(&idev->lock);
1735 return -EADDRNOTAVAIL;
1739 int addrconf_add_ifaddr(void __user *arg)
1741 struct in6_ifreq ireq;
1742 int err;
1744 if (!capable(CAP_NET_ADMIN))
1745 return -EPERM;
1747 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1748 return -EFAULT;
1750 rtnl_lock();
1751 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1752 rtnl_unlock();
1753 return err;
1756 int addrconf_del_ifaddr(void __user *arg)
1758 struct in6_ifreq ireq;
1759 int err;
1761 if (!capable(CAP_NET_ADMIN))
1762 return -EPERM;
1764 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1765 return -EFAULT;
1767 rtnl_lock();
1768 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1769 rtnl_unlock();
1770 return err;
1773 static void sit_add_v4_addrs(struct inet6_dev *idev)
1775 struct inet6_ifaddr * ifp;
1776 struct in6_addr addr;
1777 struct net_device *dev;
1778 int scope;
1780 ASSERT_RTNL();
1782 memset(&addr, 0, sizeof(struct in6_addr));
1783 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1785 if (idev->dev->flags&IFF_POINTOPOINT) {
1786 addr.s6_addr32[0] = htonl(0xfe800000);
1787 scope = IFA_LINK;
1788 } else {
1789 scope = IPV6_ADDR_COMPATv4;
1792 if (addr.s6_addr32[3]) {
1793 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1794 if (!IS_ERR(ifp)) {
1795 spin_lock_bh(&ifp->lock);
1796 ifp->flags &= ~IFA_F_TENTATIVE;
1797 spin_unlock_bh(&ifp->lock);
1798 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1799 in6_ifa_put(ifp);
1801 return;
1804 for (dev = dev_base; dev != NULL; dev = dev->next) {
1805 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1806 if (in_dev && (dev->flags & IFF_UP)) {
1807 struct in_ifaddr * ifa;
1809 int flag = scope;
1811 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1812 int plen;
1814 addr.s6_addr32[3] = ifa->ifa_local;
1816 if (ifa->ifa_scope == RT_SCOPE_LINK)
1817 continue;
1818 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1819 if (idev->dev->flags&IFF_POINTOPOINT)
1820 continue;
1821 flag |= IFA_HOST;
1823 if (idev->dev->flags&IFF_POINTOPOINT)
1824 plen = 64;
1825 else
1826 plen = 96;
1828 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1829 IFA_F_PERMANENT);
1830 if (!IS_ERR(ifp)) {
1831 spin_lock_bh(&ifp->lock);
1832 ifp->flags &= ~IFA_F_TENTATIVE;
1833 spin_unlock_bh(&ifp->lock);
1834 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1835 in6_ifa_put(ifp);
1842 static void init_loopback(struct net_device *dev)
1844 struct inet6_dev *idev;
1845 struct inet6_ifaddr * ifp;
1847 /* ::1 */
1849 ASSERT_RTNL();
1851 if ((idev = ipv6_find_idev(dev)) == NULL) {
1852 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1853 return;
1856 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1857 if (!IS_ERR(ifp)) {
1858 spin_lock_bh(&ifp->lock);
1859 ifp->flags &= ~IFA_F_TENTATIVE;
1860 spin_unlock_bh(&ifp->lock);
1861 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1862 in6_ifa_put(ifp);
1866 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1868 struct inet6_ifaddr * ifp;
1870 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1871 if (!IS_ERR(ifp)) {
1872 addrconf_dad_start(ifp, 0);
1873 in6_ifa_put(ifp);
1877 static void addrconf_dev_config(struct net_device *dev)
1879 struct in6_addr addr;
1880 struct inet6_dev * idev;
1882 ASSERT_RTNL();
1884 if ((dev->type != ARPHRD_ETHER) &&
1885 (dev->type != ARPHRD_FDDI) &&
1886 (dev->type != ARPHRD_IEEE802_TR) &&
1887 (dev->type != ARPHRD_ARCNET) &&
1888 (dev->type != ARPHRD_INFINIBAND)) {
1889 /* Alas, we support only Ethernet autoconfiguration. */
1890 return;
1893 idev = addrconf_add_dev(dev);
1894 if (idev == NULL)
1895 return;
1897 memset(&addr, 0, sizeof(struct in6_addr));
1898 addr.s6_addr32[0] = htonl(0xFE800000);
1900 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1901 addrconf_add_linklocal(idev, &addr);
1904 static void addrconf_sit_config(struct net_device *dev)
1906 struct inet6_dev *idev;
1908 ASSERT_RTNL();
1911 * Configure the tunnel with one of our IPv4
1912 * addresses... we should configure all of
1913 * our v4 addrs in the tunnel
1916 if ((idev = ipv6_find_idev(dev)) == NULL) {
1917 printk(KERN_DEBUG "init sit: add_dev failed\n");
1918 return;
1921 sit_add_v4_addrs(idev);
1923 if (dev->flags&IFF_POINTOPOINT) {
1924 addrconf_add_mroute(dev);
1925 addrconf_add_lroute(dev);
1926 } else
1927 sit_route_add(dev);
1930 static inline int
1931 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
1933 struct in6_addr lladdr;
1935 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
1936 addrconf_add_linklocal(idev, &lladdr);
1937 return 0;
1939 return -1;
1942 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
1944 struct net_device *link_dev;
1946 /* first try to inherit the link-local address from the link device */
1947 if (idev->dev->iflink &&
1948 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
1949 if (!ipv6_inherit_linklocal(idev, link_dev))
1950 return;
1952 /* then try to inherit it from any device */
1953 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
1954 if (!ipv6_inherit_linklocal(idev, link_dev))
1955 return;
1957 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
1961 * Autoconfigure tunnel with a link-local address so routing protocols,
1962 * DHCPv6, MLD etc. can be run over the virtual link
1965 static void addrconf_ip6_tnl_config(struct net_device *dev)
1967 struct inet6_dev *idev;
1969 ASSERT_RTNL();
1971 if ((idev = addrconf_add_dev(dev)) == NULL) {
1972 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
1973 return;
1975 ip6_tnl_add_linklocal(idev);
1976 addrconf_add_mroute(dev);
1979 static int addrconf_notify(struct notifier_block *this, unsigned long event,
1980 void * data)
1982 struct net_device *dev = (struct net_device *) data;
1983 struct inet6_dev *idev = __in6_dev_get(dev);
1985 switch(event) {
1986 case NETDEV_UP:
1987 switch(dev->type) {
1988 case ARPHRD_SIT:
1989 addrconf_sit_config(dev);
1990 break;
1991 case ARPHRD_TUNNEL6:
1992 addrconf_ip6_tnl_config(dev);
1993 break;
1994 case ARPHRD_LOOPBACK:
1995 init_loopback(dev);
1996 break;
1998 default:
1999 addrconf_dev_config(dev);
2000 break;
2002 if (idev) {
2003 /* If the MTU changed during the interface down, when the
2004 interface up, the changed MTU must be reflected in the
2005 idev as well as routers.
2007 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2008 rt6_mtu_change(dev, dev->mtu);
2009 idev->cnf.mtu6 = dev->mtu;
2011 idev->tstamp = jiffies;
2012 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2013 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2014 stop IPv6 on this interface.
2016 if (dev->mtu < IPV6_MIN_MTU)
2017 addrconf_ifdown(dev, event != NETDEV_DOWN);
2019 break;
2021 case NETDEV_CHANGEMTU:
2022 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2023 rt6_mtu_change(dev, dev->mtu);
2024 idev->cnf.mtu6 = dev->mtu;
2025 break;
2028 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2030 case NETDEV_DOWN:
2031 case NETDEV_UNREGISTER:
2033 * Remove all addresses from this interface.
2035 addrconf_ifdown(dev, event != NETDEV_DOWN);
2036 break;
2037 case NETDEV_CHANGE:
2038 break;
2039 case NETDEV_CHANGENAME:
2040 #ifdef CONFIG_SYSCTL
2041 if (idev) {
2042 addrconf_sysctl_unregister(&idev->cnf);
2043 neigh_sysctl_unregister(idev->nd_parms);
2044 neigh_sysctl_register(dev, idev->nd_parms,
2045 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2046 &ndisc_ifinfo_sysctl_change,
2047 NULL);
2048 addrconf_sysctl_register(idev, &idev->cnf);
2050 #endif
2051 break;
2054 return NOTIFY_OK;
2058 * addrconf module should be notified of a device going up
2060 static struct notifier_block ipv6_dev_notf = {
2061 .notifier_call = addrconf_notify,
2062 .priority = 0
2065 static int addrconf_ifdown(struct net_device *dev, int how)
2067 struct inet6_dev *idev;
2068 struct inet6_ifaddr *ifa, **bifa;
2069 int i;
2071 ASSERT_RTNL();
2073 if (dev == &loopback_dev && how == 1)
2074 how = 0;
2076 rt6_ifdown(dev);
2077 neigh_ifdown(&nd_tbl, dev);
2079 idev = __in6_dev_get(dev);
2080 if (idev == NULL)
2081 return -ENODEV;
2083 /* Step 1: remove reference to ipv6 device from parent device.
2084 Do not dev_put!
2086 if (how == 1) {
2087 write_lock_bh(&addrconf_lock);
2088 dev->ip6_ptr = NULL;
2089 idev->dead = 1;
2090 write_unlock_bh(&addrconf_lock);
2092 /* Step 1.5: remove snmp6 entry */
2093 snmp6_unregister_dev(idev);
2097 /* Step 2: clear hash table */
2098 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2099 bifa = &inet6_addr_lst[i];
2101 write_lock_bh(&addrconf_hash_lock);
2102 while ((ifa = *bifa) != NULL) {
2103 if (ifa->idev == idev) {
2104 *bifa = ifa->lst_next;
2105 ifa->lst_next = NULL;
2106 addrconf_del_timer(ifa);
2107 in6_ifa_put(ifa);
2108 continue;
2110 bifa = &ifa->lst_next;
2112 write_unlock_bh(&addrconf_hash_lock);
2115 write_lock_bh(&idev->lock);
2117 /* Step 3: clear flags for stateless addrconf */
2118 if (how != 1)
2119 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2121 /* Step 4: clear address list */
2122 #ifdef CONFIG_IPV6_PRIVACY
2123 if (how == 1 && del_timer(&idev->regen_timer))
2124 in6_dev_put(idev);
2126 /* clear tempaddr list */
2127 while ((ifa = idev->tempaddr_list) != NULL) {
2128 idev->tempaddr_list = ifa->tmp_next;
2129 ifa->tmp_next = NULL;
2130 ifa->dead = 1;
2131 write_unlock_bh(&idev->lock);
2132 spin_lock_bh(&ifa->lock);
2134 if (ifa->ifpub) {
2135 in6_ifa_put(ifa->ifpub);
2136 ifa->ifpub = NULL;
2138 spin_unlock_bh(&ifa->lock);
2139 in6_ifa_put(ifa);
2140 write_lock_bh(&idev->lock);
2142 #endif
2143 while ((ifa = idev->addr_list) != NULL) {
2144 idev->addr_list = ifa->if_next;
2145 ifa->if_next = NULL;
2146 ifa->dead = 1;
2147 addrconf_del_timer(ifa);
2148 write_unlock_bh(&idev->lock);
2150 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2151 in6_ifa_put(ifa);
2153 write_lock_bh(&idev->lock);
2155 write_unlock_bh(&idev->lock);
2157 /* Step 5: Discard multicast list */
2159 if (how == 1)
2160 ipv6_mc_destroy_dev(idev);
2161 else
2162 ipv6_mc_down(idev);
2164 /* Step 5: netlink notification of this interface */
2165 idev->tstamp = jiffies;
2166 inet6_ifinfo_notify(RTM_DELLINK, idev);
2168 /* Shot the device (if unregistered) */
2170 if (how == 1) {
2171 #ifdef CONFIG_SYSCTL
2172 addrconf_sysctl_unregister(&idev->cnf);
2173 neigh_sysctl_unregister(idev->nd_parms);
2174 #endif
2175 neigh_parms_release(&nd_tbl, idev->nd_parms);
2176 neigh_ifdown(&nd_tbl, dev);
2177 in6_dev_put(idev);
2179 return 0;
2182 static void addrconf_rs_timer(unsigned long data)
2184 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2186 if (ifp->idev->cnf.forwarding)
2187 goto out;
2189 if (ifp->idev->if_flags & IF_RA_RCVD) {
2191 * Announcement received after solicitation
2192 * was sent
2194 goto out;
2197 spin_lock(&ifp->lock);
2198 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2199 struct in6_addr all_routers;
2201 /* The wait after the last probe can be shorter */
2202 addrconf_mod_timer(ifp, AC_RS,
2203 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2204 ifp->idev->cnf.rtr_solicit_delay :
2205 ifp->idev->cnf.rtr_solicit_interval);
2206 spin_unlock(&ifp->lock);
2208 ipv6_addr_all_routers(&all_routers);
2210 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2211 } else {
2212 spin_unlock(&ifp->lock);
2214 * Note: we do not support deprecated "all on-link"
2215 * assumption any longer.
2217 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2218 ifp->idev->dev->name);
2221 out:
2222 in6_ifa_put(ifp);
2226 * Duplicate Address Detection
2228 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2230 struct inet6_dev *idev = ifp->idev;
2231 struct net_device *dev = idev->dev;
2232 unsigned long rand_num;
2234 addrconf_join_solict(dev, &ifp->addr);
2236 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2237 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2238 flags);
2240 net_srandom(ifp->addr.s6_addr32[3]);
2241 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2243 read_lock_bh(&idev->lock);
2244 if (ifp->dead)
2245 goto out;
2246 spin_lock_bh(&ifp->lock);
2248 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2249 !(ifp->flags&IFA_F_TENTATIVE)) {
2250 ifp->flags &= ~IFA_F_TENTATIVE;
2251 spin_unlock_bh(&ifp->lock);
2252 read_unlock_bh(&idev->lock);
2254 addrconf_dad_completed(ifp);
2255 return;
2258 ifp->probes = idev->cnf.dad_transmits;
2259 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2261 spin_unlock_bh(&ifp->lock);
2262 out:
2263 read_unlock_bh(&idev->lock);
2266 static void addrconf_dad_timer(unsigned long data)
2268 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2269 struct inet6_dev *idev = ifp->idev;
2270 struct in6_addr unspec;
2271 struct in6_addr mcaddr;
2273 read_lock_bh(&idev->lock);
2274 if (idev->dead) {
2275 read_unlock_bh(&idev->lock);
2276 goto out;
2278 spin_lock_bh(&ifp->lock);
2279 if (ifp->probes == 0) {
2281 * DAD was successful
2284 ifp->flags &= ~IFA_F_TENTATIVE;
2285 spin_unlock_bh(&ifp->lock);
2286 read_unlock_bh(&idev->lock);
2288 addrconf_dad_completed(ifp);
2290 goto out;
2293 ifp->probes--;
2294 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2295 spin_unlock_bh(&ifp->lock);
2296 read_unlock_bh(&idev->lock);
2298 /* send a neighbour solicitation for our addr */
2299 memset(&unspec, 0, sizeof(unspec));
2300 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2301 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2302 out:
2303 in6_ifa_put(ifp);
2306 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2308 struct net_device * dev = ifp->idev->dev;
2311 * Configure the address for reception. Now it is valid.
2314 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2316 /* If added prefix is link local and forwarding is off,
2317 start sending router solicitations.
2320 if (ifp->idev->cnf.forwarding == 0 &&
2321 ifp->idev->cnf.rtr_solicits > 0 &&
2322 (dev->flags&IFF_LOOPBACK) == 0 &&
2323 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2324 struct in6_addr all_routers;
2326 ipv6_addr_all_routers(&all_routers);
2329 * If a host as already performed a random delay
2330 * [...] as part of DAD [...] there is no need
2331 * to delay again before sending the first RS
2333 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2335 spin_lock_bh(&ifp->lock);
2336 ifp->probes = 1;
2337 ifp->idev->if_flags |= IF_RS_SENT;
2338 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2339 spin_unlock_bh(&ifp->lock);
2343 #ifdef CONFIG_PROC_FS
2344 struct if6_iter_state {
2345 int bucket;
2348 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2350 struct inet6_ifaddr *ifa = NULL;
2351 struct if6_iter_state *state = seq->private;
2353 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2354 ifa = inet6_addr_lst[state->bucket];
2355 if (ifa)
2356 break;
2358 return ifa;
2361 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2363 struct if6_iter_state *state = seq->private;
2365 ifa = ifa->lst_next;
2366 try_again:
2367 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2368 ifa = inet6_addr_lst[state->bucket];
2369 goto try_again;
2371 return ifa;
2374 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2376 struct inet6_ifaddr *ifa = if6_get_first(seq);
2378 if (ifa)
2379 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2380 --pos;
2381 return pos ? NULL : ifa;
2384 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2386 read_lock_bh(&addrconf_hash_lock);
2387 return if6_get_idx(seq, *pos);
2390 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2392 struct inet6_ifaddr *ifa;
2394 ifa = if6_get_next(seq, v);
2395 ++*pos;
2396 return ifa;
2399 static void if6_seq_stop(struct seq_file *seq, void *v)
2401 read_unlock_bh(&addrconf_hash_lock);
2404 static int if6_seq_show(struct seq_file *seq, void *v)
2406 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2407 seq_printf(seq,
2408 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2409 NIP6(ifp->addr),
2410 ifp->idev->dev->ifindex,
2411 ifp->prefix_len,
2412 ifp->scope,
2413 ifp->flags,
2414 ifp->idev->dev->name);
2415 return 0;
2418 static struct seq_operations if6_seq_ops = {
2419 .start = if6_seq_start,
2420 .next = if6_seq_next,
2421 .show = if6_seq_show,
2422 .stop = if6_seq_stop,
2425 static int if6_seq_open(struct inode *inode, struct file *file)
2427 struct seq_file *seq;
2428 int rc = -ENOMEM;
2429 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2431 if (!s)
2432 goto out;
2433 memset(s, 0, sizeof(*s));
2435 rc = seq_open(file, &if6_seq_ops);
2436 if (rc)
2437 goto out_kfree;
2439 seq = file->private_data;
2440 seq->private = s;
2441 out:
2442 return rc;
2443 out_kfree:
2444 kfree(s);
2445 goto out;
2448 static struct file_operations if6_fops = {
2449 .owner = THIS_MODULE,
2450 .open = if6_seq_open,
2451 .read = seq_read,
2452 .llseek = seq_lseek,
2453 .release = seq_release_private,
2456 int __init if6_proc_init(void)
2458 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2459 return -ENOMEM;
2460 return 0;
2463 void if6_proc_exit(void)
2465 proc_net_remove("if_inet6");
2467 #endif /* CONFIG_PROC_FS */
2470 * Periodic address status verification
2473 static void addrconf_verify(unsigned long foo)
2475 struct inet6_ifaddr *ifp;
2476 unsigned long now, next;
2477 int i;
2479 spin_lock_bh(&addrconf_verify_lock);
2480 now = jiffies;
2481 next = now + ADDR_CHECK_FREQUENCY;
2483 del_timer(&addr_chk_timer);
2485 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2487 restart:
2488 write_lock(&addrconf_hash_lock);
2489 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2490 unsigned long age;
2491 #ifdef CONFIG_IPV6_PRIVACY
2492 unsigned long regen_advance;
2493 #endif
2495 if (ifp->flags & IFA_F_PERMANENT)
2496 continue;
2498 spin_lock(&ifp->lock);
2499 age = (now - ifp->tstamp) / HZ;
2501 #ifdef CONFIG_IPV6_PRIVACY
2502 regen_advance = ifp->idev->cnf.regen_max_retry *
2503 ifp->idev->cnf.dad_transmits *
2504 ifp->idev->nd_parms->retrans_time / HZ;
2505 #endif
2507 if (age >= ifp->valid_lft) {
2508 spin_unlock(&ifp->lock);
2509 in6_ifa_hold(ifp);
2510 write_unlock(&addrconf_hash_lock);
2511 ipv6_del_addr(ifp);
2512 goto restart;
2513 } else if (age >= ifp->prefered_lft) {
2514 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2515 int deprecate = 0;
2517 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2518 deprecate = 1;
2519 ifp->flags |= IFA_F_DEPRECATED;
2522 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2523 next = ifp->tstamp + ifp->valid_lft * HZ;
2525 spin_unlock(&ifp->lock);
2527 if (deprecate) {
2528 in6_ifa_hold(ifp);
2529 write_unlock(&addrconf_hash_lock);
2531 ipv6_ifa_notify(0, ifp);
2532 in6_ifa_put(ifp);
2533 goto restart;
2535 #ifdef CONFIG_IPV6_PRIVACY
2536 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2537 !(ifp->flags&IFA_F_TENTATIVE)) {
2538 if (age >= ifp->prefered_lft - regen_advance) {
2539 struct inet6_ifaddr *ifpub = ifp->ifpub;
2540 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2541 next = ifp->tstamp + ifp->prefered_lft * HZ;
2542 if (!ifp->regen_count && ifpub) {
2543 ifp->regen_count++;
2544 in6_ifa_hold(ifp);
2545 in6_ifa_hold(ifpub);
2546 spin_unlock(&ifp->lock);
2547 write_unlock(&addrconf_hash_lock);
2548 ipv6_create_tempaddr(ifpub, ifp);
2549 in6_ifa_put(ifpub);
2550 in6_ifa_put(ifp);
2551 goto restart;
2553 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2554 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2555 spin_unlock(&ifp->lock);
2556 #endif
2557 } else {
2558 /* ifp->prefered_lft <= ifp->valid_lft */
2559 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2560 next = ifp->tstamp + ifp->prefered_lft * HZ;
2561 spin_unlock(&ifp->lock);
2564 write_unlock(&addrconf_hash_lock);
2567 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2568 add_timer(&addr_chk_timer);
2569 spin_unlock_bh(&addrconf_verify_lock);
2572 static int
2573 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2575 struct rtattr **rta = arg;
2576 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2577 struct in6_addr *pfx;
2579 pfx = NULL;
2580 if (rta[IFA_ADDRESS-1]) {
2581 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2582 return -EINVAL;
2583 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2585 if (rta[IFA_LOCAL-1]) {
2586 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2587 return -EINVAL;
2588 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2590 if (pfx == NULL)
2591 return -EINVAL;
2593 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2596 static int
2597 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2599 struct rtattr **rta = arg;
2600 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2601 struct in6_addr *pfx;
2603 pfx = NULL;
2604 if (rta[IFA_ADDRESS-1]) {
2605 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2606 return -EINVAL;
2607 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2609 if (rta[IFA_LOCAL-1]) {
2610 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2611 return -EINVAL;
2612 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2614 if (pfx == NULL)
2615 return -EINVAL;
2617 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2620 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2621 u32 pid, u32 seq, int event, unsigned int flags)
2623 struct ifaddrmsg *ifm;
2624 struct nlmsghdr *nlh;
2625 struct ifa_cacheinfo ci;
2626 unsigned char *b = skb->tail;
2628 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2629 ifm = NLMSG_DATA(nlh);
2630 ifm->ifa_family = AF_INET6;
2631 ifm->ifa_prefixlen = ifa->prefix_len;
2632 ifm->ifa_flags = ifa->flags;
2633 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2634 if (ifa->scope&IFA_HOST)
2635 ifm->ifa_scope = RT_SCOPE_HOST;
2636 else if (ifa->scope&IFA_LINK)
2637 ifm->ifa_scope = RT_SCOPE_LINK;
2638 else if (ifa->scope&IFA_SITE)
2639 ifm->ifa_scope = RT_SCOPE_SITE;
2640 ifm->ifa_index = ifa->idev->dev->ifindex;
2641 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2642 if (!(ifa->flags&IFA_F_PERMANENT)) {
2643 ci.ifa_prefered = ifa->prefered_lft;
2644 ci.ifa_valid = ifa->valid_lft;
2645 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2646 long tval = (jiffies - ifa->tstamp)/HZ;
2647 ci.ifa_prefered -= tval;
2648 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2649 ci.ifa_valid -= tval;
2651 } else {
2652 ci.ifa_prefered = INFINITY_LIFE_TIME;
2653 ci.ifa_valid = INFINITY_LIFE_TIME;
2655 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2656 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2657 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2658 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2659 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2660 nlh->nlmsg_len = skb->tail - b;
2661 return skb->len;
2663 nlmsg_failure:
2664 rtattr_failure:
2665 skb_trim(skb, b - skb->data);
2666 return -1;
2669 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2670 u32 pid, u32 seq, int event, u16 flags)
2672 struct ifaddrmsg *ifm;
2673 struct nlmsghdr *nlh;
2674 struct ifa_cacheinfo ci;
2675 unsigned char *b = skb->tail;
2677 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2678 ifm = NLMSG_DATA(nlh);
2679 ifm->ifa_family = AF_INET6;
2680 ifm->ifa_prefixlen = 128;
2681 ifm->ifa_flags = IFA_F_PERMANENT;
2682 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2683 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2684 ifm->ifa_scope = RT_SCOPE_SITE;
2685 ifm->ifa_index = ifmca->idev->dev->ifindex;
2686 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2687 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2688 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2689 * 100 / HZ);
2690 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2691 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2692 * 100 / HZ);
2693 ci.ifa_prefered = INFINITY_LIFE_TIME;
2694 ci.ifa_valid = INFINITY_LIFE_TIME;
2695 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2696 nlh->nlmsg_len = skb->tail - b;
2697 return skb->len;
2699 nlmsg_failure:
2700 rtattr_failure:
2701 skb_trim(skb, b - skb->data);
2702 return -1;
2705 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2706 u32 pid, u32 seq, int event, unsigned int flags)
2708 struct ifaddrmsg *ifm;
2709 struct nlmsghdr *nlh;
2710 struct ifa_cacheinfo ci;
2711 unsigned char *b = skb->tail;
2713 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2714 ifm = NLMSG_DATA(nlh);
2715 ifm->ifa_family = AF_INET6;
2716 ifm->ifa_prefixlen = 128;
2717 ifm->ifa_flags = IFA_F_PERMANENT;
2718 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2719 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2720 ifm->ifa_scope = RT_SCOPE_SITE;
2721 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2722 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2723 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2724 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2725 * 100 / HZ);
2726 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2727 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2728 * 100 / HZ);
2729 ci.ifa_prefered = INFINITY_LIFE_TIME;
2730 ci.ifa_valid = INFINITY_LIFE_TIME;
2731 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2732 nlh->nlmsg_len = skb->tail - b;
2733 return skb->len;
2735 nlmsg_failure:
2736 rtattr_failure:
2737 skb_trim(skb, b - skb->data);
2738 return -1;
2741 enum addr_type_t
2743 UNICAST_ADDR,
2744 MULTICAST_ADDR,
2745 ANYCAST_ADDR,
2748 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2749 enum addr_type_t type)
2751 int idx, ip_idx;
2752 int s_idx, s_ip_idx;
2753 int err = 1;
2754 struct net_device *dev;
2755 struct inet6_dev *idev = NULL;
2756 struct inet6_ifaddr *ifa;
2757 struct ifmcaddr6 *ifmca;
2758 struct ifacaddr6 *ifaca;
2760 s_idx = cb->args[0];
2761 s_ip_idx = ip_idx = cb->args[1];
2762 read_lock(&dev_base_lock);
2764 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2765 if (idx < s_idx)
2766 continue;
2767 if (idx > s_idx)
2768 s_ip_idx = 0;
2769 ip_idx = 0;
2770 if ((idev = in6_dev_get(dev)) == NULL)
2771 continue;
2772 read_lock_bh(&idev->lock);
2773 switch (type) {
2774 case UNICAST_ADDR:
2775 /* unicast address incl. temp addr */
2776 for (ifa = idev->addr_list; ifa;
2777 ifa = ifa->if_next, ip_idx++) {
2778 if (ip_idx < s_ip_idx)
2779 continue;
2780 if ((err = inet6_fill_ifaddr(skb, ifa,
2781 NETLINK_CB(cb->skb).pid,
2782 cb->nlh->nlmsg_seq, RTM_NEWADDR,
2783 NLM_F_MULTI)) <= 0)
2784 goto done;
2786 break;
2787 case MULTICAST_ADDR:
2788 /* multicast address */
2789 for (ifmca = idev->mc_list; ifmca;
2790 ifmca = ifmca->next, ip_idx++) {
2791 if (ip_idx < s_ip_idx)
2792 continue;
2793 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2794 NETLINK_CB(cb->skb).pid,
2795 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
2796 NLM_F_MULTI)) <= 0)
2797 goto done;
2799 break;
2800 case ANYCAST_ADDR:
2801 /* anycast address */
2802 for (ifaca = idev->ac_list; ifaca;
2803 ifaca = ifaca->aca_next, ip_idx++) {
2804 if (ip_idx < s_ip_idx)
2805 continue;
2806 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2807 NETLINK_CB(cb->skb).pid,
2808 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
2809 NLM_F_MULTI)) <= 0)
2810 goto done;
2812 break;
2813 default:
2814 break;
2816 read_unlock_bh(&idev->lock);
2817 in6_dev_put(idev);
2819 done:
2820 if (err <= 0) {
2821 read_unlock_bh(&idev->lock);
2822 in6_dev_put(idev);
2824 read_unlock(&dev_base_lock);
2825 cb->args[0] = idx;
2826 cb->args[1] = ip_idx;
2827 return skb->len;
2830 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2832 enum addr_type_t type = UNICAST_ADDR;
2833 return inet6_dump_addr(skb, cb, type);
2836 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2838 enum addr_type_t type = MULTICAST_ADDR;
2839 return inet6_dump_addr(skb, cb, type);
2843 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2845 enum addr_type_t type = ANYCAST_ADDR;
2846 return inet6_dump_addr(skb, cb, type);
2849 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2851 struct sk_buff *skb;
2852 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2854 skb = alloc_skb(size, GFP_ATOMIC);
2855 if (!skb) {
2856 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
2857 return;
2859 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
2860 kfree_skb(skb);
2861 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
2862 return;
2864 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
2865 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
2868 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
2869 __s32 *array, int bytes)
2871 memset(array, 0, bytes);
2872 array[DEVCONF_FORWARDING] = cnf->forwarding;
2873 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
2874 array[DEVCONF_MTU6] = cnf->mtu6;
2875 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
2876 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
2877 array[DEVCONF_AUTOCONF] = cnf->autoconf;
2878 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
2879 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
2880 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
2881 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
2882 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
2883 #ifdef CONFIG_IPV6_PRIVACY
2884 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2885 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2886 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2887 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2888 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2889 #endif
2890 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
2893 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
2894 u32 pid, u32 seq, int event, unsigned int flags)
2896 struct net_device *dev = idev->dev;
2897 __s32 *array = NULL;
2898 struct ifinfomsg *r;
2899 struct nlmsghdr *nlh;
2900 unsigned char *b = skb->tail;
2901 struct rtattr *subattr;
2902 __u32 mtu = dev->mtu;
2903 struct ifla_cacheinfo ci;
2905 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
2906 r = NLMSG_DATA(nlh);
2907 r->ifi_family = AF_INET6;
2908 r->__ifi_pad = 0;
2909 r->ifi_type = dev->type;
2910 r->ifi_index = dev->ifindex;
2911 r->ifi_flags = dev_get_flags(dev);
2912 r->ifi_change = 0;
2914 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2916 if (dev->addr_len)
2917 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2919 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2920 if (dev->ifindex != dev->iflink)
2921 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
2923 subattr = (struct rtattr*)skb->tail;
2925 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2927 /* return the device flags */
2928 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2930 /* return interface cacheinfo */
2931 ci.max_reasm_len = IPV6_MAXPLEN;
2932 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
2933 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2934 ci.reachable_time = idev->nd_parms->reachable_time;
2935 ci.retrans_time = idev->nd_parms->retrans_time;
2936 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
2938 /* return the device sysctl params */
2939 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2940 goto rtattr_failure;
2941 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2942 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2944 /* XXX - Statistics/MC not implemented */
2945 subattr->rta_len = skb->tail - (u8*)subattr;
2947 nlh->nlmsg_len = skb->tail - b;
2948 kfree(array);
2949 return skb->len;
2951 nlmsg_failure:
2952 rtattr_failure:
2953 if (array)
2954 kfree(array);
2955 skb_trim(skb, b - skb->data);
2956 return -1;
2959 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2961 int idx, err;
2962 int s_idx = cb->args[0];
2963 struct net_device *dev;
2964 struct inet6_dev *idev;
2966 read_lock(&dev_base_lock);
2967 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2968 if (idx < s_idx)
2969 continue;
2970 if ((idev = in6_dev_get(dev)) == NULL)
2971 continue;
2972 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
2973 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
2974 in6_dev_put(idev);
2975 if (err <= 0)
2976 break;
2978 read_unlock(&dev_base_lock);
2979 cb->args[0] = idx;
2981 return skb->len;
2984 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
2986 struct sk_buff *skb;
2987 /* 128 bytes ?? */
2988 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
2990 skb = alloc_skb(size, GFP_ATOMIC);
2991 if (!skb) {
2992 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
2993 return;
2995 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
2996 kfree_skb(skb);
2997 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
2998 return;
3000 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3001 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3004 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3005 struct prefix_info *pinfo, u32 pid, u32 seq,
3006 int event, unsigned int flags)
3008 struct prefixmsg *pmsg;
3009 struct nlmsghdr *nlh;
3010 unsigned char *b = skb->tail;
3011 struct prefix_cacheinfo ci;
3013 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3014 pmsg = NLMSG_DATA(nlh);
3015 pmsg->prefix_family = AF_INET6;
3016 pmsg->prefix_pad1 = 0;
3017 pmsg->prefix_pad2 = 0;
3018 pmsg->prefix_ifindex = idev->dev->ifindex;
3019 pmsg->prefix_len = pinfo->prefix_len;
3020 pmsg->prefix_type = pinfo->type;
3021 pmsg->prefix_pad3 = 0;
3023 pmsg->prefix_flags = 0;
3024 if (pinfo->onlink)
3025 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3026 if (pinfo->autoconf)
3027 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3029 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3031 ci.preferred_time = ntohl(pinfo->prefered);
3032 ci.valid_time = ntohl(pinfo->valid);
3033 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3035 nlh->nlmsg_len = skb->tail - b;
3036 return skb->len;
3038 nlmsg_failure:
3039 rtattr_failure:
3040 skb_trim(skb, b - skb->data);
3041 return -1;
3044 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3045 struct prefix_info *pinfo)
3047 struct sk_buff *skb;
3048 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3050 skb = alloc_skb(size, GFP_ATOMIC);
3051 if (!skb) {
3052 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3053 return;
3055 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3056 kfree_skb(skb);
3057 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3058 return;
3060 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3061 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3064 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3065 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3066 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3067 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3068 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3069 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3070 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3071 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3072 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3073 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3074 .dumpit = inet6_dump_fib, },
3077 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3079 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3081 switch (event) {
3082 case RTM_NEWADDR:
3083 dst_hold(&ifp->rt->u.dst);
3084 if (ip6_ins_rt(ifp->rt, NULL, NULL, NULL))
3085 dst_release(&ifp->rt->u.dst);
3086 if (ifp->idev->cnf.forwarding)
3087 addrconf_join_anycast(ifp);
3088 break;
3089 case RTM_DELADDR:
3090 if (ifp->idev->cnf.forwarding)
3091 addrconf_leave_anycast(ifp);
3092 addrconf_leave_solict(ifp->idev, &ifp->addr);
3093 dst_hold(&ifp->rt->u.dst);
3094 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3095 dst_free(&ifp->rt->u.dst);
3096 else
3097 dst_release(&ifp->rt->u.dst);
3098 break;
3102 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3104 read_lock_bh(&addrconf_lock);
3105 if (likely(ifp->idev->dead == 0))
3106 __ipv6_ifa_notify(event, ifp);
3107 read_unlock_bh(&addrconf_lock);
3110 #ifdef CONFIG_SYSCTL
3112 static
3113 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3114 void __user *buffer, size_t *lenp, loff_t *ppos)
3116 int *valp = ctl->data;
3117 int val = *valp;
3118 int ret;
3120 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3122 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3123 if (valp != &ipv6_devconf.forwarding) {
3124 if ((!*valp) ^ (!val)) {
3125 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3126 if (idev == NULL)
3127 return ret;
3128 dev_forward_change(idev);
3130 } else {
3131 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3132 addrconf_forward_change();
3134 if (*valp)
3135 rt6_purge_dflt_routers();
3138 return ret;
3141 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3142 int __user *name, int nlen,
3143 void __user *oldval,
3144 size_t __user *oldlenp,
3145 void __user *newval, size_t newlen,
3146 void **context)
3148 int *valp = table->data;
3149 int new;
3151 if (!newval || !newlen)
3152 return 0;
3153 if (newlen != sizeof(int))
3154 return -EINVAL;
3155 if (get_user(new, (int __user *)newval))
3156 return -EFAULT;
3157 if (new == *valp)
3158 return 0;
3159 if (oldval && oldlenp) {
3160 size_t len;
3161 if (get_user(len, oldlenp))
3162 return -EFAULT;
3163 if (len) {
3164 if (len > table->maxlen)
3165 len = table->maxlen;
3166 if (copy_to_user(oldval, valp, len))
3167 return -EFAULT;
3168 if (put_user(len, oldlenp))
3169 return -EFAULT;
3173 if (valp != &ipv6_devconf_dflt.forwarding) {
3174 if (valp != &ipv6_devconf.forwarding) {
3175 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3176 int changed;
3177 if (unlikely(idev == NULL))
3178 return -ENODEV;
3179 changed = (!*valp) ^ (!new);
3180 *valp = new;
3181 if (changed)
3182 dev_forward_change(idev);
3183 } else {
3184 *valp = new;
3185 addrconf_forward_change();
3188 if (*valp)
3189 rt6_purge_dflt_routers();
3190 } else
3191 *valp = new;
3193 return 1;
3196 static struct addrconf_sysctl_table
3198 struct ctl_table_header *sysctl_header;
3199 ctl_table addrconf_vars[__NET_IPV6_MAX];
3200 ctl_table addrconf_dev[2];
3201 ctl_table addrconf_conf_dir[2];
3202 ctl_table addrconf_proto_dir[2];
3203 ctl_table addrconf_root_dir[2];
3204 } addrconf_sysctl = {
3205 .sysctl_header = NULL,
3206 .addrconf_vars = {
3208 .ctl_name = NET_IPV6_FORWARDING,
3209 .procname = "forwarding",
3210 .data = &ipv6_devconf.forwarding,
3211 .maxlen = sizeof(int),
3212 .mode = 0644,
3213 .proc_handler = &addrconf_sysctl_forward,
3214 .strategy = &addrconf_sysctl_forward_strategy,
3217 .ctl_name = NET_IPV6_HOP_LIMIT,
3218 .procname = "hop_limit",
3219 .data = &ipv6_devconf.hop_limit,
3220 .maxlen = sizeof(int),
3221 .mode = 0644,
3222 .proc_handler = proc_dointvec,
3225 .ctl_name = NET_IPV6_MTU,
3226 .procname = "mtu",
3227 .data = &ipv6_devconf.mtu6,
3228 .maxlen = sizeof(int),
3229 .mode = 0644,
3230 .proc_handler = &proc_dointvec,
3233 .ctl_name = NET_IPV6_ACCEPT_RA,
3234 .procname = "accept_ra",
3235 .data = &ipv6_devconf.accept_ra,
3236 .maxlen = sizeof(int),
3237 .mode = 0644,
3238 .proc_handler = &proc_dointvec,
3241 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3242 .procname = "accept_redirects",
3243 .data = &ipv6_devconf.accept_redirects,
3244 .maxlen = sizeof(int),
3245 .mode = 0644,
3246 .proc_handler = &proc_dointvec,
3249 .ctl_name = NET_IPV6_AUTOCONF,
3250 .procname = "autoconf",
3251 .data = &ipv6_devconf.autoconf,
3252 .maxlen = sizeof(int),
3253 .mode = 0644,
3254 .proc_handler = &proc_dointvec,
3257 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3258 .procname = "dad_transmits",
3259 .data = &ipv6_devconf.dad_transmits,
3260 .maxlen = sizeof(int),
3261 .mode = 0644,
3262 .proc_handler = &proc_dointvec,
3265 .ctl_name = NET_IPV6_RTR_SOLICITS,
3266 .procname = "router_solicitations",
3267 .data = &ipv6_devconf.rtr_solicits,
3268 .maxlen = sizeof(int),
3269 .mode = 0644,
3270 .proc_handler = &proc_dointvec,
3273 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3274 .procname = "router_solicitation_interval",
3275 .data = &ipv6_devconf.rtr_solicit_interval,
3276 .maxlen = sizeof(int),
3277 .mode = 0644,
3278 .proc_handler = &proc_dointvec_jiffies,
3279 .strategy = &sysctl_jiffies,
3282 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3283 .procname = "router_solicitation_delay",
3284 .data = &ipv6_devconf.rtr_solicit_delay,
3285 .maxlen = sizeof(int),
3286 .mode = 0644,
3287 .proc_handler = &proc_dointvec_jiffies,
3288 .strategy = &sysctl_jiffies,
3291 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3292 .procname = "force_mld_version",
3293 .data = &ipv6_devconf.force_mld_version,
3294 .maxlen = sizeof(int),
3295 .mode = 0644,
3296 .proc_handler = &proc_dointvec,
3298 #ifdef CONFIG_IPV6_PRIVACY
3300 .ctl_name = NET_IPV6_USE_TEMPADDR,
3301 .procname = "use_tempaddr",
3302 .data = &ipv6_devconf.use_tempaddr,
3303 .maxlen = sizeof(int),
3304 .mode = 0644,
3305 .proc_handler = &proc_dointvec,
3308 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3309 .procname = "temp_valid_lft",
3310 .data = &ipv6_devconf.temp_valid_lft,
3311 .maxlen = sizeof(int),
3312 .mode = 0644,
3313 .proc_handler = &proc_dointvec,
3316 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3317 .procname = "temp_prefered_lft",
3318 .data = &ipv6_devconf.temp_prefered_lft,
3319 .maxlen = sizeof(int),
3320 .mode = 0644,
3321 .proc_handler = &proc_dointvec,
3324 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3325 .procname = "regen_max_retry",
3326 .data = &ipv6_devconf.regen_max_retry,
3327 .maxlen = sizeof(int),
3328 .mode = 0644,
3329 .proc_handler = &proc_dointvec,
3332 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3333 .procname = "max_desync_factor",
3334 .data = &ipv6_devconf.max_desync_factor,
3335 .maxlen = sizeof(int),
3336 .mode = 0644,
3337 .proc_handler = &proc_dointvec,
3339 #endif
3341 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3342 .procname = "max_addresses",
3343 .data = &ipv6_devconf.max_addresses,
3344 .maxlen = sizeof(int),
3345 .mode = 0644,
3346 .proc_handler = &proc_dointvec,
3349 .ctl_name = 0, /* sentinel */
3352 .addrconf_dev = {
3354 .ctl_name = NET_PROTO_CONF_ALL,
3355 .procname = "all",
3356 .mode = 0555,
3357 .child = addrconf_sysctl.addrconf_vars,
3360 .ctl_name = 0, /* sentinel */
3363 .addrconf_conf_dir = {
3365 .ctl_name = NET_IPV6_CONF,
3366 .procname = "conf",
3367 .mode = 0555,
3368 .child = addrconf_sysctl.addrconf_dev,
3371 .ctl_name = 0, /* sentinel */
3374 .addrconf_proto_dir = {
3376 .ctl_name = NET_IPV6,
3377 .procname = "ipv6",
3378 .mode = 0555,
3379 .child = addrconf_sysctl.addrconf_conf_dir,
3382 .ctl_name = 0, /* sentinel */
3385 .addrconf_root_dir = {
3387 .ctl_name = CTL_NET,
3388 .procname = "net",
3389 .mode = 0555,
3390 .child = addrconf_sysctl.addrconf_proto_dir,
3393 .ctl_name = 0, /* sentinel */
3398 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3400 int i;
3401 struct net_device *dev = idev ? idev->dev : NULL;
3402 struct addrconf_sysctl_table *t;
3403 char *dev_name = NULL;
3405 t = kmalloc(sizeof(*t), GFP_KERNEL);
3406 if (t == NULL)
3407 return;
3408 memcpy(t, &addrconf_sysctl, sizeof(*t));
3409 for (i=0; t->addrconf_vars[i].data; i++) {
3410 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3411 t->addrconf_vars[i].de = NULL;
3412 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3414 if (dev) {
3415 dev_name = dev->name;
3416 t->addrconf_dev[0].ctl_name = dev->ifindex;
3417 } else {
3418 dev_name = "default";
3419 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3423 * Make a copy of dev_name, because '.procname' is regarded as const
3424 * by sysctl and we wouldn't want anyone to change it under our feet
3425 * (see SIOCSIFNAME).
3427 dev_name = kstrdup(dev_name, GFP_KERNEL);
3428 if (!dev_name)
3429 goto free;
3431 t->addrconf_dev[0].procname = dev_name;
3433 t->addrconf_dev[0].child = t->addrconf_vars;
3434 t->addrconf_dev[0].de = NULL;
3435 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3436 t->addrconf_conf_dir[0].de = NULL;
3437 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3438 t->addrconf_proto_dir[0].de = NULL;
3439 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3440 t->addrconf_root_dir[0].de = NULL;
3442 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3443 if (t->sysctl_header == NULL)
3444 goto free_procname;
3445 else
3446 p->sysctl = t;
3447 return;
3449 /* error path */
3450 free_procname:
3451 kfree(dev_name);
3452 free:
3453 kfree(t);
3455 return;
3458 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3460 if (p->sysctl) {
3461 struct addrconf_sysctl_table *t = p->sysctl;
3462 p->sysctl = NULL;
3463 unregister_sysctl_table(t->sysctl_header);
3464 kfree(t->addrconf_dev[0].procname);
3465 kfree(t);
3470 #endif
3473 * Device notifier
3476 int register_inet6addr_notifier(struct notifier_block *nb)
3478 return notifier_chain_register(&inet6addr_chain, nb);
3481 int unregister_inet6addr_notifier(struct notifier_block *nb)
3483 return notifier_chain_unregister(&inet6addr_chain,nb);
3487 * Init / cleanup code
3490 int __init addrconf_init(void)
3492 int err = 0;
3494 /* The addrconf netdev notifier requires that loopback_dev
3495 * has it's ipv6 private information allocated and setup
3496 * before it can bring up and give link-local addresses
3497 * to other devices which are up.
3499 * Unfortunately, loopback_dev is not necessarily the first
3500 * entry in the global dev_base list of net devices. In fact,
3501 * it is likely to be the very last entry on that list.
3502 * So this causes the notifier registry below to try and
3503 * give link-local addresses to all devices besides loopback_dev
3504 * first, then loopback_dev, which cases all the non-loopback_dev
3505 * devices to fail to get a link-local address.
3507 * So, as a temporary fix, allocate the ipv6 structure for
3508 * loopback_dev first by hand.
3509 * Longer term, all of the dependencies ipv6 has upon the loopback
3510 * device and it being up should be removed.
3512 rtnl_lock();
3513 if (!ipv6_add_dev(&loopback_dev))
3514 err = -ENOMEM;
3515 rtnl_unlock();
3516 if (err)
3517 return err;
3519 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3521 register_netdevice_notifier(&ipv6_dev_notf);
3523 #ifdef CONFIG_IPV6_PRIVACY
3524 md5_tfm = crypto_alloc_tfm("md5", 0);
3525 if (unlikely(md5_tfm == NULL))
3526 printk(KERN_WARNING
3527 "failed to load transform for md5\n");
3528 #endif
3530 addrconf_verify(0);
3531 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3532 #ifdef CONFIG_SYSCTL
3533 addrconf_sysctl.sysctl_header =
3534 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3535 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3536 #endif
3538 return 0;
3541 void __exit addrconf_cleanup(void)
3543 struct net_device *dev;
3544 struct inet6_dev *idev;
3545 struct inet6_ifaddr *ifa;
3546 int i;
3548 unregister_netdevice_notifier(&ipv6_dev_notf);
3550 rtnetlink_links[PF_INET6] = NULL;
3551 #ifdef CONFIG_SYSCTL
3552 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3553 addrconf_sysctl_unregister(&ipv6_devconf);
3554 #endif
3556 rtnl_lock();
3559 * clean dev list.
3562 for (dev=dev_base; dev; dev=dev->next) {
3563 if ((idev = __in6_dev_get(dev)) == NULL)
3564 continue;
3565 addrconf_ifdown(dev, 1);
3567 addrconf_ifdown(&loopback_dev, 2);
3570 * Check hash table.
3573 write_lock_bh(&addrconf_hash_lock);
3574 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3575 for (ifa=inet6_addr_lst[i]; ifa; ) {
3576 struct inet6_ifaddr *bifa;
3578 bifa = ifa;
3579 ifa = ifa->lst_next;
3580 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3581 /* Do not free it; something is wrong.
3582 Now we can investigate it with debugger.
3586 write_unlock_bh(&addrconf_hash_lock);
3588 del_timer(&addr_chk_timer);
3590 rtnl_unlock();
3592 #ifdef CONFIG_IPV6_PRIVACY
3593 crypto_free_tfm(md5_tfm);
3594 md5_tfm = NULL;
3595 #endif
3597 #ifdef CONFIG_PROC_FS
3598 proc_net_remove("if_inet6");
3599 #endif