xref: /linux/drivers/net/wireguard/allowedips.h (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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 	u8 cidr, bit_at_a, bit_at_b, bitlen;
19 	u8 bits[16] __aligned(__alignof(u64));
20 
21 	/* Keep rarely used members at bottom to be beyond cache line. */
22 	unsigned long parent_bit_packed;
23 	union {
24 		struct list_head peer_list;
25 		struct rcu_head rcu;
26 	};
27 };
28 
29 struct allowedips {
30 	struct allowedips_node __rcu *root4;
31 	struct allowedips_node __rcu *root6;
32 	u64 seq;
33 } __aligned(4); /* We pack the lower 2 bits of &root, but m68k only gives 16-bit alignment. */
34 
35 void wg_allowedips_init(struct allowedips *table);
36 void wg_allowedips_free(struct allowedips *table, struct mutex *mutex);
37 int wg_allowedips_insert_v4(struct allowedips *table, const struct in_addr *ip,
38 			    u8 cidr, struct wg_peer *peer, struct mutex *lock);
39 int wg_allowedips_insert_v6(struct allowedips *table, const struct in6_addr *ip,
40 			    u8 cidr, struct wg_peer *peer, struct mutex *lock);
41 void wg_allowedips_remove_by_peer(struct allowedips *table,
42 				  struct wg_peer *peer, struct mutex *lock);
43 /* The ip input pointer should be __aligned(__alignof(u64))) */
44 int wg_allowedips_read_node(struct allowedips_node *node, u8 ip[16], u8 *cidr);
45 
46 /* These return a strong reference to a peer: */
47 struct wg_peer *wg_allowedips_lookup_dst(struct allowedips *table,
48 					 struct sk_buff *skb);
49 struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table,
50 					 struct sk_buff *skb);
51 
52 #ifdef DEBUG
53 bool wg_allowedips_selftest(void);
54 #endif
55 
56 int wg_allowedips_slab_init(void);
57 void wg_allowedips_slab_uninit(void);
58 
59 #endif /* _WG_ALLOWEDIPS_H */
60