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