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