162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _WG_PEER_H
762306a36Sopenharmony_ci#define _WG_PEER_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include "device.h"
1062306a36Sopenharmony_ci#include "noise.h"
1162306a36Sopenharmony_ci#include "cookie.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/types.h>
1462306a36Sopenharmony_ci#include <linux/netfilter.h>
1562306a36Sopenharmony_ci#include <linux/spinlock.h>
1662306a36Sopenharmony_ci#include <linux/kref.h>
1762306a36Sopenharmony_ci#include <net/dst_cache.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct wg_device;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistruct endpoint {
2262306a36Sopenharmony_ci	union {
2362306a36Sopenharmony_ci		struct sockaddr addr;
2462306a36Sopenharmony_ci		struct sockaddr_in addr4;
2562306a36Sopenharmony_ci		struct sockaddr_in6 addr6;
2662306a36Sopenharmony_ci	};
2762306a36Sopenharmony_ci	union {
2862306a36Sopenharmony_ci		struct {
2962306a36Sopenharmony_ci			struct in_addr src4;
3062306a36Sopenharmony_ci			/* Essentially the same as addr6->scope_id */
3162306a36Sopenharmony_ci			int src_if4;
3262306a36Sopenharmony_ci		};
3362306a36Sopenharmony_ci		struct in6_addr src6;
3462306a36Sopenharmony_ci	};
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct wg_peer {
3862306a36Sopenharmony_ci	struct wg_device *device;
3962306a36Sopenharmony_ci	struct prev_queue tx_queue, rx_queue;
4062306a36Sopenharmony_ci	struct sk_buff_head staged_packet_queue;
4162306a36Sopenharmony_ci	int serial_work_cpu;
4262306a36Sopenharmony_ci	bool is_dead;
4362306a36Sopenharmony_ci	struct noise_keypairs keypairs;
4462306a36Sopenharmony_ci	struct endpoint endpoint;
4562306a36Sopenharmony_ci	struct dst_cache endpoint_cache;
4662306a36Sopenharmony_ci	rwlock_t endpoint_lock;
4762306a36Sopenharmony_ci	struct noise_handshake handshake;
4862306a36Sopenharmony_ci	atomic64_t last_sent_handshake;
4962306a36Sopenharmony_ci	struct work_struct transmit_handshake_work, clear_peer_work, transmit_packet_work;
5062306a36Sopenharmony_ci	struct cookie latest_cookie;
5162306a36Sopenharmony_ci	struct hlist_node pubkey_hash;
5262306a36Sopenharmony_ci	u64 rx_bytes, tx_bytes;
5362306a36Sopenharmony_ci	struct timer_list timer_retransmit_handshake, timer_send_keepalive;
5462306a36Sopenharmony_ci	struct timer_list timer_new_handshake, timer_zero_key_material;
5562306a36Sopenharmony_ci	struct timer_list timer_persistent_keepalive;
5662306a36Sopenharmony_ci	unsigned int timer_handshake_attempts;
5762306a36Sopenharmony_ci	u16 persistent_keepalive_interval;
5862306a36Sopenharmony_ci	bool timer_need_another_keepalive;
5962306a36Sopenharmony_ci	bool sent_lastminute_handshake;
6062306a36Sopenharmony_ci	struct timespec64 walltime_last_handshake;
6162306a36Sopenharmony_ci	struct kref refcount;
6262306a36Sopenharmony_ci	struct rcu_head rcu;
6362306a36Sopenharmony_ci	struct list_head peer_list;
6462306a36Sopenharmony_ci	struct list_head allowedips_list;
6562306a36Sopenharmony_ci	struct napi_struct napi;
6662306a36Sopenharmony_ci	u64 internal_id;
6762306a36Sopenharmony_ci};
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistruct wg_peer *wg_peer_create(struct wg_device *wg,
7062306a36Sopenharmony_ci			       const u8 public_key[NOISE_PUBLIC_KEY_LEN],
7162306a36Sopenharmony_ci			       const u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN]);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct wg_peer *__must_check wg_peer_get_maybe_zero(struct wg_peer *peer);
7462306a36Sopenharmony_cistatic inline struct wg_peer *wg_peer_get(struct wg_peer *peer)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	kref_get(&peer->refcount);
7762306a36Sopenharmony_ci	return peer;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_civoid wg_peer_put(struct wg_peer *peer);
8062306a36Sopenharmony_civoid wg_peer_remove(struct wg_peer *peer);
8162306a36Sopenharmony_civoid wg_peer_remove_all(struct wg_device *wg);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ciint wg_peer_init(void);
8462306a36Sopenharmony_civoid wg_peer_uninit(void);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci#endif /* _WG_PEER_H */
87