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