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