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