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