xref: /freebsd-src/contrib/wireguard-tools/ipc-uapi.h (revision adf376485712c8fffbf3be330d505a969647f479)
1*adf37648SKyle Evans // SPDX-License-Identifier: 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 #include <arpa/inet.h>
7*adf37648SKyle Evans #include <errno.h>
8*adf37648SKyle Evans #include <limits.h>
9*adf37648SKyle Evans #include <net/if.h>
10*adf37648SKyle Evans #include <netdb.h>
11*adf37648SKyle Evans #include <netinet/in.h>
12*adf37648SKyle Evans #include <stddef.h>
13*adf37648SKyle Evans #include <stdio.h>
14*adf37648SKyle Evans #include <stdlib.h>
15*adf37648SKyle Evans #include <string.h>
16*adf37648SKyle Evans #include <sys/socket.h>
17*adf37648SKyle Evans #include "containers.h"
18*adf37648SKyle Evans #include "curve25519.h"
19*adf37648SKyle Evans #include "encoding.h"
20*adf37648SKyle Evans #include "ctype.h"
21*adf37648SKyle Evans 
22*adf37648SKyle Evans #ifdef _WIN32
23*adf37648SKyle Evans #include "ipc-uapi-windows.h"
24*adf37648SKyle Evans #else
25*adf37648SKyle Evans #include "ipc-uapi-unix.h"
26*adf37648SKyle Evans #endif
27*adf37648SKyle Evans 
userspace_set_device(struct wgdevice * dev)28*adf37648SKyle Evans static int userspace_set_device(struct wgdevice *dev)
29*adf37648SKyle Evans {
30*adf37648SKyle Evans 	char hex[WG_KEY_LEN_HEX], ip[INET6_ADDRSTRLEN], host[4096 + 1], service[512 + 1];
31*adf37648SKyle Evans 	struct wgpeer *peer;
32*adf37648SKyle Evans 	struct wgallowedip *allowedip;
33*adf37648SKyle Evans 	FILE *f;
34*adf37648SKyle Evans 	int ret, set_errno = -EPROTO;
35*adf37648SKyle Evans 	socklen_t addr_len;
36*adf37648SKyle Evans 	size_t line_buffer_len = 0, line_len;
37*adf37648SKyle Evans 	char *key = NULL, *value;
38*adf37648SKyle Evans 
39*adf37648SKyle Evans 	f = userspace_interface_file(dev->name);
40*adf37648SKyle Evans 	if (!f)
41*adf37648SKyle Evans 		return -errno;
42*adf37648SKyle Evans 	fprintf(f, "set=1\n");
43*adf37648SKyle Evans 
44*adf37648SKyle Evans 	if (dev->flags & WGDEVICE_HAS_PRIVATE_KEY) {
45*adf37648SKyle Evans 		key_to_hex(hex, dev->private_key);
46*adf37648SKyle Evans 		fprintf(f, "private_key=%s\n", hex);
47*adf37648SKyle Evans 	}
48*adf37648SKyle Evans 	if (dev->flags & WGDEVICE_HAS_LISTEN_PORT)
49*adf37648SKyle Evans 		fprintf(f, "listen_port=%u\n", dev->listen_port);
50*adf37648SKyle Evans 	if (dev->flags & WGDEVICE_HAS_FWMARK)
51*adf37648SKyle Evans 		fprintf(f, "fwmark=%u\n", dev->fwmark);
52*adf37648SKyle Evans 	if (dev->flags & WGDEVICE_REPLACE_PEERS)
53*adf37648SKyle Evans 		fprintf(f, "replace_peers=true\n");
54*adf37648SKyle Evans 
55*adf37648SKyle Evans 	for_each_wgpeer(dev, peer) {
56*adf37648SKyle Evans 		key_to_hex(hex, peer->public_key);
57*adf37648SKyle Evans 		fprintf(f, "public_key=%s\n", hex);
58*adf37648SKyle Evans 		if (peer->flags & WGPEER_REMOVE_ME) {
59*adf37648SKyle Evans 			fprintf(f, "remove=true\n");
60*adf37648SKyle Evans 			continue;
61*adf37648SKyle Evans 		}
62*adf37648SKyle Evans 		if (peer->flags & WGPEER_HAS_PRESHARED_KEY) {
63*adf37648SKyle Evans 			key_to_hex(hex, peer->preshared_key);
64*adf37648SKyle Evans 			fprintf(f, "preshared_key=%s\n", hex);
65*adf37648SKyle Evans 		}
66*adf37648SKyle Evans 		if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6) {
67*adf37648SKyle Evans 			addr_len = 0;
68*adf37648SKyle Evans 			if (peer->endpoint.addr.sa_family == AF_INET)
69*adf37648SKyle Evans 				addr_len = sizeof(struct sockaddr_in);
70*adf37648SKyle Evans 			else if (peer->endpoint.addr.sa_family == AF_INET6)
71*adf37648SKyle Evans 				addr_len = sizeof(struct sockaddr_in6);
72*adf37648SKyle Evans 			if (!getnameinfo(&peer->endpoint.addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST)) {
73*adf37648SKyle Evans 				if (peer->endpoint.addr.sa_family == AF_INET6 && strchr(host, ':'))
74*adf37648SKyle Evans 					fprintf(f, "endpoint=[%s]:%s\n", host, service);
75*adf37648SKyle Evans 				else
76*adf37648SKyle Evans 					fprintf(f, "endpoint=%s:%s\n", host, service);
77*adf37648SKyle Evans 			}
78*adf37648SKyle Evans 		}
79*adf37648SKyle Evans 		if (peer->flags & WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL)
80*adf37648SKyle Evans 			fprintf(f, "persistent_keepalive_interval=%u\n", peer->persistent_keepalive_interval);
81*adf37648SKyle Evans 		if (peer->flags & WGPEER_REPLACE_ALLOWEDIPS)
82*adf37648SKyle Evans 			fprintf(f, "replace_allowed_ips=true\n");
83*adf37648SKyle Evans 		for_each_wgallowedip(peer, allowedip) {
84*adf37648SKyle Evans 			if (allowedip->family == AF_INET) {
85*adf37648SKyle Evans 				if (!inet_ntop(AF_INET, &allowedip->ip4, ip, INET6_ADDRSTRLEN))
86*adf37648SKyle Evans 					continue;
87*adf37648SKyle Evans 			} else if (allowedip->family == AF_INET6) {
88*adf37648SKyle Evans 				if (!inet_ntop(AF_INET6, &allowedip->ip6, ip, INET6_ADDRSTRLEN))
89*adf37648SKyle Evans 					continue;
90*adf37648SKyle Evans 			} else
91*adf37648SKyle Evans 				continue;
92*adf37648SKyle Evans 			fprintf(f, "allowed_ip=%s/%d\n", ip, allowedip->cidr);
93*adf37648SKyle Evans 		}
94*adf37648SKyle Evans 	}
95*adf37648SKyle Evans 	fprintf(f, "\n");
96*adf37648SKyle Evans 	fflush(f);
97*adf37648SKyle Evans 
98*adf37648SKyle Evans 	while (getline(&key, &line_buffer_len, f) > 0) {
99*adf37648SKyle Evans 		line_len = strlen(key);
100*adf37648SKyle Evans 		ret = set_errno;
101*adf37648SKyle Evans 		if (line_len == 1 && key[0] == '\n')
102*adf37648SKyle Evans 			goto out;
103*adf37648SKyle Evans 		value = strchr(key, '=');
104*adf37648SKyle Evans 		if (!value || line_len == 0 || key[line_len - 1] != '\n')
105*adf37648SKyle Evans 			break;
106*adf37648SKyle Evans 		*value++ = key[--line_len] = '\0';
107*adf37648SKyle Evans 
108*adf37648SKyle Evans 		if (!strcmp(key, "errno")) {
109*adf37648SKyle Evans 			long long num;
110*adf37648SKyle Evans 			char *end;
111*adf37648SKyle Evans 			if (value[0] != '-' && !char_is_digit(value[0]))
112*adf37648SKyle Evans 				break;
113*adf37648SKyle Evans 			num = strtoll(value, &end, 10);
114*adf37648SKyle Evans 			if (*end || num > INT_MAX || num < INT_MIN)
115*adf37648SKyle Evans 				break;
116*adf37648SKyle Evans 			set_errno = num;
117*adf37648SKyle Evans 		}
118*adf37648SKyle Evans 	}
119*adf37648SKyle Evans 	ret = errno ? -errno : -EPROTO;
120*adf37648SKyle Evans out:
121*adf37648SKyle Evans 	free(key);
122*adf37648SKyle Evans 	fclose(f);
123*adf37648SKyle Evans 	errno = -ret;
124*adf37648SKyle Evans 	return ret;
125*adf37648SKyle Evans }
126*adf37648SKyle Evans 
127*adf37648SKyle Evans #define NUM(max) ({ \
128*adf37648SKyle Evans 	unsigned long long num; \
129*adf37648SKyle Evans 	char *end; \
130*adf37648SKyle Evans 	if (!char_is_digit(value[0])) \
131*adf37648SKyle Evans 		break; \
132*adf37648SKyle Evans 	num = strtoull(value, &end, 10); \
133*adf37648SKyle Evans 	if (*end || num > max) \
134*adf37648SKyle Evans 		break; \
135*adf37648SKyle Evans 	num; \
136*adf37648SKyle Evans })
137*adf37648SKyle Evans 
userspace_get_device(struct wgdevice ** out,const char * iface)138*adf37648SKyle Evans static int userspace_get_device(struct wgdevice **out, const char *iface)
139*adf37648SKyle Evans {
140*adf37648SKyle Evans 	struct wgdevice *dev;
141*adf37648SKyle Evans 	struct wgpeer *peer = NULL;
142*adf37648SKyle Evans 	struct wgallowedip *allowedip = NULL;
143*adf37648SKyle Evans 	size_t line_buffer_len = 0, line_len;
144*adf37648SKyle Evans 	char *key = NULL, *value;
145*adf37648SKyle Evans 	FILE *f;
146*adf37648SKyle Evans 	int ret = -EPROTO;
147*adf37648SKyle Evans 
148*adf37648SKyle Evans 	*out = dev = calloc(1, sizeof(*dev));
149*adf37648SKyle Evans 	if (!dev)
150*adf37648SKyle Evans 		return -errno;
151*adf37648SKyle Evans 
152*adf37648SKyle Evans 	f = userspace_interface_file(iface);
153*adf37648SKyle Evans 	if (!f) {
154*adf37648SKyle Evans 		ret = -errno;
155*adf37648SKyle Evans 		free(dev);
156*adf37648SKyle Evans 		*out = NULL;
157*adf37648SKyle Evans 		return ret;
158*adf37648SKyle Evans 	}
159*adf37648SKyle Evans 
160*adf37648SKyle Evans 	fprintf(f, "get=1\n\n");
161*adf37648SKyle Evans 	fflush(f);
162*adf37648SKyle Evans 
163*adf37648SKyle Evans 	strncpy(dev->name, iface, IFNAMSIZ - 1);
164*adf37648SKyle Evans 	dev->name[IFNAMSIZ - 1] = '\0';
165*adf37648SKyle Evans 
166*adf37648SKyle Evans 	while (getline(&key, &line_buffer_len, f) > 0) {
167*adf37648SKyle Evans 		line_len = strlen(key);
168*adf37648SKyle Evans 		if (line_len == 1 && key[0] == '\n')
169*adf37648SKyle Evans 			goto err;
170*adf37648SKyle Evans 		value = strchr(key, '=');
171*adf37648SKyle Evans 		if (!value || line_len == 0 || key[line_len - 1] != '\n')
172*adf37648SKyle Evans 			break;
173*adf37648SKyle Evans 		*value++ = key[--line_len] = '\0';
174*adf37648SKyle Evans 
175*adf37648SKyle Evans 		if (!peer && !strcmp(key, "private_key")) {
176*adf37648SKyle Evans 			if (!key_from_hex(dev->private_key, value))
177*adf37648SKyle Evans 				break;
178*adf37648SKyle Evans 			curve25519_generate_public(dev->public_key, dev->private_key);
179*adf37648SKyle Evans 			dev->flags |= WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_PUBLIC_KEY;
180*adf37648SKyle Evans 		} else if (!peer && !strcmp(key, "listen_port")) {
181*adf37648SKyle Evans 			dev->listen_port = NUM(0xffffU);
182*adf37648SKyle Evans 			dev->flags |= WGDEVICE_HAS_LISTEN_PORT;
183*adf37648SKyle Evans 		} else if (!peer && !strcmp(key, "fwmark")) {
184*adf37648SKyle Evans 			dev->fwmark = NUM(0xffffffffU);
185*adf37648SKyle Evans 			dev->flags |= WGDEVICE_HAS_FWMARK;
186*adf37648SKyle Evans 		} else if (!strcmp(key, "public_key")) {
187*adf37648SKyle Evans 			struct wgpeer *new_peer = calloc(1, sizeof(*new_peer));
188*adf37648SKyle Evans 
189*adf37648SKyle Evans 			if (!new_peer) {
190*adf37648SKyle Evans 				ret = -ENOMEM;
191*adf37648SKyle Evans 				goto err;
192*adf37648SKyle Evans 			}
193*adf37648SKyle Evans 			allowedip = NULL;
194*adf37648SKyle Evans 			if (peer)
195*adf37648SKyle Evans 				peer->next_peer = new_peer;
196*adf37648SKyle Evans 			else
197*adf37648SKyle Evans 				dev->first_peer = new_peer;
198*adf37648SKyle Evans 			peer = new_peer;
199*adf37648SKyle Evans 			if (!key_from_hex(peer->public_key, value))
200*adf37648SKyle Evans 				break;
201*adf37648SKyle Evans 			peer->flags |= WGPEER_HAS_PUBLIC_KEY;
202*adf37648SKyle Evans 		} else if (peer && !strcmp(key, "preshared_key")) {
203*adf37648SKyle Evans 			if (!key_from_hex(peer->preshared_key, value))
204*adf37648SKyle Evans 				break;
205*adf37648SKyle Evans 			if (!key_is_zero(peer->preshared_key))
206*adf37648SKyle Evans 				peer->flags |= WGPEER_HAS_PRESHARED_KEY;
207*adf37648SKyle Evans 		} else if (peer && !strcmp(key, "endpoint")) {
208*adf37648SKyle Evans 			char *begin, *end;
209*adf37648SKyle Evans 			struct addrinfo *resolved;
210*adf37648SKyle Evans 			struct addrinfo hints = {
211*adf37648SKyle Evans 				.ai_family = AF_UNSPEC,
212*adf37648SKyle Evans 				.ai_socktype = SOCK_DGRAM,
213*adf37648SKyle Evans 				.ai_protocol = IPPROTO_UDP
214*adf37648SKyle Evans 			};
215*adf37648SKyle Evans 			if (!strlen(value))
216*adf37648SKyle Evans 				break;
217*adf37648SKyle Evans 			if (value[0] == '[') {
218*adf37648SKyle Evans 				begin = &value[1];
219*adf37648SKyle Evans 				end = strchr(value, ']');
220*adf37648SKyle Evans 				if (!end)
221*adf37648SKyle Evans 					break;
222*adf37648SKyle Evans 				*end++ = '\0';
223*adf37648SKyle Evans 				if (*end++ != ':' || !*end)
224*adf37648SKyle Evans 					break;
225*adf37648SKyle Evans 			} else {
226*adf37648SKyle Evans 				begin = value;
227*adf37648SKyle Evans 				end = strrchr(value, ':');
228*adf37648SKyle Evans 				if (!end || !*(end + 1))
229*adf37648SKyle Evans 					break;
230*adf37648SKyle Evans 				*end++ = '\0';
231*adf37648SKyle Evans 			}
232*adf37648SKyle Evans 			if (getaddrinfo(begin, end, &hints, &resolved) != 0) {
233*adf37648SKyle Evans 				ret = ENETUNREACH;
234*adf37648SKyle Evans 				goto err;
235*adf37648SKyle Evans 			}
236*adf37648SKyle Evans 			if ((resolved->ai_family == AF_INET && resolved->ai_addrlen == sizeof(struct sockaddr_in)) ||
237*adf37648SKyle Evans 			    (resolved->ai_family == AF_INET6 && resolved->ai_addrlen == sizeof(struct sockaddr_in6)))
238*adf37648SKyle Evans 				memcpy(&peer->endpoint.addr, resolved->ai_addr, resolved->ai_addrlen);
239*adf37648SKyle Evans 			else  {
240*adf37648SKyle Evans 				freeaddrinfo(resolved);
241*adf37648SKyle Evans 				break;
242*adf37648SKyle Evans 			}
243*adf37648SKyle Evans 			freeaddrinfo(resolved);
244*adf37648SKyle Evans 		} else if (peer && !strcmp(key, "persistent_keepalive_interval")) {
245*adf37648SKyle Evans 			peer->persistent_keepalive_interval = NUM(0xffffU);
246*adf37648SKyle Evans 			peer->flags |= WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL;
247*adf37648SKyle Evans 		} else if (peer && !strcmp(key, "allowed_ip")) {
248*adf37648SKyle Evans 			struct wgallowedip *new_allowedip;
249*adf37648SKyle Evans 			char *end, *mask = value, *ip = strsep(&mask, "/");
250*adf37648SKyle Evans 
251*adf37648SKyle Evans 			if (!mask || !char_is_digit(mask[0]))
252*adf37648SKyle Evans 				break;
253*adf37648SKyle Evans 			new_allowedip = calloc(1, sizeof(*new_allowedip));
254*adf37648SKyle Evans 			if (!new_allowedip) {
255*adf37648SKyle Evans 				ret = -ENOMEM;
256*adf37648SKyle Evans 				goto err;
257*adf37648SKyle Evans 			}
258*adf37648SKyle Evans 			if (allowedip)
259*adf37648SKyle Evans 				allowedip->next_allowedip = new_allowedip;
260*adf37648SKyle Evans 			else
261*adf37648SKyle Evans 				peer->first_allowedip = new_allowedip;
262*adf37648SKyle Evans 			allowedip = new_allowedip;
263*adf37648SKyle Evans 			allowedip->family = AF_UNSPEC;
264*adf37648SKyle Evans 			if (strchr(ip, ':')) {
265*adf37648SKyle Evans 				if (inet_pton(AF_INET6, ip, &allowedip->ip6) == 1)
266*adf37648SKyle Evans 					allowedip->family = AF_INET6;
267*adf37648SKyle Evans 			} else {
268*adf37648SKyle Evans 				if (inet_pton(AF_INET, ip, &allowedip->ip4) == 1)
269*adf37648SKyle Evans 					allowedip->family = AF_INET;
270*adf37648SKyle Evans 			}
271*adf37648SKyle Evans 			allowedip->cidr = strtoul(mask, &end, 10);
272*adf37648SKyle Evans 			if (*end || allowedip->family == AF_UNSPEC || (allowedip->family == AF_INET6 && allowedip->cidr > 128) || (allowedip->family == AF_INET && allowedip->cidr > 32))
273*adf37648SKyle Evans 				break;
274*adf37648SKyle Evans 		} else if (peer && !strcmp(key, "last_handshake_time_sec"))
275*adf37648SKyle Evans 			peer->last_handshake_time.tv_sec = NUM(0x7fffffffffffffffULL);
276*adf37648SKyle Evans 		else if (peer && !strcmp(key, "last_handshake_time_nsec"))
277*adf37648SKyle Evans 			peer->last_handshake_time.tv_nsec = NUM(0x7fffffffffffffffULL);
278*adf37648SKyle Evans 		else if (peer && !strcmp(key, "rx_bytes"))
279*adf37648SKyle Evans 			peer->rx_bytes = NUM(0xffffffffffffffffULL);
280*adf37648SKyle Evans 		else if (peer && !strcmp(key, "tx_bytes"))
281*adf37648SKyle Evans 			peer->tx_bytes = NUM(0xffffffffffffffffULL);
282*adf37648SKyle Evans 		else if (!strcmp(key, "errno"))
283*adf37648SKyle Evans 			ret = -NUM(0x7fffffffU);
284*adf37648SKyle Evans 	}
285*adf37648SKyle Evans 	ret = -EPROTO;
286*adf37648SKyle Evans err:
287*adf37648SKyle Evans 	free(key);
288*adf37648SKyle Evans 	if (ret) {
289*adf37648SKyle Evans 		free_wgdevice(dev);
290*adf37648SKyle Evans 		*out = NULL;
291*adf37648SKyle Evans 	}
292*adf37648SKyle Evans 	fclose(f);
293*adf37648SKyle Evans 	errno = -ret;
294*adf37648SKyle Evans 	return ret;
295*adf37648SKyle Evans 
296*adf37648SKyle Evans }
297*adf37648SKyle Evans #undef NUM
298