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