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