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