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