2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/sched/signal.h>
47 #include <linux/socket.h>
48 #include <linux/sockios.h>
49 #include <linux/net.h>
50 #include <linux/inet.h>
51 #include <linux/in6.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_addr.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_arcnet.h>
56 #include <linux/if_infiniband.h>
57 #include <linux/route.h>
58 #include <linux/inetdevice.h>
59 #include <linux/init.h>
60 #include <linux/slab.h>
62 #include <linux/sysctl.h>
64 #include <linux/capability.h>
65 #include <linux/delay.h>
66 #include <linux/notifier.h>
67 #include <linux/string.h>
68 #include <linux/hash.h>
70 #include <net/net_namespace.h>
74 #include <net/6lowpan.h>
75 #include <net/firewire.h>
77 #include <net/protocol.h>
78 #include <net/ndisc.h>
79 #include <net/ip6_route.h>
80 #include <net/addrconf.h>
83 #include <net/netlink.h>
84 #include <net/pkt_sched.h>
85 #include <net/l3mdev.h>
86 #include <linux/if_tunnel.h>
87 #include <linux/rtnetlink.h>
88 #include <linux/netconf.h>
89 #include <linux/random.h>
90 #include <linux/uaccess.h>
91 #include <asm/unaligned.h>
93 #include <linux/proc_fs.h>
94 #include <linux/seq_file.h>
95 #include <linux/export.h>
97 /* Set to 3 to get tracing... */
101 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
103 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
106 #define INFINITY_LIFE_TIME 0xFFFFFFFF
108 #define IPV6_MAX_STRLEN \
109 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
111 static inline u32 cstamp_delta(unsigned long cstamp)
113 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
116 static inline s32 rfc3315_s14_backoff_init(s32 irt)
118 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120 do_div(tmp, 1000000);
124 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
126 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128 do_div(tmp, 1000000);
129 if ((s32)tmp > mrt) {
130 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132 do_div(tmp, 1000000);
138 static int addrconf_sysctl_register(struct inet6_dev *idev);
139 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
141 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
146 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
151 static void ipv6_regen_rndid(struct inet6_dev *idev);
152 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
154 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
155 static int ipv6_count_addresses(struct inet6_dev *idev);
156 static int ipv6_generate_stable_address(struct in6_addr *addr,
158 const struct inet6_dev *idev);
161 * Configured unicast address hash table
163 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
164 static DEFINE_SPINLOCK(addrconf_hash_lock);
166 static void addrconf_verify(void);
167 static void addrconf_verify_rtnl(void);
168 static void addrconf_verify_work(struct work_struct *);
170 static struct workqueue_struct *addrconf_wq;
171 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
173 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
176 static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
178 static int addrconf_ifdown(struct net_device *dev, int how);
180 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
185 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
186 static void addrconf_dad_work(struct work_struct *w);
187 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
188 static void addrconf_dad_run(struct inet6_dev *idev);
189 static void addrconf_rs_timer(unsigned long data);
190 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
193 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
194 struct prefix_info *pinfo);
195 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
198 static struct ipv6_devconf ipv6_devconf __read_mostly = {
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
203 .accept_redirects = 1,
205 .force_mld_version = 0,
206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
218 .max_addresses = IPV6_MAX_ADDRESSES,
219 .accept_ra_defrtr = 1,
220 .accept_ra_from_local = 0,
221 .accept_ra_min_hop_limit= 1,
222 .accept_ra_pinfo = 1,
223 #ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
225 .rtr_probe_interval = 60 * HZ,
226 #ifdef CONFIG_IPV6_ROUTE_INFO
227 .accept_ra_rt_info_max_plen = 0,
231 .accept_source_route = 0, /* we do not accept RH0 by default. */
234 .suppress_frag_ndisc = 1,
237 .initialized = false,
239 .use_oif_addrs_only = 0,
240 .ignore_routes_with_linkdown = 0,
241 .keep_addr_on_down = 0,
243 #ifdef CONFIG_IPV6_SEG6_HMAC
244 .seg6_require_hmac = 0,
247 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
250 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
252 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
253 .mtu6 = IPV6_MIN_MTU,
255 .accept_redirects = 1,
257 .force_mld_version = 0,
258 .mldv1_unsolicited_report_interval = 10 * HZ,
259 .mldv2_unsolicited_report_interval = HZ,
261 .rtr_solicits = MAX_RTR_SOLICITATIONS,
262 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
263 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
264 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
266 .temp_valid_lft = TEMP_VALID_LIFETIME,
267 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
268 .regen_max_retry = REGEN_MAX_RETRY,
269 .max_desync_factor = MAX_DESYNC_FACTOR,
270 .max_addresses = IPV6_MAX_ADDRESSES,
271 .accept_ra_defrtr = 1,
272 .accept_ra_from_local = 0,
273 .accept_ra_min_hop_limit= 1,
274 .accept_ra_pinfo = 1,
275 #ifdef CONFIG_IPV6_ROUTER_PREF
276 .accept_ra_rtr_pref = 1,
277 .rtr_probe_interval = 60 * HZ,
278 #ifdef CONFIG_IPV6_ROUTE_INFO
279 .accept_ra_rt_info_max_plen = 0,
283 .accept_source_route = 0, /* we do not accept RH0 by default. */
286 .suppress_frag_ndisc = 1,
289 .initialized = false,
291 .use_oif_addrs_only = 0,
292 .ignore_routes_with_linkdown = 0,
293 .keep_addr_on_down = 0,
295 #ifdef CONFIG_IPV6_SEG6_HMAC
296 .seg6_require_hmac = 0,
299 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
302 /* Check if a valid qdisc is available */
303 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
305 return !qdisc_tx_is_noop(dev);
308 static void addrconf_del_rs_timer(struct inet6_dev *idev)
310 if (del_timer(&idev->rs_timer))
314 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
316 if (cancel_delayed_work(&ifp->dad_work))
320 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
323 if (!timer_pending(&idev->rs_timer))
325 mod_timer(&idev->rs_timer, jiffies + when);
328 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
331 if (!delayed_work_pending(&ifp->dad_work))
333 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
336 static int snmp6_alloc_dev(struct inet6_dev *idev)
340 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
341 if (!idev->stats.ipv6)
344 for_each_possible_cpu(i) {
345 struct ipstats_mib *addrconf_stats;
346 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
347 u64_stats_init(&addrconf_stats->syncp);
351 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
353 if (!idev->stats.icmpv6dev)
355 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
357 if (!idev->stats.icmpv6msgdev)
363 kfree(idev->stats.icmpv6dev);
365 free_percpu(idev->stats.ipv6);
370 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
372 struct inet6_dev *ndev;
377 if (dev->mtu < IPV6_MIN_MTU)
378 return ERR_PTR(-EINVAL);
380 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
384 rwlock_init(&ndev->lock);
386 INIT_LIST_HEAD(&ndev->addr_list);
387 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
388 (unsigned long)ndev);
389 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
391 if (ndev->cnf.stable_secret.initialized)
392 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
394 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
396 ndev->cnf.mtu6 = dev->mtu;
397 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
398 if (!ndev->nd_parms) {
402 if (ndev->cnf.forwarding)
403 dev_disable_lro(dev);
404 /* We refer to the device */
407 if (snmp6_alloc_dev(ndev) < 0) {
409 "%s: cannot allocate memory for statistics; dev=%s.\n",
410 __func__, dev->name);
411 neigh_parms_release(&nd_tbl, ndev->nd_parms);
417 if (snmp6_register_dev(ndev) < 0) {
419 "%s: cannot create /proc/net/dev_snmp6/%s\n",
420 __func__, dev->name);
424 /* One reference from device. */
427 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
428 ndev->cnf.accept_dad = -1;
430 #if IS_ENABLED(CONFIG_IPV6_SIT)
431 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
432 pr_info("%s: Disabled Multicast RS\n", dev->name);
433 ndev->cnf.rtr_solicits = 0;
437 INIT_LIST_HEAD(&ndev->tempaddr_list);
438 ndev->desync_factor = U32_MAX;
439 if ((dev->flags&IFF_LOOPBACK) ||
440 dev->type == ARPHRD_TUNNEL ||
441 dev->type == ARPHRD_TUNNEL6 ||
442 dev->type == ARPHRD_SIT ||
443 dev->type == ARPHRD_NONE) {
444 ndev->cnf.use_tempaddr = -1;
446 ipv6_regen_rndid(ndev);
448 ndev->token = in6addr_any;
450 if (netif_running(dev) && addrconf_qdisc_ok(dev))
451 ndev->if_flags |= IF_READY;
453 ipv6_mc_init_dev(ndev);
454 ndev->tstamp = jiffies;
455 err = addrconf_sysctl_register(ndev);
457 ipv6_mc_destroy_dev(ndev);
458 snmp6_unregister_dev(ndev);
461 /* protected by rtnl_lock */
462 rcu_assign_pointer(dev->ip6_ptr, ndev);
464 /* Join interface-local all-node multicast group */
465 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
467 /* Join all-node multicast group */
468 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
470 /* Join all-router multicast group if forwarding is set */
471 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
472 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
477 neigh_parms_release(&nd_tbl, ndev->nd_parms);
479 in6_dev_finish_destroy(ndev);
483 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
485 struct inet6_dev *idev;
489 idev = __in6_dev_get(dev);
491 idev = ipv6_add_dev(dev);
496 if (dev->flags&IFF_UP)
501 static int inet6_netconf_msgsize_devconf(int type)
503 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
504 + nla_total_size(4); /* NETCONFA_IFINDEX */
507 if (type == NETCONFA_ALL)
510 if (all || type == NETCONFA_FORWARDING)
511 size += nla_total_size(4);
512 #ifdef CONFIG_IPV6_MROUTE
513 if (all || type == NETCONFA_MC_FORWARDING)
514 size += nla_total_size(4);
516 if (all || type == NETCONFA_PROXY_NEIGH)
517 size += nla_total_size(4);
519 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
520 size += nla_total_size(4);
525 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
526 struct ipv6_devconf *devconf, u32 portid,
527 u32 seq, int event, unsigned int flags,
530 struct nlmsghdr *nlh;
531 struct netconfmsg *ncm;
534 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
539 if (type == NETCONFA_ALL)
542 ncm = nlmsg_data(nlh);
543 ncm->ncm_family = AF_INET6;
545 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
546 goto nla_put_failure;
548 if ((all || type == NETCONFA_FORWARDING) &&
549 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
550 goto nla_put_failure;
551 #ifdef CONFIG_IPV6_MROUTE
552 if ((all || type == NETCONFA_MC_FORWARDING) &&
553 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
554 devconf->mc_forwarding) < 0)
555 goto nla_put_failure;
557 if ((all || type == NETCONFA_PROXY_NEIGH) &&
558 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
559 goto nla_put_failure;
561 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
562 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
563 devconf->ignore_routes_with_linkdown) < 0)
564 goto nla_put_failure;
570 nlmsg_cancel(skb, nlh);
574 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
575 struct ipv6_devconf *devconf)
580 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
584 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
585 RTM_NEWNETCONF, 0, type);
587 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
588 WARN_ON(err == -EMSGSIZE);
592 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
595 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
598 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
599 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
600 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
601 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
602 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
605 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
606 struct nlmsghdr *nlh)
608 struct net *net = sock_net(in_skb->sk);
609 struct nlattr *tb[NETCONFA_MAX+1];
610 struct netconfmsg *ncm;
612 struct ipv6_devconf *devconf;
613 struct inet6_dev *in6_dev;
614 struct net_device *dev;
618 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
619 devconf_ipv6_policy);
624 if (!tb[NETCONFA_IFINDEX])
627 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
629 case NETCONFA_IFINDEX_ALL:
630 devconf = net->ipv6.devconf_all;
632 case NETCONFA_IFINDEX_DEFAULT:
633 devconf = net->ipv6.devconf_dflt;
636 dev = __dev_get_by_index(net, ifindex);
639 in6_dev = __in6_dev_get(dev);
642 devconf = &in6_dev->cnf;
647 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
651 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
652 NETLINK_CB(in_skb).portid,
653 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
656 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
657 WARN_ON(err == -EMSGSIZE);
661 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
666 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
667 struct netlink_callback *cb)
669 struct net *net = sock_net(skb->sk);
672 struct net_device *dev;
673 struct inet6_dev *idev;
674 struct hlist_head *head;
677 s_idx = idx = cb->args[1];
679 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
681 head = &net->dev_index_head[h];
683 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
685 hlist_for_each_entry_rcu(dev, head, index_hlist) {
688 idev = __in6_dev_get(dev);
692 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
694 NETLINK_CB(cb->skb).portid,
702 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
708 if (h == NETDEV_HASHENTRIES) {
709 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
710 net->ipv6.devconf_all,
711 NETLINK_CB(cb->skb).portid,
713 RTM_NEWNETCONF, NLM_F_MULTI,
719 if (h == NETDEV_HASHENTRIES + 1) {
720 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
721 net->ipv6.devconf_dflt,
722 NETLINK_CB(cb->skb).portid,
724 RTM_NEWNETCONF, NLM_F_MULTI,
738 static void dev_forward_change(struct inet6_dev *idev)
740 struct net_device *dev;
741 struct inet6_ifaddr *ifa;
746 if (idev->cnf.forwarding)
747 dev_disable_lro(dev);
748 if (dev->flags & IFF_MULTICAST) {
749 if (idev->cnf.forwarding) {
750 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
751 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
752 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
754 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
755 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
756 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
760 list_for_each_entry(ifa, &idev->addr_list, if_list) {
761 if (ifa->flags&IFA_F_TENTATIVE)
763 if (idev->cnf.forwarding)
764 addrconf_join_anycast(ifa);
766 addrconf_leave_anycast(ifa);
768 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
769 dev->ifindex, &idev->cnf);
773 static void addrconf_forward_change(struct net *net, __s32 newf)
775 struct net_device *dev;
776 struct inet6_dev *idev;
778 for_each_netdev(net, dev) {
779 idev = __in6_dev_get(dev);
781 int changed = (!idev->cnf.forwarding) ^ (!newf);
782 idev->cnf.forwarding = newf;
784 dev_forward_change(idev);
789 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
795 return restart_syscall();
797 net = (struct net *)table->extra2;
801 if (p == &net->ipv6.devconf_dflt->forwarding) {
802 if ((!newf) ^ (!old))
803 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
804 NETCONFA_IFINDEX_DEFAULT,
805 net->ipv6.devconf_dflt);
810 if (p == &net->ipv6.devconf_all->forwarding) {
811 int old_dflt = net->ipv6.devconf_dflt->forwarding;
813 net->ipv6.devconf_dflt->forwarding = newf;
814 if ((!newf) ^ (!old_dflt))
815 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
816 NETCONFA_IFINDEX_DEFAULT,
817 net->ipv6.devconf_dflt);
819 addrconf_forward_change(net, newf);
820 if ((!newf) ^ (!old))
821 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
822 NETCONFA_IFINDEX_ALL,
823 net->ipv6.devconf_all);
824 } else if ((!newf) ^ (!old))
825 dev_forward_change((struct inet6_dev *)table->extra1);
829 rt6_purge_dflt_routers(net);
833 static void addrconf_linkdown_change(struct net *net, __s32 newf)
835 struct net_device *dev;
836 struct inet6_dev *idev;
838 for_each_netdev(net, dev) {
839 idev = __in6_dev_get(dev);
841 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
843 idev->cnf.ignore_routes_with_linkdown = newf;
845 inet6_netconf_notify_devconf(dev_net(dev),
846 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
853 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
859 return restart_syscall();
861 net = (struct net *)table->extra2;
865 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
866 if ((!newf) ^ (!old))
867 inet6_netconf_notify_devconf(net,
868 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
869 NETCONFA_IFINDEX_DEFAULT,
870 net->ipv6.devconf_dflt);
875 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
876 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
877 addrconf_linkdown_change(net, newf);
878 if ((!newf) ^ (!old))
879 inet6_netconf_notify_devconf(net,
880 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
881 NETCONFA_IFINDEX_ALL,
882 net->ipv6.devconf_all);
891 /* Nobody refers to this ifaddr, destroy it */
892 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
894 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
896 #ifdef NET_REFCNT_DEBUG
897 pr_debug("%s\n", __func__);
900 in6_dev_put(ifp->idev);
902 if (cancel_delayed_work(&ifp->dad_work))
903 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
906 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
907 pr_warn("Freeing alive inet6 address %p\n", ifp);
916 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
919 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
922 * Each device address list is sorted in order of scope -
923 * global before linklocal.
925 list_for_each(p, &idev->addr_list) {
926 struct inet6_ifaddr *ifa
927 = list_entry(p, struct inet6_ifaddr, if_list);
928 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
932 list_add_tail(&ifp->if_list, p);
935 static u32 inet6_addr_hash(const struct in6_addr *addr)
937 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
940 /* On success it returns ifp with increased reference count */
942 static struct inet6_ifaddr *
943 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
944 const struct in6_addr *peer_addr, int pfxlen,
945 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
947 struct inet6_ifaddr *ifa = NULL;
951 int addr_type = ipv6_addr_type(addr);
953 if (addr_type == IPV6_ADDR_ANY ||
954 addr_type & IPV6_ADDR_MULTICAST ||
955 (!(idev->dev->flags & IFF_LOOPBACK) &&
956 addr_type & IPV6_ADDR_LOOPBACK))
957 return ERR_PTR(-EADDRNOTAVAIL);
961 err = -ENODEV; /*XXX*/
965 if (idev->cnf.disable_ipv6) {
970 spin_lock(&addrconf_hash_lock);
972 /* Ignore adding duplicate addresses on an interface */
973 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
974 ADBG("ipv6_add_addr: already assigned\n");
979 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
982 ADBG("ipv6_add_addr: malloc failed\n");
987 rt = addrconf_dst_alloc(idev, addr, false);
993 neigh_parms_data_state_setall(idev->nd_parms);
997 ifa->peer_addr = *peer_addr;
999 spin_lock_init(&ifa->lock);
1000 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1001 INIT_HLIST_NODE(&ifa->addr_lst);
1003 ifa->prefix_len = pfxlen;
1004 ifa->flags = flags | IFA_F_TENTATIVE;
1005 ifa->valid_lft = valid_lft;
1006 ifa->prefered_lft = prefered_lft;
1007 ifa->cstamp = ifa->tstamp = jiffies;
1008 ifa->tokenized = false;
1017 /* Add to big hash table */
1018 hash = inet6_addr_hash(addr);
1020 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1021 spin_unlock(&addrconf_hash_lock);
1023 write_lock(&idev->lock);
1024 /* Add to inet6_dev unicast addr list. */
1025 ipv6_link_dev_addr(idev, ifa);
1027 if (ifa->flags&IFA_F_TEMPORARY) {
1028 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1033 write_unlock(&idev->lock);
1035 rcu_read_unlock_bh();
1037 if (likely(err == 0))
1038 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1046 spin_unlock(&addrconf_hash_lock);
1050 enum cleanup_prefix_rt_t {
1051 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1052 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1053 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1057 * Check, whether the prefix for ifp would still need a prefix route
1058 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1061 * 1) we don't purge prefix if address was not permanent.
1062 * prefix is managed by its own lifetime.
1063 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1064 * 3) if there are no addresses, delete prefix.
1065 * 4) if there are still other permanent address(es),
1066 * corresponding prefix is still permanent.
1067 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1068 * don't purge the prefix, assume user space is managing it.
1069 * 6) otherwise, update prefix lifetime to the
1070 * longest valid lifetime among the corresponding
1071 * addresses on the device.
1072 * Note: subsequent RA will update lifetime.
1074 static enum cleanup_prefix_rt_t
1075 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1077 struct inet6_ifaddr *ifa;
1078 struct inet6_dev *idev = ifp->idev;
1079 unsigned long lifetime;
1080 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1084 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1087 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1090 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1091 return CLEANUP_PREFIX_RT_NOP;
1093 action = CLEANUP_PREFIX_RT_EXPIRE;
1095 spin_lock(&ifa->lock);
1097 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1099 * Note: Because this address is
1100 * not permanent, lifetime <
1101 * LONG_MAX / HZ here.
1103 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1104 *expires = ifa->tstamp + lifetime * HZ;
1105 spin_unlock(&ifa->lock);
1112 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1114 struct rt6_info *rt;
1116 rt = addrconf_get_prefix_route(&ifp->addr,
1119 0, RTF_GATEWAY | RTF_DEFAULT);
1124 if (!(rt->rt6i_flags & RTF_EXPIRES))
1125 rt6_set_expires(rt, expires);
1132 /* This function wants to get referenced ifp and releases it before return */
1134 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1137 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1138 unsigned long expires;
1142 spin_lock_bh(&ifp->lock);
1144 ifp->state = INET6_IFADDR_STATE_DEAD;
1145 spin_unlock_bh(&ifp->lock);
1147 if (state == INET6_IFADDR_STATE_DEAD)
1150 spin_lock_bh(&addrconf_hash_lock);
1151 hlist_del_init_rcu(&ifp->addr_lst);
1152 spin_unlock_bh(&addrconf_hash_lock);
1154 write_lock_bh(&ifp->idev->lock);
1156 if (ifp->flags&IFA_F_TEMPORARY) {
1157 list_del(&ifp->tmp_list);
1159 in6_ifa_put(ifp->ifpub);
1165 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1166 action = check_cleanup_prefix_route(ifp, &expires);
1168 list_del_init(&ifp->if_list);
1171 write_unlock_bh(&ifp->idev->lock);
1173 addrconf_del_dad_work(ifp);
1175 ipv6_ifa_notify(RTM_DELADDR, ifp);
1177 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1179 if (action != CLEANUP_PREFIX_RT_NOP) {
1180 cleanup_prefix_route(ifp, expires,
1181 action == CLEANUP_PREFIX_RT_DEL);
1184 /* clean up prefsrc entries */
1185 rt6_remove_prefsrc(ifp);
1190 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1192 struct inet6_dev *idev = ifp->idev;
1193 struct in6_addr addr, *tmpaddr;
1194 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1195 unsigned long regen_advance;
1199 unsigned long now = jiffies;
1200 long max_desync_factor;
1201 s32 cnf_temp_preferred_lft;
1203 write_lock_bh(&idev->lock);
1205 spin_lock_bh(&ift->lock);
1206 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1207 spin_unlock_bh(&ift->lock);
1214 if (idev->cnf.use_tempaddr <= 0) {
1215 write_unlock_bh(&idev->lock);
1216 pr_info("%s: use_tempaddr is disabled\n", __func__);
1221 spin_lock_bh(&ifp->lock);
1222 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1223 idev->cnf.use_tempaddr = -1; /*XXX*/
1224 spin_unlock_bh(&ifp->lock);
1225 write_unlock_bh(&idev->lock);
1226 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1233 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1234 ipv6_try_regen_rndid(idev, tmpaddr);
1235 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1236 age = (now - ifp->tstamp) / HZ;
1238 regen_advance = idev->cnf.regen_max_retry *
1239 idev->cnf.dad_transmits *
1240 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1242 /* recalculate max_desync_factor each time and update
1243 * idev->desync_factor if it's larger
1245 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1246 max_desync_factor = min_t(__u32,
1247 idev->cnf.max_desync_factor,
1248 cnf_temp_preferred_lft - regen_advance);
1250 if (unlikely(idev->desync_factor > max_desync_factor)) {
1251 if (max_desync_factor > 0) {
1252 get_random_bytes(&idev->desync_factor,
1253 sizeof(idev->desync_factor));
1254 idev->desync_factor %= max_desync_factor;
1256 idev->desync_factor = 0;
1260 tmp_valid_lft = min_t(__u32,
1262 idev->cnf.temp_valid_lft + age);
1263 tmp_prefered_lft = cnf_temp_preferred_lft + age -
1264 idev->desync_factor;
1265 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1266 tmp_plen = ifp->prefix_len;
1267 tmp_tstamp = ifp->tstamp;
1268 spin_unlock_bh(&ifp->lock);
1270 write_unlock_bh(&idev->lock);
1272 /* A temporary address is created only if this calculated Preferred
1273 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1274 * an implementation must not create a temporary address with a zero
1275 * Preferred Lifetime.
1276 * Use age calculation as in addrconf_verify to avoid unnecessary
1277 * temporary addresses being generated.
1279 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1280 if (tmp_prefered_lft <= regen_advance + age) {
1287 addr_flags = IFA_F_TEMPORARY;
1288 /* set in addrconf_prefix_rcv() */
1289 if (ifp->flags & IFA_F_OPTIMISTIC)
1290 addr_flags |= IFA_F_OPTIMISTIC;
1292 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1293 ipv6_addr_scope(&addr), addr_flags,
1294 tmp_valid_lft, tmp_prefered_lft);
1298 pr_info("%s: retry temporary address regeneration\n", __func__);
1300 write_lock_bh(&idev->lock);
1304 spin_lock_bh(&ift->lock);
1307 ift->tstamp = tmp_tstamp;
1308 spin_unlock_bh(&ift->lock);
1310 addrconf_dad_start(ift);
1318 * Choose an appropriate source address (RFC3484)
1321 IPV6_SADDR_RULE_INIT = 0,
1322 IPV6_SADDR_RULE_LOCAL,
1323 IPV6_SADDR_RULE_SCOPE,
1324 IPV6_SADDR_RULE_PREFERRED,
1325 #ifdef CONFIG_IPV6_MIP6
1326 IPV6_SADDR_RULE_HOA,
1328 IPV6_SADDR_RULE_OIF,
1329 IPV6_SADDR_RULE_LABEL,
1330 IPV6_SADDR_RULE_PRIVACY,
1331 IPV6_SADDR_RULE_ORCHID,
1332 IPV6_SADDR_RULE_PREFIX,
1333 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1334 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1339 struct ipv6_saddr_score {
1342 struct inet6_ifaddr *ifa;
1343 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1348 struct ipv6_saddr_dst {
1349 const struct in6_addr *addr;
1356 static inline int ipv6_saddr_preferred(int type)
1358 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1363 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1365 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1366 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1372 static int ipv6_get_saddr_eval(struct net *net,
1373 struct ipv6_saddr_score *score,
1374 struct ipv6_saddr_dst *dst,
1379 if (i <= score->rule) {
1381 case IPV6_SADDR_RULE_SCOPE:
1382 ret = score->scopedist;
1384 case IPV6_SADDR_RULE_PREFIX:
1385 ret = score->matchlen;
1388 ret = !!test_bit(i, score->scorebits);
1394 case IPV6_SADDR_RULE_INIT:
1395 /* Rule 0: remember if hiscore is not ready yet */
1398 case IPV6_SADDR_RULE_LOCAL:
1399 /* Rule 1: Prefer same address */
1400 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1402 case IPV6_SADDR_RULE_SCOPE:
1403 /* Rule 2: Prefer appropriate scope
1408 * ---+--+-+---> scope
1410 * | d is scope of the destination.
1412 * | \ <- smaller scope is better if
1413 * B-15 | \ if scope is enough for destination.
1414 * | ret = B - scope (-1 <= scope >= d <= 15).
1416 * |/ <- greater is better
1417 * -C / if scope is not enough for destination.
1418 * /| ret = scope - C (-1 <= d < scope <= 15).
1420 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1421 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1422 * Assume B = 0 and we get C > 29.
1424 ret = __ipv6_addr_src_scope(score->addr_type);
1425 if (ret >= dst->scope)
1428 ret -= 128; /* 30 is enough */
1429 score->scopedist = ret;
1431 case IPV6_SADDR_RULE_PREFERRED:
1433 /* Rule 3: Avoid deprecated and optimistic addresses */
1434 u8 avoid = IFA_F_DEPRECATED;
1436 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1437 avoid |= IFA_F_OPTIMISTIC;
1438 ret = ipv6_saddr_preferred(score->addr_type) ||
1439 !(score->ifa->flags & avoid);
1442 #ifdef CONFIG_IPV6_MIP6
1443 case IPV6_SADDR_RULE_HOA:
1445 /* Rule 4: Prefer home address */
1446 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1447 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1451 case IPV6_SADDR_RULE_OIF:
1452 /* Rule 5: Prefer outgoing interface */
1453 ret = (!dst->ifindex ||
1454 dst->ifindex == score->ifa->idev->dev->ifindex);
1456 case IPV6_SADDR_RULE_LABEL:
1457 /* Rule 6: Prefer matching label */
1458 ret = ipv6_addr_label(net,
1459 &score->ifa->addr, score->addr_type,
1460 score->ifa->idev->dev->ifindex) == dst->label;
1462 case IPV6_SADDR_RULE_PRIVACY:
1464 /* Rule 7: Prefer public address
1465 * Note: prefer temporary address if use_tempaddr >= 2
1467 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1468 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1469 score->ifa->idev->cnf.use_tempaddr >= 2;
1470 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1473 case IPV6_SADDR_RULE_ORCHID:
1474 /* Rule 8-: Prefer ORCHID vs ORCHID or
1475 * non-ORCHID vs non-ORCHID
1477 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1478 ipv6_addr_orchid(dst->addr));
1480 case IPV6_SADDR_RULE_PREFIX:
1481 /* Rule 8: Use longest matching prefix */
1482 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1483 if (ret > score->ifa->prefix_len)
1484 ret = score->ifa->prefix_len;
1485 score->matchlen = ret;
1487 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1488 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1489 /* Optimistic addresses still have lower precedence than other
1490 * preferred addresses.
1492 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1500 __set_bit(i, score->scorebits);
1506 static int __ipv6_dev_get_saddr(struct net *net,
1507 struct ipv6_saddr_dst *dst,
1508 struct inet6_dev *idev,
1509 struct ipv6_saddr_score *scores,
1512 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1514 read_lock_bh(&idev->lock);
1515 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1519 * - Tentative Address (RFC2462 section 5.4)
1520 * - A tentative address is not considered
1521 * "assigned to an interface" in the traditional
1522 * sense, unless it is also flagged as optimistic.
1523 * - Candidate Source Address (section 4)
1524 * - In any case, anycast addresses, multicast
1525 * addresses, and the unspecified address MUST
1526 * NOT be included in a candidate set.
1528 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1529 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1532 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1534 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1535 score->addr_type & IPV6_ADDR_MULTICAST)) {
1536 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1542 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1544 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1545 int minihiscore, miniscore;
1547 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1548 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1550 if (minihiscore > miniscore) {
1551 if (i == IPV6_SADDR_RULE_SCOPE &&
1552 score->scopedist > 0) {
1555 * each remaining entry
1556 * has too small (not enough)
1557 * scope, because ifa entries
1558 * are sorted by their scope
1564 } else if (minihiscore < miniscore) {
1566 in6_ifa_put(hiscore->ifa);
1568 in6_ifa_hold(score->ifa);
1570 swap(hiscore, score);
1571 hiscore_idx = 1 - hiscore_idx;
1573 /* restore our iterator */
1574 score->ifa = hiscore->ifa;
1581 read_unlock_bh(&idev->lock);
1585 static int ipv6_get_saddr_master(struct net *net,
1586 const struct net_device *dst_dev,
1587 const struct net_device *master,
1588 struct ipv6_saddr_dst *dst,
1589 struct ipv6_saddr_score *scores,
1592 struct inet6_dev *idev;
1594 idev = __in6_dev_get(dst_dev);
1596 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1597 scores, hiscore_idx);
1599 idev = __in6_dev_get(master);
1601 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1602 scores, hiscore_idx);
1607 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1608 const struct in6_addr *daddr, unsigned int prefs,
1609 struct in6_addr *saddr)
1611 struct ipv6_saddr_score scores[2], *hiscore;
1612 struct ipv6_saddr_dst dst;
1613 struct inet6_dev *idev;
1614 struct net_device *dev;
1616 bool use_oif_addr = false;
1617 int hiscore_idx = 0;
1619 dst_type = __ipv6_addr_type(daddr);
1621 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1622 dst.scope = __ipv6_addr_src_scope(dst_type);
1623 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1626 scores[hiscore_idx].rule = -1;
1627 scores[hiscore_idx].ifa = NULL;
1631 /* Candidate Source Address (section 4)
1632 * - multicast and link-local destination address,
1633 * the set of candidate source address MUST only
1634 * include addresses assigned to interfaces
1635 * belonging to the same link as the outgoing
1637 * (- For site-local destination addresses, the
1638 * set of candidate source addresses MUST only
1639 * include addresses assigned to interfaces
1640 * belonging to the same site as the outgoing
1642 * - "It is RECOMMENDED that the candidate source addresses
1643 * be the set of unicast addresses assigned to the
1644 * interface that will be used to send to the destination
1645 * (the 'outgoing' interface)." (RFC 6724)
1648 idev = __in6_dev_get(dst_dev);
1649 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1650 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1651 (idev && idev->cnf.use_oif_addrs_only)) {
1652 use_oif_addr = true;
1658 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1660 const struct net_device *master;
1663 /* if dst_dev exists and is enslaved to an L3 device, then
1664 * prefer addresses from dst_dev and then the master over
1665 * any other enslaved devices in the L3 domain.
1667 master = l3mdev_master_dev_rcu(dst_dev);
1669 master_idx = master->ifindex;
1671 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1673 scores, hiscore_idx);
1675 if (scores[hiscore_idx].ifa)
1679 for_each_netdev_rcu(net, dev) {
1680 /* only consider addresses on devices in the
1683 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1685 idev = __in6_dev_get(dev);
1688 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1695 hiscore = &scores[hiscore_idx];
1697 return -EADDRNOTAVAIL;
1699 *saddr = hiscore->ifa->addr;
1700 in6_ifa_put(hiscore->ifa);
1703 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1705 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1708 struct inet6_ifaddr *ifp;
1709 int err = -EADDRNOTAVAIL;
1711 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1712 if (ifp->scope > IFA_LINK)
1714 if (ifp->scope == IFA_LINK &&
1715 !(ifp->flags & banned_flags)) {
1724 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1727 struct inet6_dev *idev;
1728 int err = -EADDRNOTAVAIL;
1731 idev = __in6_dev_get(dev);
1733 read_lock_bh(&idev->lock);
1734 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1735 read_unlock_bh(&idev->lock);
1741 static int ipv6_count_addresses(struct inet6_dev *idev)
1744 struct inet6_ifaddr *ifp;
1746 read_lock_bh(&idev->lock);
1747 list_for_each_entry(ifp, &idev->addr_list, if_list)
1749 read_unlock_bh(&idev->lock);
1753 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1754 const struct net_device *dev, int strict)
1756 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1758 EXPORT_SYMBOL(ipv6_chk_addr);
1760 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1761 const struct net_device *dev, int strict,
1764 struct inet6_ifaddr *ifp;
1765 unsigned int hash = inet6_addr_hash(addr);
1769 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1770 if (!net_eq(dev_net(ifp->idev->dev), net))
1772 /* Decouple optimistic from tentative for evaluation here.
1773 * Ban optimistic addresses explicitly, when required.
1775 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1776 ? (ifp->flags&~IFA_F_TENTATIVE)
1778 if (ipv6_addr_equal(&ifp->addr, addr) &&
1779 !(ifp_flags&banned_flags) &&
1780 (!dev || ifp->idev->dev == dev ||
1781 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1782 rcu_read_unlock_bh();
1787 rcu_read_unlock_bh();
1790 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1792 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1793 struct net_device *dev)
1795 unsigned int hash = inet6_addr_hash(addr);
1796 struct inet6_ifaddr *ifp;
1798 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1799 if (!net_eq(dev_net(ifp->idev->dev), net))
1801 if (ipv6_addr_equal(&ifp->addr, addr)) {
1802 if (!dev || ifp->idev->dev == dev)
1809 /* Compares an address/prefix_len with addresses on device @dev.
1810 * If one is found it returns true.
1812 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1813 const unsigned int prefix_len, struct net_device *dev)
1815 struct inet6_dev *idev;
1816 struct inet6_ifaddr *ifa;
1820 idev = __in6_dev_get(dev);
1822 read_lock_bh(&idev->lock);
1823 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1824 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1828 read_unlock_bh(&idev->lock);
1834 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1836 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1838 struct inet6_dev *idev;
1839 struct inet6_ifaddr *ifa;
1844 idev = __in6_dev_get(dev);
1846 read_lock_bh(&idev->lock);
1847 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1848 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1853 read_unlock_bh(&idev->lock);
1858 EXPORT_SYMBOL(ipv6_chk_prefix);
1860 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1861 struct net_device *dev, int strict)
1863 struct inet6_ifaddr *ifp, *result = NULL;
1864 unsigned int hash = inet6_addr_hash(addr);
1867 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1868 if (!net_eq(dev_net(ifp->idev->dev), net))
1870 if (ipv6_addr_equal(&ifp->addr, addr)) {
1871 if (!dev || ifp->idev->dev == dev ||
1872 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1879 rcu_read_unlock_bh();
1884 /* Gets referenced address, destroys ifaddr */
1886 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1889 ifp->flags |= IFA_F_DADFAILED;
1891 if (ifp->flags&IFA_F_PERMANENT) {
1892 spin_lock_bh(&ifp->lock);
1893 addrconf_del_dad_work(ifp);
1894 ifp->flags |= IFA_F_TENTATIVE;
1895 spin_unlock_bh(&ifp->lock);
1897 ipv6_ifa_notify(0, ifp);
1899 } else if (ifp->flags&IFA_F_TEMPORARY) {
1900 struct inet6_ifaddr *ifpub;
1901 spin_lock_bh(&ifp->lock);
1904 in6_ifa_hold(ifpub);
1905 spin_unlock_bh(&ifp->lock);
1906 ipv6_create_tempaddr(ifpub, ifp);
1909 spin_unlock_bh(&ifp->lock);
1917 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1921 spin_lock_bh(&ifp->lock);
1922 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1923 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1926 spin_unlock_bh(&ifp->lock);
1931 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1933 struct inet6_dev *idev = ifp->idev;
1934 struct net *net = dev_net(ifp->idev->dev);
1936 if (addrconf_dad_end(ifp)) {
1941 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1942 ifp->idev->dev->name, &ifp->addr);
1944 spin_lock_bh(&ifp->lock);
1946 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1947 int scope = ifp->scope;
1948 u32 flags = ifp->flags;
1949 struct in6_addr new_addr;
1950 struct inet6_ifaddr *ifp2;
1951 u32 valid_lft, preferred_lft;
1952 int pfxlen = ifp->prefix_len;
1953 int retries = ifp->stable_privacy_retry + 1;
1955 if (retries > net->ipv6.sysctl.idgen_retries) {
1956 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1957 ifp->idev->dev->name);
1961 new_addr = ifp->addr;
1962 if (ipv6_generate_stable_address(&new_addr, retries,
1966 valid_lft = ifp->valid_lft;
1967 preferred_lft = ifp->prefered_lft;
1969 spin_unlock_bh(&ifp->lock);
1971 if (idev->cnf.max_addresses &&
1972 ipv6_count_addresses(idev) >=
1973 idev->cnf.max_addresses)
1976 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1977 ifp->idev->dev->name);
1979 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1980 scope, flags, valid_lft,
1985 spin_lock_bh(&ifp2->lock);
1986 ifp2->stable_privacy_retry = retries;
1987 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1988 spin_unlock_bh(&ifp2->lock);
1990 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1993 spin_lock_bh(&ifp->lock);
1997 /* transition from _POSTDAD to _ERRDAD */
1998 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1999 spin_unlock_bh(&ifp->lock);
2001 addrconf_mod_dad_work(ifp, 0);
2005 /* Join to solicited addr multicast group.
2006 * caller must hold RTNL */
2007 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2009 struct in6_addr maddr;
2011 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2014 addrconf_addr_solict_mult(addr, &maddr);
2015 ipv6_dev_mc_inc(dev, &maddr);
2018 /* caller must hold RTNL */
2019 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2021 struct in6_addr maddr;
2023 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2026 addrconf_addr_solict_mult(addr, &maddr);
2027 __ipv6_dev_mc_dec(idev, &maddr);
2030 /* caller must hold RTNL */
2031 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2033 struct in6_addr addr;
2035 if (ifp->prefix_len >= 127) /* RFC 6164 */
2037 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2038 if (ipv6_addr_any(&addr))
2040 __ipv6_dev_ac_inc(ifp->idev, &addr);
2043 /* caller must hold RTNL */
2044 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2046 struct in6_addr addr;
2048 if (ifp->prefix_len >= 127) /* RFC 6164 */
2050 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2051 if (ipv6_addr_any(&addr))
2053 __ipv6_dev_ac_dec(ifp->idev, &addr);
2056 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2058 if (dev->addr_len != EUI64_ADDR_LEN)
2060 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2065 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2067 union fwnet_hwaddr *ha;
2069 if (dev->addr_len != FWNET_ALEN)
2072 ha = (union fwnet_hwaddr *)dev->dev_addr;
2074 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2079 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2081 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2082 if (dev->addr_len != ARCNET_ALEN)
2085 eui[7] = *(u8 *)dev->dev_addr;
2089 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2091 if (dev->addr_len != INFINIBAND_ALEN)
2093 memcpy(eui, dev->dev_addr + 12, 8);
2098 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2102 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2103 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2104 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2105 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2106 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2107 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2111 memcpy(eui + 4, &addr, 4);
2115 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2117 if (dev->priv_flags & IFF_ISATAP)
2118 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2122 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2124 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2127 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2129 memcpy(eui, dev->perm_addr, 3);
2130 memcpy(eui + 5, dev->perm_addr + 3, 3);
2137 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2139 switch (dev->type) {
2142 return addrconf_ifid_eui48(eui, dev);
2144 return addrconf_ifid_arcnet(eui, dev);
2145 case ARPHRD_INFINIBAND:
2146 return addrconf_ifid_infiniband(eui, dev);
2148 return addrconf_ifid_sit(eui, dev);
2151 return addrconf_ifid_gre(eui, dev);
2152 case ARPHRD_6LOWPAN:
2153 return addrconf_ifid_eui64(eui, dev);
2154 case ARPHRD_IEEE1394:
2155 return addrconf_ifid_ieee1394(eui, dev);
2156 case ARPHRD_TUNNEL6:
2158 return addrconf_ifid_ip6tnl(eui, dev);
2163 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2166 struct inet6_ifaddr *ifp;
2168 read_lock_bh(&idev->lock);
2169 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2170 if (ifp->scope > IFA_LINK)
2172 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2173 memcpy(eui, ifp->addr.s6_addr+8, 8);
2178 read_unlock_bh(&idev->lock);
2182 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2183 static void ipv6_regen_rndid(struct inet6_dev *idev)
2186 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2187 idev->rndid[0] &= ~0x02;
2190 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2191 * check if generated address is not inappropriate
2193 * - Reserved subnet anycast (RFC 2526)
2194 * 11111101 11....11 1xxxxxxx
2195 * - ISATAP (RFC4214) 6.1
2196 * 00-00-5E-FE-xx-xx-xx-xx
2198 * - XXX: already assigned to an address on the device
2200 if (idev->rndid[0] == 0xfd &&
2201 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2202 (idev->rndid[7]&0x80))
2204 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2205 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2207 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2212 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2214 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2215 ipv6_regen_rndid(idev);
2223 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2224 unsigned long expires, u32 flags)
2226 struct fib6_config cfg = {
2227 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2228 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2229 .fc_ifindex = dev->ifindex,
2230 .fc_expires = expires,
2232 .fc_flags = RTF_UP | flags,
2233 .fc_nlinfo.nl_net = dev_net(dev),
2234 .fc_protocol = RTPROT_KERNEL,
2239 /* Prevent useless cloning on PtP SIT.
2240 This thing is done here expecting that the whole
2241 class of non-broadcast devices need not cloning.
2243 #if IS_ENABLED(CONFIG_IPV6_SIT)
2244 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2245 cfg.fc_flags |= RTF_NONEXTHOP;
2248 ip6_route_add(&cfg);
2252 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2254 const struct net_device *dev,
2255 u32 flags, u32 noflags)
2257 struct fib6_node *fn;
2258 struct rt6_info *rt = NULL;
2259 struct fib6_table *table;
2260 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2262 table = fib6_get_table(dev_net(dev), tb_id);
2266 read_lock_bh(&table->tb6_lock);
2267 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2271 noflags |= RTF_CACHE;
2272 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2273 if (rt->dst.dev->ifindex != dev->ifindex)
2275 if ((rt->rt6i_flags & flags) != flags)
2277 if ((rt->rt6i_flags & noflags) != 0)
2283 read_unlock_bh(&table->tb6_lock);
2288 /* Create "default" multicast route to the interface */
2290 static void addrconf_add_mroute(struct net_device *dev)
2292 struct fib6_config cfg = {
2293 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2294 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2295 .fc_ifindex = dev->ifindex,
2298 .fc_nlinfo.nl_net = dev_net(dev),
2301 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2303 ip6_route_add(&cfg);
2306 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2308 struct inet6_dev *idev;
2312 idev = ipv6_find_idev(dev);
2314 return ERR_PTR(-ENOBUFS);
2316 if (idev->cnf.disable_ipv6)
2317 return ERR_PTR(-EACCES);
2319 /* Add default multicast route */
2320 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2321 addrconf_add_mroute(dev);
2326 static void manage_tempaddrs(struct inet6_dev *idev,
2327 struct inet6_ifaddr *ifp,
2328 __u32 valid_lft, __u32 prefered_lft,
2329 bool create, unsigned long now)
2332 struct inet6_ifaddr *ift;
2334 read_lock_bh(&idev->lock);
2335 /* update all temporary addresses in the list */
2336 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2337 int age, max_valid, max_prefered;
2339 if (ifp != ift->ifpub)
2342 /* RFC 4941 section 3.3:
2343 * If a received option will extend the lifetime of a public
2344 * address, the lifetimes of temporary addresses should
2345 * be extended, subject to the overall constraint that no
2346 * temporary addresses should ever remain "valid" or "preferred"
2347 * for a time longer than (TEMP_VALID_LIFETIME) or
2348 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2350 age = (now - ift->cstamp) / HZ;
2351 max_valid = idev->cnf.temp_valid_lft - age;
2355 max_prefered = idev->cnf.temp_prefered_lft -
2356 idev->desync_factor - age;
2357 if (max_prefered < 0)
2360 if (valid_lft > max_valid)
2361 valid_lft = max_valid;
2363 if (prefered_lft > max_prefered)
2364 prefered_lft = max_prefered;
2366 spin_lock(&ift->lock);
2368 ift->valid_lft = valid_lft;
2369 ift->prefered_lft = prefered_lft;
2371 if (prefered_lft > 0)
2372 ift->flags &= ~IFA_F_DEPRECATED;
2374 spin_unlock(&ift->lock);
2375 if (!(flags&IFA_F_TENTATIVE))
2376 ipv6_ifa_notify(0, ift);
2379 if ((create || list_empty(&idev->tempaddr_list)) &&
2380 idev->cnf.use_tempaddr > 0) {
2381 /* When a new public address is created as described
2382 * in [ADDRCONF], also create a new temporary address.
2383 * Also create a temporary address if it's enabled but
2384 * no temporary address currently exists.
2386 read_unlock_bh(&idev->lock);
2387 ipv6_create_tempaddr(ifp, NULL);
2389 read_unlock_bh(&idev->lock);
2393 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2395 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2396 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2399 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2400 const struct prefix_info *pinfo,
2401 struct inet6_dev *in6_dev,
2402 const struct in6_addr *addr, int addr_type,
2403 u32 addr_flags, bool sllao, bool tokenized,
2404 __u32 valid_lft, u32 prefered_lft)
2406 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2407 int create = 0, update_lft = 0;
2409 if (!ifp && valid_lft) {
2410 int max_addresses = in6_dev->cnf.max_addresses;
2412 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2413 if (in6_dev->cnf.optimistic_dad &&
2414 !net->ipv6.devconf_all->forwarding && sllao)
2415 addr_flags |= IFA_F_OPTIMISTIC;
2418 /* Do not allow to create too much of autoconfigured
2419 * addresses; this would be too easy way to crash kernel.
2421 if (!max_addresses ||
2422 ipv6_count_addresses(in6_dev) < max_addresses)
2423 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2425 addr_type&IPV6_ADDR_SCOPE_MASK,
2426 addr_flags, valid_lft,
2429 if (IS_ERR_OR_NULL(ifp))
2434 spin_lock_bh(&ifp->lock);
2435 ifp->flags |= IFA_F_MANAGETEMPADDR;
2436 ifp->cstamp = jiffies;
2437 ifp->tokenized = tokenized;
2438 spin_unlock_bh(&ifp->lock);
2439 addrconf_dad_start(ifp);
2447 /* update lifetime (RFC2462 5.5.3 e) */
2448 spin_lock_bh(&ifp->lock);
2450 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2451 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2454 if (!update_lft && !create && stored_lft) {
2455 const u32 minimum_lft = min_t(u32,
2456 stored_lft, MIN_VALID_LIFETIME);
2457 valid_lft = max(valid_lft, minimum_lft);
2459 /* RFC4862 Section 5.5.3e:
2460 * "Note that the preferred lifetime of the
2461 * corresponding address is always reset to
2462 * the Preferred Lifetime in the received
2463 * Prefix Information option, regardless of
2464 * whether the valid lifetime is also reset or
2467 * So we should always update prefered_lft here.
2473 ifp->valid_lft = valid_lft;
2474 ifp->prefered_lft = prefered_lft;
2477 ifp->flags &= ~IFA_F_DEPRECATED;
2478 spin_unlock_bh(&ifp->lock);
2480 if (!(flags&IFA_F_TENTATIVE))
2481 ipv6_ifa_notify(0, ifp);
2483 spin_unlock_bh(&ifp->lock);
2485 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2494 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2496 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2498 struct prefix_info *pinfo;
2503 struct inet6_dev *in6_dev;
2504 struct net *net = dev_net(dev);
2506 pinfo = (struct prefix_info *) opt;
2508 if (len < sizeof(struct prefix_info)) {
2509 ADBG("addrconf: prefix option too short\n");
2514 * Validation checks ([ADDRCONF], page 19)
2517 addr_type = ipv6_addr_type(&pinfo->prefix);
2519 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2522 valid_lft = ntohl(pinfo->valid);
2523 prefered_lft = ntohl(pinfo->prefered);
2525 if (prefered_lft > valid_lft) {
2526 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2530 in6_dev = in6_dev_get(dev);
2533 net_dbg_ratelimited("addrconf: device %s not configured\n",
2539 * Two things going on here:
2540 * 1) Add routes for on-link prefixes
2541 * 2) Configure prefixes with the auto flag set
2544 if (pinfo->onlink) {
2545 struct rt6_info *rt;
2546 unsigned long rt_expires;
2548 /* Avoid arithmetic overflow. Really, we could
2549 * save rt_expires in seconds, likely valid_lft,
2550 * but it would require division in fib gc, that it
2554 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2556 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2558 if (addrconf_finite_timeout(rt_expires))
2561 rt = addrconf_get_prefix_route(&pinfo->prefix,
2564 RTF_ADDRCONF | RTF_PREFIX_RT,
2565 RTF_GATEWAY | RTF_DEFAULT);
2568 /* Autoconf prefix route */
2569 if (valid_lft == 0) {
2572 } else if (addrconf_finite_timeout(rt_expires)) {
2574 rt6_set_expires(rt, jiffies + rt_expires);
2576 rt6_clean_expires(rt);
2578 } else if (valid_lft) {
2579 clock_t expires = 0;
2580 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2581 if (addrconf_finite_timeout(rt_expires)) {
2583 flags |= RTF_EXPIRES;
2584 expires = jiffies_to_clock_t(rt_expires);
2586 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2587 dev, expires, flags);
2592 /* Try to figure out our local address for this prefix */
2594 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2595 struct in6_addr addr;
2596 bool tokenized = false, dev_addr_generated = false;
2598 if (pinfo->prefix_len == 64) {
2599 memcpy(&addr, &pinfo->prefix, 8);
2601 if (!ipv6_addr_any(&in6_dev->token)) {
2602 read_lock_bh(&in6_dev->lock);
2603 memcpy(addr.s6_addr + 8,
2604 in6_dev->token.s6_addr + 8, 8);
2605 read_unlock_bh(&in6_dev->lock);
2607 } else if (is_addr_mode_generate_stable(in6_dev) &&
2608 !ipv6_generate_stable_address(&addr, 0,
2610 addr_flags |= IFA_F_STABLE_PRIVACY;
2612 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2613 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2616 dev_addr_generated = true;
2620 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2625 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2628 tokenized, valid_lft,
2633 /* Ignore error case here because previous prefix add addr was
2634 * successful which will be notified.
2636 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2637 addr_type, addr_flags, sllao,
2638 tokenized, valid_lft,
2640 dev_addr_generated);
2642 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2644 in6_dev_put(in6_dev);
2648 * Set destination address.
2649 * Special case for SIT interfaces where we create a new "virtual"
2652 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2654 struct in6_ifreq ireq;
2655 struct net_device *dev;
2661 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2664 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2670 #if IS_ENABLED(CONFIG_IPV6_SIT)
2671 if (dev->type == ARPHRD_SIT) {
2672 const struct net_device_ops *ops = dev->netdev_ops;
2674 struct ip_tunnel_parm p;
2676 err = -EADDRNOTAVAIL;
2677 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2680 memset(&p, 0, sizeof(p));
2681 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2685 p.iph.protocol = IPPROTO_IPV6;
2687 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2689 if (ops->ndo_do_ioctl) {
2690 mm_segment_t oldfs = get_fs();
2693 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2700 dev = __dev_get_by_name(net, p.name);
2703 err = dev_open(dev);
2713 static int ipv6_mc_config(struct sock *sk, bool join,
2714 const struct in6_addr *addr, int ifindex)
2722 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2724 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2731 * Manual configuration of address on an interface
2733 static int inet6_addr_add(struct net *net, int ifindex,
2734 const struct in6_addr *pfx,
2735 const struct in6_addr *peer_pfx,
2736 unsigned int plen, __u32 ifa_flags,
2737 __u32 prefered_lft, __u32 valid_lft)
2739 struct inet6_ifaddr *ifp;
2740 struct inet6_dev *idev;
2741 struct net_device *dev;
2742 unsigned long timeout;
2752 /* check the lifetime */
2753 if (!valid_lft || prefered_lft > valid_lft)
2756 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2759 dev = __dev_get_by_index(net, ifindex);
2763 idev = addrconf_add_dev(dev);
2765 return PTR_ERR(idev);
2767 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2768 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2769 true, pfx, ifindex);
2775 scope = ipv6_addr_scope(pfx);
2777 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2778 if (addrconf_finite_timeout(timeout)) {
2779 expires = jiffies_to_clock_t(timeout * HZ);
2780 valid_lft = timeout;
2781 flags = RTF_EXPIRES;
2785 ifa_flags |= IFA_F_PERMANENT;
2788 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2789 if (addrconf_finite_timeout(timeout)) {
2791 ifa_flags |= IFA_F_DEPRECATED;
2792 prefered_lft = timeout;
2795 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2796 valid_lft, prefered_lft);
2799 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2800 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2805 * Note that section 3.1 of RFC 4429 indicates
2806 * that the Optimistic flag should not be set for
2807 * manually configured addresses
2809 addrconf_dad_start(ifp);
2810 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2811 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2814 addrconf_verify_rtnl();
2816 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2817 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2818 false, pfx, ifindex);
2821 return PTR_ERR(ifp);
2824 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2825 const struct in6_addr *pfx, unsigned int plen)
2827 struct inet6_ifaddr *ifp;
2828 struct inet6_dev *idev;
2829 struct net_device *dev;
2834 dev = __dev_get_by_index(net, ifindex);
2838 idev = __in6_dev_get(dev);
2842 read_lock_bh(&idev->lock);
2843 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2844 if (ifp->prefix_len == plen &&
2845 ipv6_addr_equal(pfx, &ifp->addr)) {
2847 read_unlock_bh(&idev->lock);
2849 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2850 (ifa_flags & IFA_F_MANAGETEMPADDR))
2851 manage_tempaddrs(idev, ifp, 0, 0, false,
2854 addrconf_verify_rtnl();
2855 if (ipv6_addr_is_multicast(pfx)) {
2856 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2857 false, pfx, dev->ifindex);
2862 read_unlock_bh(&idev->lock);
2863 return -EADDRNOTAVAIL;
2867 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2869 struct in6_ifreq ireq;
2872 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2875 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2879 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2880 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2881 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2886 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2888 struct in6_ifreq ireq;
2891 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2894 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2898 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2899 ireq.ifr6_prefixlen);
2904 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2905 int plen, int scope)
2907 struct inet6_ifaddr *ifp;
2909 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2910 scope, IFA_F_PERMANENT,
2911 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2913 spin_lock_bh(&ifp->lock);
2914 ifp->flags &= ~IFA_F_TENTATIVE;
2915 spin_unlock_bh(&ifp->lock);
2916 rt_genid_bump_ipv6(dev_net(idev->dev));
2917 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2922 #if IS_ENABLED(CONFIG_IPV6_SIT)
2923 static void sit_add_v4_addrs(struct inet6_dev *idev)
2925 struct in6_addr addr;
2926 struct net_device *dev;
2927 struct net *net = dev_net(idev->dev);
2933 memset(&addr, 0, sizeof(struct in6_addr));
2934 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2936 if (idev->dev->flags&IFF_POINTOPOINT) {
2937 addr.s6_addr32[0] = htonl(0xfe800000);
2941 scope = IPV6_ADDR_COMPATv4;
2943 pflags |= RTF_NONEXTHOP;
2946 if (addr.s6_addr32[3]) {
2947 add_addr(idev, &addr, plen, scope);
2948 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2952 for_each_netdev(net, dev) {
2953 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2954 if (in_dev && (dev->flags & IFF_UP)) {
2955 struct in_ifaddr *ifa;
2959 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2961 addr.s6_addr32[3] = ifa->ifa_local;
2963 if (ifa->ifa_scope == RT_SCOPE_LINK)
2965 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2966 if (idev->dev->flags&IFF_POINTOPOINT)
2971 add_addr(idev, &addr, plen, flag);
2972 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2980 static void init_loopback(struct net_device *dev)
2982 struct inet6_dev *idev;
2983 struct net_device *sp_dev;
2984 struct inet6_ifaddr *sp_ifa;
2985 struct rt6_info *sp_rt;
2991 idev = ipv6_find_idev(dev);
2993 pr_debug("%s: add_dev failed\n", __func__);
2997 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2999 /* Add routes to other interface's IPv6 addresses */
3000 for_each_netdev(dev_net(dev), sp_dev) {
3001 if (!strcmp(sp_dev->name, dev->name))
3004 idev = __in6_dev_get(sp_dev);
3008 read_lock_bh(&idev->lock);
3009 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3011 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3015 /* This dst has been added to garbage list when
3016 * lo device down, release this obsolete dst and
3017 * reallocate a new router for ifa.
3019 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
3020 ip6_rt_put(sp_ifa->rt);
3027 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
3029 /* Failure cases are ignored */
3030 if (!IS_ERR(sp_rt)) {
3035 read_unlock_bh(&idev->lock);
3039 void addrconf_add_linklocal(struct inet6_dev *idev,
3040 const struct in6_addr *addr, u32 flags)
3042 struct inet6_ifaddr *ifp;
3043 u32 addr_flags = flags | IFA_F_PERMANENT;
3045 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3046 if (idev->cnf.optimistic_dad &&
3047 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3048 addr_flags |= IFA_F_OPTIMISTIC;
3051 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3052 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
3054 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3055 addrconf_dad_start(ifp);
3059 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3061 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3063 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3066 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3067 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3070 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3071 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3077 static int ipv6_generate_stable_address(struct in6_addr *address,
3079 const struct inet6_dev *idev)
3081 static DEFINE_SPINLOCK(lock);
3082 static __u32 digest[SHA_DIGEST_WORDS];
3083 static __u32 workspace[SHA_WORKSPACE_WORDS];
3086 char __data[SHA_MESSAGE_BYTES];
3088 struct in6_addr secret;
3090 unsigned char hwaddr[MAX_ADDR_LEN];
3095 struct in6_addr secret;
3096 struct in6_addr temp;
3097 struct net *net = dev_net(idev->dev);
3099 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3101 if (idev->cnf.stable_secret.initialized)
3102 secret = idev->cnf.stable_secret.secret;
3103 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3104 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3109 spin_lock_bh(&lock);
3112 memset(&data, 0, sizeof(data));
3113 memset(workspace, 0, sizeof(workspace));
3114 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3115 data.prefix[0] = address->s6_addr32[0];
3116 data.prefix[1] = address->s6_addr32[1];
3117 data.secret = secret;
3118 data.dad_count = dad_count;
3120 sha_transform(digest, data.__data, workspace);
3123 temp.s6_addr32[2] = (__force __be32)digest[0];
3124 temp.s6_addr32[3] = (__force __be32)digest[1];
3126 spin_unlock_bh(&lock);
3128 if (ipv6_reserved_interfaceid(temp)) {
3130 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3139 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3141 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3145 s = &idev->cnf.stable_secret;
3146 get_random_bytes(&s->secret, sizeof(s->secret));
3147 s->initialized = true;
3150 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3152 struct in6_addr addr;
3154 /* no link local addresses on L3 master devices */
3155 if (netif_is_l3_master(idev->dev))
3158 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3160 switch (idev->cnf.addr_gen_mode) {
3161 case IN6_ADDR_GEN_MODE_RANDOM:
3162 ipv6_gen_mode_random_init(idev);
3164 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3165 if (!ipv6_generate_stable_address(&addr, 0, idev))
3166 addrconf_add_linklocal(idev, &addr,
3167 IFA_F_STABLE_PRIVACY);
3168 else if (prefix_route)
3169 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3171 case IN6_ADDR_GEN_MODE_EUI64:
3172 /* addrconf_add_linklocal also adds a prefix_route and we
3173 * only need to care about prefix routes if ipv6_generate_eui64
3174 * couldn't generate one.
3176 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3177 addrconf_add_linklocal(idev, &addr, 0);
3178 else if (prefix_route)
3179 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3181 case IN6_ADDR_GEN_MODE_NONE:
3183 /* will not add any link local address */
3188 static void addrconf_dev_config(struct net_device *dev)
3190 struct inet6_dev *idev;
3194 if ((dev->type != ARPHRD_ETHER) &&
3195 (dev->type != ARPHRD_FDDI) &&
3196 (dev->type != ARPHRD_ARCNET) &&
3197 (dev->type != ARPHRD_INFINIBAND) &&
3198 (dev->type != ARPHRD_IEEE1394) &&
3199 (dev->type != ARPHRD_TUNNEL6) &&
3200 (dev->type != ARPHRD_6LOWPAN) &&
3201 (dev->type != ARPHRD_IP6GRE) &&
3202 (dev->type != ARPHRD_IPGRE) &&
3203 (dev->type != ARPHRD_TUNNEL) &&
3204 (dev->type != ARPHRD_NONE)) {
3205 /* Alas, we support only Ethernet autoconfiguration. */
3209 idev = addrconf_add_dev(dev);
3213 /* this device type has no EUI support */
3214 if (dev->type == ARPHRD_NONE &&
3215 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3216 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3218 addrconf_addr_gen(idev, false);
3221 #if IS_ENABLED(CONFIG_IPV6_SIT)
3222 static void addrconf_sit_config(struct net_device *dev)
3224 struct inet6_dev *idev;
3229 * Configure the tunnel with one of our IPv4
3230 * addresses... we should configure all of
3231 * our v4 addrs in the tunnel
3234 idev = ipv6_find_idev(dev);
3236 pr_debug("%s: add_dev failed\n", __func__);
3240 if (dev->priv_flags & IFF_ISATAP) {
3241 addrconf_addr_gen(idev, false);
3245 sit_add_v4_addrs(idev);
3247 if (dev->flags&IFF_POINTOPOINT)
3248 addrconf_add_mroute(dev);
3252 #if IS_ENABLED(CONFIG_NET_IPGRE)
3253 static void addrconf_gre_config(struct net_device *dev)
3255 struct inet6_dev *idev;
3259 idev = ipv6_find_idev(dev);
3261 pr_debug("%s: add_dev failed\n", __func__);
3265 addrconf_addr_gen(idev, true);
3266 if (dev->flags & IFF_POINTOPOINT)
3267 addrconf_add_mroute(dev);
3271 static int fixup_permanent_addr(struct inet6_dev *idev,
3272 struct inet6_ifaddr *ifp)
3275 struct rt6_info *rt;
3277 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3278 if (unlikely(IS_ERR(rt)))
3284 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3285 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3289 addrconf_dad_start(ifp);
3294 static void addrconf_permanent_addr(struct net_device *dev)
3296 struct inet6_ifaddr *ifp, *tmp;
3297 struct inet6_dev *idev;
3299 idev = __in6_dev_get(dev);
3303 write_lock_bh(&idev->lock);
3305 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3306 if ((ifp->flags & IFA_F_PERMANENT) &&
3307 fixup_permanent_addr(idev, ifp) < 0) {
3308 write_unlock_bh(&idev->lock);
3310 write_lock_bh(&idev->lock);
3312 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3313 idev->dev->name, &ifp->addr);
3317 write_unlock_bh(&idev->lock);
3320 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3323 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3324 struct netdev_notifier_changeupper_info *info;
3325 struct inet6_dev *idev = __in6_dev_get(dev);
3326 int run_pending = 0;
3330 case NETDEV_REGISTER:
3331 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3332 idev = ipv6_add_dev(dev);
3334 return notifier_from_errno(PTR_ERR(idev));
3338 case NETDEV_CHANGEMTU:
3339 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3340 if (dev->mtu < IPV6_MIN_MTU) {
3341 addrconf_ifdown(dev, 1);
3346 rt6_mtu_change(dev, dev->mtu);
3347 idev->cnf.mtu6 = dev->mtu;
3351 /* allocate new idev */
3352 idev = ipv6_add_dev(dev);
3356 /* device is still not ready */
3357 if (!(idev->if_flags & IF_READY))
3366 if (dev->flags & IFF_SLAVE)
3369 if (idev && idev->cnf.disable_ipv6)
3372 if (event == NETDEV_UP) {
3373 /* restore routes for permanent addresses */
3374 addrconf_permanent_addr(dev);
3376 if (!addrconf_qdisc_ok(dev)) {
3377 /* device is not ready yet. */
3378 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3383 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3384 idev = ipv6_add_dev(dev);
3386 if (!IS_ERR_OR_NULL(idev)) {
3387 idev->if_flags |= IF_READY;
3390 } else if (event == NETDEV_CHANGE) {
3391 if (!addrconf_qdisc_ok(dev)) {
3392 /* device is still not ready. */
3397 if (idev->if_flags & IF_READY) {
3398 /* device is already configured -
3399 * but resend MLD reports, we might
3400 * have roamed and need to update
3401 * multicast snooping switches
3406 idev->if_flags |= IF_READY;
3409 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3415 switch (dev->type) {
3416 #if IS_ENABLED(CONFIG_IPV6_SIT)
3418 addrconf_sit_config(dev);
3421 #if IS_ENABLED(CONFIG_NET_IPGRE)
3423 addrconf_gre_config(dev);
3426 case ARPHRD_LOOPBACK:
3431 addrconf_dev_config(dev);
3435 if (!IS_ERR_OR_NULL(idev)) {
3437 addrconf_dad_run(idev);
3440 * If the MTU changed during the interface down,
3441 * when the interface up, the changed MTU must be
3442 * reflected in the idev as well as routers.
3444 if (idev->cnf.mtu6 != dev->mtu &&
3445 dev->mtu >= IPV6_MIN_MTU) {
3446 rt6_mtu_change(dev, dev->mtu);
3447 idev->cnf.mtu6 = dev->mtu;
3449 idev->tstamp = jiffies;
3450 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3453 * If the changed mtu during down is lower than
3454 * IPV6_MIN_MTU stop IPv6 on this interface.
3456 if (dev->mtu < IPV6_MIN_MTU)
3457 addrconf_ifdown(dev, 1);
3462 case NETDEV_UNREGISTER:
3464 * Remove all addresses from this interface.
3466 addrconf_ifdown(dev, event != NETDEV_DOWN);
3469 case NETDEV_CHANGENAME:
3471 snmp6_unregister_dev(idev);
3472 addrconf_sysctl_unregister(idev);
3473 err = addrconf_sysctl_register(idev);
3475 return notifier_from_errno(err);
3476 err = snmp6_register_dev(idev);
3478 addrconf_sysctl_unregister(idev);
3479 return notifier_from_errno(err);
3484 case NETDEV_PRE_TYPE_CHANGE:
3485 case NETDEV_POST_TYPE_CHANGE:
3487 addrconf_type_change(dev, event);
3490 case NETDEV_CHANGEUPPER:
3493 /* flush all routes if dev is linked to or unlinked from
3494 * an L3 master device (e.g., VRF)
3496 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3497 addrconf_ifdown(dev, 0);
3504 * addrconf module should be notified of a device going up
3506 static struct notifier_block ipv6_dev_notf = {
3507 .notifier_call = addrconf_notify,
3510 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3512 struct inet6_dev *idev;
3515 idev = __in6_dev_get(dev);
3517 if (event == NETDEV_POST_TYPE_CHANGE)
3518 ipv6_mc_remap(idev);
3519 else if (event == NETDEV_PRE_TYPE_CHANGE)
3520 ipv6_mc_unmap(idev);
3523 static bool addr_is_local(const struct in6_addr *addr)
3525 return ipv6_addr_type(addr) &
3526 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3529 static int addrconf_ifdown(struct net_device *dev, int how)
3531 struct net *net = dev_net(dev);
3532 struct inet6_dev *idev;
3533 struct inet6_ifaddr *ifa, *tmp;
3534 struct list_head del_list;
3541 rt6_ifdown(net, dev);
3542 neigh_ifdown(&nd_tbl, dev);
3544 idev = __in6_dev_get(dev);
3549 * Step 1: remove reference to ipv6 device from parent device.
3555 /* protected by rtnl_lock */
3556 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3558 /* Step 1.5: remove snmp6 entry */
3559 snmp6_unregister_dev(idev);
3563 /* aggregate the system setting and interface setting */
3564 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3566 _keep_addr = idev->cnf.keep_addr_on_down;
3568 /* combine the user config with event to determine if permanent
3569 * addresses are to be removed from address hash table
3571 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3573 /* Step 2: clear hash table */
3574 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3575 struct hlist_head *h = &inet6_addr_lst[i];
3577 spin_lock_bh(&addrconf_hash_lock);
3579 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3580 if (ifa->idev == idev) {
3581 addrconf_del_dad_work(ifa);
3582 /* combined flag + permanent flag decide if
3583 * address is retained on a down event
3586 !(ifa->flags & IFA_F_PERMANENT) ||
3587 addr_is_local(&ifa->addr)) {
3588 hlist_del_init_rcu(&ifa->addr_lst);
3593 spin_unlock_bh(&addrconf_hash_lock);
3596 write_lock_bh(&idev->lock);
3598 addrconf_del_rs_timer(idev);
3600 /* Step 2: clear flags for stateless addrconf */
3602 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3604 /* Step 3: clear tempaddr list */
3605 while (!list_empty(&idev->tempaddr_list)) {
3606 ifa = list_first_entry(&idev->tempaddr_list,
3607 struct inet6_ifaddr, tmp_list);
3608 list_del(&ifa->tmp_list);
3609 write_unlock_bh(&idev->lock);
3610 spin_lock_bh(&ifa->lock);
3613 in6_ifa_put(ifa->ifpub);
3616 spin_unlock_bh(&ifa->lock);
3618 write_lock_bh(&idev->lock);
3621 /* re-combine the user config with event to determine if permanent
3622 * addresses are to be removed from the interface list
3624 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3626 INIT_LIST_HEAD(&del_list);
3627 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3628 struct rt6_info *rt = NULL;
3630 addrconf_del_dad_work(ifa);
3632 write_unlock_bh(&idev->lock);
3633 spin_lock_bh(&ifa->lock);
3635 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3636 !addr_is_local(&ifa->addr)) {
3637 /* set state to skip the notifier below */
3638 state = INET6_IFADDR_STATE_DEAD;
3640 if (!(ifa->flags & IFA_F_NODAD))
3641 ifa->flags |= IFA_F_TENTATIVE;
3647 ifa->state = INET6_IFADDR_STATE_DEAD;
3649 list_move(&ifa->if_list, &del_list);
3652 spin_unlock_bh(&ifa->lock);
3657 if (state != INET6_IFADDR_STATE_DEAD) {
3658 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3659 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3661 if (idev->cnf.forwarding)
3662 addrconf_leave_anycast(ifa);
3663 addrconf_leave_solict(ifa->idev, &ifa->addr);
3666 write_lock_bh(&idev->lock);
3669 write_unlock_bh(&idev->lock);
3671 /* now clean up addresses to be removed */
3672 while (!list_empty(&del_list)) {
3673 ifa = list_first_entry(&del_list,
3674 struct inet6_ifaddr, if_list);
3675 list_del(&ifa->if_list);
3680 /* Step 5: Discard anycast and multicast list */
3682 ipv6_ac_destroy_dev(idev);
3683 ipv6_mc_destroy_dev(idev);
3688 idev->tstamp = jiffies;
3690 /* Last: Shot the device (if unregistered) */
3692 addrconf_sysctl_unregister(idev);
3693 neigh_parms_release(&nd_tbl, idev->nd_parms);
3694 neigh_ifdown(&nd_tbl, dev);
3700 static void addrconf_rs_timer(unsigned long data)
3702 struct inet6_dev *idev = (struct inet6_dev *)data;
3703 struct net_device *dev = idev->dev;
3704 struct in6_addr lladdr;
3706 write_lock(&idev->lock);
3707 if (idev->dead || !(idev->if_flags & IF_READY))
3710 if (!ipv6_accept_ra(idev))
3713 /* Announcement received after solicitation was sent */
3714 if (idev->if_flags & IF_RA_RCVD)
3717 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3718 write_unlock(&idev->lock);
3719 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3720 ndisc_send_rs(dev, &lladdr,
3721 &in6addr_linklocal_allrouters);
3725 write_lock(&idev->lock);
3726 idev->rs_interval = rfc3315_s14_backoff_update(
3727 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3728 /* The wait after the last probe can be shorter */
3729 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3730 idev->cnf.rtr_solicits) ?
3731 idev->cnf.rtr_solicit_delay :
3735 * Note: we do not support deprecated "all on-link"
3736 * assumption any longer.
3738 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3742 write_unlock(&idev->lock);
3748 * Duplicate Address Detection
3750 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3752 unsigned long rand_num;
3753 struct inet6_dev *idev = ifp->idev;
3756 if (ifp->flags & IFA_F_OPTIMISTIC)
3759 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3762 if (idev->cnf.enhanced_dad ||
3763 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3765 get_random_bytes(&nonce, 6);
3768 ifp->dad_nonce = nonce;
3769 ifp->dad_probes = idev->cnf.dad_transmits;
3770 addrconf_mod_dad_work(ifp, rand_num);
3773 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3775 struct inet6_dev *idev = ifp->idev;
3776 struct net_device *dev = idev->dev;
3777 bool bump_id, notify = false;
3779 addrconf_join_solict(dev, &ifp->addr);
3781 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3783 read_lock_bh(&idev->lock);
3784 spin_lock(&ifp->lock);
3785 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3788 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3789 idev->cnf.accept_dad < 1 ||
3790 !(ifp->flags&IFA_F_TENTATIVE) ||
3791 ifp->flags & IFA_F_NODAD) {
3792 bump_id = ifp->flags & IFA_F_TENTATIVE;
3793 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3794 spin_unlock(&ifp->lock);
3795 read_unlock_bh(&idev->lock);
3797 addrconf_dad_completed(ifp, bump_id);
3801 if (!(idev->if_flags & IF_READY)) {
3802 spin_unlock(&ifp->lock);
3803 read_unlock_bh(&idev->lock);
3805 * If the device is not ready:
3806 * - keep it tentative if it is a permanent address.
3807 * - otherwise, kill it.
3810 addrconf_dad_stop(ifp, 0);
3815 * Optimistic nodes can start receiving
3818 if (ifp->flags & IFA_F_OPTIMISTIC) {
3819 ip6_ins_rt(ifp->rt);
3820 if (ipv6_use_optimistic_addr(idev)) {
3821 /* Because optimistic nodes can use this address,
3822 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3828 addrconf_dad_kick(ifp);
3830 spin_unlock(&ifp->lock);
3831 read_unlock_bh(&idev->lock);
3833 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3836 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3838 bool begin_dad = false;
3840 spin_lock_bh(&ifp->lock);
3841 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3842 ifp->state = INET6_IFADDR_STATE_PREDAD;
3845 spin_unlock_bh(&ifp->lock);
3848 addrconf_mod_dad_work(ifp, 0);
3851 static void addrconf_dad_work(struct work_struct *w)
3853 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3854 struct inet6_ifaddr,
3856 struct inet6_dev *idev = ifp->idev;
3857 bool bump_id, disable_ipv6 = false;
3858 struct in6_addr mcaddr;
3864 } action = DAD_PROCESS;
3868 spin_lock_bh(&ifp->lock);
3869 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3871 ifp->state = INET6_IFADDR_STATE_DAD;
3872 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3874 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3876 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3877 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3878 struct in6_addr addr;
3880 addr.s6_addr32[0] = htonl(0xfe800000);
3881 addr.s6_addr32[1] = 0;
3883 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3884 ipv6_addr_equal(&ifp->addr, &addr)) {
3885 /* DAD failed for link-local based on MAC */
3886 idev->cnf.disable_ipv6 = 1;
3888 pr_info("%s: IPv6 being disabled!\n",
3889 ifp->idev->dev->name);
3890 disable_ipv6 = true;
3894 spin_unlock_bh(&ifp->lock);
3896 if (action == DAD_BEGIN) {
3897 addrconf_dad_begin(ifp);
3899 } else if (action == DAD_ABORT) {
3901 addrconf_dad_stop(ifp, 1);
3903 addrconf_ifdown(idev->dev, 0);
3907 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3910 write_lock_bh(&idev->lock);
3911 if (idev->dead || !(idev->if_flags & IF_READY)) {
3912 write_unlock_bh(&idev->lock);
3916 spin_lock(&ifp->lock);
3917 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3918 spin_unlock(&ifp->lock);
3919 write_unlock_bh(&idev->lock);
3923 if (ifp->dad_probes == 0) {
3925 * DAD was successful
3928 bump_id = ifp->flags & IFA_F_TENTATIVE;
3929 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3930 spin_unlock(&ifp->lock);
3931 write_unlock_bh(&idev->lock);
3933 addrconf_dad_completed(ifp, bump_id);
3939 addrconf_mod_dad_work(ifp,
3940 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3941 spin_unlock(&ifp->lock);
3942 write_unlock_bh(&idev->lock);
3944 /* send a neighbour solicitation for our addr */
3945 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3946 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3953 /* ifp->idev must be at least read locked */
3954 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3956 struct inet6_ifaddr *ifpiter;
3957 struct inet6_dev *idev = ifp->idev;
3959 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3960 if (ifpiter->scope > IFA_LINK)
3962 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3963 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3964 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3971 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
3973 struct net_device *dev = ifp->idev->dev;
3974 struct in6_addr lladdr;
3975 bool send_rs, send_mld;
3977 addrconf_del_dad_work(ifp);
3980 * Configure the address for reception. Now it is valid.
3983 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3985 /* If added prefix is link local and we are prepared to process
3986 router advertisements, start sending router solicitations.
3989 read_lock_bh(&ifp->idev->lock);
3990 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3991 send_rs = send_mld &&
3992 ipv6_accept_ra(ifp->idev) &&
3993 ifp->idev->cnf.rtr_solicits != 0 &&
3994 (dev->flags&IFF_LOOPBACK) == 0;
3995 read_unlock_bh(&ifp->idev->lock);
3997 /* While dad is in progress mld report's source address is in6_addrany.
3998 * Resend with proper ll now.
4001 ipv6_mc_dad_complete(ifp->idev);
4005 * If a host as already performed a random delay
4006 * [...] as part of DAD [...] there is no need
4007 * to delay again before sending the first RS
4009 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4011 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4013 write_lock_bh(&ifp->idev->lock);
4014 spin_lock(&ifp->lock);
4015 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4016 ifp->idev->cnf.rtr_solicit_interval);
4017 ifp->idev->rs_probes = 1;
4018 ifp->idev->if_flags |= IF_RS_SENT;
4019 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4020 spin_unlock(&ifp->lock);
4021 write_unlock_bh(&ifp->idev->lock);
4025 rt_genid_bump_ipv6(dev_net(dev));
4027 /* Make sure that a new temporary address will be created
4028 * before this temporary address becomes deprecated.
4030 if (ifp->flags & IFA_F_TEMPORARY)
4031 addrconf_verify_rtnl();
4034 static void addrconf_dad_run(struct inet6_dev *idev)
4036 struct inet6_ifaddr *ifp;
4038 read_lock_bh(&idev->lock);
4039 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4040 spin_lock(&ifp->lock);
4041 if (ifp->flags & IFA_F_TENTATIVE &&
4042 ifp->state == INET6_IFADDR_STATE_DAD)
4043 addrconf_dad_kick(ifp);
4044 spin_unlock(&ifp->lock);
4046 read_unlock_bh(&idev->lock);
4049 #ifdef CONFIG_PROC_FS
4050 struct if6_iter_state {
4051 struct seq_net_private p;
4056 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4058 struct inet6_ifaddr *ifa = NULL;
4059 struct if6_iter_state *state = seq->private;
4060 struct net *net = seq_file_net(seq);
4063 /* initial bucket if pos is 0 */
4069 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4070 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
4072 if (!net_eq(dev_net(ifa->idev->dev), net))
4074 /* sync with offset */
4075 if (p < state->offset) {
4083 /* prepare for next bucket */
4090 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4091 struct inet6_ifaddr *ifa)
4093 struct if6_iter_state *state = seq->private;
4094 struct net *net = seq_file_net(seq);
4096 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
4097 if (!net_eq(dev_net(ifa->idev->dev), net))
4103 while (++state->bucket < IN6_ADDR_HSIZE) {
4105 hlist_for_each_entry_rcu_bh(ifa,
4106 &inet6_addr_lst[state->bucket], addr_lst) {
4107 if (!net_eq(dev_net(ifa->idev->dev), net))
4117 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4121 return if6_get_first(seq, *pos);
4124 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4126 struct inet6_ifaddr *ifa;
4128 ifa = if6_get_next(seq, v);
4133 static void if6_seq_stop(struct seq_file *seq, void *v)
4136 rcu_read_unlock_bh();
4139 static int if6_seq_show(struct seq_file *seq, void *v)
4141 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4142 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4144 ifp->idev->dev->ifindex,
4148 ifp->idev->dev->name);
4152 static const struct seq_operations if6_seq_ops = {
4153 .start = if6_seq_start,
4154 .next = if6_seq_next,
4155 .show = if6_seq_show,
4156 .stop = if6_seq_stop,
4159 static int if6_seq_open(struct inode *inode, struct file *file)
4161 return seq_open_net(inode, file, &if6_seq_ops,
4162 sizeof(struct if6_iter_state));
4165 static const struct file_operations if6_fops = {
4166 .owner = THIS_MODULE,
4167 .open = if6_seq_open,
4169 .llseek = seq_lseek,
4170 .release = seq_release_net,
4173 static int __net_init if6_proc_net_init(struct net *net)
4175 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
4180 static void __net_exit if6_proc_net_exit(struct net *net)
4182 remove_proc_entry("if_inet6", net->proc_net);
4185 static struct pernet_operations if6_proc_net_ops = {
4186 .init = if6_proc_net_init,
4187 .exit = if6_proc_net_exit,
4190 int __init if6_proc_init(void)
4192 return register_pernet_subsys(&if6_proc_net_ops);
4195 void if6_proc_exit(void)
4197 unregister_pernet_subsys(&if6_proc_net_ops);
4199 #endif /* CONFIG_PROC_FS */
4201 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4202 /* Check if address is a home address configured on any interface. */
4203 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4206 struct inet6_ifaddr *ifp = NULL;
4207 unsigned int hash = inet6_addr_hash(addr);
4210 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4211 if (!net_eq(dev_net(ifp->idev->dev), net))
4213 if (ipv6_addr_equal(&ifp->addr, addr) &&
4214 (ifp->flags & IFA_F_HOMEADDRESS)) {
4219 rcu_read_unlock_bh();
4225 * Periodic address status verification
4228 static void addrconf_verify_rtnl(void)
4230 unsigned long now, next, next_sec, next_sched;
4231 struct inet6_ifaddr *ifp;
4238 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4240 cancel_delayed_work(&addr_chk_work);
4242 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4244 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4247 /* When setting preferred_lft to a value not zero or
4248 * infinity, while valid_lft is infinity
4249 * IFA_F_PERMANENT has a non-infinity life time.
4251 if ((ifp->flags & IFA_F_PERMANENT) &&
4252 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4255 spin_lock(&ifp->lock);
4256 /* We try to batch several events at once. */
4257 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4259 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4260 age >= ifp->valid_lft) {
4261 spin_unlock(&ifp->lock);
4265 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4266 spin_unlock(&ifp->lock);
4268 } else if (age >= ifp->prefered_lft) {
4269 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4272 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4274 ifp->flags |= IFA_F_DEPRECATED;
4277 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4278 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4279 next = ifp->tstamp + ifp->valid_lft * HZ;
4281 spin_unlock(&ifp->lock);
4286 ipv6_ifa_notify(0, ifp);
4290 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4291 !(ifp->flags&IFA_F_TENTATIVE)) {
4292 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4293 ifp->idev->cnf.dad_transmits *
4294 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4296 if (age >= ifp->prefered_lft - regen_advance) {
4297 struct inet6_ifaddr *ifpub = ifp->ifpub;
4298 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4299 next = ifp->tstamp + ifp->prefered_lft * HZ;
4300 if (!ifp->regen_count && ifpub) {
4303 in6_ifa_hold(ifpub);
4304 spin_unlock(&ifp->lock);
4306 spin_lock(&ifpub->lock);
4307 ifpub->regen_count = 0;
4308 spin_unlock(&ifpub->lock);
4309 ipv6_create_tempaddr(ifpub, ifp);
4314 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4315 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4316 spin_unlock(&ifp->lock);
4318 /* ifp->prefered_lft <= ifp->valid_lft */
4319 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4320 next = ifp->tstamp + ifp->prefered_lft * HZ;
4321 spin_unlock(&ifp->lock);
4326 next_sec = round_jiffies_up(next);
4329 /* If rounded timeout is accurate enough, accept it. */
4330 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4331 next_sched = next_sec;
4333 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4334 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4335 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4337 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4338 now, next, next_sec, next_sched);
4339 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4340 rcu_read_unlock_bh();
4343 static void addrconf_verify_work(struct work_struct *w)
4346 addrconf_verify_rtnl();
4350 static void addrconf_verify(void)
4352 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4355 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4356 struct in6_addr **peer_pfx)
4358 struct in6_addr *pfx = NULL;
4363 pfx = nla_data(addr);
4366 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4368 pfx = nla_data(local);
4374 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4375 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4376 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4377 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4378 [IFA_FLAGS] = { .len = sizeof(u32) },
4382 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4384 struct net *net = sock_net(skb->sk);
4385 struct ifaddrmsg *ifm;
4386 struct nlattr *tb[IFA_MAX+1];
4387 struct in6_addr *pfx, *peer_pfx;
4391 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4395 ifm = nlmsg_data(nlh);
4396 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4400 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4402 /* We ignore other flags so far. */
4403 ifa_flags &= IFA_F_MANAGETEMPADDR;
4405 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4406 ifm->ifa_prefixlen);
4409 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4410 u32 prefered_lft, u32 valid_lft)
4414 unsigned long timeout;
4415 bool was_managetempaddr;
4416 bool had_prefixroute;
4420 if (!valid_lft || (prefered_lft > valid_lft))
4423 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4424 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4427 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4428 if (addrconf_finite_timeout(timeout)) {
4429 expires = jiffies_to_clock_t(timeout * HZ);
4430 valid_lft = timeout;
4431 flags = RTF_EXPIRES;
4435 ifa_flags |= IFA_F_PERMANENT;
4438 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4439 if (addrconf_finite_timeout(timeout)) {
4441 ifa_flags |= IFA_F_DEPRECATED;
4442 prefered_lft = timeout;
4445 spin_lock_bh(&ifp->lock);
4446 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4447 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4448 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4449 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4450 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4451 IFA_F_NOPREFIXROUTE);
4452 ifp->flags |= ifa_flags;
4453 ifp->tstamp = jiffies;
4454 ifp->valid_lft = valid_lft;
4455 ifp->prefered_lft = prefered_lft;
4457 spin_unlock_bh(&ifp->lock);
4458 if (!(ifp->flags&IFA_F_TENTATIVE))
4459 ipv6_ifa_notify(0, ifp);
4461 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4462 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4464 } else if (had_prefixroute) {
4465 enum cleanup_prefix_rt_t action;
4466 unsigned long rt_expires;
4468 write_lock_bh(&ifp->idev->lock);
4469 action = check_cleanup_prefix_route(ifp, &rt_expires);
4470 write_unlock_bh(&ifp->idev->lock);
4472 if (action != CLEANUP_PREFIX_RT_NOP) {
4473 cleanup_prefix_route(ifp, rt_expires,
4474 action == CLEANUP_PREFIX_RT_DEL);
4478 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4479 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4480 valid_lft = prefered_lft = 0;
4481 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4482 !was_managetempaddr, jiffies);
4485 addrconf_verify_rtnl();
4491 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4493 struct net *net = sock_net(skb->sk);
4494 struct ifaddrmsg *ifm;
4495 struct nlattr *tb[IFA_MAX+1];
4496 struct in6_addr *pfx, *peer_pfx;
4497 struct inet6_ifaddr *ifa;
4498 struct net_device *dev;
4499 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4503 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4507 ifm = nlmsg_data(nlh);
4508 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4512 if (tb[IFA_CACHEINFO]) {
4513 struct ifa_cacheinfo *ci;
4515 ci = nla_data(tb[IFA_CACHEINFO]);
4516 valid_lft = ci->ifa_valid;
4517 preferred_lft = ci->ifa_prefered;
4519 preferred_lft = INFINITY_LIFE_TIME;
4520 valid_lft = INFINITY_LIFE_TIME;
4523 dev = __dev_get_by_index(net, ifm->ifa_index);
4527 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4529 /* We ignore other flags so far. */
4530 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4531 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4533 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4536 * It would be best to check for !NLM_F_CREATE here but
4537 * userspace already relies on not having to provide this.
4539 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4540 ifm->ifa_prefixlen, ifa_flags,
4541 preferred_lft, valid_lft);
4544 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4545 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4548 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4555 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4556 u8 scope, int ifindex)
4558 struct ifaddrmsg *ifm;
4560 ifm = nlmsg_data(nlh);
4561 ifm->ifa_family = AF_INET6;
4562 ifm->ifa_prefixlen = prefixlen;
4563 ifm->ifa_flags = flags;
4564 ifm->ifa_scope = scope;
4565 ifm->ifa_index = ifindex;
4568 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4569 unsigned long tstamp, u32 preferred, u32 valid)
4571 struct ifa_cacheinfo ci;
4573 ci.cstamp = cstamp_delta(cstamp);
4574 ci.tstamp = cstamp_delta(tstamp);
4575 ci.ifa_prefered = preferred;
4576 ci.ifa_valid = valid;
4578 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4581 static inline int rt_scope(int ifa_scope)
4583 if (ifa_scope & IFA_HOST)
4584 return RT_SCOPE_HOST;
4585 else if (ifa_scope & IFA_LINK)
4586 return RT_SCOPE_LINK;
4587 else if (ifa_scope & IFA_SITE)
4588 return RT_SCOPE_SITE;
4590 return RT_SCOPE_UNIVERSE;
4593 static inline int inet6_ifaddr_msgsize(void)
4595 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4596 + nla_total_size(16) /* IFA_LOCAL */
4597 + nla_total_size(16) /* IFA_ADDRESS */
4598 + nla_total_size(sizeof(struct ifa_cacheinfo))
4599 + nla_total_size(4) /* IFA_FLAGS */;
4602 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4603 u32 portid, u32 seq, int event, unsigned int flags)
4605 struct nlmsghdr *nlh;
4606 u32 preferred, valid;
4608 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4612 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4613 ifa->idev->dev->ifindex);
4615 if (!((ifa->flags&IFA_F_PERMANENT) &&
4616 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4617 preferred = ifa->prefered_lft;
4618 valid = ifa->valid_lft;
4619 if (preferred != INFINITY_LIFE_TIME) {
4620 long tval = (jiffies - ifa->tstamp)/HZ;
4621 if (preferred > tval)
4625 if (valid != INFINITY_LIFE_TIME) {
4633 preferred = INFINITY_LIFE_TIME;
4634 valid = INFINITY_LIFE_TIME;
4637 if (!ipv6_addr_any(&ifa->peer_addr)) {
4638 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4639 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4642 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4645 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4648 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4651 nlmsg_end(skb, nlh);
4655 nlmsg_cancel(skb, nlh);
4659 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4660 u32 portid, u32 seq, int event, u16 flags)
4662 struct nlmsghdr *nlh;
4663 u8 scope = RT_SCOPE_UNIVERSE;
4664 int ifindex = ifmca->idev->dev->ifindex;
4666 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4667 scope = RT_SCOPE_SITE;
4669 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4673 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4674 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4675 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4676 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4677 nlmsg_cancel(skb, nlh);
4681 nlmsg_end(skb, nlh);
4685 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4686 u32 portid, u32 seq, int event, unsigned int flags)
4688 struct nlmsghdr *nlh;
4689 u8 scope = RT_SCOPE_UNIVERSE;
4690 int ifindex = ifaca->aca_idev->dev->ifindex;
4692 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4693 scope = RT_SCOPE_SITE;
4695 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4699 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4700 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4701 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4702 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4703 nlmsg_cancel(skb, nlh);
4707 nlmsg_end(skb, nlh);
4717 /* called with rcu_read_lock() */
4718 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4719 struct netlink_callback *cb, enum addr_type_t type,
4720 int s_ip_idx, int *p_ip_idx)
4722 struct ifmcaddr6 *ifmca;
4723 struct ifacaddr6 *ifaca;
4725 int ip_idx = *p_ip_idx;
4727 read_lock_bh(&idev->lock);
4729 case UNICAST_ADDR: {
4730 struct inet6_ifaddr *ifa;
4732 /* unicast address incl. temp addr */
4733 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4734 if (++ip_idx < s_ip_idx)
4736 err = inet6_fill_ifaddr(skb, ifa,
4737 NETLINK_CB(cb->skb).portid,
4743 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4747 case MULTICAST_ADDR:
4748 /* multicast address */
4749 for (ifmca = idev->mc_list; ifmca;
4750 ifmca = ifmca->next, ip_idx++) {
4751 if (ip_idx < s_ip_idx)
4753 err = inet6_fill_ifmcaddr(skb, ifmca,
4754 NETLINK_CB(cb->skb).portid,
4763 /* anycast address */
4764 for (ifaca = idev->ac_list; ifaca;
4765 ifaca = ifaca->aca_next, ip_idx++) {
4766 if (ip_idx < s_ip_idx)
4768 err = inet6_fill_ifacaddr(skb, ifaca,
4769 NETLINK_CB(cb->skb).portid,
4780 read_unlock_bh(&idev->lock);
4785 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4786 enum addr_type_t type)
4788 struct net *net = sock_net(skb->sk);
4791 int s_idx, s_ip_idx;
4792 struct net_device *dev;
4793 struct inet6_dev *idev;
4794 struct hlist_head *head;
4797 s_idx = idx = cb->args[1];
4798 s_ip_idx = ip_idx = cb->args[2];
4801 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4802 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4804 head = &net->dev_index_head[h];
4805 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4808 if (h > s_h || idx > s_idx)
4811 idev = __in6_dev_get(dev);
4815 if (in6_dump_addrs(idev, skb, cb, type,
4816 s_ip_idx, &ip_idx) < 0)
4826 cb->args[2] = ip_idx;
4831 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4833 enum addr_type_t type = UNICAST_ADDR;
4835 return inet6_dump_addr(skb, cb, type);
4838 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4840 enum addr_type_t type = MULTICAST_ADDR;
4842 return inet6_dump_addr(skb, cb, type);
4846 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4848 enum addr_type_t type = ANYCAST_ADDR;
4850 return inet6_dump_addr(skb, cb, type);
4853 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4855 struct net *net = sock_net(in_skb->sk);
4856 struct ifaddrmsg *ifm;
4857 struct nlattr *tb[IFA_MAX+1];
4858 struct in6_addr *addr = NULL, *peer;
4859 struct net_device *dev = NULL;
4860 struct inet6_ifaddr *ifa;
4861 struct sk_buff *skb;
4864 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4868 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4874 ifm = nlmsg_data(nlh);
4876 dev = __dev_get_by_index(net, ifm->ifa_index);
4878 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4880 err = -EADDRNOTAVAIL;
4884 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4890 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4891 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4893 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4894 WARN_ON(err == -EMSGSIZE);
4898 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4905 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4907 struct sk_buff *skb;
4908 struct net *net = dev_net(ifa->idev->dev);
4911 /* Don't send DELADDR notification for TENTATIVE address,
4912 * since NEWADDR notification is sent only after removing
4915 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4918 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4922 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4924 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4925 WARN_ON(err == -EMSGSIZE);
4929 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4933 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4936 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4937 __s32 *array, int bytes)
4939 BUG_ON(bytes < (DEVCONF_MAX * 4));
4941 memset(array, 0, bytes);
4942 array[DEVCONF_FORWARDING] = cnf->forwarding;
4943 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4944 array[DEVCONF_MTU6] = cnf->mtu6;
4945 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4946 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4947 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4948 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4949 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4950 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4951 jiffies_to_msecs(cnf->rtr_solicit_interval);
4952 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4953 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
4954 array[DEVCONF_RTR_SOLICIT_DELAY] =
4955 jiffies_to_msecs(cnf->rtr_solicit_delay);
4956 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4957 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4958 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4959 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4960 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4961 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4962 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4963 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4964 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4965 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4966 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4967 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4968 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4969 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4970 #ifdef CONFIG_IPV6_ROUTER_PREF
4971 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4972 array[DEVCONF_RTR_PROBE_INTERVAL] =
4973 jiffies_to_msecs(cnf->rtr_probe_interval);
4974 #ifdef CONFIG_IPV6_ROUTE_INFO
4975 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4978 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4979 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4980 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4981 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4982 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4984 #ifdef CONFIG_IPV6_MROUTE
4985 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4987 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4988 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4989 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4990 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4991 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4992 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4993 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4994 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
4995 /* we omit DEVCONF_STABLE_SECRET for now */
4996 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4997 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
4998 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4999 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5000 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5001 #ifdef CONFIG_IPV6_SEG6_HMAC
5002 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5004 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5005 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5008 static inline size_t inet6_ifla6_size(void)
5010 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5011 + nla_total_size(sizeof(struct ifla_cacheinfo))
5012 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5013 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5014 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5015 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5018 static inline size_t inet6_if_nlmsg_size(void)
5020 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5021 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5022 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5023 + nla_total_size(4) /* IFLA_MTU */
5024 + nla_total_size(4) /* IFLA_LINK */
5025 + nla_total_size(1) /* IFLA_OPERSTATE */
5026 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5029 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5033 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5036 /* Use put_unaligned() because stats may not be aligned for u64. */
5037 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5038 for (i = 1; i < ICMP6_MIB_MAX; i++)
5039 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5041 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5044 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5045 int bytes, size_t syncpoff)
5048 u64 buff[IPSTATS_MIB_MAX];
5049 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5053 memset(buff, 0, sizeof(buff));
5054 buff[0] = IPSTATS_MIB_MAX;
5056 for_each_possible_cpu(c) {
5057 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5058 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5061 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5062 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5065 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5069 case IFLA_INET6_STATS:
5070 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5071 offsetof(struct ipstats_mib, syncp));
5073 case IFLA_INET6_ICMP6STATS:
5074 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5079 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5080 u32 ext_filter_mask)
5083 struct ifla_cacheinfo ci;
5085 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5086 goto nla_put_failure;
5087 ci.max_reasm_len = IPV6_MAXPLEN;
5088 ci.tstamp = cstamp_delta(idev->tstamp);
5089 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5090 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5091 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5092 goto nla_put_failure;
5093 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5095 goto nla_put_failure;
5096 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5098 /* XXX - MC not implemented */
5100 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5103 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5105 goto nla_put_failure;
5106 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5108 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5110 goto nla_put_failure;
5111 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5113 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5115 goto nla_put_failure;
5117 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5118 goto nla_put_failure;
5120 read_lock_bh(&idev->lock);
5121 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5122 read_unlock_bh(&idev->lock);
5130 static size_t inet6_get_link_af_size(const struct net_device *dev,
5131 u32 ext_filter_mask)
5133 if (!__in6_dev_get(dev))
5136 return inet6_ifla6_size();
5139 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5140 u32 ext_filter_mask)
5142 struct inet6_dev *idev = __in6_dev_get(dev);
5147 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5153 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5155 struct inet6_ifaddr *ifp;
5156 struct net_device *dev = idev->dev;
5157 bool clear_token, update_rs = false;
5158 struct in6_addr ll_addr;
5164 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5166 if (!ipv6_accept_ra(idev))
5168 if (idev->cnf.rtr_solicits == 0)
5171 write_lock_bh(&idev->lock);
5173 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5174 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5176 write_unlock_bh(&idev->lock);
5178 clear_token = ipv6_addr_any(token);
5182 if (!idev->dead && (idev->if_flags & IF_READY) &&
5183 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5184 IFA_F_OPTIMISTIC)) {
5185 /* If we're not ready, then normal ifup will take care
5186 * of this. Otherwise, we need to request our rs here.
5188 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5193 write_lock_bh(&idev->lock);
5196 idev->if_flags |= IF_RS_SENT;
5197 idev->rs_interval = rfc3315_s14_backoff_init(
5198 idev->cnf.rtr_solicit_interval);
5199 idev->rs_probes = 1;
5200 addrconf_mod_rs_timer(idev, idev->rs_interval);
5203 /* Well, that's kinda nasty ... */
5204 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5205 spin_lock(&ifp->lock);
5206 if (ifp->tokenized) {
5208 ifp->prefered_lft = 0;
5210 spin_unlock(&ifp->lock);
5213 write_unlock_bh(&idev->lock);
5214 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5215 addrconf_verify_rtnl();
5219 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5220 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5221 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5224 static int inet6_validate_link_af(const struct net_device *dev,
5225 const struct nlattr *nla)
5227 struct nlattr *tb[IFLA_INET6_MAX + 1];
5229 if (dev && !__in6_dev_get(dev))
5230 return -EAFNOSUPPORT;
5232 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5235 static int check_addr_gen_mode(int mode)
5237 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5238 mode != IN6_ADDR_GEN_MODE_NONE &&
5239 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5240 mode != IN6_ADDR_GEN_MODE_RANDOM)
5245 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5248 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5249 !idev->cnf.stable_secret.initialized &&
5250 !net->ipv6.devconf_dflt->stable_secret.initialized)
5255 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5258 struct inet6_dev *idev = __in6_dev_get(dev);
5259 struct nlattr *tb[IFLA_INET6_MAX + 1];
5262 return -EAFNOSUPPORT;
5264 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5267 if (tb[IFLA_INET6_TOKEN]) {
5268 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5273 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5274 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5276 if (check_addr_gen_mode(mode) < 0 ||
5277 check_stable_privacy(idev, dev_net(dev), mode) < 0)
5280 idev->cnf.addr_gen_mode = mode;
5287 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5288 u32 portid, u32 seq, int event, unsigned int flags)
5290 struct net_device *dev = idev->dev;
5291 struct ifinfomsg *hdr;
5292 struct nlmsghdr *nlh;
5295 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5299 hdr = nlmsg_data(nlh);
5300 hdr->ifi_family = AF_INET6;
5302 hdr->ifi_type = dev->type;
5303 hdr->ifi_index = dev->ifindex;
5304 hdr->ifi_flags = dev_get_flags(dev);
5305 hdr->ifi_change = 0;
5307 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5309 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5310 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5311 (dev->ifindex != dev_get_iflink(dev) &&
5312 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5313 nla_put_u8(skb, IFLA_OPERSTATE,
5314 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5315 goto nla_put_failure;
5316 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5318 goto nla_put_failure;
5320 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5321 goto nla_put_failure;
5323 nla_nest_end(skb, protoinfo);
5324 nlmsg_end(skb, nlh);
5328 nlmsg_cancel(skb, nlh);
5332 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5334 struct net *net = sock_net(skb->sk);
5337 struct net_device *dev;
5338 struct inet6_dev *idev;
5339 struct hlist_head *head;
5342 s_idx = cb->args[1];
5345 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5347 head = &net->dev_index_head[h];
5348 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5351 idev = __in6_dev_get(dev);
5354 if (inet6_fill_ifinfo(skb, idev,
5355 NETLINK_CB(cb->skb).portid,
5357 RTM_NEWLINK, NLM_F_MULTI) < 0)
5371 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5373 struct sk_buff *skb;
5374 struct net *net = dev_net(idev->dev);
5377 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5381 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5383 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5384 WARN_ON(err == -EMSGSIZE);
5388 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5392 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5395 static inline size_t inet6_prefix_nlmsg_size(void)
5397 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5398 + nla_total_size(sizeof(struct in6_addr))
5399 + nla_total_size(sizeof(struct prefix_cacheinfo));
5402 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5403 struct prefix_info *pinfo, u32 portid, u32 seq,
5404 int event, unsigned int flags)
5406 struct prefixmsg *pmsg;
5407 struct nlmsghdr *nlh;
5408 struct prefix_cacheinfo ci;
5410 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5414 pmsg = nlmsg_data(nlh);
5415 pmsg->prefix_family = AF_INET6;
5416 pmsg->prefix_pad1 = 0;
5417 pmsg->prefix_pad2 = 0;
5418 pmsg->prefix_ifindex = idev->dev->ifindex;
5419 pmsg->prefix_len = pinfo->prefix_len;
5420 pmsg->prefix_type = pinfo->type;
5421 pmsg->prefix_pad3 = 0;
5422 pmsg->prefix_flags = 0;
5424 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5425 if (pinfo->autoconf)
5426 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5428 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5429 goto nla_put_failure;
5430 ci.preferred_time = ntohl(pinfo->prefered);
5431 ci.valid_time = ntohl(pinfo->valid);
5432 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5433 goto nla_put_failure;
5434 nlmsg_end(skb, nlh);
5438 nlmsg_cancel(skb, nlh);
5442 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5443 struct prefix_info *pinfo)
5445 struct sk_buff *skb;
5446 struct net *net = dev_net(idev->dev);
5449 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5453 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5455 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5456 WARN_ON(err == -EMSGSIZE);
5460 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5464 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5467 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5469 struct net *net = dev_net(ifp->idev->dev);
5474 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5479 * If the address was optimistic
5480 * we inserted the route at the start of
5481 * our DAD process, so we don't need
5484 if (!(ifp->rt->rt6i_node))
5485 ip6_ins_rt(ifp->rt);
5486 if (ifp->idev->cnf.forwarding)
5487 addrconf_join_anycast(ifp);
5488 if (!ipv6_addr_any(&ifp->peer_addr))
5489 addrconf_prefix_route(&ifp->peer_addr, 128,
5490 ifp->idev->dev, 0, 0);
5493 if (ifp->idev->cnf.forwarding)
5494 addrconf_leave_anycast(ifp);
5495 addrconf_leave_solict(ifp->idev, &ifp->addr);
5496 if (!ipv6_addr_any(&ifp->peer_addr)) {
5497 struct rt6_info *rt;
5499 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5500 ifp->idev->dev, 0, 0);
5505 dst_hold(&ifp->rt->dst);
5506 ip6_del_rt(ifp->rt);
5508 rt_genid_bump_ipv6(net);
5511 atomic_inc(&net->ipv6.dev_addr_genid);
5514 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5517 if (likely(ifp->idev->dead == 0))
5518 __ipv6_ifa_notify(event, ifp);
5519 rcu_read_unlock_bh();
5522 #ifdef CONFIG_SYSCTL
5525 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5526 void __user *buffer, size_t *lenp, loff_t *ppos)
5528 int *valp = ctl->data;
5531 struct ctl_table lctl;
5535 * ctl->data points to idev->cnf.forwarding, we should
5536 * not modify it until we get the rtnl lock.
5541 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5544 ret = addrconf_fixup_forwarding(ctl, valp, val);
5551 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5552 void __user *buffer, size_t *lenp, loff_t *ppos)
5554 struct inet6_dev *idev = ctl->extra1;
5555 int min_mtu = IPV6_MIN_MTU;
5556 struct ctl_table lctl;
5559 lctl.extra1 = &min_mtu;
5560 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5562 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5565 static void dev_disable_change(struct inet6_dev *idev)
5567 struct netdev_notifier_info info;
5569 if (!idev || !idev->dev)
5572 netdev_notifier_info_init(&info, idev->dev);
5573 if (idev->cnf.disable_ipv6)
5574 addrconf_notify(NULL, NETDEV_DOWN, &info);
5576 addrconf_notify(NULL, NETDEV_UP, &info);
5579 static void addrconf_disable_change(struct net *net, __s32 newf)
5581 struct net_device *dev;
5582 struct inet6_dev *idev;
5584 for_each_netdev(net, dev) {
5585 idev = __in6_dev_get(dev);
5587 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5588 idev->cnf.disable_ipv6 = newf;
5590 dev_disable_change(idev);
5595 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5600 if (!rtnl_trylock())
5601 return restart_syscall();
5603 net = (struct net *)table->extra2;
5607 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5612 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5613 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5614 addrconf_disable_change(net, newf);
5615 } else if ((!newf) ^ (!old))
5616 dev_disable_change((struct inet6_dev *)table->extra1);
5623 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5624 void __user *buffer, size_t *lenp, loff_t *ppos)
5626 int *valp = ctl->data;
5629 struct ctl_table lctl;
5633 * ctl->data points to idev->cnf.disable_ipv6, we should
5634 * not modify it until we get the rtnl lock.
5639 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5642 ret = addrconf_disable_ipv6(ctl, valp, val);
5649 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5650 void __user *buffer, size_t *lenp, loff_t *ppos)
5652 int *valp = ctl->data;
5657 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5660 if (write && old != new) {
5661 struct net *net = ctl->extra2;
5663 if (!rtnl_trylock())
5664 return restart_syscall();
5666 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5667 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5668 NETCONFA_IFINDEX_DEFAULT,
5669 net->ipv6.devconf_dflt);
5670 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5671 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5672 NETCONFA_IFINDEX_ALL,
5673 net->ipv6.devconf_all);
5675 struct inet6_dev *idev = ctl->extra1;
5677 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5687 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5688 void __user *buffer, size_t *lenp,
5693 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5694 struct net *net = (struct net *)ctl->extra2;
5696 if (!rtnl_trylock())
5697 return restart_syscall();
5699 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5702 new_val = *((int *)ctl->data);
5704 if (check_addr_gen_mode(new_val) < 0) {
5709 /* request for default */
5710 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5711 ipv6_devconf_dflt.addr_gen_mode = new_val;
5713 /* request for individual net device */
5718 if (check_stable_privacy(idev, net, new_val) < 0) {
5723 if (idev->cnf.addr_gen_mode != new_val) {
5724 idev->cnf.addr_gen_mode = new_val;
5725 addrconf_dev_config(idev->dev);
5736 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5737 void __user *buffer, size_t *lenp,
5741 struct in6_addr addr;
5742 char str[IPV6_MAX_STRLEN];
5743 struct ctl_table lctl = *ctl;
5744 struct net *net = ctl->extra2;
5745 struct ipv6_stable_secret *secret = ctl->data;
5747 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5750 lctl.maxlen = IPV6_MAX_STRLEN;
5753 if (!rtnl_trylock())
5754 return restart_syscall();
5756 if (!write && !secret->initialized) {
5761 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5762 if (err >= sizeof(str)) {
5767 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5771 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5776 secret->initialized = true;
5777 secret->secret = addr;
5779 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5780 struct net_device *dev;
5782 for_each_netdev(net, dev) {
5783 struct inet6_dev *idev = __in6_dev_get(dev);
5786 idev->cnf.addr_gen_mode =
5787 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5791 struct inet6_dev *idev = ctl->extra1;
5793 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5803 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5805 void __user *buffer,
5809 int *valp = ctl->data;
5812 struct ctl_table lctl;
5815 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5816 * we should not modify it until we get the rtnl lock.
5821 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5824 ret = addrconf_fixup_linkdown(ctl, valp, val);
5830 static int minus_one = -1;
5831 static const int one = 1;
5832 static const int two_five_five = 255;
5834 static const struct ctl_table addrconf_sysctl[] = {
5836 .procname = "forwarding",
5837 .data = &ipv6_devconf.forwarding,
5838 .maxlen = sizeof(int),
5840 .proc_handler = addrconf_sysctl_forward,
5843 .procname = "hop_limit",
5844 .data = &ipv6_devconf.hop_limit,
5845 .maxlen = sizeof(int),
5847 .proc_handler = proc_dointvec_minmax,
5848 .extra1 = (void *)&one,
5849 .extra2 = (void *)&two_five_five,
5853 .data = &ipv6_devconf.mtu6,
5854 .maxlen = sizeof(int),
5856 .proc_handler = addrconf_sysctl_mtu,
5859 .procname = "accept_ra",
5860 .data = &ipv6_devconf.accept_ra,
5861 .maxlen = sizeof(int),
5863 .proc_handler = proc_dointvec,
5866 .procname = "accept_redirects",
5867 .data = &ipv6_devconf.accept_redirects,
5868 .maxlen = sizeof(int),
5870 .proc_handler = proc_dointvec,
5873 .procname = "autoconf",
5874 .data = &ipv6_devconf.autoconf,
5875 .maxlen = sizeof(int),
5877 .proc_handler = proc_dointvec,
5880 .procname = "dad_transmits",
5881 .data = &ipv6_devconf.dad_transmits,
5882 .maxlen = sizeof(int),
5884 .proc_handler = proc_dointvec,
5887 .procname = "router_solicitations",
5888 .data = &ipv6_devconf.rtr_solicits,
5889 .maxlen = sizeof(int),
5891 .proc_handler = proc_dointvec_minmax,
5892 .extra1 = &minus_one,
5895 .procname = "router_solicitation_interval",
5896 .data = &ipv6_devconf.rtr_solicit_interval,
5897 .maxlen = sizeof(int),
5899 .proc_handler = proc_dointvec_jiffies,
5902 .procname = "router_solicitation_max_interval",
5903 .data = &ipv6_devconf.rtr_solicit_max_interval,
5904 .maxlen = sizeof(int),
5906 .proc_handler = proc_dointvec_jiffies,
5909 .procname = "router_solicitation_delay",
5910 .data = &ipv6_devconf.rtr_solicit_delay,
5911 .maxlen = sizeof(int),
5913 .proc_handler = proc_dointvec_jiffies,
5916 .procname = "force_mld_version",
5917 .data = &ipv6_devconf.force_mld_version,
5918 .maxlen = sizeof(int),
5920 .proc_handler = proc_dointvec,
5923 .procname = "mldv1_unsolicited_report_interval",
5925 &ipv6_devconf.mldv1_unsolicited_report_interval,
5926 .maxlen = sizeof(int),
5928 .proc_handler = proc_dointvec_ms_jiffies,
5931 .procname = "mldv2_unsolicited_report_interval",
5933 &ipv6_devconf.mldv2_unsolicited_report_interval,
5934 .maxlen = sizeof(int),
5936 .proc_handler = proc_dointvec_ms_jiffies,
5939 .procname = "use_tempaddr",
5940 .data = &ipv6_devconf.use_tempaddr,
5941 .maxlen = sizeof(int),
5943 .proc_handler = proc_dointvec,
5946 .procname = "temp_valid_lft",
5947 .data = &ipv6_devconf.temp_valid_lft,
5948 .maxlen = sizeof(int),
5950 .proc_handler = proc_dointvec,
5953 .procname = "temp_prefered_lft",
5954 .data = &ipv6_devconf.temp_prefered_lft,
5955 .maxlen = sizeof(int),
5957 .proc_handler = proc_dointvec,
5960 .procname = "regen_max_retry",
5961 .data = &ipv6_devconf.regen_max_retry,
5962 .maxlen = sizeof(int),
5964 .proc_handler = proc_dointvec,
5967 .procname = "max_desync_factor",
5968 .data = &ipv6_devconf.max_desync_factor,
5969 .maxlen = sizeof(int),
5971 .proc_handler = proc_dointvec,
5974 .procname = "max_addresses",
5975 .data = &ipv6_devconf.max_addresses,
5976 .maxlen = sizeof(int),
5978 .proc_handler = proc_dointvec,
5981 .procname = "accept_ra_defrtr",
5982 .data = &ipv6_devconf.accept_ra_defrtr,
5983 .maxlen = sizeof(int),
5985 .proc_handler = proc_dointvec,
5988 .procname = "accept_ra_min_hop_limit",
5989 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5990 .maxlen = sizeof(int),
5992 .proc_handler = proc_dointvec,
5995 .procname = "accept_ra_pinfo",
5996 .data = &ipv6_devconf.accept_ra_pinfo,
5997 .maxlen = sizeof(int),
5999 .proc_handler = proc_dointvec,
6001 #ifdef CONFIG_IPV6_ROUTER_PREF
6003 .procname = "accept_ra_rtr_pref",
6004 .data = &ipv6_devconf.accept_ra_rtr_pref,
6005 .maxlen = sizeof(int),
6007 .proc_handler = proc_dointvec,
6010 .procname = "router_probe_interval",
6011 .data = &ipv6_devconf.rtr_probe_interval,
6012 .maxlen = sizeof(int),
6014 .proc_handler = proc_dointvec_jiffies,
6016 #ifdef CONFIG_IPV6_ROUTE_INFO
6018 .procname = "accept_ra_rt_info_max_plen",
6019 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6020 .maxlen = sizeof(int),
6022 .proc_handler = proc_dointvec,
6027 .procname = "proxy_ndp",
6028 .data = &ipv6_devconf.proxy_ndp,
6029 .maxlen = sizeof(int),
6031 .proc_handler = addrconf_sysctl_proxy_ndp,
6034 .procname = "accept_source_route",
6035 .data = &ipv6_devconf.accept_source_route,
6036 .maxlen = sizeof(int),
6038 .proc_handler = proc_dointvec,
6040 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6042 .procname = "optimistic_dad",
6043 .data = &ipv6_devconf.optimistic_dad,
6044 .maxlen = sizeof(int),
6046 .proc_handler = proc_dointvec,
6049 .procname = "use_optimistic",
6050 .data = &ipv6_devconf.use_optimistic,
6051 .maxlen = sizeof(int),
6053 .proc_handler = proc_dointvec,
6056 #ifdef CONFIG_IPV6_MROUTE
6058 .procname = "mc_forwarding",
6059 .data = &ipv6_devconf.mc_forwarding,
6060 .maxlen = sizeof(int),
6062 .proc_handler = proc_dointvec,
6066 .procname = "disable_ipv6",
6067 .data = &ipv6_devconf.disable_ipv6,
6068 .maxlen = sizeof(int),
6070 .proc_handler = addrconf_sysctl_disable,
6073 .procname = "accept_dad",
6074 .data = &ipv6_devconf.accept_dad,
6075 .maxlen = sizeof(int),
6077 .proc_handler = proc_dointvec,
6080 .procname = "force_tllao",
6081 .data = &ipv6_devconf.force_tllao,
6082 .maxlen = sizeof(int),
6084 .proc_handler = proc_dointvec
6087 .procname = "ndisc_notify",
6088 .data = &ipv6_devconf.ndisc_notify,
6089 .maxlen = sizeof(int),
6091 .proc_handler = proc_dointvec
6094 .procname = "suppress_frag_ndisc",
6095 .data = &ipv6_devconf.suppress_frag_ndisc,
6096 .maxlen = sizeof(int),
6098 .proc_handler = proc_dointvec
6101 .procname = "accept_ra_from_local",
6102 .data = &ipv6_devconf.accept_ra_from_local,
6103 .maxlen = sizeof(int),
6105 .proc_handler = proc_dointvec,
6108 .procname = "accept_ra_mtu",
6109 .data = &ipv6_devconf.accept_ra_mtu,
6110 .maxlen = sizeof(int),
6112 .proc_handler = proc_dointvec,
6115 .procname = "stable_secret",
6116 .data = &ipv6_devconf.stable_secret,
6117 .maxlen = IPV6_MAX_STRLEN,
6119 .proc_handler = addrconf_sysctl_stable_secret,
6122 .procname = "use_oif_addrs_only",
6123 .data = &ipv6_devconf.use_oif_addrs_only,
6124 .maxlen = sizeof(int),
6126 .proc_handler = proc_dointvec,
6129 .procname = "ignore_routes_with_linkdown",
6130 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6131 .maxlen = sizeof(int),
6133 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6136 .procname = "drop_unicast_in_l2_multicast",
6137 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6138 .maxlen = sizeof(int),
6140 .proc_handler = proc_dointvec,
6143 .procname = "drop_unsolicited_na",
6144 .data = &ipv6_devconf.drop_unsolicited_na,
6145 .maxlen = sizeof(int),
6147 .proc_handler = proc_dointvec,
6150 .procname = "keep_addr_on_down",
6151 .data = &ipv6_devconf.keep_addr_on_down,
6152 .maxlen = sizeof(int),
6154 .proc_handler = proc_dointvec,
6158 .procname = "seg6_enabled",
6159 .data = &ipv6_devconf.seg6_enabled,
6160 .maxlen = sizeof(int),
6162 .proc_handler = proc_dointvec,
6164 #ifdef CONFIG_IPV6_SEG6_HMAC
6166 .procname = "seg6_require_hmac",
6167 .data = &ipv6_devconf.seg6_require_hmac,
6168 .maxlen = sizeof(int),
6170 .proc_handler = proc_dointvec,
6174 .procname = "enhanced_dad",
6175 .data = &ipv6_devconf.enhanced_dad,
6176 .maxlen = sizeof(int),
6178 .proc_handler = proc_dointvec,
6181 .procname = "addr_gen_mode",
6182 .data = &ipv6_devconf.addr_gen_mode,
6183 .maxlen = sizeof(int),
6185 .proc_handler = addrconf_sysctl_addr_gen_mode,
6192 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6193 struct inet6_dev *idev, struct ipv6_devconf *p)
6196 struct ctl_table *table;
6197 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6199 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6203 for (i = 0; table[i].data; i++) {
6204 table[i].data += (char *)p - (char *)&ipv6_devconf;
6205 /* If one of these is already set, then it is not safe to
6206 * overwrite either of them: this makes proc_dointvec_minmax
6209 if (!table[i].extra1 && !table[i].extra2) {
6210 table[i].extra1 = idev; /* embedded; no ref */
6211 table[i].extra2 = net;
6215 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6217 p->sysctl_header = register_net_sysctl(net, path, table);
6218 if (!p->sysctl_header)
6221 if (!strcmp(dev_name, "all"))
6222 ifindex = NETCONFA_IFINDEX_ALL;
6223 else if (!strcmp(dev_name, "default"))
6224 ifindex = NETCONFA_IFINDEX_DEFAULT;
6226 ifindex = idev->dev->ifindex;
6227 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
6236 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6238 struct ctl_table *table;
6240 if (!p->sysctl_header)
6243 table = p->sysctl_header->ctl_table_arg;
6244 unregister_net_sysctl_table(p->sysctl_header);
6245 p->sysctl_header = NULL;
6249 static int addrconf_sysctl_register(struct inet6_dev *idev)
6253 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6256 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6257 &ndisc_ifinfo_sysctl_change);
6260 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6263 neigh_sysctl_unregister(idev->nd_parms);
6268 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6270 __addrconf_sysctl_unregister(&idev->cnf);
6271 neigh_sysctl_unregister(idev->nd_parms);
6277 static int __net_init addrconf_init_net(struct net *net)
6280 struct ipv6_devconf *all, *dflt;
6282 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6286 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6288 goto err_alloc_dflt;
6290 /* these will be inherited by all namespaces */
6291 dflt->autoconf = ipv6_defaults.autoconf;
6292 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6294 dflt->stable_secret.initialized = false;
6295 all->stable_secret.initialized = false;
6297 net->ipv6.devconf_all = all;
6298 net->ipv6.devconf_dflt = dflt;
6300 #ifdef CONFIG_SYSCTL
6301 err = __addrconf_sysctl_register(net, "all", NULL, all);
6305 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6311 #ifdef CONFIG_SYSCTL
6313 __addrconf_sysctl_unregister(all);
6323 static void __net_exit addrconf_exit_net(struct net *net)
6325 #ifdef CONFIG_SYSCTL
6326 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6327 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6329 kfree(net->ipv6.devconf_dflt);
6330 kfree(net->ipv6.devconf_all);
6333 static struct pernet_operations addrconf_ops = {
6334 .init = addrconf_init_net,
6335 .exit = addrconf_exit_net,
6338 static struct rtnl_af_ops inet6_ops __read_mostly = {
6340 .fill_link_af = inet6_fill_link_af,
6341 .get_link_af_size = inet6_get_link_af_size,
6342 .validate_link_af = inet6_validate_link_af,
6343 .set_link_af = inet6_set_link_af,
6347 * Init / cleanup code
6350 int __init addrconf_init(void)
6352 struct inet6_dev *idev;
6355 err = ipv6_addr_label_init();
6357 pr_crit("%s: cannot initialize default policy table: %d\n",
6362 err = register_pernet_subsys(&addrconf_ops);
6366 addrconf_wq = create_workqueue("ipv6_addrconf");
6372 /* The addrconf netdev notifier requires that loopback_dev
6373 * has it's ipv6 private information allocated and setup
6374 * before it can bring up and give link-local addresses
6375 * to other devices which are up.
6377 * Unfortunately, loopback_dev is not necessarily the first
6378 * entry in the global dev_base list of net devices. In fact,
6379 * it is likely to be the very last entry on that list.
6380 * So this causes the notifier registry below to try and
6381 * give link-local addresses to all devices besides loopback_dev
6382 * first, then loopback_dev, which cases all the non-loopback_dev
6383 * devices to fail to get a link-local address.
6385 * So, as a temporary fix, allocate the ipv6 structure for
6386 * loopback_dev first by hand.
6387 * Longer term, all of the dependencies ipv6 has upon the loopback
6388 * device and it being up should be removed.
6391 idev = ipv6_add_dev(init_net.loopback_dev);
6394 err = PTR_ERR(idev);
6398 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6399 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6401 register_netdevice_notifier(&ipv6_dev_notf);
6405 rtnl_af_register(&inet6_ops);
6407 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6412 /* Only the first call to __rtnl_register can fail */
6413 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6414 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6415 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6416 inet6_dump_ifaddr, NULL);
6417 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6418 inet6_dump_ifmcaddr, NULL);
6419 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6420 inet6_dump_ifacaddr, NULL);
6421 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6422 inet6_netconf_dump_devconf, NULL);
6424 ipv6_addr_label_rtnl_register();
6428 rtnl_af_unregister(&inet6_ops);
6429 unregister_netdevice_notifier(&ipv6_dev_notf);
6431 destroy_workqueue(addrconf_wq);
6433 unregister_pernet_subsys(&addrconf_ops);
6435 ipv6_addr_label_cleanup();
6440 void addrconf_cleanup(void)
6442 struct net_device *dev;
6445 unregister_netdevice_notifier(&ipv6_dev_notf);
6446 unregister_pernet_subsys(&addrconf_ops);
6447 ipv6_addr_label_cleanup();
6451 __rtnl_af_unregister(&inet6_ops);
6453 /* clean dev list */
6454 for_each_netdev(&init_net, dev) {
6455 if (__in6_dev_get(dev) == NULL)
6457 addrconf_ifdown(dev, 1);
6459 addrconf_ifdown(init_net.loopback_dev, 2);
6464 spin_lock_bh(&addrconf_hash_lock);
6465 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6466 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
6467 spin_unlock_bh(&addrconf_hash_lock);
6468 cancel_delayed_work(&addr_chk_work);
6471 destroy_workqueue(addrconf_wq);