peer.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_PEER_H
  6. #define _WG_PEER_H
  7. #include "device.h"
  8. #include "noise.h"
  9. #include "cookie.h"
  10. #include <linux/types.h>
  11. #include <linux/netfilter.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/kref.h>
  14. #include <net/dst_cache.h>
  15. struct wg_device;
  16. struct endpoint {
  17. union {
  18. struct sockaddr addr;
  19. struct sockaddr_in addr4;
  20. struct sockaddr_in6 addr6;
  21. };
  22. union {
  23. struct {
  24. struct in_addr src4;
  25. /* Essentially the same as addr6->scope_id */
  26. int src_if4;
  27. };
  28. struct in6_addr src6;
  29. };
  30. };
  31. struct wg_peer {
  32. struct wg_device *device;
  33. struct prev_queue tx_queue, rx_queue;
  34. struct sk_buff_head staged_packet_queue;
  35. int serial_work_cpu;
  36. struct noise_keypairs keypairs;
  37. struct endpoint endpoint;
  38. struct dst_cache endpoint_cache;
  39. rwlock_t endpoint_lock;
  40. struct noise_handshake handshake;
  41. atomic64_t last_sent_handshake;
  42. struct work_struct transmit_handshake_work, clear_peer_work, transmit_packet_work;
  43. struct cookie latest_cookie;
  44. struct hlist_node pubkey_hash;
  45. u64 rx_bytes, tx_bytes;
  46. struct timer_list timer_retransmit_handshake, timer_send_keepalive;
  47. struct timer_list timer_new_handshake, timer_zero_key_material;
  48. struct timer_list timer_persistent_keepalive;
  49. unsigned int timer_handshake_attempts;
  50. u16 persistent_keepalive_interval;
  51. bool timer_need_another_keepalive;
  52. bool sent_lastminute_handshake;
  53. struct timespec64 walltime_last_handshake;
  54. struct kref refcount;
  55. struct rcu_head rcu;
  56. struct list_head peer_list;
  57. struct list_head allowedips_list;
  58. u64 internal_id;
  59. struct napi_struct napi;
  60. bool is_dead;
  61. };
  62. struct wg_peer *wg_peer_create(struct wg_device *wg,
  63. const u8 public_key[NOISE_PUBLIC_KEY_LEN],
  64. const u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN]);
  65. struct wg_peer *__must_check wg_peer_get_maybe_zero(struct wg_peer *peer);
  66. static inline struct wg_peer *wg_peer_get(struct wg_peer *peer)
  67. {
  68. kref_get(&peer->refcount);
  69. return peer;
  70. }
  71. void wg_peer_put(struct wg_peer *peer);
  72. void wg_peer_remove(struct wg_peer *peer);
  73. void wg_peer_remove_all(struct wg_device *wg);
  74. int wg_peer_init(void);
  75. void wg_peer_uninit(void);
  76. #endif /* _WG_PEER_H */