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, offset = 0;
3213 u32 pflags = 0;
3214
3215 ASSERT_RTNL();
3216
3217 memset(&addr, 0, sizeof(struct in6_addr));
3218 /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3219 if (idev->dev->addr_len == sizeof(struct in6_addr))
3220 offset = sizeof(struct in6_addr) - 4;
3221 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4);
3222
3223 if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
3224 scope = IPV6_ADDR_COMPATv4;
3225 plen = 96;
3226 pflags |= RTF_NONEXTHOP;
3227 } else {
3228 if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
3229 return;
3230
3231 addr.s6_addr32[0] = htonl(0xfe800000);
3232 scope = IFA_LINK;
3233 plen = 64;
3234 }
3235
3236 if (addr.s6_addr32[3]) {
3237 add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
3238 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3239 GFP_KERNEL);
3240 return;
3241 }
3242
3243 for_each_netdev(net, dev) {
3244 struct in_device *in_dev = __in_dev_get_rtnl(dev);
3245 if (in_dev && (dev->flags & IFF_UP)) {
3246 struct in_ifaddr *ifa;
3247 int flag = scope;
3248
3249 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3250 addr.s6_addr32[3] = ifa->ifa_local;
3251
3252 if (ifa->ifa_scope == RT_SCOPE_LINK)
3253 continue;
3254 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3255 if (idev->dev->flags&IFF_POINTOPOINT)
3256 continue;
3257 flag |= IFA_HOST;
3258 }
3259
3260 add_addr(idev, &addr, plen, flag,
3261 IFAPROT_UNSPEC);
3262 addrconf_prefix_route(&addr, plen, 0, idev->dev,
3263 0, pflags, GFP_KERNEL);
3264 }
3265 }
3266 }
3267 }
3268 #endif
3269
init_loopback(struct net_device * dev)3270 static void init_loopback(struct net_device *dev)
3271 {
3272 struct inet6_dev *idev;
3273
3274 /* ::1 */
3275
3276 ASSERT_RTNL();
3277
3278 idev = ipv6_find_idev(dev);
3279 if (IS_ERR(idev)) {
3280 pr_debug("%s: add_dev failed\n", __func__);
3281 return;
3282 }
3283
3284 add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
3285 }
3286
addrconf_add_linklocal(struct inet6_dev * idev,const struct in6_addr * addr,u32 flags)3287 void addrconf_add_linklocal(struct inet6_dev *idev,
3288 const struct in6_addr *addr, u32 flags)
3289 {
3290 struct ifa6_config cfg = {
3291 .pfx = addr,
3292 .plen = 64,
3293 .ifa_flags = flags | IFA_F_PERMANENT,
3294 .valid_lft = INFINITY_LIFE_TIME,
3295 .preferred_lft = INFINITY_LIFE_TIME,
3296 .scope = IFA_LINK,
3297 .ifa_proto = IFAPROT_KERNEL_LL
3298 };
3299 struct inet6_ifaddr *ifp;
3300
3301 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3302 if ((READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad) ||
3303 READ_ONCE(idev->cnf.optimistic_dad)) &&
3304 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3305 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3306 #endif
3307
3308 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3309 if (!IS_ERR(ifp)) {
3310 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3311 0, 0, GFP_ATOMIC);
3312 addrconf_dad_start(ifp);
3313 in6_ifa_put(ifp);
3314 }
3315 }
3316 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3317
ipv6_reserved_interfaceid(struct in6_addr address)3318 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3319 {
3320 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3321 return true;
3322
3323 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3324 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3325 return true;
3326
3327 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3328 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3329 return true;
3330
3331 return false;
3332 }
3333
ipv6_generate_stable_address(struct in6_addr * address,u8 dad_count,const struct inet6_dev * idev)3334 static int ipv6_generate_stable_address(struct in6_addr *address,
3335 u8 dad_count,
3336 const struct inet6_dev *idev)
3337 {
3338 static DEFINE_SPINLOCK(lock);
3339 static __u32 digest[SHA1_DIGEST_WORDS];
3340 static __u32 workspace[SHA1_WORKSPACE_WORDS];
3341
3342 static union {
3343 char __data[SHA1_BLOCK_SIZE];
3344 struct {
3345 struct in6_addr secret;
3346 __be32 prefix[2];
3347 unsigned char hwaddr[MAX_ADDR_LEN];
3348 u8 dad_count;
3349 } __packed;
3350 } data;
3351
3352 struct in6_addr secret;
3353 struct in6_addr temp;
3354 struct net *net = dev_net(idev->dev);
3355
3356 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3357
3358 if (idev->cnf.stable_secret.initialized)
3359 secret = idev->cnf.stable_secret.secret;
3360 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3361 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3362 else
3363 return -1;
3364
3365 retry:
3366 spin_lock_bh(&lock);
3367
3368 sha1_init(digest);
3369 memset(&data, 0, sizeof(data));
3370 memset(workspace, 0, sizeof(workspace));
3371 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3372 data.prefix[0] = address->s6_addr32[0];
3373 data.prefix[1] = address->s6_addr32[1];
3374 data.secret = secret;
3375 data.dad_count = dad_count;
3376
3377 sha1_transform(digest, data.__data, workspace);
3378
3379 temp = *address;
3380 temp.s6_addr32[2] = (__force __be32)digest[0];
3381 temp.s6_addr32[3] = (__force __be32)digest[1];
3382
3383 spin_unlock_bh(&lock);
3384
3385 if (ipv6_reserved_interfaceid(temp)) {
3386 dad_count++;
3387 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3388 return -1;
3389 goto retry;
3390 }
3391
3392 *address = temp;
3393 return 0;
3394 }
3395
ipv6_gen_mode_random_init(struct inet6_dev * idev)3396 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3397 {
3398 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3399
3400 if (s->initialized)
3401 return;
3402 s = &idev->cnf.stable_secret;
3403 get_random_bytes(&s->secret, sizeof(s->secret));
3404 s->initialized = true;
3405 }
3406
addrconf_addr_gen(struct inet6_dev * idev,bool prefix_route)3407 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3408 {
3409 struct in6_addr addr;
3410
3411 /* no link local addresses on L3 master devices */
3412 if (netif_is_l3_master(idev->dev))
3413 return;
3414
3415 /* no link local addresses on devices flagged as slaves */
3416 if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
3417 return;
3418
3419 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3420
3421 switch (idev->cnf.addr_gen_mode) {
3422 case IN6_ADDR_GEN_MODE_RANDOM:
3423 ipv6_gen_mode_random_init(idev);
3424 fallthrough;
3425 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3426 if (!ipv6_generate_stable_address(&addr, 0, idev))
3427 addrconf_add_linklocal(idev, &addr,
3428 IFA_F_STABLE_PRIVACY);
3429 else if (prefix_route)
3430 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3431 0, 0, GFP_KERNEL);
3432 break;
3433 case IN6_ADDR_GEN_MODE_EUI64:
3434 /* addrconf_add_linklocal also adds a prefix_route and we
3435 * only need to care about prefix routes if ipv6_generate_eui64
3436 * couldn't generate one.
3437 */
3438 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3439 addrconf_add_linklocal(idev, &addr, 0);
3440 else if (prefix_route)
3441 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3442 0, 0, GFP_KERNEL);
3443 break;
3444 case IN6_ADDR_GEN_MODE_NONE:
3445 default:
3446 /* will not add any link local address */
3447 break;
3448 }
3449 }
3450
addrconf_dev_config(struct net_device * dev)3451 static void addrconf_dev_config(struct net_device *dev)
3452 {
3453 struct inet6_dev *idev;
3454
3455 ASSERT_RTNL();
3456
3457 if ((dev->type != ARPHRD_ETHER) &&
3458 (dev->type != ARPHRD_FDDI) &&
3459 (dev->type != ARPHRD_ARCNET) &&
3460 (dev->type != ARPHRD_INFINIBAND) &&
3461 (dev->type != ARPHRD_IEEE1394) &&
3462 (dev->type != ARPHRD_TUNNEL6) &&
3463 (dev->type != ARPHRD_6LOWPAN) &&
3464 (dev->type != ARPHRD_TUNNEL) &&
3465 (dev->type != ARPHRD_NONE) &&
3466 (dev->type != ARPHRD_RAWIP)) {
3467 /* Alas, we support only Ethernet autoconfiguration. */
3468 idev = __in6_dev_get(dev);
3469 if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3470 dev->flags & IFF_MULTICAST)
3471 ipv6_mc_up(idev);
3472 return;
3473 }
3474
3475 idev = addrconf_add_dev(dev);
3476 if (IS_ERR(idev))
3477 return;
3478
3479 /* this device type has no EUI support */
3480 if (dev->type == ARPHRD_NONE &&
3481 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3482 WRITE_ONCE(idev->cnf.addr_gen_mode,
3483 IN6_ADDR_GEN_MODE_RANDOM);
3484
3485 addrconf_addr_gen(idev, false);
3486 }
3487
3488 #if IS_ENABLED(CONFIG_IPV6_SIT)
addrconf_sit_config(struct net_device * dev)3489 static void addrconf_sit_config(struct net_device *dev)
3490 {
3491 struct inet6_dev *idev;
3492
3493 ASSERT_RTNL();
3494
3495 /*
3496 * Configure the tunnel with one of our IPv4
3497 * addresses... we should configure all of
3498 * our v4 addrs in the tunnel
3499 */
3500
3501 idev = ipv6_find_idev(dev);
3502 if (IS_ERR(idev)) {
3503 pr_debug("%s: add_dev failed\n", __func__);
3504 return;
3505 }
3506
3507 if (dev->priv_flags & IFF_ISATAP) {
3508 addrconf_addr_gen(idev, false);
3509 return;
3510 }
3511
3512 add_v4_addrs(idev);
3513
3514 if (dev->flags&IFF_POINTOPOINT)
3515 addrconf_add_mroute(dev);
3516 }
3517 #endif
3518
3519 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
addrconf_gre_config(struct net_device * dev)3520 static void addrconf_gre_config(struct net_device *dev)
3521 {
3522 struct inet6_dev *idev;
3523
3524 ASSERT_RTNL();
3525
3526 idev = ipv6_find_idev(dev);
3527 if (IS_ERR(idev)) {
3528 pr_debug("%s: add_dev failed\n", __func__);
3529 return;
3530 }
3531
3532 if (dev->type == ARPHRD_ETHER) {
3533 addrconf_addr_gen(idev, true);
3534 return;
3535 }
3536
3537 add_v4_addrs(idev);
3538
3539 if (dev->flags & IFF_POINTOPOINT)
3540 addrconf_add_mroute(dev);
3541 }
3542 #endif
3543
addrconf_init_auto_addrs(struct net_device * dev)3544 static void addrconf_init_auto_addrs(struct net_device *dev)
3545 {
3546 switch (dev->type) {
3547 #if IS_ENABLED(CONFIG_IPV6_SIT)
3548 case ARPHRD_SIT:
3549 addrconf_sit_config(dev);
3550 break;
3551 #endif
3552 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3553 case ARPHRD_IP6GRE:
3554 case ARPHRD_IPGRE:
3555 addrconf_gre_config(dev);
3556 break;
3557 #endif
3558 case ARPHRD_LOOPBACK:
3559 init_loopback(dev);
3560 break;
3561
3562 default:
3563 addrconf_dev_config(dev);
3564 break;
3565 }
3566 }
3567
fixup_permanent_addr(struct net * net,struct inet6_dev * idev,struct inet6_ifaddr * ifp)3568 static int fixup_permanent_addr(struct net *net,
3569 struct inet6_dev *idev,
3570 struct inet6_ifaddr *ifp)
3571 {
3572 /* !fib6_node means the host route was removed from the
3573 * FIB, for example, if 'lo' device is taken down. In that
3574 * case regenerate the host route.
3575 */
3576 if (!ifp->rt || !ifp->rt->fib6_node) {
3577 struct fib6_info *f6i, *prev;
3578
3579 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3580 GFP_ATOMIC, NULL);
3581 if (IS_ERR(f6i))
3582 return PTR_ERR(f6i);
3583
3584 /* ifp->rt can be accessed outside of rtnl */
3585 spin_lock(&ifp->lock);
3586 prev = ifp->rt;
3587 ifp->rt = f6i;
3588 spin_unlock(&ifp->lock);
3589
3590 fib6_info_release(prev);
3591 }
3592
3593 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3594 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3595 ifp->rt_priority, idev->dev, 0, 0,
3596 GFP_ATOMIC);
3597 }
3598
3599 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3600 addrconf_dad_start(ifp);
3601
3602 return 0;
3603 }
3604
addrconf_permanent_addr(struct net * net,struct net_device * dev)3605 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3606 {
3607 struct inet6_ifaddr *ifp, *tmp;
3608 struct inet6_dev *idev;
3609
3610 idev = __in6_dev_get(dev);
3611 if (!idev)
3612 return;
3613
3614 write_lock_bh(&idev->lock);
3615
3616 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3617 if ((ifp->flags & IFA_F_PERMANENT) &&
3618 fixup_permanent_addr(net, idev, ifp) < 0) {
3619 write_unlock_bh(&idev->lock);
3620 in6_ifa_hold(ifp);
3621 ipv6_del_addr(ifp);
3622 write_lock_bh(&idev->lock);
3623
3624 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3625 idev->dev->name, &ifp->addr);
3626 }
3627 }
3628
3629 write_unlock_bh(&idev->lock);
3630 }
3631
addrconf_notify(struct notifier_block * this,unsigned long event,void * ptr)3632 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3633 void *ptr)
3634 {
3635 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3636 struct netdev_notifier_change_info *change_info;
3637 struct netdev_notifier_changeupper_info *info;
3638 struct inet6_dev *idev = __in6_dev_get(dev);
3639 struct net *net = dev_net(dev);
3640 int run_pending = 0;
3641 int err;
3642
3643 switch (event) {
3644 case NETDEV_REGISTER:
3645 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3646 idev = ipv6_add_dev(dev);
3647 if (IS_ERR(idev))
3648 return notifier_from_errno(PTR_ERR(idev));
3649 }
3650 break;
3651
3652 case NETDEV_CHANGEMTU:
3653 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3654 if (dev->mtu < IPV6_MIN_MTU) {
3655 addrconf_ifdown(dev, dev != net->loopback_dev);
3656 break;
3657 }
3658
3659 if (idev) {
3660 rt6_mtu_change(dev, dev->mtu);
3661 WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
3662 break;
3663 }
3664
3665 /* allocate new idev */
3666 idev = ipv6_add_dev(dev);
3667 if (IS_ERR(idev))
3668 break;
3669
3670 /* device is still not ready */
3671 if (!(idev->if_flags & IF_READY))
3672 break;
3673
3674 run_pending = 1;
3675 fallthrough;
3676 case NETDEV_UP:
3677 case NETDEV_CHANGE:
3678 if (idev && idev->cnf.disable_ipv6)
3679 break;
3680
3681 if (dev->priv_flags & IFF_NO_ADDRCONF) {
3682 if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3683 dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3684 ipv6_mc_up(idev);
3685 break;
3686 }
3687
3688 if (event == NETDEV_UP) {
3689 /* restore routes for permanent addresses */
3690 addrconf_permanent_addr(net, dev);
3691
3692 if (!addrconf_link_ready(dev)) {
3693 /* device is not ready yet. */
3694 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3695 dev->name);
3696 break;
3697 }
3698
3699 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3700 idev = ipv6_add_dev(dev);
3701
3702 if (!IS_ERR_OR_NULL(idev)) {
3703 idev->if_flags |= IF_READY;
3704 run_pending = 1;
3705 }
3706 } else if (event == NETDEV_CHANGE) {
3707 if (!addrconf_link_ready(dev)) {
3708 /* device is still not ready. */
3709 rt6_sync_down_dev(dev, event);
3710 break;
3711 }
3712
3713 if (!IS_ERR_OR_NULL(idev)) {
3714 if (idev->if_flags & IF_READY) {
3715 /* device is already configured -
3716 * but resend MLD reports, we might
3717 * have roamed and need to update
3718 * multicast snooping switches
3719 */
3720 ipv6_mc_up(idev);
3721 change_info = ptr;
3722 if (change_info->flags_changed & IFF_NOARP)
3723 addrconf_dad_run(idev, true);
3724 rt6_sync_up(dev, RTNH_F_LINKDOWN);
3725 break;
3726 }
3727 idev->if_flags |= IF_READY;
3728 }
3729
3730 pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3731 dev->name);
3732
3733 run_pending = 1;
3734 }
3735
3736 addrconf_init_auto_addrs(dev);
3737
3738 if (!IS_ERR_OR_NULL(idev)) {
3739 if (run_pending)
3740 addrconf_dad_run(idev, false);
3741
3742 /* Device has an address by now */
3743 rt6_sync_up(dev, RTNH_F_DEAD);
3744
3745 /*
3746 * If the MTU changed during the interface down,
3747 * when the interface up, the changed MTU must be
3748 * reflected in the idev as well as routers.
3749 */
3750 if (idev->cnf.mtu6 != dev->mtu &&
3751 dev->mtu >= IPV6_MIN_MTU) {
3752 rt6_mtu_change(dev, dev->mtu);
3753 WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
3754 }
3755 WRITE_ONCE(idev->tstamp, jiffies);
3756 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3757
3758 /*
3759 * If the changed mtu during down is lower than
3760 * IPV6_MIN_MTU stop IPv6 on this interface.
3761 */
3762 if (dev->mtu < IPV6_MIN_MTU)
3763 addrconf_ifdown(dev, dev != net->loopback_dev);
3764 }
3765 break;
3766
3767 case NETDEV_DOWN:
3768 case NETDEV_UNREGISTER:
3769 /*
3770 * Remove all addresses from this interface.
3771 */
3772 addrconf_ifdown(dev, event != NETDEV_DOWN);
3773 break;
3774
3775 case NETDEV_CHANGENAME:
3776 if (idev) {
3777 snmp6_unregister_dev(idev);
3778 addrconf_sysctl_unregister(idev);
3779 err = addrconf_sysctl_register(idev);
3780 if (err)
3781 return notifier_from_errno(err);
3782 err = snmp6_register_dev(idev);
3783 if (err) {
3784 addrconf_sysctl_unregister(idev);
3785 return notifier_from_errno(err);
3786 }
3787 }
3788 break;
3789
3790 case NETDEV_PRE_TYPE_CHANGE:
3791 case NETDEV_POST_TYPE_CHANGE:
3792 if (idev)
3793 addrconf_type_change(dev, event);
3794 break;
3795
3796 case NETDEV_CHANGEUPPER:
3797 info = ptr;
3798
3799 /* flush all routes if dev is linked to or unlinked from
3800 * an L3 master device (e.g., VRF)
3801 */
3802 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3803 addrconf_ifdown(dev, false);
3804 }
3805
3806 return NOTIFY_OK;
3807 }
3808
3809 /*
3810 * addrconf module should be notified of a device going up
3811 */
3812 static struct notifier_block ipv6_dev_notf = {
3813 .notifier_call = addrconf_notify,
3814 .priority = ADDRCONF_NOTIFY_PRIORITY,
3815 };
3816
addrconf_type_change(struct net_device * dev,unsigned long event)3817 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3818 {
3819 struct inet6_dev *idev;
3820 ASSERT_RTNL();
3821
3822 idev = __in6_dev_get(dev);
3823
3824 if (event == NETDEV_POST_TYPE_CHANGE)
3825 ipv6_mc_remap(idev);
3826 else if (event == NETDEV_PRE_TYPE_CHANGE)
3827 ipv6_mc_unmap(idev);
3828 }
3829
addr_is_local(const struct in6_addr * addr)3830 static bool addr_is_local(const struct in6_addr *addr)
3831 {
3832 return ipv6_addr_type(addr) &
3833 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3834 }
3835
addrconf_ifdown(struct net_device * dev,bool unregister)3836 static int addrconf_ifdown(struct net_device *dev, bool unregister)
3837 {
3838 unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3839 struct net *net = dev_net(dev);
3840 struct inet6_dev *idev;
3841 struct inet6_ifaddr *ifa;
3842 LIST_HEAD(tmp_addr_list);
3843 bool keep_addr = false;
3844 bool was_ready;
3845 int state, i;
3846
3847 ASSERT_RTNL();
3848
3849 rt6_disable_ip(dev, event);
3850
3851 idev = __in6_dev_get(dev);
3852 if (!idev)
3853 return -ENODEV;
3854
3855 /*
3856 * Step 1: remove reference to ipv6 device from parent device.
3857 * Do not dev_put!
3858 */
3859 if (unregister) {
3860 idev->dead = 1;
3861
3862 /* protected by rtnl_lock */
3863 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3864
3865 /* Step 1.5: remove snmp6 entry */
3866 snmp6_unregister_dev(idev);
3867
3868 }
3869
3870 /* combine the user config with event to determine if permanent
3871 * addresses are to be removed from address hash table
3872 */
3873 if (!unregister && !idev->cnf.disable_ipv6) {
3874 /* aggregate the system setting and interface setting */
3875 int _keep_addr = READ_ONCE(net->ipv6.devconf_all->keep_addr_on_down);
3876
3877 if (!_keep_addr)
3878 _keep_addr = READ_ONCE(idev->cnf.keep_addr_on_down);
3879
3880 keep_addr = (_keep_addr > 0);
3881 }
3882
3883 /* Step 2: clear hash table */
3884 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3885 struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
3886
3887 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
3888 restart:
3889 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3890 if (ifa->idev == idev) {
3891 addrconf_del_dad_work(ifa);
3892 /* combined flag + permanent flag decide if
3893 * address is retained on a down event
3894 */
3895 if (!keep_addr ||
3896 !(ifa->flags & IFA_F_PERMANENT) ||
3897 addr_is_local(&ifa->addr)) {
3898 hlist_del_init_rcu(&ifa->addr_lst);
3899 goto restart;
3900 }
3901 }
3902 }
3903 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
3904 }
3905
3906 write_lock_bh(&idev->lock);
3907
3908 addrconf_del_rs_timer(idev);
3909
3910 /* Step 2: clear flags for stateless addrconf, repeated down
3911 * detection
3912 */
3913 was_ready = idev->if_flags & IF_READY;
3914 if (!unregister)
3915 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3916
3917 /* Step 3: clear tempaddr list */
3918 while (!list_empty(&idev->tempaddr_list)) {
3919 ifa = list_first_entry(&idev->tempaddr_list,
3920 struct inet6_ifaddr, tmp_list);
3921 list_del(&ifa->tmp_list);
3922 write_unlock_bh(&idev->lock);
3923 spin_lock_bh(&ifa->lock);
3924
3925 if (ifa->ifpub) {
3926 in6_ifa_put(ifa->ifpub);
3927 ifa->ifpub = NULL;
3928 }
3929 spin_unlock_bh(&ifa->lock);
3930 in6_ifa_put(ifa);
3931 write_lock_bh(&idev->lock);
3932 }
3933
3934 list_for_each_entry(ifa, &idev->addr_list, if_list)
3935 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
3936 write_unlock_bh(&idev->lock);
3937
3938 while (!list_empty(&tmp_addr_list)) {
3939 struct fib6_info *rt = NULL;
3940 bool keep;
3941
3942 ifa = list_first_entry(&tmp_addr_list,
3943 struct inet6_ifaddr, if_list_aux);
3944 list_del(&ifa->if_list_aux);
3945
3946 addrconf_del_dad_work(ifa);
3947
3948 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3949 !addr_is_local(&ifa->addr);
3950
3951 spin_lock_bh(&ifa->lock);
3952
3953 if (keep) {
3954 /* set state to skip the notifier below */
3955 state = INET6_IFADDR_STATE_DEAD;
3956 ifa->state = INET6_IFADDR_STATE_PREDAD;
3957 if (!(ifa->flags & IFA_F_NODAD))
3958 ifa->flags |= IFA_F_TENTATIVE;
3959
3960 rt = ifa->rt;
3961 ifa->rt = NULL;
3962 } else {
3963 state = ifa->state;
3964 ifa->state = INET6_IFADDR_STATE_DEAD;
3965 }
3966
3967 spin_unlock_bh(&ifa->lock);
3968
3969 if (rt)
3970 ip6_del_rt(net, rt, false);
3971
3972 if (state != INET6_IFADDR_STATE_DEAD) {
3973 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3974 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3975 } else {
3976 if (idev->cnf.forwarding)
3977 addrconf_leave_anycast(ifa);
3978 addrconf_leave_solict(ifa->idev, &ifa->addr);
3979 }
3980
3981 if (!keep) {
3982 write_lock_bh(&idev->lock);
3983 list_del_rcu(&ifa->if_list);
3984 write_unlock_bh(&idev->lock);
3985 in6_ifa_put(ifa);
3986 }
3987 }
3988
3989 /* Step 5: Discard anycast and multicast list */
3990 if (unregister) {
3991 ipv6_ac_destroy_dev(idev);
3992 ipv6_mc_destroy_dev(idev);
3993 } else if (was_ready) {
3994 ipv6_mc_down(idev);
3995 }
3996
3997 WRITE_ONCE(idev->tstamp, jiffies);
3998 idev->ra_mtu = 0;
3999
4000 /* Last: Shot the device (if unregistered) */
4001 if (unregister) {
4002 addrconf_sysctl_unregister(idev);
4003 neigh_parms_release(&nd_tbl, idev->nd_parms);
4004 neigh_ifdown(&nd_tbl, dev);
4005 in6_dev_put(idev);
4006 }
4007 return 0;
4008 }
4009
addrconf_rs_timer(struct timer_list * t)4010 static void addrconf_rs_timer(struct timer_list *t)
4011 {
4012 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
4013 struct net_device *dev = idev->dev;
4014 struct in6_addr lladdr;
4015 int rtr_solicits;
4016
4017 write_lock(&idev->lock);
4018 if (idev->dead || !(idev->if_flags & IF_READY))
4019 goto out;
4020
4021 if (!ipv6_accept_ra(idev))
4022 goto out;
4023
4024 /* Announcement received after solicitation was sent */
4025 if (idev->if_flags & IF_RA_RCVD)
4026 goto out;
4027
4028 rtr_solicits = READ_ONCE(idev->cnf.rtr_solicits);
4029
4030 if (idev->rs_probes++ < rtr_solicits || rtr_solicits < 0) {
4031 write_unlock(&idev->lock);
4032 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4033 ndisc_send_rs(dev, &lladdr,
4034 &in6addr_linklocal_allrouters);
4035 else
4036 goto put;
4037
4038 write_lock(&idev->lock);
4039 idev->rs_interval = rfc3315_s14_backoff_update(
4040 idev->rs_interval,
4041 READ_ONCE(idev->cnf.rtr_solicit_max_interval));
4042 /* The wait after the last probe can be shorter */
4043 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
4044 READ_ONCE(idev->cnf.rtr_solicits)) ?
4045 READ_ONCE(idev->cnf.rtr_solicit_delay) :
4046 idev->rs_interval);
4047 } else {
4048 /*
4049 * Note: we do not support deprecated "all on-link"
4050 * assumption any longer.
4051 */
4052 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
4053 }
4054
4055 out:
4056 write_unlock(&idev->lock);
4057 put:
4058 in6_dev_put(idev);
4059 }
4060
4061 /*
4062 * Duplicate Address Detection
4063 */
addrconf_dad_kick(struct inet6_ifaddr * ifp)4064 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
4065 {
4066 struct inet6_dev *idev = ifp->idev;
4067 unsigned long rand_num;
4068 u64 nonce;
4069
4070 if (ifp->flags & IFA_F_OPTIMISTIC)
4071 rand_num = 0;
4072 else
4073 rand_num = get_random_u32_below(
4074 READ_ONCE(idev->cnf.rtr_solicit_delay) ? : 1);
4075
4076 nonce = 0;
4077 if (READ_ONCE(idev->cnf.enhanced_dad) ||
4078 READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad)) {
4079 do
4080 get_random_bytes(&nonce, 6);
4081 while (nonce == 0);
4082 }
4083 ifp->dad_nonce = nonce;
4084 ifp->dad_probes = READ_ONCE(idev->cnf.dad_transmits);
4085 addrconf_mod_dad_work(ifp, rand_num);
4086 }
4087
addrconf_dad_begin(struct inet6_ifaddr * ifp)4088 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
4089 {
4090 struct inet6_dev *idev = ifp->idev;
4091 struct net_device *dev = idev->dev;
4092 bool bump_id, notify = false;
4093 struct net *net;
4094
4095 addrconf_join_solict(dev, &ifp->addr);
4096
4097 read_lock_bh(&idev->lock);
4098 spin_lock(&ifp->lock);
4099 if (ifp->state == INET6_IFADDR_STATE_DEAD)
4100 goto out;
4101
4102 net = dev_net(dev);
4103 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4104 (READ_ONCE(net->ipv6.devconf_all->accept_dad) < 1 &&
4105 READ_ONCE(idev->cnf.accept_dad) < 1) ||
4106 !(ifp->flags&IFA_F_TENTATIVE) ||
4107 ifp->flags & IFA_F_NODAD) {
4108 bool send_na = false;
4109
4110 if (ifp->flags & IFA_F_TENTATIVE &&
4111 !(ifp->flags & IFA_F_OPTIMISTIC))
4112 send_na = true;
4113 bump_id = ifp->flags & IFA_F_TENTATIVE;
4114 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4115 spin_unlock(&ifp->lock);
4116 read_unlock_bh(&idev->lock);
4117
4118 addrconf_dad_completed(ifp, bump_id, send_na);
4119 return;
4120 }
4121
4122 if (!(idev->if_flags & IF_READY)) {
4123 spin_unlock(&ifp->lock);
4124 read_unlock_bh(&idev->lock);
4125 /*
4126 * If the device is not ready:
4127 * - keep it tentative if it is a permanent address.
4128 * - otherwise, kill it.
4129 */
4130 in6_ifa_hold(ifp);
4131 addrconf_dad_stop(ifp, 0);
4132 return;
4133 }
4134
4135 /*
4136 * Optimistic nodes can start receiving
4137 * Frames right away
4138 */
4139 if (ifp->flags & IFA_F_OPTIMISTIC) {
4140 ip6_ins_rt(net, ifp->rt);
4141 if (ipv6_use_optimistic_addr(net, idev)) {
4142 /* Because optimistic nodes can use this address,
4143 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4144 */
4145 notify = true;
4146 }
4147 }
4148
4149 addrconf_dad_kick(ifp);
4150 out:
4151 spin_unlock(&ifp->lock);
4152 read_unlock_bh(&idev->lock);
4153 if (notify)
4154 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4155 }
4156
addrconf_dad_start(struct inet6_ifaddr * ifp)4157 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4158 {
4159 bool begin_dad = false;
4160
4161 spin_lock_bh(&ifp->lock);
4162 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4163 ifp->state = INET6_IFADDR_STATE_PREDAD;
4164 begin_dad = true;
4165 }
4166 spin_unlock_bh(&ifp->lock);
4167
4168 if (begin_dad)
4169 addrconf_mod_dad_work(ifp, 0);
4170 }
4171
addrconf_dad_work(struct work_struct * w)4172 static void addrconf_dad_work(struct work_struct *w)
4173 {
4174 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4175 struct inet6_ifaddr,
4176 dad_work);
4177 struct inet6_dev *idev = ifp->idev;
4178 bool bump_id, disable_ipv6 = false;
4179 struct in6_addr mcaddr;
4180 struct net *net;
4181
4182 enum {
4183 DAD_PROCESS,
4184 DAD_BEGIN,
4185 DAD_ABORT,
4186 } action = DAD_PROCESS;
4187
4188 net = dev_net(idev->dev);
4189
4190 rtnl_net_lock(net);
4191
4192 spin_lock_bh(&ifp->lock);
4193 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4194 action = DAD_BEGIN;
4195 ifp->state = INET6_IFADDR_STATE_DAD;
4196 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4197 action = DAD_ABORT;
4198 ifp->state = INET6_IFADDR_STATE_POSTDAD;
4199
4200 if ((READ_ONCE(net->ipv6.devconf_all->accept_dad) > 1 ||
4201 READ_ONCE(idev->cnf.accept_dad) > 1) &&
4202 !idev->cnf.disable_ipv6 &&
4203 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4204 struct in6_addr addr;
4205
4206 addr.s6_addr32[0] = htonl(0xfe800000);
4207 addr.s6_addr32[1] = 0;
4208
4209 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4210 ipv6_addr_equal(&ifp->addr, &addr)) {
4211 /* DAD failed for link-local based on MAC */
4212 WRITE_ONCE(idev->cnf.disable_ipv6, 1);
4213
4214 pr_info("%s: IPv6 being disabled!\n",
4215 ifp->idev->dev->name);
4216 disable_ipv6 = true;
4217 }
4218 }
4219 }
4220 spin_unlock_bh(&ifp->lock);
4221
4222 if (action == DAD_BEGIN) {
4223 addrconf_dad_begin(ifp);
4224 goto out;
4225 } else if (action == DAD_ABORT) {
4226 in6_ifa_hold(ifp);
4227 addrconf_dad_stop(ifp, 1);
4228 if (disable_ipv6)
4229 addrconf_ifdown(idev->dev, false);
4230 goto out;
4231 }
4232
4233 if (!ifp->dad_probes && addrconf_dad_end(ifp))
4234 goto out;
4235
4236 write_lock_bh(&idev->lock);
4237 if (idev->dead || !(idev->if_flags & IF_READY)) {
4238 write_unlock_bh(&idev->lock);
4239 goto out;
4240 }
4241
4242 spin_lock(&ifp->lock);
4243 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4244 spin_unlock(&ifp->lock);
4245 write_unlock_bh(&idev->lock);
4246 goto out;
4247 }
4248
4249 if (ifp->dad_probes == 0) {
4250 bool send_na = false;
4251
4252 /*
4253 * DAD was successful
4254 */
4255
4256 if (ifp->flags & IFA_F_TENTATIVE &&
4257 !(ifp->flags & IFA_F_OPTIMISTIC))
4258 send_na = true;
4259 bump_id = ifp->flags & IFA_F_TENTATIVE;
4260 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4261 spin_unlock(&ifp->lock);
4262 write_unlock_bh(&idev->lock);
4263
4264 addrconf_dad_completed(ifp, bump_id, send_na);
4265
4266 goto out;
4267 }
4268
4269 ifp->dad_probes--;
4270 addrconf_mod_dad_work(ifp,
4271 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
4272 HZ/100));
4273 spin_unlock(&ifp->lock);
4274 write_unlock_bh(&idev->lock);
4275
4276 /* send a neighbour solicitation for our addr */
4277 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4278 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4279 ifp->dad_nonce);
4280 out:
4281 in6_ifa_put(ifp);
4282 rtnl_net_unlock(net);
4283 }
4284
4285 /* ifp->idev must be at least read locked */
ipv6_lonely_lladdr(struct inet6_ifaddr * ifp)4286 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4287 {
4288 struct inet6_ifaddr *ifpiter;
4289 struct inet6_dev *idev = ifp->idev;
4290
4291 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4292 if (ifpiter->scope > IFA_LINK)
4293 break;
4294 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4295 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4296 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4297 IFA_F_PERMANENT)
4298 return false;
4299 }
4300 return true;
4301 }
4302
addrconf_dad_completed(struct inet6_ifaddr * ifp,bool bump_id,bool send_na)4303 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4304 bool send_na)
4305 {
4306 struct net_device *dev = ifp->idev->dev;
4307 struct in6_addr lladdr;
4308 bool send_rs, send_mld;
4309
4310 addrconf_del_dad_work(ifp);
4311
4312 /*
4313 * Configure the address for reception. Now it is valid.
4314 */
4315
4316 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4317
4318 /* If added prefix is link local and we are prepared to process
4319 router advertisements, start sending router solicitations.
4320 */
4321
4322 read_lock_bh(&ifp->idev->lock);
4323 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4324 send_rs = send_mld &&
4325 ipv6_accept_ra(ifp->idev) &&
4326 READ_ONCE(ifp->idev->cnf.rtr_solicits) != 0 &&
4327 (dev->flags & IFF_LOOPBACK) == 0 &&
4328 (dev->type != ARPHRD_TUNNEL) &&
4329 !netif_is_team_port(dev);
4330 read_unlock_bh(&ifp->idev->lock);
4331
4332 /* While dad is in progress mld report's source address is in6_addrany.
4333 * Resend with proper ll now.
4334 */
4335 if (send_mld)
4336 ipv6_mc_dad_complete(ifp->idev);
4337
4338 /* send unsolicited NA if enabled */
4339 if (send_na &&
4340 (READ_ONCE(ifp->idev->cnf.ndisc_notify) ||
4341 READ_ONCE(dev_net(dev)->ipv6.devconf_all->ndisc_notify))) {
4342 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4343 /*router=*/ !!ifp->idev->cnf.forwarding,
4344 /*solicited=*/ false, /*override=*/ true,
4345 /*inc_opt=*/ true);
4346 }
4347
4348 if (send_rs) {
4349 /*
4350 * If a host as already performed a random delay
4351 * [...] as part of DAD [...] there is no need
4352 * to delay again before sending the first RS
4353 */
4354 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4355 return;
4356 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4357
4358 write_lock_bh(&ifp->idev->lock);
4359 spin_lock(&ifp->lock);
4360 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4361 READ_ONCE(ifp->idev->cnf.rtr_solicit_interval));
4362 ifp->idev->rs_probes = 1;
4363 ifp->idev->if_flags |= IF_RS_SENT;
4364 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4365 spin_unlock(&ifp->lock);
4366 write_unlock_bh(&ifp->idev->lock);
4367 }
4368
4369 if (bump_id)
4370 rt_genid_bump_ipv6(dev_net(dev));
4371
4372 /* Make sure that a new temporary address will be created
4373 * before this temporary address becomes deprecated.
4374 */
4375 if (ifp->flags & IFA_F_TEMPORARY)
4376 addrconf_verify_rtnl(dev_net(dev));
4377 }
4378
addrconf_dad_run(struct inet6_dev * idev,bool restart)4379 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4380 {
4381 struct inet6_ifaddr *ifp;
4382
4383 read_lock_bh(&idev->lock);
4384 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4385 spin_lock(&ifp->lock);
4386 if ((ifp->flags & IFA_F_TENTATIVE &&
4387 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4388 if (restart)
4389 ifp->state = INET6_IFADDR_STATE_PREDAD;
4390 addrconf_dad_kick(ifp);
4391 }
4392 spin_unlock(&ifp->lock);
4393 }
4394 read_unlock_bh(&idev->lock);
4395 }
4396
4397 #ifdef CONFIG_PROC_FS
4398 struct if6_iter_state {
4399 struct seq_net_private p;
4400 int bucket;
4401 int offset;
4402 };
4403
if6_get_first(struct seq_file * seq,loff_t pos)4404 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4405 {
4406 struct if6_iter_state *state = seq->private;
4407 struct net *net = seq_file_net(seq);
4408 struct inet6_ifaddr *ifa = NULL;
4409 int p = 0;
4410
4411 /* initial bucket if pos is 0 */
4412 if (pos == 0) {
4413 state->bucket = 0;
4414 state->offset = 0;
4415 }
4416
4417 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4418 hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
4419 addr_lst) {
4420 /* sync with offset */
4421 if (p < state->offset) {
4422 p++;
4423 continue;
4424 }
4425 return ifa;
4426 }
4427
4428 /* prepare for next bucket */
4429 state->offset = 0;
4430 p = 0;
4431 }
4432 return NULL;
4433 }
4434
if6_get_next(struct seq_file * seq,struct inet6_ifaddr * ifa)4435 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4436 struct inet6_ifaddr *ifa)
4437 {
4438 struct if6_iter_state *state = seq->private;
4439 struct net *net = seq_file_net(seq);
4440
4441 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4442 state->offset++;
4443 return ifa;
4444 }
4445
4446 state->offset = 0;
4447 while (++state->bucket < IN6_ADDR_HSIZE) {
4448 hlist_for_each_entry_rcu(ifa,
4449 &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4450 return ifa;
4451 }
4452 }
4453
4454 return NULL;
4455 }
4456
if6_seq_start(struct seq_file * seq,loff_t * pos)4457 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4458 __acquires(rcu)
4459 {
4460 rcu_read_lock();
4461 return if6_get_first(seq, *pos);
4462 }
4463
if6_seq_next(struct seq_file * seq,void * v,loff_t * pos)4464 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4465 {
4466 struct inet6_ifaddr *ifa;
4467
4468 ifa = if6_get_next(seq, v);
4469 ++*pos;
4470 return ifa;
4471 }
4472
if6_seq_stop(struct seq_file * seq,void * v)4473 static void if6_seq_stop(struct seq_file *seq, void *v)
4474 __releases(rcu)
4475 {
4476 rcu_read_unlock();
4477 }
4478
if6_seq_show(struct seq_file * seq,void * v)4479 static int if6_seq_show(struct seq_file *seq, void *v)
4480 {
4481 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4482 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4483 &ifp->addr,
4484 ifp->idev->dev->ifindex,
4485 ifp->prefix_len,
4486 ifp->scope,
4487 (u8) ifp->flags,
4488 ifp->idev->dev->name);
4489 return 0;
4490 }
4491
4492 static const struct seq_operations if6_seq_ops = {
4493 .start = if6_seq_start,
4494 .next = if6_seq_next,
4495 .show = if6_seq_show,
4496 .stop = if6_seq_stop,
4497 };
4498
if6_proc_net_init(struct net * net)4499 static int __net_init if6_proc_net_init(struct net *net)
4500 {
4501 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4502 sizeof(struct if6_iter_state)))
4503 return -ENOMEM;
4504 return 0;
4505 }
4506
if6_proc_net_exit(struct net * net)4507 static void __net_exit if6_proc_net_exit(struct net *net)
4508 {
4509 remove_proc_entry("if_inet6", net->proc_net);
4510 }
4511
4512 static struct pernet_operations if6_proc_net_ops = {
4513 .init = if6_proc_net_init,
4514 .exit = if6_proc_net_exit,
4515 };
4516
if6_proc_init(void)4517 int __init if6_proc_init(void)
4518 {
4519 return register_pernet_subsys(&if6_proc_net_ops);
4520 }
4521
if6_proc_exit(void)4522 void if6_proc_exit(void)
4523 {
4524 unregister_pernet_subsys(&if6_proc_net_ops);
4525 }
4526 #endif /* CONFIG_PROC_FS */
4527
4528 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4529 /* Check if address is a home address configured on any interface. */
ipv6_chk_home_addr(struct net * net,const struct in6_addr * addr)4530 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4531 {
4532 unsigned int hash = inet6_addr_hash(net, addr);
4533 struct inet6_ifaddr *ifp = NULL;
4534 int ret = 0;
4535
4536 rcu_read_lock();
4537 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4538 if (ipv6_addr_equal(&ifp->addr, addr) &&
4539 (ifp->flags & IFA_F_HOMEADDRESS)) {
4540 ret = 1;
4541 break;
4542 }
4543 }
4544 rcu_read_unlock();
4545 return ret;
4546 }
4547 #endif
4548
4549 /* RFC6554 has some algorithm to avoid loops in segment routing by
4550 * checking if the segments contains any of a local interface address.
4551 *
4552 * Quote:
4553 *
4554 * To detect loops in the SRH, a router MUST determine if the SRH
4555 * includes multiple addresses assigned to any interface on that router.
4556 * If such addresses appear more than once and are separated by at least
4557 * one address not assigned to that router.
4558 */
ipv6_chk_rpl_srh_loop(struct net * net,const struct in6_addr * segs,unsigned char nsegs)4559 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4560 unsigned char nsegs)
4561 {
4562 const struct in6_addr *addr;
4563 int i, ret = 0, found = 0;
4564 struct inet6_ifaddr *ifp;
4565 bool separated = false;
4566 unsigned int hash;
4567 bool hash_found;
4568
4569 rcu_read_lock();
4570 for (i = 0; i < nsegs; i++) {
4571 addr = &segs[i];
4572 hash = inet6_addr_hash(net, addr);
4573
4574 hash_found = false;
4575 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4576
4577 if (ipv6_addr_equal(&ifp->addr, addr)) {
4578 hash_found = true;
4579 break;
4580 }
4581 }
4582
4583 if (hash_found) {
4584 if (found > 1 && separated) {
4585 ret = 1;
4586 break;
4587 }
4588
4589 separated = false;
4590 found++;
4591 } else {
4592 separated = true;
4593 }
4594 }
4595 rcu_read_unlock();
4596
4597 return ret;
4598 }
4599
4600 /*
4601 * Periodic address status verification
4602 */
4603
addrconf_verify_rtnl(struct net * net)4604 static void addrconf_verify_rtnl(struct net *net)
4605 {
4606 unsigned long now, next, next_sec, next_sched;
4607 struct inet6_ifaddr *ifp;
4608 int i;
4609
4610 ASSERT_RTNL();
4611
4612 rcu_read_lock_bh();
4613 now = jiffies;
4614 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4615
4616 cancel_delayed_work(&net->ipv6.addr_chk_work);
4617
4618 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4619 restart:
4620 hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
4621 unsigned long age;
4622
4623 /* When setting preferred_lft to a value not zero or
4624 * infinity, while valid_lft is infinity
4625 * IFA_F_PERMANENT has a non-infinity life time.
4626 */
4627 if ((ifp->flags & IFA_F_PERMANENT) &&
4628 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4629 continue;
4630
4631 spin_lock(&ifp->lock);
4632 /* We try to batch several events at once. */
4633 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4634
4635 if ((ifp->flags&IFA_F_TEMPORARY) &&
4636 !(ifp->flags&IFA_F_TENTATIVE) &&
4637 ifp->prefered_lft != INFINITY_LIFE_TIME &&
4638 !ifp->regen_count && ifp->ifpub) {
4639 /* This is a non-regenerated temporary addr. */
4640
4641 unsigned long regen_advance = ipv6_get_regen_advance(ifp->idev);
4642
4643 if (age + regen_advance >= ifp->prefered_lft) {
4644 struct inet6_ifaddr *ifpub = ifp->ifpub;
4645 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4646 next = ifp->tstamp + ifp->prefered_lft * HZ;
4647
4648 ifp->regen_count++;
4649 in6_ifa_hold(ifp);
4650 in6_ifa_hold(ifpub);
4651 spin_unlock(&ifp->lock);
4652
4653 spin_lock(&ifpub->lock);
4654 ifpub->regen_count = 0;
4655 spin_unlock(&ifpub->lock);
4656 rcu_read_unlock_bh();
4657 ipv6_create_tempaddr(ifpub, true);
4658 in6_ifa_put(ifpub);
4659 in6_ifa_put(ifp);
4660 rcu_read_lock_bh();
4661 goto restart;
4662 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4663 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4664 }
4665
4666 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4667 age >= ifp->valid_lft) {
4668 spin_unlock(&ifp->lock);
4669 in6_ifa_hold(ifp);
4670 rcu_read_unlock_bh();
4671 ipv6_del_addr(ifp);
4672 rcu_read_lock_bh();
4673 goto restart;
4674 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4675 spin_unlock(&ifp->lock);
4676 continue;
4677 } else if (age >= ifp->prefered_lft) {
4678 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4679 int deprecate = 0;
4680
4681 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4682 deprecate = 1;
4683 ifp->flags |= IFA_F_DEPRECATED;
4684 }
4685
4686 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4687 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4688 next = ifp->tstamp + ifp->valid_lft * HZ;
4689
4690 spin_unlock(&ifp->lock);
4691
4692 if (deprecate) {
4693 in6_ifa_hold(ifp);
4694
4695 ipv6_ifa_notify(0, ifp);
4696 in6_ifa_put(ifp);
4697 goto restart;
4698 }
4699 } else {
4700 /* ifp->prefered_lft <= ifp->valid_lft */
4701 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4702 next = ifp->tstamp + ifp->prefered_lft * HZ;
4703 spin_unlock(&ifp->lock);
4704 }
4705 }
4706 }
4707
4708 next_sec = round_jiffies_up(next);
4709 next_sched = next;
4710
4711 /* If rounded timeout is accurate enough, accept it. */
4712 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4713 next_sched = next_sec;
4714
4715 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4716 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4717 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4718
4719 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4720 now, next, next_sec, next_sched);
4721 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
4722 rcu_read_unlock_bh();
4723 }
4724
addrconf_verify_work(struct work_struct * w)4725 static void addrconf_verify_work(struct work_struct *w)
4726 {
4727 struct net *net = container_of(to_delayed_work(w), struct net,
4728 ipv6.addr_chk_work);
4729
4730 rtnl_net_lock(net);
4731 addrconf_verify_rtnl(net);
4732 rtnl_net_unlock(net);
4733 }
4734
addrconf_verify(struct net * net)4735 static void addrconf_verify(struct net *net)
4736 {
4737 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4738 }
4739
extract_addr(struct nlattr * addr,struct nlattr * local,struct in6_addr ** peer_pfx)4740 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4741 struct in6_addr **peer_pfx)
4742 {
4743 struct in6_addr *pfx = NULL;
4744
4745 *peer_pfx = NULL;
4746
4747 if (addr)
4748 pfx = nla_data(addr);
4749
4750 if (local) {
4751 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4752 *peer_pfx = pfx;
4753 pfx = nla_data(local);
4754 }
4755
4756 return pfx;
4757 }
4758
4759 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4760 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4761 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4762 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4763 [IFA_FLAGS] = { .len = sizeof(u32) },
4764 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
4765 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
4766 [IFA_PROTO] = { .type = NLA_U8 },
4767 };
4768
4769 static int
inet6_rtm_deladdr(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4770 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4771 struct netlink_ext_ack *extack)
4772 {
4773 struct net *net = sock_net(skb->sk);
4774 struct ifaddrmsg *ifm;
4775 struct nlattr *tb[IFA_MAX+1];
4776 struct in6_addr *pfx, *peer_pfx;
4777 u32 ifa_flags;
4778 int err;
4779
4780 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4781 ifa_ipv6_policy, extack);
4782 if (err < 0)
4783 return err;
4784
4785 ifm = nlmsg_data(nlh);
4786 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4787 if (!pfx)
4788 return -EINVAL;
4789
4790 ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
4791
4792 /* We ignore other flags so far. */
4793 ifa_flags &= IFA_F_MANAGETEMPADDR;
4794
4795 rtnl_net_lock(net);
4796 err = inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4797 ifm->ifa_prefixlen, extack);
4798 rtnl_net_unlock(net);
4799
4800 return err;
4801 }
4802
modify_prefix_route(struct net * net,struct inet6_ifaddr * ifp,unsigned long expires,u32 flags,bool modify_peer)4803 static int modify_prefix_route(struct net *net, struct inet6_ifaddr *ifp,
4804 unsigned long expires, u32 flags,
4805 bool modify_peer)
4806 {
4807 struct fib6_table *table;
4808 struct fib6_info *f6i;
4809 u32 prio;
4810
4811 f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4812 ifp->prefix_len,
4813 ifp->idev->dev, 0, RTF_DEFAULT, true);
4814 if (!f6i)
4815 return -ENOENT;
4816
4817 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4818 if (f6i->fib6_metric != prio) {
4819 /* delete old one */
4820 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4821
4822 /* add new one */
4823 addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4824 ifp->prefix_len,
4825 ifp->rt_priority, ifp->idev->dev,
4826 expires, flags, GFP_KERNEL);
4827 return 0;
4828 }
4829 if (f6i != net->ipv6.fib6_null_entry) {
4830 table = f6i->fib6_table;
4831 spin_lock_bh(&table->tb6_lock);
4832
4833 if (!(flags & RTF_EXPIRES)) {
4834 fib6_clean_expires(f6i);
4835 fib6_remove_gc_list(f6i);
4836 } else {
4837 fib6_set_expires(f6i, expires);
4838 fib6_add_gc_list(f6i);
4839 }
4840
4841 spin_unlock_bh(&table->tb6_lock);
4842 }
4843 fib6_info_release(f6i);
4844
4845 return 0;
4846 }
4847
inet6_addr_modify(struct net * net,struct inet6_ifaddr * ifp,struct ifa6_config * cfg,clock_t expires,u32 flags)4848 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
4849 struct ifa6_config *cfg, clock_t expires,
4850 u32 flags)
4851 {
4852 bool was_managetempaddr;
4853 bool new_peer = false;
4854 bool had_prefixroute;
4855
4856 ASSERT_RTNL_NET(net);
4857
4858 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4859 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4860 return -EINVAL;
4861
4862 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4863 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4864
4865 if (cfg->peer_pfx &&
4866 memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4867 if (!ipv6_addr_any(&ifp->peer_addr))
4868 cleanup_prefix_route(ifp, expires, true, true);
4869 new_peer = true;
4870 }
4871
4872 spin_lock_bh(&ifp->lock);
4873 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4874 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4875 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4876 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4877 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4878 IFA_F_NOPREFIXROUTE);
4879 ifp->flags |= cfg->ifa_flags;
4880 WRITE_ONCE(ifp->tstamp, jiffies);
4881 WRITE_ONCE(ifp->valid_lft, cfg->valid_lft);
4882 WRITE_ONCE(ifp->prefered_lft, cfg->preferred_lft);
4883 WRITE_ONCE(ifp->ifa_proto, cfg->ifa_proto);
4884
4885 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4886 WRITE_ONCE(ifp->rt_priority, cfg->rt_priority);
4887
4888 if (new_peer)
4889 ifp->peer_addr = *cfg->peer_pfx;
4890
4891 spin_unlock_bh(&ifp->lock);
4892 if (!(ifp->flags&IFA_F_TENTATIVE))
4893 ipv6_ifa_notify(0, ifp);
4894
4895 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4896 int rc = -ENOENT;
4897
4898 if (had_prefixroute)
4899 rc = modify_prefix_route(net, ifp, expires, flags, false);
4900
4901 /* prefix route could have been deleted; if so restore it */
4902 if (rc == -ENOENT) {
4903 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4904 ifp->rt_priority, ifp->idev->dev,
4905 expires, flags, GFP_KERNEL);
4906 }
4907
4908 if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4909 rc = modify_prefix_route(net, ifp, expires, flags, true);
4910
4911 if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4912 addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4913 ifp->rt_priority, ifp->idev->dev,
4914 expires, flags, GFP_KERNEL);
4915 }
4916 } else if (had_prefixroute) {
4917 enum cleanup_prefix_rt_t action;
4918 unsigned long rt_expires;
4919
4920 write_lock_bh(&ifp->idev->lock);
4921 action = check_cleanup_prefix_route(ifp, &rt_expires);
4922 write_unlock_bh(&ifp->idev->lock);
4923
4924 if (action != CLEANUP_PREFIX_RT_NOP) {
4925 cleanup_prefix_route(ifp, rt_expires,
4926 action == CLEANUP_PREFIX_RT_DEL, false);
4927 }
4928 }
4929
4930 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4931 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4932 delete_tempaddrs(ifp->idev, ifp);
4933 else
4934 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4935 cfg->preferred_lft, !was_managetempaddr,
4936 jiffies);
4937 }
4938
4939 addrconf_verify_rtnl(net);
4940
4941 return 0;
4942 }
4943
4944 static int
inet6_rtm_newaddr(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4945 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4946 struct netlink_ext_ack *extack)
4947 {
4948 struct net *net = sock_net(skb->sk);
4949 struct nlattr *tb[IFA_MAX+1];
4950 struct in6_addr *peer_pfx;
4951 struct inet6_ifaddr *ifa;
4952 struct net_device *dev;
4953 struct inet6_dev *idev;
4954 struct ifa6_config cfg;
4955 struct ifaddrmsg *ifm;
4956 unsigned long timeout;
4957 clock_t expires;
4958 u32 flags;
4959 int err;
4960
4961 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4962 ifa_ipv6_policy, extack);
4963 if (err < 0)
4964 return err;
4965
4966 memset(&cfg, 0, sizeof(cfg));
4967
4968 ifm = nlmsg_data(nlh);
4969 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4970 if (!cfg.pfx)
4971 return -EINVAL;
4972
4973 cfg.peer_pfx = peer_pfx;
4974 cfg.plen = ifm->ifa_prefixlen;
4975 if (tb[IFA_RT_PRIORITY])
4976 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4977
4978 if (tb[IFA_PROTO])
4979 cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
4980
4981 cfg.ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
4982
4983 /* We ignore other flags so far. */
4984 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4985 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4986 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4987
4988 cfg.ifa_flags |= IFA_F_PERMANENT;
4989 cfg.valid_lft = INFINITY_LIFE_TIME;
4990 cfg.preferred_lft = INFINITY_LIFE_TIME;
4991 expires = 0;
4992 flags = 0;
4993
4994 if (tb[IFA_CACHEINFO]) {
4995 struct ifa_cacheinfo *ci;
4996
4997 ci = nla_data(tb[IFA_CACHEINFO]);
4998 cfg.valid_lft = ci->ifa_valid;
4999 cfg.preferred_lft = ci->ifa_prefered;
5000
5001 if (!cfg.valid_lft || cfg.preferred_lft > cfg.valid_lft) {
5002 NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
5003 return -EINVAL;
5004 }
5005
5006 timeout = addrconf_timeout_fixup(cfg.valid_lft, HZ);
5007 if (addrconf_finite_timeout(timeout)) {
5008 cfg.ifa_flags &= ~IFA_F_PERMANENT;
5009 cfg.valid_lft = timeout;
5010 expires = jiffies_to_clock_t(timeout * HZ);
5011 flags = RTF_EXPIRES;
5012 }
5013
5014 timeout = addrconf_timeout_fixup(cfg.preferred_lft, HZ);
5015 if (addrconf_finite_timeout(timeout)) {
5016 if (timeout == 0)
5017 cfg.ifa_flags |= IFA_F_DEPRECATED;
5018
5019 cfg.preferred_lft = timeout;
5020 }
5021 }
5022
5023 rtnl_net_lock(net);
5024
5025 dev = __dev_get_by_index(net, ifm->ifa_index);
5026 if (!dev) {
5027 NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
5028 err = -ENODEV;
5029 goto unlock;
5030 }
5031
5032 idev = ipv6_find_idev(dev);
5033 if (IS_ERR(idev)) {
5034 err = PTR_ERR(idev);
5035 goto unlock;
5036 }
5037
5038 if (!ipv6_allow_optimistic_dad(net, idev))
5039 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
5040
5041 if (cfg.ifa_flags & IFA_F_NODAD &&
5042 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
5043 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
5044 err = -EINVAL;
5045 goto unlock;
5046 }
5047
5048 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
5049 if (!ifa) {
5050 /*
5051 * It would be best to check for !NLM_F_CREATE here but
5052 * userspace already relies on not having to provide this.
5053 */
5054 err = inet6_addr_add(net, dev, &cfg, expires, flags, extack);
5055 goto unlock;
5056 }
5057
5058 if (nlh->nlmsg_flags & NLM_F_EXCL ||
5059 !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
5060 NL_SET_ERR_MSG_MOD(extack, "address already assigned");
5061 err = -EEXIST;
5062 } else {
5063 err = inet6_addr_modify(net, ifa, &cfg, expires, flags);
5064 }
5065
5066 in6_ifa_put(ifa);
5067 unlock:
5068 rtnl_net_unlock(net);
5069
5070 return err;
5071 }
5072
put_ifaddrmsg(struct nlmsghdr * nlh,u8 prefixlen,u32 flags,u8 scope,int ifindex)5073 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
5074 u8 scope, int ifindex)
5075 {
5076 struct ifaddrmsg *ifm;
5077
5078 ifm = nlmsg_data(nlh);
5079 ifm->ifa_family = AF_INET6;
5080 ifm->ifa_prefixlen = prefixlen;
5081 ifm->ifa_flags = flags;
5082 ifm->ifa_scope = scope;
5083 ifm->ifa_index = ifindex;
5084 }
5085
put_cacheinfo(struct sk_buff * skb,unsigned long cstamp,unsigned long tstamp,u32 preferred,u32 valid)5086 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
5087 unsigned long tstamp, u32 preferred, u32 valid)
5088 {
5089 struct ifa_cacheinfo ci;
5090
5091 ci.cstamp = cstamp_delta(cstamp);
5092 ci.tstamp = cstamp_delta(tstamp);
5093 ci.ifa_prefered = preferred;
5094 ci.ifa_valid = valid;
5095
5096 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
5097 }
5098
rt_scope(int ifa_scope)5099 static inline int rt_scope(int ifa_scope)
5100 {
5101 if (ifa_scope & IFA_HOST)
5102 return RT_SCOPE_HOST;
5103 else if (ifa_scope & IFA_LINK)
5104 return RT_SCOPE_LINK;
5105 else if (ifa_scope & IFA_SITE)
5106 return RT_SCOPE_SITE;
5107 else
5108 return RT_SCOPE_UNIVERSE;
5109 }
5110
inet6_ifaddr_msgsize(void)5111 static inline int inet6_ifaddr_msgsize(void)
5112 {
5113 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
5114 + nla_total_size(16) /* IFA_LOCAL */
5115 + nla_total_size(16) /* IFA_ADDRESS */
5116 + nla_total_size(sizeof(struct ifa_cacheinfo))
5117 + nla_total_size(4) /* IFA_FLAGS */
5118 + nla_total_size(1) /* IFA_PROTO */
5119 + nla_total_size(4) /* IFA_RT_PRIORITY */;
5120 }
5121
inet6_fill_ifaddr(struct sk_buff * skb,const struct inet6_ifaddr * ifa,struct inet6_fill_args * args)5122 static int inet6_fill_ifaddr(struct sk_buff *skb,
5123 const struct inet6_ifaddr *ifa,
5124 struct inet6_fill_args *args)
5125 {
5126 struct nlmsghdr *nlh;
5127 u32 preferred, valid;
5128 u32 flags, priority;
5129 u8 proto;
5130
5131 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5132 sizeof(struct ifaddrmsg), args->flags);
5133 if (!nlh)
5134 return -EMSGSIZE;
5135
5136 flags = READ_ONCE(ifa->flags);
5137 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5138 ifa->idev->dev->ifindex);
5139
5140 if (args->netnsid >= 0 &&
5141 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
5142 goto error;
5143
5144 preferred = READ_ONCE(ifa->prefered_lft);
5145 valid = READ_ONCE(ifa->valid_lft);
5146
5147 if (!((flags & IFA_F_PERMANENT) &&
5148 (preferred == INFINITY_LIFE_TIME))) {
5149 if (preferred != INFINITY_LIFE_TIME) {
5150 long tval = (jiffies - READ_ONCE(ifa->tstamp)) / HZ;
5151
5152 if (preferred > tval)
5153 preferred -= tval;
5154 else
5155 preferred = 0;
5156 if (valid != INFINITY_LIFE_TIME) {
5157 if (valid > tval)
5158 valid -= tval;
5159 else
5160 valid = 0;
5161 }
5162 }
5163 } else {
5164 preferred = INFINITY_LIFE_TIME;
5165 valid = INFINITY_LIFE_TIME;
5166 }
5167
5168 if (!ipv6_addr_any(&ifa->peer_addr)) {
5169 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5170 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5171 goto error;
5172 } else {
5173 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5174 goto error;
5175 }
5176
5177 priority = READ_ONCE(ifa->rt_priority);
5178 if (priority && nla_put_u32(skb, IFA_RT_PRIORITY, priority))
5179 goto error;
5180
5181 if (put_cacheinfo(skb, ifa->cstamp, READ_ONCE(ifa->tstamp),
5182 preferred, valid) < 0)
5183 goto error;
5184
5185 if (nla_put_u32(skb, IFA_FLAGS, flags) < 0)
5186 goto error;
5187
5188 proto = READ_ONCE(ifa->ifa_proto);
5189 if (proto && nla_put_u8(skb, IFA_PROTO, proto))
5190 goto error;
5191
5192 nlmsg_end(skb, nlh);
5193 return 0;
5194
5195 error:
5196 nlmsg_cancel(skb, nlh);
5197 return -EMSGSIZE;
5198 }
5199
inet6_fill_ifmcaddr(struct sk_buff * skb,const struct ifmcaddr6 * ifmca,struct inet6_fill_args * args)5200 int inet6_fill_ifmcaddr(struct sk_buff *skb,
5201 const struct ifmcaddr6 *ifmca,
5202 struct inet6_fill_args *args)
5203 {
5204 int ifindex = ifmca->idev->dev->ifindex;
5205 u8 scope = RT_SCOPE_UNIVERSE;
5206 struct nlmsghdr *nlh;
5207
5208 if (!args->force_rt_scope_universe &&
5209 ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5210 scope = RT_SCOPE_SITE;
5211
5212 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5213 sizeof(struct ifaddrmsg), args->flags);
5214 if (!nlh)
5215 return -EMSGSIZE;
5216
5217 if (args->netnsid >= 0 &&
5218 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5219 nlmsg_cancel(skb, nlh);
5220 return -EMSGSIZE;
5221 }
5222
5223 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5224 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
5225 put_cacheinfo(skb, ifmca->mca_cstamp, READ_ONCE(ifmca->mca_tstamp),
5226 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5227 nlmsg_cancel(skb, nlh);
5228 return -EMSGSIZE;
5229 }
5230
5231 nlmsg_end(skb, nlh);
5232 return 0;
5233 }
5234
inet6_fill_ifacaddr(struct sk_buff * skb,const struct ifacaddr6 * ifaca,struct inet6_fill_args * args)5235 int inet6_fill_ifacaddr(struct sk_buff *skb,
5236 const struct ifacaddr6 *ifaca,
5237 struct inet6_fill_args *args)
5238 {
5239 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5240 int ifindex = dev ? dev->ifindex : 1;
5241 u8 scope = RT_SCOPE_UNIVERSE;
5242 struct nlmsghdr *nlh;
5243
5244 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5245 scope = RT_SCOPE_SITE;
5246
5247 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5248 sizeof(struct ifaddrmsg), args->flags);
5249 if (!nlh)
5250 return -EMSGSIZE;
5251
5252 if (args->netnsid >= 0 &&
5253 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5254 nlmsg_cancel(skb, nlh);
5255 return -EMSGSIZE;
5256 }
5257
5258 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5259 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
5260 put_cacheinfo(skb, ifaca->aca_cstamp, READ_ONCE(ifaca->aca_tstamp),
5261 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5262 nlmsg_cancel(skb, nlh);
5263 return -EMSGSIZE;
5264 }
5265
5266 nlmsg_end(skb, nlh);
5267 return 0;
5268 }
5269
5270 /* 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)5271 static int in6_dump_addrs(const struct inet6_dev *idev, struct sk_buff *skb,
5272 struct netlink_callback *cb, int *s_ip_idx,
5273 struct inet6_fill_args *fillargs)
5274 {
5275 const struct ifmcaddr6 *ifmca;
5276 const struct ifacaddr6 *ifaca;
5277 int ip_idx = 0;
5278 int err = 0;
5279
5280 switch (fillargs->type) {
5281 case UNICAST_ADDR: {
5282 const struct inet6_ifaddr *ifa;
5283 fillargs->event = RTM_NEWADDR;
5284
5285 /* unicast address incl. temp addr */
5286 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
5287 if (ip_idx < *s_ip_idx)
5288 goto next;
5289 err = inet6_fill_ifaddr(skb, ifa, fillargs);
5290 if (err < 0)
5291 break;
5292 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5293 next:
5294 ip_idx++;
5295 }
5296 break;
5297 }
5298 case MULTICAST_ADDR:
5299 fillargs->event = RTM_GETMULTICAST;
5300
5301 /* multicast address */
5302 for (ifmca = rcu_dereference(idev->mc_list);
5303 ifmca;
5304 ifmca = rcu_dereference(ifmca->next), ip_idx++) {
5305 if (ip_idx < *s_ip_idx)
5306 continue;
5307 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5308 if (err < 0)
5309 break;
5310 }
5311 break;
5312 case ANYCAST_ADDR:
5313 fillargs->event = RTM_GETANYCAST;
5314 /* anycast address */
5315 for (ifaca = rcu_dereference(idev->ac_list); ifaca;
5316 ifaca = rcu_dereference(ifaca->aca_next), ip_idx++) {
5317 if (ip_idx < *s_ip_idx)
5318 continue;
5319 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5320 if (err < 0)
5321 break;
5322 }
5323 break;
5324 default:
5325 break;
5326 }
5327 *s_ip_idx = err ? ip_idx : 0;
5328 return err;
5329 }
5330
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)5331 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5332 struct inet6_fill_args *fillargs,
5333 struct net **tgt_net, struct sock *sk,
5334 struct netlink_callback *cb)
5335 {
5336 struct netlink_ext_ack *extack = cb->extack;
5337 struct nlattr *tb[IFA_MAX+1];
5338 struct ifaddrmsg *ifm;
5339 int err, i;
5340
5341 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5342 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5343 return -EINVAL;
5344 }
5345
5346 ifm = nlmsg_data(nlh);
5347 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5348 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5349 return -EINVAL;
5350 }
5351
5352 fillargs->ifindex = ifm->ifa_index;
5353 if (fillargs->ifindex) {
5354 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5355 fillargs->flags |= NLM_F_DUMP_FILTERED;
5356 }
5357
5358 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5359 ifa_ipv6_policy, extack);
5360 if (err < 0)
5361 return err;
5362
5363 for (i = 0; i <= IFA_MAX; ++i) {
5364 if (!tb[i])
5365 continue;
5366
5367 if (i == IFA_TARGET_NETNSID) {
5368 struct net *net;
5369
5370 fillargs->netnsid = nla_get_s32(tb[i]);
5371 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5372 if (IS_ERR(net)) {
5373 fillargs->netnsid = -1;
5374 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5375 return PTR_ERR(net);
5376 }
5377 *tgt_net = net;
5378 } else {
5379 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5380 return -EINVAL;
5381 }
5382 }
5383
5384 return 0;
5385 }
5386
inet6_dump_addr(struct sk_buff * skb,struct netlink_callback * cb,enum addr_type_t type)5387 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5388 enum addr_type_t type)
5389 {
5390 struct net *tgt_net = sock_net(skb->sk);
5391 const struct nlmsghdr *nlh = cb->nlh;
5392 struct inet6_fill_args fillargs = {
5393 .portid = NETLINK_CB(cb->skb).portid,
5394 .seq = cb->nlh->nlmsg_seq,
5395 .flags = NLM_F_MULTI,
5396 .netnsid = -1,
5397 .type = type,
5398 .force_rt_scope_universe = false,
5399 };
5400 struct {
5401 unsigned long ifindex;
5402 int ip_idx;
5403 } *ctx = (void *)cb->ctx;
5404 struct net_device *dev;
5405 struct inet6_dev *idev;
5406 int err = 0;
5407
5408 rcu_read_lock();
5409 if (cb->strict_check) {
5410 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5411 skb->sk, cb);
5412 if (err < 0)
5413 goto done;
5414
5415 err = 0;
5416 if (fillargs.ifindex) {
5417 dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex);
5418 if (!dev) {
5419 err = -ENODEV;
5420 goto done;
5421 }
5422 idev = __in6_dev_get(dev);
5423 if (idev)
5424 err = in6_dump_addrs(idev, skb, cb,
5425 &ctx->ip_idx,
5426 &fillargs);
5427 goto done;
5428 }
5429 }
5430
5431 cb->seq = inet6_base_seq(tgt_net);
5432 for_each_netdev_dump(tgt_net, dev, ctx->ifindex) {
5433 idev = __in6_dev_get(dev);
5434 if (!idev)
5435 continue;
5436 err = in6_dump_addrs(idev, skb, cb, &ctx->ip_idx,
5437 &fillargs);
5438 if (err < 0)
5439 goto done;
5440 }
5441 done:
5442 rcu_read_unlock();
5443 if (fillargs.netnsid >= 0)
5444 put_net(tgt_net);
5445
5446 return err;
5447 }
5448
inet6_dump_ifaddr(struct sk_buff * skb,struct netlink_callback * cb)5449 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5450 {
5451 enum addr_type_t type = UNICAST_ADDR;
5452
5453 return inet6_dump_addr(skb, cb, type);
5454 }
5455
inet6_dump_ifmcaddr(struct sk_buff * skb,struct netlink_callback * cb)5456 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5457 {
5458 enum addr_type_t type = MULTICAST_ADDR;
5459
5460 return inet6_dump_addr(skb, cb, type);
5461 }
5462
5463
inet6_dump_ifacaddr(struct sk_buff * skb,struct netlink_callback * cb)5464 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5465 {
5466 enum addr_type_t type = ANYCAST_ADDR;
5467
5468 return inet6_dump_addr(skb, cb, type);
5469 }
5470
inet6_rtm_valid_getaddr_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)5471 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5472 const struct nlmsghdr *nlh,
5473 struct nlattr **tb,
5474 struct netlink_ext_ack *extack)
5475 {
5476 struct ifaddrmsg *ifm;
5477 int i, err;
5478
5479 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5480 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5481 return -EINVAL;
5482 }
5483
5484 if (!netlink_strict_get_check(skb))
5485 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5486 ifa_ipv6_policy, extack);
5487
5488 ifm = nlmsg_data(nlh);
5489 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5490 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5491 return -EINVAL;
5492 }
5493
5494 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5495 ifa_ipv6_policy, extack);
5496 if (err)
5497 return err;
5498
5499 for (i = 0; i <= IFA_MAX; i++) {
5500 if (!tb[i])
5501 continue;
5502
5503 switch (i) {
5504 case IFA_TARGET_NETNSID:
5505 case IFA_ADDRESS:
5506 case IFA_LOCAL:
5507 break;
5508 default:
5509 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5510 return -EINVAL;
5511 }
5512 }
5513
5514 return 0;
5515 }
5516
inet6_rtm_getaddr(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5517 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5518 struct netlink_ext_ack *extack)
5519 {
5520 struct net *tgt_net = sock_net(in_skb->sk);
5521 struct inet6_fill_args fillargs = {
5522 .portid = NETLINK_CB(in_skb).portid,
5523 .seq = nlh->nlmsg_seq,
5524 .event = RTM_NEWADDR,
5525 .flags = 0,
5526 .netnsid = -1,
5527 .force_rt_scope_universe = false,
5528 };
5529 struct ifaddrmsg *ifm;
5530 struct nlattr *tb[IFA_MAX+1];
5531 struct in6_addr *addr = NULL, *peer;
5532 struct net_device *dev = NULL;
5533 struct inet6_ifaddr *ifa;
5534 struct sk_buff *skb;
5535 int err;
5536
5537 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5538 if (err < 0)
5539 return err;
5540
5541 if (tb[IFA_TARGET_NETNSID]) {
5542 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5543
5544 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5545 fillargs.netnsid);
5546 if (IS_ERR(tgt_net))
5547 return PTR_ERR(tgt_net);
5548 }
5549
5550 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5551 if (!addr) {
5552 err = -EINVAL;
5553 goto errout;
5554 }
5555 ifm = nlmsg_data(nlh);
5556 if (ifm->ifa_index)
5557 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5558
5559 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5560 if (!ifa) {
5561 err = -EADDRNOTAVAIL;
5562 goto errout;
5563 }
5564
5565 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5566 if (!skb) {
5567 err = -ENOBUFS;
5568 goto errout_ifa;
5569 }
5570
5571 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5572 if (err < 0) {
5573 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5574 WARN_ON(err == -EMSGSIZE);
5575 kfree_skb(skb);
5576 goto errout_ifa;
5577 }
5578 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5579 errout_ifa:
5580 in6_ifa_put(ifa);
5581 errout:
5582 dev_put(dev);
5583 if (fillargs.netnsid >= 0)
5584 put_net(tgt_net);
5585
5586 return err;
5587 }
5588
inet6_ifa_notify(int event,struct inet6_ifaddr * ifa)5589 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5590 {
5591 struct sk_buff *skb;
5592 struct net *net = dev_net(ifa->idev->dev);
5593 struct inet6_fill_args fillargs = {
5594 .portid = 0,
5595 .seq = 0,
5596 .event = event,
5597 .flags = 0,
5598 .netnsid = -1,
5599 .force_rt_scope_universe = false,
5600 };
5601 int err = -ENOBUFS;
5602
5603 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5604 if (!skb)
5605 goto errout;
5606
5607 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5608 if (err < 0) {
5609 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5610 WARN_ON(err == -EMSGSIZE);
5611 kfree_skb(skb);
5612 goto errout;
5613 }
5614 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5615 return;
5616 errout:
5617 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5618 }
5619
ipv6_store_devconf(const struct ipv6_devconf * cnf,__s32 * array,int bytes)5620 static void ipv6_store_devconf(const struct ipv6_devconf *cnf,
5621 __s32 *array, int bytes)
5622 {
5623 BUG_ON(bytes < (DEVCONF_MAX * 4));
5624
5625 memset(array, 0, bytes);
5626 array[DEVCONF_FORWARDING] = READ_ONCE(cnf->forwarding);
5627 array[DEVCONF_HOPLIMIT] = READ_ONCE(cnf->hop_limit);
5628 array[DEVCONF_MTU6] = READ_ONCE(cnf->mtu6);
5629 array[DEVCONF_ACCEPT_RA] = READ_ONCE(cnf->accept_ra);
5630 array[DEVCONF_ACCEPT_REDIRECTS] = READ_ONCE(cnf->accept_redirects);
5631 array[DEVCONF_AUTOCONF] = READ_ONCE(cnf->autoconf);
5632 array[DEVCONF_DAD_TRANSMITS] = READ_ONCE(cnf->dad_transmits);
5633 array[DEVCONF_RTR_SOLICITS] = READ_ONCE(cnf->rtr_solicits);
5634 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5635 jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_interval));
5636 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5637 jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_max_interval));
5638 array[DEVCONF_RTR_SOLICIT_DELAY] =
5639 jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_delay));
5640 array[DEVCONF_FORCE_MLD_VERSION] = READ_ONCE(cnf->force_mld_version);
5641 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5642 jiffies_to_msecs(READ_ONCE(cnf->mldv1_unsolicited_report_interval));
5643 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5644 jiffies_to_msecs(READ_ONCE(cnf->mldv2_unsolicited_report_interval));
5645 array[DEVCONF_USE_TEMPADDR] = READ_ONCE(cnf->use_tempaddr);
5646 array[DEVCONF_TEMP_VALID_LFT] = READ_ONCE(cnf->temp_valid_lft);
5647 array[DEVCONF_TEMP_PREFERED_LFT] = READ_ONCE(cnf->temp_prefered_lft);
5648 array[DEVCONF_REGEN_MAX_RETRY] = READ_ONCE(cnf->regen_max_retry);
5649 array[DEVCONF_MAX_DESYNC_FACTOR] = READ_ONCE(cnf->max_desync_factor);
5650 array[DEVCONF_MAX_ADDRESSES] = READ_ONCE(cnf->max_addresses);
5651 array[DEVCONF_ACCEPT_RA_DEFRTR] = READ_ONCE(cnf->accept_ra_defrtr);
5652 array[DEVCONF_RA_DEFRTR_METRIC] = READ_ONCE(cnf->ra_defrtr_metric);
5653 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] =
5654 READ_ONCE(cnf->accept_ra_min_hop_limit);
5655 array[DEVCONF_ACCEPT_RA_PINFO] = READ_ONCE(cnf->accept_ra_pinfo);
5656 #ifdef CONFIG_IPV6_ROUTER_PREF
5657 array[DEVCONF_ACCEPT_RA_RTR_PREF] = READ_ONCE(cnf->accept_ra_rtr_pref);
5658 array[DEVCONF_RTR_PROBE_INTERVAL] =
5659 jiffies_to_msecs(READ_ONCE(cnf->rtr_probe_interval));
5660 #ifdef CONFIG_IPV6_ROUTE_INFO
5661 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] =
5662 READ_ONCE(cnf->accept_ra_rt_info_min_plen);
5663 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] =
5664 READ_ONCE(cnf->accept_ra_rt_info_max_plen);
5665 #endif
5666 #endif
5667 array[DEVCONF_PROXY_NDP] = READ_ONCE(cnf->proxy_ndp);
5668 array[DEVCONF_ACCEPT_SOURCE_ROUTE] =
5669 READ_ONCE(cnf->accept_source_route);
5670 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5671 array[DEVCONF_OPTIMISTIC_DAD] = READ_ONCE(cnf->optimistic_dad);
5672 array[DEVCONF_USE_OPTIMISTIC] = READ_ONCE(cnf->use_optimistic);
5673 #endif
5674 #ifdef CONFIG_IPV6_MROUTE
5675 array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
5676 #endif
5677 array[DEVCONF_DISABLE_IPV6] = READ_ONCE(cnf->disable_ipv6);
5678 array[DEVCONF_ACCEPT_DAD] = READ_ONCE(cnf->accept_dad);
5679 array[DEVCONF_FORCE_TLLAO] = READ_ONCE(cnf->force_tllao);
5680 array[DEVCONF_NDISC_NOTIFY] = READ_ONCE(cnf->ndisc_notify);
5681 array[DEVCONF_SUPPRESS_FRAG_NDISC] =
5682 READ_ONCE(cnf->suppress_frag_ndisc);
5683 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] =
5684 READ_ONCE(cnf->accept_ra_from_local);
5685 array[DEVCONF_ACCEPT_RA_MTU] = READ_ONCE(cnf->accept_ra_mtu);
5686 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] =
5687 READ_ONCE(cnf->ignore_routes_with_linkdown);
5688 /* we omit DEVCONF_STABLE_SECRET for now */
5689 array[DEVCONF_USE_OIF_ADDRS_ONLY] = READ_ONCE(cnf->use_oif_addrs_only);
5690 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] =
5691 READ_ONCE(cnf->drop_unicast_in_l2_multicast);
5692 array[DEVCONF_DROP_UNSOLICITED_NA] = READ_ONCE(cnf->drop_unsolicited_na);
5693 array[DEVCONF_KEEP_ADDR_ON_DOWN] = READ_ONCE(cnf->keep_addr_on_down);
5694 array[DEVCONF_SEG6_ENABLED] = READ_ONCE(cnf->seg6_enabled);
5695 #ifdef CONFIG_IPV6_SEG6_HMAC
5696 array[DEVCONF_SEG6_REQUIRE_HMAC] = READ_ONCE(cnf->seg6_require_hmac);
5697 #endif
5698 array[DEVCONF_ENHANCED_DAD] = READ_ONCE(cnf->enhanced_dad);
5699 array[DEVCONF_ADDR_GEN_MODE] = READ_ONCE(cnf->addr_gen_mode);
5700 array[DEVCONF_DISABLE_POLICY] = READ_ONCE(cnf->disable_policy);
5701 array[DEVCONF_NDISC_TCLASS] = READ_ONCE(cnf->ndisc_tclass);
5702 array[DEVCONF_RPL_SEG_ENABLED] = READ_ONCE(cnf->rpl_seg_enabled);
5703 array[DEVCONF_IOAM6_ENABLED] = READ_ONCE(cnf->ioam6_enabled);
5704 array[DEVCONF_IOAM6_ID] = READ_ONCE(cnf->ioam6_id);
5705 array[DEVCONF_IOAM6_ID_WIDE] = READ_ONCE(cnf->ioam6_id_wide);
5706 array[DEVCONF_NDISC_EVICT_NOCARRIER] =
5707 READ_ONCE(cnf->ndisc_evict_nocarrier);
5708 array[DEVCONF_ACCEPT_UNTRACKED_NA] =
5709 READ_ONCE(cnf->accept_untracked_na);
5710 array[DEVCONF_ACCEPT_RA_MIN_LFT] = READ_ONCE(cnf->accept_ra_min_lft);
5711 }
5712
inet6_ifla6_size(void)5713 static inline size_t inet6_ifla6_size(void)
5714 {
5715 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5716 + nla_total_size(sizeof(struct ifla_cacheinfo))
5717 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5718 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5719 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5720 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5721 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5722 + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5723 + 0;
5724 }
5725
inet6_if_nlmsg_size(void)5726 static inline size_t inet6_if_nlmsg_size(void)
5727 {
5728 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5729 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5730 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5731 + nla_total_size(4) /* IFLA_MTU */
5732 + nla_total_size(4) /* IFLA_LINK */
5733 + nla_total_size(1) /* IFLA_OPERSTATE */
5734 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5735 }
5736
__snmp6_fill_statsdev(u64 * stats,atomic_long_t * mib,int bytes)5737 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5738 int bytes)
5739 {
5740 int i;
5741 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5742 BUG_ON(pad < 0);
5743
5744 /* Use put_unaligned() because stats may not be aligned for u64. */
5745 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5746 for (i = 1; i < ICMP6_MIB_MAX; i++)
5747 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5748
5749 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5750 }
5751
__snmp6_fill_stats64(u64 * stats,void __percpu * mib,int bytes,size_t syncpoff)5752 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5753 int bytes, size_t syncpoff)
5754 {
5755 int i, c;
5756 u64 buff[IPSTATS_MIB_MAX];
5757 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5758
5759 BUG_ON(pad < 0);
5760
5761 memset(buff, 0, sizeof(buff));
5762 buff[0] = IPSTATS_MIB_MAX;
5763
5764 for_each_possible_cpu(c) {
5765 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5766 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5767 }
5768
5769 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5770 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5771 }
5772
snmp6_fill_stats(u64 * stats,struct inet6_dev * idev,int attrtype,int bytes)5773 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5774 int bytes)
5775 {
5776 switch (attrtype) {
5777 case IFLA_INET6_STATS:
5778 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5779 offsetof(struct ipstats_mib, syncp));
5780 break;
5781 case IFLA_INET6_ICMP6STATS:
5782 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5783 break;
5784 }
5785 }
5786
inet6_fill_ifla6_attrs(struct sk_buff * skb,struct inet6_dev * idev,u32 ext_filter_mask)5787 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5788 u32 ext_filter_mask)
5789 {
5790 struct ifla_cacheinfo ci;
5791 struct nlattr *nla;
5792 u32 ra_mtu;
5793
5794 if (nla_put_u32(skb, IFLA_INET6_FLAGS, READ_ONCE(idev->if_flags)))
5795 goto nla_put_failure;
5796 ci.max_reasm_len = IPV6_MAXPLEN;
5797 ci.tstamp = cstamp_delta(READ_ONCE(idev->tstamp));
5798 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5799 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5800 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5801 goto nla_put_failure;
5802 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5803 if (!nla)
5804 goto nla_put_failure;
5805 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5806
5807 /* XXX - MC not implemented */
5808
5809 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5810 return 0;
5811
5812 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5813 if (!nla)
5814 goto nla_put_failure;
5815 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5816
5817 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5818 if (!nla)
5819 goto nla_put_failure;
5820 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5821
5822 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5823 if (!nla)
5824 goto nla_put_failure;
5825 read_lock_bh(&idev->lock);
5826 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5827 read_unlock_bh(&idev->lock);
5828
5829 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE,
5830 READ_ONCE(idev->cnf.addr_gen_mode)))
5831 goto nla_put_failure;
5832
5833 ra_mtu = READ_ONCE(idev->ra_mtu);
5834 if (ra_mtu && nla_put_u32(skb, IFLA_INET6_RA_MTU, ra_mtu))
5835 goto nla_put_failure;
5836
5837 return 0;
5838
5839 nla_put_failure:
5840 return -EMSGSIZE;
5841 }
5842
inet6_get_link_af_size(const struct net_device * dev,u32 ext_filter_mask)5843 static size_t inet6_get_link_af_size(const struct net_device *dev,
5844 u32 ext_filter_mask)
5845 {
5846 if (!__in6_dev_get(dev))
5847 return 0;
5848
5849 return inet6_ifla6_size();
5850 }
5851
inet6_fill_link_af(struct sk_buff * skb,const struct net_device * dev,u32 ext_filter_mask)5852 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5853 u32 ext_filter_mask)
5854 {
5855 struct inet6_dev *idev = __in6_dev_get(dev);
5856
5857 if (!idev)
5858 return -ENODATA;
5859
5860 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5861 return -EMSGSIZE;
5862
5863 return 0;
5864 }
5865
inet6_set_iftoken(struct inet6_dev * idev,struct in6_addr * token,struct netlink_ext_ack * extack)5866 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
5867 struct netlink_ext_ack *extack)
5868 {
5869 struct inet6_ifaddr *ifp;
5870 struct net_device *dev = idev->dev;
5871 bool clear_token, update_rs = false;
5872 struct in6_addr ll_addr;
5873
5874 ASSERT_RTNL();
5875
5876 if (!token)
5877 return -EINVAL;
5878
5879 if (dev->flags & IFF_LOOPBACK) {
5880 NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5881 return -EINVAL;
5882 }
5883
5884 if (dev->flags & IFF_NOARP) {
5885 NL_SET_ERR_MSG_MOD(extack,
5886 "Device does not do neighbour discovery");
5887 return -EINVAL;
5888 }
5889
5890 if (!ipv6_accept_ra(idev)) {
5891 NL_SET_ERR_MSG_MOD(extack,
5892 "Router advertisement is disabled on device");
5893 return -EINVAL;
5894 }
5895
5896 if (READ_ONCE(idev->cnf.rtr_solicits) == 0) {
5897 NL_SET_ERR_MSG(extack,
5898 "Router solicitation is disabled on device");
5899 return -EINVAL;
5900 }
5901
5902 write_lock_bh(&idev->lock);
5903
5904 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5905 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5906
5907 write_unlock_bh(&idev->lock);
5908
5909 clear_token = ipv6_addr_any(token);
5910 if (clear_token)
5911 goto update_lft;
5912
5913 if (!idev->dead && (idev->if_flags & IF_READY) &&
5914 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5915 IFA_F_OPTIMISTIC)) {
5916 /* If we're not ready, then normal ifup will take care
5917 * of this. Otherwise, we need to request our rs here.
5918 */
5919 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5920 update_rs = true;
5921 }
5922
5923 update_lft:
5924 write_lock_bh(&idev->lock);
5925
5926 if (update_rs) {
5927 idev->if_flags |= IF_RS_SENT;
5928 idev->rs_interval = rfc3315_s14_backoff_init(
5929 READ_ONCE(idev->cnf.rtr_solicit_interval));
5930 idev->rs_probes = 1;
5931 addrconf_mod_rs_timer(idev, idev->rs_interval);
5932 }
5933
5934 /* Well, that's kinda nasty ... */
5935 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5936 spin_lock(&ifp->lock);
5937 if (ifp->tokenized) {
5938 ifp->valid_lft = 0;
5939 ifp->prefered_lft = 0;
5940 }
5941 spin_unlock(&ifp->lock);
5942 }
5943
5944 write_unlock_bh(&idev->lock);
5945 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5946 addrconf_verify_rtnl(dev_net(dev));
5947 return 0;
5948 }
5949
5950 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5951 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5952 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5953 [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT,
5954 .reject_message =
5955 "IFLA_INET6_RA_MTU can not be set" },
5956 };
5957
check_addr_gen_mode(int mode)5958 static int check_addr_gen_mode(int mode)
5959 {
5960 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5961 mode != IN6_ADDR_GEN_MODE_NONE &&
5962 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5963 mode != IN6_ADDR_GEN_MODE_RANDOM)
5964 return -EINVAL;
5965 return 1;
5966 }
5967
check_stable_privacy(struct inet6_dev * idev,struct net * net,int mode)5968 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5969 int mode)
5970 {
5971 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5972 !idev->cnf.stable_secret.initialized &&
5973 !net->ipv6.devconf_dflt->stable_secret.initialized)
5974 return -EINVAL;
5975 return 1;
5976 }
5977
inet6_validate_link_af(const struct net_device * dev,const struct nlattr * nla,struct netlink_ext_ack * extack)5978 static int inet6_validate_link_af(const struct net_device *dev,
5979 const struct nlattr *nla,
5980 struct netlink_ext_ack *extack)
5981 {
5982 struct nlattr *tb[IFLA_INET6_MAX + 1];
5983 struct inet6_dev *idev = NULL;
5984 int err;
5985
5986 if (dev) {
5987 idev = __in6_dev_get(dev);
5988 if (!idev)
5989 return -EAFNOSUPPORT;
5990 }
5991
5992 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5993 inet6_af_policy, extack);
5994 if (err)
5995 return err;
5996
5997 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
5998 return -EINVAL;
5999
6000 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
6001 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
6002
6003 if (check_addr_gen_mode(mode) < 0)
6004 return -EINVAL;
6005 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
6006 return -EINVAL;
6007 }
6008
6009 return 0;
6010 }
6011
inet6_set_link_af(struct net_device * dev,const struct nlattr * nla,struct netlink_ext_ack * extack)6012 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
6013 struct netlink_ext_ack *extack)
6014 {
6015 struct inet6_dev *idev = __in6_dev_get(dev);
6016 struct nlattr *tb[IFLA_INET6_MAX + 1];
6017 int err;
6018
6019 if (!idev)
6020 return -EAFNOSUPPORT;
6021
6022 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
6023 return -EINVAL;
6024
6025 if (tb[IFLA_INET6_TOKEN]) {
6026 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
6027 extack);
6028 if (err)
6029 return err;
6030 }
6031
6032 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
6033 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
6034
6035 WRITE_ONCE(idev->cnf.addr_gen_mode, mode);
6036 }
6037
6038 return 0;
6039 }
6040
inet6_fill_ifinfo(struct sk_buff * skb,struct inet6_dev * idev,u32 portid,u32 seq,int event,unsigned int flags)6041 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
6042 u32 portid, u32 seq, int event, unsigned int flags)
6043 {
6044 struct net_device *dev = idev->dev;
6045 struct ifinfomsg *hdr;
6046 struct nlmsghdr *nlh;
6047 int ifindex, iflink;
6048 void *protoinfo;
6049
6050 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
6051 if (!nlh)
6052 return -EMSGSIZE;
6053
6054 hdr = nlmsg_data(nlh);
6055 hdr->ifi_family = AF_INET6;
6056 hdr->__ifi_pad = 0;
6057 hdr->ifi_type = dev->type;
6058 ifindex = READ_ONCE(dev->ifindex);
6059 hdr->ifi_index = ifindex;
6060 hdr->ifi_flags = dev_get_flags(dev);
6061 hdr->ifi_change = 0;
6062
6063 iflink = dev_get_iflink(dev);
6064 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
6065 (dev->addr_len &&
6066 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
6067 nla_put_u32(skb, IFLA_MTU, READ_ONCE(dev->mtu)) ||
6068 (ifindex != iflink &&
6069 nla_put_u32(skb, IFLA_LINK, iflink)) ||
6070 nla_put_u8(skb, IFLA_OPERSTATE,
6071 netif_running(dev) ? READ_ONCE(dev->operstate) : IF_OPER_DOWN))
6072 goto nla_put_failure;
6073 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
6074 if (!protoinfo)
6075 goto nla_put_failure;
6076
6077 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
6078 goto nla_put_failure;
6079
6080 nla_nest_end(skb, protoinfo);
6081 nlmsg_end(skb, nlh);
6082 return 0;
6083
6084 nla_put_failure:
6085 nlmsg_cancel(skb, nlh);
6086 return -EMSGSIZE;
6087 }
6088
inet6_valid_dump_ifinfo(const struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6089 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
6090 struct netlink_ext_ack *extack)
6091 {
6092 struct ifinfomsg *ifm;
6093
6094 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
6095 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
6096 return -EINVAL;
6097 }
6098
6099 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
6100 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
6101 return -EINVAL;
6102 }
6103
6104 ifm = nlmsg_data(nlh);
6105 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
6106 ifm->ifi_change || ifm->ifi_index) {
6107 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6108 return -EINVAL;
6109 }
6110
6111 return 0;
6112 }
6113
inet6_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)6114 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6115 {
6116 struct net *net = sock_net(skb->sk);
6117 struct {
6118 unsigned long ifindex;
6119 } *ctx = (void *)cb->ctx;
6120 struct net_device *dev;
6121 struct inet6_dev *idev;
6122 int err;
6123
6124 /* only requests using strict checking can pass data to
6125 * influence the dump
6126 */
6127 if (cb->strict_check) {
6128 err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6129
6130 if (err < 0)
6131 return err;
6132 }
6133
6134 err = 0;
6135 rcu_read_lock();
6136 for_each_netdev_dump(net, dev, ctx->ifindex) {
6137 idev = __in6_dev_get(dev);
6138 if (!idev)
6139 continue;
6140 err = inet6_fill_ifinfo(skb, idev,
6141 NETLINK_CB(cb->skb).portid,
6142 cb->nlh->nlmsg_seq,
6143 RTM_NEWLINK, NLM_F_MULTI);
6144 if (err < 0)
6145 break;
6146 }
6147 rcu_read_unlock();
6148
6149 return err;
6150 }
6151
inet6_ifinfo_notify(int event,struct inet6_dev * idev)6152 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
6153 {
6154 struct sk_buff *skb;
6155 struct net *net = dev_net(idev->dev);
6156 int err = -ENOBUFS;
6157
6158 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
6159 if (!skb)
6160 goto errout;
6161
6162 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
6163 if (err < 0) {
6164 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6165 WARN_ON(err == -EMSGSIZE);
6166 kfree_skb(skb);
6167 goto errout;
6168 }
6169 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
6170 return;
6171 errout:
6172 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
6173 }
6174
inet6_prefix_nlmsg_size(void)6175 static inline size_t inet6_prefix_nlmsg_size(void)
6176 {
6177 return NLMSG_ALIGN(sizeof(struct prefixmsg))
6178 + nla_total_size(sizeof(struct in6_addr))
6179 + nla_total_size(sizeof(struct prefix_cacheinfo));
6180 }
6181
inet6_fill_prefix(struct sk_buff * skb,struct inet6_dev * idev,struct prefix_info * pinfo,u32 portid,u32 seq,int event,unsigned int flags)6182 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
6183 struct prefix_info *pinfo, u32 portid, u32 seq,
6184 int event, unsigned int flags)
6185 {
6186 struct prefixmsg *pmsg;
6187 struct nlmsghdr *nlh;
6188 struct prefix_cacheinfo ci;
6189
6190 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
6191 if (!nlh)
6192 return -EMSGSIZE;
6193
6194 pmsg = nlmsg_data(nlh);
6195 pmsg->prefix_family = AF_INET6;
6196 pmsg->prefix_pad1 = 0;
6197 pmsg->prefix_pad2 = 0;
6198 pmsg->prefix_ifindex = idev->dev->ifindex;
6199 pmsg->prefix_len = pinfo->prefix_len;
6200 pmsg->prefix_type = pinfo->type;
6201 pmsg->prefix_pad3 = 0;
6202 pmsg->prefix_flags = pinfo->flags;
6203
6204 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6205 goto nla_put_failure;
6206 ci.preferred_time = ntohl(pinfo->prefered);
6207 ci.valid_time = ntohl(pinfo->valid);
6208 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6209 goto nla_put_failure;
6210 nlmsg_end(skb, nlh);
6211 return 0;
6212
6213 nla_put_failure:
6214 nlmsg_cancel(skb, nlh);
6215 return -EMSGSIZE;
6216 }
6217
inet6_prefix_notify(int event,struct inet6_dev * idev,struct prefix_info * pinfo)6218 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
6219 struct prefix_info *pinfo)
6220 {
6221 struct sk_buff *skb;
6222 struct net *net = dev_net(idev->dev);
6223 int err = -ENOBUFS;
6224
6225 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
6226 if (!skb)
6227 goto errout;
6228
6229 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
6230 if (err < 0) {
6231 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6232 WARN_ON(err == -EMSGSIZE);
6233 kfree_skb(skb);
6234 goto errout;
6235 }
6236 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
6237 return;
6238 errout:
6239 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
6240 }
6241
__ipv6_ifa_notify(int event,struct inet6_ifaddr * ifp)6242 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6243 {
6244 struct net *net = dev_net(ifp->idev->dev);
6245
6246 if (event)
6247 ASSERT_RTNL();
6248
6249 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
6250
6251 switch (event) {
6252 case RTM_NEWADDR:
6253 /*
6254 * If the address was optimistic we inserted the route at the
6255 * start of our DAD process, so we don't need to do it again.
6256 * If the device was taken down in the middle of the DAD
6257 * cycle there is a race where we could get here without a
6258 * host route, so nothing to insert. That will be fixed when
6259 * the device is brought up.
6260 */
6261 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6262 ip6_ins_rt(net, ifp->rt);
6263 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6264 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6265 &ifp->addr, ifp->idev->dev->name);
6266 }
6267
6268 if (ifp->idev->cnf.forwarding)
6269 addrconf_join_anycast(ifp);
6270 if (!ipv6_addr_any(&ifp->peer_addr))
6271 addrconf_prefix_route(&ifp->peer_addr, 128,
6272 ifp->rt_priority, ifp->idev->dev,
6273 0, 0, GFP_ATOMIC);
6274 break;
6275 case RTM_DELADDR:
6276 if (ifp->idev->cnf.forwarding)
6277 addrconf_leave_anycast(ifp);
6278 addrconf_leave_solict(ifp->idev, &ifp->addr);
6279 if (!ipv6_addr_any(&ifp->peer_addr)) {
6280 struct fib6_info *rt;
6281
6282 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
6283 ifp->idev->dev, 0, 0,
6284 false);
6285 if (rt)
6286 ip6_del_rt(net, rt, false);
6287 }
6288 if (ifp->rt) {
6289 ip6_del_rt(net, ifp->rt, false);
6290 ifp->rt = NULL;
6291 }
6292 rt_genid_bump_ipv6(net);
6293 break;
6294 }
6295 atomic_inc(&net->ipv6.dev_addr_genid);
6296 }
6297
ipv6_ifa_notify(int event,struct inet6_ifaddr * ifp)6298 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6299 {
6300 if (likely(ifp->idev->dead == 0))
6301 __ipv6_ifa_notify(event, ifp);
6302 }
6303
6304 #ifdef CONFIG_SYSCTL
6305
addrconf_sysctl_forward(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6306 static int addrconf_sysctl_forward(const struct ctl_table *ctl, int write,
6307 void *buffer, size_t *lenp, loff_t *ppos)
6308 {
6309 int *valp = ctl->data;
6310 int val = *valp;
6311 loff_t pos = *ppos;
6312 struct ctl_table lctl;
6313 int ret;
6314
6315 /*
6316 * ctl->data points to idev->cnf.forwarding, we should
6317 * not modify it until we get the rtnl lock.
6318 */
6319 lctl = *ctl;
6320 lctl.data = &val;
6321
6322 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6323
6324 if (write)
6325 ret = addrconf_fixup_forwarding(ctl, valp, val);
6326 if (ret)
6327 *ppos = pos;
6328 return ret;
6329 }
6330
addrconf_sysctl_mtu(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6331 static int addrconf_sysctl_mtu(const struct ctl_table *ctl, int write,
6332 void *buffer, size_t *lenp, loff_t *ppos)
6333 {
6334 struct inet6_dev *idev = ctl->extra1;
6335 int min_mtu = IPV6_MIN_MTU;
6336 struct ctl_table lctl;
6337
6338 lctl = *ctl;
6339 lctl.extra1 = &min_mtu;
6340 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6341
6342 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6343 }
6344
dev_disable_change(struct inet6_dev * idev)6345 static void dev_disable_change(struct inet6_dev *idev)
6346 {
6347 struct netdev_notifier_info info;
6348
6349 if (!idev || !idev->dev)
6350 return;
6351
6352 netdev_notifier_info_init(&info, idev->dev);
6353 if (idev->cnf.disable_ipv6)
6354 addrconf_notify(NULL, NETDEV_DOWN, &info);
6355 else
6356 addrconf_notify(NULL, NETDEV_UP, &info);
6357 }
6358
addrconf_disable_change(struct net * net,__s32 newf)6359 static void addrconf_disable_change(struct net *net, __s32 newf)
6360 {
6361 struct net_device *dev;
6362 struct inet6_dev *idev;
6363
6364 for_each_netdev(net, dev) {
6365 idev = __in6_dev_get_rtnl_net(dev);
6366 if (idev) {
6367 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6368
6369 WRITE_ONCE(idev->cnf.disable_ipv6, newf);
6370 if (changed)
6371 dev_disable_change(idev);
6372 }
6373 }
6374 }
6375
addrconf_disable_ipv6(const struct ctl_table * table,int * p,int newf)6376 static int addrconf_disable_ipv6(const struct ctl_table *table, int *p, int newf)
6377 {
6378 struct net *net = (struct net *)table->extra2;
6379 int old;
6380
6381 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6382 WRITE_ONCE(*p, newf);
6383 return 0;
6384 }
6385
6386 if (!rtnl_net_trylock(net))
6387 return restart_syscall();
6388
6389 old = *p;
6390 WRITE_ONCE(*p, newf);
6391
6392 if (p == &net->ipv6.devconf_all->disable_ipv6) {
6393 WRITE_ONCE(net->ipv6.devconf_dflt->disable_ipv6, newf);
6394 addrconf_disable_change(net, newf);
6395 } else if ((!newf) ^ (!old)) {
6396 dev_disable_change((struct inet6_dev *)table->extra1);
6397 }
6398
6399 rtnl_net_unlock(net);
6400 return 0;
6401 }
6402
addrconf_sysctl_disable(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6403 static int addrconf_sysctl_disable(const struct ctl_table *ctl, int write,
6404 void *buffer, size_t *lenp, loff_t *ppos)
6405 {
6406 int *valp = ctl->data;
6407 int val = *valp;
6408 loff_t pos = *ppos;
6409 struct ctl_table lctl;
6410 int ret;
6411
6412 /*
6413 * ctl->data points to idev->cnf.disable_ipv6, we should
6414 * not modify it until we get the rtnl lock.
6415 */
6416 lctl = *ctl;
6417 lctl.data = &val;
6418
6419 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6420
6421 if (write)
6422 ret = addrconf_disable_ipv6(ctl, valp, val);
6423 if (ret)
6424 *ppos = pos;
6425 return ret;
6426 }
6427
addrconf_sysctl_proxy_ndp(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6428 static int addrconf_sysctl_proxy_ndp(const struct ctl_table *ctl, int write,
6429 void *buffer, size_t *lenp, loff_t *ppos)
6430 {
6431 int *valp = ctl->data;
6432 int ret;
6433 int old, new;
6434
6435 old = *valp;
6436 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6437 new = *valp;
6438
6439 if (write && old != new) {
6440 struct net *net = ctl->extra2;
6441
6442 if (!rtnl_net_trylock(net))
6443 return restart_syscall();
6444
6445 if (valp == &net->ipv6.devconf_dflt->proxy_ndp) {
6446 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6447 NETCONFA_PROXY_NEIGH,
6448 NETCONFA_IFINDEX_DEFAULT,
6449 net->ipv6.devconf_dflt);
6450 } else if (valp == &net->ipv6.devconf_all->proxy_ndp) {
6451 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6452 NETCONFA_PROXY_NEIGH,
6453 NETCONFA_IFINDEX_ALL,
6454 net->ipv6.devconf_all);
6455 } else {
6456 struct inet6_dev *idev = ctl->extra1;
6457
6458 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6459 NETCONFA_PROXY_NEIGH,
6460 idev->dev->ifindex,
6461 &idev->cnf);
6462 }
6463 rtnl_net_unlock(net);
6464 }
6465
6466 return ret;
6467 }
6468
addrconf_sysctl_addr_gen_mode(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6469 static int addrconf_sysctl_addr_gen_mode(const struct ctl_table *ctl, int write,
6470 void *buffer, size_t *lenp,
6471 loff_t *ppos)
6472 {
6473 int ret = 0;
6474 u32 new_val;
6475 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6476 struct net *net = (struct net *)ctl->extra2;
6477 struct ctl_table tmp = {
6478 .data = &new_val,
6479 .maxlen = sizeof(new_val),
6480 .mode = ctl->mode,
6481 };
6482
6483 if (!rtnl_net_trylock(net))
6484 return restart_syscall();
6485
6486 new_val = *((u32 *)ctl->data);
6487
6488 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6489 if (ret != 0)
6490 goto out;
6491
6492 if (write) {
6493 if (check_addr_gen_mode(new_val) < 0) {
6494 ret = -EINVAL;
6495 goto out;
6496 }
6497
6498 if (idev) {
6499 if (check_stable_privacy(idev, net, new_val) < 0) {
6500 ret = -EINVAL;
6501 goto out;
6502 }
6503
6504 if (idev->cnf.addr_gen_mode != new_val) {
6505 WRITE_ONCE(idev->cnf.addr_gen_mode, new_val);
6506 addrconf_init_auto_addrs(idev->dev);
6507 }
6508 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6509 struct net_device *dev;
6510
6511 WRITE_ONCE(net->ipv6.devconf_dflt->addr_gen_mode, new_val);
6512 for_each_netdev(net, dev) {
6513 idev = __in6_dev_get_rtnl_net(dev);
6514 if (idev &&
6515 idev->cnf.addr_gen_mode != new_val) {
6516 WRITE_ONCE(idev->cnf.addr_gen_mode,
6517 new_val);
6518 addrconf_init_auto_addrs(idev->dev);
6519 }
6520 }
6521 }
6522
6523 WRITE_ONCE(*((u32 *)ctl->data), new_val);
6524 }
6525
6526 out:
6527 rtnl_net_unlock(net);
6528
6529 return ret;
6530 }
6531
addrconf_sysctl_stable_secret(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6532 static int addrconf_sysctl_stable_secret(const struct ctl_table *ctl, int write,
6533 void *buffer, size_t *lenp,
6534 loff_t *ppos)
6535 {
6536 int err;
6537 struct in6_addr addr;
6538 char str[IPV6_MAX_STRLEN];
6539 struct ctl_table lctl = *ctl;
6540 struct net *net = ctl->extra2;
6541 struct ipv6_stable_secret *secret = ctl->data;
6542
6543 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6544 return -EIO;
6545
6546 lctl.maxlen = IPV6_MAX_STRLEN;
6547 lctl.data = str;
6548
6549 if (!rtnl_net_trylock(net))
6550 return restart_syscall();
6551
6552 if (!write && !secret->initialized) {
6553 err = -EIO;
6554 goto out;
6555 }
6556
6557 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6558 if (err >= sizeof(str)) {
6559 err = -EIO;
6560 goto out;
6561 }
6562
6563 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6564 if (err || !write)
6565 goto out;
6566
6567 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6568 err = -EIO;
6569 goto out;
6570 }
6571
6572 secret->initialized = true;
6573 secret->secret = addr;
6574
6575 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6576 struct net_device *dev;
6577
6578 for_each_netdev(net, dev) {
6579 struct inet6_dev *idev = __in6_dev_get_rtnl_net(dev);
6580
6581 if (idev) {
6582 WRITE_ONCE(idev->cnf.addr_gen_mode,
6583 IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
6584 }
6585 }
6586 } else {
6587 struct inet6_dev *idev = ctl->extra1;
6588
6589 WRITE_ONCE(idev->cnf.addr_gen_mode,
6590 IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
6591 }
6592
6593 out:
6594 rtnl_net_unlock(net);
6595
6596 return err;
6597 }
6598
6599 static
addrconf_sysctl_ignore_routes_with_linkdown(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6600 int addrconf_sysctl_ignore_routes_with_linkdown(const struct ctl_table *ctl,
6601 int write, void *buffer,
6602 size_t *lenp,
6603 loff_t *ppos)
6604 {
6605 int *valp = ctl->data;
6606 int val = *valp;
6607 loff_t pos = *ppos;
6608 struct ctl_table lctl;
6609 int ret;
6610
6611 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6612 * we should not modify it until we get the rtnl lock.
6613 */
6614 lctl = *ctl;
6615 lctl.data = &val;
6616
6617 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6618
6619 if (write)
6620 ret = addrconf_fixup_linkdown(ctl, valp, val);
6621 if (ret)
6622 *ppos = pos;
6623 return ret;
6624 }
6625
6626 static
addrconf_set_nopolicy(struct rt6_info * rt,int action)6627 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6628 {
6629 if (rt) {
6630 if (action)
6631 rt->dst.flags |= DST_NOPOLICY;
6632 else
6633 rt->dst.flags &= ~DST_NOPOLICY;
6634 }
6635 }
6636
6637 static
addrconf_disable_policy_idev(struct inet6_dev * idev,int val)6638 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6639 {
6640 struct inet6_ifaddr *ifa;
6641
6642 read_lock_bh(&idev->lock);
6643 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6644 spin_lock(&ifa->lock);
6645 if (ifa->rt) {
6646 /* host routes only use builtin fib6_nh */
6647 struct fib6_nh *nh = ifa->rt->fib6_nh;
6648 int cpu;
6649
6650 rcu_read_lock();
6651 ifa->rt->dst_nopolicy = val ? true : false;
6652 if (nh->rt6i_pcpu) {
6653 for_each_possible_cpu(cpu) {
6654 struct rt6_info **rtp;
6655
6656 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6657 addrconf_set_nopolicy(*rtp, val);
6658 }
6659 }
6660 rcu_read_unlock();
6661 }
6662 spin_unlock(&ifa->lock);
6663 }
6664 read_unlock_bh(&idev->lock);
6665 }
6666
6667 static
addrconf_disable_policy(const struct ctl_table * ctl,int * valp,int val)6668 int addrconf_disable_policy(const struct ctl_table *ctl, int *valp, int val)
6669 {
6670 struct net *net = (struct net *)ctl->extra2;
6671 struct inet6_dev *idev;
6672
6673 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6674 WRITE_ONCE(*valp, val);
6675 return 0;
6676 }
6677
6678 if (!rtnl_net_trylock(net))
6679 return restart_syscall();
6680
6681 WRITE_ONCE(*valp, val);
6682
6683 if (valp == &net->ipv6.devconf_all->disable_policy) {
6684 struct net_device *dev;
6685
6686 for_each_netdev(net, dev) {
6687 idev = __in6_dev_get_rtnl_net(dev);
6688 if (idev)
6689 addrconf_disable_policy_idev(idev, val);
6690 }
6691 } else {
6692 idev = (struct inet6_dev *)ctl->extra1;
6693 addrconf_disable_policy_idev(idev, val);
6694 }
6695
6696 rtnl_net_unlock(net);
6697 return 0;
6698 }
6699
addrconf_sysctl_disable_policy(const struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)6700 static int addrconf_sysctl_disable_policy(const struct ctl_table *ctl, int write,
6701 void *buffer, size_t *lenp, loff_t *ppos)
6702 {
6703 int *valp = ctl->data;
6704 int val = *valp;
6705 loff_t pos = *ppos;
6706 struct ctl_table lctl;
6707 int ret;
6708
6709 lctl = *ctl;
6710 lctl.data = &val;
6711 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6712
6713 if (write && (*valp != val))
6714 ret = addrconf_disable_policy(ctl, valp, val);
6715
6716 if (ret)
6717 *ppos = pos;
6718
6719 return ret;
6720 }
6721
6722 static int minus_one = -1;
6723 static const int two_five_five = 255;
6724 static u32 ioam6_if_id_max = U16_MAX;
6725
6726 static const struct ctl_table addrconf_sysctl[] = {
6727 {
6728 .procname = "forwarding",
6729 .data = &ipv6_devconf.forwarding,
6730 .maxlen = sizeof(int),
6731 .mode = 0644,
6732 .proc_handler = addrconf_sysctl_forward,
6733 },
6734 {
6735 .procname = "hop_limit",
6736 .data = &ipv6_devconf.hop_limit,
6737 .maxlen = sizeof(int),
6738 .mode = 0644,
6739 .proc_handler = proc_dointvec_minmax,
6740 .extra1 = (void *)SYSCTL_ONE,
6741 .extra2 = (void *)&two_five_five,
6742 },
6743 {
6744 .procname = "mtu",
6745 .data = &ipv6_devconf.mtu6,
6746 .maxlen = sizeof(int),
6747 .mode = 0644,
6748 .proc_handler = addrconf_sysctl_mtu,
6749 },
6750 {
6751 .procname = "accept_ra",
6752 .data = &ipv6_devconf.accept_ra,
6753 .maxlen = sizeof(int),
6754 .mode = 0644,
6755 .proc_handler = proc_dointvec,
6756 },
6757 {
6758 .procname = "accept_redirects",
6759 .data = &ipv6_devconf.accept_redirects,
6760 .maxlen = sizeof(int),
6761 .mode = 0644,
6762 .proc_handler = proc_dointvec,
6763 },
6764 {
6765 .procname = "autoconf",
6766 .data = &ipv6_devconf.autoconf,
6767 .maxlen = sizeof(int),
6768 .mode = 0644,
6769 .proc_handler = proc_dointvec,
6770 },
6771 {
6772 .procname = "dad_transmits",
6773 .data = &ipv6_devconf.dad_transmits,
6774 .maxlen = sizeof(int),
6775 .mode = 0644,
6776 .proc_handler = proc_dointvec,
6777 },
6778 {
6779 .procname = "router_solicitations",
6780 .data = &ipv6_devconf.rtr_solicits,
6781 .maxlen = sizeof(int),
6782 .mode = 0644,
6783 .proc_handler = proc_dointvec_minmax,
6784 .extra1 = &minus_one,
6785 },
6786 {
6787 .procname = "router_solicitation_interval",
6788 .data = &ipv6_devconf.rtr_solicit_interval,
6789 .maxlen = sizeof(int),
6790 .mode = 0644,
6791 .proc_handler = proc_dointvec_jiffies,
6792 },
6793 {
6794 .procname = "router_solicitation_max_interval",
6795 .data = &ipv6_devconf.rtr_solicit_max_interval,
6796 .maxlen = sizeof(int),
6797 .mode = 0644,
6798 .proc_handler = proc_dointvec_jiffies,
6799 },
6800 {
6801 .procname = "router_solicitation_delay",
6802 .data = &ipv6_devconf.rtr_solicit_delay,
6803 .maxlen = sizeof(int),
6804 .mode = 0644,
6805 .proc_handler = proc_dointvec_jiffies,
6806 },
6807 {
6808 .procname = "force_mld_version",
6809 .data = &ipv6_devconf.force_mld_version,
6810 .maxlen = sizeof(int),
6811 .mode = 0644,
6812 .proc_handler = proc_dointvec,
6813 },
6814 {
6815 .procname = "mldv1_unsolicited_report_interval",
6816 .data =
6817 &ipv6_devconf.mldv1_unsolicited_report_interval,
6818 .maxlen = sizeof(int),
6819 .mode = 0644,
6820 .proc_handler = proc_dointvec_ms_jiffies,
6821 },
6822 {
6823 .procname = "mldv2_unsolicited_report_interval",
6824 .data =
6825 &ipv6_devconf.mldv2_unsolicited_report_interval,
6826 .maxlen = sizeof(int),
6827 .mode = 0644,
6828 .proc_handler = proc_dointvec_ms_jiffies,
6829 },
6830 {
6831 .procname = "use_tempaddr",
6832 .data = &ipv6_devconf.use_tempaddr,
6833 .maxlen = sizeof(int),
6834 .mode = 0644,
6835 .proc_handler = proc_dointvec,
6836 },
6837 {
6838 .procname = "temp_valid_lft",
6839 .data = &ipv6_devconf.temp_valid_lft,
6840 .maxlen = sizeof(int),
6841 .mode = 0644,
6842 .proc_handler = proc_dointvec,
6843 },
6844 {
6845 .procname = "temp_prefered_lft",
6846 .data = &ipv6_devconf.temp_prefered_lft,
6847 .maxlen = sizeof(int),
6848 .mode = 0644,
6849 .proc_handler = proc_dointvec,
6850 },
6851 {
6852 .procname = "regen_min_advance",
6853 .data = &ipv6_devconf.regen_min_advance,
6854 .maxlen = sizeof(int),
6855 .mode = 0644,
6856 .proc_handler = proc_dointvec,
6857 },
6858 {
6859 .procname = "regen_max_retry",
6860 .data = &ipv6_devconf.regen_max_retry,
6861 .maxlen = sizeof(int),
6862 .mode = 0644,
6863 .proc_handler = proc_dointvec,
6864 },
6865 {
6866 .procname = "max_desync_factor",
6867 .data = &ipv6_devconf.max_desync_factor,
6868 .maxlen = sizeof(int),
6869 .mode = 0644,
6870 .proc_handler = proc_dointvec,
6871 },
6872 {
6873 .procname = "max_addresses",
6874 .data = &ipv6_devconf.max_addresses,
6875 .maxlen = sizeof(int),
6876 .mode = 0644,
6877 .proc_handler = proc_dointvec,
6878 },
6879 {
6880 .procname = "accept_ra_defrtr",
6881 .data = &ipv6_devconf.accept_ra_defrtr,
6882 .maxlen = sizeof(int),
6883 .mode = 0644,
6884 .proc_handler = proc_dointvec,
6885 },
6886 {
6887 .procname = "ra_defrtr_metric",
6888 .data = &ipv6_devconf.ra_defrtr_metric,
6889 .maxlen = sizeof(u32),
6890 .mode = 0644,
6891 .proc_handler = proc_douintvec_minmax,
6892 .extra1 = (void *)SYSCTL_ONE,
6893 },
6894 {
6895 .procname = "accept_ra_min_hop_limit",
6896 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6897 .maxlen = sizeof(int),
6898 .mode = 0644,
6899 .proc_handler = proc_dointvec,
6900 },
6901 {
6902 .procname = "accept_ra_min_lft",
6903 .data = &ipv6_devconf.accept_ra_min_lft,
6904 .maxlen = sizeof(int),
6905 .mode = 0644,
6906 .proc_handler = proc_dointvec,
6907 },
6908 {
6909 .procname = "accept_ra_pinfo",
6910 .data = &ipv6_devconf.accept_ra_pinfo,
6911 .maxlen = sizeof(int),
6912 .mode = 0644,
6913 .proc_handler = proc_dointvec,
6914 },
6915 {
6916 .procname = "ra_honor_pio_life",
6917 .data = &ipv6_devconf.ra_honor_pio_life,
6918 .maxlen = sizeof(u8),
6919 .mode = 0644,
6920 .proc_handler = proc_dou8vec_minmax,
6921 .extra1 = SYSCTL_ZERO,
6922 .extra2 = SYSCTL_ONE,
6923 },
6924 {
6925 .procname = "ra_honor_pio_pflag",
6926 .data = &ipv6_devconf.ra_honor_pio_pflag,
6927 .maxlen = sizeof(u8),
6928 .mode = 0644,
6929 .proc_handler = proc_dou8vec_minmax,
6930 .extra1 = SYSCTL_ZERO,
6931 .extra2 = SYSCTL_ONE,
6932 },
6933 #ifdef CONFIG_IPV6_ROUTER_PREF
6934 {
6935 .procname = "accept_ra_rtr_pref",
6936 .data = &ipv6_devconf.accept_ra_rtr_pref,
6937 .maxlen = sizeof(int),
6938 .mode = 0644,
6939 .proc_handler = proc_dointvec,
6940 },
6941 {
6942 .procname = "router_probe_interval",
6943 .data = &ipv6_devconf.rtr_probe_interval,
6944 .maxlen = sizeof(int),
6945 .mode = 0644,
6946 .proc_handler = proc_dointvec_jiffies,
6947 },
6948 #ifdef CONFIG_IPV6_ROUTE_INFO
6949 {
6950 .procname = "accept_ra_rt_info_min_plen",
6951 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6952 .maxlen = sizeof(int),
6953 .mode = 0644,
6954 .proc_handler = proc_dointvec,
6955 },
6956 {
6957 .procname = "accept_ra_rt_info_max_plen",
6958 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6959 .maxlen = sizeof(int),
6960 .mode = 0644,
6961 .proc_handler = proc_dointvec,
6962 },
6963 #endif
6964 #endif
6965 {
6966 .procname = "proxy_ndp",
6967 .data = &ipv6_devconf.proxy_ndp,
6968 .maxlen = sizeof(int),
6969 .mode = 0644,
6970 .proc_handler = addrconf_sysctl_proxy_ndp,
6971 },
6972 {
6973 .procname = "accept_source_route",
6974 .data = &ipv6_devconf.accept_source_route,
6975 .maxlen = sizeof(int),
6976 .mode = 0644,
6977 .proc_handler = proc_dointvec,
6978 },
6979 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6980 {
6981 .procname = "optimistic_dad",
6982 .data = &ipv6_devconf.optimistic_dad,
6983 .maxlen = sizeof(int),
6984 .mode = 0644,
6985 .proc_handler = proc_dointvec,
6986 },
6987 {
6988 .procname = "use_optimistic",
6989 .data = &ipv6_devconf.use_optimistic,
6990 .maxlen = sizeof(int),
6991 .mode = 0644,
6992 .proc_handler = proc_dointvec,
6993 },
6994 #endif
6995 #ifdef CONFIG_IPV6_MROUTE
6996 {
6997 .procname = "mc_forwarding",
6998 .data = &ipv6_devconf.mc_forwarding,
6999 .maxlen = sizeof(int),
7000 .mode = 0444,
7001 .proc_handler = proc_dointvec,
7002 },
7003 #endif
7004 {
7005 .procname = "disable_ipv6",
7006 .data = &ipv6_devconf.disable_ipv6,
7007 .maxlen = sizeof(int),
7008 .mode = 0644,
7009 .proc_handler = addrconf_sysctl_disable,
7010 },
7011 {
7012 .procname = "accept_dad",
7013 .data = &ipv6_devconf.accept_dad,
7014 .maxlen = sizeof(int),
7015 .mode = 0644,
7016 .proc_handler = proc_dointvec,
7017 },
7018 {
7019 .procname = "force_tllao",
7020 .data = &ipv6_devconf.force_tllao,
7021 .maxlen = sizeof(int),
7022 .mode = 0644,
7023 .proc_handler = proc_dointvec
7024 },
7025 {
7026 .procname = "ndisc_notify",
7027 .data = &ipv6_devconf.ndisc_notify,
7028 .maxlen = sizeof(int),
7029 .mode = 0644,
7030 .proc_handler = proc_dointvec
7031 },
7032 {
7033 .procname = "suppress_frag_ndisc",
7034 .data = &ipv6_devconf.suppress_frag_ndisc,
7035 .maxlen = sizeof(int),
7036 .mode = 0644,
7037 .proc_handler = proc_dointvec
7038 },
7039 {
7040 .procname = "accept_ra_from_local",
7041 .data = &ipv6_devconf.accept_ra_from_local,
7042 .maxlen = sizeof(int),
7043 .mode = 0644,
7044 .proc_handler = proc_dointvec,
7045 },
7046 {
7047 .procname = "accept_ra_mtu",
7048 .data = &ipv6_devconf.accept_ra_mtu,
7049 .maxlen = sizeof(int),
7050 .mode = 0644,
7051 .proc_handler = proc_dointvec,
7052 },
7053 {
7054 .procname = "stable_secret",
7055 .data = &ipv6_devconf.stable_secret,
7056 .maxlen = IPV6_MAX_STRLEN,
7057 .mode = 0600,
7058 .proc_handler = addrconf_sysctl_stable_secret,
7059 },
7060 {
7061 .procname = "use_oif_addrs_only",
7062 .data = &ipv6_devconf.use_oif_addrs_only,
7063 .maxlen = sizeof(int),
7064 .mode = 0644,
7065 .proc_handler = proc_dointvec,
7066 },
7067 {
7068 .procname = "ignore_routes_with_linkdown",
7069 .data = &ipv6_devconf.ignore_routes_with_linkdown,
7070 .maxlen = sizeof(int),
7071 .mode = 0644,
7072 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
7073 },
7074 {
7075 .procname = "drop_unicast_in_l2_multicast",
7076 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
7077 .maxlen = sizeof(int),
7078 .mode = 0644,
7079 .proc_handler = proc_dointvec,
7080 },
7081 {
7082 .procname = "drop_unsolicited_na",
7083 .data = &ipv6_devconf.drop_unsolicited_na,
7084 .maxlen = sizeof(int),
7085 .mode = 0644,
7086 .proc_handler = proc_dointvec,
7087 },
7088 {
7089 .procname = "keep_addr_on_down",
7090 .data = &ipv6_devconf.keep_addr_on_down,
7091 .maxlen = sizeof(int),
7092 .mode = 0644,
7093 .proc_handler = proc_dointvec,
7094
7095 },
7096 {
7097 .procname = "seg6_enabled",
7098 .data = &ipv6_devconf.seg6_enabled,
7099 .maxlen = sizeof(int),
7100 .mode = 0644,
7101 .proc_handler = proc_dointvec,
7102 },
7103 #ifdef CONFIG_IPV6_SEG6_HMAC
7104 {
7105 .procname = "seg6_require_hmac",
7106 .data = &ipv6_devconf.seg6_require_hmac,
7107 .maxlen = sizeof(int),
7108 .mode = 0644,
7109 .proc_handler = proc_dointvec,
7110 },
7111 #endif
7112 {
7113 .procname = "enhanced_dad",
7114 .data = &ipv6_devconf.enhanced_dad,
7115 .maxlen = sizeof(int),
7116 .mode = 0644,
7117 .proc_handler = proc_dointvec,
7118 },
7119 {
7120 .procname = "addr_gen_mode",
7121 .data = &ipv6_devconf.addr_gen_mode,
7122 .maxlen = sizeof(int),
7123 .mode = 0644,
7124 .proc_handler = addrconf_sysctl_addr_gen_mode,
7125 },
7126 {
7127 .procname = "disable_policy",
7128 .data = &ipv6_devconf.disable_policy,
7129 .maxlen = sizeof(int),
7130 .mode = 0644,
7131 .proc_handler = addrconf_sysctl_disable_policy,
7132 },
7133 {
7134 .procname = "ndisc_tclass",
7135 .data = &ipv6_devconf.ndisc_tclass,
7136 .maxlen = sizeof(int),
7137 .mode = 0644,
7138 .proc_handler = proc_dointvec_minmax,
7139 .extra1 = (void *)SYSCTL_ZERO,
7140 .extra2 = (void *)&two_five_five,
7141 },
7142 {
7143 .procname = "rpl_seg_enabled",
7144 .data = &ipv6_devconf.rpl_seg_enabled,
7145 .maxlen = sizeof(int),
7146 .mode = 0644,
7147 .proc_handler = proc_dointvec,
7148 },
7149 {
7150 .procname = "ioam6_enabled",
7151 .data = &ipv6_devconf.ioam6_enabled,
7152 .maxlen = sizeof(u8),
7153 .mode = 0644,
7154 .proc_handler = proc_dou8vec_minmax,
7155 .extra1 = (void *)SYSCTL_ZERO,
7156 .extra2 = (void *)SYSCTL_ONE,
7157 },
7158 {
7159 .procname = "ioam6_id",
7160 .data = &ipv6_devconf.ioam6_id,
7161 .maxlen = sizeof(u32),
7162 .mode = 0644,
7163 .proc_handler = proc_douintvec_minmax,
7164 .extra1 = (void *)SYSCTL_ZERO,
7165 .extra2 = (void *)&ioam6_if_id_max,
7166 },
7167 {
7168 .procname = "ioam6_id_wide",
7169 .data = &ipv6_devconf.ioam6_id_wide,
7170 .maxlen = sizeof(u32),
7171 .mode = 0644,
7172 .proc_handler = proc_douintvec,
7173 },
7174 {
7175 .procname = "ndisc_evict_nocarrier",
7176 .data = &ipv6_devconf.ndisc_evict_nocarrier,
7177 .maxlen = sizeof(u8),
7178 .mode = 0644,
7179 .proc_handler = proc_dou8vec_minmax,
7180 .extra1 = (void *)SYSCTL_ZERO,
7181 .extra2 = (void *)SYSCTL_ONE,
7182 },
7183 {
7184 .procname = "accept_untracked_na",
7185 .data = &ipv6_devconf.accept_untracked_na,
7186 .maxlen = sizeof(int),
7187 .mode = 0644,
7188 .proc_handler = proc_dointvec_minmax,
7189 .extra1 = SYSCTL_ZERO,
7190 .extra2 = SYSCTL_TWO,
7191 },
7192 };
7193
__addrconf_sysctl_register(struct net * net,char * dev_name,struct inet6_dev * idev,struct ipv6_devconf * p)7194 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7195 struct inet6_dev *idev, struct ipv6_devconf *p)
7196 {
7197 size_t table_size = ARRAY_SIZE(addrconf_sysctl);
7198 int i, ifindex;
7199 struct ctl_table *table;
7200 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
7201
7202 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7203 if (!table)
7204 goto out;
7205
7206 for (i = 0; i < table_size; i++) {
7207 table[i].data += (char *)p - (char *)&ipv6_devconf;
7208 /* If one of these is already set, then it is not safe to
7209 * overwrite either of them: this makes proc_dointvec_minmax
7210 * usable.
7211 */
7212 if (!table[i].extra1 && !table[i].extra2) {
7213 table[i].extra1 = idev; /* embedded; no ref */
7214 table[i].extra2 = net;
7215 }
7216 }
7217
7218 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
7219
7220 p->sysctl_header = register_net_sysctl_sz(net, path, table,
7221 table_size);
7222 if (!p->sysctl_header)
7223 goto free;
7224
7225 if (!strcmp(dev_name, "all"))
7226 ifindex = NETCONFA_IFINDEX_ALL;
7227 else if (!strcmp(dev_name, "default"))
7228 ifindex = NETCONFA_IFINDEX_DEFAULT;
7229 else
7230 ifindex = idev->dev->ifindex;
7231 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
7232 ifindex, p);
7233 return 0;
7234
7235 free:
7236 kfree(table);
7237 out:
7238 return -ENOBUFS;
7239 }
7240
__addrconf_sysctl_unregister(struct net * net,struct ipv6_devconf * p,int ifindex)7241 static void __addrconf_sysctl_unregister(struct net *net,
7242 struct ipv6_devconf *p, int ifindex)
7243 {
7244 const struct ctl_table *table;
7245
7246 if (!p->sysctl_header)
7247 return;
7248
7249 table = p->sysctl_header->ctl_table_arg;
7250 unregister_net_sysctl_table(p->sysctl_header);
7251 p->sysctl_header = NULL;
7252 kfree(table);
7253
7254 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
7255 }
7256
addrconf_sysctl_register(struct inet6_dev * idev)7257 static int addrconf_sysctl_register(struct inet6_dev *idev)
7258 {
7259 int err;
7260
7261 if (!sysctl_dev_name_is_allowed(idev->dev->name))
7262 return -EINVAL;
7263
7264 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7265 &ndisc_ifinfo_sysctl_change);
7266 if (err)
7267 return err;
7268 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7269 idev, &idev->cnf);
7270 if (err)
7271 neigh_sysctl_unregister(idev->nd_parms);
7272
7273 return err;
7274 }
7275
addrconf_sysctl_unregister(struct inet6_dev * idev)7276 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7277 {
7278 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
7279 idev->dev->ifindex);
7280 neigh_sysctl_unregister(idev->nd_parms);
7281 }
7282
7283
7284 #endif
7285
addrconf_init_net(struct net * net)7286 static int __net_init addrconf_init_net(struct net *net)
7287 {
7288 int err = -ENOMEM;
7289 struct ipv6_devconf *all, *dflt;
7290
7291 spin_lock_init(&net->ipv6.addrconf_hash_lock);
7292 INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
7293 net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
7294 sizeof(struct hlist_head),
7295 GFP_KERNEL);
7296 if (!net->ipv6.inet6_addr_lst)
7297 goto err_alloc_addr;
7298
7299 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
7300 if (!all)
7301 goto err_alloc_all;
7302
7303 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
7304 if (!dflt)
7305 goto err_alloc_dflt;
7306
7307 if (!net_eq(net, &init_net)) {
7308 switch (net_inherit_devconf()) {
7309 case 1: /* copy from init_net */
7310 memcpy(all, init_net.ipv6.devconf_all,
7311 sizeof(ipv6_devconf));
7312 memcpy(dflt, init_net.ipv6.devconf_dflt,
7313 sizeof(ipv6_devconf_dflt));
7314 break;
7315 case 3: /* copy from the current netns */
7316 memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
7317 sizeof(ipv6_devconf));
7318 memcpy(dflt,
7319 current->nsproxy->net_ns->ipv6.devconf_dflt,
7320 sizeof(ipv6_devconf_dflt));
7321 break;
7322 case 0:
7323 case 2:
7324 /* use compiled values */
7325 break;
7326 }
7327 }
7328
7329 /* these will be inherited by all namespaces */
7330 dflt->autoconf = ipv6_defaults.autoconf;
7331 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7332
7333 dflt->stable_secret.initialized = false;
7334 all->stable_secret.initialized = false;
7335
7336 net->ipv6.devconf_all = all;
7337 net->ipv6.devconf_dflt = dflt;
7338
7339 #ifdef CONFIG_SYSCTL
7340 err = __addrconf_sysctl_register(net, "all", NULL, all);
7341 if (err < 0)
7342 goto err_reg_all;
7343
7344 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7345 if (err < 0)
7346 goto err_reg_dflt;
7347 #endif
7348 return 0;
7349
7350 #ifdef CONFIG_SYSCTL
7351 err_reg_dflt:
7352 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7353 err_reg_all:
7354 kfree(dflt);
7355 net->ipv6.devconf_dflt = NULL;
7356 #endif
7357 err_alloc_dflt:
7358 kfree(all);
7359 net->ipv6.devconf_all = NULL;
7360 err_alloc_all:
7361 kfree(net->ipv6.inet6_addr_lst);
7362 err_alloc_addr:
7363 return err;
7364 }
7365
addrconf_exit_net(struct net * net)7366 static void __net_exit addrconf_exit_net(struct net *net)
7367 {
7368 int i;
7369
7370 #ifdef CONFIG_SYSCTL
7371 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7372 NETCONFA_IFINDEX_DEFAULT);
7373 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7374 NETCONFA_IFINDEX_ALL);
7375 #endif
7376 kfree(net->ipv6.devconf_dflt);
7377 net->ipv6.devconf_dflt = NULL;
7378 kfree(net->ipv6.devconf_all);
7379 net->ipv6.devconf_all = NULL;
7380
7381 cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
7382 /*
7383 * Check hash table, then free it.
7384 */
7385 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7386 WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7387
7388 kfree(net->ipv6.inet6_addr_lst);
7389 net->ipv6.inet6_addr_lst = NULL;
7390 }
7391
7392 static struct pernet_operations addrconf_ops = {
7393 .init = addrconf_init_net,
7394 .exit = addrconf_exit_net,
7395 };
7396
7397 static struct rtnl_af_ops inet6_ops __read_mostly = {
7398 .family = AF_INET6,
7399 .fill_link_af = inet6_fill_link_af,
7400 .get_link_af_size = inet6_get_link_af_size,
7401 .validate_link_af = inet6_validate_link_af,
7402 .set_link_af = inet6_set_link_af,
7403 };
7404
7405 static const struct rtnl_msg_handler addrconf_rtnl_msg_handlers[] __initconst_or_module = {
7406 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETLINK,
7407 .dumpit = inet6_dump_ifinfo, .flags = RTNL_FLAG_DUMP_UNLOCKED},
7408 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWADDR,
7409 .doit = inet6_rtm_newaddr, .flags = RTNL_FLAG_DOIT_PERNET},
7410 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELADDR,
7411 .doit = inet6_rtm_deladdr, .flags = RTNL_FLAG_DOIT_PERNET},
7412 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETADDR,
7413 .doit = inet6_rtm_getaddr, .dumpit = inet6_dump_ifaddr,
7414 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
7415 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETMULTICAST,
7416 .dumpit = inet6_dump_ifmcaddr,
7417 .flags = RTNL_FLAG_DUMP_UNLOCKED},
7418 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETANYCAST,
7419 .dumpit = inet6_dump_ifacaddr,
7420 .flags = RTNL_FLAG_DUMP_UNLOCKED},
7421 {.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETNETCONF,
7422 .doit = inet6_netconf_get_devconf, .dumpit = inet6_netconf_dump_devconf,
7423 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
7424 };
7425
7426 /*
7427 * Init / cleanup code
7428 */
7429
addrconf_init(void)7430 int __init addrconf_init(void)
7431 {
7432 struct inet6_dev *idev;
7433 int err;
7434
7435 err = ipv6_addr_label_init();
7436 if (err < 0) {
7437 pr_crit("%s: cannot initialize default policy table: %d\n",
7438 __func__, err);
7439 goto out;
7440 }
7441
7442 err = register_pernet_subsys(&addrconf_ops);
7443 if (err < 0)
7444 goto out_addrlabel;
7445
7446 /* All works using addrconf_wq need to lock rtnl. */
7447 addrconf_wq = create_singlethread_workqueue("ipv6_addrconf");
7448 if (!addrconf_wq) {
7449 err = -ENOMEM;
7450 goto out_nowq;
7451 }
7452
7453 rtnl_net_lock(&init_net);
7454 idev = ipv6_add_dev(blackhole_netdev);
7455 rtnl_net_unlock(&init_net);
7456 if (IS_ERR(idev)) {
7457 err = PTR_ERR(idev);
7458 goto errlo;
7459 }
7460
7461 ip6_route_init_special_entries();
7462
7463 register_netdevice_notifier(&ipv6_dev_notf);
7464
7465 addrconf_verify(&init_net);
7466
7467 err = rtnl_af_register(&inet6_ops);
7468 if (err)
7469 goto erraf;
7470
7471 err = rtnl_register_many(addrconf_rtnl_msg_handlers);
7472 if (err)
7473 goto errout;
7474
7475 err = ipv6_addr_label_rtnl_register();
7476 if (err < 0)
7477 goto errout;
7478
7479 return 0;
7480 errout:
7481 rtnl_unregister_all(PF_INET6);
7482 rtnl_af_unregister(&inet6_ops);
7483 erraf:
7484 unregister_netdevice_notifier(&ipv6_dev_notf);
7485 errlo:
7486 destroy_workqueue(addrconf_wq);
7487 out_nowq:
7488 unregister_pernet_subsys(&addrconf_ops);
7489 out_addrlabel:
7490 ipv6_addr_label_cleanup();
7491 out:
7492 return err;
7493 }
7494
addrconf_cleanup(void)7495 void addrconf_cleanup(void)
7496 {
7497 struct net_device *dev;
7498
7499 unregister_netdevice_notifier(&ipv6_dev_notf);
7500 unregister_pernet_subsys(&addrconf_ops);
7501 ipv6_addr_label_cleanup();
7502
7503 rtnl_af_unregister(&inet6_ops);
7504
7505 rtnl_net_lock(&init_net);
7506
7507 /* clean dev list */
7508 for_each_netdev(&init_net, dev) {
7509 if (!__in6_dev_get_rtnl_net(dev))
7510 continue;
7511 addrconf_ifdown(dev, true);
7512 }
7513 addrconf_ifdown(init_net.loopback_dev, true);
7514
7515 rtnl_net_unlock(&init_net);
7516
7517 destroy_workqueue(addrconf_wq);
7518 }
7519