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