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/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/inet.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_addr.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_arcnet.h>
55 #include <linux/if_infiniband.h>
56 #include <linux/route.h>
57 #include <linux/inetdevice.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
61 #include <linux/sysctl.h>
63 #include <linux/capability.h>
64 #include <linux/delay.h>
65 #include <linux/notifier.h>
66 #include <linux/string.h>
67 #include <linux/hash.h>
69 #include <net/net_namespace.h>
73 #include <net/6lowpan.h>
74 #include <net/firewire.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <net/l3mdev.h>
85 #include <linux/if_tunnel.h>
86 #include <linux/rtnetlink.h>
87 #include <linux/netconf.h>
88 #include <linux/random.h>
89 #include <linux/uaccess.h>
90 #include <asm/unaligned.h>
92 #include <linux/proc_fs.h>
93 #include <linux/seq_file.h>
94 #include <linux/export.h>
96 /* Set to 3 to get tracing... */
100 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
102 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
105 #define INFINITY_LIFE_TIME 0xFFFFFFFF
107 #define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110 static inline u32 cstamp_delta(unsigned long cstamp)
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
115 static inline s32 rfc3315_s14_backoff_init(s32 irt)
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
119 do_div(tmp, 1000000);
123 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
127 do_div(tmp, 1000000);
128 if ((s32)tmp > mrt) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
131 do_div(tmp, 1000000);
137 static int addrconf_sysctl_register(struct inet6_dev *idev);
138 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
145 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
150 static void ipv6_regen_rndid(struct inet6_dev *idev);
151 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
153 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
154 static int ipv6_count_addresses(struct inet6_dev *idev);
155 static int ipv6_generate_stable_address(struct in6_addr *addr,
157 const struct inet6_dev *idev);
160 * Configured unicast address hash table
162 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
163 static DEFINE_SPINLOCK(addrconf_hash_lock);
165 static void addrconf_verify(void);
166 static void addrconf_verify_rtnl(void);
167 static void addrconf_verify_work(struct work_struct *);
169 static struct workqueue_struct *addrconf_wq;
170 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
172 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
173 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
175 static void addrconf_type_change(struct net_device *dev,
176 unsigned long event);
177 static int addrconf_ifdown(struct net_device *dev, int how);
179 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
181 const struct net_device *dev,
182 u32 flags, u32 noflags);
184 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
185 static void addrconf_dad_work(struct work_struct *w);
186 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
187 static void addrconf_dad_run(struct inet6_dev *idev);
188 static void addrconf_rs_timer(unsigned long data);
189 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
190 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
192 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
193 struct prefix_info *pinfo);
194 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
195 struct net_device *dev);
197 static struct ipv6_devconf ipv6_devconf __read_mostly = {
199 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
200 .mtu6 = IPV6_MIN_MTU,
202 .accept_redirects = 1,
204 .force_mld_version = 0,
205 .mldv1_unsolicited_report_interval = 10 * HZ,
206 .mldv2_unsolicited_report_interval = HZ,
208 .rtr_solicits = MAX_RTR_SOLICITATIONS,
209 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
210 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
211 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
213 .temp_valid_lft = TEMP_VALID_LIFETIME,
214 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
215 .regen_max_retry = REGEN_MAX_RETRY,
216 .max_desync_factor = MAX_DESYNC_FACTOR,
217 .max_addresses = IPV6_MAX_ADDRESSES,
218 .accept_ra_defrtr = 1,
219 .accept_ra_from_local = 0,
220 .accept_ra_min_hop_limit= 1,
221 .accept_ra_pinfo = 1,
222 #ifdef CONFIG_IPV6_ROUTER_PREF
223 .accept_ra_rtr_pref = 1,
224 .rtr_probe_interval = 60 * HZ,
225 #ifdef CONFIG_IPV6_ROUTE_INFO
226 .accept_ra_rt_info_max_plen = 0,
230 .accept_source_route = 0, /* we do not accept RH0 by default. */
233 .suppress_frag_ndisc = 1,
236 .initialized = false,
238 .use_oif_addrs_only = 0,
239 .ignore_routes_with_linkdown = 0,
240 .keep_addr_on_down = 0,
242 #ifdef CONFIG_IPV6_SEG6_HMAC
243 .seg6_require_hmac = 0,
246 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
249 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
251 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
252 .mtu6 = IPV6_MIN_MTU,
254 .accept_redirects = 1,
256 .force_mld_version = 0,
257 .mldv1_unsolicited_report_interval = 10 * HZ,
258 .mldv2_unsolicited_report_interval = HZ,
260 .rtr_solicits = MAX_RTR_SOLICITATIONS,
261 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
262 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
263 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
265 .temp_valid_lft = TEMP_VALID_LIFETIME,
266 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
267 .regen_max_retry = REGEN_MAX_RETRY,
268 .max_desync_factor = MAX_DESYNC_FACTOR,
269 .max_addresses = IPV6_MAX_ADDRESSES,
270 .accept_ra_defrtr = 1,
271 .accept_ra_from_local = 0,
272 .accept_ra_min_hop_limit= 1,
273 .accept_ra_pinfo = 1,
274 #ifdef CONFIG_IPV6_ROUTER_PREF
275 .accept_ra_rtr_pref = 1,
276 .rtr_probe_interval = 60 * HZ,
277 #ifdef CONFIG_IPV6_ROUTE_INFO
278 .accept_ra_rt_info_max_plen = 0,
282 .accept_source_route = 0, /* we do not accept RH0 by default. */
285 .suppress_frag_ndisc = 1,
288 .initialized = false,
290 .use_oif_addrs_only = 0,
291 .ignore_routes_with_linkdown = 0,
292 .keep_addr_on_down = 0,
294 #ifdef CONFIG_IPV6_SEG6_HMAC
295 .seg6_require_hmac = 0,
298 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
301 /* Check if a valid qdisc is available */
302 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
304 return !qdisc_tx_is_noop(dev);
307 static void addrconf_del_rs_timer(struct inet6_dev *idev)
309 if (del_timer(&idev->rs_timer))
313 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
315 if (cancel_delayed_work(&ifp->dad_work))
319 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
322 if (!timer_pending(&idev->rs_timer))
324 mod_timer(&idev->rs_timer, jiffies + when);
327 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
330 if (!delayed_work_pending(&ifp->dad_work))
332 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
335 static int snmp6_alloc_dev(struct inet6_dev *idev)
339 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
340 if (!idev->stats.ipv6)
343 for_each_possible_cpu(i) {
344 struct ipstats_mib *addrconf_stats;
345 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
346 u64_stats_init(&addrconf_stats->syncp);
350 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
352 if (!idev->stats.icmpv6dev)
354 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
356 if (!idev->stats.icmpv6msgdev)
362 kfree(idev->stats.icmpv6dev);
364 free_percpu(idev->stats.ipv6);
369 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
371 struct inet6_dev *ndev;
376 if (dev->mtu < IPV6_MIN_MTU)
377 return ERR_PTR(-EINVAL);
379 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
383 rwlock_init(&ndev->lock);
385 INIT_LIST_HEAD(&ndev->addr_list);
386 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
387 (unsigned long)ndev);
388 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
390 if (ndev->cnf.stable_secret.initialized)
391 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
393 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
395 ndev->cnf.mtu6 = dev->mtu;
396 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
397 if (!ndev->nd_parms) {
401 if (ndev->cnf.forwarding)
402 dev_disable_lro(dev);
403 /* We refer to the device */
406 if (snmp6_alloc_dev(ndev) < 0) {
408 "%s: cannot allocate memory for statistics; dev=%s.\n",
409 __func__, dev->name);
410 neigh_parms_release(&nd_tbl, ndev->nd_parms);
416 if (snmp6_register_dev(ndev) < 0) {
418 "%s: cannot create /proc/net/dev_snmp6/%s\n",
419 __func__, dev->name);
423 /* One reference from device. */
426 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
427 ndev->cnf.accept_dad = -1;
429 #if IS_ENABLED(CONFIG_IPV6_SIT)
430 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
431 pr_info("%s: Disabled Multicast RS\n", dev->name);
432 ndev->cnf.rtr_solicits = 0;
436 INIT_LIST_HEAD(&ndev->tempaddr_list);
437 ndev->desync_factor = U32_MAX;
438 if ((dev->flags&IFF_LOOPBACK) ||
439 dev->type == ARPHRD_TUNNEL ||
440 dev->type == ARPHRD_TUNNEL6 ||
441 dev->type == ARPHRD_SIT ||
442 dev->type == ARPHRD_NONE) {
443 ndev->cnf.use_tempaddr = -1;
445 ipv6_regen_rndid(ndev);
447 ndev->token = in6addr_any;
449 if (netif_running(dev) && addrconf_qdisc_ok(dev))
450 ndev->if_flags |= IF_READY;
452 ipv6_mc_init_dev(ndev);
453 ndev->tstamp = jiffies;
454 err = addrconf_sysctl_register(ndev);
456 ipv6_mc_destroy_dev(ndev);
457 snmp6_unregister_dev(ndev);
460 /* protected by rtnl_lock */
461 rcu_assign_pointer(dev->ip6_ptr, ndev);
463 /* Join interface-local all-node multicast group */
464 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
466 /* Join all-node multicast group */
467 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
469 /* Join all-router multicast group if forwarding is set */
470 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
471 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
476 neigh_parms_release(&nd_tbl, ndev->nd_parms);
478 in6_dev_finish_destroy(ndev);
482 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
484 struct inet6_dev *idev;
488 idev = __in6_dev_get(dev);
490 idev = ipv6_add_dev(dev);
495 if (dev->flags&IFF_UP)
500 static int inet6_netconf_msgsize_devconf(int type)
502 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
503 + nla_total_size(4); /* NETCONFA_IFINDEX */
506 if (type == NETCONFA_ALL)
509 if (all || type == NETCONFA_FORWARDING)
510 size += nla_total_size(4);
511 #ifdef CONFIG_IPV6_MROUTE
512 if (all || type == NETCONFA_MC_FORWARDING)
513 size += nla_total_size(4);
515 if (all || type == NETCONFA_PROXY_NEIGH)
516 size += nla_total_size(4);
518 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
519 size += nla_total_size(4);
524 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
525 struct ipv6_devconf *devconf, u32 portid,
526 u32 seq, int event, unsigned int flags,
529 struct nlmsghdr *nlh;
530 struct netconfmsg *ncm;
533 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
538 if (type == NETCONFA_ALL)
541 ncm = nlmsg_data(nlh);
542 ncm->ncm_family = AF_INET6;
544 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
545 goto nla_put_failure;
547 if ((all || type == NETCONFA_FORWARDING) &&
548 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
549 goto nla_put_failure;
550 #ifdef CONFIG_IPV6_MROUTE
551 if ((all || type == NETCONFA_MC_FORWARDING) &&
552 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
553 devconf->mc_forwarding) < 0)
554 goto nla_put_failure;
556 if ((all || type == NETCONFA_PROXY_NEIGH) &&
557 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
558 goto nla_put_failure;
560 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
561 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
562 devconf->ignore_routes_with_linkdown) < 0)
563 goto nla_put_failure;
569 nlmsg_cancel(skb, nlh);
573 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
574 struct ipv6_devconf *devconf)
579 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
583 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
584 RTM_NEWNETCONF, 0, type);
586 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
587 WARN_ON(err == -EMSGSIZE);
591 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
594 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
597 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
598 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
599 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
600 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
601 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
604 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
605 struct nlmsghdr *nlh)
607 struct net *net = sock_net(in_skb->sk);
608 struct nlattr *tb[NETCONFA_MAX+1];
609 struct netconfmsg *ncm;
611 struct ipv6_devconf *devconf;
612 struct inet6_dev *in6_dev;
613 struct net_device *dev;
617 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
618 devconf_ipv6_policy);
623 if (!tb[NETCONFA_IFINDEX])
626 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
628 case NETCONFA_IFINDEX_ALL:
629 devconf = net->ipv6.devconf_all;
631 case NETCONFA_IFINDEX_DEFAULT:
632 devconf = net->ipv6.devconf_dflt;
635 dev = __dev_get_by_index(net, ifindex);
638 in6_dev = __in6_dev_get(dev);
641 devconf = &in6_dev->cnf;
646 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
650 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
651 NETLINK_CB(in_skb).portid,
652 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
655 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
656 WARN_ON(err == -EMSGSIZE);
660 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
665 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
666 struct netlink_callback *cb)
668 struct net *net = sock_net(skb->sk);
671 struct net_device *dev;
672 struct inet6_dev *idev;
673 struct hlist_head *head;
676 s_idx = idx = cb->args[1];
678 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
680 head = &net->dev_index_head[h];
682 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
684 hlist_for_each_entry_rcu(dev, head, index_hlist) {
687 idev = __in6_dev_get(dev);
691 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
693 NETLINK_CB(cb->skb).portid,
701 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
707 if (h == NETDEV_HASHENTRIES) {
708 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
709 net->ipv6.devconf_all,
710 NETLINK_CB(cb->skb).portid,
712 RTM_NEWNETCONF, NLM_F_MULTI,
718 if (h == NETDEV_HASHENTRIES + 1) {
719 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
720 net->ipv6.devconf_dflt,
721 NETLINK_CB(cb->skb).portid,
723 RTM_NEWNETCONF, NLM_F_MULTI,
737 static void dev_forward_change(struct inet6_dev *idev)
739 struct net_device *dev;
740 struct inet6_ifaddr *ifa;
745 if (idev->cnf.forwarding)
746 dev_disable_lro(dev);
747 if (dev->flags & IFF_MULTICAST) {
748 if (idev->cnf.forwarding) {
749 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
750 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
751 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
753 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
754 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
755 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
759 list_for_each_entry(ifa, &idev->addr_list, if_list) {
760 if (ifa->flags&IFA_F_TENTATIVE)
762 if (idev->cnf.forwarding)
763 addrconf_join_anycast(ifa);
765 addrconf_leave_anycast(ifa);
767 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
768 dev->ifindex, &idev->cnf);
772 static void addrconf_forward_change(struct net *net, __s32 newf)
774 struct net_device *dev;
775 struct inet6_dev *idev;
777 for_each_netdev(net, dev) {
778 idev = __in6_dev_get(dev);
780 int changed = (!idev->cnf.forwarding) ^ (!newf);
781 idev->cnf.forwarding = newf;
783 dev_forward_change(idev);
788 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
794 return restart_syscall();
796 net = (struct net *)table->extra2;
800 if (p == &net->ipv6.devconf_dflt->forwarding) {
801 if ((!newf) ^ (!old))
802 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
803 NETCONFA_IFINDEX_DEFAULT,
804 net->ipv6.devconf_dflt);
809 if (p == &net->ipv6.devconf_all->forwarding) {
810 int old_dflt = net->ipv6.devconf_dflt->forwarding;
812 net->ipv6.devconf_dflt->forwarding = newf;
813 if ((!newf) ^ (!old_dflt))
814 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
815 NETCONFA_IFINDEX_DEFAULT,
816 net->ipv6.devconf_dflt);
818 addrconf_forward_change(net, newf);
819 if ((!newf) ^ (!old))
820 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
821 NETCONFA_IFINDEX_ALL,
822 net->ipv6.devconf_all);
823 } else if ((!newf) ^ (!old))
824 dev_forward_change((struct inet6_dev *)table->extra1);
828 rt6_purge_dflt_routers(net);
832 static void addrconf_linkdown_change(struct net *net, __s32 newf)
834 struct net_device *dev;
835 struct inet6_dev *idev;
837 for_each_netdev(net, dev) {
838 idev = __in6_dev_get(dev);
840 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
842 idev->cnf.ignore_routes_with_linkdown = newf;
844 inet6_netconf_notify_devconf(dev_net(dev),
845 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
852 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
858 return restart_syscall();
860 net = (struct net *)table->extra2;
864 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
865 if ((!newf) ^ (!old))
866 inet6_netconf_notify_devconf(net,
867 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
868 NETCONFA_IFINDEX_DEFAULT,
869 net->ipv6.devconf_dflt);
874 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
875 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
876 addrconf_linkdown_change(net, newf);
877 if ((!newf) ^ (!old))
878 inet6_netconf_notify_devconf(net,
879 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
880 NETCONFA_IFINDEX_ALL,
881 net->ipv6.devconf_all);
890 /* Nobody refers to this ifaddr, destroy it */
891 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
893 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
895 #ifdef NET_REFCNT_DEBUG
896 pr_debug("%s\n", __func__);
899 in6_dev_put(ifp->idev);
901 if (cancel_delayed_work(&ifp->dad_work))
902 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
905 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
906 pr_warn("Freeing alive inet6 address %p\n", ifp);
915 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
918 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
921 * Each device address list is sorted in order of scope -
922 * global before linklocal.
924 list_for_each(p, &idev->addr_list) {
925 struct inet6_ifaddr *ifa
926 = list_entry(p, struct inet6_ifaddr, if_list);
927 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
931 list_add_tail(&ifp->if_list, p);
934 static u32 inet6_addr_hash(const struct in6_addr *addr)
936 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
939 /* On success it returns ifp with increased reference count */
941 static struct inet6_ifaddr *
942 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
943 const struct in6_addr *peer_addr, int pfxlen,
944 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
946 struct inet6_ifaddr *ifa = NULL;
950 int addr_type = ipv6_addr_type(addr);
952 if (addr_type == IPV6_ADDR_ANY ||
953 addr_type & IPV6_ADDR_MULTICAST ||
954 (!(idev->dev->flags & IFF_LOOPBACK) &&
955 addr_type & IPV6_ADDR_LOOPBACK))
956 return ERR_PTR(-EADDRNOTAVAIL);
960 err = -ENODEV; /*XXX*/
964 if (idev->cnf.disable_ipv6) {
969 spin_lock(&addrconf_hash_lock);
971 /* Ignore adding duplicate addresses on an interface */
972 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
973 ADBG("ipv6_add_addr: already assigned\n");
978 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
981 ADBG("ipv6_add_addr: malloc failed\n");
986 rt = addrconf_dst_alloc(idev, addr, false);
992 neigh_parms_data_state_setall(idev->nd_parms);
996 ifa->peer_addr = *peer_addr;
998 spin_lock_init(&ifa->lock);
999 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1000 INIT_HLIST_NODE(&ifa->addr_lst);
1002 ifa->prefix_len = pfxlen;
1003 ifa->flags = flags | IFA_F_TENTATIVE;
1004 ifa->valid_lft = valid_lft;
1005 ifa->prefered_lft = prefered_lft;
1006 ifa->cstamp = ifa->tstamp = jiffies;
1007 ifa->tokenized = false;
1016 /* Add to big hash table */
1017 hash = inet6_addr_hash(addr);
1019 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1020 spin_unlock(&addrconf_hash_lock);
1022 write_lock(&idev->lock);
1023 /* Add to inet6_dev unicast addr list. */
1024 ipv6_link_dev_addr(idev, ifa);
1026 if (ifa->flags&IFA_F_TEMPORARY) {
1027 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1032 write_unlock(&idev->lock);
1034 rcu_read_unlock_bh();
1036 if (likely(err == 0))
1037 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1045 spin_unlock(&addrconf_hash_lock);
1049 enum cleanup_prefix_rt_t {
1050 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1051 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1052 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1056 * Check, whether the prefix for ifp would still need a prefix route
1057 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1060 * 1) we don't purge prefix if address was not permanent.
1061 * prefix is managed by its own lifetime.
1062 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1063 * 3) if there are no addresses, delete prefix.
1064 * 4) if there are still other permanent address(es),
1065 * corresponding prefix is still permanent.
1066 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1067 * don't purge the prefix, assume user space is managing it.
1068 * 6) otherwise, update prefix lifetime to the
1069 * longest valid lifetime among the corresponding
1070 * addresses on the device.
1071 * Note: subsequent RA will update lifetime.
1073 static enum cleanup_prefix_rt_t
1074 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1076 struct inet6_ifaddr *ifa;
1077 struct inet6_dev *idev = ifp->idev;
1078 unsigned long lifetime;
1079 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1083 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1086 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1089 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1090 return CLEANUP_PREFIX_RT_NOP;
1092 action = CLEANUP_PREFIX_RT_EXPIRE;
1094 spin_lock(&ifa->lock);
1096 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1098 * Note: Because this address is
1099 * not permanent, lifetime <
1100 * LONG_MAX / HZ here.
1102 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1103 *expires = ifa->tstamp + lifetime * HZ;
1104 spin_unlock(&ifa->lock);
1111 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1113 struct rt6_info *rt;
1115 rt = addrconf_get_prefix_route(&ifp->addr,
1118 0, RTF_GATEWAY | RTF_DEFAULT);
1123 if (!(rt->rt6i_flags & RTF_EXPIRES))
1124 rt6_set_expires(rt, expires);
1131 /* This function wants to get referenced ifp and releases it before return */
1133 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1136 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1137 unsigned long expires;
1141 spin_lock_bh(&ifp->lock);
1143 ifp->state = INET6_IFADDR_STATE_DEAD;
1144 spin_unlock_bh(&ifp->lock);
1146 if (state == INET6_IFADDR_STATE_DEAD)
1149 spin_lock_bh(&addrconf_hash_lock);
1150 hlist_del_init_rcu(&ifp->addr_lst);
1151 spin_unlock_bh(&addrconf_hash_lock);
1153 write_lock_bh(&ifp->idev->lock);
1155 if (ifp->flags&IFA_F_TEMPORARY) {
1156 list_del(&ifp->tmp_list);
1158 in6_ifa_put(ifp->ifpub);
1164 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1165 action = check_cleanup_prefix_route(ifp, &expires);
1167 list_del_init(&ifp->if_list);
1170 write_unlock_bh(&ifp->idev->lock);
1172 addrconf_del_dad_work(ifp);
1174 ipv6_ifa_notify(RTM_DELADDR, ifp);
1176 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1178 if (action != CLEANUP_PREFIX_RT_NOP) {
1179 cleanup_prefix_route(ifp, expires,
1180 action == CLEANUP_PREFIX_RT_DEL);
1183 /* clean up prefsrc entries */
1184 rt6_remove_prefsrc(ifp);
1189 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1191 struct inet6_dev *idev = ifp->idev;
1192 struct in6_addr addr, *tmpaddr;
1193 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1194 unsigned long regen_advance;
1198 unsigned long now = jiffies;
1199 long max_desync_factor;
1200 s32 cnf_temp_preferred_lft;
1202 write_lock_bh(&idev->lock);
1204 spin_lock_bh(&ift->lock);
1205 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1206 spin_unlock_bh(&ift->lock);
1213 if (idev->cnf.use_tempaddr <= 0) {
1214 write_unlock_bh(&idev->lock);
1215 pr_info("%s: use_tempaddr is disabled\n", __func__);
1220 spin_lock_bh(&ifp->lock);
1221 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1222 idev->cnf.use_tempaddr = -1; /*XXX*/
1223 spin_unlock_bh(&ifp->lock);
1224 write_unlock_bh(&idev->lock);
1225 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1232 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1233 ipv6_try_regen_rndid(idev, tmpaddr);
1234 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1235 age = (now - ifp->tstamp) / HZ;
1237 regen_advance = idev->cnf.regen_max_retry *
1238 idev->cnf.dad_transmits *
1239 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1241 /* recalculate max_desync_factor each time and update
1242 * idev->desync_factor if it's larger
1244 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1245 max_desync_factor = min_t(__u32,
1246 idev->cnf.max_desync_factor,
1247 cnf_temp_preferred_lft - regen_advance);
1249 if (unlikely(idev->desync_factor > max_desync_factor)) {
1250 if (max_desync_factor > 0) {
1251 get_random_bytes(&idev->desync_factor,
1252 sizeof(idev->desync_factor));
1253 idev->desync_factor %= max_desync_factor;
1255 idev->desync_factor = 0;
1259 tmp_valid_lft = min_t(__u32,
1261 idev->cnf.temp_valid_lft + age);
1262 tmp_prefered_lft = cnf_temp_preferred_lft + age -
1263 idev->desync_factor;
1264 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1265 tmp_plen = ifp->prefix_len;
1266 tmp_tstamp = ifp->tstamp;
1267 spin_unlock_bh(&ifp->lock);
1269 write_unlock_bh(&idev->lock);
1271 /* A temporary address is created only if this calculated Preferred
1272 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1273 * an implementation must not create a temporary address with a zero
1274 * Preferred Lifetime.
1275 * Use age calculation as in addrconf_verify to avoid unnecessary
1276 * temporary addresses being generated.
1278 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1279 if (tmp_prefered_lft <= regen_advance + age) {
1286 addr_flags = IFA_F_TEMPORARY;
1287 /* set in addrconf_prefix_rcv() */
1288 if (ifp->flags & IFA_F_OPTIMISTIC)
1289 addr_flags |= IFA_F_OPTIMISTIC;
1291 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1292 ipv6_addr_scope(&addr), addr_flags,
1293 tmp_valid_lft, tmp_prefered_lft);
1297 pr_info("%s: retry temporary address regeneration\n", __func__);
1299 write_lock_bh(&idev->lock);
1303 spin_lock_bh(&ift->lock);
1306 ift->tstamp = tmp_tstamp;
1307 spin_unlock_bh(&ift->lock);
1309 addrconf_dad_start(ift);
1317 * Choose an appropriate source address (RFC3484)
1320 IPV6_SADDR_RULE_INIT = 0,
1321 IPV6_SADDR_RULE_LOCAL,
1322 IPV6_SADDR_RULE_SCOPE,
1323 IPV6_SADDR_RULE_PREFERRED,
1324 #ifdef CONFIG_IPV6_MIP6
1325 IPV6_SADDR_RULE_HOA,
1327 IPV6_SADDR_RULE_OIF,
1328 IPV6_SADDR_RULE_LABEL,
1329 IPV6_SADDR_RULE_PRIVACY,
1330 IPV6_SADDR_RULE_ORCHID,
1331 IPV6_SADDR_RULE_PREFIX,
1332 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1333 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1338 struct ipv6_saddr_score {
1341 struct inet6_ifaddr *ifa;
1342 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1347 struct ipv6_saddr_dst {
1348 const struct in6_addr *addr;
1355 static inline int ipv6_saddr_preferred(int type)
1357 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1362 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1364 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1365 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1371 static int ipv6_get_saddr_eval(struct net *net,
1372 struct ipv6_saddr_score *score,
1373 struct ipv6_saddr_dst *dst,
1378 if (i <= score->rule) {
1380 case IPV6_SADDR_RULE_SCOPE:
1381 ret = score->scopedist;
1383 case IPV6_SADDR_RULE_PREFIX:
1384 ret = score->matchlen;
1387 ret = !!test_bit(i, score->scorebits);
1393 case IPV6_SADDR_RULE_INIT:
1394 /* Rule 0: remember if hiscore is not ready yet */
1397 case IPV6_SADDR_RULE_LOCAL:
1398 /* Rule 1: Prefer same address */
1399 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1401 case IPV6_SADDR_RULE_SCOPE:
1402 /* Rule 2: Prefer appropriate scope
1407 * ---+--+-+---> scope
1409 * | d is scope of the destination.
1411 * | \ <- smaller scope is better if
1412 * B-15 | \ if scope is enough for destination.
1413 * | ret = B - scope (-1 <= scope >= d <= 15).
1415 * |/ <- greater is better
1416 * -C / if scope is not enough for destination.
1417 * /| ret = scope - C (-1 <= d < scope <= 15).
1419 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1420 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1421 * Assume B = 0 and we get C > 29.
1423 ret = __ipv6_addr_src_scope(score->addr_type);
1424 if (ret >= dst->scope)
1427 ret -= 128; /* 30 is enough */
1428 score->scopedist = ret;
1430 case IPV6_SADDR_RULE_PREFERRED:
1432 /* Rule 3: Avoid deprecated and optimistic addresses */
1433 u8 avoid = IFA_F_DEPRECATED;
1435 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1436 avoid |= IFA_F_OPTIMISTIC;
1437 ret = ipv6_saddr_preferred(score->addr_type) ||
1438 !(score->ifa->flags & avoid);
1441 #ifdef CONFIG_IPV6_MIP6
1442 case IPV6_SADDR_RULE_HOA:
1444 /* Rule 4: Prefer home address */
1445 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1446 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1450 case IPV6_SADDR_RULE_OIF:
1451 /* Rule 5: Prefer outgoing interface */
1452 ret = (!dst->ifindex ||
1453 dst->ifindex == score->ifa->idev->dev->ifindex);
1455 case IPV6_SADDR_RULE_LABEL:
1456 /* Rule 6: Prefer matching label */
1457 ret = ipv6_addr_label(net,
1458 &score->ifa->addr, score->addr_type,
1459 score->ifa->idev->dev->ifindex) == dst->label;
1461 case IPV6_SADDR_RULE_PRIVACY:
1463 /* Rule 7: Prefer public address
1464 * Note: prefer temporary address if use_tempaddr >= 2
1466 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1467 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1468 score->ifa->idev->cnf.use_tempaddr >= 2;
1469 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1472 case IPV6_SADDR_RULE_ORCHID:
1473 /* Rule 8-: Prefer ORCHID vs ORCHID or
1474 * non-ORCHID vs non-ORCHID
1476 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1477 ipv6_addr_orchid(dst->addr));
1479 case IPV6_SADDR_RULE_PREFIX:
1480 /* Rule 8: Use longest matching prefix */
1481 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1482 if (ret > score->ifa->prefix_len)
1483 ret = score->ifa->prefix_len;
1484 score->matchlen = ret;
1486 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1487 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1488 /* Optimistic addresses still have lower precedence than other
1489 * preferred addresses.
1491 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1499 __set_bit(i, score->scorebits);
1505 static int __ipv6_dev_get_saddr(struct net *net,
1506 struct ipv6_saddr_dst *dst,
1507 struct inet6_dev *idev,
1508 struct ipv6_saddr_score *scores,
1511 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1513 read_lock_bh(&idev->lock);
1514 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1518 * - Tentative Address (RFC2462 section 5.4)
1519 * - A tentative address is not considered
1520 * "assigned to an interface" in the traditional
1521 * sense, unless it is also flagged as optimistic.
1522 * - Candidate Source Address (section 4)
1523 * - In any case, anycast addresses, multicast
1524 * addresses, and the unspecified address MUST
1525 * NOT be included in a candidate set.
1527 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1528 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1531 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1533 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1534 score->addr_type & IPV6_ADDR_MULTICAST)) {
1535 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1541 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1543 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1544 int minihiscore, miniscore;
1546 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1547 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1549 if (minihiscore > miniscore) {
1550 if (i == IPV6_SADDR_RULE_SCOPE &&
1551 score->scopedist > 0) {
1554 * each remaining entry
1555 * has too small (not enough)
1556 * scope, because ifa entries
1557 * are sorted by their scope
1563 } else if (minihiscore < miniscore) {
1565 in6_ifa_put(hiscore->ifa);
1567 in6_ifa_hold(score->ifa);
1569 swap(hiscore, score);
1570 hiscore_idx = 1 - hiscore_idx;
1572 /* restore our iterator */
1573 score->ifa = hiscore->ifa;
1580 read_unlock_bh(&idev->lock);
1584 static int ipv6_get_saddr_master(struct net *net,
1585 const struct net_device *dst_dev,
1586 const struct net_device *master,
1587 struct ipv6_saddr_dst *dst,
1588 struct ipv6_saddr_score *scores,
1591 struct inet6_dev *idev;
1593 idev = __in6_dev_get(dst_dev);
1595 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1596 scores, hiscore_idx);
1598 idev = __in6_dev_get(master);
1600 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1601 scores, hiscore_idx);
1606 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1607 const struct in6_addr *daddr, unsigned int prefs,
1608 struct in6_addr *saddr)
1610 struct ipv6_saddr_score scores[2], *hiscore;
1611 struct ipv6_saddr_dst dst;
1612 struct inet6_dev *idev;
1613 struct net_device *dev;
1615 bool use_oif_addr = false;
1616 int hiscore_idx = 0;
1618 dst_type = __ipv6_addr_type(daddr);
1620 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1621 dst.scope = __ipv6_addr_src_scope(dst_type);
1622 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1625 scores[hiscore_idx].rule = -1;
1626 scores[hiscore_idx].ifa = NULL;
1630 /* Candidate Source Address (section 4)
1631 * - multicast and link-local destination address,
1632 * the set of candidate source address MUST only
1633 * include addresses assigned to interfaces
1634 * belonging to the same link as the outgoing
1636 * (- For site-local destination addresses, the
1637 * set of candidate source addresses MUST only
1638 * include addresses assigned to interfaces
1639 * belonging to the same site as the outgoing
1641 * - "It is RECOMMENDED that the candidate source addresses
1642 * be the set of unicast addresses assigned to the
1643 * interface that will be used to send to the destination
1644 * (the 'outgoing' interface)." (RFC 6724)
1647 idev = __in6_dev_get(dst_dev);
1648 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1649 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1650 (idev && idev->cnf.use_oif_addrs_only)) {
1651 use_oif_addr = true;
1657 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1659 const struct net_device *master;
1662 /* if dst_dev exists and is enslaved to an L3 device, then
1663 * prefer addresses from dst_dev and then the master over
1664 * any other enslaved devices in the L3 domain.
1666 master = l3mdev_master_dev_rcu(dst_dev);
1668 master_idx = master->ifindex;
1670 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1672 scores, hiscore_idx);
1674 if (scores[hiscore_idx].ifa)
1678 for_each_netdev_rcu(net, dev) {
1679 /* only consider addresses on devices in the
1682 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1684 idev = __in6_dev_get(dev);
1687 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1694 hiscore = &scores[hiscore_idx];
1696 return -EADDRNOTAVAIL;
1698 *saddr = hiscore->ifa->addr;
1699 in6_ifa_put(hiscore->ifa);
1702 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1704 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1707 struct inet6_ifaddr *ifp;
1708 int err = -EADDRNOTAVAIL;
1710 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1711 if (ifp->scope > IFA_LINK)
1713 if (ifp->scope == IFA_LINK &&
1714 !(ifp->flags & banned_flags)) {
1723 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1726 struct inet6_dev *idev;
1727 int err = -EADDRNOTAVAIL;
1730 idev = __in6_dev_get(dev);
1732 read_lock_bh(&idev->lock);
1733 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1734 read_unlock_bh(&idev->lock);
1740 static int ipv6_count_addresses(struct inet6_dev *idev)
1743 struct inet6_ifaddr *ifp;
1745 read_lock_bh(&idev->lock);
1746 list_for_each_entry(ifp, &idev->addr_list, if_list)
1748 read_unlock_bh(&idev->lock);
1752 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1753 const struct net_device *dev, int strict)
1755 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1757 EXPORT_SYMBOL(ipv6_chk_addr);
1759 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1760 const struct net_device *dev, int strict,
1763 struct inet6_ifaddr *ifp;
1764 unsigned int hash = inet6_addr_hash(addr);
1768 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1769 if (!net_eq(dev_net(ifp->idev->dev), net))
1771 /* Decouple optimistic from tentative for evaluation here.
1772 * Ban optimistic addresses explicitly, when required.
1774 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1775 ? (ifp->flags&~IFA_F_TENTATIVE)
1777 if (ipv6_addr_equal(&ifp->addr, addr) &&
1778 !(ifp_flags&banned_flags) &&
1779 (!dev || ifp->idev->dev == dev ||
1780 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1781 rcu_read_unlock_bh();
1786 rcu_read_unlock_bh();
1789 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1791 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1792 struct net_device *dev)
1794 unsigned int hash = inet6_addr_hash(addr);
1795 struct inet6_ifaddr *ifp;
1797 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1798 if (!net_eq(dev_net(ifp->idev->dev), net))
1800 if (ipv6_addr_equal(&ifp->addr, addr)) {
1801 if (!dev || ifp->idev->dev == dev)
1808 /* Compares an address/prefix_len with addresses on device @dev.
1809 * If one is found it returns true.
1811 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1812 const unsigned int prefix_len, struct net_device *dev)
1814 struct inet6_dev *idev;
1815 struct inet6_ifaddr *ifa;
1819 idev = __in6_dev_get(dev);
1821 read_lock_bh(&idev->lock);
1822 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1823 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1827 read_unlock_bh(&idev->lock);
1833 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1835 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1837 struct inet6_dev *idev;
1838 struct inet6_ifaddr *ifa;
1843 idev = __in6_dev_get(dev);
1845 read_lock_bh(&idev->lock);
1846 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1847 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1852 read_unlock_bh(&idev->lock);
1857 EXPORT_SYMBOL(ipv6_chk_prefix);
1859 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1860 struct net_device *dev, int strict)
1862 struct inet6_ifaddr *ifp, *result = NULL;
1863 unsigned int hash = inet6_addr_hash(addr);
1866 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1867 if (!net_eq(dev_net(ifp->idev->dev), net))
1869 if (ipv6_addr_equal(&ifp->addr, addr)) {
1870 if (!dev || ifp->idev->dev == dev ||
1871 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1878 rcu_read_unlock_bh();
1883 /* Gets referenced address, destroys ifaddr */
1885 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1888 ifp->flags |= IFA_F_DADFAILED;
1890 if (ifp->flags&IFA_F_PERMANENT) {
1891 spin_lock_bh(&ifp->lock);
1892 addrconf_del_dad_work(ifp);
1893 ifp->flags |= IFA_F_TENTATIVE;
1894 spin_unlock_bh(&ifp->lock);
1896 ipv6_ifa_notify(0, ifp);
1898 } else if (ifp->flags&IFA_F_TEMPORARY) {
1899 struct inet6_ifaddr *ifpub;
1900 spin_lock_bh(&ifp->lock);
1903 in6_ifa_hold(ifpub);
1904 spin_unlock_bh(&ifp->lock);
1905 ipv6_create_tempaddr(ifpub, ifp);
1908 spin_unlock_bh(&ifp->lock);
1916 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1920 spin_lock_bh(&ifp->lock);
1921 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1922 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1925 spin_unlock_bh(&ifp->lock);
1930 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1932 struct inet6_dev *idev = ifp->idev;
1933 struct net *net = dev_net(ifp->idev->dev);
1935 if (addrconf_dad_end(ifp)) {
1940 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1941 ifp->idev->dev->name, &ifp->addr);
1943 spin_lock_bh(&ifp->lock);
1945 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1946 int scope = ifp->scope;
1947 u32 flags = ifp->flags;
1948 struct in6_addr new_addr;
1949 struct inet6_ifaddr *ifp2;
1950 u32 valid_lft, preferred_lft;
1951 int pfxlen = ifp->prefix_len;
1952 int retries = ifp->stable_privacy_retry + 1;
1954 if (retries > net->ipv6.sysctl.idgen_retries) {
1955 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1956 ifp->idev->dev->name);
1960 new_addr = ifp->addr;
1961 if (ipv6_generate_stable_address(&new_addr, retries,
1965 valid_lft = ifp->valid_lft;
1966 preferred_lft = ifp->prefered_lft;
1968 spin_unlock_bh(&ifp->lock);
1970 if (idev->cnf.max_addresses &&
1971 ipv6_count_addresses(idev) >=
1972 idev->cnf.max_addresses)
1975 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1976 ifp->idev->dev->name);
1978 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1979 scope, flags, valid_lft,
1984 spin_lock_bh(&ifp2->lock);
1985 ifp2->stable_privacy_retry = retries;
1986 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1987 spin_unlock_bh(&ifp2->lock);
1989 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1992 spin_lock_bh(&ifp->lock);
1996 /* transition from _POSTDAD to _ERRDAD */
1997 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1998 spin_unlock_bh(&ifp->lock);
2000 addrconf_mod_dad_work(ifp, 0);
2004 /* Join to solicited addr multicast group.
2005 * caller must hold RTNL */
2006 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2008 struct in6_addr maddr;
2010 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2013 addrconf_addr_solict_mult(addr, &maddr);
2014 ipv6_dev_mc_inc(dev, &maddr);
2017 /* caller must hold RTNL */
2018 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2020 struct in6_addr maddr;
2022 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2025 addrconf_addr_solict_mult(addr, &maddr);
2026 __ipv6_dev_mc_dec(idev, &maddr);
2029 /* caller must hold RTNL */
2030 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2032 struct in6_addr addr;
2034 if (ifp->prefix_len >= 127) /* RFC 6164 */
2036 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2037 if (ipv6_addr_any(&addr))
2039 __ipv6_dev_ac_inc(ifp->idev, &addr);
2042 /* caller must hold RTNL */
2043 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2045 struct in6_addr addr;
2047 if (ifp->prefix_len >= 127) /* RFC 6164 */
2049 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2050 if (ipv6_addr_any(&addr))
2052 __ipv6_dev_ac_dec(ifp->idev, &addr);
2055 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2057 if (dev->addr_len != EUI64_ADDR_LEN)
2059 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2064 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2066 union fwnet_hwaddr *ha;
2068 if (dev->addr_len != FWNET_ALEN)
2071 ha = (union fwnet_hwaddr *)dev->dev_addr;
2073 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2078 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2080 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2081 if (dev->addr_len != ARCNET_ALEN)
2084 eui[7] = *(u8 *)dev->dev_addr;
2088 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2090 if (dev->addr_len != INFINIBAND_ALEN)
2092 memcpy(eui, dev->dev_addr + 12, 8);
2097 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2101 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2102 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2103 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2104 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2105 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2106 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2110 memcpy(eui + 4, &addr, 4);
2114 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2116 if (dev->priv_flags & IFF_ISATAP)
2117 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2121 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2123 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2126 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2128 memcpy(eui, dev->perm_addr, 3);
2129 memcpy(eui + 5, dev->perm_addr + 3, 3);
2136 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2138 switch (dev->type) {
2141 return addrconf_ifid_eui48(eui, dev);
2143 return addrconf_ifid_arcnet(eui, dev);
2144 case ARPHRD_INFINIBAND:
2145 return addrconf_ifid_infiniband(eui, dev);
2147 return addrconf_ifid_sit(eui, dev);
2150 return addrconf_ifid_gre(eui, dev);
2151 case ARPHRD_6LOWPAN:
2152 return addrconf_ifid_eui64(eui, dev);
2153 case ARPHRD_IEEE1394:
2154 return addrconf_ifid_ieee1394(eui, dev);
2155 case ARPHRD_TUNNEL6:
2157 return addrconf_ifid_ip6tnl(eui, dev);
2162 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2165 struct inet6_ifaddr *ifp;
2167 read_lock_bh(&idev->lock);
2168 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2169 if (ifp->scope > IFA_LINK)
2171 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2172 memcpy(eui, ifp->addr.s6_addr+8, 8);
2177 read_unlock_bh(&idev->lock);
2181 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2182 static void ipv6_regen_rndid(struct inet6_dev *idev)
2185 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2186 idev->rndid[0] &= ~0x02;
2189 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2190 * check if generated address is not inappropriate
2192 * - Reserved subnet anycast (RFC 2526)
2193 * 11111101 11....11 1xxxxxxx
2194 * - ISATAP (RFC4214) 6.1
2195 * 00-00-5E-FE-xx-xx-xx-xx
2197 * - XXX: already assigned to an address on the device
2199 if (idev->rndid[0] == 0xfd &&
2200 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2201 (idev->rndid[7]&0x80))
2203 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2204 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2206 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2211 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2213 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2214 ipv6_regen_rndid(idev);
2222 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2223 unsigned long expires, u32 flags)
2225 struct fib6_config cfg = {
2226 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2227 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2228 .fc_ifindex = dev->ifindex,
2229 .fc_expires = expires,
2231 .fc_flags = RTF_UP | flags,
2232 .fc_nlinfo.nl_net = dev_net(dev),
2233 .fc_protocol = RTPROT_KERNEL,
2238 /* Prevent useless cloning on PtP SIT.
2239 This thing is done here expecting that the whole
2240 class of non-broadcast devices need not cloning.
2242 #if IS_ENABLED(CONFIG_IPV6_SIT)
2243 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2244 cfg.fc_flags |= RTF_NONEXTHOP;
2247 ip6_route_add(&cfg);
2251 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2253 const struct net_device *dev,
2254 u32 flags, u32 noflags)
2256 struct fib6_node *fn;
2257 struct rt6_info *rt = NULL;
2258 struct fib6_table *table;
2259 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2261 table = fib6_get_table(dev_net(dev), tb_id);
2265 read_lock_bh(&table->tb6_lock);
2266 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2270 noflags |= RTF_CACHE;
2271 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2272 if (rt->dst.dev->ifindex != dev->ifindex)
2274 if ((rt->rt6i_flags & flags) != flags)
2276 if ((rt->rt6i_flags & noflags) != 0)
2282 read_unlock_bh(&table->tb6_lock);
2287 /* Create "default" multicast route to the interface */
2289 static void addrconf_add_mroute(struct net_device *dev)
2291 struct fib6_config cfg = {
2292 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2293 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2294 .fc_ifindex = dev->ifindex,
2297 .fc_nlinfo.nl_net = dev_net(dev),
2300 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2302 ip6_route_add(&cfg);
2305 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2307 struct inet6_dev *idev;
2311 idev = ipv6_find_idev(dev);
2313 return ERR_PTR(-ENOBUFS);
2315 if (idev->cnf.disable_ipv6)
2316 return ERR_PTR(-EACCES);
2318 /* Add default multicast route */
2319 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2320 addrconf_add_mroute(dev);
2325 static void manage_tempaddrs(struct inet6_dev *idev,
2326 struct inet6_ifaddr *ifp,
2327 __u32 valid_lft, __u32 prefered_lft,
2328 bool create, unsigned long now)
2331 struct inet6_ifaddr *ift;
2333 read_lock_bh(&idev->lock);
2334 /* update all temporary addresses in the list */
2335 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2336 int age, max_valid, max_prefered;
2338 if (ifp != ift->ifpub)
2341 /* RFC 4941 section 3.3:
2342 * If a received option will extend the lifetime of a public
2343 * address, the lifetimes of temporary addresses should
2344 * be extended, subject to the overall constraint that no
2345 * temporary addresses should ever remain "valid" or "preferred"
2346 * for a time longer than (TEMP_VALID_LIFETIME) or
2347 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2349 age = (now - ift->cstamp) / HZ;
2350 max_valid = idev->cnf.temp_valid_lft - age;
2354 max_prefered = idev->cnf.temp_prefered_lft -
2355 idev->desync_factor - age;
2356 if (max_prefered < 0)
2359 if (valid_lft > max_valid)
2360 valid_lft = max_valid;
2362 if (prefered_lft > max_prefered)
2363 prefered_lft = max_prefered;
2365 spin_lock(&ift->lock);
2367 ift->valid_lft = valid_lft;
2368 ift->prefered_lft = prefered_lft;
2370 if (prefered_lft > 0)
2371 ift->flags &= ~IFA_F_DEPRECATED;
2373 spin_unlock(&ift->lock);
2374 if (!(flags&IFA_F_TENTATIVE))
2375 ipv6_ifa_notify(0, ift);
2378 if ((create || list_empty(&idev->tempaddr_list)) &&
2379 idev->cnf.use_tempaddr > 0) {
2380 /* When a new public address is created as described
2381 * in [ADDRCONF], also create a new temporary address.
2382 * Also create a temporary address if it's enabled but
2383 * no temporary address currently exists.
2385 read_unlock_bh(&idev->lock);
2386 ipv6_create_tempaddr(ifp, NULL);
2388 read_unlock_bh(&idev->lock);
2392 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2394 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2395 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2398 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2399 const struct prefix_info *pinfo,
2400 struct inet6_dev *in6_dev,
2401 const struct in6_addr *addr, int addr_type,
2402 u32 addr_flags, bool sllao, bool tokenized,
2403 __u32 valid_lft, u32 prefered_lft)
2405 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2406 int create = 0, update_lft = 0;
2408 if (!ifp && valid_lft) {
2409 int max_addresses = in6_dev->cnf.max_addresses;
2411 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2412 if (in6_dev->cnf.optimistic_dad &&
2413 !net->ipv6.devconf_all->forwarding && sllao)
2414 addr_flags |= IFA_F_OPTIMISTIC;
2417 /* Do not allow to create too much of autoconfigured
2418 * addresses; this would be too easy way to crash kernel.
2420 if (!max_addresses ||
2421 ipv6_count_addresses(in6_dev) < max_addresses)
2422 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2424 addr_type&IPV6_ADDR_SCOPE_MASK,
2425 addr_flags, valid_lft,
2428 if (IS_ERR_OR_NULL(ifp))
2433 spin_lock_bh(&ifp->lock);
2434 ifp->flags |= IFA_F_MANAGETEMPADDR;
2435 ifp->cstamp = jiffies;
2436 ifp->tokenized = tokenized;
2437 spin_unlock_bh(&ifp->lock);
2438 addrconf_dad_start(ifp);
2446 /* update lifetime (RFC2462 5.5.3 e) */
2447 spin_lock_bh(&ifp->lock);
2449 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2450 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2453 if (!update_lft && !create && stored_lft) {
2454 const u32 minimum_lft = min_t(u32,
2455 stored_lft, MIN_VALID_LIFETIME);
2456 valid_lft = max(valid_lft, minimum_lft);
2458 /* RFC4862 Section 5.5.3e:
2459 * "Note that the preferred lifetime of the
2460 * corresponding address is always reset to
2461 * the Preferred Lifetime in the received
2462 * Prefix Information option, regardless of
2463 * whether the valid lifetime is also reset or
2466 * So we should always update prefered_lft here.
2472 ifp->valid_lft = valid_lft;
2473 ifp->prefered_lft = prefered_lft;
2476 ifp->flags &= ~IFA_F_DEPRECATED;
2477 spin_unlock_bh(&ifp->lock);
2479 if (!(flags&IFA_F_TENTATIVE))
2480 ipv6_ifa_notify(0, ifp);
2482 spin_unlock_bh(&ifp->lock);
2484 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2493 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2495 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2497 struct prefix_info *pinfo;
2502 struct inet6_dev *in6_dev;
2503 struct net *net = dev_net(dev);
2505 pinfo = (struct prefix_info *) opt;
2507 if (len < sizeof(struct prefix_info)) {
2508 ADBG("addrconf: prefix option too short\n");
2513 * Validation checks ([ADDRCONF], page 19)
2516 addr_type = ipv6_addr_type(&pinfo->prefix);
2518 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2521 valid_lft = ntohl(pinfo->valid);
2522 prefered_lft = ntohl(pinfo->prefered);
2524 if (prefered_lft > valid_lft) {
2525 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2529 in6_dev = in6_dev_get(dev);
2532 net_dbg_ratelimited("addrconf: device %s not configured\n",
2538 * Two things going on here:
2539 * 1) Add routes for on-link prefixes
2540 * 2) Configure prefixes with the auto flag set
2543 if (pinfo->onlink) {
2544 struct rt6_info *rt;
2545 unsigned long rt_expires;
2547 /* Avoid arithmetic overflow. Really, we could
2548 * save rt_expires in seconds, likely valid_lft,
2549 * but it would require division in fib gc, that it
2553 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2555 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2557 if (addrconf_finite_timeout(rt_expires))
2560 rt = addrconf_get_prefix_route(&pinfo->prefix,
2563 RTF_ADDRCONF | RTF_PREFIX_RT,
2564 RTF_GATEWAY | RTF_DEFAULT);
2567 /* Autoconf prefix route */
2568 if (valid_lft == 0) {
2571 } else if (addrconf_finite_timeout(rt_expires)) {
2573 rt6_set_expires(rt, jiffies + rt_expires);
2575 rt6_clean_expires(rt);
2577 } else if (valid_lft) {
2578 clock_t expires = 0;
2579 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2580 if (addrconf_finite_timeout(rt_expires)) {
2582 flags |= RTF_EXPIRES;
2583 expires = jiffies_to_clock_t(rt_expires);
2585 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2586 dev, expires, flags);
2591 /* Try to figure out our local address for this prefix */
2593 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2594 struct in6_addr addr;
2595 bool tokenized = false, dev_addr_generated = false;
2597 if (pinfo->prefix_len == 64) {
2598 memcpy(&addr, &pinfo->prefix, 8);
2600 if (!ipv6_addr_any(&in6_dev->token)) {
2601 read_lock_bh(&in6_dev->lock);
2602 memcpy(addr.s6_addr + 8,
2603 in6_dev->token.s6_addr + 8, 8);
2604 read_unlock_bh(&in6_dev->lock);
2606 } else if (is_addr_mode_generate_stable(in6_dev) &&
2607 !ipv6_generate_stable_address(&addr, 0,
2609 addr_flags |= IFA_F_STABLE_PRIVACY;
2611 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2612 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2615 dev_addr_generated = true;
2619 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2624 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2627 tokenized, valid_lft,
2632 /* Ignore error case here because previous prefix add addr was
2633 * successful which will be notified.
2635 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2636 addr_type, addr_flags, sllao,
2637 tokenized, valid_lft,
2639 dev_addr_generated);
2641 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2643 in6_dev_put(in6_dev);
2647 * Set destination address.
2648 * Special case for SIT interfaces where we create a new "virtual"
2651 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2653 struct in6_ifreq ireq;
2654 struct net_device *dev;
2660 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2663 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2669 #if IS_ENABLED(CONFIG_IPV6_SIT)
2670 if (dev->type == ARPHRD_SIT) {
2671 const struct net_device_ops *ops = dev->netdev_ops;
2673 struct ip_tunnel_parm p;
2675 err = -EADDRNOTAVAIL;
2676 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2679 memset(&p, 0, sizeof(p));
2680 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2684 p.iph.protocol = IPPROTO_IPV6;
2686 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2688 if (ops->ndo_do_ioctl) {
2689 mm_segment_t oldfs = get_fs();
2692 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2699 dev = __dev_get_by_name(net, p.name);
2702 err = dev_open(dev);
2712 static int ipv6_mc_config(struct sock *sk, bool join,
2713 const struct in6_addr *addr, int ifindex)
2721 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2723 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2730 * Manual configuration of address on an interface
2732 static int inet6_addr_add(struct net *net, int ifindex,
2733 const struct in6_addr *pfx,
2734 const struct in6_addr *peer_pfx,
2735 unsigned int plen, __u32 ifa_flags,
2736 __u32 prefered_lft, __u32 valid_lft)
2738 struct inet6_ifaddr *ifp;
2739 struct inet6_dev *idev;
2740 struct net_device *dev;
2741 unsigned long timeout;
2751 /* check the lifetime */
2752 if (!valid_lft || prefered_lft > valid_lft)
2755 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2758 dev = __dev_get_by_index(net, ifindex);
2762 idev = addrconf_add_dev(dev);
2764 return PTR_ERR(idev);
2766 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2767 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2768 true, pfx, ifindex);
2774 scope = ipv6_addr_scope(pfx);
2776 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2777 if (addrconf_finite_timeout(timeout)) {
2778 expires = jiffies_to_clock_t(timeout * HZ);
2779 valid_lft = timeout;
2780 flags = RTF_EXPIRES;
2784 ifa_flags |= IFA_F_PERMANENT;
2787 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2788 if (addrconf_finite_timeout(timeout)) {
2790 ifa_flags |= IFA_F_DEPRECATED;
2791 prefered_lft = timeout;
2794 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2795 valid_lft, prefered_lft);
2798 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2799 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2804 * Note that section 3.1 of RFC 4429 indicates
2805 * that the Optimistic flag should not be set for
2806 * manually configured addresses
2808 addrconf_dad_start(ifp);
2809 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2810 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2813 addrconf_verify_rtnl();
2815 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2816 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2817 false, pfx, ifindex);
2820 return PTR_ERR(ifp);
2823 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2824 const struct in6_addr *pfx, unsigned int plen)
2826 struct inet6_ifaddr *ifp;
2827 struct inet6_dev *idev;
2828 struct net_device *dev;
2833 dev = __dev_get_by_index(net, ifindex);
2837 idev = __in6_dev_get(dev);
2841 read_lock_bh(&idev->lock);
2842 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2843 if (ifp->prefix_len == plen &&
2844 ipv6_addr_equal(pfx, &ifp->addr)) {
2846 read_unlock_bh(&idev->lock);
2848 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2849 (ifa_flags & IFA_F_MANAGETEMPADDR))
2850 manage_tempaddrs(idev, ifp, 0, 0, false,
2853 addrconf_verify_rtnl();
2854 if (ipv6_addr_is_multicast(pfx)) {
2855 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2856 false, pfx, dev->ifindex);
2861 read_unlock_bh(&idev->lock);
2862 return -EADDRNOTAVAIL;
2866 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2868 struct in6_ifreq ireq;
2871 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2874 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2878 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2879 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2880 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2885 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2887 struct in6_ifreq ireq;
2890 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2893 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2897 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2898 ireq.ifr6_prefixlen);
2903 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2904 int plen, int scope)
2906 struct inet6_ifaddr *ifp;
2908 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2909 scope, IFA_F_PERMANENT,
2910 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2912 spin_lock_bh(&ifp->lock);
2913 ifp->flags &= ~IFA_F_TENTATIVE;
2914 spin_unlock_bh(&ifp->lock);
2915 rt_genid_bump_ipv6(dev_net(idev->dev));
2916 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2921 #if IS_ENABLED(CONFIG_IPV6_SIT)
2922 static void sit_add_v4_addrs(struct inet6_dev *idev)
2924 struct in6_addr addr;
2925 struct net_device *dev;
2926 struct net *net = dev_net(idev->dev);
2932 memset(&addr, 0, sizeof(struct in6_addr));
2933 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2935 if (idev->dev->flags&IFF_POINTOPOINT) {
2936 addr.s6_addr32[0] = htonl(0xfe800000);
2940 scope = IPV6_ADDR_COMPATv4;
2942 pflags |= RTF_NONEXTHOP;
2945 if (addr.s6_addr32[3]) {
2946 add_addr(idev, &addr, plen, scope);
2947 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2951 for_each_netdev(net, dev) {
2952 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2953 if (in_dev && (dev->flags & IFF_UP)) {
2954 struct in_ifaddr *ifa;
2958 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2960 addr.s6_addr32[3] = ifa->ifa_local;
2962 if (ifa->ifa_scope == RT_SCOPE_LINK)
2964 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2965 if (idev->dev->flags&IFF_POINTOPOINT)
2970 add_addr(idev, &addr, plen, flag);
2971 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2979 static void init_loopback(struct net_device *dev)
2981 struct inet6_dev *idev;
2982 struct net_device *sp_dev;
2983 struct inet6_ifaddr *sp_ifa;
2984 struct rt6_info *sp_rt;
2990 idev = ipv6_find_idev(dev);
2992 pr_debug("%s: add_dev failed\n", __func__);
2996 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2998 /* Add routes to other interface's IPv6 addresses */
2999 for_each_netdev(dev_net(dev), sp_dev) {
3000 if (!strcmp(sp_dev->name, dev->name))
3003 idev = __in6_dev_get(sp_dev);
3007 read_lock_bh(&idev->lock);
3008 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3010 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3014 /* This dst has been added to garbage list when
3015 * lo device down, release this obsolete dst and
3016 * reallocate a new router for ifa.
3018 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
3019 ip6_rt_put(sp_ifa->rt);
3026 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
3028 /* Failure cases are ignored */
3029 if (!IS_ERR(sp_rt)) {
3034 read_unlock_bh(&idev->lock);
3038 void addrconf_add_linklocal(struct inet6_dev *idev,
3039 const struct in6_addr *addr, u32 flags)
3041 struct inet6_ifaddr *ifp;
3042 u32 addr_flags = flags | IFA_F_PERMANENT;
3044 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3045 if (idev->cnf.optimistic_dad &&
3046 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3047 addr_flags |= IFA_F_OPTIMISTIC;
3050 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3051 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
3053 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3054 addrconf_dad_start(ifp);
3058 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3060 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3062 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3065 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3066 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3069 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3070 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3076 static int ipv6_generate_stable_address(struct in6_addr *address,
3078 const struct inet6_dev *idev)
3080 static DEFINE_SPINLOCK(lock);
3081 static __u32 digest[SHA_DIGEST_WORDS];
3082 static __u32 workspace[SHA_WORKSPACE_WORDS];
3085 char __data[SHA_MESSAGE_BYTES];
3087 struct in6_addr secret;
3089 unsigned char hwaddr[MAX_ADDR_LEN];
3094 struct in6_addr secret;
3095 struct in6_addr temp;
3096 struct net *net = dev_net(idev->dev);
3098 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3100 if (idev->cnf.stable_secret.initialized)
3101 secret = idev->cnf.stable_secret.secret;
3102 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3103 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3108 spin_lock_bh(&lock);
3111 memset(&data, 0, sizeof(data));
3112 memset(workspace, 0, sizeof(workspace));
3113 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3114 data.prefix[0] = address->s6_addr32[0];
3115 data.prefix[1] = address->s6_addr32[1];
3116 data.secret = secret;
3117 data.dad_count = dad_count;
3119 sha_transform(digest, data.__data, workspace);
3122 temp.s6_addr32[2] = (__force __be32)digest[0];
3123 temp.s6_addr32[3] = (__force __be32)digest[1];
3125 spin_unlock_bh(&lock);
3127 if (ipv6_reserved_interfaceid(temp)) {
3129 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3138 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3140 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3144 s = &idev->cnf.stable_secret;
3145 get_random_bytes(&s->secret, sizeof(s->secret));
3146 s->initialized = true;
3149 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3151 struct in6_addr addr;
3153 /* no link local addresses on L3 master devices */
3154 if (netif_is_l3_master(idev->dev))
3157 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3159 switch (idev->cnf.addr_gen_mode) {
3160 case IN6_ADDR_GEN_MODE_RANDOM:
3161 ipv6_gen_mode_random_init(idev);
3163 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3164 if (!ipv6_generate_stable_address(&addr, 0, idev))
3165 addrconf_add_linklocal(idev, &addr,
3166 IFA_F_STABLE_PRIVACY);
3167 else if (prefix_route)
3168 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3170 case IN6_ADDR_GEN_MODE_EUI64:
3171 /* addrconf_add_linklocal also adds a prefix_route and we
3172 * only need to care about prefix routes if ipv6_generate_eui64
3173 * couldn't generate one.
3175 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3176 addrconf_add_linklocal(idev, &addr, 0);
3177 else if (prefix_route)
3178 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3180 case IN6_ADDR_GEN_MODE_NONE:
3182 /* will not add any link local address */
3187 static void addrconf_dev_config(struct net_device *dev)
3189 struct inet6_dev *idev;
3193 if ((dev->type != ARPHRD_ETHER) &&
3194 (dev->type != ARPHRD_FDDI) &&
3195 (dev->type != ARPHRD_ARCNET) &&
3196 (dev->type != ARPHRD_INFINIBAND) &&
3197 (dev->type != ARPHRD_IEEE1394) &&
3198 (dev->type != ARPHRD_TUNNEL6) &&
3199 (dev->type != ARPHRD_6LOWPAN) &&
3200 (dev->type != ARPHRD_IP6GRE) &&
3201 (dev->type != ARPHRD_IPGRE) &&
3202 (dev->type != ARPHRD_TUNNEL) &&
3203 (dev->type != ARPHRD_NONE)) {
3204 /* Alas, we support only Ethernet autoconfiguration. */
3208 idev = addrconf_add_dev(dev);
3212 /* this device type has no EUI support */
3213 if (dev->type == ARPHRD_NONE &&
3214 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3215 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3217 addrconf_addr_gen(idev, false);
3220 #if IS_ENABLED(CONFIG_IPV6_SIT)
3221 static void addrconf_sit_config(struct net_device *dev)
3223 struct inet6_dev *idev;
3228 * Configure the tunnel with one of our IPv4
3229 * addresses... we should configure all of
3230 * our v4 addrs in the tunnel
3233 idev = ipv6_find_idev(dev);
3235 pr_debug("%s: add_dev failed\n", __func__);
3239 if (dev->priv_flags & IFF_ISATAP) {
3240 addrconf_addr_gen(idev, false);
3244 sit_add_v4_addrs(idev);
3246 if (dev->flags&IFF_POINTOPOINT)
3247 addrconf_add_mroute(dev);
3251 #if IS_ENABLED(CONFIG_NET_IPGRE)
3252 static void addrconf_gre_config(struct net_device *dev)
3254 struct inet6_dev *idev;
3258 idev = ipv6_find_idev(dev);
3260 pr_debug("%s: add_dev failed\n", __func__);
3264 addrconf_addr_gen(idev, true);
3265 if (dev->flags & IFF_POINTOPOINT)
3266 addrconf_add_mroute(dev);
3270 static int fixup_permanent_addr(struct inet6_dev *idev,
3271 struct inet6_ifaddr *ifp)
3274 struct rt6_info *rt;
3276 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3277 if (unlikely(IS_ERR(rt)))
3283 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3284 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3288 addrconf_dad_start(ifp);
3293 static void addrconf_permanent_addr(struct net_device *dev)
3295 struct inet6_ifaddr *ifp, *tmp;
3296 struct inet6_dev *idev;
3298 idev = __in6_dev_get(dev);
3302 write_lock_bh(&idev->lock);
3304 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3305 if ((ifp->flags & IFA_F_PERMANENT) &&
3306 fixup_permanent_addr(idev, ifp) < 0) {
3307 write_unlock_bh(&idev->lock);
3309 write_lock_bh(&idev->lock);
3311 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3312 idev->dev->name, &ifp->addr);
3316 write_unlock_bh(&idev->lock);
3319 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3322 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3323 struct netdev_notifier_changeupper_info *info;
3324 struct inet6_dev *idev = __in6_dev_get(dev);
3325 int run_pending = 0;
3329 case NETDEV_REGISTER:
3330 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3331 idev = ipv6_add_dev(dev);
3333 return notifier_from_errno(PTR_ERR(idev));
3337 case NETDEV_CHANGEMTU:
3338 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3339 if (dev->mtu < IPV6_MIN_MTU) {
3340 addrconf_ifdown(dev, 1);
3345 rt6_mtu_change(dev, dev->mtu);
3346 idev->cnf.mtu6 = dev->mtu;
3350 /* allocate new idev */
3351 idev = ipv6_add_dev(dev);
3355 /* device is still not ready */
3356 if (!(idev->if_flags & IF_READY))
3365 if (dev->flags & IFF_SLAVE)
3368 if (idev && idev->cnf.disable_ipv6)
3371 if (event == NETDEV_UP) {
3372 /* restore routes for permanent addresses */
3373 addrconf_permanent_addr(dev);
3375 if (!addrconf_qdisc_ok(dev)) {
3376 /* device is not ready yet. */
3377 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3382 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3383 idev = ipv6_add_dev(dev);
3385 if (!IS_ERR_OR_NULL(idev)) {
3386 idev->if_flags |= IF_READY;
3389 } else if (event == NETDEV_CHANGE) {
3390 if (!addrconf_qdisc_ok(dev)) {
3391 /* device is still not ready. */
3396 if (idev->if_flags & IF_READY) {
3397 /* device is already configured -
3398 * but resend MLD reports, we might
3399 * have roamed and need to update
3400 * multicast snooping switches
3405 idev->if_flags |= IF_READY;
3408 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3414 switch (dev->type) {
3415 #if IS_ENABLED(CONFIG_IPV6_SIT)
3417 addrconf_sit_config(dev);
3420 #if IS_ENABLED(CONFIG_NET_IPGRE)
3422 addrconf_gre_config(dev);
3425 case ARPHRD_LOOPBACK:
3430 addrconf_dev_config(dev);
3434 if (!IS_ERR_OR_NULL(idev)) {
3436 addrconf_dad_run(idev);
3439 * If the MTU changed during the interface down,
3440 * when the interface up, the changed MTU must be
3441 * reflected in the idev as well as routers.
3443 if (idev->cnf.mtu6 != dev->mtu &&
3444 dev->mtu >= IPV6_MIN_MTU) {
3445 rt6_mtu_change(dev, dev->mtu);
3446 idev->cnf.mtu6 = dev->mtu;
3448 idev->tstamp = jiffies;
3449 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3452 * If the changed mtu during down is lower than
3453 * IPV6_MIN_MTU stop IPv6 on this interface.
3455 if (dev->mtu < IPV6_MIN_MTU)
3456 addrconf_ifdown(dev, 1);
3461 case NETDEV_UNREGISTER:
3463 * Remove all addresses from this interface.
3465 addrconf_ifdown(dev, event != NETDEV_DOWN);
3468 case NETDEV_CHANGENAME:
3470 snmp6_unregister_dev(idev);
3471 addrconf_sysctl_unregister(idev);
3472 err = addrconf_sysctl_register(idev);
3474 return notifier_from_errno(err);
3475 err = snmp6_register_dev(idev);
3477 addrconf_sysctl_unregister(idev);
3478 return notifier_from_errno(err);
3483 case NETDEV_PRE_TYPE_CHANGE:
3484 case NETDEV_POST_TYPE_CHANGE:
3486 addrconf_type_change(dev, event);
3489 case NETDEV_CHANGEUPPER:
3492 /* flush all routes if dev is linked to or unlinked from
3493 * an L3 master device (e.g., VRF)
3495 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3496 addrconf_ifdown(dev, 0);
3503 * addrconf module should be notified of a device going up
3505 static struct notifier_block ipv6_dev_notf = {
3506 .notifier_call = addrconf_notify,
3509 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3511 struct inet6_dev *idev;
3514 idev = __in6_dev_get(dev);
3516 if (event == NETDEV_POST_TYPE_CHANGE)
3517 ipv6_mc_remap(idev);
3518 else if (event == NETDEV_PRE_TYPE_CHANGE)
3519 ipv6_mc_unmap(idev);
3522 static bool addr_is_local(const struct in6_addr *addr)
3524 return ipv6_addr_type(addr) &
3525 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3528 static int addrconf_ifdown(struct net_device *dev, int how)
3530 struct net *net = dev_net(dev);
3531 struct inet6_dev *idev;
3532 struct inet6_ifaddr *ifa, *tmp;
3533 struct list_head del_list;
3540 rt6_ifdown(net, dev);
3541 neigh_ifdown(&nd_tbl, dev);
3543 idev = __in6_dev_get(dev);
3548 * Step 1: remove reference to ipv6 device from parent device.
3554 /* protected by rtnl_lock */
3555 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3557 /* Step 1.5: remove snmp6 entry */
3558 snmp6_unregister_dev(idev);
3562 /* aggregate the system setting and interface setting */
3563 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3565 _keep_addr = idev->cnf.keep_addr_on_down;
3567 /* combine the user config with event to determine if permanent
3568 * addresses are to be removed from address hash table
3570 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3572 /* Step 2: clear hash table */
3573 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3574 struct hlist_head *h = &inet6_addr_lst[i];
3576 spin_lock_bh(&addrconf_hash_lock);
3578 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3579 if (ifa->idev == idev) {
3580 addrconf_del_dad_work(ifa);
3581 /* combined flag + permanent flag decide if
3582 * address is retained on a down event
3585 !(ifa->flags & IFA_F_PERMANENT) ||
3586 addr_is_local(&ifa->addr)) {
3587 hlist_del_init_rcu(&ifa->addr_lst);
3592 spin_unlock_bh(&addrconf_hash_lock);
3595 write_lock_bh(&idev->lock);
3597 addrconf_del_rs_timer(idev);
3599 /* Step 2: clear flags for stateless addrconf */
3601 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3603 /* Step 3: clear tempaddr list */
3604 while (!list_empty(&idev->tempaddr_list)) {
3605 ifa = list_first_entry(&idev->tempaddr_list,
3606 struct inet6_ifaddr, tmp_list);
3607 list_del(&ifa->tmp_list);
3608 write_unlock_bh(&idev->lock);
3609 spin_lock_bh(&ifa->lock);
3612 in6_ifa_put(ifa->ifpub);
3615 spin_unlock_bh(&ifa->lock);
3617 write_lock_bh(&idev->lock);
3620 /* re-combine the user config with event to determine if permanent
3621 * addresses are to be removed from the interface list
3623 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3625 INIT_LIST_HEAD(&del_list);
3626 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3627 struct rt6_info *rt = NULL;
3629 addrconf_del_dad_work(ifa);
3631 write_unlock_bh(&idev->lock);
3632 spin_lock_bh(&ifa->lock);
3634 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3635 !addr_is_local(&ifa->addr)) {
3636 /* set state to skip the notifier below */
3637 state = INET6_IFADDR_STATE_DEAD;
3639 if (!(ifa->flags & IFA_F_NODAD))
3640 ifa->flags |= IFA_F_TENTATIVE;
3646 ifa->state = INET6_IFADDR_STATE_DEAD;
3648 list_move(&ifa->if_list, &del_list);
3651 spin_unlock_bh(&ifa->lock);
3656 if (state != INET6_IFADDR_STATE_DEAD) {
3657 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3658 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3660 if (idev->cnf.forwarding)
3661 addrconf_leave_anycast(ifa);
3662 addrconf_leave_solict(ifa->idev, &ifa->addr);
3665 write_lock_bh(&idev->lock);
3668 write_unlock_bh(&idev->lock);
3670 /* now clean up addresses to be removed */
3671 while (!list_empty(&del_list)) {
3672 ifa = list_first_entry(&del_list,
3673 struct inet6_ifaddr, if_list);
3674 list_del(&ifa->if_list);
3679 /* Step 5: Discard anycast and multicast list */
3681 ipv6_ac_destroy_dev(idev);
3682 ipv6_mc_destroy_dev(idev);
3687 idev->tstamp = jiffies;
3689 /* Last: Shot the device (if unregistered) */
3691 addrconf_sysctl_unregister(idev);
3692 neigh_parms_release(&nd_tbl, idev->nd_parms);
3693 neigh_ifdown(&nd_tbl, dev);
3699 static void addrconf_rs_timer(unsigned long data)
3701 struct inet6_dev *idev = (struct inet6_dev *)data;
3702 struct net_device *dev = idev->dev;
3703 struct in6_addr lladdr;
3705 write_lock(&idev->lock);
3706 if (idev->dead || !(idev->if_flags & IF_READY))
3709 if (!ipv6_accept_ra(idev))
3712 /* Announcement received after solicitation was sent */
3713 if (idev->if_flags & IF_RA_RCVD)
3716 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3717 write_unlock(&idev->lock);
3718 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3719 ndisc_send_rs(dev, &lladdr,
3720 &in6addr_linklocal_allrouters);
3724 write_lock(&idev->lock);
3725 idev->rs_interval = rfc3315_s14_backoff_update(
3726 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3727 /* The wait after the last probe can be shorter */
3728 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3729 idev->cnf.rtr_solicits) ?
3730 idev->cnf.rtr_solicit_delay :
3734 * Note: we do not support deprecated "all on-link"
3735 * assumption any longer.
3737 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3741 write_unlock(&idev->lock);
3747 * Duplicate Address Detection
3749 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3751 unsigned long rand_num;
3752 struct inet6_dev *idev = ifp->idev;
3755 if (ifp->flags & IFA_F_OPTIMISTIC)
3758 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3761 if (idev->cnf.enhanced_dad ||
3762 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3764 get_random_bytes(&nonce, 6);
3767 ifp->dad_nonce = nonce;
3768 ifp->dad_probes = idev->cnf.dad_transmits;
3769 addrconf_mod_dad_work(ifp, rand_num);
3772 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3774 struct inet6_dev *idev = ifp->idev;
3775 struct net_device *dev = idev->dev;
3776 bool bump_id, notify = false;
3778 addrconf_join_solict(dev, &ifp->addr);
3780 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3782 read_lock_bh(&idev->lock);
3783 spin_lock(&ifp->lock);
3784 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3787 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3788 idev->cnf.accept_dad < 1 ||
3789 !(ifp->flags&IFA_F_TENTATIVE) ||
3790 ifp->flags & IFA_F_NODAD) {
3791 bump_id = ifp->flags & IFA_F_TENTATIVE;
3792 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3793 spin_unlock(&ifp->lock);
3794 read_unlock_bh(&idev->lock);
3796 addrconf_dad_completed(ifp, bump_id);
3800 if (!(idev->if_flags & IF_READY)) {
3801 spin_unlock(&ifp->lock);
3802 read_unlock_bh(&idev->lock);
3804 * If the device is not ready:
3805 * - keep it tentative if it is a permanent address.
3806 * - otherwise, kill it.
3809 addrconf_dad_stop(ifp, 0);
3814 * Optimistic nodes can start receiving
3817 if (ifp->flags & IFA_F_OPTIMISTIC) {
3818 ip6_ins_rt(ifp->rt);
3819 if (ipv6_use_optimistic_addr(idev)) {
3820 /* Because optimistic nodes can use this address,
3821 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3827 addrconf_dad_kick(ifp);
3829 spin_unlock(&ifp->lock);
3830 read_unlock_bh(&idev->lock);
3832 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3835 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3837 bool begin_dad = false;
3839 spin_lock_bh(&ifp->lock);
3840 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3841 ifp->state = INET6_IFADDR_STATE_PREDAD;
3844 spin_unlock_bh(&ifp->lock);
3847 addrconf_mod_dad_work(ifp, 0);
3850 static void addrconf_dad_work(struct work_struct *w)
3852 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3853 struct inet6_ifaddr,
3855 struct inet6_dev *idev = ifp->idev;
3856 bool bump_id, disable_ipv6 = false;
3857 struct in6_addr mcaddr;
3863 } action = DAD_PROCESS;
3867 spin_lock_bh(&ifp->lock);
3868 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3870 ifp->state = INET6_IFADDR_STATE_DAD;
3871 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3873 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3875 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3876 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3877 struct in6_addr addr;
3879 addr.s6_addr32[0] = htonl(0xfe800000);
3880 addr.s6_addr32[1] = 0;
3882 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3883 ipv6_addr_equal(&ifp->addr, &addr)) {
3884 /* DAD failed for link-local based on MAC */
3885 idev->cnf.disable_ipv6 = 1;
3887 pr_info("%s: IPv6 being disabled!\n",
3888 ifp->idev->dev->name);
3889 disable_ipv6 = true;
3893 spin_unlock_bh(&ifp->lock);
3895 if (action == DAD_BEGIN) {
3896 addrconf_dad_begin(ifp);
3898 } else if (action == DAD_ABORT) {
3900 addrconf_dad_stop(ifp, 1);
3902 addrconf_ifdown(idev->dev, 0);
3906 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3909 write_lock_bh(&idev->lock);
3910 if (idev->dead || !(idev->if_flags & IF_READY)) {
3911 write_unlock_bh(&idev->lock);
3915 spin_lock(&ifp->lock);
3916 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3917 spin_unlock(&ifp->lock);
3918 write_unlock_bh(&idev->lock);
3922 if (ifp->dad_probes == 0) {
3924 * DAD was successful
3927 bump_id = ifp->flags & IFA_F_TENTATIVE;
3928 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3929 spin_unlock(&ifp->lock);
3930 write_unlock_bh(&idev->lock);
3932 addrconf_dad_completed(ifp, bump_id);
3938 addrconf_mod_dad_work(ifp,
3939 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3940 spin_unlock(&ifp->lock);
3941 write_unlock_bh(&idev->lock);
3943 /* send a neighbour solicitation for our addr */
3944 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3945 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3952 /* ifp->idev must be at least read locked */
3953 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3955 struct inet6_ifaddr *ifpiter;
3956 struct inet6_dev *idev = ifp->idev;
3958 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3959 if (ifpiter->scope > IFA_LINK)
3961 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3962 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3963 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3970 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
3972 struct net_device *dev = ifp->idev->dev;
3973 struct in6_addr lladdr;
3974 bool send_rs, send_mld;
3976 addrconf_del_dad_work(ifp);
3979 * Configure the address for reception. Now it is valid.
3982 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3984 /* If added prefix is link local and we are prepared to process
3985 router advertisements, start sending router solicitations.
3988 read_lock_bh(&ifp->idev->lock);
3989 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3990 send_rs = send_mld &&
3991 ipv6_accept_ra(ifp->idev) &&
3992 ifp->idev->cnf.rtr_solicits != 0 &&
3993 (dev->flags&IFF_LOOPBACK) == 0;
3994 read_unlock_bh(&ifp->idev->lock);
3996 /* While dad is in progress mld report's source address is in6_addrany.
3997 * Resend with proper ll now.
4000 ipv6_mc_dad_complete(ifp->idev);
4004 * If a host as already performed a random delay
4005 * [...] as part of DAD [...] there is no need
4006 * to delay again before sending the first RS
4008 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4010 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4012 write_lock_bh(&ifp->idev->lock);
4013 spin_lock(&ifp->lock);
4014 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4015 ifp->idev->cnf.rtr_solicit_interval);
4016 ifp->idev->rs_probes = 1;
4017 ifp->idev->if_flags |= IF_RS_SENT;
4018 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4019 spin_unlock(&ifp->lock);
4020 write_unlock_bh(&ifp->idev->lock);
4024 rt_genid_bump_ipv6(dev_net(dev));
4026 /* Make sure that a new temporary address will be created
4027 * before this temporary address becomes deprecated.
4029 if (ifp->flags & IFA_F_TEMPORARY)
4030 addrconf_verify_rtnl();
4033 static void addrconf_dad_run(struct inet6_dev *idev)
4035 struct inet6_ifaddr *ifp;
4037 read_lock_bh(&idev->lock);
4038 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4039 spin_lock(&ifp->lock);
4040 if (ifp->flags & IFA_F_TENTATIVE &&
4041 ifp->state == INET6_IFADDR_STATE_DAD)
4042 addrconf_dad_kick(ifp);
4043 spin_unlock(&ifp->lock);
4045 read_unlock_bh(&idev->lock);
4048 #ifdef CONFIG_PROC_FS
4049 struct if6_iter_state {
4050 struct seq_net_private p;
4055 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4057 struct inet6_ifaddr *ifa = NULL;
4058 struct if6_iter_state *state = seq->private;
4059 struct net *net = seq_file_net(seq);
4062 /* initial bucket if pos is 0 */
4068 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4069 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
4071 if (!net_eq(dev_net(ifa->idev->dev), net))
4073 /* sync with offset */
4074 if (p < state->offset) {
4082 /* prepare for next bucket */
4089 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4090 struct inet6_ifaddr *ifa)
4092 struct if6_iter_state *state = seq->private;
4093 struct net *net = seq_file_net(seq);
4095 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
4096 if (!net_eq(dev_net(ifa->idev->dev), net))
4102 while (++state->bucket < IN6_ADDR_HSIZE) {
4104 hlist_for_each_entry_rcu_bh(ifa,
4105 &inet6_addr_lst[state->bucket], addr_lst) {
4106 if (!net_eq(dev_net(ifa->idev->dev), net))
4116 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4120 return if6_get_first(seq, *pos);
4123 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4125 struct inet6_ifaddr *ifa;
4127 ifa = if6_get_next(seq, v);
4132 static void if6_seq_stop(struct seq_file *seq, void *v)
4135 rcu_read_unlock_bh();
4138 static int if6_seq_show(struct seq_file *seq, void *v)
4140 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4141 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4143 ifp->idev->dev->ifindex,
4147 ifp->idev->dev->name);
4151 static const struct seq_operations if6_seq_ops = {
4152 .start = if6_seq_start,
4153 .next = if6_seq_next,
4154 .show = if6_seq_show,
4155 .stop = if6_seq_stop,
4158 static int if6_seq_open(struct inode *inode, struct file *file)
4160 return seq_open_net(inode, file, &if6_seq_ops,
4161 sizeof(struct if6_iter_state));
4164 static const struct file_operations if6_fops = {
4165 .owner = THIS_MODULE,
4166 .open = if6_seq_open,
4168 .llseek = seq_lseek,
4169 .release = seq_release_net,
4172 static int __net_init if6_proc_net_init(struct net *net)
4174 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
4179 static void __net_exit if6_proc_net_exit(struct net *net)
4181 remove_proc_entry("if_inet6", net->proc_net);
4184 static struct pernet_operations if6_proc_net_ops = {
4185 .init = if6_proc_net_init,
4186 .exit = if6_proc_net_exit,
4189 int __init if6_proc_init(void)
4191 return register_pernet_subsys(&if6_proc_net_ops);
4194 void if6_proc_exit(void)
4196 unregister_pernet_subsys(&if6_proc_net_ops);
4198 #endif /* CONFIG_PROC_FS */
4200 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4201 /* Check if address is a home address configured on any interface. */
4202 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4205 struct inet6_ifaddr *ifp = NULL;
4206 unsigned int hash = inet6_addr_hash(addr);
4209 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4210 if (!net_eq(dev_net(ifp->idev->dev), net))
4212 if (ipv6_addr_equal(&ifp->addr, addr) &&
4213 (ifp->flags & IFA_F_HOMEADDRESS)) {
4218 rcu_read_unlock_bh();
4224 * Periodic address status verification
4227 static void addrconf_verify_rtnl(void)
4229 unsigned long now, next, next_sec, next_sched;
4230 struct inet6_ifaddr *ifp;
4237 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4239 cancel_delayed_work(&addr_chk_work);
4241 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4243 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4246 /* When setting preferred_lft to a value not zero or
4247 * infinity, while valid_lft is infinity
4248 * IFA_F_PERMANENT has a non-infinity life time.
4250 if ((ifp->flags & IFA_F_PERMANENT) &&
4251 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4254 spin_lock(&ifp->lock);
4255 /* We try to batch several events at once. */
4256 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4258 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4259 age >= ifp->valid_lft) {
4260 spin_unlock(&ifp->lock);
4264 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4265 spin_unlock(&ifp->lock);
4267 } else if (age >= ifp->prefered_lft) {
4268 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4271 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4273 ifp->flags |= IFA_F_DEPRECATED;
4276 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4277 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4278 next = ifp->tstamp + ifp->valid_lft * HZ;
4280 spin_unlock(&ifp->lock);
4285 ipv6_ifa_notify(0, ifp);
4289 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4290 !(ifp->flags&IFA_F_TENTATIVE)) {
4291 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4292 ifp->idev->cnf.dad_transmits *
4293 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4295 if (age >= ifp->prefered_lft - regen_advance) {
4296 struct inet6_ifaddr *ifpub = ifp->ifpub;
4297 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4298 next = ifp->tstamp + ifp->prefered_lft * HZ;
4299 if (!ifp->regen_count && ifpub) {
4302 in6_ifa_hold(ifpub);
4303 spin_unlock(&ifp->lock);
4305 spin_lock(&ifpub->lock);
4306 ifpub->regen_count = 0;
4307 spin_unlock(&ifpub->lock);
4308 ipv6_create_tempaddr(ifpub, ifp);
4313 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4314 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4315 spin_unlock(&ifp->lock);
4317 /* ifp->prefered_lft <= ifp->valid_lft */
4318 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4319 next = ifp->tstamp + ifp->prefered_lft * HZ;
4320 spin_unlock(&ifp->lock);
4325 next_sec = round_jiffies_up(next);
4328 /* If rounded timeout is accurate enough, accept it. */
4329 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4330 next_sched = next_sec;
4332 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4333 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4334 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4336 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4337 now, next, next_sec, next_sched);
4338 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4339 rcu_read_unlock_bh();
4342 static void addrconf_verify_work(struct work_struct *w)
4345 addrconf_verify_rtnl();
4349 static void addrconf_verify(void)
4351 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4354 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4355 struct in6_addr **peer_pfx)
4357 struct in6_addr *pfx = NULL;
4362 pfx = nla_data(addr);
4365 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4367 pfx = nla_data(local);
4373 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4374 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4375 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4376 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4377 [IFA_FLAGS] = { .len = sizeof(u32) },
4381 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4383 struct net *net = sock_net(skb->sk);
4384 struct ifaddrmsg *ifm;
4385 struct nlattr *tb[IFA_MAX+1];
4386 struct in6_addr *pfx, *peer_pfx;
4390 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4394 ifm = nlmsg_data(nlh);
4395 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4399 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4401 /* We ignore other flags so far. */
4402 ifa_flags &= IFA_F_MANAGETEMPADDR;
4404 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4405 ifm->ifa_prefixlen);
4408 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4409 u32 prefered_lft, u32 valid_lft)
4413 unsigned long timeout;
4414 bool was_managetempaddr;
4415 bool had_prefixroute;
4419 if (!valid_lft || (prefered_lft > valid_lft))
4422 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4423 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4426 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4427 if (addrconf_finite_timeout(timeout)) {
4428 expires = jiffies_to_clock_t(timeout * HZ);
4429 valid_lft = timeout;
4430 flags = RTF_EXPIRES;
4434 ifa_flags |= IFA_F_PERMANENT;
4437 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4438 if (addrconf_finite_timeout(timeout)) {
4440 ifa_flags |= IFA_F_DEPRECATED;
4441 prefered_lft = timeout;
4444 spin_lock_bh(&ifp->lock);
4445 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4446 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4447 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4448 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4449 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4450 IFA_F_NOPREFIXROUTE);
4451 ifp->flags |= ifa_flags;
4452 ifp->tstamp = jiffies;
4453 ifp->valid_lft = valid_lft;
4454 ifp->prefered_lft = prefered_lft;
4456 spin_unlock_bh(&ifp->lock);
4457 if (!(ifp->flags&IFA_F_TENTATIVE))
4458 ipv6_ifa_notify(0, ifp);
4460 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4461 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4463 } else if (had_prefixroute) {
4464 enum cleanup_prefix_rt_t action;
4465 unsigned long rt_expires;
4467 write_lock_bh(&ifp->idev->lock);
4468 action = check_cleanup_prefix_route(ifp, &rt_expires);
4469 write_unlock_bh(&ifp->idev->lock);
4471 if (action != CLEANUP_PREFIX_RT_NOP) {
4472 cleanup_prefix_route(ifp, rt_expires,
4473 action == CLEANUP_PREFIX_RT_DEL);
4477 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4478 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4479 valid_lft = prefered_lft = 0;
4480 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4481 !was_managetempaddr, jiffies);
4484 addrconf_verify_rtnl();
4490 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4492 struct net *net = sock_net(skb->sk);
4493 struct ifaddrmsg *ifm;
4494 struct nlattr *tb[IFA_MAX+1];
4495 struct in6_addr *pfx, *peer_pfx;
4496 struct inet6_ifaddr *ifa;
4497 struct net_device *dev;
4498 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4502 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4506 ifm = nlmsg_data(nlh);
4507 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4511 if (tb[IFA_CACHEINFO]) {
4512 struct ifa_cacheinfo *ci;
4514 ci = nla_data(tb[IFA_CACHEINFO]);
4515 valid_lft = ci->ifa_valid;
4516 preferred_lft = ci->ifa_prefered;
4518 preferred_lft = INFINITY_LIFE_TIME;
4519 valid_lft = INFINITY_LIFE_TIME;
4522 dev = __dev_get_by_index(net, ifm->ifa_index);
4526 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4528 /* We ignore other flags so far. */
4529 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4530 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4532 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4535 * It would be best to check for !NLM_F_CREATE here but
4536 * userspace already relies on not having to provide this.
4538 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4539 ifm->ifa_prefixlen, ifa_flags,
4540 preferred_lft, valid_lft);
4543 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4544 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4547 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4554 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4555 u8 scope, int ifindex)
4557 struct ifaddrmsg *ifm;
4559 ifm = nlmsg_data(nlh);
4560 ifm->ifa_family = AF_INET6;
4561 ifm->ifa_prefixlen = prefixlen;
4562 ifm->ifa_flags = flags;
4563 ifm->ifa_scope = scope;
4564 ifm->ifa_index = ifindex;
4567 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4568 unsigned long tstamp, u32 preferred, u32 valid)
4570 struct ifa_cacheinfo ci;
4572 ci.cstamp = cstamp_delta(cstamp);
4573 ci.tstamp = cstamp_delta(tstamp);
4574 ci.ifa_prefered = preferred;
4575 ci.ifa_valid = valid;
4577 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4580 static inline int rt_scope(int ifa_scope)
4582 if (ifa_scope & IFA_HOST)
4583 return RT_SCOPE_HOST;
4584 else if (ifa_scope & IFA_LINK)
4585 return RT_SCOPE_LINK;
4586 else if (ifa_scope & IFA_SITE)
4587 return RT_SCOPE_SITE;
4589 return RT_SCOPE_UNIVERSE;
4592 static inline int inet6_ifaddr_msgsize(void)
4594 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4595 + nla_total_size(16) /* IFA_LOCAL */
4596 + nla_total_size(16) /* IFA_ADDRESS */
4597 + nla_total_size(sizeof(struct ifa_cacheinfo))
4598 + nla_total_size(4) /* IFA_FLAGS */;
4601 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4602 u32 portid, u32 seq, int event, unsigned int flags)
4604 struct nlmsghdr *nlh;
4605 u32 preferred, valid;
4607 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4611 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4612 ifa->idev->dev->ifindex);
4614 if (!((ifa->flags&IFA_F_PERMANENT) &&
4615 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4616 preferred = ifa->prefered_lft;
4617 valid = ifa->valid_lft;
4618 if (preferred != INFINITY_LIFE_TIME) {
4619 long tval = (jiffies - ifa->tstamp)/HZ;
4620 if (preferred > tval)
4624 if (valid != INFINITY_LIFE_TIME) {
4632 preferred = INFINITY_LIFE_TIME;
4633 valid = INFINITY_LIFE_TIME;
4636 if (!ipv6_addr_any(&ifa->peer_addr)) {
4637 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4638 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4641 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4644 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4647 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4650 nlmsg_end(skb, nlh);
4654 nlmsg_cancel(skb, nlh);
4658 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4659 u32 portid, u32 seq, int event, u16 flags)
4661 struct nlmsghdr *nlh;
4662 u8 scope = RT_SCOPE_UNIVERSE;
4663 int ifindex = ifmca->idev->dev->ifindex;
4665 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4666 scope = RT_SCOPE_SITE;
4668 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4672 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4673 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4674 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4675 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4676 nlmsg_cancel(skb, nlh);
4680 nlmsg_end(skb, nlh);
4684 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4685 u32 portid, u32 seq, int event, unsigned int flags)
4687 struct nlmsghdr *nlh;
4688 u8 scope = RT_SCOPE_UNIVERSE;
4689 int ifindex = ifaca->aca_idev->dev->ifindex;
4691 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4692 scope = RT_SCOPE_SITE;
4694 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4698 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4699 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4700 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4701 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4702 nlmsg_cancel(skb, nlh);
4706 nlmsg_end(skb, nlh);
4716 /* called with rcu_read_lock() */
4717 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4718 struct netlink_callback *cb, enum addr_type_t type,
4719 int s_ip_idx, int *p_ip_idx)
4721 struct ifmcaddr6 *ifmca;
4722 struct ifacaddr6 *ifaca;
4724 int ip_idx = *p_ip_idx;
4726 read_lock_bh(&idev->lock);
4728 case UNICAST_ADDR: {
4729 struct inet6_ifaddr *ifa;
4731 /* unicast address incl. temp addr */
4732 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4733 if (++ip_idx < s_ip_idx)
4735 err = inet6_fill_ifaddr(skb, ifa,
4736 NETLINK_CB(cb->skb).portid,
4742 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4746 case MULTICAST_ADDR:
4747 /* multicast address */
4748 for (ifmca = idev->mc_list; ifmca;
4749 ifmca = ifmca->next, ip_idx++) {
4750 if (ip_idx < s_ip_idx)
4752 err = inet6_fill_ifmcaddr(skb, ifmca,
4753 NETLINK_CB(cb->skb).portid,
4762 /* anycast address */
4763 for (ifaca = idev->ac_list; ifaca;
4764 ifaca = ifaca->aca_next, ip_idx++) {
4765 if (ip_idx < s_ip_idx)
4767 err = inet6_fill_ifacaddr(skb, ifaca,
4768 NETLINK_CB(cb->skb).portid,
4779 read_unlock_bh(&idev->lock);
4784 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4785 enum addr_type_t type)
4787 struct net *net = sock_net(skb->sk);
4790 int s_idx, s_ip_idx;
4791 struct net_device *dev;
4792 struct inet6_dev *idev;
4793 struct hlist_head *head;
4796 s_idx = idx = cb->args[1];
4797 s_ip_idx = ip_idx = cb->args[2];
4800 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4801 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4803 head = &net->dev_index_head[h];
4804 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4807 if (h > s_h || idx > s_idx)
4810 idev = __in6_dev_get(dev);
4814 if (in6_dump_addrs(idev, skb, cb, type,
4815 s_ip_idx, &ip_idx) < 0)
4825 cb->args[2] = ip_idx;
4830 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4832 enum addr_type_t type = UNICAST_ADDR;
4834 return inet6_dump_addr(skb, cb, type);
4837 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4839 enum addr_type_t type = MULTICAST_ADDR;
4841 return inet6_dump_addr(skb, cb, type);
4845 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4847 enum addr_type_t type = ANYCAST_ADDR;
4849 return inet6_dump_addr(skb, cb, type);
4852 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4854 struct net *net = sock_net(in_skb->sk);
4855 struct ifaddrmsg *ifm;
4856 struct nlattr *tb[IFA_MAX+1];
4857 struct in6_addr *addr = NULL, *peer;
4858 struct net_device *dev = NULL;
4859 struct inet6_ifaddr *ifa;
4860 struct sk_buff *skb;
4863 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4867 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4873 ifm = nlmsg_data(nlh);
4875 dev = __dev_get_by_index(net, ifm->ifa_index);
4877 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4879 err = -EADDRNOTAVAIL;
4883 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4889 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4890 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4892 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4893 WARN_ON(err == -EMSGSIZE);
4897 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4904 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4906 struct sk_buff *skb;
4907 struct net *net = dev_net(ifa->idev->dev);
4910 /* Don't send DELADDR notification for TENTATIVE address,
4911 * since NEWADDR notification is sent only after removing
4914 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4917 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4921 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4923 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4924 WARN_ON(err == -EMSGSIZE);
4928 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4932 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4935 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4936 __s32 *array, int bytes)
4938 BUG_ON(bytes < (DEVCONF_MAX * 4));
4940 memset(array, 0, bytes);
4941 array[DEVCONF_FORWARDING] = cnf->forwarding;
4942 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4943 array[DEVCONF_MTU6] = cnf->mtu6;
4944 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4945 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4946 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4947 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4948 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4949 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4950 jiffies_to_msecs(cnf->rtr_solicit_interval);
4951 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4952 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
4953 array[DEVCONF_RTR_SOLICIT_DELAY] =
4954 jiffies_to_msecs(cnf->rtr_solicit_delay);
4955 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4956 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4957 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4958 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4959 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4960 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4961 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4962 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4963 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4964 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4965 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4966 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4967 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4968 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4969 #ifdef CONFIG_IPV6_ROUTER_PREF
4970 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4971 array[DEVCONF_RTR_PROBE_INTERVAL] =
4972 jiffies_to_msecs(cnf->rtr_probe_interval);
4973 #ifdef CONFIG_IPV6_ROUTE_INFO
4974 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4977 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4978 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4979 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4980 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4981 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4983 #ifdef CONFIG_IPV6_MROUTE
4984 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4986 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4987 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4988 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4989 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4990 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4991 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4992 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4993 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
4994 /* we omit DEVCONF_STABLE_SECRET for now */
4995 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4996 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
4997 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4998 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
4999 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5000 #ifdef CONFIG_IPV6_SEG6_HMAC
5001 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5003 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5004 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5007 static inline size_t inet6_ifla6_size(void)
5009 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5010 + nla_total_size(sizeof(struct ifla_cacheinfo))
5011 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5012 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5013 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5014 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5017 static inline size_t inet6_if_nlmsg_size(void)
5019 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5020 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5021 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5022 + nla_total_size(4) /* IFLA_MTU */
5023 + nla_total_size(4) /* IFLA_LINK */
5024 + nla_total_size(1) /* IFLA_OPERSTATE */
5025 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5028 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5032 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5035 /* Use put_unaligned() because stats may not be aligned for u64. */
5036 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5037 for (i = 1; i < ICMP6_MIB_MAX; i++)
5038 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5040 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5043 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5044 int bytes, size_t syncpoff)
5047 u64 buff[IPSTATS_MIB_MAX];
5048 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5052 memset(buff, 0, sizeof(buff));
5053 buff[0] = IPSTATS_MIB_MAX;
5055 for_each_possible_cpu(c) {
5056 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5057 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5060 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5061 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5064 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5068 case IFLA_INET6_STATS:
5069 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5070 offsetof(struct ipstats_mib, syncp));
5072 case IFLA_INET6_ICMP6STATS:
5073 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5078 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5079 u32 ext_filter_mask)
5082 struct ifla_cacheinfo ci;
5084 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5085 goto nla_put_failure;
5086 ci.max_reasm_len = IPV6_MAXPLEN;
5087 ci.tstamp = cstamp_delta(idev->tstamp);
5088 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5089 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5090 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5091 goto nla_put_failure;
5092 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5094 goto nla_put_failure;
5095 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5097 /* XXX - MC not implemented */
5099 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5102 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5104 goto nla_put_failure;
5105 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5107 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5109 goto nla_put_failure;
5110 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5112 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5114 goto nla_put_failure;
5116 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5117 goto nla_put_failure;
5119 read_lock_bh(&idev->lock);
5120 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5121 read_unlock_bh(&idev->lock);
5129 static size_t inet6_get_link_af_size(const struct net_device *dev,
5130 u32 ext_filter_mask)
5132 if (!__in6_dev_get(dev))
5135 return inet6_ifla6_size();
5138 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5139 u32 ext_filter_mask)
5141 struct inet6_dev *idev = __in6_dev_get(dev);
5146 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5152 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5154 struct inet6_ifaddr *ifp;
5155 struct net_device *dev = idev->dev;
5156 bool clear_token, update_rs = false;
5157 struct in6_addr ll_addr;
5163 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5165 if (!ipv6_accept_ra(idev))
5167 if (idev->cnf.rtr_solicits == 0)
5170 write_lock_bh(&idev->lock);
5172 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5173 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5175 write_unlock_bh(&idev->lock);
5177 clear_token = ipv6_addr_any(token);
5181 if (!idev->dead && (idev->if_flags & IF_READY) &&
5182 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5183 IFA_F_OPTIMISTIC)) {
5184 /* If we're not ready, then normal ifup will take care
5185 * of this. Otherwise, we need to request our rs here.
5187 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5192 write_lock_bh(&idev->lock);
5195 idev->if_flags |= IF_RS_SENT;
5196 idev->rs_interval = rfc3315_s14_backoff_init(
5197 idev->cnf.rtr_solicit_interval);
5198 idev->rs_probes = 1;
5199 addrconf_mod_rs_timer(idev, idev->rs_interval);
5202 /* Well, that's kinda nasty ... */
5203 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5204 spin_lock(&ifp->lock);
5205 if (ifp->tokenized) {
5207 ifp->prefered_lft = 0;
5209 spin_unlock(&ifp->lock);
5212 write_unlock_bh(&idev->lock);
5213 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5214 addrconf_verify_rtnl();
5218 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5219 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5220 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5223 static int inet6_validate_link_af(const struct net_device *dev,
5224 const struct nlattr *nla)
5226 struct nlattr *tb[IFLA_INET6_MAX + 1];
5228 if (dev && !__in6_dev_get(dev))
5229 return -EAFNOSUPPORT;
5231 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5234 static int check_addr_gen_mode(int mode)
5236 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5237 mode != IN6_ADDR_GEN_MODE_NONE &&
5238 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5239 mode != IN6_ADDR_GEN_MODE_RANDOM)
5244 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5247 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5248 !idev->cnf.stable_secret.initialized &&
5249 !net->ipv6.devconf_dflt->stable_secret.initialized)
5254 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5257 struct inet6_dev *idev = __in6_dev_get(dev);
5258 struct nlattr *tb[IFLA_INET6_MAX + 1];
5261 return -EAFNOSUPPORT;
5263 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5266 if (tb[IFLA_INET6_TOKEN]) {
5267 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5272 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5273 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5275 if (check_addr_gen_mode(mode) < 0 ||
5276 check_stable_privacy(idev, dev_net(dev), mode) < 0)
5279 idev->cnf.addr_gen_mode = mode;
5286 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5287 u32 portid, u32 seq, int event, unsigned int flags)
5289 struct net_device *dev = idev->dev;
5290 struct ifinfomsg *hdr;
5291 struct nlmsghdr *nlh;
5294 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5298 hdr = nlmsg_data(nlh);
5299 hdr->ifi_family = AF_INET6;
5301 hdr->ifi_type = dev->type;
5302 hdr->ifi_index = dev->ifindex;
5303 hdr->ifi_flags = dev_get_flags(dev);
5304 hdr->ifi_change = 0;
5306 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5308 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5309 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5310 (dev->ifindex != dev_get_iflink(dev) &&
5311 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5312 nla_put_u8(skb, IFLA_OPERSTATE,
5313 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5314 goto nla_put_failure;
5315 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5317 goto nla_put_failure;
5319 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5320 goto nla_put_failure;
5322 nla_nest_end(skb, protoinfo);
5323 nlmsg_end(skb, nlh);
5327 nlmsg_cancel(skb, nlh);
5331 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5333 struct net *net = sock_net(skb->sk);
5336 struct net_device *dev;
5337 struct inet6_dev *idev;
5338 struct hlist_head *head;
5341 s_idx = cb->args[1];
5344 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5346 head = &net->dev_index_head[h];
5347 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5350 idev = __in6_dev_get(dev);
5353 if (inet6_fill_ifinfo(skb, idev,
5354 NETLINK_CB(cb->skb).portid,
5356 RTM_NEWLINK, NLM_F_MULTI) < 0)
5370 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5372 struct sk_buff *skb;
5373 struct net *net = dev_net(idev->dev);
5376 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5380 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5382 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5383 WARN_ON(err == -EMSGSIZE);
5387 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5391 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5394 static inline size_t inet6_prefix_nlmsg_size(void)
5396 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5397 + nla_total_size(sizeof(struct in6_addr))
5398 + nla_total_size(sizeof(struct prefix_cacheinfo));
5401 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5402 struct prefix_info *pinfo, u32 portid, u32 seq,
5403 int event, unsigned int flags)
5405 struct prefixmsg *pmsg;
5406 struct nlmsghdr *nlh;
5407 struct prefix_cacheinfo ci;
5409 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5413 pmsg = nlmsg_data(nlh);
5414 pmsg->prefix_family = AF_INET6;
5415 pmsg->prefix_pad1 = 0;
5416 pmsg->prefix_pad2 = 0;
5417 pmsg->prefix_ifindex = idev->dev->ifindex;
5418 pmsg->prefix_len = pinfo->prefix_len;
5419 pmsg->prefix_type = pinfo->type;
5420 pmsg->prefix_pad3 = 0;
5421 pmsg->prefix_flags = 0;
5423 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5424 if (pinfo->autoconf)
5425 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5427 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5428 goto nla_put_failure;
5429 ci.preferred_time = ntohl(pinfo->prefered);
5430 ci.valid_time = ntohl(pinfo->valid);
5431 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5432 goto nla_put_failure;
5433 nlmsg_end(skb, nlh);
5437 nlmsg_cancel(skb, nlh);
5441 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5442 struct prefix_info *pinfo)
5444 struct sk_buff *skb;
5445 struct net *net = dev_net(idev->dev);
5448 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5452 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5454 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5455 WARN_ON(err == -EMSGSIZE);
5459 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5463 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5466 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5468 struct net *net = dev_net(ifp->idev->dev);
5473 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5478 * If the address was optimistic
5479 * we inserted the route at the start of
5480 * our DAD process, so we don't need
5483 if (!(ifp->rt->rt6i_node))
5484 ip6_ins_rt(ifp->rt);
5485 if (ifp->idev->cnf.forwarding)
5486 addrconf_join_anycast(ifp);
5487 if (!ipv6_addr_any(&ifp->peer_addr))
5488 addrconf_prefix_route(&ifp->peer_addr, 128,
5489 ifp->idev->dev, 0, 0);
5492 if (ifp->idev->cnf.forwarding)
5493 addrconf_leave_anycast(ifp);
5494 addrconf_leave_solict(ifp->idev, &ifp->addr);
5495 if (!ipv6_addr_any(&ifp->peer_addr)) {
5496 struct rt6_info *rt;
5498 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5499 ifp->idev->dev, 0, 0);
5504 dst_hold(&ifp->rt->dst);
5505 ip6_del_rt(ifp->rt);
5507 rt_genid_bump_ipv6(net);
5510 atomic_inc(&net->ipv6.dev_addr_genid);
5513 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5516 if (likely(ifp->idev->dead == 0))
5517 __ipv6_ifa_notify(event, ifp);
5518 rcu_read_unlock_bh();
5521 #ifdef CONFIG_SYSCTL
5524 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5525 void __user *buffer, size_t *lenp, loff_t *ppos)
5527 int *valp = ctl->data;
5530 struct ctl_table lctl;
5534 * ctl->data points to idev->cnf.forwarding, we should
5535 * not modify it until we get the rtnl lock.
5540 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5543 ret = addrconf_fixup_forwarding(ctl, valp, val);
5550 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5551 void __user *buffer, size_t *lenp, loff_t *ppos)
5553 struct inet6_dev *idev = ctl->extra1;
5554 int min_mtu = IPV6_MIN_MTU;
5555 struct ctl_table lctl;
5558 lctl.extra1 = &min_mtu;
5559 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5561 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5564 static void dev_disable_change(struct inet6_dev *idev)
5566 struct netdev_notifier_info info;
5568 if (!idev || !idev->dev)
5571 netdev_notifier_info_init(&info, idev->dev);
5572 if (idev->cnf.disable_ipv6)
5573 addrconf_notify(NULL, NETDEV_DOWN, &info);
5575 addrconf_notify(NULL, NETDEV_UP, &info);
5578 static void addrconf_disable_change(struct net *net, __s32 newf)
5580 struct net_device *dev;
5581 struct inet6_dev *idev;
5583 for_each_netdev(net, dev) {
5584 idev = __in6_dev_get(dev);
5586 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5587 idev->cnf.disable_ipv6 = newf;
5589 dev_disable_change(idev);
5594 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5599 if (!rtnl_trylock())
5600 return restart_syscall();
5602 net = (struct net *)table->extra2;
5606 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5611 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5612 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5613 addrconf_disable_change(net, newf);
5614 } else if ((!newf) ^ (!old))
5615 dev_disable_change((struct inet6_dev *)table->extra1);
5622 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5623 void __user *buffer, size_t *lenp, loff_t *ppos)
5625 int *valp = ctl->data;
5628 struct ctl_table lctl;
5632 * ctl->data points to idev->cnf.disable_ipv6, we should
5633 * not modify it until we get the rtnl lock.
5638 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5641 ret = addrconf_disable_ipv6(ctl, valp, val);
5648 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5649 void __user *buffer, size_t *lenp, loff_t *ppos)
5651 int *valp = ctl->data;
5656 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5659 if (write && old != new) {
5660 struct net *net = ctl->extra2;
5662 if (!rtnl_trylock())
5663 return restart_syscall();
5665 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5666 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5667 NETCONFA_IFINDEX_DEFAULT,
5668 net->ipv6.devconf_dflt);
5669 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5670 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5671 NETCONFA_IFINDEX_ALL,
5672 net->ipv6.devconf_all);
5674 struct inet6_dev *idev = ctl->extra1;
5676 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5686 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5687 void __user *buffer, size_t *lenp,
5692 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5693 struct net *net = (struct net *)ctl->extra2;
5695 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5698 new_val = *((int *)ctl->data);
5700 if (check_addr_gen_mode(new_val) < 0)
5703 /* request for default */
5704 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5705 ipv6_devconf_dflt.addr_gen_mode = new_val;
5707 /* request for individual net device */
5712 if (check_stable_privacy(idev, net, new_val) < 0)
5715 if (idev->cnf.addr_gen_mode != new_val) {
5716 idev->cnf.addr_gen_mode = new_val;
5718 addrconf_dev_config(idev->dev);
5727 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5728 void __user *buffer, size_t *lenp,
5732 struct in6_addr addr;
5733 char str[IPV6_MAX_STRLEN];
5734 struct ctl_table lctl = *ctl;
5735 struct net *net = ctl->extra2;
5736 struct ipv6_stable_secret *secret = ctl->data;
5738 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5741 lctl.maxlen = IPV6_MAX_STRLEN;
5744 if (!rtnl_trylock())
5745 return restart_syscall();
5747 if (!write && !secret->initialized) {
5752 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5753 if (err >= sizeof(str)) {
5758 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5762 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5767 secret->initialized = true;
5768 secret->secret = addr;
5770 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5771 struct net_device *dev;
5773 for_each_netdev(net, dev) {
5774 struct inet6_dev *idev = __in6_dev_get(dev);
5777 idev->cnf.addr_gen_mode =
5778 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5782 struct inet6_dev *idev = ctl->extra1;
5784 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5794 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5796 void __user *buffer,
5800 int *valp = ctl->data;
5803 struct ctl_table lctl;
5806 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5807 * we should not modify it until we get the rtnl lock.
5812 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5815 ret = addrconf_fixup_linkdown(ctl, valp, val);
5821 static int minus_one = -1;
5822 static const int one = 1;
5823 static const int two_five_five = 255;
5825 static const struct ctl_table addrconf_sysctl[] = {
5827 .procname = "forwarding",
5828 .data = &ipv6_devconf.forwarding,
5829 .maxlen = sizeof(int),
5831 .proc_handler = addrconf_sysctl_forward,
5834 .procname = "hop_limit",
5835 .data = &ipv6_devconf.hop_limit,
5836 .maxlen = sizeof(int),
5838 .proc_handler = proc_dointvec_minmax,
5839 .extra1 = (void *)&one,
5840 .extra2 = (void *)&two_five_five,
5844 .data = &ipv6_devconf.mtu6,
5845 .maxlen = sizeof(int),
5847 .proc_handler = addrconf_sysctl_mtu,
5850 .procname = "accept_ra",
5851 .data = &ipv6_devconf.accept_ra,
5852 .maxlen = sizeof(int),
5854 .proc_handler = proc_dointvec,
5857 .procname = "accept_redirects",
5858 .data = &ipv6_devconf.accept_redirects,
5859 .maxlen = sizeof(int),
5861 .proc_handler = proc_dointvec,
5864 .procname = "autoconf",
5865 .data = &ipv6_devconf.autoconf,
5866 .maxlen = sizeof(int),
5868 .proc_handler = proc_dointvec,
5871 .procname = "dad_transmits",
5872 .data = &ipv6_devconf.dad_transmits,
5873 .maxlen = sizeof(int),
5875 .proc_handler = proc_dointvec,
5878 .procname = "router_solicitations",
5879 .data = &ipv6_devconf.rtr_solicits,
5880 .maxlen = sizeof(int),
5882 .proc_handler = proc_dointvec_minmax,
5883 .extra1 = &minus_one,
5886 .procname = "router_solicitation_interval",
5887 .data = &ipv6_devconf.rtr_solicit_interval,
5888 .maxlen = sizeof(int),
5890 .proc_handler = proc_dointvec_jiffies,
5893 .procname = "router_solicitation_max_interval",
5894 .data = &ipv6_devconf.rtr_solicit_max_interval,
5895 .maxlen = sizeof(int),
5897 .proc_handler = proc_dointvec_jiffies,
5900 .procname = "router_solicitation_delay",
5901 .data = &ipv6_devconf.rtr_solicit_delay,
5902 .maxlen = sizeof(int),
5904 .proc_handler = proc_dointvec_jiffies,
5907 .procname = "force_mld_version",
5908 .data = &ipv6_devconf.force_mld_version,
5909 .maxlen = sizeof(int),
5911 .proc_handler = proc_dointvec,
5914 .procname = "mldv1_unsolicited_report_interval",
5916 &ipv6_devconf.mldv1_unsolicited_report_interval,
5917 .maxlen = sizeof(int),
5919 .proc_handler = proc_dointvec_ms_jiffies,
5922 .procname = "mldv2_unsolicited_report_interval",
5924 &ipv6_devconf.mldv2_unsolicited_report_interval,
5925 .maxlen = sizeof(int),
5927 .proc_handler = proc_dointvec_ms_jiffies,
5930 .procname = "use_tempaddr",
5931 .data = &ipv6_devconf.use_tempaddr,
5932 .maxlen = sizeof(int),
5934 .proc_handler = proc_dointvec,
5937 .procname = "temp_valid_lft",
5938 .data = &ipv6_devconf.temp_valid_lft,
5939 .maxlen = sizeof(int),
5941 .proc_handler = proc_dointvec,
5944 .procname = "temp_prefered_lft",
5945 .data = &ipv6_devconf.temp_prefered_lft,
5946 .maxlen = sizeof(int),
5948 .proc_handler = proc_dointvec,
5951 .procname = "regen_max_retry",
5952 .data = &ipv6_devconf.regen_max_retry,
5953 .maxlen = sizeof(int),
5955 .proc_handler = proc_dointvec,
5958 .procname = "max_desync_factor",
5959 .data = &ipv6_devconf.max_desync_factor,
5960 .maxlen = sizeof(int),
5962 .proc_handler = proc_dointvec,
5965 .procname = "max_addresses",
5966 .data = &ipv6_devconf.max_addresses,
5967 .maxlen = sizeof(int),
5969 .proc_handler = proc_dointvec,
5972 .procname = "accept_ra_defrtr",
5973 .data = &ipv6_devconf.accept_ra_defrtr,
5974 .maxlen = sizeof(int),
5976 .proc_handler = proc_dointvec,
5979 .procname = "accept_ra_min_hop_limit",
5980 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5981 .maxlen = sizeof(int),
5983 .proc_handler = proc_dointvec,
5986 .procname = "accept_ra_pinfo",
5987 .data = &ipv6_devconf.accept_ra_pinfo,
5988 .maxlen = sizeof(int),
5990 .proc_handler = proc_dointvec,
5992 #ifdef CONFIG_IPV6_ROUTER_PREF
5994 .procname = "accept_ra_rtr_pref",
5995 .data = &ipv6_devconf.accept_ra_rtr_pref,
5996 .maxlen = sizeof(int),
5998 .proc_handler = proc_dointvec,
6001 .procname = "router_probe_interval",
6002 .data = &ipv6_devconf.rtr_probe_interval,
6003 .maxlen = sizeof(int),
6005 .proc_handler = proc_dointvec_jiffies,
6007 #ifdef CONFIG_IPV6_ROUTE_INFO
6009 .procname = "accept_ra_rt_info_max_plen",
6010 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6011 .maxlen = sizeof(int),
6013 .proc_handler = proc_dointvec,
6018 .procname = "proxy_ndp",
6019 .data = &ipv6_devconf.proxy_ndp,
6020 .maxlen = sizeof(int),
6022 .proc_handler = addrconf_sysctl_proxy_ndp,
6025 .procname = "accept_source_route",
6026 .data = &ipv6_devconf.accept_source_route,
6027 .maxlen = sizeof(int),
6029 .proc_handler = proc_dointvec,
6031 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6033 .procname = "optimistic_dad",
6034 .data = &ipv6_devconf.optimistic_dad,
6035 .maxlen = sizeof(int),
6037 .proc_handler = proc_dointvec,
6040 .procname = "use_optimistic",
6041 .data = &ipv6_devconf.use_optimistic,
6042 .maxlen = sizeof(int),
6044 .proc_handler = proc_dointvec,
6047 #ifdef CONFIG_IPV6_MROUTE
6049 .procname = "mc_forwarding",
6050 .data = &ipv6_devconf.mc_forwarding,
6051 .maxlen = sizeof(int),
6053 .proc_handler = proc_dointvec,
6057 .procname = "disable_ipv6",
6058 .data = &ipv6_devconf.disable_ipv6,
6059 .maxlen = sizeof(int),
6061 .proc_handler = addrconf_sysctl_disable,
6064 .procname = "accept_dad",
6065 .data = &ipv6_devconf.accept_dad,
6066 .maxlen = sizeof(int),
6068 .proc_handler = proc_dointvec,
6071 .procname = "force_tllao",
6072 .data = &ipv6_devconf.force_tllao,
6073 .maxlen = sizeof(int),
6075 .proc_handler = proc_dointvec
6078 .procname = "ndisc_notify",
6079 .data = &ipv6_devconf.ndisc_notify,
6080 .maxlen = sizeof(int),
6082 .proc_handler = proc_dointvec
6085 .procname = "suppress_frag_ndisc",
6086 .data = &ipv6_devconf.suppress_frag_ndisc,
6087 .maxlen = sizeof(int),
6089 .proc_handler = proc_dointvec
6092 .procname = "accept_ra_from_local",
6093 .data = &ipv6_devconf.accept_ra_from_local,
6094 .maxlen = sizeof(int),
6096 .proc_handler = proc_dointvec,
6099 .procname = "accept_ra_mtu",
6100 .data = &ipv6_devconf.accept_ra_mtu,
6101 .maxlen = sizeof(int),
6103 .proc_handler = proc_dointvec,
6106 .procname = "stable_secret",
6107 .data = &ipv6_devconf.stable_secret,
6108 .maxlen = IPV6_MAX_STRLEN,
6110 .proc_handler = addrconf_sysctl_stable_secret,
6113 .procname = "use_oif_addrs_only",
6114 .data = &ipv6_devconf.use_oif_addrs_only,
6115 .maxlen = sizeof(int),
6117 .proc_handler = proc_dointvec,
6120 .procname = "ignore_routes_with_linkdown",
6121 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6122 .maxlen = sizeof(int),
6124 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6127 .procname = "drop_unicast_in_l2_multicast",
6128 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6129 .maxlen = sizeof(int),
6131 .proc_handler = proc_dointvec,
6134 .procname = "drop_unsolicited_na",
6135 .data = &ipv6_devconf.drop_unsolicited_na,
6136 .maxlen = sizeof(int),
6138 .proc_handler = proc_dointvec,
6141 .procname = "keep_addr_on_down",
6142 .data = &ipv6_devconf.keep_addr_on_down,
6143 .maxlen = sizeof(int),
6145 .proc_handler = proc_dointvec,
6149 .procname = "seg6_enabled",
6150 .data = &ipv6_devconf.seg6_enabled,
6151 .maxlen = sizeof(int),
6153 .proc_handler = proc_dointvec,
6155 #ifdef CONFIG_IPV6_SEG6_HMAC
6157 .procname = "seg6_require_hmac",
6158 .data = &ipv6_devconf.seg6_require_hmac,
6159 .maxlen = sizeof(int),
6161 .proc_handler = proc_dointvec,
6165 .procname = "enhanced_dad",
6166 .data = &ipv6_devconf.enhanced_dad,
6167 .maxlen = sizeof(int),
6169 .proc_handler = proc_dointvec,
6172 .procname = "addr_gen_mode",
6173 .data = &ipv6_devconf.addr_gen_mode,
6174 .maxlen = sizeof(int),
6176 .proc_handler = addrconf_sysctl_addr_gen_mode,
6183 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6184 struct inet6_dev *idev, struct ipv6_devconf *p)
6187 struct ctl_table *table;
6188 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6190 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6194 for (i = 0; table[i].data; i++) {
6195 table[i].data += (char *)p - (char *)&ipv6_devconf;
6196 /* If one of these is already set, then it is not safe to
6197 * overwrite either of them: this makes proc_dointvec_minmax
6200 if (!table[i].extra1 && !table[i].extra2) {
6201 table[i].extra1 = idev; /* embedded; no ref */
6202 table[i].extra2 = net;
6206 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6208 p->sysctl_header = register_net_sysctl(net, path, table);
6209 if (!p->sysctl_header)
6212 if (!strcmp(dev_name, "all"))
6213 ifindex = NETCONFA_IFINDEX_ALL;
6214 else if (!strcmp(dev_name, "default"))
6215 ifindex = NETCONFA_IFINDEX_DEFAULT;
6217 ifindex = idev->dev->ifindex;
6218 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
6227 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6229 struct ctl_table *table;
6231 if (!p->sysctl_header)
6234 table = p->sysctl_header->ctl_table_arg;
6235 unregister_net_sysctl_table(p->sysctl_header);
6236 p->sysctl_header = NULL;
6240 static int addrconf_sysctl_register(struct inet6_dev *idev)
6244 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6247 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6248 &ndisc_ifinfo_sysctl_change);
6251 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6254 neigh_sysctl_unregister(idev->nd_parms);
6259 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6261 __addrconf_sysctl_unregister(&idev->cnf);
6262 neigh_sysctl_unregister(idev->nd_parms);
6268 static int __net_init addrconf_init_net(struct net *net)
6271 struct ipv6_devconf *all, *dflt;
6273 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6277 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6279 goto err_alloc_dflt;
6281 /* these will be inherited by all namespaces */
6282 dflt->autoconf = ipv6_defaults.autoconf;
6283 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6285 dflt->stable_secret.initialized = false;
6286 all->stable_secret.initialized = false;
6288 net->ipv6.devconf_all = all;
6289 net->ipv6.devconf_dflt = dflt;
6291 #ifdef CONFIG_SYSCTL
6292 err = __addrconf_sysctl_register(net, "all", NULL, all);
6296 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6302 #ifdef CONFIG_SYSCTL
6304 __addrconf_sysctl_unregister(all);
6314 static void __net_exit addrconf_exit_net(struct net *net)
6316 #ifdef CONFIG_SYSCTL
6317 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6318 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6320 kfree(net->ipv6.devconf_dflt);
6321 kfree(net->ipv6.devconf_all);
6324 static struct pernet_operations addrconf_ops = {
6325 .init = addrconf_init_net,
6326 .exit = addrconf_exit_net,
6329 static struct rtnl_af_ops inet6_ops __read_mostly = {
6331 .fill_link_af = inet6_fill_link_af,
6332 .get_link_af_size = inet6_get_link_af_size,
6333 .validate_link_af = inet6_validate_link_af,
6334 .set_link_af = inet6_set_link_af,
6338 * Init / cleanup code
6341 int __init addrconf_init(void)
6343 struct inet6_dev *idev;
6346 err = ipv6_addr_label_init();
6348 pr_crit("%s: cannot initialize default policy table: %d\n",
6353 err = register_pernet_subsys(&addrconf_ops);
6357 addrconf_wq = create_workqueue("ipv6_addrconf");
6363 /* The addrconf netdev notifier requires that loopback_dev
6364 * has it's ipv6 private information allocated and setup
6365 * before it can bring up and give link-local addresses
6366 * to other devices which are up.
6368 * Unfortunately, loopback_dev is not necessarily the first
6369 * entry in the global dev_base list of net devices. In fact,
6370 * it is likely to be the very last entry on that list.
6371 * So this causes the notifier registry below to try and
6372 * give link-local addresses to all devices besides loopback_dev
6373 * first, then loopback_dev, which cases all the non-loopback_dev
6374 * devices to fail to get a link-local address.
6376 * So, as a temporary fix, allocate the ipv6 structure for
6377 * loopback_dev first by hand.
6378 * Longer term, all of the dependencies ipv6 has upon the loopback
6379 * device and it being up should be removed.
6382 idev = ipv6_add_dev(init_net.loopback_dev);
6385 err = PTR_ERR(idev);
6389 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6390 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6392 register_netdevice_notifier(&ipv6_dev_notf);
6396 rtnl_af_register(&inet6_ops);
6398 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6403 /* Only the first call to __rtnl_register can fail */
6404 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6405 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6406 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6407 inet6_dump_ifaddr, NULL);
6408 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6409 inet6_dump_ifmcaddr, NULL);
6410 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6411 inet6_dump_ifacaddr, NULL);
6412 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6413 inet6_netconf_dump_devconf, NULL);
6415 ipv6_addr_label_rtnl_register();
6419 rtnl_af_unregister(&inet6_ops);
6420 unregister_netdevice_notifier(&ipv6_dev_notf);
6422 destroy_workqueue(addrconf_wq);
6424 unregister_pernet_subsys(&addrconf_ops);
6426 ipv6_addr_label_cleanup();
6431 void addrconf_cleanup(void)
6433 struct net_device *dev;
6436 unregister_netdevice_notifier(&ipv6_dev_notf);
6437 unregister_pernet_subsys(&addrconf_ops);
6438 ipv6_addr_label_cleanup();
6442 __rtnl_af_unregister(&inet6_ops);
6444 /* clean dev list */
6445 for_each_netdev(&init_net, dev) {
6446 if (__in6_dev_get(dev) == NULL)
6448 addrconf_ifdown(dev, 1);
6450 addrconf_ifdown(init_net.loopback_dev, 2);
6455 spin_lock_bh(&addrconf_hash_lock);
6456 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6457 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
6458 spin_unlock_bh(&addrconf_hash_lock);
6459 cancel_delayed_work(&addr_chk_work);
6462 destroy_workqueue(addrconf_wq);