]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/addrconf.c
ipv6: sysctl to restrict candidate source addresses
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / addrconf.c
1 /*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
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.
13 */
14
15 /*
16 * Changes:
17 *
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.
39 */
40
41 #define pr_fmt(fmt) "IPv6: " fmt
42
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>
68
69 #include <net/net_namespace.h>
70 #include <net/sock.h>
71 #include <net/snmp.h>
72
73 #include <net/af_ieee802154.h>
74 #include <net/firewire.h>
75 #include <net/ipv6.h>
76 #include <net/protocol.h>
77 #include <net/ndisc.h>
78 #include <net/ip6_route.h>
79 #include <net/addrconf.h>
80 #include <net/tcp.h>
81 #include <net/ip.h>
82 #include <net/netlink.h>
83 #include <net/pkt_sched.h>
84 #include <linux/if_tunnel.h>
85 #include <linux/rtnetlink.h>
86 #include <linux/netconf.h>
87 #include <linux/random.h>
88 #include <linux/uaccess.h>
89 #include <asm/unaligned.h>
90
91 #include <linux/proc_fs.h>
92 #include <linux/seq_file.h>
93 #include <linux/export.h>
94
95 /* Set to 3 to get tracing... */
96 #define ACONF_DEBUG 2
97
98 #if ACONF_DEBUG >= 3
99 #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
100 #else
101 #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
102 #endif
103
104 #define INFINITY_LIFE_TIME 0xFFFFFFFF
105
106 #define IPV6_MAX_STRLEN \
107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
108
109 static inline u32 cstamp_delta(unsigned long cstamp)
110 {
111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
112 }
113
114 #ifdef CONFIG_SYSCTL
115 static int addrconf_sysctl_register(struct inet6_dev *idev);
116 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117 #else
118 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
119 {
120 return 0;
121 }
122
123 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
124 {
125 }
126 #endif
127
128 static void __ipv6_regen_rndid(struct inet6_dev *idev);
129 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
130 static void ipv6_regen_rndid(unsigned long data);
131
132 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
133 static int ipv6_count_addresses(struct inet6_dev *idev);
134 static int ipv6_generate_stable_address(struct in6_addr *addr,
135 u8 dad_count,
136 const struct inet6_dev *idev);
137
138 /*
139 * Configured unicast address hash table
140 */
141 static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
142 static DEFINE_SPINLOCK(addrconf_hash_lock);
143
144 static void addrconf_verify(void);
145 static void addrconf_verify_rtnl(void);
146 static void addrconf_verify_work(struct work_struct *);
147
148 static struct workqueue_struct *addrconf_wq;
149 static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
150
151 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
152 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
153
154 static void addrconf_type_change(struct net_device *dev,
155 unsigned long event);
156 static int addrconf_ifdown(struct net_device *dev, int how);
157
158 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
159 int plen,
160 const struct net_device *dev,
161 u32 flags, u32 noflags);
162
163 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
164 static void addrconf_dad_work(struct work_struct *w);
165 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
166 static void addrconf_dad_run(struct inet6_dev *idev);
167 static void addrconf_rs_timer(unsigned long data);
168 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
169 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
170
171 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
172 struct prefix_info *pinfo);
173 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
174 struct net_device *dev);
175
176 static struct ipv6_devconf ipv6_devconf __read_mostly = {
177 .forwarding = 0,
178 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
179 .mtu6 = IPV6_MIN_MTU,
180 .accept_ra = 1,
181 .accept_redirects = 1,
182 .autoconf = 1,
183 .force_mld_version = 0,
184 .mldv1_unsolicited_report_interval = 10 * HZ,
185 .mldv2_unsolicited_report_interval = HZ,
186 .dad_transmits = 1,
187 .rtr_solicits = MAX_RTR_SOLICITATIONS,
188 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
189 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
190 .use_tempaddr = 0,
191 .temp_valid_lft = TEMP_VALID_LIFETIME,
192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
193 .regen_max_retry = REGEN_MAX_RETRY,
194 .max_desync_factor = MAX_DESYNC_FACTOR,
195 .max_addresses = IPV6_MAX_ADDRESSES,
196 .accept_ra_defrtr = 1,
197 .accept_ra_from_local = 0,
198 .accept_ra_pinfo = 1,
199 #ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref = 1,
201 .rtr_probe_interval = 60 * HZ,
202 #ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen = 0,
204 #endif
205 #endif
206 .proxy_ndp = 0,
207 .accept_source_route = 0, /* we do not accept RH0 by default. */
208 .disable_ipv6 = 0,
209 .accept_dad = 1,
210 .suppress_frag_ndisc = 1,
211 .accept_ra_mtu = 1,
212 .stable_secret = {
213 .initialized = false,
214 },
215 .use_oif_addrs_only = 0,
216 };
217
218 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
219 .forwarding = 0,
220 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
221 .mtu6 = IPV6_MIN_MTU,
222 .accept_ra = 1,
223 .accept_redirects = 1,
224 .autoconf = 1,
225 .force_mld_version = 0,
226 .mldv1_unsolicited_report_interval = 10 * HZ,
227 .mldv2_unsolicited_report_interval = HZ,
228 .dad_transmits = 1,
229 .rtr_solicits = MAX_RTR_SOLICITATIONS,
230 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
231 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
232 .use_tempaddr = 0,
233 .temp_valid_lft = TEMP_VALID_LIFETIME,
234 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
235 .regen_max_retry = REGEN_MAX_RETRY,
236 .max_desync_factor = MAX_DESYNC_FACTOR,
237 .max_addresses = IPV6_MAX_ADDRESSES,
238 .accept_ra_defrtr = 1,
239 .accept_ra_from_local = 0,
240 .accept_ra_pinfo = 1,
241 #ifdef CONFIG_IPV6_ROUTER_PREF
242 .accept_ra_rtr_pref = 1,
243 .rtr_probe_interval = 60 * HZ,
244 #ifdef CONFIG_IPV6_ROUTE_INFO
245 .accept_ra_rt_info_max_plen = 0,
246 #endif
247 #endif
248 .proxy_ndp = 0,
249 .accept_source_route = 0, /* we do not accept RH0 by default. */
250 .disable_ipv6 = 0,
251 .accept_dad = 1,
252 .suppress_frag_ndisc = 1,
253 .accept_ra_mtu = 1,
254 .stable_secret = {
255 .initialized = false,
256 },
257 .use_oif_addrs_only = 0,
258 };
259
260 /* Check if a valid qdisc is available */
261 static inline bool addrconf_qdisc_ok(const struct net_device *dev)
262 {
263 return !qdisc_tx_is_noop(dev);
264 }
265
266 static void addrconf_del_rs_timer(struct inet6_dev *idev)
267 {
268 if (del_timer(&idev->rs_timer))
269 __in6_dev_put(idev);
270 }
271
272 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
273 {
274 if (cancel_delayed_work(&ifp->dad_work))
275 __in6_ifa_put(ifp);
276 }
277
278 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
279 unsigned long when)
280 {
281 if (!timer_pending(&idev->rs_timer))
282 in6_dev_hold(idev);
283 mod_timer(&idev->rs_timer, jiffies + when);
284 }
285
286 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
287 unsigned long delay)
288 {
289 if (!delayed_work_pending(&ifp->dad_work))
290 in6_ifa_hold(ifp);
291 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
292 }
293
294 static int snmp6_alloc_dev(struct inet6_dev *idev)
295 {
296 int i;
297
298 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
299 if (!idev->stats.ipv6)
300 goto err_ip;
301
302 for_each_possible_cpu(i) {
303 struct ipstats_mib *addrconf_stats;
304 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
305 u64_stats_init(&addrconf_stats->syncp);
306 }
307
308
309 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
310 GFP_KERNEL);
311 if (!idev->stats.icmpv6dev)
312 goto err_icmp;
313 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
314 GFP_KERNEL);
315 if (!idev->stats.icmpv6msgdev)
316 goto err_icmpmsg;
317
318 return 0;
319
320 err_icmpmsg:
321 kfree(idev->stats.icmpv6dev);
322 err_icmp:
323 free_percpu(idev->stats.ipv6);
324 err_ip:
325 return -ENOMEM;
326 }
327
328 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
329 {
330 struct inet6_dev *ndev;
331 int err = -ENOMEM;
332
333 ASSERT_RTNL();
334
335 if (dev->mtu < IPV6_MIN_MTU)
336 return ERR_PTR(-EINVAL);
337
338 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
339 if (!ndev)
340 return ERR_PTR(err);
341
342 rwlock_init(&ndev->lock);
343 ndev->dev = dev;
344 INIT_LIST_HEAD(&ndev->addr_list);
345 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
346 (unsigned long)ndev);
347 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
348 ndev->cnf.mtu6 = dev->mtu;
349 ndev->cnf.sysctl = NULL;
350 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
351 if (!ndev->nd_parms) {
352 kfree(ndev);
353 return ERR_PTR(err);
354 }
355 if (ndev->cnf.forwarding)
356 dev_disable_lro(dev);
357 /* We refer to the device */
358 dev_hold(dev);
359
360 if (snmp6_alloc_dev(ndev) < 0) {
361 ADBG(KERN_WARNING
362 "%s: cannot allocate memory for statistics; dev=%s.\n",
363 __func__, dev->name);
364 neigh_parms_release(&nd_tbl, ndev->nd_parms);
365 dev_put(dev);
366 kfree(ndev);
367 return ERR_PTR(err);
368 }
369
370 if (snmp6_register_dev(ndev) < 0) {
371 ADBG(KERN_WARNING
372 "%s: cannot create /proc/net/dev_snmp6/%s\n",
373 __func__, dev->name);
374 goto err_release;
375 }
376
377 /* One reference from device. We must do this before
378 * we invoke __ipv6_regen_rndid().
379 */
380 in6_dev_hold(ndev);
381
382 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
383 ndev->cnf.accept_dad = -1;
384
385 #if IS_ENABLED(CONFIG_IPV6_SIT)
386 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
387 pr_info("%s: Disabled Multicast RS\n", dev->name);
388 ndev->cnf.rtr_solicits = 0;
389 }
390 #endif
391
392 INIT_LIST_HEAD(&ndev->tempaddr_list);
393 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
394 if ((dev->flags&IFF_LOOPBACK) ||
395 dev->type == ARPHRD_TUNNEL ||
396 dev->type == ARPHRD_TUNNEL6 ||
397 dev->type == ARPHRD_SIT ||
398 dev->type == ARPHRD_NONE) {
399 ndev->cnf.use_tempaddr = -1;
400 } else {
401 in6_dev_hold(ndev);
402 ipv6_regen_rndid((unsigned long) ndev);
403 }
404
405 ndev->token = in6addr_any;
406
407 if (netif_running(dev) && addrconf_qdisc_ok(dev))
408 ndev->if_flags |= IF_READY;
409
410 ipv6_mc_init_dev(ndev);
411 ndev->tstamp = jiffies;
412 err = addrconf_sysctl_register(ndev);
413 if (err) {
414 ipv6_mc_destroy_dev(ndev);
415 del_timer(&ndev->regen_timer);
416 goto err_release;
417 }
418 /* protected by rtnl_lock */
419 rcu_assign_pointer(dev->ip6_ptr, ndev);
420
421 /* Join interface-local all-node multicast group */
422 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
423
424 /* Join all-node multicast group */
425 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
426
427 /* Join all-router multicast group if forwarding is set */
428 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
429 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
430
431 return ndev;
432
433 err_release:
434 neigh_parms_release(&nd_tbl, ndev->nd_parms);
435 ndev->dead = 1;
436 in6_dev_finish_destroy(ndev);
437 return ERR_PTR(err);
438 }
439
440 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
441 {
442 struct inet6_dev *idev;
443
444 ASSERT_RTNL();
445
446 idev = __in6_dev_get(dev);
447 if (!idev) {
448 idev = ipv6_add_dev(dev);
449 if (IS_ERR(idev))
450 return NULL;
451 }
452
453 if (dev->flags&IFF_UP)
454 ipv6_mc_up(idev);
455 return idev;
456 }
457
458 static int inet6_netconf_msgsize_devconf(int type)
459 {
460 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
461 + nla_total_size(4); /* NETCONFA_IFINDEX */
462
463 /* type -1 is used for ALL */
464 if (type == -1 || type == NETCONFA_FORWARDING)
465 size += nla_total_size(4);
466 #ifdef CONFIG_IPV6_MROUTE
467 if (type == -1 || type == NETCONFA_MC_FORWARDING)
468 size += nla_total_size(4);
469 #endif
470 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
471 size += nla_total_size(4);
472
473 return size;
474 }
475
476 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
477 struct ipv6_devconf *devconf, u32 portid,
478 u32 seq, int event, unsigned int flags,
479 int type)
480 {
481 struct nlmsghdr *nlh;
482 struct netconfmsg *ncm;
483
484 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
485 flags);
486 if (!nlh)
487 return -EMSGSIZE;
488
489 ncm = nlmsg_data(nlh);
490 ncm->ncm_family = AF_INET6;
491
492 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
493 goto nla_put_failure;
494
495 /* type -1 is used for ALL */
496 if ((type == -1 || type == NETCONFA_FORWARDING) &&
497 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
498 goto nla_put_failure;
499 #ifdef CONFIG_IPV6_MROUTE
500 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
501 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
502 devconf->mc_forwarding) < 0)
503 goto nla_put_failure;
504 #endif
505 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
506 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
507 goto nla_put_failure;
508
509 nlmsg_end(skb, nlh);
510 return 0;
511
512 nla_put_failure:
513 nlmsg_cancel(skb, nlh);
514 return -EMSGSIZE;
515 }
516
517 void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
518 struct ipv6_devconf *devconf)
519 {
520 struct sk_buff *skb;
521 int err = -ENOBUFS;
522
523 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
524 if (!skb)
525 goto errout;
526
527 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
528 RTM_NEWNETCONF, 0, type);
529 if (err < 0) {
530 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
531 WARN_ON(err == -EMSGSIZE);
532 kfree_skb(skb);
533 goto errout;
534 }
535 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
536 return;
537 errout:
538 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
539 }
540
541 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
542 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
543 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
544 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
545 };
546
547 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
548 struct nlmsghdr *nlh)
549 {
550 struct net *net = sock_net(in_skb->sk);
551 struct nlattr *tb[NETCONFA_MAX+1];
552 struct netconfmsg *ncm;
553 struct sk_buff *skb;
554 struct ipv6_devconf *devconf;
555 struct inet6_dev *in6_dev;
556 struct net_device *dev;
557 int ifindex;
558 int err;
559
560 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
561 devconf_ipv6_policy);
562 if (err < 0)
563 goto errout;
564
565 err = EINVAL;
566 if (!tb[NETCONFA_IFINDEX])
567 goto errout;
568
569 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
570 switch (ifindex) {
571 case NETCONFA_IFINDEX_ALL:
572 devconf = net->ipv6.devconf_all;
573 break;
574 case NETCONFA_IFINDEX_DEFAULT:
575 devconf = net->ipv6.devconf_dflt;
576 break;
577 default:
578 dev = __dev_get_by_index(net, ifindex);
579 if (!dev)
580 goto errout;
581 in6_dev = __in6_dev_get(dev);
582 if (!in6_dev)
583 goto errout;
584 devconf = &in6_dev->cnf;
585 break;
586 }
587
588 err = -ENOBUFS;
589 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
590 if (!skb)
591 goto errout;
592
593 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
594 NETLINK_CB(in_skb).portid,
595 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
596 -1);
597 if (err < 0) {
598 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
599 WARN_ON(err == -EMSGSIZE);
600 kfree_skb(skb);
601 goto errout;
602 }
603 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
604 errout:
605 return err;
606 }
607
608 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
609 struct netlink_callback *cb)
610 {
611 struct net *net = sock_net(skb->sk);
612 int h, s_h;
613 int idx, s_idx;
614 struct net_device *dev;
615 struct inet6_dev *idev;
616 struct hlist_head *head;
617
618 s_h = cb->args[0];
619 s_idx = idx = cb->args[1];
620
621 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
622 idx = 0;
623 head = &net->dev_index_head[h];
624 rcu_read_lock();
625 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
626 net->dev_base_seq;
627 hlist_for_each_entry_rcu(dev, head, index_hlist) {
628 if (idx < s_idx)
629 goto cont;
630 idev = __in6_dev_get(dev);
631 if (!idev)
632 goto cont;
633
634 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
635 &idev->cnf,
636 NETLINK_CB(cb->skb).portid,
637 cb->nlh->nlmsg_seq,
638 RTM_NEWNETCONF,
639 NLM_F_MULTI,
640 -1) < 0) {
641 rcu_read_unlock();
642 goto done;
643 }
644 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
645 cont:
646 idx++;
647 }
648 rcu_read_unlock();
649 }
650 if (h == NETDEV_HASHENTRIES) {
651 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
652 net->ipv6.devconf_all,
653 NETLINK_CB(cb->skb).portid,
654 cb->nlh->nlmsg_seq,
655 RTM_NEWNETCONF, NLM_F_MULTI,
656 -1) < 0)
657 goto done;
658 else
659 h++;
660 }
661 if (h == NETDEV_HASHENTRIES + 1) {
662 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
663 net->ipv6.devconf_dflt,
664 NETLINK_CB(cb->skb).portid,
665 cb->nlh->nlmsg_seq,
666 RTM_NEWNETCONF, NLM_F_MULTI,
667 -1) < 0)
668 goto done;
669 else
670 h++;
671 }
672 done:
673 cb->args[0] = h;
674 cb->args[1] = idx;
675
676 return skb->len;
677 }
678
679 #ifdef CONFIG_SYSCTL
680 static void dev_forward_change(struct inet6_dev *idev)
681 {
682 struct net_device *dev;
683 struct inet6_ifaddr *ifa;
684
685 if (!idev)
686 return;
687 dev = idev->dev;
688 if (idev->cnf.forwarding)
689 dev_disable_lro(dev);
690 if (dev->flags & IFF_MULTICAST) {
691 if (idev->cnf.forwarding) {
692 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
693 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
694 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
695 } else {
696 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
697 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
698 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
699 }
700 }
701
702 list_for_each_entry(ifa, &idev->addr_list, if_list) {
703 if (ifa->flags&IFA_F_TENTATIVE)
704 continue;
705 if (idev->cnf.forwarding)
706 addrconf_join_anycast(ifa);
707 else
708 addrconf_leave_anycast(ifa);
709 }
710 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
711 dev->ifindex, &idev->cnf);
712 }
713
714
715 static void addrconf_forward_change(struct net *net, __s32 newf)
716 {
717 struct net_device *dev;
718 struct inet6_dev *idev;
719
720 for_each_netdev(net, dev) {
721 idev = __in6_dev_get(dev);
722 if (idev) {
723 int changed = (!idev->cnf.forwarding) ^ (!newf);
724 idev->cnf.forwarding = newf;
725 if (changed)
726 dev_forward_change(idev);
727 }
728 }
729 }
730
731 static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
732 {
733 struct net *net;
734 int old;
735
736 if (!rtnl_trylock())
737 return restart_syscall();
738
739 net = (struct net *)table->extra2;
740 old = *p;
741 *p = newf;
742
743 if (p == &net->ipv6.devconf_dflt->forwarding) {
744 if ((!newf) ^ (!old))
745 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
746 NETCONFA_IFINDEX_DEFAULT,
747 net->ipv6.devconf_dflt);
748 rtnl_unlock();
749 return 0;
750 }
751
752 if (p == &net->ipv6.devconf_all->forwarding) {
753 net->ipv6.devconf_dflt->forwarding = newf;
754 addrconf_forward_change(net, newf);
755 if ((!newf) ^ (!old))
756 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
757 NETCONFA_IFINDEX_ALL,
758 net->ipv6.devconf_all);
759 } else if ((!newf) ^ (!old))
760 dev_forward_change((struct inet6_dev *)table->extra1);
761 rtnl_unlock();
762
763 if (newf)
764 rt6_purge_dflt_routers(net);
765 return 1;
766 }
767 #endif
768
769 /* Nobody refers to this ifaddr, destroy it */
770 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
771 {
772 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
773
774 #ifdef NET_REFCNT_DEBUG
775 pr_debug("%s\n", __func__);
776 #endif
777
778 in6_dev_put(ifp->idev);
779
780 if (cancel_delayed_work(&ifp->dad_work))
781 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
782 ifp);
783
784 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
785 pr_warn("Freeing alive inet6 address %p\n", ifp);
786 return;
787 }
788 ip6_rt_put(ifp->rt);
789
790 kfree_rcu(ifp, rcu);
791 }
792
793 static void
794 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
795 {
796 struct list_head *p;
797 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
798
799 /*
800 * Each device address list is sorted in order of scope -
801 * global before linklocal.
802 */
803 list_for_each(p, &idev->addr_list) {
804 struct inet6_ifaddr *ifa
805 = list_entry(p, struct inet6_ifaddr, if_list);
806 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
807 break;
808 }
809
810 list_add_tail(&ifp->if_list, p);
811 }
812
813 static u32 inet6_addr_hash(const struct in6_addr *addr)
814 {
815 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
816 }
817
818 /* On success it returns ifp with increased reference count */
819
820 static struct inet6_ifaddr *
821 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
822 const struct in6_addr *peer_addr, int pfxlen,
823 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
824 {
825 struct inet6_ifaddr *ifa = NULL;
826 struct rt6_info *rt;
827 unsigned int hash;
828 int err = 0;
829 int addr_type = ipv6_addr_type(addr);
830
831 if (addr_type == IPV6_ADDR_ANY ||
832 addr_type & IPV6_ADDR_MULTICAST ||
833 (!(idev->dev->flags & IFF_LOOPBACK) &&
834 addr_type & IPV6_ADDR_LOOPBACK))
835 return ERR_PTR(-EADDRNOTAVAIL);
836
837 rcu_read_lock_bh();
838 if (idev->dead) {
839 err = -ENODEV; /*XXX*/
840 goto out2;
841 }
842
843 if (idev->cnf.disable_ipv6) {
844 err = -EACCES;
845 goto out2;
846 }
847
848 spin_lock(&addrconf_hash_lock);
849
850 /* Ignore adding duplicate addresses on an interface */
851 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
852 ADBG("ipv6_add_addr: already assigned\n");
853 err = -EEXIST;
854 goto out;
855 }
856
857 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
858
859 if (!ifa) {
860 ADBG("ipv6_add_addr: malloc failed\n");
861 err = -ENOBUFS;
862 goto out;
863 }
864
865 rt = addrconf_dst_alloc(idev, addr, false);
866 if (IS_ERR(rt)) {
867 err = PTR_ERR(rt);
868 goto out;
869 }
870
871 neigh_parms_data_state_setall(idev->nd_parms);
872
873 ifa->addr = *addr;
874 if (peer_addr)
875 ifa->peer_addr = *peer_addr;
876
877 spin_lock_init(&ifa->lock);
878 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
879 INIT_HLIST_NODE(&ifa->addr_lst);
880 ifa->scope = scope;
881 ifa->prefix_len = pfxlen;
882 ifa->flags = flags | IFA_F_TENTATIVE;
883 ifa->valid_lft = valid_lft;
884 ifa->prefered_lft = prefered_lft;
885 ifa->cstamp = ifa->tstamp = jiffies;
886 ifa->tokenized = false;
887
888 ifa->rt = rt;
889
890 ifa->idev = idev;
891 in6_dev_hold(idev);
892 /* For caller */
893 in6_ifa_hold(ifa);
894
895 /* Add to big hash table */
896 hash = inet6_addr_hash(addr);
897
898 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
899 spin_unlock(&addrconf_hash_lock);
900
901 write_lock(&idev->lock);
902 /* Add to inet6_dev unicast addr list. */
903 ipv6_link_dev_addr(idev, ifa);
904
905 if (ifa->flags&IFA_F_TEMPORARY) {
906 list_add(&ifa->tmp_list, &idev->tempaddr_list);
907 in6_ifa_hold(ifa);
908 }
909
910 in6_ifa_hold(ifa);
911 write_unlock(&idev->lock);
912 out2:
913 rcu_read_unlock_bh();
914
915 if (likely(err == 0))
916 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
917 else {
918 kfree(ifa);
919 ifa = ERR_PTR(err);
920 }
921
922 return ifa;
923 out:
924 spin_unlock(&addrconf_hash_lock);
925 goto out2;
926 }
927
928 enum cleanup_prefix_rt_t {
929 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
930 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
931 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
932 };
933
934 /*
935 * Check, whether the prefix for ifp would still need a prefix route
936 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
937 * constants.
938 *
939 * 1) we don't purge prefix if address was not permanent.
940 * prefix is managed by its own lifetime.
941 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
942 * 3) if there are no addresses, delete prefix.
943 * 4) if there are still other permanent address(es),
944 * corresponding prefix is still permanent.
945 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
946 * don't purge the prefix, assume user space is managing it.
947 * 6) otherwise, update prefix lifetime to the
948 * longest valid lifetime among the corresponding
949 * addresses on the device.
950 * Note: subsequent RA will update lifetime.
951 **/
952 static enum cleanup_prefix_rt_t
953 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
954 {
955 struct inet6_ifaddr *ifa;
956 struct inet6_dev *idev = ifp->idev;
957 unsigned long lifetime;
958 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
959
960 *expires = jiffies;
961
962 list_for_each_entry(ifa, &idev->addr_list, if_list) {
963 if (ifa == ifp)
964 continue;
965 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
966 ifp->prefix_len))
967 continue;
968 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
969 return CLEANUP_PREFIX_RT_NOP;
970
971 action = CLEANUP_PREFIX_RT_EXPIRE;
972
973 spin_lock(&ifa->lock);
974
975 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
976 /*
977 * Note: Because this address is
978 * not permanent, lifetime <
979 * LONG_MAX / HZ here.
980 */
981 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
982 *expires = ifa->tstamp + lifetime * HZ;
983 spin_unlock(&ifa->lock);
984 }
985
986 return action;
987 }
988
989 static void
990 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
991 {
992 struct rt6_info *rt;
993
994 rt = addrconf_get_prefix_route(&ifp->addr,
995 ifp->prefix_len,
996 ifp->idev->dev,
997 0, RTF_GATEWAY | RTF_DEFAULT);
998 if (rt) {
999 if (del_rt)
1000 ip6_del_rt(rt);
1001 else {
1002 if (!(rt->rt6i_flags & RTF_EXPIRES))
1003 rt6_set_expires(rt, expires);
1004 ip6_rt_put(rt);
1005 }
1006 }
1007 }
1008
1009
1010 /* This function wants to get referenced ifp and releases it before return */
1011
1012 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1013 {
1014 int state;
1015 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1016 unsigned long expires;
1017
1018 ASSERT_RTNL();
1019
1020 spin_lock_bh(&ifp->lock);
1021 state = ifp->state;
1022 ifp->state = INET6_IFADDR_STATE_DEAD;
1023 spin_unlock_bh(&ifp->lock);
1024
1025 if (state == INET6_IFADDR_STATE_DEAD)
1026 goto out;
1027
1028 spin_lock_bh(&addrconf_hash_lock);
1029 hlist_del_init_rcu(&ifp->addr_lst);
1030 spin_unlock_bh(&addrconf_hash_lock);
1031
1032 write_lock_bh(&ifp->idev->lock);
1033
1034 if (ifp->flags&IFA_F_TEMPORARY) {
1035 list_del(&ifp->tmp_list);
1036 if (ifp->ifpub) {
1037 in6_ifa_put(ifp->ifpub);
1038 ifp->ifpub = NULL;
1039 }
1040 __in6_ifa_put(ifp);
1041 }
1042
1043 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1044 action = check_cleanup_prefix_route(ifp, &expires);
1045
1046 list_del_init(&ifp->if_list);
1047 __in6_ifa_put(ifp);
1048
1049 write_unlock_bh(&ifp->idev->lock);
1050
1051 addrconf_del_dad_work(ifp);
1052
1053 ipv6_ifa_notify(RTM_DELADDR, ifp);
1054
1055 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1056
1057 if (action != CLEANUP_PREFIX_RT_NOP) {
1058 cleanup_prefix_route(ifp, expires,
1059 action == CLEANUP_PREFIX_RT_DEL);
1060 }
1061
1062 /* clean up prefsrc entries */
1063 rt6_remove_prefsrc(ifp);
1064 out:
1065 in6_ifa_put(ifp);
1066 }
1067
1068 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1069 {
1070 struct inet6_dev *idev = ifp->idev;
1071 struct in6_addr addr, *tmpaddr;
1072 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1073 unsigned long regen_advance;
1074 int tmp_plen;
1075 int ret = 0;
1076 u32 addr_flags;
1077 unsigned long now = jiffies;
1078
1079 write_lock_bh(&idev->lock);
1080 if (ift) {
1081 spin_lock_bh(&ift->lock);
1082 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1083 spin_unlock_bh(&ift->lock);
1084 tmpaddr = &addr;
1085 } else {
1086 tmpaddr = NULL;
1087 }
1088 retry:
1089 in6_dev_hold(idev);
1090 if (idev->cnf.use_tempaddr <= 0) {
1091 write_unlock_bh(&idev->lock);
1092 pr_info("%s: use_tempaddr is disabled\n", __func__);
1093 in6_dev_put(idev);
1094 ret = -1;
1095 goto out;
1096 }
1097 spin_lock_bh(&ifp->lock);
1098 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1099 idev->cnf.use_tempaddr = -1; /*XXX*/
1100 spin_unlock_bh(&ifp->lock);
1101 write_unlock_bh(&idev->lock);
1102 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1103 __func__);
1104 in6_dev_put(idev);
1105 ret = -1;
1106 goto out;
1107 }
1108 in6_ifa_hold(ifp);
1109 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1110 __ipv6_try_regen_rndid(idev, tmpaddr);
1111 memcpy(&addr.s6_addr[8], idev->rndid, 8);
1112 age = (now - ifp->tstamp) / HZ;
1113 tmp_valid_lft = min_t(__u32,
1114 ifp->valid_lft,
1115 idev->cnf.temp_valid_lft + age);
1116 tmp_prefered_lft = min_t(__u32,
1117 ifp->prefered_lft,
1118 idev->cnf.temp_prefered_lft + age -
1119 idev->cnf.max_desync_factor);
1120 tmp_plen = ifp->prefix_len;
1121 tmp_tstamp = ifp->tstamp;
1122 spin_unlock_bh(&ifp->lock);
1123
1124 regen_advance = idev->cnf.regen_max_retry *
1125 idev->cnf.dad_transmits *
1126 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
1127 write_unlock_bh(&idev->lock);
1128
1129 /* A temporary address is created only if this calculated Preferred
1130 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1131 * an implementation must not create a temporary address with a zero
1132 * Preferred Lifetime.
1133 * Use age calculation as in addrconf_verify to avoid unnecessary
1134 * temporary addresses being generated.
1135 */
1136 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1137 if (tmp_prefered_lft <= regen_advance + age) {
1138 in6_ifa_put(ifp);
1139 in6_dev_put(idev);
1140 ret = -1;
1141 goto out;
1142 }
1143
1144 addr_flags = IFA_F_TEMPORARY;
1145 /* set in addrconf_prefix_rcv() */
1146 if (ifp->flags & IFA_F_OPTIMISTIC)
1147 addr_flags |= IFA_F_OPTIMISTIC;
1148
1149 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1150 ipv6_addr_scope(&addr), addr_flags,
1151 tmp_valid_lft, tmp_prefered_lft);
1152 if (IS_ERR(ift)) {
1153 in6_ifa_put(ifp);
1154 in6_dev_put(idev);
1155 pr_info("%s: retry temporary address regeneration\n", __func__);
1156 tmpaddr = &addr;
1157 write_lock_bh(&idev->lock);
1158 goto retry;
1159 }
1160
1161 spin_lock_bh(&ift->lock);
1162 ift->ifpub = ifp;
1163 ift->cstamp = now;
1164 ift->tstamp = tmp_tstamp;
1165 spin_unlock_bh(&ift->lock);
1166
1167 addrconf_dad_start(ift);
1168 in6_ifa_put(ift);
1169 in6_dev_put(idev);
1170 out:
1171 return ret;
1172 }
1173
1174 /*
1175 * Choose an appropriate source address (RFC3484)
1176 */
1177 enum {
1178 IPV6_SADDR_RULE_INIT = 0,
1179 IPV6_SADDR_RULE_LOCAL,
1180 IPV6_SADDR_RULE_SCOPE,
1181 IPV6_SADDR_RULE_PREFERRED,
1182 #ifdef CONFIG_IPV6_MIP6
1183 IPV6_SADDR_RULE_HOA,
1184 #endif
1185 IPV6_SADDR_RULE_OIF,
1186 IPV6_SADDR_RULE_LABEL,
1187 IPV6_SADDR_RULE_PRIVACY,
1188 IPV6_SADDR_RULE_ORCHID,
1189 IPV6_SADDR_RULE_PREFIX,
1190 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1191 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1192 #endif
1193 IPV6_SADDR_RULE_MAX
1194 };
1195
1196 struct ipv6_saddr_score {
1197 int rule;
1198 int addr_type;
1199 struct inet6_ifaddr *ifa;
1200 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1201 int scopedist;
1202 int matchlen;
1203 };
1204
1205 struct ipv6_saddr_dst {
1206 const struct in6_addr *addr;
1207 int ifindex;
1208 int scope;
1209 int label;
1210 unsigned int prefs;
1211 };
1212
1213 static inline int ipv6_saddr_preferred(int type)
1214 {
1215 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1216 return 1;
1217 return 0;
1218 }
1219
1220 static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1221 {
1222 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1223 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1224 #else
1225 return false;
1226 #endif
1227 }
1228
1229 static int ipv6_get_saddr_eval(struct net *net,
1230 struct ipv6_saddr_score *score,
1231 struct ipv6_saddr_dst *dst,
1232 int i)
1233 {
1234 int ret;
1235
1236 if (i <= score->rule) {
1237 switch (i) {
1238 case IPV6_SADDR_RULE_SCOPE:
1239 ret = score->scopedist;
1240 break;
1241 case IPV6_SADDR_RULE_PREFIX:
1242 ret = score->matchlen;
1243 break;
1244 default:
1245 ret = !!test_bit(i, score->scorebits);
1246 }
1247 goto out;
1248 }
1249
1250 switch (i) {
1251 case IPV6_SADDR_RULE_INIT:
1252 /* Rule 0: remember if hiscore is not ready yet */
1253 ret = !!score->ifa;
1254 break;
1255 case IPV6_SADDR_RULE_LOCAL:
1256 /* Rule 1: Prefer same address */
1257 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1258 break;
1259 case IPV6_SADDR_RULE_SCOPE:
1260 /* Rule 2: Prefer appropriate scope
1261 *
1262 * ret
1263 * ^
1264 * -1 | d 15
1265 * ---+--+-+---> scope
1266 * |
1267 * | d is scope of the destination.
1268 * B-d | \
1269 * | \ <- smaller scope is better if
1270 * B-15 | \ if scope is enough for destination.
1271 * | ret = B - scope (-1 <= scope >= d <= 15).
1272 * d-C-1 | /
1273 * |/ <- greater is better
1274 * -C / if scope is not enough for destination.
1275 * /| ret = scope - C (-1 <= d < scope <= 15).
1276 *
1277 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1278 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1279 * Assume B = 0 and we get C > 29.
1280 */
1281 ret = __ipv6_addr_src_scope(score->addr_type);
1282 if (ret >= dst->scope)
1283 ret = -ret;
1284 else
1285 ret -= 128; /* 30 is enough */
1286 score->scopedist = ret;
1287 break;
1288 case IPV6_SADDR_RULE_PREFERRED:
1289 {
1290 /* Rule 3: Avoid deprecated and optimistic addresses */
1291 u8 avoid = IFA_F_DEPRECATED;
1292
1293 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1294 avoid |= IFA_F_OPTIMISTIC;
1295 ret = ipv6_saddr_preferred(score->addr_type) ||
1296 !(score->ifa->flags & avoid);
1297 break;
1298 }
1299 #ifdef CONFIG_IPV6_MIP6
1300 case IPV6_SADDR_RULE_HOA:
1301 {
1302 /* Rule 4: Prefer home address */
1303 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1304 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1305 break;
1306 }
1307 #endif
1308 case IPV6_SADDR_RULE_OIF:
1309 /* Rule 5: Prefer outgoing interface */
1310 ret = (!dst->ifindex ||
1311 dst->ifindex == score->ifa->idev->dev->ifindex);
1312 break;
1313 case IPV6_SADDR_RULE_LABEL:
1314 /* Rule 6: Prefer matching label */
1315 ret = ipv6_addr_label(net,
1316 &score->ifa->addr, score->addr_type,
1317 score->ifa->idev->dev->ifindex) == dst->label;
1318 break;
1319 case IPV6_SADDR_RULE_PRIVACY:
1320 {
1321 /* Rule 7: Prefer public address
1322 * Note: prefer temporary address if use_tempaddr >= 2
1323 */
1324 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1325 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1326 score->ifa->idev->cnf.use_tempaddr >= 2;
1327 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1328 break;
1329 }
1330 case IPV6_SADDR_RULE_ORCHID:
1331 /* Rule 8-: Prefer ORCHID vs ORCHID or
1332 * non-ORCHID vs non-ORCHID
1333 */
1334 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1335 ipv6_addr_orchid(dst->addr));
1336 break;
1337 case IPV6_SADDR_RULE_PREFIX:
1338 /* Rule 8: Use longest matching prefix */
1339 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1340 if (ret > score->ifa->prefix_len)
1341 ret = score->ifa->prefix_len;
1342 score->matchlen = ret;
1343 break;
1344 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1345 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1346 /* Optimistic addresses still have lower precedence than other
1347 * preferred addresses.
1348 */
1349 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1350 break;
1351 #endif
1352 default:
1353 ret = 0;
1354 }
1355
1356 if (ret)
1357 __set_bit(i, score->scorebits);
1358 score->rule = i;
1359 out:
1360 return ret;
1361 }
1362
1363 static int __ipv6_dev_get_saddr(struct net *net,
1364 struct ipv6_saddr_dst *dst,
1365 struct inet6_dev *idev,
1366 struct ipv6_saddr_score *scores,
1367 int hiscore_idx)
1368 {
1369 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1370
1371 read_lock_bh(&idev->lock);
1372 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1373 int i;
1374
1375 /*
1376 * - Tentative Address (RFC2462 section 5.4)
1377 * - A tentative address is not considered
1378 * "assigned to an interface" in the traditional
1379 * sense, unless it is also flagged as optimistic.
1380 * - Candidate Source Address (section 4)
1381 * - In any case, anycast addresses, multicast
1382 * addresses, and the unspecified address MUST
1383 * NOT be included in a candidate set.
1384 */
1385 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1386 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1387 continue;
1388
1389 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1390
1391 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1392 score->addr_type & IPV6_ADDR_MULTICAST)) {
1393 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1394 idev->dev->name);
1395 continue;
1396 }
1397
1398 score->rule = -1;
1399 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1400
1401 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1402 int minihiscore, miniscore;
1403
1404 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1405 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1406
1407 if (minihiscore > miniscore) {
1408 if (i == IPV6_SADDR_RULE_SCOPE &&
1409 score->scopedist > 0) {
1410 /*
1411 * special case:
1412 * each remaining entry
1413 * has too small (not enough)
1414 * scope, because ifa entries
1415 * are sorted by their scope
1416 * values.
1417 */
1418 goto out;
1419 }
1420 break;
1421 } else if (minihiscore < miniscore) {
1422 if (hiscore->ifa)
1423 in6_ifa_put(hiscore->ifa);
1424
1425 in6_ifa_hold(score->ifa);
1426
1427 swap(hiscore, score);
1428 hiscore_idx = 1 - hiscore_idx;
1429
1430 /* restore our iterator */
1431 score->ifa = hiscore->ifa;
1432
1433 break;
1434 }
1435 }
1436 }
1437 out:
1438 read_unlock_bh(&idev->lock);
1439 return hiscore_idx;
1440 }
1441
1442 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1443 const struct in6_addr *daddr, unsigned int prefs,
1444 struct in6_addr *saddr)
1445 {
1446 struct ipv6_saddr_score scores[2], *hiscore;
1447 struct ipv6_saddr_dst dst;
1448 struct inet6_dev *idev;
1449 struct net_device *dev;
1450 int dst_type;
1451 bool use_oif_addr = false;
1452 int hiscore_idx = 0;
1453
1454 dst_type = __ipv6_addr_type(daddr);
1455 dst.addr = daddr;
1456 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1457 dst.scope = __ipv6_addr_src_scope(dst_type);
1458 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1459 dst.prefs = prefs;
1460
1461 scores[hiscore_idx].rule = -1;
1462 scores[hiscore_idx].ifa = NULL;
1463
1464 rcu_read_lock();
1465
1466 /* Candidate Source Address (section 4)
1467 * - multicast and link-local destination address,
1468 * the set of candidate source address MUST only
1469 * include addresses assigned to interfaces
1470 * belonging to the same link as the outgoing
1471 * interface.
1472 * (- For site-local destination addresses, the
1473 * set of candidate source addresses MUST only
1474 * include addresses assigned to interfaces
1475 * belonging to the same site as the outgoing
1476 * interface.)
1477 * - "It is RECOMMENDED that the candidate source addresses
1478 * be the set of unicast addresses assigned to the
1479 * interface that will be used to send to the destination
1480 * (the 'outgoing' interface)." (RFC 6724)
1481 */
1482 if (dst_dev) {
1483 idev = __in6_dev_get(dst_dev);
1484 if ((dst_type & IPV6_ADDR_MULTICAST) ||
1485 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1486 (idev && idev->cnf.use_oif_addrs_only)) {
1487 use_oif_addr = true;
1488 }
1489 }
1490
1491 if (use_oif_addr) {
1492 if (idev)
1493 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1494 } else {
1495 for_each_netdev_rcu(net, dev) {
1496 idev = __in6_dev_get(dev);
1497 if (!idev)
1498 continue;
1499 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1500 }
1501 }
1502 rcu_read_unlock();
1503
1504 hiscore = &scores[hiscore_idx];
1505 if (!hiscore->ifa)
1506 return -EADDRNOTAVAIL;
1507
1508 *saddr = hiscore->ifa->addr;
1509 in6_ifa_put(hiscore->ifa);
1510 return 0;
1511 }
1512 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1513
1514 int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1515 u32 banned_flags)
1516 {
1517 struct inet6_ifaddr *ifp;
1518 int err = -EADDRNOTAVAIL;
1519
1520 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1521 if (ifp->scope > IFA_LINK)
1522 break;
1523 if (ifp->scope == IFA_LINK &&
1524 !(ifp->flags & banned_flags)) {
1525 *addr = ifp->addr;
1526 err = 0;
1527 break;
1528 }
1529 }
1530 return err;
1531 }
1532
1533 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1534 u32 banned_flags)
1535 {
1536 struct inet6_dev *idev;
1537 int err = -EADDRNOTAVAIL;
1538
1539 rcu_read_lock();
1540 idev = __in6_dev_get(dev);
1541 if (idev) {
1542 read_lock_bh(&idev->lock);
1543 err = __ipv6_get_lladdr(idev, addr, banned_flags);
1544 read_unlock_bh(&idev->lock);
1545 }
1546 rcu_read_unlock();
1547 return err;
1548 }
1549
1550 static int ipv6_count_addresses(struct inet6_dev *idev)
1551 {
1552 int cnt = 0;
1553 struct inet6_ifaddr *ifp;
1554
1555 read_lock_bh(&idev->lock);
1556 list_for_each_entry(ifp, &idev->addr_list, if_list)
1557 cnt++;
1558 read_unlock_bh(&idev->lock);
1559 return cnt;
1560 }
1561
1562 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1563 const struct net_device *dev, int strict)
1564 {
1565 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1566 }
1567 EXPORT_SYMBOL(ipv6_chk_addr);
1568
1569 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1570 const struct net_device *dev, int strict,
1571 u32 banned_flags)
1572 {
1573 struct inet6_ifaddr *ifp;
1574 unsigned int hash = inet6_addr_hash(addr);
1575 u32 ifp_flags;
1576
1577 rcu_read_lock_bh();
1578 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1579 if (!net_eq(dev_net(ifp->idev->dev), net))
1580 continue;
1581 /* Decouple optimistic from tentative for evaluation here.
1582 * Ban optimistic addresses explicitly, when required.
1583 */
1584 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1585 ? (ifp->flags&~IFA_F_TENTATIVE)
1586 : ifp->flags;
1587 if (ipv6_addr_equal(&ifp->addr, addr) &&
1588 !(ifp_flags&banned_flags) &&
1589 (!dev || ifp->idev->dev == dev ||
1590 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1591 rcu_read_unlock_bh();
1592 return 1;
1593 }
1594 }
1595
1596 rcu_read_unlock_bh();
1597 return 0;
1598 }
1599 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
1600
1601 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1602 struct net_device *dev)
1603 {
1604 unsigned int hash = inet6_addr_hash(addr);
1605 struct inet6_ifaddr *ifp;
1606
1607 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1608 if (!net_eq(dev_net(ifp->idev->dev), net))
1609 continue;
1610 if (ipv6_addr_equal(&ifp->addr, addr)) {
1611 if (!dev || ifp->idev->dev == dev)
1612 return true;
1613 }
1614 }
1615 return false;
1616 }
1617
1618 /* Compares an address/prefix_len with addresses on device @dev.
1619 * If one is found it returns true.
1620 */
1621 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1622 const unsigned int prefix_len, struct net_device *dev)
1623 {
1624 struct inet6_dev *idev;
1625 struct inet6_ifaddr *ifa;
1626 bool ret = false;
1627
1628 rcu_read_lock();
1629 idev = __in6_dev_get(dev);
1630 if (idev) {
1631 read_lock_bh(&idev->lock);
1632 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1633 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1634 if (ret)
1635 break;
1636 }
1637 read_unlock_bh(&idev->lock);
1638 }
1639 rcu_read_unlock();
1640
1641 return ret;
1642 }
1643 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1644
1645 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
1646 {
1647 struct inet6_dev *idev;
1648 struct inet6_ifaddr *ifa;
1649 int onlink;
1650
1651 onlink = 0;
1652 rcu_read_lock();
1653 idev = __in6_dev_get(dev);
1654 if (idev) {
1655 read_lock_bh(&idev->lock);
1656 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1657 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1658 ifa->prefix_len);
1659 if (onlink)
1660 break;
1661 }
1662 read_unlock_bh(&idev->lock);
1663 }
1664 rcu_read_unlock();
1665 return onlink;
1666 }
1667 EXPORT_SYMBOL(ipv6_chk_prefix);
1668
1669 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1670 struct net_device *dev, int strict)
1671 {
1672 struct inet6_ifaddr *ifp, *result = NULL;
1673 unsigned int hash = inet6_addr_hash(addr);
1674
1675 rcu_read_lock_bh();
1676 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1677 if (!net_eq(dev_net(ifp->idev->dev), net))
1678 continue;
1679 if (ipv6_addr_equal(&ifp->addr, addr)) {
1680 if (!dev || ifp->idev->dev == dev ||
1681 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1682 result = ifp;
1683 in6_ifa_hold(ifp);
1684 break;
1685 }
1686 }
1687 }
1688 rcu_read_unlock_bh();
1689
1690 return result;
1691 }
1692
1693 /* Gets referenced address, destroys ifaddr */
1694
1695 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1696 {
1697 if (ifp->flags&IFA_F_PERMANENT) {
1698 spin_lock_bh(&ifp->lock);
1699 addrconf_del_dad_work(ifp);
1700 ifp->flags |= IFA_F_TENTATIVE;
1701 if (dad_failed)
1702 ifp->flags |= IFA_F_DADFAILED;
1703 spin_unlock_bh(&ifp->lock);
1704 if (dad_failed)
1705 ipv6_ifa_notify(0, ifp);
1706 in6_ifa_put(ifp);
1707 } else if (ifp->flags&IFA_F_TEMPORARY) {
1708 struct inet6_ifaddr *ifpub;
1709 spin_lock_bh(&ifp->lock);
1710 ifpub = ifp->ifpub;
1711 if (ifpub) {
1712 in6_ifa_hold(ifpub);
1713 spin_unlock_bh(&ifp->lock);
1714 ipv6_create_tempaddr(ifpub, ifp);
1715 in6_ifa_put(ifpub);
1716 } else {
1717 spin_unlock_bh(&ifp->lock);
1718 }
1719 ipv6_del_addr(ifp);
1720 } else {
1721 ipv6_del_addr(ifp);
1722 }
1723 }
1724
1725 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1726 {
1727 int err = -ENOENT;
1728
1729 spin_lock_bh(&ifp->lock);
1730 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1731 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1732 err = 0;
1733 }
1734 spin_unlock_bh(&ifp->lock);
1735
1736 return err;
1737 }
1738
1739 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1740 {
1741 struct in6_addr addr;
1742 struct inet6_dev *idev = ifp->idev;
1743 struct net *net = dev_net(ifp->idev->dev);
1744
1745 if (addrconf_dad_end(ifp)) {
1746 in6_ifa_put(ifp);
1747 return;
1748 }
1749
1750 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1751 ifp->idev->dev->name, &ifp->addr);
1752
1753 spin_lock_bh(&ifp->lock);
1754
1755 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1756 int scope = ifp->scope;
1757 u32 flags = ifp->flags;
1758 struct in6_addr new_addr;
1759 struct inet6_ifaddr *ifp2;
1760 u32 valid_lft, preferred_lft;
1761 int pfxlen = ifp->prefix_len;
1762 int retries = ifp->stable_privacy_retry + 1;
1763
1764 if (retries > net->ipv6.sysctl.idgen_retries) {
1765 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1766 ifp->idev->dev->name);
1767 goto errdad;
1768 }
1769
1770 new_addr = ifp->addr;
1771 if (ipv6_generate_stable_address(&new_addr, retries,
1772 idev))
1773 goto errdad;
1774
1775 valid_lft = ifp->valid_lft;
1776 preferred_lft = ifp->prefered_lft;
1777
1778 spin_unlock_bh(&ifp->lock);
1779
1780 if (idev->cnf.max_addresses &&
1781 ipv6_count_addresses(idev) >=
1782 idev->cnf.max_addresses)
1783 goto lock_errdad;
1784
1785 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1786 ifp->idev->dev->name);
1787
1788 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1789 scope, flags, valid_lft,
1790 preferred_lft);
1791 if (IS_ERR(ifp2))
1792 goto lock_errdad;
1793
1794 spin_lock_bh(&ifp2->lock);
1795 ifp2->stable_privacy_retry = retries;
1796 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1797 spin_unlock_bh(&ifp2->lock);
1798
1799 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1800 in6_ifa_put(ifp2);
1801 lock_errdad:
1802 spin_lock_bh(&ifp->lock);
1803 } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1804 addr.s6_addr32[0] = htonl(0xfe800000);
1805 addr.s6_addr32[1] = 0;
1806
1807 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1808 ipv6_addr_equal(&ifp->addr, &addr)) {
1809 /* DAD failed for link-local based on MAC address */
1810 idev->cnf.disable_ipv6 = 1;
1811
1812 pr_info("%s: IPv6 being disabled!\n",
1813 ifp->idev->dev->name);
1814 }
1815 }
1816
1817 errdad:
1818 /* transition from _POSTDAD to _ERRDAD */
1819 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1820 spin_unlock_bh(&ifp->lock);
1821
1822 addrconf_mod_dad_work(ifp, 0);
1823 }
1824
1825 /* Join to solicited addr multicast group.
1826 * caller must hold RTNL */
1827 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
1828 {
1829 struct in6_addr maddr;
1830
1831 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1832 return;
1833
1834 addrconf_addr_solict_mult(addr, &maddr);
1835 ipv6_dev_mc_inc(dev, &maddr);
1836 }
1837
1838 /* caller must hold RTNL */
1839 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1840 {
1841 struct in6_addr maddr;
1842
1843 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1844 return;
1845
1846 addrconf_addr_solict_mult(addr, &maddr);
1847 __ipv6_dev_mc_dec(idev, &maddr);
1848 }
1849
1850 /* caller must hold RTNL */
1851 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1852 {
1853 struct in6_addr addr;
1854
1855 if (ifp->prefix_len >= 127) /* RFC 6164 */
1856 return;
1857 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1858 if (ipv6_addr_any(&addr))
1859 return;
1860 __ipv6_dev_ac_inc(ifp->idev, &addr);
1861 }
1862
1863 /* caller must hold RTNL */
1864 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1865 {
1866 struct in6_addr addr;
1867
1868 if (ifp->prefix_len >= 127) /* RFC 6164 */
1869 return;
1870 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1871 if (ipv6_addr_any(&addr))
1872 return;
1873 __ipv6_dev_ac_dec(ifp->idev, &addr);
1874 }
1875
1876 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1877 {
1878 if (dev->addr_len != ETH_ALEN)
1879 return -1;
1880 memcpy(eui, dev->dev_addr, 3);
1881 memcpy(eui + 5, dev->dev_addr + 3, 3);
1882
1883 /*
1884 * The zSeries OSA network cards can be shared among various
1885 * OS instances, but the OSA cards have only one MAC address.
1886 * This leads to duplicate address conflicts in conjunction
1887 * with IPv6 if more than one instance uses the same card.
1888 *
1889 * The driver for these cards can deliver a unique 16-bit
1890 * identifier for each instance sharing the same card. It is
1891 * placed instead of 0xFFFE in the interface identifier. The
1892 * "u" bit of the interface identifier is not inverted in this
1893 * case. Hence the resulting interface identifier has local
1894 * scope according to RFC2373.
1895 */
1896 if (dev->dev_id) {
1897 eui[3] = (dev->dev_id >> 8) & 0xFF;
1898 eui[4] = dev->dev_id & 0xFF;
1899 } else {
1900 eui[3] = 0xFF;
1901 eui[4] = 0xFE;
1902 eui[0] ^= 2;
1903 }
1904 return 0;
1905 }
1906
1907 static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1908 {
1909 if (dev->addr_len != IEEE802154_ADDR_LEN)
1910 return -1;
1911 memcpy(eui, dev->dev_addr, 8);
1912 eui[0] ^= 2;
1913 return 0;
1914 }
1915
1916 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1917 {
1918 union fwnet_hwaddr *ha;
1919
1920 if (dev->addr_len != FWNET_ALEN)
1921 return -1;
1922
1923 ha = (union fwnet_hwaddr *)dev->dev_addr;
1924
1925 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1926 eui[0] ^= 2;
1927 return 0;
1928 }
1929
1930 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1931 {
1932 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1933 if (dev->addr_len != ARCNET_ALEN)
1934 return -1;
1935 memset(eui, 0, 7);
1936 eui[7] = *(u8 *)dev->dev_addr;
1937 return 0;
1938 }
1939
1940 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1941 {
1942 if (dev->addr_len != INFINIBAND_ALEN)
1943 return -1;
1944 memcpy(eui, dev->dev_addr + 12, 8);
1945 eui[0] |= 2;
1946 return 0;
1947 }
1948
1949 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1950 {
1951 if (addr == 0)
1952 return -1;
1953 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1954 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1955 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1956 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1957 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1958 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1959 eui[1] = 0;
1960 eui[2] = 0x5E;
1961 eui[3] = 0xFE;
1962 memcpy(eui + 4, &addr, 4);
1963 return 0;
1964 }
1965
1966 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1967 {
1968 if (dev->priv_flags & IFF_ISATAP)
1969 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1970 return -1;
1971 }
1972
1973 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1974 {
1975 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1976 }
1977
1978 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1979 {
1980 memcpy(eui, dev->perm_addr, 3);
1981 memcpy(eui + 5, dev->perm_addr + 3, 3);
1982 eui[3] = 0xFF;
1983 eui[4] = 0xFE;
1984 eui[0] ^= 2;
1985 return 0;
1986 }
1987
1988 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1989 {
1990 switch (dev->type) {
1991 case ARPHRD_ETHER:
1992 case ARPHRD_FDDI:
1993 return addrconf_ifid_eui48(eui, dev);
1994 case ARPHRD_ARCNET:
1995 return addrconf_ifid_arcnet(eui, dev);
1996 case ARPHRD_INFINIBAND:
1997 return addrconf_ifid_infiniband(eui, dev);
1998 case ARPHRD_SIT:
1999 return addrconf_ifid_sit(eui, dev);
2000 case ARPHRD_IPGRE:
2001 return addrconf_ifid_gre(eui, dev);
2002 case ARPHRD_6LOWPAN:
2003 case ARPHRD_IEEE802154:
2004 return addrconf_ifid_eui64(eui, dev);
2005 case ARPHRD_IEEE1394:
2006 return addrconf_ifid_ieee1394(eui, dev);
2007 case ARPHRD_TUNNEL6:
2008 return addrconf_ifid_ip6tnl(eui, dev);
2009 }
2010 return -1;
2011 }
2012
2013 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2014 {
2015 int err = -1;
2016 struct inet6_ifaddr *ifp;
2017
2018 read_lock_bh(&idev->lock);
2019 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2020 if (ifp->scope > IFA_LINK)
2021 break;
2022 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2023 memcpy(eui, ifp->addr.s6_addr+8, 8);
2024 err = 0;
2025 break;
2026 }
2027 }
2028 read_unlock_bh(&idev->lock);
2029 return err;
2030 }
2031
2032 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
2033 static void __ipv6_regen_rndid(struct inet6_dev *idev)
2034 {
2035 regen:
2036 get_random_bytes(idev->rndid, sizeof(idev->rndid));
2037 idev->rndid[0] &= ~0x02;
2038
2039 /*
2040 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2041 * check if generated address is not inappropriate
2042 *
2043 * - Reserved subnet anycast (RFC 2526)
2044 * 11111101 11....11 1xxxxxxx
2045 * - ISATAP (RFC4214) 6.1
2046 * 00-00-5E-FE-xx-xx-xx-xx
2047 * - value 0
2048 * - XXX: already assigned to an address on the device
2049 */
2050 if (idev->rndid[0] == 0xfd &&
2051 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2052 (idev->rndid[7]&0x80))
2053 goto regen;
2054 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2055 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2056 goto regen;
2057 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2058 goto regen;
2059 }
2060 }
2061
2062 static void ipv6_regen_rndid(unsigned long data)
2063 {
2064 struct inet6_dev *idev = (struct inet6_dev *) data;
2065 unsigned long expires;
2066
2067 rcu_read_lock_bh();
2068 write_lock_bh(&idev->lock);
2069
2070 if (idev->dead)
2071 goto out;
2072
2073 __ipv6_regen_rndid(idev);
2074
2075 expires = jiffies +
2076 idev->cnf.temp_prefered_lft * HZ -
2077 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2078 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
2079 idev->cnf.max_desync_factor * HZ;
2080 if (time_before(expires, jiffies)) {
2081 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2082 __func__, idev->dev->name);
2083 goto out;
2084 }
2085
2086 if (!mod_timer(&idev->regen_timer, expires))
2087 in6_dev_hold(idev);
2088
2089 out:
2090 write_unlock_bh(&idev->lock);
2091 rcu_read_unlock_bh();
2092 in6_dev_put(idev);
2093 }
2094
2095 static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
2096 {
2097 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
2098 __ipv6_regen_rndid(idev);
2099 }
2100
2101 /*
2102 * Add prefix route.
2103 */
2104
2105 static void
2106 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
2107 unsigned long expires, u32 flags)
2108 {
2109 struct fib6_config cfg = {
2110 .fc_table = RT6_TABLE_PREFIX,
2111 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2112 .fc_ifindex = dev->ifindex,
2113 .fc_expires = expires,
2114 .fc_dst_len = plen,
2115 .fc_flags = RTF_UP | flags,
2116 .fc_nlinfo.nl_net = dev_net(dev),
2117 .fc_protocol = RTPROT_KERNEL,
2118 };
2119
2120 cfg.fc_dst = *pfx;
2121
2122 /* Prevent useless cloning on PtP SIT.
2123 This thing is done here expecting that the whole
2124 class of non-broadcast devices need not cloning.
2125 */
2126 #if IS_ENABLED(CONFIG_IPV6_SIT)
2127 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2128 cfg.fc_flags |= RTF_NONEXTHOP;
2129 #endif
2130
2131 ip6_route_add(&cfg);
2132 }
2133
2134
2135 static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2136 int plen,
2137 const struct net_device *dev,
2138 u32 flags, u32 noflags)
2139 {
2140 struct fib6_node *fn;
2141 struct rt6_info *rt = NULL;
2142 struct fib6_table *table;
2143
2144 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2145 if (!table)
2146 return NULL;
2147
2148 read_lock_bh(&table->tb6_lock);
2149 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2150 if (!fn)
2151 goto out;
2152
2153 noflags |= RTF_CACHE;
2154 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2155 if (rt->dst.dev->ifindex != dev->ifindex)
2156 continue;
2157 if ((rt->rt6i_flags & flags) != flags)
2158 continue;
2159 if ((rt->rt6i_flags & noflags) != 0)
2160 continue;
2161 dst_hold(&rt->dst);
2162 break;
2163 }
2164 out:
2165 read_unlock_bh(&table->tb6_lock);
2166 return rt;
2167 }
2168
2169
2170 /* Create "default" multicast route to the interface */
2171
2172 static void addrconf_add_mroute(struct net_device *dev)
2173 {
2174 struct fib6_config cfg = {
2175 .fc_table = RT6_TABLE_LOCAL,
2176 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2177 .fc_ifindex = dev->ifindex,
2178 .fc_dst_len = 8,
2179 .fc_flags = RTF_UP,
2180 .fc_nlinfo.nl_net = dev_net(dev),
2181 };
2182
2183 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2184
2185 ip6_route_add(&cfg);
2186 }
2187
2188 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2189 {
2190 struct inet6_dev *idev;
2191
2192 ASSERT_RTNL();
2193
2194 idev = ipv6_find_idev(dev);
2195 if (!idev)
2196 return ERR_PTR(-ENOBUFS);
2197
2198 if (idev->cnf.disable_ipv6)
2199 return ERR_PTR(-EACCES);
2200
2201 /* Add default multicast route */
2202 if (!(dev->flags & IFF_LOOPBACK))
2203 addrconf_add_mroute(dev);
2204
2205 return idev;
2206 }
2207
2208 static void manage_tempaddrs(struct inet6_dev *idev,
2209 struct inet6_ifaddr *ifp,
2210 __u32 valid_lft, __u32 prefered_lft,
2211 bool create, unsigned long now)
2212 {
2213 u32 flags;
2214 struct inet6_ifaddr *ift;
2215
2216 read_lock_bh(&idev->lock);
2217 /* update all temporary addresses in the list */
2218 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2219 int age, max_valid, max_prefered;
2220
2221 if (ifp != ift->ifpub)
2222 continue;
2223
2224 /* RFC 4941 section 3.3:
2225 * If a received option will extend the lifetime of a public
2226 * address, the lifetimes of temporary addresses should
2227 * be extended, subject to the overall constraint that no
2228 * temporary addresses should ever remain "valid" or "preferred"
2229 * for a time longer than (TEMP_VALID_LIFETIME) or
2230 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2231 */
2232 age = (now - ift->cstamp) / HZ;
2233 max_valid = idev->cnf.temp_valid_lft - age;
2234 if (max_valid < 0)
2235 max_valid = 0;
2236
2237 max_prefered = idev->cnf.temp_prefered_lft -
2238 idev->cnf.max_desync_factor - age;
2239 if (max_prefered < 0)
2240 max_prefered = 0;
2241
2242 if (valid_lft > max_valid)
2243 valid_lft = max_valid;
2244
2245 if (prefered_lft > max_prefered)
2246 prefered_lft = max_prefered;
2247
2248 spin_lock(&ift->lock);
2249 flags = ift->flags;
2250 ift->valid_lft = valid_lft;
2251 ift->prefered_lft = prefered_lft;
2252 ift->tstamp = now;
2253 if (prefered_lft > 0)
2254 ift->flags &= ~IFA_F_DEPRECATED;
2255
2256 spin_unlock(&ift->lock);
2257 if (!(flags&IFA_F_TENTATIVE))
2258 ipv6_ifa_notify(0, ift);
2259 }
2260
2261 if ((create || list_empty(&idev->tempaddr_list)) &&
2262 idev->cnf.use_tempaddr > 0) {
2263 /* When a new public address is created as described
2264 * in [ADDRCONF], also create a new temporary address.
2265 * Also create a temporary address if it's enabled but
2266 * no temporary address currently exists.
2267 */
2268 read_unlock_bh(&idev->lock);
2269 ipv6_create_tempaddr(ifp, NULL);
2270 } else {
2271 read_unlock_bh(&idev->lock);
2272 }
2273 }
2274
2275 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2276 {
2277 struct prefix_info *pinfo;
2278 __u32 valid_lft;
2279 __u32 prefered_lft;
2280 int addr_type;
2281 u32 addr_flags = 0;
2282 struct inet6_dev *in6_dev;
2283 struct net *net = dev_net(dev);
2284
2285 pinfo = (struct prefix_info *) opt;
2286
2287 if (len < sizeof(struct prefix_info)) {
2288 ADBG("addrconf: prefix option too short\n");
2289 return;
2290 }
2291
2292 /*
2293 * Validation checks ([ADDRCONF], page 19)
2294 */
2295
2296 addr_type = ipv6_addr_type(&pinfo->prefix);
2297
2298 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2299 return;
2300
2301 valid_lft = ntohl(pinfo->valid);
2302 prefered_lft = ntohl(pinfo->prefered);
2303
2304 if (prefered_lft > valid_lft) {
2305 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2306 return;
2307 }
2308
2309 in6_dev = in6_dev_get(dev);
2310
2311 if (!in6_dev) {
2312 net_dbg_ratelimited("addrconf: device %s not configured\n",
2313 dev->name);
2314 return;
2315 }
2316
2317 /*
2318 * Two things going on here:
2319 * 1) Add routes for on-link prefixes
2320 * 2) Configure prefixes with the auto flag set
2321 */
2322
2323 if (pinfo->onlink) {
2324 struct rt6_info *rt;
2325 unsigned long rt_expires;
2326
2327 /* Avoid arithmetic overflow. Really, we could
2328 * save rt_expires in seconds, likely valid_lft,
2329 * but it would require division in fib gc, that it
2330 * not good.
2331 */
2332 if (HZ > USER_HZ)
2333 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2334 else
2335 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2336
2337 if (addrconf_finite_timeout(rt_expires))
2338 rt_expires *= HZ;
2339
2340 rt = addrconf_get_prefix_route(&pinfo->prefix,
2341 pinfo->prefix_len,
2342 dev,
2343 RTF_ADDRCONF | RTF_PREFIX_RT,
2344 RTF_GATEWAY | RTF_DEFAULT);
2345
2346 if (rt) {
2347 /* Autoconf prefix route */
2348 if (valid_lft == 0) {
2349 ip6_del_rt(rt);
2350 rt = NULL;
2351 } else if (addrconf_finite_timeout(rt_expires)) {
2352 /* not infinity */
2353 rt6_set_expires(rt, jiffies + rt_expires);
2354 } else {
2355 rt6_clean_expires(rt);
2356 }
2357 } else if (valid_lft) {
2358 clock_t expires = 0;
2359 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2360 if (addrconf_finite_timeout(rt_expires)) {
2361 /* not infinity */
2362 flags |= RTF_EXPIRES;
2363 expires = jiffies_to_clock_t(rt_expires);
2364 }
2365 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2366 dev, expires, flags);
2367 }
2368 ip6_rt_put(rt);
2369 }
2370
2371 /* Try to figure out our local address for this prefix */
2372
2373 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
2374 struct inet6_ifaddr *ifp;
2375 struct in6_addr addr;
2376 int create = 0, update_lft = 0;
2377 bool tokenized = false;
2378
2379 if (pinfo->prefix_len == 64) {
2380 memcpy(&addr, &pinfo->prefix, 8);
2381
2382 if (!ipv6_addr_any(&in6_dev->token)) {
2383 read_lock_bh(&in6_dev->lock);
2384 memcpy(addr.s6_addr + 8,
2385 in6_dev->token.s6_addr + 8, 8);
2386 read_unlock_bh(&in6_dev->lock);
2387 tokenized = true;
2388 } else if (in6_dev->addr_gen_mode ==
2389 IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
2390 !ipv6_generate_stable_address(&addr, 0,
2391 in6_dev)) {
2392 addr_flags |= IFA_F_STABLE_PRIVACY;
2393 goto ok;
2394 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2395 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2396 in6_dev_put(in6_dev);
2397 return;
2398 }
2399 goto ok;
2400 }
2401 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2402 pinfo->prefix_len);
2403 in6_dev_put(in6_dev);
2404 return;
2405
2406 ok:
2407
2408 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2409
2410 if (!ifp && valid_lft) {
2411 int max_addresses = in6_dev->cnf.max_addresses;
2412
2413 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2414 if (in6_dev->cnf.optimistic_dad &&
2415 !net->ipv6.devconf_all->forwarding && sllao)
2416 addr_flags = IFA_F_OPTIMISTIC;
2417 #endif
2418
2419 /* Do not allow to create too much of autoconfigured
2420 * addresses; this would be too easy way to crash kernel.
2421 */
2422 if (!max_addresses ||
2423 ipv6_count_addresses(in6_dev) < max_addresses)
2424 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2425 pinfo->prefix_len,
2426 addr_type&IPV6_ADDR_SCOPE_MASK,
2427 addr_flags, valid_lft,
2428 prefered_lft);
2429
2430 if (IS_ERR_OR_NULL(ifp)) {
2431 in6_dev_put(in6_dev);
2432 return;
2433 }
2434
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);
2443 }
2444
2445 if (ifp) {
2446 u32 flags;
2447 unsigned long now;
2448 u32 stored_lft;
2449
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);
2461
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."
2469 *
2470 * So we should always update prefered_lft here.
2471 */
2472 update_lft = 1;
2473 }
2474
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);
2482
2483 if (!(flags&IFA_F_TENTATIVE))
2484 ipv6_ifa_notify(0, ifp);
2485 } else
2486 spin_unlock_bh(&ifp->lock);
2487
2488 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2489 create, now);
2490
2491 in6_ifa_put(ifp);
2492 addrconf_verify();
2493 }
2494 }
2495 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2496 in6_dev_put(in6_dev);
2497 }
2498
2499 /*
2500 * Set destination address.
2501 * Special case for SIT interfaces where we create a new "virtual"
2502 * device.
2503 */
2504 int addrconf_set_dstaddr(struct net *net, void __user *arg)
2505 {
2506 struct in6_ifreq ireq;
2507 struct net_device *dev;
2508 int err = -EINVAL;
2509
2510 rtnl_lock();
2511
2512 err = -EFAULT;
2513 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2514 goto err_exit;
2515
2516 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2517
2518 err = -ENODEV;
2519 if (!dev)
2520 goto err_exit;
2521
2522 #if IS_ENABLED(CONFIG_IPV6_SIT)
2523 if (dev->type == ARPHRD_SIT) {
2524 const struct net_device_ops *ops = dev->netdev_ops;
2525 struct ifreq ifr;
2526 struct ip_tunnel_parm p;
2527
2528 err = -EADDRNOTAVAIL;
2529 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2530 goto err_exit;
2531
2532 memset(&p, 0, sizeof(p));
2533 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2534 p.iph.saddr = 0;
2535 p.iph.version = 4;
2536 p.iph.ihl = 5;
2537 p.iph.protocol = IPPROTO_IPV6;
2538 p.iph.ttl = 64;
2539 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2540
2541 if (ops->ndo_do_ioctl) {
2542 mm_segment_t oldfs = get_fs();
2543
2544 set_fs(KERNEL_DS);
2545 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2546 set_fs(oldfs);
2547 } else
2548 err = -EOPNOTSUPP;
2549
2550 if (err == 0) {
2551 err = -ENOBUFS;
2552 dev = __dev_get_by_name(net, p.name);
2553 if (!dev)
2554 goto err_exit;
2555 err = dev_open(dev);
2556 }
2557 }
2558 #endif
2559
2560 err_exit:
2561 rtnl_unlock();
2562 return err;
2563 }
2564
2565 static int ipv6_mc_config(struct sock *sk, bool join,
2566 const struct in6_addr *addr, int ifindex)
2567 {
2568 int ret;
2569
2570 ASSERT_RTNL();
2571
2572 lock_sock(sk);
2573 if (join)
2574 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2575 else
2576 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2577 release_sock(sk);
2578
2579 return ret;
2580 }
2581
2582 /*
2583 * Manual configuration of address on an interface
2584 */
2585 static int inet6_addr_add(struct net *net, int ifindex,
2586 const struct in6_addr *pfx,
2587 const struct in6_addr *peer_pfx,
2588 unsigned int plen, __u32 ifa_flags,
2589 __u32 prefered_lft, __u32 valid_lft)
2590 {
2591 struct inet6_ifaddr *ifp;
2592 struct inet6_dev *idev;
2593 struct net_device *dev;
2594 unsigned long timeout;
2595 clock_t expires;
2596 int scope;
2597 u32 flags;
2598
2599 ASSERT_RTNL();
2600
2601 if (plen > 128)
2602 return -EINVAL;
2603
2604 /* check the lifetime */
2605 if (!valid_lft || prefered_lft > valid_lft)
2606 return -EINVAL;
2607
2608 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2609 return -EINVAL;
2610
2611 dev = __dev_get_by_index(net, ifindex);
2612 if (!dev)
2613 return -ENODEV;
2614
2615 idev = addrconf_add_dev(dev);
2616 if (IS_ERR(idev))
2617 return PTR_ERR(idev);
2618
2619 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2620 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2621 true, pfx, ifindex);
2622
2623 if (ret < 0)
2624 return ret;
2625 }
2626
2627 scope = ipv6_addr_scope(pfx);
2628
2629 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2630 if (addrconf_finite_timeout(timeout)) {
2631 expires = jiffies_to_clock_t(timeout * HZ);
2632 valid_lft = timeout;
2633 flags = RTF_EXPIRES;
2634 } else {
2635 expires = 0;
2636 flags = 0;
2637 ifa_flags |= IFA_F_PERMANENT;
2638 }
2639
2640 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2641 if (addrconf_finite_timeout(timeout)) {
2642 if (timeout == 0)
2643 ifa_flags |= IFA_F_DEPRECATED;
2644 prefered_lft = timeout;
2645 }
2646
2647 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2648 valid_lft, prefered_lft);
2649
2650 if (!IS_ERR(ifp)) {
2651 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2652 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2653 expires, flags);
2654 }
2655
2656 /*
2657 * Note that section 3.1 of RFC 4429 indicates
2658 * that the Optimistic flag should not be set for
2659 * manually configured addresses
2660 */
2661 addrconf_dad_start(ifp);
2662 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2663 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2664 true, jiffies);
2665 in6_ifa_put(ifp);
2666 addrconf_verify_rtnl();
2667 return 0;
2668 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2669 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2670 false, pfx, ifindex);
2671 }
2672
2673 return PTR_ERR(ifp);
2674 }
2675
2676 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2677 const struct in6_addr *pfx, unsigned int plen)
2678 {
2679 struct inet6_ifaddr *ifp;
2680 struct inet6_dev *idev;
2681 struct net_device *dev;
2682
2683 if (plen > 128)
2684 return -EINVAL;
2685
2686 dev = __dev_get_by_index(net, ifindex);
2687 if (!dev)
2688 return -ENODEV;
2689
2690 idev = __in6_dev_get(dev);
2691 if (!idev)
2692 return -ENXIO;
2693
2694 read_lock_bh(&idev->lock);
2695 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2696 if (ifp->prefix_len == plen &&
2697 ipv6_addr_equal(pfx, &ifp->addr)) {
2698 in6_ifa_hold(ifp);
2699 read_unlock_bh(&idev->lock);
2700
2701 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2702 (ifa_flags & IFA_F_MANAGETEMPADDR))
2703 manage_tempaddrs(idev, ifp, 0, 0, false,
2704 jiffies);
2705 ipv6_del_addr(ifp);
2706 addrconf_verify_rtnl();
2707 if (ipv6_addr_is_multicast(pfx)) {
2708 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2709 false, pfx, dev->ifindex);
2710 }
2711 return 0;
2712 }
2713 }
2714 read_unlock_bh(&idev->lock);
2715 return -EADDRNOTAVAIL;
2716 }
2717
2718
2719 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2720 {
2721 struct in6_ifreq ireq;
2722 int err;
2723
2724 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2725 return -EPERM;
2726
2727 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2728 return -EFAULT;
2729
2730 rtnl_lock();
2731 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2732 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2733 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2734 rtnl_unlock();
2735 return err;
2736 }
2737
2738 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2739 {
2740 struct in6_ifreq ireq;
2741 int err;
2742
2743 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2744 return -EPERM;
2745
2746 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2747 return -EFAULT;
2748
2749 rtnl_lock();
2750 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
2751 ireq.ifr6_prefixlen);
2752 rtnl_unlock();
2753 return err;
2754 }
2755
2756 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2757 int plen, int scope)
2758 {
2759 struct inet6_ifaddr *ifp;
2760
2761 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2762 scope, IFA_F_PERMANENT,
2763 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2764 if (!IS_ERR(ifp)) {
2765 spin_lock_bh(&ifp->lock);
2766 ifp->flags &= ~IFA_F_TENTATIVE;
2767 spin_unlock_bh(&ifp->lock);
2768 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2769 in6_ifa_put(ifp);
2770 }
2771 }
2772
2773 #if IS_ENABLED(CONFIG_IPV6_SIT)
2774 static void sit_add_v4_addrs(struct inet6_dev *idev)
2775 {
2776 struct in6_addr addr;
2777 struct net_device *dev;
2778 struct net *net = dev_net(idev->dev);
2779 int scope, plen;
2780 u32 pflags = 0;
2781
2782 ASSERT_RTNL();
2783
2784 memset(&addr, 0, sizeof(struct in6_addr));
2785 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2786
2787 if (idev->dev->flags&IFF_POINTOPOINT) {
2788 addr.s6_addr32[0] = htonl(0xfe800000);
2789 scope = IFA_LINK;
2790 plen = 64;
2791 } else {
2792 scope = IPV6_ADDR_COMPATv4;
2793 plen = 96;
2794 pflags |= RTF_NONEXTHOP;
2795 }
2796
2797 if (addr.s6_addr32[3]) {
2798 add_addr(idev, &addr, plen, scope);
2799 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2800 return;
2801 }
2802
2803 for_each_netdev(net, dev) {
2804 struct in_device *in_dev = __in_dev_get_rtnl(dev);
2805 if (in_dev && (dev->flags & IFF_UP)) {
2806 struct in_ifaddr *ifa;
2807
2808 int flag = scope;
2809
2810 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2811
2812 addr.s6_addr32[3] = ifa->ifa_local;
2813
2814 if (ifa->ifa_scope == RT_SCOPE_LINK)
2815 continue;
2816 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2817 if (idev->dev->flags&IFF_POINTOPOINT)
2818 continue;
2819 flag |= IFA_HOST;
2820 }
2821
2822 add_addr(idev, &addr, plen, flag);
2823 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2824 pflags);
2825 }
2826 }
2827 }
2828 }
2829 #endif
2830
2831 static void init_loopback(struct net_device *dev)
2832 {
2833 struct inet6_dev *idev;
2834 struct net_device *sp_dev;
2835 struct inet6_ifaddr *sp_ifa;
2836 struct rt6_info *sp_rt;
2837
2838 /* ::1 */
2839
2840 ASSERT_RTNL();
2841
2842 idev = ipv6_find_idev(dev);
2843 if (!idev) {
2844 pr_debug("%s: add_dev failed\n", __func__);
2845 return;
2846 }
2847
2848 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
2849
2850 /* Add routes to other interface's IPv6 addresses */
2851 for_each_netdev(dev_net(dev), sp_dev) {
2852 if (!strcmp(sp_dev->name, dev->name))
2853 continue;
2854
2855 idev = __in6_dev_get(sp_dev);
2856 if (!idev)
2857 continue;
2858
2859 read_lock_bh(&idev->lock);
2860 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2861
2862 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2863 continue;
2864
2865 if (sp_ifa->rt) {
2866 /* This dst has been added to garbage list when
2867 * lo device down, release this obsolete dst and
2868 * reallocate a new router for ifa.
2869 */
2870 if (sp_ifa->rt->dst.obsolete > 0) {
2871 ip6_rt_put(sp_ifa->rt);
2872 sp_ifa->rt = NULL;
2873 } else {
2874 continue;
2875 }
2876 }
2877
2878 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
2879
2880 /* Failure cases are ignored */
2881 if (!IS_ERR(sp_rt)) {
2882 sp_ifa->rt = sp_rt;
2883 ip6_ins_rt(sp_rt);
2884 }
2885 }
2886 read_unlock_bh(&idev->lock);
2887 }
2888 }
2889
2890 static void addrconf_add_linklocal(struct inet6_dev *idev,
2891 const struct in6_addr *addr, u32 flags)
2892 {
2893 struct inet6_ifaddr *ifp;
2894 u32 addr_flags = flags | IFA_F_PERMANENT;
2895
2896 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2897 if (idev->cnf.optimistic_dad &&
2898 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
2899 addr_flags |= IFA_F_OPTIMISTIC;
2900 #endif
2901
2902 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2903 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2904 if (!IS_ERR(ifp)) {
2905 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2906 addrconf_dad_start(ifp);
2907 in6_ifa_put(ifp);
2908 }
2909 }
2910
2911 static bool ipv6_reserved_interfaceid(struct in6_addr address)
2912 {
2913 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2914 return true;
2915
2916 if (address.s6_addr32[2] == htonl(0x02005eff) &&
2917 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2918 return true;
2919
2920 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2921 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2922 return true;
2923
2924 return false;
2925 }
2926
2927 static int ipv6_generate_stable_address(struct in6_addr *address,
2928 u8 dad_count,
2929 const struct inet6_dev *idev)
2930 {
2931 static DEFINE_SPINLOCK(lock);
2932 static __u32 digest[SHA_DIGEST_WORDS];
2933 static __u32 workspace[SHA_WORKSPACE_WORDS];
2934
2935 static union {
2936 char __data[SHA_MESSAGE_BYTES];
2937 struct {
2938 struct in6_addr secret;
2939 __be32 prefix[2];
2940 unsigned char hwaddr[MAX_ADDR_LEN];
2941 u8 dad_count;
2942 } __packed;
2943 } data;
2944
2945 struct in6_addr secret;
2946 struct in6_addr temp;
2947 struct net *net = dev_net(idev->dev);
2948
2949 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
2950
2951 if (idev->cnf.stable_secret.initialized)
2952 secret = idev->cnf.stable_secret.secret;
2953 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
2954 secret = net->ipv6.devconf_dflt->stable_secret.secret;
2955 else
2956 return -1;
2957
2958 retry:
2959 spin_lock_bh(&lock);
2960
2961 sha_init(digest);
2962 memset(&data, 0, sizeof(data));
2963 memset(workspace, 0, sizeof(workspace));
2964 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
2965 data.prefix[0] = address->s6_addr32[0];
2966 data.prefix[1] = address->s6_addr32[1];
2967 data.secret = secret;
2968 data.dad_count = dad_count;
2969
2970 sha_transform(digest, data.__data, workspace);
2971
2972 temp = *address;
2973 temp.s6_addr32[2] = (__force __be32)digest[0];
2974 temp.s6_addr32[3] = (__force __be32)digest[1];
2975
2976 spin_unlock_bh(&lock);
2977
2978 if (ipv6_reserved_interfaceid(temp)) {
2979 dad_count++;
2980 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
2981 return -1;
2982 goto retry;
2983 }
2984
2985 *address = temp;
2986 return 0;
2987 }
2988
2989 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2990 {
2991 struct in6_addr addr;
2992
2993 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2994
2995 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) {
2996 if (!ipv6_generate_stable_address(&addr, 0, idev))
2997 addrconf_add_linklocal(idev, &addr,
2998 IFA_F_STABLE_PRIVACY);
2999 else if (prefix_route)
3000 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3001 } else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
3002 /* addrconf_add_linklocal also adds a prefix_route and we
3003 * only need to care about prefix routes if ipv6_generate_eui64
3004 * couldn't generate one.
3005 */
3006 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3007 addrconf_add_linklocal(idev, &addr, 0);
3008 else if (prefix_route)
3009 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3010 }
3011 }
3012
3013 static void addrconf_dev_config(struct net_device *dev)
3014 {
3015 struct inet6_dev *idev;
3016
3017 ASSERT_RTNL();
3018
3019 if ((dev->type != ARPHRD_ETHER) &&
3020 (dev->type != ARPHRD_FDDI) &&
3021 (dev->type != ARPHRD_ARCNET) &&
3022 (dev->type != ARPHRD_INFINIBAND) &&
3023 (dev->type != ARPHRD_IEEE802154) &&
3024 (dev->type != ARPHRD_IEEE1394) &&
3025 (dev->type != ARPHRD_TUNNEL6) &&
3026 (dev->type != ARPHRD_6LOWPAN)) {
3027 /* Alas, we support only Ethernet autoconfiguration. */
3028 return;
3029 }
3030
3031 idev = addrconf_add_dev(dev);
3032 if (IS_ERR(idev))
3033 return;
3034
3035 addrconf_addr_gen(idev, false);
3036 }
3037
3038 #if IS_ENABLED(CONFIG_IPV6_SIT)
3039 static void addrconf_sit_config(struct net_device *dev)
3040 {
3041 struct inet6_dev *idev;
3042
3043 ASSERT_RTNL();
3044
3045 /*
3046 * Configure the tunnel with one of our IPv4
3047 * addresses... we should configure all of
3048 * our v4 addrs in the tunnel
3049 */
3050
3051 idev = ipv6_find_idev(dev);
3052 if (!idev) {
3053 pr_debug("%s: add_dev failed\n", __func__);
3054 return;
3055 }
3056
3057 if (dev->priv_flags & IFF_ISATAP) {
3058 addrconf_addr_gen(idev, false);
3059 return;
3060 }
3061
3062 sit_add_v4_addrs(idev);
3063
3064 if (dev->flags&IFF_POINTOPOINT)
3065 addrconf_add_mroute(dev);
3066 }
3067 #endif
3068
3069 #if IS_ENABLED(CONFIG_NET_IPGRE)
3070 static void addrconf_gre_config(struct net_device *dev)
3071 {
3072 struct inet6_dev *idev;
3073
3074 ASSERT_RTNL();
3075
3076 idev = ipv6_find_idev(dev);
3077 if (!idev) {
3078 pr_debug("%s: add_dev failed\n", __func__);
3079 return;
3080 }
3081
3082 addrconf_addr_gen(idev, true);
3083 }
3084 #endif
3085
3086 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3087 void *ptr)
3088 {
3089 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3090 struct inet6_dev *idev = __in6_dev_get(dev);
3091 int run_pending = 0;
3092 int err;
3093
3094 switch (event) {
3095 case NETDEV_REGISTER:
3096 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3097 idev = ipv6_add_dev(dev);
3098 if (IS_ERR(idev))
3099 return notifier_from_errno(PTR_ERR(idev));
3100 }
3101 break;
3102
3103 case NETDEV_UP:
3104 case NETDEV_CHANGE:
3105 if (dev->flags & IFF_SLAVE)
3106 break;
3107
3108 if (idev && idev->cnf.disable_ipv6)
3109 break;
3110
3111 if (event == NETDEV_UP) {
3112 if (!addrconf_qdisc_ok(dev)) {
3113 /* device is not ready yet. */
3114 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3115 dev->name);
3116 break;
3117 }
3118
3119 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3120 idev = ipv6_add_dev(dev);
3121
3122 if (!IS_ERR_OR_NULL(idev)) {
3123 idev->if_flags |= IF_READY;
3124 run_pending = 1;
3125 }
3126 } else {
3127 if (!addrconf_qdisc_ok(dev)) {
3128 /* device is still not ready. */
3129 break;
3130 }
3131
3132 if (idev) {
3133 if (idev->if_flags & IF_READY)
3134 /* device is already configured. */
3135 break;
3136 idev->if_flags |= IF_READY;
3137 }
3138
3139 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3140 dev->name);
3141
3142 run_pending = 1;
3143 }
3144
3145 switch (dev->type) {
3146 #if IS_ENABLED(CONFIG_IPV6_SIT)
3147 case ARPHRD_SIT:
3148 addrconf_sit_config(dev);
3149 break;
3150 #endif
3151 #if IS_ENABLED(CONFIG_NET_IPGRE)
3152 case ARPHRD_IPGRE:
3153 addrconf_gre_config(dev);
3154 break;
3155 #endif
3156 case ARPHRD_LOOPBACK:
3157 init_loopback(dev);
3158 break;
3159
3160 default:
3161 addrconf_dev_config(dev);
3162 break;
3163 }
3164
3165 if (!IS_ERR_OR_NULL(idev)) {
3166 if (run_pending)
3167 addrconf_dad_run(idev);
3168
3169 /*
3170 * If the MTU changed during the interface down,
3171 * when the interface up, the changed MTU must be
3172 * reflected in the idev as well as routers.
3173 */
3174 if (idev->cnf.mtu6 != dev->mtu &&
3175 dev->mtu >= IPV6_MIN_MTU) {
3176 rt6_mtu_change(dev, dev->mtu);
3177 idev->cnf.mtu6 = dev->mtu;
3178 }
3179 idev->tstamp = jiffies;
3180 inet6_ifinfo_notify(RTM_NEWLINK, idev);
3181
3182 /*
3183 * If the changed mtu during down is lower than
3184 * IPV6_MIN_MTU stop IPv6 on this interface.
3185 */
3186 if (dev->mtu < IPV6_MIN_MTU)
3187 addrconf_ifdown(dev, 1);
3188 }
3189 break;
3190
3191 case NETDEV_CHANGEMTU:
3192 if (idev && dev->mtu >= IPV6_MIN_MTU) {
3193 rt6_mtu_change(dev, dev->mtu);
3194 idev->cnf.mtu6 = dev->mtu;
3195 break;
3196 }
3197
3198 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3199 idev = ipv6_add_dev(dev);
3200 if (!IS_ERR(idev))
3201 break;
3202 }
3203
3204 /*
3205 * if MTU under IPV6_MIN_MTU.
3206 * Stop IPv6 on this interface.
3207 */
3208
3209 case NETDEV_DOWN:
3210 case NETDEV_UNREGISTER:
3211 /*
3212 * Remove all addresses from this interface.
3213 */
3214 addrconf_ifdown(dev, event != NETDEV_DOWN);
3215 break;
3216
3217 case NETDEV_CHANGENAME:
3218 if (idev) {
3219 snmp6_unregister_dev(idev);
3220 addrconf_sysctl_unregister(idev);
3221 err = addrconf_sysctl_register(idev);
3222 if (err)
3223 return notifier_from_errno(err);
3224 err = snmp6_register_dev(idev);
3225 if (err) {
3226 addrconf_sysctl_unregister(idev);
3227 return notifier_from_errno(err);
3228 }
3229 }
3230 break;
3231
3232 case NETDEV_PRE_TYPE_CHANGE:
3233 case NETDEV_POST_TYPE_CHANGE:
3234 addrconf_type_change(dev, event);
3235 break;
3236 }
3237
3238 return NOTIFY_OK;
3239 }
3240
3241 /*
3242 * addrconf module should be notified of a device going up
3243 */
3244 static struct notifier_block ipv6_dev_notf = {
3245 .notifier_call = addrconf_notify,
3246 };
3247
3248 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3249 {
3250 struct inet6_dev *idev;
3251 ASSERT_RTNL();
3252
3253 idev = __in6_dev_get(dev);
3254
3255 if (event == NETDEV_POST_TYPE_CHANGE)
3256 ipv6_mc_remap(idev);
3257 else if (event == NETDEV_PRE_TYPE_CHANGE)
3258 ipv6_mc_unmap(idev);
3259 }
3260
3261 static int addrconf_ifdown(struct net_device *dev, int how)
3262 {
3263 struct net *net = dev_net(dev);
3264 struct inet6_dev *idev;
3265 struct inet6_ifaddr *ifa;
3266 int state, i;
3267
3268 ASSERT_RTNL();
3269
3270 rt6_ifdown(net, dev);
3271 neigh_ifdown(&nd_tbl, dev);
3272
3273 idev = __in6_dev_get(dev);
3274 if (!idev)
3275 return -ENODEV;
3276
3277 /*
3278 * Step 1: remove reference to ipv6 device from parent device.
3279 * Do not dev_put!
3280 */
3281 if (how) {
3282 idev->dead = 1;
3283
3284 /* protected by rtnl_lock */
3285 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3286
3287 /* Step 1.5: remove snmp6 entry */
3288 snmp6_unregister_dev(idev);
3289
3290 }
3291
3292 /* Step 2: clear hash table */
3293 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3294 struct hlist_head *h = &inet6_addr_lst[i];
3295
3296 spin_lock_bh(&addrconf_hash_lock);
3297 restart:
3298 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3299 if (ifa->idev == idev) {
3300 hlist_del_init_rcu(&ifa->addr_lst);
3301 addrconf_del_dad_work(ifa);
3302 goto restart;
3303 }
3304 }
3305 spin_unlock_bh(&addrconf_hash_lock);
3306 }
3307
3308 write_lock_bh(&idev->lock);
3309
3310 addrconf_del_rs_timer(idev);
3311
3312 /* Step 2: clear flags for stateless addrconf */
3313 if (!how)
3314 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3315
3316 if (how && del_timer(&idev->regen_timer))
3317 in6_dev_put(idev);
3318
3319 /* Step 3: clear tempaddr list */
3320 while (!list_empty(&idev->tempaddr_list)) {
3321 ifa = list_first_entry(&idev->tempaddr_list,
3322 struct inet6_ifaddr, tmp_list);
3323 list_del(&ifa->tmp_list);
3324 write_unlock_bh(&idev->lock);
3325 spin_lock_bh(&ifa->lock);
3326
3327 if (ifa->ifpub) {
3328 in6_ifa_put(ifa->ifpub);
3329 ifa->ifpub = NULL;
3330 }
3331 spin_unlock_bh(&ifa->lock);
3332 in6_ifa_put(ifa);
3333 write_lock_bh(&idev->lock);
3334 }
3335
3336 while (!list_empty(&idev->addr_list)) {
3337 ifa = list_first_entry(&idev->addr_list,
3338 struct inet6_ifaddr, if_list);
3339 addrconf_del_dad_work(ifa);
3340
3341 list_del(&ifa->if_list);
3342
3343 write_unlock_bh(&idev->lock);
3344
3345 spin_lock_bh(&ifa->lock);
3346 state = ifa->state;
3347 ifa->state = INET6_IFADDR_STATE_DEAD;
3348 spin_unlock_bh(&ifa->lock);
3349
3350 if (state != INET6_IFADDR_STATE_DEAD) {
3351 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3352 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3353 }
3354 in6_ifa_put(ifa);
3355
3356 write_lock_bh(&idev->lock);
3357 }
3358
3359 write_unlock_bh(&idev->lock);
3360
3361 /* Step 5: Discard anycast and multicast list */
3362 if (how) {
3363 ipv6_ac_destroy_dev(idev);
3364 ipv6_mc_destroy_dev(idev);
3365 } else {
3366 ipv6_mc_down(idev);
3367 }
3368
3369 idev->tstamp = jiffies;
3370
3371 /* Last: Shot the device (if unregistered) */
3372 if (how) {
3373 addrconf_sysctl_unregister(idev);
3374 neigh_parms_release(&nd_tbl, idev->nd_parms);
3375 neigh_ifdown(&nd_tbl, dev);
3376 in6_dev_put(idev);
3377 }
3378 return 0;
3379 }
3380
3381 static void addrconf_rs_timer(unsigned long data)
3382 {
3383 struct inet6_dev *idev = (struct inet6_dev *)data;
3384 struct net_device *dev = idev->dev;
3385 struct in6_addr lladdr;
3386
3387 write_lock(&idev->lock);
3388 if (idev->dead || !(idev->if_flags & IF_READY))
3389 goto out;
3390
3391 if (!ipv6_accept_ra(idev))
3392 goto out;
3393
3394 /* Announcement received after solicitation was sent */
3395 if (idev->if_flags & IF_RA_RCVD)
3396 goto out;
3397
3398 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
3399 write_unlock(&idev->lock);
3400 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3401 ndisc_send_rs(dev, &lladdr,
3402 &in6addr_linklocal_allrouters);
3403 else
3404 goto put;
3405
3406 write_lock(&idev->lock);
3407 /* The wait after the last probe can be shorter */
3408 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3409 idev->cnf.rtr_solicits) ?
3410 idev->cnf.rtr_solicit_delay :
3411 idev->cnf.rtr_solicit_interval);
3412 } else {
3413 /*
3414 * Note: we do not support deprecated "all on-link"
3415 * assumption any longer.
3416 */
3417 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
3418 }
3419
3420 out:
3421 write_unlock(&idev->lock);
3422 put:
3423 in6_dev_put(idev);
3424 }
3425
3426 /*
3427 * Duplicate Address Detection
3428 */
3429 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3430 {
3431 unsigned long rand_num;
3432 struct inet6_dev *idev = ifp->idev;
3433
3434 if (ifp->flags & IFA_F_OPTIMISTIC)
3435 rand_num = 0;
3436 else
3437 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
3438
3439 ifp->dad_probes = idev->cnf.dad_transmits;
3440 addrconf_mod_dad_work(ifp, rand_num);
3441 }
3442
3443 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
3444 {
3445 struct inet6_dev *idev = ifp->idev;
3446 struct net_device *dev = idev->dev;
3447
3448 addrconf_join_solict(dev, &ifp->addr);
3449
3450 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
3451
3452 read_lock_bh(&idev->lock);
3453 spin_lock(&ifp->lock);
3454 if (ifp->state == INET6_IFADDR_STATE_DEAD)
3455 goto out;
3456
3457 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
3458 idev->cnf.accept_dad < 1 ||
3459 !(ifp->flags&IFA_F_TENTATIVE) ||
3460 ifp->flags & IFA_F_NODAD) {
3461 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3462 spin_unlock(&ifp->lock);
3463 read_unlock_bh(&idev->lock);
3464
3465 addrconf_dad_completed(ifp);
3466 return;
3467 }
3468
3469 if (!(idev->if_flags & IF_READY)) {
3470 spin_unlock(&ifp->lock);
3471 read_unlock_bh(&idev->lock);
3472 /*
3473 * If the device is not ready:
3474 * - keep it tentative if it is a permanent address.
3475 * - otherwise, kill it.
3476 */
3477 in6_ifa_hold(ifp);
3478 addrconf_dad_stop(ifp, 0);
3479 return;
3480 }
3481
3482 /*
3483 * Optimistic nodes can start receiving
3484 * Frames right away
3485 */
3486 if (ifp->flags & IFA_F_OPTIMISTIC) {
3487 ip6_ins_rt(ifp->rt);
3488 if (ipv6_use_optimistic_addr(idev)) {
3489 /* Because optimistic nodes can use this address,
3490 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3491 */
3492 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3493 }
3494 }
3495
3496 addrconf_dad_kick(ifp);
3497 out:
3498 spin_unlock(&ifp->lock);
3499 read_unlock_bh(&idev->lock);
3500 }
3501
3502 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3503 {
3504 bool begin_dad = false;
3505
3506 spin_lock_bh(&ifp->lock);
3507 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3508 ifp->state = INET6_IFADDR_STATE_PREDAD;
3509 begin_dad = true;
3510 }
3511 spin_unlock_bh(&ifp->lock);
3512
3513 if (begin_dad)
3514 addrconf_mod_dad_work(ifp, 0);
3515 }
3516
3517 static void addrconf_dad_work(struct work_struct *w)
3518 {
3519 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3520 struct inet6_ifaddr,
3521 dad_work);
3522 struct inet6_dev *idev = ifp->idev;
3523 struct in6_addr mcaddr;
3524
3525 enum {
3526 DAD_PROCESS,
3527 DAD_BEGIN,
3528 DAD_ABORT,
3529 } action = DAD_PROCESS;
3530
3531 rtnl_lock();
3532
3533 spin_lock_bh(&ifp->lock);
3534 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3535 action = DAD_BEGIN;
3536 ifp->state = INET6_IFADDR_STATE_DAD;
3537 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3538 action = DAD_ABORT;
3539 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3540 }
3541 spin_unlock_bh(&ifp->lock);
3542
3543 if (action == DAD_BEGIN) {
3544 addrconf_dad_begin(ifp);
3545 goto out;
3546 } else if (action == DAD_ABORT) {
3547 addrconf_dad_stop(ifp, 1);
3548 goto out;
3549 }
3550
3551 if (!ifp->dad_probes && addrconf_dad_end(ifp))
3552 goto out;
3553
3554 write_lock_bh(&idev->lock);
3555 if (idev->dead || !(idev->if_flags & IF_READY)) {
3556 write_unlock_bh(&idev->lock);
3557 goto out;
3558 }
3559
3560 spin_lock(&ifp->lock);
3561 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3562 spin_unlock(&ifp->lock);
3563 write_unlock_bh(&idev->lock);
3564 goto out;
3565 }
3566
3567 if (ifp->dad_probes == 0) {
3568 /*
3569 * DAD was successful
3570 */
3571
3572 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
3573 spin_unlock(&ifp->lock);
3574 write_unlock_bh(&idev->lock);
3575
3576 addrconf_dad_completed(ifp);
3577
3578 goto out;
3579 }
3580
3581 ifp->dad_probes--;
3582 addrconf_mod_dad_work(ifp,
3583 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
3584 spin_unlock(&ifp->lock);
3585 write_unlock_bh(&idev->lock);
3586
3587 /* send a neighbour solicitation for our addr */
3588 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3589 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
3590 out:
3591 in6_ifa_put(ifp);
3592 rtnl_unlock();
3593 }
3594
3595 /* ifp->idev must be at least read locked */
3596 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3597 {
3598 struct inet6_ifaddr *ifpiter;
3599 struct inet6_dev *idev = ifp->idev;
3600
3601 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3602 if (ifpiter->scope > IFA_LINK)
3603 break;
3604 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3605 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3606 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3607 IFA_F_PERMANENT)
3608 return false;
3609 }
3610 return true;
3611 }
3612
3613 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3614 {
3615 struct net_device *dev = ifp->idev->dev;
3616 struct in6_addr lladdr;
3617 bool send_rs, send_mld;
3618
3619 addrconf_del_dad_work(ifp);
3620
3621 /*
3622 * Configure the address for reception. Now it is valid.
3623 */
3624
3625 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3626
3627 /* If added prefix is link local and we are prepared to process
3628 router advertisements, start sending router solicitations.
3629 */
3630
3631 read_lock_bh(&ifp->idev->lock);
3632 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3633 send_rs = send_mld &&
3634 ipv6_accept_ra(ifp->idev) &&
3635 ifp->idev->cnf.rtr_solicits > 0 &&
3636 (dev->flags&IFF_LOOPBACK) == 0;
3637 read_unlock_bh(&ifp->idev->lock);
3638
3639 /* While dad is in progress mld report's source address is in6_addrany.
3640 * Resend with proper ll now.
3641 */
3642 if (send_mld)
3643 ipv6_mc_dad_complete(ifp->idev);
3644
3645 if (send_rs) {
3646 /*
3647 * If a host as already performed a random delay
3648 * [...] as part of DAD [...] there is no need
3649 * to delay again before sending the first RS
3650 */
3651 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3652 return;
3653 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
3654
3655 write_lock_bh(&ifp->idev->lock);
3656 spin_lock(&ifp->lock);
3657 ifp->idev->rs_probes = 1;
3658 ifp->idev->if_flags |= IF_RS_SENT;
3659 addrconf_mod_rs_timer(ifp->idev,
3660 ifp->idev->cnf.rtr_solicit_interval);
3661 spin_unlock(&ifp->lock);
3662 write_unlock_bh(&ifp->idev->lock);
3663 }
3664 }
3665
3666 static void addrconf_dad_run(struct inet6_dev *idev)
3667 {
3668 struct inet6_ifaddr *ifp;
3669
3670 read_lock_bh(&idev->lock);
3671 list_for_each_entry(ifp, &idev->addr_list, if_list) {
3672 spin_lock(&ifp->lock);
3673 if (ifp->flags & IFA_F_TENTATIVE &&
3674 ifp->state == INET6_IFADDR_STATE_DAD)
3675 addrconf_dad_kick(ifp);
3676 spin_unlock(&ifp->lock);
3677 }
3678 read_unlock_bh(&idev->lock);
3679 }
3680
3681 #ifdef CONFIG_PROC_FS
3682 struct if6_iter_state {
3683 struct seq_net_private p;
3684 int bucket;
3685 int offset;
3686 };
3687
3688 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
3689 {
3690 struct inet6_ifaddr *ifa = NULL;
3691 struct if6_iter_state *state = seq->private;
3692 struct net *net = seq_file_net(seq);
3693 int p = 0;
3694
3695 /* initial bucket if pos is 0 */
3696 if (pos == 0) {
3697 state->bucket = 0;
3698 state->offset = 0;
3699 }
3700
3701 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3702 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
3703 addr_lst) {
3704 if (!net_eq(dev_net(ifa->idev->dev), net))
3705 continue;
3706 /* sync with offset */
3707 if (p < state->offset) {
3708 p++;
3709 continue;
3710 }
3711 state->offset++;
3712 return ifa;
3713 }
3714
3715 /* prepare for next bucket */
3716 state->offset = 0;
3717 p = 0;
3718 }
3719 return NULL;
3720 }
3721
3722 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3723 struct inet6_ifaddr *ifa)
3724 {
3725 struct if6_iter_state *state = seq->private;
3726 struct net *net = seq_file_net(seq);
3727
3728 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
3729 if (!net_eq(dev_net(ifa->idev->dev), net))
3730 continue;
3731 state->offset++;
3732 return ifa;
3733 }
3734
3735 while (++state->bucket < IN6_ADDR_HSIZE) {
3736 state->offset = 0;
3737 hlist_for_each_entry_rcu_bh(ifa,
3738 &inet6_addr_lst[state->bucket], addr_lst) {
3739 if (!net_eq(dev_net(ifa->idev->dev), net))
3740 continue;
3741 state->offset++;
3742 return ifa;
3743 }
3744 }
3745
3746 return NULL;
3747 }
3748
3749 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
3750 __acquires(rcu_bh)
3751 {
3752 rcu_read_lock_bh();
3753 return if6_get_first(seq, *pos);
3754 }
3755
3756 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3757 {
3758 struct inet6_ifaddr *ifa;
3759
3760 ifa = if6_get_next(seq, v);
3761 ++*pos;
3762 return ifa;
3763 }
3764
3765 static void if6_seq_stop(struct seq_file *seq, void *v)
3766 __releases(rcu_bh)
3767 {
3768 rcu_read_unlock_bh();
3769 }
3770
3771 static int if6_seq_show(struct seq_file *seq, void *v)
3772 {
3773 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
3774 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3775 &ifp->addr,
3776 ifp->idev->dev->ifindex,
3777 ifp->prefix_len,
3778 ifp->scope,
3779 (u8) ifp->flags,
3780 ifp->idev->dev->name);
3781 return 0;
3782 }
3783
3784 static const struct seq_operations if6_seq_ops = {
3785 .start = if6_seq_start,
3786 .next = if6_seq_next,
3787 .show = if6_seq_show,
3788 .stop = if6_seq_stop,
3789 };
3790
3791 static int if6_seq_open(struct inode *inode, struct file *file)
3792 {
3793 return seq_open_net(inode, file, &if6_seq_ops,
3794 sizeof(struct if6_iter_state));
3795 }
3796
3797 static const struct file_operations if6_fops = {
3798 .owner = THIS_MODULE,
3799 .open = if6_seq_open,
3800 .read = seq_read,
3801 .llseek = seq_lseek,
3802 .release = seq_release_net,
3803 };
3804
3805 static int __net_init if6_proc_net_init(struct net *net)
3806 {
3807 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
3808 return -ENOMEM;
3809 return 0;
3810 }
3811
3812 static void __net_exit if6_proc_net_exit(struct net *net)
3813 {
3814 remove_proc_entry("if_inet6", net->proc_net);
3815 }
3816
3817 static struct pernet_operations if6_proc_net_ops = {
3818 .init = if6_proc_net_init,
3819 .exit = if6_proc_net_exit,
3820 };
3821
3822 int __init if6_proc_init(void)
3823 {
3824 return register_pernet_subsys(&if6_proc_net_ops);
3825 }
3826
3827 void if6_proc_exit(void)
3828 {
3829 unregister_pernet_subsys(&if6_proc_net_ops);
3830 }
3831 #endif /* CONFIG_PROC_FS */
3832
3833 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3834 /* Check if address is a home address configured on any interface. */
3835 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
3836 {
3837 int ret = 0;
3838 struct inet6_ifaddr *ifp = NULL;
3839 unsigned int hash = inet6_addr_hash(addr);
3840
3841 rcu_read_lock_bh();
3842 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3843 if (!net_eq(dev_net(ifp->idev->dev), net))
3844 continue;
3845 if (ipv6_addr_equal(&ifp->addr, addr) &&
3846 (ifp->flags & IFA_F_HOMEADDRESS)) {
3847 ret = 1;
3848 break;
3849 }
3850 }
3851 rcu_read_unlock_bh();
3852 return ret;
3853 }
3854 #endif
3855
3856 /*
3857 * Periodic address status verification
3858 */
3859
3860 static void addrconf_verify_rtnl(void)
3861 {
3862 unsigned long now, next, next_sec, next_sched;
3863 struct inet6_ifaddr *ifp;
3864 int i;
3865
3866 ASSERT_RTNL();
3867
3868 rcu_read_lock_bh();
3869 now = jiffies;
3870 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
3871
3872 cancel_delayed_work(&addr_chk_work);
3873
3874 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3875 restart:
3876 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
3877 unsigned long age;
3878
3879 /* When setting preferred_lft to a value not zero or
3880 * infinity, while valid_lft is infinity
3881 * IFA_F_PERMANENT has a non-infinity life time.
3882 */
3883 if ((ifp->flags & IFA_F_PERMANENT) &&
3884 (ifp->prefered_lft == INFINITY_LIFE_TIME))
3885 continue;
3886
3887 spin_lock(&ifp->lock);
3888 /* We try to batch several events at once. */
3889 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
3890
3891 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3892 age >= ifp->valid_lft) {
3893 spin_unlock(&ifp->lock);
3894 in6_ifa_hold(ifp);
3895 ipv6_del_addr(ifp);
3896 goto restart;
3897 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3898 spin_unlock(&ifp->lock);
3899 continue;
3900 } else if (age >= ifp->prefered_lft) {
3901 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3902 int deprecate = 0;
3903
3904 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3905 deprecate = 1;
3906 ifp->flags |= IFA_F_DEPRECATED;
3907 }
3908
3909 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3910 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
3911 next = ifp->tstamp + ifp->valid_lft * HZ;
3912
3913 spin_unlock(&ifp->lock);
3914
3915 if (deprecate) {
3916 in6_ifa_hold(ifp);
3917
3918 ipv6_ifa_notify(0, ifp);
3919 in6_ifa_put(ifp);
3920 goto restart;
3921 }
3922 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3923 !(ifp->flags&IFA_F_TENTATIVE)) {
3924 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3925 ifp->idev->cnf.dad_transmits *
3926 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
3927
3928 if (age >= ifp->prefered_lft - regen_advance) {
3929 struct inet6_ifaddr *ifpub = ifp->ifpub;
3930 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3931 next = ifp->tstamp + ifp->prefered_lft * HZ;
3932 if (!ifp->regen_count && ifpub) {
3933 ifp->regen_count++;
3934 in6_ifa_hold(ifp);
3935 in6_ifa_hold(ifpub);
3936 spin_unlock(&ifp->lock);
3937
3938 spin_lock(&ifpub->lock);
3939 ifpub->regen_count = 0;
3940 spin_unlock(&ifpub->lock);
3941 ipv6_create_tempaddr(ifpub, ifp);
3942 in6_ifa_put(ifpub);
3943 in6_ifa_put(ifp);
3944 goto restart;
3945 }
3946 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3947 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3948 spin_unlock(&ifp->lock);
3949 } else {
3950 /* ifp->prefered_lft <= ifp->valid_lft */
3951 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3952 next = ifp->tstamp + ifp->prefered_lft * HZ;
3953 spin_unlock(&ifp->lock);
3954 }
3955 }
3956 }
3957
3958 next_sec = round_jiffies_up(next);
3959 next_sched = next;
3960
3961 /* If rounded timeout is accurate enough, accept it. */
3962 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3963 next_sched = next_sec;
3964
3965 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3966 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3967 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3968
3969 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3970 now, next, next_sec, next_sched);
3971 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
3972 rcu_read_unlock_bh();
3973 }
3974
3975 static void addrconf_verify_work(struct work_struct *w)
3976 {
3977 rtnl_lock();
3978 addrconf_verify_rtnl();
3979 rtnl_unlock();
3980 }
3981
3982 static void addrconf_verify(void)
3983 {
3984 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3985 }
3986
3987 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3988 struct in6_addr **peer_pfx)
3989 {
3990 struct in6_addr *pfx = NULL;
3991
3992 *peer_pfx = NULL;
3993
3994 if (addr)
3995 pfx = nla_data(addr);
3996
3997 if (local) {
3998 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3999 *peer_pfx = pfx;
4000 pfx = nla_data(local);
4001 }
4002
4003 return pfx;
4004 }
4005
4006 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4007 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4008 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4009 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
4010 [IFA_FLAGS] = { .len = sizeof(u32) },
4011 };
4012
4013 static int
4014 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
4015 {
4016 struct net *net = sock_net(skb->sk);
4017 struct ifaddrmsg *ifm;
4018 struct nlattr *tb[IFA_MAX+1];
4019 struct in6_addr *pfx, *peer_pfx;
4020 u32 ifa_flags;
4021 int err;
4022
4023 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4024 if (err < 0)
4025 return err;
4026
4027 ifm = nlmsg_data(nlh);
4028 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4029 if (!pfx)
4030 return -EINVAL;
4031
4032 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4033
4034 /* We ignore other flags so far. */
4035 ifa_flags &= IFA_F_MANAGETEMPADDR;
4036
4037 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4038 ifm->ifa_prefixlen);
4039 }
4040
4041 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
4042 u32 prefered_lft, u32 valid_lft)
4043 {
4044 u32 flags;
4045 clock_t expires;
4046 unsigned long timeout;
4047 bool was_managetempaddr;
4048 bool had_prefixroute;
4049
4050 ASSERT_RTNL();
4051
4052 if (!valid_lft || (prefered_lft > valid_lft))
4053 return -EINVAL;
4054
4055 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4056 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4057 return -EINVAL;
4058
4059 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4060 if (addrconf_finite_timeout(timeout)) {
4061 expires = jiffies_to_clock_t(timeout * HZ);
4062 valid_lft = timeout;
4063 flags = RTF_EXPIRES;
4064 } else {
4065 expires = 0;
4066 flags = 0;
4067 ifa_flags |= IFA_F_PERMANENT;
4068 }
4069
4070 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4071 if (addrconf_finite_timeout(timeout)) {
4072 if (timeout == 0)
4073 ifa_flags |= IFA_F_DEPRECATED;
4074 prefered_lft = timeout;
4075 }
4076
4077 spin_lock_bh(&ifp->lock);
4078 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4079 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4080 !(ifp->flags & IFA_F_NOPREFIXROUTE);
4081 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4082 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4083 IFA_F_NOPREFIXROUTE);
4084 ifp->flags |= ifa_flags;
4085 ifp->tstamp = jiffies;
4086 ifp->valid_lft = valid_lft;
4087 ifp->prefered_lft = prefered_lft;
4088
4089 spin_unlock_bh(&ifp->lock);
4090 if (!(ifp->flags&IFA_F_TENTATIVE))
4091 ipv6_ifa_notify(0, ifp);
4092
4093 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4094 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4095 expires, flags);
4096 } else if (had_prefixroute) {
4097 enum cleanup_prefix_rt_t action;
4098 unsigned long rt_expires;
4099
4100 write_lock_bh(&ifp->idev->lock);
4101 action = check_cleanup_prefix_route(ifp, &rt_expires);
4102 write_unlock_bh(&ifp->idev->lock);
4103
4104 if (action != CLEANUP_PREFIX_RT_NOP) {
4105 cleanup_prefix_route(ifp, rt_expires,
4106 action == CLEANUP_PREFIX_RT_DEL);
4107 }
4108 }
4109
4110 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4111 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4112 valid_lft = prefered_lft = 0;
4113 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4114 !was_managetempaddr, jiffies);
4115 }
4116
4117 addrconf_verify_rtnl();
4118
4119 return 0;
4120 }
4121
4122 static int
4123 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
4124 {
4125 struct net *net = sock_net(skb->sk);
4126 struct ifaddrmsg *ifm;
4127 struct nlattr *tb[IFA_MAX+1];
4128 struct in6_addr *pfx, *peer_pfx;
4129 struct inet6_ifaddr *ifa;
4130 struct net_device *dev;
4131 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
4132 u32 ifa_flags;
4133 int err;
4134
4135 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4136 if (err < 0)
4137 return err;
4138
4139 ifm = nlmsg_data(nlh);
4140 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4141 if (!pfx)
4142 return -EINVAL;
4143
4144 if (tb[IFA_CACHEINFO]) {
4145 struct ifa_cacheinfo *ci;
4146
4147 ci = nla_data(tb[IFA_CACHEINFO]);
4148 valid_lft = ci->ifa_valid;
4149 preferred_lft = ci->ifa_prefered;
4150 } else {
4151 preferred_lft = INFINITY_LIFE_TIME;
4152 valid_lft = INFINITY_LIFE_TIME;
4153 }
4154
4155 dev = __dev_get_by_index(net, ifm->ifa_index);
4156 if (!dev)
4157 return -ENODEV;
4158
4159 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4160
4161 /* We ignore other flags so far. */
4162 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4163 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
4164
4165 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
4166 if (!ifa) {
4167 /*
4168 * It would be best to check for !NLM_F_CREATE here but
4169 * userspace already relies on not having to provide this.
4170 */
4171 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
4172 ifm->ifa_prefixlen, ifa_flags,
4173 preferred_lft, valid_lft);
4174 }
4175
4176 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4177 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4178 err = -EEXIST;
4179 else
4180 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
4181
4182 in6_ifa_put(ifa);
4183
4184 return err;
4185 }
4186
4187 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
4188 u8 scope, int ifindex)
4189 {
4190 struct ifaddrmsg *ifm;
4191
4192 ifm = nlmsg_data(nlh);
4193 ifm->ifa_family = AF_INET6;
4194 ifm->ifa_prefixlen = prefixlen;
4195 ifm->ifa_flags = flags;
4196 ifm->ifa_scope = scope;
4197 ifm->ifa_index = ifindex;
4198 }
4199
4200 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4201 unsigned long tstamp, u32 preferred, u32 valid)
4202 {
4203 struct ifa_cacheinfo ci;
4204
4205 ci.cstamp = cstamp_delta(cstamp);
4206 ci.tstamp = cstamp_delta(tstamp);
4207 ci.ifa_prefered = preferred;
4208 ci.ifa_valid = valid;
4209
4210 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4211 }
4212
4213 static inline int rt_scope(int ifa_scope)
4214 {
4215 if (ifa_scope & IFA_HOST)
4216 return RT_SCOPE_HOST;
4217 else if (ifa_scope & IFA_LINK)
4218 return RT_SCOPE_LINK;
4219 else if (ifa_scope & IFA_SITE)
4220 return RT_SCOPE_SITE;
4221 else
4222 return RT_SCOPE_UNIVERSE;
4223 }
4224
4225 static inline int inet6_ifaddr_msgsize(void)
4226 {
4227 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
4228 + nla_total_size(16) /* IFA_LOCAL */
4229 + nla_total_size(16) /* IFA_ADDRESS */
4230 + nla_total_size(sizeof(struct ifa_cacheinfo))
4231 + nla_total_size(4) /* IFA_FLAGS */;
4232 }
4233
4234 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4235 u32 portid, u32 seq, int event, unsigned int flags)
4236 {
4237 struct nlmsghdr *nlh;
4238 u32 preferred, valid;
4239
4240 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4241 if (!nlh)
4242 return -EMSGSIZE;
4243
4244 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4245 ifa->idev->dev->ifindex);
4246
4247 if (!((ifa->flags&IFA_F_PERMANENT) &&
4248 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
4249 preferred = ifa->prefered_lft;
4250 valid = ifa->valid_lft;
4251 if (preferred != INFINITY_LIFE_TIME) {
4252 long tval = (jiffies - ifa->tstamp)/HZ;
4253 if (preferred > tval)
4254 preferred -= tval;
4255 else
4256 preferred = 0;
4257 if (valid != INFINITY_LIFE_TIME) {
4258 if (valid > tval)
4259 valid -= tval;
4260 else
4261 valid = 0;
4262 }
4263 }
4264 } else {
4265 preferred = INFINITY_LIFE_TIME;
4266 valid = INFINITY_LIFE_TIME;
4267 }
4268
4269 if (!ipv6_addr_any(&ifa->peer_addr)) {
4270 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4271 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4272 goto error;
4273 } else
4274 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4275 goto error;
4276
4277 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4278 goto error;
4279
4280 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4281 goto error;
4282
4283 nlmsg_end(skb, nlh);
4284 return 0;
4285
4286 error:
4287 nlmsg_cancel(skb, nlh);
4288 return -EMSGSIZE;
4289 }
4290
4291 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4292 u32 portid, u32 seq, int event, u16 flags)
4293 {
4294 struct nlmsghdr *nlh;
4295 u8 scope = RT_SCOPE_UNIVERSE;
4296 int ifindex = ifmca->idev->dev->ifindex;
4297
4298 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4299 scope = RT_SCOPE_SITE;
4300
4301 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4302 if (!nlh)
4303 return -EMSGSIZE;
4304
4305 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4306 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4307 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4308 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4309 nlmsg_cancel(skb, nlh);
4310 return -EMSGSIZE;
4311 }
4312
4313 nlmsg_end(skb, nlh);
4314 return 0;
4315 }
4316
4317 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4318 u32 portid, u32 seq, int event, unsigned int flags)
4319 {
4320 struct nlmsghdr *nlh;
4321 u8 scope = RT_SCOPE_UNIVERSE;
4322 int ifindex = ifaca->aca_idev->dev->ifindex;
4323
4324 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4325 scope = RT_SCOPE_SITE;
4326
4327 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4328 if (!nlh)
4329 return -EMSGSIZE;
4330
4331 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4332 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4333 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4334 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4335 nlmsg_cancel(skb, nlh);
4336 return -EMSGSIZE;
4337 }
4338
4339 nlmsg_end(skb, nlh);
4340 return 0;
4341 }
4342
4343 enum addr_type_t {
4344 UNICAST_ADDR,
4345 MULTICAST_ADDR,
4346 ANYCAST_ADDR,
4347 };
4348
4349 /* called with rcu_read_lock() */
4350 static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4351 struct netlink_callback *cb, enum addr_type_t type,
4352 int s_ip_idx, int *p_ip_idx)
4353 {
4354 struct ifmcaddr6 *ifmca;
4355 struct ifacaddr6 *ifaca;
4356 int err = 1;
4357 int ip_idx = *p_ip_idx;
4358
4359 read_lock_bh(&idev->lock);
4360 switch (type) {
4361 case UNICAST_ADDR: {
4362 struct inet6_ifaddr *ifa;
4363
4364 /* unicast address incl. temp addr */
4365 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4366 if (++ip_idx < s_ip_idx)
4367 continue;
4368 err = inet6_fill_ifaddr(skb, ifa,
4369 NETLINK_CB(cb->skb).portid,
4370 cb->nlh->nlmsg_seq,
4371 RTM_NEWADDR,
4372 NLM_F_MULTI);
4373 if (err < 0)
4374 break;
4375 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4376 }
4377 break;
4378 }
4379 case MULTICAST_ADDR:
4380 /* multicast address */
4381 for (ifmca = idev->mc_list; ifmca;
4382 ifmca = ifmca->next, ip_idx++) {
4383 if (ip_idx < s_ip_idx)
4384 continue;
4385 err = inet6_fill_ifmcaddr(skb, ifmca,
4386 NETLINK_CB(cb->skb).portid,
4387 cb->nlh->nlmsg_seq,
4388 RTM_GETMULTICAST,
4389 NLM_F_MULTI);
4390 if (err < 0)
4391 break;
4392 }
4393 break;
4394 case ANYCAST_ADDR:
4395 /* anycast address */
4396 for (ifaca = idev->ac_list; ifaca;
4397 ifaca = ifaca->aca_next, ip_idx++) {
4398 if (ip_idx < s_ip_idx)
4399 continue;
4400 err = inet6_fill_ifacaddr(skb, ifaca,
4401 NETLINK_CB(cb->skb).portid,
4402 cb->nlh->nlmsg_seq,
4403 RTM_GETANYCAST,
4404 NLM_F_MULTI);
4405 if (err < 0)
4406 break;
4407 }
4408 break;
4409 default:
4410 break;
4411 }
4412 read_unlock_bh(&idev->lock);
4413 *p_ip_idx = ip_idx;
4414 return err;
4415 }
4416
4417 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4418 enum addr_type_t type)
4419 {
4420 struct net *net = sock_net(skb->sk);
4421 int h, s_h;
4422 int idx, ip_idx;
4423 int s_idx, s_ip_idx;
4424 struct net_device *dev;
4425 struct inet6_dev *idev;
4426 struct hlist_head *head;
4427
4428 s_h = cb->args[0];
4429 s_idx = idx = cb->args[1];
4430 s_ip_idx = ip_idx = cb->args[2];
4431
4432 rcu_read_lock();
4433 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4434 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4435 idx = 0;
4436 head = &net->dev_index_head[h];
4437 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4438 if (idx < s_idx)
4439 goto cont;
4440 if (h > s_h || idx > s_idx)
4441 s_ip_idx = 0;
4442 ip_idx = 0;
4443 idev = __in6_dev_get(dev);
4444 if (!idev)
4445 goto cont;
4446
4447 if (in6_dump_addrs(idev, skb, cb, type,
4448 s_ip_idx, &ip_idx) < 0)
4449 goto done;
4450 cont:
4451 idx++;
4452 }
4453 }
4454 done:
4455 rcu_read_unlock();
4456 cb->args[0] = h;
4457 cb->args[1] = idx;
4458 cb->args[2] = ip_idx;
4459
4460 return skb->len;
4461 }
4462
4463 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4464 {
4465 enum addr_type_t type = UNICAST_ADDR;
4466
4467 return inet6_dump_addr(skb, cb, type);
4468 }
4469
4470 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4471 {
4472 enum addr_type_t type = MULTICAST_ADDR;
4473
4474 return inet6_dump_addr(skb, cb, type);
4475 }
4476
4477
4478 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4479 {
4480 enum addr_type_t type = ANYCAST_ADDR;
4481
4482 return inet6_dump_addr(skb, cb, type);
4483 }
4484
4485 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4486 {
4487 struct net *net = sock_net(in_skb->sk);
4488 struct ifaddrmsg *ifm;
4489 struct nlattr *tb[IFA_MAX+1];
4490 struct in6_addr *addr = NULL, *peer;
4491 struct net_device *dev = NULL;
4492 struct inet6_ifaddr *ifa;
4493 struct sk_buff *skb;
4494 int err;
4495
4496 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4497 if (err < 0)
4498 goto errout;
4499
4500 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4501 if (!addr) {
4502 err = -EINVAL;
4503 goto errout;
4504 }
4505
4506 ifm = nlmsg_data(nlh);
4507 if (ifm->ifa_index)
4508 dev = __dev_get_by_index(net, ifm->ifa_index);
4509
4510 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4511 if (!ifa) {
4512 err = -EADDRNOTAVAIL;
4513 goto errout;
4514 }
4515
4516 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4517 if (!skb) {
4518 err = -ENOBUFS;
4519 goto errout_ifa;
4520 }
4521
4522 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
4523 nlh->nlmsg_seq, RTM_NEWADDR, 0);
4524 if (err < 0) {
4525 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4526 WARN_ON(err == -EMSGSIZE);
4527 kfree_skb(skb);
4528 goto errout_ifa;
4529 }
4530 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4531 errout_ifa:
4532 in6_ifa_put(ifa);
4533 errout:
4534 return err;
4535 }
4536
4537 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4538 {
4539 struct sk_buff *skb;
4540 struct net *net = dev_net(ifa->idev->dev);
4541 int err = -ENOBUFS;
4542
4543 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4544 if (!skb)
4545 goto errout;
4546
4547 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
4548 if (err < 0) {
4549 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4550 WARN_ON(err == -EMSGSIZE);
4551 kfree_skb(skb);
4552 goto errout;
4553 }
4554 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4555 return;
4556 errout:
4557 if (err < 0)
4558 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
4559 }
4560
4561 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4562 __s32 *array, int bytes)
4563 {
4564 BUG_ON(bytes < (DEVCONF_MAX * 4));
4565
4566 memset(array, 0, bytes);
4567 array[DEVCONF_FORWARDING] = cnf->forwarding;
4568 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4569 array[DEVCONF_MTU6] = cnf->mtu6;
4570 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4571 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4572 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4573 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4574 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
4575 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4576 jiffies_to_msecs(cnf->rtr_solicit_interval);
4577 array[DEVCONF_RTR_SOLICIT_DELAY] =
4578 jiffies_to_msecs(cnf->rtr_solicit_delay);
4579 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4580 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4581 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4582 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4583 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4584 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4585 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4586 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4587 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4588 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4589 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4590 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4591 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4592 #ifdef CONFIG_IPV6_ROUTER_PREF
4593 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
4594 array[DEVCONF_RTR_PROBE_INTERVAL] =
4595 jiffies_to_msecs(cnf->rtr_probe_interval);
4596 #ifdef CONFIG_IPV6_ROUTE_INFO
4597 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4598 #endif
4599 #endif
4600 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
4601 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
4602 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4603 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4604 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
4605 #endif
4606 #ifdef CONFIG_IPV6_MROUTE
4607 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4608 #endif
4609 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
4610 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4611 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
4612 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
4613 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4614 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4615 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4616 /* we omit DEVCONF_STABLE_SECRET for now */
4617 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4618 }
4619
4620 static inline size_t inet6_ifla6_size(void)
4621 {
4622 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4623 + nla_total_size(sizeof(struct ifla_cacheinfo))
4624 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4625 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4626 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4627 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4628 }
4629
4630 static inline size_t inet6_if_nlmsg_size(void)
4631 {
4632 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4633 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4634 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4635 + nla_total_size(4) /* IFLA_MTU */
4636 + nla_total_size(4) /* IFLA_LINK */
4637 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4638 }
4639
4640 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
4641 int items, int bytes)
4642 {
4643 int i;
4644 int pad = bytes - sizeof(u64) * items;
4645 BUG_ON(pad < 0);
4646
4647 /* Use put_unaligned() because stats may not be aligned for u64. */
4648 put_unaligned(items, &stats[0]);
4649 for (i = 1; i < items; i++)
4650 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
4651
4652 memset(&stats[items], 0, pad);
4653 }
4654
4655 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
4656 int items, int bytes, size_t syncpoff)
4657 {
4658 int i;
4659 int pad = bytes - sizeof(u64) * items;
4660 BUG_ON(pad < 0);
4661
4662 /* Use put_unaligned() because stats may not be aligned for u64. */
4663 put_unaligned(items, &stats[0]);
4664 for (i = 1; i < items; i++)
4665 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4666
4667 memset(&stats[items], 0, pad);
4668 }
4669
4670 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4671 int bytes)
4672 {
4673 switch (attrtype) {
4674 case IFLA_INET6_STATS:
4675 __snmp6_fill_stats64(stats, idev->stats.ipv6,
4676 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
4677 break;
4678 case IFLA_INET6_ICMP6STATS:
4679 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
4680 break;
4681 }
4682 }
4683
4684 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4685 {
4686 struct nlattr *nla;
4687 struct ifla_cacheinfo ci;
4688
4689 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4690 goto nla_put_failure;
4691 ci.max_reasm_len = IPV6_MAXPLEN;
4692 ci.tstamp = cstamp_delta(idev->tstamp);
4693 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4694 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
4695 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4696 goto nla_put_failure;
4697 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4698 if (!nla)
4699 goto nla_put_failure;
4700 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4701
4702 /* XXX - MC not implemented */
4703
4704 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4705 if (!nla)
4706 goto nla_put_failure;
4707 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4708
4709 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4710 if (!nla)
4711 goto nla_put_failure;
4712 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4713
4714 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4715 if (!nla)
4716 goto nla_put_failure;
4717
4718 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4719 goto nla_put_failure;
4720
4721 read_lock_bh(&idev->lock);
4722 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4723 read_unlock_bh(&idev->lock);
4724
4725 return 0;
4726
4727 nla_put_failure:
4728 return -EMSGSIZE;
4729 }
4730
4731 static size_t inet6_get_link_af_size(const struct net_device *dev)
4732 {
4733 if (!__in6_dev_get(dev))
4734 return 0;
4735
4736 return inet6_ifla6_size();
4737 }
4738
4739 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4740 {
4741 struct inet6_dev *idev = __in6_dev_get(dev);
4742
4743 if (!idev)
4744 return -ENODATA;
4745
4746 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4747 return -EMSGSIZE;
4748
4749 return 0;
4750 }
4751
4752 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4753 {
4754 struct inet6_ifaddr *ifp;
4755 struct net_device *dev = idev->dev;
4756 bool update_rs = false;
4757 struct in6_addr ll_addr;
4758
4759 ASSERT_RTNL();
4760
4761 if (!token)
4762 return -EINVAL;
4763 if (ipv6_addr_any(token))
4764 return -EINVAL;
4765 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4766 return -EINVAL;
4767 if (!ipv6_accept_ra(idev))
4768 return -EINVAL;
4769 if (idev->cnf.rtr_solicits <= 0)
4770 return -EINVAL;
4771
4772 write_lock_bh(&idev->lock);
4773
4774 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4775 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4776
4777 write_unlock_bh(&idev->lock);
4778
4779 if (!idev->dead && (idev->if_flags & IF_READY) &&
4780 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4781 IFA_F_OPTIMISTIC)) {
4782
4783 /* If we're not ready, then normal ifup will take care
4784 * of this. Otherwise, we need to request our rs here.
4785 */
4786 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4787 update_rs = true;
4788 }
4789
4790 write_lock_bh(&idev->lock);
4791
4792 if (update_rs) {
4793 idev->if_flags |= IF_RS_SENT;
4794 idev->rs_probes = 1;
4795 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4796 }
4797
4798 /* Well, that's kinda nasty ... */
4799 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4800 spin_lock(&ifp->lock);
4801 if (ifp->tokenized) {
4802 ifp->valid_lft = 0;
4803 ifp->prefered_lft = 0;
4804 }
4805 spin_unlock(&ifp->lock);
4806 }
4807
4808 write_unlock_bh(&idev->lock);
4809 inet6_ifinfo_notify(RTM_NEWLINK, idev);
4810 addrconf_verify_rtnl();
4811 return 0;
4812 }
4813
4814 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
4815 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
4816 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
4817 };
4818
4819 static int inet6_validate_link_af(const struct net_device *dev,
4820 const struct nlattr *nla)
4821 {
4822 struct nlattr *tb[IFLA_INET6_MAX + 1];
4823
4824 if (dev && !__in6_dev_get(dev))
4825 return -EAFNOSUPPORT;
4826
4827 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
4828 }
4829
4830 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4831 {
4832 int err = -EINVAL;
4833 struct inet6_dev *idev = __in6_dev_get(dev);
4834 struct nlattr *tb[IFLA_INET6_MAX + 1];
4835
4836 if (!idev)
4837 return -EAFNOSUPPORT;
4838
4839 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4840 BUG();
4841
4842 if (tb[IFLA_INET6_TOKEN]) {
4843 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4844 if (err)
4845 return err;
4846 }
4847
4848 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4849 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4850
4851 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4852 mode != IN6_ADDR_GEN_MODE_NONE &&
4853 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY)
4854 return -EINVAL;
4855
4856 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4857 !idev->cnf.stable_secret.initialized &&
4858 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4859 return -EINVAL;
4860
4861 idev->addr_gen_mode = mode;
4862 err = 0;
4863 }
4864
4865 return err;
4866 }
4867
4868 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4869 u32 portid, u32 seq, int event, unsigned int flags)
4870 {
4871 struct net_device *dev = idev->dev;
4872 struct ifinfomsg *hdr;
4873 struct nlmsghdr *nlh;
4874 void *protoinfo;
4875
4876 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4877 if (!nlh)
4878 return -EMSGSIZE;
4879
4880 hdr = nlmsg_data(nlh);
4881 hdr->ifi_family = AF_INET6;
4882 hdr->__ifi_pad = 0;
4883 hdr->ifi_type = dev->type;
4884 hdr->ifi_index = dev->ifindex;
4885 hdr->ifi_flags = dev_get_flags(dev);
4886 hdr->ifi_change = 0;
4887
4888 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4889 (dev->addr_len &&
4890 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4891 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4892 (dev->ifindex != dev_get_iflink(dev) &&
4893 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4894 goto nla_put_failure;
4895 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4896 if (!protoinfo)
4897 goto nla_put_failure;
4898
4899 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4900 goto nla_put_failure;
4901
4902 nla_nest_end(skb, protoinfo);
4903 nlmsg_end(skb, nlh);
4904 return 0;
4905
4906 nla_put_failure:
4907 nlmsg_cancel(skb, nlh);
4908 return -EMSGSIZE;
4909 }
4910
4911 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4912 {
4913 struct net *net = sock_net(skb->sk);
4914 int h, s_h;
4915 int idx = 0, s_idx;
4916 struct net_device *dev;
4917 struct inet6_dev *idev;
4918 struct hlist_head *head;
4919
4920 s_h = cb->args[0];
4921 s_idx = cb->args[1];
4922
4923 rcu_read_lock();
4924 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4925 idx = 0;
4926 head = &net->dev_index_head[h];
4927 hlist_for_each_entry_rcu(dev, head, index_hlist) {
4928 if (idx < s_idx)
4929 goto cont;
4930 idev = __in6_dev_get(dev);
4931 if (!idev)
4932 goto cont;
4933 if (inet6_fill_ifinfo(skb, idev,
4934 NETLINK_CB(cb->skb).portid,
4935 cb->nlh->nlmsg_seq,
4936 RTM_NEWLINK, NLM_F_MULTI) < 0)
4937 goto out;
4938 cont:
4939 idx++;
4940 }
4941 }
4942 out:
4943 rcu_read_unlock();
4944 cb->args[1] = idx;
4945 cb->args[0] = h;
4946
4947 return skb->len;
4948 }
4949
4950 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4951 {
4952 struct sk_buff *skb;
4953 struct net *net = dev_net(idev->dev);
4954 int err = -ENOBUFS;
4955
4956 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4957 if (!skb)
4958 goto errout;
4959
4960 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
4961 if (err < 0) {
4962 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4963 WARN_ON(err == -EMSGSIZE);
4964 kfree_skb(skb);
4965 goto errout;
4966 }
4967 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
4968 return;
4969 errout:
4970 if (err < 0)
4971 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
4972 }
4973
4974 static inline size_t inet6_prefix_nlmsg_size(void)
4975 {
4976 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4977 + nla_total_size(sizeof(struct in6_addr))
4978 + nla_total_size(sizeof(struct prefix_cacheinfo));
4979 }
4980
4981 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4982 struct prefix_info *pinfo, u32 portid, u32 seq,
4983 int event, unsigned int flags)
4984 {
4985 struct prefixmsg *pmsg;
4986 struct nlmsghdr *nlh;
4987 struct prefix_cacheinfo ci;
4988
4989 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4990 if (!nlh)
4991 return -EMSGSIZE;
4992
4993 pmsg = nlmsg_data(nlh);
4994 pmsg->prefix_family = AF_INET6;
4995 pmsg->prefix_pad1 = 0;
4996 pmsg->prefix_pad2 = 0;
4997 pmsg->prefix_ifindex = idev->dev->ifindex;
4998 pmsg->prefix_len = pinfo->prefix_len;
4999 pmsg->prefix_type = pinfo->type;
5000 pmsg->prefix_pad3 = 0;
5001 pmsg->prefix_flags = 0;
5002 if (pinfo->onlink)
5003 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5004 if (pinfo->autoconf)
5005 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5006
5007 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5008 goto nla_put_failure;
5009 ci.preferred_time = ntohl(pinfo->prefered);
5010 ci.valid_time = ntohl(pinfo->valid);
5011 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5012 goto nla_put_failure;
5013 nlmsg_end(skb, nlh);
5014 return 0;
5015
5016 nla_put_failure:
5017 nlmsg_cancel(skb, nlh);
5018 return -EMSGSIZE;
5019 }
5020
5021 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
5022 struct prefix_info *pinfo)
5023 {
5024 struct sk_buff *skb;
5025 struct net *net = dev_net(idev->dev);
5026 int err = -ENOBUFS;
5027
5028 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
5029 if (!skb)
5030 goto errout;
5031
5032 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
5033 if (err < 0) {
5034 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5035 WARN_ON(err == -EMSGSIZE);
5036 kfree_skb(skb);
5037 goto errout;
5038 }
5039 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5040 return;
5041 errout:
5042 if (err < 0)
5043 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
5044 }
5045
5046 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5047 {
5048 struct net *net = dev_net(ifp->idev->dev);
5049
5050 if (event)
5051 ASSERT_RTNL();
5052
5053 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5054
5055 switch (event) {
5056 case RTM_NEWADDR:
5057 /*
5058 * If the address was optimistic
5059 * we inserted the route at the start of
5060 * our DAD process, so we don't need
5061 * to do it again
5062 */
5063 if (!(ifp->rt->rt6i_node))
5064 ip6_ins_rt(ifp->rt);
5065 if (ifp->idev->cnf.forwarding)
5066 addrconf_join_anycast(ifp);
5067 if (!ipv6_addr_any(&ifp->peer_addr))
5068 addrconf_prefix_route(&ifp->peer_addr, 128,
5069 ifp->idev->dev, 0, 0);
5070 break;
5071 case RTM_DELADDR:
5072 if (ifp->idev->cnf.forwarding)
5073 addrconf_leave_anycast(ifp);
5074 addrconf_leave_solict(ifp->idev, &ifp->addr);
5075 if (!ipv6_addr_any(&ifp->peer_addr)) {
5076 struct rt6_info *rt;
5077
5078 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5079 ifp->idev->dev, 0, 0);
5080 if (rt && ip6_del_rt(rt))
5081 dst_free(&rt->dst);
5082 }
5083 dst_hold(&ifp->rt->dst);
5084
5085 if (ip6_del_rt(ifp->rt))
5086 dst_free(&ifp->rt->dst);
5087
5088 rt_genid_bump_ipv6(net);
5089 break;
5090 }
5091 atomic_inc(&net->ipv6.dev_addr_genid);
5092 }
5093
5094 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5095 {
5096 rcu_read_lock_bh();
5097 if (likely(ifp->idev->dead == 0))
5098 __ipv6_ifa_notify(event, ifp);
5099 rcu_read_unlock_bh();
5100 }
5101
5102 #ifdef CONFIG_SYSCTL
5103
5104 static
5105 int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
5106 void __user *buffer, size_t *lenp, loff_t *ppos)
5107 {
5108 int *valp = ctl->data;
5109 int val = *valp;
5110 loff_t pos = *ppos;
5111 struct ctl_table lctl;
5112 int ret;
5113
5114 /*
5115 * ctl->data points to idev->cnf.forwarding, we should
5116 * not modify it until we get the rtnl lock.
5117 */
5118 lctl = *ctl;
5119 lctl.data = &val;
5120
5121 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5122
5123 if (write)
5124 ret = addrconf_fixup_forwarding(ctl, valp, val);
5125 if (ret)
5126 *ppos = pos;
5127 return ret;
5128 }
5129
5130 static
5131 int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5132 void __user *buffer, size_t *lenp, loff_t *ppos)
5133 {
5134 struct inet6_dev *idev = ctl->extra1;
5135 int min_mtu = IPV6_MIN_MTU;
5136 struct ctl_table lctl;
5137
5138 lctl = *ctl;
5139 lctl.extra1 = &min_mtu;
5140 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5141
5142 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5143 }
5144
5145 static void dev_disable_change(struct inet6_dev *idev)
5146 {
5147 struct netdev_notifier_info info;
5148
5149 if (!idev || !idev->dev)
5150 return;
5151
5152 netdev_notifier_info_init(&info, idev->dev);
5153 if (idev->cnf.disable_ipv6)
5154 addrconf_notify(NULL, NETDEV_DOWN, &info);
5155 else
5156 addrconf_notify(NULL, NETDEV_UP, &info);
5157 }
5158
5159 static void addrconf_disable_change(struct net *net, __s32 newf)
5160 {
5161 struct net_device *dev;
5162 struct inet6_dev *idev;
5163
5164 rcu_read_lock();
5165 for_each_netdev_rcu(net, dev) {
5166 idev = __in6_dev_get(dev);
5167 if (idev) {
5168 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5169 idev->cnf.disable_ipv6 = newf;
5170 if (changed)
5171 dev_disable_change(idev);
5172 }
5173 }
5174 rcu_read_unlock();
5175 }
5176
5177 static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
5178 {
5179 struct net *net;
5180 int old;
5181
5182 if (!rtnl_trylock())
5183 return restart_syscall();
5184
5185 net = (struct net *)table->extra2;
5186 old = *p;
5187 *p = newf;
5188
5189 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5190 rtnl_unlock();
5191 return 0;
5192 }
5193
5194 if (p == &net->ipv6.devconf_all->disable_ipv6) {
5195 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5196 addrconf_disable_change(net, newf);
5197 } else if ((!newf) ^ (!old))
5198 dev_disable_change((struct inet6_dev *)table->extra1);
5199
5200 rtnl_unlock();
5201 return 0;
5202 }
5203
5204 static
5205 int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
5206 void __user *buffer, size_t *lenp, loff_t *ppos)
5207 {
5208 int *valp = ctl->data;
5209 int val = *valp;
5210 loff_t pos = *ppos;
5211 struct ctl_table lctl;
5212 int ret;
5213
5214 /*
5215 * ctl->data points to idev->cnf.disable_ipv6, we should
5216 * not modify it until we get the rtnl lock.
5217 */
5218 lctl = *ctl;
5219 lctl.data = &val;
5220
5221 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5222
5223 if (write)
5224 ret = addrconf_disable_ipv6(ctl, valp, val);
5225 if (ret)
5226 *ppos = pos;
5227 return ret;
5228 }
5229
5230 static
5231 int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5232 void __user *buffer, size_t *lenp, loff_t *ppos)
5233 {
5234 int *valp = ctl->data;
5235 int ret;
5236 int old, new;
5237
5238 old = *valp;
5239 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5240 new = *valp;
5241
5242 if (write && old != new) {
5243 struct net *net = ctl->extra2;
5244
5245 if (!rtnl_trylock())
5246 return restart_syscall();
5247
5248 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5249 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5250 NETCONFA_IFINDEX_DEFAULT,
5251 net->ipv6.devconf_dflt);
5252 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5253 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5254 NETCONFA_IFINDEX_ALL,
5255 net->ipv6.devconf_all);
5256 else {
5257 struct inet6_dev *idev = ctl->extra1;
5258
5259 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5260 idev->dev->ifindex,
5261 &idev->cnf);
5262 }
5263 rtnl_unlock();
5264 }
5265
5266 return ret;
5267 }
5268
5269 static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5270 void __user *buffer, size_t *lenp,
5271 loff_t *ppos)
5272 {
5273 int err;
5274 struct in6_addr addr;
5275 char str[IPV6_MAX_STRLEN];
5276 struct ctl_table lctl = *ctl;
5277 struct net *net = ctl->extra2;
5278 struct ipv6_stable_secret *secret = ctl->data;
5279
5280 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5281 return -EIO;
5282
5283 lctl.maxlen = IPV6_MAX_STRLEN;
5284 lctl.data = str;
5285
5286 if (!rtnl_trylock())
5287 return restart_syscall();
5288
5289 if (!write && !secret->initialized) {
5290 err = -EIO;
5291 goto out;
5292 }
5293
5294 if (!write) {
5295 err = snprintf(str, sizeof(str), "%pI6",
5296 &secret->secret);
5297 if (err >= sizeof(str)) {
5298 err = -EIO;
5299 goto out;
5300 }
5301 }
5302
5303 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5304 if (err || !write)
5305 goto out;
5306
5307 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5308 err = -EIO;
5309 goto out;
5310 }
5311
5312 secret->initialized = true;
5313 secret->secret = addr;
5314
5315 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5316 struct net_device *dev;
5317
5318 for_each_netdev(net, dev) {
5319 struct inet6_dev *idev = __in6_dev_get(dev);
5320
5321 if (idev) {
5322 idev->addr_gen_mode =
5323 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5324 }
5325 }
5326 } else {
5327 struct inet6_dev *idev = ctl->extra1;
5328
5329 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5330 }
5331
5332 out:
5333 rtnl_unlock();
5334
5335 return err;
5336 }
5337
5338 static struct addrconf_sysctl_table
5339 {
5340 struct ctl_table_header *sysctl_header;
5341 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
5342 } addrconf_sysctl __read_mostly = {
5343 .sysctl_header = NULL,
5344 .addrconf_vars = {
5345 {
5346 .procname = "forwarding",
5347 .data = &ipv6_devconf.forwarding,
5348 .maxlen = sizeof(int),
5349 .mode = 0644,
5350 .proc_handler = addrconf_sysctl_forward,
5351 },
5352 {
5353 .procname = "hop_limit",
5354 .data = &ipv6_devconf.hop_limit,
5355 .maxlen = sizeof(int),
5356 .mode = 0644,
5357 .proc_handler = proc_dointvec,
5358 },
5359 {
5360 .procname = "mtu",
5361 .data = &ipv6_devconf.mtu6,
5362 .maxlen = sizeof(int),
5363 .mode = 0644,
5364 .proc_handler = addrconf_sysctl_mtu,
5365 },
5366 {
5367 .procname = "accept_ra",
5368 .data = &ipv6_devconf.accept_ra,
5369 .maxlen = sizeof(int),
5370 .mode = 0644,
5371 .proc_handler = proc_dointvec,
5372 },
5373 {
5374 .procname = "accept_redirects",
5375 .data = &ipv6_devconf.accept_redirects,
5376 .maxlen = sizeof(int),
5377 .mode = 0644,
5378 .proc_handler = proc_dointvec,
5379 },
5380 {
5381 .procname = "autoconf",
5382 .data = &ipv6_devconf.autoconf,
5383 .maxlen = sizeof(int),
5384 .mode = 0644,
5385 .proc_handler = proc_dointvec,
5386 },
5387 {
5388 .procname = "dad_transmits",
5389 .data = &ipv6_devconf.dad_transmits,
5390 .maxlen = sizeof(int),
5391 .mode = 0644,
5392 .proc_handler = proc_dointvec,
5393 },
5394 {
5395 .procname = "router_solicitations",
5396 .data = &ipv6_devconf.rtr_solicits,
5397 .maxlen = sizeof(int),
5398 .mode = 0644,
5399 .proc_handler = proc_dointvec,
5400 },
5401 {
5402 .procname = "router_solicitation_interval",
5403 .data = &ipv6_devconf.rtr_solicit_interval,
5404 .maxlen = sizeof(int),
5405 .mode = 0644,
5406 .proc_handler = proc_dointvec_jiffies,
5407 },
5408 {
5409 .procname = "router_solicitation_delay",
5410 .data = &ipv6_devconf.rtr_solicit_delay,
5411 .maxlen = sizeof(int),
5412 .mode = 0644,
5413 .proc_handler = proc_dointvec_jiffies,
5414 },
5415 {
5416 .procname = "force_mld_version",
5417 .data = &ipv6_devconf.force_mld_version,
5418 .maxlen = sizeof(int),
5419 .mode = 0644,
5420 .proc_handler = proc_dointvec,
5421 },
5422 {
5423 .procname = "mldv1_unsolicited_report_interval",
5424 .data =
5425 &ipv6_devconf.mldv1_unsolicited_report_interval,
5426 .maxlen = sizeof(int),
5427 .mode = 0644,
5428 .proc_handler = proc_dointvec_ms_jiffies,
5429 },
5430 {
5431 .procname = "mldv2_unsolicited_report_interval",
5432 .data =
5433 &ipv6_devconf.mldv2_unsolicited_report_interval,
5434 .maxlen = sizeof(int),
5435 .mode = 0644,
5436 .proc_handler = proc_dointvec_ms_jiffies,
5437 },
5438 {
5439 .procname = "use_tempaddr",
5440 .data = &ipv6_devconf.use_tempaddr,
5441 .maxlen = sizeof(int),
5442 .mode = 0644,
5443 .proc_handler = proc_dointvec,
5444 },
5445 {
5446 .procname = "temp_valid_lft",
5447 .data = &ipv6_devconf.temp_valid_lft,
5448 .maxlen = sizeof(int),
5449 .mode = 0644,
5450 .proc_handler = proc_dointvec,
5451 },
5452 {
5453 .procname = "temp_prefered_lft",
5454 .data = &ipv6_devconf.temp_prefered_lft,
5455 .maxlen = sizeof(int),
5456 .mode = 0644,
5457 .proc_handler = proc_dointvec,
5458 },
5459 {
5460 .procname = "regen_max_retry",
5461 .data = &ipv6_devconf.regen_max_retry,
5462 .maxlen = sizeof(int),
5463 .mode = 0644,
5464 .proc_handler = proc_dointvec,
5465 },
5466 {
5467 .procname = "max_desync_factor",
5468 .data = &ipv6_devconf.max_desync_factor,
5469 .maxlen = sizeof(int),
5470 .mode = 0644,
5471 .proc_handler = proc_dointvec,
5472 },
5473 {
5474 .procname = "max_addresses",
5475 .data = &ipv6_devconf.max_addresses,
5476 .maxlen = sizeof(int),
5477 .mode = 0644,
5478 .proc_handler = proc_dointvec,
5479 },
5480 {
5481 .procname = "accept_ra_defrtr",
5482 .data = &ipv6_devconf.accept_ra_defrtr,
5483 .maxlen = sizeof(int),
5484 .mode = 0644,
5485 .proc_handler = proc_dointvec,
5486 },
5487 {
5488 .procname = "accept_ra_pinfo",
5489 .data = &ipv6_devconf.accept_ra_pinfo,
5490 .maxlen = sizeof(int),
5491 .mode = 0644,
5492 .proc_handler = proc_dointvec,
5493 },
5494 #ifdef CONFIG_IPV6_ROUTER_PREF
5495 {
5496 .procname = "accept_ra_rtr_pref",
5497 .data = &ipv6_devconf.accept_ra_rtr_pref,
5498 .maxlen = sizeof(int),
5499 .mode = 0644,
5500 .proc_handler = proc_dointvec,
5501 },
5502 {
5503 .procname = "router_probe_interval",
5504 .data = &ipv6_devconf.rtr_probe_interval,
5505 .maxlen = sizeof(int),
5506 .mode = 0644,
5507 .proc_handler = proc_dointvec_jiffies,
5508 },
5509 #ifdef CONFIG_IPV6_ROUTE_INFO
5510 {
5511 .procname = "accept_ra_rt_info_max_plen",
5512 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5513 .maxlen = sizeof(int),
5514 .mode = 0644,
5515 .proc_handler = proc_dointvec,
5516 },
5517 #endif
5518 #endif
5519 {
5520 .procname = "proxy_ndp",
5521 .data = &ipv6_devconf.proxy_ndp,
5522 .maxlen = sizeof(int),
5523 .mode = 0644,
5524 .proc_handler = addrconf_sysctl_proxy_ndp,
5525 },
5526 {
5527 .procname = "accept_source_route",
5528 .data = &ipv6_devconf.accept_source_route,
5529 .maxlen = sizeof(int),
5530 .mode = 0644,
5531 .proc_handler = proc_dointvec,
5532 },
5533 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5534 {
5535 .procname = "optimistic_dad",
5536 .data = &ipv6_devconf.optimistic_dad,
5537 .maxlen = sizeof(int),
5538 .mode = 0644,
5539 .proc_handler = proc_dointvec,
5540
5541 },
5542 {
5543 .procname = "use_optimistic",
5544 .data = &ipv6_devconf.use_optimistic,
5545 .maxlen = sizeof(int),
5546 .mode = 0644,
5547 .proc_handler = proc_dointvec,
5548
5549 },
5550 #endif
5551 #ifdef CONFIG_IPV6_MROUTE
5552 {
5553 .procname = "mc_forwarding",
5554 .data = &ipv6_devconf.mc_forwarding,
5555 .maxlen = sizeof(int),
5556 .mode = 0444,
5557 .proc_handler = proc_dointvec,
5558 },
5559 #endif
5560 {
5561 .procname = "disable_ipv6",
5562 .data = &ipv6_devconf.disable_ipv6,
5563 .maxlen = sizeof(int),
5564 .mode = 0644,
5565 .proc_handler = addrconf_sysctl_disable,
5566 },
5567 {
5568 .procname = "accept_dad",
5569 .data = &ipv6_devconf.accept_dad,
5570 .maxlen = sizeof(int),
5571 .mode = 0644,
5572 .proc_handler = proc_dointvec,
5573 },
5574 {
5575 .procname = "force_tllao",
5576 .data = &ipv6_devconf.force_tllao,
5577 .maxlen = sizeof(int),
5578 .mode = 0644,
5579 .proc_handler = proc_dointvec
5580 },
5581 {
5582 .procname = "ndisc_notify",
5583 .data = &ipv6_devconf.ndisc_notify,
5584 .maxlen = sizeof(int),
5585 .mode = 0644,
5586 .proc_handler = proc_dointvec
5587 },
5588 {
5589 .procname = "suppress_frag_ndisc",
5590 .data = &ipv6_devconf.suppress_frag_ndisc,
5591 .maxlen = sizeof(int),
5592 .mode = 0644,
5593 .proc_handler = proc_dointvec
5594 },
5595 {
5596 .procname = "accept_ra_from_local",
5597 .data = &ipv6_devconf.accept_ra_from_local,
5598 .maxlen = sizeof(int),
5599 .mode = 0644,
5600 .proc_handler = proc_dointvec,
5601 },
5602 {
5603 .procname = "accept_ra_mtu",
5604 .data = &ipv6_devconf.accept_ra_mtu,
5605 .maxlen = sizeof(int),
5606 .mode = 0644,
5607 .proc_handler = proc_dointvec,
5608 },
5609 {
5610 .procname = "stable_secret",
5611 .data = &ipv6_devconf.stable_secret,
5612 .maxlen = IPV6_MAX_STRLEN,
5613 .mode = 0600,
5614 .proc_handler = addrconf_sysctl_stable_secret,
5615 },
5616 {
5617 .procname = "use_oif_addrs_only",
5618 .data = &ipv6_devconf.use_oif_addrs_only,
5619 .maxlen = sizeof(int),
5620 .mode = 0644,
5621 .proc_handler = proc_dointvec,
5622
5623 },
5624 {
5625 /* sentinel */
5626 }
5627 },
5628 };
5629
5630 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5631 struct inet6_dev *idev, struct ipv6_devconf *p)
5632 {
5633 int i;
5634 struct addrconf_sysctl_table *t;
5635 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5636
5637 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5638 if (!t)
5639 goto out;
5640
5641 for (i = 0; t->addrconf_vars[i].data; i++) {
5642 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
5643 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
5644 t->addrconf_vars[i].extra2 = net;
5645 }
5646
5647 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5648
5649 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5650 if (!t->sysctl_header)
5651 goto free;
5652
5653 p->sysctl = t;
5654 return 0;
5655
5656 free:
5657 kfree(t);
5658 out:
5659 return -ENOBUFS;
5660 }
5661
5662 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5663 {
5664 struct addrconf_sysctl_table *t;
5665
5666 if (!p->sysctl)
5667 return;
5668
5669 t = p->sysctl;
5670 p->sysctl = NULL;
5671 unregister_net_sysctl_table(t->sysctl_header);
5672 kfree(t);
5673 }
5674
5675 static int addrconf_sysctl_register(struct inet6_dev *idev)
5676 {
5677 int err;
5678
5679 if (!sysctl_dev_name_is_allowed(idev->dev->name))
5680 return -EINVAL;
5681
5682 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5683 &ndisc_ifinfo_sysctl_change);
5684 if (err)
5685 return err;
5686 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5687 idev, &idev->cnf);
5688 if (err)
5689 neigh_sysctl_unregister(idev->nd_parms);
5690
5691 return err;
5692 }
5693
5694 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5695 {
5696 __addrconf_sysctl_unregister(&idev->cnf);
5697 neigh_sysctl_unregister(idev->nd_parms);
5698 }
5699
5700
5701 #endif
5702
5703 static int __net_init addrconf_init_net(struct net *net)
5704 {
5705 int err = -ENOMEM;
5706 struct ipv6_devconf *all, *dflt;
5707
5708 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5709 if (!all)
5710 goto err_alloc_all;
5711
5712 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5713 if (!dflt)
5714 goto err_alloc_dflt;
5715
5716 /* these will be inherited by all namespaces */
5717 dflt->autoconf = ipv6_defaults.autoconf;
5718 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5719
5720 dflt->stable_secret.initialized = false;
5721 all->stable_secret.initialized = false;
5722
5723 net->ipv6.devconf_all = all;
5724 net->ipv6.devconf_dflt = dflt;
5725
5726 #ifdef CONFIG_SYSCTL
5727 err = __addrconf_sysctl_register(net, "all", NULL, all);
5728 if (err < 0)
5729 goto err_reg_all;
5730
5731 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5732 if (err < 0)
5733 goto err_reg_dflt;
5734 #endif
5735 return 0;
5736
5737 #ifdef CONFIG_SYSCTL
5738 err_reg_dflt:
5739 __addrconf_sysctl_unregister(all);
5740 err_reg_all:
5741 kfree(dflt);
5742 #endif
5743 err_alloc_dflt:
5744 kfree(all);
5745 err_alloc_all:
5746 return err;
5747 }
5748
5749 static void __net_exit addrconf_exit_net(struct net *net)
5750 {
5751 #ifdef CONFIG_SYSCTL
5752 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5753 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5754 #endif
5755 kfree(net->ipv6.devconf_dflt);
5756 kfree(net->ipv6.devconf_all);
5757 }
5758
5759 static struct pernet_operations addrconf_ops = {
5760 .init = addrconf_init_net,
5761 .exit = addrconf_exit_net,
5762 };
5763
5764 static struct rtnl_af_ops inet6_ops __read_mostly = {
5765 .family = AF_INET6,
5766 .fill_link_af = inet6_fill_link_af,
5767 .get_link_af_size = inet6_get_link_af_size,
5768 .validate_link_af = inet6_validate_link_af,
5769 .set_link_af = inet6_set_link_af,
5770 };
5771
5772 /*
5773 * Init / cleanup code
5774 */
5775
5776 int __init addrconf_init(void)
5777 {
5778 struct inet6_dev *idev;
5779 int i, err;
5780
5781 err = ipv6_addr_label_init();
5782 if (err < 0) {
5783 pr_crit("%s: cannot initialize default policy table: %d\n",
5784 __func__, err);
5785 goto out;
5786 }
5787
5788 err = register_pernet_subsys(&addrconf_ops);
5789 if (err < 0)
5790 goto out_addrlabel;
5791
5792 addrconf_wq = create_workqueue("ipv6_addrconf");
5793 if (!addrconf_wq) {
5794 err = -ENOMEM;
5795 goto out_nowq;
5796 }
5797
5798 /* The addrconf netdev notifier requires that loopback_dev
5799 * has it's ipv6 private information allocated and setup
5800 * before it can bring up and give link-local addresses
5801 * to other devices which are up.
5802 *
5803 * Unfortunately, loopback_dev is not necessarily the first
5804 * entry in the global dev_base list of net devices. In fact,
5805 * it is likely to be the very last entry on that list.
5806 * So this causes the notifier registry below to try and
5807 * give link-local addresses to all devices besides loopback_dev
5808 * first, then loopback_dev, which cases all the non-loopback_dev
5809 * devices to fail to get a link-local address.
5810 *
5811 * So, as a temporary fix, allocate the ipv6 structure for
5812 * loopback_dev first by hand.
5813 * Longer term, all of the dependencies ipv6 has upon the loopback
5814 * device and it being up should be removed.
5815 */
5816 rtnl_lock();
5817 idev = ipv6_add_dev(init_net.loopback_dev);
5818 rtnl_unlock();
5819 if (IS_ERR(idev)) {
5820 err = PTR_ERR(idev);
5821 goto errlo;
5822 }
5823
5824 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5825 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5826
5827 register_netdevice_notifier(&ipv6_dev_notf);
5828
5829 addrconf_verify();
5830
5831 rtnl_af_register(&inet6_ops);
5832
5833 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5834 NULL);
5835 if (err < 0)
5836 goto errout;
5837
5838 /* Only the first call to __rtnl_register can fail */
5839 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5840 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5841 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5842 inet6_dump_ifaddr, NULL);
5843 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5844 inet6_dump_ifmcaddr, NULL);
5845 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5846 inet6_dump_ifacaddr, NULL);
5847 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
5848 inet6_netconf_dump_devconf, NULL);
5849
5850 ipv6_addr_label_rtnl_register();
5851
5852 return 0;
5853 errout:
5854 rtnl_af_unregister(&inet6_ops);
5855 unregister_netdevice_notifier(&ipv6_dev_notf);
5856 errlo:
5857 destroy_workqueue(addrconf_wq);
5858 out_nowq:
5859 unregister_pernet_subsys(&addrconf_ops);
5860 out_addrlabel:
5861 ipv6_addr_label_cleanup();
5862 out:
5863 return err;
5864 }
5865
5866 void addrconf_cleanup(void)
5867 {
5868 struct net_device *dev;
5869 int i;
5870
5871 unregister_netdevice_notifier(&ipv6_dev_notf);
5872 unregister_pernet_subsys(&addrconf_ops);
5873 ipv6_addr_label_cleanup();
5874
5875 rtnl_lock();
5876
5877 __rtnl_af_unregister(&inet6_ops);
5878
5879 /* clean dev list */
5880 for_each_netdev(&init_net, dev) {
5881 if (__in6_dev_get(dev) == NULL)
5882 continue;
5883 addrconf_ifdown(dev, 1);
5884 }
5885 addrconf_ifdown(init_net.loopback_dev, 2);
5886
5887 /*
5888 * Check hash table.
5889 */
5890 spin_lock_bh(&addrconf_hash_lock);
5891 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5892 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
5893 spin_unlock_bh(&addrconf_hash_lock);
5894 cancel_delayed_work(&addr_chk_work);
5895 rtnl_unlock();
5896
5897 destroy_workqueue(addrconf_wq);
5898 }