1*adf37648SKyle Evans /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2*adf37648SKyle Evans /*
3*adf37648SKyle Evans  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4*adf37648SKyle Evans  */
5*adf37648SKyle Evans 
6*adf37648SKyle Evans #ifndef CONTAINERS_H
7*adf37648SKyle Evans #define CONTAINERS_H
8*adf37648SKyle Evans 
9*adf37648SKyle Evans #include <stdint.h>
10*adf37648SKyle Evans #include <stdlib.h>
11*adf37648SKyle Evans #include <time.h>
12*adf37648SKyle Evans #include <sys/socket.h>
13*adf37648SKyle Evans #include <net/if.h>
14*adf37648SKyle Evans #include <netinet/in.h>
15*adf37648SKyle Evans #if defined(__linux__)
16*adf37648SKyle Evans #include <linux/wireguard.h>
17*adf37648SKyle Evans #elif defined(__OpenBSD__)
18*adf37648SKyle Evans #include <net/if_wg.h>
19*adf37648SKyle Evans #endif
20*adf37648SKyle Evans 
21*adf37648SKyle Evans #ifndef WG_KEY_LEN
22*adf37648SKyle Evans #define WG_KEY_LEN 32
23*adf37648SKyle Evans #endif
24*adf37648SKyle Evans 
25*adf37648SKyle Evans /* Cross platform __kernel_timespec */
26*adf37648SKyle Evans struct timespec64 {
27*adf37648SKyle Evans 	int64_t tv_sec;
28*adf37648SKyle Evans 	int64_t tv_nsec;
29*adf37648SKyle Evans };
30*adf37648SKyle Evans 
31*adf37648SKyle Evans struct wgallowedip {
32*adf37648SKyle Evans 	uint16_t family;
33*adf37648SKyle Evans 	union {
34*adf37648SKyle Evans 		struct in_addr ip4;
35*adf37648SKyle Evans 		struct in6_addr ip6;
36*adf37648SKyle Evans 	};
37*adf37648SKyle Evans 	uint8_t cidr;
38*adf37648SKyle Evans 	struct wgallowedip *next_allowedip;
39*adf37648SKyle Evans };
40*adf37648SKyle Evans 
41*adf37648SKyle Evans enum {
42*adf37648SKyle Evans 	WGPEER_REMOVE_ME = 1U << 0,
43*adf37648SKyle Evans 	WGPEER_REPLACE_ALLOWEDIPS = 1U << 1,
44*adf37648SKyle Evans 	WGPEER_HAS_PUBLIC_KEY = 1U << 2,
45*adf37648SKyle Evans 	WGPEER_HAS_PRESHARED_KEY = 1U << 3,
46*adf37648SKyle Evans 	WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL = 1U << 4
47*adf37648SKyle Evans };
48*adf37648SKyle Evans 
49*adf37648SKyle Evans struct wgpeer {
50*adf37648SKyle Evans 	uint32_t flags;
51*adf37648SKyle Evans 
52*adf37648SKyle Evans 	uint8_t public_key[WG_KEY_LEN];
53*adf37648SKyle Evans 	uint8_t preshared_key[WG_KEY_LEN];
54*adf37648SKyle Evans 
55*adf37648SKyle Evans 	union {
56*adf37648SKyle Evans 		struct sockaddr addr;
57*adf37648SKyle Evans 		struct sockaddr_in addr4;
58*adf37648SKyle Evans 		struct sockaddr_in6 addr6;
59*adf37648SKyle Evans 	} endpoint;
60*adf37648SKyle Evans 
61*adf37648SKyle Evans 	struct timespec64 last_handshake_time;
62*adf37648SKyle Evans 	uint64_t rx_bytes, tx_bytes;
63*adf37648SKyle Evans 	uint16_t persistent_keepalive_interval;
64*adf37648SKyle Evans 
65*adf37648SKyle Evans 	struct wgallowedip *first_allowedip, *last_allowedip;
66*adf37648SKyle Evans 	struct wgpeer *next_peer;
67*adf37648SKyle Evans };
68*adf37648SKyle Evans 
69*adf37648SKyle Evans enum {
70*adf37648SKyle Evans 	WGDEVICE_REPLACE_PEERS = 1U << 0,
71*adf37648SKyle Evans 	WGDEVICE_HAS_PRIVATE_KEY = 1U << 1,
72*adf37648SKyle Evans 	WGDEVICE_HAS_PUBLIC_KEY = 1U << 2,
73*adf37648SKyle Evans 	WGDEVICE_HAS_LISTEN_PORT = 1U << 3,
74*adf37648SKyle Evans 	WGDEVICE_HAS_FWMARK = 1U << 4
75*adf37648SKyle Evans };
76*adf37648SKyle Evans 
77*adf37648SKyle Evans struct wgdevice {
78*adf37648SKyle Evans 	char name[IFNAMSIZ];
79*adf37648SKyle Evans 	uint32_t ifindex;
80*adf37648SKyle Evans 
81*adf37648SKyle Evans 	uint32_t flags;
82*adf37648SKyle Evans 
83*adf37648SKyle Evans 	uint8_t public_key[WG_KEY_LEN];
84*adf37648SKyle Evans 	uint8_t private_key[WG_KEY_LEN];
85*adf37648SKyle Evans 
86*adf37648SKyle Evans 	uint32_t fwmark;
87*adf37648SKyle Evans 	uint16_t listen_port;
88*adf37648SKyle Evans 
89*adf37648SKyle Evans 	struct wgpeer *first_peer, *last_peer;
90*adf37648SKyle Evans };
91*adf37648SKyle Evans 
92*adf37648SKyle Evans #define for_each_wgpeer(__dev, __peer) for ((__peer) = (__dev)->first_peer; (__peer); (__peer) = (__peer)->next_peer)
93*adf37648SKyle Evans #define for_each_wgallowedip(__peer, __allowedip) for ((__allowedip) = (__peer)->first_allowedip; (__allowedip); (__allowedip) = (__allowedip)->next_allowedip)
94*adf37648SKyle Evans 
free_wgdevice(struct wgdevice * dev)95*adf37648SKyle Evans static inline void free_wgdevice(struct wgdevice *dev)
96*adf37648SKyle Evans {
97*adf37648SKyle Evans 	if (!dev)
98*adf37648SKyle Evans 		return;
99*adf37648SKyle Evans 	for (struct wgpeer *peer = dev->first_peer, *np = peer ? peer->next_peer : NULL; peer; peer = np, np = peer ? peer->next_peer : NULL) {
100*adf37648SKyle Evans 		for (struct wgallowedip *allowedip = peer->first_allowedip, *na = allowedip ? allowedip->next_allowedip : NULL; allowedip; allowedip = na, na = allowedip ? allowedip->next_allowedip : NULL)
101*adf37648SKyle Evans 			free(allowedip);
102*adf37648SKyle Evans 		free(peer);
103*adf37648SKyle Evans 	}
104*adf37648SKyle Evans 	free(dev);
105*adf37648SKyle Evans }
106*adf37648SKyle Evans 
107*adf37648SKyle Evans #endif
108