Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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