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