^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef _WG_PEERLOOKUP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _WG_PEERLOOKUP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "messages.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/siphash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct wg_peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct pubkey_hashtable {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* TODO: move to rhashtable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) DECLARE_HASHTABLE(hashtable, 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) siphash_key_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct pubkey_hashtable *wg_pubkey_hashtable_alloc(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void wg_pubkey_hashtable_add(struct pubkey_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct wg_peer *peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void wg_pubkey_hashtable_remove(struct pubkey_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct wg_peer *peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct wg_peer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) wg_pubkey_hashtable_lookup(struct pubkey_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) const u8 pubkey[NOISE_PUBLIC_KEY_LEN]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct index_hashtable {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* TODO: move to rhashtable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) DECLARE_HASHTABLE(hashtable, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) enum index_hashtable_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) INDEX_HASHTABLE_HANDSHAKE = 1U << 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) INDEX_HASHTABLE_KEYPAIR = 1U << 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct index_hashtable_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct wg_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct hlist_node index_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) enum index_hashtable_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __le32 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct index_hashtable *wg_index_hashtable_alloc(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) __le32 wg_index_hashtable_insert(struct index_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct index_hashtable_entry *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) bool wg_index_hashtable_replace(struct index_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct index_hashtable_entry *old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct index_hashtable_entry *new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) void wg_index_hashtable_remove(struct index_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct index_hashtable_entry *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct index_hashtable_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) wg_index_hashtable_lookup(struct index_hashtable *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) const enum index_hashtable_type type_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const __le32 index, struct wg_peer **peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #endif /* _WG_PEERLOOKUP_H */