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