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