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