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