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(fmt, ...) printk(fmt, ##__VA_ARGS__)
104 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
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,
180 .mldv1_unsolicited_report_interval = 10 * HZ,
181 .mldv2_unsolicited_report_interval = HZ,
183 .rtr_solicits = MAX_RTR_SOLICITATIONS,
184 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
185 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
186 #ifdef CONFIG_IPV6_PRIVACY
188 .temp_valid_lft = TEMP_VALID_LIFETIME,
189 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
190 .regen_max_retry = REGEN_MAX_RETRY,
191 .max_desync_factor = MAX_DESYNC_FACTOR,
193 .max_addresses = IPV6_MAX_ADDRESSES,
194 .accept_ra_defrtr = 1,
195 .accept_ra_pinfo = 1,
196 #ifdef CONFIG_IPV6_ROUTER_PREF
197 .accept_ra_rtr_pref = 1,
198 .rtr_probe_interval = 60 * HZ,
199 #ifdef CONFIG_IPV6_ROUTE_INFO
200 .accept_ra_rt_info_max_plen = 0,
204 .accept_source_route = 0, /* we do not accept RH0 by default. */
207 .suppress_frag_ndisc = 1,
210 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
212 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
213 .mtu6 = IPV6_MIN_MTU,
215 .accept_redirects = 1,
217 .force_mld_version = 0,
218 .mldv1_unsolicited_report_interval = 10 * HZ,
219 .mldv2_unsolicited_report_interval = HZ,
221 .rtr_solicits = MAX_RTR_SOLICITATIONS,
222 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
223 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
224 #ifdef CONFIG_IPV6_PRIVACY
226 .temp_valid_lft = TEMP_VALID_LIFETIME,
227 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
228 .regen_max_retry = REGEN_MAX_RETRY,
229 .max_desync_factor = MAX_DESYNC_FACTOR,
231 .max_addresses = IPV6_MAX_ADDRESSES,
232 .accept_ra_defrtr = 1,
233 .accept_ra_pinfo = 1,
234 #ifdef CONFIG_IPV6_ROUTER_PREF
235 .accept_ra_rtr_pref = 1,
236 .rtr_probe_interval = 60 * HZ,
237 #ifdef CONFIG_IPV6_ROUTE_INFO
238 .accept_ra_rt_info_max_plen = 0,
242 .accept_source_route = 0, /* we do not accept RH0 by default. */
245 .suppress_frag_ndisc = 1,
248 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
249 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
250 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
251 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
252 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
253 const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
254 const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
255 const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
257 /* Check if a valid qdisc is available */
258 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
260 return !qdisc_tx_is_noop(dev);
263 static void addrconf_del_rs_timer(struct inet6_dev *idev)
265 if (del_timer(&idev->rs_timer))
269 static void addrconf_del_dad_timer(struct inet6_ifaddr *ifp)
271 if (del_timer(&ifp->dad_timer))
275 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
278 if (!timer_pending(&idev->rs_timer))
280 mod_timer(&idev->rs_timer, jiffies + when);
283 static void addrconf_mod_dad_timer(struct inet6_ifaddr *ifp,
286 if (!timer_pending(&ifp->dad_timer))
288 mod_timer(&ifp->dad_timer, jiffies + when);
291 static int snmp6_alloc_dev(struct inet6_dev *idev)
293 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
294 sizeof(struct ipstats_mib),
295 __alignof__(struct ipstats_mib)) < 0)
297 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
299 if (!idev->stats.icmpv6dev)
301 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
303 if (!idev->stats.icmpv6msgdev)
309 kfree(idev->stats.icmpv6dev);
311 snmp_mib_free((void __percpu **)idev->stats.ipv6);
316 static void snmp6_free_dev(struct inet6_dev *idev)
318 kfree(idev->stats.icmpv6msgdev);
319 kfree(idev->stats.icmpv6dev);
320 snmp_mib_free((void __percpu **)idev->stats.ipv6);
323 /* Nobody refers to this device, we may destroy it. */
325 void in6_dev_finish_destroy(struct inet6_dev *idev)
327 struct net_device *dev = idev->dev;
329 WARN_ON(!list_empty(&idev->addr_list));
330 WARN_ON(idev->mc_list != NULL);
331 WARN_ON(timer_pending(&idev->rs_timer));
333 #ifdef NET_REFCNT_DEBUG
334 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
338 pr_warn("Freeing alive inet6 device %p\n", idev);
341 snmp6_free_dev(idev);
342 kfree_rcu(idev, rcu);
344 EXPORT_SYMBOL(in6_dev_finish_destroy);
346 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
348 struct inet6_dev *ndev;
352 if (dev->mtu < IPV6_MIN_MTU)
355 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
360 rwlock_init(&ndev->lock);
362 INIT_LIST_HEAD(&ndev->addr_list);
363 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
364 (unsigned long)ndev);
365 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
366 ndev->cnf.mtu6 = dev->mtu;
367 ndev->cnf.sysctl = NULL;
368 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
369 if (ndev->nd_parms == NULL) {
373 if (ndev->cnf.forwarding)
374 dev_disable_lro(dev);
375 /* We refer to the device */
378 if (snmp6_alloc_dev(ndev) < 0) {
380 "%s: cannot allocate memory for statistics; dev=%s.\n",
381 __func__, dev->name);
382 neigh_parms_release(&nd_tbl, ndev->nd_parms);
388 if (snmp6_register_dev(ndev) < 0) {
390 "%s: cannot create /proc/net/dev_snmp6/%s\n",
391 __func__, dev->name);
392 neigh_parms_release(&nd_tbl, ndev->nd_parms);
394 in6_dev_finish_destroy(ndev);
398 /* One reference from device. We must do this before
399 * we invoke __ipv6_regen_rndid().
403 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
404 ndev->cnf.accept_dad = -1;
406 #if IS_ENABLED(CONFIG_IPV6_SIT)
407 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
408 pr_info("%s: Disabled Multicast RS\n", dev->name);
409 ndev->cnf.rtr_solicits = 0;
413 #ifdef CONFIG_IPV6_PRIVACY
414 INIT_LIST_HEAD(&ndev->tempaddr_list);
415 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
416 if ((dev->flags&IFF_LOOPBACK) ||
417 dev->type == ARPHRD_TUNNEL ||
418 dev->type == ARPHRD_TUNNEL6 ||
419 dev->type == ARPHRD_SIT ||
420 dev->type == ARPHRD_NONE) {
421 ndev->cnf.use_tempaddr = -1;
424 ipv6_regen_rndid((unsigned long) ndev);
427 ndev->token = in6addr_any;
429 if (netif_running(dev) && addrconf_qdisc_ok(dev))
430 ndev->if_flags |= IF_READY;
432 ipv6_mc_init_dev(ndev);
433 ndev->tstamp = jiffies;
434 addrconf_sysctl_register(ndev);
435 /* protected by rtnl_lock */
436 rcu_assign_pointer(dev->ip6_ptr, ndev);
438 /* Join interface-local all-node multicast group */
439 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
441 /* Join all-node multicast group */
442 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
444 /* Join all-router multicast group if forwarding is set */
445 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
446 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
451 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
453 struct inet6_dev *idev;
457 idev = __in6_dev_get(dev);
459 idev = ipv6_add_dev(dev);
464 if (dev->flags&IFF_UP)
469 static int inet6_netconf_msgsize_devconf(int type)
471 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
472 + nla_total_size(4); /* NETCONFA_IFINDEX */
474 /* type -1 is used for ALL */
475 if (type == -1 || type == NETCONFA_FORWARDING)
476 size += nla_total_size(4);
477 #ifdef CONFIG_IPV6_MROUTE
478 if (type == -1 || type == NETCONFA_MC_FORWARDING)
479 size += nla_total_size(4);
485 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
486 struct ipv6_devconf *devconf, u32 portid,
487 u32 seq, int event, unsigned int flags,
490 struct nlmsghdr *nlh;
491 struct netconfmsg *ncm;
493 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
498 ncm = nlmsg_data(nlh);
499 ncm->ncm_family = AF_INET6;
501 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
502 goto nla_put_failure;
504 /* type -1 is used for ALL */
505 if ((type == -1 || type == NETCONFA_FORWARDING) &&
506 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
507 goto nla_put_failure;
508 #ifdef CONFIG_IPV6_MROUTE
509 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
510 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
511 devconf->mc_forwarding) < 0)
512 goto nla_put_failure;
514 return nlmsg_end(skb, nlh);
517 nlmsg_cancel(skb, nlh);
521 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
522 struct ipv6_devconf *devconf)
527 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
531 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
532 RTM_NEWNETCONF, 0, type);
534 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
535 WARN_ON(err == -EMSGSIZE);
539 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
542 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
545 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
546 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
547 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
550 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
551 struct nlmsghdr *nlh)
553 struct net *net = sock_net(in_skb->sk);
554 struct nlattr *tb[NETCONFA_MAX+1];
555 struct netconfmsg *ncm;
557 struct ipv6_devconf *devconf;
558 struct inet6_dev *in6_dev;
559 struct net_device *dev;
563 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
564 devconf_ipv6_policy);
569 if (!tb[NETCONFA_IFINDEX])
572 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
574 case NETCONFA_IFINDEX_ALL:
575 devconf = net->ipv6.devconf_all;
577 case NETCONFA_IFINDEX_DEFAULT:
578 devconf = net->ipv6.devconf_dflt;
581 dev = __dev_get_by_index(net, ifindex);
584 in6_dev = __in6_dev_get(dev);
587 devconf = &in6_dev->cnf;
592 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
596 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
597 NETLINK_CB(in_skb).portid,
598 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
601 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
602 WARN_ON(err == -EMSGSIZE);
606 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
611 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
612 struct netlink_callback *cb)
614 struct net *net = sock_net(skb->sk);
617 struct net_device *dev;
618 struct inet6_dev *idev;
619 struct hlist_head *head;
622 s_idx = idx = cb->args[1];
624 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
626 head = &net->dev_index_head[h];
628 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
630 hlist_for_each_entry_rcu(dev, head, index_hlist) {
633 idev = __in6_dev_get(dev);
637 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
639 NETLINK_CB(cb->skb).portid,
647 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
653 if (h == NETDEV_HASHENTRIES) {
654 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
655 net->ipv6.devconf_all,
656 NETLINK_CB(cb->skb).portid,
658 RTM_NEWNETCONF, NLM_F_MULTI,
664 if (h == NETDEV_HASHENTRIES + 1) {
665 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
666 net->ipv6.devconf_dflt,
667 NETLINK_CB(cb->skb).portid,
669 RTM_NEWNETCONF, NLM_F_MULTI,
683 static void dev_forward_change(struct inet6_dev *idev)
685 struct net_device *dev;
686 struct inet6_ifaddr *ifa;
691 if (idev->cnf.forwarding)
692 dev_disable_lro(dev);
693 if (dev->flags & IFF_MULTICAST) {
694 if (idev->cnf.forwarding) {
695 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
696 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
697 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
699 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
700 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
701 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
705 list_for_each_entry(ifa, &idev->addr_list, if_list) {
706 if (ifa->flags&IFA_F_TENTATIVE)
708 if (idev->cnf.forwarding)
709 addrconf_join_anycast(ifa);
711 addrconf_leave_anycast(ifa);
713 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
714 dev->ifindex, &idev->cnf);
718 static void addrconf_forward_change(struct net *net, __s32 newf)
720 struct net_device *dev;
721 struct inet6_dev *idev;
723 for_each_netdev(net, dev) {
724 idev = __in6_dev_get(dev);
726 int changed = (!idev->cnf.forwarding) ^ (!newf);
727 idev->cnf.forwarding = newf;
729 dev_forward_change(idev);
734 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
740 return restart_syscall();
742 net = (struct net *)table->extra2;
746 if (p == &net->ipv6.devconf_dflt->forwarding) {
747 if ((!newf) ^ (!old))
748 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
749 NETCONFA_IFINDEX_DEFAULT,
750 net->ipv6.devconf_dflt);
755 if (p == &net->ipv6.devconf_all->forwarding) {
756 net->ipv6.devconf_dflt->forwarding = newf;
757 addrconf_forward_change(net, newf);
758 if ((!newf) ^ (!old))
759 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
760 NETCONFA_IFINDEX_ALL,
761 net->ipv6.devconf_all);
762 } else if ((!newf) ^ (!old))
763 dev_forward_change((struct inet6_dev *)table->extra1);
767 rt6_purge_dflt_routers(net);
772 /* Nobody refers to this ifaddr, destroy it */
773 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
775 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
777 #ifdef NET_REFCNT_DEBUG
778 pr_debug("%s\n", __func__);
781 in6_dev_put(ifp->idev);
783 if (del_timer(&ifp->dad_timer))
784 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
786 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
787 pr_warn("Freeing alive inet6 address %p\n", ifp);
796 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
799 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
802 * Each device address list is sorted in order of scope -
803 * global before linklocal.
805 list_for_each(p, &idev->addr_list) {
806 struct inet6_ifaddr *ifa
807 = list_entry(p, struct inet6_ifaddr, if_list);
808 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
812 list_add_tail(&ifp->if_list, p);
815 static u32 inet6_addr_hash(const struct in6_addr *addr)
817 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
820 /* On success it returns ifp with increased reference count */
822 static struct inet6_ifaddr *
823 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
824 const struct in6_addr *peer_addr, int pfxlen,
825 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
827 struct inet6_ifaddr *ifa = NULL;
831 int addr_type = ipv6_addr_type(addr);
833 if (addr_type == IPV6_ADDR_ANY ||
834 addr_type & IPV6_ADDR_MULTICAST ||
835 (!(idev->dev->flags & IFF_LOOPBACK) &&
836 addr_type & IPV6_ADDR_LOOPBACK))
837 return ERR_PTR(-EADDRNOTAVAIL);
841 err = -ENODEV; /*XXX*/
845 if (idev->cnf.disable_ipv6) {
850 spin_lock(&addrconf_hash_lock);
852 /* Ignore adding duplicate addresses on an interface */
853 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
854 ADBG("ipv6_add_addr: already assigned\n");
859 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
862 ADBG("ipv6_add_addr: malloc failed\n");
867 rt = addrconf_dst_alloc(idev, addr, false);
875 ifa->peer_addr = *peer_addr;
877 spin_lock_init(&ifa->lock);
878 spin_lock_init(&ifa->state_lock);
879 setup_timer(&ifa->dad_timer, addrconf_dad_timer,
881 INIT_HLIST_NODE(&ifa->addr_lst);
883 ifa->prefix_len = pfxlen;
884 ifa->flags = flags | IFA_F_TENTATIVE;
885 ifa->valid_lft = valid_lft;
886 ifa->prefered_lft = prefered_lft;
887 ifa->cstamp = ifa->tstamp = jiffies;
888 ifa->tokenized = false;
897 /* Add to big hash table */
898 hash = inet6_addr_hash(addr);
900 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
901 spin_unlock(&addrconf_hash_lock);
903 write_lock(&idev->lock);
904 /* Add to inet6_dev unicast addr list. */
905 ipv6_link_dev_addr(idev, ifa);
907 #ifdef CONFIG_IPV6_PRIVACY
908 if (ifa->flags&IFA_F_TEMPORARY) {
909 list_add(&ifa->tmp_list, &idev->tempaddr_list);
915 write_unlock(&idev->lock);
917 rcu_read_unlock_bh();
919 if (likely(err == 0))
920 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
928 spin_unlock(&addrconf_hash_lock);
932 /* This function wants to get referenced ifp and releases it before return */
934 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
936 struct inet6_ifaddr *ifa, *ifn;
937 struct inet6_dev *idev = ifp->idev;
939 int deleted = 0, onlink = 0;
940 unsigned long expires = jiffies;
942 spin_lock_bh(&ifp->state_lock);
944 ifp->state = INET6_IFADDR_STATE_DEAD;
945 spin_unlock_bh(&ifp->state_lock);
947 if (state == INET6_IFADDR_STATE_DEAD)
950 spin_lock_bh(&addrconf_hash_lock);
951 hlist_del_init_rcu(&ifp->addr_lst);
952 spin_unlock_bh(&addrconf_hash_lock);
954 write_lock_bh(&idev->lock);
955 #ifdef CONFIG_IPV6_PRIVACY
956 if (ifp->flags&IFA_F_TEMPORARY) {
957 list_del(&ifp->tmp_list);
959 in6_ifa_put(ifp->ifpub);
966 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
968 list_del_init(&ifp->if_list);
971 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
975 } else if (ifp->flags & IFA_F_PERMANENT) {
976 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
978 if (ifa->flags & IFA_F_PERMANENT) {
983 unsigned long lifetime;
988 spin_lock(&ifa->lock);
990 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
992 * Note: Because this address is
993 * not permanent, lifetime <
994 * LONG_MAX / HZ here.
996 if (time_before(expires,
997 ifa->tstamp + lifetime * HZ))
998 expires = ifa->tstamp + lifetime * HZ;
999 spin_unlock(&ifa->lock);
1004 write_unlock_bh(&idev->lock);
1006 addrconf_del_dad_timer(ifp);
1008 ipv6_ifa_notify(RTM_DELADDR, ifp);
1010 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1013 * Purge or update corresponding prefix
1015 * 1) we don't purge prefix here if address was not permanent.
1016 * prefix is managed by its own lifetime.
1017 * 2) if there're no addresses, delete prefix.
1018 * 3) if there're still other permanent address(es),
1019 * corresponding prefix is still permanent.
1020 * 4) otherwise, update prefix lifetime to the
1021 * longest valid lifetime among the corresponding
1022 * addresses on the device.
1023 * Note: subsequent RA will update lifetime.
1027 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
1028 struct in6_addr prefix;
1029 struct rt6_info *rt;
1031 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
1033 rt = addrconf_get_prefix_route(&prefix,
1036 0, RTF_GATEWAY | RTF_DEFAULT);
1042 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
1043 rt6_set_expires(rt, expires);
1049 /* clean up prefsrc entries */
1050 rt6_remove_prefsrc(ifp);
1055 #ifdef CONFIG_IPV6_PRIVACY
1056 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1058 struct inet6_dev *idev = ifp->idev;
1059 struct in6_addr addr, *tmpaddr;
1060 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1061 unsigned long regen_advance;
1066 unsigned long now = jiffies;
1068 write_lock(&idev->lock);
1070 spin_lock_bh(&ift->lock);
1071 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1072 spin_unlock_bh(&ift->lock);
1079 if (idev->cnf.use_tempaddr <= 0) {
1080 write_unlock(&idev->lock);
1081 pr_info("%s: use_tempaddr is disabled\n", __func__);
1086 spin_lock_bh(&ifp->lock);
1087 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1088 idev->cnf.use_tempaddr = -1; /*XXX*/
1089 spin_unlock_bh(&ifp->lock);
1090 write_unlock(&idev->lock);
1091 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1098 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1099 __ipv6_try_regen_rndid(idev, tmpaddr);
1100 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1101 age = (now - ifp->tstamp) / HZ;
1102 tmp_valid_lft = min_t(__u32,
1104 idev->cnf.temp_valid_lft + age);
1105 tmp_prefered_lft = min_t(__u32,
1107 idev->cnf.temp_prefered_lft + age -
1108 idev->cnf.max_desync_factor);
1109 tmp_plen = ifp->prefix_len;
1110 max_addresses = idev->cnf.max_addresses;
1111 tmp_tstamp = ifp->tstamp;
1112 spin_unlock_bh(&ifp->lock);
1114 regen_advance = idev->cnf.regen_max_retry *
1115 idev->cnf.dad_transmits *
1116 idev->nd_parms->retrans_time / HZ;
1117 write_unlock(&idev->lock);
1119 /* A temporary address is created only if this calculated Preferred
1120 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1121 * an implementation must not create a temporary address with a zero
1122 * Preferred Lifetime.
1124 if (tmp_prefered_lft <= regen_advance) {
1131 addr_flags = IFA_F_TEMPORARY;
1132 /* set in addrconf_prefix_rcv() */
1133 if (ifp->flags & IFA_F_OPTIMISTIC)
1134 addr_flags |= IFA_F_OPTIMISTIC;
1136 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1137 ipv6_addr_scope(&addr), addr_flags,
1138 tmp_valid_lft, tmp_prefered_lft);
1142 pr_info("%s: retry temporary address regeneration\n", __func__);
1144 write_lock(&idev->lock);
1148 spin_lock_bh(&ift->lock);
1151 ift->tstamp = tmp_tstamp;
1152 spin_unlock_bh(&ift->lock);
1154 addrconf_dad_start(ift);
1163 * Choose an appropriate source address (RFC3484)
1166 IPV6_SADDR_RULE_INIT = 0,
1167 IPV6_SADDR_RULE_LOCAL,
1168 IPV6_SADDR_RULE_SCOPE,
1169 IPV6_SADDR_RULE_PREFERRED,
1170 #ifdef CONFIG_IPV6_MIP6
1171 IPV6_SADDR_RULE_HOA,
1173 IPV6_SADDR_RULE_OIF,
1174 IPV6_SADDR_RULE_LABEL,
1175 #ifdef CONFIG_IPV6_PRIVACY
1176 IPV6_SADDR_RULE_PRIVACY,
1178 IPV6_SADDR_RULE_ORCHID,
1179 IPV6_SADDR_RULE_PREFIX,
1183 struct ipv6_saddr_score {
1186 struct inet6_ifaddr *ifa;
1187 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1192 struct ipv6_saddr_dst {
1193 const struct in6_addr *addr;
1200 static inline int ipv6_saddr_preferred(int type)
1202 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1207 static int ipv6_get_saddr_eval(struct net *net,
1208 struct ipv6_saddr_score *score,
1209 struct ipv6_saddr_dst *dst,
1214 if (i <= score->rule) {
1216 case IPV6_SADDR_RULE_SCOPE:
1217 ret = score->scopedist;
1219 case IPV6_SADDR_RULE_PREFIX:
1220 ret = score->matchlen;
1223 ret = !!test_bit(i, score->scorebits);
1229 case IPV6_SADDR_RULE_INIT:
1230 /* Rule 0: remember if hiscore is not ready yet */
1233 case IPV6_SADDR_RULE_LOCAL:
1234 /* Rule 1: Prefer same address */
1235 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1237 case IPV6_SADDR_RULE_SCOPE:
1238 /* Rule 2: Prefer appropriate scope
1243 * ---+--+-+---> scope
1245 * | d is scope of the destination.
1247 * | \ <- smaller scope is better if
1248 * B-15 | \ if scope is enough for destinaion.
1249 * | ret = B - scope (-1 <= scope >= d <= 15).
1251 * |/ <- greater is better
1252 * -C / if scope is not enough for destination.
1253 * /| ret = scope - C (-1 <= d < scope <= 15).
1255 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1256 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1257 * Assume B = 0 and we get C > 29.
1259 ret = __ipv6_addr_src_scope(score->addr_type);
1260 if (ret >= dst->scope)
1263 ret -= 128; /* 30 is enough */
1264 score->scopedist = ret;
1266 case IPV6_SADDR_RULE_PREFERRED:
1267 /* Rule 3: Avoid deprecated and optimistic addresses */
1268 ret = ipv6_saddr_preferred(score->addr_type) ||
1269 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1271 #ifdef CONFIG_IPV6_MIP6
1272 case IPV6_SADDR_RULE_HOA:
1274 /* Rule 4: Prefer home address */
1275 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1276 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1280 case IPV6_SADDR_RULE_OIF:
1281 /* Rule 5: Prefer outgoing interface */
1282 ret = (!dst->ifindex ||
1283 dst->ifindex == score->ifa->idev->dev->ifindex);
1285 case IPV6_SADDR_RULE_LABEL:
1286 /* Rule 6: Prefer matching label */
1287 ret = ipv6_addr_label(net,
1288 &score->ifa->addr, score->addr_type,
1289 score->ifa->idev->dev->ifindex) == dst->label;
1291 #ifdef CONFIG_IPV6_PRIVACY
1292 case IPV6_SADDR_RULE_PRIVACY:
1294 /* Rule 7: Prefer public address
1295 * Note: prefer temporary address if use_tempaddr >= 2
1297 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1298 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1299 score->ifa->idev->cnf.use_tempaddr >= 2;
1300 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1304 case IPV6_SADDR_RULE_ORCHID:
1305 /* Rule 8-: Prefer ORCHID vs ORCHID or
1306 * non-ORCHID vs non-ORCHID
1308 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1309 ipv6_addr_orchid(dst->addr));
1311 case IPV6_SADDR_RULE_PREFIX:
1312 /* Rule 8: Use longest matching prefix */
1313 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1314 if (ret > score->ifa->prefix_len)
1315 ret = score->ifa->prefix_len;
1316 score->matchlen = ret;
1323 __set_bit(i, score->scorebits);
1329 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1330 const struct in6_addr *daddr, unsigned int prefs,
1331 struct in6_addr *saddr)
1333 struct ipv6_saddr_score scores[2],
1334 *score = &scores[0], *hiscore = &scores[1];
1335 struct ipv6_saddr_dst dst;
1336 struct net_device *dev;
1339 dst_type = __ipv6_addr_type(daddr);
1341 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1342 dst.scope = __ipv6_addr_src_scope(dst_type);
1343 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1347 hiscore->ifa = NULL;
1351 for_each_netdev_rcu(net, dev) {
1352 struct inet6_dev *idev;
1354 /* Candidate Source Address (section 4)
1355 * - multicast and link-local destination address,
1356 * the set of candidate source address MUST only
1357 * include addresses assigned to interfaces
1358 * belonging to the same link as the outgoing
1360 * (- For site-local destination addresses, the
1361 * set of candidate source addresses MUST only
1362 * include addresses assigned to interfaces
1363 * belonging to the same site as the outgoing
1366 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1367 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1368 dst.ifindex && dev->ifindex != dst.ifindex)
1371 idev = __in6_dev_get(dev);
1375 read_lock_bh(&idev->lock);
1376 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1380 * - Tentative Address (RFC2462 section 5.4)
1381 * - A tentative address is not considered
1382 * "assigned to an interface" in the traditional
1383 * sense, unless it is also flagged as optimistic.
1384 * - Candidate Source Address (section 4)
1385 * - In any case, anycast addresses, multicast
1386 * addresses, and the unspecified address MUST
1387 * NOT be included in a candidate set.
1389 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1390 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1393 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1395 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1396 score->addr_type & IPV6_ADDR_MULTICAST)) {
1397 LIMIT_NETDEBUG(KERN_DEBUG
1398 "ADDRCONF: unspecified / multicast address "
1399 "assigned as unicast address on %s",
1405 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1407 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1408 int minihiscore, miniscore;
1410 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1411 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1413 if (minihiscore > miniscore) {
1414 if (i == IPV6_SADDR_RULE_SCOPE &&
1415 score->scopedist > 0) {
1418 * each remaining entry
1419 * has too small (not enough)
1420 * scope, because ifa entries
1421 * are sorted by their scope
1427 } else if (minihiscore < miniscore) {
1429 in6_ifa_put(hiscore->ifa);
1431 in6_ifa_hold(score->ifa);
1433 swap(hiscore, score);
1435 /* restore our iterator */
1436 score->ifa = hiscore->ifa;
1443 read_unlock_bh(&idev->lock);
1448 return -EADDRNOTAVAIL;
1450 *saddr = hiscore->ifa->addr;
1451 in6_ifa_put(hiscore->ifa);
1454 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1456 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1457 unsigned char banned_flags)
1459 struct inet6_ifaddr *ifp;
1460 int err = -EADDRNOTAVAIL;
1462 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1463 if (ifp->scope == IFA_LINK &&
1464 !(ifp->flags & banned_flags)) {
1473 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1474 unsigned char banned_flags)
1476 struct inet6_dev *idev;
1477 int err = -EADDRNOTAVAIL;
1480 idev = __in6_dev_get(dev);
1482 read_lock_bh(&idev->lock);
1483 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1484 read_unlock_bh(&idev->lock);
1490 static int ipv6_count_addresses(struct inet6_dev *idev)
1493 struct inet6_ifaddr *ifp;
1495 read_lock_bh(&idev->lock);
1496 list_for_each_entry(ifp, &idev->addr_list, if_list)
1498 read_unlock_bh(&idev->lock);
1502 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1503 const struct net_device *dev, int strict)
1505 struct inet6_ifaddr *ifp;
1506 unsigned int hash = inet6_addr_hash(addr);
1509 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1510 if (!net_eq(dev_net(ifp->idev->dev), net))
1512 if (ipv6_addr_equal(&ifp->addr, addr) &&
1513 !(ifp->flags&IFA_F_TENTATIVE) &&
1514 (dev == NULL || ifp->idev->dev == dev ||
1515 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1516 rcu_read_unlock_bh();
1521 rcu_read_unlock_bh();
1524 EXPORT_SYMBOL(ipv6_chk_addr);
1526 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1527 struct net_device *dev)
1529 unsigned int hash = inet6_addr_hash(addr);
1530 struct inet6_ifaddr *ifp;
1532 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1533 if (!net_eq(dev_net(ifp->idev->dev), net))
1535 if (ipv6_addr_equal(&ifp->addr, addr)) {
1536 if (dev == NULL || ifp->idev->dev == dev)
1543 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1545 struct inet6_dev *idev;
1546 struct inet6_ifaddr *ifa;
1551 idev = __in6_dev_get(dev);
1553 read_lock_bh(&idev->lock);
1554 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1555 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1560 read_unlock_bh(&idev->lock);
1565 EXPORT_SYMBOL(ipv6_chk_prefix);
1567 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1568 struct net_device *dev, int strict)
1570 struct inet6_ifaddr *ifp, *result = NULL;
1571 unsigned int hash = inet6_addr_hash(addr);
1574 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1575 if (!net_eq(dev_net(ifp->idev->dev), net))
1577 if (ipv6_addr_equal(&ifp->addr, addr)) {
1578 if (dev == NULL || ifp->idev->dev == dev ||
1579 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1586 rcu_read_unlock_bh();
1591 /* Gets referenced address, destroys ifaddr */
1593 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1595 if (ifp->flags&IFA_F_PERMANENT) {
1596 spin_lock_bh(&ifp->lock);
1597 addrconf_del_dad_timer(ifp);
1598 ifp->flags |= IFA_F_TENTATIVE;
1600 ifp->flags |= IFA_F_DADFAILED;
1601 spin_unlock_bh(&ifp->lock);
1603 ipv6_ifa_notify(0, ifp);
1605 #ifdef CONFIG_IPV6_PRIVACY
1606 } else if (ifp->flags&IFA_F_TEMPORARY) {
1607 struct inet6_ifaddr *ifpub;
1608 spin_lock_bh(&ifp->lock);
1611 in6_ifa_hold(ifpub);
1612 spin_unlock_bh(&ifp->lock);
1613 ipv6_create_tempaddr(ifpub, ifp);
1616 spin_unlock_bh(&ifp->lock);
1624 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1628 spin_lock(&ifp->state_lock);
1629 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1630 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1633 spin_unlock(&ifp->state_lock);
1638 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1640 struct inet6_dev *idev = ifp->idev;
1642 if (addrconf_dad_end(ifp)) {
1647 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1648 ifp->idev->dev->name, &ifp->addr);
1650 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1651 struct in6_addr addr;
1653 addr.s6_addr32[0] = htonl(0xfe800000);
1654 addr.s6_addr32[1] = 0;
1656 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1657 ipv6_addr_equal(&ifp->addr, &addr)) {
1658 /* DAD failed for link-local based on MAC address */
1659 idev->cnf.disable_ipv6 = 1;
1661 pr_info("%s: IPv6 being disabled!\n",
1662 ifp->idev->dev->name);
1666 addrconf_dad_stop(ifp, 1);
1669 /* Join to solicited addr multicast group. */
1671 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1673 struct in6_addr maddr;
1675 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1678 addrconf_addr_solict_mult(addr, &maddr);
1679 ipv6_dev_mc_inc(dev, &maddr);
1682 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1684 struct in6_addr maddr;
1686 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1689 addrconf_addr_solict_mult(addr, &maddr);
1690 __ipv6_dev_mc_dec(idev, &maddr);
1693 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1695 struct in6_addr addr;
1696 if (ifp->prefix_len == 127) /* RFC 6164 */
1698 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1699 if (ipv6_addr_any(&addr))
1701 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1704 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1706 struct in6_addr addr;
1707 if (ifp->prefix_len == 127) /* RFC 6164 */
1709 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1710 if (ipv6_addr_any(&addr))
1712 __ipv6_dev_ac_dec(ifp->idev, &addr);
1715 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1717 if (dev->addr_len != ETH_ALEN)
1719 memcpy(eui, dev->dev_addr, 3);
1720 memcpy(eui + 5, dev->dev_addr + 3, 3);
1723 * The zSeries OSA network cards can be shared among various
1724 * OS instances, but the OSA cards have only one MAC address.
1725 * This leads to duplicate address conflicts in conjunction
1726 * with IPv6 if more than one instance uses the same card.
1728 * The driver for these cards can deliver a unique 16-bit
1729 * identifier for each instance sharing the same card. It is
1730 * placed instead of 0xFFFE in the interface identifier. The
1731 * "u" bit of the interface identifier is not inverted in this
1732 * case. Hence the resulting interface identifier has local
1733 * scope according to RFC2373.
1736 eui[3] = (dev->dev_id >> 8) & 0xFF;
1737 eui[4] = dev->dev_id & 0xFF;
1746 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1748 if (dev->addr_len != IEEE802154_ADDR_LEN)
1750 memcpy(eui, dev->dev_addr, 8);
1755 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1757 union fwnet_hwaddr *ha;
1759 if (dev->addr_len != FWNET_ALEN)
1762 ha = (union fwnet_hwaddr *)dev->dev_addr;
1764 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1769 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1771 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1772 if (dev->addr_len != ARCNET_ALEN)
1775 eui[7] = *(u8 *)dev->dev_addr;
1779 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1781 if (dev->addr_len != INFINIBAND_ALEN)
1783 memcpy(eui, dev->dev_addr + 12, 8);
1788 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1792 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1793 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1794 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1795 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1796 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1797 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1801 memcpy(eui + 4, &addr, 4);
1805 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1807 if (dev->priv_flags & IFF_ISATAP)
1808 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1812 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1814 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1817 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1819 memcpy(eui, dev->perm_addr, 3);
1820 memcpy(eui + 5, dev->perm_addr + 3, 3);
1827 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1829 switch (dev->type) {
1832 return addrconf_ifid_eui48(eui, dev);
1834 return addrconf_ifid_arcnet(eui, dev);
1835 case ARPHRD_INFINIBAND:
1836 return addrconf_ifid_infiniband(eui, dev);
1838 return addrconf_ifid_sit(eui, dev);
1840 return addrconf_ifid_gre(eui, dev);
1841 case ARPHRD_IEEE802154:
1842 return addrconf_ifid_eui64(eui, dev);
1843 case ARPHRD_IEEE1394:
1844 return addrconf_ifid_ieee1394(eui, dev);
1845 case ARPHRD_TUNNEL6:
1846 return addrconf_ifid_ip6tnl(eui, dev);
1851 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1854 struct inet6_ifaddr *ifp;
1856 read_lock_bh(&idev->lock);
1857 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1858 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1859 memcpy(eui, ifp->addr.s6_addr+8, 8);
1864 read_unlock_bh(&idev->lock);
1868 #ifdef CONFIG_IPV6_PRIVACY
1869 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1870 static void __ipv6_regen_rndid(struct inet6_dev *idev)
1873 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1874 idev->rndid[0] &= ~0x02;
1877 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1878 * check if generated address is not inappropriate
1880 * - Reserved subnet anycast (RFC 2526)
1881 * 11111101 11....11 1xxxxxxx
1882 * - ISATAP (RFC4214) 6.1
1883 * 00-00-5E-FE-xx-xx-xx-xx
1885 * - XXX: already assigned to an address on the device
1887 if (idev->rndid[0] == 0xfd &&
1888 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1889 (idev->rndid[7]&0x80))
1891 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1892 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1894 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1899 static void ipv6_regen_rndid(unsigned long data)
1901 struct inet6_dev *idev = (struct inet6_dev *) data;
1902 unsigned long expires;
1905 write_lock_bh(&idev->lock);
1910 __ipv6_regen_rndid(idev);
1913 idev->cnf.temp_prefered_lft * HZ -
1914 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1915 idev->cnf.max_desync_factor * HZ;
1916 if (time_before(expires, jiffies)) {
1917 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1918 __func__, idev->dev->name);
1922 if (!mod_timer(&idev->regen_timer, expires))
1926 write_unlock_bh(&idev->lock);
1927 rcu_read_unlock_bh();
1931 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
1933 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1934 __ipv6_regen_rndid(idev);
1943 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1944 unsigned long expires, u32 flags)
1946 struct fib6_config cfg = {
1947 .fc_table = RT6_TABLE_PREFIX,
1948 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1949 .fc_ifindex = dev->ifindex,
1950 .fc_expires = expires,
1952 .fc_flags = RTF_UP | flags,
1953 .fc_nlinfo.nl_net = dev_net(dev),
1954 .fc_protocol = RTPROT_KERNEL,
1959 /* Prevent useless cloning on PtP SIT.
1960 This thing is done here expecting that the whole
1961 class of non-broadcast devices need not cloning.
1963 #if IS_ENABLED(CONFIG_IPV6_SIT)
1964 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1965 cfg.fc_flags |= RTF_NONEXTHOP;
1968 ip6_route_add(&cfg);
1972 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1974 const struct net_device *dev,
1975 u32 flags, u32 noflags)
1977 struct fib6_node *fn;
1978 struct rt6_info *rt = NULL;
1979 struct fib6_table *table;
1981 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1985 read_lock_bh(&table->tb6_lock);
1986 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1989 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1990 if (rt->dst.dev->ifindex != dev->ifindex)
1992 if ((rt->rt6i_flags & flags) != flags)
1994 if ((rt->rt6i_flags & noflags) != 0)
2000 read_unlock_bh(&table->tb6_lock);
2005 /* Create "default" multicast route to the interface */
2007 static void addrconf_add_mroute(struct net_device *dev)
2009 struct fib6_config cfg = {
2010 .fc_table = RT6_TABLE_LOCAL,
2011 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2012 .fc_ifindex = dev->ifindex,
2015 .fc_nlinfo.nl_net = dev_net(dev),
2018 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2020 ip6_route_add(&cfg);
2023 #if IS_ENABLED(CONFIG_IPV6_SIT)
2024 static void sit_route_add(struct net_device *dev)
2026 struct fib6_config cfg = {
2027 .fc_table = RT6_TABLE_MAIN,
2028 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2029 .fc_ifindex = dev->ifindex,
2031 .fc_flags = RTF_UP | RTF_NONEXTHOP,
2032 .fc_nlinfo.nl_net = dev_net(dev),
2035 /* prefix length - 96 bits "::d.d.d.d" */
2036 ip6_route_add(&cfg);
2040 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2042 struct inet6_dev *idev;
2046 idev = ipv6_find_idev(dev);
2048 return ERR_PTR(-ENOBUFS);
2050 if (idev->cnf.disable_ipv6)
2051 return ERR_PTR(-EACCES);
2053 /* Add default multicast route */
2054 if (!(dev->flags & IFF_LOOPBACK))
2055 addrconf_add_mroute(dev);
2060 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2062 struct prefix_info *pinfo;
2066 struct inet6_dev *in6_dev;
2067 struct net *net = dev_net(dev);
2069 pinfo = (struct prefix_info *) opt;
2071 if (len < sizeof(struct prefix_info)) {
2072 ADBG("addrconf: prefix option too short\n");
2077 * Validation checks ([ADDRCONF], page 19)
2080 addr_type = ipv6_addr_type(&pinfo->prefix);
2082 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2085 valid_lft = ntohl(pinfo->valid);
2086 prefered_lft = ntohl(pinfo->prefered);
2088 if (prefered_lft > valid_lft) {
2089 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2093 in6_dev = in6_dev_get(dev);
2095 if (in6_dev == NULL) {
2096 net_dbg_ratelimited("addrconf: device %s not configured\n",
2102 * Two things going on here:
2103 * 1) Add routes for on-link prefixes
2104 * 2) Configure prefixes with the auto flag set
2107 if (pinfo->onlink) {
2108 struct rt6_info *rt;
2109 unsigned long rt_expires;
2111 /* Avoid arithmetic overflow. Really, we could
2112 * save rt_expires in seconds, likely valid_lft,
2113 * but it would require division in fib gc, that it
2117 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2119 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2121 if (addrconf_finite_timeout(rt_expires))
2124 rt = addrconf_get_prefix_route(&pinfo->prefix,
2127 RTF_ADDRCONF | RTF_PREFIX_RT,
2128 RTF_GATEWAY | RTF_DEFAULT);
2131 /* Autoconf prefix route */
2132 if (valid_lft == 0) {
2135 } else if (addrconf_finite_timeout(rt_expires)) {
2137 rt6_set_expires(rt, jiffies + rt_expires);
2139 rt6_clean_expires(rt);
2141 } else if (valid_lft) {
2142 clock_t expires = 0;
2143 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2144 if (addrconf_finite_timeout(rt_expires)) {
2146 flags |= RTF_EXPIRES;
2147 expires = jiffies_to_clock_t(rt_expires);
2149 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2150 dev, expires, flags);
2155 /* Try to figure out our local address for this prefix */
2157 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2158 struct inet6_ifaddr *ifp;
2159 struct in6_addr addr;
2160 int create = 0, update_lft = 0;
2161 bool tokenized = false;
2163 if (pinfo->prefix_len == 64) {
2164 memcpy(&addr, &pinfo->prefix, 8);
2166 if (!ipv6_addr_any(&in6_dev->token)) {
2167 read_lock_bh(&in6_dev->lock);
2168 memcpy(addr.s6_addr + 8,
2169 in6_dev->token.s6_addr + 8, 8);
2170 read_unlock_bh(&in6_dev->lock);
2172 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2173 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2174 in6_dev_put(in6_dev);
2179 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2181 in6_dev_put(in6_dev);
2186 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2188 if (ifp == NULL && valid_lft) {
2189 int max_addresses = in6_dev->cnf.max_addresses;
2192 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2193 if (in6_dev->cnf.optimistic_dad &&
2194 !net->ipv6.devconf_all->forwarding && sllao)
2195 addr_flags = IFA_F_OPTIMISTIC;
2198 /* Do not allow to create too much of autoconfigured
2199 * addresses; this would be too easy way to crash kernel.
2201 if (!max_addresses ||
2202 ipv6_count_addresses(in6_dev) < max_addresses)
2203 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2205 addr_type&IPV6_ADDR_SCOPE_MASK,
2206 addr_flags, valid_lft,
2209 if (IS_ERR_OR_NULL(ifp)) {
2210 in6_dev_put(in6_dev);
2216 ifp->cstamp = jiffies;
2217 ifp->tokenized = tokenized;
2218 addrconf_dad_start(ifp);
2224 #ifdef CONFIG_IPV6_PRIVACY
2225 struct inet6_ifaddr *ift;
2229 /* update lifetime (RFC2462 5.5.3 e) */
2230 spin_lock(&ifp->lock);
2232 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2233 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2236 if (!update_lft && !create && stored_lft) {
2237 if (valid_lft > MIN_VALID_LIFETIME ||
2238 valid_lft > stored_lft)
2240 else if (stored_lft <= MIN_VALID_LIFETIME) {
2241 /* valid_lft <= stored_lft is always true */
2243 * RFC 4862 Section 5.5.3e:
2244 * "Note that the preferred lifetime of
2245 * the corresponding address is always
2246 * reset to the Preferred Lifetime in
2247 * the received Prefix Information
2248 * option, regardless of whether the
2249 * valid lifetime is also reset or
2252 * So if the preferred lifetime in
2253 * this advertisement is different
2254 * than what we have stored, but the
2255 * valid lifetime is invalid, just
2256 * reset prefered_lft.
2258 * We must set the valid lifetime
2259 * to the stored lifetime since we'll
2260 * be updating the timestamp below,
2261 * else we'll set it back to the
2264 if (prefered_lft != ifp->prefered_lft) {
2265 valid_lft = stored_lft;
2269 valid_lft = MIN_VALID_LIFETIME;
2270 if (valid_lft < prefered_lft)
2271 prefered_lft = valid_lft;
2277 ifp->valid_lft = valid_lft;
2278 ifp->prefered_lft = prefered_lft;
2281 ifp->flags &= ~IFA_F_DEPRECATED;
2282 spin_unlock(&ifp->lock);
2284 if (!(flags&IFA_F_TENTATIVE))
2285 ipv6_ifa_notify(0, ifp);
2287 spin_unlock(&ifp->lock);
2289 #ifdef CONFIG_IPV6_PRIVACY
2290 read_lock_bh(&in6_dev->lock);
2291 /* update all temporary addresses in the list */
2292 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2294 int age, max_valid, max_prefered;
2296 if (ifp != ift->ifpub)
2300 * RFC 4941 section 3.3:
2301 * If a received option will extend the lifetime
2302 * of a public address, the lifetimes of
2303 * temporary addresses should be extended,
2304 * subject to the overall constraint that no
2305 * temporary addresses should ever remain
2306 * "valid" or "preferred" for a time longer than
2307 * (TEMP_VALID_LIFETIME) or
2308 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2311 age = (now - ift->cstamp) / HZ;
2312 max_valid = in6_dev->cnf.temp_valid_lft - age;
2316 max_prefered = in6_dev->cnf.temp_prefered_lft -
2317 in6_dev->cnf.max_desync_factor -
2319 if (max_prefered < 0)
2322 if (valid_lft > max_valid)
2323 valid_lft = max_valid;
2325 if (prefered_lft > max_prefered)
2326 prefered_lft = max_prefered;
2328 spin_lock(&ift->lock);
2330 ift->valid_lft = valid_lft;
2331 ift->prefered_lft = prefered_lft;
2333 if (prefered_lft > 0)
2334 ift->flags &= ~IFA_F_DEPRECATED;
2336 spin_unlock(&ift->lock);
2337 if (!(flags&IFA_F_TENTATIVE))
2338 ipv6_ifa_notify(0, ift);
2341 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
2343 * When a new public address is created as
2344 * described in [ADDRCONF], also create a new
2345 * temporary address. Also create a temporary
2346 * address if it's enabled but no temporary
2347 * address currently exists.
2349 read_unlock_bh(&in6_dev->lock);
2350 ipv6_create_tempaddr(ifp, NULL);
2352 read_unlock_bh(&in6_dev->lock);
2359 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2360 in6_dev_put(in6_dev);
2364 * Set destination address.
2365 * Special case for SIT interfaces where we create a new "virtual"
2368 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2370 struct in6_ifreq ireq;
2371 struct net_device *dev;
2377 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2380 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2386 #if IS_ENABLED(CONFIG_IPV6_SIT)
2387 if (dev->type == ARPHRD_SIT) {
2388 const struct net_device_ops *ops = dev->netdev_ops;
2390 struct ip_tunnel_parm p;
2392 err = -EADDRNOTAVAIL;
2393 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2396 memset(&p, 0, sizeof(p));
2397 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2401 p.iph.protocol = IPPROTO_IPV6;
2403 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2405 if (ops->ndo_do_ioctl) {
2406 mm_segment_t oldfs = get_fs();
2409 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2416 dev = __dev_get_by_name(net, p.name);
2419 err = dev_open(dev);
2430 * Manual configuration of address on an interface
2432 static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2433 const struct in6_addr *peer_pfx,
2434 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2437 struct inet6_ifaddr *ifp;
2438 struct inet6_dev *idev;
2439 struct net_device *dev;
2443 unsigned long timeout;
2450 /* check the lifetime */
2451 if (!valid_lft || prefered_lft > valid_lft)
2454 dev = __dev_get_by_index(net, ifindex);
2458 idev = addrconf_add_dev(dev);
2460 return PTR_ERR(idev);
2462 scope = ipv6_addr_scope(pfx);
2464 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2465 if (addrconf_finite_timeout(timeout)) {
2466 expires = jiffies_to_clock_t(timeout * HZ);
2467 valid_lft = timeout;
2468 flags = RTF_EXPIRES;
2472 ifa_flags |= IFA_F_PERMANENT;
2475 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2476 if (addrconf_finite_timeout(timeout)) {
2478 ifa_flags |= IFA_F_DEPRECATED;
2479 prefered_lft = timeout;
2482 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2483 valid_lft, prefered_lft);
2486 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2489 * Note that section 3.1 of RFC 4429 indicates
2490 * that the Optimistic flag should not be set for
2491 * manually configured addresses
2493 addrconf_dad_start(ifp);
2499 return PTR_ERR(ifp);
2502 static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
2505 struct inet6_ifaddr *ifp;
2506 struct inet6_dev *idev;
2507 struct net_device *dev;
2512 dev = __dev_get_by_index(net, ifindex);
2516 if ((idev = __in6_dev_get(dev)) == NULL)
2519 read_lock_bh(&idev->lock);
2520 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2521 if (ifp->prefix_len == plen &&
2522 ipv6_addr_equal(pfx, &ifp->addr)) {
2524 read_unlock_bh(&idev->lock);
2530 read_unlock_bh(&idev->lock);
2531 return -EADDRNOTAVAIL;
2535 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2537 struct in6_ifreq ireq;
2540 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2543 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2547 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2548 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2549 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2554 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2556 struct in6_ifreq ireq;
2559 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2562 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2566 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2567 ireq.ifr6_prefixlen);
2572 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2573 int plen, int scope)
2575 struct inet6_ifaddr *ifp;
2577 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2578 scope, IFA_F_PERMANENT, 0, 0);
2580 spin_lock_bh(&ifp->lock);
2581 ifp->flags &= ~IFA_F_TENTATIVE;
2582 spin_unlock_bh(&ifp->lock);
2583 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2588 #if IS_ENABLED(CONFIG_IPV6_SIT)
2589 static void sit_add_v4_addrs(struct inet6_dev *idev)
2591 struct in6_addr addr;
2592 struct net_device *dev;
2593 struct net *net = dev_net(idev->dev);
2598 memset(&addr, 0, sizeof(struct in6_addr));
2599 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2601 if (idev->dev->flags&IFF_POINTOPOINT) {
2602 addr.s6_addr32[0] = htonl(0xfe800000);
2605 scope = IPV6_ADDR_COMPATv4;
2608 if (addr.s6_addr32[3]) {
2609 add_addr(idev, &addr, 128, scope);
2613 for_each_netdev(net, dev) {
2614 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2615 if (in_dev && (dev->flags & IFF_UP)) {
2616 struct in_ifaddr *ifa;
2620 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2623 addr.s6_addr32[3] = ifa->ifa_local;
2625 if (ifa->ifa_scope == RT_SCOPE_LINK)
2627 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2628 if (idev->dev->flags&IFF_POINTOPOINT)
2632 if (idev->dev->flags&IFF_POINTOPOINT)
2637 add_addr(idev, &addr, plen, flag);
2644 static void init_loopback(struct net_device *dev)
2646 struct inet6_dev *idev;
2647 struct net_device *sp_dev;
2648 struct inet6_ifaddr *sp_ifa;
2649 struct rt6_info *sp_rt;
2655 if ((idev = ipv6_find_idev(dev)) == NULL) {
2656 pr_debug("%s: add_dev failed\n", __func__);
2660 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2662 /* Add routes to other interface's IPv6 addresses */
2663 for_each_netdev(dev_net(dev), sp_dev) {
2664 if (!strcmp(sp_dev->name, dev->name))
2667 idev = __in6_dev_get(sp_dev);
2671 read_lock_bh(&idev->lock);
2672 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2674 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2680 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2682 /* Failure cases are ignored */
2683 if (!IS_ERR(sp_rt)) {
2688 read_unlock_bh(&idev->lock);
2692 static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
2694 struct inet6_ifaddr *ifp;
2695 u32 addr_flags = IFA_F_PERMANENT;
2697 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2698 if (idev->cnf.optimistic_dad &&
2699 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2700 addr_flags |= IFA_F_OPTIMISTIC;
2704 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 0, 0);
2706 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2707 addrconf_dad_start(ifp);
2712 static void addrconf_dev_config(struct net_device *dev)
2714 struct in6_addr addr;
2715 struct inet6_dev *idev;
2719 if ((dev->type != ARPHRD_ETHER) &&
2720 (dev->type != ARPHRD_FDDI) &&
2721 (dev->type != ARPHRD_ARCNET) &&
2722 (dev->type != ARPHRD_INFINIBAND) &&
2723 (dev->type != ARPHRD_IEEE802154) &&
2724 (dev->type != ARPHRD_IEEE1394) &&
2725 (dev->type != ARPHRD_TUNNEL6)) {
2726 /* Alas, we support only Ethernet autoconfiguration. */
2730 idev = addrconf_add_dev(dev);
2734 memset(&addr, 0, sizeof(struct in6_addr));
2735 addr.s6_addr32[0] = htonl(0xFE800000);
2737 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2738 addrconf_add_linklocal(idev, &addr);
2741 #if IS_ENABLED(CONFIG_IPV6_SIT)
2742 static void addrconf_sit_config(struct net_device *dev)
2744 struct inet6_dev *idev;
2749 * Configure the tunnel with one of our IPv4
2750 * addresses... we should configure all of
2751 * our v4 addrs in the tunnel
2754 if ((idev = ipv6_find_idev(dev)) == NULL) {
2755 pr_debug("%s: add_dev failed\n", __func__);
2759 if (dev->priv_flags & IFF_ISATAP) {
2760 struct in6_addr addr;
2762 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2763 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2764 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2765 addrconf_add_linklocal(idev, &addr);
2769 sit_add_v4_addrs(idev);
2771 if (dev->flags&IFF_POINTOPOINT)
2772 addrconf_add_mroute(dev);
2778 #if IS_ENABLED(CONFIG_NET_IPGRE)
2779 static void addrconf_gre_config(struct net_device *dev)
2781 struct inet6_dev *idev;
2782 struct in6_addr addr;
2786 if ((idev = ipv6_find_idev(dev)) == NULL) {
2787 pr_debug("%s: add_dev failed\n", __func__);
2791 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2792 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2794 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2795 addrconf_add_linklocal(idev, &addr);
2800 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2802 struct in6_addr lladdr;
2804 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2805 addrconf_add_linklocal(idev, &lladdr);
2811 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2814 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2815 struct inet6_dev *idev = __in6_dev_get(dev);
2816 int run_pending = 0;
2820 case NETDEV_REGISTER:
2821 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2822 idev = ipv6_add_dev(dev);
2824 return notifier_from_errno(-ENOMEM);
2830 if (dev->flags & IFF_SLAVE)
2833 if (event == NETDEV_UP) {
2834 if (!addrconf_qdisc_ok(dev)) {
2835 /* device is not ready yet. */
2836 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2841 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2842 idev = ipv6_add_dev(dev);
2845 idev->if_flags |= IF_READY;
2849 if (!addrconf_qdisc_ok(dev)) {
2850 /* device is still not ready. */
2855 if (idev->if_flags & IF_READY)
2856 /* device is already configured. */
2858 idev->if_flags |= IF_READY;
2861 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2867 switch (dev->type) {
2868 #if IS_ENABLED(CONFIG_IPV6_SIT)
2870 addrconf_sit_config(dev);
2873 #if IS_ENABLED(CONFIG_NET_IPGRE)
2875 addrconf_gre_config(dev);
2878 case ARPHRD_LOOPBACK:
2883 addrconf_dev_config(dev);
2889 addrconf_dad_run(idev);
2892 * If the MTU changed during the interface down,
2893 * when the interface up, the changed MTU must be
2894 * reflected in the idev as well as routers.
2896 if (idev->cnf.mtu6 != dev->mtu &&
2897 dev->mtu >= IPV6_MIN_MTU) {
2898 rt6_mtu_change(dev, dev->mtu);
2899 idev->cnf.mtu6 = dev->mtu;
2901 idev->tstamp = jiffies;
2902 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2905 * If the changed mtu during down is lower than
2906 * IPV6_MIN_MTU stop IPv6 on this interface.
2908 if (dev->mtu < IPV6_MIN_MTU)
2909 addrconf_ifdown(dev, 1);
2913 case NETDEV_CHANGEMTU:
2914 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2915 rt6_mtu_change(dev, dev->mtu);
2916 idev->cnf.mtu6 = dev->mtu;
2920 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2921 idev = ipv6_add_dev(dev);
2927 * MTU falled under IPV6_MIN_MTU.
2928 * Stop IPv6 on this interface.
2932 case NETDEV_UNREGISTER:
2934 * Remove all addresses from this interface.
2936 addrconf_ifdown(dev, event != NETDEV_DOWN);
2939 case NETDEV_CHANGENAME:
2941 snmp6_unregister_dev(idev);
2942 addrconf_sysctl_unregister(idev);
2943 addrconf_sysctl_register(idev);
2944 err = snmp6_register_dev(idev);
2946 return notifier_from_errno(err);
2950 case NETDEV_PRE_TYPE_CHANGE:
2951 case NETDEV_POST_TYPE_CHANGE:
2952 addrconf_type_change(dev, event);
2960 * addrconf module should be notified of a device going up
2962 static struct notifier_block ipv6_dev_notf = {
2963 .notifier_call = addrconf_notify,
2966 static void addrconf_type_change(struct net_device *dev, unsigned long event)
2968 struct inet6_dev *idev;
2971 idev = __in6_dev_get(dev);
2973 if (event == NETDEV_POST_TYPE_CHANGE)
2974 ipv6_mc_remap(idev);
2975 else if (event == NETDEV_PRE_TYPE_CHANGE)
2976 ipv6_mc_unmap(idev);
2979 static int addrconf_ifdown(struct net_device *dev, int how)
2981 struct net *net = dev_net(dev);
2982 struct inet6_dev *idev;
2983 struct inet6_ifaddr *ifa;
2988 rt6_ifdown(net, dev);
2989 neigh_ifdown(&nd_tbl, dev);
2991 idev = __in6_dev_get(dev);
2996 * Step 1: remove reference to ipv6 device from parent device.
3002 /* protected by rtnl_lock */
3003 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3005 /* Step 1.5: remove snmp6 entry */
3006 snmp6_unregister_dev(idev);
3010 /* Step 2: clear hash table */
3011 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3012 struct hlist_head *h = &inet6_addr_lst[i];
3014 spin_lock_bh(&addrconf_hash_lock);
3016 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3017 if (ifa->idev == idev) {
3018 hlist_del_init_rcu(&ifa->addr_lst);
3019 addrconf_del_dad_timer(ifa);
3023 spin_unlock_bh(&addrconf_hash_lock);
3026 write_lock_bh(&idev->lock);
3028 addrconf_del_rs_timer(idev);
3030 /* Step 2: clear flags for stateless addrconf */
3032 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3034 #ifdef CONFIG_IPV6_PRIVACY
3035 if (how && del_timer(&idev->regen_timer))
3038 /* Step 3: clear tempaddr list */
3039 while (!list_empty(&idev->tempaddr_list)) {
3040 ifa = list_first_entry(&idev->tempaddr_list,
3041 struct inet6_ifaddr, tmp_list);
3042 list_del(&ifa->tmp_list);
3043 write_unlock_bh(&idev->lock);
3044 spin_lock_bh(&ifa->lock);
3047 in6_ifa_put(ifa->ifpub);
3050 spin_unlock_bh(&ifa->lock);
3052 write_lock_bh(&idev->lock);
3056 while (!list_empty(&idev->addr_list)) {
3057 ifa = list_first_entry(&idev->addr_list,
3058 struct inet6_ifaddr, if_list);
3059 addrconf_del_dad_timer(ifa);
3061 list_del(&ifa->if_list);
3063 write_unlock_bh(&idev->lock);
3065 spin_lock_bh(&ifa->state_lock);
3067 ifa->state = INET6_IFADDR_STATE_DEAD;
3068 spin_unlock_bh(&ifa->state_lock);
3070 if (state != INET6_IFADDR_STATE_DEAD) {
3071 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3072 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3076 write_lock_bh(&idev->lock);
3079 write_unlock_bh(&idev->lock);
3081 /* Step 5: Discard multicast list */
3083 ipv6_mc_destroy_dev(idev);
3087 idev->tstamp = jiffies;
3089 /* Last: Shot the device (if unregistered) */
3091 addrconf_sysctl_unregister(idev);
3092 neigh_parms_release(&nd_tbl, idev->nd_parms);
3093 neigh_ifdown(&nd_tbl, dev);
3099 static void addrconf_rs_timer(unsigned long data)
3101 struct inet6_dev *idev = (struct inet6_dev *)data;
3102 struct in6_addr lladdr;
3104 write_lock(&idev->lock);
3105 if (idev->dead || !(idev->if_flags & IF_READY))
3108 if (!ipv6_accept_ra(idev))
3111 /* Announcement received after solicitation was sent */
3112 if (idev->if_flags & IF_RA_RCVD)
3115 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3116 if (!__ipv6_get_lladdr(idev, &lladdr, IFA_F_TENTATIVE))
3117 ndisc_send_rs(idev->dev, &lladdr,
3118 &in6addr_linklocal_allrouters);
3122 /* The wait after the last probe can be shorter */
3123 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3124 idev->cnf.rtr_solicits) ?
3125 idev->cnf.rtr_solicit_delay :
3126 idev->cnf.rtr_solicit_interval);
3129 * Note: we do not support deprecated "all on-link"
3130 * assumption any longer.
3132 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3136 write_unlock(&idev->lock);
3141 * Duplicate Address Detection
3143 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3145 unsigned long rand_num;
3146 struct inet6_dev *idev = ifp->idev;
3148 if (ifp->flags & IFA_F_OPTIMISTIC)
3151 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3153 ifp->dad_probes = idev->cnf.dad_transmits;
3154 addrconf_mod_dad_timer(ifp, rand_num);
3157 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3159 struct inet6_dev *idev = ifp->idev;
3160 struct net_device *dev = idev->dev;
3162 addrconf_join_solict(dev, &ifp->addr);
3164 net_srandom(ifp->addr.s6_addr32[3]);
3166 read_lock_bh(&idev->lock);
3167 spin_lock(&ifp->lock);
3168 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3171 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3172 idev->cnf.accept_dad < 1 ||
3173 !(ifp->flags&IFA_F_TENTATIVE) ||
3174 ifp->flags & IFA_F_NODAD) {
3175 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3176 spin_unlock(&ifp->lock);
3177 read_unlock_bh(&idev->lock);
3179 addrconf_dad_completed(ifp);
3183 if (!(idev->if_flags & IF_READY)) {
3184 spin_unlock(&ifp->lock);
3185 read_unlock_bh(&idev->lock);
3187 * If the device is not ready:
3188 * - keep it tentative if it is a permanent address.
3189 * - otherwise, kill it.
3192 addrconf_dad_stop(ifp, 0);
3197 * Optimistic nodes can start receiving
3200 if (ifp->flags & IFA_F_OPTIMISTIC)
3201 ip6_ins_rt(ifp->rt);
3203 addrconf_dad_kick(ifp);
3205 spin_unlock(&ifp->lock);
3206 read_unlock_bh(&idev->lock);
3209 static void addrconf_dad_timer(unsigned long data)
3211 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3212 struct inet6_dev *idev = ifp->idev;
3213 struct in6_addr mcaddr;
3215 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3218 write_lock(&idev->lock);
3219 if (idev->dead || !(idev->if_flags & IF_READY)) {
3220 write_unlock(&idev->lock);
3224 spin_lock(&ifp->lock);
3225 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3226 spin_unlock(&ifp->lock);
3227 write_unlock(&idev->lock);
3231 if (ifp->dad_probes == 0) {
3233 * DAD was successful
3236 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3237 spin_unlock(&ifp->lock);
3238 write_unlock(&idev->lock);
3240 addrconf_dad_completed(ifp);
3246 addrconf_mod_dad_timer(ifp, ifp->idev->nd_parms->retrans_time);
3247 spin_unlock(&ifp->lock);
3248 write_unlock(&idev->lock);
3250 /* send a neighbour solicitation for our addr */
3251 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3252 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3257 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3259 struct net_device *dev = ifp->idev->dev;
3260 struct in6_addr lladdr;
3261 bool send_rs, send_mld;
3263 addrconf_del_dad_timer(ifp);
3266 * Configure the address for reception. Now it is valid.
3269 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3271 /* If added prefix is link local and we are prepared to process
3272 router advertisements, start sending router solicitations.
3275 read_lock_bh(&ifp->idev->lock);
3276 spin_lock(&ifp->lock);
3277 send_mld = ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL &&
3278 ifp->idev->valid_ll_addr_cnt == 1;
3279 send_rs = send_mld &&
3280 ipv6_accept_ra(ifp->idev) &&
3281 ifp->idev->cnf.rtr_solicits > 0 &&
3282 (dev->flags&IFF_LOOPBACK) == 0;
3283 spin_unlock(&ifp->lock);
3284 read_unlock_bh(&ifp->idev->lock);
3286 /* While dad is in progress mld report's source address is in6_addrany.
3287 * Resend with proper ll now.
3290 ipv6_mc_dad_complete(ifp->idev);
3294 * If a host as already performed a random delay
3295 * [...] as part of DAD [...] there is no need
3296 * to delay again before sending the first RS
3298 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3300 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3302 write_lock_bh(&ifp->idev->lock);
3303 spin_lock(&ifp->lock);
3304 ifp->idev->rs_probes = 1;
3305 ifp->idev->if_flags |= IF_RS_SENT;
3306 addrconf_mod_rs_timer(ifp->idev,
3307 ifp->idev->cnf.rtr_solicit_interval);
3308 spin_unlock(&ifp->lock);
3309 write_unlock_bh(&ifp->idev->lock);
3313 static void addrconf_dad_run(struct inet6_dev *idev)
3315 struct inet6_ifaddr *ifp;
3317 read_lock_bh(&idev->lock);
3318 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3319 spin_lock(&ifp->lock);
3320 if (ifp->flags & IFA_F_TENTATIVE &&
3321 ifp->state == INET6_IFADDR_STATE_DAD)
3322 addrconf_dad_kick(ifp);
3323 spin_unlock(&ifp->lock);
3325 read_unlock_bh(&idev->lock);
3328 #ifdef CONFIG_PROC_FS
3329 struct if6_iter_state {
3330 struct seq_net_private p;
3335 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3337 struct inet6_ifaddr *ifa = NULL;
3338 struct if6_iter_state *state = seq->private;
3339 struct net *net = seq_file_net(seq);
3342 /* initial bucket if pos is 0 */
3348 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3349 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3351 if (!net_eq(dev_net(ifa->idev->dev), net))
3353 /* sync with offset */
3354 if (p < state->offset) {
3362 /* prepare for next bucket */
3369 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3370 struct inet6_ifaddr *ifa)
3372 struct if6_iter_state *state = seq->private;
3373 struct net *net = seq_file_net(seq);
3375 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3376 if (!net_eq(dev_net(ifa->idev->dev), net))
3382 while (++state->bucket < IN6_ADDR_HSIZE) {
3384 hlist_for_each_entry_rcu_bh(ifa,
3385 &inet6_addr_lst[state->bucket], addr_lst) {
3386 if (!net_eq(dev_net(ifa->idev->dev), net))
3396 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3400 return if6_get_first(seq, *pos);
3403 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3405 struct inet6_ifaddr *ifa;
3407 ifa = if6_get_next(seq, v);
3412 static void if6_seq_stop(struct seq_file *seq, void *v)
3415 rcu_read_unlock_bh();
3418 static int if6_seq_show(struct seq_file *seq, void *v)
3420 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3421 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3423 ifp->idev->dev->ifindex,
3427 ifp->idev->dev->name);
3431 static const struct seq_operations if6_seq_ops = {
3432 .start = if6_seq_start,
3433 .next = if6_seq_next,
3434 .show = if6_seq_show,
3435 .stop = if6_seq_stop,
3438 static int if6_seq_open(struct inode *inode, struct file *file)
3440 return seq_open_net(inode, file, &if6_seq_ops,
3441 sizeof(struct if6_iter_state));
3444 static const struct file_operations if6_fops = {
3445 .owner = THIS_MODULE,
3446 .open = if6_seq_open,
3448 .llseek = seq_lseek,
3449 .release = seq_release_net,
3452 static int __net_init if6_proc_net_init(struct net *net)
3454 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3459 static void __net_exit if6_proc_net_exit(struct net *net)
3461 remove_proc_entry("if_inet6", net->proc_net);
3464 static struct pernet_operations if6_proc_net_ops = {
3465 .init = if6_proc_net_init,
3466 .exit = if6_proc_net_exit,
3469 int __init if6_proc_init(void)
3471 return register_pernet_subsys(&if6_proc_net_ops);
3474 void if6_proc_exit(void)
3476 unregister_pernet_subsys(&if6_proc_net_ops);
3478 #endif /* CONFIG_PROC_FS */
3480 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3481 /* Check if address is a home address configured on any interface. */
3482 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3485 struct inet6_ifaddr *ifp = NULL;
3486 unsigned int hash = inet6_addr_hash(addr);
3489 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3490 if (!net_eq(dev_net(ifp->idev->dev), net))
3492 if (ipv6_addr_equal(&ifp->addr, addr) &&
3493 (ifp->flags & IFA_F_HOMEADDRESS)) {
3498 rcu_read_unlock_bh();
3504 * Periodic address status verification
3507 static void addrconf_verify(unsigned long foo)
3509 unsigned long now, next, next_sec, next_sched;
3510 struct inet6_ifaddr *ifp;
3514 spin_lock(&addrconf_verify_lock);
3516 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3518 del_timer(&addr_chk_timer);
3520 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3522 hlist_for_each_entry_rcu_bh(ifp,
3523 &inet6_addr_lst[i], addr_lst) {
3526 if (ifp->flags & IFA_F_PERMANENT)
3529 spin_lock(&ifp->lock);
3530 /* We try to batch several events at once. */
3531 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3533 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3534 age >= ifp->valid_lft) {
3535 spin_unlock(&ifp->lock);
3539 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3540 spin_unlock(&ifp->lock);
3542 } else if (age >= ifp->prefered_lft) {
3543 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3546 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3548 ifp->flags |= IFA_F_DEPRECATED;
3551 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3552 next = ifp->tstamp + ifp->valid_lft * HZ;
3554 spin_unlock(&ifp->lock);
3559 ipv6_ifa_notify(0, ifp);
3563 #ifdef CONFIG_IPV6_PRIVACY
3564 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3565 !(ifp->flags&IFA_F_TENTATIVE)) {
3566 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3567 ifp->idev->cnf.dad_transmits *
3568 ifp->idev->nd_parms->retrans_time / HZ;
3570 if (age >= ifp->prefered_lft - regen_advance) {
3571 struct inet6_ifaddr *ifpub = ifp->ifpub;
3572 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3573 next = ifp->tstamp + ifp->prefered_lft * HZ;
3574 if (!ifp->regen_count && ifpub) {
3577 in6_ifa_hold(ifpub);
3578 spin_unlock(&ifp->lock);
3580 spin_lock(&ifpub->lock);
3581 ifpub->regen_count = 0;
3582 spin_unlock(&ifpub->lock);
3583 ipv6_create_tempaddr(ifpub, ifp);
3588 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3589 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3590 spin_unlock(&ifp->lock);
3593 /* ifp->prefered_lft <= ifp->valid_lft */
3594 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3595 next = ifp->tstamp + ifp->prefered_lft * HZ;
3596 spin_unlock(&ifp->lock);
3601 next_sec = round_jiffies_up(next);
3604 /* If rounded timeout is accurate enough, accept it. */
3605 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3606 next_sched = next_sec;
3608 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3609 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3610 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3612 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3613 now, next, next_sec, next_sched);
3615 addr_chk_timer.expires = next_sched;
3616 add_timer(&addr_chk_timer);
3617 spin_unlock(&addrconf_verify_lock);
3618 rcu_read_unlock_bh();
3621 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3622 struct in6_addr **peer_pfx)
3624 struct in6_addr *pfx = NULL;
3629 pfx = nla_data(addr);
3632 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3634 pfx = nla_data(local);
3640 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3641 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3642 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3643 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3647 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3649 struct net *net = sock_net(skb->sk);
3650 struct ifaddrmsg *ifm;
3651 struct nlattr *tb[IFA_MAX+1];
3652 struct in6_addr *pfx, *peer_pfx;
3655 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3659 ifm = nlmsg_data(nlh);
3660 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3664 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3667 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3668 u32 prefered_lft, u32 valid_lft)
3672 unsigned long timeout;
3674 if (!valid_lft || (prefered_lft > valid_lft))
3677 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3678 if (addrconf_finite_timeout(timeout)) {
3679 expires = jiffies_to_clock_t(timeout * HZ);
3680 valid_lft = timeout;
3681 flags = RTF_EXPIRES;
3685 ifa_flags |= IFA_F_PERMANENT;
3688 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3689 if (addrconf_finite_timeout(timeout)) {
3691 ifa_flags |= IFA_F_DEPRECATED;
3692 prefered_lft = timeout;
3695 spin_lock_bh(&ifp->lock);
3696 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3697 ifp->tstamp = jiffies;
3698 ifp->valid_lft = valid_lft;
3699 ifp->prefered_lft = prefered_lft;
3701 spin_unlock_bh(&ifp->lock);
3702 if (!(ifp->flags&IFA_F_TENTATIVE))
3703 ipv6_ifa_notify(0, ifp);
3705 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3713 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3715 struct net *net = sock_net(skb->sk);
3716 struct ifaddrmsg *ifm;
3717 struct nlattr *tb[IFA_MAX+1];
3718 struct in6_addr *pfx, *peer_pfx;
3719 struct inet6_ifaddr *ifa;
3720 struct net_device *dev;
3721 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3725 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3729 ifm = nlmsg_data(nlh);
3730 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3734 if (tb[IFA_CACHEINFO]) {
3735 struct ifa_cacheinfo *ci;
3737 ci = nla_data(tb[IFA_CACHEINFO]);
3738 valid_lft = ci->ifa_valid;
3739 preferred_lft = ci->ifa_prefered;
3741 preferred_lft = INFINITY_LIFE_TIME;
3742 valid_lft = INFINITY_LIFE_TIME;
3745 dev = __dev_get_by_index(net, ifm->ifa_index);
3749 /* We ignore other flags so far. */
3750 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3752 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3755 * It would be best to check for !NLM_F_CREATE here but
3756 * userspace alreay relies on not having to provide this.
3758 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3759 ifm->ifa_prefixlen, ifa_flags,
3760 preferred_lft, valid_lft);
3763 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3764 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3767 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3774 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3775 u8 scope, int ifindex)
3777 struct ifaddrmsg *ifm;
3779 ifm = nlmsg_data(nlh);
3780 ifm->ifa_family = AF_INET6;
3781 ifm->ifa_prefixlen = prefixlen;
3782 ifm->ifa_flags = flags;
3783 ifm->ifa_scope = scope;
3784 ifm->ifa_index = ifindex;
3787 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3788 unsigned long tstamp, u32 preferred, u32 valid)
3790 struct ifa_cacheinfo ci;
3792 ci.cstamp = cstamp_delta(cstamp);
3793 ci.tstamp = cstamp_delta(tstamp);
3794 ci.ifa_prefered = preferred;
3795 ci.ifa_valid = valid;
3797 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3800 static inline int rt_scope(int ifa_scope)
3802 if (ifa_scope & IFA_HOST)
3803 return RT_SCOPE_HOST;
3804 else if (ifa_scope & IFA_LINK)
3805 return RT_SCOPE_LINK;
3806 else if (ifa_scope & IFA_SITE)
3807 return RT_SCOPE_SITE;
3809 return RT_SCOPE_UNIVERSE;
3812 static inline int inet6_ifaddr_msgsize(void)
3814 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3815 + nla_total_size(16) /* IFA_LOCAL */
3816 + nla_total_size(16) /* IFA_ADDRESS */
3817 + nla_total_size(sizeof(struct ifa_cacheinfo));
3820 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3821 u32 portid, u32 seq, int event, unsigned int flags)
3823 struct nlmsghdr *nlh;
3824 u32 preferred, valid;
3826 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3830 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3831 ifa->idev->dev->ifindex);
3833 if (!(ifa->flags&IFA_F_PERMANENT)) {
3834 preferred = ifa->prefered_lft;
3835 valid = ifa->valid_lft;
3836 if (preferred != INFINITY_LIFE_TIME) {
3837 long tval = (jiffies - ifa->tstamp)/HZ;
3838 if (preferred > tval)
3842 if (valid != INFINITY_LIFE_TIME) {
3850 preferred = INFINITY_LIFE_TIME;
3851 valid = INFINITY_LIFE_TIME;
3854 if (!ipv6_addr_any(&ifa->peer_addr)) {
3855 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
3856 nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
3859 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
3862 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3865 return nlmsg_end(skb, nlh);
3868 nlmsg_cancel(skb, nlh);
3872 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3873 u32 portid, u32 seq, int event, u16 flags)
3875 struct nlmsghdr *nlh;
3876 u8 scope = RT_SCOPE_UNIVERSE;
3877 int ifindex = ifmca->idev->dev->ifindex;
3879 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3880 scope = RT_SCOPE_SITE;
3882 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3886 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3887 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3888 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3889 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3890 nlmsg_cancel(skb, nlh);
3894 return nlmsg_end(skb, nlh);
3897 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3898 u32 portid, u32 seq, int event, unsigned int flags)
3900 struct nlmsghdr *nlh;
3901 u8 scope = RT_SCOPE_UNIVERSE;
3902 int ifindex = ifaca->aca_idev->dev->ifindex;
3904 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3905 scope = RT_SCOPE_SITE;
3907 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
3911 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3912 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3913 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3914 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3915 nlmsg_cancel(skb, nlh);
3919 return nlmsg_end(skb, nlh);
3928 /* called with rcu_read_lock() */
3929 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3930 struct netlink_callback *cb, enum addr_type_t type,
3931 int s_ip_idx, int *p_ip_idx)
3933 struct ifmcaddr6 *ifmca;
3934 struct ifacaddr6 *ifaca;
3936 int ip_idx = *p_ip_idx;
3938 read_lock_bh(&idev->lock);
3940 case UNICAST_ADDR: {
3941 struct inet6_ifaddr *ifa;
3943 /* unicast address incl. temp addr */
3944 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3945 if (++ip_idx < s_ip_idx)
3947 err = inet6_fill_ifaddr(skb, ifa,
3948 NETLINK_CB(cb->skb).portid,
3954 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3958 case MULTICAST_ADDR:
3959 /* multicast address */
3960 for (ifmca = idev->mc_list; ifmca;
3961 ifmca = ifmca->next, ip_idx++) {
3962 if (ip_idx < s_ip_idx)
3964 err = inet6_fill_ifmcaddr(skb, ifmca,
3965 NETLINK_CB(cb->skb).portid,
3974 /* anycast address */
3975 for (ifaca = idev->ac_list; ifaca;
3976 ifaca = ifaca->aca_next, ip_idx++) {
3977 if (ip_idx < s_ip_idx)
3979 err = inet6_fill_ifacaddr(skb, ifaca,
3980 NETLINK_CB(cb->skb).portid,
3991 read_unlock_bh(&idev->lock);
3996 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3997 enum addr_type_t type)
3999 struct net *net = sock_net(skb->sk);
4002 int s_idx, s_ip_idx;
4003 struct net_device *dev;
4004 struct inet6_dev *idev;
4005 struct hlist_head *head;
4008 s_idx = idx = cb->args[1];
4009 s_ip_idx = ip_idx = cb->args[2];
4012 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4013 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4015 head = &net->dev_index_head[h];
4016 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4019 if (h > s_h || idx > s_idx)
4022 idev = __in6_dev_get(dev);
4026 if (in6_dump_addrs(idev, skb, cb, type,
4027 s_ip_idx, &ip_idx) <= 0)
4037 cb->args[2] = ip_idx;
4042 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4044 enum addr_type_t type = UNICAST_ADDR;
4046 return inet6_dump_addr(skb, cb, type);
4049 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4051 enum addr_type_t type = MULTICAST_ADDR;
4053 return inet6_dump_addr(skb, cb, type);
4057 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4059 enum addr_type_t type = ANYCAST_ADDR;
4061 return inet6_dump_addr(skb, cb, type);
4064 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4066 struct net *net = sock_net(in_skb->sk);
4067 struct ifaddrmsg *ifm;
4068 struct nlattr *tb[IFA_MAX+1];
4069 struct in6_addr *addr = NULL, *peer;
4070 struct net_device *dev = NULL;
4071 struct inet6_ifaddr *ifa;
4072 struct sk_buff *skb;
4075 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4079 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4085 ifm = nlmsg_data(nlh);
4087 dev = __dev_get_by_index(net, ifm->ifa_index);
4089 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4091 err = -EADDRNOTAVAIL;
4095 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4101 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4102 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4104 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4105 WARN_ON(err == -EMSGSIZE);
4109 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4116 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4118 struct sk_buff *skb;
4119 struct net *net = dev_net(ifa->idev->dev);
4122 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4126 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4128 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4129 WARN_ON(err == -EMSGSIZE);
4133 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4137 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4140 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4141 __s32 *array, int bytes)
4143 BUG_ON(bytes < (DEVCONF_MAX * 4));
4145 memset(array, 0, bytes);
4146 array[DEVCONF_FORWARDING] = cnf->forwarding;
4147 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4148 array[DEVCONF_MTU6] = cnf->mtu6;
4149 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4150 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4151 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4152 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4153 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4154 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4155 jiffies_to_msecs(cnf->rtr_solicit_interval);
4156 array[DEVCONF_RTR_SOLICIT_DELAY] =
4157 jiffies_to_msecs(cnf->rtr_solicit_delay);
4158 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4159 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4160 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4161 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4162 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4163 #ifdef CONFIG_IPV6_PRIVACY
4164 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4165 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4166 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4167 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4168 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4170 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4171 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4172 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4173 #ifdef CONFIG_IPV6_ROUTER_PREF
4174 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4175 array[DEVCONF_RTR_PROBE_INTERVAL] =
4176 jiffies_to_msecs(cnf->rtr_probe_interval);
4177 #ifdef CONFIG_IPV6_ROUTE_INFO
4178 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4181 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4182 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4183 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4184 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4186 #ifdef CONFIG_IPV6_MROUTE
4187 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4189 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4190 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4191 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4192 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4193 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4196 static inline size_t inet6_ifla6_size(void)
4198 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4199 + nla_total_size(sizeof(struct ifla_cacheinfo))
4200 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4201 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4202 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4203 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4206 static inline size_t inet6_if_nlmsg_size(void)
4208 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4209 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4210 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4211 + nla_total_size(4) /* IFLA_MTU */
4212 + nla_total_size(4) /* IFLA_LINK */
4213 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4216 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4217 int items, int bytes)
4220 int pad = bytes - sizeof(u64) * items;
4223 /* Use put_unaligned() because stats may not be aligned for u64. */
4224 put_unaligned(items, &stats[0]);
4225 for (i = 1; i < items; i++)
4226 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4228 memset(&stats[items], 0, pad);
4231 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4232 int items, int bytes, size_t syncpoff)
4235 int pad = bytes - sizeof(u64) * items;
4238 /* Use put_unaligned() because stats may not be aligned for u64. */
4239 put_unaligned(items, &stats[0]);
4240 for (i = 1; i < items; i++)
4241 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4243 memset(&stats[items], 0, pad);
4246 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4250 case IFLA_INET6_STATS:
4251 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4252 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4254 case IFLA_INET6_ICMP6STATS:
4255 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4260 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4263 struct ifla_cacheinfo ci;
4265 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4266 goto nla_put_failure;
4267 ci.max_reasm_len = IPV6_MAXPLEN;
4268 ci.tstamp = cstamp_delta(idev->tstamp);
4269 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4270 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
4271 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4272 goto nla_put_failure;
4273 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4275 goto nla_put_failure;
4276 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4278 /* XXX - MC not implemented */
4280 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4282 goto nla_put_failure;
4283 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4285 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4287 goto nla_put_failure;
4288 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4290 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4292 goto nla_put_failure;
4293 read_lock_bh(&idev->lock);
4294 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4295 read_unlock_bh(&idev->lock);
4303 static size_t inet6_get_link_af_size(const struct net_device *dev)
4305 if (!__in6_dev_get(dev))
4308 return inet6_ifla6_size();
4311 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4313 struct inet6_dev *idev = __in6_dev_get(dev);
4318 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4324 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4326 struct inet6_ifaddr *ifp;
4327 struct net_device *dev = idev->dev;
4328 bool update_rs = false;
4329 struct in6_addr ll_addr;
4333 if (ipv6_addr_any(token))
4335 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4337 if (!ipv6_accept_ra(idev))
4339 if (idev->cnf.rtr_solicits <= 0)
4342 write_lock_bh(&idev->lock);
4344 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4345 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4347 write_unlock_bh(&idev->lock);
4349 if (!idev->dead && (idev->if_flags & IF_READY) &&
4350 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4351 IFA_F_OPTIMISTIC)) {
4353 /* If we're not ready, then normal ifup will take care
4354 * of this. Otherwise, we need to request our rs here.
4356 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4360 write_lock_bh(&idev->lock);
4363 idev->if_flags |= IF_RS_SENT;
4364 idev->rs_probes = 1;
4365 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4368 /* Well, that's kinda nasty ... */
4369 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4370 spin_lock(&ifp->lock);
4371 if (ifp->tokenized) {
4373 ifp->prefered_lft = 0;
4375 spin_unlock(&ifp->lock);
4378 write_unlock_bh(&idev->lock);
4383 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4386 struct inet6_dev *idev = __in6_dev_get(dev);
4387 struct nlattr *tb[IFLA_INET6_MAX + 1];
4390 return -EAFNOSUPPORT;
4392 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4395 if (tb[IFLA_INET6_TOKEN])
4396 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4401 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4402 u32 portid, u32 seq, int event, unsigned int flags)
4404 struct net_device *dev = idev->dev;
4405 struct ifinfomsg *hdr;
4406 struct nlmsghdr *nlh;
4409 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4413 hdr = nlmsg_data(nlh);
4414 hdr->ifi_family = AF_INET6;
4416 hdr->ifi_type = dev->type;
4417 hdr->ifi_index = dev->ifindex;
4418 hdr->ifi_flags = dev_get_flags(dev);
4419 hdr->ifi_change = 0;
4421 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4423 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4424 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4425 (dev->ifindex != dev->iflink &&
4426 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4427 goto nla_put_failure;
4428 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4429 if (protoinfo == NULL)
4430 goto nla_put_failure;
4432 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4433 goto nla_put_failure;
4435 nla_nest_end(skb, protoinfo);
4436 return nlmsg_end(skb, nlh);
4439 nlmsg_cancel(skb, nlh);
4443 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4445 struct net *net = sock_net(skb->sk);
4448 struct net_device *dev;
4449 struct inet6_dev *idev;
4450 struct hlist_head *head;
4453 s_idx = cb->args[1];
4456 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4458 head = &net->dev_index_head[h];
4459 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4462 idev = __in6_dev_get(dev);
4465 if (inet6_fill_ifinfo(skb, idev,
4466 NETLINK_CB(cb->skb).portid,
4468 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4482 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4484 struct sk_buff *skb;
4485 struct net *net = dev_net(idev->dev);
4488 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4492 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4494 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4495 WARN_ON(err == -EMSGSIZE);
4499 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4503 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4506 static inline size_t inet6_prefix_nlmsg_size(void)
4508 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4509 + nla_total_size(sizeof(struct in6_addr))
4510 + nla_total_size(sizeof(struct prefix_cacheinfo));
4513 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4514 struct prefix_info *pinfo, u32 portid, u32 seq,
4515 int event, unsigned int flags)
4517 struct prefixmsg *pmsg;
4518 struct nlmsghdr *nlh;
4519 struct prefix_cacheinfo ci;
4521 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4525 pmsg = nlmsg_data(nlh);
4526 pmsg->prefix_family = AF_INET6;
4527 pmsg->prefix_pad1 = 0;
4528 pmsg->prefix_pad2 = 0;
4529 pmsg->prefix_ifindex = idev->dev->ifindex;
4530 pmsg->prefix_len = pinfo->prefix_len;
4531 pmsg->prefix_type = pinfo->type;
4532 pmsg->prefix_pad3 = 0;
4533 pmsg->prefix_flags = 0;
4535 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4536 if (pinfo->autoconf)
4537 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4539 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4540 goto nla_put_failure;
4541 ci.preferred_time = ntohl(pinfo->prefered);
4542 ci.valid_time = ntohl(pinfo->valid);
4543 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4544 goto nla_put_failure;
4545 return nlmsg_end(skb, nlh);
4548 nlmsg_cancel(skb, nlh);
4552 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4553 struct prefix_info *pinfo)
4555 struct sk_buff *skb;
4556 struct net *net = dev_net(idev->dev);
4559 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4563 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
4565 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4566 WARN_ON(err == -EMSGSIZE);
4570 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4574 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4577 static void update_valid_ll_addr_cnt(struct inet6_ifaddr *ifp, int count)
4579 write_lock_bh(&ifp->idev->lock);
4580 spin_lock(&ifp->lock);
4581 if (((ifp->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|
4582 IFA_F_DADFAILED)) == IFA_F_PERMANENT) &&
4583 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL))
4584 ifp->idev->valid_ll_addr_cnt += count;
4585 WARN_ON(ifp->idev->valid_ll_addr_cnt < 0);
4586 spin_unlock(&ifp->lock);
4587 write_unlock_bh(&ifp->idev->lock);
4590 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4592 struct net *net = dev_net(ifp->idev->dev);
4594 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4598 update_valid_ll_addr_cnt(ifp, 1);
4601 * If the address was optimistic
4602 * we inserted the route at the start of
4603 * our DAD process, so we don't need
4606 if (!(ifp->rt->rt6i_node))
4607 ip6_ins_rt(ifp->rt);
4608 if (ifp->idev->cnf.forwarding)
4609 addrconf_join_anycast(ifp);
4610 if (!ipv6_addr_any(&ifp->peer_addr))
4611 addrconf_prefix_route(&ifp->peer_addr, 128,
4612 ifp->idev->dev, 0, 0);
4615 update_valid_ll_addr_cnt(ifp, -1);
4617 if (ifp->idev->cnf.forwarding)
4618 addrconf_leave_anycast(ifp);
4619 addrconf_leave_solict(ifp->idev, &ifp->addr);
4620 if (!ipv6_addr_any(&ifp->peer_addr)) {
4621 struct rt6_info *rt;
4622 struct net_device *dev = ifp->idev->dev;
4624 rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL,
4632 dst_hold(&ifp->rt->dst);
4634 if (ip6_del_rt(ifp->rt))
4635 dst_free(&ifp->rt->dst);
4638 atomic_inc(&net->ipv6.dev_addr_genid);
4639 rt_genid_bump_ipv6(net);
4642 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4645 if (likely(ifp->idev->dead == 0))
4646 __ipv6_ifa_notify(event, ifp);
4647 rcu_read_unlock_bh();
4650 #ifdef CONFIG_SYSCTL
4653 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
4654 void __user *buffer, size_t *lenp, loff_t *ppos)
4656 int *valp = ctl->data;
4659 struct ctl_table lctl;
4663 * ctl->data points to idev->cnf.forwarding, we should
4664 * not modify it until we get the rtnl lock.
4669 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4672 ret = addrconf_fixup_forwarding(ctl, valp, val);
4678 static void dev_disable_change(struct inet6_dev *idev)
4680 struct netdev_notifier_info info;
4682 if (!idev || !idev->dev)
4685 netdev_notifier_info_init(&info, idev->dev);
4686 if (idev->cnf.disable_ipv6)
4687 addrconf_notify(NULL, NETDEV_DOWN, &info);
4689 addrconf_notify(NULL, NETDEV_UP, &info);
4692 static void addrconf_disable_change(struct net *net, __s32 newf)
4694 struct net_device *dev;
4695 struct inet6_dev *idev;
4698 for_each_netdev_rcu(net, dev) {
4699 idev = __in6_dev_get(dev);
4701 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4702 idev->cnf.disable_ipv6 = newf;
4704 dev_disable_change(idev);
4710 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
4715 if (!rtnl_trylock())
4716 return restart_syscall();
4718 net = (struct net *)table->extra2;
4722 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4727 if (p == &net->ipv6.devconf_all->disable_ipv6) {
4728 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4729 addrconf_disable_change(net, newf);
4730 } else if ((!newf) ^ (!old))
4731 dev_disable_change((struct inet6_dev *)table->extra1);
4738 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
4739 void __user *buffer, size_t *lenp, loff_t *ppos)
4741 int *valp = ctl->data;
4744 struct ctl_table lctl;
4748 * ctl->data points to idev->cnf.disable_ipv6, we should
4749 * not modify it until we get the rtnl lock.
4754 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
4757 ret = addrconf_disable_ipv6(ctl, valp, val);
4763 static struct addrconf_sysctl_table
4765 struct ctl_table_header *sysctl_header;
4766 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
4767 } addrconf_sysctl __read_mostly = {
4768 .sysctl_header = NULL,
4771 .procname = "forwarding",
4772 .data = &ipv6_devconf.forwarding,
4773 .maxlen = sizeof(int),
4775 .proc_handler = addrconf_sysctl_forward,
4778 .procname = "hop_limit",
4779 .data = &ipv6_devconf.hop_limit,
4780 .maxlen = sizeof(int),
4782 .proc_handler = proc_dointvec,
4786 .data = &ipv6_devconf.mtu6,
4787 .maxlen = sizeof(int),
4789 .proc_handler = proc_dointvec,
4792 .procname = "accept_ra",
4793 .data = &ipv6_devconf.accept_ra,
4794 .maxlen = sizeof(int),
4796 .proc_handler = proc_dointvec,
4799 .procname = "accept_redirects",
4800 .data = &ipv6_devconf.accept_redirects,
4801 .maxlen = sizeof(int),
4803 .proc_handler = proc_dointvec,
4806 .procname = "autoconf",
4807 .data = &ipv6_devconf.autoconf,
4808 .maxlen = sizeof(int),
4810 .proc_handler = proc_dointvec,
4813 .procname = "dad_transmits",
4814 .data = &ipv6_devconf.dad_transmits,
4815 .maxlen = sizeof(int),
4817 .proc_handler = proc_dointvec,
4820 .procname = "router_solicitations",
4821 .data = &ipv6_devconf.rtr_solicits,
4822 .maxlen = sizeof(int),
4824 .proc_handler = proc_dointvec,
4827 .procname = "router_solicitation_interval",
4828 .data = &ipv6_devconf.rtr_solicit_interval,
4829 .maxlen = sizeof(int),
4831 .proc_handler = proc_dointvec_jiffies,
4834 .procname = "router_solicitation_delay",
4835 .data = &ipv6_devconf.rtr_solicit_delay,
4836 .maxlen = sizeof(int),
4838 .proc_handler = proc_dointvec_jiffies,
4841 .procname = "force_mld_version",
4842 .data = &ipv6_devconf.force_mld_version,
4843 .maxlen = sizeof(int),
4845 .proc_handler = proc_dointvec,
4848 .procname = "mldv1_unsolicited_report_interval",
4850 &ipv6_devconf.mldv1_unsolicited_report_interval,
4851 .maxlen = sizeof(int),
4853 .proc_handler = proc_dointvec_ms_jiffies,
4856 .procname = "mldv2_unsolicited_report_interval",
4858 &ipv6_devconf.mldv2_unsolicited_report_interval,
4859 .maxlen = sizeof(int),
4861 .proc_handler = proc_dointvec_ms_jiffies,
4863 #ifdef CONFIG_IPV6_PRIVACY
4865 .procname = "use_tempaddr",
4866 .data = &ipv6_devconf.use_tempaddr,
4867 .maxlen = sizeof(int),
4869 .proc_handler = proc_dointvec,
4872 .procname = "temp_valid_lft",
4873 .data = &ipv6_devconf.temp_valid_lft,
4874 .maxlen = sizeof(int),
4876 .proc_handler = proc_dointvec,
4879 .procname = "temp_prefered_lft",
4880 .data = &ipv6_devconf.temp_prefered_lft,
4881 .maxlen = sizeof(int),
4883 .proc_handler = proc_dointvec,
4886 .procname = "regen_max_retry",
4887 .data = &ipv6_devconf.regen_max_retry,
4888 .maxlen = sizeof(int),
4890 .proc_handler = proc_dointvec,
4893 .procname = "max_desync_factor",
4894 .data = &ipv6_devconf.max_desync_factor,
4895 .maxlen = sizeof(int),
4897 .proc_handler = proc_dointvec,
4901 .procname = "max_addresses",
4902 .data = &ipv6_devconf.max_addresses,
4903 .maxlen = sizeof(int),
4905 .proc_handler = proc_dointvec,
4908 .procname = "accept_ra_defrtr",
4909 .data = &ipv6_devconf.accept_ra_defrtr,
4910 .maxlen = sizeof(int),
4912 .proc_handler = proc_dointvec,
4915 .procname = "accept_ra_pinfo",
4916 .data = &ipv6_devconf.accept_ra_pinfo,
4917 .maxlen = sizeof(int),
4919 .proc_handler = proc_dointvec,
4921 #ifdef CONFIG_IPV6_ROUTER_PREF
4923 .procname = "accept_ra_rtr_pref",
4924 .data = &ipv6_devconf.accept_ra_rtr_pref,
4925 .maxlen = sizeof(int),
4927 .proc_handler = proc_dointvec,
4930 .procname = "router_probe_interval",
4931 .data = &ipv6_devconf.rtr_probe_interval,
4932 .maxlen = sizeof(int),
4934 .proc_handler = proc_dointvec_jiffies,
4936 #ifdef CONFIG_IPV6_ROUTE_INFO
4938 .procname = "accept_ra_rt_info_max_plen",
4939 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4940 .maxlen = sizeof(int),
4942 .proc_handler = proc_dointvec,
4947 .procname = "proxy_ndp",
4948 .data = &ipv6_devconf.proxy_ndp,
4949 .maxlen = sizeof(int),
4951 .proc_handler = proc_dointvec,
4954 .procname = "accept_source_route",
4955 .data = &ipv6_devconf.accept_source_route,
4956 .maxlen = sizeof(int),
4958 .proc_handler = proc_dointvec,
4960 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4962 .procname = "optimistic_dad",
4963 .data = &ipv6_devconf.optimistic_dad,
4964 .maxlen = sizeof(int),
4966 .proc_handler = proc_dointvec,
4970 #ifdef CONFIG_IPV6_MROUTE
4972 .procname = "mc_forwarding",
4973 .data = &ipv6_devconf.mc_forwarding,
4974 .maxlen = sizeof(int),
4976 .proc_handler = proc_dointvec,
4980 .procname = "disable_ipv6",
4981 .data = &ipv6_devconf.disable_ipv6,
4982 .maxlen = sizeof(int),
4984 .proc_handler = addrconf_sysctl_disable,
4987 .procname = "accept_dad",
4988 .data = &ipv6_devconf.accept_dad,
4989 .maxlen = sizeof(int),
4991 .proc_handler = proc_dointvec,
4994 .procname = "force_tllao",
4995 .data = &ipv6_devconf.force_tllao,
4996 .maxlen = sizeof(int),
4998 .proc_handler = proc_dointvec
5001 .procname = "ndisc_notify",
5002 .data = &ipv6_devconf.ndisc_notify,
5003 .maxlen = sizeof(int),
5005 .proc_handler = proc_dointvec
5008 .procname = "suppress_frag_ndisc",
5009 .data = &ipv6_devconf.suppress_frag_ndisc,
5010 .maxlen = sizeof(int),
5012 .proc_handler = proc_dointvec
5020 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5021 struct inet6_dev *idev, struct ipv6_devconf *p)
5024 struct addrconf_sysctl_table *t;
5025 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5027 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5031 for (i = 0; t->addrconf_vars[i].data; i++) {
5032 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5033 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5034 t->addrconf_vars[i].extra2 = net;
5037 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5039 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5040 if (t->sysctl_header == NULL)
5052 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5054 struct addrconf_sysctl_table *t;
5056 if (p->sysctl == NULL)
5061 unregister_net_sysctl_table(t->sysctl_header);
5065 static void addrconf_sysctl_register(struct inet6_dev *idev)
5067 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
5068 &ndisc_ifinfo_sysctl_change);
5069 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5073 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5075 __addrconf_sysctl_unregister(&idev->cnf);
5076 neigh_sysctl_unregister(idev->nd_parms);
5082 static int __net_init addrconf_init_net(struct net *net)
5085 struct ipv6_devconf *all, *dflt;
5087 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5091 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5093 goto err_alloc_dflt;
5095 /* these will be inherited by all namespaces */
5096 dflt->autoconf = ipv6_defaults.autoconf;
5097 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5099 net->ipv6.devconf_all = all;
5100 net->ipv6.devconf_dflt = dflt;
5102 #ifdef CONFIG_SYSCTL
5103 err = __addrconf_sysctl_register(net, "all", NULL, all);
5107 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5113 #ifdef CONFIG_SYSCTL
5115 __addrconf_sysctl_unregister(all);
5125 static void __net_exit addrconf_exit_net(struct net *net)
5127 #ifdef CONFIG_SYSCTL
5128 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5129 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5131 if (!net_eq(net, &init_net)) {
5132 kfree(net->ipv6.devconf_dflt);
5133 kfree(net->ipv6.devconf_all);
5137 static struct pernet_operations addrconf_ops = {
5138 .init = addrconf_init_net,
5139 .exit = addrconf_exit_net,
5142 static struct rtnl_af_ops inet6_ops = {
5144 .fill_link_af = inet6_fill_link_af,
5145 .get_link_af_size = inet6_get_link_af_size,
5146 .set_link_af = inet6_set_link_af,
5150 * Init / cleanup code
5153 int __init addrconf_init(void)
5157 err = ipv6_addr_label_init();
5159 pr_crit("%s: cannot initialize default policy table: %d\n",
5164 err = register_pernet_subsys(&addrconf_ops);
5168 /* The addrconf netdev notifier requires that loopback_dev
5169 * has it's ipv6 private information allocated and setup
5170 * before it can bring up and give link-local addresses
5171 * to other devices which are up.
5173 * Unfortunately, loopback_dev is not necessarily the first
5174 * entry in the global dev_base list of net devices. In fact,
5175 * it is likely to be the very last entry on that list.
5176 * So this causes the notifier registry below to try and
5177 * give link-local addresses to all devices besides loopback_dev
5178 * first, then loopback_dev, which cases all the non-loopback_dev
5179 * devices to fail to get a link-local address.
5181 * So, as a temporary fix, allocate the ipv6 structure for
5182 * loopback_dev first by hand.
5183 * Longer term, all of the dependencies ipv6 has upon the loopback
5184 * device and it being up should be removed.
5187 if (!ipv6_add_dev(init_net.loopback_dev))
5193 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5194 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5196 register_netdevice_notifier(&ipv6_dev_notf);
5200 err = rtnl_af_register(&inet6_ops);
5204 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5209 /* Only the first call to __rtnl_register can fail */
5210 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5211 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5212 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5213 inet6_dump_ifaddr, NULL);
5214 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5215 inet6_dump_ifmcaddr, NULL);
5216 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5217 inet6_dump_ifacaddr, NULL);
5218 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5219 inet6_netconf_dump_devconf, NULL);
5221 ipv6_addr_label_rtnl_register();
5225 rtnl_af_unregister(&inet6_ops);
5227 unregister_netdevice_notifier(&ipv6_dev_notf);
5229 unregister_pernet_subsys(&addrconf_ops);
5231 ipv6_addr_label_cleanup();
5236 void addrconf_cleanup(void)
5238 struct net_device *dev;
5241 unregister_netdevice_notifier(&ipv6_dev_notf);
5242 unregister_pernet_subsys(&addrconf_ops);
5243 ipv6_addr_label_cleanup();
5247 __rtnl_af_unregister(&inet6_ops);
5249 /* clean dev list */
5250 for_each_netdev(&init_net, dev) {
5251 if (__in6_dev_get(dev) == NULL)
5253 addrconf_ifdown(dev, 1);
5255 addrconf_ifdown(init_net.loopback_dev, 2);
5260 spin_lock_bh(&addrconf_hash_lock);
5261 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5262 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5263 spin_unlock_bh(&addrconf_hash_lock);
5265 del_timer(&addr_chk_timer);