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,
207 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
209 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
210 .mtu6 = IPV6_MIN_MTU,
212 .accept_redirects = 1,
214 .force_mld_version = 0,
215 .mldv1_unsolicited_report_interval = 10 * HZ,
216 .mldv2_unsolicited_report_interval = HZ,
218 .rtr_solicits = MAX_RTR_SOLICITATIONS,
219 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
220 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
222 .temp_valid_lft = TEMP_VALID_LIFETIME,
223 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
224 .regen_max_retry = REGEN_MAX_RETRY,
225 .max_desync_factor = MAX_DESYNC_FACTOR,
226 .max_addresses = IPV6_MAX_ADDRESSES,
227 .accept_ra_defrtr = 1,
228 .accept_ra_from_local = 0,
229 .accept_ra_pinfo = 1,
230 #ifdef CONFIG_IPV6_ROUTER_PREF
231 .accept_ra_rtr_pref = 1,
232 .rtr_probe_interval = 60 * HZ,
233 #ifdef CONFIG_IPV6_ROUTE_INFO
234 .accept_ra_rt_info_max_plen = 0,
238 .accept_source_route = 0, /* we do not accept RH0 by default. */
241 .suppress_frag_ndisc = 1,
245 /* Check if a valid qdisc is available */
246 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
248 return !qdisc_tx_is_noop(dev);
251 static void addrconf_del_rs_timer(struct inet6_dev *idev)
253 if (del_timer(&idev->rs_timer))
257 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
259 if (cancel_delayed_work(&ifp->dad_work))
263 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
266 if (!timer_pending(&idev->rs_timer))
268 mod_timer(&idev->rs_timer, jiffies + when);
271 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
274 if (!delayed_work_pending(&ifp->dad_work))
276 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
279 static int snmp6_alloc_dev(struct inet6_dev *idev)
283 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
284 if (!idev->stats.ipv6)
287 for_each_possible_cpu(i) {
288 struct ipstats_mib *addrconf_stats;
289 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
290 u64_stats_init(&addrconf_stats->syncp);
294 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
296 if (!idev->stats.icmpv6dev)
298 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
300 if (!idev->stats.icmpv6msgdev)
306 kfree(idev->stats.icmpv6dev);
308 free_percpu(idev->stats.ipv6);
313 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
315 struct inet6_dev *ndev;
320 if (dev->mtu < IPV6_MIN_MTU)
321 return ERR_PTR(-EINVAL);
323 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
327 rwlock_init(&ndev->lock);
329 INIT_LIST_HEAD(&ndev->addr_list);
330 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
331 (unsigned long)ndev);
332 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
333 ndev->cnf.mtu6 = dev->mtu;
334 ndev->cnf.sysctl = NULL;
335 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
336 if (ndev->nd_parms == NULL) {
340 if (ndev->cnf.forwarding)
341 dev_disable_lro(dev);
342 /* We refer to the device */
345 if (snmp6_alloc_dev(ndev) < 0) {
347 "%s: cannot allocate memory for statistics; dev=%s.\n",
348 __func__, dev->name);
349 neigh_parms_release(&nd_tbl, ndev->nd_parms);
355 if (snmp6_register_dev(ndev) < 0) {
357 "%s: cannot create /proc/net/dev_snmp6/%s\n",
358 __func__, dev->name);
362 /* One reference from device. We must do this before
363 * we invoke __ipv6_regen_rndid().
367 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
368 ndev->cnf.accept_dad = -1;
370 #if IS_ENABLED(CONFIG_IPV6_SIT)
371 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
372 pr_info("%s: Disabled Multicast RS\n", dev->name);
373 ndev->cnf.rtr_solicits = 0;
377 INIT_LIST_HEAD(&ndev->tempaddr_list);
378 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
379 if ((dev->flags&IFF_LOOPBACK) ||
380 dev->type == ARPHRD_TUNNEL ||
381 dev->type == ARPHRD_TUNNEL6 ||
382 dev->type == ARPHRD_SIT ||
383 dev->type == ARPHRD_NONE) {
384 ndev->cnf.use_tempaddr = -1;
387 ipv6_regen_rndid((unsigned long) ndev);
390 ndev->token = in6addr_any;
392 if (netif_running(dev) && addrconf_qdisc_ok(dev))
393 ndev->if_flags |= IF_READY;
395 ipv6_mc_init_dev(ndev);
396 ndev->tstamp = jiffies;
397 err = addrconf_sysctl_register(ndev);
399 ipv6_mc_destroy_dev(ndev);
400 del_timer(&ndev->regen_timer);
403 /* protected by rtnl_lock */
404 rcu_assign_pointer(dev->ip6_ptr, ndev);
406 /* Join interface-local all-node multicast group */
407 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
409 /* Join all-node multicast group */
410 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
412 /* Join all-router multicast group if forwarding is set */
413 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
414 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
419 neigh_parms_release(&nd_tbl, ndev->nd_parms);
421 in6_dev_finish_destroy(ndev);
425 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
427 struct inet6_dev *idev;
431 idev = __in6_dev_get(dev);
433 idev = ipv6_add_dev(dev);
438 if (dev->flags&IFF_UP)
443 static int inet6_netconf_msgsize_devconf(int type)
445 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
446 + nla_total_size(4); /* NETCONFA_IFINDEX */
448 /* type -1 is used for ALL */
449 if (type == -1 || type == NETCONFA_FORWARDING)
450 size += nla_total_size(4);
451 #ifdef CONFIG_IPV6_MROUTE
452 if (type == -1 || type == NETCONFA_MC_FORWARDING)
453 size += nla_total_size(4);
455 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
456 size += nla_total_size(4);
461 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
462 struct ipv6_devconf *devconf, u32 portid,
463 u32 seq, int event, unsigned int flags,
466 struct nlmsghdr *nlh;
467 struct netconfmsg *ncm;
469 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
474 ncm = nlmsg_data(nlh);
475 ncm->ncm_family = AF_INET6;
477 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
478 goto nla_put_failure;
480 /* type -1 is used for ALL */
481 if ((type == -1 || type == NETCONFA_FORWARDING) &&
482 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
483 goto nla_put_failure;
484 #ifdef CONFIG_IPV6_MROUTE
485 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
486 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
487 devconf->mc_forwarding) < 0)
488 goto nla_put_failure;
490 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
491 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
492 goto nla_put_failure;
498 nlmsg_cancel(skb, nlh);
502 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
503 struct ipv6_devconf *devconf)
508 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
512 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
513 RTM_NEWNETCONF, 0, type);
515 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
516 WARN_ON(err == -EMSGSIZE);
520 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
523 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
526 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
527 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
528 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
529 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
532 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
533 struct nlmsghdr *nlh)
535 struct net *net = sock_net(in_skb->sk);
536 struct nlattr *tb[NETCONFA_MAX+1];
537 struct netconfmsg *ncm;
539 struct ipv6_devconf *devconf;
540 struct inet6_dev *in6_dev;
541 struct net_device *dev;
545 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
546 devconf_ipv6_policy);
551 if (!tb[NETCONFA_IFINDEX])
554 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
556 case NETCONFA_IFINDEX_ALL:
557 devconf = net->ipv6.devconf_all;
559 case NETCONFA_IFINDEX_DEFAULT:
560 devconf = net->ipv6.devconf_dflt;
563 dev = __dev_get_by_index(net, ifindex);
566 in6_dev = __in6_dev_get(dev);
569 devconf = &in6_dev->cnf;
574 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
578 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
579 NETLINK_CB(in_skb).portid,
580 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
583 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
584 WARN_ON(err == -EMSGSIZE);
588 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
593 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
594 struct netlink_callback *cb)
596 struct net *net = sock_net(skb->sk);
599 struct net_device *dev;
600 struct inet6_dev *idev;
601 struct hlist_head *head;
604 s_idx = idx = cb->args[1];
606 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
608 head = &net->dev_index_head[h];
610 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
612 hlist_for_each_entry_rcu(dev, head, index_hlist) {
615 idev = __in6_dev_get(dev);
619 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
621 NETLINK_CB(cb->skb).portid,
629 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
635 if (h == NETDEV_HASHENTRIES) {
636 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
637 net->ipv6.devconf_all,
638 NETLINK_CB(cb->skb).portid,
640 RTM_NEWNETCONF, NLM_F_MULTI,
646 if (h == NETDEV_HASHENTRIES + 1) {
647 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
648 net->ipv6.devconf_dflt,
649 NETLINK_CB(cb->skb).portid,
651 RTM_NEWNETCONF, NLM_F_MULTI,
665 static void dev_forward_change(struct inet6_dev *idev)
667 struct net_device *dev;
668 struct inet6_ifaddr *ifa;
673 if (idev->cnf.forwarding)
674 dev_disable_lro(dev);
675 if (dev->flags & IFF_MULTICAST) {
676 if (idev->cnf.forwarding) {
677 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
678 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
679 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
681 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
682 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
683 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
687 list_for_each_entry(ifa, &idev->addr_list, if_list) {
688 if (ifa->flags&IFA_F_TENTATIVE)
690 if (idev->cnf.forwarding)
691 addrconf_join_anycast(ifa);
693 addrconf_leave_anycast(ifa);
695 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
696 dev->ifindex, &idev->cnf);
700 static void addrconf_forward_change(struct net *net, __s32 newf)
702 struct net_device *dev;
703 struct inet6_dev *idev;
705 for_each_netdev(net, dev) {
706 idev = __in6_dev_get(dev);
708 int changed = (!idev->cnf.forwarding) ^ (!newf);
709 idev->cnf.forwarding = newf;
711 dev_forward_change(idev);
716 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
722 return restart_syscall();
724 net = (struct net *)table->extra2;
728 if (p == &net->ipv6.devconf_dflt->forwarding) {
729 if ((!newf) ^ (!old))
730 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
731 NETCONFA_IFINDEX_DEFAULT,
732 net->ipv6.devconf_dflt);
737 if (p == &net->ipv6.devconf_all->forwarding) {
738 net->ipv6.devconf_dflt->forwarding = newf;
739 addrconf_forward_change(net, newf);
740 if ((!newf) ^ (!old))
741 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
742 NETCONFA_IFINDEX_ALL,
743 net->ipv6.devconf_all);
744 } else if ((!newf) ^ (!old))
745 dev_forward_change((struct inet6_dev *)table->extra1);
749 rt6_purge_dflt_routers(net);
754 /* Nobody refers to this ifaddr, destroy it */
755 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
757 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
759 #ifdef NET_REFCNT_DEBUG
760 pr_debug("%s\n", __func__);
763 in6_dev_put(ifp->idev);
765 if (cancel_delayed_work(&ifp->dad_work))
766 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
769 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
770 pr_warn("Freeing alive inet6 address %p\n", ifp);
779 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
782 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
785 * Each device address list is sorted in order of scope -
786 * global before linklocal.
788 list_for_each(p, &idev->addr_list) {
789 struct inet6_ifaddr *ifa
790 = list_entry(p, struct inet6_ifaddr, if_list);
791 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
795 list_add_tail(&ifp->if_list, p);
798 static u32 inet6_addr_hash(const struct in6_addr *addr)
800 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
803 /* On success it returns ifp with increased reference count */
805 static struct inet6_ifaddr *
806 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
807 const struct in6_addr *peer_addr, int pfxlen,
808 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
810 struct inet6_ifaddr *ifa = NULL;
814 int addr_type = ipv6_addr_type(addr);
816 if (addr_type == IPV6_ADDR_ANY ||
817 addr_type & IPV6_ADDR_MULTICAST ||
818 (!(idev->dev->flags & IFF_LOOPBACK) &&
819 addr_type & IPV6_ADDR_LOOPBACK))
820 return ERR_PTR(-EADDRNOTAVAIL);
824 err = -ENODEV; /*XXX*/
828 if (idev->cnf.disable_ipv6) {
833 spin_lock(&addrconf_hash_lock);
835 /* Ignore adding duplicate addresses on an interface */
836 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
837 ADBG("ipv6_add_addr: already assigned\n");
842 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
845 ADBG("ipv6_add_addr: malloc failed\n");
850 rt = addrconf_dst_alloc(idev, addr, false);
856 neigh_parms_data_state_setall(idev->nd_parms);
860 ifa->peer_addr = *peer_addr;
862 spin_lock_init(&ifa->lock);
863 spin_lock_init(&ifa->state_lock);
864 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
865 INIT_HLIST_NODE(&ifa->addr_lst);
867 ifa->prefix_len = pfxlen;
868 ifa->flags = flags | IFA_F_TENTATIVE;
869 ifa->valid_lft = valid_lft;
870 ifa->prefered_lft = prefered_lft;
871 ifa->cstamp = ifa->tstamp = jiffies;
872 ifa->tokenized = false;
881 /* Add to big hash table */
882 hash = inet6_addr_hash(addr);
884 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
885 spin_unlock(&addrconf_hash_lock);
887 write_lock(&idev->lock);
888 /* Add to inet6_dev unicast addr list. */
889 ipv6_link_dev_addr(idev, ifa);
891 if (ifa->flags&IFA_F_TEMPORARY) {
892 list_add(&ifa->tmp_list, &idev->tempaddr_list);
897 write_unlock(&idev->lock);
899 rcu_read_unlock_bh();
901 if (likely(err == 0))
902 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
910 spin_unlock(&addrconf_hash_lock);
914 enum cleanup_prefix_rt_t {
915 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
916 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
917 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
921 * Check, whether the prefix for ifp would still need a prefix route
922 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
925 * 1) we don't purge prefix if address was not permanent.
926 * prefix is managed by its own lifetime.
927 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
928 * 3) if there are no addresses, delete prefix.
929 * 4) if there are still other permanent address(es),
930 * corresponding prefix is still permanent.
931 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
932 * don't purge the prefix, assume user space is managing it.
933 * 6) otherwise, update prefix lifetime to the
934 * longest valid lifetime among the corresponding
935 * addresses on the device.
936 * Note: subsequent RA will update lifetime.
938 static enum cleanup_prefix_rt_t
939 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
941 struct inet6_ifaddr *ifa;
942 struct inet6_dev *idev = ifp->idev;
943 unsigned long lifetime;
944 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
948 list_for_each_entry(ifa, &idev->addr_list, if_list) {
951 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
954 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
955 return CLEANUP_PREFIX_RT_NOP;
957 action = CLEANUP_PREFIX_RT_EXPIRE;
959 spin_lock(&ifa->lock);
961 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
963 * Note: Because this address is
964 * not permanent, lifetime <
965 * LONG_MAX / HZ here.
967 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
968 *expires = ifa->tstamp + lifetime * HZ;
969 spin_unlock(&ifa->lock);
976 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
980 rt = addrconf_get_prefix_route(&ifp->addr,
983 0, RTF_GATEWAY | RTF_DEFAULT);
988 if (!(rt->rt6i_flags & RTF_EXPIRES))
989 rt6_set_expires(rt, expires);
996 /* This function wants to get referenced ifp and releases it before return */
998 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1001 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1002 unsigned long expires;
1006 spin_lock_bh(&ifp->state_lock);
1008 ifp->state = INET6_IFADDR_STATE_DEAD;
1009 spin_unlock_bh(&ifp->state_lock);
1011 if (state == INET6_IFADDR_STATE_DEAD)
1014 spin_lock_bh(&addrconf_hash_lock);
1015 hlist_del_init_rcu(&ifp->addr_lst);
1016 spin_unlock_bh(&addrconf_hash_lock);
1018 write_lock_bh(&ifp->idev->lock);
1020 if (ifp->flags&IFA_F_TEMPORARY) {
1021 list_del(&ifp->tmp_list);
1023 in6_ifa_put(ifp->ifpub);
1029 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1030 action = check_cleanup_prefix_route(ifp, &expires);
1032 list_del_init(&ifp->if_list);
1035 write_unlock_bh(&ifp->idev->lock);
1037 addrconf_del_dad_work(ifp);
1039 ipv6_ifa_notify(RTM_DELADDR, ifp);
1041 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1043 if (action != CLEANUP_PREFIX_RT_NOP) {
1044 cleanup_prefix_route(ifp, expires,
1045 action == CLEANUP_PREFIX_RT_DEL);
1048 /* clean up prefsrc entries */
1049 rt6_remove_prefsrc(ifp);
1054 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1056 struct inet6_dev *idev = ifp->idev;
1057 struct in6_addr addr, *tmpaddr;
1058 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1059 unsigned long regen_advance;
1063 unsigned long now = jiffies;
1065 write_lock_bh(&idev->lock);
1067 spin_lock_bh(&ift->lock);
1068 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1069 spin_unlock_bh(&ift->lock);
1076 if (idev->cnf.use_tempaddr <= 0) {
1077 write_unlock_bh(&idev->lock);
1078 pr_info("%s: use_tempaddr is disabled\n", __func__);
1083 spin_lock_bh(&ifp->lock);
1084 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1085 idev->cnf.use_tempaddr = -1; /*XXX*/
1086 spin_unlock_bh(&ifp->lock);
1087 write_unlock_bh(&idev->lock);
1088 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1095 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1096 __ipv6_try_regen_rndid(idev, tmpaddr);
1097 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1098 age = (now - ifp->tstamp) / HZ;
1099 tmp_valid_lft = min_t(__u32,
1101 idev->cnf.temp_valid_lft + age);
1102 tmp_prefered_lft = min_t(__u32,
1104 idev->cnf.temp_prefered_lft + age -
1105 idev->cnf.max_desync_factor);
1106 tmp_plen = ifp->prefix_len;
1107 tmp_tstamp = ifp->tstamp;
1108 spin_unlock_bh(&ifp->lock);
1110 regen_advance = idev->cnf.regen_max_retry *
1111 idev->cnf.dad_transmits *
1112 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1113 write_unlock_bh(&idev->lock);
1115 /* A temporary address is created only if this calculated Preferred
1116 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1117 * an implementation must not create a temporary address with a zero
1118 * Preferred Lifetime.
1119 * Use age calculation as in addrconf_verify to avoid unnecessary
1120 * temporary addresses being generated.
1122 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1123 if (tmp_prefered_lft <= regen_advance + age) {
1130 addr_flags = IFA_F_TEMPORARY;
1131 /* set in addrconf_prefix_rcv() */
1132 if (ifp->flags & IFA_F_OPTIMISTIC)
1133 addr_flags |= IFA_F_OPTIMISTIC;
1135 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1136 ipv6_addr_scope(&addr), addr_flags,
1137 tmp_valid_lft, tmp_prefered_lft);
1141 pr_info("%s: retry temporary address regeneration\n", __func__);
1143 write_lock_bh(&idev->lock);
1147 spin_lock_bh(&ift->lock);
1150 ift->tstamp = tmp_tstamp;
1151 spin_unlock_bh(&ift->lock);
1153 addrconf_dad_start(ift);
1161 * Choose an appropriate source address (RFC3484)
1164 IPV6_SADDR_RULE_INIT = 0,
1165 IPV6_SADDR_RULE_LOCAL,
1166 IPV6_SADDR_RULE_SCOPE,
1167 IPV6_SADDR_RULE_PREFERRED,
1168 #ifdef CONFIG_IPV6_MIP6
1169 IPV6_SADDR_RULE_HOA,
1171 IPV6_SADDR_RULE_OIF,
1172 IPV6_SADDR_RULE_LABEL,
1173 IPV6_SADDR_RULE_PRIVACY,
1174 IPV6_SADDR_RULE_ORCHID,
1175 IPV6_SADDR_RULE_PREFIX,
1176 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1177 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1182 struct ipv6_saddr_score {
1185 struct inet6_ifaddr *ifa;
1186 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1191 struct ipv6_saddr_dst {
1192 const struct in6_addr *addr;
1199 static inline int ipv6_saddr_preferred(int type)
1201 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1206 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1208 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1209 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1215 static int ipv6_get_saddr_eval(struct net *net,
1216 struct ipv6_saddr_score *score,
1217 struct ipv6_saddr_dst *dst,
1222 if (i <= score->rule) {
1224 case IPV6_SADDR_RULE_SCOPE:
1225 ret = score->scopedist;
1227 case IPV6_SADDR_RULE_PREFIX:
1228 ret = score->matchlen;
1231 ret = !!test_bit(i, score->scorebits);
1237 case IPV6_SADDR_RULE_INIT:
1238 /* Rule 0: remember if hiscore is not ready yet */
1241 case IPV6_SADDR_RULE_LOCAL:
1242 /* Rule 1: Prefer same address */
1243 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1245 case IPV6_SADDR_RULE_SCOPE:
1246 /* Rule 2: Prefer appropriate scope
1251 * ---+--+-+---> scope
1253 * | d is scope of the destination.
1255 * | \ <- smaller scope is better if
1256 * B-15 | \ if scope is enough for destination.
1257 * | ret = B - scope (-1 <= scope >= d <= 15).
1259 * |/ <- greater is better
1260 * -C / if scope is not enough for destination.
1261 * /| ret = scope - C (-1 <= d < scope <= 15).
1263 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1264 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1265 * Assume B = 0 and we get C > 29.
1267 ret = __ipv6_addr_src_scope(score->addr_type);
1268 if (ret >= dst->scope)
1271 ret -= 128; /* 30 is enough */
1272 score->scopedist = ret;
1274 case IPV6_SADDR_RULE_PREFERRED:
1276 /* Rule 3: Avoid deprecated and optimistic addresses */
1277 u8 avoid = IFA_F_DEPRECATED;
1279 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1280 avoid |= IFA_F_OPTIMISTIC;
1281 ret = ipv6_saddr_preferred(score->addr_type) ||
1282 !(score->ifa->flags & avoid);
1285 #ifdef CONFIG_IPV6_MIP6
1286 case IPV6_SADDR_RULE_HOA:
1288 /* Rule 4: Prefer home address */
1289 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1290 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1294 case IPV6_SADDR_RULE_OIF:
1295 /* Rule 5: Prefer outgoing interface */
1296 ret = (!dst->ifindex ||
1297 dst->ifindex == score->ifa->idev->dev->ifindex);
1299 case IPV6_SADDR_RULE_LABEL:
1300 /* Rule 6: Prefer matching label */
1301 ret = ipv6_addr_label(net,
1302 &score->ifa->addr, score->addr_type,
1303 score->ifa->idev->dev->ifindex) == dst->label;
1305 case IPV6_SADDR_RULE_PRIVACY:
1307 /* Rule 7: Prefer public address
1308 * Note: prefer temporary address if use_tempaddr >= 2
1310 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1311 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1312 score->ifa->idev->cnf.use_tempaddr >= 2;
1313 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1316 case IPV6_SADDR_RULE_ORCHID:
1317 /* Rule 8-: Prefer ORCHID vs ORCHID or
1318 * non-ORCHID vs non-ORCHID
1320 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1321 ipv6_addr_orchid(dst->addr));
1323 case IPV6_SADDR_RULE_PREFIX:
1324 /* Rule 8: Use longest matching prefix */
1325 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1326 if (ret > score->ifa->prefix_len)
1327 ret = score->ifa->prefix_len;
1328 score->matchlen = ret;
1330 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1331 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1332 /* Optimistic addresses still have lower precedence than other
1333 * preferred addresses.
1335 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1343 __set_bit(i, score->scorebits);
1349 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1350 const struct in6_addr *daddr, unsigned int prefs,
1351 struct in6_addr *saddr)
1353 struct ipv6_saddr_score scores[2],
1354 *score = &scores[0], *hiscore = &scores[1];
1355 struct ipv6_saddr_dst dst;
1356 struct net_device *dev;
1359 dst_type = __ipv6_addr_type(daddr);
1361 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1362 dst.scope = __ipv6_addr_src_scope(dst_type);
1363 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1367 hiscore->ifa = NULL;
1371 for_each_netdev_rcu(net, dev) {
1372 struct inet6_dev *idev;
1374 /* Candidate Source Address (section 4)
1375 * - multicast and link-local destination address,
1376 * the set of candidate source address MUST only
1377 * include addresses assigned to interfaces
1378 * belonging to the same link as the outgoing
1380 * (- For site-local destination addresses, the
1381 * set of candidate source addresses MUST only
1382 * include addresses assigned to interfaces
1383 * belonging to the same site as the outgoing
1386 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1387 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1388 dst.ifindex && dev->ifindex != dst.ifindex)
1391 idev = __in6_dev_get(dev);
1395 read_lock_bh(&idev->lock);
1396 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1400 * - Tentative Address (RFC2462 section 5.4)
1401 * - A tentative address is not considered
1402 * "assigned to an interface" in the traditional
1403 * sense, unless it is also flagged as optimistic.
1404 * - Candidate Source Address (section 4)
1405 * - In any case, anycast addresses, multicast
1406 * addresses, and the unspecified address MUST
1407 * NOT be included in a candidate set.
1409 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1410 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1413 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1415 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1416 score->addr_type & IPV6_ADDR_MULTICAST)) {
1417 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1423 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1425 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1426 int minihiscore, miniscore;
1428 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1429 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1431 if (minihiscore > miniscore) {
1432 if (i == IPV6_SADDR_RULE_SCOPE &&
1433 score->scopedist > 0) {
1436 * each remaining entry
1437 * has too small (not enough)
1438 * scope, because ifa entries
1439 * are sorted by their scope
1445 } else if (minihiscore < miniscore) {
1447 in6_ifa_put(hiscore->ifa);
1449 in6_ifa_hold(score->ifa);
1451 swap(hiscore, score);
1453 /* restore our iterator */
1454 score->ifa = hiscore->ifa;
1461 read_unlock_bh(&idev->lock);
1466 return -EADDRNOTAVAIL;
1468 *saddr = hiscore->ifa->addr;
1469 in6_ifa_put(hiscore->ifa);
1472 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1474 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1477 struct inet6_ifaddr *ifp;
1478 int err = -EADDRNOTAVAIL;
1480 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1481 if (ifp->scope > IFA_LINK)
1483 if (ifp->scope == IFA_LINK &&
1484 !(ifp->flags & banned_flags)) {
1493 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1496 struct inet6_dev *idev;
1497 int err = -EADDRNOTAVAIL;
1500 idev = __in6_dev_get(dev);
1502 read_lock_bh(&idev->lock);
1503 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1504 read_unlock_bh(&idev->lock);
1510 static int ipv6_count_addresses(struct inet6_dev *idev)
1513 struct inet6_ifaddr *ifp;
1515 read_lock_bh(&idev->lock);
1516 list_for_each_entry(ifp, &idev->addr_list, if_list)
1518 read_unlock_bh(&idev->lock);
1522 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1523 const struct net_device *dev, int strict)
1525 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1527 EXPORT_SYMBOL(ipv6_chk_addr);
1529 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1530 const struct net_device *dev, int strict,
1533 struct inet6_ifaddr *ifp;
1534 unsigned int hash = inet6_addr_hash(addr);
1538 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1539 if (!net_eq(dev_net(ifp->idev->dev), net))
1541 /* Decouple optimistic from tentative for evaluation here.
1542 * Ban optimistic addresses explicitly, when required.
1544 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1545 ? (ifp->flags&~IFA_F_TENTATIVE)
1547 if (ipv6_addr_equal(&ifp->addr, addr) &&
1548 !(ifp_flags&banned_flags) &&
1549 (dev == NULL || ifp->idev->dev == dev ||
1550 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1551 rcu_read_unlock_bh();
1556 rcu_read_unlock_bh();
1559 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1561 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1562 struct net_device *dev)
1564 unsigned int hash = inet6_addr_hash(addr);
1565 struct inet6_ifaddr *ifp;
1567 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1568 if (!net_eq(dev_net(ifp->idev->dev), net))
1570 if (ipv6_addr_equal(&ifp->addr, addr)) {
1571 if (dev == NULL || ifp->idev->dev == dev)
1578 /* Compares an address/prefix_len with addresses on device @dev.
1579 * If one is found it returns true.
1581 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1582 const unsigned int prefix_len, struct net_device *dev)
1584 struct inet6_dev *idev;
1585 struct inet6_ifaddr *ifa;
1589 idev = __in6_dev_get(dev);
1591 read_lock_bh(&idev->lock);
1592 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1593 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1597 read_unlock_bh(&idev->lock);
1603 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1605 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1607 struct inet6_dev *idev;
1608 struct inet6_ifaddr *ifa;
1613 idev = __in6_dev_get(dev);
1615 read_lock_bh(&idev->lock);
1616 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1617 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1622 read_unlock_bh(&idev->lock);
1627 EXPORT_SYMBOL(ipv6_chk_prefix);
1629 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1630 struct net_device *dev, int strict)
1632 struct inet6_ifaddr *ifp, *result = NULL;
1633 unsigned int hash = inet6_addr_hash(addr);
1636 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1637 if (!net_eq(dev_net(ifp->idev->dev), net))
1639 if (ipv6_addr_equal(&ifp->addr, addr)) {
1640 if (dev == NULL || ifp->idev->dev == dev ||
1641 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1648 rcu_read_unlock_bh();
1653 /* Gets referenced address, destroys ifaddr */
1655 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1657 if (ifp->flags&IFA_F_PERMANENT) {
1658 spin_lock_bh(&ifp->lock);
1659 addrconf_del_dad_work(ifp);
1660 ifp->flags |= IFA_F_TENTATIVE;
1662 ifp->flags |= IFA_F_DADFAILED;
1663 spin_unlock_bh(&ifp->lock);
1665 ipv6_ifa_notify(0, ifp);
1667 } else if (ifp->flags&IFA_F_TEMPORARY) {
1668 struct inet6_ifaddr *ifpub;
1669 spin_lock_bh(&ifp->lock);
1672 in6_ifa_hold(ifpub);
1673 spin_unlock_bh(&ifp->lock);
1674 ipv6_create_tempaddr(ifpub, ifp);
1677 spin_unlock_bh(&ifp->lock);
1685 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1689 spin_lock_bh(&ifp->state_lock);
1690 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1691 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1694 spin_unlock_bh(&ifp->state_lock);
1699 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1701 struct inet6_dev *idev = ifp->idev;
1703 if (addrconf_dad_end(ifp)) {
1708 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1709 ifp->idev->dev->name, &ifp->addr);
1711 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1712 struct in6_addr addr;
1714 addr.s6_addr32[0] = htonl(0xfe800000);
1715 addr.s6_addr32[1] = 0;
1717 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1718 ipv6_addr_equal(&ifp->addr, &addr)) {
1719 /* DAD failed for link-local based on MAC address */
1720 idev->cnf.disable_ipv6 = 1;
1722 pr_info("%s: IPv6 being disabled!\n",
1723 ifp->idev->dev->name);
1727 spin_lock_bh(&ifp->state_lock);
1728 /* transition from _POSTDAD to _ERRDAD */
1729 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1730 spin_unlock_bh(&ifp->state_lock);
1732 addrconf_mod_dad_work(ifp, 0);
1735 /* Join to solicited addr multicast group.
1736 * caller must hold RTNL */
1737 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1739 struct in6_addr maddr;
1741 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1744 addrconf_addr_solict_mult(addr, &maddr);
1745 ipv6_dev_mc_inc(dev, &maddr);
1748 /* caller must hold RTNL */
1749 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1751 struct in6_addr maddr;
1753 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1756 addrconf_addr_solict_mult(addr, &maddr);
1757 __ipv6_dev_mc_dec(idev, &maddr);
1760 /* caller must hold RTNL */
1761 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1763 struct in6_addr addr;
1765 if (ifp->prefix_len >= 127) /* RFC 6164 */
1767 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1768 if (ipv6_addr_any(&addr))
1770 __ipv6_dev_ac_inc(ifp->idev, &addr);
1773 /* caller must hold RTNL */
1774 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1776 struct in6_addr addr;
1778 if (ifp->prefix_len >= 127) /* RFC 6164 */
1780 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1781 if (ipv6_addr_any(&addr))
1783 __ipv6_dev_ac_dec(ifp->idev, &addr);
1786 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1788 if (dev->addr_len != ETH_ALEN)
1790 memcpy(eui, dev->dev_addr, 3);
1791 memcpy(eui + 5, dev->dev_addr + 3, 3);
1794 * The zSeries OSA network cards can be shared among various
1795 * OS instances, but the OSA cards have only one MAC address.
1796 * This leads to duplicate address conflicts in conjunction
1797 * with IPv6 if more than one instance uses the same card.
1799 * The driver for these cards can deliver a unique 16-bit
1800 * identifier for each instance sharing the same card. It is
1801 * placed instead of 0xFFFE in the interface identifier. The
1802 * "u" bit of the interface identifier is not inverted in this
1803 * case. Hence the resulting interface identifier has local
1804 * scope according to RFC2373.
1807 eui[3] = (dev->dev_id >> 8) & 0xFF;
1808 eui[4] = dev->dev_id & 0xFF;
1817 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1819 if (dev->addr_len != IEEE802154_ADDR_LEN)
1821 memcpy(eui, dev->dev_addr, 8);
1826 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1828 union fwnet_hwaddr *ha;
1830 if (dev->addr_len != FWNET_ALEN)
1833 ha = (union fwnet_hwaddr *)dev->dev_addr;
1835 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1840 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1842 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1843 if (dev->addr_len != ARCNET_ALEN)
1846 eui[7] = *(u8 *)dev->dev_addr;
1850 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1852 if (dev->addr_len != INFINIBAND_ALEN)
1854 memcpy(eui, dev->dev_addr + 12, 8);
1859 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1863 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1864 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1865 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1866 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1867 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1868 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1872 memcpy(eui + 4, &addr, 4);
1876 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1878 if (dev->priv_flags & IFF_ISATAP)
1879 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1883 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1885 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1888 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1890 memcpy(eui, dev->perm_addr, 3);
1891 memcpy(eui + 5, dev->perm_addr + 3, 3);
1898 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1900 switch (dev->type) {
1903 return addrconf_ifid_eui48(eui, dev);
1905 return addrconf_ifid_arcnet(eui, dev);
1906 case ARPHRD_INFINIBAND:
1907 return addrconf_ifid_infiniband(eui, dev);
1909 return addrconf_ifid_sit(eui, dev);
1911 return addrconf_ifid_gre(eui, dev);
1912 case ARPHRD_6LOWPAN:
1913 case ARPHRD_IEEE802154:
1914 return addrconf_ifid_eui64(eui, dev);
1915 case ARPHRD_IEEE1394:
1916 return addrconf_ifid_ieee1394(eui, dev);
1917 case ARPHRD_TUNNEL6:
1918 return addrconf_ifid_ip6tnl(eui, dev);
1923 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1926 struct inet6_ifaddr *ifp;
1928 read_lock_bh(&idev->lock);
1929 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1930 if (ifp->scope > IFA_LINK)
1932 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1933 memcpy(eui, ifp->addr.s6_addr+8, 8);
1938 read_unlock_bh(&idev->lock);
1942 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1943 static void __ipv6_regen_rndid(struct inet6_dev *idev)
1946 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1947 idev->rndid[0] &= ~0x02;
1950 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1951 * check if generated address is not inappropriate
1953 * - Reserved subnet anycast (RFC 2526)
1954 * 11111101 11....11 1xxxxxxx
1955 * - ISATAP (RFC4214) 6.1
1956 * 00-00-5E-FE-xx-xx-xx-xx
1958 * - XXX: already assigned to an address on the device
1960 if (idev->rndid[0] == 0xfd &&
1961 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1962 (idev->rndid[7]&0x80))
1964 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1965 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1967 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1972 static void ipv6_regen_rndid(unsigned long data)
1974 struct inet6_dev *idev = (struct inet6_dev *) data;
1975 unsigned long expires;
1978 write_lock_bh(&idev->lock);
1983 __ipv6_regen_rndid(idev);
1986 idev->cnf.temp_prefered_lft * HZ -
1987 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
1988 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
1989 idev->cnf.max_desync_factor * HZ;
1990 if (time_before(expires, jiffies)) {
1991 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1992 __func__, idev->dev->name);
1996 if (!mod_timer(&idev->regen_timer, expires))
2000 write_unlock_bh(&idev->lock);
2001 rcu_read_unlock_bh();
2005 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2007 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2008 __ipv6_regen_rndid(idev);
2016 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2017 unsigned long expires, u32 flags)
2019 struct fib6_config cfg = {
2020 .fc_table = RT6_TABLE_PREFIX,
2021 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2022 .fc_ifindex = dev->ifindex,
2023 .fc_expires = expires,
2025 .fc_flags = RTF_UP | flags,
2026 .fc_nlinfo.nl_net = dev_net(dev),
2027 .fc_protocol = RTPROT_KERNEL,
2032 /* Prevent useless cloning on PtP SIT.
2033 This thing is done here expecting that the whole
2034 class of non-broadcast devices need not cloning.
2036 #if IS_ENABLED(CONFIG_IPV6_SIT)
2037 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2038 cfg.fc_flags |= RTF_NONEXTHOP;
2041 ip6_route_add(&cfg);
2045 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2047 const struct net_device *dev,
2048 u32 flags, u32 noflags)
2050 struct fib6_node *fn;
2051 struct rt6_info *rt = NULL;
2052 struct fib6_table *table;
2054 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2058 read_lock_bh(&table->tb6_lock);
2059 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2062 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2063 if (rt->dst.dev->ifindex != dev->ifindex)
2065 if ((rt->rt6i_flags & flags) != flags)
2067 if ((rt->rt6i_flags & noflags) != 0)
2073 read_unlock_bh(&table->tb6_lock);
2078 /* Create "default" multicast route to the interface */
2080 static void addrconf_add_mroute(struct net_device *dev)
2082 struct fib6_config cfg = {
2083 .fc_table = RT6_TABLE_LOCAL,
2084 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2085 .fc_ifindex = dev->ifindex,
2088 .fc_nlinfo.nl_net = dev_net(dev),
2091 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2093 ip6_route_add(&cfg);
2096 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2098 struct inet6_dev *idev;
2102 idev = ipv6_find_idev(dev);
2104 return ERR_PTR(-ENOBUFS);
2106 if (idev->cnf.disable_ipv6)
2107 return ERR_PTR(-EACCES);
2109 /* Add default multicast route */
2110 if (!(dev->flags & IFF_LOOPBACK))
2111 addrconf_add_mroute(dev);
2116 static void manage_tempaddrs(struct inet6_dev *idev,
2117 struct inet6_ifaddr *ifp,
2118 __u32 valid_lft, __u32 prefered_lft,
2119 bool create, unsigned long now)
2122 struct inet6_ifaddr *ift;
2124 read_lock_bh(&idev->lock);
2125 /* update all temporary addresses in the list */
2126 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2127 int age, max_valid, max_prefered;
2129 if (ifp != ift->ifpub)
2132 /* RFC 4941 section 3.3:
2133 * If a received option will extend the lifetime of a public
2134 * address, the lifetimes of temporary addresses should
2135 * be extended, subject to the overall constraint that no
2136 * temporary addresses should ever remain "valid" or "preferred"
2137 * for a time longer than (TEMP_VALID_LIFETIME) or
2138 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2140 age = (now - ift->cstamp) / HZ;
2141 max_valid = idev->cnf.temp_valid_lft - age;
2145 max_prefered = idev->cnf.temp_prefered_lft -
2146 idev->cnf.max_desync_factor - age;
2147 if (max_prefered < 0)
2150 if (valid_lft > max_valid)
2151 valid_lft = max_valid;
2153 if (prefered_lft > max_prefered)
2154 prefered_lft = max_prefered;
2156 spin_lock(&ift->lock);
2158 ift->valid_lft = valid_lft;
2159 ift->prefered_lft = prefered_lft;
2161 if (prefered_lft > 0)
2162 ift->flags &= ~IFA_F_DEPRECATED;
2164 spin_unlock(&ift->lock);
2165 if (!(flags&IFA_F_TENTATIVE))
2166 ipv6_ifa_notify(0, ift);
2169 if ((create || list_empty(&idev->tempaddr_list)) &&
2170 idev->cnf.use_tempaddr > 0) {
2171 /* When a new public address is created as described
2172 * in [ADDRCONF], also create a new temporary address.
2173 * Also create a temporary address if it's enabled but
2174 * no temporary address currently exists.
2176 read_unlock_bh(&idev->lock);
2177 ipv6_create_tempaddr(ifp, NULL);
2179 read_unlock_bh(&idev->lock);
2183 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2185 struct prefix_info *pinfo;
2189 struct inet6_dev *in6_dev;
2190 struct net *net = dev_net(dev);
2192 pinfo = (struct prefix_info *) opt;
2194 if (len < sizeof(struct prefix_info)) {
2195 ADBG("addrconf: prefix option too short\n");
2200 * Validation checks ([ADDRCONF], page 19)
2203 addr_type = ipv6_addr_type(&pinfo->prefix);
2205 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2208 valid_lft = ntohl(pinfo->valid);
2209 prefered_lft = ntohl(pinfo->prefered);
2211 if (prefered_lft > valid_lft) {
2212 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2216 in6_dev = in6_dev_get(dev);
2218 if (in6_dev == NULL) {
2219 net_dbg_ratelimited("addrconf: device %s not configured\n",
2225 * Two things going on here:
2226 * 1) Add routes for on-link prefixes
2227 * 2) Configure prefixes with the auto flag set
2230 if (pinfo->onlink) {
2231 struct rt6_info *rt;
2232 unsigned long rt_expires;
2234 /* Avoid arithmetic overflow. Really, we could
2235 * save rt_expires in seconds, likely valid_lft,
2236 * but it would require division in fib gc, that it
2240 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2242 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2244 if (addrconf_finite_timeout(rt_expires))
2247 rt = addrconf_get_prefix_route(&pinfo->prefix,
2250 RTF_ADDRCONF | RTF_PREFIX_RT,
2251 RTF_GATEWAY | RTF_DEFAULT);
2254 /* Autoconf prefix route */
2255 if (valid_lft == 0) {
2258 } else if (addrconf_finite_timeout(rt_expires)) {
2260 rt6_set_expires(rt, jiffies + rt_expires);
2262 rt6_clean_expires(rt);
2264 } else if (valid_lft) {
2265 clock_t expires = 0;
2266 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2267 if (addrconf_finite_timeout(rt_expires)) {
2269 flags |= RTF_EXPIRES;
2270 expires = jiffies_to_clock_t(rt_expires);
2272 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2273 dev, expires, flags);
2278 /* Try to figure out our local address for this prefix */
2280 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2281 struct inet6_ifaddr *ifp;
2282 struct in6_addr addr;
2283 int create = 0, update_lft = 0;
2284 bool tokenized = false;
2286 if (pinfo->prefix_len == 64) {
2287 memcpy(&addr, &pinfo->prefix, 8);
2289 if (!ipv6_addr_any(&in6_dev->token)) {
2290 read_lock_bh(&in6_dev->lock);
2291 memcpy(addr.s6_addr + 8,
2292 in6_dev->token.s6_addr + 8, 8);
2293 read_unlock_bh(&in6_dev->lock);
2295 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2296 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2297 in6_dev_put(in6_dev);
2302 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2304 in6_dev_put(in6_dev);
2309 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2311 if (ifp == NULL && valid_lft) {
2312 int max_addresses = in6_dev->cnf.max_addresses;
2315 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2316 if (in6_dev->cnf.optimistic_dad &&
2317 !net->ipv6.devconf_all->forwarding && sllao)
2318 addr_flags = IFA_F_OPTIMISTIC;
2321 /* Do not allow to create too much of autoconfigured
2322 * addresses; this would be too easy way to crash kernel.
2324 if (!max_addresses ||
2325 ipv6_count_addresses(in6_dev) < max_addresses)
2326 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2328 addr_type&IPV6_ADDR_SCOPE_MASK,
2329 addr_flags, valid_lft,
2332 if (IS_ERR_OR_NULL(ifp)) {
2333 in6_dev_put(in6_dev);
2339 spin_lock_bh(&ifp->lock);
2340 ifp->flags |= IFA_F_MANAGETEMPADDR;
2341 ifp->cstamp = jiffies;
2342 ifp->tokenized = tokenized;
2343 spin_unlock_bh(&ifp->lock);
2344 addrconf_dad_start(ifp);
2352 /* update lifetime (RFC2462 5.5.3 e) */
2353 spin_lock(&ifp->lock);
2355 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2356 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2359 if (!update_lft && !create && stored_lft) {
2360 const u32 minimum_lft = min_t(u32,
2361 stored_lft, MIN_VALID_LIFETIME);
2362 valid_lft = max(valid_lft, minimum_lft);
2364 /* RFC4862 Section 5.5.3e:
2365 * "Note that the preferred lifetime of the
2366 * corresponding address is always reset to
2367 * the Preferred Lifetime in the received
2368 * Prefix Information option, regardless of
2369 * whether the valid lifetime is also reset or
2372 * So we should always update prefered_lft here.
2378 ifp->valid_lft = valid_lft;
2379 ifp->prefered_lft = prefered_lft;
2382 ifp->flags &= ~IFA_F_DEPRECATED;
2383 spin_unlock(&ifp->lock);
2385 if (!(flags&IFA_F_TENTATIVE))
2386 ipv6_ifa_notify(0, ifp);
2388 spin_unlock(&ifp->lock);
2390 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2397 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2398 in6_dev_put(in6_dev);
2402 * Set destination address.
2403 * Special case for SIT interfaces where we create a new "virtual"
2406 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2408 struct in6_ifreq ireq;
2409 struct net_device *dev;
2415 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2418 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2424 #if IS_ENABLED(CONFIG_IPV6_SIT)
2425 if (dev->type == ARPHRD_SIT) {
2426 const struct net_device_ops *ops = dev->netdev_ops;
2428 struct ip_tunnel_parm p;
2430 err = -EADDRNOTAVAIL;
2431 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2434 memset(&p, 0, sizeof(p));
2435 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2439 p.iph.protocol = IPPROTO_IPV6;
2441 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2443 if (ops->ndo_do_ioctl) {
2444 mm_segment_t oldfs = get_fs();
2447 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2454 dev = __dev_get_by_name(net, p.name);
2457 err = dev_open(dev);
2467 static int ipv6_mc_config(struct sock *sk, bool join,
2468 const struct in6_addr *addr, int ifindex)
2476 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2478 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2485 * Manual configuration of address on an interface
2487 static int inet6_addr_add(struct net *net, int ifindex,
2488 const struct in6_addr *pfx,
2489 const struct in6_addr *peer_pfx,
2490 unsigned int plen, __u32 ifa_flags,
2491 __u32 prefered_lft, __u32 valid_lft)
2493 struct inet6_ifaddr *ifp;
2494 struct inet6_dev *idev;
2495 struct net_device *dev;
2496 unsigned long timeout;
2506 /* check the lifetime */
2507 if (!valid_lft || prefered_lft > valid_lft)
2510 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2513 dev = __dev_get_by_index(net, ifindex);
2517 idev = addrconf_add_dev(dev);
2519 return PTR_ERR(idev);
2521 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2522 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2523 true, pfx, ifindex);
2529 scope = ipv6_addr_scope(pfx);
2531 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2532 if (addrconf_finite_timeout(timeout)) {
2533 expires = jiffies_to_clock_t(timeout * HZ);
2534 valid_lft = timeout;
2535 flags = RTF_EXPIRES;
2539 ifa_flags |= IFA_F_PERMANENT;
2542 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2543 if (addrconf_finite_timeout(timeout)) {
2545 ifa_flags |= IFA_F_DEPRECATED;
2546 prefered_lft = timeout;
2549 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2550 valid_lft, prefered_lft);
2553 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2554 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2559 * Note that section 3.1 of RFC 4429 indicates
2560 * that the Optimistic flag should not be set for
2561 * manually configured addresses
2563 addrconf_dad_start(ifp);
2564 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2565 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2568 addrconf_verify_rtnl();
2570 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2571 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2572 false, pfx, ifindex);
2575 return PTR_ERR(ifp);
2578 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2579 const struct in6_addr *pfx, unsigned int plen)
2581 struct inet6_ifaddr *ifp;
2582 struct inet6_dev *idev;
2583 struct net_device *dev;
2588 dev = __dev_get_by_index(net, ifindex);
2592 idev = __in6_dev_get(dev);
2596 read_lock_bh(&idev->lock);
2597 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2598 if (ifp->prefix_len == plen &&
2599 ipv6_addr_equal(pfx, &ifp->addr)) {
2601 read_unlock_bh(&idev->lock);
2603 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2604 (ifa_flags & IFA_F_MANAGETEMPADDR))
2605 manage_tempaddrs(idev, ifp, 0, 0, false,
2608 addrconf_verify_rtnl();
2609 if (ipv6_addr_is_multicast(pfx)) {
2610 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2611 false, pfx, dev->ifindex);
2616 read_unlock_bh(&idev->lock);
2617 return -EADDRNOTAVAIL;
2621 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2623 struct in6_ifreq ireq;
2626 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2629 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2633 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2634 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2635 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2640 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2642 struct in6_ifreq ireq;
2645 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2648 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2652 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2653 ireq.ifr6_prefixlen);
2658 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2659 int plen, int scope)
2661 struct inet6_ifaddr *ifp;
2663 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2664 scope, IFA_F_PERMANENT,
2665 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2667 spin_lock_bh(&ifp->lock);
2668 ifp->flags &= ~IFA_F_TENTATIVE;
2669 spin_unlock_bh(&ifp->lock);
2670 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2675 #if IS_ENABLED(CONFIG_IPV6_SIT)
2676 static void sit_add_v4_addrs(struct inet6_dev *idev)
2678 struct in6_addr addr;
2679 struct net_device *dev;
2680 struct net *net = dev_net(idev->dev);
2686 memset(&addr, 0, sizeof(struct in6_addr));
2687 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2689 if (idev->dev->flags&IFF_POINTOPOINT) {
2690 addr.s6_addr32[0] = htonl(0xfe800000);
2694 scope = IPV6_ADDR_COMPATv4;
2696 pflags |= RTF_NONEXTHOP;
2699 if (addr.s6_addr32[3]) {
2700 add_addr(idev, &addr, plen, scope);
2701 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2705 for_each_netdev(net, dev) {
2706 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2707 if (in_dev && (dev->flags & IFF_UP)) {
2708 struct in_ifaddr *ifa;
2712 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2714 addr.s6_addr32[3] = ifa->ifa_local;
2716 if (ifa->ifa_scope == RT_SCOPE_LINK)
2718 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2719 if (idev->dev->flags&IFF_POINTOPOINT)
2724 add_addr(idev, &addr, plen, flag);
2725 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2733 static void init_loopback(struct net_device *dev)
2735 struct inet6_dev *idev;
2736 struct net_device *sp_dev;
2737 struct inet6_ifaddr *sp_ifa;
2738 struct rt6_info *sp_rt;
2744 idev = ipv6_find_idev(dev);
2746 pr_debug("%s: add_dev failed\n", __func__);
2750 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2752 /* Add routes to other interface's IPv6 addresses */
2753 for_each_netdev(dev_net(dev), sp_dev) {
2754 if (!strcmp(sp_dev->name, dev->name))
2757 idev = __in6_dev_get(sp_dev);
2761 read_lock_bh(&idev->lock);
2762 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2764 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2768 /* This dst has been added to garbage list when
2769 * lo device down, release this obsolete dst and
2770 * reallocate a new router for ifa.
2772 if (sp_ifa->rt->dst.obsolete > 0) {
2773 ip6_rt_put(sp_ifa->rt);
2780 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
2782 /* Failure cases are ignored */
2783 if (!IS_ERR(sp_rt)) {
2788 read_unlock_bh(&idev->lock);
2792 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2794 struct inet6_ifaddr *ifp;
2795 u32 addr_flags = IFA_F_PERMANENT;
2797 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2798 if (idev->cnf.optimistic_dad &&
2799 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2800 addr_flags |= IFA_F_OPTIMISTIC;
2804 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2805 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2807 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2808 addrconf_dad_start(ifp);
2813 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2815 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
2816 struct in6_addr addr;
2818 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2819 /* addrconf_add_linklocal also adds a prefix_route and we
2820 * only need to care about prefix routes if ipv6_generate_eui64
2821 * couldn't generate one.
2823 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
2824 addrconf_add_linklocal(idev, &addr);
2825 else if (prefix_route)
2826 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2830 static void addrconf_dev_config(struct net_device *dev)
2832 struct inet6_dev *idev;
2836 if ((dev->type != ARPHRD_ETHER) &&
2837 (dev->type != ARPHRD_FDDI) &&
2838 (dev->type != ARPHRD_ARCNET) &&
2839 (dev->type != ARPHRD_INFINIBAND) &&
2840 (dev->type != ARPHRD_IEEE802154) &&
2841 (dev->type != ARPHRD_IEEE1394) &&
2842 (dev->type != ARPHRD_TUNNEL6) &&
2843 (dev->type != ARPHRD_6LOWPAN)) {
2844 /* Alas, we support only Ethernet autoconfiguration. */
2848 idev = addrconf_add_dev(dev);
2852 addrconf_addr_gen(idev, false);
2855 #if IS_ENABLED(CONFIG_IPV6_SIT)
2856 static void addrconf_sit_config(struct net_device *dev)
2858 struct inet6_dev *idev;
2863 * Configure the tunnel with one of our IPv4
2864 * addresses... we should configure all of
2865 * our v4 addrs in the tunnel
2868 idev = ipv6_find_idev(dev);
2870 pr_debug("%s: add_dev failed\n", __func__);
2874 if (dev->priv_flags & IFF_ISATAP) {
2875 addrconf_addr_gen(idev, false);
2879 sit_add_v4_addrs(idev);
2881 if (dev->flags&IFF_POINTOPOINT)
2882 addrconf_add_mroute(dev);
2886 #if IS_ENABLED(CONFIG_NET_IPGRE)
2887 static void addrconf_gre_config(struct net_device *dev)
2889 struct inet6_dev *idev;
2893 idev = ipv6_find_idev(dev);
2895 pr_debug("%s: add_dev failed\n", __func__);
2899 addrconf_addr_gen(idev, true);
2903 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2906 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2907 struct inet6_dev *idev = __in6_dev_get(dev);
2908 int run_pending = 0;
2912 case NETDEV_REGISTER:
2913 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2914 idev = ipv6_add_dev(dev);
2916 return notifier_from_errno(PTR_ERR(idev));
2922 if (dev->flags & IFF_SLAVE)
2925 if (idev && idev->cnf.disable_ipv6)
2928 if (event == NETDEV_UP) {
2929 if (!addrconf_qdisc_ok(dev)) {
2930 /* device is not ready yet. */
2931 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2936 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2937 idev = ipv6_add_dev(dev);
2939 if (!IS_ERR_OR_NULL(idev)) {
2940 idev->if_flags |= IF_READY;
2944 if (!addrconf_qdisc_ok(dev)) {
2945 /* device is still not ready. */
2950 if (idev->if_flags & IF_READY)
2951 /* device is already configured. */
2953 idev->if_flags |= IF_READY;
2956 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2962 switch (dev->type) {
2963 #if IS_ENABLED(CONFIG_IPV6_SIT)
2965 addrconf_sit_config(dev);
2968 #if IS_ENABLED(CONFIG_NET_IPGRE)
2970 addrconf_gre_config(dev);
2973 case ARPHRD_LOOPBACK:
2978 addrconf_dev_config(dev);
2982 if (!IS_ERR_OR_NULL(idev)) {
2984 addrconf_dad_run(idev);
2987 * If the MTU changed during the interface down,
2988 * when the interface up, the changed MTU must be
2989 * reflected in the idev as well as routers.
2991 if (idev->cnf.mtu6 != dev->mtu &&
2992 dev->mtu >= IPV6_MIN_MTU) {
2993 rt6_mtu_change(dev, dev->mtu);
2994 idev->cnf.mtu6 = dev->mtu;
2996 idev->tstamp = jiffies;
2997 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3000 * If the changed mtu during down is lower than
3001 * IPV6_MIN_MTU stop IPv6 on this interface.
3003 if (dev->mtu < IPV6_MIN_MTU)
3004 addrconf_ifdown(dev, 1);
3008 case NETDEV_CHANGEMTU:
3009 if (idev && dev->mtu >= IPV6_MIN_MTU) {
3010 rt6_mtu_change(dev, dev->mtu);
3011 idev->cnf.mtu6 = dev->mtu;
3015 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3016 idev = ipv6_add_dev(dev);
3022 * if MTU under IPV6_MIN_MTU.
3023 * Stop IPv6 on this interface.
3027 case NETDEV_UNREGISTER:
3029 * Remove all addresses from this interface.
3031 addrconf_ifdown(dev, event != NETDEV_DOWN);
3034 case NETDEV_CHANGENAME:
3036 snmp6_unregister_dev(idev);
3037 addrconf_sysctl_unregister(idev);
3038 err = addrconf_sysctl_register(idev);
3040 return notifier_from_errno(err);
3041 err = snmp6_register_dev(idev);
3043 addrconf_sysctl_unregister(idev);
3044 return notifier_from_errno(err);
3049 case NETDEV_PRE_TYPE_CHANGE:
3050 case NETDEV_POST_TYPE_CHANGE:
3051 addrconf_type_change(dev, event);
3059 * addrconf module should be notified of a device going up
3061 static struct notifier_block ipv6_dev_notf = {
3062 .notifier_call = addrconf_notify,
3065 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3067 struct inet6_dev *idev;
3070 idev = __in6_dev_get(dev);
3072 if (event == NETDEV_POST_TYPE_CHANGE)
3073 ipv6_mc_remap(idev);
3074 else if (event == NETDEV_PRE_TYPE_CHANGE)
3075 ipv6_mc_unmap(idev);
3078 static int addrconf_ifdown(struct net_device *dev, int how)
3080 struct net *net = dev_net(dev);
3081 struct inet6_dev *idev;
3082 struct inet6_ifaddr *ifa;
3087 rt6_ifdown(net, dev);
3088 neigh_ifdown(&nd_tbl, dev);
3090 idev = __in6_dev_get(dev);
3095 * Step 1: remove reference to ipv6 device from parent device.
3101 /* protected by rtnl_lock */
3102 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3104 /* Step 1.5: remove snmp6 entry */
3105 snmp6_unregister_dev(idev);
3109 /* Step 2: clear hash table */
3110 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3111 struct hlist_head *h = &inet6_addr_lst[i];
3113 spin_lock_bh(&addrconf_hash_lock);
3115 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3116 if (ifa->idev == idev) {
3117 hlist_del_init_rcu(&ifa->addr_lst);
3118 addrconf_del_dad_work(ifa);
3122 spin_unlock_bh(&addrconf_hash_lock);
3125 write_lock_bh(&idev->lock);
3127 addrconf_del_rs_timer(idev);
3129 /* Step 2: clear flags for stateless addrconf */
3131 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3133 if (how && del_timer(&idev->regen_timer))
3136 /* Step 3: clear tempaddr list */
3137 while (!list_empty(&idev->tempaddr_list)) {
3138 ifa = list_first_entry(&idev->tempaddr_list,
3139 struct inet6_ifaddr, tmp_list);
3140 list_del(&ifa->tmp_list);
3141 write_unlock_bh(&idev->lock);
3142 spin_lock_bh(&ifa->lock);
3145 in6_ifa_put(ifa->ifpub);
3148 spin_unlock_bh(&ifa->lock);
3150 write_lock_bh(&idev->lock);
3153 while (!list_empty(&idev->addr_list)) {
3154 ifa = list_first_entry(&idev->addr_list,
3155 struct inet6_ifaddr, if_list);
3156 addrconf_del_dad_work(ifa);
3158 list_del(&ifa->if_list);
3160 write_unlock_bh(&idev->lock);
3162 spin_lock_bh(&ifa->state_lock);
3164 ifa->state = INET6_IFADDR_STATE_DEAD;
3165 spin_unlock_bh(&ifa->state_lock);
3167 if (state != INET6_IFADDR_STATE_DEAD) {
3168 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3169 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3173 write_lock_bh(&idev->lock);
3176 write_unlock_bh(&idev->lock);
3178 /* Step 5: Discard anycast and multicast list */
3180 ipv6_ac_destroy_dev(idev);
3181 ipv6_mc_destroy_dev(idev);
3186 idev->tstamp = jiffies;
3188 /* Last: Shot the device (if unregistered) */
3190 addrconf_sysctl_unregister(idev);
3191 neigh_parms_release(&nd_tbl, idev->nd_parms);
3192 neigh_ifdown(&nd_tbl, dev);
3198 static void addrconf_rs_timer(unsigned long data)
3200 struct inet6_dev *idev = (struct inet6_dev *)data;
3201 struct net_device *dev = idev->dev;
3202 struct in6_addr lladdr;
3204 write_lock(&idev->lock);
3205 if (idev->dead || !(idev->if_flags & IF_READY))
3208 if (!ipv6_accept_ra(idev))
3211 /* Announcement received after solicitation was sent */
3212 if (idev->if_flags & IF_RA_RCVD)
3215 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3216 write_unlock(&idev->lock);
3217 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3218 ndisc_send_rs(dev, &lladdr,
3219 &in6addr_linklocal_allrouters);
3223 write_lock(&idev->lock);
3224 /* The wait after the last probe can be shorter */
3225 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3226 idev->cnf.rtr_solicits) ?
3227 idev->cnf.rtr_solicit_delay :
3228 idev->cnf.rtr_solicit_interval);
3231 * Note: we do not support deprecated "all on-link"
3232 * assumption any longer.
3234 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3238 write_unlock(&idev->lock);
3244 * Duplicate Address Detection
3246 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3248 unsigned long rand_num;
3249 struct inet6_dev *idev = ifp->idev;
3251 if (ifp->flags & IFA_F_OPTIMISTIC)
3254 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3256 ifp->dad_probes = idev->cnf.dad_transmits;
3257 addrconf_mod_dad_work(ifp, rand_num);
3260 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3262 struct inet6_dev *idev = ifp->idev;
3263 struct net_device *dev = idev->dev;
3265 addrconf_join_solict(dev, &ifp->addr);
3267 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3269 read_lock_bh(&idev->lock);
3270 spin_lock(&ifp->lock);
3271 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3274 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3275 idev->cnf.accept_dad < 1 ||
3276 !(ifp->flags&IFA_F_TENTATIVE) ||
3277 ifp->flags & IFA_F_NODAD) {
3278 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3279 spin_unlock(&ifp->lock);
3280 read_unlock_bh(&idev->lock);
3282 addrconf_dad_completed(ifp);
3286 if (!(idev->if_flags & IF_READY)) {
3287 spin_unlock(&ifp->lock);
3288 read_unlock_bh(&idev->lock);
3290 * If the device is not ready:
3291 * - keep it tentative if it is a permanent address.
3292 * - otherwise, kill it.
3295 addrconf_dad_stop(ifp, 0);
3300 * Optimistic nodes can start receiving
3303 if (ifp->flags & IFA_F_OPTIMISTIC) {
3304 ip6_ins_rt(ifp->rt);
3305 if (ipv6_use_optimistic_addr(idev)) {
3306 /* Because optimistic nodes can use this address,
3307 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3309 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3313 addrconf_dad_kick(ifp);
3315 spin_unlock(&ifp->lock);
3316 read_unlock_bh(&idev->lock);
3319 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3321 bool begin_dad = false;
3323 spin_lock_bh(&ifp->state_lock);
3324 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3325 ifp->state = INET6_IFADDR_STATE_PREDAD;
3328 spin_unlock_bh(&ifp->state_lock);
3331 addrconf_mod_dad_work(ifp, 0);
3334 static void addrconf_dad_work(struct work_struct *w)
3336 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3337 struct inet6_ifaddr,
3339 struct inet6_dev *idev = ifp->idev;
3340 struct in6_addr mcaddr;
3346 } action = DAD_PROCESS;
3350 spin_lock_bh(&ifp->state_lock);
3351 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3353 ifp->state = INET6_IFADDR_STATE_DAD;
3354 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3356 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3358 spin_unlock_bh(&ifp->state_lock);
3360 if (action == DAD_BEGIN) {
3361 addrconf_dad_begin(ifp);
3363 } else if (action == DAD_ABORT) {
3364 addrconf_dad_stop(ifp, 1);
3368 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3371 write_lock_bh(&idev->lock);
3372 if (idev->dead || !(idev->if_flags & IF_READY)) {
3373 write_unlock_bh(&idev->lock);
3377 spin_lock(&ifp->lock);
3378 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3379 spin_unlock(&ifp->lock);
3380 write_unlock_bh(&idev->lock);
3384 if (ifp->dad_probes == 0) {
3386 * DAD was successful
3389 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3390 spin_unlock(&ifp->lock);
3391 write_unlock_bh(&idev->lock);
3393 addrconf_dad_completed(ifp);
3399 addrconf_mod_dad_work(ifp,
3400 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3401 spin_unlock(&ifp->lock);
3402 write_unlock_bh(&idev->lock);
3404 /* send a neighbour solicitation for our addr */
3405 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3406 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3412 /* ifp->idev must be at least read locked */
3413 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3415 struct inet6_ifaddr *ifpiter;
3416 struct inet6_dev *idev = ifp->idev;
3418 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3419 if (ifpiter->scope > IFA_LINK)
3421 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3422 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3423 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3430 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3432 struct net_device *dev = ifp->idev->dev;
3433 struct in6_addr lladdr;
3434 bool send_rs, send_mld;
3436 addrconf_del_dad_work(ifp);
3439 * Configure the address for reception. Now it is valid.
3442 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3444 /* If added prefix is link local and we are prepared to process
3445 router advertisements, start sending router solicitations.
3448 read_lock_bh(&ifp->idev->lock);
3449 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3450 send_rs = send_mld &&
3451 ipv6_accept_ra(ifp->idev) &&
3452 ifp->idev->cnf.rtr_solicits > 0 &&
3453 (dev->flags&IFF_LOOPBACK) == 0;
3454 read_unlock_bh(&ifp->idev->lock);
3456 /* While dad is in progress mld report's source address is in6_addrany.
3457 * Resend with proper ll now.
3460 ipv6_mc_dad_complete(ifp->idev);
3464 * If a host as already performed a random delay
3465 * [...] as part of DAD [...] there is no need
3466 * to delay again before sending the first RS
3468 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3470 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3472 write_lock_bh(&ifp->idev->lock);
3473 spin_lock(&ifp->lock);
3474 ifp->idev->rs_probes = 1;
3475 ifp->idev->if_flags |= IF_RS_SENT;
3476 addrconf_mod_rs_timer(ifp->idev,
3477 ifp->idev->cnf.rtr_solicit_interval);
3478 spin_unlock(&ifp->lock);
3479 write_unlock_bh(&ifp->idev->lock);
3483 static void addrconf_dad_run(struct inet6_dev *idev)
3485 struct inet6_ifaddr *ifp;
3487 read_lock_bh(&idev->lock);
3488 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3489 spin_lock(&ifp->lock);
3490 if (ifp->flags & IFA_F_TENTATIVE &&
3491 ifp->state == INET6_IFADDR_STATE_DAD)
3492 addrconf_dad_kick(ifp);
3493 spin_unlock(&ifp->lock);
3495 read_unlock_bh(&idev->lock);
3498 #ifdef CONFIG_PROC_FS
3499 struct if6_iter_state {
3500 struct seq_net_private p;
3505 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3507 struct inet6_ifaddr *ifa = NULL;
3508 struct if6_iter_state *state = seq->private;
3509 struct net *net = seq_file_net(seq);
3512 /* initial bucket if pos is 0 */
3518 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3519 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3521 if (!net_eq(dev_net(ifa->idev->dev), net))
3523 /* sync with offset */
3524 if (p < state->offset) {
3532 /* prepare for next bucket */
3539 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3540 struct inet6_ifaddr *ifa)
3542 struct if6_iter_state *state = seq->private;
3543 struct net *net = seq_file_net(seq);
3545 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3546 if (!net_eq(dev_net(ifa->idev->dev), net))
3552 while (++state->bucket < IN6_ADDR_HSIZE) {
3554 hlist_for_each_entry_rcu_bh(ifa,
3555 &inet6_addr_lst[state->bucket], addr_lst) {
3556 if (!net_eq(dev_net(ifa->idev->dev), net))
3566 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3570 return if6_get_first(seq, *pos);
3573 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3575 struct inet6_ifaddr *ifa;
3577 ifa = if6_get_next(seq, v);
3582 static void if6_seq_stop(struct seq_file *seq, void *v)
3585 rcu_read_unlock_bh();
3588 static int if6_seq_show(struct seq_file *seq, void *v)
3590 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3591 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3593 ifp->idev->dev->ifindex,
3597 ifp->idev->dev->name);
3601 static const struct seq_operations if6_seq_ops = {
3602 .start = if6_seq_start,
3603 .next = if6_seq_next,
3604 .show = if6_seq_show,
3605 .stop = if6_seq_stop,
3608 static int if6_seq_open(struct inode *inode, struct file *file)
3610 return seq_open_net(inode, file, &if6_seq_ops,
3611 sizeof(struct if6_iter_state));
3614 static const struct file_operations if6_fops = {
3615 .owner = THIS_MODULE,
3616 .open = if6_seq_open,
3618 .llseek = seq_lseek,
3619 .release = seq_release_net,
3622 static int __net_init if6_proc_net_init(struct net *net)
3624 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3629 static void __net_exit if6_proc_net_exit(struct net *net)
3631 remove_proc_entry("if_inet6", net->proc_net);
3634 static struct pernet_operations if6_proc_net_ops = {
3635 .init = if6_proc_net_init,
3636 .exit = if6_proc_net_exit,
3639 int __init if6_proc_init(void)
3641 return register_pernet_subsys(&if6_proc_net_ops);
3644 void if6_proc_exit(void)
3646 unregister_pernet_subsys(&if6_proc_net_ops);
3648 #endif /* CONFIG_PROC_FS */
3650 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3651 /* Check if address is a home address configured on any interface. */
3652 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3655 struct inet6_ifaddr *ifp = NULL;
3656 unsigned int hash = inet6_addr_hash(addr);
3659 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3660 if (!net_eq(dev_net(ifp->idev->dev), net))
3662 if (ipv6_addr_equal(&ifp->addr, addr) &&
3663 (ifp->flags & IFA_F_HOMEADDRESS)) {
3668 rcu_read_unlock_bh();
3674 * Periodic address status verification
3677 static void addrconf_verify_rtnl(void)
3679 unsigned long now, next, next_sec, next_sched;
3680 struct inet6_ifaddr *ifp;
3687 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3689 cancel_delayed_work(&addr_chk_work);
3691 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3693 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
3696 /* When setting preferred_lft to a value not zero or
3697 * infinity, while valid_lft is infinity
3698 * IFA_F_PERMANENT has a non-infinity life time.
3700 if ((ifp->flags & IFA_F_PERMANENT) &&
3701 (ifp->prefered_lft == INFINITY_LIFE_TIME))
3704 spin_lock(&ifp->lock);
3705 /* We try to batch several events at once. */
3706 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3708 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3709 age >= ifp->valid_lft) {
3710 spin_unlock(&ifp->lock);
3714 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3715 spin_unlock(&ifp->lock);
3717 } else if (age >= ifp->prefered_lft) {
3718 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3721 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3723 ifp->flags |= IFA_F_DEPRECATED;
3726 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3727 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
3728 next = ifp->tstamp + ifp->valid_lft * HZ;
3730 spin_unlock(&ifp->lock);
3735 ipv6_ifa_notify(0, ifp);
3739 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3740 !(ifp->flags&IFA_F_TENTATIVE)) {
3741 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3742 ifp->idev->cnf.dad_transmits *
3743 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
3745 if (age >= ifp->prefered_lft - regen_advance) {
3746 struct inet6_ifaddr *ifpub = ifp->ifpub;
3747 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3748 next = ifp->tstamp + ifp->prefered_lft * HZ;
3749 if (!ifp->regen_count && ifpub) {
3752 in6_ifa_hold(ifpub);
3753 spin_unlock(&ifp->lock);
3755 spin_lock(&ifpub->lock);
3756 ifpub->regen_count = 0;
3757 spin_unlock(&ifpub->lock);
3758 ipv6_create_tempaddr(ifpub, ifp);
3763 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3764 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3765 spin_unlock(&ifp->lock);
3767 /* ifp->prefered_lft <= ifp->valid_lft */
3768 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3769 next = ifp->tstamp + ifp->prefered_lft * HZ;
3770 spin_unlock(&ifp->lock);
3775 next_sec = round_jiffies_up(next);
3778 /* If rounded timeout is accurate enough, accept it. */
3779 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3780 next_sched = next_sec;
3782 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3783 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3784 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3786 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3787 now, next, next_sec, next_sched);
3788 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
3789 rcu_read_unlock_bh();
3792 static void addrconf_verify_work(struct work_struct *w)
3795 addrconf_verify_rtnl();
3799 static void addrconf_verify(void)
3801 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3804 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3805 struct in6_addr **peer_pfx)
3807 struct in6_addr *pfx = NULL;
3812 pfx = nla_data(addr);
3815 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3817 pfx = nla_data(local);
3823 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3824 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3825 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3826 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3827 [IFA_FLAGS] = { .len = sizeof(u32) },
3831 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3833 struct net *net = sock_net(skb->sk);
3834 struct ifaddrmsg *ifm;
3835 struct nlattr *tb[IFA_MAX+1];
3836 struct in6_addr *pfx, *peer_pfx;
3840 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3844 ifm = nlmsg_data(nlh);
3845 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3849 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3851 /* We ignore other flags so far. */
3852 ifa_flags &= IFA_F_MANAGETEMPADDR;
3854 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
3855 ifm->ifa_prefixlen);
3858 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
3859 u32 prefered_lft, u32 valid_lft)
3863 unsigned long timeout;
3864 bool was_managetempaddr;
3865 bool had_prefixroute;
3869 if (!valid_lft || (prefered_lft > valid_lft))
3872 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
3873 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
3876 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3877 if (addrconf_finite_timeout(timeout)) {
3878 expires = jiffies_to_clock_t(timeout * HZ);
3879 valid_lft = timeout;
3880 flags = RTF_EXPIRES;
3884 ifa_flags |= IFA_F_PERMANENT;
3887 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3888 if (addrconf_finite_timeout(timeout)) {
3890 ifa_flags |= IFA_F_DEPRECATED;
3891 prefered_lft = timeout;
3894 spin_lock_bh(&ifp->lock);
3895 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
3896 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
3897 !(ifp->flags & IFA_F_NOPREFIXROUTE);
3898 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
3899 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3900 IFA_F_NOPREFIXROUTE);
3901 ifp->flags |= ifa_flags;
3902 ifp->tstamp = jiffies;
3903 ifp->valid_lft = valid_lft;
3904 ifp->prefered_lft = prefered_lft;
3906 spin_unlock_bh(&ifp->lock);
3907 if (!(ifp->flags&IFA_F_TENTATIVE))
3908 ipv6_ifa_notify(0, ifp);
3910 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
3911 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3913 } else if (had_prefixroute) {
3914 enum cleanup_prefix_rt_t action;
3915 unsigned long rt_expires;
3917 write_lock_bh(&ifp->idev->lock);
3918 action = check_cleanup_prefix_route(ifp, &rt_expires);
3919 write_unlock_bh(&ifp->idev->lock);
3921 if (action != CLEANUP_PREFIX_RT_NOP) {
3922 cleanup_prefix_route(ifp, rt_expires,
3923 action == CLEANUP_PREFIX_RT_DEL);
3927 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
3928 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
3929 valid_lft = prefered_lft = 0;
3930 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
3931 !was_managetempaddr, jiffies);
3934 addrconf_verify_rtnl();
3940 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3942 struct net *net = sock_net(skb->sk);
3943 struct ifaddrmsg *ifm;
3944 struct nlattr *tb[IFA_MAX+1];
3945 struct in6_addr *pfx, *peer_pfx;
3946 struct inet6_ifaddr *ifa;
3947 struct net_device *dev;
3948 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3952 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3956 ifm = nlmsg_data(nlh);
3957 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3961 if (tb[IFA_CACHEINFO]) {
3962 struct ifa_cacheinfo *ci;
3964 ci = nla_data(tb[IFA_CACHEINFO]);
3965 valid_lft = ci->ifa_valid;
3966 preferred_lft = ci->ifa_prefered;
3968 preferred_lft = INFINITY_LIFE_TIME;
3969 valid_lft = INFINITY_LIFE_TIME;
3972 dev = __dev_get_by_index(net, ifm->ifa_index);
3976 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3978 /* We ignore other flags so far. */
3979 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3980 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
3982 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3985 * It would be best to check for !NLM_F_CREATE here but
3986 * userspace already relies on not having to provide this.
3988 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3989 ifm->ifa_prefixlen, ifa_flags,
3990 preferred_lft, valid_lft);
3993 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3994 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3997 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4004 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4005 u8 scope, int ifindex)
4007 struct ifaddrmsg *ifm;
4009 ifm = nlmsg_data(nlh);
4010 ifm->ifa_family = AF_INET6;
4011 ifm->ifa_prefixlen = prefixlen;
4012 ifm->ifa_flags = flags;
4013 ifm->ifa_scope = scope;
4014 ifm->ifa_index = ifindex;
4017 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4018 unsigned long tstamp, u32 preferred, u32 valid)
4020 struct ifa_cacheinfo ci;
4022 ci.cstamp = cstamp_delta(cstamp);
4023 ci.tstamp = cstamp_delta(tstamp);
4024 ci.ifa_prefered = preferred;
4025 ci.ifa_valid = valid;
4027 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4030 static inline int rt_scope(int ifa_scope)
4032 if (ifa_scope & IFA_HOST)
4033 return RT_SCOPE_HOST;
4034 else if (ifa_scope & IFA_LINK)
4035 return RT_SCOPE_LINK;
4036 else if (ifa_scope & IFA_SITE)
4037 return RT_SCOPE_SITE;
4039 return RT_SCOPE_UNIVERSE;
4042 static inline int inet6_ifaddr_msgsize(void)
4044 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4045 + nla_total_size(16) /* IFA_LOCAL */
4046 + nla_total_size(16) /* IFA_ADDRESS */
4047 + nla_total_size(sizeof(struct ifa_cacheinfo))
4048 + nla_total_size(4) /* IFA_FLAGS */;
4051 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4052 u32 portid, u32 seq, int event, unsigned int flags)
4054 struct nlmsghdr *nlh;
4055 u32 preferred, valid;
4057 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4061 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4062 ifa->idev->dev->ifindex);
4064 if (!((ifa->flags&IFA_F_PERMANENT) &&
4065 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4066 preferred = ifa->prefered_lft;
4067 valid = ifa->valid_lft;
4068 if (preferred != INFINITY_LIFE_TIME) {
4069 long tval = (jiffies - ifa->tstamp)/HZ;
4070 if (preferred > tval)
4074 if (valid != INFINITY_LIFE_TIME) {
4082 preferred = INFINITY_LIFE_TIME;
4083 valid = INFINITY_LIFE_TIME;
4086 if (!ipv6_addr_any(&ifa->peer_addr)) {
4087 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
4088 nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
4091 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
4094 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4097 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4100 nlmsg_end(skb, nlh);
4104 nlmsg_cancel(skb, nlh);
4108 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4109 u32 portid, u32 seq, int event, u16 flags)
4111 struct nlmsghdr *nlh;
4112 u8 scope = RT_SCOPE_UNIVERSE;
4113 int ifindex = ifmca->idev->dev->ifindex;
4115 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4116 scope = RT_SCOPE_SITE;
4118 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4122 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4123 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
4124 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4125 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4126 nlmsg_cancel(skb, nlh);
4130 nlmsg_end(skb, nlh);
4134 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4135 u32 portid, u32 seq, int event, unsigned int flags)
4137 struct nlmsghdr *nlh;
4138 u8 scope = RT_SCOPE_UNIVERSE;
4139 int ifindex = ifaca->aca_idev->dev->ifindex;
4141 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4142 scope = RT_SCOPE_SITE;
4144 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4148 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4149 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
4150 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4151 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4152 nlmsg_cancel(skb, nlh);
4156 nlmsg_end(skb, nlh);
4166 /* called with rcu_read_lock() */
4167 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4168 struct netlink_callback *cb, enum addr_type_t type,
4169 int s_ip_idx, int *p_ip_idx)
4171 struct ifmcaddr6 *ifmca;
4172 struct ifacaddr6 *ifaca;
4174 int ip_idx = *p_ip_idx;
4176 read_lock_bh(&idev->lock);
4178 case UNICAST_ADDR: {
4179 struct inet6_ifaddr *ifa;
4181 /* unicast address incl. temp addr */
4182 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4183 if (++ip_idx < s_ip_idx)
4185 err = inet6_fill_ifaddr(skb, ifa,
4186 NETLINK_CB(cb->skb).portid,
4192 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4196 case MULTICAST_ADDR:
4197 /* multicast address */
4198 for (ifmca = idev->mc_list; ifmca;
4199 ifmca = ifmca->next, ip_idx++) {
4200 if (ip_idx < s_ip_idx)
4202 err = inet6_fill_ifmcaddr(skb, ifmca,
4203 NETLINK_CB(cb->skb).portid,
4212 /* anycast address */
4213 for (ifaca = idev->ac_list; ifaca;
4214 ifaca = ifaca->aca_next, ip_idx++) {
4215 if (ip_idx < s_ip_idx)
4217 err = inet6_fill_ifacaddr(skb, ifaca,
4218 NETLINK_CB(cb->skb).portid,
4229 read_unlock_bh(&idev->lock);
4234 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4235 enum addr_type_t type)
4237 struct net *net = sock_net(skb->sk);
4240 int s_idx, s_ip_idx;
4241 struct net_device *dev;
4242 struct inet6_dev *idev;
4243 struct hlist_head *head;
4246 s_idx = idx = cb->args[1];
4247 s_ip_idx = ip_idx = cb->args[2];
4250 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4251 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4253 head = &net->dev_index_head[h];
4254 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4257 if (h > s_h || idx > s_idx)
4260 idev = __in6_dev_get(dev);
4264 if (in6_dump_addrs(idev, skb, cb, type,
4265 s_ip_idx, &ip_idx) < 0)
4275 cb->args[2] = ip_idx;
4280 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4282 enum addr_type_t type = UNICAST_ADDR;
4284 return inet6_dump_addr(skb, cb, type);
4287 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4289 enum addr_type_t type = MULTICAST_ADDR;
4291 return inet6_dump_addr(skb, cb, type);
4295 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4297 enum addr_type_t type = ANYCAST_ADDR;
4299 return inet6_dump_addr(skb, cb, type);
4302 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4304 struct net *net = sock_net(in_skb->sk);
4305 struct ifaddrmsg *ifm;
4306 struct nlattr *tb[IFA_MAX+1];
4307 struct in6_addr *addr = NULL, *peer;
4308 struct net_device *dev = NULL;
4309 struct inet6_ifaddr *ifa;
4310 struct sk_buff *skb;
4313 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4317 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4323 ifm = nlmsg_data(nlh);
4325 dev = __dev_get_by_index(net, ifm->ifa_index);
4327 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4329 err = -EADDRNOTAVAIL;
4333 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4339 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4340 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4342 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4343 WARN_ON(err == -EMSGSIZE);
4347 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4354 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4356 struct sk_buff *skb;
4357 struct net *net = dev_net(ifa->idev->dev);
4360 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4364 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4366 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4367 WARN_ON(err == -EMSGSIZE);
4371 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4375 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4378 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4379 __s32 *array, int bytes)
4381 BUG_ON(bytes < (DEVCONF_MAX * 4));
4383 memset(array, 0, bytes);
4384 array[DEVCONF_FORWARDING] = cnf->forwarding;
4385 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4386 array[DEVCONF_MTU6] = cnf->mtu6;
4387 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4388 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4389 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4390 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4391 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4392 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4393 jiffies_to_msecs(cnf->rtr_solicit_interval);
4394 array[DEVCONF_RTR_SOLICIT_DELAY] =
4395 jiffies_to_msecs(cnf->rtr_solicit_delay);
4396 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4397 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4398 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4399 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4400 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4401 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4402 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4403 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4404 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4405 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4406 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4407 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4408 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4409 #ifdef CONFIG_IPV6_ROUTER_PREF
4410 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4411 array[DEVCONF_RTR_PROBE_INTERVAL] =
4412 jiffies_to_msecs(cnf->rtr_probe_interval);
4413 #ifdef CONFIG_IPV6_ROUTE_INFO
4414 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4417 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4418 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4419 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4420 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4421 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4423 #ifdef CONFIG_IPV6_MROUTE
4424 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4426 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4427 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4428 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4429 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4430 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4431 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4432 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4435 static inline size_t inet6_ifla6_size(void)
4437 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4438 + nla_total_size(sizeof(struct ifla_cacheinfo))
4439 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4440 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4441 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4442 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4445 static inline size_t inet6_if_nlmsg_size(void)
4447 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4448 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4449 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4450 + nla_total_size(4) /* IFLA_MTU */
4451 + nla_total_size(4) /* IFLA_LINK */
4452 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4455 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4456 int items, int bytes)
4459 int pad = bytes - sizeof(u64) * items;
4462 /* Use put_unaligned() because stats may not be aligned for u64. */
4463 put_unaligned(items, &stats[0]);
4464 for (i = 1; i < items; i++)
4465 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4467 memset(&stats[items], 0, pad);
4470 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
4471 int items, int bytes, size_t syncpoff)
4474 int pad = bytes - sizeof(u64) * items;
4477 /* Use put_unaligned() because stats may not be aligned for u64. */
4478 put_unaligned(items, &stats[0]);
4479 for (i = 1; i < items; i++)
4480 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4482 memset(&stats[items], 0, pad);
4485 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4489 case IFLA_INET6_STATS:
4490 __snmp6_fill_stats64(stats, idev->stats.ipv6,
4491 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4493 case IFLA_INET6_ICMP6STATS:
4494 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4499 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4502 struct ifla_cacheinfo ci;
4504 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4505 goto nla_put_failure;
4506 ci.max_reasm_len = IPV6_MAXPLEN;
4507 ci.tstamp = cstamp_delta(idev->tstamp);
4508 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4509 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4510 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4511 goto nla_put_failure;
4512 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4514 goto nla_put_failure;
4515 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4517 /* XXX - MC not implemented */
4519 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4521 goto nla_put_failure;
4522 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4524 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4526 goto nla_put_failure;
4527 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4529 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4531 goto nla_put_failure;
4533 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4534 goto nla_put_failure;
4536 read_lock_bh(&idev->lock);
4537 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4538 read_unlock_bh(&idev->lock);
4546 static size_t inet6_get_link_af_size(const struct net_device *dev)
4548 if (!__in6_dev_get(dev))
4551 return inet6_ifla6_size();
4554 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4556 struct inet6_dev *idev = __in6_dev_get(dev);
4561 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4567 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4569 struct inet6_ifaddr *ifp;
4570 struct net_device *dev = idev->dev;
4571 bool update_rs = false;
4572 struct in6_addr ll_addr;
4578 if (ipv6_addr_any(token))
4580 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4582 if (!ipv6_accept_ra(idev))
4584 if (idev->cnf.rtr_solicits <= 0)
4587 write_lock_bh(&idev->lock);
4589 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4590 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4592 write_unlock_bh(&idev->lock);
4594 if (!idev->dead && (idev->if_flags & IF_READY) &&
4595 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4596 IFA_F_OPTIMISTIC)) {
4598 /* If we're not ready, then normal ifup will take care
4599 * of this. Otherwise, we need to request our rs here.
4601 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4605 write_lock_bh(&idev->lock);
4608 idev->if_flags |= IF_RS_SENT;
4609 idev->rs_probes = 1;
4610 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4613 /* Well, that's kinda nasty ... */
4614 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4615 spin_lock(&ifp->lock);
4616 if (ifp->tokenized) {
4618 ifp->prefered_lft = 0;
4620 spin_unlock(&ifp->lock);
4623 write_unlock_bh(&idev->lock);
4624 inet6_ifinfo_notify(RTM_NEWLINK, idev);
4625 addrconf_verify_rtnl();
4629 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
4630 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
4631 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
4634 static int inet6_validate_link_af(const struct net_device *dev,
4635 const struct nlattr *nla)
4637 struct nlattr *tb[IFLA_INET6_MAX + 1];
4639 if (dev && !__in6_dev_get(dev))
4640 return -EAFNOSUPPORT;
4642 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
4645 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4648 struct inet6_dev *idev = __in6_dev_get(dev);
4649 struct nlattr *tb[IFLA_INET6_MAX + 1];
4652 return -EAFNOSUPPORT;
4654 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4657 if (tb[IFLA_INET6_TOKEN]) {
4658 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4663 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4664 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4666 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4667 mode != IN6_ADDR_GEN_MODE_NONE)
4669 idev->addr_gen_mode = mode;
4676 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4677 u32 portid, u32 seq, int event, unsigned int flags)
4679 struct net_device *dev = idev->dev;
4680 struct ifinfomsg *hdr;
4681 struct nlmsghdr *nlh;
4684 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4688 hdr = nlmsg_data(nlh);
4689 hdr->ifi_family = AF_INET6;
4691 hdr->ifi_type = dev->type;
4692 hdr->ifi_index = dev->ifindex;
4693 hdr->ifi_flags = dev_get_flags(dev);
4694 hdr->ifi_change = 0;
4696 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4698 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4699 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4700 (dev->ifindex != dev->iflink &&
4701 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4702 goto nla_put_failure;
4703 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4704 if (protoinfo == NULL)
4705 goto nla_put_failure;
4707 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4708 goto nla_put_failure;
4710 nla_nest_end(skb, protoinfo);
4711 nlmsg_end(skb, nlh);
4715 nlmsg_cancel(skb, nlh);
4719 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4721 struct net *net = sock_net(skb->sk);
4724 struct net_device *dev;
4725 struct inet6_dev *idev;
4726 struct hlist_head *head;
4729 s_idx = cb->args[1];
4732 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4734 head = &net->dev_index_head[h];
4735 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4738 idev = __in6_dev_get(dev);
4741 if (inet6_fill_ifinfo(skb, idev,
4742 NETLINK_CB(cb->skb).portid,
4744 RTM_NEWLINK, NLM_F_MULTI) < 0)
4758 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4760 struct sk_buff *skb;
4761 struct net *net = dev_net(idev->dev);
4764 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4768 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4770 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4771 WARN_ON(err == -EMSGSIZE);
4775 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4779 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4782 static inline size_t inet6_prefix_nlmsg_size(void)
4784 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4785 + nla_total_size(sizeof(struct in6_addr))
4786 + nla_total_size(sizeof(struct prefix_cacheinfo));
4789 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4790 struct prefix_info *pinfo, u32 portid, u32 seq,
4791 int event, unsigned int flags)
4793 struct prefixmsg *pmsg;
4794 struct nlmsghdr *nlh;
4795 struct prefix_cacheinfo ci;
4797 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4801 pmsg = nlmsg_data(nlh);
4802 pmsg->prefix_family = AF_INET6;
4803 pmsg->prefix_pad1 = 0;
4804 pmsg->prefix_pad2 = 0;
4805 pmsg->prefix_ifindex = idev->dev->ifindex;
4806 pmsg->prefix_len = pinfo->prefix_len;
4807 pmsg->prefix_type = pinfo->type;
4808 pmsg->prefix_pad3 = 0;
4809 pmsg->prefix_flags = 0;
4811 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4812 if (pinfo->autoconf)
4813 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4815 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4816 goto nla_put_failure;
4817 ci.preferred_time = ntohl(pinfo->prefered);
4818 ci.valid_time = ntohl(pinfo->valid);
4819 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4820 goto nla_put_failure;
4821 nlmsg_end(skb, nlh);
4825 nlmsg_cancel(skb, nlh);
4829 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4830 struct prefix_info *pinfo)
4832 struct sk_buff *skb;
4833 struct net *net = dev_net(idev->dev);
4836 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4840 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
4842 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4843 WARN_ON(err == -EMSGSIZE);
4847 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4851 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4854 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4856 struct net *net = dev_net(ifp->idev->dev);
4861 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4866 * If the address was optimistic
4867 * we inserted the route at the start of
4868 * our DAD process, so we don't need
4871 if (!(ifp->rt->rt6i_node))
4872 ip6_ins_rt(ifp->rt);
4873 if (ifp->idev->cnf.forwarding)
4874 addrconf_join_anycast(ifp);
4875 if (!ipv6_addr_any(&ifp->peer_addr))
4876 addrconf_prefix_route(&ifp->peer_addr, 128,
4877 ifp->idev->dev, 0, 0);
4880 if (ifp->idev->cnf.forwarding)
4881 addrconf_leave_anycast(ifp);
4882 addrconf_leave_solict(ifp->idev, &ifp->addr);
4883 if (!ipv6_addr_any(&ifp->peer_addr)) {
4884 struct rt6_info *rt;
4886 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
4887 ifp->idev->dev, 0, 0);
4888 if (rt && ip6_del_rt(rt))
4891 dst_hold(&ifp->rt->dst);
4893 if (ip6_del_rt(ifp->rt))
4894 dst_free(&ifp->rt->dst);
4896 rt_genid_bump_ipv6(net);
4899 atomic_inc(&net->ipv6.dev_addr_genid);
4902 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4905 if (likely(ifp->idev->dead == 0))
4906 __ipv6_ifa_notify(event, ifp);
4907 rcu_read_unlock_bh();
4910 #ifdef CONFIG_SYSCTL
4913 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
4914 void __user *buffer, size_t *lenp, loff_t *ppos)
4916 int *valp = ctl->data;
4919 struct ctl_table lctl;
4923 * ctl->data points to idev->cnf.forwarding, we should
4924 * not modify it until we get the rtnl lock.
4929 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4932 ret = addrconf_fixup_forwarding(ctl, valp, val);
4939 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
4940 void __user *buffer, size_t *lenp, loff_t *ppos)
4942 struct inet6_dev *idev = ctl->extra1;
4943 int min_mtu = IPV6_MIN_MTU;
4944 struct ctl_table lctl;
4947 lctl.extra1 = &min_mtu;
4948 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
4950 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
4953 static void dev_disable_change(struct inet6_dev *idev)
4955 struct netdev_notifier_info info;
4957 if (!idev || !idev->dev)
4960 netdev_notifier_info_init(&info, idev->dev);
4961 if (idev->cnf.disable_ipv6)
4962 addrconf_notify(NULL, NETDEV_DOWN, &info);
4964 addrconf_notify(NULL, NETDEV_UP, &info);
4967 static void addrconf_disable_change(struct net *net, __s32 newf)
4969 struct net_device *dev;
4970 struct inet6_dev *idev;
4973 for_each_netdev_rcu(net, dev) {
4974 idev = __in6_dev_get(dev);
4976 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4977 idev->cnf.disable_ipv6 = newf;
4979 dev_disable_change(idev);
4985 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
4990 if (!rtnl_trylock())
4991 return restart_syscall();
4993 net = (struct net *)table->extra2;
4997 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5002 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5003 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5004 addrconf_disable_change(net, newf);
5005 } else if ((!newf) ^ (!old))
5006 dev_disable_change((struct inet6_dev *)table->extra1);
5013 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5014 void __user *buffer, size_t *lenp, loff_t *ppos)
5016 int *valp = ctl->data;
5019 struct ctl_table lctl;
5023 * ctl->data points to idev->cnf.disable_ipv6, we should
5024 * not modify it until we get the rtnl lock.
5029 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5032 ret = addrconf_disable_ipv6(ctl, valp, val);
5039 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5040 void __user *buffer, size_t *lenp, loff_t *ppos)
5042 int *valp = ctl->data;
5047 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5050 if (write && old != new) {
5051 struct net *net = ctl->extra2;
5053 if (!rtnl_trylock())
5054 return restart_syscall();
5056 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5057 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5058 NETCONFA_IFINDEX_DEFAULT,
5059 net->ipv6.devconf_dflt);
5060 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5061 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5062 NETCONFA_IFINDEX_ALL,
5063 net->ipv6.devconf_all);
5065 struct inet6_dev *idev = ctl->extra1;
5067 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5078 static struct addrconf_sysctl_table
5080 struct ctl_table_header *sysctl_header;
5081 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
5082 } addrconf_sysctl __read_mostly = {
5083 .sysctl_header = NULL,
5086 .procname = "forwarding",
5087 .data = &ipv6_devconf.forwarding,
5088 .maxlen = sizeof(int),
5090 .proc_handler = addrconf_sysctl_forward,
5093 .procname = "hop_limit",
5094 .data = &ipv6_devconf.hop_limit,
5095 .maxlen = sizeof(int),
5097 .proc_handler = proc_dointvec,
5101 .data = &ipv6_devconf.mtu6,
5102 .maxlen = sizeof(int),
5104 .proc_handler = addrconf_sysctl_mtu,
5107 .procname = "accept_ra",
5108 .data = &ipv6_devconf.accept_ra,
5109 .maxlen = sizeof(int),
5111 .proc_handler = proc_dointvec,
5114 .procname = "accept_redirects",
5115 .data = &ipv6_devconf.accept_redirects,
5116 .maxlen = sizeof(int),
5118 .proc_handler = proc_dointvec,
5121 .procname = "autoconf",
5122 .data = &ipv6_devconf.autoconf,
5123 .maxlen = sizeof(int),
5125 .proc_handler = proc_dointvec,
5128 .procname = "dad_transmits",
5129 .data = &ipv6_devconf.dad_transmits,
5130 .maxlen = sizeof(int),
5132 .proc_handler = proc_dointvec,
5135 .procname = "router_solicitations",
5136 .data = &ipv6_devconf.rtr_solicits,
5137 .maxlen = sizeof(int),
5139 .proc_handler = proc_dointvec,
5142 .procname = "router_solicitation_interval",
5143 .data = &ipv6_devconf.rtr_solicit_interval,
5144 .maxlen = sizeof(int),
5146 .proc_handler = proc_dointvec_jiffies,
5149 .procname = "router_solicitation_delay",
5150 .data = &ipv6_devconf.rtr_solicit_delay,
5151 .maxlen = sizeof(int),
5153 .proc_handler = proc_dointvec_jiffies,
5156 .procname = "force_mld_version",
5157 .data = &ipv6_devconf.force_mld_version,
5158 .maxlen = sizeof(int),
5160 .proc_handler = proc_dointvec,
5163 .procname = "mldv1_unsolicited_report_interval",
5165 &ipv6_devconf.mldv1_unsolicited_report_interval,
5166 .maxlen = sizeof(int),
5168 .proc_handler = proc_dointvec_ms_jiffies,
5171 .procname = "mldv2_unsolicited_report_interval",
5173 &ipv6_devconf.mldv2_unsolicited_report_interval,
5174 .maxlen = sizeof(int),
5176 .proc_handler = proc_dointvec_ms_jiffies,
5179 .procname = "use_tempaddr",
5180 .data = &ipv6_devconf.use_tempaddr,
5181 .maxlen = sizeof(int),
5183 .proc_handler = proc_dointvec,
5186 .procname = "temp_valid_lft",
5187 .data = &ipv6_devconf.temp_valid_lft,
5188 .maxlen = sizeof(int),
5190 .proc_handler = proc_dointvec,
5193 .procname = "temp_prefered_lft",
5194 .data = &ipv6_devconf.temp_prefered_lft,
5195 .maxlen = sizeof(int),
5197 .proc_handler = proc_dointvec,
5200 .procname = "regen_max_retry",
5201 .data = &ipv6_devconf.regen_max_retry,
5202 .maxlen = sizeof(int),
5204 .proc_handler = proc_dointvec,
5207 .procname = "max_desync_factor",
5208 .data = &ipv6_devconf.max_desync_factor,
5209 .maxlen = sizeof(int),
5211 .proc_handler = proc_dointvec,
5214 .procname = "max_addresses",
5215 .data = &ipv6_devconf.max_addresses,
5216 .maxlen = sizeof(int),
5218 .proc_handler = proc_dointvec,
5221 .procname = "accept_ra_defrtr",
5222 .data = &ipv6_devconf.accept_ra_defrtr,
5223 .maxlen = sizeof(int),
5225 .proc_handler = proc_dointvec,
5228 .procname = "accept_ra_pinfo",
5229 .data = &ipv6_devconf.accept_ra_pinfo,
5230 .maxlen = sizeof(int),
5232 .proc_handler = proc_dointvec,
5234 #ifdef CONFIG_IPV6_ROUTER_PREF
5236 .procname = "accept_ra_rtr_pref",
5237 .data = &ipv6_devconf.accept_ra_rtr_pref,
5238 .maxlen = sizeof(int),
5240 .proc_handler = proc_dointvec,
5243 .procname = "router_probe_interval",
5244 .data = &ipv6_devconf.rtr_probe_interval,
5245 .maxlen = sizeof(int),
5247 .proc_handler = proc_dointvec_jiffies,
5249 #ifdef CONFIG_IPV6_ROUTE_INFO
5251 .procname = "accept_ra_rt_info_max_plen",
5252 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5253 .maxlen = sizeof(int),
5255 .proc_handler = proc_dointvec,
5260 .procname = "proxy_ndp",
5261 .data = &ipv6_devconf.proxy_ndp,
5262 .maxlen = sizeof(int),
5264 .proc_handler = addrconf_sysctl_proxy_ndp,
5267 .procname = "accept_source_route",
5268 .data = &ipv6_devconf.accept_source_route,
5269 .maxlen = sizeof(int),
5271 .proc_handler = proc_dointvec,
5273 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5275 .procname = "optimistic_dad",
5276 .data = &ipv6_devconf.optimistic_dad,
5277 .maxlen = sizeof(int),
5279 .proc_handler = proc_dointvec,
5283 .procname = "use_optimistic",
5284 .data = &ipv6_devconf.use_optimistic,
5285 .maxlen = sizeof(int),
5287 .proc_handler = proc_dointvec,
5291 #ifdef CONFIG_IPV6_MROUTE
5293 .procname = "mc_forwarding",
5294 .data = &ipv6_devconf.mc_forwarding,
5295 .maxlen = sizeof(int),
5297 .proc_handler = proc_dointvec,
5301 .procname = "disable_ipv6",
5302 .data = &ipv6_devconf.disable_ipv6,
5303 .maxlen = sizeof(int),
5305 .proc_handler = addrconf_sysctl_disable,
5308 .procname = "accept_dad",
5309 .data = &ipv6_devconf.accept_dad,
5310 .maxlen = sizeof(int),
5312 .proc_handler = proc_dointvec,
5315 .procname = "force_tllao",
5316 .data = &ipv6_devconf.force_tllao,
5317 .maxlen = sizeof(int),
5319 .proc_handler = proc_dointvec
5322 .procname = "ndisc_notify",
5323 .data = &ipv6_devconf.ndisc_notify,
5324 .maxlen = sizeof(int),
5326 .proc_handler = proc_dointvec
5329 .procname = "suppress_frag_ndisc",
5330 .data = &ipv6_devconf.suppress_frag_ndisc,
5331 .maxlen = sizeof(int),
5333 .proc_handler = proc_dointvec
5336 .procname = "accept_ra_from_local",
5337 .data = &ipv6_devconf.accept_ra_from_local,
5338 .maxlen = sizeof(int),
5340 .proc_handler = proc_dointvec,
5343 .procname = "accept_ra_mtu",
5344 .data = &ipv6_devconf.accept_ra_mtu,
5345 .maxlen = sizeof(int),
5347 .proc_handler = proc_dointvec,
5355 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5356 struct inet6_dev *idev, struct ipv6_devconf *p)
5359 struct addrconf_sysctl_table *t;
5360 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5362 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5366 for (i = 0; t->addrconf_vars[i].data; i++) {
5367 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5368 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5369 t->addrconf_vars[i].extra2 = net;
5372 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5374 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5375 if (t->sysctl_header == NULL)
5387 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5389 struct addrconf_sysctl_table *t;
5391 if (p->sysctl == NULL)
5396 unregister_net_sysctl_table(t->sysctl_header);
5400 static int addrconf_sysctl_register(struct inet6_dev *idev)
5404 if (!sysctl_dev_name_is_allowed(idev->dev->name))
5407 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5408 &ndisc_ifinfo_sysctl_change);
5411 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5414 neigh_sysctl_unregister(idev->nd_parms);
5419 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5421 __addrconf_sysctl_unregister(&idev->cnf);
5422 neigh_sysctl_unregister(idev->nd_parms);
5428 static int __net_init addrconf_init_net(struct net *net)
5431 struct ipv6_devconf *all, *dflt;
5433 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5437 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5439 goto err_alloc_dflt;
5441 /* these will be inherited by all namespaces */
5442 dflt->autoconf = ipv6_defaults.autoconf;
5443 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5445 net->ipv6.devconf_all = all;
5446 net->ipv6.devconf_dflt = dflt;
5448 #ifdef CONFIG_SYSCTL
5449 err = __addrconf_sysctl_register(net, "all", NULL, all);
5453 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5459 #ifdef CONFIG_SYSCTL
5461 __addrconf_sysctl_unregister(all);
5471 static void __net_exit addrconf_exit_net(struct net *net)
5473 #ifdef CONFIG_SYSCTL
5474 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5475 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5477 kfree(net->ipv6.devconf_dflt);
5478 kfree(net->ipv6.devconf_all);
5481 static struct pernet_operations addrconf_ops = {
5482 .init = addrconf_init_net,
5483 .exit = addrconf_exit_net,
5486 static struct rtnl_af_ops inet6_ops __read_mostly = {
5488 .fill_link_af = inet6_fill_link_af,
5489 .get_link_af_size = inet6_get_link_af_size,
5490 .validate_link_af = inet6_validate_link_af,
5491 .set_link_af = inet6_set_link_af,
5495 * Init / cleanup code
5498 int __init addrconf_init(void)
5500 struct inet6_dev *idev;
5503 err = ipv6_addr_label_init();
5505 pr_crit("%s: cannot initialize default policy table: %d\n",
5510 err = register_pernet_subsys(&addrconf_ops);
5514 addrconf_wq = create_workqueue("ipv6_addrconf");
5520 /* The addrconf netdev notifier requires that loopback_dev
5521 * has it's ipv6 private information allocated and setup
5522 * before it can bring up and give link-local addresses
5523 * to other devices which are up.
5525 * Unfortunately, loopback_dev is not necessarily the first
5526 * entry in the global dev_base list of net devices. In fact,
5527 * it is likely to be the very last entry on that list.
5528 * So this causes the notifier registry below to try and
5529 * give link-local addresses to all devices besides loopback_dev
5530 * first, then loopback_dev, which cases all the non-loopback_dev
5531 * devices to fail to get a link-local address.
5533 * So, as a temporary fix, allocate the ipv6 structure for
5534 * loopback_dev first by hand.
5535 * Longer term, all of the dependencies ipv6 has upon the loopback
5536 * device and it being up should be removed.
5539 idev = ipv6_add_dev(init_net.loopback_dev);
5542 err = PTR_ERR(idev);
5546 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5547 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5549 register_netdevice_notifier(&ipv6_dev_notf);
5553 rtnl_af_register(&inet6_ops);
5555 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5560 /* Only the first call to __rtnl_register can fail */
5561 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5562 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5563 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5564 inet6_dump_ifaddr, NULL);
5565 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5566 inet6_dump_ifmcaddr, NULL);
5567 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5568 inet6_dump_ifacaddr, NULL);
5569 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5570 inet6_netconf_dump_devconf, NULL);
5572 ipv6_addr_label_rtnl_register();
5576 rtnl_af_unregister(&inet6_ops);
5577 unregister_netdevice_notifier(&ipv6_dev_notf);
5579 destroy_workqueue(addrconf_wq);
5581 unregister_pernet_subsys(&addrconf_ops);
5583 ipv6_addr_label_cleanup();
5588 void addrconf_cleanup(void)
5590 struct net_device *dev;
5593 unregister_netdevice_notifier(&ipv6_dev_notf);
5594 unregister_pernet_subsys(&addrconf_ops);
5595 ipv6_addr_label_cleanup();
5599 __rtnl_af_unregister(&inet6_ops);
5601 /* clean dev list */
5602 for_each_netdev(&init_net, dev) {
5603 if (__in6_dev_get(dev) == NULL)
5605 addrconf_ifdown(dev, 1);
5607 addrconf_ifdown(init_net.loopback_dev, 2);
5612 spin_lock_bh(&addrconf_hash_lock);
5613 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5614 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5615 spin_unlock_bh(&addrconf_hash_lock);
5616 cancel_delayed_work(&addr_chk_work);
5619 destroy_workqueue(addrconf_wq);