Linux 4.1.16
[linux/fpc-iii.git] / net / ipv6 / addrconf.c
bloba2d685030a34fb9cfcfb63cf5e7aba4de84bea0f
1 /*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
5 * Authors:
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.
16 * Changes:
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
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
35 * seq_file.
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
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/inet.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_addr.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_arcnet.h>
55 #include <linux/if_infiniband.h>
56 #include <linux/route.h>
57 #include <linux/inetdevice.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #ifdef CONFIG_SYSCTL
61 #include <linux/sysctl.h>
62 #endif
63 #include <linux/capability.h>
64 #include <linux/delay.h>
65 #include <linux/notifier.h>
66 #include <linux/string.h>
67 #include <linux/hash.h>
69 #include <net/net_namespace.h>
70 #include <net/sock.h>
71 #include <net/snmp.h>
73 #include <net/af_ieee802154.h>
74 #include <net/firewire.h>
75 #include <net/ipv6.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
80 #include <net/tcp.h>
81 #include <net/ip.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <linux/if_tunnel.h>
85 #include <linux/rtnetlink.h>
86 #include <linux/netconf.h>
87 #include <linux/random.h>
88 #include <linux/uaccess.h>
89 #include <asm/unaligned.h>
91 #include <linux/proc_fs.h>
92 #include <linux/seq_file.h>
93 #include <linux/export.h>
95 /* Set to 3 to get tracing... */
96 #define ACONF_DEBUG 2
98 #if ACONF_DEBUG >= 3
99 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
100 #else
101 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
102 #endif
104 #define INFINITY_LIFE_TIME 0xFFFFFFFF
106 #define IPV6_MAX_STRLEN \
107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109 static inline u32 cstamp_delta(unsigned long cstamp)
111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
114 #ifdef CONFIG_SYSCTL
115 static int addrconf_sysctl_register(struct inet6_dev *idev);
116 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117 #else
118 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
120 return 0;
123 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
126 #endif
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);
132 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
133 static int ipv6_count_addresses(struct inet6_dev *idev);
134 static int ipv6_generate_stable_address(struct in6_addr *addr,
135 u8 dad_count,
136 const struct inet6_dev *idev);
139 * Configured unicast address hash table
141 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
142 static DEFINE_SPINLOCK(addrconf_hash_lock);
144 static void addrconf_verify(void);
145 static void addrconf_verify_rtnl(void);
146 static void addrconf_verify_work(struct work_struct *);
148 static struct workqueue_struct *addrconf_wq;
149 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
151 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
152 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
154 static void addrconf_type_change(struct net_device *dev,
155 unsigned long event);
156 static int addrconf_ifdown(struct net_device *dev, int how);
158 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
159 int plen,
160 const struct net_device *dev,
161 u32 flags, u32 noflags);
163 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
164 static void addrconf_dad_work(struct work_struct *w);
165 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
166 static void addrconf_dad_run(struct inet6_dev *idev);
167 static void addrconf_rs_timer(unsigned long data);
168 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
169 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
171 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
172 struct prefix_info *pinfo);
173 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
174 struct net_device *dev);
176 static struct ipv6_devconf ipv6_devconf __read_mostly = {
177 .forwarding = 0,
178 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
179 .mtu6 = IPV6_MIN_MTU,
180 .accept_ra = 1,
181 .accept_redirects = 1,
182 .autoconf = 1,
183 .force_mld_version = 0,
184 .mldv1_unsolicited_report_interval = 10 * HZ,
185 .mldv2_unsolicited_report_interval = HZ,
186 .dad_transmits = 1,
187 .rtr_solicits = MAX_RTR_SOLICITATIONS,
188 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
189 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
190 .use_tempaddr = 0,
191 .temp_valid_lft = TEMP_VALID_LIFETIME,
192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
193 .regen_max_retry = REGEN_MAX_RETRY,
194 .max_desync_factor = MAX_DESYNC_FACTOR,
195 .max_addresses = IPV6_MAX_ADDRESSES,
196 .accept_ra_defrtr = 1,
197 .accept_ra_from_local = 0,
198 .accept_ra_pinfo = 1,
199 #ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref = 1,
201 .rtr_probe_interval = 60 * HZ,
202 #ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen = 0,
204 #endif
205 #endif
206 .proxy_ndp = 0,
207 .accept_source_route = 0, /* we do not accept RH0 by default. */
208 .disable_ipv6 = 0,
209 .accept_dad = 1,
210 .suppress_frag_ndisc = 1,
211 .accept_ra_mtu = 1,
212 .stable_secret = {
213 .initialized = false,
217 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
218 .forwarding = 0,
219 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
220 .mtu6 = IPV6_MIN_MTU,
221 .accept_ra = 1,
222 .accept_redirects = 1,
223 .autoconf = 1,
224 .force_mld_version = 0,
225 .mldv1_unsolicited_report_interval = 10 * HZ,
226 .mldv2_unsolicited_report_interval = HZ,
227 .dad_transmits = 1,
228 .rtr_solicits = MAX_RTR_SOLICITATIONS,
229 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
230 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
231 .use_tempaddr = 0,
232 .temp_valid_lft = TEMP_VALID_LIFETIME,
233 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
234 .regen_max_retry = REGEN_MAX_RETRY,
235 .max_desync_factor = MAX_DESYNC_FACTOR,
236 .max_addresses = IPV6_MAX_ADDRESSES,
237 .accept_ra_defrtr = 1,
238 .accept_ra_from_local = 0,
239 .accept_ra_pinfo = 1,
240 #ifdef CONFIG_IPV6_ROUTER_PREF
241 .accept_ra_rtr_pref = 1,
242 .rtr_probe_interval = 60 * HZ,
243 #ifdef CONFIG_IPV6_ROUTE_INFO
244 .accept_ra_rt_info_max_plen = 0,
245 #endif
246 #endif
247 .proxy_ndp = 0,
248 .accept_source_route = 0, /* we do not accept RH0 by default. */
249 .disable_ipv6 = 0,
250 .accept_dad = 1,
251 .suppress_frag_ndisc = 1,
252 .accept_ra_mtu = 1,
253 .stable_secret = {
254 .initialized = false,
258 /* Check if a valid qdisc is available */
259 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
261 return !qdisc_tx_is_noop(dev);
264 static void addrconf_del_rs_timer(struct inet6_dev *idev)
266 if (del_timer(&idev->rs_timer))
267 __in6_dev_put(idev);
270 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
272 if (cancel_delayed_work(&ifp->dad_work))
273 __in6_ifa_put(ifp);
276 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
277 unsigned long when)
279 if (!timer_pending(&idev->rs_timer))
280 in6_dev_hold(idev);
281 mod_timer(&idev->rs_timer, jiffies + when);
284 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
285 unsigned long delay)
287 if (!delayed_work_pending(&ifp->dad_work))
288 in6_ifa_hold(ifp);
289 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
292 static int snmp6_alloc_dev(struct inet6_dev *idev)
294 int i;
296 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
297 if (!idev->stats.ipv6)
298 goto err_ip;
300 for_each_possible_cpu(i) {
301 struct ipstats_mib *addrconf_stats;
302 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
303 u64_stats_init(&addrconf_stats->syncp);
307 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
308 GFP_KERNEL);
309 if (!idev->stats.icmpv6dev)
310 goto err_icmp;
311 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
312 GFP_KERNEL);
313 if (!idev->stats.icmpv6msgdev)
314 goto err_icmpmsg;
316 return 0;
318 err_icmpmsg:
319 kfree(idev->stats.icmpv6dev);
320 err_icmp:
321 free_percpu(idev->stats.ipv6);
322 err_ip:
323 return -ENOMEM;
326 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
328 struct inet6_dev *ndev;
329 int err = -ENOMEM;
331 ASSERT_RTNL();
333 if (dev->mtu < IPV6_MIN_MTU)
334 return ERR_PTR(-EINVAL);
336 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
337 if (!ndev)
338 return ERR_PTR(err);
340 rwlock_init(&ndev->lock);
341 ndev->dev = dev;
342 INIT_LIST_HEAD(&ndev->addr_list);
343 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
344 (unsigned long)ndev);
345 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
347 if (ndev->cnf.stable_secret.initialized)
348 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
349 else
350 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
352 ndev->cnf.mtu6 = dev->mtu;
353 ndev->cnf.sysctl = NULL;
354 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
355 if (!ndev->nd_parms) {
356 kfree(ndev);
357 return ERR_PTR(err);
359 if (ndev->cnf.forwarding)
360 dev_disable_lro(dev);
361 /* We refer to the device */
362 dev_hold(dev);
364 if (snmp6_alloc_dev(ndev) < 0) {
365 ADBG(KERN_WARNING
366 "%s: cannot allocate memory for statistics; dev=%s.\n",
367 __func__, dev->name);
368 neigh_parms_release(&nd_tbl, ndev->nd_parms);
369 dev_put(dev);
370 kfree(ndev);
371 return ERR_PTR(err);
374 if (snmp6_register_dev(ndev) < 0) {
375 ADBG(KERN_WARNING
376 "%s: cannot create /proc/net/dev_snmp6/%s\n",
377 __func__, dev->name);
378 goto err_release;
381 /* One reference from device. We must do this before
382 * we invoke __ipv6_regen_rndid().
384 in6_dev_hold(ndev);
386 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
387 ndev->cnf.accept_dad = -1;
389 #if IS_ENABLED(CONFIG_IPV6_SIT)
390 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
391 pr_info("%s: Disabled Multicast RS\n", dev->name);
392 ndev->cnf.rtr_solicits = 0;
394 #endif
396 INIT_LIST_HEAD(&ndev->tempaddr_list);
397 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
398 if ((dev->flags&IFF_LOOPBACK) ||
399 dev->type == ARPHRD_TUNNEL ||
400 dev->type == ARPHRD_TUNNEL6 ||
401 dev->type == ARPHRD_SIT ||
402 dev->type == ARPHRD_NONE) {
403 ndev->cnf.use_tempaddr = -1;
404 } else {
405 in6_dev_hold(ndev);
406 ipv6_regen_rndid((unsigned long) ndev);
409 ndev->token = in6addr_any;
411 if (netif_running(dev) && addrconf_qdisc_ok(dev))
412 ndev->if_flags |= IF_READY;
414 ipv6_mc_init_dev(ndev);
415 ndev->tstamp = jiffies;
416 err = addrconf_sysctl_register(ndev);
417 if (err) {
418 ipv6_mc_destroy_dev(ndev);
419 del_timer(&ndev->regen_timer);
420 snmp6_unregister_dev(ndev);
421 goto err_release;
423 /* protected by rtnl_lock */
424 rcu_assign_pointer(dev->ip6_ptr, ndev);
426 /* Join interface-local all-node multicast group */
427 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
429 /* Join all-node multicast group */
430 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
432 /* Join all-router multicast group if forwarding is set */
433 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
434 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
436 return ndev;
438 err_release:
439 neigh_parms_release(&nd_tbl, ndev->nd_parms);
440 ndev->dead = 1;
441 in6_dev_finish_destroy(ndev);
442 return ERR_PTR(err);
445 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
447 struct inet6_dev *idev;
449 ASSERT_RTNL();
451 idev = __in6_dev_get(dev);
452 if (!idev) {
453 idev = ipv6_add_dev(dev);
454 if (IS_ERR(idev))
455 return NULL;
458 if (dev->flags&IFF_UP)
459 ipv6_mc_up(idev);
460 return idev;
463 static int inet6_netconf_msgsize_devconf(int type)
465 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
466 + nla_total_size(4); /* NETCONFA_IFINDEX */
468 /* type -1 is used for ALL */
469 if (type == -1 || type == NETCONFA_FORWARDING)
470 size += nla_total_size(4);
471 #ifdef CONFIG_IPV6_MROUTE
472 if (type == -1 || type == NETCONFA_MC_FORWARDING)
473 size += nla_total_size(4);
474 #endif
475 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
476 size += nla_total_size(4);
478 return size;
481 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
482 struct ipv6_devconf *devconf, u32 portid,
483 u32 seq, int event, unsigned int flags,
484 int type)
486 struct nlmsghdr *nlh;
487 struct netconfmsg *ncm;
489 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
490 flags);
491 if (!nlh)
492 return -EMSGSIZE;
494 ncm = nlmsg_data(nlh);
495 ncm->ncm_family = AF_INET6;
497 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
498 goto nla_put_failure;
500 /* type -1 is used for ALL */
501 if ((type == -1 || type == NETCONFA_FORWARDING) &&
502 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
503 goto nla_put_failure;
504 #ifdef CONFIG_IPV6_MROUTE
505 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
506 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
507 devconf->mc_forwarding) < 0)
508 goto nla_put_failure;
509 #endif
510 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
511 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
512 goto nla_put_failure;
514 nlmsg_end(skb, nlh);
515 return 0;
517 nla_put_failure:
518 nlmsg_cancel(skb, nlh);
519 return -EMSGSIZE;
522 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
523 struct ipv6_devconf *devconf)
525 struct sk_buff *skb;
526 int err = -ENOBUFS;
528 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
529 if (!skb)
530 goto errout;
532 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
533 RTM_NEWNETCONF, 0, type);
534 if (err < 0) {
535 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
536 WARN_ON(err == -EMSGSIZE);
537 kfree_skb(skb);
538 goto errout;
540 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
541 return;
542 errout:
543 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
546 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
547 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
548 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
549 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
552 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
553 struct nlmsghdr *nlh)
555 struct net *net = sock_net(in_skb->sk);
556 struct nlattr *tb[NETCONFA_MAX+1];
557 struct netconfmsg *ncm;
558 struct sk_buff *skb;
559 struct ipv6_devconf *devconf;
560 struct inet6_dev *in6_dev;
561 struct net_device *dev;
562 int ifindex;
563 int err;
565 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
566 devconf_ipv6_policy);
567 if (err < 0)
568 goto errout;
570 err = EINVAL;
571 if (!tb[NETCONFA_IFINDEX])
572 goto errout;
574 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
575 switch (ifindex) {
576 case NETCONFA_IFINDEX_ALL:
577 devconf = net->ipv6.devconf_all;
578 break;
579 case NETCONFA_IFINDEX_DEFAULT:
580 devconf = net->ipv6.devconf_dflt;
581 break;
582 default:
583 dev = __dev_get_by_index(net, ifindex);
584 if (!dev)
585 goto errout;
586 in6_dev = __in6_dev_get(dev);
587 if (!in6_dev)
588 goto errout;
589 devconf = &in6_dev->cnf;
590 break;
593 err = -ENOBUFS;
594 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
595 if (!skb)
596 goto errout;
598 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
599 NETLINK_CB(in_skb).portid,
600 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
601 -1);
602 if (err < 0) {
603 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
604 WARN_ON(err == -EMSGSIZE);
605 kfree_skb(skb);
606 goto errout;
608 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
609 errout:
610 return err;
613 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
614 struct netlink_callback *cb)
616 struct net *net = sock_net(skb->sk);
617 int h, s_h;
618 int idx, s_idx;
619 struct net_device *dev;
620 struct inet6_dev *idev;
621 struct hlist_head *head;
623 s_h = cb->args[0];
624 s_idx = idx = cb->args[1];
626 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
627 idx = 0;
628 head = &net->dev_index_head[h];
629 rcu_read_lock();
630 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
631 net->dev_base_seq;
632 hlist_for_each_entry_rcu(dev, head, index_hlist) {
633 if (idx < s_idx)
634 goto cont;
635 idev = __in6_dev_get(dev);
636 if (!idev)
637 goto cont;
639 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
640 &idev->cnf,
641 NETLINK_CB(cb->skb).portid,
642 cb->nlh->nlmsg_seq,
643 RTM_NEWNETCONF,
644 NLM_F_MULTI,
645 -1) < 0) {
646 rcu_read_unlock();
647 goto done;
649 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
650 cont:
651 idx++;
653 rcu_read_unlock();
655 if (h == NETDEV_HASHENTRIES) {
656 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
657 net->ipv6.devconf_all,
658 NETLINK_CB(cb->skb).portid,
659 cb->nlh->nlmsg_seq,
660 RTM_NEWNETCONF, NLM_F_MULTI,
661 -1) < 0)
662 goto done;
663 else
664 h++;
666 if (h == NETDEV_HASHENTRIES + 1) {
667 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
668 net->ipv6.devconf_dflt,
669 NETLINK_CB(cb->skb).portid,
670 cb->nlh->nlmsg_seq,
671 RTM_NEWNETCONF, NLM_F_MULTI,
672 -1) < 0)
673 goto done;
674 else
675 h++;
677 done:
678 cb->args[0] = h;
679 cb->args[1] = idx;
681 return skb->len;
684 #ifdef CONFIG_SYSCTL
685 static void dev_forward_change(struct inet6_dev *idev)
687 struct net_device *dev;
688 struct inet6_ifaddr *ifa;
690 if (!idev)
691 return;
692 dev = idev->dev;
693 if (idev->cnf.forwarding)
694 dev_disable_lro(dev);
695 if (dev->flags & IFF_MULTICAST) {
696 if (idev->cnf.forwarding) {
697 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
698 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
699 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
700 } else {
701 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
702 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
703 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
707 list_for_each_entry(ifa, &idev->addr_list, if_list) {
708 if (ifa->flags&IFA_F_TENTATIVE)
709 continue;
710 if (idev->cnf.forwarding)
711 addrconf_join_anycast(ifa);
712 else
713 addrconf_leave_anycast(ifa);
715 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
716 dev->ifindex, &idev->cnf);
720 static void addrconf_forward_change(struct net *net, __s32 newf)
722 struct net_device *dev;
723 struct inet6_dev *idev;
725 for_each_netdev(net, dev) {
726 idev = __in6_dev_get(dev);
727 if (idev) {
728 int changed = (!idev->cnf.forwarding) ^ (!newf);
729 idev->cnf.forwarding = newf;
730 if (changed)
731 dev_forward_change(idev);
736 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
738 struct net *net;
739 int old;
741 if (!rtnl_trylock())
742 return restart_syscall();
744 net = (struct net *)table->extra2;
745 old = *p;
746 *p = newf;
748 if (p == &net->ipv6.devconf_dflt->forwarding) {
749 if ((!newf) ^ (!old))
750 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
751 NETCONFA_IFINDEX_DEFAULT,
752 net->ipv6.devconf_dflt);
753 rtnl_unlock();
754 return 0;
757 if (p == &net->ipv6.devconf_all->forwarding) {
758 net->ipv6.devconf_dflt->forwarding = newf;
759 addrconf_forward_change(net, newf);
760 if ((!newf) ^ (!old))
761 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
762 NETCONFA_IFINDEX_ALL,
763 net->ipv6.devconf_all);
764 } else if ((!newf) ^ (!old))
765 dev_forward_change((struct inet6_dev *)table->extra1);
766 rtnl_unlock();
768 if (newf)
769 rt6_purge_dflt_routers(net);
770 return 1;
772 #endif
774 /* Nobody refers to this ifaddr, destroy it */
775 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
777 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
779 #ifdef NET_REFCNT_DEBUG
780 pr_debug("%s\n", __func__);
781 #endif
783 in6_dev_put(ifp->idev);
785 if (cancel_delayed_work(&ifp->dad_work))
786 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
787 ifp);
789 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
790 pr_warn("Freeing alive inet6 address %p\n", ifp);
791 return;
793 ip6_rt_put(ifp->rt);
795 kfree_rcu(ifp, rcu);
798 static void
799 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
801 struct list_head *p;
802 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
805 * Each device address list is sorted in order of scope -
806 * global before linklocal.
808 list_for_each(p, &idev->addr_list) {
809 struct inet6_ifaddr *ifa
810 = list_entry(p, struct inet6_ifaddr, if_list);
811 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
812 break;
815 list_add_tail(&ifp->if_list, p);
818 static u32 inet6_addr_hash(const struct in6_addr *addr)
820 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
823 /* On success it returns ifp with increased reference count */
825 static struct inet6_ifaddr *
826 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
827 const struct in6_addr *peer_addr, int pfxlen,
828 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
830 struct inet6_ifaddr *ifa = NULL;
831 struct rt6_info *rt;
832 unsigned int hash;
833 int err = 0;
834 int addr_type = ipv6_addr_type(addr);
836 if (addr_type == IPV6_ADDR_ANY ||
837 addr_type & IPV6_ADDR_MULTICAST ||
838 (!(idev->dev->flags & IFF_LOOPBACK) &&
839 addr_type & IPV6_ADDR_LOOPBACK))
840 return ERR_PTR(-EADDRNOTAVAIL);
842 rcu_read_lock_bh();
843 if (idev->dead) {
844 err = -ENODEV; /*XXX*/
845 goto out2;
848 if (idev->cnf.disable_ipv6) {
849 err = -EACCES;
850 goto out2;
853 spin_lock(&addrconf_hash_lock);
855 /* Ignore adding duplicate addresses on an interface */
856 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
857 ADBG("ipv6_add_addr: already assigned\n");
858 err = -EEXIST;
859 goto out;
862 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
864 if (!ifa) {
865 ADBG("ipv6_add_addr: malloc failed\n");
866 err = -ENOBUFS;
867 goto out;
870 rt = addrconf_dst_alloc(idev, addr, false);
871 if (IS_ERR(rt)) {
872 err = PTR_ERR(rt);
873 goto out;
876 neigh_parms_data_state_setall(idev->nd_parms);
878 ifa->addr = *addr;
879 if (peer_addr)
880 ifa->peer_addr = *peer_addr;
882 spin_lock_init(&ifa->lock);
883 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
884 INIT_HLIST_NODE(&ifa->addr_lst);
885 ifa->scope = scope;
886 ifa->prefix_len = pfxlen;
887 ifa->flags = flags | IFA_F_TENTATIVE;
888 ifa->valid_lft = valid_lft;
889 ifa->prefered_lft = prefered_lft;
890 ifa->cstamp = ifa->tstamp = jiffies;
891 ifa->tokenized = false;
893 ifa->rt = rt;
895 ifa->idev = idev;
896 in6_dev_hold(idev);
897 /* For caller */
898 in6_ifa_hold(ifa);
900 /* Add to big hash table */
901 hash = inet6_addr_hash(addr);
903 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
904 spin_unlock(&addrconf_hash_lock);
906 write_lock(&idev->lock);
907 /* Add to inet6_dev unicast addr list. */
908 ipv6_link_dev_addr(idev, ifa);
910 if (ifa->flags&IFA_F_TEMPORARY) {
911 list_add(&ifa->tmp_list, &idev->tempaddr_list);
912 in6_ifa_hold(ifa);
915 in6_ifa_hold(ifa);
916 write_unlock(&idev->lock);
917 out2:
918 rcu_read_unlock_bh();
920 if (likely(err == 0))
921 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
922 else {
923 kfree(ifa);
924 ifa = ERR_PTR(err);
927 return ifa;
928 out:
929 spin_unlock(&addrconf_hash_lock);
930 goto out2;
933 enum cleanup_prefix_rt_t {
934 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
935 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
936 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
940 * Check, whether the prefix for ifp would still need a prefix route
941 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
942 * constants.
944 * 1) we don't purge prefix if address was not permanent.
945 * prefix is managed by its own lifetime.
946 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
947 * 3) if there are no addresses, delete prefix.
948 * 4) if there are still other permanent address(es),
949 * corresponding prefix is still permanent.
950 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
951 * don't purge the prefix, assume user space is managing it.
952 * 6) otherwise, update prefix lifetime to the
953 * longest valid lifetime among the corresponding
954 * addresses on the device.
955 * Note: subsequent RA will update lifetime.
957 static enum cleanup_prefix_rt_t
958 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
960 struct inet6_ifaddr *ifa;
961 struct inet6_dev *idev = ifp->idev;
962 unsigned long lifetime;
963 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
965 *expires = jiffies;
967 list_for_each_entry(ifa, &idev->addr_list, if_list) {
968 if (ifa == ifp)
969 continue;
970 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
971 ifp->prefix_len))
972 continue;
973 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
974 return CLEANUP_PREFIX_RT_NOP;
976 action = CLEANUP_PREFIX_RT_EXPIRE;
978 spin_lock(&ifa->lock);
980 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
982 * Note: Because this address is
983 * not permanent, lifetime <
984 * LONG_MAX / HZ here.
986 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
987 *expires = ifa->tstamp + lifetime * HZ;
988 spin_unlock(&ifa->lock);
991 return action;
994 static void
995 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
997 struct rt6_info *rt;
999 rt = addrconf_get_prefix_route(&ifp->addr,
1000 ifp->prefix_len,
1001 ifp->idev->dev,
1002 0, RTF_GATEWAY | RTF_DEFAULT);
1003 if (rt) {
1004 if (del_rt)
1005 ip6_del_rt(rt);
1006 else {
1007 if (!(rt->rt6i_flags & RTF_EXPIRES))
1008 rt6_set_expires(rt, expires);
1009 ip6_rt_put(rt);
1015 /* This function wants to get referenced ifp and releases it before return */
1017 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1019 int state;
1020 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1021 unsigned long expires;
1023 ASSERT_RTNL();
1025 spin_lock_bh(&ifp->lock);
1026 state = ifp->state;
1027 ifp->state = INET6_IFADDR_STATE_DEAD;
1028 spin_unlock_bh(&ifp->lock);
1030 if (state == INET6_IFADDR_STATE_DEAD)
1031 goto out;
1033 spin_lock_bh(&addrconf_hash_lock);
1034 hlist_del_init_rcu(&ifp->addr_lst);
1035 spin_unlock_bh(&addrconf_hash_lock);
1037 write_lock_bh(&ifp->idev->lock);
1039 if (ifp->flags&IFA_F_TEMPORARY) {
1040 list_del(&ifp->tmp_list);
1041 if (ifp->ifpub) {
1042 in6_ifa_put(ifp->ifpub);
1043 ifp->ifpub = NULL;
1045 __in6_ifa_put(ifp);
1048 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1049 action = check_cleanup_prefix_route(ifp, &expires);
1051 list_del_init(&ifp->if_list);
1052 __in6_ifa_put(ifp);
1054 write_unlock_bh(&ifp->idev->lock);
1056 addrconf_del_dad_work(ifp);
1058 ipv6_ifa_notify(RTM_DELADDR, ifp);
1060 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1062 if (action != CLEANUP_PREFIX_RT_NOP) {
1063 cleanup_prefix_route(ifp, expires,
1064 action == CLEANUP_PREFIX_RT_DEL);
1067 /* clean up prefsrc entries */
1068 rt6_remove_prefsrc(ifp);
1069 out:
1070 in6_ifa_put(ifp);
1073 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1075 struct inet6_dev *idev = ifp->idev;
1076 struct in6_addr addr, *tmpaddr;
1077 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1078 unsigned long regen_advance;
1079 int tmp_plen;
1080 int ret = 0;
1081 u32 addr_flags;
1082 unsigned long now = jiffies;
1084 write_lock_bh(&idev->lock);
1085 if (ift) {
1086 spin_lock_bh(&ift->lock);
1087 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1088 spin_unlock_bh(&ift->lock);
1089 tmpaddr = &addr;
1090 } else {
1091 tmpaddr = NULL;
1093 retry:
1094 in6_dev_hold(idev);
1095 if (idev->cnf.use_tempaddr <= 0) {
1096 write_unlock_bh(&idev->lock);
1097 pr_info("%s: use_tempaddr is disabled\n", __func__);
1098 in6_dev_put(idev);
1099 ret = -1;
1100 goto out;
1102 spin_lock_bh(&ifp->lock);
1103 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1104 idev->cnf.use_tempaddr = -1; /*XXX*/
1105 spin_unlock_bh(&ifp->lock);
1106 write_unlock_bh(&idev->lock);
1107 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1108 __func__);
1109 in6_dev_put(idev);
1110 ret = -1;
1111 goto out;
1113 in6_ifa_hold(ifp);
1114 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1115 __ipv6_try_regen_rndid(idev, tmpaddr);
1116 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1117 age = (now - ifp->tstamp) / HZ;
1118 tmp_valid_lft = min_t(__u32,
1119 ifp->valid_lft,
1120 idev->cnf.temp_valid_lft + age);
1121 tmp_prefered_lft = min_t(__u32,
1122 ifp->prefered_lft,
1123 idev->cnf.temp_prefered_lft + age -
1124 idev->cnf.max_desync_factor);
1125 tmp_plen = ifp->prefix_len;
1126 tmp_tstamp = ifp->tstamp;
1127 spin_unlock_bh(&ifp->lock);
1129 regen_advance = idev->cnf.regen_max_retry *
1130 idev->cnf.dad_transmits *
1131 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1132 write_unlock_bh(&idev->lock);
1134 /* A temporary address is created only if this calculated Preferred
1135 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1136 * an implementation must not create a temporary address with a zero
1137 * Preferred Lifetime.
1138 * Use age calculation as in addrconf_verify to avoid unnecessary
1139 * temporary addresses being generated.
1141 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1142 if (tmp_prefered_lft <= regen_advance + age) {
1143 in6_ifa_put(ifp);
1144 in6_dev_put(idev);
1145 ret = -1;
1146 goto out;
1149 addr_flags = IFA_F_TEMPORARY;
1150 /* set in addrconf_prefix_rcv() */
1151 if (ifp->flags & IFA_F_OPTIMISTIC)
1152 addr_flags |= IFA_F_OPTIMISTIC;
1154 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1155 ipv6_addr_scope(&addr), addr_flags,
1156 tmp_valid_lft, tmp_prefered_lft);
1157 if (IS_ERR(ift)) {
1158 in6_ifa_put(ifp);
1159 in6_dev_put(idev);
1160 pr_info("%s: retry temporary address regeneration\n", __func__);
1161 tmpaddr = &addr;
1162 write_lock_bh(&idev->lock);
1163 goto retry;
1166 spin_lock_bh(&ift->lock);
1167 ift->ifpub = ifp;
1168 ift->cstamp = now;
1169 ift->tstamp = tmp_tstamp;
1170 spin_unlock_bh(&ift->lock);
1172 addrconf_dad_start(ift);
1173 in6_ifa_put(ift);
1174 in6_dev_put(idev);
1175 out:
1176 return ret;
1180 * Choose an appropriate source address (RFC3484)
1182 enum {
1183 IPV6_SADDR_RULE_INIT = 0,
1184 IPV6_SADDR_RULE_LOCAL,
1185 IPV6_SADDR_RULE_SCOPE,
1186 IPV6_SADDR_RULE_PREFERRED,
1187 #ifdef CONFIG_IPV6_MIP6
1188 IPV6_SADDR_RULE_HOA,
1189 #endif
1190 IPV6_SADDR_RULE_OIF,
1191 IPV6_SADDR_RULE_LABEL,
1192 IPV6_SADDR_RULE_PRIVACY,
1193 IPV6_SADDR_RULE_ORCHID,
1194 IPV6_SADDR_RULE_PREFIX,
1195 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1196 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1197 #endif
1198 IPV6_SADDR_RULE_MAX
1201 struct ipv6_saddr_score {
1202 int rule;
1203 int addr_type;
1204 struct inet6_ifaddr *ifa;
1205 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1206 int scopedist;
1207 int matchlen;
1210 struct ipv6_saddr_dst {
1211 const struct in6_addr *addr;
1212 int ifindex;
1213 int scope;
1214 int label;
1215 unsigned int prefs;
1218 static inline int ipv6_saddr_preferred(int type)
1220 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1221 return 1;
1222 return 0;
1225 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1227 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1228 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1229 #else
1230 return false;
1231 #endif
1234 static int ipv6_get_saddr_eval(struct net *net,
1235 struct ipv6_saddr_score *score,
1236 struct ipv6_saddr_dst *dst,
1237 int i)
1239 int ret;
1241 if (i <= score->rule) {
1242 switch (i) {
1243 case IPV6_SADDR_RULE_SCOPE:
1244 ret = score->scopedist;
1245 break;
1246 case IPV6_SADDR_RULE_PREFIX:
1247 ret = score->matchlen;
1248 break;
1249 default:
1250 ret = !!test_bit(i, score->scorebits);
1252 goto out;
1255 switch (i) {
1256 case IPV6_SADDR_RULE_INIT:
1257 /* Rule 0: remember if hiscore is not ready yet */
1258 ret = !!score->ifa;
1259 break;
1260 case IPV6_SADDR_RULE_LOCAL:
1261 /* Rule 1: Prefer same address */
1262 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1263 break;
1264 case IPV6_SADDR_RULE_SCOPE:
1265 /* Rule 2: Prefer appropriate scope
1267 * ret
1269 * -1 | d 15
1270 * ---+--+-+---> scope
1272 * | d is scope of the destination.
1273 * B-d | \
1274 * | \ <- smaller scope is better if
1275 * B-15 | \ if scope is enough for destination.
1276 * | ret = B - scope (-1 <= scope >= d <= 15).
1277 * d-C-1 | /
1278 * |/ <- greater is better
1279 * -C / if scope is not enough for destination.
1280 * /| ret = scope - C (-1 <= d < scope <= 15).
1282 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1283 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1284 * Assume B = 0 and we get C > 29.
1286 ret = __ipv6_addr_src_scope(score->addr_type);
1287 if (ret >= dst->scope)
1288 ret = -ret;
1289 else
1290 ret -= 128; /* 30 is enough */
1291 score->scopedist = ret;
1292 break;
1293 case IPV6_SADDR_RULE_PREFERRED:
1295 /* Rule 3: Avoid deprecated and optimistic addresses */
1296 u8 avoid = IFA_F_DEPRECATED;
1298 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1299 avoid |= IFA_F_OPTIMISTIC;
1300 ret = ipv6_saddr_preferred(score->addr_type) ||
1301 !(score->ifa->flags & avoid);
1302 break;
1304 #ifdef CONFIG_IPV6_MIP6
1305 case IPV6_SADDR_RULE_HOA:
1307 /* Rule 4: Prefer home address */
1308 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1309 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1310 break;
1312 #endif
1313 case IPV6_SADDR_RULE_OIF:
1314 /* Rule 5: Prefer outgoing interface */
1315 ret = (!dst->ifindex ||
1316 dst->ifindex == score->ifa->idev->dev->ifindex);
1317 break;
1318 case IPV6_SADDR_RULE_LABEL:
1319 /* Rule 6: Prefer matching label */
1320 ret = ipv6_addr_label(net,
1321 &score->ifa->addr, score->addr_type,
1322 score->ifa->idev->dev->ifindex) == dst->label;
1323 break;
1324 case IPV6_SADDR_RULE_PRIVACY:
1326 /* Rule 7: Prefer public address
1327 * Note: prefer temporary address if use_tempaddr >= 2
1329 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1330 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1331 score->ifa->idev->cnf.use_tempaddr >= 2;
1332 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1333 break;
1335 case IPV6_SADDR_RULE_ORCHID:
1336 /* Rule 8-: Prefer ORCHID vs ORCHID or
1337 * non-ORCHID vs non-ORCHID
1339 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1340 ipv6_addr_orchid(dst->addr));
1341 break;
1342 case IPV6_SADDR_RULE_PREFIX:
1343 /* Rule 8: Use longest matching prefix */
1344 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1345 if (ret > score->ifa->prefix_len)
1346 ret = score->ifa->prefix_len;
1347 score->matchlen = ret;
1348 break;
1349 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1350 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1351 /* Optimistic addresses still have lower precedence than other
1352 * preferred addresses.
1354 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1355 break;
1356 #endif
1357 default:
1358 ret = 0;
1361 if (ret)
1362 __set_bit(i, score->scorebits);
1363 score->rule = i;
1364 out:
1365 return ret;
1368 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1369 const struct in6_addr *daddr, unsigned int prefs,
1370 struct in6_addr *saddr)
1372 struct ipv6_saddr_score scores[2],
1373 *score = &scores[0], *hiscore = &scores[1];
1374 struct ipv6_saddr_dst dst;
1375 struct net_device *dev;
1376 int dst_type;
1378 dst_type = __ipv6_addr_type(daddr);
1379 dst.addr = daddr;
1380 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1381 dst.scope = __ipv6_addr_src_scope(dst_type);
1382 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1383 dst.prefs = prefs;
1385 hiscore->rule = -1;
1386 hiscore->ifa = NULL;
1388 rcu_read_lock();
1390 for_each_netdev_rcu(net, dev) {
1391 struct inet6_dev *idev;
1393 /* Candidate Source Address (section 4)
1394 * - multicast and link-local destination address,
1395 * the set of candidate source address MUST only
1396 * include addresses assigned to interfaces
1397 * belonging to the same link as the outgoing
1398 * interface.
1399 * (- For site-local destination addresses, the
1400 * set of candidate source addresses MUST only
1401 * include addresses assigned to interfaces
1402 * belonging to the same site as the outgoing
1403 * interface.)
1405 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1406 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1407 dst.ifindex && dev->ifindex != dst.ifindex)
1408 continue;
1410 idev = __in6_dev_get(dev);
1411 if (!idev)
1412 continue;
1414 read_lock_bh(&idev->lock);
1415 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1416 int i;
1419 * - Tentative Address (RFC2462 section 5.4)
1420 * - A tentative address is not considered
1421 * "assigned to an interface" in the traditional
1422 * sense, unless it is also flagged as optimistic.
1423 * - Candidate Source Address (section 4)
1424 * - In any case, anycast addresses, multicast
1425 * addresses, and the unspecified address MUST
1426 * NOT be included in a candidate set.
1428 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1429 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1430 continue;
1432 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1434 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1435 score->addr_type & IPV6_ADDR_MULTICAST)) {
1436 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1437 dev->name);
1438 continue;
1441 score->rule = -1;
1442 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1444 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1445 int minihiscore, miniscore;
1447 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1448 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1450 if (minihiscore > miniscore) {
1451 if (i == IPV6_SADDR_RULE_SCOPE &&
1452 score->scopedist > 0) {
1454 * special case:
1455 * each remaining entry
1456 * has too small (not enough)
1457 * scope, because ifa entries
1458 * are sorted by their scope
1459 * values.
1461 goto try_nextdev;
1463 break;
1464 } else if (minihiscore < miniscore) {
1465 if (hiscore->ifa)
1466 in6_ifa_put(hiscore->ifa);
1468 in6_ifa_hold(score->ifa);
1470 swap(hiscore, score);
1472 /* restore our iterator */
1473 score->ifa = hiscore->ifa;
1475 break;
1479 try_nextdev:
1480 read_unlock_bh(&idev->lock);
1482 rcu_read_unlock();
1484 if (!hiscore->ifa)
1485 return -EADDRNOTAVAIL;
1487 *saddr = hiscore->ifa->addr;
1488 in6_ifa_put(hiscore->ifa);
1489 return 0;
1491 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1493 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1494 u32 banned_flags)
1496 struct inet6_ifaddr *ifp;
1497 int err = -EADDRNOTAVAIL;
1499 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1500 if (ifp->scope > IFA_LINK)
1501 break;
1502 if (ifp->scope == IFA_LINK &&
1503 !(ifp->flags & banned_flags)) {
1504 *addr = ifp->addr;
1505 err = 0;
1506 break;
1509 return err;
1512 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1513 u32 banned_flags)
1515 struct inet6_dev *idev;
1516 int err = -EADDRNOTAVAIL;
1518 rcu_read_lock();
1519 idev = __in6_dev_get(dev);
1520 if (idev) {
1521 read_lock_bh(&idev->lock);
1522 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1523 read_unlock_bh(&idev->lock);
1525 rcu_read_unlock();
1526 return err;
1529 static int ipv6_count_addresses(struct inet6_dev *idev)
1531 int cnt = 0;
1532 struct inet6_ifaddr *ifp;
1534 read_lock_bh(&idev->lock);
1535 list_for_each_entry(ifp, &idev->addr_list, if_list)
1536 cnt++;
1537 read_unlock_bh(&idev->lock);
1538 return cnt;
1541 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1542 const struct net_device *dev, int strict)
1544 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1546 EXPORT_SYMBOL(ipv6_chk_addr);
1548 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1549 const struct net_device *dev, int strict,
1550 u32 banned_flags)
1552 struct inet6_ifaddr *ifp;
1553 unsigned int hash = inet6_addr_hash(addr);
1554 u32 ifp_flags;
1556 rcu_read_lock_bh();
1557 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1558 if (!net_eq(dev_net(ifp->idev->dev), net))
1559 continue;
1560 /* Decouple optimistic from tentative for evaluation here.
1561 * Ban optimistic addresses explicitly, when required.
1563 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1564 ? (ifp->flags&~IFA_F_TENTATIVE)
1565 : ifp->flags;
1566 if (ipv6_addr_equal(&ifp->addr, addr) &&
1567 !(ifp_flags&banned_flags) &&
1568 (!dev || ifp->idev->dev == dev ||
1569 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1570 rcu_read_unlock_bh();
1571 return 1;
1575 rcu_read_unlock_bh();
1576 return 0;
1578 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1580 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1581 struct net_device *dev)
1583 unsigned int hash = inet6_addr_hash(addr);
1584 struct inet6_ifaddr *ifp;
1586 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1587 if (!net_eq(dev_net(ifp->idev->dev), net))
1588 continue;
1589 if (ipv6_addr_equal(&ifp->addr, addr)) {
1590 if (!dev || ifp->idev->dev == dev)
1591 return true;
1594 return false;
1597 /* Compares an address/prefix_len with addresses on device @dev.
1598 * If one is found it returns true.
1600 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1601 const unsigned int prefix_len, struct net_device *dev)
1603 struct inet6_dev *idev;
1604 struct inet6_ifaddr *ifa;
1605 bool ret = false;
1607 rcu_read_lock();
1608 idev = __in6_dev_get(dev);
1609 if (idev) {
1610 read_lock_bh(&idev->lock);
1611 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1612 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1613 if (ret)
1614 break;
1616 read_unlock_bh(&idev->lock);
1618 rcu_read_unlock();
1620 return ret;
1622 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1624 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1626 struct inet6_dev *idev;
1627 struct inet6_ifaddr *ifa;
1628 int onlink;
1630 onlink = 0;
1631 rcu_read_lock();
1632 idev = __in6_dev_get(dev);
1633 if (idev) {
1634 read_lock_bh(&idev->lock);
1635 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1636 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1637 ifa->prefix_len);
1638 if (onlink)
1639 break;
1641 read_unlock_bh(&idev->lock);
1643 rcu_read_unlock();
1644 return onlink;
1646 EXPORT_SYMBOL(ipv6_chk_prefix);
1648 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1649 struct net_device *dev, int strict)
1651 struct inet6_ifaddr *ifp, *result = NULL;
1652 unsigned int hash = inet6_addr_hash(addr);
1654 rcu_read_lock_bh();
1655 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1656 if (!net_eq(dev_net(ifp->idev->dev), net))
1657 continue;
1658 if (ipv6_addr_equal(&ifp->addr, addr)) {
1659 if (!dev || ifp->idev->dev == dev ||
1660 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1661 result = ifp;
1662 in6_ifa_hold(ifp);
1663 break;
1667 rcu_read_unlock_bh();
1669 return result;
1672 /* Gets referenced address, destroys ifaddr */
1674 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1676 if (ifp->flags&IFA_F_PERMANENT) {
1677 spin_lock_bh(&ifp->lock);
1678 addrconf_del_dad_work(ifp);
1679 ifp->flags |= IFA_F_TENTATIVE;
1680 if (dad_failed)
1681 ifp->flags |= IFA_F_DADFAILED;
1682 spin_unlock_bh(&ifp->lock);
1683 if (dad_failed)
1684 ipv6_ifa_notify(0, ifp);
1685 in6_ifa_put(ifp);
1686 } else if (ifp->flags&IFA_F_TEMPORARY) {
1687 struct inet6_ifaddr *ifpub;
1688 spin_lock_bh(&ifp->lock);
1689 ifpub = ifp->ifpub;
1690 if (ifpub) {
1691 in6_ifa_hold(ifpub);
1692 spin_unlock_bh(&ifp->lock);
1693 ipv6_create_tempaddr(ifpub, ifp);
1694 in6_ifa_put(ifpub);
1695 } else {
1696 spin_unlock_bh(&ifp->lock);
1698 ipv6_del_addr(ifp);
1699 } else {
1700 ipv6_del_addr(ifp);
1704 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1706 int err = -ENOENT;
1708 spin_lock_bh(&ifp->lock);
1709 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1710 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1711 err = 0;
1713 spin_unlock_bh(&ifp->lock);
1715 return err;
1718 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1720 struct in6_addr addr;
1721 struct inet6_dev *idev = ifp->idev;
1722 struct net *net = dev_net(ifp->idev->dev);
1724 if (addrconf_dad_end(ifp)) {
1725 in6_ifa_put(ifp);
1726 return;
1729 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1730 ifp->idev->dev->name, &ifp->addr);
1732 spin_lock_bh(&ifp->lock);
1734 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1735 int scope = ifp->scope;
1736 u32 flags = ifp->flags;
1737 struct in6_addr new_addr;
1738 struct inet6_ifaddr *ifp2;
1739 u32 valid_lft, preferred_lft;
1740 int pfxlen = ifp->prefix_len;
1741 int retries = ifp->stable_privacy_retry + 1;
1743 if (retries > net->ipv6.sysctl.idgen_retries) {
1744 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1745 ifp->idev->dev->name);
1746 goto errdad;
1749 new_addr = ifp->addr;
1750 if (ipv6_generate_stable_address(&new_addr, retries,
1751 idev))
1752 goto errdad;
1754 valid_lft = ifp->valid_lft;
1755 preferred_lft = ifp->prefered_lft;
1757 spin_unlock_bh(&ifp->lock);
1759 if (idev->cnf.max_addresses &&
1760 ipv6_count_addresses(idev) >=
1761 idev->cnf.max_addresses)
1762 goto lock_errdad;
1764 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1765 ifp->idev->dev->name);
1767 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1768 scope, flags, valid_lft,
1769 preferred_lft);
1770 if (IS_ERR(ifp2))
1771 goto lock_errdad;
1773 spin_lock_bh(&ifp2->lock);
1774 ifp2->stable_privacy_retry = retries;
1775 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1776 spin_unlock_bh(&ifp2->lock);
1778 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1779 in6_ifa_put(ifp2);
1780 lock_errdad:
1781 spin_lock_bh(&ifp->lock);
1782 } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1783 addr.s6_addr32[0] = htonl(0xfe800000);
1784 addr.s6_addr32[1] = 0;
1786 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1787 ipv6_addr_equal(&ifp->addr, &addr)) {
1788 /* DAD failed for link-local based on MAC address */
1789 idev->cnf.disable_ipv6 = 1;
1791 pr_info("%s: IPv6 being disabled!\n",
1792 ifp->idev->dev->name);
1796 errdad:
1797 /* transition from _POSTDAD to _ERRDAD */
1798 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1799 spin_unlock_bh(&ifp->lock);
1801 addrconf_mod_dad_work(ifp, 0);
1804 /* Join to solicited addr multicast group.
1805 * caller must hold RTNL */
1806 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1808 struct in6_addr maddr;
1810 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1811 return;
1813 addrconf_addr_solict_mult(addr, &maddr);
1814 ipv6_dev_mc_inc(dev, &maddr);
1817 /* caller must hold RTNL */
1818 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1820 struct in6_addr maddr;
1822 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1823 return;
1825 addrconf_addr_solict_mult(addr, &maddr);
1826 __ipv6_dev_mc_dec(idev, &maddr);
1829 /* caller must hold RTNL */
1830 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1832 struct in6_addr addr;
1834 if (ifp->prefix_len >= 127) /* RFC 6164 */
1835 return;
1836 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1837 if (ipv6_addr_any(&addr))
1838 return;
1839 __ipv6_dev_ac_inc(ifp->idev, &addr);
1842 /* caller must hold RTNL */
1843 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1845 struct in6_addr addr;
1847 if (ifp->prefix_len >= 127) /* RFC 6164 */
1848 return;
1849 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1850 if (ipv6_addr_any(&addr))
1851 return;
1852 __ipv6_dev_ac_dec(ifp->idev, &addr);
1855 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1857 if (dev->addr_len != ETH_ALEN)
1858 return -1;
1859 memcpy(eui, dev->dev_addr, 3);
1860 memcpy(eui + 5, dev->dev_addr + 3, 3);
1863 * The zSeries OSA network cards can be shared among various
1864 * OS instances, but the OSA cards have only one MAC address.
1865 * This leads to duplicate address conflicts in conjunction
1866 * with IPv6 if more than one instance uses the same card.
1868 * The driver for these cards can deliver a unique 16-bit
1869 * identifier for each instance sharing the same card. It is
1870 * placed instead of 0xFFFE in the interface identifier. The
1871 * "u" bit of the interface identifier is not inverted in this
1872 * case. Hence the resulting interface identifier has local
1873 * scope according to RFC2373.
1875 if (dev->dev_id) {
1876 eui[3] = (dev->dev_id >> 8) & 0xFF;
1877 eui[4] = dev->dev_id & 0xFF;
1878 } else {
1879 eui[3] = 0xFF;
1880 eui[4] = 0xFE;
1881 eui[0] ^= 2;
1883 return 0;
1886 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1888 if (dev->addr_len != IEEE802154_ADDR_LEN)
1889 return -1;
1890 memcpy(eui, dev->dev_addr, 8);
1891 eui[0] ^= 2;
1892 return 0;
1895 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1897 union fwnet_hwaddr *ha;
1899 if (dev->addr_len != FWNET_ALEN)
1900 return -1;
1902 ha = (union fwnet_hwaddr *)dev->dev_addr;
1904 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1905 eui[0] ^= 2;
1906 return 0;
1909 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1911 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1912 if (dev->addr_len != ARCNET_ALEN)
1913 return -1;
1914 memset(eui, 0, 7);
1915 eui[7] = *(u8 *)dev->dev_addr;
1916 return 0;
1919 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1921 if (dev->addr_len != INFINIBAND_ALEN)
1922 return -1;
1923 memcpy(eui, dev->dev_addr + 12, 8);
1924 eui[0] |= 2;
1925 return 0;
1928 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1930 if (addr == 0)
1931 return -1;
1932 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1933 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1934 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1935 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1936 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1937 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1938 eui[1] = 0;
1939 eui[2] = 0x5E;
1940 eui[3] = 0xFE;
1941 memcpy(eui + 4, &addr, 4);
1942 return 0;
1945 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1947 if (dev->priv_flags & IFF_ISATAP)
1948 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1949 return -1;
1952 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1954 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1957 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1959 memcpy(eui, dev->perm_addr, 3);
1960 memcpy(eui + 5, dev->perm_addr + 3, 3);
1961 eui[3] = 0xFF;
1962 eui[4] = 0xFE;
1963 eui[0] ^= 2;
1964 return 0;
1967 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1969 switch (dev->type) {
1970 case ARPHRD_ETHER:
1971 case ARPHRD_FDDI:
1972 return addrconf_ifid_eui48(eui, dev);
1973 case ARPHRD_ARCNET:
1974 return addrconf_ifid_arcnet(eui, dev);
1975 case ARPHRD_INFINIBAND:
1976 return addrconf_ifid_infiniband(eui, dev);
1977 case ARPHRD_SIT:
1978 return addrconf_ifid_sit(eui, dev);
1979 case ARPHRD_IPGRE:
1980 return addrconf_ifid_gre(eui, dev);
1981 case ARPHRD_6LOWPAN:
1982 case ARPHRD_IEEE802154:
1983 return addrconf_ifid_eui64(eui, dev);
1984 case ARPHRD_IEEE1394:
1985 return addrconf_ifid_ieee1394(eui, dev);
1986 case ARPHRD_TUNNEL6:
1987 return addrconf_ifid_ip6tnl(eui, dev);
1989 return -1;
1992 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1994 int err = -1;
1995 struct inet6_ifaddr *ifp;
1997 read_lock_bh(&idev->lock);
1998 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1999 if (ifp->scope > IFA_LINK)
2000 break;
2001 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2002 memcpy(eui, ifp->addr.s6_addr+8, 8);
2003 err = 0;
2004 break;
2007 read_unlock_bh(&idev->lock);
2008 return err;
2011 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2012 static void __ipv6_regen_rndid(struct inet6_dev *idev)
2014 regen:
2015 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2016 idev->rndid[0] &= ~0x02;
2019 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2020 * check if generated address is not inappropriate
2022 * - Reserved subnet anycast (RFC 2526)
2023 * 11111101 11....11 1xxxxxxx
2024 * - ISATAP (RFC4214) 6.1
2025 * 00-00-5E-FE-xx-xx-xx-xx
2026 * - value 0
2027 * - XXX: already assigned to an address on the device
2029 if (idev->rndid[0] == 0xfd &&
2030 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2031 (idev->rndid[7]&0x80))
2032 goto regen;
2033 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2034 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2035 goto regen;
2036 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2037 goto regen;
2041 static void ipv6_regen_rndid(unsigned long data)
2043 struct inet6_dev *idev = (struct inet6_dev *) data;
2044 unsigned long expires;
2046 rcu_read_lock_bh();
2047 write_lock_bh(&idev->lock);
2049 if (idev->dead)
2050 goto out;
2052 __ipv6_regen_rndid(idev);
2054 expires = jiffies +
2055 idev->cnf.temp_prefered_lft * HZ -
2056 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2057 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2058 idev->cnf.max_desync_factor * HZ;
2059 if (time_before(expires, jiffies)) {
2060 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2061 __func__, idev->dev->name);
2062 goto out;
2065 if (!mod_timer(&idev->regen_timer, expires))
2066 in6_dev_hold(idev);
2068 out:
2069 write_unlock_bh(&idev->lock);
2070 rcu_read_unlock_bh();
2071 in6_dev_put(idev);
2074 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2076 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2077 __ipv6_regen_rndid(idev);
2081 * Add prefix route.
2084 static void
2085 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2086 unsigned long expires, u32 flags)
2088 struct fib6_config cfg = {
2089 .fc_table = RT6_TABLE_PREFIX,
2090 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2091 .fc_ifindex = dev->ifindex,
2092 .fc_expires = expires,
2093 .fc_dst_len = plen,
2094 .fc_flags = RTF_UP | flags,
2095 .fc_nlinfo.nl_net = dev_net(dev),
2096 .fc_protocol = RTPROT_KERNEL,
2099 cfg.fc_dst = *pfx;
2101 /* Prevent useless cloning on PtP SIT.
2102 This thing is done here expecting that the whole
2103 class of non-broadcast devices need not cloning.
2105 #if IS_ENABLED(CONFIG_IPV6_SIT)
2106 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2107 cfg.fc_flags |= RTF_NONEXTHOP;
2108 #endif
2110 ip6_route_add(&cfg);
2114 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2115 int plen,
2116 const struct net_device *dev,
2117 u32 flags, u32 noflags)
2119 struct fib6_node *fn;
2120 struct rt6_info *rt = NULL;
2121 struct fib6_table *table;
2123 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2124 if (!table)
2125 return NULL;
2127 read_lock_bh(&table->tb6_lock);
2128 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2129 if (!fn)
2130 goto out;
2131 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2132 if (rt->dst.dev->ifindex != dev->ifindex)
2133 continue;
2134 if ((rt->rt6i_flags & flags) != flags)
2135 continue;
2136 if ((rt->rt6i_flags & noflags) != 0)
2137 continue;
2138 dst_hold(&rt->dst);
2139 break;
2141 out:
2142 read_unlock_bh(&table->tb6_lock);
2143 return rt;
2147 /* Create "default" multicast route to the interface */
2149 static void addrconf_add_mroute(struct net_device *dev)
2151 struct fib6_config cfg = {
2152 .fc_table = RT6_TABLE_LOCAL,
2153 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2154 .fc_ifindex = dev->ifindex,
2155 .fc_dst_len = 8,
2156 .fc_flags = RTF_UP,
2157 .fc_nlinfo.nl_net = dev_net(dev),
2160 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2162 ip6_route_add(&cfg);
2165 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2167 struct inet6_dev *idev;
2169 ASSERT_RTNL();
2171 idev = ipv6_find_idev(dev);
2172 if (!idev)
2173 return ERR_PTR(-ENOBUFS);
2175 if (idev->cnf.disable_ipv6)
2176 return ERR_PTR(-EACCES);
2178 /* Add default multicast route */
2179 if (!(dev->flags & IFF_LOOPBACK))
2180 addrconf_add_mroute(dev);
2182 return idev;
2185 static void manage_tempaddrs(struct inet6_dev *idev,
2186 struct inet6_ifaddr *ifp,
2187 __u32 valid_lft, __u32 prefered_lft,
2188 bool create, unsigned long now)
2190 u32 flags;
2191 struct inet6_ifaddr *ift;
2193 read_lock_bh(&idev->lock);
2194 /* update all temporary addresses in the list */
2195 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2196 int age, max_valid, max_prefered;
2198 if (ifp != ift->ifpub)
2199 continue;
2201 /* RFC 4941 section 3.3:
2202 * If a received option will extend the lifetime of a public
2203 * address, the lifetimes of temporary addresses should
2204 * be extended, subject to the overall constraint that no
2205 * temporary addresses should ever remain "valid" or "preferred"
2206 * for a time longer than (TEMP_VALID_LIFETIME) or
2207 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2209 age = (now - ift->cstamp) / HZ;
2210 max_valid = idev->cnf.temp_valid_lft - age;
2211 if (max_valid < 0)
2212 max_valid = 0;
2214 max_prefered = idev->cnf.temp_prefered_lft -
2215 idev->cnf.max_desync_factor - age;
2216 if (max_prefered < 0)
2217 max_prefered = 0;
2219 if (valid_lft > max_valid)
2220 valid_lft = max_valid;
2222 if (prefered_lft > max_prefered)
2223 prefered_lft = max_prefered;
2225 spin_lock(&ift->lock);
2226 flags = ift->flags;
2227 ift->valid_lft = valid_lft;
2228 ift->prefered_lft = prefered_lft;
2229 ift->tstamp = now;
2230 if (prefered_lft > 0)
2231 ift->flags &= ~IFA_F_DEPRECATED;
2233 spin_unlock(&ift->lock);
2234 if (!(flags&IFA_F_TENTATIVE))
2235 ipv6_ifa_notify(0, ift);
2238 if ((create || list_empty(&idev->tempaddr_list)) &&
2239 idev->cnf.use_tempaddr > 0) {
2240 /* When a new public address is created as described
2241 * in [ADDRCONF], also create a new temporary address.
2242 * Also create a temporary address if it's enabled but
2243 * no temporary address currently exists.
2245 read_unlock_bh(&idev->lock);
2246 ipv6_create_tempaddr(ifp, NULL);
2247 } else {
2248 read_unlock_bh(&idev->lock);
2252 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2254 struct prefix_info *pinfo;
2255 __u32 valid_lft;
2256 __u32 prefered_lft;
2257 int addr_type;
2258 u32 addr_flags = 0;
2259 struct inet6_dev *in6_dev;
2260 struct net *net = dev_net(dev);
2262 pinfo = (struct prefix_info *) opt;
2264 if (len < sizeof(struct prefix_info)) {
2265 ADBG("addrconf: prefix option too short\n");
2266 return;
2270 * Validation checks ([ADDRCONF], page 19)
2273 addr_type = ipv6_addr_type(&pinfo->prefix);
2275 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2276 return;
2278 valid_lft = ntohl(pinfo->valid);
2279 prefered_lft = ntohl(pinfo->prefered);
2281 if (prefered_lft > valid_lft) {
2282 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2283 return;
2286 in6_dev = in6_dev_get(dev);
2288 if (!in6_dev) {
2289 net_dbg_ratelimited("addrconf: device %s not configured\n",
2290 dev->name);
2291 return;
2295 * Two things going on here:
2296 * 1) Add routes for on-link prefixes
2297 * 2) Configure prefixes with the auto flag set
2300 if (pinfo->onlink) {
2301 struct rt6_info *rt;
2302 unsigned long rt_expires;
2304 /* Avoid arithmetic overflow. Really, we could
2305 * save rt_expires in seconds, likely valid_lft,
2306 * but it would require division in fib gc, that it
2307 * not good.
2309 if (HZ > USER_HZ)
2310 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2311 else
2312 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2314 if (addrconf_finite_timeout(rt_expires))
2315 rt_expires *= HZ;
2317 rt = addrconf_get_prefix_route(&pinfo->prefix,
2318 pinfo->prefix_len,
2319 dev,
2320 RTF_ADDRCONF | RTF_PREFIX_RT,
2321 RTF_GATEWAY | RTF_DEFAULT);
2323 if (rt) {
2324 /* Autoconf prefix route */
2325 if (valid_lft == 0) {
2326 ip6_del_rt(rt);
2327 rt = NULL;
2328 } else if (addrconf_finite_timeout(rt_expires)) {
2329 /* not infinity */
2330 rt6_set_expires(rt, jiffies + rt_expires);
2331 } else {
2332 rt6_clean_expires(rt);
2334 } else if (valid_lft) {
2335 clock_t expires = 0;
2336 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2337 if (addrconf_finite_timeout(rt_expires)) {
2338 /* not infinity */
2339 flags |= RTF_EXPIRES;
2340 expires = jiffies_to_clock_t(rt_expires);
2342 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2343 dev, expires, flags);
2345 ip6_rt_put(rt);
2348 /* Try to figure out our local address for this prefix */
2350 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2351 struct inet6_ifaddr *ifp;
2352 struct in6_addr addr;
2353 int create = 0, update_lft = 0;
2354 bool tokenized = false;
2356 if (pinfo->prefix_len == 64) {
2357 memcpy(&addr, &pinfo->prefix, 8);
2359 if (!ipv6_addr_any(&in6_dev->token)) {
2360 read_lock_bh(&in6_dev->lock);
2361 memcpy(addr.s6_addr + 8,
2362 in6_dev->token.s6_addr + 8, 8);
2363 read_unlock_bh(&in6_dev->lock);
2364 tokenized = true;
2365 } else if (in6_dev->addr_gen_mode ==
2366 IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
2367 !ipv6_generate_stable_address(&addr, 0,
2368 in6_dev)) {
2369 addr_flags |= IFA_F_STABLE_PRIVACY;
2370 goto ok;
2371 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2372 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2373 in6_dev_put(in6_dev);
2374 return;
2376 goto ok;
2378 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2379 pinfo->prefix_len);
2380 in6_dev_put(in6_dev);
2381 return;
2385 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2387 if (!ifp && valid_lft) {
2388 int max_addresses = in6_dev->cnf.max_addresses;
2390 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2391 if (in6_dev->cnf.optimistic_dad &&
2392 !net->ipv6.devconf_all->forwarding && sllao)
2393 addr_flags |= IFA_F_OPTIMISTIC;
2394 #endif
2396 /* Do not allow to create too much of autoconfigured
2397 * addresses; this would be too easy way to crash kernel.
2399 if (!max_addresses ||
2400 ipv6_count_addresses(in6_dev) < max_addresses)
2401 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2402 pinfo->prefix_len,
2403 addr_type&IPV6_ADDR_SCOPE_MASK,
2404 addr_flags, valid_lft,
2405 prefered_lft);
2407 if (IS_ERR_OR_NULL(ifp)) {
2408 in6_dev_put(in6_dev);
2409 return;
2412 update_lft = 0;
2413 create = 1;
2414 spin_lock_bh(&ifp->lock);
2415 ifp->flags |= IFA_F_MANAGETEMPADDR;
2416 ifp->cstamp = jiffies;
2417 ifp->tokenized = tokenized;
2418 spin_unlock_bh(&ifp->lock);
2419 addrconf_dad_start(ifp);
2422 if (ifp) {
2423 u32 flags;
2424 unsigned long now;
2425 u32 stored_lft;
2427 /* update lifetime (RFC2462 5.5.3 e) */
2428 spin_lock_bh(&ifp->lock);
2429 now = jiffies;
2430 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2431 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2432 else
2433 stored_lft = 0;
2434 if (!update_lft && !create && stored_lft) {
2435 const u32 minimum_lft = min_t(u32,
2436 stored_lft, MIN_VALID_LIFETIME);
2437 valid_lft = max(valid_lft, minimum_lft);
2439 /* RFC4862 Section 5.5.3e:
2440 * "Note that the preferred lifetime of the
2441 * corresponding address is always reset to
2442 * the Preferred Lifetime in the received
2443 * Prefix Information option, regardless of
2444 * whether the valid lifetime is also reset or
2445 * ignored."
2447 * So we should always update prefered_lft here.
2449 update_lft = 1;
2452 if (update_lft) {
2453 ifp->valid_lft = valid_lft;
2454 ifp->prefered_lft = prefered_lft;
2455 ifp->tstamp = now;
2456 flags = ifp->flags;
2457 ifp->flags &= ~IFA_F_DEPRECATED;
2458 spin_unlock_bh(&ifp->lock);
2460 if (!(flags&IFA_F_TENTATIVE))
2461 ipv6_ifa_notify(0, ifp);
2462 } else
2463 spin_unlock_bh(&ifp->lock);
2465 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2466 create, now);
2468 in6_ifa_put(ifp);
2469 addrconf_verify();
2472 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2473 in6_dev_put(in6_dev);
2477 * Set destination address.
2478 * Special case for SIT interfaces where we create a new "virtual"
2479 * device.
2481 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2483 struct in6_ifreq ireq;
2484 struct net_device *dev;
2485 int err = -EINVAL;
2487 rtnl_lock();
2489 err = -EFAULT;
2490 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2491 goto err_exit;
2493 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2495 err = -ENODEV;
2496 if (!dev)
2497 goto err_exit;
2499 #if IS_ENABLED(CONFIG_IPV6_SIT)
2500 if (dev->type == ARPHRD_SIT) {
2501 const struct net_device_ops *ops = dev->netdev_ops;
2502 struct ifreq ifr;
2503 struct ip_tunnel_parm p;
2505 err = -EADDRNOTAVAIL;
2506 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2507 goto err_exit;
2509 memset(&p, 0, sizeof(p));
2510 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2511 p.iph.saddr = 0;
2512 p.iph.version = 4;
2513 p.iph.ihl = 5;
2514 p.iph.protocol = IPPROTO_IPV6;
2515 p.iph.ttl = 64;
2516 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2518 if (ops->ndo_do_ioctl) {
2519 mm_segment_t oldfs = get_fs();
2521 set_fs(KERNEL_DS);
2522 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2523 set_fs(oldfs);
2524 } else
2525 err = -EOPNOTSUPP;
2527 if (err == 0) {
2528 err = -ENOBUFS;
2529 dev = __dev_get_by_name(net, p.name);
2530 if (!dev)
2531 goto err_exit;
2532 err = dev_open(dev);
2535 #endif
2537 err_exit:
2538 rtnl_unlock();
2539 return err;
2542 static int ipv6_mc_config(struct sock *sk, bool join,
2543 const struct in6_addr *addr, int ifindex)
2545 int ret;
2547 ASSERT_RTNL();
2549 lock_sock(sk);
2550 if (join)
2551 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2552 else
2553 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2554 release_sock(sk);
2556 return ret;
2560 * Manual configuration of address on an interface
2562 static int inet6_addr_add(struct net *net, int ifindex,
2563 const struct in6_addr *pfx,
2564 const struct in6_addr *peer_pfx,
2565 unsigned int plen, __u32 ifa_flags,
2566 __u32 prefered_lft, __u32 valid_lft)
2568 struct inet6_ifaddr *ifp;
2569 struct inet6_dev *idev;
2570 struct net_device *dev;
2571 unsigned long timeout;
2572 clock_t expires;
2573 int scope;
2574 u32 flags;
2576 ASSERT_RTNL();
2578 if (plen > 128)
2579 return -EINVAL;
2581 /* check the lifetime */
2582 if (!valid_lft || prefered_lft > valid_lft)
2583 return -EINVAL;
2585 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2586 return -EINVAL;
2588 dev = __dev_get_by_index(net, ifindex);
2589 if (!dev)
2590 return -ENODEV;
2592 idev = addrconf_add_dev(dev);
2593 if (IS_ERR(idev))
2594 return PTR_ERR(idev);
2596 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2597 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2598 true, pfx, ifindex);
2600 if (ret < 0)
2601 return ret;
2604 scope = ipv6_addr_scope(pfx);
2606 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2607 if (addrconf_finite_timeout(timeout)) {
2608 expires = jiffies_to_clock_t(timeout * HZ);
2609 valid_lft = timeout;
2610 flags = RTF_EXPIRES;
2611 } else {
2612 expires = 0;
2613 flags = 0;
2614 ifa_flags |= IFA_F_PERMANENT;
2617 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2618 if (addrconf_finite_timeout(timeout)) {
2619 if (timeout == 0)
2620 ifa_flags |= IFA_F_DEPRECATED;
2621 prefered_lft = timeout;
2624 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2625 valid_lft, prefered_lft);
2627 if (!IS_ERR(ifp)) {
2628 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2629 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2630 expires, flags);
2634 * Note that section 3.1 of RFC 4429 indicates
2635 * that the Optimistic flag should not be set for
2636 * manually configured addresses
2638 addrconf_dad_start(ifp);
2639 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2640 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2641 true, jiffies);
2642 in6_ifa_put(ifp);
2643 addrconf_verify_rtnl();
2644 return 0;
2645 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2646 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2647 false, pfx, ifindex);
2650 return PTR_ERR(ifp);
2653 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2654 const struct in6_addr *pfx, unsigned int plen)
2656 struct inet6_ifaddr *ifp;
2657 struct inet6_dev *idev;
2658 struct net_device *dev;
2660 if (plen > 128)
2661 return -EINVAL;
2663 dev = __dev_get_by_index(net, ifindex);
2664 if (!dev)
2665 return -ENODEV;
2667 idev = __in6_dev_get(dev);
2668 if (!idev)
2669 return -ENXIO;
2671 read_lock_bh(&idev->lock);
2672 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2673 if (ifp->prefix_len == plen &&
2674 ipv6_addr_equal(pfx, &ifp->addr)) {
2675 in6_ifa_hold(ifp);
2676 read_unlock_bh(&idev->lock);
2678 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2679 (ifa_flags & IFA_F_MANAGETEMPADDR))
2680 manage_tempaddrs(idev, ifp, 0, 0, false,
2681 jiffies);
2682 ipv6_del_addr(ifp);
2683 addrconf_verify_rtnl();
2684 if (ipv6_addr_is_multicast(pfx)) {
2685 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2686 false, pfx, dev->ifindex);
2688 return 0;
2691 read_unlock_bh(&idev->lock);
2692 return -EADDRNOTAVAIL;
2696 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2698 struct in6_ifreq ireq;
2699 int err;
2701 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2702 return -EPERM;
2704 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2705 return -EFAULT;
2707 rtnl_lock();
2708 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2709 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2710 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2711 rtnl_unlock();
2712 return err;
2715 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2717 struct in6_ifreq ireq;
2718 int err;
2720 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2721 return -EPERM;
2723 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2724 return -EFAULT;
2726 rtnl_lock();
2727 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2728 ireq.ifr6_prefixlen);
2729 rtnl_unlock();
2730 return err;
2733 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2734 int plen, int scope)
2736 struct inet6_ifaddr *ifp;
2738 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2739 scope, IFA_F_PERMANENT,
2740 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2741 if (!IS_ERR(ifp)) {
2742 spin_lock_bh(&ifp->lock);
2743 ifp->flags &= ~IFA_F_TENTATIVE;
2744 spin_unlock_bh(&ifp->lock);
2745 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2746 in6_ifa_put(ifp);
2750 #if IS_ENABLED(CONFIG_IPV6_SIT)
2751 static void sit_add_v4_addrs(struct inet6_dev *idev)
2753 struct in6_addr addr;
2754 struct net_device *dev;
2755 struct net *net = dev_net(idev->dev);
2756 int scope, plen;
2757 u32 pflags = 0;
2759 ASSERT_RTNL();
2761 memset(&addr, 0, sizeof(struct in6_addr));
2762 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2764 if (idev->dev->flags&IFF_POINTOPOINT) {
2765 addr.s6_addr32[0] = htonl(0xfe800000);
2766 scope = IFA_LINK;
2767 plen = 64;
2768 } else {
2769 scope = IPV6_ADDR_COMPATv4;
2770 plen = 96;
2771 pflags |= RTF_NONEXTHOP;
2774 if (addr.s6_addr32[3]) {
2775 add_addr(idev, &addr, plen, scope);
2776 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2777 return;
2780 for_each_netdev(net, dev) {
2781 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2782 if (in_dev && (dev->flags & IFF_UP)) {
2783 struct in_ifaddr *ifa;
2785 int flag = scope;
2787 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2789 addr.s6_addr32[3] = ifa->ifa_local;
2791 if (ifa->ifa_scope == RT_SCOPE_LINK)
2792 continue;
2793 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2794 if (idev->dev->flags&IFF_POINTOPOINT)
2795 continue;
2796 flag |= IFA_HOST;
2799 add_addr(idev, &addr, plen, flag);
2800 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2801 pflags);
2806 #endif
2808 static void init_loopback(struct net_device *dev)
2810 struct inet6_dev *idev;
2811 struct net_device *sp_dev;
2812 struct inet6_ifaddr *sp_ifa;
2813 struct rt6_info *sp_rt;
2815 /* ::1 */
2817 ASSERT_RTNL();
2819 idev = ipv6_find_idev(dev);
2820 if (!idev) {
2821 pr_debug("%s: add_dev failed\n", __func__);
2822 return;
2825 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2827 /* Add routes to other interface's IPv6 addresses */
2828 for_each_netdev(dev_net(dev), sp_dev) {
2829 if (!strcmp(sp_dev->name, dev->name))
2830 continue;
2832 idev = __in6_dev_get(sp_dev);
2833 if (!idev)
2834 continue;
2836 read_lock_bh(&idev->lock);
2837 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2839 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2840 continue;
2842 if (sp_ifa->rt) {
2843 /* This dst has been added to garbage list when
2844 * lo device down, release this obsolete dst and
2845 * reallocate a new router for ifa.
2847 if (sp_ifa->rt->dst.obsolete > 0) {
2848 ip6_rt_put(sp_ifa->rt);
2849 sp_ifa->rt = NULL;
2850 } else {
2851 continue;
2855 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
2857 /* Failure cases are ignored */
2858 if (!IS_ERR(sp_rt)) {
2859 sp_ifa->rt = sp_rt;
2860 ip6_ins_rt(sp_rt);
2863 read_unlock_bh(&idev->lock);
2867 static void addrconf_add_linklocal(struct inet6_dev *idev,
2868 const struct in6_addr *addr, u32 flags)
2870 struct inet6_ifaddr *ifp;
2871 u32 addr_flags = flags | IFA_F_PERMANENT;
2873 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2874 if (idev->cnf.optimistic_dad &&
2875 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2876 addr_flags |= IFA_F_OPTIMISTIC;
2877 #endif
2879 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2880 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2881 if (!IS_ERR(ifp)) {
2882 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2883 addrconf_dad_start(ifp);
2884 in6_ifa_put(ifp);
2888 static bool ipv6_reserved_interfaceid(struct in6_addr address)
2890 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2891 return true;
2893 if (address.s6_addr32[2] == htonl(0x02005eff) &&
2894 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2895 return true;
2897 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2898 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2899 return true;
2901 return false;
2904 static int ipv6_generate_stable_address(struct in6_addr *address,
2905 u8 dad_count,
2906 const struct inet6_dev *idev)
2908 static DEFINE_SPINLOCK(lock);
2909 static __u32 digest[SHA_DIGEST_WORDS];
2910 static __u32 workspace[SHA_WORKSPACE_WORDS];
2912 static union {
2913 char __data[SHA_MESSAGE_BYTES];
2914 struct {
2915 struct in6_addr secret;
2916 __be32 prefix[2];
2917 unsigned char hwaddr[MAX_ADDR_LEN];
2918 u8 dad_count;
2919 } __packed;
2920 } data;
2922 struct in6_addr secret;
2923 struct in6_addr temp;
2924 struct net *net = dev_net(idev->dev);
2926 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
2928 if (idev->cnf.stable_secret.initialized)
2929 secret = idev->cnf.stable_secret.secret;
2930 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
2931 secret = net->ipv6.devconf_dflt->stable_secret.secret;
2932 else
2933 return -1;
2935 retry:
2936 spin_lock_bh(&lock);
2938 sha_init(digest);
2939 memset(&data, 0, sizeof(data));
2940 memset(workspace, 0, sizeof(workspace));
2941 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
2942 data.prefix[0] = address->s6_addr32[0];
2943 data.prefix[1] = address->s6_addr32[1];
2944 data.secret = secret;
2945 data.dad_count = dad_count;
2947 sha_transform(digest, data.__data, workspace);
2949 temp = *address;
2950 temp.s6_addr32[2] = (__force __be32)digest[0];
2951 temp.s6_addr32[3] = (__force __be32)digest[1];
2953 spin_unlock_bh(&lock);
2955 if (ipv6_reserved_interfaceid(temp)) {
2956 dad_count++;
2957 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
2958 return -1;
2959 goto retry;
2962 *address = temp;
2963 return 0;
2966 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2968 struct in6_addr addr;
2970 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2972 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) {
2973 if (!ipv6_generate_stable_address(&addr, 0, idev))
2974 addrconf_add_linklocal(idev, &addr,
2975 IFA_F_STABLE_PRIVACY);
2976 else if (prefix_route)
2977 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2978 } else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
2979 /* addrconf_add_linklocal also adds a prefix_route and we
2980 * only need to care about prefix routes if ipv6_generate_eui64
2981 * couldn't generate one.
2983 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
2984 addrconf_add_linklocal(idev, &addr, 0);
2985 else if (prefix_route)
2986 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2990 static void addrconf_dev_config(struct net_device *dev)
2992 struct inet6_dev *idev;
2994 ASSERT_RTNL();
2996 if ((dev->type != ARPHRD_ETHER) &&
2997 (dev->type != ARPHRD_FDDI) &&
2998 (dev->type != ARPHRD_ARCNET) &&
2999 (dev->type != ARPHRD_INFINIBAND) &&
3000 (dev->type != ARPHRD_IEEE802154) &&
3001 (dev->type != ARPHRD_IEEE1394) &&
3002 (dev->type != ARPHRD_TUNNEL6) &&
3003 (dev->type != ARPHRD_6LOWPAN)) {
3004 /* Alas, we support only Ethernet autoconfiguration. */
3005 return;
3008 idev = addrconf_add_dev(dev);
3009 if (IS_ERR(idev))
3010 return;
3012 addrconf_addr_gen(idev, false);
3015 #if IS_ENABLED(CONFIG_IPV6_SIT)
3016 static void addrconf_sit_config(struct net_device *dev)
3018 struct inet6_dev *idev;
3020 ASSERT_RTNL();
3023 * Configure the tunnel with one of our IPv4
3024 * addresses... we should configure all of
3025 * our v4 addrs in the tunnel
3028 idev = ipv6_find_idev(dev);
3029 if (!idev) {
3030 pr_debug("%s: add_dev failed\n", __func__);
3031 return;
3034 if (dev->priv_flags & IFF_ISATAP) {
3035 addrconf_addr_gen(idev, false);
3036 return;
3039 sit_add_v4_addrs(idev);
3041 if (dev->flags&IFF_POINTOPOINT)
3042 addrconf_add_mroute(dev);
3044 #endif
3046 #if IS_ENABLED(CONFIG_NET_IPGRE)
3047 static void addrconf_gre_config(struct net_device *dev)
3049 struct inet6_dev *idev;
3051 ASSERT_RTNL();
3053 idev = ipv6_find_idev(dev);
3054 if (!idev) {
3055 pr_debug("%s: add_dev failed\n", __func__);
3056 return;
3059 addrconf_addr_gen(idev, true);
3061 #endif
3063 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3064 void *ptr)
3066 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3067 struct inet6_dev *idev = __in6_dev_get(dev);
3068 int run_pending = 0;
3069 int err;
3071 switch (event) {
3072 case NETDEV_REGISTER:
3073 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3074 idev = ipv6_add_dev(dev);
3075 if (IS_ERR(idev))
3076 return notifier_from_errno(PTR_ERR(idev));
3078 break;
3080 case NETDEV_UP:
3081 case NETDEV_CHANGE:
3082 if (dev->flags & IFF_SLAVE)
3083 break;
3085 if (idev && idev->cnf.disable_ipv6)
3086 break;
3088 if (event == NETDEV_UP) {
3089 if (!addrconf_qdisc_ok(dev)) {
3090 /* device is not ready yet. */
3091 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3092 dev->name);
3093 break;
3096 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3097 idev = ipv6_add_dev(dev);
3099 if (!IS_ERR_OR_NULL(idev)) {
3100 idev->if_flags |= IF_READY;
3101 run_pending = 1;
3103 } else {
3104 if (!addrconf_qdisc_ok(dev)) {
3105 /* device is still not ready. */
3106 break;
3109 if (idev) {
3110 if (idev->if_flags & IF_READY)
3111 /* device is already configured. */
3112 break;
3113 idev->if_flags |= IF_READY;
3116 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3117 dev->name);
3119 run_pending = 1;
3122 switch (dev->type) {
3123 #if IS_ENABLED(CONFIG_IPV6_SIT)
3124 case ARPHRD_SIT:
3125 addrconf_sit_config(dev);
3126 break;
3127 #endif
3128 #if IS_ENABLED(CONFIG_NET_IPGRE)
3129 case ARPHRD_IPGRE:
3130 addrconf_gre_config(dev);
3131 break;
3132 #endif
3133 case ARPHRD_LOOPBACK:
3134 init_loopback(dev);
3135 break;
3137 default:
3138 addrconf_dev_config(dev);
3139 break;
3142 if (!IS_ERR_OR_NULL(idev)) {
3143 if (run_pending)
3144 addrconf_dad_run(idev);
3147 * If the MTU changed during the interface down,
3148 * when the interface up, the changed MTU must be
3149 * reflected in the idev as well as routers.
3151 if (idev->cnf.mtu6 != dev->mtu &&
3152 dev->mtu >= IPV6_MIN_MTU) {
3153 rt6_mtu_change(dev, dev->mtu);
3154 idev->cnf.mtu6 = dev->mtu;
3156 idev->tstamp = jiffies;
3157 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3160 * If the changed mtu during down is lower than
3161 * IPV6_MIN_MTU stop IPv6 on this interface.
3163 if (dev->mtu < IPV6_MIN_MTU)
3164 addrconf_ifdown(dev, 1);
3166 break;
3168 case NETDEV_CHANGEMTU:
3169 if (idev && dev->mtu >= IPV6_MIN_MTU) {
3170 rt6_mtu_change(dev, dev->mtu);
3171 idev->cnf.mtu6 = dev->mtu;
3172 break;
3175 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3176 idev = ipv6_add_dev(dev);
3177 if (!IS_ERR(idev))
3178 break;
3182 * if MTU under IPV6_MIN_MTU.
3183 * Stop IPv6 on this interface.
3186 case NETDEV_DOWN:
3187 case NETDEV_UNREGISTER:
3189 * Remove all addresses from this interface.
3191 addrconf_ifdown(dev, event != NETDEV_DOWN);
3192 break;
3194 case NETDEV_CHANGENAME:
3195 if (idev) {
3196 snmp6_unregister_dev(idev);
3197 addrconf_sysctl_unregister(idev);
3198 err = addrconf_sysctl_register(idev);
3199 if (err)
3200 return notifier_from_errno(err);
3201 err = snmp6_register_dev(idev);
3202 if (err) {
3203 addrconf_sysctl_unregister(idev);
3204 return notifier_from_errno(err);
3207 break;
3209 case NETDEV_PRE_TYPE_CHANGE:
3210 case NETDEV_POST_TYPE_CHANGE:
3211 addrconf_type_change(dev, event);
3212 break;
3215 return NOTIFY_OK;
3219 * addrconf module should be notified of a device going up
3221 static struct notifier_block ipv6_dev_notf = {
3222 .notifier_call = addrconf_notify,
3225 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3227 struct inet6_dev *idev;
3228 ASSERT_RTNL();
3230 idev = __in6_dev_get(dev);
3232 if (event == NETDEV_POST_TYPE_CHANGE)
3233 ipv6_mc_remap(idev);
3234 else if (event == NETDEV_PRE_TYPE_CHANGE)
3235 ipv6_mc_unmap(idev);
3238 static int addrconf_ifdown(struct net_device *dev, int how)
3240 struct net *net = dev_net(dev);
3241 struct inet6_dev *idev;
3242 struct inet6_ifaddr *ifa;
3243 int state, i;
3245 ASSERT_RTNL();
3247 rt6_ifdown(net, dev);
3248 neigh_ifdown(&nd_tbl, dev);
3250 idev = __in6_dev_get(dev);
3251 if (!idev)
3252 return -ENODEV;
3255 * Step 1: remove reference to ipv6 device from parent device.
3256 * Do not dev_put!
3258 if (how) {
3259 idev->dead = 1;
3261 /* protected by rtnl_lock */
3262 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3264 /* Step 1.5: remove snmp6 entry */
3265 snmp6_unregister_dev(idev);
3269 /* Step 2: clear hash table */
3270 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3271 struct hlist_head *h = &inet6_addr_lst[i];
3273 spin_lock_bh(&addrconf_hash_lock);
3274 restart:
3275 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3276 if (ifa->idev == idev) {
3277 hlist_del_init_rcu(&ifa->addr_lst);
3278 addrconf_del_dad_work(ifa);
3279 goto restart;
3282 spin_unlock_bh(&addrconf_hash_lock);
3285 write_lock_bh(&idev->lock);
3287 addrconf_del_rs_timer(idev);
3289 /* Step 2: clear flags for stateless addrconf */
3290 if (!how)
3291 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3293 if (how && del_timer(&idev->regen_timer))
3294 in6_dev_put(idev);
3296 /* Step 3: clear tempaddr list */
3297 while (!list_empty(&idev->tempaddr_list)) {
3298 ifa = list_first_entry(&idev->tempaddr_list,
3299 struct inet6_ifaddr, tmp_list);
3300 list_del(&ifa->tmp_list);
3301 write_unlock_bh(&idev->lock);
3302 spin_lock_bh(&ifa->lock);
3304 if (ifa->ifpub) {
3305 in6_ifa_put(ifa->ifpub);
3306 ifa->ifpub = NULL;
3308 spin_unlock_bh(&ifa->lock);
3309 in6_ifa_put(ifa);
3310 write_lock_bh(&idev->lock);
3313 while (!list_empty(&idev->addr_list)) {
3314 ifa = list_first_entry(&idev->addr_list,
3315 struct inet6_ifaddr, if_list);
3316 addrconf_del_dad_work(ifa);
3318 list_del(&ifa->if_list);
3320 write_unlock_bh(&idev->lock);
3322 spin_lock_bh(&ifa->lock);
3323 state = ifa->state;
3324 ifa->state = INET6_IFADDR_STATE_DEAD;
3325 spin_unlock_bh(&ifa->lock);
3327 if (state != INET6_IFADDR_STATE_DEAD) {
3328 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3329 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3331 in6_ifa_put(ifa);
3333 write_lock_bh(&idev->lock);
3336 write_unlock_bh(&idev->lock);
3338 /* Step 5: Discard anycast and multicast list */
3339 if (how) {
3340 ipv6_ac_destroy_dev(idev);
3341 ipv6_mc_destroy_dev(idev);
3342 } else {
3343 ipv6_mc_down(idev);
3346 idev->tstamp = jiffies;
3348 /* Last: Shot the device (if unregistered) */
3349 if (how) {
3350 addrconf_sysctl_unregister(idev);
3351 neigh_parms_release(&nd_tbl, idev->nd_parms);
3352 neigh_ifdown(&nd_tbl, dev);
3353 in6_dev_put(idev);
3355 return 0;
3358 static void addrconf_rs_timer(unsigned long data)
3360 struct inet6_dev *idev = (struct inet6_dev *)data;
3361 struct net_device *dev = idev->dev;
3362 struct in6_addr lladdr;
3364 write_lock(&idev->lock);
3365 if (idev->dead || !(idev->if_flags & IF_READY))
3366 goto out;
3368 if (!ipv6_accept_ra(idev))
3369 goto out;
3371 /* Announcement received after solicitation was sent */
3372 if (idev->if_flags & IF_RA_RCVD)
3373 goto out;
3375 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3376 write_unlock(&idev->lock);
3377 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3378 ndisc_send_rs(dev, &lladdr,
3379 &in6addr_linklocal_allrouters);
3380 else
3381 goto put;
3383 write_lock(&idev->lock);
3384 /* The wait after the last probe can be shorter */
3385 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3386 idev->cnf.rtr_solicits) ?
3387 idev->cnf.rtr_solicit_delay :
3388 idev->cnf.rtr_solicit_interval);
3389 } else {
3391 * Note: we do not support deprecated "all on-link"
3392 * assumption any longer.
3394 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3397 out:
3398 write_unlock(&idev->lock);
3399 put:
3400 in6_dev_put(idev);
3404 * Duplicate Address Detection
3406 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3408 unsigned long rand_num;
3409 struct inet6_dev *idev = ifp->idev;
3411 if (ifp->flags & IFA_F_OPTIMISTIC)
3412 rand_num = 0;
3413 else
3414 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3416 ifp->dad_probes = idev->cnf.dad_transmits;
3417 addrconf_mod_dad_work(ifp, rand_num);
3420 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3422 struct inet6_dev *idev = ifp->idev;
3423 struct net_device *dev = idev->dev;
3425 addrconf_join_solict(dev, &ifp->addr);
3427 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3429 read_lock_bh(&idev->lock);
3430 spin_lock(&ifp->lock);
3431 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3432 goto out;
3434 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3435 idev->cnf.accept_dad < 1 ||
3436 !(ifp->flags&IFA_F_TENTATIVE) ||
3437 ifp->flags & IFA_F_NODAD) {
3438 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3439 spin_unlock(&ifp->lock);
3440 read_unlock_bh(&idev->lock);
3442 addrconf_dad_completed(ifp);
3443 return;
3446 if (!(idev->if_flags & IF_READY)) {
3447 spin_unlock(&ifp->lock);
3448 read_unlock_bh(&idev->lock);
3450 * If the device is not ready:
3451 * - keep it tentative if it is a permanent address.
3452 * - otherwise, kill it.
3454 in6_ifa_hold(ifp);
3455 addrconf_dad_stop(ifp, 0);
3456 return;
3460 * Optimistic nodes can start receiving
3461 * Frames right away
3463 if (ifp->flags & IFA_F_OPTIMISTIC) {
3464 ip6_ins_rt(ifp->rt);
3465 if (ipv6_use_optimistic_addr(idev)) {
3466 /* Because optimistic nodes can use this address,
3467 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3469 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3473 addrconf_dad_kick(ifp);
3474 out:
3475 spin_unlock(&ifp->lock);
3476 read_unlock_bh(&idev->lock);
3479 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3481 bool begin_dad = false;
3483 spin_lock_bh(&ifp->lock);
3484 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3485 ifp->state = INET6_IFADDR_STATE_PREDAD;
3486 begin_dad = true;
3488 spin_unlock_bh(&ifp->lock);
3490 if (begin_dad)
3491 addrconf_mod_dad_work(ifp, 0);
3494 static void addrconf_dad_work(struct work_struct *w)
3496 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3497 struct inet6_ifaddr,
3498 dad_work);
3499 struct inet6_dev *idev = ifp->idev;
3500 struct in6_addr mcaddr;
3502 enum {
3503 DAD_PROCESS,
3504 DAD_BEGIN,
3505 DAD_ABORT,
3506 } action = DAD_PROCESS;
3508 rtnl_lock();
3510 spin_lock_bh(&ifp->lock);
3511 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3512 action = DAD_BEGIN;
3513 ifp->state = INET6_IFADDR_STATE_DAD;
3514 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3515 action = DAD_ABORT;
3516 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3518 spin_unlock_bh(&ifp->lock);
3520 if (action == DAD_BEGIN) {
3521 addrconf_dad_begin(ifp);
3522 goto out;
3523 } else if (action == DAD_ABORT) {
3524 addrconf_dad_stop(ifp, 1);
3525 goto out;
3528 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3529 goto out;
3531 write_lock_bh(&idev->lock);
3532 if (idev->dead || !(idev->if_flags & IF_READY)) {
3533 write_unlock_bh(&idev->lock);
3534 goto out;
3537 spin_lock(&ifp->lock);
3538 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3539 spin_unlock(&ifp->lock);
3540 write_unlock_bh(&idev->lock);
3541 goto out;
3544 if (ifp->dad_probes == 0) {
3546 * DAD was successful
3549 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3550 spin_unlock(&ifp->lock);
3551 write_unlock_bh(&idev->lock);
3553 addrconf_dad_completed(ifp);
3555 goto out;
3558 ifp->dad_probes--;
3559 addrconf_mod_dad_work(ifp,
3560 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3561 spin_unlock(&ifp->lock);
3562 write_unlock_bh(&idev->lock);
3564 /* send a neighbour solicitation for our addr */
3565 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3566 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3567 out:
3568 in6_ifa_put(ifp);
3569 rtnl_unlock();
3572 /* ifp->idev must be at least read locked */
3573 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3575 struct inet6_ifaddr *ifpiter;
3576 struct inet6_dev *idev = ifp->idev;
3578 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3579 if (ifpiter->scope > IFA_LINK)
3580 break;
3581 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3582 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3583 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3584 IFA_F_PERMANENT)
3585 return false;
3587 return true;
3590 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3592 struct net_device *dev = ifp->idev->dev;
3593 struct in6_addr lladdr;
3594 bool send_rs, send_mld;
3596 addrconf_del_dad_work(ifp);
3599 * Configure the address for reception. Now it is valid.
3602 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3604 /* If added prefix is link local and we are prepared to process
3605 router advertisements, start sending router solicitations.
3608 read_lock_bh(&ifp->idev->lock);
3609 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3610 send_rs = send_mld &&
3611 ipv6_accept_ra(ifp->idev) &&
3612 ifp->idev->cnf.rtr_solicits > 0 &&
3613 (dev->flags&IFF_LOOPBACK) == 0;
3614 read_unlock_bh(&ifp->idev->lock);
3616 /* While dad is in progress mld report's source address is in6_addrany.
3617 * Resend with proper ll now.
3619 if (send_mld)
3620 ipv6_mc_dad_complete(ifp->idev);
3622 if (send_rs) {
3624 * If a host as already performed a random delay
3625 * [...] as part of DAD [...] there is no need
3626 * to delay again before sending the first RS
3628 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3629 return;
3630 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3632 write_lock_bh(&ifp->idev->lock);
3633 spin_lock(&ifp->lock);
3634 ifp->idev->rs_probes = 1;
3635 ifp->idev->if_flags |= IF_RS_SENT;
3636 addrconf_mod_rs_timer(ifp->idev,
3637 ifp->idev->cnf.rtr_solicit_interval);
3638 spin_unlock(&ifp->lock);
3639 write_unlock_bh(&ifp->idev->lock);
3643 static void addrconf_dad_run(struct inet6_dev *idev)
3645 struct inet6_ifaddr *ifp;
3647 read_lock_bh(&idev->lock);
3648 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3649 spin_lock(&ifp->lock);
3650 if (ifp->flags & IFA_F_TENTATIVE &&
3651 ifp->state == INET6_IFADDR_STATE_DAD)
3652 addrconf_dad_kick(ifp);
3653 spin_unlock(&ifp->lock);
3655 read_unlock_bh(&idev->lock);
3658 #ifdef CONFIG_PROC_FS
3659 struct if6_iter_state {
3660 struct seq_net_private p;
3661 int bucket;
3662 int offset;
3665 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3667 struct inet6_ifaddr *ifa = NULL;
3668 struct if6_iter_state *state = seq->private;
3669 struct net *net = seq_file_net(seq);
3670 int p = 0;
3672 /* initial bucket if pos is 0 */
3673 if (pos == 0) {
3674 state->bucket = 0;
3675 state->offset = 0;
3678 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3679 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3680 addr_lst) {
3681 if (!net_eq(dev_net(ifa->idev->dev), net))
3682 continue;
3683 /* sync with offset */
3684 if (p < state->offset) {
3685 p++;
3686 continue;
3688 state->offset++;
3689 return ifa;
3692 /* prepare for next bucket */
3693 state->offset = 0;
3694 p = 0;
3696 return NULL;
3699 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3700 struct inet6_ifaddr *ifa)
3702 struct if6_iter_state *state = seq->private;
3703 struct net *net = seq_file_net(seq);
3705 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3706 if (!net_eq(dev_net(ifa->idev->dev), net))
3707 continue;
3708 state->offset++;
3709 return ifa;
3712 while (++state->bucket < IN6_ADDR_HSIZE) {
3713 state->offset = 0;
3714 hlist_for_each_entry_rcu_bh(ifa,
3715 &inet6_addr_lst[state->bucket], addr_lst) {
3716 if (!net_eq(dev_net(ifa->idev->dev), net))
3717 continue;
3718 state->offset++;
3719 return ifa;
3723 return NULL;
3726 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3727 __acquires(rcu_bh)
3729 rcu_read_lock_bh();
3730 return if6_get_first(seq, *pos);
3733 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3735 struct inet6_ifaddr *ifa;
3737 ifa = if6_get_next(seq, v);
3738 ++*pos;
3739 return ifa;
3742 static void if6_seq_stop(struct seq_file *seq, void *v)
3743 __releases(rcu_bh)
3745 rcu_read_unlock_bh();
3748 static int if6_seq_show(struct seq_file *seq, void *v)
3750 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3751 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3752 &ifp->addr,
3753 ifp->idev->dev->ifindex,
3754 ifp->prefix_len,
3755 ifp->scope,
3756 (u8) ifp->flags,
3757 ifp->idev->dev->name);
3758 return 0;
3761 static const struct seq_operations if6_seq_ops = {
3762 .start = if6_seq_start,
3763 .next = if6_seq_next,
3764 .show = if6_seq_show,
3765 .stop = if6_seq_stop,
3768 static int if6_seq_open(struct inode *inode, struct file *file)
3770 return seq_open_net(inode, file, &if6_seq_ops,
3771 sizeof(struct if6_iter_state));
3774 static const struct file_operations if6_fops = {
3775 .owner = THIS_MODULE,
3776 .open = if6_seq_open,
3777 .read = seq_read,
3778 .llseek = seq_lseek,
3779 .release = seq_release_net,
3782 static int __net_init if6_proc_net_init(struct net *net)
3784 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3785 return -ENOMEM;
3786 return 0;
3789 static void __net_exit if6_proc_net_exit(struct net *net)
3791 remove_proc_entry("if_inet6", net->proc_net);
3794 static struct pernet_operations if6_proc_net_ops = {
3795 .init = if6_proc_net_init,
3796 .exit = if6_proc_net_exit,
3799 int __init if6_proc_init(void)
3801 return register_pernet_subsys(&if6_proc_net_ops);
3804 void if6_proc_exit(void)
3806 unregister_pernet_subsys(&if6_proc_net_ops);
3808 #endif /* CONFIG_PROC_FS */
3810 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3811 /* Check if address is a home address configured on any interface. */
3812 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3814 int ret = 0;
3815 struct inet6_ifaddr *ifp = NULL;
3816 unsigned int hash = inet6_addr_hash(addr);
3818 rcu_read_lock_bh();
3819 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3820 if (!net_eq(dev_net(ifp->idev->dev), net))
3821 continue;
3822 if (ipv6_addr_equal(&ifp->addr, addr) &&
3823 (ifp->flags & IFA_F_HOMEADDRESS)) {
3824 ret = 1;
3825 break;
3828 rcu_read_unlock_bh();
3829 return ret;
3831 #endif
3834 * Periodic address status verification
3837 static void addrconf_verify_rtnl(void)
3839 unsigned long now, next, next_sec, next_sched;
3840 struct inet6_ifaddr *ifp;
3841 int i;
3843 ASSERT_RTNL();
3845 rcu_read_lock_bh();
3846 now = jiffies;
3847 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3849 cancel_delayed_work(&addr_chk_work);
3851 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3852 restart:
3853 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
3854 unsigned long age;
3856 /* When setting preferred_lft to a value not zero or
3857 * infinity, while valid_lft is infinity
3858 * IFA_F_PERMANENT has a non-infinity life time.
3860 if ((ifp->flags & IFA_F_PERMANENT) &&
3861 (ifp->prefered_lft == INFINITY_LIFE_TIME))
3862 continue;
3864 spin_lock(&ifp->lock);
3865 /* We try to batch several events at once. */
3866 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3868 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3869 age >= ifp->valid_lft) {
3870 spin_unlock(&ifp->lock);
3871 in6_ifa_hold(ifp);
3872 ipv6_del_addr(ifp);
3873 goto restart;
3874 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3875 spin_unlock(&ifp->lock);
3876 continue;
3877 } else if (age >= ifp->prefered_lft) {
3878 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3879 int deprecate = 0;
3881 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3882 deprecate = 1;
3883 ifp->flags |= IFA_F_DEPRECATED;
3886 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3887 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
3888 next = ifp->tstamp + ifp->valid_lft * HZ;
3890 spin_unlock(&ifp->lock);
3892 if (deprecate) {
3893 in6_ifa_hold(ifp);
3895 ipv6_ifa_notify(0, ifp);
3896 in6_ifa_put(ifp);
3897 goto restart;
3899 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3900 !(ifp->flags&IFA_F_TENTATIVE)) {
3901 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3902 ifp->idev->cnf.dad_transmits *
3903 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
3905 if (age >= ifp->prefered_lft - regen_advance) {
3906 struct inet6_ifaddr *ifpub = ifp->ifpub;
3907 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3908 next = ifp->tstamp + ifp->prefered_lft * HZ;
3909 if (!ifp->regen_count && ifpub) {
3910 ifp->regen_count++;
3911 in6_ifa_hold(ifp);
3912 in6_ifa_hold(ifpub);
3913 spin_unlock(&ifp->lock);
3915 spin_lock(&ifpub->lock);
3916 ifpub->regen_count = 0;
3917 spin_unlock(&ifpub->lock);
3918 ipv6_create_tempaddr(ifpub, ifp);
3919 in6_ifa_put(ifpub);
3920 in6_ifa_put(ifp);
3921 goto restart;
3923 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3924 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3925 spin_unlock(&ifp->lock);
3926 } else {
3927 /* ifp->prefered_lft <= ifp->valid_lft */
3928 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3929 next = ifp->tstamp + ifp->prefered_lft * HZ;
3930 spin_unlock(&ifp->lock);
3935 next_sec = round_jiffies_up(next);
3936 next_sched = next;
3938 /* If rounded timeout is accurate enough, accept it. */
3939 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3940 next_sched = next_sec;
3942 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3943 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3944 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3946 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3947 now, next, next_sec, next_sched);
3948 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
3949 rcu_read_unlock_bh();
3952 static void addrconf_verify_work(struct work_struct *w)
3954 rtnl_lock();
3955 addrconf_verify_rtnl();
3956 rtnl_unlock();
3959 static void addrconf_verify(void)
3961 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3964 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3965 struct in6_addr **peer_pfx)
3967 struct in6_addr *pfx = NULL;
3969 *peer_pfx = NULL;
3971 if (addr)
3972 pfx = nla_data(addr);
3974 if (local) {
3975 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3976 *peer_pfx = pfx;
3977 pfx = nla_data(local);
3980 return pfx;
3983 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3984 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3985 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3986 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3987 [IFA_FLAGS] = { .len = sizeof(u32) },
3990 static int
3991 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3993 struct net *net = sock_net(skb->sk);
3994 struct ifaddrmsg *ifm;
3995 struct nlattr *tb[IFA_MAX+1];
3996 struct in6_addr *pfx, *peer_pfx;
3997 u32 ifa_flags;
3998 int err;
4000 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4001 if (err < 0)
4002 return err;
4004 ifm = nlmsg_data(nlh);
4005 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4006 if (!pfx)
4007 return -EINVAL;
4009 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4011 /* We ignore other flags so far. */
4012 ifa_flags &= IFA_F_MANAGETEMPADDR;
4014 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4015 ifm->ifa_prefixlen);
4018 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4019 u32 prefered_lft, u32 valid_lft)
4021 u32 flags;
4022 clock_t expires;
4023 unsigned long timeout;
4024 bool was_managetempaddr;
4025 bool had_prefixroute;
4027 ASSERT_RTNL();
4029 if (!valid_lft || (prefered_lft > valid_lft))
4030 return -EINVAL;
4032 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4033 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4034 return -EINVAL;
4036 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4037 if (addrconf_finite_timeout(timeout)) {
4038 expires = jiffies_to_clock_t(timeout * HZ);
4039 valid_lft = timeout;
4040 flags = RTF_EXPIRES;
4041 } else {
4042 expires = 0;
4043 flags = 0;
4044 ifa_flags |= IFA_F_PERMANENT;
4047 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4048 if (addrconf_finite_timeout(timeout)) {
4049 if (timeout == 0)
4050 ifa_flags |= IFA_F_DEPRECATED;
4051 prefered_lft = timeout;
4054 spin_lock_bh(&ifp->lock);
4055 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4056 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4057 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4058 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4059 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4060 IFA_F_NOPREFIXROUTE);
4061 ifp->flags |= ifa_flags;
4062 ifp->tstamp = jiffies;
4063 ifp->valid_lft = valid_lft;
4064 ifp->prefered_lft = prefered_lft;
4066 spin_unlock_bh(&ifp->lock);
4067 if (!(ifp->flags&IFA_F_TENTATIVE))
4068 ipv6_ifa_notify(0, ifp);
4070 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4071 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4072 expires, flags);
4073 } else if (had_prefixroute) {
4074 enum cleanup_prefix_rt_t action;
4075 unsigned long rt_expires;
4077 write_lock_bh(&ifp->idev->lock);
4078 action = check_cleanup_prefix_route(ifp, &rt_expires);
4079 write_unlock_bh(&ifp->idev->lock);
4081 if (action != CLEANUP_PREFIX_RT_NOP) {
4082 cleanup_prefix_route(ifp, rt_expires,
4083 action == CLEANUP_PREFIX_RT_DEL);
4087 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4088 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4089 valid_lft = prefered_lft = 0;
4090 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4091 !was_managetempaddr, jiffies);
4094 addrconf_verify_rtnl();
4096 return 0;
4099 static int
4100 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4102 struct net *net = sock_net(skb->sk);
4103 struct ifaddrmsg *ifm;
4104 struct nlattr *tb[IFA_MAX+1];
4105 struct in6_addr *pfx, *peer_pfx;
4106 struct inet6_ifaddr *ifa;
4107 struct net_device *dev;
4108 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4109 u32 ifa_flags;
4110 int err;
4112 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4113 if (err < 0)
4114 return err;
4116 ifm = nlmsg_data(nlh);
4117 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4118 if (!pfx)
4119 return -EINVAL;
4121 if (tb[IFA_CACHEINFO]) {
4122 struct ifa_cacheinfo *ci;
4124 ci = nla_data(tb[IFA_CACHEINFO]);
4125 valid_lft = ci->ifa_valid;
4126 preferred_lft = ci->ifa_prefered;
4127 } else {
4128 preferred_lft = INFINITY_LIFE_TIME;
4129 valid_lft = INFINITY_LIFE_TIME;
4132 dev = __dev_get_by_index(net, ifm->ifa_index);
4133 if (!dev)
4134 return -ENODEV;
4136 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4138 /* We ignore other flags so far. */
4139 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4140 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4142 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4143 if (!ifa) {
4145 * It would be best to check for !NLM_F_CREATE here but
4146 * userspace already relies on not having to provide this.
4148 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4149 ifm->ifa_prefixlen, ifa_flags,
4150 preferred_lft, valid_lft);
4153 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4154 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4155 err = -EEXIST;
4156 else
4157 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4159 in6_ifa_put(ifa);
4161 return err;
4164 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4165 u8 scope, int ifindex)
4167 struct ifaddrmsg *ifm;
4169 ifm = nlmsg_data(nlh);
4170 ifm->ifa_family = AF_INET6;
4171 ifm->ifa_prefixlen = prefixlen;
4172 ifm->ifa_flags = flags;
4173 ifm->ifa_scope = scope;
4174 ifm->ifa_index = ifindex;
4177 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4178 unsigned long tstamp, u32 preferred, u32 valid)
4180 struct ifa_cacheinfo ci;
4182 ci.cstamp = cstamp_delta(cstamp);
4183 ci.tstamp = cstamp_delta(tstamp);
4184 ci.ifa_prefered = preferred;
4185 ci.ifa_valid = valid;
4187 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4190 static inline int rt_scope(int ifa_scope)
4192 if (ifa_scope & IFA_HOST)
4193 return RT_SCOPE_HOST;
4194 else if (ifa_scope & IFA_LINK)
4195 return RT_SCOPE_LINK;
4196 else if (ifa_scope & IFA_SITE)
4197 return RT_SCOPE_SITE;
4198 else
4199 return RT_SCOPE_UNIVERSE;
4202 static inline int inet6_ifaddr_msgsize(void)
4204 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4205 + nla_total_size(16) /* IFA_LOCAL */
4206 + nla_total_size(16) /* IFA_ADDRESS */
4207 + nla_total_size(sizeof(struct ifa_cacheinfo))
4208 + nla_total_size(4) /* IFA_FLAGS */;
4211 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4212 u32 portid, u32 seq, int event, unsigned int flags)
4214 struct nlmsghdr *nlh;
4215 u32 preferred, valid;
4217 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4218 if (!nlh)
4219 return -EMSGSIZE;
4221 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4222 ifa->idev->dev->ifindex);
4224 if (!((ifa->flags&IFA_F_PERMANENT) &&
4225 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4226 preferred = ifa->prefered_lft;
4227 valid = ifa->valid_lft;
4228 if (preferred != INFINITY_LIFE_TIME) {
4229 long tval = (jiffies - ifa->tstamp)/HZ;
4230 if (preferred > tval)
4231 preferred -= tval;
4232 else
4233 preferred = 0;
4234 if (valid != INFINITY_LIFE_TIME) {
4235 if (valid > tval)
4236 valid -= tval;
4237 else
4238 valid = 0;
4241 } else {
4242 preferred = INFINITY_LIFE_TIME;
4243 valid = INFINITY_LIFE_TIME;
4246 if (!ipv6_addr_any(&ifa->peer_addr)) {
4247 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4248 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4249 goto error;
4250 } else
4251 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4252 goto error;
4254 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4255 goto error;
4257 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4258 goto error;
4260 nlmsg_end(skb, nlh);
4261 return 0;
4263 error:
4264 nlmsg_cancel(skb, nlh);
4265 return -EMSGSIZE;
4268 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4269 u32 portid, u32 seq, int event, u16 flags)
4271 struct nlmsghdr *nlh;
4272 u8 scope = RT_SCOPE_UNIVERSE;
4273 int ifindex = ifmca->idev->dev->ifindex;
4275 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4276 scope = RT_SCOPE_SITE;
4278 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4279 if (!nlh)
4280 return -EMSGSIZE;
4282 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4283 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4284 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4285 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4286 nlmsg_cancel(skb, nlh);
4287 return -EMSGSIZE;
4290 nlmsg_end(skb, nlh);
4291 return 0;
4294 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4295 u32 portid, u32 seq, int event, unsigned int flags)
4297 struct nlmsghdr *nlh;
4298 u8 scope = RT_SCOPE_UNIVERSE;
4299 int ifindex = ifaca->aca_idev->dev->ifindex;
4301 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4302 scope = RT_SCOPE_SITE;
4304 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4305 if (!nlh)
4306 return -EMSGSIZE;
4308 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4309 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4310 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4311 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4312 nlmsg_cancel(skb, nlh);
4313 return -EMSGSIZE;
4316 nlmsg_end(skb, nlh);
4317 return 0;
4320 enum addr_type_t {
4321 UNICAST_ADDR,
4322 MULTICAST_ADDR,
4323 ANYCAST_ADDR,
4326 /* called with rcu_read_lock() */
4327 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4328 struct netlink_callback *cb, enum addr_type_t type,
4329 int s_ip_idx, int *p_ip_idx)
4331 struct ifmcaddr6 *ifmca;
4332 struct ifacaddr6 *ifaca;
4333 int err = 1;
4334 int ip_idx = *p_ip_idx;
4336 read_lock_bh(&idev->lock);
4337 switch (type) {
4338 case UNICAST_ADDR: {
4339 struct inet6_ifaddr *ifa;
4341 /* unicast address incl. temp addr */
4342 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4343 if (++ip_idx < s_ip_idx)
4344 continue;
4345 err = inet6_fill_ifaddr(skb, ifa,
4346 NETLINK_CB(cb->skb).portid,
4347 cb->nlh->nlmsg_seq,
4348 RTM_NEWADDR,
4349 NLM_F_MULTI);
4350 if (err < 0)
4351 break;
4352 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4354 break;
4356 case MULTICAST_ADDR:
4357 /* multicast address */
4358 for (ifmca = idev->mc_list; ifmca;
4359 ifmca = ifmca->next, ip_idx++) {
4360 if (ip_idx < s_ip_idx)
4361 continue;
4362 err = inet6_fill_ifmcaddr(skb, ifmca,
4363 NETLINK_CB(cb->skb).portid,
4364 cb->nlh->nlmsg_seq,
4365 RTM_GETMULTICAST,
4366 NLM_F_MULTI);
4367 if (err < 0)
4368 break;
4370 break;
4371 case ANYCAST_ADDR:
4372 /* anycast address */
4373 for (ifaca = idev->ac_list; ifaca;
4374 ifaca = ifaca->aca_next, ip_idx++) {
4375 if (ip_idx < s_ip_idx)
4376 continue;
4377 err = inet6_fill_ifacaddr(skb, ifaca,
4378 NETLINK_CB(cb->skb).portid,
4379 cb->nlh->nlmsg_seq,
4380 RTM_GETANYCAST,
4381 NLM_F_MULTI);
4382 if (err < 0)
4383 break;
4385 break;
4386 default:
4387 break;
4389 read_unlock_bh(&idev->lock);
4390 *p_ip_idx = ip_idx;
4391 return err;
4394 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4395 enum addr_type_t type)
4397 struct net *net = sock_net(skb->sk);
4398 int h, s_h;
4399 int idx, ip_idx;
4400 int s_idx, s_ip_idx;
4401 struct net_device *dev;
4402 struct inet6_dev *idev;
4403 struct hlist_head *head;
4405 s_h = cb->args[0];
4406 s_idx = idx = cb->args[1];
4407 s_ip_idx = ip_idx = cb->args[2];
4409 rcu_read_lock();
4410 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4411 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4412 idx = 0;
4413 head = &net->dev_index_head[h];
4414 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4415 if (idx < s_idx)
4416 goto cont;
4417 if (h > s_h || idx > s_idx)
4418 s_ip_idx = 0;
4419 ip_idx = 0;
4420 idev = __in6_dev_get(dev);
4421 if (!idev)
4422 goto cont;
4424 if (in6_dump_addrs(idev, skb, cb, type,
4425 s_ip_idx, &ip_idx) < 0)
4426 goto done;
4427 cont:
4428 idx++;
4431 done:
4432 rcu_read_unlock();
4433 cb->args[0] = h;
4434 cb->args[1] = idx;
4435 cb->args[2] = ip_idx;
4437 return skb->len;
4440 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4442 enum addr_type_t type = UNICAST_ADDR;
4444 return inet6_dump_addr(skb, cb, type);
4447 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4449 enum addr_type_t type = MULTICAST_ADDR;
4451 return inet6_dump_addr(skb, cb, type);
4455 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4457 enum addr_type_t type = ANYCAST_ADDR;
4459 return inet6_dump_addr(skb, cb, type);
4462 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4464 struct net *net = sock_net(in_skb->sk);
4465 struct ifaddrmsg *ifm;
4466 struct nlattr *tb[IFA_MAX+1];
4467 struct in6_addr *addr = NULL, *peer;
4468 struct net_device *dev = NULL;
4469 struct inet6_ifaddr *ifa;
4470 struct sk_buff *skb;
4471 int err;
4473 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4474 if (err < 0)
4475 goto errout;
4477 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4478 if (!addr) {
4479 err = -EINVAL;
4480 goto errout;
4483 ifm = nlmsg_data(nlh);
4484 if (ifm->ifa_index)
4485 dev = __dev_get_by_index(net, ifm->ifa_index);
4487 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4488 if (!ifa) {
4489 err = -EADDRNOTAVAIL;
4490 goto errout;
4493 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4494 if (!skb) {
4495 err = -ENOBUFS;
4496 goto errout_ifa;
4499 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4500 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4501 if (err < 0) {
4502 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4503 WARN_ON(err == -EMSGSIZE);
4504 kfree_skb(skb);
4505 goto errout_ifa;
4507 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4508 errout_ifa:
4509 in6_ifa_put(ifa);
4510 errout:
4511 return err;
4514 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4516 struct sk_buff *skb;
4517 struct net *net = dev_net(ifa->idev->dev);
4518 int err = -ENOBUFS;
4520 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4521 if (!skb)
4522 goto errout;
4524 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4525 if (err < 0) {
4526 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4527 WARN_ON(err == -EMSGSIZE);
4528 kfree_skb(skb);
4529 goto errout;
4531 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4532 return;
4533 errout:
4534 if (err < 0)
4535 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4538 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4539 __s32 *array, int bytes)
4541 BUG_ON(bytes < (DEVCONF_MAX * 4));
4543 memset(array, 0, bytes);
4544 array[DEVCONF_FORWARDING] = cnf->forwarding;
4545 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4546 array[DEVCONF_MTU6] = cnf->mtu6;
4547 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4548 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4549 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4550 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4551 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4552 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4553 jiffies_to_msecs(cnf->rtr_solicit_interval);
4554 array[DEVCONF_RTR_SOLICIT_DELAY] =
4555 jiffies_to_msecs(cnf->rtr_solicit_delay);
4556 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4557 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4558 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4559 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4560 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4561 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4562 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4563 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4564 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4565 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4566 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4567 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4568 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4569 #ifdef CONFIG_IPV6_ROUTER_PREF
4570 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4571 array[DEVCONF_RTR_PROBE_INTERVAL] =
4572 jiffies_to_msecs(cnf->rtr_probe_interval);
4573 #ifdef CONFIG_IPV6_ROUTE_INFO
4574 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4575 #endif
4576 #endif
4577 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4578 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4579 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4580 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4581 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4582 #endif
4583 #ifdef CONFIG_IPV6_MROUTE
4584 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4585 #endif
4586 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4587 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4588 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4589 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4590 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4591 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4592 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4593 /* we omit DEVCONF_STABLE_SECRET for now */
4596 static inline size_t inet6_ifla6_size(void)
4598 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4599 + nla_total_size(sizeof(struct ifla_cacheinfo))
4600 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4601 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4602 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4603 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4606 static inline size_t inet6_if_nlmsg_size(void)
4608 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4609 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4610 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4611 + nla_total_size(4) /* IFLA_MTU */
4612 + nla_total_size(4) /* IFLA_LINK */
4613 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4616 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4617 int items, int bytes)
4619 int i;
4620 int pad = bytes - sizeof(u64) * items;
4621 BUG_ON(pad < 0);
4623 /* Use put_unaligned() because stats may not be aligned for u64. */
4624 put_unaligned(items, &stats[0]);
4625 for (i = 1; i < items; i++)
4626 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4628 memset(&stats[items], 0, pad);
4631 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
4632 int items, int bytes, size_t syncpoff)
4634 int i;
4635 int pad = bytes - sizeof(u64) * items;
4636 BUG_ON(pad < 0);
4638 /* Use put_unaligned() because stats may not be aligned for u64. */
4639 put_unaligned(items, &stats[0]);
4640 for (i = 1; i < items; i++)
4641 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4643 memset(&stats[items], 0, pad);
4646 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4647 int bytes)
4649 switch (attrtype) {
4650 case IFLA_INET6_STATS:
4651 __snmp6_fill_stats64(stats, idev->stats.ipv6,
4652 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4653 break;
4654 case IFLA_INET6_ICMP6STATS:
4655 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4656 break;
4660 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4662 struct nlattr *nla;
4663 struct ifla_cacheinfo ci;
4665 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4666 goto nla_put_failure;
4667 ci.max_reasm_len = IPV6_MAXPLEN;
4668 ci.tstamp = cstamp_delta(idev->tstamp);
4669 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4670 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4671 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4672 goto nla_put_failure;
4673 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4674 if (!nla)
4675 goto nla_put_failure;
4676 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4678 /* XXX - MC not implemented */
4680 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4681 if (!nla)
4682 goto nla_put_failure;
4683 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4685 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4686 if (!nla)
4687 goto nla_put_failure;
4688 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4690 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4691 if (!nla)
4692 goto nla_put_failure;
4694 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4695 goto nla_put_failure;
4697 read_lock_bh(&idev->lock);
4698 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4699 read_unlock_bh(&idev->lock);
4701 return 0;
4703 nla_put_failure:
4704 return -EMSGSIZE;
4707 static size_t inet6_get_link_af_size(const struct net_device *dev)
4709 if (!__in6_dev_get(dev))
4710 return 0;
4712 return inet6_ifla6_size();
4715 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4717 struct inet6_dev *idev = __in6_dev_get(dev);
4719 if (!idev)
4720 return -ENODATA;
4722 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4723 return -EMSGSIZE;
4725 return 0;
4728 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4730 struct inet6_ifaddr *ifp;
4731 struct net_device *dev = idev->dev;
4732 bool update_rs = false;
4733 struct in6_addr ll_addr;
4735 ASSERT_RTNL();
4737 if (!token)
4738 return -EINVAL;
4739 if (ipv6_addr_any(token))
4740 return -EINVAL;
4741 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4742 return -EINVAL;
4743 if (!ipv6_accept_ra(idev))
4744 return -EINVAL;
4745 if (idev->cnf.rtr_solicits <= 0)
4746 return -EINVAL;
4748 write_lock_bh(&idev->lock);
4750 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4751 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4753 write_unlock_bh(&idev->lock);
4755 if (!idev->dead && (idev->if_flags & IF_READY) &&
4756 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4757 IFA_F_OPTIMISTIC)) {
4759 /* If we're not ready, then normal ifup will take care
4760 * of this. Otherwise, we need to request our rs here.
4762 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4763 update_rs = true;
4766 write_lock_bh(&idev->lock);
4768 if (update_rs) {
4769 idev->if_flags |= IF_RS_SENT;
4770 idev->rs_probes = 1;
4771 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4774 /* Well, that's kinda nasty ... */
4775 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4776 spin_lock(&ifp->lock);
4777 if (ifp->tokenized) {
4778 ifp->valid_lft = 0;
4779 ifp->prefered_lft = 0;
4781 spin_unlock(&ifp->lock);
4784 write_unlock_bh(&idev->lock);
4785 inet6_ifinfo_notify(RTM_NEWLINK, idev);
4786 addrconf_verify_rtnl();
4787 return 0;
4790 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
4791 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
4792 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
4795 static int inet6_validate_link_af(const struct net_device *dev,
4796 const struct nlattr *nla)
4798 struct nlattr *tb[IFLA_INET6_MAX + 1];
4800 if (dev && !__in6_dev_get(dev))
4801 return -EAFNOSUPPORT;
4803 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
4806 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4808 int err = -EINVAL;
4809 struct inet6_dev *idev = __in6_dev_get(dev);
4810 struct nlattr *tb[IFLA_INET6_MAX + 1];
4812 if (!idev)
4813 return -EAFNOSUPPORT;
4815 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4816 BUG();
4818 if (tb[IFLA_INET6_TOKEN]) {
4819 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4820 if (err)
4821 return err;
4824 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4825 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4827 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4828 mode != IN6_ADDR_GEN_MODE_NONE &&
4829 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY)
4830 return -EINVAL;
4832 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4833 !idev->cnf.stable_secret.initialized &&
4834 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4835 return -EINVAL;
4837 idev->addr_gen_mode = mode;
4838 err = 0;
4841 return err;
4844 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4845 u32 portid, u32 seq, int event, unsigned int flags)
4847 struct net_device *dev = idev->dev;
4848 struct ifinfomsg *hdr;
4849 struct nlmsghdr *nlh;
4850 void *protoinfo;
4852 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4853 if (!nlh)
4854 return -EMSGSIZE;
4856 hdr = nlmsg_data(nlh);
4857 hdr->ifi_family = AF_INET6;
4858 hdr->__ifi_pad = 0;
4859 hdr->ifi_type = dev->type;
4860 hdr->ifi_index = dev->ifindex;
4861 hdr->ifi_flags = dev_get_flags(dev);
4862 hdr->ifi_change = 0;
4864 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4865 (dev->addr_len &&
4866 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4867 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4868 (dev->ifindex != dev_get_iflink(dev) &&
4869 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4870 goto nla_put_failure;
4871 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4872 if (!protoinfo)
4873 goto nla_put_failure;
4875 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4876 goto nla_put_failure;
4878 nla_nest_end(skb, protoinfo);
4879 nlmsg_end(skb, nlh);
4880 return 0;
4882 nla_put_failure:
4883 nlmsg_cancel(skb, nlh);
4884 return -EMSGSIZE;
4887 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4889 struct net *net = sock_net(skb->sk);
4890 int h, s_h;
4891 int idx = 0, s_idx;
4892 struct net_device *dev;
4893 struct inet6_dev *idev;
4894 struct hlist_head *head;
4896 s_h = cb->args[0];
4897 s_idx = cb->args[1];
4899 rcu_read_lock();
4900 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4901 idx = 0;
4902 head = &net->dev_index_head[h];
4903 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4904 if (idx < s_idx)
4905 goto cont;
4906 idev = __in6_dev_get(dev);
4907 if (!idev)
4908 goto cont;
4909 if (inet6_fill_ifinfo(skb, idev,
4910 NETLINK_CB(cb->skb).portid,
4911 cb->nlh->nlmsg_seq,
4912 RTM_NEWLINK, NLM_F_MULTI) < 0)
4913 goto out;
4914 cont:
4915 idx++;
4918 out:
4919 rcu_read_unlock();
4920 cb->args[1] = idx;
4921 cb->args[0] = h;
4923 return skb->len;
4926 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4928 struct sk_buff *skb;
4929 struct net *net = dev_net(idev->dev);
4930 int err = -ENOBUFS;
4932 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4933 if (!skb)
4934 goto errout;
4936 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4937 if (err < 0) {
4938 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4939 WARN_ON(err == -EMSGSIZE);
4940 kfree_skb(skb);
4941 goto errout;
4943 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4944 return;
4945 errout:
4946 if (err < 0)
4947 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4950 static inline size_t inet6_prefix_nlmsg_size(void)
4952 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4953 + nla_total_size(sizeof(struct in6_addr))
4954 + nla_total_size(sizeof(struct prefix_cacheinfo));
4957 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4958 struct prefix_info *pinfo, u32 portid, u32 seq,
4959 int event, unsigned int flags)
4961 struct prefixmsg *pmsg;
4962 struct nlmsghdr *nlh;
4963 struct prefix_cacheinfo ci;
4965 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4966 if (!nlh)
4967 return -EMSGSIZE;
4969 pmsg = nlmsg_data(nlh);
4970 pmsg->prefix_family = AF_INET6;
4971 pmsg->prefix_pad1 = 0;
4972 pmsg->prefix_pad2 = 0;
4973 pmsg->prefix_ifindex = idev->dev->ifindex;
4974 pmsg->prefix_len = pinfo->prefix_len;
4975 pmsg->prefix_type = pinfo->type;
4976 pmsg->prefix_pad3 = 0;
4977 pmsg->prefix_flags = 0;
4978 if (pinfo->onlink)
4979 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4980 if (pinfo->autoconf)
4981 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4983 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4984 goto nla_put_failure;
4985 ci.preferred_time = ntohl(pinfo->prefered);
4986 ci.valid_time = ntohl(pinfo->valid);
4987 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4988 goto nla_put_failure;
4989 nlmsg_end(skb, nlh);
4990 return 0;
4992 nla_put_failure:
4993 nlmsg_cancel(skb, nlh);
4994 return -EMSGSIZE;
4997 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4998 struct prefix_info *pinfo)
5000 struct sk_buff *skb;
5001 struct net *net = dev_net(idev->dev);
5002 int err = -ENOBUFS;
5004 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5005 if (!skb)
5006 goto errout;
5008 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5009 if (err < 0) {
5010 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5011 WARN_ON(err == -EMSGSIZE);
5012 kfree_skb(skb);
5013 goto errout;
5015 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5016 return;
5017 errout:
5018 if (err < 0)
5019 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5022 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5024 struct net *net = dev_net(ifp->idev->dev);
5026 if (event)
5027 ASSERT_RTNL();
5029 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5031 switch (event) {
5032 case RTM_NEWADDR:
5034 * If the address was optimistic
5035 * we inserted the route at the start of
5036 * our DAD process, so we don't need
5037 * to do it again
5039 if (!(ifp->rt->rt6i_node))
5040 ip6_ins_rt(ifp->rt);
5041 if (ifp->idev->cnf.forwarding)
5042 addrconf_join_anycast(ifp);
5043 if (!ipv6_addr_any(&ifp->peer_addr))
5044 addrconf_prefix_route(&ifp->peer_addr, 128,
5045 ifp->idev->dev, 0, 0);
5046 break;
5047 case RTM_DELADDR:
5048 if (ifp->idev->cnf.forwarding)
5049 addrconf_leave_anycast(ifp);
5050 addrconf_leave_solict(ifp->idev, &ifp->addr);
5051 if (!ipv6_addr_any(&ifp->peer_addr)) {
5052 struct rt6_info *rt;
5054 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5055 ifp->idev->dev, 0, 0);
5056 if (rt && ip6_del_rt(rt))
5057 dst_free(&rt->dst);
5059 dst_hold(&ifp->rt->dst);
5061 if (ip6_del_rt(ifp->rt))
5062 dst_free(&ifp->rt->dst);
5064 rt_genid_bump_ipv6(net);
5065 break;
5067 atomic_inc(&net->ipv6.dev_addr_genid);
5070 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5072 rcu_read_lock_bh();
5073 if (likely(ifp->idev->dead == 0))
5074 __ipv6_ifa_notify(event, ifp);
5075 rcu_read_unlock_bh();
5078 #ifdef CONFIG_SYSCTL
5080 static
5081 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5082 void __user *buffer, size_t *lenp, loff_t *ppos)
5084 int *valp = ctl->data;
5085 int val = *valp;
5086 loff_t pos = *ppos;
5087 struct ctl_table lctl;
5088 int ret;
5091 * ctl->data points to idev->cnf.forwarding, we should
5092 * not modify it until we get the rtnl lock.
5094 lctl = *ctl;
5095 lctl.data = &val;
5097 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5099 if (write)
5100 ret = addrconf_fixup_forwarding(ctl, valp, val);
5101 if (ret)
5102 *ppos = pos;
5103 return ret;
5106 static
5107 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5108 void __user *buffer, size_t *lenp, loff_t *ppos)
5110 struct inet6_dev *idev = ctl->extra1;
5111 int min_mtu = IPV6_MIN_MTU;
5112 struct ctl_table lctl;
5114 lctl = *ctl;
5115 lctl.extra1 = &min_mtu;
5116 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5118 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5121 static void dev_disable_change(struct inet6_dev *idev)
5123 struct netdev_notifier_info info;
5125 if (!idev || !idev->dev)
5126 return;
5128 netdev_notifier_info_init(&info, idev->dev);
5129 if (idev->cnf.disable_ipv6)
5130 addrconf_notify(NULL, NETDEV_DOWN, &info);
5131 else
5132 addrconf_notify(NULL, NETDEV_UP, &info);
5135 static void addrconf_disable_change(struct net *net, __s32 newf)
5137 struct net_device *dev;
5138 struct inet6_dev *idev;
5140 rcu_read_lock();
5141 for_each_netdev_rcu(net, dev) {
5142 idev = __in6_dev_get(dev);
5143 if (idev) {
5144 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5145 idev->cnf.disable_ipv6 = newf;
5146 if (changed)
5147 dev_disable_change(idev);
5150 rcu_read_unlock();
5153 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5155 struct net *net;
5156 int old;
5158 if (!rtnl_trylock())
5159 return restart_syscall();
5161 net = (struct net *)table->extra2;
5162 old = *p;
5163 *p = newf;
5165 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5166 rtnl_unlock();
5167 return 0;
5170 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5171 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5172 addrconf_disable_change(net, newf);
5173 } else if ((!newf) ^ (!old))
5174 dev_disable_change((struct inet6_dev *)table->extra1);
5176 rtnl_unlock();
5177 return 0;
5180 static
5181 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5182 void __user *buffer, size_t *lenp, loff_t *ppos)
5184 int *valp = ctl->data;
5185 int val = *valp;
5186 loff_t pos = *ppos;
5187 struct ctl_table lctl;
5188 int ret;
5191 * ctl->data points to idev->cnf.disable_ipv6, we should
5192 * not modify it until we get the rtnl lock.
5194 lctl = *ctl;
5195 lctl.data = &val;
5197 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5199 if (write)
5200 ret = addrconf_disable_ipv6(ctl, valp, val);
5201 if (ret)
5202 *ppos = pos;
5203 return ret;
5206 static
5207 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5208 void __user *buffer, size_t *lenp, loff_t *ppos)
5210 int *valp = ctl->data;
5211 int ret;
5212 int old, new;
5214 old = *valp;
5215 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5216 new = *valp;
5218 if (write && old != new) {
5219 struct net *net = ctl->extra2;
5221 if (!rtnl_trylock())
5222 return restart_syscall();
5224 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5225 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5226 NETCONFA_IFINDEX_DEFAULT,
5227 net->ipv6.devconf_dflt);
5228 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5229 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5230 NETCONFA_IFINDEX_ALL,
5231 net->ipv6.devconf_all);
5232 else {
5233 struct inet6_dev *idev = ctl->extra1;
5235 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5236 idev->dev->ifindex,
5237 &idev->cnf);
5239 rtnl_unlock();
5242 return ret;
5245 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5246 void __user *buffer, size_t *lenp,
5247 loff_t *ppos)
5249 int err;
5250 struct in6_addr addr;
5251 char str[IPV6_MAX_STRLEN];
5252 struct ctl_table lctl = *ctl;
5253 struct net *net = ctl->extra2;
5254 struct ipv6_stable_secret *secret = ctl->data;
5256 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5257 return -EIO;
5259 lctl.maxlen = IPV6_MAX_STRLEN;
5260 lctl.data = str;
5262 if (!rtnl_trylock())
5263 return restart_syscall();
5265 if (!write && !secret->initialized) {
5266 err = -EIO;
5267 goto out;
5270 if (!write) {
5271 err = snprintf(str, sizeof(str), "%pI6",
5272 &secret->secret);
5273 if (err >= sizeof(str)) {
5274 err = -EIO;
5275 goto out;
5279 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5280 if (err || !write)
5281 goto out;
5283 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5284 err = -EIO;
5285 goto out;
5288 secret->initialized = true;
5289 secret->secret = addr;
5291 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5292 struct net_device *dev;
5294 for_each_netdev(net, dev) {
5295 struct inet6_dev *idev = __in6_dev_get(dev);
5297 if (idev) {
5298 idev->addr_gen_mode =
5299 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5302 } else {
5303 struct inet6_dev *idev = ctl->extra1;
5305 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5308 out:
5309 rtnl_unlock();
5311 return err;
5314 static struct addrconf_sysctl_table
5316 struct ctl_table_header *sysctl_header;
5317 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
5318 } addrconf_sysctl __read_mostly = {
5319 .sysctl_header = NULL,
5320 .addrconf_vars = {
5322 .procname = "forwarding",
5323 .data = &ipv6_devconf.forwarding,
5324 .maxlen = sizeof(int),
5325 .mode = 0644,
5326 .proc_handler = addrconf_sysctl_forward,
5329 .procname = "hop_limit",
5330 .data = &ipv6_devconf.hop_limit,
5331 .maxlen = sizeof(int),
5332 .mode = 0644,
5333 .proc_handler = proc_dointvec,
5336 .procname = "mtu",
5337 .data = &ipv6_devconf.mtu6,
5338 .maxlen = sizeof(int),
5339 .mode = 0644,
5340 .proc_handler = addrconf_sysctl_mtu,
5343 .procname = "accept_ra",
5344 .data = &ipv6_devconf.accept_ra,
5345 .maxlen = sizeof(int),
5346 .mode = 0644,
5347 .proc_handler = proc_dointvec,
5350 .procname = "accept_redirects",
5351 .data = &ipv6_devconf.accept_redirects,
5352 .maxlen = sizeof(int),
5353 .mode = 0644,
5354 .proc_handler = proc_dointvec,
5357 .procname = "autoconf",
5358 .data = &ipv6_devconf.autoconf,
5359 .maxlen = sizeof(int),
5360 .mode = 0644,
5361 .proc_handler = proc_dointvec,
5364 .procname = "dad_transmits",
5365 .data = &ipv6_devconf.dad_transmits,
5366 .maxlen = sizeof(int),
5367 .mode = 0644,
5368 .proc_handler = proc_dointvec,
5371 .procname = "router_solicitations",
5372 .data = &ipv6_devconf.rtr_solicits,
5373 .maxlen = sizeof(int),
5374 .mode = 0644,
5375 .proc_handler = proc_dointvec,
5378 .procname = "router_solicitation_interval",
5379 .data = &ipv6_devconf.rtr_solicit_interval,
5380 .maxlen = sizeof(int),
5381 .mode = 0644,
5382 .proc_handler = proc_dointvec_jiffies,
5385 .procname = "router_solicitation_delay",
5386 .data = &ipv6_devconf.rtr_solicit_delay,
5387 .maxlen = sizeof(int),
5388 .mode = 0644,
5389 .proc_handler = proc_dointvec_jiffies,
5392 .procname = "force_mld_version",
5393 .data = &ipv6_devconf.force_mld_version,
5394 .maxlen = sizeof(int),
5395 .mode = 0644,
5396 .proc_handler = proc_dointvec,
5399 .procname = "mldv1_unsolicited_report_interval",
5400 .data =
5401 &ipv6_devconf.mldv1_unsolicited_report_interval,
5402 .maxlen = sizeof(int),
5403 .mode = 0644,
5404 .proc_handler = proc_dointvec_ms_jiffies,
5407 .procname = "mldv2_unsolicited_report_interval",
5408 .data =
5409 &ipv6_devconf.mldv2_unsolicited_report_interval,
5410 .maxlen = sizeof(int),
5411 .mode = 0644,
5412 .proc_handler = proc_dointvec_ms_jiffies,
5415 .procname = "use_tempaddr",
5416 .data = &ipv6_devconf.use_tempaddr,
5417 .maxlen = sizeof(int),
5418 .mode = 0644,
5419 .proc_handler = proc_dointvec,
5422 .procname = "temp_valid_lft",
5423 .data = &ipv6_devconf.temp_valid_lft,
5424 .maxlen = sizeof(int),
5425 .mode = 0644,
5426 .proc_handler = proc_dointvec,
5429 .procname = "temp_prefered_lft",
5430 .data = &ipv6_devconf.temp_prefered_lft,
5431 .maxlen = sizeof(int),
5432 .mode = 0644,
5433 .proc_handler = proc_dointvec,
5436 .procname = "regen_max_retry",
5437 .data = &ipv6_devconf.regen_max_retry,
5438 .maxlen = sizeof(int),
5439 .mode = 0644,
5440 .proc_handler = proc_dointvec,
5443 .procname = "max_desync_factor",
5444 .data = &ipv6_devconf.max_desync_factor,
5445 .maxlen = sizeof(int),
5446 .mode = 0644,
5447 .proc_handler = proc_dointvec,
5450 .procname = "max_addresses",
5451 .data = &ipv6_devconf.max_addresses,
5452 .maxlen = sizeof(int),
5453 .mode = 0644,
5454 .proc_handler = proc_dointvec,
5457 .procname = "accept_ra_defrtr",
5458 .data = &ipv6_devconf.accept_ra_defrtr,
5459 .maxlen = sizeof(int),
5460 .mode = 0644,
5461 .proc_handler = proc_dointvec,
5464 .procname = "accept_ra_pinfo",
5465 .data = &ipv6_devconf.accept_ra_pinfo,
5466 .maxlen = sizeof(int),
5467 .mode = 0644,
5468 .proc_handler = proc_dointvec,
5470 #ifdef CONFIG_IPV6_ROUTER_PREF
5472 .procname = "accept_ra_rtr_pref",
5473 .data = &ipv6_devconf.accept_ra_rtr_pref,
5474 .maxlen = sizeof(int),
5475 .mode = 0644,
5476 .proc_handler = proc_dointvec,
5479 .procname = "router_probe_interval",
5480 .data = &ipv6_devconf.rtr_probe_interval,
5481 .maxlen = sizeof(int),
5482 .mode = 0644,
5483 .proc_handler = proc_dointvec_jiffies,
5485 #ifdef CONFIG_IPV6_ROUTE_INFO
5487 .procname = "accept_ra_rt_info_max_plen",
5488 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5489 .maxlen = sizeof(int),
5490 .mode = 0644,
5491 .proc_handler = proc_dointvec,
5493 #endif
5494 #endif
5496 .procname = "proxy_ndp",
5497 .data = &ipv6_devconf.proxy_ndp,
5498 .maxlen = sizeof(int),
5499 .mode = 0644,
5500 .proc_handler = addrconf_sysctl_proxy_ndp,
5503 .procname = "accept_source_route",
5504 .data = &ipv6_devconf.accept_source_route,
5505 .maxlen = sizeof(int),
5506 .mode = 0644,
5507 .proc_handler = proc_dointvec,
5509 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5511 .procname = "optimistic_dad",
5512 .data = &ipv6_devconf.optimistic_dad,
5513 .maxlen = sizeof(int),
5514 .mode = 0644,
5515 .proc_handler = proc_dointvec,
5519 .procname = "use_optimistic",
5520 .data = &ipv6_devconf.use_optimistic,
5521 .maxlen = sizeof(int),
5522 .mode = 0644,
5523 .proc_handler = proc_dointvec,
5526 #endif
5527 #ifdef CONFIG_IPV6_MROUTE
5529 .procname = "mc_forwarding",
5530 .data = &ipv6_devconf.mc_forwarding,
5531 .maxlen = sizeof(int),
5532 .mode = 0444,
5533 .proc_handler = proc_dointvec,
5535 #endif
5537 .procname = "disable_ipv6",
5538 .data = &ipv6_devconf.disable_ipv6,
5539 .maxlen = sizeof(int),
5540 .mode = 0644,
5541 .proc_handler = addrconf_sysctl_disable,
5544 .procname = "accept_dad",
5545 .data = &ipv6_devconf.accept_dad,
5546 .maxlen = sizeof(int),
5547 .mode = 0644,
5548 .proc_handler = proc_dointvec,
5551 .procname = "force_tllao",
5552 .data = &ipv6_devconf.force_tllao,
5553 .maxlen = sizeof(int),
5554 .mode = 0644,
5555 .proc_handler = proc_dointvec
5558 .procname = "ndisc_notify",
5559 .data = &ipv6_devconf.ndisc_notify,
5560 .maxlen = sizeof(int),
5561 .mode = 0644,
5562 .proc_handler = proc_dointvec
5565 .procname = "suppress_frag_ndisc",
5566 .data = &ipv6_devconf.suppress_frag_ndisc,
5567 .maxlen = sizeof(int),
5568 .mode = 0644,
5569 .proc_handler = proc_dointvec
5572 .procname = "accept_ra_from_local",
5573 .data = &ipv6_devconf.accept_ra_from_local,
5574 .maxlen = sizeof(int),
5575 .mode = 0644,
5576 .proc_handler = proc_dointvec,
5579 .procname = "accept_ra_mtu",
5580 .data = &ipv6_devconf.accept_ra_mtu,
5581 .maxlen = sizeof(int),
5582 .mode = 0644,
5583 .proc_handler = proc_dointvec,
5586 .procname = "stable_secret",
5587 .data = &ipv6_devconf.stable_secret,
5588 .maxlen = IPV6_MAX_STRLEN,
5589 .mode = 0600,
5590 .proc_handler = addrconf_sysctl_stable_secret,
5593 /* sentinel */
5598 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5599 struct inet6_dev *idev, struct ipv6_devconf *p)
5601 int i;
5602 struct addrconf_sysctl_table *t;
5603 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5605 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5606 if (!t)
5607 goto out;
5609 for (i = 0; t->addrconf_vars[i].data; i++) {
5610 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5611 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5612 t->addrconf_vars[i].extra2 = net;
5615 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5617 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5618 if (!t->sysctl_header)
5619 goto free;
5621 p->sysctl = t;
5622 return 0;
5624 free:
5625 kfree(t);
5626 out:
5627 return -ENOBUFS;
5630 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5632 struct addrconf_sysctl_table *t;
5634 if (!p->sysctl)
5635 return;
5637 t = p->sysctl;
5638 p->sysctl = NULL;
5639 unregister_net_sysctl_table(t->sysctl_header);
5640 kfree(t);
5643 static int addrconf_sysctl_register(struct inet6_dev *idev)
5645 int err;
5647 if (!sysctl_dev_name_is_allowed(idev->dev->name))
5648 return -EINVAL;
5650 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5651 &ndisc_ifinfo_sysctl_change);
5652 if (err)
5653 return err;
5654 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5655 idev, &idev->cnf);
5656 if (err)
5657 neigh_sysctl_unregister(idev->nd_parms);
5659 return err;
5662 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5664 __addrconf_sysctl_unregister(&idev->cnf);
5665 neigh_sysctl_unregister(idev->nd_parms);
5669 #endif
5671 static int __net_init addrconf_init_net(struct net *net)
5673 int err = -ENOMEM;
5674 struct ipv6_devconf *all, *dflt;
5676 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5677 if (!all)
5678 goto err_alloc_all;
5680 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5681 if (!dflt)
5682 goto err_alloc_dflt;
5684 /* these will be inherited by all namespaces */
5685 dflt->autoconf = ipv6_defaults.autoconf;
5686 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5688 dflt->stable_secret.initialized = false;
5689 all->stable_secret.initialized = false;
5691 net->ipv6.devconf_all = all;
5692 net->ipv6.devconf_dflt = dflt;
5694 #ifdef CONFIG_SYSCTL
5695 err = __addrconf_sysctl_register(net, "all", NULL, all);
5696 if (err < 0)
5697 goto err_reg_all;
5699 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5700 if (err < 0)
5701 goto err_reg_dflt;
5702 #endif
5703 return 0;
5705 #ifdef CONFIG_SYSCTL
5706 err_reg_dflt:
5707 __addrconf_sysctl_unregister(all);
5708 err_reg_all:
5709 kfree(dflt);
5710 #endif
5711 err_alloc_dflt:
5712 kfree(all);
5713 err_alloc_all:
5714 return err;
5717 static void __net_exit addrconf_exit_net(struct net *net)
5719 #ifdef CONFIG_SYSCTL
5720 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5721 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5722 #endif
5723 kfree(net->ipv6.devconf_dflt);
5724 kfree(net->ipv6.devconf_all);
5727 static struct pernet_operations addrconf_ops = {
5728 .init = addrconf_init_net,
5729 .exit = addrconf_exit_net,
5732 static struct rtnl_af_ops inet6_ops __read_mostly = {
5733 .family = AF_INET6,
5734 .fill_link_af = inet6_fill_link_af,
5735 .get_link_af_size = inet6_get_link_af_size,
5736 .validate_link_af = inet6_validate_link_af,
5737 .set_link_af = inet6_set_link_af,
5741 * Init / cleanup code
5744 int __init addrconf_init(void)
5746 struct inet6_dev *idev;
5747 int i, err;
5749 err = ipv6_addr_label_init();
5750 if (err < 0) {
5751 pr_crit("%s: cannot initialize default policy table: %d\n",
5752 __func__, err);
5753 goto out;
5756 err = register_pernet_subsys(&addrconf_ops);
5757 if (err < 0)
5758 goto out_addrlabel;
5760 addrconf_wq = create_workqueue("ipv6_addrconf");
5761 if (!addrconf_wq) {
5762 err = -ENOMEM;
5763 goto out_nowq;
5766 /* The addrconf netdev notifier requires that loopback_dev
5767 * has it's ipv6 private information allocated and setup
5768 * before it can bring up and give link-local addresses
5769 * to other devices which are up.
5771 * Unfortunately, loopback_dev is not necessarily the first
5772 * entry in the global dev_base list of net devices. In fact,
5773 * it is likely to be the very last entry on that list.
5774 * So this causes the notifier registry below to try and
5775 * give link-local addresses to all devices besides loopback_dev
5776 * first, then loopback_dev, which cases all the non-loopback_dev
5777 * devices to fail to get a link-local address.
5779 * So, as a temporary fix, allocate the ipv6 structure for
5780 * loopback_dev first by hand.
5781 * Longer term, all of the dependencies ipv6 has upon the loopback
5782 * device and it being up should be removed.
5784 rtnl_lock();
5785 idev = ipv6_add_dev(init_net.loopback_dev);
5786 rtnl_unlock();
5787 if (IS_ERR(idev)) {
5788 err = PTR_ERR(idev);
5789 goto errlo;
5792 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5793 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5795 register_netdevice_notifier(&ipv6_dev_notf);
5797 addrconf_verify();
5799 rtnl_af_register(&inet6_ops);
5801 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5802 NULL);
5803 if (err < 0)
5804 goto errout;
5806 /* Only the first call to __rtnl_register can fail */
5807 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5808 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5809 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5810 inet6_dump_ifaddr, NULL);
5811 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5812 inet6_dump_ifmcaddr, NULL);
5813 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5814 inet6_dump_ifacaddr, NULL);
5815 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5816 inet6_netconf_dump_devconf, NULL);
5818 ipv6_addr_label_rtnl_register();
5820 return 0;
5821 errout:
5822 rtnl_af_unregister(&inet6_ops);
5823 unregister_netdevice_notifier(&ipv6_dev_notf);
5824 errlo:
5825 destroy_workqueue(addrconf_wq);
5826 out_nowq:
5827 unregister_pernet_subsys(&addrconf_ops);
5828 out_addrlabel:
5829 ipv6_addr_label_cleanup();
5830 out:
5831 return err;
5834 void addrconf_cleanup(void)
5836 struct net_device *dev;
5837 int i;
5839 unregister_netdevice_notifier(&ipv6_dev_notf);
5840 unregister_pernet_subsys(&addrconf_ops);
5841 ipv6_addr_label_cleanup();
5843 rtnl_lock();
5845 __rtnl_af_unregister(&inet6_ops);
5847 /* clean dev list */
5848 for_each_netdev(&init_net, dev) {
5849 if (__in6_dev_get(dev) == NULL)
5850 continue;
5851 addrconf_ifdown(dev, 1);
5853 addrconf_ifdown(init_net.loopback_dev, 2);
5856 * Check hash table.
5858 spin_lock_bh(&addrconf_hash_lock);
5859 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5860 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5861 spin_unlock_bh(&addrconf_hash_lock);
5862 cancel_delayed_work(&addr_chk_work);
5863 rtnl_unlock();
5865 destroy_workqueue(addrconf_wq);