port more changes to make PCI work
[linux-2.4.git] / net / ipv6 / addrconf.c
1 /*
2  *      IPv6 Address [auto]configuration
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <pedro_m@yahoo.com>     
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  *      Yuji SEKIYA @USAGI              :       Don't assign a same IPv6
32  *                                              address on a same interface.
33  *      YOSHIFUJI Hideaki @USAGI        :       ARCnet support
34  */
35
36 #include <linux/config.h>
37 #include <linux/errno.h>
38 #include <linux/types.h>
39 #include <linux/socket.h>
40 #include <linux/sockios.h>
41 #include <linux/sched.h>
42 #include <linux/net.h>
43 #include <linux/in6.h>
44 #include <linux/netdevice.h>
45 #include <linux/if_arp.h>
46 #include <linux/if_arcnet.h>
47 #include <linux/route.h>
48 #include <linux/inetdevice.h>
49 #include <linux/init.h>
50 #ifdef CONFIG_SYSCTL
51 #include <linux/sysctl.h>
52 #endif
53 #include <linux/delay.h>
54 #include <linux/notifier.h>
55
56 #include <linux/proc_fs.h>
57 #include <net/sock.h>
58 #include <net/snmp.h>
59
60 #include <net/ipv6.h>
61 #include <net/protocol.h>
62 #include <net/ndisc.h>
63 #include <net/ip6_route.h>
64 #include <net/addrconf.h>
65 #include <net/ip.h>
66 #include <linux/if_tunnel.h>
67 #include <linux/rtnetlink.h>
68
69 #include <asm/uaccess.h>
70
71 #define IPV6_MAX_ADDRESSES 16
72
73 /* Set to 3 to get tracing... */
74 #define ACONF_DEBUG 2
75
76 #if ACONF_DEBUG >= 3
77 #define ADBG(x) printk x
78 #else
79 #define ADBG(x)
80 #endif
81
82 #ifdef CONFIG_SYSCTL
83 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
84 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
85 #endif
86
87 int inet6_dev_count;
88 int inet6_ifa_count;
89
90 /*
91  *      Configured unicast address hash table
92  */
93 static struct inet6_ifaddr              *inet6_addr_lst[IN6_ADDR_HSIZE];
94 static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;
95
96 /* Protects inet6 devices */
97 rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;
98
99 void addrconf_verify(unsigned long);
100
101 static struct timer_list addr_chk_timer = { function: addrconf_verify };
102 static spinlock_t addrconf_verify_lock = SPIN_LOCK_UNLOCKED;
103
104 static int addrconf_ifdown(struct net_device *dev, int how);
105
106 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags);
107 static void addrconf_dad_timer(unsigned long data);
108 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
109 static void addrconf_rs_timer(unsigned long data);
110 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
111
112 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
113
114 static struct notifier_block *inet6addr_chain;
115
116 struct ipv6_devconf ipv6_devconf =
117 {
118         0,                              /* forwarding           */
119         IPV6_DEFAULT_HOPLIMIT,          /* hop limit            */
120         IPV6_MIN_MTU,                   /* mtu                  */
121         1,                              /* accept RAs           */
122         1,                              /* accept redirects     */
123         1,                              /* autoconfiguration    */
124         1,                              /* dad transmits        */
125         MAX_RTR_SOLICITATIONS,          /* router solicits      */
126         RTR_SOLICITATION_INTERVAL,      /* rtr solicit interval */
127         MAX_RTR_SOLICITATION_DELAY,     /* rtr solicit delay    */
128 };
129
130 static struct ipv6_devconf ipv6_devconf_dflt =
131 {
132         0,                              /* forwarding           */
133         IPV6_DEFAULT_HOPLIMIT,          /* hop limit            */
134         IPV6_MIN_MTU,                   /* mtu                  */
135         1,                              /* accept RAs           */
136         1,                              /* accept redirects     */
137         1,                              /* autoconfiguration    */
138         1,                              /* dad transmits        */
139         MAX_RTR_SOLICITATIONS,          /* router solicits      */
140         RTR_SOLICITATION_INTERVAL,      /* rtr solicit interval */
141         MAX_RTR_SOLICITATION_DELAY,     /* rtr solicit delay    */
142 };
143
144 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
145 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
146 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
147
148 int ipv6_addr_type(const struct in6_addr *addr)
149 {
150         int type;
151         u32 st;
152
153         st = addr->s6_addr32[0];
154
155         if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
156                 type = IPV6_ADDR_MULTICAST;
157
158                 switch((st & htonl(0x00FF0000))) {
159                         case __constant_htonl(0x00010000):
160                                 type |= IPV6_ADDR_LOOPBACK;
161                                 break;
162
163                         case __constant_htonl(0x00020000):
164                                 type |= IPV6_ADDR_LINKLOCAL;
165                                 break;
166
167                         case __constant_htonl(0x00050000):
168                                 type |= IPV6_ADDR_SITELOCAL;
169                                 break;
170                 };
171                 return type;
172         }
173         /* check for reserved anycast addresses */
174         
175         if ((st & htonl(0xE0000000)) &&
176             ((addr->s6_addr32[2] == htonl(0xFDFFFFFF) &&
177             (addr->s6_addr32[3] | htonl(0x7F)) == (u32)~0) ||
178             (addr->s6_addr32[2] == 0 && addr->s6_addr32[3] == 0)))
179                 type = IPV6_ADDR_ANYCAST;
180         else
181                 type = IPV6_ADDR_UNICAST;
182
183         /* Consider all addresses with the first three bits different of
184            000 and 111 as finished.
185          */
186         if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
187             (st & htonl(0xE0000000)) != htonl(0xE0000000))
188                 return type;
189         
190         if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
191                 return (IPV6_ADDR_LINKLOCAL | type);
192
193         if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
194                 return (IPV6_ADDR_SITELOCAL | type);
195
196         if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
197                 if (addr->s6_addr32[2] == 0) {
198                         if (addr->in6_u.u6_addr32[3] == 0)
199                                 return IPV6_ADDR_ANY;
200
201                         if (addr->s6_addr32[3] == htonl(0x00000001))
202                                 return (IPV6_ADDR_LOOPBACK | type);
203
204                         return (IPV6_ADDR_COMPATv4 | type);
205                 }
206
207                 if (addr->s6_addr32[2] == htonl(0x0000ffff))
208                         return IPV6_ADDR_MAPPED;
209         }
210
211         st &= htonl(0xFF000000);
212         if (st == 0)
213                 return IPV6_ADDR_RESERVED;
214         st &= htonl(0xFE000000);
215         if (st == htonl(0x02000000))
216                 return IPV6_ADDR_RESERVED;      /* for NSAP */
217         if (st == htonl(0x04000000))
218                 return IPV6_ADDR_RESERVED;      /* for IPX */
219         return type;
220 }
221
222 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
223 {
224         if (del_timer(&ifp->timer))
225                 __in6_ifa_put(ifp);
226 }
227
228 enum addrconf_timer_t
229 {
230         AC_NONE,
231         AC_DAD,
232         AC_RS,
233 };
234
235 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
236                                enum addrconf_timer_t what,
237                                unsigned long when)
238 {
239         if (!del_timer(&ifp->timer))
240                 in6_ifa_hold(ifp);
241
242         switch (what) {
243         case AC_DAD:
244                 ifp->timer.function = addrconf_dad_timer;
245                 break;
246         case AC_RS:
247                 ifp->timer.function = addrconf_rs_timer;
248                 break;
249         default:;
250         }
251         ifp->timer.expires = jiffies + when;
252         add_timer(&ifp->timer);
253 }
254
255 /* Nobody refers to this device, we may destroy it. */
256
257 void in6_dev_finish_destroy(struct inet6_dev *idev)
258 {
259         struct net_device *dev = idev->dev;
260         BUG_TRAP(idev->addr_list==NULL);
261         BUG_TRAP(idev->mc_list==NULL);
262 #ifdef NET_REFCNT_DEBUG
263         printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
264 #endif
265         dev_put(dev);
266         if (!idev->dead) {
267                 printk("Freeing alive inet6 device %p\n", idev);
268                 return;
269         }
270         inet6_dev_count--;
271         kfree(idev);
272 }
273
274 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
275 {
276         struct inet6_dev *ndev;
277
278         ASSERT_RTNL();
279
280         if (dev->mtu < IPV6_MIN_MTU)
281                 return NULL;
282
283         ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
284
285         if (ndev) {
286                 memset(ndev, 0, sizeof(struct inet6_dev));
287
288                 ndev->lock = RW_LOCK_UNLOCKED;
289                 ndev->dev = dev;
290                 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
291                 ndev->cnf.mtu6 = dev->mtu;
292                 ndev->cnf.sysctl = NULL;
293                 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
294                 if (ndev->nd_parms == NULL) {
295                         kfree(ndev);
296                         return NULL;
297                 }
298                 inet6_dev_count++;
299                 /* We refer to the device */
300                 dev_hold(dev);
301
302                 write_lock_bh(&addrconf_lock);
303                 dev->ip6_ptr = ndev;
304                 /* One reference from device */
305                 in6_dev_hold(ndev);
306                 write_unlock_bh(&addrconf_lock);
307
308                 ipv6_mc_init_dev(ndev);
309
310 #ifdef CONFIG_SYSCTL
311                 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6");
312                 addrconf_sysctl_register(ndev, &ndev->cnf);
313 #endif
314         }
315         return ndev;
316 }
317
318 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
319 {
320         struct inet6_dev *idev;
321
322         ASSERT_RTNL();
323
324         if ((idev = __in6_dev_get(dev)) == NULL) {
325                 if ((idev = ipv6_add_dev(dev)) == NULL)
326                         return NULL;
327         }
328         if (dev->flags&IFF_UP)
329                 ipv6_mc_up(idev);
330         return idev;
331 }
332
333 void ipv6_addr_prefix(struct in6_addr *prefix,
334         struct in6_addr *addr, int prefix_len)
335 {
336         unsigned long mask;
337         int ncopy, nbits;
338
339         memset(prefix, 0, sizeof(*prefix));
340
341         if (prefix_len <= 0)
342                 return;
343         if (prefix_len > 128)
344                 prefix_len = 128;
345
346         ncopy = prefix_len / 32;
347         switch (ncopy) {
348         case 4: prefix->s6_addr32[3] = addr->s6_addr32[3];
349         case 3: prefix->s6_addr32[2] = addr->s6_addr32[2];
350         case 2: prefix->s6_addr32[1] = addr->s6_addr32[1];
351         case 1: prefix->s6_addr32[0] = addr->s6_addr32[0];
352         case 0: break;
353         }
354         nbits = prefix_len % 32;
355         if (nbits == 0)
356                 return;
357
358         mask = ~((1 << (32 - nbits)) - 1);
359         mask = htonl(mask);
360
361         prefix->s6_addr32[ncopy] = addr->s6_addr32[ncopy] & mask;
362 }
363
364
365 static void dev_forward_change(struct inet6_dev *idev)
366 {
367         struct net_device *dev;
368         struct inet6_ifaddr *ifa;
369         struct in6_addr addr;
370
371         if (!idev)
372                 return;
373         dev = idev->dev;
374         if (dev && (dev->flags & IFF_MULTICAST)) {
375                 ipv6_addr_all_routers(&addr);
376         
377                 if (idev->cnf.forwarding)
378                         ipv6_dev_mc_inc(dev, &addr);
379                 else
380                         ipv6_dev_mc_dec(dev, &addr);
381         }
382         for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
383                 ipv6_addr_prefix(&addr, &ifa->addr, ifa->prefix_len);
384                 if (ipv6_addr_any(&addr))
385                         continue;
386                 if (idev->cnf.forwarding)
387                         ipv6_dev_ac_inc(idev->dev, &addr);
388                 else
389                         ipv6_dev_ac_dec(idev->dev, &addr);
390         }
391 }
392
393
394 static void addrconf_forward_change(struct inet6_dev *idev)
395 {
396         struct net_device *dev;
397
398         if (idev) {
399                 dev_forward_change(idev);
400                 return;
401         }
402
403         read_lock(&dev_base_lock);
404         for (dev=dev_base; dev; dev=dev->next) {
405                 read_lock(&addrconf_lock);
406                 idev = __in6_dev_get(dev);
407                 if (idev) {
408                         idev->cnf.forwarding = ipv6_devconf.forwarding;
409                         dev_forward_change(idev);
410                 }
411                 read_unlock(&addrconf_lock);
412         }
413         read_unlock(&dev_base_lock);
414 }
415
416
417 /* Nobody refers to this ifaddr, destroy it */
418
419 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
420 {
421         BUG_TRAP(ifp->if_next==NULL);
422         BUG_TRAP(ifp->lst_next==NULL);
423 #ifdef NET_REFCNT_DEBUG
424         printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
425 #endif
426
427         in6_dev_put(ifp->idev);
428
429         if (del_timer(&ifp->timer))
430                 printk("Timer is still running, when freeing ifa=%p\n", ifp);
431
432         if (!ifp->dead) {
433                 printk("Freeing alive inet6 address %p\n", ifp);
434                 return;
435         }
436         inet6_ifa_count--;
437         kfree(ifp);
438 }
439
440 /* On success it returns ifp with increased reference count */
441
442 static struct inet6_ifaddr *
443 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
444               int scope, unsigned flags)
445 {
446         struct inet6_ifaddr *ifa;
447         int hash;
448         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
449
450         spin_lock_bh(&lock);
451
452         /* Ignore adding duplicate addresses on an interface */
453         if (ipv6_chk_same_addr(addr, idev->dev)) {
454                 spin_unlock_bh(&lock);
455                 ADBG(("ipv6_add_addr: already assigned\n"));
456                 return ERR_PTR(-EEXIST);
457         }
458
459         ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
460
461         if (ifa == NULL) {
462                 spin_unlock_bh(&lock);
463                 ADBG(("ipv6_add_addr: malloc failed\n"));
464                 return ERR_PTR(-ENOBUFS);
465         }
466
467         memset(ifa, 0, sizeof(struct inet6_ifaddr));
468         ipv6_addr_copy(&ifa->addr, addr);
469
470         spin_lock_init(&ifa->lock);
471         init_timer(&ifa->timer);
472         ifa->timer.data = (unsigned long) ifa;
473         ifa->scope = scope;
474         ifa->prefix_len = pfxlen;
475         ifa->flags = flags | IFA_F_TENTATIVE;
476
477         read_lock(&addrconf_lock);
478         if (idev->dead) {
479                 read_unlock(&addrconf_lock);
480                 spin_unlock_bh(&lock);
481                 kfree(ifa);
482                 return ERR_PTR(-ENODEV);        /*XXX*/
483         }
484
485         inet6_ifa_count++;
486         ifa->idev = idev;
487         in6_dev_hold(idev);
488         /* For caller */
489         in6_ifa_hold(ifa);
490
491         /* Add to big hash table */
492         hash = ipv6_addr_hash(addr);
493
494         write_lock_bh(&addrconf_hash_lock);
495         ifa->lst_next = inet6_addr_lst[hash];
496         inet6_addr_lst[hash] = ifa;
497         in6_ifa_hold(ifa);
498         write_unlock_bh(&addrconf_hash_lock);
499
500         write_lock_bh(&idev->lock);
501         /* Add to inet6_dev unicast addr list. */
502         ifa->if_next = idev->addr_list;
503         idev->addr_list = ifa;
504         in6_ifa_hold(ifa);
505         write_unlock_bh(&idev->lock);
506         read_unlock(&addrconf_lock);
507         spin_unlock_bh(&lock);
508
509         notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa);
510
511         return ifa;
512 }
513
514 /* This function wants to get referenced ifp and releases it before return */
515
516 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
517 {
518         struct inet6_ifaddr *ifa, **ifap;
519         struct inet6_dev *idev = ifp->idev;
520         int hash;
521
522         hash = ipv6_addr_hash(&ifp->addr);
523
524         ifp->dead = 1;
525
526         write_lock_bh(&addrconf_hash_lock);
527         for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
528              ifap = &ifa->lst_next) {
529                 if (ifa == ifp) {
530                         *ifap = ifa->lst_next;
531                         __in6_ifa_put(ifp);
532                         ifa->lst_next = NULL;
533                         break;
534                 }
535         }
536         write_unlock_bh(&addrconf_hash_lock);
537
538         write_lock_bh(&idev->lock);
539         for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
540              ifap = &ifa->if_next) {
541                 if (ifa == ifp) {
542                         *ifap = ifa->if_next;
543                         __in6_ifa_put(ifp);
544                         ifa->if_next = NULL;
545                         break;
546                 }
547         }
548         write_unlock_bh(&idev->lock);
549
550         ipv6_ifa_notify(RTM_DELADDR, ifp);
551
552         notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
553
554         addrconf_del_timer(ifp);
555
556         in6_ifa_put(ifp);
557 }
558
559 /*
560  *      Choose an apropriate source address
561  *      should do:
562  *      i)      get an address with an apropriate scope
563  *      ii)     see if there is a specific route for the destination and use
564  *              an address of the attached interface 
565  *      iii)    don't use deprecated addresses
566  */
567 int ipv6_dev_get_saddr(struct net_device *dev,
568                    struct in6_addr *daddr, struct in6_addr *saddr, int onlink)
569 {
570         struct inet6_ifaddr *ifp = NULL;
571         struct inet6_ifaddr *match = NULL;
572         struct inet6_dev *idev;
573         int scope;
574         int err;
575
576
577         if (!onlink)
578                 scope = ipv6_addr_scope(daddr);
579         else
580                 scope = IFA_LINK;
581
582         /*
583          *      known dev
584          *      search dev and walk through dev addresses
585          */
586
587         if (dev) {
588                 if (dev->flags & IFF_LOOPBACK)
589                         scope = IFA_HOST;
590
591                 read_lock(&addrconf_lock);
592                 idev = __in6_dev_get(dev);
593                 if (idev) {
594                         read_lock_bh(&idev->lock);
595                         for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
596                                 if (ifp->scope == scope) {
597                                         if (!(ifp->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
598                                                 in6_ifa_hold(ifp);
599                                                 read_unlock_bh(&idev->lock);
600                                                 read_unlock(&addrconf_lock);
601                                                 goto out;
602                                         }
603
604                                         if (!match && !(ifp->flags & IFA_F_TENTATIVE)) {
605                                                 match = ifp;
606                                                 in6_ifa_hold(ifp);
607                                         }
608                                 }
609                         }
610                         read_unlock_bh(&idev->lock);
611                 }
612                 read_unlock(&addrconf_lock);
613         }
614
615         if (scope == IFA_LINK)
616                 goto out;
617
618         /*
619          *      dev == NULL or search failed for specified dev
620          */
621
622         read_lock(&dev_base_lock);
623         read_lock(&addrconf_lock);
624         for (dev = dev_base; dev; dev=dev->next) {
625                 idev = __in6_dev_get(dev);
626                 if (idev) {
627                         read_lock_bh(&idev->lock);
628                         for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
629                                 if (ifp->scope == scope) {
630                                         if (!(ifp->flags&(IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
631                                                 in6_ifa_hold(ifp);
632                                                 read_unlock_bh(&idev->lock);
633                                                 goto out_unlock_base;
634                                         }
635
636                                         if (!match && !(ifp->flags&IFA_F_TENTATIVE)) {
637                                                 match = ifp;
638                                                 in6_ifa_hold(ifp);
639                                         }
640                                 }
641                         }
642                         read_unlock_bh(&idev->lock);
643                 }
644         }
645
646 out_unlock_base:
647         read_unlock(&addrconf_lock);
648         read_unlock(&dev_base_lock);
649
650 out:
651         if (ifp == NULL) {
652                 ifp = match;
653                 match = NULL;
654         }
655
656         err = -EADDRNOTAVAIL;
657         if (ifp) {
658                 ipv6_addr_copy(saddr, &ifp->addr);
659                 err = 0;
660                 in6_ifa_put(ifp);
661         }
662         if (match)
663                 in6_ifa_put(match);
664
665         return err;
666 }
667
668
669 int ipv6_get_saddr(struct dst_entry *dst,
670                    struct in6_addr *daddr, struct in6_addr *saddr)
671 {
672         struct rt6_info *rt;
673         struct net_device *dev = NULL;
674         int onlink;
675
676         rt = (struct rt6_info *) dst;
677         if (rt)
678                 dev = rt->rt6i_dev;
679
680         onlink = (rt && (rt->rt6i_flags & RTF_ALLONLINK));
681
682         return ipv6_dev_get_saddr(dev, daddr, saddr, onlink);
683 }
684
685
686 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
687 {
688         struct inet6_dev *idev;
689         int err = -EADDRNOTAVAIL;
690
691         read_lock(&addrconf_lock);
692         if ((idev = __in6_dev_get(dev)) != NULL) {
693                 struct inet6_ifaddr *ifp;
694
695                 read_lock_bh(&idev->lock);
696                 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
697                         if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
698                                 ipv6_addr_copy(addr, &ifp->addr);
699                                 err = 0;
700                                 break;
701                         }
702                 }
703                 read_unlock_bh(&idev->lock);
704         }
705         read_unlock(&addrconf_lock);
706         return err;
707 }
708
709 int ipv6_count_addresses(struct inet6_dev *idev)
710 {
711         int cnt = 0;
712         struct inet6_ifaddr *ifp;
713
714         read_lock_bh(&idev->lock);
715         for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
716                 cnt++;
717         read_unlock_bh(&idev->lock);
718         return cnt;
719 }
720
721 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev)
722 {
723         struct inet6_ifaddr * ifp;
724         u8 hash = ipv6_addr_hash(addr);
725
726         read_lock_bh(&addrconf_hash_lock);
727         for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
728                 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
729                     !(ifp->flags&IFA_F_TENTATIVE)) {
730                         if (dev == NULL || ifp->idev->dev == dev ||
731                             !(ifp->scope&(IFA_LINK|IFA_HOST)))
732                                 break;
733                 }
734         }
735         read_unlock_bh(&addrconf_hash_lock);
736         return ifp != NULL;
737 }
738
739 static
740 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
741 {
742         struct inet6_ifaddr * ifp;
743         u8 hash = ipv6_addr_hash(addr);
744
745         read_lock_bh(&addrconf_hash_lock);
746         for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
747                 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
748                         if (dev == NULL || ifp->idev->dev == dev)
749                                 break;
750                 }
751         }
752         read_unlock_bh(&addrconf_hash_lock);
753         return ifp != NULL;
754 }
755
756 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev)
757 {
758         struct inet6_ifaddr * ifp;
759         u8 hash = ipv6_addr_hash(addr);
760
761         read_lock_bh(&addrconf_hash_lock);
762         for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
763                 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
764                         if (dev == NULL || ifp->idev->dev == dev ||
765                             !(ifp->scope&(IFA_LINK|IFA_HOST))) {
766                                 in6_ifa_hold(ifp);
767                                 break;
768                         }
769                 }
770         }
771         read_unlock_bh(&addrconf_hash_lock);
772
773         return ifp;
774 }
775
776 /* Gets referenced address, destroys ifaddr */
777
778 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
779 {
780         if (net_ratelimit())
781                 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
782         if (ifp->flags&IFA_F_PERMANENT) {
783                 spin_lock_bh(&ifp->lock);
784                 addrconf_del_timer(ifp);
785                 ifp->flags |= IFA_F_TENTATIVE;
786                 spin_unlock_bh(&ifp->lock);
787                 in6_ifa_put(ifp);
788         } else
789                 ipv6_del_addr(ifp);
790 }
791
792
793 /* Join to solicited addr multicast group. */
794
795 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
796 {
797         struct in6_addr maddr;
798
799         if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
800                 return;
801
802         addrconf_addr_solict_mult(addr, &maddr);
803         ipv6_dev_mc_inc(dev, &maddr);
804 }
805
806 void addrconf_leave_solict(struct net_device *dev, struct in6_addr *addr)
807 {
808         struct in6_addr maddr;
809
810         if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
811                 return;
812
813         addrconf_addr_solict_mult(addr, &maddr);
814         ipv6_dev_mc_dec(dev, &maddr);
815 }
816
817
818 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
819 {
820         switch (dev->type) {
821         case ARPHRD_ETHER:
822         case ARPHRD_FDDI:
823         case ARPHRD_IEEE802_TR:
824                 if (dev->addr_len != ETH_ALEN)
825                         return -1;
826                 memcpy(eui, dev->dev_addr, 3);
827                 memcpy(eui + 5, dev->dev_addr+3, 3);
828                 eui[3] = 0xFF;
829                 eui[4] = 0xFE;
830                 eui[0] ^= 2;
831                 return 0;
832         case ARPHRD_ARCNET:
833                 /* XXX: inherit EUI-64 fro mother interface -- yoshfuji */
834                 if (dev->addr_len != ARCNET_ALEN)
835                         return -1;
836                 memset(eui, 0, 7);
837                 eui[7] = *(u8*)dev->dev_addr;
838                 return 0;
839         }
840         return -1;
841 }
842
843 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
844 {
845         int err = -1;
846         struct inet6_ifaddr *ifp;
847
848         read_lock_bh(&idev->lock);
849         for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
850                 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
851                         memcpy(eui, ifp->addr.s6_addr+8, 8);
852                         err = 0;
853                         break;
854                 }
855         }
856         read_unlock_bh(&idev->lock);
857         return err;
858 }
859
860 /*
861  *      Add prefix route.
862  */
863
864 static void
865 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
866                       unsigned long expires, unsigned flags)
867 {
868         struct in6_rtmsg rtmsg;
869
870         memset(&rtmsg, 0, sizeof(rtmsg));
871         memcpy(&rtmsg.rtmsg_dst, pfx, sizeof(struct in6_addr));
872         rtmsg.rtmsg_dst_len = plen;
873         rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
874         rtmsg.rtmsg_ifindex = dev->ifindex;
875         rtmsg.rtmsg_info = expires;
876         rtmsg.rtmsg_flags = RTF_UP|flags;
877         rtmsg.rtmsg_type = RTMSG_NEWROUTE;
878
879         /* Prevent useless cloning on PtP SIT.
880            This thing is done here expecting that the whole
881            class of non-broadcast devices need not cloning.
882          */
883         if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
884                 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
885
886         ip6_route_add(&rtmsg, NULL, NULL);
887 }
888
889 /* Create "default" multicast route to the interface */
890
891 static void addrconf_add_mroute(struct net_device *dev)
892 {
893         struct in6_rtmsg rtmsg;
894
895         memset(&rtmsg, 0, sizeof(rtmsg));
896         ipv6_addr_set(&rtmsg.rtmsg_dst,
897                       htonl(0xFF000000), 0, 0, 0);
898         rtmsg.rtmsg_dst_len = 8;
899         rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
900         rtmsg.rtmsg_ifindex = dev->ifindex;
901         rtmsg.rtmsg_flags = RTF_UP;
902         rtmsg.rtmsg_type = RTMSG_NEWROUTE;
903         ip6_route_add(&rtmsg, NULL, NULL);
904 }
905
906 static void sit_route_add(struct net_device *dev)
907 {
908         struct in6_rtmsg rtmsg;
909
910         memset(&rtmsg, 0, sizeof(rtmsg));
911
912         rtmsg.rtmsg_type        = RTMSG_NEWROUTE;
913         rtmsg.rtmsg_metric      = IP6_RT_PRIO_ADDRCONF;
914
915         /* prefix length - 96 bits "::d.d.d.d" */
916         rtmsg.rtmsg_dst_len     = 96;
917         rtmsg.rtmsg_flags       = RTF_UP|RTF_NONEXTHOP;
918         rtmsg.rtmsg_ifindex     = dev->ifindex;
919
920         ip6_route_add(&rtmsg, NULL, NULL);
921 }
922
923 static void addrconf_add_lroute(struct net_device *dev)
924 {
925         struct in6_addr addr;
926
927         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
928         addrconf_prefix_route(&addr, 64, dev, 0, 0);
929 }
930
931 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
932 {
933         struct inet6_dev *idev;
934
935         ASSERT_RTNL();
936
937         if ((idev = ipv6_find_idev(dev)) == NULL)
938                 return NULL;
939
940         /* Add default multicast route */
941         addrconf_add_mroute(dev);
942
943         /* Add link local route */
944         addrconf_add_lroute(dev);
945         return idev;
946 }
947
948 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
949 {
950         struct prefix_info *pinfo;
951         struct rt6_info *rt;
952         __u32 valid_lft;
953         __u32 prefered_lft;
954         int addr_type;
955         unsigned long rt_expires;
956         struct inet6_dev *in6_dev;
957
958         pinfo = (struct prefix_info *) opt;
959         
960         if (len < sizeof(struct prefix_info)) {
961                 ADBG(("addrconf: prefix option too short\n"));
962                 return;
963         }
964         
965         /*
966          *      Validation checks ([ADDRCONF], page 19)
967          */
968
969         addr_type = ipv6_addr_type(&pinfo->prefix);
970
971         if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
972                 return;
973
974         valid_lft = ntohl(pinfo->valid);
975         prefered_lft = ntohl(pinfo->prefered);
976
977         if (prefered_lft > valid_lft) {
978                 if (net_ratelimit())
979                         printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
980                 return;
981         }
982
983         in6_dev = in6_dev_get(dev);
984
985         if (in6_dev == NULL) {
986                 if (net_ratelimit())
987                         printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
988                 return;
989         }
990
991         /*
992          *      Two things going on here:
993          *      1) Add routes for on-link prefixes
994          *      2) Configure prefixes with the auto flag set
995          */
996
997         /* Avoid arithemtic overflow. Really, we could
998            save rt_expires in seconds, likely valid_lft,
999            but it would require division in fib gc, that it
1000            not good.
1001          */
1002         if (valid_lft >= 0x7FFFFFFF/HZ)
1003                 rt_expires = 0;
1004         else
1005                 rt_expires = jiffies + valid_lft * HZ;
1006
1007         rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1008
1009         if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1010                 if (rt->rt6i_flags&RTF_EXPIRES) {
1011                         if (pinfo->onlink == 0 || valid_lft == 0) {
1012                                 ip6_del_rt(rt, NULL, NULL);
1013                                 rt = NULL;
1014                         } else {
1015                                 rt->rt6i_expires = rt_expires;
1016                         }
1017                 }
1018         } else if (pinfo->onlink && valid_lft) {
1019                 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1020                                       dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1021         }
1022         if (rt)
1023                 dst_release(&rt->u.dst);
1024
1025         /* Try to figure out our local address for this prefix */
1026
1027         if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1028                 struct inet6_ifaddr * ifp;
1029                 struct in6_addr addr;
1030                 int plen;
1031
1032                 plen = pinfo->prefix_len >> 3;
1033
1034                 if (pinfo->prefix_len == 64) {
1035                         memcpy(&addr, &pinfo->prefix, 8);
1036                         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1037                             ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1038                                 in6_dev_put(in6_dev);
1039                                 return;
1040                         }
1041                         goto ok;
1042                 }
1043                 if (net_ratelimit())
1044                         printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1045                                pinfo->prefix_len);
1046                 in6_dev_put(in6_dev);
1047                 return;
1048
1049 ok:
1050
1051                 ifp = ipv6_get_ifaddr(&addr, dev);
1052
1053                 if (ifp == NULL && valid_lft) {
1054                         /* Do not allow to create too much of autoconfigured
1055                          * addresses; this would be too easy way to crash kernel.
1056                          */
1057                         if (ipv6_count_addresses(in6_dev) < IPV6_MAX_ADDRESSES)
1058                                 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1059                                                     addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1060
1061                         if (IS_ERR(ifp)) {
1062                                 in6_dev_put(in6_dev);
1063                                 return;
1064                         }
1065
1066                         addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1067                 }
1068
1069                 if (ifp && valid_lft == 0) {
1070                         ipv6_del_addr(ifp);
1071                         ifp = NULL;
1072                 }
1073
1074                 if (ifp) {
1075                         int flags;
1076
1077                         spin_lock(&ifp->lock);
1078                         ifp->valid_lft = valid_lft;
1079                         ifp->prefered_lft = prefered_lft;
1080                         ifp->tstamp = jiffies;
1081                         flags = ifp->flags;
1082                         ifp->flags &= ~IFA_F_DEPRECATED;
1083                         spin_unlock(&ifp->lock);
1084
1085                         if (!(flags&IFA_F_TENTATIVE))
1086                                 ipv6_ifa_notify((flags&IFA_F_DEPRECATED) ?
1087                                                 0 : RTM_NEWADDR, ifp);
1088                         in6_ifa_put(ifp);
1089                         addrconf_verify(0);
1090                 }
1091         }
1092         in6_dev_put(in6_dev);
1093 }
1094
1095 /*
1096  *      Set destination address.
1097  *      Special case for SIT interfaces where we create a new "virtual"
1098  *      device.
1099  */
1100 int addrconf_set_dstaddr(void *arg)
1101 {
1102         struct in6_ifreq ireq;
1103         struct net_device *dev;
1104         int err = -EINVAL;
1105
1106         rtnl_lock();
1107
1108         err = -EFAULT;
1109         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1110                 goto err_exit;
1111
1112         dev = __dev_get_by_index(ireq.ifr6_ifindex);
1113
1114         err = -ENODEV;
1115         if (dev == NULL)
1116                 goto err_exit;
1117
1118         if (dev->type == ARPHRD_SIT) {
1119                 struct ifreq ifr;
1120                 mm_segment_t    oldfs;
1121                 struct ip_tunnel_parm p;
1122
1123                 err = -EADDRNOTAVAIL;
1124                 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1125                         goto err_exit;
1126
1127                 memset(&p, 0, sizeof(p));
1128                 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1129                 p.iph.saddr = 0;
1130                 p.iph.version = 4;
1131                 p.iph.ihl = 5;
1132                 p.iph.protocol = IPPROTO_IPV6;
1133                 p.iph.ttl = 64;
1134                 ifr.ifr_ifru.ifru_data = (void*)&p;
1135
1136                 oldfs = get_fs(); set_fs(KERNEL_DS);
1137                 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1138                 set_fs(oldfs);
1139
1140                 if (err == 0) {
1141                         err = -ENOBUFS;
1142                         if ((dev = __dev_get_by_name(p.name)) == NULL)
1143                                 goto err_exit;
1144                         err = dev_open(dev);
1145                 }
1146         }
1147
1148 err_exit:
1149         rtnl_unlock();
1150         return err;
1151 }
1152
1153 /*
1154  *      Manual configuration of address on an interface
1155  */
1156 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1157 {
1158         struct inet6_ifaddr *ifp;
1159         struct inet6_dev *idev;
1160         struct net_device *dev;
1161         int scope;
1162
1163         ASSERT_RTNL();
1164         
1165         if ((dev = __dev_get_by_index(ifindex)) == NULL)
1166                 return -ENODEV;
1167         
1168         if (!(dev->flags&IFF_UP))
1169                 return -ENETDOWN;
1170
1171         if ((idev = addrconf_add_dev(dev)) == NULL)
1172                 return -ENOBUFS;
1173
1174         scope = ipv6_addr_scope(pfx);
1175
1176         ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1177         if (!IS_ERR(ifp)) {
1178                 addrconf_dad_start(ifp, 0);
1179                 in6_ifa_put(ifp);
1180                 return 0;
1181         }
1182
1183         return PTR_ERR(ifp);
1184 }
1185
1186 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1187 {
1188         struct inet6_ifaddr *ifp;
1189         struct inet6_dev *idev;
1190         struct net_device *dev;
1191         
1192         if ((dev = __dev_get_by_index(ifindex)) == NULL)
1193                 return -ENODEV;
1194
1195         if ((idev = __in6_dev_get(dev)) == NULL)
1196                 return -ENXIO;
1197
1198         read_lock_bh(&idev->lock);
1199         for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1200                 if (ifp->prefix_len == plen &&
1201                     (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
1202                         in6_ifa_hold(ifp);
1203                         read_unlock_bh(&idev->lock);
1204                         
1205                         ipv6_del_addr(ifp);
1206
1207                         /* If the last address is deleted administratively,
1208                            disable IPv6 on this interface.
1209                          */
1210                         if (idev->addr_list == NULL)
1211                                 addrconf_ifdown(idev->dev, 1);
1212                         return 0;
1213                 }
1214         }
1215         read_unlock_bh(&idev->lock);
1216         return -EADDRNOTAVAIL;
1217 }
1218
1219
1220 int addrconf_add_ifaddr(void *arg)
1221 {
1222         struct in6_ifreq ireq;
1223         int err;
1224         
1225         if (!capable(CAP_NET_ADMIN))
1226                 return -EPERM;
1227         
1228         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1229                 return -EFAULT;
1230
1231         rtnl_lock();
1232         err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1233         rtnl_unlock();
1234         return err;
1235 }
1236
1237 int addrconf_del_ifaddr(void *arg)
1238 {
1239         struct in6_ifreq ireq;
1240         int err;
1241         
1242         if (!capable(CAP_NET_ADMIN))
1243                 return -EPERM;
1244
1245         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1246                 return -EFAULT;
1247
1248         rtnl_lock();
1249         err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1250         rtnl_unlock();
1251         return err;
1252 }
1253
1254 static void sit_add_v4_addrs(struct inet6_dev *idev)
1255 {
1256         struct inet6_ifaddr * ifp;
1257         struct in6_addr addr;
1258         struct net_device *dev;
1259         int scope;
1260
1261         ASSERT_RTNL();
1262
1263         memset(&addr, 0, sizeof(struct in6_addr));
1264         memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1265
1266         if (idev->dev->flags&IFF_POINTOPOINT) {
1267                 addr.s6_addr32[0] = htonl(0xfe800000);
1268                 scope = IFA_LINK;
1269         } else {
1270                 scope = IPV6_ADDR_COMPATv4;
1271         }
1272
1273         if (addr.s6_addr32[3]) {
1274                 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1275                 if (!IS_ERR(ifp)) {
1276                         spin_lock_bh(&ifp->lock);
1277                         ifp->flags &= ~IFA_F_TENTATIVE;
1278                         spin_unlock_bh(&ifp->lock);
1279                         ipv6_ifa_notify(RTM_NEWADDR, ifp);
1280                         in6_ifa_put(ifp);
1281                 }
1282                 return;
1283         }
1284
1285         for (dev = dev_base; dev != NULL; dev = dev->next) {
1286                 struct in_device * in_dev = __in_dev_get(dev);
1287                 if (in_dev && (dev->flags & IFF_UP)) {
1288                         struct in_ifaddr * ifa;
1289
1290                         int flag = scope;
1291
1292                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1293                                 int plen;
1294
1295                                 addr.s6_addr32[3] = ifa->ifa_local;
1296
1297                                 if (ifa->ifa_scope == RT_SCOPE_LINK)
1298                                         continue;
1299                                 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1300                                         if (idev->dev->flags&IFF_POINTOPOINT)
1301                                                 continue;
1302                                         flag |= IFA_HOST;
1303                                 }
1304                                 if (idev->dev->flags&IFF_POINTOPOINT)
1305                                         plen = 64;
1306                                 else
1307                                         plen = 96;
1308
1309                                 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1310                                                     IFA_F_PERMANENT);
1311                                 if (!IS_ERR(ifp)) {
1312                                         spin_lock_bh(&ifp->lock);
1313                                         ifp->flags &= ~IFA_F_TENTATIVE;
1314                                         spin_unlock_bh(&ifp->lock);
1315                                         ipv6_ifa_notify(RTM_NEWADDR, ifp);
1316                                         in6_ifa_put(ifp);
1317                                 }
1318                         }
1319                 }
1320         }
1321 }
1322
1323 static void init_loopback(struct net_device *dev)
1324 {
1325         struct inet6_dev  *idev;
1326         struct inet6_ifaddr * ifp;
1327
1328         /* ::1 */
1329
1330         ASSERT_RTNL();
1331
1332         if ((idev = ipv6_find_idev(dev)) == NULL) {
1333                 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1334                 return;
1335         }
1336
1337         ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1338         if (!IS_ERR(ifp)) {
1339                 spin_lock_bh(&ifp->lock);
1340                 ifp->flags &= ~IFA_F_TENTATIVE;
1341                 spin_unlock_bh(&ifp->lock);
1342                 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1343                 in6_ifa_put(ifp);
1344         }
1345 }
1346
1347 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1348 {
1349         struct inet6_ifaddr * ifp;
1350
1351         ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1352         if (!IS_ERR(ifp)) {
1353                 addrconf_dad_start(ifp, 0);
1354                 in6_ifa_put(ifp);
1355         }
1356 }
1357
1358 static void addrconf_dev_config(struct net_device *dev)
1359 {
1360         struct in6_addr addr;
1361         struct inet6_dev    * idev;
1362
1363         ASSERT_RTNL();
1364
1365         if ((dev->type != ARPHRD_ETHER) && 
1366             (dev->type != ARPHRD_FDDI) &&
1367             (dev->type != ARPHRD_IEEE802_TR) &&
1368             (dev->type != ARPHRD_ARCNET)) {
1369                 /* Alas, we support only Ethernet autoconfiguration. */
1370                 return;
1371         }
1372
1373         idev = addrconf_add_dev(dev);
1374         if (idev == NULL)
1375                 return;
1376
1377         memset(&addr, 0, sizeof(struct in6_addr));
1378         addr.s6_addr32[0] = htonl(0xFE800000);
1379
1380         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1381                 addrconf_add_linklocal(idev, &addr);
1382 }
1383
1384 static void addrconf_sit_config(struct net_device *dev)
1385 {
1386         struct inet6_dev *idev;
1387
1388         ASSERT_RTNL();
1389
1390         /* 
1391          * Configure the tunnel with one of our IPv4 
1392          * addresses... we should configure all of 
1393          * our v4 addrs in the tunnel
1394          */
1395
1396         if ((idev = ipv6_find_idev(dev)) == NULL) {
1397                 printk(KERN_DEBUG "init sit: add_dev failed\n");
1398                 return;
1399         }
1400
1401         sit_add_v4_addrs(idev);
1402
1403         if (dev->flags&IFF_POINTOPOINT) {
1404                 addrconf_add_mroute(dev);
1405                 addrconf_add_lroute(dev);
1406         } else
1407                 sit_route_add(dev);
1408 }
1409
1410
1411 int addrconf_notify(struct notifier_block *this, unsigned long event, 
1412                     void * data)
1413 {
1414         struct net_device *dev = (struct net_device *) data;
1415         struct inet6_dev *idev = __in6_dev_get(dev);
1416
1417         switch(event) {
1418         case NETDEV_UP:
1419                 switch(dev->type) {
1420                 case ARPHRD_SIT:
1421                         addrconf_sit_config(dev);
1422                         break;
1423
1424                 case ARPHRD_LOOPBACK:
1425                         init_loopback(dev);
1426                         break;
1427
1428                 default:
1429                         addrconf_dev_config(dev);
1430                         break;
1431                 };
1432                 if (idev) {
1433                         /* If the MTU changed during the interface down, when the 
1434                            interface up, the changed MTU must be reflected in the 
1435                            idev as well as routers.
1436                          */
1437                         if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
1438                                 rt6_mtu_change(dev, dev->mtu);
1439                                 idev->cnf.mtu6 = dev->mtu;
1440                         }
1441                         /* If the changed mtu during down is lower than IPV6_MIN_MTU
1442                            stop IPv6 on this interface.
1443                          */
1444                         if (dev->mtu < IPV6_MIN_MTU)
1445                                 addrconf_ifdown(dev, event != NETDEV_DOWN);
1446                 }
1447                 break;
1448
1449         case NETDEV_CHANGEMTU:
1450                 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
1451                         rt6_mtu_change(dev, dev->mtu);
1452                         idev->cnf.mtu6 = dev->mtu;
1453                         break;
1454                 }
1455
1456                 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1457
1458         case NETDEV_DOWN:
1459         case NETDEV_UNREGISTER:
1460                 /*
1461                  *      Remove all addresses from this interface.
1462                  */
1463                 addrconf_ifdown(dev, event != NETDEV_DOWN);
1464                 break;
1465         case NETDEV_CHANGE:
1466                 break;
1467         };
1468
1469         return NOTIFY_OK;
1470 }
1471
1472 static int addrconf_ifdown(struct net_device *dev, int how)
1473 {
1474         struct inet6_dev *idev;
1475         struct inet6_ifaddr *ifa, **bifa;
1476         int i;
1477
1478         ASSERT_RTNL();
1479
1480         rt6_ifdown(dev);
1481         neigh_ifdown(&nd_tbl, dev);
1482
1483         idev = __in6_dev_get(dev);
1484         if (idev == NULL)
1485                 return -ENODEV;
1486
1487         /* Step 1: remove reference to ipv6 device from parent device.
1488                    Do not dev_put!
1489          */
1490         if (how == 1) {
1491                 write_lock_bh(&addrconf_lock);
1492                 dev->ip6_ptr = NULL;
1493                 idev->dead = 1;
1494                 write_unlock_bh(&addrconf_lock);
1495         }
1496
1497         /* Step 2: clear hash table */
1498         for (i=0; i<IN6_ADDR_HSIZE; i++) {
1499                 bifa = &inet6_addr_lst[i];
1500
1501                 write_lock_bh(&addrconf_hash_lock);
1502                 while ((ifa = *bifa) != NULL) {
1503                         if (ifa->idev == idev) {
1504                                 *bifa = ifa->lst_next;
1505                                 ifa->lst_next = NULL;
1506                                 addrconf_del_timer(ifa);
1507                                 in6_ifa_put(ifa);
1508                                 continue;
1509                         }
1510                         bifa = &ifa->lst_next;
1511                 }
1512                 write_unlock_bh(&addrconf_hash_lock);
1513         }
1514
1515         /* Step 3: clear address list */
1516
1517         write_lock_bh(&idev->lock);
1518         while ((ifa = idev->addr_list) != NULL) {
1519                 idev->addr_list = ifa->if_next;
1520                 ifa->if_next = NULL;
1521                 ifa->dead = 1;
1522                 addrconf_del_timer(ifa);
1523                 write_unlock_bh(&idev->lock);
1524
1525                 ipv6_ifa_notify(RTM_DELADDR, ifa);
1526                 in6_ifa_put(ifa);
1527
1528                 write_lock_bh(&idev->lock);
1529         }
1530         write_unlock_bh(&idev->lock);
1531
1532         /* Step 4: Discard multicast list */
1533
1534         if (how == 1)
1535                 ipv6_mc_destroy_dev(idev);
1536         else
1537                 ipv6_mc_down(idev);
1538
1539         /* Shot the device (if unregistered) */
1540
1541         if (how == 1) {
1542                 neigh_parms_release(&nd_tbl, idev->nd_parms);
1543 #ifdef CONFIG_SYSCTL
1544                 addrconf_sysctl_unregister(&idev->cnf);
1545 #endif
1546                 in6_dev_put(idev);
1547         }
1548         return 0;
1549 }
1550
1551 static void addrconf_rs_timer(unsigned long data)
1552 {
1553         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
1554
1555         if (ifp->idev->cnf.forwarding)
1556                 goto out;
1557
1558         if (ifp->idev->if_flags & IF_RA_RCVD) {
1559                 /*
1560                  *      Announcement received after solicitation
1561                  *      was sent
1562                  */
1563                 goto out;
1564         }
1565
1566         spin_lock(&ifp->lock);
1567         if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
1568                 struct in6_addr all_routers;
1569
1570                 /* The wait after the last probe can be shorter */
1571                 addrconf_mod_timer(ifp, AC_RS,
1572                                    (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
1573                                    ifp->idev->cnf.rtr_solicit_delay :
1574                                    ifp->idev->cnf.rtr_solicit_interval);
1575                 spin_unlock(&ifp->lock);
1576
1577                 ipv6_addr_all_routers(&all_routers);
1578
1579                 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
1580         } else {
1581                 struct in6_rtmsg rtmsg;
1582
1583                 spin_unlock(&ifp->lock);
1584
1585                 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
1586                        ifp->idev->dev->name);
1587
1588                 memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
1589                 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1590                 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1591                 rtmsg.rtmsg_flags = (RTF_ALLONLINK | RTF_DEFAULT | RTF_UP);
1592
1593                 rtmsg.rtmsg_ifindex = ifp->idev->dev->ifindex;
1594
1595                 ip6_route_add(&rtmsg, NULL, NULL);
1596         }
1597
1598 out:
1599         in6_ifa_put(ifp);
1600 }
1601
1602 /*
1603  *      Duplicate Address Detection
1604  */
1605 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
1606 {
1607         struct net_device *dev;
1608         unsigned long rand_num;
1609
1610         dev = ifp->idev->dev;
1611
1612         addrconf_join_solict(dev, &ifp->addr);
1613
1614         if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
1615                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0, flags);
1616
1617         net_srandom(ifp->addr.s6_addr32[3]);
1618         rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
1619
1620         spin_lock_bh(&ifp->lock);
1621
1622         if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
1623             !(ifp->flags&IFA_F_TENTATIVE)) {
1624                 ifp->flags &= ~IFA_F_TENTATIVE;
1625                 spin_unlock_bh(&ifp->lock);
1626
1627                 addrconf_dad_completed(ifp);
1628                 return;
1629         }
1630
1631         ifp->probes = ifp->idev->cnf.dad_transmits;
1632         addrconf_mod_timer(ifp, AC_DAD, rand_num);
1633
1634         spin_unlock_bh(&ifp->lock);
1635 }
1636
1637 static void addrconf_dad_timer(unsigned long data)
1638 {
1639         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
1640         struct in6_addr unspec;
1641         struct in6_addr mcaddr;
1642
1643         spin_lock_bh(&ifp->lock);
1644         if (ifp->probes == 0) {
1645                 /*
1646                  * DAD was successful
1647                  */
1648
1649                 ifp->flags &= ~IFA_F_TENTATIVE;
1650                 spin_unlock_bh(&ifp->lock);
1651
1652                 addrconf_dad_completed(ifp);
1653
1654                 in6_ifa_put(ifp);
1655                 return;
1656         }
1657
1658         ifp->probes--;
1659         addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
1660         spin_unlock_bh(&ifp->lock);
1661
1662         /* send a neighbour solicitation for our addr */
1663         memset(&unspec, 0, sizeof(unspec));
1664         addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
1665         ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
1666
1667         in6_ifa_put(ifp);
1668 }
1669
1670 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
1671 {
1672         struct net_device *     dev = ifp->idev->dev;
1673
1674         /*
1675          *      Configure the address for reception. Now it is valid.
1676          */
1677
1678         ipv6_ifa_notify(RTM_NEWADDR, ifp);
1679
1680         /* If added prefix is link local and forwarding is off,
1681            start sending router solicitations.
1682          */
1683
1684         if (ifp->idev->cnf.forwarding == 0 &&
1685             (dev->flags&IFF_LOOPBACK) == 0 &&
1686             (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
1687                 struct in6_addr all_routers;
1688
1689                 ipv6_addr_all_routers(&all_routers);
1690
1691                 /*
1692                  *      If a host as already performed a random delay
1693                  *      [...] as part of DAD [...] there is no need
1694                  *      to delay again before sending the first RS
1695                  */
1696                 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
1697
1698                 spin_lock_bh(&ifp->lock);
1699                 ifp->probes = 1;
1700                 ifp->idev->if_flags |= IF_RS_SENT;
1701                 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
1702                 spin_unlock_bh(&ifp->lock);
1703         }
1704
1705         if (ifp->idev->cnf.forwarding) {
1706                 struct in6_addr addr;
1707
1708                 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1709                 if (!ipv6_addr_any(&addr))
1710                         ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1711         }
1712 }
1713
1714 #ifdef CONFIG_PROC_FS
1715 static int iface_proc_info(char *buffer, char **start, off_t offset,
1716                            int length)
1717 {
1718         struct inet6_ifaddr *ifp;
1719         int i;
1720         int len = 0;
1721         off_t pos=0;
1722         off_t begin=0;
1723
1724         for (i=0; i < IN6_ADDR_HSIZE; i++) {
1725                 read_lock_bh(&addrconf_hash_lock);
1726                 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
1727                         int j;
1728
1729                         for (j=0; j<16; j++) {
1730                                 sprintf(buffer + len, "%02x",
1731                                         ifp->addr.s6_addr[j]);
1732                                 len += 2;
1733                         }
1734
1735                         len += sprintf(buffer + len,
1736                                        " %02x %02x %02x %02x %8s\n",
1737                                        ifp->idev->dev->ifindex,
1738                                        ifp->prefix_len,
1739                                        ifp->scope,
1740                                        ifp->flags,
1741                                        ifp->idev->dev->name);
1742                         pos=begin+len;
1743                         if(pos<offset) {
1744                                 len=0;
1745                                 begin=pos;
1746                         }
1747                         if(pos>offset+length) {
1748                                 read_unlock_bh(&addrconf_hash_lock);
1749                                 goto done;
1750                         }
1751                 }
1752                 read_unlock_bh(&addrconf_hash_lock);
1753         }
1754
1755 done:
1756
1757         *start=buffer+(offset-begin);
1758         len-=(offset-begin);
1759         if(len>length)
1760                 len=length;
1761         if(len<0)
1762                 len=0;
1763         return len;
1764 }
1765
1766 #endif  /* CONFIG_PROC_FS */
1767
1768 /*
1769  *      Periodic address status verification
1770  */
1771
1772 void addrconf_verify(unsigned long foo)
1773 {
1774         struct inet6_ifaddr *ifp;
1775         unsigned long now, next;
1776         int i;
1777
1778         spin_lock_bh(&addrconf_verify_lock);
1779         now = jiffies;
1780         next = now + ADDR_CHECK_FREQUENCY;
1781
1782         del_timer(&addr_chk_timer);
1783
1784         for (i=0; i < IN6_ADDR_HSIZE; i++) {
1785
1786 restart:
1787                 write_lock(&addrconf_hash_lock);
1788                 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
1789                         unsigned long age;
1790
1791                         if (ifp->flags & IFA_F_PERMANENT)
1792                                 continue;
1793
1794                         spin_lock(&ifp->lock);
1795                         age = (now - ifp->tstamp) / HZ;
1796
1797                         if (age >= ifp->valid_lft) {
1798                                 spin_unlock(&ifp->lock);
1799                                 in6_ifa_hold(ifp);
1800                                 write_unlock(&addrconf_hash_lock);
1801                                 ipv6_del_addr(ifp);
1802                                 goto restart;
1803                         } else if (age >= ifp->prefered_lft) {
1804                                 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
1805                                 int deprecate = 0;
1806
1807                                 if (!(ifp->flags&IFA_F_DEPRECATED)) {
1808                                         deprecate = 1;
1809                                         ifp->flags |= IFA_F_DEPRECATED;
1810                                 }
1811
1812                                 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
1813                                         next = ifp->tstamp + ifp->valid_lft * HZ;
1814
1815                                 spin_unlock(&ifp->lock);
1816
1817                                 if (deprecate) {
1818                                         in6_ifa_hold(ifp);
1819                                         write_unlock(&addrconf_hash_lock);
1820
1821                                         ipv6_ifa_notify(0, ifp);
1822                                         in6_ifa_put(ifp);
1823                                         goto restart;
1824                                 }
1825                         } else {
1826                                 /* ifp->prefered_lft <= ifp->valid_lft */
1827                                 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
1828                                         next = ifp->tstamp + ifp->prefered_lft * HZ;
1829                                 spin_unlock(&ifp->lock);
1830                         }
1831                 }
1832                 write_unlock(&addrconf_hash_lock);
1833         }
1834
1835         addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
1836         add_timer(&addr_chk_timer);
1837         spin_unlock_bh(&addrconf_verify_lock);
1838 }
1839
1840 static int
1841 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1842 {
1843         struct rtattr **rta = arg;
1844         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
1845         struct in6_addr *pfx;
1846
1847         pfx = NULL;
1848         if (rta[IFA_ADDRESS-1]) {
1849                 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
1850                         return -EINVAL;
1851                 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
1852         }
1853         if (rta[IFA_LOCAL-1]) {
1854                 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
1855                         return -EINVAL;
1856                 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
1857         }
1858         if (pfx == NULL)
1859                 return -EINVAL;
1860
1861         return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
1862 }
1863
1864 static int
1865 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1866 {
1867         struct rtattr  **rta = arg;
1868         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
1869         struct in6_addr *pfx;
1870
1871         pfx = NULL;
1872         if (rta[IFA_ADDRESS-1]) {
1873                 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
1874                         return -EINVAL;
1875                 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
1876         }
1877         if (rta[IFA_LOCAL-1]) {
1878                 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
1879                         return -EINVAL;
1880                 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
1881         }
1882         if (pfx == NULL)
1883                 return -EINVAL;
1884
1885         return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
1886 }
1887
1888 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
1889                              u32 pid, u32 seq, int event)
1890 {
1891         struct ifaddrmsg *ifm;
1892         struct nlmsghdr  *nlh;
1893         struct ifa_cacheinfo ci;
1894         unsigned char    *b = skb->tail;
1895
1896         nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1897         if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1898         ifm = NLMSG_DATA(nlh);
1899         ifm->ifa_family = AF_INET6;
1900         ifm->ifa_prefixlen = ifa->prefix_len;
1901         ifm->ifa_flags = ifa->flags;
1902         ifm->ifa_scope = RT_SCOPE_UNIVERSE;
1903         if (ifa->scope&IFA_HOST)
1904                 ifm->ifa_scope = RT_SCOPE_HOST;
1905         else if (ifa->scope&IFA_LINK)
1906                 ifm->ifa_scope = RT_SCOPE_LINK;
1907         else if (ifa->scope&IFA_SITE)
1908                 ifm->ifa_scope = RT_SCOPE_SITE;
1909         ifm->ifa_index = ifa->idev->dev->ifindex;
1910         RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
1911         if (!(ifa->flags&IFA_F_PERMANENT)) {
1912                 ci.ifa_prefered = ifa->prefered_lft;
1913                 ci.ifa_valid = ifa->valid_lft;
1914                 if (ci.ifa_prefered != 0xFFFFFFFF) {
1915                         long tval = (jiffies - ifa->tstamp)/HZ;
1916                         ci.ifa_prefered -= tval;
1917                         if (ci.ifa_valid != 0xFFFFFFFF)
1918                                 ci.ifa_valid -= tval;
1919                 }
1920                 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1921         }
1922         nlh->nlmsg_len = skb->tail - b;
1923         return skb->len;
1924
1925 nlmsg_failure:
1926 rtattr_failure:
1927         skb_trim(skb, b - skb->data);
1928         return -1;
1929 }
1930
1931 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1932 {
1933         int idx, ip_idx;
1934         int s_idx, s_ip_idx;
1935         struct inet6_ifaddr *ifa;
1936
1937         s_idx = cb->args[0];
1938         s_ip_idx = ip_idx = cb->args[1];
1939
1940         for (idx=0; idx < IN6_ADDR_HSIZE; idx++) {
1941                 if (idx < s_idx)
1942                         continue;
1943                 if (idx > s_idx)
1944                         s_ip_idx = 0;
1945                 read_lock_bh(&addrconf_hash_lock);
1946                 for (ifa=inet6_addr_lst[idx], ip_idx = 0; ifa;
1947                      ifa = ifa->lst_next, ip_idx++) {
1948                         if (ip_idx < s_ip_idx)
1949                                 continue;
1950                         if (inet6_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1951                                               cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
1952                                 read_unlock_bh(&addrconf_hash_lock);
1953                                 goto done;
1954                         }
1955                 }
1956                 read_unlock_bh(&addrconf_hash_lock);
1957         }
1958 done:
1959         cb->args[0] = idx;
1960         cb->args[1] = ip_idx;
1961
1962         return skb->len;
1963 }
1964
1965 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
1966 {
1967         struct sk_buff *skb;
1968         int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
1969
1970         skb = alloc_skb(size, GFP_ATOMIC);
1971         if (!skb) {
1972                 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
1973                 return;
1974         }
1975         if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1976                 kfree_skb(skb);
1977                 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
1978                 return;
1979         }
1980         NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
1981         netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
1982 }
1983
1984 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
1985                                 __s32 *array, int bytes)
1986 {
1987         memset(array, 0, bytes);
1988         array[DEVCONF_FORWARDING] = cnf->forwarding;
1989         array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
1990         array[DEVCONF_MTU6] = cnf->mtu6;
1991         array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
1992         array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
1993         array[DEVCONF_AUTOCONF] = cnf->autoconf;
1994         array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
1995         array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
1996         array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
1997         array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
1998 #ifdef CONFIG_IPV6_PRIVACY
1999         array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2000         array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2001         array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2002         array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2003         array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2004 #endif
2005 }
2006
2007 static int inet6_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
2008                             struct inet6_dev *idev,
2009                             int type, u32 pid, u32 seq)
2010 {
2011         __s32                   *array = NULL;
2012         struct ifinfomsg        *r;
2013         struct nlmsghdr         *nlh;
2014         unsigned char           *b = skb->tail;
2015         struct rtattr           *subattr;
2016
2017         nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
2018         if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2019         r = NLMSG_DATA(nlh);
2020         r->ifi_family = AF_INET6;
2021         r->ifi_type = dev->type;
2022         r->ifi_index = dev->ifindex;
2023         r->ifi_flags = dev->flags;
2024         r->ifi_change = 0;
2025         if (!netif_running(dev) || !netif_carrier_ok(dev))
2026                 r->ifi_flags &= ~IFF_RUNNING;
2027         else
2028                 r->ifi_flags |= IFF_RUNNING;
2029
2030         RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2031
2032         subattr = (struct rtattr*)skb->tail;
2033
2034         RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2035
2036         /* return the device flags */
2037         RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2038
2039         /* return the device sysctl params */
2040         if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2041                 goto rtattr_failure;
2042         ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2043         RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2044
2045         /* XXX - Statistics/MC not implemented */
2046         subattr->rta_len = skb->tail - (u8*)subattr;
2047
2048         nlh->nlmsg_len = skb->tail - b;
2049         kfree(array);
2050         return skb->len;
2051
2052 nlmsg_failure:
2053 rtattr_failure:
2054         if (array)
2055                 kfree(array);
2056         skb_trim(skb, b - skb->data);
2057         return -1;
2058 }
2059
2060 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2061 {
2062         int idx, err;
2063         int s_idx = cb->args[0];
2064         struct net_device *dev;
2065         struct inet6_dev *idev;
2066
2067         read_lock(&dev_base_lock);
2068         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2069                 if (idx < s_idx)
2070                         continue;
2071                 if ((idev = in6_dev_get(dev)) == NULL)
2072                         continue;
2073                 err = inet6_fill_ifinfo(skb, dev, idev, RTM_NEWLINK,
2074                                 NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq);
2075                 in6_dev_put(idev);
2076                 if (err <= 0)
2077                         break;
2078         }
2079         read_unlock(&dev_base_lock);
2080         cb->args[0] = idx;
2081
2082         return skb->len;
2083 }
2084
2085 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
2086 {
2087         { NULL,                 NULL,                   },
2088         { NULL,                 NULL,                   },
2089         { NULL,                 inet6_dump_ifinfo,      },
2090         { NULL,                 NULL,                   },
2091
2092         { inet6_rtm_newaddr,    NULL,                   },
2093         { inet6_rtm_deladdr,    NULL,                   },
2094         { NULL,                 inet6_dump_ifaddr,      },
2095         { NULL,                 NULL,                   },
2096
2097         { inet6_rtm_newroute,   NULL,                   },
2098         { inet6_rtm_delroute,   NULL,                   },
2099         { inet6_rtm_getroute,   inet6_dump_fib,         },
2100         { NULL,                 NULL,                   },
2101 };
2102
2103 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
2104 {
2105         inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
2106
2107         switch (event) {
2108         case RTM_NEWADDR:
2109                 ip6_rt_addr_add(&ifp->addr, ifp->idev->dev);
2110                 break;
2111         case RTM_DELADDR:
2112                 addrconf_leave_solict(ifp->idev->dev, &ifp->addr);
2113                 if (ifp->idev->cnf.forwarding) {
2114                         struct in6_addr addr;
2115
2116                         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2117                         if (!ipv6_addr_any(&addr))
2118                                 ipv6_dev_ac_dec(ifp->idev->dev, &addr);
2119                 }
2120                 if (!ipv6_chk_addr(&ifp->addr, NULL))
2121                         ip6_rt_addr_del(&ifp->addr, ifp->idev->dev);
2122                 break;
2123         }
2124 }
2125
2126 #ifdef CONFIG_SYSCTL
2127
2128 static
2129 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
2130                            void *buffer, size_t *lenp)
2131 {
2132         int *valp = ctl->data;
2133         int val = *valp;
2134         int ret;
2135
2136         ret = proc_dointvec(ctl, write, filp, buffer, lenp);
2137
2138         if (write && *valp != val && valp != &ipv6_devconf_dflt.forwarding) {
2139                 struct inet6_dev *idev = NULL;
2140
2141                 if (valp != &ipv6_devconf.forwarding) {
2142                         idev = (struct inet6_dev *)ctl->extra1;
2143                         if (idev == NULL)
2144                                 return ret;
2145                 } else
2146                         ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
2147
2148                 addrconf_forward_change(idev);
2149
2150                 if (*valp)
2151                         rt6_purge_dflt_routers(0);
2152         }
2153
2154         return ret;
2155 }
2156
2157 static struct addrconf_sysctl_table
2158 {
2159         struct ctl_table_header *sysctl_header;
2160         ctl_table addrconf_vars[11];
2161         ctl_table addrconf_dev[2];
2162         ctl_table addrconf_conf_dir[2];
2163         ctl_table addrconf_proto_dir[2];
2164         ctl_table addrconf_root_dir[2];
2165 } addrconf_sysctl = {
2166         NULL,
2167         {{NET_IPV6_FORWARDING, "forwarding",
2168          &ipv6_devconf.forwarding, sizeof(int), 0644, NULL,
2169          &addrconf_sysctl_forward},
2170
2171         {NET_IPV6_HOP_LIMIT, "hop_limit",
2172          &ipv6_devconf.hop_limit, sizeof(int), 0644, NULL,
2173          &proc_dointvec},
2174
2175         {NET_IPV6_MTU, "mtu",
2176          &ipv6_devconf.mtu6, sizeof(int), 0644, NULL,
2177          &proc_dointvec},
2178
2179         {NET_IPV6_ACCEPT_RA, "accept_ra",
2180          &ipv6_devconf.accept_ra, sizeof(int), 0644, NULL,
2181          &proc_dointvec},
2182
2183         {NET_IPV6_ACCEPT_REDIRECTS, "accept_redirects",
2184          &ipv6_devconf.accept_redirects, sizeof(int), 0644, NULL,
2185          &proc_dointvec},
2186
2187         {NET_IPV6_AUTOCONF, "autoconf",
2188          &ipv6_devconf.autoconf, sizeof(int), 0644, NULL,
2189          &proc_dointvec},
2190
2191         {NET_IPV6_DAD_TRANSMITS, "dad_transmits",
2192          &ipv6_devconf.dad_transmits, sizeof(int), 0644, NULL,
2193          &proc_dointvec},
2194
2195         {NET_IPV6_RTR_SOLICITS, "router_solicitations",
2196          &ipv6_devconf.rtr_solicits, sizeof(int), 0644, NULL,
2197          &proc_dointvec},
2198
2199         {NET_IPV6_RTR_SOLICIT_INTERVAL, "router_solicitation_interval",
2200          &ipv6_devconf.rtr_solicit_interval, sizeof(int), 0644, NULL,
2201          &proc_dointvec_jiffies},
2202
2203         {NET_IPV6_RTR_SOLICIT_DELAY, "router_solicitation_delay",
2204          &ipv6_devconf.rtr_solicit_delay, sizeof(int), 0644, NULL,
2205          &proc_dointvec_jiffies},
2206
2207         {0}},
2208
2209         {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, addrconf_sysctl.addrconf_vars},{0}},
2210         {{NET_IPV6_CONF, "conf", NULL, 0, 0555, addrconf_sysctl.addrconf_dev},{0}},
2211         {{NET_IPV6, "ipv6", NULL, 0, 0555, addrconf_sysctl.addrconf_conf_dir},{0}},
2212         {{CTL_NET, "net", NULL, 0, 0555, addrconf_sysctl.addrconf_proto_dir},{0}}
2213 };
2214
2215 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
2216 {
2217         int i;
2218         struct net_device *dev = idev ? idev->dev : NULL;
2219         struct addrconf_sysctl_table *t;
2220
2221         t = kmalloc(sizeof(*t), GFP_KERNEL);
2222         if (t == NULL)
2223                 return;
2224         memcpy(t, &addrconf_sysctl, sizeof(*t));
2225         for (i=0; i<sizeof(t->addrconf_vars)/sizeof(t->addrconf_vars[0])-1; i++) {
2226                 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
2227                 t->addrconf_vars[i].de = NULL;
2228                 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
2229         }
2230         if (dev) {
2231                 t->addrconf_dev[0].procname = dev->name;
2232                 t->addrconf_dev[0].ctl_name = dev->ifindex;
2233         } else {
2234                 t->addrconf_dev[0].procname = "default";
2235                 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
2236         }
2237         t->addrconf_dev[0].child = t->addrconf_vars;
2238         t->addrconf_dev[0].de = NULL;
2239         t->addrconf_conf_dir[0].child = t->addrconf_dev;
2240         t->addrconf_conf_dir[0].de = NULL;
2241         t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
2242         t->addrconf_proto_dir[0].de = NULL;
2243         t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
2244         t->addrconf_root_dir[0].de = NULL;
2245
2246         t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
2247         if (t->sysctl_header == NULL)
2248                 kfree(t);
2249         else
2250                 p->sysctl = t;
2251 }
2252
2253 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
2254 {
2255         if (p->sysctl) {
2256                 struct addrconf_sysctl_table *t = p->sysctl;
2257                 p->sysctl = NULL;
2258                 unregister_sysctl_table(t->sysctl_header);
2259                 kfree(t);
2260         }
2261 }
2262
2263
2264 #endif
2265
2266 /*
2267  *      Device notifier
2268  */
2269
2270 int register_inet6addr_notifier(struct notifier_block *nb)
2271 {
2272         return notifier_chain_register(&inet6addr_chain, nb);
2273 }
2274
2275 int unregister_inet6addr_notifier(struct notifier_block *nb)
2276 {
2277         return notifier_chain_unregister(&inet6addr_chain,nb);
2278 }
2279
2280 /*
2281  *      Init / cleanup code
2282  */
2283
2284 void __init addrconf_init(void)
2285 {
2286 #ifdef MODULE
2287         struct net_device *dev;
2288
2289         /* This takes sense only during module load. */
2290         rtnl_lock();
2291         for (dev = dev_base; dev; dev = dev->next) {
2292                 if (!(dev->flags&IFF_UP))
2293                         continue;
2294
2295                 switch (dev->type) {
2296                 case ARPHRD_LOOPBACK:   
2297                         init_loopback(dev);
2298                         break;
2299                 case ARPHRD_ETHER:
2300                 case ARPHRD_FDDI:
2301                 case ARPHRD_IEEE802_TR: 
2302                 case ARPHRD_ARCNET:
2303                         addrconf_dev_config(dev);
2304                         break;
2305                 default:;
2306                         /* Ignore all other */
2307                 }
2308         }
2309         rtnl_unlock();
2310 #endif
2311
2312 #ifdef CONFIG_PROC_FS
2313         proc_net_create("if_inet6", 0, iface_proc_info);
2314 #endif
2315         
2316         addrconf_verify(0);
2317         rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
2318 #ifdef CONFIG_SYSCTL
2319         addrconf_sysctl.sysctl_header =
2320                 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
2321         addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
2322 #endif
2323 }
2324
2325 #ifdef MODULE
2326 void addrconf_cleanup(void)
2327 {
2328         struct net_device *dev;
2329         struct inet6_dev *idev;
2330         struct inet6_ifaddr *ifa;
2331         int i;
2332
2333         rtnetlink_links[PF_INET6] = NULL;
2334 #ifdef CONFIG_SYSCTL
2335         addrconf_sysctl_unregister(&ipv6_devconf_dflt);
2336         addrconf_sysctl_unregister(&ipv6_devconf);
2337 #endif
2338
2339         rtnl_lock();
2340
2341         /*
2342          *      clean dev list.
2343          */
2344
2345         for (dev=dev_base; dev; dev=dev->next) {
2346                 if ((idev = __in6_dev_get(dev)) == NULL)
2347                         continue;
2348                 addrconf_ifdown(dev, 1);
2349         }
2350
2351         /*
2352          *      Check hash table.
2353          */
2354
2355         write_lock_bh(&addrconf_hash_lock);
2356         for (i=0; i < IN6_ADDR_HSIZE; i++) {
2357                 for (ifa=inet6_addr_lst[i]; ifa; ) {
2358                         struct inet6_ifaddr *bifa;
2359
2360                         bifa = ifa;
2361                         ifa = ifa->lst_next;
2362                         printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
2363                         /* Do not free it; something is wrong.
2364                            Now we can investigate it with debugger.
2365                          */
2366                 }
2367         }
2368         write_unlock_bh(&addrconf_hash_lock);
2369
2370         del_timer(&addr_chk_timer);
2371
2372         rtnl_unlock();
2373
2374 #ifdef CONFIG_PROC_FS
2375         proc_net_remove("if_inet6");
2376 #endif
2377 }
2378 #endif  /* MODULE */