]> err.no Git - linux-2.6/blob - net/ipv6/addrconf.c
[IPV6]: ADDRCONF: Use our standard algorithm for randomized ifid.
[linux-2.6] / net / ipv6 / addrconf.c
1 /*
2  *      IPv6 Address [auto]configuration
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
7  *      Alexey Kuznetsov        <kuznet@ms2.inr.ac.ru>
8  *
9  *      $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
10  *
11  *      This program is free software; you can redistribute it and/or
12  *      modify it under the terms of the GNU General Public License
13  *      as published by the Free Software Foundation; either version
14  *      2 of the License, or (at your option) any later version.
15  */
16
17 /*
18  *      Changes:
19  *
20  *      Janos Farkas                    :       delete timer on ifdown
21  *      <chexum@bankinf.banki.hu>
22  *      Andi Kleen                      :       kill double kfree on module
23  *                                              unload.
24  *      Maciej W. Rozycki               :       FDDI support
25  *      sekiya@USAGI                    :       Don't send too many RS
26  *                                              packets.
27  *      yoshfuji@USAGI                  :       Fixed interval between DAD
28  *                                              packets.
29  *      YOSHIFUJI Hideaki @USAGI        :       improved accuracy of
30  *                                              address validation timer.
31  *      YOSHIFUJI Hideaki @USAGI        :       Privacy Extensions (RFC3041)
32  *                                              support.
33  *      Yuji SEKIYA @USAGI              :       Don't assign a same IPv6
34  *                                              address on a same interface.
35  *      YOSHIFUJI Hideaki @USAGI        :       ARCnet support
36  *      YOSHIFUJI Hideaki @USAGI        :       convert /proc/net/if_inet6 to
37  *                                              seq_file.
38  *      YOSHIFUJI Hideaki @USAGI        :       improved source address
39  *                                              selection; consider scope,
40  *                                              status etc.
41  */
42
43 #include <linux/config.h>
44 #include <linux/errno.h>
45 #include <linux/types.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/sched.h>
49 #include <linux/net.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
58 #ifdef CONFIG_SYSCTL
59 #include <linux/sysctl.h>
60 #endif
61 #include <linux/capability.h>
62 #include <linux/delay.h>
63 #include <linux/notifier.h>
64 #include <linux/string.h>
65
66 #include <net/sock.h>
67 #include <net/snmp.h>
68
69 #include <net/ipv6.h>
70 #include <net/protocol.h>
71 #include <net/ndisc.h>
72 #include <net/ip6_route.h>
73 #include <net/addrconf.h>
74 #include <net/tcp.h>
75 #include <net/ip.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
78
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
81 #endif
82
83 #include <asm/uaccess.h>
84
85 #include <linux/proc_fs.h>
86 #include <linux/seq_file.h>
87
88 /* Set to 3 to get tracing... */
89 #define ACONF_DEBUG 2
90
91 #if ACONF_DEBUG >= 3
92 #define ADBG(x) printk x
93 #else
94 #define ADBG(x)
95 #endif
96
97 #define INFINITY_LIFE_TIME      0xFFFFFFFF
98 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
99
100 #ifdef CONFIG_SYSCTL
101 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
102 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
103 #endif
104
105 #ifdef CONFIG_IPV6_PRIVACY
106 static int __ipv6_regen_rndid(struct inet6_dev *idev);
107 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 
108 static void ipv6_regen_rndid(unsigned long data);
109
110 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
111 #endif
112
113 static int ipv6_count_addresses(struct inet6_dev *idev);
114
115 /*
116  *      Configured unicast address hash table
117  */
118 static struct inet6_ifaddr              *inet6_addr_lst[IN6_ADDR_HSIZE];
119 static DEFINE_RWLOCK(addrconf_hash_lock);
120
121 /* Protects inet6 devices */
122 DEFINE_RWLOCK(addrconf_lock);
123
124 static void addrconf_verify(unsigned long);
125
126 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
127 static DEFINE_SPINLOCK(addrconf_verify_lock);
128
129 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
130 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
131
132 static int addrconf_ifdown(struct net_device *dev, int how);
133
134 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
135 static void addrconf_dad_timer(unsigned long data);
136 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
137 static void addrconf_dad_run(struct inet6_dev *idev);
138 static void addrconf_rs_timer(unsigned long data);
139 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
140 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
141
142 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 
143                                 struct prefix_info *pinfo);
144 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
145
146 static struct notifier_block *inet6addr_chain;
147
148 struct ipv6_devconf ipv6_devconf = {
149         .forwarding             = 0,
150         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
151         .mtu6                   = IPV6_MIN_MTU,
152         .accept_ra              = 1,
153         .accept_redirects       = 1,
154         .autoconf               = 1,
155         .force_mld_version      = 0,
156         .dad_transmits          = 1,
157         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
158         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
159         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
160 #ifdef CONFIG_IPV6_PRIVACY
161         .use_tempaddr           = 0,
162         .temp_valid_lft         = TEMP_VALID_LIFETIME,
163         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
164         .regen_max_retry        = REGEN_MAX_RETRY,
165         .max_desync_factor      = MAX_DESYNC_FACTOR,
166 #endif
167         .max_addresses          = IPV6_MAX_ADDRESSES,
168 };
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 ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1303 {
1304         switch (dev->type) {
1305         case ARPHRD_ETHER:
1306         case ARPHRD_FDDI:
1307         case ARPHRD_IEEE802_TR:
1308                 if (dev->addr_len != ETH_ALEN)
1309                         return -1;
1310                 memcpy(eui, dev->dev_addr, 3);
1311                 memcpy(eui + 5, dev->dev_addr + 3, 3);
1312
1313                 /*
1314                  * The zSeries OSA network cards can be shared among various
1315                  * OS instances, but the OSA cards have only one MAC address.
1316                  * This leads to duplicate address conflicts in conjunction
1317                  * with IPv6 if more than one instance uses the same card.
1318                  * 
1319                  * The driver for these cards can deliver a unique 16-bit
1320                  * identifier for each instance sharing the same card.  It is
1321                  * placed instead of 0xFFFE in the interface identifier.  The
1322                  * "u" bit of the interface identifier is not inverted in this
1323                  * case.  Hence the resulting interface identifier has local
1324                  * scope according to RFC2373.
1325                  */
1326                 if (dev->dev_id) {
1327                         eui[3] = (dev->dev_id >> 8) & 0xFF;
1328                         eui[4] = dev->dev_id & 0xFF;
1329                 } else {
1330                         eui[3] = 0xFF;
1331                         eui[4] = 0xFE;
1332                         eui[0] ^= 2;
1333                 }
1334                 return 0;
1335         case ARPHRD_ARCNET:
1336                 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1337                 if (dev->addr_len != ARCNET_ALEN)
1338                         return -1;
1339                 memset(eui, 0, 7);
1340                 eui[7] = *(u8*)dev->dev_addr;
1341                 return 0;
1342         case ARPHRD_INFINIBAND:
1343                 if (dev->addr_len != INFINIBAND_ALEN)
1344                         return -1;
1345                 memcpy(eui, dev->dev_addr + 12, 8);
1346                 eui[0] |= 2;
1347                 return 0;
1348         }
1349         return -1;
1350 }
1351
1352 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1353 {
1354         int err = -1;
1355         struct inet6_ifaddr *ifp;
1356
1357         read_lock_bh(&idev->lock);
1358         for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1359                 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1360                         memcpy(eui, ifp->addr.s6_addr+8, 8);
1361                         err = 0;
1362                         break;
1363                 }
1364         }
1365         read_unlock_bh(&idev->lock);
1366         return err;
1367 }
1368
1369 #ifdef CONFIG_IPV6_PRIVACY
1370 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1371 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1372 {
1373 regen:
1374         get_random_bytes(idev->rndid, sizeof(idev->rndid));
1375         idev->rndid[0] &= ~0x02;
1376
1377         /*
1378          * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1379          * check if generated address is not inappropriate
1380          *
1381          *  - Reserved subnet anycast (RFC 2526)
1382          *      11111101 11....11 1xxxxxxx
1383          *  - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1384          *      00-00-5E-FE-xx-xx-xx-xx
1385          *  - value 0
1386          *  - XXX: already assigned to an address on the device
1387          */
1388         if (idev->rndid[0] == 0xfd && 
1389             (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1390             (idev->rndid[7]&0x80))
1391                 goto regen;
1392         if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1393                 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1394                         goto regen;
1395                 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1396                         goto regen;
1397         }
1398
1399         return 0;
1400 }
1401
1402 static void ipv6_regen_rndid(unsigned long data)
1403 {
1404         struct inet6_dev *idev = (struct inet6_dev *) data;
1405         unsigned long expires;
1406
1407         read_lock_bh(&addrconf_lock);
1408         write_lock_bh(&idev->lock);
1409
1410         if (idev->dead)
1411                 goto out;
1412
1413         if (__ipv6_regen_rndid(idev) < 0)
1414                 goto out;
1415         
1416         expires = jiffies +
1417                 idev->cnf.temp_prefered_lft * HZ - 
1418                 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1419         if (time_before(expires, jiffies)) {
1420                 printk(KERN_WARNING
1421                         "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1422                         idev->dev->name);
1423                 goto out;
1424         }
1425
1426         if (!mod_timer(&idev->regen_timer, expires))
1427                 in6_dev_hold(idev);
1428
1429 out:
1430         write_unlock_bh(&idev->lock);
1431         read_unlock_bh(&addrconf_lock);
1432         in6_dev_put(idev);
1433 }
1434
1435 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1436         int ret = 0;
1437
1438         if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1439                 ret = __ipv6_regen_rndid(idev);
1440         return ret;
1441 }
1442 #endif
1443
1444 /*
1445  *      Add prefix route.
1446  */
1447
1448 static void
1449 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1450                       unsigned long expires, u32 flags)
1451 {
1452         struct in6_rtmsg rtmsg;
1453
1454         memset(&rtmsg, 0, sizeof(rtmsg));
1455         ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1456         rtmsg.rtmsg_dst_len = plen;
1457         rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1458         rtmsg.rtmsg_ifindex = dev->ifindex;
1459         rtmsg.rtmsg_info = expires;
1460         rtmsg.rtmsg_flags = RTF_UP|flags;
1461         rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1462
1463         /* Prevent useless cloning on PtP SIT.
1464            This thing is done here expecting that the whole
1465            class of non-broadcast devices need not cloning.
1466          */
1467         if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1468                 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1469
1470         ip6_route_add(&rtmsg, NULL, NULL, NULL);
1471 }
1472
1473 /* Create "default" multicast route to the interface */
1474
1475 static void addrconf_add_mroute(struct net_device *dev)
1476 {
1477         struct in6_rtmsg rtmsg;
1478
1479         memset(&rtmsg, 0, sizeof(rtmsg));
1480         ipv6_addr_set(&rtmsg.rtmsg_dst,
1481                       htonl(0xFF000000), 0, 0, 0);
1482         rtmsg.rtmsg_dst_len = 8;
1483         rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1484         rtmsg.rtmsg_ifindex = dev->ifindex;
1485         rtmsg.rtmsg_flags = RTF_UP;
1486         rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1487         ip6_route_add(&rtmsg, NULL, NULL, NULL);
1488 }
1489
1490 static void sit_route_add(struct net_device *dev)
1491 {
1492         struct in6_rtmsg rtmsg;
1493
1494         memset(&rtmsg, 0, sizeof(rtmsg));
1495
1496         rtmsg.rtmsg_type        = RTMSG_NEWROUTE;
1497         rtmsg.rtmsg_metric      = IP6_RT_PRIO_ADDRCONF;
1498
1499         /* prefix length - 96 bits "::d.d.d.d" */
1500         rtmsg.rtmsg_dst_len     = 96;
1501         rtmsg.rtmsg_flags       = RTF_UP|RTF_NONEXTHOP;
1502         rtmsg.rtmsg_ifindex     = dev->ifindex;
1503
1504         ip6_route_add(&rtmsg, NULL, NULL, NULL);
1505 }
1506
1507 static void addrconf_add_lroute(struct net_device *dev)
1508 {
1509         struct in6_addr addr;
1510
1511         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
1512         addrconf_prefix_route(&addr, 64, dev, 0, 0);
1513 }
1514
1515 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1516 {
1517         struct inet6_dev *idev;
1518
1519         ASSERT_RTNL();
1520
1521         if ((idev = ipv6_find_idev(dev)) == NULL)
1522                 return NULL;
1523
1524         /* Add default multicast route */
1525         addrconf_add_mroute(dev);
1526
1527         /* Add link local route */
1528         addrconf_add_lroute(dev);
1529         return idev;
1530 }
1531
1532 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1533 {
1534         struct prefix_info *pinfo;
1535         __u32 valid_lft;
1536         __u32 prefered_lft;
1537         int addr_type;
1538         unsigned long rt_expires;
1539         struct inet6_dev *in6_dev;
1540
1541         pinfo = (struct prefix_info *) opt;
1542         
1543         if (len < sizeof(struct prefix_info)) {
1544                 ADBG(("addrconf: prefix option too short\n"));
1545                 return;
1546         }
1547         
1548         /*
1549          *      Validation checks ([ADDRCONF], page 19)
1550          */
1551
1552         addr_type = ipv6_addr_type(&pinfo->prefix);
1553
1554         if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1555                 return;
1556
1557         valid_lft = ntohl(pinfo->valid);
1558         prefered_lft = ntohl(pinfo->prefered);
1559
1560         if (prefered_lft > valid_lft) {
1561                 if (net_ratelimit())
1562                         printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1563                 return;
1564         }
1565
1566         in6_dev = in6_dev_get(dev);
1567
1568         if (in6_dev == NULL) {
1569                 if (net_ratelimit())
1570                         printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1571                 return;
1572         }
1573
1574         /*
1575          *      Two things going on here:
1576          *      1) Add routes for on-link prefixes
1577          *      2) Configure prefixes with the auto flag set
1578          */
1579
1580         /* Avoid arithmetic overflow. Really, we could
1581            save rt_expires in seconds, likely valid_lft,
1582            but it would require division in fib gc, that it
1583            not good.
1584          */
1585         if (valid_lft >= 0x7FFFFFFF/HZ)
1586                 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1587         else
1588                 rt_expires = valid_lft * HZ;
1589
1590         /*
1591          * We convert this (in jiffies) to clock_t later.
1592          * Avoid arithmetic overflow there as well.
1593          * Overflow can happen only if HZ < USER_HZ.
1594          */
1595         if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1596                 rt_expires = 0x7FFFFFFF / USER_HZ;
1597
1598         if (pinfo->onlink) {
1599                 struct rt6_info *rt;
1600                 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1601
1602                 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1603                         if (rt->rt6i_flags&RTF_EXPIRES) {
1604                                 if (valid_lft == 0) {
1605                                         ip6_del_rt(rt, NULL, NULL, NULL);
1606                                         rt = NULL;
1607                                 } else {
1608                                         rt->rt6i_expires = jiffies + rt_expires;
1609                                 }
1610                         }
1611                 } else if (valid_lft) {
1612                         addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1613                                               dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1614                 }
1615                 if (rt)
1616                         dst_release(&rt->u.dst);
1617         }
1618
1619         /* Try to figure out our local address for this prefix */
1620
1621         if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1622                 struct inet6_ifaddr * ifp;
1623                 struct in6_addr addr;
1624                 int create = 0, update_lft = 0;
1625
1626                 if (pinfo->prefix_len == 64) {
1627                         memcpy(&addr, &pinfo->prefix, 8);
1628                         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1629                             ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1630                                 in6_dev_put(in6_dev);
1631                                 return;
1632                         }
1633                         goto ok;
1634                 }
1635                 if (net_ratelimit())
1636                         printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1637                                pinfo->prefix_len);
1638                 in6_dev_put(in6_dev);
1639                 return;
1640
1641 ok:
1642
1643                 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1644
1645                 if (ifp == NULL && valid_lft) {
1646                         int max_addresses = in6_dev->cnf.max_addresses;
1647
1648                         /* Do not allow to create too much of autoconfigured
1649                          * addresses; this would be too easy way to crash kernel.
1650                          */
1651                         if (!max_addresses ||
1652                             ipv6_count_addresses(in6_dev) < max_addresses)
1653                                 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1654                                                     addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1655
1656                         if (!ifp || IS_ERR(ifp)) {
1657                                 in6_dev_put(in6_dev);
1658                                 return;
1659                         }
1660
1661                         update_lft = create = 1;
1662                         ifp->cstamp = jiffies;
1663                         addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1664                 }
1665
1666                 if (ifp) {
1667                         int flags;
1668                         unsigned long now;
1669 #ifdef CONFIG_IPV6_PRIVACY
1670                         struct inet6_ifaddr *ift;
1671 #endif
1672                         u32 stored_lft;
1673
1674                         /* update lifetime (RFC2462 5.5.3 e) */
1675                         spin_lock(&ifp->lock);
1676                         now = jiffies;
1677                         if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1678                                 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1679                         else
1680                                 stored_lft = 0;
1681                         if (!update_lft && stored_lft) {
1682                                 if (valid_lft > MIN_VALID_LIFETIME ||
1683                                     valid_lft > stored_lft)
1684                                         update_lft = 1;
1685                                 else if (stored_lft <= MIN_VALID_LIFETIME) {
1686                                         /* valid_lft <= stored_lft is always true */
1687                                         /* XXX: IPsec */
1688                                         update_lft = 0;
1689                                 } else {
1690                                         valid_lft = MIN_VALID_LIFETIME;
1691                                         if (valid_lft < prefered_lft)
1692                                                 prefered_lft = valid_lft;
1693                                         update_lft = 1;
1694                                 }
1695                         }
1696
1697                         if (update_lft) {
1698                                 ifp->valid_lft = valid_lft;
1699                                 ifp->prefered_lft = prefered_lft;
1700                                 ifp->tstamp = now;
1701                                 flags = ifp->flags;
1702                                 ifp->flags &= ~IFA_F_DEPRECATED;
1703                                 spin_unlock(&ifp->lock);
1704
1705                                 if (!(flags&IFA_F_TENTATIVE))
1706                                         ipv6_ifa_notify(0, ifp);
1707                         } else
1708                                 spin_unlock(&ifp->lock);
1709
1710 #ifdef CONFIG_IPV6_PRIVACY
1711                         read_lock_bh(&in6_dev->lock);
1712                         /* update all temporary addresses in the list */
1713                         for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1714                                 /*
1715                                  * When adjusting the lifetimes of an existing
1716                                  * temporary address, only lower the lifetimes.
1717                                  * Implementations must not increase the
1718                                  * lifetimes of an existing temporary address
1719                                  * when processing a Prefix Information Option.
1720                                  */
1721                                 spin_lock(&ift->lock);
1722                                 flags = ift->flags;
1723                                 if (ift->valid_lft > valid_lft &&
1724                                     ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1725                                         ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1726                                 if (ift->prefered_lft > prefered_lft &&
1727                                     ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1728                                         ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1729                                 spin_unlock(&ift->lock);
1730                                 if (!(flags&IFA_F_TENTATIVE))
1731                                         ipv6_ifa_notify(0, ift);
1732                         }
1733
1734                         if (create && in6_dev->cnf.use_tempaddr > 0) {
1735                                 /*
1736                                  * When a new public address is created as described in [ADDRCONF],
1737                                  * also create a new temporary address.
1738                                  */
1739                                 read_unlock_bh(&in6_dev->lock); 
1740                                 ipv6_create_tempaddr(ifp, NULL);
1741                         } else {
1742                                 read_unlock_bh(&in6_dev->lock);
1743                         }
1744 #endif
1745                         in6_ifa_put(ifp);
1746                         addrconf_verify(0);
1747                 }
1748         }
1749         inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1750         in6_dev_put(in6_dev);
1751 }
1752
1753 /*
1754  *      Set destination address.
1755  *      Special case for SIT interfaces where we create a new "virtual"
1756  *      device.
1757  */
1758 int addrconf_set_dstaddr(void __user *arg)
1759 {
1760         struct in6_ifreq ireq;
1761         struct net_device *dev;
1762         int err = -EINVAL;
1763
1764         rtnl_lock();
1765
1766         err = -EFAULT;
1767         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1768                 goto err_exit;
1769
1770         dev = __dev_get_by_index(ireq.ifr6_ifindex);
1771
1772         err = -ENODEV;
1773         if (dev == NULL)
1774                 goto err_exit;
1775
1776         if (dev->type == ARPHRD_SIT) {
1777                 struct ifreq ifr;
1778                 mm_segment_t    oldfs;
1779                 struct ip_tunnel_parm p;
1780
1781                 err = -EADDRNOTAVAIL;
1782                 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1783                         goto err_exit;
1784
1785                 memset(&p, 0, sizeof(p));
1786                 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1787                 p.iph.saddr = 0;
1788                 p.iph.version = 4;
1789                 p.iph.ihl = 5;
1790                 p.iph.protocol = IPPROTO_IPV6;
1791                 p.iph.ttl = 64;
1792                 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1793
1794                 oldfs = get_fs(); set_fs(KERNEL_DS);
1795                 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1796                 set_fs(oldfs);
1797
1798                 if (err == 0) {
1799                         err = -ENOBUFS;
1800                         if ((dev = __dev_get_by_name(p.name)) == NULL)
1801                                 goto err_exit;
1802                         err = dev_open(dev);
1803                 }
1804         }
1805
1806 err_exit:
1807         rtnl_unlock();
1808         return err;
1809 }
1810
1811 /*
1812  *      Manual configuration of address on an interface
1813  */
1814 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1815 {
1816         struct inet6_ifaddr *ifp;
1817         struct inet6_dev *idev;
1818         struct net_device *dev;
1819         int scope;
1820
1821         ASSERT_RTNL();
1822         
1823         if ((dev = __dev_get_by_index(ifindex)) == NULL)
1824                 return -ENODEV;
1825         
1826         if (!(dev->flags&IFF_UP))
1827                 return -ENETDOWN;
1828
1829         if ((idev = addrconf_add_dev(dev)) == NULL)
1830                 return -ENOBUFS;
1831
1832         scope = ipv6_addr_scope(pfx);
1833
1834         ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1835         if (!IS_ERR(ifp)) {
1836                 addrconf_dad_start(ifp, 0);
1837                 in6_ifa_put(ifp);
1838                 return 0;
1839         }
1840
1841         return PTR_ERR(ifp);
1842 }
1843
1844 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1845 {
1846         struct inet6_ifaddr *ifp;
1847         struct inet6_dev *idev;
1848         struct net_device *dev;
1849         
1850         if ((dev = __dev_get_by_index(ifindex)) == NULL)
1851                 return -ENODEV;
1852
1853         if ((idev = __in6_dev_get(dev)) == NULL)
1854                 return -ENXIO;
1855
1856         read_lock_bh(&idev->lock);
1857         for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1858                 if (ifp->prefix_len == plen &&
1859                     ipv6_addr_equal(pfx, &ifp->addr)) {
1860                         in6_ifa_hold(ifp);
1861                         read_unlock_bh(&idev->lock);
1862                         
1863                         ipv6_del_addr(ifp);
1864
1865                         /* If the last address is deleted administratively,
1866                            disable IPv6 on this interface.
1867                          */
1868                         if (idev->addr_list == NULL)
1869                                 addrconf_ifdown(idev->dev, 1);
1870                         return 0;
1871                 }
1872         }
1873         read_unlock_bh(&idev->lock);
1874         return -EADDRNOTAVAIL;
1875 }
1876
1877
1878 int addrconf_add_ifaddr(void __user *arg)
1879 {
1880         struct in6_ifreq ireq;
1881         int err;
1882         
1883         if (!capable(CAP_NET_ADMIN))
1884                 return -EPERM;
1885         
1886         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1887                 return -EFAULT;
1888
1889         rtnl_lock();
1890         err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1891         rtnl_unlock();
1892         return err;
1893 }
1894
1895 int addrconf_del_ifaddr(void __user *arg)
1896 {
1897         struct in6_ifreq ireq;
1898         int err;
1899         
1900         if (!capable(CAP_NET_ADMIN))
1901                 return -EPERM;
1902
1903         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1904                 return -EFAULT;
1905
1906         rtnl_lock();
1907         err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1908         rtnl_unlock();
1909         return err;
1910 }
1911
1912 static void sit_add_v4_addrs(struct inet6_dev *idev)
1913 {
1914         struct inet6_ifaddr * ifp;
1915         struct in6_addr addr;
1916         struct net_device *dev;
1917         int scope;
1918
1919         ASSERT_RTNL();
1920
1921         memset(&addr, 0, sizeof(struct in6_addr));
1922         memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1923
1924         if (idev->dev->flags&IFF_POINTOPOINT) {
1925                 addr.s6_addr32[0] = htonl(0xfe800000);
1926                 scope = IFA_LINK;
1927         } else {
1928                 scope = IPV6_ADDR_COMPATv4;
1929         }
1930
1931         if (addr.s6_addr32[3]) {
1932                 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1933                 if (!IS_ERR(ifp)) {
1934                         spin_lock_bh(&ifp->lock);
1935                         ifp->flags &= ~IFA_F_TENTATIVE;
1936                         spin_unlock_bh(&ifp->lock);
1937                         ipv6_ifa_notify(RTM_NEWADDR, ifp);
1938                         in6_ifa_put(ifp);
1939                 }
1940                 return;
1941         }
1942
1943         for (dev = dev_base; dev != NULL; dev = dev->next) {
1944                 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1945                 if (in_dev && (dev->flags & IFF_UP)) {
1946                         struct in_ifaddr * ifa;
1947
1948                         int flag = scope;
1949
1950                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1951                                 int plen;
1952
1953                                 addr.s6_addr32[3] = ifa->ifa_local;
1954
1955                                 if (ifa->ifa_scope == RT_SCOPE_LINK)
1956                                         continue;
1957                                 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1958                                         if (idev->dev->flags&IFF_POINTOPOINT)
1959                                                 continue;
1960                                         flag |= IFA_HOST;
1961                                 }
1962                                 if (idev->dev->flags&IFF_POINTOPOINT)
1963                                         plen = 64;
1964                                 else
1965                                         plen = 96;
1966
1967                                 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1968                                                     IFA_F_PERMANENT);
1969                                 if (!IS_ERR(ifp)) {
1970                                         spin_lock_bh(&ifp->lock);
1971                                         ifp->flags &= ~IFA_F_TENTATIVE;
1972                                         spin_unlock_bh(&ifp->lock);
1973                                         ipv6_ifa_notify(RTM_NEWADDR, ifp);
1974                                         in6_ifa_put(ifp);
1975                                 }
1976                         }
1977                 }
1978         }
1979 }
1980
1981 static void init_loopback(struct net_device *dev)
1982 {
1983         struct inet6_dev  *idev;
1984         struct inet6_ifaddr * ifp;
1985
1986         /* ::1 */
1987
1988         ASSERT_RTNL();
1989
1990         if ((idev = ipv6_find_idev(dev)) == NULL) {
1991                 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1992                 return;
1993         }
1994
1995         ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1996         if (!IS_ERR(ifp)) {
1997                 spin_lock_bh(&ifp->lock);
1998                 ifp->flags &= ~IFA_F_TENTATIVE;
1999                 spin_unlock_bh(&ifp->lock);
2000                 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2001                 in6_ifa_put(ifp);
2002         }
2003 }
2004
2005 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2006 {
2007         struct inet6_ifaddr * ifp;
2008
2009         ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2010         if (!IS_ERR(ifp)) {
2011                 addrconf_dad_start(ifp, 0);
2012                 in6_ifa_put(ifp);
2013         }
2014 }
2015
2016 static void addrconf_dev_config(struct net_device *dev)
2017 {
2018         struct in6_addr addr;
2019         struct inet6_dev    * idev;
2020
2021         ASSERT_RTNL();
2022
2023         if ((dev->type != ARPHRD_ETHER) && 
2024             (dev->type != ARPHRD_FDDI) &&
2025             (dev->type != ARPHRD_IEEE802_TR) &&
2026             (dev->type != ARPHRD_ARCNET) &&
2027             (dev->type != ARPHRD_INFINIBAND)) {
2028                 /* Alas, we support only Ethernet autoconfiguration. */
2029                 return;
2030         }
2031
2032         idev = addrconf_add_dev(dev);
2033         if (idev == NULL)
2034                 return;
2035
2036         memset(&addr, 0, sizeof(struct in6_addr));
2037         addr.s6_addr32[0] = htonl(0xFE800000);
2038
2039         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2040                 addrconf_add_linklocal(idev, &addr);
2041 }
2042
2043 static void addrconf_sit_config(struct net_device *dev)
2044 {
2045         struct inet6_dev *idev;
2046
2047         ASSERT_RTNL();
2048
2049         /* 
2050          * Configure the tunnel with one of our IPv4 
2051          * addresses... we should configure all of 
2052          * our v4 addrs in the tunnel
2053          */
2054
2055         if ((idev = ipv6_find_idev(dev)) == NULL) {
2056                 printk(KERN_DEBUG "init sit: add_dev failed\n");
2057                 return;
2058         }
2059
2060         sit_add_v4_addrs(idev);
2061
2062         if (dev->flags&IFF_POINTOPOINT) {
2063                 addrconf_add_mroute(dev);
2064                 addrconf_add_lroute(dev);
2065         } else
2066                 sit_route_add(dev);
2067 }
2068
2069 static inline int
2070 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2071 {
2072         struct in6_addr lladdr;
2073
2074         if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2075                 addrconf_add_linklocal(idev, &lladdr);
2076                 return 0;
2077         }
2078         return -1;
2079 }
2080
2081 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2082 {
2083         struct net_device *link_dev;
2084
2085         /* first try to inherit the link-local address from the link device */
2086         if (idev->dev->iflink &&
2087             (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2088                 if (!ipv6_inherit_linklocal(idev, link_dev))
2089                         return;
2090         }
2091         /* then try to inherit it from any device */
2092         for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2093                 if (!ipv6_inherit_linklocal(idev, link_dev))
2094                         return;
2095         }
2096         printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2097 }
2098
2099 /*
2100  * Autoconfigure tunnel with a link-local address so routing protocols,
2101  * DHCPv6, MLD etc. can be run over the virtual link
2102  */
2103
2104 static void addrconf_ip6_tnl_config(struct net_device *dev)
2105 {
2106         struct inet6_dev *idev;
2107
2108         ASSERT_RTNL();
2109
2110         if ((idev = addrconf_add_dev(dev)) == NULL) {
2111                 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2112                 return;
2113         }
2114         ip6_tnl_add_linklocal(idev);
2115 }
2116
2117 static int addrconf_notify(struct notifier_block *this, unsigned long event, 
2118                            void * data)
2119 {
2120         struct net_device *dev = (struct net_device *) data;
2121         struct inet6_dev *idev = __in6_dev_get(dev);
2122         int run_pending = 0;
2123
2124         switch(event) {
2125         case NETDEV_UP:
2126         case NETDEV_CHANGE:
2127                 if (event == NETDEV_UP) {
2128                         if (!netif_carrier_ok(dev)) {
2129                                 /* device is not ready yet. */
2130                                 printk(KERN_INFO
2131                                         "ADDRCONF(NETDEV_UP): %s: "
2132                                         "link is not ready\n",
2133                                         dev->name);
2134                                 break;
2135                         }
2136
2137                         if (idev)
2138                                 idev->if_flags |= IF_READY;
2139                 } else {
2140                         if (!netif_carrier_ok(dev)) {
2141                                 /* device is still not ready. */
2142                                 break;
2143                         }
2144
2145                         if (idev) {
2146                                 if (idev->if_flags & IF_READY) {
2147                                         /* device is already configured. */
2148                                         break;
2149                                 }
2150                                 idev->if_flags |= IF_READY;
2151                         }
2152
2153                         printk(KERN_INFO
2154                                         "ADDRCONF(NETDEV_CHANGE): %s: "
2155                                         "link becomes ready\n",
2156                                         dev->name);
2157
2158                         run_pending = 1;
2159                 }
2160
2161                 switch(dev->type) {
2162                 case ARPHRD_SIT:
2163                         addrconf_sit_config(dev);
2164                         break;
2165                 case ARPHRD_TUNNEL6:
2166                         addrconf_ip6_tnl_config(dev);
2167                         break;
2168                 case ARPHRD_LOOPBACK:
2169                         init_loopback(dev);
2170                         break;
2171
2172                 default:
2173                         addrconf_dev_config(dev);
2174                         break;
2175                 };
2176                 if (idev) {
2177                         if (run_pending)
2178                                 addrconf_dad_run(idev);
2179
2180                         /* If the MTU changed during the interface down, when the
2181                            interface up, the changed MTU must be reflected in the
2182                            idev as well as routers.
2183                          */
2184                         if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2185                                 rt6_mtu_change(dev, dev->mtu);
2186                                 idev->cnf.mtu6 = dev->mtu;
2187                         }
2188                         idev->tstamp = jiffies;
2189                         inet6_ifinfo_notify(RTM_NEWLINK, idev);
2190                         /* If the changed mtu during down is lower than IPV6_MIN_MTU
2191                            stop IPv6 on this interface.
2192                          */
2193                         if (dev->mtu < IPV6_MIN_MTU)
2194                                 addrconf_ifdown(dev, event != NETDEV_DOWN);
2195                 }
2196                 break;
2197
2198         case NETDEV_CHANGEMTU:
2199                 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2200                         rt6_mtu_change(dev, dev->mtu);
2201                         idev->cnf.mtu6 = dev->mtu;
2202                         break;
2203                 }
2204
2205                 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2206
2207         case NETDEV_DOWN:
2208         case NETDEV_UNREGISTER:
2209                 /*
2210                  *      Remove all addresses from this interface.
2211                  */
2212                 addrconf_ifdown(dev, event != NETDEV_DOWN);
2213                 break;
2214
2215         case NETDEV_CHANGENAME:
2216 #ifdef CONFIG_SYSCTL
2217                 if (idev) {
2218                         addrconf_sysctl_unregister(&idev->cnf);
2219                         neigh_sysctl_unregister(idev->nd_parms);
2220                         neigh_sysctl_register(dev, idev->nd_parms,
2221                                               NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2222                                               &ndisc_ifinfo_sysctl_change,
2223                                               NULL);
2224                         addrconf_sysctl_register(idev, &idev->cnf);
2225                 }
2226 #endif
2227                 break;
2228         };
2229
2230         return NOTIFY_OK;
2231 }
2232
2233 /*
2234  *      addrconf module should be notified of a device going up
2235  */
2236 static struct notifier_block ipv6_dev_notf = {
2237         .notifier_call = addrconf_notify,
2238         .priority = 0
2239 };
2240
2241 static int addrconf_ifdown(struct net_device *dev, int how)
2242 {
2243         struct inet6_dev *idev;
2244         struct inet6_ifaddr *ifa, **bifa;
2245         int i;
2246
2247         ASSERT_RTNL();
2248
2249         if (dev == &loopback_dev && how == 1)
2250                 how = 0;
2251
2252         rt6_ifdown(dev);
2253         neigh_ifdown(&nd_tbl, dev);
2254
2255         idev = __in6_dev_get(dev);
2256         if (idev == NULL)
2257                 return -ENODEV;
2258
2259         /* Step 1: remove reference to ipv6 device from parent device.
2260                    Do not dev_put!
2261          */
2262         if (how == 1) {
2263                 write_lock_bh(&addrconf_lock);
2264                 dev->ip6_ptr = NULL;
2265                 idev->dead = 1;
2266                 write_unlock_bh(&addrconf_lock);
2267
2268                 /* Step 1.5: remove snmp6 entry */
2269                 snmp6_unregister_dev(idev);
2270
2271         }
2272
2273         /* Step 2: clear hash table */
2274         for (i=0; i<IN6_ADDR_HSIZE; i++) {
2275                 bifa = &inet6_addr_lst[i];
2276
2277                 write_lock_bh(&addrconf_hash_lock);
2278                 while ((ifa = *bifa) != NULL) {
2279                         if (ifa->idev == idev) {
2280                                 *bifa = ifa->lst_next;
2281                                 ifa->lst_next = NULL;
2282                                 addrconf_del_timer(ifa);
2283                                 in6_ifa_put(ifa);
2284                                 continue;
2285                         }
2286                         bifa = &ifa->lst_next;
2287                 }
2288                 write_unlock_bh(&addrconf_hash_lock);
2289         }
2290
2291         write_lock_bh(&idev->lock);
2292
2293         /* Step 3: clear flags for stateless addrconf */
2294         if (how != 1)
2295                 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2296
2297         /* Step 4: clear address list */
2298 #ifdef CONFIG_IPV6_PRIVACY
2299         if (how == 1 && del_timer(&idev->regen_timer))
2300                 in6_dev_put(idev);
2301
2302         /* clear tempaddr list */
2303         while ((ifa = idev->tempaddr_list) != NULL) {
2304                 idev->tempaddr_list = ifa->tmp_next;
2305                 ifa->tmp_next = NULL;
2306                 ifa->dead = 1;
2307                 write_unlock_bh(&idev->lock);
2308                 spin_lock_bh(&ifa->lock);
2309
2310                 if (ifa->ifpub) {
2311                         in6_ifa_put(ifa->ifpub);
2312                         ifa->ifpub = NULL;
2313                 }
2314                 spin_unlock_bh(&ifa->lock);
2315                 in6_ifa_put(ifa);
2316                 write_lock_bh(&idev->lock);
2317         }
2318 #endif
2319         while ((ifa = idev->addr_list) != NULL) {
2320                 idev->addr_list = ifa->if_next;
2321                 ifa->if_next = NULL;
2322                 ifa->dead = 1;
2323                 addrconf_del_timer(ifa);
2324                 write_unlock_bh(&idev->lock);
2325
2326                 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2327                 in6_ifa_put(ifa);
2328
2329                 write_lock_bh(&idev->lock);
2330         }
2331         write_unlock_bh(&idev->lock);
2332
2333         /* Step 5: Discard multicast list */
2334
2335         if (how == 1)
2336                 ipv6_mc_destroy_dev(idev);
2337         else
2338                 ipv6_mc_down(idev);
2339
2340         /* Step 5: netlink notification of this interface */
2341         idev->tstamp = jiffies;
2342         inet6_ifinfo_notify(RTM_DELLINK, idev);
2343         
2344         /* Shot the device (if unregistered) */
2345
2346         if (how == 1) {
2347 #ifdef CONFIG_SYSCTL
2348                 addrconf_sysctl_unregister(&idev->cnf);
2349                 neigh_sysctl_unregister(idev->nd_parms);
2350 #endif
2351                 neigh_parms_release(&nd_tbl, idev->nd_parms);
2352                 neigh_ifdown(&nd_tbl, dev);
2353                 in6_dev_put(idev);
2354         }
2355         return 0;
2356 }
2357
2358 static void addrconf_rs_timer(unsigned long data)
2359 {
2360         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2361
2362         if (ifp->idev->cnf.forwarding)
2363                 goto out;
2364
2365         if (ifp->idev->if_flags & IF_RA_RCVD) {
2366                 /*
2367                  *      Announcement received after solicitation
2368                  *      was sent
2369                  */
2370                 goto out;
2371         }
2372
2373         spin_lock(&ifp->lock);
2374         if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2375                 struct in6_addr all_routers;
2376
2377                 /* The wait after the last probe can be shorter */
2378                 addrconf_mod_timer(ifp, AC_RS,
2379                                    (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2380                                    ifp->idev->cnf.rtr_solicit_delay :
2381                                    ifp->idev->cnf.rtr_solicit_interval);
2382                 spin_unlock(&ifp->lock);
2383
2384                 ipv6_addr_all_routers(&all_routers);
2385
2386                 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2387         } else {
2388                 spin_unlock(&ifp->lock);
2389                 /*
2390                  * Note: we do not support deprecated "all on-link"
2391                  * assumption any longer.
2392                  */
2393                 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2394                        ifp->idev->dev->name);
2395         }
2396
2397 out:
2398         in6_ifa_put(ifp);
2399 }
2400
2401 /*
2402  *      Duplicate Address Detection
2403  */
2404 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2405 {
2406         unsigned long rand_num;
2407         struct inet6_dev *idev = ifp->idev;
2408
2409         rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2410         ifp->probes = idev->cnf.dad_transmits;
2411         addrconf_mod_timer(ifp, AC_DAD, rand_num);
2412 }
2413
2414 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2415 {
2416         struct inet6_dev *idev = ifp->idev;
2417         struct net_device *dev = idev->dev;
2418
2419         addrconf_join_solict(dev, &ifp->addr);
2420
2421         if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2422                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2423                                         flags);
2424
2425         net_srandom(ifp->addr.s6_addr32[3]);
2426
2427         read_lock_bh(&idev->lock);
2428         if (ifp->dead)
2429                 goto out;
2430         spin_lock_bh(&ifp->lock);
2431
2432         if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2433             !(ifp->flags&IFA_F_TENTATIVE)) {
2434                 ifp->flags &= ~IFA_F_TENTATIVE;
2435                 spin_unlock_bh(&ifp->lock);
2436                 read_unlock_bh(&idev->lock);
2437
2438                 addrconf_dad_completed(ifp);
2439                 return;
2440         }
2441
2442         if (!(idev->if_flags & IF_READY)) {
2443                 spin_unlock_bh(&ifp->lock);
2444                 read_unlock_bh(&idev->lock);
2445                 /*
2446                  * If the defice is not ready:
2447                  * - keep it tentative if it is a permanent address.
2448                  * - otherwise, kill it.
2449                  */
2450                 in6_ifa_hold(ifp);
2451                 addrconf_dad_stop(ifp);
2452                 return;
2453         }
2454         addrconf_dad_kick(ifp);
2455         spin_unlock_bh(&ifp->lock);
2456 out:
2457         read_unlock_bh(&idev->lock);
2458 }
2459
2460 static void addrconf_dad_timer(unsigned long data)
2461 {
2462         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2463         struct inet6_dev *idev = ifp->idev;
2464         struct in6_addr unspec;
2465         struct in6_addr mcaddr;
2466
2467         read_lock_bh(&idev->lock);
2468         if (idev->dead) {
2469                 read_unlock_bh(&idev->lock);
2470                 goto out;
2471         }
2472         spin_lock_bh(&ifp->lock);
2473         if (ifp->probes == 0) {
2474                 /*
2475                  * DAD was successful
2476                  */
2477
2478                 ifp->flags &= ~IFA_F_TENTATIVE;
2479                 spin_unlock_bh(&ifp->lock);
2480                 read_unlock_bh(&idev->lock);
2481
2482                 addrconf_dad_completed(ifp);
2483
2484                 goto out;
2485         }
2486
2487         ifp->probes--;
2488         addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2489         spin_unlock_bh(&ifp->lock);
2490         read_unlock_bh(&idev->lock);
2491
2492         /* send a neighbour solicitation for our addr */
2493         memset(&unspec, 0, sizeof(unspec));
2494         addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2495         ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2496 out:
2497         in6_ifa_put(ifp);
2498 }
2499
2500 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2501 {
2502         struct net_device *     dev = ifp->idev->dev;
2503
2504         /*
2505          *      Configure the address for reception. Now it is valid.
2506          */
2507
2508         ipv6_ifa_notify(RTM_NEWADDR, ifp);
2509
2510         /* If added prefix is link local and forwarding is off,
2511            start sending router solicitations.
2512          */
2513
2514         if (ifp->idev->cnf.forwarding == 0 &&
2515             ifp->idev->cnf.rtr_solicits > 0 &&
2516             (dev->flags&IFF_LOOPBACK) == 0 &&
2517             (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2518                 struct in6_addr all_routers;
2519
2520                 ipv6_addr_all_routers(&all_routers);
2521
2522                 /*
2523                  *      If a host as already performed a random delay
2524                  *      [...] as part of DAD [...] there is no need
2525                  *      to delay again before sending the first RS
2526                  */
2527                 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2528
2529                 spin_lock_bh(&ifp->lock);
2530                 ifp->probes = 1;
2531                 ifp->idev->if_flags |= IF_RS_SENT;
2532                 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2533                 spin_unlock_bh(&ifp->lock);
2534         }
2535 }
2536
2537 static void addrconf_dad_run(struct inet6_dev *idev) {
2538         struct inet6_ifaddr *ifp;
2539
2540         read_lock_bh(&idev->lock);
2541         for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2542                 spin_lock_bh(&ifp->lock);
2543                 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2544                         spin_unlock_bh(&ifp->lock);
2545                         continue;
2546                 }
2547                 spin_unlock_bh(&ifp->lock);
2548                 addrconf_dad_kick(ifp);
2549         }
2550         read_unlock_bh(&idev->lock);
2551 }
2552
2553 #ifdef CONFIG_PROC_FS
2554 struct if6_iter_state {
2555         int bucket;
2556 };
2557
2558 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2559 {
2560         struct inet6_ifaddr *ifa = NULL;
2561         struct if6_iter_state *state = seq->private;
2562
2563         for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2564                 ifa = inet6_addr_lst[state->bucket];
2565                 if (ifa)
2566                         break;
2567         }
2568         return ifa;
2569 }
2570
2571 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2572 {
2573         struct if6_iter_state *state = seq->private;
2574
2575         ifa = ifa->lst_next;
2576 try_again:
2577         if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2578                 ifa = inet6_addr_lst[state->bucket];
2579                 goto try_again;
2580         }
2581         return ifa;
2582 }
2583
2584 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2585 {
2586         struct inet6_ifaddr *ifa = if6_get_first(seq);
2587
2588         if (ifa)
2589                 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2590                         --pos;
2591         return pos ? NULL : ifa;
2592 }
2593
2594 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2595 {
2596         read_lock_bh(&addrconf_hash_lock);
2597         return if6_get_idx(seq, *pos);
2598 }
2599
2600 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2601 {
2602         struct inet6_ifaddr *ifa;
2603
2604         ifa = if6_get_next(seq, v);
2605         ++*pos;
2606         return ifa;
2607 }
2608
2609 static void if6_seq_stop(struct seq_file *seq, void *v)
2610 {
2611         read_unlock_bh(&addrconf_hash_lock);
2612 }
2613
2614 static int if6_seq_show(struct seq_file *seq, void *v)
2615 {
2616         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2617         seq_printf(seq,
2618                    NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2619                    NIP6(ifp->addr),
2620                    ifp->idev->dev->ifindex,
2621                    ifp->prefix_len,
2622                    ifp->scope,
2623                    ifp->flags,
2624                    ifp->idev->dev->name);
2625         return 0;
2626 }
2627
2628 static struct seq_operations if6_seq_ops = {
2629         .start  = if6_seq_start,
2630         .next   = if6_seq_next,
2631         .show   = if6_seq_show,
2632         .stop   = if6_seq_stop,
2633 };
2634
2635 static int if6_seq_open(struct inode *inode, struct file *file)
2636 {
2637         struct seq_file *seq;
2638         int rc = -ENOMEM;
2639         struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2640
2641         if (!s)
2642                 goto out;
2643         memset(s, 0, sizeof(*s));
2644
2645         rc = seq_open(file, &if6_seq_ops);
2646         if (rc)
2647                 goto out_kfree;
2648
2649         seq = file->private_data;
2650         seq->private = s;
2651 out:
2652         return rc;
2653 out_kfree:
2654         kfree(s);
2655         goto out;
2656 }
2657
2658 static struct file_operations if6_fops = {
2659         .owner          = THIS_MODULE,
2660         .open           = if6_seq_open,
2661         .read           = seq_read,
2662         .llseek         = seq_lseek,
2663         .release        = seq_release_private,
2664 };
2665
2666 int __init if6_proc_init(void)
2667 {
2668         if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2669                 return -ENOMEM;
2670         return 0;
2671 }
2672
2673 void if6_proc_exit(void)
2674 {
2675         proc_net_remove("if_inet6");
2676 }
2677 #endif  /* CONFIG_PROC_FS */
2678
2679 /*
2680  *      Periodic address status verification
2681  */
2682
2683 static void addrconf_verify(unsigned long foo)
2684 {
2685         struct inet6_ifaddr *ifp;
2686         unsigned long now, next;
2687         int i;
2688
2689         spin_lock_bh(&addrconf_verify_lock);
2690         now = jiffies;
2691         next = now + ADDR_CHECK_FREQUENCY;
2692
2693         del_timer(&addr_chk_timer);
2694
2695         for (i=0; i < IN6_ADDR_HSIZE; i++) {
2696
2697 restart:
2698                 read_lock(&addrconf_hash_lock);
2699                 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2700                         unsigned long age;
2701 #ifdef CONFIG_IPV6_PRIVACY
2702                         unsigned long regen_advance;
2703 #endif
2704
2705                         if (ifp->flags & IFA_F_PERMANENT)
2706                                 continue;
2707
2708                         spin_lock(&ifp->lock);
2709                         age = (now - ifp->tstamp) / HZ;
2710
2711 #ifdef CONFIG_IPV6_PRIVACY
2712                         regen_advance = ifp->idev->cnf.regen_max_retry * 
2713                                         ifp->idev->cnf.dad_transmits * 
2714                                         ifp->idev->nd_parms->retrans_time / HZ;
2715 #endif
2716
2717                         if (age >= ifp->valid_lft) {
2718                                 spin_unlock(&ifp->lock);
2719                                 in6_ifa_hold(ifp);
2720                                 read_unlock(&addrconf_hash_lock);
2721                                 ipv6_del_addr(ifp);
2722                                 goto restart;
2723                         } else if (age >= ifp->prefered_lft) {
2724                                 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2725                                 int deprecate = 0;
2726
2727                                 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2728                                         deprecate = 1;
2729                                         ifp->flags |= IFA_F_DEPRECATED;
2730                                 }
2731
2732                                 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2733                                         next = ifp->tstamp + ifp->valid_lft * HZ;
2734
2735                                 spin_unlock(&ifp->lock);
2736
2737                                 if (deprecate) {
2738                                         in6_ifa_hold(ifp);
2739                                         read_unlock(&addrconf_hash_lock);
2740
2741                                         ipv6_ifa_notify(0, ifp);
2742                                         in6_ifa_put(ifp);
2743                                         goto restart;
2744                                 }
2745 #ifdef CONFIG_IPV6_PRIVACY
2746                         } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2747                                    !(ifp->flags&IFA_F_TENTATIVE)) {
2748                                 if (age >= ifp->prefered_lft - regen_advance) {
2749                                         struct inet6_ifaddr *ifpub = ifp->ifpub;
2750                                         if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2751                                                 next = ifp->tstamp + ifp->prefered_lft * HZ;
2752                                         if (!ifp->regen_count && ifpub) {
2753                                                 ifp->regen_count++;
2754                                                 in6_ifa_hold(ifp);
2755                                                 in6_ifa_hold(ifpub);
2756                                                 spin_unlock(&ifp->lock);
2757                                                 read_unlock(&addrconf_hash_lock);
2758                                                 spin_lock(&ifpub->lock);
2759                                                 ifpub->regen_count = 0;
2760                                                 spin_unlock(&ifpub->lock);
2761                                                 ipv6_create_tempaddr(ifpub, ifp);
2762                                                 in6_ifa_put(ifpub);
2763                                                 in6_ifa_put(ifp);
2764                                                 goto restart;
2765                                         }
2766                                 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2767                                         next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2768                                 spin_unlock(&ifp->lock);
2769 #endif
2770                         } else {
2771                                 /* ifp->prefered_lft <= ifp->valid_lft */
2772                                 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2773                                         next = ifp->tstamp + ifp->prefered_lft * HZ;
2774                                 spin_unlock(&ifp->lock);
2775                         }
2776                 }
2777                 read_unlock(&addrconf_hash_lock);
2778         }
2779
2780         addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2781         add_timer(&addr_chk_timer);
2782         spin_unlock_bh(&addrconf_verify_lock);
2783 }
2784
2785 static int
2786 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2787 {
2788         struct rtattr **rta = arg;
2789         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2790         struct in6_addr *pfx;
2791
2792         pfx = NULL;
2793         if (rta[IFA_ADDRESS-1]) {
2794                 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2795                         return -EINVAL;
2796                 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2797         }
2798         if (rta[IFA_LOCAL-1]) {
2799                 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2800                         return -EINVAL;
2801                 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2802         }
2803         if (pfx == NULL)
2804                 return -EINVAL;
2805
2806         return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2807 }
2808
2809 static int
2810 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2811 {
2812         struct rtattr  **rta = arg;
2813         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2814         struct in6_addr *pfx;
2815
2816         pfx = NULL;
2817         if (rta[IFA_ADDRESS-1]) {
2818                 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2819                         return -EINVAL;
2820                 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2821         }
2822         if (rta[IFA_LOCAL-1]) {
2823                 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2824                         return -EINVAL;
2825                 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2826         }
2827         if (pfx == NULL)
2828                 return -EINVAL;
2829
2830         return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2831 }
2832
2833 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2834                              u32 pid, u32 seq, int event, unsigned int flags)
2835 {
2836         struct ifaddrmsg *ifm;
2837         struct nlmsghdr  *nlh;
2838         struct ifa_cacheinfo ci;
2839         unsigned char    *b = skb->tail;
2840
2841         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2842         ifm = NLMSG_DATA(nlh);
2843         ifm->ifa_family = AF_INET6;
2844         ifm->ifa_prefixlen = ifa->prefix_len;
2845         ifm->ifa_flags = ifa->flags;
2846         ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2847         if (ifa->scope&IFA_HOST)
2848                 ifm->ifa_scope = RT_SCOPE_HOST;
2849         else if (ifa->scope&IFA_LINK)
2850                 ifm->ifa_scope = RT_SCOPE_LINK;
2851         else if (ifa->scope&IFA_SITE)
2852                 ifm->ifa_scope = RT_SCOPE_SITE;
2853         ifm->ifa_index = ifa->idev->dev->ifindex;
2854         RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2855         if (!(ifa->flags&IFA_F_PERMANENT)) {
2856                 ci.ifa_prefered = ifa->prefered_lft;
2857                 ci.ifa_valid = ifa->valid_lft;
2858                 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2859                         long tval = (jiffies - ifa->tstamp)/HZ;
2860                         ci.ifa_prefered -= tval;
2861                         if (ci.ifa_valid != INFINITY_LIFE_TIME)
2862                                 ci.ifa_valid -= tval;
2863                 }
2864         } else {
2865                 ci.ifa_prefered = INFINITY_LIFE_TIME;
2866                 ci.ifa_valid = INFINITY_LIFE_TIME;
2867         }
2868         ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2869                     + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2870         ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2871                     + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2872         RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2873         nlh->nlmsg_len = skb->tail - b;
2874         return skb->len;
2875
2876 nlmsg_failure:
2877 rtattr_failure:
2878         skb_trim(skb, b - skb->data);
2879         return -1;
2880 }
2881
2882 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2883                                 u32 pid, u32 seq, int event, u16 flags)
2884 {
2885         struct ifaddrmsg *ifm;
2886         struct nlmsghdr  *nlh;
2887         struct ifa_cacheinfo ci;
2888         unsigned char    *b = skb->tail;
2889
2890         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2891         ifm = NLMSG_DATA(nlh);
2892         ifm->ifa_family = AF_INET6;     
2893         ifm->ifa_prefixlen = 128;
2894         ifm->ifa_flags = IFA_F_PERMANENT;
2895         ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2896         if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2897                 ifm->ifa_scope = RT_SCOPE_SITE;
2898         ifm->ifa_index = ifmca->idev->dev->ifindex;
2899         RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2900         ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2901                     * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2902                     * 100 / HZ);
2903         ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2904                     * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2905                     * 100 / HZ);
2906         ci.ifa_prefered = INFINITY_LIFE_TIME;
2907         ci.ifa_valid = INFINITY_LIFE_TIME;
2908         RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2909         nlh->nlmsg_len = skb->tail - b;
2910         return skb->len;
2911
2912 nlmsg_failure:
2913 rtattr_failure:
2914         skb_trim(skb, b - skb->data);
2915         return -1;
2916 }
2917
2918 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2919                                 u32 pid, u32 seq, int event, unsigned int flags)
2920 {
2921         struct ifaddrmsg *ifm;
2922         struct nlmsghdr  *nlh;
2923         struct ifa_cacheinfo ci;
2924         unsigned char    *b = skb->tail;
2925
2926         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2927         ifm = NLMSG_DATA(nlh);
2928         ifm->ifa_family = AF_INET6;     
2929         ifm->ifa_prefixlen = 128;
2930         ifm->ifa_flags = IFA_F_PERMANENT;
2931         ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2932         if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2933                 ifm->ifa_scope = RT_SCOPE_SITE;
2934         ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2935         RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2936         ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2937                     * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2938                     * 100 / HZ);
2939         ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2940                     * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2941                     * 100 / HZ);
2942         ci.ifa_prefered = INFINITY_LIFE_TIME;
2943         ci.ifa_valid = INFINITY_LIFE_TIME;
2944         RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2945         nlh->nlmsg_len = skb->tail - b;
2946         return skb->len;
2947
2948 nlmsg_failure:
2949 rtattr_failure:
2950         skb_trim(skb, b - skb->data);
2951         return -1;
2952 }
2953
2954 enum addr_type_t
2955 {
2956         UNICAST_ADDR,
2957         MULTICAST_ADDR,
2958         ANYCAST_ADDR,
2959 };
2960
2961 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2962                            enum addr_type_t type)
2963 {
2964         int idx, ip_idx;
2965         int s_idx, s_ip_idx;
2966         int err = 1;
2967         struct net_device *dev;
2968         struct inet6_dev *idev = NULL;
2969         struct inet6_ifaddr *ifa;
2970         struct ifmcaddr6 *ifmca;
2971         struct ifacaddr6 *ifaca;
2972
2973         s_idx = cb->args[0];
2974         s_ip_idx = ip_idx = cb->args[1];
2975         read_lock(&dev_base_lock);
2976         
2977         for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2978                 if (idx < s_idx)
2979                         continue;
2980                 if (idx > s_idx)
2981                         s_ip_idx = 0;
2982                 ip_idx = 0;
2983                 if ((idev = in6_dev_get(dev)) == NULL)
2984                         continue;
2985                 read_lock_bh(&idev->lock);
2986                 switch (type) {
2987                 case UNICAST_ADDR:
2988                         /* unicast address incl. temp addr */
2989                         for (ifa = idev->addr_list; ifa;
2990                              ifa = ifa->if_next, ip_idx++) {
2991                                 if (ip_idx < s_ip_idx)
2992                                         continue;
2993                                 if ((err = inet6_fill_ifaddr(skb, ifa, 
2994                                     NETLINK_CB(cb->skb).pid, 
2995                                     cb->nlh->nlmsg_seq, RTM_NEWADDR,
2996                                     NLM_F_MULTI)) <= 0)
2997                                         goto done;
2998                         }
2999                         break;
3000                 case MULTICAST_ADDR:
3001                         /* multicast address */
3002                         for (ifmca = idev->mc_list; ifmca; 
3003                              ifmca = ifmca->next, ip_idx++) {
3004                                 if (ip_idx < s_ip_idx)
3005                                         continue;
3006                                 if ((err = inet6_fill_ifmcaddr(skb, ifmca, 
3007                                     NETLINK_CB(cb->skb).pid, 
3008                                     cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3009                                     NLM_F_MULTI)) <= 0)
3010                                         goto done;
3011                         }
3012                         break;
3013                 case ANYCAST_ADDR:
3014                         /* anycast address */
3015                         for (ifaca = idev->ac_list; ifaca;
3016                              ifaca = ifaca->aca_next, ip_idx++) {
3017                                 if (ip_idx < s_ip_idx)
3018                                         continue;
3019                                 if ((err = inet6_fill_ifacaddr(skb, ifaca, 
3020                                     NETLINK_CB(cb->skb).pid, 
3021                                     cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3022                                     NLM_F_MULTI)) <= 0) 
3023                                         goto done;
3024                         }
3025                         break;
3026                 default:
3027                         break;
3028                 }
3029                 read_unlock_bh(&idev->lock);
3030                 in6_dev_put(idev);
3031         }
3032 done:
3033         if (err <= 0) {
3034                 read_unlock_bh(&idev->lock);
3035                 in6_dev_put(idev);
3036         }
3037         read_unlock(&dev_base_lock);
3038         cb->args[0] = idx;
3039         cb->args[1] = ip_idx;
3040         return skb->len;
3041 }
3042
3043 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3044 {
3045         enum addr_type_t type = UNICAST_ADDR;
3046         return inet6_dump_addr(skb, cb, type);
3047 }
3048
3049 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3050 {
3051         enum addr_type_t type = MULTICAST_ADDR;
3052         return inet6_dump_addr(skb, cb, type);
3053 }
3054
3055
3056 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3057 {
3058         enum addr_type_t type = ANYCAST_ADDR;
3059         return inet6_dump_addr(skb, cb, type);
3060 }
3061
3062 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3063 {
3064         struct sk_buff *skb;
3065         int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3066
3067         skb = alloc_skb(size, GFP_ATOMIC);
3068         if (!skb) {
3069                 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3070                 return;
3071         }
3072         if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3073                 kfree_skb(skb);
3074                 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3075                 return;
3076         }
3077         NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3078         netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3079 }
3080
3081 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3082                                 __s32 *array, int bytes)
3083 {
3084         memset(array, 0, bytes);
3085         array[DEVCONF_FORWARDING] = cnf->forwarding;
3086         array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3087         array[DEVCONF_MTU6] = cnf->mtu6;
3088         array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3089         array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3090         array[DEVCONF_AUTOCONF] = cnf->autoconf;
3091         array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3092         array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3093         array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3094         array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3095         array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3096 #ifdef CONFIG_IPV6_PRIVACY
3097         array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3098         array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3099         array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3100         array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3101         array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3102 #endif
3103         array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3104 }
3105
3106 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 
3107                              u32 pid, u32 seq, int event, unsigned int flags)
3108 {
3109         struct net_device       *dev = idev->dev;
3110         __s32                   *array = NULL;
3111         struct ifinfomsg        *r;
3112         struct nlmsghdr         *nlh;
3113         unsigned char           *b = skb->tail;
3114         struct rtattr           *subattr;
3115         __u32                   mtu = dev->mtu;
3116         struct ifla_cacheinfo   ci;
3117
3118         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3119         r = NLMSG_DATA(nlh);
3120         r->ifi_family = AF_INET6;
3121         r->__ifi_pad = 0;
3122         r->ifi_type = dev->type;
3123         r->ifi_index = dev->ifindex;
3124         r->ifi_flags = dev_get_flags(dev);
3125         r->ifi_change = 0;
3126
3127         RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3128
3129         if (dev->addr_len)
3130                 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3131
3132         RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3133         if (dev->ifindex != dev->iflink)
3134                 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3135                         
3136         subattr = (struct rtattr*)skb->tail;
3137
3138         RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3139
3140         /* return the device flags */
3141         RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3142
3143         /* return interface cacheinfo */
3144         ci.max_reasm_len = IPV6_MAXPLEN;
3145         ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3146                     + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3147         ci.reachable_time = idev->nd_parms->reachable_time;
3148         ci.retrans_time = idev->nd_parms->retrans_time;
3149         RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3150         
3151         /* return the device sysctl params */
3152         if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3153                 goto rtattr_failure;
3154         ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3155         RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3156
3157         /* XXX - Statistics/MC not implemented */
3158         subattr->rta_len = skb->tail - (u8*)subattr;
3159
3160         nlh->nlmsg_len = skb->tail - b;
3161         kfree(array);
3162         return skb->len;
3163
3164 nlmsg_failure:
3165 rtattr_failure:
3166         kfree(array);
3167         skb_trim(skb, b - skb->data);
3168         return -1;
3169 }
3170
3171 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3172 {
3173         int idx, err;
3174         int s_idx = cb->args[0];
3175         struct net_device *dev;
3176         struct inet6_dev *idev;
3177
3178         read_lock(&dev_base_lock);
3179         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3180                 if (idx < s_idx)
3181                         continue;
3182                 if ((idev = in6_dev_get(dev)) == NULL)
3183                         continue;
3184                 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid, 
3185                                 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3186                 in6_dev_put(idev);
3187                 if (err <= 0)
3188                         break;
3189         }
3190         read_unlock(&dev_base_lock);
3191         cb->args[0] = idx;
3192
3193         return skb->len;
3194 }
3195
3196 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3197 {
3198         struct sk_buff *skb;
3199         /* 128 bytes ?? */
3200         int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3201         
3202         skb = alloc_skb(size, GFP_ATOMIC);
3203         if (!skb) {
3204                 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3205                 return;
3206         }
3207         if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3208                 kfree_skb(skb);
3209                 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3210                 return;
3211         }
3212         NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3213         netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3214 }
3215
3216 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3217                         struct prefix_info *pinfo, u32 pid, u32 seq, 
3218                         int event, unsigned int flags)
3219 {
3220         struct prefixmsg        *pmsg;
3221         struct nlmsghdr         *nlh;
3222         unsigned char           *b = skb->tail;
3223         struct prefix_cacheinfo ci;
3224
3225         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3226         pmsg = NLMSG_DATA(nlh);
3227         pmsg->prefix_family = AF_INET6;
3228         pmsg->prefix_pad1 = 0;
3229         pmsg->prefix_pad2 = 0;
3230         pmsg->prefix_ifindex = idev->dev->ifindex;
3231         pmsg->prefix_len = pinfo->prefix_len;
3232         pmsg->prefix_type = pinfo->type;
3233         pmsg->prefix_pad3 = 0;
3234         
3235         pmsg->prefix_flags = 0;
3236         if (pinfo->onlink)
3237                 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3238         if (pinfo->autoconf)
3239                 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3240
3241         RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3242
3243         ci.preferred_time = ntohl(pinfo->prefered);
3244         ci.valid_time = ntohl(pinfo->valid);
3245         RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3246
3247         nlh->nlmsg_len = skb->tail - b;
3248         return skb->len;
3249
3250 nlmsg_failure:
3251 rtattr_failure:
3252         skb_trim(skb, b - skb->data);
3253         return -1;
3254 }
3255
3256 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 
3257                          struct prefix_info *pinfo)
3258 {
3259         struct sk_buff *skb;
3260         int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3261
3262         skb = alloc_skb(size, GFP_ATOMIC);
3263         if (!skb) {
3264                 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3265                 return;
3266         }
3267         if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3268                 kfree_skb(skb);
3269                 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3270                 return;
3271         }
3272         NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3273         netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3274 }
3275
3276 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3277         [RTM_GETLINK - RTM_BASE] = { .dumpit    = inet6_dump_ifinfo, },
3278         [RTM_NEWADDR - RTM_BASE] = { .doit      = inet6_rtm_newaddr, },
3279         [RTM_DELADDR - RTM_BASE] = { .doit      = inet6_rtm_deladdr, },
3280         [RTM_GETADDR - RTM_BASE] = { .dumpit    = inet6_dump_ifaddr, },
3281         [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3282         [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3283         [RTM_NEWROUTE - RTM_BASE] = { .doit     = inet6_rtm_newroute, },
3284         [RTM_DELROUTE - RTM_BASE] = { .doit     = inet6_rtm_delroute, },
3285         [RTM_GETROUTE - RTM_BASE] = { .doit     = inet6_rtm_getroute,
3286                                       .dumpit   = inet6_dump_fib, },
3287 };
3288
3289 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3290 {
3291         inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3292
3293         switch (event) {
3294         case RTM_NEWADDR:
3295                 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3296                 if (ifp->idev->cnf.forwarding)
3297                         addrconf_join_anycast(ifp);
3298                 break;
3299         case RTM_DELADDR:
3300                 if (ifp->idev->cnf.forwarding)
3301                         addrconf_leave_anycast(ifp);
3302                 addrconf_leave_solict(ifp->idev, &ifp->addr);
3303                 dst_hold(&ifp->rt->u.dst);
3304                 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3305                         dst_free(&ifp->rt->u.dst);
3306                 break;
3307         }
3308 }
3309
3310 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3311 {
3312         read_lock_bh(&addrconf_lock);
3313         if (likely(ifp->idev->dead == 0))
3314                 __ipv6_ifa_notify(event, ifp);
3315         read_unlock_bh(&addrconf_lock);
3316 }
3317
3318 #ifdef CONFIG_SYSCTL
3319
3320 static
3321 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3322                            void __user *buffer, size_t *lenp, loff_t *ppos)
3323 {
3324         int *valp = ctl->data;
3325         int val = *valp;
3326         int ret;
3327
3328         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3329
3330         if (write && valp != &ipv6_devconf_dflt.forwarding) {
3331                 if (valp != &ipv6_devconf.forwarding) {
3332                         if ((!*valp) ^ (!val)) {
3333                                 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3334                                 if (idev == NULL)
3335                                         return ret;
3336                                 dev_forward_change(idev);
3337                         }
3338                 } else {
3339                         ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3340                         addrconf_forward_change();
3341                 }
3342                 if (*valp)
3343                         rt6_purge_dflt_routers();
3344         }
3345
3346         return ret;
3347 }
3348
3349 static int addrconf_sysctl_forward_strategy(ctl_table *table, 
3350                                             int __user *name, int nlen,
3351                                             void __user *oldval,
3352                                             size_t __user *oldlenp,
3353                                             void __user *newval, size_t newlen,
3354                                             void **context)
3355 {
3356         int *valp = table->data;
3357         int new;
3358
3359         if (!newval || !newlen)
3360                 return 0;
3361         if (newlen != sizeof(int))
3362                 return -EINVAL;
3363         if (get_user(new, (int __user *)newval))
3364                 return -EFAULT;
3365         if (new == *valp)
3366                 return 0;
3367         if (oldval && oldlenp) {
3368                 size_t len;
3369                 if (get_user(len, oldlenp))
3370                         return -EFAULT;
3371                 if (len) {
3372                         if (len > table->maxlen)
3373                                 len = table->maxlen;
3374                         if (copy_to_user(oldval, valp, len))
3375                                 return -EFAULT;
3376                         if (put_user(len, oldlenp))
3377                                 return -EFAULT;
3378                 }
3379         }
3380
3381         if (valp != &ipv6_devconf_dflt.forwarding) {
3382                 if (valp != &ipv6_devconf.forwarding) {
3383                         struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3384                         int changed;
3385                         if (unlikely(idev == NULL))
3386                                 return -ENODEV;
3387                         changed = (!*valp) ^ (!new);
3388                         *valp = new;
3389                         if (changed)
3390                                 dev_forward_change(idev);
3391                 } else {
3392                         *valp = new;
3393                         addrconf_forward_change();
3394                 }
3395
3396                 if (*valp)
3397                         rt6_purge_dflt_routers();
3398         } else
3399                 *valp = new;
3400
3401         return 1;
3402 }
3403
3404 static struct addrconf_sysctl_table
3405 {
3406         struct ctl_table_header *sysctl_header;
3407         ctl_table addrconf_vars[__NET_IPV6_MAX];
3408         ctl_table addrconf_dev[2];
3409         ctl_table addrconf_conf_dir[2];
3410         ctl_table addrconf_proto_dir[2];
3411         ctl_table addrconf_root_dir[2];
3412 } addrconf_sysctl = {
3413         .sysctl_header = NULL,
3414         .addrconf_vars = {
3415                 {
3416                         .ctl_name       =       NET_IPV6_FORWARDING,
3417                         .procname       =       "forwarding",
3418                         .data           =       &ipv6_devconf.forwarding,
3419                         .maxlen         =       sizeof(int),
3420                         .mode           =       0644,
3421                         .proc_handler   =       &addrconf_sysctl_forward,
3422                         .strategy       =       &addrconf_sysctl_forward_strategy,
3423                 },
3424                 {
3425                         .ctl_name       =       NET_IPV6_HOP_LIMIT,
3426                         .procname       =       "hop_limit",
3427                         .data           =       &ipv6_devconf.hop_limit,
3428                         .maxlen         =       sizeof(int),
3429                         .mode           =       0644,
3430                         .proc_handler   =       proc_dointvec,
3431                 },
3432                 {
3433                         .ctl_name       =       NET_IPV6_MTU,
3434                         .procname       =       "mtu",
3435                         .data           =       &ipv6_devconf.mtu6,
3436                         .maxlen         =       sizeof(int),
3437                         .mode           =       0644,
3438                         .proc_handler   =       &proc_dointvec,
3439                 },
3440                 {
3441                         .ctl_name       =       NET_IPV6_ACCEPT_RA,
3442                         .procname       =       "accept_ra",
3443                         .data           =       &ipv6_devconf.accept_ra,
3444                         .maxlen         =       sizeof(int),
3445                         .mode           =       0644,
3446                         .proc_handler   =       &proc_dointvec,
3447                 },
3448                 {
3449                         .ctl_name       =       NET_IPV6_ACCEPT_REDIRECTS,
3450                         .procname       =       "accept_redirects",
3451                         .data           =       &ipv6_devconf.accept_redirects,
3452                         .maxlen         =       sizeof(int),
3453                         .mode           =       0644,
3454                         .proc_handler   =       &proc_dointvec,
3455                 },
3456                 {
3457                         .ctl_name       =       NET_IPV6_AUTOCONF,
3458                         .procname       =       "autoconf",
3459                         .data           =       &ipv6_devconf.autoconf,
3460                         .maxlen         =       sizeof(int),
3461                         .mode           =       0644,
3462                         .proc_handler   =       &proc_dointvec,
3463                 },
3464                 {
3465                         .ctl_name       =       NET_IPV6_DAD_TRANSMITS,
3466                         .procname       =       "dad_transmits",
3467                         .data           =       &ipv6_devconf.dad_transmits,
3468                         .maxlen         =       sizeof(int),
3469                         .mode           =       0644,
3470                         .proc_handler   =       &proc_dointvec,
3471                 },
3472                 {
3473                         .ctl_name       =       NET_IPV6_RTR_SOLICITS,
3474                         .procname       =       "router_solicitations",
3475                         .data           =       &ipv6_devconf.rtr_solicits,
3476                         .maxlen         =       sizeof(int),
3477                         .mode           =       0644,
3478                         .proc_handler   =       &proc_dointvec,
3479                 },
3480                 {
3481                         .ctl_name       =       NET_IPV6_RTR_SOLICIT_INTERVAL,
3482                         .procname       =       "router_solicitation_interval",
3483                         .data           =       &ipv6_devconf.rtr_solicit_interval,
3484                         .maxlen         =       sizeof(int),
3485                         .mode           =       0644,
3486                         .proc_handler   =       &proc_dointvec_jiffies,
3487                         .strategy       =       &sysctl_jiffies,
3488                 },
3489                 {
3490                         .ctl_name       =       NET_IPV6_RTR_SOLICIT_DELAY,
3491                         .procname       =       "router_solicitation_delay",
3492                         .data           =       &ipv6_devconf.rtr_solicit_delay,
3493                         .maxlen         =       sizeof(int),
3494                         .mode           =       0644,
3495                         .proc_handler   =       &proc_dointvec_jiffies,
3496                         .strategy       =       &sysctl_jiffies,
3497                 },
3498                 {
3499                         .ctl_name       =       NET_IPV6_FORCE_MLD_VERSION,
3500                         .procname       =       "force_mld_version",
3501                         .data           =       &ipv6_devconf.force_mld_version,
3502                         .maxlen         =       sizeof(int),
3503                         .mode           =       0644,
3504                         .proc_handler   =       &proc_dointvec,
3505                 },
3506 #ifdef CONFIG_IPV6_PRIVACY
3507                 {
3508                         .ctl_name       =       NET_IPV6_USE_TEMPADDR,
3509                         .procname       =       "use_tempaddr",
3510                         .data           =       &ipv6_devconf.use_tempaddr,
3511                         .maxlen         =       sizeof(int),
3512                         .mode           =       0644,
3513                         .proc_handler   =       &proc_dointvec,
3514                 },
3515                 {
3516                         .ctl_name       =       NET_IPV6_TEMP_VALID_LFT,
3517                         .procname       =       "temp_valid_lft",
3518                         .data           =       &ipv6_devconf.temp_valid_lft,
3519                         .maxlen         =       sizeof(int),
3520                         .mode           =       0644,
3521                         .proc_handler   =       &proc_dointvec,
3522                 },
3523                 {
3524                         .ctl_name       =       NET_IPV6_TEMP_PREFERED_LFT,
3525                         .procname       =       "temp_prefered_lft",
3526                         .data           =       &ipv6_devconf.temp_prefered_lft,
3527                         .maxlen         =       sizeof(int),
3528                         .mode           =       0644,
3529                         .proc_handler   =       &proc_dointvec,
3530                 },
3531                 {
3532                         .ctl_name       =       NET_IPV6_REGEN_MAX_RETRY,
3533                         .procname       =       "regen_max_retry",
3534                         .data           =       &ipv6_devconf.regen_max_retry,
3535                         .maxlen         =       sizeof(int),
3536                         .mode           =       0644,
3537                         .proc_handler   =       &proc_dointvec,
3538                 },
3539                 {
3540                         .ctl_name       =       NET_IPV6_MAX_DESYNC_FACTOR,
3541                         .procname       =       "max_desync_factor",
3542                         .data           =       &ipv6_devconf.max_desync_factor,
3543                         .maxlen         =       sizeof(int),
3544                         .mode           =       0644,
3545                         .proc_handler   =       &proc_dointvec,
3546                 },
3547 #endif
3548                 {
3549                         .ctl_name       =       NET_IPV6_MAX_ADDRESSES,
3550                         .procname       =       "max_addresses",
3551                         .data           =       &ipv6_devconf.max_addresses,
3552                         .maxlen         =       sizeof(int),
3553                         .mode           =       0644,
3554                         .proc_handler   =       &proc_dointvec,
3555                 },
3556                 {
3557                         .ctl_name       =       0,      /* sentinel */
3558                 }
3559         },
3560         .addrconf_dev = {
3561                 {
3562                         .ctl_name       =       NET_PROTO_CONF_ALL,
3563                         .procname       =       "all",
3564                         .mode           =       0555,
3565                         .child          =       addrconf_sysctl.addrconf_vars,
3566                 },
3567                 {
3568                         .ctl_name       =       0,      /* sentinel */
3569                 }
3570         },
3571         .addrconf_conf_dir = {
3572                 {
3573                         .ctl_name       =       NET_IPV6_CONF,
3574                         .procname       =       "conf",
3575                         .mode           =       0555,
3576                         .child          =       addrconf_sysctl.addrconf_dev,
3577                 },
3578                 {
3579                         .ctl_name       =       0,      /* sentinel */
3580                 }
3581         },
3582         .addrconf_proto_dir = {
3583                 {
3584                         .ctl_name       =       NET_IPV6,
3585                         .procname       =       "ipv6",
3586                         .mode           =       0555,
3587                         .child          =       addrconf_sysctl.addrconf_conf_dir,
3588                 },
3589                 {
3590                         .ctl_name       =       0,      /* sentinel */
3591                 }
3592         },
3593         .addrconf_root_dir = {
3594                 {
3595                         .ctl_name       =       CTL_NET,
3596                         .procname       =       "net",
3597                         .mode           =       0555,
3598                         .child          =       addrconf_sysctl.addrconf_proto_dir,
3599                 },
3600                 {
3601                         .ctl_name       =       0,      /* sentinel */
3602                 }
3603         },
3604 };
3605
3606 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3607 {
3608         int i;
3609         struct net_device *dev = idev ? idev->dev : NULL;
3610         struct addrconf_sysctl_table *t;
3611         char *dev_name = NULL;
3612
3613         t = kmalloc(sizeof(*t), GFP_KERNEL);
3614         if (t == NULL)
3615                 return;
3616         memcpy(t, &addrconf_sysctl, sizeof(*t));
3617         for (i=0; t->addrconf_vars[i].data; i++) {
3618                 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3619                 t->addrconf_vars[i].de = NULL;
3620                 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3621         }
3622         if (dev) {
3623                 dev_name = dev->name; 
3624                 t->addrconf_dev[0].ctl_name = dev->ifindex;
3625         } else {
3626                 dev_name = "default";
3627                 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3628         }
3629
3630         /* 
3631          * Make a copy of dev_name, because '.procname' is regarded as const 
3632          * by sysctl and we wouldn't want anyone to change it under our feet
3633          * (see SIOCSIFNAME).
3634          */     
3635         dev_name = kstrdup(dev_name, GFP_KERNEL);
3636         if (!dev_name)
3637             goto free;
3638
3639         t->addrconf_dev[0].procname = dev_name;
3640
3641         t->addrconf_dev[0].child = t->addrconf_vars;
3642         t->addrconf_dev[0].de = NULL;
3643         t->addrconf_conf_dir[0].child = t->addrconf_dev;
3644         t->addrconf_conf_dir[0].de = NULL;
3645         t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3646         t->addrconf_proto_dir[0].de = NULL;
3647         t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3648         t->addrconf_root_dir[0].de = NULL;
3649
3650         t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3651         if (t->sysctl_header == NULL)
3652                 goto free_procname;
3653         else
3654                 p->sysctl = t;
3655         return;
3656
3657         /* error path */
3658  free_procname:
3659         kfree(dev_name);
3660  free:
3661         kfree(t);
3662
3663         return;
3664 }
3665
3666 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3667 {
3668         if (p->sysctl) {
3669                 struct addrconf_sysctl_table *t = p->sysctl;
3670                 p->sysctl = NULL;
3671                 unregister_sysctl_table(t->sysctl_header);
3672                 kfree(t->addrconf_dev[0].procname);
3673                 kfree(t);
3674         }
3675 }
3676
3677
3678 #endif
3679
3680 /*
3681  *      Device notifier
3682  */
3683
3684 int register_inet6addr_notifier(struct notifier_block *nb)
3685 {
3686         return notifier_chain_register(&inet6addr_chain, nb);
3687 }
3688
3689 int unregister_inet6addr_notifier(struct notifier_block *nb)
3690 {
3691         return notifier_chain_unregister(&inet6addr_chain,nb);
3692 }
3693
3694 /*
3695  *      Init / cleanup code
3696  */
3697
3698 int __init addrconf_init(void)
3699 {
3700         int err = 0;
3701
3702         /* The addrconf netdev notifier requires that loopback_dev
3703          * has it's ipv6 private information allocated and setup
3704          * before it can bring up and give link-local addresses
3705          * to other devices which are up.
3706          *
3707          * Unfortunately, loopback_dev is not necessarily the first
3708          * entry in the global dev_base list of net devices.  In fact,
3709          * it is likely to be the very last entry on that list.
3710          * So this causes the notifier registry below to try and
3711          * give link-local addresses to all devices besides loopback_dev
3712          * first, then loopback_dev, which cases all the non-loopback_dev
3713          * devices to fail to get a link-local address.
3714          *
3715          * So, as a temporary fix, allocate the ipv6 structure for
3716          * loopback_dev first by hand.
3717          * Longer term, all of the dependencies ipv6 has upon the loopback
3718          * device and it being up should be removed.
3719          */
3720         rtnl_lock();
3721         if (!ipv6_add_dev(&loopback_dev))
3722                 err = -ENOMEM;
3723         rtnl_unlock();
3724         if (err)
3725                 return err;
3726
3727         ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3728
3729         register_netdevice_notifier(&ipv6_dev_notf);
3730
3731         addrconf_verify(0);
3732         rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3733 #ifdef CONFIG_SYSCTL
3734         addrconf_sysctl.sysctl_header =
3735                 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3736         addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3737 #endif
3738
3739         return 0;
3740 }
3741
3742 void __exit addrconf_cleanup(void)
3743 {
3744         struct net_device *dev;
3745         struct inet6_dev *idev;
3746         struct inet6_ifaddr *ifa;
3747         int i;
3748
3749         unregister_netdevice_notifier(&ipv6_dev_notf);
3750
3751         rtnetlink_links[PF_INET6] = NULL;
3752 #ifdef CONFIG_SYSCTL
3753         addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3754         addrconf_sysctl_unregister(&ipv6_devconf);
3755 #endif
3756
3757         rtnl_lock();
3758
3759         /*
3760          *      clean dev list.
3761          */
3762
3763         for (dev=dev_base; dev; dev=dev->next) {
3764                 if ((idev = __in6_dev_get(dev)) == NULL)
3765                         continue;
3766                 addrconf_ifdown(dev, 1);
3767         }
3768         addrconf_ifdown(&loopback_dev, 2);
3769
3770         /*
3771          *      Check hash table.
3772          */
3773
3774         write_lock_bh(&addrconf_hash_lock);
3775         for (i=0; i < IN6_ADDR_HSIZE; i++) {
3776                 for (ifa=inet6_addr_lst[i]; ifa; ) {
3777                         struct inet6_ifaddr *bifa;
3778
3779                         bifa = ifa;
3780                         ifa = ifa->lst_next;
3781                         printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3782                         /* Do not free it; something is wrong.
3783                            Now we can investigate it with debugger.
3784                          */
3785                 }
3786         }
3787         write_unlock_bh(&addrconf_hash_lock);
3788
3789         del_timer(&addr_chk_timer);
3790
3791         rtnl_unlock();
3792
3793 #ifdef CONFIG_PROC_FS
3794         proc_net_remove("if_inet6");
3795 #endif
3796 }