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