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_min_lft = 0,
206 .accept_ra_pinfo = 1,
207 #ifdef CONFIG_IPV6_ROUTER_PREF
208 .accept_ra_rtr_pref = 1,
209 .rtr_probe_interval = 60 * HZ,
210 #ifdef CONFIG_IPV6_ROUTE_INFO
211 .accept_ra_rt_info_min_plen = 0,
212 .accept_ra_rt_info_max_plen = 0,
216 .accept_source_route = 0, /* we do not accept RH0 by default. */
219 .suppress_frag_ndisc = 1,
222 .initialized = false,
224 .use_oif_addrs_only = 0,
225 .ignore_routes_with_linkdown = 0,
226 .keep_addr_on_down = 0,
228 #ifdef CONFIG_IPV6_SEG6_HMAC
229 .seg6_require_hmac = 0,
232 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
234 .rpl_seg_enabled = 0,
236 .ioam6_id = IOAM6_DEFAULT_IF_ID,
237 .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
238 .ndisc_evict_nocarrier = 1,
241 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
243 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
244 .mtu6 = IPV6_MIN_MTU,
246 .accept_redirects = 1,
248 .force_mld_version = 0,
249 .mldv1_unsolicited_report_interval = 10 * HZ,
250 .mldv2_unsolicited_report_interval = HZ,
252 .rtr_solicits = MAX_RTR_SOLICITATIONS,
253 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
254 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
255 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
257 .temp_valid_lft = TEMP_VALID_LIFETIME,
258 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
259 .regen_max_retry = REGEN_MAX_RETRY,
260 .max_desync_factor = MAX_DESYNC_FACTOR,
261 .max_addresses = IPV6_MAX_ADDRESSES,
262 .accept_ra_defrtr = 1,
263 .ra_defrtr_metric = IP6_RT_PRIO_USER,
264 .accept_ra_from_local = 0,
265 .accept_ra_min_hop_limit= 1,
266 .accept_ra_min_lft = 0,
267 .accept_ra_pinfo = 1,
268 #ifdef CONFIG_IPV6_ROUTER_PREF
269 .accept_ra_rtr_pref = 1,
270 .rtr_probe_interval = 60 * HZ,
271 #ifdef CONFIG_IPV6_ROUTE_INFO
272 .accept_ra_rt_info_min_plen = 0,
273 .accept_ra_rt_info_max_plen = 0,
277 .accept_source_route = 0, /* we do not accept RH0 by default. */
280 .suppress_frag_ndisc = 1,
283 .initialized = false,
285 .use_oif_addrs_only = 0,
286 .ignore_routes_with_linkdown = 0,
287 .keep_addr_on_down = 0,
289 #ifdef CONFIG_IPV6_SEG6_HMAC
290 .seg6_require_hmac = 0,
293 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
295 .rpl_seg_enabled = 0,
297 .ioam6_id = IOAM6_DEFAULT_IF_ID,
298 .ioam6_id_wide = IOAM6_DEFAULT_IF_ID_WIDE,
299 .ndisc_evict_nocarrier = 1,
302 /* Check if link is ready: is it up and is a valid qdisc available */
303 static inline bool addrconf_link_ready(const struct net_device *dev)
305 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
308 static void addrconf_del_rs_timer(struct inet6_dev *idev)
310 if (del_timer(&idev->rs_timer))
314 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
316 if (cancel_delayed_work(&ifp->dad_work))
320 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
323 if (!mod_timer(&idev->rs_timer, jiffies + when))
327 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
331 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
335 static int snmp6_alloc_dev(struct inet6_dev *idev)
339 idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT);
340 if (!idev->stats.ipv6)
343 for_each_possible_cpu(i) {
344 struct ipstats_mib *addrconf_stats;
345 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
346 u64_stats_init(&addrconf_stats->syncp);
350 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
352 if (!idev->stats.icmpv6dev)
354 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
356 if (!idev->stats.icmpv6msgdev)
362 kfree(idev->stats.icmpv6dev);
364 free_percpu(idev->stats.ipv6);
369 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
371 struct inet6_dev *ndev;
376 if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev)
377 return ERR_PTR(-EINVAL);
379 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL_ACCOUNT);
383 rwlock_init(&ndev->lock);
385 INIT_LIST_HEAD(&ndev->addr_list);
386 timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
387 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
389 if (ndev->cnf.stable_secret.initialized)
390 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
392 ndev->cnf.mtu6 = dev->mtu;
394 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
395 if (!ndev->nd_parms) {
399 if (ndev->cnf.forwarding)
400 dev_disable_lro(dev);
401 /* We refer to the device */
402 netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL);
404 if (snmp6_alloc_dev(ndev) < 0) {
405 netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
407 neigh_parms_release(&nd_tbl, ndev->nd_parms);
408 netdev_put(dev, &ndev->dev_tracker);
413 if (dev != blackhole_netdev) {
414 if (snmp6_register_dev(ndev) < 0) {
415 netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
416 __func__, dev->name);
420 /* One reference from device. */
421 refcount_set(&ndev->refcnt, 1);
423 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
424 ndev->cnf.accept_dad = -1;
426 #if IS_ENABLED(CONFIG_IPV6_SIT)
427 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
428 pr_info("%s: Disabled Multicast RS\n", dev->name);
429 ndev->cnf.rtr_solicits = 0;
433 INIT_LIST_HEAD(&ndev->tempaddr_list);
434 ndev->desync_factor = U32_MAX;
435 if ((dev->flags&IFF_LOOPBACK) ||
436 dev->type == ARPHRD_TUNNEL ||
437 dev->type == ARPHRD_TUNNEL6 ||
438 dev->type == ARPHRD_SIT ||
439 dev->type == ARPHRD_NONE) {
440 ndev->cnf.use_tempaddr = -1;
443 ndev->token = in6addr_any;
445 if (netif_running(dev) && addrconf_link_ready(dev))
446 ndev->if_flags |= IF_READY;
448 ipv6_mc_init_dev(ndev);
449 ndev->tstamp = jiffies;
450 if (dev != blackhole_netdev) {
451 err = addrconf_sysctl_register(ndev);
453 ipv6_mc_destroy_dev(ndev);
454 snmp6_unregister_dev(ndev);
458 /* protected by rtnl_lock */
459 rcu_assign_pointer(dev->ip6_ptr, ndev);
461 if (dev != blackhole_netdev) {
462 /* Join interface-local all-node multicast group */
463 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
465 /* Join all-node multicast group */
466 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
468 /* Join all-router multicast group if forwarding is set */
469 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
470 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
475 neigh_parms_release(&nd_tbl, ndev->nd_parms);
477 in6_dev_finish_destroy(ndev);
481 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
483 struct inet6_dev *idev;
487 idev = __in6_dev_get(dev);
489 idev = ipv6_add_dev(dev);
494 if (dev->flags&IFF_UP)
499 static int inet6_netconf_msgsize_devconf(int type)
501 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
502 + nla_total_size(4); /* NETCONFA_IFINDEX */
505 if (type == NETCONFA_ALL)
508 if (all || type == NETCONFA_FORWARDING)
509 size += nla_total_size(4);
510 #ifdef CONFIG_IPV6_MROUTE
511 if (all || type == NETCONFA_MC_FORWARDING)
512 size += nla_total_size(4);
514 if (all || type == NETCONFA_PROXY_NEIGH)
515 size += nla_total_size(4);
517 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
518 size += nla_total_size(4);
523 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
524 struct ipv6_devconf *devconf, u32 portid,
525 u32 seq, int event, unsigned int flags,
528 struct nlmsghdr *nlh;
529 struct netconfmsg *ncm;
532 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
537 if (type == NETCONFA_ALL)
540 ncm = nlmsg_data(nlh);
541 ncm->ncm_family = AF_INET6;
543 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
544 goto nla_put_failure;
549 if ((all || type == NETCONFA_FORWARDING) &&
550 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
551 goto nla_put_failure;
552 #ifdef CONFIG_IPV6_MROUTE
553 if ((all || type == NETCONFA_MC_FORWARDING) &&
554 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
555 atomic_read(&devconf->mc_forwarding)) < 0)
556 goto nla_put_failure;
558 if ((all || type == NETCONFA_PROXY_NEIGH) &&
559 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
560 goto nla_put_failure;
562 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
563 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
564 devconf->ignore_routes_with_linkdown) < 0)
565 goto nla_put_failure;
572 nlmsg_cancel(skb, nlh);
576 void inet6_netconf_notify_devconf(struct net *net, int event, int type,
577 int ifindex, struct ipv6_devconf *devconf)
582 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
586 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
589 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
590 WARN_ON(err == -EMSGSIZE);
594 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
597 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
600 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
601 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
602 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
603 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
604 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
607 static int inet6_netconf_valid_get_req(struct sk_buff *skb,
608 const struct nlmsghdr *nlh,
610 struct netlink_ext_ack *extack)
614 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
615 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
619 if (!netlink_strict_get_check(skb))
620 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
622 devconf_ipv6_policy, extack);
624 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
626 devconf_ipv6_policy, extack);
630 for (i = 0; i <= NETCONFA_MAX; i++) {
635 case NETCONFA_IFINDEX:
638 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
646 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
647 struct nlmsghdr *nlh,
648 struct netlink_ext_ack *extack)
650 struct net *net = sock_net(in_skb->sk);
651 struct nlattr *tb[NETCONFA_MAX+1];
652 struct inet6_dev *in6_dev = NULL;
653 struct net_device *dev = NULL;
655 struct ipv6_devconf *devconf;
659 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
663 if (!tb[NETCONFA_IFINDEX])
667 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
669 case NETCONFA_IFINDEX_ALL:
670 devconf = net->ipv6.devconf_all;
672 case NETCONFA_IFINDEX_DEFAULT:
673 devconf = net->ipv6.devconf_dflt;
676 dev = dev_get_by_index(net, ifindex);
679 in6_dev = in6_dev_get(dev);
682 devconf = &in6_dev->cnf;
687 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
691 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
692 NETLINK_CB(in_skb).portid,
693 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
696 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
697 WARN_ON(err == -EMSGSIZE);
701 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
704 in6_dev_put(in6_dev);
709 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
710 struct netlink_callback *cb)
712 const struct nlmsghdr *nlh = cb->nlh;
713 struct net *net = sock_net(skb->sk);
716 struct net_device *dev;
717 struct inet6_dev *idev;
718 struct hlist_head *head;
720 if (cb->strict_check) {
721 struct netlink_ext_ack *extack = cb->extack;
722 struct netconfmsg *ncm;
724 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
725 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
729 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
730 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
736 s_idx = idx = cb->args[1];
738 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
740 head = &net->dev_index_head[h];
742 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
744 hlist_for_each_entry_rcu(dev, head, index_hlist) {
747 idev = __in6_dev_get(dev);
751 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
753 NETLINK_CB(cb->skb).portid,
761 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
767 if (h == NETDEV_HASHENTRIES) {
768 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
769 net->ipv6.devconf_all,
770 NETLINK_CB(cb->skb).portid,
772 RTM_NEWNETCONF, NLM_F_MULTI,
778 if (h == NETDEV_HASHENTRIES + 1) {
779 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
780 net->ipv6.devconf_dflt,
781 NETLINK_CB(cb->skb).portid,
783 RTM_NEWNETCONF, NLM_F_MULTI,
797 static void dev_forward_change(struct inet6_dev *idev)
799 struct net_device *dev;
800 struct inet6_ifaddr *ifa;
801 LIST_HEAD(tmp_addr_list);
806 if (idev->cnf.forwarding)
807 dev_disable_lro(dev);
808 if (dev->flags & IFF_MULTICAST) {
809 if (idev->cnf.forwarding) {
810 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
811 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
812 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
814 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
815 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
816 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
820 read_lock_bh(&idev->lock);
821 list_for_each_entry(ifa, &idev->addr_list, if_list) {
822 if (ifa->flags&IFA_F_TENTATIVE)
824 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
826 read_unlock_bh(&idev->lock);
828 while (!list_empty(&tmp_addr_list)) {
829 ifa = list_first_entry(&tmp_addr_list,
830 struct inet6_ifaddr, if_list_aux);
831 list_del(&ifa->if_list_aux);
832 if (idev->cnf.forwarding)
833 addrconf_join_anycast(ifa);
835 addrconf_leave_anycast(ifa);
838 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
840 dev->ifindex, &idev->cnf);
844 static void addrconf_forward_change(struct net *net, __s32 newf)
846 struct net_device *dev;
847 struct inet6_dev *idev;
849 for_each_netdev(net, dev) {
850 idev = __in6_dev_get(dev);
852 int changed = (!idev->cnf.forwarding) ^ (!newf);
853 idev->cnf.forwarding = newf;
855 dev_forward_change(idev);
860 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
866 return restart_syscall();
868 net = (struct net *)table->extra2;
872 if (p == &net->ipv6.devconf_dflt->forwarding) {
873 if ((!newf) ^ (!old))
874 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
876 NETCONFA_IFINDEX_DEFAULT,
877 net->ipv6.devconf_dflt);
882 if (p == &net->ipv6.devconf_all->forwarding) {
883 int old_dflt = net->ipv6.devconf_dflt->forwarding;
885 net->ipv6.devconf_dflt->forwarding = newf;
886 if ((!newf) ^ (!old_dflt))
887 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
889 NETCONFA_IFINDEX_DEFAULT,
890 net->ipv6.devconf_dflt);
892 addrconf_forward_change(net, newf);
893 if ((!newf) ^ (!old))
894 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
896 NETCONFA_IFINDEX_ALL,
897 net->ipv6.devconf_all);
898 } else if ((!newf) ^ (!old))
899 dev_forward_change((struct inet6_dev *)table->extra1);
903 rt6_purge_dflt_routers(net);
907 static void addrconf_linkdown_change(struct net *net, __s32 newf)
909 struct net_device *dev;
910 struct inet6_dev *idev;
912 for_each_netdev(net, dev) {
913 idev = __in6_dev_get(dev);
915 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
917 idev->cnf.ignore_routes_with_linkdown = newf;
919 inet6_netconf_notify_devconf(dev_net(dev),
921 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
928 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
934 return restart_syscall();
936 net = (struct net *)table->extra2;
940 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
941 if ((!newf) ^ (!old))
942 inet6_netconf_notify_devconf(net,
944 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
945 NETCONFA_IFINDEX_DEFAULT,
946 net->ipv6.devconf_dflt);
951 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
952 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
953 addrconf_linkdown_change(net, newf);
954 if ((!newf) ^ (!old))
955 inet6_netconf_notify_devconf(net,
957 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
958 NETCONFA_IFINDEX_ALL,
959 net->ipv6.devconf_all);
968 /* Nobody refers to this ifaddr, destroy it */
969 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
971 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
973 #ifdef NET_REFCNT_DEBUG
974 pr_debug("%s\n", __func__);
977 in6_dev_put(ifp->idev);
979 if (cancel_delayed_work(&ifp->dad_work))
980 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
983 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
984 pr_warn("Freeing alive inet6 address %p\n", ifp);
992 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
995 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
998 * Each device address list is sorted in order of scope -
999 * global before linklocal.
1001 list_for_each(p, &idev->addr_list) {
1002 struct inet6_ifaddr *ifa
1003 = list_entry(p, struct inet6_ifaddr, if_list);
1004 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
1008 list_add_tail_rcu(&ifp->if_list, p);
1011 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
1013 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
1015 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
1018 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1019 struct net_device *dev, unsigned int hash)
1021 struct inet6_ifaddr *ifp;
1023 hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1024 if (ipv6_addr_equal(&ifp->addr, addr)) {
1025 if (!dev || ifp->idev->dev == dev)
1032 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1034 struct net *net = dev_net(dev);
1035 unsigned int hash = inet6_addr_hash(net, &ifa->addr);
1038 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1040 /* Ignore adding duplicate addresses on an interface */
1041 if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) {
1042 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1045 hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]);
1048 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1053 /* On success it returns ifp with increased reference count */
1055 static struct inet6_ifaddr *
1056 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1057 bool can_block, struct netlink_ext_ack *extack)
1059 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1060 int addr_type = ipv6_addr_type(cfg->pfx);
1061 struct net *net = dev_net(idev->dev);
1062 struct inet6_ifaddr *ifa = NULL;
1063 struct fib6_info *f6i = NULL;
1066 if (addr_type == IPV6_ADDR_ANY) {
1067 NL_SET_ERR_MSG_MOD(extack, "Invalid address");
1068 return ERR_PTR(-EADDRNOTAVAIL);
1069 } else if (addr_type & IPV6_ADDR_MULTICAST &&
1070 !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) {
1071 NL_SET_ERR_MSG_MOD(extack, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag");
1072 return ERR_PTR(-EADDRNOTAVAIL);
1073 } else if (!(idev->dev->flags & IFF_LOOPBACK) &&
1074 !netif_is_l3_master(idev->dev) &&
1075 addr_type & IPV6_ADDR_LOOPBACK) {
1076 NL_SET_ERR_MSG_MOD(extack, "Cannot assign loopback address on this device");
1077 return ERR_PTR(-EADDRNOTAVAIL);
1081 NL_SET_ERR_MSG_MOD(extack, "device is going away");
1086 if (idev->cnf.disable_ipv6) {
1087 NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
1092 /* validator notifier needs to be blocking;
1093 * do not call in atomic context
1096 struct in6_validator_info i6vi = {
1097 .i6vi_addr = *cfg->pfx,
1102 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1103 err = notifier_to_errno(err);
1108 ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
1114 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags, extack);
1121 neigh_parms_data_state_setall(idev->nd_parms);
1123 ifa->addr = *cfg->pfx;
1125 ifa->peer_addr = *cfg->peer_pfx;
1127 spin_lock_init(&ifa->lock);
1128 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1129 INIT_HLIST_NODE(&ifa->addr_lst);
1130 ifa->scope = cfg->scope;
1131 ifa->prefix_len = cfg->plen;
1132 ifa->rt_priority = cfg->rt_priority;
1133 ifa->flags = cfg->ifa_flags;
1134 ifa->ifa_proto = cfg->ifa_proto;
1135 /* No need to add the TENTATIVE flag for addresses with NODAD */
1136 if (!(cfg->ifa_flags & IFA_F_NODAD))
1137 ifa->flags |= IFA_F_TENTATIVE;
1138 ifa->valid_lft = cfg->valid_lft;
1139 ifa->prefered_lft = cfg->preferred_lft;
1140 ifa->cstamp = ifa->tstamp = jiffies;
1141 ifa->tokenized = false;
1149 refcount_set(&ifa->refcnt, 1);
1153 err = ipv6_add_addr_hash(idev->dev, ifa);
1159 write_lock_bh(&idev->lock);
1161 /* Add to inet6_dev unicast addr list. */
1162 ipv6_link_dev_addr(idev, ifa);
1164 if (ifa->flags&IFA_F_TEMPORARY) {
1165 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1170 write_unlock_bh(&idev->lock);
1174 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1176 if (unlikely(err < 0)) {
1177 fib6_info_release(f6i);
1181 in6_dev_put(ifa->idev);
1190 enum cleanup_prefix_rt_t {
1191 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1192 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1193 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1197 * Check, whether the prefix for ifp would still need a prefix route
1198 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1201 * 1) we don't purge prefix if address was not permanent.
1202 * prefix is managed by its own lifetime.
1203 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1204 * 3) if there are no addresses, delete prefix.
1205 * 4) if there are still other permanent address(es),
1206 * corresponding prefix is still permanent.
1207 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1208 * don't purge the prefix, assume user space is managing it.
1209 * 6) otherwise, update prefix lifetime to the
1210 * longest valid lifetime among the corresponding
1211 * addresses on the device.
1212 * Note: subsequent RA will update lifetime.
1214 static enum cleanup_prefix_rt_t
1215 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1217 struct inet6_ifaddr *ifa;
1218 struct inet6_dev *idev = ifp->idev;
1219 unsigned long lifetime;
1220 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1224 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1227 if (ifa->prefix_len != ifp->prefix_len ||
1228 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1231 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1232 return CLEANUP_PREFIX_RT_NOP;
1234 action = CLEANUP_PREFIX_RT_EXPIRE;
1236 spin_lock(&ifa->lock);
1238 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1240 * Note: Because this address is
1241 * not permanent, lifetime <
1242 * LONG_MAX / HZ here.
1244 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1245 *expires = ifa->tstamp + lifetime * HZ;
1246 spin_unlock(&ifa->lock);
1253 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1254 bool del_rt, bool del_peer)
1256 struct fib6_info *f6i;
1258 f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1260 ifp->idev->dev, 0, RTF_DEFAULT, true);
1263 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
1265 if (!(f6i->fib6_flags & RTF_EXPIRES))
1266 fib6_set_expires(f6i, expires);
1267 fib6_info_release(f6i);
1273 /* This function wants to get referenced ifp and releases it before return */
1275 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1277 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1278 struct net *net = dev_net(ifp->idev->dev);
1279 unsigned long expires;
1284 spin_lock_bh(&ifp->lock);
1286 ifp->state = INET6_IFADDR_STATE_DEAD;
1287 spin_unlock_bh(&ifp->lock);
1289 if (state == INET6_IFADDR_STATE_DEAD)
1292 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1293 hlist_del_init_rcu(&ifp->addr_lst);
1294 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1296 write_lock_bh(&ifp->idev->lock);
1298 if (ifp->flags&IFA_F_TEMPORARY) {
1299 list_del(&ifp->tmp_list);
1301 in6_ifa_put(ifp->ifpub);
1307 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1308 action = check_cleanup_prefix_route(ifp, &expires);
1310 list_del_rcu(&ifp->if_list);
1313 write_unlock_bh(&ifp->idev->lock);
1315 addrconf_del_dad_work(ifp);
1317 ipv6_ifa_notify(RTM_DELADDR, ifp);
1319 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1321 if (action != CLEANUP_PREFIX_RT_NOP) {
1322 cleanup_prefix_route(ifp, expires,
1323 action == CLEANUP_PREFIX_RT_DEL, false);
1326 /* clean up prefsrc entries */
1327 rt6_remove_prefsrc(ifp);
1332 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
1334 struct inet6_dev *idev = ifp->idev;
1335 unsigned long tmp_tstamp, age;
1336 unsigned long regen_advance;
1337 unsigned long now = jiffies;
1338 s32 cnf_temp_preferred_lft;
1339 struct inet6_ifaddr *ift;
1340 struct ifa6_config cfg;
1341 long max_desync_factor;
1342 struct in6_addr addr;
1345 write_lock_bh(&idev->lock);
1349 if (idev->cnf.use_tempaddr <= 0) {
1350 write_unlock_bh(&idev->lock);
1351 pr_info("%s: use_tempaddr is disabled\n", __func__);
1356 spin_lock_bh(&ifp->lock);
1357 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1358 idev->cnf.use_tempaddr = -1; /*XXX*/
1359 spin_unlock_bh(&ifp->lock);
1360 write_unlock_bh(&idev->lock);
1361 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1368 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1369 ipv6_gen_rnd_iid(&addr);
1371 age = (now - ifp->tstamp) / HZ;
1373 regen_advance = idev->cnf.regen_max_retry *
1374 idev->cnf.dad_transmits *
1375 max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
1377 /* recalculate max_desync_factor each time and update
1378 * idev->desync_factor if it's larger
1380 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1381 max_desync_factor = min_t(__u32,
1382 idev->cnf.max_desync_factor,
1383 cnf_temp_preferred_lft - regen_advance);
1385 if (unlikely(idev->desync_factor > max_desync_factor)) {
1386 if (max_desync_factor > 0) {
1387 get_random_bytes(&idev->desync_factor,
1388 sizeof(idev->desync_factor));
1389 idev->desync_factor %= max_desync_factor;
1391 idev->desync_factor = 0;
1395 memset(&cfg, 0, sizeof(cfg));
1396 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
1397 idev->cnf.temp_valid_lft + age);
1398 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1399 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1401 cfg.plen = ifp->prefix_len;
1402 tmp_tstamp = ifp->tstamp;
1403 spin_unlock_bh(&ifp->lock);
1405 write_unlock_bh(&idev->lock);
1407 /* A temporary address is created only if this calculated Preferred
1408 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1409 * an implementation must not create a temporary address with a zero
1410 * Preferred Lifetime.
1411 * Use age calculation as in addrconf_verify to avoid unnecessary
1412 * temporary addresses being generated.
1414 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1415 if (cfg.preferred_lft <= regen_advance + age) {
1422 cfg.ifa_flags = IFA_F_TEMPORARY;
1423 /* set in addrconf_prefix_rcv() */
1424 if (ifp->flags & IFA_F_OPTIMISTIC)
1425 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
1428 cfg.scope = ipv6_addr_scope(cfg.pfx);
1430 ift = ipv6_add_addr(idev, &cfg, block, NULL);
1434 pr_info("%s: retry temporary address regeneration\n", __func__);
1435 write_lock_bh(&idev->lock);
1439 spin_lock_bh(&ift->lock);
1442 ift->tstamp = tmp_tstamp;
1443 spin_unlock_bh(&ift->lock);
1445 addrconf_dad_start(ift);
1453 * Choose an appropriate source address (RFC3484)
1456 IPV6_SADDR_RULE_INIT = 0,
1457 IPV6_SADDR_RULE_LOCAL,
1458 IPV6_SADDR_RULE_SCOPE,
1459 IPV6_SADDR_RULE_PREFERRED,
1460 #ifdef CONFIG_IPV6_MIP6
1461 IPV6_SADDR_RULE_HOA,
1463 IPV6_SADDR_RULE_OIF,
1464 IPV6_SADDR_RULE_LABEL,
1465 IPV6_SADDR_RULE_PRIVACY,
1466 IPV6_SADDR_RULE_ORCHID,
1467 IPV6_SADDR_RULE_PREFIX,
1468 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1469 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1474 struct ipv6_saddr_score {
1477 struct inet6_ifaddr *ifa;
1478 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1483 struct ipv6_saddr_dst {
1484 const struct in6_addr *addr;
1491 static inline int ipv6_saddr_preferred(int type)
1493 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1498 static bool ipv6_use_optimistic_addr(struct net *net,
1499 struct inet6_dev *idev)
1501 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1504 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1506 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1515 static bool ipv6_allow_optimistic_dad(struct net *net,
1516 struct inet6_dev *idev)
1518 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1521 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1530 static int ipv6_get_saddr_eval(struct net *net,
1531 struct ipv6_saddr_score *score,
1532 struct ipv6_saddr_dst *dst,
1537 if (i <= score->rule) {
1539 case IPV6_SADDR_RULE_SCOPE:
1540 ret = score->scopedist;
1542 case IPV6_SADDR_RULE_PREFIX:
1543 ret = score->matchlen;
1546 ret = !!test_bit(i, score->scorebits);
1552 case IPV6_SADDR_RULE_INIT:
1553 /* Rule 0: remember if hiscore is not ready yet */
1556 case IPV6_SADDR_RULE_LOCAL:
1557 /* Rule 1: Prefer same address */
1558 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1560 case IPV6_SADDR_RULE_SCOPE:
1561 /* Rule 2: Prefer appropriate scope
1566 * ---+--+-+---> scope
1568 * | d is scope of the destination.
1570 * | \ <- smaller scope is better if
1571 * B-15 | \ if scope is enough for destination.
1572 * | ret = B - scope (-1 <= scope >= d <= 15).
1574 * |/ <- greater is better
1575 * -C / if scope is not enough for destination.
1576 * /| ret = scope - C (-1 <= d < scope <= 15).
1578 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1579 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1580 * Assume B = 0 and we get C > 29.
1582 ret = __ipv6_addr_src_scope(score->addr_type);
1583 if (ret >= dst->scope)
1586 ret -= 128; /* 30 is enough */
1587 score->scopedist = ret;
1589 case IPV6_SADDR_RULE_PREFERRED:
1591 /* Rule 3: Avoid deprecated and optimistic addresses */
1592 u8 avoid = IFA_F_DEPRECATED;
1594 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
1595 avoid |= IFA_F_OPTIMISTIC;
1596 ret = ipv6_saddr_preferred(score->addr_type) ||
1597 !(score->ifa->flags & avoid);
1600 #ifdef CONFIG_IPV6_MIP6
1601 case IPV6_SADDR_RULE_HOA:
1603 /* Rule 4: Prefer home address */
1604 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1605 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1609 case IPV6_SADDR_RULE_OIF:
1610 /* Rule 5: Prefer outgoing interface */
1611 ret = (!dst->ifindex ||
1612 dst->ifindex == score->ifa->idev->dev->ifindex);
1614 case IPV6_SADDR_RULE_LABEL:
1615 /* Rule 6: Prefer matching label */
1616 ret = ipv6_addr_label(net,
1617 &score->ifa->addr, score->addr_type,
1618 score->ifa->idev->dev->ifindex) == dst->label;
1620 case IPV6_SADDR_RULE_PRIVACY:
1622 /* Rule 7: Prefer public address
1623 * Note: prefer temporary address if use_tempaddr >= 2
1625 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1626 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1627 score->ifa->idev->cnf.use_tempaddr >= 2;
1628 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1631 case IPV6_SADDR_RULE_ORCHID:
1632 /* Rule 8-: Prefer ORCHID vs ORCHID or
1633 * non-ORCHID vs non-ORCHID
1635 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1636 ipv6_addr_orchid(dst->addr));
1638 case IPV6_SADDR_RULE_PREFIX:
1639 /* Rule 8: Use longest matching prefix */
1640 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1641 if (ret > score->ifa->prefix_len)
1642 ret = score->ifa->prefix_len;
1643 score->matchlen = ret;
1645 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1646 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1647 /* Optimistic addresses still have lower precedence than other
1648 * preferred addresses.
1650 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1658 __set_bit(i, score->scorebits);
1664 static int __ipv6_dev_get_saddr(struct net *net,
1665 struct ipv6_saddr_dst *dst,
1666 struct inet6_dev *idev,
1667 struct ipv6_saddr_score *scores,
1670 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1672 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1676 * - Tentative Address (RFC2462 section 5.4)
1677 * - A tentative address is not considered
1678 * "assigned to an interface" in the traditional
1679 * sense, unless it is also flagged as optimistic.
1680 * - Candidate Source Address (section 4)
1681 * - In any case, anycast addresses, multicast
1682 * addresses, and the unspecified address MUST
1683 * NOT be included in a candidate set.
1685 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1686 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1689 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1691 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1692 score->addr_type & IPV6_ADDR_MULTICAST)) {
1693 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1699 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1701 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1702 int minihiscore, miniscore;
1704 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1705 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1707 if (minihiscore > miniscore) {
1708 if (i == IPV6_SADDR_RULE_SCOPE &&
1709 score->scopedist > 0) {
1712 * each remaining entry
1713 * has too small (not enough)
1714 * scope, because ifa entries
1715 * are sorted by their scope
1721 } else if (minihiscore < miniscore) {
1722 swap(hiscore, score);
1723 hiscore_idx = 1 - hiscore_idx;
1725 /* restore our iterator */
1726 score->ifa = hiscore->ifa;
1736 static int ipv6_get_saddr_master(struct net *net,
1737 const struct net_device *dst_dev,
1738 const struct net_device *master,
1739 struct ipv6_saddr_dst *dst,
1740 struct ipv6_saddr_score *scores,
1743 struct inet6_dev *idev;
1745 idev = __in6_dev_get(dst_dev);
1747 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1748 scores, hiscore_idx);
1750 idev = __in6_dev_get(master);
1752 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1753 scores, hiscore_idx);
1758 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1759 const struct in6_addr *daddr, unsigned int prefs,
1760 struct in6_addr *saddr)
1762 struct ipv6_saddr_score scores[2], *hiscore;
1763 struct ipv6_saddr_dst dst;
1764 struct inet6_dev *idev;
1765 struct net_device *dev;
1767 bool use_oif_addr = false;
1768 int hiscore_idx = 0;
1771 dst_type = __ipv6_addr_type(daddr);
1773 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1774 dst.scope = __ipv6_addr_src_scope(dst_type);
1775 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1778 scores[hiscore_idx].rule = -1;
1779 scores[hiscore_idx].ifa = NULL;
1783 /* Candidate Source Address (section 4)
1784 * - multicast and link-local destination address,
1785 * the set of candidate source address MUST only
1786 * include addresses assigned to interfaces
1787 * belonging to the same link as the outgoing
1789 * (- For site-local destination addresses, the
1790 * set of candidate source addresses MUST only
1791 * include addresses assigned to interfaces
1792 * belonging to the same site as the outgoing
1794 * - "It is RECOMMENDED that the candidate source addresses
1795 * be the set of unicast addresses assigned to the
1796 * interface that will be used to send to the destination
1797 * (the 'outgoing' interface)." (RFC 6724)
1800 idev = __in6_dev_get(dst_dev);
1801 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1802 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1803 (idev && idev->cnf.use_oif_addrs_only)) {
1804 use_oif_addr = true;
1810 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1812 const struct net_device *master;
1815 /* if dst_dev exists and is enslaved to an L3 device, then
1816 * prefer addresses from dst_dev and then the master over
1817 * any other enslaved devices in the L3 domain.
1819 master = l3mdev_master_dev_rcu(dst_dev);
1821 master_idx = master->ifindex;
1823 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1825 scores, hiscore_idx);
1827 if (scores[hiscore_idx].ifa)
1831 for_each_netdev_rcu(net, dev) {
1832 /* only consider addresses on devices in the
1835 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1837 idev = __in6_dev_get(dev);
1840 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1845 hiscore = &scores[hiscore_idx];
1847 ret = -EADDRNOTAVAIL;
1849 *saddr = hiscore->ifa->addr;
1854 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1856 static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1859 struct inet6_ifaddr *ifp;
1860 int err = -EADDRNOTAVAIL;
1862 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1863 if (ifp->scope > IFA_LINK)
1865 if (ifp->scope == IFA_LINK &&
1866 !(ifp->flags & banned_flags)) {
1875 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1878 struct inet6_dev *idev;
1879 int err = -EADDRNOTAVAIL;
1882 idev = __in6_dev_get(dev);
1884 read_lock_bh(&idev->lock);
1885 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1886 read_unlock_bh(&idev->lock);
1892 static int ipv6_count_addresses(const struct inet6_dev *idev)
1894 const struct inet6_ifaddr *ifp;
1898 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1904 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1905 const struct net_device *dev, int strict)
1907 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1908 strict, IFA_F_TENTATIVE);
1910 EXPORT_SYMBOL(ipv6_chk_addr);
1912 /* device argument is used to find the L3 domain of interest. If
1913 * skip_dev_check is set, then the ifp device is not checked against
1914 * the passed in dev argument. So the 2 cases for addresses checks are:
1915 * 1. does the address exist in the L3 domain that dev is part of
1916 * (skip_dev_check = true), or
1918 * 2. does the address exist on the specific device
1919 * (skip_dev_check = false)
1921 static struct net_device *
1922 __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1923 const struct net_device *dev, bool skip_dev_check,
1924 int strict, u32 banned_flags)
1926 unsigned int hash = inet6_addr_hash(net, addr);
1927 struct net_device *l3mdev, *ndev;
1928 struct inet6_ifaddr *ifp;
1933 l3mdev = l3mdev_master_dev_rcu(dev);
1937 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1938 ndev = ifp->idev->dev;
1940 if (l3mdev_master_dev_rcu(ndev) != l3mdev)
1943 /* Decouple optimistic from tentative for evaluation here.
1944 * Ban optimistic addresses explicitly, when required.
1946 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1947 ? (ifp->flags&~IFA_F_TENTATIVE)
1949 if (ipv6_addr_equal(&ifp->addr, addr) &&
1950 !(ifp_flags&banned_flags) &&
1951 (!dev || ndev == dev ||
1952 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1962 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1963 const struct net_device *dev, bool skip_dev_check,
1964 int strict, u32 banned_flags)
1966 return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
1967 strict, banned_flags) ? 1 : 0;
1969 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1972 /* Compares an address/prefix_len with addresses on device @dev.
1973 * If one is found it returns true.
1975 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1976 const unsigned int prefix_len, struct net_device *dev)
1978 const struct inet6_ifaddr *ifa;
1979 const struct inet6_dev *idev;
1983 idev = __in6_dev_get(dev);
1985 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1986 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1995 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1997 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1999 const struct inet6_ifaddr *ifa;
2000 const struct inet6_dev *idev;
2005 idev = __in6_dev_get(dev);
2007 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
2008 onlink = ipv6_prefix_equal(addr, &ifa->addr,
2017 EXPORT_SYMBOL(ipv6_chk_prefix);
2020 * ipv6_dev_find - find the first device with a given source address.
2021 * @net: the net namespace
2022 * @addr: the source address
2023 * @dev: used to find the L3 domain of interest
2025 * The caller should be protected by RCU, or RTNL.
2027 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
2028 struct net_device *dev)
2030 return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
2033 EXPORT_SYMBOL(ipv6_dev_find);
2035 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
2036 struct net_device *dev, int strict)
2038 unsigned int hash = inet6_addr_hash(net, addr);
2039 struct inet6_ifaddr *ifp, *result = NULL;
2042 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2043 if (ipv6_addr_equal(&ifp->addr, addr)) {
2044 if (!dev || ifp->idev->dev == dev ||
2045 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2057 /* Gets referenced address, destroys ifaddr */
2059 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2062 ifp->flags |= IFA_F_DADFAILED;
2064 if (ifp->flags&IFA_F_TEMPORARY) {
2065 struct inet6_ifaddr *ifpub;
2066 spin_lock_bh(&ifp->lock);
2069 in6_ifa_hold(ifpub);
2070 spin_unlock_bh(&ifp->lock);
2071 ipv6_create_tempaddr(ifpub, true);
2074 spin_unlock_bh(&ifp->lock);
2077 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2078 spin_lock_bh(&ifp->lock);
2079 addrconf_del_dad_work(ifp);
2080 ifp->flags |= IFA_F_TENTATIVE;
2082 ifp->flags &= ~IFA_F_OPTIMISTIC;
2083 spin_unlock_bh(&ifp->lock);
2085 ipv6_ifa_notify(0, ifp);
2092 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2096 spin_lock_bh(&ifp->lock);
2097 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2098 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2101 spin_unlock_bh(&ifp->lock);
2106 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2108 struct inet6_dev *idev = ifp->idev;
2109 struct net *net = dev_net(idev->dev);
2111 if (addrconf_dad_end(ifp)) {
2116 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2117 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2119 spin_lock_bh(&ifp->lock);
2121 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2122 struct in6_addr new_addr;
2123 struct inet6_ifaddr *ifp2;
2124 int retries = ifp->stable_privacy_retry + 1;
2125 struct ifa6_config cfg = {
2127 .plen = ifp->prefix_len,
2128 .ifa_flags = ifp->flags,
2129 .valid_lft = ifp->valid_lft,
2130 .preferred_lft = ifp->prefered_lft,
2131 .scope = ifp->scope,
2134 if (retries > net->ipv6.sysctl.idgen_retries) {
2135 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2136 ifp->idev->dev->name);
2140 new_addr = ifp->addr;
2141 if (ipv6_generate_stable_address(&new_addr, retries,
2145 spin_unlock_bh(&ifp->lock);
2147 if (idev->cnf.max_addresses &&
2148 ipv6_count_addresses(idev) >=
2149 idev->cnf.max_addresses)
2152 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2153 ifp->idev->dev->name);
2155 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2159 spin_lock_bh(&ifp2->lock);
2160 ifp2->stable_privacy_retry = retries;
2161 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2162 spin_unlock_bh(&ifp2->lock);
2164 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2167 spin_lock_bh(&ifp->lock);
2171 /* transition from _POSTDAD to _ERRDAD */
2172 ifp->state = INET6_IFADDR_STATE_ERRDAD;
2173 spin_unlock_bh(&ifp->lock);
2175 addrconf_mod_dad_work(ifp, 0);
2179 /* Join to solicited addr multicast group.
2180 * caller must hold RTNL */
2181 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2183 struct in6_addr maddr;
2185 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2188 addrconf_addr_solict_mult(addr, &maddr);
2189 ipv6_dev_mc_inc(dev, &maddr);
2192 /* caller must hold RTNL */
2193 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2195 struct in6_addr maddr;
2197 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2200 addrconf_addr_solict_mult(addr, &maddr);
2201 __ipv6_dev_mc_dec(idev, &maddr);
2204 /* caller must hold RTNL */
2205 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2207 struct in6_addr addr;
2209 if (ifp->prefix_len >= 127) /* RFC 6164 */
2211 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2212 if (ipv6_addr_any(&addr))
2214 __ipv6_dev_ac_inc(ifp->idev, &addr);
2217 /* caller must hold RTNL */
2218 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2220 struct in6_addr addr;
2222 if (ifp->prefix_len >= 127) /* RFC 6164 */
2224 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2225 if (ipv6_addr_any(&addr))
2227 __ipv6_dev_ac_dec(ifp->idev, &addr);
2230 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2232 switch (dev->addr_len) {
2234 memcpy(eui, dev->dev_addr, 3);
2237 memcpy(eui + 5, dev->dev_addr + 3, 3);
2239 case EUI64_ADDR_LEN:
2240 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2250 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2252 const union fwnet_hwaddr *ha;
2254 if (dev->addr_len != FWNET_ALEN)
2257 ha = (const union fwnet_hwaddr *)dev->dev_addr;
2259 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2264 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2266 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2267 if (dev->addr_len != ARCNET_ALEN)
2270 eui[7] = *(u8 *)dev->dev_addr;
2274 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2276 if (dev->addr_len != INFINIBAND_ALEN)
2278 memcpy(eui, dev->dev_addr + 12, 8);
2283 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2287 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2288 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2289 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2290 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2291 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2292 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2296 memcpy(eui + 4, &addr, 4);
2300 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2302 if (dev->priv_flags & IFF_ISATAP)
2303 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2307 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2309 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2312 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2314 memcpy(eui, dev->perm_addr, 3);
2315 memcpy(eui + 5, dev->perm_addr + 3, 3);
2322 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2324 switch (dev->type) {
2327 return addrconf_ifid_eui48(eui, dev);
2329 return addrconf_ifid_arcnet(eui, dev);
2330 case ARPHRD_INFINIBAND:
2331 return addrconf_ifid_infiniband(eui, dev);
2333 return addrconf_ifid_sit(eui, dev);
2336 return addrconf_ifid_gre(eui, dev);
2337 case ARPHRD_6LOWPAN:
2338 return addrconf_ifid_6lowpan(eui, dev);
2339 case ARPHRD_IEEE1394:
2340 return addrconf_ifid_ieee1394(eui, dev);
2341 case ARPHRD_TUNNEL6:
2344 return addrconf_ifid_ip6tnl(eui, dev);
2349 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2352 struct inet6_ifaddr *ifp;
2354 read_lock_bh(&idev->lock);
2355 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2356 if (ifp->scope > IFA_LINK)
2358 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2359 memcpy(eui, ifp->addr.s6_addr+8, 8);
2364 read_unlock_bh(&idev->lock);
2368 /* Generation of a randomized Interface Identifier
2369 * draft-ietf-6man-rfc4941bis, Section 3.3.1
2372 static void ipv6_gen_rnd_iid(struct in6_addr *addr)
2375 get_random_bytes(&addr->s6_addr[8], 8);
2377 /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2378 * check if generated address is not inappropriate:
2380 * - Reserved IPv6 Interface Identifiers
2381 * - XXX: already assigned to an address on the device
2384 /* Subnet-router anycast: 0000:0000:0000:0000 */
2385 if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2388 /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2389 * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
2390 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2392 if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2393 (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2396 /* Reserved subnet anycast addresses */
2397 if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2398 ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2407 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2408 struct net_device *dev, unsigned long expires,
2409 u32 flags, gfp_t gfp_flags)
2411 struct fib6_config cfg = {
2412 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2413 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2414 .fc_ifindex = dev->ifindex,
2415 .fc_expires = expires,
2417 .fc_flags = RTF_UP | flags,
2418 .fc_nlinfo.nl_net = dev_net(dev),
2419 .fc_protocol = RTPROT_KERNEL,
2420 .fc_type = RTN_UNICAST,
2425 /* Prevent useless cloning on PtP SIT.
2426 This thing is done here expecting that the whole
2427 class of non-broadcast devices need not cloning.
2429 #if IS_ENABLED(CONFIG_IPV6_SIT)
2430 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2431 cfg.fc_flags |= RTF_NONEXTHOP;
2434 ip6_route_add(&cfg, gfp_flags, NULL);
2438 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2440 const struct net_device *dev,
2441 u32 flags, u32 noflags,
2444 struct fib6_node *fn;
2445 struct fib6_info *rt = NULL;
2446 struct fib6_table *table;
2447 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2449 table = fib6_get_table(dev_net(dev), tb_id);
2454 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2458 for_each_fib6_node_rt_rcu(fn) {
2459 /* prefix routes only use builtin fib6_nh */
2463 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
2465 if (no_gw && rt->fib6_nh->fib_nh_gw_family)
2467 if ((rt->fib6_flags & flags) != flags)
2469 if ((rt->fib6_flags & noflags) != 0)
2471 if (!fib6_info_hold_safe(rt))
2481 /* Create "default" multicast route to the interface */
2483 static void addrconf_add_mroute(struct net_device *dev)
2485 struct fib6_config cfg = {
2486 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2487 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2488 .fc_ifindex = dev->ifindex,
2491 .fc_type = RTN_MULTICAST,
2492 .fc_nlinfo.nl_net = dev_net(dev),
2493 .fc_protocol = RTPROT_KERNEL,
2496 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2498 ip6_route_add(&cfg, GFP_KERNEL, NULL);
2501 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2503 struct inet6_dev *idev;
2507 idev = ipv6_find_idev(dev);
2511 if (idev->cnf.disable_ipv6)
2512 return ERR_PTR(-EACCES);
2514 /* Add default multicast route */
2515 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2516 addrconf_add_mroute(dev);
2521 static void manage_tempaddrs(struct inet6_dev *idev,
2522 struct inet6_ifaddr *ifp,
2523 __u32 valid_lft, __u32 prefered_lft,
2524 bool create, unsigned long now)
2527 struct inet6_ifaddr *ift;
2529 read_lock_bh(&idev->lock);
2530 /* update all temporary addresses in the list */
2531 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2532 int age, max_valid, max_prefered;
2534 if (ifp != ift->ifpub)
2537 /* RFC 4941 section 3.3:
2538 * If a received option will extend the lifetime of a public
2539 * address, the lifetimes of temporary addresses should
2540 * be extended, subject to the overall constraint that no
2541 * temporary addresses should ever remain "valid" or "preferred"
2542 * for a time longer than (TEMP_VALID_LIFETIME) or
2543 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2545 age = (now - ift->cstamp) / HZ;
2546 max_valid = idev->cnf.temp_valid_lft - age;
2550 max_prefered = idev->cnf.temp_prefered_lft -
2551 idev->desync_factor - age;
2552 if (max_prefered < 0)
2555 if (valid_lft > max_valid)
2556 valid_lft = max_valid;
2558 if (prefered_lft > max_prefered)
2559 prefered_lft = max_prefered;
2561 spin_lock(&ift->lock);
2563 ift->valid_lft = valid_lft;
2564 ift->prefered_lft = prefered_lft;
2566 if (prefered_lft > 0)
2567 ift->flags &= ~IFA_F_DEPRECATED;
2569 spin_unlock(&ift->lock);
2570 if (!(flags&IFA_F_TENTATIVE))
2571 ipv6_ifa_notify(0, ift);
2574 /* Also create a temporary address if it's enabled but no temporary
2575 * address currently exists.
2576 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
2577 * as part of cleanup (ie. deleting the mngtmpaddr).
2578 * We don't want that to result in creating a new temporary ip address.
2580 if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
2583 if (create && idev->cnf.use_tempaddr > 0) {
2584 /* When a new public address is created as described
2585 * in [ADDRCONF], also create a new temporary address.
2587 read_unlock_bh(&idev->lock);
2588 ipv6_create_tempaddr(ifp, false);
2590 read_unlock_bh(&idev->lock);
2594 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2596 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2597 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2600 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2601 const struct prefix_info *pinfo,
2602 struct inet6_dev *in6_dev,
2603 const struct in6_addr *addr, int addr_type,
2604 u32 addr_flags, bool sllao, bool tokenized,
2605 __u32 valid_lft, u32 prefered_lft)
2607 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2608 int create = 0, update_lft = 0;
2610 if (!ifp && valid_lft) {
2611 int max_addresses = in6_dev->cnf.max_addresses;
2612 struct ifa6_config cfg = {
2614 .plen = pinfo->prefix_len,
2615 .ifa_flags = addr_flags,
2616 .valid_lft = valid_lft,
2617 .preferred_lft = prefered_lft,
2618 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2619 .ifa_proto = IFAPROT_KERNEL_RA
2622 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2623 if ((net->ipv6.devconf_all->optimistic_dad ||
2624 in6_dev->cnf.optimistic_dad) &&
2625 !net->ipv6.devconf_all->forwarding && sllao)
2626 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2629 /* Do not allow to create too much of autoconfigured
2630 * addresses; this would be too easy way to crash kernel.
2632 if (!max_addresses ||
2633 ipv6_count_addresses(in6_dev) < max_addresses)
2634 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2636 if (IS_ERR_OR_NULL(ifp))
2640 spin_lock_bh(&ifp->lock);
2641 ifp->flags |= IFA_F_MANAGETEMPADDR;
2642 ifp->cstamp = jiffies;
2643 ifp->tokenized = tokenized;
2644 spin_unlock_bh(&ifp->lock);
2645 addrconf_dad_start(ifp);
2653 /* update lifetime (RFC2462 5.5.3 e) */
2654 spin_lock_bh(&ifp->lock);
2656 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2657 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2660 if (!create && stored_lft) {
2661 const u32 minimum_lft = min_t(u32,
2662 stored_lft, MIN_VALID_LIFETIME);
2663 valid_lft = max(valid_lft, minimum_lft);
2665 /* RFC4862 Section 5.5.3e:
2666 * "Note that the preferred lifetime of the
2667 * corresponding address is always reset to
2668 * the Preferred Lifetime in the received
2669 * Prefix Information option, regardless of
2670 * whether the valid lifetime is also reset or
2673 * So we should always update prefered_lft here.
2679 ifp->valid_lft = valid_lft;
2680 ifp->prefered_lft = prefered_lft;
2683 ifp->flags &= ~IFA_F_DEPRECATED;
2684 spin_unlock_bh(&ifp->lock);
2686 if (!(flags&IFA_F_TENTATIVE))
2687 ipv6_ifa_notify(0, ifp);
2689 spin_unlock_bh(&ifp->lock);
2691 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2695 addrconf_verify(net);
2700 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2702 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2704 struct prefix_info *pinfo;
2709 struct inet6_dev *in6_dev;
2710 struct net *net = dev_net(dev);
2712 pinfo = (struct prefix_info *) opt;
2714 if (len < sizeof(struct prefix_info)) {
2715 netdev_dbg(dev, "addrconf: prefix option too short\n");
2720 * Validation checks ([ADDRCONF], page 19)
2723 addr_type = ipv6_addr_type(&pinfo->prefix);
2725 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2728 valid_lft = ntohl(pinfo->valid);
2729 prefered_lft = ntohl(pinfo->prefered);
2731 if (prefered_lft > valid_lft) {
2732 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2736 in6_dev = in6_dev_get(dev);
2739 net_dbg_ratelimited("addrconf: device %s not configured\n",
2744 if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft)
2748 * Two things going on here:
2749 * 1) Add routes for on-link prefixes
2750 * 2) Configure prefixes with the auto flag set
2753 if (pinfo->onlink) {
2754 struct fib6_info *rt;
2755 unsigned long rt_expires;
2757 /* Avoid arithmetic overflow. Really, we could
2758 * save rt_expires in seconds, likely valid_lft,
2759 * but it would require division in fib gc, that it
2763 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2765 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2767 if (addrconf_finite_timeout(rt_expires))
2770 rt = addrconf_get_prefix_route(&pinfo->prefix,
2773 RTF_ADDRCONF | RTF_PREFIX_RT,
2777 /* Autoconf prefix route */
2778 if (valid_lft == 0) {
2779 ip6_del_rt(net, rt, false);
2781 } else if (addrconf_finite_timeout(rt_expires)) {
2783 fib6_set_expires(rt, jiffies + rt_expires);
2785 fib6_clean_expires(rt);
2787 } else if (valid_lft) {
2788 clock_t expires = 0;
2789 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2790 if (addrconf_finite_timeout(rt_expires)) {
2792 flags |= RTF_EXPIRES;
2793 expires = jiffies_to_clock_t(rt_expires);
2795 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2796 0, dev, expires, flags,
2799 fib6_info_release(rt);
2802 /* Try to figure out our local address for this prefix */
2804 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2805 struct in6_addr addr;
2806 bool tokenized = false, dev_addr_generated = false;
2808 if (pinfo->prefix_len == 64) {
2809 memcpy(&addr, &pinfo->prefix, 8);
2811 if (!ipv6_addr_any(&in6_dev->token)) {
2812 read_lock_bh(&in6_dev->lock);
2813 memcpy(addr.s6_addr + 8,
2814 in6_dev->token.s6_addr + 8, 8);
2815 read_unlock_bh(&in6_dev->lock);
2817 } else if (is_addr_mode_generate_stable(in6_dev) &&
2818 !ipv6_generate_stable_address(&addr, 0,
2820 addr_flags |= IFA_F_STABLE_PRIVACY;
2822 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2823 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2826 dev_addr_generated = true;
2830 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2835 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2838 tokenized, valid_lft,
2843 /* Ignore error case here because previous prefix add addr was
2844 * successful which will be notified.
2846 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2847 addr_type, addr_flags, sllao,
2848 tokenized, valid_lft,
2850 dev_addr_generated);
2852 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2854 in6_dev_put(in6_dev);
2857 static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
2858 struct in6_ifreq *ireq)
2860 struct ip_tunnel_parm p = { };
2863 if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
2864 return -EADDRNOTAVAIL;
2866 p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
2869 p.iph.protocol = IPPROTO_IPV6;
2872 if (!dev->netdev_ops->ndo_tunnel_ctl)
2874 err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
2878 dev = __dev_get_by_name(net, p.name);
2881 return dev_open(dev, NULL);
2885 * Set destination address.
2886 * Special case for SIT interfaces where we create a new "virtual"
2889 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2891 struct net_device *dev;
2892 struct in6_ifreq ireq;
2895 if (!IS_ENABLED(CONFIG_IPV6_SIT))
2897 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2901 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2902 if (dev && dev->type == ARPHRD_SIT)
2903 err = addrconf_set_sit_dstaddr(net, dev, &ireq);
2908 static int ipv6_mc_config(struct sock *sk, bool join,
2909 const struct in6_addr *addr, int ifindex)
2917 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2919 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2926 * Manual configuration of address on an interface
2928 static int inet6_addr_add(struct net *net, int ifindex,
2929 struct ifa6_config *cfg,
2930 struct netlink_ext_ack *extack)
2932 struct inet6_ifaddr *ifp;
2933 struct inet6_dev *idev;
2934 struct net_device *dev;
2935 unsigned long timeout;
2941 if (cfg->plen > 128) {
2942 NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
2946 /* check the lifetime */
2947 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) {
2948 NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
2952 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) {
2953 NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
2957 dev = __dev_get_by_index(net, ifindex);
2961 idev = addrconf_add_dev(dev);
2963 NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
2964 return PTR_ERR(idev);
2967 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2968 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2969 true, cfg->pfx, ifindex);
2972 NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed");
2977 cfg->scope = ipv6_addr_scope(cfg->pfx);
2979 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
2980 if (addrconf_finite_timeout(timeout)) {
2981 expires = jiffies_to_clock_t(timeout * HZ);
2982 cfg->valid_lft = timeout;
2983 flags = RTF_EXPIRES;
2987 cfg->ifa_flags |= IFA_F_PERMANENT;
2990 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
2991 if (addrconf_finite_timeout(timeout)) {
2993 cfg->ifa_flags |= IFA_F_DEPRECATED;
2994 cfg->preferred_lft = timeout;
2997 ifp = ipv6_add_addr(idev, cfg, true, extack);
2999 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
3000 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3001 ifp->rt_priority, dev, expires,
3005 /* Send a netlink notification if DAD is enabled and
3006 * optimistic flag is not set
3008 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
3009 ipv6_ifa_notify(0, ifp);
3011 * Note that section 3.1 of RFC 4429 indicates
3012 * that the Optimistic flag should not be set for
3013 * manually configured addresses
3015 addrconf_dad_start(ifp);
3016 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
3017 manage_tempaddrs(idev, ifp, cfg->valid_lft,
3018 cfg->preferred_lft, true, jiffies);
3020 addrconf_verify_rtnl(net);
3022 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3023 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
3027 return PTR_ERR(ifp);
3030 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
3031 const struct in6_addr *pfx, unsigned int plen,
3032 struct netlink_ext_ack *extack)
3034 struct inet6_ifaddr *ifp;
3035 struct inet6_dev *idev;
3036 struct net_device *dev;
3039 NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
3043 dev = __dev_get_by_index(net, ifindex);
3045 NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
3049 idev = __in6_dev_get(dev);
3051 NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
3055 read_lock_bh(&idev->lock);
3056 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3057 if (ifp->prefix_len == plen &&
3058 ipv6_addr_equal(pfx, &ifp->addr)) {
3060 read_unlock_bh(&idev->lock);
3062 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3063 (ifa_flags & IFA_F_MANAGETEMPADDR))
3064 manage_tempaddrs(idev, ifp, 0, 0, false,
3067 addrconf_verify_rtnl(net);
3068 if (ipv6_addr_is_multicast(pfx)) {
3069 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3070 false, pfx, dev->ifindex);
3075 read_unlock_bh(&idev->lock);
3077 NL_SET_ERR_MSG_MOD(extack, "address not found");
3078 return -EADDRNOTAVAIL;
3082 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3084 struct ifa6_config cfg = {
3085 .ifa_flags = IFA_F_PERMANENT,
3086 .preferred_lft = INFINITY_LIFE_TIME,
3087 .valid_lft = INFINITY_LIFE_TIME,
3089 struct in6_ifreq ireq;
3092 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3095 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3098 cfg.pfx = &ireq.ifr6_addr;
3099 cfg.plen = ireq.ifr6_prefixlen;
3102 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
3107 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3109 struct in6_ifreq ireq;
3112 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3115 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3119 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3120 ireq.ifr6_prefixlen, NULL);
3125 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3126 int plen, int scope, u8 proto)
3128 struct inet6_ifaddr *ifp;
3129 struct ifa6_config cfg = {
3132 .ifa_flags = IFA_F_PERMANENT,
3133 .valid_lft = INFINITY_LIFE_TIME,
3134 .preferred_lft = INFINITY_LIFE_TIME,
3139 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3141 spin_lock_bh(&ifp->lock);
3142 ifp->flags &= ~IFA_F_TENTATIVE;
3143 spin_unlock_bh(&ifp->lock);
3144 rt_genid_bump_ipv6(dev_net(idev->dev));
3145 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3150 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3151 static void add_v4_addrs(struct inet6_dev *idev)
3153 struct in6_addr addr;
3154 struct net_device *dev;
3155 struct net *net = dev_net(idev->dev);
3156 int scope, plen, offset = 0;
3161 memset(&addr, 0, sizeof(struct in6_addr));
3162 /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3163 if (idev->dev->addr_len == sizeof(struct in6_addr))
3164 offset = sizeof(struct in6_addr) - 4;
3165 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4);
3167 if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
3168 scope = IPV6_ADDR_COMPATv4;
3170 pflags |= RTF_NONEXTHOP;
3172 if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
3175 addr.s6_addr32[0] = htonl(0xfe800000);
3180 if (addr.s6_addr32[3]) {
3181 add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
3182 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3187 for_each_netdev(net, dev) {
3188 struct in_device *in_dev = __in_dev_get_rtnl(dev);
3189 if (in_dev && (dev->flags & IFF_UP)) {
3190 struct in_ifaddr *ifa;
3193 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3194 addr.s6_addr32[3] = ifa->ifa_local;
3196 if (ifa->ifa_scope == RT_SCOPE_LINK)
3198 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3199 if (idev->dev->flags&IFF_POINTOPOINT)
3204 add_addr(idev, &addr, plen, flag,
3206 addrconf_prefix_route(&addr, plen, 0, idev->dev,
3207 0, pflags, GFP_KERNEL);
3214 static void init_loopback(struct net_device *dev)
3216 struct inet6_dev *idev;
3222 idev = ipv6_find_idev(dev);
3224 pr_debug("%s: add_dev failed\n", __func__);
3228 add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
3231 void addrconf_add_linklocal(struct inet6_dev *idev,
3232 const struct in6_addr *addr, u32 flags)
3234 struct ifa6_config cfg = {
3237 .ifa_flags = flags | IFA_F_PERMANENT,
3238 .valid_lft = INFINITY_LIFE_TIME,
3239 .preferred_lft = INFINITY_LIFE_TIME,
3241 .ifa_proto = IFAPROT_KERNEL_LL
3243 struct inet6_ifaddr *ifp;
3245 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3246 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3247 idev->cnf.optimistic_dad) &&
3248 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3249 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3252 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3254 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3256 addrconf_dad_start(ifp);
3260 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3262 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3264 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3267 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3268 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3271 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3272 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3278 static int ipv6_generate_stable_address(struct in6_addr *address,
3280 const struct inet6_dev *idev)
3282 static DEFINE_SPINLOCK(lock);
3283 static __u32 digest[SHA1_DIGEST_WORDS];
3284 static __u32 workspace[SHA1_WORKSPACE_WORDS];
3287 char __data[SHA1_BLOCK_SIZE];
3289 struct in6_addr secret;
3291 unsigned char hwaddr[MAX_ADDR_LEN];
3296 struct in6_addr secret;
3297 struct in6_addr temp;
3298 struct net *net = dev_net(idev->dev);
3300 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3302 if (idev->cnf.stable_secret.initialized)
3303 secret = idev->cnf.stable_secret.secret;
3304 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3305 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3310 spin_lock_bh(&lock);
3313 memset(&data, 0, sizeof(data));
3314 memset(workspace, 0, sizeof(workspace));
3315 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3316 data.prefix[0] = address->s6_addr32[0];
3317 data.prefix[1] = address->s6_addr32[1];
3318 data.secret = secret;
3319 data.dad_count = dad_count;
3321 sha1_transform(digest, data.__data, workspace);
3324 temp.s6_addr32[2] = (__force __be32)digest[0];
3325 temp.s6_addr32[3] = (__force __be32)digest[1];
3327 spin_unlock_bh(&lock);
3329 if (ipv6_reserved_interfaceid(temp)) {
3331 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3340 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3342 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3346 s = &idev->cnf.stable_secret;
3347 get_random_bytes(&s->secret, sizeof(s->secret));
3348 s->initialized = true;
3351 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3353 struct in6_addr addr;
3355 /* no link local addresses on L3 master devices */
3356 if (netif_is_l3_master(idev->dev))
3359 /* no link local addresses on devices flagged as slaves */
3360 if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
3363 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3365 switch (idev->cnf.addr_gen_mode) {
3366 case IN6_ADDR_GEN_MODE_RANDOM:
3367 ipv6_gen_mode_random_init(idev);
3369 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3370 if (!ipv6_generate_stable_address(&addr, 0, idev))
3371 addrconf_add_linklocal(idev, &addr,
3372 IFA_F_STABLE_PRIVACY);
3373 else if (prefix_route)
3374 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3377 case IN6_ADDR_GEN_MODE_EUI64:
3378 /* addrconf_add_linklocal also adds a prefix_route and we
3379 * only need to care about prefix routes if ipv6_generate_eui64
3380 * couldn't generate one.
3382 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3383 addrconf_add_linklocal(idev, &addr, 0);
3384 else if (prefix_route)
3385 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3388 case IN6_ADDR_GEN_MODE_NONE:
3390 /* will not add any link local address */
3395 static void addrconf_dev_config(struct net_device *dev)
3397 struct inet6_dev *idev;
3401 if ((dev->type != ARPHRD_ETHER) &&
3402 (dev->type != ARPHRD_FDDI) &&
3403 (dev->type != ARPHRD_ARCNET) &&
3404 (dev->type != ARPHRD_INFINIBAND) &&
3405 (dev->type != ARPHRD_IEEE1394) &&
3406 (dev->type != ARPHRD_TUNNEL6) &&
3407 (dev->type != ARPHRD_6LOWPAN) &&
3408 (dev->type != ARPHRD_TUNNEL) &&
3409 (dev->type != ARPHRD_NONE) &&
3410 (dev->type != ARPHRD_RAWIP)) {
3411 /* Alas, we support only Ethernet autoconfiguration. */
3412 idev = __in6_dev_get(dev);
3413 if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3414 dev->flags & IFF_MULTICAST)
3419 idev = addrconf_add_dev(dev);
3423 /* this device type has no EUI support */
3424 if (dev->type == ARPHRD_NONE &&
3425 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3426 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3428 addrconf_addr_gen(idev, false);
3431 #if IS_ENABLED(CONFIG_IPV6_SIT)
3432 static void addrconf_sit_config(struct net_device *dev)
3434 struct inet6_dev *idev;
3439 * Configure the tunnel with one of our IPv4
3440 * addresses... we should configure all of
3441 * our v4 addrs in the tunnel
3444 idev = ipv6_find_idev(dev);
3446 pr_debug("%s: add_dev failed\n", __func__);
3450 if (dev->priv_flags & IFF_ISATAP) {
3451 addrconf_addr_gen(idev, false);
3457 if (dev->flags&IFF_POINTOPOINT)
3458 addrconf_add_mroute(dev);
3462 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3463 static void addrconf_gre_config(struct net_device *dev)
3465 struct inet6_dev *idev;
3469 idev = ipv6_find_idev(dev);
3471 pr_debug("%s: add_dev failed\n", __func__);
3475 if (dev->type == ARPHRD_ETHER) {
3476 addrconf_addr_gen(idev, true);
3482 if (dev->flags & IFF_POINTOPOINT)
3483 addrconf_add_mroute(dev);
3487 static void addrconf_init_auto_addrs(struct net_device *dev)
3489 switch (dev->type) {
3490 #if IS_ENABLED(CONFIG_IPV6_SIT)
3492 addrconf_sit_config(dev);
3495 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3498 addrconf_gre_config(dev);
3501 case ARPHRD_LOOPBACK:
3506 addrconf_dev_config(dev);
3511 static int fixup_permanent_addr(struct net *net,
3512 struct inet6_dev *idev,
3513 struct inet6_ifaddr *ifp)
3515 /* !fib6_node means the host route was removed from the
3516 * FIB, for example, if 'lo' device is taken down. In that
3517 * case regenerate the host route.
3519 if (!ifp->rt || !ifp->rt->fib6_node) {
3520 struct fib6_info *f6i, *prev;
3522 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3525 return PTR_ERR(f6i);
3527 /* ifp->rt can be accessed outside of rtnl */
3528 spin_lock(&ifp->lock);
3531 spin_unlock(&ifp->lock);
3533 fib6_info_release(prev);
3536 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3537 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3538 ifp->rt_priority, idev->dev, 0, 0,
3542 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3543 addrconf_dad_start(ifp);
3548 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3550 struct inet6_ifaddr *ifp, *tmp;
3551 struct inet6_dev *idev;
3553 idev = __in6_dev_get(dev);
3557 write_lock_bh(&idev->lock);
3559 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3560 if ((ifp->flags & IFA_F_PERMANENT) &&
3561 fixup_permanent_addr(net, idev, ifp) < 0) {
3562 write_unlock_bh(&idev->lock);
3565 write_lock_bh(&idev->lock);
3567 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3568 idev->dev->name, &ifp->addr);
3572 write_unlock_bh(&idev->lock);
3575 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3578 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3579 struct netdev_notifier_change_info *change_info;
3580 struct netdev_notifier_changeupper_info *info;
3581 struct inet6_dev *idev = __in6_dev_get(dev);
3582 struct net *net = dev_net(dev);
3583 int run_pending = 0;
3587 case NETDEV_REGISTER:
3588 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3589 idev = ipv6_add_dev(dev);
3591 return notifier_from_errno(PTR_ERR(idev));
3595 case NETDEV_CHANGEMTU:
3596 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3597 if (dev->mtu < IPV6_MIN_MTU) {
3598 addrconf_ifdown(dev, dev != net->loopback_dev);
3603 rt6_mtu_change(dev, dev->mtu);
3604 idev->cnf.mtu6 = dev->mtu;
3608 /* allocate new idev */
3609 idev = ipv6_add_dev(dev);
3613 /* device is still not ready */
3614 if (!(idev->if_flags & IF_READY))
3621 if (idev && idev->cnf.disable_ipv6)
3624 if (dev->priv_flags & IFF_NO_ADDRCONF) {
3625 if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3626 dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3631 if (event == NETDEV_UP) {
3632 /* restore routes for permanent addresses */
3633 addrconf_permanent_addr(net, dev);
3635 if (!addrconf_link_ready(dev)) {
3636 /* device is not ready yet. */
3637 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3642 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3643 idev = ipv6_add_dev(dev);
3645 if (!IS_ERR_OR_NULL(idev)) {
3646 idev->if_flags |= IF_READY;
3649 } else if (event == NETDEV_CHANGE) {
3650 if (!addrconf_link_ready(dev)) {
3651 /* device is still not ready. */
3652 rt6_sync_down_dev(dev, event);
3656 if (!IS_ERR_OR_NULL(idev)) {
3657 if (idev->if_flags & IF_READY) {
3658 /* device is already configured -
3659 * but resend MLD reports, we might
3660 * have roamed and need to update
3661 * multicast snooping switches
3665 if (change_info->flags_changed & IFF_NOARP)
3666 addrconf_dad_run(idev, true);
3667 rt6_sync_up(dev, RTNH_F_LINKDOWN);
3670 idev->if_flags |= IF_READY;
3673 pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3679 addrconf_init_auto_addrs(dev);
3681 if (!IS_ERR_OR_NULL(idev)) {
3683 addrconf_dad_run(idev, false);
3685 /* Device has an address by now */
3686 rt6_sync_up(dev, RTNH_F_DEAD);
3689 * If the MTU changed during the interface down,
3690 * when the interface up, the changed MTU must be
3691 * reflected in the idev as well as routers.
3693 if (idev->cnf.mtu6 != dev->mtu &&
3694 dev->mtu >= IPV6_MIN_MTU) {
3695 rt6_mtu_change(dev, dev->mtu);
3696 idev->cnf.mtu6 = dev->mtu;
3698 idev->tstamp = jiffies;
3699 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3702 * If the changed mtu during down is lower than
3703 * IPV6_MIN_MTU stop IPv6 on this interface.
3705 if (dev->mtu < IPV6_MIN_MTU)
3706 addrconf_ifdown(dev, dev != net->loopback_dev);
3711 case NETDEV_UNREGISTER:
3713 * Remove all addresses from this interface.
3715 addrconf_ifdown(dev, event != NETDEV_DOWN);
3718 case NETDEV_CHANGENAME:
3720 snmp6_unregister_dev(idev);
3721 addrconf_sysctl_unregister(idev);
3722 err = addrconf_sysctl_register(idev);
3724 return notifier_from_errno(err);
3725 err = snmp6_register_dev(idev);
3727 addrconf_sysctl_unregister(idev);
3728 return notifier_from_errno(err);
3733 case NETDEV_PRE_TYPE_CHANGE:
3734 case NETDEV_POST_TYPE_CHANGE:
3736 addrconf_type_change(dev, event);
3739 case NETDEV_CHANGEUPPER:
3742 /* flush all routes if dev is linked to or unlinked from
3743 * an L3 master device (e.g., VRF)
3745 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3746 addrconf_ifdown(dev, false);
3753 * addrconf module should be notified of a device going up
3755 static struct notifier_block ipv6_dev_notf = {
3756 .notifier_call = addrconf_notify,
3757 .priority = ADDRCONF_NOTIFY_PRIORITY,
3760 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3762 struct inet6_dev *idev;
3765 idev = __in6_dev_get(dev);
3767 if (event == NETDEV_POST_TYPE_CHANGE)
3768 ipv6_mc_remap(idev);
3769 else if (event == NETDEV_PRE_TYPE_CHANGE)
3770 ipv6_mc_unmap(idev);
3773 static bool addr_is_local(const struct in6_addr *addr)
3775 return ipv6_addr_type(addr) &
3776 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3779 static int addrconf_ifdown(struct net_device *dev, bool unregister)
3781 unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3782 struct net *net = dev_net(dev);
3783 struct inet6_dev *idev;
3784 struct inet6_ifaddr *ifa;
3785 LIST_HEAD(tmp_addr_list);
3786 bool keep_addr = false;
3792 rt6_disable_ip(dev, event);
3794 idev = __in6_dev_get(dev);
3799 * Step 1: remove reference to ipv6 device from parent device.
3805 /* protected by rtnl_lock */
3806 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3808 /* Step 1.5: remove snmp6 entry */
3809 snmp6_unregister_dev(idev);
3813 /* combine the user config with event to determine if permanent
3814 * addresses are to be removed from address hash table
3816 if (!unregister && !idev->cnf.disable_ipv6) {
3817 /* aggregate the system setting and interface setting */
3818 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3821 _keep_addr = idev->cnf.keep_addr_on_down;
3823 keep_addr = (_keep_addr > 0);
3826 /* Step 2: clear hash table */
3827 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3828 struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
3830 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
3832 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3833 if (ifa->idev == idev) {
3834 addrconf_del_dad_work(ifa);
3835 /* combined flag + permanent flag decide if
3836 * address is retained on a down event
3839 !(ifa->flags & IFA_F_PERMANENT) ||
3840 addr_is_local(&ifa->addr)) {
3841 hlist_del_init_rcu(&ifa->addr_lst);
3846 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
3849 write_lock_bh(&idev->lock);
3851 addrconf_del_rs_timer(idev);
3853 /* Step 2: clear flags for stateless addrconf, repeated down
3856 was_ready = idev->if_flags & IF_READY;
3858 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3860 /* Step 3: clear tempaddr list */
3861 while (!list_empty(&idev->tempaddr_list)) {
3862 ifa = list_first_entry(&idev->tempaddr_list,
3863 struct inet6_ifaddr, tmp_list);
3864 list_del(&ifa->tmp_list);
3865 write_unlock_bh(&idev->lock);
3866 spin_lock_bh(&ifa->lock);
3869 in6_ifa_put(ifa->ifpub);
3872 spin_unlock_bh(&ifa->lock);
3874 write_lock_bh(&idev->lock);
3877 list_for_each_entry(ifa, &idev->addr_list, if_list)
3878 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
3879 write_unlock_bh(&idev->lock);
3881 while (!list_empty(&tmp_addr_list)) {
3882 struct fib6_info *rt = NULL;
3885 ifa = list_first_entry(&tmp_addr_list,
3886 struct inet6_ifaddr, if_list_aux);
3887 list_del(&ifa->if_list_aux);
3889 addrconf_del_dad_work(ifa);
3891 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3892 !addr_is_local(&ifa->addr);
3894 spin_lock_bh(&ifa->lock);
3897 /* set state to skip the notifier below */
3898 state = INET6_IFADDR_STATE_DEAD;
3899 ifa->state = INET6_IFADDR_STATE_PREDAD;
3900 if (!(ifa->flags & IFA_F_NODAD))
3901 ifa->flags |= IFA_F_TENTATIVE;
3907 ifa->state = INET6_IFADDR_STATE_DEAD;
3910 spin_unlock_bh(&ifa->lock);
3913 ip6_del_rt(net, rt, false);
3915 if (state != INET6_IFADDR_STATE_DEAD) {
3916 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3917 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3919 if (idev->cnf.forwarding)
3920 addrconf_leave_anycast(ifa);
3921 addrconf_leave_solict(ifa->idev, &ifa->addr);
3925 write_lock_bh(&idev->lock);
3926 list_del_rcu(&ifa->if_list);
3927 write_unlock_bh(&idev->lock);
3932 /* Step 5: Discard anycast and multicast list */
3934 ipv6_ac_destroy_dev(idev);
3935 ipv6_mc_destroy_dev(idev);
3936 } else if (was_ready) {
3940 idev->tstamp = jiffies;
3943 /* Last: Shot the device (if unregistered) */
3945 addrconf_sysctl_unregister(idev);
3946 neigh_parms_release(&nd_tbl, idev->nd_parms);
3947 neigh_ifdown(&nd_tbl, dev);
3953 static void addrconf_rs_timer(struct timer_list *t)
3955 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3956 struct net_device *dev = idev->dev;
3957 struct in6_addr lladdr;
3959 write_lock(&idev->lock);
3960 if (idev->dead || !(idev->if_flags & IF_READY))
3963 if (!ipv6_accept_ra(idev))
3966 /* Announcement received after solicitation was sent */
3967 if (idev->if_flags & IF_RA_RCVD)
3970 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3971 write_unlock(&idev->lock);
3972 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3973 ndisc_send_rs(dev, &lladdr,
3974 &in6addr_linklocal_allrouters);
3978 write_lock(&idev->lock);
3979 idev->rs_interval = rfc3315_s14_backoff_update(
3980 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3981 /* The wait after the last probe can be shorter */
3982 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3983 idev->cnf.rtr_solicits) ?
3984 idev->cnf.rtr_solicit_delay :
3988 * Note: we do not support deprecated "all on-link"
3989 * assumption any longer.
3991 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3995 write_unlock(&idev->lock);
4001 * Duplicate Address Detection
4003 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
4005 unsigned long rand_num;
4006 struct inet6_dev *idev = ifp->idev;
4009 if (ifp->flags & IFA_F_OPTIMISTIC)
4012 rand_num = get_random_u32_below(idev->cnf.rtr_solicit_delay ? : 1);
4015 if (idev->cnf.enhanced_dad ||
4016 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
4018 get_random_bytes(&nonce, 6);
4021 ifp->dad_nonce = nonce;
4022 ifp->dad_probes = idev->cnf.dad_transmits;
4023 addrconf_mod_dad_work(ifp, rand_num);
4026 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
4028 struct inet6_dev *idev = ifp->idev;
4029 struct net_device *dev = idev->dev;
4030 bool bump_id, notify = false;
4033 addrconf_join_solict(dev, &ifp->addr);
4035 read_lock_bh(&idev->lock);
4036 spin_lock(&ifp->lock);
4037 if (ifp->state == INET6_IFADDR_STATE_DEAD)
4041 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4042 (net->ipv6.devconf_all->accept_dad < 1 &&
4043 idev->cnf.accept_dad < 1) ||
4044 !(ifp->flags&IFA_F_TENTATIVE) ||
4045 ifp->flags & IFA_F_NODAD) {
4046 bool send_na = false;
4048 if (ifp->flags & IFA_F_TENTATIVE &&
4049 !(ifp->flags & IFA_F_OPTIMISTIC))
4051 bump_id = ifp->flags & IFA_F_TENTATIVE;
4052 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4053 spin_unlock(&ifp->lock);
4054 read_unlock_bh(&idev->lock);
4056 addrconf_dad_completed(ifp, bump_id, send_na);
4060 if (!(idev->if_flags & IF_READY)) {
4061 spin_unlock(&ifp->lock);
4062 read_unlock_bh(&idev->lock);
4064 * If the device is not ready:
4065 * - keep it tentative if it is a permanent address.
4066 * - otherwise, kill it.
4069 addrconf_dad_stop(ifp, 0);
4074 * Optimistic nodes can start receiving
4077 if (ifp->flags & IFA_F_OPTIMISTIC) {
4078 ip6_ins_rt(net, ifp->rt);
4079 if (ipv6_use_optimistic_addr(net, idev)) {
4080 /* Because optimistic nodes can use this address,
4081 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4087 addrconf_dad_kick(ifp);
4089 spin_unlock(&ifp->lock);
4090 read_unlock_bh(&idev->lock);
4092 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4095 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4097 bool begin_dad = false;
4099 spin_lock_bh(&ifp->lock);
4100 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4101 ifp->state = INET6_IFADDR_STATE_PREDAD;
4104 spin_unlock_bh(&ifp->lock);
4107 addrconf_mod_dad_work(ifp, 0);
4110 static void addrconf_dad_work(struct work_struct *w)
4112 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4113 struct inet6_ifaddr,
4115 struct inet6_dev *idev = ifp->idev;
4116 bool bump_id, disable_ipv6 = false;
4117 struct in6_addr mcaddr;
4123 } action = DAD_PROCESS;
4127 spin_lock_bh(&ifp->lock);
4128 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4130 ifp->state = INET6_IFADDR_STATE_DAD;
4131 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4133 ifp->state = INET6_IFADDR_STATE_POSTDAD;
4135 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4136 idev->cnf.accept_dad > 1) &&
4137 !idev->cnf.disable_ipv6 &&
4138 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4139 struct in6_addr addr;
4141 addr.s6_addr32[0] = htonl(0xfe800000);
4142 addr.s6_addr32[1] = 0;
4144 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4145 ipv6_addr_equal(&ifp->addr, &addr)) {
4146 /* DAD failed for link-local based on MAC */
4147 idev->cnf.disable_ipv6 = 1;
4149 pr_info("%s: IPv6 being disabled!\n",
4150 ifp->idev->dev->name);
4151 disable_ipv6 = true;
4155 spin_unlock_bh(&ifp->lock);
4157 if (action == DAD_BEGIN) {
4158 addrconf_dad_begin(ifp);
4160 } else if (action == DAD_ABORT) {
4162 addrconf_dad_stop(ifp, 1);
4164 addrconf_ifdown(idev->dev, false);
4168 if (!ifp->dad_probes && addrconf_dad_end(ifp))
4171 write_lock_bh(&idev->lock);
4172 if (idev->dead || !(idev->if_flags & IF_READY)) {
4173 write_unlock_bh(&idev->lock);
4177 spin_lock(&ifp->lock);
4178 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4179 spin_unlock(&ifp->lock);
4180 write_unlock_bh(&idev->lock);
4184 if (ifp->dad_probes == 0) {
4185 bool send_na = false;
4188 * DAD was successful
4191 if (ifp->flags & IFA_F_TENTATIVE &&
4192 !(ifp->flags & IFA_F_OPTIMISTIC))
4194 bump_id = ifp->flags & IFA_F_TENTATIVE;
4195 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4196 spin_unlock(&ifp->lock);
4197 write_unlock_bh(&idev->lock);
4199 addrconf_dad_completed(ifp, bump_id, send_na);
4205 addrconf_mod_dad_work(ifp,
4206 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
4208 spin_unlock(&ifp->lock);
4209 write_unlock_bh(&idev->lock);
4211 /* send a neighbour solicitation for our addr */
4212 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4213 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4220 /* ifp->idev must be at least read locked */
4221 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4223 struct inet6_ifaddr *ifpiter;
4224 struct inet6_dev *idev = ifp->idev;
4226 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4227 if (ifpiter->scope > IFA_LINK)
4229 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4230 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4231 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4238 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4241 struct net_device *dev = ifp->idev->dev;
4242 struct in6_addr lladdr;
4243 bool send_rs, send_mld;
4245 addrconf_del_dad_work(ifp);
4248 * Configure the address for reception. Now it is valid.
4251 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4253 /* If added prefix is link local and we are prepared to process
4254 router advertisements, start sending router solicitations.
4257 read_lock_bh(&ifp->idev->lock);
4258 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4259 send_rs = send_mld &&
4260 ipv6_accept_ra(ifp->idev) &&
4261 ifp->idev->cnf.rtr_solicits != 0 &&
4262 (dev->flags & IFF_LOOPBACK) == 0 &&
4263 (dev->type != ARPHRD_TUNNEL) &&
4264 !netif_is_team_port(dev);
4265 read_unlock_bh(&ifp->idev->lock);
4267 /* While dad is in progress mld report's source address is in6_addrany.
4268 * Resend with proper ll now.
4271 ipv6_mc_dad_complete(ifp->idev);
4273 /* send unsolicited NA if enabled */
4275 (ifp->idev->cnf.ndisc_notify ||
4276 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4277 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4278 /*router=*/ !!ifp->idev->cnf.forwarding,
4279 /*solicited=*/ false, /*override=*/ true,
4285 * If a host as already performed a random delay
4286 * [...] as part of DAD [...] there is no need
4287 * to delay again before sending the first RS
4289 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4291 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4293 write_lock_bh(&ifp->idev->lock);
4294 spin_lock(&ifp->lock);
4295 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4296 ifp->idev->cnf.rtr_solicit_interval);
4297 ifp->idev->rs_probes = 1;
4298 ifp->idev->if_flags |= IF_RS_SENT;
4299 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4300 spin_unlock(&ifp->lock);
4301 write_unlock_bh(&ifp->idev->lock);
4305 rt_genid_bump_ipv6(dev_net(dev));
4307 /* Make sure that a new temporary address will be created
4308 * before this temporary address becomes deprecated.
4310 if (ifp->flags & IFA_F_TEMPORARY)
4311 addrconf_verify_rtnl(dev_net(dev));
4314 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4316 struct inet6_ifaddr *ifp;
4318 read_lock_bh(&idev->lock);
4319 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4320 spin_lock(&ifp->lock);
4321 if ((ifp->flags & IFA_F_TENTATIVE &&
4322 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4324 ifp->state = INET6_IFADDR_STATE_PREDAD;
4325 addrconf_dad_kick(ifp);
4327 spin_unlock(&ifp->lock);
4329 read_unlock_bh(&idev->lock);
4332 #ifdef CONFIG_PROC_FS
4333 struct if6_iter_state {
4334 struct seq_net_private p;
4339 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4341 struct if6_iter_state *state = seq->private;
4342 struct net *net = seq_file_net(seq);
4343 struct inet6_ifaddr *ifa = NULL;
4346 /* initial bucket if pos is 0 */
4352 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4353 hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
4355 /* sync with offset */
4356 if (p < state->offset) {
4363 /* prepare for next bucket */
4370 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4371 struct inet6_ifaddr *ifa)
4373 struct if6_iter_state *state = seq->private;
4374 struct net *net = seq_file_net(seq);
4376 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4382 while (++state->bucket < IN6_ADDR_HSIZE) {
4383 hlist_for_each_entry_rcu(ifa,
4384 &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4392 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4396 return if6_get_first(seq, *pos);
4399 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4401 struct inet6_ifaddr *ifa;
4403 ifa = if6_get_next(seq, v);
4408 static void if6_seq_stop(struct seq_file *seq, void *v)
4414 static int if6_seq_show(struct seq_file *seq, void *v)
4416 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4417 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4419 ifp->idev->dev->ifindex,
4423 ifp->idev->dev->name);
4427 static const struct seq_operations if6_seq_ops = {
4428 .start = if6_seq_start,
4429 .next = if6_seq_next,
4430 .show = if6_seq_show,
4431 .stop = if6_seq_stop,
4434 static int __net_init if6_proc_net_init(struct net *net)
4436 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4437 sizeof(struct if6_iter_state)))
4442 static void __net_exit if6_proc_net_exit(struct net *net)
4444 remove_proc_entry("if_inet6", net->proc_net);
4447 static struct pernet_operations if6_proc_net_ops = {
4448 .init = if6_proc_net_init,
4449 .exit = if6_proc_net_exit,
4452 int __init if6_proc_init(void)
4454 return register_pernet_subsys(&if6_proc_net_ops);
4457 void if6_proc_exit(void)
4459 unregister_pernet_subsys(&if6_proc_net_ops);
4461 #endif /* CONFIG_PROC_FS */
4463 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4464 /* Check if address is a home address configured on any interface. */
4465 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4467 unsigned int hash = inet6_addr_hash(net, addr);
4468 struct inet6_ifaddr *ifp = NULL;
4472 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4473 if (ipv6_addr_equal(&ifp->addr, addr) &&
4474 (ifp->flags & IFA_F_HOMEADDRESS)) {
4484 /* RFC6554 has some algorithm to avoid loops in segment routing by
4485 * checking if the segments contains any of a local interface address.
4489 * To detect loops in the SRH, a router MUST determine if the SRH
4490 * includes multiple addresses assigned to any interface on that router.
4491 * If such addresses appear more than once and are separated by at least
4492 * one address not assigned to that router.
4494 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4495 unsigned char nsegs)
4497 const struct in6_addr *addr;
4498 int i, ret = 0, found = 0;
4499 struct inet6_ifaddr *ifp;
4500 bool separated = false;
4505 for (i = 0; i < nsegs; i++) {
4507 hash = inet6_addr_hash(net, addr);
4510 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4512 if (ipv6_addr_equal(&ifp->addr, addr)) {
4519 if (found > 1 && separated) {
4536 * Periodic address status verification
4539 static void addrconf_verify_rtnl(struct net *net)
4541 unsigned long now, next, next_sec, next_sched;
4542 struct inet6_ifaddr *ifp;
4549 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4551 cancel_delayed_work(&net->ipv6.addr_chk_work);
4553 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4555 hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
4558 /* When setting preferred_lft to a value not zero or
4559 * infinity, while valid_lft is infinity
4560 * IFA_F_PERMANENT has a non-infinity life time.
4562 if ((ifp->flags & IFA_F_PERMANENT) &&
4563 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4566 spin_lock(&ifp->lock);
4567 /* We try to batch several events at once. */
4568 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4570 if ((ifp->flags&IFA_F_TEMPORARY) &&
4571 !(ifp->flags&IFA_F_TENTATIVE) &&
4572 ifp->prefered_lft != INFINITY_LIFE_TIME &&
4573 !ifp->regen_count && ifp->ifpub) {
4574 /* This is a non-regenerated temporary addr. */
4576 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4577 ifp->idev->cnf.dad_transmits *
4578 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
4580 if (age + regen_advance >= ifp->prefered_lft) {
4581 struct inet6_ifaddr *ifpub = ifp->ifpub;
4582 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4583 next = ifp->tstamp + ifp->prefered_lft * HZ;
4587 in6_ifa_hold(ifpub);
4588 spin_unlock(&ifp->lock);
4590 spin_lock(&ifpub->lock);
4591 ifpub->regen_count = 0;
4592 spin_unlock(&ifpub->lock);
4593 rcu_read_unlock_bh();
4594 ipv6_create_tempaddr(ifpub, true);
4599 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4600 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4603 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4604 age >= ifp->valid_lft) {
4605 spin_unlock(&ifp->lock);
4607 rcu_read_unlock_bh();
4611 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4612 spin_unlock(&ifp->lock);
4614 } else if (age >= ifp->prefered_lft) {
4615 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4618 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4620 ifp->flags |= IFA_F_DEPRECATED;
4623 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4624 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4625 next = ifp->tstamp + ifp->valid_lft * HZ;
4627 spin_unlock(&ifp->lock);
4632 ipv6_ifa_notify(0, ifp);
4637 /* ifp->prefered_lft <= ifp->valid_lft */
4638 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4639 next = ifp->tstamp + ifp->prefered_lft * HZ;
4640 spin_unlock(&ifp->lock);
4645 next_sec = round_jiffies_up(next);
4648 /* If rounded timeout is accurate enough, accept it. */
4649 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4650 next_sched = next_sec;
4652 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4653 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4654 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4656 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4657 now, next, next_sec, next_sched);
4658 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
4659 rcu_read_unlock_bh();
4662 static void addrconf_verify_work(struct work_struct *w)
4664 struct net *net = container_of(to_delayed_work(w), struct net,
4665 ipv6.addr_chk_work);
4668 addrconf_verify_rtnl(net);
4672 static void addrconf_verify(struct net *net)
4674 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4677 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4678 struct in6_addr **peer_pfx)
4680 struct in6_addr *pfx = NULL;
4685 pfx = nla_data(addr);
4688 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4690 pfx = nla_data(local);
4696 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4697 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4698 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4699 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4700 [IFA_FLAGS] = { .len = sizeof(u32) },
4701 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
4702 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
4703 [IFA_PROTO] = { .type = NLA_U8 },
4707 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4708 struct netlink_ext_ack *extack)
4710 struct net *net = sock_net(skb->sk);
4711 struct ifaddrmsg *ifm;
4712 struct nlattr *tb[IFA_MAX+1];
4713 struct in6_addr *pfx, *peer_pfx;
4717 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4718 ifa_ipv6_policy, extack);
4722 ifm = nlmsg_data(nlh);
4723 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4727 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4729 /* We ignore other flags so far. */
4730 ifa_flags &= IFA_F_MANAGETEMPADDR;
4732 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4733 ifm->ifa_prefixlen, extack);
4736 static int modify_prefix_route(struct inet6_ifaddr *ifp,
4737 unsigned long expires, u32 flags,
4740 struct fib6_info *f6i;
4743 f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4745 ifp->idev->dev, 0, RTF_DEFAULT, true);
4749 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4750 if (f6i->fib6_metric != prio) {
4751 /* delete old one */
4752 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4755 addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4757 ifp->rt_priority, ifp->idev->dev,
4758 expires, flags, GFP_KERNEL);
4761 fib6_clean_expires(f6i);
4763 fib6_set_expires(f6i, expires);
4765 fib6_info_release(f6i);
4771 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
4772 struct ifa6_config *cfg)
4776 unsigned long timeout;
4777 bool was_managetempaddr;
4778 bool had_prefixroute;
4779 bool new_peer = false;
4783 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4786 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4787 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4790 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4791 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4793 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
4794 if (addrconf_finite_timeout(timeout)) {
4795 expires = jiffies_to_clock_t(timeout * HZ);
4796 cfg->valid_lft = timeout;
4797 flags = RTF_EXPIRES;
4801 cfg->ifa_flags |= IFA_F_PERMANENT;
4804 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
4805 if (addrconf_finite_timeout(timeout)) {
4807 cfg->ifa_flags |= IFA_F_DEPRECATED;
4808 cfg->preferred_lft = timeout;
4811 if (cfg->peer_pfx &&
4812 memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4813 if (!ipv6_addr_any(&ifp->peer_addr))
4814 cleanup_prefix_route(ifp, expires, true, true);
4818 spin_lock_bh(&ifp->lock);
4819 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4820 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4821 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4822 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4823 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4824 IFA_F_NOPREFIXROUTE);
4825 ifp->flags |= cfg->ifa_flags;
4826 ifp->tstamp = jiffies;
4827 ifp->valid_lft = cfg->valid_lft;
4828 ifp->prefered_lft = cfg->preferred_lft;
4829 ifp->ifa_proto = cfg->ifa_proto;
4831 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4832 ifp->rt_priority = cfg->rt_priority;
4835 ifp->peer_addr = *cfg->peer_pfx;
4837 spin_unlock_bh(&ifp->lock);
4838 if (!(ifp->flags&IFA_F_TENTATIVE))
4839 ipv6_ifa_notify(0, ifp);
4841 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4844 if (had_prefixroute)
4845 rc = modify_prefix_route(ifp, expires, flags, false);
4847 /* prefix route could have been deleted; if so restore it */
4848 if (rc == -ENOENT) {
4849 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4850 ifp->rt_priority, ifp->idev->dev,
4851 expires, flags, GFP_KERNEL);
4854 if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4855 rc = modify_prefix_route(ifp, expires, flags, true);
4857 if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4858 addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4859 ifp->rt_priority, ifp->idev->dev,
4860 expires, flags, GFP_KERNEL);
4862 } else if (had_prefixroute) {
4863 enum cleanup_prefix_rt_t action;
4864 unsigned long rt_expires;
4866 write_lock_bh(&ifp->idev->lock);
4867 action = check_cleanup_prefix_route(ifp, &rt_expires);
4868 write_unlock_bh(&ifp->idev->lock);
4870 if (action != CLEANUP_PREFIX_RT_NOP) {
4871 cleanup_prefix_route(ifp, rt_expires,
4872 action == CLEANUP_PREFIX_RT_DEL, false);
4876 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4877 if (was_managetempaddr &&
4878 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4880 cfg->preferred_lft = 0;
4882 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4883 cfg->preferred_lft, !was_managetempaddr,
4887 addrconf_verify_rtnl(net);
4893 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4894 struct netlink_ext_ack *extack)
4896 struct net *net = sock_net(skb->sk);
4897 struct ifaddrmsg *ifm;
4898 struct nlattr *tb[IFA_MAX+1];
4899 struct in6_addr *peer_pfx;
4900 struct inet6_ifaddr *ifa;
4901 struct net_device *dev;
4902 struct inet6_dev *idev;
4903 struct ifa6_config cfg;
4906 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4907 ifa_ipv6_policy, extack);
4911 memset(&cfg, 0, sizeof(cfg));
4913 ifm = nlmsg_data(nlh);
4914 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4918 cfg.peer_pfx = peer_pfx;
4919 cfg.plen = ifm->ifa_prefixlen;
4920 if (tb[IFA_RT_PRIORITY])
4921 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4924 cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
4926 cfg.valid_lft = INFINITY_LIFE_TIME;
4927 cfg.preferred_lft = INFINITY_LIFE_TIME;
4929 if (tb[IFA_CACHEINFO]) {
4930 struct ifa_cacheinfo *ci;
4932 ci = nla_data(tb[IFA_CACHEINFO]);
4933 cfg.valid_lft = ci->ifa_valid;
4934 cfg.preferred_lft = ci->ifa_prefered;
4937 dev = __dev_get_by_index(net, ifm->ifa_index);
4939 NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
4944 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4946 cfg.ifa_flags = ifm->ifa_flags;
4948 /* We ignore other flags so far. */
4949 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4950 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4951 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4953 idev = ipv6_find_idev(dev);
4955 return PTR_ERR(idev);
4957 if (!ipv6_allow_optimistic_dad(net, idev))
4958 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4960 if (cfg.ifa_flags & IFA_F_NODAD &&
4961 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4962 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4966 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
4969 * It would be best to check for !NLM_F_CREATE here but
4970 * userspace already relies on not having to provide this.
4972 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
4975 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4976 !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
4977 NL_SET_ERR_MSG_MOD(extack, "address already assigned");
4980 err = inet6_addr_modify(net, ifa, &cfg);
4988 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4989 u8 scope, int ifindex)
4991 struct ifaddrmsg *ifm;
4993 ifm = nlmsg_data(nlh);
4994 ifm->ifa_family = AF_INET6;
4995 ifm->ifa_prefixlen = prefixlen;
4996 ifm->ifa_flags = flags;
4997 ifm->ifa_scope = scope;
4998 ifm->ifa_index = ifindex;
5001 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
5002 unsigned long tstamp, u32 preferred, u32 valid)
5004 struct ifa_cacheinfo ci;
5006 ci.cstamp = cstamp_delta(cstamp);
5007 ci.tstamp = cstamp_delta(tstamp);
5008 ci.ifa_prefered = preferred;
5009 ci.ifa_valid = valid;
5011 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
5014 static inline int rt_scope(int ifa_scope)
5016 if (ifa_scope & IFA_HOST)
5017 return RT_SCOPE_HOST;
5018 else if (ifa_scope & IFA_LINK)
5019 return RT_SCOPE_LINK;
5020 else if (ifa_scope & IFA_SITE)
5021 return RT_SCOPE_SITE;
5023 return RT_SCOPE_UNIVERSE;
5026 static inline int inet6_ifaddr_msgsize(void)
5028 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
5029 + nla_total_size(16) /* IFA_LOCAL */
5030 + nla_total_size(16) /* IFA_ADDRESS */
5031 + nla_total_size(sizeof(struct ifa_cacheinfo))
5032 + nla_total_size(4) /* IFA_FLAGS */
5033 + nla_total_size(1) /* IFA_PROTO */
5034 + nla_total_size(4) /* IFA_RT_PRIORITY */;
5043 struct inet6_fill_args {
5050 enum addr_type_t type;
5053 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
5054 struct inet6_fill_args *args)
5056 struct nlmsghdr *nlh;
5057 u32 preferred, valid;
5059 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5060 sizeof(struct ifaddrmsg), args->flags);
5064 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5065 ifa->idev->dev->ifindex);
5067 if (args->netnsid >= 0 &&
5068 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
5071 spin_lock_bh(&ifa->lock);
5072 if (!((ifa->flags&IFA_F_PERMANENT) &&
5073 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
5074 preferred = ifa->prefered_lft;
5075 valid = ifa->valid_lft;
5076 if (preferred != INFINITY_LIFE_TIME) {
5077 long tval = (jiffies - ifa->tstamp)/HZ;
5078 if (preferred > tval)
5082 if (valid != INFINITY_LIFE_TIME) {
5090 preferred = INFINITY_LIFE_TIME;
5091 valid = INFINITY_LIFE_TIME;
5093 spin_unlock_bh(&ifa->lock);
5095 if (!ipv6_addr_any(&ifa->peer_addr)) {
5096 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5097 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5100 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5103 if (ifa->rt_priority &&
5104 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
5107 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
5110 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
5113 if (ifa->ifa_proto &&
5114 nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto))
5117 nlmsg_end(skb, nlh);
5121 nlmsg_cancel(skb, nlh);
5125 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
5126 struct inet6_fill_args *args)
5128 struct nlmsghdr *nlh;
5129 u8 scope = RT_SCOPE_UNIVERSE;
5130 int ifindex = ifmca->idev->dev->ifindex;
5132 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5133 scope = RT_SCOPE_SITE;
5135 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5136 sizeof(struct ifaddrmsg), args->flags);
5140 if (args->netnsid >= 0 &&
5141 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5142 nlmsg_cancel(skb, nlh);
5146 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5147 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
5148 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
5149 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5150 nlmsg_cancel(skb, nlh);
5154 nlmsg_end(skb, nlh);
5158 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
5159 struct inet6_fill_args *args)
5161 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5162 int ifindex = dev ? dev->ifindex : 1;
5163 struct nlmsghdr *nlh;
5164 u8 scope = RT_SCOPE_UNIVERSE;
5166 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5167 scope = RT_SCOPE_SITE;
5169 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5170 sizeof(struct ifaddrmsg), args->flags);
5174 if (args->netnsid >= 0 &&
5175 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5176 nlmsg_cancel(skb, nlh);
5180 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5181 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
5182 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
5183 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5184 nlmsg_cancel(skb, nlh);
5188 nlmsg_end(skb, nlh);
5192 /* called with rcu_read_lock() */
5193 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5194 struct netlink_callback *cb, int s_ip_idx,
5195 struct inet6_fill_args *fillargs)
5197 struct ifmcaddr6 *ifmca;
5198 struct ifacaddr6 *ifaca;
5202 read_lock_bh(&idev->lock);
5203 switch (fillargs->type) {
5204 case UNICAST_ADDR: {
5205 struct inet6_ifaddr *ifa;
5206 fillargs->event = RTM_NEWADDR;
5208 /* unicast address incl. temp addr */
5209 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5210 if (ip_idx < s_ip_idx)
5212 err = inet6_fill_ifaddr(skb, ifa, fillargs);
5215 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5221 case MULTICAST_ADDR:
5222 read_unlock_bh(&idev->lock);
5223 fillargs->event = RTM_GETMULTICAST;
5225 /* multicast address */
5226 for (ifmca = rtnl_dereference(idev->mc_list);
5228 ifmca = rtnl_dereference(ifmca->next), ip_idx++) {
5229 if (ip_idx < s_ip_idx)
5231 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5235 read_lock_bh(&idev->lock);
5238 fillargs->event = RTM_GETANYCAST;
5239 /* anycast address */
5240 for (ifaca = idev->ac_list; ifaca;
5241 ifaca = ifaca->aca_next, ip_idx++) {
5242 if (ip_idx < s_ip_idx)
5244 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5252 read_unlock_bh(&idev->lock);
5253 cb->args[2] = ip_idx;
5257 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5258 struct inet6_fill_args *fillargs,
5259 struct net **tgt_net, struct sock *sk,
5260 struct netlink_callback *cb)
5262 struct netlink_ext_ack *extack = cb->extack;
5263 struct nlattr *tb[IFA_MAX+1];
5264 struct ifaddrmsg *ifm;
5267 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5268 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5272 ifm = nlmsg_data(nlh);
5273 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5274 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5278 fillargs->ifindex = ifm->ifa_index;
5279 if (fillargs->ifindex) {
5280 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5281 fillargs->flags |= NLM_F_DUMP_FILTERED;
5284 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5285 ifa_ipv6_policy, extack);
5289 for (i = 0; i <= IFA_MAX; ++i) {
5293 if (i == IFA_TARGET_NETNSID) {
5296 fillargs->netnsid = nla_get_s32(tb[i]);
5297 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5299 fillargs->netnsid = -1;
5300 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5301 return PTR_ERR(net);
5305 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5313 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5314 enum addr_type_t type)
5316 const struct nlmsghdr *nlh = cb->nlh;
5317 struct inet6_fill_args fillargs = {
5318 .portid = NETLINK_CB(cb->skb).portid,
5319 .seq = cb->nlh->nlmsg_seq,
5320 .flags = NLM_F_MULTI,
5324 struct net *tgt_net = sock_net(skb->sk);
5325 int idx, s_idx, s_ip_idx;
5327 struct net_device *dev;
5328 struct inet6_dev *idev;
5329 struct hlist_head *head;
5333 s_idx = idx = cb->args[1];
5334 s_ip_idx = cb->args[2];
5336 if (cb->strict_check) {
5337 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5343 if (fillargs.ifindex) {
5344 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5349 idev = __in6_dev_get(dev);
5351 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5361 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
5362 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5364 head = &tgt_net->dev_index_head[h];
5365 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5368 if (h > s_h || idx > s_idx)
5370 idev = __in6_dev_get(dev);
5374 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
5386 if (fillargs.netnsid >= 0)
5389 return skb->len ? : err;
5392 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5394 enum addr_type_t type = UNICAST_ADDR;
5396 return inet6_dump_addr(skb, cb, type);
5399 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5401 enum addr_type_t type = MULTICAST_ADDR;
5403 return inet6_dump_addr(skb, cb, type);
5407 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5409 enum addr_type_t type = ANYCAST_ADDR;
5411 return inet6_dump_addr(skb, cb, type);
5414 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5415 const struct nlmsghdr *nlh,
5417 struct netlink_ext_ack *extack)
5419 struct ifaddrmsg *ifm;
5422 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5423 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5427 if (!netlink_strict_get_check(skb))
5428 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5429 ifa_ipv6_policy, extack);
5431 ifm = nlmsg_data(nlh);
5432 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5433 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5437 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5438 ifa_ipv6_policy, extack);
5442 for (i = 0; i <= IFA_MAX; i++) {
5447 case IFA_TARGET_NETNSID:
5452 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5460 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5461 struct netlink_ext_ack *extack)
5463 struct net *tgt_net = sock_net(in_skb->sk);
5464 struct inet6_fill_args fillargs = {
5465 .portid = NETLINK_CB(in_skb).portid,
5466 .seq = nlh->nlmsg_seq,
5467 .event = RTM_NEWADDR,
5471 struct ifaddrmsg *ifm;
5472 struct nlattr *tb[IFA_MAX+1];
5473 struct in6_addr *addr = NULL, *peer;
5474 struct net_device *dev = NULL;
5475 struct inet6_ifaddr *ifa;
5476 struct sk_buff *skb;
5479 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5483 if (tb[IFA_TARGET_NETNSID]) {
5484 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5486 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5488 if (IS_ERR(tgt_net))
5489 return PTR_ERR(tgt_net);
5492 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5496 ifm = nlmsg_data(nlh);
5498 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5500 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5502 err = -EADDRNOTAVAIL;
5506 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5512 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5514 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5515 WARN_ON(err == -EMSGSIZE);
5519 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5524 if (fillargs.netnsid >= 0)
5530 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5532 struct sk_buff *skb;
5533 struct net *net = dev_net(ifa->idev->dev);
5534 struct inet6_fill_args fillargs = {
5543 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5547 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5549 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5550 WARN_ON(err == -EMSGSIZE);
5554 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5558 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5561 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
5562 __s32 *array, int bytes)
5564 BUG_ON(bytes < (DEVCONF_MAX * 4));
5566 memset(array, 0, bytes);
5567 array[DEVCONF_FORWARDING] = cnf->forwarding;
5568 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5569 array[DEVCONF_MTU6] = cnf->mtu6;
5570 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5571 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5572 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5573 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5574 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
5575 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5576 jiffies_to_msecs(cnf->rtr_solicit_interval);
5577 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5578 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
5579 array[DEVCONF_RTR_SOLICIT_DELAY] =
5580 jiffies_to_msecs(cnf->rtr_solicit_delay);
5581 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5582 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5583 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5584 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5585 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
5586 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5587 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5588 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5589 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5590 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
5591 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
5592 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
5593 array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric;
5594 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5595 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5596 #ifdef CONFIG_IPV6_ROUTER_PREF
5597 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
5598 array[DEVCONF_RTR_PROBE_INTERVAL] =
5599 jiffies_to_msecs(cnf->rtr_probe_interval);
5600 #ifdef CONFIG_IPV6_ROUTE_INFO
5601 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
5602 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5605 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
5606 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
5607 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5608 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
5609 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
5611 #ifdef CONFIG_IPV6_MROUTE
5612 array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
5614 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
5615 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5616 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
5617 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5618 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5619 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5620 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
5621 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
5622 /* we omit DEVCONF_STABLE_SECRET for now */
5623 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5624 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
5625 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5626 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5627 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5628 #ifdef CONFIG_IPV6_SEG6_HMAC
5629 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5631 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5632 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5633 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
5634 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
5635 array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
5636 array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled;
5637 array[DEVCONF_IOAM6_ID] = cnf->ioam6_id;
5638 array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide;
5639 array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier;
5640 array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na;
5641 array[DEVCONF_ACCEPT_RA_MIN_LFT] = cnf->accept_ra_min_lft;
5644 static inline size_t inet6_ifla6_size(void)
5646 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5647 + nla_total_size(sizeof(struct ifla_cacheinfo))
5648 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5649 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5650 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5651 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5652 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5653 + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5657 static inline size_t inet6_if_nlmsg_size(void)
5659 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5660 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5661 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5662 + nla_total_size(4) /* IFLA_MTU */
5663 + nla_total_size(4) /* IFLA_LINK */
5664 + nla_total_size(1) /* IFLA_OPERSTATE */
5665 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5668 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5672 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5675 /* Use put_unaligned() because stats may not be aligned for u64. */
5676 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5677 for (i = 1; i < ICMP6_MIB_MAX; i++)
5678 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5680 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5683 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5684 int bytes, size_t syncpoff)
5687 u64 buff[IPSTATS_MIB_MAX];
5688 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5692 memset(buff, 0, sizeof(buff));
5693 buff[0] = IPSTATS_MIB_MAX;
5695 for_each_possible_cpu(c) {
5696 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5697 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5700 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5701 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5704 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5708 case IFLA_INET6_STATS:
5709 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5710 offsetof(struct ipstats_mib, syncp));
5712 case IFLA_INET6_ICMP6STATS:
5713 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5718 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5719 u32 ext_filter_mask)
5722 struct ifla_cacheinfo ci;
5724 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5725 goto nla_put_failure;
5726 ci.max_reasm_len = IPV6_MAXPLEN;
5727 ci.tstamp = cstamp_delta(idev->tstamp);
5728 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5729 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5730 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5731 goto nla_put_failure;
5732 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5734 goto nla_put_failure;
5735 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5737 /* XXX - MC not implemented */
5739 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5742 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5744 goto nla_put_failure;
5745 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5747 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5749 goto nla_put_failure;
5750 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5752 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5754 goto nla_put_failure;
5755 read_lock_bh(&idev->lock);
5756 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5757 read_unlock_bh(&idev->lock);
5759 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5760 goto nla_put_failure;
5763 nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu))
5764 goto nla_put_failure;
5772 static size_t inet6_get_link_af_size(const struct net_device *dev,
5773 u32 ext_filter_mask)
5775 if (!__in6_dev_get(dev))
5778 return inet6_ifla6_size();
5781 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5782 u32 ext_filter_mask)
5784 struct inet6_dev *idev = __in6_dev_get(dev);
5789 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5795 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
5796 struct netlink_ext_ack *extack)
5798 struct inet6_ifaddr *ifp;
5799 struct net_device *dev = idev->dev;
5800 bool clear_token, update_rs = false;
5801 struct in6_addr ll_addr;
5808 if (dev->flags & IFF_LOOPBACK) {
5809 NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5813 if (dev->flags & IFF_NOARP) {
5814 NL_SET_ERR_MSG_MOD(extack,
5815 "Device does not do neighbour discovery");
5819 if (!ipv6_accept_ra(idev)) {
5820 NL_SET_ERR_MSG_MOD(extack,
5821 "Router advertisement is disabled on device");
5825 if (idev->cnf.rtr_solicits == 0) {
5826 NL_SET_ERR_MSG(extack,
5827 "Router solicitation is disabled on device");
5831 write_lock_bh(&idev->lock);
5833 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5834 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5836 write_unlock_bh(&idev->lock);
5838 clear_token = ipv6_addr_any(token);
5842 if (!idev->dead && (idev->if_flags & IF_READY) &&
5843 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5844 IFA_F_OPTIMISTIC)) {
5845 /* If we're not ready, then normal ifup will take care
5846 * of this. Otherwise, we need to request our rs here.
5848 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5853 write_lock_bh(&idev->lock);
5856 idev->if_flags |= IF_RS_SENT;
5857 idev->rs_interval = rfc3315_s14_backoff_init(
5858 idev->cnf.rtr_solicit_interval);
5859 idev->rs_probes = 1;
5860 addrconf_mod_rs_timer(idev, idev->rs_interval);
5863 /* Well, that's kinda nasty ... */
5864 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5865 spin_lock(&ifp->lock);
5866 if (ifp->tokenized) {
5868 ifp->prefered_lft = 0;
5870 spin_unlock(&ifp->lock);
5873 write_unlock_bh(&idev->lock);
5874 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5875 addrconf_verify_rtnl(dev_net(dev));
5879 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5880 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5881 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5882 [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT,
5884 "IFLA_INET6_RA_MTU can not be set" },
5887 static int check_addr_gen_mode(int mode)
5889 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5890 mode != IN6_ADDR_GEN_MODE_NONE &&
5891 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5892 mode != IN6_ADDR_GEN_MODE_RANDOM)
5897 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5900 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5901 !idev->cnf.stable_secret.initialized &&
5902 !net->ipv6.devconf_dflt->stable_secret.initialized)
5907 static int inet6_validate_link_af(const struct net_device *dev,
5908 const struct nlattr *nla,
5909 struct netlink_ext_ack *extack)
5911 struct nlattr *tb[IFLA_INET6_MAX + 1];
5912 struct inet6_dev *idev = NULL;
5916 idev = __in6_dev_get(dev);
5918 return -EAFNOSUPPORT;
5921 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5922 inet6_af_policy, extack);
5926 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
5929 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5930 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5932 if (check_addr_gen_mode(mode) < 0)
5934 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
5941 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
5942 struct netlink_ext_ack *extack)
5944 struct inet6_dev *idev = __in6_dev_get(dev);
5945 struct nlattr *tb[IFLA_INET6_MAX + 1];
5949 return -EAFNOSUPPORT;
5951 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5954 if (tb[IFLA_INET6_TOKEN]) {
5955 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
5961 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5962 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5964 idev->cnf.addr_gen_mode = mode;
5970 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5971 u32 portid, u32 seq, int event, unsigned int flags)
5973 struct net_device *dev = idev->dev;
5974 struct ifinfomsg *hdr;
5975 struct nlmsghdr *nlh;
5978 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5982 hdr = nlmsg_data(nlh);
5983 hdr->ifi_family = AF_INET6;
5985 hdr->ifi_type = dev->type;
5986 hdr->ifi_index = dev->ifindex;
5987 hdr->ifi_flags = dev_get_flags(dev);
5988 hdr->ifi_change = 0;
5990 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5992 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5993 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5994 (dev->ifindex != dev_get_iflink(dev) &&
5995 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5996 nla_put_u8(skb, IFLA_OPERSTATE,
5997 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5998 goto nla_put_failure;
5999 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
6001 goto nla_put_failure;
6003 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
6004 goto nla_put_failure;
6006 nla_nest_end(skb, protoinfo);
6007 nlmsg_end(skb, nlh);
6011 nlmsg_cancel(skb, nlh);
6015 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
6016 struct netlink_ext_ack *extack)
6018 struct ifinfomsg *ifm;
6020 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
6021 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
6025 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
6026 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
6030 ifm = nlmsg_data(nlh);
6031 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
6032 ifm->ifi_change || ifm->ifi_index) {
6033 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6040 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6042 struct net *net = sock_net(skb->sk);
6045 struct net_device *dev;
6046 struct inet6_dev *idev;
6047 struct hlist_head *head;
6049 /* only requests using strict checking can pass data to
6050 * influence the dump
6052 if (cb->strict_check) {
6053 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6060 s_idx = cb->args[1];
6063 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6065 head = &net->dev_index_head[h];
6066 hlist_for_each_entry_rcu(dev, head, index_hlist) {
6069 idev = __in6_dev_get(dev);
6072 if (inet6_fill_ifinfo(skb, idev,
6073 NETLINK_CB(cb->skb).portid,
6075 RTM_NEWLINK, NLM_F_MULTI) < 0)
6089 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
6091 struct sk_buff *skb;
6092 struct net *net = dev_net(idev->dev);
6095 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
6099 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
6101 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6102 WARN_ON(err == -EMSGSIZE);
6106 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
6110 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
6113 static inline size_t inet6_prefix_nlmsg_size(void)
6115 return NLMSG_ALIGN(sizeof(struct prefixmsg))
6116 + nla_total_size(sizeof(struct in6_addr))
6117 + nla_total_size(sizeof(struct prefix_cacheinfo));
6120 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
6121 struct prefix_info *pinfo, u32 portid, u32 seq,
6122 int event, unsigned int flags)
6124 struct prefixmsg *pmsg;
6125 struct nlmsghdr *nlh;
6126 struct prefix_cacheinfo ci;
6128 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
6132 pmsg = nlmsg_data(nlh);
6133 pmsg->prefix_family = AF_INET6;
6134 pmsg->prefix_pad1 = 0;
6135 pmsg->prefix_pad2 = 0;
6136 pmsg->prefix_ifindex = idev->dev->ifindex;
6137 pmsg->prefix_len = pinfo->prefix_len;
6138 pmsg->prefix_type = pinfo->type;
6139 pmsg->prefix_pad3 = 0;
6140 pmsg->prefix_flags = 0;
6142 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
6143 if (pinfo->autoconf)
6144 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
6146 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6147 goto nla_put_failure;
6148 ci.preferred_time = ntohl(pinfo->prefered);
6149 ci.valid_time = ntohl(pinfo->valid);
6150 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6151 goto nla_put_failure;
6152 nlmsg_end(skb, nlh);
6156 nlmsg_cancel(skb, nlh);
6160 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
6161 struct prefix_info *pinfo)
6163 struct sk_buff *skb;
6164 struct net *net = dev_net(idev->dev);
6167 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
6171 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
6173 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6174 WARN_ON(err == -EMSGSIZE);
6178 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
6182 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
6185 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6187 struct net *net = dev_net(ifp->idev->dev);
6192 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
6197 * If the address was optimistic we inserted the route at the
6198 * start of our DAD process, so we don't need to do it again.
6199 * If the device was taken down in the middle of the DAD
6200 * cycle there is a race where we could get here without a
6201 * host route, so nothing to insert. That will be fixed when
6202 * the device is brought up.
6204 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6205 ip6_ins_rt(net, ifp->rt);
6206 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6207 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6208 &ifp->addr, ifp->idev->dev->name);
6211 if (ifp->idev->cnf.forwarding)
6212 addrconf_join_anycast(ifp);
6213 if (!ipv6_addr_any(&ifp->peer_addr))
6214 addrconf_prefix_route(&ifp->peer_addr, 128,
6215 ifp->rt_priority, ifp->idev->dev,
6219 if (ifp->idev->cnf.forwarding)
6220 addrconf_leave_anycast(ifp);
6221 addrconf_leave_solict(ifp->idev, &ifp->addr);
6222 if (!ipv6_addr_any(&ifp->peer_addr)) {
6223 struct fib6_info *rt;
6225 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
6226 ifp->idev->dev, 0, 0,
6229 ip6_del_rt(net, rt, false);
6232 ip6_del_rt(net, ifp->rt, false);
6235 rt_genid_bump_ipv6(net);
6238 atomic_inc(&net->ipv6.dev_addr_genid);
6241 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6243 if (likely(ifp->idev->dead == 0))
6244 __ipv6_ifa_notify(event, ifp);
6247 #ifdef CONFIG_SYSCTL
6249 static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
6250 void *buffer, size_t *lenp, loff_t *ppos)
6252 int *valp = ctl->data;
6255 struct ctl_table lctl;
6259 * ctl->data points to idev->cnf.forwarding, we should
6260 * not modify it until we get the rtnl lock.
6265 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6268 ret = addrconf_fixup_forwarding(ctl, valp, val);
6274 static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6275 void *buffer, size_t *lenp, loff_t *ppos)
6277 struct inet6_dev *idev = ctl->extra1;
6278 int min_mtu = IPV6_MIN_MTU;
6279 struct ctl_table lctl;
6282 lctl.extra1 = &min_mtu;
6283 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6285 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6288 static void dev_disable_change(struct inet6_dev *idev)
6290 struct netdev_notifier_info info;
6292 if (!idev || !idev->dev)
6295 netdev_notifier_info_init(&info, idev->dev);
6296 if (idev->cnf.disable_ipv6)
6297 addrconf_notify(NULL, NETDEV_DOWN, &info);
6299 addrconf_notify(NULL, NETDEV_UP, &info);
6302 static void addrconf_disable_change(struct net *net, __s32 newf)
6304 struct net_device *dev;
6305 struct inet6_dev *idev;
6307 for_each_netdev(net, dev) {
6308 idev = __in6_dev_get(dev);
6310 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6311 idev->cnf.disable_ipv6 = newf;
6313 dev_disable_change(idev);
6318 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
6323 if (!rtnl_trylock())
6324 return restart_syscall();
6326 net = (struct net *)table->extra2;
6330 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6335 if (p == &net->ipv6.devconf_all->disable_ipv6) {
6336 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6337 addrconf_disable_change(net, newf);
6338 } else if ((!newf) ^ (!old))
6339 dev_disable_change((struct inet6_dev *)table->extra1);
6345 static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
6346 void *buffer, size_t *lenp, loff_t *ppos)
6348 int *valp = ctl->data;
6351 struct ctl_table lctl;
6355 * ctl->data points to idev->cnf.disable_ipv6, we should
6356 * not modify it until we get the rtnl lock.
6361 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6364 ret = addrconf_disable_ipv6(ctl, valp, val);
6370 static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6371 void *buffer, size_t *lenp, loff_t *ppos)
6373 int *valp = ctl->data;
6378 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6381 if (write && old != new) {
6382 struct net *net = ctl->extra2;
6384 if (!rtnl_trylock())
6385 return restart_syscall();
6387 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
6388 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6389 NETCONFA_PROXY_NEIGH,
6390 NETCONFA_IFINDEX_DEFAULT,
6391 net->ipv6.devconf_dflt);
6392 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
6393 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6394 NETCONFA_PROXY_NEIGH,
6395 NETCONFA_IFINDEX_ALL,
6396 net->ipv6.devconf_all);
6398 struct inet6_dev *idev = ctl->extra1;
6400 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6401 NETCONFA_PROXY_NEIGH,
6411 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6412 void *buffer, size_t *lenp,
6417 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6418 struct net *net = (struct net *)ctl->extra2;
6419 struct ctl_table tmp = {
6421 .maxlen = sizeof(new_val),
6425 if (!rtnl_trylock())
6426 return restart_syscall();
6428 new_val = *((u32 *)ctl->data);
6430 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6435 if (check_addr_gen_mode(new_val) < 0) {
6441 if (check_stable_privacy(idev, net, new_val) < 0) {
6446 if (idev->cnf.addr_gen_mode != new_val) {
6447 idev->cnf.addr_gen_mode = new_val;
6448 addrconf_init_auto_addrs(idev->dev);
6450 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6451 struct net_device *dev;
6453 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6454 for_each_netdev(net, dev) {
6455 idev = __in6_dev_get(dev);
6457 idev->cnf.addr_gen_mode != new_val) {
6458 idev->cnf.addr_gen_mode = new_val;
6459 addrconf_init_auto_addrs(idev->dev);
6464 *((u32 *)ctl->data) = new_val;
6473 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6474 void *buffer, size_t *lenp,
6478 struct in6_addr addr;
6479 char str[IPV6_MAX_STRLEN];
6480 struct ctl_table lctl = *ctl;
6481 struct net *net = ctl->extra2;
6482 struct ipv6_stable_secret *secret = ctl->data;
6484 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6487 lctl.maxlen = IPV6_MAX_STRLEN;
6490 if (!rtnl_trylock())
6491 return restart_syscall();
6493 if (!write && !secret->initialized) {
6498 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6499 if (err >= sizeof(str)) {
6504 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6508 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6513 secret->initialized = true;
6514 secret->secret = addr;
6516 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6517 struct net_device *dev;
6519 for_each_netdev(net, dev) {
6520 struct inet6_dev *idev = __in6_dev_get(dev);
6523 idev->cnf.addr_gen_mode =
6524 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6528 struct inet6_dev *idev = ctl->extra1;
6530 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6540 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6541 int write, void *buffer,
6545 int *valp = ctl->data;
6548 struct ctl_table lctl;
6551 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6552 * we should not modify it until we get the rtnl lock.
6557 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6560 ret = addrconf_fixup_linkdown(ctl, valp, val);
6567 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6571 rt->dst.flags |= DST_NOPOLICY;
6573 rt->dst.flags &= ~DST_NOPOLICY;
6578 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6580 struct inet6_ifaddr *ifa;
6582 read_lock_bh(&idev->lock);
6583 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6584 spin_lock(&ifa->lock);
6586 /* host routes only use builtin fib6_nh */
6587 struct fib6_nh *nh = ifa->rt->fib6_nh;
6591 ifa->rt->dst_nopolicy = val ? true : false;
6592 if (nh->rt6i_pcpu) {
6593 for_each_possible_cpu(cpu) {
6594 struct rt6_info **rtp;
6596 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6597 addrconf_set_nopolicy(*rtp, val);
6602 spin_unlock(&ifa->lock);
6604 read_unlock_bh(&idev->lock);
6608 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6610 struct inet6_dev *idev;
6613 if (!rtnl_trylock())
6614 return restart_syscall();
6618 net = (struct net *)ctl->extra2;
6619 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6624 if (valp == &net->ipv6.devconf_all->disable_policy) {
6625 struct net_device *dev;
6627 for_each_netdev(net, dev) {
6628 idev = __in6_dev_get(dev);
6630 addrconf_disable_policy_idev(idev, val);
6633 idev = (struct inet6_dev *)ctl->extra1;
6634 addrconf_disable_policy_idev(idev, val);
6641 static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6642 void *buffer, size_t *lenp, loff_t *ppos)
6644 int *valp = ctl->data;
6647 struct ctl_table lctl;
6652 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6654 if (write && (*valp != val))
6655 ret = addrconf_disable_policy(ctl, valp, val);
6663 static int minus_one = -1;
6664 static const int two_five_five = 255;
6665 static u32 ioam6_if_id_max = U16_MAX;
6667 static const struct ctl_table addrconf_sysctl[] = {
6669 .procname = "forwarding",
6670 .data = &ipv6_devconf.forwarding,
6671 .maxlen = sizeof(int),
6673 .proc_handler = addrconf_sysctl_forward,
6676 .procname = "hop_limit",
6677 .data = &ipv6_devconf.hop_limit,
6678 .maxlen = sizeof(int),
6680 .proc_handler = proc_dointvec_minmax,
6681 .extra1 = (void *)SYSCTL_ONE,
6682 .extra2 = (void *)&two_five_five,
6686 .data = &ipv6_devconf.mtu6,
6687 .maxlen = sizeof(int),
6689 .proc_handler = addrconf_sysctl_mtu,
6692 .procname = "accept_ra",
6693 .data = &ipv6_devconf.accept_ra,
6694 .maxlen = sizeof(int),
6696 .proc_handler = proc_dointvec,
6699 .procname = "accept_redirects",
6700 .data = &ipv6_devconf.accept_redirects,
6701 .maxlen = sizeof(int),
6703 .proc_handler = proc_dointvec,
6706 .procname = "autoconf",
6707 .data = &ipv6_devconf.autoconf,
6708 .maxlen = sizeof(int),
6710 .proc_handler = proc_dointvec,
6713 .procname = "dad_transmits",
6714 .data = &ipv6_devconf.dad_transmits,
6715 .maxlen = sizeof(int),
6717 .proc_handler = proc_dointvec,
6720 .procname = "router_solicitations",
6721 .data = &ipv6_devconf.rtr_solicits,
6722 .maxlen = sizeof(int),
6724 .proc_handler = proc_dointvec_minmax,
6725 .extra1 = &minus_one,
6728 .procname = "router_solicitation_interval",
6729 .data = &ipv6_devconf.rtr_solicit_interval,
6730 .maxlen = sizeof(int),
6732 .proc_handler = proc_dointvec_jiffies,
6735 .procname = "router_solicitation_max_interval",
6736 .data = &ipv6_devconf.rtr_solicit_max_interval,
6737 .maxlen = sizeof(int),
6739 .proc_handler = proc_dointvec_jiffies,
6742 .procname = "router_solicitation_delay",
6743 .data = &ipv6_devconf.rtr_solicit_delay,
6744 .maxlen = sizeof(int),
6746 .proc_handler = proc_dointvec_jiffies,
6749 .procname = "force_mld_version",
6750 .data = &ipv6_devconf.force_mld_version,
6751 .maxlen = sizeof(int),
6753 .proc_handler = proc_dointvec,
6756 .procname = "mldv1_unsolicited_report_interval",
6758 &ipv6_devconf.mldv1_unsolicited_report_interval,
6759 .maxlen = sizeof(int),
6761 .proc_handler = proc_dointvec_ms_jiffies,
6764 .procname = "mldv2_unsolicited_report_interval",
6766 &ipv6_devconf.mldv2_unsolicited_report_interval,
6767 .maxlen = sizeof(int),
6769 .proc_handler = proc_dointvec_ms_jiffies,
6772 .procname = "use_tempaddr",
6773 .data = &ipv6_devconf.use_tempaddr,
6774 .maxlen = sizeof(int),
6776 .proc_handler = proc_dointvec,
6779 .procname = "temp_valid_lft",
6780 .data = &ipv6_devconf.temp_valid_lft,
6781 .maxlen = sizeof(int),
6783 .proc_handler = proc_dointvec,
6786 .procname = "temp_prefered_lft",
6787 .data = &ipv6_devconf.temp_prefered_lft,
6788 .maxlen = sizeof(int),
6790 .proc_handler = proc_dointvec,
6793 .procname = "regen_max_retry",
6794 .data = &ipv6_devconf.regen_max_retry,
6795 .maxlen = sizeof(int),
6797 .proc_handler = proc_dointvec,
6800 .procname = "max_desync_factor",
6801 .data = &ipv6_devconf.max_desync_factor,
6802 .maxlen = sizeof(int),
6804 .proc_handler = proc_dointvec,
6807 .procname = "max_addresses",
6808 .data = &ipv6_devconf.max_addresses,
6809 .maxlen = sizeof(int),
6811 .proc_handler = proc_dointvec,
6814 .procname = "accept_ra_defrtr",
6815 .data = &ipv6_devconf.accept_ra_defrtr,
6816 .maxlen = sizeof(int),
6818 .proc_handler = proc_dointvec,
6821 .procname = "ra_defrtr_metric",
6822 .data = &ipv6_devconf.ra_defrtr_metric,
6823 .maxlen = sizeof(u32),
6825 .proc_handler = proc_douintvec_minmax,
6826 .extra1 = (void *)SYSCTL_ONE,
6829 .procname = "accept_ra_min_hop_limit",
6830 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6831 .maxlen = sizeof(int),
6833 .proc_handler = proc_dointvec,
6836 .procname = "accept_ra_min_lft",
6837 .data = &ipv6_devconf.accept_ra_min_lft,
6838 .maxlen = sizeof(int),
6840 .proc_handler = proc_dointvec,
6843 .procname = "accept_ra_pinfo",
6844 .data = &ipv6_devconf.accept_ra_pinfo,
6845 .maxlen = sizeof(int),
6847 .proc_handler = proc_dointvec,
6849 #ifdef CONFIG_IPV6_ROUTER_PREF
6851 .procname = "accept_ra_rtr_pref",
6852 .data = &ipv6_devconf.accept_ra_rtr_pref,
6853 .maxlen = sizeof(int),
6855 .proc_handler = proc_dointvec,
6858 .procname = "router_probe_interval",
6859 .data = &ipv6_devconf.rtr_probe_interval,
6860 .maxlen = sizeof(int),
6862 .proc_handler = proc_dointvec_jiffies,
6864 #ifdef CONFIG_IPV6_ROUTE_INFO
6866 .procname = "accept_ra_rt_info_min_plen",
6867 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6868 .maxlen = sizeof(int),
6870 .proc_handler = proc_dointvec,
6873 .procname = "accept_ra_rt_info_max_plen",
6874 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6875 .maxlen = sizeof(int),
6877 .proc_handler = proc_dointvec,
6882 .procname = "proxy_ndp",
6883 .data = &ipv6_devconf.proxy_ndp,
6884 .maxlen = sizeof(int),
6886 .proc_handler = addrconf_sysctl_proxy_ndp,
6889 .procname = "accept_source_route",
6890 .data = &ipv6_devconf.accept_source_route,
6891 .maxlen = sizeof(int),
6893 .proc_handler = proc_dointvec,
6895 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6897 .procname = "optimistic_dad",
6898 .data = &ipv6_devconf.optimistic_dad,
6899 .maxlen = sizeof(int),
6901 .proc_handler = proc_dointvec,
6904 .procname = "use_optimistic",
6905 .data = &ipv6_devconf.use_optimistic,
6906 .maxlen = sizeof(int),
6908 .proc_handler = proc_dointvec,
6911 #ifdef CONFIG_IPV6_MROUTE
6913 .procname = "mc_forwarding",
6914 .data = &ipv6_devconf.mc_forwarding,
6915 .maxlen = sizeof(int),
6917 .proc_handler = proc_dointvec,
6921 .procname = "disable_ipv6",
6922 .data = &ipv6_devconf.disable_ipv6,
6923 .maxlen = sizeof(int),
6925 .proc_handler = addrconf_sysctl_disable,
6928 .procname = "accept_dad",
6929 .data = &ipv6_devconf.accept_dad,
6930 .maxlen = sizeof(int),
6932 .proc_handler = proc_dointvec,
6935 .procname = "force_tllao",
6936 .data = &ipv6_devconf.force_tllao,
6937 .maxlen = sizeof(int),
6939 .proc_handler = proc_dointvec
6942 .procname = "ndisc_notify",
6943 .data = &ipv6_devconf.ndisc_notify,
6944 .maxlen = sizeof(int),
6946 .proc_handler = proc_dointvec
6949 .procname = "suppress_frag_ndisc",
6950 .data = &ipv6_devconf.suppress_frag_ndisc,
6951 .maxlen = sizeof(int),
6953 .proc_handler = proc_dointvec
6956 .procname = "accept_ra_from_local",
6957 .data = &ipv6_devconf.accept_ra_from_local,
6958 .maxlen = sizeof(int),
6960 .proc_handler = proc_dointvec,
6963 .procname = "accept_ra_mtu",
6964 .data = &ipv6_devconf.accept_ra_mtu,
6965 .maxlen = sizeof(int),
6967 .proc_handler = proc_dointvec,
6970 .procname = "stable_secret",
6971 .data = &ipv6_devconf.stable_secret,
6972 .maxlen = IPV6_MAX_STRLEN,
6974 .proc_handler = addrconf_sysctl_stable_secret,
6977 .procname = "use_oif_addrs_only",
6978 .data = &ipv6_devconf.use_oif_addrs_only,
6979 .maxlen = sizeof(int),
6981 .proc_handler = proc_dointvec,
6984 .procname = "ignore_routes_with_linkdown",
6985 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6986 .maxlen = sizeof(int),
6988 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6991 .procname = "drop_unicast_in_l2_multicast",
6992 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6993 .maxlen = sizeof(int),
6995 .proc_handler = proc_dointvec,
6998 .procname = "drop_unsolicited_na",
6999 .data = &ipv6_devconf.drop_unsolicited_na,
7000 .maxlen = sizeof(int),
7002 .proc_handler = proc_dointvec,
7005 .procname = "keep_addr_on_down",
7006 .data = &ipv6_devconf.keep_addr_on_down,
7007 .maxlen = sizeof(int),
7009 .proc_handler = proc_dointvec,
7013 .procname = "seg6_enabled",
7014 .data = &ipv6_devconf.seg6_enabled,
7015 .maxlen = sizeof(int),
7017 .proc_handler = proc_dointvec,
7019 #ifdef CONFIG_IPV6_SEG6_HMAC
7021 .procname = "seg6_require_hmac",
7022 .data = &ipv6_devconf.seg6_require_hmac,
7023 .maxlen = sizeof(int),
7025 .proc_handler = proc_dointvec,
7029 .procname = "enhanced_dad",
7030 .data = &ipv6_devconf.enhanced_dad,
7031 .maxlen = sizeof(int),
7033 .proc_handler = proc_dointvec,
7036 .procname = "addr_gen_mode",
7037 .data = &ipv6_devconf.addr_gen_mode,
7038 .maxlen = sizeof(int),
7040 .proc_handler = addrconf_sysctl_addr_gen_mode,
7043 .procname = "disable_policy",
7044 .data = &ipv6_devconf.disable_policy,
7045 .maxlen = sizeof(int),
7047 .proc_handler = addrconf_sysctl_disable_policy,
7050 .procname = "ndisc_tclass",
7051 .data = &ipv6_devconf.ndisc_tclass,
7052 .maxlen = sizeof(int),
7054 .proc_handler = proc_dointvec_minmax,
7055 .extra1 = (void *)SYSCTL_ZERO,
7056 .extra2 = (void *)&two_five_five,
7059 .procname = "rpl_seg_enabled",
7060 .data = &ipv6_devconf.rpl_seg_enabled,
7061 .maxlen = sizeof(int),
7063 .proc_handler = proc_dointvec,
7066 .procname = "ioam6_enabled",
7067 .data = &ipv6_devconf.ioam6_enabled,
7068 .maxlen = sizeof(u8),
7070 .proc_handler = proc_dou8vec_minmax,
7071 .extra1 = (void *)SYSCTL_ZERO,
7072 .extra2 = (void *)SYSCTL_ONE,
7075 .procname = "ioam6_id",
7076 .data = &ipv6_devconf.ioam6_id,
7077 .maxlen = sizeof(u32),
7079 .proc_handler = proc_douintvec_minmax,
7080 .extra1 = (void *)SYSCTL_ZERO,
7081 .extra2 = (void *)&ioam6_if_id_max,
7084 .procname = "ioam6_id_wide",
7085 .data = &ipv6_devconf.ioam6_id_wide,
7086 .maxlen = sizeof(u32),
7088 .proc_handler = proc_douintvec,
7091 .procname = "ndisc_evict_nocarrier",
7092 .data = &ipv6_devconf.ndisc_evict_nocarrier,
7093 .maxlen = sizeof(u8),
7095 .proc_handler = proc_dou8vec_minmax,
7096 .extra1 = (void *)SYSCTL_ZERO,
7097 .extra2 = (void *)SYSCTL_ONE,
7100 .procname = "accept_untracked_na",
7101 .data = &ipv6_devconf.accept_untracked_na,
7102 .maxlen = sizeof(int),
7104 .proc_handler = proc_dointvec_minmax,
7105 .extra1 = SYSCTL_ZERO,
7106 .extra2 = SYSCTL_TWO,
7113 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7114 struct inet6_dev *idev, struct ipv6_devconf *p)
7117 struct ctl_table *table;
7118 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
7120 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7124 for (i = 0; table[i].data; i++) {
7125 table[i].data += (char *)p - (char *)&ipv6_devconf;
7126 /* If one of these is already set, then it is not safe to
7127 * overwrite either of them: this makes proc_dointvec_minmax
7130 if (!table[i].extra1 && !table[i].extra2) {
7131 table[i].extra1 = idev; /* embedded; no ref */
7132 table[i].extra2 = net;
7136 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
7138 p->sysctl_header = register_net_sysctl_sz(net, path, table,
7139 ARRAY_SIZE(addrconf_sysctl));
7140 if (!p->sysctl_header)
7143 if (!strcmp(dev_name, "all"))
7144 ifindex = NETCONFA_IFINDEX_ALL;
7145 else if (!strcmp(dev_name, "default"))
7146 ifindex = NETCONFA_IFINDEX_DEFAULT;
7148 ifindex = idev->dev->ifindex;
7149 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
7159 static void __addrconf_sysctl_unregister(struct net *net,
7160 struct ipv6_devconf *p, int ifindex)
7162 struct ctl_table *table;
7164 if (!p->sysctl_header)
7167 table = p->sysctl_header->ctl_table_arg;
7168 unregister_net_sysctl_table(p->sysctl_header);
7169 p->sysctl_header = NULL;
7172 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
7175 static int addrconf_sysctl_register(struct inet6_dev *idev)
7179 if (!sysctl_dev_name_is_allowed(idev->dev->name))
7182 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7183 &ndisc_ifinfo_sysctl_change);
7186 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7189 neigh_sysctl_unregister(idev->nd_parms);
7194 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7196 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
7197 idev->dev->ifindex);
7198 neigh_sysctl_unregister(idev->nd_parms);
7204 static int __net_init addrconf_init_net(struct net *net)
7207 struct ipv6_devconf *all, *dflt;
7209 spin_lock_init(&net->ipv6.addrconf_hash_lock);
7210 INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
7211 net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
7212 sizeof(struct hlist_head),
7214 if (!net->ipv6.inet6_addr_lst)
7215 goto err_alloc_addr;
7217 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
7221 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
7223 goto err_alloc_dflt;
7225 if (!net_eq(net, &init_net)) {
7226 switch (net_inherit_devconf()) {
7227 case 1: /* copy from init_net */
7228 memcpy(all, init_net.ipv6.devconf_all,
7229 sizeof(ipv6_devconf));
7230 memcpy(dflt, init_net.ipv6.devconf_dflt,
7231 sizeof(ipv6_devconf_dflt));
7233 case 3: /* copy from the current netns */
7234 memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
7235 sizeof(ipv6_devconf));
7237 current->nsproxy->net_ns->ipv6.devconf_dflt,
7238 sizeof(ipv6_devconf_dflt));
7242 /* use compiled values */
7247 /* these will be inherited by all namespaces */
7248 dflt->autoconf = ipv6_defaults.autoconf;
7249 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7251 dflt->stable_secret.initialized = false;
7252 all->stable_secret.initialized = false;
7254 net->ipv6.devconf_all = all;
7255 net->ipv6.devconf_dflt = dflt;
7257 #ifdef CONFIG_SYSCTL
7258 err = __addrconf_sysctl_register(net, "all", NULL, all);
7262 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7268 #ifdef CONFIG_SYSCTL
7270 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7273 net->ipv6.devconf_dflt = NULL;
7277 net->ipv6.devconf_all = NULL;
7279 kfree(net->ipv6.inet6_addr_lst);
7284 static void __net_exit addrconf_exit_net(struct net *net)
7288 #ifdef CONFIG_SYSCTL
7289 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7290 NETCONFA_IFINDEX_DEFAULT);
7291 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7292 NETCONFA_IFINDEX_ALL);
7294 kfree(net->ipv6.devconf_dflt);
7295 net->ipv6.devconf_dflt = NULL;
7296 kfree(net->ipv6.devconf_all);
7297 net->ipv6.devconf_all = NULL;
7299 cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
7301 * Check hash table, then free it.
7303 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7304 WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7306 kfree(net->ipv6.inet6_addr_lst);
7307 net->ipv6.inet6_addr_lst = NULL;
7310 static struct pernet_operations addrconf_ops = {
7311 .init = addrconf_init_net,
7312 .exit = addrconf_exit_net,
7315 static struct rtnl_af_ops inet6_ops __read_mostly = {
7317 .fill_link_af = inet6_fill_link_af,
7318 .get_link_af_size = inet6_get_link_af_size,
7319 .validate_link_af = inet6_validate_link_af,
7320 .set_link_af = inet6_set_link_af,
7324 * Init / cleanup code
7327 int __init addrconf_init(void)
7329 struct inet6_dev *idev;
7332 err = ipv6_addr_label_init();
7334 pr_crit("%s: cannot initialize default policy table: %d\n",
7339 err = register_pernet_subsys(&addrconf_ops);
7343 addrconf_wq = create_workqueue("ipv6_addrconf");
7350 idev = ipv6_add_dev(blackhole_netdev);
7353 err = PTR_ERR(idev);
7357 ip6_route_init_special_entries();
7359 register_netdevice_notifier(&ipv6_dev_notf);
7361 addrconf_verify(&init_net);
7363 rtnl_af_register(&inet6_ops);
7365 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7366 NULL, inet6_dump_ifinfo, 0);
7370 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7371 inet6_rtm_newaddr, NULL, 0);
7374 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7375 inet6_rtm_deladdr, NULL, 0);
7378 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7379 inet6_rtm_getaddr, inet6_dump_ifaddr,
7380 RTNL_FLAG_DOIT_UNLOCKED);
7383 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7384 NULL, inet6_dump_ifmcaddr, 0);
7387 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7388 NULL, inet6_dump_ifacaddr, 0);
7391 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7392 inet6_netconf_get_devconf,
7393 inet6_netconf_dump_devconf,
7394 RTNL_FLAG_DOIT_UNLOCKED);
7397 err = ipv6_addr_label_rtnl_register();
7403 rtnl_unregister_all(PF_INET6);
7404 rtnl_af_unregister(&inet6_ops);
7405 unregister_netdevice_notifier(&ipv6_dev_notf);
7407 destroy_workqueue(addrconf_wq);
7409 unregister_pernet_subsys(&addrconf_ops);
7411 ipv6_addr_label_cleanup();
7416 void addrconf_cleanup(void)
7418 struct net_device *dev;
7420 unregister_netdevice_notifier(&ipv6_dev_notf);
7421 unregister_pernet_subsys(&addrconf_ops);
7422 ipv6_addr_label_cleanup();
7424 rtnl_af_unregister(&inet6_ops);
7428 /* clean dev list */
7429 for_each_netdev(&init_net, dev) {
7430 if (__in6_dev_get(dev) == NULL)
7432 addrconf_ifdown(dev, true);
7434 addrconf_ifdown(init_net.loopback_dev, true);
7438 destroy_workqueue(addrconf_wq);