2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
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
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/inet.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_addr.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_arcnet.h>
55 #include <linux/if_infiniband.h>
56 #include <linux/route.h>
57 #include <linux/inetdevice.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
61 #include <linux/sysctl.h>
63 #include <linux/capability.h>
64 #include <linux/delay.h>
65 #include <linux/notifier.h>
66 #include <linux/string.h>
67 #include <linux/hash.h>
69 #include <net/net_namespace.h>
73 #include <net/6lowpan.h>
74 #include <net/firewire.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <net/l3mdev.h>
85 #include <linux/if_tunnel.h>
86 #include <linux/rtnetlink.h>
87 #include <linux/netconf.h>
88 #include <linux/random.h>
89 #include <linux/uaccess.h>
90 #include <asm/unaligned.h>
92 #include <linux/proc_fs.h>
93 #include <linux/seq_file.h>
94 #include <linux/export.h>
96 /* Set to 3 to get tracing... */
100 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
102 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
105 #define INFINITY_LIFE_TIME 0xFFFFFFFF
107 #define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110 static inline u32 cstamp_delta(unsigned long cstamp)
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
115 static inline s32 rfc3315_s14_backoff_init(s32 irt)
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
119 do_div(tmp, 1000000);
123 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
127 do_div(tmp, 1000000);
128 if ((s32)tmp > mrt) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
131 do_div(tmp, 1000000);
137 static int addrconf_sysctl_register(struct inet6_dev *idev);
138 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
145 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
150 static void __ipv6_regen_rndid(struct inet6_dev *idev);
151 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
152 static void ipv6_regen_rndid(unsigned long data);
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);
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,
244 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
246 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
247 .mtu6 = IPV6_MIN_MTU,
249 .accept_redirects = 1,
251 .force_mld_version = 0,
252 .mldv1_unsolicited_report_interval = 10 * HZ,
253 .mldv2_unsolicited_report_interval = HZ,
255 .rtr_solicits = MAX_RTR_SOLICITATIONS,
256 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
257 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
258 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
260 .temp_valid_lft = TEMP_VALID_LIFETIME,
261 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
262 .regen_max_retry = REGEN_MAX_RETRY,
263 .max_desync_factor = MAX_DESYNC_FACTOR,
264 .max_addresses = IPV6_MAX_ADDRESSES,
265 .accept_ra_defrtr = 1,
266 .accept_ra_from_local = 0,
267 .accept_ra_min_hop_limit= 1,
268 .accept_ra_pinfo = 1,
269 #ifdef CONFIG_IPV6_ROUTER_PREF
270 .accept_ra_rtr_pref = 1,
271 .rtr_probe_interval = 60 * HZ,
272 #ifdef CONFIG_IPV6_ROUTE_INFO
273 .accept_ra_rt_info_max_plen = 0,
277 .accept_source_route = 0, /* we do not accept RH0 by default. */
280 .suppress_frag_ndisc = 1,
283 .initialized = false,
285 .use_oif_addrs_only = 0,
286 .ignore_routes_with_linkdown = 0,
287 .keep_addr_on_down = 0,
290 /* Check if a valid qdisc is available */
291 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
293 return !qdisc_tx_is_noop(dev);
296 static void addrconf_del_rs_timer(struct inet6_dev *idev)
298 if (del_timer(&idev->rs_timer))
302 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
304 if (cancel_delayed_work(&ifp->dad_work))
308 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
311 if (!timer_pending(&idev->rs_timer))
313 mod_timer(&idev->rs_timer, jiffies + when);
316 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
319 if (!delayed_work_pending(&ifp->dad_work))
321 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
324 static int snmp6_alloc_dev(struct inet6_dev *idev)
328 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
329 if (!idev->stats.ipv6)
332 for_each_possible_cpu(i) {
333 struct ipstats_mib *addrconf_stats;
334 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
335 u64_stats_init(&addrconf_stats->syncp);
339 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
341 if (!idev->stats.icmpv6dev)
343 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
345 if (!idev->stats.icmpv6msgdev)
351 kfree(idev->stats.icmpv6dev);
353 free_percpu(idev->stats.ipv6);
358 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
360 struct inet6_dev *ndev;
365 if (dev->mtu < IPV6_MIN_MTU)
366 return ERR_PTR(-EINVAL);
368 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
372 rwlock_init(&ndev->lock);
374 INIT_LIST_HEAD(&ndev->addr_list);
375 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
376 (unsigned long)ndev);
377 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
379 if (ndev->cnf.stable_secret.initialized)
380 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
382 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
384 ndev->cnf.mtu6 = dev->mtu;
385 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
386 if (!ndev->nd_parms) {
390 if (ndev->cnf.forwarding)
391 dev_disable_lro(dev);
392 /* We refer to the device */
395 if (snmp6_alloc_dev(ndev) < 0) {
397 "%s: cannot allocate memory for statistics; dev=%s.\n",
398 __func__, dev->name);
399 neigh_parms_release(&nd_tbl, ndev->nd_parms);
405 if (snmp6_register_dev(ndev) < 0) {
407 "%s: cannot create /proc/net/dev_snmp6/%s\n",
408 __func__, dev->name);
412 /* One reference from device. We must do this before
413 * we invoke __ipv6_regen_rndid().
417 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
418 ndev->cnf.accept_dad = -1;
420 #if IS_ENABLED(CONFIG_IPV6_SIT)
421 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
422 pr_info("%s: Disabled Multicast RS\n", dev->name);
423 ndev->cnf.rtr_solicits = 0;
427 INIT_LIST_HEAD(&ndev->tempaddr_list);
428 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
429 if ((dev->flags&IFF_LOOPBACK) ||
430 dev->type == ARPHRD_TUNNEL ||
431 dev->type == ARPHRD_TUNNEL6 ||
432 dev->type == ARPHRD_SIT ||
433 dev->type == ARPHRD_NONE) {
434 ndev->cnf.use_tempaddr = -1;
437 ipv6_regen_rndid((unsigned long) ndev);
440 ndev->token = in6addr_any;
442 if (netif_running(dev) && addrconf_qdisc_ok(dev))
443 ndev->if_flags |= IF_READY;
445 ipv6_mc_init_dev(ndev);
446 ndev->tstamp = jiffies;
447 err = addrconf_sysctl_register(ndev);
449 ipv6_mc_destroy_dev(ndev);
450 del_timer(&ndev->regen_timer);
451 snmp6_unregister_dev(ndev);
454 /* protected by rtnl_lock */
455 rcu_assign_pointer(dev->ip6_ptr, ndev);
457 /* Join interface-local all-node multicast group */
458 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
460 /* Join all-node multicast group */
461 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
463 /* Join all-router multicast group if forwarding is set */
464 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
465 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
470 neigh_parms_release(&nd_tbl, ndev->nd_parms);
472 in6_dev_finish_destroy(ndev);
476 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
478 struct inet6_dev *idev;
482 idev = __in6_dev_get(dev);
484 idev = ipv6_add_dev(dev);
489 if (dev->flags&IFF_UP)
494 static int inet6_netconf_msgsize_devconf(int type)
496 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
497 + nla_total_size(4); /* NETCONFA_IFINDEX */
500 if (type == NETCONFA_ALL)
503 if (all || type == NETCONFA_FORWARDING)
504 size += nla_total_size(4);
505 #ifdef CONFIG_IPV6_MROUTE
506 if (all || type == NETCONFA_MC_FORWARDING)
507 size += nla_total_size(4);
509 if (all || type == NETCONFA_PROXY_NEIGH)
510 size += nla_total_size(4);
512 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
513 size += nla_total_size(4);
518 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
519 struct ipv6_devconf *devconf, u32 portid,
520 u32 seq, int event, unsigned int flags,
523 struct nlmsghdr *nlh;
524 struct netconfmsg *ncm;
527 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
532 if (type == NETCONFA_ALL)
535 ncm = nlmsg_data(nlh);
536 ncm->ncm_family = AF_INET6;
538 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
539 goto nla_put_failure;
541 if ((all || type == NETCONFA_FORWARDING) &&
542 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
543 goto nla_put_failure;
544 #ifdef CONFIG_IPV6_MROUTE
545 if ((all || type == NETCONFA_MC_FORWARDING) &&
546 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
547 devconf->mc_forwarding) < 0)
548 goto nla_put_failure;
550 if ((all || type == NETCONFA_PROXY_NEIGH) &&
551 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
552 goto nla_put_failure;
554 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
555 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
556 devconf->ignore_routes_with_linkdown) < 0)
557 goto nla_put_failure;
563 nlmsg_cancel(skb, nlh);
567 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
568 struct ipv6_devconf *devconf)
573 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
577 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
578 RTM_NEWNETCONF, 0, type);
580 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
581 WARN_ON(err == -EMSGSIZE);
585 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
588 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
591 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
592 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
593 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
594 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
595 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
598 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
599 struct nlmsghdr *nlh)
601 struct net *net = sock_net(in_skb->sk);
602 struct nlattr *tb[NETCONFA_MAX+1];
603 struct netconfmsg *ncm;
605 struct ipv6_devconf *devconf;
606 struct inet6_dev *in6_dev;
607 struct net_device *dev;
611 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
612 devconf_ipv6_policy);
617 if (!tb[NETCONFA_IFINDEX])
620 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
622 case NETCONFA_IFINDEX_ALL:
623 devconf = net->ipv6.devconf_all;
625 case NETCONFA_IFINDEX_DEFAULT:
626 devconf = net->ipv6.devconf_dflt;
629 dev = __dev_get_by_index(net, ifindex);
632 in6_dev = __in6_dev_get(dev);
635 devconf = &in6_dev->cnf;
640 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
644 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
645 NETLINK_CB(in_skb).portid,
646 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
649 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
650 WARN_ON(err == -EMSGSIZE);
654 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
659 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
660 struct netlink_callback *cb)
662 struct net *net = sock_net(skb->sk);
665 struct net_device *dev;
666 struct inet6_dev *idev;
667 struct hlist_head *head;
670 s_idx = idx = cb->args[1];
672 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
674 head = &net->dev_index_head[h];
676 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
678 hlist_for_each_entry_rcu(dev, head, index_hlist) {
681 idev = __in6_dev_get(dev);
685 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
687 NETLINK_CB(cb->skb).portid,
695 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
701 if (h == NETDEV_HASHENTRIES) {
702 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
703 net->ipv6.devconf_all,
704 NETLINK_CB(cb->skb).portid,
706 RTM_NEWNETCONF, NLM_F_MULTI,
712 if (h == NETDEV_HASHENTRIES + 1) {
713 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
714 net->ipv6.devconf_dflt,
715 NETLINK_CB(cb->skb).portid,
717 RTM_NEWNETCONF, NLM_F_MULTI,
731 static void dev_forward_change(struct inet6_dev *idev)
733 struct net_device *dev;
734 struct inet6_ifaddr *ifa;
739 if (idev->cnf.forwarding)
740 dev_disable_lro(dev);
741 if (dev->flags & IFF_MULTICAST) {
742 if (idev->cnf.forwarding) {
743 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
744 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
745 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
747 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
748 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
749 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
753 list_for_each_entry(ifa, &idev->addr_list, if_list) {
754 if (ifa->flags&IFA_F_TENTATIVE)
756 if (idev->cnf.forwarding)
757 addrconf_join_anycast(ifa);
759 addrconf_leave_anycast(ifa);
761 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
762 dev->ifindex, &idev->cnf);
766 static void addrconf_forward_change(struct net *net, __s32 newf)
768 struct net_device *dev;
769 struct inet6_dev *idev;
771 for_each_netdev(net, dev) {
772 idev = __in6_dev_get(dev);
774 int changed = (!idev->cnf.forwarding) ^ (!newf);
775 idev->cnf.forwarding = newf;
777 dev_forward_change(idev);
782 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
788 return restart_syscall();
790 net = (struct net *)table->extra2;
794 if (p == &net->ipv6.devconf_dflt->forwarding) {
795 if ((!newf) ^ (!old))
796 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
797 NETCONFA_IFINDEX_DEFAULT,
798 net->ipv6.devconf_dflt);
803 if (p == &net->ipv6.devconf_all->forwarding) {
804 int old_dflt = net->ipv6.devconf_dflt->forwarding;
806 net->ipv6.devconf_dflt->forwarding = newf;
807 if ((!newf) ^ (!old_dflt))
808 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
809 NETCONFA_IFINDEX_DEFAULT,
810 net->ipv6.devconf_dflt);
812 addrconf_forward_change(net, newf);
813 if ((!newf) ^ (!old))
814 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
815 NETCONFA_IFINDEX_ALL,
816 net->ipv6.devconf_all);
817 } else if ((!newf) ^ (!old))
818 dev_forward_change((struct inet6_dev *)table->extra1);
822 rt6_purge_dflt_routers(net);
826 static void addrconf_linkdown_change(struct net *net, __s32 newf)
828 struct net_device *dev;
829 struct inet6_dev *idev;
831 for_each_netdev(net, dev) {
832 idev = __in6_dev_get(dev);
834 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
836 idev->cnf.ignore_routes_with_linkdown = newf;
838 inet6_netconf_notify_devconf(dev_net(dev),
839 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
846 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
852 return restart_syscall();
854 net = (struct net *)table->extra2;
858 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
859 if ((!newf) ^ (!old))
860 inet6_netconf_notify_devconf(net,
861 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
862 NETCONFA_IFINDEX_DEFAULT,
863 net->ipv6.devconf_dflt);
868 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
869 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
870 addrconf_linkdown_change(net, newf);
871 if ((!newf) ^ (!old))
872 inet6_netconf_notify_devconf(net,
873 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
874 NETCONFA_IFINDEX_ALL,
875 net->ipv6.devconf_all);
884 /* Nobody refers to this ifaddr, destroy it */
885 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
887 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
889 #ifdef NET_REFCNT_DEBUG
890 pr_debug("%s\n", __func__);
893 in6_dev_put(ifp->idev);
895 if (cancel_delayed_work(&ifp->dad_work))
896 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
899 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
900 pr_warn("Freeing alive inet6 address %p\n", ifp);
909 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
912 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
915 * Each device address list is sorted in order of scope -
916 * global before linklocal.
918 list_for_each(p, &idev->addr_list) {
919 struct inet6_ifaddr *ifa
920 = list_entry(p, struct inet6_ifaddr, if_list);
921 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
925 list_add_tail(&ifp->if_list, p);
928 static u32 inet6_addr_hash(const struct in6_addr *addr)
930 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
933 /* On success it returns ifp with increased reference count */
935 static struct inet6_ifaddr *
936 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
937 const struct in6_addr *peer_addr, int pfxlen,
938 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
940 struct inet6_ifaddr *ifa = NULL;
944 int addr_type = ipv6_addr_type(addr);
946 if (addr_type == IPV6_ADDR_ANY ||
947 addr_type & IPV6_ADDR_MULTICAST ||
948 (!(idev->dev->flags & IFF_LOOPBACK) &&
949 addr_type & IPV6_ADDR_LOOPBACK))
950 return ERR_PTR(-EADDRNOTAVAIL);
954 err = -ENODEV; /*XXX*/
958 if (idev->cnf.disable_ipv6) {
963 spin_lock(&addrconf_hash_lock);
965 /* Ignore adding duplicate addresses on an interface */
966 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
967 ADBG("ipv6_add_addr: already assigned\n");
972 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
975 ADBG("ipv6_add_addr: malloc failed\n");
980 rt = addrconf_dst_alloc(idev, addr, false);
986 neigh_parms_data_state_setall(idev->nd_parms);
990 ifa->peer_addr = *peer_addr;
992 spin_lock_init(&ifa->lock);
993 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
994 INIT_HLIST_NODE(&ifa->addr_lst);
996 ifa->prefix_len = pfxlen;
997 ifa->flags = flags | IFA_F_TENTATIVE;
998 ifa->valid_lft = valid_lft;
999 ifa->prefered_lft = prefered_lft;
1000 ifa->cstamp = ifa->tstamp = jiffies;
1001 ifa->tokenized = false;
1010 /* Add to big hash table */
1011 hash = inet6_addr_hash(addr);
1013 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1014 spin_unlock(&addrconf_hash_lock);
1016 write_lock(&idev->lock);
1017 /* Add to inet6_dev unicast addr list. */
1018 ipv6_link_dev_addr(idev, ifa);
1020 if (ifa->flags&IFA_F_TEMPORARY) {
1021 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1026 write_unlock(&idev->lock);
1028 rcu_read_unlock_bh();
1030 if (likely(err == 0))
1031 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1039 spin_unlock(&addrconf_hash_lock);
1043 enum cleanup_prefix_rt_t {
1044 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1045 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1046 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1050 * Check, whether the prefix for ifp would still need a prefix route
1051 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1054 * 1) we don't purge prefix if address was not permanent.
1055 * prefix is managed by its own lifetime.
1056 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1057 * 3) if there are no addresses, delete prefix.
1058 * 4) if there are still other permanent address(es),
1059 * corresponding prefix is still permanent.
1060 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1061 * don't purge the prefix, assume user space is managing it.
1062 * 6) otherwise, update prefix lifetime to the
1063 * longest valid lifetime among the corresponding
1064 * addresses on the device.
1065 * Note: subsequent RA will update lifetime.
1067 static enum cleanup_prefix_rt_t
1068 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1070 struct inet6_ifaddr *ifa;
1071 struct inet6_dev *idev = ifp->idev;
1072 unsigned long lifetime;
1073 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1077 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1080 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1083 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1084 return CLEANUP_PREFIX_RT_NOP;
1086 action = CLEANUP_PREFIX_RT_EXPIRE;
1088 spin_lock(&ifa->lock);
1090 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1092 * Note: Because this address is
1093 * not permanent, lifetime <
1094 * LONG_MAX / HZ here.
1096 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1097 *expires = ifa->tstamp + lifetime * HZ;
1098 spin_unlock(&ifa->lock);
1105 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1107 struct rt6_info *rt;
1109 rt = addrconf_get_prefix_route(&ifp->addr,
1112 0, RTF_GATEWAY | RTF_DEFAULT);
1117 if (!(rt->rt6i_flags & RTF_EXPIRES))
1118 rt6_set_expires(rt, expires);
1125 /* This function wants to get referenced ifp and releases it before return */
1127 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1130 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1131 unsigned long expires;
1135 spin_lock_bh(&ifp->lock);
1137 ifp->state = INET6_IFADDR_STATE_DEAD;
1138 spin_unlock_bh(&ifp->lock);
1140 if (state == INET6_IFADDR_STATE_DEAD)
1143 spin_lock_bh(&addrconf_hash_lock);
1144 hlist_del_init_rcu(&ifp->addr_lst);
1145 spin_unlock_bh(&addrconf_hash_lock);
1147 write_lock_bh(&ifp->idev->lock);
1149 if (ifp->flags&IFA_F_TEMPORARY) {
1150 list_del(&ifp->tmp_list);
1152 in6_ifa_put(ifp->ifpub);
1158 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1159 action = check_cleanup_prefix_route(ifp, &expires);
1161 list_del_init(&ifp->if_list);
1164 write_unlock_bh(&ifp->idev->lock);
1166 addrconf_del_dad_work(ifp);
1168 ipv6_ifa_notify(RTM_DELADDR, ifp);
1170 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1172 if (action != CLEANUP_PREFIX_RT_NOP) {
1173 cleanup_prefix_route(ifp, expires,
1174 action == CLEANUP_PREFIX_RT_DEL);
1177 /* clean up prefsrc entries */
1178 rt6_remove_prefsrc(ifp);
1183 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1185 struct inet6_dev *idev = ifp->idev;
1186 struct in6_addr addr, *tmpaddr;
1187 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1188 unsigned long regen_advance;
1192 unsigned long now = jiffies;
1194 write_lock_bh(&idev->lock);
1196 spin_lock_bh(&ift->lock);
1197 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1198 spin_unlock_bh(&ift->lock);
1205 if (idev->cnf.use_tempaddr <= 0) {
1206 write_unlock_bh(&idev->lock);
1207 pr_info("%s: use_tempaddr is disabled\n", __func__);
1212 spin_lock_bh(&ifp->lock);
1213 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1214 idev->cnf.use_tempaddr = -1; /*XXX*/
1215 spin_unlock_bh(&ifp->lock);
1216 write_unlock_bh(&idev->lock);
1217 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1224 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1225 __ipv6_try_regen_rndid(idev, tmpaddr);
1226 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1227 age = (now - ifp->tstamp) / HZ;
1228 tmp_valid_lft = min_t(__u32,
1230 idev->cnf.temp_valid_lft + age);
1231 tmp_prefered_lft = min_t(__u32,
1233 idev->cnf.temp_prefered_lft + age -
1234 idev->cnf.max_desync_factor);
1235 tmp_plen = ifp->prefix_len;
1236 tmp_tstamp = ifp->tstamp;
1237 spin_unlock_bh(&ifp->lock);
1239 regen_advance = idev->cnf.regen_max_retry *
1240 idev->cnf.dad_transmits *
1241 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1242 write_unlock_bh(&idev->lock);
1244 /* A temporary address is created only if this calculated Preferred
1245 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1246 * an implementation must not create a temporary address with a zero
1247 * Preferred Lifetime.
1248 * Use age calculation as in addrconf_verify to avoid unnecessary
1249 * temporary addresses being generated.
1251 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1252 if (tmp_prefered_lft <= regen_advance + age) {
1259 addr_flags = IFA_F_TEMPORARY;
1260 /* set in addrconf_prefix_rcv() */
1261 if (ifp->flags & IFA_F_OPTIMISTIC)
1262 addr_flags |= IFA_F_OPTIMISTIC;
1264 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1265 ipv6_addr_scope(&addr), addr_flags,
1266 tmp_valid_lft, tmp_prefered_lft);
1270 pr_info("%s: retry temporary address regeneration\n", __func__);
1272 write_lock_bh(&idev->lock);
1276 spin_lock_bh(&ift->lock);
1279 ift->tstamp = tmp_tstamp;
1280 spin_unlock_bh(&ift->lock);
1282 addrconf_dad_start(ift);
1290 * Choose an appropriate source address (RFC3484)
1293 IPV6_SADDR_RULE_INIT = 0,
1294 IPV6_SADDR_RULE_LOCAL,
1295 IPV6_SADDR_RULE_SCOPE,
1296 IPV6_SADDR_RULE_PREFERRED,
1297 #ifdef CONFIG_IPV6_MIP6
1298 IPV6_SADDR_RULE_HOA,
1300 IPV6_SADDR_RULE_OIF,
1301 IPV6_SADDR_RULE_LABEL,
1302 IPV6_SADDR_RULE_PRIVACY,
1303 IPV6_SADDR_RULE_ORCHID,
1304 IPV6_SADDR_RULE_PREFIX,
1305 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1306 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1311 struct ipv6_saddr_score {
1314 struct inet6_ifaddr *ifa;
1315 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1320 struct ipv6_saddr_dst {
1321 const struct in6_addr *addr;
1328 static inline int ipv6_saddr_preferred(int type)
1330 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1335 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1337 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1338 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1344 static int ipv6_get_saddr_eval(struct net *net,
1345 struct ipv6_saddr_score *score,
1346 struct ipv6_saddr_dst *dst,
1351 if (i <= score->rule) {
1353 case IPV6_SADDR_RULE_SCOPE:
1354 ret = score->scopedist;
1356 case IPV6_SADDR_RULE_PREFIX:
1357 ret = score->matchlen;
1360 ret = !!test_bit(i, score->scorebits);
1366 case IPV6_SADDR_RULE_INIT:
1367 /* Rule 0: remember if hiscore is not ready yet */
1370 case IPV6_SADDR_RULE_LOCAL:
1371 /* Rule 1: Prefer same address */
1372 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1374 case IPV6_SADDR_RULE_SCOPE:
1375 /* Rule 2: Prefer appropriate scope
1380 * ---+--+-+---> scope
1382 * | d is scope of the destination.
1384 * | \ <- smaller scope is better if
1385 * B-15 | \ if scope is enough for destination.
1386 * | ret = B - scope (-1 <= scope >= d <= 15).
1388 * |/ <- greater is better
1389 * -C / if scope is not enough for destination.
1390 * /| ret = scope - C (-1 <= d < scope <= 15).
1392 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1393 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1394 * Assume B = 0 and we get C > 29.
1396 ret = __ipv6_addr_src_scope(score->addr_type);
1397 if (ret >= dst->scope)
1400 ret -= 128; /* 30 is enough */
1401 score->scopedist = ret;
1403 case IPV6_SADDR_RULE_PREFERRED:
1405 /* Rule 3: Avoid deprecated and optimistic addresses */
1406 u8 avoid = IFA_F_DEPRECATED;
1408 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1409 avoid |= IFA_F_OPTIMISTIC;
1410 ret = ipv6_saddr_preferred(score->addr_type) ||
1411 !(score->ifa->flags & avoid);
1414 #ifdef CONFIG_IPV6_MIP6
1415 case IPV6_SADDR_RULE_HOA:
1417 /* Rule 4: Prefer home address */
1418 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1419 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1423 case IPV6_SADDR_RULE_OIF:
1424 /* Rule 5: Prefer outgoing interface */
1425 ret = (!dst->ifindex ||
1426 dst->ifindex == score->ifa->idev->dev->ifindex);
1428 case IPV6_SADDR_RULE_LABEL:
1429 /* Rule 6: Prefer matching label */
1430 ret = ipv6_addr_label(net,
1431 &score->ifa->addr, score->addr_type,
1432 score->ifa->idev->dev->ifindex) == dst->label;
1434 case IPV6_SADDR_RULE_PRIVACY:
1436 /* Rule 7: Prefer public address
1437 * Note: prefer temporary address if use_tempaddr >= 2
1439 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1440 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1441 score->ifa->idev->cnf.use_tempaddr >= 2;
1442 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1445 case IPV6_SADDR_RULE_ORCHID:
1446 /* Rule 8-: Prefer ORCHID vs ORCHID or
1447 * non-ORCHID vs non-ORCHID
1449 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1450 ipv6_addr_orchid(dst->addr));
1452 case IPV6_SADDR_RULE_PREFIX:
1453 /* Rule 8: Use longest matching prefix */
1454 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1455 if (ret > score->ifa->prefix_len)
1456 ret = score->ifa->prefix_len;
1457 score->matchlen = ret;
1459 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1460 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1461 /* Optimistic addresses still have lower precedence than other
1462 * preferred addresses.
1464 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1472 __set_bit(i, score->scorebits);
1478 static int __ipv6_dev_get_saddr(struct net *net,
1479 struct ipv6_saddr_dst *dst,
1480 struct inet6_dev *idev,
1481 struct ipv6_saddr_score *scores,
1484 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1486 read_lock_bh(&idev->lock);
1487 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1491 * - Tentative Address (RFC2462 section 5.4)
1492 * - A tentative address is not considered
1493 * "assigned to an interface" in the traditional
1494 * sense, unless it is also flagged as optimistic.
1495 * - Candidate Source Address (section 4)
1496 * - In any case, anycast addresses, multicast
1497 * addresses, and the unspecified address MUST
1498 * NOT be included in a candidate set.
1500 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1501 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1504 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1506 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1507 score->addr_type & IPV6_ADDR_MULTICAST)) {
1508 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1514 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1516 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1517 int minihiscore, miniscore;
1519 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1520 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1522 if (minihiscore > miniscore) {
1523 if (i == IPV6_SADDR_RULE_SCOPE &&
1524 score->scopedist > 0) {
1527 * each remaining entry
1528 * has too small (not enough)
1529 * scope, because ifa entries
1530 * are sorted by their scope
1536 } else if (minihiscore < miniscore) {
1538 in6_ifa_put(hiscore->ifa);
1540 in6_ifa_hold(score->ifa);
1542 swap(hiscore, score);
1543 hiscore_idx = 1 - hiscore_idx;
1545 /* restore our iterator */
1546 score->ifa = hiscore->ifa;
1553 read_unlock_bh(&idev->lock);
1557 static int ipv6_get_saddr_master(struct net *net,
1558 const struct net_device *dst_dev,
1559 const struct net_device *master,
1560 struct ipv6_saddr_dst *dst,
1561 struct ipv6_saddr_score *scores,
1564 struct inet6_dev *idev;
1566 idev = __in6_dev_get(dst_dev);
1568 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1569 scores, hiscore_idx);
1571 idev = __in6_dev_get(master);
1573 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1574 scores, hiscore_idx);
1579 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1580 const struct in6_addr *daddr, unsigned int prefs,
1581 struct in6_addr *saddr)
1583 struct ipv6_saddr_score scores[2], *hiscore;
1584 struct ipv6_saddr_dst dst;
1585 struct inet6_dev *idev;
1586 struct net_device *dev;
1588 bool use_oif_addr = false;
1589 int hiscore_idx = 0;
1591 dst_type = __ipv6_addr_type(daddr);
1593 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1594 dst.scope = __ipv6_addr_src_scope(dst_type);
1595 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1598 scores[hiscore_idx].rule = -1;
1599 scores[hiscore_idx].ifa = NULL;
1603 /* Candidate Source Address (section 4)
1604 * - multicast and link-local destination address,
1605 * the set of candidate source address MUST only
1606 * include addresses assigned to interfaces
1607 * belonging to the same link as the outgoing
1609 * (- For site-local destination addresses, the
1610 * set of candidate source addresses MUST only
1611 * include addresses assigned to interfaces
1612 * belonging to the same site as the outgoing
1614 * - "It is RECOMMENDED that the candidate source addresses
1615 * be the set of unicast addresses assigned to the
1616 * interface that will be used to send to the destination
1617 * (the 'outgoing' interface)." (RFC 6724)
1620 idev = __in6_dev_get(dst_dev);
1621 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1622 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1623 (idev && idev->cnf.use_oif_addrs_only)) {
1624 use_oif_addr = true;
1630 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1632 const struct net_device *master;
1635 /* if dst_dev exists and is enslaved to an L3 device, then
1636 * prefer addresses from dst_dev and then the master over
1637 * any other enslaved devices in the L3 domain.
1639 master = l3mdev_master_dev_rcu(dst_dev);
1641 master_idx = master->ifindex;
1643 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1645 scores, hiscore_idx);
1647 if (scores[hiscore_idx].ifa)
1651 for_each_netdev_rcu(net, dev) {
1652 /* only consider addresses on devices in the
1655 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1657 idev = __in6_dev_get(dev);
1660 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1667 hiscore = &scores[hiscore_idx];
1669 return -EADDRNOTAVAIL;
1671 *saddr = hiscore->ifa->addr;
1672 in6_ifa_put(hiscore->ifa);
1675 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1677 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1680 struct inet6_ifaddr *ifp;
1681 int err = -EADDRNOTAVAIL;
1683 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1684 if (ifp->scope > IFA_LINK)
1686 if (ifp->scope == IFA_LINK &&
1687 !(ifp->flags & banned_flags)) {
1696 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1699 struct inet6_dev *idev;
1700 int err = -EADDRNOTAVAIL;
1703 idev = __in6_dev_get(dev);
1705 read_lock_bh(&idev->lock);
1706 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1707 read_unlock_bh(&idev->lock);
1713 static int ipv6_count_addresses(struct inet6_dev *idev)
1716 struct inet6_ifaddr *ifp;
1718 read_lock_bh(&idev->lock);
1719 list_for_each_entry(ifp, &idev->addr_list, if_list)
1721 read_unlock_bh(&idev->lock);
1725 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1726 const struct net_device *dev, int strict)
1728 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1730 EXPORT_SYMBOL(ipv6_chk_addr);
1732 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1733 const struct net_device *dev, int strict,
1736 struct inet6_ifaddr *ifp;
1737 unsigned int hash = inet6_addr_hash(addr);
1741 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1742 if (!net_eq(dev_net(ifp->idev->dev), net))
1744 /* Decouple optimistic from tentative for evaluation here.
1745 * Ban optimistic addresses explicitly, when required.
1747 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1748 ? (ifp->flags&~IFA_F_TENTATIVE)
1750 if (ipv6_addr_equal(&ifp->addr, addr) &&
1751 !(ifp_flags&banned_flags) &&
1752 (!dev || ifp->idev->dev == dev ||
1753 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1754 rcu_read_unlock_bh();
1759 rcu_read_unlock_bh();
1762 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1764 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1765 struct net_device *dev)
1767 unsigned int hash = inet6_addr_hash(addr);
1768 struct inet6_ifaddr *ifp;
1770 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1771 if (!net_eq(dev_net(ifp->idev->dev), net))
1773 if (ipv6_addr_equal(&ifp->addr, addr)) {
1774 if (!dev || ifp->idev->dev == dev)
1781 /* Compares an address/prefix_len with addresses on device @dev.
1782 * If one is found it returns true.
1784 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1785 const unsigned int prefix_len, struct net_device *dev)
1787 struct inet6_dev *idev;
1788 struct inet6_ifaddr *ifa;
1792 idev = __in6_dev_get(dev);
1794 read_lock_bh(&idev->lock);
1795 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1796 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1800 read_unlock_bh(&idev->lock);
1806 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1808 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1810 struct inet6_dev *idev;
1811 struct inet6_ifaddr *ifa;
1816 idev = __in6_dev_get(dev);
1818 read_lock_bh(&idev->lock);
1819 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1820 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1825 read_unlock_bh(&idev->lock);
1830 EXPORT_SYMBOL(ipv6_chk_prefix);
1832 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1833 struct net_device *dev, int strict)
1835 struct inet6_ifaddr *ifp, *result = NULL;
1836 unsigned int hash = inet6_addr_hash(addr);
1839 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1840 if (!net_eq(dev_net(ifp->idev->dev), net))
1842 if (ipv6_addr_equal(&ifp->addr, addr)) {
1843 if (!dev || ifp->idev->dev == dev ||
1844 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1851 rcu_read_unlock_bh();
1856 /* Gets referenced address, destroys ifaddr */
1858 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1861 ifp->flags |= IFA_F_DADFAILED;
1863 if (ifp->flags&IFA_F_PERMANENT) {
1864 spin_lock_bh(&ifp->lock);
1865 addrconf_del_dad_work(ifp);
1866 ifp->flags |= IFA_F_TENTATIVE;
1867 spin_unlock_bh(&ifp->lock);
1869 ipv6_ifa_notify(0, ifp);
1871 } else if (ifp->flags&IFA_F_TEMPORARY) {
1872 struct inet6_ifaddr *ifpub;
1873 spin_lock_bh(&ifp->lock);
1876 in6_ifa_hold(ifpub);
1877 spin_unlock_bh(&ifp->lock);
1878 ipv6_create_tempaddr(ifpub, ifp);
1881 spin_unlock_bh(&ifp->lock);
1889 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1893 spin_lock_bh(&ifp->lock);
1894 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1895 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1898 spin_unlock_bh(&ifp->lock);
1903 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1905 struct inet6_dev *idev = ifp->idev;
1906 struct net *net = dev_net(ifp->idev->dev);
1908 if (addrconf_dad_end(ifp)) {
1913 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1914 ifp->idev->dev->name, &ifp->addr);
1916 spin_lock_bh(&ifp->lock);
1918 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1919 int scope = ifp->scope;
1920 u32 flags = ifp->flags;
1921 struct in6_addr new_addr;
1922 struct inet6_ifaddr *ifp2;
1923 u32 valid_lft, preferred_lft;
1924 int pfxlen = ifp->prefix_len;
1925 int retries = ifp->stable_privacy_retry + 1;
1927 if (retries > net->ipv6.sysctl.idgen_retries) {
1928 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1929 ifp->idev->dev->name);
1933 new_addr = ifp->addr;
1934 if (ipv6_generate_stable_address(&new_addr, retries,
1938 valid_lft = ifp->valid_lft;
1939 preferred_lft = ifp->prefered_lft;
1941 spin_unlock_bh(&ifp->lock);
1943 if (idev->cnf.max_addresses &&
1944 ipv6_count_addresses(idev) >=
1945 idev->cnf.max_addresses)
1948 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1949 ifp->idev->dev->name);
1951 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1952 scope, flags, valid_lft,
1957 spin_lock_bh(&ifp2->lock);
1958 ifp2->stable_privacy_retry = retries;
1959 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1960 spin_unlock_bh(&ifp2->lock);
1962 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1965 spin_lock_bh(&ifp->lock);
1969 /* transition from _POSTDAD to _ERRDAD */
1970 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1971 spin_unlock_bh(&ifp->lock);
1973 addrconf_mod_dad_work(ifp, 0);
1977 /* Join to solicited addr multicast group.
1978 * caller must hold RTNL */
1979 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1981 struct in6_addr maddr;
1983 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1986 addrconf_addr_solict_mult(addr, &maddr);
1987 ipv6_dev_mc_inc(dev, &maddr);
1990 /* caller must hold RTNL */
1991 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1993 struct in6_addr maddr;
1995 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1998 addrconf_addr_solict_mult(addr, &maddr);
1999 __ipv6_dev_mc_dec(idev, &maddr);
2002 /* caller must hold RTNL */
2003 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2005 struct in6_addr addr;
2007 if (ifp->prefix_len >= 127) /* RFC 6164 */
2009 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2010 if (ipv6_addr_any(&addr))
2012 __ipv6_dev_ac_inc(ifp->idev, &addr);
2015 /* caller must hold RTNL */
2016 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2018 struct in6_addr addr;
2020 if (ifp->prefix_len >= 127) /* RFC 6164 */
2022 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2023 if (ipv6_addr_any(&addr))
2025 __ipv6_dev_ac_dec(ifp->idev, &addr);
2028 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2030 if (dev->addr_len != EUI64_ADDR_LEN)
2032 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2037 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2039 union fwnet_hwaddr *ha;
2041 if (dev->addr_len != FWNET_ALEN)
2044 ha = (union fwnet_hwaddr *)dev->dev_addr;
2046 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2051 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2053 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2054 if (dev->addr_len != ARCNET_ALEN)
2057 eui[7] = *(u8 *)dev->dev_addr;
2061 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2063 if (dev->addr_len != INFINIBAND_ALEN)
2065 memcpy(eui, dev->dev_addr + 12, 8);
2070 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2074 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2075 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2076 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2077 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2078 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2079 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2083 memcpy(eui + 4, &addr, 4);
2087 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2089 if (dev->priv_flags & IFF_ISATAP)
2090 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2094 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2096 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2099 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2101 memcpy(eui, dev->perm_addr, 3);
2102 memcpy(eui + 5, dev->perm_addr + 3, 3);
2109 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2111 switch (dev->type) {
2114 return addrconf_ifid_eui48(eui, dev);
2116 return addrconf_ifid_arcnet(eui, dev);
2117 case ARPHRD_INFINIBAND:
2118 return addrconf_ifid_infiniband(eui, dev);
2120 return addrconf_ifid_sit(eui, dev);
2122 return addrconf_ifid_gre(eui, dev);
2123 case ARPHRD_6LOWPAN:
2124 return addrconf_ifid_eui64(eui, dev);
2125 case ARPHRD_IEEE1394:
2126 return addrconf_ifid_ieee1394(eui, dev);
2127 case ARPHRD_TUNNEL6:
2128 return addrconf_ifid_ip6tnl(eui, dev);
2133 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2136 struct inet6_ifaddr *ifp;
2138 read_lock_bh(&idev->lock);
2139 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2140 if (ifp->scope > IFA_LINK)
2142 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2143 memcpy(eui, ifp->addr.s6_addr+8, 8);
2148 read_unlock_bh(&idev->lock);
2152 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2153 static void __ipv6_regen_rndid(struct inet6_dev *idev)
2156 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2157 idev->rndid[0] &= ~0x02;
2160 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2161 * check if generated address is not inappropriate
2163 * - Reserved subnet anycast (RFC 2526)
2164 * 11111101 11....11 1xxxxxxx
2165 * - ISATAP (RFC4214) 6.1
2166 * 00-00-5E-FE-xx-xx-xx-xx
2168 * - XXX: already assigned to an address on the device
2170 if (idev->rndid[0] == 0xfd &&
2171 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2172 (idev->rndid[7]&0x80))
2174 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2175 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2177 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2182 static void ipv6_regen_rndid(unsigned long data)
2184 struct inet6_dev *idev = (struct inet6_dev *) data;
2185 unsigned long expires;
2188 write_lock_bh(&idev->lock);
2193 __ipv6_regen_rndid(idev);
2196 idev->cnf.temp_prefered_lft * HZ -
2197 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2198 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2199 idev->cnf.max_desync_factor * HZ;
2200 if (time_before(expires, jiffies)) {
2201 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2202 __func__, idev->dev->name);
2206 if (!mod_timer(&idev->regen_timer, expires))
2210 write_unlock_bh(&idev->lock);
2211 rcu_read_unlock_bh();
2215 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2217 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2218 __ipv6_regen_rndid(idev);
2226 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2227 unsigned long expires, u32 flags)
2229 struct fib6_config cfg = {
2230 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2231 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2232 .fc_ifindex = dev->ifindex,
2233 .fc_expires = expires,
2235 .fc_flags = RTF_UP | flags,
2236 .fc_nlinfo.nl_net = dev_net(dev),
2237 .fc_protocol = RTPROT_KERNEL,
2242 /* Prevent useless cloning on PtP SIT.
2243 This thing is done here expecting that the whole
2244 class of non-broadcast devices need not cloning.
2246 #if IS_ENABLED(CONFIG_IPV6_SIT)
2247 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2248 cfg.fc_flags |= RTF_NONEXTHOP;
2251 ip6_route_add(&cfg);
2255 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2257 const struct net_device *dev,
2258 u32 flags, u32 noflags)
2260 struct fib6_node *fn;
2261 struct rt6_info *rt = NULL;
2262 struct fib6_table *table;
2263 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2265 table = fib6_get_table(dev_net(dev), tb_id);
2269 read_lock_bh(&table->tb6_lock);
2270 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2274 noflags |= RTF_CACHE;
2275 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2276 if (rt->dst.dev->ifindex != dev->ifindex)
2278 if ((rt->rt6i_flags & flags) != flags)
2280 if ((rt->rt6i_flags & noflags) != 0)
2286 read_unlock_bh(&table->tb6_lock);
2291 /* Create "default" multicast route to the interface */
2293 static void addrconf_add_mroute(struct net_device *dev)
2295 struct fib6_config cfg = {
2296 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2297 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2298 .fc_ifindex = dev->ifindex,
2301 .fc_nlinfo.nl_net = dev_net(dev),
2304 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2306 ip6_route_add(&cfg);
2309 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2311 struct inet6_dev *idev;
2315 idev = ipv6_find_idev(dev);
2317 return ERR_PTR(-ENOBUFS);
2319 if (idev->cnf.disable_ipv6)
2320 return ERR_PTR(-EACCES);
2322 /* Add default multicast route */
2323 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2324 addrconf_add_mroute(dev);
2329 static void manage_tempaddrs(struct inet6_dev *idev,
2330 struct inet6_ifaddr *ifp,
2331 __u32 valid_lft, __u32 prefered_lft,
2332 bool create, unsigned long now)
2335 struct inet6_ifaddr *ift;
2337 read_lock_bh(&idev->lock);
2338 /* update all temporary addresses in the list */
2339 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2340 int age, max_valid, max_prefered;
2342 if (ifp != ift->ifpub)
2345 /* RFC 4941 section 3.3:
2346 * If a received option will extend the lifetime of a public
2347 * address, the lifetimes of temporary addresses should
2348 * be extended, subject to the overall constraint that no
2349 * temporary addresses should ever remain "valid" or "preferred"
2350 * for a time longer than (TEMP_VALID_LIFETIME) or
2351 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2353 age = (now - ift->cstamp) / HZ;
2354 max_valid = idev->cnf.temp_valid_lft - age;
2358 max_prefered = idev->cnf.temp_prefered_lft -
2359 idev->cnf.max_desync_factor - age;
2360 if (max_prefered < 0)
2363 if (valid_lft > max_valid)
2364 valid_lft = max_valid;
2366 if (prefered_lft > max_prefered)
2367 prefered_lft = max_prefered;
2369 spin_lock(&ift->lock);
2371 ift->valid_lft = valid_lft;
2372 ift->prefered_lft = prefered_lft;
2374 if (prefered_lft > 0)
2375 ift->flags &= ~IFA_F_DEPRECATED;
2377 spin_unlock(&ift->lock);
2378 if (!(flags&IFA_F_TENTATIVE))
2379 ipv6_ifa_notify(0, ift);
2382 if ((create || list_empty(&idev->tempaddr_list)) &&
2383 idev->cnf.use_tempaddr > 0) {
2384 /* When a new public address is created as described
2385 * in [ADDRCONF], also create a new temporary address.
2386 * Also create a temporary address if it's enabled but
2387 * no temporary address currently exists.
2389 read_unlock_bh(&idev->lock);
2390 ipv6_create_tempaddr(ifp, NULL);
2392 read_unlock_bh(&idev->lock);
2396 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2398 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2399 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2402 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2403 const struct prefix_info *pinfo,
2404 struct inet6_dev *in6_dev,
2405 const struct in6_addr *addr, int addr_type,
2406 u32 addr_flags, bool sllao, bool tokenized,
2407 __u32 valid_lft, u32 prefered_lft)
2409 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2410 int create = 0, update_lft = 0;
2412 if (!ifp && valid_lft) {
2413 int max_addresses = in6_dev->cnf.max_addresses;
2415 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2416 if (in6_dev->cnf.optimistic_dad &&
2417 !net->ipv6.devconf_all->forwarding && sllao)
2418 addr_flags |= IFA_F_OPTIMISTIC;
2421 /* Do not allow to create too much of autoconfigured
2422 * addresses; this would be too easy way to crash kernel.
2424 if (!max_addresses ||
2425 ipv6_count_addresses(in6_dev) < max_addresses)
2426 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2428 addr_type&IPV6_ADDR_SCOPE_MASK,
2429 addr_flags, valid_lft,
2432 if (IS_ERR_OR_NULL(ifp))
2437 spin_lock_bh(&ifp->lock);
2438 ifp->flags |= IFA_F_MANAGETEMPADDR;
2439 ifp->cstamp = jiffies;
2440 ifp->tokenized = tokenized;
2441 spin_unlock_bh(&ifp->lock);
2442 addrconf_dad_start(ifp);
2450 /* update lifetime (RFC2462 5.5.3 e) */
2451 spin_lock_bh(&ifp->lock);
2453 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2454 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2457 if (!update_lft && !create && stored_lft) {
2458 const u32 minimum_lft = min_t(u32,
2459 stored_lft, MIN_VALID_LIFETIME);
2460 valid_lft = max(valid_lft, minimum_lft);
2462 /* RFC4862 Section 5.5.3e:
2463 * "Note that the preferred lifetime of the
2464 * corresponding address is always reset to
2465 * the Preferred Lifetime in the received
2466 * Prefix Information option, regardless of
2467 * whether the valid lifetime is also reset or
2470 * So we should always update prefered_lft here.
2476 ifp->valid_lft = valid_lft;
2477 ifp->prefered_lft = prefered_lft;
2480 ifp->flags &= ~IFA_F_DEPRECATED;
2481 spin_unlock_bh(&ifp->lock);
2483 if (!(flags&IFA_F_TENTATIVE))
2484 ipv6_ifa_notify(0, ifp);
2486 spin_unlock_bh(&ifp->lock);
2488 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2497 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2499 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2501 struct prefix_info *pinfo;
2506 struct inet6_dev *in6_dev;
2507 struct net *net = dev_net(dev);
2509 pinfo = (struct prefix_info *) opt;
2511 if (len < sizeof(struct prefix_info)) {
2512 ADBG("addrconf: prefix option too short\n");
2517 * Validation checks ([ADDRCONF], page 19)
2520 addr_type = ipv6_addr_type(&pinfo->prefix);
2522 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2525 valid_lft = ntohl(pinfo->valid);
2526 prefered_lft = ntohl(pinfo->prefered);
2528 if (prefered_lft > valid_lft) {
2529 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2533 in6_dev = in6_dev_get(dev);
2536 net_dbg_ratelimited("addrconf: device %s not configured\n",
2542 * Two things going on here:
2543 * 1) Add routes for on-link prefixes
2544 * 2) Configure prefixes with the auto flag set
2547 if (pinfo->onlink) {
2548 struct rt6_info *rt;
2549 unsigned long rt_expires;
2551 /* Avoid arithmetic overflow. Really, we could
2552 * save rt_expires in seconds, likely valid_lft,
2553 * but it would require division in fib gc, that it
2557 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2559 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2561 if (addrconf_finite_timeout(rt_expires))
2564 rt = addrconf_get_prefix_route(&pinfo->prefix,
2567 RTF_ADDRCONF | RTF_PREFIX_RT,
2568 RTF_GATEWAY | RTF_DEFAULT);
2571 /* Autoconf prefix route */
2572 if (valid_lft == 0) {
2575 } else if (addrconf_finite_timeout(rt_expires)) {
2577 rt6_set_expires(rt, jiffies + rt_expires);
2579 rt6_clean_expires(rt);
2581 } else if (valid_lft) {
2582 clock_t expires = 0;
2583 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2584 if (addrconf_finite_timeout(rt_expires)) {
2586 flags |= RTF_EXPIRES;
2587 expires = jiffies_to_clock_t(rt_expires);
2589 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2590 dev, expires, flags);
2595 /* Try to figure out our local address for this prefix */
2597 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2598 struct in6_addr addr;
2599 bool tokenized = false, dev_addr_generated = false;
2601 if (pinfo->prefix_len == 64) {
2602 memcpy(&addr, &pinfo->prefix, 8);
2604 if (!ipv6_addr_any(&in6_dev->token)) {
2605 read_lock_bh(&in6_dev->lock);
2606 memcpy(addr.s6_addr + 8,
2607 in6_dev->token.s6_addr + 8, 8);
2608 read_unlock_bh(&in6_dev->lock);
2610 } else if (is_addr_mode_generate_stable(in6_dev) &&
2611 !ipv6_generate_stable_address(&addr, 0,
2613 addr_flags |= IFA_F_STABLE_PRIVACY;
2615 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2616 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2619 dev_addr_generated = true;
2623 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2628 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2631 tokenized, valid_lft,
2636 /* Ignore error case here because previous prefix add addr was
2637 * successful which will be notified.
2639 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2640 addr_type, addr_flags, sllao,
2641 tokenized, valid_lft,
2643 dev_addr_generated);
2645 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2647 in6_dev_put(in6_dev);
2651 * Set destination address.
2652 * Special case for SIT interfaces where we create a new "virtual"
2655 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2657 struct in6_ifreq ireq;
2658 struct net_device *dev;
2664 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2667 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2673 #if IS_ENABLED(CONFIG_IPV6_SIT)
2674 if (dev->type == ARPHRD_SIT) {
2675 const struct net_device_ops *ops = dev->netdev_ops;
2677 struct ip_tunnel_parm p;
2679 err = -EADDRNOTAVAIL;
2680 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2683 memset(&p, 0, sizeof(p));
2684 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2688 p.iph.protocol = IPPROTO_IPV6;
2690 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2692 if (ops->ndo_do_ioctl) {
2693 mm_segment_t oldfs = get_fs();
2696 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2703 dev = __dev_get_by_name(net, p.name);
2706 err = dev_open(dev);
2716 static int ipv6_mc_config(struct sock *sk, bool join,
2717 const struct in6_addr *addr, int ifindex)
2725 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2727 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2734 * Manual configuration of address on an interface
2736 static int inet6_addr_add(struct net *net, int ifindex,
2737 const struct in6_addr *pfx,
2738 const struct in6_addr *peer_pfx,
2739 unsigned int plen, __u32 ifa_flags,
2740 __u32 prefered_lft, __u32 valid_lft)
2742 struct inet6_ifaddr *ifp;
2743 struct inet6_dev *idev;
2744 struct net_device *dev;
2745 unsigned long timeout;
2755 /* check the lifetime */
2756 if (!valid_lft || prefered_lft > valid_lft)
2759 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2762 dev = __dev_get_by_index(net, ifindex);
2766 idev = addrconf_add_dev(dev);
2768 return PTR_ERR(idev);
2770 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2771 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2772 true, pfx, ifindex);
2778 scope = ipv6_addr_scope(pfx);
2780 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2781 if (addrconf_finite_timeout(timeout)) {
2782 expires = jiffies_to_clock_t(timeout * HZ);
2783 valid_lft = timeout;
2784 flags = RTF_EXPIRES;
2788 ifa_flags |= IFA_F_PERMANENT;
2791 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2792 if (addrconf_finite_timeout(timeout)) {
2794 ifa_flags |= IFA_F_DEPRECATED;
2795 prefered_lft = timeout;
2798 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2799 valid_lft, prefered_lft);
2802 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2803 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2808 * Note that section 3.1 of RFC 4429 indicates
2809 * that the Optimistic flag should not be set for
2810 * manually configured addresses
2812 addrconf_dad_start(ifp);
2813 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2814 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2817 addrconf_verify_rtnl();
2819 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2820 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2821 false, pfx, ifindex);
2824 return PTR_ERR(ifp);
2827 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2828 const struct in6_addr *pfx, unsigned int plen)
2830 struct inet6_ifaddr *ifp;
2831 struct inet6_dev *idev;
2832 struct net_device *dev;
2837 dev = __dev_get_by_index(net, ifindex);
2841 idev = __in6_dev_get(dev);
2845 read_lock_bh(&idev->lock);
2846 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2847 if (ifp->prefix_len == plen &&
2848 ipv6_addr_equal(pfx, &ifp->addr)) {
2850 read_unlock_bh(&idev->lock);
2852 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2853 (ifa_flags & IFA_F_MANAGETEMPADDR))
2854 manage_tempaddrs(idev, ifp, 0, 0, false,
2857 addrconf_verify_rtnl();
2858 if (ipv6_addr_is_multicast(pfx)) {
2859 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2860 false, pfx, dev->ifindex);
2865 read_unlock_bh(&idev->lock);
2866 return -EADDRNOTAVAIL;
2870 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2872 struct in6_ifreq ireq;
2875 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2878 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2882 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2883 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2884 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2889 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2891 struct in6_ifreq ireq;
2894 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2897 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2901 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2902 ireq.ifr6_prefixlen);
2907 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2908 int plen, int scope)
2910 struct inet6_ifaddr *ifp;
2912 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2913 scope, IFA_F_PERMANENT,
2914 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2916 spin_lock_bh(&ifp->lock);
2917 ifp->flags &= ~IFA_F_TENTATIVE;
2918 spin_unlock_bh(&ifp->lock);
2919 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2924 #if IS_ENABLED(CONFIG_IPV6_SIT)
2925 static void sit_add_v4_addrs(struct inet6_dev *idev)
2927 struct in6_addr addr;
2928 struct net_device *dev;
2929 struct net *net = dev_net(idev->dev);
2935 memset(&addr, 0, sizeof(struct in6_addr));
2936 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2938 if (idev->dev->flags&IFF_POINTOPOINT) {
2939 addr.s6_addr32[0] = htonl(0xfe800000);
2943 scope = IPV6_ADDR_COMPATv4;
2945 pflags |= RTF_NONEXTHOP;
2948 if (addr.s6_addr32[3]) {
2949 add_addr(idev, &addr, plen, scope);
2950 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2954 for_each_netdev(net, dev) {
2955 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2956 if (in_dev && (dev->flags & IFF_UP)) {
2957 struct in_ifaddr *ifa;
2961 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2963 addr.s6_addr32[3] = ifa->ifa_local;
2965 if (ifa->ifa_scope == RT_SCOPE_LINK)
2967 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2968 if (idev->dev->flags&IFF_POINTOPOINT)
2973 add_addr(idev, &addr, plen, flag);
2974 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2982 static void init_loopback(struct net_device *dev)
2984 struct inet6_dev *idev;
2985 struct net_device *sp_dev;
2986 struct inet6_ifaddr *sp_ifa;
2987 struct rt6_info *sp_rt;
2993 idev = ipv6_find_idev(dev);
2995 pr_debug("%s: add_dev failed\n", __func__);
2999 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
3001 /* Add routes to other interface's IPv6 addresses */
3002 for_each_netdev(dev_net(dev), sp_dev) {
3003 if (!strcmp(sp_dev->name, dev->name))
3006 idev = __in6_dev_get(sp_dev);
3010 read_lock_bh(&idev->lock);
3011 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3013 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3017 /* This dst has been added to garbage list when
3018 * lo device down, release this obsolete dst and
3019 * reallocate a new router for ifa.
3021 if (sp_ifa->rt->dst.obsolete > 0) {
3022 ip6_rt_put(sp_ifa->rt);
3029 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
3031 /* Failure cases are ignored */
3032 if (!IS_ERR(sp_rt)) {
3037 read_unlock_bh(&idev->lock);
3041 void addrconf_add_linklocal(struct inet6_dev *idev,
3042 const struct in6_addr *addr, u32 flags)
3044 struct inet6_ifaddr *ifp;
3045 u32 addr_flags = flags | IFA_F_PERMANENT;
3047 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3048 if (idev->cnf.optimistic_dad &&
3049 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3050 addr_flags |= IFA_F_OPTIMISTIC;
3053 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3054 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
3056 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3057 addrconf_dad_start(ifp);
3061 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3063 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3065 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3068 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3069 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3072 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3073 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3079 static int ipv6_generate_stable_address(struct in6_addr *address,
3081 const struct inet6_dev *idev)
3083 static DEFINE_SPINLOCK(lock);
3084 static __u32 digest[SHA_DIGEST_WORDS];
3085 static __u32 workspace[SHA_WORKSPACE_WORDS];
3088 char __data[SHA_MESSAGE_BYTES];
3090 struct in6_addr secret;
3092 unsigned char hwaddr[MAX_ADDR_LEN];
3097 struct in6_addr secret;
3098 struct in6_addr temp;
3099 struct net *net = dev_net(idev->dev);
3101 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3103 if (idev->cnf.stable_secret.initialized)
3104 secret = idev->cnf.stable_secret.secret;
3105 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3106 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3111 spin_lock_bh(&lock);
3114 memset(&data, 0, sizeof(data));
3115 memset(workspace, 0, sizeof(workspace));
3116 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3117 data.prefix[0] = address->s6_addr32[0];
3118 data.prefix[1] = address->s6_addr32[1];
3119 data.secret = secret;
3120 data.dad_count = dad_count;
3122 sha_transform(digest, data.__data, workspace);
3125 temp.s6_addr32[2] = (__force __be32)digest[0];
3126 temp.s6_addr32[3] = (__force __be32)digest[1];
3128 spin_unlock_bh(&lock);
3130 if (ipv6_reserved_interfaceid(temp)) {
3132 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3141 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3143 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3147 s = &idev->cnf.stable_secret;
3148 get_random_bytes(&s->secret, sizeof(s->secret));
3149 s->initialized = true;
3152 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3154 struct in6_addr addr;
3156 /* no link local addresses on L3 master devices */
3157 if (netif_is_l3_master(idev->dev))
3160 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3162 switch (idev->addr_gen_mode) {
3163 case IN6_ADDR_GEN_MODE_RANDOM:
3164 ipv6_gen_mode_random_init(idev);
3166 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3167 if (!ipv6_generate_stable_address(&addr, 0, idev))
3168 addrconf_add_linklocal(idev, &addr,
3169 IFA_F_STABLE_PRIVACY);
3170 else if (prefix_route)
3171 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3173 case IN6_ADDR_GEN_MODE_EUI64:
3174 /* addrconf_add_linklocal also adds a prefix_route and we
3175 * only need to care about prefix routes if ipv6_generate_eui64
3176 * couldn't generate one.
3178 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3179 addrconf_add_linklocal(idev, &addr, 0);
3180 else if (prefix_route)
3181 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3183 case IN6_ADDR_GEN_MODE_NONE:
3185 /* will not add any link local address */
3190 static void addrconf_dev_config(struct net_device *dev)
3192 struct inet6_dev *idev;
3196 if ((dev->type != ARPHRD_ETHER) &&
3197 (dev->type != ARPHRD_FDDI) &&
3198 (dev->type != ARPHRD_ARCNET) &&
3199 (dev->type != ARPHRD_INFINIBAND) &&
3200 (dev->type != ARPHRD_IEEE1394) &&
3201 (dev->type != ARPHRD_TUNNEL6) &&
3202 (dev->type != ARPHRD_6LOWPAN) &&
3203 (dev->type != ARPHRD_NONE)) {
3204 /* Alas, we support only Ethernet autoconfiguration. */
3208 idev = addrconf_add_dev(dev);
3212 /* this device type has no EUI support */
3213 if (dev->type == ARPHRD_NONE &&
3214 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3215 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3217 addrconf_addr_gen(idev, false);
3220 #if IS_ENABLED(CONFIG_IPV6_SIT)
3221 static void addrconf_sit_config(struct net_device *dev)
3223 struct inet6_dev *idev;
3228 * Configure the tunnel with one of our IPv4
3229 * addresses... we should configure all of
3230 * our v4 addrs in the tunnel
3233 idev = ipv6_find_idev(dev);
3235 pr_debug("%s: add_dev failed\n", __func__);
3239 if (dev->priv_flags & IFF_ISATAP) {
3240 addrconf_addr_gen(idev, false);
3244 sit_add_v4_addrs(idev);
3246 if (dev->flags&IFF_POINTOPOINT)
3247 addrconf_add_mroute(dev);
3251 #if IS_ENABLED(CONFIG_NET_IPGRE)
3252 static void addrconf_gre_config(struct net_device *dev)
3254 struct inet6_dev *idev;
3258 idev = ipv6_find_idev(dev);
3260 pr_debug("%s: add_dev failed\n", __func__);
3264 addrconf_addr_gen(idev, true);
3265 if (dev->flags & IFF_POINTOPOINT)
3266 addrconf_add_mroute(dev);
3270 static int fixup_permanent_addr(struct inet6_dev *idev,
3271 struct inet6_ifaddr *ifp)
3274 struct rt6_info *rt;
3276 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3277 if (unlikely(IS_ERR(rt)))
3283 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3284 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3288 addrconf_dad_start(ifp);
3293 static void addrconf_permanent_addr(struct net_device *dev)
3295 struct inet6_ifaddr *ifp, *tmp;
3296 struct inet6_dev *idev;
3298 idev = __in6_dev_get(dev);
3302 write_lock_bh(&idev->lock);
3304 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3305 if ((ifp->flags & IFA_F_PERMANENT) &&
3306 fixup_permanent_addr(idev, ifp) < 0) {
3307 write_unlock_bh(&idev->lock);
3309 write_lock_bh(&idev->lock);
3311 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3312 idev->dev->name, &ifp->addr);
3316 write_unlock_bh(&idev->lock);
3319 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3322 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3323 struct netdev_notifier_changeupper_info *info;
3324 struct inet6_dev *idev = __in6_dev_get(dev);
3325 int run_pending = 0;
3329 case NETDEV_REGISTER:
3330 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3331 idev = ipv6_add_dev(dev);
3333 return notifier_from_errno(PTR_ERR(idev));
3337 case NETDEV_CHANGEMTU:
3338 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3339 if (dev->mtu < IPV6_MIN_MTU) {
3340 addrconf_ifdown(dev, 1);
3345 rt6_mtu_change(dev, dev->mtu);
3346 idev->cnf.mtu6 = dev->mtu;
3350 /* allocate new idev */
3351 idev = ipv6_add_dev(dev);
3355 /* device is still not ready */
3356 if (!(idev->if_flags & IF_READY))
3365 if (dev->flags & IFF_SLAVE)
3368 if (idev && idev->cnf.disable_ipv6)
3371 if (event == NETDEV_UP) {
3372 /* restore routes for permanent addresses */
3373 addrconf_permanent_addr(dev);
3375 if (!addrconf_qdisc_ok(dev)) {
3376 /* device is not ready yet. */
3377 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3382 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3383 idev = ipv6_add_dev(dev);
3385 if (!IS_ERR_OR_NULL(idev)) {
3386 idev->if_flags |= IF_READY;
3389 } else if (event == NETDEV_CHANGE) {
3390 if (!addrconf_qdisc_ok(dev)) {
3391 /* device is still not ready. */
3396 if (idev->if_flags & IF_READY)
3397 /* device is already configured. */
3399 idev->if_flags |= IF_READY;
3402 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3408 switch (dev->type) {
3409 #if IS_ENABLED(CONFIG_IPV6_SIT)
3411 addrconf_sit_config(dev);
3414 #if IS_ENABLED(CONFIG_NET_IPGRE)
3416 addrconf_gre_config(dev);
3419 case ARPHRD_LOOPBACK:
3424 addrconf_dev_config(dev);
3428 if (!IS_ERR_OR_NULL(idev)) {
3430 addrconf_dad_run(idev);
3433 * If the MTU changed during the interface down,
3434 * when the interface up, the changed MTU must be
3435 * reflected in the idev as well as routers.
3437 if (idev->cnf.mtu6 != dev->mtu &&
3438 dev->mtu >= IPV6_MIN_MTU) {
3439 rt6_mtu_change(dev, dev->mtu);
3440 idev->cnf.mtu6 = dev->mtu;
3442 idev->tstamp = jiffies;
3443 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3446 * If the changed mtu during down is lower than
3447 * IPV6_MIN_MTU stop IPv6 on this interface.
3449 if (dev->mtu < IPV6_MIN_MTU)
3450 addrconf_ifdown(dev, 1);
3455 case NETDEV_UNREGISTER:
3457 * Remove all addresses from this interface.
3459 addrconf_ifdown(dev, event != NETDEV_DOWN);
3462 case NETDEV_CHANGENAME:
3464 snmp6_unregister_dev(idev);
3465 addrconf_sysctl_unregister(idev);
3466 err = addrconf_sysctl_register(idev);
3468 return notifier_from_errno(err);
3469 err = snmp6_register_dev(idev);
3471 addrconf_sysctl_unregister(idev);
3472 return notifier_from_errno(err);
3477 case NETDEV_PRE_TYPE_CHANGE:
3478 case NETDEV_POST_TYPE_CHANGE:
3480 addrconf_type_change(dev, event);
3483 case NETDEV_CHANGEUPPER:
3486 /* flush all routes if dev is linked to or unlinked from
3487 * an L3 master device (e.g., VRF)
3489 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3490 addrconf_ifdown(dev, 0);
3497 * addrconf module should be notified of a device going up
3499 static struct notifier_block ipv6_dev_notf = {
3500 .notifier_call = addrconf_notify,
3503 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3505 struct inet6_dev *idev;
3508 idev = __in6_dev_get(dev);
3510 if (event == NETDEV_POST_TYPE_CHANGE)
3511 ipv6_mc_remap(idev);
3512 else if (event == NETDEV_PRE_TYPE_CHANGE)
3513 ipv6_mc_unmap(idev);
3516 static bool addr_is_local(const struct in6_addr *addr)
3518 return ipv6_addr_type(addr) &
3519 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3522 static int addrconf_ifdown(struct net_device *dev, int how)
3524 struct net *net = dev_net(dev);
3525 struct inet6_dev *idev;
3526 struct inet6_ifaddr *ifa, *tmp;
3527 struct list_head del_list;
3534 rt6_ifdown(net, dev);
3535 neigh_ifdown(&nd_tbl, dev);
3537 idev = __in6_dev_get(dev);
3542 * Step 1: remove reference to ipv6 device from parent device.
3548 /* protected by rtnl_lock */
3549 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3551 /* Step 1.5: remove snmp6 entry */
3552 snmp6_unregister_dev(idev);
3556 /* aggregate the system setting and interface setting */
3557 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3559 _keep_addr = idev->cnf.keep_addr_on_down;
3561 /* combine the user config with event to determine if permanent
3562 * addresses are to be removed from address hash table
3564 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3566 /* Step 2: clear hash table */
3567 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3568 struct hlist_head *h = &inet6_addr_lst[i];
3570 spin_lock_bh(&addrconf_hash_lock);
3572 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3573 if (ifa->idev == idev) {
3574 addrconf_del_dad_work(ifa);
3575 /* combined flag + permanent flag decide if
3576 * address is retained on a down event
3579 !(ifa->flags & IFA_F_PERMANENT) ||
3580 addr_is_local(&ifa->addr)) {
3581 hlist_del_init_rcu(&ifa->addr_lst);
3586 spin_unlock_bh(&addrconf_hash_lock);
3589 write_lock_bh(&idev->lock);
3591 addrconf_del_rs_timer(idev);
3593 /* Step 2: clear flags for stateless addrconf */
3595 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3597 if (how && del_timer(&idev->regen_timer))
3600 /* Step 3: clear tempaddr list */
3601 while (!list_empty(&idev->tempaddr_list)) {
3602 ifa = list_first_entry(&idev->tempaddr_list,
3603 struct inet6_ifaddr, tmp_list);
3604 list_del(&ifa->tmp_list);
3605 write_unlock_bh(&idev->lock);
3606 spin_lock_bh(&ifa->lock);
3609 in6_ifa_put(ifa->ifpub);
3612 spin_unlock_bh(&ifa->lock);
3614 write_lock_bh(&idev->lock);
3617 /* re-combine the user config with event to determine if permanent
3618 * addresses are to be removed from the interface list
3620 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3622 INIT_LIST_HEAD(&del_list);
3623 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3624 struct rt6_info *rt = NULL;
3626 addrconf_del_dad_work(ifa);
3628 write_unlock_bh(&idev->lock);
3629 spin_lock_bh(&ifa->lock);
3631 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3632 !addr_is_local(&ifa->addr)) {
3633 /* set state to skip the notifier below */
3634 state = INET6_IFADDR_STATE_DEAD;
3636 if (!(ifa->flags & IFA_F_NODAD))
3637 ifa->flags |= IFA_F_TENTATIVE;
3643 ifa->state = INET6_IFADDR_STATE_DEAD;
3645 list_move(&ifa->if_list, &del_list);
3648 spin_unlock_bh(&ifa->lock);
3653 if (state != INET6_IFADDR_STATE_DEAD) {
3654 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3655 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3657 if (idev->cnf.forwarding)
3658 addrconf_leave_anycast(ifa);
3659 addrconf_leave_solict(ifa->idev, &ifa->addr);
3662 write_lock_bh(&idev->lock);
3665 write_unlock_bh(&idev->lock);
3667 /* now clean up addresses to be removed */
3668 while (!list_empty(&del_list)) {
3669 ifa = list_first_entry(&del_list,
3670 struct inet6_ifaddr, if_list);
3671 list_del(&ifa->if_list);
3676 /* Step 5: Discard anycast and multicast list */
3678 ipv6_ac_destroy_dev(idev);
3679 ipv6_mc_destroy_dev(idev);
3684 idev->tstamp = jiffies;
3686 /* Last: Shot the device (if unregistered) */
3688 addrconf_sysctl_unregister(idev);
3689 neigh_parms_release(&nd_tbl, idev->nd_parms);
3690 neigh_ifdown(&nd_tbl, dev);
3696 static void addrconf_rs_timer(unsigned long data)
3698 struct inet6_dev *idev = (struct inet6_dev *)data;
3699 struct net_device *dev = idev->dev;
3700 struct in6_addr lladdr;
3702 write_lock(&idev->lock);
3703 if (idev->dead || !(idev->if_flags & IF_READY))
3706 if (!ipv6_accept_ra(idev))
3709 /* Announcement received after solicitation was sent */
3710 if (idev->if_flags & IF_RA_RCVD)
3713 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3714 write_unlock(&idev->lock);
3715 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3716 ndisc_send_rs(dev, &lladdr,
3717 &in6addr_linklocal_allrouters);
3721 write_lock(&idev->lock);
3722 idev->rs_interval = rfc3315_s14_backoff_update(
3723 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3724 /* The wait after the last probe can be shorter */
3725 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3726 idev->cnf.rtr_solicits) ?
3727 idev->cnf.rtr_solicit_delay :
3731 * Note: we do not support deprecated "all on-link"
3732 * assumption any longer.
3734 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3738 write_unlock(&idev->lock);
3744 * Duplicate Address Detection
3746 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3748 unsigned long rand_num;
3749 struct inet6_dev *idev = ifp->idev;
3751 if (ifp->flags & IFA_F_OPTIMISTIC)
3754 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3756 ifp->dad_probes = idev->cnf.dad_transmits;
3757 addrconf_mod_dad_work(ifp, rand_num);
3760 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3762 struct inet6_dev *idev = ifp->idev;
3763 struct net_device *dev = idev->dev;
3764 bool notify = false;
3766 addrconf_join_solict(dev, &ifp->addr);
3768 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3770 read_lock_bh(&idev->lock);
3771 spin_lock(&ifp->lock);
3772 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3775 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3776 idev->cnf.accept_dad < 1 ||
3777 !(ifp->flags&IFA_F_TENTATIVE) ||
3778 ifp->flags & IFA_F_NODAD) {
3779 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3780 spin_unlock(&ifp->lock);
3781 read_unlock_bh(&idev->lock);
3783 addrconf_dad_completed(ifp);
3787 if (!(idev->if_flags & IF_READY)) {
3788 spin_unlock(&ifp->lock);
3789 read_unlock_bh(&idev->lock);
3791 * If the device is not ready:
3792 * - keep it tentative if it is a permanent address.
3793 * - otherwise, kill it.
3796 addrconf_dad_stop(ifp, 0);
3801 * Optimistic nodes can start receiving
3804 if (ifp->flags & IFA_F_OPTIMISTIC) {
3805 ip6_ins_rt(ifp->rt);
3806 if (ipv6_use_optimistic_addr(idev)) {
3807 /* Because optimistic nodes can use this address,
3808 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3814 addrconf_dad_kick(ifp);
3816 spin_unlock(&ifp->lock);
3817 read_unlock_bh(&idev->lock);
3819 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3822 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3824 bool begin_dad = false;
3826 spin_lock_bh(&ifp->lock);
3827 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3828 ifp->state = INET6_IFADDR_STATE_PREDAD;
3831 spin_unlock_bh(&ifp->lock);
3834 addrconf_mod_dad_work(ifp, 0);
3837 static void addrconf_dad_work(struct work_struct *w)
3839 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3840 struct inet6_ifaddr,
3842 struct inet6_dev *idev = ifp->idev;
3843 struct in6_addr mcaddr;
3844 bool disable_ipv6 = false;
3850 } action = DAD_PROCESS;
3854 spin_lock_bh(&ifp->lock);
3855 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3857 ifp->state = INET6_IFADDR_STATE_DAD;
3858 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3860 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3862 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3863 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3864 struct in6_addr addr;
3866 addr.s6_addr32[0] = htonl(0xfe800000);
3867 addr.s6_addr32[1] = 0;
3869 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3870 ipv6_addr_equal(&ifp->addr, &addr)) {
3871 /* DAD failed for link-local based on MAC */
3872 idev->cnf.disable_ipv6 = 1;
3874 pr_info("%s: IPv6 being disabled!\n",
3875 ifp->idev->dev->name);
3876 disable_ipv6 = true;
3880 spin_unlock_bh(&ifp->lock);
3882 if (action == DAD_BEGIN) {
3883 addrconf_dad_begin(ifp);
3885 } else if (action == DAD_ABORT) {
3887 addrconf_dad_stop(ifp, 1);
3889 addrconf_ifdown(idev->dev, 0);
3893 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3896 write_lock_bh(&idev->lock);
3897 if (idev->dead || !(idev->if_flags & IF_READY)) {
3898 write_unlock_bh(&idev->lock);
3902 spin_lock(&ifp->lock);
3903 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3904 spin_unlock(&ifp->lock);
3905 write_unlock_bh(&idev->lock);
3909 if (ifp->dad_probes == 0) {
3911 * DAD was successful
3914 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3915 spin_unlock(&ifp->lock);
3916 write_unlock_bh(&idev->lock);
3918 addrconf_dad_completed(ifp);
3924 addrconf_mod_dad_work(ifp,
3925 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3926 spin_unlock(&ifp->lock);
3927 write_unlock_bh(&idev->lock);
3929 /* send a neighbour solicitation for our addr */
3930 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3931 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
3937 /* ifp->idev must be at least read locked */
3938 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3940 struct inet6_ifaddr *ifpiter;
3941 struct inet6_dev *idev = ifp->idev;
3943 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3944 if (ifpiter->scope > IFA_LINK)
3946 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3947 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3948 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3955 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3957 struct net_device *dev = ifp->idev->dev;
3958 struct in6_addr lladdr;
3959 bool send_rs, send_mld;
3961 addrconf_del_dad_work(ifp);
3964 * Configure the address for reception. Now it is valid.
3967 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3969 /* If added prefix is link local and we are prepared to process
3970 router advertisements, start sending router solicitations.
3973 read_lock_bh(&ifp->idev->lock);
3974 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3975 send_rs = send_mld &&
3976 ipv6_accept_ra(ifp->idev) &&
3977 ifp->idev->cnf.rtr_solicits != 0 &&
3978 (dev->flags&IFF_LOOPBACK) == 0;
3979 read_unlock_bh(&ifp->idev->lock);
3981 /* While dad is in progress mld report's source address is in6_addrany.
3982 * Resend with proper ll now.
3985 ipv6_mc_dad_complete(ifp->idev);
3989 * If a host as already performed a random delay
3990 * [...] as part of DAD [...] there is no need
3991 * to delay again before sending the first RS
3993 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3995 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3997 write_lock_bh(&ifp->idev->lock);
3998 spin_lock(&ifp->lock);
3999 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4000 ifp->idev->cnf.rtr_solicit_interval);
4001 ifp->idev->rs_probes = 1;
4002 ifp->idev->if_flags |= IF_RS_SENT;
4003 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4004 spin_unlock(&ifp->lock);
4005 write_unlock_bh(&ifp->idev->lock);
4009 static void addrconf_dad_run(struct inet6_dev *idev)
4011 struct inet6_ifaddr *ifp;
4013 read_lock_bh(&idev->lock);
4014 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4015 spin_lock(&ifp->lock);
4016 if (ifp->flags & IFA_F_TENTATIVE &&
4017 ifp->state == INET6_IFADDR_STATE_DAD)
4018 addrconf_dad_kick(ifp);
4019 spin_unlock(&ifp->lock);
4021 read_unlock_bh(&idev->lock);
4024 #ifdef CONFIG_PROC_FS
4025 struct if6_iter_state {
4026 struct seq_net_private p;
4031 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4033 struct inet6_ifaddr *ifa = NULL;
4034 struct if6_iter_state *state = seq->private;
4035 struct net *net = seq_file_net(seq);
4038 /* initial bucket if pos is 0 */
4044 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4045 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
4047 if (!net_eq(dev_net(ifa->idev->dev), net))
4049 /* sync with offset */
4050 if (p < state->offset) {
4058 /* prepare for next bucket */
4065 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4066 struct inet6_ifaddr *ifa)
4068 struct if6_iter_state *state = seq->private;
4069 struct net *net = seq_file_net(seq);
4071 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
4072 if (!net_eq(dev_net(ifa->idev->dev), net))
4078 while (++state->bucket < IN6_ADDR_HSIZE) {
4080 hlist_for_each_entry_rcu_bh(ifa,
4081 &inet6_addr_lst[state->bucket], addr_lst) {
4082 if (!net_eq(dev_net(ifa->idev->dev), net))
4092 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4096 return if6_get_first(seq, *pos);
4099 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4101 struct inet6_ifaddr *ifa;
4103 ifa = if6_get_next(seq, v);
4108 static void if6_seq_stop(struct seq_file *seq, void *v)
4111 rcu_read_unlock_bh();
4114 static int if6_seq_show(struct seq_file *seq, void *v)
4116 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4117 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4119 ifp->idev->dev->ifindex,
4123 ifp->idev->dev->name);
4127 static const struct seq_operations if6_seq_ops = {
4128 .start = if6_seq_start,
4129 .next = if6_seq_next,
4130 .show = if6_seq_show,
4131 .stop = if6_seq_stop,
4134 static int if6_seq_open(struct inode *inode, struct file *file)
4136 return seq_open_net(inode, file, &if6_seq_ops,
4137 sizeof(struct if6_iter_state));
4140 static const struct file_operations if6_fops = {
4141 .owner = THIS_MODULE,
4142 .open = if6_seq_open,
4144 .llseek = seq_lseek,
4145 .release = seq_release_net,
4148 static int __net_init if6_proc_net_init(struct net *net)
4150 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
4155 static void __net_exit if6_proc_net_exit(struct net *net)
4157 remove_proc_entry("if_inet6", net->proc_net);
4160 static struct pernet_operations if6_proc_net_ops = {
4161 .init = if6_proc_net_init,
4162 .exit = if6_proc_net_exit,
4165 int __init if6_proc_init(void)
4167 return register_pernet_subsys(&if6_proc_net_ops);
4170 void if6_proc_exit(void)
4172 unregister_pernet_subsys(&if6_proc_net_ops);
4174 #endif /* CONFIG_PROC_FS */
4176 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4177 /* Check if address is a home address configured on any interface. */
4178 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4181 struct inet6_ifaddr *ifp = NULL;
4182 unsigned int hash = inet6_addr_hash(addr);
4185 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4186 if (!net_eq(dev_net(ifp->idev->dev), net))
4188 if (ipv6_addr_equal(&ifp->addr, addr) &&
4189 (ifp->flags & IFA_F_HOMEADDRESS)) {
4194 rcu_read_unlock_bh();
4200 * Periodic address status verification
4203 static void addrconf_verify_rtnl(void)
4205 unsigned long now, next, next_sec, next_sched;
4206 struct inet6_ifaddr *ifp;
4213 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4215 cancel_delayed_work(&addr_chk_work);
4217 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4219 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4222 /* When setting preferred_lft to a value not zero or
4223 * infinity, while valid_lft is infinity
4224 * IFA_F_PERMANENT has a non-infinity life time.
4226 if ((ifp->flags & IFA_F_PERMANENT) &&
4227 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4230 spin_lock(&ifp->lock);
4231 /* We try to batch several events at once. */
4232 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4234 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4235 age >= ifp->valid_lft) {
4236 spin_unlock(&ifp->lock);
4240 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4241 spin_unlock(&ifp->lock);
4243 } else if (age >= ifp->prefered_lft) {
4244 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4247 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4249 ifp->flags |= IFA_F_DEPRECATED;
4252 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4253 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4254 next = ifp->tstamp + ifp->valid_lft * HZ;
4256 spin_unlock(&ifp->lock);
4261 ipv6_ifa_notify(0, ifp);
4265 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4266 !(ifp->flags&IFA_F_TENTATIVE)) {
4267 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4268 ifp->idev->cnf.dad_transmits *
4269 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4271 if (age >= ifp->prefered_lft - regen_advance) {
4272 struct inet6_ifaddr *ifpub = ifp->ifpub;
4273 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4274 next = ifp->tstamp + ifp->prefered_lft * HZ;
4275 if (!ifp->regen_count && ifpub) {
4278 in6_ifa_hold(ifpub);
4279 spin_unlock(&ifp->lock);
4281 spin_lock(&ifpub->lock);
4282 ifpub->regen_count = 0;
4283 spin_unlock(&ifpub->lock);
4284 ipv6_create_tempaddr(ifpub, ifp);
4289 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4290 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4291 spin_unlock(&ifp->lock);
4293 /* ifp->prefered_lft <= ifp->valid_lft */
4294 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4295 next = ifp->tstamp + ifp->prefered_lft * HZ;
4296 spin_unlock(&ifp->lock);
4301 next_sec = round_jiffies_up(next);
4304 /* If rounded timeout is accurate enough, accept it. */
4305 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4306 next_sched = next_sec;
4308 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4309 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4310 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4312 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4313 now, next, next_sec, next_sched);
4314 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4315 rcu_read_unlock_bh();
4318 static void addrconf_verify_work(struct work_struct *w)
4321 addrconf_verify_rtnl();
4325 static void addrconf_verify(void)
4327 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4330 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4331 struct in6_addr **peer_pfx)
4333 struct in6_addr *pfx = NULL;
4338 pfx = nla_data(addr);
4341 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4343 pfx = nla_data(local);
4349 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4350 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4351 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4352 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4353 [IFA_FLAGS] = { .len = sizeof(u32) },
4357 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4359 struct net *net = sock_net(skb->sk);
4360 struct ifaddrmsg *ifm;
4361 struct nlattr *tb[IFA_MAX+1];
4362 struct in6_addr *pfx, *peer_pfx;
4366 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4370 ifm = nlmsg_data(nlh);
4371 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4375 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4377 /* We ignore other flags so far. */
4378 ifa_flags &= IFA_F_MANAGETEMPADDR;
4380 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4381 ifm->ifa_prefixlen);
4384 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4385 u32 prefered_lft, u32 valid_lft)
4389 unsigned long timeout;
4390 bool was_managetempaddr;
4391 bool had_prefixroute;
4395 if (!valid_lft || (prefered_lft > valid_lft))
4398 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4399 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4402 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4403 if (addrconf_finite_timeout(timeout)) {
4404 expires = jiffies_to_clock_t(timeout * HZ);
4405 valid_lft = timeout;
4406 flags = RTF_EXPIRES;
4410 ifa_flags |= IFA_F_PERMANENT;
4413 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4414 if (addrconf_finite_timeout(timeout)) {
4416 ifa_flags |= IFA_F_DEPRECATED;
4417 prefered_lft = timeout;
4420 spin_lock_bh(&ifp->lock);
4421 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4422 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4423 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4424 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4425 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4426 IFA_F_NOPREFIXROUTE);
4427 ifp->flags |= ifa_flags;
4428 ifp->tstamp = jiffies;
4429 ifp->valid_lft = valid_lft;
4430 ifp->prefered_lft = prefered_lft;
4432 spin_unlock_bh(&ifp->lock);
4433 if (!(ifp->flags&IFA_F_TENTATIVE))
4434 ipv6_ifa_notify(0, ifp);
4436 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4437 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4439 } else if (had_prefixroute) {
4440 enum cleanup_prefix_rt_t action;
4441 unsigned long rt_expires;
4443 write_lock_bh(&ifp->idev->lock);
4444 action = check_cleanup_prefix_route(ifp, &rt_expires);
4445 write_unlock_bh(&ifp->idev->lock);
4447 if (action != CLEANUP_PREFIX_RT_NOP) {
4448 cleanup_prefix_route(ifp, rt_expires,
4449 action == CLEANUP_PREFIX_RT_DEL);
4453 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4454 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4455 valid_lft = prefered_lft = 0;
4456 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4457 !was_managetempaddr, jiffies);
4460 addrconf_verify_rtnl();
4466 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4468 struct net *net = sock_net(skb->sk);
4469 struct ifaddrmsg *ifm;
4470 struct nlattr *tb[IFA_MAX+1];
4471 struct in6_addr *pfx, *peer_pfx;
4472 struct inet6_ifaddr *ifa;
4473 struct net_device *dev;
4474 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4478 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4482 ifm = nlmsg_data(nlh);
4483 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4487 if (tb[IFA_CACHEINFO]) {
4488 struct ifa_cacheinfo *ci;
4490 ci = nla_data(tb[IFA_CACHEINFO]);
4491 valid_lft = ci->ifa_valid;
4492 preferred_lft = ci->ifa_prefered;
4494 preferred_lft = INFINITY_LIFE_TIME;
4495 valid_lft = INFINITY_LIFE_TIME;
4498 dev = __dev_get_by_index(net, ifm->ifa_index);
4502 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4504 /* We ignore other flags so far. */
4505 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4506 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4508 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4511 * It would be best to check for !NLM_F_CREATE here but
4512 * userspace already relies on not having to provide this.
4514 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4515 ifm->ifa_prefixlen, ifa_flags,
4516 preferred_lft, valid_lft);
4519 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4520 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4523 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4530 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4531 u8 scope, int ifindex)
4533 struct ifaddrmsg *ifm;
4535 ifm = nlmsg_data(nlh);
4536 ifm->ifa_family = AF_INET6;
4537 ifm->ifa_prefixlen = prefixlen;
4538 ifm->ifa_flags = flags;
4539 ifm->ifa_scope = scope;
4540 ifm->ifa_index = ifindex;
4543 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4544 unsigned long tstamp, u32 preferred, u32 valid)
4546 struct ifa_cacheinfo ci;
4548 ci.cstamp = cstamp_delta(cstamp);
4549 ci.tstamp = cstamp_delta(tstamp);
4550 ci.ifa_prefered = preferred;
4551 ci.ifa_valid = valid;
4553 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4556 static inline int rt_scope(int ifa_scope)
4558 if (ifa_scope & IFA_HOST)
4559 return RT_SCOPE_HOST;
4560 else if (ifa_scope & IFA_LINK)
4561 return RT_SCOPE_LINK;
4562 else if (ifa_scope & IFA_SITE)
4563 return RT_SCOPE_SITE;
4565 return RT_SCOPE_UNIVERSE;
4568 static inline int inet6_ifaddr_msgsize(void)
4570 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4571 + nla_total_size(16) /* IFA_LOCAL */
4572 + nla_total_size(16) /* IFA_ADDRESS */
4573 + nla_total_size(sizeof(struct ifa_cacheinfo))
4574 + nla_total_size(4) /* IFA_FLAGS */;
4577 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4578 u32 portid, u32 seq, int event, unsigned int flags)
4580 struct nlmsghdr *nlh;
4581 u32 preferred, valid;
4583 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4587 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4588 ifa->idev->dev->ifindex);
4590 if (!((ifa->flags&IFA_F_PERMANENT) &&
4591 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4592 preferred = ifa->prefered_lft;
4593 valid = ifa->valid_lft;
4594 if (preferred != INFINITY_LIFE_TIME) {
4595 long tval = (jiffies - ifa->tstamp)/HZ;
4596 if (preferred > tval)
4600 if (valid != INFINITY_LIFE_TIME) {
4608 preferred = INFINITY_LIFE_TIME;
4609 valid = INFINITY_LIFE_TIME;
4612 if (!ipv6_addr_any(&ifa->peer_addr)) {
4613 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4614 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4617 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4620 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4623 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4626 nlmsg_end(skb, nlh);
4630 nlmsg_cancel(skb, nlh);
4634 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4635 u32 portid, u32 seq, int event, u16 flags)
4637 struct nlmsghdr *nlh;
4638 u8 scope = RT_SCOPE_UNIVERSE;
4639 int ifindex = ifmca->idev->dev->ifindex;
4641 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4642 scope = RT_SCOPE_SITE;
4644 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4648 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4649 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4650 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4651 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4652 nlmsg_cancel(skb, nlh);
4656 nlmsg_end(skb, nlh);
4660 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4661 u32 portid, u32 seq, int event, unsigned int flags)
4663 struct nlmsghdr *nlh;
4664 u8 scope = RT_SCOPE_UNIVERSE;
4665 int ifindex = ifaca->aca_idev->dev->ifindex;
4667 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4668 scope = RT_SCOPE_SITE;
4670 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4674 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4675 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4676 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4677 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4678 nlmsg_cancel(skb, nlh);
4682 nlmsg_end(skb, nlh);
4692 /* called with rcu_read_lock() */
4693 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4694 struct netlink_callback *cb, enum addr_type_t type,
4695 int s_ip_idx, int *p_ip_idx)
4697 struct ifmcaddr6 *ifmca;
4698 struct ifacaddr6 *ifaca;
4700 int ip_idx = *p_ip_idx;
4702 read_lock_bh(&idev->lock);
4704 case UNICAST_ADDR: {
4705 struct inet6_ifaddr *ifa;
4707 /* unicast address incl. temp addr */
4708 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4709 if (++ip_idx < s_ip_idx)
4711 err = inet6_fill_ifaddr(skb, ifa,
4712 NETLINK_CB(cb->skb).portid,
4718 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4722 case MULTICAST_ADDR:
4723 /* multicast address */
4724 for (ifmca = idev->mc_list; ifmca;
4725 ifmca = ifmca->next, ip_idx++) {
4726 if (ip_idx < s_ip_idx)
4728 err = inet6_fill_ifmcaddr(skb, ifmca,
4729 NETLINK_CB(cb->skb).portid,
4738 /* anycast address */
4739 for (ifaca = idev->ac_list; ifaca;
4740 ifaca = ifaca->aca_next, ip_idx++) {
4741 if (ip_idx < s_ip_idx)
4743 err = inet6_fill_ifacaddr(skb, ifaca,
4744 NETLINK_CB(cb->skb).portid,
4755 read_unlock_bh(&idev->lock);
4760 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4761 enum addr_type_t type)
4763 struct net *net = sock_net(skb->sk);
4766 int s_idx, s_ip_idx;
4767 struct net_device *dev;
4768 struct inet6_dev *idev;
4769 struct hlist_head *head;
4772 s_idx = idx = cb->args[1];
4773 s_ip_idx = ip_idx = cb->args[2];
4776 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4777 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4779 head = &net->dev_index_head[h];
4780 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4783 if (h > s_h || idx > s_idx)
4786 idev = __in6_dev_get(dev);
4790 if (in6_dump_addrs(idev, skb, cb, type,
4791 s_ip_idx, &ip_idx) < 0)
4801 cb->args[2] = ip_idx;
4806 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4808 enum addr_type_t type = UNICAST_ADDR;
4810 return inet6_dump_addr(skb, cb, type);
4813 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4815 enum addr_type_t type = MULTICAST_ADDR;
4817 return inet6_dump_addr(skb, cb, type);
4821 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4823 enum addr_type_t type = ANYCAST_ADDR;
4825 return inet6_dump_addr(skb, cb, type);
4828 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4830 struct net *net = sock_net(in_skb->sk);
4831 struct ifaddrmsg *ifm;
4832 struct nlattr *tb[IFA_MAX+1];
4833 struct in6_addr *addr = NULL, *peer;
4834 struct net_device *dev = NULL;
4835 struct inet6_ifaddr *ifa;
4836 struct sk_buff *skb;
4839 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4843 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4849 ifm = nlmsg_data(nlh);
4851 dev = __dev_get_by_index(net, ifm->ifa_index);
4853 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4855 err = -EADDRNOTAVAIL;
4859 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4865 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4866 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4868 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4869 WARN_ON(err == -EMSGSIZE);
4873 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4880 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4882 struct sk_buff *skb;
4883 struct net *net = dev_net(ifa->idev->dev);
4886 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4890 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4892 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4893 WARN_ON(err == -EMSGSIZE);
4897 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4901 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4904 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4905 __s32 *array, int bytes)
4907 BUG_ON(bytes < (DEVCONF_MAX * 4));
4909 memset(array, 0, bytes);
4910 array[DEVCONF_FORWARDING] = cnf->forwarding;
4911 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4912 array[DEVCONF_MTU6] = cnf->mtu6;
4913 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4914 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4915 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4916 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4917 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4918 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4919 jiffies_to_msecs(cnf->rtr_solicit_interval);
4920 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4921 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
4922 array[DEVCONF_RTR_SOLICIT_DELAY] =
4923 jiffies_to_msecs(cnf->rtr_solicit_delay);
4924 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4925 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4926 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4927 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4928 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4929 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4930 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4931 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4932 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4933 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4934 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4935 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4936 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4937 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4938 #ifdef CONFIG_IPV6_ROUTER_PREF
4939 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4940 array[DEVCONF_RTR_PROBE_INTERVAL] =
4941 jiffies_to_msecs(cnf->rtr_probe_interval);
4942 #ifdef CONFIG_IPV6_ROUTE_INFO
4943 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4946 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4947 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4948 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4949 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4950 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4952 #ifdef CONFIG_IPV6_MROUTE
4953 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4955 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4956 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4957 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4958 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4959 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4960 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4961 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4962 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
4963 /* we omit DEVCONF_STABLE_SECRET for now */
4964 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4965 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
4966 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4967 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
4970 static inline size_t inet6_ifla6_size(void)
4972 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4973 + nla_total_size(sizeof(struct ifla_cacheinfo))
4974 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4975 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4976 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4977 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4980 static inline size_t inet6_if_nlmsg_size(void)
4982 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4983 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4984 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4985 + nla_total_size(4) /* IFLA_MTU */
4986 + nla_total_size(4) /* IFLA_LINK */
4987 + nla_total_size(1) /* IFLA_OPERSTATE */
4988 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4991 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4995 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
4998 /* Use put_unaligned() because stats may not be aligned for u64. */
4999 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5000 for (i = 1; i < ICMP6_MIB_MAX; i++)
5001 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5003 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5006 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5007 int bytes, size_t syncpoff)
5010 u64 buff[IPSTATS_MIB_MAX];
5011 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5015 memset(buff, 0, sizeof(buff));
5016 buff[0] = IPSTATS_MIB_MAX;
5018 for_each_possible_cpu(c) {
5019 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5020 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5023 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5024 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5027 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5031 case IFLA_INET6_STATS:
5032 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5033 offsetof(struct ipstats_mib, syncp));
5035 case IFLA_INET6_ICMP6STATS:
5036 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5041 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5042 u32 ext_filter_mask)
5045 struct ifla_cacheinfo ci;
5047 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5048 goto nla_put_failure;
5049 ci.max_reasm_len = IPV6_MAXPLEN;
5050 ci.tstamp = cstamp_delta(idev->tstamp);
5051 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5052 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5053 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5054 goto nla_put_failure;
5055 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5057 goto nla_put_failure;
5058 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5060 /* XXX - MC not implemented */
5062 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5065 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5067 goto nla_put_failure;
5068 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5070 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5072 goto nla_put_failure;
5073 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5075 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5077 goto nla_put_failure;
5079 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5080 goto nla_put_failure;
5082 read_lock_bh(&idev->lock);
5083 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5084 read_unlock_bh(&idev->lock);
5092 static size_t inet6_get_link_af_size(const struct net_device *dev,
5093 u32 ext_filter_mask)
5095 if (!__in6_dev_get(dev))
5098 return inet6_ifla6_size();
5101 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5102 u32 ext_filter_mask)
5104 struct inet6_dev *idev = __in6_dev_get(dev);
5109 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5115 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5117 struct inet6_ifaddr *ifp;
5118 struct net_device *dev = idev->dev;
5119 bool clear_token, update_rs = false;
5120 struct in6_addr ll_addr;
5126 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5128 if (!ipv6_accept_ra(idev))
5130 if (idev->cnf.rtr_solicits == 0)
5133 write_lock_bh(&idev->lock);
5135 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5136 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5138 write_unlock_bh(&idev->lock);
5140 clear_token = ipv6_addr_any(token);
5144 if (!idev->dead && (idev->if_flags & IF_READY) &&
5145 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5146 IFA_F_OPTIMISTIC)) {
5147 /* If we're not ready, then normal ifup will take care
5148 * of this. Otherwise, we need to request our rs here.
5150 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5155 write_lock_bh(&idev->lock);
5158 idev->if_flags |= IF_RS_SENT;
5159 idev->rs_interval = rfc3315_s14_backoff_init(
5160 idev->cnf.rtr_solicit_interval);
5161 idev->rs_probes = 1;
5162 addrconf_mod_rs_timer(idev, idev->rs_interval);
5165 /* Well, that's kinda nasty ... */
5166 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5167 spin_lock(&ifp->lock);
5168 if (ifp->tokenized) {
5170 ifp->prefered_lft = 0;
5172 spin_unlock(&ifp->lock);
5175 write_unlock_bh(&idev->lock);
5176 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5177 addrconf_verify_rtnl();
5181 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5182 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5183 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5186 static int inet6_validate_link_af(const struct net_device *dev,
5187 const struct nlattr *nla)
5189 struct nlattr *tb[IFLA_INET6_MAX + 1];
5191 if (dev && !__in6_dev_get(dev))
5192 return -EAFNOSUPPORT;
5194 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5197 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5200 struct inet6_dev *idev = __in6_dev_get(dev);
5201 struct nlattr *tb[IFLA_INET6_MAX + 1];
5204 return -EAFNOSUPPORT;
5206 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5209 if (tb[IFLA_INET6_TOKEN]) {
5210 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5215 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5216 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5218 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5219 mode != IN6_ADDR_GEN_MODE_NONE &&
5220 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5221 mode != IN6_ADDR_GEN_MODE_RANDOM)
5224 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5225 !idev->cnf.stable_secret.initialized &&
5226 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5229 idev->addr_gen_mode = mode;
5236 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5237 u32 portid, u32 seq, int event, unsigned int flags)
5239 struct net_device *dev = idev->dev;
5240 struct ifinfomsg *hdr;
5241 struct nlmsghdr *nlh;
5244 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5248 hdr = nlmsg_data(nlh);
5249 hdr->ifi_family = AF_INET6;
5251 hdr->ifi_type = dev->type;
5252 hdr->ifi_index = dev->ifindex;
5253 hdr->ifi_flags = dev_get_flags(dev);
5254 hdr->ifi_change = 0;
5256 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5258 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5259 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5260 (dev->ifindex != dev_get_iflink(dev) &&
5261 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5262 nla_put_u8(skb, IFLA_OPERSTATE,
5263 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5264 goto nla_put_failure;
5265 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5267 goto nla_put_failure;
5269 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5270 goto nla_put_failure;
5272 nla_nest_end(skb, protoinfo);
5273 nlmsg_end(skb, nlh);
5277 nlmsg_cancel(skb, nlh);
5281 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5283 struct net *net = sock_net(skb->sk);
5286 struct net_device *dev;
5287 struct inet6_dev *idev;
5288 struct hlist_head *head;
5291 s_idx = cb->args[1];
5294 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5296 head = &net->dev_index_head[h];
5297 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5300 idev = __in6_dev_get(dev);
5303 if (inet6_fill_ifinfo(skb, idev,
5304 NETLINK_CB(cb->skb).portid,
5306 RTM_NEWLINK, NLM_F_MULTI) < 0)
5320 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5322 struct sk_buff *skb;
5323 struct net *net = dev_net(idev->dev);
5326 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5330 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5332 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5333 WARN_ON(err == -EMSGSIZE);
5337 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5341 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5344 static inline size_t inet6_prefix_nlmsg_size(void)
5346 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5347 + nla_total_size(sizeof(struct in6_addr))
5348 + nla_total_size(sizeof(struct prefix_cacheinfo));
5351 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5352 struct prefix_info *pinfo, u32 portid, u32 seq,
5353 int event, unsigned int flags)
5355 struct prefixmsg *pmsg;
5356 struct nlmsghdr *nlh;
5357 struct prefix_cacheinfo ci;
5359 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5363 pmsg = nlmsg_data(nlh);
5364 pmsg->prefix_family = AF_INET6;
5365 pmsg->prefix_pad1 = 0;
5366 pmsg->prefix_pad2 = 0;
5367 pmsg->prefix_ifindex = idev->dev->ifindex;
5368 pmsg->prefix_len = pinfo->prefix_len;
5369 pmsg->prefix_type = pinfo->type;
5370 pmsg->prefix_pad3 = 0;
5371 pmsg->prefix_flags = 0;
5373 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5374 if (pinfo->autoconf)
5375 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5377 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5378 goto nla_put_failure;
5379 ci.preferred_time = ntohl(pinfo->prefered);
5380 ci.valid_time = ntohl(pinfo->valid);
5381 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5382 goto nla_put_failure;
5383 nlmsg_end(skb, nlh);
5387 nlmsg_cancel(skb, nlh);
5391 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5392 struct prefix_info *pinfo)
5394 struct sk_buff *skb;
5395 struct net *net = dev_net(idev->dev);
5398 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5402 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5404 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5405 WARN_ON(err == -EMSGSIZE);
5409 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5413 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5416 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5418 struct net *net = dev_net(ifp->idev->dev);
5423 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5428 * If the address was optimistic
5429 * we inserted the route at the start of
5430 * our DAD process, so we don't need
5433 if (!(ifp->rt->rt6i_node))
5434 ip6_ins_rt(ifp->rt);
5435 if (ifp->idev->cnf.forwarding)
5436 addrconf_join_anycast(ifp);
5437 if (!ipv6_addr_any(&ifp->peer_addr))
5438 addrconf_prefix_route(&ifp->peer_addr, 128,
5439 ifp->idev->dev, 0, 0);
5442 if (ifp->idev->cnf.forwarding)
5443 addrconf_leave_anycast(ifp);
5444 addrconf_leave_solict(ifp->idev, &ifp->addr);
5445 if (!ipv6_addr_any(&ifp->peer_addr)) {
5446 struct rt6_info *rt;
5448 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5449 ifp->idev->dev, 0, 0);
5454 dst_hold(&ifp->rt->dst);
5455 ip6_del_rt(ifp->rt);
5457 rt_genid_bump_ipv6(net);
5460 atomic_inc(&net->ipv6.dev_addr_genid);
5463 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5466 if (likely(ifp->idev->dead == 0))
5467 __ipv6_ifa_notify(event, ifp);
5468 rcu_read_unlock_bh();
5471 #ifdef CONFIG_SYSCTL
5474 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5475 void __user *buffer, size_t *lenp, loff_t *ppos)
5477 int *valp = ctl->data;
5480 struct ctl_table lctl;
5484 * ctl->data points to idev->cnf.forwarding, we should
5485 * not modify it until we get the rtnl lock.
5490 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5493 ret = addrconf_fixup_forwarding(ctl, valp, val);
5500 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5501 void __user *buffer, size_t *lenp, loff_t *ppos)
5503 struct inet6_dev *idev = ctl->extra1;
5504 int min_mtu = IPV6_MIN_MTU;
5505 struct ctl_table lctl;
5508 lctl.extra1 = &min_mtu;
5509 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5511 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5514 static void dev_disable_change(struct inet6_dev *idev)
5516 struct netdev_notifier_info info;
5518 if (!idev || !idev->dev)
5521 netdev_notifier_info_init(&info, idev->dev);
5522 if (idev->cnf.disable_ipv6)
5523 addrconf_notify(NULL, NETDEV_DOWN, &info);
5525 addrconf_notify(NULL, NETDEV_UP, &info);
5528 static void addrconf_disable_change(struct net *net, __s32 newf)
5530 struct net_device *dev;
5531 struct inet6_dev *idev;
5534 for_each_netdev_rcu(net, dev) {
5535 idev = __in6_dev_get(dev);
5537 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5538 idev->cnf.disable_ipv6 = newf;
5540 dev_disable_change(idev);
5546 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5551 if (!rtnl_trylock())
5552 return restart_syscall();
5554 net = (struct net *)table->extra2;
5558 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5563 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5564 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5565 addrconf_disable_change(net, newf);
5566 } else if ((!newf) ^ (!old))
5567 dev_disable_change((struct inet6_dev *)table->extra1);
5574 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5575 void __user *buffer, size_t *lenp, loff_t *ppos)
5577 int *valp = ctl->data;
5580 struct ctl_table lctl;
5584 * ctl->data points to idev->cnf.disable_ipv6, we should
5585 * not modify it until we get the rtnl lock.
5590 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5593 ret = addrconf_disable_ipv6(ctl, valp, val);
5600 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5601 void __user *buffer, size_t *lenp, loff_t *ppos)
5603 int *valp = ctl->data;
5608 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5611 if (write && old != new) {
5612 struct net *net = ctl->extra2;
5614 if (!rtnl_trylock())
5615 return restart_syscall();
5617 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5618 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5619 NETCONFA_IFINDEX_DEFAULT,
5620 net->ipv6.devconf_dflt);
5621 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5622 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5623 NETCONFA_IFINDEX_ALL,
5624 net->ipv6.devconf_all);
5626 struct inet6_dev *idev = ctl->extra1;
5628 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5638 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5639 void __user *buffer, size_t *lenp,
5643 struct in6_addr addr;
5644 char str[IPV6_MAX_STRLEN];
5645 struct ctl_table lctl = *ctl;
5646 struct net *net = ctl->extra2;
5647 struct ipv6_stable_secret *secret = ctl->data;
5649 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5652 lctl.maxlen = IPV6_MAX_STRLEN;
5655 if (!rtnl_trylock())
5656 return restart_syscall();
5658 if (!write && !secret->initialized) {
5663 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5664 if (err >= sizeof(str)) {
5669 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5673 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5678 secret->initialized = true;
5679 secret->secret = addr;
5681 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5682 struct net_device *dev;
5684 for_each_netdev(net, dev) {
5685 struct inet6_dev *idev = __in6_dev_get(dev);
5688 idev->addr_gen_mode =
5689 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5693 struct inet6_dev *idev = ctl->extra1;
5695 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5705 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5707 void __user *buffer,
5711 int *valp = ctl->data;
5714 struct ctl_table lctl;
5717 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5718 * we should not modify it until we get the rtnl lock.
5723 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5726 ret = addrconf_fixup_linkdown(ctl, valp, val);
5732 static int minus_one = -1;
5733 static const int one = 1;
5734 static const int two_five_five = 255;
5736 static const struct ctl_table addrconf_sysctl[] = {
5738 .procname = "forwarding",
5739 .data = &ipv6_devconf.forwarding,
5740 .maxlen = sizeof(int),
5742 .proc_handler = addrconf_sysctl_forward,
5745 .procname = "hop_limit",
5746 .data = &ipv6_devconf.hop_limit,
5747 .maxlen = sizeof(int),
5749 .proc_handler = proc_dointvec_minmax,
5750 .extra1 = (void *)&one,
5751 .extra2 = (void *)&two_five_five,
5755 .data = &ipv6_devconf.mtu6,
5756 .maxlen = sizeof(int),
5758 .proc_handler = addrconf_sysctl_mtu,
5761 .procname = "accept_ra",
5762 .data = &ipv6_devconf.accept_ra,
5763 .maxlen = sizeof(int),
5765 .proc_handler = proc_dointvec,
5768 .procname = "accept_redirects",
5769 .data = &ipv6_devconf.accept_redirects,
5770 .maxlen = sizeof(int),
5772 .proc_handler = proc_dointvec,
5775 .procname = "autoconf",
5776 .data = &ipv6_devconf.autoconf,
5777 .maxlen = sizeof(int),
5779 .proc_handler = proc_dointvec,
5782 .procname = "dad_transmits",
5783 .data = &ipv6_devconf.dad_transmits,
5784 .maxlen = sizeof(int),
5786 .proc_handler = proc_dointvec,
5789 .procname = "router_solicitations",
5790 .data = &ipv6_devconf.rtr_solicits,
5791 .maxlen = sizeof(int),
5793 .proc_handler = proc_dointvec_minmax,
5794 .extra1 = &minus_one,
5797 .procname = "router_solicitation_interval",
5798 .data = &ipv6_devconf.rtr_solicit_interval,
5799 .maxlen = sizeof(int),
5801 .proc_handler = proc_dointvec_jiffies,
5804 .procname = "router_solicitation_max_interval",
5805 .data = &ipv6_devconf.rtr_solicit_max_interval,
5806 .maxlen = sizeof(int),
5808 .proc_handler = proc_dointvec_jiffies,
5811 .procname = "router_solicitation_delay",
5812 .data = &ipv6_devconf.rtr_solicit_delay,
5813 .maxlen = sizeof(int),
5815 .proc_handler = proc_dointvec_jiffies,
5818 .procname = "force_mld_version",
5819 .data = &ipv6_devconf.force_mld_version,
5820 .maxlen = sizeof(int),
5822 .proc_handler = proc_dointvec,
5825 .procname = "mldv1_unsolicited_report_interval",
5827 &ipv6_devconf.mldv1_unsolicited_report_interval,
5828 .maxlen = sizeof(int),
5830 .proc_handler = proc_dointvec_ms_jiffies,
5833 .procname = "mldv2_unsolicited_report_interval",
5835 &ipv6_devconf.mldv2_unsolicited_report_interval,
5836 .maxlen = sizeof(int),
5838 .proc_handler = proc_dointvec_ms_jiffies,
5841 .procname = "use_tempaddr",
5842 .data = &ipv6_devconf.use_tempaddr,
5843 .maxlen = sizeof(int),
5845 .proc_handler = proc_dointvec,
5848 .procname = "temp_valid_lft",
5849 .data = &ipv6_devconf.temp_valid_lft,
5850 .maxlen = sizeof(int),
5852 .proc_handler = proc_dointvec,
5855 .procname = "temp_prefered_lft",
5856 .data = &ipv6_devconf.temp_prefered_lft,
5857 .maxlen = sizeof(int),
5859 .proc_handler = proc_dointvec,
5862 .procname = "regen_max_retry",
5863 .data = &ipv6_devconf.regen_max_retry,
5864 .maxlen = sizeof(int),
5866 .proc_handler = proc_dointvec,
5869 .procname = "max_desync_factor",
5870 .data = &ipv6_devconf.max_desync_factor,
5871 .maxlen = sizeof(int),
5873 .proc_handler = proc_dointvec,
5876 .procname = "max_addresses",
5877 .data = &ipv6_devconf.max_addresses,
5878 .maxlen = sizeof(int),
5880 .proc_handler = proc_dointvec,
5883 .procname = "accept_ra_defrtr",
5884 .data = &ipv6_devconf.accept_ra_defrtr,
5885 .maxlen = sizeof(int),
5887 .proc_handler = proc_dointvec,
5890 .procname = "accept_ra_min_hop_limit",
5891 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5892 .maxlen = sizeof(int),
5894 .proc_handler = proc_dointvec,
5897 .procname = "accept_ra_pinfo",
5898 .data = &ipv6_devconf.accept_ra_pinfo,
5899 .maxlen = sizeof(int),
5901 .proc_handler = proc_dointvec,
5903 #ifdef CONFIG_IPV6_ROUTER_PREF
5905 .procname = "accept_ra_rtr_pref",
5906 .data = &ipv6_devconf.accept_ra_rtr_pref,
5907 .maxlen = sizeof(int),
5909 .proc_handler = proc_dointvec,
5912 .procname = "router_probe_interval",
5913 .data = &ipv6_devconf.rtr_probe_interval,
5914 .maxlen = sizeof(int),
5916 .proc_handler = proc_dointvec_jiffies,
5918 #ifdef CONFIG_IPV6_ROUTE_INFO
5920 .procname = "accept_ra_rt_info_max_plen",
5921 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5922 .maxlen = sizeof(int),
5924 .proc_handler = proc_dointvec,
5929 .procname = "proxy_ndp",
5930 .data = &ipv6_devconf.proxy_ndp,
5931 .maxlen = sizeof(int),
5933 .proc_handler = addrconf_sysctl_proxy_ndp,
5936 .procname = "accept_source_route",
5937 .data = &ipv6_devconf.accept_source_route,
5938 .maxlen = sizeof(int),
5940 .proc_handler = proc_dointvec,
5942 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5944 .procname = "optimistic_dad",
5945 .data = &ipv6_devconf.optimistic_dad,
5946 .maxlen = sizeof(int),
5948 .proc_handler = proc_dointvec,
5951 .procname = "use_optimistic",
5952 .data = &ipv6_devconf.use_optimistic,
5953 .maxlen = sizeof(int),
5955 .proc_handler = proc_dointvec,
5958 #ifdef CONFIG_IPV6_MROUTE
5960 .procname = "mc_forwarding",
5961 .data = &ipv6_devconf.mc_forwarding,
5962 .maxlen = sizeof(int),
5964 .proc_handler = proc_dointvec,
5968 .procname = "disable_ipv6",
5969 .data = &ipv6_devconf.disable_ipv6,
5970 .maxlen = sizeof(int),
5972 .proc_handler = addrconf_sysctl_disable,
5975 .procname = "accept_dad",
5976 .data = &ipv6_devconf.accept_dad,
5977 .maxlen = sizeof(int),
5979 .proc_handler = proc_dointvec,
5982 .procname = "force_tllao",
5983 .data = &ipv6_devconf.force_tllao,
5984 .maxlen = sizeof(int),
5986 .proc_handler = proc_dointvec
5989 .procname = "ndisc_notify",
5990 .data = &ipv6_devconf.ndisc_notify,
5991 .maxlen = sizeof(int),
5993 .proc_handler = proc_dointvec
5996 .procname = "suppress_frag_ndisc",
5997 .data = &ipv6_devconf.suppress_frag_ndisc,
5998 .maxlen = sizeof(int),
6000 .proc_handler = proc_dointvec
6003 .procname = "accept_ra_from_local",
6004 .data = &ipv6_devconf.accept_ra_from_local,
6005 .maxlen = sizeof(int),
6007 .proc_handler = proc_dointvec,
6010 .procname = "accept_ra_mtu",
6011 .data = &ipv6_devconf.accept_ra_mtu,
6012 .maxlen = sizeof(int),
6014 .proc_handler = proc_dointvec,
6017 .procname = "stable_secret",
6018 .data = &ipv6_devconf.stable_secret,
6019 .maxlen = IPV6_MAX_STRLEN,
6021 .proc_handler = addrconf_sysctl_stable_secret,
6024 .procname = "use_oif_addrs_only",
6025 .data = &ipv6_devconf.use_oif_addrs_only,
6026 .maxlen = sizeof(int),
6028 .proc_handler = proc_dointvec,
6031 .procname = "ignore_routes_with_linkdown",
6032 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6033 .maxlen = sizeof(int),
6035 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6038 .procname = "drop_unicast_in_l2_multicast",
6039 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6040 .maxlen = sizeof(int),
6042 .proc_handler = proc_dointvec,
6045 .procname = "drop_unsolicited_na",
6046 .data = &ipv6_devconf.drop_unsolicited_na,
6047 .maxlen = sizeof(int),
6049 .proc_handler = proc_dointvec,
6052 .procname = "keep_addr_on_down",
6053 .data = &ipv6_devconf.keep_addr_on_down,
6054 .maxlen = sizeof(int),
6056 .proc_handler = proc_dointvec,
6064 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6065 struct inet6_dev *idev, struct ipv6_devconf *p)
6068 struct ctl_table *table;
6069 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6071 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6075 for (i = 0; table[i].data; i++) {
6076 table[i].data += (char *)p - (char *)&ipv6_devconf;
6077 /* If one of these is already set, then it is not safe to
6078 * overwrite either of them: this makes proc_dointvec_minmax
6081 if (!table[i].extra1 && !table[i].extra2) {
6082 table[i].extra1 = idev; /* embedded; no ref */
6083 table[i].extra2 = net;
6087 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6089 p->sysctl_header = register_net_sysctl(net, path, table);
6090 if (!p->sysctl_header)
6093 if (!strcmp(dev_name, "all"))
6094 ifindex = NETCONFA_IFINDEX_ALL;
6095 else if (!strcmp(dev_name, "default"))
6096 ifindex = NETCONFA_IFINDEX_DEFAULT;
6098 ifindex = idev->dev->ifindex;
6099 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
6108 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6110 struct ctl_table *table;
6112 if (!p->sysctl_header)
6115 table = p->sysctl_header->ctl_table_arg;
6116 unregister_net_sysctl_table(p->sysctl_header);
6117 p->sysctl_header = NULL;
6121 static int addrconf_sysctl_register(struct inet6_dev *idev)
6125 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6128 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6129 &ndisc_ifinfo_sysctl_change);
6132 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6135 neigh_sysctl_unregister(idev->nd_parms);
6140 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6142 __addrconf_sysctl_unregister(&idev->cnf);
6143 neigh_sysctl_unregister(idev->nd_parms);
6149 static int __net_init addrconf_init_net(struct net *net)
6152 struct ipv6_devconf *all, *dflt;
6154 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6158 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6160 goto err_alloc_dflt;
6162 /* these will be inherited by all namespaces */
6163 dflt->autoconf = ipv6_defaults.autoconf;
6164 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6166 dflt->stable_secret.initialized = false;
6167 all->stable_secret.initialized = false;
6169 net->ipv6.devconf_all = all;
6170 net->ipv6.devconf_dflt = dflt;
6172 #ifdef CONFIG_SYSCTL
6173 err = __addrconf_sysctl_register(net, "all", NULL, all);
6177 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6183 #ifdef CONFIG_SYSCTL
6185 __addrconf_sysctl_unregister(all);
6195 static void __net_exit addrconf_exit_net(struct net *net)
6197 #ifdef CONFIG_SYSCTL
6198 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6199 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6201 kfree(net->ipv6.devconf_dflt);
6202 kfree(net->ipv6.devconf_all);
6205 static struct pernet_operations addrconf_ops = {
6206 .init = addrconf_init_net,
6207 .exit = addrconf_exit_net,
6210 static struct rtnl_af_ops inet6_ops __read_mostly = {
6212 .fill_link_af = inet6_fill_link_af,
6213 .get_link_af_size = inet6_get_link_af_size,
6214 .validate_link_af = inet6_validate_link_af,
6215 .set_link_af = inet6_set_link_af,
6219 * Init / cleanup code
6222 int __init addrconf_init(void)
6224 struct inet6_dev *idev;
6227 err = ipv6_addr_label_init();
6229 pr_crit("%s: cannot initialize default policy table: %d\n",
6234 err = register_pernet_subsys(&addrconf_ops);
6238 addrconf_wq = create_workqueue("ipv6_addrconf");
6244 /* The addrconf netdev notifier requires that loopback_dev
6245 * has it's ipv6 private information allocated and setup
6246 * before it can bring up and give link-local addresses
6247 * to other devices which are up.
6249 * Unfortunately, loopback_dev is not necessarily the first
6250 * entry in the global dev_base list of net devices. In fact,
6251 * it is likely to be the very last entry on that list.
6252 * So this causes the notifier registry below to try and
6253 * give link-local addresses to all devices besides loopback_dev
6254 * first, then loopback_dev, which cases all the non-loopback_dev
6255 * devices to fail to get a link-local address.
6257 * So, as a temporary fix, allocate the ipv6 structure for
6258 * loopback_dev first by hand.
6259 * Longer term, all of the dependencies ipv6 has upon the loopback
6260 * device and it being up should be removed.
6263 idev = ipv6_add_dev(init_net.loopback_dev);
6266 err = PTR_ERR(idev);
6270 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6271 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6273 register_netdevice_notifier(&ipv6_dev_notf);
6277 rtnl_af_register(&inet6_ops);
6279 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6284 /* Only the first call to __rtnl_register can fail */
6285 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6286 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6287 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6288 inet6_dump_ifaddr, NULL);
6289 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6290 inet6_dump_ifmcaddr, NULL);
6291 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6292 inet6_dump_ifacaddr, NULL);
6293 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6294 inet6_netconf_dump_devconf, NULL);
6296 ipv6_addr_label_rtnl_register();
6300 rtnl_af_unregister(&inet6_ops);
6301 unregister_netdevice_notifier(&ipv6_dev_notf);
6303 destroy_workqueue(addrconf_wq);
6305 unregister_pernet_subsys(&addrconf_ops);
6307 ipv6_addr_label_cleanup();
6312 void addrconf_cleanup(void)
6314 struct net_device *dev;
6317 unregister_netdevice_notifier(&ipv6_dev_notf);
6318 unregister_pernet_subsys(&addrconf_ops);
6319 ipv6_addr_label_cleanup();
6323 __rtnl_af_unregister(&inet6_ops);
6325 /* clean dev list */
6326 for_each_netdev(&init_net, dev) {
6327 if (__in6_dev_get(dev) == NULL)
6329 addrconf_ifdown(dev, 1);
6331 addrconf_ifdown(init_net.loopback_dev, 2);
6336 spin_lock_bh(&addrconf_hash_lock);
6337 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6338 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
6339 spin_unlock_bh(&addrconf_hash_lock);
6340 cancel_delayed_work(&addr_chk_work);
6343 destroy_workqueue(addrconf_wq);