2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/sched/signal.h>
47 #include <linux/socket.h>
48 #include <linux/sockios.h>
49 #include <linux/net.h>
50 #include <linux/inet.h>
51 #include <linux/in6.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_addr.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_arcnet.h>
56 #include <linux/if_infiniband.h>
57 #include <linux/route.h>
58 #include <linux/inetdevice.h>
59 #include <linux/init.h>
60 #include <linux/slab.h>
62 #include <linux/sysctl.h>
64 #include <linux/capability.h>
65 #include <linux/delay.h>
66 #include <linux/notifier.h>
67 #include <linux/string.h>
68 #include <linux/hash.h>
70 #include <net/net_namespace.h>
74 #include <net/6lowpan.h>
75 #include <net/firewire.h>
77 #include <net/protocol.h>
78 #include <net/ndisc.h>
79 #include <net/ip6_route.h>
80 #include <net/addrconf.h>
83 #include <net/netlink.h>
84 #include <net/pkt_sched.h>
85 #include <net/l3mdev.h>
86 #include <linux/if_tunnel.h>
87 #include <linux/rtnetlink.h>
88 #include <linux/netconf.h>
89 #include <linux/random.h>
90 #include <linux/uaccess.h>
91 #include <asm/unaligned.h>
93 #include <linux/proc_fs.h>
94 #include <linux/seq_file.h>
95 #include <linux/export.h>
97 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define IPV6_MAX_STRLEN \
100 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
102 static inline u32 cstamp_delta(unsigned long cstamp)
104 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
107 static inline s32 rfc3315_s14_backoff_init(s32 irt)
109 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
110 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
111 do_div(tmp, 1000000);
115 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
117 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
118 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
119 do_div(tmp, 1000000);
120 if ((s32)tmp > mrt) {
121 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
122 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
123 do_div(tmp, 1000000);
129 static int addrconf_sysctl_register(struct inet6_dev *idev);
130 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
132 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
137 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
142 static void ipv6_regen_rndid(struct inet6_dev *idev);
143 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
145 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
146 static int ipv6_count_addresses(const struct inet6_dev *idev);
147 static int ipv6_generate_stable_address(struct in6_addr *addr,
149 const struct inet6_dev *idev);
151 #define IN6_ADDR_HSIZE_SHIFT 8
152 #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
154 * Configured unicast address hash table
156 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
157 static DEFINE_SPINLOCK(addrconf_hash_lock);
159 static void addrconf_verify(void);
160 static void addrconf_verify_rtnl(void);
161 static void addrconf_verify_work(struct work_struct *);
163 static struct workqueue_struct *addrconf_wq;
164 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
166 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
167 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
169 static void addrconf_type_change(struct net_device *dev,
170 unsigned long event);
171 static int addrconf_ifdown(struct net_device *dev, int how);
173 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
175 const struct net_device *dev,
176 u32 flags, u32 noflags,
179 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
180 static void addrconf_dad_work(struct work_struct *w);
181 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
183 static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
184 static void addrconf_rs_timer(struct timer_list *t);
185 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
186 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
188 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
189 struct prefix_info *pinfo);
191 static struct ipv6_devconf ipv6_devconf __read_mostly = {
193 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
194 .mtu6 = IPV6_MIN_MTU,
196 .accept_redirects = 1,
198 .force_mld_version = 0,
199 .mldv1_unsolicited_report_interval = 10 * HZ,
200 .mldv2_unsolicited_report_interval = HZ,
202 .rtr_solicits = MAX_RTR_SOLICITATIONS,
203 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
204 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
205 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
207 .temp_valid_lft = TEMP_VALID_LIFETIME,
208 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
209 .regen_max_retry = REGEN_MAX_RETRY,
210 .max_desync_factor = MAX_DESYNC_FACTOR,
211 .max_addresses = IPV6_MAX_ADDRESSES,
212 .accept_ra_defrtr = 1,
213 .accept_ra_from_local = 0,
214 .accept_ra_min_hop_limit= 1,
215 .accept_ra_pinfo = 1,
216 #ifdef CONFIG_IPV6_ROUTER_PREF
217 .accept_ra_rtr_pref = 1,
218 .rtr_probe_interval = 60 * HZ,
219 #ifdef CONFIG_IPV6_ROUTE_INFO
220 .accept_ra_rt_info_min_plen = 0,
221 .accept_ra_rt_info_max_plen = 0,
225 .accept_source_route = 0, /* we do not accept RH0 by default. */
228 .suppress_frag_ndisc = 1,
231 .initialized = false,
233 .use_oif_addrs_only = 0,
234 .ignore_routes_with_linkdown = 0,
235 .keep_addr_on_down = 0,
237 #ifdef CONFIG_IPV6_SEG6_HMAC
238 .seg6_require_hmac = 0,
241 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
245 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
247 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
248 .mtu6 = IPV6_MIN_MTU,
250 .accept_redirects = 1,
252 .force_mld_version = 0,
253 .mldv1_unsolicited_report_interval = 10 * HZ,
254 .mldv2_unsolicited_report_interval = HZ,
256 .rtr_solicits = MAX_RTR_SOLICITATIONS,
257 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
258 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
259 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
261 .temp_valid_lft = TEMP_VALID_LIFETIME,
262 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
263 .regen_max_retry = REGEN_MAX_RETRY,
264 .max_desync_factor = MAX_DESYNC_FACTOR,
265 .max_addresses = IPV6_MAX_ADDRESSES,
266 .accept_ra_defrtr = 1,
267 .accept_ra_from_local = 0,
268 .accept_ra_min_hop_limit= 1,
269 .accept_ra_pinfo = 1,
270 #ifdef CONFIG_IPV6_ROUTER_PREF
271 .accept_ra_rtr_pref = 1,
272 .rtr_probe_interval = 60 * HZ,
273 #ifdef CONFIG_IPV6_ROUTE_INFO
274 .accept_ra_rt_info_min_plen = 0,
275 .accept_ra_rt_info_max_plen = 0,
279 .accept_source_route = 0, /* we do not accept RH0 by default. */
282 .suppress_frag_ndisc = 1,
285 .initialized = false,
287 .use_oif_addrs_only = 0,
288 .ignore_routes_with_linkdown = 0,
289 .keep_addr_on_down = 0,
291 #ifdef CONFIG_IPV6_SEG6_HMAC
292 .seg6_require_hmac = 0,
295 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
299 /* Check if link is ready: is it up and is a valid qdisc available */
300 static inline bool addrconf_link_ready(const struct net_device *dev)
302 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
305 static void addrconf_del_rs_timer(struct inet6_dev *idev)
307 if (del_timer(&idev->rs_timer))
311 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
313 if (cancel_delayed_work(&ifp->dad_work))
317 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
320 if (!timer_pending(&idev->rs_timer))
322 mod_timer(&idev->rs_timer, jiffies + when);
325 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
329 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
333 static int snmp6_alloc_dev(struct inet6_dev *idev)
337 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
338 if (!idev->stats.ipv6)
341 for_each_possible_cpu(i) {
342 struct ipstats_mib *addrconf_stats;
343 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
344 u64_stats_init(&addrconf_stats->syncp);
348 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
350 if (!idev->stats.icmpv6dev)
352 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
354 if (!idev->stats.icmpv6msgdev)
360 kfree(idev->stats.icmpv6dev);
362 free_percpu(idev->stats.ipv6);
367 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
369 struct inet6_dev *ndev;
374 if (dev->mtu < IPV6_MIN_MTU)
375 return ERR_PTR(-EINVAL);
377 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
381 rwlock_init(&ndev->lock);
383 INIT_LIST_HEAD(&ndev->addr_list);
384 timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
385 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
387 if (ndev->cnf.stable_secret.initialized)
388 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
390 ndev->cnf.mtu6 = dev->mtu;
391 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
392 if (!ndev->nd_parms) {
396 if (ndev->cnf.forwarding)
397 dev_disable_lro(dev);
398 /* We refer to the device */
401 if (snmp6_alloc_dev(ndev) < 0) {
402 netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
404 neigh_parms_release(&nd_tbl, ndev->nd_parms);
410 if (snmp6_register_dev(ndev) < 0) {
411 netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
412 __func__, dev->name);
416 /* One reference from device. */
417 refcount_set(&ndev->refcnt, 1);
419 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
420 ndev->cnf.accept_dad = -1;
422 #if IS_ENABLED(CONFIG_IPV6_SIT)
423 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
424 pr_info("%s: Disabled Multicast RS\n", dev->name);
425 ndev->cnf.rtr_solicits = 0;
429 INIT_LIST_HEAD(&ndev->tempaddr_list);
430 ndev->desync_factor = U32_MAX;
431 if ((dev->flags&IFF_LOOPBACK) ||
432 dev->type == ARPHRD_TUNNEL ||
433 dev->type == ARPHRD_TUNNEL6 ||
434 dev->type == ARPHRD_SIT ||
435 dev->type == ARPHRD_NONE) {
436 ndev->cnf.use_tempaddr = -1;
438 ipv6_regen_rndid(ndev);
440 ndev->token = in6addr_any;
442 if (netif_running(dev) && addrconf_link_ready(dev))
443 ndev->if_flags |= IF_READY;
445 ipv6_mc_init_dev(ndev);
446 ndev->tstamp = jiffies;
447 err = addrconf_sysctl_register(ndev);
449 ipv6_mc_destroy_dev(ndev);
450 snmp6_unregister_dev(ndev);
453 /* protected by rtnl_lock */
454 rcu_assign_pointer(dev->ip6_ptr, ndev);
456 /* Join interface-local all-node multicast group */
457 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
459 /* Join all-node multicast group */
460 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
462 /* Join all-router multicast group if forwarding is set */
463 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
464 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
469 neigh_parms_release(&nd_tbl, ndev->nd_parms);
471 in6_dev_finish_destroy(ndev);
475 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
477 struct inet6_dev *idev;
481 idev = __in6_dev_get(dev);
483 idev = ipv6_add_dev(dev);
488 if (dev->flags&IFF_UP)
493 static int inet6_netconf_msgsize_devconf(int type)
495 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
496 + nla_total_size(4); /* NETCONFA_IFINDEX */
499 if (type == NETCONFA_ALL)
502 if (all || type == NETCONFA_FORWARDING)
503 size += nla_total_size(4);
504 #ifdef CONFIG_IPV6_MROUTE
505 if (all || type == NETCONFA_MC_FORWARDING)
506 size += nla_total_size(4);
508 if (all || type == NETCONFA_PROXY_NEIGH)
509 size += nla_total_size(4);
511 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
512 size += nla_total_size(4);
517 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
518 struct ipv6_devconf *devconf, u32 portid,
519 u32 seq, int event, unsigned int flags,
522 struct nlmsghdr *nlh;
523 struct netconfmsg *ncm;
526 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
531 if (type == NETCONFA_ALL)
534 ncm = nlmsg_data(nlh);
535 ncm->ncm_family = AF_INET6;
537 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
538 goto nla_put_failure;
543 if ((all || type == NETCONFA_FORWARDING) &&
544 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
545 goto nla_put_failure;
546 #ifdef CONFIG_IPV6_MROUTE
547 if ((all || type == NETCONFA_MC_FORWARDING) &&
548 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
549 devconf->mc_forwarding) < 0)
550 goto nla_put_failure;
552 if ((all || type == NETCONFA_PROXY_NEIGH) &&
553 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
554 goto nla_put_failure;
556 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
557 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
558 devconf->ignore_routes_with_linkdown) < 0)
559 goto nla_put_failure;
566 nlmsg_cancel(skb, nlh);
570 void inet6_netconf_notify_devconf(struct net *net, int event, int type,
571 int ifindex, struct ipv6_devconf *devconf)
576 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
580 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
583 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
584 WARN_ON(err == -EMSGSIZE);
588 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
591 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
594 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
595 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
596 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
597 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
598 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
601 static int inet6_netconf_valid_get_req(struct sk_buff *skb,
602 const struct nlmsghdr *nlh,
604 struct netlink_ext_ack *extack)
608 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
609 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
613 if (!netlink_strict_get_check(skb))
614 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
616 devconf_ipv6_policy, extack);
618 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
620 devconf_ipv6_policy, extack);
624 for (i = 0; i <= NETCONFA_MAX; i++) {
629 case NETCONFA_IFINDEX:
632 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
640 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
641 struct nlmsghdr *nlh,
642 struct netlink_ext_ack *extack)
644 struct net *net = sock_net(in_skb->sk);
645 struct nlattr *tb[NETCONFA_MAX+1];
646 struct inet6_dev *in6_dev = NULL;
647 struct net_device *dev = NULL;
649 struct ipv6_devconf *devconf;
653 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
657 if (!tb[NETCONFA_IFINDEX])
661 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
663 case NETCONFA_IFINDEX_ALL:
664 devconf = net->ipv6.devconf_all;
666 case NETCONFA_IFINDEX_DEFAULT:
667 devconf = net->ipv6.devconf_dflt;
670 dev = dev_get_by_index(net, ifindex);
673 in6_dev = in6_dev_get(dev);
676 devconf = &in6_dev->cnf;
681 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
685 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
686 NETLINK_CB(in_skb).portid,
687 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
690 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
691 WARN_ON(err == -EMSGSIZE);
695 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
698 in6_dev_put(in6_dev);
704 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
705 struct netlink_callback *cb)
707 const struct nlmsghdr *nlh = cb->nlh;
708 struct net *net = sock_net(skb->sk);
711 struct net_device *dev;
712 struct inet6_dev *idev;
713 struct hlist_head *head;
715 if (cb->strict_check) {
716 struct netlink_ext_ack *extack = cb->extack;
717 struct netconfmsg *ncm;
719 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
720 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
724 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
725 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
731 s_idx = idx = cb->args[1];
733 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
735 head = &net->dev_index_head[h];
737 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
739 hlist_for_each_entry_rcu(dev, head, index_hlist) {
742 idev = __in6_dev_get(dev);
746 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
748 NETLINK_CB(cb->skb).portid,
756 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
762 if (h == NETDEV_HASHENTRIES) {
763 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
764 net->ipv6.devconf_all,
765 NETLINK_CB(cb->skb).portid,
767 RTM_NEWNETCONF, NLM_F_MULTI,
773 if (h == NETDEV_HASHENTRIES + 1) {
774 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
775 net->ipv6.devconf_dflt,
776 NETLINK_CB(cb->skb).portid,
778 RTM_NEWNETCONF, NLM_F_MULTI,
792 static void dev_forward_change(struct inet6_dev *idev)
794 struct net_device *dev;
795 struct inet6_ifaddr *ifa;
800 if (idev->cnf.forwarding)
801 dev_disable_lro(dev);
802 if (dev->flags & IFF_MULTICAST) {
803 if (idev->cnf.forwarding) {
804 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
805 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
806 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
808 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
809 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
810 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
814 list_for_each_entry(ifa, &idev->addr_list, if_list) {
815 if (ifa->flags&IFA_F_TENTATIVE)
817 if (idev->cnf.forwarding)
818 addrconf_join_anycast(ifa);
820 addrconf_leave_anycast(ifa);
822 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
824 dev->ifindex, &idev->cnf);
828 static void addrconf_forward_change(struct net *net, __s32 newf)
830 struct net_device *dev;
831 struct inet6_dev *idev;
833 for_each_netdev(net, dev) {
834 idev = __in6_dev_get(dev);
836 int changed = (!idev->cnf.forwarding) ^ (!newf);
837 idev->cnf.forwarding = newf;
839 dev_forward_change(idev);
844 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
850 return restart_syscall();
852 net = (struct net *)table->extra2;
856 if (p == &net->ipv6.devconf_dflt->forwarding) {
857 if ((!newf) ^ (!old))
858 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
860 NETCONFA_IFINDEX_DEFAULT,
861 net->ipv6.devconf_dflt);
866 if (p == &net->ipv6.devconf_all->forwarding) {
867 int old_dflt = net->ipv6.devconf_dflt->forwarding;
869 net->ipv6.devconf_dflt->forwarding = newf;
870 if ((!newf) ^ (!old_dflt))
871 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
873 NETCONFA_IFINDEX_DEFAULT,
874 net->ipv6.devconf_dflt);
876 addrconf_forward_change(net, newf);
877 if ((!newf) ^ (!old))
878 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
880 NETCONFA_IFINDEX_ALL,
881 net->ipv6.devconf_all);
882 } else if ((!newf) ^ (!old))
883 dev_forward_change((struct inet6_dev *)table->extra1);
887 rt6_purge_dflt_routers(net);
891 static void addrconf_linkdown_change(struct net *net, __s32 newf)
893 struct net_device *dev;
894 struct inet6_dev *idev;
896 for_each_netdev(net, dev) {
897 idev = __in6_dev_get(dev);
899 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
901 idev->cnf.ignore_routes_with_linkdown = newf;
903 inet6_netconf_notify_devconf(dev_net(dev),
905 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
912 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
918 return restart_syscall();
920 net = (struct net *)table->extra2;
924 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
925 if ((!newf) ^ (!old))
926 inet6_netconf_notify_devconf(net,
928 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
929 NETCONFA_IFINDEX_DEFAULT,
930 net->ipv6.devconf_dflt);
935 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
936 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
937 addrconf_linkdown_change(net, newf);
938 if ((!newf) ^ (!old))
939 inet6_netconf_notify_devconf(net,
941 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
942 NETCONFA_IFINDEX_ALL,
943 net->ipv6.devconf_all);
952 /* Nobody refers to this ifaddr, destroy it */
953 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
955 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
957 #ifdef NET_REFCNT_DEBUG
958 pr_debug("%s\n", __func__);
961 in6_dev_put(ifp->idev);
963 if (cancel_delayed_work(&ifp->dad_work))
964 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
967 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
968 pr_warn("Freeing alive inet6 address %p\n", ifp);
976 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
979 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
982 * Each device address list is sorted in order of scope -
983 * global before linklocal.
985 list_for_each(p, &idev->addr_list) {
986 struct inet6_ifaddr *ifa
987 = list_entry(p, struct inet6_ifaddr, if_list);
988 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
992 list_add_tail_rcu(&ifp->if_list, p);
995 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
997 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
999 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
1002 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1003 struct net_device *dev, unsigned int hash)
1005 struct inet6_ifaddr *ifp;
1007 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1008 if (!net_eq(dev_net(ifp->idev->dev), net))
1010 if (ipv6_addr_equal(&ifp->addr, addr)) {
1011 if (!dev || ifp->idev->dev == dev)
1018 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1020 unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
1023 spin_lock(&addrconf_hash_lock);
1025 /* Ignore adding duplicate addresses on an interface */
1026 if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
1027 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1030 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1033 spin_unlock(&addrconf_hash_lock);
1038 /* On success it returns ifp with increased reference count */
1040 static struct inet6_ifaddr *
1041 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1042 bool can_block, struct netlink_ext_ack *extack)
1044 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1045 int addr_type = ipv6_addr_type(cfg->pfx);
1046 struct net *net = dev_net(idev->dev);
1047 struct inet6_ifaddr *ifa = NULL;
1048 struct fib6_info *f6i = NULL;
1051 if (addr_type == IPV6_ADDR_ANY ||
1052 addr_type & IPV6_ADDR_MULTICAST ||
1053 (!(idev->dev->flags & IFF_LOOPBACK) &&
1054 !netif_is_l3_master(idev->dev) &&
1055 addr_type & IPV6_ADDR_LOOPBACK))
1056 return ERR_PTR(-EADDRNOTAVAIL);
1059 err = -ENODEV; /*XXX*/
1063 if (idev->cnf.disable_ipv6) {
1068 /* validator notifier needs to be blocking;
1069 * do not call in atomic context
1072 struct in6_validator_info i6vi = {
1073 .i6vi_addr = *cfg->pfx,
1078 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1079 err = notifier_to_errno(err);
1084 ifa = kzalloc(sizeof(*ifa), gfp_flags);
1090 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1097 if (net->ipv6.devconf_all->disable_policy ||
1098 idev->cnf.disable_policy)
1099 f6i->dst_nopolicy = true;
1101 neigh_parms_data_state_setall(idev->nd_parms);
1103 ifa->addr = *cfg->pfx;
1105 ifa->peer_addr = *cfg->peer_pfx;
1107 spin_lock_init(&ifa->lock);
1108 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1109 INIT_HLIST_NODE(&ifa->addr_lst);
1110 ifa->scope = cfg->scope;
1111 ifa->prefix_len = cfg->plen;
1112 ifa->rt_priority = cfg->rt_priority;
1113 ifa->flags = cfg->ifa_flags;
1114 /* No need to add the TENTATIVE flag for addresses with NODAD */
1115 if (!(cfg->ifa_flags & IFA_F_NODAD))
1116 ifa->flags |= IFA_F_TENTATIVE;
1117 ifa->valid_lft = cfg->valid_lft;
1118 ifa->prefered_lft = cfg->preferred_lft;
1119 ifa->cstamp = ifa->tstamp = jiffies;
1120 ifa->tokenized = false;
1128 refcount_set(&ifa->refcnt, 1);
1132 err = ipv6_add_addr_hash(idev->dev, ifa);
1134 rcu_read_unlock_bh();
1138 write_lock(&idev->lock);
1140 /* Add to inet6_dev unicast addr list. */
1141 ipv6_link_dev_addr(idev, ifa);
1143 if (ifa->flags&IFA_F_TEMPORARY) {
1144 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1149 write_unlock(&idev->lock);
1151 rcu_read_unlock_bh();
1153 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1155 if (unlikely(err < 0)) {
1156 fib6_info_release(f6i);
1160 in6_dev_put(ifa->idev);
1169 enum cleanup_prefix_rt_t {
1170 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1171 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1172 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1176 * Check, whether the prefix for ifp would still need a prefix route
1177 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1180 * 1) we don't purge prefix if address was not permanent.
1181 * prefix is managed by its own lifetime.
1182 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1183 * 3) if there are no addresses, delete prefix.
1184 * 4) if there are still other permanent address(es),
1185 * corresponding prefix is still permanent.
1186 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1187 * don't purge the prefix, assume user space is managing it.
1188 * 6) otherwise, update prefix lifetime to the
1189 * longest valid lifetime among the corresponding
1190 * addresses on the device.
1191 * Note: subsequent RA will update lifetime.
1193 static enum cleanup_prefix_rt_t
1194 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1196 struct inet6_ifaddr *ifa;
1197 struct inet6_dev *idev = ifp->idev;
1198 unsigned long lifetime;
1199 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1203 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1206 if (ifa->prefix_len != ifp->prefix_len ||
1207 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1210 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1211 return CLEANUP_PREFIX_RT_NOP;
1213 action = CLEANUP_PREFIX_RT_EXPIRE;
1215 spin_lock(&ifa->lock);
1217 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1219 * Note: Because this address is
1220 * not permanent, lifetime <
1221 * LONG_MAX / HZ here.
1223 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1224 *expires = ifa->tstamp + lifetime * HZ;
1225 spin_unlock(&ifa->lock);
1232 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1234 struct fib6_info *f6i;
1236 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
1237 ifp->idev->dev, 0, RTF_DEFAULT, true);
1240 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
1242 if (!(f6i->fib6_flags & RTF_EXPIRES))
1243 fib6_set_expires(f6i, expires);
1244 fib6_info_release(f6i);
1250 /* This function wants to get referenced ifp and releases it before return */
1252 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1255 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1256 unsigned long expires;
1260 spin_lock_bh(&ifp->lock);
1262 ifp->state = INET6_IFADDR_STATE_DEAD;
1263 spin_unlock_bh(&ifp->lock);
1265 if (state == INET6_IFADDR_STATE_DEAD)
1268 spin_lock_bh(&addrconf_hash_lock);
1269 hlist_del_init_rcu(&ifp->addr_lst);
1270 spin_unlock_bh(&addrconf_hash_lock);
1272 write_lock_bh(&ifp->idev->lock);
1274 if (ifp->flags&IFA_F_TEMPORARY) {
1275 list_del(&ifp->tmp_list);
1277 in6_ifa_put(ifp->ifpub);
1283 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1284 action = check_cleanup_prefix_route(ifp, &expires);
1286 list_del_rcu(&ifp->if_list);
1289 write_unlock_bh(&ifp->idev->lock);
1291 addrconf_del_dad_work(ifp);
1293 ipv6_ifa_notify(RTM_DELADDR, ifp);
1295 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1297 if (action != CLEANUP_PREFIX_RT_NOP) {
1298 cleanup_prefix_route(ifp, expires,
1299 action == CLEANUP_PREFIX_RT_DEL);
1302 /* clean up prefsrc entries */
1303 rt6_remove_prefsrc(ifp);
1308 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp,
1309 struct inet6_ifaddr *ift,
1312 struct inet6_dev *idev = ifp->idev;
1313 struct in6_addr addr, *tmpaddr;
1314 unsigned long tmp_tstamp, age;
1315 unsigned long regen_advance;
1316 struct ifa6_config cfg;
1318 unsigned long now = jiffies;
1319 long max_desync_factor;
1320 s32 cnf_temp_preferred_lft;
1322 write_lock_bh(&idev->lock);
1324 spin_lock_bh(&ift->lock);
1325 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1326 spin_unlock_bh(&ift->lock);
1333 if (idev->cnf.use_tempaddr <= 0) {
1334 write_unlock_bh(&idev->lock);
1335 pr_info("%s: use_tempaddr is disabled\n", __func__);
1340 spin_lock_bh(&ifp->lock);
1341 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1342 idev->cnf.use_tempaddr = -1; /*XXX*/
1343 spin_unlock_bh(&ifp->lock);
1344 write_unlock_bh(&idev->lock);
1345 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1352 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1353 ipv6_try_regen_rndid(idev, tmpaddr);
1354 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1355 age = (now - ifp->tstamp) / HZ;
1357 regen_advance = idev->cnf.regen_max_retry *
1358 idev->cnf.dad_transmits *
1359 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1361 /* recalculate max_desync_factor each time and update
1362 * idev->desync_factor if it's larger
1364 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1365 max_desync_factor = min_t(__u32,
1366 idev->cnf.max_desync_factor,
1367 cnf_temp_preferred_lft - regen_advance);
1369 if (unlikely(idev->desync_factor > max_desync_factor)) {
1370 if (max_desync_factor > 0) {
1371 get_random_bytes(&idev->desync_factor,
1372 sizeof(idev->desync_factor));
1373 idev->desync_factor %= max_desync_factor;
1375 idev->desync_factor = 0;
1379 memset(&cfg, 0, sizeof(cfg));
1380 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
1381 idev->cnf.temp_valid_lft + age);
1382 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1383 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1385 cfg.plen = ifp->prefix_len;
1386 tmp_tstamp = ifp->tstamp;
1387 spin_unlock_bh(&ifp->lock);
1389 write_unlock_bh(&idev->lock);
1391 /* A temporary address is created only if this calculated Preferred
1392 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1393 * an implementation must not create a temporary address with a zero
1394 * Preferred Lifetime.
1395 * Use age calculation as in addrconf_verify to avoid unnecessary
1396 * temporary addresses being generated.
1398 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1399 if (cfg.preferred_lft <= regen_advance + age) {
1406 cfg.ifa_flags = IFA_F_TEMPORARY;
1407 /* set in addrconf_prefix_rcv() */
1408 if (ifp->flags & IFA_F_OPTIMISTIC)
1409 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
1412 cfg.scope = ipv6_addr_scope(cfg.pfx);
1414 ift = ipv6_add_addr(idev, &cfg, block, NULL);
1418 pr_info("%s: retry temporary address regeneration\n", __func__);
1420 write_lock_bh(&idev->lock);
1424 spin_lock_bh(&ift->lock);
1427 ift->tstamp = tmp_tstamp;
1428 spin_unlock_bh(&ift->lock);
1430 addrconf_dad_start(ift);
1438 * Choose an appropriate source address (RFC3484)
1441 IPV6_SADDR_RULE_INIT = 0,
1442 IPV6_SADDR_RULE_LOCAL,
1443 IPV6_SADDR_RULE_SCOPE,
1444 IPV6_SADDR_RULE_PREFERRED,
1445 #ifdef CONFIG_IPV6_MIP6
1446 IPV6_SADDR_RULE_HOA,
1448 IPV6_SADDR_RULE_OIF,
1449 IPV6_SADDR_RULE_LABEL,
1450 IPV6_SADDR_RULE_PRIVACY,
1451 IPV6_SADDR_RULE_ORCHID,
1452 IPV6_SADDR_RULE_PREFIX,
1453 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1454 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1459 struct ipv6_saddr_score {
1462 struct inet6_ifaddr *ifa;
1463 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1468 struct ipv6_saddr_dst {
1469 const struct in6_addr *addr;
1476 static inline int ipv6_saddr_preferred(int type)
1478 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1483 static bool ipv6_use_optimistic_addr(struct net *net,
1484 struct inet6_dev *idev)
1486 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1489 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1491 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1500 static bool ipv6_allow_optimistic_dad(struct net *net,
1501 struct inet6_dev *idev)
1503 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1506 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1515 static int ipv6_get_saddr_eval(struct net *net,
1516 struct ipv6_saddr_score *score,
1517 struct ipv6_saddr_dst *dst,
1522 if (i <= score->rule) {
1524 case IPV6_SADDR_RULE_SCOPE:
1525 ret = score->scopedist;
1527 case IPV6_SADDR_RULE_PREFIX:
1528 ret = score->matchlen;
1531 ret = !!test_bit(i, score->scorebits);
1537 case IPV6_SADDR_RULE_INIT:
1538 /* Rule 0: remember if hiscore is not ready yet */
1541 case IPV6_SADDR_RULE_LOCAL:
1542 /* Rule 1: Prefer same address */
1543 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1545 case IPV6_SADDR_RULE_SCOPE:
1546 /* Rule 2: Prefer appropriate scope
1551 * ---+--+-+---> scope
1553 * | d is scope of the destination.
1555 * | \ <- smaller scope is better if
1556 * B-15 | \ if scope is enough for destination.
1557 * | ret = B - scope (-1 <= scope >= d <= 15).
1559 * |/ <- greater is better
1560 * -C / if scope is not enough for destination.
1561 * /| ret = scope - C (-1 <= d < scope <= 15).
1563 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1564 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1565 * Assume B = 0 and we get C > 29.
1567 ret = __ipv6_addr_src_scope(score->addr_type);
1568 if (ret >= dst->scope)
1571 ret -= 128; /* 30 is enough */
1572 score->scopedist = ret;
1574 case IPV6_SADDR_RULE_PREFERRED:
1576 /* Rule 3: Avoid deprecated and optimistic addresses */
1577 u8 avoid = IFA_F_DEPRECATED;
1579 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
1580 avoid |= IFA_F_OPTIMISTIC;
1581 ret = ipv6_saddr_preferred(score->addr_type) ||
1582 !(score->ifa->flags & avoid);
1585 #ifdef CONFIG_IPV6_MIP6
1586 case IPV6_SADDR_RULE_HOA:
1588 /* Rule 4: Prefer home address */
1589 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1590 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1594 case IPV6_SADDR_RULE_OIF:
1595 /* Rule 5: Prefer outgoing interface */
1596 ret = (!dst->ifindex ||
1597 dst->ifindex == score->ifa->idev->dev->ifindex);
1599 case IPV6_SADDR_RULE_LABEL:
1600 /* Rule 6: Prefer matching label */
1601 ret = ipv6_addr_label(net,
1602 &score->ifa->addr, score->addr_type,
1603 score->ifa->idev->dev->ifindex) == dst->label;
1605 case IPV6_SADDR_RULE_PRIVACY:
1607 /* Rule 7: Prefer public address
1608 * Note: prefer temporary address if use_tempaddr >= 2
1610 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1611 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1612 score->ifa->idev->cnf.use_tempaddr >= 2;
1613 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1616 case IPV6_SADDR_RULE_ORCHID:
1617 /* Rule 8-: Prefer ORCHID vs ORCHID or
1618 * non-ORCHID vs non-ORCHID
1620 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1621 ipv6_addr_orchid(dst->addr));
1623 case IPV6_SADDR_RULE_PREFIX:
1624 /* Rule 8: Use longest matching prefix */
1625 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1626 if (ret > score->ifa->prefix_len)
1627 ret = score->ifa->prefix_len;
1628 score->matchlen = ret;
1630 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1631 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1632 /* Optimistic addresses still have lower precedence than other
1633 * preferred addresses.
1635 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1643 __set_bit(i, score->scorebits);
1649 static int __ipv6_dev_get_saddr(struct net *net,
1650 struct ipv6_saddr_dst *dst,
1651 struct inet6_dev *idev,
1652 struct ipv6_saddr_score *scores,
1655 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1657 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1661 * - Tentative Address (RFC2462 section 5.4)
1662 * - A tentative address is not considered
1663 * "assigned to an interface" in the traditional
1664 * sense, unless it is also flagged as optimistic.
1665 * - Candidate Source Address (section 4)
1666 * - In any case, anycast addresses, multicast
1667 * addresses, and the unspecified address MUST
1668 * NOT be included in a candidate set.
1670 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1671 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1674 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1676 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1677 score->addr_type & IPV6_ADDR_MULTICAST)) {
1678 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1684 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1686 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1687 int minihiscore, miniscore;
1689 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1690 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1692 if (minihiscore > miniscore) {
1693 if (i == IPV6_SADDR_RULE_SCOPE &&
1694 score->scopedist > 0) {
1697 * each remaining entry
1698 * has too small (not enough)
1699 * scope, because ifa entries
1700 * are sorted by their scope
1706 } else if (minihiscore < miniscore) {
1707 swap(hiscore, score);
1708 hiscore_idx = 1 - hiscore_idx;
1710 /* restore our iterator */
1711 score->ifa = hiscore->ifa;
1721 static int ipv6_get_saddr_master(struct net *net,
1722 const struct net_device *dst_dev,
1723 const struct net_device *master,
1724 struct ipv6_saddr_dst *dst,
1725 struct ipv6_saddr_score *scores,
1728 struct inet6_dev *idev;
1730 idev = __in6_dev_get(dst_dev);
1732 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1733 scores, hiscore_idx);
1735 idev = __in6_dev_get(master);
1737 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1738 scores, hiscore_idx);
1743 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1744 const struct in6_addr *daddr, unsigned int prefs,
1745 struct in6_addr *saddr)
1747 struct ipv6_saddr_score scores[2], *hiscore;
1748 struct ipv6_saddr_dst dst;
1749 struct inet6_dev *idev;
1750 struct net_device *dev;
1752 bool use_oif_addr = false;
1753 int hiscore_idx = 0;
1756 dst_type = __ipv6_addr_type(daddr);
1758 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1759 dst.scope = __ipv6_addr_src_scope(dst_type);
1760 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1763 scores[hiscore_idx].rule = -1;
1764 scores[hiscore_idx].ifa = NULL;
1768 /* Candidate Source Address (section 4)
1769 * - multicast and link-local destination address,
1770 * the set of candidate source address MUST only
1771 * include addresses assigned to interfaces
1772 * belonging to the same link as the outgoing
1774 * (- For site-local destination addresses, the
1775 * set of candidate source addresses MUST only
1776 * include addresses assigned to interfaces
1777 * belonging to the same site as the outgoing
1779 * - "It is RECOMMENDED that the candidate source addresses
1780 * be the set of unicast addresses assigned to the
1781 * interface that will be used to send to the destination
1782 * (the 'outgoing' interface)." (RFC 6724)
1785 idev = __in6_dev_get(dst_dev);
1786 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1787 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1788 (idev && idev->cnf.use_oif_addrs_only)) {
1789 use_oif_addr = true;
1795 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1797 const struct net_device *master;
1800 /* if dst_dev exists and is enslaved to an L3 device, then
1801 * prefer addresses from dst_dev and then the master over
1802 * any other enslaved devices in the L3 domain.
1804 master = l3mdev_master_dev_rcu(dst_dev);
1806 master_idx = master->ifindex;
1808 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1810 scores, hiscore_idx);
1812 if (scores[hiscore_idx].ifa)
1816 for_each_netdev_rcu(net, dev) {
1817 /* only consider addresses on devices in the
1820 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1822 idev = __in6_dev_get(dev);
1825 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1830 hiscore = &scores[hiscore_idx];
1832 ret = -EADDRNOTAVAIL;
1834 *saddr = hiscore->ifa->addr;
1839 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1841 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1844 struct inet6_ifaddr *ifp;
1845 int err = -EADDRNOTAVAIL;
1847 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1848 if (ifp->scope > IFA_LINK)
1850 if (ifp->scope == IFA_LINK &&
1851 !(ifp->flags & banned_flags)) {
1860 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1863 struct inet6_dev *idev;
1864 int err = -EADDRNOTAVAIL;
1867 idev = __in6_dev_get(dev);
1869 read_lock_bh(&idev->lock);
1870 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1871 read_unlock_bh(&idev->lock);
1877 static int ipv6_count_addresses(const struct inet6_dev *idev)
1879 const struct inet6_ifaddr *ifp;
1883 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1889 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1890 const struct net_device *dev, int strict)
1892 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1893 strict, IFA_F_TENTATIVE);
1895 EXPORT_SYMBOL(ipv6_chk_addr);
1897 /* device argument is used to find the L3 domain of interest. If
1898 * skip_dev_check is set, then the ifp device is not checked against
1899 * the passed in dev argument. So the 2 cases for addresses checks are:
1900 * 1. does the address exist in the L3 domain that dev is part of
1901 * (skip_dev_check = true), or
1903 * 2. does the address exist on the specific device
1904 * (skip_dev_check = false)
1906 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1907 const struct net_device *dev, bool skip_dev_check,
1908 int strict, u32 banned_flags)
1910 unsigned int hash = inet6_addr_hash(net, addr);
1911 const struct net_device *l3mdev;
1912 struct inet6_ifaddr *ifp;
1917 l3mdev = l3mdev_master_dev_rcu(dev);
1921 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1922 if (!net_eq(dev_net(ifp->idev->dev), net))
1925 if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
1928 /* Decouple optimistic from tentative for evaluation here.
1929 * Ban optimistic addresses explicitly, when required.
1931 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1932 ? (ifp->flags&~IFA_F_TENTATIVE)
1934 if (ipv6_addr_equal(&ifp->addr, addr) &&
1935 !(ifp_flags&banned_flags) &&
1936 (!dev || ifp->idev->dev == dev ||
1937 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1946 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1949 /* Compares an address/prefix_len with addresses on device @dev.
1950 * If one is found it returns true.
1952 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1953 const unsigned int prefix_len, struct net_device *dev)
1955 const struct inet6_ifaddr *ifa;
1956 const struct inet6_dev *idev;
1960 idev = __in6_dev_get(dev);
1962 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1963 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1972 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1974 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1976 const struct inet6_ifaddr *ifa;
1977 const struct inet6_dev *idev;
1982 idev = __in6_dev_get(dev);
1984 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1985 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1994 EXPORT_SYMBOL(ipv6_chk_prefix);
1996 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1997 struct net_device *dev, int strict)
1999 unsigned int hash = inet6_addr_hash(net, addr);
2000 struct inet6_ifaddr *ifp, *result = NULL;
2003 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
2004 if (!net_eq(dev_net(ifp->idev->dev), net))
2006 if (ipv6_addr_equal(&ifp->addr, addr)) {
2007 if (!dev || ifp->idev->dev == dev ||
2008 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2020 /* Gets referenced address, destroys ifaddr */
2022 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2025 ifp->flags |= IFA_F_DADFAILED;
2027 if (ifp->flags&IFA_F_TEMPORARY) {
2028 struct inet6_ifaddr *ifpub;
2029 spin_lock_bh(&ifp->lock);
2032 in6_ifa_hold(ifpub);
2033 spin_unlock_bh(&ifp->lock);
2034 ipv6_create_tempaddr(ifpub, ifp, true);
2037 spin_unlock_bh(&ifp->lock);
2040 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2041 spin_lock_bh(&ifp->lock);
2042 addrconf_del_dad_work(ifp);
2043 ifp->flags |= IFA_F_TENTATIVE;
2045 ifp->flags &= ~IFA_F_OPTIMISTIC;
2046 spin_unlock_bh(&ifp->lock);
2048 ipv6_ifa_notify(0, ifp);
2055 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2059 spin_lock_bh(&ifp->lock);
2060 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2061 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2064 spin_unlock_bh(&ifp->lock);
2069 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2071 struct inet6_dev *idev = ifp->idev;
2072 struct net *net = dev_net(ifp->idev->dev);
2074 if (addrconf_dad_end(ifp)) {
2079 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2080 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2082 spin_lock_bh(&ifp->lock);
2084 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2085 struct in6_addr new_addr;
2086 struct inet6_ifaddr *ifp2;
2087 int retries = ifp->stable_privacy_retry + 1;
2088 struct ifa6_config cfg = {
2090 .plen = ifp->prefix_len,
2091 .ifa_flags = ifp->flags,
2092 .valid_lft = ifp->valid_lft,
2093 .preferred_lft = ifp->prefered_lft,
2094 .scope = ifp->scope,
2097 if (retries > net->ipv6.sysctl.idgen_retries) {
2098 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2099 ifp->idev->dev->name);
2103 new_addr = ifp->addr;
2104 if (ipv6_generate_stable_address(&new_addr, retries,
2108 spin_unlock_bh(&ifp->lock);
2110 if (idev->cnf.max_addresses &&
2111 ipv6_count_addresses(idev) >=
2112 idev->cnf.max_addresses)
2115 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2116 ifp->idev->dev->name);
2118 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2122 spin_lock_bh(&ifp2->lock);
2123 ifp2->stable_privacy_retry = retries;
2124 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2125 spin_unlock_bh(&ifp2->lock);
2127 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2130 spin_lock_bh(&ifp->lock);
2134 /* transition from _POSTDAD to _ERRDAD */
2135 ifp->state = INET6_IFADDR_STATE_ERRDAD;
2136 spin_unlock_bh(&ifp->lock);
2138 addrconf_mod_dad_work(ifp, 0);
2142 /* Join to solicited addr multicast group.
2143 * caller must hold RTNL */
2144 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2146 struct in6_addr maddr;
2148 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2151 addrconf_addr_solict_mult(addr, &maddr);
2152 ipv6_dev_mc_inc(dev, &maddr);
2155 /* caller must hold RTNL */
2156 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2158 struct in6_addr maddr;
2160 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2163 addrconf_addr_solict_mult(addr, &maddr);
2164 __ipv6_dev_mc_dec(idev, &maddr);
2167 /* caller must hold RTNL */
2168 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2170 struct in6_addr addr;
2172 if (ifp->prefix_len >= 127) /* RFC 6164 */
2174 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2175 if (ipv6_addr_any(&addr))
2177 __ipv6_dev_ac_inc(ifp->idev, &addr);
2180 /* caller must hold RTNL */
2181 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2183 struct in6_addr addr;
2185 if (ifp->prefix_len >= 127) /* RFC 6164 */
2187 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2188 if (ipv6_addr_any(&addr))
2190 __ipv6_dev_ac_dec(ifp->idev, &addr);
2193 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2195 switch (dev->addr_len) {
2197 memcpy(eui, dev->dev_addr, 3);
2200 memcpy(eui + 5, dev->dev_addr + 3, 3);
2202 case EUI64_ADDR_LEN:
2203 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2213 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2215 union fwnet_hwaddr *ha;
2217 if (dev->addr_len != FWNET_ALEN)
2220 ha = (union fwnet_hwaddr *)dev->dev_addr;
2222 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2227 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2229 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2230 if (dev->addr_len != ARCNET_ALEN)
2233 eui[7] = *(u8 *)dev->dev_addr;
2237 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2239 if (dev->addr_len != INFINIBAND_ALEN)
2241 memcpy(eui, dev->dev_addr + 12, 8);
2246 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2250 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2251 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2252 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2253 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2254 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2255 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2259 memcpy(eui + 4, &addr, 4);
2263 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2265 if (dev->priv_flags & IFF_ISATAP)
2266 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2270 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2272 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2275 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2277 memcpy(eui, dev->perm_addr, 3);
2278 memcpy(eui + 5, dev->perm_addr + 3, 3);
2285 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2287 switch (dev->type) {
2290 return addrconf_ifid_eui48(eui, dev);
2292 return addrconf_ifid_arcnet(eui, dev);
2293 case ARPHRD_INFINIBAND:
2294 return addrconf_ifid_infiniband(eui, dev);
2296 return addrconf_ifid_sit(eui, dev);
2299 return addrconf_ifid_gre(eui, dev);
2300 case ARPHRD_6LOWPAN:
2301 return addrconf_ifid_6lowpan(eui, dev);
2302 case ARPHRD_IEEE1394:
2303 return addrconf_ifid_ieee1394(eui, dev);
2304 case ARPHRD_TUNNEL6:
2307 return addrconf_ifid_ip6tnl(eui, dev);
2312 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2315 struct inet6_ifaddr *ifp;
2317 read_lock_bh(&idev->lock);
2318 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2319 if (ifp->scope > IFA_LINK)
2321 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2322 memcpy(eui, ifp->addr.s6_addr+8, 8);
2327 read_unlock_bh(&idev->lock);
2331 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2332 static void ipv6_regen_rndid(struct inet6_dev *idev)
2335 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2336 idev->rndid[0] &= ~0x02;
2339 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2340 * check if generated address is not inappropriate
2342 * - Reserved subnet anycast (RFC 2526)
2343 * 11111101 11....11 1xxxxxxx
2344 * - ISATAP (RFC4214) 6.1
2345 * 00-00-5E-FE-xx-xx-xx-xx
2347 * - XXX: already assigned to an address on the device
2349 if (idev->rndid[0] == 0xfd &&
2350 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2351 (idev->rndid[7]&0x80))
2353 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2354 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2356 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2361 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2363 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2364 ipv6_regen_rndid(idev);
2372 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2373 struct net_device *dev, unsigned long expires,
2374 u32 flags, gfp_t gfp_flags)
2376 struct fib6_config cfg = {
2377 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2378 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2379 .fc_ifindex = dev->ifindex,
2380 .fc_expires = expires,
2382 .fc_flags = RTF_UP | flags,
2383 .fc_nlinfo.nl_net = dev_net(dev),
2384 .fc_protocol = RTPROT_KERNEL,
2385 .fc_type = RTN_UNICAST,
2390 /* Prevent useless cloning on PtP SIT.
2391 This thing is done here expecting that the whole
2392 class of non-broadcast devices need not cloning.
2394 #if IS_ENABLED(CONFIG_IPV6_SIT)
2395 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2396 cfg.fc_flags |= RTF_NONEXTHOP;
2399 ip6_route_add(&cfg, gfp_flags, NULL);
2403 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2405 const struct net_device *dev,
2406 u32 flags, u32 noflags,
2409 struct fib6_node *fn;
2410 struct fib6_info *rt = NULL;
2411 struct fib6_table *table;
2412 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2414 table = fib6_get_table(dev_net(dev), tb_id);
2419 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2423 for_each_fib6_node_rt_rcu(fn) {
2424 if (rt->fib6_nh.fib_nh_dev->ifindex != dev->ifindex)
2426 if (no_gw && rt->fib6_nh.fib_nh_gw_family)
2428 if ((rt->fib6_flags & flags) != flags)
2430 if ((rt->fib6_flags & noflags) != 0)
2432 if (!fib6_info_hold_safe(rt))
2442 /* Create "default" multicast route to the interface */
2444 static void addrconf_add_mroute(struct net_device *dev)
2446 struct fib6_config cfg = {
2447 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2448 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2449 .fc_ifindex = dev->ifindex,
2452 .fc_type = RTN_UNICAST,
2453 .fc_nlinfo.nl_net = dev_net(dev),
2456 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2458 ip6_route_add(&cfg, GFP_KERNEL, NULL);
2461 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2463 struct inet6_dev *idev;
2467 idev = ipv6_find_idev(dev);
2469 return ERR_PTR(-ENOBUFS);
2471 if (idev->cnf.disable_ipv6)
2472 return ERR_PTR(-EACCES);
2474 /* Add default multicast route */
2475 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2476 addrconf_add_mroute(dev);
2481 static void manage_tempaddrs(struct inet6_dev *idev,
2482 struct inet6_ifaddr *ifp,
2483 __u32 valid_lft, __u32 prefered_lft,
2484 bool create, unsigned long now)
2487 struct inet6_ifaddr *ift;
2489 read_lock_bh(&idev->lock);
2490 /* update all temporary addresses in the list */
2491 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2492 int age, max_valid, max_prefered;
2494 if (ifp != ift->ifpub)
2497 /* RFC 4941 section 3.3:
2498 * If a received option will extend the lifetime of a public
2499 * address, the lifetimes of temporary addresses should
2500 * be extended, subject to the overall constraint that no
2501 * temporary addresses should ever remain "valid" or "preferred"
2502 * for a time longer than (TEMP_VALID_LIFETIME) or
2503 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2505 age = (now - ift->cstamp) / HZ;
2506 max_valid = idev->cnf.temp_valid_lft - age;
2510 max_prefered = idev->cnf.temp_prefered_lft -
2511 idev->desync_factor - age;
2512 if (max_prefered < 0)
2515 if (valid_lft > max_valid)
2516 valid_lft = max_valid;
2518 if (prefered_lft > max_prefered)
2519 prefered_lft = max_prefered;
2521 spin_lock(&ift->lock);
2523 ift->valid_lft = valid_lft;
2524 ift->prefered_lft = prefered_lft;
2526 if (prefered_lft > 0)
2527 ift->flags &= ~IFA_F_DEPRECATED;
2529 spin_unlock(&ift->lock);
2530 if (!(flags&IFA_F_TENTATIVE))
2531 ipv6_ifa_notify(0, ift);
2534 if ((create || list_empty(&idev->tempaddr_list)) &&
2535 idev->cnf.use_tempaddr > 0) {
2536 /* When a new public address is created as described
2537 * in [ADDRCONF], also create a new temporary address.
2538 * Also create a temporary address if it's enabled but
2539 * no temporary address currently exists.
2541 read_unlock_bh(&idev->lock);
2542 ipv6_create_tempaddr(ifp, NULL, false);
2544 read_unlock_bh(&idev->lock);
2548 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2550 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2551 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2554 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2555 const struct prefix_info *pinfo,
2556 struct inet6_dev *in6_dev,
2557 const struct in6_addr *addr, int addr_type,
2558 u32 addr_flags, bool sllao, bool tokenized,
2559 __u32 valid_lft, u32 prefered_lft)
2561 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2562 int create = 0, update_lft = 0;
2564 if (!ifp && valid_lft) {
2565 int max_addresses = in6_dev->cnf.max_addresses;
2566 struct ifa6_config cfg = {
2568 .plen = pinfo->prefix_len,
2569 .ifa_flags = addr_flags,
2570 .valid_lft = valid_lft,
2571 .preferred_lft = prefered_lft,
2572 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2575 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2576 if ((net->ipv6.devconf_all->optimistic_dad ||
2577 in6_dev->cnf.optimistic_dad) &&
2578 !net->ipv6.devconf_all->forwarding && sllao)
2579 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2582 /* Do not allow to create too much of autoconfigured
2583 * addresses; this would be too easy way to crash kernel.
2585 if (!max_addresses ||
2586 ipv6_count_addresses(in6_dev) < max_addresses)
2587 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2589 if (IS_ERR_OR_NULL(ifp))
2593 spin_lock_bh(&ifp->lock);
2594 ifp->flags |= IFA_F_MANAGETEMPADDR;
2595 ifp->cstamp = jiffies;
2596 ifp->tokenized = tokenized;
2597 spin_unlock_bh(&ifp->lock);
2598 addrconf_dad_start(ifp);
2606 /* update lifetime (RFC2462 5.5.3 e) */
2607 spin_lock_bh(&ifp->lock);
2609 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2610 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2613 if (!create && stored_lft) {
2614 const u32 minimum_lft = min_t(u32,
2615 stored_lft, MIN_VALID_LIFETIME);
2616 valid_lft = max(valid_lft, minimum_lft);
2618 /* RFC4862 Section 5.5.3e:
2619 * "Note that the preferred lifetime of the
2620 * corresponding address is always reset to
2621 * the Preferred Lifetime in the received
2622 * Prefix Information option, regardless of
2623 * whether the valid lifetime is also reset or
2626 * So we should always update prefered_lft here.
2632 ifp->valid_lft = valid_lft;
2633 ifp->prefered_lft = prefered_lft;
2636 ifp->flags &= ~IFA_F_DEPRECATED;
2637 spin_unlock_bh(&ifp->lock);
2639 if (!(flags&IFA_F_TENTATIVE))
2640 ipv6_ifa_notify(0, ifp);
2642 spin_unlock_bh(&ifp->lock);
2644 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2653 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2655 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2657 struct prefix_info *pinfo;
2662 struct inet6_dev *in6_dev;
2663 struct net *net = dev_net(dev);
2665 pinfo = (struct prefix_info *) opt;
2667 if (len < sizeof(struct prefix_info)) {
2668 netdev_dbg(dev, "addrconf: prefix option too short\n");
2673 * Validation checks ([ADDRCONF], page 19)
2676 addr_type = ipv6_addr_type(&pinfo->prefix);
2678 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2681 valid_lft = ntohl(pinfo->valid);
2682 prefered_lft = ntohl(pinfo->prefered);
2684 if (prefered_lft > valid_lft) {
2685 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2689 in6_dev = in6_dev_get(dev);
2692 net_dbg_ratelimited("addrconf: device %s not configured\n",
2698 * Two things going on here:
2699 * 1) Add routes for on-link prefixes
2700 * 2) Configure prefixes with the auto flag set
2703 if (pinfo->onlink) {
2704 struct fib6_info *rt;
2705 unsigned long rt_expires;
2707 /* Avoid arithmetic overflow. Really, we could
2708 * save rt_expires in seconds, likely valid_lft,
2709 * but it would require division in fib gc, that it
2713 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2715 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2717 if (addrconf_finite_timeout(rt_expires))
2720 rt = addrconf_get_prefix_route(&pinfo->prefix,
2723 RTF_ADDRCONF | RTF_PREFIX_RT,
2727 /* Autoconf prefix route */
2728 if (valid_lft == 0) {
2729 ip6_del_rt(net, rt);
2731 } else if (addrconf_finite_timeout(rt_expires)) {
2733 fib6_set_expires(rt, jiffies + rt_expires);
2735 fib6_clean_expires(rt);
2737 } else if (valid_lft) {
2738 clock_t expires = 0;
2739 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2740 if (addrconf_finite_timeout(rt_expires)) {
2742 flags |= RTF_EXPIRES;
2743 expires = jiffies_to_clock_t(rt_expires);
2745 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2746 0, dev, expires, flags,
2749 fib6_info_release(rt);
2752 /* Try to figure out our local address for this prefix */
2754 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2755 struct in6_addr addr;
2756 bool tokenized = false, dev_addr_generated = false;
2758 if (pinfo->prefix_len == 64) {
2759 memcpy(&addr, &pinfo->prefix, 8);
2761 if (!ipv6_addr_any(&in6_dev->token)) {
2762 read_lock_bh(&in6_dev->lock);
2763 memcpy(addr.s6_addr + 8,
2764 in6_dev->token.s6_addr + 8, 8);
2765 read_unlock_bh(&in6_dev->lock);
2767 } else if (is_addr_mode_generate_stable(in6_dev) &&
2768 !ipv6_generate_stable_address(&addr, 0,
2770 addr_flags |= IFA_F_STABLE_PRIVACY;
2772 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2773 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2776 dev_addr_generated = true;
2780 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2785 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2788 tokenized, valid_lft,
2793 /* Ignore error case here because previous prefix add addr was
2794 * successful which will be notified.
2796 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2797 addr_type, addr_flags, sllao,
2798 tokenized, valid_lft,
2800 dev_addr_generated);
2802 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2804 in6_dev_put(in6_dev);
2808 * Set destination address.
2809 * Special case for SIT interfaces where we create a new "virtual"
2812 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2814 struct in6_ifreq ireq;
2815 struct net_device *dev;
2821 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2824 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2830 #if IS_ENABLED(CONFIG_IPV6_SIT)
2831 if (dev->type == ARPHRD_SIT) {
2832 const struct net_device_ops *ops = dev->netdev_ops;
2834 struct ip_tunnel_parm p;
2836 err = -EADDRNOTAVAIL;
2837 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2840 memset(&p, 0, sizeof(p));
2841 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2845 p.iph.protocol = IPPROTO_IPV6;
2847 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2849 if (ops->ndo_do_ioctl) {
2850 mm_segment_t oldfs = get_fs();
2853 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2860 dev = __dev_get_by_name(net, p.name);
2863 err = dev_open(dev, NULL);
2873 static int ipv6_mc_config(struct sock *sk, bool join,
2874 const struct in6_addr *addr, int ifindex)
2882 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2884 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2891 * Manual configuration of address on an interface
2893 static int inet6_addr_add(struct net *net, int ifindex,
2894 struct ifa6_config *cfg,
2895 struct netlink_ext_ack *extack)
2897 struct inet6_ifaddr *ifp;
2898 struct inet6_dev *idev;
2899 struct net_device *dev;
2900 unsigned long timeout;
2906 if (cfg->plen > 128)
2909 /* check the lifetime */
2910 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
2913 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
2916 dev = __dev_get_by_index(net, ifindex);
2920 idev = addrconf_add_dev(dev);
2922 return PTR_ERR(idev);
2924 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2925 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2926 true, cfg->pfx, ifindex);
2932 cfg->scope = ipv6_addr_scope(cfg->pfx);
2934 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
2935 if (addrconf_finite_timeout(timeout)) {
2936 expires = jiffies_to_clock_t(timeout * HZ);
2937 cfg->valid_lft = timeout;
2938 flags = RTF_EXPIRES;
2942 cfg->ifa_flags |= IFA_F_PERMANENT;
2945 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
2946 if (addrconf_finite_timeout(timeout)) {
2948 cfg->ifa_flags |= IFA_F_DEPRECATED;
2949 cfg->preferred_lft = timeout;
2952 ifp = ipv6_add_addr(idev, cfg, true, extack);
2954 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
2955 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2956 ifp->rt_priority, dev, expires,
2960 /* Send a netlink notification if DAD is enabled and
2961 * optimistic flag is not set
2963 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2964 ipv6_ifa_notify(0, ifp);
2966 * Note that section 3.1 of RFC 4429 indicates
2967 * that the Optimistic flag should not be set for
2968 * manually configured addresses
2970 addrconf_dad_start(ifp);
2971 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
2972 manage_tempaddrs(idev, ifp, cfg->valid_lft,
2973 cfg->preferred_lft, true, jiffies);
2975 addrconf_verify_rtnl();
2977 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2978 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
2982 return PTR_ERR(ifp);
2985 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2986 const struct in6_addr *pfx, unsigned int plen)
2988 struct inet6_ifaddr *ifp;
2989 struct inet6_dev *idev;
2990 struct net_device *dev;
2995 dev = __dev_get_by_index(net, ifindex);
2999 idev = __in6_dev_get(dev);
3003 read_lock_bh(&idev->lock);
3004 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3005 if (ifp->prefix_len == plen &&
3006 ipv6_addr_equal(pfx, &ifp->addr)) {
3008 read_unlock_bh(&idev->lock);
3010 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3011 (ifa_flags & IFA_F_MANAGETEMPADDR))
3012 manage_tempaddrs(idev, ifp, 0, 0, false,
3015 addrconf_verify_rtnl();
3016 if (ipv6_addr_is_multicast(pfx)) {
3017 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3018 false, pfx, dev->ifindex);
3023 read_unlock_bh(&idev->lock);
3024 return -EADDRNOTAVAIL;
3028 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3030 struct ifa6_config cfg = {
3031 .ifa_flags = IFA_F_PERMANENT,
3032 .preferred_lft = INFINITY_LIFE_TIME,
3033 .valid_lft = INFINITY_LIFE_TIME,
3035 struct in6_ifreq ireq;
3038 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3041 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3044 cfg.pfx = &ireq.ifr6_addr;
3045 cfg.plen = ireq.ifr6_prefixlen;
3048 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
3053 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3055 struct in6_ifreq ireq;
3058 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3061 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3065 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3066 ireq.ifr6_prefixlen);
3071 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3072 int plen, int scope)
3074 struct inet6_ifaddr *ifp;
3075 struct ifa6_config cfg = {
3078 .ifa_flags = IFA_F_PERMANENT,
3079 .valid_lft = INFINITY_LIFE_TIME,
3080 .preferred_lft = INFINITY_LIFE_TIME,
3084 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3086 spin_lock_bh(&ifp->lock);
3087 ifp->flags &= ~IFA_F_TENTATIVE;
3088 spin_unlock_bh(&ifp->lock);
3089 rt_genid_bump_ipv6(dev_net(idev->dev));
3090 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3095 #if IS_ENABLED(CONFIG_IPV6_SIT)
3096 static void sit_add_v4_addrs(struct inet6_dev *idev)
3098 struct in6_addr addr;
3099 struct net_device *dev;
3100 struct net *net = dev_net(idev->dev);
3106 memset(&addr, 0, sizeof(struct in6_addr));
3107 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3109 if (idev->dev->flags&IFF_POINTOPOINT) {
3110 addr.s6_addr32[0] = htonl(0xfe800000);
3114 scope = IPV6_ADDR_COMPATv4;
3116 pflags |= RTF_NONEXTHOP;
3119 if (addr.s6_addr32[3]) {
3120 add_addr(idev, &addr, plen, scope);
3121 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3126 for_each_netdev(net, dev) {
3127 struct in_device *in_dev = __in_dev_get_rtnl(dev);
3128 if (in_dev && (dev->flags & IFF_UP)) {
3129 struct in_ifaddr *ifa;
3133 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
3135 addr.s6_addr32[3] = ifa->ifa_local;
3137 if (ifa->ifa_scope == RT_SCOPE_LINK)
3139 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3140 if (idev->dev->flags&IFF_POINTOPOINT)
3145 add_addr(idev, &addr, plen, flag);
3146 addrconf_prefix_route(&addr, plen, 0, idev->dev,
3147 0, pflags, GFP_KERNEL);
3154 static void init_loopback(struct net_device *dev)
3156 struct inet6_dev *idev;
3162 idev = ipv6_find_idev(dev);
3164 pr_debug("%s: add_dev failed\n", __func__);
3168 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
3171 void addrconf_add_linklocal(struct inet6_dev *idev,
3172 const struct in6_addr *addr, u32 flags)
3174 struct ifa6_config cfg = {
3177 .ifa_flags = flags | IFA_F_PERMANENT,
3178 .valid_lft = INFINITY_LIFE_TIME,
3179 .preferred_lft = INFINITY_LIFE_TIME,
3182 struct inet6_ifaddr *ifp;
3184 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3185 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3186 idev->cnf.optimistic_dad) &&
3187 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3188 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3191 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3193 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3195 addrconf_dad_start(ifp);
3199 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3201 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3203 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3206 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3207 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3210 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3211 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3217 static int ipv6_generate_stable_address(struct in6_addr *address,
3219 const struct inet6_dev *idev)
3221 static DEFINE_SPINLOCK(lock);
3222 static __u32 digest[SHA_DIGEST_WORDS];
3223 static __u32 workspace[SHA_WORKSPACE_WORDS];
3226 char __data[SHA_MESSAGE_BYTES];
3228 struct in6_addr secret;
3230 unsigned char hwaddr[MAX_ADDR_LEN];
3235 struct in6_addr secret;
3236 struct in6_addr temp;
3237 struct net *net = dev_net(idev->dev);
3239 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3241 if (idev->cnf.stable_secret.initialized)
3242 secret = idev->cnf.stable_secret.secret;
3243 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3244 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3249 spin_lock_bh(&lock);
3252 memset(&data, 0, sizeof(data));
3253 memset(workspace, 0, sizeof(workspace));
3254 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3255 data.prefix[0] = address->s6_addr32[0];
3256 data.prefix[1] = address->s6_addr32[1];
3257 data.secret = secret;
3258 data.dad_count = dad_count;
3260 sha_transform(digest, data.__data, workspace);
3263 temp.s6_addr32[2] = (__force __be32)digest[0];
3264 temp.s6_addr32[3] = (__force __be32)digest[1];
3266 spin_unlock_bh(&lock);
3268 if (ipv6_reserved_interfaceid(temp)) {
3270 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3279 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3281 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3285 s = &idev->cnf.stable_secret;
3286 get_random_bytes(&s->secret, sizeof(s->secret));
3287 s->initialized = true;
3290 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3292 struct in6_addr addr;
3294 /* no link local addresses on L3 master devices */
3295 if (netif_is_l3_master(idev->dev))
3298 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3300 switch (idev->cnf.addr_gen_mode) {
3301 case IN6_ADDR_GEN_MODE_RANDOM:
3302 ipv6_gen_mode_random_init(idev);
3304 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3305 if (!ipv6_generate_stable_address(&addr, 0, idev))
3306 addrconf_add_linklocal(idev, &addr,
3307 IFA_F_STABLE_PRIVACY);
3308 else if (prefix_route)
3309 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3312 case IN6_ADDR_GEN_MODE_EUI64:
3313 /* addrconf_add_linklocal also adds a prefix_route and we
3314 * only need to care about prefix routes if ipv6_generate_eui64
3315 * couldn't generate one.
3317 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3318 addrconf_add_linklocal(idev, &addr, 0);
3319 else if (prefix_route)
3320 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3323 case IN6_ADDR_GEN_MODE_NONE:
3325 /* will not add any link local address */
3330 static void addrconf_dev_config(struct net_device *dev)
3332 struct inet6_dev *idev;
3336 if ((dev->type != ARPHRD_ETHER) &&
3337 (dev->type != ARPHRD_FDDI) &&
3338 (dev->type != ARPHRD_ARCNET) &&
3339 (dev->type != ARPHRD_INFINIBAND) &&
3340 (dev->type != ARPHRD_IEEE1394) &&
3341 (dev->type != ARPHRD_TUNNEL6) &&
3342 (dev->type != ARPHRD_6LOWPAN) &&
3343 (dev->type != ARPHRD_IP6GRE) &&
3344 (dev->type != ARPHRD_IPGRE) &&
3345 (dev->type != ARPHRD_TUNNEL) &&
3346 (dev->type != ARPHRD_NONE) &&
3347 (dev->type != ARPHRD_RAWIP)) {
3348 /* Alas, we support only Ethernet autoconfiguration. */
3352 idev = addrconf_add_dev(dev);
3356 /* this device type has no EUI support */
3357 if (dev->type == ARPHRD_NONE &&
3358 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3359 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3361 addrconf_addr_gen(idev, false);
3364 #if IS_ENABLED(CONFIG_IPV6_SIT)
3365 static void addrconf_sit_config(struct net_device *dev)
3367 struct inet6_dev *idev;
3372 * Configure the tunnel with one of our IPv4
3373 * addresses... we should configure all of
3374 * our v4 addrs in the tunnel
3377 idev = ipv6_find_idev(dev);
3379 pr_debug("%s: add_dev failed\n", __func__);
3383 if (dev->priv_flags & IFF_ISATAP) {
3384 addrconf_addr_gen(idev, false);
3388 sit_add_v4_addrs(idev);
3390 if (dev->flags&IFF_POINTOPOINT)
3391 addrconf_add_mroute(dev);
3395 #if IS_ENABLED(CONFIG_NET_IPGRE)
3396 static void addrconf_gre_config(struct net_device *dev)
3398 struct inet6_dev *idev;
3402 idev = ipv6_find_idev(dev);
3404 pr_debug("%s: add_dev failed\n", __func__);
3408 addrconf_addr_gen(idev, true);
3409 if (dev->flags & IFF_POINTOPOINT)
3410 addrconf_add_mroute(dev);
3414 static int fixup_permanent_addr(struct net *net,
3415 struct inet6_dev *idev,
3416 struct inet6_ifaddr *ifp)
3418 /* !fib6_node means the host route was removed from the
3419 * FIB, for example, if 'lo' device is taken down. In that
3420 * case regenerate the host route.
3422 if (!ifp->rt || !ifp->rt->fib6_node) {
3423 struct fib6_info *f6i, *prev;
3425 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3428 return PTR_ERR(f6i);
3430 /* ifp->rt can be accessed outside of rtnl */
3431 spin_lock(&ifp->lock);
3434 spin_unlock(&ifp->lock);
3436 fib6_info_release(prev);
3439 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3440 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3441 ifp->rt_priority, idev->dev, 0, 0,
3445 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3446 addrconf_dad_start(ifp);
3451 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3453 struct inet6_ifaddr *ifp, *tmp;
3454 struct inet6_dev *idev;
3456 idev = __in6_dev_get(dev);
3460 write_lock_bh(&idev->lock);
3462 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3463 if ((ifp->flags & IFA_F_PERMANENT) &&
3464 fixup_permanent_addr(net, idev, ifp) < 0) {
3465 write_unlock_bh(&idev->lock);
3468 write_lock_bh(&idev->lock);
3470 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3471 idev->dev->name, &ifp->addr);
3475 write_unlock_bh(&idev->lock);
3478 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3481 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3482 struct netdev_notifier_change_info *change_info;
3483 struct netdev_notifier_changeupper_info *info;
3484 struct inet6_dev *idev = __in6_dev_get(dev);
3485 struct net *net = dev_net(dev);
3486 int run_pending = 0;
3490 case NETDEV_REGISTER:
3491 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3492 idev = ipv6_add_dev(dev);
3494 return notifier_from_errno(PTR_ERR(idev));
3498 case NETDEV_CHANGEMTU:
3499 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3500 if (dev->mtu < IPV6_MIN_MTU) {
3501 addrconf_ifdown(dev, dev != net->loopback_dev);
3506 rt6_mtu_change(dev, dev->mtu);
3507 idev->cnf.mtu6 = dev->mtu;
3511 /* allocate new idev */
3512 idev = ipv6_add_dev(dev);
3516 /* device is still not ready */
3517 if (!(idev->if_flags & IF_READY))
3526 if (dev->flags & IFF_SLAVE)
3529 if (idev && idev->cnf.disable_ipv6)
3532 if (event == NETDEV_UP) {
3533 /* restore routes for permanent addresses */
3534 addrconf_permanent_addr(net, dev);
3536 if (!addrconf_link_ready(dev)) {
3537 /* device is not ready yet. */
3538 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3543 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3544 idev = ipv6_add_dev(dev);
3546 if (!IS_ERR_OR_NULL(idev)) {
3547 idev->if_flags |= IF_READY;
3550 } else if (event == NETDEV_CHANGE) {
3551 if (!addrconf_link_ready(dev)) {
3552 /* device is still not ready. */
3553 rt6_sync_down_dev(dev, event);
3557 if (!IS_ERR_OR_NULL(idev)) {
3558 if (idev->if_flags & IF_READY) {
3559 /* device is already configured -
3560 * but resend MLD reports, we might
3561 * have roamed and need to update
3562 * multicast snooping switches
3566 if (change_info->flags_changed & IFF_NOARP)
3567 addrconf_dad_run(idev, true);
3568 rt6_sync_up(dev, RTNH_F_LINKDOWN);
3571 idev->if_flags |= IF_READY;
3574 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3580 switch (dev->type) {
3581 #if IS_ENABLED(CONFIG_IPV6_SIT)
3583 addrconf_sit_config(dev);
3586 #if IS_ENABLED(CONFIG_NET_IPGRE)
3588 addrconf_gre_config(dev);
3591 case ARPHRD_LOOPBACK:
3596 addrconf_dev_config(dev);
3600 if (!IS_ERR_OR_NULL(idev)) {
3602 addrconf_dad_run(idev, false);
3604 /* Device has an address by now */
3605 rt6_sync_up(dev, RTNH_F_DEAD);
3608 * If the MTU changed during the interface down,
3609 * when the interface up, the changed MTU must be
3610 * reflected in the idev as well as routers.
3612 if (idev->cnf.mtu6 != dev->mtu &&
3613 dev->mtu >= IPV6_MIN_MTU) {
3614 rt6_mtu_change(dev, dev->mtu);
3615 idev->cnf.mtu6 = dev->mtu;
3617 idev->tstamp = jiffies;
3618 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3621 * If the changed mtu during down is lower than
3622 * IPV6_MIN_MTU stop IPv6 on this interface.
3624 if (dev->mtu < IPV6_MIN_MTU)
3625 addrconf_ifdown(dev, dev != net->loopback_dev);
3630 case NETDEV_UNREGISTER:
3632 * Remove all addresses from this interface.
3634 addrconf_ifdown(dev, event != NETDEV_DOWN);
3637 case NETDEV_CHANGENAME:
3639 snmp6_unregister_dev(idev);
3640 addrconf_sysctl_unregister(idev);
3641 err = addrconf_sysctl_register(idev);
3643 return notifier_from_errno(err);
3644 err = snmp6_register_dev(idev);
3646 addrconf_sysctl_unregister(idev);
3647 return notifier_from_errno(err);
3652 case NETDEV_PRE_TYPE_CHANGE:
3653 case NETDEV_POST_TYPE_CHANGE:
3655 addrconf_type_change(dev, event);
3658 case NETDEV_CHANGEUPPER:
3661 /* flush all routes if dev is linked to or unlinked from
3662 * an L3 master device (e.g., VRF)
3664 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3665 addrconf_ifdown(dev, 0);
3672 * addrconf module should be notified of a device going up
3674 static struct notifier_block ipv6_dev_notf = {
3675 .notifier_call = addrconf_notify,
3676 .priority = ADDRCONF_NOTIFY_PRIORITY,
3679 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3681 struct inet6_dev *idev;
3684 idev = __in6_dev_get(dev);
3686 if (event == NETDEV_POST_TYPE_CHANGE)
3687 ipv6_mc_remap(idev);
3688 else if (event == NETDEV_PRE_TYPE_CHANGE)
3689 ipv6_mc_unmap(idev);
3692 static bool addr_is_local(const struct in6_addr *addr)
3694 return ipv6_addr_type(addr) &
3695 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3698 static int addrconf_ifdown(struct net_device *dev, int how)
3700 unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
3701 struct net *net = dev_net(dev);
3702 struct inet6_dev *idev;
3703 struct inet6_ifaddr *ifa, *tmp;
3704 bool keep_addr = false;
3709 rt6_disable_ip(dev, event);
3711 idev = __in6_dev_get(dev);
3716 * Step 1: remove reference to ipv6 device from parent device.
3722 /* protected by rtnl_lock */
3723 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3725 /* Step 1.5: remove snmp6 entry */
3726 snmp6_unregister_dev(idev);
3730 /* combine the user config with event to determine if permanent
3731 * addresses are to be removed from address hash table
3733 if (!how && !idev->cnf.disable_ipv6) {
3734 /* aggregate the system setting and interface setting */
3735 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3738 _keep_addr = idev->cnf.keep_addr_on_down;
3740 keep_addr = (_keep_addr > 0);
3743 /* Step 2: clear hash table */
3744 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3745 struct hlist_head *h = &inet6_addr_lst[i];
3747 spin_lock_bh(&addrconf_hash_lock);
3749 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3750 if (ifa->idev == idev) {
3751 addrconf_del_dad_work(ifa);
3752 /* combined flag + permanent flag decide if
3753 * address is retained on a down event
3756 !(ifa->flags & IFA_F_PERMANENT) ||
3757 addr_is_local(&ifa->addr)) {
3758 hlist_del_init_rcu(&ifa->addr_lst);
3763 spin_unlock_bh(&addrconf_hash_lock);
3766 write_lock_bh(&idev->lock);
3768 addrconf_del_rs_timer(idev);
3770 /* Step 2: clear flags for stateless addrconf */
3772 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3774 /* Step 3: clear tempaddr list */
3775 while (!list_empty(&idev->tempaddr_list)) {
3776 ifa = list_first_entry(&idev->tempaddr_list,
3777 struct inet6_ifaddr, tmp_list);
3778 list_del(&ifa->tmp_list);
3779 write_unlock_bh(&idev->lock);
3780 spin_lock_bh(&ifa->lock);
3783 in6_ifa_put(ifa->ifpub);
3786 spin_unlock_bh(&ifa->lock);
3788 write_lock_bh(&idev->lock);
3791 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3792 struct fib6_info *rt = NULL;
3795 addrconf_del_dad_work(ifa);
3797 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3798 !addr_is_local(&ifa->addr);
3800 write_unlock_bh(&idev->lock);
3801 spin_lock_bh(&ifa->lock);
3804 /* set state to skip the notifier below */
3805 state = INET6_IFADDR_STATE_DEAD;
3806 ifa->state = INET6_IFADDR_STATE_PREDAD;
3807 if (!(ifa->flags & IFA_F_NODAD))
3808 ifa->flags |= IFA_F_TENTATIVE;
3814 ifa->state = INET6_IFADDR_STATE_DEAD;
3817 spin_unlock_bh(&ifa->lock);
3820 ip6_del_rt(net, rt);
3822 if (state != INET6_IFADDR_STATE_DEAD) {
3823 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3824 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3826 if (idev->cnf.forwarding)
3827 addrconf_leave_anycast(ifa);
3828 addrconf_leave_solict(ifa->idev, &ifa->addr);
3831 write_lock_bh(&idev->lock);
3833 list_del_rcu(&ifa->if_list);
3838 write_unlock_bh(&idev->lock);
3840 /* Step 5: Discard anycast and multicast list */
3842 ipv6_ac_destroy_dev(idev);
3843 ipv6_mc_destroy_dev(idev);
3848 idev->tstamp = jiffies;
3850 /* Last: Shot the device (if unregistered) */
3852 addrconf_sysctl_unregister(idev);
3853 neigh_parms_release(&nd_tbl, idev->nd_parms);
3854 neigh_ifdown(&nd_tbl, dev);
3860 static void addrconf_rs_timer(struct timer_list *t)
3862 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3863 struct net_device *dev = idev->dev;
3864 struct in6_addr lladdr;
3866 write_lock(&idev->lock);
3867 if (idev->dead || !(idev->if_flags & IF_READY))
3870 if (!ipv6_accept_ra(idev))
3873 /* Announcement received after solicitation was sent */
3874 if (idev->if_flags & IF_RA_RCVD)
3877 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3878 write_unlock(&idev->lock);
3879 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3880 ndisc_send_rs(dev, &lladdr,
3881 &in6addr_linklocal_allrouters);
3885 write_lock(&idev->lock);
3886 idev->rs_interval = rfc3315_s14_backoff_update(
3887 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3888 /* The wait after the last probe can be shorter */
3889 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3890 idev->cnf.rtr_solicits) ?
3891 idev->cnf.rtr_solicit_delay :
3895 * Note: we do not support deprecated "all on-link"
3896 * assumption any longer.
3898 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3902 write_unlock(&idev->lock);
3908 * Duplicate Address Detection
3910 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3912 unsigned long rand_num;
3913 struct inet6_dev *idev = ifp->idev;
3916 if (ifp->flags & IFA_F_OPTIMISTIC)
3919 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3922 if (idev->cnf.enhanced_dad ||
3923 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3925 get_random_bytes(&nonce, 6);
3928 ifp->dad_nonce = nonce;
3929 ifp->dad_probes = idev->cnf.dad_transmits;
3930 addrconf_mod_dad_work(ifp, rand_num);
3933 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3935 struct inet6_dev *idev = ifp->idev;
3936 struct net_device *dev = idev->dev;
3937 bool bump_id, notify = false;
3940 addrconf_join_solict(dev, &ifp->addr);
3942 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3944 read_lock_bh(&idev->lock);
3945 spin_lock(&ifp->lock);
3946 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3950 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3951 (net->ipv6.devconf_all->accept_dad < 1 &&
3952 idev->cnf.accept_dad < 1) ||
3953 !(ifp->flags&IFA_F_TENTATIVE) ||
3954 ifp->flags & IFA_F_NODAD) {
3955 bool send_na = false;
3957 if (ifp->flags & IFA_F_TENTATIVE &&
3958 !(ifp->flags & IFA_F_OPTIMISTIC))
3960 bump_id = ifp->flags & IFA_F_TENTATIVE;
3961 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3962 spin_unlock(&ifp->lock);
3963 read_unlock_bh(&idev->lock);
3965 addrconf_dad_completed(ifp, bump_id, send_na);
3969 if (!(idev->if_flags & IF_READY)) {
3970 spin_unlock(&ifp->lock);
3971 read_unlock_bh(&idev->lock);
3973 * If the device is not ready:
3974 * - keep it tentative if it is a permanent address.
3975 * - otherwise, kill it.
3978 addrconf_dad_stop(ifp, 0);
3983 * Optimistic nodes can start receiving
3986 if (ifp->flags & IFA_F_OPTIMISTIC) {
3987 ip6_ins_rt(net, ifp->rt);
3988 if (ipv6_use_optimistic_addr(net, idev)) {
3989 /* Because optimistic nodes can use this address,
3990 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3996 addrconf_dad_kick(ifp);
3998 spin_unlock(&ifp->lock);
3999 read_unlock_bh(&idev->lock);
4001 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4004 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4006 bool begin_dad = false;
4008 spin_lock_bh(&ifp->lock);
4009 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4010 ifp->state = INET6_IFADDR_STATE_PREDAD;
4013 spin_unlock_bh(&ifp->lock);
4016 addrconf_mod_dad_work(ifp, 0);
4019 static void addrconf_dad_work(struct work_struct *w)
4021 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4022 struct inet6_ifaddr,
4024 struct inet6_dev *idev = ifp->idev;
4025 bool bump_id, disable_ipv6 = false;
4026 struct in6_addr mcaddr;
4032 } action = DAD_PROCESS;
4036 spin_lock_bh(&ifp->lock);
4037 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4039 ifp->state = INET6_IFADDR_STATE_DAD;
4040 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4042 ifp->state = INET6_IFADDR_STATE_POSTDAD;
4044 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4045 idev->cnf.accept_dad > 1) &&
4046 !idev->cnf.disable_ipv6 &&
4047 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4048 struct in6_addr addr;
4050 addr.s6_addr32[0] = htonl(0xfe800000);
4051 addr.s6_addr32[1] = 0;
4053 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4054 ipv6_addr_equal(&ifp->addr, &addr)) {
4055 /* DAD failed for link-local based on MAC */
4056 idev->cnf.disable_ipv6 = 1;
4058 pr_info("%s: IPv6 being disabled!\n",
4059 ifp->idev->dev->name);
4060 disable_ipv6 = true;
4064 spin_unlock_bh(&ifp->lock);
4066 if (action == DAD_BEGIN) {
4067 addrconf_dad_begin(ifp);
4069 } else if (action == DAD_ABORT) {
4071 addrconf_dad_stop(ifp, 1);
4073 addrconf_ifdown(idev->dev, 0);
4077 if (!ifp->dad_probes && addrconf_dad_end(ifp))
4080 write_lock_bh(&idev->lock);
4081 if (idev->dead || !(idev->if_flags & IF_READY)) {
4082 write_unlock_bh(&idev->lock);
4086 spin_lock(&ifp->lock);
4087 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4088 spin_unlock(&ifp->lock);
4089 write_unlock_bh(&idev->lock);
4093 if (ifp->dad_probes == 0) {
4094 bool send_na = false;
4097 * DAD was successful
4100 if (ifp->flags & IFA_F_TENTATIVE &&
4101 !(ifp->flags & IFA_F_OPTIMISTIC))
4103 bump_id = ifp->flags & IFA_F_TENTATIVE;
4104 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4105 spin_unlock(&ifp->lock);
4106 write_unlock_bh(&idev->lock);
4108 addrconf_dad_completed(ifp, bump_id, send_na);
4114 addrconf_mod_dad_work(ifp,
4115 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
4116 spin_unlock(&ifp->lock);
4117 write_unlock_bh(&idev->lock);
4119 /* send a neighbour solicitation for our addr */
4120 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4121 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4128 /* ifp->idev must be at least read locked */
4129 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4131 struct inet6_ifaddr *ifpiter;
4132 struct inet6_dev *idev = ifp->idev;
4134 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4135 if (ifpiter->scope > IFA_LINK)
4137 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4138 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4139 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4146 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4149 struct net_device *dev = ifp->idev->dev;
4150 struct in6_addr lladdr;
4151 bool send_rs, send_mld;
4153 addrconf_del_dad_work(ifp);
4156 * Configure the address for reception. Now it is valid.
4159 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4161 /* If added prefix is link local and we are prepared to process
4162 router advertisements, start sending router solicitations.
4165 read_lock_bh(&ifp->idev->lock);
4166 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4167 send_rs = send_mld &&
4168 ipv6_accept_ra(ifp->idev) &&
4169 ifp->idev->cnf.rtr_solicits != 0 &&
4170 (dev->flags&IFF_LOOPBACK) == 0;
4171 read_unlock_bh(&ifp->idev->lock);
4173 /* While dad is in progress mld report's source address is in6_addrany.
4174 * Resend with proper ll now.
4177 ipv6_mc_dad_complete(ifp->idev);
4179 /* send unsolicited NA if enabled */
4181 (ifp->idev->cnf.ndisc_notify ||
4182 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4183 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4184 /*router=*/ !!ifp->idev->cnf.forwarding,
4185 /*solicited=*/ false, /*override=*/ true,
4191 * If a host as already performed a random delay
4192 * [...] as part of DAD [...] there is no need
4193 * to delay again before sending the first RS
4195 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4197 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4199 write_lock_bh(&ifp->idev->lock);
4200 spin_lock(&ifp->lock);
4201 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4202 ifp->idev->cnf.rtr_solicit_interval);
4203 ifp->idev->rs_probes = 1;
4204 ifp->idev->if_flags |= IF_RS_SENT;
4205 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4206 spin_unlock(&ifp->lock);
4207 write_unlock_bh(&ifp->idev->lock);
4211 rt_genid_bump_ipv6(dev_net(dev));
4213 /* Make sure that a new temporary address will be created
4214 * before this temporary address becomes deprecated.
4216 if (ifp->flags & IFA_F_TEMPORARY)
4217 addrconf_verify_rtnl();
4220 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4222 struct inet6_ifaddr *ifp;
4224 read_lock_bh(&idev->lock);
4225 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4226 spin_lock(&ifp->lock);
4227 if ((ifp->flags & IFA_F_TENTATIVE &&
4228 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4230 ifp->state = INET6_IFADDR_STATE_PREDAD;
4231 addrconf_dad_kick(ifp);
4233 spin_unlock(&ifp->lock);
4235 read_unlock_bh(&idev->lock);
4238 #ifdef CONFIG_PROC_FS
4239 struct if6_iter_state {
4240 struct seq_net_private p;
4245 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4247 struct if6_iter_state *state = seq->private;
4248 struct net *net = seq_file_net(seq);
4249 struct inet6_ifaddr *ifa = NULL;
4252 /* initial bucket if pos is 0 */
4258 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4259 hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
4261 if (!net_eq(dev_net(ifa->idev->dev), net))
4263 /* sync with offset */
4264 if (p < state->offset) {
4271 /* prepare for next bucket */
4278 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4279 struct inet6_ifaddr *ifa)
4281 struct if6_iter_state *state = seq->private;
4282 struct net *net = seq_file_net(seq);
4284 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4285 if (!net_eq(dev_net(ifa->idev->dev), net))
4292 while (++state->bucket < IN6_ADDR_HSIZE) {
4293 hlist_for_each_entry_rcu(ifa,
4294 &inet6_addr_lst[state->bucket], addr_lst) {
4295 if (!net_eq(dev_net(ifa->idev->dev), net))
4304 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4308 return if6_get_first(seq, *pos);
4311 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4313 struct inet6_ifaddr *ifa;
4315 ifa = if6_get_next(seq, v);
4320 static void if6_seq_stop(struct seq_file *seq, void *v)
4326 static int if6_seq_show(struct seq_file *seq, void *v)
4328 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4329 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4331 ifp->idev->dev->ifindex,
4335 ifp->idev->dev->name);
4339 static const struct seq_operations if6_seq_ops = {
4340 .start = if6_seq_start,
4341 .next = if6_seq_next,
4342 .show = if6_seq_show,
4343 .stop = if6_seq_stop,
4346 static int __net_init if6_proc_net_init(struct net *net)
4348 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4349 sizeof(struct if6_iter_state)))
4354 static void __net_exit if6_proc_net_exit(struct net *net)
4356 remove_proc_entry("if_inet6", net->proc_net);
4359 static struct pernet_operations if6_proc_net_ops = {
4360 .init = if6_proc_net_init,
4361 .exit = if6_proc_net_exit,
4364 int __init if6_proc_init(void)
4366 return register_pernet_subsys(&if6_proc_net_ops);
4369 void if6_proc_exit(void)
4371 unregister_pernet_subsys(&if6_proc_net_ops);
4373 #endif /* CONFIG_PROC_FS */
4375 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4376 /* Check if address is a home address configured on any interface. */
4377 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4379 unsigned int hash = inet6_addr_hash(net, addr);
4380 struct inet6_ifaddr *ifp = NULL;
4384 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4385 if (!net_eq(dev_net(ifp->idev->dev), net))
4387 if (ipv6_addr_equal(&ifp->addr, addr) &&
4388 (ifp->flags & IFA_F_HOMEADDRESS)) {
4399 * Periodic address status verification
4402 static void addrconf_verify_rtnl(void)
4404 unsigned long now, next, next_sec, next_sched;
4405 struct inet6_ifaddr *ifp;
4412 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4414 cancel_delayed_work(&addr_chk_work);
4416 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4418 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4421 /* When setting preferred_lft to a value not zero or
4422 * infinity, while valid_lft is infinity
4423 * IFA_F_PERMANENT has a non-infinity life time.
4425 if ((ifp->flags & IFA_F_PERMANENT) &&
4426 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4429 spin_lock(&ifp->lock);
4430 /* We try to batch several events at once. */
4431 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4433 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4434 age >= ifp->valid_lft) {
4435 spin_unlock(&ifp->lock);
4439 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4440 spin_unlock(&ifp->lock);
4442 } else if (age >= ifp->prefered_lft) {
4443 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4446 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4448 ifp->flags |= IFA_F_DEPRECATED;
4451 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4452 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4453 next = ifp->tstamp + ifp->valid_lft * HZ;
4455 spin_unlock(&ifp->lock);
4460 ipv6_ifa_notify(0, ifp);
4464 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4465 !(ifp->flags&IFA_F_TENTATIVE)) {
4466 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4467 ifp->idev->cnf.dad_transmits *
4468 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4470 if (age >= ifp->prefered_lft - regen_advance) {
4471 struct inet6_ifaddr *ifpub = ifp->ifpub;
4472 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4473 next = ifp->tstamp + ifp->prefered_lft * HZ;
4474 if (!ifp->regen_count && ifpub) {
4477 in6_ifa_hold(ifpub);
4478 spin_unlock(&ifp->lock);
4480 spin_lock(&ifpub->lock);
4481 ifpub->regen_count = 0;
4482 spin_unlock(&ifpub->lock);
4483 rcu_read_unlock_bh();
4484 ipv6_create_tempaddr(ifpub, ifp, true);
4490 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4491 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4492 spin_unlock(&ifp->lock);
4494 /* ifp->prefered_lft <= ifp->valid_lft */
4495 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4496 next = ifp->tstamp + ifp->prefered_lft * HZ;
4497 spin_unlock(&ifp->lock);
4502 next_sec = round_jiffies_up(next);
4505 /* If rounded timeout is accurate enough, accept it. */
4506 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4507 next_sched = next_sec;
4509 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4510 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4511 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4513 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4514 now, next, next_sec, next_sched);
4515 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4516 rcu_read_unlock_bh();
4519 static void addrconf_verify_work(struct work_struct *w)
4522 addrconf_verify_rtnl();
4526 static void addrconf_verify(void)
4528 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4531 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4532 struct in6_addr **peer_pfx)
4534 struct in6_addr *pfx = NULL;
4539 pfx = nla_data(addr);
4542 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4544 pfx = nla_data(local);
4550 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4551 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4552 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4553 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4554 [IFA_FLAGS] = { .len = sizeof(u32) },
4555 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
4556 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
4560 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4561 struct netlink_ext_ack *extack)
4563 struct net *net = sock_net(skb->sk);
4564 struct ifaddrmsg *ifm;
4565 struct nlattr *tb[IFA_MAX+1];
4566 struct in6_addr *pfx, *peer_pfx;
4570 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4571 ifa_ipv6_policy, extack);
4575 ifm = nlmsg_data(nlh);
4576 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4580 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4582 /* We ignore other flags so far. */
4583 ifa_flags &= IFA_F_MANAGETEMPADDR;
4585 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4586 ifm->ifa_prefixlen);
4589 static int modify_prefix_route(struct inet6_ifaddr *ifp,
4590 unsigned long expires, u32 flags)
4592 struct fib6_info *f6i;
4595 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
4596 ifp->idev->dev, 0, RTF_DEFAULT, true);
4600 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4601 if (f6i->fib6_metric != prio) {
4602 /* delete old one */
4603 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
4606 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4607 ifp->rt_priority, ifp->idev->dev,
4608 expires, flags, GFP_KERNEL);
4611 fib6_clean_expires(f6i);
4613 fib6_set_expires(f6i, expires);
4615 fib6_info_release(f6i);
4621 static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
4625 unsigned long timeout;
4626 bool was_managetempaddr;
4627 bool had_prefixroute;
4631 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4634 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4635 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4638 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4639 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4641 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
4642 if (addrconf_finite_timeout(timeout)) {
4643 expires = jiffies_to_clock_t(timeout * HZ);
4644 cfg->valid_lft = timeout;
4645 flags = RTF_EXPIRES;
4649 cfg->ifa_flags |= IFA_F_PERMANENT;
4652 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
4653 if (addrconf_finite_timeout(timeout)) {
4655 cfg->ifa_flags |= IFA_F_DEPRECATED;
4656 cfg->preferred_lft = timeout;
4659 spin_lock_bh(&ifp->lock);
4660 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4661 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4662 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4663 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4664 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4665 IFA_F_NOPREFIXROUTE);
4666 ifp->flags |= cfg->ifa_flags;
4667 ifp->tstamp = jiffies;
4668 ifp->valid_lft = cfg->valid_lft;
4669 ifp->prefered_lft = cfg->preferred_lft;
4671 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4672 ifp->rt_priority = cfg->rt_priority;
4674 spin_unlock_bh(&ifp->lock);
4675 if (!(ifp->flags&IFA_F_TENTATIVE))
4676 ipv6_ifa_notify(0, ifp);
4678 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4681 if (had_prefixroute)
4682 rc = modify_prefix_route(ifp, expires, flags);
4684 /* prefix route could have been deleted; if so restore it */
4685 if (rc == -ENOENT) {
4686 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4687 ifp->rt_priority, ifp->idev->dev,
4688 expires, flags, GFP_KERNEL);
4690 } else if (had_prefixroute) {
4691 enum cleanup_prefix_rt_t action;
4692 unsigned long rt_expires;
4694 write_lock_bh(&ifp->idev->lock);
4695 action = check_cleanup_prefix_route(ifp, &rt_expires);
4696 write_unlock_bh(&ifp->idev->lock);
4698 if (action != CLEANUP_PREFIX_RT_NOP) {
4699 cleanup_prefix_route(ifp, rt_expires,
4700 action == CLEANUP_PREFIX_RT_DEL);
4704 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4705 if (was_managetempaddr &&
4706 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4708 cfg->preferred_lft = 0;
4710 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4711 cfg->preferred_lft, !was_managetempaddr,
4715 addrconf_verify_rtnl();
4721 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4722 struct netlink_ext_ack *extack)
4724 struct net *net = sock_net(skb->sk);
4725 struct ifaddrmsg *ifm;
4726 struct nlattr *tb[IFA_MAX+1];
4727 struct in6_addr *peer_pfx;
4728 struct inet6_ifaddr *ifa;
4729 struct net_device *dev;
4730 struct inet6_dev *idev;
4731 struct ifa6_config cfg;
4734 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4735 ifa_ipv6_policy, extack);
4739 memset(&cfg, 0, sizeof(cfg));
4741 ifm = nlmsg_data(nlh);
4742 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4746 cfg.peer_pfx = peer_pfx;
4747 cfg.plen = ifm->ifa_prefixlen;
4748 if (tb[IFA_RT_PRIORITY])
4749 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4751 cfg.valid_lft = INFINITY_LIFE_TIME;
4752 cfg.preferred_lft = INFINITY_LIFE_TIME;
4754 if (tb[IFA_CACHEINFO]) {
4755 struct ifa_cacheinfo *ci;
4757 ci = nla_data(tb[IFA_CACHEINFO]);
4758 cfg.valid_lft = ci->ifa_valid;
4759 cfg.preferred_lft = ci->ifa_prefered;
4762 dev = __dev_get_by_index(net, ifm->ifa_index);
4767 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4769 cfg.ifa_flags = ifm->ifa_flags;
4771 /* We ignore other flags so far. */
4772 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4773 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4774 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4776 idev = ipv6_find_idev(dev);
4780 if (!ipv6_allow_optimistic_dad(net, idev))
4781 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4783 if (cfg.ifa_flags & IFA_F_NODAD &&
4784 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4785 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4789 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
4792 * It would be best to check for !NLM_F_CREATE here but
4793 * userspace already relies on not having to provide this.
4795 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
4798 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4799 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4802 err = inet6_addr_modify(ifa, &cfg);
4809 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4810 u8 scope, int ifindex)
4812 struct ifaddrmsg *ifm;
4814 ifm = nlmsg_data(nlh);
4815 ifm->ifa_family = AF_INET6;
4816 ifm->ifa_prefixlen = prefixlen;
4817 ifm->ifa_flags = flags;
4818 ifm->ifa_scope = scope;
4819 ifm->ifa_index = ifindex;
4822 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4823 unsigned long tstamp, u32 preferred, u32 valid)
4825 struct ifa_cacheinfo ci;
4827 ci.cstamp = cstamp_delta(cstamp);
4828 ci.tstamp = cstamp_delta(tstamp);
4829 ci.ifa_prefered = preferred;
4830 ci.ifa_valid = valid;
4832 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4835 static inline int rt_scope(int ifa_scope)
4837 if (ifa_scope & IFA_HOST)
4838 return RT_SCOPE_HOST;
4839 else if (ifa_scope & IFA_LINK)
4840 return RT_SCOPE_LINK;
4841 else if (ifa_scope & IFA_SITE)
4842 return RT_SCOPE_SITE;
4844 return RT_SCOPE_UNIVERSE;
4847 static inline int inet6_ifaddr_msgsize(void)
4849 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4850 + nla_total_size(16) /* IFA_LOCAL */
4851 + nla_total_size(16) /* IFA_ADDRESS */
4852 + nla_total_size(sizeof(struct ifa_cacheinfo))
4853 + nla_total_size(4) /* IFA_FLAGS */
4854 + nla_total_size(4) /* IFA_RT_PRIORITY */;
4863 struct inet6_fill_args {
4870 enum addr_type_t type;
4873 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4874 struct inet6_fill_args *args)
4876 struct nlmsghdr *nlh;
4877 u32 preferred, valid;
4879 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4880 sizeof(struct ifaddrmsg), args->flags);
4884 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4885 ifa->idev->dev->ifindex);
4887 if (args->netnsid >= 0 &&
4888 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
4891 if (!((ifa->flags&IFA_F_PERMANENT) &&
4892 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4893 preferred = ifa->prefered_lft;
4894 valid = ifa->valid_lft;
4895 if (preferred != INFINITY_LIFE_TIME) {
4896 long tval = (jiffies - ifa->tstamp)/HZ;
4897 if (preferred > tval)
4901 if (valid != INFINITY_LIFE_TIME) {
4909 preferred = INFINITY_LIFE_TIME;
4910 valid = INFINITY_LIFE_TIME;
4913 if (!ipv6_addr_any(&ifa->peer_addr)) {
4914 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4915 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4918 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4921 if (ifa->rt_priority &&
4922 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
4925 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4928 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4931 nlmsg_end(skb, nlh);
4935 nlmsg_cancel(skb, nlh);
4939 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4940 struct inet6_fill_args *args)
4942 struct nlmsghdr *nlh;
4943 u8 scope = RT_SCOPE_UNIVERSE;
4944 int ifindex = ifmca->idev->dev->ifindex;
4946 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4947 scope = RT_SCOPE_SITE;
4949 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4950 sizeof(struct ifaddrmsg), args->flags);
4954 if (args->netnsid >= 0 &&
4955 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
4958 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4959 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4960 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4961 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4962 nlmsg_cancel(skb, nlh);
4966 nlmsg_end(skb, nlh);
4970 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4971 struct inet6_fill_args *args)
4973 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
4974 int ifindex = dev ? dev->ifindex : 1;
4975 struct nlmsghdr *nlh;
4976 u8 scope = RT_SCOPE_UNIVERSE;
4978 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4979 scope = RT_SCOPE_SITE;
4981 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4982 sizeof(struct ifaddrmsg), args->flags);
4986 if (args->netnsid >= 0 &&
4987 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
4990 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4991 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4992 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4993 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4994 nlmsg_cancel(skb, nlh);
4998 nlmsg_end(skb, nlh);
5002 /* called with rcu_read_lock() */
5003 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5004 struct netlink_callback *cb, int s_ip_idx,
5005 struct inet6_fill_args *fillargs)
5007 struct ifmcaddr6 *ifmca;
5008 struct ifacaddr6 *ifaca;
5012 read_lock_bh(&idev->lock);
5013 switch (fillargs->type) {
5014 case UNICAST_ADDR: {
5015 struct inet6_ifaddr *ifa;
5016 fillargs->event = RTM_NEWADDR;
5018 /* unicast address incl. temp addr */
5019 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5020 if (ip_idx < s_ip_idx)
5022 err = inet6_fill_ifaddr(skb, ifa, fillargs);
5025 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5031 case MULTICAST_ADDR:
5032 fillargs->event = RTM_GETMULTICAST;
5034 /* multicast address */
5035 for (ifmca = idev->mc_list; ifmca;
5036 ifmca = ifmca->next, ip_idx++) {
5037 if (ip_idx < s_ip_idx)
5039 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5045 fillargs->event = RTM_GETANYCAST;
5046 /* anycast address */
5047 for (ifaca = idev->ac_list; ifaca;
5048 ifaca = ifaca->aca_next, ip_idx++) {
5049 if (ip_idx < s_ip_idx)
5051 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5059 read_unlock_bh(&idev->lock);
5060 cb->args[2] = ip_idx;
5064 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5065 struct inet6_fill_args *fillargs,
5066 struct net **tgt_net, struct sock *sk,
5067 struct netlink_callback *cb)
5069 struct netlink_ext_ack *extack = cb->extack;
5070 struct nlattr *tb[IFA_MAX+1];
5071 struct ifaddrmsg *ifm;
5074 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5075 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5079 ifm = nlmsg_data(nlh);
5080 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5081 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5085 fillargs->ifindex = ifm->ifa_index;
5086 if (fillargs->ifindex) {
5087 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5088 fillargs->flags |= NLM_F_DUMP_FILTERED;
5091 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5092 ifa_ipv6_policy, extack);
5096 for (i = 0; i <= IFA_MAX; ++i) {
5100 if (i == IFA_TARGET_NETNSID) {
5103 fillargs->netnsid = nla_get_s32(tb[i]);
5104 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5106 fillargs->netnsid = -1;
5107 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5108 return PTR_ERR(net);
5112 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5120 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5121 enum addr_type_t type)
5123 const struct nlmsghdr *nlh = cb->nlh;
5124 struct inet6_fill_args fillargs = {
5125 .portid = NETLINK_CB(cb->skb).portid,
5126 .seq = cb->nlh->nlmsg_seq,
5127 .flags = NLM_F_MULTI,
5131 struct net *net = sock_net(skb->sk);
5132 struct net *tgt_net = net;
5133 int idx, s_idx, s_ip_idx;
5135 struct net_device *dev;
5136 struct inet6_dev *idev;
5137 struct hlist_head *head;
5141 s_idx = idx = cb->args[1];
5142 s_ip_idx = cb->args[2];
5144 if (cb->strict_check) {
5145 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5151 if (fillargs.ifindex) {
5152 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5157 idev = __in6_dev_get(dev);
5159 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5169 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
5170 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5172 head = &tgt_net->dev_index_head[h];
5173 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5176 if (h > s_h || idx > s_idx)
5178 idev = __in6_dev_get(dev);
5182 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
5194 if (fillargs.netnsid >= 0)
5197 return skb->len ? : err;
5200 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5202 enum addr_type_t type = UNICAST_ADDR;
5204 return inet6_dump_addr(skb, cb, type);
5207 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5209 enum addr_type_t type = MULTICAST_ADDR;
5211 return inet6_dump_addr(skb, cb, type);
5215 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5217 enum addr_type_t type = ANYCAST_ADDR;
5219 return inet6_dump_addr(skb, cb, type);
5222 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5223 const struct nlmsghdr *nlh,
5225 struct netlink_ext_ack *extack)
5227 struct ifaddrmsg *ifm;
5230 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5231 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5235 ifm = nlmsg_data(nlh);
5236 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5237 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5241 if (!netlink_strict_get_check(skb))
5242 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5243 ifa_ipv6_policy, extack);
5245 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5246 ifa_ipv6_policy, extack);
5250 for (i = 0; i <= IFA_MAX; i++) {
5255 case IFA_TARGET_NETNSID:
5260 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5268 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5269 struct netlink_ext_ack *extack)
5271 struct net *net = sock_net(in_skb->sk);
5272 struct inet6_fill_args fillargs = {
5273 .portid = NETLINK_CB(in_skb).portid,
5274 .seq = nlh->nlmsg_seq,
5275 .event = RTM_NEWADDR,
5279 struct net *tgt_net = net;
5280 struct ifaddrmsg *ifm;
5281 struct nlattr *tb[IFA_MAX+1];
5282 struct in6_addr *addr = NULL, *peer;
5283 struct net_device *dev = NULL;
5284 struct inet6_ifaddr *ifa;
5285 struct sk_buff *skb;
5288 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5292 if (tb[IFA_TARGET_NETNSID]) {
5293 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5295 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5297 if (IS_ERR(tgt_net))
5298 return PTR_ERR(tgt_net);
5301 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5305 ifm = nlmsg_data(nlh);
5307 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5309 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5311 err = -EADDRNOTAVAIL;
5315 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5321 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5323 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5324 WARN_ON(err == -EMSGSIZE);
5328 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5334 if (fillargs.netnsid >= 0)
5340 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5342 struct sk_buff *skb;
5343 struct net *net = dev_net(ifa->idev->dev);
5344 struct inet6_fill_args fillargs = {
5353 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5357 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5359 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5360 WARN_ON(err == -EMSGSIZE);
5364 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5368 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5371 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
5372 __s32 *array, int bytes)
5374 BUG_ON(bytes < (DEVCONF_MAX * 4));
5376 memset(array, 0, bytes);
5377 array[DEVCONF_FORWARDING] = cnf->forwarding;
5378 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5379 array[DEVCONF_MTU6] = cnf->mtu6;
5380 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5381 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5382 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5383 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5384 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
5385 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5386 jiffies_to_msecs(cnf->rtr_solicit_interval);
5387 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5388 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
5389 array[DEVCONF_RTR_SOLICIT_DELAY] =
5390 jiffies_to_msecs(cnf->rtr_solicit_delay);
5391 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5392 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5393 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5394 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5395 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
5396 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5397 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5398 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5399 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5400 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
5401 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
5402 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
5403 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5404 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5405 #ifdef CONFIG_IPV6_ROUTER_PREF
5406 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
5407 array[DEVCONF_RTR_PROBE_INTERVAL] =
5408 jiffies_to_msecs(cnf->rtr_probe_interval);
5409 #ifdef CONFIG_IPV6_ROUTE_INFO
5410 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
5411 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5414 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
5415 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
5416 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5417 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
5418 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
5420 #ifdef CONFIG_IPV6_MROUTE
5421 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5423 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
5424 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5425 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
5426 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5427 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5428 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5429 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
5430 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
5431 /* we omit DEVCONF_STABLE_SECRET for now */
5432 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5433 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
5434 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5435 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5436 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5437 #ifdef CONFIG_IPV6_SEG6_HMAC
5438 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5440 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5441 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5442 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
5443 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
5446 static inline size_t inet6_ifla6_size(void)
5448 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5449 + nla_total_size(sizeof(struct ifla_cacheinfo))
5450 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5451 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5452 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5453 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5454 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5458 static inline size_t inet6_if_nlmsg_size(void)
5460 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5461 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5462 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5463 + nla_total_size(4) /* IFLA_MTU */
5464 + nla_total_size(4) /* IFLA_LINK */
5465 + nla_total_size(1) /* IFLA_OPERSTATE */
5466 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5469 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5473 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5476 /* Use put_unaligned() because stats may not be aligned for u64. */
5477 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5478 for (i = 1; i < ICMP6_MIB_MAX; i++)
5479 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5481 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5484 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5485 int bytes, size_t syncpoff)
5488 u64 buff[IPSTATS_MIB_MAX];
5489 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5493 memset(buff, 0, sizeof(buff));
5494 buff[0] = IPSTATS_MIB_MAX;
5496 for_each_possible_cpu(c) {
5497 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5498 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5501 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5502 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5505 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5509 case IFLA_INET6_STATS:
5510 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5511 offsetof(struct ipstats_mib, syncp));
5513 case IFLA_INET6_ICMP6STATS:
5514 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5519 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5520 u32 ext_filter_mask)
5523 struct ifla_cacheinfo ci;
5525 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5526 goto nla_put_failure;
5527 ci.max_reasm_len = IPV6_MAXPLEN;
5528 ci.tstamp = cstamp_delta(idev->tstamp);
5529 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5530 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5531 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5532 goto nla_put_failure;
5533 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5535 goto nla_put_failure;
5536 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5538 /* XXX - MC not implemented */
5540 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5543 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5545 goto nla_put_failure;
5546 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5548 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5550 goto nla_put_failure;
5551 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5553 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5555 goto nla_put_failure;
5557 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5558 goto nla_put_failure;
5560 read_lock_bh(&idev->lock);
5561 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5562 read_unlock_bh(&idev->lock);
5570 static size_t inet6_get_link_af_size(const struct net_device *dev,
5571 u32 ext_filter_mask)
5573 if (!__in6_dev_get(dev))
5576 return inet6_ifla6_size();
5579 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5580 u32 ext_filter_mask)
5582 struct inet6_dev *idev = __in6_dev_get(dev);
5587 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5593 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5595 struct inet6_ifaddr *ifp;
5596 struct net_device *dev = idev->dev;
5597 bool clear_token, update_rs = false;
5598 struct in6_addr ll_addr;
5604 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5606 if (!ipv6_accept_ra(idev))
5608 if (idev->cnf.rtr_solicits == 0)
5611 write_lock_bh(&idev->lock);
5613 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5614 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5616 write_unlock_bh(&idev->lock);
5618 clear_token = ipv6_addr_any(token);
5622 if (!idev->dead && (idev->if_flags & IF_READY) &&
5623 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5624 IFA_F_OPTIMISTIC)) {
5625 /* If we're not ready, then normal ifup will take care
5626 * of this. Otherwise, we need to request our rs here.
5628 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5633 write_lock_bh(&idev->lock);
5636 idev->if_flags |= IF_RS_SENT;
5637 idev->rs_interval = rfc3315_s14_backoff_init(
5638 idev->cnf.rtr_solicit_interval);
5639 idev->rs_probes = 1;
5640 addrconf_mod_rs_timer(idev, idev->rs_interval);
5643 /* Well, that's kinda nasty ... */
5644 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5645 spin_lock(&ifp->lock);
5646 if (ifp->tokenized) {
5648 ifp->prefered_lft = 0;
5650 spin_unlock(&ifp->lock);
5653 write_unlock_bh(&idev->lock);
5654 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5655 addrconf_verify_rtnl();
5659 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5660 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5661 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5664 static int inet6_validate_link_af(const struct net_device *dev,
5665 const struct nlattr *nla)
5667 struct nlattr *tb[IFLA_INET6_MAX + 1];
5669 if (dev && !__in6_dev_get(dev))
5670 return -EAFNOSUPPORT;
5672 return nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5673 inet6_af_policy, NULL);
5676 static int check_addr_gen_mode(int mode)
5678 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5679 mode != IN6_ADDR_GEN_MODE_NONE &&
5680 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5681 mode != IN6_ADDR_GEN_MODE_RANDOM)
5686 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5689 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5690 !idev->cnf.stable_secret.initialized &&
5691 !net->ipv6.devconf_dflt->stable_secret.initialized)
5696 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5699 struct inet6_dev *idev = __in6_dev_get(dev);
5700 struct nlattr *tb[IFLA_INET6_MAX + 1];
5703 return -EAFNOSUPPORT;
5705 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5708 if (tb[IFLA_INET6_TOKEN]) {
5709 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5714 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5715 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5717 if (check_addr_gen_mode(mode) < 0 ||
5718 check_stable_privacy(idev, dev_net(dev), mode) < 0)
5721 idev->cnf.addr_gen_mode = mode;
5728 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5729 u32 portid, u32 seq, int event, unsigned int flags)
5731 struct net_device *dev = idev->dev;
5732 struct ifinfomsg *hdr;
5733 struct nlmsghdr *nlh;
5736 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5740 hdr = nlmsg_data(nlh);
5741 hdr->ifi_family = AF_INET6;
5743 hdr->ifi_type = dev->type;
5744 hdr->ifi_index = dev->ifindex;
5745 hdr->ifi_flags = dev_get_flags(dev);
5746 hdr->ifi_change = 0;
5748 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5750 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5751 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5752 (dev->ifindex != dev_get_iflink(dev) &&
5753 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5754 nla_put_u8(skb, IFLA_OPERSTATE,
5755 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5756 goto nla_put_failure;
5757 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
5759 goto nla_put_failure;
5761 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5762 goto nla_put_failure;
5764 nla_nest_end(skb, protoinfo);
5765 nlmsg_end(skb, nlh);
5769 nlmsg_cancel(skb, nlh);
5773 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5774 struct netlink_ext_ack *extack)
5776 struct ifinfomsg *ifm;
5778 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5779 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5783 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5784 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5788 ifm = nlmsg_data(nlh);
5789 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5790 ifm->ifi_change || ifm->ifi_index) {
5791 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5798 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5800 struct net *net = sock_net(skb->sk);
5803 struct net_device *dev;
5804 struct inet6_dev *idev;
5805 struct hlist_head *head;
5807 /* only requests using strict checking can pass data to
5808 * influence the dump
5810 if (cb->strict_check) {
5811 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5818 s_idx = cb->args[1];
5821 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5823 head = &net->dev_index_head[h];
5824 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5827 idev = __in6_dev_get(dev);
5830 if (inet6_fill_ifinfo(skb, idev,
5831 NETLINK_CB(cb->skb).portid,
5833 RTM_NEWLINK, NLM_F_MULTI) < 0)
5847 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5849 struct sk_buff *skb;
5850 struct net *net = dev_net(idev->dev);
5853 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5857 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5859 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5860 WARN_ON(err == -EMSGSIZE);
5864 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5868 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5871 static inline size_t inet6_prefix_nlmsg_size(void)
5873 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5874 + nla_total_size(sizeof(struct in6_addr))
5875 + nla_total_size(sizeof(struct prefix_cacheinfo));
5878 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5879 struct prefix_info *pinfo, u32 portid, u32 seq,
5880 int event, unsigned int flags)
5882 struct prefixmsg *pmsg;
5883 struct nlmsghdr *nlh;
5884 struct prefix_cacheinfo ci;
5886 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5890 pmsg = nlmsg_data(nlh);
5891 pmsg->prefix_family = AF_INET6;
5892 pmsg->prefix_pad1 = 0;
5893 pmsg->prefix_pad2 = 0;
5894 pmsg->prefix_ifindex = idev->dev->ifindex;
5895 pmsg->prefix_len = pinfo->prefix_len;
5896 pmsg->prefix_type = pinfo->type;
5897 pmsg->prefix_pad3 = 0;
5898 pmsg->prefix_flags = 0;
5900 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5901 if (pinfo->autoconf)
5902 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5904 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5905 goto nla_put_failure;
5906 ci.preferred_time = ntohl(pinfo->prefered);
5907 ci.valid_time = ntohl(pinfo->valid);
5908 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5909 goto nla_put_failure;
5910 nlmsg_end(skb, nlh);
5914 nlmsg_cancel(skb, nlh);
5918 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5919 struct prefix_info *pinfo)
5921 struct sk_buff *skb;
5922 struct net *net = dev_net(idev->dev);
5925 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5929 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5931 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5932 WARN_ON(err == -EMSGSIZE);
5936 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5940 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5943 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5945 struct net *net = dev_net(ifp->idev->dev);
5950 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5955 * If the address was optimistic
5956 * we inserted the route at the start of
5957 * our DAD process, so we don't need
5960 if (!rcu_access_pointer(ifp->rt->fib6_node))
5961 ip6_ins_rt(net, ifp->rt);
5962 if (ifp->idev->cnf.forwarding)
5963 addrconf_join_anycast(ifp);
5964 if (!ipv6_addr_any(&ifp->peer_addr))
5965 addrconf_prefix_route(&ifp->peer_addr, 128, 0,
5966 ifp->idev->dev, 0, 0,
5970 if (ifp->idev->cnf.forwarding)
5971 addrconf_leave_anycast(ifp);
5972 addrconf_leave_solict(ifp->idev, &ifp->addr);
5973 if (!ipv6_addr_any(&ifp->peer_addr)) {
5974 struct fib6_info *rt;
5976 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5977 ifp->idev->dev, 0, 0,
5980 ip6_del_rt(net, rt);
5983 ip6_del_rt(net, ifp->rt);
5986 rt_genid_bump_ipv6(net);
5989 atomic_inc(&net->ipv6.dev_addr_genid);
5992 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5995 if (likely(ifp->idev->dead == 0))
5996 __ipv6_ifa_notify(event, ifp);
5997 rcu_read_unlock_bh();
6000 #ifdef CONFIG_SYSCTL
6003 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
6004 void __user *buffer, size_t *lenp, loff_t *ppos)
6006 int *valp = ctl->data;
6009 struct ctl_table lctl;
6013 * ctl->data points to idev->cnf.forwarding, we should
6014 * not modify it until we get the rtnl lock.
6019 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6022 ret = addrconf_fixup_forwarding(ctl, valp, val);
6029 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6030 void __user *buffer, size_t *lenp, loff_t *ppos)
6032 struct inet6_dev *idev = ctl->extra1;
6033 int min_mtu = IPV6_MIN_MTU;
6034 struct ctl_table lctl;
6037 lctl.extra1 = &min_mtu;
6038 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6040 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6043 static void dev_disable_change(struct inet6_dev *idev)
6045 struct netdev_notifier_info info;
6047 if (!idev || !idev->dev)
6050 netdev_notifier_info_init(&info, idev->dev);
6051 if (idev->cnf.disable_ipv6)
6052 addrconf_notify(NULL, NETDEV_DOWN, &info);
6054 addrconf_notify(NULL, NETDEV_UP, &info);
6057 static void addrconf_disable_change(struct net *net, __s32 newf)
6059 struct net_device *dev;
6060 struct inet6_dev *idev;
6062 for_each_netdev(net, dev) {
6063 idev = __in6_dev_get(dev);
6065 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6066 idev->cnf.disable_ipv6 = newf;
6068 dev_disable_change(idev);
6073 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
6078 if (!rtnl_trylock())
6079 return restart_syscall();
6081 net = (struct net *)table->extra2;
6085 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6090 if (p == &net->ipv6.devconf_all->disable_ipv6) {
6091 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6092 addrconf_disable_change(net, newf);
6093 } else if ((!newf) ^ (!old))
6094 dev_disable_change((struct inet6_dev *)table->extra1);
6101 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
6102 void __user *buffer, size_t *lenp, loff_t *ppos)
6104 int *valp = ctl->data;
6107 struct ctl_table lctl;
6111 * ctl->data points to idev->cnf.disable_ipv6, we should
6112 * not modify it until we get the rtnl lock.
6117 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6120 ret = addrconf_disable_ipv6(ctl, valp, val);
6127 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6128 void __user *buffer, size_t *lenp, loff_t *ppos)
6130 int *valp = ctl->data;
6135 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6138 if (write && old != new) {
6139 struct net *net = ctl->extra2;
6141 if (!rtnl_trylock())
6142 return restart_syscall();
6144 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
6145 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6146 NETCONFA_PROXY_NEIGH,
6147 NETCONFA_IFINDEX_DEFAULT,
6148 net->ipv6.devconf_dflt);
6149 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
6150 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6151 NETCONFA_PROXY_NEIGH,
6152 NETCONFA_IFINDEX_ALL,
6153 net->ipv6.devconf_all);
6155 struct inet6_dev *idev = ctl->extra1;
6157 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6158 NETCONFA_PROXY_NEIGH,
6168 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6169 void __user *buffer, size_t *lenp,
6174 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6175 struct net *net = (struct net *)ctl->extra2;
6176 struct ctl_table tmp = {
6178 .maxlen = sizeof(new_val),
6182 if (!rtnl_trylock())
6183 return restart_syscall();
6185 new_val = *((u32 *)ctl->data);
6187 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6192 if (check_addr_gen_mode(new_val) < 0) {
6198 if (check_stable_privacy(idev, net, new_val) < 0) {
6203 if (idev->cnf.addr_gen_mode != new_val) {
6204 idev->cnf.addr_gen_mode = new_val;
6205 addrconf_dev_config(idev->dev);
6207 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6208 struct net_device *dev;
6210 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6211 for_each_netdev(net, dev) {
6212 idev = __in6_dev_get(dev);
6214 idev->cnf.addr_gen_mode != new_val) {
6215 idev->cnf.addr_gen_mode = new_val;
6216 addrconf_dev_config(idev->dev);
6221 *((u32 *)ctl->data) = new_val;
6230 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6231 void __user *buffer, size_t *lenp,
6235 struct in6_addr addr;
6236 char str[IPV6_MAX_STRLEN];
6237 struct ctl_table lctl = *ctl;
6238 struct net *net = ctl->extra2;
6239 struct ipv6_stable_secret *secret = ctl->data;
6241 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6244 lctl.maxlen = IPV6_MAX_STRLEN;
6247 if (!rtnl_trylock())
6248 return restart_syscall();
6250 if (!write && !secret->initialized) {
6255 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6256 if (err >= sizeof(str)) {
6261 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6265 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6270 secret->initialized = true;
6271 secret->secret = addr;
6273 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6274 struct net_device *dev;
6276 for_each_netdev(net, dev) {
6277 struct inet6_dev *idev = __in6_dev_get(dev);
6280 idev->cnf.addr_gen_mode =
6281 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6285 struct inet6_dev *idev = ctl->extra1;
6287 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6297 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6299 void __user *buffer,
6303 int *valp = ctl->data;
6306 struct ctl_table lctl;
6309 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6310 * we should not modify it until we get the rtnl lock.
6315 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6318 ret = addrconf_fixup_linkdown(ctl, valp, val);
6325 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6329 rt->dst.flags |= DST_NOPOLICY;
6331 rt->dst.flags &= ~DST_NOPOLICY;
6336 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6338 struct inet6_ifaddr *ifa;
6340 read_lock_bh(&idev->lock);
6341 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6342 spin_lock(&ifa->lock);
6344 struct fib6_info *rt = ifa->rt;
6348 ifa->rt->dst_nopolicy = val ? true : false;
6349 if (rt->rt6i_pcpu) {
6350 for_each_possible_cpu(cpu) {
6351 struct rt6_info **rtp;
6353 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
6354 addrconf_set_nopolicy(*rtp, val);
6359 spin_unlock(&ifa->lock);
6361 read_unlock_bh(&idev->lock);
6365 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6367 struct inet6_dev *idev;
6370 if (!rtnl_trylock())
6371 return restart_syscall();
6375 net = (struct net *)ctl->extra2;
6376 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6381 if (valp == &net->ipv6.devconf_all->disable_policy) {
6382 struct net_device *dev;
6384 for_each_netdev(net, dev) {
6385 idev = __in6_dev_get(dev);
6387 addrconf_disable_policy_idev(idev, val);
6390 idev = (struct inet6_dev *)ctl->extra1;
6391 addrconf_disable_policy_idev(idev, val);
6399 int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6400 void __user *buffer, size_t *lenp,
6403 int *valp = ctl->data;
6406 struct ctl_table lctl;
6411 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6413 if (write && (*valp != val))
6414 ret = addrconf_disable_policy(ctl, valp, val);
6422 static int minus_one = -1;
6423 static const int zero = 0;
6424 static const int one = 1;
6425 static const int two_five_five = 255;
6427 static const struct ctl_table addrconf_sysctl[] = {
6429 .procname = "forwarding",
6430 .data = &ipv6_devconf.forwarding,
6431 .maxlen = sizeof(int),
6433 .proc_handler = addrconf_sysctl_forward,
6436 .procname = "hop_limit",
6437 .data = &ipv6_devconf.hop_limit,
6438 .maxlen = sizeof(int),
6440 .proc_handler = proc_dointvec_minmax,
6441 .extra1 = (void *)&one,
6442 .extra2 = (void *)&two_five_five,
6446 .data = &ipv6_devconf.mtu6,
6447 .maxlen = sizeof(int),
6449 .proc_handler = addrconf_sysctl_mtu,
6452 .procname = "accept_ra",
6453 .data = &ipv6_devconf.accept_ra,
6454 .maxlen = sizeof(int),
6456 .proc_handler = proc_dointvec,
6459 .procname = "accept_redirects",
6460 .data = &ipv6_devconf.accept_redirects,
6461 .maxlen = sizeof(int),
6463 .proc_handler = proc_dointvec,
6466 .procname = "autoconf",
6467 .data = &ipv6_devconf.autoconf,
6468 .maxlen = sizeof(int),
6470 .proc_handler = proc_dointvec,
6473 .procname = "dad_transmits",
6474 .data = &ipv6_devconf.dad_transmits,
6475 .maxlen = sizeof(int),
6477 .proc_handler = proc_dointvec,
6480 .procname = "router_solicitations",
6481 .data = &ipv6_devconf.rtr_solicits,
6482 .maxlen = sizeof(int),
6484 .proc_handler = proc_dointvec_minmax,
6485 .extra1 = &minus_one,
6488 .procname = "router_solicitation_interval",
6489 .data = &ipv6_devconf.rtr_solicit_interval,
6490 .maxlen = sizeof(int),
6492 .proc_handler = proc_dointvec_jiffies,
6495 .procname = "router_solicitation_max_interval",
6496 .data = &ipv6_devconf.rtr_solicit_max_interval,
6497 .maxlen = sizeof(int),
6499 .proc_handler = proc_dointvec_jiffies,
6502 .procname = "router_solicitation_delay",
6503 .data = &ipv6_devconf.rtr_solicit_delay,
6504 .maxlen = sizeof(int),
6506 .proc_handler = proc_dointvec_jiffies,
6509 .procname = "force_mld_version",
6510 .data = &ipv6_devconf.force_mld_version,
6511 .maxlen = sizeof(int),
6513 .proc_handler = proc_dointvec,
6516 .procname = "mldv1_unsolicited_report_interval",
6518 &ipv6_devconf.mldv1_unsolicited_report_interval,
6519 .maxlen = sizeof(int),
6521 .proc_handler = proc_dointvec_ms_jiffies,
6524 .procname = "mldv2_unsolicited_report_interval",
6526 &ipv6_devconf.mldv2_unsolicited_report_interval,
6527 .maxlen = sizeof(int),
6529 .proc_handler = proc_dointvec_ms_jiffies,
6532 .procname = "use_tempaddr",
6533 .data = &ipv6_devconf.use_tempaddr,
6534 .maxlen = sizeof(int),
6536 .proc_handler = proc_dointvec,
6539 .procname = "temp_valid_lft",
6540 .data = &ipv6_devconf.temp_valid_lft,
6541 .maxlen = sizeof(int),
6543 .proc_handler = proc_dointvec,
6546 .procname = "temp_prefered_lft",
6547 .data = &ipv6_devconf.temp_prefered_lft,
6548 .maxlen = sizeof(int),
6550 .proc_handler = proc_dointvec,
6553 .procname = "regen_max_retry",
6554 .data = &ipv6_devconf.regen_max_retry,
6555 .maxlen = sizeof(int),
6557 .proc_handler = proc_dointvec,
6560 .procname = "max_desync_factor",
6561 .data = &ipv6_devconf.max_desync_factor,
6562 .maxlen = sizeof(int),
6564 .proc_handler = proc_dointvec,
6567 .procname = "max_addresses",
6568 .data = &ipv6_devconf.max_addresses,
6569 .maxlen = sizeof(int),
6571 .proc_handler = proc_dointvec,
6574 .procname = "accept_ra_defrtr",
6575 .data = &ipv6_devconf.accept_ra_defrtr,
6576 .maxlen = sizeof(int),
6578 .proc_handler = proc_dointvec,
6581 .procname = "accept_ra_min_hop_limit",
6582 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6583 .maxlen = sizeof(int),
6585 .proc_handler = proc_dointvec,
6588 .procname = "accept_ra_pinfo",
6589 .data = &ipv6_devconf.accept_ra_pinfo,
6590 .maxlen = sizeof(int),
6592 .proc_handler = proc_dointvec,
6594 #ifdef CONFIG_IPV6_ROUTER_PREF
6596 .procname = "accept_ra_rtr_pref",
6597 .data = &ipv6_devconf.accept_ra_rtr_pref,
6598 .maxlen = sizeof(int),
6600 .proc_handler = proc_dointvec,
6603 .procname = "router_probe_interval",
6604 .data = &ipv6_devconf.rtr_probe_interval,
6605 .maxlen = sizeof(int),
6607 .proc_handler = proc_dointvec_jiffies,
6609 #ifdef CONFIG_IPV6_ROUTE_INFO
6611 .procname = "accept_ra_rt_info_min_plen",
6612 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6613 .maxlen = sizeof(int),
6615 .proc_handler = proc_dointvec,
6618 .procname = "accept_ra_rt_info_max_plen",
6619 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6620 .maxlen = sizeof(int),
6622 .proc_handler = proc_dointvec,
6627 .procname = "proxy_ndp",
6628 .data = &ipv6_devconf.proxy_ndp,
6629 .maxlen = sizeof(int),
6631 .proc_handler = addrconf_sysctl_proxy_ndp,
6634 .procname = "accept_source_route",
6635 .data = &ipv6_devconf.accept_source_route,
6636 .maxlen = sizeof(int),
6638 .proc_handler = proc_dointvec,
6640 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6642 .procname = "optimistic_dad",
6643 .data = &ipv6_devconf.optimistic_dad,
6644 .maxlen = sizeof(int),
6646 .proc_handler = proc_dointvec,
6649 .procname = "use_optimistic",
6650 .data = &ipv6_devconf.use_optimistic,
6651 .maxlen = sizeof(int),
6653 .proc_handler = proc_dointvec,
6656 #ifdef CONFIG_IPV6_MROUTE
6658 .procname = "mc_forwarding",
6659 .data = &ipv6_devconf.mc_forwarding,
6660 .maxlen = sizeof(int),
6662 .proc_handler = proc_dointvec,
6666 .procname = "disable_ipv6",
6667 .data = &ipv6_devconf.disable_ipv6,
6668 .maxlen = sizeof(int),
6670 .proc_handler = addrconf_sysctl_disable,
6673 .procname = "accept_dad",
6674 .data = &ipv6_devconf.accept_dad,
6675 .maxlen = sizeof(int),
6677 .proc_handler = proc_dointvec,
6680 .procname = "force_tllao",
6681 .data = &ipv6_devconf.force_tllao,
6682 .maxlen = sizeof(int),
6684 .proc_handler = proc_dointvec
6687 .procname = "ndisc_notify",
6688 .data = &ipv6_devconf.ndisc_notify,
6689 .maxlen = sizeof(int),
6691 .proc_handler = proc_dointvec
6694 .procname = "suppress_frag_ndisc",
6695 .data = &ipv6_devconf.suppress_frag_ndisc,
6696 .maxlen = sizeof(int),
6698 .proc_handler = proc_dointvec
6701 .procname = "accept_ra_from_local",
6702 .data = &ipv6_devconf.accept_ra_from_local,
6703 .maxlen = sizeof(int),
6705 .proc_handler = proc_dointvec,
6708 .procname = "accept_ra_mtu",
6709 .data = &ipv6_devconf.accept_ra_mtu,
6710 .maxlen = sizeof(int),
6712 .proc_handler = proc_dointvec,
6715 .procname = "stable_secret",
6716 .data = &ipv6_devconf.stable_secret,
6717 .maxlen = IPV6_MAX_STRLEN,
6719 .proc_handler = addrconf_sysctl_stable_secret,
6722 .procname = "use_oif_addrs_only",
6723 .data = &ipv6_devconf.use_oif_addrs_only,
6724 .maxlen = sizeof(int),
6726 .proc_handler = proc_dointvec,
6729 .procname = "ignore_routes_with_linkdown",
6730 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6731 .maxlen = sizeof(int),
6733 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6736 .procname = "drop_unicast_in_l2_multicast",
6737 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6738 .maxlen = sizeof(int),
6740 .proc_handler = proc_dointvec,
6743 .procname = "drop_unsolicited_na",
6744 .data = &ipv6_devconf.drop_unsolicited_na,
6745 .maxlen = sizeof(int),
6747 .proc_handler = proc_dointvec,
6750 .procname = "keep_addr_on_down",
6751 .data = &ipv6_devconf.keep_addr_on_down,
6752 .maxlen = sizeof(int),
6754 .proc_handler = proc_dointvec,
6758 .procname = "seg6_enabled",
6759 .data = &ipv6_devconf.seg6_enabled,
6760 .maxlen = sizeof(int),
6762 .proc_handler = proc_dointvec,
6764 #ifdef CONFIG_IPV6_SEG6_HMAC
6766 .procname = "seg6_require_hmac",
6767 .data = &ipv6_devconf.seg6_require_hmac,
6768 .maxlen = sizeof(int),
6770 .proc_handler = proc_dointvec,
6774 .procname = "enhanced_dad",
6775 .data = &ipv6_devconf.enhanced_dad,
6776 .maxlen = sizeof(int),
6778 .proc_handler = proc_dointvec,
6781 .procname = "addr_gen_mode",
6782 .data = &ipv6_devconf.addr_gen_mode,
6783 .maxlen = sizeof(int),
6785 .proc_handler = addrconf_sysctl_addr_gen_mode,
6788 .procname = "disable_policy",
6789 .data = &ipv6_devconf.disable_policy,
6790 .maxlen = sizeof(int),
6792 .proc_handler = addrconf_sysctl_disable_policy,
6795 .procname = "ndisc_tclass",
6796 .data = &ipv6_devconf.ndisc_tclass,
6797 .maxlen = sizeof(int),
6799 .proc_handler = proc_dointvec_minmax,
6800 .extra1 = (void *)&zero,
6801 .extra2 = (void *)&two_five_five,
6808 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6809 struct inet6_dev *idev, struct ipv6_devconf *p)
6812 struct ctl_table *table;
6813 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6815 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6819 for (i = 0; table[i].data; i++) {
6820 table[i].data += (char *)p - (char *)&ipv6_devconf;
6821 /* If one of these is already set, then it is not safe to
6822 * overwrite either of them: this makes proc_dointvec_minmax
6825 if (!table[i].extra1 && !table[i].extra2) {
6826 table[i].extra1 = idev; /* embedded; no ref */
6827 table[i].extra2 = net;
6831 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6833 p->sysctl_header = register_net_sysctl(net, path, table);
6834 if (!p->sysctl_header)
6837 if (!strcmp(dev_name, "all"))
6838 ifindex = NETCONFA_IFINDEX_ALL;
6839 else if (!strcmp(dev_name, "default"))
6840 ifindex = NETCONFA_IFINDEX_DEFAULT;
6842 ifindex = idev->dev->ifindex;
6843 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6853 static void __addrconf_sysctl_unregister(struct net *net,
6854 struct ipv6_devconf *p, int ifindex)
6856 struct ctl_table *table;
6858 if (!p->sysctl_header)
6861 table = p->sysctl_header->ctl_table_arg;
6862 unregister_net_sysctl_table(p->sysctl_header);
6863 p->sysctl_header = NULL;
6866 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
6869 static int addrconf_sysctl_register(struct inet6_dev *idev)
6873 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6876 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6877 &ndisc_ifinfo_sysctl_change);
6880 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6883 neigh_sysctl_unregister(idev->nd_parms);
6888 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6890 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6891 idev->dev->ifindex);
6892 neigh_sysctl_unregister(idev->nd_parms);
6898 static int __net_init addrconf_init_net(struct net *net)
6901 struct ipv6_devconf *all, *dflt;
6903 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6907 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6909 goto err_alloc_dflt;
6911 if (IS_ENABLED(CONFIG_SYSCTL) &&
6912 sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) {
6913 memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf));
6914 memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt));
6917 /* these will be inherited by all namespaces */
6918 dflt->autoconf = ipv6_defaults.autoconf;
6919 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6921 dflt->stable_secret.initialized = false;
6922 all->stable_secret.initialized = false;
6924 net->ipv6.devconf_all = all;
6925 net->ipv6.devconf_dflt = dflt;
6927 #ifdef CONFIG_SYSCTL
6928 err = __addrconf_sysctl_register(net, "all", NULL, all);
6932 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6938 #ifdef CONFIG_SYSCTL
6940 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
6950 static void __net_exit addrconf_exit_net(struct net *net)
6952 #ifdef CONFIG_SYSCTL
6953 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6954 NETCONFA_IFINDEX_DEFAULT);
6955 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6956 NETCONFA_IFINDEX_ALL);
6958 kfree(net->ipv6.devconf_dflt);
6959 kfree(net->ipv6.devconf_all);
6962 static struct pernet_operations addrconf_ops = {
6963 .init = addrconf_init_net,
6964 .exit = addrconf_exit_net,
6967 static struct rtnl_af_ops inet6_ops __read_mostly = {
6969 .fill_link_af = inet6_fill_link_af,
6970 .get_link_af_size = inet6_get_link_af_size,
6971 .validate_link_af = inet6_validate_link_af,
6972 .set_link_af = inet6_set_link_af,
6976 * Init / cleanup code
6979 int __init addrconf_init(void)
6981 struct inet6_dev *idev;
6984 err = ipv6_addr_label_init();
6986 pr_crit("%s: cannot initialize default policy table: %d\n",
6991 err = register_pernet_subsys(&addrconf_ops);
6995 addrconf_wq = create_workqueue("ipv6_addrconf");
7001 /* The addrconf netdev notifier requires that loopback_dev
7002 * has it's ipv6 private information allocated and setup
7003 * before it can bring up and give link-local addresses
7004 * to other devices which are up.
7006 * Unfortunately, loopback_dev is not necessarily the first
7007 * entry in the global dev_base list of net devices. In fact,
7008 * it is likely to be the very last entry on that list.
7009 * So this causes the notifier registry below to try and
7010 * give link-local addresses to all devices besides loopback_dev
7011 * first, then loopback_dev, which cases all the non-loopback_dev
7012 * devices to fail to get a link-local address.
7014 * So, as a temporary fix, allocate the ipv6 structure for
7015 * loopback_dev first by hand.
7016 * Longer term, all of the dependencies ipv6 has upon the loopback
7017 * device and it being up should be removed.
7020 idev = ipv6_add_dev(init_net.loopback_dev);
7023 err = PTR_ERR(idev);
7027 ip6_route_init_special_entries();
7029 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7030 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7032 register_netdevice_notifier(&ipv6_dev_notf);
7036 rtnl_af_register(&inet6_ops);
7038 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7039 NULL, inet6_dump_ifinfo, 0);
7043 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7044 inet6_rtm_newaddr, NULL, 0);
7047 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7048 inet6_rtm_deladdr, NULL, 0);
7051 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7052 inet6_rtm_getaddr, inet6_dump_ifaddr,
7053 RTNL_FLAG_DOIT_UNLOCKED);
7056 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7057 NULL, inet6_dump_ifmcaddr, 0);
7060 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7061 NULL, inet6_dump_ifacaddr, 0);
7064 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7065 inet6_netconf_get_devconf,
7066 inet6_netconf_dump_devconf,
7067 RTNL_FLAG_DOIT_UNLOCKED);
7070 err = ipv6_addr_label_rtnl_register();
7076 rtnl_unregister_all(PF_INET6);
7077 rtnl_af_unregister(&inet6_ops);
7078 unregister_netdevice_notifier(&ipv6_dev_notf);
7080 destroy_workqueue(addrconf_wq);
7082 unregister_pernet_subsys(&addrconf_ops);
7084 ipv6_addr_label_cleanup();
7089 void addrconf_cleanup(void)
7091 struct net_device *dev;
7094 unregister_netdevice_notifier(&ipv6_dev_notf);
7095 unregister_pernet_subsys(&addrconf_ops);
7096 ipv6_addr_label_cleanup();
7098 rtnl_af_unregister(&inet6_ops);
7102 /* clean dev list */
7103 for_each_netdev(&init_net, dev) {
7104 if (__in6_dev_get(dev) == NULL)
7106 addrconf_ifdown(dev, 1);
7108 addrconf_ifdown(init_net.loopback_dev, 2);
7113 spin_lock_bh(&addrconf_hash_lock);
7114 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7115 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
7116 spin_unlock_bh(&addrconf_hash_lock);
7117 cancel_delayed_work(&addr_chk_work);
7120 destroy_workqueue(addrconf_wq);