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