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