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