peerlookup.h 1.8 KB

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