Lines Matching +full:key +full:- +full:value
1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
23 #include "ipc-uapi-windows.h"
25 #include "ipc-uapi-unix.h"
34 int ret, set_errno = -EPROTO; in userspace_set_device()
37 char *key = NULL, *value; in userspace_set_device() local
39 f = userspace_interface_file(dev->name); in userspace_set_device()
41 return -errno; in userspace_set_device()
44 if (dev->flags & WGDEVICE_HAS_PRIVATE_KEY) { in userspace_set_device()
45 key_to_hex(hex, dev->private_key); in userspace_set_device()
48 if (dev->flags & WGDEVICE_HAS_LISTEN_PORT) in userspace_set_device()
49 fprintf(f, "listen_port=%u\n", dev->listen_port); in userspace_set_device()
50 if (dev->flags & WGDEVICE_HAS_FWMARK) in userspace_set_device()
51 fprintf(f, "fwmark=%u\n", dev->fwmark); in userspace_set_device()
52 if (dev->flags & WGDEVICE_REPLACE_PEERS) in userspace_set_device()
56 key_to_hex(hex, peer->public_key); in userspace_set_device()
58 if (peer->flags & WGPEER_REMOVE_ME) { in userspace_set_device()
62 if (peer->flags & WGPEER_HAS_PRESHARED_KEY) { in userspace_set_device()
63 key_to_hex(hex, peer->preshared_key); in userspace_set_device()
66 if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6) { in userspace_set_device()
68 if (peer->endpoint.addr.sa_family == AF_INET) in userspace_set_device()
70 else if (peer->endpoint.addr.sa_family == AF_INET6) in userspace_set_device()
72 …if (!getnameinfo(&peer->endpoint.addr, addr_len, host, sizeof(host), service, sizeof(service), NI_… in userspace_set_device()
73 if (peer->endpoint.addr.sa_family == AF_INET6 && strchr(host, ':')) in userspace_set_device()
79 if (peer->flags & WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL) in userspace_set_device()
80 fprintf(f, "persistent_keepalive_interval=%u\n", peer->persistent_keepalive_interval); in userspace_set_device()
81 if (peer->flags & WGPEER_REPLACE_ALLOWEDIPS) in userspace_set_device()
84 if (allowedip->family == AF_INET) { in userspace_set_device()
85 if (!inet_ntop(AF_INET, &allowedip->ip4, ip, INET6_ADDRSTRLEN)) in userspace_set_device()
87 } else if (allowedip->family == AF_INET6) { in userspace_set_device()
88 if (!inet_ntop(AF_INET6, &allowedip->ip6, ip, INET6_ADDRSTRLEN)) in userspace_set_device()
92 fprintf(f, "allowed_ip=%s/%d\n", ip, allowedip->cidr); in userspace_set_device()
98 while (getline(&key, &line_buffer_len, f) > 0) { in userspace_set_device()
99 line_len = strlen(key); in userspace_set_device()
101 if (line_len == 1 && key[0] == '\n') in userspace_set_device()
103 value = strchr(key, '='); in userspace_set_device()
104 if (!value || line_len == 0 || key[line_len - 1] != '\n') in userspace_set_device()
106 *value++ = key[--line_len] = '\0'; in userspace_set_device()
108 if (!strcmp(key, "errno")) { in userspace_set_device()
111 if (value[0] != '-' && !char_is_digit(value[0])) in userspace_set_device()
113 num = strtoll(value, &end, 10); in userspace_set_device()
119 ret = errno ? -errno : -EPROTO; in userspace_set_device()
121 free(key); in userspace_set_device()
123 errno = -ret; in userspace_set_device()
130 if (!char_is_digit(value[0])) \
132 num = strtoull(value, &end, 10); \
144 char *key = NULL, *value; in userspace_get_device() local
146 int ret = -EPROTO; in userspace_get_device()
150 return -errno; in userspace_get_device()
154 ret = -errno; in userspace_get_device()
163 strncpy(dev->name, iface, IFNAMSIZ - 1); in userspace_get_device()
164 dev->name[IFNAMSIZ - 1] = '\0'; in userspace_get_device()
166 while (getline(&key, &line_buffer_len, f) > 0) { in userspace_get_device()
167 line_len = strlen(key); in userspace_get_device()
168 if (line_len == 1 && key[0] == '\n') in userspace_get_device()
170 value = strchr(key, '='); in userspace_get_device()
171 if (!value || line_len == 0 || key[line_len - 1] != '\n') in userspace_get_device()
173 *value++ = key[--line_len] = '\0'; in userspace_get_device()
175 if (!peer && !strcmp(key, "private_key")) { in userspace_get_device()
176 if (!key_from_hex(dev->private_key, value)) in userspace_get_device()
178 curve25519_generate_public(dev->public_key, dev->private_key); in userspace_get_device()
179 dev->flags |= WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_PUBLIC_KEY; in userspace_get_device()
180 } else if (!peer && !strcmp(key, "listen_port")) { in userspace_get_device()
181 dev->listen_port = NUM(0xffffU); in userspace_get_device()
182 dev->flags |= WGDEVICE_HAS_LISTEN_PORT; in userspace_get_device()
183 } else if (!peer && !strcmp(key, "fwmark")) { in userspace_get_device()
184 dev->fwmark = NUM(0xffffffffU); in userspace_get_device()
185 dev->flags |= WGDEVICE_HAS_FWMARK; in userspace_get_device()
186 } else if (!strcmp(key, "public_key")) { in userspace_get_device()
190 ret = -ENOMEM; in userspace_get_device()
195 peer->next_peer = new_peer; in userspace_get_device()
197 dev->first_peer = new_peer; in userspace_get_device()
199 if (!key_from_hex(peer->public_key, value)) in userspace_get_device()
201 peer->flags |= WGPEER_HAS_PUBLIC_KEY; in userspace_get_device()
202 } else if (peer && !strcmp(key, "preshared_key")) { in userspace_get_device()
203 if (!key_from_hex(peer->preshared_key, value)) in userspace_get_device()
205 if (!key_is_zero(peer->preshared_key)) in userspace_get_device()
206 peer->flags |= WGPEER_HAS_PRESHARED_KEY; in userspace_get_device()
207 } else if (peer && !strcmp(key, "endpoint")) { in userspace_get_device()
215 if (!strlen(value)) in userspace_get_device()
217 if (value[0] == '[') { in userspace_get_device()
218 begin = &value[1]; in userspace_get_device()
219 end = strchr(value, ']'); in userspace_get_device()
226 begin = value; in userspace_get_device()
227 end = strrchr(value, ':'); in userspace_get_device()
236 if ((resolved->ai_family == AF_INET && resolved->ai_addrlen == sizeof(struct sockaddr_in)) || in userspace_get_device()
237 (resolved->ai_family == AF_INET6 && resolved->ai_addrlen == sizeof(struct sockaddr_in6))) in userspace_get_device()
238 memcpy(&peer->endpoint.addr, resolved->ai_addr, resolved->ai_addrlen); in userspace_get_device()
244 } else if (peer && !strcmp(key, "persistent_keepalive_interval")) { in userspace_get_device()
245 peer->persistent_keepalive_interval = NUM(0xffffU); in userspace_get_device()
246 peer->flags |= WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL; in userspace_get_device()
247 } else if (peer && !strcmp(key, "allowed_ip")) { in userspace_get_device()
249 char *end, *mask = value, *ip = strsep(&mask, "/"); in userspace_get_device()
255 ret = -ENOMEM; in userspace_get_device()
259 allowedip->next_allowedip = new_allowedip; in userspace_get_device()
261 peer->first_allowedip = new_allowedip; in userspace_get_device()
263 allowedip->family = AF_UNSPEC; in userspace_get_device()
265 if (inet_pton(AF_INET6, ip, &allowedip->ip6) == 1) in userspace_get_device()
266 allowedip->family = AF_INET6; in userspace_get_device()
268 if (inet_pton(AF_INET, ip, &allowedip->ip4) == 1) in userspace_get_device()
269 allowedip->family = AF_INET; in userspace_get_device()
271 allowedip->cidr = strtoul(mask, &end, 10); in userspace_get_device()
272 …|| allowedip->family == AF_UNSPEC || (allowedip->family == AF_INET6 && allowedip->cidr > 128) || (… in userspace_get_device()
274 } else if (peer && !strcmp(key, "last_handshake_time_sec")) in userspace_get_device()
275 peer->last_handshake_time.tv_sec = NUM(0x7fffffffffffffffULL); in userspace_get_device()
276 else if (peer && !strcmp(key, "last_handshake_time_nsec")) in userspace_get_device()
277 peer->last_handshake_time.tv_nsec = NUM(0x7fffffffffffffffULL); in userspace_get_device()
278 else if (peer && !strcmp(key, "rx_bytes")) in userspace_get_device()
279 peer->rx_bytes = NUM(0xffffffffffffffffULL); in userspace_get_device()
280 else if (peer && !strcmp(key, "tx_bytes")) in userspace_get_device()
281 peer->tx_bytes = NUM(0xffffffffffffffffULL); in userspace_get_device()
282 else if (!strcmp(key, "errno")) in userspace_get_device()
283 ret = -NUM(0x7fffffffU); in userspace_get_device()
285 ret = -EPROTO; in userspace_get_device()
287 free(key); in userspace_get_device()
293 errno = -ret; in userspace_get_device()