allowedips.h 1.8 KB

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