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