- Linux 3.0.96.
[opensuse:kernel.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  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  */
14
15 /*
16  *      Changes:
17  *
18  *      Janos Farkas                    :       delete timer on ifdown
19  *      <chexum@bankinf.banki.hu>
20  *      Andi Kleen                      :       kill double kfree on module
21  *                                              unload.
22  *      Maciej W. Rozycki               :       FDDI support
23  *      sekiya@USAGI                    :       Don't send too many RS
24  *                                              packets.
25  *      yoshfuji@USAGI                  :       Fixed interval between DAD
26  *                                              packets.
27  *      YOSHIFUJI Hideaki @USAGI        :       improved accuracy of
28  *                                              address validation timer.
29  *      YOSHIFUJI Hideaki @USAGI        :       Privacy Extensions (RFC3041)
30  *                                              support.
31  *      Yuji SEKIYA @USAGI              :       Don't assign a same IPv6
32  *                                              address on a same interface.
33  *      YOSHIFUJI Hideaki @USAGI        :       ARCnet support
34  *      YOSHIFUJI Hideaki @USAGI        :       convert /proc/net/if_inet6 to
35  *                                              seq_file.
36  *      YOSHIFUJI Hideaki @USAGI        :       improved source address
37  *                                              selection; consider scope,
38  *                                              status etc.
39  */
40
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/socket.h>
45 #include <linux/sockios.h>
46 #include <linux/net.h>
47 #include <linux/in6.h>
48 #include <linux/netdevice.h>
49 #include <linux/if_addr.h>
50 #include <linux/if_arp.h>
51 #include <linux/if_arcnet.h>
52 #include <linux/if_infiniband.h>
53 #include <linux/route.h>
54 #include <linux/inetdevice.h>
55 #include <linux/init.h>
56 #include <linux/slab.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/net_namespace.h>
66 #include <net/sock.h>
67 #include <net/snmp.h>
68
69 #include <net/ipv6.h>
70 #include <net/protocol.h>
71 #include <net/ndisc.h>
72 #include <net/ip6_route.h>
73 #include <net/addrconf.h>
74 #include <net/tcp.h>
75 #include <net/ip.h>
76 #include <net/netlink.h>
77 #include <net/pkt_sched.h>
78 #include <linux/if_tunnel.h>
79 #include <linux/rtnetlink.h>
80
81 #ifdef CONFIG_IPV6_PRIVACY
82 #include <linux/random.h>
83 #endif
84
85 #include <linux/uaccess.h>
86 #include <asm/unaligned.h>
87
88 #include <linux/proc_fs.h>
89 #include <linux/seq_file.h>
90 #include "ipv6_noinit.h"
91
92 /* Set to 3 to get tracing... */
93 #define ACONF_DEBUG 2
94
95 #if ACONF_DEBUG >= 3
96 #define ADBG(x) printk x
97 #else
98 #define ADBG(x)
99 #endif
100
101 #define INFINITY_LIFE_TIME      0xFFFFFFFF
102
103 static inline u32 cstamp_delta(unsigned long cstamp)
104 {
105         return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
106 }
107
108 #define ADDRCONF_TIMER_FUZZ_MINUS       (HZ > 50 ? HZ/50 : 1)
109 #define ADDRCONF_TIMER_FUZZ             (HZ / 4)
110 #define ADDRCONF_TIMER_FUZZ_MAX         (HZ)
111
112 #ifdef CONFIG_SYSCTL
113 static void addrconf_sysctl_register(struct inet6_dev *idev);
114 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
115 #else
116 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
117 {
118 }
119
120 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
121 {
122 }
123 #endif
124
125 #ifdef CONFIG_IPV6_PRIVACY
126 static int __ipv6_regen_rndid(struct inet6_dev *idev);
127 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
128 static void ipv6_regen_rndid(unsigned long data);
129 #endif
130
131 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
132 static int ipv6_count_addresses(struct inet6_dev *idev);
133
134 /*
135  *      Configured unicast address hash table
136  */
137 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
138 static DEFINE_SPINLOCK(addrconf_hash_lock);
139
140 static void addrconf_verify(unsigned long);
141
142 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
143 static DEFINE_SPINLOCK(addrconf_verify_lock);
144
145 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
146 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
147
148 static void addrconf_type_change(struct net_device *dev,
149                                  unsigned long event);
150 static int addrconf_ifdown(struct net_device *dev, int how);
151
152 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
153 static void addrconf_dad_timer(unsigned long data);
154 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
155 static void addrconf_dad_run(struct inet6_dev *idev);
156 static void addrconf_rs_timer(unsigned long data);
157 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
158 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
159
160 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
161                                 struct prefix_info *pinfo);
162 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
163                                struct net_device *dev);
164
165 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
166
167 static struct ipv6_devconf ipv6_devconf __read_mostly = {
168         .forwarding             = 0,
169         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
170         .mtu6                   = IPV6_MIN_MTU,
171         .accept_ra              = 1,
172         .accept_redirects       = 1,
173         .autoconf               = 1,
174         .force_mld_version      = 0,
175         .dad_transmits          = 1,
176         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
177         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
178         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
179 #ifdef CONFIG_IPV6_PRIVACY
180         .use_tempaddr           = 0,
181         .temp_valid_lft         = TEMP_VALID_LIFETIME,
182         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
183         .regen_max_retry        = REGEN_MAX_RETRY,
184         .max_desync_factor      = MAX_DESYNC_FACTOR,
185 #endif
186         .max_addresses          = IPV6_MAX_ADDRESSES,
187         .accept_ra_defrtr       = 1,
188         .accept_ra_pinfo        = 1,
189 #ifdef CONFIG_IPV6_ROUTER_PREF
190         .accept_ra_rtr_pref     = 1,
191         .rtr_probe_interval     = 60 * HZ,
192 #ifdef CONFIG_IPV6_ROUTE_INFO
193         .accept_ra_rt_info_max_plen = 0,
194 #endif
195 #endif
196         .proxy_ndp              = 0,
197         .accept_source_route    = 0,    /* we do not accept RH0 by default. */
198         .disable_ipv6           = 0,
199         .accept_dad             = 1,
200 };
201
202 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
203         .forwarding             = 0,
204         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
205         .mtu6                   = IPV6_MIN_MTU,
206         .accept_ra              = 1,
207         .accept_redirects       = 1,
208         .autoconf               = 1,
209         .dad_transmits          = 1,
210         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
211         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
212         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
213 #ifdef CONFIG_IPV6_PRIVACY
214         .use_tempaddr           = 0,
215         .temp_valid_lft         = TEMP_VALID_LIFETIME,
216         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
217         .regen_max_retry        = REGEN_MAX_RETRY,
218         .max_desync_factor      = MAX_DESYNC_FACTOR,
219 #endif
220         .max_addresses          = IPV6_MAX_ADDRESSES,
221         .accept_ra_defrtr       = 1,
222         .accept_ra_pinfo        = 1,
223 #ifdef CONFIG_IPV6_ROUTER_PREF
224         .accept_ra_rtr_pref     = 1,
225         .rtr_probe_interval     = 60 * HZ,
226 #ifdef CONFIG_IPV6_ROUTE_INFO
227         .accept_ra_rt_info_max_plen = 0,
228 #endif
229 #endif
230         .proxy_ndp              = 0,
231         .accept_source_route    = 0,    /* we do not accept RH0 by default. */
232         .disable_ipv6           = 0,
233         .accept_dad             = 1,
234 };
235
236 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
237 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
238 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
239 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
240 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
241
242 /* Check if a valid qdisc is available */
243 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
244 {
245         return !qdisc_tx_is_noop(dev);
246 }
247
248 /* Check if a route is valid prefix route */
249 static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
250 {
251         return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0;
252 }
253
254 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
255 {
256         if (del_timer(&ifp->timer))
257                 __in6_ifa_put(ifp);
258 }
259
260 enum addrconf_timer_t {
261         AC_NONE,
262         AC_DAD,
263         AC_RS,
264 };
265
266 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
267                                enum addrconf_timer_t what,
268                                unsigned long when)
269 {
270         if (!del_timer(&ifp->timer))
271                 in6_ifa_hold(ifp);
272
273         switch (what) {
274         case AC_DAD:
275                 ifp->timer.function = addrconf_dad_timer;
276                 break;
277         case AC_RS:
278                 ifp->timer.function = addrconf_rs_timer;
279                 break;
280         default:
281                 break;
282         }
283         ifp->timer.expires = jiffies + when;
284         add_timer(&ifp->timer);
285 }
286
287 static int snmp6_alloc_dev(struct inet6_dev *idev)
288 {
289         if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
290                           sizeof(struct ipstats_mib),
291                           __alignof__(struct ipstats_mib)) < 0)
292                 goto err_ip;
293         idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
294                                         GFP_KERNEL);
295         if (!idev->stats.icmpv6dev)
296                 goto err_icmp;
297         idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
298                                            GFP_KERNEL);
299         if (!idev->stats.icmpv6msgdev)
300                 goto err_icmpmsg;
301
302         return 0;
303
304 err_icmpmsg:
305         kfree(idev->stats.icmpv6dev);
306 err_icmp:
307         snmp_mib_free((void __percpu **)idev->stats.ipv6);
308 err_ip:
309         return -ENOMEM;
310 }
311
312 static void snmp6_free_dev(struct inet6_dev *idev)
313 {
314         kfree(idev->stats.icmpv6msgdev);
315         kfree(idev->stats.icmpv6dev);
316         snmp_mib_free((void __percpu **)idev->stats.ipv6);
317 }
318
319 /* Nobody refers to this device, we may destroy it. */
320
321 void in6_dev_finish_destroy(struct inet6_dev *idev)
322 {
323         struct net_device *dev = idev->dev;
324
325         WARN_ON(!list_empty(&idev->addr_list));
326         WARN_ON(idev->mc_list != NULL);
327
328 #ifdef NET_REFCNT_DEBUG
329         printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
330 #endif
331         dev_put(dev);
332         if (!idev->dead) {
333                 pr_warning("Freeing alive inet6 device %p\n", idev);
334                 return;
335         }
336         snmp6_free_dev(idev);
337         kfree_rcu(idev, rcu);
338 }
339
340 EXPORT_SYMBOL(in6_dev_finish_destroy);
341
342 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
343 {
344         struct inet6_dev *ndev;
345
346         ASSERT_RTNL();
347
348         if (dev->mtu < IPV6_MIN_MTU)
349                 return NULL;
350
351         ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
352
353         if (ndev == NULL)
354                 return NULL;
355
356         rwlock_init(&ndev->lock);
357         ndev->dev = dev;
358         INIT_LIST_HEAD(&ndev->addr_list);
359
360         memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
361         ndev->cnf.mtu6 = dev->mtu;
362         ndev->cnf.sysctl = NULL;
363         ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
364         if (ndev->nd_parms == NULL) {
365                 kfree(ndev);
366                 return NULL;
367         }
368         if (ndev->cnf.forwarding)
369                 dev_disable_lro(dev);
370         /* We refer to the device */
371         dev_hold(dev);
372
373         if (snmp6_alloc_dev(ndev) < 0) {
374                 ADBG((KERN_WARNING
375                         "%s(): cannot allocate memory for statistics; dev=%s.\n",
376                         __func__, dev->name));
377                 neigh_parms_release(&nd_tbl, ndev->nd_parms);
378                 ndev->dead = 1;
379                 in6_dev_finish_destroy(ndev);
380                 return NULL;
381         }
382
383         if (snmp6_register_dev(ndev) < 0) {
384                 ADBG((KERN_WARNING
385                         "%s(): cannot create /proc/net/dev_snmp6/%s\n",
386                         __func__, dev->name));
387                 neigh_parms_release(&nd_tbl, ndev->nd_parms);
388                 ndev->dead = 1;
389                 in6_dev_finish_destroy(ndev);
390                 return NULL;
391         }
392
393         /* One reference from device.  We must do this before
394          * we invoke __ipv6_regen_rndid().
395          */
396         in6_dev_hold(ndev);
397
398         if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
399                 ndev->cnf.accept_dad = -1;
400
401 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
402         if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
403                 printk(KERN_INFO
404                        "%s: Disabled Multicast RS\n",
405                        dev->name);
406                 ndev->cnf.rtr_solicits = 0;
407         }
408 #endif
409
410 #ifdef CONFIG_IPV6_PRIVACY
411         INIT_LIST_HEAD(&ndev->tempaddr_list);
412         setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
413         if ((dev->flags&IFF_LOOPBACK) ||
414             dev->type == ARPHRD_TUNNEL ||
415             dev->type == ARPHRD_TUNNEL6 ||
416             dev->type == ARPHRD_SIT ||
417             dev->type == ARPHRD_NONE) {
418                 ndev->cnf.use_tempaddr = -1;
419         } else {
420                 in6_dev_hold(ndev);
421                 ipv6_regen_rndid((unsigned long) ndev);
422         }
423 #endif
424
425         if (netif_running(dev) && addrconf_qdisc_ok(dev))
426                 ndev->if_flags |= IF_READY;
427
428         ipv6_mc_init_dev(ndev);
429         ndev->tstamp = jiffies;
430         addrconf_sysctl_register(ndev);
431         /* protected by rtnl_lock */
432         rcu_assign_pointer(dev->ip6_ptr, ndev);
433
434         /* Join all-node multicast group */
435         ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
436
437         /* Join all-router multicast group if forwarding is set */
438         if (ndev->cnf.forwarding && dev && (dev->flags & IFF_MULTICAST))
439                 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
440
441         return ndev;
442 }
443
444 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
445 {
446         struct inet6_dev *idev;
447
448         ASSERT_RTNL();
449
450         idev = __in6_dev_get(dev);
451         if (!idev) {
452                 idev = ipv6_add_dev(dev);
453                 if (!idev)
454                         return NULL;
455         }
456
457         if (dev->flags&IFF_UP)
458                 ipv6_mc_up(idev);
459         return idev;
460 }
461
462 #ifdef CONFIG_SYSCTL
463 static void dev_forward_change(struct inet6_dev *idev)
464 {
465         struct net_device *dev;
466         struct inet6_ifaddr *ifa;
467
468         if (!idev)
469                 return;
470         dev = idev->dev;
471         if (idev->cnf.forwarding)
472                 dev_disable_lro(dev);
473         if (dev && (dev->flags & IFF_MULTICAST)) {
474                 if (idev->cnf.forwarding)
475                         ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
476                 else
477                         ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
478         }
479
480         list_for_each_entry(ifa, &idev->addr_list, if_list) {
481                 if (ifa->flags&IFA_F_TENTATIVE)
482                         continue;
483                 if (idev->cnf.forwarding)
484                         addrconf_join_anycast(ifa);
485                 else
486                         addrconf_leave_anycast(ifa);
487         }
488 }
489
490
491 static void addrconf_forward_change(struct net *net, __s32 newf)
492 {
493         struct net_device *dev;
494         struct inet6_dev *idev;
495
496         for_each_netdev(net, dev) {
497                 idev = __in6_dev_get(dev);
498                 if (idev) {
499                         int changed = (!idev->cnf.forwarding) ^ (!newf);
500                         idev->cnf.forwarding = newf;
501                         if (changed)
502                                 dev_forward_change(idev);
503                 }
504         }
505 }
506
507 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
508 {
509         struct net *net;
510
511         net = (struct net *)table->extra2;
512         if (p == &net->ipv6.devconf_dflt->forwarding)
513                 return 0;
514
515         if (!rtnl_trylock()) {
516                 /* Restore the original values before restarting */
517                 *p = old;
518                 return restart_syscall();
519         }
520
521         if (p == &net->ipv6.devconf_all->forwarding) {
522                 __s32 newf = net->ipv6.devconf_all->forwarding;
523                 net->ipv6.devconf_dflt->forwarding = newf;
524                 addrconf_forward_change(net, newf);
525         } else if ((!*p) ^ (!old))
526                 dev_forward_change((struct inet6_dev *)table->extra1);
527         rtnl_unlock();
528
529         if (*p)
530                 rt6_purge_dflt_routers(net);
531         return 1;
532 }
533 #endif
534
535 /* Nobody refers to this ifaddr, destroy it */
536 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
537 {
538         WARN_ON(!hlist_unhashed(&ifp->addr_lst));
539
540 #ifdef NET_REFCNT_DEBUG
541         printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
542 #endif
543
544         in6_dev_put(ifp->idev);
545
546         if (del_timer(&ifp->timer))
547                 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
548
549         if (ifp->state != INET6_IFADDR_STATE_DEAD) {
550                 pr_warning("Freeing alive inet6 address %p\n", ifp);
551                 return;
552         }
553         dst_release(&ifp->rt->dst);
554
555         kfree_rcu(ifp, rcu);
556 }
557
558 static void
559 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
560 {
561         struct list_head *p;
562         int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
563
564         /*
565          * Each device address list is sorted in order of scope -
566          * global before linklocal.
567          */
568         list_for_each(p, &idev->addr_list) {
569                 struct inet6_ifaddr *ifa
570                         = list_entry(p, struct inet6_ifaddr, if_list);
571                 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
572                         break;
573         }
574
575         list_add_tail(&ifp->if_list, p);
576 }
577
578 static u32 ipv6_addr_hash(const struct in6_addr *addr)
579 {
580         /*
581          * We perform the hash function over the last 64 bits of the address
582          * This will include the IEEE address token on links that support it.
583          */
584         return jhash_2words((__force u32)addr->s6_addr32[2],
585                             (__force u32)addr->s6_addr32[3], 0)
586                 & (IN6_ADDR_HSIZE - 1);
587 }
588
589 /* On success it returns ifp with increased reference count */
590
591 static struct inet6_ifaddr *
592 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
593               int scope, u32 flags)
594 {
595         struct inet6_ifaddr *ifa = NULL;
596         struct rt6_info *rt;
597         unsigned int hash;
598         int err = 0;
599         int addr_type = ipv6_addr_type(addr);
600
601         if (addr_type == IPV6_ADDR_ANY ||
602             addr_type & IPV6_ADDR_MULTICAST ||
603             (!(idev->dev->flags & IFF_LOOPBACK) &&
604              addr_type & IPV6_ADDR_LOOPBACK))
605                 return ERR_PTR(-EADDRNOTAVAIL);
606
607         rcu_read_lock_bh();
608         if (idev->dead) {
609                 err = -ENODEV;                  /*XXX*/
610                 goto out2;
611         }
612
613         if (idev->cnf.disable_ipv6) {
614                 err = -EACCES;
615                 goto out2;
616         }
617
618         spin_lock(&addrconf_hash_lock);
619
620         /* Ignore adding duplicate addresses on an interface */
621         if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
622                 ADBG(("ipv6_add_addr: already assigned\n"));
623                 err = -EEXIST;
624                 goto out;
625         }
626
627         ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
628
629         if (ifa == NULL) {
630                 ADBG(("ipv6_add_addr: malloc failed\n"));
631                 err = -ENOBUFS;
632                 goto out;
633         }
634
635         rt = addrconf_dst_alloc(idev, addr, 0);
636         if (IS_ERR(rt)) {
637                 err = PTR_ERR(rt);
638                 goto out;
639         }
640
641         ipv6_addr_copy(&ifa->addr, addr);
642
643         spin_lock_init(&ifa->lock);
644         spin_lock_init(&ifa->state_lock);
645         init_timer(&ifa->timer);
646         INIT_HLIST_NODE(&ifa->addr_lst);
647         ifa->timer.data = (unsigned long) ifa;
648         ifa->scope = scope;
649         ifa->prefix_len = pfxlen;
650         ifa->flags = flags | IFA_F_TENTATIVE;
651         ifa->cstamp = ifa->tstamp = jiffies;
652
653         ifa->rt = rt;
654
655         /*
656          * part one of RFC 4429, section 3.3
657          * We should not configure an address as
658          * optimistic if we do not yet know the link
659          * layer address of our nexhop router
660          */
661
662         if (rt->rt6i_nexthop == NULL)
663                 ifa->flags &= ~IFA_F_OPTIMISTIC;
664
665         ifa->idev = idev;
666         in6_dev_hold(idev);
667         /* For caller */
668         in6_ifa_hold(ifa);
669
670         /* Add to big hash table */
671         hash = ipv6_addr_hash(addr);
672
673         hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
674         spin_unlock(&addrconf_hash_lock);
675
676         write_lock(&idev->lock);
677         /* Add to inet6_dev unicast addr list. */
678         ipv6_link_dev_addr(idev, ifa);
679
680 #ifdef CONFIG_IPV6_PRIVACY
681         if (ifa->flags&IFA_F_TEMPORARY) {
682                 list_add(&ifa->tmp_list, &idev->tempaddr_list);
683                 in6_ifa_hold(ifa);
684         }
685 #endif
686
687         in6_ifa_hold(ifa);
688         write_unlock(&idev->lock);
689 out2:
690         rcu_read_unlock_bh();
691
692         if (likely(err == 0))
693                 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
694         else {
695                 kfree(ifa);
696                 ifa = ERR_PTR(err);
697         }
698
699         return ifa;
700 out:
701         spin_unlock(&addrconf_hash_lock);
702         goto out2;
703 }
704
705 /* This function wants to get referenced ifp and releases it before return */
706
707 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
708 {
709         struct inet6_ifaddr *ifa, *ifn;
710         struct inet6_dev *idev = ifp->idev;
711         int state;
712         int deleted = 0, onlink = 0;
713         unsigned long expires = jiffies;
714
715         spin_lock_bh(&ifp->state_lock);
716         state = ifp->state;
717         ifp->state = INET6_IFADDR_STATE_DEAD;
718         spin_unlock_bh(&ifp->state_lock);
719
720         if (state == INET6_IFADDR_STATE_DEAD)
721                 goto out;
722
723         spin_lock_bh(&addrconf_hash_lock);
724         hlist_del_init_rcu(&ifp->addr_lst);
725         spin_unlock_bh(&addrconf_hash_lock);
726
727         write_lock_bh(&idev->lock);
728 #ifdef CONFIG_IPV6_PRIVACY
729         if (ifp->flags&IFA_F_TEMPORARY) {
730                 list_del(&ifp->tmp_list);
731                 if (ifp->ifpub) {
732                         in6_ifa_put(ifp->ifpub);
733                         ifp->ifpub = NULL;
734                 }
735                 __in6_ifa_put(ifp);
736         }
737 #endif
738
739         list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
740                 if (ifa == ifp) {
741                         list_del_init(&ifp->if_list);
742                         __in6_ifa_put(ifp);
743
744                         if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
745                                 break;
746                         deleted = 1;
747                         continue;
748                 } else if (ifp->flags & IFA_F_PERMANENT) {
749                         if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
750                                               ifp->prefix_len)) {
751                                 if (ifa->flags & IFA_F_PERMANENT) {
752                                         onlink = 1;
753                                         if (deleted)
754                                                 break;
755                                 } else {
756                                         unsigned long lifetime;
757
758                                         if (!onlink)
759                                                 onlink = -1;
760
761                                         spin_lock(&ifa->lock);
762
763                                         lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
764                                         /*
765                                          * Note: Because this address is
766                                          * not permanent, lifetime <
767                                          * LONG_MAX / HZ here.
768                                          */
769                                         if (time_before(expires,
770                                                         ifa->tstamp + lifetime * HZ))
771                                                 expires = ifa->tstamp + lifetime * HZ;
772                                         spin_unlock(&ifa->lock);
773                                 }
774                         }
775                 }
776         }
777         write_unlock_bh(&idev->lock);
778
779         addrconf_del_timer(ifp);
780
781         ipv6_ifa_notify(RTM_DELADDR, ifp);
782
783         atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
784
785         /*
786          * Purge or update corresponding prefix
787          *
788          * 1) we don't purge prefix here if address was not permanent.
789          *    prefix is managed by its own lifetime.
790          * 2) if there're no addresses, delete prefix.
791          * 3) if there're still other permanent address(es),
792          *    corresponding prefix is still permanent.
793          * 4) otherwise, update prefix lifetime to the
794          *    longest valid lifetime among the corresponding
795          *    addresses on the device.
796          *    Note: subsequent RA will update lifetime.
797          *
798          * --yoshfuji
799          */
800         if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
801                 struct in6_addr prefix;
802                 struct rt6_info *rt;
803                 struct net *net = dev_net(ifp->idev->dev);
804                 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
805                 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
806
807                 if (rt && addrconf_is_prefix_route(rt)) {
808                         if (onlink == 0) {
809                                 ip6_del_rt(rt);
810                                 rt = NULL;
811                         } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
812                                 rt6_set_expires(rt, expires);
813                         }
814                 }
815                 dst_release(&rt->dst);
816         }
817
818         /* clean up prefsrc entries */
819         rt6_remove_prefsrc(ifp);
820 out:
821         in6_ifa_put(ifp);
822 }
823
824 #ifdef CONFIG_IPV6_PRIVACY
825 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
826 {
827         struct inet6_dev *idev = ifp->idev;
828         struct in6_addr addr, *tmpaddr;
829         unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp, age;
830         unsigned long regen_advance;
831         int tmp_plen;
832         int ret = 0;
833         int max_addresses;
834         u32 addr_flags;
835
836         write_lock(&idev->lock);
837         if (ift) {
838                 spin_lock_bh(&ift->lock);
839                 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
840                 spin_unlock_bh(&ift->lock);
841                 tmpaddr = &addr;
842         } else {
843                 tmpaddr = NULL;
844         }
845 retry:
846         in6_dev_hold(idev);
847         if (idev->cnf.use_tempaddr <= 0) {
848                 write_unlock(&idev->lock);
849                 printk(KERN_INFO
850                         "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
851                 in6_dev_put(idev);
852                 ret = -1;
853                 goto out;
854         }
855         spin_lock_bh(&ifp->lock);
856         if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
857                 idev->cnf.use_tempaddr = -1;    /*XXX*/
858                 spin_unlock_bh(&ifp->lock);
859                 write_unlock(&idev->lock);
860                 printk(KERN_WARNING
861                         "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
862                 in6_dev_put(idev);
863                 ret = -1;
864                 goto out;
865         }
866         in6_ifa_hold(ifp);
867         memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
868         if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
869                 spin_unlock_bh(&ifp->lock);
870                 write_unlock(&idev->lock);
871                 printk(KERN_WARNING
872                         "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
873                 in6_ifa_put(ifp);
874                 in6_dev_put(idev);
875                 ret = -1;
876                 goto out;
877         }
878         memcpy(&addr.s6_addr[8], idev->rndid, 8);
879         age = (jiffies - ifp->tstamp) / HZ;
880         tmp_valid_lft = min_t(__u32,
881                               ifp->valid_lft,
882                               idev->cnf.temp_valid_lft + age);
883         tmp_prefered_lft = min_t(__u32,
884                                  ifp->prefered_lft,
885                                  idev->cnf.temp_prefered_lft + age -
886                                  idev->cnf.max_desync_factor);
887         tmp_plen = ifp->prefix_len;
888         max_addresses = idev->cnf.max_addresses;
889         tmp_cstamp = ifp->cstamp;
890         tmp_tstamp = ifp->tstamp;
891         spin_unlock_bh(&ifp->lock);
892
893         regen_advance = idev->cnf.regen_max_retry *
894                         idev->cnf.dad_transmits *
895                         idev->nd_parms->retrans_time / HZ;
896         write_unlock(&idev->lock);
897
898         /* A temporary address is created only if this calculated Preferred
899          * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
900          * an implementation must not create a temporary address with a zero
901          * Preferred Lifetime.
902          */
903         if (tmp_prefered_lft <= regen_advance) {
904                 in6_ifa_put(ifp);
905                 in6_dev_put(idev);
906                 ret = -1;
907                 goto out;
908         }
909
910         addr_flags = IFA_F_TEMPORARY;
911         /* set in addrconf_prefix_rcv() */
912         if (ifp->flags & IFA_F_OPTIMISTIC)
913                 addr_flags |= IFA_F_OPTIMISTIC;
914
915         ift = ipv6_add_addr(idev, &addr, tmp_plen,
916                             ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
917                             addr_flags);
918         if (IS_ERR(ift)) {
919                 in6_ifa_put(ifp);
920                 in6_dev_put(idev);
921                 printk(KERN_INFO
922                         "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
923                 tmpaddr = &addr;
924                 write_lock(&idev->lock);
925                 goto retry;
926         }
927
928         spin_lock_bh(&ift->lock);
929         ift->ifpub = ifp;
930         ift->valid_lft = tmp_valid_lft;
931         ift->prefered_lft = tmp_prefered_lft;
932         ift->cstamp = tmp_cstamp;
933         ift->tstamp = tmp_tstamp;
934         spin_unlock_bh(&ift->lock);
935
936         addrconf_dad_start(ift, 0);
937         in6_ifa_put(ift);
938         in6_dev_put(idev);
939 out:
940         return ret;
941 }
942 #endif
943
944 /*
945  *      Choose an appropriate source address (RFC3484)
946  */
947 enum {
948         IPV6_SADDR_RULE_INIT = 0,
949         IPV6_SADDR_RULE_LOCAL,
950         IPV6_SADDR_RULE_SCOPE,
951         IPV6_SADDR_RULE_PREFERRED,
952 #ifdef CONFIG_IPV6_MIP6
953         IPV6_SADDR_RULE_HOA,
954 #endif
955         IPV6_SADDR_RULE_OIF,
956         IPV6_SADDR_RULE_LABEL,
957 #ifdef CONFIG_IPV6_PRIVACY
958         IPV6_SADDR_RULE_PRIVACY,
959 #endif
960         IPV6_SADDR_RULE_ORCHID,
961         IPV6_SADDR_RULE_PREFIX,
962         IPV6_SADDR_RULE_MAX
963 };
964
965 struct ipv6_saddr_score {
966         int                     rule;
967         int                     addr_type;
968         struct inet6_ifaddr     *ifa;
969         DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
970         int                     scopedist;
971         int                     matchlen;
972 };
973
974 struct ipv6_saddr_dst {
975         const struct in6_addr *addr;
976         int ifindex;
977         int scope;
978         int label;
979         unsigned int prefs;
980 };
981
982 static inline int ipv6_saddr_preferred(int type)
983 {
984         if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
985                 return 1;
986         return 0;
987 }
988
989 static int ipv6_get_saddr_eval(struct net *net,
990                                struct ipv6_saddr_score *score,
991                                struct ipv6_saddr_dst *dst,
992                                int i)
993 {
994         int ret;
995
996         if (i <= score->rule) {
997                 switch (i) {
998                 case IPV6_SADDR_RULE_SCOPE:
999                         ret = score->scopedist;
1000                         break;
1001                 case IPV6_SADDR_RULE_PREFIX:
1002                         ret = score->matchlen;
1003                         break;
1004                 default:
1005                         ret = !!test_bit(i, score->scorebits);
1006                 }
1007                 goto out;
1008         }
1009
1010         switch (i) {
1011         case IPV6_SADDR_RULE_INIT:
1012                 /* Rule 0: remember if hiscore is not ready yet */
1013                 ret = !!score->ifa;
1014                 break;
1015         case IPV6_SADDR_RULE_LOCAL:
1016                 /* Rule 1: Prefer same address */
1017                 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1018                 break;
1019         case IPV6_SADDR_RULE_SCOPE:
1020                 /* Rule 2: Prefer appropriate scope
1021                  *
1022                  *      ret
1023                  *       ^
1024                  *    -1 |  d 15
1025                  *    ---+--+-+---> scope
1026                  *       |
1027                  *       |             d is scope of the destination.
1028                  *  B-d  |  \
1029                  *       |   \      <- smaller scope is better if
1030                  *  B-15 |    \        if scope is enough for destinaion.
1031                  *       |             ret = B - scope (-1 <= scope >= d <= 15).
1032                  * d-C-1 | /
1033                  *       |/         <- greater is better
1034                  *   -C  /             if scope is not enough for destination.
1035                  *      /|             ret = scope - C (-1 <= d < scope <= 15).
1036                  *
1037                  * d - C - 1 < B -15 (for all -1 <= d <= 15).
1038                  * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1039                  * Assume B = 0 and we get C > 29.
1040                  */
1041                 ret = __ipv6_addr_src_scope(score->addr_type);
1042                 if (ret >= dst->scope)
1043                         ret = -ret;
1044                 else
1045                         ret -= 128;     /* 30 is enough */
1046                 score->scopedist = ret;
1047                 break;
1048         case IPV6_SADDR_RULE_PREFERRED:
1049                 /* Rule 3: Avoid deprecated and optimistic addresses */
1050                 ret = ipv6_saddr_preferred(score->addr_type) ||
1051                       !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1052                 break;
1053 #ifdef CONFIG_IPV6_MIP6
1054         case IPV6_SADDR_RULE_HOA:
1055             {
1056                 /* Rule 4: Prefer home address */
1057                 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1058                 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1059                 break;
1060             }
1061 #endif
1062         case IPV6_SADDR_RULE_OIF:
1063                 /* Rule 5: Prefer outgoing interface */
1064                 ret = (!dst->ifindex ||
1065                        dst->ifindex == score->ifa->idev->dev->ifindex);
1066                 break;
1067         case IPV6_SADDR_RULE_LABEL:
1068                 /* Rule 6: Prefer matching label */
1069                 ret = ipv6_addr_label(net,
1070                                       &score->ifa->addr, score->addr_type,
1071                                       score->ifa->idev->dev->ifindex) == dst->label;
1072                 break;
1073 #ifdef CONFIG_IPV6_PRIVACY
1074         case IPV6_SADDR_RULE_PRIVACY:
1075             {
1076                 /* Rule 7: Prefer public address
1077                  * Note: prefer temporary address if use_tempaddr >= 2
1078                  */
1079                 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1080                                 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1081                                 score->ifa->idev->cnf.use_tempaddr >= 2;
1082                 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1083                 break;
1084             }
1085 #endif
1086         case IPV6_SADDR_RULE_ORCHID:
1087                 /* Rule 8-: Prefer ORCHID vs ORCHID or
1088                  *          non-ORCHID vs non-ORCHID
1089                  */
1090                 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1091                         ipv6_addr_orchid(dst->addr));
1092                 break;
1093         case IPV6_SADDR_RULE_PREFIX:
1094                 /* Rule 8: Use longest matching prefix */
1095                 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1096                                                        dst->addr);
1097                 break;
1098         default:
1099                 ret = 0;
1100         }
1101
1102         if (ret)
1103                 __set_bit(i, score->scorebits);
1104         score->rule = i;
1105 out:
1106         return ret;
1107 }
1108
1109 int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
1110                        const struct in6_addr *daddr, unsigned int prefs,
1111                        struct in6_addr *saddr)
1112 {
1113         struct ipv6_saddr_score scores[2],
1114                                 *score = &scores[0], *hiscore = &scores[1];
1115         struct ipv6_saddr_dst dst;
1116         struct net_device *dev;
1117         int dst_type;
1118
1119         dst_type = __ipv6_addr_type(daddr);
1120         dst.addr = daddr;
1121         dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1122         dst.scope = __ipv6_addr_src_scope(dst_type);
1123         dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1124         dst.prefs = prefs;
1125
1126         hiscore->rule = -1;
1127         hiscore->ifa = NULL;
1128
1129         rcu_read_lock();
1130
1131         for_each_netdev_rcu(net, dev) {
1132                 struct inet6_dev *idev;
1133
1134                 /* Candidate Source Address (section 4)
1135                  *  - multicast and link-local destination address,
1136                  *    the set of candidate source address MUST only
1137                  *    include addresses assigned to interfaces
1138                  *    belonging to the same link as the outgoing
1139                  *    interface.
1140                  * (- For site-local destination addresses, the
1141                  *    set of candidate source addresses MUST only
1142                  *    include addresses assigned to interfaces
1143                  *    belonging to the same site as the outgoing
1144                  *    interface.)
1145                  */
1146                 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1147                      dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1148                     dst.ifindex && dev->ifindex != dst.ifindex)
1149                         continue;
1150
1151                 idev = __in6_dev_get(dev);
1152                 if (!idev)
1153                         continue;
1154
1155                 read_lock_bh(&idev->lock);
1156                 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1157                         int i;
1158
1159                         /*
1160                          * - Tentative Address (RFC2462 section 5.4)
1161                          *  - A tentative address is not considered
1162                          *    "assigned to an interface" in the traditional
1163                          *    sense, unless it is also flagged as optimistic.
1164                          * - Candidate Source Address (section 4)
1165                          *  - In any case, anycast addresses, multicast
1166                          *    addresses, and the unspecified address MUST
1167                          *    NOT be included in a candidate set.
1168                          */
1169                         if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1170                             (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1171                                 continue;
1172
1173                         score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1174
1175                         if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1176                                      score->addr_type & IPV6_ADDR_MULTICAST)) {
1177                                 LIMIT_NETDEBUG(KERN_DEBUG
1178                                                "ADDRCONF: unspecified / multicast address "
1179                                                "assigned as unicast address on %s",
1180                                                dev->name);
1181                                 continue;
1182                         }
1183
1184                         score->rule = -1;
1185                         bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1186
1187                         for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1188                                 int minihiscore, miniscore;
1189
1190                                 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1191                                 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1192
1193                                 if (minihiscore > miniscore) {
1194                                         if (i == IPV6_SADDR_RULE_SCOPE &&
1195                                             score->scopedist > 0) {
1196                                                 /*
1197                                                  * special case:
1198                                                  * each remaining entry
1199                                                  * has too small (not enough)
1200                                                  * scope, because ifa entries
1201                                                  * are sorted by their scope
1202                                                  * values.
1203                                                  */
1204                                                 goto try_nextdev;
1205                                         }
1206                                         break;
1207                                 } else if (minihiscore < miniscore) {
1208                                         if (hiscore->ifa)
1209                                                 in6_ifa_put(hiscore->ifa);
1210
1211                                         in6_ifa_hold(score->ifa);
1212
1213                                         swap(hiscore, score);
1214
1215                                         /* restore our iterator */
1216                                         score->ifa = hiscore->ifa;
1217
1218                                         break;
1219                                 }
1220                         }
1221                 }
1222 try_nextdev:
1223                 read_unlock_bh(&idev->lock);
1224         }
1225         rcu_read_unlock();
1226
1227         if (!hiscore->ifa)
1228                 return -EADDRNOTAVAIL;
1229
1230         ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1231         in6_ifa_put(hiscore->ifa);
1232         return 0;
1233 }
1234 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1235
1236 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1237                       unsigned char banned_flags)
1238 {
1239         struct inet6_ifaddr *ifp;
1240         int err = -EADDRNOTAVAIL;
1241
1242         list_for_each_entry(ifp, &idev->addr_list, if_list) {
1243                 if (ifp->scope == IFA_LINK &&
1244                     !(ifp->flags & banned_flags)) {
1245                         ipv6_addr_copy(addr, &ifp->addr);
1246                         err = 0;
1247                         break;
1248                 }
1249         }
1250         return err;
1251 }
1252
1253 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1254                     unsigned char banned_flags)
1255 {
1256         struct inet6_dev *idev;
1257         int err = -EADDRNOTAVAIL;
1258
1259         rcu_read_lock();
1260         idev = __in6_dev_get(dev);
1261         if (idev) {
1262                 read_lock_bh(&idev->lock);
1263                 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1264                 read_unlock_bh(&idev->lock);
1265         }
1266         rcu_read_unlock();
1267         return err;
1268 }
1269
1270 static int ipv6_count_addresses(struct inet6_dev *idev)
1271 {
1272         int cnt = 0;
1273         struct inet6_ifaddr *ifp;
1274
1275         read_lock_bh(&idev->lock);
1276         list_for_each_entry(ifp, &idev->addr_list, if_list)
1277                 cnt++;
1278         read_unlock_bh(&idev->lock);
1279         return cnt;
1280 }
1281
1282 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1283                   struct net_device *dev, int strict)
1284 {
1285         struct inet6_ifaddr *ifp;
1286         struct hlist_node *node;
1287         unsigned int hash = ipv6_addr_hash(addr);
1288
1289         rcu_read_lock_bh();
1290         hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1291                 if (!net_eq(dev_net(ifp->idev->dev), net))
1292                         continue;
1293                 if (ipv6_addr_equal(&ifp->addr, addr) &&
1294                     !(ifp->flags&IFA_F_TENTATIVE) &&
1295                     (dev == NULL || ifp->idev->dev == dev ||
1296                      !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1297                         rcu_read_unlock_bh();
1298                         return 1;
1299                 }
1300         }
1301
1302         rcu_read_unlock_bh();
1303         return 0;
1304 }
1305 EXPORT_SYMBOL(ipv6_chk_addr);
1306
1307 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1308                                struct net_device *dev)
1309 {
1310         unsigned int hash = ipv6_addr_hash(addr);
1311         struct inet6_ifaddr *ifp;
1312         struct hlist_node *node;
1313
1314         hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1315                 if (!net_eq(dev_net(ifp->idev->dev), net))
1316                         continue;
1317                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1318                         if (dev == NULL || ifp->idev->dev == dev)
1319                                 return true;
1320                 }
1321         }
1322         return false;
1323 }
1324
1325 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1326 {
1327         struct inet6_dev *idev;
1328         struct inet6_ifaddr *ifa;
1329         int     onlink;
1330
1331         onlink = 0;
1332         rcu_read_lock();
1333         idev = __in6_dev_get(dev);
1334         if (idev) {
1335                 read_lock_bh(&idev->lock);
1336                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1337                         onlink = ipv6_prefix_equal(addr, &ifa->addr,
1338                                                    ifa->prefix_len);
1339                         if (onlink)
1340                                 break;
1341                 }
1342                 read_unlock_bh(&idev->lock);
1343         }
1344         rcu_read_unlock();
1345         return onlink;
1346 }
1347
1348 EXPORT_SYMBOL(ipv6_chk_prefix);
1349
1350 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1351                                      struct net_device *dev, int strict)
1352 {
1353         struct inet6_ifaddr *ifp, *result = NULL;
1354         unsigned int hash = ipv6_addr_hash(addr);
1355         struct hlist_node *node;
1356
1357         rcu_read_lock_bh();
1358         hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1359                 if (!net_eq(dev_net(ifp->idev->dev), net))
1360                         continue;
1361                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1362                         if (dev == NULL || ifp->idev->dev == dev ||
1363                             !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1364                                 result = ifp;
1365                                 in6_ifa_hold(ifp);
1366                                 break;
1367                         }
1368                 }
1369         }
1370         rcu_read_unlock_bh();
1371
1372         return result;
1373 }
1374
1375 /* Gets referenced address, destroys ifaddr */
1376
1377 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1378 {
1379         if (ifp->flags&IFA_F_PERMANENT) {
1380                 spin_lock_bh(&ifp->lock);
1381                 addrconf_del_timer(ifp);
1382                 ifp->flags |= IFA_F_TENTATIVE;
1383                 if (dad_failed)
1384                         ifp->flags |= IFA_F_DADFAILED;
1385                 spin_unlock_bh(&ifp->lock);
1386                 if (dad_failed)
1387                         ipv6_ifa_notify(0, ifp);
1388                 in6_ifa_put(ifp);
1389 #ifdef CONFIG_IPV6_PRIVACY
1390         } else if (ifp->flags&IFA_F_TEMPORARY) {
1391                 struct inet6_ifaddr *ifpub;
1392                 spin_lock_bh(&ifp->lock);
1393                 ifpub = ifp->ifpub;
1394                 if (ifpub) {
1395                         in6_ifa_hold(ifpub);
1396                         spin_unlock_bh(&ifp->lock);
1397                         ipv6_create_tempaddr(ifpub, ifp);
1398                         in6_ifa_put(ifpub);
1399                 } else {
1400                         spin_unlock_bh(&ifp->lock);
1401                 }
1402                 ipv6_del_addr(ifp);
1403 #endif
1404         } else
1405                 ipv6_del_addr(ifp);
1406 }
1407
1408 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1409 {
1410         int err = -ENOENT;
1411
1412         spin_lock(&ifp->state_lock);
1413         if (ifp->state == INET6_IFADDR_STATE_DAD) {
1414                 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1415                 err = 0;
1416         }
1417         spin_unlock(&ifp->state_lock);
1418
1419         return err;
1420 }
1421
1422 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1423 {
1424         struct inet6_dev *idev = ifp->idev;
1425
1426         if (addrconf_dad_end(ifp)) {
1427                 in6_ifa_put(ifp);
1428                 return;
1429         }
1430
1431         if (net_ratelimit())
1432                 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
1433                         ifp->idev->dev->name, &ifp->addr);
1434
1435         if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1436                 struct in6_addr addr;
1437
1438                 addr.s6_addr32[0] = htonl(0xfe800000);
1439                 addr.s6_addr32[1] = 0;
1440
1441                 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1442                     ipv6_addr_equal(&ifp->addr, &addr)) {
1443                         /* DAD failed for link-local based on MAC address */
1444                         idev->cnf.disable_ipv6 = 1;
1445
1446                         printk(KERN_INFO "%s: IPv6 being disabled!\n",
1447                                 ifp->idev->dev->name);
1448                 }
1449         }
1450
1451         addrconf_dad_stop(ifp, 1);
1452 }
1453
1454 /* Join to solicited addr multicast group. */
1455
1456 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1457 {
1458         struct in6_addr maddr;
1459
1460         if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1461                 return;
1462
1463         addrconf_addr_solict_mult(addr, &maddr);
1464         ipv6_dev_mc_inc(dev, &maddr);
1465 }
1466
1467 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1468 {
1469         struct in6_addr maddr;
1470
1471         if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1472                 return;
1473
1474         addrconf_addr_solict_mult(addr, &maddr);
1475         __ipv6_dev_mc_dec(idev, &maddr);
1476 }
1477
1478 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1479 {
1480         struct in6_addr addr;
1481         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1482         if (ipv6_addr_any(&addr))
1483                 return;
1484         ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1485 }
1486
1487 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1488 {
1489         struct in6_addr addr;
1490         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1491         if (ipv6_addr_any(&addr))
1492                 return;
1493         __ipv6_dev_ac_dec(ifp->idev, &addr);
1494 }
1495
1496 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1497 {
1498         if (dev->addr_len != ETH_ALEN)
1499                 return -1;
1500         memcpy(eui, dev->dev_addr, 3);
1501         memcpy(eui + 5, dev->dev_addr + 3, 3);
1502
1503         /*
1504          * The zSeries OSA network cards can be shared among various
1505          * OS instances, but the OSA cards have only one MAC address.
1506          * This leads to duplicate address conflicts in conjunction
1507          * with IPv6 if more than one instance uses the same card.
1508          *
1509          * The driver for these cards can deliver a unique 16-bit
1510          * identifier for each instance sharing the same card.  It is
1511          * placed instead of 0xFFFE in the interface identifier.  The
1512          * "u" bit of the interface identifier is not inverted in this
1513          * case.  Hence the resulting interface identifier has local
1514          * scope according to RFC2373.
1515          */
1516         if (dev->dev_id) {
1517                 eui[3] = (dev->dev_id >> 8) & 0xFF;
1518                 eui[4] = dev->dev_id & 0xFF;
1519         } else {
1520                 eui[3] = 0xFF;
1521                 eui[4] = 0xFE;
1522                 eui[0] ^= 2;
1523         }
1524         return 0;
1525 }
1526
1527 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1528 {
1529         /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1530         if (dev->addr_len != ARCNET_ALEN)
1531                 return -1;
1532         memset(eui, 0, 7);
1533         eui[7] = *(u8*)dev->dev_addr;
1534         return 0;
1535 }
1536
1537 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1538 {
1539         if (dev->addr_len != INFINIBAND_ALEN)
1540                 return -1;
1541         memcpy(eui, dev->dev_addr + 12, 8);
1542         eui[0] |= 2;
1543         return 0;
1544 }
1545
1546 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1547 {
1548         if (addr == 0)
1549                 return -1;
1550         eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1551                   ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1552                   ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1553                   ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1554                   ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1555                   ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1556         eui[1] = 0;
1557         eui[2] = 0x5E;
1558         eui[3] = 0xFE;
1559         memcpy(eui + 4, &addr, 4);
1560         return 0;
1561 }
1562
1563 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1564 {
1565         if (dev->priv_flags & IFF_ISATAP)
1566                 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1567         return -1;
1568 }
1569
1570 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1571 {
1572         switch (dev->type) {
1573         case ARPHRD_ETHER:
1574         case ARPHRD_FDDI:
1575         case ARPHRD_IEEE802_TR:
1576                 return addrconf_ifid_eui48(eui, dev);
1577         case ARPHRD_ARCNET:
1578                 return addrconf_ifid_arcnet(eui, dev);
1579         case ARPHRD_INFINIBAND:
1580                 return addrconf_ifid_infiniband(eui, dev);
1581         case ARPHRD_SIT:
1582                 return addrconf_ifid_sit(eui, dev);
1583         }
1584         return -1;
1585 }
1586
1587 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1588 {
1589         int err = -1;
1590         struct inet6_ifaddr *ifp;
1591
1592         read_lock_bh(&idev->lock);
1593         list_for_each_entry(ifp, &idev->addr_list, if_list) {
1594                 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1595                         memcpy(eui, ifp->addr.s6_addr+8, 8);
1596                         err = 0;
1597                         break;
1598                 }
1599         }
1600         read_unlock_bh(&idev->lock);
1601         return err;
1602 }
1603
1604 #ifdef CONFIG_IPV6_PRIVACY
1605 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1606 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1607 {
1608 regen:
1609         get_random_bytes(idev->rndid, sizeof(idev->rndid));
1610         idev->rndid[0] &= ~0x02;
1611
1612         /*
1613          * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1614          * check if generated address is not inappropriate
1615          *
1616          *  - Reserved subnet anycast (RFC 2526)
1617          *      11111101 11....11 1xxxxxxx
1618          *  - ISATAP (RFC4214) 6.1
1619          *      00-00-5E-FE-xx-xx-xx-xx
1620          *  - value 0
1621          *  - XXX: already assigned to an address on the device
1622          */
1623         if (idev->rndid[0] == 0xfd &&
1624             (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1625             (idev->rndid[7]&0x80))
1626                 goto regen;
1627         if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1628                 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1629                         goto regen;
1630                 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1631                         goto regen;
1632         }
1633
1634         return 0;
1635 }
1636
1637 static void ipv6_regen_rndid(unsigned long data)
1638 {
1639         struct inet6_dev *idev = (struct inet6_dev *) data;
1640         unsigned long expires;
1641
1642         rcu_read_lock_bh();
1643         write_lock_bh(&idev->lock);
1644
1645         if (idev->dead)
1646                 goto out;
1647
1648         if (__ipv6_regen_rndid(idev) < 0)
1649                 goto out;
1650
1651         expires = jiffies +
1652                 idev->cnf.temp_prefered_lft * HZ -
1653                 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1654                 idev->cnf.max_desync_factor * HZ;
1655         if (time_before(expires, jiffies)) {
1656                 printk(KERN_WARNING
1657                         "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1658                         idev->dev->name);
1659                 goto out;
1660         }
1661
1662         if (!mod_timer(&idev->regen_timer, expires))
1663                 in6_dev_hold(idev);
1664
1665 out:
1666         write_unlock_bh(&idev->lock);
1667         rcu_read_unlock_bh();
1668         in6_dev_put(idev);
1669 }
1670
1671 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1672         int ret = 0;
1673
1674         if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1675                 ret = __ipv6_regen_rndid(idev);
1676         return ret;
1677 }
1678 #endif
1679
1680 /*
1681  *      Add prefix route.
1682  */
1683
1684 static void
1685 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1686                       unsigned long expires, u32 flags)
1687 {
1688         struct fib6_config cfg = {
1689                 .fc_table = RT6_TABLE_PREFIX,
1690                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1691                 .fc_ifindex = dev->ifindex,
1692                 .fc_expires = expires,
1693                 .fc_dst_len = plen,
1694                 .fc_flags = RTF_UP | flags,
1695                 .fc_nlinfo.nl_net = dev_net(dev),
1696                 .fc_protocol = RTPROT_KERNEL,
1697         };
1698
1699         ipv6_addr_copy(&cfg.fc_dst, pfx);
1700
1701         /* Prevent useless cloning on PtP SIT.
1702            This thing is done here expecting that the whole
1703            class of non-broadcast devices need not cloning.
1704          */
1705 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1706         if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1707                 cfg.fc_flags |= RTF_NONEXTHOP;
1708 #endif
1709
1710         ip6_route_add(&cfg);
1711 }
1712
1713 /* Create "default" multicast route to the interface */
1714
1715 static void addrconf_add_mroute(struct net_device *dev)
1716 {
1717         struct fib6_config cfg = {
1718                 .fc_table = RT6_TABLE_LOCAL,
1719                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1720                 .fc_ifindex = dev->ifindex,
1721                 .fc_dst_len = 8,
1722                 .fc_flags = RTF_UP,
1723                 .fc_nlinfo.nl_net = dev_net(dev),
1724         };
1725
1726         ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1727
1728         ip6_route_add(&cfg);
1729 }
1730
1731 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1732 static void sit_route_add(struct net_device *dev)
1733 {
1734         struct fib6_config cfg = {
1735                 .fc_table = RT6_TABLE_MAIN,
1736                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1737                 .fc_ifindex = dev->ifindex,
1738                 .fc_dst_len = 96,
1739                 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1740                 .fc_nlinfo.nl_net = dev_net(dev),
1741         };
1742
1743         /* prefix length - 96 bits "::d.d.d.d" */
1744         ip6_route_add(&cfg);
1745 }
1746 #endif
1747
1748 static void addrconf_add_lroute(struct net_device *dev)
1749 {
1750         struct in6_addr addr;
1751
1752         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
1753         addrconf_prefix_route(&addr, 64, dev, 0, 0);
1754 }
1755
1756 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1757 {
1758         struct inet6_dev *idev;
1759
1760         ASSERT_RTNL();
1761
1762         idev = ipv6_find_idev(dev);
1763         if (!idev)
1764                 return ERR_PTR(-ENOBUFS);
1765
1766         if (idev->cnf.disable_ipv6)
1767                 return ERR_PTR(-EACCES);
1768
1769         /* Add default multicast route */
1770         addrconf_add_mroute(dev);
1771
1772         /* Add link local route */
1773         addrconf_add_lroute(dev);
1774         return idev;
1775 }
1776
1777 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1778 {
1779         struct prefix_info *pinfo;
1780         __u32 valid_lft;
1781         __u32 prefered_lft;
1782         int addr_type;
1783         struct inet6_dev *in6_dev;
1784         struct net *net = dev_net(dev);
1785
1786         pinfo = (struct prefix_info *) opt;
1787
1788         if (len < sizeof(struct prefix_info)) {
1789                 ADBG(("addrconf: prefix option too short\n"));
1790                 return;
1791         }
1792
1793         /*
1794          *      Validation checks ([ADDRCONF], page 19)
1795          */
1796
1797         addr_type = ipv6_addr_type(&pinfo->prefix);
1798
1799         if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1800                 return;
1801
1802         valid_lft = ntohl(pinfo->valid);
1803         prefered_lft = ntohl(pinfo->prefered);
1804
1805         if (prefered_lft > valid_lft) {
1806                 if (net_ratelimit())
1807                         printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1808                 return;
1809         }
1810
1811         in6_dev = in6_dev_get(dev);
1812
1813         if (in6_dev == NULL) {
1814                 if (net_ratelimit())
1815                         printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1816                 return;
1817         }
1818
1819         /*
1820          *      Two things going on here:
1821          *      1) Add routes for on-link prefixes
1822          *      2) Configure prefixes with the auto flag set
1823          */
1824
1825         if (pinfo->onlink) {
1826                 struct rt6_info *rt;
1827                 unsigned long rt_expires;
1828
1829                 /* Avoid arithmetic overflow. Really, we could
1830                  * save rt_expires in seconds, likely valid_lft,
1831                  * but it would require division in fib gc, that it
1832                  * not good.
1833                  */
1834                 if (HZ > USER_HZ)
1835                         rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1836                 else
1837                         rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
1838
1839                 if (addrconf_finite_timeout(rt_expires))
1840                         rt_expires *= HZ;
1841
1842                 rt = rt6_lookup(net, &pinfo->prefix, NULL,
1843                                 dev->ifindex, 1);
1844
1845                 if (rt && addrconf_is_prefix_route(rt)) {
1846                         /* Autoconf prefix route */
1847                         if (valid_lft == 0) {
1848                                 ip6_del_rt(rt);
1849                                 rt = NULL;
1850                         } else if (addrconf_finite_timeout(rt_expires)) {
1851                                 /* not infinity */
1852                                 rt6_set_expires(rt, jiffies + rt_expires);
1853                         } else {
1854                                 rt6_clean_expires(rt);
1855                         }
1856                 } else if (valid_lft) {
1857                         clock_t expires = 0;
1858                         int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1859                         if (addrconf_finite_timeout(rt_expires)) {
1860                                 /* not infinity */
1861                                 flags |= RTF_EXPIRES;
1862                                 expires = jiffies_to_clock_t(rt_expires);
1863                         }
1864                         addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1865                                               dev, expires, flags);
1866                 }
1867                 if (rt)
1868                         dst_release(&rt->dst);
1869         }
1870
1871         /* Try to figure out our local address for this prefix */
1872
1873         if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1874                 struct inet6_ifaddr * ifp;
1875                 struct in6_addr addr;
1876                 int create = 0, update_lft = 0;
1877
1878                 if (pinfo->prefix_len == 64) {
1879                         memcpy(&addr, &pinfo->prefix, 8);
1880                         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1881                             ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1882                                 in6_dev_put(in6_dev);
1883                                 return;
1884                         }
1885                         goto ok;
1886                 }
1887                 if (net_ratelimit())
1888                         printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1889                                pinfo->prefix_len);
1890                 in6_dev_put(in6_dev);
1891                 return;
1892
1893 ok:
1894
1895                 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
1896
1897                 if (ifp == NULL && valid_lft) {
1898                         int max_addresses = in6_dev->cnf.max_addresses;
1899                         u32 addr_flags = 0;
1900
1901 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1902                         if (in6_dev->cnf.optimistic_dad &&
1903                             !net->ipv6.devconf_all->forwarding)
1904                                 addr_flags = IFA_F_OPTIMISTIC;
1905 #endif
1906
1907                         /* Do not allow to create too much of autoconfigured
1908                          * addresses; this would be too easy way to crash kernel.
1909                          */
1910                         if (!max_addresses ||
1911                             ipv6_count_addresses(in6_dev) < max_addresses)
1912                                 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1913                                                     addr_type&IPV6_ADDR_SCOPE_MASK,
1914                                                     addr_flags);
1915
1916                         if (!ifp || IS_ERR(ifp)) {
1917                                 in6_dev_put(in6_dev);
1918                                 return;
1919                         }
1920
1921                         update_lft = create = 1;
1922                         ifp->cstamp = jiffies;
1923                         addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1924                 }
1925
1926                 if (ifp) {
1927                         int flags;
1928                         unsigned long now;
1929 #ifdef CONFIG_IPV6_PRIVACY
1930                         struct inet6_ifaddr *ift;
1931 #endif
1932                         u32 stored_lft;
1933
1934                         /* update lifetime (RFC2462 5.5.3 e) */
1935                         spin_lock(&ifp->lock);
1936                         now = jiffies;
1937                         if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1938                                 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1939                         else
1940                                 stored_lft = 0;
1941                         if (!update_lft && stored_lft) {
1942                                 if (valid_lft > MIN_VALID_LIFETIME ||
1943                                     valid_lft > stored_lft)
1944                                         update_lft = 1;
1945                                 else if (stored_lft <= MIN_VALID_LIFETIME) {
1946                                         /* valid_lft <= stored_lft is always true */
1947                                         /*
1948                                          * RFC 4862 Section 5.5.3e:
1949                                          * "Note that the preferred lifetime of
1950                                          *  the corresponding address is always
1951                                          *  reset to the Preferred Lifetime in
1952                                          *  the received Prefix Information
1953                                          *  option, regardless of whether the
1954                                          *  valid lifetime is also reset or
1955                                          *  ignored."
1956                                          *
1957                                          *  So if the preferred lifetime in
1958                                          *  this advertisement is different
1959                                          *  than what we have stored, but the
1960                                          *  valid lifetime is invalid, just
1961                                          *  reset prefered_lft.
1962                                          *
1963                                          *  We must set the valid lifetime
1964                                          *  to the stored lifetime since we'll
1965                                          *  be updating the timestamp below,
1966                                          *  else we'll set it back to the
1967                                          *  minimum.
1968                                          */
1969                                         if (prefered_lft != ifp->prefered_lft) {
1970                                                 valid_lft = stored_lft;
1971                                                 update_lft = 1;
1972                                         }
1973                                 } else {
1974                                         valid_lft = MIN_VALID_LIFETIME;
1975                                         if (valid_lft < prefered_lft)
1976                                                 prefered_lft = valid_lft;
1977                                         update_lft = 1;
1978                                 }
1979                         }
1980
1981                         if (update_lft) {
1982                                 ifp->valid_lft = valid_lft;
1983                                 ifp->prefered_lft = prefered_lft;
1984                                 ifp->tstamp = now;
1985                                 flags = ifp->flags;
1986                                 ifp->flags &= ~IFA_F_DEPRECATED;
1987                                 spin_unlock(&ifp->lock);
1988
1989                                 if (!(flags&IFA_F_TENTATIVE))
1990                                         ipv6_ifa_notify(0, ifp);
1991                         } else
1992                                 spin_unlock(&ifp->lock);
1993
1994 #ifdef CONFIG_IPV6_PRIVACY
1995                         read_lock_bh(&in6_dev->lock);
1996                         /* update all temporary addresses in the list */
1997                         list_for_each_entry(ift, &in6_dev->tempaddr_list, tmp_list) {
1998                                 /*
1999                                  * When adjusting the lifetimes of an existing
2000                                  * temporary address, only lower the lifetimes.
2001                                  * Implementations must not increase the
2002                                  * lifetimes of an existing temporary address
2003                                  * when processing a Prefix Information Option.
2004                                  */
2005                                 if (ifp != ift->ifpub)
2006                                         continue;
2007
2008                                 spin_lock(&ift->lock);
2009                                 flags = ift->flags;
2010                                 if (ift->valid_lft > valid_lft &&
2011                                     ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
2012                                         ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
2013                                 if (ift->prefered_lft > prefered_lft &&
2014                                     ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
2015                                         ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
2016                                 spin_unlock(&ift->lock);
2017                                 if (!(flags&IFA_F_TENTATIVE))
2018                                         ipv6_ifa_notify(0, ift);
2019                         }
2020
2021                         if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
2022                                 /*
2023                                  * When a new public address is created as described in [ADDRCONF],
2024                                  * also create a new temporary address. Also create a temporary
2025                                  * address if it's enabled but no temporary address currently exists.
2026                                  */
2027                                 read_unlock_bh(&in6_dev->lock);
2028                                 ipv6_create_tempaddr(ifp, NULL);
2029                         } else {
2030                                 read_unlock_bh(&in6_dev->lock);
2031                         }
2032 #endif
2033                         in6_ifa_put(ifp);
2034                         addrconf_verify(0);
2035                 }
2036         }
2037         inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2038         in6_dev_put(in6_dev);
2039 }
2040
2041 /*
2042  *      Set destination address.
2043  *      Special case for SIT interfaces where we create a new "virtual"
2044  *      device.
2045  */
2046 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2047 {
2048         struct in6_ifreq ireq;
2049         struct net_device *dev;
2050         int err = -EINVAL;
2051
2052         rtnl_lock();
2053
2054         err = -EFAULT;
2055         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2056                 goto err_exit;
2057
2058         dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2059
2060         err = -ENODEV;
2061         if (dev == NULL)
2062                 goto err_exit;
2063
2064 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2065         if (dev->type == ARPHRD_SIT) {
2066                 const struct net_device_ops *ops = dev->netdev_ops;
2067                 struct ifreq ifr;
2068                 struct ip_tunnel_parm p;
2069
2070                 err = -EADDRNOTAVAIL;
2071                 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2072                         goto err_exit;
2073
2074                 memset(&p, 0, sizeof(p));
2075                 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2076                 p.iph.saddr = 0;
2077                 p.iph.version = 4;
2078                 p.iph.ihl = 5;
2079                 p.iph.protocol = IPPROTO_IPV6;
2080                 p.iph.ttl = 64;
2081                 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2082
2083                 if (ops->ndo_do_ioctl) {
2084                         mm_segment_t oldfs = get_fs();
2085
2086                         set_fs(KERNEL_DS);
2087                         err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2088                         set_fs(oldfs);
2089                 } else
2090                         err = -EOPNOTSUPP;
2091
2092                 if (err == 0) {
2093                         err = -ENOBUFS;
2094                         dev = __dev_get_by_name(net, p.name);
2095                         if (!dev)
2096                                 goto err_exit;
2097                         err = dev_open(dev);
2098                 }
2099         }
2100 #endif
2101
2102 err_exit:
2103         rtnl_unlock();
2104         return err;
2105 }
2106
2107 /*
2108  *      Manual configuration of address on an interface
2109  */
2110 static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2111                           unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2112                           __u32 valid_lft)
2113 {
2114         struct inet6_ifaddr *ifp;
2115         struct inet6_dev *idev;
2116         struct net_device *dev;
2117         int scope;
2118         u32 flags;
2119         clock_t expires;
2120         unsigned long timeout;
2121
2122         ASSERT_RTNL();
2123
2124         if (plen > 128)
2125                 return -EINVAL;
2126
2127         /* check the lifetime */
2128         if (!valid_lft || prefered_lft > valid_lft)
2129                 return -EINVAL;
2130
2131         dev = __dev_get_by_index(net, ifindex);
2132         if (!dev)
2133                 return -ENODEV;
2134
2135         idev = addrconf_add_dev(dev);
2136         if (IS_ERR(idev))
2137                 return PTR_ERR(idev);
2138
2139         scope = ipv6_addr_scope(pfx);
2140
2141         timeout = addrconf_timeout_fixup(valid_lft, HZ);
2142         if (addrconf_finite_timeout(timeout)) {
2143                 expires = jiffies_to_clock_t(timeout * HZ);
2144                 valid_lft = timeout;
2145                 flags = RTF_EXPIRES;
2146         } else {
2147                 expires = 0;
2148                 flags = 0;
2149                 ifa_flags |= IFA_F_PERMANENT;
2150         }
2151
2152         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2153         if (addrconf_finite_timeout(timeout)) {
2154                 if (timeout == 0)
2155                         ifa_flags |= IFA_F_DEPRECATED;
2156                 prefered_lft = timeout;
2157         }
2158
2159         ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2160
2161         if (!IS_ERR(ifp)) {
2162                 spin_lock_bh(&ifp->lock);
2163                 ifp->valid_lft = valid_lft;
2164                 ifp->prefered_lft = prefered_lft;
2165                 ifp->tstamp = jiffies;
2166                 spin_unlock_bh(&ifp->lock);
2167
2168                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2169                                       expires, flags);
2170                 /*
2171                  * Note that section 3.1 of RFC 4429 indicates
2172                  * that the Optimistic flag should not be set for
2173                  * manually configured addresses
2174                  */
2175                 addrconf_dad_start(ifp, 0);
2176                 in6_ifa_put(ifp);
2177                 addrconf_verify(0);
2178                 return 0;
2179         }
2180
2181         return PTR_ERR(ifp);
2182 }
2183
2184 static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
2185                           unsigned int plen)
2186 {
2187         struct inet6_ifaddr *ifp;
2188         struct inet6_dev *idev;
2189         struct net_device *dev;
2190
2191         if (plen > 128)
2192                 return -EINVAL;
2193
2194         dev = __dev_get_by_index(net, ifindex);
2195         if (!dev)
2196                 return -ENODEV;
2197
2198         if ((idev = __in6_dev_get(dev)) == NULL)
2199                 return -ENXIO;
2200
2201         read_lock_bh(&idev->lock);
2202         list_for_each_entry(ifp, &idev->addr_list, if_list) {
2203                 if (ifp->prefix_len == plen &&
2204                     ipv6_addr_equal(pfx, &ifp->addr)) {
2205                         in6_ifa_hold(ifp);
2206                         read_unlock_bh(&idev->lock);
2207
2208                         ipv6_del_addr(ifp);
2209
2210                         /* If the last address is deleted administratively,
2211                            disable IPv6 on this interface.
2212                          */
2213                         if (list_empty(&idev->addr_list))
2214                                 addrconf_ifdown(idev->dev, 1);
2215                         return 0;
2216                 }
2217         }
2218         read_unlock_bh(&idev->lock);
2219         return -EADDRNOTAVAIL;
2220 }
2221
2222
2223 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2224 {
2225         struct in6_ifreq ireq;
2226         int err;
2227
2228         if (!capable(CAP_NET_ADMIN))
2229                 return -EPERM;
2230
2231         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2232                 return -EFAULT;
2233
2234         rtnl_lock();
2235         err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2236                              ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2237                              INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2238         rtnl_unlock();
2239         return err;
2240 }
2241
2242 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2243 {
2244         struct in6_ifreq ireq;
2245         int err;
2246
2247         if (!capable(CAP_NET_ADMIN))
2248                 return -EPERM;
2249
2250         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2251                 return -EFAULT;
2252
2253         rtnl_lock();
2254         err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2255                              ireq.ifr6_prefixlen);
2256         rtnl_unlock();
2257         return err;
2258 }
2259
2260 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2261                      int plen, int scope)
2262 {
2263         struct inet6_ifaddr *ifp;
2264
2265         ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2266         if (!IS_ERR(ifp)) {
2267                 spin_lock_bh(&ifp->lock);
2268                 ifp->flags &= ~IFA_F_TENTATIVE;
2269                 spin_unlock_bh(&ifp->lock);
2270                 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2271                 in6_ifa_put(ifp);
2272         }
2273 }
2274
2275 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2276 static void sit_add_v4_addrs(struct inet6_dev *idev)
2277 {
2278         struct in6_addr addr;
2279         struct net_device *dev;
2280         struct net *net = dev_net(idev->dev);
2281         int scope;
2282
2283         ASSERT_RTNL();
2284
2285         memset(&addr, 0, sizeof(struct in6_addr));
2286         memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2287
2288         if (idev->dev->flags&IFF_POINTOPOINT) {
2289                 addr.s6_addr32[0] = htonl(0xfe800000);
2290                 scope = IFA_LINK;
2291         } else {
2292                 scope = IPV6_ADDR_COMPATv4;
2293         }
2294
2295         if (addr.s6_addr32[3]) {
2296                 add_addr(idev, &addr, 128, scope);
2297                 return;
2298         }
2299
2300         for_each_netdev(net, dev) {
2301                 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2302                 if (in_dev && (dev->flags & IFF_UP)) {
2303                         struct in_ifaddr * ifa;
2304
2305                         int flag = scope;
2306
2307                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2308                                 int plen;
2309
2310                                 addr.s6_addr32[3] = ifa->ifa_local;
2311
2312                                 if (ifa->ifa_scope == RT_SCOPE_LINK)
2313                                         continue;
2314                                 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2315                                         if (idev->dev->flags&IFF_POINTOPOINT)
2316                                                 continue;
2317                                         flag |= IFA_HOST;
2318                                 }
2319                                 if (idev->dev->flags&IFF_POINTOPOINT)
2320                                         plen = 64;
2321                                 else
2322                                         plen = 96;
2323
2324                                 add_addr(idev, &addr, plen, flag);
2325                         }
2326                 }
2327         }
2328 }
2329 #endif
2330
2331 static void init_loopback(struct net_device *dev)
2332 {
2333         struct inet6_dev  *idev;
2334         struct net_device *sp_dev;
2335         struct inet6_ifaddr *sp_ifa;
2336         struct rt6_info *sp_rt;
2337
2338         /* ::1 */
2339
2340         ASSERT_RTNL();
2341
2342         if ((idev = ipv6_find_idev(dev)) == NULL) {
2343                 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2344                 return;
2345         }
2346
2347         add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2348
2349         /* Add routes to other interface's IPv6 addresses */
2350         for_each_netdev(dev_net(dev), sp_dev) {
2351                 if (!strcmp(sp_dev->name, dev->name))
2352                         continue;
2353
2354                 idev = __in6_dev_get(sp_dev);
2355                 if (!idev)
2356                         continue;
2357
2358                 read_lock_bh(&idev->lock);
2359                 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2360
2361                         if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2362                                 continue;
2363
2364                         if (sp_ifa->rt)
2365                                 continue;
2366
2367                         sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2368
2369                         /* Failure cases are ignored */
2370                         if (!IS_ERR(sp_rt)) {
2371                                 sp_ifa->rt = sp_rt;
2372                                 ip6_ins_rt(sp_rt);
2373                         }
2374                 }
2375                 read_unlock_bh(&idev->lock);
2376         }
2377 }
2378
2379 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2380 {
2381         struct inet6_ifaddr * ifp;
2382         u32 addr_flags = IFA_F_PERMANENT;
2383
2384 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2385         if (idev->cnf.optimistic_dad &&
2386             !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2387                 addr_flags |= IFA_F_OPTIMISTIC;
2388 #endif
2389
2390
2391         ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2392         if (!IS_ERR(ifp)) {
2393                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2394                 addrconf_dad_start(ifp, 0);
2395                 in6_ifa_put(ifp);
2396         }
2397 }
2398
2399 static void addrconf_dev_config(struct net_device *dev)
2400 {
2401         struct in6_addr addr;
2402         struct inet6_dev    * idev;
2403
2404         ASSERT_RTNL();
2405
2406         if ((dev->type != ARPHRD_ETHER) &&
2407             (dev->type != ARPHRD_FDDI) &&
2408             (dev->type != ARPHRD_IEEE802_TR) &&
2409             (dev->type != ARPHRD_ARCNET) &&
2410             (dev->type != ARPHRD_INFINIBAND)) {
2411                 /* Alas, we support only Ethernet autoconfiguration. */
2412                 return;
2413         }
2414
2415         idev = addrconf_add_dev(dev);
2416         if (IS_ERR(idev))
2417                 return;
2418
2419         memset(&addr, 0, sizeof(struct in6_addr));
2420         addr.s6_addr32[0] = htonl(0xFE800000);
2421
2422         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2423                 addrconf_add_linklocal(idev, &addr);
2424 }
2425
2426 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2427 static void addrconf_sit_config(struct net_device *dev)
2428 {
2429         struct inet6_dev *idev;
2430
2431         ASSERT_RTNL();
2432
2433         /*
2434          * Configure the tunnel with one of our IPv4
2435          * addresses... we should configure all of
2436          * our v4 addrs in the tunnel
2437          */
2438
2439         if ((idev = ipv6_find_idev(dev)) == NULL) {
2440                 printk(KERN_DEBUG "init sit: add_dev failed\n");
2441                 return;
2442         }
2443
2444         if (dev->priv_flags & IFF_ISATAP) {
2445                 struct in6_addr addr;
2446
2447                 ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2448                 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2449                 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2450                         addrconf_add_linklocal(idev, &addr);
2451                 return;
2452         }
2453
2454         sit_add_v4_addrs(idev);
2455
2456         if (dev->flags&IFF_POINTOPOINT) {
2457                 addrconf_add_mroute(dev);
2458                 addrconf_add_lroute(dev);
2459         } else
2460                 sit_route_add(dev);
2461 }
2462 #endif
2463
2464 static inline int
2465 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2466 {
2467         struct in6_addr lladdr;
2468
2469         if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2470                 addrconf_add_linklocal(idev, &lladdr);
2471                 return 0;
2472         }
2473         return -1;
2474 }
2475
2476 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2477 {
2478         struct net_device *link_dev;
2479         struct net *net = dev_net(idev->dev);
2480
2481         /* first try to inherit the link-local address from the link device */
2482         if (idev->dev->iflink &&
2483             (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2484                 if (!ipv6_inherit_linklocal(idev, link_dev))
2485                         return;
2486         }
2487         /* then try to inherit it from any device */
2488         for_each_netdev(net, link_dev) {
2489                 if (!ipv6_inherit_linklocal(idev, link_dev))
2490                         return;
2491         }
2492         printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2493 }
2494
2495 /*
2496  * Autoconfigure tunnel with a link-local address so routing protocols,
2497  * DHCPv6, MLD etc. can be run over the virtual link
2498  */
2499
2500 static void addrconf_ip6_tnl_config(struct net_device *dev)
2501 {
2502         struct inet6_dev *idev;
2503
2504         ASSERT_RTNL();
2505
2506         idev = addrconf_add_dev(dev);
2507         if (IS_ERR(idev)) {
2508                 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2509                 return;
2510         }
2511         ip6_tnl_add_linklocal(idev);
2512 }
2513
2514 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2515                            void * data)
2516 {
2517         struct net_device *dev = (struct net_device *) data;
2518         struct inet6_dev *idev = __in6_dev_get(dev);
2519         int run_pending = 0;
2520         int err;
2521
2522         switch (event) {
2523         case NETDEV_REGISTER:
2524                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2525                         idev = ipv6_add_dev(dev);
2526                         if (!idev)
2527                                 return notifier_from_errno(-ENOMEM);
2528                 }
2529                 break;
2530
2531         case NETDEV_UP:
2532         case NETDEV_CHANGE:
2533                 if (dev->flags & IFF_SLAVE)
2534                         break;
2535
2536                 if (event == NETDEV_UP) {
2537                         if (!addrconf_qdisc_ok(dev)) {
2538                                 /* device is not ready yet. */
2539                                 printk(KERN_INFO
2540                                         "ADDRCONF(NETDEV_UP): %s: "
2541                                         "link is not ready\n",
2542                                         dev->name);
2543                                 break;
2544                         }
2545
2546                         if (!idev && dev->mtu >= IPV6_MIN_MTU)
2547                                 idev = ipv6_add_dev(dev);
2548
2549                         if (idev) {
2550                                 idev->if_flags |= IF_READY;
2551                                 run_pending = 1;
2552                         }
2553                 } else {
2554                         if (!addrconf_qdisc_ok(dev)) {
2555                                 /* device is still not ready. */
2556                                 break;
2557                         }
2558
2559                         if (idev) {
2560                                 if (idev->if_flags & IF_READY)
2561                                         /* device is already configured. */
2562                                         break;
2563                                 idev->if_flags |= IF_READY;
2564                         }
2565
2566                         printk(KERN_INFO
2567                                         "ADDRCONF(NETDEV_CHANGE): %s: "
2568                                         "link becomes ready\n",
2569                                         dev->name);
2570
2571                         run_pending = 1;
2572                 }
2573
2574                 switch (dev->type) {
2575 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2576                 case ARPHRD_SIT:
2577                         addrconf_sit_config(dev);
2578                         break;
2579 #endif
2580                 case ARPHRD_TUNNEL6:
2581                         addrconf_ip6_tnl_config(dev);
2582                         break;
2583                 case ARPHRD_LOOPBACK:
2584                         init_loopback(dev);
2585                         break;
2586
2587                 default:
2588                         addrconf_dev_config(dev);
2589                         break;
2590                 }
2591
2592                 if (idev) {
2593                         if (run_pending)
2594                                 addrconf_dad_run(idev);
2595
2596                         /*
2597                          * If the MTU changed during the interface down,
2598                          * when the interface up, the changed MTU must be
2599                          * reflected in the idev as well as routers.
2600                          */
2601                         if (idev->cnf.mtu6 != dev->mtu &&
2602                             dev->mtu >= IPV6_MIN_MTU) {
2603                                 rt6_mtu_change(dev, dev->mtu);
2604                                 idev->cnf.mtu6 = dev->mtu;
2605                         }
2606                         idev->tstamp = jiffies;
2607                         inet6_ifinfo_notify(RTM_NEWLINK, idev);
2608
2609                         /*
2610                          * If the changed mtu during down is lower than
2611                          * IPV6_MIN_MTU stop IPv6 on this interface.
2612                          */
2613                         if (dev->mtu < IPV6_MIN_MTU)
2614                                 addrconf_ifdown(dev, 1);
2615                 }
2616                 break;
2617
2618         case NETDEV_CHANGEMTU:
2619                 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2620                         rt6_mtu_change(dev, dev->mtu);
2621                         idev->cnf.mtu6 = dev->mtu;
2622                         break;
2623                 }
2624
2625                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2626                         idev = ipv6_add_dev(dev);
2627                         if (idev)
2628                                 break;
2629                 }
2630
2631                 /*
2632                  * MTU falled under IPV6_MIN_MTU.
2633                  * Stop IPv6 on this interface.
2634                  */
2635
2636         case NETDEV_DOWN:
2637         case NETDEV_UNREGISTER:
2638                 /*
2639                  *      Remove all addresses from this interface.
2640                  */
2641                 addrconf_ifdown(dev, event != NETDEV_DOWN);
2642                 break;
2643
2644         case NETDEV_CHANGENAME:
2645                 if (idev) {
2646                         snmp6_unregister_dev(idev);
2647                         addrconf_sysctl_unregister(idev);
2648                         addrconf_sysctl_register(idev);
2649                         err = snmp6_register_dev(idev);
2650                         if (err)
2651                                 return notifier_from_errno(err);
2652                 }
2653                 break;
2654
2655         case NETDEV_PRE_TYPE_CHANGE:
2656         case NETDEV_POST_TYPE_CHANGE:
2657                 addrconf_type_change(dev, event);
2658                 break;
2659         }
2660
2661         return NOTIFY_OK;
2662 }
2663
2664 /*
2665  *      addrconf module should be notified of a device going up
2666  */
2667 static struct notifier_block ipv6_dev_notf = {
2668         .notifier_call = addrconf_notify,
2669 };
2670
2671 static void addrconf_type_change(struct net_device *dev, unsigned long event)
2672 {
2673         struct inet6_dev *idev;
2674         ASSERT_RTNL();
2675
2676         idev = __in6_dev_get(dev);
2677
2678         if (event == NETDEV_POST_TYPE_CHANGE)
2679                 ipv6_mc_remap(idev);
2680         else if (event == NETDEV_PRE_TYPE_CHANGE)
2681                 ipv6_mc_unmap(idev);
2682 }
2683
2684 static int addrconf_ifdown(struct net_device *dev, int how)
2685 {
2686         struct net *net = dev_net(dev);
2687         struct inet6_dev *idev;
2688         struct inet6_ifaddr *ifa;
2689         int state, i;
2690
2691         ASSERT_RTNL();
2692
2693         rt6_ifdown(net, dev);
2694         neigh_ifdown(&nd_tbl, dev);
2695
2696         idev = __in6_dev_get(dev);
2697         if (idev == NULL)
2698                 return -ENODEV;
2699
2700         /*
2701          * Step 1: remove reference to ipv6 device from parent device.
2702          *         Do not dev_put!
2703          */
2704         if (how) {
2705                 idev->dead = 1;
2706
2707                 /* protected by rtnl_lock */
2708                 rcu_assign_pointer(dev->ip6_ptr, NULL);
2709
2710                 /* Step 1.5: remove snmp6 entry */
2711                 snmp6_unregister_dev(idev);
2712
2713         }
2714
2715         /* Step 2: clear hash table */
2716         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2717                 struct hlist_head *h = &inet6_addr_lst[i];
2718                 struct hlist_node *n;
2719
2720                 spin_lock_bh(&addrconf_hash_lock);
2721         restart:
2722                 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2723                         if (ifa->idev == idev) {
2724                                 hlist_del_init_rcu(&ifa->addr_lst);
2725                                 addrconf_del_timer(ifa);
2726                                 goto restart;
2727                         }
2728                 }
2729                 spin_unlock_bh(&addrconf_hash_lock);
2730         }
2731
2732         write_lock_bh(&idev->lock);
2733
2734         /* Step 2: clear flags for stateless addrconf */
2735         if (!how)
2736                 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2737
2738 #ifdef CONFIG_IPV6_PRIVACY
2739         if (how && del_timer(&idev->regen_timer))
2740                 in6_dev_put(idev);
2741
2742         /* Step 3: clear tempaddr list */
2743         while (!list_empty(&idev->tempaddr_list)) {
2744                 ifa = list_first_entry(&idev->tempaddr_list,
2745                                        struct inet6_ifaddr, tmp_list);
2746                 list_del(&ifa->tmp_list);
2747                 write_unlock_bh(&idev->lock);
2748                 spin_lock_bh(&ifa->lock);
2749
2750      &n