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