a5521c589511a677440c94ff2b225bac132558f6
[pandora-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 <linux/export.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                 dev_put(dev);
379                 kfree(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         rcu_read_lock();
497         for_each_netdev_rcu(net, dev) {
498                 idev = __in6_dev_get(dev);
499                 if (idev) {
500                         int changed = (!idev->cnf.forwarding) ^ (!newf);
501                         idev->cnf.forwarding = newf;
502                         if (changed)
503                                 dev_forward_change(idev);
504                 }
505         }
506         rcu_read_unlock();
507 }
508
509 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
510 {
511         struct net *net;
512
513         net = (struct net *)table->extra2;
514         if (p == &net->ipv6.devconf_dflt->forwarding)
515                 return 0;
516
517         if (!rtnl_trylock()) {
518                 /* Restore the original values before restarting */
519                 *p = old;
520                 return restart_syscall();
521         }
522
523         if (p == &net->ipv6.devconf_all->forwarding) {
524                 __s32 newf = net->ipv6.devconf_all->forwarding;
525                 net->ipv6.devconf_dflt->forwarding = newf;
526                 addrconf_forward_change(net, newf);
527         } else if ((!*p) ^ (!old))
528                 dev_forward_change((struct inet6_dev *)table->extra1);
529         rtnl_unlock();
530
531         if (*p)
532                 rt6_purge_dflt_routers(net);
533         return 1;
534 }
535 #endif
536
537 /* Nobody refers to this ifaddr, destroy it */
538 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
539 {
540         WARN_ON(!hlist_unhashed(&ifp->addr_lst));
541
542 #ifdef NET_REFCNT_DEBUG
543         printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
544 #endif
545
546         in6_dev_put(ifp->idev);
547
548         if (del_timer(&ifp->timer))
549                 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
550
551         if (ifp->state != INET6_IFADDR_STATE_DEAD) {
552                 pr_warning("Freeing alive inet6 address %p\n", ifp);
553                 return;
554         }
555         dst_release(&ifp->rt->dst);
556
557         kfree_rcu(ifp, rcu);
558 }
559
560 static void
561 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
562 {
563         struct list_head *p;
564         int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
565
566         /*
567          * Each device address list is sorted in order of scope -
568          * global before linklocal.
569          */
570         list_for_each(p, &idev->addr_list) {
571                 struct inet6_ifaddr *ifa
572                         = list_entry(p, struct inet6_ifaddr, if_list);
573                 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
574                         break;
575         }
576
577         list_add_tail(&ifp->if_list, p);
578 }
579
580 static u32 ipv6_addr_hash(const struct in6_addr *addr)
581 {
582         /*
583          * We perform the hash function over the last 64 bits of the address
584          * This will include the IEEE address token on links that support it.
585          */
586         return jhash_2words((__force u32)addr->s6_addr32[2],
587                             (__force u32)addr->s6_addr32[3], 0)
588                 & (IN6_ADDR_HSIZE - 1);
589 }
590
591 /* On success it returns ifp with increased reference count */
592
593 static struct inet6_ifaddr *
594 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
595               int scope, u32 flags)
596 {
597         struct inet6_ifaddr *ifa = NULL;
598         struct rt6_info *rt;
599         unsigned int hash;
600         int err = 0;
601         int addr_type = ipv6_addr_type(addr);
602
603         if (addr_type == IPV6_ADDR_ANY ||
604             addr_type & IPV6_ADDR_MULTICAST ||
605             (!(idev->dev->flags & IFF_LOOPBACK) &&
606              addr_type & IPV6_ADDR_LOOPBACK))
607                 return ERR_PTR(-EADDRNOTAVAIL);
608
609         rcu_read_lock_bh();
610         if (idev->dead) {
611                 err = -ENODEV;                  /*XXX*/
612                 goto out2;
613         }
614
615         if (idev->cnf.disable_ipv6) {
616                 err = -EACCES;
617                 goto out2;
618         }
619
620         spin_lock(&addrconf_hash_lock);
621
622         /* Ignore adding duplicate addresses on an interface */
623         if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
624                 ADBG(("ipv6_add_addr: already assigned\n"));
625                 err = -EEXIST;
626                 goto out;
627         }
628
629         ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
630
631         if (ifa == NULL) {
632                 ADBG(("ipv6_add_addr: malloc failed\n"));
633                 err = -ENOBUFS;
634                 goto out;
635         }
636
637         rt = addrconf_dst_alloc(idev, addr, 0);
638         if (IS_ERR(rt)) {
639                 err = PTR_ERR(rt);
640                 goto out;
641         }
642
643         ipv6_addr_copy(&ifa->addr, addr);
644
645         spin_lock_init(&ifa->lock);
646         spin_lock_init(&ifa->state_lock);
647         init_timer(&ifa->timer);
648         INIT_HLIST_NODE(&ifa->addr_lst);
649         ifa->timer.data = (unsigned long) ifa;
650         ifa->scope = scope;
651         ifa->prefix_len = pfxlen;
652         ifa->flags = flags | IFA_F_TENTATIVE;
653         ifa->cstamp = ifa->tstamp = jiffies;
654
655         ifa->rt = rt;
656
657         /*
658          * part one of RFC 4429, section 3.3
659          * We should not configure an address as
660          * optimistic if we do not yet know the link
661          * layer address of our nexhop router
662          */
663
664         if (dst_get_neighbour_raw(&rt->dst) == NULL)
665                 ifa->flags &= ~IFA_F_OPTIMISTIC;
666
667         ifa->idev = idev;
668         in6_dev_hold(idev);
669         /* For caller */
670         in6_ifa_hold(ifa);
671
672         /* Add to big hash table */
673         hash = ipv6_addr_hash(addr);
674
675         hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
676         spin_unlock(&addrconf_hash_lock);
677
678         write_lock(&idev->lock);
679         /* Add to inet6_dev unicast addr list. */
680         ipv6_link_dev_addr(idev, ifa);
681
682 #ifdef CONFIG_IPV6_PRIVACY
683         if (ifa->flags&IFA_F_TEMPORARY) {
684                 list_add(&ifa->tmp_list, &idev->tempaddr_list);
685                 in6_ifa_hold(ifa);
686         }
687 #endif
688
689         in6_ifa_hold(ifa);
690         write_unlock(&idev->lock);
691 out2:
692         rcu_read_unlock_bh();
693
694         if (likely(err == 0))
695                 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
696         else {
697                 kfree(ifa);
698                 ifa = ERR_PTR(err);
699         }
700
701         return ifa;
702 out:
703         spin_unlock(&addrconf_hash_lock);
704         goto out2;
705 }
706
707 /* This function wants to get referenced ifp and releases it before return */
708
709 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
710 {
711         struct inet6_ifaddr *ifa, *ifn;
712         struct inet6_dev *idev = ifp->idev;
713         int state;
714         int deleted = 0, onlink = 0;
715         unsigned long expires = jiffies;
716
717         spin_lock_bh(&ifp->state_lock);
718         state = ifp->state;
719         ifp->state = INET6_IFADDR_STATE_DEAD;
720         spin_unlock_bh(&ifp->state_lock);
721
722         if (state == INET6_IFADDR_STATE_DEAD)
723                 goto out;
724
725         spin_lock_bh(&addrconf_hash_lock);
726         hlist_del_init_rcu(&ifp->addr_lst);
727         spin_unlock_bh(&addrconf_hash_lock);
728
729         write_lock_bh(&idev->lock);
730 #ifdef CONFIG_IPV6_PRIVACY
731         if (ifp->flags&IFA_F_TEMPORARY) {
732                 list_del(&ifp->tmp_list);
733                 if (ifp->ifpub) {
734                         in6_ifa_put(ifp->ifpub);
735                         ifp->ifpub = NULL;
736                 }
737                 __in6_ifa_put(ifp);
738         }
739 #endif
740
741         list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
742                 if (ifa == ifp) {
743                         list_del_init(&ifp->if_list);
744                         __in6_ifa_put(ifp);
745
746                         if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
747                                 break;
748                         deleted = 1;
749                         continue;
750                 } else if (ifp->flags & IFA_F_PERMANENT) {
751                         if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
752                                               ifp->prefix_len)) {
753                                 if (ifa->flags & IFA_F_PERMANENT) {
754                                         onlink = 1;
755                                         if (deleted)
756                                                 break;
757                                 } else {
758                                         unsigned long lifetime;
759
760                                         if (!onlink)
761                                                 onlink = -1;
762
763                                         spin_lock(&ifa->lock);
764
765                                         lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
766                                         /*
767                                          * Note: Because this address is
768                                          * not permanent, lifetime <
769                                          * LONG_MAX / HZ here.
770                                          */
771                                         if (time_before(expires,
772                                                         ifa->tstamp + lifetime * HZ))
773                                                 expires = ifa->tstamp + lifetime * HZ;
774                                         spin_unlock(&ifa->lock);
775                                 }
776                         }
777                 }
778         }
779         write_unlock_bh(&idev->lock);
780
781         addrconf_del_timer(ifp);
782
783         ipv6_ifa_notify(RTM_DELADDR, ifp);
784
785         atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
786
787         /*
788          * Purge or update corresponding prefix
789          *
790          * 1) we don't purge prefix here if address was not permanent.
791          *    prefix is managed by its own lifetime.
792          * 2) if there're no addresses, delete prefix.
793          * 3) if there're still other permanent address(es),
794          *    corresponding prefix is still permanent.
795          * 4) otherwise, update prefix lifetime to the
796          *    longest valid lifetime among the corresponding
797          *    addresses on the device.
798          *    Note: subsequent RA will update lifetime.
799          *
800          * --yoshfuji
801          */
802         if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
803                 struct in6_addr prefix;
804                 struct rt6_info *rt;
805                 struct net *net = dev_net(ifp->idev->dev);
806                 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
807                 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
808
809                 if (rt && addrconf_is_prefix_route(rt)) {
810                         if (onlink == 0) {
811                                 ip6_del_rt(rt);
812                                 rt = NULL;
813                         } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
814                                 rt->rt6i_expires = expires;
815                                 rt->rt6i_flags |= RTF_EXPIRES;
816                         }
817                 }
818                 dst_release(&rt->dst);
819         }
820
821         /* clean up prefsrc entries */
822         rt6_remove_prefsrc(ifp);
823 out:
824         in6_ifa_put(ifp);
825 }
826
827 #ifdef CONFIG_IPV6_PRIVACY
828 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
829 {
830         struct inet6_dev *idev = ifp->idev;
831         struct in6_addr addr, *tmpaddr;
832         unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
833         unsigned long regen_advance;
834         int tmp_plen;
835         int ret = 0;
836         int max_addresses;
837         u32 addr_flags;
838         unsigned long now = jiffies;
839
840         write_lock(&idev->lock);
841         if (ift) {
842                 spin_lock_bh(&ift->lock);
843                 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
844                 spin_unlock_bh(&ift->lock);
845                 tmpaddr = &addr;
846         } else {
847                 tmpaddr = NULL;
848         }
849 retry:
850         in6_dev_hold(idev);
851         if (idev->cnf.use_tempaddr <= 0) {
852                 write_unlock(&idev->lock);
853                 printk(KERN_INFO
854                         "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
855                 in6_dev_put(idev);
856                 ret = -1;
857                 goto out;
858         }
859         spin_lock_bh(&ifp->lock);
860         if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
861                 idev->cnf.use_tempaddr = -1;    /*XXX*/
862                 spin_unlock_bh(&ifp->lock);
863                 write_unlock(&idev->lock);
864                 printk(KERN_WARNING
865                         "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
866                 in6_dev_put(idev);
867                 ret = -1;
868                 goto out;
869         }
870         in6_ifa_hold(ifp);
871         memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
872         if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
873                 spin_unlock_bh(&ifp->lock);
874                 write_unlock(&idev->lock);
875                 printk(KERN_WARNING
876                         "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
877                 in6_ifa_put(ifp);
878                 in6_dev_put(idev);
879                 ret = -1;
880                 goto out;
881         }
882         memcpy(&addr.s6_addr[8], idev->rndid, 8);
883         age = (now - ifp->tstamp) / HZ;
884         tmp_valid_lft = min_t(__u32,
885                               ifp->valid_lft,
886                               idev->cnf.temp_valid_lft + age);
887         tmp_prefered_lft = min_t(__u32,
888                                  ifp->prefered_lft,
889                                  idev->cnf.temp_prefered_lft + age -
890                                  idev->cnf.max_desync_factor);
891         tmp_plen = ifp->prefix_len;
892         max_addresses = idev->cnf.max_addresses;
893         tmp_tstamp = ifp->tstamp;
894         spin_unlock_bh(&ifp->lock);
895
896         regen_advance = idev->cnf.regen_max_retry *
897                         idev->cnf.dad_transmits *
898                         idev->nd_parms->retrans_time / HZ;
899         write_unlock(&idev->lock);
900
901         /* A temporary address is created only if this calculated Preferred
902          * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
903          * an implementation must not create a temporary address with a zero
904          * Preferred Lifetime.
905          */
906         if (tmp_prefered_lft <= regen_advance) {
907                 in6_ifa_put(ifp);
908                 in6_dev_put(idev);
909                 ret = -1;
910                 goto out;
911         }
912
913         addr_flags = IFA_F_TEMPORARY;
914         /* set in addrconf_prefix_rcv() */
915         if (ifp->flags & IFA_F_OPTIMISTIC)
916                 addr_flags |= IFA_F_OPTIMISTIC;
917
918         ift = !max_addresses ||
919               ipv6_count_addresses(idev) < max_addresses ?
920                 ipv6_add_addr(idev, &addr, tmp_plen,
921                               ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
922                               addr_flags) : NULL;
923         if (!ift || IS_ERR(ift)) {
924                 in6_ifa_put(ifp);
925                 in6_dev_put(idev);
926                 printk(KERN_INFO
927                         "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
928                 tmpaddr = &addr;
929                 write_lock(&idev->lock);
930                 goto retry;
931         }
932
933         spin_lock_bh(&ift->lock);
934         ift->ifpub = ifp;
935         ift->valid_lft = tmp_valid_lft;
936         ift->prefered_lft = tmp_prefered_lft;
937         ift->cstamp = now;
938         ift->tstamp = tmp_tstamp;
939         spin_unlock_bh(&ift->lock);
940
941         addrconf_dad_start(ift, 0);
942         in6_ifa_put(ift);
943         in6_dev_put(idev);
944 out:
945         return ret;
946 }
947 #endif
948
949 /*
950  *      Choose an appropriate source address (RFC3484)
951  */
952 enum {
953         IPV6_SADDR_RULE_INIT = 0,
954         IPV6_SADDR_RULE_LOCAL,
955         IPV6_SADDR_RULE_SCOPE,
956         IPV6_SADDR_RULE_PREFERRED,
957 #ifdef CONFIG_IPV6_MIP6
958         IPV6_SADDR_RULE_HOA,
959 #endif
960         IPV6_SADDR_RULE_OIF,
961         IPV6_SADDR_RULE_LABEL,
962 #ifdef CONFIG_IPV6_PRIVACY
963         IPV6_SADDR_RULE_PRIVACY,
964 #endif
965         IPV6_SADDR_RULE_ORCHID,
966         IPV6_SADDR_RULE_PREFIX,
967         IPV6_SADDR_RULE_MAX
968 };
969
970 struct ipv6_saddr_score {
971         int                     rule;
972         int                     addr_type;
973         struct inet6_ifaddr     *ifa;
974         DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
975         int                     scopedist;
976         int                     matchlen;
977 };
978
979 struct ipv6_saddr_dst {
980         const struct in6_addr *addr;
981         int ifindex;
982         int scope;
983         int label;
984         unsigned int prefs;
985 };
986
987 static inline int ipv6_saddr_preferred(int type)
988 {
989         if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
990                 return 1;
991         return 0;
992 }
993
994 static int ipv6_get_saddr_eval(struct net *net,
995                                struct ipv6_saddr_score *score,
996                                struct ipv6_saddr_dst *dst,
997                                int i)
998 {
999         int ret;
1000
1001         if (i <= score->rule) {
1002                 switch (i) {
1003                 case IPV6_SADDR_RULE_SCOPE:
1004                         ret = score->scopedist;
1005                         break;
1006                 case IPV6_SADDR_RULE_PREFIX:
1007                         ret = score->matchlen;
1008                         break;
1009                 default:
1010                         ret = !!test_bit(i, score->scorebits);
1011                 }
1012                 goto out;
1013         }
1014
1015         switch (i) {
1016         case IPV6_SADDR_RULE_INIT:
1017                 /* Rule 0: remember if hiscore is not ready yet */
1018                 ret = !!score->ifa;
1019                 break;
1020         case IPV6_SADDR_RULE_LOCAL:
1021                 /* Rule 1: Prefer same address */
1022                 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1023                 break;
1024         case IPV6_SADDR_RULE_SCOPE:
1025                 /* Rule 2: Prefer appropriate scope
1026                  *
1027                  *      ret
1028                  *       ^
1029                  *    -1 |  d 15
1030                  *    ---+--+-+---> scope
1031                  *       |
1032                  *       |             d is scope of the destination.
1033                  *  B-d  |  \
1034                  *       |   \      <- smaller scope is better if
1035                  *  B-15 |    \        if scope is enough for destinaion.
1036                  *       |             ret = B - scope (-1 <= scope >= d <= 15).
1037                  * d-C-1 | /
1038                  *       |/         <- greater is better
1039                  *   -C  /             if scope is not enough for destination.
1040                  *      /|             ret = scope - C (-1 <= d < scope <= 15).
1041                  *
1042                  * d - C - 1 < B -15 (for all -1 <= d <= 15).
1043                  * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1044                  * Assume B = 0 and we get C > 29.
1045                  */
1046                 ret = __ipv6_addr_src_scope(score->addr_type);
1047                 if (ret >= dst->scope)
1048                         ret = -ret;
1049                 else
1050                         ret -= 128;     /* 30 is enough */
1051                 score->scopedist = ret;
1052                 break;
1053         case IPV6_SADDR_RULE_PREFERRED:
1054                 /* Rule 3: Avoid deprecated and optimistic addresses */
1055                 ret = ipv6_saddr_preferred(score->addr_type) ||
1056                       !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1057                 break;
1058 #ifdef CONFIG_IPV6_MIP6
1059         case IPV6_SADDR_RULE_HOA:
1060             {
1061                 /* Rule 4: Prefer home address */
1062                 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1063                 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1064                 break;
1065             }
1066 #endif
1067         case IPV6_SADDR_RULE_OIF:
1068                 /* Rule 5: Prefer outgoing interface */
1069                 ret = (!dst->ifindex ||
1070                        dst->ifindex == score->ifa->idev->dev->ifindex);
1071                 break;
1072         case IPV6_SADDR_RULE_LABEL:
1073                 /* Rule 6: Prefer matching label */
1074                 ret = ipv6_addr_label(net,
1075                                       &score->ifa->addr, score->addr_type,
1076                                       score->ifa->idev->dev->ifindex) == dst->label;
1077                 break;
1078 #ifdef CONFIG_IPV6_PRIVACY
1079         case IPV6_SADDR_RULE_PRIVACY:
1080             {
1081                 /* Rule 7: Prefer public address
1082                  * Note: prefer temporary address if use_tempaddr >= 2
1083                  */
1084                 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1085                                 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1086                                 score->ifa->idev->cnf.use_tempaddr >= 2;
1087                 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1088                 break;
1089             }
1090 #endif
1091         case IPV6_SADDR_RULE_ORCHID:
1092                 /* Rule 8-: Prefer ORCHID vs ORCHID or
1093                  *          non-ORCHID vs non-ORCHID
1094                  */
1095                 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1096                         ipv6_addr_orchid(dst->addr));
1097                 break;
1098         case IPV6_SADDR_RULE_PREFIX:
1099                 /* Rule 8: Use longest matching prefix */
1100                 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1101                                                        dst->addr);
1102                 break;
1103         default:
1104                 ret = 0;
1105         }
1106
1107         if (ret)
1108                 __set_bit(i, score->scorebits);
1109         score->rule = i;
1110 out:
1111         return ret;
1112 }
1113
1114 int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
1115                        const struct in6_addr *daddr, unsigned int prefs,
1116                        struct in6_addr *saddr)
1117 {
1118         struct ipv6_saddr_score scores[2],
1119                                 *score = &scores[0], *hiscore = &scores[1];
1120         struct ipv6_saddr_dst dst;
1121         struct net_device *dev;
1122         int dst_type;
1123
1124         dst_type = __ipv6_addr_type(daddr);
1125         dst.addr = daddr;
1126         dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1127         dst.scope = __ipv6_addr_src_scope(dst_type);
1128         dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1129         dst.prefs = prefs;
1130
1131         hiscore->rule = -1;
1132         hiscore->ifa = NULL;
1133
1134         rcu_read_lock();
1135
1136         for_each_netdev_rcu(net, dev) {
1137                 struct inet6_dev *idev;
1138
1139                 /* Candidate Source Address (section 4)
1140                  *  - multicast and link-local destination address,
1141                  *    the set of candidate source address MUST only
1142                  *    include addresses assigned to interfaces
1143                  *    belonging to the same link as the outgoing
1144                  *    interface.
1145                  * (- For site-local destination addresses, the
1146                  *    set of candidate source addresses MUST only
1147                  *    include addresses assigned to interfaces
1148                  *    belonging to the same site as the outgoing
1149                  *    interface.)
1150                  */
1151                 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1152                      dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1153                     dst.ifindex && dev->ifindex != dst.ifindex)
1154                         continue;
1155
1156                 idev = __in6_dev_get(dev);
1157                 if (!idev)
1158                         continue;
1159
1160                 read_lock_bh(&idev->lock);
1161                 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1162                         int i;
1163
1164                         /*
1165                          * - Tentative Address (RFC2462 section 5.4)
1166                          *  - A tentative address is not considered
1167                          *    "assigned to an interface" in the traditional
1168                          *    sense, unless it is also flagged as optimistic.
1169                          * - Candidate Source Address (section 4)
1170                          *  - In any case, anycast addresses, multicast
1171                          *    addresses, and the unspecified address MUST
1172                          *    NOT be included in a candidate set.
1173                          */
1174                         if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1175                             (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1176                                 continue;
1177
1178                         score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1179
1180                         if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1181                                      score->addr_type & IPV6_ADDR_MULTICAST)) {
1182                                 LIMIT_NETDEBUG(KERN_DEBUG
1183                                                "ADDRCONF: unspecified / multicast address "
1184                                                "assigned as unicast address on %s",
1185                                                dev->name);
1186                                 continue;
1187                         }
1188
1189                         score->rule = -1;
1190                         bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1191
1192                         for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1193                                 int minihiscore, miniscore;
1194
1195                                 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1196                                 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1197
1198                                 if (minihiscore > miniscore) {
1199                                         if (i == IPV6_SADDR_RULE_SCOPE &&
1200                                             score->scopedist > 0) {
1201                                                 /*
1202                                                  * special case:
1203                                                  * each remaining entry
1204                                                  * has too small (not enough)
1205                                                  * scope, because ifa entries
1206                                                  * are sorted by their scope
1207                                                  * values.
1208                                                  */
1209                                                 goto try_nextdev;
1210                                         }
1211                                         break;
1212                                 } else if (minihiscore < miniscore) {
1213                                         if (hiscore->ifa)
1214                                                 in6_ifa_put(hiscore->ifa);
1215
1216                                         in6_ifa_hold(score->ifa);
1217
1218                                         swap(hiscore, score);
1219
1220                                         /* restore our iterator */
1221                                         score->ifa = hiscore->ifa;
1222
1223                                         break;
1224                                 }
1225                         }
1226                 }
1227 try_nextdev:
1228                 read_unlock_bh(&idev->lock);
1229         }
1230         rcu_read_unlock();
1231
1232         if (!hiscore->ifa)
1233                 return -EADDRNOTAVAIL;
1234
1235         ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1236         in6_ifa_put(hiscore->ifa);
1237         return 0;
1238 }
1239 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1240
1241 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1242                     unsigned char banned_flags)
1243 {
1244         struct inet6_dev *idev;
1245         int err = -EADDRNOTAVAIL;
1246
1247         rcu_read_lock();
1248         idev = __in6_dev_get(dev);
1249         if (idev) {
1250                 struct inet6_ifaddr *ifp;
1251
1252                 read_lock_bh(&idev->lock);
1253                 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1254                         if (ifp->scope == IFA_LINK &&
1255                             !(ifp->flags & banned_flags)) {
1256                                 ipv6_addr_copy(addr, &ifp->addr);
1257                                 err = 0;
1258                                 break;
1259                         }
1260                 }
1261                 read_unlock_bh(&idev->lock);
1262         }
1263         rcu_read_unlock();
1264         return err;
1265 }
1266
1267 static int ipv6_count_addresses(struct inet6_dev *idev)
1268 {
1269         int cnt = 0;
1270         struct inet6_ifaddr *ifp;
1271
1272         read_lock_bh(&idev->lock);
1273         list_for_each_entry(ifp, &idev->addr_list, if_list)
1274                 cnt++;
1275         read_unlock_bh(&idev->lock);
1276         return cnt;
1277 }
1278
1279 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1280                   struct net_device *dev, int strict)
1281 {
1282         struct inet6_ifaddr *ifp;
1283         struct hlist_node *node;
1284         unsigned int hash = ipv6_addr_hash(addr);
1285
1286         rcu_read_lock_bh();
1287         hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1288                 if (!net_eq(dev_net(ifp->idev->dev), net))
1289                         continue;
1290                 if (ipv6_addr_equal(&ifp->addr, addr) &&
1291                     !(ifp->flags&IFA_F_TENTATIVE) &&
1292                     (dev == NULL || ifp->idev->dev == dev ||
1293                      !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1294                         rcu_read_unlock_bh();
1295                         return 1;
1296                 }
1297         }
1298
1299         rcu_read_unlock_bh();
1300         return 0;
1301 }
1302 EXPORT_SYMBOL(ipv6_chk_addr);
1303
1304 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1305                                struct net_device *dev)
1306 {
1307         unsigned int hash = ipv6_addr_hash(addr);
1308         struct inet6_ifaddr *ifp;
1309         struct hlist_node *node;
1310
1311         hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1312                 if (!net_eq(dev_net(ifp->idev->dev), net))
1313                         continue;
1314                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1315                         if (dev == NULL || ifp->idev->dev == dev)
1316                                 return true;
1317                 }
1318         }
1319         return false;
1320 }
1321
1322 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1323 {
1324         struct inet6_dev *idev;
1325         struct inet6_ifaddr *ifa;
1326         int     onlink;
1327
1328         onlink = 0;
1329         rcu_read_lock();
1330         idev = __in6_dev_get(dev);
1331         if (idev) {
1332                 read_lock_bh(&idev->lock);
1333                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1334                         onlink = ipv6_prefix_equal(addr, &ifa->addr,
1335                                                    ifa->prefix_len);
1336                         if (onlink)
1337                                 break;
1338                 }
1339                 read_unlock_bh(&idev->lock);
1340         }
1341         rcu_read_unlock();
1342         return onlink;
1343 }
1344
1345 EXPORT_SYMBOL(ipv6_chk_prefix);
1346
1347 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1348                                      struct net_device *dev, int strict)
1349 {
1350         struct inet6_ifaddr *ifp, *result = NULL;
1351         unsigned int hash = ipv6_addr_hash(addr);
1352         struct hlist_node *node;
1353
1354         rcu_read_lock_bh();
1355         hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1356                 if (!net_eq(dev_net(ifp->idev->dev), net))
1357                         continue;
1358                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1359                         if (dev == NULL || ifp->idev->dev == dev ||
1360                             !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1361                                 result = ifp;
1362                                 in6_ifa_hold(ifp);
1363                                 break;
1364                         }
1365                 }
1366         }
1367         rcu_read_unlock_bh();
1368
1369         return result;
1370 }
1371
1372 /* Gets referenced address, destroys ifaddr */
1373
1374 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1375 {
1376         if (ifp->flags&IFA_F_PERMANENT) {
1377                 spin_lock_bh(&ifp->lock);
1378                 addrconf_del_timer(ifp);
1379                 ifp->flags |= IFA_F_TENTATIVE;
1380                 if (dad_failed)
1381                         ifp->flags |= IFA_F_DADFAILED;
1382                 spin_unlock_bh(&ifp->lock);
1383                 if (dad_failed)
1384                         ipv6_ifa_notify(0, ifp);
1385                 in6_ifa_put(ifp);
1386 #ifdef CONFIG_IPV6_PRIVACY
1387         } else if (ifp->flags&IFA_F_TEMPORARY) {
1388                 struct inet6_ifaddr *ifpub;
1389                 spin_lock_bh(&ifp->lock);
1390                 ifpub = ifp->ifpub;
1391                 if (ifpub) {
1392                         in6_ifa_hold(ifpub);
1393                         spin_unlock_bh(&ifp->lock);
1394                         ipv6_create_tempaddr(ifpub, ifp);
1395                         in6_ifa_put(ifpub);
1396                 } else {
1397                         spin_unlock_bh(&ifp->lock);
1398                 }
1399                 ipv6_del_addr(ifp);
1400 #endif
1401         } else
1402                 ipv6_del_addr(ifp);
1403 }
1404
1405 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1406 {
1407         int err = -ENOENT;
1408
1409         spin_lock(&ifp->state_lock);
1410         if (ifp->state == INET6_IFADDR_STATE_DAD) {
1411                 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1412                 err = 0;
1413         }
1414         spin_unlock(&ifp->state_lock);
1415
1416         return err;
1417 }
1418
1419 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1420 {
1421         struct inet6_dev *idev = ifp->idev;
1422
1423         if (addrconf_dad_end(ifp)) {
1424                 in6_ifa_put(ifp);
1425                 return;
1426         }
1427
1428         if (net_ratelimit())
1429                 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
1430                         ifp->idev->dev->name, &ifp->addr);
1431
1432         if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1433                 struct in6_addr addr;
1434
1435                 addr.s6_addr32[0] = htonl(0xfe800000);
1436                 addr.s6_addr32[1] = 0;
1437
1438                 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1439                     ipv6_addr_equal(&ifp->addr, &addr)) {
1440                         /* DAD failed for link-local based on MAC address */
1441                         idev->cnf.disable_ipv6 = 1;
1442
1443                         printk(KERN_INFO "%s: IPv6 being disabled!\n",
1444                                 ifp->idev->dev->name);
1445                 }
1446         }
1447
1448         addrconf_dad_stop(ifp, 1);
1449 }
1450
1451 /* Join to solicited addr multicast group. */
1452
1453 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1454 {
1455         struct in6_addr maddr;
1456
1457         if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1458                 return;
1459
1460         addrconf_addr_solict_mult(addr, &maddr);
1461         ipv6_dev_mc_inc(dev, &maddr);
1462 }
1463
1464 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1465 {
1466         struct in6_addr maddr;
1467
1468         if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1469                 return;
1470
1471         addrconf_addr_solict_mult(addr, &maddr);
1472         __ipv6_dev_mc_dec(idev, &maddr);
1473 }
1474
1475 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1476 {
1477         struct in6_addr addr;
1478         if (ifp->prefix_len == 127) /* RFC 6164 */
1479                 return;
1480         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1481         if (ipv6_addr_any(&addr))
1482                 return;
1483         ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1484 }
1485
1486 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1487 {
1488         struct in6_addr addr;
1489         if (ifp->prefix_len == 127) /* RFC 6164 */
1490                 return;
1491         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1492         if (ipv6_addr_any(&addr))
1493                 return;
1494         __ipv6_dev_ac_dec(ifp->idev, &addr);
1495 }
1496
1497 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1498 {
1499         if (dev->addr_len != ETH_ALEN)
1500                 return -1;
1501         memcpy(eui, dev->dev_addr, 3);
1502         memcpy(eui + 5, dev->dev_addr + 3, 3);
1503
1504         /*
1505          * The zSeries OSA network cards can be shared among various
1506          * OS instances, but the OSA cards have only one MAC address.
1507          * This leads to duplicate address conflicts in conjunction
1508          * with IPv6 if more than one instance uses the same card.
1509          *
1510          * The driver for these cards can deliver a unique 16-bit
1511          * identifier for each instance sharing the same card.  It is
1512          * placed instead of 0xFFFE in the interface identifier.  The
1513          * "u" bit of the interface identifier is not inverted in this
1514          * case.  Hence the resulting interface identifier has local
1515          * scope according to RFC2373.
1516          */
1517         if (dev->dev_id) {
1518                 eui[3] = (dev->dev_id >> 8) & 0xFF;
1519                 eui[4] = dev->dev_id & 0xFF;
1520         } else {
1521                 eui[3] = 0xFF;
1522                 eui[4] = 0xFE;
1523                 eui[0] ^= 2;
1524         }
1525         return 0;
1526 }
1527
1528 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1529 {
1530         /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1531         if (dev->addr_len != ARCNET_ALEN)
1532                 return -1;
1533         memset(eui, 0, 7);
1534         eui[7] = *(u8*)dev->dev_addr;
1535         return 0;
1536 }
1537
1538 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1539 {
1540         if (dev->addr_len != INFINIBAND_ALEN)
1541                 return -1;
1542         memcpy(eui, dev->dev_addr + 12, 8);
1543         eui[0] |= 2;
1544         return 0;
1545 }
1546
1547 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1548 {
1549         if (addr == 0)
1550                 return -1;
1551         eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1552                   ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1553                   ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1554                   ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1555                   ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1556                   ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1557         eui[1] = 0;
1558         eui[2] = 0x5E;
1559         eui[3] = 0xFE;
1560         memcpy(eui + 4, &addr, 4);
1561         return 0;
1562 }
1563
1564 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1565 {
1566         if (dev->priv_flags & IFF_ISATAP)
1567                 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1568         return -1;
1569 }
1570
1571 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1572 {
1573         return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1574 }
1575
1576 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1577 {
1578         switch (dev->type) {
1579         case ARPHRD_ETHER:
1580         case ARPHRD_FDDI:
1581         case ARPHRD_IEEE802_TR:
1582                 return addrconf_ifid_eui48(eui, dev);
1583         case ARPHRD_ARCNET:
1584                 return addrconf_ifid_arcnet(eui, dev);
1585         case ARPHRD_INFINIBAND:
1586                 return addrconf_ifid_infiniband(eui, dev);
1587         case ARPHRD_SIT:
1588                 return addrconf_ifid_sit(eui, dev);
1589         case ARPHRD_IPGRE:
1590                 return addrconf_ifid_gre(eui, dev);
1591         }
1592         return -1;
1593 }
1594
1595 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1596 {
1597         int err = -1;
1598         struct inet6_ifaddr *ifp;
1599
1600         read_lock_bh(&idev->lock);
1601         list_for_each_entry(ifp, &idev->addr_list, if_list) {
1602                 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1603                         memcpy(eui, ifp->addr.s6_addr+8, 8);
1604                         err = 0;
1605                         break;
1606                 }
1607         }
1608         read_unlock_bh(&idev->lock);
1609         return err;
1610 }
1611
1612 #ifdef CONFIG_IPV6_PRIVACY
1613 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1614 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1615 {
1616 regen:
1617         get_random_bytes(idev->rndid, sizeof(idev->rndid));
1618         idev->rndid[0] &= ~0x02;
1619
1620         /*
1621          * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1622          * check if generated address is not inappropriate
1623          *
1624          *  - Reserved subnet anycast (RFC 2526)
1625          *      11111101 11....11 1xxxxxxx
1626          *  - ISATAP (RFC4214) 6.1
1627          *      00-00-5E-FE-xx-xx-xx-xx
1628          *  - value 0
1629          *  - XXX: already assigned to an address on the device
1630          */
1631         if (idev->rndid[0] == 0xfd &&
1632             (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1633             (idev->rndid[7]&0x80))
1634                 goto regen;
1635         if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1636                 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1637                         goto regen;
1638                 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1639                         goto regen;
1640         }
1641
1642         return 0;
1643 }
1644
1645 static void ipv6_regen_rndid(unsigned long data)
1646 {
1647         struct inet6_dev *idev = (struct inet6_dev *) data;
1648         unsigned long expires;
1649
1650         rcu_read_lock_bh();
1651         write_lock_bh(&idev->lock);
1652
1653         if (idev->dead)
1654                 goto out;
1655
1656         if (__ipv6_regen_rndid(idev) < 0)
1657                 goto out;
1658
1659         expires = jiffies +
1660                 idev->cnf.temp_prefered_lft * HZ -
1661                 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1662                 idev->cnf.max_desync_factor * HZ;
1663         if (time_before(expires, jiffies)) {
1664                 printk(KERN_WARNING
1665                         "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1666                         idev->dev->name);
1667                 goto out;
1668         }
1669
1670         if (!mod_timer(&idev->regen_timer, expires))
1671                 in6_dev_hold(idev);
1672
1673 out:
1674         write_unlock_bh(&idev->lock);
1675         rcu_read_unlock_bh();
1676         in6_dev_put(idev);
1677 }
1678
1679 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1680         int ret = 0;
1681
1682         if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1683                 ret = __ipv6_regen_rndid(idev);
1684         return ret;
1685 }
1686 #endif
1687
1688 /*
1689  *      Add prefix route.
1690  */
1691
1692 static void
1693 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1694                       unsigned long expires, u32 flags)
1695 {
1696         struct fib6_config cfg = {
1697                 .fc_table = RT6_TABLE_PREFIX,
1698                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1699                 .fc_ifindex = dev->ifindex,
1700                 .fc_expires = expires,
1701                 .fc_dst_len = plen,
1702                 .fc_flags = RTF_UP | flags,
1703                 .fc_nlinfo.nl_net = dev_net(dev),
1704                 .fc_protocol = RTPROT_KERNEL,
1705         };
1706
1707         ipv6_addr_copy(&cfg.fc_dst, pfx);
1708
1709         /* Prevent useless cloning on PtP SIT.
1710            This thing is done here expecting that the whole
1711            class of non-broadcast devices need not cloning.
1712          */
1713 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1714         if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1715                 cfg.fc_flags |= RTF_NONEXTHOP;
1716 #endif
1717
1718         ip6_route_add(&cfg);
1719 }
1720
1721
1722 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1723                                                   int plen,
1724                                                   const struct net_device *dev,
1725                                                   u32 flags, u32 noflags)
1726 {
1727         struct fib6_node *fn;
1728         struct rt6_info *rt = NULL;
1729         struct fib6_table *table;
1730
1731         table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1732         if (table == NULL)
1733                 return NULL;
1734
1735         write_lock_bh(&table->tb6_lock);
1736         fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1737         if (!fn)
1738                 goto out;
1739         for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1740                 if (rt->rt6i_dev->ifindex != dev->ifindex)
1741                         continue;
1742                 if ((rt->rt6i_flags & flags) != flags)
1743                         continue;
1744                 if ((noflags != 0) && ((rt->rt6i_flags & flags) != 0))
1745                         continue;
1746                 dst_hold(&rt->dst);
1747                 break;
1748         }
1749 out:
1750         write_unlock_bh(&table->tb6_lock);
1751         return rt;
1752 }
1753
1754
1755 /* Create "default" multicast route to the interface */
1756
1757 static void addrconf_add_mroute(struct net_device *dev)
1758 {
1759         struct fib6_config cfg = {
1760                 .fc_table = RT6_TABLE_LOCAL,
1761                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1762                 .fc_ifindex = dev->ifindex,
1763                 .fc_dst_len = 8,
1764                 .fc_flags = RTF_UP,
1765                 .fc_nlinfo.nl_net = dev_net(dev),
1766         };
1767
1768         ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1769
1770         ip6_route_add(&cfg);
1771 }
1772
1773 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1774 static void sit_route_add(struct net_device *dev)
1775 {
1776         struct fib6_config cfg = {
1777                 .fc_table = RT6_TABLE_MAIN,
1778                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1779                 .fc_ifindex = dev->ifindex,
1780                 .fc_dst_len = 96,
1781                 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1782                 .fc_nlinfo.nl_net = dev_net(dev),
1783         };
1784
1785         /* prefix length - 96 bits "::d.d.d.d" */
1786         ip6_route_add(&cfg);
1787 }
1788 #endif
1789
1790 static void addrconf_add_lroute(struct net_device *dev)
1791 {
1792         struct in6_addr addr;
1793
1794         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
1795         addrconf_prefix_route(&addr, 64, dev, 0, 0);
1796 }
1797
1798 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1799 {
1800         struct inet6_dev *idev;
1801
1802         ASSERT_RTNL();
1803
1804         idev = ipv6_find_idev(dev);
1805         if (!idev)
1806                 return ERR_PTR(-ENOBUFS);
1807
1808         if (idev->cnf.disable_ipv6)
1809                 return ERR_PTR(-EACCES);
1810
1811         /* Add default multicast route */
1812         if (!(dev->flags & IFF_LOOPBACK))
1813                 addrconf_add_mroute(dev);
1814
1815         /* Add link local route */
1816         addrconf_add_lroute(dev);
1817         return idev;
1818 }
1819
1820 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1821 {
1822         struct prefix_info *pinfo;
1823         __u32 valid_lft;
1824         __u32 prefered_lft;
1825         int addr_type;
1826         struct inet6_dev *in6_dev;
1827         struct net *net = dev_net(dev);
1828
1829         pinfo = (struct prefix_info *) opt;
1830
1831         if (len < sizeof(struct prefix_info)) {
1832                 ADBG(("addrconf: prefix option too short\n"));
1833                 return;
1834         }
1835
1836         /*
1837          *      Validation checks ([ADDRCONF], page 19)
1838          */
1839
1840         addr_type = ipv6_addr_type(&pinfo->prefix);
1841
1842         if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1843                 return;
1844
1845         valid_lft = ntohl(pinfo->valid);
1846         prefered_lft = ntohl(pinfo->prefered);
1847
1848         if (prefered_lft > valid_lft) {
1849                 if (net_ratelimit())
1850                         printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1851                 return;
1852         }
1853
1854         in6_dev = in6_dev_get(dev);
1855
1856         if (in6_dev == NULL) {
1857                 if (net_ratelimit())
1858                         printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1859                 return;
1860         }
1861
1862         /*
1863          *      Two things going on here:
1864          *      1) Add routes for on-link prefixes
1865          *      2) Configure prefixes with the auto flag set
1866          */
1867
1868         if (pinfo->onlink) {
1869                 struct rt6_info *rt;
1870                 unsigned long rt_expires;
1871
1872                 /* Avoid arithmetic overflow. Really, we could
1873                  * save rt_expires in seconds, likely valid_lft,
1874                  * but it would require division in fib gc, that it
1875                  * not good.
1876                  */
1877                 if (HZ > USER_HZ)
1878                         rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1879                 else
1880                         rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
1881
1882                 if (addrconf_finite_timeout(rt_expires))
1883                         rt_expires *= HZ;
1884
1885                 rt = addrconf_get_prefix_route(&pinfo->prefix,
1886                                                pinfo->prefix_len,
1887                                                dev,
1888                                                RTF_ADDRCONF | RTF_PREFIX_RT,
1889                                                RTF_GATEWAY | RTF_DEFAULT);
1890
1891                 if (rt) {
1892                         /* Autoconf prefix route */
1893                         if (valid_lft == 0) {
1894                                 ip6_del_rt(rt);
1895                                 rt = NULL;
1896                         } else if (addrconf_finite_timeout(rt_expires)) {
1897                                 /* not infinity */
1898                                 rt->rt6i_expires = jiffies + rt_expires;
1899                                 rt->rt6i_flags |= RTF_EXPIRES;
1900                         } else {
1901                                 rt->rt6i_flags &= ~RTF_EXPIRES;
1902                                 rt->rt6i_expires = 0;
1903                         }
1904                 } else if (valid_lft) {
1905                         clock_t expires = 0;
1906                         int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1907                         if (addrconf_finite_timeout(rt_expires)) {
1908                                 /* not infinity */
1909                                 flags |= RTF_EXPIRES;
1910                                 expires = jiffies_to_clock_t(rt_expires);
1911                         }
1912                         addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1913                                               dev, expires, flags);
1914                 }
1915                 if (rt)
1916                         dst_release(&rt->dst);
1917         }
1918
1919         /* Try to figure out our local address for this prefix */
1920
1921         if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1922                 struct inet6_ifaddr * ifp;
1923                 struct in6_addr addr;
1924                 int create = 0, update_lft = 0;
1925
1926                 if (pinfo->prefix_len == 64) {
1927                         memcpy(&addr, &pinfo->prefix, 8);
1928                         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1929                             ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1930                                 in6_dev_put(in6_dev);
1931                                 return;
1932                         }
1933                         goto ok;
1934                 }
1935                 if (net_ratelimit())
1936                         printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1937                                pinfo->prefix_len);
1938                 in6_dev_put(in6_dev);
1939                 return;
1940
1941 ok:
1942
1943                 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
1944
1945                 if (ifp == NULL && valid_lft) {
1946                         int max_addresses = in6_dev->cnf.max_addresses;
1947                         u32 addr_flags = 0;
1948
1949 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1950                         if (in6_dev->cnf.optimistic_dad &&
1951                             !net->ipv6.devconf_all->forwarding)
1952                                 addr_flags = IFA_F_OPTIMISTIC;
1953 #endif
1954
1955                         /* Do not allow to create too much of autoconfigured
1956                          * addresses; this would be too easy way to crash kernel.
1957                          */
1958                         if (!max_addresses ||
1959                             ipv6_count_addresses(in6_dev) < max_addresses)
1960                                 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1961                                                     addr_type&IPV6_ADDR_SCOPE_MASK,
1962                                                     addr_flags);
1963
1964                         if (!ifp || IS_ERR(ifp)) {
1965                                 in6_dev_put(in6_dev);
1966                                 return;
1967                         }
1968
1969                         update_lft = create = 1;
1970                         ifp->cstamp = jiffies;
1971                         addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1972                 }
1973
1974                 if (ifp) {
1975                         int flags;
1976                         unsigned long now;
1977 #ifdef CONFIG_IPV6_PRIVACY
1978                         struct inet6_ifaddr *ift;
1979 #endif
1980                         u32 stored_lft;
1981
1982                         /* update lifetime (RFC2462 5.5.3 e) */
1983                         spin_lock(&ifp->lock);
1984                         now = jiffies;
1985                         if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1986                                 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1987                         else
1988                                 stored_lft = 0;
1989                         if (!update_lft && stored_lft) {
1990                                 if (valid_lft > MIN_VALID_LIFETIME ||
1991                                     valid_lft > stored_lft)
1992                                         update_lft = 1;
1993                                 else if (stored_lft <= MIN_VALID_LIFETIME) {
1994                                         /* valid_lft <= stored_lft is always true */
1995                                         /*
1996                                          * RFC 4862 Section 5.5.3e:
1997                                          * "Note that the preferred lifetime of
1998                                          *  the corresponding address is always
1999                                          *  reset to the Preferred Lifetime in
2000                                          *  the received Prefix Information
2001                                          *  option, regardless of whether the
2002                                          *  valid lifetime is also reset or
2003                                          *  ignored."
2004                                          *
2005                                          *  So if the preferred lifetime in
2006                                          *  this advertisement is different
2007                                          *  than what we have stored, but the
2008                                          *  valid lifetime is invalid, just
2009                                          *  reset prefered_lft.
2010                                          *
2011                                          *  We must set the valid lifetime
2012                                          *  to the stored lifetime since we'll
2013                                          *  be updating the timestamp below,
2014                                          *  else we'll set it back to the
2015                                          *  minimum.
2016                                          */
2017                                         if (prefered_lft != ifp->prefered_lft) {
2018                                                 valid_lft = stored_lft;
2019                                                 update_lft = 1;
2020                                         }
2021                                 } else {
2022                                         valid_lft = MIN_VALID_LIFETIME;
2023                                         if (valid_lft < prefered_lft)
2024                                                 prefered_lft = valid_lft;
2025                                         update_lft = 1;
2026                                 }
2027                         }
2028
2029                         if (update_lft) {
2030                                 ifp->valid_lft = valid_lft;
2031                                 ifp->prefered_lft = prefered_lft;
2032                                 ifp->tstamp = now;
2033                                 flags = ifp->flags;
2034                                 ifp->flags &= ~IFA_F_DEPRECATED;
2035                                 spin_unlock(&ifp->lock);
2036
2037                                 if (!(flags&IFA_F_TENTATIVE))
2038                                         ipv6_ifa_notify(0, ifp);
2039                         } else
2040                                 spin_unlock(&ifp->lock);
2041
2042 #ifdef CONFIG_IPV6_PRIVACY
2043                         read_lock_bh(&in6_dev->lock);
2044                         /* update all temporary addresses in the list */
2045                         list_for_each_entry(ift, &in6_dev->tempaddr_list,
2046                                             tmp_list) {
2047                                 int age, max_valid, max_prefered;
2048
2049                                 if (ifp != ift->ifpub)
2050                                         continue;
2051
2052                                 /*
2053                                  * RFC 4941 section 3.3:
2054                                  * If a received option will extend the lifetime
2055                                  * of a public address, the lifetimes of
2056                                  * temporary addresses should be extended,
2057                                  * subject to the overall constraint that no
2058                                  * temporary addresses should ever remain
2059                                  * "valid" or "preferred" for a time longer than
2060                                  * (TEMP_VALID_LIFETIME) or
2061                                  * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2062                                  * respectively.
2063                                  */
2064                                 age = (now - ift->cstamp) / HZ;
2065                                 max_valid = in6_dev->cnf.temp_valid_lft - age;
2066                                 if (max_valid < 0)
2067                                         max_valid = 0;
2068
2069                                 max_prefered = in6_dev->cnf.temp_prefered_lft -
2070                                                in6_dev->cnf.max_desync_factor -
2071                                                age;
2072                                 if (max_prefered < 0)
2073                                         max_prefered = 0;
2074
2075                                 if (valid_lft > max_valid)
2076                                         valid_lft = max_valid;
2077
2078                                 if (prefered_lft > max_prefered)
2079                                         prefered_lft = max_prefered;
2080
2081                                 spin_lock(&ift->lock);
2082                                 flags = ift->flags;
2083                                 ift->valid_lft = valid_lft;
2084                                 ift->prefered_lft = prefered_lft;
2085                                 ift->tstamp = now;
2086                                 if (prefered_lft > 0)
2087                                         ift->flags &= ~IFA_F_DEPRECATED;
2088
2089                                 spin_unlock(&ift->lock);
2090                                 if (!(flags&IFA_F_TENTATIVE))
2091                                         ipv6_ifa_notify(0, ift);
2092                         }
2093
2094                         if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
2095                                 /*
2096                                  * When a new public address is created as
2097                                  * described in [ADDRCONF], also create a new
2098                                  * temporary address. Also create a temporary
2099                                  * address if it's enabled but no temporary
2100                                  * address currently exists.
2101                                  */
2102                                 read_unlock_bh(&in6_dev->lock);
2103                                 ipv6_create_tempaddr(ifp, NULL);
2104                         } else {
2105                                 read_unlock_bh(&in6_dev->lock);
2106                         }
2107 #endif
2108                         in6_ifa_put(ifp);
2109                         addrconf_verify(0);
2110                 }
2111         }
2112         inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2113         in6_dev_put(in6_dev);
2114 }
2115
2116 /*
2117  *      Set destination address.
2118  *      Special case for SIT interfaces where we create a new "virtual"
2119  *      device.
2120  */
2121 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2122 {
2123         struct in6_ifreq ireq;
2124         struct net_device *dev;
2125         int err = -EINVAL;
2126
2127         rtnl_lock();
2128
2129         err = -EFAULT;
2130         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2131                 goto err_exit;
2132
2133         dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2134
2135         err = -ENODEV;
2136         if (dev == NULL)
2137                 goto err_exit;
2138
2139 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2140         if (dev->type == ARPHRD_SIT) {
2141                 const struct net_device_ops *ops = dev->netdev_ops;
2142                 struct ifreq ifr;
2143                 struct ip_tunnel_parm p;
2144
2145                 err = -EADDRNOTAVAIL;
2146                 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2147                         goto err_exit;
2148
2149                 memset(&p, 0, sizeof(p));
2150                 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2151                 p.iph.saddr = 0;
2152                 p.iph.version = 4;
2153                 p.iph.ihl = 5;
2154                 p.iph.protocol = IPPROTO_IPV6;
2155                 p.iph.ttl = 64;
2156                 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2157
2158                 if (ops->ndo_do_ioctl) {
2159                         mm_segment_t oldfs = get_fs();
2160
2161                         set_fs(KERNEL_DS);
2162                         err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2163                         set_fs(oldfs);
2164                 } else
2165                         err = -EOPNOTSUPP;
2166
2167                 if (err == 0) {
2168                         err = -ENOBUFS;
2169                         dev = __dev_get_by_name(net, p.name);
2170                         if (!dev)
2171                                 goto err_exit;
2172                         err = dev_open(dev);
2173                 }
2174         }
2175 #endif
2176
2177 err_exit:
2178         rtnl_unlock();
2179         return err;
2180 }
2181
2182 /*
2183  *      Manual configuration of address on an interface
2184  */
2185 static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2186                           unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2187                           __u32 valid_lft)
2188 {
2189         struct inet6_ifaddr *ifp;
2190         struct inet6_dev *idev;
2191         struct net_device *dev;
2192         int scope;
2193         u32 flags;
2194         clock_t expires;
2195         unsigned long timeout;
2196
2197         ASSERT_RTNL();
2198
2199         if (plen > 128)
2200                 return -EINVAL;
2201
2202         /* check the lifetime */
2203         if (!valid_lft || prefered_lft > valid_lft)
2204                 return -EINVAL;
2205
2206         dev = __dev_get_by_index(net, ifindex);
2207         if (!dev)
2208                 return -ENODEV;
2209
2210         idev = addrconf_add_dev(dev);
2211         if (IS_ERR(idev))
2212                 return PTR_ERR(idev);
2213
2214         scope = ipv6_addr_scope(pfx);
2215
2216         timeout = addrconf_timeout_fixup(valid_lft, HZ);
2217         if (addrconf_finite_timeout(timeout)) {
2218                 expires = jiffies_to_clock_t(timeout * HZ);
2219                 valid_lft = timeout;
2220                 flags = RTF_EXPIRES;
2221         } else {
2222                 expires = 0;
2223                 flags = 0;
2224                 ifa_flags |= IFA_F_PERMANENT;
2225         }
2226
2227         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2228         if (addrconf_finite_timeout(timeout)) {
2229                 if (timeout == 0)
2230                         ifa_flags |= IFA_F_DEPRECATED;
2231                 prefered_lft = timeout;
2232         }
2233
2234         ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2235
2236         if (!IS_ERR(ifp)) {
2237                 spin_lock_bh(&ifp->lock);
2238                 ifp->valid_lft = valid_lft;
2239                 ifp->prefered_lft = prefered_lft;
2240                 ifp->tstamp = jiffies;
2241                 spin_unlock_bh(&ifp->lock);
2242
2243                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2244                                       expires, flags);
2245                 /*
2246                  * Note that section 3.1 of RFC 4429 indicates
2247                  * that the Optimistic flag should not be set for
2248                  * manually configured addresses
2249                  */
2250                 addrconf_dad_start(ifp, 0);
2251                 in6_ifa_put(ifp);
2252                 addrconf_verify(0);
2253                 return 0;
2254         }
2255
2256         return PTR_ERR(ifp);
2257 }
2258
2259 static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
2260                           unsigned int plen)
2261 {
2262         struct inet6_ifaddr *ifp;
2263         struct inet6_dev *idev;
2264         struct net_device *dev;
2265
2266         if (plen > 128)
2267                 return -EINVAL;
2268
2269         dev = __dev_get_by_index(net, ifindex);
2270         if (!dev)
2271                 return -ENODEV;
2272
2273         if ((idev = __in6_dev_get(dev)) == NULL)
2274                 return -ENXIO;
2275
2276         read_lock_bh(&idev->lock);
2277         list_for_each_entry(ifp, &idev->addr_list, if_list) {
2278                 if (ifp->prefix_len == plen &&
2279                     ipv6_addr_equal(pfx, &ifp->addr)) {
2280                         in6_ifa_hold(ifp);
2281                         read_unlock_bh(&idev->lock);
2282
2283                         ipv6_del_addr(ifp);
2284
2285                         /* If the last address is deleted administratively,
2286                            disable IPv6 on this interface.
2287                          */
2288                         if (list_empty(&idev->addr_list))
2289                                 addrconf_ifdown(idev->dev, 1);
2290                         return 0;
2291                 }
2292         }
2293         read_unlock_bh(&idev->lock);
2294         return -EADDRNOTAVAIL;
2295 }
2296
2297
2298 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2299 {
2300         struct in6_ifreq ireq;
2301         int err;
2302
2303         if (!capable(CAP_NET_ADMIN))
2304                 return -EPERM;
2305
2306         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2307                 return -EFAULT;
2308
2309         rtnl_lock();
2310         err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2311                              ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2312                              INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2313         rtnl_unlock();
2314         return err;
2315 }
2316
2317 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2318 {
2319         struct in6_ifreq ireq;
2320         int err;
2321
2322         if (!capable(CAP_NET_ADMIN))
2323                 return -EPERM;
2324
2325         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2326                 return -EFAULT;
2327
2328         rtnl_lock();
2329         err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2330                              ireq.ifr6_prefixlen);
2331         rtnl_unlock();
2332         return err;
2333 }
2334
2335 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2336                      int plen, int scope)
2337 {
2338         struct inet6_ifaddr *ifp;
2339
2340         ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2341         if (!IS_ERR(ifp)) {
2342                 spin_lock_bh(&ifp->lock);
2343                 ifp->flags &= ~IFA_F_TENTATIVE;
2344                 spin_unlock_bh(&ifp->lock);
2345                 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2346                 in6_ifa_put(ifp);
2347         }
2348 }
2349
2350 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2351 static void sit_add_v4_addrs(struct inet6_dev *idev)
2352 {
2353         struct in6_addr addr;
2354         struct net_device *dev;
2355         struct net *net = dev_net(idev->dev);
2356         int scope;
2357
2358         ASSERT_RTNL();
2359
2360         memset(&addr, 0, sizeof(struct in6_addr));
2361         memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2362
2363         if (idev->dev->flags&IFF_POINTOPOINT) {
2364                 addr.s6_addr32[0] = htonl(0xfe800000);
2365                 scope = IFA_LINK;
2366         } else {
2367                 scope = IPV6_ADDR_COMPATv4;
2368         }
2369
2370         if (addr.s6_addr32[3]) {
2371                 add_addr(idev, &addr, 128, scope);
2372                 return;
2373         }
2374
2375         for_each_netdev(net, dev) {
2376                 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2377                 if (in_dev && (dev->flags & IFF_UP)) {
2378                         struct in_ifaddr * ifa;
2379
2380                         int flag = scope;
2381
2382                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2383                                 int plen;
2384
2385                                 addr.s6_addr32[3] = ifa->ifa_local;
2386
2387                                 if (ifa->ifa_scope == RT_SCOPE_LINK)
2388                                         continue;
2389                                 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2390                                         if (idev->dev->flags&IFF_POINTOPOINT)
2391                                                 continue;
2392                                         flag |= IFA_HOST;
2393                                 }
2394                                 if (idev->dev->flags&IFF_POINTOPOINT)
2395                                         plen = 64;
2396                                 else
2397                                         plen = 96;
2398
2399                                 add_addr(idev, &addr, plen, flag);
2400                         }
2401                 }
2402         }
2403 }
2404 #endif
2405
2406 static void init_loopback(struct net_device *dev)
2407 {
2408         struct inet6_dev  *idev;
2409
2410         /* ::1 */
2411
2412         ASSERT_RTNL();
2413
2414         if ((idev = ipv6_find_idev(dev)) == NULL) {
2415                 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2416                 return;
2417         }
2418
2419         add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2420 }
2421
2422 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2423 {
2424         struct inet6_ifaddr * ifp;
2425         u32 addr_flags = IFA_F_PERMANENT;
2426
2427 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2428         if (idev->cnf.optimistic_dad &&
2429             !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2430                 addr_flags |= IFA_F_OPTIMISTIC;
2431 #endif
2432
2433
2434         ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2435         if (!IS_ERR(ifp)) {
2436                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2437                 addrconf_dad_start(ifp, 0);
2438                 in6_ifa_put(ifp);
2439         }
2440 }
2441
2442 static void addrconf_dev_config(struct net_device *dev)
2443 {
2444         struct in6_addr addr;
2445         struct inet6_dev    * idev;
2446
2447         ASSERT_RTNL();
2448
2449         if ((dev->type != ARPHRD_ETHER) &&
2450             (dev->type != ARPHRD_FDDI) &&
2451             (dev->type != ARPHRD_IEEE802_TR) &&
2452             (dev->type != ARPHRD_ARCNET) &&
2453             (dev->type != ARPHRD_INFINIBAND)) {
2454                 /* Alas, we support only Ethernet autoconfiguration. */
2455                 return;
2456         }
2457
2458         idev = addrconf_add_dev(dev);
2459         if (IS_ERR(idev))
2460                 return;
2461
2462         memset(&addr, 0, sizeof(struct in6_addr));
2463         addr.s6_addr32[0] = htonl(0xFE800000);
2464
2465         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2466                 addrconf_add_linklocal(idev, &addr);
2467 }
2468
2469 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2470 static void addrconf_sit_config(struct net_device *dev)
2471 {
2472         struct inet6_dev *idev;
2473
2474         ASSERT_RTNL();
2475
2476         /*
2477          * Configure the tunnel with one of our IPv4
2478          * addresses... we should configure all of
2479          * our v4 addrs in the tunnel
2480          */
2481
2482         if ((idev = ipv6_find_idev(dev)) == NULL) {
2483                 printk(KERN_DEBUG "init sit: add_dev failed\n");
2484                 return;
2485         }
2486
2487         if (dev->priv_flags & IFF_ISATAP) {
2488                 struct in6_addr addr;
2489
2490                 ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2491                 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2492                 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2493                         addrconf_add_linklocal(idev, &addr);
2494                 return;
2495         }
2496
2497         sit_add_v4_addrs(idev);
2498
2499         if (dev->flags&IFF_POINTOPOINT) {
2500                 addrconf_add_mroute(dev);
2501                 addrconf_add_lroute(dev);
2502         } else
2503                 sit_route_add(dev);
2504 }
2505 #endif
2506
2507 #if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2508 static void addrconf_gre_config(struct net_device *dev)
2509 {
2510         struct inet6_dev *idev;
2511         struct in6_addr addr;
2512
2513         pr_info("ipv6: addrconf_gre_config(%s)\n", dev->name);
2514
2515         ASSERT_RTNL();
2516
2517         if ((idev = ipv6_find_idev(dev)) == NULL) {
2518                 printk(KERN_DEBUG "init gre: add_dev failed\n");
2519                 return;
2520         }
2521
2522         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2523         addrconf_prefix_route(&addr, 64, dev, 0, 0);
2524
2525         if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2526                 addrconf_add_linklocal(idev, &addr);
2527 }
2528 #endif
2529
2530 static inline int
2531 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2532 {
2533         struct in6_addr lladdr;
2534
2535         if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2536                 addrconf_add_linklocal(idev, &lladdr);
2537                 return 0;
2538         }
2539         return -1;
2540 }
2541
2542 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2543 {
2544         struct net_device *link_dev;
2545         struct net *net = dev_net(idev->dev);
2546
2547         /* first try to inherit the link-local address from the link device */
2548         if (idev->dev->iflink &&
2549             (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2550                 if (!ipv6_inherit_linklocal(idev, link_dev))
2551                         return;
2552         }
2553         /* then try to inherit it from any device */
2554         for_each_netdev(net, link_dev) {
2555                 if (!ipv6_inherit_linklocal(idev, link_dev))
2556                         return;
2557         }
2558         printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2559 }
2560
2561 /*
2562  * Autoconfigure tunnel with a link-local address so routing protocols,
2563  * DHCPv6, MLD etc. can be run over the virtual link
2564  */
2565
2566 static void addrconf_ip6_tnl_config(struct net_device *dev)
2567 {
2568         struct inet6_dev *idev;
2569
2570         ASSERT_RTNL();
2571
2572         idev = addrconf_add_dev(dev);
2573         if (IS_ERR(idev)) {
2574                 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2575                 return;
2576         }
2577         ip6_tnl_add_linklocal(idev);
2578 }
2579
2580 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2581                            void * data)
2582 {
2583         struct net_device *dev = (struct net_device *) data;
2584         struct inet6_dev *idev = __in6_dev_get(dev);
2585         int run_pending = 0;
2586         int err;
2587
2588         switch (event) {
2589         case NETDEV_REGISTER:
2590                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2591                         idev = ipv6_add_dev(dev);
2592                         if (!idev)
2593                                 return notifier_from_errno(-ENOMEM);
2594                 }
2595                 break;
2596
2597         case NETDEV_UP:
2598         case NETDEV_CHANGE:
2599                 if (dev->flags & IFF_SLAVE)
2600                         break;
2601
2602                 if (event == NETDEV_UP) {
2603                         if (!addrconf_qdisc_ok(dev)) {
2604                                 /* device is not ready yet. */
2605                                 printk(KERN_INFO
2606                                         "ADDRCONF(NETDEV_UP): %s: "
2607                                         "link is not ready\n",
2608                                         dev->name);
2609                                 break;
2610                         }
2611
2612                         if (!idev && dev->mtu >= IPV6_MIN_MTU)
2613                                 idev = ipv6_add_dev(dev);
2614
2615                         if (idev) {
2616                                 idev->if_flags |= IF_READY;
2617                                 run_pending = 1;
2618                         }
2619                 } else {
2620                         if (!addrconf_qdisc_ok(dev)) {
2621                                 /* device is still not ready. */
2622                                 break;
2623                         }
2624
2625                         if (idev) {
2626                                 if (idev->if_flags & IF_READY)
2627                                         /* device is already configured. */
2628                                         break;
2629                                 idev->if_flags |= IF_READY;
2630                         }
2631
2632                         printk(KERN_INFO
2633                                         "ADDRCONF(NETDEV_CHANGE): %s: "
2634                                         "link becomes ready\n",
2635                                         dev->name);
2636
2637                         run_pending = 1;
2638                 }
2639
2640                 switch (dev->type) {
2641 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2642                 case ARPHRD_SIT:
2643                         addrconf_sit_config(dev);
2644                         break;
2645 #endif
2646 #if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2647                 case ARPHRD_IPGRE:
2648                         addrconf_gre_config(dev);
2649                         break;
2650 #endif
2651                 case ARPHRD_TUNNEL6:
2652                         addrconf_ip6_tnl_config(dev);
2653                         break;
2654                 case ARPHRD_LOOPBACK:
2655                         init_loopback(dev);
2656                         break;
2657
2658                 default:
2659                         addrconf_dev_config(dev);
2660                         break;
2661                 }
2662
2663                 if (idev) {
2664                         if (run_pending)
2665                                 addrconf_dad_run(idev);
2666
2667                         /*
2668                          * If the MTU changed during the interface down,
2669                          * when the interface up, the changed MTU must be
2670                          * reflected in the idev as well as routers.
2671                          */
2672                         if (idev->cnf.mtu6 != dev->mtu &&
2673                             dev->mtu >= IPV6_MIN_MTU) {
2674                                 rt6_mtu_change(dev, dev->mtu);
2675                                 idev->cnf.mtu6 = dev->mtu;
2676                         }
2677                         idev->tstamp = jiffies;
2678                         inet6_ifinfo_notify(RTM_NEWLINK, idev);
2679
2680                         /*
2681                          * If the changed mtu during down is lower than
2682                          * IPV6_MIN_MTU stop IPv6 on this interface.
2683                          */
2684                         if (dev->mtu < IPV6_MIN_MTU)
2685                                 addrconf_ifdown(dev, 1);
2686                 }
2687                 break;
2688
2689         case NETDEV_CHANGEMTU:
2690                 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2691                         rt6_mtu_change(dev, dev->mtu);
2692                         idev->cnf.mtu6 = dev->mtu;
2693                         break;
2694                 }
2695
2696                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2697                         idev = ipv6_add_dev(dev);
2698                         if (idev)
2699                                 break;
2700                 }
2701
2702                 /*
2703                  * MTU falled under IPV6_MIN_MTU.
2704                  * Stop IPv6 on this interface.
2705                  */
2706
2707         case NETDEV_DOWN:
2708         case NETDEV_UNREGISTER:
2709                 /*
2710                  *      Remove all addresses from this interface.
2711                  */
2712                 addrconf_ifdown(dev, event != NETDEV_DOWN);
2713                 break;
2714
2715         case NETDEV_CHANGENAME:
2716                 if (idev) {
2717                         snmp6_unregister_dev(idev);
2718                         addrconf_sysctl_unregister(idev);
2719                         addrconf_sysctl_register(idev);
2720                         err = snmp6_register_dev(idev);
2721                         if (err)
2722                                 return notifier_from_errno(err);
2723                 }
2724                 break;
2725
2726         case NETDEV_PRE_TYPE_CHANGE:
2727         case NETDEV_POST_TYPE_CHANGE:
2728                 addrconf_type_change(dev, event);
2729                 break;
2730         }
2731
2732         return NOTIFY_OK;
2733 }
2734
2735 /*
2736  *      addrconf module should be notified of a device going up
2737  */
2738 static struct notifier_block ipv6_dev_notf = {
2739         .notifier_call = addrconf_notify,
2740 };
2741
2742 static void addrconf_type_change(struct net_device *dev, unsigned long event)
2743 {
2744         struct inet6_dev *idev;
2745         ASSERT_RTNL();
2746
2747         idev = __in6_dev_get(dev);
2748
2749         if (event == NETDEV_POST_TYPE_CHANGE)
2750                 ipv6_mc_remap(idev);
2751         else if (event == NETDEV_PRE_TYPE_CHANGE)
2752                 ipv6_mc_unmap(idev);
2753 }
2754
2755 static int addrconf_ifdown(struct net_device *dev, int how)
2756 {
2757         struct net *net = dev_net(dev);
2758         struct inet6_dev *idev;
2759         struct inet6_ifaddr *ifa;
2760         int state, i;
2761
2762         ASSERT_RTNL();
2763
2764         rt6_ifdown(net, dev);
2765         neigh_ifdown(&nd_tbl, dev);
2766
2767         idev = __in6_dev_get(dev);
2768         if (idev == NULL)
2769                 return -ENODEV;
2770
2771         /*
2772          * Step 1: remove reference to ipv6 device from parent device.
2773          *         Do not dev_put!
2774          */
2775         if (how) {
2776                 idev->dead = 1;
2777
2778                 /* protected by rtnl_lock */
2779                 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
2780
2781                 /* Step 1.5: remove snmp6 entry */
2782                 snmp6_unregister_dev(idev);
2783
2784         }
2785
2786         /* Step 2: clear hash table */
2787         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2788                 struct hlist_head *h = &inet6_addr_lst[i];
2789                 struct hlist_node *n;
2790
2791                 spin_lock_bh(&addrconf_hash_lock);
2792         restart:
2793                 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2794                         if (ifa->idev == idev) {
2795                                 hlist_del_init_rcu(&ifa->addr_lst);
2796                                 addrconf_del_timer(ifa);
2797                                 goto restart;
2798                         }
2799                 }
2800                 spin_unlock_bh(&addrconf_hash_lock);
2801         }
2802
2803         write_lock_bh(&idev->lock);
2804
2805         /* Step 2: clear flags for stateless addrconf */
2806         if (!how)
2807                 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2808
2809 #ifdef CONFIG_IPV6_PRIVACY
2810         if (how && del_timer(&idev->regen_timer))
2811                 in6_dev_put(idev);
2812
2813         /* Step 3: clear tempaddr list */
2814         while (!list_empty(&idev->tempaddr_list)) {
2815                 ifa = list_first_entry(&idev->tempaddr_list,
2816                                        struct inet6_ifaddr, tmp_list);
2817                 list_del(&ifa->tmp_list);
2818                 write_unlock_bh(&idev->lock);
2819                 spin_lock_bh(&ifa->lock);
2820
2821                 if (ifa->ifpub) {
2822                         in6_ifa_put(ifa->ifpub);
2823                         ifa->ifpub = NULL;
2824                 }
2825                 spin_unlock_bh(&ifa->lock);
2826                 in6_ifa_put(ifa);
2827                 write_lock_bh(&idev->lock);
2828         }
2829 #endif
2830
2831         while (!list_empty(&idev->addr_list)) {
2832                 ifa = list_first_entry(&idev->addr_list,
2833                                        struct inet6_ifaddr, if_list);
2834                 addrconf_del_timer(ifa);
2835
2836                 list_del(&ifa->if_list);
2837
2838                 write_unlock_bh(&idev->lock);
2839
2840                 spin_lock_bh(&ifa->state_lock);
2841                 state = ifa->state;
2842                 ifa->state = INET6_IFADDR_STATE_DEAD;
2843                 spin_unlock_bh(&ifa->state_lock);
2844
2845                 if (state != INET6_IFADDR_STATE_DEAD) {
2846                         __ipv6_ifa_notify(RTM_DELADDR, ifa);
2847                         atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
2848                 }
2849                 in6_ifa_put(ifa);
2850
2851                 write_lock_bh(&idev->lock);
2852         }
2853
2854         write_unlock_bh(&idev->lock);
2855
2856         /* Step 5: Discard multicast list */
2857         if (how)
2858                 ipv6_mc_destroy_dev(idev);
2859         else
2860                 ipv6_mc_down(idev);
2861
2862         idev->tstamp = jiffies;
2863
2864         /* Last: Shot the device (if unregistered) */
2865         if (how) {
2866                 addrconf_sysctl_unregister(idev);
2867                 neigh_parms_release(&nd_tbl, idev->nd_parms);
2868                 neigh_ifdown(&nd_tbl, dev);
2869                 in6_dev_put(idev);
2870         }
2871         return 0;
2872 }
2873
2874 static void addrconf_rs_timer(unsigned long data)
2875 {
2876         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2877         struct inet6_dev *idev = ifp->idev;
2878
2879         read_lock(&idev->lock);
2880         if (idev->dead || !(idev->if_flags & IF_READY))
2881                 goto out;
2882
2883         if (idev->cnf.forwarding)
2884                 goto out;
2885
2886         /* Announcement received after solicitation was sent */
2887         if (idev->if_flags & IF_RA_RCVD)
2888                 goto out;
2889
2890         spin_lock(&ifp->lock);
2891         if (ifp->probes++ < idev->cnf.rtr_solicits) {
2892                 /* The wait after the last probe can be shorter */
2893                 addrconf_mod_timer(ifp, AC_RS,
2894                                    (ifp->probes == idev->cnf.rtr_solicits) ?
2895                                    idev->cnf.rtr_solicit_delay :
2896                                    idev->cnf.rtr_solicit_interval);
2897                 spin_unlock(&ifp->lock);
2898
2899                 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
2900         } else {
2901                 spin_unlock(&ifp->lock);
2902                 /*
2903                  * Note: we do not support deprecated "all on-link"
2904                  * assumption any longer.
2905                  */
2906                 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2907                        idev->dev->name);
2908         }
2909
2910 out:
2911         read_unlock(&idev->lock);
2912         in6_ifa_put(ifp);
2913 }
2914
2915 /*
2916  *      Duplicate Address Detection
2917  */
2918 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2919 {
2920         unsigned long rand_num;
2921         struct inet6_dev *idev = ifp->idev;
2922
2923         if (ifp->flags & IFA_F_OPTIMISTIC)
2924                 rand_num = 0;
2925         else
2926                 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2927
2928         ifp->probes = idev->cnf.dad_transmits;
2929         addrconf_mod_timer(ifp, AC_DAD, rand_num);
2930 }
2931
2932 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2933 {
2934         struct inet6_dev *idev = ifp->idev;
2935         struct net_device *dev = idev->dev;
2936
2937         addrconf_join_solict(dev, &ifp->addr);
2938
2939         net_srandom(ifp->addr.s6_addr32[3]);
2940
2941         read_lock_bh(&idev->lock);
2942         spin_lock(&ifp->lock);
2943         if (ifp->state == INET6_IFADDR_STATE_DEAD)
2944                 goto out;
2945
2946         if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2947             idev->cnf.accept_dad < 1 ||
2948             !(ifp->flags&IFA_F_TENTATIVE) ||
2949             ifp->flags & IFA_F_NODAD) {
2950                 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
2951                 spin_unlock(&ifp->lock);
2952                 read_unlock_bh(&idev->lock);
2953
2954                 addrconf_dad_completed(ifp);
2955                 return;
2956         }
2957
2958         if (!(idev->if_flags & IF_READY)) {
2959                 spin_unlock(&ifp->lock);
2960                 read_unlock_bh(&idev->lock);
2961                 /*
2962                  * If the device is not ready:
2963                  * - keep it tentative if it is a permanent address.
2964                  * - otherwise, kill it.
2965                  */
2966                 in6_ifa_hold(ifp);
2967                 addrconf_dad_stop(ifp, 0);
2968                 return;
2969         }
2970
2971         /*
2972          * Optimistic nodes can start receiving
2973          * Frames right away
2974          */
2975         if (ifp->flags & IFA_F_OPTIMISTIC)
2976                 ip6_ins_rt(ifp->rt);
2977
2978         addrconf_dad_kick(ifp);
2979 out:
2980         spin_unlock(&ifp->lock);
2981         read_unlock_bh(&idev->lock);
2982 }
2983
2984 static void addrconf_dad_timer(unsigned long data)
2985 {
2986         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2987         struct inet6_dev *idev = ifp->idev;
2988         struct in6_addr mcaddr;
2989
2990         if (!ifp->probes && addrconf_dad_end(ifp))
2991                 goto out;
2992
2993         read_lock(&idev->lock);
2994         if (idev->dead || !(idev->if_flags & IF_READY)) {
2995                 read_unlock(&idev->lock);
2996                 goto out;
2997         }
2998
2999         spin_lock(&ifp->lock);
3000         if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3001                 spin_unlock(&ifp->lock);
3002                 read_unlock(&idev->lock);
3003                 goto out;
3004         }
3005
3006         if (ifp->probes == 0) {
3007                 /*
3008                  * DAD was successful
3009                  */
3010
3011                 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3012                 spin_unlock(&ifp->lock);
3013                 read_unlock(&idev->lock);
3014
3015                 addrconf_dad_completed(ifp);
3016
3017                 goto out;
3018         }
3019
3020         ifp->probes--;
3021         addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
3022         spin_unlock(&ifp->lock);
3023         read_unlock(&idev->lock);
3024
3025         /* send a neighbour solicitation for our addr */
3026         addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3027         ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3028 out:
3029         in6_ifa_put(ifp);
3030 }
3031
3032 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3033 {
3034         struct net_device *dev = ifp->idev->dev;
3035
3036         /*
3037          *      Configure the address for reception. Now it is valid.
3038          */
3039
3040         ipv6_ifa_notify(RTM_NEWADDR, ifp);
3041
3042         /* If added prefix is link local and we are prepared to process
3043            router advertisements, start sending router solicitations.
3044          */
3045
3046         if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) ||
3047              ifp->idev->cnf.accept_ra == 2) &&
3048             ifp->idev->cnf.rtr_solicits > 0 &&
3049             (dev->flags&IFF_LOOPBACK) == 0 &&
3050             (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
3051                 /*
3052                  *      If a host as already performed a random delay
3053                  *      [...] as part of DAD [...] there is no need
3054                  *      to delay again before sending the first RS
3055                  */
3056                 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
3057
3058                 spin_lock_bh(&ifp->lock);
3059                 ifp->probes = 1;
3060                 ifp->idev->if_flags |= IF_RS_SENT;
3061                 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3062                 spin_unlock_bh(&ifp->lock);
3063         }
3064 }
3065
3066 static void addrconf_dad_run(struct inet6_dev *idev)
3067 {
3068         struct inet6_ifaddr *ifp;
3069
3070         read_lock_bh(&idev->lock);
3071         list_for_each_entry(ifp, &idev->addr_list, if_list) {
3072                 spin_lock(&ifp->lock);
3073                 if (ifp->flags & IFA_F_TENTATIVE &&
3074                     ifp->state == INET6_IFADDR_STATE_DAD)
3075                         addrconf_dad_kick(ifp);
3076                 spin_unlock(&ifp->lock);
3077         }
3078         read_unlock_bh(&idev->lock);
3079 }
3080
3081 #ifdef CONFIG_PROC_FS
3082 struct if6_iter_state {
3083         struct seq_net_private p;
3084         int bucket;
3085 };
3086
3087 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
3088 {
3089         struct inet6_ifaddr *ifa = NULL;
3090         struct if6_iter_state *state = seq->private;
3091         struct net *net = seq_file_net(seq);
3092
3093         for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3094                 struct hlist_node *n;
3095                 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
3096                                          addr_lst)
3097                         if (net_eq(dev_net(ifa->idev->dev), net))
3098                                 return ifa;
3099         }
3100         return NULL;
3101 }
3102
3103 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3104                                          struct inet6_ifaddr *ifa)
3105 {
3106         struct if6_iter_state *state = seq->private;
3107         struct net *net = seq_file_net(seq);
3108         struct hlist_node *n = &ifa->addr_lst;
3109
3110         hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst)
3111                 if (net_eq(dev_net(ifa->idev->dev), net))
3112                         return ifa;
3113
3114         while (++state->bucket < IN6_ADDR_HSIZE) {
3115                 hlist_for_each_entry_rcu_bh(ifa, n,
3116                                      &inet6_addr_lst[state->bucket], addr_lst) {
3117                         if (net_eq(dev_net(ifa->idev->dev), net))
3118                                 return ifa;
3119                 }
3120         }
3121
3122         return NULL;
3123 }
3124
3125 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
3126 {
3127         struct inet6_ifaddr *ifa = if6_get_first(seq);
3128
3129         if (ifa)
3130                 while (pos && (ifa = if6_get_next(seq, ifa)) != NULL)
3131                         --pos;
3132         return pos ? NULL : ifa;
3133 }
3134
3135 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3136         __acquires(rcu_bh)
3137 {
3138         rcu_read_lock_bh();
3139         return if6_get_idx(seq, *pos);
3140 }
3141
3142 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3143 {
3144         struct inet6_ifaddr *ifa;
3145
3146         ifa = if6_get_next(seq, v);
3147         ++*pos;
3148         return ifa;
3149 }
3150
3151 static void if6_seq_stop(struct seq_file *seq, void *v)
3152         __releases(rcu_bh)
3153 {
3154         rcu_read_unlock_bh();
3155 }
3156
3157 static int if6_seq_show(struct seq_file *seq, void *v)
3158 {
3159         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3160         seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3161                    &ifp->addr,
3162                    ifp->idev->dev->ifindex,
3163                    ifp->prefix_len,
3164                    ifp->scope,
3165                    ifp->flags,
3166                    ifp->idev->dev->name);
3167         return 0;
3168 }
3169
3170 static const struct seq_operations if6_seq_ops = {
3171         .start  = if6_seq_start,
3172         .next   = if6_seq_next,
3173         .show   = if6_seq_show,
3174         .stop   = if6_seq_stop,
3175 };
3176
3177 static int if6_seq_open(struct inode *inode, struct file *file)
3178 {
3179         return seq_open_net(inode, file, &if6_seq_ops,
3180                             sizeof(struct if6_iter_state));
3181 }
3182
3183 static const struct file_operations if6_fops = {
3184         .owner          = THIS_MODULE,
3185         .open           = if6_seq_open,
3186         .read           = seq_read,
3187         .llseek         = seq_lseek,
3188         .release        = seq_release_net,
3189 };
3190
3191 static int __net_init if6_proc_net_init(struct net *net)
3192 {
3193         if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
3194                 return -ENOMEM;
3195         return 0;
3196 }
3197
3198 static void __net_exit if6_proc_net_exit(struct net *net)
3199 {
3200        proc_net_remove(net, "if_inet6");
3201 }
3202
3203 static struct pernet_operations if6_proc_net_ops = {
3204        .init = if6_proc_net_init,
3205        .exit = if6_proc_net_exit,
3206 };
3207
3208 int __init if6_proc_init(void)
3209 {
3210         return register_pernet_subsys(&if6_proc_net_ops);
3211 }
3212
3213 void if6_proc_exit(void)
3214 {
3215         unregister_pernet_subsys(&if6_proc_net_ops);
3216 }
3217 #endif  /* CONFIG_PROC_FS */
3218
3219 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
3220 /* Check if address is a home address configured on any interface. */
3221 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3222 {
3223         int ret = 0;
3224         struct inet6_ifaddr *ifp = NULL;
3225         struct hlist_node *n;
3226         unsigned int hash = ipv6_addr_hash(addr);
3227
3228         rcu_read_lock_bh();
3229         hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
3230                 if (!net_eq(dev_net(ifp->idev->dev), net))
3231                         continue;
3232                 if (ipv6_addr_equal(&ifp->addr, addr) &&
3233                     (ifp->flags & IFA_F_HOMEADDRESS)) {
3234                         ret = 1;
3235                         break;
3236                 }
3237         }
3238         rcu_read_unlock_bh();
3239         return ret;
3240 }
3241 #endif
3242
3243 /*
3244  *      Periodic address status verification
3245  */
3246
3247 static void addrconf_verify(unsigned long foo)
3248 {
3249         unsigned long now, next, next_sec, next_sched;
3250         struct inet6_ifaddr *ifp;
3251         struct hlist_node *node;
3252         int i;
3253
3254         rcu_read_lock_bh();
3255         spin_lock(&addrconf_verify_lock);
3256         now = jiffies;
3257         next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3258
3259         del_timer(&addr_chk_timer);
3260
3261         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3262 restart:
3263                 hlist_for_each_entry_rcu_bh(ifp, node,
3264                                          &inet6_addr_lst[i], addr_lst) {
3265                         unsigned long age;
3266
3267                         if (ifp->flags & IFA_F_PERMANENT)
3268                                 continue;
3269
3270                         spin_lock(&ifp->lock);
3271                         /* We try to batch several events at once. */
3272                         age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3273
3274                         if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3275                             age >= ifp->valid_lft) {
3276                                 spin_unlock(&ifp->lock);
3277                                 in6_ifa_hold(ifp);
3278                                 ipv6_del_addr(ifp);
3279                                 goto restart;
3280                         } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3281                                 spin_unlock(&ifp->lock);
3282                                 continue;
3283                         } else if (age >= ifp->prefered_lft) {
3284                                 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3285                                 int deprecate = 0;
3286
3287                                 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3288                                         deprecate = 1;
3289                                         ifp->flags |= IFA_F_DEPRECATED;
3290                                 }
3291
3292                                 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3293                                         next = ifp->tstamp + ifp->valid_lft * HZ;
3294
3295                                 spin_unlock(&ifp->lock);
3296
3297                                 if (deprecate) {
3298                                         in6_ifa_hold(ifp);
3299
3300                                         ipv6_ifa_notify(0, ifp);
3301                                         in6_ifa_put(ifp);
3302                                         goto restart;
3303                                 }
3304 #ifdef CONFIG_IPV6_PRIVACY
3305                         } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3306                                    !(ifp->flags&IFA_F_TENTATIVE)) {
3307                                 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3308                                         ifp->idev->cnf.dad_transmits *
3309                                         ifp->idev->nd_parms->retrans_time / HZ;
3310
3311                                 if (age >= ifp->prefered_lft - regen_advance) {
3312                                         struct inet6_ifaddr *ifpub = ifp->ifpub;
3313                                         if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3314                                                 next = ifp->tstamp + ifp->prefered_lft * HZ;
3315                                         if (!ifp->regen_count && ifpub) {
3316                                                 ifp->regen_count++;
3317                                                 in6_ifa_hold(ifp);
3318                                                 in6_ifa_hold(ifpub);
3319                                                 spin_unlock(&ifp->lock);
3320
3321                                                 spin_lock(&ifpub->lock);
3322                                                 ifpub->regen_count = 0;
3323                                                 spin_unlock(&ifpub->lock);
3324                                                 ipv6_create_tempaddr(ifpub, ifp);
3325                                                 in6_ifa_put(ifpub);
3326                                                 in6_ifa_put(ifp);
3327                                                 goto restart;
3328                                         }
3329                                 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3330                                         next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3331                                 spin_unlock(&ifp->lock);
3332 #endif
3333                         } else {
3334                                 /* ifp->prefered_lft <= ifp->valid_lft */
3335                                 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3336                                         next = ifp->tstamp + ifp->prefered_lft * HZ;
3337                                 spin_unlock(&ifp->lock);
3338                         }
3339                 }
3340         }
3341
3342         next_sec = round_jiffies_up(next);
3343         next_sched = next;
3344
3345         /* If rounded timeout is accurate enough, accept it. */
3346         if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3347                 next_sched = next_sec;
3348
3349         /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3350         if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3351                 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3352
3353         ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3354               now, next, next_sec, next_sched));
3355
3356         addr_chk_timer.expires = next_sched;
3357         add_timer(&addr_chk_timer);
3358         spin_unlock(&addrconf_verify_lock);
3359         rcu_read_unlock_bh();
3360 }
3361
3362 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3363 {
3364         struct in6_addr *pfx = NULL;
3365
3366         if (addr)
3367                 pfx = nla_data(addr);
3368
3369         if (local) {
3370                 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3371                         pfx = NULL;
3372                 else
3373                         pfx = nla_data(local);
3374         }
3375
3376         return pfx;
3377 }
3378
3379 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3380         [IFA_ADDRESS]           = { .len = sizeof(struct in6_addr) },
3381         [IFA_LOCAL]             = { .len = sizeof(struct in6_addr) },
3382         [IFA_CACHEINFO]         = { .len = sizeof(struct ifa_cacheinfo) },
3383 };
3384
3385 static int
3386 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3387 {
3388         struct net *net = sock_net(skb->sk);
3389         struct ifaddrmsg *ifm;
3390         struct nlattr *tb[IFA_MAX+1];
3391         struct in6_addr *pfx;
3392         int err;
3393
3394         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3395         if (err < 0)
3396                 return err;
3397
3398         ifm = nlmsg_data(nlh);
3399         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3400         if (pfx == NULL)
3401                 return -EINVAL;
3402
3403         return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3404 }
3405
3406 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3407                              u32 prefered_lft, u32 valid_lft)
3408 {
3409         u32 flags;
3410         clock_t expires;
3411         unsigned long timeout;
3412
3413         if (!valid_lft || (prefered_lft > valid_lft))
3414                 return -EINVAL;
3415
3416         timeout = addrconf_timeout_fixup(valid_lft, HZ);
3417         if (addrconf_finite_timeout(timeout)) {
3418                 expires = jiffies_to_clock_t(timeout * HZ);
3419                 valid_lft = timeout;
3420                 flags = RTF_EXPIRES;
3421         } else {
3422                 expires = 0;
3423                 flags = 0;
3424                 ifa_flags |= IFA_F_PERMANENT;
3425         }
3426
3427         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3428         if (addrconf_finite_timeout(timeout)) {
3429                 if (timeout == 0)
3430                         ifa_flags |= IFA_F_DEPRECATED;
3431                 prefered_lft = timeout;
3432         }
3433
3434         spin_lock_bh(&ifp->lock);
3435         ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3436         ifp->tstamp = jiffies;
3437         ifp->valid_lft = valid_lft;
3438         ifp->prefered_lft = prefered_lft;
3439
3440         spin_unlock_bh(&ifp->lock);
3441         if (!(ifp->flags&IFA_F_TENTATIVE))
3442                 ipv6_ifa_notify(0, ifp);
3443
3444         addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3445                               expires, flags);
3446         addrconf_verify(0);
3447
3448         return 0;
3449 }
3450
3451 static int
3452 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3453 {
3454         struct net *net = sock_net(skb->sk);
3455         struct ifaddrmsg *ifm;
3456         struct nlattr *tb[IFA_MAX+1];
3457         struct in6_addr *pfx;
3458         struct inet6_ifaddr *ifa;
3459         struct net_device *dev;
3460         u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3461         u8 ifa_flags;
3462         int err;
3463
3464         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3465         if (err < 0)
3466                 return err;
3467
3468         ifm = nlmsg_data(nlh);
3469         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3470         if (pfx == NULL)
3471                 return -EINVAL;
3472
3473         if (tb[IFA_CACHEINFO]) {
3474                 struct ifa_cacheinfo *ci;
3475
3476                 ci = nla_data(tb[IFA_CACHEINFO]);
3477                 valid_lft = ci->ifa_valid;
3478                 preferred_lft = ci->ifa_prefered;
3479         } else {
3480                 preferred_lft = INFINITY_LIFE_TIME;
3481                 valid_lft = INFINITY_LIFE_TIME;
3482         }
3483
3484         dev =  __dev_get_by_index(net, ifm->ifa_index);
3485         if (dev == NULL)
3486                 return -ENODEV;
3487
3488         /* We ignore other flags so far. */
3489         ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3490
3491         ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3492         if (ifa == NULL) {
3493                 /*
3494                  * It would be best to check for !NLM_F_CREATE here but
3495                  * userspace alreay relies on not having to provide this.
3496                  */
3497                 return inet6_addr_add(net, ifm->ifa_index, pfx,
3498                                       ifm->ifa_prefixlen, ifa_flags,
3499                                       preferred_lft, valid_lft);
3500         }
3501
3502         if (nlh->nlmsg_flags & NLM_F_EXCL ||
3503             !(nlh->nlmsg_flags & NLM_F_REPLACE))
3504                 err = -EEXIST;
3505         else
3506                 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3507
3508         in6_ifa_put(ifa);
3509
3510         return err;
3511 }
3512
3513 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3514                           u8 scope, int ifindex)
3515 {
3516         struct ifaddrmsg *ifm;
3517
3518         ifm = nlmsg_data(nlh);
3519         ifm->ifa_family = AF_INET6;
3520         ifm->ifa_prefixlen = prefixlen;
3521         ifm->ifa_flags = flags;
3522         ifm->ifa_scope = scope;
3523         ifm->ifa_index = ifindex;
3524 }
3525
3526 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3527                          unsigned long tstamp, u32 preferred, u32 valid)
3528 {
3529         struct ifa_cacheinfo ci;
3530
3531         ci.cstamp = cstamp_delta(cstamp);
3532         ci.tstamp = cstamp_delta(tstamp);
3533         ci.ifa_prefered = preferred;
3534         ci.ifa_valid = valid;
3535
3536         return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3537 }
3538
3539 static inline int rt_scope(int ifa_scope)
3540 {
3541         if (ifa_scope & IFA_HOST)
3542                 return RT_SCOPE_HOST;
3543         else if (ifa_scope & IFA_LINK)
3544                 return RT_SCOPE_LINK;
3545         else if (ifa_scope & IFA_SITE)
3546                 return RT_SCOPE_SITE;
3547         else
3548                 return RT_SCOPE_UNIVERSE;
3549 }
3550
3551 static inline int inet6_ifaddr_msgsize(void)
3552 {
3553         return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3554                + nla_total_size(16) /* IFA_ADDRESS */
3555                + nla_total_size(sizeof(struct ifa_cacheinfo));
3556 }
3557
3558 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3559                              u32 pid, u32 seq, int event, unsigned int flags)
3560 {
3561         struct nlmsghdr  *nlh;
3562         u32 preferred, valid;
3563
3564         nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3565         if (nlh == NULL)
3566                 return -EMSGSIZE;
3567
3568         put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3569                       ifa->idev->dev->ifindex);
3570
3571         if (!(ifa->flags&IFA_F_PERMANENT)) {
3572                 preferred = ifa->prefered_lft;
3573                 valid = ifa->valid_lft;
3574                 if (preferred != INFINITY_LIFE_TIME) {
3575                         long tval = (jiffies - ifa->tstamp)/HZ;
3576                         if (preferred > tval)
3577                                 preferred -= tval;
3578                         else
3579                                 preferred = 0;
3580                         if (valid != INFINITY_LIFE_TIME) {
3581                                 if (valid > tval)
3582                                         valid -= tval;
3583                                 else
3584                                         valid = 0;
3585                         }
3586                 }
3587         } else {
3588                 preferred = INFINITY_LIFE_TIME;
3589                 valid = INFINITY_LIFE_TIME;
3590         }
3591
3592         if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3593             put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3594                 nlmsg_cancel(skb, nlh);
3595                 return -EMSGSIZE;
3596         }
3597
3598         return nlmsg_end(skb, nlh);
3599 }
3600
3601 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3602                                 u32 pid, u32 seq, int event, u16 flags)
3603 {
3604         struct nlmsghdr  *nlh;
3605         u8 scope = RT_SCOPE_UNIVERSE;
3606         int ifindex = ifmca->idev->dev->ifindex;
3607
3608         if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3609                 scope = RT_SCOPE_SITE;
3610
3611         nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3612         if (nlh == NULL)
3613                 return -EMSGSIZE;
3614
3615         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3616         if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3617             put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3618                           INFINITY_LIFE_TIME,