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