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