18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef _WG_PEER_H
78c2ecf20Sopenharmony_ci#define _WG_PEER_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "device.h"
108c2ecf20Sopenharmony_ci#include "noise.h"
118c2ecf20Sopenharmony_ci#include "cookie.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/netfilter.h>
158c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
168c2ecf20Sopenharmony_ci#include <linux/kref.h>
178c2ecf20Sopenharmony_ci#include <net/dst_cache.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct wg_device;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct endpoint {
228c2ecf20Sopenharmony_ci	union {
238c2ecf20Sopenharmony_ci		struct sockaddr addr;
248c2ecf20Sopenharmony_ci		struct sockaddr_in addr4;
258c2ecf20Sopenharmony_ci		struct sockaddr_in6 addr6;
268c2ecf20Sopenharmony_ci	};
278c2ecf20Sopenharmony_ci	union {
288c2ecf20Sopenharmony_ci		struct {
298c2ecf20Sopenharmony_ci			struct in_addr src4;
308c2ecf20Sopenharmony_ci			/* Essentially the same as addr6->scope_id */
318c2ecf20Sopenharmony_ci			int src_if4;
328c2ecf20Sopenharmony_ci		};
338c2ecf20Sopenharmony_ci		struct in6_addr src6;
348c2ecf20Sopenharmony_ci	};
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct wg_peer {
388c2ecf20Sopenharmony_ci	struct wg_device *device;
398c2ecf20Sopenharmony_ci	struct prev_queue tx_queue, rx_queue;
408c2ecf20Sopenharmony_ci	struct sk_buff_head staged_packet_queue;
418c2ecf20Sopenharmony_ci	int serial_work_cpu;
428c2ecf20Sopenharmony_ci	struct noise_keypairs keypairs;
438c2ecf20Sopenharmony_ci	struct endpoint endpoint;
448c2ecf20Sopenharmony_ci	struct dst_cache endpoint_cache;
458c2ecf20Sopenharmony_ci	rwlock_t endpoint_lock;
468c2ecf20Sopenharmony_ci	struct noise_handshake handshake;
478c2ecf20Sopenharmony_ci	atomic64_t last_sent_handshake;
488c2ecf20Sopenharmony_ci	struct work_struct transmit_handshake_work, clear_peer_work, transmit_packet_work;
498c2ecf20Sopenharmony_ci	struct cookie latest_cookie;
508c2ecf20Sopenharmony_ci	struct hlist_node pubkey_hash;
518c2ecf20Sopenharmony_ci	u64 rx_bytes, tx_bytes;
528c2ecf20Sopenharmony_ci	struct timer_list timer_retransmit_handshake, timer_send_keepalive;
538c2ecf20Sopenharmony_ci	struct timer_list timer_new_handshake, timer_zero_key_material;
548c2ecf20Sopenharmony_ci	struct timer_list timer_persistent_keepalive;
558c2ecf20Sopenharmony_ci	unsigned int timer_handshake_attempts;
568c2ecf20Sopenharmony_ci	u16 persistent_keepalive_interval;
578c2ecf20Sopenharmony_ci	bool timer_need_another_keepalive;
588c2ecf20Sopenharmony_ci	bool sent_lastminute_handshake;
598c2ecf20Sopenharmony_ci	struct timespec64 walltime_last_handshake;
608c2ecf20Sopenharmony_ci	struct kref refcount;
618c2ecf20Sopenharmony_ci	struct rcu_head rcu;
628c2ecf20Sopenharmony_ci	struct list_head peer_list;
638c2ecf20Sopenharmony_ci	struct list_head allowedips_list;
648c2ecf20Sopenharmony_ci	u64 internal_id;
658c2ecf20Sopenharmony_ci	struct napi_struct napi;
668c2ecf20Sopenharmony_ci	bool is_dead;
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistruct wg_peer *wg_peer_create(struct wg_device *wg,
708c2ecf20Sopenharmony_ci			       const u8 public_key[NOISE_PUBLIC_KEY_LEN],
718c2ecf20Sopenharmony_ci			       const u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN]);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct wg_peer *__must_check wg_peer_get_maybe_zero(struct wg_peer *peer);
748c2ecf20Sopenharmony_cistatic inline struct wg_peer *wg_peer_get(struct wg_peer *peer)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	kref_get(&peer->refcount);
778c2ecf20Sopenharmony_ci	return peer;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_civoid wg_peer_put(struct wg_peer *peer);
808c2ecf20Sopenharmony_civoid wg_peer_remove(struct wg_peer *peer);
818c2ecf20Sopenharmony_civoid wg_peer_remove_all(struct wg_device *wg);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciint wg_peer_init(void);
848c2ecf20Sopenharmony_civoid wg_peer_uninit(void);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#endif /* _WG_PEER_H */
87