]> Pileus Git - ~andy/linux/blob - net/ipv6/addrconf.c
15794fdaf028079ba0fab468784e81f6bc093c6a
[~andy/linux] / 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 #define pr_fmt(fmt) "IPv6: " fmt
42
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/in6.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_addr.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #ifdef CONFIG_SYSCTL
60 #include <linux/sysctl.h>
61 #endif
62 #include <linux/capability.h>
63 #include <linux/delay.h>
64 #include <linux/notifier.h>
65 #include <linux/string.h>
66 #include <linux/hash.h>
67
68 #include <net/net_namespace.h>
69 #include <net/sock.h>
70 #include <net/snmp.h>
71
72 #include <net/af_ieee802154.h>
73 #include <net/ipv6.h>
74 #include <net/protocol.h>
75 #include <net/ndisc.h>
76 #include <net/ip6_route.h>
77 #include <net/addrconf.h>
78 #include <net/tcp.h>
79 #include <net/ip.h>
80 #include <net/netlink.h>
81 #include <net/pkt_sched.h>
82 #include <linux/if_tunnel.h>
83 #include <linux/rtnetlink.h>
84 #include <linux/netconf.h>
85
86 #ifdef CONFIG_IPV6_PRIVACY
87 #include <linux/random.h>
88 #endif
89
90 #include <linux/uaccess.h>
91 #include <asm/unaligned.h>
92
93 #include <linux/proc_fs.h>
94 #include <linux/seq_file.h>
95 #include <linux/export.h>
96
97 /* Set to 3 to get tracing... */
98 #define ACONF_DEBUG 2
99
100 #if ACONF_DEBUG >= 3
101 #define ADBG(x) printk x
102 #else
103 #define ADBG(x)
104 #endif
105
106 #define INFINITY_LIFE_TIME      0xFFFFFFFF
107
108 static inline u32 cstamp_delta(unsigned long cstamp)
109 {
110         return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
111 }
112
113 #ifdef CONFIG_SYSCTL
114 static void addrconf_sysctl_register(struct inet6_dev *idev);
115 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
116 #else
117 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
118 {
119 }
120
121 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
122 {
123 }
124 #endif
125
126 #ifdef CONFIG_IPV6_PRIVACY
127 static void __ipv6_regen_rndid(struct inet6_dev *idev);
128 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
129 static void ipv6_regen_rndid(unsigned long data);
130 #endif
131
132 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
133 static int ipv6_count_addresses(struct inet6_dev *idev);
134
135 /*
136  *      Configured unicast address hash table
137  */
138 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
139 static DEFINE_SPINLOCK(addrconf_hash_lock);
140
141 static void addrconf_verify(unsigned long);
142
143 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
144 static DEFINE_SPINLOCK(addrconf_verify_lock);
145
146 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
147 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
148
149 static void addrconf_type_change(struct net_device *dev,
150                                  unsigned long event);
151 static int addrconf_ifdown(struct net_device *dev, int how);
152
153 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
154                                                   int plen,
155                                                   const struct net_device *dev,
156                                                   u32 flags, u32 noflags);
157
158 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
159 static void addrconf_dad_timer(unsigned long data);
160 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
161 static void addrconf_dad_run(struct inet6_dev *idev);
162 static void addrconf_rs_timer(unsigned long data);
163 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
164 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
165
166 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
167                                 struct prefix_info *pinfo);
168 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
169                                struct net_device *dev);
170
171 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
172
173 static struct ipv6_devconf ipv6_devconf __read_mostly = {
174         .forwarding             = 0,
175         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
176         .mtu6                   = IPV6_MIN_MTU,
177         .accept_ra              = 1,
178         .accept_redirects       = 1,
179         .autoconf               = 1,
180         .force_mld_version      = 0,
181         .dad_transmits          = 1,
182         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
183         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
184         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
185 #ifdef CONFIG_IPV6_PRIVACY
186         .use_tempaddr           = 0,
187         .temp_valid_lft         = TEMP_VALID_LIFETIME,
188         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
189         .regen_max_retry        = REGEN_MAX_RETRY,
190         .max_desync_factor      = MAX_DESYNC_FACTOR,
191 #endif
192         .max_addresses          = IPV6_MAX_ADDRESSES,
193         .accept_ra_defrtr       = 1,
194         .accept_ra_pinfo        = 1,
195 #ifdef CONFIG_IPV6_ROUTER_PREF
196         .accept_ra_rtr_pref     = 1,
197         .rtr_probe_interval     = 60 * HZ,
198 #ifdef CONFIG_IPV6_ROUTE_INFO
199         .accept_ra_rt_info_max_plen = 0,
200 #endif
201 #endif
202         .proxy_ndp              = 0,
203         .accept_source_route    = 0,    /* we do not accept RH0 by default. */
204         .disable_ipv6           = 0,
205         .accept_dad             = 1,
206 };
207
208 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
209         .forwarding             = 0,
210         .hop_limit              = IPV6_DEFAULT_HOPLIMIT,
211         .mtu6                   = IPV6_MIN_MTU,
212         .accept_ra              = 1,
213         .accept_redirects       = 1,
214         .autoconf               = 1,
215         .dad_transmits          = 1,
216         .rtr_solicits           = MAX_RTR_SOLICITATIONS,
217         .rtr_solicit_interval   = RTR_SOLICITATION_INTERVAL,
218         .rtr_solicit_delay      = MAX_RTR_SOLICITATION_DELAY,
219 #ifdef CONFIG_IPV6_PRIVACY
220         .use_tempaddr           = 0,
221         .temp_valid_lft         = TEMP_VALID_LIFETIME,
222         .temp_prefered_lft      = TEMP_PREFERRED_LIFETIME,
223         .regen_max_retry        = REGEN_MAX_RETRY,
224         .max_desync_factor      = MAX_DESYNC_FACTOR,
225 #endif
226         .max_addresses          = IPV6_MAX_ADDRESSES,
227         .accept_ra_defrtr       = 1,
228         .accept_ra_pinfo        = 1,
229 #ifdef CONFIG_IPV6_ROUTER_PREF
230         .accept_ra_rtr_pref     = 1,
231         .rtr_probe_interval     = 60 * HZ,
232 #ifdef CONFIG_IPV6_ROUTE_INFO
233         .accept_ra_rt_info_max_plen = 0,
234 #endif
235 #endif
236         .proxy_ndp              = 0,
237         .accept_source_route    = 0,    /* we do not accept RH0 by default. */
238         .disable_ipv6           = 0,
239         .accept_dad             = 1,
240 };
241
242 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
243 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
244 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
245 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
246 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
247 const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
248 const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
249 const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
250
251 /* Check if a valid qdisc is available */
252 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
253 {
254         return !qdisc_tx_is_noop(dev);
255 }
256
257 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
258 {
259         if (del_timer(&ifp->timer))
260                 __in6_ifa_put(ifp);
261 }
262
263 enum addrconf_timer_t {
264         AC_NONE,
265         AC_DAD,
266         AC_RS,
267 };
268
269 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
270                                enum addrconf_timer_t what,
271                                unsigned long when)
272 {
273         if (!del_timer(&ifp->timer))
274                 in6_ifa_hold(ifp);
275
276         switch (what) {
277         case AC_DAD:
278                 ifp->timer.function = addrconf_dad_timer;
279                 break;
280         case AC_RS:
281                 ifp->timer.function = addrconf_rs_timer;
282                 break;
283         default:
284                 break;
285         }
286         ifp->timer.expires = jiffies + when;
287         add_timer(&ifp->timer);
288 }
289
290 static int snmp6_alloc_dev(struct inet6_dev *idev)
291 {
292         if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
293                           sizeof(struct ipstats_mib),
294                           __alignof__(struct ipstats_mib)) < 0)
295                 goto err_ip;
296         idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
297                                         GFP_KERNEL);
298         if (!idev->stats.icmpv6dev)
299                 goto err_icmp;
300         idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
301                                            GFP_KERNEL);
302         if (!idev->stats.icmpv6msgdev)
303                 goto err_icmpmsg;
304
305         return 0;
306
307 err_icmpmsg:
308         kfree(idev->stats.icmpv6dev);
309 err_icmp:
310         snmp_mib_free((void __percpu **)idev->stats.ipv6);
311 err_ip:
312         return -ENOMEM;
313 }
314
315 static void snmp6_free_dev(struct inet6_dev *idev)
316 {
317         kfree(idev->stats.icmpv6msgdev);
318         kfree(idev->stats.icmpv6dev);
319         snmp_mib_free((void __percpu **)idev->stats.ipv6);
320 }
321
322 /* Nobody refers to this device, we may destroy it. */
323
324 void in6_dev_finish_destroy(struct inet6_dev *idev)
325 {
326         struct net_device *dev = idev->dev;
327
328         WARN_ON(!list_empty(&idev->addr_list));
329         WARN_ON(idev->mc_list != NULL);
330
331 #ifdef NET_REFCNT_DEBUG
332         pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
333 #endif
334         dev_put(dev);
335         if (!idev->dead) {
336                 pr_warn("Freeing alive inet6 device %p\n", idev);
337                 return;
338         }
339         snmp6_free_dev(idev);
340         kfree_rcu(idev, rcu);
341 }
342 EXPORT_SYMBOL(in6_dev_finish_destroy);
343
344 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
345 {
346         struct inet6_dev *ndev;
347
348         ASSERT_RTNL();
349
350         if (dev->mtu < IPV6_MIN_MTU)
351                 return NULL;
352
353         ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
354
355         if (ndev == NULL)
356                 return NULL;
357
358         rwlock_init(&ndev->lock);
359         ndev->dev = dev;
360         INIT_LIST_HEAD(&ndev->addr_list);
361
362         memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
363         ndev->cnf.mtu6 = dev->mtu;
364         ndev->cnf.sysctl = NULL;
365         ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
366         if (ndev->nd_parms == NULL) {
367                 kfree(ndev);
368                 return NULL;
369         }
370         if (ndev->cnf.forwarding)
371                 dev_disable_lro(dev);
372         /* We refer to the device */
373         dev_hold(dev);
374
375         if (snmp6_alloc_dev(ndev) < 0) {
376                 ADBG((KERN_WARNING
377                         "%s: cannot allocate memory for statistics; dev=%s.\n",
378                         __func__, dev->name));
379                 neigh_parms_release(&nd_tbl, ndev->nd_parms);
380                 dev_put(dev);
381                 kfree(ndev);
382                 return NULL;
383         }
384
385         if (snmp6_register_dev(ndev) < 0) {
386                 ADBG((KERN_WARNING
387                         "%s: cannot create /proc/net/dev_snmp6/%s\n",
388                         __func__, dev->name));
389                 neigh_parms_release(&nd_tbl, ndev->nd_parms);
390                 ndev->dead = 1;
391                 in6_dev_finish_destroy(ndev);
392                 return NULL;
393         }
394
395         /* One reference from device.  We must do this before
396          * we invoke __ipv6_regen_rndid().
397          */
398         in6_dev_hold(ndev);
399
400         if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
401                 ndev->cnf.accept_dad = -1;
402
403 #if IS_ENABLED(CONFIG_IPV6_SIT)
404         if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
405                 pr_info("%s: Disabled Multicast RS\n", 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 interface-local all-node multicast group */
435         ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
436
437         /* Join all-node multicast group */
438         ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
439
440         /* Join all-router multicast group if forwarding is set */
441         if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
442                 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
443
444         return ndev;
445 }
446
447 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
448 {
449         struct inet6_dev *idev;
450
451         ASSERT_RTNL();
452
453         idev = __in6_dev_get(dev);
454         if (!idev) {
455                 idev = ipv6_add_dev(dev);
456                 if (!idev)
457                         return NULL;
458         }
459
460         if (dev->flags&IFF_UP)
461                 ipv6_mc_up(idev);
462         return idev;
463 }
464
465 static int inet6_netconf_msgsize_devconf(int type)
466 {
467         int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
468                     + nla_total_size(4);        /* NETCONFA_IFINDEX */
469
470         /* type -1 is used for ALL */
471         if (type == -1 || type == NETCONFA_FORWARDING)
472                 size += nla_total_size(4);
473 #ifdef CONFIG_IPV6_MROUTE
474         if (type == -1 || type == NETCONFA_MC_FORWARDING)
475                 size += nla_total_size(4);
476 #endif
477
478         return size;
479 }
480
481 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
482                                       struct ipv6_devconf *devconf, u32 portid,
483                                       u32 seq, int event, unsigned int flags,
484                                       int type)
485 {
486         struct nlmsghdr  *nlh;
487         struct netconfmsg *ncm;
488
489         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
490                         flags);
491         if (nlh == NULL)
492                 return -EMSGSIZE;
493
494         ncm = nlmsg_data(nlh);
495         ncm->ncm_family = AF_INET6;
496
497         if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
498                 goto nla_put_failure;
499
500         /* type -1 is used for ALL */
501         if ((type == -1 || type == NETCONFA_FORWARDING) &&
502             nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
503                 goto nla_put_failure;
504 #ifdef CONFIG_IPV6_MROUTE
505         if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
506             nla_put_s32(skb, NETCONFA_MC_FORWARDING,
507                         devconf->mc_forwarding) < 0)
508                 goto nla_put_failure;
509 #endif
510         return nlmsg_end(skb, nlh);
511
512 nla_put_failure:
513         nlmsg_cancel(skb, nlh);
514         return -EMSGSIZE;
515 }
516
517 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
518                                   struct ipv6_devconf *devconf)
519 {
520         struct sk_buff *skb;
521         int err = -ENOBUFS;
522
523         skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
524         if (skb == NULL)
525                 goto errout;
526
527         err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
528                                          RTM_NEWNETCONF, 0, type);
529         if (err < 0) {
530                 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
531                 WARN_ON(err == -EMSGSIZE);
532                 kfree_skb(skb);
533                 goto errout;
534         }
535         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
536         return;
537 errout:
538         rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
539 }
540
541 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
542         [NETCONFA_IFINDEX]      = { .len = sizeof(int) },
543         [NETCONFA_FORWARDING]   = { .len = sizeof(int) },
544 };
545
546 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
547                                      struct nlmsghdr *nlh)
548 {
549         struct net *net = sock_net(in_skb->sk);
550         struct nlattr *tb[NETCONFA_MAX+1];
551         struct netconfmsg *ncm;
552         struct sk_buff *skb;
553         struct ipv6_devconf *devconf;
554         struct inet6_dev *in6_dev;
555         struct net_device *dev;
556         int ifindex;
557         int err;
558
559         err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
560                           devconf_ipv6_policy);
561         if (err < 0)
562                 goto errout;
563
564         err = EINVAL;
565         if (!tb[NETCONFA_IFINDEX])
566                 goto errout;
567
568         ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
569         switch (ifindex) {
570         case NETCONFA_IFINDEX_ALL:
571                 devconf = net->ipv6.devconf_all;
572                 break;
573         case NETCONFA_IFINDEX_DEFAULT:
574                 devconf = net->ipv6.devconf_dflt;
575                 break;
576         default:
577                 dev = __dev_get_by_index(net, ifindex);
578                 if (dev == NULL)
579                         goto errout;
580                 in6_dev = __in6_dev_get(dev);
581                 if (in6_dev == NULL)
582                         goto errout;
583                 devconf = &in6_dev->cnf;
584                 break;
585         }
586
587         err = -ENOBUFS;
588         skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
589         if (skb == NULL)
590                 goto errout;
591
592         err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
593                                          NETLINK_CB(in_skb).portid,
594                                          nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
595                                          -1);
596         if (err < 0) {
597                 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
598                 WARN_ON(err == -EMSGSIZE);
599                 kfree_skb(skb);
600                 goto errout;
601         }
602         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
603 errout:
604         return err;
605 }
606
607 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
608                                       struct netlink_callback *cb)
609 {
610         struct net *net = sock_net(skb->sk);
611         int h, s_h;
612         int idx, s_idx;
613         struct net_device *dev;
614         struct inet6_dev *idev;
615         struct hlist_head *head;
616
617         s_h = cb->args[0];
618         s_idx = idx = cb->args[1];
619
620         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
621                 idx = 0;
622                 head = &net->dev_index_head[h];
623                 rcu_read_lock();
624                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
625                         if (idx < s_idx)
626                                 goto cont;
627                         idev = __in6_dev_get(dev);
628                         if (!idev)
629                                 goto cont;
630
631                         if (inet6_netconf_fill_devconf(skb, dev->ifindex,
632                                                        &idev->cnf,
633                                                        NETLINK_CB(cb->skb).portid,
634                                                        cb->nlh->nlmsg_seq,
635                                                        RTM_NEWNETCONF,
636                                                        NLM_F_MULTI,
637                                                        -1) <= 0) {
638                                 rcu_read_unlock();
639                                 goto done;
640                         }
641 cont:
642                         idx++;
643                 }
644                 rcu_read_unlock();
645         }
646         if (h == NETDEV_HASHENTRIES) {
647                 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
648                                                net->ipv6.devconf_all,
649                                                NETLINK_CB(cb->skb).portid,
650                                                cb->nlh->nlmsg_seq,
651                                                RTM_NEWNETCONF, NLM_F_MULTI,
652                                                -1) <= 0)
653                         goto done;
654                 else
655                         h++;
656         }
657         if (h == NETDEV_HASHENTRIES + 1) {
658                 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
659                                                net->ipv6.devconf_dflt,
660                                                NETLINK_CB(cb->skb).portid,
661                                                cb->nlh->nlmsg_seq,
662                                                RTM_NEWNETCONF, NLM_F_MULTI,
663                                                -1) <= 0)
664                         goto done;
665                 else
666                         h++;
667         }
668 done:
669         cb->args[0] = h;
670         cb->args[1] = idx;
671
672         return skb->len;
673 }
674
675 #ifdef CONFIG_SYSCTL
676 static void dev_forward_change(struct inet6_dev *idev)
677 {
678         struct net_device *dev;
679         struct inet6_ifaddr *ifa;
680
681         if (!idev)
682                 return;
683         dev = idev->dev;
684         if (idev->cnf.forwarding)
685                 dev_disable_lro(dev);
686         if (dev->flags & IFF_MULTICAST) {
687                 if (idev->cnf.forwarding) {
688                         ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
689                         ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
690                         ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
691                 } else {
692                         ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
693                         ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
694                         ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
695                 }
696         }
697
698         list_for_each_entry(ifa, &idev->addr_list, if_list) {
699                 if (ifa->flags&IFA_F_TENTATIVE)
700                         continue;
701                 if (idev->cnf.forwarding)
702                         addrconf_join_anycast(ifa);
703                 else
704                         addrconf_leave_anycast(ifa);
705         }
706         inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
707                                      dev->ifindex, &idev->cnf);
708 }
709
710
711 static void addrconf_forward_change(struct net *net, __s32 newf)
712 {
713         struct net_device *dev;
714         struct inet6_dev *idev;
715
716         for_each_netdev(net, dev) {
717                 idev = __in6_dev_get(dev);
718                 if (idev) {
719                         int changed = (!idev->cnf.forwarding) ^ (!newf);
720                         idev->cnf.forwarding = newf;
721                         if (changed)
722                                 dev_forward_change(idev);
723                 }
724         }
725 }
726
727 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
728 {
729         struct net *net;
730         int old;
731
732         if (!rtnl_trylock())
733                 return restart_syscall();
734
735         net = (struct net *)table->extra2;
736         old = *p;
737         *p = newf;
738
739         if (p == &net->ipv6.devconf_dflt->forwarding) {
740                 if ((!newf) ^ (!old))
741                         inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
742                                                      NETCONFA_IFINDEX_DEFAULT,
743                                                      net->ipv6.devconf_dflt);
744                 rtnl_unlock();
745                 return 0;
746         }
747
748         if (p == &net->ipv6.devconf_all->forwarding) {
749                 net->ipv6.devconf_dflt->forwarding = newf;
750                 addrconf_forward_change(net, newf);
751                 if ((!newf) ^ (!old))
752                         inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
753                                                      NETCONFA_IFINDEX_ALL,
754                                                      net->ipv6.devconf_all);
755         } else if ((!newf) ^ (!old))
756                 dev_forward_change((struct inet6_dev *)table->extra1);
757         rtnl_unlock();
758
759         if (newf)
760                 rt6_purge_dflt_routers(net);
761         return 1;
762 }
763 #endif
764
765 /* Nobody refers to this ifaddr, destroy it */
766 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
767 {
768         WARN_ON(!hlist_unhashed(&ifp->addr_lst));
769
770 #ifdef NET_REFCNT_DEBUG
771         pr_debug("%s\n", __func__);
772 #endif
773
774         in6_dev_put(ifp->idev);
775
776         if (del_timer(&ifp->timer))
777                 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
778
779         if (ifp->state != INET6_IFADDR_STATE_DEAD) {
780                 pr_warn("Freeing alive inet6 address %p\n", ifp);
781                 return;
782         }
783         ip6_rt_put(ifp->rt);
784
785         kfree_rcu(ifp, rcu);
786 }
787
788 static void
789 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
790 {
791         struct list_head *p;
792         int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
793
794         /*
795          * Each device address list is sorted in order of scope -
796          * global before linklocal.
797          */
798         list_for_each(p, &idev->addr_list) {
799                 struct inet6_ifaddr *ifa
800                         = list_entry(p, struct inet6_ifaddr, if_list);
801                 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
802                         break;
803         }
804
805         list_add_tail(&ifp->if_list, p);
806 }
807
808 static u32 inet6_addr_hash(const struct in6_addr *addr)
809 {
810         return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
811 }
812
813 /* On success it returns ifp with increased reference count */
814
815 static struct inet6_ifaddr *
816 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
817               int scope, u32 flags)
818 {
819         struct inet6_ifaddr *ifa = NULL;
820         struct rt6_info *rt;
821         unsigned int hash;
822         int err = 0;
823         int addr_type = ipv6_addr_type(addr);
824
825         if (addr_type == IPV6_ADDR_ANY ||
826             addr_type & IPV6_ADDR_MULTICAST ||
827             (!(idev->dev->flags & IFF_LOOPBACK) &&
828              addr_type & IPV6_ADDR_LOOPBACK))
829                 return ERR_PTR(-EADDRNOTAVAIL);
830
831         rcu_read_lock_bh();
832         if (idev->dead) {
833                 err = -ENODEV;                  /*XXX*/
834                 goto out2;
835         }
836
837         if (idev->cnf.disable_ipv6) {
838                 err = -EACCES;
839                 goto out2;
840         }
841
842         spin_lock(&addrconf_hash_lock);
843
844         /* Ignore adding duplicate addresses on an interface */
845         if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
846                 ADBG(("ipv6_add_addr: already assigned\n"));
847                 err = -EEXIST;
848                 goto out;
849         }
850
851         ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
852
853         if (ifa == NULL) {
854                 ADBG(("ipv6_add_addr: malloc failed\n"));
855                 err = -ENOBUFS;
856                 goto out;
857         }
858
859         rt = addrconf_dst_alloc(idev, addr, false);
860         if (IS_ERR(rt)) {
861                 err = PTR_ERR(rt);
862                 goto out;
863         }
864
865         ifa->addr = *addr;
866
867         spin_lock_init(&ifa->lock);
868         spin_lock_init(&ifa->state_lock);
869         init_timer(&ifa->timer);
870         INIT_HLIST_NODE(&ifa->addr_lst);
871         ifa->timer.data = (unsigned long) ifa;
872         ifa->scope = scope;
873         ifa->prefix_len = pfxlen;
874         ifa->flags = flags | IFA_F_TENTATIVE;
875         ifa->cstamp = ifa->tstamp = jiffies;
876
877         ifa->rt = rt;
878
879         ifa->idev = idev;
880         in6_dev_hold(idev);
881         /* For caller */
882         in6_ifa_hold(ifa);
883
884         /* Add to big hash table */
885         hash = inet6_addr_hash(addr);
886
887         hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
888         spin_unlock(&addrconf_hash_lock);
889
890         write_lock(&idev->lock);
891         /* Add to inet6_dev unicast addr list. */
892         ipv6_link_dev_addr(idev, ifa);
893
894 #ifdef CONFIG_IPV6_PRIVACY
895         if (ifa->flags&IFA_F_TEMPORARY) {
896                 list_add(&ifa->tmp_list, &idev->tempaddr_list);
897                 in6_ifa_hold(ifa);
898         }
899 #endif
900
901         in6_ifa_hold(ifa);
902         write_unlock(&idev->lock);
903 out2:
904         rcu_read_unlock_bh();
905
906         if (likely(err == 0))
907                 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
908         else {
909                 kfree(ifa);
910                 ifa = ERR_PTR(err);
911         }
912
913         return ifa;
914 out:
915         spin_unlock(&addrconf_hash_lock);
916         goto out2;
917 }
918
919 /* This function wants to get referenced ifp and releases it before return */
920
921 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
922 {
923         struct inet6_ifaddr *ifa, *ifn;
924         struct inet6_dev *idev = ifp->idev;
925         int state;
926         int deleted = 0, onlink = 0;
927         unsigned long expires = jiffies;
928
929         spin_lock_bh(&ifp->state_lock);
930         state = ifp->state;
931         ifp->state = INET6_IFADDR_STATE_DEAD;
932         spin_unlock_bh(&ifp->state_lock);
933
934         if (state == INET6_IFADDR_STATE_DEAD)
935                 goto out;
936
937         spin_lock_bh(&addrconf_hash_lock);
938         hlist_del_init_rcu(&ifp->addr_lst);
939         spin_unlock_bh(&addrconf_hash_lock);
940
941         write_lock_bh(&idev->lock);
942 #ifdef CONFIG_IPV6_PRIVACY
943         if (ifp->flags&IFA_F_TEMPORARY) {
944                 list_del(&ifp->tmp_list);
945                 if (ifp->ifpub) {
946                         in6_ifa_put(ifp->ifpub);
947                         ifp->ifpub = NULL;
948                 }
949                 __in6_ifa_put(ifp);
950         }
951 #endif
952
953         list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
954                 if (ifa == ifp) {
955                         list_del_init(&ifp->if_list);
956                         __in6_ifa_put(ifp);
957
958                         if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
959                                 break;
960                         deleted = 1;
961                         continue;
962                 } else if (ifp->flags & IFA_F_PERMANENT) {
963                         if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
964                                               ifp->prefix_len)) {
965                                 if (ifa->flags & IFA_F_PERMANENT) {
966                                         onlink = 1;
967                                         if (deleted)
968                                                 break;
969                                 } else {
970                                         unsigned long lifetime;
971
972                                         if (!onlink)
973                                                 onlink = -1;
974
975                                         spin_lock(&ifa->lock);
976
977                                         lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
978                                         /*
979                                          * Note: Because this address is
980                                          * not permanent, lifetime <
981                                          * LONG_MAX / HZ here.
982                                          */
983                                         if (time_before(expires,
984                                                         ifa->tstamp + lifetime * HZ))
985                                                 expires = ifa->tstamp + lifetime * HZ;
986                                         spin_unlock(&ifa->lock);
987                                 }
988                         }
989                 }
990         }
991         write_unlock_bh(&idev->lock);
992
993         addrconf_del_timer(ifp);
994
995         ipv6_ifa_notify(RTM_DELADDR, ifp);
996
997         atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
998
999         /*
1000          * Purge or update corresponding prefix
1001          *
1002          * 1) we don't purge prefix here if address was not permanent.
1003          *    prefix is managed by its own lifetime.
1004          * 2) if there're no addresses, delete prefix.
1005          * 3) if there're still other permanent address(es),
1006          *    corresponding prefix is still permanent.
1007          * 4) otherwise, update prefix lifetime to the
1008          *    longest valid lifetime among the corresponding
1009          *    addresses on the device.
1010          *    Note: subsequent RA will update lifetime.
1011          *
1012          * --yoshfuji
1013          */
1014         if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
1015                 struct in6_addr prefix;
1016                 struct rt6_info *rt;
1017
1018                 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
1019
1020                 rt = addrconf_get_prefix_route(&prefix,
1021                                                ifp->prefix_len,
1022                                                ifp->idev->dev,
1023                                                0, RTF_GATEWAY | RTF_DEFAULT);
1024
1025                 if (rt) {
1026                         if (onlink == 0) {
1027                                 ip6_del_rt(rt);
1028                                 rt = NULL;
1029                         } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1030                                 rt6_set_expires(rt, expires);
1031                         }
1032                 }
1033                 ip6_rt_put(rt);
1034         }
1035
1036         /* clean up prefsrc entries */
1037         rt6_remove_prefsrc(ifp);
1038 out:
1039         in6_ifa_put(ifp);
1040 }
1041
1042 #ifdef CONFIG_IPV6_PRIVACY
1043 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1044 {
1045         struct inet6_dev *idev = ifp->idev;
1046         struct in6_addr addr, *tmpaddr;
1047         unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1048         unsigned long regen_advance;
1049         int tmp_plen;
1050         int ret = 0;
1051         int max_addresses;
1052         u32 addr_flags;
1053         unsigned long now = jiffies;
1054
1055         write_lock(&idev->lock);
1056         if (ift) {
1057                 spin_lock_bh(&ift->lock);
1058                 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1059                 spin_unlock_bh(&ift->lock);
1060                 tmpaddr = &addr;
1061         } else {
1062                 tmpaddr = NULL;
1063         }
1064 retry:
1065         in6_dev_hold(idev);
1066         if (idev->cnf.use_tempaddr <= 0) {
1067                 write_unlock(&idev->lock);
1068                 pr_info("%s: use_tempaddr is disabled\n", __func__);
1069                 in6_dev_put(idev);
1070                 ret = -1;
1071                 goto out;
1072         }
1073         spin_lock_bh(&ifp->lock);
1074         if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1075                 idev->cnf.use_tempaddr = -1;    /*XXX*/
1076                 spin_unlock_bh(&ifp->lock);
1077                 write_unlock(&idev->lock);
1078                 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1079                         __func__);
1080                 in6_dev_put(idev);
1081                 ret = -1;
1082                 goto out;
1083         }
1084         in6_ifa_hold(ifp);
1085         memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1086         __ipv6_try_regen_rndid(idev, tmpaddr);
1087         memcpy(&addr.s6_addr[8], idev->rndid, 8);
1088         age = (now - ifp->tstamp) / HZ;
1089         tmp_valid_lft = min_t(__u32,
1090                               ifp->valid_lft,
1091                               idev->cnf.temp_valid_lft + age);
1092         tmp_prefered_lft = min_t(__u32,
1093                                  ifp->prefered_lft,
1094                                  idev->cnf.temp_prefered_lft + age -
1095                                  idev->cnf.max_desync_factor);
1096         tmp_plen = ifp->prefix_len;
1097         max_addresses = idev->cnf.max_addresses;
1098         tmp_tstamp = ifp->tstamp;
1099         spin_unlock_bh(&ifp->lock);
1100
1101         regen_advance = idev->cnf.regen_max_retry *
1102                         idev->cnf.dad_transmits *
1103                         idev->nd_parms->retrans_time / HZ;
1104         write_unlock(&idev->lock);
1105
1106         /* A temporary address is created only if this calculated Preferred
1107          * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1108          * an implementation must not create a temporary address with a zero
1109          * Preferred Lifetime.
1110          */
1111         if (tmp_prefered_lft <= regen_advance) {
1112                 in6_ifa_put(ifp);
1113                 in6_dev_put(idev);
1114                 ret = -1;
1115                 goto out;
1116         }
1117
1118         addr_flags = IFA_F_TEMPORARY;
1119         /* set in addrconf_prefix_rcv() */
1120         if (ifp->flags & IFA_F_OPTIMISTIC)
1121                 addr_flags |= IFA_F_OPTIMISTIC;
1122
1123         ift = !max_addresses ||
1124               ipv6_count_addresses(idev) < max_addresses ?
1125                 ipv6_add_addr(idev, &addr, tmp_plen,
1126                               ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
1127                               addr_flags) : NULL;
1128         if (IS_ERR_OR_NULL(ift)) {
1129                 in6_ifa_put(ifp);
1130                 in6_dev_put(idev);
1131                 pr_info("%s: retry temporary address regeneration\n", __func__);
1132                 tmpaddr = &addr;
1133                 write_lock(&idev->lock);
1134                 goto retry;
1135         }
1136
1137         spin_lock_bh(&ift->lock);
1138         ift->ifpub = ifp;
1139         ift->valid_lft = tmp_valid_lft;
1140         ift->prefered_lft = tmp_prefered_lft;
1141         ift->cstamp = now;
1142         ift->tstamp = tmp_tstamp;
1143         spin_unlock_bh(&ift->lock);
1144
1145         addrconf_dad_start(ift);
1146         in6_ifa_put(ift);
1147         in6_dev_put(idev);
1148 out:
1149         return ret;
1150 }
1151 #endif
1152
1153 /*
1154  *      Choose an appropriate source address (RFC3484)
1155  */
1156 enum {
1157         IPV6_SADDR_RULE_INIT = 0,
1158         IPV6_SADDR_RULE_LOCAL,
1159         IPV6_SADDR_RULE_SCOPE,
1160         IPV6_SADDR_RULE_PREFERRED,
1161 #ifdef CONFIG_IPV6_MIP6
1162         IPV6_SADDR_RULE_HOA,
1163 #endif
1164         IPV6_SADDR_RULE_OIF,
1165         IPV6_SADDR_RULE_LABEL,
1166 #ifdef CONFIG_IPV6_PRIVACY
1167         IPV6_SADDR_RULE_PRIVACY,
1168 #endif
1169         IPV6_SADDR_RULE_ORCHID,
1170         IPV6_SADDR_RULE_PREFIX,
1171         IPV6_SADDR_RULE_MAX
1172 };
1173
1174 struct ipv6_saddr_score {
1175         int                     rule;
1176         int                     addr_type;
1177         struct inet6_ifaddr     *ifa;
1178         DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1179         int                     scopedist;
1180         int                     matchlen;
1181 };
1182
1183 struct ipv6_saddr_dst {
1184         const struct in6_addr *addr;
1185         int ifindex;
1186         int scope;
1187         int label;
1188         unsigned int prefs;
1189 };
1190
1191 static inline int ipv6_saddr_preferred(int type)
1192 {
1193         if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1194                 return 1;
1195         return 0;
1196 }
1197
1198 static int ipv6_get_saddr_eval(struct net *net,
1199                                struct ipv6_saddr_score *score,
1200                                struct ipv6_saddr_dst *dst,
1201                                int i)
1202 {
1203         int ret;
1204
1205         if (i <= score->rule) {
1206                 switch (i) {
1207                 case IPV6_SADDR_RULE_SCOPE:
1208                         ret = score->scopedist;
1209                         break;
1210                 case IPV6_SADDR_RULE_PREFIX:
1211                         ret = score->matchlen;
1212                         break;
1213                 default:
1214                         ret = !!test_bit(i, score->scorebits);
1215                 }
1216                 goto out;
1217         }
1218
1219         switch (i) {
1220         case IPV6_SADDR_RULE_INIT:
1221                 /* Rule 0: remember if hiscore is not ready yet */
1222                 ret = !!score->ifa;
1223                 break;
1224         case IPV6_SADDR_RULE_LOCAL:
1225                 /* Rule 1: Prefer same address */
1226                 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1227                 break;
1228         case IPV6_SADDR_RULE_SCOPE:
1229                 /* Rule 2: Prefer appropriate scope
1230                  *
1231                  *      ret
1232                  *       ^
1233                  *    -1 |  d 15
1234                  *    ---+--+-+---> scope
1235                  *       |
1236                  *       |             d is scope of the destination.
1237                  *  B-d  |  \
1238                  *       |   \      <- smaller scope is better if
1239                  *  B-15 |    \        if scope is enough for destinaion.
1240                  *       |             ret = B - scope (-1 <= scope >= d <= 15).
1241                  * d-C-1 | /
1242                  *       |/         <- greater is better
1243                  *   -C  /             if scope is not enough for destination.
1244                  *      /|             ret = scope - C (-1 <= d < scope <= 15).
1245                  *
1246                  * d - C - 1 < B -15 (for all -1 <= d <= 15).
1247                  * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1248                  * Assume B = 0 and we get C > 29.
1249                  */
1250                 ret = __ipv6_addr_src_scope(score->addr_type);
1251                 if (ret >= dst->scope)
1252                         ret = -ret;
1253                 else
1254                         ret -= 128;     /* 30 is enough */
1255                 score->scopedist = ret;
1256                 break;
1257         case IPV6_SADDR_RULE_PREFERRED:
1258                 /* Rule 3: Avoid deprecated and optimistic addresses */
1259                 ret = ipv6_saddr_preferred(score->addr_type) ||
1260                       !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1261                 break;
1262 #ifdef CONFIG_IPV6_MIP6
1263         case IPV6_SADDR_RULE_HOA:
1264             {
1265                 /* Rule 4: Prefer home address */
1266                 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1267                 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1268                 break;
1269             }
1270 #endif
1271         case IPV6_SADDR_RULE_OIF:
1272                 /* Rule 5: Prefer outgoing interface */
1273                 ret = (!dst->ifindex ||
1274                        dst->ifindex == score->ifa->idev->dev->ifindex);
1275                 break;
1276         case IPV6_SADDR_RULE_LABEL:
1277                 /* Rule 6: Prefer matching label */
1278                 ret = ipv6_addr_label(net,
1279                                       &score->ifa->addr, score->addr_type,
1280                                       score->ifa->idev->dev->ifindex) == dst->label;
1281                 break;
1282 #ifdef CONFIG_IPV6_PRIVACY
1283         case IPV6_SADDR_RULE_PRIVACY:
1284             {
1285                 /* Rule 7: Prefer public address
1286                  * Note: prefer temporary address if use_tempaddr >= 2
1287                  */
1288                 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1289                                 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1290                                 score->ifa->idev->cnf.use_tempaddr >= 2;
1291                 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1292                 break;
1293             }
1294 #endif
1295         case IPV6_SADDR_RULE_ORCHID:
1296                 /* Rule 8-: Prefer ORCHID vs ORCHID or
1297                  *          non-ORCHID vs non-ORCHID
1298                  */
1299                 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1300                         ipv6_addr_orchid(dst->addr));
1301                 break;
1302         case IPV6_SADDR_RULE_PREFIX:
1303                 /* Rule 8: Use longest matching prefix */
1304                 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1305                 if (ret > score->ifa->prefix_len)
1306                         ret = score->ifa->prefix_len;
1307                 score->matchlen = ret;
1308                 break;
1309         default:
1310                 ret = 0;
1311         }
1312
1313         if (ret)
1314                 __set_bit(i, score->scorebits);
1315         score->rule = i;
1316 out:
1317         return ret;
1318 }
1319
1320 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1321                        const struct in6_addr *daddr, unsigned int prefs,
1322                        struct in6_addr *saddr)
1323 {
1324         struct ipv6_saddr_score scores[2],
1325                                 *score = &scores[0], *hiscore = &scores[1];
1326         struct ipv6_saddr_dst dst;
1327         struct net_device *dev;
1328         int dst_type;
1329
1330         dst_type = __ipv6_addr_type(daddr);
1331         dst.addr = daddr;
1332         dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1333         dst.scope = __ipv6_addr_src_scope(dst_type);
1334         dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1335         dst.prefs = prefs;
1336
1337         hiscore->rule = -1;
1338         hiscore->ifa = NULL;
1339
1340         rcu_read_lock();
1341
1342         for_each_netdev_rcu(net, dev) {
1343                 struct inet6_dev *idev;
1344
1345                 /* Candidate Source Address (section 4)
1346                  *  - multicast and link-local destination address,
1347                  *    the set of candidate source address MUST only
1348                  *    include addresses assigned to interfaces
1349                  *    belonging to the same link as the outgoing
1350                  *    interface.
1351                  * (- For site-local destination addresses, the
1352                  *    set of candidate source addresses MUST only
1353                  *    include addresses assigned to interfaces
1354                  *    belonging to the same site as the outgoing
1355                  *    interface.)
1356                  */
1357                 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1358                      dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1359                     dst.ifindex && dev->ifindex != dst.ifindex)
1360                         continue;
1361
1362                 idev = __in6_dev_get(dev);
1363                 if (!idev)
1364                         continue;
1365
1366                 read_lock_bh(&idev->lock);
1367                 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1368                         int i;
1369
1370                         /*
1371                          * - Tentative Address (RFC2462 section 5.4)
1372                          *  - A tentative address is not considered
1373                          *    "assigned to an interface" in the traditional
1374                          *    sense, unless it is also flagged as optimistic.
1375                          * - Candidate Source Address (section 4)
1376                          *  - In any case, anycast addresses, multicast
1377                          *    addresses, and the unspecified address MUST
1378                          *    NOT be included in a candidate set.
1379                          */
1380                         if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1381                             (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1382                                 continue;
1383
1384                         score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1385
1386                         if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1387                                      score->addr_type & IPV6_ADDR_MULTICAST)) {
1388                                 LIMIT_NETDEBUG(KERN_DEBUG
1389                                                "ADDRCONF: unspecified / multicast address "
1390                                                "assigned as unicast address on %s",
1391                                                dev->name);
1392                                 continue;
1393                         }
1394
1395                         score->rule = -1;
1396                         bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1397
1398                         for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1399                                 int minihiscore, miniscore;
1400
1401                                 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1402                                 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1403
1404                                 if (minihiscore > miniscore) {
1405                                         if (i == IPV6_SADDR_RULE_SCOPE &&
1406                                             score->scopedist > 0) {
1407                                                 /*
1408                                                  * special case:
1409                                                  * each remaining entry
1410                                                  * has too small (not enough)
1411                                                  * scope, because ifa entries
1412                                                  * are sorted by their scope
1413                                                  * values.
1414                                                  */
1415                                                 goto try_nextdev;
1416                                         }
1417                                         break;
1418                                 } else if (minihiscore < miniscore) {
1419                                         if (hiscore->ifa)
1420                                                 in6_ifa_put(hiscore->ifa);
1421
1422                                         in6_ifa_hold(score->ifa);
1423
1424                                         swap(hiscore, score);
1425
1426                                         /* restore our iterator */
1427                                         score->ifa = hiscore->ifa;
1428
1429                                         break;
1430                                 }
1431                         }
1432                 }
1433 try_nextdev:
1434                 read_unlock_bh(&idev->lock);
1435         }
1436         rcu_read_unlock();
1437
1438         if (!hiscore->ifa)
1439                 return -EADDRNOTAVAIL;
1440
1441         *saddr = hiscore->ifa->addr;
1442         in6_ifa_put(hiscore->ifa);
1443         return 0;
1444 }
1445 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1446
1447 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1448                     unsigned char banned_flags)
1449 {
1450         struct inet6_dev *idev;
1451         int err = -EADDRNOTAVAIL;
1452
1453         rcu_read_lock();
1454         idev = __in6_dev_get(dev);
1455         if (idev) {
1456                 struct inet6_ifaddr *ifp;
1457
1458                 read_lock_bh(&idev->lock);
1459                 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1460                         if (ifp->scope == IFA_LINK &&
1461                             !(ifp->flags & banned_flags)) {
1462                                 *addr = ifp->addr;
1463                                 err = 0;
1464                                 break;
1465                         }
1466                 }
1467                 read_unlock_bh(&idev->lock);
1468         }
1469         rcu_read_unlock();
1470         return err;
1471 }
1472
1473 static int ipv6_count_addresses(struct inet6_dev *idev)
1474 {
1475         int cnt = 0;
1476         struct inet6_ifaddr *ifp;
1477
1478         read_lock_bh(&idev->lock);
1479         list_for_each_entry(ifp, &idev->addr_list, if_list)
1480                 cnt++;
1481         read_unlock_bh(&idev->lock);
1482         return cnt;
1483 }
1484
1485 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1486                   struct net_device *dev, int strict)
1487 {
1488         struct inet6_ifaddr *ifp;
1489         unsigned int hash = inet6_addr_hash(addr);
1490
1491         rcu_read_lock_bh();
1492         hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1493                 if (!net_eq(dev_net(ifp->idev->dev), net))
1494                         continue;
1495                 if (ipv6_addr_equal(&ifp->addr, addr) &&
1496                     !(ifp->flags&IFA_F_TENTATIVE) &&
1497                     (dev == NULL || ifp->idev->dev == dev ||
1498                      !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1499                         rcu_read_unlock_bh();
1500                         return 1;
1501                 }
1502         }
1503
1504         rcu_read_unlock_bh();
1505         return 0;
1506 }
1507 EXPORT_SYMBOL(ipv6_chk_addr);
1508
1509 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1510                                struct net_device *dev)
1511 {
1512         unsigned int hash = inet6_addr_hash(addr);
1513         struct inet6_ifaddr *ifp;
1514
1515         hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1516                 if (!net_eq(dev_net(ifp->idev->dev), net))
1517                         continue;
1518                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1519                         if (dev == NULL || ifp->idev->dev == dev)
1520                                 return true;
1521                 }
1522         }
1523         return false;
1524 }
1525
1526 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1527 {
1528         struct inet6_dev *idev;
1529         struct inet6_ifaddr *ifa;
1530         int     onlink;
1531
1532         onlink = 0;
1533         rcu_read_lock();
1534         idev = __in6_dev_get(dev);
1535         if (idev) {
1536                 read_lock_bh(&idev->lock);
1537                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1538                         onlink = ipv6_prefix_equal(addr, &ifa->addr,
1539                                                    ifa->prefix_len);
1540                         if (onlink)
1541                                 break;
1542                 }
1543                 read_unlock_bh(&idev->lock);
1544         }
1545         rcu_read_unlock();
1546         return onlink;
1547 }
1548 EXPORT_SYMBOL(ipv6_chk_prefix);
1549
1550 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1551                                      struct net_device *dev, int strict)
1552 {
1553         struct inet6_ifaddr *ifp, *result = NULL;
1554         unsigned int hash = inet6_addr_hash(addr);
1555
1556         rcu_read_lock_bh();
1557         hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1558                 if (!net_eq(dev_net(ifp->idev->dev), net))
1559                         continue;
1560                 if (ipv6_addr_equal(&ifp->addr, addr)) {
1561                         if (dev == NULL || ifp->idev->dev == dev ||
1562                             !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1563                                 result = ifp;
1564                                 in6_ifa_hold(ifp);
1565                                 break;
1566                         }
1567                 }
1568         }
1569         rcu_read_unlock_bh();
1570
1571         return result;
1572 }
1573
1574 /* Gets referenced address, destroys ifaddr */
1575
1576 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1577 {
1578         if (ifp->flags&IFA_F_PERMANENT) {
1579                 spin_lock_bh(&ifp->lock);
1580                 addrconf_del_timer(ifp);
1581                 ifp->flags |= IFA_F_TENTATIVE;
1582                 if (dad_failed)
1583                         ifp->flags |= IFA_F_DADFAILED;
1584                 spin_unlock_bh(&ifp->lock);
1585                 if (dad_failed)
1586                         ipv6_ifa_notify(0, ifp);
1587                 in6_ifa_put(ifp);
1588 #ifdef CONFIG_IPV6_PRIVACY
1589         } else if (ifp->flags&IFA_F_TEMPORARY) {
1590                 struct inet6_ifaddr *ifpub;
1591                 spin_lock_bh(&ifp->lock);
1592                 ifpub = ifp->ifpub;
1593                 if (ifpub) {
1594                         in6_ifa_hold(ifpub);
1595                         spin_unlock_bh(&ifp->lock);
1596                         ipv6_create_tempaddr(ifpub, ifp);
1597                         in6_ifa_put(ifpub);
1598                 } else {
1599                         spin_unlock_bh(&ifp->lock);
1600                 }
1601                 ipv6_del_addr(ifp);
1602 #endif
1603         } else
1604                 ipv6_del_addr(ifp);
1605 }
1606
1607 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1608 {
1609         int err = -ENOENT;
1610
1611         spin_lock(&ifp->state_lock);
1612         if (ifp->state == INET6_IFADDR_STATE_DAD) {
1613                 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1614                 err = 0;
1615         }
1616         spin_unlock(&ifp->state_lock);
1617
1618         return err;
1619 }
1620
1621 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1622 {
1623         struct inet6_dev *idev = ifp->idev;
1624
1625         if (addrconf_dad_end(ifp)) {
1626                 in6_ifa_put(ifp);
1627                 return;
1628         }
1629
1630         net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1631                              ifp->idev->dev->name, &ifp->addr);
1632
1633         if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1634                 struct in6_addr addr;
1635
1636                 addr.s6_addr32[0] = htonl(0xfe800000);
1637                 addr.s6_addr32[1] = 0;
1638
1639                 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1640                     ipv6_addr_equal(&ifp->addr, &addr)) {
1641                         /* DAD failed for link-local based on MAC address */
1642                         idev->cnf.disable_ipv6 = 1;
1643
1644                         pr_info("%s: IPv6 being disabled!\n",
1645                                 ifp->idev->dev->name);
1646                 }
1647         }
1648
1649         addrconf_dad_stop(ifp, 1);
1650 }
1651
1652 /* Join to solicited addr multicast group. */
1653
1654 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1655 {
1656         struct in6_addr maddr;
1657
1658         if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1659                 return;
1660
1661         addrconf_addr_solict_mult(addr, &maddr);
1662         ipv6_dev_mc_inc(dev, &maddr);
1663 }
1664
1665 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1666 {
1667         struct in6_addr maddr;
1668
1669         if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1670                 return;
1671
1672         addrconf_addr_solict_mult(addr, &maddr);
1673         __ipv6_dev_mc_dec(idev, &maddr);
1674 }
1675
1676 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1677 {
1678         struct in6_addr addr;
1679         if (ifp->prefix_len == 127) /* RFC 6164 */
1680                 return;
1681         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1682         if (ipv6_addr_any(&addr))
1683                 return;
1684         ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1685 }
1686
1687 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1688 {
1689         struct in6_addr addr;
1690         if (ifp->prefix_len == 127) /* RFC 6164 */
1691                 return;
1692         ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1693         if (ipv6_addr_any(&addr))
1694                 return;
1695         __ipv6_dev_ac_dec(ifp->idev, &addr);
1696 }
1697
1698 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1699 {
1700         if (dev->addr_len != ETH_ALEN)
1701                 return -1;
1702         memcpy(eui, dev->dev_addr, 3);
1703         memcpy(eui + 5, dev->dev_addr + 3, 3);
1704
1705         /*
1706          * The zSeries OSA network cards can be shared among various
1707          * OS instances, but the OSA cards have only one MAC address.
1708          * This leads to duplicate address conflicts in conjunction
1709          * with IPv6 if more than one instance uses the same card.
1710          *
1711          * The driver for these cards can deliver a unique 16-bit
1712          * identifier for each instance sharing the same card.  It is
1713          * placed instead of 0xFFFE in the interface identifier.  The
1714          * "u" bit of the interface identifier is not inverted in this
1715          * case.  Hence the resulting interface identifier has local
1716          * scope according to RFC2373.
1717          */
1718         if (dev->dev_id) {
1719                 eui[3] = (dev->dev_id >> 8) & 0xFF;
1720                 eui[4] = dev->dev_id & 0xFF;
1721         } else {
1722                 eui[3] = 0xFF;
1723                 eui[4] = 0xFE;
1724                 eui[0] ^= 2;
1725         }
1726         return 0;
1727 }
1728
1729 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1730 {
1731         if (dev->addr_len != IEEE802154_ADDR_LEN)
1732                 return -1;
1733         memcpy(eui, dev->dev_addr, 8);
1734         eui[0] ^= 2;
1735         return 0;
1736 }
1737
1738 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1739 {
1740         /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1741         if (dev->addr_len != ARCNET_ALEN)
1742                 return -1;
1743         memset(eui, 0, 7);
1744         eui[7] = *(u8 *)dev->dev_addr;
1745         return 0;
1746 }
1747
1748 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1749 {
1750         if (dev->addr_len != INFINIBAND_ALEN)
1751                 return -1;
1752         memcpy(eui, dev->dev_addr + 12, 8);
1753         eui[0] |= 2;
1754         return 0;
1755 }
1756
1757 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1758 {
1759         if (addr == 0)
1760                 return -1;
1761         eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1762                   ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1763                   ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1764                   ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1765                   ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1766                   ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1767         eui[1] = 0;
1768         eui[2] = 0x5E;
1769         eui[3] = 0xFE;
1770         memcpy(eui + 4, &addr, 4);
1771         return 0;
1772 }
1773
1774 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1775 {
1776         if (dev->priv_flags & IFF_ISATAP)
1777                 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1778         return -1;
1779 }
1780
1781 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1782 {
1783         return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1784 }
1785
1786 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1787 {
1788         switch (dev->type) {
1789         case ARPHRD_ETHER:
1790         case ARPHRD_FDDI:
1791                 return addrconf_ifid_eui48(eui, dev);
1792         case ARPHRD_ARCNET:
1793                 return addrconf_ifid_arcnet(eui, dev);
1794         case ARPHRD_INFINIBAND:
1795                 return addrconf_ifid_infiniband(eui, dev);
1796         case ARPHRD_SIT:
1797                 return addrconf_ifid_sit(eui, dev);
1798         case ARPHRD_IPGRE:
1799                 return addrconf_ifid_gre(eui, dev);
1800         case ARPHRD_IEEE802154:
1801                 return addrconf_ifid_eui64(eui, dev);
1802         }
1803         return -1;
1804 }
1805
1806 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1807 {
1808         int err = -1;
1809         struct inet6_ifaddr *ifp;
1810
1811         read_lock_bh(&idev->lock);
1812         list_for_each_entry(ifp, &idev->addr_list, if_list) {
1813                 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1814                         memcpy(eui, ifp->addr.s6_addr+8, 8);
1815                         err = 0;
1816                         break;
1817                 }
1818         }
1819         read_unlock_bh(&idev->lock);
1820         return err;
1821 }
1822
1823 #ifdef CONFIG_IPV6_PRIVACY
1824 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1825 static void __ipv6_regen_rndid(struct inet6_dev *idev)
1826 {
1827 regen:
1828         get_random_bytes(idev->rndid, sizeof(idev->rndid));
1829         idev->rndid[0] &= ~0x02;
1830
1831         /*
1832          * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1833          * check if generated address is not inappropriate
1834          *
1835          *  - Reserved subnet anycast (RFC 2526)
1836          *      11111101 11....11 1xxxxxxx
1837          *  - ISATAP (RFC4214) 6.1
1838          *      00-00-5E-FE-xx-xx-xx-xx
1839          *  - value 0
1840          *  - XXX: already assigned to an address on the device
1841          */
1842         if (idev->rndid[0] == 0xfd &&
1843             (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1844             (idev->rndid[7]&0x80))
1845                 goto regen;
1846         if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1847                 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1848                         goto regen;
1849                 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1850                         goto regen;
1851         }
1852 }
1853
1854 static void ipv6_regen_rndid(unsigned long data)
1855 {
1856         struct inet6_dev *idev = (struct inet6_dev *) data;
1857         unsigned long expires;
1858
1859         rcu_read_lock_bh();
1860         write_lock_bh(&idev->lock);
1861
1862         if (idev->dead)
1863                 goto out;
1864
1865         __ipv6_regen_rndid(idev);
1866
1867         expires = jiffies +
1868                 idev->cnf.temp_prefered_lft * HZ -
1869                 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1870                 idev->cnf.max_desync_factor * HZ;
1871         if (time_before(expires, jiffies)) {
1872                 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1873                         __func__, idev->dev->name);
1874                 goto out;
1875         }
1876
1877         if (!mod_timer(&idev->regen_timer, expires))
1878                 in6_dev_hold(idev);
1879
1880 out:
1881         write_unlock_bh(&idev->lock);
1882         rcu_read_unlock_bh();
1883         in6_dev_put(idev);
1884 }
1885
1886 static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
1887 {
1888         if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1889                 __ipv6_regen_rndid(idev);
1890 }
1891 #endif
1892
1893 /*
1894  *      Add prefix route.
1895  */
1896
1897 static void
1898 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1899                       unsigned long expires, u32 flags)
1900 {
1901         struct fib6_config cfg = {
1902                 .fc_table = RT6_TABLE_PREFIX,
1903                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1904                 .fc_ifindex = dev->ifindex,
1905                 .fc_expires = expires,
1906                 .fc_dst_len = plen,
1907                 .fc_flags = RTF_UP | flags,
1908                 .fc_nlinfo.nl_net = dev_net(dev),
1909                 .fc_protocol = RTPROT_KERNEL,
1910         };
1911
1912         cfg.fc_dst = *pfx;
1913
1914         /* Prevent useless cloning on PtP SIT.
1915            This thing is done here expecting that the whole
1916            class of non-broadcast devices need not cloning.
1917          */
1918 #if IS_ENABLED(CONFIG_IPV6_SIT)
1919         if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1920                 cfg.fc_flags |= RTF_NONEXTHOP;
1921 #endif
1922
1923         ip6_route_add(&cfg);
1924 }
1925
1926
1927 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1928                                                   int plen,
1929                                                   const struct net_device *dev,
1930                                                   u32 flags, u32 noflags)
1931 {
1932         struct fib6_node *fn;
1933         struct rt6_info *rt = NULL;
1934         struct fib6_table *table;
1935
1936         table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1937         if (table == NULL)
1938                 return NULL;
1939
1940         read_lock_bh(&table->tb6_lock);
1941         fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1942         if (!fn)
1943                 goto out;
1944         for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1945                 if (rt->dst.dev->ifindex != dev->ifindex)
1946                         continue;
1947                 if ((rt->rt6i_flags & flags) != flags)
1948                         continue;
1949                 if ((rt->rt6i_flags & noflags) != 0)
1950                         continue;
1951                 dst_hold(&rt->dst);
1952                 break;
1953         }
1954 out:
1955         read_unlock_bh(&table->tb6_lock);
1956         return rt;
1957 }
1958
1959
1960 /* Create "default" multicast route to the interface */
1961
1962 static void addrconf_add_mroute(struct net_device *dev)
1963 {
1964         struct fib6_config cfg = {
1965                 .fc_table = RT6_TABLE_LOCAL,
1966                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1967                 .fc_ifindex = dev->ifindex,
1968                 .fc_dst_len = 8,
1969                 .fc_flags = RTF_UP,
1970                 .fc_nlinfo.nl_net = dev_net(dev),
1971         };
1972
1973         ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1974
1975         ip6_route_add(&cfg);
1976 }
1977
1978 #if IS_ENABLED(CONFIG_IPV6_SIT)
1979 static void sit_route_add(struct net_device *dev)
1980 {
1981         struct fib6_config cfg = {
1982                 .fc_table = RT6_TABLE_MAIN,
1983                 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1984                 .fc_ifindex = dev->ifindex,
1985                 .fc_dst_len = 96,
1986                 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1987                 .fc_nlinfo.nl_net = dev_net(dev),
1988         };
1989
1990         /* prefix length - 96 bits "::d.d.d.d" */
1991         ip6_route_add(&cfg);
1992 }
1993 #endif
1994
1995 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1996 {
1997         struct inet6_dev *idev;
1998
1999         ASSERT_RTNL();
2000
2001         idev = ipv6_find_idev(dev);
2002         if (!idev)
2003                 return ERR_PTR(-ENOBUFS);
2004
2005         if (idev->cnf.disable_ipv6)
2006                 return ERR_PTR(-EACCES);
2007
2008         /* Add default multicast route */
2009         if (!(dev->flags & IFF_LOOPBACK))
2010                 addrconf_add_mroute(dev);
2011
2012         return idev;
2013 }
2014
2015 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2016 {
2017         struct prefix_info *pinfo;
2018         __u32 valid_lft;
2019         __u32 prefered_lft;
2020         int addr_type;
2021         struct inet6_dev *in6_dev;
2022         struct net *net = dev_net(dev);
2023
2024         pinfo = (struct prefix_info *) opt;
2025
2026         if (len < sizeof(struct prefix_info)) {
2027                 ADBG(("addrconf: prefix option too short\n"));
2028                 return;
2029         }
2030
2031         /*
2032          *      Validation checks ([ADDRCONF], page 19)
2033          */
2034
2035         addr_type = ipv6_addr_type(&pinfo->prefix);
2036
2037         if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2038                 return;
2039
2040         valid_lft = ntohl(pinfo->valid);
2041         prefered_lft = ntohl(pinfo->prefered);
2042
2043         if (prefered_lft > valid_lft) {
2044                 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2045                 return;
2046         }
2047
2048         in6_dev = in6_dev_get(dev);
2049
2050         if (in6_dev == NULL) {
2051                 net_dbg_ratelimited("addrconf: device %s not configured\n",
2052                                     dev->name);
2053                 return;
2054         }
2055
2056         /*
2057          *      Two things going on here:
2058          *      1) Add routes for on-link prefixes
2059          *      2) Configure prefixes with the auto flag set
2060          */
2061
2062         if (pinfo->onlink) {
2063                 struct rt6_info *rt;
2064                 unsigned long rt_expires;
2065
2066                 /* Avoid arithmetic overflow. Really, we could
2067                  * save rt_expires in seconds, likely valid_lft,
2068                  * but it would require division in fib gc, that it
2069                  * not good.
2070                  */
2071                 if (HZ > USER_HZ)
2072                         rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2073                 else
2074                         rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2075
2076                 if (addrconf_finite_timeout(rt_expires))
2077                         rt_expires *= HZ;
2078
2079                 rt = addrconf_get_prefix_route(&pinfo->prefix,
2080                                                pinfo->prefix_len,
2081                                                dev,
2082                                                RTF_ADDRCONF | RTF_PREFIX_RT,
2083                                                RTF_GATEWAY | RTF_DEFAULT);
2084
2085                 if (rt) {
2086                         /* Autoconf prefix route */
2087                         if (valid_lft == 0) {
2088                                 ip6_del_rt(rt);
2089                                 rt = NULL;
2090                         } else if (addrconf_finite_timeout(rt_expires)) {
2091                                 /* not infinity */
2092                                 rt6_set_expires(rt, jiffies + rt_expires);
2093                         } else {
2094                                 rt6_clean_expires(rt);
2095                         }
2096                 } else if (valid_lft) {
2097                         clock_t expires = 0;
2098                         int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2099                         if (addrconf_finite_timeout(rt_expires)) {
2100                                 /* not infinity */
2101                                 flags |= RTF_EXPIRES;
2102                                 expires = jiffies_to_clock_t(rt_expires);
2103                         }
2104                         addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2105                                               dev, expires, flags);
2106                 }
2107                 ip6_rt_put(rt);
2108         }
2109
2110         /* Try to figure out our local address for this prefix */
2111
2112         if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2113                 struct inet6_ifaddr *ifp;
2114                 struct in6_addr addr;
2115                 int create = 0, update_lft = 0;
2116
2117                 if (pinfo->prefix_len == 64) {
2118                         memcpy(&addr, &pinfo->prefix, 8);
2119                         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2120                             ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2121                                 in6_dev_put(in6_dev);
2122                                 return;
2123                         }
2124                         goto ok;
2125                 }
2126                 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2127                                     pinfo->prefix_len);
2128                 in6_dev_put(in6_dev);
2129                 return;
2130
2131 ok:
2132
2133                 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2134
2135                 if (ifp == NULL && valid_lft) {
2136                         int max_addresses = in6_dev->cnf.max_addresses;
2137                         u32 addr_flags = 0;
2138
2139 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2140                         if (in6_dev->cnf.optimistic_dad &&
2141                             !net->ipv6.devconf_all->forwarding && sllao)
2142                                 addr_flags = IFA_F_OPTIMISTIC;
2143 #endif
2144
2145                         /* Do not allow to create too much of autoconfigured
2146                          * addresses; this would be too easy way to crash kernel.
2147                          */
2148                         if (!max_addresses ||
2149                             ipv6_count_addresses(in6_dev) < max_addresses)
2150                                 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
2151                                                     addr_type&IPV6_ADDR_SCOPE_MASK,
2152                                                     addr_flags);
2153
2154                         if (IS_ERR_OR_NULL(ifp)) {
2155                                 in6_dev_put(in6_dev);
2156                                 return;
2157                         }
2158
2159                         update_lft = create = 1;
2160                         ifp->cstamp = jiffies;
2161                         addrconf_dad_start(ifp);
2162                 }
2163
2164                 if (ifp) {
2165                         int flags;
2166                         unsigned long now;
2167 #ifdef CONFIG_IPV6_PRIVACY
2168                         struct inet6_ifaddr *ift;
2169 #endif
2170                         u32 stored_lft;
2171
2172                         /* update lifetime (RFC2462 5.5.3 e) */
2173                         spin_lock(&ifp->lock);
2174                         now = jiffies;
2175                         if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2176                                 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2177                         else
2178                                 stored_lft = 0;
2179                         if (!update_lft && stored_lft) {
2180                                 if (valid_lft > MIN_VALID_LIFETIME ||
2181                                     valid_lft > stored_lft)
2182                                         update_lft = 1;
2183                                 else if (stored_lft <= MIN_VALID_LIFETIME) {
2184                                         /* valid_lft <= stored_lft is always true */
2185                                         /*
2186                                          * RFC 4862 Section 5.5.3e:
2187                                          * "Note that the preferred lifetime of
2188                                          *  the corresponding address is always
2189                                          *  reset to the Preferred Lifetime in
2190                                          *  the received Prefix Information
2191                                          *  option, regardless of whether the
2192                                          *  valid lifetime is also reset or
2193                                          *  ignored."
2194                                          *
2195                                          *  So if the preferred lifetime in
2196                                          *  this advertisement is different
2197                                          *  than what we have stored, but the
2198                                          *  valid lifetime is invalid, just
2199                                          *  reset prefered_lft.
2200                                          *
2201                                          *  We must set the valid lifetime
2202                                          *  to the stored lifetime since we'll
2203                                          *  be updating the timestamp below,
2204                                          *  else we'll set it back to the
2205                                          *  minimum.
2206                                          */
2207                                         if (prefered_lft != ifp->prefered_lft) {
2208                                                 valid_lft = stored_lft;
2209                                                 update_lft = 1;
2210                                         }
2211                                 } else {
2212                                         valid_lft = MIN_VALID_LIFETIME;
2213                                         if (valid_lft < prefered_lft)
2214                                                 prefered_lft = valid_lft;
2215                                         update_lft = 1;
2216                                 }
2217                         }
2218
2219                         if (update_lft) {
2220                                 ifp->valid_lft = valid_lft;
2221                                 ifp->prefered_lft = prefered_lft;
2222                                 ifp->tstamp = now;
2223                                 flags = ifp->flags;
2224                                 ifp->flags &= ~IFA_F_DEPRECATED;
2225                                 spin_unlock(&ifp->lock);
2226
2227                                 if (!(flags&IFA_F_TENTATIVE))
2228                                         ipv6_ifa_notify(0, ifp);
2229                         } else
2230                                 spin_unlock(&ifp->lock);
2231
2232 #ifdef CONFIG_IPV6_PRIVACY
2233                         read_lock_bh(&in6_dev->lock);
2234                         /* update all temporary addresses in the list */
2235                         list_for_each_entry(ift, &in6_dev->tempaddr_list,
2236                                             tmp_list) {
2237                                 int age, max_valid, max_prefered;
2238
2239                                 if (ifp != ift->ifpub)
2240                                         continue;
2241
2242                                 /*
2243                                  * RFC 4941 section 3.3:
2244                                  * If a received option will extend the lifetime
2245                                  * of a public address, the lifetimes of
2246                                  * temporary addresses should be extended,
2247                                  * subject to the overall constraint that no
2248                                  * temporary addresses should ever remain
2249                                  * "valid" or "preferred" for a time longer than
2250                                  * (TEMP_VALID_LIFETIME) or
2251                                  * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2252                                  * respectively.
2253                                  */
2254                                 age = (now - ift->cstamp) / HZ;
2255                                 max_valid = in6_dev->cnf.temp_valid_lft - age;
2256                                 if (max_valid < 0)
2257                                         max_valid = 0;
2258
2259                                 max_prefered = in6_dev->cnf.temp_prefered_lft -
2260                                                in6_dev->cnf.max_desync_factor -
2261                                                age;
2262                                 if (max_prefered < 0)
2263                                         max_prefered = 0;
2264
2265                                 if (valid_lft > max_valid)
2266                                         valid_lft = max_valid;
2267
2268                                 if (prefered_lft > max_prefered)
2269                                         prefered_lft = max_prefered;
2270
2271                                 spin_lock(&ift->lock);
2272                                 flags = ift->flags;
2273                                 ift->valid_lft = valid_lft;
2274                                 ift->prefered_lft = prefered_lft;
2275                                 ift->tstamp = now;
2276                                 if (prefered_lft > 0)
2277                                         ift->flags &= ~IFA_F_DEPRECATED;
2278
2279                                 spin_unlock(&ift->lock);
2280                                 if (!(flags&IFA_F_TENTATIVE))
2281                                         ipv6_ifa_notify(0, ift);
2282                         }
2283
2284                         if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
2285                                 /*
2286                                  * When a new public address is created as
2287                                  * described in [ADDRCONF], also create a new
2288                                  * temporary address. Also create a temporary
2289                                  * address if it's enabled but no temporary
2290                                  * address currently exists.
2291                                  */
2292                                 read_unlock_bh(&in6_dev->lock);
2293                                 ipv6_create_tempaddr(ifp, NULL);
2294                         } else {
2295                                 read_unlock_bh(&in6_dev->lock);
2296                         }
2297 #endif
2298                         in6_ifa_put(ifp);
2299                         addrconf_verify(0);
2300                 }
2301         }
2302         inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2303         in6_dev_put(in6_dev);
2304 }
2305
2306 /*
2307  *      Set destination address.
2308  *      Special case for SIT interfaces where we create a new "virtual"
2309  *      device.
2310  */
2311 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2312 {
2313         struct in6_ifreq ireq;
2314         struct net_device *dev;
2315         int err = -EINVAL;
2316
2317         rtnl_lock();
2318
2319         err = -EFAULT;
2320         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2321                 goto err_exit;
2322
2323         dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2324
2325         err = -ENODEV;
2326         if (dev == NULL)
2327                 goto err_exit;
2328
2329 #if IS_ENABLED(CONFIG_IPV6_SIT)
2330         if (dev->type == ARPHRD_SIT) {
2331                 const struct net_device_ops *ops = dev->netdev_ops;
2332                 struct ifreq ifr;
2333                 struct ip_tunnel_parm p;
2334
2335                 err = -EADDRNOTAVAIL;
2336                 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2337                         goto err_exit;
2338
2339                 memset(&p, 0, sizeof(p));
2340                 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2341                 p.iph.saddr = 0;
2342                 p.iph.version = 4;
2343                 p.iph.ihl = 5;
2344                 p.iph.protocol = IPPROTO_IPV6;
2345                 p.iph.ttl = 64;
2346                 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2347
2348                 if (ops->ndo_do_ioctl) {
2349                         mm_segment_t oldfs = get_fs();
2350
2351                         set_fs(KERNEL_DS);
2352                         err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2353                         set_fs(oldfs);
2354                 } else
2355                         err = -EOPNOTSUPP;
2356
2357                 if (err == 0) {
2358                         err = -ENOBUFS;
2359                         dev = __dev_get_by_name(net, p.name);
2360                         if (!dev)
2361                                 goto err_exit;
2362                         err = dev_open(dev);
2363                 }
2364         }
2365 #endif
2366
2367 err_exit:
2368         rtnl_unlock();
2369         return err;
2370 }
2371
2372 /*
2373  *      Manual configuration of address on an interface
2374  */
2375 static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2376                           unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2377                           __u32 valid_lft)
2378 {
2379         struct inet6_ifaddr *ifp;
2380         struct inet6_dev *idev;
2381         struct net_device *dev;
2382         int scope;
2383         u32 flags;
2384         clock_t expires;
2385         unsigned long timeout;
2386
2387         ASSERT_RTNL();
2388
2389         if (plen > 128)
2390                 return -EINVAL;
2391
2392         /* check the lifetime */
2393         if (!valid_lft || prefered_lft > valid_lft)
2394                 return -EINVAL;
2395
2396         dev = __dev_get_by_index(net, ifindex);
2397         if (!dev)
2398                 return -ENODEV;
2399
2400         idev = addrconf_add_dev(dev);
2401         if (IS_ERR(idev))
2402                 return PTR_ERR(idev);
2403
2404         scope = ipv6_addr_scope(pfx);
2405
2406         timeout = addrconf_timeout_fixup(valid_lft, HZ);
2407         if (addrconf_finite_timeout(timeout)) {
2408                 expires = jiffies_to_clock_t(timeout * HZ);
2409                 valid_lft = timeout;
2410                 flags = RTF_EXPIRES;
2411         } else {
2412                 expires = 0;
2413                 flags = 0;
2414                 ifa_flags |= IFA_F_PERMANENT;
2415         }
2416
2417         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2418         if (addrconf_finite_timeout(timeout)) {
2419                 if (timeout == 0)
2420                         ifa_flags |= IFA_F_DEPRECATED;
2421                 prefered_lft = timeout;
2422         }
2423
2424         ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2425
2426         if (!IS_ERR(ifp)) {
2427                 spin_lock_bh(&ifp->lock);
2428                 ifp->valid_lft = valid_lft;
2429                 ifp->prefered_lft = prefered_lft;
2430                 ifp->tstamp = jiffies;
2431                 spin_unlock_bh(&ifp->lock);
2432
2433                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2434                                       expires, flags);
2435                 /*
2436                  * Note that section 3.1 of RFC 4429 indicates
2437                  * that the Optimistic flag should not be set for
2438                  * manually configured addresses
2439                  */
2440                 addrconf_dad_start(ifp);
2441                 in6_ifa_put(ifp);
2442                 addrconf_verify(0);
2443                 return 0;
2444         }
2445
2446         return PTR_ERR(ifp);
2447 }
2448
2449 static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
2450                           unsigned int plen)
2451 {
2452         struct inet6_ifaddr *ifp;
2453         struct inet6_dev *idev;
2454         struct net_device *dev;
2455
2456         if (plen > 128)
2457                 return -EINVAL;
2458
2459         dev = __dev_get_by_index(net, ifindex);
2460         if (!dev)
2461                 return -ENODEV;
2462
2463         if ((idev = __in6_dev_get(dev)) == NULL)
2464                 return -ENXIO;
2465
2466         read_lock_bh(&idev->lock);
2467         list_for_each_entry(ifp, &idev->addr_list, if_list) {
2468                 if (ifp->prefix_len == plen &&
2469                     ipv6_addr_equal(pfx, &ifp->addr)) {
2470                         in6_ifa_hold(ifp);
2471                         read_unlock_bh(&idev->lock);
2472
2473                         ipv6_del_addr(ifp);
2474
2475                         /* If the last address is deleted administratively,
2476                            disable IPv6 on this interface.
2477                          */
2478                         if (list_empty(&idev->addr_list))
2479                                 addrconf_ifdown(idev->dev, 1);
2480                         return 0;
2481                 }
2482         }
2483         read_unlock_bh(&idev->lock);
2484         return -EADDRNOTAVAIL;
2485 }
2486
2487
2488 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2489 {
2490         struct in6_ifreq ireq;
2491         int err;
2492
2493         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2494                 return -EPERM;
2495
2496         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2497                 return -EFAULT;
2498
2499         rtnl_lock();
2500         err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2501                              ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2502                              INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2503         rtnl_unlock();
2504         return err;
2505 }
2506
2507 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2508 {
2509         struct in6_ifreq ireq;
2510         int err;
2511
2512         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2513                 return -EPERM;
2514
2515         if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2516                 return -EFAULT;
2517
2518         rtnl_lock();
2519         err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2520                              ireq.ifr6_prefixlen);
2521         rtnl_unlock();
2522         return err;
2523 }
2524
2525 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2526                      int plen, int scope)
2527 {
2528         struct inet6_ifaddr *ifp;
2529
2530         ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2531         if (!IS_ERR(ifp)) {
2532                 spin_lock_bh(&ifp->lock);
2533                 ifp->flags &= ~IFA_F_TENTATIVE;
2534                 spin_unlock_bh(&ifp->lock);
2535                 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2536                 in6_ifa_put(ifp);
2537         }
2538 }
2539
2540 #if IS_ENABLED(CONFIG_IPV6_SIT)
2541 static void sit_add_v4_addrs(struct inet6_dev *idev)
2542 {
2543         struct in6_addr addr;
2544         struct net_device *dev;
2545         struct net *net = dev_net(idev->dev);
2546         int scope;
2547
2548         ASSERT_RTNL();
2549
2550         memset(&addr, 0, sizeof(struct in6_addr));
2551         memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2552
2553         if (idev->dev->flags&IFF_POINTOPOINT) {
2554                 addr.s6_addr32[0] = htonl(0xfe800000);
2555                 scope = IFA_LINK;
2556         } else {
2557                 scope = IPV6_ADDR_COMPATv4;
2558         }
2559
2560         if (addr.s6_addr32[3]) {
2561                 add_addr(idev, &addr, 128, scope);
2562                 return;
2563         }
2564
2565         for_each_netdev(net, dev) {
2566                 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2567                 if (in_dev && (dev->flags & IFF_UP)) {
2568                         struct in_ifaddr *ifa;
2569
2570                         int flag = scope;
2571
2572                         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2573                                 int plen;
2574
2575                                 addr.s6_addr32[3] = ifa->ifa_local;
2576
2577                                 if (ifa->ifa_scope == RT_SCOPE_LINK)
2578                                         continue;
2579                                 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2580                                         if (idev->dev->flags&IFF_POINTOPOINT)
2581                                                 continue;
2582                                         flag |= IFA_HOST;
2583                                 }
2584                                 if (idev->dev->flags&IFF_POINTOPOINT)
2585                                         plen = 64;
2586                                 else
2587                                         plen = 96;
2588
2589                                 add_addr(idev, &addr, plen, flag);
2590                         }
2591                 }
2592         }
2593 }
2594 #endif
2595
2596 static void init_loopback(struct net_device *dev)
2597 {
2598         struct inet6_dev  *idev;
2599
2600         /* ::1 */
2601
2602         ASSERT_RTNL();
2603
2604         if ((idev = ipv6_find_idev(dev)) == NULL) {
2605                 pr_debug("%s: add_dev failed\n", __func__);
2606                 return;
2607         }
2608
2609         add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2610 }
2611
2612 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2613 {
2614         struct inet6_ifaddr *ifp;
2615         u32 addr_flags = IFA_F_PERMANENT;
2616
2617 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2618         if (idev->cnf.optimistic_dad &&
2619             !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2620                 addr_flags |= IFA_F_OPTIMISTIC;
2621 #endif
2622
2623
2624         ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2625         if (!IS_ERR(ifp)) {
2626                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2627                 addrconf_dad_start(ifp);
2628                 in6_ifa_put(ifp);
2629         }
2630 }
2631
2632 static void addrconf_dev_config(struct net_device *dev)
2633 {
2634         struct in6_addr addr;
2635         struct inet6_dev *idev;
2636
2637         ASSERT_RTNL();
2638
2639         if ((dev->type != ARPHRD_ETHER) &&
2640             (dev->type != ARPHRD_FDDI) &&
2641             (dev->type != ARPHRD_ARCNET) &&
2642             (dev->type != ARPHRD_INFINIBAND) &&
2643             (dev->type != ARPHRD_IEEE802154)) {
2644                 /* Alas, we support only Ethernet autoconfiguration. */
2645                 return;
2646         }
2647
2648         idev = addrconf_add_dev(dev);
2649         if (IS_ERR(idev))
2650                 return;
2651
2652         memset(&addr, 0, sizeof(struct in6_addr));
2653         addr.s6_addr32[0] = htonl(0xFE800000);
2654
2655         if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2656                 addrconf_add_linklocal(idev, &addr);
2657 }
2658
2659 #if IS_ENABLED(CONFIG_IPV6_SIT)
2660 static void addrconf_sit_config(struct net_device *dev)
2661 {
2662         struct inet6_dev *idev;
2663
2664         ASSERT_RTNL();
2665
2666         /*
2667          * Configure the tunnel with one of our IPv4
2668          * addresses... we should configure all of
2669          * our v4 addrs in the tunnel
2670          */
2671
2672         if ((idev = ipv6_find_idev(dev)) == NULL) {
2673                 pr_debug("%s: add_dev failed\n", __func__);
2674                 return;
2675         }
2676
2677         if (dev->priv_flags & IFF_ISATAP) {
2678                 struct in6_addr addr;
2679
2680                 ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2681                 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2682                 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2683                         addrconf_add_linklocal(idev, &addr);
2684                 return;
2685         }
2686
2687         sit_add_v4_addrs(idev);
2688
2689         if (dev->flags&IFF_POINTOPOINT)
2690                 addrconf_add_mroute(dev);
2691         else
2692                 sit_route_add(dev);
2693 }
2694 #endif
2695
2696 #if IS_ENABLED(CONFIG_NET_IPGRE)
2697 static void addrconf_gre_config(struct net_device *dev)
2698 {
2699         struct inet6_dev *idev;
2700         struct in6_addr addr;
2701
2702         pr_info("%s(%s)\n", __func__, dev->name);
2703
2704         ASSERT_RTNL();
2705
2706         if ((idev = ipv6_find_idev(dev)) == NULL) {
2707                 pr_debug("%s: add_dev failed\n", __func__);
2708                 return;
2709         }
2710
2711         ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2712         addrconf_prefix_route(&addr, 64, dev, 0, 0);
2713
2714         if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2715                 addrconf_add_linklocal(idev, &addr);
2716 }
2717 #endif
2718
2719 static inline int
2720 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2721 {
2722         struct in6_addr lladdr;
2723
2724         if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2725                 addrconf_add_linklocal(idev, &lladdr);
2726                 return 0;
2727         }
2728         return -1;
2729 }
2730
2731 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2732 {
2733         struct net_device *link_dev;
2734         struct net *net = dev_net(idev->dev);
2735
2736         /* first try to inherit the link-local address from the link device */
2737         if (idev->dev->iflink &&
2738             (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2739                 if (!ipv6_inherit_linklocal(idev, link_dev))
2740                         return;
2741         }
2742         /* then try to inherit it from any device */
2743         for_each_netdev(net, link_dev) {
2744                 if (!ipv6_inherit_linklocal(idev, link_dev))
2745                         return;
2746         }
2747         pr_debug("init ip6-ip6: add_linklocal failed\n");
2748 }
2749
2750 /*
2751  * Autoconfigure tunnel with a link-local address so routing protocols,
2752  * DHCPv6, MLD etc. can be run over the virtual link
2753  */
2754
2755 static void addrconf_ip6_tnl_config(struct net_device *dev)
2756 {
2757         struct inet6_dev *idev;
2758
2759         ASSERT_RTNL();
2760
2761         idev = addrconf_add_dev(dev);
2762         if (IS_ERR(idev)) {
2763                 pr_debug("init ip6-ip6: add_dev failed\n");
2764                 return;
2765         }
2766         ip6_tnl_add_linklocal(idev);
2767 }
2768
2769 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2770                            void *data)
2771 {
2772         struct net_device *dev = (struct net_device *) data;
2773         struct inet6_dev *idev = __in6_dev_get(dev);
2774         int run_pending = 0;
2775         int err;
2776
2777         switch (event) {
2778         case NETDEV_REGISTER:
2779                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2780                         idev = ipv6_add_dev(dev);
2781                         if (!idev)
2782                                 return notifier_from_errno(-ENOMEM);
2783                 }
2784                 break;
2785
2786         case NETDEV_UP:
2787         case NETDEV_CHANGE:
2788                 if (dev->flags & IFF_SLAVE)
2789                         break;
2790
2791                 if (event == NETDEV_UP) {
2792                         if (!addrconf_qdisc_ok(dev)) {
2793                                 /* device is not ready yet. */
2794                                 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2795                                         dev->name);
2796                                 break;
2797                         }
2798
2799                         if (!idev && dev->mtu >= IPV6_MIN_MTU)
2800                                 idev = ipv6_add_dev(dev);
2801
2802                         if (idev) {
2803                                 idev->if_flags |= IF_READY;
2804                                 run_pending = 1;
2805                         }
2806                 } else {
2807                         if (!addrconf_qdisc_ok(dev)) {
2808                                 /* device is still not ready. */
2809                                 break;
2810                         }
2811
2812                         if (idev) {
2813                                 if (idev->if_flags & IF_READY)
2814                                         /* device is already configured. */
2815                                         break;
2816                                 idev->if_flags |= IF_READY;
2817                         }
2818
2819                         pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2820                                 dev->name);
2821
2822                         run_pending = 1;
2823                 }
2824
2825                 switch (dev->type) {
2826 #if IS_ENABLED(CONFIG_IPV6_SIT)
2827                 case ARPHRD_SIT:
2828                         addrconf_sit_config(dev);
2829                         break;
2830 #endif
2831 #if IS_ENABLED(CONFIG_NET_IPGRE)
2832                 case ARPHRD_IPGRE:
2833                         addrconf_gre_config(dev);
2834                         break;
2835 #endif
2836                 case ARPHRD_TUNNEL6:
2837                         addrconf_ip6_tnl_config(dev);
2838                         break;
2839                 case ARPHRD_LOOPBACK:
2840                         init_loopback(dev);
2841                         break;
2842
2843                 default:
2844                         addrconf_dev_config(dev);
2845                         break;
2846                 }
2847
2848                 if (idev) {
2849                         if (run_pending)
2850                                 addrconf_dad_run(idev);
2851
2852                         /*
2853                          * If the MTU changed during the interface down,
2854                          * when the interface up, the changed MTU must be
2855                          * reflected in the idev as well as routers.
2856                          */
2857                         if (idev->cnf.mtu6 != dev->mtu &&
2858                             dev->mtu >= IPV6_MIN_MTU) {
2859                                 rt6_mtu_change(dev, dev->mtu);
2860                                 idev->cnf.mtu6 = dev->mtu;
2861                         }
2862                         idev->tstamp = jiffies;
2863                         inet6_ifinfo_notify(RTM_NEWLINK, idev);
2864
2865                         /*
2866                          * If the changed mtu during down is lower than
2867                          * IPV6_MIN_MTU stop IPv6 on this interface.
2868                          */
2869                         if (dev->mtu < IPV6_MIN_MTU)
2870                                 addrconf_ifdown(dev, 1);
2871                 }
2872                 break;
2873
2874         case NETDEV_CHANGEMTU:
2875                 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2876                         rt6_mtu_change(dev, dev->mtu);
2877                         idev->cnf.mtu6 = dev->mtu;
2878                         break;
2879                 }
2880
2881                 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2882                         idev = ipv6_add_dev(dev);
2883                         if (idev)
2884                                 break;
2885                 }
2886
2887                 /*
2888                  * MTU falled under IPV6_MIN_MTU.
2889                  * Stop IPv6 on this interface.
2890                  */
2891
2892         case NETDEV_DOWN:
2893         case NETDEV_UNREGISTER:
2894                 /*
2895                  *      Remove all addresses from this interface.
2896                  */
2897                 addrconf_ifdown(dev, event != NETDEV_DOWN);
2898                 break;
2899
2900         case NETDEV_CHANGENAME:
2901                 if (idev) {
2902                         snmp6_unregister_dev(idev);
2903                         addrconf_sysctl_unregister(idev);
2904                         addrconf_sysctl_register(idev);
2905                         err = snmp6_register_dev(idev);
2906                         if (err)
2907                                 return notifier_from_errno(err);
2908                 }
2909                 break;
2910
2911         case NETDEV_PRE_TYPE_CHANGE:
2912         case NETDEV_POST_TYPE_CHANGE:
2913                 addrconf_type_change(dev, event);
2914                 break;
2915         }
2916
2917         return NOTIFY_OK;
2918 }
2919
2920 /*
2921  *      addrconf module should be notified of a device going up
2922  */
2923 static struct notifier_block ipv6_dev_notf = {
2924         .notifier_call = addrconf_notify,
2925 };
2926
2927 static void addrconf_type_change(struct net_device *dev, unsigned long event)
2928 {
2929         struct inet6_dev *idev;
2930         ASSERT_RTNL();
2931
2932         idev = __in6_dev_get(dev);
2933
2934         if (event == NETDEV_POST_TYPE_CHANGE)
2935                 ipv6_mc_remap(idev);
2936         else if (event == NETDEV_PRE_TYPE_CHANGE)
2937                 ipv6_mc_unmap(idev);
2938 }
2939
2940 static int addrconf_ifdown(struct net_device *dev, int how)
2941 {
2942         struct net *net = dev_net(dev);
2943         struct inet6_dev *idev;
2944         struct inet6_ifaddr *ifa;
2945         int state, i;
2946
2947         ASSERT_RTNL();
2948
2949         rt6_ifdown(net, dev);
2950         neigh_ifdown(&nd_tbl, dev);
2951
2952         idev = __in6_dev_get(dev);
2953         if (idev == NULL)
2954                 return -ENODEV;
2955
2956         /*
2957          * Step 1: remove reference to ipv6 device from parent device.
2958          *         Do not dev_put!
2959          */
2960         if (how) {
2961                 idev->dead = 1;
2962
2963                 /* protected by rtnl_lock */
2964                 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
2965
2966                 /* Step 1.5: remove snmp6 entry */
2967                 snmp6_unregister_dev(idev);
2968
2969         }
2970
2971         /* Step 2: clear hash table */
2972         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2973                 struct hlist_head *h = &inet6_addr_lst[i];
2974
2975                 spin_lock_bh(&addrconf_hash_lock);
2976         restart:
2977                 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
2978                         if (ifa->idev == idev) {
2979                                 hlist_del_init_rcu(&ifa->addr_lst);
2980                                 addrconf_del_timer(ifa);
2981                                 goto restart;
2982                         }
2983                 }
2984                 spin_unlock_bh(&addrconf_hash_lock);
2985         }
2986
2987         write_lock_bh(&idev->lock);
2988
2989         /* Step 2: clear flags for stateless addrconf */
2990         if (!how)
2991                 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2992
2993 #ifdef CONFIG_IPV6_PRIVACY
2994         if (how && del_timer(&idev->regen_timer))
2995                 in6_dev_put(idev);
2996
2997         /* Step 3: clear tempaddr list */
2998         while (!list_empty(&idev->tempaddr_list)) {
2999                 ifa = list_first_entry(&idev->tempaddr_list,
3000                                        struct inet6_ifaddr, tmp_list);
3001                 list_del(&ifa->tmp_list);
3002                 write_unlock_bh(&idev->lock);
3003                 spin_lock_bh(&ifa->lock);
3004
3005                 if (ifa->ifpub) {
3006                         in6_ifa_put(ifa->ifpub);
3007                         ifa->ifpub = NULL;
3008                 }
3009                 spin_unlock_bh(&ifa->lock);
3010                 in6_ifa_put(ifa);
3011                 write_lock_bh(&idev->lock);
3012         }
3013 #endif
3014
3015         while (!list_empty(&idev->addr_list)) {
3016                 ifa = list_first_entry(&idev->addr_list,
3017                                        struct inet6_ifaddr, if_list);
3018                 addrconf_del_timer(ifa);
3019
3020                 list_del(&ifa->if_list);
3021
3022                 write_unlock_bh(&idev->lock);
3023
3024                 spin_lock_bh(&ifa->state_lock);
3025                 state = ifa->state;
3026                 ifa->state = INET6_IFADDR_STATE_DEAD;
3027                 spin_unlock_bh(&ifa->state_lock);
3028
3029                 if (state != INET6_IFADDR_STATE_DEAD) {
3030                         __ipv6_ifa_notify(RTM_DELADDR, ifa);
3031                         atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
3032                 }
3033                 in6_ifa_put(ifa);
3034
3035                 write_lock_bh(&idev->lock);
3036         }
3037
3038         write_unlock_bh(&idev->lock);
3039
3040         /* Step 5: Discard multicast list */
3041         if (how)
3042                 ipv6_mc_destroy_dev(idev);
3043         else
3044                 ipv6_mc_down(idev);
3045
3046         idev->tstamp = jiffies;
3047
3048         /* Last: Shot the device (if unregistered) */
3049         if (how) {
3050                 addrconf_sysctl_unregister(idev);
3051                 neigh_parms_release(&nd_tbl, idev->nd_parms);
3052                 neigh_ifdown(&nd_tbl, dev);
3053                 in6_dev_put(idev);
3054         }
3055         return 0;
3056 }
3057
3058 static void addrconf_rs_timer(unsigned long data)
3059 {
3060         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3061         struct inet6_dev *idev = ifp->idev;
3062
3063         read_lock(&idev->lock);
3064         if (idev->dead || !(idev->if_flags & IF_READY))
3065                 goto out;
3066
3067         if (!ipv6_accept_ra(idev))
3068                 goto out;
3069
3070         /* Announcement received after solicitation was sent */
3071         if (idev->if_flags & IF_RA_RCVD)
3072                 goto out;
3073
3074         spin_lock(&ifp->lock);
3075         if (ifp->probes++ < idev->cnf.rtr_solicits) {
3076                 /* The wait after the last probe can be shorter */
3077                 addrconf_mod_timer(ifp, AC_RS,
3078                                    (ifp->probes == idev->cnf.rtr_solicits) ?
3079                                    idev->cnf.rtr_solicit_delay :
3080                                    idev->cnf.rtr_solicit_interval);
3081                 spin_unlock(&ifp->lock);
3082
3083                 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
3084         } else {
3085                 spin_unlock(&ifp->lock);
3086                 /*
3087                  * Note: we do not support deprecated "all on-link"
3088                  * assumption any longer.
3089                  */
3090                 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3091         }
3092
3093 out:
3094         read_unlock(&idev->lock);
3095         in6_ifa_put(ifp);
3096 }
3097
3098 /*
3099  *      Duplicate Address Detection
3100  */
3101 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3102 {
3103         unsigned long rand_num;
3104         struct inet6_dev *idev = ifp->idev;
3105
3106         if (ifp->flags & IFA_F_OPTIMISTIC)
3107                 rand_num = 0;
3108         else
3109                 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3110
3111         ifp->probes = idev->cnf.dad_transmits;
3112         addrconf_mod_timer(ifp, AC_DAD, rand_num);
3113 }
3114
3115 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3116 {
3117         struct inet6_dev *idev = ifp->idev;
3118         struct net_device *dev = idev->dev;
3119
3120         addrconf_join_solict(dev, &ifp->addr);
3121
3122         net_srandom(ifp->addr.s6_addr32[3]);
3123
3124         read_lock_bh(&idev->lock);
3125         spin_lock(&ifp->lock);
3126         if (ifp->state == INET6_IFADDR_STATE_DEAD)
3127                 goto out;
3128
3129         if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3130             idev->cnf.accept_dad < 1 ||
3131             !(ifp->flags&IFA_F_TENTATIVE) ||
3132             ifp->flags & IFA_F_NODAD) {
3133                 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3134                 spin_unlock(&ifp->lock);
3135                 read_unlock_bh(&idev->lock);
3136
3137                 addrconf_dad_completed(ifp);
3138                 return;
3139         }
3140
3141         if (!(idev->if_flags & IF_READY)) {
3142                 spin_unlock(&ifp->lock);
3143                 read_unlock_bh(&idev->lock);
3144                 /*
3145                  * If the device is not ready:
3146                  * - keep it tentative if it is a permanent address.
3147                  * - otherwise, kill it.
3148                  */
3149                 in6_ifa_hold(ifp);
3150                 addrconf_dad_stop(ifp, 0);
3151                 return;
3152         }
3153
3154         /*
3155          * Optimistic nodes can start receiving
3156          * Frames right away
3157          */
3158         if (ifp->flags & IFA_F_OPTIMISTIC)
3159                 ip6_ins_rt(ifp->rt);
3160
3161         addrconf_dad_kick(ifp);
3162 out:
3163         spin_unlock(&ifp->lock);
3164         read_unlock_bh(&idev->lock);
3165 }
3166
3167 static void addrconf_dad_timer(unsigned long data)
3168 {
3169         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3170         struct inet6_dev *idev = ifp->idev;
3171         struct in6_addr mcaddr;
3172
3173         if (!ifp->probes && addrconf_dad_end(ifp))
3174                 goto out;
3175
3176         read_lock(&idev->lock);
3177         if (idev->dead || !(idev->if_flags & IF_READY)) {
3178                 read_unlock(&idev->lock);
3179                 goto out;
3180         }
3181
3182         spin_lock(&ifp->lock);
3183         if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3184                 spin_unlock(&ifp->lock);
3185                 read_unlock(&idev->lock);
3186                 goto out;
3187         }
3188
3189         if (ifp->probes == 0) {
3190                 /*
3191                  * DAD was successful
3192                  */
3193
3194                 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3195                 spin_unlock(&ifp->lock);
3196                 read_unlock(&idev->lock);
3197
3198                 addrconf_dad_completed(ifp);
3199
3200                 goto out;
3201         }
3202
3203         ifp->probes--;
3204         addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
3205         spin_unlock(&ifp->lock);
3206         read_unlock(&idev->lock);
3207
3208         /* send a neighbour solicitation for our addr */
3209         addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3210         ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3211 out:
3212         in6_ifa_put(ifp);
3213 }
3214
3215 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3216 {
3217         struct net_device *dev = ifp->idev->dev;
3218
3219         /*
3220          *      Configure the address for reception. Now it is valid.
3221          */
3222
3223         ipv6_ifa_notify(RTM_NEWADDR, ifp);
3224
3225         /* If added prefix is link local and we are prepared to process
3226            router advertisements, start sending router solicitations.
3227          */
3228
3229         if (ipv6_accept_ra(ifp->idev) &&
3230             ifp->idev->cnf.rtr_solicits > 0 &&
3231             (dev->flags&IFF_LOOPBACK) == 0 &&
3232             (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
3233                 /*
3234                  *      If a host as already performed a random delay
3235                  *      [...] as part of DAD [...] there is no need
3236                  *      to delay again before sending the first RS
3237                  */
3238                 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
3239
3240                 spin_lock_bh(&ifp->lock);
3241                 ifp->probes = 1;
3242                 ifp->idev->if_flags |= IF_RS_SENT;
3243                 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3244                 spin_unlock_bh(&ifp->lock);
3245         }
3246 }
3247
3248 static void addrconf_dad_run(struct inet6_dev *idev)
3249 {
3250         struct inet6_ifaddr *ifp;
3251
3252         read_lock_bh(&idev->lock);
3253         list_for_each_entry(ifp, &idev->addr_list, if_list) {
3254                 spin_lock(&ifp->lock);
3255                 if (ifp->flags & IFA_F_TENTATIVE &&
3256                     ifp->state == INET6_IFADDR_STATE_DAD)
3257                         addrconf_dad_kick(ifp);
3258                 spin_unlock(&ifp->lock);
3259         }
3260         read_unlock_bh(&idev->lock);
3261 }
3262
3263 #ifdef CONFIG_PROC_FS
3264 struct if6_iter_state {
3265         struct seq_net_private p;
3266         int bucket;
3267         int offset;
3268 };
3269
3270 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3271 {
3272         struct inet6_ifaddr *ifa = NULL;
3273         struct if6_iter_state *state = seq->private;
3274         struct net *net = seq_file_net(seq);
3275         int p = 0;
3276
3277         /* initial bucket if pos is 0 */
3278         if (pos == 0) {
3279                 state->bucket = 0;
3280                 state->offset = 0;
3281         }
3282
3283         for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3284                 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3285                                          addr_lst) {
3286                         if (!net_eq(dev_net(ifa->idev->dev), net))
3287                                 continue;
3288                         /* sync with offset */
3289                         if (p < state->offset) {
3290                                 p++;
3291                                 continue;
3292                         }
3293                         state->offset++;
3294                         return ifa;
3295                 }
3296
3297                 /* prepare for next bucket */
3298                 state->offset = 0;
3299                 p = 0;
3300         }
3301         return NULL;
3302 }
3303
3304 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3305                                          struct inet6_ifaddr *ifa)
3306 {
3307         struct if6_iter_state *state = seq->private;
3308         struct net *net = seq_file_net(seq);
3309
3310         hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3311                 if (!net_eq(dev_net(ifa->idev->dev), net))
3312                         continue;
3313                 state->offset++;
3314                 return ifa;
3315         }
3316
3317         while (++state->bucket < IN6_ADDR_HSIZE) {
3318                 state->offset = 0;
3319                 hlist_for_each_entry_rcu_bh(ifa,
3320                                      &inet6_addr_lst[state->bucket], addr_lst) {
3321                         if (!net_eq(dev_net(ifa->idev->dev), net))
3322                                 continue;
3323                         state->offset++;
3324                         return ifa;
3325                 }
3326         }
3327
3328         return NULL;
3329 }
3330
3331 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3332         __acquires(rcu_bh)
3333 {
3334         rcu_read_lock_bh();
3335         return if6_get_first(seq, *pos);
3336 }
3337
3338 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3339 {
3340         struct inet6_ifaddr *ifa;
3341
3342         ifa = if6_get_next(seq, v);
3343         ++*pos;
3344         return ifa;
3345 }
3346
3347 static void if6_seq_stop(struct seq_file *seq, void *v)
3348         __releases(rcu_bh)
3349 {
3350         rcu_read_unlock_bh();
3351 }
3352
3353 static int if6_seq_show(struct seq_file *seq, void *v)
3354 {
3355         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3356         seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3357                    &ifp->addr,
3358                    ifp->idev->dev->ifindex,
3359                    ifp->prefix_len,
3360                    ifp->scope,
3361                    ifp->flags,
3362                    ifp->idev->dev->name);
3363         return 0;
3364 }
3365
3366 static const struct seq_operations if6_seq_ops = {
3367         .start  = if6_seq_start,
3368         .next   = if6_seq_next,
3369         .show   = if6_seq_show,
3370         .stop   = if6_seq_stop,
3371 };
3372
3373 static int if6_seq_open(struct inode *inode, struct file *file)
3374 {
3375         return seq_open_net(inode, file, &if6_seq_ops,
3376                             sizeof(struct if6_iter_state));
3377 }
3378
3379 static const struct file_operations if6_fops = {
3380         .owner          = THIS_MODULE,
3381         .open           = if6_seq_open,
3382         .read           = seq_read,
3383         .llseek         = seq_lseek,
3384         .release        = seq_release_net,
3385 };
3386
3387 static int __net_init if6_proc_net_init(struct net *net)
3388 {
3389         if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3390                 return -ENOMEM;
3391         return 0;
3392 }
3393
3394 static void __net_exit if6_proc_net_exit(struct net *net)
3395 {
3396         remove_proc_entry("if_inet6", net->proc_net);
3397 }
3398
3399 static struct pernet_operations if6_proc_net_ops = {
3400        .init = if6_proc_net_init,
3401        .exit = if6_proc_net_exit,
3402 };
3403
3404 int __init if6_proc_init(void)
3405 {
3406         return register_pernet_subsys(&if6_proc_net_ops);
3407 }
3408
3409 void if6_proc_exit(void)
3410 {
3411         unregister_pernet_subsys(&if6_proc_net_ops);
3412 }
3413 #endif  /* CONFIG_PROC_FS */
3414
3415 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3416 /* Check if address is a home address configured on any interface. */
3417 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3418 {
3419         int ret = 0;
3420         struct inet6_ifaddr *ifp = NULL;
3421         unsigned int hash = inet6_addr_hash(addr);
3422
3423         rcu_read_lock_bh();
3424         hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3425                 if (!net_eq(dev_net(ifp->idev->dev), net))
3426                         continue;
3427                 if (ipv6_addr_equal(&ifp->addr, addr) &&
3428                     (ifp->flags & IFA_F_HOMEADDRESS)) {
3429                         ret = 1;
3430                         break;
3431                 }
3432         }
3433         rcu_read_unlock_bh();
3434         return ret;
3435 }
3436 #endif
3437
3438 /*
3439  *      Periodic address status verification
3440  */
3441
3442 static void addrconf_verify(unsigned long foo)
3443 {
3444         unsigned long now, next, next_sec, next_sched;
3445         struct inet6_ifaddr *ifp;
3446         int i;
3447
3448         rcu_read_lock_bh();
3449         spin_lock(&addrconf_verify_lock);
3450         now = jiffies;
3451         next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3452
3453         del_timer(&addr_chk_timer);
3454
3455         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3456 restart:
3457                 hlist_for_each_entry_rcu_bh(ifp,
3458                                          &inet6_addr_lst[i], addr_lst) {
3459                         unsigned long age;
3460
3461                         if (ifp->flags & IFA_F_PERMANENT)
3462                                 continue;
3463
3464                         spin_lock(&ifp->lock);
3465                         /* We try to batch several events at once. */
3466                         age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3467
3468                         if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3469                             age >= ifp->valid_lft) {
3470                                 spin_unlock(&ifp->lock);
3471                                 in6_ifa_hold(ifp);
3472                                 ipv6_del_addr(ifp);
3473                                 goto restart;
3474                         } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3475                                 spin_unlock(&ifp->lock);
3476                                 continue;
3477                         } else if (age >= ifp->prefered_lft) {
3478                                 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3479                                 int deprecate = 0;
3480
3481                                 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3482                                         deprecate = 1;
3483                                         ifp->flags |= IFA_F_DEPRECATED;
3484                                 }
3485
3486                                 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3487                                         next = ifp->tstamp + ifp->valid_lft * HZ;
3488
3489                                 spin_unlock(&ifp->lock);
3490
3491                                 if (deprecate) {
3492                                         in6_ifa_hold(ifp);
3493
3494                                         ipv6_ifa_notify(0, ifp);
3495                                         in6_ifa_put(ifp);
3496                                         goto restart;
3497                                 }
3498 #ifdef CONFIG_IPV6_PRIVACY
3499                         } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3500                                    !(ifp->flags&IFA_F_TENTATIVE)) {
3501                                 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3502                                         ifp->idev->cnf.dad_transmits *
3503                                         ifp->idev->nd_parms->retrans_time / HZ;
3504
3505                                 if (age >= ifp->prefered_lft - regen_advance) {
3506                                         struct inet6_ifaddr *ifpub = ifp->ifpub;
3507                                         if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3508                                                 next = ifp->tstamp + ifp->prefered_lft * HZ;
3509                                         if (!ifp->regen_count && ifpub) {
3510                                                 ifp->regen_count++;
3511                                                 in6_ifa_hold(ifp);
3512                                                 in6_ifa_hold(ifpub);
3513                                                 spin_unlock(&ifp->lock);
3514
3515                                                 spin_lock(&ifpub->lock);
3516                                                 ifpub->regen_count = 0;
3517                                                 spin_unlock(&ifpub->lock);
3518                                                 ipv6_create_tempaddr(ifpub, ifp);
3519                                                 in6_ifa_put(ifpub);
3520                                                 in6_ifa_put(ifp);
3521                                                 goto restart;
3522                                         }
3523                                 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3524                                         next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3525                                 spin_unlock(&ifp->lock);
3526 #endif
3527                         } else {
3528                                 /* ifp->prefered_lft <= ifp->valid_lft */
3529                                 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3530                                         next = ifp->tstamp + ifp->prefered_lft * HZ;
3531                                 spin_unlock(&ifp->lock);
3532                         }
3533                 }
3534         }
3535
3536         next_sec = round_jiffies_up(next);
3537         next_sched = next;
3538
3539         /* If rounded timeout is accurate enough, accept it. */
3540         if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3541                 next_sched = next_sec;
3542
3543         /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3544         if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3545                 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3546
3547         ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3548               now, next, next_sec, next_sched));
3549
3550         addr_chk_timer.expires = next_sched;
3551         add_timer(&addr_chk_timer);
3552         spin_unlock(&addrconf_verify_lock);
3553         rcu_read_unlock_bh();
3554 }
3555
3556 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3557 {
3558         struct in6_addr *pfx = NULL;
3559
3560         if (addr)
3561                 pfx = nla_data(addr);
3562
3563         if (local) {
3564                 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3565                         pfx = NULL;
3566                 else
3567                         pfx = nla_data(local);
3568         }
3569
3570         return pfx;
3571 }
3572
3573 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3574         [IFA_ADDRESS]           = { .len = sizeof(struct in6_addr) },
3575         [IFA_LOCAL]             = { .len = sizeof(struct in6_addr) },
3576         [IFA_CACHEINFO]         = { .len = sizeof(struct ifa_cacheinfo) },
3577 };
3578
3579 static int
3580 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3581 {
3582         struct net *net = sock_net(skb->sk);
3583         struct ifaddrmsg *ifm;
3584         struct nlattr *tb[IFA_MAX+1];
3585         struct in6_addr *pfx;
3586         int err;
3587
3588         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3589         if (err < 0)
3590                 return err;
3591
3592         ifm = nlmsg_data(nlh);
3593         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3594         if (pfx == NULL)
3595                 return -EINVAL;
3596
3597         return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3598 }
3599
3600 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3601                              u32 prefered_lft, u32 valid_lft)
3602 {
3603         u32 flags;
3604         clock_t expires;
3605         unsigned long timeout;
3606
3607         if (!valid_lft || (prefered_lft > valid_lft))
3608                 return -EINVAL;
3609
3610         timeout = addrconf_timeout_fixup(valid_lft, HZ);
3611         if (addrconf_finite_timeout(timeout)) {
3612                 expires = jiffies_to_clock_t(timeout * HZ);
3613                 valid_lft = timeout;
3614                 flags = RTF_EXPIRES;
3615         } else {
3616                 expires = 0;
3617                 flags = 0;
3618                 ifa_flags |= IFA_F_PERMANENT;
3619         }
3620
3621         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3622         if (addrconf_finite_timeout(timeout)) {
3623                 if (timeout == 0)
3624                         ifa_flags |= IFA_F_DEPRECATED;
3625                 prefered_lft = timeout;
3626         }
3627
3628         spin_lock_bh(&ifp->lock);
3629         ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3630         ifp->tstamp = jiffies;
3631         ifp->valid_lft = valid_lft;
3632         ifp->prefered_lft = prefered_lft;
3633
3634         spin_unlock_bh(&ifp->lock);
3635         if (!(ifp->flags&IFA_F_TENTATIVE))
3636                 ipv6_ifa_notify(0, ifp);
3637
3638         addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3639                               expires, flags);
3640         addrconf_verify(0);
3641
3642         return 0;
3643 }
3644
3645 static int
3646 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3647 {
3648         struct net *net = sock_net(skb->sk);
3649         struct ifaddrmsg *ifm;
3650         struct nlattr *tb[IFA_MAX+1];
3651         struct in6_addr *pfx;
3652         struct inet6_ifaddr *ifa;
3653         struct net_device *dev;
3654         u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3655         u8 ifa_flags;
3656         int err;
3657
3658         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3659         if (err < 0)
3660                 return err;
3661
3662         ifm = nlmsg_data(nlh);
3663         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3664         if (pfx == NULL)
3665                 return -EINVAL;
3666
3667         if (tb[IFA_CACHEINFO]) {
3668                 struct ifa_cacheinfo *ci;
3669
3670                 ci = nla_data(tb[IFA_CACHEINFO]);
3671                 valid_lft = ci->ifa_valid;
3672                 preferred_lft = ci->ifa_prefered;
3673         } else {
3674                 preferred_lft = INFINITY_LIFE_TIME;
3675                 valid_lft = INFINITY_LIFE_TIME;
3676         }
3677
3678         dev =  __dev_get_by_index(net, ifm->ifa_index);
3679         if (dev == NULL)
3680                 return -ENODEV;
3681
3682         /* We ignore other flags so far. */
3683         ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3684
3685         ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3686         if (ifa == NULL) {
3687                 /*
3688                  * It would be best to check for !NLM_F_CREATE here but
3689                  * userspace alreay relies on not having to provide this.
3690                  */
3691                 return inet6_addr_add(net, ifm->ifa_index, pfx,
3692                                       ifm->ifa_prefixlen, ifa_flags,
3693                                       preferred_lft, valid_lft);
3694         }
3695
3696         if (nlh->nlmsg_flags & NLM_F_EXCL ||
3697             !(nlh->nlmsg_flags & NLM_F_REPLACE))
3698                 err = -EEXIST;
3699         else
3700                 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3701
3702         in6_ifa_put(ifa);
3703
3704         return err;
3705 }
3706
3707 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3708                           u8 scope, int ifindex)
3709 {
3710         struct ifaddrmsg *ifm;
3711
3712         ifm = nlmsg_data(nlh);
3713         ifm->ifa_family = AF_INET6;
3714         ifm->ifa_prefixlen = prefixlen;
3715         ifm->ifa_flags = flags;
3716         ifm->ifa_scope = scope;
3717         ifm->ifa_index = ifindex;
3718 }
3719
3720 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3721                          unsigned long tstamp, u32 preferred, u32 valid)
3722 {
3723         struct ifa_cacheinfo ci;
3724
3725         ci.cstamp = cstamp_delta(cstamp);
3726         ci.tstamp = cstamp_delta(tstamp);
3727         ci.ifa_prefered = preferred;
3728         ci.ifa_valid = valid;
3729
3730         return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3731 }
3732
3733 static inline int rt_scope(int ifa_scope)
3734 {
3735         if (ifa_scope & IFA_HOST)
3736                 return RT_SCOPE_HOST;
3737         else if (ifa_scope & IFA_LINK)
3738                 return RT_SCOPE_LINK;
3739         else if (ifa_scope & IFA_SITE)
3740                 return RT_SCOPE_SITE;
3741         else
3742                 return RT_SCOPE_UNIVERSE;
3743 }
3744
3745 static inline int inet6_ifaddr_msgsize(void)
3746 {
3747         return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3748                + nla_total_size(16) /* IFA_ADDRESS */
3749                + nla_total_size(sizeof(struct ifa_cacheinfo));
3750 }
3751
3752 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3753                              u32 portid, u32 seq, int event, unsigned int flags)
3754 {
3755         struct nlmsghdr  *nlh;
3756         u32 preferred, valid;
3757
3758         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3759         if (nlh == NULL)
3760                 return -EMSGSIZE;
3761
3762         put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3763                       ifa->idev->dev->ifindex);
3764
3765         if (!(ifa->flags&IFA_F_PERMANENT)) {
3766                 preferred = ifa->prefered_lft;
3767                 valid = ifa->valid_lft;
3768                 if (preferred != INFINITY_LIFE_TIME) {
3769                         long tval = (jiffies - ifa->tstamp)/HZ;
3770                         if (preferred > tval)
3771                                 preferred -= tval;
3772                         else
3773                                 preferred = 0;
3774                         if (valid != INFINITY_LIFE_TIME) {
3775                                 if (valid > tval)
3776                                         valid -= tval;
3777                                 else
3778                                         valid = 0;
3779                         }
3780                 }
3781         } else {
3782                 preferred = INFINITY_LIFE_TIME;
3783                 valid = INFINITY_LIFE_TIME;
3784         }
3785
3786         if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3787             put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3788                 nlmsg_cancel(skb, nlh);
3789                 return -EMSGSIZE;
3790         }
3791
3792         return nlmsg_end(skb, nlh);
3793 }
3794
3795 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3796                                 u32 portid, u32 seq, int event, u16 flags)
3797 {
3798         struct nlmsghdr  *nlh;
3799         u8 scope = RT_SCOPE_UNIVERSE;
3800         int ifindex = ifmca->idev->dev->ifindex;
3801
3802         if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3803                 scope = RT_SCOPE_SITE;
3804
3805         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3806         if (nlh == NULL)
3807                 return -EMSGSIZE;
3808
3809         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3810         if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3811             put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3812                           INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3813                 nlmsg_cancel(skb, nlh);
3814                 return -EMSGSIZE;
3815         }
3816
3817         return nlmsg_end(skb, nlh);
3818 }
3819
3820 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3821                                 u32 portid, u32 seq, int event, unsigned int flags)
3822 {
3823         struct nlmsghdr  *nlh;
3824         u8 scope = RT_SCOPE_UNIVERSE;
3825         int ifindex = ifaca->aca_idev->dev->ifindex;
3826
3827         if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3828                 scope = RT_SCOPE_SITE;
3829
3830         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3831         if (nlh == NULL)
3832                 return -EMSGSIZE;
3833
3834         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3835         if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3836             put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3837                           INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3838                 nlmsg_cancel(skb, nlh);
3839                 return -EMSGSIZE;
3840         }
3841
3842         return nlmsg_end(skb, nlh);
3843 }
3844
3845 enum addr_type_t {
3846         UNICAST_ADDR,
3847         MULTICAST_ADDR,
3848         ANYCAST_ADDR,
3849 };
3850
3851 /* called with rcu_read_lock() */
3852 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3853                           struct netlink_callback *cb, enum addr_type_t type,
3854                           int s_ip_idx, int *p_ip_idx)
3855 {
3856         struct ifmcaddr6 *ifmca;
3857         struct ifacaddr6 *ifaca;
3858         int err = 1;
3859         int ip_idx = *p_ip_idx;
3860
3861         read_lock_bh(&idev->lock);
3862         switch (type) {
3863         case UNICAST_ADDR: {
3864                 struct inet6_ifaddr *ifa;
3865
3866                 /* unicast address incl. temp addr */
3867                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3868                         if (++ip_idx < s_ip_idx)
3869                                 continue;
3870                         err = inet6_fill_ifaddr(skb, ifa,
3871                                                 NETLINK_CB(cb->skb).portid,
3872                                                 cb->nlh->nlmsg_seq,
3873                                                 RTM_NEWADDR,
3874                                                 NLM_F_MULTI);
3875                         if (err <= 0)
3876                                 break;
3877                 }
3878                 break;
3879         }
3880         case MULTICAST_ADDR:
3881                 /* multicast address */
3882                 for (ifmca = idev->mc_list; ifmca;
3883                      ifmca = ifmca->next, ip_idx++) {
3884                         if (ip_idx < s_ip_idx)
3885                                 continue;
3886                         err = inet6_fill_ifmcaddr(skb, ifmca,
3887                                                   NETLINK_CB(cb->skb).portid,
3888                                                   cb->nlh->nlmsg_seq,
3889                                                   RTM_GETMULTICAST,
3890                                                   NLM_F_MULTI);
3891                         if (err <= 0)
3892                                 break;
3893                 }
3894                 break;
3895         case ANYCAST_ADDR:
3896                 /* anycast address */
3897                 for (ifaca = idev->ac_list; ifaca;
3898                      ifaca = ifaca->aca_next, ip_idx++) {
3899                         if (ip_idx < s_ip_idx)
3900                                 continue;
3901                         err = inet6_fill_ifacaddr(skb, ifaca,
3902                                                   NETLINK_CB(cb->skb).portid,
3903                                                   cb->nlh->nlmsg_seq,
3904                                                   RTM_GETANYCAST,
3905                                                   NLM_F_MULTI);
3906                         if (err <= 0)
3907                                 break;
3908                 }
3909                 break;
3910         default:
3911                 break;
3912         }
3913         read_unlock_bh(&idev->lock);
3914         *p_ip_idx = ip_idx;
3915         return err;
3916 }
3917
3918 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3919                            enum addr_type_t type)
3920 {
3921         struct net *net = sock_net(skb->sk);
3922         int h, s_h;
3923         int idx, ip_idx;
3924         int s_idx, s_ip_idx;
3925         struct net_device *dev;
3926         struct inet6_dev *idev;
3927         struct hlist_head *head;
3928
3929         s_h = cb->args[0];
3930         s_idx = idx = cb->args[1];
3931         s_ip_idx = ip_idx = cb->args[2];
3932
3933         rcu_read_lock();
3934         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3935                 idx = 0;
3936                 head = &net->dev_index_head[h];
3937                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
3938                         if (idx < s_idx)
3939                                 goto cont;
3940                         if (h > s_h || idx > s_idx)
3941                                 s_ip_idx = 0;
3942                         ip_idx = 0;
3943                         idev = __in6_dev_get(dev);
3944                         if (!idev)
3945                                 goto cont;
3946
3947                         if (in6_dump_addrs(idev, skb, cb, type,
3948                                            s_ip_idx, &ip_idx) <= 0)
3949                                 goto done;
3950 cont:
3951                         idx++;
3952                 }
3953         }
3954 done:
3955         rcu_read_unlock();
3956         cb->args[0] = h;
3957         cb->args[1] = idx;
3958         cb->args[2] = ip_idx;
3959
3960         return skb->len;
3961 }
3962
3963 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3964 {
3965         enum addr_type_t type = UNICAST_ADDR;
3966
3967         return inet6_dump_addr(skb, cb, type);
3968 }
3969
3970 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3971 {
3972         enum addr_type_t type = MULTICAST_ADDR;
3973
3974         return inet6_dump_addr(skb, cb, type);
3975 }
3976
3977
3978 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3979 {
3980         enum addr_type_t type = ANYCAST_ADDR;
3981
3982         return inet6_dump_addr(skb, cb, type);
3983 }
3984
3985 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
3986 {
3987         struct net *net = sock_net(in_skb->sk);
3988         struct ifaddrmsg *ifm;
3989         struct nlattr *tb[IFA_MAX+1];
3990         struct in6_addr *addr = NULL;
3991         struct net_device *dev = NULL;
3992         struct inet6_ifaddr *ifa;
3993         struct sk_buff *skb;
3994         int err;
3995
3996         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3997         if (err < 0)
3998                 goto errout;
3999
4000         addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
4001         if (addr == NULL) {
4002                 err = -EINVAL;
4003                 goto errout;
4004         }
4005
4006         ifm = nlmsg_data(nlh);
4007         if (ifm->ifa_index)
4008                 dev = __dev_get_by_index(net, ifm->ifa_index);
4009
4010         ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4011         if (!ifa) {
4012                 err = -EADDRNOTAVAIL;
4013                 goto errout;
4014         }
4015
4016         skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4017         if (!skb) {
4018                 err = -ENOBUFS;
4019                 goto errout_ifa;
4020         }
4021
4022         err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4023                                 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4024         if (err < 0) {
4025                 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4026                 WARN_ON(err == -EMSGSIZE);
4027                 kfree_skb(skb);
4028                 goto errout_ifa;
4029         }
4030         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4031 errout_ifa:
4032         in6_ifa_put(ifa);
4033 errout:
4034         return err;
4035 }
4036
4037 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4038 {
4039         struct sk_buff *skb;
4040         struct net *net = dev_net(ifa->idev->dev);
4041         int err = -ENOBUFS;
4042
4043         skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4044         if (skb == NULL)
4045                 goto errout;
4046
4047         err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4048         if (err < 0) {
4049                 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4050                 WARN_ON(err == -EMSGSIZE);
4051                 kfree_skb(skb);
4052                 goto errout;
4053         }
4054         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4055         return;
4056 errout:
4057         if (err < 0)
4058                 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4059 }
4060
4061 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4062                                 __s32 *array, int bytes)
4063 {
4064         BUG_ON(bytes < (DEVCONF_MAX * 4));
4065
4066         memset(array, 0, bytes);
4067         array[DEVCONF_FORWARDING] = cnf->forwarding;
4068         array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4069         array[DEVCONF_MTU6] = cnf->mtu6;
4070         array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4071         array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4072         array[DEVCONF_AUTOCONF] = cnf->autoconf;
4073         array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4074         array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4075         array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4076                 jiffies_to_msecs(cnf->rtr_solicit_interval);
4077         array[DEVCONF_RTR_SOLICIT_DELAY] =
4078                 jiffies_to_msecs(cnf->rtr_solicit_delay);
4079         array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4080 #ifdef CONFIG_IPV6_PRIVACY
4081         array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4082         array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4083         array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4084         array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4085         array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4086 #endif
4087         array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4088         array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4089         array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4090 #ifdef CONFIG_IPV6_ROUTER_PREF
4091         array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4092         array[DEVCONF_RTR_PROBE_INTERVAL] =
4093                 jiffies_to_msecs(cnf->rtr_probe_interval);
4094 #ifdef CONFIG_IPV6_ROUTE_INFO
4095         array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4096 #endif
4097 #endif
4098         array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4099         array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4100 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4101         array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4102 #endif
4103 #ifdef CONFIG_IPV6_MROUTE
4104         array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4105 #endif
4106         array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4107         array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4108         array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4109         array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4110 }
4111
4112 static inline size_t inet6_ifla6_size(void)
4113 {
4114         return nla_total_size(4) /* IFLA_INET6_FLAGS */
4115              + nla_total_size(sizeof(struct ifla_cacheinfo))
4116              + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4117              + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4118              + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
4119 }
4120
4121 static inline size_t inet6_if_nlmsg_size(void)
4122 {
4123         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4124                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4125                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4126                + nla_total_size(4) /* IFLA_MTU */
4127                + nla_total_size(4) /* IFLA_LINK */
4128                + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4129 }
4130
4131 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4132                                       int items, int bytes)
4133 {
4134         int i;
4135         int pad = bytes - sizeof(u64) * items;
4136         BUG_ON(pad < 0);
4137
4138         /* Use put_unaligned() because stats may not be aligned for u64. */
4139         put_unaligned(items, &stats[0]);
4140         for (i = 1; i < items; i++)
4141                 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4142
4143         memset(&stats[items], 0, pad);
4144 }
4145
4146 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4147                                       int items, int bytes, size_t syncpoff)
4148 {
4149         int i;
4150         int pad = bytes - sizeof(u64) * items;
4151         BUG_ON(pad < 0);
4152
4153         /* Use put_unaligned() because stats may not be aligned for u64. */
4154         put_unaligned(items, &stats[0]);
4155         for (i = 1; i < items; i++)
4156                 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4157
4158         memset(&stats[items], 0, pad);
4159 }
4160
4161 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4162                              int bytes)
4163 {
4164         switch (attrtype) {
4165         case IFLA_INET6_STATS:
4166                 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4167                                      IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4168                 break;
4169         case IFLA_INET6_ICMP6STATS:
4170                 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4171                 break;
4172         }
4173 }
4174
4175 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4176 {
4177         struct nlattr *nla;
4178         struct ifla_cacheinfo ci;
4179
4180         if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4181                 goto nla_put_failure;
4182         ci.max_reasm_len = IPV6_MAXPLEN;
4183         ci.tstamp = cstamp_delta(idev->tstamp);
4184         ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4185         ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
4186         if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4187                 goto nla_put_failure;
4188         nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4189         if (nla == NULL)
4190                 goto nla_put_failure;
4191         ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4192
4193         /* XXX - MC not implemented */
4194
4195         nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4196         if (nla == NULL)
4197                 goto nla_put_failure;
4198         snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4199
4200         nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4201         if (nla == NULL)
4202                 goto nla_put_failure;
4203         snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4204
4205         return 0;
4206
4207 nla_put_failure:
4208         return -EMSGSIZE;
4209 }
4210
4211 static size_t inet6_get_link_af_size(const struct net_device *dev)
4212 {
4213         if (!__in6_dev_get(dev))
4214                 return 0;
4215
4216         return inet6_ifla6_size();
4217 }
4218
4219 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4220 {
4221         struct inet6_dev *idev = __in6_dev_get(dev);
4222
4223         if (!idev)
4224                 return -ENODATA;
4225
4226         if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4227                 return -EMSGSIZE;
4228
4229         return 0;
4230 }
4231
4232 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4233                              u32 portid, u32 seq, int event, unsigned int flags)
4234 {
4235         struct net_device *dev = idev->dev;
4236         struct ifinfomsg *hdr;
4237         struct nlmsghdr *nlh;
4238         void *protoinfo;
4239
4240         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4241         if (nlh == NULL)
4242                 return -EMSGSIZE;
4243
4244         hdr = nlmsg_data(nlh);
4245         hdr->ifi_family = AF_INET6;
4246         hdr->__ifi_pad = 0;
4247         hdr->ifi_type = dev->type;
4248         hdr->ifi_index = dev->ifindex;
4249         hdr->ifi_flags = dev_get_flags(dev);
4250         hdr->ifi_change = 0;
4251
4252         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4253             (dev->addr_len &&
4254              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4255             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4256             (dev->ifindex != dev->iflink &&
4257              nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4258                 goto nla_put_failure;
4259         protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4260         if (protoinfo == NULL)
4261                 goto nla_put_failure;
4262
4263         if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4264                 goto nla_put_failure;
4265
4266         nla_nest_end(skb, protoinfo);
4267         return nlmsg_end(skb, nlh);
4268
4269 nla_put_failure:
4270         nlmsg_cancel(skb, nlh);
4271         return -EMSGSIZE;
4272 }
4273
4274 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4275 {
4276         struct net *net = sock_net(skb->sk);
4277         int h, s_h;
4278         int idx = 0, s_idx;
4279         struct net_device *dev;
4280         struct inet6_dev *idev;
4281         struct hlist_head *head;
4282
4283         s_h = cb->args[0];
4284         s_idx = cb->args[1];
4285
4286         rcu_read_lock();
4287         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4288                 idx = 0;
4289                 head = &net->dev_index_head[h];
4290                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4291                         if (idx < s_idx)
4292                                 goto cont;
4293                         idev = __in6_dev_get(dev);
4294                         if (!idev)
4295                                 goto cont;
4296                         if (inet6_fill_ifinfo(skb, idev,
4297                                               NETLINK_CB(cb->skb).portid,
4298                                               cb->nlh->nlmsg_seq,
4299                                               RTM_NEWLINK, NLM_F_MULTI) <= 0)
4300                                 goto out;
4301 cont:
4302                         idx++;
4303                 }
4304         }
4305 out:
4306         rcu_read_unlock();
4307         cb->args[1] = idx;
4308         cb->args[0] = h;
4309
4310         return skb->len;
4311 }
4312
4313 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4314 {
4315         struct sk_buff *skb;
4316         struct net *net = dev_net(idev->dev);
4317         int err = -ENOBUFS;
4318
4319         skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4320         if (skb == NULL)
4321                 goto errout;
4322
4323         err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4324         if (err < 0) {
4325                 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4326                 WARN_ON(err == -EMSGSIZE);
4327                 kfree_skb(skb);
4328                 goto errout;
4329         }
4330         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4331         return;
4332 errout:
4333         if (err < 0)
4334                 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4335 }
4336
4337 static inline size_t inet6_prefix_nlmsg_size(void)
4338 {
4339         return NLMSG_ALIGN(sizeof(struct prefixmsg))
4340                + nla_total_size(sizeof(struct in6_addr))
4341                + nla_total_size(sizeof(struct prefix_cacheinfo));
4342 }
4343
4344 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4345                              struct prefix_info *pinfo, u32 portid, u32 seq,
4346                              int event, unsigned int flags)
4347 {
4348         struct prefixmsg *pmsg;
4349         struct nlmsghdr *nlh;
4350         struct prefix_cacheinfo ci;
4351
4352         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4353         if (nlh == NULL)
4354                 return -EMSGSIZE;
4355
4356         pmsg = nlmsg_data(nlh);
4357         pmsg->prefix_family = AF_INET6;
4358         pmsg->prefix_pad1 = 0;
4359         pmsg->prefix_pad2 = 0;
4360         pmsg->prefix_ifindex = idev->dev->ifindex;
4361         pmsg->prefix_len = pinfo->prefix_len;
4362         pmsg->prefix_type = pinfo->type;
4363         pmsg->prefix_pad3 = 0;
4364         pmsg->prefix_flags = 0;
4365         if (pinfo->onlink)
4366                 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4367         if (pinfo->autoconf)
4368                 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4369
4370         if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4371                 goto nla_put_failure;
4372         ci.preferred_time = ntohl(pinfo->prefered);
4373         ci.valid_time = ntohl(pinfo->valid);
4374         if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4375                 goto nla_put_failure;
4376         return nlmsg_end(skb, nlh);
4377
4378 nla_put_failure:
4379         nlmsg_cancel(skb, nlh);
4380         return -EMSGSIZE;
4381 }
4382
4383 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4384                          struct prefix_info *pinfo)
4385 {
4386         struct sk_buff *skb;
4387         struct net *net = dev_net(idev->dev);
4388         int err = -ENOBUFS;
4389
4390         skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4391         if (skb == NULL)
4392                 goto errout;
4393
4394         err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
4395         if (err < 0) {
4396                 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4397                 WARN_ON(err == -EMSGSIZE);
4398                 kfree_skb(skb);
4399                 goto errout;
4400         }
4401         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4402         return;
4403 errout:
4404         if (err < 0)
4405                 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4406 }
4407
4408 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4409 {
4410         inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4411
4412         switch (event) {
4413         case RTM_NEWADDR:
4414                 /*
4415                  * If the address was optimistic
4416                  * we inserted the route at the start of
4417                  * our DAD process, so we don't need
4418                  * to do it again
4419                  */
4420                 if (!(ifp->rt->rt6i_node))
4421                         ip6_ins_rt(ifp->rt);
4422                 if (ifp->idev->cnf.forwarding)
4423                         addrconf_join_anycast(ifp);
4424                 break;
4425         case RTM_DELADDR:
4426                 if (ifp->idev->cnf.forwarding)
4427                         addrconf_leave_anycast(ifp);
4428                 addrconf_leave_solict(ifp->idev, &ifp->addr);
4429                 dst_hold(&ifp->rt->dst);
4430
4431                 if (ip6_del_rt(ifp->rt))
4432                         dst_free(&ifp->rt->dst);
4433                 break;
4434         }
4435 }
4436
4437 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4438 {
4439         rcu_read_lock_bh();
4440         if (likely(ifp->idev->dead == 0))
4441                 __ipv6_ifa_notify(event, ifp);
4442         rcu_read_unlock_bh();
4443 }
4444
4445 #ifdef CONFIG_SYSCTL
4446
4447 static
4448 int addrconf_sysctl_forward(ctl_table *ctl, int write,
4449                            void __user *buffer, size_t *lenp, loff_t *ppos)
4450 {
4451         int *valp = ctl->data;
4452         int val = *valp;
4453         loff_t pos = *ppos;
4454         ctl_table lctl;
4455         int ret;
4456
4457         /*
4458          * ctl->data points to idev->cnf.forwarding, we should
4459          * not modify it until we get the rtnl lock.
4460          */
4461         lctl = *ctl;
4462         lctl.data = &val;
4463
4464         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4465
4466         if (write)
4467                 ret = addrconf_fixup_forwarding(ctl, valp, val);
4468         if (ret)
4469                 *ppos = pos;
4470         return ret;
4471 }
4472
4473 static void dev_disable_change(struct inet6_dev *idev)
4474 {
4475         if (!idev || !idev->dev)
4476                 return;
4477
4478         if (idev->cnf.disable_ipv6)
4479                 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4480         else
4481                 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4482 }
4483
4484 static void addrconf_disable_change(struct net *net, __s32 newf)
4485 {
4486         struct net_device *dev;
4487         struct inet6_dev *idev;
4488
4489         rcu_read_lock();
4490         for_each_netdev_rcu(net, dev) {
4491                 idev = __in6_dev_get(dev);
4492                 if (idev) {
4493                         int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4494                         idev->cnf.disable_ipv6 = newf;
4495                         if (changed)
4496                                 dev_disable_change(idev);
4497                 }
4498         }
4499         rcu_read_unlock();
4500 }
4501
4502 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
4503 {
4504         struct net *net;
4505         int old;
4506
4507         if (!rtnl_trylock())
4508                 return restart_syscall();
4509
4510         net = (struct net *)table->extra2;
4511         old = *p;
4512         *p = newf;
4513
4514         if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4515                 rtnl_unlock();
4516                 return 0;
4517         }
4518
4519         if (p == &net->ipv6.devconf_all->disable_ipv6) {
4520                 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4521                 addrconf_disable_change(net, newf);
4522         } else if ((!newf) ^ (!old))
4523                 dev_disable_change((struct inet6_dev *)table->extra1);
4524
4525         rtnl_unlock();
4526         return 0;
4527 }
4528
4529 static
4530 int addrconf_sysctl_disable(ctl_table *ctl, int write,
4531                             void __user *buffer, size_t *lenp, loff_t *ppos)
4532 {
4533         int *valp = ctl->data;
4534         int val = *valp;
4535         loff_t pos = *ppos;
4536         ctl_table lctl;
4537         int ret;
4538
4539         /*
4540          * ctl->data points to idev->cnf.disable_ipv6, we should
4541          * not modify it until we get the rtnl lock.
4542          */
4543         lctl = *ctl;
4544         lctl.data = &val;
4545
4546         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4547
4548         if (write)
4549                 ret = addrconf_disable_ipv6(ctl, valp, val);
4550         if (ret)
4551                 *ppos = pos;
4552         return ret;
4553 }
4554
4555 static struct addrconf_sysctl_table
4556 {
4557         struct ctl_table_header *sysctl_header;
4558         ctl_table addrconf_vars[DEVCONF_MAX+1];
4559 } addrconf_sysctl __read_mostly = {
4560         .sysctl_header = NULL,
4561         .addrconf_vars = {
4562                 {
4563                         .procname       = "forwarding",
4564                         .data           = &ipv6_devconf.forwarding,
4565                         .maxlen         = sizeof(int),
4566                         .mode           = 0644,
4567                         .proc_handler   = addrconf_sysctl_forward,
4568                 },
4569                 {
4570                         .procname       = "hop_limit",
4571                         .data           = &ipv6_devconf.hop_limit,
4572                         .maxlen         = sizeof(int),
4573                         .mode           = 0644,
4574                         .proc_handler   = proc_dointvec,
4575                 },
4576                 {
4577                         .procname       = "mtu",
4578                         .data           = &ipv6_devconf.mtu6,
4579                         .maxlen         = sizeof(int),
4580                         .mode           = 0644,
4581                         .proc_handler   = proc_dointvec,
4582                 },
4583                 {
4584                         .procname       = "accept_ra",
4585                         .data           = &ipv6_devconf.accept_ra,
4586                         .maxlen         = sizeof(int),
4587                         .mode           = 0644,
4588                         .proc_handler   = proc_dointvec,
4589                 },
4590                 {
4591                         .procname       = "accept_redirects",
4592                         .data           = &ipv6_devconf.accept_redirects,
4593                         .maxlen         = sizeof(int),
4594                         .mode           = 0644,
4595                         .proc_handler   = proc_dointvec,
4596                 },
4597                 {
4598                         .procname       = "autoconf",
4599                         .data           = &ipv6_devconf.autoconf,
4600                         .maxlen         = sizeof(int),
4601                         .mode           = 0644,
4602                         .proc_handler   = proc_dointvec,
4603                 },
4604                 {
4605                         .procname       = "dad_transmits",
4606                         .data           = &ipv6_devconf.dad_transmits,
4607                         .maxlen         = sizeof(int),
4608                         .mode           = 0644,
4609                         .proc_handler   = proc_dointvec,
4610                 },
4611                 {
4612                         .procname       = "router_solicitations",
4613                         .data           = &ipv6_devconf.rtr_solicits,
4614                         .maxlen         = sizeof(int),
4615                         .mode           = 0644,
4616                         .proc_handler   = proc_dointvec,
4617                 },
4618                 {
4619                         .procname       = "router_solicitation_interval",
4620                         .data           = &ipv6_devconf.rtr_solicit_interval,
4621                         .maxlen         = sizeof(int),
4622                         .mode           = 0644,
4623                         .proc_handler   = proc_dointvec_jiffies,
4624                 },
4625                 {
4626                         .procname       = "router_solicitation_delay",
4627                         .data           = &ipv6_devconf.rtr_solicit_delay,
4628                         .maxlen         = sizeof(int),
4629                         .mode           = 0644,
4630                         .proc_handler   = proc_dointvec_jiffies,
4631                 },
4632                 {
4633                         .procname       = "force_mld_version",
4634                         .data           = &ipv6_devconf.force_mld_version,
4635                         .maxlen         = sizeof(int),
4636                         .mode           = 0644,
4637                         .proc_handler   = proc_dointvec,
4638                 },
4639 #ifdef CONFIG_IPV6_PRIVACY
4640                 {
4641                         .procname       = "use_tempaddr",
4642                         .data           = &ipv6_devconf.use_tempaddr,
4643                         .maxlen         = sizeof(int),
4644                         .mode           = 0644,
4645                         .proc_handler   = proc_dointvec,
4646                 },
4647                 {
4648                         .procname       = "temp_valid_lft",
4649                         .data           = &ipv6_devconf.temp_valid_lft,
4650                         .maxlen         = sizeof(int),
4651                         .mode           = 0644,
4652                         .proc_handler   = proc_dointvec,
4653                 },
4654                 {
4655                         .procname       = "temp_prefered_lft",
4656                         .data           = &ipv6_devconf.temp_prefered_lft,
4657                         .maxlen         = sizeof(int),
4658                         .mode           = 0644,
4659                         .proc_handler   = proc_dointvec,
4660                 },
4661                 {
4662                         .procname       = "regen_max_retry",
4663                         .data           = &ipv6_devconf.regen_max_retry,
4664                         .maxlen         = sizeof(int),
4665                         .mode           = 0644,
4666                         .proc_handler   = proc_dointvec,
4667                 },
4668                 {
4669                         .procname       = "max_desync_factor",
4670                         .data           = &ipv6_devconf.max_desync_factor,
4671                         .maxlen         = sizeof(int),
4672                         .mode           = 0644,
4673                         .proc_handler   = proc_dointvec,
4674                 },
4675 #endif
4676                 {
4677                         .procname       = "max_addresses",
4678                         .data           = &ipv6_devconf.max_addresses,
4679                         .maxlen         = sizeof(int),
4680                         .mode           = 0644,
4681                         .proc_handler   = proc_dointvec,
4682                 },
4683                 {
4684                         .procname       = "accept_ra_defrtr",
4685                         .data           = &ipv6_devconf.accept_ra_defrtr,
4686                         .maxlen         = sizeof(int),
4687                         .mode           = 0644,
4688                         .proc_handler   = proc_dointvec,
4689                 },
4690                 {
4691                         .procname       = "accept_ra_pinfo",
4692                         .data           = &ipv6_devconf.accept_ra_pinfo,
4693                         .maxlen         = sizeof(int),
4694                         .mode           = 0644,
4695                         .proc_handler   = proc_dointvec,
4696                 },
4697 #ifdef CONFIG_IPV6_ROUTER_PREF
4698                 {
4699                         .procname       = "accept_ra_rtr_pref",
4700                         .data           = &ipv6_devconf.accept_ra_rtr_pref,
4701                         .maxlen         = sizeof(int),
4702                         .mode           = 0644,
4703                         .proc_handler   = proc_dointvec,
4704                 },
4705                 {
4706                         .procname       = "router_probe_interval",
4707                         .data           = &ipv6_devconf.rtr_probe_interval,
4708                         .maxlen         = sizeof(int),
4709                         .mode           = 0644,
4710                         .proc_handler   = proc_dointvec_jiffies,
4711                 },
4712 #ifdef CONFIG_IPV6_ROUTE_INFO
4713                 {
4714                         .procname       = "accept_ra_rt_info_max_plen",
4715                         .data           = &ipv6_devconf.accept_ra_rt_info_max_plen,
4716                         .maxlen         = sizeof(int),
4717                         .mode           = 0644,
4718                         .proc_handler   = proc_dointvec,
4719                 },
4720 #endif
4721 #endif
4722                 {
4723                         .procname       = "proxy_ndp",
4724                         .data           = &ipv6_devconf.proxy_ndp,
4725                         .maxlen         = sizeof(int),
4726                         .mode           = 0644,
4727                         .proc_handler   = proc_dointvec,
4728                 },
4729                 {
4730                         .procname       = "accept_source_route",
4731                         .data           = &ipv6_devconf.accept_source_route,
4732                         .maxlen         = sizeof(int),
4733                         .mode           = 0644,
4734                         .proc_handler   = proc_dointvec,
4735                 },
4736 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4737                 {
4738                         .procname       = "optimistic_dad",
4739                         .data           = &ipv6_devconf.optimistic_dad,
4740                         .maxlen         = sizeof(int),
4741                         .mode           = 0644,
4742                         .proc_handler   = proc_dointvec,
4743
4744                 },
4745 #endif
4746 #ifdef CONFIG_IPV6_MROUTE
4747                 {
4748                         .procname       = "mc_forwarding",
4749                         .data           = &ipv6_devconf.mc_forwarding,
4750                         .maxlen         = sizeof(int),
4751                         .mode           = 0444,
4752                         .proc_handler   = proc_dointvec,
4753                 },
4754 #endif
4755                 {
4756                         .procname       = "disable_ipv6",
4757                         .data           = &ipv6_devconf.disable_ipv6,
4758                         .maxlen         = sizeof(int),
4759                         .mode           = 0644,
4760                         .proc_handler   = addrconf_sysctl_disable,
4761                 },
4762                 {
4763                         .procname       = "accept_dad",
4764                         .data           = &ipv6_devconf.accept_dad,
4765                         .maxlen         = sizeof(int),
4766                         .mode           = 0644,
4767                         .proc_handler   = proc_dointvec,
4768                 },
4769                 {
4770                         .procname       = "force_tllao",
4771                         .data           = &ipv6_devconf.force_tllao,
4772                         .maxlen         = sizeof(int),
4773                         .mode           = 0644,
4774                         .proc_handler   = proc_dointvec
4775                 },
4776                 {
4777                         .procname       = "ndisc_notify",
4778                         .data           = &ipv6_devconf.ndisc_notify,
4779                         .maxlen         = sizeof(int),
4780                         .mode           = 0644,
4781                         .proc_handler   = proc_dointvec
4782                 },
4783                 {
4784                         /* sentinel */
4785                 }
4786         },
4787 };
4788
4789 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4790                 struct inet6_dev *idev, struct ipv6_devconf *p)
4791 {
4792         int i;
4793         struct addrconf_sysctl_table *t;
4794         char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
4795
4796         t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
4797         if (t == NULL)
4798                 goto out;
4799
4800         for (i = 0; t->addrconf_vars[i].data; i++) {
4801                 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
4802                 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4803                 t->addrconf_vars[i].extra2 = net;
4804         }
4805
4806         snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
4807
4808         t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
4809         if (t->sysctl_header == NULL)
4810                 goto free;
4811
4812         p->sysctl = t;
4813         return 0;
4814
4815 free:
4816         kfree(t);
4817 out:
4818         return -ENOBUFS;
4819 }
4820
4821 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4822 {
4823         struct addrconf_sysctl_table *t;
4824
4825         if (p->sysctl == NULL)
4826                 return;
4827
4828         t = p->sysctl;
4829         p->sysctl = NULL;
4830         unregister_net_sysctl_table(t->sysctl_header);
4831         kfree(t);
4832 }
4833
4834 static void addrconf_sysctl_register(struct inet6_dev *idev)
4835 {
4836         neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
4837                               &ndisc_ifinfo_sysctl_change);
4838         __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4839                                         idev, &idev->cnf);
4840 }
4841
4842 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4843 {
4844         __addrconf_sysctl_unregister(&idev->cnf);
4845         neigh_sysctl_unregister(idev->nd_parms);
4846 }
4847
4848
4849 #endif
4850
4851 static int __net_init addrconf_init_net(struct net *net)
4852 {
4853         int err;
4854         struct ipv6_devconf *all, *dflt;
4855
4856         err = -ENOMEM;
4857         all = &ipv6_devconf;
4858         dflt = &ipv6_devconf_dflt;
4859
4860         if (!net_eq(net, &init_net)) {
4861                 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4862                 if (all == NULL)
4863                         goto err_alloc_all;
4864
4865                 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4866                 if (dflt == NULL)
4867                         goto err_alloc_dflt;
4868         } else {
4869                 /* these will be inherited by all namespaces */
4870                 dflt->autoconf = ipv6_defaults.autoconf;
4871                 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
4872         }
4873
4874         net->ipv6.devconf_all = all;
4875         net->ipv6.devconf_dflt = dflt;
4876
4877 #ifdef CONFIG_SYSCTL
4878         err = __addrconf_sysctl_register(net, "all", NULL, all);
4879         if (err < 0)
4880                 goto err_reg_all;
4881
4882         err = __addrconf_sysctl_register(net, "default", NULL, dflt);
4883         if (err < 0)
4884                 goto err_reg_dflt;
4885 #endif
4886         return 0;
4887
4888 #ifdef CONFIG_SYSCTL
4889 err_reg_dflt:
4890         __addrconf_sysctl_unregister(all);
4891 err_reg_all:
4892         kfree(dflt);
4893 #endif
4894 err_alloc_dflt:
4895         kfree(all);
4896 err_alloc_all:
4897         return err;
4898 }
4899
4900 static void __net_exit addrconf_exit_net(struct net *net)
4901 {
4902 #ifdef CONFIG_SYSCTL
4903         __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4904         __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4905 #endif
4906         if (!net_eq(net, &init_net)) {
4907                 kfree(net->ipv6.devconf_dflt);
4908                 kfree(net->ipv6.devconf_all);
4909         }
4910 }
4911
4912 static struct pernet_operations addrconf_ops = {
4913         .init = addrconf_init_net,
4914         .exit = addrconf_exit_net,
4915 };
4916
4917 /*
4918  *      Device notifier
4919  */
4920
4921 int register_inet6addr_notifier(struct notifier_block *nb)
4922 {
4923         return atomic_notifier_chain_register(&inet6addr_chain, nb);
4924 }
4925 EXPORT_SYMBOL(register_inet6addr_notifier);
4926
4927 int unregister_inet6addr_notifier(struct notifier_block *nb)
4928 {
4929         return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
4930 }
4931 EXPORT_SYMBOL(unregister_inet6addr_notifier);
4932
4933 static struct rtnl_af_ops inet6_ops = {
4934         .family           = AF_INET6,
4935         .fill_link_af     = inet6_fill_link_af,
4936         .get_link_af_size = inet6_get_link_af_size,
4937 };
4938
4939 /*
4940  *      Init / cleanup code
4941  */
4942
4943 int __init addrconf_init(void)
4944 {
4945         int i, err;
4946
4947         err = ipv6_addr_label_init();
4948         if (err < 0) {
4949                 pr_crit("%s: cannot initialize default policy table: %d\n",
4950                         __func__, err);
4951                 goto out;
4952         }
4953
4954         err = register_pernet_subsys(&addrconf_ops);
4955         if (err < 0)
4956                 goto out_addrlabel;
4957
4958         /* The addrconf netdev notifier requires that loopback_dev
4959          * has it's ipv6 private information allocated and setup
4960          * before it can bring up and give link-local addresses
4961          * to other devices which are up.
4962          *
4963          * Unfortunately, loopback_dev is not necessarily the first
4964          * entry in the global dev_base list of net devices.  In fact,
4965          * it is likely to be the very last entry on that list.
4966          * So this causes the notifier registry below to try and
4967          * give link-local addresses to all devices besides loopback_dev
4968          * first, then loopback_dev, which cases all the non-loopback_dev
4969          * devices to fail to get a link-local address.
4970          *
4971          * So, as a temporary fix, allocate the ipv6 structure for
4972          * loopback_dev first by hand.
4973          * Longer term, all of the dependencies ipv6 has upon the loopback
4974          * device and it being up should be removed.
4975          */
4976         rtnl_lock();
4977         if (!ipv6_add_dev(init_net.loopback_dev))
4978                 err = -ENOMEM;
4979         rtnl_unlock();
4980         if (err)
4981                 goto errlo;
4982
4983         for (i = 0; i < IN6_ADDR_HSIZE; i++)
4984                 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4985
4986         register_netdevice_notifier(&ipv6_dev_notf);
4987
4988         addrconf_verify(0);
4989
4990         err = rtnl_af_register(&inet6_ops);
4991         if (err < 0)
4992                 goto errout_af;
4993
4994         err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4995                               NULL);
4996         if (err < 0)
4997                 goto errout;
4998
4999         /* Only the first call to __rtnl_register can fail */
5000         __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5001         __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5002         __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5003                         inet6_dump_ifaddr, NULL);
5004         __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5005                         inet6_dump_ifmcaddr, NULL);
5006         __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5007                         inet6_dump_ifacaddr, NULL);
5008         __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5009                         inet6_netconf_dump_devconf, NULL);
5010
5011         ipv6_addr_label_rtnl_register();
5012
5013         return 0;
5014 errout:
5015         rtnl_af_unregister(&inet6_ops);
5016 errout_af:
5017         unregister_netdevice_notifier(&ipv6_dev_notf);
5018 errlo:
5019         unregister_pernet_subsys(&addrconf_ops);
5020 out_addrlabel:
5021         ipv6_addr_label_cleanup();
5022 out:
5023         return err;
5024 }
5025
5026 void addrconf_cleanup(void)
5027 {
5028         struct net_device *dev;
5029         int i;
5030
5031         unregister_netdevice_notifier(&ipv6_dev_notf);
5032         unregister_pernet_subsys(&addrconf_ops);
5033         ipv6_addr_label_cleanup();
5034
5035         rtnl_lock();
5036
5037         __rtnl_af_unregister(&inet6_ops);
5038
5039         /* clean dev list */
5040         for_each_netdev(&init_net, dev) {
5041                 if (__in6_dev_get(dev) == NULL)
5042                         continue;
5043                 addrconf_ifdown(dev, 1);
5044         }
5045         addrconf_ifdown(init_net.loopback_dev, 2);
5046
5047         /*
5048          *      Check hash table.
5049          */
5050         spin_lock_bh(&addrconf_hash_lock);
5051         for (i = 0; i < IN6_ADDR_HSIZE; i++)
5052                 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5053         spin_unlock_bh(&addrconf_hash_lock);
5054
5055         del_timer(&addr_chk_timer);
5056         rtnl_unlock();
5057 }