blob: 7698637cf827648988d93a2b529810c5ee08109e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Anycast support for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
6 * David L Stevens (dlstevens@us.ibm.com)
7 *
8 * based heavily on net/ipv6/mcast.c
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/errno.h>
19#include <linux/types.h>
20#include <linux/random.h>
21#include <linux/string.h>
22#include <linux/socket.h>
23#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/net.h>
25#include <linux/in6.h>
26#include <linux/netdevice.h>
27#include <linux/if_arp.h>
28#include <linux/route.h>
29#include <linux/init.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020034#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/sock.h>
36#include <net/snmp.h>
37
38#include <net/ipv6.h>
39#include <net/protocol.h>
40#include <net/if_inet6.h>
41#include <net/ndisc.h>
42#include <net/addrconf.h>
43#include <net/ip6_route.h>
44
45#include <net/checksum.h>
46
Jeff Barnhill2384d022018-11-02 20:23:57 +000047#define IN6_ADDR_HSIZE_SHIFT 8
48#define IN6_ADDR_HSIZE BIT(IN6_ADDR_HSIZE_SHIFT)
49/* anycast address hash table
50 */
51static struct hlist_head inet6_acaddr_lst[IN6_ADDR_HSIZE];
52static DEFINE_SPINLOCK(acaddr_hash_lock);
53
Eric Dumazetb71d1d42011-04-22 04:53:02 +000054static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Barnhill2384d022018-11-02 20:23:57 +000056static u32 inet6_acaddr_hash(struct net *net, const struct in6_addr *addr)
57{
58 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
59
60 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
61}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * socket join an anycast group
65 */
66
Eric Dumazetb71d1d42011-04-22 04:53:02 +000067int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct ipv6_pinfo *np = inet6_sk(sk);
70 struct net_device *dev = NULL;
71 struct inet6_dev *idev;
72 struct ipv6_ac_socklist *pac;
Daniel Lezcano6ab57e72008-03-26 16:52:32 -070073 struct net *net = sock_net(sk);
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -070074 int ishost = !net->ipv6.devconf_all->forwarding;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int err = 0;
76
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -030077 ASSERT_RTNL();
78
Eric W. Biedermanaf31f412012-11-16 03:03:06 +000079 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EPERM;
81 if (ipv6_addr_is_multicast(addr))
82 return -EINVAL;
David Ahern232378e2018-03-13 08:29:37 -070083
84 if (ifindex)
85 dev = __dev_get_by_index(net, ifindex);
86
87 if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return -EINVAL;
89
90 pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +010091 if (!pac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -ENOMEM;
93 pac->acl_next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +000094 pac->acl_addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (ifindex == 0) {
97 struct rt6_info *rt;
98
David Ahernb75cc8f2018-03-02 08:32:17 -080099 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500101 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000102 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } else if (ishost) {
104 err = -EADDRNOTAVAIL;
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000105 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 } else {
107 /* router, no matching interface: just pick one */
WANG Cong6c555492014-09-11 15:35:09 -0700108 dev = __dev_get_by_flags(net, IFF_UP,
109 IFF_UP | IFF_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
David Ahern232378e2018-03-13 08:29:37 -0700111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ian Morris63159f22015-03-29 14:00:04 +0100113 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 err = -ENODEV;
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000115 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000118 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!idev) {
120 if (ifindex)
121 err = -ENODEV;
122 else
123 err = -EADDRNOTAVAIL;
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000124 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 /* reset ishost, now that we have a specific device */
127 ishost = !idev->cnf.forwarding;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 pac->acl_ifindex = dev->ifindex;
130
131 /* XXX
132 * For hosts, allow link-local or matching prefix anycasts.
133 * This obviates the need for propagating anycast routes while
134 * still allowing some non-router anycast participation.
135 */
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -0400136 if (!ipv6_chk_prefix(addr, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (ishost)
138 err = -EADDRNOTAVAIL;
139 if (err)
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000140 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
WANG Cong013b4d92014-09-11 15:35:11 -0700143 err = __ipv6_dev_ac_inc(idev, addr);
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000144 if (!err) {
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000145 pac->acl_next = np->ipv6_ac_list;
146 np->ipv6_ac_list = pac;
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000147 pac = NULL;
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000150error:
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000151 if (pac)
152 sock_kfree_s(sk, pac, sizeof(*pac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return err;
154}
155
156/*
157 * socket leave an anycast group
158 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000159int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct ipv6_pinfo *np = inet6_sk(sk);
162 struct net_device *dev;
163 struct ipv6_ac_socklist *pac, *prev_pac;
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700164 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300166 ASSERT_RTNL();
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 prev_pac = NULL;
169 for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
170 if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
171 ipv6_addr_equal(&pac->acl_addr, addr))
172 break;
173 prev_pac = pac;
174 }
Marcelo Ricardo Leitnerc4a68532015-03-20 11:37:17 -0300175 if (!pac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (prev_pac)
178 prev_pac->acl_next = pac->acl_next;
179 else
180 np->ipv6_ac_list = pac->acl_next;
181
WANG Cong6c555492014-09-11 15:35:09 -0700182 dev = __dev_get_by_index(net, pac->acl_ifindex);
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000183 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ipv6_dev_ac_dec(dev, &pac->acl_addr);
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sock_kfree_s(sk, pac, sizeof(*pac));
187 return 0;
188}
189
190void ipv6_sock_ac_close(struct sock *sk)
191{
192 struct ipv6_pinfo *np = inet6_sk(sk);
193 struct net_device *dev = NULL;
194 struct ipv6_ac_socklist *pac;
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700195 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int prev_index;
197
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000198 if (!np->ipv6_ac_list)
199 return;
200
WANG Congb03a9c02014-09-11 15:35:10 -0700201 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 pac = np->ipv6_ac_list;
203 np->ipv6_ac_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 prev_index = 0;
206 while (pac) {
207 struct ipv6_ac_socklist *next = pac->acl_next;
208
209 if (pac->acl_ifindex != prev_index) {
WANG Cong6c555492014-09-11 15:35:09 -0700210 dev = __dev_get_by_index(net, pac->acl_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 prev_index = pac->acl_ifindex;
212 }
213 if (dev)
214 ipv6_dev_ac_dec(dev, &pac->acl_addr);
215 sock_kfree_s(sk, pac, sizeof(*pac));
216 pac = next;
217 }
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200218 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Jeff Barnhill2384d022018-11-02 20:23:57 +0000221static void ipv6_add_acaddr_hash(struct net *net, struct ifacaddr6 *aca)
222{
223 unsigned int hash = inet6_acaddr_hash(net, &aca->aca_addr);
224
225 spin_lock(&acaddr_hash_lock);
226 hlist_add_head_rcu(&aca->aca_addr_lst, &inet6_acaddr_lst[hash]);
227 spin_unlock(&acaddr_hash_lock);
228}
229
230static void ipv6_del_acaddr_hash(struct ifacaddr6 *aca)
231{
232 spin_lock(&acaddr_hash_lock);
233 hlist_del_init_rcu(&aca->aca_addr_lst);
234 spin_unlock(&acaddr_hash_lock);
235}
236
WANG Cong83aa29e2014-09-11 15:35:12 -0700237static void aca_get(struct ifacaddr6 *aca)
238{
Reshetova, Elenaaffa78b2017-07-04 09:34:58 +0300239 refcount_inc(&aca->aca_refcnt);
WANG Cong83aa29e2014-09-11 15:35:12 -0700240}
241
Jeff Barnhill2384d022018-11-02 20:23:57 +0000242static void aca_free_rcu(struct rcu_head *h)
243{
244 struct ifacaddr6 *aca = container_of(h, struct ifacaddr6, rcu);
245
246 fib6_info_release(aca->aca_rt);
247 kfree(aca);
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static void aca_put(struct ifacaddr6 *ac)
251{
Reshetova, Elenaaffa78b2017-07-04 09:34:58 +0300252 if (refcount_dec_and_test(&ac->aca_refcnt)) {
Jeff Barnhill2384d022018-11-02 20:23:57 +0000253 call_rcu(&ac->rcu, aca_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255}
256
David Ahern93c2fb22018-04-18 15:38:59 -0700257static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
WANG Cong83aa29e2014-09-11 15:35:12 -0700258 const struct in6_addr *addr)
259{
WANG Cong83aa29e2014-09-11 15:35:12 -0700260 struct ifacaddr6 *aca;
261
262 aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100263 if (!aca)
WANG Cong83aa29e2014-09-11 15:35:12 -0700264 return NULL;
265
266 aca->aca_addr = *addr;
David Ahern93c2fb22018-04-18 15:38:59 -0700267 fib6_info_hold(f6i);
268 aca->aca_rt = f6i;
Jeff Barnhill2384d022018-11-02 20:23:57 +0000269 INIT_HLIST_NODE(&aca->aca_addr_lst);
WANG Cong83aa29e2014-09-11 15:35:12 -0700270 aca->aca_users = 1;
271 /* aca_tstamp should be updated upon changes */
272 aca->aca_cstamp = aca->aca_tstamp = jiffies;
Reshetova, Elenaaffa78b2017-07-04 09:34:58 +0300273 refcount_set(&aca->aca_refcnt, 1);
WANG Cong83aa29e2014-09-11 15:35:12 -0700274
275 return aca;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * device anycast group inc (add if not found)
280 */
WANG Cong013b4d92014-09-11 15:35:11 -0700281int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct ifacaddr6 *aca;
David Ahern360a9882018-04-18 15:39:00 -0700284 struct fib6_info *f6i;
David Ahernafb1d4b52018-04-17 17:33:11 -0700285 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int err;
287
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200288 ASSERT_RTNL();
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 write_lock_bh(&idev->lock);
291 if (idev->dead) {
292 err = -ENODEV;
293 goto out;
294 }
295
296 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
297 if (ipv6_addr_equal(&aca->aca_addr, addr)) {
298 aca->aca_users++;
299 err = 0;
300 goto out;
301 }
302 }
303
David Ahernafb1d4b52018-04-17 17:33:11 -0700304 net = dev_net(idev->dev);
David Ahern360a9882018-04-18 15:39:00 -0700305 f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC);
306 if (IS_ERR(f6i)) {
307 err = PTR_ERR(f6i);
WANG Cong83aa29e2014-09-11 15:35:12 -0700308 goto out;
309 }
David Ahern360a9882018-04-18 15:39:00 -0700310 aca = aca_alloc(f6i, addr);
Ian Morris63159f22015-03-29 14:00:04 +0100311 if (!aca) {
David Ahern360a9882018-04-18 15:39:00 -0700312 fib6_info_release(f6i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 err = -ENOMEM;
314 goto out;
315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 aca->aca_next = idev->ac_list;
318 idev->ac_list = aca;
WANG Cong83aa29e2014-09-11 15:35:12 -0700319
320 /* Hold this for addrconf_join_solict() below before we unlock,
321 * it is already exposed via idev->ac_list.
322 */
323 aca_get(aca);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 write_unlock_bh(&idev->lock);
325
Jeff Barnhill2384d022018-11-02 20:23:57 +0000326 ipv6_add_acaddr_hash(net, aca);
327
David Ahern360a9882018-04-18 15:39:00 -0700328 ip6_ins_rt(net, f6i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
WANG Cong013b4d92014-09-11 15:35:11 -0700330 addrconf_join_solict(idev->dev, &aca->aca_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 aca_put(aca);
333 return 0;
334out:
335 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return err;
337}
338
339/*
340 * device anycast group decrement
341 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000342int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct ifacaddr6 *aca, *prev_aca;
345
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200346 ASSERT_RTNL();
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 write_lock_bh(&idev->lock);
349 prev_aca = NULL;
350 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
351 if (ipv6_addr_equal(&aca->aca_addr, addr))
352 break;
353 prev_aca = aca;
354 }
355 if (!aca) {
356 write_unlock_bh(&idev->lock);
357 return -ENOENT;
358 }
359 if (--aca->aca_users > 0) {
360 write_unlock_bh(&idev->lock);
361 return 0;
362 }
363 if (prev_aca)
364 prev_aca->aca_next = aca->aca_next;
365 else
366 idev->ac_list = aca->aca_next;
367 write_unlock_bh(&idev->lock);
Jeff Barnhill2384d022018-11-02 20:23:57 +0000368 ipv6_del_acaddr_hash(aca);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 addrconf_leave_solict(idev, &aca->aca_addr);
370
David Ahernafb1d4b52018-04-17 17:33:11 -0700371 ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 aca_put(aca);
374 return 0;
375}
376
WANG Cong6c555492014-09-11 15:35:09 -0700377/* called with rtnl_lock() */
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000378static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000380 struct inet6_dev *idev = __in6_dev_get(dev);
381
Ian Morris63159f22015-03-29 14:00:04 +0100382 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -ENODEV;
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000384 return __ipv6_dev_ac_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900386
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +0200387void ipv6_ac_destroy_dev(struct inet6_dev *idev)
388{
389 struct ifacaddr6 *aca;
390
391 write_lock_bh(&idev->lock);
392 while ((aca = idev->ac_list) != NULL) {
393 idev->ac_list = aca->aca_next;
394 write_unlock_bh(&idev->lock);
395
Jeff Barnhill2384d022018-11-02 20:23:57 +0000396 ipv6_del_acaddr_hash(aca);
397
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +0200398 addrconf_leave_solict(idev, &aca->aca_addr);
399
David Ahernafb1d4b52018-04-17 17:33:11 -0700400 ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +0200401
402 aca_put(aca);
403
404 write_lock_bh(&idev->lock);
405 }
406 write_unlock_bh(&idev->lock);
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
410 * check if the interface has this anycast address
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000411 * called with rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000413static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct inet6_dev *idev;
416 struct ifacaddr6 *aca;
417
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000418 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (idev) {
420 read_lock_bh(&idev->lock);
421 for (aca = idev->ac_list; aca; aca = aca->aca_next)
422 if (ipv6_addr_equal(&aca->aca_addr, addr))
423 break;
424 read_unlock_bh(&idev->lock);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700425 return aca != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Eric Dumazeta50feda2012-05-18 18:57:34 +0000427 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/*
431 * check if given interface (or any, if dev==0) has this anycast address
432 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000433bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
434 const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jeff Barnhill2384d022018-11-02 20:23:57 +0000436 unsigned int hash = inet6_acaddr_hash(net, addr);
437 struct net_device *nh_dev;
438 struct ifacaddr6 *aca;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000439 bool found = false;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700440
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800441 rcu_read_lock();
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000442 if (dev)
443 found = ipv6_chk_acast_dev(dev, addr);
444 else
Jeff Barnhill2384d022018-11-02 20:23:57 +0000445 hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash],
446 aca_addr_lst) {
447 nh_dev = fib6_info_nh_dev(aca->aca_rt);
448 if (!nh_dev || !net_eq(dev_net(nh_dev), net))
449 continue;
450 if (ipv6_addr_equal(&aca->aca_addr, addr)) {
Eric Dumazeta50feda2012-05-18 18:57:34 +0000451 found = true;
Eric Dumazetbb69ae02010-06-07 11:42:13 +0000452 break;
453 }
Jeff Barnhill2384d022018-11-02 20:23:57 +0000454 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -0800455 rcu_read_unlock();
Pavel Emelianov7562f872007-05-03 15:13:45 -0700456 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
FX Le Bail7c90cc22014-01-22 07:42:37 +0100459/* check if this anycast address is link-local on given interface or
460 * is global
461 */
462bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
463 const struct in6_addr *addr)
464{
465 return ipv6_chk_acast_addr(net,
466 (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
467 dev : NULL),
468 addr);
469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471#ifdef CONFIG_PROC_FS
472struct ac6_iter_state {
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700473 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct net_device *dev;
475 struct inet6_dev *idev;
476};
477
478#define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
479
480static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
481{
482 struct ifacaddr6 *im = NULL;
483 struct ac6_iter_state *state = ac6_seq_private(seq);
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700484 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Pavel Emelianov7562f872007-05-03 15:13:45 -0700486 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +0000487 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +0000489 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!idev)
491 continue;
492 read_lock_bh(&idev->lock);
493 im = idev->ac_list;
494 if (im) {
495 state->idev = idev;
496 break;
497 }
498 read_unlock_bh(&idev->lock);
499 }
500 return im;
501}
502
503static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
504{
505 struct ac6_iter_state *state = ac6_seq_private(seq);
506
507 im = im->aca_next;
508 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +0000509 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +0000511
512 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!state->dev) {
514 state->idev = NULL;
515 break;
516 }
Eric Dumazetce81b762009-11-11 17:34:30 +0000517 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!state->idev)
519 continue;
520 read_lock_bh(&state->idev->lock);
521 im = state->idev->ac_list;
522 }
523 return im;
524}
525
526static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
527{
528 struct ifacaddr6 *im = ac6_get_first(seq);
529 if (im)
530 while (pos && (im = ac6_get_next(seq, im)) != NULL)
531 --pos;
532 return pos ? NULL : im;
533}
534
535static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +0000536 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Eric Dumazetce81b762009-11-11 17:34:30 +0000538 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return ac6_get_idx(seq, *pos);
540}
541
542static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
543{
Eric Dumazetce81b762009-11-11 17:34:30 +0000544 struct ifacaddr6 *im = ac6_get_next(seq, v);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ++*pos;
547 return im;
548}
549
550static void ac6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +0000551 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct ac6_iter_state *state = ac6_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +0000554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (likely(state->idev != NULL)) {
556 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +0000557 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Eric Dumazetce81b762009-11-11 17:34:30 +0000559 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562static int ac6_seq_show(struct seq_file *seq, void *v)
563{
564 struct ifacaddr6 *im = (struct ifacaddr6 *)v;
565 struct ac6_iter_state *state = ac6_seq_private(seq);
566
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700567 seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -0700569 &im->aca_addr, im->aca_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700573static const struct seq_operations ac6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .start = ac6_seq_start,
575 .next = ac6_seq_next,
576 .stop = ac6_seq_stop,
577 .show = ac6_seq_show,
578};
579
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000580int __net_init ac6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Christoph Hellwigc3506372018-04-10 19:42:55 +0200582 if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
583 sizeof(struct ac6_iter_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -ENOMEM;
585
586 return 0;
587}
588
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700589void ac6_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Gao fengece31ff2013-02-18 01:34:56 +0000591 remove_proc_entry("anycast6", net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
Jeff Barnhill2384d022018-11-02 20:23:57 +0000593
594/* Init / cleanup code
595 */
596int __init ipv6_anycast_init(void)
597{
598 int i;
599
600 for (i = 0; i < IN6_ADDR_HSIZE; i++)
601 INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
602 return 0;
603}
604
605void ipv6_anycast_cleanup(void)
606{
607 int i;
608
609 spin_lock(&acaddr_hash_lock);
610 for (i = 0; i < IN6_ADDR_HSIZE; i++)
611 WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
612 spin_unlock(&acaddr_hash_lock);
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif