2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/sched/signal.h>
47 #include <linux/socket.h>
48 #include <linux/sockios.h>
49 #include <linux/net.h>
50 #include <linux/inet.h>
51 #include <linux/in6.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_addr.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_arcnet.h>
56 #include <linux/if_infiniband.h>
57 #include <linux/route.h>
58 #include <linux/inetdevice.h>
59 #include <linux/init.h>
60 #include <linux/slab.h>
62 #include <linux/sysctl.h>
64 #include <linux/capability.h>
65 #include <linux/delay.h>
66 #include <linux/notifier.h>
67 #include <linux/string.h>
68 #include <linux/hash.h>
70 #include <net/net_namespace.h>
74 #include <net/6lowpan.h>
75 #include <net/firewire.h>
77 #include <net/protocol.h>
78 #include <net/ndisc.h>
79 #include <net/ip6_route.h>
80 #include <net/addrconf.h>
83 #include <net/netlink.h>
84 #include <net/pkt_sched.h>
85 #include <net/l3mdev.h>
86 #include <linux/if_tunnel.h>
87 #include <linux/rtnetlink.h>
88 #include <linux/netconf.h>
89 #include <linux/random.h>
90 #include <linux/uaccess.h>
91 #include <asm/unaligned.h>
93 #include <linux/proc_fs.h>
94 #include <linux/seq_file.h>
95 #include <linux/export.h>
97 /* Set to 3 to get tracing... */
101 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
103 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
106 #define INFINITY_LIFE_TIME 0xFFFFFFFF
108 #define IPV6_MAX_STRLEN \
109 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
111 static inline u32 cstamp_delta(unsigned long cstamp)
113 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
116 static inline s32 rfc3315_s14_backoff_init(s32 irt)
118 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120 do_div(tmp, 1000000);
124 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
126 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128 do_div(tmp, 1000000);
129 if ((s32)tmp > mrt) {
130 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132 do_div(tmp, 1000000);
138 static int addrconf_sysctl_register(struct inet6_dev *idev);
139 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
141 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
146 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
151 static void ipv6_regen_rndid(struct inet6_dev *idev);
152 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
154 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
155 static int ipv6_count_addresses(struct inet6_dev *idev);
156 static int ipv6_generate_stable_address(struct in6_addr *addr,
158 const struct inet6_dev *idev);
161 * Configured unicast address hash table
163 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
164 static DEFINE_SPINLOCK(addrconf_hash_lock);
166 static void addrconf_verify(void);
167 static void addrconf_verify_rtnl(void);
168 static void addrconf_verify_work(struct work_struct *);
170 static struct workqueue_struct *addrconf_wq;
171 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
173 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
176 static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
178 static int addrconf_ifdown(struct net_device *dev, int how);
180 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
185 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
186 static void addrconf_dad_work(struct work_struct *w);
187 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
188 static void addrconf_dad_run(struct inet6_dev *idev);
189 static void addrconf_rs_timer(unsigned long data);
190 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
193 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
194 struct prefix_info *pinfo);
195 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
198 static struct ipv6_devconf ipv6_devconf __read_mostly = {
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
203 .accept_redirects = 1,
205 .force_mld_version = 0,
206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
218 .max_addresses = IPV6_MAX_ADDRESSES,
219 .accept_ra_defrtr = 1,
220 .accept_ra_from_local = 0,
221 .accept_ra_min_hop_limit= 1,
222 .accept_ra_pinfo = 1,
223 #ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
225 .rtr_probe_interval = 60 * HZ,
226 #ifdef CONFIG_IPV6_ROUTE_INFO
227 .accept_ra_rt_info_min_plen = 0,
228 .accept_ra_rt_info_max_plen = 0,
232 .accept_source_route = 0, /* we do not accept RH0 by default. */
235 .suppress_frag_ndisc = 1,
238 .initialized = false,
240 .use_oif_addrs_only = 0,
241 .ignore_routes_with_linkdown = 0,
242 .keep_addr_on_down = 0,
244 #ifdef CONFIG_IPV6_SEG6_HMAC
245 .seg6_require_hmac = 0,
248 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
252 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
254 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
255 .mtu6 = IPV6_MIN_MTU,
257 .accept_redirects = 1,
259 .force_mld_version = 0,
260 .mldv1_unsolicited_report_interval = 10 * HZ,
261 .mldv2_unsolicited_report_interval = HZ,
263 .rtr_solicits = MAX_RTR_SOLICITATIONS,
264 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
265 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
266 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
268 .temp_valid_lft = TEMP_VALID_LIFETIME,
269 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
270 .regen_max_retry = REGEN_MAX_RETRY,
271 .max_desync_factor = MAX_DESYNC_FACTOR,
272 .max_addresses = IPV6_MAX_ADDRESSES,
273 .accept_ra_defrtr = 1,
274 .accept_ra_from_local = 0,
275 .accept_ra_min_hop_limit= 1,
276 .accept_ra_pinfo = 1,
277 #ifdef CONFIG_IPV6_ROUTER_PREF
278 .accept_ra_rtr_pref = 1,
279 .rtr_probe_interval = 60 * HZ,
280 #ifdef CONFIG_IPV6_ROUTE_INFO
281 .accept_ra_rt_info_min_plen = 0,
282 .accept_ra_rt_info_max_plen = 0,
286 .accept_source_route = 0, /* we do not accept RH0 by default. */
289 .suppress_frag_ndisc = 1,
292 .initialized = false,
294 .use_oif_addrs_only = 0,
295 .ignore_routes_with_linkdown = 0,
296 .keep_addr_on_down = 0,
298 #ifdef CONFIG_IPV6_SEG6_HMAC
299 .seg6_require_hmac = 0,
302 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
306 /* Check if a valid qdisc is available */
307 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
309 return !qdisc_tx_is_noop(dev);
312 static void addrconf_del_rs_timer(struct inet6_dev *idev)
314 if (del_timer(&idev->rs_timer))
318 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
320 if (cancel_delayed_work(&ifp->dad_work))
324 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
327 if (!timer_pending(&idev->rs_timer))
329 mod_timer(&idev->rs_timer, jiffies + when);
332 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
335 if (!delayed_work_pending(&ifp->dad_work))
337 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
340 static int snmp6_alloc_dev(struct inet6_dev *idev)
344 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
345 if (!idev->stats.ipv6)
348 for_each_possible_cpu(i) {
349 struct ipstats_mib *addrconf_stats;
350 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
351 u64_stats_init(&addrconf_stats->syncp);
355 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
357 if (!idev->stats.icmpv6dev)
359 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
361 if (!idev->stats.icmpv6msgdev)
367 kfree(idev->stats.icmpv6dev);
369 free_percpu(idev->stats.ipv6);
374 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
376 struct inet6_dev *ndev;
381 if (dev->mtu < IPV6_MIN_MTU)
382 return ERR_PTR(-EINVAL);
384 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
388 rwlock_init(&ndev->lock);
390 INIT_LIST_HEAD(&ndev->addr_list);
391 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
392 (unsigned long)ndev);
393 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
395 if (ndev->cnf.stable_secret.initialized)
396 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
398 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
400 ndev->cnf.mtu6 = dev->mtu;
401 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
402 if (!ndev->nd_parms) {
406 if (ndev->cnf.forwarding)
407 dev_disable_lro(dev);
408 /* We refer to the device */
411 if (snmp6_alloc_dev(ndev) < 0) {
413 "%s: cannot allocate memory for statistics; dev=%s.\n",
414 __func__, dev->name);
415 neigh_parms_release(&nd_tbl, ndev->nd_parms);
421 if (snmp6_register_dev(ndev) < 0) {
423 "%s: cannot create /proc/net/dev_snmp6/%s\n",
424 __func__, dev->name);
428 /* One reference from device. */
431 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
432 ndev->cnf.accept_dad = -1;
434 #if IS_ENABLED(CONFIG_IPV6_SIT)
435 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
436 pr_info("%s: Disabled Multicast RS\n", dev->name);
437 ndev->cnf.rtr_solicits = 0;
441 INIT_LIST_HEAD(&ndev->tempaddr_list);
442 ndev->desync_factor = U32_MAX;
443 if ((dev->flags&IFF_LOOPBACK) ||
444 dev->type == ARPHRD_TUNNEL ||
445 dev->type == ARPHRD_TUNNEL6 ||
446 dev->type == ARPHRD_SIT ||
447 dev->type == ARPHRD_NONE) {
448 ndev->cnf.use_tempaddr = -1;
450 ipv6_regen_rndid(ndev);
452 ndev->token = in6addr_any;
454 if (netif_running(dev) && addrconf_qdisc_ok(dev))
455 ndev->if_flags |= IF_READY;
457 ipv6_mc_init_dev(ndev);
458 ndev->tstamp = jiffies;
459 err = addrconf_sysctl_register(ndev);
461 ipv6_mc_destroy_dev(ndev);
462 snmp6_unregister_dev(ndev);
465 /* protected by rtnl_lock */
466 rcu_assign_pointer(dev->ip6_ptr, ndev);
468 /* Join interface-local all-node multicast group */
469 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
471 /* Join all-node multicast group */
472 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
474 /* Join all-router multicast group if forwarding is set */
475 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
476 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
481 neigh_parms_release(&nd_tbl, ndev->nd_parms);
483 in6_dev_finish_destroy(ndev);
487 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
489 struct inet6_dev *idev;
493 idev = __in6_dev_get(dev);
495 idev = ipv6_add_dev(dev);
500 if (dev->flags&IFF_UP)
505 static int inet6_netconf_msgsize_devconf(int type)
507 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
508 + nla_total_size(4); /* NETCONFA_IFINDEX */
511 if (type == NETCONFA_ALL)
514 if (all || type == NETCONFA_FORWARDING)
515 size += nla_total_size(4);
516 #ifdef CONFIG_IPV6_MROUTE
517 if (all || type == NETCONFA_MC_FORWARDING)
518 size += nla_total_size(4);
520 if (all || type == NETCONFA_PROXY_NEIGH)
521 size += nla_total_size(4);
523 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
524 size += nla_total_size(4);
529 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
530 struct ipv6_devconf *devconf, u32 portid,
531 u32 seq, int event, unsigned int flags,
534 struct nlmsghdr *nlh;
535 struct netconfmsg *ncm;
538 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
543 if (type == NETCONFA_ALL)
546 ncm = nlmsg_data(nlh);
547 ncm->ncm_family = AF_INET6;
549 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
550 goto nla_put_failure;
555 if ((all || type == NETCONFA_FORWARDING) &&
556 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
557 goto nla_put_failure;
558 #ifdef CONFIG_IPV6_MROUTE
559 if ((all || type == NETCONFA_MC_FORWARDING) &&
560 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
561 devconf->mc_forwarding) < 0)
562 goto nla_put_failure;
564 if ((all || type == NETCONFA_PROXY_NEIGH) &&
565 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
566 goto nla_put_failure;
568 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
569 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
570 devconf->ignore_routes_with_linkdown) < 0)
571 goto nla_put_failure;
578 nlmsg_cancel(skb, nlh);
582 void inet6_netconf_notify_devconf(struct net *net, int event, int type,
583 int ifindex, struct ipv6_devconf *devconf)
588 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
592 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
595 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
596 WARN_ON(err == -EMSGSIZE);
600 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
603 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
606 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
607 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
608 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
609 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
610 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
613 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
614 struct nlmsghdr *nlh)
616 struct net *net = sock_net(in_skb->sk);
617 struct nlattr *tb[NETCONFA_MAX+1];
618 struct netconfmsg *ncm;
620 struct ipv6_devconf *devconf;
621 struct inet6_dev *in6_dev;
622 struct net_device *dev;
626 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
627 devconf_ipv6_policy, NULL);
632 if (!tb[NETCONFA_IFINDEX])
635 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
637 case NETCONFA_IFINDEX_ALL:
638 devconf = net->ipv6.devconf_all;
640 case NETCONFA_IFINDEX_DEFAULT:
641 devconf = net->ipv6.devconf_dflt;
644 dev = __dev_get_by_index(net, ifindex);
647 in6_dev = __in6_dev_get(dev);
650 devconf = &in6_dev->cnf;
655 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
659 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
660 NETLINK_CB(in_skb).portid,
661 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
664 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
665 WARN_ON(err == -EMSGSIZE);
669 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
674 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
675 struct netlink_callback *cb)
677 struct net *net = sock_net(skb->sk);
680 struct net_device *dev;
681 struct inet6_dev *idev;
682 struct hlist_head *head;
685 s_idx = idx = cb->args[1];
687 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
689 head = &net->dev_index_head[h];
691 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
693 hlist_for_each_entry_rcu(dev, head, index_hlist) {
696 idev = __in6_dev_get(dev);
700 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
702 NETLINK_CB(cb->skb).portid,
710 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
716 if (h == NETDEV_HASHENTRIES) {
717 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
718 net->ipv6.devconf_all,
719 NETLINK_CB(cb->skb).portid,
721 RTM_NEWNETCONF, NLM_F_MULTI,
727 if (h == NETDEV_HASHENTRIES + 1) {
728 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
729 net->ipv6.devconf_dflt,
730 NETLINK_CB(cb->skb).portid,
732 RTM_NEWNETCONF, NLM_F_MULTI,
746 static void dev_forward_change(struct inet6_dev *idev)
748 struct net_device *dev;
749 struct inet6_ifaddr *ifa;
754 if (idev->cnf.forwarding)
755 dev_disable_lro(dev);
756 if (dev->flags & IFF_MULTICAST) {
757 if (idev->cnf.forwarding) {
758 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
759 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
760 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
762 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
763 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
764 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
768 list_for_each_entry(ifa, &idev->addr_list, if_list) {
769 if (ifa->flags&IFA_F_TENTATIVE)
771 if (idev->cnf.forwarding)
772 addrconf_join_anycast(ifa);
774 addrconf_leave_anycast(ifa);
776 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
778 dev->ifindex, &idev->cnf);
782 static void addrconf_forward_change(struct net *net, __s32 newf)
784 struct net_device *dev;
785 struct inet6_dev *idev;
787 for_each_netdev(net, dev) {
788 idev = __in6_dev_get(dev);
790 int changed = (!idev->cnf.forwarding) ^ (!newf);
791 idev->cnf.forwarding = newf;
793 dev_forward_change(idev);
798 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
804 return restart_syscall();
806 net = (struct net *)table->extra2;
810 if (p == &net->ipv6.devconf_dflt->forwarding) {
811 if ((!newf) ^ (!old))
812 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
814 NETCONFA_IFINDEX_DEFAULT,
815 net->ipv6.devconf_dflt);
820 if (p == &net->ipv6.devconf_all->forwarding) {
821 int old_dflt = net->ipv6.devconf_dflt->forwarding;
823 net->ipv6.devconf_dflt->forwarding = newf;
824 if ((!newf) ^ (!old_dflt))
825 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
827 NETCONFA_IFINDEX_DEFAULT,
828 net->ipv6.devconf_dflt);
830 addrconf_forward_change(net, newf);
831 if ((!newf) ^ (!old))
832 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
834 NETCONFA_IFINDEX_ALL,
835 net->ipv6.devconf_all);
836 } else if ((!newf) ^ (!old))
837 dev_forward_change((struct inet6_dev *)table->extra1);
841 rt6_purge_dflt_routers(net);
845 static void addrconf_linkdown_change(struct net *net, __s32 newf)
847 struct net_device *dev;
848 struct inet6_dev *idev;
850 for_each_netdev(net, dev) {
851 idev = __in6_dev_get(dev);
853 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
855 idev->cnf.ignore_routes_with_linkdown = newf;
857 inet6_netconf_notify_devconf(dev_net(dev),
859 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
866 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
872 return restart_syscall();
874 net = (struct net *)table->extra2;
878 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
879 if ((!newf) ^ (!old))
880 inet6_netconf_notify_devconf(net,
882 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
883 NETCONFA_IFINDEX_DEFAULT,
884 net->ipv6.devconf_dflt);
889 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
890 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
891 addrconf_linkdown_change(net, newf);
892 if ((!newf) ^ (!old))
893 inet6_netconf_notify_devconf(net,
895 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
896 NETCONFA_IFINDEX_ALL,
897 net->ipv6.devconf_all);
906 /* Nobody refers to this ifaddr, destroy it */
907 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
909 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
911 #ifdef NET_REFCNT_DEBUG
912 pr_debug("%s\n", __func__);
915 in6_dev_put(ifp->idev);
917 if (cancel_delayed_work(&ifp->dad_work))
918 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
921 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
922 pr_warn("Freeing alive inet6 address %p\n", ifp);
931 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
934 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
937 * Each device address list is sorted in order of scope -
938 * global before linklocal.
940 list_for_each(p, &idev->addr_list) {
941 struct inet6_ifaddr *ifa
942 = list_entry(p, struct inet6_ifaddr, if_list);
943 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
947 list_add_tail(&ifp->if_list, p);
950 static u32 inet6_addr_hash(const struct in6_addr *addr)
952 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
955 /* On success it returns ifp with increased reference count */
957 static struct inet6_ifaddr *
958 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
959 const struct in6_addr *peer_addr, int pfxlen,
960 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
962 struct net *net = dev_net(idev->dev);
963 struct inet6_ifaddr *ifa = NULL;
967 int addr_type = ipv6_addr_type(addr);
969 if (addr_type == IPV6_ADDR_ANY ||
970 addr_type & IPV6_ADDR_MULTICAST ||
971 (!(idev->dev->flags & IFF_LOOPBACK) &&
972 addr_type & IPV6_ADDR_LOOPBACK))
973 return ERR_PTR(-EADDRNOTAVAIL);
977 err = -ENODEV; /*XXX*/
981 if (idev->cnf.disable_ipv6) {
986 spin_lock(&addrconf_hash_lock);
988 /* Ignore adding duplicate addresses on an interface */
989 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
990 ADBG("ipv6_add_addr: already assigned\n");
995 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
998 ADBG("ipv6_add_addr: malloc failed\n");
1003 rt = addrconf_dst_alloc(idev, addr, false);
1009 if (net->ipv6.devconf_all->disable_policy ||
1010 idev->cnf.disable_policy)
1011 rt->dst.flags |= DST_NOPOLICY;
1013 neigh_parms_data_state_setall(idev->nd_parms);
1017 ifa->peer_addr = *peer_addr;
1019 spin_lock_init(&ifa->lock);
1020 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1021 INIT_HLIST_NODE(&ifa->addr_lst);
1023 ifa->prefix_len = pfxlen;
1024 ifa->flags = flags | IFA_F_TENTATIVE;
1025 ifa->valid_lft = valid_lft;
1026 ifa->prefered_lft = prefered_lft;
1027 ifa->cstamp = ifa->tstamp = jiffies;
1028 ifa->tokenized = false;
1037 /* Add to big hash table */
1038 hash = inet6_addr_hash(addr);
1040 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1041 spin_unlock(&addrconf_hash_lock);
1043 write_lock(&idev->lock);
1044 /* Add to inet6_dev unicast addr list. */
1045 ipv6_link_dev_addr(idev, ifa);
1047 if (ifa->flags&IFA_F_TEMPORARY) {
1048 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1053 write_unlock(&idev->lock);
1055 rcu_read_unlock_bh();
1057 if (likely(err == 0))
1058 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1066 spin_unlock(&addrconf_hash_lock);
1070 enum cleanup_prefix_rt_t {
1071 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1072 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1073 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1077 * Check, whether the prefix for ifp would still need a prefix route
1078 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1081 * 1) we don't purge prefix if address was not permanent.
1082 * prefix is managed by its own lifetime.
1083 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1084 * 3) if there are no addresses, delete prefix.
1085 * 4) if there are still other permanent address(es),
1086 * corresponding prefix is still permanent.
1087 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1088 * don't purge the prefix, assume user space is managing it.
1089 * 6) otherwise, update prefix lifetime to the
1090 * longest valid lifetime among the corresponding
1091 * addresses on the device.
1092 * Note: subsequent RA will update lifetime.
1094 static enum cleanup_prefix_rt_t
1095 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1097 struct inet6_ifaddr *ifa;
1098 struct inet6_dev *idev = ifp->idev;
1099 unsigned long lifetime;
1100 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1104 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1107 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1110 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1111 return CLEANUP_PREFIX_RT_NOP;
1113 action = CLEANUP_PREFIX_RT_EXPIRE;
1115 spin_lock(&ifa->lock);
1117 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1119 * Note: Because this address is
1120 * not permanent, lifetime <
1121 * LONG_MAX / HZ here.
1123 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1124 *expires = ifa->tstamp + lifetime * HZ;
1125 spin_unlock(&ifa->lock);
1132 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1134 struct rt6_info *rt;
1136 rt = addrconf_get_prefix_route(&ifp->addr,
1139 0, RTF_GATEWAY | RTF_DEFAULT);
1144 if (!(rt->rt6i_flags & RTF_EXPIRES))
1145 rt6_set_expires(rt, expires);
1152 /* This function wants to get referenced ifp and releases it before return */
1154 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1157 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1158 unsigned long expires;
1162 spin_lock_bh(&ifp->lock);
1164 ifp->state = INET6_IFADDR_STATE_DEAD;
1165 spin_unlock_bh(&ifp->lock);
1167 if (state == INET6_IFADDR_STATE_DEAD)
1170 spin_lock_bh(&addrconf_hash_lock);
1171 hlist_del_init_rcu(&ifp->addr_lst);
1172 spin_unlock_bh(&addrconf_hash_lock);
1174 write_lock_bh(&ifp->idev->lock);
1176 if (ifp->flags&IFA_F_TEMPORARY) {
1177 list_del(&ifp->tmp_list);
1179 in6_ifa_put(ifp->ifpub);
1185 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1186 action = check_cleanup_prefix_route(ifp, &expires);
1188 list_del_init(&ifp->if_list);
1191 write_unlock_bh(&ifp->idev->lock);
1193 addrconf_del_dad_work(ifp);
1195 ipv6_ifa_notify(RTM_DELADDR, ifp);
1197 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1199 if (action != CLEANUP_PREFIX_RT_NOP) {
1200 cleanup_prefix_route(ifp, expires,
1201 action == CLEANUP_PREFIX_RT_DEL);
1204 /* clean up prefsrc entries */
1205 rt6_remove_prefsrc(ifp);
1210 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1212 struct inet6_dev *idev = ifp->idev;
1213 struct in6_addr addr, *tmpaddr;
1214 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1215 unsigned long regen_advance;
1219 unsigned long now = jiffies;
1220 long max_desync_factor;
1221 s32 cnf_temp_preferred_lft;
1223 write_lock_bh(&idev->lock);
1225 spin_lock_bh(&ift->lock);
1226 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1227 spin_unlock_bh(&ift->lock);
1234 if (idev->cnf.use_tempaddr <= 0) {
1235 write_unlock_bh(&idev->lock);
1236 pr_info("%s: use_tempaddr is disabled\n", __func__);
1241 spin_lock_bh(&ifp->lock);
1242 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1243 idev->cnf.use_tempaddr = -1; /*XXX*/
1244 spin_unlock_bh(&ifp->lock);
1245 write_unlock_bh(&idev->lock);
1246 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1253 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1254 ipv6_try_regen_rndid(idev, tmpaddr);
1255 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1256 age = (now - ifp->tstamp) / HZ;
1258 regen_advance = idev->cnf.regen_max_retry *
1259 idev->cnf.dad_transmits *
1260 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1262 /* recalculate max_desync_factor each time and update
1263 * idev->desync_factor if it's larger
1265 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1266 max_desync_factor = min_t(__u32,
1267 idev->cnf.max_desync_factor,
1268 cnf_temp_preferred_lft - regen_advance);
1270 if (unlikely(idev->desync_factor > max_desync_factor)) {
1271 if (max_desync_factor > 0) {
1272 get_random_bytes(&idev->desync_factor,
1273 sizeof(idev->desync_factor));
1274 idev->desync_factor %= max_desync_factor;
1276 idev->desync_factor = 0;
1280 tmp_valid_lft = min_t(__u32,
1282 idev->cnf.temp_valid_lft + age);
1283 tmp_prefered_lft = cnf_temp_preferred_lft + age -
1284 idev->desync_factor;
1285 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1286 tmp_plen = ifp->prefix_len;
1287 tmp_tstamp = ifp->tstamp;
1288 spin_unlock_bh(&ifp->lock);
1290 write_unlock_bh(&idev->lock);
1292 /* A temporary address is created only if this calculated Preferred
1293 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1294 * an implementation must not create a temporary address with a zero
1295 * Preferred Lifetime.
1296 * Use age calculation as in addrconf_verify to avoid unnecessary
1297 * temporary addresses being generated.
1299 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1300 if (tmp_prefered_lft <= regen_advance + age) {
1307 addr_flags = IFA_F_TEMPORARY;
1308 /* set in addrconf_prefix_rcv() */
1309 if (ifp->flags & IFA_F_OPTIMISTIC)
1310 addr_flags |= IFA_F_OPTIMISTIC;
1312 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1313 ipv6_addr_scope(&addr), addr_flags,
1314 tmp_valid_lft, tmp_prefered_lft);
1318 pr_info("%s: retry temporary address regeneration\n", __func__);
1320 write_lock_bh(&idev->lock);
1324 spin_lock_bh(&ift->lock);
1327 ift->tstamp = tmp_tstamp;
1328 spin_unlock_bh(&ift->lock);
1330 addrconf_dad_start(ift);
1338 * Choose an appropriate source address (RFC3484)
1341 IPV6_SADDR_RULE_INIT = 0,
1342 IPV6_SADDR_RULE_LOCAL,
1343 IPV6_SADDR_RULE_SCOPE,
1344 IPV6_SADDR_RULE_PREFERRED,
1345 #ifdef CONFIG_IPV6_MIP6
1346 IPV6_SADDR_RULE_HOA,
1348 IPV6_SADDR_RULE_OIF,
1349 IPV6_SADDR_RULE_LABEL,
1350 IPV6_SADDR_RULE_PRIVACY,
1351 IPV6_SADDR_RULE_ORCHID,
1352 IPV6_SADDR_RULE_PREFIX,
1353 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1354 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1359 struct ipv6_saddr_score {
1362 struct inet6_ifaddr *ifa;
1363 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1368 struct ipv6_saddr_dst {
1369 const struct in6_addr *addr;
1376 static inline int ipv6_saddr_preferred(int type)
1378 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1383 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1385 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1386 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1392 static int ipv6_get_saddr_eval(struct net *net,
1393 struct ipv6_saddr_score *score,
1394 struct ipv6_saddr_dst *dst,
1399 if (i <= score->rule) {
1401 case IPV6_SADDR_RULE_SCOPE:
1402 ret = score->scopedist;
1404 case IPV6_SADDR_RULE_PREFIX:
1405 ret = score->matchlen;
1408 ret = !!test_bit(i, score->scorebits);
1414 case IPV6_SADDR_RULE_INIT:
1415 /* Rule 0: remember if hiscore is not ready yet */
1418 case IPV6_SADDR_RULE_LOCAL:
1419 /* Rule 1: Prefer same address */
1420 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1422 case IPV6_SADDR_RULE_SCOPE:
1423 /* Rule 2: Prefer appropriate scope
1428 * ---+--+-+---> scope
1430 * | d is scope of the destination.
1432 * | \ <- smaller scope is better if
1433 * B-15 | \ if scope is enough for destination.
1434 * | ret = B - scope (-1 <= scope >= d <= 15).
1436 * |/ <- greater is better
1437 * -C / if scope is not enough for destination.
1438 * /| ret = scope - C (-1 <= d < scope <= 15).
1440 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1441 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1442 * Assume B = 0 and we get C > 29.
1444 ret = __ipv6_addr_src_scope(score->addr_type);
1445 if (ret >= dst->scope)
1448 ret -= 128; /* 30 is enough */
1449 score->scopedist = ret;
1451 case IPV6_SADDR_RULE_PREFERRED:
1453 /* Rule 3: Avoid deprecated and optimistic addresses */
1454 u8 avoid = IFA_F_DEPRECATED;
1456 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1457 avoid |= IFA_F_OPTIMISTIC;
1458 ret = ipv6_saddr_preferred(score->addr_type) ||
1459 !(score->ifa->flags & avoid);
1462 #ifdef CONFIG_IPV6_MIP6
1463 case IPV6_SADDR_RULE_HOA:
1465 /* Rule 4: Prefer home address */
1466 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1467 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1471 case IPV6_SADDR_RULE_OIF:
1472 /* Rule 5: Prefer outgoing interface */
1473 ret = (!dst->ifindex ||
1474 dst->ifindex == score->ifa->idev->dev->ifindex);
1476 case IPV6_SADDR_RULE_LABEL:
1477 /* Rule 6: Prefer matching label */
1478 ret = ipv6_addr_label(net,
1479 &score->ifa->addr, score->addr_type,
1480 score->ifa->idev->dev->ifindex) == dst->label;
1482 case IPV6_SADDR_RULE_PRIVACY:
1484 /* Rule 7: Prefer public address
1485 * Note: prefer temporary address if use_tempaddr >= 2
1487 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1488 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1489 score->ifa->idev->cnf.use_tempaddr >= 2;
1490 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1493 case IPV6_SADDR_RULE_ORCHID:
1494 /* Rule 8-: Prefer ORCHID vs ORCHID or
1495 * non-ORCHID vs non-ORCHID
1497 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1498 ipv6_addr_orchid(dst->addr));
1500 case IPV6_SADDR_RULE_PREFIX:
1501 /* Rule 8: Use longest matching prefix */
1502 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1503 if (ret > score->ifa->prefix_len)
1504 ret = score->ifa->prefix_len;
1505 score->matchlen = ret;
1507 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1508 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1509 /* Optimistic addresses still have lower precedence than other
1510 * preferred addresses.
1512 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1520 __set_bit(i, score->scorebits);
1526 static int __ipv6_dev_get_saddr(struct net *net,
1527 struct ipv6_saddr_dst *dst,
1528 struct inet6_dev *idev,
1529 struct ipv6_saddr_score *scores,
1532 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1534 read_lock_bh(&idev->lock);
1535 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1539 * - Tentative Address (RFC2462 section 5.4)
1540 * - A tentative address is not considered
1541 * "assigned to an interface" in the traditional
1542 * sense, unless it is also flagged as optimistic.
1543 * - Candidate Source Address (section 4)
1544 * - In any case, anycast addresses, multicast
1545 * addresses, and the unspecified address MUST
1546 * NOT be included in a candidate set.
1548 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1549 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1552 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1554 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1555 score->addr_type & IPV6_ADDR_MULTICAST)) {
1556 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1562 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1564 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1565 int minihiscore, miniscore;
1567 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1568 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1570 if (minihiscore > miniscore) {
1571 if (i == IPV6_SADDR_RULE_SCOPE &&
1572 score->scopedist > 0) {
1575 * each remaining entry
1576 * has too small (not enough)
1577 * scope, because ifa entries
1578 * are sorted by their scope
1584 } else if (minihiscore < miniscore) {
1586 in6_ifa_put(hiscore->ifa);
1588 in6_ifa_hold(score->ifa);
1590 swap(hiscore, score);
1591 hiscore_idx = 1 - hiscore_idx;
1593 /* restore our iterator */
1594 score->ifa = hiscore->ifa;
1601 read_unlock_bh(&idev->lock);
1605 static int ipv6_get_saddr_master(struct net *net,
1606 const struct net_device *dst_dev,
1607 const struct net_device *master,
1608 struct ipv6_saddr_dst *dst,
1609 struct ipv6_saddr_score *scores,
1612 struct inet6_dev *idev;
1614 idev = __in6_dev_get(dst_dev);
1616 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1617 scores, hiscore_idx);
1619 idev = __in6_dev_get(master);
1621 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1622 scores, hiscore_idx);
1627 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1628 const struct in6_addr *daddr, unsigned int prefs,
1629 struct in6_addr *saddr)
1631 struct ipv6_saddr_score scores[2], *hiscore;
1632 struct ipv6_saddr_dst dst;
1633 struct inet6_dev *idev;
1634 struct net_device *dev;
1636 bool use_oif_addr = false;
1637 int hiscore_idx = 0;
1639 dst_type = __ipv6_addr_type(daddr);
1641 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1642 dst.scope = __ipv6_addr_src_scope(dst_type);
1643 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1646 scores[hiscore_idx].rule = -1;
1647 scores[hiscore_idx].ifa = NULL;
1651 /* Candidate Source Address (section 4)
1652 * - multicast and link-local destination address,
1653 * the set of candidate source address MUST only
1654 * include addresses assigned to interfaces
1655 * belonging to the same link as the outgoing
1657 * (- For site-local destination addresses, the
1658 * set of candidate source addresses MUST only
1659 * include addresses assigned to interfaces
1660 * belonging to the same site as the outgoing
1662 * - "It is RECOMMENDED that the candidate source addresses
1663 * be the set of unicast addresses assigned to the
1664 * interface that will be used to send to the destination
1665 * (the 'outgoing' interface)." (RFC 6724)
1668 idev = __in6_dev_get(dst_dev);
1669 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1670 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1671 (idev && idev->cnf.use_oif_addrs_only)) {
1672 use_oif_addr = true;
1678 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1680 const struct net_device *master;
1683 /* if dst_dev exists and is enslaved to an L3 device, then
1684 * prefer addresses from dst_dev and then the master over
1685 * any other enslaved devices in the L3 domain.
1687 master = l3mdev_master_dev_rcu(dst_dev);
1689 master_idx = master->ifindex;
1691 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1693 scores, hiscore_idx);
1695 if (scores[hiscore_idx].ifa)
1699 for_each_netdev_rcu(net, dev) {
1700 /* only consider addresses on devices in the
1703 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1705 idev = __in6_dev_get(dev);
1708 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1715 hiscore = &scores[hiscore_idx];
1717 return -EADDRNOTAVAIL;
1719 *saddr = hiscore->ifa->addr;
1720 in6_ifa_put(hiscore->ifa);
1723 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1725 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1728 struct inet6_ifaddr *ifp;
1729 int err = -EADDRNOTAVAIL;
1731 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1732 if (ifp->scope > IFA_LINK)
1734 if (ifp->scope == IFA_LINK &&
1735 !(ifp->flags & banned_flags)) {
1744 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1747 struct inet6_dev *idev;
1748 int err = -EADDRNOTAVAIL;
1751 idev = __in6_dev_get(dev);
1753 read_lock_bh(&idev->lock);
1754 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1755 read_unlock_bh(&idev->lock);
1761 static int ipv6_count_addresses(struct inet6_dev *idev)
1764 struct inet6_ifaddr *ifp;
1766 read_lock_bh(&idev->lock);
1767 list_for_each_entry(ifp, &idev->addr_list, if_list)
1769 read_unlock_bh(&idev->lock);
1773 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1774 const struct net_device *dev, int strict)
1776 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1778 EXPORT_SYMBOL(ipv6_chk_addr);
1780 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1781 const struct net_device *dev, int strict,
1784 struct inet6_ifaddr *ifp;
1785 unsigned int hash = inet6_addr_hash(addr);
1789 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1790 if (!net_eq(dev_net(ifp->idev->dev), net))
1792 /* Decouple optimistic from tentative for evaluation here.
1793 * Ban optimistic addresses explicitly, when required.
1795 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1796 ? (ifp->flags&~IFA_F_TENTATIVE)
1798 if (ipv6_addr_equal(&ifp->addr, addr) &&
1799 !(ifp_flags&banned_flags) &&
1800 (!dev || ifp->idev->dev == dev ||
1801 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1802 rcu_read_unlock_bh();
1807 rcu_read_unlock_bh();
1810 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1812 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1813 struct net_device *dev)
1815 unsigned int hash = inet6_addr_hash(addr);
1816 struct inet6_ifaddr *ifp;
1818 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1819 if (!net_eq(dev_net(ifp->idev->dev), net))
1821 if (ipv6_addr_equal(&ifp->addr, addr)) {
1822 if (!dev || ifp->idev->dev == dev)
1829 /* Compares an address/prefix_len with addresses on device @dev.
1830 * If one is found it returns true.
1832 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1833 const unsigned int prefix_len, struct net_device *dev)
1835 struct inet6_dev *idev;
1836 struct inet6_ifaddr *ifa;
1840 idev = __in6_dev_get(dev);
1842 read_lock_bh(&idev->lock);
1843 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1844 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1848 read_unlock_bh(&idev->lock);
1854 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1856 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1858 struct inet6_dev *idev;
1859 struct inet6_ifaddr *ifa;
1864 idev = __in6_dev_get(dev);
1866 read_lock_bh(&idev->lock);
1867 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1868 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1873 read_unlock_bh(&idev->lock);
1878 EXPORT_SYMBOL(ipv6_chk_prefix);
1880 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1881 struct net_device *dev, int strict)
1883 struct inet6_ifaddr *ifp, *result = NULL;
1884 unsigned int hash = inet6_addr_hash(addr);
1887 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1888 if (!net_eq(dev_net(ifp->idev->dev), net))
1890 if (ipv6_addr_equal(&ifp->addr, addr)) {
1891 if (!dev || ifp->idev->dev == dev ||
1892 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1899 rcu_read_unlock_bh();
1904 /* Gets referenced address, destroys ifaddr */
1906 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1909 ifp->flags |= IFA_F_DADFAILED;
1911 if (ifp->flags&IFA_F_PERMANENT) {
1912 spin_lock_bh(&ifp->lock);
1913 addrconf_del_dad_work(ifp);
1914 ifp->flags |= IFA_F_TENTATIVE;
1915 spin_unlock_bh(&ifp->lock);
1917 ipv6_ifa_notify(0, ifp);
1919 } else if (ifp->flags&IFA_F_TEMPORARY) {
1920 struct inet6_ifaddr *ifpub;
1921 spin_lock_bh(&ifp->lock);
1924 in6_ifa_hold(ifpub);
1925 spin_unlock_bh(&ifp->lock);
1926 ipv6_create_tempaddr(ifpub, ifp);
1929 spin_unlock_bh(&ifp->lock);
1937 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1941 spin_lock_bh(&ifp->lock);
1942 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1943 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1946 spin_unlock_bh(&ifp->lock);
1951 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1953 struct inet6_dev *idev = ifp->idev;
1954 struct net *net = dev_net(ifp->idev->dev);
1956 if (addrconf_dad_end(ifp)) {
1961 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1962 ifp->idev->dev->name, &ifp->addr);
1964 spin_lock_bh(&ifp->lock);
1966 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1967 int scope = ifp->scope;
1968 u32 flags = ifp->flags;
1969 struct in6_addr new_addr;
1970 struct inet6_ifaddr *ifp2;
1971 u32 valid_lft, preferred_lft;
1972 int pfxlen = ifp->prefix_len;
1973 int retries = ifp->stable_privacy_retry + 1;
1975 if (retries > net->ipv6.sysctl.idgen_retries) {
1976 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1977 ifp->idev->dev->name);
1981 new_addr = ifp->addr;
1982 if (ipv6_generate_stable_address(&new_addr, retries,
1986 valid_lft = ifp->valid_lft;
1987 preferred_lft = ifp->prefered_lft;
1989 spin_unlock_bh(&ifp->lock);
1991 if (idev->cnf.max_addresses &&
1992 ipv6_count_addresses(idev) >=
1993 idev->cnf.max_addresses)
1996 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1997 ifp->idev->dev->name);
1999 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
2000 scope, flags, valid_lft,
2005 spin_lock_bh(&ifp2->lock);
2006 ifp2->stable_privacy_retry = retries;
2007 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2008 spin_unlock_bh(&ifp2->lock);
2010 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2013 spin_lock_bh(&ifp->lock);
2017 /* transition from _POSTDAD to _ERRDAD */
2018 ifp->state = INET6_IFADDR_STATE_ERRDAD;
2019 spin_unlock_bh(&ifp->lock);
2021 addrconf_mod_dad_work(ifp, 0);
2025 /* Join to solicited addr multicast group.
2026 * caller must hold RTNL */
2027 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2029 struct in6_addr maddr;
2031 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2034 addrconf_addr_solict_mult(addr, &maddr);
2035 ipv6_dev_mc_inc(dev, &maddr);
2038 /* caller must hold RTNL */
2039 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2041 struct in6_addr maddr;
2043 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2046 addrconf_addr_solict_mult(addr, &maddr);
2047 __ipv6_dev_mc_dec(idev, &maddr);
2050 /* caller must hold RTNL */
2051 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2053 struct in6_addr addr;
2055 if (ifp->prefix_len >= 127) /* RFC 6164 */
2057 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2058 if (ipv6_addr_any(&addr))
2060 __ipv6_dev_ac_inc(ifp->idev, &addr);
2063 /* caller must hold RTNL */
2064 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2066 struct in6_addr addr;
2068 if (ifp->prefix_len >= 127) /* RFC 6164 */
2070 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2071 if (ipv6_addr_any(&addr))
2073 __ipv6_dev_ac_dec(ifp->idev, &addr);
2076 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2078 switch (dev->addr_len) {
2080 memcpy(eui, dev->dev_addr, 3);
2083 memcpy(eui + 5, dev->dev_addr + 3, 3);
2085 case EUI64_ADDR_LEN:
2086 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2096 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2098 union fwnet_hwaddr *ha;
2100 if (dev->addr_len != FWNET_ALEN)
2103 ha = (union fwnet_hwaddr *)dev->dev_addr;
2105 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2110 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2112 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2113 if (dev->addr_len != ARCNET_ALEN)
2116 eui[7] = *(u8 *)dev->dev_addr;
2120 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2122 if (dev->addr_len != INFINIBAND_ALEN)
2124 memcpy(eui, dev->dev_addr + 12, 8);
2129 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2133 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2134 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2135 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2136 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2137 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2138 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2142 memcpy(eui + 4, &addr, 4);
2146 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2148 if (dev->priv_flags & IFF_ISATAP)
2149 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2153 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2155 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2158 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2160 memcpy(eui, dev->perm_addr, 3);
2161 memcpy(eui + 5, dev->perm_addr + 3, 3);
2168 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2170 switch (dev->type) {
2173 return addrconf_ifid_eui48(eui, dev);
2175 return addrconf_ifid_arcnet(eui, dev);
2176 case ARPHRD_INFINIBAND:
2177 return addrconf_ifid_infiniband(eui, dev);
2179 return addrconf_ifid_sit(eui, dev);
2182 return addrconf_ifid_gre(eui, dev);
2183 case ARPHRD_6LOWPAN:
2184 return addrconf_ifid_6lowpan(eui, dev);
2185 case ARPHRD_IEEE1394:
2186 return addrconf_ifid_ieee1394(eui, dev);
2187 case ARPHRD_TUNNEL6:
2189 return addrconf_ifid_ip6tnl(eui, dev);
2194 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2197 struct inet6_ifaddr *ifp;
2199 read_lock_bh(&idev->lock);
2200 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2201 if (ifp->scope > IFA_LINK)
2203 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2204 memcpy(eui, ifp->addr.s6_addr+8, 8);
2209 read_unlock_bh(&idev->lock);
2213 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2214 static void ipv6_regen_rndid(struct inet6_dev *idev)
2217 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2218 idev->rndid[0] &= ~0x02;
2221 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2222 * check if generated address is not inappropriate
2224 * - Reserved subnet anycast (RFC 2526)
2225 * 11111101 11....11 1xxxxxxx
2226 * - ISATAP (RFC4214) 6.1
2227 * 00-00-5E-FE-xx-xx-xx-xx
2229 * - XXX: already assigned to an address on the device
2231 if (idev->rndid[0] == 0xfd &&
2232 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2233 (idev->rndid[7]&0x80))
2235 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2236 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2238 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2243 static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2245 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2246 ipv6_regen_rndid(idev);
2254 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2255 unsigned long expires, u32 flags)
2257 struct fib6_config cfg = {
2258 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2259 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2260 .fc_ifindex = dev->ifindex,
2261 .fc_expires = expires,
2263 .fc_flags = RTF_UP | flags,
2264 .fc_nlinfo.nl_net = dev_net(dev),
2265 .fc_protocol = RTPROT_KERNEL,
2270 /* Prevent useless cloning on PtP SIT.
2271 This thing is done here expecting that the whole
2272 class of non-broadcast devices need not cloning.
2274 #if IS_ENABLED(CONFIG_IPV6_SIT)
2275 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2276 cfg.fc_flags |= RTF_NONEXTHOP;
2279 ip6_route_add(&cfg);
2283 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2285 const struct net_device *dev,
2286 u32 flags, u32 noflags)
2288 struct fib6_node *fn;
2289 struct rt6_info *rt = NULL;
2290 struct fib6_table *table;
2291 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2293 table = fib6_get_table(dev_net(dev), tb_id);
2297 read_lock_bh(&table->tb6_lock);
2298 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2302 noflags |= RTF_CACHE;
2303 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2304 if (rt->dst.dev->ifindex != dev->ifindex)
2306 if ((rt->rt6i_flags & flags) != flags)
2308 if ((rt->rt6i_flags & noflags) != 0)
2314 read_unlock_bh(&table->tb6_lock);
2319 /* Create "default" multicast route to the interface */
2321 static void addrconf_add_mroute(struct net_device *dev)
2323 struct fib6_config cfg = {
2324 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2325 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2326 .fc_ifindex = dev->ifindex,
2329 .fc_nlinfo.nl_net = dev_net(dev),
2332 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2334 ip6_route_add(&cfg);
2337 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2339 struct inet6_dev *idev;
2343 idev = ipv6_find_idev(dev);
2345 return ERR_PTR(-ENOBUFS);
2347 if (idev->cnf.disable_ipv6)
2348 return ERR_PTR(-EACCES);
2350 /* Add default multicast route */
2351 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2352 addrconf_add_mroute(dev);
2357 static void manage_tempaddrs(struct inet6_dev *idev,
2358 struct inet6_ifaddr *ifp,
2359 __u32 valid_lft, __u32 prefered_lft,
2360 bool create, unsigned long now)
2363 struct inet6_ifaddr *ift;
2365 read_lock_bh(&idev->lock);
2366 /* update all temporary addresses in the list */
2367 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2368 int age, max_valid, max_prefered;
2370 if (ifp != ift->ifpub)
2373 /* RFC 4941 section 3.3:
2374 * If a received option will extend the lifetime of a public
2375 * address, the lifetimes of temporary addresses should
2376 * be extended, subject to the overall constraint that no
2377 * temporary addresses should ever remain "valid" or "preferred"
2378 * for a time longer than (TEMP_VALID_LIFETIME) or
2379 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2381 age = (now - ift->cstamp) / HZ;
2382 max_valid = idev->cnf.temp_valid_lft - age;
2386 max_prefered = idev->cnf.temp_prefered_lft -
2387 idev->desync_factor - age;
2388 if (max_prefered < 0)
2391 if (valid_lft > max_valid)
2392 valid_lft = max_valid;
2394 if (prefered_lft > max_prefered)
2395 prefered_lft = max_prefered;
2397 spin_lock(&ift->lock);
2399 ift->valid_lft = valid_lft;
2400 ift->prefered_lft = prefered_lft;
2402 if (prefered_lft > 0)
2403 ift->flags &= ~IFA_F_DEPRECATED;
2405 spin_unlock(&ift->lock);
2406 if (!(flags&IFA_F_TENTATIVE))
2407 ipv6_ifa_notify(0, ift);
2410 if ((create || list_empty(&idev->tempaddr_list)) &&
2411 idev->cnf.use_tempaddr > 0) {
2412 /* When a new public address is created as described
2413 * in [ADDRCONF], also create a new temporary address.
2414 * Also create a temporary address if it's enabled but
2415 * no temporary address currently exists.
2417 read_unlock_bh(&idev->lock);
2418 ipv6_create_tempaddr(ifp, NULL);
2420 read_unlock_bh(&idev->lock);
2424 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2426 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2427 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2430 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2431 const struct prefix_info *pinfo,
2432 struct inet6_dev *in6_dev,
2433 const struct in6_addr *addr, int addr_type,
2434 u32 addr_flags, bool sllao, bool tokenized,
2435 __u32 valid_lft, u32 prefered_lft)
2437 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2438 int create = 0, update_lft = 0;
2440 if (!ifp && valid_lft) {
2441 int max_addresses = in6_dev->cnf.max_addresses;
2443 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2444 if (in6_dev->cnf.optimistic_dad &&
2445 !net->ipv6.devconf_all->forwarding && sllao)
2446 addr_flags |= IFA_F_OPTIMISTIC;
2449 /* Do not allow to create too much of autoconfigured
2450 * addresses; this would be too easy way to crash kernel.
2452 if (!max_addresses ||
2453 ipv6_count_addresses(in6_dev) < max_addresses)
2454 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2456 addr_type&IPV6_ADDR_SCOPE_MASK,
2457 addr_flags, valid_lft,
2460 if (IS_ERR_OR_NULL(ifp))
2465 spin_lock_bh(&ifp->lock);
2466 ifp->flags |= IFA_F_MANAGETEMPADDR;
2467 ifp->cstamp = jiffies;
2468 ifp->tokenized = tokenized;
2469 spin_unlock_bh(&ifp->lock);
2470 addrconf_dad_start(ifp);
2478 /* update lifetime (RFC2462 5.5.3 e) */
2479 spin_lock_bh(&ifp->lock);
2481 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2482 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2485 if (!update_lft && !create && stored_lft) {
2486 const u32 minimum_lft = min_t(u32,
2487 stored_lft, MIN_VALID_LIFETIME);
2488 valid_lft = max(valid_lft, minimum_lft);
2490 /* RFC4862 Section 5.5.3e:
2491 * "Note that the preferred lifetime of the
2492 * corresponding address is always reset to
2493 * the Preferred Lifetime in the received
2494 * Prefix Information option, regardless of
2495 * whether the valid lifetime is also reset or
2498 * So we should always update prefered_lft here.
2504 ifp->valid_lft = valid_lft;
2505 ifp->prefered_lft = prefered_lft;
2508 ifp->flags &= ~IFA_F_DEPRECATED;
2509 spin_unlock_bh(&ifp->lock);
2511 if (!(flags&IFA_F_TENTATIVE))
2512 ipv6_ifa_notify(0, ifp);
2514 spin_unlock_bh(&ifp->lock);
2516 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2525 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2527 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2529 struct prefix_info *pinfo;
2534 struct inet6_dev *in6_dev;
2535 struct net *net = dev_net(dev);
2537 pinfo = (struct prefix_info *) opt;
2539 if (len < sizeof(struct prefix_info)) {
2540 ADBG("addrconf: prefix option too short\n");
2545 * Validation checks ([ADDRCONF], page 19)
2548 addr_type = ipv6_addr_type(&pinfo->prefix);
2550 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2553 valid_lft = ntohl(pinfo->valid);
2554 prefered_lft = ntohl(pinfo->prefered);
2556 if (prefered_lft > valid_lft) {
2557 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2561 in6_dev = in6_dev_get(dev);
2564 net_dbg_ratelimited("addrconf: device %s not configured\n",
2570 * Two things going on here:
2571 * 1) Add routes for on-link prefixes
2572 * 2) Configure prefixes with the auto flag set
2575 if (pinfo->onlink) {
2576 struct rt6_info *rt;
2577 unsigned long rt_expires;
2579 /* Avoid arithmetic overflow. Really, we could
2580 * save rt_expires in seconds, likely valid_lft,
2581 * but it would require division in fib gc, that it
2585 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2587 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2589 if (addrconf_finite_timeout(rt_expires))
2592 rt = addrconf_get_prefix_route(&pinfo->prefix,
2595 RTF_ADDRCONF | RTF_PREFIX_RT,
2596 RTF_GATEWAY | RTF_DEFAULT);
2599 /* Autoconf prefix route */
2600 if (valid_lft == 0) {
2603 } else if (addrconf_finite_timeout(rt_expires)) {
2605 rt6_set_expires(rt, jiffies + rt_expires);
2607 rt6_clean_expires(rt);
2609 } else if (valid_lft) {
2610 clock_t expires = 0;
2611 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2612 if (addrconf_finite_timeout(rt_expires)) {
2614 flags |= RTF_EXPIRES;
2615 expires = jiffies_to_clock_t(rt_expires);
2617 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2618 dev, expires, flags);
2623 /* Try to figure out our local address for this prefix */
2625 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2626 struct in6_addr addr;
2627 bool tokenized = false, dev_addr_generated = false;
2629 if (pinfo->prefix_len == 64) {
2630 memcpy(&addr, &pinfo->prefix, 8);
2632 if (!ipv6_addr_any(&in6_dev->token)) {
2633 read_lock_bh(&in6_dev->lock);
2634 memcpy(addr.s6_addr + 8,
2635 in6_dev->token.s6_addr + 8, 8);
2636 read_unlock_bh(&in6_dev->lock);
2638 } else if (is_addr_mode_generate_stable(in6_dev) &&
2639 !ipv6_generate_stable_address(&addr, 0,
2641 addr_flags |= IFA_F_STABLE_PRIVACY;
2643 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2644 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2647 dev_addr_generated = true;
2651 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2656 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2659 tokenized, valid_lft,
2664 /* Ignore error case here because previous prefix add addr was
2665 * successful which will be notified.
2667 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2668 addr_type, addr_flags, sllao,
2669 tokenized, valid_lft,
2671 dev_addr_generated);
2673 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2675 in6_dev_put(in6_dev);
2679 * Set destination address.
2680 * Special case for SIT interfaces where we create a new "virtual"
2683 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2685 struct in6_ifreq ireq;
2686 struct net_device *dev;
2692 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2695 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2701 #if IS_ENABLED(CONFIG_IPV6_SIT)
2702 if (dev->type == ARPHRD_SIT) {
2703 const struct net_device_ops *ops = dev->netdev_ops;
2705 struct ip_tunnel_parm p;
2707 err = -EADDRNOTAVAIL;
2708 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2711 memset(&p, 0, sizeof(p));
2712 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2716 p.iph.protocol = IPPROTO_IPV6;
2718 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2720 if (ops->ndo_do_ioctl) {
2721 mm_segment_t oldfs = get_fs();
2724 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2731 dev = __dev_get_by_name(net, p.name);
2734 err = dev_open(dev);
2744 static int ipv6_mc_config(struct sock *sk, bool join,
2745 const struct in6_addr *addr, int ifindex)
2753 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2755 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2762 * Manual configuration of address on an interface
2764 static int inet6_addr_add(struct net *net, int ifindex,
2765 const struct in6_addr *pfx,
2766 const struct in6_addr *peer_pfx,
2767 unsigned int plen, __u32 ifa_flags,
2768 __u32 prefered_lft, __u32 valid_lft)
2770 struct inet6_ifaddr *ifp;
2771 struct inet6_dev *idev;
2772 struct net_device *dev;
2773 unsigned long timeout;
2783 /* check the lifetime */
2784 if (!valid_lft || prefered_lft > valid_lft)
2787 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2790 dev = __dev_get_by_index(net, ifindex);
2794 idev = addrconf_add_dev(dev);
2796 return PTR_ERR(idev);
2798 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2799 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2800 true, pfx, ifindex);
2806 scope = ipv6_addr_scope(pfx);
2808 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2809 if (addrconf_finite_timeout(timeout)) {
2810 expires = jiffies_to_clock_t(timeout * HZ);
2811 valid_lft = timeout;
2812 flags = RTF_EXPIRES;
2816 ifa_flags |= IFA_F_PERMANENT;
2819 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2820 if (addrconf_finite_timeout(timeout)) {
2822 ifa_flags |= IFA_F_DEPRECATED;
2823 prefered_lft = timeout;
2826 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2827 valid_lft, prefered_lft);
2830 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2831 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2836 * Note that section 3.1 of RFC 4429 indicates
2837 * that the Optimistic flag should not be set for
2838 * manually configured addresses
2840 addrconf_dad_start(ifp);
2841 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2842 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2845 addrconf_verify_rtnl();
2847 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2848 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2849 false, pfx, ifindex);
2852 return PTR_ERR(ifp);
2855 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2856 const struct in6_addr *pfx, unsigned int plen)
2858 struct inet6_ifaddr *ifp;
2859 struct inet6_dev *idev;
2860 struct net_device *dev;
2865 dev = __dev_get_by_index(net, ifindex);
2869 idev = __in6_dev_get(dev);
2873 read_lock_bh(&idev->lock);
2874 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2875 if (ifp->prefix_len == plen &&
2876 ipv6_addr_equal(pfx, &ifp->addr)) {
2878 read_unlock_bh(&idev->lock);
2880 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2881 (ifa_flags & IFA_F_MANAGETEMPADDR))
2882 manage_tempaddrs(idev, ifp, 0, 0, false,
2885 addrconf_verify_rtnl();
2886 if (ipv6_addr_is_multicast(pfx)) {
2887 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2888 false, pfx, dev->ifindex);
2893 read_unlock_bh(&idev->lock);
2894 return -EADDRNOTAVAIL;
2898 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2900 struct in6_ifreq ireq;
2903 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2906 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2910 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2911 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2912 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2917 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2919 struct in6_ifreq ireq;
2922 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2925 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2929 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2930 ireq.ifr6_prefixlen);
2935 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2936 int plen, int scope)
2938 struct inet6_ifaddr *ifp;
2940 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2941 scope, IFA_F_PERMANENT,
2942 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2944 spin_lock_bh(&ifp->lock);
2945 ifp->flags &= ~IFA_F_TENTATIVE;
2946 spin_unlock_bh(&ifp->lock);
2947 rt_genid_bump_ipv6(dev_net(idev->dev));
2948 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2953 #if IS_ENABLED(CONFIG_IPV6_SIT)
2954 static void sit_add_v4_addrs(struct inet6_dev *idev)
2956 struct in6_addr addr;
2957 struct net_device *dev;
2958 struct net *net = dev_net(idev->dev);
2964 memset(&addr, 0, sizeof(struct in6_addr));
2965 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2967 if (idev->dev->flags&IFF_POINTOPOINT) {
2968 addr.s6_addr32[0] = htonl(0xfe800000);
2972 scope = IPV6_ADDR_COMPATv4;
2974 pflags |= RTF_NONEXTHOP;
2977 if (addr.s6_addr32[3]) {
2978 add_addr(idev, &addr, plen, scope);
2979 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2983 for_each_netdev(net, dev) {
2984 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2985 if (in_dev && (dev->flags & IFF_UP)) {
2986 struct in_ifaddr *ifa;
2990 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2992 addr.s6_addr32[3] = ifa->ifa_local;
2994 if (ifa->ifa_scope == RT_SCOPE_LINK)
2996 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2997 if (idev->dev->flags&IFF_POINTOPOINT)
3002 add_addr(idev, &addr, plen, flag);
3003 addrconf_prefix_route(&addr, plen, idev->dev, 0,
3011 static void init_loopback(struct net_device *dev)
3013 struct inet6_dev *idev;
3014 struct net_device *sp_dev;
3015 struct inet6_ifaddr *sp_ifa;
3016 struct rt6_info *sp_rt;
3022 idev = ipv6_find_idev(dev);
3024 pr_debug("%s: add_dev failed\n", __func__);
3028 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
3030 /* Add routes to other interface's IPv6 addresses */
3031 for_each_netdev(dev_net(dev), sp_dev) {
3032 if (!strcmp(sp_dev->name, dev->name))
3035 idev = __in6_dev_get(sp_dev);
3039 read_lock_bh(&idev->lock);
3040 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3042 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3046 /* This dst has been added to garbage list when
3047 * lo device down, release this obsolete dst and
3048 * reallocate a new router for ifa.
3050 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
3051 ip6_rt_put(sp_ifa->rt);
3058 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
3060 /* Failure cases are ignored */
3061 if (!IS_ERR(sp_rt)) {
3066 read_unlock_bh(&idev->lock);
3070 void addrconf_add_linklocal(struct inet6_dev *idev,
3071 const struct in6_addr *addr, u32 flags)
3073 struct inet6_ifaddr *ifp;
3074 u32 addr_flags = flags | IFA_F_PERMANENT;
3076 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3077 if (idev->cnf.optimistic_dad &&
3078 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3079 addr_flags |= IFA_F_OPTIMISTIC;
3082 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3083 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
3085 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3086 addrconf_dad_start(ifp);
3090 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3092 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3094 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3097 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3098 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3101 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3102 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3108 static int ipv6_generate_stable_address(struct in6_addr *address,
3110 const struct inet6_dev *idev)
3112 static DEFINE_SPINLOCK(lock);
3113 static __u32 digest[SHA_DIGEST_WORDS];
3114 static __u32 workspace[SHA_WORKSPACE_WORDS];
3117 char __data[SHA_MESSAGE_BYTES];
3119 struct in6_addr secret;
3121 unsigned char hwaddr[MAX_ADDR_LEN];
3126 struct in6_addr secret;
3127 struct in6_addr temp;
3128 struct net *net = dev_net(idev->dev);
3130 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3132 if (idev->cnf.stable_secret.initialized)
3133 secret = idev->cnf.stable_secret.secret;
3134 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3135 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3140 spin_lock_bh(&lock);
3143 memset(&data, 0, sizeof(data));
3144 memset(workspace, 0, sizeof(workspace));
3145 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3146 data.prefix[0] = address->s6_addr32[0];
3147 data.prefix[1] = address->s6_addr32[1];
3148 data.secret = secret;
3149 data.dad_count = dad_count;
3151 sha_transform(digest, data.__data, workspace);
3154 temp.s6_addr32[2] = (__force __be32)digest[0];
3155 temp.s6_addr32[3] = (__force __be32)digest[1];
3157 spin_unlock_bh(&lock);
3159 if (ipv6_reserved_interfaceid(temp)) {
3161 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3170 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3172 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3176 s = &idev->cnf.stable_secret;
3177 get_random_bytes(&s->secret, sizeof(s->secret));
3178 s->initialized = true;
3181 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3183 struct in6_addr addr;
3185 /* no link local addresses on L3 master devices */
3186 if (netif_is_l3_master(idev->dev))
3189 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3191 switch (idev->cnf.addr_gen_mode) {
3192 case IN6_ADDR_GEN_MODE_RANDOM:
3193 ipv6_gen_mode_random_init(idev);
3195 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3196 if (!ipv6_generate_stable_address(&addr, 0, idev))
3197 addrconf_add_linklocal(idev, &addr,
3198 IFA_F_STABLE_PRIVACY);
3199 else if (prefix_route)
3200 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3202 case IN6_ADDR_GEN_MODE_EUI64:
3203 /* addrconf_add_linklocal also adds a prefix_route and we
3204 * only need to care about prefix routes if ipv6_generate_eui64
3205 * couldn't generate one.
3207 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3208 addrconf_add_linklocal(idev, &addr, 0);
3209 else if (prefix_route)
3210 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3212 case IN6_ADDR_GEN_MODE_NONE:
3214 /* will not add any link local address */
3219 static void addrconf_dev_config(struct net_device *dev)
3221 struct inet6_dev *idev;
3225 if ((dev->type != ARPHRD_ETHER) &&
3226 (dev->type != ARPHRD_FDDI) &&
3227 (dev->type != ARPHRD_ARCNET) &&
3228 (dev->type != ARPHRD_INFINIBAND) &&
3229 (dev->type != ARPHRD_IEEE1394) &&
3230 (dev->type != ARPHRD_TUNNEL6) &&
3231 (dev->type != ARPHRD_6LOWPAN) &&
3232 (dev->type != ARPHRD_IP6GRE) &&
3233 (dev->type != ARPHRD_IPGRE) &&
3234 (dev->type != ARPHRD_TUNNEL) &&
3235 (dev->type != ARPHRD_NONE)) {
3236 /* Alas, we support only Ethernet autoconfiguration. */
3240 idev = addrconf_add_dev(dev);
3244 /* this device type has no EUI support */
3245 if (dev->type == ARPHRD_NONE &&
3246 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3247 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3249 addrconf_addr_gen(idev, false);
3252 #if IS_ENABLED(CONFIG_IPV6_SIT)
3253 static void addrconf_sit_config(struct net_device *dev)
3255 struct inet6_dev *idev;
3260 * Configure the tunnel with one of our IPv4
3261 * addresses... we should configure all of
3262 * our v4 addrs in the tunnel
3265 idev = ipv6_find_idev(dev);
3267 pr_debug("%s: add_dev failed\n", __func__);
3271 if (dev->priv_flags & IFF_ISATAP) {
3272 addrconf_addr_gen(idev, false);
3276 sit_add_v4_addrs(idev);
3278 if (dev->flags&IFF_POINTOPOINT)
3279 addrconf_add_mroute(dev);
3283 #if IS_ENABLED(CONFIG_NET_IPGRE)
3284 static void addrconf_gre_config(struct net_device *dev)
3286 struct inet6_dev *idev;
3290 idev = ipv6_find_idev(dev);
3292 pr_debug("%s: add_dev failed\n", __func__);
3296 addrconf_addr_gen(idev, true);
3297 if (dev->flags & IFF_POINTOPOINT)
3298 addrconf_add_mroute(dev);
3302 static int fixup_permanent_addr(struct inet6_dev *idev,
3303 struct inet6_ifaddr *ifp)
3306 struct rt6_info *rt;
3308 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3309 if (unlikely(IS_ERR(rt)))
3315 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3316 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3320 addrconf_dad_start(ifp);
3325 static void addrconf_permanent_addr(struct net_device *dev)
3327 struct inet6_ifaddr *ifp, *tmp;
3328 struct inet6_dev *idev;
3330 idev = __in6_dev_get(dev);
3334 write_lock_bh(&idev->lock);
3336 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3337 if ((ifp->flags & IFA_F_PERMANENT) &&
3338 fixup_permanent_addr(idev, ifp) < 0) {
3339 write_unlock_bh(&idev->lock);
3341 write_lock_bh(&idev->lock);
3343 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3344 idev->dev->name, &ifp->addr);
3348 write_unlock_bh(&idev->lock);
3351 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3354 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3355 struct netdev_notifier_changeupper_info *info;
3356 struct inet6_dev *idev = __in6_dev_get(dev);
3357 int run_pending = 0;
3361 case NETDEV_REGISTER:
3362 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3363 idev = ipv6_add_dev(dev);
3365 return notifier_from_errno(PTR_ERR(idev));
3369 case NETDEV_CHANGEMTU:
3370 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3371 if (dev->mtu < IPV6_MIN_MTU) {
3372 addrconf_ifdown(dev, 1);
3377 rt6_mtu_change(dev, dev->mtu);
3378 idev->cnf.mtu6 = dev->mtu;
3382 /* allocate new idev */
3383 idev = ipv6_add_dev(dev);
3387 /* device is still not ready */
3388 if (!(idev->if_flags & IF_READY))
3397 if (dev->flags & IFF_SLAVE)
3400 if (idev && idev->cnf.disable_ipv6)
3403 if (event == NETDEV_UP) {
3404 /* restore routes for permanent addresses */
3405 addrconf_permanent_addr(dev);
3407 if (!addrconf_qdisc_ok(dev)) {
3408 /* device is not ready yet. */
3409 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3414 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3415 idev = ipv6_add_dev(dev);
3417 if (!IS_ERR_OR_NULL(idev)) {
3418 idev->if_flags |= IF_READY;
3421 } else if (event == NETDEV_CHANGE) {
3422 if (!addrconf_qdisc_ok(dev)) {
3423 /* device is still not ready. */
3428 if (idev->if_flags & IF_READY) {
3429 /* device is already configured -
3430 * but resend MLD reports, we might
3431 * have roamed and need to update
3432 * multicast snooping switches
3437 idev->if_flags |= IF_READY;
3440 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3446 switch (dev->type) {
3447 #if IS_ENABLED(CONFIG_IPV6_SIT)
3449 addrconf_sit_config(dev);
3452 #if IS_ENABLED(CONFIG_NET_IPGRE)
3454 addrconf_gre_config(dev);
3457 case ARPHRD_LOOPBACK:
3462 addrconf_dev_config(dev);
3466 if (!IS_ERR_OR_NULL(idev)) {
3468 addrconf_dad_run(idev);
3471 * If the MTU changed during the interface down,
3472 * when the interface up, the changed MTU must be
3473 * reflected in the idev as well as routers.
3475 if (idev->cnf.mtu6 != dev->mtu &&
3476 dev->mtu >= IPV6_MIN_MTU) {
3477 rt6_mtu_change(dev, dev->mtu);
3478 idev->cnf.mtu6 = dev->mtu;
3480 idev->tstamp = jiffies;
3481 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3484 * If the changed mtu during down is lower than
3485 * IPV6_MIN_MTU stop IPv6 on this interface.
3487 if (dev->mtu < IPV6_MIN_MTU)
3488 addrconf_ifdown(dev, 1);
3493 case NETDEV_UNREGISTER:
3495 * Remove all addresses from this interface.
3497 addrconf_ifdown(dev, event != NETDEV_DOWN);
3500 case NETDEV_CHANGENAME:
3502 snmp6_unregister_dev(idev);
3503 addrconf_sysctl_unregister(idev);
3504 err = addrconf_sysctl_register(idev);
3506 return notifier_from_errno(err);
3507 err = snmp6_register_dev(idev);
3509 addrconf_sysctl_unregister(idev);
3510 return notifier_from_errno(err);
3515 case NETDEV_PRE_TYPE_CHANGE:
3516 case NETDEV_POST_TYPE_CHANGE:
3518 addrconf_type_change(dev, event);
3521 case NETDEV_CHANGEUPPER:
3524 /* flush all routes if dev is linked to or unlinked from
3525 * an L3 master device (e.g., VRF)
3527 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3528 addrconf_ifdown(dev, 0);
3535 * addrconf module should be notified of a device going up
3537 static struct notifier_block ipv6_dev_notf = {
3538 .notifier_call = addrconf_notify,
3541 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3543 struct inet6_dev *idev;
3546 idev = __in6_dev_get(dev);
3548 if (event == NETDEV_POST_TYPE_CHANGE)
3549 ipv6_mc_remap(idev);
3550 else if (event == NETDEV_PRE_TYPE_CHANGE)
3551 ipv6_mc_unmap(idev);
3554 static bool addr_is_local(const struct in6_addr *addr)
3556 return ipv6_addr_type(addr) &
3557 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3560 static int addrconf_ifdown(struct net_device *dev, int how)
3562 struct net *net = dev_net(dev);
3563 struct inet6_dev *idev;
3564 struct inet6_ifaddr *ifa, *tmp;
3565 struct list_head del_list;
3572 rt6_ifdown(net, dev);
3573 neigh_ifdown(&nd_tbl, dev);
3575 idev = __in6_dev_get(dev);
3580 * Step 1: remove reference to ipv6 device from parent device.
3586 /* protected by rtnl_lock */
3587 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3589 /* Step 1.5: remove snmp6 entry */
3590 snmp6_unregister_dev(idev);
3594 /* aggregate the system setting and interface setting */
3595 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3597 _keep_addr = idev->cnf.keep_addr_on_down;
3599 /* combine the user config with event to determine if permanent
3600 * addresses are to be removed from address hash table
3602 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3604 /* Step 2: clear hash table */
3605 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3606 struct hlist_head *h = &inet6_addr_lst[i];
3608 spin_lock_bh(&addrconf_hash_lock);
3610 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3611 if (ifa->idev == idev) {
3612 addrconf_del_dad_work(ifa);
3613 /* combined flag + permanent flag decide if
3614 * address is retained on a down event
3617 !(ifa->flags & IFA_F_PERMANENT) ||
3618 addr_is_local(&ifa->addr)) {
3619 hlist_del_init_rcu(&ifa->addr_lst);
3624 spin_unlock_bh(&addrconf_hash_lock);
3627 write_lock_bh(&idev->lock);
3629 addrconf_del_rs_timer(idev);
3631 /* Step 2: clear flags for stateless addrconf */
3633 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3635 /* Step 3: clear tempaddr list */
3636 while (!list_empty(&idev->tempaddr_list)) {
3637 ifa = list_first_entry(&idev->tempaddr_list,
3638 struct inet6_ifaddr, tmp_list);
3639 list_del(&ifa->tmp_list);
3640 write_unlock_bh(&idev->lock);
3641 spin_lock_bh(&ifa->lock);
3644 in6_ifa_put(ifa->ifpub);
3647 spin_unlock_bh(&ifa->lock);
3649 write_lock_bh(&idev->lock);
3652 /* re-combine the user config with event to determine if permanent
3653 * addresses are to be removed from the interface list
3655 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3657 INIT_LIST_HEAD(&del_list);
3658 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3659 struct rt6_info *rt = NULL;
3662 addrconf_del_dad_work(ifa);
3664 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3665 !addr_is_local(&ifa->addr);
3667 list_move(&ifa->if_list, &del_list);
3669 write_unlock_bh(&idev->lock);
3670 spin_lock_bh(&ifa->lock);
3673 /* set state to skip the notifier below */
3674 state = INET6_IFADDR_STATE_DEAD;
3676 if (!(ifa->flags & IFA_F_NODAD))
3677 ifa->flags |= IFA_F_TENTATIVE;
3683 ifa->state = INET6_IFADDR_STATE_DEAD;
3686 spin_unlock_bh(&ifa->lock);
3691 if (state != INET6_IFADDR_STATE_DEAD) {
3692 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3693 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3695 if (idev->cnf.forwarding)
3696 addrconf_leave_anycast(ifa);
3697 addrconf_leave_solict(ifa->idev, &ifa->addr);
3700 write_lock_bh(&idev->lock);
3703 write_unlock_bh(&idev->lock);
3705 /* now clean up addresses to be removed */
3706 while (!list_empty(&del_list)) {
3707 ifa = list_first_entry(&del_list,
3708 struct inet6_ifaddr, if_list);
3709 list_del(&ifa->if_list);
3714 /* Step 5: Discard anycast and multicast list */
3716 ipv6_ac_destroy_dev(idev);
3717 ipv6_mc_destroy_dev(idev);
3722 idev->tstamp = jiffies;
3724 /* Last: Shot the device (if unregistered) */
3726 addrconf_sysctl_unregister(idev);
3727 neigh_parms_release(&nd_tbl, idev->nd_parms);
3728 neigh_ifdown(&nd_tbl, dev);
3734 static void addrconf_rs_timer(unsigned long data)
3736 struct inet6_dev *idev = (struct inet6_dev *)data;
3737 struct net_device *dev = idev->dev;
3738 struct in6_addr lladdr;
3740 write_lock(&idev->lock);
3741 if (idev->dead || !(idev->if_flags & IF_READY))
3744 if (!ipv6_accept_ra(idev))
3747 /* Announcement received after solicitation was sent */
3748 if (idev->if_flags & IF_RA_RCVD)
3751 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3752 write_unlock(&idev->lock);
3753 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3754 ndisc_send_rs(dev, &lladdr,
3755 &in6addr_linklocal_allrouters);
3759 write_lock(&idev->lock);
3760 idev->rs_interval = rfc3315_s14_backoff_update(
3761 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3762 /* The wait after the last probe can be shorter */
3763 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3764 idev->cnf.rtr_solicits) ?
3765 idev->cnf.rtr_solicit_delay :
3769 * Note: we do not support deprecated "all on-link"
3770 * assumption any longer.
3772 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3776 write_unlock(&idev->lock);
3782 * Duplicate Address Detection
3784 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3786 unsigned long rand_num;
3787 struct inet6_dev *idev = ifp->idev;
3790 if (ifp->flags & IFA_F_OPTIMISTIC)
3793 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3796 if (idev->cnf.enhanced_dad ||
3797 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3799 get_random_bytes(&nonce, 6);
3802 ifp->dad_nonce = nonce;
3803 ifp->dad_probes = idev->cnf.dad_transmits;
3804 addrconf_mod_dad_work(ifp, rand_num);
3807 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3809 struct inet6_dev *idev = ifp->idev;
3810 struct net_device *dev = idev->dev;
3811 bool bump_id, notify = false;
3813 addrconf_join_solict(dev, &ifp->addr);
3815 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3817 read_lock_bh(&idev->lock);
3818 spin_lock(&ifp->lock);
3819 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3822 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3823 idev->cnf.accept_dad < 1 ||
3824 !(ifp->flags&IFA_F_TENTATIVE) ||
3825 ifp->flags & IFA_F_NODAD) {
3826 bump_id = ifp->flags & IFA_F_TENTATIVE;
3827 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3828 spin_unlock(&ifp->lock);
3829 read_unlock_bh(&idev->lock);
3831 addrconf_dad_completed(ifp, bump_id);
3835 if (!(idev->if_flags & IF_READY)) {
3836 spin_unlock(&ifp->lock);
3837 read_unlock_bh(&idev->lock);
3839 * If the device is not ready:
3840 * - keep it tentative if it is a permanent address.
3841 * - otherwise, kill it.
3844 addrconf_dad_stop(ifp, 0);
3849 * Optimistic nodes can start receiving
3852 if (ifp->flags & IFA_F_OPTIMISTIC) {
3853 ip6_ins_rt(ifp->rt);
3854 if (ipv6_use_optimistic_addr(idev)) {
3855 /* Because optimistic nodes can use this address,
3856 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3862 addrconf_dad_kick(ifp);
3864 spin_unlock(&ifp->lock);
3865 read_unlock_bh(&idev->lock);
3867 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3870 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3872 bool begin_dad = false;
3874 spin_lock_bh(&ifp->lock);
3875 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3876 ifp->state = INET6_IFADDR_STATE_PREDAD;
3879 spin_unlock_bh(&ifp->lock);
3882 addrconf_mod_dad_work(ifp, 0);
3885 static void addrconf_dad_work(struct work_struct *w)
3887 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3888 struct inet6_ifaddr,
3890 struct inet6_dev *idev = ifp->idev;
3891 bool bump_id, disable_ipv6 = false;
3892 struct in6_addr mcaddr;
3898 } action = DAD_PROCESS;
3902 spin_lock_bh(&ifp->lock);
3903 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3905 ifp->state = INET6_IFADDR_STATE_DAD;
3906 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3908 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3910 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3911 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3912 struct in6_addr addr;
3914 addr.s6_addr32[0] = htonl(0xfe800000);
3915 addr.s6_addr32[1] = 0;
3917 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3918 ipv6_addr_equal(&ifp->addr, &addr)) {
3919 /* DAD failed for link-local based on MAC */
3920 idev->cnf.disable_ipv6 = 1;
3922 pr_info("%s: IPv6 being disabled!\n",
3923 ifp->idev->dev->name);
3924 disable_ipv6 = true;
3928 spin_unlock_bh(&ifp->lock);
3930 if (action == DAD_BEGIN) {
3931 addrconf_dad_begin(ifp);
3933 } else if (action == DAD_ABORT) {
3935 addrconf_dad_stop(ifp, 1);
3937 addrconf_ifdown(idev->dev, 0);
3941 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3944 write_lock_bh(&idev->lock);
3945 if (idev->dead || !(idev->if_flags & IF_READY)) {
3946 write_unlock_bh(&idev->lock);
3950 spin_lock(&ifp->lock);
3951 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3952 spin_unlock(&ifp->lock);
3953 write_unlock_bh(&idev->lock);
3957 if (ifp->dad_probes == 0) {
3959 * DAD was successful
3962 bump_id = ifp->flags & IFA_F_TENTATIVE;
3963 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3964 spin_unlock(&ifp->lock);
3965 write_unlock_bh(&idev->lock);
3967 addrconf_dad_completed(ifp, bump_id);
3973 addrconf_mod_dad_work(ifp,
3974 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3975 spin_unlock(&ifp->lock);
3976 write_unlock_bh(&idev->lock);
3978 /* send a neighbour solicitation for our addr */
3979 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3980 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3987 /* ifp->idev must be at least read locked */
3988 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3990 struct inet6_ifaddr *ifpiter;
3991 struct inet6_dev *idev = ifp->idev;
3993 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3994 if (ifpiter->scope > IFA_LINK)
3996 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3997 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3998 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4005 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
4007 struct net_device *dev = ifp->idev->dev;
4008 struct in6_addr lladdr;
4009 bool send_rs, send_mld;
4011 addrconf_del_dad_work(ifp);
4014 * Configure the address for reception. Now it is valid.
4017 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4019 /* If added prefix is link local and we are prepared to process
4020 router advertisements, start sending router solicitations.
4023 read_lock_bh(&ifp->idev->lock);
4024 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4025 send_rs = send_mld &&
4026 ipv6_accept_ra(ifp->idev) &&
4027 ifp->idev->cnf.rtr_solicits != 0 &&
4028 (dev->flags&IFF_LOOPBACK) == 0;
4029 read_unlock_bh(&ifp->idev->lock);
4031 /* While dad is in progress mld report's source address is in6_addrany.
4032 * Resend with proper ll now.
4035 ipv6_mc_dad_complete(ifp->idev);
4039 * If a host as already performed a random delay
4040 * [...] as part of DAD [...] there is no need
4041 * to delay again before sending the first RS
4043 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4045 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4047 write_lock_bh(&ifp->idev->lock);
4048 spin_lock(&ifp->lock);
4049 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4050 ifp->idev->cnf.rtr_solicit_interval);
4051 ifp->idev->rs_probes = 1;
4052 ifp->idev->if_flags |= IF_RS_SENT;
4053 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4054 spin_unlock(&ifp->lock);
4055 write_unlock_bh(&ifp->idev->lock);
4059 rt_genid_bump_ipv6(dev_net(dev));
4061 /* Make sure that a new temporary address will be created
4062 * before this temporary address becomes deprecated.
4064 if (ifp->flags & IFA_F_TEMPORARY)
4065 addrconf_verify_rtnl();
4068 static void addrconf_dad_run(struct inet6_dev *idev)
4070 struct inet6_ifaddr *ifp;
4072 read_lock_bh(&idev->lock);
4073 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4074 spin_lock(&ifp->lock);
4075 if (ifp->flags & IFA_F_TENTATIVE &&
4076 ifp->state == INET6_IFADDR_STATE_DAD)
4077 addrconf_dad_kick(ifp);
4078 spin_unlock(&ifp->lock);
4080 read_unlock_bh(&idev->lock);
4083 #ifdef CONFIG_PROC_FS
4084 struct if6_iter_state {
4085 struct seq_net_private p;
4090 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4092 struct inet6_ifaddr *ifa = NULL;
4093 struct if6_iter_state *state = seq->private;
4094 struct net *net = seq_file_net(seq);
4097 /* initial bucket if pos is 0 */
4103 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4104 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
4106 if (!net_eq(dev_net(ifa->idev->dev), net))
4108 /* sync with offset */
4109 if (p < state->offset) {
4117 /* prepare for next bucket */
4124 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4125 struct inet6_ifaddr *ifa)
4127 struct if6_iter_state *state = seq->private;
4128 struct net *net = seq_file_net(seq);
4130 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
4131 if (!net_eq(dev_net(ifa->idev->dev), net))
4137 while (++state->bucket < IN6_ADDR_HSIZE) {
4139 hlist_for_each_entry_rcu_bh(ifa,
4140 &inet6_addr_lst[state->bucket], addr_lst) {
4141 if (!net_eq(dev_net(ifa->idev->dev), net))
4151 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4155 return if6_get_first(seq, *pos);
4158 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4160 struct inet6_ifaddr *ifa;
4162 ifa = if6_get_next(seq, v);
4167 static void if6_seq_stop(struct seq_file *seq, void *v)
4170 rcu_read_unlock_bh();
4173 static int if6_seq_show(struct seq_file *seq, void *v)
4175 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4176 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4178 ifp->idev->dev->ifindex,
4182 ifp->idev->dev->name);
4186 static const struct seq_operations if6_seq_ops = {
4187 .start = if6_seq_start,
4188 .next = if6_seq_next,
4189 .show = if6_seq_show,
4190 .stop = if6_seq_stop,
4193 static int if6_seq_open(struct inode *inode, struct file *file)
4195 return seq_open_net(inode, file, &if6_seq_ops,
4196 sizeof(struct if6_iter_state));
4199 static const struct file_operations if6_fops = {
4200 .owner = THIS_MODULE,
4201 .open = if6_seq_open,
4203 .llseek = seq_lseek,
4204 .release = seq_release_net,
4207 static int __net_init if6_proc_net_init(struct net *net)
4209 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
4214 static void __net_exit if6_proc_net_exit(struct net *net)
4216 remove_proc_entry("if_inet6", net->proc_net);
4219 static struct pernet_operations if6_proc_net_ops = {
4220 .init = if6_proc_net_init,
4221 .exit = if6_proc_net_exit,
4224 int __init if6_proc_init(void)
4226 return register_pernet_subsys(&if6_proc_net_ops);
4229 void if6_proc_exit(void)
4231 unregister_pernet_subsys(&if6_proc_net_ops);
4233 #endif /* CONFIG_PROC_FS */
4235 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4236 /* Check if address is a home address configured on any interface. */
4237 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4240 struct inet6_ifaddr *ifp = NULL;
4241 unsigned int hash = inet6_addr_hash(addr);
4244 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4245 if (!net_eq(dev_net(ifp->idev->dev), net))
4247 if (ipv6_addr_equal(&ifp->addr, addr) &&
4248 (ifp->flags & IFA_F_HOMEADDRESS)) {
4253 rcu_read_unlock_bh();
4259 * Periodic address status verification
4262 static void addrconf_verify_rtnl(void)
4264 unsigned long now, next, next_sec, next_sched;
4265 struct inet6_ifaddr *ifp;
4272 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4274 cancel_delayed_work(&addr_chk_work);
4276 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4278 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4281 /* When setting preferred_lft to a value not zero or
4282 * infinity, while valid_lft is infinity
4283 * IFA_F_PERMANENT has a non-infinity life time.
4285 if ((ifp->flags & IFA_F_PERMANENT) &&
4286 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4289 spin_lock(&ifp->lock);
4290 /* We try to batch several events at once. */
4291 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4293 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4294 age >= ifp->valid_lft) {
4295 spin_unlock(&ifp->lock);
4299 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4300 spin_unlock(&ifp->lock);
4302 } else if (age >= ifp->prefered_lft) {
4303 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4306 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4308 ifp->flags |= IFA_F_DEPRECATED;
4311 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4312 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4313 next = ifp->tstamp + ifp->valid_lft * HZ;
4315 spin_unlock(&ifp->lock);
4320 ipv6_ifa_notify(0, ifp);
4324 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4325 !(ifp->flags&IFA_F_TENTATIVE)) {
4326 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4327 ifp->idev->cnf.dad_transmits *
4328 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4330 if (age >= ifp->prefered_lft - regen_advance) {
4331 struct inet6_ifaddr *ifpub = ifp->ifpub;
4332 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4333 next = ifp->tstamp + ifp->prefered_lft * HZ;
4334 if (!ifp->regen_count && ifpub) {
4337 in6_ifa_hold(ifpub);
4338 spin_unlock(&ifp->lock);
4340 spin_lock(&ifpub->lock);
4341 ifpub->regen_count = 0;
4342 spin_unlock(&ifpub->lock);
4343 ipv6_create_tempaddr(ifpub, ifp);
4348 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4349 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4350 spin_unlock(&ifp->lock);
4352 /* ifp->prefered_lft <= ifp->valid_lft */
4353 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4354 next = ifp->tstamp + ifp->prefered_lft * HZ;
4355 spin_unlock(&ifp->lock);
4360 next_sec = round_jiffies_up(next);
4363 /* If rounded timeout is accurate enough, accept it. */
4364 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4365 next_sched = next_sec;
4367 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4368 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4369 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4371 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4372 now, next, next_sec, next_sched);
4373 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4374 rcu_read_unlock_bh();
4377 static void addrconf_verify_work(struct work_struct *w)
4380 addrconf_verify_rtnl();
4384 static void addrconf_verify(void)
4386 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4389 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4390 struct in6_addr **peer_pfx)
4392 struct in6_addr *pfx = NULL;
4397 pfx = nla_data(addr);
4400 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4402 pfx = nla_data(local);
4408 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4409 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4410 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4411 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4412 [IFA_FLAGS] = { .len = sizeof(u32) },
4416 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4418 struct net *net = sock_net(skb->sk);
4419 struct ifaddrmsg *ifm;
4420 struct nlattr *tb[IFA_MAX+1];
4421 struct in6_addr *pfx, *peer_pfx;
4425 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4430 ifm = nlmsg_data(nlh);
4431 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4435 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4437 /* We ignore other flags so far. */
4438 ifa_flags &= IFA_F_MANAGETEMPADDR;
4440 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4441 ifm->ifa_prefixlen);
4444 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4445 u32 prefered_lft, u32 valid_lft)
4449 unsigned long timeout;
4450 bool was_managetempaddr;
4451 bool had_prefixroute;
4455 if (!valid_lft || (prefered_lft > valid_lft))
4458 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4459 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4462 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4463 if (addrconf_finite_timeout(timeout)) {
4464 expires = jiffies_to_clock_t(timeout * HZ);
4465 valid_lft = timeout;
4466 flags = RTF_EXPIRES;
4470 ifa_flags |= IFA_F_PERMANENT;
4473 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4474 if (addrconf_finite_timeout(timeout)) {
4476 ifa_flags |= IFA_F_DEPRECATED;
4477 prefered_lft = timeout;
4480 spin_lock_bh(&ifp->lock);
4481 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4482 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4483 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4484 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4485 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4486 IFA_F_NOPREFIXROUTE);
4487 ifp->flags |= ifa_flags;
4488 ifp->tstamp = jiffies;
4489 ifp->valid_lft = valid_lft;
4490 ifp->prefered_lft = prefered_lft;
4492 spin_unlock_bh(&ifp->lock);
4493 if (!(ifp->flags&IFA_F_TENTATIVE))
4494 ipv6_ifa_notify(0, ifp);
4496 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4497 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4499 } else if (had_prefixroute) {
4500 enum cleanup_prefix_rt_t action;
4501 unsigned long rt_expires;
4503 write_lock_bh(&ifp->idev->lock);
4504 action = check_cleanup_prefix_route(ifp, &rt_expires);
4505 write_unlock_bh(&ifp->idev->lock);
4507 if (action != CLEANUP_PREFIX_RT_NOP) {
4508 cleanup_prefix_route(ifp, rt_expires,
4509 action == CLEANUP_PREFIX_RT_DEL);
4513 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4514 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4515 valid_lft = prefered_lft = 0;
4516 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4517 !was_managetempaddr, jiffies);
4520 addrconf_verify_rtnl();
4526 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4528 struct net *net = sock_net(skb->sk);
4529 struct ifaddrmsg *ifm;
4530 struct nlattr *tb[IFA_MAX+1];
4531 struct in6_addr *pfx, *peer_pfx;
4532 struct inet6_ifaddr *ifa;
4533 struct net_device *dev;
4534 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4538 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4543 ifm = nlmsg_data(nlh);
4544 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4548 if (tb[IFA_CACHEINFO]) {
4549 struct ifa_cacheinfo *ci;
4551 ci = nla_data(tb[IFA_CACHEINFO]);
4552 valid_lft = ci->ifa_valid;
4553 preferred_lft = ci->ifa_prefered;
4555 preferred_lft = INFINITY_LIFE_TIME;
4556 valid_lft = INFINITY_LIFE_TIME;
4559 dev = __dev_get_by_index(net, ifm->ifa_index);
4563 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4565 /* We ignore other flags so far. */
4566 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4567 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4569 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4572 * It would be best to check for !NLM_F_CREATE here but
4573 * userspace already relies on not having to provide this.
4575 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4576 ifm->ifa_prefixlen, ifa_flags,
4577 preferred_lft, valid_lft);
4580 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4581 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4584 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4591 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4592 u8 scope, int ifindex)
4594 struct ifaddrmsg *ifm;
4596 ifm = nlmsg_data(nlh);
4597 ifm->ifa_family = AF_INET6;
4598 ifm->ifa_prefixlen = prefixlen;
4599 ifm->ifa_flags = flags;
4600 ifm->ifa_scope = scope;
4601 ifm->ifa_index = ifindex;
4604 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4605 unsigned long tstamp, u32 preferred, u32 valid)
4607 struct ifa_cacheinfo ci;
4609 ci.cstamp = cstamp_delta(cstamp);
4610 ci.tstamp = cstamp_delta(tstamp);
4611 ci.ifa_prefered = preferred;
4612 ci.ifa_valid = valid;
4614 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4617 static inline int rt_scope(int ifa_scope)
4619 if (ifa_scope & IFA_HOST)
4620 return RT_SCOPE_HOST;
4621 else if (ifa_scope & IFA_LINK)
4622 return RT_SCOPE_LINK;
4623 else if (ifa_scope & IFA_SITE)
4624 return RT_SCOPE_SITE;
4626 return RT_SCOPE_UNIVERSE;
4629 static inline int inet6_ifaddr_msgsize(void)
4631 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4632 + nla_total_size(16) /* IFA_LOCAL */
4633 + nla_total_size(16) /* IFA_ADDRESS */
4634 + nla_total_size(sizeof(struct ifa_cacheinfo))
4635 + nla_total_size(4) /* IFA_FLAGS */;
4638 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4639 u32 portid, u32 seq, int event, unsigned int flags)
4641 struct nlmsghdr *nlh;
4642 u32 preferred, valid;
4644 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4648 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4649 ifa->idev->dev->ifindex);
4651 if (!((ifa->flags&IFA_F_PERMANENT) &&
4652 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4653 preferred = ifa->prefered_lft;
4654 valid = ifa->valid_lft;
4655 if (preferred != INFINITY_LIFE_TIME) {
4656 long tval = (jiffies - ifa->tstamp)/HZ;
4657 if (preferred > tval)
4661 if (valid != INFINITY_LIFE_TIME) {
4669 preferred = INFINITY_LIFE_TIME;
4670 valid = INFINITY_LIFE_TIME;
4673 if (!ipv6_addr_any(&ifa->peer_addr)) {
4674 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4675 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4678 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4681 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4684 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4687 nlmsg_end(skb, nlh);
4691 nlmsg_cancel(skb, nlh);
4695 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4696 u32 portid, u32 seq, int event, u16 flags)
4698 struct nlmsghdr *nlh;
4699 u8 scope = RT_SCOPE_UNIVERSE;
4700 int ifindex = ifmca->idev->dev->ifindex;
4702 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4703 scope = RT_SCOPE_SITE;
4705 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4709 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4710 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4711 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4712 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4713 nlmsg_cancel(skb, nlh);
4717 nlmsg_end(skb, nlh);
4721 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4722 u32 portid, u32 seq, int event, unsigned int flags)
4724 struct nlmsghdr *nlh;
4725 u8 scope = RT_SCOPE_UNIVERSE;
4726 int ifindex = ifaca->aca_idev->dev->ifindex;
4728 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4729 scope = RT_SCOPE_SITE;
4731 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4735 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4736 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4737 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4738 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4739 nlmsg_cancel(skb, nlh);
4743 nlmsg_end(skb, nlh);
4753 /* called with rcu_read_lock() */
4754 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4755 struct netlink_callback *cb, enum addr_type_t type,
4756 int s_ip_idx, int *p_ip_idx)
4758 struct ifmcaddr6 *ifmca;
4759 struct ifacaddr6 *ifaca;
4761 int ip_idx = *p_ip_idx;
4763 read_lock_bh(&idev->lock);
4765 case UNICAST_ADDR: {
4766 struct inet6_ifaddr *ifa;
4768 /* unicast address incl. temp addr */
4769 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4770 if (++ip_idx < s_ip_idx)
4772 err = inet6_fill_ifaddr(skb, ifa,
4773 NETLINK_CB(cb->skb).portid,
4779 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4783 case MULTICAST_ADDR:
4784 /* multicast address */
4785 for (ifmca = idev->mc_list; ifmca;
4786 ifmca = ifmca->next, ip_idx++) {
4787 if (ip_idx < s_ip_idx)
4789 err = inet6_fill_ifmcaddr(skb, ifmca,
4790 NETLINK_CB(cb->skb).portid,
4799 /* anycast address */
4800 for (ifaca = idev->ac_list; ifaca;
4801 ifaca = ifaca->aca_next, ip_idx++) {
4802 if (ip_idx < s_ip_idx)
4804 err = inet6_fill_ifacaddr(skb, ifaca,
4805 NETLINK_CB(cb->skb).portid,
4816 read_unlock_bh(&idev->lock);
4821 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4822 enum addr_type_t type)
4824 struct net *net = sock_net(skb->sk);
4827 int s_idx, s_ip_idx;
4828 struct net_device *dev;
4829 struct inet6_dev *idev;
4830 struct hlist_head *head;
4833 s_idx = idx = cb->args[1];
4834 s_ip_idx = ip_idx = cb->args[2];
4837 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4838 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4840 head = &net->dev_index_head[h];
4841 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4844 if (h > s_h || idx > s_idx)
4847 idev = __in6_dev_get(dev);
4851 if (in6_dump_addrs(idev, skb, cb, type,
4852 s_ip_idx, &ip_idx) < 0)
4862 cb->args[2] = ip_idx;
4867 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4869 enum addr_type_t type = UNICAST_ADDR;
4871 return inet6_dump_addr(skb, cb, type);
4874 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4876 enum addr_type_t type = MULTICAST_ADDR;
4878 return inet6_dump_addr(skb, cb, type);
4882 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4884 enum addr_type_t type = ANYCAST_ADDR;
4886 return inet6_dump_addr(skb, cb, type);
4889 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4891 struct net *net = sock_net(in_skb->sk);
4892 struct ifaddrmsg *ifm;
4893 struct nlattr *tb[IFA_MAX+1];
4894 struct in6_addr *addr = NULL, *peer;
4895 struct net_device *dev = NULL;
4896 struct inet6_ifaddr *ifa;
4897 struct sk_buff *skb;
4900 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
4905 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4911 ifm = nlmsg_data(nlh);
4913 dev = __dev_get_by_index(net, ifm->ifa_index);
4915 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4917 err = -EADDRNOTAVAIL;
4921 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4927 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4928 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4930 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4931 WARN_ON(err == -EMSGSIZE);
4935 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4942 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4944 struct sk_buff *skb;
4945 struct net *net = dev_net(ifa->idev->dev);
4948 /* Don't send DELADDR notification for TENTATIVE address,
4949 * since NEWADDR notification is sent only after removing
4952 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4955 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4959 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4961 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4962 WARN_ON(err == -EMSGSIZE);
4966 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4970 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4973 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4974 __s32 *array, int bytes)
4976 BUG_ON(bytes < (DEVCONF_MAX * 4));
4978 memset(array, 0, bytes);
4979 array[DEVCONF_FORWARDING] = cnf->forwarding;
4980 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4981 array[DEVCONF_MTU6] = cnf->mtu6;
4982 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4983 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4984 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4985 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4986 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4987 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4988 jiffies_to_msecs(cnf->rtr_solicit_interval);
4989 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4990 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
4991 array[DEVCONF_RTR_SOLICIT_DELAY] =
4992 jiffies_to_msecs(cnf->rtr_solicit_delay);
4993 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4994 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4995 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4996 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4997 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4998 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4999 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5000 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5001 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5002 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
5003 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
5004 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
5005 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
5006 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
5007 #ifdef CONFIG_IPV6_ROUTER_PREF
5008 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
5009 array[DEVCONF_RTR_PROBE_INTERVAL] =
5010 jiffies_to_msecs(cnf->rtr_probe_interval);
5011 #ifdef CONFIG_IPV6_ROUTE_INFO
5012 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
5013 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5016 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
5017 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
5018 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5019 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
5020 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
5022 #ifdef CONFIG_IPV6_MROUTE
5023 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5025 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
5026 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
5027 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
5028 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
5029 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
5030 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
5031 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
5032 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
5033 /* we omit DEVCONF_STABLE_SECRET for now */
5034 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
5035 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
5036 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
5037 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
5038 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
5039 #ifdef CONFIG_IPV6_SEG6_HMAC
5040 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5042 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
5043 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
5044 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
5047 static inline size_t inet6_ifla6_size(void)
5049 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5050 + nla_total_size(sizeof(struct ifla_cacheinfo))
5051 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5052 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5053 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5054 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
5057 static inline size_t inet6_if_nlmsg_size(void)
5059 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5060 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5061 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5062 + nla_total_size(4) /* IFLA_MTU */
5063 + nla_total_size(4) /* IFLA_LINK */
5064 + nla_total_size(1) /* IFLA_OPERSTATE */
5065 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5068 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5072 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5075 /* Use put_unaligned() because stats may not be aligned for u64. */
5076 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5077 for (i = 1; i < ICMP6_MIB_MAX; i++)
5078 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5080 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5083 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5084 int bytes, size_t syncpoff)
5087 u64 buff[IPSTATS_MIB_MAX];
5088 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5092 memset(buff, 0, sizeof(buff));
5093 buff[0] = IPSTATS_MIB_MAX;
5095 for_each_possible_cpu(c) {
5096 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5097 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5100 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5101 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5104 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5108 case IFLA_INET6_STATS:
5109 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5110 offsetof(struct ipstats_mib, syncp));
5112 case IFLA_INET6_ICMP6STATS:
5113 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5118 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5119 u32 ext_filter_mask)
5122 struct ifla_cacheinfo ci;
5124 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5125 goto nla_put_failure;
5126 ci.max_reasm_len = IPV6_MAXPLEN;
5127 ci.tstamp = cstamp_delta(idev->tstamp);
5128 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5129 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5130 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5131 goto nla_put_failure;
5132 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5134 goto nla_put_failure;
5135 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5137 /* XXX - MC not implemented */
5139 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5142 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5144 goto nla_put_failure;
5145 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5147 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5149 goto nla_put_failure;
5150 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5152 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5154 goto nla_put_failure;
5156 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
5157 goto nla_put_failure;
5159 read_lock_bh(&idev->lock);
5160 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5161 read_unlock_bh(&idev->lock);
5169 static size_t inet6_get_link_af_size(const struct net_device *dev,
5170 u32 ext_filter_mask)
5172 if (!__in6_dev_get(dev))
5175 return inet6_ifla6_size();
5178 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5179 u32 ext_filter_mask)
5181 struct inet6_dev *idev = __in6_dev_get(dev);
5186 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5192 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5194 struct inet6_ifaddr *ifp;
5195 struct net_device *dev = idev->dev;
5196 bool clear_token, update_rs = false;
5197 struct in6_addr ll_addr;
5203 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5205 if (!ipv6_accept_ra(idev))
5207 if (idev->cnf.rtr_solicits == 0)
5210 write_lock_bh(&idev->lock);
5212 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5213 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5215 write_unlock_bh(&idev->lock);
5217 clear_token = ipv6_addr_any(token);
5221 if (!idev->dead && (idev->if_flags & IF_READY) &&
5222 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5223 IFA_F_OPTIMISTIC)) {
5224 /* If we're not ready, then normal ifup will take care
5225 * of this. Otherwise, we need to request our rs here.
5227 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5232 write_lock_bh(&idev->lock);
5235 idev->if_flags |= IF_RS_SENT;
5236 idev->rs_interval = rfc3315_s14_backoff_init(
5237 idev->cnf.rtr_solicit_interval);
5238 idev->rs_probes = 1;
5239 addrconf_mod_rs_timer(idev, idev->rs_interval);
5242 /* Well, that's kinda nasty ... */
5243 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5244 spin_lock(&ifp->lock);
5245 if (ifp->tokenized) {
5247 ifp->prefered_lft = 0;
5249 spin_unlock(&ifp->lock);
5252 write_unlock_bh(&idev->lock);
5253 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5254 addrconf_verify_rtnl();
5258 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5259 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5260 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5263 static int inet6_validate_link_af(const struct net_device *dev,
5264 const struct nlattr *nla)
5266 struct nlattr *tb[IFLA_INET6_MAX + 1];
5268 if (dev && !__in6_dev_get(dev))
5269 return -EAFNOSUPPORT;
5271 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5275 static int check_addr_gen_mode(int mode)
5277 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5278 mode != IN6_ADDR_GEN_MODE_NONE &&
5279 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5280 mode != IN6_ADDR_GEN_MODE_RANDOM)
5285 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5288 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5289 !idev->cnf.stable_secret.initialized &&
5290 !net->ipv6.devconf_dflt->stable_secret.initialized)
5295 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5298 struct inet6_dev *idev = __in6_dev_get(dev);
5299 struct nlattr *tb[IFLA_INET6_MAX + 1];
5302 return -EAFNOSUPPORT;
5304 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
5307 if (tb[IFLA_INET6_TOKEN]) {
5308 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5313 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5314 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5316 if (check_addr_gen_mode(mode) < 0 ||
5317 check_stable_privacy(idev, dev_net(dev), mode) < 0)
5320 idev->cnf.addr_gen_mode = mode;
5327 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5328 u32 portid, u32 seq, int event, unsigned int flags)
5330 struct net_device *dev = idev->dev;
5331 struct ifinfomsg *hdr;
5332 struct nlmsghdr *nlh;
5335 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5339 hdr = nlmsg_data(nlh);
5340 hdr->ifi_family = AF_INET6;
5342 hdr->ifi_type = dev->type;
5343 hdr->ifi_index = dev->ifindex;
5344 hdr->ifi_flags = dev_get_flags(dev);
5345 hdr->ifi_change = 0;
5347 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5349 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5350 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5351 (dev->ifindex != dev_get_iflink(dev) &&
5352 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5353 nla_put_u8(skb, IFLA_OPERSTATE,
5354 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5355 goto nla_put_failure;
5356 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5358 goto nla_put_failure;
5360 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5361 goto nla_put_failure;
5363 nla_nest_end(skb, protoinfo);
5364 nlmsg_end(skb, nlh);
5368 nlmsg_cancel(skb, nlh);
5372 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5374 struct net *net = sock_net(skb->sk);
5377 struct net_device *dev;
5378 struct inet6_dev *idev;
5379 struct hlist_head *head;
5382 s_idx = cb->args[1];
5385 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5387 head = &net->dev_index_head[h];
5388 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5391 idev = __in6_dev_get(dev);
5394 if (inet6_fill_ifinfo(skb, idev,
5395 NETLINK_CB(cb->skb).portid,
5397 RTM_NEWLINK, NLM_F_MULTI) < 0)
5411 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5413 struct sk_buff *skb;
5414 struct net *net = dev_net(idev->dev);
5417 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5421 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5423 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5424 WARN_ON(err == -EMSGSIZE);
5428 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5432 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5435 static inline size_t inet6_prefix_nlmsg_size(void)
5437 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5438 + nla_total_size(sizeof(struct in6_addr))
5439 + nla_total_size(sizeof(struct prefix_cacheinfo));
5442 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5443 struct prefix_info *pinfo, u32 portid, u32 seq,
5444 int event, unsigned int flags)
5446 struct prefixmsg *pmsg;
5447 struct nlmsghdr *nlh;
5448 struct prefix_cacheinfo ci;
5450 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5454 pmsg = nlmsg_data(nlh);
5455 pmsg->prefix_family = AF_INET6;
5456 pmsg->prefix_pad1 = 0;
5457 pmsg->prefix_pad2 = 0;
5458 pmsg->prefix_ifindex = idev->dev->ifindex;
5459 pmsg->prefix_len = pinfo->prefix_len;
5460 pmsg->prefix_type = pinfo->type;
5461 pmsg->prefix_pad3 = 0;
5462 pmsg->prefix_flags = 0;
5464 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5465 if (pinfo->autoconf)
5466 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5468 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5469 goto nla_put_failure;
5470 ci.preferred_time = ntohl(pinfo->prefered);
5471 ci.valid_time = ntohl(pinfo->valid);
5472 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5473 goto nla_put_failure;
5474 nlmsg_end(skb, nlh);
5478 nlmsg_cancel(skb, nlh);
5482 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5483 struct prefix_info *pinfo)
5485 struct sk_buff *skb;
5486 struct net *net = dev_net(idev->dev);
5489 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5493 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5495 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5496 WARN_ON(err == -EMSGSIZE);
5500 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5504 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5507 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5509 struct net *net = dev_net(ifp->idev->dev);
5514 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5519 * If the address was optimistic
5520 * we inserted the route at the start of
5521 * our DAD process, so we don't need
5524 if (!(ifp->rt->rt6i_node))
5525 ip6_ins_rt(ifp->rt);
5526 if (ifp->idev->cnf.forwarding)
5527 addrconf_join_anycast(ifp);
5528 if (!ipv6_addr_any(&ifp->peer_addr))
5529 addrconf_prefix_route(&ifp->peer_addr, 128,
5530 ifp->idev->dev, 0, 0);
5533 if (ifp->idev->cnf.forwarding)
5534 addrconf_leave_anycast(ifp);
5535 addrconf_leave_solict(ifp->idev, &ifp->addr);
5536 if (!ipv6_addr_any(&ifp->peer_addr)) {
5537 struct rt6_info *rt;
5539 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5540 ifp->idev->dev, 0, 0);
5545 dst_hold(&ifp->rt->dst);
5546 ip6_del_rt(ifp->rt);
5548 rt_genid_bump_ipv6(net);
5551 atomic_inc(&net->ipv6.dev_addr_genid);
5554 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5557 if (likely(ifp->idev->dead == 0))
5558 __ipv6_ifa_notify(event, ifp);
5559 rcu_read_unlock_bh();
5562 #ifdef CONFIG_SYSCTL
5565 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5566 void __user *buffer, size_t *lenp, loff_t *ppos)
5568 int *valp = ctl->data;
5571 struct ctl_table lctl;
5575 * ctl->data points to idev->cnf.forwarding, we should
5576 * not modify it until we get the rtnl lock.
5581 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5584 ret = addrconf_fixup_forwarding(ctl, valp, val);
5591 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5592 void __user *buffer, size_t *lenp, loff_t *ppos)
5594 struct inet6_dev *idev = ctl->extra1;
5595 int min_mtu = IPV6_MIN_MTU;
5596 struct ctl_table lctl;
5599 lctl.extra1 = &min_mtu;
5600 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5602 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5605 static void dev_disable_change(struct inet6_dev *idev)
5607 struct netdev_notifier_info info;
5609 if (!idev || !idev->dev)
5612 netdev_notifier_info_init(&info, idev->dev);
5613 if (idev->cnf.disable_ipv6)
5614 addrconf_notify(NULL, NETDEV_DOWN, &info);
5616 addrconf_notify(NULL, NETDEV_UP, &info);
5619 static void addrconf_disable_change(struct net *net, __s32 newf)
5621 struct net_device *dev;
5622 struct inet6_dev *idev;
5624 for_each_netdev(net, dev) {
5625 idev = __in6_dev_get(dev);
5627 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5628 idev->cnf.disable_ipv6 = newf;
5630 dev_disable_change(idev);
5635 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5640 if (!rtnl_trylock())
5641 return restart_syscall();
5643 net = (struct net *)table->extra2;
5647 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5652 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5653 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5654 addrconf_disable_change(net, newf);
5655 } else if ((!newf) ^ (!old))
5656 dev_disable_change((struct inet6_dev *)table->extra1);
5663 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5664 void __user *buffer, size_t *lenp, loff_t *ppos)
5666 int *valp = ctl->data;
5669 struct ctl_table lctl;
5673 * ctl->data points to idev->cnf.disable_ipv6, we should
5674 * not modify it until we get the rtnl lock.
5679 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5682 ret = addrconf_disable_ipv6(ctl, valp, val);
5689 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5690 void __user *buffer, size_t *lenp, loff_t *ppos)
5692 int *valp = ctl->data;
5697 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5700 if (write && old != new) {
5701 struct net *net = ctl->extra2;
5703 if (!rtnl_trylock())
5704 return restart_syscall();
5706 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5707 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5708 NETCONFA_PROXY_NEIGH,
5709 NETCONFA_IFINDEX_DEFAULT,
5710 net->ipv6.devconf_dflt);
5711 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5712 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5713 NETCONFA_PROXY_NEIGH,
5714 NETCONFA_IFINDEX_ALL,
5715 net->ipv6.devconf_all);
5717 struct inet6_dev *idev = ctl->extra1;
5719 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5720 NETCONFA_PROXY_NEIGH,
5730 static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5731 void __user *buffer, size_t *lenp,
5736 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5737 struct net *net = (struct net *)ctl->extra2;
5739 if (!rtnl_trylock())
5740 return restart_syscall();
5742 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5745 new_val = *((int *)ctl->data);
5747 if (check_addr_gen_mode(new_val) < 0) {
5752 /* request for default */
5753 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5754 ipv6_devconf_dflt.addr_gen_mode = new_val;
5756 /* request for individual net device */
5761 if (check_stable_privacy(idev, net, new_val) < 0) {
5766 if (idev->cnf.addr_gen_mode != new_val) {
5767 idev->cnf.addr_gen_mode = new_val;
5768 addrconf_dev_config(idev->dev);
5779 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5780 void __user *buffer, size_t *lenp,
5784 struct in6_addr addr;
5785 char str[IPV6_MAX_STRLEN];
5786 struct ctl_table lctl = *ctl;
5787 struct net *net = ctl->extra2;
5788 struct ipv6_stable_secret *secret = ctl->data;
5790 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5793 lctl.maxlen = IPV6_MAX_STRLEN;
5796 if (!rtnl_trylock())
5797 return restart_syscall();
5799 if (!write && !secret->initialized) {
5804 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5805 if (err >= sizeof(str)) {
5810 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5814 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5819 secret->initialized = true;
5820 secret->secret = addr;
5822 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5823 struct net_device *dev;
5825 for_each_netdev(net, dev) {
5826 struct inet6_dev *idev = __in6_dev_get(dev);
5829 idev->cnf.addr_gen_mode =
5830 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5834 struct inet6_dev *idev = ctl->extra1;
5836 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5846 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5848 void __user *buffer,
5852 int *valp = ctl->data;
5855 struct ctl_table lctl;
5858 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5859 * we should not modify it until we get the rtnl lock.
5864 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5867 ret = addrconf_fixup_linkdown(ctl, valp, val);
5874 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5878 rt->dst.flags |= DST_NOPOLICY;
5880 rt->dst.flags &= ~DST_NOPOLICY;
5885 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5887 struct inet6_ifaddr *ifa;
5889 read_lock_bh(&idev->lock);
5890 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5891 spin_lock(&ifa->lock);
5893 struct rt6_info *rt = ifa->rt;
5894 struct fib6_table *table = rt->rt6i_table;
5897 read_lock(&table->tb6_lock);
5898 addrconf_set_nopolicy(ifa->rt, val);
5899 if (rt->rt6i_pcpu) {
5900 for_each_possible_cpu(cpu) {
5901 struct rt6_info **rtp;
5903 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5904 addrconf_set_nopolicy(*rtp, val);
5907 read_unlock(&table->tb6_lock);
5909 spin_unlock(&ifa->lock);
5911 read_unlock_bh(&idev->lock);
5915 int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5917 struct inet6_dev *idev;
5920 if (!rtnl_trylock())
5921 return restart_syscall();
5925 net = (struct net *)ctl->extra2;
5926 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5931 if (valp == &net->ipv6.devconf_all->disable_policy) {
5932 struct net_device *dev;
5934 for_each_netdev(net, dev) {
5935 idev = __in6_dev_get(dev);
5937 addrconf_disable_policy_idev(idev, val);
5940 idev = (struct inet6_dev *)ctl->extra1;
5941 addrconf_disable_policy_idev(idev, val);
5949 int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5950 void __user *buffer, size_t *lenp,
5953 int *valp = ctl->data;
5956 struct ctl_table lctl;
5961 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5963 if (write && (*valp != val))
5964 ret = addrconf_disable_policy(ctl, valp, val);
5972 static int minus_one = -1;
5973 static const int one = 1;
5974 static const int two_five_five = 255;
5976 static const struct ctl_table addrconf_sysctl[] = {
5978 .procname = "forwarding",
5979 .data = &ipv6_devconf.forwarding,
5980 .maxlen = sizeof(int),
5982 .proc_handler = addrconf_sysctl_forward,
5985 .procname = "hop_limit",
5986 .data = &ipv6_devconf.hop_limit,
5987 .maxlen = sizeof(int),
5989 .proc_handler = proc_dointvec_minmax,
5990 .extra1 = (void *)&one,
5991 .extra2 = (void *)&two_five_five,
5995 .data = &ipv6_devconf.mtu6,
5996 .maxlen = sizeof(int),
5998 .proc_handler = addrconf_sysctl_mtu,
6001 .procname = "accept_ra",
6002 .data = &ipv6_devconf.accept_ra,
6003 .maxlen = sizeof(int),
6005 .proc_handler = proc_dointvec,
6008 .procname = "accept_redirects",
6009 .data = &ipv6_devconf.accept_redirects,
6010 .maxlen = sizeof(int),
6012 .proc_handler = proc_dointvec,
6015 .procname = "autoconf",
6016 .data = &ipv6_devconf.autoconf,
6017 .maxlen = sizeof(int),
6019 .proc_handler = proc_dointvec,
6022 .procname = "dad_transmits",
6023 .data = &ipv6_devconf.dad_transmits,
6024 .maxlen = sizeof(int),
6026 .proc_handler = proc_dointvec,
6029 .procname = "router_solicitations",
6030 .data = &ipv6_devconf.rtr_solicits,
6031 .maxlen = sizeof(int),
6033 .proc_handler = proc_dointvec_minmax,
6034 .extra1 = &minus_one,
6037 .procname = "router_solicitation_interval",
6038 .data = &ipv6_devconf.rtr_solicit_interval,
6039 .maxlen = sizeof(int),
6041 .proc_handler = proc_dointvec_jiffies,
6044 .procname = "router_solicitation_max_interval",
6045 .data = &ipv6_devconf.rtr_solicit_max_interval,
6046 .maxlen = sizeof(int),
6048 .proc_handler = proc_dointvec_jiffies,
6051 .procname = "router_solicitation_delay",
6052 .data = &ipv6_devconf.rtr_solicit_delay,
6053 .maxlen = sizeof(int),
6055 .proc_handler = proc_dointvec_jiffies,
6058 .procname = "force_mld_version",
6059 .data = &ipv6_devconf.force_mld_version,
6060 .maxlen = sizeof(int),
6062 .proc_handler = proc_dointvec,
6065 .procname = "mldv1_unsolicited_report_interval",
6067 &ipv6_devconf.mldv1_unsolicited_report_interval,
6068 .maxlen = sizeof(int),
6070 .proc_handler = proc_dointvec_ms_jiffies,
6073 .procname = "mldv2_unsolicited_report_interval",
6075 &ipv6_devconf.mldv2_unsolicited_report_interval,
6076 .maxlen = sizeof(int),
6078 .proc_handler = proc_dointvec_ms_jiffies,
6081 .procname = "use_tempaddr",
6082 .data = &ipv6_devconf.use_tempaddr,
6083 .maxlen = sizeof(int),
6085 .proc_handler = proc_dointvec,
6088 .procname = "temp_valid_lft",
6089 .data = &ipv6_devconf.temp_valid_lft,
6090 .maxlen = sizeof(int),
6092 .proc_handler = proc_dointvec,
6095 .procname = "temp_prefered_lft",
6096 .data = &ipv6_devconf.temp_prefered_lft,
6097 .maxlen = sizeof(int),
6099 .proc_handler = proc_dointvec,
6102 .procname = "regen_max_retry",
6103 .data = &ipv6_devconf.regen_max_retry,
6104 .maxlen = sizeof(int),
6106 .proc_handler = proc_dointvec,
6109 .procname = "max_desync_factor",
6110 .data = &ipv6_devconf.max_desync_factor,
6111 .maxlen = sizeof(int),
6113 .proc_handler = proc_dointvec,
6116 .procname = "max_addresses",
6117 .data = &ipv6_devconf.max_addresses,
6118 .maxlen = sizeof(int),
6120 .proc_handler = proc_dointvec,
6123 .procname = "accept_ra_defrtr",
6124 .data = &ipv6_devconf.accept_ra_defrtr,
6125 .maxlen = sizeof(int),
6127 .proc_handler = proc_dointvec,
6130 .procname = "accept_ra_min_hop_limit",
6131 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6132 .maxlen = sizeof(int),
6134 .proc_handler = proc_dointvec,
6137 .procname = "accept_ra_pinfo",
6138 .data = &ipv6_devconf.accept_ra_pinfo,
6139 .maxlen = sizeof(int),
6141 .proc_handler = proc_dointvec,
6143 #ifdef CONFIG_IPV6_ROUTER_PREF
6145 .procname = "accept_ra_rtr_pref",
6146 .data = &ipv6_devconf.accept_ra_rtr_pref,
6147 .maxlen = sizeof(int),
6149 .proc_handler = proc_dointvec,
6152 .procname = "router_probe_interval",
6153 .data = &ipv6_devconf.rtr_probe_interval,
6154 .maxlen = sizeof(int),
6156 .proc_handler = proc_dointvec_jiffies,
6158 #ifdef CONFIG_IPV6_ROUTE_INFO
6160 .procname = "accept_ra_rt_info_min_plen",
6161 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6162 .maxlen = sizeof(int),
6164 .proc_handler = proc_dointvec,
6167 .procname = "accept_ra_rt_info_max_plen",
6168 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6169 .maxlen = sizeof(int),
6171 .proc_handler = proc_dointvec,
6176 .procname = "proxy_ndp",
6177 .data = &ipv6_devconf.proxy_ndp,
6178 .maxlen = sizeof(int),
6180 .proc_handler = addrconf_sysctl_proxy_ndp,
6183 .procname = "accept_source_route",
6184 .data = &ipv6_devconf.accept_source_route,
6185 .maxlen = sizeof(int),
6187 .proc_handler = proc_dointvec,
6189 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6191 .procname = "optimistic_dad",
6192 .data = &ipv6_devconf.optimistic_dad,
6193 .maxlen = sizeof(int),
6195 .proc_handler = proc_dointvec,
6198 .procname = "use_optimistic",
6199 .data = &ipv6_devconf.use_optimistic,
6200 .maxlen = sizeof(int),
6202 .proc_handler = proc_dointvec,
6205 #ifdef CONFIG_IPV6_MROUTE
6207 .procname = "mc_forwarding",
6208 .data = &ipv6_devconf.mc_forwarding,
6209 .maxlen = sizeof(int),
6211 .proc_handler = proc_dointvec,
6215 .procname = "disable_ipv6",
6216 .data = &ipv6_devconf.disable_ipv6,
6217 .maxlen = sizeof(int),
6219 .proc_handler = addrconf_sysctl_disable,
6222 .procname = "accept_dad",
6223 .data = &ipv6_devconf.accept_dad,
6224 .maxlen = sizeof(int),
6226 .proc_handler = proc_dointvec,
6229 .procname = "force_tllao",
6230 .data = &ipv6_devconf.force_tllao,
6231 .maxlen = sizeof(int),
6233 .proc_handler = proc_dointvec
6236 .procname = "ndisc_notify",
6237 .data = &ipv6_devconf.ndisc_notify,
6238 .maxlen = sizeof(int),
6240 .proc_handler = proc_dointvec
6243 .procname = "suppress_frag_ndisc",
6244 .data = &ipv6_devconf.suppress_frag_ndisc,
6245 .maxlen = sizeof(int),
6247 .proc_handler = proc_dointvec
6250 .procname = "accept_ra_from_local",
6251 .data = &ipv6_devconf.accept_ra_from_local,
6252 .maxlen = sizeof(int),
6254 .proc_handler = proc_dointvec,
6257 .procname = "accept_ra_mtu",
6258 .data = &ipv6_devconf.accept_ra_mtu,
6259 .maxlen = sizeof(int),
6261 .proc_handler = proc_dointvec,
6264 .procname = "stable_secret",
6265 .data = &ipv6_devconf.stable_secret,
6266 .maxlen = IPV6_MAX_STRLEN,
6268 .proc_handler = addrconf_sysctl_stable_secret,
6271 .procname = "use_oif_addrs_only",
6272 .data = &ipv6_devconf.use_oif_addrs_only,
6273 .maxlen = sizeof(int),
6275 .proc_handler = proc_dointvec,
6278 .procname = "ignore_routes_with_linkdown",
6279 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6280 .maxlen = sizeof(int),
6282 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6285 .procname = "drop_unicast_in_l2_multicast",
6286 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6287 .maxlen = sizeof(int),
6289 .proc_handler = proc_dointvec,
6292 .procname = "drop_unsolicited_na",
6293 .data = &ipv6_devconf.drop_unsolicited_na,
6294 .maxlen = sizeof(int),
6296 .proc_handler = proc_dointvec,
6299 .procname = "keep_addr_on_down",
6300 .data = &ipv6_devconf.keep_addr_on_down,
6301 .maxlen = sizeof(int),
6303 .proc_handler = proc_dointvec,
6307 .procname = "seg6_enabled",
6308 .data = &ipv6_devconf.seg6_enabled,
6309 .maxlen = sizeof(int),
6311 .proc_handler = proc_dointvec,
6313 #ifdef CONFIG_IPV6_SEG6_HMAC
6315 .procname = "seg6_require_hmac",
6316 .data = &ipv6_devconf.seg6_require_hmac,
6317 .maxlen = sizeof(int),
6319 .proc_handler = proc_dointvec,
6323 .procname = "enhanced_dad",
6324 .data = &ipv6_devconf.enhanced_dad,
6325 .maxlen = sizeof(int),
6327 .proc_handler = proc_dointvec,
6330 .procname = "addr_gen_mode",
6331 .data = &ipv6_devconf.addr_gen_mode,
6332 .maxlen = sizeof(int),
6334 .proc_handler = addrconf_sysctl_addr_gen_mode,
6337 .procname = "disable_policy",
6338 .data = &ipv6_devconf.disable_policy,
6339 .maxlen = sizeof(int),
6341 .proc_handler = addrconf_sysctl_disable_policy,
6348 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6349 struct inet6_dev *idev, struct ipv6_devconf *p)
6352 struct ctl_table *table;
6353 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6355 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6359 for (i = 0; table[i].data; i++) {
6360 table[i].data += (char *)p - (char *)&ipv6_devconf;
6361 /* If one of these is already set, then it is not safe to
6362 * overwrite either of them: this makes proc_dointvec_minmax
6365 if (!table[i].extra1 && !table[i].extra2) {
6366 table[i].extra1 = idev; /* embedded; no ref */
6367 table[i].extra2 = net;
6371 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6373 p->sysctl_header = register_net_sysctl(net, path, table);
6374 if (!p->sysctl_header)
6377 if (!strcmp(dev_name, "all"))
6378 ifindex = NETCONFA_IFINDEX_ALL;
6379 else if (!strcmp(dev_name, "default"))
6380 ifindex = NETCONFA_IFINDEX_DEFAULT;
6382 ifindex = idev->dev->ifindex;
6383 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6393 static void __addrconf_sysctl_unregister(struct net *net,
6394 struct ipv6_devconf *p, int ifindex)
6396 struct ctl_table *table;
6398 if (!p->sysctl_header)
6401 table = p->sysctl_header->ctl_table_arg;
6402 unregister_net_sysctl_table(p->sysctl_header);
6403 p->sysctl_header = NULL;
6406 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
6409 static int addrconf_sysctl_register(struct inet6_dev *idev)
6413 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6416 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6417 &ndisc_ifinfo_sysctl_change);
6420 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6423 neigh_sysctl_unregister(idev->nd_parms);
6428 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6430 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6431 idev->dev->ifindex);
6432 neigh_sysctl_unregister(idev->nd_parms);
6438 static int __net_init addrconf_init_net(struct net *net)
6441 struct ipv6_devconf *all, *dflt;
6443 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6447 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6449 goto err_alloc_dflt;
6451 /* these will be inherited by all namespaces */
6452 dflt->autoconf = ipv6_defaults.autoconf;
6453 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6455 dflt->stable_secret.initialized = false;
6456 all->stable_secret.initialized = false;
6458 net->ipv6.devconf_all = all;
6459 net->ipv6.devconf_dflt = dflt;
6461 #ifdef CONFIG_SYSCTL
6462 err = __addrconf_sysctl_register(net, "all", NULL, all);
6466 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6472 #ifdef CONFIG_SYSCTL
6474 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
6484 static void __net_exit addrconf_exit_net(struct net *net)
6486 #ifdef CONFIG_SYSCTL
6487 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6488 NETCONFA_IFINDEX_DEFAULT);
6489 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6490 NETCONFA_IFINDEX_ALL);
6492 kfree(net->ipv6.devconf_dflt);
6493 kfree(net->ipv6.devconf_all);
6496 static struct pernet_operations addrconf_ops = {
6497 .init = addrconf_init_net,
6498 .exit = addrconf_exit_net,
6501 static struct rtnl_af_ops inet6_ops __read_mostly = {
6503 .fill_link_af = inet6_fill_link_af,
6504 .get_link_af_size = inet6_get_link_af_size,
6505 .validate_link_af = inet6_validate_link_af,
6506 .set_link_af = inet6_set_link_af,
6510 * Init / cleanup code
6513 int __init addrconf_init(void)
6515 struct inet6_dev *idev;
6518 err = ipv6_addr_label_init();
6520 pr_crit("%s: cannot initialize default policy table: %d\n",
6525 err = register_pernet_subsys(&addrconf_ops);
6529 addrconf_wq = create_workqueue("ipv6_addrconf");
6535 /* The addrconf netdev notifier requires that loopback_dev
6536 * has it's ipv6 private information allocated and setup
6537 * before it can bring up and give link-local addresses
6538 * to other devices which are up.
6540 * Unfortunately, loopback_dev is not necessarily the first
6541 * entry in the global dev_base list of net devices. In fact,
6542 * it is likely to be the very last entry on that list.
6543 * So this causes the notifier registry below to try and
6544 * give link-local addresses to all devices besides loopback_dev
6545 * first, then loopback_dev, which cases all the non-loopback_dev
6546 * devices to fail to get a link-local address.
6548 * So, as a temporary fix, allocate the ipv6 structure for
6549 * loopback_dev first by hand.
6550 * Longer term, all of the dependencies ipv6 has upon the loopback
6551 * device and it being up should be removed.
6554 idev = ipv6_add_dev(init_net.loopback_dev);
6557 err = PTR_ERR(idev);
6561 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6562 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6564 register_netdevice_notifier(&ipv6_dev_notf);
6568 rtnl_af_register(&inet6_ops);
6570 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6575 /* Only the first call to __rtnl_register can fail */
6576 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6577 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6578 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6579 inet6_dump_ifaddr, NULL);
6580 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6581 inet6_dump_ifmcaddr, NULL);
6582 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6583 inet6_dump_ifacaddr, NULL);
6584 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6585 inet6_netconf_dump_devconf, NULL);
6587 ipv6_addr_label_rtnl_register();
6591 rtnl_af_unregister(&inet6_ops);
6592 unregister_netdevice_notifier(&ipv6_dev_notf);
6594 destroy_workqueue(addrconf_wq);
6596 unregister_pernet_subsys(&addrconf_ops);
6598 ipv6_addr_label_cleanup();
6603 void addrconf_cleanup(void)
6605 struct net_device *dev;
6608 unregister_netdevice_notifier(&ipv6_dev_notf);
6609 unregister_pernet_subsys(&addrconf_ops);
6610 ipv6_addr_label_cleanup();
6614 __rtnl_af_unregister(&inet6_ops);
6616 /* clean dev list */
6617 for_each_netdev(&init_net, dev) {
6618 if (__in6_dev_get(dev) == NULL)
6620 addrconf_ifdown(dev, 1);
6622 addrconf_ifdown(init_net.loopback_dev, 2);
6627 spin_lock_bh(&addrconf_hash_lock);
6628 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6629 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
6630 spin_unlock_bh(&addrconf_hash_lock);
6631 cancel_delayed_work(&addr_chk_work);
6634 destroy_workqueue(addrconf_wq);