xref: /linux/drivers/net/wireguard/peerlookup.h (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
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_PEERLOOKUP_H
7 #define _WG_PEERLOOKUP_H
8 
9 #include "messages.h"
10 
11 #include <linux/hashtable.h>
12 #include <linux/mutex.h>
13 #include <linux/siphash.h>
14 
15 struct wg_peer;
16 
17 struct pubkey_hashtable {
18 	/* TODO: move to rhashtable */
19 	DECLARE_HASHTABLE(hashtable, 11);
20 	siphash_key_t key;
21 	struct mutex lock;
22 };
23 
24 struct pubkey_hashtable *wg_pubkey_hashtable_alloc(void);
25 void wg_pubkey_hashtable_add(struct pubkey_hashtable *table,
26 			     struct wg_peer *peer);
27 void wg_pubkey_hashtable_remove(struct pubkey_hashtable *table,
28 				struct wg_peer *peer);
29 struct wg_peer *
30 wg_pubkey_hashtable_lookup(struct pubkey_hashtable *table,
31 			   const u8 pubkey[NOISE_PUBLIC_KEY_LEN]);
32 
33 struct index_hashtable {
34 	/* TODO: move to rhashtable */
35 	DECLARE_HASHTABLE(hashtable, 13);
36 	spinlock_t lock;
37 };
38 
39 enum index_hashtable_type {
40 	INDEX_HASHTABLE_HANDSHAKE = 1U << 0,
41 	INDEX_HASHTABLE_KEYPAIR = 1U << 1
42 };
43 
44 struct index_hashtable_entry {
45 	struct wg_peer *peer;
46 	struct hlist_node index_hash;
47 	enum index_hashtable_type type;
48 	__le32 index;
49 };
50 
51 struct index_hashtable *wg_index_hashtable_alloc(void);
52 __le32 wg_index_hashtable_insert(struct index_hashtable *table,
53 				 struct index_hashtable_entry *entry);
54 bool wg_index_hashtable_replace(struct index_hashtable *table,
55 				struct index_hashtable_entry *old,
56 				struct index_hashtable_entry *new);
57 void wg_index_hashtable_remove(struct index_hashtable *table,
58 			       struct index_hashtable_entry *entry);
59 struct index_hashtable_entry *
60 wg_index_hashtable_lookup(struct index_hashtable *table,
61 			  const enum index_hashtable_type type_mask,
62 			  const __le32 index, struct wg_peer **peer);
63 
64 #endif /* _WG_PEERLOOKUP_H */
65