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