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