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