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