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