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 = (900000 + prandom_u32_max(200001)) * (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 = (1900000 + prandom_u32_max(200001)) * (u64)rt;
116 do_div(tmp, 1000000);
117 if ((s32)tmp > mrt) {
118 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
119 tmp = (900000 + prandom_u32_max(200001)) * (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 /* Combine dev_addr_genid and dev_base_seq to detect changes.
711 static u32 inet6_base_seq(const struct net *net)
713 u32 res = atomic_read(&net->ipv6.dev_addr_genid) +
716 /* Must not return 0 (see nl_dump_check_consistent()).
717 * Chose a value far away from 0.
725 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
726 struct netlink_callback *cb)
728 const struct nlmsghdr *nlh = cb->nlh;
729 struct net *net = sock_net(skb->sk);
732 struct net_device *dev;
733 struct inet6_dev *idev;
734 struct hlist_head *head;
736 if (cb->strict_check) {
737 struct netlink_ext_ack *extack = cb->extack;
738 struct netconfmsg *ncm;
740 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
741 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
745 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
746 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
752 s_idx = idx = cb->args[1];
754 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
756 head = &net->dev_index_head[h];
758 cb->seq = inet6_base_seq(net);
759 hlist_for_each_entry_rcu(dev, head, index_hlist) {
762 idev = __in6_dev_get(dev);
766 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
768 NETLINK_CB(cb->skb).portid,
776 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
782 if (h == NETDEV_HASHENTRIES) {
783 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
784 net->ipv6.devconf_all,
785 NETLINK_CB(cb->skb).portid,
787 RTM_NEWNETCONF, NLM_F_MULTI,
793 if (h == NETDEV_HASHENTRIES + 1) {
794 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
795 net->ipv6.devconf_dflt,
796 NETLINK_CB(cb->skb).portid,
798 RTM_NEWNETCONF, NLM_F_MULTI,
812 static void dev_forward_change(struct inet6_dev *idev)
814 struct net_device *dev;
815 struct inet6_ifaddr *ifa;
816 LIST_HEAD(tmp_addr_list);
821 if (idev->cnf.forwarding)
822 dev_disable_lro(dev);
823 if (dev->flags & IFF_MULTICAST) {
824 if (idev->cnf.forwarding) {
825 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
826 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
827 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
829 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
830 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
831 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
835 read_lock_bh(&idev->lock);
836 list_for_each_entry(ifa, &idev->addr_list, if_list) {
837 if (ifa->flags&IFA_F_TENTATIVE)
839 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
841 read_unlock_bh(&idev->lock);
843 while (!list_empty(&tmp_addr_list)) {
844 ifa = list_first_entry(&tmp_addr_list,
845 struct inet6_ifaddr, if_list_aux);
846 list_del(&ifa->if_list_aux);
847 if (idev->cnf.forwarding)
848 addrconf_join_anycast(ifa);
850 addrconf_leave_anycast(ifa);
853 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
855 dev->ifindex, &idev->cnf);
859 static void addrconf_forward_change(struct net *net, __s32 newf)
861 struct net_device *dev;
862 struct inet6_dev *idev;
864 for_each_netdev(net, dev) {
865 idev = __in6_dev_get(dev);
867 int changed = (!idev->cnf.forwarding) ^ (!newf);
868 idev->cnf.forwarding = newf;
870 dev_forward_change(idev);
875 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
881 return restart_syscall();
883 net = (struct net *)table->extra2;
887 if (p == &net->ipv6.devconf_dflt->forwarding) {
888 if ((!newf) ^ (!old))
889 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
891 NETCONFA_IFINDEX_DEFAULT,
892 net->ipv6.devconf_dflt);
897 if (p == &net->ipv6.devconf_all->forwarding) {
898 int old_dflt = net->ipv6.devconf_dflt->forwarding;
900 net->ipv6.devconf_dflt->forwarding = newf;
901 if ((!newf) ^ (!old_dflt))
902 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
904 NETCONFA_IFINDEX_DEFAULT,
905 net->ipv6.devconf_dflt);
907 addrconf_forward_change(net, newf);
908 if ((!newf) ^ (!old))
909 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
911 NETCONFA_IFINDEX_ALL,
912 net->ipv6.devconf_all);
913 } else if ((!newf) ^ (!old))
914 dev_forward_change((struct inet6_dev *)table->extra1);
918 rt6_purge_dflt_routers(net);
922 static void addrconf_linkdown_change(struct net *net, __s32 newf)
924 struct net_device *dev;
925 struct inet6_dev *idev;
927 for_each_netdev(net, dev) {
928 idev = __in6_dev_get(dev);
930 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
932 idev->cnf.ignore_routes_with_linkdown = newf;
934 inet6_netconf_notify_devconf(dev_net(dev),
936 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
943 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
949 return restart_syscall();
951 net = (struct net *)table->extra2;
955 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
956 if ((!newf) ^ (!old))
957 inet6_netconf_notify_devconf(net,
959 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
960 NETCONFA_IFINDEX_DEFAULT,
961 net->ipv6.devconf_dflt);
966 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
967 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
968 addrconf_linkdown_change(net, newf);
969 if ((!newf) ^ (!old))
970 inet6_netconf_notify_devconf(net,
972 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
973 NETCONFA_IFINDEX_ALL,
974 net->ipv6.devconf_all);
983 /* Nobody refers to this ifaddr, destroy it */
984 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
986 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
988 #ifdef NET_REFCNT_DEBUG
989 pr_debug("%s\n", __func__);
992 in6_dev_put(ifp->idev);
994 if (cancel_delayed_work(&ifp->dad_work))
995 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
998 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
999 pr_warn("Freeing alive inet6 address %p\n", ifp);
1003 kfree_rcu(ifp, rcu);
1007 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
1009 struct list_head *p;
1010 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
1013 * Each device address list is sorted in order of scope -
1014 * global before linklocal.
1016 list_for_each(p, &idev->addr_list) {
1017 struct inet6_ifaddr *ifa
1018 = list_entry(p, struct inet6_ifaddr, if_list);
1019 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
1023 list_add_tail_rcu(&ifp->if_list, p);
1026 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
1028 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
1030 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
1033 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1034 struct net_device *dev, unsigned int hash)
1036 struct inet6_ifaddr *ifp;
1038 hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1039 if (ipv6_addr_equal(&ifp->addr, addr)) {
1040 if (!dev || ifp->idev->dev == dev)
1047 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1049 struct net *net = dev_net(dev);
1050 unsigned int hash = inet6_addr_hash(net, &ifa->addr);
1053 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1055 /* Ignore adding duplicate addresses on an interface */
1056 if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) {
1057 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1060 hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]);
1063 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1068 /* On success it returns ifp with increased reference count */
1070 static struct inet6_ifaddr *
1071 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1072 bool can_block, struct netlink_ext_ack *extack)
1074 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1075 int addr_type = ipv6_addr_type(cfg->pfx);
1076 struct net *net = dev_net(idev->dev);
1077 struct inet6_ifaddr *ifa = NULL;
1078 struct fib6_info *f6i = NULL;
1081 if (addr_type == IPV6_ADDR_ANY ||
1082 (addr_type & IPV6_ADDR_MULTICAST &&
1083 !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
1084 (!(idev->dev->flags & IFF_LOOPBACK) &&
1085 !netif_is_l3_master(idev->dev) &&
1086 addr_type & IPV6_ADDR_LOOPBACK))
1087 return ERR_PTR(-EADDRNOTAVAIL);
1090 err = -ENODEV; /*XXX*/
1094 if (idev->cnf.disable_ipv6) {
1099 /* validator notifier needs to be blocking;
1100 * do not call in atomic context
1103 struct in6_validator_info i6vi = {
1104 .i6vi_addr = *cfg->pfx,
1109 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1110 err = notifier_to_errno(err);
1115 ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
1121 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1128 neigh_parms_data_state_setall(idev->nd_parms);
1130 ifa->addr = *cfg->pfx;
1132 ifa->peer_addr = *cfg->peer_pfx;
1134 spin_lock_init(&ifa->lock);
1135 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1136 INIT_HLIST_NODE(&ifa->addr_lst);
1137 ifa->scope = cfg->scope;
1138 ifa->prefix_len = cfg->plen;
1139 ifa->rt_priority = cfg->rt_priority;
1140 ifa->flags = cfg->ifa_flags;
1141 ifa->ifa_proto = cfg->ifa_proto;
1142 /* No need to add the TENTATIVE flag for addresses with NODAD */
1143 if (!(cfg->ifa_flags & IFA_F_NODAD))
1144 ifa->flags |= IFA_F_TENTATIVE;
1145 ifa->valid_lft = cfg->valid_lft;
1146 ifa->prefered_lft = cfg->preferred_lft;
1147 ifa->cstamp = ifa->tstamp = jiffies;
1148 ifa->tokenized = false;
1156 refcount_set(&ifa->refcnt, 1);
1160 err = ipv6_add_addr_hash(idev->dev, ifa);
1166 write_lock_bh(&idev->lock);
1168 /* Add to inet6_dev unicast addr list. */
1169 ipv6_link_dev_addr(idev, ifa);
1171 if (ifa->flags&IFA_F_TEMPORARY) {
1172 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1177 write_unlock_bh(&idev->lock);
1181 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1183 if (unlikely(err < 0)) {
1184 fib6_info_release(f6i);
1188 in6_dev_put(ifa->idev);
1197 enum cleanup_prefix_rt_t {
1198 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1199 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1200 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1204 * Check, whether the prefix for ifp would still need a prefix route
1205 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1208 * 1) we don't purge prefix if address was not permanent.
1209 * prefix is managed by its own lifetime.
1210 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1211 * 3) if there are no addresses, delete prefix.
1212 * 4) if there are still other permanent address(es),
1213 * corresponding prefix is still permanent.
1214 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1215 * don't purge the prefix, assume user space is managing it.
1216 * 6) otherwise, update prefix lifetime to the
1217 * longest valid lifetime among the corresponding
1218 * addresses on the device.
1219 * Note: subsequent RA will update lifetime.
1221 static enum cleanup_prefix_rt_t
1222 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1224 struct inet6_ifaddr *ifa;
1225 struct inet6_dev *idev = ifp->idev;
1226 unsigned long lifetime;
1227 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1231 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1234 if (ifa->prefix_len != ifp->prefix_len ||
1235 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1238 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1239 return CLEANUP_PREFIX_RT_NOP;
1241 action = CLEANUP_PREFIX_RT_EXPIRE;
1243 spin_lock(&ifa->lock);
1245 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1247 * Note: Because this address is
1248 * not permanent, lifetime <
1249 * LONG_MAX / HZ here.
1251 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1252 *expires = ifa->tstamp + lifetime * HZ;
1253 spin_unlock(&ifa->lock);
1260 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1261 bool del_rt, bool del_peer)
1263 struct fib6_info *f6i;
1265 f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1267 ifp->idev->dev, 0, RTF_DEFAULT, true);
1270 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
1272 if (!(f6i->fib6_flags & RTF_EXPIRES))
1273 fib6_set_expires(f6i, expires);
1274 fib6_info_release(f6i);
1280 /* This function wants to get referenced ifp and releases it before return */
1282 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1284 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1285 struct net *net = dev_net(ifp->idev->dev);
1286 unsigned long expires;
1291 spin_lock_bh(&ifp->lock);
1293 ifp->state = INET6_IFADDR_STATE_DEAD;
1294 spin_unlock_bh(&ifp->lock);
1296 if (state == INET6_IFADDR_STATE_DEAD)
1299 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1300 hlist_del_init_rcu(&ifp->addr_lst);
1301 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1303 write_lock_bh(&ifp->idev->lock);
1305 if (ifp->flags&IFA_F_TEMPORARY) {
1306 list_del(&ifp->tmp_list);
1308 in6_ifa_put(ifp->ifpub);
1314 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1315 action = check_cleanup_prefix_route(ifp, &expires);
1317 list_del_rcu(&ifp->if_list);
1320 write_unlock_bh(&ifp->idev->lock);
1322 addrconf_del_dad_work(ifp);
1324 ipv6_ifa_notify(RTM_DELADDR, ifp);
1326 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1328 if (action != CLEANUP_PREFIX_RT_NOP) {
1329 cleanup_prefix_route(ifp, expires,
1330 action == CLEANUP_PREFIX_RT_DEL, false);
1333 /* clean up prefsrc entries */
1334 rt6_remove_prefsrc(ifp);
1339 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
1341 struct inet6_dev *idev = ifp->idev;
1342 unsigned long tmp_tstamp, age;
1343 unsigned long regen_advance;
1344 unsigned long now = jiffies;
1345 s32 cnf_temp_preferred_lft;
1346 struct inet6_ifaddr *ift;
1347 struct ifa6_config cfg;
1348 long max_desync_factor;
1349 struct in6_addr addr;
1352 write_lock_bh(&idev->lock);
1356 if (idev->cnf.use_tempaddr <= 0) {
1357 write_unlock_bh(&idev->lock);
1358 pr_info("%s: use_tempaddr is disabled\n", __func__);
1363 spin_lock_bh(&ifp->lock);
1364 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1365 idev->cnf.use_tempaddr = -1; /*XXX*/
1366 spin_unlock_bh(&ifp->lock);
1367 write_unlock_bh(&idev->lock);
1368 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1375 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1376 ipv6_gen_rnd_iid(&addr);
1378 age = (now - ifp->tstamp) / HZ;
1380 regen_advance = idev->cnf.regen_max_retry *
1381 idev->cnf.dad_transmits *
1382 max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
1384 /* recalculate max_desync_factor each time and update
1385 * idev->desync_factor if it's larger
1387 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1388 max_desync_factor = min_t(long,
1389 idev->cnf.max_desync_factor,
1390 cnf_temp_preferred_lft - regen_advance);
1392 if (unlikely(idev->desync_factor > max_desync_factor)) {
1393 if (max_desync_factor > 0) {
1394 get_random_bytes(&idev->desync_factor,
1395 sizeof(idev->desync_factor));
1396 idev->desync_factor %= max_desync_factor;
1398 idev->desync_factor = 0;
1402 memset(&cfg, 0, sizeof(cfg));
1403 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
1404 idev->cnf.temp_valid_lft + age);
1405 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1406 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1408 cfg.plen = ifp->prefix_len;
1409 tmp_tstamp = ifp->tstamp;
1410 spin_unlock_bh(&ifp->lock);
1412 write_unlock_bh(&idev->lock);
1414 /* A temporary address is created only if this calculated Preferred
1415 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1416 * an implementation must not create a temporary address with a zero
1417 * Preferred Lifetime.
1418 * Use age calculation as in addrconf_verify to avoid unnecessary
1419 * temporary addresses being generated.
1421 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1422 if (cfg.preferred_lft <= regen_advance + age) {
1429 cfg.ifa_flags = IFA_F_TEMPORARY;
1430 /* set in addrconf_prefix_rcv() */
1431 if (ifp->flags & IFA_F_OPTIMISTIC)
1432 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
1435 cfg.scope = ipv6_addr_scope(cfg.pfx);
1437 ift = ipv6_add_addr(idev, &cfg, block, NULL);
1441 pr_info("%s: retry temporary address regeneration\n", __func__);
1442 write_lock_bh(&idev->lock);
1446 spin_lock_bh(&ift->lock);
1449 ift->tstamp = tmp_tstamp;
1450 spin_unlock_bh(&ift->lock);
1452 addrconf_dad_start(ift);
1460 * Choose an appropriate source address (RFC3484)
1463 IPV6_SADDR_RULE_INIT = 0,
1464 IPV6_SADDR_RULE_LOCAL,
1465 IPV6_SADDR_RULE_SCOPE,
1466 IPV6_SADDR_RULE_PREFERRED,
1467 #ifdef CONFIG_IPV6_MIP6
1468 IPV6_SADDR_RULE_HOA,
1470 IPV6_SADDR_RULE_OIF,
1471 IPV6_SADDR_RULE_LABEL,
1472 IPV6_SADDR_RULE_PRIVACY,
1473 IPV6_SADDR_RULE_ORCHID,
1474 IPV6_SADDR_RULE_PREFIX,
1475 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1476 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1481 struct ipv6_saddr_score {
1484 struct inet6_ifaddr *ifa;
1485 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1490 struct ipv6_saddr_dst {
1491 const struct in6_addr *addr;
1498 static inline int ipv6_saddr_preferred(int type)
1500 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1505 static bool ipv6_use_optimistic_addr(struct net *net,
1506 struct inet6_dev *idev)
1508 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1511 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1513 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1522 static bool ipv6_allow_optimistic_dad(struct net *net,
1523 struct inet6_dev *idev)
1525 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1528 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1537 static int ipv6_get_saddr_eval(struct net *net,
1538 struct ipv6_saddr_score *score,
1539 struct ipv6_saddr_dst *dst,
1544 if (i <= score->rule) {
1546 case IPV6_SADDR_RULE_SCOPE:
1547 ret = score->scopedist;
1549 case IPV6_SADDR_RULE_PREFIX:
1550 ret = score->matchlen;
1553 ret = !!test_bit(i, score->scorebits);
1559 case IPV6_SADDR_RULE_INIT:
1560 /* Rule 0: remember if hiscore is not ready yet */
1563 case IPV6_SADDR_RULE_LOCAL:
1564 /* Rule 1: Prefer same address */
1565 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1567 case IPV6_SADDR_RULE_SCOPE:
1568 /* Rule 2: Prefer appropriate scope
1573 * ---+--+-+---> scope
1575 * | d is scope of the destination.
1577 * | \ <- smaller scope is better if
1578 * B-15 | \ if scope is enough for destination.
1579 * | ret = B - scope (-1 <= scope >= d <= 15).
1581 * |/ <- greater is better
1582 * -C / if scope is not enough for destination.
1583 * /| ret = scope - C (-1 <= d < scope <= 15).
1585 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1586 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1587 * Assume B = 0 and we get C > 29.
1589 ret = __ipv6_addr_src_scope(score->addr_type);
1590 if (ret >= dst->scope)
1593 ret -= 128; /* 30 is enough */
1594 score->scopedist = ret;
1596 case IPV6_SADDR_RULE_PREFERRED:
1598 /* Rule 3: Avoid deprecated and optimistic addresses */
1599 u8 avoid = IFA_F_DEPRECATED;
1601 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
1602 avoid |= IFA_F_OPTIMISTIC;
1603 ret = ipv6_saddr_preferred(score->addr_type) ||
1604 !(score->ifa->flags & avoid);
1607 #ifdef CONFIG_IPV6_MIP6
1608 case IPV6_SADDR_RULE_HOA:
1610 /* Rule 4: Prefer home address */
1611 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1612 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1616 case IPV6_SADDR_RULE_OIF:
1617 /* Rule 5: Prefer outgoing interface */
1618 ret = (!dst->ifindex ||
1619 dst->ifindex == score->ifa->idev->dev->ifindex);
1621 case IPV6_SADDR_RULE_LABEL:
1622 /* Rule 6: Prefer matching label */
1623 ret = ipv6_addr_label(net,
1624 &score->ifa->addr, score->addr_type,
1625 score->ifa->idev->dev->ifindex) == dst->label;
1627 case IPV6_SADDR_RULE_PRIVACY:
1629 /* Rule 7: Prefer public address
1630 * Note: prefer temporary address if use_tempaddr >= 2
1632 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1633 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1634 score->ifa->idev->cnf.use_tempaddr >= 2;
1635 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1638 case IPV6_SADDR_RULE_ORCHID:
1639 /* Rule 8-: Prefer ORCHID vs ORCHID or
1640 * non-ORCHID vs non-ORCHID
1642 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1643 ipv6_addr_orchid(dst->addr));
1645 case IPV6_SADDR_RULE_PREFIX:
1646 /* Rule 8: Use longest matching prefix */
1647 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1648 if (ret > score->ifa->prefix_len)
1649 ret = score->ifa->prefix_len;
1650 score->matchlen = ret;
1652 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1653 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1654 /* Optimistic addresses still have lower precedence than other
1655 * preferred addresses.
1657 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1665 __set_bit(i, score->scorebits);
1671 static int __ipv6_dev_get_saddr(struct net *net,
1672 struct ipv6_saddr_dst *dst,
1673 struct inet6_dev *idev,
1674 struct ipv6_saddr_score *scores,
1677 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1679 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1683 * - Tentative Address (RFC2462 section 5.4)
1684 * - A tentative address is not considered
1685 * "assigned to an interface" in the traditional
1686 * sense, unless it is also flagged as optimistic.
1687 * - Candidate Source Address (section 4)
1688 * - In any case, anycast addresses, multicast
1689 * addresses, and the unspecified address MUST
1690 * NOT be included in a candidate set.
1692 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1693 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1696 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1698 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1699 score->addr_type & IPV6_ADDR_MULTICAST)) {
1700 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1706 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1708 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1709 int minihiscore, miniscore;
1711 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1712 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1714 if (minihiscore > miniscore) {
1715 if (i == IPV6_SADDR_RULE_SCOPE &&
1716 score->scopedist > 0) {
1719 * each remaining entry
1720 * has too small (not enough)
1721 * scope, because ifa entries
1722 * are sorted by their scope
1728 } else if (minihiscore < miniscore) {
1729 swap(hiscore, score);
1730 hiscore_idx = 1 - hiscore_idx;
1732 /* restore our iterator */
1733 score->ifa = hiscore->ifa;
1743 static int ipv6_get_saddr_master(struct net *net,
1744 const struct net_device *dst_dev,
1745 const struct net_device *master,
1746 struct ipv6_saddr_dst *dst,
1747 struct ipv6_saddr_score *scores,
1750 struct inet6_dev *idev;
1752 idev = __in6_dev_get(dst_dev);
1754 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1755 scores, hiscore_idx);
1757 idev = __in6_dev_get(master);
1759 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1760 scores, hiscore_idx);
1765 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1766 const struct in6_addr *daddr, unsigned int prefs,
1767 struct in6_addr *saddr)
1769 struct ipv6_saddr_score scores[2], *hiscore;
1770 struct ipv6_saddr_dst dst;
1771 struct inet6_dev *idev;
1772 struct net_device *dev;
1774 bool use_oif_addr = false;
1775 int hiscore_idx = 0;
1778 dst_type = __ipv6_addr_type(daddr);
1780 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1781 dst.scope = __ipv6_addr_src_scope(dst_type);
1782 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1785 scores[hiscore_idx].rule = -1;
1786 scores[hiscore_idx].ifa = NULL;
1790 /* Candidate Source Address (section 4)
1791 * - multicast and link-local destination address,
1792 * the set of candidate source address MUST only
1793 * include addresses assigned to interfaces
1794 * belonging to the same link as the outgoing
1796 * (- For site-local destination addresses, the
1797 * set of candidate source addresses MUST only
1798 * include addresses assigned to interfaces
1799 * belonging to the same site as the outgoing
1801 * - "It is RECOMMENDED that the candidate source addresses
1802 * be the set of unicast addresses assigned to the
1803 * interface that will be used to send to the destination
1804 * (the 'outgoing' interface)." (RFC 6724)
1807 idev = __in6_dev_get(dst_dev);
1808 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1809 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1810 (idev && idev->cnf.use_oif_addrs_only)) {
1811 use_oif_addr = true;
1817 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1819 const struct net_device *master;
1822 /* if dst_dev exists and is enslaved to an L3 device, then
1823 * prefer addresses from dst_dev and then the master over
1824 * any other enslaved devices in the L3 domain.
1826 master = l3mdev_master_dev_rcu(dst_dev);
1828 master_idx = master->ifindex;
1830 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1832 scores, hiscore_idx);
1834 if (scores[hiscore_idx].ifa)
1838 for_each_netdev_rcu(net, dev) {
1839 /* only consider addresses on devices in the
1842 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1844 idev = __in6_dev_get(dev);
1847 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1852 hiscore = &scores[hiscore_idx];
1854 ret = -EADDRNOTAVAIL;
1856 *saddr = hiscore->ifa->addr;
1861 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1863 static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1866 struct inet6_ifaddr *ifp;
1867 int err = -EADDRNOTAVAIL;
1869 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1870 if (ifp->scope > IFA_LINK)
1872 if (ifp->scope == IFA_LINK &&
1873 !(ifp->flags & banned_flags)) {
1882 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1885 struct inet6_dev *idev;
1886 int err = -EADDRNOTAVAIL;
1889 idev = __in6_dev_get(dev);
1891 read_lock_bh(&idev->lock);
1892 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1893 read_unlock_bh(&idev->lock);
1899 static int ipv6_count_addresses(const struct inet6_dev *idev)
1901 const struct inet6_ifaddr *ifp;
1905 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1911 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1912 const struct net_device *dev, int strict)
1914 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1915 strict, IFA_F_TENTATIVE);
1917 EXPORT_SYMBOL(ipv6_chk_addr);
1919 /* device argument is used to find the L3 domain of interest. If
1920 * skip_dev_check is set, then the ifp device is not checked against
1921 * the passed in dev argument. So the 2 cases for addresses checks are:
1922 * 1. does the address exist in the L3 domain that dev is part of
1923 * (skip_dev_check = true), or
1925 * 2. does the address exist on the specific device
1926 * (skip_dev_check = false)
1928 static struct net_device *
1929 __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1930 const struct net_device *dev, bool skip_dev_check,
1931 int strict, u32 banned_flags)
1933 unsigned int hash = inet6_addr_hash(net, addr);
1934 struct net_device *l3mdev, *ndev;
1935 struct inet6_ifaddr *ifp;
1940 l3mdev = l3mdev_master_dev_rcu(dev);
1944 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1945 ndev = ifp->idev->dev;
1947 if (l3mdev_master_dev_rcu(ndev) != l3mdev)
1950 /* Decouple optimistic from tentative for evaluation here.
1951 * Ban optimistic addresses explicitly, when required.
1953 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1954 ? (ifp->flags&~IFA_F_TENTATIVE)
1956 if (ipv6_addr_equal(&ifp->addr, addr) &&
1957 !(ifp_flags&banned_flags) &&
1958 (!dev || ndev == dev ||
1959 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1969 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1970 const struct net_device *dev, bool skip_dev_check,
1971 int strict, u32 banned_flags)
1973 return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
1974 strict, banned_flags) ? 1 : 0;
1976 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1979 /* Compares an address/prefix_len with addresses on device @dev.
1980 * If one is found it returns true.
1982 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1983 const unsigned int prefix_len, struct net_device *dev)
1985 const struct inet6_ifaddr *ifa;
1986 const struct inet6_dev *idev;
1990 idev = __in6_dev_get(dev);
1992 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1993 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
2002 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
2004 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
2006 const struct inet6_ifaddr *ifa;
2007 const struct inet6_dev *idev;
2012 idev = __in6_dev_get(dev);
2014 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
2015 onlink = ipv6_prefix_equal(addr, &ifa->addr,
2024 EXPORT_SYMBOL(ipv6_chk_prefix);
2027 * ipv6_dev_find - find the first device with a given source address.
2028 * @net: the net namespace
2029 * @addr: the source address
2030 * @dev: used to find the L3 domain of interest
2032 * The caller should be protected by RCU, or RTNL.
2034 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
2035 struct net_device *dev)
2037 return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
2040 EXPORT_SYMBOL(ipv6_dev_find);
2042 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
2043 struct net_device *dev, int strict)
2045 unsigned int hash = inet6_addr_hash(net, addr);
2046 struct inet6_ifaddr *ifp, *result = NULL;
2049 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2050 if (ipv6_addr_equal(&ifp->addr, addr)) {
2051 if (!dev || ifp->idev->dev == dev ||
2052 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2053 if (in6_ifa_hold_safe(ifp)) {
2065 /* Gets referenced address, destroys ifaddr */
2067 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2070 ifp->flags |= IFA_F_DADFAILED;
2072 if (ifp->flags&IFA_F_TEMPORARY) {
2073 struct inet6_ifaddr *ifpub;
2074 spin_lock_bh(&ifp->lock);
2077 in6_ifa_hold(ifpub);
2078 spin_unlock_bh(&ifp->lock);
2079 ipv6_create_tempaddr(ifpub, true);
2082 spin_unlock_bh(&ifp->lock);
2085 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2086 spin_lock_bh(&ifp->lock);
2087 addrconf_del_dad_work(ifp);
2088 ifp->flags |= IFA_F_TENTATIVE;
2090 ifp->flags &= ~IFA_F_OPTIMISTIC;
2091 spin_unlock_bh(&ifp->lock);
2093 ipv6_ifa_notify(0, ifp);
2100 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2104 spin_lock_bh(&ifp->lock);
2105 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2106 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2109 spin_unlock_bh(&ifp->lock);
2114 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2116 struct inet6_dev *idev = ifp->idev;
2117 struct net *net = dev_net(idev->dev);
2119 if (addrconf_dad_end(ifp)) {
2124 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2125 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2127 spin_lock_bh(&ifp->lock);
2129 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2130 struct in6_addr new_addr;
2131 struct inet6_ifaddr *ifp2;
2132 int retries = ifp->stable_privacy_retry + 1;
2133 struct ifa6_config cfg = {
2135 .plen = ifp->prefix_len,
2136 .ifa_flags = ifp->flags,
2137 .valid_lft = ifp->valid_lft,
2138 .preferred_lft = ifp->prefered_lft,
2139 .scope = ifp->scope,
2142 if (retries > net->ipv6.sysctl.idgen_retries) {
2143 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2144 ifp->idev->dev->name);
2148 new_addr = ifp->addr;
2149 if (ipv6_generate_stable_address(&new_addr, retries,
2153 spin_unlock_bh(&ifp->lock);
2155 if (idev->cnf.max_addresses &&
2156 ipv6_count_addresses(idev) >=
2157 idev->cnf.max_addresses)
2160 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2161 ifp->idev->dev->name);
2163 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2167 spin_lock_bh(&ifp2->lock);
2168 ifp2->stable_privacy_retry = retries;
2169 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2170 spin_unlock_bh(&ifp2->lock);
2172 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2175 spin_lock_bh(&ifp->lock);
2179 /* transition from _POSTDAD to _ERRDAD */
2180 ifp->state = INET6_IFADDR_STATE_ERRDAD;
2181 spin_unlock_bh(&ifp->lock);
2183 addrconf_mod_dad_work(ifp, 0);
2187 /* Join to solicited addr multicast group.
2188 * caller must hold RTNL */
2189 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2191 struct in6_addr maddr;
2193 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2196 addrconf_addr_solict_mult(addr, &maddr);
2197 ipv6_dev_mc_inc(dev, &maddr);
2200 /* caller must hold RTNL */
2201 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2203 struct in6_addr maddr;
2205 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2208 addrconf_addr_solict_mult(addr, &maddr);
2209 __ipv6_dev_mc_dec(idev, &maddr);
2212 /* caller must hold RTNL */
2213 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2215 struct in6_addr addr;
2217 if (ifp->prefix_len >= 127) /* RFC 6164 */
2219 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2220 if (ipv6_addr_any(&addr))
2222 __ipv6_dev_ac_inc(ifp->idev, &addr);
2225 /* caller must hold RTNL */
2226 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2228 struct in6_addr addr;
2230 if (ifp->prefix_len >= 127) /* RFC 6164 */
2232 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2233 if (ipv6_addr_any(&addr))
2235 __ipv6_dev_ac_dec(ifp->idev, &addr);
2238 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2240 switch (dev->addr_len) {
2242 memcpy(eui, dev->dev_addr, 3);
2245 memcpy(eui + 5, dev->dev_addr + 3, 3);
2247 case EUI64_ADDR_LEN:
2248 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2258 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2260 const union fwnet_hwaddr *ha;
2262 if (dev->addr_len != FWNET_ALEN)
2265 ha = (const union fwnet_hwaddr *)dev->dev_addr;
2267 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2272 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2274 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2275 if (dev->addr_len != ARCNET_ALEN)
2278 eui[7] = *(u8 *)dev->dev_addr;
2282 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2284 if (dev->addr_len != INFINIBAND_ALEN)
2286 memcpy(eui, dev->dev_addr + 12, 8);
2291 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2295 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2296 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2297 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2298 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2299 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2300 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2304 memcpy(eui + 4, &addr, 4);
2308 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2310 if (dev->priv_flags & IFF_ISATAP)
2311 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2315 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2317 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2320 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2322 memcpy(eui, dev->perm_addr, 3);
2323 memcpy(eui + 5, dev->perm_addr + 3, 3);
2330 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2332 switch (dev->type) {
2335 return addrconf_ifid_eui48(eui, dev);
2337 return addrconf_ifid_arcnet(eui, dev);
2338 case ARPHRD_INFINIBAND:
2339 return addrconf_ifid_infiniband(eui, dev);
2341 return addrconf_ifid_sit(eui, dev);
2344 return addrconf_ifid_gre(eui, dev);
2345 case ARPHRD_6LOWPAN:
2346 return addrconf_ifid_6lowpan(eui, dev);
2347 case ARPHRD_IEEE1394:
2348 return addrconf_ifid_ieee1394(eui, dev);
2349 case ARPHRD_TUNNEL6:
2352 return addrconf_ifid_ip6tnl(eui, dev);
2357 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2360 struct inet6_ifaddr *ifp;
2362 read_lock_bh(&idev->lock);
2363 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2364 if (ifp->scope > IFA_LINK)
2366 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2367 memcpy(eui, ifp->addr.s6_addr+8, 8);
2372 read_unlock_bh(&idev->lock);
2376 /* Generation of a randomized Interface Identifier
2377 * draft-ietf-6man-rfc4941bis, Section 3.3.1
2380 static void ipv6_gen_rnd_iid(struct in6_addr *addr)
2383 get_random_bytes(&addr->s6_addr[8], 8);
2385 /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2386 * check if generated address is not inappropriate:
2388 * - Reserved IPv6 Interface Identifiers
2389 * - XXX: already assigned to an address on the device
2392 /* Subnet-router anycast: 0000:0000:0000:0000 */
2393 if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2396 /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2397 * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
2398 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2400 if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2401 (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2404 /* Reserved subnet anycast addresses */
2405 if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2406 ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2415 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2416 struct net_device *dev, unsigned long expires,
2417 u32 flags, gfp_t gfp_flags)
2419 struct fib6_config cfg = {
2420 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2421 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2422 .fc_ifindex = dev->ifindex,
2423 .fc_expires = expires,
2425 .fc_flags = RTF_UP | flags,
2426 .fc_nlinfo.nl_net = dev_net(dev),
2427 .fc_protocol = RTPROT_KERNEL,
2428 .fc_type = RTN_UNICAST,
2433 /* Prevent useless cloning on PtP SIT.
2434 This thing is done here expecting that the whole
2435 class of non-broadcast devices need not cloning.
2437 #if IS_ENABLED(CONFIG_IPV6_SIT)
2438 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2439 cfg.fc_flags |= RTF_NONEXTHOP;
2442 ip6_route_add(&cfg, gfp_flags, NULL);
2446 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2448 const struct net_device *dev,
2449 u32 flags, u32 noflags,
2452 struct fib6_node *fn;
2453 struct fib6_info *rt = NULL;
2454 struct fib6_table *table;
2455 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2457 table = fib6_get_table(dev_net(dev), tb_id);
2462 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2466 for_each_fib6_node_rt_rcu(fn) {
2467 /* prefix routes only use builtin fib6_nh */
2471 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
2473 if (no_gw && rt->fib6_nh->fib_nh_gw_family)
2475 if ((rt->fib6_flags & flags) != flags)
2477 if ((rt->fib6_flags & noflags) != 0)
2479 if (!fib6_info_hold_safe(rt))
2489 /* Create "default" multicast route to the interface */
2491 static void addrconf_add_mroute(struct net_device *dev)
2493 struct fib6_config cfg = {
2494 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2495 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2496 .fc_ifindex = dev->ifindex,
2499 .fc_type = RTN_MULTICAST,
2500 .fc_nlinfo.nl_net = dev_net(dev),
2501 .fc_protocol = RTPROT_KERNEL,
2504 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2506 ip6_route_add(&cfg, GFP_KERNEL, NULL);
2509 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2511 struct inet6_dev *idev;
2515 idev = ipv6_find_idev(dev);
2519 if (idev->cnf.disable_ipv6)
2520 return ERR_PTR(-EACCES);
2522 /* Add default multicast route */
2523 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2524 addrconf_add_mroute(dev);
2529 static void manage_tempaddrs(struct inet6_dev *idev,
2530 struct inet6_ifaddr *ifp,
2531 __u32 valid_lft, __u32 prefered_lft,
2532 bool create, unsigned long now)
2535 struct inet6_ifaddr *ift;
2537 read_lock_bh(&idev->lock);
2538 /* update all temporary addresses in the list */
2539 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2540 int age, max_valid, max_prefered;
2542 if (ifp != ift->ifpub)
2545 /* RFC 4941 section 3.3:
2546 * If a received option will extend the lifetime of a public
2547 * address, the lifetimes of temporary addresses should
2548 * be extended, subject to the overall constraint that no
2549 * temporary addresses should ever remain "valid" or "preferred"
2550 * for a time longer than (TEMP_VALID_LIFETIME) or
2551 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2553 age = (now - ift->cstamp) / HZ;
2554 max_valid = idev->cnf.temp_valid_lft - age;
2558 max_prefered = idev->cnf.temp_prefered_lft -
2559 idev->desync_factor - age;
2560 if (max_prefered < 0)
2563 if (valid_lft > max_valid)
2564 valid_lft = max_valid;
2566 if (prefered_lft > max_prefered)
2567 prefered_lft = max_prefered;
2569 spin_lock(&ift->lock);
2571 ift->valid_lft = valid_lft;
2572 ift->prefered_lft = prefered_lft;
2574 if (prefered_lft > 0)
2575 ift->flags &= ~IFA_F_DEPRECATED;
2577 spin_unlock(&ift->lock);
2578 if (!(flags&IFA_F_TENTATIVE))
2579 ipv6_ifa_notify(0, ift);
2582 /* Also create a temporary address if it's enabled but no temporary
2583 * address currently exists.
2584 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
2585 * as part of cleanup (ie. deleting the mngtmpaddr).
2586 * We don't want that to result in creating a new temporary ip address.
2588 if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
2591 if (create && idev->cnf.use_tempaddr > 0) {
2592 /* When a new public address is created as described
2593 * in [ADDRCONF], also create a new temporary address.
2595 read_unlock_bh(&idev->lock);
2596 ipv6_create_tempaddr(ifp, false);
2598 read_unlock_bh(&idev->lock);
2602 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2604 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2605 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2608 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2609 const struct prefix_info *pinfo,
2610 struct inet6_dev *in6_dev,
2611 const struct in6_addr *addr, int addr_type,
2612 u32 addr_flags, bool sllao, bool tokenized,
2613 __u32 valid_lft, u32 prefered_lft)
2615 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2616 int create = 0, update_lft = 0;
2618 if (!ifp && valid_lft) {
2619 int max_addresses = in6_dev->cnf.max_addresses;
2620 struct ifa6_config cfg = {
2622 .plen = pinfo->prefix_len,
2623 .ifa_flags = addr_flags,
2624 .valid_lft = valid_lft,
2625 .preferred_lft = prefered_lft,
2626 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2627 .ifa_proto = IFAPROT_KERNEL_RA
2630 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2631 if ((net->ipv6.devconf_all->optimistic_dad ||
2632 in6_dev->cnf.optimistic_dad) &&
2633 !net->ipv6.devconf_all->forwarding && sllao)
2634 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2637 /* Do not allow to create too much of autoconfigured
2638 * addresses; this would be too easy way to crash kernel.
2640 if (!max_addresses ||
2641 ipv6_count_addresses(in6_dev) < max_addresses)
2642 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2644 if (IS_ERR_OR_NULL(ifp))
2648 spin_lock_bh(&ifp->lock);
2649 ifp->flags |= IFA_F_MANAGETEMPADDR;
2650 ifp->cstamp = jiffies;
2651 ifp->tokenized = tokenized;
2652 spin_unlock_bh(&ifp->lock);
2653 addrconf_dad_start(ifp);
2661 /* update lifetime (RFC2462 5.5.3 e) */
2662 spin_lock_bh(&ifp->lock);
2664 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2665 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2668 if (!create && stored_lft) {
2669 const u32 minimum_lft = min_t(u32,
2670 stored_lft, MIN_VALID_LIFETIME);
2671 valid_lft = max(valid_lft, minimum_lft);
2673 /* RFC4862 Section 5.5.3e:
2674 * "Note that the preferred lifetime of the
2675 * corresponding address is always reset to
2676 * the Preferred Lifetime in the received
2677 * Prefix Information option, regardless of
2678 * whether the valid lifetime is also reset or
2681 * So we should always update prefered_lft here.
2687 ifp->valid_lft = valid_lft;
2688 ifp->prefered_lft = prefered_lft;
2691 ifp->flags &= ~IFA_F_DEPRECATED;
2692 spin_unlock_bh(&ifp->lock);
2694 if (!(flags&IFA_F_TENTATIVE))
2695 ipv6_ifa_notify(0, ifp);
2697 spin_unlock_bh(&ifp->lock);
2699 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2703 addrconf_verify(net);
2708 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2710 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2712 struct prefix_info *pinfo;
2717 struct inet6_dev *in6_dev;
2718 struct net *net = dev_net(dev);
2720 pinfo = (struct prefix_info *) opt;
2722 if (len < sizeof(struct prefix_info)) {
2723 netdev_dbg(dev, "addrconf: prefix option too short\n");
2728 * Validation checks ([ADDRCONF], page 19)
2731 addr_type = ipv6_addr_type(&pinfo->prefix);
2733 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2736 valid_lft = ntohl(pinfo->valid);
2737 prefered_lft = ntohl(pinfo->prefered);
2739 if (prefered_lft > valid_lft) {
2740 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2744 in6_dev = in6_dev_get(dev);
2747 net_dbg_ratelimited("addrconf: device %s not configured\n",
2752 if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft)
2756 * Two things going on here:
2757 * 1) Add routes for on-link prefixes
2758 * 2) Configure prefixes with the auto flag set
2761 if (pinfo->onlink) {
2762 struct fib6_info *rt;
2763 unsigned long rt_expires;
2765 /* Avoid arithmetic overflow. Really, we could
2766 * save rt_expires in seconds, likely valid_lft,
2767 * but it would require division in fib gc, that it
2771 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2773 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2775 if (addrconf_finite_timeout(rt_expires))
2778 rt = addrconf_get_prefix_route(&pinfo->prefix,
2781 RTF_ADDRCONF | RTF_PREFIX_RT,
2785 /* Autoconf prefix route */
2786 if (valid_lft == 0) {
2787 ip6_del_rt(net, rt, false);
2789 } else if (addrconf_finite_timeout(rt_expires)) {
2791 fib6_set_expires(rt, jiffies + rt_expires);
2793 fib6_clean_expires(rt);
2795 } else if (valid_lft) {
2796 clock_t expires = 0;
2797 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2798 if (addrconf_finite_timeout(rt_expires)) {
2800 flags |= RTF_EXPIRES;
2801 expires = jiffies_to_clock_t(rt_expires);
2803 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2804 0, dev, expires, flags,
2807 fib6_info_release(rt);
2810 /* Try to figure out our local address for this prefix */
2812 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2813 struct in6_addr addr;
2814 bool tokenized = false, dev_addr_generated = false;
2816 if (pinfo->prefix_len == 64) {
2817 memcpy(&addr, &pinfo->prefix, 8);
2819 if (!ipv6_addr_any(&in6_dev->token)) {
2820 read_lock_bh(&in6_dev->lock);
2821 memcpy(addr.s6_addr + 8,
2822 in6_dev->token.s6_addr + 8, 8);
2823 read_unlock_bh(&in6_dev->lock);
2825 } else if (is_addr_mode_generate_stable(in6_dev) &&
2826 !ipv6_generate_stable_address(&addr, 0,
2828 addr_flags |= IFA_F_STABLE_PRIVACY;
2830 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2831 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2834 dev_addr_generated = true;
2838 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2843 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2846 tokenized, valid_lft,
2851 /* Ignore error case here because previous prefix add addr was
2852 * successful which will be notified.
2854 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2855 addr_type, addr_flags, sllao,
2856 tokenized, valid_lft,
2858 dev_addr_generated);
2860 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2862 in6_dev_put(in6_dev);
2865 static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
2866 struct in6_ifreq *ireq)
2868 struct ip_tunnel_parm p = { };
2871 if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
2872 return -EADDRNOTAVAIL;
2874 p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
2877 p.iph.protocol = IPPROTO_IPV6;
2880 if (!dev->netdev_ops->ndo_tunnel_ctl)
2882 err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
2886 dev = __dev_get_by_name(net, p.name);
2889 return dev_open(dev, NULL);
2893 * Set destination address.
2894 * Special case for SIT interfaces where we create a new "virtual"
2897 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2899 struct net_device *dev;
2900 struct in6_ifreq ireq;
2903 if (!IS_ENABLED(CONFIG_IPV6_SIT))
2905 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2909 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2910 if (dev && dev->type == ARPHRD_SIT)
2911 err = addrconf_set_sit_dstaddr(net, dev, &ireq);
2916 static int ipv6_mc_config(struct sock *sk, bool join,
2917 const struct in6_addr *addr, int ifindex)
2925 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2927 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2934 * Manual configuration of address on an interface
2936 static int inet6_addr_add(struct net *net, int ifindex,
2937 struct ifa6_config *cfg,
2938 struct netlink_ext_ack *extack)
2940 struct inet6_ifaddr *ifp;
2941 struct inet6_dev *idev;
2942 struct net_device *dev;
2943 unsigned long timeout;
2949 if (cfg->plen > 128)
2952 /* check the lifetime */
2953 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
2956 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
2959 dev = __dev_get_by_index(net, ifindex);
2963 idev = addrconf_add_dev(dev);
2965 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);
2975 cfg->scope = ipv6_addr_scope(cfg->pfx);
2977 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
2978 if (addrconf_finite_timeout(timeout)) {
2979 expires = jiffies_to_clock_t(timeout * HZ);
2980 cfg->valid_lft = timeout;
2981 flags = RTF_EXPIRES;
2985 cfg->ifa_flags |= IFA_F_PERMANENT;
2988 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
2989 if (addrconf_finite_timeout(timeout)) {
2991 cfg->ifa_flags |= IFA_F_DEPRECATED;
2992 cfg->preferred_lft = timeout;
2995 ifp = ipv6_add_addr(idev, cfg, true, extack);
2997 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
2998 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2999 ifp->rt_priority, dev, expires,
3003 /* Send a netlink notification if DAD is enabled and
3004 * optimistic flag is not set
3006 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
3007 ipv6_ifa_notify(0, ifp);
3009 * Note that section 3.1 of RFC 4429 indicates
3010 * that the Optimistic flag should not be set for
3011 * manually configured addresses
3013 addrconf_dad_start(ifp);
3014 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
3015 manage_tempaddrs(idev, ifp, cfg->valid_lft,
3016 cfg->preferred_lft, true, jiffies);
3018 addrconf_verify_rtnl(net);
3020 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3021 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
3025 return PTR_ERR(ifp);
3028 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
3029 const struct in6_addr *pfx, unsigned int plen)
3031 struct inet6_ifaddr *ifp;
3032 struct inet6_dev *idev;
3033 struct net_device *dev;
3038 dev = __dev_get_by_index(net, ifindex);
3042 idev = __in6_dev_get(dev);
3046 read_lock_bh(&idev->lock);
3047 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3048 if (ifp->prefix_len == plen &&
3049 ipv6_addr_equal(pfx, &ifp->addr)) {
3051 read_unlock_bh(&idev->lock);
3053 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3054 (ifa_flags & IFA_F_MANAGETEMPADDR))
3055 manage_tempaddrs(idev, ifp, 0, 0, false,
3058 addrconf_verify_rtnl(net);
3059 if (ipv6_addr_is_multicast(pfx)) {
3060 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3061 false, pfx, dev->ifindex);
3066 read_unlock_bh(&idev->lock);
3067 return -EADDRNOTAVAIL;
3071 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3073 struct ifa6_config cfg = {
3074 .ifa_flags = IFA_F_PERMANENT,
3075 .preferred_lft = INFINITY_LIFE_TIME,
3076 .valid_lft = INFINITY_LIFE_TIME,
3078 struct in6_ifreq ireq;
3081 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3084 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3087 cfg.pfx = &ireq.ifr6_addr;
3088 cfg.plen = ireq.ifr6_prefixlen;
3091 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
3096 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3098 struct in6_ifreq ireq;
3101 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3104 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3108 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3109 ireq.ifr6_prefixlen);
3114 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3115 int plen, int scope, u8 proto)
3117 struct inet6_ifaddr *ifp;
3118 struct ifa6_config cfg = {
3121 .ifa_flags = IFA_F_PERMANENT,
3122 .valid_lft = INFINITY_LIFE_TIME,
3123 .preferred_lft = INFINITY_LIFE_TIME,
3128 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3130 spin_lock_bh(&ifp->lock);
3131 ifp->flags &= ~IFA_F_TENTATIVE;
3132 spin_unlock_bh(&ifp->lock);
3133 rt_genid_bump_ipv6(dev_net(idev->dev));
3134 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3139 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3140 static void add_v4_addrs(struct inet6_dev *idev)
3142 struct in6_addr addr;
3143 struct net_device *dev;
3144 struct net *net = dev_net(idev->dev);
3145 int scope, plen, offset = 0;
3150 memset(&addr, 0, sizeof(struct in6_addr));
3151 /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3152 if (idev->dev->addr_len == sizeof(struct in6_addr))
3153 offset = sizeof(struct in6_addr) - 4;
3154 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr + offset, 4);
3156 if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
3157 scope = IPV6_ADDR_COMPATv4;
3159 pflags |= RTF_NONEXTHOP;
3161 if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
3164 addr.s6_addr32[0] = htonl(0xfe800000);
3169 if (addr.s6_addr32[3]) {
3170 add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
3171 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3176 for_each_netdev(net, dev) {
3177 struct in_device *in_dev = __in_dev_get_rtnl(dev);
3178 if (in_dev && (dev->flags & IFF_UP)) {
3179 struct in_ifaddr *ifa;
3182 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3183 addr.s6_addr32[3] = ifa->ifa_local;
3185 if (ifa->ifa_scope == RT_SCOPE_LINK)
3187 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3188 if (idev->dev->flags&IFF_POINTOPOINT)
3193 add_addr(idev, &addr, plen, flag,
3195 addrconf_prefix_route(&addr, plen, 0, idev->dev,
3196 0, pflags, GFP_KERNEL);
3203 static void init_loopback(struct net_device *dev)
3205 struct inet6_dev *idev;
3211 idev = ipv6_find_idev(dev);
3213 pr_debug("%s: add_dev failed\n", __func__);
3217 add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
3220 void addrconf_add_linklocal(struct inet6_dev *idev,
3221 const struct in6_addr *addr, u32 flags)
3223 struct ifa6_config cfg = {
3226 .ifa_flags = flags | IFA_F_PERMANENT,
3227 .valid_lft = INFINITY_LIFE_TIME,
3228 .preferred_lft = INFINITY_LIFE_TIME,
3230 .ifa_proto = IFAPROT_KERNEL_LL
3232 struct inet6_ifaddr *ifp;
3234 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3235 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3236 idev->cnf.optimistic_dad) &&
3237 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3238 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3241 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3243 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3245 addrconf_dad_start(ifp);
3249 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3251 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3253 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3256 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3257 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3260 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3261 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3267 static int ipv6_generate_stable_address(struct in6_addr *address,
3269 const struct inet6_dev *idev)
3271 static DEFINE_SPINLOCK(lock);
3272 static __u32 digest[SHA1_DIGEST_WORDS];
3273 static __u32 workspace[SHA1_WORKSPACE_WORDS];
3276 char __data[SHA1_BLOCK_SIZE];
3278 struct in6_addr secret;
3280 unsigned char hwaddr[MAX_ADDR_LEN];
3285 struct in6_addr secret;
3286 struct in6_addr temp;
3287 struct net *net = dev_net(idev->dev);
3289 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3291 if (idev->cnf.stable_secret.initialized)
3292 secret = idev->cnf.stable_secret.secret;
3293 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3294 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3299 spin_lock_bh(&lock);
3302 memset(&data, 0, sizeof(data));
3303 memset(workspace, 0, sizeof(workspace));
3304 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3305 data.prefix[0] = address->s6_addr32[0];
3306 data.prefix[1] = address->s6_addr32[1];
3307 data.secret = secret;
3308 data.dad_count = dad_count;
3310 sha1_transform(digest, data.__data, workspace);
3313 temp.s6_addr32[2] = (__force __be32)digest[0];
3314 temp.s6_addr32[3] = (__force __be32)digest[1];
3316 spin_unlock_bh(&lock);
3318 if (ipv6_reserved_interfaceid(temp)) {
3320 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3329 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3331 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3335 s = &idev->cnf.stable_secret;
3336 get_random_bytes(&s->secret, sizeof(s->secret));
3337 s->initialized = true;
3340 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3342 struct in6_addr addr;
3344 /* no link local addresses on L3 master devices */
3345 if (netif_is_l3_master(idev->dev))
3348 /* no link local addresses on devices flagged as slaves */
3349 if (idev->dev->flags & IFF_SLAVE)
3352 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3354 switch (idev->cnf.addr_gen_mode) {
3355 case IN6_ADDR_GEN_MODE_RANDOM:
3356 ipv6_gen_mode_random_init(idev);
3358 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3359 if (!ipv6_generate_stable_address(&addr, 0, idev))
3360 addrconf_add_linklocal(idev, &addr,
3361 IFA_F_STABLE_PRIVACY);
3362 else if (prefix_route)
3363 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3366 case IN6_ADDR_GEN_MODE_EUI64:
3367 /* addrconf_add_linklocal also adds a prefix_route and we
3368 * only need to care about prefix routes if ipv6_generate_eui64
3369 * couldn't generate one.
3371 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3372 addrconf_add_linklocal(idev, &addr, 0);
3373 else if (prefix_route)
3374 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3377 case IN6_ADDR_GEN_MODE_NONE:
3379 /* will not add any link local address */
3384 static void addrconf_dev_config(struct net_device *dev)
3386 struct inet6_dev *idev;
3390 if ((dev->type != ARPHRD_ETHER) &&
3391 (dev->type != ARPHRD_FDDI) &&
3392 (dev->type != ARPHRD_ARCNET) &&
3393 (dev->type != ARPHRD_INFINIBAND) &&
3394 (dev->type != ARPHRD_IEEE1394) &&
3395 (dev->type != ARPHRD_TUNNEL6) &&
3396 (dev->type != ARPHRD_6LOWPAN) &&
3397 (dev->type != ARPHRD_TUNNEL) &&
3398 (dev->type != ARPHRD_NONE) &&
3399 (dev->type != ARPHRD_RAWIP)) {
3400 /* Alas, we support only Ethernet autoconfiguration. */
3401 idev = __in6_dev_get(dev);
3402 if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3403 dev->flags & IFF_MULTICAST)
3408 idev = addrconf_add_dev(dev);
3412 /* this device type has no EUI support */
3413 if (dev->type == ARPHRD_NONE &&
3414 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3415 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3417 addrconf_addr_gen(idev, false);
3420 #if IS_ENABLED(CONFIG_IPV6_SIT)
3421 static void addrconf_sit_config(struct net_device *dev)
3423 struct inet6_dev *idev;
3428 * Configure the tunnel with one of our IPv4
3429 * addresses... we should configure all of
3430 * our v4 addrs in the tunnel
3433 idev = ipv6_find_idev(dev);
3435 pr_debug("%s: add_dev failed\n", __func__);
3439 if (dev->priv_flags & IFF_ISATAP) {
3440 addrconf_addr_gen(idev, false);
3446 if (dev->flags&IFF_POINTOPOINT)
3447 addrconf_add_mroute(dev);
3451 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3452 static void addrconf_gre_config(struct net_device *dev)
3454 struct inet6_dev *idev;
3458 idev = ipv6_find_idev(dev);
3460 pr_debug("%s: add_dev failed\n", __func__);
3464 if (dev->type == ARPHRD_ETHER) {
3465 addrconf_addr_gen(idev, true);
3471 if (dev->flags & IFF_POINTOPOINT)
3472 addrconf_add_mroute(dev);
3476 static void addrconf_init_auto_addrs(struct net_device *dev)
3478 switch (dev->type) {
3479 #if IS_ENABLED(CONFIG_IPV6_SIT)
3481 addrconf_sit_config(dev);
3484 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3487 addrconf_gre_config(dev);
3490 case ARPHRD_LOOPBACK:
3495 addrconf_dev_config(dev);
3500 static int fixup_permanent_addr(struct net *net,
3501 struct inet6_dev *idev,
3502 struct inet6_ifaddr *ifp)
3504 /* !fib6_node means the host route was removed from the
3505 * FIB, for example, if 'lo' device is taken down. In that
3506 * case regenerate the host route.
3508 if (!ifp->rt || !ifp->rt->fib6_node) {
3509 struct fib6_info *f6i, *prev;
3511 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3514 return PTR_ERR(f6i);
3516 /* ifp->rt can be accessed outside of rtnl */
3517 spin_lock(&ifp->lock);
3520 spin_unlock(&ifp->lock);
3522 fib6_info_release(prev);
3525 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3526 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3527 ifp->rt_priority, idev->dev, 0, 0,
3531 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3532 addrconf_dad_start(ifp);
3537 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3539 struct inet6_ifaddr *ifp, *tmp;
3540 struct inet6_dev *idev;
3542 idev = __in6_dev_get(dev);
3546 write_lock_bh(&idev->lock);
3548 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3549 if ((ifp->flags & IFA_F_PERMANENT) &&
3550 fixup_permanent_addr(net, idev, ifp) < 0) {
3551 write_unlock_bh(&idev->lock);
3554 write_lock_bh(&idev->lock);
3556 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3557 idev->dev->name, &ifp->addr);
3561 write_unlock_bh(&idev->lock);
3564 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3567 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3568 struct netdev_notifier_change_info *change_info;
3569 struct netdev_notifier_changeupper_info *info;
3570 struct inet6_dev *idev = __in6_dev_get(dev);
3571 struct net *net = dev_net(dev);
3572 int run_pending = 0;
3576 case NETDEV_REGISTER:
3577 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3578 idev = ipv6_add_dev(dev);
3580 return notifier_from_errno(PTR_ERR(idev));
3584 case NETDEV_CHANGEMTU:
3585 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3586 if (dev->mtu < IPV6_MIN_MTU) {
3587 addrconf_ifdown(dev, dev != net->loopback_dev);
3592 rt6_mtu_change(dev, dev->mtu);
3593 idev->cnf.mtu6 = dev->mtu;
3597 /* allocate new idev */
3598 idev = ipv6_add_dev(dev);
3602 /* device is still not ready */
3603 if (!(idev->if_flags & IF_READY))
3610 if (idev && idev->cnf.disable_ipv6)
3613 if (dev->flags & IFF_SLAVE) {
3614 if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3615 dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3620 if (event == NETDEV_UP) {
3621 /* restore routes for permanent addresses */
3622 addrconf_permanent_addr(net, dev);
3624 if (!addrconf_link_ready(dev)) {
3625 /* device is not ready yet. */
3626 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3631 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3632 idev = ipv6_add_dev(dev);
3634 if (!IS_ERR_OR_NULL(idev)) {
3635 idev->if_flags |= IF_READY;
3638 } else if (event == NETDEV_CHANGE) {
3639 if (!addrconf_link_ready(dev)) {
3640 /* device is still not ready. */
3641 rt6_sync_down_dev(dev, event);
3645 if (!IS_ERR_OR_NULL(idev)) {
3646 if (idev->if_flags & IF_READY) {
3647 /* device is already configured -
3648 * but resend MLD reports, we might
3649 * have roamed and need to update
3650 * multicast snooping switches
3654 if (change_info->flags_changed & IFF_NOARP)
3655 addrconf_dad_run(idev, true);
3656 rt6_sync_up(dev, RTNH_F_LINKDOWN);
3659 idev->if_flags |= IF_READY;
3662 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3668 addrconf_init_auto_addrs(dev);
3670 if (!IS_ERR_OR_NULL(idev)) {
3672 addrconf_dad_run(idev, false);
3674 /* Device has an address by now */
3675 rt6_sync_up(dev, RTNH_F_DEAD);
3678 * If the MTU changed during the interface down,
3679 * when the interface up, the changed MTU must be
3680 * reflected in the idev as well as routers.
3682 if (idev->cnf.mtu6 != dev->mtu &&
3683 dev->mtu >= IPV6_MIN_MTU) {
3684 rt6_mtu_change(dev, dev->mtu);
3685 idev->cnf.mtu6 = dev->mtu;
3687 idev->tstamp = jiffies;
3688 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3691 * If the changed mtu during down is lower than
3692 * IPV6_MIN_MTU stop IPv6 on this interface.
3694 if (dev->mtu < IPV6_MIN_MTU)
3695 addrconf_ifdown(dev, dev != net->loopback_dev);
3700 case NETDEV_UNREGISTER:
3702 * Remove all addresses from this interface.
3704 addrconf_ifdown(dev, event != NETDEV_DOWN);
3707 case NETDEV_CHANGENAME:
3709 snmp6_unregister_dev(idev);
3710 addrconf_sysctl_unregister(idev);
3711 err = addrconf_sysctl_register(idev);
3713 return notifier_from_errno(err);
3714 err = snmp6_register_dev(idev);
3716 addrconf_sysctl_unregister(idev);
3717 return notifier_from_errno(err);
3722 case NETDEV_PRE_TYPE_CHANGE:
3723 case NETDEV_POST_TYPE_CHANGE:
3725 addrconf_type_change(dev, event);
3728 case NETDEV_CHANGEUPPER:
3731 /* flush all routes if dev is linked to or unlinked from
3732 * an L3 master device (e.g., VRF)
3734 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3735 addrconf_ifdown(dev, false);
3742 * addrconf module should be notified of a device going up
3744 static struct notifier_block ipv6_dev_notf = {
3745 .notifier_call = addrconf_notify,
3746 .priority = ADDRCONF_NOTIFY_PRIORITY,
3749 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3751 struct inet6_dev *idev;
3754 idev = __in6_dev_get(dev);
3756 if (event == NETDEV_POST_TYPE_CHANGE)
3757 ipv6_mc_remap(idev);
3758 else if (event == NETDEV_PRE_TYPE_CHANGE)
3759 ipv6_mc_unmap(idev);
3762 static bool addr_is_local(const struct in6_addr *addr)
3764 return ipv6_addr_type(addr) &
3765 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3768 static int addrconf_ifdown(struct net_device *dev, bool unregister)
3770 unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3771 struct net *net = dev_net(dev);
3772 struct inet6_dev *idev;
3773 struct inet6_ifaddr *ifa;
3774 LIST_HEAD(tmp_addr_list);
3775 bool keep_addr = false;
3781 rt6_disable_ip(dev, event);
3783 idev = __in6_dev_get(dev);
3788 * Step 1: remove reference to ipv6 device from parent device.
3794 /* protected by rtnl_lock */
3795 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3797 /* Step 1.5: remove snmp6 entry */
3798 snmp6_unregister_dev(idev);
3802 /* combine the user config with event to determine if permanent
3803 * addresses are to be removed from address hash table
3805 if (!unregister && !idev->cnf.disable_ipv6) {
3806 /* aggregate the system setting and interface setting */
3807 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3810 _keep_addr = idev->cnf.keep_addr_on_down;
3812 keep_addr = (_keep_addr > 0);
3815 /* Step 2: clear hash table */
3816 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3817 struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
3819 spin_lock_bh(&net->ipv6.addrconf_hash_lock);
3821 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3822 if (ifa->idev == idev) {
3823 addrconf_del_dad_work(ifa);
3824 /* combined flag + permanent flag decide if
3825 * address is retained on a down event
3828 !(ifa->flags & IFA_F_PERMANENT) ||
3829 addr_is_local(&ifa->addr)) {
3830 hlist_del_init_rcu(&ifa->addr_lst);
3835 spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
3838 write_lock_bh(&idev->lock);
3840 addrconf_del_rs_timer(idev);
3842 /* Step 2: clear flags for stateless addrconf, repeated down
3845 was_ready = idev->if_flags & IF_READY;
3847 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3849 /* Step 3: clear tempaddr list */
3850 while (!list_empty(&idev->tempaddr_list)) {
3851 ifa = list_first_entry(&idev->tempaddr_list,
3852 struct inet6_ifaddr, tmp_list);
3853 list_del(&ifa->tmp_list);
3854 write_unlock_bh(&idev->lock);
3855 spin_lock_bh(&ifa->lock);
3858 in6_ifa_put(ifa->ifpub);
3861 spin_unlock_bh(&ifa->lock);
3863 write_lock_bh(&idev->lock);
3866 list_for_each_entry(ifa, &idev->addr_list, if_list)
3867 list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
3868 write_unlock_bh(&idev->lock);
3870 while (!list_empty(&tmp_addr_list)) {
3871 struct fib6_info *rt = NULL;
3874 ifa = list_first_entry(&tmp_addr_list,
3875 struct inet6_ifaddr, if_list_aux);
3876 list_del(&ifa->if_list_aux);
3878 addrconf_del_dad_work(ifa);
3880 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3881 !addr_is_local(&ifa->addr);
3883 spin_lock_bh(&ifa->lock);
3886 /* set state to skip the notifier below */
3887 state = INET6_IFADDR_STATE_DEAD;
3888 ifa->state = INET6_IFADDR_STATE_PREDAD;
3889 if (!(ifa->flags & IFA_F_NODAD))
3890 ifa->flags |= IFA_F_TENTATIVE;
3896 ifa->state = INET6_IFADDR_STATE_DEAD;
3899 spin_unlock_bh(&ifa->lock);
3902 ip6_del_rt(net, rt, false);
3904 if (state != INET6_IFADDR_STATE_DEAD) {
3905 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3906 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3908 if (idev->cnf.forwarding)
3909 addrconf_leave_anycast(ifa);
3910 addrconf_leave_solict(ifa->idev, &ifa->addr);
3914 write_lock_bh(&idev->lock);
3915 list_del_rcu(&ifa->if_list);
3916 write_unlock_bh(&idev->lock);
3921 /* Step 5: Discard anycast and multicast list */
3923 ipv6_ac_destroy_dev(idev);
3924 ipv6_mc_destroy_dev(idev);
3925 } else if (was_ready) {
3929 idev->tstamp = jiffies;
3932 /* Last: Shot the device (if unregistered) */
3934 addrconf_sysctl_unregister(idev);
3935 neigh_parms_release(&nd_tbl, idev->nd_parms);
3936 neigh_ifdown(&nd_tbl, dev);
3942 static void addrconf_rs_timer(struct timer_list *t)
3944 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3945 struct net_device *dev = idev->dev;
3946 struct in6_addr lladdr;
3948 write_lock(&idev->lock);
3949 if (idev->dead || !(idev->if_flags & IF_READY))
3952 if (!ipv6_accept_ra(idev))
3955 /* Announcement received after solicitation was sent */
3956 if (idev->if_flags & IF_RA_RCVD)
3959 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3960 write_unlock(&idev->lock);
3961 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3962 ndisc_send_rs(dev, &lladdr,
3963 &in6addr_linklocal_allrouters);
3967 write_lock(&idev->lock);
3968 idev->rs_interval = rfc3315_s14_backoff_update(
3969 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3970 /* The wait after the last probe can be shorter */
3971 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3972 idev->cnf.rtr_solicits) ?
3973 idev->cnf.rtr_solicit_delay :
3977 * Note: we do not support deprecated "all on-link"
3978 * assumption any longer.
3980 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3984 write_unlock(&idev->lock);
3990 * Duplicate Address Detection
3992 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3994 unsigned long rand_num;
3995 struct inet6_dev *idev = ifp->idev;
3998 if (ifp->flags & IFA_F_OPTIMISTIC)
4001 rand_num = prandom_u32_max(idev->cnf.rtr_solicit_delay ?: 1);
4004 if (idev->cnf.enhanced_dad ||
4005 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
4007 get_random_bytes(&nonce, 6);
4010 ifp->dad_nonce = nonce;
4011 ifp->dad_probes = idev->cnf.dad_transmits;
4012 addrconf_mod_dad_work(ifp, rand_num);
4015 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
4017 struct inet6_dev *idev = ifp->idev;
4018 struct net_device *dev = idev->dev;
4019 bool bump_id, notify = false;
4022 addrconf_join_solict(dev, &ifp->addr);
4024 read_lock_bh(&idev->lock);
4025 spin_lock(&ifp->lock);
4026 if (ifp->state == INET6_IFADDR_STATE_DEAD)
4030 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4031 (net->ipv6.devconf_all->accept_dad < 1 &&
4032 idev->cnf.accept_dad < 1) ||
4033 !(ifp->flags&IFA_F_TENTATIVE) ||
4034 ifp->flags & IFA_F_NODAD) {
4035 bool send_na = false;
4037 if (ifp->flags & IFA_F_TENTATIVE &&
4038 !(ifp->flags & IFA_F_OPTIMISTIC))
4040 bump_id = ifp->flags & IFA_F_TENTATIVE;
4041 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4042 spin_unlock(&ifp->lock);
4043 read_unlock_bh(&idev->lock);
4045 addrconf_dad_completed(ifp, bump_id, send_na);
4049 if (!(idev->if_flags & IF_READY)) {
4050 spin_unlock(&ifp->lock);
4051 read_unlock_bh(&idev->lock);
4053 * If the device is not ready:
4054 * - keep it tentative if it is a permanent address.
4055 * - otherwise, kill it.
4058 addrconf_dad_stop(ifp, 0);
4063 * Optimistic nodes can start receiving
4066 if (ifp->flags & IFA_F_OPTIMISTIC) {
4067 ip6_ins_rt(net, ifp->rt);
4068 if (ipv6_use_optimistic_addr(net, idev)) {
4069 /* Because optimistic nodes can use this address,
4070 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4076 addrconf_dad_kick(ifp);
4078 spin_unlock(&ifp->lock);
4079 read_unlock_bh(&idev->lock);
4081 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4084 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4086 bool begin_dad = false;
4088 spin_lock_bh(&ifp->lock);
4089 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4090 ifp->state = INET6_IFADDR_STATE_PREDAD;
4093 spin_unlock_bh(&ifp->lock);
4096 addrconf_mod_dad_work(ifp, 0);
4099 static void addrconf_dad_work(struct work_struct *w)
4101 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4102 struct inet6_ifaddr,
4104 struct inet6_dev *idev = ifp->idev;
4105 bool bump_id, disable_ipv6 = false;
4106 struct in6_addr mcaddr;
4112 } action = DAD_PROCESS;
4116 spin_lock_bh(&ifp->lock);
4117 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4119 ifp->state = INET6_IFADDR_STATE_DAD;
4120 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4122 ifp->state = INET6_IFADDR_STATE_POSTDAD;
4124 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4125 idev->cnf.accept_dad > 1) &&
4126 !idev->cnf.disable_ipv6 &&
4127 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4128 struct in6_addr addr;
4130 addr.s6_addr32[0] = htonl(0xfe800000);
4131 addr.s6_addr32[1] = 0;
4133 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4134 ipv6_addr_equal(&ifp->addr, &addr)) {
4135 /* DAD failed for link-local based on MAC */
4136 idev->cnf.disable_ipv6 = 1;
4138 pr_info("%s: IPv6 being disabled!\n",
4139 ifp->idev->dev->name);
4140 disable_ipv6 = true;
4144 spin_unlock_bh(&ifp->lock);
4146 if (action == DAD_BEGIN) {
4147 addrconf_dad_begin(ifp);
4149 } else if (action == DAD_ABORT) {
4151 addrconf_dad_stop(ifp, 1);
4153 addrconf_ifdown(idev->dev, false);
4157 if (!ifp->dad_probes && addrconf_dad_end(ifp))
4160 write_lock_bh(&idev->lock);
4161 if (idev->dead || !(idev->if_flags & IF_READY)) {
4162 write_unlock_bh(&idev->lock);
4166 spin_lock(&ifp->lock);
4167 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4168 spin_unlock(&ifp->lock);
4169 write_unlock_bh(&idev->lock);
4173 if (ifp->dad_probes == 0) {
4174 bool send_na = false;
4177 * DAD was successful
4180 if (ifp->flags & IFA_F_TENTATIVE &&
4181 !(ifp->flags & IFA_F_OPTIMISTIC))
4183 bump_id = ifp->flags & IFA_F_TENTATIVE;
4184 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4185 spin_unlock(&ifp->lock);
4186 write_unlock_bh(&idev->lock);
4188 addrconf_dad_completed(ifp, bump_id, send_na);
4194 addrconf_mod_dad_work(ifp,
4195 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
4197 spin_unlock(&ifp->lock);
4198 write_unlock_bh(&idev->lock);
4200 /* send a neighbour solicitation for our addr */
4201 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4202 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4209 /* ifp->idev must be at least read locked */
4210 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4212 struct inet6_ifaddr *ifpiter;
4213 struct inet6_dev *idev = ifp->idev;
4215 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4216 if (ifpiter->scope > IFA_LINK)
4218 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4219 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4220 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4227 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4230 struct net_device *dev = ifp->idev->dev;
4231 struct in6_addr lladdr;
4232 bool send_rs, send_mld;
4234 addrconf_del_dad_work(ifp);
4237 * Configure the address for reception. Now it is valid.
4240 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4242 /* If added prefix is link local and we are prepared to process
4243 router advertisements, start sending router solicitations.
4246 read_lock_bh(&ifp->idev->lock);
4247 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4248 send_rs = send_mld &&
4249 ipv6_accept_ra(ifp->idev) &&
4250 ifp->idev->cnf.rtr_solicits != 0 &&
4251 (dev->flags & IFF_LOOPBACK) == 0 &&
4252 (dev->type != ARPHRD_TUNNEL);
4253 read_unlock_bh(&ifp->idev->lock);
4255 /* While dad is in progress mld report's source address is in6_addrany.
4256 * Resend with proper ll now.
4259 ipv6_mc_dad_complete(ifp->idev);
4261 /* send unsolicited NA if enabled */
4263 (ifp->idev->cnf.ndisc_notify ||
4264 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4265 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4266 /*router=*/ !!ifp->idev->cnf.forwarding,
4267 /*solicited=*/ false, /*override=*/ true,
4273 * If a host as already performed a random delay
4274 * [...] as part of DAD [...] there is no need
4275 * to delay again before sending the first RS
4277 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4279 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4281 write_lock_bh(&ifp->idev->lock);
4282 spin_lock(&ifp->lock);
4283 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4284 ifp->idev->cnf.rtr_solicit_interval);
4285 ifp->idev->rs_probes = 1;
4286 ifp->idev->if_flags |= IF_RS_SENT;
4287 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4288 spin_unlock(&ifp->lock);
4289 write_unlock_bh(&ifp->idev->lock);
4293 rt_genid_bump_ipv6(dev_net(dev));
4295 /* Make sure that a new temporary address will be created
4296 * before this temporary address becomes deprecated.
4298 if (ifp->flags & IFA_F_TEMPORARY)
4299 addrconf_verify_rtnl(dev_net(dev));
4302 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4304 struct inet6_ifaddr *ifp;
4306 read_lock_bh(&idev->lock);
4307 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4308 spin_lock(&ifp->lock);
4309 if ((ifp->flags & IFA_F_TENTATIVE &&
4310 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4312 ifp->state = INET6_IFADDR_STATE_PREDAD;
4313 addrconf_dad_kick(ifp);
4315 spin_unlock(&ifp->lock);
4317 read_unlock_bh(&idev->lock);
4320 #ifdef CONFIG_PROC_FS
4321 struct if6_iter_state {
4322 struct seq_net_private p;
4327 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4329 struct if6_iter_state *state = seq->private;
4330 struct net *net = seq_file_net(seq);
4331 struct inet6_ifaddr *ifa = NULL;
4334 /* initial bucket if pos is 0 */
4340 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4341 hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
4343 /* sync with offset */
4344 if (p < state->offset) {
4351 /* prepare for next bucket */
4358 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4359 struct inet6_ifaddr *ifa)
4361 struct if6_iter_state *state = seq->private;
4362 struct net *net = seq_file_net(seq);
4364 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4370 while (++state->bucket < IN6_ADDR_HSIZE) {
4371 hlist_for_each_entry_rcu(ifa,
4372 &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4380 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4384 return if6_get_first(seq, *pos);
4387 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4389 struct inet6_ifaddr *ifa;
4391 ifa = if6_get_next(seq, v);
4396 static void if6_seq_stop(struct seq_file *seq, void *v)
4402 static int if6_seq_show(struct seq_file *seq, void *v)
4404 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4405 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4407 ifp->idev->dev->ifindex,
4411 ifp->idev->dev->name);
4415 static const struct seq_operations if6_seq_ops = {
4416 .start = if6_seq_start,
4417 .next = if6_seq_next,
4418 .show = if6_seq_show,
4419 .stop = if6_seq_stop,
4422 static int __net_init if6_proc_net_init(struct net *net)
4424 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4425 sizeof(struct if6_iter_state)))
4430 static void __net_exit if6_proc_net_exit(struct net *net)
4432 remove_proc_entry("if_inet6", net->proc_net);
4435 static struct pernet_operations if6_proc_net_ops = {
4436 .init = if6_proc_net_init,
4437 .exit = if6_proc_net_exit,
4440 int __init if6_proc_init(void)
4442 return register_pernet_subsys(&if6_proc_net_ops);
4445 void if6_proc_exit(void)
4447 unregister_pernet_subsys(&if6_proc_net_ops);
4449 #endif /* CONFIG_PROC_FS */
4451 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4452 /* Check if address is a home address configured on any interface. */
4453 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4455 unsigned int hash = inet6_addr_hash(net, addr);
4456 struct inet6_ifaddr *ifp = NULL;
4460 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4461 if (ipv6_addr_equal(&ifp->addr, addr) &&
4462 (ifp->flags & IFA_F_HOMEADDRESS)) {
4472 /* RFC6554 has some algorithm to avoid loops in segment routing by
4473 * checking if the segments contains any of a local interface address.
4477 * To detect loops in the SRH, a router MUST determine if the SRH
4478 * includes multiple addresses assigned to any interface on that router.
4479 * If such addresses appear more than once and are separated by at least
4480 * one address not assigned to that router.
4482 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4483 unsigned char nsegs)
4485 const struct in6_addr *addr;
4486 int i, ret = 0, found = 0;
4487 struct inet6_ifaddr *ifp;
4488 bool separated = false;
4493 for (i = 0; i < nsegs; i++) {
4495 hash = inet6_addr_hash(net, addr);
4498 hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4500 if (ipv6_addr_equal(&ifp->addr, addr)) {
4507 if (found > 1 && separated) {
4524 * Periodic address status verification
4527 static void addrconf_verify_rtnl(struct net *net)
4529 unsigned long now, next, next_sec, next_sched;
4530 struct inet6_ifaddr *ifp;
4537 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4539 cancel_delayed_work(&net->ipv6.addr_chk_work);
4541 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4543 hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
4546 /* When setting preferred_lft to a value not zero or
4547 * infinity, while valid_lft is infinity
4548 * IFA_F_PERMANENT has a non-infinity life time.
4550 if ((ifp->flags & IFA_F_PERMANENT) &&
4551 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4554 spin_lock(&ifp->lock);
4555 /* We try to batch several events at once. */
4556 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4558 if ((ifp->flags&IFA_F_TEMPORARY) &&
4559 !(ifp->flags&IFA_F_TENTATIVE) &&
4560 ifp->prefered_lft != INFINITY_LIFE_TIME &&
4561 !ifp->regen_count && ifp->ifpub) {
4562 /* This is a non-regenerated temporary addr. */
4564 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4565 ifp->idev->cnf.dad_transmits *
4566 max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
4568 if (age + regen_advance >= ifp->prefered_lft) {
4569 struct inet6_ifaddr *ifpub = ifp->ifpub;
4570 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4571 next = ifp->tstamp + ifp->prefered_lft * HZ;
4575 in6_ifa_hold(ifpub);
4576 spin_unlock(&ifp->lock);
4578 spin_lock(&ifpub->lock);
4579 ifpub->regen_count = 0;
4580 spin_unlock(&ifpub->lock);
4581 rcu_read_unlock_bh();
4582 ipv6_create_tempaddr(ifpub, true);
4587 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4588 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4591 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4592 age >= ifp->valid_lft) {
4593 spin_unlock(&ifp->lock);
4595 rcu_read_unlock_bh();
4599 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4600 spin_unlock(&ifp->lock);
4602 } else if (age >= ifp->prefered_lft) {
4603 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4606 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4608 ifp->flags |= IFA_F_DEPRECATED;
4611 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4612 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4613 next = ifp->tstamp + ifp->valid_lft * HZ;
4615 spin_unlock(&ifp->lock);
4620 ipv6_ifa_notify(0, ifp);
4625 /* ifp->prefered_lft <= ifp->valid_lft */
4626 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4627 next = ifp->tstamp + ifp->prefered_lft * HZ;
4628 spin_unlock(&ifp->lock);
4633 next_sec = round_jiffies_up(next);
4636 /* If rounded timeout is accurate enough, accept it. */
4637 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4638 next_sched = next_sec;
4640 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4641 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4642 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4644 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4645 now, next, next_sec, next_sched);
4646 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
4647 rcu_read_unlock_bh();
4650 static void addrconf_verify_work(struct work_struct *w)
4652 struct net *net = container_of(to_delayed_work(w), struct net,
4653 ipv6.addr_chk_work);
4656 addrconf_verify_rtnl(net);
4660 static void addrconf_verify(struct net *net)
4662 mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4665 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4666 struct in6_addr **peer_pfx)
4668 struct in6_addr *pfx = NULL;
4673 pfx = nla_data(addr);
4676 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4678 pfx = nla_data(local);
4684 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4685 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4686 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4687 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4688 [IFA_FLAGS] = { .len = sizeof(u32) },
4689 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
4690 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
4691 [IFA_PROTO] = { .type = NLA_U8 },
4695 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4696 struct netlink_ext_ack *extack)
4698 struct net *net = sock_net(skb->sk);
4699 struct ifaddrmsg *ifm;
4700 struct nlattr *tb[IFA_MAX+1];
4701 struct in6_addr *pfx, *peer_pfx;
4705 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4706 ifa_ipv6_policy, extack);
4710 ifm = nlmsg_data(nlh);
4711 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4715 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4717 /* We ignore other flags so far. */
4718 ifa_flags &= IFA_F_MANAGETEMPADDR;
4720 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4721 ifm->ifa_prefixlen);
4724 static int modify_prefix_route(struct inet6_ifaddr *ifp,
4725 unsigned long expires, u32 flags,
4728 struct fib6_info *f6i;
4731 f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4733 ifp->idev->dev, 0, RTF_DEFAULT, true);
4737 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4738 if (f6i->fib6_metric != prio) {
4739 /* delete old one */
4740 ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4743 addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4745 ifp->rt_priority, ifp->idev->dev,
4746 expires, flags, GFP_KERNEL);
4749 fib6_clean_expires(f6i);
4751 fib6_set_expires(f6i, expires);
4753 fib6_info_release(f6i);
4759 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
4760 struct ifa6_config *cfg)
4764 unsigned long timeout;
4765 bool was_managetempaddr;
4766 bool had_prefixroute;
4767 bool new_peer = false;
4771 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4774 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4775 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4778 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4779 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4781 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
4782 if (addrconf_finite_timeout(timeout)) {
4783 expires = jiffies_to_clock_t(timeout * HZ);
4784 cfg->valid_lft = timeout;
4785 flags = RTF_EXPIRES;
4789 cfg->ifa_flags |= IFA_F_PERMANENT;
4792 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
4793 if (addrconf_finite_timeout(timeout)) {
4795 cfg->ifa_flags |= IFA_F_DEPRECATED;
4796 cfg->preferred_lft = timeout;
4799 if (cfg->peer_pfx &&
4800 memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4801 if (!ipv6_addr_any(&ifp->peer_addr))
4802 cleanup_prefix_route(ifp, expires, true, true);
4806 spin_lock_bh(&ifp->lock);
4807 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4808 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4809 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4810 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4811 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4812 IFA_F_NOPREFIXROUTE);
4813 ifp->flags |= cfg->ifa_flags;
4814 ifp->tstamp = jiffies;
4815 ifp->valid_lft = cfg->valid_lft;
4816 ifp->prefered_lft = cfg->preferred_lft;
4817 ifp->ifa_proto = cfg->ifa_proto;
4819 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4820 ifp->rt_priority = cfg->rt_priority;
4823 ifp->peer_addr = *cfg->peer_pfx;
4825 spin_unlock_bh(&ifp->lock);
4826 if (!(ifp->flags&IFA_F_TENTATIVE))
4827 ipv6_ifa_notify(0, ifp);
4829 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4832 if (had_prefixroute)
4833 rc = modify_prefix_route(ifp, expires, flags, false);
4835 /* prefix route could have been deleted; if so restore it */
4836 if (rc == -ENOENT) {
4837 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4838 ifp->rt_priority, ifp->idev->dev,
4839 expires, flags, GFP_KERNEL);
4842 if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4843 rc = modify_prefix_route(ifp, expires, flags, true);
4845 if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4846 addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4847 ifp->rt_priority, ifp->idev->dev,
4848 expires, flags, GFP_KERNEL);
4850 } else if (had_prefixroute) {
4851 enum cleanup_prefix_rt_t action;
4852 unsigned long rt_expires;
4854 write_lock_bh(&ifp->idev->lock);
4855 action = check_cleanup_prefix_route(ifp, &rt_expires);
4856 write_unlock_bh(&ifp->idev->lock);
4858 if (action != CLEANUP_PREFIX_RT_NOP) {
4859 cleanup_prefix_route(ifp, rt_expires,
4860 action == CLEANUP_PREFIX_RT_DEL, false);
4864 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4865 if (was_managetempaddr &&
4866 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4868 cfg->preferred_lft = 0;
4870 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4871 cfg->preferred_lft, !was_managetempaddr,
4875 addrconf_verify_rtnl(net);
4881 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4882 struct netlink_ext_ack *extack)
4884 struct net *net = sock_net(skb->sk);
4885 struct ifaddrmsg *ifm;
4886 struct nlattr *tb[IFA_MAX+1];
4887 struct in6_addr *peer_pfx;
4888 struct inet6_ifaddr *ifa;
4889 struct net_device *dev;
4890 struct inet6_dev *idev;
4891 struct ifa6_config cfg;
4894 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4895 ifa_ipv6_policy, extack);
4899 memset(&cfg, 0, sizeof(cfg));
4901 ifm = nlmsg_data(nlh);
4902 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4906 cfg.peer_pfx = peer_pfx;
4907 cfg.plen = ifm->ifa_prefixlen;
4908 if (tb[IFA_RT_PRIORITY])
4909 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4912 cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
4914 cfg.valid_lft = INFINITY_LIFE_TIME;
4915 cfg.preferred_lft = INFINITY_LIFE_TIME;
4917 if (tb[IFA_CACHEINFO]) {
4918 struct ifa_cacheinfo *ci;
4920 ci = nla_data(tb[IFA_CACHEINFO]);
4921 cfg.valid_lft = ci->ifa_valid;
4922 cfg.preferred_lft = ci->ifa_prefered;
4925 dev = __dev_get_by_index(net, ifm->ifa_index);
4930 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4932 cfg.ifa_flags = ifm->ifa_flags;
4934 /* We ignore other flags so far. */
4935 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4936 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4937 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4939 idev = ipv6_find_idev(dev);
4941 return PTR_ERR(idev);
4943 if (!ipv6_allow_optimistic_dad(net, idev))
4944 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4946 if (cfg.ifa_flags & IFA_F_NODAD &&
4947 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4948 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4952 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
4955 * It would be best to check for !NLM_F_CREATE here but
4956 * userspace already relies on not having to provide this.
4958 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
4961 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4962 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4965 err = inet6_addr_modify(net, ifa, &cfg);
4972 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4973 u8 scope, int ifindex)
4975 struct ifaddrmsg *ifm;
4977 ifm = nlmsg_data(nlh);
4978 ifm->ifa_family = AF_INET6;
4979 ifm->ifa_prefixlen = prefixlen;
4980 ifm->ifa_flags = flags;
4981 ifm->ifa_scope = scope;
4982 ifm->ifa_index = ifindex;
4985 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4986 unsigned long tstamp, u32 preferred, u32 valid)
4988 struct ifa_cacheinfo ci;
4990 ci.cstamp = cstamp_delta(cstamp);
4991 ci.tstamp = cstamp_delta(tstamp);
4992 ci.ifa_prefered = preferred;
4993 ci.ifa_valid = valid;
4995 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4998 static inline int rt_scope(int ifa_scope)
5000 if (ifa_scope & IFA_HOST)
5001 return RT_SCOPE_HOST;
5002 else if (ifa_scope & IFA_LINK)
5003 return RT_SCOPE_LINK;
5004 else if (ifa_scope & IFA_SITE)
5005 return RT_SCOPE_SITE;
5007 return RT_SCOPE_UNIVERSE;
5010 static inline int inet6_ifaddr_msgsize(void)
5012 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
5013 + nla_total_size(16) /* IFA_LOCAL */
5014 + nla_total_size(16) /* IFA_ADDRESS */
5015 + nla_total_size(sizeof(struct ifa_cacheinfo))
5016 + nla_total_size(4) /* IFA_FLAGS */
5017 + nla_total_size(1) /* IFA_PROTO */
5018 + nla_total_size(4) /* IFA_RT_PRIORITY */;
5027 struct inet6_fill_args {
5034 enum addr_type_t type;
5037 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
5038 struct inet6_fill_args *args)
5040 struct nlmsghdr *nlh;
5041 u32 preferred, valid;
5043 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5044 sizeof(struct ifaddrmsg), args->flags);
5048 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5049 ifa->idev->dev->ifindex);
5051 if (args->netnsid >= 0 &&
5052 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
5055 spin_lock_bh(&ifa->lock);
5056 if (!((ifa->flags&IFA_F_PERMANENT) &&
5057 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
5058 preferred = ifa->prefered_lft;
5059 valid = ifa->valid_lft;
5060 if (preferred != INFINITY_LIFE_TIME) {
5061 long tval = (jiffies - ifa->tstamp)/HZ;
5062 if (preferred > tval)
5066 if (valid != INFINITY_LIFE_TIME) {
5074 preferred = INFINITY_LIFE_TIME;
5075 valid = INFINITY_LIFE_TIME;
5077 spin_unlock_bh(&ifa->lock);
5079 if (!ipv6_addr_any(&ifa->peer_addr)) {
5080 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5081 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5084 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5087 if (ifa->rt_priority &&
5088 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
5091 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
5094 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
5097 if (ifa->ifa_proto &&
5098 nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto))
5101 nlmsg_end(skb, nlh);
5105 nlmsg_cancel(skb, nlh);
5109 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
5110 struct inet6_fill_args *args)
5112 struct nlmsghdr *nlh;
5113 u8 scope = RT_SCOPE_UNIVERSE;
5114 int ifindex = ifmca->idev->dev->ifindex;
5116 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5117 scope = RT_SCOPE_SITE;
5119 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5120 sizeof(struct ifaddrmsg), args->flags);
5124 if (args->netnsid >= 0 &&
5125 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5126 nlmsg_cancel(skb, nlh);
5130 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5131 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
5132 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
5133 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5134 nlmsg_cancel(skb, nlh);
5138 nlmsg_end(skb, nlh);
5142 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
5143 struct inet6_fill_args *args)
5145 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5146 int ifindex = dev ? dev->ifindex : 1;
5147 struct nlmsghdr *nlh;
5148 u8 scope = RT_SCOPE_UNIVERSE;
5150 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5151 scope = RT_SCOPE_SITE;
5153 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5154 sizeof(struct ifaddrmsg), args->flags);
5158 if (args->netnsid >= 0 &&
5159 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5160 nlmsg_cancel(skb, nlh);
5164 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5165 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
5166 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
5167 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5168 nlmsg_cancel(skb, nlh);
5172 nlmsg_end(skb, nlh);
5176 /* called with rcu_read_lock() */
5177 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5178 struct netlink_callback *cb, int s_ip_idx,
5179 struct inet6_fill_args *fillargs)
5181 struct ifmcaddr6 *ifmca;
5182 struct ifacaddr6 *ifaca;
5186 read_lock_bh(&idev->lock);
5187 switch (fillargs->type) {
5188 case UNICAST_ADDR: {
5189 struct inet6_ifaddr *ifa;
5190 fillargs->event = RTM_NEWADDR;
5192 /* unicast address incl. temp addr */
5193 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5194 if (ip_idx < s_ip_idx)
5196 err = inet6_fill_ifaddr(skb, ifa, fillargs);
5199 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5205 case MULTICAST_ADDR:
5206 read_unlock_bh(&idev->lock);
5207 fillargs->event = RTM_GETMULTICAST;
5209 /* multicast address */
5210 for (ifmca = rtnl_dereference(idev->mc_list);
5212 ifmca = rtnl_dereference(ifmca->next), ip_idx++) {
5213 if (ip_idx < s_ip_idx)
5215 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5219 read_lock_bh(&idev->lock);
5222 fillargs->event = RTM_GETANYCAST;
5223 /* anycast address */
5224 for (ifaca = idev->ac_list; ifaca;
5225 ifaca = ifaca->aca_next, ip_idx++) {
5226 if (ip_idx < s_ip_idx)
5228 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5236 read_unlock_bh(&idev->lock);
5237 cb->args[2] = ip_idx;
5241 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5242 struct inet6_fill_args *fillargs,
5243 struct net **tgt_net, struct sock *sk,
5244 struct netlink_callback *cb)
5246 struct netlink_ext_ack *extack = cb->extack;
5247 struct nlattr *tb[IFA_MAX+1];
5248 struct ifaddrmsg *ifm;
5251 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5252 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5256 ifm = nlmsg_data(nlh);
5257 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5258 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5262 fillargs->ifindex = ifm->ifa_index;
5263 if (fillargs->ifindex) {
5264 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5265 fillargs->flags |= NLM_F_DUMP_FILTERED;
5268 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5269 ifa_ipv6_policy, extack);
5273 for (i = 0; i <= IFA_MAX; ++i) {
5277 if (i == IFA_TARGET_NETNSID) {
5280 fillargs->netnsid = nla_get_s32(tb[i]);
5281 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5283 fillargs->netnsid = -1;
5284 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5285 return PTR_ERR(net);
5289 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5297 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5298 enum addr_type_t type)
5300 const struct nlmsghdr *nlh = cb->nlh;
5301 struct inet6_fill_args fillargs = {
5302 .portid = NETLINK_CB(cb->skb).portid,
5303 .seq = cb->nlh->nlmsg_seq,
5304 .flags = NLM_F_MULTI,
5308 struct net *tgt_net = sock_net(skb->sk);
5309 int idx, s_idx, s_ip_idx;
5311 struct net_device *dev;
5312 struct inet6_dev *idev;
5313 struct hlist_head *head;
5317 s_idx = idx = cb->args[1];
5318 s_ip_idx = cb->args[2];
5320 if (cb->strict_check) {
5321 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5327 if (fillargs.ifindex) {
5328 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5333 idev = __in6_dev_get(dev);
5335 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5345 cb->seq = inet6_base_seq(tgt_net);
5346 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5348 head = &tgt_net->dev_index_head[h];
5349 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5352 if (h > s_h || idx > s_idx)
5354 idev = __in6_dev_get(dev);
5358 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
5370 if (fillargs.netnsid >= 0)
5373 return skb->len ? : err;
5376 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5378 enum addr_type_t type = UNICAST_ADDR;
5380 return inet6_dump_addr(skb, cb, type);
5383 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5385 enum addr_type_t type = MULTICAST_ADDR;
5387 return inet6_dump_addr(skb, cb, type);
5391 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5393 enum addr_type_t type = ANYCAST_ADDR;
5395 return inet6_dump_addr(skb, cb, type);
5398 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5399 const struct nlmsghdr *nlh,
5401 struct netlink_ext_ack *extack)
5403 struct ifaddrmsg *ifm;
5406 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5407 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5411 if (!netlink_strict_get_check(skb))
5412 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5413 ifa_ipv6_policy, extack);
5415 ifm = nlmsg_data(nlh);
5416 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5417 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5421 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5422 ifa_ipv6_policy, extack);
5426 for (i = 0; i <= IFA_MAX; i++) {
5431 case IFA_TARGET_NETNSID:
5436 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5444 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5445 struct netlink_ext_ack *extack)
5447 struct net *tgt_net = sock_net(in_skb->sk);
5448 struct inet6_fill_args fillargs = {
5449 .portid = NETLINK_CB(in_skb).portid,
5450 .seq = nlh->nlmsg_seq,
5451 .event = RTM_NEWADDR,
5455 struct ifaddrmsg *ifm;
5456 struct nlattr *tb[IFA_MAX+1];
5457 struct in6_addr *addr = NULL, *peer;
5458 struct net_device *dev = NULL;
5459 struct inet6_ifaddr *ifa;
5460 struct sk_buff *skb;
5463 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5467 if (tb[IFA_TARGET_NETNSID]) {
5468 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5470 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5472 if (IS_ERR(tgt_net))
5473 return PTR_ERR(tgt_net);
5476 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5481 ifm = nlmsg_data(nlh);
5483 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5485 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5487 err = -EADDRNOTAVAIL;
5491 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5497 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5499 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5500 WARN_ON(err == -EMSGSIZE);
5504 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5509 if (fillargs.netnsid >= 0)
5515 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5517 struct sk_buff *skb;
5518 struct net *net = dev_net(ifa->idev->dev);
5519 struct inet6_fill_args fillargs = {
5528 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5532 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5534 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5535 WARN_ON(err == -EMSGSIZE);
5539 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5543 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5546 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
5547 __s32 *array, int bytes)
5549 BUG_ON(bytes < (DEVCONF_MAX * 4));
5551 memset(array, 0, bytes);
5552 array[DEVCONF_FORWARDING] = cnf->forwarding;
5553 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5554 array[DEVCONF_MTU6] = cnf->mtu6;
5555 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5556 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5557 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5558 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5559 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
5560 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5561 jiffies_to_msecs(cnf->rtr_solicit_interval);
5562 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5563 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
5564 array[DEVCONF_RTR_SOLICIT_DELAY] =
5565 jiffies_to_msecs(cnf->rtr_solicit_delay);
5566 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5567 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5568 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5569 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5570 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
5571 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5572 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5573 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5574 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5575 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
5576 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
5577 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
5578 array[DEVCONF_RA_DEFRTR_METRIC] = cnf->ra_defrtr_metric;
5579 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5580 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5581 #ifdef CONFIG_IPV6_ROUTER_PREF
5582 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
5583 array[DEVCONF_RTR_PROBE_INTERVAL] =
5584 jiffies_to_msecs(cnf->rtr_probe_interval);
5585 #ifdef CONFIG_IPV6_ROUTE_INFO
5586 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
5587 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5590 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
5591 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
5592 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5593 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
5594 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
5596 #ifdef CONFIG_IPV6_MROUTE
5597 array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
5599 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
5600 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5601 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
5602 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5603 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5604 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5605 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
5606 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
5607 /* we omit DEVCONF_STABLE_SECRET for now */
5608 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5609 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
5610 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5611 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5612 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5613 #ifdef CONFIG_IPV6_SEG6_HMAC
5614 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5616 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5617 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5618 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
5619 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
5620 array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
5621 array[DEVCONF_IOAM6_ENABLED] = cnf->ioam6_enabled;
5622 array[DEVCONF_IOAM6_ID] = cnf->ioam6_id;
5623 array[DEVCONF_IOAM6_ID_WIDE] = cnf->ioam6_id_wide;
5624 array[DEVCONF_NDISC_EVICT_NOCARRIER] = cnf->ndisc_evict_nocarrier;
5625 array[DEVCONF_ACCEPT_UNTRACKED_NA] = cnf->accept_untracked_na;
5626 array[DEVCONF_ACCEPT_RA_MIN_LFT] = cnf->accept_ra_min_lft;
5629 static inline size_t inet6_ifla6_size(void)
5631 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5632 + nla_total_size(sizeof(struct ifla_cacheinfo))
5633 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5634 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5635 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5636 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5637 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5638 + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5642 static inline size_t inet6_if_nlmsg_size(void)
5644 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5645 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5646 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5647 + nla_total_size(4) /* IFLA_MTU */
5648 + nla_total_size(4) /* IFLA_LINK */
5649 + nla_total_size(1) /* IFLA_OPERSTATE */
5650 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5653 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5657 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5660 /* Use put_unaligned() because stats may not be aligned for u64. */
5661 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5662 for (i = 1; i < ICMP6_MIB_MAX; i++)
5663 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5665 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5668 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5669 int bytes, size_t syncpoff)
5672 u64 buff[IPSTATS_MIB_MAX];
5673 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5677 memset(buff, 0, sizeof(buff));
5678 buff[0] = IPSTATS_MIB_MAX;
5680 for_each_possible_cpu(c) {
5681 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5682 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5685 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5686 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5689 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5693 case IFLA_INET6_STATS:
5694 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5695 offsetof(struct ipstats_mib, syncp));
5697 case IFLA_INET6_ICMP6STATS:
5698 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5703 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5704 u32 ext_filter_mask)
5707 struct ifla_cacheinfo ci;
5709 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5710 goto nla_put_failure;
5711 ci.max_reasm_len = IPV6_MAXPLEN;
5712 ci.tstamp = cstamp_delta(idev->tstamp);
5713 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5714 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5715 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5716 goto nla_put_failure;
5717 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5719 goto nla_put_failure;
5720 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5722 /* XXX - MC not implemented */
5724 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5727 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5729 goto nla_put_failure;
5730 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5732 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5734 goto nla_put_failure;
5735 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5737 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5739 goto nla_put_failure;
5740 read_lock_bh(&idev->lock);
5741 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5742 read_unlock_bh(&idev->lock);
5744 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5745 goto nla_put_failure;
5748 nla_put_u32(skb, IFLA_INET6_RA_MTU, idev->ra_mtu))
5749 goto nla_put_failure;
5757 static size_t inet6_get_link_af_size(const struct net_device *dev,
5758 u32 ext_filter_mask)
5760 if (!__in6_dev_get(dev))
5763 return inet6_ifla6_size();
5766 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5767 u32 ext_filter_mask)
5769 struct inet6_dev *idev = __in6_dev_get(dev);
5774 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5780 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
5781 struct netlink_ext_ack *extack)
5783 struct inet6_ifaddr *ifp;
5784 struct net_device *dev = idev->dev;
5785 bool clear_token, update_rs = false;
5786 struct in6_addr ll_addr;
5793 if (dev->flags & IFF_LOOPBACK) {
5794 NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5798 if (dev->flags & IFF_NOARP) {
5799 NL_SET_ERR_MSG_MOD(extack,
5800 "Device does not do neighbour discovery");
5804 if (!ipv6_accept_ra(idev)) {
5805 NL_SET_ERR_MSG_MOD(extack,
5806 "Router advertisement is disabled on device");
5810 if (idev->cnf.rtr_solicits == 0) {
5811 NL_SET_ERR_MSG(extack,
5812 "Router solicitation is disabled on device");
5816 write_lock_bh(&idev->lock);
5818 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5819 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5821 write_unlock_bh(&idev->lock);
5823 clear_token = ipv6_addr_any(token);
5827 if (!idev->dead && (idev->if_flags & IF_READY) &&
5828 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5829 IFA_F_OPTIMISTIC)) {
5830 /* If we're not ready, then normal ifup will take care
5831 * of this. Otherwise, we need to request our rs here.
5833 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5838 write_lock_bh(&idev->lock);
5841 idev->if_flags |= IF_RS_SENT;
5842 idev->rs_interval = rfc3315_s14_backoff_init(
5843 idev->cnf.rtr_solicit_interval);
5844 idev->rs_probes = 1;
5845 addrconf_mod_rs_timer(idev, idev->rs_interval);
5848 /* Well, that's kinda nasty ... */
5849 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5850 spin_lock(&ifp->lock);
5851 if (ifp->tokenized) {
5853 ifp->prefered_lft = 0;
5855 spin_unlock(&ifp->lock);
5858 write_unlock_bh(&idev->lock);
5859 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5860 addrconf_verify_rtnl(dev_net(dev));
5864 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5865 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5866 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5867 [IFLA_INET6_RA_MTU] = { .type = NLA_REJECT,
5869 "IFLA_INET6_RA_MTU can not be set" },
5872 static int check_addr_gen_mode(int mode)
5874 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5875 mode != IN6_ADDR_GEN_MODE_NONE &&
5876 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5877 mode != IN6_ADDR_GEN_MODE_RANDOM)
5882 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5885 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5886 !idev->cnf.stable_secret.initialized &&
5887 !net->ipv6.devconf_dflt->stable_secret.initialized)
5892 static int inet6_validate_link_af(const struct net_device *dev,
5893 const struct nlattr *nla,
5894 struct netlink_ext_ack *extack)
5896 struct nlattr *tb[IFLA_INET6_MAX + 1];
5897 struct inet6_dev *idev = NULL;
5901 idev = __in6_dev_get(dev);
5903 return -EAFNOSUPPORT;
5906 err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5907 inet6_af_policy, extack);
5911 if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
5914 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5915 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5917 if (check_addr_gen_mode(mode) < 0)
5919 if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
5926 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
5927 struct netlink_ext_ack *extack)
5929 struct inet6_dev *idev = __in6_dev_get(dev);
5930 struct nlattr *tb[IFLA_INET6_MAX + 1];
5934 return -EAFNOSUPPORT;
5936 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5939 if (tb[IFLA_INET6_TOKEN]) {
5940 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
5946 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5947 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5949 idev->cnf.addr_gen_mode = mode;
5955 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5956 u32 portid, u32 seq, int event, unsigned int flags)
5958 struct net_device *dev = idev->dev;
5959 struct ifinfomsg *hdr;
5960 struct nlmsghdr *nlh;
5963 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5967 hdr = nlmsg_data(nlh);
5968 hdr->ifi_family = AF_INET6;
5970 hdr->ifi_type = dev->type;
5971 hdr->ifi_index = dev->ifindex;
5972 hdr->ifi_flags = dev_get_flags(dev);
5973 hdr->ifi_change = 0;
5975 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5977 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5978 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5979 (dev->ifindex != dev_get_iflink(dev) &&
5980 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5981 nla_put_u8(skb, IFLA_OPERSTATE,
5982 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5983 goto nla_put_failure;
5984 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
5986 goto nla_put_failure;
5988 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5989 goto nla_put_failure;
5991 nla_nest_end(skb, protoinfo);
5992 nlmsg_end(skb, nlh);
5996 nlmsg_cancel(skb, nlh);
6000 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
6001 struct netlink_ext_ack *extack)
6003 struct ifinfomsg *ifm;
6005 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
6006 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
6010 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
6011 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
6015 ifm = nlmsg_data(nlh);
6016 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
6017 ifm->ifi_change || ifm->ifi_index) {
6018 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6025 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6027 struct net *net = sock_net(skb->sk);
6030 struct net_device *dev;
6031 struct inet6_dev *idev;
6032 struct hlist_head *head;
6034 /* only requests using strict checking can pass data to
6035 * influence the dump
6037 if (cb->strict_check) {
6038 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6045 s_idx = cb->args[1];
6048 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6050 head = &net->dev_index_head[h];
6051 hlist_for_each_entry_rcu(dev, head, index_hlist) {
6054 idev = __in6_dev_get(dev);
6057 if (inet6_fill_ifinfo(skb, idev,
6058 NETLINK_CB(cb->skb).portid,
6060 RTM_NEWLINK, NLM_F_MULTI) < 0)
6074 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
6076 struct sk_buff *skb;
6077 struct net *net = dev_net(idev->dev);
6080 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
6084 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
6086 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6087 WARN_ON(err == -EMSGSIZE);
6091 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
6095 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
6098 static inline size_t inet6_prefix_nlmsg_size(void)
6100 return NLMSG_ALIGN(sizeof(struct prefixmsg))
6101 + nla_total_size(sizeof(struct in6_addr))
6102 + nla_total_size(sizeof(struct prefix_cacheinfo));
6105 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
6106 struct prefix_info *pinfo, u32 portid, u32 seq,
6107 int event, unsigned int flags)
6109 struct prefixmsg *pmsg;
6110 struct nlmsghdr *nlh;
6111 struct prefix_cacheinfo ci;
6113 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
6117 pmsg = nlmsg_data(nlh);
6118 pmsg->prefix_family = AF_INET6;
6119 pmsg->prefix_pad1 = 0;
6120 pmsg->prefix_pad2 = 0;
6121 pmsg->prefix_ifindex = idev->dev->ifindex;
6122 pmsg->prefix_len = pinfo->prefix_len;
6123 pmsg->prefix_type = pinfo->type;
6124 pmsg->prefix_pad3 = 0;
6125 pmsg->prefix_flags = pinfo->flags;
6127 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6128 goto nla_put_failure;
6129 ci.preferred_time = ntohl(pinfo->prefered);
6130 ci.valid_time = ntohl(pinfo->valid);
6131 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6132 goto nla_put_failure;
6133 nlmsg_end(skb, nlh);
6137 nlmsg_cancel(skb, nlh);
6141 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
6142 struct prefix_info *pinfo)
6144 struct sk_buff *skb;
6145 struct net *net = dev_net(idev->dev);
6148 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
6152 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
6154 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6155 WARN_ON(err == -EMSGSIZE);
6159 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
6163 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
6166 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6168 struct net *net = dev_net(ifp->idev->dev);
6173 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
6178 * If the address was optimistic we inserted the route at the
6179 * start of our DAD process, so we don't need to do it again.
6180 * If the device was taken down in the middle of the DAD
6181 * cycle there is a race where we could get here without a
6182 * host route, so nothing to insert. That will be fixed when
6183 * the device is brought up.
6185 if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6186 ip6_ins_rt(net, ifp->rt);
6187 } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6188 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6189 &ifp->addr, ifp->idev->dev->name);
6192 if (ifp->idev->cnf.forwarding)
6193 addrconf_join_anycast(ifp);
6194 if (!ipv6_addr_any(&ifp->peer_addr))
6195 addrconf_prefix_route(&ifp->peer_addr, 128,
6196 ifp->rt_priority, ifp->idev->dev,
6200 if (ifp->idev->cnf.forwarding)
6201 addrconf_leave_anycast(ifp);
6202 addrconf_leave_solict(ifp->idev, &ifp->addr);
6203 if (!ipv6_addr_any(&ifp->peer_addr)) {
6204 struct fib6_info *rt;
6206 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
6207 ifp->idev->dev, 0, 0,
6210 ip6_del_rt(net, rt, false);
6213 ip6_del_rt(net, ifp->rt, false);
6216 rt_genid_bump_ipv6(net);
6219 atomic_inc(&net->ipv6.dev_addr_genid);
6222 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6224 if (likely(ifp->idev->dead == 0))
6225 __ipv6_ifa_notify(event, ifp);
6228 #ifdef CONFIG_SYSCTL
6230 static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
6231 void *buffer, size_t *lenp, loff_t *ppos)
6233 int *valp = ctl->data;
6236 struct ctl_table lctl;
6240 * ctl->data points to idev->cnf.forwarding, we should
6241 * not modify it until we get the rtnl lock.
6246 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6249 ret = addrconf_fixup_forwarding(ctl, valp, val);
6255 static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6256 void *buffer, size_t *lenp, loff_t *ppos)
6258 struct inet6_dev *idev = ctl->extra1;
6259 int min_mtu = IPV6_MIN_MTU;
6260 struct ctl_table lctl;
6263 lctl.extra1 = &min_mtu;
6264 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6266 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6269 static void dev_disable_change(struct inet6_dev *idev)
6271 struct netdev_notifier_info info;
6273 if (!idev || !idev->dev)
6276 netdev_notifier_info_init(&info, idev->dev);
6277 if (idev->cnf.disable_ipv6)
6278 addrconf_notify(NULL, NETDEV_DOWN, &info);
6280 addrconf_notify(NULL, NETDEV_UP, &info);
6283 static void addrconf_disable_change(struct net *net, __s32 newf)
6285 struct net_device *dev;
6286 struct inet6_dev *idev;
6288 for_each_netdev(net, dev) {
6289 idev = __in6_dev_get(dev);
6291 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6292 idev->cnf.disable_ipv6 = newf;
6294 dev_disable_change(idev);
6299 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
6304 if (!rtnl_trylock())
6305 return restart_syscall();
6307 net = (struct net *)table->extra2;
6311 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6316 if (p == &net->ipv6.devconf_all->disable_ipv6) {
6317 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6318 addrconf_disable_change(net, newf);
6319 } else if ((!newf) ^ (!old))
6320 dev_disable_change((struct inet6_dev *)table->extra1);
6326 static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
6327 void *buffer, size_t *lenp, loff_t *ppos)
6329 int *valp = ctl->data;
6332 struct ctl_table lctl;
6336 * ctl->data points to idev->cnf.disable_ipv6, we should
6337 * not modify it until we get the rtnl lock.
6342 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6345 ret = addrconf_disable_ipv6(ctl, valp, val);
6351 static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6352 void *buffer, size_t *lenp, loff_t *ppos)
6354 int *valp = ctl->data;
6359 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6362 if (write && old != new) {
6363 struct net *net = ctl->extra2;
6365 if (!rtnl_trylock())
6366 return restart_syscall();
6368 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
6369 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6370 NETCONFA_PROXY_NEIGH,
6371 NETCONFA_IFINDEX_DEFAULT,
6372 net->ipv6.devconf_dflt);
6373 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
6374 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6375 NETCONFA_PROXY_NEIGH,
6376 NETCONFA_IFINDEX_ALL,
6377 net->ipv6.devconf_all);
6379 struct inet6_dev *idev = ctl->extra1;
6381 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6382 NETCONFA_PROXY_NEIGH,
6392 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6393 void *buffer, size_t *lenp,
6398 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6399 struct net *net = (struct net *)ctl->extra2;
6400 struct ctl_table tmp = {
6402 .maxlen = sizeof(new_val),
6406 if (!rtnl_trylock())
6407 return restart_syscall();
6409 new_val = *((u32 *)ctl->data);
6411 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6416 if (check_addr_gen_mode(new_val) < 0) {
6422 if (check_stable_privacy(idev, net, new_val) < 0) {
6427 if (idev->cnf.addr_gen_mode != new_val) {
6428 idev->cnf.addr_gen_mode = new_val;
6429 addrconf_init_auto_addrs(idev->dev);
6431 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6432 struct net_device *dev;
6434 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6435 for_each_netdev(net, dev) {
6436 idev = __in6_dev_get(dev);
6438 idev->cnf.addr_gen_mode != new_val) {
6439 idev->cnf.addr_gen_mode = new_val;
6440 addrconf_init_auto_addrs(idev->dev);
6445 *((u32 *)ctl->data) = new_val;
6454 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6455 void *buffer, size_t *lenp,
6459 struct in6_addr addr;
6460 char str[IPV6_MAX_STRLEN];
6461 struct ctl_table lctl = *ctl;
6462 struct net *net = ctl->extra2;
6463 struct ipv6_stable_secret *secret = ctl->data;
6465 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6468 lctl.maxlen = IPV6_MAX_STRLEN;
6471 if (!rtnl_trylock())
6472 return restart_syscall();
6474 if (!write && !secret->initialized) {
6479 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6480 if (err >= sizeof(str)) {
6485 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6489 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6494 secret->initialized = true;
6495 secret->secret = addr;
6497 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6498 struct net_device *dev;
6500 for_each_netdev(net, dev) {
6501 struct inet6_dev *idev = __in6_dev_get(dev);
6504 idev->cnf.addr_gen_mode =
6505 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6509 struct inet6_dev *idev = ctl->extra1;
6511 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6521 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6522 int write, void *buffer,
6526 int *valp = ctl->data;
6529 struct ctl_table lctl;
6532 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6533 * we should not modify it until we get the rtnl lock.
6538 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6541 ret = addrconf_fixup_linkdown(ctl, valp, val);
6548 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6552 rt->dst.flags |= DST_NOPOLICY;
6554 rt->dst.flags &= ~DST_NOPOLICY;
6559 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6561 struct inet6_ifaddr *ifa;
6563 read_lock_bh(&idev->lock);
6564 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6565 spin_lock(&ifa->lock);
6567 /* host routes only use builtin fib6_nh */
6568 struct fib6_nh *nh = ifa->rt->fib6_nh;
6572 ifa->rt->dst_nopolicy = val ? true : false;
6573 if (nh->rt6i_pcpu) {
6574 for_each_possible_cpu(cpu) {
6575 struct rt6_info **rtp;
6577 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6578 addrconf_set_nopolicy(*rtp, val);
6583 spin_unlock(&ifa->lock);
6585 read_unlock_bh(&idev->lock);
6589 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6591 struct inet6_dev *idev;
6594 if (!rtnl_trylock())
6595 return restart_syscall();
6599 net = (struct net *)ctl->extra2;
6600 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6605 if (valp == &net->ipv6.devconf_all->disable_policy) {
6606 struct net_device *dev;
6608 for_each_netdev(net, dev) {
6609 idev = __in6_dev_get(dev);
6611 addrconf_disable_policy_idev(idev, val);
6614 idev = (struct inet6_dev *)ctl->extra1;
6615 addrconf_disable_policy_idev(idev, val);
6622 static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6623 void *buffer, size_t *lenp, loff_t *ppos)
6625 int *valp = ctl->data;
6628 struct ctl_table lctl;
6633 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6635 if (write && (*valp != val))
6636 ret = addrconf_disable_policy(ctl, valp, val);
6644 static int minus_one = -1;
6645 static const int two_five_five = 255;
6646 static u32 ioam6_if_id_max = U16_MAX;
6648 static const struct ctl_table addrconf_sysctl[] = {
6650 .procname = "forwarding",
6651 .data = &ipv6_devconf.forwarding,
6652 .maxlen = sizeof(int),
6654 .proc_handler = addrconf_sysctl_forward,
6657 .procname = "hop_limit",
6658 .data = &ipv6_devconf.hop_limit,
6659 .maxlen = sizeof(int),
6661 .proc_handler = proc_dointvec_minmax,
6662 .extra1 = (void *)SYSCTL_ONE,
6663 .extra2 = (void *)&two_five_five,
6667 .data = &ipv6_devconf.mtu6,
6668 .maxlen = sizeof(int),
6670 .proc_handler = addrconf_sysctl_mtu,
6673 .procname = "accept_ra",
6674 .data = &ipv6_devconf.accept_ra,
6675 .maxlen = sizeof(int),
6677 .proc_handler = proc_dointvec,
6680 .procname = "accept_redirects",
6681 .data = &ipv6_devconf.accept_redirects,
6682 .maxlen = sizeof(int),
6684 .proc_handler = proc_dointvec,
6687 .procname = "autoconf",
6688 .data = &ipv6_devconf.autoconf,
6689 .maxlen = sizeof(int),
6691 .proc_handler = proc_dointvec,
6694 .procname = "dad_transmits",
6695 .data = &ipv6_devconf.dad_transmits,
6696 .maxlen = sizeof(int),
6698 .proc_handler = proc_dointvec,
6701 .procname = "router_solicitations",
6702 .data = &ipv6_devconf.rtr_solicits,
6703 .maxlen = sizeof(int),
6705 .proc_handler = proc_dointvec_minmax,
6706 .extra1 = &minus_one,
6709 .procname = "router_solicitation_interval",
6710 .data = &ipv6_devconf.rtr_solicit_interval,
6711 .maxlen = sizeof(int),
6713 .proc_handler = proc_dointvec_jiffies,
6716 .procname = "router_solicitation_max_interval",
6717 .data = &ipv6_devconf.rtr_solicit_max_interval,
6718 .maxlen = sizeof(int),
6720 .proc_handler = proc_dointvec_jiffies,
6723 .procname = "router_solicitation_delay",
6724 .data = &ipv6_devconf.rtr_solicit_delay,
6725 .maxlen = sizeof(int),
6727 .proc_handler = proc_dointvec_jiffies,
6730 .procname = "force_mld_version",
6731 .data = &ipv6_devconf.force_mld_version,
6732 .maxlen = sizeof(int),
6734 .proc_handler = proc_dointvec,
6737 .procname = "mldv1_unsolicited_report_interval",
6739 &ipv6_devconf.mldv1_unsolicited_report_interval,
6740 .maxlen = sizeof(int),
6742 .proc_handler = proc_dointvec_ms_jiffies,
6745 .procname = "mldv2_unsolicited_report_interval",
6747 &ipv6_devconf.mldv2_unsolicited_report_interval,
6748 .maxlen = sizeof(int),
6750 .proc_handler = proc_dointvec_ms_jiffies,
6753 .procname = "use_tempaddr",
6754 .data = &ipv6_devconf.use_tempaddr,
6755 .maxlen = sizeof(int),
6757 .proc_handler = proc_dointvec,
6760 .procname = "temp_valid_lft",
6761 .data = &ipv6_devconf.temp_valid_lft,
6762 .maxlen = sizeof(int),
6764 .proc_handler = proc_dointvec,
6767 .procname = "temp_prefered_lft",
6768 .data = &ipv6_devconf.temp_prefered_lft,
6769 .maxlen = sizeof(int),
6771 .proc_handler = proc_dointvec,
6774 .procname = "regen_max_retry",
6775 .data = &ipv6_devconf.regen_max_retry,
6776 .maxlen = sizeof(int),
6778 .proc_handler = proc_dointvec,
6781 .procname = "max_desync_factor",
6782 .data = &ipv6_devconf.max_desync_factor,
6783 .maxlen = sizeof(int),
6785 .proc_handler = proc_dointvec,
6788 .procname = "max_addresses",
6789 .data = &ipv6_devconf.max_addresses,
6790 .maxlen = sizeof(int),
6792 .proc_handler = proc_dointvec,
6795 .procname = "accept_ra_defrtr",
6796 .data = &ipv6_devconf.accept_ra_defrtr,
6797 .maxlen = sizeof(int),
6799 .proc_handler = proc_dointvec,
6802 .procname = "ra_defrtr_metric",
6803 .data = &ipv6_devconf.ra_defrtr_metric,
6804 .maxlen = sizeof(u32),
6806 .proc_handler = proc_douintvec_minmax,
6807 .extra1 = (void *)SYSCTL_ONE,
6810 .procname = "accept_ra_min_hop_limit",
6811 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6812 .maxlen = sizeof(int),
6814 .proc_handler = proc_dointvec,
6817 .procname = "accept_ra_min_lft",
6818 .data = &ipv6_devconf.accept_ra_min_lft,
6819 .maxlen = sizeof(int),
6821 .proc_handler = proc_dointvec,
6824 .procname = "accept_ra_pinfo",
6825 .data = &ipv6_devconf.accept_ra_pinfo,
6826 .maxlen = sizeof(int),
6828 .proc_handler = proc_dointvec,
6830 #ifdef CONFIG_IPV6_ROUTER_PREF
6832 .procname = "accept_ra_rtr_pref",
6833 .data = &ipv6_devconf.accept_ra_rtr_pref,
6834 .maxlen = sizeof(int),
6836 .proc_handler = proc_dointvec,
6839 .procname = "router_probe_interval",
6840 .data = &ipv6_devconf.rtr_probe_interval,
6841 .maxlen = sizeof(int),
6843 .proc_handler = proc_dointvec_jiffies,
6845 #ifdef CONFIG_IPV6_ROUTE_INFO
6847 .procname = "accept_ra_rt_info_min_plen",
6848 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6849 .maxlen = sizeof(int),
6851 .proc_handler = proc_dointvec,
6854 .procname = "accept_ra_rt_info_max_plen",
6855 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6856 .maxlen = sizeof(int),
6858 .proc_handler = proc_dointvec,
6863 .procname = "proxy_ndp",
6864 .data = &ipv6_devconf.proxy_ndp,
6865 .maxlen = sizeof(int),
6867 .proc_handler = addrconf_sysctl_proxy_ndp,
6870 .procname = "accept_source_route",
6871 .data = &ipv6_devconf.accept_source_route,
6872 .maxlen = sizeof(int),
6874 .proc_handler = proc_dointvec,
6876 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6878 .procname = "optimistic_dad",
6879 .data = &ipv6_devconf.optimistic_dad,
6880 .maxlen = sizeof(int),
6882 .proc_handler = proc_dointvec,
6885 .procname = "use_optimistic",
6886 .data = &ipv6_devconf.use_optimistic,
6887 .maxlen = sizeof(int),
6889 .proc_handler = proc_dointvec,
6892 #ifdef CONFIG_IPV6_MROUTE
6894 .procname = "mc_forwarding",
6895 .data = &ipv6_devconf.mc_forwarding,
6896 .maxlen = sizeof(int),
6898 .proc_handler = proc_dointvec,
6902 .procname = "disable_ipv6",
6903 .data = &ipv6_devconf.disable_ipv6,
6904 .maxlen = sizeof(int),
6906 .proc_handler = addrconf_sysctl_disable,
6909 .procname = "accept_dad",
6910 .data = &ipv6_devconf.accept_dad,
6911 .maxlen = sizeof(int),
6913 .proc_handler = proc_dointvec,
6916 .procname = "force_tllao",
6917 .data = &ipv6_devconf.force_tllao,
6918 .maxlen = sizeof(int),
6920 .proc_handler = proc_dointvec
6923 .procname = "ndisc_notify",
6924 .data = &ipv6_devconf.ndisc_notify,
6925 .maxlen = sizeof(int),
6927 .proc_handler = proc_dointvec
6930 .procname = "suppress_frag_ndisc",
6931 .data = &ipv6_devconf.suppress_frag_ndisc,
6932 .maxlen = sizeof(int),
6934 .proc_handler = proc_dointvec
6937 .procname = "accept_ra_from_local",
6938 .data = &ipv6_devconf.accept_ra_from_local,
6939 .maxlen = sizeof(int),
6941 .proc_handler = proc_dointvec,
6944 .procname = "accept_ra_mtu",
6945 .data = &ipv6_devconf.accept_ra_mtu,
6946 .maxlen = sizeof(int),
6948 .proc_handler = proc_dointvec,
6951 .procname = "stable_secret",
6952 .data = &ipv6_devconf.stable_secret,
6953 .maxlen = IPV6_MAX_STRLEN,
6955 .proc_handler = addrconf_sysctl_stable_secret,
6958 .procname = "use_oif_addrs_only",
6959 .data = &ipv6_devconf.use_oif_addrs_only,
6960 .maxlen = sizeof(int),
6962 .proc_handler = proc_dointvec,
6965 .procname = "ignore_routes_with_linkdown",
6966 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6967 .maxlen = sizeof(int),
6969 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6972 .procname = "drop_unicast_in_l2_multicast",
6973 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6974 .maxlen = sizeof(int),
6976 .proc_handler = proc_dointvec,
6979 .procname = "drop_unsolicited_na",
6980 .data = &ipv6_devconf.drop_unsolicited_na,
6981 .maxlen = sizeof(int),
6983 .proc_handler = proc_dointvec,
6986 .procname = "keep_addr_on_down",
6987 .data = &ipv6_devconf.keep_addr_on_down,
6988 .maxlen = sizeof(int),
6990 .proc_handler = proc_dointvec,
6994 .procname = "seg6_enabled",
6995 .data = &ipv6_devconf.seg6_enabled,
6996 .maxlen = sizeof(int),
6998 .proc_handler = proc_dointvec,
7000 #ifdef CONFIG_IPV6_SEG6_HMAC
7002 .procname = "seg6_require_hmac",
7003 .data = &ipv6_devconf.seg6_require_hmac,
7004 .maxlen = sizeof(int),
7006 .proc_handler = proc_dointvec,
7010 .procname = "enhanced_dad",
7011 .data = &ipv6_devconf.enhanced_dad,
7012 .maxlen = sizeof(int),
7014 .proc_handler = proc_dointvec,
7017 .procname = "addr_gen_mode",
7018 .data = &ipv6_devconf.addr_gen_mode,
7019 .maxlen = sizeof(int),
7021 .proc_handler = addrconf_sysctl_addr_gen_mode,
7024 .procname = "disable_policy",
7025 .data = &ipv6_devconf.disable_policy,
7026 .maxlen = sizeof(int),
7028 .proc_handler = addrconf_sysctl_disable_policy,
7031 .procname = "ndisc_tclass",
7032 .data = &ipv6_devconf.ndisc_tclass,
7033 .maxlen = sizeof(int),
7035 .proc_handler = proc_dointvec_minmax,
7036 .extra1 = (void *)SYSCTL_ZERO,
7037 .extra2 = (void *)&two_five_five,
7040 .procname = "rpl_seg_enabled",
7041 .data = &ipv6_devconf.rpl_seg_enabled,
7042 .maxlen = sizeof(int),
7044 .proc_handler = proc_dointvec,
7047 .procname = "ioam6_enabled",
7048 .data = &ipv6_devconf.ioam6_enabled,
7049 .maxlen = sizeof(u8),
7051 .proc_handler = proc_dou8vec_minmax,
7052 .extra1 = (void *)SYSCTL_ZERO,
7053 .extra2 = (void *)SYSCTL_ONE,
7056 .procname = "ioam6_id",
7057 .data = &ipv6_devconf.ioam6_id,
7058 .maxlen = sizeof(u32),
7060 .proc_handler = proc_douintvec_minmax,
7061 .extra1 = (void *)SYSCTL_ZERO,
7062 .extra2 = (void *)&ioam6_if_id_max,
7065 .procname = "ioam6_id_wide",
7066 .data = &ipv6_devconf.ioam6_id_wide,
7067 .maxlen = sizeof(u32),
7069 .proc_handler = proc_douintvec,
7072 .procname = "ndisc_evict_nocarrier",
7073 .data = &ipv6_devconf.ndisc_evict_nocarrier,
7074 .maxlen = sizeof(u8),
7076 .proc_handler = proc_dou8vec_minmax,
7077 .extra1 = (void *)SYSCTL_ZERO,
7078 .extra2 = (void *)SYSCTL_ONE,
7081 .procname = "accept_untracked_na",
7082 .data = &ipv6_devconf.accept_untracked_na,
7083 .maxlen = sizeof(int),
7085 .proc_handler = proc_dointvec_minmax,
7086 .extra1 = SYSCTL_ZERO,
7087 .extra2 = SYSCTL_TWO,
7094 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7095 struct inet6_dev *idev, struct ipv6_devconf *p)
7098 struct ctl_table *table;
7099 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
7101 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7105 for (i = 0; table[i].data; i++) {
7106 table[i].data += (char *)p - (char *)&ipv6_devconf;
7107 /* If one of these is already set, then it is not safe to
7108 * overwrite either of them: this makes proc_dointvec_minmax
7111 if (!table[i].extra1 && !table[i].extra2) {
7112 table[i].extra1 = idev; /* embedded; no ref */
7113 table[i].extra2 = net;
7117 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
7119 p->sysctl_header = register_net_sysctl(net, path, table);
7120 if (!p->sysctl_header)
7123 if (!strcmp(dev_name, "all"))
7124 ifindex = NETCONFA_IFINDEX_ALL;
7125 else if (!strcmp(dev_name, "default"))
7126 ifindex = NETCONFA_IFINDEX_DEFAULT;
7128 ifindex = idev->dev->ifindex;
7129 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
7139 static void __addrconf_sysctl_unregister(struct net *net,
7140 struct ipv6_devconf *p, int ifindex)
7142 struct ctl_table *table;
7144 if (!p->sysctl_header)
7147 table = p->sysctl_header->ctl_table_arg;
7148 unregister_net_sysctl_table(p->sysctl_header);
7149 p->sysctl_header = NULL;
7152 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
7155 static int addrconf_sysctl_register(struct inet6_dev *idev)
7159 if (!sysctl_dev_name_is_allowed(idev->dev->name))
7162 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7163 &ndisc_ifinfo_sysctl_change);
7166 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7169 neigh_sysctl_unregister(idev->nd_parms);
7174 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7176 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
7177 idev->dev->ifindex);
7178 neigh_sysctl_unregister(idev->nd_parms);
7184 static int __net_init addrconf_init_net(struct net *net)
7187 struct ipv6_devconf *all, *dflt;
7189 spin_lock_init(&net->ipv6.addrconf_hash_lock);
7190 INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
7191 net->ipv6.inet6_addr_lst = kcalloc(IN6_ADDR_HSIZE,
7192 sizeof(struct hlist_head),
7194 if (!net->ipv6.inet6_addr_lst)
7195 goto err_alloc_addr;
7197 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
7201 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
7203 goto err_alloc_dflt;
7205 if (!net_eq(net, &init_net)) {
7206 switch (net_inherit_devconf()) {
7207 case 1: /* copy from init_net */
7208 memcpy(all, init_net.ipv6.devconf_all,
7209 sizeof(ipv6_devconf));
7210 memcpy(dflt, init_net.ipv6.devconf_dflt,
7211 sizeof(ipv6_devconf_dflt));
7213 case 3: /* copy from the current netns */
7214 memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
7215 sizeof(ipv6_devconf));
7217 current->nsproxy->net_ns->ipv6.devconf_dflt,
7218 sizeof(ipv6_devconf_dflt));
7222 /* use compiled values */
7227 /* these will be inherited by all namespaces */
7228 dflt->autoconf = ipv6_defaults.autoconf;
7229 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7231 dflt->stable_secret.initialized = false;
7232 all->stable_secret.initialized = false;
7234 net->ipv6.devconf_all = all;
7235 net->ipv6.devconf_dflt = dflt;
7237 #ifdef CONFIG_SYSCTL
7238 err = __addrconf_sysctl_register(net, "all", NULL, all);
7242 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7248 #ifdef CONFIG_SYSCTL
7250 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7253 net->ipv6.devconf_dflt = NULL;
7257 net->ipv6.devconf_all = NULL;
7259 kfree(net->ipv6.inet6_addr_lst);
7264 static void __net_exit addrconf_exit_net(struct net *net)
7268 #ifdef CONFIG_SYSCTL
7269 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7270 NETCONFA_IFINDEX_DEFAULT);
7271 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7272 NETCONFA_IFINDEX_ALL);
7274 kfree(net->ipv6.devconf_dflt);
7275 net->ipv6.devconf_dflt = NULL;
7276 kfree(net->ipv6.devconf_all);
7277 net->ipv6.devconf_all = NULL;
7279 cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
7281 * Check hash table, then free it.
7283 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7284 WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7286 kfree(net->ipv6.inet6_addr_lst);
7287 net->ipv6.inet6_addr_lst = NULL;
7290 static struct pernet_operations addrconf_ops = {
7291 .init = addrconf_init_net,
7292 .exit = addrconf_exit_net,
7295 static struct rtnl_af_ops inet6_ops __read_mostly = {
7297 .fill_link_af = inet6_fill_link_af,
7298 .get_link_af_size = inet6_get_link_af_size,
7299 .validate_link_af = inet6_validate_link_af,
7300 .set_link_af = inet6_set_link_af,
7304 * Init / cleanup code
7307 int __init addrconf_init(void)
7309 struct inet6_dev *idev;
7312 err = ipv6_addr_label_init();
7314 pr_crit("%s: cannot initialize default policy table: %d\n",
7319 err = register_pernet_subsys(&addrconf_ops);
7323 addrconf_wq = create_workqueue("ipv6_addrconf");
7330 idev = ipv6_add_dev(blackhole_netdev);
7333 err = PTR_ERR(idev);
7337 ip6_route_init_special_entries();
7339 register_netdevice_notifier(&ipv6_dev_notf);
7341 addrconf_verify(&init_net);
7343 rtnl_af_register(&inet6_ops);
7345 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7346 NULL, inet6_dump_ifinfo, 0);
7350 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7351 inet6_rtm_newaddr, NULL, 0);
7354 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7355 inet6_rtm_deladdr, NULL, 0);
7358 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7359 inet6_rtm_getaddr, inet6_dump_ifaddr,
7360 RTNL_FLAG_DOIT_UNLOCKED);
7363 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7364 NULL, inet6_dump_ifmcaddr, 0);
7367 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7368 NULL, inet6_dump_ifacaddr, 0);
7371 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7372 inet6_netconf_get_devconf,
7373 inet6_netconf_dump_devconf,
7374 RTNL_FLAG_DOIT_UNLOCKED);
7377 err = ipv6_addr_label_rtnl_register();
7383 rtnl_unregister_all(PF_INET6);
7384 rtnl_af_unregister(&inet6_ops);
7385 unregister_netdevice_notifier(&ipv6_dev_notf);
7387 destroy_workqueue(addrconf_wq);
7389 unregister_pernet_subsys(&addrconf_ops);
7391 ipv6_addr_label_cleanup();
7396 void addrconf_cleanup(void)
7398 struct net_device *dev;
7400 unregister_netdevice_notifier(&ipv6_dev_notf);
7401 unregister_pernet_subsys(&addrconf_ops);
7402 ipv6_addr_label_cleanup();
7404 rtnl_af_unregister(&inet6_ops);
7408 /* clean dev list */
7409 for_each_netdev(&init_net, dev) {
7410 if (__in6_dev_get(dev) == NULL)
7412 addrconf_ifdown(dev, true);
7414 addrconf_ifdown(init_net.loopback_dev, true);
7418 destroy_workqueue(addrconf_wq);