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