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