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