xref: /linux/drivers/net/wireguard/allowedips.h (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 
6 #ifndef _WG_ALLOWEDIPS_H
7 #define _WG_ALLOWEDIPS_H
8 
9 #include <linux/mutex.h>
10 #include <linux/ip.h>
11 #include <linux/ipv6.h>
12 
13 struct wg_peer;
14 
15 struct allowedips_node {
16 	struct wg_peer __rcu *peer;
17 	struct allowedips_node __rcu *bit[2];
18 	/* While it may seem scandalous that we waste space for v4,
19 	 * we're alloc'ing to the nearest power of 2 anyway, so this
20 	 * doesn't actually make a difference.
21 	 */
22 	u8 bits[16] __aligned(__alignof(u64));
23 	u8 cidr, bit_at_a, bit_at_b, bitlen;
24 
25 	/* Keep rarely used list at bottom to be beyond cache line. */
26 	union {
27 		struct list_head peer_list;
28 		struct rcu_head rcu;
29 	};
30 };
31 
32 struct allowedips {
33 	struct allowedips_node __rcu *root4;
34 	struct allowedips_node __rcu *root6;
35 	u64 seq;
36 };
37 
38 void wg_allowedips_init(struct allowedips *table);
39 void wg_allowedips_free(struct allowedips *table, struct mutex *mutex);
40 int wg_allowedips_insert_v4(struct allowedips *table, const struct in_addr *ip,
41 			    u8 cidr, struct wg_peer *peer, struct mutex *lock);
42 int wg_allowedips_insert_v6(struct allowedips *table, const struct in6_addr *ip,
43 			    u8 cidr, struct wg_peer *peer, struct mutex *lock);
44 void wg_allowedips_remove_by_peer(struct allowedips *table,
45 				  struct wg_peer *peer, struct mutex *lock);
46 /* The ip input pointer should be __aligned(__alignof(u64))) */
47 int wg_allowedips_read_node(struct allowedips_node *node, u8 ip[16], u8 *cidr);
48 
49 /* These return a strong reference to a peer: */
50 struct wg_peer *wg_allowedips_lookup_dst(struct allowedips *table,
51 					 struct sk_buff *skb);
52 struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table,
53 					 struct sk_buff *skb);
54 
55 #ifdef DEBUG
56 bool wg_allowedips_selftest(void);
57 #endif
58 
59 #endif /* _WG_ALLOWEDIPS_H */
60