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