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 */
26 struct timespec64 {
27 int64_t tv_sec;
28 int64_t tv_nsec;
29 };
30
31 enum {
32 WGALLOWEDIP_REMOVE_ME = 1U << 0,
33 };
34
35 struct wgallowedip {
36 uint16_t family;
37 union {
38 struct in_addr ip4;
39 struct in6_addr ip6;
40 };
41 uint8_t cidr;
42 uint32_t flags;
43 struct wgallowedip *next_allowedip;
44 };
45
46 enum {
47 WGPEER_REMOVE_ME = 1U << 0,
48 WGPEER_REPLACE_ALLOWEDIPS = 1U << 1,
49 WGPEER_HAS_PUBLIC_KEY = 1U << 2,
50 WGPEER_HAS_PRESHARED_KEY = 1U << 3,
51 WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL = 1U << 4
52 };
53
54 struct wgpeer {
55 uint32_t flags;
56
57 uint8_t public_key[WG_KEY_LEN];
58 uint8_t preshared_key[WG_KEY_LEN];
59
60 union {
61 struct sockaddr addr;
62 struct sockaddr_in addr4;
63 struct sockaddr_in6 addr6;
64 } endpoint;
65
66 struct timespec64 last_handshake_time;
67 uint64_t rx_bytes, tx_bytes;
68 uint16_t persistent_keepalive_interval;
69
70 struct wgallowedip *first_allowedip, *last_allowedip;
71 struct wgpeer *next_peer;
72 };
73
74 enum {
75 WGDEVICE_REPLACE_PEERS = 1U << 0,
76 WGDEVICE_HAS_PRIVATE_KEY = 1U << 1,
77 WGDEVICE_HAS_PUBLIC_KEY = 1U << 2,
78 WGDEVICE_HAS_LISTEN_PORT = 1U << 3,
79 WGDEVICE_HAS_FWMARK = 1U << 4
80 };
81
82 struct wgdevice {
83 char name[IFNAMSIZ];
84 uint32_t ifindex;
85
86 uint32_t flags;
87
88 uint8_t public_key[WG_KEY_LEN];
89 uint8_t private_key[WG_KEY_LEN];
90
91 uint32_t fwmark;
92 uint16_t listen_port;
93
94 struct wgpeer *first_peer, *last_peer;
95 };
96
97 #define for_each_wgpeer(__dev, __peer) for ((__peer) = (__dev)->first_peer; (__peer); (__peer) = (__peer)->next_peer)
98 #define for_each_wgallowedip(__peer, __allowedip) for ((__allowedip) = (__peer)->first_allowedip; (__allowedip); (__allowedip) = (__allowedip)->next_allowedip)
99
free_wgdevice(struct wgdevice * dev)100 static inline void free_wgdevice(struct wgdevice *dev)
101 {
102 if (!dev)
103 return;
104 for (struct wgpeer *peer = dev->first_peer, *np = peer ? peer->next_peer : NULL; peer; peer = np, np = peer ? peer->next_peer : NULL) {
105 for (struct wgallowedip *allowedip = peer->first_allowedip, *na = allowedip ? allowedip->next_allowedip : NULL; allowedip; allowedip = na, na = allowedip ? allowedip->next_allowedip : NULL)
106 free(allowedip);
107 free(peer);
108 }
109 free(dev);
110 }
111
112 #endif
113