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