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