ASoC: intel: Replace kthread with work
[linux/fpc-iii.git] / net / ipv6 / addrconf.c
blobd8983e15f85945343ab85b85b0e1c5cb9916b6ab
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/6lowpan.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 <net/l3mdev.h>
85 #include <linux/if_tunnel.h>
86 #include <linux/rtnetlink.h>
87 #include <linux/netconf.h>
88 #include <linux/random.h>
89 #include <linux/uaccess.h>
90 #include <asm/unaligned.h>
92 #include <linux/proc_fs.h>
93 #include <linux/seq_file.h>
94 #include <linux/export.h>
96 /* Set to 3 to get tracing... */
97 #define ACONF_DEBUG 2
99 #if ACONF_DEBUG >= 3
100 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
101 #else
102 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
103 #endif
105 #define INFINITY_LIFE_TIME 0xFFFFFFFF
107 #define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110 static inline u32 cstamp_delta(unsigned long cstamp)
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
115 static inline s32 rfc3315_s14_backoff_init(s32 irt)
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
119 do_div(tmp, 1000000);
120 return (s32)tmp;
123 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
127 do_div(tmp, 1000000);
128 if ((s32)tmp > mrt) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
131 do_div(tmp, 1000000);
133 return (s32)tmp;
136 #ifdef CONFIG_SYSCTL
137 static int addrconf_sysctl_register(struct inet6_dev *idev);
138 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
139 #else
140 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
142 return 0;
145 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
148 #endif
150 static void __ipv6_regen_rndid(struct inet6_dev *idev);
151 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
152 static void ipv6_regen_rndid(unsigned long data);
154 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
155 static int ipv6_count_addresses(struct inet6_dev *idev);
156 static int ipv6_generate_stable_address(struct in6_addr *addr,
157 u8 dad_count,
158 const struct inet6_dev *idev);
161 * Configured unicast address hash table
163 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
164 static DEFINE_SPINLOCK(addrconf_hash_lock);
166 static void addrconf_verify(void);
167 static void addrconf_verify_rtnl(void);
168 static void addrconf_verify_work(struct work_struct *);
170 static struct workqueue_struct *addrconf_wq;
171 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
173 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
176 static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
178 static int addrconf_ifdown(struct net_device *dev, int how);
180 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
181 int plen,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
185 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
186 static void addrconf_dad_work(struct work_struct *w);
187 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
188 static void addrconf_dad_run(struct inet6_dev *idev);
189 static void addrconf_rs_timer(unsigned long data);
190 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
193 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
194 struct prefix_info *pinfo);
195 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
198 static struct ipv6_devconf ipv6_devconf __read_mostly = {
199 .forwarding = 0,
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
202 .accept_ra = 1,
203 .accept_redirects = 1,
204 .autoconf = 1,
205 .force_mld_version = 0,
206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
208 .dad_transmits = 1,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
213 .use_tempaddr = 0,
214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
218 .max_addresses = IPV6_MAX_ADDRESSES,
219 .accept_ra_defrtr = 1,
220 .accept_ra_from_local = 0,
221 .accept_ra_min_hop_limit= 1,
222 .accept_ra_pinfo = 1,
223 #ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
225 .rtr_probe_interval = 60 * HZ,
226 #ifdef CONFIG_IPV6_ROUTE_INFO
227 .accept_ra_rt_info_max_plen = 0,
228 #endif
229 #endif
230 .proxy_ndp = 0,
231 .accept_source_route = 0, /* we do not accept RH0 by default. */
232 .disable_ipv6 = 0,
233 .accept_dad = 1,
234 .suppress_frag_ndisc = 1,
235 .accept_ra_mtu = 1,
236 .stable_secret = {
237 .initialized = false,
239 .use_oif_addrs_only = 0,
240 .ignore_routes_with_linkdown = 0,
241 .keep_addr_on_down = 0,
244 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
245 .forwarding = 0,
246 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
247 .mtu6 = IPV6_MIN_MTU,
248 .accept_ra = 1,
249 .accept_redirects = 1,
250 .autoconf = 1,
251 .force_mld_version = 0,
252 .mldv1_unsolicited_report_interval = 10 * HZ,
253 .mldv2_unsolicited_report_interval = HZ,
254 .dad_transmits = 1,
255 .rtr_solicits = MAX_RTR_SOLICITATIONS,
256 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
257 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
258 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
259 .use_tempaddr = 0,
260 .temp_valid_lft = TEMP_VALID_LIFETIME,
261 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
262 .regen_max_retry = REGEN_MAX_RETRY,
263 .max_desync_factor = MAX_DESYNC_FACTOR,
264 .max_addresses = IPV6_MAX_ADDRESSES,
265 .accept_ra_defrtr = 1,
266 .accept_ra_from_local = 0,
267 .accept_ra_min_hop_limit= 1,
268 .accept_ra_pinfo = 1,
269 #ifdef CONFIG_IPV6_ROUTER_PREF
270 .accept_ra_rtr_pref = 1,
271 .rtr_probe_interval = 60 * HZ,
272 #ifdef CONFIG_IPV6_ROUTE_INFO
273 .accept_ra_rt_info_max_plen = 0,
274 #endif
275 #endif
276 .proxy_ndp = 0,
277 .accept_source_route = 0, /* we do not accept RH0 by default. */
278 .disable_ipv6 = 0,
279 .accept_dad = 1,
280 .suppress_frag_ndisc = 1,
281 .accept_ra_mtu = 1,
282 .stable_secret = {
283 .initialized = false,
285 .use_oif_addrs_only = 0,
286 .ignore_routes_with_linkdown = 0,
287 .keep_addr_on_down = 0,
290 /* Check if a valid qdisc is available */
291 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
293 return !qdisc_tx_is_noop(dev);
296 static void addrconf_del_rs_timer(struct inet6_dev *idev)
298 if (del_timer(&idev->rs_timer))
299 __in6_dev_put(idev);
302 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
304 if (cancel_delayed_work(&ifp->dad_work))
305 __in6_ifa_put(ifp);
308 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
309 unsigned long when)
311 if (!timer_pending(&idev->rs_timer))
312 in6_dev_hold(idev);
313 mod_timer(&idev->rs_timer, jiffies + when);
316 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
317 unsigned long delay)
319 if (!delayed_work_pending(&ifp->dad_work))
320 in6_ifa_hold(ifp);
321 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
324 static int snmp6_alloc_dev(struct inet6_dev *idev)
326 int i;
328 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
329 if (!idev->stats.ipv6)
330 goto err_ip;
332 for_each_possible_cpu(i) {
333 struct ipstats_mib *addrconf_stats;
334 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
335 u64_stats_init(&addrconf_stats->syncp);
339 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
340 GFP_KERNEL);
341 if (!idev->stats.icmpv6dev)
342 goto err_icmp;
343 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
344 GFP_KERNEL);
345 if (!idev->stats.icmpv6msgdev)
346 goto err_icmpmsg;
348 return 0;
350 err_icmpmsg:
351 kfree(idev->stats.icmpv6dev);
352 err_icmp:
353 free_percpu(idev->stats.ipv6);
354 err_ip:
355 return -ENOMEM;
358 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
360 struct inet6_dev *ndev;
361 int err = -ENOMEM;
363 ASSERT_RTNL();
365 if (dev->mtu < IPV6_MIN_MTU)
366 return ERR_PTR(-EINVAL);
368 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
369 if (!ndev)
370 return ERR_PTR(err);
372 rwlock_init(&ndev->lock);
373 ndev->dev = dev;
374 INIT_LIST_HEAD(&ndev->addr_list);
375 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
376 (unsigned long)ndev);
377 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
379 if (ndev->cnf.stable_secret.initialized)
380 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
381 else
382 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
384 ndev->cnf.mtu6 = dev->mtu;
385 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
386 if (!ndev->nd_parms) {
387 kfree(ndev);
388 return ERR_PTR(err);
390 if (ndev->cnf.forwarding)
391 dev_disable_lro(dev);
392 /* We refer to the device */
393 dev_hold(dev);
395 if (snmp6_alloc_dev(ndev) < 0) {
396 ADBG(KERN_WARNING
397 "%s: cannot allocate memory for statistics; dev=%s.\n",
398 __func__, dev->name);
399 neigh_parms_release(&nd_tbl, ndev->nd_parms);
400 dev_put(dev);
401 kfree(ndev);
402 return ERR_PTR(err);
405 if (snmp6_register_dev(ndev) < 0) {
406 ADBG(KERN_WARNING
407 "%s: cannot create /proc/net/dev_snmp6/%s\n",
408 __func__, dev->name);
409 goto err_release;
412 /* One reference from device. We must do this before
413 * we invoke __ipv6_regen_rndid().
415 in6_dev_hold(ndev);
417 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
418 ndev->cnf.accept_dad = -1;
420 #if IS_ENABLED(CONFIG_IPV6_SIT)
421 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
422 pr_info("%s: Disabled Multicast RS\n", dev->name);
423 ndev->cnf.rtr_solicits = 0;
425 #endif
427 INIT_LIST_HEAD(&ndev->tempaddr_list);
428 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
429 if ((dev->flags&IFF_LOOPBACK) ||
430 dev->type == ARPHRD_TUNNEL ||
431 dev->type == ARPHRD_TUNNEL6 ||
432 dev->type == ARPHRD_SIT ||
433 dev->type == ARPHRD_NONE) {
434 ndev->cnf.use_tempaddr = -1;
435 } else {
436 in6_dev_hold(ndev);
437 ipv6_regen_rndid((unsigned long) ndev);
440 ndev->token = in6addr_any;
442 if (netif_running(dev) && addrconf_qdisc_ok(dev))
443 ndev->if_flags |= IF_READY;
445 ipv6_mc_init_dev(ndev);
446 ndev->tstamp = jiffies;
447 err = addrconf_sysctl_register(ndev);
448 if (err) {
449 ipv6_mc_destroy_dev(ndev);
450 del_timer(&ndev->regen_timer);
451 snmp6_unregister_dev(ndev);
452 goto err_release;
454 /* protected by rtnl_lock */
455 rcu_assign_pointer(dev->ip6_ptr, ndev);
457 /* Join interface-local all-node multicast group */
458 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
460 /* Join all-node multicast group */
461 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
463 /* Join all-router multicast group if forwarding is set */
464 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
465 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
467 return ndev;
469 err_release:
470 neigh_parms_release(&nd_tbl, ndev->nd_parms);
471 ndev->dead = 1;
472 in6_dev_finish_destroy(ndev);
473 return ERR_PTR(err);
476 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
478 struct inet6_dev *idev;
480 ASSERT_RTNL();
482 idev = __in6_dev_get(dev);
483 if (!idev) {
484 idev = ipv6_add_dev(dev);
485 if (IS_ERR(idev))
486 return NULL;
489 if (dev->flags&IFF_UP)
490 ipv6_mc_up(idev);
491 return idev;
494 static int inet6_netconf_msgsize_devconf(int type)
496 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
497 + nla_total_size(4); /* NETCONFA_IFINDEX */
498 bool all = false;
500 if (type == NETCONFA_ALL)
501 all = true;
503 if (all || type == NETCONFA_FORWARDING)
504 size += nla_total_size(4);
505 #ifdef CONFIG_IPV6_MROUTE
506 if (all || type == NETCONFA_MC_FORWARDING)
507 size += nla_total_size(4);
508 #endif
509 if (all || type == NETCONFA_PROXY_NEIGH)
510 size += nla_total_size(4);
512 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
513 size += nla_total_size(4);
515 return size;
518 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
519 struct ipv6_devconf *devconf, u32 portid,
520 u32 seq, int event, unsigned int flags,
521 int type)
523 struct nlmsghdr *nlh;
524 struct netconfmsg *ncm;
525 bool all = false;
527 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
528 flags);
529 if (!nlh)
530 return -EMSGSIZE;
532 if (type == NETCONFA_ALL)
533 all = true;
535 ncm = nlmsg_data(nlh);
536 ncm->ncm_family = AF_INET6;
538 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
539 goto nla_put_failure;
541 if ((all || type == NETCONFA_FORWARDING) &&
542 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
543 goto nla_put_failure;
544 #ifdef CONFIG_IPV6_MROUTE
545 if ((all || type == NETCONFA_MC_FORWARDING) &&
546 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
547 devconf->mc_forwarding) < 0)
548 goto nla_put_failure;
549 #endif
550 if ((all || type == NETCONFA_PROXY_NEIGH) &&
551 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
552 goto nla_put_failure;
554 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
555 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
556 devconf->ignore_routes_with_linkdown) < 0)
557 goto nla_put_failure;
559 nlmsg_end(skb, nlh);
560 return 0;
562 nla_put_failure:
563 nlmsg_cancel(skb, nlh);
564 return -EMSGSIZE;
567 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
568 struct ipv6_devconf *devconf)
570 struct sk_buff *skb;
571 int err = -ENOBUFS;
573 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
574 if (!skb)
575 goto errout;
577 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
578 RTM_NEWNETCONF, 0, type);
579 if (err < 0) {
580 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
581 WARN_ON(err == -EMSGSIZE);
582 kfree_skb(skb);
583 goto errout;
585 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
586 return;
587 errout:
588 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
591 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
592 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
593 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
594 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
595 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
598 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
599 struct nlmsghdr *nlh)
601 struct net *net = sock_net(in_skb->sk);
602 struct nlattr *tb[NETCONFA_MAX+1];
603 struct netconfmsg *ncm;
604 struct sk_buff *skb;
605 struct ipv6_devconf *devconf;
606 struct inet6_dev *in6_dev;
607 struct net_device *dev;
608 int ifindex;
609 int err;
611 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
612 devconf_ipv6_policy);
613 if (err < 0)
614 goto errout;
616 err = -EINVAL;
617 if (!tb[NETCONFA_IFINDEX])
618 goto errout;
620 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
621 switch (ifindex) {
622 case NETCONFA_IFINDEX_ALL:
623 devconf = net->ipv6.devconf_all;
624 break;
625 case NETCONFA_IFINDEX_DEFAULT:
626 devconf = net->ipv6.devconf_dflt;
627 break;
628 default:
629 dev = __dev_get_by_index(net, ifindex);
630 if (!dev)
631 goto errout;
632 in6_dev = __in6_dev_get(dev);
633 if (!in6_dev)
634 goto errout;
635 devconf = &in6_dev->cnf;
636 break;
639 err = -ENOBUFS;
640 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
641 if (!skb)
642 goto errout;
644 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
645 NETLINK_CB(in_skb).portid,
646 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
647 NETCONFA_ALL);
648 if (err < 0) {
649 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
650 WARN_ON(err == -EMSGSIZE);
651 kfree_skb(skb);
652 goto errout;
654 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
655 errout:
656 return err;
659 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
660 struct netlink_callback *cb)
662 struct net *net = sock_net(skb->sk);
663 int h, s_h;
664 int idx, s_idx;
665 struct net_device *dev;
666 struct inet6_dev *idev;
667 struct hlist_head *head;
669 s_h = cb->args[0];
670 s_idx = idx = cb->args[1];
672 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
673 idx = 0;
674 head = &net->dev_index_head[h];
675 rcu_read_lock();
676 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
677 net->dev_base_seq;
678 hlist_for_each_entry_rcu(dev, head, index_hlist) {
679 if (idx < s_idx)
680 goto cont;
681 idev = __in6_dev_get(dev);
682 if (!idev)
683 goto cont;
685 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
686 &idev->cnf,
687 NETLINK_CB(cb->skb).portid,
688 cb->nlh->nlmsg_seq,
689 RTM_NEWNETCONF,
690 NLM_F_MULTI,
691 NETCONFA_ALL) < 0) {
692 rcu_read_unlock();
693 goto done;
695 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
696 cont:
697 idx++;
699 rcu_read_unlock();
701 if (h == NETDEV_HASHENTRIES) {
702 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
703 net->ipv6.devconf_all,
704 NETLINK_CB(cb->skb).portid,
705 cb->nlh->nlmsg_seq,
706 RTM_NEWNETCONF, NLM_F_MULTI,
707 NETCONFA_ALL) < 0)
708 goto done;
709 else
710 h++;
712 if (h == NETDEV_HASHENTRIES + 1) {
713 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
714 net->ipv6.devconf_dflt,
715 NETLINK_CB(cb->skb).portid,
716 cb->nlh->nlmsg_seq,
717 RTM_NEWNETCONF, NLM_F_MULTI,
718 NETCONFA_ALL) < 0)
719 goto done;
720 else
721 h++;
723 done:
724 cb->args[0] = h;
725 cb->args[1] = idx;
727 return skb->len;
730 #ifdef CONFIG_SYSCTL
731 static void dev_forward_change(struct inet6_dev *idev)
733 struct net_device *dev;
734 struct inet6_ifaddr *ifa;
736 if (!idev)
737 return;
738 dev = idev->dev;
739 if (idev->cnf.forwarding)
740 dev_disable_lro(dev);
741 if (dev->flags & IFF_MULTICAST) {
742 if (idev->cnf.forwarding) {
743 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
744 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
745 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
746 } else {
747 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
748 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
749 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
753 list_for_each_entry(ifa, &idev->addr_list, if_list) {
754 if (ifa->flags&IFA_F_TENTATIVE)
755 continue;
756 if (idev->cnf.forwarding)
757 addrconf_join_anycast(ifa);
758 else
759 addrconf_leave_anycast(ifa);
761 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
762 dev->ifindex, &idev->cnf);
766 static void addrconf_forward_change(struct net *net, __s32 newf)
768 struct net_device *dev;
769 struct inet6_dev *idev;
771 for_each_netdev(net, dev) {
772 idev = __in6_dev_get(dev);
773 if (idev) {
774 int changed = (!idev->cnf.forwarding) ^ (!newf);
775 idev->cnf.forwarding = newf;
776 if (changed)
777 dev_forward_change(idev);
782 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
784 struct net *net;
785 int old;
787 if (!rtnl_trylock())
788 return restart_syscall();
790 net = (struct net *)table->extra2;
791 old = *p;
792 *p = newf;
794 if (p == &net->ipv6.devconf_dflt->forwarding) {
795 if ((!newf) ^ (!old))
796 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
797 NETCONFA_IFINDEX_DEFAULT,
798 net->ipv6.devconf_dflt);
799 rtnl_unlock();
800 return 0;
803 if (p == &net->ipv6.devconf_all->forwarding) {
804 int old_dflt = net->ipv6.devconf_dflt->forwarding;
806 net->ipv6.devconf_dflt->forwarding = newf;
807 if ((!newf) ^ (!old_dflt))
808 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
809 NETCONFA_IFINDEX_DEFAULT,
810 net->ipv6.devconf_dflt);
812 addrconf_forward_change(net, newf);
813 if ((!newf) ^ (!old))
814 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
815 NETCONFA_IFINDEX_ALL,
816 net->ipv6.devconf_all);
817 } else if ((!newf) ^ (!old))
818 dev_forward_change((struct inet6_dev *)table->extra1);
819 rtnl_unlock();
821 if (newf)
822 rt6_purge_dflt_routers(net);
823 return 1;
826 static void addrconf_linkdown_change(struct net *net, __s32 newf)
828 struct net_device *dev;
829 struct inet6_dev *idev;
831 for_each_netdev(net, dev) {
832 idev = __in6_dev_get(dev);
833 if (idev) {
834 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
836 idev->cnf.ignore_routes_with_linkdown = newf;
837 if (changed)
838 inet6_netconf_notify_devconf(dev_net(dev),
839 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
840 dev->ifindex,
841 &idev->cnf);
846 static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
848 struct net *net;
849 int old;
851 if (!rtnl_trylock())
852 return restart_syscall();
854 net = (struct net *)table->extra2;
855 old = *p;
856 *p = newf;
858 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
859 if ((!newf) ^ (!old))
860 inet6_netconf_notify_devconf(net,
861 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
862 NETCONFA_IFINDEX_DEFAULT,
863 net->ipv6.devconf_dflt);
864 rtnl_unlock();
865 return 0;
868 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
869 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
870 addrconf_linkdown_change(net, newf);
871 if ((!newf) ^ (!old))
872 inet6_netconf_notify_devconf(net,
873 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
874 NETCONFA_IFINDEX_ALL,
875 net->ipv6.devconf_all);
877 rtnl_unlock();
879 return 1;
882 #endif
884 /* Nobody refers to this ifaddr, destroy it */
885 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
887 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
889 #ifdef NET_REFCNT_DEBUG
890 pr_debug("%s\n", __func__);
891 #endif
893 in6_dev_put(ifp->idev);
895 if (cancel_delayed_work(&ifp->dad_work))
896 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
897 ifp);
899 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
900 pr_warn("Freeing alive inet6 address %p\n", ifp);
901 return;
903 ip6_rt_put(ifp->rt);
905 kfree_rcu(ifp, rcu);
908 static void
909 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
911 struct list_head *p;
912 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
915 * Each device address list is sorted in order of scope -
916 * global before linklocal.
918 list_for_each(p, &idev->addr_list) {
919 struct inet6_ifaddr *ifa
920 = list_entry(p, struct inet6_ifaddr, if_list);
921 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
922 break;
925 list_add_tail(&ifp->if_list, p);
928 static u32 inet6_addr_hash(const struct in6_addr *addr)
930 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
933 /* On success it returns ifp with increased reference count */
935 static struct inet6_ifaddr *
936 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
937 const struct in6_addr *peer_addr, int pfxlen,
938 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
940 struct inet6_ifaddr *ifa = NULL;
941 struct rt6_info *rt;
942 unsigned int hash;
943 int err = 0;
944 int addr_type = ipv6_addr_type(addr);
946 if (addr_type == IPV6_ADDR_ANY ||
947 addr_type & IPV6_ADDR_MULTICAST ||
948 (!(idev->dev->flags & IFF_LOOPBACK) &&
949 addr_type & IPV6_ADDR_LOOPBACK))
950 return ERR_PTR(-EADDRNOTAVAIL);
952 rcu_read_lock_bh();
953 if (idev->dead) {
954 err = -ENODEV; /*XXX*/
955 goto out2;
958 if (idev->cnf.disable_ipv6) {
959 err = -EACCES;
960 goto out2;
963 spin_lock(&addrconf_hash_lock);
965 /* Ignore adding duplicate addresses on an interface */
966 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
967 ADBG("ipv6_add_addr: already assigned\n");
968 err = -EEXIST;
969 goto out;
972 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
974 if (!ifa) {
975 ADBG("ipv6_add_addr: malloc failed\n");
976 err = -ENOBUFS;
977 goto out;
980 rt = addrconf_dst_alloc(idev, addr, false);
981 if (IS_ERR(rt)) {
982 err = PTR_ERR(rt);
983 goto out;
986 neigh_parms_data_state_setall(idev->nd_parms);
988 ifa->addr = *addr;
989 if (peer_addr)
990 ifa->peer_addr = *peer_addr;
992 spin_lock_init(&ifa->lock);
993 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
994 INIT_HLIST_NODE(&ifa->addr_lst);
995 ifa->scope = scope;
996 ifa->prefix_len = pfxlen;
997 ifa->flags = flags | IFA_F_TENTATIVE;
998 ifa->valid_lft = valid_lft;
999 ifa->prefered_lft = prefered_lft;
1000 ifa->cstamp = ifa->tstamp = jiffies;
1001 ifa->tokenized = false;
1003 ifa->rt = rt;
1005 ifa->idev = idev;
1006 in6_dev_hold(idev);
1007 /* For caller */
1008 in6_ifa_hold(ifa);
1010 /* Add to big hash table */
1011 hash = inet6_addr_hash(addr);
1013 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
1014 spin_unlock(&addrconf_hash_lock);
1016 write_lock(&idev->lock);
1017 /* Add to inet6_dev unicast addr list. */
1018 ipv6_link_dev_addr(idev, ifa);
1020 if (ifa->flags&IFA_F_TEMPORARY) {
1021 list_add(&ifa->tmp_list, &idev->tempaddr_list);
1022 in6_ifa_hold(ifa);
1025 in6_ifa_hold(ifa);
1026 write_unlock(&idev->lock);
1027 out2:
1028 rcu_read_unlock_bh();
1030 if (likely(err == 0))
1031 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1032 else {
1033 kfree(ifa);
1034 ifa = ERR_PTR(err);
1037 return ifa;
1038 out:
1039 spin_unlock(&addrconf_hash_lock);
1040 goto out2;
1043 enum cleanup_prefix_rt_t {
1044 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1045 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1046 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1050 * Check, whether the prefix for ifp would still need a prefix route
1051 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1052 * constants.
1054 * 1) we don't purge prefix if address was not permanent.
1055 * prefix is managed by its own lifetime.
1056 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1057 * 3) if there are no addresses, delete prefix.
1058 * 4) if there are still other permanent address(es),
1059 * corresponding prefix is still permanent.
1060 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1061 * don't purge the prefix, assume user space is managing it.
1062 * 6) otherwise, update prefix lifetime to the
1063 * longest valid lifetime among the corresponding
1064 * addresses on the device.
1065 * Note: subsequent RA will update lifetime.
1067 static enum cleanup_prefix_rt_t
1068 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1070 struct inet6_ifaddr *ifa;
1071 struct inet6_dev *idev = ifp->idev;
1072 unsigned long lifetime;
1073 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1075 *expires = jiffies;
1077 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1078 if (ifa == ifp)
1079 continue;
1080 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1081 ifp->prefix_len))
1082 continue;
1083 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1084 return CLEANUP_PREFIX_RT_NOP;
1086 action = CLEANUP_PREFIX_RT_EXPIRE;
1088 spin_lock(&ifa->lock);
1090 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1092 * Note: Because this address is
1093 * not permanent, lifetime <
1094 * LONG_MAX / HZ here.
1096 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1097 *expires = ifa->tstamp + lifetime * HZ;
1098 spin_unlock(&ifa->lock);
1101 return action;
1104 static void
1105 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1107 struct rt6_info *rt;
1109 rt = addrconf_get_prefix_route(&ifp->addr,
1110 ifp->prefix_len,
1111 ifp->idev->dev,
1112 0, RTF_GATEWAY | RTF_DEFAULT);
1113 if (rt) {
1114 if (del_rt)
1115 ip6_del_rt(rt);
1116 else {
1117 if (!(rt->rt6i_flags & RTF_EXPIRES))
1118 rt6_set_expires(rt, expires);
1119 ip6_rt_put(rt);
1125 /* This function wants to get referenced ifp and releases it before return */
1127 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1129 int state;
1130 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1131 unsigned long expires;
1133 ASSERT_RTNL();
1135 spin_lock_bh(&ifp->lock);
1136 state = ifp->state;
1137 ifp->state = INET6_IFADDR_STATE_DEAD;
1138 spin_unlock_bh(&ifp->lock);
1140 if (state == INET6_IFADDR_STATE_DEAD)
1141 goto out;
1143 spin_lock_bh(&addrconf_hash_lock);
1144 hlist_del_init_rcu(&ifp->addr_lst);
1145 spin_unlock_bh(&addrconf_hash_lock);
1147 write_lock_bh(&ifp->idev->lock);
1149 if (ifp->flags&IFA_F_TEMPORARY) {
1150 list_del(&ifp->tmp_list);
1151 if (ifp->ifpub) {
1152 in6_ifa_put(ifp->ifpub);
1153 ifp->ifpub = NULL;
1155 __in6_ifa_put(ifp);
1158 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1159 action = check_cleanup_prefix_route(ifp, &expires);
1161 list_del_init(&ifp->if_list);
1162 __in6_ifa_put(ifp);
1164 write_unlock_bh(&ifp->idev->lock);
1166 addrconf_del_dad_work(ifp);
1168 ipv6_ifa_notify(RTM_DELADDR, ifp);
1170 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1172 if (action != CLEANUP_PREFIX_RT_NOP) {
1173 cleanup_prefix_route(ifp, expires,
1174 action == CLEANUP_PREFIX_RT_DEL);
1177 /* clean up prefsrc entries */
1178 rt6_remove_prefsrc(ifp);
1179 out:
1180 in6_ifa_put(ifp);
1183 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1185 struct inet6_dev *idev = ifp->idev;
1186 struct in6_addr addr, *tmpaddr;
1187 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1188 unsigned long regen_advance;
1189 int tmp_plen;
1190 int ret = 0;
1191 u32 addr_flags;
1192 unsigned long now = jiffies;
1194 write_lock_bh(&idev->lock);
1195 if (ift) {
1196 spin_lock_bh(&ift->lock);
1197 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1198 spin_unlock_bh(&ift->lock);
1199 tmpaddr = &addr;
1200 } else {
1201 tmpaddr = NULL;
1203 retry:
1204 in6_dev_hold(idev);
1205 if (idev->cnf.use_tempaddr <= 0) {
1206 write_unlock_bh(&idev->lock);
1207 pr_info("%s: use_tempaddr is disabled\n", __func__);
1208 in6_dev_put(idev);
1209 ret = -1;
1210 goto out;
1212 spin_lock_bh(&ifp->lock);
1213 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1214 idev->cnf.use_tempaddr = -1; /*XXX*/
1215 spin_unlock_bh(&ifp->lock);
1216 write_unlock_bh(&idev->lock);
1217 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1218 __func__);
1219 in6_dev_put(idev);
1220 ret = -1;
1221 goto out;
1223 in6_ifa_hold(ifp);
1224 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1225 __ipv6_try_regen_rndid(idev, tmpaddr);
1226 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1227 age = (now - ifp->tstamp) / HZ;
1228 tmp_valid_lft = min_t(__u32,
1229 ifp->valid_lft,
1230 idev->cnf.temp_valid_lft + age);
1231 tmp_prefered_lft = min_t(__u32,
1232 ifp->prefered_lft,
1233 idev->cnf.temp_prefered_lft + age -
1234 idev->cnf.max_desync_factor);
1235 tmp_plen = ifp->prefix_len;
1236 tmp_tstamp = ifp->tstamp;
1237 spin_unlock_bh(&ifp->lock);
1239 regen_advance = idev->cnf.regen_max_retry *
1240 idev->cnf.dad_transmits *
1241 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1242 write_unlock_bh(&idev->lock);
1244 /* A temporary address is created only if this calculated Preferred
1245 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1246 * an implementation must not create a temporary address with a zero
1247 * Preferred Lifetime.
1248 * Use age calculation as in addrconf_verify to avoid unnecessary
1249 * temporary addresses being generated.
1251 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1252 if (tmp_prefered_lft <= regen_advance + age) {
1253 in6_ifa_put(ifp);
1254 in6_dev_put(idev);
1255 ret = -1;
1256 goto out;
1259 addr_flags = IFA_F_TEMPORARY;
1260 /* set in addrconf_prefix_rcv() */
1261 if (ifp->flags & IFA_F_OPTIMISTIC)
1262 addr_flags |= IFA_F_OPTIMISTIC;
1264 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1265 ipv6_addr_scope(&addr), addr_flags,
1266 tmp_valid_lft, tmp_prefered_lft);
1267 if (IS_ERR(ift)) {
1268 in6_ifa_put(ifp);
1269 in6_dev_put(idev);
1270 pr_info("%s: retry temporary address regeneration\n", __func__);
1271 tmpaddr = &addr;
1272 write_lock_bh(&idev->lock);
1273 goto retry;
1276 spin_lock_bh(&ift->lock);
1277 ift->ifpub = ifp;
1278 ift->cstamp = now;
1279 ift->tstamp = tmp_tstamp;
1280 spin_unlock_bh(&ift->lock);
1282 addrconf_dad_start(ift);
1283 in6_ifa_put(ift);
1284 in6_dev_put(idev);
1285 out:
1286 return ret;
1290 * Choose an appropriate source address (RFC3484)
1292 enum {
1293 IPV6_SADDR_RULE_INIT = 0,
1294 IPV6_SADDR_RULE_LOCAL,
1295 IPV6_SADDR_RULE_SCOPE,
1296 IPV6_SADDR_RULE_PREFERRED,
1297 #ifdef CONFIG_IPV6_MIP6
1298 IPV6_SADDR_RULE_HOA,
1299 #endif
1300 IPV6_SADDR_RULE_OIF,
1301 IPV6_SADDR_RULE_LABEL,
1302 IPV6_SADDR_RULE_PRIVACY,
1303 IPV6_SADDR_RULE_ORCHID,
1304 IPV6_SADDR_RULE_PREFIX,
1305 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1306 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1307 #endif
1308 IPV6_SADDR_RULE_MAX
1311 struct ipv6_saddr_score {
1312 int rule;
1313 int addr_type;
1314 struct inet6_ifaddr *ifa;
1315 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1316 int scopedist;
1317 int matchlen;
1320 struct ipv6_saddr_dst {
1321 const struct in6_addr *addr;
1322 int ifindex;
1323 int scope;
1324 int label;
1325 unsigned int prefs;
1328 static inline int ipv6_saddr_preferred(int type)
1330 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1331 return 1;
1332 return 0;
1335 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1337 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1338 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1339 #else
1340 return false;
1341 #endif
1344 static int ipv6_get_saddr_eval(struct net *net,
1345 struct ipv6_saddr_score *score,
1346 struct ipv6_saddr_dst *dst,
1347 int i)
1349 int ret;
1351 if (i <= score->rule) {
1352 switch (i) {
1353 case IPV6_SADDR_RULE_SCOPE:
1354 ret = score->scopedist;
1355 break;
1356 case IPV6_SADDR_RULE_PREFIX:
1357 ret = score->matchlen;
1358 break;
1359 default:
1360 ret = !!test_bit(i, score->scorebits);
1362 goto out;
1365 switch (i) {
1366 case IPV6_SADDR_RULE_INIT:
1367 /* Rule 0: remember if hiscore is not ready yet */
1368 ret = !!score->ifa;
1369 break;
1370 case IPV6_SADDR_RULE_LOCAL:
1371 /* Rule 1: Prefer same address */
1372 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1373 break;
1374 case IPV6_SADDR_RULE_SCOPE:
1375 /* Rule 2: Prefer appropriate scope
1377 * ret
1379 * -1 | d 15
1380 * ---+--+-+---> scope
1382 * | d is scope of the destination.
1383 * B-d | \
1384 * | \ <- smaller scope is better if
1385 * B-15 | \ if scope is enough for destination.
1386 * | ret = B - scope (-1 <= scope >= d <= 15).
1387 * d-C-1 | /
1388 * |/ <- greater is better
1389 * -C / if scope is not enough for destination.
1390 * /| ret = scope - C (-1 <= d < scope <= 15).
1392 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1393 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1394 * Assume B = 0 and we get C > 29.
1396 ret = __ipv6_addr_src_scope(score->addr_type);
1397 if (ret >= dst->scope)
1398 ret = -ret;
1399 else
1400 ret -= 128; /* 30 is enough */
1401 score->scopedist = ret;
1402 break;
1403 case IPV6_SADDR_RULE_PREFERRED:
1405 /* Rule 3: Avoid deprecated and optimistic addresses */
1406 u8 avoid = IFA_F_DEPRECATED;
1408 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1409 avoid |= IFA_F_OPTIMISTIC;
1410 ret = ipv6_saddr_preferred(score->addr_type) ||
1411 !(score->ifa->flags & avoid);
1412 break;
1414 #ifdef CONFIG_IPV6_MIP6
1415 case IPV6_SADDR_RULE_HOA:
1417 /* Rule 4: Prefer home address */
1418 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1419 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1420 break;
1422 #endif
1423 case IPV6_SADDR_RULE_OIF:
1424 /* Rule 5: Prefer outgoing interface */
1425 ret = (!dst->ifindex ||
1426 dst->ifindex == score->ifa->idev->dev->ifindex);
1427 break;
1428 case IPV6_SADDR_RULE_LABEL:
1429 /* Rule 6: Prefer matching label */
1430 ret = ipv6_addr_label(net,
1431 &score->ifa->addr, score->addr_type,
1432 score->ifa->idev->dev->ifindex) == dst->label;
1433 break;
1434 case IPV6_SADDR_RULE_PRIVACY:
1436 /* Rule 7: Prefer public address
1437 * Note: prefer temporary address if use_tempaddr >= 2
1439 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1440 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1441 score->ifa->idev->cnf.use_tempaddr >= 2;
1442 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1443 break;
1445 case IPV6_SADDR_RULE_ORCHID:
1446 /* Rule 8-: Prefer ORCHID vs ORCHID or
1447 * non-ORCHID vs non-ORCHID
1449 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1450 ipv6_addr_orchid(dst->addr));
1451 break;
1452 case IPV6_SADDR_RULE_PREFIX:
1453 /* Rule 8: Use longest matching prefix */
1454 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1455 if (ret > score->ifa->prefix_len)
1456 ret = score->ifa->prefix_len;
1457 score->matchlen = ret;
1458 break;
1459 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1460 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1461 /* Optimistic addresses still have lower precedence than other
1462 * preferred addresses.
1464 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1465 break;
1466 #endif
1467 default:
1468 ret = 0;
1471 if (ret)
1472 __set_bit(i, score->scorebits);
1473 score->rule = i;
1474 out:
1475 return ret;
1478 static int __ipv6_dev_get_saddr(struct net *net,
1479 struct ipv6_saddr_dst *dst,
1480 struct inet6_dev *idev,
1481 struct ipv6_saddr_score *scores,
1482 int hiscore_idx)
1484 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1486 read_lock_bh(&idev->lock);
1487 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1488 int i;
1491 * - Tentative Address (RFC2462 section 5.4)
1492 * - A tentative address is not considered
1493 * "assigned to an interface" in the traditional
1494 * sense, unless it is also flagged as optimistic.
1495 * - Candidate Source Address (section 4)
1496 * - In any case, anycast addresses, multicast
1497 * addresses, and the unspecified address MUST
1498 * NOT be included in a candidate set.
1500 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1501 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1502 continue;
1504 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1506 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1507 score->addr_type & IPV6_ADDR_MULTICAST)) {
1508 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1509 idev->dev->name);
1510 continue;
1513 score->rule = -1;
1514 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1516 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1517 int minihiscore, miniscore;
1519 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1520 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1522 if (minihiscore > miniscore) {
1523 if (i == IPV6_SADDR_RULE_SCOPE &&
1524 score->scopedist > 0) {
1526 * special case:
1527 * each remaining entry
1528 * has too small (not enough)
1529 * scope, because ifa entries
1530 * are sorted by their scope
1531 * values.
1533 goto out;
1535 break;
1536 } else if (minihiscore < miniscore) {
1537 if (hiscore->ifa)
1538 in6_ifa_put(hiscore->ifa);
1540 in6_ifa_hold(score->ifa);
1542 swap(hiscore, score);
1543 hiscore_idx = 1 - hiscore_idx;
1545 /* restore our iterator */
1546 score->ifa = hiscore->ifa;
1548 break;
1552 out:
1553 read_unlock_bh(&idev->lock);
1554 return hiscore_idx;
1557 static int ipv6_get_saddr_master(struct net *net,
1558 const struct net_device *dst_dev,
1559 const struct net_device *master,
1560 struct ipv6_saddr_dst *dst,
1561 struct ipv6_saddr_score *scores,
1562 int hiscore_idx)
1564 struct inet6_dev *idev;
1566 idev = __in6_dev_get(dst_dev);
1567 if (idev)
1568 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1569 scores, hiscore_idx);
1571 idev = __in6_dev_get(master);
1572 if (idev)
1573 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1574 scores, hiscore_idx);
1576 return hiscore_idx;
1579 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1580 const struct in6_addr *daddr, unsigned int prefs,
1581 struct in6_addr *saddr)
1583 struct ipv6_saddr_score scores[2], *hiscore;
1584 struct ipv6_saddr_dst dst;
1585 struct inet6_dev *idev;
1586 struct net_device *dev;
1587 int dst_type;
1588 bool use_oif_addr = false;
1589 int hiscore_idx = 0;
1591 dst_type = __ipv6_addr_type(daddr);
1592 dst.addr = daddr;
1593 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1594 dst.scope = __ipv6_addr_src_scope(dst_type);
1595 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1596 dst.prefs = prefs;
1598 scores[hiscore_idx].rule = -1;
1599 scores[hiscore_idx].ifa = NULL;
1601 rcu_read_lock();
1603 /* Candidate Source Address (section 4)
1604 * - multicast and link-local destination address,
1605 * the set of candidate source address MUST only
1606 * include addresses assigned to interfaces
1607 * belonging to the same link as the outgoing
1608 * interface.
1609 * (- For site-local destination addresses, the
1610 * set of candidate source addresses MUST only
1611 * include addresses assigned to interfaces
1612 * belonging to the same site as the outgoing
1613 * interface.)
1614 * - "It is RECOMMENDED that the candidate source addresses
1615 * be the set of unicast addresses assigned to the
1616 * interface that will be used to send to the destination
1617 * (the 'outgoing' interface)." (RFC 6724)
1619 if (dst_dev) {
1620 idev = __in6_dev_get(dst_dev);
1621 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1622 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1623 (idev && idev->cnf.use_oif_addrs_only)) {
1624 use_oif_addr = true;
1628 if (use_oif_addr) {
1629 if (idev)
1630 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1631 } else {
1632 const struct net_device *master;
1633 int master_idx = 0;
1635 /* if dst_dev exists and is enslaved to an L3 device, then
1636 * prefer addresses from dst_dev and then the master over
1637 * any other enslaved devices in the L3 domain.
1639 master = l3mdev_master_dev_rcu(dst_dev);
1640 if (master) {
1641 master_idx = master->ifindex;
1643 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1644 master, &dst,
1645 scores, hiscore_idx);
1647 if (scores[hiscore_idx].ifa)
1648 goto out;
1651 for_each_netdev_rcu(net, dev) {
1652 /* only consider addresses on devices in the
1653 * same L3 domain
1655 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1656 continue;
1657 idev = __in6_dev_get(dev);
1658 if (!idev)
1659 continue;
1660 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1664 out:
1665 rcu_read_unlock();
1667 hiscore = &scores[hiscore_idx];
1668 if (!hiscore->ifa)
1669 return -EADDRNOTAVAIL;
1671 *saddr = hiscore->ifa->addr;
1672 in6_ifa_put(hiscore->ifa);
1673 return 0;
1675 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1677 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1678 u32 banned_flags)
1680 struct inet6_ifaddr *ifp;
1681 int err = -EADDRNOTAVAIL;
1683 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1684 if (ifp->scope > IFA_LINK)
1685 break;
1686 if (ifp->scope == IFA_LINK &&
1687 !(ifp->flags & banned_flags)) {
1688 *addr = ifp->addr;
1689 err = 0;
1690 break;
1693 return err;
1696 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1697 u32 banned_flags)
1699 struct inet6_dev *idev;
1700 int err = -EADDRNOTAVAIL;
1702 rcu_read_lock();
1703 idev = __in6_dev_get(dev);
1704 if (idev) {
1705 read_lock_bh(&idev->lock);
1706 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1707 read_unlock_bh(&idev->lock);
1709 rcu_read_unlock();
1710 return err;
1713 static int ipv6_count_addresses(struct inet6_dev *idev)
1715 int cnt = 0;
1716 struct inet6_ifaddr *ifp;
1718 read_lock_bh(&idev->lock);
1719 list_for_each_entry(ifp, &idev->addr_list, if_list)
1720 cnt++;
1721 read_unlock_bh(&idev->lock);
1722 return cnt;
1725 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1726 const struct net_device *dev, int strict)
1728 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1730 EXPORT_SYMBOL(ipv6_chk_addr);
1732 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1733 const struct net_device *dev, int strict,
1734 u32 banned_flags)
1736 struct inet6_ifaddr *ifp;
1737 unsigned int hash = inet6_addr_hash(addr);
1738 u32 ifp_flags;
1740 rcu_read_lock_bh();
1741 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1742 if (!net_eq(dev_net(ifp->idev->dev), net))
1743 continue;
1744 /* Decouple optimistic from tentative for evaluation here.
1745 * Ban optimistic addresses explicitly, when required.
1747 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1748 ? (ifp->flags&~IFA_F_TENTATIVE)
1749 : ifp->flags;
1750 if (ipv6_addr_equal(&ifp->addr, addr) &&
1751 !(ifp_flags&banned_flags) &&
1752 (!dev || ifp->idev->dev == dev ||
1753 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1754 rcu_read_unlock_bh();
1755 return 1;
1759 rcu_read_unlock_bh();
1760 return 0;
1762 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1764 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1765 struct net_device *dev)
1767 unsigned int hash = inet6_addr_hash(addr);
1768 struct inet6_ifaddr *ifp;
1770 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1771 if (!net_eq(dev_net(ifp->idev->dev), net))
1772 continue;
1773 if (ipv6_addr_equal(&ifp->addr, addr)) {
1774 if (!dev || ifp->idev->dev == dev)
1775 return true;
1778 return false;
1781 /* Compares an address/prefix_len with addresses on device @dev.
1782 * If one is found it returns true.
1784 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1785 const unsigned int prefix_len, struct net_device *dev)
1787 struct inet6_dev *idev;
1788 struct inet6_ifaddr *ifa;
1789 bool ret = false;
1791 rcu_read_lock();
1792 idev = __in6_dev_get(dev);
1793 if (idev) {
1794 read_lock_bh(&idev->lock);
1795 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1796 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1797 if (ret)
1798 break;
1800 read_unlock_bh(&idev->lock);
1802 rcu_read_unlock();
1804 return ret;
1806 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1808 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1810 struct inet6_dev *idev;
1811 struct inet6_ifaddr *ifa;
1812 int onlink;
1814 onlink = 0;
1815 rcu_read_lock();
1816 idev = __in6_dev_get(dev);
1817 if (idev) {
1818 read_lock_bh(&idev->lock);
1819 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1820 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1821 ifa->prefix_len);
1822 if (onlink)
1823 break;
1825 read_unlock_bh(&idev->lock);
1827 rcu_read_unlock();
1828 return onlink;
1830 EXPORT_SYMBOL(ipv6_chk_prefix);
1832 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1833 struct net_device *dev, int strict)
1835 struct inet6_ifaddr *ifp, *result = NULL;
1836 unsigned int hash = inet6_addr_hash(addr);
1838 rcu_read_lock_bh();
1839 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1840 if (!net_eq(dev_net(ifp->idev->dev), net))
1841 continue;
1842 if (ipv6_addr_equal(&ifp->addr, addr)) {
1843 if (!dev || ifp->idev->dev == dev ||
1844 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1845 result = ifp;
1846 in6_ifa_hold(ifp);
1847 break;
1851 rcu_read_unlock_bh();
1853 return result;
1856 /* Gets referenced address, destroys ifaddr */
1858 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1860 if (dad_failed)
1861 ifp->flags |= IFA_F_DADFAILED;
1863 if (ifp->flags&IFA_F_PERMANENT) {
1864 spin_lock_bh(&ifp->lock);
1865 addrconf_del_dad_work(ifp);
1866 ifp->flags |= IFA_F_TENTATIVE;
1867 spin_unlock_bh(&ifp->lock);
1868 if (dad_failed)
1869 ipv6_ifa_notify(0, ifp);
1870 in6_ifa_put(ifp);
1871 } else if (ifp->flags&IFA_F_TEMPORARY) {
1872 struct inet6_ifaddr *ifpub;
1873 spin_lock_bh(&ifp->lock);
1874 ifpub = ifp->ifpub;
1875 if (ifpub) {
1876 in6_ifa_hold(ifpub);
1877 spin_unlock_bh(&ifp->lock);
1878 ipv6_create_tempaddr(ifpub, ifp);
1879 in6_ifa_put(ifpub);
1880 } else {
1881 spin_unlock_bh(&ifp->lock);
1883 ipv6_del_addr(ifp);
1884 } else {
1885 ipv6_del_addr(ifp);
1889 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1891 int err = -ENOENT;
1893 spin_lock_bh(&ifp->lock);
1894 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1895 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1896 err = 0;
1898 spin_unlock_bh(&ifp->lock);
1900 return err;
1903 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1905 struct inet6_dev *idev = ifp->idev;
1906 struct net *net = dev_net(ifp->idev->dev);
1908 if (addrconf_dad_end(ifp)) {
1909 in6_ifa_put(ifp);
1910 return;
1913 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1914 ifp->idev->dev->name, &ifp->addr);
1916 spin_lock_bh(&ifp->lock);
1918 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1919 int scope = ifp->scope;
1920 u32 flags = ifp->flags;
1921 struct in6_addr new_addr;
1922 struct inet6_ifaddr *ifp2;
1923 u32 valid_lft, preferred_lft;
1924 int pfxlen = ifp->prefix_len;
1925 int retries = ifp->stable_privacy_retry + 1;
1927 if (retries > net->ipv6.sysctl.idgen_retries) {
1928 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1929 ifp->idev->dev->name);
1930 goto errdad;
1933 new_addr = ifp->addr;
1934 if (ipv6_generate_stable_address(&new_addr, retries,
1935 idev))
1936 goto errdad;
1938 valid_lft = ifp->valid_lft;
1939 preferred_lft = ifp->prefered_lft;
1941 spin_unlock_bh(&ifp->lock);
1943 if (idev->cnf.max_addresses &&
1944 ipv6_count_addresses(idev) >=
1945 idev->cnf.max_addresses)
1946 goto lock_errdad;
1948 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1949 ifp->idev->dev->name);
1951 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1952 scope, flags, valid_lft,
1953 preferred_lft);
1954 if (IS_ERR(ifp2))
1955 goto lock_errdad;
1957 spin_lock_bh(&ifp2->lock);
1958 ifp2->stable_privacy_retry = retries;
1959 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1960 spin_unlock_bh(&ifp2->lock);
1962 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1963 in6_ifa_put(ifp2);
1964 lock_errdad:
1965 spin_lock_bh(&ifp->lock);
1968 errdad:
1969 /* transition from _POSTDAD to _ERRDAD */
1970 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1971 spin_unlock_bh(&ifp->lock);
1973 addrconf_mod_dad_work(ifp, 0);
1974 in6_ifa_put(ifp);
1977 /* Join to solicited addr multicast group.
1978 * caller must hold RTNL */
1979 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1981 struct in6_addr maddr;
1983 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1984 return;
1986 addrconf_addr_solict_mult(addr, &maddr);
1987 ipv6_dev_mc_inc(dev, &maddr);
1990 /* caller must hold RTNL */
1991 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1993 struct in6_addr maddr;
1995 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1996 return;
1998 addrconf_addr_solict_mult(addr, &maddr);
1999 __ipv6_dev_mc_dec(idev, &maddr);
2002 /* caller must hold RTNL */
2003 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2005 struct in6_addr addr;
2007 if (ifp->prefix_len >= 127) /* RFC 6164 */
2008 return;
2009 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2010 if (ipv6_addr_any(&addr))
2011 return;
2012 __ipv6_dev_ac_inc(ifp->idev, &addr);
2015 /* caller must hold RTNL */
2016 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2018 struct in6_addr addr;
2020 if (ifp->prefix_len >= 127) /* RFC 6164 */
2021 return;
2022 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2023 if (ipv6_addr_any(&addr))
2024 return;
2025 __ipv6_dev_ac_dec(ifp->idev, &addr);
2028 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2030 if (dev->addr_len != EUI64_ADDR_LEN)
2031 return -1;
2032 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2033 eui[0] ^= 2;
2034 return 0;
2037 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2039 union fwnet_hwaddr *ha;
2041 if (dev->addr_len != FWNET_ALEN)
2042 return -1;
2044 ha = (union fwnet_hwaddr *)dev->dev_addr;
2046 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2047 eui[0] ^= 2;
2048 return 0;
2051 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2053 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2054 if (dev->addr_len != ARCNET_ALEN)
2055 return -1;
2056 memset(eui, 0, 7);
2057 eui[7] = *(u8 *)dev->dev_addr;
2058 return 0;
2061 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2063 if (dev->addr_len != INFINIBAND_ALEN)
2064 return -1;
2065 memcpy(eui, dev->dev_addr + 12, 8);
2066 eui[0] |= 2;
2067 return 0;
2070 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2072 if (addr == 0)
2073 return -1;
2074 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2075 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2076 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2077 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2078 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2079 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2080 eui[1] = 0;
2081 eui[2] = 0x5E;
2082 eui[3] = 0xFE;
2083 memcpy(eui + 4, &addr, 4);
2084 return 0;
2087 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2089 if (dev->priv_flags & IFF_ISATAP)
2090 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2091 return -1;
2094 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2096 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2099 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2101 memcpy(eui, dev->perm_addr, 3);
2102 memcpy(eui + 5, dev->perm_addr + 3, 3);
2103 eui[3] = 0xFF;
2104 eui[4] = 0xFE;
2105 eui[0] ^= 2;
2106 return 0;
2109 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2111 switch (dev->type) {
2112 case ARPHRD_ETHER:
2113 case ARPHRD_FDDI:
2114 return addrconf_ifid_eui48(eui, dev);
2115 case ARPHRD_ARCNET:
2116 return addrconf_ifid_arcnet(eui, dev);
2117 case ARPHRD_INFINIBAND:
2118 return addrconf_ifid_infiniband(eui, dev);
2119 case ARPHRD_SIT:
2120 return addrconf_ifid_sit(eui, dev);
2121 case ARPHRD_IPGRE:
2122 return addrconf_ifid_gre(eui, dev);
2123 case ARPHRD_6LOWPAN:
2124 return addrconf_ifid_eui64(eui, dev);
2125 case ARPHRD_IEEE1394:
2126 return addrconf_ifid_ieee1394(eui, dev);
2127 case ARPHRD_TUNNEL6:
2128 return addrconf_ifid_ip6tnl(eui, dev);
2130 return -1;
2133 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2135 int err = -1;
2136 struct inet6_ifaddr *ifp;
2138 read_lock_bh(&idev->lock);
2139 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2140 if (ifp->scope > IFA_LINK)
2141 break;
2142 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2143 memcpy(eui, ifp->addr.s6_addr+8, 8);
2144 err = 0;
2145 break;
2148 read_unlock_bh(&idev->lock);
2149 return err;
2152 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2153 static void __ipv6_regen_rndid(struct inet6_dev *idev)
2155 regen:
2156 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2157 idev->rndid[0] &= ~0x02;
2160 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2161 * check if generated address is not inappropriate
2163 * - Reserved subnet anycast (RFC 2526)
2164 * 11111101 11....11 1xxxxxxx
2165 * - ISATAP (RFC4214) 6.1
2166 * 00-00-5E-FE-xx-xx-xx-xx
2167 * - value 0
2168 * - XXX: already assigned to an address on the device
2170 if (idev->rndid[0] == 0xfd &&
2171 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2172 (idev->rndid[7]&0x80))
2173 goto regen;
2174 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2175 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2176 goto regen;
2177 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2178 goto regen;
2182 static void ipv6_regen_rndid(unsigned long data)
2184 struct inet6_dev *idev = (struct inet6_dev *) data;
2185 unsigned long expires;
2187 rcu_read_lock_bh();
2188 write_lock_bh(&idev->lock);
2190 if (idev->dead)
2191 goto out;
2193 __ipv6_regen_rndid(idev);
2195 expires = jiffies +
2196 idev->cnf.temp_prefered_lft * HZ -
2197 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2198 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2199 idev->cnf.max_desync_factor * HZ;
2200 if (time_before(expires, jiffies)) {
2201 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2202 __func__, idev->dev->name);
2203 goto out;
2206 if (!mod_timer(&idev->regen_timer, expires))
2207 in6_dev_hold(idev);
2209 out:
2210 write_unlock_bh(&idev->lock);
2211 rcu_read_unlock_bh();
2212 in6_dev_put(idev);
2215 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2217 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2218 __ipv6_regen_rndid(idev);
2222 * Add prefix route.
2225 static void
2226 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2227 unsigned long expires, u32 flags)
2229 struct fib6_config cfg = {
2230 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2231 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2232 .fc_ifindex = dev->ifindex,
2233 .fc_expires = expires,
2234 .fc_dst_len = plen,
2235 .fc_flags = RTF_UP | flags,
2236 .fc_nlinfo.nl_net = dev_net(dev),
2237 .fc_protocol = RTPROT_KERNEL,
2240 cfg.fc_dst = *pfx;
2242 /* Prevent useless cloning on PtP SIT.
2243 This thing is done here expecting that the whole
2244 class of non-broadcast devices need not cloning.
2246 #if IS_ENABLED(CONFIG_IPV6_SIT)
2247 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2248 cfg.fc_flags |= RTF_NONEXTHOP;
2249 #endif
2251 ip6_route_add(&cfg);
2255 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2256 int plen,
2257 const struct net_device *dev,
2258 u32 flags, u32 noflags)
2260 struct fib6_node *fn;
2261 struct rt6_info *rt = NULL;
2262 struct fib6_table *table;
2263 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2265 table = fib6_get_table(dev_net(dev), tb_id);
2266 if (!table)
2267 return NULL;
2269 read_lock_bh(&table->tb6_lock);
2270 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2271 if (!fn)
2272 goto out;
2274 noflags |= RTF_CACHE;
2275 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2276 if (rt->dst.dev->ifindex != dev->ifindex)
2277 continue;
2278 if ((rt->rt6i_flags & flags) != flags)
2279 continue;
2280 if ((rt->rt6i_flags & noflags) != 0)
2281 continue;
2282 dst_hold(&rt->dst);
2283 break;
2285 out:
2286 read_unlock_bh(&table->tb6_lock);
2287 return rt;
2291 /* Create "default" multicast route to the interface */
2293 static void addrconf_add_mroute(struct net_device *dev)
2295 struct fib6_config cfg = {
2296 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2297 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2298 .fc_ifindex = dev->ifindex,
2299 .fc_dst_len = 8,
2300 .fc_flags = RTF_UP,
2301 .fc_nlinfo.nl_net = dev_net(dev),
2304 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2306 ip6_route_add(&cfg);
2309 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2311 struct inet6_dev *idev;
2313 ASSERT_RTNL();
2315 idev = ipv6_find_idev(dev);
2316 if (!idev)
2317 return ERR_PTR(-ENOBUFS);
2319 if (idev->cnf.disable_ipv6)
2320 return ERR_PTR(-EACCES);
2322 /* Add default multicast route */
2323 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2324 addrconf_add_mroute(dev);
2326 return idev;
2329 static void manage_tempaddrs(struct inet6_dev *idev,
2330 struct inet6_ifaddr *ifp,
2331 __u32 valid_lft, __u32 prefered_lft,
2332 bool create, unsigned long now)
2334 u32 flags;
2335 struct inet6_ifaddr *ift;
2337 read_lock_bh(&idev->lock);
2338 /* update all temporary addresses in the list */
2339 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2340 int age, max_valid, max_prefered;
2342 if (ifp != ift->ifpub)
2343 continue;
2345 /* RFC 4941 section 3.3:
2346 * If a received option will extend the lifetime of a public
2347 * address, the lifetimes of temporary addresses should
2348 * be extended, subject to the overall constraint that no
2349 * temporary addresses should ever remain "valid" or "preferred"
2350 * for a time longer than (TEMP_VALID_LIFETIME) or
2351 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2353 age = (now - ift->cstamp) / HZ;
2354 max_valid = idev->cnf.temp_valid_lft - age;
2355 if (max_valid < 0)
2356 max_valid = 0;
2358 max_prefered = idev->cnf.temp_prefered_lft -
2359 idev->cnf.max_desync_factor - age;
2360 if (max_prefered < 0)
2361 max_prefered = 0;
2363 if (valid_lft > max_valid)
2364 valid_lft = max_valid;
2366 if (prefered_lft > max_prefered)
2367 prefered_lft = max_prefered;
2369 spin_lock(&ift->lock);
2370 flags = ift->flags;
2371 ift->valid_lft = valid_lft;
2372 ift->prefered_lft = prefered_lft;
2373 ift->tstamp = now;
2374 if (prefered_lft > 0)
2375 ift->flags &= ~IFA_F_DEPRECATED;
2377 spin_unlock(&ift->lock);
2378 if (!(flags&IFA_F_TENTATIVE))
2379 ipv6_ifa_notify(0, ift);
2382 if ((create || list_empty(&idev->tempaddr_list)) &&
2383 idev->cnf.use_tempaddr > 0) {
2384 /* When a new public address is created as described
2385 * in [ADDRCONF], also create a new temporary address.
2386 * Also create a temporary address if it's enabled but
2387 * no temporary address currently exists.
2389 read_unlock_bh(&idev->lock);
2390 ipv6_create_tempaddr(ifp, NULL);
2391 } else {
2392 read_unlock_bh(&idev->lock);
2396 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2398 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2399 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2402 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2403 const struct prefix_info *pinfo,
2404 struct inet6_dev *in6_dev,
2405 const struct in6_addr *addr, int addr_type,
2406 u32 addr_flags, bool sllao, bool tokenized,
2407 __u32 valid_lft, u32 prefered_lft)
2409 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2410 int create = 0, update_lft = 0;
2412 if (!ifp && valid_lft) {
2413 int max_addresses = in6_dev->cnf.max_addresses;
2415 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2416 if (in6_dev->cnf.optimistic_dad &&
2417 !net->ipv6.devconf_all->forwarding && sllao)
2418 addr_flags |= IFA_F_OPTIMISTIC;
2419 #endif
2421 /* Do not allow to create too much of autoconfigured
2422 * addresses; this would be too easy way to crash kernel.
2424 if (!max_addresses ||
2425 ipv6_count_addresses(in6_dev) < max_addresses)
2426 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2427 pinfo->prefix_len,
2428 addr_type&IPV6_ADDR_SCOPE_MASK,
2429 addr_flags, valid_lft,
2430 prefered_lft);
2432 if (IS_ERR_OR_NULL(ifp))
2433 return -1;
2435 update_lft = 0;
2436 create = 1;
2437 spin_lock_bh(&ifp->lock);
2438 ifp->flags |= IFA_F_MANAGETEMPADDR;
2439 ifp->cstamp = jiffies;
2440 ifp->tokenized = tokenized;
2441 spin_unlock_bh(&ifp->lock);
2442 addrconf_dad_start(ifp);
2445 if (ifp) {
2446 u32 flags;
2447 unsigned long now;
2448 u32 stored_lft;
2450 /* update lifetime (RFC2462 5.5.3 e) */
2451 spin_lock_bh(&ifp->lock);
2452 now = jiffies;
2453 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2454 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2455 else
2456 stored_lft = 0;
2457 if (!update_lft && !create && stored_lft) {
2458 const u32 minimum_lft = min_t(u32,
2459 stored_lft, MIN_VALID_LIFETIME);
2460 valid_lft = max(valid_lft, minimum_lft);
2462 /* RFC4862 Section 5.5.3e:
2463 * "Note that the preferred lifetime of the
2464 * corresponding address is always reset to
2465 * the Preferred Lifetime in the received
2466 * Prefix Information option, regardless of
2467 * whether the valid lifetime is also reset or
2468 * ignored."
2470 * So we should always update prefered_lft here.
2472 update_lft = 1;
2475 if (update_lft) {
2476 ifp->valid_lft = valid_lft;
2477 ifp->prefered_lft = prefered_lft;
2478 ifp->tstamp = now;
2479 flags = ifp->flags;
2480 ifp->flags &= ~IFA_F_DEPRECATED;
2481 spin_unlock_bh(&ifp->lock);
2483 if (!(flags&IFA_F_TENTATIVE))
2484 ipv6_ifa_notify(0, ifp);
2485 } else
2486 spin_unlock_bh(&ifp->lock);
2488 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2489 create, now);
2491 in6_ifa_put(ifp);
2492 addrconf_verify();
2495 return 0;
2497 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2499 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2501 struct prefix_info *pinfo;
2502 __u32 valid_lft;
2503 __u32 prefered_lft;
2504 int addr_type, err;
2505 u32 addr_flags = 0;
2506 struct inet6_dev *in6_dev;
2507 struct net *net = dev_net(dev);
2509 pinfo = (struct prefix_info *) opt;
2511 if (len < sizeof(struct prefix_info)) {
2512 ADBG("addrconf: prefix option too short\n");
2513 return;
2517 * Validation checks ([ADDRCONF], page 19)
2520 addr_type = ipv6_addr_type(&pinfo->prefix);
2522 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2523 return;
2525 valid_lft = ntohl(pinfo->valid);
2526 prefered_lft = ntohl(pinfo->prefered);
2528 if (prefered_lft > valid_lft) {
2529 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2530 return;
2533 in6_dev = in6_dev_get(dev);
2535 if (!in6_dev) {
2536 net_dbg_ratelimited("addrconf: device %s not configured\n",
2537 dev->name);
2538 return;
2542 * Two things going on here:
2543 * 1) Add routes for on-link prefixes
2544 * 2) Configure prefixes with the auto flag set
2547 if (pinfo->onlink) {
2548 struct rt6_info *rt;
2549 unsigned long rt_expires;
2551 /* Avoid arithmetic overflow. Really, we could
2552 * save rt_expires in seconds, likely valid_lft,
2553 * but it would require division in fib gc, that it
2554 * not good.
2556 if (HZ > USER_HZ)
2557 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2558 else
2559 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2561 if (addrconf_finite_timeout(rt_expires))
2562 rt_expires *= HZ;
2564 rt = addrconf_get_prefix_route(&pinfo->prefix,
2565 pinfo->prefix_len,
2566 dev,
2567 RTF_ADDRCONF | RTF_PREFIX_RT,
2568 RTF_GATEWAY | RTF_DEFAULT);
2570 if (rt) {
2571 /* Autoconf prefix route */
2572 if (valid_lft == 0) {
2573 ip6_del_rt(rt);
2574 rt = NULL;
2575 } else if (addrconf_finite_timeout(rt_expires)) {
2576 /* not infinity */
2577 rt6_set_expires(rt, jiffies + rt_expires);
2578 } else {
2579 rt6_clean_expires(rt);
2581 } else if (valid_lft) {
2582 clock_t expires = 0;
2583 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2584 if (addrconf_finite_timeout(rt_expires)) {
2585 /* not infinity */
2586 flags |= RTF_EXPIRES;
2587 expires = jiffies_to_clock_t(rt_expires);
2589 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2590 dev, expires, flags);
2592 ip6_rt_put(rt);
2595 /* Try to figure out our local address for this prefix */
2597 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2598 struct in6_addr addr;
2599 bool tokenized = false, dev_addr_generated = false;
2601 if (pinfo->prefix_len == 64) {
2602 memcpy(&addr, &pinfo->prefix, 8);
2604 if (!ipv6_addr_any(&in6_dev->token)) {
2605 read_lock_bh(&in6_dev->lock);
2606 memcpy(addr.s6_addr + 8,
2607 in6_dev->token.s6_addr + 8, 8);
2608 read_unlock_bh(&in6_dev->lock);
2609 tokenized = true;
2610 } else if (is_addr_mode_generate_stable(in6_dev) &&
2611 !ipv6_generate_stable_address(&addr, 0,
2612 in6_dev)) {
2613 addr_flags |= IFA_F_STABLE_PRIVACY;
2614 goto ok;
2615 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2616 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2617 goto put;
2618 } else {
2619 dev_addr_generated = true;
2621 goto ok;
2623 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2624 pinfo->prefix_len);
2625 goto put;
2628 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2629 &addr, addr_type,
2630 addr_flags, sllao,
2631 tokenized, valid_lft,
2632 prefered_lft);
2633 if (err)
2634 goto put;
2636 /* Ignore error case here because previous prefix add addr was
2637 * successful which will be notified.
2639 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2640 addr_type, addr_flags, sllao,
2641 tokenized, valid_lft,
2642 prefered_lft,
2643 dev_addr_generated);
2645 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2646 put:
2647 in6_dev_put(in6_dev);
2651 * Set destination address.
2652 * Special case for SIT interfaces where we create a new "virtual"
2653 * device.
2655 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2657 struct in6_ifreq ireq;
2658 struct net_device *dev;
2659 int err = -EINVAL;
2661 rtnl_lock();
2663 err = -EFAULT;
2664 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2665 goto err_exit;
2667 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2669 err = -ENODEV;
2670 if (!dev)
2671 goto err_exit;
2673 #if IS_ENABLED(CONFIG_IPV6_SIT)
2674 if (dev->type == ARPHRD_SIT) {
2675 const struct net_device_ops *ops = dev->netdev_ops;
2676 struct ifreq ifr;
2677 struct ip_tunnel_parm p;
2679 err = -EADDRNOTAVAIL;
2680 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2681 goto err_exit;
2683 memset(&p, 0, sizeof(p));
2684 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2685 p.iph.saddr = 0;
2686 p.iph.version = 4;
2687 p.iph.ihl = 5;
2688 p.iph.protocol = IPPROTO_IPV6;
2689 p.iph.ttl = 64;
2690 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2692 if (ops->ndo_do_ioctl) {
2693 mm_segment_t oldfs = get_fs();
2695 set_fs(KERNEL_DS);
2696 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2697 set_fs(oldfs);
2698 } else
2699 err = -EOPNOTSUPP;
2701 if (err == 0) {
2702 err = -ENOBUFS;
2703 dev = __dev_get_by_name(net, p.name);
2704 if (!dev)
2705 goto err_exit;
2706 err = dev_open(dev);
2709 #endif
2711 err_exit:
2712 rtnl_unlock();
2713 return err;
2716 static int ipv6_mc_config(struct sock *sk, bool join,
2717 const struct in6_addr *addr, int ifindex)
2719 int ret;
2721 ASSERT_RTNL();
2723 lock_sock(sk);
2724 if (join)
2725 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2726 else
2727 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2728 release_sock(sk);
2730 return ret;
2734 * Manual configuration of address on an interface
2736 static int inet6_addr_add(struct net *net, int ifindex,
2737 const struct in6_addr *pfx,
2738 const struct in6_addr *peer_pfx,
2739 unsigned int plen, __u32 ifa_flags,
2740 __u32 prefered_lft, __u32 valid_lft)
2742 struct inet6_ifaddr *ifp;
2743 struct inet6_dev *idev;
2744 struct net_device *dev;
2745 unsigned long timeout;
2746 clock_t expires;
2747 int scope;
2748 u32 flags;
2750 ASSERT_RTNL();
2752 if (plen > 128)
2753 return -EINVAL;
2755 /* check the lifetime */
2756 if (!valid_lft || prefered_lft > valid_lft)
2757 return -EINVAL;
2759 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2760 return -EINVAL;
2762 dev = __dev_get_by_index(net, ifindex);
2763 if (!dev)
2764 return -ENODEV;
2766 idev = addrconf_add_dev(dev);
2767 if (IS_ERR(idev))
2768 return PTR_ERR(idev);
2770 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2771 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2772 true, pfx, ifindex);
2774 if (ret < 0)
2775 return ret;
2778 scope = ipv6_addr_scope(pfx);
2780 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2781 if (addrconf_finite_timeout(timeout)) {
2782 expires = jiffies_to_clock_t(timeout * HZ);
2783 valid_lft = timeout;
2784 flags = RTF_EXPIRES;
2785 } else {
2786 expires = 0;
2787 flags = 0;
2788 ifa_flags |= IFA_F_PERMANENT;
2791 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2792 if (addrconf_finite_timeout(timeout)) {
2793 if (timeout == 0)
2794 ifa_flags |= IFA_F_DEPRECATED;
2795 prefered_lft = timeout;
2798 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2799 valid_lft, prefered_lft);
2801 if (!IS_ERR(ifp)) {
2802 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2803 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2804 expires, flags);
2808 * Note that section 3.1 of RFC 4429 indicates
2809 * that the Optimistic flag should not be set for
2810 * manually configured addresses
2812 addrconf_dad_start(ifp);
2813 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2814 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2815 true, jiffies);
2816 in6_ifa_put(ifp);
2817 addrconf_verify_rtnl();
2818 return 0;
2819 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2820 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2821 false, pfx, ifindex);
2824 return PTR_ERR(ifp);
2827 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2828 const struct in6_addr *pfx, unsigned int plen)
2830 struct inet6_ifaddr *ifp;
2831 struct inet6_dev *idev;
2832 struct net_device *dev;
2834 if (plen > 128)
2835 return -EINVAL;
2837 dev = __dev_get_by_index(net, ifindex);
2838 if (!dev)
2839 return -ENODEV;
2841 idev = __in6_dev_get(dev);
2842 if (!idev)
2843 return -ENXIO;
2845 read_lock_bh(&idev->lock);
2846 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2847 if (ifp->prefix_len == plen &&
2848 ipv6_addr_equal(pfx, &ifp->addr)) {
2849 in6_ifa_hold(ifp);
2850 read_unlock_bh(&idev->lock);
2852 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2853 (ifa_flags & IFA_F_MANAGETEMPADDR))
2854 manage_tempaddrs(idev, ifp, 0, 0, false,
2855 jiffies);
2856 ipv6_del_addr(ifp);
2857 addrconf_verify_rtnl();
2858 if (ipv6_addr_is_multicast(pfx)) {
2859 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2860 false, pfx, dev->ifindex);
2862 return 0;
2865 read_unlock_bh(&idev->lock);
2866 return -EADDRNOTAVAIL;
2870 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2872 struct in6_ifreq ireq;
2873 int err;
2875 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2876 return -EPERM;
2878 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2879 return -EFAULT;
2881 rtnl_lock();
2882 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2883 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2884 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2885 rtnl_unlock();
2886 return err;
2889 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2891 struct in6_ifreq ireq;
2892 int err;
2894 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2895 return -EPERM;
2897 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2898 return -EFAULT;
2900 rtnl_lock();
2901 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2902 ireq.ifr6_prefixlen);
2903 rtnl_unlock();
2904 return err;
2907 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2908 int plen, int scope)
2910 struct inet6_ifaddr *ifp;
2912 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2913 scope, IFA_F_PERMANENT,
2914 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2915 if (!IS_ERR(ifp)) {
2916 spin_lock_bh(&ifp->lock);
2917 ifp->flags &= ~IFA_F_TENTATIVE;
2918 spin_unlock_bh(&ifp->lock);
2919 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2920 in6_ifa_put(ifp);
2924 #if IS_ENABLED(CONFIG_IPV6_SIT)
2925 static void sit_add_v4_addrs(struct inet6_dev *idev)
2927 struct in6_addr addr;
2928 struct net_device *dev;
2929 struct net *net = dev_net(idev->dev);
2930 int scope, plen;
2931 u32 pflags = 0;
2933 ASSERT_RTNL();
2935 memset(&addr, 0, sizeof(struct in6_addr));
2936 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2938 if (idev->dev->flags&IFF_POINTOPOINT) {
2939 addr.s6_addr32[0] = htonl(0xfe800000);
2940 scope = IFA_LINK;
2941 plen = 64;
2942 } else {
2943 scope = IPV6_ADDR_COMPATv4;
2944 plen = 96;
2945 pflags |= RTF_NONEXTHOP;
2948 if (addr.s6_addr32[3]) {
2949 add_addr(idev, &addr, plen, scope);
2950 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2951 return;
2954 for_each_netdev(net, dev) {
2955 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2956 if (in_dev && (dev->flags & IFF_UP)) {
2957 struct in_ifaddr *ifa;
2959 int flag = scope;
2961 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2963 addr.s6_addr32[3] = ifa->ifa_local;
2965 if (ifa->ifa_scope == RT_SCOPE_LINK)
2966 continue;
2967 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2968 if (idev->dev->flags&IFF_POINTOPOINT)
2969 continue;
2970 flag |= IFA_HOST;
2973 add_addr(idev, &addr, plen, flag);
2974 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2975 pflags);
2980 #endif
2982 static void init_loopback(struct net_device *dev)
2984 struct inet6_dev *idev;
2985 struct net_device *sp_dev;
2986 struct inet6_ifaddr *sp_ifa;
2987 struct rt6_info *sp_rt;
2989 /* ::1 */
2991 ASSERT_RTNL();
2993 idev = ipv6_find_idev(dev);
2994 if (!idev) {
2995 pr_debug("%s: add_dev failed\n", __func__);
2996 return;
2999 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
3001 /* Add routes to other interface's IPv6 addresses */
3002 for_each_netdev(dev_net(dev), sp_dev) {
3003 if (!strcmp(sp_dev->name, dev->name))
3004 continue;
3006 idev = __in6_dev_get(sp_dev);
3007 if (!idev)
3008 continue;
3010 read_lock_bh(&idev->lock);
3011 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3013 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3014 continue;
3016 if (sp_ifa->rt) {
3017 /* This dst has been added to garbage list when
3018 * lo device down, release this obsolete dst and
3019 * reallocate a new router for ifa.
3021 if (sp_ifa->rt->dst.obsolete > 0) {
3022 ip6_rt_put(sp_ifa->rt);
3023 sp_ifa->rt = NULL;
3024 } else {
3025 continue;
3029 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
3031 /* Failure cases are ignored */
3032 if (!IS_ERR(sp_rt)) {
3033 sp_ifa->rt = sp_rt;
3034 ip6_ins_rt(sp_rt);
3037 read_unlock_bh(&idev->lock);
3041 void addrconf_add_linklocal(struct inet6_dev *idev,
3042 const struct in6_addr *addr, u32 flags)
3044 struct inet6_ifaddr *ifp;
3045 u32 addr_flags = flags | IFA_F_PERMANENT;
3047 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3048 if (idev->cnf.optimistic_dad &&
3049 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3050 addr_flags |= IFA_F_OPTIMISTIC;
3051 #endif
3053 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3054 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
3055 if (!IS_ERR(ifp)) {
3056 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
3057 addrconf_dad_start(ifp);
3058 in6_ifa_put(ifp);
3061 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3063 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3065 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3066 return true;
3068 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3069 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3070 return true;
3072 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3073 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3074 return true;
3076 return false;
3079 static int ipv6_generate_stable_address(struct in6_addr *address,
3080 u8 dad_count,
3081 const struct inet6_dev *idev)
3083 static DEFINE_SPINLOCK(lock);
3084 static __u32 digest[SHA_DIGEST_WORDS];
3085 static __u32 workspace[SHA_WORKSPACE_WORDS];
3087 static union {
3088 char __data[SHA_MESSAGE_BYTES];
3089 struct {
3090 struct in6_addr secret;
3091 __be32 prefix[2];
3092 unsigned char hwaddr[MAX_ADDR_LEN];
3093 u8 dad_count;
3094 } __packed;
3095 } data;
3097 struct in6_addr secret;
3098 struct in6_addr temp;
3099 struct net *net = dev_net(idev->dev);
3101 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3103 if (idev->cnf.stable_secret.initialized)
3104 secret = idev->cnf.stable_secret.secret;
3105 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3106 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3107 else
3108 return -1;
3110 retry:
3111 spin_lock_bh(&lock);
3113 sha_init(digest);
3114 memset(&data, 0, sizeof(data));
3115 memset(workspace, 0, sizeof(workspace));
3116 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3117 data.prefix[0] = address->s6_addr32[0];
3118 data.prefix[1] = address->s6_addr32[1];
3119 data.secret = secret;
3120 data.dad_count = dad_count;
3122 sha_transform(digest, data.__data, workspace);
3124 temp = *address;
3125 temp.s6_addr32[2] = (__force __be32)digest[0];
3126 temp.s6_addr32[3] = (__force __be32)digest[1];
3128 spin_unlock_bh(&lock);
3130 if (ipv6_reserved_interfaceid(temp)) {
3131 dad_count++;
3132 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3133 return -1;
3134 goto retry;
3137 *address = temp;
3138 return 0;
3141 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3143 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3145 if (s->initialized)
3146 return;
3147 s = &idev->cnf.stable_secret;
3148 get_random_bytes(&s->secret, sizeof(s->secret));
3149 s->initialized = true;
3152 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3154 struct in6_addr addr;
3156 /* no link local addresses on L3 master devices */
3157 if (netif_is_l3_master(idev->dev))
3158 return;
3160 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3162 switch (idev->addr_gen_mode) {
3163 case IN6_ADDR_GEN_MODE_RANDOM:
3164 ipv6_gen_mode_random_init(idev);
3165 /* fallthrough */
3166 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3167 if (!ipv6_generate_stable_address(&addr, 0, idev))
3168 addrconf_add_linklocal(idev, &addr,
3169 IFA_F_STABLE_PRIVACY);
3170 else if (prefix_route)
3171 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3172 break;
3173 case IN6_ADDR_GEN_MODE_EUI64:
3174 /* addrconf_add_linklocal also adds a prefix_route and we
3175 * only need to care about prefix routes if ipv6_generate_eui64
3176 * couldn't generate one.
3178 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3179 addrconf_add_linklocal(idev, &addr, 0);
3180 else if (prefix_route)
3181 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3182 break;
3183 case IN6_ADDR_GEN_MODE_NONE:
3184 default:
3185 /* will not add any link local address */
3186 break;
3190 static void addrconf_dev_config(struct net_device *dev)
3192 struct inet6_dev *idev;
3194 ASSERT_RTNL();
3196 if ((dev->type != ARPHRD_ETHER) &&
3197 (dev->type != ARPHRD_FDDI) &&
3198 (dev->type != ARPHRD_ARCNET) &&
3199 (dev->type != ARPHRD_INFINIBAND) &&
3200 (dev->type != ARPHRD_IEEE1394) &&
3201 (dev->type != ARPHRD_TUNNEL6) &&
3202 (dev->type != ARPHRD_6LOWPAN) &&
3203 (dev->type != ARPHRD_NONE)) {
3204 /* Alas, we support only Ethernet autoconfiguration. */
3205 return;
3208 idev = addrconf_add_dev(dev);
3209 if (IS_ERR(idev))
3210 return;
3212 /* this device type has no EUI support */
3213 if (dev->type == ARPHRD_NONE &&
3214 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3215 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3217 addrconf_addr_gen(idev, false);
3220 #if IS_ENABLED(CONFIG_IPV6_SIT)
3221 static void addrconf_sit_config(struct net_device *dev)
3223 struct inet6_dev *idev;
3225 ASSERT_RTNL();
3228 * Configure the tunnel with one of our IPv4
3229 * addresses... we should configure all of
3230 * our v4 addrs in the tunnel
3233 idev = ipv6_find_idev(dev);
3234 if (!idev) {
3235 pr_debug("%s: add_dev failed\n", __func__);
3236 return;
3239 if (dev->priv_flags & IFF_ISATAP) {
3240 addrconf_addr_gen(idev, false);
3241 return;
3244 sit_add_v4_addrs(idev);
3246 if (dev->flags&IFF_POINTOPOINT)
3247 addrconf_add_mroute(dev);
3249 #endif
3251 #if IS_ENABLED(CONFIG_NET_IPGRE)
3252 static void addrconf_gre_config(struct net_device *dev)
3254 struct inet6_dev *idev;
3256 ASSERT_RTNL();
3258 idev = ipv6_find_idev(dev);
3259 if (!idev) {
3260 pr_debug("%s: add_dev failed\n", __func__);
3261 return;
3264 addrconf_addr_gen(idev, true);
3265 if (dev->flags & IFF_POINTOPOINT)
3266 addrconf_add_mroute(dev);
3268 #endif
3270 static int fixup_permanent_addr(struct inet6_dev *idev,
3271 struct inet6_ifaddr *ifp)
3273 if (!ifp->rt) {
3274 struct rt6_info *rt;
3276 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3277 if (unlikely(IS_ERR(rt)))
3278 return PTR_ERR(rt);
3280 ifp->rt = rt;
3283 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3284 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3285 idev->dev, 0, 0);
3288 addrconf_dad_start(ifp);
3290 return 0;
3293 static void addrconf_permanent_addr(struct net_device *dev)
3295 struct inet6_ifaddr *ifp, *tmp;
3296 struct inet6_dev *idev;
3298 idev = __in6_dev_get(dev);
3299 if (!idev)
3300 return;
3302 write_lock_bh(&idev->lock);
3304 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3305 if ((ifp->flags & IFA_F_PERMANENT) &&
3306 fixup_permanent_addr(idev, ifp) < 0) {
3307 write_unlock_bh(&idev->lock);
3308 ipv6_del_addr(ifp);
3309 write_lock_bh(&idev->lock);
3311 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3312 idev->dev->name, &ifp->addr);
3316 write_unlock_bh(&idev->lock);
3319 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3320 void *ptr)
3322 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3323 struct netdev_notifier_changeupper_info *info;
3324 struct inet6_dev *idev = __in6_dev_get(dev);
3325 int run_pending = 0;
3326 int err;
3328 switch (event) {
3329 case NETDEV_REGISTER:
3330 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3331 idev = ipv6_add_dev(dev);
3332 if (IS_ERR(idev))
3333 return notifier_from_errno(PTR_ERR(idev));
3335 break;
3337 case NETDEV_CHANGEMTU:
3338 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3339 if (dev->mtu < IPV6_MIN_MTU) {
3340 addrconf_ifdown(dev, 1);
3341 break;
3344 if (idev) {
3345 rt6_mtu_change(dev, dev->mtu);
3346 idev->cnf.mtu6 = dev->mtu;
3347 break;
3350 /* allocate new idev */
3351 idev = ipv6_add_dev(dev);
3352 if (IS_ERR(idev))
3353 break;
3355 /* device is still not ready */
3356 if (!(idev->if_flags & IF_READY))
3357 break;
3359 run_pending = 1;
3361 /* fall through */
3363 case NETDEV_UP:
3364 case NETDEV_CHANGE:
3365 if (dev->flags & IFF_SLAVE)
3366 break;
3368 if (idev && idev->cnf.disable_ipv6)
3369 break;
3371 if (event == NETDEV_UP) {
3372 /* restore routes for permanent addresses */
3373 addrconf_permanent_addr(dev);
3375 if (!addrconf_qdisc_ok(dev)) {
3376 /* device is not ready yet. */
3377 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3378 dev->name);
3379 break;
3382 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3383 idev = ipv6_add_dev(dev);
3385 if (!IS_ERR_OR_NULL(idev)) {
3386 idev->if_flags |= IF_READY;
3387 run_pending = 1;
3389 } else if (event == NETDEV_CHANGE) {
3390 if (!addrconf_qdisc_ok(dev)) {
3391 /* device is still not ready. */
3392 break;
3395 if (idev) {
3396 if (idev->if_flags & IF_READY)
3397 /* device is already configured. */
3398 break;
3399 idev->if_flags |= IF_READY;
3402 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3403 dev->name);
3405 run_pending = 1;
3408 switch (dev->type) {
3409 #if IS_ENABLED(CONFIG_IPV6_SIT)
3410 case ARPHRD_SIT:
3411 addrconf_sit_config(dev);
3412 break;
3413 #endif
3414 #if IS_ENABLED(CONFIG_NET_IPGRE)
3415 case ARPHRD_IPGRE:
3416 addrconf_gre_config(dev);
3417 break;
3418 #endif
3419 case ARPHRD_LOOPBACK:
3420 init_loopback(dev);
3421 break;
3423 default:
3424 addrconf_dev_config(dev);
3425 break;
3428 if (!IS_ERR_OR_NULL(idev)) {
3429 if (run_pending)
3430 addrconf_dad_run(idev);
3433 * If the MTU changed during the interface down,
3434 * when the interface up, the changed MTU must be
3435 * reflected in the idev as well as routers.
3437 if (idev->cnf.mtu6 != dev->mtu &&
3438 dev->mtu >= IPV6_MIN_MTU) {
3439 rt6_mtu_change(dev, dev->mtu);
3440 idev->cnf.mtu6 = dev->mtu;
3442 idev->tstamp = jiffies;
3443 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3446 * If the changed mtu during down is lower than
3447 * IPV6_MIN_MTU stop IPv6 on this interface.
3449 if (dev->mtu < IPV6_MIN_MTU)
3450 addrconf_ifdown(dev, 1);
3452 break;
3454 case NETDEV_DOWN:
3455 case NETDEV_UNREGISTER:
3457 * Remove all addresses from this interface.
3459 addrconf_ifdown(dev, event != NETDEV_DOWN);
3460 break;
3462 case NETDEV_CHANGENAME:
3463 if (idev) {
3464 snmp6_unregister_dev(idev);
3465 addrconf_sysctl_unregister(idev);
3466 err = addrconf_sysctl_register(idev);
3467 if (err)
3468 return notifier_from_errno(err);
3469 err = snmp6_register_dev(idev);
3470 if (err) {
3471 addrconf_sysctl_unregister(idev);
3472 return notifier_from_errno(err);
3475 break;
3477 case NETDEV_PRE_TYPE_CHANGE:
3478 case NETDEV_POST_TYPE_CHANGE:
3479 if (idev)
3480 addrconf_type_change(dev, event);
3481 break;
3483 case NETDEV_CHANGEUPPER:
3484 info = ptr;
3486 /* flush all routes if dev is linked to or unlinked from
3487 * an L3 master device (e.g., VRF)
3489 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3490 addrconf_ifdown(dev, 0);
3493 return NOTIFY_OK;
3497 * addrconf module should be notified of a device going up
3499 static struct notifier_block ipv6_dev_notf = {
3500 .notifier_call = addrconf_notify,
3503 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3505 struct inet6_dev *idev;
3506 ASSERT_RTNL();
3508 idev = __in6_dev_get(dev);
3510 if (event == NETDEV_POST_TYPE_CHANGE)
3511 ipv6_mc_remap(idev);
3512 else if (event == NETDEV_PRE_TYPE_CHANGE)
3513 ipv6_mc_unmap(idev);
3516 static bool addr_is_local(const struct in6_addr *addr)
3518 return ipv6_addr_type(addr) &
3519 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3522 static int addrconf_ifdown(struct net_device *dev, int how)
3524 struct net *net = dev_net(dev);
3525 struct inet6_dev *idev;
3526 struct inet6_ifaddr *ifa, *tmp;
3527 struct list_head del_list;
3528 int _keep_addr;
3529 bool keep_addr;
3530 int state, i;
3532 ASSERT_RTNL();
3534 rt6_ifdown(net, dev);
3535 neigh_ifdown(&nd_tbl, dev);
3537 idev = __in6_dev_get(dev);
3538 if (!idev)
3539 return -ENODEV;
3542 * Step 1: remove reference to ipv6 device from parent device.
3543 * Do not dev_put!
3545 if (how) {
3546 idev->dead = 1;
3548 /* protected by rtnl_lock */
3549 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3551 /* Step 1.5: remove snmp6 entry */
3552 snmp6_unregister_dev(idev);
3556 /* aggregate the system setting and interface setting */
3557 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3558 if (!_keep_addr)
3559 _keep_addr = idev->cnf.keep_addr_on_down;
3561 /* combine the user config with event to determine if permanent
3562 * addresses are to be removed from address hash table
3564 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
3566 /* Step 2: clear hash table */
3567 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3568 struct hlist_head *h = &inet6_addr_lst[i];
3570 spin_lock_bh(&addrconf_hash_lock);
3571 restart:
3572 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3573 if (ifa->idev == idev) {
3574 addrconf_del_dad_work(ifa);
3575 /* combined flag + permanent flag decide if
3576 * address is retained on a down event
3578 if (!keep_addr ||
3579 !(ifa->flags & IFA_F_PERMANENT) ||
3580 addr_is_local(&ifa->addr)) {
3581 hlist_del_init_rcu(&ifa->addr_lst);
3582 goto restart;
3586 spin_unlock_bh(&addrconf_hash_lock);
3589 write_lock_bh(&idev->lock);
3591 addrconf_del_rs_timer(idev);
3593 /* Step 2: clear flags for stateless addrconf */
3594 if (!how)
3595 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3597 if (how && del_timer(&idev->regen_timer))
3598 in6_dev_put(idev);
3600 /* Step 3: clear tempaddr list */
3601 while (!list_empty(&idev->tempaddr_list)) {
3602 ifa = list_first_entry(&idev->tempaddr_list,
3603 struct inet6_ifaddr, tmp_list);
3604 list_del(&ifa->tmp_list);
3605 write_unlock_bh(&idev->lock);
3606 spin_lock_bh(&ifa->lock);
3608 if (ifa->ifpub) {
3609 in6_ifa_put(ifa->ifpub);
3610 ifa->ifpub = NULL;
3612 spin_unlock_bh(&ifa->lock);
3613 in6_ifa_put(ifa);
3614 write_lock_bh(&idev->lock);
3617 /* re-combine the user config with event to determine if permanent
3618 * addresses are to be removed from the interface list
3620 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
3622 INIT_LIST_HEAD(&del_list);
3623 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3624 struct rt6_info *rt = NULL;
3626 addrconf_del_dad_work(ifa);
3628 write_unlock_bh(&idev->lock);
3629 spin_lock_bh(&ifa->lock);
3631 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3632 !addr_is_local(&ifa->addr)) {
3633 /* set state to skip the notifier below */
3634 state = INET6_IFADDR_STATE_DEAD;
3635 ifa->state = 0;
3636 if (!(ifa->flags & IFA_F_NODAD))
3637 ifa->flags |= IFA_F_TENTATIVE;
3639 rt = ifa->rt;
3640 ifa->rt = NULL;
3641 } else {
3642 state = ifa->state;
3643 ifa->state = INET6_IFADDR_STATE_DEAD;
3645 list_move(&ifa->if_list, &del_list);
3648 spin_unlock_bh(&ifa->lock);
3650 if (rt)
3651 ip6_del_rt(rt);
3653 if (state != INET6_IFADDR_STATE_DEAD) {
3654 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3655 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3656 } else {
3657 if (idev->cnf.forwarding)
3658 addrconf_leave_anycast(ifa);
3659 addrconf_leave_solict(ifa->idev, &ifa->addr);
3662 write_lock_bh(&idev->lock);
3665 write_unlock_bh(&idev->lock);
3667 /* now clean up addresses to be removed */
3668 while (!list_empty(&del_list)) {
3669 ifa = list_first_entry(&del_list,
3670 struct inet6_ifaddr, if_list);
3671 list_del(&ifa->if_list);
3673 in6_ifa_put(ifa);
3676 /* Step 5: Discard anycast and multicast list */
3677 if (how) {
3678 ipv6_ac_destroy_dev(idev);
3679 ipv6_mc_destroy_dev(idev);
3680 } else {
3681 ipv6_mc_down(idev);
3684 idev->tstamp = jiffies;
3686 /* Last: Shot the device (if unregistered) */
3687 if (how) {
3688 addrconf_sysctl_unregister(idev);
3689 neigh_parms_release(&nd_tbl, idev->nd_parms);
3690 neigh_ifdown(&nd_tbl, dev);
3691 in6_dev_put(idev);
3693 return 0;
3696 static void addrconf_rs_timer(unsigned long data)
3698 struct inet6_dev *idev = (struct inet6_dev *)data;
3699 struct net_device *dev = idev->dev;
3700 struct in6_addr lladdr;
3702 write_lock(&idev->lock);
3703 if (idev->dead || !(idev->if_flags & IF_READY))
3704 goto out;
3706 if (!ipv6_accept_ra(idev))
3707 goto out;
3709 /* Announcement received after solicitation was sent */
3710 if (idev->if_flags & IF_RA_RCVD)
3711 goto out;
3713 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
3714 write_unlock(&idev->lock);
3715 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3716 ndisc_send_rs(dev, &lladdr,
3717 &in6addr_linklocal_allrouters);
3718 else
3719 goto put;
3721 write_lock(&idev->lock);
3722 idev->rs_interval = rfc3315_s14_backoff_update(
3723 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
3724 /* The wait after the last probe can be shorter */
3725 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3726 idev->cnf.rtr_solicits) ?
3727 idev->cnf.rtr_solicit_delay :
3728 idev->rs_interval);
3729 } else {
3731 * Note: we do not support deprecated "all on-link"
3732 * assumption any longer.
3734 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3737 out:
3738 write_unlock(&idev->lock);
3739 put:
3740 in6_dev_put(idev);
3744 * Duplicate Address Detection
3746 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3748 unsigned long rand_num;
3749 struct inet6_dev *idev = ifp->idev;
3751 if (ifp->flags & IFA_F_OPTIMISTIC)
3752 rand_num = 0;
3753 else
3754 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3756 ifp->dad_probes = idev->cnf.dad_transmits;
3757 addrconf_mod_dad_work(ifp, rand_num);
3760 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3762 struct inet6_dev *idev = ifp->idev;
3763 struct net_device *dev = idev->dev;
3764 bool notify = false;
3766 addrconf_join_solict(dev, &ifp->addr);
3768 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3770 read_lock_bh(&idev->lock);
3771 spin_lock(&ifp->lock);
3772 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3773 goto out;
3775 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3776 idev->cnf.accept_dad < 1 ||
3777 !(ifp->flags&IFA_F_TENTATIVE) ||
3778 ifp->flags & IFA_F_NODAD) {
3779 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3780 spin_unlock(&ifp->lock);
3781 read_unlock_bh(&idev->lock);
3783 addrconf_dad_completed(ifp);
3784 return;
3787 if (!(idev->if_flags & IF_READY)) {
3788 spin_unlock(&ifp->lock);
3789 read_unlock_bh(&idev->lock);
3791 * If the device is not ready:
3792 * - keep it tentative if it is a permanent address.
3793 * - otherwise, kill it.
3795 in6_ifa_hold(ifp);
3796 addrconf_dad_stop(ifp, 0);
3797 return;
3801 * Optimistic nodes can start receiving
3802 * Frames right away
3804 if (ifp->flags & IFA_F_OPTIMISTIC) {
3805 ip6_ins_rt(ifp->rt);
3806 if (ipv6_use_optimistic_addr(idev)) {
3807 /* Because optimistic nodes can use this address,
3808 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3810 notify = true;
3814 addrconf_dad_kick(ifp);
3815 out:
3816 spin_unlock(&ifp->lock);
3817 read_unlock_bh(&idev->lock);
3818 if (notify)
3819 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3822 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3824 bool begin_dad = false;
3826 spin_lock_bh(&ifp->lock);
3827 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3828 ifp->state = INET6_IFADDR_STATE_PREDAD;
3829 begin_dad = true;
3831 spin_unlock_bh(&ifp->lock);
3833 if (begin_dad)
3834 addrconf_mod_dad_work(ifp, 0);
3837 static void addrconf_dad_work(struct work_struct *w)
3839 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3840 struct inet6_ifaddr,
3841 dad_work);
3842 struct inet6_dev *idev = ifp->idev;
3843 struct in6_addr mcaddr;
3844 bool disable_ipv6 = false;
3846 enum {
3847 DAD_PROCESS,
3848 DAD_BEGIN,
3849 DAD_ABORT,
3850 } action = DAD_PROCESS;
3852 rtnl_lock();
3854 spin_lock_bh(&ifp->lock);
3855 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3856 action = DAD_BEGIN;
3857 ifp->state = INET6_IFADDR_STATE_DAD;
3858 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3859 action = DAD_ABORT;
3860 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3862 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3863 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3864 struct in6_addr addr;
3866 addr.s6_addr32[0] = htonl(0xfe800000);
3867 addr.s6_addr32[1] = 0;
3869 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3870 ipv6_addr_equal(&ifp->addr, &addr)) {
3871 /* DAD failed for link-local based on MAC */
3872 idev->cnf.disable_ipv6 = 1;
3874 pr_info("%s: IPv6 being disabled!\n",
3875 ifp->idev->dev->name);
3876 disable_ipv6 = true;
3880 spin_unlock_bh(&ifp->lock);
3882 if (action == DAD_BEGIN) {
3883 addrconf_dad_begin(ifp);
3884 goto out;
3885 } else if (action == DAD_ABORT) {
3886 in6_ifa_hold(ifp);
3887 addrconf_dad_stop(ifp, 1);
3888 if (disable_ipv6)
3889 addrconf_ifdown(idev->dev, 0);
3890 goto out;
3893 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3894 goto out;
3896 write_lock_bh(&idev->lock);
3897 if (idev->dead || !(idev->if_flags & IF_READY)) {
3898 write_unlock_bh(&idev->lock);
3899 goto out;
3902 spin_lock(&ifp->lock);
3903 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3904 spin_unlock(&ifp->lock);
3905 write_unlock_bh(&idev->lock);
3906 goto out;
3909 if (ifp->dad_probes == 0) {
3911 * DAD was successful
3914 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3915 spin_unlock(&ifp->lock);
3916 write_unlock_bh(&idev->lock);
3918 addrconf_dad_completed(ifp);
3920 goto out;
3923 ifp->dad_probes--;
3924 addrconf_mod_dad_work(ifp,
3925 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3926 spin_unlock(&ifp->lock);
3927 write_unlock_bh(&idev->lock);
3929 /* send a neighbour solicitation for our addr */
3930 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3931 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
3932 out:
3933 in6_ifa_put(ifp);
3934 rtnl_unlock();
3937 /* ifp->idev must be at least read locked */
3938 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3940 struct inet6_ifaddr *ifpiter;
3941 struct inet6_dev *idev = ifp->idev;
3943 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3944 if (ifpiter->scope > IFA_LINK)
3945 break;
3946 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3947 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3948 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3949 IFA_F_PERMANENT)
3950 return false;
3952 return true;
3955 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3957 struct net_device *dev = ifp->idev->dev;
3958 struct in6_addr lladdr;
3959 bool send_rs, send_mld;
3961 addrconf_del_dad_work(ifp);
3964 * Configure the address for reception. Now it is valid.
3967 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3969 /* If added prefix is link local and we are prepared to process
3970 router advertisements, start sending router solicitations.
3973 read_lock_bh(&ifp->idev->lock);
3974 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3975 send_rs = send_mld &&
3976 ipv6_accept_ra(ifp->idev) &&
3977 ifp->idev->cnf.rtr_solicits != 0 &&
3978 (dev->flags&IFF_LOOPBACK) == 0;
3979 read_unlock_bh(&ifp->idev->lock);
3981 /* While dad is in progress mld report's source address is in6_addrany.
3982 * Resend with proper ll now.
3984 if (send_mld)
3985 ipv6_mc_dad_complete(ifp->idev);
3987 if (send_rs) {
3989 * If a host as already performed a random delay
3990 * [...] as part of DAD [...] there is no need
3991 * to delay again before sending the first RS
3993 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3994 return;
3995 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3997 write_lock_bh(&ifp->idev->lock);
3998 spin_lock(&ifp->lock);
3999 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4000 ifp->idev->cnf.rtr_solicit_interval);
4001 ifp->idev->rs_probes = 1;
4002 ifp->idev->if_flags |= IF_RS_SENT;
4003 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4004 spin_unlock(&ifp->lock);
4005 write_unlock_bh(&ifp->idev->lock);
4009 static void addrconf_dad_run(struct inet6_dev *idev)
4011 struct inet6_ifaddr *ifp;
4013 read_lock_bh(&idev->lock);
4014 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4015 spin_lock(&ifp->lock);
4016 if (ifp->flags & IFA_F_TENTATIVE &&
4017 ifp->state == INET6_IFADDR_STATE_DAD)
4018 addrconf_dad_kick(ifp);
4019 spin_unlock(&ifp->lock);
4021 read_unlock_bh(&idev->lock);
4024 #ifdef CONFIG_PROC_FS
4025 struct if6_iter_state {
4026 struct seq_net_private p;
4027 int bucket;
4028 int offset;
4031 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4033 struct inet6_ifaddr *ifa = NULL;
4034 struct if6_iter_state *state = seq->private;
4035 struct net *net = seq_file_net(seq);
4036 int p = 0;
4038 /* initial bucket if pos is 0 */
4039 if (pos == 0) {
4040 state->bucket = 0;
4041 state->offset = 0;
4044 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4045 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
4046 addr_lst) {
4047 if (!net_eq(dev_net(ifa->idev->dev), net))
4048 continue;
4049 /* sync with offset */
4050 if (p < state->offset) {
4051 p++;
4052 continue;
4054 state->offset++;
4055 return ifa;
4058 /* prepare for next bucket */
4059 state->offset = 0;
4060 p = 0;
4062 return NULL;
4065 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4066 struct inet6_ifaddr *ifa)
4068 struct if6_iter_state *state = seq->private;
4069 struct net *net = seq_file_net(seq);
4071 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
4072 if (!net_eq(dev_net(ifa->idev->dev), net))
4073 continue;
4074 state->offset++;
4075 return ifa;
4078 while (++state->bucket < IN6_ADDR_HSIZE) {
4079 state->offset = 0;
4080 hlist_for_each_entry_rcu_bh(ifa,
4081 &inet6_addr_lst[state->bucket], addr_lst) {
4082 if (!net_eq(dev_net(ifa->idev->dev), net))
4083 continue;
4084 state->offset++;
4085 return ifa;
4089 return NULL;
4092 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4093 __acquires(rcu_bh)
4095 rcu_read_lock_bh();
4096 return if6_get_first(seq, *pos);
4099 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4101 struct inet6_ifaddr *ifa;
4103 ifa = if6_get_next(seq, v);
4104 ++*pos;
4105 return ifa;
4108 static void if6_seq_stop(struct seq_file *seq, void *v)
4109 __releases(rcu_bh)
4111 rcu_read_unlock_bh();
4114 static int if6_seq_show(struct seq_file *seq, void *v)
4116 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4117 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4118 &ifp->addr,
4119 ifp->idev->dev->ifindex,
4120 ifp->prefix_len,
4121 ifp->scope,
4122 (u8) ifp->flags,
4123 ifp->idev->dev->name);
4124 return 0;
4127 static const struct seq_operations if6_seq_ops = {
4128 .start = if6_seq_start,
4129 .next = if6_seq_next,
4130 .show = if6_seq_show,
4131 .stop = if6_seq_stop,
4134 static int if6_seq_open(struct inode *inode, struct file *file)
4136 return seq_open_net(inode, file, &if6_seq_ops,
4137 sizeof(struct if6_iter_state));
4140 static const struct file_operations if6_fops = {
4141 .owner = THIS_MODULE,
4142 .open = if6_seq_open,
4143 .read = seq_read,
4144 .llseek = seq_lseek,
4145 .release = seq_release_net,
4148 static int __net_init if6_proc_net_init(struct net *net)
4150 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
4151 return -ENOMEM;
4152 return 0;
4155 static void __net_exit if6_proc_net_exit(struct net *net)
4157 remove_proc_entry("if_inet6", net->proc_net);
4160 static struct pernet_operations if6_proc_net_ops = {
4161 .init = if6_proc_net_init,
4162 .exit = if6_proc_net_exit,
4165 int __init if6_proc_init(void)
4167 return register_pernet_subsys(&if6_proc_net_ops);
4170 void if6_proc_exit(void)
4172 unregister_pernet_subsys(&if6_proc_net_ops);
4174 #endif /* CONFIG_PROC_FS */
4176 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4177 /* Check if address is a home address configured on any interface. */
4178 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4180 int ret = 0;
4181 struct inet6_ifaddr *ifp = NULL;
4182 unsigned int hash = inet6_addr_hash(addr);
4184 rcu_read_lock_bh();
4185 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
4186 if (!net_eq(dev_net(ifp->idev->dev), net))
4187 continue;
4188 if (ipv6_addr_equal(&ifp->addr, addr) &&
4189 (ifp->flags & IFA_F_HOMEADDRESS)) {
4190 ret = 1;
4191 break;
4194 rcu_read_unlock_bh();
4195 return ret;
4197 #endif
4200 * Periodic address status verification
4203 static void addrconf_verify_rtnl(void)
4205 unsigned long now, next, next_sec, next_sched;
4206 struct inet6_ifaddr *ifp;
4207 int i;
4209 ASSERT_RTNL();
4211 rcu_read_lock_bh();
4212 now = jiffies;
4213 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4215 cancel_delayed_work(&addr_chk_work);
4217 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4218 restart:
4219 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
4220 unsigned long age;
4222 /* When setting preferred_lft to a value not zero or
4223 * infinity, while valid_lft is infinity
4224 * IFA_F_PERMANENT has a non-infinity life time.
4226 if ((ifp->flags & IFA_F_PERMANENT) &&
4227 (ifp->prefered_lft == INFINITY_LIFE_TIME))
4228 continue;
4230 spin_lock(&ifp->lock);
4231 /* We try to batch several events at once. */
4232 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4234 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4235 age >= ifp->valid_lft) {
4236 spin_unlock(&ifp->lock);
4237 in6_ifa_hold(ifp);
4238 ipv6_del_addr(ifp);
4239 goto restart;
4240 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4241 spin_unlock(&ifp->lock);
4242 continue;
4243 } else if (age >= ifp->prefered_lft) {
4244 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4245 int deprecate = 0;
4247 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4248 deprecate = 1;
4249 ifp->flags |= IFA_F_DEPRECATED;
4252 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4253 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4254 next = ifp->tstamp + ifp->valid_lft * HZ;
4256 spin_unlock(&ifp->lock);
4258 if (deprecate) {
4259 in6_ifa_hold(ifp);
4261 ipv6_ifa_notify(0, ifp);
4262 in6_ifa_put(ifp);
4263 goto restart;
4265 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4266 !(ifp->flags&IFA_F_TENTATIVE)) {
4267 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4268 ifp->idev->cnf.dad_transmits *
4269 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
4271 if (age >= ifp->prefered_lft - regen_advance) {
4272 struct inet6_ifaddr *ifpub = ifp->ifpub;
4273 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4274 next = ifp->tstamp + ifp->prefered_lft * HZ;
4275 if (!ifp->regen_count && ifpub) {
4276 ifp->regen_count++;
4277 in6_ifa_hold(ifp);
4278 in6_ifa_hold(ifpub);
4279 spin_unlock(&ifp->lock);
4281 spin_lock(&ifpub->lock);
4282 ifpub->regen_count = 0;
4283 spin_unlock(&ifpub->lock);
4284 ipv6_create_tempaddr(ifpub, ifp);
4285 in6_ifa_put(ifpub);
4286 in6_ifa_put(ifp);
4287 goto restart;
4289 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4290 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4291 spin_unlock(&ifp->lock);
4292 } else {
4293 /* ifp->prefered_lft <= ifp->valid_lft */
4294 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4295 next = ifp->tstamp + ifp->prefered_lft * HZ;
4296 spin_unlock(&ifp->lock);
4301 next_sec = round_jiffies_up(next);
4302 next_sched = next;
4304 /* If rounded timeout is accurate enough, accept it. */
4305 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4306 next_sched = next_sec;
4308 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4309 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4310 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4312 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4313 now, next, next_sec, next_sched);
4314 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
4315 rcu_read_unlock_bh();
4318 static void addrconf_verify_work(struct work_struct *w)
4320 rtnl_lock();
4321 addrconf_verify_rtnl();
4322 rtnl_unlock();
4325 static void addrconf_verify(void)
4327 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4330 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4331 struct in6_addr **peer_pfx)
4333 struct in6_addr *pfx = NULL;
4335 *peer_pfx = NULL;
4337 if (addr)
4338 pfx = nla_data(addr);
4340 if (local) {
4341 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4342 *peer_pfx = pfx;
4343 pfx = nla_data(local);
4346 return pfx;
4349 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4350 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4351 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4352 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4353 [IFA_FLAGS] = { .len = sizeof(u32) },
4356 static int
4357 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4359 struct net *net = sock_net(skb->sk);
4360 struct ifaddrmsg *ifm;
4361 struct nlattr *tb[IFA_MAX+1];
4362 struct in6_addr *pfx, *peer_pfx;
4363 u32 ifa_flags;
4364 int err;
4366 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4367 if (err < 0)
4368 return err;
4370 ifm = nlmsg_data(nlh);
4371 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4372 if (!pfx)
4373 return -EINVAL;
4375 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4377 /* We ignore other flags so far. */
4378 ifa_flags &= IFA_F_MANAGETEMPADDR;
4380 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4381 ifm->ifa_prefixlen);
4384 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4385 u32 prefered_lft, u32 valid_lft)
4387 u32 flags;
4388 clock_t expires;
4389 unsigned long timeout;
4390 bool was_managetempaddr;
4391 bool had_prefixroute;
4393 ASSERT_RTNL();
4395 if (!valid_lft || (prefered_lft > valid_lft))
4396 return -EINVAL;
4398 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4399 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4400 return -EINVAL;
4402 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4403 if (addrconf_finite_timeout(timeout)) {
4404 expires = jiffies_to_clock_t(timeout * HZ);
4405 valid_lft = timeout;
4406 flags = RTF_EXPIRES;
4407 } else {
4408 expires = 0;
4409 flags = 0;
4410 ifa_flags |= IFA_F_PERMANENT;
4413 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4414 if (addrconf_finite_timeout(timeout)) {
4415 if (timeout == 0)
4416 ifa_flags |= IFA_F_DEPRECATED;
4417 prefered_lft = timeout;
4420 spin_lock_bh(&ifp->lock);
4421 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4422 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4423 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4424 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4425 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4426 IFA_F_NOPREFIXROUTE);
4427 ifp->flags |= ifa_flags;
4428 ifp->tstamp = jiffies;
4429 ifp->valid_lft = valid_lft;
4430 ifp->prefered_lft = prefered_lft;
4432 spin_unlock_bh(&ifp->lock);
4433 if (!(ifp->flags&IFA_F_TENTATIVE))
4434 ipv6_ifa_notify(0, ifp);
4436 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4437 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4438 expires, flags);
4439 } else if (had_prefixroute) {
4440 enum cleanup_prefix_rt_t action;
4441 unsigned long rt_expires;
4443 write_lock_bh(&ifp->idev->lock);
4444 action = check_cleanup_prefix_route(ifp, &rt_expires);
4445 write_unlock_bh(&ifp->idev->lock);
4447 if (action != CLEANUP_PREFIX_RT_NOP) {
4448 cleanup_prefix_route(ifp, rt_expires,
4449 action == CLEANUP_PREFIX_RT_DEL);
4453 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4454 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4455 valid_lft = prefered_lft = 0;
4456 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4457 !was_managetempaddr, jiffies);
4460 addrconf_verify_rtnl();
4462 return 0;
4465 static int
4466 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4468 struct net *net = sock_net(skb->sk);
4469 struct ifaddrmsg *ifm;
4470 struct nlattr *tb[IFA_MAX+1];
4471 struct in6_addr *pfx, *peer_pfx;
4472 struct inet6_ifaddr *ifa;
4473 struct net_device *dev;
4474 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4475 u32 ifa_flags;
4476 int err;
4478 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4479 if (err < 0)
4480 return err;
4482 ifm = nlmsg_data(nlh);
4483 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4484 if (!pfx)
4485 return -EINVAL;
4487 if (tb[IFA_CACHEINFO]) {
4488 struct ifa_cacheinfo *ci;
4490 ci = nla_data(tb[IFA_CACHEINFO]);
4491 valid_lft = ci->ifa_valid;
4492 preferred_lft = ci->ifa_prefered;
4493 } else {
4494 preferred_lft = INFINITY_LIFE_TIME;
4495 valid_lft = INFINITY_LIFE_TIME;
4498 dev = __dev_get_by_index(net, ifm->ifa_index);
4499 if (!dev)
4500 return -ENODEV;
4502 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4504 /* We ignore other flags so far. */
4505 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4506 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4508 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4509 if (!ifa) {
4511 * It would be best to check for !NLM_F_CREATE here but
4512 * userspace already relies on not having to provide this.
4514 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4515 ifm->ifa_prefixlen, ifa_flags,
4516 preferred_lft, valid_lft);
4519 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4520 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4521 err = -EEXIST;
4522 else
4523 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4525 in6_ifa_put(ifa);
4527 return err;
4530 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4531 u8 scope, int ifindex)
4533 struct ifaddrmsg *ifm;
4535 ifm = nlmsg_data(nlh);
4536 ifm->ifa_family = AF_INET6;
4537 ifm->ifa_prefixlen = prefixlen;
4538 ifm->ifa_flags = flags;
4539 ifm->ifa_scope = scope;
4540 ifm->ifa_index = ifindex;
4543 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4544 unsigned long tstamp, u32 preferred, u32 valid)
4546 struct ifa_cacheinfo ci;
4548 ci.cstamp = cstamp_delta(cstamp);
4549 ci.tstamp = cstamp_delta(tstamp);
4550 ci.ifa_prefered = preferred;
4551 ci.ifa_valid = valid;
4553 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4556 static inline int rt_scope(int ifa_scope)
4558 if (ifa_scope & IFA_HOST)
4559 return RT_SCOPE_HOST;
4560 else if (ifa_scope & IFA_LINK)
4561 return RT_SCOPE_LINK;
4562 else if (ifa_scope & IFA_SITE)
4563 return RT_SCOPE_SITE;
4564 else
4565 return RT_SCOPE_UNIVERSE;
4568 static inline int inet6_ifaddr_msgsize(void)
4570 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4571 + nla_total_size(16) /* IFA_LOCAL */
4572 + nla_total_size(16) /* IFA_ADDRESS */
4573 + nla_total_size(sizeof(struct ifa_cacheinfo))
4574 + nla_total_size(4) /* IFA_FLAGS */;
4577 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4578 u32 portid, u32 seq, int event, unsigned int flags)
4580 struct nlmsghdr *nlh;
4581 u32 preferred, valid;
4583 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4584 if (!nlh)
4585 return -EMSGSIZE;
4587 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4588 ifa->idev->dev->ifindex);
4590 if (!((ifa->flags&IFA_F_PERMANENT) &&
4591 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4592 preferred = ifa->prefered_lft;
4593 valid = ifa->valid_lft;
4594 if (preferred != INFINITY_LIFE_TIME) {
4595 long tval = (jiffies - ifa->tstamp)/HZ;
4596 if (preferred > tval)
4597 preferred -= tval;
4598 else
4599 preferred = 0;
4600 if (valid != INFINITY_LIFE_TIME) {
4601 if (valid > tval)
4602 valid -= tval;
4603 else
4604 valid = 0;
4607 } else {
4608 preferred = INFINITY_LIFE_TIME;
4609 valid = INFINITY_LIFE_TIME;
4612 if (!ipv6_addr_any(&ifa->peer_addr)) {
4613 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4614 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4615 goto error;
4616 } else
4617 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4618 goto error;
4620 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4621 goto error;
4623 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4624 goto error;
4626 nlmsg_end(skb, nlh);
4627 return 0;
4629 error:
4630 nlmsg_cancel(skb, nlh);
4631 return -EMSGSIZE;
4634 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4635 u32 portid, u32 seq, int event, u16 flags)
4637 struct nlmsghdr *nlh;
4638 u8 scope = RT_SCOPE_UNIVERSE;
4639 int ifindex = ifmca->idev->dev->ifindex;
4641 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4642 scope = RT_SCOPE_SITE;
4644 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4645 if (!nlh)
4646 return -EMSGSIZE;
4648 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4649 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4650 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4651 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4652 nlmsg_cancel(skb, nlh);
4653 return -EMSGSIZE;
4656 nlmsg_end(skb, nlh);
4657 return 0;
4660 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4661 u32 portid, u32 seq, int event, unsigned int flags)
4663 struct nlmsghdr *nlh;
4664 u8 scope = RT_SCOPE_UNIVERSE;
4665 int ifindex = ifaca->aca_idev->dev->ifindex;
4667 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4668 scope = RT_SCOPE_SITE;
4670 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4671 if (!nlh)
4672 return -EMSGSIZE;
4674 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4675 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4676 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4677 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4678 nlmsg_cancel(skb, nlh);
4679 return -EMSGSIZE;
4682 nlmsg_end(skb, nlh);
4683 return 0;
4686 enum addr_type_t {
4687 UNICAST_ADDR,
4688 MULTICAST_ADDR,
4689 ANYCAST_ADDR,
4692 /* called with rcu_read_lock() */
4693 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4694 struct netlink_callback *cb, enum addr_type_t type,
4695 int s_ip_idx, int *p_ip_idx)
4697 struct ifmcaddr6 *ifmca;
4698 struct ifacaddr6 *ifaca;
4699 int err = 1;
4700 int ip_idx = *p_ip_idx;
4702 read_lock_bh(&idev->lock);
4703 switch (type) {
4704 case UNICAST_ADDR: {
4705 struct inet6_ifaddr *ifa;
4707 /* unicast address incl. temp addr */
4708 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4709 if (++ip_idx < s_ip_idx)
4710 continue;
4711 err = inet6_fill_ifaddr(skb, ifa,
4712 NETLINK_CB(cb->skb).portid,
4713 cb->nlh->nlmsg_seq,
4714 RTM_NEWADDR,
4715 NLM_F_MULTI);
4716 if (err < 0)
4717 break;
4718 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4720 break;
4722 case MULTICAST_ADDR:
4723 /* multicast address */
4724 for (ifmca = idev->mc_list; ifmca;
4725 ifmca = ifmca->next, ip_idx++) {
4726 if (ip_idx < s_ip_idx)
4727 continue;
4728 err = inet6_fill_ifmcaddr(skb, ifmca,
4729 NETLINK_CB(cb->skb).portid,
4730 cb->nlh->nlmsg_seq,
4731 RTM_GETMULTICAST,
4732 NLM_F_MULTI);
4733 if (err < 0)
4734 break;
4736 break;
4737 case ANYCAST_ADDR:
4738 /* anycast address */
4739 for (ifaca = idev->ac_list; ifaca;
4740 ifaca = ifaca->aca_next, ip_idx++) {
4741 if (ip_idx < s_ip_idx)
4742 continue;
4743 err = inet6_fill_ifacaddr(skb, ifaca,
4744 NETLINK_CB(cb->skb).portid,
4745 cb->nlh->nlmsg_seq,
4746 RTM_GETANYCAST,
4747 NLM_F_MULTI);
4748 if (err < 0)
4749 break;
4751 break;
4752 default:
4753 break;
4755 read_unlock_bh(&idev->lock);
4756 *p_ip_idx = ip_idx;
4757 return err;
4760 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4761 enum addr_type_t type)
4763 struct net *net = sock_net(skb->sk);
4764 int h, s_h;
4765 int idx, ip_idx;
4766 int s_idx, s_ip_idx;
4767 struct net_device *dev;
4768 struct inet6_dev *idev;
4769 struct hlist_head *head;
4771 s_h = cb->args[0];
4772 s_idx = idx = cb->args[1];
4773 s_ip_idx = ip_idx = cb->args[2];
4775 rcu_read_lock();
4776 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4777 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4778 idx = 0;
4779 head = &net->dev_index_head[h];
4780 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4781 if (idx < s_idx)
4782 goto cont;
4783 if (h > s_h || idx > s_idx)
4784 s_ip_idx = 0;
4785 ip_idx = 0;
4786 idev = __in6_dev_get(dev);
4787 if (!idev)
4788 goto cont;
4790 if (in6_dump_addrs(idev, skb, cb, type,
4791 s_ip_idx, &ip_idx) < 0)
4792 goto done;
4793 cont:
4794 idx++;
4797 done:
4798 rcu_read_unlock();
4799 cb->args[0] = h;
4800 cb->args[1] = idx;
4801 cb->args[2] = ip_idx;
4803 return skb->len;
4806 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4808 enum addr_type_t type = UNICAST_ADDR;
4810 return inet6_dump_addr(skb, cb, type);
4813 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4815 enum addr_type_t type = MULTICAST_ADDR;
4817 return inet6_dump_addr(skb, cb, type);
4821 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4823 enum addr_type_t type = ANYCAST_ADDR;
4825 return inet6_dump_addr(skb, cb, type);
4828 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4830 struct net *net = sock_net(in_skb->sk);
4831 struct ifaddrmsg *ifm;
4832 struct nlattr *tb[IFA_MAX+1];
4833 struct in6_addr *addr = NULL, *peer;
4834 struct net_device *dev = NULL;
4835 struct inet6_ifaddr *ifa;
4836 struct sk_buff *skb;
4837 int err;
4839 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4840 if (err < 0)
4841 goto errout;
4843 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4844 if (!addr) {
4845 err = -EINVAL;
4846 goto errout;
4849 ifm = nlmsg_data(nlh);
4850 if (ifm->ifa_index)
4851 dev = __dev_get_by_index(net, ifm->ifa_index);
4853 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4854 if (!ifa) {
4855 err = -EADDRNOTAVAIL;
4856 goto errout;
4859 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4860 if (!skb) {
4861 err = -ENOBUFS;
4862 goto errout_ifa;
4865 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4866 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4867 if (err < 0) {
4868 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4869 WARN_ON(err == -EMSGSIZE);
4870 kfree_skb(skb);
4871 goto errout_ifa;
4873 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4874 errout_ifa:
4875 in6_ifa_put(ifa);
4876 errout:
4877 return err;
4880 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4882 struct sk_buff *skb;
4883 struct net *net = dev_net(ifa->idev->dev);
4884 int err = -ENOBUFS;
4886 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4887 if (!skb)
4888 goto errout;
4890 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4891 if (err < 0) {
4892 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4893 WARN_ON(err == -EMSGSIZE);
4894 kfree_skb(skb);
4895 goto errout;
4897 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4898 return;
4899 errout:
4900 if (err < 0)
4901 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4904 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4905 __s32 *array, int bytes)
4907 BUG_ON(bytes < (DEVCONF_MAX * 4));
4909 memset(array, 0, bytes);
4910 array[DEVCONF_FORWARDING] = cnf->forwarding;
4911 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4912 array[DEVCONF_MTU6] = cnf->mtu6;
4913 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4914 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4915 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4916 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4917 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4918 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4919 jiffies_to_msecs(cnf->rtr_solicit_interval);
4920 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4921 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
4922 array[DEVCONF_RTR_SOLICIT_DELAY] =
4923 jiffies_to_msecs(cnf->rtr_solicit_delay);
4924 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4925 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4926 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4927 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4928 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4929 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4930 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4931 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4932 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4933 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4934 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4935 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4936 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
4937 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4938 #ifdef CONFIG_IPV6_ROUTER_PREF
4939 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4940 array[DEVCONF_RTR_PROBE_INTERVAL] =
4941 jiffies_to_msecs(cnf->rtr_probe_interval);
4942 #ifdef CONFIG_IPV6_ROUTE_INFO
4943 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4944 #endif
4945 #endif
4946 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4947 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4948 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4949 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4950 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4951 #endif
4952 #ifdef CONFIG_IPV6_MROUTE
4953 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4954 #endif
4955 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4956 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4957 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4958 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4959 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4960 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4961 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4962 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
4963 /* we omit DEVCONF_STABLE_SECRET for now */
4964 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4965 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
4966 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4967 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
4970 static inline size_t inet6_ifla6_size(void)
4972 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4973 + nla_total_size(sizeof(struct ifla_cacheinfo))
4974 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4975 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4976 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4977 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4980 static inline size_t inet6_if_nlmsg_size(void)
4982 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4983 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4984 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4985 + nla_total_size(4) /* IFLA_MTU */
4986 + nla_total_size(4) /* IFLA_LINK */
4987 + nla_total_size(1) /* IFLA_OPERSTATE */
4988 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4991 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4992 int bytes)
4994 int i;
4995 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
4996 BUG_ON(pad < 0);
4998 /* Use put_unaligned() because stats may not be aligned for u64. */
4999 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5000 for (i = 1; i < ICMP6_MIB_MAX; i++)
5001 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5003 memset(&stats[ICMP6_MIB_MAX], 0, pad);
5006 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5007 int bytes, size_t syncpoff)
5009 int i, c;
5010 u64 buff[IPSTATS_MIB_MAX];
5011 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5013 BUG_ON(pad < 0);
5015 memset(buff, 0, sizeof(buff));
5016 buff[0] = IPSTATS_MIB_MAX;
5018 for_each_possible_cpu(c) {
5019 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5020 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5023 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5024 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5027 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5028 int bytes)
5030 switch (attrtype) {
5031 case IFLA_INET6_STATS:
5032 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5033 offsetof(struct ipstats_mib, syncp));
5034 break;
5035 case IFLA_INET6_ICMP6STATS:
5036 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5037 break;
5041 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5042 u32 ext_filter_mask)
5044 struct nlattr *nla;
5045 struct ifla_cacheinfo ci;
5047 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5048 goto nla_put_failure;
5049 ci.max_reasm_len = IPV6_MAXPLEN;
5050 ci.tstamp = cstamp_delta(idev->tstamp);
5051 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5052 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5053 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5054 goto nla_put_failure;
5055 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5056 if (!nla)
5057 goto nla_put_failure;
5058 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5060 /* XXX - MC not implemented */
5062 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5063 return 0;
5065 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5066 if (!nla)
5067 goto nla_put_failure;
5068 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5070 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5071 if (!nla)
5072 goto nla_put_failure;
5073 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5075 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5076 if (!nla)
5077 goto nla_put_failure;
5079 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5080 goto nla_put_failure;
5082 read_lock_bh(&idev->lock);
5083 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5084 read_unlock_bh(&idev->lock);
5086 return 0;
5088 nla_put_failure:
5089 return -EMSGSIZE;
5092 static size_t inet6_get_link_af_size(const struct net_device *dev,
5093 u32 ext_filter_mask)
5095 if (!__in6_dev_get(dev))
5096 return 0;
5098 return inet6_ifla6_size();
5101 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5102 u32 ext_filter_mask)
5104 struct inet6_dev *idev = __in6_dev_get(dev);
5106 if (!idev)
5107 return -ENODATA;
5109 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5110 return -EMSGSIZE;
5112 return 0;
5115 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5117 struct inet6_ifaddr *ifp;
5118 struct net_device *dev = idev->dev;
5119 bool clear_token, update_rs = false;
5120 struct in6_addr ll_addr;
5122 ASSERT_RTNL();
5124 if (!token)
5125 return -EINVAL;
5126 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5127 return -EINVAL;
5128 if (!ipv6_accept_ra(idev))
5129 return -EINVAL;
5130 if (idev->cnf.rtr_solicits == 0)
5131 return -EINVAL;
5133 write_lock_bh(&idev->lock);
5135 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5136 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5138 write_unlock_bh(&idev->lock);
5140 clear_token = ipv6_addr_any(token);
5141 if (clear_token)
5142 goto update_lft;
5144 if (!idev->dead && (idev->if_flags & IF_READY) &&
5145 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5146 IFA_F_OPTIMISTIC)) {
5147 /* If we're not ready, then normal ifup will take care
5148 * of this. Otherwise, we need to request our rs here.
5150 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5151 update_rs = true;
5154 update_lft:
5155 write_lock_bh(&idev->lock);
5157 if (update_rs) {
5158 idev->if_flags |= IF_RS_SENT;
5159 idev->rs_interval = rfc3315_s14_backoff_init(
5160 idev->cnf.rtr_solicit_interval);
5161 idev->rs_probes = 1;
5162 addrconf_mod_rs_timer(idev, idev->rs_interval);
5165 /* Well, that's kinda nasty ... */
5166 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5167 spin_lock(&ifp->lock);
5168 if (ifp->tokenized) {
5169 ifp->valid_lft = 0;
5170 ifp->prefered_lft = 0;
5172 spin_unlock(&ifp->lock);
5175 write_unlock_bh(&idev->lock);
5176 inet6_ifinfo_notify(RTM_NEWLINK, idev);
5177 addrconf_verify_rtnl();
5178 return 0;
5181 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5182 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5183 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5186 static int inet6_validate_link_af(const struct net_device *dev,
5187 const struct nlattr *nla)
5189 struct nlattr *tb[IFLA_INET6_MAX + 1];
5191 if (dev && !__in6_dev_get(dev))
5192 return -EAFNOSUPPORT;
5194 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5197 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5199 int err = -EINVAL;
5200 struct inet6_dev *idev = __in6_dev_get(dev);
5201 struct nlattr *tb[IFLA_INET6_MAX + 1];
5203 if (!idev)
5204 return -EAFNOSUPPORT;
5206 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5207 BUG();
5209 if (tb[IFLA_INET6_TOKEN]) {
5210 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
5211 if (err)
5212 return err;
5215 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5216 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5218 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5219 mode != IN6_ADDR_GEN_MODE_NONE &&
5220 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5221 mode != IN6_ADDR_GEN_MODE_RANDOM)
5222 return -EINVAL;
5224 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5225 !idev->cnf.stable_secret.initialized &&
5226 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5227 return -EINVAL;
5229 idev->addr_gen_mode = mode;
5230 err = 0;
5233 return err;
5236 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
5237 u32 portid, u32 seq, int event, unsigned int flags)
5239 struct net_device *dev = idev->dev;
5240 struct ifinfomsg *hdr;
5241 struct nlmsghdr *nlh;
5242 void *protoinfo;
5244 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
5245 if (!nlh)
5246 return -EMSGSIZE;
5248 hdr = nlmsg_data(nlh);
5249 hdr->ifi_family = AF_INET6;
5250 hdr->__ifi_pad = 0;
5251 hdr->ifi_type = dev->type;
5252 hdr->ifi_index = dev->ifindex;
5253 hdr->ifi_flags = dev_get_flags(dev);
5254 hdr->ifi_change = 0;
5256 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5257 (dev->addr_len &&
5258 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5259 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
5260 (dev->ifindex != dev_get_iflink(dev) &&
5261 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5262 nla_put_u8(skb, IFLA_OPERSTATE,
5263 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
5264 goto nla_put_failure;
5265 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
5266 if (!protoinfo)
5267 goto nla_put_failure;
5269 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
5270 goto nla_put_failure;
5272 nla_nest_end(skb, protoinfo);
5273 nlmsg_end(skb, nlh);
5274 return 0;
5276 nla_put_failure:
5277 nlmsg_cancel(skb, nlh);
5278 return -EMSGSIZE;
5281 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5283 struct net *net = sock_net(skb->sk);
5284 int h, s_h;
5285 int idx = 0, s_idx;
5286 struct net_device *dev;
5287 struct inet6_dev *idev;
5288 struct hlist_head *head;
5290 s_h = cb->args[0];
5291 s_idx = cb->args[1];
5293 rcu_read_lock();
5294 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5295 idx = 0;
5296 head = &net->dev_index_head[h];
5297 hlist_for_each_entry_rcu(dev, head, index_hlist) {
5298 if (idx < s_idx)
5299 goto cont;
5300 idev = __in6_dev_get(dev);
5301 if (!idev)
5302 goto cont;
5303 if (inet6_fill_ifinfo(skb, idev,
5304 NETLINK_CB(cb->skb).portid,
5305 cb->nlh->nlmsg_seq,
5306 RTM_NEWLINK, NLM_F_MULTI) < 0)
5307 goto out;
5308 cont:
5309 idx++;
5312 out:
5313 rcu_read_unlock();
5314 cb->args[1] = idx;
5315 cb->args[0] = h;
5317 return skb->len;
5320 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5322 struct sk_buff *skb;
5323 struct net *net = dev_net(idev->dev);
5324 int err = -ENOBUFS;
5326 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
5327 if (!skb)
5328 goto errout;
5330 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
5331 if (err < 0) {
5332 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5333 WARN_ON(err == -EMSGSIZE);
5334 kfree_skb(skb);
5335 goto errout;
5337 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
5338 return;
5339 errout:
5340 if (err < 0)
5341 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
5344 static inline size_t inet6_prefix_nlmsg_size(void)
5346 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5347 + nla_total_size(sizeof(struct in6_addr))
5348 + nla_total_size(sizeof(struct prefix_cacheinfo));
5351 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
5352 struct prefix_info *pinfo, u32 portid, u32 seq,
5353 int event, unsigned int flags)
5355 struct prefixmsg *pmsg;
5356 struct nlmsghdr *nlh;
5357 struct prefix_cacheinfo ci;
5359 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
5360 if (!nlh)
5361 return -EMSGSIZE;
5363 pmsg = nlmsg_data(nlh);
5364 pmsg->prefix_family = AF_INET6;
5365 pmsg->prefix_pad1 = 0;
5366 pmsg->prefix_pad2 = 0;
5367 pmsg->prefix_ifindex = idev->dev->ifindex;
5368 pmsg->prefix_len = pinfo->prefix_len;
5369 pmsg->prefix_type = pinfo->type;
5370 pmsg->prefix_pad3 = 0;
5371 pmsg->prefix_flags = 0;
5372 if (pinfo->onlink)
5373 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5374 if (pinfo->autoconf)
5375 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5377 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5378 goto nla_put_failure;
5379 ci.preferred_time = ntohl(pinfo->prefered);
5380 ci.valid_time = ntohl(pinfo->valid);
5381 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5382 goto nla_put_failure;
5383 nlmsg_end(skb, nlh);
5384 return 0;
5386 nla_put_failure:
5387 nlmsg_cancel(skb, nlh);
5388 return -EMSGSIZE;
5391 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5392 struct prefix_info *pinfo)
5394 struct sk_buff *skb;
5395 struct net *net = dev_net(idev->dev);
5396 int err = -ENOBUFS;
5398 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5399 if (!skb)
5400 goto errout;
5402 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5403 if (err < 0) {
5404 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5405 WARN_ON(err == -EMSGSIZE);
5406 kfree_skb(skb);
5407 goto errout;
5409 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5410 return;
5411 errout:
5412 if (err < 0)
5413 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5416 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5418 struct net *net = dev_net(ifp->idev->dev);
5420 if (event)
5421 ASSERT_RTNL();
5423 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5425 switch (event) {
5426 case RTM_NEWADDR:
5428 * If the address was optimistic
5429 * we inserted the route at the start of
5430 * our DAD process, so we don't need
5431 * to do it again
5433 if (!(ifp->rt->rt6i_node))
5434 ip6_ins_rt(ifp->rt);
5435 if (ifp->idev->cnf.forwarding)
5436 addrconf_join_anycast(ifp);
5437 if (!ipv6_addr_any(&ifp->peer_addr))
5438 addrconf_prefix_route(&ifp->peer_addr, 128,
5439 ifp->idev->dev, 0, 0);
5440 break;
5441 case RTM_DELADDR:
5442 if (ifp->idev->cnf.forwarding)
5443 addrconf_leave_anycast(ifp);
5444 addrconf_leave_solict(ifp->idev, &ifp->addr);
5445 if (!ipv6_addr_any(&ifp->peer_addr)) {
5446 struct rt6_info *rt;
5448 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5449 ifp->idev->dev, 0, 0);
5450 if (rt)
5451 ip6_del_rt(rt);
5453 if (ifp->rt) {
5454 dst_hold(&ifp->rt->dst);
5455 ip6_del_rt(ifp->rt);
5457 rt_genid_bump_ipv6(net);
5458 break;
5460 atomic_inc(&net->ipv6.dev_addr_genid);
5463 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5465 rcu_read_lock_bh();
5466 if (likely(ifp->idev->dead == 0))
5467 __ipv6_ifa_notify(event, ifp);
5468 rcu_read_unlock_bh();
5471 #ifdef CONFIG_SYSCTL
5473 static
5474 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5475 void __user *buffer, size_t *lenp, loff_t *ppos)
5477 int *valp = ctl->data;
5478 int val = *valp;
5479 loff_t pos = *ppos;
5480 struct ctl_table lctl;
5481 int ret;
5484 * ctl->data points to idev->cnf.forwarding, we should
5485 * not modify it until we get the rtnl lock.
5487 lctl = *ctl;
5488 lctl.data = &val;
5490 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5492 if (write)
5493 ret = addrconf_fixup_forwarding(ctl, valp, val);
5494 if (ret)
5495 *ppos = pos;
5496 return ret;
5499 static
5500 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5501 void __user *buffer, size_t *lenp, loff_t *ppos)
5503 struct inet6_dev *idev = ctl->extra1;
5504 int min_mtu = IPV6_MIN_MTU;
5505 struct ctl_table lctl;
5507 lctl = *ctl;
5508 lctl.extra1 = &min_mtu;
5509 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5511 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5514 static void dev_disable_change(struct inet6_dev *idev)
5516 struct netdev_notifier_info info;
5518 if (!idev || !idev->dev)
5519 return;
5521 netdev_notifier_info_init(&info, idev->dev);
5522 if (idev->cnf.disable_ipv6)
5523 addrconf_notify(NULL, NETDEV_DOWN, &info);
5524 else
5525 addrconf_notify(NULL, NETDEV_UP, &info);
5528 static void addrconf_disable_change(struct net *net, __s32 newf)
5530 struct net_device *dev;
5531 struct inet6_dev *idev;
5533 rcu_read_lock();
5534 for_each_netdev_rcu(net, dev) {
5535 idev = __in6_dev_get(dev);
5536 if (idev) {
5537 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5538 idev->cnf.disable_ipv6 = newf;
5539 if (changed)
5540 dev_disable_change(idev);
5543 rcu_read_unlock();
5546 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5548 struct net *net;
5549 int old;
5551 if (!rtnl_trylock())
5552 return restart_syscall();
5554 net = (struct net *)table->extra2;
5555 old = *p;
5556 *p = newf;
5558 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5559 rtnl_unlock();
5560 return 0;
5563 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5564 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5565 addrconf_disable_change(net, newf);
5566 } else if ((!newf) ^ (!old))
5567 dev_disable_change((struct inet6_dev *)table->extra1);
5569 rtnl_unlock();
5570 return 0;
5573 static
5574 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5575 void __user *buffer, size_t *lenp, loff_t *ppos)
5577 int *valp = ctl->data;
5578 int val = *valp;
5579 loff_t pos = *ppos;
5580 struct ctl_table lctl;
5581 int ret;
5584 * ctl->data points to idev->cnf.disable_ipv6, we should
5585 * not modify it until we get the rtnl lock.
5587 lctl = *ctl;
5588 lctl.data = &val;
5590 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5592 if (write)
5593 ret = addrconf_disable_ipv6(ctl, valp, val);
5594 if (ret)
5595 *ppos = pos;
5596 return ret;
5599 static
5600 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5601 void __user *buffer, size_t *lenp, loff_t *ppos)
5603 int *valp = ctl->data;
5604 int ret;
5605 int old, new;
5607 old = *valp;
5608 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5609 new = *valp;
5611 if (write && old != new) {
5612 struct net *net = ctl->extra2;
5614 if (!rtnl_trylock())
5615 return restart_syscall();
5617 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5618 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5619 NETCONFA_IFINDEX_DEFAULT,
5620 net->ipv6.devconf_dflt);
5621 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5622 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5623 NETCONFA_IFINDEX_ALL,
5624 net->ipv6.devconf_all);
5625 else {
5626 struct inet6_dev *idev = ctl->extra1;
5628 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5629 idev->dev->ifindex,
5630 &idev->cnf);
5632 rtnl_unlock();
5635 return ret;
5638 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5639 void __user *buffer, size_t *lenp,
5640 loff_t *ppos)
5642 int err;
5643 struct in6_addr addr;
5644 char str[IPV6_MAX_STRLEN];
5645 struct ctl_table lctl = *ctl;
5646 struct net *net = ctl->extra2;
5647 struct ipv6_stable_secret *secret = ctl->data;
5649 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5650 return -EIO;
5652 lctl.maxlen = IPV6_MAX_STRLEN;
5653 lctl.data = str;
5655 if (!rtnl_trylock())
5656 return restart_syscall();
5658 if (!write && !secret->initialized) {
5659 err = -EIO;
5660 goto out;
5663 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5664 if (err >= sizeof(str)) {
5665 err = -EIO;
5666 goto out;
5669 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5670 if (err || !write)
5671 goto out;
5673 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5674 err = -EIO;
5675 goto out;
5678 secret->initialized = true;
5679 secret->secret = addr;
5681 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5682 struct net_device *dev;
5684 for_each_netdev(net, dev) {
5685 struct inet6_dev *idev = __in6_dev_get(dev);
5687 if (idev) {
5688 idev->addr_gen_mode =
5689 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5692 } else {
5693 struct inet6_dev *idev = ctl->extra1;
5695 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5698 out:
5699 rtnl_unlock();
5701 return err;
5704 static
5705 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5706 int write,
5707 void __user *buffer,
5708 size_t *lenp,
5709 loff_t *ppos)
5711 int *valp = ctl->data;
5712 int val = *valp;
5713 loff_t pos = *ppos;
5714 struct ctl_table lctl;
5715 int ret;
5717 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5718 * we should not modify it until we get the rtnl lock.
5720 lctl = *ctl;
5721 lctl.data = &val;
5723 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5725 if (write)
5726 ret = addrconf_fixup_linkdown(ctl, valp, val);
5727 if (ret)
5728 *ppos = pos;
5729 return ret;
5732 static int minus_one = -1;
5733 static const int one = 1;
5734 static const int two_five_five = 255;
5736 static const struct ctl_table addrconf_sysctl[] = {
5738 .procname = "forwarding",
5739 .data = &ipv6_devconf.forwarding,
5740 .maxlen = sizeof(int),
5741 .mode = 0644,
5742 .proc_handler = addrconf_sysctl_forward,
5745 .procname = "hop_limit",
5746 .data = &ipv6_devconf.hop_limit,
5747 .maxlen = sizeof(int),
5748 .mode = 0644,
5749 .proc_handler = proc_dointvec_minmax,
5750 .extra1 = (void *)&one,
5751 .extra2 = (void *)&two_five_five,
5754 .procname = "mtu",
5755 .data = &ipv6_devconf.mtu6,
5756 .maxlen = sizeof(int),
5757 .mode = 0644,
5758 .proc_handler = addrconf_sysctl_mtu,
5761 .procname = "accept_ra",
5762 .data = &ipv6_devconf.accept_ra,
5763 .maxlen = sizeof(int),
5764 .mode = 0644,
5765 .proc_handler = proc_dointvec,
5768 .procname = "accept_redirects",
5769 .data = &ipv6_devconf.accept_redirects,
5770 .maxlen = sizeof(int),
5771 .mode = 0644,
5772 .proc_handler = proc_dointvec,
5775 .procname = "autoconf",
5776 .data = &ipv6_devconf.autoconf,
5777 .maxlen = sizeof(int),
5778 .mode = 0644,
5779 .proc_handler = proc_dointvec,
5782 .procname = "dad_transmits",
5783 .data = &ipv6_devconf.dad_transmits,
5784 .maxlen = sizeof(int),
5785 .mode = 0644,
5786 .proc_handler = proc_dointvec,
5789 .procname = "router_solicitations",
5790 .data = &ipv6_devconf.rtr_solicits,
5791 .maxlen = sizeof(int),
5792 .mode = 0644,
5793 .proc_handler = proc_dointvec_minmax,
5794 .extra1 = &minus_one,
5797 .procname = "router_solicitation_interval",
5798 .data = &ipv6_devconf.rtr_solicit_interval,
5799 .maxlen = sizeof(int),
5800 .mode = 0644,
5801 .proc_handler = proc_dointvec_jiffies,
5804 .procname = "router_solicitation_max_interval",
5805 .data = &ipv6_devconf.rtr_solicit_max_interval,
5806 .maxlen = sizeof(int),
5807 .mode = 0644,
5808 .proc_handler = proc_dointvec_jiffies,
5811 .procname = "router_solicitation_delay",
5812 .data = &ipv6_devconf.rtr_solicit_delay,
5813 .maxlen = sizeof(int),
5814 .mode = 0644,
5815 .proc_handler = proc_dointvec_jiffies,
5818 .procname = "force_mld_version",
5819 .data = &ipv6_devconf.force_mld_version,
5820 .maxlen = sizeof(int),
5821 .mode = 0644,
5822 .proc_handler = proc_dointvec,
5825 .procname = "mldv1_unsolicited_report_interval",
5826 .data =
5827 &ipv6_devconf.mldv1_unsolicited_report_interval,
5828 .maxlen = sizeof(int),
5829 .mode = 0644,
5830 .proc_handler = proc_dointvec_ms_jiffies,
5833 .procname = "mldv2_unsolicited_report_interval",
5834 .data =
5835 &ipv6_devconf.mldv2_unsolicited_report_interval,
5836 .maxlen = sizeof(int),
5837 .mode = 0644,
5838 .proc_handler = proc_dointvec_ms_jiffies,
5841 .procname = "use_tempaddr",
5842 .data = &ipv6_devconf.use_tempaddr,
5843 .maxlen = sizeof(int),
5844 .mode = 0644,
5845 .proc_handler = proc_dointvec,
5848 .procname = "temp_valid_lft",
5849 .data = &ipv6_devconf.temp_valid_lft,
5850 .maxlen = sizeof(int),
5851 .mode = 0644,
5852 .proc_handler = proc_dointvec,
5855 .procname = "temp_prefered_lft",
5856 .data = &ipv6_devconf.temp_prefered_lft,
5857 .maxlen = sizeof(int),
5858 .mode = 0644,
5859 .proc_handler = proc_dointvec,
5862 .procname = "regen_max_retry",
5863 .data = &ipv6_devconf.regen_max_retry,
5864 .maxlen = sizeof(int),
5865 .mode = 0644,
5866 .proc_handler = proc_dointvec,
5869 .procname = "max_desync_factor",
5870 .data = &ipv6_devconf.max_desync_factor,
5871 .maxlen = sizeof(int),
5872 .mode = 0644,
5873 .proc_handler = proc_dointvec,
5876 .procname = "max_addresses",
5877 .data = &ipv6_devconf.max_addresses,
5878 .maxlen = sizeof(int),
5879 .mode = 0644,
5880 .proc_handler = proc_dointvec,
5883 .procname = "accept_ra_defrtr",
5884 .data = &ipv6_devconf.accept_ra_defrtr,
5885 .maxlen = sizeof(int),
5886 .mode = 0644,
5887 .proc_handler = proc_dointvec,
5890 .procname = "accept_ra_min_hop_limit",
5891 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5892 .maxlen = sizeof(int),
5893 .mode = 0644,
5894 .proc_handler = proc_dointvec,
5897 .procname = "accept_ra_pinfo",
5898 .data = &ipv6_devconf.accept_ra_pinfo,
5899 .maxlen = sizeof(int),
5900 .mode = 0644,
5901 .proc_handler = proc_dointvec,
5903 #ifdef CONFIG_IPV6_ROUTER_PREF
5905 .procname = "accept_ra_rtr_pref",
5906 .data = &ipv6_devconf.accept_ra_rtr_pref,
5907 .maxlen = sizeof(int),
5908 .mode = 0644,
5909 .proc_handler = proc_dointvec,
5912 .procname = "router_probe_interval",
5913 .data = &ipv6_devconf.rtr_probe_interval,
5914 .maxlen = sizeof(int),
5915 .mode = 0644,
5916 .proc_handler = proc_dointvec_jiffies,
5918 #ifdef CONFIG_IPV6_ROUTE_INFO
5920 .procname = "accept_ra_rt_info_max_plen",
5921 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5922 .maxlen = sizeof(int),
5923 .mode = 0644,
5924 .proc_handler = proc_dointvec,
5926 #endif
5927 #endif
5929 .procname = "proxy_ndp",
5930 .data = &ipv6_devconf.proxy_ndp,
5931 .maxlen = sizeof(int),
5932 .mode = 0644,
5933 .proc_handler = addrconf_sysctl_proxy_ndp,
5936 .procname = "accept_source_route",
5937 .data = &ipv6_devconf.accept_source_route,
5938 .maxlen = sizeof(int),
5939 .mode = 0644,
5940 .proc_handler = proc_dointvec,
5942 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5944 .procname = "optimistic_dad",
5945 .data = &ipv6_devconf.optimistic_dad,
5946 .maxlen = sizeof(int),
5947 .mode = 0644,
5948 .proc_handler = proc_dointvec,
5951 .procname = "use_optimistic",
5952 .data = &ipv6_devconf.use_optimistic,
5953 .maxlen = sizeof(int),
5954 .mode = 0644,
5955 .proc_handler = proc_dointvec,
5957 #endif
5958 #ifdef CONFIG_IPV6_MROUTE
5960 .procname = "mc_forwarding",
5961 .data = &ipv6_devconf.mc_forwarding,
5962 .maxlen = sizeof(int),
5963 .mode = 0444,
5964 .proc_handler = proc_dointvec,
5966 #endif
5968 .procname = "disable_ipv6",
5969 .data = &ipv6_devconf.disable_ipv6,
5970 .maxlen = sizeof(int),
5971 .mode = 0644,
5972 .proc_handler = addrconf_sysctl_disable,
5975 .procname = "accept_dad",
5976 .data = &ipv6_devconf.accept_dad,
5977 .maxlen = sizeof(int),
5978 .mode = 0644,
5979 .proc_handler = proc_dointvec,
5982 .procname = "force_tllao",
5983 .data = &ipv6_devconf.force_tllao,
5984 .maxlen = sizeof(int),
5985 .mode = 0644,
5986 .proc_handler = proc_dointvec
5989 .procname = "ndisc_notify",
5990 .data = &ipv6_devconf.ndisc_notify,
5991 .maxlen = sizeof(int),
5992 .mode = 0644,
5993 .proc_handler = proc_dointvec
5996 .procname = "suppress_frag_ndisc",
5997 .data = &ipv6_devconf.suppress_frag_ndisc,
5998 .maxlen = sizeof(int),
5999 .mode = 0644,
6000 .proc_handler = proc_dointvec
6003 .procname = "accept_ra_from_local",
6004 .data = &ipv6_devconf.accept_ra_from_local,
6005 .maxlen = sizeof(int),
6006 .mode = 0644,
6007 .proc_handler = proc_dointvec,
6010 .procname = "accept_ra_mtu",
6011 .data = &ipv6_devconf.accept_ra_mtu,
6012 .maxlen = sizeof(int),
6013 .mode = 0644,
6014 .proc_handler = proc_dointvec,
6017 .procname = "stable_secret",
6018 .data = &ipv6_devconf.stable_secret,
6019 .maxlen = IPV6_MAX_STRLEN,
6020 .mode = 0600,
6021 .proc_handler = addrconf_sysctl_stable_secret,
6024 .procname = "use_oif_addrs_only",
6025 .data = &ipv6_devconf.use_oif_addrs_only,
6026 .maxlen = sizeof(int),
6027 .mode = 0644,
6028 .proc_handler = proc_dointvec,
6031 .procname = "ignore_routes_with_linkdown",
6032 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6033 .maxlen = sizeof(int),
6034 .mode = 0644,
6035 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6038 .procname = "drop_unicast_in_l2_multicast",
6039 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6040 .maxlen = sizeof(int),
6041 .mode = 0644,
6042 .proc_handler = proc_dointvec,
6045 .procname = "drop_unsolicited_na",
6046 .data = &ipv6_devconf.drop_unsolicited_na,
6047 .maxlen = sizeof(int),
6048 .mode = 0644,
6049 .proc_handler = proc_dointvec,
6052 .procname = "keep_addr_on_down",
6053 .data = &ipv6_devconf.keep_addr_on_down,
6054 .maxlen = sizeof(int),
6055 .mode = 0644,
6056 .proc_handler = proc_dointvec,
6060 /* sentinel */
6064 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
6065 struct inet6_dev *idev, struct ipv6_devconf *p)
6067 int i, ifindex;
6068 struct ctl_table *table;
6069 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
6071 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6072 if (!table)
6073 goto out;
6075 for (i = 0; table[i].data; i++) {
6076 table[i].data += (char *)p - (char *)&ipv6_devconf;
6077 /* If one of these is already set, then it is not safe to
6078 * overwrite either of them: this makes proc_dointvec_minmax
6079 * usable.
6081 if (!table[i].extra1 && !table[i].extra2) {
6082 table[i].extra1 = idev; /* embedded; no ref */
6083 table[i].extra2 = net;
6087 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
6089 p->sysctl_header = register_net_sysctl(net, path, table);
6090 if (!p->sysctl_header)
6091 goto free;
6093 if (!strcmp(dev_name, "all"))
6094 ifindex = NETCONFA_IFINDEX_ALL;
6095 else if (!strcmp(dev_name, "default"))
6096 ifindex = NETCONFA_IFINDEX_DEFAULT;
6097 else
6098 ifindex = idev->dev->ifindex;
6099 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
6100 return 0;
6102 free:
6103 kfree(table);
6104 out:
6105 return -ENOBUFS;
6108 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6110 struct ctl_table *table;
6112 if (!p->sysctl_header)
6113 return;
6115 table = p->sysctl_header->ctl_table_arg;
6116 unregister_net_sysctl_table(p->sysctl_header);
6117 p->sysctl_header = NULL;
6118 kfree(table);
6121 static int addrconf_sysctl_register(struct inet6_dev *idev)
6123 int err;
6125 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6126 return -EINVAL;
6128 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6129 &ndisc_ifinfo_sysctl_change);
6130 if (err)
6131 return err;
6132 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6133 idev, &idev->cnf);
6134 if (err)
6135 neigh_sysctl_unregister(idev->nd_parms);
6137 return err;
6140 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
6142 __addrconf_sysctl_unregister(&idev->cnf);
6143 neigh_sysctl_unregister(idev->nd_parms);
6147 #endif
6149 static int __net_init addrconf_init_net(struct net *net)
6151 int err = -ENOMEM;
6152 struct ipv6_devconf *all, *dflt;
6154 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
6155 if (!all)
6156 goto err_alloc_all;
6158 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
6159 if (!dflt)
6160 goto err_alloc_dflt;
6162 /* these will be inherited by all namespaces */
6163 dflt->autoconf = ipv6_defaults.autoconf;
6164 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
6166 dflt->stable_secret.initialized = false;
6167 all->stable_secret.initialized = false;
6169 net->ipv6.devconf_all = all;
6170 net->ipv6.devconf_dflt = dflt;
6172 #ifdef CONFIG_SYSCTL
6173 err = __addrconf_sysctl_register(net, "all", NULL, all);
6174 if (err < 0)
6175 goto err_reg_all;
6177 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
6178 if (err < 0)
6179 goto err_reg_dflt;
6180 #endif
6181 return 0;
6183 #ifdef CONFIG_SYSCTL
6184 err_reg_dflt:
6185 __addrconf_sysctl_unregister(all);
6186 err_reg_all:
6187 kfree(dflt);
6188 #endif
6189 err_alloc_dflt:
6190 kfree(all);
6191 err_alloc_all:
6192 return err;
6195 static void __net_exit addrconf_exit_net(struct net *net)
6197 #ifdef CONFIG_SYSCTL
6198 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6199 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6200 #endif
6201 kfree(net->ipv6.devconf_dflt);
6202 kfree(net->ipv6.devconf_all);
6205 static struct pernet_operations addrconf_ops = {
6206 .init = addrconf_init_net,
6207 .exit = addrconf_exit_net,
6210 static struct rtnl_af_ops inet6_ops __read_mostly = {
6211 .family = AF_INET6,
6212 .fill_link_af = inet6_fill_link_af,
6213 .get_link_af_size = inet6_get_link_af_size,
6214 .validate_link_af = inet6_validate_link_af,
6215 .set_link_af = inet6_set_link_af,
6219 * Init / cleanup code
6222 int __init addrconf_init(void)
6224 struct inet6_dev *idev;
6225 int i, err;
6227 err = ipv6_addr_label_init();
6228 if (err < 0) {
6229 pr_crit("%s: cannot initialize default policy table: %d\n",
6230 __func__, err);
6231 goto out;
6234 err = register_pernet_subsys(&addrconf_ops);
6235 if (err < 0)
6236 goto out_addrlabel;
6238 addrconf_wq = create_workqueue("ipv6_addrconf");
6239 if (!addrconf_wq) {
6240 err = -ENOMEM;
6241 goto out_nowq;
6244 /* The addrconf netdev notifier requires that loopback_dev
6245 * has it's ipv6 private information allocated and setup
6246 * before it can bring up and give link-local addresses
6247 * to other devices which are up.
6249 * Unfortunately, loopback_dev is not necessarily the first
6250 * entry in the global dev_base list of net devices. In fact,
6251 * it is likely to be the very last entry on that list.
6252 * So this causes the notifier registry below to try and
6253 * give link-local addresses to all devices besides loopback_dev
6254 * first, then loopback_dev, which cases all the non-loopback_dev
6255 * devices to fail to get a link-local address.
6257 * So, as a temporary fix, allocate the ipv6 structure for
6258 * loopback_dev first by hand.
6259 * Longer term, all of the dependencies ipv6 has upon the loopback
6260 * device and it being up should be removed.
6262 rtnl_lock();
6263 idev = ipv6_add_dev(init_net.loopback_dev);
6264 rtnl_unlock();
6265 if (IS_ERR(idev)) {
6266 err = PTR_ERR(idev);
6267 goto errlo;
6270 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6271 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6273 register_netdevice_notifier(&ipv6_dev_notf);
6275 addrconf_verify();
6277 rtnl_af_register(&inet6_ops);
6279 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6280 NULL);
6281 if (err < 0)
6282 goto errout;
6284 /* Only the first call to __rtnl_register can fail */
6285 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6286 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6287 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6288 inet6_dump_ifaddr, NULL);
6289 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6290 inet6_dump_ifmcaddr, NULL);
6291 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6292 inet6_dump_ifacaddr, NULL);
6293 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
6294 inet6_netconf_dump_devconf, NULL);
6296 ipv6_addr_label_rtnl_register();
6298 return 0;
6299 errout:
6300 rtnl_af_unregister(&inet6_ops);
6301 unregister_netdevice_notifier(&ipv6_dev_notf);
6302 errlo:
6303 destroy_workqueue(addrconf_wq);
6304 out_nowq:
6305 unregister_pernet_subsys(&addrconf_ops);
6306 out_addrlabel:
6307 ipv6_addr_label_cleanup();
6308 out:
6309 return err;
6312 void addrconf_cleanup(void)
6314 struct net_device *dev;
6315 int i;
6317 unregister_netdevice_notifier(&ipv6_dev_notf);
6318 unregister_pernet_subsys(&addrconf_ops);
6319 ipv6_addr_label_cleanup();
6321 rtnl_lock();
6323 __rtnl_af_unregister(&inet6_ops);
6325 /* clean dev list */
6326 for_each_netdev(&init_net, dev) {
6327 if (__in6_dev_get(dev) == NULL)
6328 continue;
6329 addrconf_ifdown(dev, 1);
6331 addrconf_ifdown(init_net.loopback_dev, 2);
6334 * Check hash table.
6336 spin_lock_bh(&addrconf_hash_lock);
6337 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6338 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
6339 spin_unlock_bh(&addrconf_hash_lock);
6340 cancel_delayed_work(&addr_chk_work);
6341 rtnl_unlock();
6343 destroy_workqueue(addrconf_wq);