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