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