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