2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/in6.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_addr.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
60 #include <linux/sysctl.h>
62 #include <linux/capability.h>
63 #include <linux/delay.h>
64 #include <linux/notifier.h>
65 #include <linux/string.h>
66 #include <linux/hash.h>
68 #include <net/net_namespace.h>
72 #include <net/af_ieee802154.h>
73 #include <net/firewire.h>
75 #include <net/protocol.h>
76 #include <net/ndisc.h>
77 #include <net/ip6_route.h>
78 #include <net/addrconf.h>
81 #include <net/netlink.h>
82 #include <net/pkt_sched.h>
83 #include <linux/if_tunnel.h>
84 #include <linux/rtnetlink.h>
85 #include <linux/netconf.h>
86 #include <linux/random.h>
87 #include <linux/uaccess.h>
88 #include <asm/unaligned.h>
90 #include <linux/proc_fs.h>
91 #include <linux/seq_file.h>
92 #include <linux/export.h>
94 /* Set to 3 to get tracing... */
98 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
100 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
103 #define INFINITY_LIFE_TIME 0xFFFFFFFF
105 static inline u32 cstamp_delta(unsigned long cstamp)
107 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
111 static int addrconf_sysctl_register(struct inet6_dev *idev);
112 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
114 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
119 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
124 static void __ipv6_regen_rndid(struct inet6_dev *idev);
125 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
126 static void ipv6_regen_rndid(unsigned long data);
128 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
129 static int ipv6_count_addresses(struct inet6_dev *idev);
132 * Configured unicast address hash table
134 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
135 static DEFINE_SPINLOCK(addrconf_hash_lock);
137 static void addrconf_verify(void);
138 static void addrconf_verify_rtnl(void);
139 static void addrconf_verify_work(struct work_struct *);
141 static struct workqueue_struct *addrconf_wq;
142 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
144 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
145 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
147 static void addrconf_type_change(struct net_device *dev,
148 unsigned long event);
149 static int addrconf_ifdown(struct net_device *dev, int how);
151 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
153 const struct net_device *dev,
154 u32 flags, u32 noflags);
156 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
157 static void addrconf_dad_work(struct work_struct *w);
158 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
159 static void addrconf_dad_run(struct inet6_dev *idev);
160 static void addrconf_rs_timer(unsigned long data);
161 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
162 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
164 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
165 struct prefix_info *pinfo);
166 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
167 struct net_device *dev);
169 static struct ipv6_devconf ipv6_devconf __read_mostly = {
171 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
172 .mtu6 = IPV6_MIN_MTU,
174 .accept_redirects = 1,
176 .force_mld_version = 0,
177 .mldv1_unsolicited_report_interval = 10 * HZ,
178 .mldv2_unsolicited_report_interval = HZ,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
184 .temp_valid_lft = TEMP_VALID_LIFETIME,
185 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
186 .regen_max_retry = REGEN_MAX_RETRY,
187 .max_desync_factor = MAX_DESYNC_FACTOR,
188 .max_addresses = IPV6_MAX_ADDRESSES,
189 .accept_ra_defrtr = 1,
190 .accept_ra_from_local = 0,
191 .accept_ra_pinfo = 1,
192 #ifdef CONFIG_IPV6_ROUTER_PREF
193 .accept_ra_rtr_pref = 1,
194 .rtr_probe_interval = 60 * HZ,
195 #ifdef CONFIG_IPV6_ROUTE_INFO
196 .accept_ra_rt_info_max_plen = 0,
200 .accept_source_route = 0, /* we do not accept RH0 by default. */
203 .suppress_frag_ndisc = 1,
206 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
208 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
209 .mtu6 = IPV6_MIN_MTU,
211 .accept_redirects = 1,
213 .force_mld_version = 0,
214 .mldv1_unsolicited_report_interval = 10 * HZ,
215 .mldv2_unsolicited_report_interval = HZ,
217 .rtr_solicits = MAX_RTR_SOLICITATIONS,
218 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
219 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
221 .temp_valid_lft = TEMP_VALID_LIFETIME,
222 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
223 .regen_max_retry = REGEN_MAX_RETRY,
224 .max_desync_factor = MAX_DESYNC_FACTOR,
225 .max_addresses = IPV6_MAX_ADDRESSES,
226 .accept_ra_defrtr = 1,
227 .accept_ra_from_local = 0,
228 .accept_ra_pinfo = 1,
229 #ifdef CONFIG_IPV6_ROUTER_PREF
230 .accept_ra_rtr_pref = 1,
231 .rtr_probe_interval = 60 * HZ,
232 #ifdef CONFIG_IPV6_ROUTE_INFO
233 .accept_ra_rt_info_max_plen = 0,
237 .accept_source_route = 0, /* we do not accept RH0 by default. */
240 .suppress_frag_ndisc = 1,
243 /* Check if a valid qdisc is available */
244 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
246 return !qdisc_tx_is_noop(dev);
249 static void addrconf_del_rs_timer(struct inet6_dev *idev)
251 if (del_timer(&idev->rs_timer))
255 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
257 if (cancel_delayed_work(&ifp->dad_work))
261 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
264 if (!timer_pending(&idev->rs_timer))
266 mod_timer(&idev->rs_timer, jiffies + when);
269 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
272 if (!delayed_work_pending(&ifp->dad_work))
274 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
277 static int snmp6_alloc_dev(struct inet6_dev *idev)
281 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
282 if (!idev->stats.ipv6)
285 for_each_possible_cpu(i) {
286 struct ipstats_mib *addrconf_stats;
287 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
288 u64_stats_init(&addrconf_stats->syncp);
292 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
294 if (!idev->stats.icmpv6dev)
296 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
298 if (!idev->stats.icmpv6msgdev)
304 kfree(idev->stats.icmpv6dev);
306 free_percpu(idev->stats.ipv6);
311 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
313 struct inet6_dev *ndev;
318 if (dev->mtu < IPV6_MIN_MTU)
319 return ERR_PTR(-EINVAL);
321 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
325 rwlock_init(&ndev->lock);
327 INIT_LIST_HEAD(&ndev->addr_list);
328 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
329 (unsigned long)ndev);
330 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
331 ndev->cnf.mtu6 = dev->mtu;
332 ndev->cnf.sysctl = NULL;
333 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
334 if (ndev->nd_parms == NULL) {
338 if (ndev->cnf.forwarding)
339 dev_disable_lro(dev);
340 /* We refer to the device */
343 if (snmp6_alloc_dev(ndev) < 0) {
345 "%s: cannot allocate memory for statistics; dev=%s.\n",
346 __func__, dev->name);
347 neigh_parms_release(&nd_tbl, ndev->nd_parms);
353 if (snmp6_register_dev(ndev) < 0) {
355 "%s: cannot create /proc/net/dev_snmp6/%s\n",
356 __func__, dev->name);
360 /* One reference from device. We must do this before
361 * we invoke __ipv6_regen_rndid().
365 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
366 ndev->cnf.accept_dad = -1;
368 #if IS_ENABLED(CONFIG_IPV6_SIT)
369 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
370 pr_info("%s: Disabled Multicast RS\n", dev->name);
371 ndev->cnf.rtr_solicits = 0;
375 INIT_LIST_HEAD(&ndev->tempaddr_list);
376 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
377 if ((dev->flags&IFF_LOOPBACK) ||
378 dev->type == ARPHRD_TUNNEL ||
379 dev->type == ARPHRD_TUNNEL6 ||
380 dev->type == ARPHRD_SIT ||
381 dev->type == ARPHRD_NONE) {
382 ndev->cnf.use_tempaddr = -1;
385 ipv6_regen_rndid((unsigned long) ndev);
388 ndev->token = in6addr_any;
390 if (netif_running(dev) && addrconf_qdisc_ok(dev))
391 ndev->if_flags |= IF_READY;
393 ipv6_mc_init_dev(ndev);
394 ndev->tstamp = jiffies;
395 err = addrconf_sysctl_register(ndev);
397 ipv6_mc_destroy_dev(ndev);
398 del_timer(&ndev->regen_timer);
401 /* protected by rtnl_lock */
402 rcu_assign_pointer(dev->ip6_ptr, ndev);
404 /* Join interface-local all-node multicast group */
405 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
407 /* Join all-node multicast group */
408 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
410 /* Join all-router multicast group if forwarding is set */
411 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
412 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
417 neigh_parms_release(&nd_tbl, ndev->nd_parms);
419 in6_dev_finish_destroy(ndev);
423 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
425 struct inet6_dev *idev;
429 idev = __in6_dev_get(dev);
431 idev = ipv6_add_dev(dev);
436 if (dev->flags&IFF_UP)
441 static int inet6_netconf_msgsize_devconf(int type)
443 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
444 + nla_total_size(4); /* NETCONFA_IFINDEX */
446 /* type -1 is used for ALL */
447 if (type == -1 || type == NETCONFA_FORWARDING)
448 size += nla_total_size(4);
449 #ifdef CONFIG_IPV6_MROUTE
450 if (type == -1 || type == NETCONFA_MC_FORWARDING)
451 size += nla_total_size(4);
453 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
454 size += nla_total_size(4);
459 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
460 struct ipv6_devconf *devconf, u32 portid,
461 u32 seq, int event, unsigned int flags,
464 struct nlmsghdr *nlh;
465 struct netconfmsg *ncm;
467 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
472 ncm = nlmsg_data(nlh);
473 ncm->ncm_family = AF_INET6;
475 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
476 goto nla_put_failure;
478 /* type -1 is used for ALL */
479 if ((type == -1 || type == NETCONFA_FORWARDING) &&
480 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
481 goto nla_put_failure;
482 #ifdef CONFIG_IPV6_MROUTE
483 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
484 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
485 devconf->mc_forwarding) < 0)
486 goto nla_put_failure;
488 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
489 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
490 goto nla_put_failure;
492 return nlmsg_end(skb, nlh);
495 nlmsg_cancel(skb, nlh);
499 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
500 struct ipv6_devconf *devconf)
505 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
509 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
510 RTM_NEWNETCONF, 0, type);
512 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
513 WARN_ON(err == -EMSGSIZE);
517 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
520 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
523 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
524 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
525 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
526 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
529 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
530 struct nlmsghdr *nlh)
532 struct net *net = sock_net(in_skb->sk);
533 struct nlattr *tb[NETCONFA_MAX+1];
534 struct netconfmsg *ncm;
536 struct ipv6_devconf *devconf;
537 struct inet6_dev *in6_dev;
538 struct net_device *dev;
542 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
543 devconf_ipv6_policy);
548 if (!tb[NETCONFA_IFINDEX])
551 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
553 case NETCONFA_IFINDEX_ALL:
554 devconf = net->ipv6.devconf_all;
556 case NETCONFA_IFINDEX_DEFAULT:
557 devconf = net->ipv6.devconf_dflt;
560 dev = __dev_get_by_index(net, ifindex);
563 in6_dev = __in6_dev_get(dev);
566 devconf = &in6_dev->cnf;
571 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
575 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
576 NETLINK_CB(in_skb).portid,
577 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
580 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
581 WARN_ON(err == -EMSGSIZE);
585 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
590 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
591 struct netlink_callback *cb)
593 struct net *net = sock_net(skb->sk);
596 struct net_device *dev;
597 struct inet6_dev *idev;
598 struct hlist_head *head;
601 s_idx = idx = cb->args[1];
603 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
605 head = &net->dev_index_head[h];
607 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
609 hlist_for_each_entry_rcu(dev, head, index_hlist) {
612 idev = __in6_dev_get(dev);
616 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
618 NETLINK_CB(cb->skb).portid,
626 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
632 if (h == NETDEV_HASHENTRIES) {
633 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
634 net->ipv6.devconf_all,
635 NETLINK_CB(cb->skb).portid,
637 RTM_NEWNETCONF, NLM_F_MULTI,
643 if (h == NETDEV_HASHENTRIES + 1) {
644 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
645 net->ipv6.devconf_dflt,
646 NETLINK_CB(cb->skb).portid,
648 RTM_NEWNETCONF, NLM_F_MULTI,
662 static void dev_forward_change(struct inet6_dev *idev)
664 struct net_device *dev;
665 struct inet6_ifaddr *ifa;
670 if (idev->cnf.forwarding)
671 dev_disable_lro(dev);
672 if (dev->flags & IFF_MULTICAST) {
673 if (idev->cnf.forwarding) {
674 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
675 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
676 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
678 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
679 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
680 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
684 list_for_each_entry(ifa, &idev->addr_list, if_list) {
685 if (ifa->flags&IFA_F_TENTATIVE)
687 if (idev->cnf.forwarding)
688 addrconf_join_anycast(ifa);
690 addrconf_leave_anycast(ifa);
692 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
693 dev->ifindex, &idev->cnf);
697 static void addrconf_forward_change(struct net *net, __s32 newf)
699 struct net_device *dev;
700 struct inet6_dev *idev;
702 for_each_netdev(net, dev) {
703 idev = __in6_dev_get(dev);
705 int changed = (!idev->cnf.forwarding) ^ (!newf);
706 idev->cnf.forwarding = newf;
708 dev_forward_change(idev);
713 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
719 return restart_syscall();
721 net = (struct net *)table->extra2;
725 if (p == &net->ipv6.devconf_dflt->forwarding) {
726 if ((!newf) ^ (!old))
727 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
728 NETCONFA_IFINDEX_DEFAULT,
729 net->ipv6.devconf_dflt);
734 if (p == &net->ipv6.devconf_all->forwarding) {
735 net->ipv6.devconf_dflt->forwarding = newf;
736 addrconf_forward_change(net, newf);
737 if ((!newf) ^ (!old))
738 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
739 NETCONFA_IFINDEX_ALL,
740 net->ipv6.devconf_all);
741 } else if ((!newf) ^ (!old))
742 dev_forward_change((struct inet6_dev *)table->extra1);
746 rt6_purge_dflt_routers(net);
751 /* Nobody refers to this ifaddr, destroy it */
752 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
754 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
756 #ifdef NET_REFCNT_DEBUG
757 pr_debug("%s\n", __func__);
760 in6_dev_put(ifp->idev);
762 if (cancel_delayed_work(&ifp->dad_work))
763 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
766 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
767 pr_warn("Freeing alive inet6 address %p\n", ifp);
776 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
779 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
782 * Each device address list is sorted in order of scope -
783 * global before linklocal.
785 list_for_each(p, &idev->addr_list) {
786 struct inet6_ifaddr *ifa
787 = list_entry(p, struct inet6_ifaddr, if_list);
788 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
792 list_add_tail(&ifp->if_list, p);
795 static u32 inet6_addr_hash(const struct in6_addr *addr)
797 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
800 /* On success it returns ifp with increased reference count */
802 static struct inet6_ifaddr *
803 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
804 const struct in6_addr *peer_addr, int pfxlen,
805 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
807 struct inet6_ifaddr *ifa = NULL;
811 int addr_type = ipv6_addr_type(addr);
813 if (addr_type == IPV6_ADDR_ANY ||
814 addr_type & IPV6_ADDR_MULTICAST ||
815 (!(idev->dev->flags & IFF_LOOPBACK) &&
816 addr_type & IPV6_ADDR_LOOPBACK))
817 return ERR_PTR(-EADDRNOTAVAIL);
821 err = -ENODEV; /*XXX*/
825 if (idev->cnf.disable_ipv6) {
830 spin_lock(&addrconf_hash_lock);
832 /* Ignore adding duplicate addresses on an interface */
833 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
834 ADBG("ipv6_add_addr: already assigned\n");
839 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
842 ADBG("ipv6_add_addr: malloc failed\n");
847 rt = addrconf_dst_alloc(idev, addr, false);
853 neigh_parms_data_state_setall(idev->nd_parms);
857 ifa->peer_addr = *peer_addr;
859 spin_lock_init(&ifa->lock);
860 spin_lock_init(&ifa->state_lock);
861 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
862 INIT_HLIST_NODE(&ifa->addr_lst);
864 ifa->prefix_len = pfxlen;
865 ifa->flags = flags | IFA_F_TENTATIVE;
866 ifa->valid_lft = valid_lft;
867 ifa->prefered_lft = prefered_lft;
868 ifa->cstamp = ifa->tstamp = jiffies;
869 ifa->tokenized = false;
878 /* Add to big hash table */
879 hash = inet6_addr_hash(addr);
881 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
882 spin_unlock(&addrconf_hash_lock);
884 write_lock(&idev->lock);
885 /* Add to inet6_dev unicast addr list. */
886 ipv6_link_dev_addr(idev, ifa);
888 if (ifa->flags&IFA_F_TEMPORARY) {
889 list_add(&ifa->tmp_list, &idev->tempaddr_list);
894 write_unlock(&idev->lock);
896 rcu_read_unlock_bh();
898 if (likely(err == 0))
899 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
907 spin_unlock(&addrconf_hash_lock);
911 enum cleanup_prefix_rt_t {
912 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
913 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
914 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
918 * Check, whether the prefix for ifp would still need a prefix route
919 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
922 * 1) we don't purge prefix if address was not permanent.
923 * prefix is managed by its own lifetime.
924 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
925 * 3) if there are no addresses, delete prefix.
926 * 4) if there are still other permanent address(es),
927 * corresponding prefix is still permanent.
928 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
929 * don't purge the prefix, assume user space is managing it.
930 * 6) otherwise, update prefix lifetime to the
931 * longest valid lifetime among the corresponding
932 * addresses on the device.
933 * Note: subsequent RA will update lifetime.
935 static enum cleanup_prefix_rt_t
936 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
938 struct inet6_ifaddr *ifa;
939 struct inet6_dev *idev = ifp->idev;
940 unsigned long lifetime;
941 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
945 list_for_each_entry(ifa, &idev->addr_list, if_list) {
948 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
951 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
952 return CLEANUP_PREFIX_RT_NOP;
954 action = CLEANUP_PREFIX_RT_EXPIRE;
956 spin_lock(&ifa->lock);
958 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
960 * Note: Because this address is
961 * not permanent, lifetime <
962 * LONG_MAX / HZ here.
964 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
965 *expires = ifa->tstamp + lifetime * HZ;
966 spin_unlock(&ifa->lock);
973 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
977 rt = addrconf_get_prefix_route(&ifp->addr,
980 0, RTF_GATEWAY | RTF_DEFAULT);
985 if (!(rt->rt6i_flags & RTF_EXPIRES))
986 rt6_set_expires(rt, expires);
993 /* This function wants to get referenced ifp and releases it before return */
995 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
998 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
999 unsigned long expires;
1003 spin_lock_bh(&ifp->state_lock);
1005 ifp->state = INET6_IFADDR_STATE_DEAD;
1006 spin_unlock_bh(&ifp->state_lock);
1008 if (state == INET6_IFADDR_STATE_DEAD)
1011 spin_lock_bh(&addrconf_hash_lock);
1012 hlist_del_init_rcu(&ifp->addr_lst);
1013 spin_unlock_bh(&addrconf_hash_lock);
1015 write_lock_bh(&ifp->idev->lock);
1017 if (ifp->flags&IFA_F_TEMPORARY) {
1018 list_del(&ifp->tmp_list);
1020 in6_ifa_put(ifp->ifpub);
1026 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1027 action = check_cleanup_prefix_route(ifp, &expires);
1029 list_del_init(&ifp->if_list);
1032 write_unlock_bh(&ifp->idev->lock);
1034 addrconf_del_dad_work(ifp);
1036 ipv6_ifa_notify(RTM_DELADDR, ifp);
1038 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1040 if (action != CLEANUP_PREFIX_RT_NOP) {
1041 cleanup_prefix_route(ifp, expires,
1042 action == CLEANUP_PREFIX_RT_DEL);
1045 /* clean up prefsrc entries */
1046 rt6_remove_prefsrc(ifp);
1051 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1053 struct inet6_dev *idev = ifp->idev;
1054 struct in6_addr addr, *tmpaddr;
1055 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1056 unsigned long regen_advance;
1060 unsigned long now = jiffies;
1062 write_lock_bh(&idev->lock);
1064 spin_lock_bh(&ift->lock);
1065 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1066 spin_unlock_bh(&ift->lock);
1073 if (idev->cnf.use_tempaddr <= 0) {
1074 write_unlock_bh(&idev->lock);
1075 pr_info("%s: use_tempaddr is disabled\n", __func__);
1080 spin_lock_bh(&ifp->lock);
1081 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1082 idev->cnf.use_tempaddr = -1; /*XXX*/
1083 spin_unlock_bh(&ifp->lock);
1084 write_unlock_bh(&idev->lock);
1085 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1092 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1093 __ipv6_try_regen_rndid(idev, tmpaddr);
1094 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1095 age = (now - ifp->tstamp) / HZ;
1096 tmp_valid_lft = min_t(__u32,
1098 idev->cnf.temp_valid_lft + age);
1099 tmp_prefered_lft = min_t(__u32,
1101 idev->cnf.temp_prefered_lft + age -
1102 idev->cnf.max_desync_factor);
1103 tmp_plen = ifp->prefix_len;
1104 tmp_tstamp = ifp->tstamp;
1105 spin_unlock_bh(&ifp->lock);
1107 regen_advance = idev->cnf.regen_max_retry *
1108 idev->cnf.dad_transmits *
1109 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1110 write_unlock_bh(&idev->lock);
1112 /* A temporary address is created only if this calculated Preferred
1113 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1114 * an implementation must not create a temporary address with a zero
1115 * Preferred Lifetime.
1116 * Use age calculation as in addrconf_verify to avoid unnecessary
1117 * temporary addresses being generated.
1119 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1120 if (tmp_prefered_lft <= regen_advance + age) {
1127 addr_flags = IFA_F_TEMPORARY;
1128 /* set in addrconf_prefix_rcv() */
1129 if (ifp->flags & IFA_F_OPTIMISTIC)
1130 addr_flags |= IFA_F_OPTIMISTIC;
1132 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1133 ipv6_addr_scope(&addr), addr_flags,
1134 tmp_valid_lft, tmp_prefered_lft);
1138 pr_info("%s: retry temporary address regeneration\n", __func__);
1140 write_lock_bh(&idev->lock);
1144 spin_lock_bh(&ift->lock);
1147 ift->tstamp = tmp_tstamp;
1148 spin_unlock_bh(&ift->lock);
1150 addrconf_dad_start(ift);
1158 * Choose an appropriate source address (RFC3484)
1161 IPV6_SADDR_RULE_INIT = 0,
1162 IPV6_SADDR_RULE_LOCAL,
1163 IPV6_SADDR_RULE_SCOPE,
1164 IPV6_SADDR_RULE_PREFERRED,
1165 #ifdef CONFIG_IPV6_MIP6
1166 IPV6_SADDR_RULE_HOA,
1168 IPV6_SADDR_RULE_OIF,
1169 IPV6_SADDR_RULE_LABEL,
1170 IPV6_SADDR_RULE_PRIVACY,
1171 IPV6_SADDR_RULE_ORCHID,
1172 IPV6_SADDR_RULE_PREFIX,
1173 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1174 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1179 struct ipv6_saddr_score {
1182 struct inet6_ifaddr *ifa;
1183 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1188 struct ipv6_saddr_dst {
1189 const struct in6_addr *addr;
1196 static inline int ipv6_saddr_preferred(int type)
1198 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1203 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1205 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1206 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1212 static int ipv6_get_saddr_eval(struct net *net,
1213 struct ipv6_saddr_score *score,
1214 struct ipv6_saddr_dst *dst,
1219 if (i <= score->rule) {
1221 case IPV6_SADDR_RULE_SCOPE:
1222 ret = score->scopedist;
1224 case IPV6_SADDR_RULE_PREFIX:
1225 ret = score->matchlen;
1228 ret = !!test_bit(i, score->scorebits);
1234 case IPV6_SADDR_RULE_INIT:
1235 /* Rule 0: remember if hiscore is not ready yet */
1238 case IPV6_SADDR_RULE_LOCAL:
1239 /* Rule 1: Prefer same address */
1240 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1242 case IPV6_SADDR_RULE_SCOPE:
1243 /* Rule 2: Prefer appropriate scope
1248 * ---+--+-+---> scope
1250 * | d is scope of the destination.
1252 * | \ <- smaller scope is better if
1253 * B-15 | \ if scope is enough for destination.
1254 * | ret = B - scope (-1 <= scope >= d <= 15).
1256 * |/ <- greater is better
1257 * -C / if scope is not enough for destination.
1258 * /| ret = scope - C (-1 <= d < scope <= 15).
1260 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1261 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1262 * Assume B = 0 and we get C > 29.
1264 ret = __ipv6_addr_src_scope(score->addr_type);
1265 if (ret >= dst->scope)
1268 ret -= 128; /* 30 is enough */
1269 score->scopedist = ret;
1271 case IPV6_SADDR_RULE_PREFERRED:
1273 /* Rule 3: Avoid deprecated and optimistic addresses */
1274 u8 avoid = IFA_F_DEPRECATED;
1276 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1277 avoid |= IFA_F_OPTIMISTIC;
1278 ret = ipv6_saddr_preferred(score->addr_type) ||
1279 !(score->ifa->flags & avoid);
1282 #ifdef CONFIG_IPV6_MIP6
1283 case IPV6_SADDR_RULE_HOA:
1285 /* Rule 4: Prefer home address */
1286 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1287 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1291 case IPV6_SADDR_RULE_OIF:
1292 /* Rule 5: Prefer outgoing interface */
1293 ret = (!dst->ifindex ||
1294 dst->ifindex == score->ifa->idev->dev->ifindex);
1296 case IPV6_SADDR_RULE_LABEL:
1297 /* Rule 6: Prefer matching label */
1298 ret = ipv6_addr_label(net,
1299 &score->ifa->addr, score->addr_type,
1300 score->ifa->idev->dev->ifindex) == dst->label;
1302 case IPV6_SADDR_RULE_PRIVACY:
1304 /* Rule 7: Prefer public address
1305 * Note: prefer temporary address if use_tempaddr >= 2
1307 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1308 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1309 score->ifa->idev->cnf.use_tempaddr >= 2;
1310 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1313 case IPV6_SADDR_RULE_ORCHID:
1314 /* Rule 8-: Prefer ORCHID vs ORCHID or
1315 * non-ORCHID vs non-ORCHID
1317 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1318 ipv6_addr_orchid(dst->addr));
1320 case IPV6_SADDR_RULE_PREFIX:
1321 /* Rule 8: Use longest matching prefix */
1322 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1323 if (ret > score->ifa->prefix_len)
1324 ret = score->ifa->prefix_len;
1325 score->matchlen = ret;
1327 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1328 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1329 /* Optimistic addresses still have lower precedence than other
1330 * preferred addresses.
1332 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1340 __set_bit(i, score->scorebits);
1346 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1347 const struct in6_addr *daddr, unsigned int prefs,
1348 struct in6_addr *saddr)
1350 struct ipv6_saddr_score scores[2],
1351 *score = &scores[0], *hiscore = &scores[1];
1352 struct ipv6_saddr_dst dst;
1353 struct net_device *dev;
1356 dst_type = __ipv6_addr_type(daddr);
1358 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1359 dst.scope = __ipv6_addr_src_scope(dst_type);
1360 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1364 hiscore->ifa = NULL;
1368 for_each_netdev_rcu(net, dev) {
1369 struct inet6_dev *idev;
1371 /* Candidate Source Address (section 4)
1372 * - multicast and link-local destination address,
1373 * the set of candidate source address MUST only
1374 * include addresses assigned to interfaces
1375 * belonging to the same link as the outgoing
1377 * (- For site-local destination addresses, the
1378 * set of candidate source addresses MUST only
1379 * include addresses assigned to interfaces
1380 * belonging to the same site as the outgoing
1383 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1384 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1385 dst.ifindex && dev->ifindex != dst.ifindex)
1388 idev = __in6_dev_get(dev);
1392 read_lock_bh(&idev->lock);
1393 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1397 * - Tentative Address (RFC2462 section 5.4)
1398 * - A tentative address is not considered
1399 * "assigned to an interface" in the traditional
1400 * sense, unless it is also flagged as optimistic.
1401 * - Candidate Source Address (section 4)
1402 * - In any case, anycast addresses, multicast
1403 * addresses, and the unspecified address MUST
1404 * NOT be included in a candidate set.
1406 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1407 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1410 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1412 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1413 score->addr_type & IPV6_ADDR_MULTICAST)) {
1414 LIMIT_NETDEBUG(KERN_DEBUG
1415 "ADDRCONF: unspecified / multicast address "
1416 "assigned as unicast address on %s",
1422 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1424 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1425 int minihiscore, miniscore;
1427 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1428 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1430 if (minihiscore > miniscore) {
1431 if (i == IPV6_SADDR_RULE_SCOPE &&
1432 score->scopedist > 0) {
1435 * each remaining entry
1436 * has too small (not enough)
1437 * scope, because ifa entries
1438 * are sorted by their scope
1444 } else if (minihiscore < miniscore) {
1446 in6_ifa_put(hiscore->ifa);
1448 in6_ifa_hold(score->ifa);
1450 swap(hiscore, score);
1452 /* restore our iterator */
1453 score->ifa = hiscore->ifa;
1460 read_unlock_bh(&idev->lock);
1465 return -EADDRNOTAVAIL;
1467 *saddr = hiscore->ifa->addr;
1468 in6_ifa_put(hiscore->ifa);
1471 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1473 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1476 struct inet6_ifaddr *ifp;
1477 int err = -EADDRNOTAVAIL;
1479 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1480 if (ifp->scope > IFA_LINK)
1482 if (ifp->scope == IFA_LINK &&
1483 !(ifp->flags & banned_flags)) {
1492 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1495 struct inet6_dev *idev;
1496 int err = -EADDRNOTAVAIL;
1499 idev = __in6_dev_get(dev);
1501 read_lock_bh(&idev->lock);
1502 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1503 read_unlock_bh(&idev->lock);
1509 static int ipv6_count_addresses(struct inet6_dev *idev)
1512 struct inet6_ifaddr *ifp;
1514 read_lock_bh(&idev->lock);
1515 list_for_each_entry(ifp, &idev->addr_list, if_list)
1517 read_unlock_bh(&idev->lock);
1521 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1522 const struct net_device *dev, int strict)
1524 struct inet6_ifaddr *ifp;
1525 unsigned int hash = inet6_addr_hash(addr);
1528 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1529 if (!net_eq(dev_net(ifp->idev->dev), net))
1531 if (ipv6_addr_equal(&ifp->addr, addr) &&
1532 !(ifp->flags&IFA_F_TENTATIVE) &&
1533 (dev == NULL || ifp->idev->dev == dev ||
1534 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1535 rcu_read_unlock_bh();
1540 rcu_read_unlock_bh();
1543 EXPORT_SYMBOL(ipv6_chk_addr);
1545 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1546 struct net_device *dev)
1548 unsigned int hash = inet6_addr_hash(addr);
1549 struct inet6_ifaddr *ifp;
1551 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1552 if (!net_eq(dev_net(ifp->idev->dev), net))
1554 if (ipv6_addr_equal(&ifp->addr, addr)) {
1555 if (dev == NULL || ifp->idev->dev == dev)
1562 /* Compares an address/prefix_len with addresses on device @dev.
1563 * If one is found it returns true.
1565 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1566 const unsigned int prefix_len, struct net_device *dev)
1568 struct inet6_dev *idev;
1569 struct inet6_ifaddr *ifa;
1573 idev = __in6_dev_get(dev);
1575 read_lock_bh(&idev->lock);
1576 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1577 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1581 read_unlock_bh(&idev->lock);
1587 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1589 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1591 struct inet6_dev *idev;
1592 struct inet6_ifaddr *ifa;
1597 idev = __in6_dev_get(dev);
1599 read_lock_bh(&idev->lock);
1600 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1601 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1606 read_unlock_bh(&idev->lock);
1611 EXPORT_SYMBOL(ipv6_chk_prefix);
1613 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1614 struct net_device *dev, int strict)
1616 struct inet6_ifaddr *ifp, *result = NULL;
1617 unsigned int hash = inet6_addr_hash(addr);
1620 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1621 if (!net_eq(dev_net(ifp->idev->dev), net))
1623 if (ipv6_addr_equal(&ifp->addr, addr)) {
1624 if (dev == NULL || ifp->idev->dev == dev ||
1625 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1632 rcu_read_unlock_bh();
1637 /* Gets referenced address, destroys ifaddr */
1639 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1641 if (ifp->flags&IFA_F_PERMANENT) {
1642 spin_lock_bh(&ifp->lock);
1643 addrconf_del_dad_work(ifp);
1644 ifp->flags |= IFA_F_TENTATIVE;
1646 ifp->flags |= IFA_F_DADFAILED;
1647 spin_unlock_bh(&ifp->lock);
1649 ipv6_ifa_notify(0, ifp);
1651 } else if (ifp->flags&IFA_F_TEMPORARY) {
1652 struct inet6_ifaddr *ifpub;
1653 spin_lock_bh(&ifp->lock);
1656 in6_ifa_hold(ifpub);
1657 spin_unlock_bh(&ifp->lock);
1658 ipv6_create_tempaddr(ifpub, ifp);
1661 spin_unlock_bh(&ifp->lock);
1669 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1673 spin_lock_bh(&ifp->state_lock);
1674 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1675 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1678 spin_unlock_bh(&ifp->state_lock);
1683 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1685 struct inet6_dev *idev = ifp->idev;
1687 if (addrconf_dad_end(ifp)) {
1692 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1693 ifp->idev->dev->name, &ifp->addr);
1695 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1696 struct in6_addr addr;
1698 addr.s6_addr32[0] = htonl(0xfe800000);
1699 addr.s6_addr32[1] = 0;
1701 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1702 ipv6_addr_equal(&ifp->addr, &addr)) {
1703 /* DAD failed for link-local based on MAC address */
1704 idev->cnf.disable_ipv6 = 1;
1706 pr_info("%s: IPv6 being disabled!\n",
1707 ifp->idev->dev->name);
1711 spin_lock_bh(&ifp->state_lock);
1712 /* transition from _POSTDAD to _ERRDAD */
1713 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1714 spin_unlock_bh(&ifp->state_lock);
1716 addrconf_mod_dad_work(ifp, 0);
1719 /* Join to solicited addr multicast group.
1720 * caller must hold RTNL */
1721 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1723 struct in6_addr maddr;
1725 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1728 addrconf_addr_solict_mult(addr, &maddr);
1729 ipv6_dev_mc_inc(dev, &maddr);
1732 /* caller must hold RTNL */
1733 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1735 struct in6_addr maddr;
1737 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1740 addrconf_addr_solict_mult(addr, &maddr);
1741 __ipv6_dev_mc_dec(idev, &maddr);
1744 /* caller must hold RTNL */
1745 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1747 struct in6_addr addr;
1749 if (ifp->prefix_len >= 127) /* RFC 6164 */
1751 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1752 if (ipv6_addr_any(&addr))
1754 __ipv6_dev_ac_inc(ifp->idev, &addr);
1757 /* caller must hold RTNL */
1758 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1760 struct in6_addr addr;
1762 if (ifp->prefix_len >= 127) /* RFC 6164 */
1764 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1765 if (ipv6_addr_any(&addr))
1767 __ipv6_dev_ac_dec(ifp->idev, &addr);
1770 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1772 if (dev->addr_len != ETH_ALEN)
1774 memcpy(eui, dev->dev_addr, 3);
1775 memcpy(eui + 5, dev->dev_addr + 3, 3);
1778 * The zSeries OSA network cards can be shared among various
1779 * OS instances, but the OSA cards have only one MAC address.
1780 * This leads to duplicate address conflicts in conjunction
1781 * with IPv6 if more than one instance uses the same card.
1783 * The driver for these cards can deliver a unique 16-bit
1784 * identifier for each instance sharing the same card. It is
1785 * placed instead of 0xFFFE in the interface identifier. The
1786 * "u" bit of the interface identifier is not inverted in this
1787 * case. Hence the resulting interface identifier has local
1788 * scope according to RFC2373.
1791 eui[3] = (dev->dev_id >> 8) & 0xFF;
1792 eui[4] = dev->dev_id & 0xFF;
1801 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1803 if (dev->addr_len != IEEE802154_ADDR_LEN)
1805 memcpy(eui, dev->dev_addr, 8);
1810 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1812 union fwnet_hwaddr *ha;
1814 if (dev->addr_len != FWNET_ALEN)
1817 ha = (union fwnet_hwaddr *)dev->dev_addr;
1819 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1824 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1826 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1827 if (dev->addr_len != ARCNET_ALEN)
1830 eui[7] = *(u8 *)dev->dev_addr;
1834 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1836 if (dev->addr_len != INFINIBAND_ALEN)
1838 memcpy(eui, dev->dev_addr + 12, 8);
1843 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1847 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1848 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1849 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1850 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1851 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1852 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1856 memcpy(eui + 4, &addr, 4);
1860 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1862 if (dev->priv_flags & IFF_ISATAP)
1863 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1867 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1869 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1872 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1874 memcpy(eui, dev->perm_addr, 3);
1875 memcpy(eui + 5, dev->perm_addr + 3, 3);
1882 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1884 switch (dev->type) {
1887 return addrconf_ifid_eui48(eui, dev);
1889 return addrconf_ifid_arcnet(eui, dev);
1890 case ARPHRD_INFINIBAND:
1891 return addrconf_ifid_infiniband(eui, dev);
1893 return addrconf_ifid_sit(eui, dev);
1895 return addrconf_ifid_gre(eui, dev);
1896 case ARPHRD_6LOWPAN:
1897 case ARPHRD_IEEE802154:
1898 return addrconf_ifid_eui64(eui, dev);
1899 case ARPHRD_IEEE1394:
1900 return addrconf_ifid_ieee1394(eui, dev);
1901 case ARPHRD_TUNNEL6:
1902 return addrconf_ifid_ip6tnl(eui, dev);
1907 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1910 struct inet6_ifaddr *ifp;
1912 read_lock_bh(&idev->lock);
1913 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1914 if (ifp->scope > IFA_LINK)
1916 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1917 memcpy(eui, ifp->addr.s6_addr+8, 8);
1922 read_unlock_bh(&idev->lock);
1926 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1927 static void __ipv6_regen_rndid(struct inet6_dev *idev)
1930 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1931 idev->rndid[0] &= ~0x02;
1934 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1935 * check if generated address is not inappropriate
1937 * - Reserved subnet anycast (RFC 2526)
1938 * 11111101 11....11 1xxxxxxx
1939 * - ISATAP (RFC4214) 6.1
1940 * 00-00-5E-FE-xx-xx-xx-xx
1942 * - XXX: already assigned to an address on the device
1944 if (idev->rndid[0] == 0xfd &&
1945 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1946 (idev->rndid[7]&0x80))
1948 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1949 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1951 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1956 static void ipv6_regen_rndid(unsigned long data)
1958 struct inet6_dev *idev = (struct inet6_dev *) data;
1959 unsigned long expires;
1962 write_lock_bh(&idev->lock);
1967 __ipv6_regen_rndid(idev);
1970 idev->cnf.temp_prefered_lft * HZ -
1971 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
1972 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
1973 idev->cnf.max_desync_factor * HZ;
1974 if (time_before(expires, jiffies)) {
1975 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1976 __func__, idev->dev->name);
1980 if (!mod_timer(&idev->regen_timer, expires))
1984 write_unlock_bh(&idev->lock);
1985 rcu_read_unlock_bh();
1989 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
1991 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1992 __ipv6_regen_rndid(idev);
2000 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2001 unsigned long expires, u32 flags)
2003 struct fib6_config cfg = {
2004 .fc_table = RT6_TABLE_PREFIX,
2005 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2006 .fc_ifindex = dev->ifindex,
2007 .fc_expires = expires,
2009 .fc_flags = RTF_UP | flags,
2010 .fc_nlinfo.nl_net = dev_net(dev),
2011 .fc_protocol = RTPROT_KERNEL,
2016 /* Prevent useless cloning on PtP SIT.
2017 This thing is done here expecting that the whole
2018 class of non-broadcast devices need not cloning.
2020 #if IS_ENABLED(CONFIG_IPV6_SIT)
2021 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2022 cfg.fc_flags |= RTF_NONEXTHOP;
2025 ip6_route_add(&cfg);
2029 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2031 const struct net_device *dev,
2032 u32 flags, u32 noflags)
2034 struct fib6_node *fn;
2035 struct rt6_info *rt = NULL;
2036 struct fib6_table *table;
2038 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2042 read_lock_bh(&table->tb6_lock);
2043 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2046 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2047 if (rt->dst.dev->ifindex != dev->ifindex)
2049 if ((rt->rt6i_flags & flags) != flags)
2051 if ((rt->rt6i_flags & noflags) != 0)
2057 read_unlock_bh(&table->tb6_lock);
2062 /* Create "default" multicast route to the interface */
2064 static void addrconf_add_mroute(struct net_device *dev)
2066 struct fib6_config cfg = {
2067 .fc_table = RT6_TABLE_LOCAL,
2068 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2069 .fc_ifindex = dev->ifindex,
2072 .fc_nlinfo.nl_net = dev_net(dev),
2075 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2077 ip6_route_add(&cfg);
2080 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2082 struct inet6_dev *idev;
2086 idev = ipv6_find_idev(dev);
2088 return ERR_PTR(-ENOBUFS);
2090 if (idev->cnf.disable_ipv6)
2091 return ERR_PTR(-EACCES);
2093 /* Add default multicast route */
2094 if (!(dev->flags & IFF_LOOPBACK))
2095 addrconf_add_mroute(dev);
2100 static void manage_tempaddrs(struct inet6_dev *idev,
2101 struct inet6_ifaddr *ifp,
2102 __u32 valid_lft, __u32 prefered_lft,
2103 bool create, unsigned long now)
2106 struct inet6_ifaddr *ift;
2108 read_lock_bh(&idev->lock);
2109 /* update all temporary addresses in the list */
2110 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2111 int age, max_valid, max_prefered;
2113 if (ifp != ift->ifpub)
2116 /* RFC 4941 section 3.3:
2117 * If a received option will extend the lifetime of a public
2118 * address, the lifetimes of temporary addresses should
2119 * be extended, subject to the overall constraint that no
2120 * temporary addresses should ever remain "valid" or "preferred"
2121 * for a time longer than (TEMP_VALID_LIFETIME) or
2122 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2124 age = (now - ift->cstamp) / HZ;
2125 max_valid = idev->cnf.temp_valid_lft - age;
2129 max_prefered = idev->cnf.temp_prefered_lft -
2130 idev->cnf.max_desync_factor - age;
2131 if (max_prefered < 0)
2134 if (valid_lft > max_valid)
2135 valid_lft = max_valid;
2137 if (prefered_lft > max_prefered)
2138 prefered_lft = max_prefered;
2140 spin_lock(&ift->lock);
2142 ift->valid_lft = valid_lft;
2143 ift->prefered_lft = prefered_lft;
2145 if (prefered_lft > 0)
2146 ift->flags &= ~IFA_F_DEPRECATED;
2148 spin_unlock(&ift->lock);
2149 if (!(flags&IFA_F_TENTATIVE))
2150 ipv6_ifa_notify(0, ift);
2153 if ((create || list_empty(&idev->tempaddr_list)) &&
2154 idev->cnf.use_tempaddr > 0) {
2155 /* When a new public address is created as described
2156 * in [ADDRCONF], also create a new temporary address.
2157 * Also create a temporary address if it's enabled but
2158 * no temporary address currently exists.
2160 read_unlock_bh(&idev->lock);
2161 ipv6_create_tempaddr(ifp, NULL);
2163 read_unlock_bh(&idev->lock);
2167 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2169 struct prefix_info *pinfo;
2173 struct inet6_dev *in6_dev;
2174 struct net *net = dev_net(dev);
2176 pinfo = (struct prefix_info *) opt;
2178 if (len < sizeof(struct prefix_info)) {
2179 ADBG("addrconf: prefix option too short\n");
2184 * Validation checks ([ADDRCONF], page 19)
2187 addr_type = ipv6_addr_type(&pinfo->prefix);
2189 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2192 valid_lft = ntohl(pinfo->valid);
2193 prefered_lft = ntohl(pinfo->prefered);
2195 if (prefered_lft > valid_lft) {
2196 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2200 in6_dev = in6_dev_get(dev);
2202 if (in6_dev == NULL) {
2203 net_dbg_ratelimited("addrconf: device %s not configured\n",
2209 * Two things going on here:
2210 * 1) Add routes for on-link prefixes
2211 * 2) Configure prefixes with the auto flag set
2214 if (pinfo->onlink) {
2215 struct rt6_info *rt;
2216 unsigned long rt_expires;
2218 /* Avoid arithmetic overflow. Really, we could
2219 * save rt_expires in seconds, likely valid_lft,
2220 * but it would require division in fib gc, that it
2224 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2226 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2228 if (addrconf_finite_timeout(rt_expires))
2231 rt = addrconf_get_prefix_route(&pinfo->prefix,
2234 RTF_ADDRCONF | RTF_PREFIX_RT,
2235 RTF_GATEWAY | RTF_DEFAULT);
2238 /* Autoconf prefix route */
2239 if (valid_lft == 0) {
2242 } else if (addrconf_finite_timeout(rt_expires)) {
2244 rt6_set_expires(rt, jiffies + rt_expires);
2246 rt6_clean_expires(rt);
2248 } else if (valid_lft) {
2249 clock_t expires = 0;
2250 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2251 if (addrconf_finite_timeout(rt_expires)) {
2253 flags |= RTF_EXPIRES;
2254 expires = jiffies_to_clock_t(rt_expires);
2256 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2257 dev, expires, flags);
2262 /* Try to figure out our local address for this prefix */
2264 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2265 struct inet6_ifaddr *ifp;
2266 struct in6_addr addr;
2267 int create = 0, update_lft = 0;
2268 bool tokenized = false;
2270 if (pinfo->prefix_len == 64) {
2271 memcpy(&addr, &pinfo->prefix, 8);
2273 if (!ipv6_addr_any(&in6_dev->token)) {
2274 read_lock_bh(&in6_dev->lock);
2275 memcpy(addr.s6_addr + 8,
2276 in6_dev->token.s6_addr + 8, 8);
2277 read_unlock_bh(&in6_dev->lock);
2279 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2280 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2281 in6_dev_put(in6_dev);
2286 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2288 in6_dev_put(in6_dev);
2293 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2295 if (ifp == NULL && valid_lft) {
2296 int max_addresses = in6_dev->cnf.max_addresses;
2299 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2300 if (in6_dev->cnf.optimistic_dad &&
2301 !net->ipv6.devconf_all->forwarding && sllao)
2302 addr_flags = IFA_F_OPTIMISTIC;
2305 /* Do not allow to create too much of autoconfigured
2306 * addresses; this would be too easy way to crash kernel.
2308 if (!max_addresses ||
2309 ipv6_count_addresses(in6_dev) < max_addresses)
2310 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2312 addr_type&IPV6_ADDR_SCOPE_MASK,
2313 addr_flags, valid_lft,
2316 if (IS_ERR_OR_NULL(ifp)) {
2317 in6_dev_put(in6_dev);
2323 spin_lock_bh(&ifp->lock);
2324 ifp->flags |= IFA_F_MANAGETEMPADDR;
2325 ifp->cstamp = jiffies;
2326 ifp->tokenized = tokenized;
2327 spin_unlock_bh(&ifp->lock);
2328 addrconf_dad_start(ifp);
2336 /* update lifetime (RFC2462 5.5.3 e) */
2337 spin_lock(&ifp->lock);
2339 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2340 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2343 if (!update_lft && !create && stored_lft) {
2344 const u32 minimum_lft = min_t(u32,
2345 stored_lft, MIN_VALID_LIFETIME);
2346 valid_lft = max(valid_lft, minimum_lft);
2348 /* RFC4862 Section 5.5.3e:
2349 * "Note that the preferred lifetime of the
2350 * corresponding address is always reset to
2351 * the Preferred Lifetime in the received
2352 * Prefix Information option, regardless of
2353 * whether the valid lifetime is also reset or
2356 * So we should always update prefered_lft here.
2362 ifp->valid_lft = valid_lft;
2363 ifp->prefered_lft = prefered_lft;
2366 ifp->flags &= ~IFA_F_DEPRECATED;
2367 spin_unlock(&ifp->lock);
2369 if (!(flags&IFA_F_TENTATIVE))
2370 ipv6_ifa_notify(0, ifp);
2372 spin_unlock(&ifp->lock);
2374 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2381 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2382 in6_dev_put(in6_dev);
2386 * Set destination address.
2387 * Special case for SIT interfaces where we create a new "virtual"
2390 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2392 struct in6_ifreq ireq;
2393 struct net_device *dev;
2399 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2402 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2408 #if IS_ENABLED(CONFIG_IPV6_SIT)
2409 if (dev->type == ARPHRD_SIT) {
2410 const struct net_device_ops *ops = dev->netdev_ops;
2412 struct ip_tunnel_parm p;
2414 err = -EADDRNOTAVAIL;
2415 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2418 memset(&p, 0, sizeof(p));
2419 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2423 p.iph.protocol = IPPROTO_IPV6;
2425 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2427 if (ops->ndo_do_ioctl) {
2428 mm_segment_t oldfs = get_fs();
2431 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2438 dev = __dev_get_by_name(net, p.name);
2441 err = dev_open(dev);
2452 * Manual configuration of address on an interface
2454 static int inet6_addr_add(struct net *net, int ifindex,
2455 const struct in6_addr *pfx,
2456 const struct in6_addr *peer_pfx,
2457 unsigned int plen, __u32 ifa_flags,
2458 __u32 prefered_lft, __u32 valid_lft)
2460 struct inet6_ifaddr *ifp;
2461 struct inet6_dev *idev;
2462 struct net_device *dev;
2466 unsigned long timeout;
2473 /* check the lifetime */
2474 if (!valid_lft || prefered_lft > valid_lft)
2477 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2480 dev = __dev_get_by_index(net, ifindex);
2484 idev = addrconf_add_dev(dev);
2486 return PTR_ERR(idev);
2488 scope = ipv6_addr_scope(pfx);
2490 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2491 if (addrconf_finite_timeout(timeout)) {
2492 expires = jiffies_to_clock_t(timeout * HZ);
2493 valid_lft = timeout;
2494 flags = RTF_EXPIRES;
2498 ifa_flags |= IFA_F_PERMANENT;
2501 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2502 if (addrconf_finite_timeout(timeout)) {
2504 ifa_flags |= IFA_F_DEPRECATED;
2505 prefered_lft = timeout;
2508 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2509 valid_lft, prefered_lft);
2512 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2513 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2518 * Note that section 3.1 of RFC 4429 indicates
2519 * that the Optimistic flag should not be set for
2520 * manually configured addresses
2522 addrconf_dad_start(ifp);
2523 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2524 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2527 addrconf_verify_rtnl();
2531 return PTR_ERR(ifp);
2534 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2535 const struct in6_addr *pfx, unsigned int plen)
2537 struct inet6_ifaddr *ifp;
2538 struct inet6_dev *idev;
2539 struct net_device *dev;
2544 dev = __dev_get_by_index(net, ifindex);
2548 if ((idev = __in6_dev_get(dev)) == NULL)
2551 read_lock_bh(&idev->lock);
2552 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2553 if (ifp->prefix_len == plen &&
2554 ipv6_addr_equal(pfx, &ifp->addr)) {
2556 read_unlock_bh(&idev->lock);
2558 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2559 (ifa_flags & IFA_F_MANAGETEMPADDR))
2560 manage_tempaddrs(idev, ifp, 0, 0, false,
2563 addrconf_verify_rtnl();
2567 read_unlock_bh(&idev->lock);
2568 return -EADDRNOTAVAIL;
2572 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2574 struct in6_ifreq ireq;
2577 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2580 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2584 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2585 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2586 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2591 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2593 struct in6_ifreq ireq;
2596 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2599 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2603 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2604 ireq.ifr6_prefixlen);
2609 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2610 int plen, int scope)
2612 struct inet6_ifaddr *ifp;
2614 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2615 scope, IFA_F_PERMANENT,
2616 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2618 spin_lock_bh(&ifp->lock);
2619 ifp->flags &= ~IFA_F_TENTATIVE;
2620 spin_unlock_bh(&ifp->lock);
2621 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2626 #if IS_ENABLED(CONFIG_IPV6_SIT)
2627 static void sit_add_v4_addrs(struct inet6_dev *idev)
2629 struct in6_addr addr;
2630 struct net_device *dev;
2631 struct net *net = dev_net(idev->dev);
2637 memset(&addr, 0, sizeof(struct in6_addr));
2638 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2640 if (idev->dev->flags&IFF_POINTOPOINT) {
2641 addr.s6_addr32[0] = htonl(0xfe800000);
2645 scope = IPV6_ADDR_COMPATv4;
2647 pflags |= RTF_NONEXTHOP;
2650 if (addr.s6_addr32[3]) {
2651 add_addr(idev, &addr, plen, scope);
2652 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2656 for_each_netdev(net, dev) {
2657 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2658 if (in_dev && (dev->flags & IFF_UP)) {
2659 struct in_ifaddr *ifa;
2663 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2665 addr.s6_addr32[3] = ifa->ifa_local;
2667 if (ifa->ifa_scope == RT_SCOPE_LINK)
2669 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2670 if (idev->dev->flags&IFF_POINTOPOINT)
2675 add_addr(idev, &addr, plen, flag);
2676 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2684 static void init_loopback(struct net_device *dev)
2686 struct inet6_dev *idev;
2687 struct net_device *sp_dev;
2688 struct inet6_ifaddr *sp_ifa;
2689 struct rt6_info *sp_rt;
2695 if ((idev = ipv6_find_idev(dev)) == NULL) {
2696 pr_debug("%s: add_dev failed\n", __func__);
2700 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2702 /* Add routes to other interface's IPv6 addresses */
2703 for_each_netdev(dev_net(dev), sp_dev) {
2704 if (!strcmp(sp_dev->name, dev->name))
2707 idev = __in6_dev_get(sp_dev);
2711 read_lock_bh(&idev->lock);
2712 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2714 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2718 /* This dst has been added to garbage list when
2719 * lo device down, release this obsolete dst and
2720 * reallocate a new router for ifa.
2722 if (sp_ifa->rt->dst.obsolete > 0) {
2723 ip6_rt_put(sp_ifa->rt);
2730 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
2732 /* Failure cases are ignored */
2733 if (!IS_ERR(sp_rt)) {
2738 read_unlock_bh(&idev->lock);
2742 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2744 struct inet6_ifaddr *ifp;
2745 u32 addr_flags = IFA_F_PERMANENT;
2747 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2748 if (idev->cnf.optimistic_dad &&
2749 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2750 addr_flags |= IFA_F_OPTIMISTIC;
2754 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2755 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2757 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2758 addrconf_dad_start(ifp);
2763 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2765 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
2766 struct in6_addr addr;
2768 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2769 /* addrconf_add_linklocal also adds a prefix_route and we
2770 * only need to care about prefix routes if ipv6_generate_eui64
2771 * couldn't generate one.
2773 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
2774 addrconf_add_linklocal(idev, &addr);
2775 else if (prefix_route)
2776 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2780 static void addrconf_dev_config(struct net_device *dev)
2782 struct inet6_dev *idev;
2786 if ((dev->type != ARPHRD_ETHER) &&
2787 (dev->type != ARPHRD_FDDI) &&
2788 (dev->type != ARPHRD_ARCNET) &&
2789 (dev->type != ARPHRD_INFINIBAND) &&
2790 (dev->type != ARPHRD_IEEE802154) &&
2791 (dev->type != ARPHRD_IEEE1394) &&
2792 (dev->type != ARPHRD_TUNNEL6) &&
2793 (dev->type != ARPHRD_6LOWPAN)) {
2794 /* Alas, we support only Ethernet autoconfiguration. */
2798 idev = addrconf_add_dev(dev);
2802 addrconf_addr_gen(idev, false);
2805 #if IS_ENABLED(CONFIG_IPV6_SIT)
2806 static void addrconf_sit_config(struct net_device *dev)
2808 struct inet6_dev *idev;
2813 * Configure the tunnel with one of our IPv4
2814 * addresses... we should configure all of
2815 * our v4 addrs in the tunnel
2818 if ((idev = ipv6_find_idev(dev)) == NULL) {
2819 pr_debug("%s: add_dev failed\n", __func__);
2823 if (dev->priv_flags & IFF_ISATAP) {
2824 addrconf_addr_gen(idev, false);
2828 sit_add_v4_addrs(idev);
2830 if (dev->flags&IFF_POINTOPOINT)
2831 addrconf_add_mroute(dev);
2835 #if IS_ENABLED(CONFIG_NET_IPGRE)
2836 static void addrconf_gre_config(struct net_device *dev)
2838 struct inet6_dev *idev;
2842 if ((idev = ipv6_find_idev(dev)) == NULL) {
2843 pr_debug("%s: add_dev failed\n", __func__);
2847 addrconf_addr_gen(idev, true);
2851 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2854 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2855 struct inet6_dev *idev = __in6_dev_get(dev);
2856 int run_pending = 0;
2860 case NETDEV_REGISTER:
2861 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2862 idev = ipv6_add_dev(dev);
2864 return notifier_from_errno(PTR_ERR(idev));
2870 if (dev->flags & IFF_SLAVE)
2873 if (idev && idev->cnf.disable_ipv6)
2876 if (event == NETDEV_UP) {
2877 if (!addrconf_qdisc_ok(dev)) {
2878 /* device is not ready yet. */
2879 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2884 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2885 idev = ipv6_add_dev(dev);
2887 if (!IS_ERR_OR_NULL(idev)) {
2888 idev->if_flags |= IF_READY;
2892 if (!addrconf_qdisc_ok(dev)) {
2893 /* device is still not ready. */
2898 if (idev->if_flags & IF_READY)
2899 /* device is already configured. */
2901 idev->if_flags |= IF_READY;
2904 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2910 switch (dev->type) {
2911 #if IS_ENABLED(CONFIG_IPV6_SIT)
2913 addrconf_sit_config(dev);
2916 #if IS_ENABLED(CONFIG_NET_IPGRE)
2918 addrconf_gre_config(dev);
2921 case ARPHRD_LOOPBACK:
2926 addrconf_dev_config(dev);
2930 if (!IS_ERR_OR_NULL(idev)) {
2932 addrconf_dad_run(idev);
2935 * If the MTU changed during the interface down,
2936 * when the interface up, the changed MTU must be
2937 * reflected in the idev as well as routers.
2939 if (idev->cnf.mtu6 != dev->mtu &&
2940 dev->mtu >= IPV6_MIN_MTU) {
2941 rt6_mtu_change(dev, dev->mtu);
2942 idev->cnf.mtu6 = dev->mtu;
2944 idev->tstamp = jiffies;
2945 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2948 * If the changed mtu during down is lower than
2949 * IPV6_MIN_MTU stop IPv6 on this interface.
2951 if (dev->mtu < IPV6_MIN_MTU)
2952 addrconf_ifdown(dev, 1);
2956 case NETDEV_CHANGEMTU:
2957 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2958 rt6_mtu_change(dev, dev->mtu);
2959 idev->cnf.mtu6 = dev->mtu;
2963 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2964 idev = ipv6_add_dev(dev);
2970 * if MTU under IPV6_MIN_MTU.
2971 * Stop IPv6 on this interface.
2975 case NETDEV_UNREGISTER:
2977 * Remove all addresses from this interface.
2979 addrconf_ifdown(dev, event != NETDEV_DOWN);
2982 case NETDEV_CHANGENAME:
2984 snmp6_unregister_dev(idev);
2985 addrconf_sysctl_unregister(idev);
2986 err = addrconf_sysctl_register(idev);
2988 return notifier_from_errno(err);
2989 err = snmp6_register_dev(idev);
2991 addrconf_sysctl_unregister(idev);
2992 return notifier_from_errno(err);
2997 case NETDEV_PRE_TYPE_CHANGE:
2998 case NETDEV_POST_TYPE_CHANGE:
2999 addrconf_type_change(dev, event);
3007 * addrconf module should be notified of a device going up
3009 static struct notifier_block ipv6_dev_notf = {
3010 .notifier_call = addrconf_notify,
3013 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3015 struct inet6_dev *idev;
3018 idev = __in6_dev_get(dev);
3020 if (event == NETDEV_POST_TYPE_CHANGE)
3021 ipv6_mc_remap(idev);
3022 else if (event == NETDEV_PRE_TYPE_CHANGE)
3023 ipv6_mc_unmap(idev);
3026 static int addrconf_ifdown(struct net_device *dev, int how)
3028 struct net *net = dev_net(dev);
3029 struct inet6_dev *idev;
3030 struct inet6_ifaddr *ifa;
3035 rt6_ifdown(net, dev);
3036 neigh_ifdown(&nd_tbl, dev);
3038 idev = __in6_dev_get(dev);
3043 * Step 1: remove reference to ipv6 device from parent device.
3049 /* protected by rtnl_lock */
3050 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3052 /* Step 1.5: remove snmp6 entry */
3053 snmp6_unregister_dev(idev);
3057 /* Step 2: clear hash table */
3058 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3059 struct hlist_head *h = &inet6_addr_lst[i];
3061 spin_lock_bh(&addrconf_hash_lock);
3063 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3064 if (ifa->idev == idev) {
3065 hlist_del_init_rcu(&ifa->addr_lst);
3066 addrconf_del_dad_work(ifa);
3070 spin_unlock_bh(&addrconf_hash_lock);
3073 write_lock_bh(&idev->lock);
3075 addrconf_del_rs_timer(idev);
3077 /* Step 2: clear flags for stateless addrconf */
3079 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3081 if (how && del_timer(&idev->regen_timer))
3084 /* Step 3: clear tempaddr list */
3085 while (!list_empty(&idev->tempaddr_list)) {
3086 ifa = list_first_entry(&idev->tempaddr_list,
3087 struct inet6_ifaddr, tmp_list);
3088 list_del(&ifa->tmp_list);
3089 write_unlock_bh(&idev->lock);
3090 spin_lock_bh(&ifa->lock);
3093 in6_ifa_put(ifa->ifpub);
3096 spin_unlock_bh(&ifa->lock);
3098 write_lock_bh(&idev->lock);
3101 while (!list_empty(&idev->addr_list)) {
3102 ifa = list_first_entry(&idev->addr_list,
3103 struct inet6_ifaddr, if_list);
3104 addrconf_del_dad_work(ifa);
3106 list_del(&ifa->if_list);
3108 write_unlock_bh(&idev->lock);
3110 spin_lock_bh(&ifa->state_lock);
3112 ifa->state = INET6_IFADDR_STATE_DEAD;
3113 spin_unlock_bh(&ifa->state_lock);
3115 if (state != INET6_IFADDR_STATE_DEAD) {
3116 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3117 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3121 write_lock_bh(&idev->lock);
3124 write_unlock_bh(&idev->lock);
3126 /* Step 5: Discard anycast and multicast list */
3128 ipv6_ac_destroy_dev(idev);
3129 ipv6_mc_destroy_dev(idev);
3134 idev->tstamp = jiffies;
3136 /* Last: Shot the device (if unregistered) */
3138 addrconf_sysctl_unregister(idev);
3139 neigh_parms_release(&nd_tbl, idev->nd_parms);
3140 neigh_ifdown(&nd_tbl, dev);
3146 static void addrconf_rs_timer(unsigned long data)
3148 struct inet6_dev *idev = (struct inet6_dev *)data;
3149 struct net_device *dev = idev->dev;
3150 struct in6_addr lladdr;
3152 write_lock(&idev->lock);
3153 if (idev->dead || !(idev->if_flags & IF_READY))
3156 if (!ipv6_accept_ra(idev))
3159 /* Announcement received after solicitation was sent */
3160 if (idev->if_flags & IF_RA_RCVD)
3163 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3164 write_unlock(&idev->lock);
3165 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3166 ndisc_send_rs(dev, &lladdr,
3167 &in6addr_linklocal_allrouters);
3171 write_lock(&idev->lock);
3172 /* The wait after the last probe can be shorter */
3173 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3174 idev->cnf.rtr_solicits) ?
3175 idev->cnf.rtr_solicit_delay :
3176 idev->cnf.rtr_solicit_interval);
3179 * Note: we do not support deprecated "all on-link"
3180 * assumption any longer.
3182 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3186 write_unlock(&idev->lock);
3192 * Duplicate Address Detection
3194 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3196 unsigned long rand_num;
3197 struct inet6_dev *idev = ifp->idev;
3199 if (ifp->flags & IFA_F_OPTIMISTIC)
3202 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3204 ifp->dad_probes = idev->cnf.dad_transmits;
3205 addrconf_mod_dad_work(ifp, rand_num);
3208 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3210 struct inet6_dev *idev = ifp->idev;
3211 struct net_device *dev = idev->dev;
3213 addrconf_join_solict(dev, &ifp->addr);
3215 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3217 read_lock_bh(&idev->lock);
3218 spin_lock(&ifp->lock);
3219 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3222 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3223 idev->cnf.accept_dad < 1 ||
3224 !(ifp->flags&IFA_F_TENTATIVE) ||
3225 ifp->flags & IFA_F_NODAD) {
3226 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3227 spin_unlock(&ifp->lock);
3228 read_unlock_bh(&idev->lock);
3230 addrconf_dad_completed(ifp);
3234 if (!(idev->if_flags & IF_READY)) {
3235 spin_unlock(&ifp->lock);
3236 read_unlock_bh(&idev->lock);
3238 * If the device is not ready:
3239 * - keep it tentative if it is a permanent address.
3240 * - otherwise, kill it.
3243 addrconf_dad_stop(ifp, 0);
3248 * Optimistic nodes can start receiving
3251 if (ifp->flags & IFA_F_OPTIMISTIC) {
3252 ip6_ins_rt(ifp->rt);
3253 if (ipv6_use_optimistic_addr(idev)) {
3254 /* Because optimistic nodes can use this address,
3255 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3257 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3261 addrconf_dad_kick(ifp);
3263 spin_unlock(&ifp->lock);
3264 read_unlock_bh(&idev->lock);
3267 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3269 bool begin_dad = false;
3271 spin_lock_bh(&ifp->state_lock);
3272 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3273 ifp->state = INET6_IFADDR_STATE_PREDAD;
3276 spin_unlock_bh(&ifp->state_lock);
3279 addrconf_mod_dad_work(ifp, 0);
3282 static void addrconf_dad_work(struct work_struct *w)
3284 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3285 struct inet6_ifaddr,
3287 struct inet6_dev *idev = ifp->idev;
3288 struct in6_addr mcaddr;
3294 } action = DAD_PROCESS;
3298 spin_lock_bh(&ifp->state_lock);
3299 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3301 ifp->state = INET6_IFADDR_STATE_DAD;
3302 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3304 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3306 spin_unlock_bh(&ifp->state_lock);
3308 if (action == DAD_BEGIN) {
3309 addrconf_dad_begin(ifp);
3311 } else if (action == DAD_ABORT) {
3312 addrconf_dad_stop(ifp, 1);
3316 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3319 write_lock_bh(&idev->lock);
3320 if (idev->dead || !(idev->if_flags & IF_READY)) {
3321 write_unlock_bh(&idev->lock);
3325 spin_lock(&ifp->lock);
3326 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3327 spin_unlock(&ifp->lock);
3328 write_unlock_bh(&idev->lock);
3332 if (ifp->dad_probes == 0) {
3334 * DAD was successful
3337 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3338 spin_unlock(&ifp->lock);
3339 write_unlock_bh(&idev->lock);
3341 addrconf_dad_completed(ifp);
3347 addrconf_mod_dad_work(ifp,
3348 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3349 spin_unlock(&ifp->lock);
3350 write_unlock_bh(&idev->lock);
3352 /* send a neighbour solicitation for our addr */
3353 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3354 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3360 /* ifp->idev must be at least read locked */
3361 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3363 struct inet6_ifaddr *ifpiter;
3364 struct inet6_dev *idev = ifp->idev;
3366 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3367 if (ifpiter->scope > IFA_LINK)
3369 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3370 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3371 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3378 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3380 struct net_device *dev = ifp->idev->dev;
3381 struct in6_addr lladdr;
3382 bool send_rs, send_mld;
3384 addrconf_del_dad_work(ifp);
3387 * Configure the address for reception. Now it is valid.
3390 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3392 /* If added prefix is link local and we are prepared to process
3393 router advertisements, start sending router solicitations.
3396 read_lock_bh(&ifp->idev->lock);
3397 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3398 send_rs = send_mld &&
3399 ipv6_accept_ra(ifp->idev) &&
3400 ifp->idev->cnf.rtr_solicits > 0 &&
3401 (dev->flags&IFF_LOOPBACK) == 0;
3402 read_unlock_bh(&ifp->idev->lock);
3404 /* While dad is in progress mld report's source address is in6_addrany.
3405 * Resend with proper ll now.
3408 ipv6_mc_dad_complete(ifp->idev);
3412 * If a host as already performed a random delay
3413 * [...] as part of DAD [...] there is no need
3414 * to delay again before sending the first RS
3416 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3418 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3420 write_lock_bh(&ifp->idev->lock);
3421 spin_lock(&ifp->lock);
3422 ifp->idev->rs_probes = 1;
3423 ifp->idev->if_flags |= IF_RS_SENT;
3424 addrconf_mod_rs_timer(ifp->idev,
3425 ifp->idev->cnf.rtr_solicit_interval);
3426 spin_unlock(&ifp->lock);
3427 write_unlock_bh(&ifp->idev->lock);
3431 static void addrconf_dad_run(struct inet6_dev *idev)
3433 struct inet6_ifaddr *ifp;
3435 read_lock_bh(&idev->lock);
3436 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3437 spin_lock(&ifp->lock);
3438 if (ifp->flags & IFA_F_TENTATIVE &&
3439 ifp->state == INET6_IFADDR_STATE_DAD)
3440 addrconf_dad_kick(ifp);
3441 spin_unlock(&ifp->lock);
3443 read_unlock_bh(&idev->lock);
3446 #ifdef CONFIG_PROC_FS
3447 struct if6_iter_state {
3448 struct seq_net_private p;
3453 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3455 struct inet6_ifaddr *ifa = NULL;
3456 struct if6_iter_state *state = seq->private;
3457 struct net *net = seq_file_net(seq);
3460 /* initial bucket if pos is 0 */
3466 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3467 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3469 if (!net_eq(dev_net(ifa->idev->dev), net))
3471 /* sync with offset */
3472 if (p < state->offset) {
3480 /* prepare for next bucket */
3487 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3488 struct inet6_ifaddr *ifa)
3490 struct if6_iter_state *state = seq->private;
3491 struct net *net = seq_file_net(seq);
3493 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3494 if (!net_eq(dev_net(ifa->idev->dev), net))
3500 while (++state->bucket < IN6_ADDR_HSIZE) {
3502 hlist_for_each_entry_rcu_bh(ifa,
3503 &inet6_addr_lst[state->bucket], addr_lst) {
3504 if (!net_eq(dev_net(ifa->idev->dev), net))
3514 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3518 return if6_get_first(seq, *pos);
3521 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3523 struct inet6_ifaddr *ifa;
3525 ifa = if6_get_next(seq, v);
3530 static void if6_seq_stop(struct seq_file *seq, void *v)
3533 rcu_read_unlock_bh();
3536 static int if6_seq_show(struct seq_file *seq, void *v)
3538 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3539 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3541 ifp->idev->dev->ifindex,
3545 ifp->idev->dev->name);
3549 static const struct seq_operations if6_seq_ops = {
3550 .start = if6_seq_start,
3551 .next = if6_seq_next,
3552 .show = if6_seq_show,
3553 .stop = if6_seq_stop,
3556 static int if6_seq_open(struct inode *inode, struct file *file)
3558 return seq_open_net(inode, file, &if6_seq_ops,
3559 sizeof(struct if6_iter_state));
3562 static const struct file_operations if6_fops = {
3563 .owner = THIS_MODULE,
3564 .open = if6_seq_open,
3566 .llseek = seq_lseek,
3567 .release = seq_release_net,
3570 static int __net_init if6_proc_net_init(struct net *net)
3572 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3577 static void __net_exit if6_proc_net_exit(struct net *net)
3579 remove_proc_entry("if_inet6", net->proc_net);
3582 static struct pernet_operations if6_proc_net_ops = {
3583 .init = if6_proc_net_init,
3584 .exit = if6_proc_net_exit,
3587 int __init if6_proc_init(void)
3589 return register_pernet_subsys(&if6_proc_net_ops);
3592 void if6_proc_exit(void)
3594 unregister_pernet_subsys(&if6_proc_net_ops);
3596 #endif /* CONFIG_PROC_FS */
3598 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3599 /* Check if address is a home address configured on any interface. */
3600 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3603 struct inet6_ifaddr *ifp = NULL;
3604 unsigned int hash = inet6_addr_hash(addr);
3607 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3608 if (!net_eq(dev_net(ifp->idev->dev), net))
3610 if (ipv6_addr_equal(&ifp->addr, addr) &&
3611 (ifp->flags & IFA_F_HOMEADDRESS)) {
3616 rcu_read_unlock_bh();
3622 * Periodic address status verification
3625 static void addrconf_verify_rtnl(void)
3627 unsigned long now, next, next_sec, next_sched;
3628 struct inet6_ifaddr *ifp;
3635 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3637 cancel_delayed_work(&addr_chk_work);
3639 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3641 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
3644 /* When setting preferred_lft to a value not zero or
3645 * infinity, while valid_lft is infinity
3646 * IFA_F_PERMANENT has a non-infinity life time.
3648 if ((ifp->flags & IFA_F_PERMANENT) &&
3649 (ifp->prefered_lft == INFINITY_LIFE_TIME))
3652 spin_lock(&ifp->lock);
3653 /* We try to batch several events at once. */
3654 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3656 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3657 age >= ifp->valid_lft) {
3658 spin_unlock(&ifp->lock);
3662 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3663 spin_unlock(&ifp->lock);
3665 } else if (age >= ifp->prefered_lft) {
3666 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3669 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3671 ifp->flags |= IFA_F_DEPRECATED;
3674 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3675 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
3676 next = ifp->tstamp + ifp->valid_lft * HZ;
3678 spin_unlock(&ifp->lock);
3683 ipv6_ifa_notify(0, ifp);
3687 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3688 !(ifp->flags&IFA_F_TENTATIVE)) {
3689 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3690 ifp->idev->cnf.dad_transmits *
3691 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
3693 if (age >= ifp->prefered_lft - regen_advance) {
3694 struct inet6_ifaddr *ifpub = ifp->ifpub;
3695 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3696 next = ifp->tstamp + ifp->prefered_lft * HZ;
3697 if (!ifp->regen_count && ifpub) {
3700 in6_ifa_hold(ifpub);
3701 spin_unlock(&ifp->lock);
3703 spin_lock(&ifpub->lock);
3704 ifpub->regen_count = 0;
3705 spin_unlock(&ifpub->lock);
3706 ipv6_create_tempaddr(ifpub, ifp);
3711 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3712 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3713 spin_unlock(&ifp->lock);
3715 /* ifp->prefered_lft <= ifp->valid_lft */
3716 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3717 next = ifp->tstamp + ifp->prefered_lft * HZ;
3718 spin_unlock(&ifp->lock);
3723 next_sec = round_jiffies_up(next);
3726 /* If rounded timeout is accurate enough, accept it. */
3727 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3728 next_sched = next_sec;
3730 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3731 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3732 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3734 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3735 now, next, next_sec, next_sched);
3736 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
3737 rcu_read_unlock_bh();
3740 static void addrconf_verify_work(struct work_struct *w)
3743 addrconf_verify_rtnl();
3747 static void addrconf_verify(void)
3749 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3752 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3753 struct in6_addr **peer_pfx)
3755 struct in6_addr *pfx = NULL;
3760 pfx = nla_data(addr);
3763 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3765 pfx = nla_data(local);
3771 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3772 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3773 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3774 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3775 [IFA_FLAGS] = { .len = sizeof(u32) },
3779 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3781 struct net *net = sock_net(skb->sk);
3782 struct ifaddrmsg *ifm;
3783 struct nlattr *tb[IFA_MAX+1];
3784 struct in6_addr *pfx, *peer_pfx;
3788 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3792 ifm = nlmsg_data(nlh);
3793 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3797 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3799 /* We ignore other flags so far. */
3800 ifa_flags &= IFA_F_MANAGETEMPADDR;
3802 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
3803 ifm->ifa_prefixlen);
3806 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
3807 u32 prefered_lft, u32 valid_lft)
3811 unsigned long timeout;
3812 bool was_managetempaddr;
3813 bool had_prefixroute;
3817 if (!valid_lft || (prefered_lft > valid_lft))
3820 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
3821 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
3824 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3825 if (addrconf_finite_timeout(timeout)) {
3826 expires = jiffies_to_clock_t(timeout * HZ);
3827 valid_lft = timeout;
3828 flags = RTF_EXPIRES;
3832 ifa_flags |= IFA_F_PERMANENT;
3835 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3836 if (addrconf_finite_timeout(timeout)) {
3838 ifa_flags |= IFA_F_DEPRECATED;
3839 prefered_lft = timeout;
3842 spin_lock_bh(&ifp->lock);
3843 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
3844 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
3845 !(ifp->flags & IFA_F_NOPREFIXROUTE);
3846 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
3847 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3848 IFA_F_NOPREFIXROUTE);
3849 ifp->flags |= ifa_flags;
3850 ifp->tstamp = jiffies;
3851 ifp->valid_lft = valid_lft;
3852 ifp->prefered_lft = prefered_lft;
3854 spin_unlock_bh(&ifp->lock);
3855 if (!(ifp->flags&IFA_F_TENTATIVE))
3856 ipv6_ifa_notify(0, ifp);
3858 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
3859 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3861 } else if (had_prefixroute) {
3862 enum cleanup_prefix_rt_t action;
3863 unsigned long rt_expires;
3865 write_lock_bh(&ifp->idev->lock);
3866 action = check_cleanup_prefix_route(ifp, &rt_expires);
3867 write_unlock_bh(&ifp->idev->lock);
3869 if (action != CLEANUP_PREFIX_RT_NOP) {
3870 cleanup_prefix_route(ifp, rt_expires,
3871 action == CLEANUP_PREFIX_RT_DEL);
3875 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
3876 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
3877 valid_lft = prefered_lft = 0;
3878 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
3879 !was_managetempaddr, jiffies);
3882 addrconf_verify_rtnl();
3888 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3890 struct net *net = sock_net(skb->sk);
3891 struct ifaddrmsg *ifm;
3892 struct nlattr *tb[IFA_MAX+1];
3893 struct in6_addr *pfx, *peer_pfx;
3894 struct inet6_ifaddr *ifa;
3895 struct net_device *dev;
3896 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3900 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3904 ifm = nlmsg_data(nlh);
3905 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3909 if (tb[IFA_CACHEINFO]) {
3910 struct ifa_cacheinfo *ci;
3912 ci = nla_data(tb[IFA_CACHEINFO]);
3913 valid_lft = ci->ifa_valid;
3914 preferred_lft = ci->ifa_prefered;
3916 preferred_lft = INFINITY_LIFE_TIME;
3917 valid_lft = INFINITY_LIFE_TIME;
3920 dev = __dev_get_by_index(net, ifm->ifa_index);
3924 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3926 /* We ignore other flags so far. */
3927 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3928 IFA_F_NOPREFIXROUTE;
3930 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3933 * It would be best to check for !NLM_F_CREATE here but
3934 * userspace already relies on not having to provide this.
3936 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3937 ifm->ifa_prefixlen, ifa_flags,
3938 preferred_lft, valid_lft);
3941 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3942 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3945 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3952 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
3953 u8 scope, int ifindex)
3955 struct ifaddrmsg *ifm;
3957 ifm = nlmsg_data(nlh);
3958 ifm->ifa_family = AF_INET6;
3959 ifm->ifa_prefixlen = prefixlen;
3960 ifm->ifa_flags = flags;
3961 ifm->ifa_scope = scope;
3962 ifm->ifa_index = ifindex;
3965 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3966 unsigned long tstamp, u32 preferred, u32 valid)
3968 struct ifa_cacheinfo ci;
3970 ci.cstamp = cstamp_delta(cstamp);
3971 ci.tstamp = cstamp_delta(tstamp);
3972 ci.ifa_prefered = preferred;
3973 ci.ifa_valid = valid;
3975 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3978 static inline int rt_scope(int ifa_scope)
3980 if (ifa_scope & IFA_HOST)
3981 return RT_SCOPE_HOST;
3982 else if (ifa_scope & IFA_LINK)
3983 return RT_SCOPE_LINK;
3984 else if (ifa_scope & IFA_SITE)
3985 return RT_SCOPE_SITE;
3987 return RT_SCOPE_UNIVERSE;
3990 static inline int inet6_ifaddr_msgsize(void)
3992 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3993 + nla_total_size(16) /* IFA_LOCAL */
3994 + nla_total_size(16) /* IFA_ADDRESS */
3995 + nla_total_size(sizeof(struct ifa_cacheinfo))
3996 + nla_total_size(4) /* IFA_FLAGS */;
3999 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4000 u32 portid, u32 seq, int event, unsigned int flags)
4002 struct nlmsghdr *nlh;
4003 u32 preferred, valid;
4005 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4009 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4010 ifa->idev->dev->ifindex);
4012 if (!((ifa->flags&IFA_F_PERMANENT) &&
4013 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4014 preferred = ifa->prefered_lft;
4015 valid = ifa->valid_lft;
4016 if (preferred != INFINITY_LIFE_TIME) {
4017 long tval = (jiffies - ifa->tstamp)/HZ;
4018 if (preferred > tval)
4022 if (valid != INFINITY_LIFE_TIME) {
4030 preferred = INFINITY_LIFE_TIME;
4031 valid = INFINITY_LIFE_TIME;
4034 if (!ipv6_addr_any(&ifa->peer_addr)) {
4035 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
4036 nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
4039 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
4042 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4045 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4048 return nlmsg_end(skb, nlh);
4051 nlmsg_cancel(skb, nlh);
4055 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4056 u32 portid, u32 seq, int event, u16 flags)
4058 struct nlmsghdr *nlh;
4059 u8 scope = RT_SCOPE_UNIVERSE;
4060 int ifindex = ifmca->idev->dev->ifindex;
4062 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4063 scope = RT_SCOPE_SITE;
4065 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4069 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4070 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
4071 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4072 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4073 nlmsg_cancel(skb, nlh);
4077 return nlmsg_end(skb, nlh);
4080 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4081 u32 portid, u32 seq, int event, unsigned int flags)
4083 struct nlmsghdr *nlh;
4084 u8 scope = RT_SCOPE_UNIVERSE;
4085 int ifindex = ifaca->aca_idev->dev->ifindex;
4087 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4088 scope = RT_SCOPE_SITE;
4090 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4094 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4095 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
4096 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4097 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4098 nlmsg_cancel(skb, nlh);
4102 return nlmsg_end(skb, nlh);
4111 /* called with rcu_read_lock() */
4112 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4113 struct netlink_callback *cb, enum addr_type_t type,
4114 int s_ip_idx, int *p_ip_idx)
4116 struct ifmcaddr6 *ifmca;
4117 struct ifacaddr6 *ifaca;
4119 int ip_idx = *p_ip_idx;
4121 read_lock_bh(&idev->lock);
4123 case UNICAST_ADDR: {
4124 struct inet6_ifaddr *ifa;
4126 /* unicast address incl. temp addr */
4127 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4128 if (++ip_idx < s_ip_idx)
4130 err = inet6_fill_ifaddr(skb, ifa,
4131 NETLINK_CB(cb->skb).portid,
4137 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4141 case MULTICAST_ADDR:
4142 /* multicast address */
4143 for (ifmca = idev->mc_list; ifmca;
4144 ifmca = ifmca->next, ip_idx++) {
4145 if (ip_idx < s_ip_idx)
4147 err = inet6_fill_ifmcaddr(skb, ifmca,
4148 NETLINK_CB(cb->skb).portid,
4157 /* anycast address */
4158 for (ifaca = idev->ac_list; ifaca;
4159 ifaca = ifaca->aca_next, ip_idx++) {
4160 if (ip_idx < s_ip_idx)
4162 err = inet6_fill_ifacaddr(skb, ifaca,
4163 NETLINK_CB(cb->skb).portid,
4174 read_unlock_bh(&idev->lock);
4179 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4180 enum addr_type_t type)
4182 struct net *net = sock_net(skb->sk);
4185 int s_idx, s_ip_idx;
4186 struct net_device *dev;
4187 struct inet6_dev *idev;
4188 struct hlist_head *head;
4191 s_idx = idx = cb->args[1];
4192 s_ip_idx = ip_idx = cb->args[2];
4195 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4196 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4198 head = &net->dev_index_head[h];
4199 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4202 if (h > s_h || idx > s_idx)
4205 idev = __in6_dev_get(dev);
4209 if (in6_dump_addrs(idev, skb, cb, type,
4210 s_ip_idx, &ip_idx) <= 0)
4220 cb->args[2] = ip_idx;
4225 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4227 enum addr_type_t type = UNICAST_ADDR;
4229 return inet6_dump_addr(skb, cb, type);
4232 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4234 enum addr_type_t type = MULTICAST_ADDR;
4236 return inet6_dump_addr(skb, cb, type);
4240 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4242 enum addr_type_t type = ANYCAST_ADDR;
4244 return inet6_dump_addr(skb, cb, type);
4247 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4249 struct net *net = sock_net(in_skb->sk);
4250 struct ifaddrmsg *ifm;
4251 struct nlattr *tb[IFA_MAX+1];
4252 struct in6_addr *addr = NULL, *peer;
4253 struct net_device *dev = NULL;
4254 struct inet6_ifaddr *ifa;
4255 struct sk_buff *skb;
4258 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4262 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4268 ifm = nlmsg_data(nlh);
4270 dev = __dev_get_by_index(net, ifm->ifa_index);
4272 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4274 err = -EADDRNOTAVAIL;
4278 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4284 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4285 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4287 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4288 WARN_ON(err == -EMSGSIZE);
4292 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4299 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4301 struct sk_buff *skb;
4302 struct net *net = dev_net(ifa->idev->dev);
4305 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4309 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4311 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4312 WARN_ON(err == -EMSGSIZE);
4316 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4320 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4323 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4324 __s32 *array, int bytes)
4326 BUG_ON(bytes < (DEVCONF_MAX * 4));
4328 memset(array, 0, bytes);
4329 array[DEVCONF_FORWARDING] = cnf->forwarding;
4330 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4331 array[DEVCONF_MTU6] = cnf->mtu6;
4332 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4333 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4334 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4335 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4336 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4337 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4338 jiffies_to_msecs(cnf->rtr_solicit_interval);
4339 array[DEVCONF_RTR_SOLICIT_DELAY] =
4340 jiffies_to_msecs(cnf->rtr_solicit_delay);
4341 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4342 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4343 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4344 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4345 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4346 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4347 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4348 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4349 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4350 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4351 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4352 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4353 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4354 #ifdef CONFIG_IPV6_ROUTER_PREF
4355 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4356 array[DEVCONF_RTR_PROBE_INTERVAL] =
4357 jiffies_to_msecs(cnf->rtr_probe_interval);
4358 #ifdef CONFIG_IPV6_ROUTE_INFO
4359 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4362 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4363 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4364 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4365 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4366 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4368 #ifdef CONFIG_IPV6_MROUTE
4369 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4371 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4372 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4373 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4374 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4375 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4376 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4379 static inline size_t inet6_ifla6_size(void)
4381 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4382 + nla_total_size(sizeof(struct ifla_cacheinfo))
4383 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4384 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4385 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4386 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4389 static inline size_t inet6_if_nlmsg_size(void)
4391 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4392 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4393 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4394 + nla_total_size(4) /* IFLA_MTU */
4395 + nla_total_size(4) /* IFLA_LINK */
4396 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4399 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4400 int items, int bytes)
4403 int pad = bytes - sizeof(u64) * items;
4406 /* Use put_unaligned() because stats may not be aligned for u64. */
4407 put_unaligned(items, &stats[0]);
4408 for (i = 1; i < items; i++)
4409 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4411 memset(&stats[items], 0, pad);
4414 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
4415 int items, int bytes, size_t syncpoff)
4418 int pad = bytes - sizeof(u64) * items;
4421 /* Use put_unaligned() because stats may not be aligned for u64. */
4422 put_unaligned(items, &stats[0]);
4423 for (i = 1; i < items; i++)
4424 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4426 memset(&stats[items], 0, pad);
4429 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4433 case IFLA_INET6_STATS:
4434 __snmp6_fill_stats64(stats, idev->stats.ipv6,
4435 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4437 case IFLA_INET6_ICMP6STATS:
4438 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4443 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4446 struct ifla_cacheinfo ci;
4448 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4449 goto nla_put_failure;
4450 ci.max_reasm_len = IPV6_MAXPLEN;
4451 ci.tstamp = cstamp_delta(idev->tstamp);
4452 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4453 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4454 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4455 goto nla_put_failure;
4456 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4458 goto nla_put_failure;
4459 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4461 /* XXX - MC not implemented */
4463 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4465 goto nla_put_failure;
4466 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4468 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4470 goto nla_put_failure;
4471 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4473 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4475 goto nla_put_failure;
4477 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4478 goto nla_put_failure;
4480 read_lock_bh(&idev->lock);
4481 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4482 read_unlock_bh(&idev->lock);
4490 static size_t inet6_get_link_af_size(const struct net_device *dev)
4492 if (!__in6_dev_get(dev))
4495 return inet6_ifla6_size();
4498 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4500 struct inet6_dev *idev = __in6_dev_get(dev);
4505 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4511 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4513 struct inet6_ifaddr *ifp;
4514 struct net_device *dev = idev->dev;
4515 bool update_rs = false;
4516 struct in6_addr ll_addr;
4522 if (ipv6_addr_any(token))
4524 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4526 if (!ipv6_accept_ra(idev))
4528 if (idev->cnf.rtr_solicits <= 0)
4531 write_lock_bh(&idev->lock);
4533 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4534 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4536 write_unlock_bh(&idev->lock);
4538 if (!idev->dead && (idev->if_flags & IF_READY) &&
4539 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4540 IFA_F_OPTIMISTIC)) {
4542 /* If we're not ready, then normal ifup will take care
4543 * of this. Otherwise, we need to request our rs here.
4545 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4549 write_lock_bh(&idev->lock);
4552 idev->if_flags |= IF_RS_SENT;
4553 idev->rs_probes = 1;
4554 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4557 /* Well, that's kinda nasty ... */
4558 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4559 spin_lock(&ifp->lock);
4560 if (ifp->tokenized) {
4562 ifp->prefered_lft = 0;
4564 spin_unlock(&ifp->lock);
4567 write_unlock_bh(&idev->lock);
4568 inet6_ifinfo_notify(RTM_NEWLINK, idev);
4569 addrconf_verify_rtnl();
4573 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4576 struct inet6_dev *idev = __in6_dev_get(dev);
4577 struct nlattr *tb[IFLA_INET6_MAX + 1];
4580 return -EAFNOSUPPORT;
4582 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4585 if (tb[IFLA_INET6_TOKEN]) {
4586 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4591 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4592 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4594 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4595 mode != IN6_ADDR_GEN_MODE_NONE)
4597 idev->addr_gen_mode = mode;
4604 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4605 u32 portid, u32 seq, int event, unsigned int flags)
4607 struct net_device *dev = idev->dev;
4608 struct ifinfomsg *hdr;
4609 struct nlmsghdr *nlh;
4612 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4616 hdr = nlmsg_data(nlh);
4617 hdr->ifi_family = AF_INET6;
4619 hdr->ifi_type = dev->type;
4620 hdr->ifi_index = dev->ifindex;
4621 hdr->ifi_flags = dev_get_flags(dev);
4622 hdr->ifi_change = 0;
4624 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4626 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4627 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4628 (dev->ifindex != dev->iflink &&
4629 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4630 goto nla_put_failure;
4631 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4632 if (protoinfo == NULL)
4633 goto nla_put_failure;
4635 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4636 goto nla_put_failure;
4638 nla_nest_end(skb, protoinfo);
4639 return nlmsg_end(skb, nlh);
4642 nlmsg_cancel(skb, nlh);
4646 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4648 struct net *net = sock_net(skb->sk);
4651 struct net_device *dev;
4652 struct inet6_dev *idev;
4653 struct hlist_head *head;
4656 s_idx = cb->args[1];
4659 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4661 head = &net->dev_index_head[h];
4662 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4665 idev = __in6_dev_get(dev);
4668 if (inet6_fill_ifinfo(skb, idev,
4669 NETLINK_CB(cb->skb).portid,
4671 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4685 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4687 struct sk_buff *skb;
4688 struct net *net = dev_net(idev->dev);
4691 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4695 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4697 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4698 WARN_ON(err == -EMSGSIZE);
4702 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4706 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4709 static inline size_t inet6_prefix_nlmsg_size(void)
4711 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4712 + nla_total_size(sizeof(struct in6_addr))
4713 + nla_total_size(sizeof(struct prefix_cacheinfo));
4716 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4717 struct prefix_info *pinfo, u32 portid, u32 seq,
4718 int event, unsigned int flags)
4720 struct prefixmsg *pmsg;
4721 struct nlmsghdr *nlh;
4722 struct prefix_cacheinfo ci;
4724 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4728 pmsg = nlmsg_data(nlh);
4729 pmsg->prefix_family = AF_INET6;
4730 pmsg->prefix_pad1 = 0;
4731 pmsg->prefix_pad2 = 0;
4732 pmsg->prefix_ifindex = idev->dev->ifindex;
4733 pmsg->prefix_len = pinfo->prefix_len;
4734 pmsg->prefix_type = pinfo->type;
4735 pmsg->prefix_pad3 = 0;
4736 pmsg->prefix_flags = 0;
4738 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4739 if (pinfo->autoconf)
4740 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4742 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4743 goto nla_put_failure;
4744 ci.preferred_time = ntohl(pinfo->prefered);
4745 ci.valid_time = ntohl(pinfo->valid);
4746 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4747 goto nla_put_failure;
4748 return nlmsg_end(skb, nlh);
4751 nlmsg_cancel(skb, nlh);
4755 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4756 struct prefix_info *pinfo)
4758 struct sk_buff *skb;
4759 struct net *net = dev_net(idev->dev);
4762 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4766 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
4768 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4769 WARN_ON(err == -EMSGSIZE);
4773 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4777 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4780 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4782 struct net *net = dev_net(ifp->idev->dev);
4787 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4792 * If the address was optimistic
4793 * we inserted the route at the start of
4794 * our DAD process, so we don't need
4797 if (!(ifp->rt->rt6i_node))
4798 ip6_ins_rt(ifp->rt);
4799 if (ifp->idev->cnf.forwarding)
4800 addrconf_join_anycast(ifp);
4801 if (!ipv6_addr_any(&ifp->peer_addr))
4802 addrconf_prefix_route(&ifp->peer_addr, 128,
4803 ifp->idev->dev, 0, 0);
4806 if (ifp->idev->cnf.forwarding)
4807 addrconf_leave_anycast(ifp);
4808 addrconf_leave_solict(ifp->idev, &ifp->addr);
4809 if (!ipv6_addr_any(&ifp->peer_addr)) {
4810 struct rt6_info *rt;
4812 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
4813 ifp->idev->dev, 0, 0);
4814 if (rt && ip6_del_rt(rt))
4817 dst_hold(&ifp->rt->dst);
4819 if (ip6_del_rt(ifp->rt))
4820 dst_free(&ifp->rt->dst);
4822 rt_genid_bump_ipv6(net);
4825 atomic_inc(&net->ipv6.dev_addr_genid);
4828 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4831 if (likely(ifp->idev->dead == 0))
4832 __ipv6_ifa_notify(event, ifp);
4833 rcu_read_unlock_bh();
4836 #ifdef CONFIG_SYSCTL
4839 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
4840 void __user *buffer, size_t *lenp, loff_t *ppos)
4842 int *valp = ctl->data;
4845 struct ctl_table lctl;
4849 * ctl->data points to idev->cnf.forwarding, we should
4850 * not modify it until we get the rtnl lock.
4855 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4858 ret = addrconf_fixup_forwarding(ctl, valp, val);
4864 static void dev_disable_change(struct inet6_dev *idev)
4866 struct netdev_notifier_info info;
4868 if (!idev || !idev->dev)
4871 netdev_notifier_info_init(&info, idev->dev);
4872 if (idev->cnf.disable_ipv6)
4873 addrconf_notify(NULL, NETDEV_DOWN, &info);
4875 addrconf_notify(NULL, NETDEV_UP, &info);
4878 static void addrconf_disable_change(struct net *net, __s32 newf)
4880 struct net_device *dev;
4881 struct inet6_dev *idev;
4884 for_each_netdev_rcu(net, dev) {
4885 idev = __in6_dev_get(dev);
4887 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4888 idev->cnf.disable_ipv6 = newf;
4890 dev_disable_change(idev);
4896 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
4901 if (!rtnl_trylock())
4902 return restart_syscall();
4904 net = (struct net *)table->extra2;
4908 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4913 if (p == &net->ipv6.devconf_all->disable_ipv6) {
4914 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4915 addrconf_disable_change(net, newf);
4916 } else if ((!newf) ^ (!old))
4917 dev_disable_change((struct inet6_dev *)table->extra1);
4924 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
4925 void __user *buffer, size_t *lenp, loff_t *ppos)
4927 int *valp = ctl->data;
4930 struct ctl_table lctl;
4934 * ctl->data points to idev->cnf.disable_ipv6, we should
4935 * not modify it until we get the rtnl lock.
4940 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4943 ret = addrconf_disable_ipv6(ctl, valp, val);
4950 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
4951 void __user *buffer, size_t *lenp, loff_t *ppos)
4953 int *valp = ctl->data;
4958 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
4961 if (write && old != new) {
4962 struct net *net = ctl->extra2;
4964 if (!rtnl_trylock())
4965 return restart_syscall();
4967 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
4968 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4969 NETCONFA_IFINDEX_DEFAULT,
4970 net->ipv6.devconf_dflt);
4971 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
4972 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4973 NETCONFA_IFINDEX_ALL,
4974 net->ipv6.devconf_all);
4976 struct inet6_dev *idev = ctl->extra1;
4978 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4989 static struct addrconf_sysctl_table
4991 struct ctl_table_header *sysctl_header;
4992 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
4993 } addrconf_sysctl __read_mostly = {
4994 .sysctl_header = NULL,
4997 .procname = "forwarding",
4998 .data = &ipv6_devconf.forwarding,
4999 .maxlen = sizeof(int),
5001 .proc_handler = addrconf_sysctl_forward,
5004 .procname = "hop_limit",
5005 .data = &ipv6_devconf.hop_limit,
5006 .maxlen = sizeof(int),
5008 .proc_handler = proc_dointvec,
5012 .data = &ipv6_devconf.mtu6,
5013 .maxlen = sizeof(int),
5015 .proc_handler = proc_dointvec,
5018 .procname = "accept_ra",
5019 .data = &ipv6_devconf.accept_ra,
5020 .maxlen = sizeof(int),
5022 .proc_handler = proc_dointvec,
5025 .procname = "accept_redirects",
5026 .data = &ipv6_devconf.accept_redirects,
5027 .maxlen = sizeof(int),
5029 .proc_handler = proc_dointvec,
5032 .procname = "autoconf",
5033 .data = &ipv6_devconf.autoconf,
5034 .maxlen = sizeof(int),
5036 .proc_handler = proc_dointvec,
5039 .procname = "dad_transmits",
5040 .data = &ipv6_devconf.dad_transmits,
5041 .maxlen = sizeof(int),
5043 .proc_handler = proc_dointvec,
5046 .procname = "router_solicitations",
5047 .data = &ipv6_devconf.rtr_solicits,
5048 .maxlen = sizeof(int),
5050 .proc_handler = proc_dointvec,
5053 .procname = "router_solicitation_interval",
5054 .data = &ipv6_devconf.rtr_solicit_interval,
5055 .maxlen = sizeof(int),
5057 .proc_handler = proc_dointvec_jiffies,
5060 .procname = "router_solicitation_delay",
5061 .data = &ipv6_devconf.rtr_solicit_delay,
5062 .maxlen = sizeof(int),
5064 .proc_handler = proc_dointvec_jiffies,
5067 .procname = "force_mld_version",
5068 .data = &ipv6_devconf.force_mld_version,
5069 .maxlen = sizeof(int),
5071 .proc_handler = proc_dointvec,
5074 .procname = "mldv1_unsolicited_report_interval",
5076 &ipv6_devconf.mldv1_unsolicited_report_interval,
5077 .maxlen = sizeof(int),
5079 .proc_handler = proc_dointvec_ms_jiffies,
5082 .procname = "mldv2_unsolicited_report_interval",
5084 &ipv6_devconf.mldv2_unsolicited_report_interval,
5085 .maxlen = sizeof(int),
5087 .proc_handler = proc_dointvec_ms_jiffies,
5090 .procname = "use_tempaddr",
5091 .data = &ipv6_devconf.use_tempaddr,
5092 .maxlen = sizeof(int),
5094 .proc_handler = proc_dointvec,
5097 .procname = "temp_valid_lft",
5098 .data = &ipv6_devconf.temp_valid_lft,
5099 .maxlen = sizeof(int),
5101 .proc_handler = proc_dointvec,
5104 .procname = "temp_prefered_lft",
5105 .data = &ipv6_devconf.temp_prefered_lft,
5106 .maxlen = sizeof(int),
5108 .proc_handler = proc_dointvec,
5111 .procname = "regen_max_retry",
5112 .data = &ipv6_devconf.regen_max_retry,
5113 .maxlen = sizeof(int),
5115 .proc_handler = proc_dointvec,
5118 .procname = "max_desync_factor",
5119 .data = &ipv6_devconf.max_desync_factor,
5120 .maxlen = sizeof(int),
5122 .proc_handler = proc_dointvec,
5125 .procname = "max_addresses",
5126 .data = &ipv6_devconf.max_addresses,
5127 .maxlen = sizeof(int),
5129 .proc_handler = proc_dointvec,
5132 .procname = "accept_ra_defrtr",
5133 .data = &ipv6_devconf.accept_ra_defrtr,
5134 .maxlen = sizeof(int),
5136 .proc_handler = proc_dointvec,
5139 .procname = "accept_ra_pinfo",
5140 .data = &ipv6_devconf.accept_ra_pinfo,
5141 .maxlen = sizeof(int),
5143 .proc_handler = proc_dointvec,
5145 #ifdef CONFIG_IPV6_ROUTER_PREF
5147 .procname = "accept_ra_rtr_pref",
5148 .data = &ipv6_devconf.accept_ra_rtr_pref,
5149 .maxlen = sizeof(int),
5151 .proc_handler = proc_dointvec,
5154 .procname = "router_probe_interval",
5155 .data = &ipv6_devconf.rtr_probe_interval,
5156 .maxlen = sizeof(int),
5158 .proc_handler = proc_dointvec_jiffies,
5160 #ifdef CONFIG_IPV6_ROUTE_INFO
5162 .procname = "accept_ra_rt_info_max_plen",
5163 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5164 .maxlen = sizeof(int),
5166 .proc_handler = proc_dointvec,
5171 .procname = "proxy_ndp",
5172 .data = &ipv6_devconf.proxy_ndp,
5173 .maxlen = sizeof(int),
5175 .proc_handler = addrconf_sysctl_proxy_ndp,
5178 .procname = "accept_source_route",
5179 .data = &ipv6_devconf.accept_source_route,
5180 .maxlen = sizeof(int),
5182 .proc_handler = proc_dointvec,
5184 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5186 .procname = "optimistic_dad",
5187 .data = &ipv6_devconf.optimistic_dad,
5188 .maxlen = sizeof(int),
5190 .proc_handler = proc_dointvec,
5194 .procname = "use_optimistic",
5195 .data = &ipv6_devconf.use_optimistic,
5196 .maxlen = sizeof(int),
5198 .proc_handler = proc_dointvec,
5202 #ifdef CONFIG_IPV6_MROUTE
5204 .procname = "mc_forwarding",
5205 .data = &ipv6_devconf.mc_forwarding,
5206 .maxlen = sizeof(int),
5208 .proc_handler = proc_dointvec,
5212 .procname = "disable_ipv6",
5213 .data = &ipv6_devconf.disable_ipv6,
5214 .maxlen = sizeof(int),
5216 .proc_handler = addrconf_sysctl_disable,
5219 .procname = "accept_dad",
5220 .data = &ipv6_devconf.accept_dad,
5221 .maxlen = sizeof(int),
5223 .proc_handler = proc_dointvec,
5226 .procname = "force_tllao",
5227 .data = &ipv6_devconf.force_tllao,
5228 .maxlen = sizeof(int),
5230 .proc_handler = proc_dointvec
5233 .procname = "ndisc_notify",
5234 .data = &ipv6_devconf.ndisc_notify,
5235 .maxlen = sizeof(int),
5237 .proc_handler = proc_dointvec
5240 .procname = "suppress_frag_ndisc",
5241 .data = &ipv6_devconf.suppress_frag_ndisc,
5242 .maxlen = sizeof(int),
5244 .proc_handler = proc_dointvec
5247 .procname = "accept_ra_from_local",
5248 .data = &ipv6_devconf.accept_ra_from_local,
5249 .maxlen = sizeof(int),
5251 .proc_handler = proc_dointvec,
5259 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5260 struct inet6_dev *idev, struct ipv6_devconf *p)
5263 struct addrconf_sysctl_table *t;
5264 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5266 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5270 for (i = 0; t->addrconf_vars[i].data; i++) {
5271 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5272 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5273 t->addrconf_vars[i].extra2 = net;
5276 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5278 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5279 if (t->sysctl_header == NULL)
5291 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5293 struct addrconf_sysctl_table *t;
5295 if (p->sysctl == NULL)
5300 unregister_net_sysctl_table(t->sysctl_header);
5304 static int addrconf_sysctl_register(struct inet6_dev *idev)
5308 if (!sysctl_dev_name_is_allowed(idev->dev->name))
5311 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5312 &ndisc_ifinfo_sysctl_change);
5315 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5318 neigh_sysctl_unregister(idev->nd_parms);
5323 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5325 __addrconf_sysctl_unregister(&idev->cnf);
5326 neigh_sysctl_unregister(idev->nd_parms);
5332 static int __net_init addrconf_init_net(struct net *net)
5335 struct ipv6_devconf *all, *dflt;
5337 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5341 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5343 goto err_alloc_dflt;
5345 /* these will be inherited by all namespaces */
5346 dflt->autoconf = ipv6_defaults.autoconf;
5347 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5349 net->ipv6.devconf_all = all;
5350 net->ipv6.devconf_dflt = dflt;
5352 #ifdef CONFIG_SYSCTL
5353 err = __addrconf_sysctl_register(net, "all", NULL, all);
5357 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5363 #ifdef CONFIG_SYSCTL
5365 __addrconf_sysctl_unregister(all);
5375 static void __net_exit addrconf_exit_net(struct net *net)
5377 #ifdef CONFIG_SYSCTL
5378 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5379 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5381 if (!net_eq(net, &init_net)) {
5382 kfree(net->ipv6.devconf_dflt);
5383 kfree(net->ipv6.devconf_all);
5387 static struct pernet_operations addrconf_ops = {
5388 .init = addrconf_init_net,
5389 .exit = addrconf_exit_net,
5392 static struct rtnl_af_ops inet6_ops = {
5394 .fill_link_af = inet6_fill_link_af,
5395 .get_link_af_size = inet6_get_link_af_size,
5396 .set_link_af = inet6_set_link_af,
5400 * Init / cleanup code
5403 int __init addrconf_init(void)
5405 struct inet6_dev *idev;
5408 err = ipv6_addr_label_init();
5410 pr_crit("%s: cannot initialize default policy table: %d\n",
5415 err = register_pernet_subsys(&addrconf_ops);
5419 addrconf_wq = create_workqueue("ipv6_addrconf");
5425 /* The addrconf netdev notifier requires that loopback_dev
5426 * has it's ipv6 private information allocated and setup
5427 * before it can bring up and give link-local addresses
5428 * to other devices which are up.
5430 * Unfortunately, loopback_dev is not necessarily the first
5431 * entry in the global dev_base list of net devices. In fact,
5432 * it is likely to be the very last entry on that list.
5433 * So this causes the notifier registry below to try and
5434 * give link-local addresses to all devices besides loopback_dev
5435 * first, then loopback_dev, which cases all the non-loopback_dev
5436 * devices to fail to get a link-local address.
5438 * So, as a temporary fix, allocate the ipv6 structure for
5439 * loopback_dev first by hand.
5440 * Longer term, all of the dependencies ipv6 has upon the loopback
5441 * device and it being up should be removed.
5444 idev = ipv6_add_dev(init_net.loopback_dev);
5447 err = PTR_ERR(idev);
5451 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5452 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5454 register_netdevice_notifier(&ipv6_dev_notf);
5458 rtnl_af_register(&inet6_ops);
5460 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5465 /* Only the first call to __rtnl_register can fail */
5466 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5467 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5468 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5469 inet6_dump_ifaddr, NULL);
5470 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5471 inet6_dump_ifmcaddr, NULL);
5472 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5473 inet6_dump_ifacaddr, NULL);
5474 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5475 inet6_netconf_dump_devconf, NULL);
5477 ipv6_addr_label_rtnl_register();
5481 rtnl_af_unregister(&inet6_ops);
5482 unregister_netdevice_notifier(&ipv6_dev_notf);
5484 destroy_workqueue(addrconf_wq);
5486 unregister_pernet_subsys(&addrconf_ops);
5488 ipv6_addr_label_cleanup();
5493 void addrconf_cleanup(void)
5495 struct net_device *dev;
5498 unregister_netdevice_notifier(&ipv6_dev_notf);
5499 unregister_pernet_subsys(&addrconf_ops);
5500 ipv6_addr_label_cleanup();
5504 __rtnl_af_unregister(&inet6_ops);
5506 /* clean dev list */
5507 for_each_netdev(&init_net, dev) {
5508 if (__in6_dev_get(dev) == NULL)
5510 addrconf_ifdown(dev, 1);
5512 addrconf_ifdown(init_net.loopback_dev, 2);
5517 spin_lock_bh(&addrconf_hash_lock);
5518 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5519 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5520 spin_unlock_bh(&addrconf_hash_lock);
5521 cancel_delayed_work(&addr_chk_work);
5524 destroy_workqueue(addrconf_wq);