1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPv6 Address [auto]configuration
4 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
14 * Janos Farkas : delete timer on ifdown
15 * <chexum@bankinf.banki.hu>
16 * Andi Kleen : kill double kfree on module
18 * Maciej W. Rozycki : FDDI support
19 * sekiya@USAGI : Don't send too many RS
21 * yoshfuji@USAGI : Fixed interval between DAD
23 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
24 * address validation timer.
25 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
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
32 * YOSHIFUJI Hideaki @USAGI : improved source address
33 * selection; consider scope,
37 #define pr_fmt(fmt) "IPv6: " fmt
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>
58 #include <linux/sysctl.h>
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>
66 #include <net/net_namespace.h>
70 #include <net/6lowpan.h>
71 #include <net/firewire.h>
73 #include <net/protocol.h>
74 #include <net/ndisc.h>
75 #include <net/ip6_route.h>
76 #include <net/addrconf.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>
89 #include <linux/proc_fs.h>
90 #include <linux/seq_file.h>
91 #include <linux/export.h>
92 #include <linux/ioam6.h>
94 #define INFINITY_LIFE_TIME 0xFFFFFFFF
96 #define IPV6_MAX_STRLEN \
97 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
99 static inline u32 cstamp_delta(unsigned long cstamp)
101 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
104 static inline s32 rfc3315_s14_backoff_init(s32 irt)
106 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
107 u64 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)irt;
108 do_div(tmp, 1000000);
112 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
114 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
115 u64 tmp = get_random_u32_inclusive(1900000, 2100000) * (u64)rt;
116 do_div(tmp, 1000000);
117 if ((s32)tmp > mrt) {
118 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
119 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)mrt;
120 do_div(tmp, 1000000);
126 static int addrconf_sysctl_register(struct inet6_dev *idev);
127 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
129 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
134 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
139 static void ipv6_gen_rnd_iid(struct in6_addr *addr);
141 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
142 static int ipv6_count_addresses(const struct inet6_dev *idev);
143 static int ipv6_generate_stable_address(struct in6_addr *addr,
145 const struct inet6_dev *idev);
147 #define IN6_ADDR_HSIZE_SHIFT 8
148 #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
150 static void addrconf_verify(struct net *net);
151 static void addrconf_verify_rtnl(struct net *net);
153 static struct workqueue_struct *addrconf_wq;
155 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
156 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
158 static void addrconf_type_change(struct net_device *dev,
159 unsigned long event);
160 static int addrconf_ifdown(struct net_device *dev, bool unregister);
162 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
164 const struct net_device *dev,
165 u32 flags, u32 noflags,
168 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
169 static void addrconf_dad_work(struct work_struct *w);
170 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
172 static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
173 static void addrconf_rs_timer(struct timer_list *t);
174 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
175 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
177 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
178 struct prefix_info *pinfo);
180 static struct ipv6_devconf ipv6_devconf __read_mostly = {
182 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
183 .mtu6 = IPV6_MIN_MTU,
185 .accept_redirects = 1,
187 .force_mld_version = 0,
188 .mldv1_unsolicited_report_interval = 10 * HZ,
189 .mldv2_unsolicited_report_interval = HZ,
191 .rtr_solicits = MAX_RTR_SOLICITATIONS,
192 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
193 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
194 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
196 .temp_valid_lft = TEMP_VALID_LIFETIME,
197 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
198 .regen_max_retry = REGEN_MAX_RETRY,
199 .max_desync_factor = MAX_DESYNC_FACTOR,
200 .max_addresses = IPV6_MAX_ADDRESSES,
201 .accept_ra_defrtr = 1,
202 .ra_defrtr_metric = IP6_RT_PRIO_USER,
203 .accept_ra_from_local = 0,
204 .accept_ra_min_hop_limit= 1,
205 .accept_ra_pinfo = 1,
206 #ifdef CONFIG_IPV6_ROUTER_PREF
207 .accept_ra_rtr_pref = 1,
208 .rtr_probe_interval = 60 * HZ,
209 #ifdef CONFIG_IPV6_ROUTE_INFO
210 .accept_ra_rt_info_min_plen = 0,
211 .accept_ra_rt_info_max_plen = 0,
215 .accept_source_route = 0, /* we do not accept RH0 by default. */
218 .suppress_frag_ndisc = 1,
221 .initialized = false,
223 .use_oif_addrs_only = 0,
224 .ignore_routes_with_linkdown = 0,
225 .keep_addr_on_down = 0,
227 #ifdef CONFIG_IPV6_SEG6_HMAC
228 .seg6_require_hmac = 0,
231 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
233 .rpl_seg_enabled = 0,
235 .ioam6_id = IOAM6_DEFAULT_IF_ID,
236 .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
237 .ndisc_evict_nocarrier = 1,
240 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
242 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
243 .mtu6 = IPV6_MIN_MTU,
245 .accept_redirects = 1,
247 .force_mld_version = 0,
248 .mldv1_unsolicited_report_interval = 10 * HZ,
249 .mldv2_unsolicited_report_interval = HZ,
251 .rtr_solicits = MAX_RTR_SOLICITATIONS,
252 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
253 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
254 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
256 .temp_valid_lft = TEMP_VALID_LIFETIME,
257 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
258 .regen_max_retry = REGEN_MAX_RETRY,
259 .max_desync_factor = MAX_DESYNC_FACTOR,
260 .max_addresses = IPV6_MAX_ADDRESSES,
261 .accept_ra_defrtr = 1,
262 .ra_defrtr_metric = IP6_RT_PRIO_USER,
263 .accept_ra_from_local = 0,
264 .accept_ra_min_hop_limit= 1,
265 .accept_ra_pinfo = 1,
266 #ifdef CONFIG_IPV6_ROUTER_PREF
267 .accept_ra_rtr_pref = 1,
268 .rtr_probe_interval = 60 * HZ,
269 #ifdef CONFIG_IPV6_ROUTE_INFO
270 .accept_ra_rt_info_min_plen = 0,
271 .accept_ra_rt_info_max_plen = 0,
275 .accept_source_route = 0, /* we do not accept RH0 by default. */
278 .suppress_frag_ndisc = 1,
281 .initialized = false,
283 .use_oif_addrs_only = 0,
284 .ignore_routes_with_linkdown = 0,
285 .keep_addr_on_down = 0,
287 #ifdef CONFIG_IPV6_SEG6_HMAC
288 .seg6_require_hmac = 0,
291 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
293 .rpl_seg_enabled = 0,
295 .ioam6_id = IOAM6_DEFAULT_IF_ID,
296 .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
297 .ndisc_evict_nocarrier = 1,
300 /* Check if link is ready: is it up and is a valid qdisc available */
301 static inline bool addrconf_link_ready(const struct net_device *dev)
303 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
306 static void addrconf_del_rs_timer(struct inet6_dev *idev)
308 if (del_timer(&idev->rs_timer))
312 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
314 if (cancel_delayed_work(&ifp->dad_work))
318 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
321 if (!mod_timer(&idev->rs_timer, jiffies + when))
325 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
329 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
333 static int snmp6_alloc_dev(struct inet6_dev *idev)
337 idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT);
338 if (!idev->stats.ipv6)
341 for_each_possible_cpu(i) {
342 struct ipstats_mib *addrconf_stats;
343 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
344 u64_stats_init(&addrconf_stats->syncp);
348 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
350 if (!idev->stats.icmpv6dev)
352 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
354 if (!idev->stats.icmpv6msgdev)
360 kfree(idev->stats.icmpv6dev);
362 free_percpu(idev->stats.ipv6);
367 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
369 struct inet6_dev *ndev;
374 if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev)
375 return ERR_PTR(-EINVAL);
377 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL_ACCOUNT);
381 rwlock_init(&ndev->lock);
383 INIT_LIST_HEAD(&ndev->addr_list);
384 timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
385 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
387 if (ndev->cnf.stable_secret.initialized)
388 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
390 ndev->cnf.mtu6 = dev->mtu;
392 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
393 if (!ndev->nd_parms) {
397 if (ndev->cnf.forwarding)
398 dev_disable_lro(dev);
399 /* We refer to the device */
400 netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL);
402 if (snmp6_alloc_dev(ndev) < 0) {
403 netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
405 neigh_parms_release(&nd_tbl, ndev->nd_parms);
406 netdev_put(dev, &ndev->dev_tracker);
411 if (dev != blackhole_netdev) {
412 if (snmp6_register_dev(ndev) < 0) {
413 netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
414 __func__, dev->name);
418 /* One reference from device. */
419 refcount_set(&ndev->refcnt, 1);
421 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
422 ndev->cnf.accept_dad = -1;
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;
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;
441 ndev->token = in6addr_any;
443 if (netif_running(dev) && addrconf_link_ready(dev))
444 ndev->if_flags |= IF_READY;
446 ipv6_mc_init_dev(ndev);
447 ndev->tstamp = jiffies;
448 if (dev != blackhole_netdev) {
449 err = addrconf_sysctl_register(ndev);
451 ipv6_mc_destroy_dev(ndev);
452 snmp6_unregister_dev(ndev);
456 /* protected by rtnl_lock */
457 rcu_assign_pointer(dev->ip6_ptr, ndev);
459 if (dev != blackhole_netdev) {
460 /* Join interface-local all-node multicast group */
461 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
463 /* Join all-node multicast group */
464 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
466 /* Join all-router multicast group if forwarding is set */
467 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
468 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
473 neigh_parms_release(&nd_tbl, ndev->nd_parms);
475 in6_dev_finish_destroy(ndev);
479 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
481 struct inet6_dev *idev;
485 idev = __in6_dev_get(dev);
487 idev = ipv6_add_dev(dev);
492 if (dev->flags&IFF_UP)
497 static int inet6_netconf_msgsize_devconf(int type)
499 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
500 + nla_total_size(4); /* NETCONFA_IFINDEX */
503 if (type == NETCONFA_ALL)
506 if (all || type == NETCONFA_FORWARDING)
507 size += nla_total_size(4);
508 #ifdef CONFIG_IPV6_MROUTE
509 if (all || type == NETCONFA_MC_FORWARDING)
510 size += nla_total_size(4);
512 if (all || type == NETCONFA_PROXY_NEIGH)
513 size += nla_total_size(4);
515 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
516 size += nla_total_size(4);
521 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
522 struct ipv6_devconf *devconf, u32 portid,
523 u32 seq, int event, unsigned int flags,
526 struct nlmsghdr *nlh;
527 struct netconfmsg *ncm;
530 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
535 if (type == NETCONFA_ALL)
538 ncm = nlmsg_data(nlh);
539 ncm->ncm_family = AF_INET6;
541 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
542 goto nla_put_failure;
547 if ((all || type == NETCONFA_FORWARDING) &&
548 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
549 goto nla_put_failure;
550 #ifdef CONFIG_IPV6_MROUTE
551 if ((all || type == NETCONFA_MC_FORWARDING) &&
552 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
553 atomic_read(&devconf->mc_forwarding)) < 0)
554 goto nla_put_failure;
556 if ((all || type == NETCONFA_PROXY_NEIGH) &&
557 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
558 goto nla_put_failure;
560 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
561 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
562 devconf->ignore_routes_with_linkdown) < 0)
563 goto nla_put_failure;
570 nlmsg_cancel(skb, nlh);
574 void inet6_netconf_notify_devconf(struct net *net, int event, int type,
575 int ifindex, struct ipv6_devconf *devconf)
580 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
584 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
587 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
588 WARN_ON(err == -EMSGSIZE);
592 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
595 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
598 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
599 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
600 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
601 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
602 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
605 static int inet6_netconf_valid_get_req(struct sk_buff *skb,
606 const struct nlmsghdr *nlh,
608 struct netlink_ext_ack *extack)
612 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
613 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
617 if (!netlink_strict_get_check(skb))
618 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
620 devconf_ipv6_policy, extack);
622 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
624 devconf_ipv6_policy, extack);
628 for (i = 0; i <= NETCONFA_MAX; i++) {
633 case NETCONFA_IFINDEX:
636 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
644 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
645 struct nlmsghdr *nlh,
646 struct netlink_ext_ack *extack)
648 struct net *net = sock_net(in_skb->sk);
649 struct nlattr *tb[NETCONFA_MAX+1];
650 struct inet6_dev *in6_dev = NULL;
651 struct net_device *dev = NULL;
653 struct ipv6_devconf *devconf;
657 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
661 if (!tb[NETCONFA_IFINDEX])
665 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
667 case NETCONFA_IFINDEX_ALL:
668 devconf = net->ipv6.devconf_all;
670 case NETCONFA_IFINDEX_DEFAULT:
671 devconf = net->ipv6.devconf_dflt;
674 dev = dev_get_by_index(net, ifindex);
677 in6_dev = in6_dev_get(dev);
680 devconf = &in6_dev->cnf;
685 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
689 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
690 NETLINK_CB(in_skb).portid,
691 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
694 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
695 WARN_ON(err == -EMSGSIZE);
699 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
702 in6_dev_put(in6_dev);
707 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
708 struct netlink_callback *cb)
710 const struct nlmsghdr *nlh = cb->nlh;
711 struct net *net = sock_net(skb->sk);
714 struct net_device *dev;
715 struct inet6_dev *idev;
716 struct hlist_head *head;
718 if (cb->strict_check) {
719 struct netlink_ext_ack *extack = cb->extack;
720 struct netconfmsg *ncm;
722 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
723 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
727 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
728 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
734 s_idx = idx = cb->args[1];
736 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
738 head = &net->dev_index_head[h];
740 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
742 hlist_for_each_entry_rcu(dev, head, index_hlist) {
745 idev = __in6_dev_get(dev);
749 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
751 NETLINK_CB(cb->skb).portid,
759 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
765 if (h == NETDEV_HASHENTRIES) {
766 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
767 net->ipv6.devconf_all,
768 NETLINK_CB(cb->skb).portid,
770 RTM_NEWNETCONF, NLM_F_MULTI,
776 if (h == NETDEV_HASHENTRIES + 1) {
777 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
778 net->ipv6.devconf_dflt,
779 NETLINK_CB(cb->skb).portid,
781 RTM_NEWNETCONF, NLM_F_MULTI,
795 static void dev_forward_change(struct inet6_dev *idev)
797 struct net_device *dev;
798 struct inet6_ifaddr *ifa;
799 LIST_HEAD(tmp_addr_list);
804 if (idev->cnf.forwarding)
805 dev_disable_lro(dev);
806 if (dev->flags & IFF_MULTICAST) {
807 if (idev->cnf.forwarding) {
808 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
809 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
810 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
812 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
813 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
814 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
818 read_lock_bh(&idev->lock);
819 list_for_each_entry(ifa, &idev->addr_list, if_list) {
820 if (ifa->flags&IFA_F_TENTATIVE)
822 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
824 read_unlock_bh(&idev->lock);
826 while (!list_empty(&tmp_addr_list)) {
827 ifa = list_first_entry(&tmp_addr_list,
828 struct inet6_ifaddr, if_list_aux);
829 list_del(&ifa->if_list_aux);
830 if (idev->cnf.forwarding)
831 addrconf_join_anycast(ifa);
833 addrconf_leave_anycast(ifa);
836 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
838 dev->ifindex, &idev->cnf);
842 static void addrconf_forward_change(struct net *net, __s32 newf)
844 struct net_device *dev;
845 struct inet6_dev *idev;
847 for_each_netdev(net, dev) {
848 idev = __in6_dev_get(dev);
850 int changed = (!idev->cnf.forwarding) ^ (!newf);
851 idev->cnf.forwarding = newf;
853 dev_forward_change(idev);
858 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
864 return restart_syscall();
866 net = (struct net *)table->extra2;
870 if (p == &net->ipv6.devconf_dflt->forwarding) {
871 if ((!newf) ^ (!old))
872 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
874 NETCONFA_IFINDEX_DEFAULT,
875 net->ipv6.devconf_dflt);
880 if (p == &net->ipv6.devconf_all->forwarding) {
881 int old_dflt = net->ipv6.devconf_dflt->forwarding;
883 net->ipv6.devconf_dflt->forwarding = newf;
884 if ((!newf) ^ (!old_dflt))
885 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
887 NETCONFA_IFINDEX_DEFAULT,
888 net->ipv6.devconf_dflt);
890 addrconf_forward_change(net, newf);
891 if ((!newf) ^ (!old))
892 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
894 NETCONFA_IFINDEX_ALL,
895 net->ipv6.devconf_all);
896 } else if ((!newf) ^ (!old))
897 dev_forward_change((struct inet6_dev *)table->extra1);
901 rt6_purge_dflt_routers(net);
905 static void addrconf_linkdown_change(struct net *net, __s32 newf)
907 struct net_device *dev;
908 struct inet6_dev *idev;
910 for_each_netdev(net, dev) {
911 idev = __in6_dev_get(dev);
913 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
915 idev->cnf.ignore_routes_with_linkdown = newf;
917 inet6_netconf_notify_devconf(dev_net(dev),
919 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
926 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
932 return restart_syscall();
934 net = (struct net *)table->extra2;
938 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
939 if ((!newf) ^ (!old))
940 inet6_netconf_notify_devconf(net,
942 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
943 NETCONFA_IFINDEX_DEFAULT,
944 net->ipv6.devconf_dflt);
949 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
950 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
951 addrconf_linkdown_change(net, newf);
952 if ((!newf) ^ (!old))
953 inet6_netconf_notify_devconf(net,
955 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
956 NETCONFA_IFINDEX_ALL,
957 net->ipv6.devconf_all);
966 /* Nobody refers to this ifaddr, destroy it */
967 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
969 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
971 #ifdef NET_REFCNT_DEBUG
972 pr_debug("%s\n", __func__);
975 in6_dev_put(ifp->idev);
977 if (cancel_delayed_work(&ifp->dad_work))
978 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
981 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
982 pr_warn("Freeing alive inet6 address %p\n", ifp);
990 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
993 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
996 * Each device address list is sorted in order of scope -
997 * global before linklocal.
999 list_for_each(p, &idev->addr_list) {
1000 struct inet6_ifaddr *ifa
1001 = list_entry(p, struct inet6_ifaddr, if_list);
1002 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
1006 list_add_tail_rcu(&ifp->if_list, p);
1009 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
1011 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
1013 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
1016 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1017 struct net_device *dev, unsigned int hash)
1019 struct inet6_ifaddr *ifp;
1021 hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1022 if (ipv6_addr_equal(&ifp->addr, addr)) {
1023 if (!dev || ifp->idev->dev == dev)
1030 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1032 struct net *net = dev_net(dev);
1033 unsigned int hash = inet6_addr_hash(net, &ifa->addr);
1036 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1038 /* Ignore adding duplicate addresses on an interface */
1039 if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) {
1040 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1043 hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]);
1046 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1051 /* On success it returns ifp with increased reference count */
1053 static struct inet6_ifaddr *
1054 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1055 bool can_block, struct netlink_ext_ack *extack)
1057 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1058 int addr_type = ipv6_addr_type(cfg->pfx);
1059 struct net *net = dev_net(idev->dev);
1060 struct inet6_ifaddr *ifa = NULL;
1061 struct fib6_info *f6i = NULL;
1064 if (addr_type == IPV6_ADDR_ANY ||
1065 (addr_type & IPV6_ADDR_MULTICAST &&
1066 !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
1067 (!(idev->dev->flags & IFF_LOOPBACK) &&
1068 !netif_is_l3_master(idev->dev) &&
1069 addr_type & IPV6_ADDR_LOOPBACK))
1070 return ERR_PTR(-EADDRNOTAVAIL);
1073 err = -ENODEV; /*XXX*/
1077 if (idev->cnf.disable_ipv6) {
1082 /* validator notifier needs to be blocking;
1083 * do not call in atomic context
1086 struct in6_validator_info i6vi = {
1087 .i6vi_addr = *cfg->pfx,
1092 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1093 err = notifier_to_errno(err);
1098 ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
1104 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1111 neigh_parms_data_state_setall(idev->nd_parms);
1113 ifa->addr = *cfg->pfx;
1115 ifa->peer_addr = *cfg->peer_pfx;
1117 spin_lock_init(&ifa->lock);
1118 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1119 INIT_HLIST_NODE(&ifa->addr_lst);
1120 ifa->scope = cfg->scope;
1121 ifa->prefix_len = cfg->plen;
1122 ifa->rt_priority = cfg->rt_priority;
1123 ifa->flags = cfg->ifa_flags;
1124 ifa->ifa_proto = cfg->ifa_proto;
1125 /* No need to add the TENTATIVE flag for addresses with NODAD */
1126 if (!(cfg->ifa_flags & IFA_F_NODAD))
1127 ifa->flags |= IFA_F_TENTATIVE;
1128 ifa->valid_lft = cfg->valid_lft;
1129 ifa->prefered_lft = cfg->preferred_lft;
1130 ifa->cstamp = ifa->tstamp = jiffies;
1131 ifa->tokenized = false;
1139 refcount_set(&ifa->refcnt, 1);
1143 err = ipv6_add_addr_hash(idev->dev, ifa);
1149 write_lock_bh(&idev->lock);
1151 /* Add to inet6_dev unicast addr list. */
1152 ipv6_link_dev_addr(idev, ifa);
1154 if (ifa->flags&IFA_F_TEMPORARY) {
1155 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1160 write_unlock_bh(&idev->lock);
1164 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1166 if (unlikely(err < 0)) {
1167 fib6_info_release(f6i);
1171 in6_dev_put(ifa->idev);
1180 enum cleanup_prefix_rt_t {
1181 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1182 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1183 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1187 * Check, whether the prefix for ifp would still need a prefix route
1188 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1191 * 1) we don't purge prefix if address was not permanent.
1192 * prefix is managed by its own lifetime.
1193 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1194 * 3) if there are no addresses, delete prefix.
1195 * 4) if there are still other permanent address(es),
1196 * corresponding prefix is still permanent.
1197 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1198 * don't purge the prefix, assume user space is managing it.
1199 * 6) otherwise, update prefix lifetime to the
1200 * longest valid lifetime among the corresponding
1201 * addresses on the device.
1202 * Note: subsequent RA will update lifetime.
1204 static enum cleanup_prefix_rt_t
1205 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1207 struct inet6_ifaddr *ifa;
1208 struct inet6_dev *idev = ifp->idev;
1209 unsigned long lifetime;
1210 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1214 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1217 if (ifa->prefix_len != ifp->prefix_len ||
1218 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1221 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1222 return CLEANUP_PREFIX_RT_NOP;
1224 action = CLEANUP_PREFIX_RT_EXPIRE;
1226 spin_lock(&ifa->lock);
1228 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1230 * Note: Because this address is
1231 * not permanent, lifetime <
1232 * LONG_MAX / HZ here.
1234 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1235 *expires = ifa->tstamp + lifetime * HZ;
1236 spin_unlock(&ifa->lock);
1243 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1244 bool del_rt, bool del_peer)
1246 struct fib6_info *f6i;
1248 f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1250 ifp->idev->dev, 0, RTF_DEFAULT, true);
1253 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
1255 if (!(f6i->fib6_flags & RTF_EXPIRES))
1256 fib6_set_expires(f6i, expires);
1257 fib6_info_release(f6i);
1263 /* This function wants to get referenced ifp and releases it before return */
1265 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1267 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1268 struct net *net = dev_net(ifp->idev->dev);
1269 unsigned long expires;
1274 spin_lock_bh(&ifp->lock);
1276 ifp->state = INET6_IFADDR_STATE_DEAD;
1277 spin_unlock_bh(&ifp->lock);
1279 if (state == INET6_IFADDR_STATE_DEAD)
1282 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1283 hlist_del_init_rcu(&ifp->addr_lst);
1284 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1286 write_lock_bh(&ifp->idev->lock);
1288 if (ifp->flags&IFA_F_TEMPORARY) {
1289 list_del(&ifp->tmp_list);
1291 in6_ifa_put(ifp->ifpub);
1297 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1298 action = check_cleanup_prefix_route(ifp, &expires);
1300 list_del_rcu(&ifp->if_list);
1303 write_unlock_bh(&ifp->idev->lock);
1305 addrconf_del_dad_work(ifp);
1307 ipv6_ifa_notify(RTM_DELADDR, ifp);
1309 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1311 if (action != CLEANUP_PREFIX_RT_NOP) {
1312 cleanup_prefix_route(ifp, expires,
1313 action == CLEANUP_PREFIX_RT_DEL, false);
1316 /* clean up prefsrc entries */
1317 rt6_remove_prefsrc(ifp);
1322 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
1324 struct inet6_dev *idev = ifp->idev;
1325 unsigned long tmp_tstamp, age;
1326 unsigned long regen_advance;
1327 unsigned long now = jiffies;
1328 s32 cnf_temp_preferred_lft;
1329 struct inet6_ifaddr *ift;
1330 struct ifa6_config cfg;
1331 long max_desync_factor;
1332 struct in6_addr addr;
1335 write_lock_bh(&idev->lock);
1339 if (idev->cnf.use_tempaddr <= 0) {
1340 write_unlock_bh(&idev->lock);
1341 pr_info("%s: use_tempaddr is disabled\n", __func__);
1346 spin_lock_bh(&ifp->lock);
1347 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1348 idev->cnf.use_tempaddr = -1; /*XXX*/
1349 spin_unlock_bh(&ifp->lock);
1350 write_unlock_bh(&idev->lock);
1351 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1358 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1359 ipv6_gen_rnd_iid(&addr);
1361 age = (now - ifp->tstamp) / HZ;
1363 regen_advance = idev->cnf.regen_max_retry *
1364 idev->cnf.dad_transmits *
1365 max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
1367 /* recalculate max_desync_factor each time and update
1368 * idev->desync_factor if it's larger
1370 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1371 max_desync_factor = min_t(__u32,
1372 idev->cnf.max_desync_factor,
1373 cnf_temp_preferred_lft - regen_advance);
1375 if (unlikely(idev->desync_factor > max_desync_factor)) {
1376 if (max_desync_factor > 0) {
1377 get_random_bytes(&idev->desync_factor,
1378 sizeof(idev->desync_factor));
1379 idev->desync_factor %= max_desync_factor;
1381 idev->desync_factor = 0;
1385 memset(&cfg, 0, sizeof(cfg));
1386 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
1387 idev->cnf.temp_valid_lft + age);
1388 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1389 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1391 cfg.plen = ifp->prefix_len;
1392 tmp_tstamp = ifp->tstamp;
1393 spin_unlock_bh(&ifp->lock);
1395 write_unlock_bh(&idev->lock);
1397 /* A temporary address is created only if this calculated Preferred
1398 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1399 * an implementation must not create a temporary address with a zero
1400 * Preferred Lifetime.
1401 * Use age calculation as in addrconf_verify to avoid unnecessary
1402 * temporary addresses being generated.
1404 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1405 if (cfg.preferred_lft <= regen_advance + age) {
1412 cfg.ifa_flags = IFA_F_TEMPORARY;
1413 /* set in addrconf_prefix_rcv() */
1414 if (ifp->flags & IFA_F_OPTIMISTIC)
1415 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
1418 cfg.scope = ipv6_addr_scope(cfg.pfx);
1420 ift = ipv6_add_addr(idev, &cfg, block, NULL);
1424 pr_info("%s: retry temporary address regeneration\n", __func__);
1425 write_lock_bh(&idev->lock);
1429 spin_lock_bh(&ift->lock);
1432 ift->tstamp = tmp_tstamp;
1433 spin_unlock_bh(&ift->lock);
1435 addrconf_dad_start(ift);
1443 * Choose an appropriate source address (RFC3484)
1446 IPV6_SADDR_RULE_INIT = 0,
1447 IPV6_SADDR_RULE_LOCAL,
1448 IPV6_SADDR_RULE_SCOPE,
1449 IPV6_SADDR_RULE_PREFERRED,
1450 #ifdef CONFIG_IPV6_MIP6
1451 IPV6_SADDR_RULE_HOA,
1453 IPV6_SADDR_RULE_OIF,
1454 IPV6_SADDR_RULE_LABEL,
1455 IPV6_SADDR_RULE_PRIVACY,
1456 IPV6_SADDR_RULE_ORCHID,
1457 IPV6_SADDR_RULE_PREFIX,
1458 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1459 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1464 struct ipv6_saddr_score {
1467 struct inet6_ifaddr *ifa;
1468 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1473 struct ipv6_saddr_dst {
1474 const struct in6_addr *addr;
1481 static inline int ipv6_saddr_preferred(int type)
1483 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1488 static bool ipv6_use_optimistic_addr(struct net *net,
1489 struct inet6_dev *idev)
1491 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1494 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1496 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1505 static bool ipv6_allow_optimistic_dad(struct net *net,
1506 struct inet6_dev *idev)
1508 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1511 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1520 static int ipv6_get_saddr_eval(struct net *net,
1521 struct ipv6_saddr_score *score,
1522 struct ipv6_saddr_dst *dst,
1527 if (i <= score->rule) {
1529 case IPV6_SADDR_RULE_SCOPE:
1530 ret = score->scopedist;
1532 case IPV6_SADDR_RULE_PREFIX:
1533 ret = score->matchlen;
1536 ret = !!test_bit(i, score->scorebits);
1542 case IPV6_SADDR_RULE_INIT:
1543 /* Rule 0: remember if hiscore is not ready yet */
1546 case IPV6_SADDR_RULE_LOCAL:
1547 /* Rule 1: Prefer same address */
1548 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1550 case IPV6_SADDR_RULE_SCOPE:
1551 /* Rule 2: Prefer appropriate scope
1556 * ---+--+-+---> scope
1558 * | d is scope of the destination.
1560 * | \ <- smaller scope is better if
1561 * B-15 | \ if scope is enough for destination.
1562 * | ret = B - scope (-1 <= scope >= d <= 15).
1564 * |/ <- greater is better
1565 * -C / if scope is not enough for destination.
1566 * /| ret = scope - C (-1 <= d < scope <= 15).
1568 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1569 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1570 * Assume B = 0 and we get C > 29.
1572 ret = __ipv6_addr_src_scope(score->addr_type);
1573 if (ret >= dst->scope)
1576 ret -= 128; /* 30 is enough */
1577 score->scopedist = ret;
1579 case IPV6_SADDR_RULE_PREFERRED:
1581 /* Rule 3: Avoid deprecated and optimistic addresses */
1582 u8 avoid = IFA_F_DEPRECATED;
1584 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
1585 avoid |= IFA_F_OPTIMISTIC;
1586 ret = ipv6_saddr_preferred(score->addr_type) ||
1587 !(score->ifa->flags & avoid);
1590 #ifdef CONFIG_IPV6_MIP6
1591 case IPV6_SADDR_RULE_HOA:
1593 /* Rule 4: Prefer home address */
1594 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1595 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1599 case IPV6_SADDR_RULE_OIF:
1600 /* Rule 5: Prefer outgoing interface */
1601 ret = (!dst->ifindex ||
1602 dst->ifindex == score->ifa->idev->dev->ifindex);
1604 case IPV6_SADDR_RULE_LABEL:
1605 /* Rule 6: Prefer matching label */
1606 ret = ipv6_addr_label(net,
1607 &score->ifa->addr, score->addr_type,
1608 score->ifa->idev->dev->ifindex) == dst->label;
1610 case IPV6_SADDR_RULE_PRIVACY:
1612 /* Rule 7: Prefer public address
1613 * Note: prefer temporary address if use_tempaddr >= 2
1615 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1616 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1617 score->ifa->idev->cnf.use_tempaddr >= 2;
1618 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1621 case IPV6_SADDR_RULE_ORCHID:
1622 /* Rule 8-: Prefer ORCHID vs ORCHID or
1623 * non-ORCHID vs non-ORCHID
1625 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1626 ipv6_addr_orchid(dst->addr));
1628 case IPV6_SADDR_RULE_PREFIX:
1629 /* Rule 8: Use longest matching prefix */
1630 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1631 if (ret > score->ifa->prefix_len)
1632 ret = score->ifa->prefix_len;
1633 score->matchlen = ret;
1635 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1636 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1637 /* Optimistic addresses still have lower precedence than other
1638 * preferred addresses.
1640 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1648 __set_bit(i, score->scorebits);
1654 static int __ipv6_dev_get_saddr(struct net *net,
1655 struct ipv6_saddr_dst *dst,
1656 struct inet6_dev *idev,
1657 struct ipv6_saddr_score *scores,
1660 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1662 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1666 * - Tentative Address (RFC2462 section 5.4)
1667 * - A tentative address is not considered
1668 * "assigned to an interface" in the traditional
1669 * sense, unless it is also flagged as optimistic.
1670 * - Candidate Source Address (section 4)
1671 * - In any case, anycast addresses, multicast
1672 * addresses, and the unspecified address MUST
1673 * NOT be included in a candidate set.
1675 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1676 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1679 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1681 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1682 score->addr_type & IPV6_ADDR_MULTICAST)) {
1683 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1689 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1691 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1692 int minihiscore, miniscore;
1694 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1695 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1697 if (minihiscore > miniscore) {
1698 if (i == IPV6_SADDR_RULE_SCOPE &&
1699 score->scopedist > 0) {
1702 * each remaining entry
1703 * has too small (not enough)
1704 * scope, because ifa entries
1705 * are sorted by their scope
1711 } else if (minihiscore < miniscore) {
1712 swap(hiscore, score);
1713 hiscore_idx = 1 - hiscore_idx;
1715 /* restore our iterator */
1716 score->ifa = hiscore->ifa;
1726 static int ipv6_get_saddr_master(struct net *net,
1727 const struct net_device *dst_dev,
1728 const struct net_device *master,
1729 struct ipv6_saddr_dst *dst,
1730 struct ipv6_saddr_score *scores,
1733 struct inet6_dev *idev;
1735 idev = __in6_dev_get(dst_dev);
1737 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1738 scores, hiscore_idx);
1740 idev = __in6_dev_get(master);
1742 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1743 scores, hiscore_idx);
1748 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1749 const struct in6_addr *daddr, unsigned int prefs,
1750 struct in6_addr *saddr)
1752 struct ipv6_saddr_score scores[2], *hiscore;
1753 struct ipv6_saddr_dst dst;
1754 struct inet6_dev *idev;
1755 struct net_device *dev;
1757 bool use_oif_addr = false;
1758 int hiscore_idx = 0;
1761 dst_type = __ipv6_addr_type(daddr);
1763 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1764 dst.scope = __ipv6_addr_src_scope(dst_type);
1765 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1768 scores[hiscore_idx].rule = -1;
1769 scores[hiscore_idx].ifa = NULL;
1773 /* Candidate Source Address (section 4)
1774 * - multicast and link-local destination address,
1775 * the set of candidate source address MUST only
1776 * include addresses assigned to interfaces
1777 * belonging to the same link as the outgoing
1779 * (- For site-local destination addresses, the
1780 * set of candidate source addresses MUST only
1781 * include addresses assigned to interfaces
1782 * belonging to the same site as the outgoing
1784 * - "It is RECOMMENDED that the candidate source addresses
1785 * be the set of unicast addresses assigned to the
1786 * interface that will be used to send to the destination
1787 * (the 'outgoing' interface)." (RFC 6724)
1790 idev = __in6_dev_get(dst_dev);
1791 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1792 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1793 (idev && idev->cnf.use_oif_addrs_only)) {
1794 use_oif_addr = true;
1800 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1802 const struct net_device *master;
1805 /* if dst_dev exists and is enslaved to an L3 device, then
1806 * prefer addresses from dst_dev and then the master over
1807 * any other enslaved devices in the L3 domain.
1809 master = l3mdev_master_dev_rcu(dst_dev);
1811 master_idx = master->ifindex;
1813 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1815 scores, hiscore_idx);
1817 if (scores[hiscore_idx].ifa)
1821 for_each_netdev_rcu(net, dev) {
1822 /* only consider addresses on devices in the
1825 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1827 idev = __in6_dev_get(dev);
1830 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1835 hiscore = &scores[hiscore_idx];
1837 ret = -EADDRNOTAVAIL;
1839 *saddr = hiscore->ifa->addr;
1844 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1846 static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1849 struct inet6_ifaddr *ifp;
1850 int err = -EADDRNOTAVAIL;
1852 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1853 if (ifp->scope > IFA_LINK)
1855 if (ifp->scope == IFA_LINK &&
1856 !(ifp->flags & banned_flags)) {
1865 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1868 struct inet6_dev *idev;
1869 int err = -EADDRNOTAVAIL;
1872 idev = __in6_dev_get(dev);
1874 read_lock_bh(&idev->lock);
1875 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1876 read_unlock_bh(&idev->lock);
1882 static int ipv6_count_addresses(const struct inet6_dev *idev)
1884 const struct inet6_ifaddr *ifp;
1888 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1894 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1895 const struct net_device *dev, int strict)
1897 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1898 strict, IFA_F_TENTATIVE);
1900 EXPORT_SYMBOL(ipv6_chk_addr);
1902 /* device argument is used to find the L3 domain of interest. If
1903 * skip_dev_check is set, then the ifp device is not checked against
1904 * the passed in dev argument. So the 2 cases for addresses checks are:
1905 * 1. does the address exist in the L3 domain that dev is part of
1906 * (skip_dev_check = true), or
1908 * 2. does the address exist on the specific device
1909 * (skip_dev_check = false)
1911 static struct net_device *
1912 __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1913 const struct net_device *dev, bool skip_dev_check,
1914 int strict, u32 banned_flags)
1916 unsigned int hash = inet6_addr_hash(net, addr);
1917 struct net_device *l3mdev, *ndev;
1918 struct inet6_ifaddr *ifp;
1923 l3mdev = l3mdev_master_dev_rcu(dev);
1927 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1928 ndev = ifp->idev->dev;
1930 if (l3mdev_master_dev_rcu(ndev) != l3mdev)
1933 /* Decouple optimistic from tentative for evaluation here.
1934 * Ban optimistic addresses explicitly, when required.
1936 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1937 ? (ifp->flags&~IFA_F_TENTATIVE)
1939 if (ipv6_addr_equal(&ifp->addr, addr) &&
1940 !(ifp_flags&banned_flags) &&
1941 (!dev || ndev == dev ||
1942 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1952 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1953 const struct net_device *dev, bool skip_dev_check,
1954 int strict, u32 banned_flags)
1956 return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
1957 strict, banned_flags) ? 1 : 0;
1959 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1962 /* Compares an address/prefix_len with addresses on device @dev.
1963 * If one is found it returns true.
1965 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1966 const unsigned int prefix_len, struct net_device *dev)
1968 const struct inet6_ifaddr *ifa;
1969 const struct inet6_dev *idev;
1973 idev = __in6_dev_get(dev);
1975 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1976 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1985 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1987 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1989 const struct inet6_ifaddr *ifa;
1990 const struct inet6_dev *idev;
1995 idev = __in6_dev_get(dev);
1997 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1998 onlink = ipv6_prefix_equal(addr, &ifa->addr,
2007 EXPORT_SYMBOL(ipv6_chk_prefix);
2010 * ipv6_dev_find - find the first device with a given source address.
2011 * @net: the net namespace
2012 * @addr: the source address
2013 * @dev: used to find the L3 domain of interest
2015 * The caller should be protected by RCU, or RTNL.
2017 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
2018 struct net_device *dev)
2020 return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
2023 EXPORT_SYMBOL(ipv6_dev_find);
2025 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
2026 struct net_device *dev, int strict)
2028 unsigned int hash = inet6_addr_hash(net, addr);
2029 struct inet6_ifaddr *ifp, *result = NULL;
2032 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2033 if (ipv6_addr_equal(&ifp->addr, addr)) {
2034 if (!dev || ifp->idev->dev == dev ||
2035 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2047 /* Gets referenced address, destroys ifaddr */
2049 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2052 ifp->flags |= IFA_F_DADFAILED;
2054 if (ifp->flags&IFA_F_TEMPORARY) {
2055 struct inet6_ifaddr *ifpub;
2056 spin_lock_bh(&ifp->lock);
2059 in6_ifa_hold(ifpub);
2060 spin_unlock_bh(&ifp->lock);
2061 ipv6_create_tempaddr(ifpub, true);
2064 spin_unlock_bh(&ifp->lock);
2067 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2068 spin_lock_bh(&ifp->lock);
2069 addrconf_del_dad_work(ifp);
2070 ifp->flags |= IFA_F_TENTATIVE;
2072 ifp->flags &= ~IFA_F_OPTIMISTIC;
2073 spin_unlock_bh(&ifp->lock);
2075 ipv6_ifa_notify(0, ifp);
2082 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2086 spin_lock_bh(&ifp->lock);
2087 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2088 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2091 spin_unlock_bh(&ifp->lock);
2096 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2098 struct inet6_dev *idev = ifp->idev;
2099 struct net *net = dev_net(idev->dev);
2101 if (addrconf_dad_end(ifp)) {
2106 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2107 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2109 spin_lock_bh(&ifp->lock);
2111 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2112 struct in6_addr new_addr;
2113 struct inet6_ifaddr *ifp2;
2114 int retries = ifp->stable_privacy_retry + 1;
2115 struct ifa6_config cfg = {
2117 .plen = ifp->prefix_len,
2118 .ifa_flags = ifp->flags,
2119 .valid_lft = ifp->valid_lft,
2120 .preferred_lft = ifp->prefered_lft,
2121 .scope = ifp->scope,
2124 if (retries > net->ipv6.sysctl.idgen_retries) {
2125 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2126 ifp->idev->dev->name);
2130 new_addr = ifp->addr;
2131 if (ipv6_generate_stable_address(&new_addr, retries,
2135 spin_unlock_bh(&ifp->lock);
2137 if (idev->cnf.max_addresses &&
2138 ipv6_count_addresses(idev) >=
2139 idev->cnf.max_addresses)
2142 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2143 ifp->idev->dev->name);
2145 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2149 spin_lock_bh(&ifp2->lock);
2150 ifp2->stable_privacy_retry = retries;
2151 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2152 spin_unlock_bh(&ifp2->lock);
2154 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2157 spin_lock_bh(&ifp->lock);
2161 /* transition from _POSTDAD to _ERRDAD */
2162 ifp->state = INET6_IFADDR_STATE_ERRDAD;
2163 spin_unlock_bh(&ifp->lock);
2165 addrconf_mod_dad_work(ifp, 0);
2169 /* Join to solicited addr multicast group.
2170 * caller must hold RTNL */
2171 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2173 struct in6_addr maddr;
2175 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2178 addrconf_addr_solict_mult(addr, &maddr);
2179 ipv6_dev_mc_inc(dev, &maddr);
2182 /* caller must hold RTNL */
2183 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2185 struct in6_addr maddr;
2187 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2190 addrconf_addr_solict_mult(addr, &maddr);
2191 __ipv6_dev_mc_dec(idev, &maddr);
2194 /* caller must hold RTNL */
2195 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2197 struct in6_addr addr;
2199 if (ifp->prefix_len >= 127) /* RFC 6164 */
2201 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2202 if (ipv6_addr_any(&addr))
2204 __ipv6_dev_ac_inc(ifp->idev, &addr);
2207 /* caller must hold RTNL */
2208 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2210 struct in6_addr addr;
2212 if (ifp->prefix_len >= 127) /* RFC 6164 */
2214 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2215 if (ipv6_addr_any(&addr))
2217 __ipv6_dev_ac_dec(ifp->idev, &addr);
2220 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2222 switch (dev->addr_len) {
2224 memcpy(eui, dev->dev_addr, 3);
2227 memcpy(eui + 5, dev->dev_addr + 3, 3);
2229 case EUI64_ADDR_LEN:
2230 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2240 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2242 const union fwnet_hwaddr *ha;
2244 if (dev->addr_len != FWNET_ALEN)
2247 ha = (const union fwnet_hwaddr *)dev->dev_addr;
2249 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2254 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2256 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2257 if (dev->addr_len != ARCNET_ALEN)
2260 eui[7] = *(u8 *)dev->dev_addr;
2264 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2266 if (dev->addr_len != INFINIBAND_ALEN)
2268 memcpy(eui, dev->dev_addr + 12, 8);
2273 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2277 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2278 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2279 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2280 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2281 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2282 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2286 memcpy(eui + 4, &addr, 4);
2290 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2292 if (dev->priv_flags & IFF_ISATAP)
2293 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2297 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2299 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2302 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2304 memcpy(eui, dev->perm_addr, 3);
2305 memcpy(eui + 5, dev->perm_addr + 3, 3);
2312 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2314 switch (dev->type) {
2317 return addrconf_ifid_eui48(eui, dev);
2319 return addrconf_ifid_arcnet(eui, dev);
2320 case ARPHRD_INFINIBAND:
2321 return addrconf_ifid_infiniband(eui, dev);
2323 return addrconf_ifid_sit(eui, dev);
2326 return addrconf_ifid_gre(eui, dev);
2327 case ARPHRD_6LOWPAN:
2328 return addrconf_ifid_6lowpan(eui, dev);
2329 case ARPHRD_IEEE1394:
2330 return addrconf_ifid_ieee1394(eui, dev);
2331 case ARPHRD_TUNNEL6:
2334 return addrconf_ifid_ip6tnl(eui, dev);
2339 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2342 struct inet6_ifaddr *ifp;
2344 read_lock_bh(&idev->lock);
2345 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2346 if (ifp->scope > IFA_LINK)
2348 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2349 memcpy(eui, ifp->addr.s6_addr+8, 8);
2354 read_unlock_bh(&idev->lock);
2358 /* Generation of a randomized Interface Identifier
2359 * draft-ietf-6man-rfc4941bis, Section 3.3.1
2362 static void ipv6_gen_rnd_iid(struct in6_addr *addr)
2365 get_random_bytes(&addr->s6_addr[8], 8);
2367 /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2368 * check if generated address is not inappropriate:
2370 * - Reserved IPv6 Interface Identifiers
2371 * - XXX: already assigned to an address on the device
2374 /* Subnet-router anycast: 0000:0000:0000:0000 */
2375 if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2378 /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2379 * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
2380 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2382 if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2383 (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2386 /* Reserved subnet anycast addresses */
2387 if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2388 ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2397 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2398 struct net_device *dev, unsigned long expires,
2399 u32 flags, gfp_t gfp_flags)
2401 struct fib6_config cfg = {
2402 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2403 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2404 .fc_ifindex = dev->ifindex,
2405 .fc_expires = expires,
2407 .fc_flags = RTF_UP | flags,
2408 .fc_nlinfo.nl_net = dev_net(dev),
2409 .fc_protocol = RTPROT_KERNEL,
2410 .fc_type = RTN_UNICAST,
2415 /* Prevent useless cloning on PtP SIT.
2416 This thing is done here expecting that the whole
2417 class of non-broadcast devices need not cloning.
2419 #if IS_ENABLED(CONFIG_IPV6_SIT)
2420 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2421 cfg.fc_flags |= RTF_NONEXTHOP;
2424 ip6_route_add(&cfg, gfp_flags, NULL);
2428 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2430 const struct net_device *dev,
2431 u32 flags, u32 noflags,
2434 struct fib6_node *fn;
2435 struct fib6_info *rt = NULL;
2436 struct fib6_table *table;
2437 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2439 table = fib6_get_table(dev_net(dev), tb_id);
2444 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2448 for_each_fib6_node_rt_rcu(fn) {
2449 /* prefix routes only use builtin fib6_nh */
2453 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
2455 if (no_gw && rt->fib6_nh->fib_nh_gw_family)
2457 if ((rt->fib6_flags & flags) != flags)
2459 if ((rt->fib6_flags & noflags) != 0)
2461 if (!fib6_info_hold_safe(rt))
2471 /* Create "default" multicast route to the interface */
2473 static void addrconf_add_mroute(struct net_device *dev)
2475 struct fib6_config cfg = {
2476 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2477 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2478 .fc_ifindex = dev->ifindex,
2481 .fc_type = RTN_MULTICAST,
2482 .fc_nlinfo.nl_net = dev_net(dev),
2483 .fc_protocol = RTPROT_KERNEL,
2486 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2488 ip6_route_add(&cfg, GFP_KERNEL, NULL);
2491 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2493 struct inet6_dev *idev;
2497 idev = ipv6_find_idev(dev);
2501 if (idev->cnf.disable_ipv6)
2502 return ERR_PTR(-EACCES);
2504 /* Add default multicast route */
2505 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2506 addrconf_add_mroute(dev);
2511 static void manage_tempaddrs(struct inet6_dev *idev,
2512 struct inet6_ifaddr *ifp,
2513 __u32 valid_lft, __u32 prefered_lft,
2514 bool create, unsigned long now)
2517 struct inet6_ifaddr *ift;
2519 read_lock_bh(&idev->lock);
2520 /* update all temporary addresses in the list */
2521 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2522 int age, max_valid, max_prefered;
2524 if (ifp != ift->ifpub)
2527 /* RFC 4941 section 3.3:
2528 * If a received option will extend the lifetime of a public
2529 * address, the lifetimes of temporary addresses should
2530 * be extended, subject to the overall constraint that no
2531 * temporary addresses should ever remain "valid" or "preferred"
2532 * for a time longer than (TEMP_VALID_LIFETIME) or
2533 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2535 age = (now - ift->cstamp) / HZ;
2536 max_valid = idev->cnf.temp_valid_lft - age;
2540 max_prefered = idev->cnf.temp_prefered_lft -
2541 idev->desync_factor - age;
2542 if (max_prefered < 0)
2545 if (valid_lft > max_valid)
2546 valid_lft = max_valid;
2548 if (prefered_lft > max_prefered)
2549 prefered_lft = max_prefered;
2551 spin_lock(&ift->lock);
2553 ift->valid_lft = valid_lft;
2554 ift->prefered_lft = prefered_lft;
2556 if (prefered_lft > 0)
2557 ift->flags &= ~IFA_F_DEPRECATED;
2559 spin_unlock(&ift->lock);
2560 if (!(flags&IFA_F_TENTATIVE))
2561 ipv6_ifa_notify(0, ift);
2564 /* Also create a temporary address if it's enabled but no temporary
2565 * address currently exists.
2566 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
2567 * as part of cleanup (ie. deleting the mngtmpaddr).
2568 * We don't want that to result in creating a new temporary ip address.
2570 if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
2573 if (create && idev->cnf.use_tempaddr > 0) {
2574 /* When a new public address is created as described
2575 * in [ADDRCONF], also create a new temporary address.
2577 read_unlock_bh(&idev->lock);
2578 ipv6_create_tempaddr(ifp, false);
2580 read_unlock_bh(&idev->lock);
2584 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2586 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2587 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2590 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2591 const struct prefix_info *pinfo,
2592 struct inet6_dev *in6_dev,
2593 const struct in6_addr *addr, int addr_type,
2594 u32 addr_flags, bool sllao, bool tokenized,
2595 __u32 valid_lft, u32 prefered_lft)
2597 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2598 int create = 0, update_lft = 0;
2600 if (!ifp && valid_lft) {
2601 int max_addresses = in6_dev->cnf.max_addresses;
2602 struct ifa6_config cfg = {
2604 .plen = pinfo->prefix_len,
2605 .ifa_flags = addr_flags,
2606 .valid_lft = valid_lft,
2607 .preferred_lft = prefered_lft,
2608 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2609 .ifa_proto = IFAPROT_KERNEL_RA
2612 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2613 if ((net->ipv6.devconf_all->optimistic_dad ||
2614 in6_dev->cnf.optimistic_dad) &&
2615 !net->ipv6.devconf_all->forwarding && sllao)
2616 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2619 /* Do not allow to create too much of autoconfigured
2620 * addresses; this would be too easy way to crash kernel.
2622 if (!max_addresses ||
2623 ipv6_count_addresses(in6_dev) < max_addresses)
2624 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2626 if (IS_ERR_OR_NULL(ifp))
2630 spin_lock_bh(&ifp->lock);
2631 ifp->flags |= IFA_F_MANAGETEMPADDR;
2632 ifp->cstamp = jiffies;
2633 ifp->tokenized = tokenized;
2634 spin_unlock_bh(&ifp->lock);
2635 addrconf_dad_start(ifp);
2643 /* update lifetime (RFC2462 5.5.3 e) */
2644 spin_lock_bh(&ifp->lock);
2646 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2647 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2650 if (!create && stored_lft) {
2651 const u32 minimum_lft = min_t(u32,
2652 stored_lft, MIN_VALID_LIFETIME);
2653 valid_lft = max(valid_lft, minimum_lft);
2655 /* RFC4862 Section 5.5.3e:
2656 * "Note that the preferred lifetime of the
2657 * corresponding address is always reset to
2658 * the Preferred Lifetime in the received
2659 * Prefix Information option, regardless of
2660 * whether the valid lifetime is also reset or
2663 * So we should always update prefered_lft here.
2669 ifp->valid_lft = valid_lft;
2670 ifp->prefered_lft = prefered_lft;
2673 ifp->flags &= ~IFA_F_DEPRECATED;
2674 spin_unlock_bh(&ifp->lock);
2676 if (!(flags&IFA_F_TENTATIVE))
2677 ipv6_ifa_notify(0, ifp);
2679 spin_unlock_bh(&ifp->lock);
2681 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2685 addrconf_verify(net);
2690 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2692 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2694 struct prefix_info *pinfo;
2699 struct inet6_dev *in6_dev;
2700 struct net *net = dev_net(dev);
2702 pinfo = (struct prefix_info *) opt;
2704 if (len < sizeof(struct prefix_info)) {
2705 netdev_dbg(dev, "addrconf: prefix option too short\n");
2710 * Validation checks ([ADDRCONF], page 19)
2713 addr_type = ipv6_addr_type(&pinfo->prefix);
2715 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2718 valid_lft = ntohl(pinfo->valid);
2719 prefered_lft = ntohl(pinfo->prefered);
2721 if (prefered_lft > valid_lft) {
2722 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2726 in6_dev = in6_dev_get(dev);
2729 net_dbg_ratelimited("addrconf: device %s not configured\n",
2735 * Two things going on here:
2736 * 1) Add routes for on-link prefixes
2737 * 2) Configure prefixes with the auto flag set
2740 if (pinfo->onlink) {
2741 struct fib6_info *rt;
2742 unsigned long rt_expires;
2744 /* Avoid arithmetic overflow. Really, we could
2745 * save rt_expires in seconds, likely valid_lft,
2746 * but it would require division in fib gc, that it
2750 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2752 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2754 if (addrconf_finite_timeout(rt_expires))
2757 rt = addrconf_get_prefix_route(&pinfo->prefix,
2760 RTF_ADDRCONF | RTF_PREFIX_RT,
2764 /* Autoconf prefix route */
2765 if (valid_lft == 0) {
2766 ip6_del_rt(net, rt, false);
2768 } else if (addrconf_finite_timeout(rt_expires)) {
2770 fib6_set_expires(rt, jiffies + rt_expires);
2772 fib6_clean_expires(rt);
2774 } else if (valid_lft) {
2775 clock_t expires = 0;
2776 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2777 if (addrconf_finite_timeout(rt_expires)) {
2779 flags |= RTF_EXPIRES;
2780 expires = jiffies_to_clock_t(rt_expires);
2782 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2783 0, dev, expires, flags,
2786 fib6_info_release(rt);
2789 /* Try to figure out our local address for this prefix */
2791 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2792 struct in6_addr addr;
2793 bool tokenized = false, dev_addr_generated = false;
2795 if (pinfo->prefix_len == 64) {
2796 memcpy(&addr, &pinfo->prefix, 8);
2798 if (!ipv6_addr_any(&in6_dev->token)) {
2799 read_lock_bh(&in6_dev->lock);
2800 memcpy(addr.s6_addr + 8,
2801 in6_dev->token.s6_addr + 8, 8);
2802 read_unlock_bh(&in6_dev->lock);
2804 } else if (is_addr_mode_generate_stable(in6_dev) &&
2805 !ipv6_generate_stable_address(&addr, 0,
2807 addr_flags |= IFA_F_STABLE_PRIVACY;
2809 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2810 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2813 dev_addr_generated = true;
2817 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2822 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2825 tokenized, valid_lft,
2830 /* Ignore error case here because previous prefix add addr was
2831 * successful which will be notified.
2833 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2834 addr_type, addr_flags, sllao,
2835 tokenized, valid_lft,
2837 dev_addr_generated);
2839 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2841 in6_dev_put(in6_dev);
2844 static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
2845 struct in6_ifreq *ireq)
2847 struct ip_tunnel_parm p = { };
2850 if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
2851 return -EADDRNOTAVAIL;
2853 p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
2856 p.iph.protocol = IPPROTO_IPV6;
2859 if (!dev->netdev_ops->ndo_tunnel_ctl)
2861 err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
2865 dev = __dev_get_by_name(net, p.name);
2868 return dev_open(dev, NULL);
2872 * Set destination address.
2873 * Special case for SIT interfaces where we create a new "virtual"
2876 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2878 struct net_device *dev;
2879 struct in6_ifreq ireq;
2882 if (!IS_ENABLED(CONFIG_IPV6_SIT))
2884 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2888 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2889 if (dev && dev->type == ARPHRD_SIT)
2890 err = addrconf_set_sit_dstaddr(net, dev, &ireq);
2895 static int ipv6_mc_config(struct sock *sk, bool join,
2896 const struct in6_addr *addr, int ifindex)
2904 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2906 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2913 * Manual configuration of address on an interface
2915 static int inet6_addr_add(struct net *net, int ifindex,
2916 struct ifa6_config *cfg,
2917 struct netlink_ext_ack *extack)
2919 struct inet6_ifaddr *ifp;
2920 struct inet6_dev *idev;
2921 struct net_device *dev;
2922 unsigned long timeout;
2928 if (cfg->plen > 128)
2931 /* check the lifetime */
2932 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
2935 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
2938 dev = __dev_get_by_index(net, ifindex);
2942 idev = addrconf_add_dev(dev);
2944 return PTR_ERR(idev);
2946 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2947 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2948 true, cfg->pfx, ifindex);
2954 cfg->scope = ipv6_addr_scope(cfg->pfx);
2956 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
2957 if (addrconf_finite_timeout(timeout)) {
2958 expires = jiffies_to_clock_t(timeout * HZ);
2959 cfg->valid_lft = timeout;
2960 flags = RTF_EXPIRES;
2964 cfg->ifa_flags |= IFA_F_PERMANENT;
2967 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
2968 if (addrconf_finite_timeout(timeout)) {
2970 cfg->ifa_flags |= IFA_F_DEPRECATED;
2971 cfg->preferred_lft = timeout;
2974 ifp = ipv6_add_addr(idev, cfg, true, extack);
2976 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
2977 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2978 ifp->rt_priority, dev, expires,
2982 /* Send a netlink notification if DAD is enabled and
2983 * optimistic flag is not set
2985 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2986 ipv6_ifa_notify(0, ifp);
2988 * Note that section 3.1 of RFC 4429 indicates
2989 * that the Optimistic flag should not be set for
2990 * manually configured addresses
2992 addrconf_dad_start(ifp);
2993 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
2994 manage_tempaddrs(idev, ifp, cfg->valid_lft,
2995 cfg->preferred_lft, true, jiffies);
2997 addrconf_verify_rtnl(net);
2999 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3000 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
3004 return PTR_ERR(ifp);
3007 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
3008 const struct in6_addr *pfx, unsigned int plen)
3010 struct inet6_ifaddr *ifp;
3011 struct inet6_dev *idev;
3012 struct net_device *dev;
3017 dev = __dev_get_by_index(net, ifindex);
3021 idev = __in6_dev_get(dev);
3025 read_lock_bh(&idev->lock);
3026 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3027 if (ifp->prefix_len == plen &&
3028 ipv6_addr_equal(pfx, &ifp->addr)) {
3030 read_unlock_bh(&idev->lock);
3032 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3033 (ifa_flags & IFA_F_MANAGETEMPADDR))
3034 manage_tempaddrs(idev, ifp, 0, 0, false,
3037 addrconf_verify_rtnl(net);
3038 if (ipv6_addr_is_multicast(pfx)) {
3039 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3040 false, pfx, dev->ifindex);
3045 read_unlock_bh(&idev->lock);
3046 return -EADDRNOTAVAIL;
3050 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3052 struct ifa6_config cfg = {
3053 .ifa_flags = IFA_F_PERMANENT,
3054 .preferred_lft = INFINITY_LIFE_TIME,
3055 .valid_lft = INFINITY_LIFE_TIME,
3057 struct in6_ifreq ireq;
3060 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3063 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3066 cfg.pfx = &ireq.ifr6_addr;
3067 cfg.plen = ireq.ifr6_prefixlen;
3070 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
3075 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3077 struct in6_ifreq ireq;
3080 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3083 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3087 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3088 ireq.ifr6_prefixlen);
3093 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3094 int plen, int scope, u8 proto)
3096 struct inet6_ifaddr *ifp;
3097 struct ifa6_config cfg = {
3100 .ifa_flags = IFA_F_PERMANENT,
3101 .valid_lft = INFINITY_LIFE_TIME,
3102 .preferred_lft = INFINITY_LIFE_TIME,
3107 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3109 spin_lock_bh(&ifp->lock);
3110 ifp->flags &= ~IFA_F_TENTATIVE;
3111 spin_unlock_bh(&ifp->lock);
3112 rt_genid_bump_ipv6(dev_net(idev->dev));
3113 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3118 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3119 static void add_v4_addrs(struct inet6_dev *idev)
3121 struct in6_addr addr;
3122 struct net_device *dev;
3123 struct net *net = dev_net(idev->dev);
3124 int scope, plen, offset = 0;
3129 memset(&addr, 0, sizeof(struct in6_addr));
3130 /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3131 if (idev->dev->addr_len == sizeof(struct in6_addr))
3132 offset = sizeof(struct in6_addr) - 4;
3133 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4);
3135 if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
3136 scope = IPV6_ADDR_COMPATv4;
3138 pflags |= RTF_NONEXTHOP;
3140 if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
3143 addr.s6_addr32[0] = htonl(0xfe800000);
3148 if (addr.s6_addr32[3]) {
3149 add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
3150 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3155 for_each_netdev(net, dev) {
3156 struct in_device *in_dev = __in_dev_get_rtnl(dev);
3157 if (in_dev && (dev->flags & IFF_UP)) {
3158 struct in_ifaddr *ifa;
3161 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3162 addr.s6_addr32[3] = ifa->ifa_local;
3164 if (ifa->ifa_scope == RT_SCOPE_LINK)
3166 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3167 if (idev->dev->flags&IFF_POINTOPOINT)
3172 add_addr(idev, &addr, plen, flag,
3174 addrconf_prefix_route(&addr, plen, 0, idev->dev,
3175 0, pflags, GFP_KERNEL);
3182 static void init_loopback(struct net_device *dev)
3184 struct inet6_dev *idev;
3190 idev = ipv6_find_idev(dev);
3192 pr_debug("%s: add_dev failed\n", __func__);
3196 add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
3199 void addrconf_add_linklocal(struct inet6_dev *idev,
3200 const struct in6_addr *addr, u32 flags)
3202 struct ifa6_config cfg = {
3205 .ifa_flags = flags | IFA_F_PERMANENT,
3206 .valid_lft = INFINITY_LIFE_TIME,
3207 .preferred_lft = INFINITY_LIFE_TIME,
3209 .ifa_proto = IFAPROT_KERNEL_LL
3211 struct inet6_ifaddr *ifp;
3213 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3214 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3215 idev->cnf.optimistic_dad) &&
3216 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3217 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3220 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3222 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3224 addrconf_dad_start(ifp);
3228 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3230 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3232 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3235 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3236 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3239 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3240 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3246 static int ipv6_generate_stable_address(struct in6_addr *address,
3248 const struct inet6_dev *idev)
3250 static DEFINE_SPINLOCK(lock);
3251 static __u32 digest[SHA1_DIGEST_WORDS];
3252 static __u32 workspace[SHA1_WORKSPACE_WORDS];
3255 char __data[SHA1_BLOCK_SIZE];
3257 struct in6_addr secret;
3259 unsigned char hwaddr[MAX_ADDR_LEN];
3264 struct in6_addr secret;
3265 struct in6_addr temp;
3266 struct net *net = dev_net(idev->dev);
3268 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3270 if (idev->cnf.stable_secret.initialized)
3271 secret = idev->cnf.stable_secret.secret;
3272 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3273 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3278 spin_lock_bh(&lock);
3281 memset(&data, 0, sizeof(data));
3282 memset(workspace, 0, sizeof(workspace));
3283 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3284 data.prefix[0] = address->s6_addr32[0];
3285 data.prefix[1] = address->s6_addr32[1];
3286 data.secret = secret;
3287 data.dad_count = dad_count;
3289 sha1_transform(digest, data.__data, workspace);
3292 temp.s6_addr32[2] = (__force __be32)digest[0];
3293 temp.s6_addr32[3] = (__force __be32)digest[1];
3295 spin_unlock_bh(&lock);
3297 if (ipv6_reserved_interfaceid(temp)) {
3299 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3308 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3310 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3314 s = &idev->cnf.stable_secret;
3315 get_random_bytes(&s->secret, sizeof(s->secret));
3316 s->initialized = true;
3319 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3321 struct in6_addr addr;
3323 /* no link local addresses on L3 master devices */
3324 if (netif_is_l3_master(idev->dev))
3327 /* no link local addresses on devices flagged as slaves */
3328 if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
3331 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3333 switch (idev->cnf.addr_gen_mode) {
3334 case IN6_ADDR_GEN_MODE_RANDOM:
3335 ipv6_gen_mode_random_init(idev);
3337 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3338 if (!ipv6_generate_stable_address(&addr, 0, idev))
3339 addrconf_add_linklocal(idev, &addr,
3340 IFA_F_STABLE_PRIVACY);
3341 else if (prefix_route)
3342 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3345 case IN6_ADDR_GEN_MODE_EUI64:
3346 /* addrconf_add_linklocal also adds a prefix_route and we
3347 * only need to care about prefix routes if ipv6_generate_eui64
3348 * couldn't generate one.
3350 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3351 addrconf_add_linklocal(idev, &addr, 0);
3352 else if (prefix_route)
3353 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3356 case IN6_ADDR_GEN_MODE_NONE:
3358 /* will not add any link local address */
3363 static void addrconf_dev_config(struct net_device *dev)
3365 struct inet6_dev *idev;
3369 if ((dev->type != ARPHRD_ETHER) &&
3370 (dev->type != ARPHRD_FDDI) &&
3371 (dev->type != ARPHRD_ARCNET) &&
3372 (dev->type != ARPHRD_INFINIBAND) &&
3373 (dev->type != ARPHRD_IEEE1394) &&
3374 (dev->type != ARPHRD_TUNNEL6) &&
3375 (dev->type != ARPHRD_6LOWPAN) &&
3376 (dev->type != ARPHRD_TUNNEL) &&
3377 (dev->type != ARPHRD_NONE) &&
3378 (dev->type != ARPHRD_RAWIP)) {
3379 /* Alas, we support only Ethernet autoconfiguration. */
3380 idev = __in6_dev_get(dev);
3381 if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3382 dev->flags & IFF_MULTICAST)
3387 idev = addrconf_add_dev(dev);
3391 /* this device type has no EUI support */
3392 if (dev->type == ARPHRD_NONE &&
3393 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3394 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3396 addrconf_addr_gen(idev, false);
3399 #if IS_ENABLED(CONFIG_IPV6_SIT)
3400 static void addrconf_sit_config(struct net_device *dev)
3402 struct inet6_dev *idev;
3407 * Configure the tunnel with one of our IPv4
3408 * addresses... we should configure all of
3409 * our v4 addrs in the tunnel
3412 idev = ipv6_find_idev(dev);
3414 pr_debug("%s: add_dev failed\n", __func__);
3418 if (dev->priv_flags & IFF_ISATAP) {
3419 addrconf_addr_gen(idev, false);
3425 if (dev->flags&IFF_POINTOPOINT)
3426 addrconf_add_mroute(dev);
3430 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3431 static void addrconf_gre_config(struct net_device *dev)
3433 struct inet6_dev *idev;
3437 idev = ipv6_find_idev(dev);
3439 pr_debug("%s: add_dev failed\n", __func__);
3443 if (dev->type == ARPHRD_ETHER) {
3444 addrconf_addr_gen(idev, true);
3450 if (dev->flags & IFF_POINTOPOINT)
3451 addrconf_add_mroute(dev);
3455 static void addrconf_init_auto_addrs(struct net_device *dev)
3457 switch (dev->type) {
3458 #if IS_ENABLED(CONFIG_IPV6_SIT)
3460 addrconf_sit_config(dev);
3463 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3466 addrconf_gre_config(dev);
3469 case ARPHRD_LOOPBACK:
3474 addrconf_dev_config(dev);
3479 static int fixup_permanent_addr(struct net *net,
3480 struct inet6_dev *idev,
3481 struct inet6_ifaddr *ifp)
3483 /* !fib6_node means the host route was removed from the
3484 * FIB, for example, if 'lo' device is taken down. In that
3485 * case regenerate the host route.
3487 if (!ifp->rt || !ifp->rt->fib6_node) {
3488 struct fib6_info *f6i, *prev;
3490 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3493 return PTR_ERR(f6i);
3495 /* ifp->rt can be accessed outside of rtnl */
3496 spin_lock(&ifp->lock);
3499 spin_unlock(&ifp->lock);
3501 fib6_info_release(prev);
3504 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3505 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3506 ifp->rt_priority, idev->dev, 0, 0,
3510 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3511 addrconf_dad_start(ifp);
3516 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3518 struct inet6_ifaddr *ifp, *tmp;
3519 struct inet6_dev *idev;
3521 idev = __in6_dev_get(dev);
3525 write_lock_bh(&idev->lock);
3527 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3528 if ((ifp->flags & IFA_F_PERMANENT) &&
3529 fixup_permanent_addr(net, idev, ifp) < 0) {
3530 write_unlock_bh(&idev->lock);
3533 write_lock_bh(&idev->lock);
3535 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3536 idev->dev->name, &ifp->addr);
3540 write_unlock_bh(&idev->lock);
3543 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3546 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3547 struct netdev_notifier_change_info *change_info;
3548 struct netdev_notifier_changeupper_info *info;
3549 struct inet6_dev *idev = __in6_dev_get(dev);
3550 struct net *net = dev_net(dev);
3551 int run_pending = 0;
3555 case NETDEV_REGISTER:
3556 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3557 idev = ipv6_add_dev(dev);
3559 return notifier_from_errno(PTR_ERR(idev));
3563 case NETDEV_CHANGEMTU:
3564 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3565 if (dev->mtu < IPV6_MIN_MTU) {
3566 addrconf_ifdown(dev, dev != net->loopback_dev);
3571 rt6_mtu_change(dev, dev->mtu);
3572 idev->cnf.mtu6 = dev->mtu;
3576 /* allocate new idev */
3577 idev = ipv6_add_dev(dev);
3581 /* device is still not ready */
3582 if (!(idev->if_flags & IF_READY))
3589 if (idev && idev->cnf.disable_ipv6)
3592 if (dev->priv_flags & IFF_NO_ADDRCONF) {
3593 if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3594 dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3599 if (event == NETDEV_UP) {
3600 /* restore routes for permanent addresses */
3601 addrconf_permanent_addr(net, dev);
3603 if (!addrconf_link_ready(dev)) {
3604 /* device is not ready yet. */
3605 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3610 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3611 idev = ipv6_add_dev(dev);
3613 if (!IS_ERR_OR_NULL(idev)) {
3614 idev->if_flags |= IF_READY;
3617 } else if (event == NETDEV_CHANGE) {
3618 if (!addrconf_link_ready(dev)) {
3619 /* device is still not ready. */
3620 rt6_sync_down_dev(dev, event);
3624 if (!IS_ERR_OR_NULL(idev)) {
3625 if (idev->if_flags & IF_READY) {
3626 /* device is already configured -
3627 * but resend MLD reports, we might
3628 * have roamed and need to update
3629 * multicast snooping switches
3633 if (change_info->flags_changed & IFF_NOARP)
3634 addrconf_dad_run(idev, true);
3635 rt6_sync_up(dev, RTNH_F_LINKDOWN);
3638 idev->if_flags |= IF_READY;
3641 pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3647 addrconf_init_auto_addrs(dev);
3649 if (!IS_ERR_OR_NULL(idev)) {
3651 addrconf_dad_run(idev, false);
3653 /* Device has an address by now */
3654 rt6_sync_up(dev, RTNH_F_DEAD);
3657 * If the MTU changed during the interface down,
3658 * when the interface up, the changed MTU must be
3659 * reflected in the idev as well as routers.
3661 if (idev->cnf.mtu6 != dev->mtu &&
3662 dev->mtu >= IPV6_MIN_MTU) {
3663 rt6_mtu_change(dev, dev->mtu);
3664 idev->cnf.mtu6 = dev->mtu;
3666 idev->tstamp = jiffies;
3667 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3670 * If the changed mtu during down is lower than
3671 * IPV6_MIN_MTU stop IPv6 on this interface.
3673 if (dev->mtu < IPV6_MIN_MTU)
3674 addrconf_ifdown(dev, dev != net->loopback_dev);
3679 case NETDEV_UNREGISTER:
3681 * Remove all addresses from this interface.
3683 addrconf_ifdown(dev, event != NETDEV_DOWN);
3686 case NETDEV_CHANGENAME:
3688 snmp6_unregister_dev(idev);
3689 addrconf_sysctl_unregister(idev);
3690 err = addrconf_sysctl_register(idev);
3692 return notifier_from_errno(err);
3693 err = snmp6_register_dev(idev);
3695 addrconf_sysctl_unregister(idev);
3696 return notifier_from_errno(err);
3701 case NETDEV_PRE_TYPE_CHANGE:
3702 case NETDEV_POST_TYPE_CHANGE:
3704 addrconf_type_change(dev, event);
3707 case NETDEV_CHANGEUPPER:
3710 /* flush all routes if dev is linked to or unlinked from
3711 * an L3 master device (e.g., VRF)
3713 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3714 addrconf_ifdown(dev, false);
3721 * addrconf module should be notified of a device going up
3723 static struct notifier_block ipv6_dev_notf = {
3724 .notifier_call = addrconf_notify,
3725 .priority = ADDRCONF_NOTIFY_PRIORITY,
3728 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3730 struct inet6_dev *idev;
3733 idev = __in6_dev_get(dev);
3735 if (event == NETDEV_POST_TYPE_CHANGE)
3736 ipv6_mc_remap(idev);
3737 else if (event == NETDEV_PRE_TYPE_CHANGE)
3738 ipv6_mc_unmap(idev);
3741 static bool addr_is_local(const struct in6_addr *addr)
3743 return ipv6_addr_type(addr) &
3744 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3747 static int addrconf_ifdown(struct net_device *dev, bool unregister)
3749 unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3750 struct net *net = dev_net(dev);
3751 struct inet6_dev *idev;
3752 struct inet6_ifaddr *ifa;
3753 LIST_HEAD(tmp_addr_list);
3754 bool keep_addr = false;
3760 rt6_disable_ip(dev, event);
3762 idev = __in6_dev_get(dev);
3767 * Step 1: remove reference to ipv6 device from parent device.
3773 /* protected by rtnl_lock */
3774 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3776 /* Step 1.5: remove snmp6 entry */
3777 snmp6_unregister_dev(idev);
3781 /* combine the user config with event to determine if permanent
3782 * addresses are to be removed from address hash table
3784 if (!unregister && !idev->cnf.disable_ipv6) {
3785 /* aggregate the system setting and interface setting */
3786 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3789 _keep_addr = idev->cnf.keep_addr_on_down;
3791 keep_addr = (_keep_addr > 0);
3794 /* Step 2: clear hash table */
3795 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3796 struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
3798 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
3800 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3801 if (ifa->idev == idev) {
3802 addrconf_del_dad_work(ifa);
3803 /* combined flag + permanent flag decide if
3804 * address is retained on a down event
3807 !(ifa->flags & IFA_F_PERMANENT) ||
3808 addr_is_local(&ifa->addr)) {
3809 hlist_del_init_rcu(&ifa->addr_lst);
3814 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
3817 write_lock_bh(&idev->lock);
3819 addrconf_del_rs_timer(idev);
3821 /* Step 2: clear flags for stateless addrconf, repeated down
3824 was_ready = idev->if_flags & IF_READY;
3826 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3828 /* Step 3: clear tempaddr list */
3829 while (!list_empty(&idev->tempaddr_list)) {
3830 ifa = list_first_entry(&idev->tempaddr_list,
3831 struct inet6_ifaddr, tmp_list);
3832 list_del(&ifa->tmp_list);
3833 write_unlock_bh(&idev->lock);
3834 spin_lock_bh(&ifa->lock);
3837 in6_ifa_put(ifa->ifpub);
3840 spin_unlock_bh(&ifa->lock);
3842 write_lock_bh(&idev->lock);
3845 list_for_each_entry(ifa, &idev->addr_list, if_list)
3846 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
3847 write_unlock_bh(&idev->lock);
3849 while (!list_empty(&tmp_addr_list)) {
3850 struct fib6_info *rt = NULL;
3853 ifa = list_first_entry(&tmp_addr_list,
3854 struct inet6_ifaddr, if_list_aux);
3855 list_del(&ifa->if_list_aux);
3857 addrconf_del_dad_work(ifa);
3859 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3860 !addr_is_local(&ifa->addr);
3862 spin_lock_bh(&ifa->lock);
3865 /* set state to skip the notifier below */
3866 state = INET6_IFADDR_STATE_DEAD;
3867 ifa->state = INET6_IFADDR_STATE_PREDAD;
3868 if (!(ifa->flags & IFA_F_NODAD))
3869 ifa->flags |= IFA_F_TENTATIVE;
3875 ifa->state = INET6_IFADDR_STATE_DEAD;
3878 spin_unlock_bh(&ifa->lock);
3881 ip6_del_rt(net, rt, false);
3883 if (state != INET6_IFADDR_STATE_DEAD) {
3884 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3885 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3887 if (idev->cnf.forwarding)
3888 addrconf_leave_anycast(ifa);
3889 addrconf_leave_solict(ifa->idev, &ifa->addr);
3893 write_lock_bh(&idev->lock);
3894 list_del_rcu(&ifa->if_list);
3895 write_unlock_bh(&idev->lock);
3900 /* Step 5: Discard anycast and multicast list */
3902 ipv6_ac_destroy_dev(idev);
3903 ipv6_mc_destroy_dev(idev);
3904 } else if (was_ready) {
3908 idev->tstamp = jiffies;
3911 /* Last: Shot the device (if unregistered) */
3913 addrconf_sysctl_unregister(idev);
3914 neigh_parms_release(&nd_tbl, idev->nd_parms);
3915 neigh_ifdown(&nd_tbl, dev);
3921 static void addrconf_rs_timer(struct timer_list *t)
3923 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3924 struct net_device *dev = idev->dev;
3925 struct in6_addr lladdr;
3927 write_lock(&idev->lock);
3928 if (idev->dead || !(idev->if_flags & IF_READY))
3931 if (!ipv6_accept_ra(idev))
3934 /* Announcement received after solicitation was sent */
3935 if (idev->if_flags & IF_RA_RCVD)
3938 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3939 write_unlock(&idev->lock);
3940 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3941 ndisc_send_rs(dev, &lladdr,
3942 &in6addr_linklocal_allrouters);
3946 write_lock(&idev->lock);
3947 idev->rs_interval = rfc3315_s14_backoff_update(
3948 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3949 /* The wait after the last probe can be shorter */
3950 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3951 idev->cnf.rtr_solicits) ?
3952 idev->cnf.rtr_solicit_delay :
3956 * Note: we do not support deprecated "all on-link"
3957 * assumption any longer.
3959 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3963 write_unlock(&idev->lock);
3969 * Duplicate Address Detection
3971 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3973 unsigned long rand_num;
3974 struct inet6_dev *idev = ifp->idev;
3977 if (ifp->flags & IFA_F_OPTIMISTIC)
3980 rand_num = get_random_u32_below(idev->cnf.rtr_solicit_delay ? : 1);
3983 if (idev->cnf.enhanced_dad ||
3984 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3986 get_random_bytes(&nonce, 6);
3989 ifp->dad_nonce = nonce;
3990 ifp->dad_probes = idev->cnf.dad_transmits;
3991 addrconf_mod_dad_work(ifp, rand_num);
3994 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3996 struct inet6_dev *idev = ifp->idev;
3997 struct net_device *dev = idev->dev;
3998 bool bump_id, notify = false;
4001 addrconf_join_solict(dev, &ifp->addr);
4003 read_lock_bh(&idev->lock);
4004 spin_lock(&ifp->lock);
4005 if (ifp->state == INET6_IFADDR_STATE_DEAD)
4009 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4010 (net->ipv6.devconf_all->accept_dad < 1 &&
4011 idev->cnf.accept_dad < 1) ||
4012 !(ifp->flags&IFA_F_TENTATIVE) ||
4013 ifp->flags & IFA_F_NODAD) {
4014 bool send_na = false;
4016 if (ifp->flags & IFA_F_TENTATIVE &&
4017 !(ifp->flags & IFA_F_OPTIMISTIC))
4019 bump_id = ifp->flags & IFA_F_TENTATIVE;
4020 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4021 spin_unlock(&ifp->lock);
4022 read_unlock_bh(&idev->lock);
4024 addrconf_dad_completed(ifp, bump_id, send_na);
4028 if (!(idev->if_flags & IF_READY)) {
4029 spin_unlock(&ifp->lock);
4030 read_unlock_bh(&idev->lock);
4032 * If the device is not ready:
4033 * - keep it tentative if it is a permanent address.
4034 * - otherwise, kill it.
4037 addrconf_dad_stop(ifp, 0);
4042 * Optimistic nodes can start receiving
4045 if (ifp->flags & IFA_F_OPTIMISTIC) {
4046 ip6_ins_rt(net, ifp->rt);
4047 if (ipv6_use_optimistic_addr(net, idev)) {
4048 /* Because optimistic nodes can use this address,
4049 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4055 addrconf_dad_kick(ifp);
4057 spin_unlock(&ifp->lock);
4058 read_unlock_bh(&idev->lock);
4060 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4063 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4065 bool begin_dad = false;
4067 spin_lock_bh(&ifp->lock);
4068 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4069 ifp->state = INET6_IFADDR_STATE_PREDAD;
4072 spin_unlock_bh(&ifp->lock);
4075 addrconf_mod_dad_work(ifp, 0);
4078 static void addrconf_dad_work(struct work_struct *w)
4080 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4081 struct inet6_ifaddr,
4083 struct inet6_dev *idev = ifp->idev;
4084 bool bump_id, disable_ipv6 = false;
4085 struct in6_addr mcaddr;
4091 } action = DAD_PROCESS;
4095 spin_lock_bh(&ifp->lock);
4096 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4098 ifp->state = INET6_IFADDR_STATE_DAD;
4099 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4101 ifp->state = INET6_IFADDR_STATE_POSTDAD;
4103 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4104 idev->cnf.accept_dad > 1) &&
4105 !idev->cnf.disable_ipv6 &&
4106 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4107 struct in6_addr addr;
4109 addr.s6_addr32[0] = htonl(0xfe800000);
4110 addr.s6_addr32[1] = 0;
4112 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4113 ipv6_addr_equal(&ifp->addr, &addr)) {
4114 /* DAD failed for link-local based on MAC */
4115 idev->cnf.disable_ipv6 = 1;
4117 pr_info("%s: IPv6 being disabled!\n",
4118 ifp->idev->dev->name);
4119 disable_ipv6 = true;
4123 spin_unlock_bh(&ifp->lock);
4125 if (action == DAD_BEGIN) {
4126 addrconf_dad_begin(ifp);
4128 } else if (action == DAD_ABORT) {
4130 addrconf_dad_stop(ifp, 1);
4132 addrconf_ifdown(idev->dev, false);
4136 if (!ifp->dad_probes && addrconf_dad_end(ifp))
4139 write_lock_bh(&idev->lock);
4140 if (idev->dead || !(idev->if_flags & IF_READY)) {
4141 write_unlock_bh(&idev->lock);
4145 spin_lock(&ifp->lock);
4146 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4147 spin_unlock(&ifp->lock);
4148 write_unlock_bh(&idev->lock);
4152 if (ifp->dad_probes == 0) {
4153 bool send_na = false;
4156 * DAD was successful
4159 if (ifp->flags & IFA_F_TENTATIVE &&
4160 !(ifp->flags & IFA_F_OPTIMISTIC))
4162 bump_id = ifp->flags & IFA_F_TENTATIVE;
4163 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4164 spin_unlock(&ifp->lock);
4165 write_unlock_bh(&idev->lock);
4167 addrconf_dad_completed(ifp, bump_id, send_na);
4173 addrconf_mod_dad_work(ifp,
4174 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
4176 spin_unlock(&ifp->lock);
4177 write_unlock_bh(&idev->lock);
4179 /* send a neighbour solicitation for our addr */
4180 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4181 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4188 /* ifp->idev must be at least read locked */
4189 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4191 struct inet6_ifaddr *ifpiter;
4192 struct inet6_dev *idev = ifp->idev;
4194 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4195 if (ifpiter->scope > IFA_LINK)
4197 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4198 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4199 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4206 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4209 struct net_device *dev = ifp->idev->dev;
4210 struct in6_addr lladdr;
4211 bool send_rs, send_mld;
4213 addrconf_del_dad_work(ifp);
4216 * Configure the address for reception. Now it is valid.
4219 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4221 /* If added prefix is link local and we are prepared to process
4222 router advertisements, start sending router solicitations.
4225 read_lock_bh(&ifp->idev->lock);
4226 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4227 send_rs = send_mld &&
4228 ipv6_accept_ra(ifp->idev) &&
4229 ifp->idev->cnf.rtr_solicits != 0 &&
4230 (dev->flags & IFF_LOOPBACK) == 0 &&
4231 (dev->type != ARPHRD_TUNNEL) &&
4232 !netif_is_team_port(dev);
4233 read_unlock_bh(&ifp->idev->lock);
4235 /* While dad is in progress mld report's source address is in6_addrany.
4236 * Resend with proper ll now.
4239 ipv6_mc_dad_complete(ifp->idev);
4241 /* send unsolicited NA if enabled */
4243 (ifp->idev->cnf.ndisc_notify ||
4244 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4245 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4246 /*router=*/ !!ifp->idev->cnf.forwarding,
4247 /*solicited=*/ false, /*override=*/ true,
4253 * If a host as already performed a random delay
4254 * [...] as part of DAD [...] there is no need
4255 * to delay again before sending the first RS
4257 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4259 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4261 write_lock_bh(&ifp->idev->lock);
4262 spin_lock(&ifp->lock);
4263 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4264 ifp->idev->cnf.rtr_solicit_interval);
4265 ifp->idev->rs_probes = 1;
4266 ifp->idev->if_flags |= IF_RS_SENT;
4267 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4268 spin_unlock(&ifp->lock);
4269 write_unlock_bh(&ifp->idev->lock);
4273 rt_genid_bump_ipv6(dev_net(dev));
4275 /* Make sure that a new temporary address will be created
4276 * before this temporary address becomes deprecated.
4278 if (ifp->flags & IFA_F_TEMPORARY)
4279 addrconf_verify_rtnl(dev_net(dev));
4282 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4284 struct inet6_ifaddr *ifp;
4286 read_lock_bh(&idev->lock);
4287 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4288 spin_lock(&ifp->lock);
4289 if ((ifp->flags & IFA_F_TENTATIVE &&
4290 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4292 ifp->state = INET6_IFADDR_STATE_PREDAD;
4293 addrconf_dad_kick(ifp);
4295 spin_unlock(&ifp->lock);
4297 read_unlock_bh(&idev->lock);
4300 #ifdef CONFIG_PROC_FS
4301 struct if6_iter_state {
4302 struct seq_net_private p;
4307 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4309 struct if6_iter_state *state = seq->private;
4310 struct net *net = seq_file_net(seq);
4311 struct inet6_ifaddr *ifa = NULL;
4314 /* initial bucket if pos is 0 */
4320 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4321 hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
4323 /* sync with offset */
4324 if (p < state->offset) {
4331 /* prepare for next bucket */
4338 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4339 struct inet6_ifaddr *ifa)
4341 struct if6_iter_state *state = seq->private;
4342 struct net *net = seq_file_net(seq);
4344 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4350 while (++state->bucket < IN6_ADDR_HSIZE) {
4351 hlist_for_each_entry_rcu(ifa,
4352 &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4360 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4364 return if6_get_first(seq, *pos);
4367 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4369 struct inet6_ifaddr *ifa;
4371 ifa = if6_get_next(seq, v);
4376 static void if6_seq_stop(struct seq_file *seq, void *v)
4382 static int if6_seq_show(struct seq_file *seq, void *v)
4384 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4385 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4387 ifp->idev->dev->ifindex,
4391 ifp->idev->dev->name);
4395 static const struct seq_operations if6_seq_ops = {
4396 .start = if6_seq_start,
4397 .next = if6_seq_next,
4398 .show = if6_seq_show,
4399 .stop = if6_seq_stop,
4402 static int __net_init if6_proc_net_init(struct net *net)
4404 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4405 sizeof(struct if6_iter_state)))
4410 static void __net_exit if6_proc_net_exit(struct net *net)
4412 remove_proc_entry("if_inet6", net->proc_net);
4415 static struct pernet_operations if6_proc_net_ops = {
4416 .init = if6_proc_net_init,
4417 .exit = if6_proc_net_exit,
4420 int __init if6_proc_init(void)
4422 return register_pernet_subsys(&if6_proc_net_ops);
4425 void if6_proc_exit(void)
4427 unregister_pernet_subsys(&if6_proc_net_ops);
4429 #endif /* CONFIG_PROC_FS */
4431 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4432 /* Check if address is a home address configured on any interface. */
4433 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4435 unsigned int hash = inet6_addr_hash(net, addr);
4436 struct inet6_ifaddr *ifp = NULL;
4440 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4441 if (ipv6_addr_equal(&ifp->addr, addr) &&
4442 (ifp->flags & IFA_F_HOMEADDRESS)) {
4452 /* RFC6554 has some algorithm to avoid loops in segment routing by
4453 * checking if the segments contains any of a local interface address.
4457 * To detect loops in the SRH, a router MUST determine if the SRH
4458 * includes multiple addresses assigned to any interface on that router.
4459 * If such addresses appear more than once and are separated by at least
4460 * one address not assigned to that router.
4462 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4463 unsigned char nsegs)
4465 const struct in6_addr *addr;
4466 int i, ret = 0, found = 0;
4467 struct inet6_ifaddr *ifp;
4468 bool separated = false;
4473 for (i = 0; i < nsegs; i++) {
4475 hash = inet6_addr_hash(net, addr);
4478 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4480 if (ipv6_addr_equal(&ifp->addr, addr)) {
4487 if (found > 1 && separated) {
4504 * Periodic address status verification
4507 static void addrconf_verify_rtnl(struct net *net)
4509 unsigned long now, next, next_sec, next_sched;
4510 struct inet6_ifaddr *ifp;
4517 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4519 cancel_delayed_work(&net->ipv6.addr_chk_work);
4521 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4523 hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
4526 /* When setting preferred_lft to a value not zero or
4527 * infinity, while valid_lft is infinity
4528 * IFA_F_PERMANENT has a non-infinity life time.
4530 if ((ifp->flags & IFA_F_PERMANENT) &&
4531 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4534 spin_lock(&ifp->lock);
4535 /* We try to batch several events at once. */
4536 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4538 if ((ifp->flags&IFA_F_TEMPORARY) &&
4539 !(ifp->flags&IFA_F_TENTATIVE) &&
4540 ifp->prefered_lft != INFINITY_LIFE_TIME &&
4541 !ifp->regen_count && ifp->ifpub) {
4542 /* This is a non-regenerated temporary addr. */
4544 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4545 ifp->idev->cnf.dad_transmits *
4546 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
4548 if (age + regen_advance >= ifp->prefered_lft) {
4549 struct inet6_ifaddr *ifpub = ifp->ifpub;
4550 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4551 next = ifp->tstamp + ifp->prefered_lft * HZ;
4555 in6_ifa_hold(ifpub);
4556 spin_unlock(&ifp->lock);
4558 spin_lock(&ifpub->lock);
4559 ifpub->regen_count = 0;
4560 spin_unlock(&ifpub->lock);
4561 rcu_read_unlock_bh();
4562 ipv6_create_tempaddr(ifpub, true);
4567 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4568 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4571 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4572 age >= ifp->valid_lft) {
4573 spin_unlock(&ifp->lock);
4575 rcu_read_unlock_bh();
4579 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4580 spin_unlock(&ifp->lock);
4582 } else if (age >= ifp->prefered_lft) {
4583 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4586 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4588 ifp->flags |= IFA_F_DEPRECATED;
4591 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4592 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4593 next = ifp->tstamp + ifp->valid_lft * HZ;
4595 spin_unlock(&ifp->lock);
4600 ipv6_ifa_notify(0, ifp);
4605 /* ifp->prefered_lft <= ifp->valid_lft */
4606 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4607 next = ifp->tstamp + ifp->prefered_lft * HZ;
4608 spin_unlock(&ifp->lock);
4613 next_sec = round_jiffies_up(next);
4616 /* If rounded timeout is accurate enough, accept it. */
4617 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4618 next_sched = next_sec;
4620 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4621 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4622 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4624 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4625 now, next, next_sec, next_sched);
4626 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
4627 rcu_read_unlock_bh();
4630 static void addrconf_verify_work(struct work_struct *w)
4632 struct net *net = container_of(to_delayed_work(w), struct net,
4633 ipv6.addr_chk_work);
4636 addrconf_verify_rtnl(net);
4640 static void addrconf_verify(struct net *net)
4642 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4645 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4646 struct in6_addr **peer_pfx)
4648 struct in6_addr *pfx = NULL;
4653 pfx = nla_data(addr);
4656 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4658 pfx = nla_data(local);
4664 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4665 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4666 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4667 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4668 [IFA_FLAGS] = { .len = sizeof(u32) },
4669 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
4670 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
4671 [IFA_PROTO] = { .type = NLA_U8 },
4675 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4676 struct netlink_ext_ack *extack)
4678 struct net *net = sock_net(skb->sk);
4679 struct ifaddrmsg *ifm;
4680 struct nlattr *tb[IFA_MAX+1];
4681 struct in6_addr *pfx, *peer_pfx;
4685 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4686 ifa_ipv6_policy, extack);
4690 ifm = nlmsg_data(nlh);
4691 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4695 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4697 /* We ignore other flags so far. */
4698 ifa_flags &= IFA_F_MANAGETEMPADDR;
4700 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4701 ifm->ifa_prefixlen);
4704 static int modify_prefix_route(struct inet6_ifaddr *ifp,
4705 unsigned long expires, u32 flags,
4708 struct fib6_info *f6i;
4711 f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4713 ifp->idev->dev, 0, RTF_DEFAULT, true);
4717 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4718 if (f6i->fib6_metric != prio) {
4719 /* delete old one */
4720 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4723 addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4725 ifp->rt_priority, ifp->idev->dev,
4726 expires, flags, GFP_KERNEL);
4729 fib6_clean_expires(f6i);
4731 fib6_set_expires(f6i, expires);
4733 fib6_info_release(f6i);
4739 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
4740 struct ifa6_config *cfg)
4744 unsigned long timeout;
4745 bool was_managetempaddr;
4746 bool had_prefixroute;
4747 bool new_peer = false;
4751 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4754 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4755 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4758 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4759 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4761 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
4762 if (addrconf_finite_timeout(timeout)) {
4763 expires = jiffies_to_clock_t(timeout * HZ);
4764 cfg->valid_lft = timeout;
4765 flags = RTF_EXPIRES;
4769 cfg->ifa_flags |= IFA_F_PERMANENT;
4772 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
4773 if (addrconf_finite_timeout(timeout)) {
4775 cfg->ifa_flags |= IFA_F_DEPRECATED;
4776 cfg->preferred_lft = timeout;
4779 if (cfg->peer_pfx &&
4780 memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4781 if (!ipv6_addr_any(&ifp->peer_addr))
4782 cleanup_prefix_route(ifp, expires, true, true);
4786 spin_lock_bh(&ifp->lock);
4787 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4788 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4789 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4790 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4791 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4792 IFA_F_NOPREFIXROUTE);
4793 ifp->flags |= cfg->ifa_flags;
4794 ifp->tstamp = jiffies;
4795 ifp->valid_lft = cfg->valid_lft;
4796 ifp->prefered_lft = cfg->preferred_lft;
4797 ifp->ifa_proto = cfg->ifa_proto;
4799 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4800 ifp->rt_priority = cfg->rt_priority;
4803 ifp->peer_addr = *cfg->peer_pfx;
4805 spin_unlock_bh(&ifp->lock);
4806 if (!(ifp->flags&IFA_F_TENTATIVE))
4807 ipv6_ifa_notify(0, ifp);
4809 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4812 if (had_prefixroute)
4813 rc = modify_prefix_route(ifp, expires, flags, false);
4815 /* prefix route could have been deleted; if so restore it */
4816 if (rc == -ENOENT) {
4817 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4818 ifp->rt_priority, ifp->idev->dev,
4819 expires, flags, GFP_KERNEL);
4822 if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4823 rc = modify_prefix_route(ifp, expires, flags, true);
4825 if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4826 addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4827 ifp->rt_priority, ifp->idev->dev,
4828 expires, flags, GFP_KERNEL);
4830 } else if (had_prefixroute) {
4831 enum cleanup_prefix_rt_t action;
4832 unsigned long rt_expires;
4834 write_lock_bh(&ifp->idev->lock);
4835 action = check_cleanup_prefix_route(ifp, &rt_expires);
4836 write_unlock_bh(&ifp->idev->lock);
4838 if (action != CLEANUP_PREFIX_RT_NOP) {
4839 cleanup_prefix_route(ifp, rt_expires,
4840 action == CLEANUP_PREFIX_RT_DEL, false);
4844 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4845 if (was_managetempaddr &&
4846 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4848 cfg->preferred_lft = 0;
4850 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4851 cfg->preferred_lft, !was_managetempaddr,
4855 addrconf_verify_rtnl(net);
4861 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4862 struct netlink_ext_ack *extack)
4864 struct net *net = sock_net(skb->sk);
4865 struct ifaddrmsg *ifm;
4866 struct nlattr *tb[IFA_MAX+1];
4867 struct in6_addr *peer_pfx;
4868 struct inet6_ifaddr *ifa;
4869 struct net_device *dev;
4870 struct inet6_dev *idev;
4871 struct ifa6_config cfg;
4874 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4875 ifa_ipv6_policy, extack);
4879 memset(&cfg, 0, sizeof(cfg));
4881 ifm = nlmsg_data(nlh);
4882 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4886 cfg.peer_pfx = peer_pfx;
4887 cfg.plen = ifm->ifa_prefixlen;
4888 if (tb[IFA_RT_PRIORITY])
4889 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4892 cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
4894 cfg.valid_lft = INFINITY_LIFE_TIME;
4895 cfg.preferred_lft = INFINITY_LIFE_TIME;
4897 if (tb[IFA_CACHEINFO]) {
4898 struct ifa_cacheinfo *ci;
4900 ci = nla_data(tb[IFA_CACHEINFO]);
4901 cfg.valid_lft = ci->ifa_valid;
4902 cfg.preferred_lft = ci->ifa_prefered;
4905 dev = __dev_get_by_index(net, ifm->ifa_index);
4910 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4912 cfg.ifa_flags = ifm->ifa_flags;
4914 /* We ignore other flags so far. */
4915 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4916 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4917 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4919 idev = ipv6_find_idev(dev);
4921 return PTR_ERR(idev);
4923 if (!ipv6_allow_optimistic_dad(net, idev))
4924 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4926 if (cfg.ifa_flags & IFA_F_NODAD &&
4927 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4928 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4932 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
4935 * It would be best to check for !NLM_F_CREATE here but
4936 * userspace already relies on not having to provide this.
4938 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
4941 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4942 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4945 err = inet6_addr_modify(net, ifa, &cfg);
4952 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4953 u8 scope, int ifindex)
4955 struct ifaddrmsg *ifm;
4957 ifm = nlmsg_data(nlh);
4958 ifm->ifa_family = AF_INET6;
4959 ifm->ifa_prefixlen = prefixlen;
4960 ifm->ifa_flags = flags;
4961 ifm->ifa_scope = scope;
4962 ifm->ifa_index = ifindex;
4965 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4966 unsigned long tstamp, u32 preferred, u32 valid)
4968 struct ifa_cacheinfo ci;
4970 ci.cstamp = cstamp_delta(cstamp);
4971 ci.tstamp = cstamp_delta(tstamp);
4972 ci.ifa_prefered = preferred;
4973 ci.ifa_valid = valid;
4975 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4978 static inline int rt_scope(int ifa_scope)
4980 if (ifa_scope & IFA_HOST)
4981 return RT_SCOPE_HOST;
4982 else if (ifa_scope & IFA_LINK)
4983 return RT_SCOPE_LINK;
4984 else if (ifa_scope & IFA_SITE)
4985 return RT_SCOPE_SITE;
4987 return RT_SCOPE_UNIVERSE;
4990 static inline int inet6_ifaddr_msgsize(void)
4992 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4993 + nla_total_size(16) /* IFA_LOCAL */
4994 + nla_total_size(16) /* IFA_ADDRESS */
4995 + nla_total_size(sizeof(struct ifa_cacheinfo))
4996 + nla_total_size(4) /* IFA_FLAGS */
4997 + nla_total_size(1) /* IFA_PROTO */
4998 + nla_total_size(4) /* IFA_RT_PRIORITY */;
5007 struct inet6_fill_args {
5014 enum addr_type_t type;
5017 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
5018 struct inet6_fill_args *args)
5020 struct nlmsghdr *nlh;
5021 u32 preferred, valid;
5023 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5024 sizeof(struct ifaddrmsg), args->flags);
5028 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5029 ifa->idev->dev->ifindex);
5031 if (args->netnsid >= 0 &&
5032 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
5035 spin_lock_bh(&ifa->lock);
5036 if (!((ifa->flags&IFA_F_PERMANENT) &&
5037 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
5038 preferred = ifa->prefered_lft;
5039 valid = ifa->valid_lft;
5040 if (preferred != INFINITY_LIFE_TIME) {
5041 long tval = (jiffies - ifa->tstamp)/HZ;
5042 if (preferred > tval)
5046 if (valid != INFINITY_LIFE_TIME) {
5054 preferred = INFINITY_LIFE_TIME;
5055 valid = INFINITY_LIFE_TIME;
5057 spin_unlock_bh(&ifa->lock);
5059 if (!ipv6_addr_any(&ifa->peer_addr)) {
5060 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5061 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5064 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5067 if (ifa->rt_priority &&
5068 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
5071 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
5074 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
5077 if (ifa->ifa_proto &&
5078 nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto))
5081 nlmsg_end(skb, nlh);
5085 nlmsg_cancel(skb, nlh);
5089 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
5090 struct inet6_fill_args *args)
5092 struct nlmsghdr *nlh;
5093 u8 scope = RT_SCOPE_UNIVERSE;
5094 int ifindex = ifmca->idev->dev->ifindex;
5096 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5097 scope = RT_SCOPE_SITE;
5099 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5100 sizeof(struct ifaddrmsg), args->flags);
5104 if (args->netnsid >= 0 &&
5105 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5106 nlmsg_cancel(skb, nlh);
5110 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5111 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
5112 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
5113 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5114 nlmsg_cancel(skb, nlh);
5118 nlmsg_end(skb, nlh);
5122 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
5123 struct inet6_fill_args *args)
5125 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5126 int ifindex = dev ? dev->ifindex : 1;
5127 struct nlmsghdr *nlh;
5128 u8 scope = RT_SCOPE_UNIVERSE;
5130 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5131 scope = RT_SCOPE_SITE;
5133 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5134 sizeof(struct ifaddrmsg), args->flags);
5138 if (args->netnsid >= 0 &&
5139 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5140 nlmsg_cancel(skb, nlh);
5144 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5145 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
5146 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
5147 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5148 nlmsg_cancel(skb, nlh);
5152 nlmsg_end(skb, nlh);
5156 /* called with rcu_read_lock() */
5157 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5158 struct netlink_callback *cb, int s_ip_idx,
5159 struct inet6_fill_args *fillargs)
5161 struct ifmcaddr6 *ifmca;
5162 struct ifacaddr6 *ifaca;
5166 read_lock_bh(&idev->lock);
5167 switch (fillargs->type) {
5168 case UNICAST_ADDR: {
5169 struct inet6_ifaddr *ifa;
5170 fillargs->event = RTM_NEWADDR;
5172 /* unicast address incl. temp addr */
5173 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5174 if (ip_idx < s_ip_idx)
5176 err = inet6_fill_ifaddr(skb, ifa, fillargs);
5179 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5185 case MULTICAST_ADDR:
5186 read_unlock_bh(&idev->lock);
5187 fillargs->event = RTM_GETMULTICAST;
5189 /* multicast address */
5190 for (ifmca = rtnl_dereference(idev->mc_list);
5192 ifmca = rtnl_dereference(ifmca->next), ip_idx++) {
5193 if (ip_idx < s_ip_idx)
5195 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5199 read_lock_bh(&idev->lock);
5202 fillargs->event = RTM_GETANYCAST;
5203 /* anycast address */
5204 for (ifaca = idev->ac_list; ifaca;
5205 ifaca = ifaca->aca_next, ip_idx++) {
5206 if (ip_idx < s_ip_idx)
5208 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5216 read_unlock_bh(&idev->lock);
5217 cb->args[2] = ip_idx;
5221 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5222 struct inet6_fill_args *fillargs,
5223 struct net **tgt_net, struct sock *sk,
5224 struct netlink_callback *cb)
5226 struct netlink_ext_ack *extack = cb->extack;
5227 struct nlattr *tb[IFA_MAX+1];
5228 struct ifaddrmsg *ifm;
5231 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5232 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5236 ifm = nlmsg_data(nlh);
5237 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5238 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5242 fillargs->ifindex = ifm->ifa_index;
5243 if (fillargs->ifindex) {
5244 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5245 fillargs->flags |= NLM_F_DUMP_FILTERED;
5248 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5249 ifa_ipv6_policy, extack);
5253 for (i = 0; i <= IFA_MAX; ++i) {
5257 if (i == IFA_TARGET_NETNSID) {
5260 fillargs->netnsid = nla_get_s32(tb[i]);
5261 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5263 fillargs->netnsid = -1;
5264 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5265 return PTR_ERR(net);
5269 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5277 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5278 enum addr_type_t type)
5280 const struct nlmsghdr *nlh = cb->nlh;
5281 struct inet6_fill_args fillargs = {
5282 .portid = NETLINK_CB(cb->skb).portid,
5283 .seq = cb->nlh->nlmsg_seq,
5284 .flags = NLM_F_MULTI,
5288 struct net *tgt_net = sock_net(skb->sk);
5289 int idx, s_idx, s_ip_idx;
5291 struct net_device *dev;
5292 struct inet6_dev *idev;
5293 struct hlist_head *head;
5297 s_idx = idx = cb->args[1];
5298 s_ip_idx = cb->args[2];
5300 if (cb->strict_check) {
5301 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5307 if (fillargs.ifindex) {
5308 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5313 idev = __in6_dev_get(dev);
5315 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5325 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
5326 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5328 head = &tgt_net->dev_index_head[h];
5329 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5332 if (h > s_h || idx > s_idx)
5334 idev = __in6_dev_get(dev);
5338 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
5350 if (fillargs.netnsid >= 0)
5353 return skb->len ? : err;
5356 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5358 enum addr_type_t type = UNICAST_ADDR;
5360 return inet6_dump_addr(skb, cb, type);
5363 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5365 enum addr_type_t type = MULTICAST_ADDR;
5367 return inet6_dump_addr(skb, cb, type);
5371 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5373 enum addr_type_t type = ANYCAST_ADDR;
5375 return inet6_dump_addr(skb, cb, type);
5378 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5379 const struct nlmsghdr *nlh,
5381 struct netlink_ext_ack *extack)
5383 struct ifaddrmsg *ifm;
5386 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5387 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5391 if (!netlink_strict_get_check(skb))
5392 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5393 ifa_ipv6_policy, extack);
5395 ifm = nlmsg_data(nlh);
5396 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5397 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5401 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5402 ifa_ipv6_policy, extack);
5406 for (i = 0; i <= IFA_MAX; i++) {
5411 case IFA_TARGET_NETNSID:
5416 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5424 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5425 struct netlink_ext_ack *extack)
5427 struct net *tgt_net = sock_net(in_skb->sk);
5428 struct inet6_fill_args fillargs = {
5429 .portid = NETLINK_CB(in_skb).portid,
5430 .seq = nlh->nlmsg_seq,
5431 .event = RTM_NEWADDR,
5435 struct ifaddrmsg *ifm;
5436 struct nlattr *tb[IFA_MAX+1];
5437 struct in6_addr *addr = NULL, *peer;
5438 struct net_device *dev = NULL;
5439 struct inet6_ifaddr *ifa;
5440 struct sk_buff *skb;
5443 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5447 if (tb[IFA_TARGET_NETNSID]) {
5448 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5450 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5452 if (IS_ERR(tgt_net))
5453 return PTR_ERR(tgt_net);
5456 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5460 ifm = nlmsg_data(nlh);
5462 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5464 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5466 err = -EADDRNOTAVAIL;
5470 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5476 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5478 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5479 WARN_ON(err == -EMSGSIZE);
5483 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5488 if (fillargs.netnsid >= 0)
5494 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5496 struct sk_buff *skb;
5497 struct net *net = dev_net(ifa->idev->dev);
5498 struct inet6_fill_args fillargs = {
5507 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5511 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5513 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5514 WARN_ON(err == -EMSGSIZE);
5518 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5522 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5525 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
5526 __s32 *array, int bytes)
5528 BUG_ON(bytes < (DEVCONF_MAX * 4));
5530 memset(array, 0, bytes);
5531 array[DEVCONF_FORWARDING] = cnf->forwarding;
5532 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5533 array[DEVCONF_MTU6] = cnf->mtu6;
5534 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5535 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5536 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5537 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5538 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
5539 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5540 jiffies_to_msecs(cnf->rtr_solicit_interval);
5541 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5542 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
5543 array[DEVCONF_RTR_SOLICIT_DELAY] =
5544 jiffies_to_msecs(cnf->rtr_solicit_delay);
5545 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5546 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5547 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5548 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5549 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
5550 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5551 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5552 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5553 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5554 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
5555 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
5556 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
5557 array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric;
5558 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5559 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5560 #ifdef CONFIG_IPV6_ROUTER_PREF
5561 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
5562 array[DEVCONF_RTR_PROBE_INTERVAL] =
5563 jiffies_to_msecs(cnf->rtr_probe_interval);
5564 #ifdef CONFIG_IPV6_ROUTE_INFO
5565 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
5566 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5569 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
5570 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
5571 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5572 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
5573 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
5575 #ifdef CONFIG_IPV6_MROUTE
5576 array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
5578 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
5579 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5580 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
5581 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5582 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5583 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5584 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
5585 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
5586 /* we omit DEVCONF_STABLE_SECRET for now */
5587 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5588 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
5589 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5590 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5591 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5592 #ifdef CONFIG_IPV6_SEG6_HMAC
5593 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5595 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5596 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5597 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
5598 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
5599 array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
5600 array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled;
5601 array[DEVCONF_IOAM6_ID] = cnf->ioam6_id;
5602 array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide;
5603 array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier;
5604 array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na;
5607 static inline size_t inet6_ifla6_size(void)
5609 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5610 + nla_total_size(sizeof(struct ifla_cacheinfo))
5611 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5612 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5613 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5614 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5615 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5616 + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5620 static inline size_t inet6_if_nlmsg_size(void)
5622 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5623 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5624 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5625 + nla_total_size(4) /* IFLA_MTU */
5626 + nla_total_size(4) /* IFLA_LINK */
5627 + nla_total_size(1) /* IFLA_OPERSTATE */
5628 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5631 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5635 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5638 /* Use put_unaligned() because stats may not be aligned for u64. */
5639 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5640 for (i = 1; i < ICMP6_MIB_MAX; i++)
5641 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5643 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5646 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5647 int bytes, size_t syncpoff)
5650 u64 buff[IPSTATS_MIB_MAX];
5651 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5655 memset(buff, 0, sizeof(buff));
5656 buff[0] = IPSTATS_MIB_MAX;
5658 for_each_possible_cpu(c) {
5659 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5660 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5663 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5664 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5667 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5671 case IFLA_INET6_STATS:
5672 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5673 offsetof(struct ipstats_mib, syncp));
5675 case IFLA_INET6_ICMP6STATS:
5676 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5681 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5682 u32 ext_filter_mask)
5685 struct ifla_cacheinfo ci;
5687 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5688 goto nla_put_failure;
5689 ci.max_reasm_len = IPV6_MAXPLEN;
5690 ci.tstamp = cstamp_delta(idev->tstamp);
5691 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5692 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5693 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5694 goto nla_put_failure;
5695 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5697 goto nla_put_failure;
5698 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5700 /* XXX - MC not implemented */
5702 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5705 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5707 goto nla_put_failure;
5708 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5710 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5712 goto nla_put_failure;
5713 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5715 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5717 goto nla_put_failure;
5718 read_lock_bh(&idev->lock);
5719 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5720 read_unlock_bh(&idev->lock);
5722 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5723 goto nla_put_failure;
5726 nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu))
5727 goto nla_put_failure;
5735 static size_t inet6_get_link_af_size(const struct net_device *dev,
5736 u32 ext_filter_mask)
5738 if (!__in6_dev_get(dev))
5741 return inet6_ifla6_size();
5744 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5745 u32 ext_filter_mask)
5747 struct inet6_dev *idev = __in6_dev_get(dev);
5752 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5758 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
5759 struct netlink_ext_ack *extack)
5761 struct inet6_ifaddr *ifp;
5762 struct net_device *dev = idev->dev;
5763 bool clear_token, update_rs = false;
5764 struct in6_addr ll_addr;
5771 if (dev->flags & IFF_LOOPBACK) {
5772 NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5776 if (dev->flags & IFF_NOARP) {
5777 NL_SET_ERR_MSG_MOD(extack,
5778 "Device does not do neighbour discovery");
5782 if (!ipv6_accept_ra(idev)) {
5783 NL_SET_ERR_MSG_MOD(extack,
5784 "Router advertisement is disabled on device");
5788 if (idev->cnf.rtr_solicits == 0) {
5789 NL_SET_ERR_MSG(extack,
5790 "Router solicitation is disabled on device");
5794 write_lock_bh(&idev->lock);
5796 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5797 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5799 write_unlock_bh(&idev->lock);
5801 clear_token = ipv6_addr_any(token);
5805 if (!idev->dead && (idev->if_flags & IF_READY) &&
5806 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5807 IFA_F_OPTIMISTIC)) {
5808 /* If we're not ready, then normal ifup will take care
5809 * of this. Otherwise, we need to request our rs here.
5811 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5816 write_lock_bh(&idev->lock);
5819 idev->if_flags |= IF_RS_SENT;
5820 idev->rs_interval = rfc3315_s14_backoff_init(
5821 idev->cnf.rtr_solicit_interval);
5822 idev->rs_probes = 1;
5823 addrconf_mod_rs_timer(idev, idev->rs_interval);
5826 /* Well, that's kinda nasty ... */
5827 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5828 spin_lock(&ifp->lock);
5829 if (ifp->tokenized) {
5831 ifp->prefered_lft = 0;
5833 spin_unlock(&ifp->lock);
5836 write_unlock_bh(&idev->lock);
5837 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5838 addrconf_verify_rtnl(dev_net(dev));
5842 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5843 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5844 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5845 [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT,
5847 "IFLA_INET6_RA_MTU can not be set" },
5850 static int check_addr_gen_mode(int mode)
5852 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5853 mode != IN6_ADDR_GEN_MODE_NONE &&
5854 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5855 mode != IN6_ADDR_GEN_MODE_RANDOM)
5860 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5863 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5864 !idev->cnf.stable_secret.initialized &&
5865 !net->ipv6.devconf_dflt->stable_secret.initialized)
5870 static int inet6_validate_link_af(const struct net_device *dev,
5871 const struct nlattr *nla,
5872 struct netlink_ext_ack *extack)
5874 struct nlattr *tb[IFLA_INET6_MAX + 1];
5875 struct inet6_dev *idev = NULL;
5879 idev = __in6_dev_get(dev);
5881 return -EAFNOSUPPORT;
5884 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5885 inet6_af_policy, extack);
5889 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
5892 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5893 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5895 if (check_addr_gen_mode(mode) < 0)
5897 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
5904 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
5905 struct netlink_ext_ack *extack)
5907 struct inet6_dev *idev = __in6_dev_get(dev);
5908 struct nlattr *tb[IFLA_INET6_MAX + 1];
5912 return -EAFNOSUPPORT;
5914 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5917 if (tb[IFLA_INET6_TOKEN]) {
5918 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
5924 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5925 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5927 idev->cnf.addr_gen_mode = mode;
5933 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5934 u32 portid, u32 seq, int event, unsigned int flags)
5936 struct net_device *dev = idev->dev;
5937 struct ifinfomsg *hdr;
5938 struct nlmsghdr *nlh;
5941 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5945 hdr = nlmsg_data(nlh);
5946 hdr->ifi_family = AF_INET6;
5948 hdr->ifi_type = dev->type;
5949 hdr->ifi_index = dev->ifindex;
5950 hdr->ifi_flags = dev_get_flags(dev);
5951 hdr->ifi_change = 0;
5953 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5955 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5956 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5957 (dev->ifindex != dev_get_iflink(dev) &&
5958 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5959 nla_put_u8(skb, IFLA_OPERSTATE,
5960 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5961 goto nla_put_failure;
5962 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
5964 goto nla_put_failure;
5966 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5967 goto nla_put_failure;
5969 nla_nest_end(skb, protoinfo);
5970 nlmsg_end(skb, nlh);
5974 nlmsg_cancel(skb, nlh);
5978 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5979 struct netlink_ext_ack *extack)
5981 struct ifinfomsg *ifm;
5983 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5984 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5988 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5989 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5993 ifm = nlmsg_data(nlh);
5994 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5995 ifm->ifi_change || ifm->ifi_index) {
5996 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6003 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6005 struct net *net = sock_net(skb->sk);
6008 struct net_device *dev;
6009 struct inet6_dev *idev;
6010 struct hlist_head *head;
6012 /* only requests using strict checking can pass data to
6013 * influence the dump
6015 if (cb->strict_check) {
6016 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6023 s_idx = cb->args[1];
6026 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6028 head = &net->dev_index_head[h];
6029 hlist_for_each_entry_rcu(dev, head, index_hlist) {
6032 idev = __in6_dev_get(dev);
6035 if (inet6_fill_ifinfo(skb, idev,
6036 NETLINK_CB(cb->skb).portid,
6038 RTM_NEWLINK, NLM_F_MULTI) < 0)
6052 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
6054 struct sk_buff *skb;
6055 struct net *net = dev_net(idev->dev);
6058 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
6062 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
6064 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6065 WARN_ON(err == -EMSGSIZE);
6069 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
6073 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
6076 static inline size_t inet6_prefix_nlmsg_size(void)
6078 return NLMSG_ALIGN(sizeof(struct prefixmsg))
6079 + nla_total_size(sizeof(struct in6_addr))
6080 + nla_total_size(sizeof(struct prefix_cacheinfo));
6083 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
6084 struct prefix_info *pinfo, u32 portid, u32 seq,
6085 int event, unsigned int flags)
6087 struct prefixmsg *pmsg;
6088 struct nlmsghdr *nlh;
6089 struct prefix_cacheinfo ci;
6091 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
6095 pmsg = nlmsg_data(nlh);
6096 pmsg->prefix_family = AF_INET6;
6097 pmsg->prefix_pad1 = 0;
6098 pmsg->prefix_pad2 = 0;
6099 pmsg->prefix_ifindex = idev->dev->ifindex;
6100 pmsg->prefix_len = pinfo->prefix_len;
6101 pmsg->prefix_type = pinfo->type;
6102 pmsg->prefix_pad3 = 0;
6103 pmsg->prefix_flags = 0;
6105 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
6106 if (pinfo->autoconf)
6107 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
6109 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6110 goto nla_put_failure;
6111 ci.preferred_time = ntohl(pinfo->prefered);
6112 ci.valid_time = ntohl(pinfo->valid);
6113 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6114 goto nla_put_failure;
6115 nlmsg_end(skb, nlh);
6119 nlmsg_cancel(skb, nlh);
6123 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
6124 struct prefix_info *pinfo)
6126 struct sk_buff *skb;
6127 struct net *net = dev_net(idev->dev);
6130 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
6134 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
6136 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6137 WARN_ON(err == -EMSGSIZE);
6141 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
6145 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
6148 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6150 struct net *net = dev_net(ifp->idev->dev);
6155 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
6160 * If the address was optimistic we inserted the route at the
6161 * start of our DAD process, so we don't need to do it again.
6162 * If the device was taken down in the middle of the DAD
6163 * cycle there is a race where we could get here without a
6164 * host route, so nothing to insert. That will be fixed when
6165 * the device is brought up.
6167 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6168 ip6_ins_rt(net, ifp->rt);
6169 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6170 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6171 &ifp->addr, ifp->idev->dev->name);
6174 if (ifp->idev->cnf.forwarding)
6175 addrconf_join_anycast(ifp);
6176 if (!ipv6_addr_any(&ifp->peer_addr))
6177 addrconf_prefix_route(&ifp->peer_addr, 128,
6178 ifp->rt_priority, ifp->idev->dev,
6182 if (ifp->idev->cnf.forwarding)
6183 addrconf_leave_anycast(ifp);
6184 addrconf_leave_solict(ifp->idev, &ifp->addr);
6185 if (!ipv6_addr_any(&ifp->peer_addr)) {
6186 struct fib6_info *rt;
6188 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
6189 ifp->idev->dev, 0, 0,
6192 ip6_del_rt(net, rt, false);
6195 ip6_del_rt(net, ifp->rt, false);
6198 rt_genid_bump_ipv6(net);
6201 atomic_inc(&net->ipv6.dev_addr_genid);
6204 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6206 if (likely(ifp->idev->dead == 0))
6207 __ipv6_ifa_notify(event, ifp);
6210 #ifdef CONFIG_SYSCTL
6212 static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
6213 void *buffer, size_t *lenp, loff_t *ppos)
6215 int *valp = ctl->data;
6218 struct ctl_table lctl;
6222 * ctl->data points to idev->cnf.forwarding, we should
6223 * not modify it until we get the rtnl lock.
6228 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6231 ret = addrconf_fixup_forwarding(ctl, valp, val);
6237 static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6238 void *buffer, size_t *lenp, loff_t *ppos)
6240 struct inet6_dev *idev = ctl->extra1;
6241 int min_mtu = IPV6_MIN_MTU;
6242 struct ctl_table lctl;
6245 lctl.extra1 = &min_mtu;
6246 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6248 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6251 static void dev_disable_change(struct inet6_dev *idev)
6253 struct netdev_notifier_info info;
6255 if (!idev || !idev->dev)
6258 netdev_notifier_info_init(&info, idev->dev);
6259 if (idev->cnf.disable_ipv6)
6260 addrconf_notify(NULL, NETDEV_DOWN, &info);
6262 addrconf_notify(NULL, NETDEV_UP, &info);
6265 static void addrconf_disable_change(struct net *net, __s32 newf)
6267 struct net_device *dev;
6268 struct inet6_dev *idev;
6270 for_each_netdev(net, dev) {
6271 idev = __in6_dev_get(dev);
6273 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6274 idev->cnf.disable_ipv6 = newf;
6276 dev_disable_change(idev);
6281 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
6286 if (!rtnl_trylock())
6287 return restart_syscall();
6289 net = (struct net *)table->extra2;
6293 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6298 if (p == &net->ipv6.devconf_all->disable_ipv6) {
6299 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6300 addrconf_disable_change(net, newf);
6301 } else if ((!newf) ^ (!old))
6302 dev_disable_change((struct inet6_dev *)table->extra1);
6308 static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
6309 void *buffer, size_t *lenp, loff_t *ppos)
6311 int *valp = ctl->data;
6314 struct ctl_table lctl;
6318 * ctl->data points to idev->cnf.disable_ipv6, we should
6319 * not modify it until we get the rtnl lock.
6324 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6327 ret = addrconf_disable_ipv6(ctl, valp, val);
6333 static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6334 void *buffer, size_t *lenp, loff_t *ppos)
6336 int *valp = ctl->data;
6341 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6344 if (write && old != new) {
6345 struct net *net = ctl->extra2;
6347 if (!rtnl_trylock())
6348 return restart_syscall();
6350 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
6351 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6352 NETCONFA_PROXY_NEIGH,
6353 NETCONFA_IFINDEX_DEFAULT,
6354 net->ipv6.devconf_dflt);
6355 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
6356 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6357 NETCONFA_PROXY_NEIGH,
6358 NETCONFA_IFINDEX_ALL,
6359 net->ipv6.devconf_all);
6361 struct inet6_dev *idev = ctl->extra1;
6363 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6364 NETCONFA_PROXY_NEIGH,
6374 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6375 void *buffer, size_t *lenp,
6380 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6381 struct net *net = (struct net *)ctl->extra2;
6382 struct ctl_table tmp = {
6384 .maxlen = sizeof(new_val),
6388 if (!rtnl_trylock())
6389 return restart_syscall();
6391 new_val = *((u32 *)ctl->data);
6393 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6398 if (check_addr_gen_mode(new_val) < 0) {
6404 if (check_stable_privacy(idev, net, new_val) < 0) {
6409 if (idev->cnf.addr_gen_mode != new_val) {
6410 idev->cnf.addr_gen_mode = new_val;
6411 addrconf_init_auto_addrs(idev->dev);
6413 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6414 struct net_device *dev;
6416 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6417 for_each_netdev(net, dev) {
6418 idev = __in6_dev_get(dev);
6420 idev->cnf.addr_gen_mode != new_val) {
6421 idev->cnf.addr_gen_mode = new_val;
6422 addrconf_init_auto_addrs(idev->dev);
6427 *((u32 *)ctl->data) = new_val;
6436 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6437 void *buffer, size_t *lenp,
6441 struct in6_addr addr;
6442 char str[IPV6_MAX_STRLEN];
6443 struct ctl_table lctl = *ctl;
6444 struct net *net = ctl->extra2;
6445 struct ipv6_stable_secret *secret = ctl->data;
6447 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6450 lctl.maxlen = IPV6_MAX_STRLEN;
6453 if (!rtnl_trylock())
6454 return restart_syscall();
6456 if (!write && !secret->initialized) {
6461 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6462 if (err >= sizeof(str)) {
6467 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6471 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6476 secret->initialized = true;
6477 secret->secret = addr;
6479 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6480 struct net_device *dev;
6482 for_each_netdev(net, dev) {
6483 struct inet6_dev *idev = __in6_dev_get(dev);
6486 idev->cnf.addr_gen_mode =
6487 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6491 struct inet6_dev *idev = ctl->extra1;
6493 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6503 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6504 int write, void *buffer,
6508 int *valp = ctl->data;
6511 struct ctl_table lctl;
6514 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6515 * we should not modify it until we get the rtnl lock.
6520 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6523 ret = addrconf_fixup_linkdown(ctl, valp, val);
6530 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6534 rt->dst.flags |= DST_NOPOLICY;
6536 rt->dst.flags &= ~DST_NOPOLICY;
6541 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6543 struct inet6_ifaddr *ifa;
6545 read_lock_bh(&idev->lock);
6546 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6547 spin_lock(&ifa->lock);
6549 /* host routes only use builtin fib6_nh */
6550 struct fib6_nh *nh = ifa->rt->fib6_nh;
6554 ifa->rt->dst_nopolicy = val ? true : false;
6555 if (nh->rt6i_pcpu) {
6556 for_each_possible_cpu(cpu) {
6557 struct rt6_info **rtp;
6559 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6560 addrconf_set_nopolicy(*rtp, val);
6565 spin_unlock(&ifa->lock);
6567 read_unlock_bh(&idev->lock);
6571 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6573 struct inet6_dev *idev;
6576 if (!rtnl_trylock())
6577 return restart_syscall();
6581 net = (struct net *)ctl->extra2;
6582 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6587 if (valp == &net->ipv6.devconf_all->disable_policy) {
6588 struct net_device *dev;
6590 for_each_netdev(net, dev) {
6591 idev = __in6_dev_get(dev);
6593 addrconf_disable_policy_idev(idev, val);
6596 idev = (struct inet6_dev *)ctl->extra1;
6597 addrconf_disable_policy_idev(idev, val);
6604 static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6605 void *buffer, size_t *lenp, loff_t *ppos)
6607 int *valp = ctl->data;
6610 struct ctl_table lctl;
6615 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6617 if (write && (*valp != val))
6618 ret = addrconf_disable_policy(ctl, valp, val);
6626 static int minus_one = -1;
6627 static const int two_five_five = 255;
6628 static u32 ioam6_if_id_max = U16_MAX;
6630 static const struct ctl_table addrconf_sysctl[] = {
6632 .procname = "forwarding",
6633 .data = &ipv6_devconf.forwarding,
6634 .maxlen = sizeof(int),
6636 .proc_handler = addrconf_sysctl_forward,
6639 .procname = "hop_limit",
6640 .data = &ipv6_devconf.hop_limit,
6641 .maxlen = sizeof(int),
6643 .proc_handler = proc_dointvec_minmax,
6644 .extra1 = (void *)SYSCTL_ONE,
6645 .extra2 = (void *)&two_five_five,
6649 .data = &ipv6_devconf.mtu6,
6650 .maxlen = sizeof(int),
6652 .proc_handler = addrconf_sysctl_mtu,
6655 .procname = "accept_ra",
6656 .data = &ipv6_devconf.accept_ra,
6657 .maxlen = sizeof(int),
6659 .proc_handler = proc_dointvec,
6662 .procname = "accept_redirects",
6663 .data = &ipv6_devconf.accept_redirects,
6664 .maxlen = sizeof(int),
6666 .proc_handler = proc_dointvec,
6669 .procname = "autoconf",
6670 .data = &ipv6_devconf.autoconf,
6671 .maxlen = sizeof(int),
6673 .proc_handler = proc_dointvec,
6676 .procname = "dad_transmits",
6677 .data = &ipv6_devconf.dad_transmits,
6678 .maxlen = sizeof(int),
6680 .proc_handler = proc_dointvec,
6683 .procname = "router_solicitations",
6684 .data = &ipv6_devconf.rtr_solicits,
6685 .maxlen = sizeof(int),
6687 .proc_handler = proc_dointvec_minmax,
6688 .extra1 = &minus_one,
6691 .procname = "router_solicitation_interval",
6692 .data = &ipv6_devconf.rtr_solicit_interval,
6693 .maxlen = sizeof(int),
6695 .proc_handler = proc_dointvec_jiffies,
6698 .procname = "router_solicitation_max_interval",
6699 .data = &ipv6_devconf.rtr_solicit_max_interval,
6700 .maxlen = sizeof(int),
6702 .proc_handler = proc_dointvec_jiffies,
6705 .procname = "router_solicitation_delay",
6706 .data = &ipv6_devconf.rtr_solicit_delay,
6707 .maxlen = sizeof(int),
6709 .proc_handler = proc_dointvec_jiffies,
6712 .procname = "force_mld_version",
6713 .data = &ipv6_devconf.force_mld_version,
6714 .maxlen = sizeof(int),
6716 .proc_handler = proc_dointvec,
6719 .procname = "mldv1_unsolicited_report_interval",
6721 &ipv6_devconf.mldv1_unsolicited_report_interval,
6722 .maxlen = sizeof(int),
6724 .proc_handler = proc_dointvec_ms_jiffies,
6727 .procname = "mldv2_unsolicited_report_interval",
6729 &ipv6_devconf.mldv2_unsolicited_report_interval,
6730 .maxlen = sizeof(int),
6732 .proc_handler = proc_dointvec_ms_jiffies,
6735 .procname = "use_tempaddr",
6736 .data = &ipv6_devconf.use_tempaddr,
6737 .maxlen = sizeof(int),
6739 .proc_handler = proc_dointvec,
6742 .procname = "temp_valid_lft",
6743 .data = &ipv6_devconf.temp_valid_lft,
6744 .maxlen = sizeof(int),
6746 .proc_handler = proc_dointvec,
6749 .procname = "temp_prefered_lft",
6750 .data = &ipv6_devconf.temp_prefered_lft,
6751 .maxlen = sizeof(int),
6753 .proc_handler = proc_dointvec,
6756 .procname = "regen_max_retry",
6757 .data = &ipv6_devconf.regen_max_retry,
6758 .maxlen = sizeof(int),
6760 .proc_handler = proc_dointvec,
6763 .procname = "max_desync_factor",
6764 .data = &ipv6_devconf.max_desync_factor,
6765 .maxlen = sizeof(int),
6767 .proc_handler = proc_dointvec,
6770 .procname = "max_addresses",
6771 .data = &ipv6_devconf.max_addresses,
6772 .maxlen = sizeof(int),
6774 .proc_handler = proc_dointvec,
6777 .procname = "accept_ra_defrtr",
6778 .data = &ipv6_devconf.accept_ra_defrtr,
6779 .maxlen = sizeof(int),
6781 .proc_handler = proc_dointvec,
6784 .procname = "ra_defrtr_metric",
6785 .data = &ipv6_devconf.ra_defrtr_metric,
6786 .maxlen = sizeof(u32),
6788 .proc_handler = proc_douintvec_minmax,
6789 .extra1 = (void *)SYSCTL_ONE,
6792 .procname = "accept_ra_min_hop_limit",
6793 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6794 .maxlen = sizeof(int),
6796 .proc_handler = proc_dointvec,
6799 .procname = "accept_ra_pinfo",
6800 .data = &ipv6_devconf.accept_ra_pinfo,
6801 .maxlen = sizeof(int),
6803 .proc_handler = proc_dointvec,
6805 #ifdef CONFIG_IPV6_ROUTER_PREF
6807 .procname = "accept_ra_rtr_pref",
6808 .data = &ipv6_devconf.accept_ra_rtr_pref,
6809 .maxlen = sizeof(int),
6811 .proc_handler = proc_dointvec,
6814 .procname = "router_probe_interval",
6815 .data = &ipv6_devconf.rtr_probe_interval,
6816 .maxlen = sizeof(int),
6818 .proc_handler = proc_dointvec_jiffies,
6820 #ifdef CONFIG_IPV6_ROUTE_INFO
6822 .procname = "accept_ra_rt_info_min_plen",
6823 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6824 .maxlen = sizeof(int),
6826 .proc_handler = proc_dointvec,
6829 .procname = "accept_ra_rt_info_max_plen",
6830 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6831 .maxlen = sizeof(int),
6833 .proc_handler = proc_dointvec,
6838 .procname = "proxy_ndp",
6839 .data = &ipv6_devconf.proxy_ndp,
6840 .maxlen = sizeof(int),
6842 .proc_handler = addrconf_sysctl_proxy_ndp,
6845 .procname = "accept_source_route",
6846 .data = &ipv6_devconf.accept_source_route,
6847 .maxlen = sizeof(int),
6849 .proc_handler = proc_dointvec,
6851 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6853 .procname = "optimistic_dad",
6854 .data = &ipv6_devconf.optimistic_dad,
6855 .maxlen = sizeof(int),
6857 .proc_handler = proc_dointvec,
6860 .procname = "use_optimistic",
6861 .data = &ipv6_devconf.use_optimistic,
6862 .maxlen = sizeof(int),
6864 .proc_handler = proc_dointvec,
6867 #ifdef CONFIG_IPV6_MROUTE
6869 .procname = "mc_forwarding",
6870 .data = &ipv6_devconf.mc_forwarding,
6871 .maxlen = sizeof(int),
6873 .proc_handler = proc_dointvec,
6877 .procname = "disable_ipv6",
6878 .data = &ipv6_devconf.disable_ipv6,
6879 .maxlen = sizeof(int),
6881 .proc_handler = addrconf_sysctl_disable,
6884 .procname = "accept_dad",
6885 .data = &ipv6_devconf.accept_dad,
6886 .maxlen = sizeof(int),
6888 .proc_handler = proc_dointvec,
6891 .procname = "force_tllao",
6892 .data = &ipv6_devconf.force_tllao,
6893 .maxlen = sizeof(int),
6895 .proc_handler = proc_dointvec
6898 .procname = "ndisc_notify",
6899 .data = &ipv6_devconf.ndisc_notify,
6900 .maxlen = sizeof(int),
6902 .proc_handler = proc_dointvec
6905 .procname = "suppress_frag_ndisc",
6906 .data = &ipv6_devconf.suppress_frag_ndisc,
6907 .maxlen = sizeof(int),
6909 .proc_handler = proc_dointvec
6912 .procname = "accept_ra_from_local",
6913 .data = &ipv6_devconf.accept_ra_from_local,
6914 .maxlen = sizeof(int),
6916 .proc_handler = proc_dointvec,
6919 .procname = "accept_ra_mtu",
6920 .data = &ipv6_devconf.accept_ra_mtu,
6921 .maxlen = sizeof(int),
6923 .proc_handler = proc_dointvec,
6926 .procname = "stable_secret",
6927 .data = &ipv6_devconf.stable_secret,
6928 .maxlen = IPV6_MAX_STRLEN,
6930 .proc_handler = addrconf_sysctl_stable_secret,
6933 .procname = "use_oif_addrs_only",
6934 .data = &ipv6_devconf.use_oif_addrs_only,
6935 .maxlen = sizeof(int),
6937 .proc_handler = proc_dointvec,
6940 .procname = "ignore_routes_with_linkdown",
6941 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6942 .maxlen = sizeof(int),
6944 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6947 .procname = "drop_unicast_in_l2_multicast",
6948 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6949 .maxlen = sizeof(int),
6951 .proc_handler = proc_dointvec,
6954 .procname = "drop_unsolicited_na",
6955 .data = &ipv6_devconf.drop_unsolicited_na,
6956 .maxlen = sizeof(int),
6958 .proc_handler = proc_dointvec,
6961 .procname = "keep_addr_on_down",
6962 .data = &ipv6_devconf.keep_addr_on_down,
6963 .maxlen = sizeof(int),
6965 .proc_handler = proc_dointvec,
6969 .procname = "seg6_enabled",
6970 .data = &ipv6_devconf.seg6_enabled,
6971 .maxlen = sizeof(int),
6973 .proc_handler = proc_dointvec,
6975 #ifdef CONFIG_IPV6_SEG6_HMAC
6977 .procname = "seg6_require_hmac",
6978 .data = &ipv6_devconf.seg6_require_hmac,
6979 .maxlen = sizeof(int),
6981 .proc_handler = proc_dointvec,
6985 .procname = "enhanced_dad",
6986 .data = &ipv6_devconf.enhanced_dad,
6987 .maxlen = sizeof(int),
6989 .proc_handler = proc_dointvec,
6992 .procname = "addr_gen_mode",
6993 .data = &ipv6_devconf.addr_gen_mode,
6994 .maxlen = sizeof(int),
6996 .proc_handler = addrconf_sysctl_addr_gen_mode,
6999 .procname = "disable_policy",
7000 .data = &ipv6_devconf.disable_policy,
7001 .maxlen = sizeof(int),
7003 .proc_handler = addrconf_sysctl_disable_policy,
7006 .procname = "ndisc_tclass",
7007 .data = &ipv6_devconf.ndisc_tclass,
7008 .maxlen = sizeof(int),
7010 .proc_handler = proc_dointvec_minmax,
7011 .extra1 = (void *)SYSCTL_ZERO,
7012 .extra2 = (void *)&two_five_five,
7015 .procname = "rpl_seg_enabled",
7016 .data = &ipv6_devconf.rpl_seg_enabled,
7017 .maxlen = sizeof(int),
7019 .proc_handler = proc_dointvec,
7022 .procname = "ioam6_enabled",
7023 .data = &ipv6_devconf.ioam6_enabled,
7024 .maxlen = sizeof(u8),
7026 .proc_handler = proc_dou8vec_minmax,
7027 .extra1 = (void *)SYSCTL_ZERO,
7028 .extra2 = (void *)SYSCTL_ONE,
7031 .procname = "ioam6_id",
7032 .data = &ipv6_devconf.ioam6_id,
7033 .maxlen = sizeof(u32),
7035 .proc_handler = proc_douintvec_minmax,
7036 .extra1 = (void *)SYSCTL_ZERO,
7037 .extra2 = (void *)&ioam6_if_id_max,
7040 .procname = "ioam6_id_wide",
7041 .data = &ipv6_devconf.ioam6_id_wide,
7042 .maxlen = sizeof(u32),
7044 .proc_handler = proc_douintvec,
7047 .procname = "ndisc_evict_nocarrier",
7048 .data = &ipv6_devconf.ndisc_evict_nocarrier,
7049 .maxlen = sizeof(u8),
7051 .proc_handler = proc_dou8vec_minmax,
7052 .extra1 = (void *)SYSCTL_ZERO,
7053 .extra2 = (void *)SYSCTL_ONE,
7056 .procname = "accept_untracked_na",
7057 .data = &ipv6_devconf.accept_untracked_na,
7058 .maxlen = sizeof(int),
7060 .proc_handler = proc_dointvec_minmax,
7061 .extra1 = SYSCTL_ZERO,
7062 .extra2 = SYSCTL_TWO,
7069 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7070 struct inet6_dev *idev, struct ipv6_devconf *p)
7073 struct ctl_table *table;
7074 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
7076 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7080 for (i = 0; table[i].data; i++) {
7081 table[i].data += (char *)p - (char *)&ipv6_devconf;
7082 /* If one of these is already set, then it is not safe to
7083 * overwrite either of them: this makes proc_dointvec_minmax
7086 if (!table[i].extra1 && !table[i].extra2) {
7087 table[i].extra1 = idev; /* embedded; no ref */
7088 table[i].extra2 = net;
7092 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
7094 p->sysctl_header = register_net_sysctl(net, path, table);
7095 if (!p->sysctl_header)
7098 if (!strcmp(dev_name, "all"))
7099 ifindex = NETCONFA_IFINDEX_ALL;
7100 else if (!strcmp(dev_name, "default"))
7101 ifindex = NETCONFA_IFINDEX_DEFAULT;
7103 ifindex = idev->dev->ifindex;
7104 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
7114 static void __addrconf_sysctl_unregister(struct net *net,
7115 struct ipv6_devconf *p, int ifindex)
7117 struct ctl_table *table;
7119 if (!p->sysctl_header)
7122 table = p->sysctl_header->ctl_table_arg;
7123 unregister_net_sysctl_table(p->sysctl_header);
7124 p->sysctl_header = NULL;
7127 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
7130 static int addrconf_sysctl_register(struct inet6_dev *idev)
7134 if (!sysctl_dev_name_is_allowed(idev->dev->name))
7137 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7138 &ndisc_ifinfo_sysctl_change);
7141 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7144 neigh_sysctl_unregister(idev->nd_parms);
7149 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7151 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
7152 idev->dev->ifindex);
7153 neigh_sysctl_unregister(idev->nd_parms);
7159 static int __net_init addrconf_init_net(struct net *net)
7162 struct ipv6_devconf *all, *dflt;
7164 spin_lock_init(&net->ipv6.addrconf_hash_lock);
7165 INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
7166 net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
7167 sizeof(struct hlist_head),
7169 if (!net->ipv6.inet6_addr_lst)
7170 goto err_alloc_addr;
7172 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
7176 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
7178 goto err_alloc_dflt;
7180 if (!net_eq(net, &init_net)) {
7181 switch (net_inherit_devconf()) {
7182 case 1: /* copy from init_net */
7183 memcpy(all, init_net.ipv6.devconf_all,
7184 sizeof(ipv6_devconf));
7185 memcpy(dflt, init_net.ipv6.devconf_dflt,
7186 sizeof(ipv6_devconf_dflt));
7188 case 3: /* copy from the current netns */
7189 memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
7190 sizeof(ipv6_devconf));
7192 current->nsproxy->net_ns->ipv6.devconf_dflt,
7193 sizeof(ipv6_devconf_dflt));
7197 /* use compiled values */
7202 /* these will be inherited by all namespaces */
7203 dflt->autoconf = ipv6_defaults.autoconf;
7204 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7206 dflt->stable_secret.initialized = false;
7207 all->stable_secret.initialized = false;
7209 net->ipv6.devconf_all = all;
7210 net->ipv6.devconf_dflt = dflt;
7212 #ifdef CONFIG_SYSCTL
7213 err = __addrconf_sysctl_register(net, "all", NULL, all);
7217 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7223 #ifdef CONFIG_SYSCTL
7225 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7228 net->ipv6.devconf_dflt = NULL;
7232 net->ipv6.devconf_all = NULL;
7234 kfree(net->ipv6.inet6_addr_lst);
7239 static void __net_exit addrconf_exit_net(struct net *net)
7243 #ifdef CONFIG_SYSCTL
7244 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7245 NETCONFA_IFINDEX_DEFAULT);
7246 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7247 NETCONFA_IFINDEX_ALL);
7249 kfree(net->ipv6.devconf_dflt);
7250 net->ipv6.devconf_dflt = NULL;
7251 kfree(net->ipv6.devconf_all);
7252 net->ipv6.devconf_all = NULL;
7254 cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
7256 * Check hash table, then free it.
7258 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7259 WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7261 kfree(net->ipv6.inet6_addr_lst);
7262 net->ipv6.inet6_addr_lst = NULL;
7265 static struct pernet_operations addrconf_ops = {
7266 .init = addrconf_init_net,
7267 .exit = addrconf_exit_net,
7270 static struct rtnl_af_ops inet6_ops __read_mostly = {
7272 .fill_link_af = inet6_fill_link_af,
7273 .get_link_af_size = inet6_get_link_af_size,
7274 .validate_link_af = inet6_validate_link_af,
7275 .set_link_af = inet6_set_link_af,
7279 * Init / cleanup code
7282 int __init addrconf_init(void)
7284 struct inet6_dev *idev;
7287 err = ipv6_addr_label_init();
7289 pr_crit("%s: cannot initialize default policy table: %d\n",
7294 err = register_pernet_subsys(&addrconf_ops);
7298 addrconf_wq = create_workqueue("ipv6_addrconf");
7305 idev = ipv6_add_dev(blackhole_netdev);
7308 err = PTR_ERR(idev);
7312 ip6_route_init_special_entries();
7314 register_netdevice_notifier(&ipv6_dev_notf);
7316 addrconf_verify(&init_net);
7318 rtnl_af_register(&inet6_ops);
7320 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7321 NULL, inet6_dump_ifinfo, 0);
7325 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7326 inet6_rtm_newaddr, NULL, 0);
7329 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7330 inet6_rtm_deladdr, NULL, 0);
7333 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7334 inet6_rtm_getaddr, inet6_dump_ifaddr,
7335 RTNL_FLAG_DOIT_UNLOCKED);
7338 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7339 NULL, inet6_dump_ifmcaddr, 0);
7342 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7343 NULL, inet6_dump_ifacaddr, 0);
7346 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7347 inet6_netconf_get_devconf,
7348 inet6_netconf_dump_devconf,
7349 RTNL_FLAG_DOIT_UNLOCKED);
7352 err = ipv6_addr_label_rtnl_register();
7358 rtnl_unregister_all(PF_INET6);
7359 rtnl_af_unregister(&inet6_ops);
7360 unregister_netdevice_notifier(&ipv6_dev_notf);
7362 destroy_workqueue(addrconf_wq);
7364 unregister_pernet_subsys(&addrconf_ops);
7366 ipv6_addr_label_cleanup();
7371 void addrconf_cleanup(void)
7373 struct net_device *dev;
7375 unregister_netdevice_notifier(&ipv6_dev_notf);
7376 unregister_pernet_subsys(&addrconf_ops);
7377 ipv6_addr_label_cleanup();
7379 rtnl_af_unregister(&inet6_ops);
7383 /* clean dev list */
7384 for_each_netdev(&init_net, dev) {
7385 if (__in6_dev_get(dev) == NULL)
7387 addrconf_ifdown(dev, true);
7389 addrconf_ifdown(init_net.loopback_dev, true);
7393 destroy_workqueue(addrconf_wq);