add protocol for exchanging signed network data
[project/unetd.git] / pex-msg.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
4 */
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <arpa/inet.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <libubox/list.h>
11 #include <libubox/uloop.h>
12 #include "pex-msg.h"
13 #include "chacha20.h"
14 #include "auth-data.h"
15
16 static char pex_tx_buf[PEX_BUF_SIZE];
17 static FILE *pex_urandom;
18 static struct uloop_fd pex_fd;
19 static LIST_HEAD(requests);
20 static struct uloop_timeout gc_timer;
21
22 static pex_recv_cb_t pex_recv_cb;
23
24 struct pex_msg_update_recv_ctx {
25 struct list_head list;
26
27 union network_endpoint addr;
28
29 uint8_t priv_key[CURVE25519_KEY_SIZE];
30 uint8_t auth_key[CURVE25519_KEY_SIZE];
31 uint8_t e_key[CURVE25519_KEY_SIZE];
32
33 uint64_t req_id;
34
35 void *data;
36 int data_len;
37 int data_ofs;
38
39 int idle;
40 };
41
42 uint64_t pex_network_hash(const uint8_t *auth_key, uint64_t req_id)
43 {
44 siphash_key_t key = {
45 be64_to_cpu(req_id),
46 be64_to_cpu(req_id)
47 };
48 uint64_t hash;
49
50 siphash_to_be64(&hash, auth_key, CURVE25519_KEY_SIZE, &key);
51
52 return hash;
53 }
54
55
56 struct pex_hdr *__pex_msg_init(const uint8_t *pubkey, uint8_t opcode)
57 {
58 struct pex_hdr *hdr = (struct pex_hdr *)pex_tx_buf;
59
60 hdr->version = 0;
61 hdr->opcode = opcode;
62 hdr->len = 0;
63 memcpy(hdr->id, pubkey, sizeof(hdr->id));
64
65 return hdr;
66 }
67
68 struct pex_hdr *__pex_msg_init_ext(const uint8_t *pubkey, const uint8_t *auth_key,
69 uint8_t opcode, bool ext)
70 {
71 struct pex_hdr *hdr = __pex_msg_init(pubkey, opcode);
72 struct pex_ext_hdr *ehdr = (struct pex_ext_hdr *)(hdr + 1);
73 uint64_t hash;
74
75 if (!ext)
76 return hdr;
77
78 hdr->len = sizeof(*ehdr);
79
80 fread(&ehdr->nonce, sizeof(ehdr->nonce), 1, pex_urandom);
81
82 hash = pex_network_hash(auth_key, ehdr->nonce);
83 *(uint64_t *)hdr->id ^= hash;
84 memcpy(ehdr->auth_id, auth_key, sizeof(ehdr->auth_id));
85
86 return hdr;
87 }
88
89 void *pex_msg_append(size_t len)
90 {
91 struct pex_hdr *hdr = (struct pex_hdr *)pex_tx_buf;
92 int ofs = hdr->len + sizeof(struct pex_hdr);
93 void *buf = &pex_tx_buf[ofs];
94
95 if (sizeof(pex_tx_buf) - ofs < len)
96 return NULL;
97
98 hdr->len += len;
99 memset(buf, 0, len);
100
101 return buf;
102 }
103
104 static void
105 pex_fd_cb(struct uloop_fd *fd, unsigned int events)
106 {
107 struct sockaddr_in6 sin6;
108 static char buf[PEX_BUF_SIZE];
109 struct pex_hdr *hdr = (struct pex_hdr *)buf;
110 ssize_t len;
111
112 while (1) {
113 socklen_t slen = sizeof(sin6);
114
115 len = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&sin6, &slen);
116 if (len < 0) {
117 if (errno == EINTR)
118 continue;
119
120 if (errno == EAGAIN)
121 break;
122
123 pex_close();
124 return;
125 }
126
127 if (!len)
128 continue;
129
130 if (len < sizeof(*hdr) + sizeof(struct pex_ext_hdr))
131 continue;
132
133 hdr->len = ntohs(hdr->len);
134 if (len - sizeof(hdr) - sizeof(struct pex_ext_hdr) < hdr->len)
135 continue;
136
137 pex_recv_cb(hdr, &sin6);
138 }
139 }
140
141 int __pex_msg_send(int fd, const void *addr)
142 {
143 struct pex_hdr *hdr = (struct pex_hdr *)pex_tx_buf;
144 const struct sockaddr *sa = addr;
145 size_t tx_len = sizeof(*hdr) + hdr->len;
146 uint16_t orig_len = hdr->len;
147 size_t addr_len;
148 int ret;
149
150 if (fd < 0) {
151 hdr->len -= sizeof(struct pex_ext_hdr);
152 fd = pex_fd.fd;
153 }
154
155 hdr->len = htons(hdr->len);
156 if (addr) {
157 if (sa->sa_family == AF_INET6)
158 addr_len = sizeof(struct sockaddr_in6);
159 else
160 addr_len = sizeof(struct sockaddr_in);
161 ret = sendto(fd, pex_tx_buf, tx_len, 0, sa, addr_len);
162 } else {
163 ret = send(fd, pex_tx_buf, tx_len, 0);
164 }
165 hdr->len = orig_len;
166
167 return ret;
168 }
169
170 static void
171 pex_msg_update_response_fill(struct pex_msg_update_send_ctx *ctx)
172 {
173 struct pex_hdr *hdr = (struct pex_hdr *)pex_tx_buf;
174 int ofs = hdr->len + sizeof(struct pex_hdr);
175 int cur_len = ctx->rem;
176
177 if (cur_len > PEX_BUF_SIZE - ofs)
178 cur_len = PEX_BUF_SIZE - ofs;
179
180 memcpy(pex_msg_append(cur_len), ctx->cur, cur_len);
181 ctx->cur += cur_len;
182 ctx->rem -= cur_len;
183 }
184
185 void pex_msg_update_response_init(struct pex_msg_update_send_ctx *ctx,
186 const uint8_t *pubkey, const uint8_t *auth_key,
187 const uint8_t *peer_key, bool ext,
188 struct pex_update_request *req,
189 const void *data, int len)
190 {
191 uint8_t e_key_priv[CURVE25519_KEY_SIZE];
192 uint8_t enc_key[CURVE25519_KEY_SIZE];
193 struct pex_update_response *res;
194
195 ctx->pubkey = pubkey;
196 ctx->auth_key = auth_key;
197 ctx->ext = ext;
198 ctx->req_id = req->req_id;
199
200 __pex_msg_init_ext(pubkey, auth_key, PEX_MSG_UPDATE_RESPONSE, ext);
201 res = pex_msg_append(sizeof(*res));
202 res->req_id = req->req_id;
203 res->data_len = len;
204
205 fread(e_key_priv, sizeof(e_key_priv), 1, pex_urandom);
206 curve25519_clamp_secret(e_key_priv);
207 curve25519_generate_public(res->e_key, e_key_priv);
208 curve25519(enc_key, e_key_priv, peer_key);
209
210 ctx->data = ctx->cur = malloc(len);
211 ctx->rem = len;
212
213 memcpy(ctx->data, data, len);
214 chacha20_encrypt_msg(ctx->data, len, &req->req_id, enc_key);
215
216 pex_msg_update_response_fill(ctx);
217 }
218
219 bool pex_msg_update_response_continue(struct pex_msg_update_send_ctx *ctx)
220 {
221 struct pex_update_response_data *res_ext;
222
223 if (ctx->rem <= 0) {
224 free(ctx->data);
225 ctx->data = NULL;
226
227 return false;
228 }
229
230 __pex_msg_init_ext(ctx->pubkey, ctx->auth_key,
231 PEX_MSG_UPDATE_RESPONSE_DATA, ctx->ext);
232 res_ext = pex_msg_append(sizeof(*res_ext));
233 res_ext->req_id = ctx->req_id;
234 res_ext->offset = ctx->cur - ctx->data;
235 pex_msg_update_response_fill(ctx);
236
237 return true;
238 }
239
240
241 struct pex_update_request *
242 pex_msg_update_request_init(const uint8_t *pubkey, const uint8_t *priv_key,
243 const uint8_t *auth_key, union network_endpoint *addr,
244 uint64_t cur_version, bool ext)
245 {
246 struct pex_update_request *req;
247 struct pex_msg_update_recv_ctx *ctx;
248
249 list_for_each_entry(ctx, &requests, list) {
250 if (!memcmp(&ctx->addr, addr, sizeof(ctx->addr)))
251 return NULL;
252 }
253
254 ctx = calloc(1, sizeof(*ctx));
255 memcpy(&ctx->addr, addr, sizeof(ctx->addr));
256 memcpy(ctx->auth_key, auth_key, sizeof(ctx->auth_key));
257 memcpy(ctx->priv_key, priv_key, sizeof(ctx->priv_key));
258 fread(&ctx->req_id, sizeof(ctx->req_id), 1, pex_urandom);
259 list_add_tail(&ctx->list, &requests);
260 if (!gc_timer.pending)
261 uloop_timeout_set(&gc_timer, 1000);
262
263 __pex_msg_init_ext(pubkey, auth_key, PEX_MSG_UPDATE_REQUEST, ext);
264 req = pex_msg_append(sizeof(*req));
265 req->cur_version = cpu_to_be64(cur_version);
266 req->req_id = ctx->req_id;
267
268 return req;
269 }
270
271 static struct pex_msg_update_recv_ctx *
272 pex_msg_update_recv_ctx_get(uint64_t req_id)
273 {
274 struct pex_msg_update_recv_ctx *ctx;
275
276 list_for_each_entry(ctx, &requests, list) {
277 if (ctx->req_id == req_id) {
278 ctx->idle = 0;
279 return ctx;
280 }
281 }
282
283 return NULL;
284 }
285
286 static void pex_msg_update_ctx_free(struct pex_msg_update_recv_ctx *ctx)
287 {
288 list_del(&ctx->list);
289 free(ctx->data);
290 free(ctx);
291 }
292
293 void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op,
294 int *data_len, uint64_t *timestamp)
295 {
296 struct pex_msg_update_recv_ctx *ctx;
297 uint8_t enc_key[CURVE25519_KEY_SIZE];
298 void *ret;
299
300 *data_len = 0;
301 if (op == PEX_MSG_UPDATE_RESPONSE) {
302 const struct pex_update_response *res = data;
303
304 if (len < sizeof(*res))
305 return NULL;
306
307 ctx = pex_msg_update_recv_ctx_get(res->req_id);
308 if (!ctx || ctx->data_len || !res->data_len ||
309 res->data_len > UNETD_NET_DATA_SIZE_MAX)
310 return NULL;
311
312 data += sizeof(*res);
313 len -= sizeof(*res);
314
315 ctx->data_len = res->data_len;
316 memcpy(ctx->e_key, res->e_key, sizeof(ctx->e_key));
317 ctx->data = malloc(ctx->data_len);
318 } else if (op == PEX_MSG_UPDATE_RESPONSE_DATA) {
319 const struct pex_update_response_data *res = data;
320
321 if (len <= sizeof(*res))
322 return NULL;
323
324 ctx = pex_msg_update_recv_ctx_get(res->req_id);
325 if (!ctx || ctx->data_ofs != res->offset)
326 return NULL;
327
328 data += sizeof(*res);
329 len -= sizeof(*res);
330 } else if (op == PEX_MSG_UPDATE_RESPONSE_NO_DATA) {
331 const struct pex_update_response_no_data *res = data;
332
333 if (len < sizeof(*res))
334 return NULL;
335
336 ctx = pex_msg_update_recv_ctx_get(res->req_id);
337 if (!ctx)
338 return NULL;
339
340 goto error;
341 } else {
342 return NULL;
343 }
344
345 if (ctx->data_ofs + len > ctx->data_len)
346 goto error;
347
348 memcpy(ctx->data + ctx->data_ofs, data, len);
349 ctx->data_ofs += len;
350 if (ctx->data_ofs < ctx->data_len)
351 return NULL;
352
353 curve25519(enc_key, ctx->priv_key, ctx->e_key);
354 chacha20_encrypt_msg(ctx->data, ctx->data_len, &ctx->req_id, enc_key);
355 if (unet_auth_data_validate(ctx->auth_key, ctx->data, ctx->data_len, timestamp, NULL))
356 goto error;
357
358 *data_len = ctx->data_len;
359 ret = ctx->data;
360 ctx->data = NULL;
361 pex_msg_update_ctx_free(ctx);
362
363 return ret;
364
365 error:
366 pex_msg_update_ctx_free(ctx);
367 *data_len = -1;
368 return NULL;
369 }
370
371 static void
372 pex_gc_cb(struct uloop_timeout *t)
373 {
374 struct pex_msg_update_recv_ctx *ctx, *tmp;
375
376 list_for_each_entry_safe(ctx, tmp, &requests, list) {
377 if (++ctx->idle <= 3)
378 continue;
379
380 pex_msg_update_ctx_free(ctx);
381 }
382
383 if (!list_empty(&requests))
384 uloop_timeout_set(t, 1000);
385 }
386
387 int pex_open(void *addr, size_t addr_len, pex_recv_cb_t cb, bool server)
388 {
389 struct sockaddr *sa = addr;
390 int yes = 1, no = 0;
391 int fd;
392
393 pex_recv_cb = cb;
394
395 pex_urandom = fopen("/dev/urandom", "r");
396 if (!pex_urandom)
397 return -1;
398
399 fd = socket(sa->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
400 if (fd < 0)
401 goto close_urandom;
402
403 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
404 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
405
406 if (server) {
407 if (bind(fd, addr, addr_len) < 0) {
408 perror("bind");
409 goto close_socket;
410 }
411
412 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
413 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
414 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
415 } else {
416 if (connect(fd, addr, addr_len) < 0) {
417 perror("connect");
418 goto close_socket;
419 }
420 }
421
422 pex_fd.fd = fd;
423 pex_fd.cb = pex_fd_cb;
424 uloop_fd_add(&pex_fd, ULOOP_READ);
425
426 gc_timer.cb = pex_gc_cb;
427
428 return 0;
429
430 close_socket:
431 close(fd);
432 close_urandom:
433 fclose(pex_urandom);
434 return -1;
435 }
436
437 void pex_close(void)
438 {
439 if (!pex_fd.cb)
440 return;
441
442 fclose(pex_urandom);
443 uloop_fd_delete(&pex_fd);
444 close(pex_fd.fd);
445 pex_fd.cb = NULL;
446 pex_urandom = NULL;
447 }