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(nlh, sizeof(struct netconfmsg), tb,
615 NETCONFA_MAX, devconf_ipv6_policy, extack);
617 err = nlmsg_parse_strict(nlh, sizeof(struct netconfmsg), tb,
618 NETCONFA_MAX, devconf_ipv6_policy, extack);
622 for (i = 0; i <= NETCONFA_MAX; i++) {
627 case NETCONFA_IFINDEX:
630 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
638 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
639 struct nlmsghdr *nlh,
640 struct netlink_ext_ack *extack)
642 struct net *net = sock_net(in_skb->sk);
643 struct nlattr *tb[NETCONFA_MAX+1];
644 struct inet6_dev *in6_dev = NULL;
645 struct net_device *dev = NULL;
647 struct ipv6_devconf *devconf;
651 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
655 if (!tb[NETCONFA_IFINDEX])
659 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
661 case NETCONFA_IFINDEX_ALL:
662 devconf = net->ipv6.devconf_all;
664 case NETCONFA_IFINDEX_DEFAULT:
665 devconf = net->ipv6.devconf_dflt;
668 dev = dev_get_by_index(net, ifindex);
671 in6_dev = in6_dev_get(dev);
674 devconf = &in6_dev->cnf;
679 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
683 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
684 NETLINK_CB(in_skb).portid,
685 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
688 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
689 WARN_ON(err == -EMSGSIZE);
693 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
696 in6_dev_put(in6_dev);
702 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
703 struct netlink_callback *cb)
705 const struct nlmsghdr *nlh = cb->nlh;
706 struct net *net = sock_net(skb->sk);
709 struct net_device *dev;
710 struct inet6_dev *idev;
711 struct hlist_head *head;
713 if (cb->strict_check) {
714 struct netlink_ext_ack *extack = cb->extack;
715 struct netconfmsg *ncm;
717 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
718 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
722 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
723 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
729 s_idx = idx = cb->args[1];
731 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
733 head = &net->dev_index_head[h];
735 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
737 hlist_for_each_entry_rcu(dev, head, index_hlist) {
740 idev = __in6_dev_get(dev);
744 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
746 NETLINK_CB(cb->skb).portid,
754 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
760 if (h == NETDEV_HASHENTRIES) {
761 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
762 net->ipv6.devconf_all,
763 NETLINK_CB(cb->skb).portid,
765 RTM_NEWNETCONF, NLM_F_MULTI,
771 if (h == NETDEV_HASHENTRIES + 1) {
772 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
773 net->ipv6.devconf_dflt,
774 NETLINK_CB(cb->skb).portid,
776 RTM_NEWNETCONF, NLM_F_MULTI,
790 static void dev_forward_change(struct inet6_dev *idev)
792 struct net_device *dev;
793 struct inet6_ifaddr *ifa;
798 if (idev->cnf.forwarding)
799 dev_disable_lro(dev);
800 if (dev->flags & IFF_MULTICAST) {
801 if (idev->cnf.forwarding) {
802 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
803 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
804 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
806 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
807 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
808 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
812 list_for_each_entry(ifa, &idev->addr_list, if_list) {
813 if (ifa->flags&IFA_F_TENTATIVE)
815 if (idev->cnf.forwarding)
816 addrconf_join_anycast(ifa);
818 addrconf_leave_anycast(ifa);
820 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
822 dev->ifindex, &idev->cnf);
826 static void addrconf_forward_change(struct net *net, __s32 newf)
828 struct net_device *dev;
829 struct inet6_dev *idev;
831 for_each_netdev(net, dev) {
832 idev = __in6_dev_get(dev);
834 int changed = (!idev->cnf.forwarding) ^ (!newf);
835 idev->cnf.forwarding = newf;
837 dev_forward_change(idev);
842 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
848 return restart_syscall();
850 net = (struct net *)table->extra2;
854 if (p == &net->ipv6.devconf_dflt->forwarding) {
855 if ((!newf) ^ (!old))
856 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
858 NETCONFA_IFINDEX_DEFAULT,
859 net->ipv6.devconf_dflt);
864 if (p == &net->ipv6.devconf_all->forwarding) {
865 int old_dflt = net->ipv6.devconf_dflt->forwarding;
867 net->ipv6.devconf_dflt->forwarding = newf;
868 if ((!newf) ^ (!old_dflt))
869 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
871 NETCONFA_IFINDEX_DEFAULT,
872 net->ipv6.devconf_dflt);
874 addrconf_forward_change(net, newf);
875 if ((!newf) ^ (!old))
876 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
878 NETCONFA_IFINDEX_ALL,
879 net->ipv6.devconf_all);
880 } else if ((!newf) ^ (!old))
881 dev_forward_change((struct inet6_dev *)table->extra1);
885 rt6_purge_dflt_routers(net);
889 static void addrconf_linkdown_change(struct net *net, __s32 newf)
891 struct net_device *dev;
892 struct inet6_dev *idev;
894 for_each_netdev(net, dev) {
895 idev = __in6_dev_get(dev);
897 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
899 idev->cnf.ignore_routes_with_linkdown = newf;
901 inet6_netconf_notify_devconf(dev_net(dev),
903 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
910 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
916 return restart_syscall();
918 net = (struct net *)table->extra2;
922 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
923 if ((!newf) ^ (!old))
924 inet6_netconf_notify_devconf(net,
926 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
927 NETCONFA_IFINDEX_DEFAULT,
928 net->ipv6.devconf_dflt);
933 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
934 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
935 addrconf_linkdown_change(net, newf);
936 if ((!newf) ^ (!old))
937 inet6_netconf_notify_devconf(net,
939 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
940 NETCONFA_IFINDEX_ALL,
941 net->ipv6.devconf_all);
950 /* Nobody refers to this ifaddr, destroy it */
951 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
953 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
955 #ifdef NET_REFCNT_DEBUG
956 pr_debug("%s\n", __func__);
959 in6_dev_put(ifp->idev);
961 if (cancel_delayed_work(&ifp->dad_work))
962 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
965 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
966 pr_warn("Freeing alive inet6 address %p\n", ifp);
974 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
977 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
980 * Each device address list is sorted in order of scope -
981 * global before linklocal.
983 list_for_each(p, &idev->addr_list) {
984 struct inet6_ifaddr *ifa
985 = list_entry(p, struct inet6_ifaddr, if_list);
986 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
990 list_add_tail_rcu(&ifp->if_list, p);
993 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
995 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
997 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
1000 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1001 struct net_device *dev, unsigned int hash)
1003 struct inet6_ifaddr *ifp;
1005 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1006 if (!net_eq(dev_net(ifp->idev->dev), net))
1008 if (ipv6_addr_equal(&ifp->addr, addr)) {
1009 if (!dev || ifp->idev->dev == dev)
1016 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1018 unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
1021 spin_lock(&addrconf_hash_lock);
1023 /* Ignore adding duplicate addresses on an interface */
1024 if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
1025 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1028 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1031 spin_unlock(&addrconf_hash_lock);
1036 /* On success it returns ifp with increased reference count */
1038 static struct inet6_ifaddr *
1039 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1040 bool can_block, struct netlink_ext_ack *extack)
1042 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1043 int addr_type = ipv6_addr_type(cfg->pfx);
1044 struct net *net = dev_net(idev->dev);
1045 struct inet6_ifaddr *ifa = NULL;
1046 struct fib6_info *f6i = NULL;
1049 if (addr_type == IPV6_ADDR_ANY ||
1050 addr_type & IPV6_ADDR_MULTICAST ||
1051 (!(idev->dev->flags & IFF_LOOPBACK) &&
1052 !netif_is_l3_master(idev->dev) &&
1053 addr_type & IPV6_ADDR_LOOPBACK))
1054 return ERR_PTR(-EADDRNOTAVAIL);
1057 err = -ENODEV; /*XXX*/
1061 if (idev->cnf.disable_ipv6) {
1066 /* validator notifier needs to be blocking;
1067 * do not call in atomic context
1070 struct in6_validator_info i6vi = {
1071 .i6vi_addr = *cfg->pfx,
1076 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1077 err = notifier_to_errno(err);
1082 ifa = kzalloc(sizeof(*ifa), gfp_flags);
1088 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
1095 if (net->ipv6.devconf_all->disable_policy ||
1096 idev->cnf.disable_policy)
1097 f6i->dst_nopolicy = true;
1099 neigh_parms_data_state_setall(idev->nd_parms);
1101 ifa->addr = *cfg->pfx;
1103 ifa->peer_addr = *cfg->peer_pfx;
1105 spin_lock_init(&ifa->lock);
1106 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1107 INIT_HLIST_NODE(&ifa->addr_lst);
1108 ifa->scope = cfg->scope;
1109 ifa->prefix_len = cfg->plen;
1110 ifa->rt_priority = cfg->rt_priority;
1111 ifa->flags = cfg->ifa_flags;
1112 /* No need to add the TENTATIVE flag for addresses with NODAD */
1113 if (!(cfg->ifa_flags & IFA_F_NODAD))
1114 ifa->flags |= IFA_F_TENTATIVE;
1115 ifa->valid_lft = cfg->valid_lft;
1116 ifa->prefered_lft = cfg->preferred_lft;
1117 ifa->cstamp = ifa->tstamp = jiffies;
1118 ifa->tokenized = false;
1126 refcount_set(&ifa->refcnt, 1);
1130 err = ipv6_add_addr_hash(idev->dev, ifa);
1132 rcu_read_unlock_bh();
1136 write_lock(&idev->lock);
1138 /* Add to inet6_dev unicast addr list. */
1139 ipv6_link_dev_addr(idev, ifa);
1141 if (ifa->flags&IFA_F_TEMPORARY) {
1142 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1147 write_unlock(&idev->lock);
1149 rcu_read_unlock_bh();
1151 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1153 if (unlikely(err < 0)) {
1154 fib6_info_release(f6i);
1158 in6_dev_put(ifa->idev);
1167 enum cleanup_prefix_rt_t {
1168 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1169 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1170 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1174 * Check, whether the prefix for ifp would still need a prefix route
1175 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1178 * 1) we don't purge prefix if address was not permanent.
1179 * prefix is managed by its own lifetime.
1180 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1181 * 3) if there are no addresses, delete prefix.
1182 * 4) if there are still other permanent address(es),
1183 * corresponding prefix is still permanent.
1184 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1185 * don't purge the prefix, assume user space is managing it.
1186 * 6) otherwise, update prefix lifetime to the
1187 * longest valid lifetime among the corresponding
1188 * addresses on the device.
1189 * Note: subsequent RA will update lifetime.
1191 static enum cleanup_prefix_rt_t
1192 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1194 struct inet6_ifaddr *ifa;
1195 struct inet6_dev *idev = ifp->idev;
1196 unsigned long lifetime;
1197 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1201 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1204 if (ifa->prefix_len != ifp->prefix_len ||
1205 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1208 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1209 return CLEANUP_PREFIX_RT_NOP;
1211 action = CLEANUP_PREFIX_RT_EXPIRE;
1213 spin_lock(&ifa->lock);
1215 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1217 * Note: Because this address is
1218 * not permanent, lifetime <
1219 * LONG_MAX / HZ here.
1221 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1222 *expires = ifa->tstamp + lifetime * HZ;
1223 spin_unlock(&ifa->lock);
1230 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1232 struct fib6_info *f6i;
1234 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
1235 ifp->idev->dev, 0, RTF_DEFAULT, true);
1238 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
1240 if (!(f6i->fib6_flags & RTF_EXPIRES))
1241 fib6_set_expires(f6i, expires);
1242 fib6_info_release(f6i);
1248 /* This function wants to get referenced ifp and releases it before return */
1250 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1253 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1254 unsigned long expires;
1258 spin_lock_bh(&ifp->lock);
1260 ifp->state = INET6_IFADDR_STATE_DEAD;
1261 spin_unlock_bh(&ifp->lock);
1263 if (state == INET6_IFADDR_STATE_DEAD)
1266 spin_lock_bh(&addrconf_hash_lock);
1267 hlist_del_init_rcu(&ifp->addr_lst);
1268 spin_unlock_bh(&addrconf_hash_lock);
1270 write_lock_bh(&ifp->idev->lock);
1272 if (ifp->flags&IFA_F_TEMPORARY) {
1273 list_del(&ifp->tmp_list);
1275 in6_ifa_put(ifp->ifpub);
1281 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1282 action = check_cleanup_prefix_route(ifp, &expires);
1284 list_del_rcu(&ifp->if_list);
1287 write_unlock_bh(&ifp->idev->lock);
1289 addrconf_del_dad_work(ifp);
1291 ipv6_ifa_notify(RTM_DELADDR, ifp);
1293 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1295 if (action != CLEANUP_PREFIX_RT_NOP) {
1296 cleanup_prefix_route(ifp, expires,
1297 action == CLEANUP_PREFIX_RT_DEL);
1300 /* clean up prefsrc entries */
1301 rt6_remove_prefsrc(ifp);
1306 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp,
1307 struct inet6_ifaddr *ift,
1310 struct inet6_dev *idev = ifp->idev;
1311 struct in6_addr addr, *tmpaddr;
1312 unsigned long tmp_tstamp, age;
1313 unsigned long regen_advance;
1314 struct ifa6_config cfg;
1316 unsigned long now = jiffies;
1317 long max_desync_factor;
1318 s32 cnf_temp_preferred_lft;
1320 write_lock_bh(&idev->lock);
1322 spin_lock_bh(&ift->lock);
1323 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1324 spin_unlock_bh(&ift->lock);
1331 if (idev->cnf.use_tempaddr <= 0) {
1332 write_unlock_bh(&idev->lock);
1333 pr_info("%s: use_tempaddr is disabled\n", __func__);
1338 spin_lock_bh(&ifp->lock);
1339 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1340 idev->cnf.use_tempaddr = -1; /*XXX*/
1341 spin_unlock_bh(&ifp->lock);
1342 write_unlock_bh(&idev->lock);
1343 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1350 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1351 ipv6_try_regen_rndid(idev, tmpaddr);
1352 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1353 age = (now - ifp->tstamp) / HZ;
1355 regen_advance = idev->cnf.regen_max_retry *
1356 idev->cnf.dad_transmits *
1357 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1359 /* recalculate max_desync_factor each time and update
1360 * idev->desync_factor if it's larger
1362 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1363 max_desync_factor = min_t(__u32,
1364 idev->cnf.max_desync_factor,
1365 cnf_temp_preferred_lft - regen_advance);
1367 if (unlikely(idev->desync_factor > max_desync_factor)) {
1368 if (max_desync_factor > 0) {
1369 get_random_bytes(&idev->desync_factor,
1370 sizeof(idev->desync_factor));
1371 idev->desync_factor %= max_desync_factor;
1373 idev->desync_factor = 0;
1377 memset(&cfg, 0, sizeof(cfg));
1378 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
1379 idev->cnf.temp_valid_lft + age);
1380 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1381 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1383 cfg.plen = ifp->prefix_len;
1384 tmp_tstamp = ifp->tstamp;
1385 spin_unlock_bh(&ifp->lock);
1387 write_unlock_bh(&idev->lock);
1389 /* A temporary address is created only if this calculated Preferred
1390 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1391 * an implementation must not create a temporary address with a zero
1392 * Preferred Lifetime.
1393 * Use age calculation as in addrconf_verify to avoid unnecessary
1394 * temporary addresses being generated.
1396 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1397 if (cfg.preferred_lft <= regen_advance + age) {
1404 cfg.ifa_flags = IFA_F_TEMPORARY;
1405 /* set in addrconf_prefix_rcv() */
1406 if (ifp->flags & IFA_F_OPTIMISTIC)
1407 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
1410 cfg.scope = ipv6_addr_scope(cfg.pfx);
1412 ift = ipv6_add_addr(idev, &cfg, block, NULL);
1416 pr_info("%s: retry temporary address regeneration\n", __func__);
1418 write_lock_bh(&idev->lock);
1422 spin_lock_bh(&ift->lock);
1425 ift->tstamp = tmp_tstamp;
1426 spin_unlock_bh(&ift->lock);
1428 addrconf_dad_start(ift);
1436 * Choose an appropriate source address (RFC3484)
1439 IPV6_SADDR_RULE_INIT = 0,
1440 IPV6_SADDR_RULE_LOCAL,
1441 IPV6_SADDR_RULE_SCOPE,
1442 IPV6_SADDR_RULE_PREFERRED,
1443 #ifdef CONFIG_IPV6_MIP6
1444 IPV6_SADDR_RULE_HOA,
1446 IPV6_SADDR_RULE_OIF,
1447 IPV6_SADDR_RULE_LABEL,
1448 IPV6_SADDR_RULE_PRIVACY,
1449 IPV6_SADDR_RULE_ORCHID,
1450 IPV6_SADDR_RULE_PREFIX,
1451 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1452 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1457 struct ipv6_saddr_score {
1460 struct inet6_ifaddr *ifa;
1461 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1466 struct ipv6_saddr_dst {
1467 const struct in6_addr *addr;
1474 static inline int ipv6_saddr_preferred(int type)
1476 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1481 static bool ipv6_use_optimistic_addr(struct net *net,
1482 struct inet6_dev *idev)
1484 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1487 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1489 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1498 static bool ipv6_allow_optimistic_dad(struct net *net,
1499 struct inet6_dev *idev)
1501 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1504 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1513 static int ipv6_get_saddr_eval(struct net *net,
1514 struct ipv6_saddr_score *score,
1515 struct ipv6_saddr_dst *dst,
1520 if (i <= score->rule) {
1522 case IPV6_SADDR_RULE_SCOPE:
1523 ret = score->scopedist;
1525 case IPV6_SADDR_RULE_PREFIX:
1526 ret = score->matchlen;
1529 ret = !!test_bit(i, score->scorebits);
1535 case IPV6_SADDR_RULE_INIT:
1536 /* Rule 0: remember if hiscore is not ready yet */
1539 case IPV6_SADDR_RULE_LOCAL:
1540 /* Rule 1: Prefer same address */
1541 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1543 case IPV6_SADDR_RULE_SCOPE:
1544 /* Rule 2: Prefer appropriate scope
1549 * ---+--+-+---> scope
1551 * | d is scope of the destination.
1553 * | \ <- smaller scope is better if
1554 * B-15 | \ if scope is enough for destination.
1555 * | ret = B - scope (-1 <= scope >= d <= 15).
1557 * |/ <- greater is better
1558 * -C / if scope is not enough for destination.
1559 * /| ret = scope - C (-1 <= d < scope <= 15).
1561 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1562 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1563 * Assume B = 0 and we get C > 29.
1565 ret = __ipv6_addr_src_scope(score->addr_type);
1566 if (ret >= dst->scope)
1569 ret -= 128; /* 30 is enough */
1570 score->scopedist = ret;
1572 case IPV6_SADDR_RULE_PREFERRED:
1574 /* Rule 3: Avoid deprecated and optimistic addresses */
1575 u8 avoid = IFA_F_DEPRECATED;
1577 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
1578 avoid |= IFA_F_OPTIMISTIC;
1579 ret = ipv6_saddr_preferred(score->addr_type) ||
1580 !(score->ifa->flags & avoid);
1583 #ifdef CONFIG_IPV6_MIP6
1584 case IPV6_SADDR_RULE_HOA:
1586 /* Rule 4: Prefer home address */
1587 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1588 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1592 case IPV6_SADDR_RULE_OIF:
1593 /* Rule 5: Prefer outgoing interface */
1594 ret = (!dst->ifindex ||
1595 dst->ifindex == score->ifa->idev->dev->ifindex);
1597 case IPV6_SADDR_RULE_LABEL:
1598 /* Rule 6: Prefer matching label */
1599 ret = ipv6_addr_label(net,
1600 &score->ifa->addr, score->addr_type,
1601 score->ifa->idev->dev->ifindex) == dst->label;
1603 case IPV6_SADDR_RULE_PRIVACY:
1605 /* Rule 7: Prefer public address
1606 * Note: prefer temporary address if use_tempaddr >= 2
1608 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1609 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1610 score->ifa->idev->cnf.use_tempaddr >= 2;
1611 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1614 case IPV6_SADDR_RULE_ORCHID:
1615 /* Rule 8-: Prefer ORCHID vs ORCHID or
1616 * non-ORCHID vs non-ORCHID
1618 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1619 ipv6_addr_orchid(dst->addr));
1621 case IPV6_SADDR_RULE_PREFIX:
1622 /* Rule 8: Use longest matching prefix */
1623 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1624 if (ret > score->ifa->prefix_len)
1625 ret = score->ifa->prefix_len;
1626 score->matchlen = ret;
1628 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1629 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1630 /* Optimistic addresses still have lower precedence than other
1631 * preferred addresses.
1633 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1641 __set_bit(i, score->scorebits);
1647 static int __ipv6_dev_get_saddr(struct net *net,
1648 struct ipv6_saddr_dst *dst,
1649 struct inet6_dev *idev,
1650 struct ipv6_saddr_score *scores,
1653 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1655 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1659 * - Tentative Address (RFC2462 section 5.4)
1660 * - A tentative address is not considered
1661 * "assigned to an interface" in the traditional
1662 * sense, unless it is also flagged as optimistic.
1663 * - Candidate Source Address (section 4)
1664 * - In any case, anycast addresses, multicast
1665 * addresses, and the unspecified address MUST
1666 * NOT be included in a candidate set.
1668 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1669 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1672 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1674 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1675 score->addr_type & IPV6_ADDR_MULTICAST)) {
1676 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1682 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1684 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1685 int minihiscore, miniscore;
1687 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1688 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1690 if (minihiscore > miniscore) {
1691 if (i == IPV6_SADDR_RULE_SCOPE &&
1692 score->scopedist > 0) {
1695 * each remaining entry
1696 * has too small (not enough)
1697 * scope, because ifa entries
1698 * are sorted by their scope
1704 } else if (minihiscore < miniscore) {
1705 swap(hiscore, score);
1706 hiscore_idx = 1 - hiscore_idx;
1708 /* restore our iterator */
1709 score->ifa = hiscore->ifa;
1719 static int ipv6_get_saddr_master(struct net *net,
1720 const struct net_device *dst_dev,
1721 const struct net_device *master,
1722 struct ipv6_saddr_dst *dst,
1723 struct ipv6_saddr_score *scores,
1726 struct inet6_dev *idev;
1728 idev = __in6_dev_get(dst_dev);
1730 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1731 scores, hiscore_idx);
1733 idev = __in6_dev_get(master);
1735 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1736 scores, hiscore_idx);
1741 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1742 const struct in6_addr *daddr, unsigned int prefs,
1743 struct in6_addr *saddr)
1745 struct ipv6_saddr_score scores[2], *hiscore;
1746 struct ipv6_saddr_dst dst;
1747 struct inet6_dev *idev;
1748 struct net_device *dev;
1750 bool use_oif_addr = false;
1751 int hiscore_idx = 0;
1754 dst_type = __ipv6_addr_type(daddr);
1756 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1757 dst.scope = __ipv6_addr_src_scope(dst_type);
1758 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1761 scores[hiscore_idx].rule = -1;
1762 scores[hiscore_idx].ifa = NULL;
1766 /* Candidate Source Address (section 4)
1767 * - multicast and link-local destination address,
1768 * the set of candidate source address MUST only
1769 * include addresses assigned to interfaces
1770 * belonging to the same link as the outgoing
1772 * (- For site-local destination addresses, the
1773 * set of candidate source addresses MUST only
1774 * include addresses assigned to interfaces
1775 * belonging to the same site as the outgoing
1777 * - "It is RECOMMENDED that the candidate source addresses
1778 * be the set of unicast addresses assigned to the
1779 * interface that will be used to send to the destination
1780 * (the 'outgoing' interface)." (RFC 6724)
1783 idev = __in6_dev_get(dst_dev);
1784 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1785 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1786 (idev && idev->cnf.use_oif_addrs_only)) {
1787 use_oif_addr = true;
1793 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1795 const struct net_device *master;
1798 /* if dst_dev exists and is enslaved to an L3 device, then
1799 * prefer addresses from dst_dev and then the master over
1800 * any other enslaved devices in the L3 domain.
1802 master = l3mdev_master_dev_rcu(dst_dev);
1804 master_idx = master->ifindex;
1806 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1808 scores, hiscore_idx);
1810 if (scores[hiscore_idx].ifa)
1814 for_each_netdev_rcu(net, dev) {
1815 /* only consider addresses on devices in the
1818 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1820 idev = __in6_dev_get(dev);
1823 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1828 hiscore = &scores[hiscore_idx];
1830 ret = -EADDRNOTAVAIL;
1832 *saddr = hiscore->ifa->addr;
1837 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1839 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1842 struct inet6_ifaddr *ifp;
1843 int err = -EADDRNOTAVAIL;
1845 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1846 if (ifp->scope > IFA_LINK)
1848 if (ifp->scope == IFA_LINK &&
1849 !(ifp->flags & banned_flags)) {
1858 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1861 struct inet6_dev *idev;
1862 int err = -EADDRNOTAVAIL;
1865 idev = __in6_dev_get(dev);
1867 read_lock_bh(&idev->lock);
1868 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1869 read_unlock_bh(&idev->lock);
1875 static int ipv6_count_addresses(const struct inet6_dev *idev)
1877 const struct inet6_ifaddr *ifp;
1881 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1887 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1888 const struct net_device *dev, int strict)
1890 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1891 strict, IFA_F_TENTATIVE);
1893 EXPORT_SYMBOL(ipv6_chk_addr);
1895 /* device argument is used to find the L3 domain of interest. If
1896 * skip_dev_check is set, then the ifp device is not checked against
1897 * the passed in dev argument. So the 2 cases for addresses checks are:
1898 * 1. does the address exist in the L3 domain that dev is part of
1899 * (skip_dev_check = true), or
1901 * 2. does the address exist on the specific device
1902 * (skip_dev_check = false)
1904 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1905 const struct net_device *dev, bool skip_dev_check,
1906 int strict, u32 banned_flags)
1908 unsigned int hash = inet6_addr_hash(net, addr);
1909 const struct net_device *l3mdev;
1910 struct inet6_ifaddr *ifp;
1915 l3mdev = l3mdev_master_dev_rcu(dev);
1919 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1920 if (!net_eq(dev_net(ifp->idev->dev), net))
1923 if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
1926 /* Decouple optimistic from tentative for evaluation here.
1927 * Ban optimistic addresses explicitly, when required.
1929 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1930 ? (ifp->flags&~IFA_F_TENTATIVE)
1932 if (ipv6_addr_equal(&ifp->addr, addr) &&
1933 !(ifp_flags&banned_flags) &&
1934 (!dev || ifp->idev->dev == dev ||
1935 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1944 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1947 /* Compares an address/prefix_len with addresses on device @dev.
1948 * If one is found it returns true.
1950 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1951 const unsigned int prefix_len, struct net_device *dev)
1953 const struct inet6_ifaddr *ifa;
1954 const struct inet6_dev *idev;
1958 idev = __in6_dev_get(dev);
1960 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1961 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1970 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1972 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1974 const struct inet6_ifaddr *ifa;
1975 const struct inet6_dev *idev;
1980 idev = __in6_dev_get(dev);
1982 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
1983 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1992 EXPORT_SYMBOL(ipv6_chk_prefix);
1994 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1995 struct net_device *dev, int strict)
1997 unsigned int hash = inet6_addr_hash(net, addr);
1998 struct inet6_ifaddr *ifp, *result = NULL;
2001 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
2002 if (!net_eq(dev_net(ifp->idev->dev), net))
2004 if (ipv6_addr_equal(&ifp->addr, addr)) {
2005 if (!dev || ifp->idev->dev == dev ||
2006 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2018 /* Gets referenced address, destroys ifaddr */
2020 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2023 ifp->flags |= IFA_F_DADFAILED;
2025 if (ifp->flags&IFA_F_TEMPORARY) {
2026 struct inet6_ifaddr *ifpub;
2027 spin_lock_bh(&ifp->lock);
2030 in6_ifa_hold(ifpub);
2031 spin_unlock_bh(&ifp->lock);
2032 ipv6_create_tempaddr(ifpub, ifp, true);
2035 spin_unlock_bh(&ifp->lock);
2038 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2039 spin_lock_bh(&ifp->lock);
2040 addrconf_del_dad_work(ifp);
2041 ifp->flags |= IFA_F_TENTATIVE;
2043 ifp->flags &= ~IFA_F_OPTIMISTIC;
2044 spin_unlock_bh(&ifp->lock);
2046 ipv6_ifa_notify(0, ifp);
2053 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2057 spin_lock_bh(&ifp->lock);
2058 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2059 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2062 spin_unlock_bh(&ifp->lock);
2067 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2069 struct inet6_dev *idev = ifp->idev;
2070 struct net *net = dev_net(ifp->idev->dev);
2072 if (addrconf_dad_end(ifp)) {
2077 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2078 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2080 spin_lock_bh(&ifp->lock);
2082 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2083 struct in6_addr new_addr;
2084 struct inet6_ifaddr *ifp2;
2085 int retries = ifp->stable_privacy_retry + 1;
2086 struct ifa6_config cfg = {
2088 .plen = ifp->prefix_len,
2089 .ifa_flags = ifp->flags,
2090 .valid_lft = ifp->valid_lft,
2091 .preferred_lft = ifp->prefered_lft,
2092 .scope = ifp->scope,
2095 if (retries > net->ipv6.sysctl.idgen_retries) {
2096 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2097 ifp->idev->dev->name);
2101 new_addr = ifp->addr;
2102 if (ipv6_generate_stable_address(&new_addr, retries,
2106 spin_unlock_bh(&ifp->lock);
2108 if (idev->cnf.max_addresses &&
2109 ipv6_count_addresses(idev) >=
2110 idev->cnf.max_addresses)
2113 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2114 ifp->idev->dev->name);
2116 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2120 spin_lock_bh(&ifp2->lock);
2121 ifp2->stable_privacy_retry = retries;
2122 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2123 spin_unlock_bh(&ifp2->lock);
2125 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2128 spin_lock_bh(&ifp->lock);
2132 /* transition from _POSTDAD to _ERRDAD */
2133 ifp->state = INET6_IFADDR_STATE_ERRDAD;
2134 spin_unlock_bh(&ifp->lock);
2136 addrconf_mod_dad_work(ifp, 0);
2140 /* Join to solicited addr multicast group.
2141 * caller must hold RTNL */
2142 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2144 struct in6_addr maddr;
2146 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2149 addrconf_addr_solict_mult(addr, &maddr);
2150 ipv6_dev_mc_inc(dev, &maddr);
2153 /* caller must hold RTNL */
2154 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2156 struct in6_addr maddr;
2158 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2161 addrconf_addr_solict_mult(addr, &maddr);
2162 __ipv6_dev_mc_dec(idev, &maddr);
2165 /* caller must hold RTNL */
2166 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2168 struct in6_addr addr;
2170 if (ifp->prefix_len >= 127) /* RFC 6164 */
2172 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2173 if (ipv6_addr_any(&addr))
2175 __ipv6_dev_ac_inc(ifp->idev, &addr);
2178 /* caller must hold RTNL */
2179 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2181 struct in6_addr addr;
2183 if (ifp->prefix_len >= 127) /* RFC 6164 */
2185 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2186 if (ipv6_addr_any(&addr))
2188 __ipv6_dev_ac_dec(ifp->idev, &addr);
2191 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2193 switch (dev->addr_len) {
2195 memcpy(eui, dev->dev_addr, 3);
2198 memcpy(eui + 5, dev->dev_addr + 3, 3);
2200 case EUI64_ADDR_LEN:
2201 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2211 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2213 union fwnet_hwaddr *ha;
2215 if (dev->addr_len != FWNET_ALEN)
2218 ha = (union fwnet_hwaddr *)dev->dev_addr;
2220 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2225 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2227 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2228 if (dev->addr_len != ARCNET_ALEN)
2231 eui[7] = *(u8 *)dev->dev_addr;
2235 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2237 if (dev->addr_len != INFINIBAND_ALEN)
2239 memcpy(eui, dev->dev_addr + 12, 8);
2244 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2248 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2249 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2250 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2251 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2252 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2253 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2257 memcpy(eui + 4, &addr, 4);
2261 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2263 if (dev->priv_flags & IFF_ISATAP)
2264 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2268 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2270 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2273 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2275 memcpy(eui, dev->perm_addr, 3);
2276 memcpy(eui + 5, dev->perm_addr + 3, 3);
2283 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2285 switch (dev->type) {
2288 return addrconf_ifid_eui48(eui, dev);
2290 return addrconf_ifid_arcnet(eui, dev);
2291 case ARPHRD_INFINIBAND:
2292 return addrconf_ifid_infiniband(eui, dev);
2294 return addrconf_ifid_sit(eui, dev);
2297 return addrconf_ifid_gre(eui, dev);
2298 case ARPHRD_6LOWPAN:
2299 return addrconf_ifid_6lowpan(eui, dev);
2300 case ARPHRD_IEEE1394:
2301 return addrconf_ifid_ieee1394(eui, dev);
2302 case ARPHRD_TUNNEL6:
2305 return addrconf_ifid_ip6tnl(eui, dev);
2310 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2313 struct inet6_ifaddr *ifp;
2315 read_lock_bh(&idev->lock);
2316 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2317 if (ifp->scope > IFA_LINK)
2319 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2320 memcpy(eui, ifp->addr.s6_addr+8, 8);
2325 read_unlock_bh(&idev->lock);
2329 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2330 static void ipv6_regen_rndid(struct inet6_dev *idev)
2333 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2334 idev->rndid[0] &= ~0x02;
2337 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2338 * check if generated address is not inappropriate
2340 * - Reserved subnet anycast (RFC 2526)
2341 * 11111101 11....11 1xxxxxxx
2342 * - ISATAP (RFC4214) 6.1
2343 * 00-00-5E-FE-xx-xx-xx-xx
2345 * - XXX: already assigned to an address on the device
2347 if (idev->rndid[0] == 0xfd &&
2348 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2349 (idev->rndid[7]&0x80))
2351 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2352 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2354 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2359 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2361 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2362 ipv6_regen_rndid(idev);
2370 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2371 struct net_device *dev, unsigned long expires,
2372 u32 flags, gfp_t gfp_flags)
2374 struct fib6_config cfg = {
2375 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2376 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2377 .fc_ifindex = dev->ifindex,
2378 .fc_expires = expires,
2380 .fc_flags = RTF_UP | flags,
2381 .fc_nlinfo.nl_net = dev_net(dev),
2382 .fc_protocol = RTPROT_KERNEL,
2383 .fc_type = RTN_UNICAST,
2388 /* Prevent useless cloning on PtP SIT.
2389 This thing is done here expecting that the whole
2390 class of non-broadcast devices need not cloning.
2392 #if IS_ENABLED(CONFIG_IPV6_SIT)
2393 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2394 cfg.fc_flags |= RTF_NONEXTHOP;
2397 ip6_route_add(&cfg, gfp_flags, NULL);
2401 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2403 const struct net_device *dev,
2404 u32 flags, u32 noflags,
2407 struct fib6_node *fn;
2408 struct fib6_info *rt = NULL;
2409 struct fib6_table *table;
2410 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2412 table = fib6_get_table(dev_net(dev), tb_id);
2417 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2421 for_each_fib6_node_rt_rcu(fn) {
2422 if (rt->fib6_nh.fib_nh_dev->ifindex != dev->ifindex)
2424 if (no_gw && rt->fib6_nh.fib_nh_has_gw)
2426 if ((rt->fib6_flags & flags) != flags)
2428 if ((rt->fib6_flags & noflags) != 0)
2430 if (!fib6_info_hold_safe(rt))
2440 /* Create "default" multicast route to the interface */
2442 static void addrconf_add_mroute(struct net_device *dev)
2444 struct fib6_config cfg = {
2445 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2446 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2447 .fc_ifindex = dev->ifindex,
2450 .fc_type = RTN_UNICAST,
2451 .fc_nlinfo.nl_net = dev_net(dev),
2454 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2456 ip6_route_add(&cfg, GFP_KERNEL, NULL);
2459 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2461 struct inet6_dev *idev;
2465 idev = ipv6_find_idev(dev);
2467 return ERR_PTR(-ENOBUFS);
2469 if (idev->cnf.disable_ipv6)
2470 return ERR_PTR(-EACCES);
2472 /* Add default multicast route */
2473 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2474 addrconf_add_mroute(dev);
2479 static void manage_tempaddrs(struct inet6_dev *idev,
2480 struct inet6_ifaddr *ifp,
2481 __u32 valid_lft, __u32 prefered_lft,
2482 bool create, unsigned long now)
2485 struct inet6_ifaddr *ift;
2487 read_lock_bh(&idev->lock);
2488 /* update all temporary addresses in the list */
2489 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2490 int age, max_valid, max_prefered;
2492 if (ifp != ift->ifpub)
2495 /* RFC 4941 section 3.3:
2496 * If a received option will extend the lifetime of a public
2497 * address, the lifetimes of temporary addresses should
2498 * be extended, subject to the overall constraint that no
2499 * temporary addresses should ever remain "valid" or "preferred"
2500 * for a time longer than (TEMP_VALID_LIFETIME) or
2501 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2503 age = (now - ift->cstamp) / HZ;
2504 max_valid = idev->cnf.temp_valid_lft - age;
2508 max_prefered = idev->cnf.temp_prefered_lft -
2509 idev->desync_factor - age;
2510 if (max_prefered < 0)
2513 if (valid_lft > max_valid)
2514 valid_lft = max_valid;
2516 if (prefered_lft > max_prefered)
2517 prefered_lft = max_prefered;
2519 spin_lock(&ift->lock);
2521 ift->valid_lft = valid_lft;
2522 ift->prefered_lft = prefered_lft;
2524 if (prefered_lft > 0)
2525 ift->flags &= ~IFA_F_DEPRECATED;
2527 spin_unlock(&ift->lock);
2528 if (!(flags&IFA_F_TENTATIVE))
2529 ipv6_ifa_notify(0, ift);
2532 if ((create || list_empty(&idev->tempaddr_list)) &&
2533 idev->cnf.use_tempaddr > 0) {
2534 /* When a new public address is created as described
2535 * in [ADDRCONF], also create a new temporary address.
2536 * Also create a temporary address if it's enabled but
2537 * no temporary address currently exists.
2539 read_unlock_bh(&idev->lock);
2540 ipv6_create_tempaddr(ifp, NULL, false);
2542 read_unlock_bh(&idev->lock);
2546 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2548 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2549 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2552 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2553 const struct prefix_info *pinfo,
2554 struct inet6_dev *in6_dev,
2555 const struct in6_addr *addr, int addr_type,
2556 u32 addr_flags, bool sllao, bool tokenized,
2557 __u32 valid_lft, u32 prefered_lft)
2559 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2560 int create = 0, update_lft = 0;
2562 if (!ifp && valid_lft) {
2563 int max_addresses = in6_dev->cnf.max_addresses;
2564 struct ifa6_config cfg = {
2566 .plen = pinfo->prefix_len,
2567 .ifa_flags = addr_flags,
2568 .valid_lft = valid_lft,
2569 .preferred_lft = prefered_lft,
2570 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2573 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2574 if ((net->ipv6.devconf_all->optimistic_dad ||
2575 in6_dev->cnf.optimistic_dad) &&
2576 !net->ipv6.devconf_all->forwarding && sllao)
2577 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2580 /* Do not allow to create too much of autoconfigured
2581 * addresses; this would be too easy way to crash kernel.
2583 if (!max_addresses ||
2584 ipv6_count_addresses(in6_dev) < max_addresses)
2585 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2587 if (IS_ERR_OR_NULL(ifp))
2591 spin_lock_bh(&ifp->lock);
2592 ifp->flags |= IFA_F_MANAGETEMPADDR;
2593 ifp->cstamp = jiffies;
2594 ifp->tokenized = tokenized;
2595 spin_unlock_bh(&ifp->lock);
2596 addrconf_dad_start(ifp);
2604 /* update lifetime (RFC2462 5.5.3 e) */
2605 spin_lock_bh(&ifp->lock);
2607 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2608 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2611 if (!create && stored_lft) {
2612 const u32 minimum_lft = min_t(u32,
2613 stored_lft, MIN_VALID_LIFETIME);
2614 valid_lft = max(valid_lft, minimum_lft);
2616 /* RFC4862 Section 5.5.3e:
2617 * "Note that the preferred lifetime of the
2618 * corresponding address is always reset to
2619 * the Preferred Lifetime in the received
2620 * Prefix Information option, regardless of
2621 * whether the valid lifetime is also reset or
2624 * So we should always update prefered_lft here.
2630 ifp->valid_lft = valid_lft;
2631 ifp->prefered_lft = prefered_lft;
2634 ifp->flags &= ~IFA_F_DEPRECATED;
2635 spin_unlock_bh(&ifp->lock);
2637 if (!(flags&IFA_F_TENTATIVE))
2638 ipv6_ifa_notify(0, ifp);
2640 spin_unlock_bh(&ifp->lock);
2642 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2651 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2653 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2655 struct prefix_info *pinfo;
2660 struct inet6_dev *in6_dev;
2661 struct net *net = dev_net(dev);
2663 pinfo = (struct prefix_info *) opt;
2665 if (len < sizeof(struct prefix_info)) {
2666 netdev_dbg(dev, "addrconf: prefix option too short\n");
2671 * Validation checks ([ADDRCONF], page 19)
2674 addr_type = ipv6_addr_type(&pinfo->prefix);
2676 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2679 valid_lft = ntohl(pinfo->valid);
2680 prefered_lft = ntohl(pinfo->prefered);
2682 if (prefered_lft > valid_lft) {
2683 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2687 in6_dev = in6_dev_get(dev);
2690 net_dbg_ratelimited("addrconf: device %s not configured\n",
2696 * Two things going on here:
2697 * 1) Add routes for on-link prefixes
2698 * 2) Configure prefixes with the auto flag set
2701 if (pinfo->onlink) {
2702 struct fib6_info *rt;
2703 unsigned long rt_expires;
2705 /* Avoid arithmetic overflow. Really, we could
2706 * save rt_expires in seconds, likely valid_lft,
2707 * but it would require division in fib gc, that it
2711 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2713 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2715 if (addrconf_finite_timeout(rt_expires))
2718 rt = addrconf_get_prefix_route(&pinfo->prefix,
2721 RTF_ADDRCONF | RTF_PREFIX_RT,
2725 /* Autoconf prefix route */
2726 if (valid_lft == 0) {
2727 ip6_del_rt(net, rt);
2729 } else if (addrconf_finite_timeout(rt_expires)) {
2731 fib6_set_expires(rt, jiffies + rt_expires);
2733 fib6_clean_expires(rt);
2735 } else if (valid_lft) {
2736 clock_t expires = 0;
2737 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2738 if (addrconf_finite_timeout(rt_expires)) {
2740 flags |= RTF_EXPIRES;
2741 expires = jiffies_to_clock_t(rt_expires);
2743 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2744 0, dev, expires, flags,
2747 fib6_info_release(rt);
2750 /* Try to figure out our local address for this prefix */
2752 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2753 struct in6_addr addr;
2754 bool tokenized = false, dev_addr_generated = false;
2756 if (pinfo->prefix_len == 64) {
2757 memcpy(&addr, &pinfo->prefix, 8);
2759 if (!ipv6_addr_any(&in6_dev->token)) {
2760 read_lock_bh(&in6_dev->lock);
2761 memcpy(addr.s6_addr + 8,
2762 in6_dev->token.s6_addr + 8, 8);
2763 read_unlock_bh(&in6_dev->lock);
2765 } else if (is_addr_mode_generate_stable(in6_dev) &&
2766 !ipv6_generate_stable_address(&addr, 0,
2768 addr_flags |= IFA_F_STABLE_PRIVACY;
2770 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2771 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2774 dev_addr_generated = true;
2778 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2783 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2786 tokenized, valid_lft,
2791 /* Ignore error case here because previous prefix add addr was
2792 * successful which will be notified.
2794 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2795 addr_type, addr_flags, sllao,
2796 tokenized, valid_lft,
2798 dev_addr_generated);
2800 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2802 in6_dev_put(in6_dev);
2806 * Set destination address.
2807 * Special case for SIT interfaces where we create a new "virtual"
2810 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2812 struct in6_ifreq ireq;
2813 struct net_device *dev;
2819 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2822 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2828 #if IS_ENABLED(CONFIG_IPV6_SIT)
2829 if (dev->type == ARPHRD_SIT) {
2830 const struct net_device_ops *ops = dev->netdev_ops;
2832 struct ip_tunnel_parm p;
2834 err = -EADDRNOTAVAIL;
2835 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2838 memset(&p, 0, sizeof(p));
2839 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2843 p.iph.protocol = IPPROTO_IPV6;
2845 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2847 if (ops->ndo_do_ioctl) {
2848 mm_segment_t oldfs = get_fs();
2851 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2858 dev = __dev_get_by_name(net, p.name);
2861 err = dev_open(dev, NULL);
2871 static int ipv6_mc_config(struct sock *sk, bool join,
2872 const struct in6_addr *addr, int ifindex)
2880 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2882 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2889 * Manual configuration of address on an interface
2891 static int inet6_addr_add(struct net *net, int ifindex,
2892 struct ifa6_config *cfg,
2893 struct netlink_ext_ack *extack)
2895 struct inet6_ifaddr *ifp;
2896 struct inet6_dev *idev;
2897 struct net_device *dev;
2898 unsigned long timeout;
2904 if (cfg->plen > 128)
2907 /* check the lifetime */
2908 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
2911 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
2914 dev = __dev_get_by_index(net, ifindex);
2918 idev = addrconf_add_dev(dev);
2920 return PTR_ERR(idev);
2922 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2923 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2924 true, cfg->pfx, ifindex);
2930 cfg->scope = ipv6_addr_scope(cfg->pfx);
2932 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
2933 if (addrconf_finite_timeout(timeout)) {
2934 expires = jiffies_to_clock_t(timeout * HZ);
2935 cfg->valid_lft = timeout;
2936 flags = RTF_EXPIRES;
2940 cfg->ifa_flags |= IFA_F_PERMANENT;
2943 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
2944 if (addrconf_finite_timeout(timeout)) {
2946 cfg->ifa_flags |= IFA_F_DEPRECATED;
2947 cfg->preferred_lft = timeout;
2950 ifp = ipv6_add_addr(idev, cfg, true, extack);
2952 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
2953 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2954 ifp->rt_priority, dev, expires,
2958 /* Send a netlink notification if DAD is enabled and
2959 * optimistic flag is not set
2961 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2962 ipv6_ifa_notify(0, ifp);
2964 * Note that section 3.1 of RFC 4429 indicates
2965 * that the Optimistic flag should not be set for
2966 * manually configured addresses
2968 addrconf_dad_start(ifp);
2969 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
2970 manage_tempaddrs(idev, ifp, cfg->valid_lft,
2971 cfg->preferred_lft, true, jiffies);
2973 addrconf_verify_rtnl();
2975 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2976 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
2980 return PTR_ERR(ifp);
2983 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2984 const struct in6_addr *pfx, unsigned int plen)
2986 struct inet6_ifaddr *ifp;
2987 struct inet6_dev *idev;
2988 struct net_device *dev;
2993 dev = __dev_get_by_index(net, ifindex);
2997 idev = __in6_dev_get(dev);
3001 read_lock_bh(&idev->lock);
3002 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3003 if (ifp->prefix_len == plen &&
3004 ipv6_addr_equal(pfx, &ifp->addr)) {
3006 read_unlock_bh(&idev->lock);
3008 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3009 (ifa_flags & IFA_F_MANAGETEMPADDR))
3010 manage_tempaddrs(idev, ifp, 0, 0, false,
3013 addrconf_verify_rtnl();
3014 if (ipv6_addr_is_multicast(pfx)) {
3015 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3016 false, pfx, dev->ifindex);
3021 read_unlock_bh(&idev->lock);
3022 return -EADDRNOTAVAIL;
3026 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3028 struct ifa6_config cfg = {
3029 .ifa_flags = IFA_F_PERMANENT,
3030 .preferred_lft = INFINITY_LIFE_TIME,
3031 .valid_lft = INFINITY_LIFE_TIME,
3033 struct in6_ifreq ireq;
3036 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3039 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3042 cfg.pfx = &ireq.ifr6_addr;
3043 cfg.plen = ireq.ifr6_prefixlen;
3046 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
3051 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3053 struct in6_ifreq ireq;
3056 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3059 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3063 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3064 ireq.ifr6_prefixlen);
3069 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3070 int plen, int scope)
3072 struct inet6_ifaddr *ifp;
3073 struct ifa6_config cfg = {
3076 .ifa_flags = IFA_F_PERMANENT,
3077 .valid_lft = INFINITY_LIFE_TIME,
3078 .preferred_lft = INFINITY_LIFE_TIME,
3082 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3084 spin_lock_bh(&ifp->lock);
3085 ifp->flags &= ~IFA_F_TENTATIVE;
3086 spin_unlock_bh(&ifp->lock);
3087 rt_genid_bump_ipv6(dev_net(idev->dev));
3088 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3093 #if IS_ENABLED(CONFIG_IPV6_SIT)
3094 static void sit_add_v4_addrs(struct inet6_dev *idev)
3096 struct in6_addr addr;
3097 struct net_device *dev;
3098 struct net *net = dev_net(idev->dev);
3104 memset(&addr, 0, sizeof(struct in6_addr));
3105 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3107 if (idev->dev->flags&IFF_POINTOPOINT) {
3108 addr.s6_addr32[0] = htonl(0xfe800000);
3112 scope = IPV6_ADDR_COMPATv4;
3114 pflags |= RTF_NONEXTHOP;
3117 if (addr.s6_addr32[3]) {
3118 add_addr(idev, &addr, plen, scope);
3119 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3124 for_each_netdev(net, dev) {
3125 struct in_device *in_dev = __in_dev_get_rtnl(dev);
3126 if (in_dev && (dev->flags & IFF_UP)) {
3127 struct in_ifaddr *ifa;
3131 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
3133 addr.s6_addr32[3] = ifa->ifa_local;
3135 if (ifa->ifa_scope == RT_SCOPE_LINK)
3137 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3138 if (idev->dev->flags&IFF_POINTOPOINT)
3143 add_addr(idev, &addr, plen, flag);
3144 addrconf_prefix_route(&addr, plen, 0, idev->dev,
3145 0, pflags, GFP_KERNEL);
3152 static void init_loopback(struct net_device *dev)
3154 struct inet6_dev *idev;
3160 idev = ipv6_find_idev(dev);
3162 pr_debug("%s: add_dev failed\n", __func__);
3166 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
3169 void addrconf_add_linklocal(struct inet6_dev *idev,
3170 const struct in6_addr *addr, u32 flags)
3172 struct ifa6_config cfg = {
3175 .ifa_flags = flags | IFA_F_PERMANENT,
3176 .valid_lft = INFINITY_LIFE_TIME,
3177 .preferred_lft = INFINITY_LIFE_TIME,
3180 struct inet6_ifaddr *ifp;
3182 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3183 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3184 idev->cnf.optimistic_dad) &&
3185 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3186 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3189 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3191 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3193 addrconf_dad_start(ifp);
3197 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3199 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3201 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3204 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3205 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3208 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3209 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3215 static int ipv6_generate_stable_address(struct in6_addr *address,
3217 const struct inet6_dev *idev)
3219 static DEFINE_SPINLOCK(lock);
3220 static __u32 digest[SHA_DIGEST_WORDS];
3221 static __u32 workspace[SHA_WORKSPACE_WORDS];
3224 char __data[SHA_MESSAGE_BYTES];
3226 struct in6_addr secret;
3228 unsigned char hwaddr[MAX_ADDR_LEN];
3233 struct in6_addr secret;
3234 struct in6_addr temp;
3235 struct net *net = dev_net(idev->dev);
3237 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3239 if (idev->cnf.stable_secret.initialized)
3240 secret = idev->cnf.stable_secret.secret;
3241 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3242 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3247 spin_lock_bh(&lock);
3250 memset(&data, 0, sizeof(data));
3251 memset(workspace, 0, sizeof(workspace));
3252 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3253 data.prefix[0] = address->s6_addr32[0];
3254 data.prefix[1] = address->s6_addr32[1];
3255 data.secret = secret;
3256 data.dad_count = dad_count;
3258 sha_transform(digest, data.__data, workspace);
3261 temp.s6_addr32[2] = (__force __be32)digest[0];
3262 temp.s6_addr32[3] = (__force __be32)digest[1];
3264 spin_unlock_bh(&lock);
3266 if (ipv6_reserved_interfaceid(temp)) {
3268 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3277 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3279 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3283 s = &idev->cnf.stable_secret;
3284 get_random_bytes(&s->secret, sizeof(s->secret));
3285 s->initialized = true;
3288 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3290 struct in6_addr addr;
3292 /* no link local addresses on L3 master devices */
3293 if (netif_is_l3_master(idev->dev))
3296 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3298 switch (idev->cnf.addr_gen_mode) {
3299 case IN6_ADDR_GEN_MODE_RANDOM:
3300 ipv6_gen_mode_random_init(idev);
3302 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3303 if (!ipv6_generate_stable_address(&addr, 0, idev))
3304 addrconf_add_linklocal(idev, &addr,
3305 IFA_F_STABLE_PRIVACY);
3306 else if (prefix_route)
3307 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3310 case IN6_ADDR_GEN_MODE_EUI64:
3311 /* addrconf_add_linklocal also adds a prefix_route and we
3312 * only need to care about prefix routes if ipv6_generate_eui64
3313 * couldn't generate one.
3315 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3316 addrconf_add_linklocal(idev, &addr, 0);
3317 else if (prefix_route)
3318 addrconf_prefix_route(&addr, 64, 0, idev->dev,
3321 case IN6_ADDR_GEN_MODE_NONE:
3323 /* will not add any link local address */
3328 static void addrconf_dev_config(struct net_device *dev)
3330 struct inet6_dev *idev;
3334 if ((dev->type != ARPHRD_ETHER) &&
3335 (dev->type != ARPHRD_FDDI) &&
3336 (dev->type != ARPHRD_ARCNET) &&
3337 (dev->type != ARPHRD_INFINIBAND) &&
3338 (dev->type != ARPHRD_IEEE1394) &&
3339 (dev->type != ARPHRD_TUNNEL6) &&
3340 (dev->type != ARPHRD_6LOWPAN) &&
3341 (dev->type != ARPHRD_IP6GRE) &&
3342 (dev->type != ARPHRD_IPGRE) &&
3343 (dev->type != ARPHRD_TUNNEL) &&
3344 (dev->type != ARPHRD_NONE) &&
3345 (dev->type != ARPHRD_RAWIP)) {
3346 /* Alas, we support only Ethernet autoconfiguration. */
3350 idev = addrconf_add_dev(dev);
3354 /* this device type has no EUI support */
3355 if (dev->type == ARPHRD_NONE &&
3356 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3357 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3359 addrconf_addr_gen(idev, false);
3362 #if IS_ENABLED(CONFIG_IPV6_SIT)
3363 static void addrconf_sit_config(struct net_device *dev)
3365 struct inet6_dev *idev;
3370 * Configure the tunnel with one of our IPv4
3371 * addresses... we should configure all of
3372 * our v4 addrs in the tunnel
3375 idev = ipv6_find_idev(dev);
3377 pr_debug("%s: add_dev failed\n", __func__);
3381 if (dev->priv_flags & IFF_ISATAP) {
3382 addrconf_addr_gen(idev, false);
3386 sit_add_v4_addrs(idev);
3388 if (dev->flags&IFF_POINTOPOINT)
3389 addrconf_add_mroute(dev);
3393 #if IS_ENABLED(CONFIG_NET_IPGRE)
3394 static void addrconf_gre_config(struct net_device *dev)
3396 struct inet6_dev *idev;
3400 idev = ipv6_find_idev(dev);
3402 pr_debug("%s: add_dev failed\n", __func__);
3406 addrconf_addr_gen(idev, true);
3407 if (dev->flags & IFF_POINTOPOINT)
3408 addrconf_add_mroute(dev);
3412 static int fixup_permanent_addr(struct net *net,
3413 struct inet6_dev *idev,
3414 struct inet6_ifaddr *ifp)
3416 /* !fib6_node means the host route was removed from the
3417 * FIB, for example, if 'lo' device is taken down. In that
3418 * case regenerate the host route.
3420 if (!ifp->rt || !ifp->rt->fib6_node) {
3421 struct fib6_info *f6i, *prev;
3423 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3426 return PTR_ERR(f6i);
3428 /* ifp->rt can be accessed outside of rtnl */
3429 spin_lock(&ifp->lock);
3432 spin_unlock(&ifp->lock);
3434 fib6_info_release(prev);
3437 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3438 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3439 ifp->rt_priority, idev->dev, 0, 0,
3443 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3444 addrconf_dad_start(ifp);
3449 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3451 struct inet6_ifaddr *ifp, *tmp;
3452 struct inet6_dev *idev;
3454 idev = __in6_dev_get(dev);
3458 write_lock_bh(&idev->lock);
3460 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3461 if ((ifp->flags & IFA_F_PERMANENT) &&
3462 fixup_permanent_addr(net, idev, ifp) < 0) {
3463 write_unlock_bh(&idev->lock);
3466 write_lock_bh(&idev->lock);
3468 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3469 idev->dev->name, &ifp->addr);
3473 write_unlock_bh(&idev->lock);
3476 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3479 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3480 struct netdev_notifier_change_info *change_info;
3481 struct netdev_notifier_changeupper_info *info;
3482 struct inet6_dev *idev = __in6_dev_get(dev);
3483 struct net *net = dev_net(dev);
3484 int run_pending = 0;
3488 case NETDEV_REGISTER:
3489 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3490 idev = ipv6_add_dev(dev);
3492 return notifier_from_errno(PTR_ERR(idev));
3496 case NETDEV_CHANGEMTU:
3497 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3498 if (dev->mtu < IPV6_MIN_MTU) {
3499 addrconf_ifdown(dev, dev != net->loopback_dev);
3504 rt6_mtu_change(dev, dev->mtu);
3505 idev->cnf.mtu6 = dev->mtu;
3509 /* allocate new idev */
3510 idev = ipv6_add_dev(dev);
3514 /* device is still not ready */
3515 if (!(idev->if_flags & IF_READY))
3524 if (dev->flags & IFF_SLAVE)
3527 if (idev && idev->cnf.disable_ipv6)
3530 if (event == NETDEV_UP) {
3531 /* restore routes for permanent addresses */
3532 addrconf_permanent_addr(net, dev);
3534 if (!addrconf_link_ready(dev)) {
3535 /* device is not ready yet. */
3536 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3541 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3542 idev = ipv6_add_dev(dev);
3544 if (!IS_ERR_OR_NULL(idev)) {
3545 idev->if_flags |= IF_READY;
3548 } else if (event == NETDEV_CHANGE) {
3549 if (!addrconf_link_ready(dev)) {
3550 /* device is still not ready. */
3551 rt6_sync_down_dev(dev, event);
3555 if (!IS_ERR_OR_NULL(idev)) {
3556 if (idev->if_flags & IF_READY) {
3557 /* device is already configured -
3558 * but resend MLD reports, we might
3559 * have roamed and need to update
3560 * multicast snooping switches
3564 if (change_info->flags_changed & IFF_NOARP)
3565 addrconf_dad_run(idev, true);
3566 rt6_sync_up(dev, RTNH_F_LINKDOWN);
3569 idev->if_flags |= IF_READY;
3572 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3578 switch (dev->type) {
3579 #if IS_ENABLED(CONFIG_IPV6_SIT)
3581 addrconf_sit_config(dev);
3584 #if IS_ENABLED(CONFIG_NET_IPGRE)
3586 addrconf_gre_config(dev);
3589 case ARPHRD_LOOPBACK:
3594 addrconf_dev_config(dev);
3598 if (!IS_ERR_OR_NULL(idev)) {
3600 addrconf_dad_run(idev, false);
3602 /* Device has an address by now */
3603 rt6_sync_up(dev, RTNH_F_DEAD);
3606 * If the MTU changed during the interface down,
3607 * when the interface up, the changed MTU must be
3608 * reflected in the idev as well as routers.
3610 if (idev->cnf.mtu6 != dev->mtu &&
3611 dev->mtu >= IPV6_MIN_MTU) {
3612 rt6_mtu_change(dev, dev->mtu);
3613 idev->cnf.mtu6 = dev->mtu;
3615 idev->tstamp = jiffies;
3616 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3619 * If the changed mtu during down is lower than
3620 * IPV6_MIN_MTU stop IPv6 on this interface.
3622 if (dev->mtu < IPV6_MIN_MTU)
3623 addrconf_ifdown(dev, dev != net->loopback_dev);
3628 case NETDEV_UNREGISTER:
3630 * Remove all addresses from this interface.
3632 addrconf_ifdown(dev, event != NETDEV_DOWN);
3635 case NETDEV_CHANGENAME:
3637 snmp6_unregister_dev(idev);
3638 addrconf_sysctl_unregister(idev);
3639 err = addrconf_sysctl_register(idev);
3641 return notifier_from_errno(err);
3642 err = snmp6_register_dev(idev);
3644 addrconf_sysctl_unregister(idev);
3645 return notifier_from_errno(err);
3650 case NETDEV_PRE_TYPE_CHANGE:
3651 case NETDEV_POST_TYPE_CHANGE:
3653 addrconf_type_change(dev, event);
3656 case NETDEV_CHANGEUPPER:
3659 /* flush all routes if dev is linked to or unlinked from
3660 * an L3 master device (e.g., VRF)
3662 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3663 addrconf_ifdown(dev, 0);
3670 * addrconf module should be notified of a device going up
3672 static struct notifier_block ipv6_dev_notf = {
3673 .notifier_call = addrconf_notify,
3674 .priority = ADDRCONF_NOTIFY_PRIORITY,
3677 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3679 struct inet6_dev *idev;
3682 idev = __in6_dev_get(dev);
3684 if (event == NETDEV_POST_TYPE_CHANGE)
3685 ipv6_mc_remap(idev);
3686 else if (event == NETDEV_PRE_TYPE_CHANGE)
3687 ipv6_mc_unmap(idev);
3690 static bool addr_is_local(const struct in6_addr *addr)
3692 return ipv6_addr_type(addr) &
3693 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3696 static int addrconf_ifdown(struct net_device *dev, int how)
3698 unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
3699 struct net *net = dev_net(dev);
3700 struct inet6_dev *idev;
3701 struct inet6_ifaddr *ifa, *tmp;
3702 bool keep_addr = false;
3707 rt6_disable_ip(dev, event);
3709 idev = __in6_dev_get(dev);
3714 * Step 1: remove reference to ipv6 device from parent device.
3720 /* protected by rtnl_lock */
3721 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3723 /* Step 1.5: remove snmp6 entry */
3724 snmp6_unregister_dev(idev);
3728 /* combine the user config with event to determine if permanent
3729 * addresses are to be removed from address hash table
3731 if (!how && !idev->cnf.disable_ipv6) {
3732 /* aggregate the system setting and interface setting */
3733 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3736 _keep_addr = idev->cnf.keep_addr_on_down;
3738 keep_addr = (_keep_addr > 0);
3741 /* Step 2: clear hash table */
3742 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3743 struct hlist_head *h = &inet6_addr_lst[i];
3745 spin_lock_bh(&addrconf_hash_lock);
3747 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3748 if (ifa->idev == idev) {
3749 addrconf_del_dad_work(ifa);
3750 /* combined flag + permanent flag decide if
3751 * address is retained on a down event
3754 !(ifa->flags & IFA_F_PERMANENT) ||
3755 addr_is_local(&ifa->addr)) {
3756 hlist_del_init_rcu(&ifa->addr_lst);
3761 spin_unlock_bh(&addrconf_hash_lock);
3764 write_lock_bh(&idev->lock);
3766 addrconf_del_rs_timer(idev);
3768 /* Step 2: clear flags for stateless addrconf */
3770 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3772 /* Step 3: clear tempaddr list */
3773 while (!list_empty(&idev->tempaddr_list)) {
3774 ifa = list_first_entry(&idev->tempaddr_list,
3775 struct inet6_ifaddr, tmp_list);
3776 list_del(&ifa->tmp_list);
3777 write_unlock_bh(&idev->lock);
3778 spin_lock_bh(&ifa->lock);
3781 in6_ifa_put(ifa->ifpub);
3784 spin_unlock_bh(&ifa->lock);
3786 write_lock_bh(&idev->lock);
3789 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3790 struct fib6_info *rt = NULL;
3793 addrconf_del_dad_work(ifa);
3795 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3796 !addr_is_local(&ifa->addr);
3798 write_unlock_bh(&idev->lock);
3799 spin_lock_bh(&ifa->lock);
3802 /* set state to skip the notifier below */
3803 state = INET6_IFADDR_STATE_DEAD;
3804 ifa->state = INET6_IFADDR_STATE_PREDAD;
3805 if (!(ifa->flags & IFA_F_NODAD))
3806 ifa->flags |= IFA_F_TENTATIVE;
3812 ifa->state = INET6_IFADDR_STATE_DEAD;
3815 spin_unlock_bh(&ifa->lock);
3818 ip6_del_rt(net, rt);
3820 if (state != INET6_IFADDR_STATE_DEAD) {
3821 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3822 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3824 if (idev->cnf.forwarding)
3825 addrconf_leave_anycast(ifa);
3826 addrconf_leave_solict(ifa->idev, &ifa->addr);
3829 write_lock_bh(&idev->lock);
3831 list_del_rcu(&ifa->if_list);
3836 write_unlock_bh(&idev->lock);
3838 /* Step 5: Discard anycast and multicast list */
3840 ipv6_ac_destroy_dev(idev);
3841 ipv6_mc_destroy_dev(idev);
3846 idev->tstamp = jiffies;
3848 /* Last: Shot the device (if unregistered) */
3850 addrconf_sysctl_unregister(idev);
3851 neigh_parms_release(&nd_tbl, idev->nd_parms);
3852 neigh_ifdown(&nd_tbl, dev);
3858 static void addrconf_rs_timer(struct timer_list *t)
3860 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
3861 struct net_device *dev = idev->dev;
3862 struct in6_addr lladdr;
3864 write_lock(&idev->lock);
3865 if (idev->dead || !(idev->if_flags & IF_READY))
3868 if (!ipv6_accept_ra(idev))
3871 /* Announcement received after solicitation was sent */
3872 if (idev->if_flags & IF_RA_RCVD)
3875 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3876 write_unlock(&idev->lock);
3877 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3878 ndisc_send_rs(dev, &lladdr,
3879 &in6addr_linklocal_allrouters);
3883 write_lock(&idev->lock);
3884 idev->rs_interval = rfc3315_s14_backoff_update(
3885 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3886 /* The wait after the last probe can be shorter */
3887 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3888 idev->cnf.rtr_solicits) ?
3889 idev->cnf.rtr_solicit_delay :
3893 * Note: we do not support deprecated "all on-link"
3894 * assumption any longer.
3896 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3900 write_unlock(&idev->lock);
3906 * Duplicate Address Detection
3908 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3910 unsigned long rand_num;
3911 struct inet6_dev *idev = ifp->idev;
3914 if (ifp->flags & IFA_F_OPTIMISTIC)
3917 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3920 if (idev->cnf.enhanced_dad ||
3921 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3923 get_random_bytes(&nonce, 6);
3926 ifp->dad_nonce = nonce;
3927 ifp->dad_probes = idev->cnf.dad_transmits;
3928 addrconf_mod_dad_work(ifp, rand_num);
3931 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3933 struct inet6_dev *idev = ifp->idev;
3934 struct net_device *dev = idev->dev;
3935 bool bump_id, notify = false;
3938 addrconf_join_solict(dev, &ifp->addr);
3940 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3942 read_lock_bh(&idev->lock);
3943 spin_lock(&ifp->lock);
3944 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3948 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3949 (net->ipv6.devconf_all->accept_dad < 1 &&
3950 idev->cnf.accept_dad < 1) ||
3951 !(ifp->flags&IFA_F_TENTATIVE) ||
3952 ifp->flags & IFA_F_NODAD) {
3953 bool send_na = false;
3955 if (ifp->flags & IFA_F_TENTATIVE &&
3956 !(ifp->flags & IFA_F_OPTIMISTIC))
3958 bump_id = ifp->flags & IFA_F_TENTATIVE;
3959 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3960 spin_unlock(&ifp->lock);
3961 read_unlock_bh(&idev->lock);
3963 addrconf_dad_completed(ifp, bump_id, send_na);
3967 if (!(idev->if_flags & IF_READY)) {
3968 spin_unlock(&ifp->lock);
3969 read_unlock_bh(&idev->lock);
3971 * If the device is not ready:
3972 * - keep it tentative if it is a permanent address.
3973 * - otherwise, kill it.
3976 addrconf_dad_stop(ifp, 0);
3981 * Optimistic nodes can start receiving
3984 if (ifp->flags & IFA_F_OPTIMISTIC) {
3985 ip6_ins_rt(net, ifp->rt);
3986 if (ipv6_use_optimistic_addr(net, idev)) {
3987 /* Because optimistic nodes can use this address,
3988 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3994 addrconf_dad_kick(ifp);
3996 spin_unlock(&ifp->lock);
3997 read_unlock_bh(&idev->lock);
3999 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4002 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4004 bool begin_dad = false;
4006 spin_lock_bh(&ifp->lock);
4007 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4008 ifp->state = INET6_IFADDR_STATE_PREDAD;
4011 spin_unlock_bh(&ifp->lock);
4014 addrconf_mod_dad_work(ifp, 0);
4017 static void addrconf_dad_work(struct work_struct *w)
4019 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4020 struct inet6_ifaddr,
4022 struct inet6_dev *idev = ifp->idev;
4023 bool bump_id, disable_ipv6 = false;
4024 struct in6_addr mcaddr;
4030 } action = DAD_PROCESS;
4034 spin_lock_bh(&ifp->lock);
4035 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4037 ifp->state = INET6_IFADDR_STATE_DAD;
4038 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4040 ifp->state = INET6_IFADDR_STATE_POSTDAD;
4042 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4043 idev->cnf.accept_dad > 1) &&
4044 !idev->cnf.disable_ipv6 &&
4045 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4046 struct in6_addr addr;
4048 addr.s6_addr32[0] = htonl(0xfe800000);
4049 addr.s6_addr32[1] = 0;
4051 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4052 ipv6_addr_equal(&ifp->addr, &addr)) {
4053 /* DAD failed for link-local based on MAC */
4054 idev->cnf.disable_ipv6 = 1;
4056 pr_info("%s: IPv6 being disabled!\n",
4057 ifp->idev->dev->name);
4058 disable_ipv6 = true;
4062 spin_unlock_bh(&ifp->lock);
4064 if (action == DAD_BEGIN) {
4065 addrconf_dad_begin(ifp);
4067 } else if (action == DAD_ABORT) {
4069 addrconf_dad_stop(ifp, 1);
4071 addrconf_ifdown(idev->dev, 0);
4075 if (!ifp->dad_probes && addrconf_dad_end(ifp))
4078 write_lock_bh(&idev->lock);
4079 if (idev->dead || !(idev->if_flags & IF_READY)) {
4080 write_unlock_bh(&idev->lock);
4084 spin_lock(&ifp->lock);
4085 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4086 spin_unlock(&ifp->lock);
4087 write_unlock_bh(&idev->lock);
4091 if (ifp->dad_probes == 0) {
4092 bool send_na = false;
4095 * DAD was successful
4098 if (ifp->flags & IFA_F_TENTATIVE &&
4099 !(ifp->flags & IFA_F_OPTIMISTIC))
4101 bump_id = ifp->flags & IFA_F_TENTATIVE;
4102 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4103 spin_unlock(&ifp->lock);
4104 write_unlock_bh(&idev->lock);
4106 addrconf_dad_completed(ifp, bump_id, send_na);
4112 addrconf_mod_dad_work(ifp,
4113 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
4114 spin_unlock(&ifp->lock);
4115 write_unlock_bh(&idev->lock);
4117 /* send a neighbour solicitation for our addr */
4118 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4119 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4126 /* ifp->idev must be at least read locked */
4127 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4129 struct inet6_ifaddr *ifpiter;
4130 struct inet6_dev *idev = ifp->idev;
4132 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4133 if (ifpiter->scope > IFA_LINK)
4135 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4136 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4137 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4144 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4147 struct net_device *dev = ifp->idev->dev;
4148 struct in6_addr lladdr;
4149 bool send_rs, send_mld;
4151 addrconf_del_dad_work(ifp);
4154 * Configure the address for reception. Now it is valid.
4157 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4159 /* If added prefix is link local and we are prepared to process
4160 router advertisements, start sending router solicitations.
4163 read_lock_bh(&ifp->idev->lock);
4164 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4165 send_rs = send_mld &&
4166 ipv6_accept_ra(ifp->idev) &&
4167 ifp->idev->cnf.rtr_solicits != 0 &&
4168 (dev->flags&IFF_LOOPBACK) == 0;
4169 read_unlock_bh(&ifp->idev->lock);
4171 /* While dad is in progress mld report's source address is in6_addrany.
4172 * Resend with proper ll now.
4175 ipv6_mc_dad_complete(ifp->idev);
4177 /* send unsolicited NA if enabled */
4179 (ifp->idev->cnf.ndisc_notify ||
4180 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4181 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4182 /*router=*/ !!ifp->idev->cnf.forwarding,
4183 /*solicited=*/ false, /*override=*/ true,
4189 * If a host as already performed a random delay
4190 * [...] as part of DAD [...] there is no need
4191 * to delay again before sending the first RS
4193 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4195 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4197 write_lock_bh(&ifp->idev->lock);
4198 spin_lock(&ifp->lock);
4199 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4200 ifp->idev->cnf.rtr_solicit_interval);
4201 ifp->idev->rs_probes = 1;
4202 ifp->idev->if_flags |= IF_RS_SENT;
4203 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4204 spin_unlock(&ifp->lock);
4205 write_unlock_bh(&ifp->idev->lock);
4209 rt_genid_bump_ipv6(dev_net(dev));
4211 /* Make sure that a new temporary address will be created
4212 * before this temporary address becomes deprecated.
4214 if (ifp->flags & IFA_F_TEMPORARY)
4215 addrconf_verify_rtnl();
4218 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4220 struct inet6_ifaddr *ifp;
4222 read_lock_bh(&idev->lock);
4223 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4224 spin_lock(&ifp->lock);
4225 if ((ifp->flags & IFA_F_TENTATIVE &&
4226 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4228 ifp->state = INET6_IFADDR_STATE_PREDAD;
4229 addrconf_dad_kick(ifp);
4231 spin_unlock(&ifp->lock);
4233 read_unlock_bh(&idev->lock);
4236 #ifdef CONFIG_PROC_FS
4237 struct if6_iter_state {
4238 struct seq_net_private p;
4243 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4245 struct if6_iter_state *state = seq->private;
4246 struct net *net = seq_file_net(seq);
4247 struct inet6_ifaddr *ifa = NULL;
4250 /* initial bucket if pos is 0 */
4256 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4257 hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
4259 if (!net_eq(dev_net(ifa->idev->dev), net))
4261 /* sync with offset */
4262 if (p < state->offset) {
4269 /* prepare for next bucket */
4276 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4277 struct inet6_ifaddr *ifa)
4279 struct if6_iter_state *state = seq->private;
4280 struct net *net = seq_file_net(seq);
4282 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4283 if (!net_eq(dev_net(ifa->idev->dev), net))
4290 while (++state->bucket < IN6_ADDR_HSIZE) {
4291 hlist_for_each_entry_rcu(ifa,
4292 &inet6_addr_lst[state->bucket], addr_lst) {
4293 if (!net_eq(dev_net(ifa->idev->dev), net))
4302 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4306 return if6_get_first(seq, *pos);
4309 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4311 struct inet6_ifaddr *ifa;
4313 ifa = if6_get_next(seq, v);
4318 static void if6_seq_stop(struct seq_file *seq, void *v)
4324 static int if6_seq_show(struct seq_file *seq, void *v)
4326 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4327 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4329 ifp->idev->dev->ifindex,
4333 ifp->idev->dev->name);
4337 static const struct seq_operations if6_seq_ops = {
4338 .start = if6_seq_start,
4339 .next = if6_seq_next,
4340 .show = if6_seq_show,
4341 .stop = if6_seq_stop,
4344 static int __net_init if6_proc_net_init(struct net *net)
4346 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4347 sizeof(struct if6_iter_state)))
4352 static void __net_exit if6_proc_net_exit(struct net *net)
4354 remove_proc_entry("if_inet6", net->proc_net);
4357 static struct pernet_operations if6_proc_net_ops = {
4358 .init = if6_proc_net_init,
4359 .exit = if6_proc_net_exit,
4362 int __init if6_proc_init(void)
4364 return register_pernet_subsys(&if6_proc_net_ops);
4367 void if6_proc_exit(void)
4369 unregister_pernet_subsys(&if6_proc_net_ops);
4371 #endif /* CONFIG_PROC_FS */
4373 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4374 /* Check if address is a home address configured on any interface. */
4375 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4377 unsigned int hash = inet6_addr_hash(net, addr);
4378 struct inet6_ifaddr *ifp = NULL;
4382 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
4383 if (!net_eq(dev_net(ifp->idev->dev), net))
4385 if (ipv6_addr_equal(&ifp->addr, addr) &&
4386 (ifp->flags & IFA_F_HOMEADDRESS)) {
4397 * Periodic address status verification
4400 static void addrconf_verify_rtnl(void)
4402 unsigned long now, next, next_sec, next_sched;
4403 struct inet6_ifaddr *ifp;
4410 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4412 cancel_delayed_work(&addr_chk_work);
4414 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4416 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4419 /* When setting preferred_lft to a value not zero or
4420 * infinity, while valid_lft is infinity
4421 * IFA_F_PERMANENT has a non-infinity life time.
4423 if ((ifp->flags & IFA_F_PERMANENT) &&
4424 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4427 spin_lock(&ifp->lock);
4428 /* We try to batch several events at once. */
4429 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4431 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4432 age >= ifp->valid_lft) {
4433 spin_unlock(&ifp->lock);
4437 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4438 spin_unlock(&ifp->lock);
4440 } else if (age >= ifp->prefered_lft) {
4441 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4444 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4446 ifp->flags |= IFA_F_DEPRECATED;
4449 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4450 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4451 next = ifp->tstamp + ifp->valid_lft * HZ;
4453 spin_unlock(&ifp->lock);
4458 ipv6_ifa_notify(0, ifp);
4462 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4463 !(ifp->flags&IFA_F_TENTATIVE)) {
4464 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4465 ifp->idev->cnf.dad_transmits *
4466 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4468 if (age >= ifp->prefered_lft - regen_advance) {
4469 struct inet6_ifaddr *ifpub = ifp->ifpub;
4470 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4471 next = ifp->tstamp + ifp->prefered_lft * HZ;
4472 if (!ifp->regen_count && ifpub) {
4475 in6_ifa_hold(ifpub);
4476 spin_unlock(&ifp->lock);
4478 spin_lock(&ifpub->lock);
4479 ifpub->regen_count = 0;
4480 spin_unlock(&ifpub->lock);
4481 rcu_read_unlock_bh();
4482 ipv6_create_tempaddr(ifpub, ifp, true);
4488 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4489 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4490 spin_unlock(&ifp->lock);
4492 /* ifp->prefered_lft <= ifp->valid_lft */
4493 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4494 next = ifp->tstamp + ifp->prefered_lft * HZ;
4495 spin_unlock(&ifp->lock);
4500 next_sec = round_jiffies_up(next);
4503 /* If rounded timeout is accurate enough, accept it. */
4504 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4505 next_sched = next_sec;
4507 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4508 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4509 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4511 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4512 now, next, next_sec, next_sched);
4513 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4514 rcu_read_unlock_bh();
4517 static void addrconf_verify_work(struct work_struct *w)
4520 addrconf_verify_rtnl();
4524 static void addrconf_verify(void)
4526 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4529 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4530 struct in6_addr **peer_pfx)
4532 struct in6_addr *pfx = NULL;
4537 pfx = nla_data(addr);
4540 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4542 pfx = nla_data(local);
4548 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4549 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4550 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4551 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4552 [IFA_FLAGS] = { .len = sizeof(u32) },
4553 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
4554 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
4558 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4559 struct netlink_ext_ack *extack)
4561 struct net *net = sock_net(skb->sk);
4562 struct ifaddrmsg *ifm;
4563 struct nlattr *tb[IFA_MAX+1];
4564 struct in6_addr *pfx, *peer_pfx;
4568 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4573 ifm = nlmsg_data(nlh);
4574 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4578 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4580 /* We ignore other flags so far. */
4581 ifa_flags &= IFA_F_MANAGETEMPADDR;
4583 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4584 ifm->ifa_prefixlen);
4587 static int modify_prefix_route(struct inet6_ifaddr *ifp,
4588 unsigned long expires, u32 flags)
4590 struct fib6_info *f6i;
4593 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
4594 ifp->idev->dev, 0, RTF_DEFAULT, true);
4598 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4599 if (f6i->fib6_metric != prio) {
4600 /* delete old one */
4601 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
4604 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4605 ifp->rt_priority, ifp->idev->dev,
4606 expires, flags, GFP_KERNEL);
4609 fib6_clean_expires(f6i);
4611 fib6_set_expires(f6i, expires);
4613 fib6_info_release(f6i);
4619 static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
4623 unsigned long timeout;
4624 bool was_managetempaddr;
4625 bool had_prefixroute;
4629 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
4632 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4633 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4636 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4637 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4639 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
4640 if (addrconf_finite_timeout(timeout)) {
4641 expires = jiffies_to_clock_t(timeout * HZ);
4642 cfg->valid_lft = timeout;
4643 flags = RTF_EXPIRES;
4647 cfg->ifa_flags |= IFA_F_PERMANENT;
4650 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
4651 if (addrconf_finite_timeout(timeout)) {
4653 cfg->ifa_flags |= IFA_F_DEPRECATED;
4654 cfg->preferred_lft = timeout;
4657 spin_lock_bh(&ifp->lock);
4658 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4659 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4660 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4661 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4662 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4663 IFA_F_NOPREFIXROUTE);
4664 ifp->flags |= cfg->ifa_flags;
4665 ifp->tstamp = jiffies;
4666 ifp->valid_lft = cfg->valid_lft;
4667 ifp->prefered_lft = cfg->preferred_lft;
4669 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4670 ifp->rt_priority = cfg->rt_priority;
4672 spin_unlock_bh(&ifp->lock);
4673 if (!(ifp->flags&IFA_F_TENTATIVE))
4674 ipv6_ifa_notify(0, ifp);
4676 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4679 if (had_prefixroute)
4680 rc = modify_prefix_route(ifp, expires, flags);
4682 /* prefix route could have been deleted; if so restore it */
4683 if (rc == -ENOENT) {
4684 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4685 ifp->rt_priority, ifp->idev->dev,
4686 expires, flags, GFP_KERNEL);
4688 } else if (had_prefixroute) {
4689 enum cleanup_prefix_rt_t action;
4690 unsigned long rt_expires;
4692 write_lock_bh(&ifp->idev->lock);
4693 action = check_cleanup_prefix_route(ifp, &rt_expires);
4694 write_unlock_bh(&ifp->idev->lock);
4696 if (action != CLEANUP_PREFIX_RT_NOP) {
4697 cleanup_prefix_route(ifp, rt_expires,
4698 action == CLEANUP_PREFIX_RT_DEL);
4702 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4703 if (was_managetempaddr &&
4704 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4706 cfg->preferred_lft = 0;
4708 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4709 cfg->preferred_lft, !was_managetempaddr,
4713 addrconf_verify_rtnl();
4719 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4720 struct netlink_ext_ack *extack)
4722 struct net *net = sock_net(skb->sk);
4723 struct ifaddrmsg *ifm;
4724 struct nlattr *tb[IFA_MAX+1];
4725 struct in6_addr *peer_pfx;
4726 struct inet6_ifaddr *ifa;
4727 struct net_device *dev;
4728 struct inet6_dev *idev;
4729 struct ifa6_config cfg;
4732 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4737 memset(&cfg, 0, sizeof(cfg));
4739 ifm = nlmsg_data(nlh);
4740 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4744 cfg.peer_pfx = peer_pfx;
4745 cfg.plen = ifm->ifa_prefixlen;
4746 if (tb[IFA_RT_PRIORITY])
4747 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4749 cfg.valid_lft = INFINITY_LIFE_TIME;
4750 cfg.preferred_lft = INFINITY_LIFE_TIME;
4752 if (tb[IFA_CACHEINFO]) {
4753 struct ifa_cacheinfo *ci;
4755 ci = nla_data(tb[IFA_CACHEINFO]);
4756 cfg.valid_lft = ci->ifa_valid;
4757 cfg.preferred_lft = ci->ifa_prefered;
4760 dev = __dev_get_by_index(net, ifm->ifa_index);
4765 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4767 cfg.ifa_flags = ifm->ifa_flags;
4769 /* We ignore other flags so far. */
4770 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4771 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4772 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
4774 idev = ipv6_find_idev(dev);
4778 if (!ipv6_allow_optimistic_dad(net, idev))
4779 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
4781 if (cfg.ifa_flags & IFA_F_NODAD &&
4782 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
4783 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4787 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
4790 * It would be best to check for !NLM_F_CREATE here but
4791 * userspace already relies on not having to provide this.
4793 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
4796 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4797 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4800 err = inet6_addr_modify(ifa, &cfg);
4807 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4808 u8 scope, int ifindex)
4810 struct ifaddrmsg *ifm;
4812 ifm = nlmsg_data(nlh);
4813 ifm->ifa_family = AF_INET6;
4814 ifm->ifa_prefixlen = prefixlen;
4815 ifm->ifa_flags = flags;
4816 ifm->ifa_scope = scope;
4817 ifm->ifa_index = ifindex;
4820 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4821 unsigned long tstamp, u32 preferred, u32 valid)
4823 struct ifa_cacheinfo ci;
4825 ci.cstamp = cstamp_delta(cstamp);
4826 ci.tstamp = cstamp_delta(tstamp);
4827 ci.ifa_prefered = preferred;
4828 ci.ifa_valid = valid;
4830 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4833 static inline int rt_scope(int ifa_scope)
4835 if (ifa_scope & IFA_HOST)
4836 return RT_SCOPE_HOST;
4837 else if (ifa_scope & IFA_LINK)
4838 return RT_SCOPE_LINK;
4839 else if (ifa_scope & IFA_SITE)
4840 return RT_SCOPE_SITE;
4842 return RT_SCOPE_UNIVERSE;
4845 static inline int inet6_ifaddr_msgsize(void)
4847 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4848 + nla_total_size(16) /* IFA_LOCAL */
4849 + nla_total_size(16) /* IFA_ADDRESS */
4850 + nla_total_size(sizeof(struct ifa_cacheinfo))
4851 + nla_total_size(4) /* IFA_FLAGS */
4852 + nla_total_size(4) /* IFA_RT_PRIORITY */;
4861 struct inet6_fill_args {
4868 enum addr_type_t type;
4871 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4872 struct inet6_fill_args *args)
4874 struct nlmsghdr *nlh;
4875 u32 preferred, valid;
4877 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4878 sizeof(struct ifaddrmsg), args->flags);
4882 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4883 ifa->idev->dev->ifindex);
4885 if (args->netnsid >= 0 &&
4886 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
4889 if (!((ifa->flags&IFA_F_PERMANENT) &&
4890 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4891 preferred = ifa->prefered_lft;
4892 valid = ifa->valid_lft;
4893 if (preferred != INFINITY_LIFE_TIME) {
4894 long tval = (jiffies - ifa->tstamp)/HZ;
4895 if (preferred > tval)
4899 if (valid != INFINITY_LIFE_TIME) {
4907 preferred = INFINITY_LIFE_TIME;
4908 valid = INFINITY_LIFE_TIME;
4911 if (!ipv6_addr_any(&ifa->peer_addr)) {
4912 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4913 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4916 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4919 if (ifa->rt_priority &&
4920 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
4923 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4926 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4929 nlmsg_end(skb, nlh);
4933 nlmsg_cancel(skb, nlh);
4937 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4938 struct inet6_fill_args *args)
4940 struct nlmsghdr *nlh;
4941 u8 scope = RT_SCOPE_UNIVERSE;
4942 int ifindex = ifmca->idev->dev->ifindex;
4944 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4945 scope = RT_SCOPE_SITE;
4947 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4948 sizeof(struct ifaddrmsg), args->flags);
4952 if (args->netnsid >= 0 &&
4953 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
4956 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4957 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4958 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4959 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4960 nlmsg_cancel(skb, nlh);
4964 nlmsg_end(skb, nlh);
4968 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4969 struct inet6_fill_args *args)
4971 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
4972 int ifindex = dev ? dev->ifindex : 1;
4973 struct nlmsghdr *nlh;
4974 u8 scope = RT_SCOPE_UNIVERSE;
4976 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4977 scope = RT_SCOPE_SITE;
4979 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4980 sizeof(struct ifaddrmsg), args->flags);
4984 if (args->netnsid >= 0 &&
4985 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
4988 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4989 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4990 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4991 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4992 nlmsg_cancel(skb, nlh);
4996 nlmsg_end(skb, nlh);
5000 /* called with rcu_read_lock() */
5001 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
5002 struct netlink_callback *cb, int s_ip_idx,
5003 struct inet6_fill_args *fillargs)
5005 struct ifmcaddr6 *ifmca;
5006 struct ifacaddr6 *ifaca;
5010 read_lock_bh(&idev->lock);
5011 switch (fillargs->type) {
5012 case UNICAST_ADDR: {
5013 struct inet6_ifaddr *ifa;
5014 fillargs->event = RTM_NEWADDR;
5016 /* unicast address incl. temp addr */
5017 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5018 if (ip_idx < s_ip_idx)
5020 err = inet6_fill_ifaddr(skb, ifa, fillargs);
5023 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5029 case MULTICAST_ADDR:
5030 fillargs->event = RTM_GETMULTICAST;
5032 /* multicast address */
5033 for (ifmca = idev->mc_list; ifmca;
5034 ifmca = ifmca->next, ip_idx++) {
5035 if (ip_idx < s_ip_idx)
5037 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5043 fillargs->event = RTM_GETANYCAST;
5044 /* anycast address */
5045 for (ifaca = idev->ac_list; ifaca;
5046 ifaca = ifaca->aca_next, ip_idx++) {
5047 if (ip_idx < s_ip_idx)
5049 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5057 read_unlock_bh(&idev->lock);
5058 cb->args[2] = ip_idx;
5062 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5063 struct inet6_fill_args *fillargs,
5064 struct net **tgt_net, struct sock *sk,
5065 struct netlink_callback *cb)
5067 struct netlink_ext_ack *extack = cb->extack;
5068 struct nlattr *tb[IFA_MAX+1];
5069 struct ifaddrmsg *ifm;
5072 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5073 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5077 ifm = nlmsg_data(nlh);
5078 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5079 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5083 fillargs->ifindex = ifm->ifa_index;
5084 if (fillargs->ifindex) {
5085 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5086 fillargs->flags |= NLM_F_DUMP_FILTERED;
5089 err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5090 ifa_ipv6_policy, extack);
5094 for (i = 0; i <= IFA_MAX; ++i) {
5098 if (i == IFA_TARGET_NETNSID) {
5101 fillargs->netnsid = nla_get_s32(tb[i]);
5102 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5104 fillargs->netnsid = -1;
5105 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5106 return PTR_ERR(net);
5110 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5118 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5119 enum addr_type_t type)
5121 const struct nlmsghdr *nlh = cb->nlh;
5122 struct inet6_fill_args fillargs = {
5123 .portid = NETLINK_CB(cb->skb).portid,
5124 .seq = cb->nlh->nlmsg_seq,
5125 .flags = NLM_F_MULTI,
5129 struct net *net = sock_net(skb->sk);
5130 struct net *tgt_net = net;
5131 int idx, s_idx, s_ip_idx;
5133 struct net_device *dev;
5134 struct inet6_dev *idev;
5135 struct hlist_head *head;
5139 s_idx = idx = cb->args[1];
5140 s_ip_idx = cb->args[2];
5142 if (cb->strict_check) {
5143 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5149 if (fillargs.ifindex) {
5150 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
5155 idev = __in6_dev_get(dev);
5157 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5167 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
5168 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5170 head = &tgt_net->dev_index_head[h];
5171 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5174 if (h > s_h || idx > s_idx)
5176 idev = __in6_dev_get(dev);
5180 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
5192 if (fillargs.netnsid >= 0)
5195 return skb->len ? : err;
5198 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5200 enum addr_type_t type = UNICAST_ADDR;
5202 return inet6_dump_addr(skb, cb, type);
5205 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5207 enum addr_type_t type = MULTICAST_ADDR;
5209 return inet6_dump_addr(skb, cb, type);
5213 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5215 enum addr_type_t type = ANYCAST_ADDR;
5217 return inet6_dump_addr(skb, cb, type);
5220 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5221 const struct nlmsghdr *nlh,
5223 struct netlink_ext_ack *extack)
5225 struct ifaddrmsg *ifm;
5228 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5229 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5233 ifm = nlmsg_data(nlh);
5234 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5235 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5239 if (!netlink_strict_get_check(skb))
5240 return nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX,
5241 ifa_ipv6_policy, extack);
5243 err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5244 ifa_ipv6_policy, extack);
5248 for (i = 0; i <= IFA_MAX; i++) {
5253 case IFA_TARGET_NETNSID:
5258 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5266 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5267 struct netlink_ext_ack *extack)
5269 struct net *net = sock_net(in_skb->sk);
5270 struct inet6_fill_args fillargs = {
5271 .portid = NETLINK_CB(in_skb).portid,
5272 .seq = nlh->nlmsg_seq,
5273 .event = RTM_NEWADDR,
5277 struct net *tgt_net = net;
5278 struct ifaddrmsg *ifm;
5279 struct nlattr *tb[IFA_MAX+1];
5280 struct in6_addr *addr = NULL, *peer;
5281 struct net_device *dev = NULL;
5282 struct inet6_ifaddr *ifa;
5283 struct sk_buff *skb;
5286 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5290 if (tb[IFA_TARGET_NETNSID]) {
5291 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5293 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5295 if (IS_ERR(tgt_net))
5296 return PTR_ERR(tgt_net);
5299 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5303 ifm = nlmsg_data(nlh);
5305 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5307 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5309 err = -EADDRNOTAVAIL;
5313 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5319 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5321 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5322 WARN_ON(err == -EMSGSIZE);
5326 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5332 if (fillargs.netnsid >= 0)
5338 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5340 struct sk_buff *skb;
5341 struct net *net = dev_net(ifa->idev->dev);
5342 struct inet6_fill_args fillargs = {
5351 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5355 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5357 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5358 WARN_ON(err == -EMSGSIZE);
5362 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5366 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5369 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
5370 __s32 *array, int bytes)
5372 BUG_ON(bytes < (DEVCONF_MAX * 4));
5374 memset(array, 0, bytes);
5375 array[DEVCONF_FORWARDING] = cnf->forwarding;
5376 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5377 array[DEVCONF_MTU6] = cnf->mtu6;
5378 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5379 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5380 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5381 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5382 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
5383 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5384 jiffies_to_msecs(cnf->rtr_solicit_interval);
5385 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5386 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
5387 array[DEVCONF_RTR_SOLICIT_DELAY] =
5388 jiffies_to_msecs(cnf->rtr_solicit_delay);
5389 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
5390 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5391 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5392 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5393 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
5394 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5395 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5396 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5397 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5398 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
5399 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
5400 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
5401 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5402 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5403 #ifdef CONFIG_IPV6_ROUTER_PREF
5404 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
5405 array[DEVCONF_RTR_PROBE_INTERVAL] =
5406 jiffies_to_msecs(cnf->rtr_probe_interval);
5407 #ifdef CONFIG_IPV6_ROUTE_INFO
5408 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
5409 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5412 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
5413 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
5414 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5415 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
5416 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
5418 #ifdef CONFIG_IPV6_MROUTE
5419 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5421 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
5422 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5423 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
5424 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5425 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5426 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5427 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
5428 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
5429 /* we omit DEVCONF_STABLE_SECRET for now */
5430 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5431 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
5432 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5433 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5434 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5435 #ifdef CONFIG_IPV6_SEG6_HMAC
5436 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5438 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5439 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5440 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
5441 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
5444 static inline size_t inet6_ifla6_size(void)
5446 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5447 + nla_total_size(sizeof(struct ifla_cacheinfo))
5448 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5449 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5450 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5451 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5452 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5456 static inline size_t inet6_if_nlmsg_size(void)
5458 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5459 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5460 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5461 + nla_total_size(4) /* IFLA_MTU */
5462 + nla_total_size(4) /* IFLA_LINK */
5463 + nla_total_size(1) /* IFLA_OPERSTATE */
5464 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5467 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5471 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5474 /* Use put_unaligned() because stats may not be aligned for u64. */
5475 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5476 for (i = 1; i < ICMP6_MIB_MAX; i++)
5477 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5479 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5482 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5483 int bytes, size_t syncpoff)
5486 u64 buff[IPSTATS_MIB_MAX];
5487 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5491 memset(buff, 0, sizeof(buff));
5492 buff[0] = IPSTATS_MIB_MAX;
5494 for_each_possible_cpu(c) {
5495 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5496 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5499 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5500 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5503 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5507 case IFLA_INET6_STATS:
5508 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5509 offsetof(struct ipstats_mib, syncp));
5511 case IFLA_INET6_ICMP6STATS:
5512 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5517 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5518 u32 ext_filter_mask)
5521 struct ifla_cacheinfo ci;
5523 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5524 goto nla_put_failure;
5525 ci.max_reasm_len = IPV6_MAXPLEN;
5526 ci.tstamp = cstamp_delta(idev->tstamp);
5527 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5528 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5529 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5530 goto nla_put_failure;
5531 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5533 goto nla_put_failure;
5534 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5536 /* XXX - MC not implemented */
5538 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5541 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5543 goto nla_put_failure;
5544 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5546 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5548 goto nla_put_failure;
5549 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5551 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5553 goto nla_put_failure;
5555 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5556 goto nla_put_failure;
5558 read_lock_bh(&idev->lock);
5559 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5560 read_unlock_bh(&idev->lock);
5568 static size_t inet6_get_link_af_size(const struct net_device *dev,
5569 u32 ext_filter_mask)
5571 if (!__in6_dev_get(dev))
5574 return inet6_ifla6_size();
5577 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5578 u32 ext_filter_mask)
5580 struct inet6_dev *idev = __in6_dev_get(dev);
5585 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5591 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5593 struct inet6_ifaddr *ifp;
5594 struct net_device *dev = idev->dev;
5595 bool clear_token, update_rs = false;
5596 struct in6_addr ll_addr;
5602 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5604 if (!ipv6_accept_ra(idev))
5606 if (idev->cnf.rtr_solicits == 0)
5609 write_lock_bh(&idev->lock);
5611 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5612 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5614 write_unlock_bh(&idev->lock);
5616 clear_token = ipv6_addr_any(token);
5620 if (!idev->dead && (idev->if_flags & IF_READY) &&
5621 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5622 IFA_F_OPTIMISTIC)) {
5623 /* If we're not ready, then normal ifup will take care
5624 * of this. Otherwise, we need to request our rs here.
5626 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5631 write_lock_bh(&idev->lock);
5634 idev->if_flags |= IF_RS_SENT;
5635 idev->rs_interval = rfc3315_s14_backoff_init(
5636 idev->cnf.rtr_solicit_interval);
5637 idev->rs_probes = 1;
5638 addrconf_mod_rs_timer(idev, idev->rs_interval);
5641 /* Well, that's kinda nasty ... */
5642 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5643 spin_lock(&ifp->lock);
5644 if (ifp->tokenized) {
5646 ifp->prefered_lft = 0;
5648 spin_unlock(&ifp->lock);
5651 write_unlock_bh(&idev->lock);
5652 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5653 addrconf_verify_rtnl();
5657 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5658 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5659 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5662 static int inet6_validate_link_af(const struct net_device *dev,
5663 const struct nlattr *nla)
5665 struct nlattr *tb[IFLA_INET6_MAX + 1];
5667 if (dev && !__in6_dev_get(dev))
5668 return -EAFNOSUPPORT;
5670 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5674 static int check_addr_gen_mode(int mode)
5676 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5677 mode != IN6_ADDR_GEN_MODE_NONE &&
5678 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5679 mode != IN6_ADDR_GEN_MODE_RANDOM)
5684 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5687 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5688 !idev->cnf.stable_secret.initialized &&
5689 !net->ipv6.devconf_dflt->stable_secret.initialized)
5694 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5697 struct inet6_dev *idev = __in6_dev_get(dev);
5698 struct nlattr *tb[IFLA_INET6_MAX + 1];
5701 return -EAFNOSUPPORT;
5703 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5706 if (tb[IFLA_INET6_TOKEN]) {
5707 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5712 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5713 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5715 if (check_addr_gen_mode(mode) < 0 ||
5716 check_stable_privacy(idev, dev_net(dev), mode) < 0)
5719 idev->cnf.addr_gen_mode = mode;
5726 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5727 u32 portid, u32 seq, int event, unsigned int flags)
5729 struct net_device *dev = idev->dev;
5730 struct ifinfomsg *hdr;
5731 struct nlmsghdr *nlh;
5734 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5738 hdr = nlmsg_data(nlh);
5739 hdr->ifi_family = AF_INET6;
5741 hdr->ifi_type = dev->type;
5742 hdr->ifi_index = dev->ifindex;
5743 hdr->ifi_flags = dev_get_flags(dev);
5744 hdr->ifi_change = 0;
5746 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5748 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5749 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5750 (dev->ifindex != dev_get_iflink(dev) &&
5751 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5752 nla_put_u8(skb, IFLA_OPERSTATE,
5753 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5754 goto nla_put_failure;
5755 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5757 goto nla_put_failure;
5759 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5760 goto nla_put_failure;
5762 nla_nest_end(skb, protoinfo);
5763 nlmsg_end(skb, nlh);
5767 nlmsg_cancel(skb, nlh);
5771 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5772 struct netlink_ext_ack *extack)
5774 struct ifinfomsg *ifm;
5776 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5777 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5781 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5782 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5786 ifm = nlmsg_data(nlh);
5787 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5788 ifm->ifi_change || ifm->ifi_index) {
5789 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5796 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5798 struct net *net = sock_net(skb->sk);
5801 struct net_device *dev;
5802 struct inet6_dev *idev;
5803 struct hlist_head *head;
5805 /* only requests using strict checking can pass data to
5806 * influence the dump
5808 if (cb->strict_check) {
5809 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5816 s_idx = cb->args[1];
5819 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5821 head = &net->dev_index_head[h];
5822 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5825 idev = __in6_dev_get(dev);
5828 if (inet6_fill_ifinfo(skb, idev,
5829 NETLINK_CB(cb->skb).portid,
5831 RTM_NEWLINK, NLM_F_MULTI) < 0)
5845 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5847 struct sk_buff *skb;
5848 struct net *net = dev_net(idev->dev);
5851 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5855 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5857 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5858 WARN_ON(err == -EMSGSIZE);
5862 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5866 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5869 static inline size_t inet6_prefix_nlmsg_size(void)
5871 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5872 + nla_total_size(sizeof(struct in6_addr))
5873 + nla_total_size(sizeof(struct prefix_cacheinfo));
5876 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5877 struct prefix_info *pinfo, u32 portid, u32 seq,
5878 int event, unsigned int flags)
5880 struct prefixmsg *pmsg;
5881 struct nlmsghdr *nlh;
5882 struct prefix_cacheinfo ci;
5884 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5888 pmsg = nlmsg_data(nlh);
5889 pmsg->prefix_family = AF_INET6;
5890 pmsg->prefix_pad1 = 0;
5891 pmsg->prefix_pad2 = 0;
5892 pmsg->prefix_ifindex = idev->dev->ifindex;
5893 pmsg->prefix_len = pinfo->prefix_len;
5894 pmsg->prefix_type = pinfo->type;
5895 pmsg->prefix_pad3 = 0;
5896 pmsg->prefix_flags = 0;
5898 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5899 if (pinfo->autoconf)
5900 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5902 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5903 goto nla_put_failure;
5904 ci.preferred_time = ntohl(pinfo->prefered);
5905 ci.valid_time = ntohl(pinfo->valid);
5906 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5907 goto nla_put_failure;
5908 nlmsg_end(skb, nlh);
5912 nlmsg_cancel(skb, nlh);
5916 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5917 struct prefix_info *pinfo)
5919 struct sk_buff *skb;
5920 struct net *net = dev_net(idev->dev);
5923 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5927 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5929 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5930 WARN_ON(err == -EMSGSIZE);
5934 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5938 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5941 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5943 struct net *net = dev_net(ifp->idev->dev);
5948 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5953 * If the address was optimistic
5954 * we inserted the route at the start of
5955 * our DAD process, so we don't need
5958 if (!rcu_access_pointer(ifp->rt->fib6_node))
5959 ip6_ins_rt(net, ifp->rt);
5960 if (ifp->idev->cnf.forwarding)
5961 addrconf_join_anycast(ifp);
5962 if (!ipv6_addr_any(&ifp->peer_addr))
5963 addrconf_prefix_route(&ifp->peer_addr, 128, 0,
5964 ifp->idev->dev, 0, 0,
5968 if (ifp->idev->cnf.forwarding)
5969 addrconf_leave_anycast(ifp);
5970 addrconf_leave_solict(ifp->idev, &ifp->addr);
5971 if (!ipv6_addr_any(&ifp->peer_addr)) {
5972 struct fib6_info *rt;
5974 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5975 ifp->idev->dev, 0, 0,
5978 ip6_del_rt(net, rt);
5981 ip6_del_rt(net, ifp->rt);
5984 rt_genid_bump_ipv6(net);
5987 atomic_inc(&net->ipv6.dev_addr_genid);
5990 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5993 if (likely(ifp->idev->dead == 0))
5994 __ipv6_ifa_notify(event, ifp);
5995 rcu_read_unlock_bh();
5998 #ifdef CONFIG_SYSCTL
6001 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
6002 void __user *buffer, size_t *lenp, loff_t *ppos)
6004 int *valp = ctl->data;
6007 struct ctl_table lctl;
6011 * ctl->data points to idev->cnf.forwarding, we should
6012 * not modify it until we get the rtnl lock.
6017 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6020 ret = addrconf_fixup_forwarding(ctl, valp, val);
6027 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6028 void __user *buffer, size_t *lenp, loff_t *ppos)
6030 struct inet6_dev *idev = ctl->extra1;
6031 int min_mtu = IPV6_MIN_MTU;
6032 struct ctl_table lctl;
6035 lctl.extra1 = &min_mtu;
6036 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6038 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6041 static void dev_disable_change(struct inet6_dev *idev)
6043 struct netdev_notifier_info info;
6045 if (!idev || !idev->dev)
6048 netdev_notifier_info_init(&info, idev->dev);
6049 if (idev->cnf.disable_ipv6)
6050 addrconf_notify(NULL, NETDEV_DOWN, &info);
6052 addrconf_notify(NULL, NETDEV_UP, &info);
6055 static void addrconf_disable_change(struct net *net, __s32 newf)
6057 struct net_device *dev;
6058 struct inet6_dev *idev;
6060 for_each_netdev(net, dev) {
6061 idev = __in6_dev_get(dev);
6063 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6064 idev->cnf.disable_ipv6 = newf;
6066 dev_disable_change(idev);
6071 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
6076 if (!rtnl_trylock())
6077 return restart_syscall();
6079 net = (struct net *)table->extra2;
6083 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6088 if (p == &net->ipv6.devconf_all->disable_ipv6) {
6089 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6090 addrconf_disable_change(net, newf);
6091 } else if ((!newf) ^ (!old))
6092 dev_disable_change((struct inet6_dev *)table->extra1);
6099 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
6100 void __user *buffer, size_t *lenp, loff_t *ppos)
6102 int *valp = ctl->data;
6105 struct ctl_table lctl;
6109 * ctl->data points to idev->cnf.disable_ipv6, we should
6110 * not modify it until we get the rtnl lock.
6115 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6118 ret = addrconf_disable_ipv6(ctl, valp, val);
6125 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6126 void __user *buffer, size_t *lenp, loff_t *ppos)
6128 int *valp = ctl->data;
6133 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6136 if (write && old != new) {
6137 struct net *net = ctl->extra2;
6139 if (!rtnl_trylock())
6140 return restart_syscall();
6142 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
6143 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6144 NETCONFA_PROXY_NEIGH,
6145 NETCONFA_IFINDEX_DEFAULT,
6146 net->ipv6.devconf_dflt);
6147 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
6148 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6149 NETCONFA_PROXY_NEIGH,
6150 NETCONFA_IFINDEX_ALL,
6151 net->ipv6.devconf_all);
6153 struct inet6_dev *idev = ctl->extra1;
6155 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6156 NETCONFA_PROXY_NEIGH,
6166 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6167 void __user *buffer, size_t *lenp,
6172 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6173 struct net *net = (struct net *)ctl->extra2;
6174 struct ctl_table tmp = {
6176 .maxlen = sizeof(new_val),
6180 if (!rtnl_trylock())
6181 return restart_syscall();
6183 new_val = *((u32 *)ctl->data);
6185 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6190 if (check_addr_gen_mode(new_val) < 0) {
6196 if (check_stable_privacy(idev, net, new_val) < 0) {
6201 if (idev->cnf.addr_gen_mode != new_val) {
6202 idev->cnf.addr_gen_mode = new_val;
6203 addrconf_dev_config(idev->dev);
6205 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6206 struct net_device *dev;
6208 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6209 for_each_netdev(net, dev) {
6210 idev = __in6_dev_get(dev);
6212 idev->cnf.addr_gen_mode != new_val) {
6213 idev->cnf.addr_gen_mode = new_val;
6214 addrconf_dev_config(idev->dev);
6219 *((u32 *)ctl->data) = new_val;
6228 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6229 void __user *buffer, size_t *lenp,
6233 struct in6_addr addr;
6234 char str[IPV6_MAX_STRLEN];
6235 struct ctl_table lctl = *ctl;
6236 struct net *net = ctl->extra2;
6237 struct ipv6_stable_secret *secret = ctl->data;
6239 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6242 lctl.maxlen = IPV6_MAX_STRLEN;
6245 if (!rtnl_trylock())
6246 return restart_syscall();
6248 if (!write && !secret->initialized) {
6253 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6254 if (err >= sizeof(str)) {
6259 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6263 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6268 secret->initialized = true;
6269 secret->secret = addr;
6271 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6272 struct net_device *dev;
6274 for_each_netdev(net, dev) {
6275 struct inet6_dev *idev = __in6_dev_get(dev);
6278 idev->cnf.addr_gen_mode =
6279 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6283 struct inet6_dev *idev = ctl->extra1;
6285 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6295 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6297 void __user *buffer,
6301 int *valp = ctl->data;
6304 struct ctl_table lctl;
6307 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6308 * we should not modify it until we get the rtnl lock.
6313 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6316 ret = addrconf_fixup_linkdown(ctl, valp, val);
6323 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6327 rt->dst.flags |= DST_NOPOLICY;
6329 rt->dst.flags &= ~DST_NOPOLICY;
6334 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6336 struct inet6_ifaddr *ifa;
6338 read_lock_bh(&idev->lock);
6339 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6340 spin_lock(&ifa->lock);
6342 struct fib6_info *rt = ifa->rt;
6346 ifa->rt->dst_nopolicy = val ? true : false;
6347 if (rt->rt6i_pcpu) {
6348 for_each_possible_cpu(cpu) {
6349 struct rt6_info **rtp;
6351 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
6352 addrconf_set_nopolicy(*rtp, val);
6357 spin_unlock(&ifa->lock);
6359 read_unlock_bh(&idev->lock);
6363 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6365 struct inet6_dev *idev;
6368 if (!rtnl_trylock())
6369 return restart_syscall();
6373 net = (struct net *)ctl->extra2;
6374 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6379 if (valp == &net->ipv6.devconf_all->disable_policy) {
6380 struct net_device *dev;
6382 for_each_netdev(net, dev) {
6383 idev = __in6_dev_get(dev);
6385 addrconf_disable_policy_idev(idev, val);
6388 idev = (struct inet6_dev *)ctl->extra1;
6389 addrconf_disable_policy_idev(idev, val);
6397 int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6398 void __user *buffer, size_t *lenp,
6401 int *valp = ctl->data;
6404 struct ctl_table lctl;
6409 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6411 if (write && (*valp != val))
6412 ret = addrconf_disable_policy(ctl, valp, val);
6420 static int minus_one = -1;
6421 static const int zero = 0;
6422 static const int one = 1;
6423 static const int two_five_five = 255;
6425 static const struct ctl_table addrconf_sysctl[] = {
6427 .procname = "forwarding",
6428 .data = &ipv6_devconf.forwarding,
6429 .maxlen = sizeof(int),
6431 .proc_handler = addrconf_sysctl_forward,
6434 .procname = "hop_limit",
6435 .data = &ipv6_devconf.hop_limit,
6436 .maxlen = sizeof(int),
6438 .proc_handler = proc_dointvec_minmax,
6439 .extra1 = (void *)&one,
6440 .extra2 = (void *)&two_five_five,
6444 .data = &ipv6_devconf.mtu6,
6445 .maxlen = sizeof(int),
6447 .proc_handler = addrconf_sysctl_mtu,
6450 .procname = "accept_ra",
6451 .data = &ipv6_devconf.accept_ra,
6452 .maxlen = sizeof(int),
6454 .proc_handler = proc_dointvec,
6457 .procname = "accept_redirects",
6458 .data = &ipv6_devconf.accept_redirects,
6459 .maxlen = sizeof(int),
6461 .proc_handler = proc_dointvec,
6464 .procname = "autoconf",
6465 .data = &ipv6_devconf.autoconf,
6466 .maxlen = sizeof(int),
6468 .proc_handler = proc_dointvec,
6471 .procname = "dad_transmits",
6472 .data = &ipv6_devconf.dad_transmits,
6473 .maxlen = sizeof(int),
6475 .proc_handler = proc_dointvec,
6478 .procname = "router_solicitations",
6479 .data = &ipv6_devconf.rtr_solicits,
6480 .maxlen = sizeof(int),
6482 .proc_handler = proc_dointvec_minmax,
6483 .extra1 = &minus_one,
6486 .procname = "router_solicitation_interval",
6487 .data = &ipv6_devconf.rtr_solicit_interval,
6488 .maxlen = sizeof(int),
6490 .proc_handler = proc_dointvec_jiffies,
6493 .procname = "router_solicitation_max_interval",
6494 .data = &ipv6_devconf.rtr_solicit_max_interval,
6495 .maxlen = sizeof(int),
6497 .proc_handler = proc_dointvec_jiffies,
6500 .procname = "router_solicitation_delay",
6501 .data = &ipv6_devconf.rtr_solicit_delay,
6502 .maxlen = sizeof(int),
6504 .proc_handler = proc_dointvec_jiffies,
6507 .procname = "force_mld_version",
6508 .data = &ipv6_devconf.force_mld_version,
6509 .maxlen = sizeof(int),
6511 .proc_handler = proc_dointvec,
6514 .procname = "mldv1_unsolicited_report_interval",
6516 &ipv6_devconf.mldv1_unsolicited_report_interval,
6517 .maxlen = sizeof(int),
6519 .proc_handler = proc_dointvec_ms_jiffies,
6522 .procname = "mldv2_unsolicited_report_interval",
6524 &ipv6_devconf.mldv2_unsolicited_report_interval,
6525 .maxlen = sizeof(int),
6527 .proc_handler = proc_dointvec_ms_jiffies,
6530 .procname = "use_tempaddr",
6531 .data = &ipv6_devconf.use_tempaddr,
6532 .maxlen = sizeof(int),
6534 .proc_handler = proc_dointvec,
6537 .procname = "temp_valid_lft",
6538 .data = &ipv6_devconf.temp_valid_lft,
6539 .maxlen = sizeof(int),
6541 .proc_handler = proc_dointvec,
6544 .procname = "temp_prefered_lft",
6545 .data = &ipv6_devconf.temp_prefered_lft,
6546 .maxlen = sizeof(int),
6548 .proc_handler = proc_dointvec,
6551 .procname = "regen_max_retry",
6552 .data = &ipv6_devconf.regen_max_retry,
6553 .maxlen = sizeof(int),
6555 .proc_handler = proc_dointvec,
6558 .procname = "max_desync_factor",
6559 .data = &ipv6_devconf.max_desync_factor,
6560 .maxlen = sizeof(int),
6562 .proc_handler = proc_dointvec,
6565 .procname = "max_addresses",
6566 .data = &ipv6_devconf.max_addresses,
6567 .maxlen = sizeof(int),
6569 .proc_handler = proc_dointvec,
6572 .procname = "accept_ra_defrtr",
6573 .data = &ipv6_devconf.accept_ra_defrtr,
6574 .maxlen = sizeof(int),
6576 .proc_handler = proc_dointvec,
6579 .procname = "accept_ra_min_hop_limit",
6580 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6581 .maxlen = sizeof(int),
6583 .proc_handler = proc_dointvec,
6586 .procname = "accept_ra_pinfo",
6587 .data = &ipv6_devconf.accept_ra_pinfo,
6588 .maxlen = sizeof(int),
6590 .proc_handler = proc_dointvec,
6592 #ifdef CONFIG_IPV6_ROUTER_PREF
6594 .procname = "accept_ra_rtr_pref",
6595 .data = &ipv6_devconf.accept_ra_rtr_pref,
6596 .maxlen = sizeof(int),
6598 .proc_handler = proc_dointvec,
6601 .procname = "router_probe_interval",
6602 .data = &ipv6_devconf.rtr_probe_interval,
6603 .maxlen = sizeof(int),
6605 .proc_handler = proc_dointvec_jiffies,
6607 #ifdef CONFIG_IPV6_ROUTE_INFO
6609 .procname = "accept_ra_rt_info_min_plen",
6610 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6611 .maxlen = sizeof(int),
6613 .proc_handler = proc_dointvec,
6616 .procname = "accept_ra_rt_info_max_plen",
6617 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6618 .maxlen = sizeof(int),
6620 .proc_handler = proc_dointvec,
6625 .procname = "proxy_ndp",
6626 .data = &ipv6_devconf.proxy_ndp,
6627 .maxlen = sizeof(int),
6629 .proc_handler = addrconf_sysctl_proxy_ndp,
6632 .procname = "accept_source_route",
6633 .data = &ipv6_devconf.accept_source_route,
6634 .maxlen = sizeof(int),
6636 .proc_handler = proc_dointvec,
6638 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6640 .procname = "optimistic_dad",
6641 .data = &ipv6_devconf.optimistic_dad,
6642 .maxlen = sizeof(int),
6644 .proc_handler = proc_dointvec,
6647 .procname = "use_optimistic",
6648 .data = &ipv6_devconf.use_optimistic,
6649 .maxlen = sizeof(int),
6651 .proc_handler = proc_dointvec,
6654 #ifdef CONFIG_IPV6_MROUTE
6656 .procname = "mc_forwarding",
6657 .data = &ipv6_devconf.mc_forwarding,
6658 .maxlen = sizeof(int),
6660 .proc_handler = proc_dointvec,
6664 .procname = "disable_ipv6",
6665 .data = &ipv6_devconf.disable_ipv6,
6666 .maxlen = sizeof(int),
6668 .proc_handler = addrconf_sysctl_disable,
6671 .procname = "accept_dad",
6672 .data = &ipv6_devconf.accept_dad,
6673 .maxlen = sizeof(int),
6675 .proc_handler = proc_dointvec,
6678 .procname = "force_tllao",
6679 .data = &ipv6_devconf.force_tllao,
6680 .maxlen = sizeof(int),
6682 .proc_handler = proc_dointvec
6685 .procname = "ndisc_notify",
6686 .data = &ipv6_devconf.ndisc_notify,
6687 .maxlen = sizeof(int),
6689 .proc_handler = proc_dointvec
6692 .procname = "suppress_frag_ndisc",
6693 .data = &ipv6_devconf.suppress_frag_ndisc,
6694 .maxlen = sizeof(int),
6696 .proc_handler = proc_dointvec
6699 .procname = "accept_ra_from_local",
6700 .data = &ipv6_devconf.accept_ra_from_local,
6701 .maxlen = sizeof(int),
6703 .proc_handler = proc_dointvec,
6706 .procname = "accept_ra_mtu",
6707 .data = &ipv6_devconf.accept_ra_mtu,
6708 .maxlen = sizeof(int),
6710 .proc_handler = proc_dointvec,
6713 .procname = "stable_secret",
6714 .data = &ipv6_devconf.stable_secret,
6715 .maxlen = IPV6_MAX_STRLEN,
6717 .proc_handler = addrconf_sysctl_stable_secret,
6720 .procname = "use_oif_addrs_only",
6721 .data = &ipv6_devconf.use_oif_addrs_only,
6722 .maxlen = sizeof(int),
6724 .proc_handler = proc_dointvec,
6727 .procname = "ignore_routes_with_linkdown",
6728 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6729 .maxlen = sizeof(int),
6731 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6734 .procname = "drop_unicast_in_l2_multicast",
6735 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6736 .maxlen = sizeof(int),
6738 .proc_handler = proc_dointvec,
6741 .procname = "drop_unsolicited_na",
6742 .data = &ipv6_devconf.drop_unsolicited_na,
6743 .maxlen = sizeof(int),
6745 .proc_handler = proc_dointvec,
6748 .procname = "keep_addr_on_down",
6749 .data = &ipv6_devconf.keep_addr_on_down,
6750 .maxlen = sizeof(int),
6752 .proc_handler = proc_dointvec,
6756 .procname = "seg6_enabled",
6757 .data = &ipv6_devconf.seg6_enabled,
6758 .maxlen = sizeof(int),
6760 .proc_handler = proc_dointvec,
6762 #ifdef CONFIG_IPV6_SEG6_HMAC
6764 .procname = "seg6_require_hmac",
6765 .data = &ipv6_devconf.seg6_require_hmac,
6766 .maxlen = sizeof(int),
6768 .proc_handler = proc_dointvec,
6772 .procname = "enhanced_dad",
6773 .data = &ipv6_devconf.enhanced_dad,
6774 .maxlen = sizeof(int),
6776 .proc_handler = proc_dointvec,
6779 .procname = "addr_gen_mode",
6780 .data = &ipv6_devconf.addr_gen_mode,
6781 .maxlen = sizeof(int),
6783 .proc_handler = addrconf_sysctl_addr_gen_mode,
6786 .procname = "disable_policy",
6787 .data = &ipv6_devconf.disable_policy,
6788 .maxlen = sizeof(int),
6790 .proc_handler = addrconf_sysctl_disable_policy,
6793 .procname = "ndisc_tclass",
6794 .data = &ipv6_devconf.ndisc_tclass,
6795 .maxlen = sizeof(int),
6797 .proc_handler = proc_dointvec_minmax,
6798 .extra1 = (void *)&zero,
6799 .extra2 = (void *)&two_five_five,
6806 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6807 struct inet6_dev *idev, struct ipv6_devconf *p)
6810 struct ctl_table *table;
6811 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6813 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6817 for (i = 0; table[i].data; i++) {
6818 table[i].data += (char *)p - (char *)&ipv6_devconf;
6819 /* If one of these is already set, then it is not safe to
6820 * overwrite either of them: this makes proc_dointvec_minmax
6823 if (!table[i].extra1 && !table[i].extra2) {
6824 table[i].extra1 = idev; /* embedded; no ref */
6825 table[i].extra2 = net;
6829 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6831 p->sysctl_header = register_net_sysctl(net, path, table);
6832 if (!p->sysctl_header)
6835 if (!strcmp(dev_name, "all"))
6836 ifindex = NETCONFA_IFINDEX_ALL;
6837 else if (!strcmp(dev_name, "default"))
6838 ifindex = NETCONFA_IFINDEX_DEFAULT;
6840 ifindex = idev->dev->ifindex;
6841 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6851 static void __addrconf_sysctl_unregister(struct net *net,
6852 struct ipv6_devconf *p, int ifindex)
6854 struct ctl_table *table;
6856 if (!p->sysctl_header)
6859 table = p->sysctl_header->ctl_table_arg;
6860 unregister_net_sysctl_table(p->sysctl_header);
6861 p->sysctl_header = NULL;
6864 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
6867 static int addrconf_sysctl_register(struct inet6_dev *idev)
6871 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6874 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6875 &ndisc_ifinfo_sysctl_change);
6878 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6881 neigh_sysctl_unregister(idev->nd_parms);
6886 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6888 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6889 idev->dev->ifindex);
6890 neigh_sysctl_unregister(idev->nd_parms);
6896 static int __net_init addrconf_init_net(struct net *net)
6899 struct ipv6_devconf *all, *dflt;
6901 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6905 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6907 goto err_alloc_dflt;
6909 if (IS_ENABLED(CONFIG_SYSCTL) &&
6910 sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) {
6911 memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf));
6912 memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt));
6915 /* these will be inherited by all namespaces */
6916 dflt->autoconf = ipv6_defaults.autoconf;
6917 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6919 dflt->stable_secret.initialized = false;
6920 all->stable_secret.initialized = false;
6922 net->ipv6.devconf_all = all;
6923 net->ipv6.devconf_dflt = dflt;
6925 #ifdef CONFIG_SYSCTL
6926 err = __addrconf_sysctl_register(net, "all", NULL, all);
6930 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6936 #ifdef CONFIG_SYSCTL
6938 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
6948 static void __net_exit addrconf_exit_net(struct net *net)
6950 #ifdef CONFIG_SYSCTL
6951 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6952 NETCONFA_IFINDEX_DEFAULT);
6953 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6954 NETCONFA_IFINDEX_ALL);
6956 kfree(net->ipv6.devconf_dflt);
6957 kfree(net->ipv6.devconf_all);
6960 static struct pernet_operations addrconf_ops = {
6961 .init = addrconf_init_net,
6962 .exit = addrconf_exit_net,
6965 static struct rtnl_af_ops inet6_ops __read_mostly = {
6967 .fill_link_af = inet6_fill_link_af,
6968 .get_link_af_size = inet6_get_link_af_size,
6969 .validate_link_af = inet6_validate_link_af,
6970 .set_link_af = inet6_set_link_af,
6974 * Init / cleanup code
6977 int __init addrconf_init(void)
6979 struct inet6_dev *idev;
6982 err = ipv6_addr_label_init();
6984 pr_crit("%s: cannot initialize default policy table: %d\n",
6989 err = register_pernet_subsys(&addrconf_ops);
6993 addrconf_wq = create_workqueue("ipv6_addrconf");
6999 /* The addrconf netdev notifier requires that loopback_dev
7000 * has it's ipv6 private information allocated and setup
7001 * before it can bring up and give link-local addresses
7002 * to other devices which are up.
7004 * Unfortunately, loopback_dev is not necessarily the first
7005 * entry in the global dev_base list of net devices. In fact,
7006 * it is likely to be the very last entry on that list.
7007 * So this causes the notifier registry below to try and
7008 * give link-local addresses to all devices besides loopback_dev
7009 * first, then loopback_dev, which cases all the non-loopback_dev
7010 * devices to fail to get a link-local address.
7012 * So, as a temporary fix, allocate the ipv6 structure for
7013 * loopback_dev first by hand.
7014 * Longer term, all of the dependencies ipv6 has upon the loopback
7015 * device and it being up should be removed.
7018 idev = ipv6_add_dev(init_net.loopback_dev);
7021 err = PTR_ERR(idev);
7025 ip6_route_init_special_entries();
7027 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7028 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7030 register_netdevice_notifier(&ipv6_dev_notf);
7034 rtnl_af_register(&inet6_ops);
7036 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7037 NULL, inet6_dump_ifinfo, 0);
7041 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7042 inet6_rtm_newaddr, NULL, 0);
7045 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7046 inet6_rtm_deladdr, NULL, 0);
7049 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7050 inet6_rtm_getaddr, inet6_dump_ifaddr,
7051 RTNL_FLAG_DOIT_UNLOCKED);
7054 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7055 NULL, inet6_dump_ifmcaddr, 0);
7058 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7059 NULL, inet6_dump_ifacaddr, 0);
7062 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7063 inet6_netconf_get_devconf,
7064 inet6_netconf_dump_devconf,
7065 RTNL_FLAG_DOIT_UNLOCKED);
7068 err = ipv6_addr_label_rtnl_register();
7074 rtnl_unregister_all(PF_INET6);
7075 rtnl_af_unregister(&inet6_ops);
7076 unregister_netdevice_notifier(&ipv6_dev_notf);
7078 destroy_workqueue(addrconf_wq);
7080 unregister_pernet_subsys(&addrconf_ops);
7082 ipv6_addr_label_cleanup();
7087 void addrconf_cleanup(void)
7089 struct net_device *dev;
7092 unregister_netdevice_notifier(&ipv6_dev_notf);
7093 unregister_pernet_subsys(&addrconf_ops);
7094 ipv6_addr_label_cleanup();
7096 rtnl_af_unregister(&inet6_ops);
7100 /* clean dev list */
7101 for_each_netdev(&init_net, dev) {
7102 if (__in6_dev_get(dev) == NULL)
7104 addrconf_ifdown(dev, 1);
7106 addrconf_ifdown(init_net.loopback_dev, 2);
7111 spin_lock_bh(&addrconf_hash_lock);
7112 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7113 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
7114 spin_unlock_bh(&addrconf_hash_lock);
7115 cancel_delayed_work(&addr_chk_work);
7118 destroy_workqueue(addrconf_wq);