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