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>
87 #ifdef CONFIG_IPV6_PRIVACY
88 #include <linux/random.h>
91 #include <linux/uaccess.h>
92 #include <asm/unaligned.h>
94 #include <linux/proc_fs.h>
95 #include <linux/seq_file.h>
96 #include <linux/export.h>
98 /* Set to 3 to get tracing... */
102 #define ADBG(x) printk x
107 #define INFINITY_LIFE_TIME 0xFFFFFFFF
109 static inline u32 cstamp_delta(unsigned long cstamp)
111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
115 static void addrconf_sysctl_register(struct inet6_dev *idev);
116 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
118 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
122 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
127 #ifdef CONFIG_IPV6_PRIVACY
128 static void __ipv6_regen_rndid(struct inet6_dev *idev);
129 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
130 static void ipv6_regen_rndid(unsigned long data);
133 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
134 static int ipv6_count_addresses(struct inet6_dev *idev);
137 * Configured unicast address hash table
139 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
140 static DEFINE_SPINLOCK(addrconf_hash_lock);
142 static void addrconf_verify(unsigned long);
144 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
145 static DEFINE_SPINLOCK(addrconf_verify_lock);
147 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
148 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
150 static void addrconf_type_change(struct net_device *dev,
151 unsigned long event);
152 static int addrconf_ifdown(struct net_device *dev, int how);
154 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
156 const struct net_device *dev,
157 u32 flags, u32 noflags);
159 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
160 static void addrconf_dad_timer(unsigned long data);
161 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
162 static void addrconf_dad_run(struct inet6_dev *idev);
163 static void addrconf_rs_timer(unsigned long data);
164 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
165 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
167 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
168 struct prefix_info *pinfo);
169 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
170 struct net_device *dev);
172 static struct ipv6_devconf ipv6_devconf __read_mostly = {
174 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
175 .mtu6 = IPV6_MIN_MTU,
177 .accept_redirects = 1,
179 .force_mld_version = 0,
181 .rtr_solicits = MAX_RTR_SOLICITATIONS,
182 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
183 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
184 #ifdef CONFIG_IPV6_PRIVACY
186 .temp_valid_lft = TEMP_VALID_LIFETIME,
187 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
188 .regen_max_retry = REGEN_MAX_RETRY,
189 .max_desync_factor = MAX_DESYNC_FACTOR,
191 .max_addresses = IPV6_MAX_ADDRESSES,
192 .accept_ra_defrtr = 1,
193 .accept_ra_pinfo = 1,
194 #ifdef CONFIG_IPV6_ROUTER_PREF
195 .accept_ra_rtr_pref = 1,
196 .rtr_probe_interval = 60 * HZ,
197 #ifdef CONFIG_IPV6_ROUTE_INFO
198 .accept_ra_rt_info_max_plen = 0,
202 .accept_source_route = 0, /* we do not accept RH0 by default. */
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,
215 .rtr_solicits = MAX_RTR_SOLICITATIONS,
216 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
217 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
218 #ifdef CONFIG_IPV6_PRIVACY
220 .temp_valid_lft = TEMP_VALID_LIFETIME,
221 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
222 .regen_max_retry = REGEN_MAX_RETRY,
223 .max_desync_factor = MAX_DESYNC_FACTOR,
225 .max_addresses = IPV6_MAX_ADDRESSES,
226 .accept_ra_defrtr = 1,
227 .accept_ra_pinfo = 1,
228 #ifdef CONFIG_IPV6_ROUTER_PREF
229 .accept_ra_rtr_pref = 1,
230 .rtr_probe_interval = 60 * HZ,
231 #ifdef CONFIG_IPV6_ROUTE_INFO
232 .accept_ra_rt_info_max_plen = 0,
236 .accept_source_route = 0, /* we do not accept RH0 by default. */
241 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
242 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
243 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
244 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
245 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
246 const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
247 const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
248 const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
250 /* Check if a valid qdisc is available */
251 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
253 return !qdisc_tx_is_noop(dev);
256 static void addrconf_del_rs_timer(struct inet6_dev *idev)
258 if (del_timer(&idev->rs_timer))
262 static void addrconf_del_dad_timer(struct inet6_ifaddr *ifp)
264 if (del_timer(&ifp->dad_timer))
268 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
271 if (!timer_pending(&idev->rs_timer))
273 mod_timer(&idev->rs_timer, jiffies + when);
276 static void addrconf_mod_dad_timer(struct inet6_ifaddr *ifp,
279 if (!timer_pending(&ifp->dad_timer))
281 mod_timer(&ifp->dad_timer, jiffies + when);
284 static int snmp6_alloc_dev(struct inet6_dev *idev)
286 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
287 sizeof(struct ipstats_mib),
288 __alignof__(struct ipstats_mib)) < 0)
290 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
292 if (!idev->stats.icmpv6dev)
294 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
296 if (!idev->stats.icmpv6msgdev)
302 kfree(idev->stats.icmpv6dev);
304 snmp_mib_free((void __percpu **)idev->stats.ipv6);
309 static void snmp6_free_dev(struct inet6_dev *idev)
311 kfree(idev->stats.icmpv6msgdev);
312 kfree(idev->stats.icmpv6dev);
313 snmp_mib_free((void __percpu **)idev->stats.ipv6);
316 /* Nobody refers to this device, we may destroy it. */
318 void in6_dev_finish_destroy(struct inet6_dev *idev)
320 struct net_device *dev = idev->dev;
322 WARN_ON(!list_empty(&idev->addr_list));
323 WARN_ON(idev->mc_list != NULL);
324 WARN_ON(timer_pending(&idev->rs_timer));
326 #ifdef NET_REFCNT_DEBUG
327 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
331 pr_warn("Freeing alive inet6 device %p\n", idev);
334 snmp6_free_dev(idev);
335 kfree_rcu(idev, rcu);
337 EXPORT_SYMBOL(in6_dev_finish_destroy);
339 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
341 struct inet6_dev *ndev;
345 if (dev->mtu < IPV6_MIN_MTU)
348 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
353 rwlock_init(&ndev->lock);
355 INIT_LIST_HEAD(&ndev->addr_list);
356 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
357 (unsigned long)ndev);
358 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
359 ndev->cnf.mtu6 = dev->mtu;
360 ndev->cnf.sysctl = NULL;
361 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
362 if (ndev->nd_parms == NULL) {
366 if (ndev->cnf.forwarding)
367 dev_disable_lro(dev);
368 /* We refer to the device */
371 if (snmp6_alloc_dev(ndev) < 0) {
373 "%s: cannot allocate memory for statistics; dev=%s.\n",
374 __func__, dev->name));
375 neigh_parms_release(&nd_tbl, ndev->nd_parms);
381 if (snmp6_register_dev(ndev) < 0) {
383 "%s: cannot create /proc/net/dev_snmp6/%s\n",
384 __func__, dev->name));
385 neigh_parms_release(&nd_tbl, ndev->nd_parms);
387 in6_dev_finish_destroy(ndev);
391 /* One reference from device. We must do this before
392 * we invoke __ipv6_regen_rndid().
396 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
397 ndev->cnf.accept_dad = -1;
399 #if IS_ENABLED(CONFIG_IPV6_SIT)
400 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
401 pr_info("%s: Disabled Multicast RS\n", dev->name);
402 ndev->cnf.rtr_solicits = 0;
406 #ifdef CONFIG_IPV6_PRIVACY
407 INIT_LIST_HEAD(&ndev->tempaddr_list);
408 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
409 if ((dev->flags&IFF_LOOPBACK) ||
410 dev->type == ARPHRD_TUNNEL ||
411 dev->type == ARPHRD_TUNNEL6 ||
412 dev->type == ARPHRD_SIT ||
413 dev->type == ARPHRD_NONE) {
414 ndev->cnf.use_tempaddr = -1;
417 ipv6_regen_rndid((unsigned long) ndev);
420 ndev->token = in6addr_any;
422 if (netif_running(dev) && addrconf_qdisc_ok(dev))
423 ndev->if_flags |= IF_READY;
425 ipv6_mc_init_dev(ndev);
426 ndev->tstamp = jiffies;
427 addrconf_sysctl_register(ndev);
428 /* protected by rtnl_lock */
429 rcu_assign_pointer(dev->ip6_ptr, ndev);
431 /* Join interface-local all-node multicast group */
432 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
434 /* Join all-node multicast group */
435 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
437 /* Join all-router multicast group if forwarding is set */
438 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
439 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
444 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
446 struct inet6_dev *idev;
450 idev = __in6_dev_get(dev);
452 idev = ipv6_add_dev(dev);
457 if (dev->flags&IFF_UP)
462 static int inet6_netconf_msgsize_devconf(int type)
464 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
465 + nla_total_size(4); /* NETCONFA_IFINDEX */
467 /* type -1 is used for ALL */
468 if (type == -1 || type == NETCONFA_FORWARDING)
469 size += nla_total_size(4);
470 #ifdef CONFIG_IPV6_MROUTE
471 if (type == -1 || type == NETCONFA_MC_FORWARDING)
472 size += nla_total_size(4);
478 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
479 struct ipv6_devconf *devconf, u32 portid,
480 u32 seq, int event, unsigned int flags,
483 struct nlmsghdr *nlh;
484 struct netconfmsg *ncm;
486 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
491 ncm = nlmsg_data(nlh);
492 ncm->ncm_family = AF_INET6;
494 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
495 goto nla_put_failure;
497 /* type -1 is used for ALL */
498 if ((type == -1 || type == NETCONFA_FORWARDING) &&
499 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
500 goto nla_put_failure;
501 #ifdef CONFIG_IPV6_MROUTE
502 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
503 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
504 devconf->mc_forwarding) < 0)
505 goto nla_put_failure;
507 return nlmsg_end(skb, nlh);
510 nlmsg_cancel(skb, nlh);
514 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
515 struct ipv6_devconf *devconf)
520 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
524 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
525 RTM_NEWNETCONF, 0, type);
527 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
528 WARN_ON(err == -EMSGSIZE);
532 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
535 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
538 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
539 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
540 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
543 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
544 struct nlmsghdr *nlh)
546 struct net *net = sock_net(in_skb->sk);
547 struct nlattr *tb[NETCONFA_MAX+1];
548 struct netconfmsg *ncm;
550 struct ipv6_devconf *devconf;
551 struct inet6_dev *in6_dev;
552 struct net_device *dev;
556 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
557 devconf_ipv6_policy);
562 if (!tb[NETCONFA_IFINDEX])
565 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
567 case NETCONFA_IFINDEX_ALL:
568 devconf = net->ipv6.devconf_all;
570 case NETCONFA_IFINDEX_DEFAULT:
571 devconf = net->ipv6.devconf_dflt;
574 dev = __dev_get_by_index(net, ifindex);
577 in6_dev = __in6_dev_get(dev);
580 devconf = &in6_dev->cnf;
585 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
589 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
590 NETLINK_CB(in_skb).portid,
591 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
594 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
595 WARN_ON(err == -EMSGSIZE);
599 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
604 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
605 struct netlink_callback *cb)
607 struct net *net = sock_net(skb->sk);
610 struct net_device *dev;
611 struct inet6_dev *idev;
612 struct hlist_head *head;
615 s_idx = idx = cb->args[1];
617 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
619 head = &net->dev_index_head[h];
621 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
623 hlist_for_each_entry_rcu(dev, head, index_hlist) {
626 idev = __in6_dev_get(dev);
630 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
632 NETLINK_CB(cb->skb).portid,
640 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
646 if (h == NETDEV_HASHENTRIES) {
647 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
648 net->ipv6.devconf_all,
649 NETLINK_CB(cb->skb).portid,
651 RTM_NEWNETCONF, NLM_F_MULTI,
657 if (h == NETDEV_HASHENTRIES + 1) {
658 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
659 net->ipv6.devconf_dflt,
660 NETLINK_CB(cb->skb).portid,
662 RTM_NEWNETCONF, NLM_F_MULTI,
676 static void dev_forward_change(struct inet6_dev *idev)
678 struct net_device *dev;
679 struct inet6_ifaddr *ifa;
684 if (idev->cnf.forwarding)
685 dev_disable_lro(dev);
686 if (dev->flags & IFF_MULTICAST) {
687 if (idev->cnf.forwarding) {
688 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
689 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
690 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
692 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
693 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
694 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
698 list_for_each_entry(ifa, &idev->addr_list, if_list) {
699 if (ifa->flags&IFA_F_TENTATIVE)
701 if (idev->cnf.forwarding)
702 addrconf_join_anycast(ifa);
704 addrconf_leave_anycast(ifa);
706 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
707 dev->ifindex, &idev->cnf);
711 static void addrconf_forward_change(struct net *net, __s32 newf)
713 struct net_device *dev;
714 struct inet6_dev *idev;
716 for_each_netdev(net, dev) {
717 idev = __in6_dev_get(dev);
719 int changed = (!idev->cnf.forwarding) ^ (!newf);
720 idev->cnf.forwarding = newf;
722 dev_forward_change(idev);
727 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
733 return restart_syscall();
735 net = (struct net *)table->extra2;
739 if (p == &net->ipv6.devconf_dflt->forwarding) {
740 if ((!newf) ^ (!old))
741 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
742 NETCONFA_IFINDEX_DEFAULT,
743 net->ipv6.devconf_dflt);
748 if (p == &net->ipv6.devconf_all->forwarding) {
749 net->ipv6.devconf_dflt->forwarding = newf;
750 addrconf_forward_change(net, newf);
751 if ((!newf) ^ (!old))
752 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
753 NETCONFA_IFINDEX_ALL,
754 net->ipv6.devconf_all);
755 } else if ((!newf) ^ (!old))
756 dev_forward_change((struct inet6_dev *)table->extra1);
760 rt6_purge_dflt_routers(net);
765 /* Nobody refers to this ifaddr, destroy it */
766 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
768 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
770 #ifdef NET_REFCNT_DEBUG
771 pr_debug("%s\n", __func__);
774 in6_dev_put(ifp->idev);
776 if (del_timer(&ifp->dad_timer))
777 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
779 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
780 pr_warn("Freeing alive inet6 address %p\n", ifp);
789 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
792 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
795 * Each device address list is sorted in order of scope -
796 * global before linklocal.
798 list_for_each(p, &idev->addr_list) {
799 struct inet6_ifaddr *ifa
800 = list_entry(p, struct inet6_ifaddr, if_list);
801 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
805 list_add_tail(&ifp->if_list, p);
808 static u32 inet6_addr_hash(const struct in6_addr *addr)
810 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
813 /* On success it returns ifp with increased reference count */
815 static struct inet6_ifaddr *
816 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
817 const struct in6_addr *peer_addr, int pfxlen,
818 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
820 struct inet6_ifaddr *ifa = NULL;
824 int addr_type = ipv6_addr_type(addr);
826 if (addr_type == IPV6_ADDR_ANY ||
827 addr_type & IPV6_ADDR_MULTICAST ||
828 (!(idev->dev->flags & IFF_LOOPBACK) &&
829 addr_type & IPV6_ADDR_LOOPBACK))
830 return ERR_PTR(-EADDRNOTAVAIL);
834 err = -ENODEV; /*XXX*/
838 if (idev->cnf.disable_ipv6) {
843 spin_lock(&addrconf_hash_lock);
845 /* Ignore adding duplicate addresses on an interface */
846 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
847 ADBG(("ipv6_add_addr: already assigned\n"));
852 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
855 ADBG(("ipv6_add_addr: malloc failed\n"));
860 rt = addrconf_dst_alloc(idev, addr, false);
868 ifa->peer_addr = *peer_addr;
870 spin_lock_init(&ifa->lock);
871 spin_lock_init(&ifa->state_lock);
872 setup_timer(&ifa->dad_timer, addrconf_dad_timer,
874 INIT_HLIST_NODE(&ifa->addr_lst);
876 ifa->prefix_len = pfxlen;
877 ifa->flags = flags | IFA_F_TENTATIVE;
878 ifa->valid_lft = valid_lft;
879 ifa->prefered_lft = prefered_lft;
880 ifa->cstamp = ifa->tstamp = jiffies;
881 ifa->tokenized = false;
890 /* Add to big hash table */
891 hash = inet6_addr_hash(addr);
893 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
894 spin_unlock(&addrconf_hash_lock);
896 write_lock(&idev->lock);
897 /* Add to inet6_dev unicast addr list. */
898 ipv6_link_dev_addr(idev, ifa);
900 #ifdef CONFIG_IPV6_PRIVACY
901 if (ifa->flags&IFA_F_TEMPORARY) {
902 list_add(&ifa->tmp_list, &idev->tempaddr_list);
908 write_unlock(&idev->lock);
910 rcu_read_unlock_bh();
912 if (likely(err == 0))
913 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
921 spin_unlock(&addrconf_hash_lock);
925 /* This function wants to get referenced ifp and releases it before return */
927 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
929 struct inet6_ifaddr *ifa, *ifn;
930 struct inet6_dev *idev = ifp->idev;
932 int deleted = 0, onlink = 0;
933 unsigned long expires = jiffies;
935 spin_lock_bh(&ifp->state_lock);
937 ifp->state = INET6_IFADDR_STATE_DEAD;
938 spin_unlock_bh(&ifp->state_lock);
940 if (state == INET6_IFADDR_STATE_DEAD)
943 spin_lock_bh(&addrconf_hash_lock);
944 hlist_del_init_rcu(&ifp->addr_lst);
945 spin_unlock_bh(&addrconf_hash_lock);
947 write_lock_bh(&idev->lock);
948 #ifdef CONFIG_IPV6_PRIVACY
949 if (ifp->flags&IFA_F_TEMPORARY) {
950 list_del(&ifp->tmp_list);
952 in6_ifa_put(ifp->ifpub);
959 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
961 list_del_init(&ifp->if_list);
964 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
968 } else if (ifp->flags & IFA_F_PERMANENT) {
969 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
971 if (ifa->flags & IFA_F_PERMANENT) {
976 unsigned long lifetime;
981 spin_lock(&ifa->lock);
983 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
985 * Note: Because this address is
986 * not permanent, lifetime <
987 * LONG_MAX / HZ here.
989 if (time_before(expires,
990 ifa->tstamp + lifetime * HZ))
991 expires = ifa->tstamp + lifetime * HZ;
992 spin_unlock(&ifa->lock);
997 write_unlock_bh(&idev->lock);
999 addrconf_del_dad_timer(ifp);
1001 ipv6_ifa_notify(RTM_DELADDR, ifp);
1003 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1006 * Purge or update corresponding prefix
1008 * 1) we don't purge prefix here if address was not permanent.
1009 * prefix is managed by its own lifetime.
1010 * 2) if there're no addresses, delete prefix.
1011 * 3) if there're still other permanent address(es),
1012 * corresponding prefix is still permanent.
1013 * 4) otherwise, update prefix lifetime to the
1014 * longest valid lifetime among the corresponding
1015 * addresses on the device.
1016 * Note: subsequent RA will update lifetime.
1020 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
1021 struct in6_addr prefix;
1022 struct rt6_info *rt;
1024 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
1026 rt = addrconf_get_prefix_route(&prefix,
1029 0, RTF_GATEWAY | RTF_DEFAULT);
1035 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1036 rt6_set_expires(rt, expires);
1042 /* clean up prefsrc entries */
1043 rt6_remove_prefsrc(ifp);
1048 #ifdef CONFIG_IPV6_PRIVACY
1049 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1051 struct inet6_dev *idev = ifp->idev;
1052 struct in6_addr addr, *tmpaddr;
1053 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1054 unsigned long regen_advance;
1059 unsigned long now = jiffies;
1061 write_lock(&idev->lock);
1063 spin_lock_bh(&ift->lock);
1064 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1065 spin_unlock_bh(&ift->lock);
1072 if (idev->cnf.use_tempaddr <= 0) {
1073 write_unlock(&idev->lock);
1074 pr_info("%s: use_tempaddr is disabled\n", __func__);
1079 spin_lock_bh(&ifp->lock);
1080 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1081 idev->cnf.use_tempaddr = -1; /*XXX*/
1082 spin_unlock_bh(&ifp->lock);
1083 write_unlock(&idev->lock);
1084 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1091 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1092 __ipv6_try_regen_rndid(idev, tmpaddr);
1093 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1094 age = (now - ifp->tstamp) / HZ;
1095 tmp_valid_lft = min_t(__u32,
1097 idev->cnf.temp_valid_lft + age);
1098 tmp_prefered_lft = min_t(__u32,
1100 idev->cnf.temp_prefered_lft + age -
1101 idev->cnf.max_desync_factor);
1102 tmp_plen = ifp->prefix_len;
1103 max_addresses = idev->cnf.max_addresses;
1104 tmp_tstamp = ifp->tstamp;
1105 spin_unlock_bh(&ifp->lock);
1107 regen_advance = idev->cnf.regen_max_retry *
1108 idev->cnf.dad_transmits *
1109 idev->nd_parms->retrans_time / HZ;
1110 write_unlock(&idev->lock);
1112 /* A temporary address is created only if this calculated Preferred
1113 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1114 * an implementation must not create a temporary address with a zero
1115 * Preferred Lifetime.
1117 if (tmp_prefered_lft <= regen_advance) {
1124 addr_flags = IFA_F_TEMPORARY;
1125 /* set in addrconf_prefix_rcv() */
1126 if (ifp->flags & IFA_F_OPTIMISTIC)
1127 addr_flags |= IFA_F_OPTIMISTIC;
1129 ift = !max_addresses ||
1130 ipv6_count_addresses(idev) < max_addresses ?
1131 ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1132 ipv6_addr_scope(&addr), addr_flags,
1133 tmp_valid_lft, tmp_prefered_lft) : NULL;
1134 if (IS_ERR_OR_NULL(ift)) {
1137 pr_info("%s: retry temporary address regeneration\n", __func__);
1139 write_lock(&idev->lock);
1143 spin_lock_bh(&ift->lock);
1146 ift->tstamp = tmp_tstamp;
1147 spin_unlock_bh(&ift->lock);
1149 addrconf_dad_start(ift);
1158 * Choose an appropriate source address (RFC3484)
1161 IPV6_SADDR_RULE_INIT = 0,
1162 IPV6_SADDR_RULE_LOCAL,
1163 IPV6_SADDR_RULE_SCOPE,
1164 IPV6_SADDR_RULE_PREFERRED,
1165 #ifdef CONFIG_IPV6_MIP6
1166 IPV6_SADDR_RULE_HOA,
1168 IPV6_SADDR_RULE_OIF,
1169 IPV6_SADDR_RULE_LABEL,
1170 #ifdef CONFIG_IPV6_PRIVACY
1171 IPV6_SADDR_RULE_PRIVACY,
1173 IPV6_SADDR_RULE_ORCHID,
1174 IPV6_SADDR_RULE_PREFIX,
1178 struct ipv6_saddr_score {
1181 struct inet6_ifaddr *ifa;
1182 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1187 struct ipv6_saddr_dst {
1188 const struct in6_addr *addr;
1195 static inline int ipv6_saddr_preferred(int type)
1197 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1202 static int ipv6_get_saddr_eval(struct net *net,
1203 struct ipv6_saddr_score *score,
1204 struct ipv6_saddr_dst *dst,
1209 if (i <= score->rule) {
1211 case IPV6_SADDR_RULE_SCOPE:
1212 ret = score->scopedist;
1214 case IPV6_SADDR_RULE_PREFIX:
1215 ret = score->matchlen;
1218 ret = !!test_bit(i, score->scorebits);
1224 case IPV6_SADDR_RULE_INIT:
1225 /* Rule 0: remember if hiscore is not ready yet */
1228 case IPV6_SADDR_RULE_LOCAL:
1229 /* Rule 1: Prefer same address */
1230 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1232 case IPV6_SADDR_RULE_SCOPE:
1233 /* Rule 2: Prefer appropriate scope
1238 * ---+--+-+---> scope
1240 * | d is scope of the destination.
1242 * | \ <- smaller scope is better if
1243 * B-15 | \ if scope is enough for destinaion.
1244 * | ret = B - scope (-1 <= scope >= d <= 15).
1246 * |/ <- greater is better
1247 * -C / if scope is not enough for destination.
1248 * /| ret = scope - C (-1 <= d < scope <= 15).
1250 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1251 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1252 * Assume B = 0 and we get C > 29.
1254 ret = __ipv6_addr_src_scope(score->addr_type);
1255 if (ret >= dst->scope)
1258 ret -= 128; /* 30 is enough */
1259 score->scopedist = ret;
1261 case IPV6_SADDR_RULE_PREFERRED:
1262 /* Rule 3: Avoid deprecated and optimistic addresses */
1263 ret = ipv6_saddr_preferred(score->addr_type) ||
1264 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1266 #ifdef CONFIG_IPV6_MIP6
1267 case IPV6_SADDR_RULE_HOA:
1269 /* Rule 4: Prefer home address */
1270 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1271 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1275 case IPV6_SADDR_RULE_OIF:
1276 /* Rule 5: Prefer outgoing interface */
1277 ret = (!dst->ifindex ||
1278 dst->ifindex == score->ifa->idev->dev->ifindex);
1280 case IPV6_SADDR_RULE_LABEL:
1281 /* Rule 6: Prefer matching label */
1282 ret = ipv6_addr_label(net,
1283 &score->ifa->addr, score->addr_type,
1284 score->ifa->idev->dev->ifindex) == dst->label;
1286 #ifdef CONFIG_IPV6_PRIVACY
1287 case IPV6_SADDR_RULE_PRIVACY:
1289 /* Rule 7: Prefer public address
1290 * Note: prefer temporary address if use_tempaddr >= 2
1292 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1293 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1294 score->ifa->idev->cnf.use_tempaddr >= 2;
1295 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1299 case IPV6_SADDR_RULE_ORCHID:
1300 /* Rule 8-: Prefer ORCHID vs ORCHID or
1301 * non-ORCHID vs non-ORCHID
1303 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1304 ipv6_addr_orchid(dst->addr));
1306 case IPV6_SADDR_RULE_PREFIX:
1307 /* Rule 8: Use longest matching prefix */
1308 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1309 if (ret > score->ifa->prefix_len)
1310 ret = score->ifa->prefix_len;
1311 score->matchlen = ret;
1318 __set_bit(i, score->scorebits);
1324 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1325 const struct in6_addr *daddr, unsigned int prefs,
1326 struct in6_addr *saddr)
1328 struct ipv6_saddr_score scores[2],
1329 *score = &scores[0], *hiscore = &scores[1];
1330 struct ipv6_saddr_dst dst;
1331 struct net_device *dev;
1334 dst_type = __ipv6_addr_type(daddr);
1336 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1337 dst.scope = __ipv6_addr_src_scope(dst_type);
1338 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1342 hiscore->ifa = NULL;
1346 for_each_netdev_rcu(net, dev) {
1347 struct inet6_dev *idev;
1349 /* Candidate Source Address (section 4)
1350 * - multicast and link-local destination address,
1351 * the set of candidate source address MUST only
1352 * include addresses assigned to interfaces
1353 * belonging to the same link as the outgoing
1355 * (- For site-local destination addresses, the
1356 * set of candidate source addresses MUST only
1357 * include addresses assigned to interfaces
1358 * belonging to the same site as the outgoing
1361 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1362 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1363 dst.ifindex && dev->ifindex != dst.ifindex)
1366 idev = __in6_dev_get(dev);
1370 read_lock_bh(&idev->lock);
1371 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1375 * - Tentative Address (RFC2462 section 5.4)
1376 * - A tentative address is not considered
1377 * "assigned to an interface" in the traditional
1378 * sense, unless it is also flagged as optimistic.
1379 * - Candidate Source Address (section 4)
1380 * - In any case, anycast addresses, multicast
1381 * addresses, and the unspecified address MUST
1382 * NOT be included in a candidate set.
1384 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1385 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1388 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1390 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1391 score->addr_type & IPV6_ADDR_MULTICAST)) {
1392 LIMIT_NETDEBUG(KERN_DEBUG
1393 "ADDRCONF: unspecified / multicast address "
1394 "assigned as unicast address on %s",
1400 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1402 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1403 int minihiscore, miniscore;
1405 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1406 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1408 if (minihiscore > miniscore) {
1409 if (i == IPV6_SADDR_RULE_SCOPE &&
1410 score->scopedist > 0) {
1413 * each remaining entry
1414 * has too small (not enough)
1415 * scope, because ifa entries
1416 * are sorted by their scope
1422 } else if (minihiscore < miniscore) {
1424 in6_ifa_put(hiscore->ifa);
1426 in6_ifa_hold(score->ifa);
1428 swap(hiscore, score);
1430 /* restore our iterator */
1431 score->ifa = hiscore->ifa;
1438 read_unlock_bh(&idev->lock);
1443 return -EADDRNOTAVAIL;
1445 *saddr = hiscore->ifa->addr;
1446 in6_ifa_put(hiscore->ifa);
1449 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1451 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1452 unsigned char banned_flags)
1454 struct inet6_ifaddr *ifp;
1455 int err = -EADDRNOTAVAIL;
1457 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1458 if (ifp->scope == IFA_LINK &&
1459 !(ifp->flags & banned_flags)) {
1468 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1469 unsigned char banned_flags)
1471 struct inet6_dev *idev;
1472 int err = -EADDRNOTAVAIL;
1475 idev = __in6_dev_get(dev);
1477 read_lock_bh(&idev->lock);
1478 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1479 read_unlock_bh(&idev->lock);
1485 static int ipv6_count_addresses(struct inet6_dev *idev)
1488 struct inet6_ifaddr *ifp;
1490 read_lock_bh(&idev->lock);
1491 list_for_each_entry(ifp, &idev->addr_list, if_list)
1493 read_unlock_bh(&idev->lock);
1497 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1498 const struct net_device *dev, int strict)
1500 struct inet6_ifaddr *ifp;
1501 unsigned int hash = inet6_addr_hash(addr);
1504 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1505 if (!net_eq(dev_net(ifp->idev->dev), net))
1507 if (ipv6_addr_equal(&ifp->addr, addr) &&
1508 !(ifp->flags&IFA_F_TENTATIVE) &&
1509 (dev == NULL || ifp->idev->dev == dev ||
1510 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1511 rcu_read_unlock_bh();
1516 rcu_read_unlock_bh();
1519 EXPORT_SYMBOL(ipv6_chk_addr);
1521 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1522 struct net_device *dev)
1524 unsigned int hash = inet6_addr_hash(addr);
1525 struct inet6_ifaddr *ifp;
1527 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1528 if (!net_eq(dev_net(ifp->idev->dev), net))
1530 if (ipv6_addr_equal(&ifp->addr, addr)) {
1531 if (dev == NULL || ifp->idev->dev == dev)
1538 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1540 struct inet6_dev *idev;
1541 struct inet6_ifaddr *ifa;
1546 idev = __in6_dev_get(dev);
1548 read_lock_bh(&idev->lock);
1549 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1550 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1555 read_unlock_bh(&idev->lock);
1560 EXPORT_SYMBOL(ipv6_chk_prefix);
1562 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1563 struct net_device *dev, int strict)
1565 struct inet6_ifaddr *ifp, *result = NULL;
1566 unsigned int hash = inet6_addr_hash(addr);
1569 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1570 if (!net_eq(dev_net(ifp->idev->dev), net))
1572 if (ipv6_addr_equal(&ifp->addr, addr)) {
1573 if (dev == NULL || ifp->idev->dev == dev ||
1574 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1581 rcu_read_unlock_bh();
1586 /* Gets referenced address, destroys ifaddr */
1588 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1590 if (ifp->flags&IFA_F_PERMANENT) {
1591 spin_lock_bh(&ifp->lock);
1592 addrconf_del_dad_timer(ifp);
1593 ifp->flags |= IFA_F_TENTATIVE;
1595 ifp->flags |= IFA_F_DADFAILED;
1596 spin_unlock_bh(&ifp->lock);
1598 ipv6_ifa_notify(0, ifp);
1600 #ifdef CONFIG_IPV6_PRIVACY
1601 } else if (ifp->flags&IFA_F_TEMPORARY) {
1602 struct inet6_ifaddr *ifpub;
1603 spin_lock_bh(&ifp->lock);
1606 in6_ifa_hold(ifpub);
1607 spin_unlock_bh(&ifp->lock);
1608 ipv6_create_tempaddr(ifpub, ifp);
1611 spin_unlock_bh(&ifp->lock);
1619 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1623 spin_lock(&ifp->state_lock);
1624 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1625 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1628 spin_unlock(&ifp->state_lock);
1633 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1635 struct inet6_dev *idev = ifp->idev;
1637 if (addrconf_dad_end(ifp)) {
1642 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1643 ifp->idev->dev->name, &ifp->addr);
1645 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1646 struct in6_addr addr;
1648 addr.s6_addr32[0] = htonl(0xfe800000);
1649 addr.s6_addr32[1] = 0;
1651 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1652 ipv6_addr_equal(&ifp->addr, &addr)) {
1653 /* DAD failed for link-local based on MAC address */
1654 idev->cnf.disable_ipv6 = 1;
1656 pr_info("%s: IPv6 being disabled!\n",
1657 ifp->idev->dev->name);
1661 addrconf_dad_stop(ifp, 1);
1664 /* Join to solicited addr multicast group. */
1666 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1668 struct in6_addr maddr;
1670 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1673 addrconf_addr_solict_mult(addr, &maddr);
1674 ipv6_dev_mc_inc(dev, &maddr);
1677 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1679 struct in6_addr maddr;
1681 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1684 addrconf_addr_solict_mult(addr, &maddr);
1685 __ipv6_dev_mc_dec(idev, &maddr);
1688 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1690 struct in6_addr addr;
1691 if (ifp->prefix_len == 127) /* RFC 6164 */
1693 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1694 if (ipv6_addr_any(&addr))
1696 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1699 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1701 struct in6_addr addr;
1702 if (ifp->prefix_len == 127) /* RFC 6164 */
1704 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1705 if (ipv6_addr_any(&addr))
1707 __ipv6_dev_ac_dec(ifp->idev, &addr);
1710 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1712 if (dev->addr_len != ETH_ALEN)
1714 memcpy(eui, dev->dev_addr, 3);
1715 memcpy(eui + 5, dev->dev_addr + 3, 3);
1718 * The zSeries OSA network cards can be shared among various
1719 * OS instances, but the OSA cards have only one MAC address.
1720 * This leads to duplicate address conflicts in conjunction
1721 * with IPv6 if more than one instance uses the same card.
1723 * The driver for these cards can deliver a unique 16-bit
1724 * identifier for each instance sharing the same card. It is
1725 * placed instead of 0xFFFE in the interface identifier. The
1726 * "u" bit of the interface identifier is not inverted in this
1727 * case. Hence the resulting interface identifier has local
1728 * scope according to RFC2373.
1731 eui[3] = (dev->dev_id >> 8) & 0xFF;
1732 eui[4] = dev->dev_id & 0xFF;
1741 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1743 if (dev->addr_len != IEEE802154_ADDR_LEN)
1745 memcpy(eui, dev->dev_addr, 8);
1750 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1752 union fwnet_hwaddr *ha;
1754 if (dev->addr_len != FWNET_ALEN)
1757 ha = (union fwnet_hwaddr *)dev->dev_addr;
1759 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1764 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1766 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1767 if (dev->addr_len != ARCNET_ALEN)
1770 eui[7] = *(u8 *)dev->dev_addr;
1774 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1776 if (dev->addr_len != INFINIBAND_ALEN)
1778 memcpy(eui, dev->dev_addr + 12, 8);
1783 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1787 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1788 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1789 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1790 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1791 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1792 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1796 memcpy(eui + 4, &addr, 4);
1800 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1802 if (dev->priv_flags & IFF_ISATAP)
1803 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1807 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1809 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1812 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1814 switch (dev->type) {
1817 return addrconf_ifid_eui48(eui, dev);
1819 return addrconf_ifid_arcnet(eui, dev);
1820 case ARPHRD_INFINIBAND:
1821 return addrconf_ifid_infiniband(eui, dev);
1823 return addrconf_ifid_sit(eui, dev);
1825 return addrconf_ifid_gre(eui, dev);
1826 case ARPHRD_IEEE802154:
1827 return addrconf_ifid_eui64(eui, dev);
1828 case ARPHRD_IEEE1394:
1829 return addrconf_ifid_ieee1394(eui, dev);
1834 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1837 struct inet6_ifaddr *ifp;
1839 read_lock_bh(&idev->lock);
1840 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1841 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1842 memcpy(eui, ifp->addr.s6_addr+8, 8);
1847 read_unlock_bh(&idev->lock);
1851 #ifdef CONFIG_IPV6_PRIVACY
1852 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1853 static void __ipv6_regen_rndid(struct inet6_dev *idev)
1856 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1857 idev->rndid[0] &= ~0x02;
1860 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1861 * check if generated address is not inappropriate
1863 * - Reserved subnet anycast (RFC 2526)
1864 * 11111101 11....11 1xxxxxxx
1865 * - ISATAP (RFC4214) 6.1
1866 * 00-00-5E-FE-xx-xx-xx-xx
1868 * - XXX: already assigned to an address on the device
1870 if (idev->rndid[0] == 0xfd &&
1871 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1872 (idev->rndid[7]&0x80))
1874 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1875 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1877 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1882 static void ipv6_regen_rndid(unsigned long data)
1884 struct inet6_dev *idev = (struct inet6_dev *) data;
1885 unsigned long expires;
1888 write_lock_bh(&idev->lock);
1893 __ipv6_regen_rndid(idev);
1896 idev->cnf.temp_prefered_lft * HZ -
1897 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1898 idev->cnf.max_desync_factor * HZ;
1899 if (time_before(expires, jiffies)) {
1900 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1901 __func__, idev->dev->name);
1905 if (!mod_timer(&idev->regen_timer, expires))
1909 write_unlock_bh(&idev->lock);
1910 rcu_read_unlock_bh();
1914 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
1916 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1917 __ipv6_regen_rndid(idev);
1926 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1927 unsigned long expires, u32 flags)
1929 struct fib6_config cfg = {
1930 .fc_table = RT6_TABLE_PREFIX,
1931 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1932 .fc_ifindex = dev->ifindex,
1933 .fc_expires = expires,
1935 .fc_flags = RTF_UP | flags,
1936 .fc_nlinfo.nl_net = dev_net(dev),
1937 .fc_protocol = RTPROT_KERNEL,
1942 /* Prevent useless cloning on PtP SIT.
1943 This thing is done here expecting that the whole
1944 class of non-broadcast devices need not cloning.
1946 #if IS_ENABLED(CONFIG_IPV6_SIT)
1947 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1948 cfg.fc_flags |= RTF_NONEXTHOP;
1951 ip6_route_add(&cfg);
1955 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1957 const struct net_device *dev,
1958 u32 flags, u32 noflags)
1960 struct fib6_node *fn;
1961 struct rt6_info *rt = NULL;
1962 struct fib6_table *table;
1964 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1968 read_lock_bh(&table->tb6_lock);
1969 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1972 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1973 if (rt->dst.dev->ifindex != dev->ifindex)
1975 if ((rt->rt6i_flags & flags) != flags)
1977 if ((rt->rt6i_flags & noflags) != 0)
1983 read_unlock_bh(&table->tb6_lock);
1988 /* Create "default" multicast route to the interface */
1990 static void addrconf_add_mroute(struct net_device *dev)
1992 struct fib6_config cfg = {
1993 .fc_table = RT6_TABLE_LOCAL,
1994 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1995 .fc_ifindex = dev->ifindex,
1998 .fc_nlinfo.nl_net = dev_net(dev),
2001 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2003 ip6_route_add(&cfg);
2006 #if IS_ENABLED(CONFIG_IPV6_SIT)
2007 static void sit_route_add(struct net_device *dev)
2009 struct fib6_config cfg = {
2010 .fc_table = RT6_TABLE_MAIN,
2011 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2012 .fc_ifindex = dev->ifindex,
2014 .fc_flags = RTF_UP | RTF_NONEXTHOP,
2015 .fc_nlinfo.nl_net = dev_net(dev),
2018 /* prefix length - 96 bits "::d.d.d.d" */
2019 ip6_route_add(&cfg);
2023 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2025 struct inet6_dev *idev;
2029 idev = ipv6_find_idev(dev);
2031 return ERR_PTR(-ENOBUFS);
2033 if (idev->cnf.disable_ipv6)
2034 return ERR_PTR(-EACCES);
2036 /* Add default multicast route */
2037 if (!(dev->flags & IFF_LOOPBACK))
2038 addrconf_add_mroute(dev);
2043 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2045 struct prefix_info *pinfo;
2049 struct inet6_dev *in6_dev;
2050 struct net *net = dev_net(dev);
2052 pinfo = (struct prefix_info *) opt;
2054 if (len < sizeof(struct prefix_info)) {
2055 ADBG(("addrconf: prefix option too short\n"));
2060 * Validation checks ([ADDRCONF], page 19)
2063 addr_type = ipv6_addr_type(&pinfo->prefix);
2065 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2068 valid_lft = ntohl(pinfo->valid);
2069 prefered_lft = ntohl(pinfo->prefered);
2071 if (prefered_lft > valid_lft) {
2072 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2076 in6_dev = in6_dev_get(dev);
2078 if (in6_dev == NULL) {
2079 net_dbg_ratelimited("addrconf: device %s not configured\n",
2085 * Two things going on here:
2086 * 1) Add routes for on-link prefixes
2087 * 2) Configure prefixes with the auto flag set
2090 if (pinfo->onlink) {
2091 struct rt6_info *rt;
2092 unsigned long rt_expires;
2094 /* Avoid arithmetic overflow. Really, we could
2095 * save rt_expires in seconds, likely valid_lft,
2096 * but it would require division in fib gc, that it
2100 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2102 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2104 if (addrconf_finite_timeout(rt_expires))
2107 rt = addrconf_get_prefix_route(&pinfo->prefix,
2110 RTF_ADDRCONF | RTF_PREFIX_RT,
2111 RTF_GATEWAY | RTF_DEFAULT);
2114 /* Autoconf prefix route */
2115 if (valid_lft == 0) {
2118 } else if (addrconf_finite_timeout(rt_expires)) {
2120 rt6_set_expires(rt, jiffies + rt_expires);
2122 rt6_clean_expires(rt);
2124 } else if (valid_lft) {
2125 clock_t expires = 0;
2126 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2127 if (addrconf_finite_timeout(rt_expires)) {
2129 flags |= RTF_EXPIRES;
2130 expires = jiffies_to_clock_t(rt_expires);
2132 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2133 dev, expires, flags);
2138 /* Try to figure out our local address for this prefix */
2140 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2141 struct inet6_ifaddr *ifp;
2142 struct in6_addr addr;
2143 int create = 0, update_lft = 0;
2144 bool tokenized = false;
2146 if (pinfo->prefix_len == 64) {
2147 memcpy(&addr, &pinfo->prefix, 8);
2149 if (!ipv6_addr_any(&in6_dev->token)) {
2150 read_lock_bh(&in6_dev->lock);
2151 memcpy(addr.s6_addr + 8,
2152 in6_dev->token.s6_addr + 8, 8);
2153 read_unlock_bh(&in6_dev->lock);
2155 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2156 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2157 in6_dev_put(in6_dev);
2162 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2164 in6_dev_put(in6_dev);
2169 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2171 if (ifp == NULL && valid_lft) {
2172 int max_addresses = in6_dev->cnf.max_addresses;
2175 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2176 if (in6_dev->cnf.optimistic_dad &&
2177 !net->ipv6.devconf_all->forwarding && sllao)
2178 addr_flags = IFA_F_OPTIMISTIC;
2181 /* Do not allow to create too much of autoconfigured
2182 * addresses; this would be too easy way to crash kernel.
2184 if (!max_addresses ||
2185 ipv6_count_addresses(in6_dev) < max_addresses)
2186 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2188 addr_type&IPV6_ADDR_SCOPE_MASK,
2189 addr_flags, valid_lft,
2192 if (IS_ERR_OR_NULL(ifp)) {
2193 in6_dev_put(in6_dev);
2199 ifp->cstamp = jiffies;
2200 ifp->tokenized = tokenized;
2201 addrconf_dad_start(ifp);
2207 #ifdef CONFIG_IPV6_PRIVACY
2208 struct inet6_ifaddr *ift;
2212 /* update lifetime (RFC2462 5.5.3 e) */
2213 spin_lock(&ifp->lock);
2215 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2216 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2219 if (!update_lft && !create && stored_lft) {
2220 if (valid_lft > MIN_VALID_LIFETIME ||
2221 valid_lft > stored_lft)
2223 else if (stored_lft <= MIN_VALID_LIFETIME) {
2224 /* valid_lft <= stored_lft is always true */
2226 * RFC 4862 Section 5.5.3e:
2227 * "Note that the preferred lifetime of
2228 * the corresponding address is always
2229 * reset to the Preferred Lifetime in
2230 * the received Prefix Information
2231 * option, regardless of whether the
2232 * valid lifetime is also reset or
2235 * So if the preferred lifetime in
2236 * this advertisement is different
2237 * than what we have stored, but the
2238 * valid lifetime is invalid, just
2239 * reset prefered_lft.
2241 * We must set the valid lifetime
2242 * to the stored lifetime since we'll
2243 * be updating the timestamp below,
2244 * else we'll set it back to the
2247 if (prefered_lft != ifp->prefered_lft) {
2248 valid_lft = stored_lft;
2252 valid_lft = MIN_VALID_LIFETIME;
2253 if (valid_lft < prefered_lft)
2254 prefered_lft = valid_lft;
2260 ifp->valid_lft = valid_lft;
2261 ifp->prefered_lft = prefered_lft;
2264 ifp->flags &= ~IFA_F_DEPRECATED;
2265 spin_unlock(&ifp->lock);
2267 if (!(flags&IFA_F_TENTATIVE))
2268 ipv6_ifa_notify(0, ifp);
2270 spin_unlock(&ifp->lock);
2272 #ifdef CONFIG_IPV6_PRIVACY
2273 read_lock_bh(&in6_dev->lock);
2274 /* update all temporary addresses in the list */
2275 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2277 int age, max_valid, max_prefered;
2279 if (ifp != ift->ifpub)
2283 * RFC 4941 section 3.3:
2284 * If a received option will extend the lifetime
2285 * of a public address, the lifetimes of
2286 * temporary addresses should be extended,
2287 * subject to the overall constraint that no
2288 * temporary addresses should ever remain
2289 * "valid" or "preferred" for a time longer than
2290 * (TEMP_VALID_LIFETIME) or
2291 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2294 age = (now - ift->cstamp) / HZ;
2295 max_valid = in6_dev->cnf.temp_valid_lft - age;
2299 max_prefered = in6_dev->cnf.temp_prefered_lft -
2300 in6_dev->cnf.max_desync_factor -
2302 if (max_prefered < 0)
2305 if (valid_lft > max_valid)
2306 valid_lft = max_valid;
2308 if (prefered_lft > max_prefered)
2309 prefered_lft = max_prefered;
2311 spin_lock(&ift->lock);
2313 ift->valid_lft = valid_lft;
2314 ift->prefered_lft = prefered_lft;
2316 if (prefered_lft > 0)
2317 ift->flags &= ~IFA_F_DEPRECATED;
2319 spin_unlock(&ift->lock);
2320 if (!(flags&IFA_F_TENTATIVE))
2321 ipv6_ifa_notify(0, ift);
2324 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
2326 * When a new public address is created as
2327 * described in [ADDRCONF], also create a new
2328 * temporary address. Also create a temporary
2329 * address if it's enabled but no temporary
2330 * address currently exists.
2332 read_unlock_bh(&in6_dev->lock);
2333 ipv6_create_tempaddr(ifp, NULL);
2335 read_unlock_bh(&in6_dev->lock);
2342 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2343 in6_dev_put(in6_dev);
2347 * Set destination address.
2348 * Special case for SIT interfaces where we create a new "virtual"
2351 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2353 struct in6_ifreq ireq;
2354 struct net_device *dev;
2360 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2363 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2369 #if IS_ENABLED(CONFIG_IPV6_SIT)
2370 if (dev->type == ARPHRD_SIT) {
2371 const struct net_device_ops *ops = dev->netdev_ops;
2373 struct ip_tunnel_parm p;
2375 err = -EADDRNOTAVAIL;
2376 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2379 memset(&p, 0, sizeof(p));
2380 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2384 p.iph.protocol = IPPROTO_IPV6;
2386 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2388 if (ops->ndo_do_ioctl) {
2389 mm_segment_t oldfs = get_fs();
2392 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2399 dev = __dev_get_by_name(net, p.name);
2402 err = dev_open(dev);
2413 * Manual configuration of address on an interface
2415 static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2416 const struct in6_addr *peer_pfx,
2417 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2420 struct inet6_ifaddr *ifp;
2421 struct inet6_dev *idev;
2422 struct net_device *dev;
2426 unsigned long timeout;
2433 /* check the lifetime */
2434 if (!valid_lft || prefered_lft > valid_lft)
2437 dev = __dev_get_by_index(net, ifindex);
2441 idev = addrconf_add_dev(dev);
2443 return PTR_ERR(idev);
2445 scope = ipv6_addr_scope(pfx);
2447 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2448 if (addrconf_finite_timeout(timeout)) {
2449 expires = jiffies_to_clock_t(timeout * HZ);
2450 valid_lft = timeout;
2451 flags = RTF_EXPIRES;
2455 ifa_flags |= IFA_F_PERMANENT;
2458 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2459 if (addrconf_finite_timeout(timeout)) {
2461 ifa_flags |= IFA_F_DEPRECATED;
2462 prefered_lft = timeout;
2465 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2466 valid_lft, prefered_lft);
2469 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2472 * Note that section 3.1 of RFC 4429 indicates
2473 * that the Optimistic flag should not be set for
2474 * manually configured addresses
2476 addrconf_dad_start(ifp);
2482 return PTR_ERR(ifp);
2485 static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
2488 struct inet6_ifaddr *ifp;
2489 struct inet6_dev *idev;
2490 struct net_device *dev;
2495 dev = __dev_get_by_index(net, ifindex);
2499 if ((idev = __in6_dev_get(dev)) == NULL)
2502 read_lock_bh(&idev->lock);
2503 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2504 if (ifp->prefix_len == plen &&
2505 ipv6_addr_equal(pfx, &ifp->addr)) {
2507 read_unlock_bh(&idev->lock);
2513 read_unlock_bh(&idev->lock);
2514 return -EADDRNOTAVAIL;
2518 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2520 struct in6_ifreq ireq;
2523 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2526 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2530 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2531 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2532 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2537 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2539 struct in6_ifreq ireq;
2542 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2545 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2549 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2550 ireq.ifr6_prefixlen);
2555 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2556 int plen, int scope)
2558 struct inet6_ifaddr *ifp;
2560 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2561 scope, IFA_F_PERMANENT, 0, 0);
2563 spin_lock_bh(&ifp->lock);
2564 ifp->flags &= ~IFA_F_TENTATIVE;
2565 spin_unlock_bh(&ifp->lock);
2566 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2571 #if IS_ENABLED(CONFIG_IPV6_SIT)
2572 static void sit_add_v4_addrs(struct inet6_dev *idev)
2574 struct in6_addr addr;
2575 struct net_device *dev;
2576 struct net *net = dev_net(idev->dev);
2581 memset(&addr, 0, sizeof(struct in6_addr));
2582 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2584 if (idev->dev->flags&IFF_POINTOPOINT) {
2585 addr.s6_addr32[0] = htonl(0xfe800000);
2588 scope = IPV6_ADDR_COMPATv4;
2591 if (addr.s6_addr32[3]) {
2592 add_addr(idev, &addr, 128, scope);
2596 for_each_netdev(net, dev) {
2597 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2598 if (in_dev && (dev->flags & IFF_UP)) {
2599 struct in_ifaddr *ifa;
2603 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2606 addr.s6_addr32[3] = ifa->ifa_local;
2608 if (ifa->ifa_scope == RT_SCOPE_LINK)
2610 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2611 if (idev->dev->flags&IFF_POINTOPOINT)
2615 if (idev->dev->flags&IFF_POINTOPOINT)
2620 add_addr(idev, &addr, plen, flag);
2627 static void init_loopback(struct net_device *dev)
2629 struct inet6_dev *idev;
2630 struct net_device *sp_dev;
2631 struct inet6_ifaddr *sp_ifa;
2632 struct rt6_info *sp_rt;
2638 if ((idev = ipv6_find_idev(dev)) == NULL) {
2639 pr_debug("%s: add_dev failed\n", __func__);
2643 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2645 /* Add routes to other interface's IPv6 addresses */
2646 for_each_netdev(dev_net(dev), sp_dev) {
2647 if (!strcmp(sp_dev->name, dev->name))
2650 idev = __in6_dev_get(sp_dev);
2654 read_lock_bh(&idev->lock);
2655 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2657 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2663 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2665 /* Failure cases are ignored */
2666 if (!IS_ERR(sp_rt)) {
2671 read_unlock_bh(&idev->lock);
2675 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2677 struct inet6_ifaddr *ifp;
2678 u32 addr_flags = IFA_F_PERMANENT;
2680 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2681 if (idev->cnf.optimistic_dad &&
2682 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2683 addr_flags |= IFA_F_OPTIMISTIC;
2687 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 0, 0);
2689 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2690 addrconf_dad_start(ifp);
2695 static void addrconf_dev_config(struct net_device *dev)
2697 struct in6_addr addr;
2698 struct inet6_dev *idev;
2702 if ((dev->type != ARPHRD_ETHER) &&
2703 (dev->type != ARPHRD_FDDI) &&
2704 (dev->type != ARPHRD_ARCNET) &&
2705 (dev->type != ARPHRD_INFINIBAND) &&
2706 (dev->type != ARPHRD_IEEE802154) &&
2707 (dev->type != ARPHRD_IEEE1394)) {
2708 /* Alas, we support only Ethernet autoconfiguration. */
2712 idev = addrconf_add_dev(dev);
2716 memset(&addr, 0, sizeof(struct in6_addr));
2717 addr.s6_addr32[0] = htonl(0xFE800000);
2719 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2720 addrconf_add_linklocal(idev, &addr);
2723 #if IS_ENABLED(CONFIG_IPV6_SIT)
2724 static void addrconf_sit_config(struct net_device *dev)
2726 struct inet6_dev *idev;
2731 * Configure the tunnel with one of our IPv4
2732 * addresses... we should configure all of
2733 * our v4 addrs in the tunnel
2736 if ((idev = ipv6_find_idev(dev)) == NULL) {
2737 pr_debug("%s: add_dev failed\n", __func__);
2741 if (dev->priv_flags & IFF_ISATAP) {
2742 struct in6_addr addr;
2744 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2745 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2746 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2747 addrconf_add_linklocal(idev, &addr);
2751 sit_add_v4_addrs(idev);
2753 if (dev->flags&IFF_POINTOPOINT)
2754 addrconf_add_mroute(dev);
2760 #if IS_ENABLED(CONFIG_NET_IPGRE)
2761 static void addrconf_gre_config(struct net_device *dev)
2763 struct inet6_dev *idev;
2764 struct in6_addr addr;
2768 if ((idev = ipv6_find_idev(dev)) == NULL) {
2769 pr_debug("%s: add_dev failed\n", __func__);
2773 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2774 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2776 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2777 addrconf_add_linklocal(idev, &addr);
2782 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2784 struct in6_addr lladdr;
2786 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2787 addrconf_add_linklocal(idev, &lladdr);
2793 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2795 struct net_device *link_dev;
2796 struct net *net = dev_net(idev->dev);
2798 /* first try to inherit the link-local address from the link device */
2799 if (idev->dev->iflink &&
2800 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2801 if (!ipv6_inherit_linklocal(idev, link_dev))
2804 /* then try to inherit it from any device */
2805 for_each_netdev(net, link_dev) {
2806 if (!ipv6_inherit_linklocal(idev, link_dev))
2809 pr_debug("init ip6-ip6: add_linklocal failed\n");
2813 * Autoconfigure tunnel with a link-local address so routing protocols,
2814 * DHCPv6, MLD etc. can be run over the virtual link
2817 static void addrconf_ip6_tnl_config(struct net_device *dev)
2819 struct inet6_dev *idev;
2823 idev = addrconf_add_dev(dev);
2825 pr_debug("init ip6-ip6: add_dev failed\n");
2828 ip6_tnl_add_linklocal(idev);
2831 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2834 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2835 struct inet6_dev *idev = __in6_dev_get(dev);
2836 int run_pending = 0;
2840 case NETDEV_REGISTER:
2841 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2842 idev = ipv6_add_dev(dev);
2844 return notifier_from_errno(-ENOMEM);
2850 if (dev->flags & IFF_SLAVE)
2853 if (event == NETDEV_UP) {
2854 if (!addrconf_qdisc_ok(dev)) {
2855 /* device is not ready yet. */
2856 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2861 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2862 idev = ipv6_add_dev(dev);
2865 idev->if_flags |= IF_READY;
2869 if (!addrconf_qdisc_ok(dev)) {
2870 /* device is still not ready. */
2875 if (idev->if_flags & IF_READY)
2876 /* device is already configured. */
2878 idev->if_flags |= IF_READY;
2881 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2887 switch (dev->type) {
2888 #if IS_ENABLED(CONFIG_IPV6_SIT)
2890 addrconf_sit_config(dev);
2893 #if IS_ENABLED(CONFIG_NET_IPGRE)
2895 addrconf_gre_config(dev);
2898 case ARPHRD_TUNNEL6:
2899 addrconf_ip6_tnl_config(dev);
2901 case ARPHRD_LOOPBACK:
2906 addrconf_dev_config(dev);
2912 addrconf_dad_run(idev);
2915 * If the MTU changed during the interface down,
2916 * when the interface up, the changed MTU must be
2917 * reflected in the idev as well as routers.
2919 if (idev->cnf.mtu6 != dev->mtu &&
2920 dev->mtu >= IPV6_MIN_MTU) {
2921 rt6_mtu_change(dev, dev->mtu);
2922 idev->cnf.mtu6 = dev->mtu;
2924 idev->tstamp = jiffies;
2925 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2928 * If the changed mtu during down is lower than
2929 * IPV6_MIN_MTU stop IPv6 on this interface.
2931 if (dev->mtu < IPV6_MIN_MTU)
2932 addrconf_ifdown(dev, 1);
2936 case NETDEV_CHANGEMTU:
2937 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2938 rt6_mtu_change(dev, dev->mtu);
2939 idev->cnf.mtu6 = dev->mtu;
2943 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2944 idev = ipv6_add_dev(dev);
2950 * MTU falled under IPV6_MIN_MTU.
2951 * Stop IPv6 on this interface.
2955 case NETDEV_UNREGISTER:
2957 * Remove all addresses from this interface.
2959 addrconf_ifdown(dev, event != NETDEV_DOWN);
2962 case NETDEV_CHANGENAME:
2964 snmp6_unregister_dev(idev);
2965 addrconf_sysctl_unregister(idev);
2966 addrconf_sysctl_register(idev);
2967 err = snmp6_register_dev(idev);
2969 return notifier_from_errno(err);
2973 case NETDEV_PRE_TYPE_CHANGE:
2974 case NETDEV_POST_TYPE_CHANGE:
2975 addrconf_type_change(dev, event);
2983 * addrconf module should be notified of a device going up
2985 static struct notifier_block ipv6_dev_notf = {
2986 .notifier_call = addrconf_notify,
2989 static void addrconf_type_change(struct net_device *dev, unsigned long event)
2991 struct inet6_dev *idev;
2994 idev = __in6_dev_get(dev);
2996 if (event == NETDEV_POST_TYPE_CHANGE)
2997 ipv6_mc_remap(idev);
2998 else if (event == NETDEV_PRE_TYPE_CHANGE)
2999 ipv6_mc_unmap(idev);
3002 static int addrconf_ifdown(struct net_device *dev, int how)
3004 struct net *net = dev_net(dev);
3005 struct inet6_dev *idev;
3006 struct inet6_ifaddr *ifa;
3011 rt6_ifdown(net, dev);
3012 neigh_ifdown(&nd_tbl, dev);
3014 idev = __in6_dev_get(dev);
3019 * Step 1: remove reference to ipv6 device from parent device.
3025 /* protected by rtnl_lock */
3026 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3028 /* Step 1.5: remove snmp6 entry */
3029 snmp6_unregister_dev(idev);
3033 /* Step 2: clear hash table */
3034 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3035 struct hlist_head *h = &inet6_addr_lst[i];
3037 spin_lock_bh(&addrconf_hash_lock);
3039 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3040 if (ifa->idev == idev) {
3041 hlist_del_init_rcu(&ifa->addr_lst);
3042 addrconf_del_dad_timer(ifa);
3046 spin_unlock_bh(&addrconf_hash_lock);
3049 write_lock_bh(&idev->lock);
3051 addrconf_del_rs_timer(idev);
3053 /* Step 2: clear flags for stateless addrconf */
3055 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3057 #ifdef CONFIG_IPV6_PRIVACY
3058 if (how && del_timer(&idev->regen_timer))
3061 /* Step 3: clear tempaddr list */
3062 while (!list_empty(&idev->tempaddr_list)) {
3063 ifa = list_first_entry(&idev->tempaddr_list,
3064 struct inet6_ifaddr, tmp_list);
3065 list_del(&ifa->tmp_list);
3066 write_unlock_bh(&idev->lock);
3067 spin_lock_bh(&ifa->lock);
3070 in6_ifa_put(ifa->ifpub);
3073 spin_unlock_bh(&ifa->lock);
3075 write_lock_bh(&idev->lock);
3079 while (!list_empty(&idev->addr_list)) {
3080 ifa = list_first_entry(&idev->addr_list,
3081 struct inet6_ifaddr, if_list);
3082 addrconf_del_dad_timer(ifa);
3084 list_del(&ifa->if_list);
3086 write_unlock_bh(&idev->lock);
3088 spin_lock_bh(&ifa->state_lock);
3090 ifa->state = INET6_IFADDR_STATE_DEAD;
3091 spin_unlock_bh(&ifa->state_lock);
3093 if (state != INET6_IFADDR_STATE_DEAD) {
3094 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3095 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3099 write_lock_bh(&idev->lock);
3102 write_unlock_bh(&idev->lock);
3104 /* Step 5: Discard multicast list */
3106 ipv6_mc_destroy_dev(idev);
3110 idev->tstamp = jiffies;
3112 /* Last: Shot the device (if unregistered) */
3114 addrconf_sysctl_unregister(idev);
3115 neigh_parms_release(&nd_tbl, idev->nd_parms);
3116 neigh_ifdown(&nd_tbl, dev);
3122 static void addrconf_rs_timer(unsigned long data)
3124 struct inet6_dev *idev = (struct inet6_dev *)data;
3125 struct in6_addr lladdr;
3127 write_lock(&idev->lock);
3128 if (idev->dead || !(idev->if_flags & IF_READY))
3131 if (!ipv6_accept_ra(idev))
3134 /* Announcement received after solicitation was sent */
3135 if (idev->if_flags & IF_RA_RCVD)
3138 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3139 if (!__ipv6_get_lladdr(idev, &lladdr, IFA_F_TENTATIVE))
3140 ndisc_send_rs(idev->dev, &lladdr,
3141 &in6addr_linklocal_allrouters);
3145 /* The wait after the last probe can be shorter */
3146 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3147 idev->cnf.rtr_solicits) ?
3148 idev->cnf.rtr_solicit_delay :
3149 idev->cnf.rtr_solicit_interval);
3152 * Note: we do not support deprecated "all on-link"
3153 * assumption any longer.
3155 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3159 write_unlock(&idev->lock);
3164 * Duplicate Address Detection
3166 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3168 unsigned long rand_num;
3169 struct inet6_dev *idev = ifp->idev;
3171 if (ifp->flags & IFA_F_OPTIMISTIC)
3174 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3176 ifp->dad_probes = idev->cnf.dad_transmits;
3177 addrconf_mod_dad_timer(ifp, rand_num);
3180 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3182 struct inet6_dev *idev = ifp->idev;
3183 struct net_device *dev = idev->dev;
3185 addrconf_join_solict(dev, &ifp->addr);
3187 net_srandom(ifp->addr.s6_addr32[3]);
3189 read_lock_bh(&idev->lock);
3190 spin_lock(&ifp->lock);
3191 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3194 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3195 idev->cnf.accept_dad < 1 ||
3196 !(ifp->flags&IFA_F_TENTATIVE) ||
3197 ifp->flags & IFA_F_NODAD) {
3198 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3199 spin_unlock(&ifp->lock);
3200 read_unlock_bh(&idev->lock);
3202 addrconf_dad_completed(ifp);
3206 if (!(idev->if_flags & IF_READY)) {
3207 spin_unlock(&ifp->lock);
3208 read_unlock_bh(&idev->lock);
3210 * If the device is not ready:
3211 * - keep it tentative if it is a permanent address.
3212 * - otherwise, kill it.
3215 addrconf_dad_stop(ifp, 0);
3220 * Optimistic nodes can start receiving
3223 if (ifp->flags & IFA_F_OPTIMISTIC)
3224 ip6_ins_rt(ifp->rt);
3226 addrconf_dad_kick(ifp);
3228 spin_unlock(&ifp->lock);
3229 read_unlock_bh(&idev->lock);
3232 static void addrconf_dad_timer(unsigned long data)
3234 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3235 struct inet6_dev *idev = ifp->idev;
3236 struct in6_addr mcaddr;
3238 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3241 write_lock(&idev->lock);
3242 if (idev->dead || !(idev->if_flags & IF_READY)) {
3243 write_unlock(&idev->lock);
3247 spin_lock(&ifp->lock);
3248 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3249 spin_unlock(&ifp->lock);
3250 write_unlock(&idev->lock);
3254 if (ifp->dad_probes == 0) {
3256 * DAD was successful
3259 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3260 spin_unlock(&ifp->lock);
3261 write_unlock(&idev->lock);
3263 addrconf_dad_completed(ifp);
3269 addrconf_mod_dad_timer(ifp, ifp->idev->nd_parms->retrans_time);
3270 spin_unlock(&ifp->lock);
3271 write_unlock(&idev->lock);
3273 /* send a neighbour solicitation for our addr */
3274 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3275 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3280 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3282 struct net_device *dev = ifp->idev->dev;
3283 struct in6_addr lladdr;
3284 bool send_rs, send_mld;
3286 addrconf_del_dad_timer(ifp);
3289 * Configure the address for reception. Now it is valid.
3292 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3294 /* If added prefix is link local and we are prepared to process
3295 router advertisements, start sending router solicitations.
3298 read_lock_bh(&ifp->idev->lock);
3299 spin_lock(&ifp->lock);
3300 send_mld = ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL &&
3301 ifp->idev->valid_ll_addr_cnt == 1;
3302 send_rs = send_mld &&
3303 ipv6_accept_ra(ifp->idev) &&
3304 ifp->idev->cnf.rtr_solicits > 0 &&
3305 (dev->flags&IFF_LOOPBACK) == 0;
3306 spin_unlock(&ifp->lock);
3307 read_unlock_bh(&ifp->idev->lock);
3309 /* While dad is in progress mld report's source address is in6_addrany.
3310 * Resend with proper ll now.
3313 ipv6_mc_dad_complete(ifp->idev);
3317 * If a host as already performed a random delay
3318 * [...] as part of DAD [...] there is no need
3319 * to delay again before sending the first RS
3321 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3323 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3325 write_lock_bh(&ifp->idev->lock);
3326 spin_lock(&ifp->lock);
3327 ifp->idev->rs_probes = 1;
3328 ifp->idev->if_flags |= IF_RS_SENT;
3329 addrconf_mod_rs_timer(ifp->idev,
3330 ifp->idev->cnf.rtr_solicit_interval);
3331 spin_unlock(&ifp->lock);
3332 write_unlock_bh(&ifp->idev->lock);
3336 static void addrconf_dad_run(struct inet6_dev *idev)
3338 struct inet6_ifaddr *ifp;
3340 read_lock_bh(&idev->lock);
3341 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3342 spin_lock(&ifp->lock);
3343 if (ifp->flags & IFA_F_TENTATIVE &&
3344 ifp->state == INET6_IFADDR_STATE_DAD)
3345 addrconf_dad_kick(ifp);
3346 spin_unlock(&ifp->lock);
3348 read_unlock_bh(&idev->lock);
3351 #ifdef CONFIG_PROC_FS
3352 struct if6_iter_state {
3353 struct seq_net_private p;
3358 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3360 struct inet6_ifaddr *ifa = NULL;
3361 struct if6_iter_state *state = seq->private;
3362 struct net *net = seq_file_net(seq);
3365 /* initial bucket if pos is 0 */
3371 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3372 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3374 if (!net_eq(dev_net(ifa->idev->dev), net))
3376 /* sync with offset */
3377 if (p < state->offset) {
3385 /* prepare for next bucket */
3392 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3393 struct inet6_ifaddr *ifa)
3395 struct if6_iter_state *state = seq->private;
3396 struct net *net = seq_file_net(seq);
3398 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3399 if (!net_eq(dev_net(ifa->idev->dev), net))
3405 while (++state->bucket < IN6_ADDR_HSIZE) {
3407 hlist_for_each_entry_rcu_bh(ifa,
3408 &inet6_addr_lst[state->bucket], addr_lst) {
3409 if (!net_eq(dev_net(ifa->idev->dev), net))
3419 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3423 return if6_get_first(seq, *pos);
3426 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3428 struct inet6_ifaddr *ifa;
3430 ifa = if6_get_next(seq, v);
3435 static void if6_seq_stop(struct seq_file *seq, void *v)
3438 rcu_read_unlock_bh();
3441 static int if6_seq_show(struct seq_file *seq, void *v)
3443 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3444 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3446 ifp->idev->dev->ifindex,
3450 ifp->idev->dev->name);
3454 static const struct seq_operations if6_seq_ops = {
3455 .start = if6_seq_start,
3456 .next = if6_seq_next,
3457 .show = if6_seq_show,
3458 .stop = if6_seq_stop,
3461 static int if6_seq_open(struct inode *inode, struct file *file)
3463 return seq_open_net(inode, file, &if6_seq_ops,
3464 sizeof(struct if6_iter_state));
3467 static const struct file_operations if6_fops = {
3468 .owner = THIS_MODULE,
3469 .open = if6_seq_open,
3471 .llseek = seq_lseek,
3472 .release = seq_release_net,
3475 static int __net_init if6_proc_net_init(struct net *net)
3477 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3482 static void __net_exit if6_proc_net_exit(struct net *net)
3484 remove_proc_entry("if_inet6", net->proc_net);
3487 static struct pernet_operations if6_proc_net_ops = {
3488 .init = if6_proc_net_init,
3489 .exit = if6_proc_net_exit,
3492 int __init if6_proc_init(void)
3494 return register_pernet_subsys(&if6_proc_net_ops);
3497 void if6_proc_exit(void)
3499 unregister_pernet_subsys(&if6_proc_net_ops);
3501 #endif /* CONFIG_PROC_FS */
3503 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3504 /* Check if address is a home address configured on any interface. */
3505 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3508 struct inet6_ifaddr *ifp = NULL;
3509 unsigned int hash = inet6_addr_hash(addr);
3512 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3513 if (!net_eq(dev_net(ifp->idev->dev), net))
3515 if (ipv6_addr_equal(&ifp->addr, addr) &&
3516 (ifp->flags & IFA_F_HOMEADDRESS)) {
3521 rcu_read_unlock_bh();
3527 * Periodic address status verification
3530 static void addrconf_verify(unsigned long foo)
3532 unsigned long now, next, next_sec, next_sched;
3533 struct inet6_ifaddr *ifp;
3537 spin_lock(&addrconf_verify_lock);
3539 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3541 del_timer(&addr_chk_timer);
3543 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3545 hlist_for_each_entry_rcu_bh(ifp,
3546 &inet6_addr_lst[i], addr_lst) {
3549 if (ifp->flags & IFA_F_PERMANENT)
3552 spin_lock(&ifp->lock);
3553 /* We try to batch several events at once. */
3554 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3556 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3557 age >= ifp->valid_lft) {
3558 spin_unlock(&ifp->lock);
3562 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3563 spin_unlock(&ifp->lock);
3565 } else if (age >= ifp->prefered_lft) {
3566 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3569 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3571 ifp->flags |= IFA_F_DEPRECATED;
3574 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3575 next = ifp->tstamp + ifp->valid_lft * HZ;
3577 spin_unlock(&ifp->lock);
3582 ipv6_ifa_notify(0, ifp);
3586 #ifdef CONFIG_IPV6_PRIVACY
3587 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3588 !(ifp->flags&IFA_F_TENTATIVE)) {
3589 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3590 ifp->idev->cnf.dad_transmits *
3591 ifp->idev->nd_parms->retrans_time / HZ;
3593 if (age >= ifp->prefered_lft - regen_advance) {
3594 struct inet6_ifaddr *ifpub = ifp->ifpub;
3595 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3596 next = ifp->tstamp + ifp->prefered_lft * HZ;
3597 if (!ifp->regen_count && ifpub) {
3600 in6_ifa_hold(ifpub);
3601 spin_unlock(&ifp->lock);
3603 spin_lock(&ifpub->lock);
3604 ifpub->regen_count = 0;
3605 spin_unlock(&ifpub->lock);
3606 ipv6_create_tempaddr(ifpub, ifp);
3611 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3612 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3613 spin_unlock(&ifp->lock);
3616 /* ifp->prefered_lft <= ifp->valid_lft */
3617 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3618 next = ifp->tstamp + ifp->prefered_lft * HZ;
3619 spin_unlock(&ifp->lock);
3624 next_sec = round_jiffies_up(next);
3627 /* If rounded timeout is accurate enough, accept it. */
3628 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3629 next_sched = next_sec;
3631 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3632 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3633 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3635 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3636 now, next, next_sec, next_sched));
3638 addr_chk_timer.expires = next_sched;
3639 add_timer(&addr_chk_timer);
3640 spin_unlock(&addrconf_verify_lock);
3641 rcu_read_unlock_bh();
3644 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3645 struct in6_addr **peer_pfx)
3647 struct in6_addr *pfx = NULL;
3652 pfx = nla_data(addr);
3655 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3657 pfx = nla_data(local);
3663 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3664 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3665 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3666 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3670 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3672 struct net *net = sock_net(skb->sk);
3673 struct ifaddrmsg *ifm;
3674 struct nlattr *tb[IFA_MAX+1];
3675 struct in6_addr *pfx, *peer_pfx;
3678 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3682 ifm = nlmsg_data(nlh);
3683 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3687 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3690 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3691 u32 prefered_lft, u32 valid_lft)
3695 unsigned long timeout;
3697 if (!valid_lft || (prefered_lft > valid_lft))
3700 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3701 if (addrconf_finite_timeout(timeout)) {
3702 expires = jiffies_to_clock_t(timeout * HZ);
3703 valid_lft = timeout;
3704 flags = RTF_EXPIRES;
3708 ifa_flags |= IFA_F_PERMANENT;
3711 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3712 if (addrconf_finite_timeout(timeout)) {
3714 ifa_flags |= IFA_F_DEPRECATED;
3715 prefered_lft = timeout;
3718 spin_lock_bh(&ifp->lock);
3719 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3720 ifp->tstamp = jiffies;
3721 ifp->valid_lft = valid_lft;
3722 ifp->prefered_lft = prefered_lft;
3724 spin_unlock_bh(&ifp->lock);
3725 if (!(ifp->flags&IFA_F_TENTATIVE))
3726 ipv6_ifa_notify(0, ifp);
3728 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3736 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3738 struct net *net = sock_net(skb->sk);
3739 struct ifaddrmsg *ifm;
3740 struct nlattr *tb[IFA_MAX+1];
3741 struct in6_addr *pfx, *peer_pfx;
3742 struct inet6_ifaddr *ifa;
3743 struct net_device *dev;
3744 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3748 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3752 ifm = nlmsg_data(nlh);
3753 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3757 if (tb[IFA_CACHEINFO]) {
3758 struct ifa_cacheinfo *ci;
3760 ci = nla_data(tb[IFA_CACHEINFO]);
3761 valid_lft = ci->ifa_valid;
3762 preferred_lft = ci->ifa_prefered;
3764 preferred_lft = INFINITY_LIFE_TIME;
3765 valid_lft = INFINITY_LIFE_TIME;
3768 dev = __dev_get_by_index(net, ifm->ifa_index);
3772 /* We ignore other flags so far. */
3773 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3775 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3778 * It would be best to check for !NLM_F_CREATE here but
3779 * userspace alreay relies on not having to provide this.
3781 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3782 ifm->ifa_prefixlen, ifa_flags,
3783 preferred_lft, valid_lft);
3786 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3787 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3790 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3797 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3798 u8 scope, int ifindex)
3800 struct ifaddrmsg *ifm;
3802 ifm = nlmsg_data(nlh);
3803 ifm->ifa_family = AF_INET6;
3804 ifm->ifa_prefixlen = prefixlen;
3805 ifm->ifa_flags = flags;
3806 ifm->ifa_scope = scope;
3807 ifm->ifa_index = ifindex;
3810 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3811 unsigned long tstamp, u32 preferred, u32 valid)
3813 struct ifa_cacheinfo ci;
3815 ci.cstamp = cstamp_delta(cstamp);
3816 ci.tstamp = cstamp_delta(tstamp);
3817 ci.ifa_prefered = preferred;
3818 ci.ifa_valid = valid;
3820 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3823 static inline int rt_scope(int ifa_scope)
3825 if (ifa_scope & IFA_HOST)
3826 return RT_SCOPE_HOST;
3827 else if (ifa_scope & IFA_LINK)
3828 return RT_SCOPE_LINK;
3829 else if (ifa_scope & IFA_SITE)
3830 return RT_SCOPE_SITE;
3832 return RT_SCOPE_UNIVERSE;
3835 static inline int inet6_ifaddr_msgsize(void)
3837 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3838 + nla_total_size(16) /* IFA_LOCAL */
3839 + nla_total_size(16) /* IFA_ADDRESS */
3840 + nla_total_size(sizeof(struct ifa_cacheinfo));
3843 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3844 u32 portid, u32 seq, int event, unsigned int flags)
3846 struct nlmsghdr *nlh;
3847 u32 preferred, valid;
3849 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3853 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3854 ifa->idev->dev->ifindex);
3856 if (!(ifa->flags&IFA_F_PERMANENT)) {
3857 preferred = ifa->prefered_lft;
3858 valid = ifa->valid_lft;
3859 if (preferred != INFINITY_LIFE_TIME) {
3860 long tval = (jiffies - ifa->tstamp)/HZ;
3861 if (preferred > tval)
3865 if (valid != INFINITY_LIFE_TIME) {
3873 preferred = INFINITY_LIFE_TIME;
3874 valid = INFINITY_LIFE_TIME;
3877 if (!ipv6_addr_any(&ifa->peer_addr)) {
3878 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
3879 nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
3882 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
3885 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3888 return nlmsg_end(skb, nlh);
3891 nlmsg_cancel(skb, nlh);
3895 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3896 u32 portid, u32 seq, int event, u16 flags)
3898 struct nlmsghdr *nlh;
3899 u8 scope = RT_SCOPE_UNIVERSE;
3900 int ifindex = ifmca->idev->dev->ifindex;
3902 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3903 scope = RT_SCOPE_SITE;
3905 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3909 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3910 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3911 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3912 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3913 nlmsg_cancel(skb, nlh);
3917 return nlmsg_end(skb, nlh);
3920 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3921 u32 portid, u32 seq, int event, unsigned int flags)
3923 struct nlmsghdr *nlh;
3924 u8 scope = RT_SCOPE_UNIVERSE;
3925 int ifindex = ifaca->aca_idev->dev->ifindex;
3927 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3928 scope = RT_SCOPE_SITE;
3930 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3934 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3935 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3936 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3937 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3938 nlmsg_cancel(skb, nlh);
3942 return nlmsg_end(skb, nlh);
3951 /* called with rcu_read_lock() */
3952 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3953 struct netlink_callback *cb, enum addr_type_t type,
3954 int s_ip_idx, int *p_ip_idx)
3956 struct ifmcaddr6 *ifmca;
3957 struct ifacaddr6 *ifaca;
3959 int ip_idx = *p_ip_idx;
3961 read_lock_bh(&idev->lock);
3963 case UNICAST_ADDR: {
3964 struct inet6_ifaddr *ifa;
3966 /* unicast address incl. temp addr */
3967 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3968 if (++ip_idx < s_ip_idx)
3970 err = inet6_fill_ifaddr(skb, ifa,
3971 NETLINK_CB(cb->skb).portid,
3977 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3981 case MULTICAST_ADDR:
3982 /* multicast address */
3983 for (ifmca = idev->mc_list; ifmca;
3984 ifmca = ifmca->next, ip_idx++) {
3985 if (ip_idx < s_ip_idx)
3987 err = inet6_fill_ifmcaddr(skb, ifmca,
3988 NETLINK_CB(cb->skb).portid,
3997 /* anycast address */
3998 for (ifaca = idev->ac_list; ifaca;
3999 ifaca = ifaca->aca_next, ip_idx++) {
4000 if (ip_idx < s_ip_idx)
4002 err = inet6_fill_ifacaddr(skb, ifaca,
4003 NETLINK_CB(cb->skb).portid,
4014 read_unlock_bh(&idev->lock);
4019 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4020 enum addr_type_t type)
4022 struct net *net = sock_net(skb->sk);
4025 int s_idx, s_ip_idx;
4026 struct net_device *dev;
4027 struct inet6_dev *idev;
4028 struct hlist_head *head;
4031 s_idx = idx = cb->args[1];
4032 s_ip_idx = ip_idx = cb->args[2];
4035 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4036 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4038 head = &net->dev_index_head[h];
4039 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4042 if (h > s_h || idx > s_idx)
4045 idev = __in6_dev_get(dev);
4049 if (in6_dump_addrs(idev, skb, cb, type,
4050 s_ip_idx, &ip_idx) <= 0)
4060 cb->args[2] = ip_idx;
4065 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4067 enum addr_type_t type = UNICAST_ADDR;
4069 return inet6_dump_addr(skb, cb, type);
4072 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4074 enum addr_type_t type = MULTICAST_ADDR;
4076 return inet6_dump_addr(skb, cb, type);
4080 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4082 enum addr_type_t type = ANYCAST_ADDR;
4084 return inet6_dump_addr(skb, cb, type);
4087 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4089 struct net *net = sock_net(in_skb->sk);
4090 struct ifaddrmsg *ifm;
4091 struct nlattr *tb[IFA_MAX+1];
4092 struct in6_addr *addr = NULL, *peer;
4093 struct net_device *dev = NULL;
4094 struct inet6_ifaddr *ifa;
4095 struct sk_buff *skb;
4098 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4102 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4108 ifm = nlmsg_data(nlh);
4110 dev = __dev_get_by_index(net, ifm->ifa_index);
4112 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4114 err = -EADDRNOTAVAIL;
4118 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4124 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4125 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4127 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4128 WARN_ON(err == -EMSGSIZE);
4132 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4139 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4141 struct sk_buff *skb;
4142 struct net *net = dev_net(ifa->idev->dev);
4145 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4149 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4151 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4152 WARN_ON(err == -EMSGSIZE);
4156 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4160 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4163 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4164 __s32 *array, int bytes)
4166 BUG_ON(bytes < (DEVCONF_MAX * 4));
4168 memset(array, 0, bytes);
4169 array[DEVCONF_FORWARDING] = cnf->forwarding;
4170 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4171 array[DEVCONF_MTU6] = cnf->mtu6;
4172 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4173 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4174 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4175 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4176 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4177 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4178 jiffies_to_msecs(cnf->rtr_solicit_interval);
4179 array[DEVCONF_RTR_SOLICIT_DELAY] =
4180 jiffies_to_msecs(cnf->rtr_solicit_delay);
4181 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4182 #ifdef CONFIG_IPV6_PRIVACY
4183 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4184 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4185 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4186 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4187 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4189 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4190 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4191 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4192 #ifdef CONFIG_IPV6_ROUTER_PREF
4193 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4194 array[DEVCONF_RTR_PROBE_INTERVAL] =
4195 jiffies_to_msecs(cnf->rtr_probe_interval);
4196 #ifdef CONFIG_IPV6_ROUTE_INFO
4197 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4200 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4201 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4202 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4203 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4205 #ifdef CONFIG_IPV6_MROUTE
4206 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4208 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4209 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4210 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4211 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4214 static inline size_t inet6_ifla6_size(void)
4216 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4217 + nla_total_size(sizeof(struct ifla_cacheinfo))
4218 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4219 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4220 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4221 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4224 static inline size_t inet6_if_nlmsg_size(void)
4226 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4227 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4228 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4229 + nla_total_size(4) /* IFLA_MTU */
4230 + nla_total_size(4) /* IFLA_LINK */
4231 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4234 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4235 int items, int bytes)
4238 int pad = bytes - sizeof(u64) * items;
4241 /* Use put_unaligned() because stats may not be aligned for u64. */
4242 put_unaligned(items, &stats[0]);
4243 for (i = 1; i < items; i++)
4244 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4246 memset(&stats[items], 0, pad);
4249 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4250 int items, int bytes, size_t syncpoff)
4253 int pad = bytes - sizeof(u64) * items;
4256 /* Use put_unaligned() because stats may not be aligned for u64. */
4257 put_unaligned(items, &stats[0]);
4258 for (i = 1; i < items; i++)
4259 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4261 memset(&stats[items], 0, pad);
4264 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4268 case IFLA_INET6_STATS:
4269 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4270 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4272 case IFLA_INET6_ICMP6STATS:
4273 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4278 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4281 struct ifla_cacheinfo ci;
4283 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4284 goto nla_put_failure;
4285 ci.max_reasm_len = IPV6_MAXPLEN;
4286 ci.tstamp = cstamp_delta(idev->tstamp);
4287 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4288 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
4289 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4290 goto nla_put_failure;
4291 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4293 goto nla_put_failure;
4294 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4296 /* XXX - MC not implemented */
4298 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4300 goto nla_put_failure;
4301 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4303 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4305 goto nla_put_failure;
4306 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4308 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4310 goto nla_put_failure;
4311 read_lock_bh(&idev->lock);
4312 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4313 read_unlock_bh(&idev->lock);
4321 static size_t inet6_get_link_af_size(const struct net_device *dev)
4323 if (!__in6_dev_get(dev))
4326 return inet6_ifla6_size();
4329 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4331 struct inet6_dev *idev = __in6_dev_get(dev);
4336 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4342 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4344 struct inet6_ifaddr *ifp;
4345 struct net_device *dev = idev->dev;
4346 bool update_rs = false;
4347 struct in6_addr ll_addr;
4351 if (ipv6_addr_any(token))
4353 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4355 if (!ipv6_accept_ra(idev))
4357 if (idev->cnf.rtr_solicits <= 0)
4360 write_lock_bh(&idev->lock);
4362 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4363 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4365 write_unlock_bh(&idev->lock);
4367 if (!idev->dead && (idev->if_flags & IF_READY) &&
4368 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4369 IFA_F_OPTIMISTIC)) {
4371 /* If we're not ready, then normal ifup will take care
4372 * of this. Otherwise, we need to request our rs here.
4374 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4378 write_lock_bh(&idev->lock);
4381 idev->if_flags |= IF_RS_SENT;
4382 idev->rs_probes = 1;
4383 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4386 /* Well, that's kinda nasty ... */
4387 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4388 spin_lock(&ifp->lock);
4389 if (ifp->tokenized) {
4391 ifp->prefered_lft = 0;
4393 spin_unlock(&ifp->lock);
4396 write_unlock_bh(&idev->lock);
4401 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4404 struct inet6_dev *idev = __in6_dev_get(dev);
4405 struct nlattr *tb[IFLA_INET6_MAX + 1];
4408 return -EAFNOSUPPORT;
4410 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4413 if (tb[IFLA_INET6_TOKEN])
4414 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4419 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4420 u32 portid, u32 seq, int event, unsigned int flags)
4422 struct net_device *dev = idev->dev;
4423 struct ifinfomsg *hdr;
4424 struct nlmsghdr *nlh;
4427 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4431 hdr = nlmsg_data(nlh);
4432 hdr->ifi_family = AF_INET6;
4434 hdr->ifi_type = dev->type;
4435 hdr->ifi_index = dev->ifindex;
4436 hdr->ifi_flags = dev_get_flags(dev);
4437 hdr->ifi_change = 0;
4439 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4441 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4442 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4443 (dev->ifindex != dev->iflink &&
4444 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4445 goto nla_put_failure;
4446 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4447 if (protoinfo == NULL)
4448 goto nla_put_failure;
4450 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4451 goto nla_put_failure;
4453 nla_nest_end(skb, protoinfo);
4454 return nlmsg_end(skb, nlh);
4457 nlmsg_cancel(skb, nlh);
4461 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4463 struct net *net = sock_net(skb->sk);
4466 struct net_device *dev;
4467 struct inet6_dev *idev;
4468 struct hlist_head *head;
4471 s_idx = cb->args[1];
4474 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4476 head = &net->dev_index_head[h];
4477 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4480 idev = __in6_dev_get(dev);
4483 if (inet6_fill_ifinfo(skb, idev,
4484 NETLINK_CB(cb->skb).portid,
4486 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4500 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4502 struct sk_buff *skb;
4503 struct net *net = dev_net(idev->dev);
4506 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4510 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4512 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4513 WARN_ON(err == -EMSGSIZE);
4517 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4521 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4524 static inline size_t inet6_prefix_nlmsg_size(void)
4526 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4527 + nla_total_size(sizeof(struct in6_addr))
4528 + nla_total_size(sizeof(struct prefix_cacheinfo));
4531 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4532 struct prefix_info *pinfo, u32 portid, u32 seq,
4533 int event, unsigned int flags)
4535 struct prefixmsg *pmsg;
4536 struct nlmsghdr *nlh;
4537 struct prefix_cacheinfo ci;
4539 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4543 pmsg = nlmsg_data(nlh);
4544 pmsg->prefix_family = AF_INET6;
4545 pmsg->prefix_pad1 = 0;
4546 pmsg->prefix_pad2 = 0;
4547 pmsg->prefix_ifindex = idev->dev->ifindex;
4548 pmsg->prefix_len = pinfo->prefix_len;
4549 pmsg->prefix_type = pinfo->type;
4550 pmsg->prefix_pad3 = 0;
4551 pmsg->prefix_flags = 0;
4553 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4554 if (pinfo->autoconf)
4555 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4557 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4558 goto nla_put_failure;
4559 ci.preferred_time = ntohl(pinfo->prefered);
4560 ci.valid_time = ntohl(pinfo->valid);
4561 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4562 goto nla_put_failure;
4563 return nlmsg_end(skb, nlh);
4566 nlmsg_cancel(skb, nlh);
4570 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4571 struct prefix_info *pinfo)
4573 struct sk_buff *skb;
4574 struct net *net = dev_net(idev->dev);
4577 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4581 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
4583 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4584 WARN_ON(err == -EMSGSIZE);
4588 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4592 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4595 static void update_valid_ll_addr_cnt(struct inet6_ifaddr *ifp, int count)
4597 write_lock_bh(&ifp->idev->lock);
4598 spin_lock(&ifp->lock);
4599 if (((ifp->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|
4600 IFA_F_DADFAILED)) == IFA_F_PERMANENT) &&
4601 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL))
4602 ifp->idev->valid_ll_addr_cnt += count;
4603 WARN_ON(ifp->idev->valid_ll_addr_cnt < 0);
4604 spin_unlock(&ifp->lock);
4605 write_unlock_bh(&ifp->idev->lock);
4608 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4610 struct net *net = dev_net(ifp->idev->dev);
4612 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4616 update_valid_ll_addr_cnt(ifp, 1);
4619 * If the address was optimistic
4620 * we inserted the route at the start of
4621 * our DAD process, so we don't need
4624 if (!(ifp->rt->rt6i_node))
4625 ip6_ins_rt(ifp->rt);
4626 if (ifp->idev->cnf.forwarding)
4627 addrconf_join_anycast(ifp);
4628 if (!ipv6_addr_any(&ifp->peer_addr))
4629 addrconf_prefix_route(&ifp->peer_addr, 128,
4630 ifp->idev->dev, 0, 0);
4633 update_valid_ll_addr_cnt(ifp, -1);
4635 if (ifp->idev->cnf.forwarding)
4636 addrconf_leave_anycast(ifp);
4637 addrconf_leave_solict(ifp->idev, &ifp->addr);
4638 if (!ipv6_addr_any(&ifp->peer_addr)) {
4639 struct rt6_info *rt;
4640 struct net_device *dev = ifp->idev->dev;
4642 rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL,
4650 dst_hold(&ifp->rt->dst);
4652 if (ip6_del_rt(ifp->rt))
4653 dst_free(&ifp->rt->dst);
4656 atomic_inc(&net->ipv6.dev_addr_genid);
4659 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4662 if (likely(ifp->idev->dead == 0))
4663 __ipv6_ifa_notify(event, ifp);
4664 rcu_read_unlock_bh();
4667 #ifdef CONFIG_SYSCTL
4670 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
4671 void __user *buffer, size_t *lenp, loff_t *ppos)
4673 int *valp = ctl->data;
4676 struct ctl_table lctl;
4680 * ctl->data points to idev->cnf.forwarding, we should
4681 * not modify it until we get the rtnl lock.
4686 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4689 ret = addrconf_fixup_forwarding(ctl, valp, val);
4695 static void dev_disable_change(struct inet6_dev *idev)
4697 struct netdev_notifier_info info;
4699 if (!idev || !idev->dev)
4702 netdev_notifier_info_init(&info, idev->dev);
4703 if (idev->cnf.disable_ipv6)
4704 addrconf_notify(NULL, NETDEV_DOWN, &info);
4706 addrconf_notify(NULL, NETDEV_UP, &info);
4709 static void addrconf_disable_change(struct net *net, __s32 newf)
4711 struct net_device *dev;
4712 struct inet6_dev *idev;
4715 for_each_netdev_rcu(net, dev) {
4716 idev = __in6_dev_get(dev);
4718 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4719 idev->cnf.disable_ipv6 = newf;
4721 dev_disable_change(idev);
4727 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
4732 if (!rtnl_trylock())
4733 return restart_syscall();
4735 net = (struct net *)table->extra2;
4739 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4744 if (p == &net->ipv6.devconf_all->disable_ipv6) {
4745 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4746 addrconf_disable_change(net, newf);
4747 } else if ((!newf) ^ (!old))
4748 dev_disable_change((struct inet6_dev *)table->extra1);
4755 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
4756 void __user *buffer, size_t *lenp, loff_t *ppos)
4758 int *valp = ctl->data;
4761 struct ctl_table lctl;
4765 * ctl->data points to idev->cnf.disable_ipv6, we should
4766 * not modify it until we get the rtnl lock.
4771 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4774 ret = addrconf_disable_ipv6(ctl, valp, val);
4780 static struct addrconf_sysctl_table
4782 struct ctl_table_header *sysctl_header;
4783 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
4784 } addrconf_sysctl __read_mostly = {
4785 .sysctl_header = NULL,
4788 .procname = "forwarding",
4789 .data = &ipv6_devconf.forwarding,
4790 .maxlen = sizeof(int),
4792 .proc_handler = addrconf_sysctl_forward,
4795 .procname = "hop_limit",
4796 .data = &ipv6_devconf.hop_limit,
4797 .maxlen = sizeof(int),
4799 .proc_handler = proc_dointvec,
4803 .data = &ipv6_devconf.mtu6,
4804 .maxlen = sizeof(int),
4806 .proc_handler = proc_dointvec,
4809 .procname = "accept_ra",
4810 .data = &ipv6_devconf.accept_ra,
4811 .maxlen = sizeof(int),
4813 .proc_handler = proc_dointvec,
4816 .procname = "accept_redirects",
4817 .data = &ipv6_devconf.accept_redirects,
4818 .maxlen = sizeof(int),
4820 .proc_handler = proc_dointvec,
4823 .procname = "autoconf",
4824 .data = &ipv6_devconf.autoconf,
4825 .maxlen = sizeof(int),
4827 .proc_handler = proc_dointvec,
4830 .procname = "dad_transmits",
4831 .data = &ipv6_devconf.dad_transmits,
4832 .maxlen = sizeof(int),
4834 .proc_handler = proc_dointvec,
4837 .procname = "router_solicitations",
4838 .data = &ipv6_devconf.rtr_solicits,
4839 .maxlen = sizeof(int),
4841 .proc_handler = proc_dointvec,
4844 .procname = "router_solicitation_interval",
4845 .data = &ipv6_devconf.rtr_solicit_interval,
4846 .maxlen = sizeof(int),
4848 .proc_handler = proc_dointvec_jiffies,
4851 .procname = "router_solicitation_delay",
4852 .data = &ipv6_devconf.rtr_solicit_delay,
4853 .maxlen = sizeof(int),
4855 .proc_handler = proc_dointvec_jiffies,
4858 .procname = "force_mld_version",
4859 .data = &ipv6_devconf.force_mld_version,
4860 .maxlen = sizeof(int),
4862 .proc_handler = proc_dointvec,
4864 #ifdef CONFIG_IPV6_PRIVACY
4866 .procname = "use_tempaddr",
4867 .data = &ipv6_devconf.use_tempaddr,
4868 .maxlen = sizeof(int),
4870 .proc_handler = proc_dointvec,
4873 .procname = "temp_valid_lft",
4874 .data = &ipv6_devconf.temp_valid_lft,
4875 .maxlen = sizeof(int),
4877 .proc_handler = proc_dointvec,
4880 .procname = "temp_prefered_lft",
4881 .data = &ipv6_devconf.temp_prefered_lft,
4882 .maxlen = sizeof(int),
4884 .proc_handler = proc_dointvec,
4887 .procname = "regen_max_retry",
4888 .data = &ipv6_devconf.regen_max_retry,
4889 .maxlen = sizeof(int),
4891 .proc_handler = proc_dointvec,
4894 .procname = "max_desync_factor",
4895 .data = &ipv6_devconf.max_desync_factor,
4896 .maxlen = sizeof(int),
4898 .proc_handler = proc_dointvec,
4902 .procname = "max_addresses",
4903 .data = &ipv6_devconf.max_addresses,
4904 .maxlen = sizeof(int),
4906 .proc_handler = proc_dointvec,
4909 .procname = "accept_ra_defrtr",
4910 .data = &ipv6_devconf.accept_ra_defrtr,
4911 .maxlen = sizeof(int),
4913 .proc_handler = proc_dointvec,
4916 .procname = "accept_ra_pinfo",
4917 .data = &ipv6_devconf.accept_ra_pinfo,
4918 .maxlen = sizeof(int),
4920 .proc_handler = proc_dointvec,
4922 #ifdef CONFIG_IPV6_ROUTER_PREF
4924 .procname = "accept_ra_rtr_pref",
4925 .data = &ipv6_devconf.accept_ra_rtr_pref,
4926 .maxlen = sizeof(int),
4928 .proc_handler = proc_dointvec,
4931 .procname = "router_probe_interval",
4932 .data = &ipv6_devconf.rtr_probe_interval,
4933 .maxlen = sizeof(int),
4935 .proc_handler = proc_dointvec_jiffies,
4937 #ifdef CONFIG_IPV6_ROUTE_INFO
4939 .procname = "accept_ra_rt_info_max_plen",
4940 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4941 .maxlen = sizeof(int),
4943 .proc_handler = proc_dointvec,
4948 .procname = "proxy_ndp",
4949 .data = &ipv6_devconf.proxy_ndp,
4950 .maxlen = sizeof(int),
4952 .proc_handler = proc_dointvec,
4955 .procname = "accept_source_route",
4956 .data = &ipv6_devconf.accept_source_route,
4957 .maxlen = sizeof(int),
4959 .proc_handler = proc_dointvec,
4961 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4963 .procname = "optimistic_dad",
4964 .data = &ipv6_devconf.optimistic_dad,
4965 .maxlen = sizeof(int),
4967 .proc_handler = proc_dointvec,
4971 #ifdef CONFIG_IPV6_MROUTE
4973 .procname = "mc_forwarding",
4974 .data = &ipv6_devconf.mc_forwarding,
4975 .maxlen = sizeof(int),
4977 .proc_handler = proc_dointvec,
4981 .procname = "disable_ipv6",
4982 .data = &ipv6_devconf.disable_ipv6,
4983 .maxlen = sizeof(int),
4985 .proc_handler = addrconf_sysctl_disable,
4988 .procname = "accept_dad",
4989 .data = &ipv6_devconf.accept_dad,
4990 .maxlen = sizeof(int),
4992 .proc_handler = proc_dointvec,
4995 .procname = "force_tllao",
4996 .data = &ipv6_devconf.force_tllao,
4997 .maxlen = sizeof(int),
4999 .proc_handler = proc_dointvec
5002 .procname = "ndisc_notify",
5003 .data = &ipv6_devconf.ndisc_notify,
5004 .maxlen = sizeof(int),
5006 .proc_handler = proc_dointvec
5014 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5015 struct inet6_dev *idev, struct ipv6_devconf *p)
5018 struct addrconf_sysctl_table *t;
5019 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5021 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5025 for (i = 0; t->addrconf_vars[i].data; i++) {
5026 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5027 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5028 t->addrconf_vars[i].extra2 = net;
5031 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5033 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5034 if (t->sysctl_header == NULL)
5046 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5048 struct addrconf_sysctl_table *t;
5050 if (p->sysctl == NULL)
5055 unregister_net_sysctl_table(t->sysctl_header);
5059 static void addrconf_sysctl_register(struct inet6_dev *idev)
5061 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
5062 &ndisc_ifinfo_sysctl_change);
5063 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5067 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5069 __addrconf_sysctl_unregister(&idev->cnf);
5070 neigh_sysctl_unregister(idev->nd_parms);
5076 static int __net_init addrconf_init_net(struct net *net)
5079 struct ipv6_devconf *all, *dflt;
5081 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5085 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5087 goto err_alloc_dflt;
5089 /* these will be inherited by all namespaces */
5090 dflt->autoconf = ipv6_defaults.autoconf;
5091 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5093 net->ipv6.devconf_all = all;
5094 net->ipv6.devconf_dflt = dflt;
5096 #ifdef CONFIG_SYSCTL
5097 err = __addrconf_sysctl_register(net, "all", NULL, all);
5101 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5107 #ifdef CONFIG_SYSCTL
5109 __addrconf_sysctl_unregister(all);
5119 static void __net_exit addrconf_exit_net(struct net *net)
5121 #ifdef CONFIG_SYSCTL
5122 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5123 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5125 if (!net_eq(net, &init_net)) {
5126 kfree(net->ipv6.devconf_dflt);
5127 kfree(net->ipv6.devconf_all);
5131 static struct pernet_operations addrconf_ops = {
5132 .init = addrconf_init_net,
5133 .exit = addrconf_exit_net,
5136 static struct rtnl_af_ops inet6_ops = {
5138 .fill_link_af = inet6_fill_link_af,
5139 .get_link_af_size = inet6_get_link_af_size,
5140 .set_link_af = inet6_set_link_af,
5144 * Init / cleanup code
5147 int __init addrconf_init(void)
5151 err = ipv6_addr_label_init();
5153 pr_crit("%s: cannot initialize default policy table: %d\n",
5158 err = register_pernet_subsys(&addrconf_ops);
5162 /* The addrconf netdev notifier requires that loopback_dev
5163 * has it's ipv6 private information allocated and setup
5164 * before it can bring up and give link-local addresses
5165 * to other devices which are up.
5167 * Unfortunately, loopback_dev is not necessarily the first
5168 * entry in the global dev_base list of net devices. In fact,
5169 * it is likely to be the very last entry on that list.
5170 * So this causes the notifier registry below to try and
5171 * give link-local addresses to all devices besides loopback_dev
5172 * first, then loopback_dev, which cases all the non-loopback_dev
5173 * devices to fail to get a link-local address.
5175 * So, as a temporary fix, allocate the ipv6 structure for
5176 * loopback_dev first by hand.
5177 * Longer term, all of the dependencies ipv6 has upon the loopback
5178 * device and it being up should be removed.
5181 if (!ipv6_add_dev(init_net.loopback_dev))
5187 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5188 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5190 register_netdevice_notifier(&ipv6_dev_notf);
5194 err = rtnl_af_register(&inet6_ops);
5198 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5203 /* Only the first call to __rtnl_register can fail */
5204 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5205 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5206 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5207 inet6_dump_ifaddr, NULL);
5208 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5209 inet6_dump_ifmcaddr, NULL);
5210 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5211 inet6_dump_ifacaddr, NULL);
5212 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5213 inet6_netconf_dump_devconf, NULL);
5215 ipv6_addr_label_rtnl_register();
5219 rtnl_af_unregister(&inet6_ops);
5221 unregister_netdevice_notifier(&ipv6_dev_notf);
5223 unregister_pernet_subsys(&addrconf_ops);
5225 ipv6_addr_label_cleanup();
5230 void addrconf_cleanup(void)
5232 struct net_device *dev;
5235 unregister_netdevice_notifier(&ipv6_dev_notf);
5236 unregister_pernet_subsys(&addrconf_ops);
5237 ipv6_addr_label_cleanup();
5241 __rtnl_af_unregister(&inet6_ops);
5243 /* clean dev list */
5244 for_each_netdev(&init_net, dev) {
5245 if (__in6_dev_get(dev) == NULL)
5247 addrconf_ifdown(dev, 1);
5249 addrconf_ifdown(init_net.loopback_dev, 2);
5254 spin_lock_bh(&addrconf_hash_lock);
5255 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5256 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5257 spin_unlock_bh(&addrconf_hash_lock);
5259 del_timer(&addr_chk_timer);