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