GNU Linux-libre 4.9.315-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         read_unlock_bh(&ifp->idev->lock);
3998
3999         /* While dad is in progress mld report's source address is in6_addrany.
4000          * Resend with proper ll now.
4001          */
4002         if (send_mld)
4003                 ipv6_mc_dad_complete(ifp->idev);
4004
4005         if (send_rs) {
4006                 /*
4007                  *      If a host as already performed a random delay
4008                  *      [...] as part of DAD [...] there is no need
4009                  *      to delay again before sending the first RS
4010                  */
4011                 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4012                         return;
4013                 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4014
4015                 write_lock_bh(&ifp->idev->lock);
4016                 spin_lock(&ifp->lock);
4017                 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4018                         ifp->idev->cnf.rtr_solicit_interval);
4019                 ifp->idev->rs_probes = 1;
4020                 ifp->idev->if_flags |= IF_RS_SENT;
4021                 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4022                 spin_unlock(&ifp->lock);
4023                 write_unlock_bh(&ifp->idev->lock);
4024         }
4025
4026         if (bump_id)
4027                 rt_genid_bump_ipv6(dev_net(dev));
4028
4029         /* Make sure that a new temporary address will be created
4030          * before this temporary address becomes deprecated.
4031          */
4032         if (ifp->flags & IFA_F_TEMPORARY)
4033                 addrconf_verify_rtnl();
4034 }
4035
4036 static void addrconf_dad_run(struct inet6_dev *idev)
4037 {
4038         struct inet6_ifaddr *ifp;
4039
4040         read_lock_bh(&idev->lock);
4041         list_for_each_entry(ifp, &idev->addr_list, if_list) {
4042                 spin_lock(&ifp->lock);
4043                 if (ifp->flags & IFA_F_TENTATIVE &&
4044                     ifp->state == INET6_IFADDR_STATE_DAD)
4045                         addrconf_dad_kick(ifp);
4046                 spin_unlock(&ifp->lock);
4047         }
4048         read_unlock_bh(&idev->lock);
4049 }
4050
4051 #ifdef CONFIG_PROC_FS
4052 struct if6_iter_state {
4053         struct seq_net_private p;
4054         int bucket;
4055         int offset;
4056 };
4057
4058 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4059 {
4060         struct inet6_ifaddr *ifa = NULL;
4061         struct if6_iter_state *state = seq->private;
4062         struct net *net = seq_file_net(seq);
4063         int p = 0;
4064
4065         /* initial bucket if pos is 0 */
4066         if (pos == 0) {
4067                 state->bucket = 0;
4068                 state->offset = 0;
4069         }
4070
4071         for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4072                 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
4073                                          addr_lst) {
4074                         if (!net_eq(dev_net(ifa->idev->dev), net))
4075                                 continue;
4076                         /* sync with offset */
4077                         if (p < state->offset) {
4078                                 p++;
4079                                 continue;
4080                         }
4081                         return ifa;
4082                 }
4083
4084                 /* prepare for next bucket */
4085                 state->offset = 0;
4086                 p = 0;
4087         }
4088         return NULL;
4089 }
4090
4091 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4092                                          struct inet6_ifaddr *ifa)
4093 {
4094         struct if6_iter_state *state = seq->private;
4095         struct net *net = seq_file_net(seq);
4096
4097         hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
4098                 if (!net_eq(dev_net(ifa->idev->dev), net))
4099                         continue;
4100                 state->offset++;
4101                 return ifa;
4102         }
4103
4104         state->offset = 0;
4105         while (++state->bucket < IN6_ADDR_HSIZE) {
4106                 hlist_for_each_entry_rcu_bh(ifa,
4107                                      &inet6_addr_lst[state->bucket], addr_lst) {
4108                         if (!net_eq(dev_net(ifa->idev->dev), net))
4109                                 continue;
4110                         return ifa;
4111                 }
4112         }
4113
4114         return NULL;
4115 }
4116
4117 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4118         __acquires(rcu_bh)
4119 {
4120         rcu_read_lock_bh();
4121         return if6_get_first(seq, *pos);
4122 }
4123
4124 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4125 {
4126         struct inet6_ifaddr *ifa;
4127
4128         ifa = if6_get_next(seq, v);
4129         ++*pos;
4130         return ifa;
4131 }
4132
4133 static void if6_seq_stop(struct seq_file *seq, void *v)
4134         __releases(rcu_bh)
4135 {
4136         rcu_read_unlock_bh();
4137 }
4138
4139 static int if6_seq_show(struct seq_file *seq, void *v)
4140 {
4141         struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4142         seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4143                    &ifp->addr,
4144                    ifp->idev->dev->ifindex,
4145                    ifp->prefix_len,
4146                    ifp->scope,
4147                    (u8) ifp->flags,
4148                    ifp->idev->dev->name);
4149         return 0;
4150 }
4151
4152 static const struct seq_operations if6_seq_ops = {
4153         .start  = if6_seq_start,
4154         .next   = if6_seq_next,
4155         .show   = if6_seq_show,
4156         .stop   = if6_seq_stop,
4157 };
4158
4159 static int if6_seq_open(struct inode *inode, struct file *file)
4160 {
4161         return seq_open_net(inode, file, &if6_seq_ops,
4162                             sizeof(struct if6_iter_state));
4163 }
4164
4165 static const struct file_operations if6_fops = {
4166         .owner          = THIS_MODULE,
4167         .open           = if6_seq_open,
4168         .read           = seq_read,
4169         .llseek         = seq_lseek,
4170         .release        = seq_release_net,
4171 };
4172
4173 static int __net_init if6_proc_net_init(struct net *net)
4174 {
4175         if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
4176                 return -ENOMEM;
4177         return 0;
4178 }
4179
4180 static void __net_exit if6_proc_net_exit(struct net *net)
4181 {
4182         remove_proc_entry("if_inet6", net->proc_net);
4183 }
4184
4185 static struct pernet_operations if6_proc_net_ops = {
4186         .init = if6_proc_net_init,
4187         .exit = if6_proc_net_exit,
4188 };
4189
4190 int __init if6_proc_init(void)
4191 {
4192         return register_pernet_subsys(&if6_proc_net_ops);
4193 }
4194
4195 void if6_proc_exit(void)
4196 {
4197         unregister_pernet_subsys(&if6_proc_net_ops);
4198 }
4199 #endif  /* CONFIG_PROC_FS */
4200
4201 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4202 /* Check if address is a home address configured on any interface. */
4203 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4204 {
4205         int ret = 0;
4206         struct inet6_ifaddr *ifp = NULL;
4207         unsigned int hash = inet6_addr_hash(addr);
4208
4209         rcu_read_lock_bh();
4210         hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4211                 if (!net_eq(dev_net(ifp->idev->dev), net))
4212                         continue;
4213                 if (ipv6_addr_equal(&ifp->addr, addr) &&
4214                     (ifp->flags & IFA_F_HOMEADDRESS)) {
4215                         ret = 1;
4216                         break;
4217                 }
4218         }
4219         rcu_read_unlock_bh();
4220         return ret;
4221 }
4222 #endif
4223
4224 /*
4225  *      Periodic address status verification
4226  */
4227
4228 static void addrconf_verify_rtnl(void)
4229 {
4230         unsigned long now, next, next_sec, next_sched;
4231         struct inet6_ifaddr *ifp;
4232         int i;
4233
4234         ASSERT_RTNL();
4235
4236         rcu_read_lock_bh();
4237         now = jiffies;
4238         next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4239
4240         cancel_delayed_work(&addr_chk_work);
4241
4242         for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4243 restart:
4244                 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4245                         unsigned long age;
4246
4247                         /* When setting preferred_lft to a value not zero or
4248                          * infinity, while valid_lft is infinity
4249                          * IFA_F_PERMANENT has a non-infinity life time.
4250                          */
4251                         if ((ifp->flags & IFA_F_PERMANENT) &&
4252                             (ifp->prefered_lft == INFINITY_LIFE_TIME))
4253                                 continue;
4254
4255                         spin_lock(&ifp->lock);
4256                         /* We try to batch several events at once. */
4257                         age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4258
4259                         if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4260                             age >= ifp->valid_lft) {
4261                                 spin_unlock(&ifp->lock);
4262                                 in6_ifa_hold(ifp);
4263                                 ipv6_del_addr(ifp);
4264                                 goto restart;
4265                         } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4266                                 spin_unlock(&ifp->lock);
4267                                 continue;
4268                         } else if (age >= ifp->prefered_lft) {
4269                                 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4270                                 int deprecate = 0;
4271
4272                                 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4273                                         deprecate = 1;
4274                                         ifp->flags |= IFA_F_DEPRECATED;
4275                                 }
4276
4277                                 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4278                                     (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4279                                         next = ifp->tstamp + ifp->valid_lft * HZ;
4280
4281                                 spin_unlock(&ifp->lock);
4282
4283                                 if (deprecate) {
4284                                         in6_ifa_hold(ifp);
4285
4286                                         ipv6_ifa_notify(0, ifp);
4287                                         in6_ifa_put(ifp);
4288                                         goto restart;
4289                                 }
4290                         } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4291                                    !(ifp->flags&IFA_F_TENTATIVE)) {
4292                                 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4293                                         ifp->idev->cnf.dad_transmits *
4294                                         NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4295
4296                                 if (age >= ifp->prefered_lft - regen_advance) {
4297                                         struct inet6_ifaddr *ifpub = ifp->ifpub;
4298                                         if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4299                                                 next = ifp->tstamp + ifp->prefered_lft * HZ;
4300                                         if (!ifp->regen_count && ifpub) {
4301                                                 ifp->regen_count++;
4302                                                 in6_ifa_hold(ifp);
4303                                                 in6_ifa_hold(ifpub);
4304                                                 spin_unlock(&ifp->lock);
4305
4306                                                 spin_lock(&ifpub->lock);
4307                                                 ifpub->regen_count = 0;
4308                                                 spin_unlock(&ifpub->lock);
4309                                                 ipv6_create_tempaddr(ifpub, ifp);
4310                                                 in6_ifa_put(ifpub);
4311                                                 in6_ifa_put(ifp);
4312                                                 goto restart;
4313                                         }
4314                                 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4315                                         next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4316                                 spin_unlock(&ifp->lock);
4317                         } else {
4318                                 /* ifp->prefered_lft <= ifp->valid_lft */
4319                                 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4320                                         next = ifp->tstamp + ifp->prefered_lft * HZ;
4321                                 spin_unlock(&ifp->lock);
4322                         }
4323                 }
4324         }
4325
4326         next_sec = round_jiffies_up(next);
4327         next_sched = next;
4328
4329         /* If rounded timeout is accurate enough, accept it. */
4330         if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4331                 next_sched = next_sec;
4332
4333         /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4334         if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4335                 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4336
4337         ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4338               now, next, next_sec, next_sched);
4339         mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4340         rcu_read_unlock_bh();
4341 }
4342
4343 static void addrconf_verify_work(struct work_struct *w)
4344 {
4345         rtnl_lock();
4346         addrconf_verify_rtnl();
4347         rtnl_unlock();
4348 }
4349
4350 static void addrconf_verify(void)
4351 {
4352         mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4353 }
4354
4355 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4356                                      struct in6_addr **peer_pfx)
4357 {
4358         struct in6_addr *pfx = NULL;
4359
4360         *peer_pfx = NULL;
4361
4362         if (addr)
4363                 pfx = nla_data(addr);
4364
4365         if (local) {
4366                 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4367                         *peer_pfx = pfx;
4368                 pfx = nla_data(local);
4369         }
4370
4371         return pfx;
4372 }
4373
4374 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4375         [IFA_ADDRESS]           = { .len = sizeof(struct in6_addr) },
4376         [IFA_LOCAL]             = { .len = sizeof(struct in6_addr) },
4377         [IFA_CACHEINFO]         = { .len = sizeof(struct ifa_cacheinfo) },
4378         [IFA_FLAGS]             = { .len = sizeof(u32) },
4379 };
4380
4381 static int
4382 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4383 {
4384         struct net *net = sock_net(skb->sk);
4385         struct ifaddrmsg *ifm;
4386         struct nlattr *tb[IFA_MAX+1];
4387         struct in6_addr *pfx, *peer_pfx;
4388         u32 ifa_flags;
4389         int err;
4390
4391         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4392         if (err < 0)
4393                 return err;
4394
4395         ifm = nlmsg_data(nlh);
4396         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4397         if (!pfx)
4398                 return -EINVAL;
4399
4400         ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4401
4402         /* We ignore other flags so far. */
4403         ifa_flags &= IFA_F_MANAGETEMPADDR;
4404
4405         return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4406                               ifm->ifa_prefixlen);
4407 }
4408
4409 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4410                              u32 prefered_lft, u32 valid_lft)
4411 {
4412         u32 flags;
4413         clock_t expires;
4414         unsigned long timeout;
4415         bool was_managetempaddr;
4416         bool had_prefixroute;
4417
4418         ASSERT_RTNL();
4419
4420         if (!valid_lft || (prefered_lft > valid_lft))
4421                 return -EINVAL;
4422
4423         if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4424             (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4425                 return -EINVAL;
4426
4427         timeout = addrconf_timeout_fixup(valid_lft, HZ);
4428         if (addrconf_finite_timeout(timeout)) {
4429                 expires = jiffies_to_clock_t(timeout * HZ);
4430                 valid_lft = timeout;
4431                 flags = RTF_EXPIRES;
4432         } else {
4433                 expires = 0;
4434                 flags = 0;
4435                 ifa_flags |= IFA_F_PERMANENT;
4436         }
4437
4438         timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4439         if (addrconf_finite_timeout(timeout)) {
4440                 if (timeout == 0)
4441                         ifa_flags |= IFA_F_DEPRECATED;
4442                 prefered_lft = timeout;
4443         }
4444
4445         spin_lock_bh(&ifp->lock);
4446         was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4447         had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4448                           !(ifp->flags & IFA_F_NOPREFIXROUTE);
4449         ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4450                         IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4451                         IFA_F_NOPREFIXROUTE);
4452         ifp->flags |= ifa_flags;
4453         ifp->tstamp = jiffies;
4454         ifp->valid_lft = valid_lft;
4455         ifp->prefered_lft = prefered_lft;
4456
4457         spin_unlock_bh(&ifp->lock);
4458         if (!(ifp->flags&IFA_F_TENTATIVE))
4459                 ipv6_ifa_notify(0, ifp);
4460
4461         if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4462                 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4463                                       expires, flags);
4464         } else if (had_prefixroute) {
4465                 enum cleanup_prefix_rt_t action;
4466                 unsigned long rt_expires;
4467
4468                 write_lock_bh(&ifp->idev->lock);
4469                 action = check_cleanup_prefix_route(ifp, &rt_expires);
4470                 write_unlock_bh(&ifp->idev->lock);
4471
4472                 if (action != CLEANUP_PREFIX_RT_NOP) {
4473                         cleanup_prefix_route(ifp, rt_expires,
4474                                 action == CLEANUP_PREFIX_RT_DEL);
4475                 }
4476         }
4477
4478         if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4479                 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4480                         valid_lft = prefered_lft = 0;
4481                 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4482                                  !was_managetempaddr, jiffies);
4483         }
4484
4485         addrconf_verify_rtnl();
4486
4487         return 0;
4488 }
4489
4490 static int
4491 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4492 {
4493         struct net *net = sock_net(skb->sk);
4494         struct ifaddrmsg *ifm;
4495         struct nlattr *tb[IFA_MAX+1];
4496         struct in6_addr *pfx, *peer_pfx;
4497         struct inet6_ifaddr *ifa;
4498         struct net_device *dev;
4499         u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4500         u32 ifa_flags;
4501         int err;
4502
4503         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4504         if (err < 0)
4505                 return err;
4506
4507         ifm = nlmsg_data(nlh);
4508         pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4509         if (!pfx)
4510                 return -EINVAL;
4511
4512         if (tb[IFA_CACHEINFO]) {
4513                 struct ifa_cacheinfo *ci;
4514
4515                 ci = nla_data(tb[IFA_CACHEINFO]);
4516                 valid_lft = ci->ifa_valid;
4517                 preferred_lft = ci->ifa_prefered;
4518         } else {
4519                 preferred_lft = INFINITY_LIFE_TIME;
4520                 valid_lft = INFINITY_LIFE_TIME;
4521         }
4522
4523         dev =  __dev_get_by_index(net, ifm->ifa_index);
4524         if (!dev)
4525                 return -ENODEV;
4526
4527         ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4528
4529         /* We ignore other flags so far. */
4530         ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4531                      IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4532
4533         ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4534         if (!ifa) {
4535                 /*
4536                  * It would be best to check for !NLM_F_CREATE here but
4537                  * userspace already relies on not having to provide this.
4538                  */
4539                 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4540                                       ifm->ifa_prefixlen, ifa_flags,
4541                                       preferred_lft, valid_lft);
4542         }
4543
4544         if (nlh->nlmsg_flags & NLM_F_EXCL ||
4545             !(nlh->nlmsg_flags & NLM_F_REPLACE))
4546                 err = -EEXIST;
4547         else
4548                 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4549
4550         in6_ifa_put(ifa);
4551
4552         return err;
4553 }
4554
4555 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4556                           u8 scope, int ifindex)
4557 {
4558         struct ifaddrmsg *ifm;
4559
4560         ifm = nlmsg_data(nlh);
4561         ifm->ifa_family = AF_INET6;
4562         ifm->ifa_prefixlen = prefixlen;
4563         ifm->ifa_flags = flags;
4564         ifm->ifa_scope = scope;
4565         ifm->ifa_index = ifindex;
4566 }
4567
4568 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4569                          unsigned long tstamp, u32 preferred, u32 valid)
4570 {
4571         struct ifa_cacheinfo ci;
4572
4573         ci.cstamp = cstamp_delta(cstamp);
4574         ci.tstamp = cstamp_delta(tstamp);
4575         ci.ifa_prefered = preferred;
4576         ci.ifa_valid = valid;
4577
4578         return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4579 }
4580
4581 static inline int rt_scope(int ifa_scope)
4582 {
4583         if (ifa_scope & IFA_HOST)
4584                 return RT_SCOPE_HOST;
4585         else if (ifa_scope & IFA_LINK)
4586                 return RT_SCOPE_LINK;
4587         else if (ifa_scope & IFA_SITE)
4588                 return RT_SCOPE_SITE;
4589         else
4590                 return RT_SCOPE_UNIVERSE;
4591 }
4592
4593 static inline int inet6_ifaddr_msgsize(void)
4594 {
4595         return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4596                + nla_total_size(16) /* IFA_LOCAL */
4597                + nla_total_size(16) /* IFA_ADDRESS */
4598                + nla_total_size(sizeof(struct ifa_cacheinfo))
4599                + nla_total_size(4)  /* IFA_FLAGS */;
4600 }
4601
4602 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4603                              u32 portid, u32 seq, int event, unsigned int flags)
4604 {
4605         struct nlmsghdr  *nlh;
4606         u32 preferred, valid;
4607
4608         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4609         if (!nlh)
4610                 return -EMSGSIZE;
4611
4612         put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4613                       ifa->idev->dev->ifindex);
4614
4615         if (!((ifa->flags&IFA_F_PERMANENT) &&
4616               (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4617                 preferred = ifa->prefered_lft;
4618                 valid = ifa->valid_lft;
4619                 if (preferred != INFINITY_LIFE_TIME) {
4620                         long tval = (jiffies - ifa->tstamp)/HZ;
4621                         if (preferred > tval)
4622                                 preferred -= tval;
4623                         else
4624                                 preferred = 0;
4625                         if (valid != INFINITY_LIFE_TIME) {
4626                                 if (valid > tval)
4627                                         valid -= tval;
4628                                 else
4629                                         valid = 0;
4630                         }
4631                 }
4632         } else {
4633                 preferred = INFINITY_LIFE_TIME;
4634                 valid = INFINITY_LIFE_TIME;
4635         }
4636
4637         if (!ipv6_addr_any(&ifa->peer_addr)) {
4638                 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4639                     nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4640                         goto error;
4641         } else
4642                 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4643                         goto error;
4644
4645         if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4646                 goto error;
4647
4648         if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4649                 goto error;
4650
4651         nlmsg_end(skb, nlh);
4652         return 0;
4653
4654 error:
4655         nlmsg_cancel(skb, nlh);
4656         return -EMSGSIZE;
4657 }
4658
4659 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4660                                 u32 portid, u32 seq, int event, u16 flags)
4661 {
4662         struct nlmsghdr  *nlh;
4663         u8 scope = RT_SCOPE_UNIVERSE;
4664         int ifindex = ifmca->idev->dev->ifindex;
4665
4666         if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4667                 scope = RT_SCOPE_SITE;
4668
4669         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4670         if (!nlh)
4671                 return -EMSGSIZE;
4672
4673         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4674         if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4675             put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4676                           INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4677                 nlmsg_cancel(skb, nlh);
4678                 return -EMSGSIZE;
4679         }
4680
4681         nlmsg_end(skb, nlh);
4682         return 0;
4683 }
4684
4685 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4686                                 u32 portid, u32 seq, int event, unsigned int flags)
4687 {
4688         struct nlmsghdr  *nlh;
4689         u8 scope = RT_SCOPE_UNIVERSE;
4690         int ifindex = ifaca->aca_idev->dev->ifindex;
4691
4692         if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4693                 scope = RT_SCOPE_SITE;
4694
4695         nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4696         if (!nlh)
4697                 return -EMSGSIZE;
4698
4699         put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4700         if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4701             put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4702                           INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4703                 nlmsg_cancel(skb, nlh);
4704                 return -EMSGSIZE;
4705         }
4706
4707         nlmsg_end(skb, nlh);
4708         return 0;
4709 }
4710
4711 enum addr_type_t {
4712         UNICAST_ADDR,
4713         MULTICAST_ADDR,
4714         ANYCAST_ADDR,
4715 };
4716
4717 /* called with rcu_read_lock() */
4718 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4719                           struct netlink_callback *cb, enum addr_type_t type,
4720                           int s_ip_idx, int *p_ip_idx)
4721 {
4722         struct ifmcaddr6 *ifmca;
4723         struct ifacaddr6 *ifaca;
4724         int err = 1;
4725         int ip_idx = *p_ip_idx;
4726
4727         read_lock_bh(&idev->lock);
4728         switch (type) {
4729         case UNICAST_ADDR: {
4730                 struct inet6_ifaddr *ifa;
4731
4732                 /* unicast address incl. temp addr */
4733                 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4734                         if (ip_idx < s_ip_idx)
4735                                 goto next;
4736                         err = inet6_fill_ifaddr(skb, ifa,
4737                                                 NETLINK_CB(cb->skb).portid,
4738                                                 cb->nlh->nlmsg_seq,
4739                                                 RTM_NEWADDR,
4740                                                 NLM_F_MULTI);
4741                         if (err < 0)
4742                                 break;
4743                         nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4744 next:
4745                         ip_idx++;
4746                 }
4747                 break;
4748         }
4749         case MULTICAST_ADDR:
4750                 /* multicast address */
4751                 for (ifmca = idev->mc_list; ifmca;
4752                      ifmca = ifmca->next, ip_idx++) {
4753                         if (ip_idx < s_ip_idx)
4754                                 continue;
4755                         err = inet6_fill_ifmcaddr(skb, ifmca,
4756                                                   NETLINK_CB(cb->skb).portid,
4757                                                   cb->nlh->nlmsg_seq,
4758                                                   RTM_GETMULTICAST,
4759                                                   NLM_F_MULTI);
4760                         if (err < 0)
4761                                 break;
4762                 }
4763                 break;
4764         case ANYCAST_ADDR:
4765                 /* anycast address */
4766                 for (ifaca = idev->ac_list; ifaca;
4767                      ifaca = ifaca->aca_next, ip_idx++) {
4768                         if (ip_idx < s_ip_idx)
4769                                 continue;
4770                         err = inet6_fill_ifacaddr(skb, ifaca,
4771                                                   NETLINK_CB(cb->skb).portid,
4772                                                   cb->nlh->nlmsg_seq,
4773                                                   RTM_GETANYCAST,
4774                                                   NLM_F_MULTI);
4775                         if (err < 0)
4776                                 break;
4777                 }
4778                 break;
4779         default:
4780                 break;
4781         }
4782         read_unlock_bh(&idev->lock);
4783         *p_ip_idx = ip_idx;
4784         return err;
4785 }
4786
4787 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4788                            enum addr_type_t type)
4789 {
4790         struct net *net = sock_net(skb->sk);
4791         int h, s_h;
4792         int idx, ip_idx;
4793         int s_idx, s_ip_idx;
4794         struct net_device *dev;
4795         struct inet6_dev *idev;
4796         struct hlist_head *head;
4797
4798         s_h = cb->args[0];
4799         s_idx = idx = cb->args[1];
4800         s_ip_idx = ip_idx = cb->args[2];
4801
4802         rcu_read_lock();
4803         cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4804         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4805                 idx = 0;
4806                 head = &net->dev_index_head[h];
4807                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4808                         if (idx < s_idx)
4809                                 goto cont;
4810                         if (h > s_h || idx > s_idx)
4811                                 s_ip_idx = 0;
4812                         ip_idx = 0;
4813                         idev = __in6_dev_get(dev);
4814                         if (!idev)
4815                                 goto cont;
4816
4817                         if (in6_dump_addrs(idev, skb, cb, type,
4818                                            s_ip_idx, &ip_idx) < 0)
4819                                 goto done;
4820 cont:
4821                         idx++;
4822                 }
4823         }
4824 done:
4825         rcu_read_unlock();
4826         cb->args[0] = h;
4827         cb->args[1] = idx;
4828         cb->args[2] = ip_idx;
4829
4830         return skb->len;
4831 }
4832
4833 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4834 {
4835         enum addr_type_t type = UNICAST_ADDR;
4836
4837         return inet6_dump_addr(skb, cb, type);
4838 }
4839
4840 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4841 {
4842         enum addr_type_t type = MULTICAST_ADDR;
4843
4844         return inet6_dump_addr(skb, cb, type);
4845 }
4846
4847
4848 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4849 {
4850         enum addr_type_t type = ANYCAST_ADDR;
4851
4852         return inet6_dump_addr(skb, cb, type);
4853 }
4854
4855 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4856 {
4857         struct net *net = sock_net(in_skb->sk);
4858         struct ifaddrmsg *ifm;
4859         struct nlattr *tb[IFA_MAX+1];
4860         struct in6_addr *addr = NULL, *peer;
4861         struct net_device *dev = NULL;
4862         struct inet6_ifaddr *ifa;
4863         struct sk_buff *skb;
4864         int err;
4865
4866         err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4867         if (err < 0)
4868                 goto errout;
4869
4870         addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4871         if (!addr) {
4872                 err = -EINVAL;
4873                 goto errout;
4874         }
4875
4876         ifm = nlmsg_data(nlh);
4877         if (ifm->ifa_index)
4878                 dev = __dev_get_by_index(net, ifm->ifa_index);
4879
4880         ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4881         if (!ifa) {
4882                 err = -EADDRNOTAVAIL;
4883                 goto errout;
4884         }
4885
4886         skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4887         if (!skb) {
4888                 err = -ENOBUFS;
4889                 goto errout_ifa;
4890         }
4891
4892         err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4893                                 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4894         if (err < 0) {
4895                 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4896                 WARN_ON(err == -EMSGSIZE);
4897                 kfree_skb(skb);
4898                 goto errout_ifa;
4899         }
4900         err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4901 errout_ifa:
4902         in6_ifa_put(ifa);
4903 errout:
4904         return err;
4905 }
4906
4907 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4908 {
4909         struct sk_buff *skb;
4910         struct net *net = dev_net(ifa->idev->dev);
4911         int err = -ENOBUFS;
4912
4913         skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4914         if (!skb)
4915                 goto errout;
4916
4917         err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4918         if (err < 0) {
4919                 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4920                 WARN_ON(err == -EMSGSIZE);
4921                 kfree_skb(skb);
4922                 goto errout;
4923         }
4924         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4925         return;
4926 errout:
4927         if (err < 0)
4928                 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4929 }
4930
4931 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4932                                 __s32 *array, int bytes)
4933 {
4934         BUG_ON(bytes < (DEVCONF_MAX * 4));
4935
4936         memset(array, 0, bytes);
4937         array[DEVCONF_FORWARDING] = cnf->forwarding;
4938         array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4939         array[DEVCONF_MTU6] = cnf->mtu6;
4940         array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4941         array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4942         array[DEVCONF_AUTOCONF] = cnf->autoconf;
4943         array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4944         array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4945         array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4946                 jiffies_to_msecs(cnf->rtr_solicit_interval);
4947         array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4948                 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
4949         array[DEVCONF_RTR_SOLICIT_DELAY] =
4950                 jiffies_to_msecs(cnf->rtr_solicit_delay);
4951         array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4952         array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4953                 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4954         array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4955                 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4956         array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4957         array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4958         array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4959         array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4960         array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4961         array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4962         array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4963         array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4964         array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4965 #ifdef CONFIG_IPV6_ROUTER_PREF
4966         array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4967         array[DEVCONF_RTR_PROBE_INTERVAL] =
4968                 jiffies_to_msecs(cnf->rtr_probe_interval);
4969 #ifdef CONFIG_IPV6_ROUTE_INFO
4970         array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4971 #endif
4972 #endif
4973         array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4974         array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4975 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4976         array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4977         array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4978 #endif
4979 #ifdef CONFIG_IPV6_MROUTE
4980         array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4981 #endif
4982         array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4983         array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4984         array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4985         array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4986         array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4987         array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4988         array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4989         array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
4990         /* we omit DEVCONF_STABLE_SECRET for now */
4991         array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4992         array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
4993         array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4994         array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
4995 }
4996
4997 static inline size_t inet6_ifla6_size(void)
4998 {
4999         return nla_total_size(4) /* IFLA_INET6_FLAGS */
5000              + nla_total_size(sizeof(struct ifla_cacheinfo))
5001              + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5002              + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5003              + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5004              + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5005 }
5006
5007 static inline size_t inet6_if_nlmsg_size(void)
5008 {
5009         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5010                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5011                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5012                + nla_total_size(4) /* IFLA_MTU */
5013                + nla_total_size(4) /* IFLA_LINK */
5014                + nla_total_size(1) /* IFLA_OPERSTATE */
5015                + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5016 }
5017
5018 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5019                                         int bytes)
5020 {
5021         int i;
5022         int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5023         BUG_ON(pad < 0);
5024
5025         /* Use put_unaligned() because stats may not be aligned for u64. */
5026         put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5027         for (i = 1; i < ICMP6_MIB_MAX; i++)
5028                 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5029
5030         memset(&stats[ICMP6_MIB_MAX], 0, pad);
5031 }
5032
5033 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5034                                         int bytes, size_t syncpoff)
5035 {
5036         int i, c;
5037         u64 buff[IPSTATS_MIB_MAX];
5038         int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5039
5040         BUG_ON(pad < 0);
5041
5042         memset(buff, 0, sizeof(buff));
5043         buff[0] = IPSTATS_MIB_MAX;
5044
5045         for_each_possible_cpu(c) {
5046                 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5047                         buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5048         }
5049
5050         memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5051         memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5052 }
5053
5054 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5055                              int bytes)
5056 {
5057         switch (attrtype) {
5058         case IFLA_INET6_STATS:
5059                 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5060                                      offsetof(struct ipstats_mib, syncp));
5061                 break;
5062         case IFLA_INET6_ICMP6STATS:
5063                 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5064                 break;
5065         }
5066 }
5067
5068 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5069                                   u32 ext_filter_mask)
5070 {
5071         struct nlattr *nla;
5072         struct ifla_cacheinfo ci;
5073
5074         if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5075                 goto nla_put_failure;
5076         ci.max_reasm_len = IPV6_MAXPLEN;
5077         ci.tstamp = cstamp_delta(idev->tstamp);
5078         ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5079         ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5080         if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5081                 goto nla_put_failure;
5082         nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5083         if (!nla)
5084                 goto nla_put_failure;
5085         ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5086
5087         /* XXX - MC not implemented */
5088
5089         if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5090                 return 0;
5091
5092         nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5093         if (!nla)
5094                 goto nla_put_failure;
5095         snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5096
5097         nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5098         if (!nla)
5099                 goto nla_put_failure;
5100         snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5101
5102         nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5103         if (!nla)
5104                 goto nla_put_failure;
5105
5106         if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5107                 goto nla_put_failure;
5108
5109         read_lock_bh(&idev->lock);
5110         memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5111         read_unlock_bh(&idev->lock);
5112
5113         return 0;
5114
5115 nla_put_failure:
5116         return -EMSGSIZE;
5117 }
5118
5119 static size_t inet6_get_link_af_size(const struct net_device *dev,
5120                                      u32 ext_filter_mask)
5121 {
5122         if (!__in6_dev_get(dev))
5123                 return 0;
5124
5125         return inet6_ifla6_size();
5126 }
5127
5128 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5129                               u32 ext_filter_mask)
5130 {
5131         struct inet6_dev *idev = __in6_dev_get(dev);
5132
5133         if (!idev)
5134                 return -ENODATA;
5135
5136         if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5137                 return -EMSGSIZE;
5138
5139         return 0;
5140 }
5141
5142 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5143 {
5144         struct inet6_ifaddr *ifp;
5145         struct net_device *dev = idev->dev;
5146         bool clear_token, update_rs = false;
5147         struct in6_addr ll_addr;
5148
5149         ASSERT_RTNL();
5150
5151         if (!token)
5152                 return -EINVAL;
5153         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5154                 return -EINVAL;
5155         if (!ipv6_accept_ra(idev))
5156                 return -EINVAL;
5157         if (idev->cnf.rtr_solicits == 0)
5158                 return -EINVAL;
5159
5160         write_lock_bh(&idev->lock);
5161
5162         BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5163         memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5164
5165         write_unlock_bh(&idev->lock);
5166
5167         clear_token = ipv6_addr_any(token);
5168         if (clear_token)
5169                 goto update_lft;
5170
5171         if (!idev->dead && (idev->if_flags & IF_READY) &&
5172             !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5173                              IFA_F_OPTIMISTIC)) {
5174                 /* If we're not ready, then normal ifup will take care
5175                  * of this. Otherwise, we need to request our rs here.
5176                  */
5177                 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5178                 update_rs = true;
5179         }
5180
5181 update_lft:
5182         write_lock_bh(&idev->lock);
5183
5184         if (update_rs) {
5185                 idev->if_flags |= IF_RS_SENT;
5186                 idev->rs_interval = rfc3315_s14_backoff_init(
5187                         idev->cnf.rtr_solicit_interval);
5188                 idev->rs_probes = 1;
5189                 addrconf_mod_rs_timer(idev, idev->rs_interval);
5190         }
5191
5192         /* Well, that's kinda nasty ... */
5193         list_for_each_entry(ifp, &idev->addr_list, if_list) {
5194                 spin_lock(&ifp->lock);
5195                 if (ifp->tokenized) {
5196                         ifp->valid_lft = 0;
5197                         ifp->prefered_lft = 0;
5198                 }
5199                 spin_unlock(&ifp->lock);
5200         }
5201
5202         write_unlock_bh(&idev->lock);
5203         inet6_ifinfo_notify(RTM_NEWLINK, idev);
5204         addrconf_verify_rtnl();
5205         return 0;
5206 }
5207
5208 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5209         [IFLA_INET6_ADDR_GEN_MODE]      = { .type = NLA_U8 },
5210         [IFLA_INET6_TOKEN]              = { .len = sizeof(struct in6_addr) },
5211 };
5212
5213 static int inet6_validate_link_af(const struct net_device *dev,
5214                                   const struct nlattr *nla)
5215 {
5216         struct nlattr *tb[IFLA_INET6_MAX + 1];
5217
5218         if (dev && !__in6_dev_get(dev))
5219                 return -EAFNOSUPPORT;
5220
5221         return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5222 }
5223
5224 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5225 {
5226         int err = -EINVAL;
5227         struct inet6_dev *idev = __in6_dev_get(dev);
5228         struct nlattr *tb[IFLA_INET6_MAX + 1];
5229
5230         if (!idev)
5231                 return -EAFNOSUPPORT;
5232
5233         if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5234                 BUG();
5235
5236         if (tb[IFLA_INET6_TOKEN]) {
5237                 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5238                 if (err)
5239                         return err;
5240         }
5241
5242         if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5243                 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5244
5245                 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5246                     mode != IN6_ADDR_GEN_MODE_NONE &&
5247                     mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5248                     mode != IN6_ADDR_GEN_MODE_RANDOM)
5249                         return -EINVAL;
5250
5251                 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5252                     !idev->cnf.stable_secret.initialized &&
5253                     !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5254                         return -EINVAL;
5255
5256                 idev->addr_gen_mode = mode;
5257                 err = 0;
5258         }
5259
5260         return err;
5261 }
5262
5263 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5264                              u32 portid, u32 seq, int event, unsigned int flags)
5265 {
5266         struct net_device *dev = idev->dev;
5267         struct ifinfomsg *hdr;
5268         struct nlmsghdr *nlh;
5269         void *protoinfo;
5270
5271         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5272         if (!nlh)
5273                 return -EMSGSIZE;
5274
5275         hdr = nlmsg_data(nlh);
5276         hdr->ifi_family = AF_INET6;
5277         hdr->__ifi_pad = 0;
5278         hdr->ifi_type = dev->type;
5279         hdr->ifi_index = dev->ifindex;
5280         hdr->ifi_flags = dev_get_flags(dev);
5281         hdr->ifi_change = 0;
5282
5283         if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5284             (dev->addr_len &&
5285              nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5286             nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5287             (dev->ifindex != dev_get_iflink(dev) &&
5288              nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5289             nla_put_u8(skb, IFLA_OPERSTATE,
5290                        netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5291                 goto nla_put_failure;
5292         protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5293         if (!protoinfo)
5294                 goto nla_put_failure;
5295
5296         if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5297                 goto nla_put_failure;
5298
5299         nla_nest_end(skb, protoinfo);
5300         nlmsg_end(skb, nlh);
5301         return 0;
5302
5303 nla_put_failure:
5304         nlmsg_cancel(skb, nlh);
5305         return -EMSGSIZE;
5306 }
5307
5308 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5309 {
5310         struct net *net = sock_net(skb->sk);
5311         int h, s_h;
5312         int idx = 0, s_idx;
5313         struct net_device *dev;
5314         struct inet6_dev *idev;
5315         struct hlist_head *head;
5316
5317         s_h = cb->args[0];
5318         s_idx = cb->args[1];
5319
5320         rcu_read_lock();
5321         for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5322                 idx = 0;
5323                 head = &net->dev_index_head[h];
5324                 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5325                         if (idx < s_idx)
5326                                 goto cont;
5327                         idev = __in6_dev_get(dev);
5328                         if (!idev)
5329                                 goto cont;
5330                         if (inet6_fill_ifinfo(skb, idev,
5331                                               NETLINK_CB(cb->skb).portid,
5332                                               cb->nlh->nlmsg_seq,
5333                                               RTM_NEWLINK, NLM_F_MULTI) < 0)
5334                                 goto out;
5335 cont:
5336                         idx++;
5337                 }
5338         }
5339 out:
5340         rcu_read_unlock();
5341         cb->args[1] = idx;
5342         cb->args[0] = h;
5343
5344         return skb->len;
5345 }
5346
5347 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5348 {
5349         struct sk_buff *skb;
5350         struct net *net = dev_net(idev->dev);
5351         int err = -ENOBUFS;
5352
5353         skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5354         if (!skb)
5355                 goto errout;
5356
5357         err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5358         if (err < 0) {
5359                 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5360                 WARN_ON(err == -EMSGSIZE);
5361                 kfree_skb(skb);
5362                 goto errout;
5363         }
5364         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5365         return;
5366 errout:
5367         if (err < 0)
5368                 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5369 }
5370
5371 static inline size_t inet6_prefix_nlmsg_size(void)
5372 {
5373         return NLMSG_ALIGN(sizeof(struct prefixmsg))
5374                + nla_total_size(sizeof(struct in6_addr))
5375                + nla_total_size(sizeof(struct prefix_cacheinfo));
5376 }
5377
5378 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5379                              struct prefix_info *pinfo, u32 portid, u32 seq,
5380                              int event, unsigned int flags)
5381 {
5382         struct prefixmsg *pmsg;
5383         struct nlmsghdr *nlh;
5384         struct prefix_cacheinfo ci;
5385
5386         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5387         if (!nlh)
5388                 return -EMSGSIZE;
5389
5390         pmsg = nlmsg_data(nlh);
5391         pmsg->prefix_family = AF_INET6;
5392         pmsg->prefix_pad1 = 0;
5393         pmsg->prefix_pad2 = 0;
5394         pmsg->prefix_ifindex = idev->dev->ifindex;
5395         pmsg->prefix_len = pinfo->prefix_len;
5396         pmsg->prefix_type = pinfo->type;
5397         pmsg->prefix_pad3 = 0;
5398         pmsg->prefix_flags = 0;
5399         if (pinfo->onlink)
5400                 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5401         if (pinfo->autoconf)
5402                 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5403
5404         if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5405                 goto nla_put_failure;
5406         ci.preferred_time = ntohl(pinfo->prefered);
5407         ci.valid_time = ntohl(pinfo->valid);
5408         if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5409                 goto nla_put_failure;
5410         nlmsg_end(skb, nlh);
5411         return 0;
5412
5413 nla_put_failure:
5414         nlmsg_cancel(skb, nlh);
5415         return -EMSGSIZE;
5416 }
5417
5418 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5419                          struct prefix_info *pinfo)
5420 {
5421         struct sk_buff *skb;
5422         struct net *net = dev_net(idev->dev);
5423         int err = -ENOBUFS;
5424
5425         skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5426         if (!skb)
5427                 goto errout;
5428
5429         err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5430         if (err < 0) {
5431                 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5432                 WARN_ON(err == -EMSGSIZE);
5433                 kfree_skb(skb);
5434                 goto errout;
5435         }
5436         rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5437         return;
5438 errout:
5439         if (err < 0)
5440                 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5441 }
5442
5443 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5444 {
5445         struct net *net = dev_net(ifp->idev->dev);
5446
5447         if (event)
5448                 ASSERT_RTNL();
5449
5450         inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5451
5452         switch (event) {
5453         case RTM_NEWADDR:
5454                 /*
5455                  * If the address was optimistic we inserted the route at the
5456                  * start of our DAD process, so we don't need to do it again.
5457                  * If the device was taken down in the middle of the DAD
5458                  * cycle there is a race where we could get here without a
5459                  * host route, so nothing to insert. That will be fixed when
5460                  * the device is brought up.
5461                  */
5462                 if (ifp->rt && !rcu_access_pointer(ifp->rt->rt6i_node)) {
5463                         ip6_ins_rt(ifp->rt);
5464                 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
5465                         pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
5466                                 &ifp->addr, ifp->idev->dev->name);
5467                 }
5468
5469                 if (ifp->idev->cnf.forwarding)
5470                         addrconf_join_anycast(ifp);
5471                 if (!ipv6_addr_any(&ifp->peer_addr))
5472                         addrconf_prefix_route(&ifp->peer_addr, 128,
5473                                               ifp->idev->dev, 0, 0);
5474                 break;
5475         case RTM_DELADDR:
5476                 if (ifp->idev->cnf.forwarding)
5477                         addrconf_leave_anycast(ifp);
5478                 addrconf_leave_solict(ifp->idev, &ifp->addr);
5479                 if (!ipv6_addr_any(&ifp->peer_addr)) {
5480                         struct rt6_info *rt;
5481
5482                         rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5483                                                        ifp->idev->dev, 0, 0);
5484                         if (rt)
5485                                 ip6_del_rt(rt);
5486                 }
5487                 if (ifp->rt) {
5488                         dst_hold(&ifp->rt->dst);
5489                         ip6_del_rt(ifp->rt);
5490                 }
5491                 rt_genid_bump_ipv6(net);
5492                 break;
5493         }
5494         atomic_inc(&net->ipv6.dev_addr_genid);
5495 }
5496
5497 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5498 {
5499         rcu_read_lock_bh();
5500         if (likely(ifp->idev->dead == 0))
5501                 __ipv6_ifa_notify(event, ifp);
5502         rcu_read_unlock_bh();
5503 }
5504
5505 #ifdef CONFIG_SYSCTL
5506
5507 static
5508 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5509                            void __user *buffer, size_t *lenp, loff_t *ppos)
5510 {
5511         int *valp = ctl->data;
5512         int val = *valp;
5513         loff_t pos = *ppos;
5514         struct ctl_table lctl;
5515         int ret;
5516
5517         /*
5518          * ctl->data points to idev->cnf.forwarding, we should
5519          * not modify it until we get the rtnl lock.
5520          */
5521         lctl = *ctl;
5522         lctl.data = &val;
5523
5524         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5525
5526         if (write)
5527                 ret = addrconf_fixup_forwarding(ctl, valp, val);
5528         if (ret)
5529                 *ppos = pos;
5530         return ret;
5531 }
5532
5533 static
5534 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5535                         void __user *buffer, size_t *lenp, loff_t *ppos)
5536 {
5537         struct inet6_dev *idev = ctl->extra1;
5538         int min_mtu = IPV6_MIN_MTU;
5539         struct ctl_table lctl;
5540
5541         lctl = *ctl;
5542         lctl.extra1 = &min_mtu;
5543         lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5544
5545         return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5546 }
5547
5548 static void dev_disable_change(struct inet6_dev *idev)
5549 {
5550         struct netdev_notifier_info info;
5551
5552         if (!idev || !idev->dev)
5553                 return;
5554
5555         netdev_notifier_info_init(&info, idev->dev);
5556         if (idev->cnf.disable_ipv6)
5557                 addrconf_notify(NULL, NETDEV_DOWN, &info);
5558         else
5559                 addrconf_notify(NULL, NETDEV_UP, &info);
5560 }
5561
5562 static void addrconf_disable_change(struct net *net, __s32 newf)
5563 {
5564         struct net_device *dev;
5565         struct inet6_dev *idev;
5566
5567         for_each_netdev(net, dev) {
5568                 idev = __in6_dev_get(dev);
5569                 if (idev) {
5570                         int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5571                         idev->cnf.disable_ipv6 = newf;
5572                         if (changed)
5573                                 dev_disable_change(idev);
5574                 }
5575         }
5576 }
5577
5578 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5579 {
5580         struct net *net;
5581         int old;
5582
5583         if (!rtnl_trylock())
5584                 return restart_syscall();
5585
5586         net = (struct net *)table->extra2;
5587         old = *p;
5588         *p = newf;
5589
5590         if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5591                 rtnl_unlock();
5592                 return 0;
5593         }
5594
5595         if (p == &net->ipv6.devconf_all->disable_ipv6) {
5596                 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5597                 addrconf_disable_change(net, newf);
5598         } else if ((!newf) ^ (!old))
5599                 dev_disable_change((struct inet6_dev *)table->extra1);
5600
5601         rtnl_unlock();
5602         return 0;
5603 }
5604
5605 static
5606 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5607                             void __user *buffer, size_t *lenp, loff_t *ppos)
5608 {
5609         int *valp = ctl->data;
5610         int val = *valp;
5611         loff_t pos = *ppos;
5612         struct ctl_table lctl;
5613         int ret;
5614
5615         /*
5616          * ctl->data points to idev->cnf.disable_ipv6, we should
5617          * not modify it until we get the rtnl lock.
5618          */
5619         lctl = *ctl;
5620         lctl.data = &val;
5621
5622         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5623
5624         if (write)
5625                 ret = addrconf_disable_ipv6(ctl, valp, val);
5626         if (ret)
5627                 *ppos = pos;
5628         return ret;
5629 }
5630
5631 static
5632 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5633                               void __user *buffer, size_t *lenp, loff_t *ppos)
5634 {
5635         int *valp = ctl->data;
5636         int ret;
5637         int old, new;
5638
5639         old = *valp;
5640         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5641         new = *valp;
5642
5643         if (write && old != new) {
5644                 struct net *net = ctl->extra2;
5645
5646                 if (!rtnl_trylock())
5647                         return restart_syscall();
5648
5649                 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5650                         inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5651                                                      NETCONFA_IFINDEX_DEFAULT,
5652                                                      net->ipv6.devconf_dflt);
5653                 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5654                         inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5655                                                      NETCONFA_IFINDEX_ALL,
5656                                                      net->ipv6.devconf_all);
5657                 else {
5658                         struct inet6_dev *idev = ctl->extra1;
5659
5660                         inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5661                                                      idev->dev->ifindex,
5662                                                      &idev->cnf);
5663                 }
5664                 rtnl_unlock();
5665         }
5666
5667         return ret;
5668 }
5669
5670 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5671                                          void __user *buffer, size_t *lenp,
5672                                          loff_t *ppos)
5673 {
5674         int err;
5675         struct in6_addr addr;
5676         char str[IPV6_MAX_STRLEN];
5677         struct ctl_table lctl = *ctl;
5678         struct net *net = ctl->extra2;
5679         struct ipv6_stable_secret *secret = ctl->data;
5680
5681         if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5682                 return -EIO;
5683
5684         lctl.maxlen = IPV6_MAX_STRLEN;
5685         lctl.data = str;
5686
5687         if (!rtnl_trylock())
5688                 return restart_syscall();
5689
5690         if (!write && !secret->initialized) {
5691                 err = -EIO;
5692                 goto out;
5693         }
5694
5695         err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5696         if (err >= sizeof(str)) {
5697                 err = -EIO;
5698                 goto out;
5699         }
5700
5701         err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5702         if (err || !write)
5703                 goto out;
5704
5705         if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5706                 err = -EIO;
5707                 goto out;
5708         }
5709
5710         secret->initialized = true;
5711         secret->secret = addr;
5712
5713         if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5714                 struct net_device *dev;
5715
5716                 for_each_netdev(net, dev) {
5717                         struct inet6_dev *idev = __in6_dev_get(dev);
5718
5719                         if (idev) {
5720                                 idev->addr_gen_mode =
5721                                         IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5722                         }
5723                 }
5724         } else {
5725                 struct inet6_dev *idev = ctl->extra1;
5726
5727                 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5728         }
5729
5730 out:
5731         rtnl_unlock();
5732
5733         return err;
5734 }
5735
5736 static
5737 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5738                                                 int write,
5739                                                 void __user *buffer,
5740                                                 size_t *lenp,
5741                                                 loff_t *ppos)
5742 {
5743         int *valp = ctl->data;
5744         int val = *valp;
5745         loff_t pos = *ppos;
5746         struct ctl_table lctl;
5747         int ret;
5748
5749         /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5750          * we should not modify it until we get the rtnl lock.
5751          */
5752         lctl = *ctl;
5753         lctl.data = &val;
5754
5755         ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5756
5757         if (write)
5758                 ret = addrconf_fixup_linkdown(ctl, valp, val);
5759         if (ret)
5760                 *ppos = pos;
5761         return ret;
5762 }
5763
5764 static int minus_one = -1;
5765 static const int one = 1;
5766 static const int two_five_five = 255;
5767
5768 static const struct ctl_table addrconf_sysctl[] = {
5769         {
5770                 .procname       = "forwarding",
5771                 .data           = &ipv6_devconf.forwarding,
5772                 .maxlen         = sizeof(int),
5773                 .mode           = 0644,
5774                 .proc_handler   = addrconf_sysctl_forward,
5775         },
5776         {
5777                 .procname       = "hop_limit",
5778                 .data           = &ipv6_devconf.hop_limit,
5779                 .maxlen         = sizeof(int),
5780                 .mode           = 0644,
5781                 .proc_handler   = proc_dointvec_minmax,
5782                 .extra1         = (void *)&one,
5783                 .extra2         = (void *)&two_five_five,
5784         },
5785         {
5786                 .procname       = "mtu",
5787                 .data           = &ipv6_devconf.mtu6,
5788                 .maxlen         = sizeof(int),
5789                 .mode           = 0644,
5790                 .proc_handler   = addrconf_sysctl_mtu,
5791         },
5792         {
5793                 .procname       = "accept_ra",
5794                 .data           = &ipv6_devconf.accept_ra,
5795                 .maxlen         = sizeof(int),
5796                 .mode           = 0644,
5797                 .proc_handler   = proc_dointvec,
5798         },
5799         {
5800                 .procname       = "accept_redirects",
5801                 .data           = &ipv6_devconf.accept_redirects,
5802                 .maxlen         = sizeof(int),
5803                 .mode           = 0644,
5804                 .proc_handler   = proc_dointvec,
5805         },
5806         {
5807                 .procname       = "autoconf",
5808                 .data           = &ipv6_devconf.autoconf,
5809                 .maxlen         = sizeof(int),
5810                 .mode           = 0644,
5811                 .proc_handler   = proc_dointvec,
5812         },
5813         {
5814                 .procname       = "dad_transmits",
5815                 .data           = &ipv6_devconf.dad_transmits,
5816                 .maxlen         = sizeof(int),
5817                 .mode           = 0644,
5818                 .proc_handler   = proc_dointvec,
5819         },
5820         {
5821                 .procname       = "router_solicitations",
5822                 .data           = &ipv6_devconf.rtr_solicits,
5823                 .maxlen         = sizeof(int),
5824                 .mode           = 0644,
5825                 .proc_handler   = proc_dointvec_minmax,
5826                 .extra1         = &minus_one,
5827         },
5828         {
5829                 .procname       = "router_solicitation_interval",
5830                 .data           = &ipv6_devconf.rtr_solicit_interval,
5831                 .maxlen         = sizeof(int),
5832                 .mode           = 0644,
5833                 .proc_handler   = proc_dointvec_jiffies,
5834         },
5835         {
5836                 .procname       = "router_solicitation_max_interval",
5837                 .data           = &ipv6_devconf.rtr_solicit_max_interval,
5838                 .maxlen         = sizeof(int),
5839                 .mode           = 0644,
5840                 .proc_handler   = proc_dointvec_jiffies,
5841         },
5842         {
5843                 .procname       = "router_solicitation_delay",
5844                 .data           = &ipv6_devconf.rtr_solicit_delay,
5845                 .maxlen         = sizeof(int),
5846                 .mode           = 0644,
5847                 .proc_handler   = proc_dointvec_jiffies,
5848         },
5849         {
5850                 .procname       = "force_mld_version",
5851                 .data           = &ipv6_devconf.force_mld_version,
5852                 .maxlen         = sizeof(int),
5853                 .mode           = 0644,
5854                 .proc_handler   = proc_dointvec,
5855         },
5856         {
5857                 .procname       = "mldv1_unsolicited_report_interval",
5858                 .data           =
5859                         &ipv6_devconf.mldv1_unsolicited_report_interval,
5860                 .maxlen         = sizeof(int),
5861                 .mode           = 0644,
5862                 .proc_handler   = proc_dointvec_ms_jiffies,
5863         },
5864         {
5865                 .procname       = "mldv2_unsolicited_report_interval",
5866                 .data           =
5867                         &ipv6_devconf.mldv2_unsolicited_report_interval,
5868                 .maxlen         = sizeof(int),
5869                 .mode           = 0644,
5870                 .proc_handler   = proc_dointvec_ms_jiffies,
5871         },
5872         {
5873                 .procname       = "use_tempaddr",
5874                 .data           = &ipv6_devconf.use_tempaddr,
5875                 .maxlen         = sizeof(int),
5876                 .mode           = 0644,
5877                 .proc_handler   = proc_dointvec,
5878         },
5879         {
5880                 .procname       = "temp_valid_lft",
5881                 .data           = &ipv6_devconf.temp_valid_lft,
5882                 .maxlen         = sizeof(int),
5883                 .mode           = 0644,
5884                 .proc_handler   = proc_dointvec,
5885         },
5886         {
5887                 .procname       = "temp_prefered_lft",
5888                 .data           = &ipv6_devconf.temp_prefered_lft,
5889                 .maxlen         = sizeof(int),
5890                 .mode           = 0644,
5891                 .proc_handler   = proc_dointvec,
5892         },
5893         {
5894                 .procname       = "regen_max_retry",
5895                 .data           = &ipv6_devconf.regen_max_retry,
5896                 .maxlen         = sizeof(int),
5897                 .mode           = 0644,
5898                 .proc_handler   = proc_dointvec,
5899         },
5900         {
5901                 .procname       = "max_desync_factor",
5902                 .data           = &ipv6_devconf.max_desync_factor,
5903                 .maxlen         = sizeof(int),
5904                 .mode           = 0644,
5905                 .proc_handler   = proc_dointvec,
5906         },
5907         {
5908                 .procname       = "max_addresses",
5909                 .data           = &ipv6_devconf.max_addresses,
5910                 .maxlen         = sizeof(int),
5911                 .mode           = 0644,
5912                 .proc_handler   = proc_dointvec,
5913         },
5914         {
5915                 .procname       = "accept_ra_defrtr",
5916                 .data           = &ipv6_devconf.accept_ra_defrtr,
5917                 .maxlen         = sizeof(int),
5918                 .mode           = 0644,
5919                 .proc_handler   = proc_dointvec,
5920         },
5921         {
5922                 .procname       = "accept_ra_min_hop_limit",
5923                 .data           = &ipv6_devconf.accept_ra_min_hop_limit,
5924                 .maxlen         = sizeof(int),
5925                 .mode           = 0644,
5926                 .proc_handler   = proc_dointvec,
5927         },
5928         {
5929                 .procname       = "accept_ra_pinfo",
5930                 .data           = &ipv6_devconf.accept_ra_pinfo,
5931                 .maxlen         = sizeof(int),
5932                 .mode           = 0644,
5933                 .proc_handler   = proc_dointvec,
5934         },
5935 #ifdef CONFIG_IPV6_ROUTER_PREF
5936         {
5937                 .procname       = "accept_ra_rtr_pref",
5938                 .data           = &ipv6_devconf.accept_ra_rtr_pref,
5939                 .maxlen         = sizeof(int),
5940                 .mode           = 0644,
5941                 .proc_handler   = proc_dointvec,
5942         },
5943         {
5944                 .procname       = "router_probe_interval",
5945                 .data           = &ipv6_devconf.rtr_probe_interval,
5946                 .maxlen         = sizeof(int),
5947                 .mode           = 0644,
5948                 .proc_handler   = proc_dointvec_jiffies,
5949         },
5950 #ifdef CONFIG_IPV6_ROUTE_INFO
5951         {
5952                 .procname       = "accept_ra_rt_info_max_plen",
5953                 .data           = &ipv6_devconf.accept_ra_rt_info_max_plen,
5954                 .maxlen         = sizeof(int),
5955                 .mode           = 0644,
5956                 .proc_handler   = proc_dointvec,
5957         },
5958 #endif
5959 #endif
5960         {
5961                 .procname       = "proxy_ndp",
5962                 .data           = &ipv6_devconf.proxy_ndp,
5963                 .maxlen         = sizeof(int),
5964                 .mode           = 0644,
5965                 .proc_handler   = addrconf_sysctl_proxy_ndp,
5966         },
5967         {
5968                 .procname       = "accept_source_route",
5969                 .data           = &ipv6_devconf.accept_source_route,
5970                 .maxlen         = sizeof(int),
5971                 .mode           = 0644,
5972                 .proc_handler   = proc_dointvec,
5973         },
5974 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5975         {
5976                 .procname       = "optimistic_dad",
5977                 .data           = &ipv6_devconf.optimistic_dad,
5978                 .maxlen         = sizeof(int),
5979                 .mode           = 0644,
5980                 .proc_handler   = proc_dointvec,
5981         },
5982         {
5983                 .procname       = "use_optimistic",
5984                 .data           = &ipv6_devconf.use_optimistic,
5985                 .maxlen         = sizeof(int),
5986                 .mode           = 0644,
5987                 .proc_handler   = proc_dointvec,
5988         },
5989 #endif
5990 #ifdef CONFIG_IPV6_MROUTE
5991         {
5992                 .procname       = "mc_forwarding",
5993                 .data           = &ipv6_devconf.mc_forwarding,
5994                 .maxlen         = sizeof(int),
5995                 .mode           = 0444,
5996                 .proc_handler   = proc_dointvec,
5997         },
5998 #endif
5999         {
6000                 .procname       = "disable_ipv6",
6001                 .data           = &ipv6_devconf.disable_ipv6,
6002                 .maxlen         = sizeof(int),
6003                 .mode           = 0644,
6004                 .proc_handler   = addrconf_sysctl_disable,
6005         },
6006         {
6007                 .procname       = "accept_dad",
6008                 .data           = &ipv6_devconf.accept_dad,
6009                 .maxlen         = sizeof(int),
6010                 .mode           = 0644,
6011                 .proc_handler   = proc_dointvec,
6012         },
6013         {
6014                 .procname       = "force_tllao",
6015                 .data           = &ipv6_devconf.force_tllao,
6016                 .maxlen         = sizeof(int),
6017                 .mode           = 0644,
6018                 .proc_handler   = proc_dointvec
6019         },
6020         {
6021                 .procname       = "ndisc_notify",
6022                 .data           = &ipv6_devconf.ndisc_notify,
6023                 .maxlen         = sizeof(int),
6024                 .mode           = 0644,
6025                 .proc_handler   = proc_dointvec
6026         },
6027         {
6028                 .procname       = "suppress_frag_ndisc",
6029                 .data           = &ipv6_devconf.suppress_frag_ndisc,
6030                 .maxlen         = sizeof(int),
6031                 .mode           = 0644,
6032                 .proc_handler   = proc_dointvec
6033         },
6034         {
6035                 .procname       = "accept_ra_from_local",
6036                 .data           = &ipv6_devconf.accept_ra_from_local,
6037                 .maxlen         = sizeof(int),
6038                 .mode           = 0644,
6039                 .proc_handler   = proc_dointvec,
6040         },
6041         {
6042                 .procname       = "accept_ra_mtu",
6043                 .data           = &ipv6_devconf.accept_ra_mtu,
6044                 .maxlen         = sizeof(int),
6045                 .mode           = 0644,
6046                 .proc_handler   = proc_dointvec,
6047         },
6048         {
6049                 .procname       = "stable_secret",
6050                 .data           = &ipv6_devconf.stable_secret,
6051                 .maxlen         = IPV6_MAX_STRLEN,
6052                 .mode           = 0600,
6053                 .proc_handler   = addrconf_sysctl_stable_secret,
6054         },
6055         {
6056                 .procname       = "use_oif_addrs_only",
6057                 .data           = &ipv6_devconf.use_oif_addrs_only,
6058                 .maxlen         = sizeof(int),
6059                 .mode           = 0644,
6060                 .proc_handler   = proc_dointvec,
6061         },
6062         {
6063                 .procname       = "ignore_routes_with_linkdown",
6064                 .data           = &ipv6_devconf.ignore_routes_with_linkdown,
6065                 .maxlen         = sizeof(int),
6066                 .mode           = 0644,
6067                 .proc_handler   = addrconf_sysctl_ignore_routes_with_linkdown,
6068         },
6069         {
6070                 .procname       = "drop_unicast_in_l2_multicast",
6071                 .data           = &ipv6_devconf.drop_unicast_in_l2_multicast,
6072                 .maxlen         = sizeof(int),
6073                 .mode           = 0644,
6074                 .proc_handler   = proc_dointvec,
6075         },
6076         {
6077                 .procname       = "drop_unsolicited_na",
6078                 .data           = &ipv6_devconf.drop_unsolicited_na,
6079                 .maxlen         = sizeof(int),
6080                 .mode           = 0644,
6081                 .proc_handler   = proc_dointvec,
6082         },
6083         {
6084                 .procname       = "keep_addr_on_down",
6085                 .data           = &ipv6_devconf.keep_addr_on_down,
6086                 .maxlen         = sizeof(int),
6087                 .mode           = 0644,
6088                 .proc_handler   = proc_dointvec,
6089
6090         },
6091         {
6092                 /* sentinel */
6093         }
6094 };
6095
6096 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6097                 struct inet6_dev *idev, struct ipv6_devconf *p)
6098 {
6099         int i, ifindex;
6100         struct ctl_table *table;
6101         char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6102
6103         table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6104         if (!table)
6105                 goto out;
6106
6107         for (i = 0; table[i].data; i++) {
6108                 table[i].data += (char *)p - (char *)&ipv6_devconf;
6109                 /* If one of these is already set, then it is not safe to
6110                  * overwrite either of them: this makes proc_dointvec_minmax
6111                  * usable.
6112                  */
6113                 if (!table[i].extra1 && !table[i].extra2) {
6114                         table[i].extra1 = idev; /* embedded; no ref */
6115                         table[i].extra2 = net;
6116                 }
6117         }
6118
6119         snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6120
6121         p->sysctl_header = register_net_sysctl(net, path, table);
6122         if (!p->sysctl_header)
6123                 goto free;
6124
6125         if (!strcmp(dev_name, "all"))
6126                 ifindex = NETCONFA_IFINDEX_ALL;
6127         else if (!strcmp(dev_name, "default"))
6128                 ifindex = NETCONFA_IFINDEX_DEFAULT;
6129         else
6130                 ifindex = idev->dev->ifindex;
6131         inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
6132         return 0;
6133
6134 free:
6135         kfree(table);
6136 out:
6137         return -ENOBUFS;
6138 }
6139
6140 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6141 {
6142         struct ctl_table *table;
6143
6144         if (!p->sysctl_header)
6145                 return;
6146
6147         table = p->sysctl_header->ctl_table_arg;
6148         unregister_net_sysctl_table(p->sysctl_header);
6149         p->sysctl_header = NULL;
6150         kfree(table);
6151 }
6152
6153 static int addrconf_sysctl_register(struct inet6_dev *idev)
6154 {
6155         int err;
6156
6157         if (!sysctl_dev_name_is_allowed(idev->dev->name))
6158                 return -EINVAL;
6159
6160         err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6161                                     &ndisc_ifinfo_sysctl_change);
6162         if (err)
6163                 return err;
6164         err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6165                                          idev, &idev->cnf);
6166         if (err)
6167                 neigh_sysctl_unregister(idev->nd_parms);
6168
6169         return err;
6170 }
6171
6172 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6173 {
6174         __addrconf_sysctl_unregister(&idev->cnf);
6175         neigh_sysctl_unregister(idev->nd_parms);
6176 }
6177
6178
6179 #endif
6180
6181 static int __net_init addrconf_init_net(struct net *net)
6182 {
6183         int err = -ENOMEM;
6184         struct ipv6_devconf *all, *dflt;
6185
6186         all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6187         if (!all)
6188                 goto err_alloc_all;
6189
6190         dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6191         if (!dflt)
6192                 goto err_alloc_dflt;
6193
6194         /* these will be inherited by all namespaces */
6195         dflt->autoconf = ipv6_defaults.autoconf;
6196         dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6197
6198         dflt->stable_secret.initialized = false;
6199         all->stable_secret.initialized = false;
6200
6201         net->ipv6.devconf_all = all;
6202         net->ipv6.devconf_dflt = dflt;
6203
6204 #ifdef CONFIG_SYSCTL
6205         err = __addrconf_sysctl_register(net, "all", NULL, all);
6206         if (err < 0)
6207                 goto err_reg_all;
6208
6209         err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6210         if (err < 0)
6211                 goto err_reg_dflt;
6212 #endif
6213         return 0;
6214
6215 #ifdef CONFIG_SYSCTL
6216 err_reg_dflt:
6217         __addrconf_sysctl_unregister(all);
6218 err_reg_all:
6219         kfree(dflt);
6220 #endif
6221 err_alloc_dflt:
6222         kfree(all);
6223 err_alloc_all:
6224         return err;
6225 }
6226
6227 static void __net_exit addrconf_exit_net(struct net *net)
6228 {
6229 #ifdef CONFIG_SYSCTL
6230         __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6231         __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6232 #endif
6233         kfree(net->ipv6.devconf_dflt);
6234         kfree(net->ipv6.devconf_all);
6235 }
6236
6237 static struct pernet_operations addrconf_ops = {
6238         .init = addrconf_init_net,
6239         .exit = addrconf_exit_net,
6240 };
6241
6242 static struct rtnl_af_ops inet6_ops __read_mostly = {
6243         .family           = AF_INET6,
6244         .fill_link_af     = inet6_fill_link_af,
6245         .get_link_af_size = inet6_get_link_af_size,
6246         .validate_link_af = inet6_validate_link_af,
6247         .set_link_af      = inet6_set_link_af,
6248 };
6249
6250 /*
6251  *      Init / cleanup code
6252  */
6253
6254 int __init addrconf_init(void)
6255 {
6256         struct inet6_dev *idev;
6257         int i, err;
6258
6259         err = ipv6_addr_label_init();
6260         if (err < 0) {
6261                 pr_crit("%s: cannot initialize default policy table: %d\n",
6262                         __func__, err);
6263                 goto out;
6264         }
6265
6266         err = register_pernet_subsys(&addrconf_ops);
6267         if (err < 0)
6268                 goto out_addrlabel;
6269
6270         addrconf_wq = create_workqueue("ipv6_addrconf");
6271         if (!addrconf_wq) {
6272                 err = -ENOMEM;
6273                 goto out_nowq;
6274         }
6275
6276         /* The addrconf netdev notifier requires that loopback_dev
6277          * has it's ipv6 private information allocated and setup
6278          * before it can bring up and give link-local addresses
6279          * to other devices which are up.
6280          *
6281          * Unfortunately, loopback_dev is not necessarily the first
6282          * entry in the global dev_base list of net devices.  In fact,
6283          * it is likely to be the very last entry on that list.
6284          * So this causes the notifier registry below to try and
6285          * give link-local addresses to all devices besides loopback_dev
6286          * first, then loopback_dev, which cases all the non-loopback_dev
6287          * devices to fail to get a link-local address.
6288          *
6289          * So, as a temporary fix, allocate the ipv6 structure for
6290          * loopback_dev first by hand.
6291          * Longer term, all of the dependencies ipv6 has upon the loopback
6292          * device and it being up should be removed.
6293          */
6294         rtnl_lock();
6295         idev = ipv6_add_dev(init_net.loopback_dev);
6296         rtnl_unlock();
6297         if (IS_ERR(idev)) {
6298                 err = PTR_ERR(idev);
6299                 goto errlo;
6300         }
6301
6302         ip6_route_init_special_entries();
6303
6304         for (i = 0; i < IN6_ADDR_HSIZE; i++)
6305                 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6306
6307         register_netdevice_notifier(&ipv6_dev_notf);
6308
6309         addrconf_verify();
6310
6311         rtnl_af_register(&inet6_ops);
6312
6313         err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6314                               NULL);
6315         if (err < 0)
6316                 goto errout;
6317
6318         /* Only the first call to __rtnl_register can fail */
6319         __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6320         __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6321         __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6322                         inet6_dump_ifaddr, NULL);
6323         __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6324                         inet6_dump_ifmcaddr, NULL);
6325         __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6326                         inet6_dump_ifacaddr, NULL);
6327         __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6328                         inet6_netconf_dump_devconf, NULL);
6329
6330         ipv6_addr_label_rtnl_register();
6331
6332         return 0;
6333 errout:
6334         rtnl_af_unregister(&inet6_ops);
6335         unregister_netdevice_notifier(&ipv6_dev_notf);
6336 errlo:
6337         destroy_workqueue(addrconf_wq);
6338 out_nowq:
6339         unregister_pernet_subsys(&addrconf_ops);
6340 out_addrlabel:
6341         ipv6_addr_label_cleanup();
6342 out:
6343         return err;
6344 }
6345
6346 void addrconf_cleanup(void)
6347 {
6348         struct net_device *dev;
6349         int i;
6350
6351         unregister_netdevice_notifier(&ipv6_dev_notf);
6352         unregister_pernet_subsys(&addrconf_ops);
6353         ipv6_addr_label_cleanup();
6354
6355         rtnl_lock();
6356
6357         __rtnl_af_unregister(&inet6_ops);
6358
6359         /* clean dev list */
6360         for_each_netdev(&init_net, dev) {
6361                 if (__in6_dev_get(dev) == NULL)
6362                         continue;
6363                 addrconf_ifdown(dev, 1);
6364         }
6365         addrconf_ifdown(init_net.loopback_dev, 2);
6366
6367         /*
6368          *      Check hash table.
6369          */
6370         spin_lock_bh(&addrconf_hash_lock);
6371         for (i = 0; i < IN6_ADDR_HSIZE; i++)
6372                 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
6373         spin_unlock_bh(&addrconf_hash_lock);
6374         cancel_delayed_work(&addr_chk_work);
6375         rtnl_unlock();
6376
6377         destroy_workqueue(addrconf_wq);
6378 }