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