unet-cli: add DHT support
[project/unetd.git] / pex.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 <netinet/in.h>
9 #include <netinet/ip.h>
10 #include <netinet/ip6.h>
11 #include <netinet/udp.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <inttypes.h>
15 #include "unetd.h"
16 #include "pex-msg.h"
17
18 static const char *pex_peer_id_str(const uint8_t *key)
19 {
20 static char str[20];
21 int i;
22
23 for (i = 0; i < 8; i++)
24 sprintf(str + i * 2, "%02x", key[i]);
25
26 return str;
27 }
28
29 static struct pex_hdr *
30 pex_msg_init(struct network *net, uint8_t opcode)
31 {
32 return __pex_msg_init(net->config.pubkey, opcode);
33 }
34
35 static struct pex_hdr *
36 pex_msg_init_ext(struct network *net, uint8_t opcode, bool ext)
37 {
38 return __pex_msg_init_ext(net->config.pubkey, net->config.auth_key, opcode, ext);
39 }
40
41 static struct network_peer *
42 pex_msg_peer(struct network *net, const uint8_t *id)
43 {
44 struct network_peer *peer;
45 uint8_t key[WG_KEY_LEN] = {};
46
47 memcpy(key, id, PEX_ID_LEN);
48 peer = avl_find_ge_element(&net->peers.avl, key, peer, node.avl);
49 if (!peer || memcmp(peer->key, key, PEX_ID_LEN) != 0) {
50 D_NET(net, "can't find peer %s", pex_peer_id_str(id));
51 return NULL;
52 }
53
54 return peer;
55 }
56
57 static void
58 pex_get_peer_addr(struct sockaddr_in6 *sin6, struct network *net,
59 struct network_peer *peer)
60 {
61 *sin6 = (struct sockaddr_in6){
62 .sin6_family = AF_INET6,
63 .sin6_addr = peer->local_addr.in6,
64 .sin6_port = htons(peer->pex_port),
65 };
66 }
67
68 static void pex_msg_send(struct network *net, struct network_peer *peer)
69 {
70 struct sockaddr_in6 sin6 = {};
71
72 if (!peer || peer == &net->net_config.local_host->peer ||
73 !peer->pex_port)
74 return;
75
76 pex_get_peer_addr(&sin6, net, peer);
77 if (__pex_msg_send(net->pex.fd.fd, &sin6, NULL, 0) < 0)
78 D_PEER(net, peer, "pex_msg_send failed: %s", strerror(errno));
79 }
80
81 static void pex_msg_send_ext(struct network *net, struct network_peer *peer,
82 struct sockaddr_in6 *addr)
83 {
84 char addrbuf[INET6_ADDRSTRLEN];
85
86 if (!addr)
87 return pex_msg_send(net, peer);
88
89 if (__pex_msg_send(-1, addr, NULL, 0) < 0)
90 D_NET(net, "pex_msg_send_ext(%s) failed: %s",
91 inet_ntop(addr->sin6_family, (const void *)&addr->sin6_addr, addrbuf,
92 sizeof(addrbuf)),
93 strerror(errno));
94 }
95
96 static void
97 pex_send_hello(struct network *net, struct network_peer *peer)
98 {
99 struct pex_hello *data;
100
101 pex_msg_init(net, PEX_MSG_HELLO);
102 data = pex_msg_append(sizeof(*data));
103 if (peer->state.endpoint.sa.sa_family == AF_INET6)
104 data->flags |= htons(PEER_EP_F_IPV6);
105 if (network_get_local_addr(&data->local_addr, &peer->state.endpoint))
106 return;
107
108 pex_msg_send(net, peer);
109 }
110
111 static int
112 pex_msg_add_peer_endpoint(struct network *net, struct network_peer *peer,
113 struct network_peer *receiver)
114 {
115 struct pex_peer_endpoint *data;
116 uint16_t flags = 0;
117 const void *addr;
118 int port;
119 int len;
120
121 addr = network_endpoint_addr(&peer->state.endpoint, &len);
122 port = peer->state.endpoint.in.sin_port;
123 if (len > 4)
124 flags |= PEER_EP_F_IPV6;
125 if (network_endpoint_addr_equal(&peer->state.endpoint,
126 &receiver->state.endpoint)) {
127 if (!peer->state.has_local_ep_addr) {
128 D_PEER(net, peer, "can't send peer to %s, missing local address",
129 network_peer_name(receiver));
130 return -1;
131 }
132
133 addr = &peer->state.local_ep_addr;
134 port = htons(peer->port);
135 flags |= PEER_EP_F_LOCAL;
136 }
137
138 data = pex_msg_append(sizeof(*data));
139 if (!data)
140 return -1;
141
142 memcpy(data->peer_id, peer->key, sizeof(data->peer_id));
143 memcpy(data->addr, addr, len);
144 data->port = port;
145 data->flags = htons(flags);
146 D_PEER(net, peer, "send endpoint to %s", network_peer_name(receiver));
147
148 return 0;
149 }
150
151 static void
152 network_pex_handle_endpoint_change(struct network *net, struct network_peer *peer)
153 {
154 struct network_peer *cur;
155
156 vlist_for_each_element(&net->peers, cur, node) {
157 if (cur == peer || !cur->state.connected)
158 continue;
159
160 pex_msg_init(net, PEX_MSG_NOTIFY_PEERS);
161 if (pex_msg_add_peer_endpoint(net, peer, cur))
162 continue;
163
164 pex_msg_send(net, cur);
165 }
166 }
167
168 static void
169 network_pex_host_send_endpoint_notify(struct network *net, struct network_pex_host *host)
170 {
171 union {
172 struct {
173 struct ip ip;
174 struct udphdr udp;
175 } ipv4;
176 struct {
177 struct ip6_hdr ip;
178 struct udphdr udp;
179 } ipv6;
180 } packet = {};
181 struct udphdr *udp;
182 union network_endpoint dest_ep;
183 union network_addr local_addr = {};
184 int len;
185
186 pex_msg_init_ext(net, PEX_MSG_ENDPOINT_NOTIFY, true);
187
188 memcpy(&dest_ep, &host->endpoint, sizeof(dest_ep));
189
190 /* work around issue with local address lookup for local broadcast */
191 if (host->endpoint.sa.sa_family == AF_INET) {
192 uint8_t *data = (uint8_t *)&dest_ep.in.sin_addr;
193
194 if (data[3] == 0xff)
195 data[3] = 0xfe;
196 }
197 network_get_local_addr(&local_addr, &dest_ep);
198
199 memset(&dest_ep, 0, sizeof(dest_ep));
200 dest_ep.sa.sa_family = host->endpoint.sa.sa_family;
201 if (host->endpoint.sa.sa_family == AF_INET) {
202 packet.ipv4.ip = (struct ip){
203 .ip_hl = 5,
204 .ip_v = 4,
205 .ip_ttl = 64,
206 .ip_p = IPPROTO_UDP,
207 .ip_src = local_addr.in,
208 .ip_dst = host->endpoint.in.sin_addr,
209 };
210 dest_ep.in.sin_addr = host->endpoint.in.sin_addr;
211 udp = &packet.ipv4.udp;
212 len = sizeof(packet.ipv4);
213 } else {
214 packet.ipv6.ip = (struct ip6_hdr){
215 .ip6_flow = htonl(6 << 28),
216 .ip6_hops = 128,
217 .ip6_nxt = IPPROTO_UDP,
218 .ip6_src = local_addr.in6,
219 .ip6_dst = host->endpoint.in6.sin6_addr,
220 };
221 dest_ep.in6.sin6_addr = host->endpoint.in6.sin6_addr;
222 udp = &packet.ipv6.udp;
223 len = sizeof(packet.ipv6);
224 }
225
226 udp->uh_sport = htons(net->net_config.local_host->peer.port);
227 udp->uh_dport = host->endpoint.in6.sin6_port;
228
229 if (__pex_msg_send(-1, &dest_ep, &packet, len) < 0)
230 D_NET(net, "pex_msg_send_raw failed: %s", strerror(errno));
231 }
232
233
234 static void
235 network_pex_host_send_port_notify(struct network *net, struct network_pex_host *host)
236 {
237 struct pex_endpoint_port_notify *data;
238
239 if (!net->stun.port_ext)
240 return;
241
242 pex_msg_init_ext(net, PEX_MSG_ENDPOINT_PORT_NOTIFY, true);
243
244 data = pex_msg_append(sizeof(*data));
245 data->port = htons(net->stun.port_ext);
246
247 __pex_msg_send(-1, &host->endpoint, NULL, 0);
248 }
249
250 static void
251 network_pex_host_request_update(struct network *net, struct network_pex_host *host)
252 {
253 char addrstr[INET6_ADDRSTRLEN];
254 uint64_t version = 0;
255
256 host->last_ping = unet_gettime();
257
258 if (net->net_data_len)
259 version = net->net_data_version;
260
261 D("request network data from host %s",
262 inet_ntop(host->endpoint.sa.sa_family,
263 (host->endpoint.sa.sa_family == AF_INET6 ?
264 (const void *)&host->endpoint.in6.sin6_addr :
265 (const void *)&host->endpoint.in.sin_addr),
266 addrstr, sizeof(addrstr)));
267
268 if (!pex_msg_update_request_init(net->config.pubkey, net->config.key,
269 net->config.auth_key, &host->endpoint,
270 version, true))
271 return;
272
273 __pex_msg_send(-1, &host->endpoint, NULL, 0);
274
275 if (!net->net_config.local_host)
276 return;
277
278 network_pex_host_send_port_notify(net, host);
279 network_pex_host_send_endpoint_notify(net, host);
280 }
281
282 static void
283 network_pex_request_update_cb(struct uloop_timeout *t)
284 {
285 struct network *net = container_of(t, struct network, pex.request_update_timer);
286 struct network_pex *pex = &net->pex;
287 struct network_pex_host *host, *tmp;
288 uint64_t now = unet_gettime();
289
290 uloop_timeout_set(t, 500);
291
292 if (list_empty(&pex->hosts))
293 return;
294
295 list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
296 if (host->timeout && host->timeout < now) {
297 list_del(&host->list);
298 free(host);
299 continue;
300 }
301
302 if (host->last_ping + 10 >= now)
303 continue;
304
305 list_move_tail(&host->list, &pex->hosts);
306 network_pex_host_request_update(net, host);
307 }
308 }
309
310 void network_pex_init(struct network *net)
311 {
312 struct network_pex *pex = &net->pex;
313
314 memset(pex, 0, sizeof(*pex));
315 pex->fd.fd = -1;
316 INIT_LIST_HEAD(&pex->hosts);
317 pex->request_update_timer.cb = network_pex_request_update_cb;
318 }
319
320 static void
321 network_pex_query_hosts(struct network *net)
322 {
323 struct network_host *host;
324 int rv = rand();
325 int hosts = 0;
326 int i;
327
328 pex_msg_init(net, PEX_MSG_QUERY);
329
330 avl_for_each_element(&net->hosts, host, node) {
331 struct network_peer *peer = &host->peer;
332 void *id;
333
334 if ((net->stun.port_ext && host == net->net_config.local_host) ||
335 peer->state.connected || peer->endpoint)
336 continue;
337
338 id = pex_msg_append(PEX_ID_LEN);
339 if (!id)
340 break;
341
342 memcpy(id, peer->key, PEX_ID_LEN);
343 hosts++;
344 }
345
346 if (!hosts)
347 return;
348
349 rv %= net->hosts.count;
350 for (i = 0; i < 2; i++) {
351 avl_for_each_element(&net->hosts, host, node) {
352 struct network_peer *peer = &host->peer;
353
354 if (rv > 0) {
355 rv--;
356 continue;
357 }
358
359 if (host == net->net_config.local_host)
360 continue;
361
362 if (!peer->state.connected)
363 continue;
364
365 D_PEER(net, peer, "send query for %d hosts", hosts);
366 pex_msg_send(net, peer);
367 return;
368 }
369 }
370
371 }
372
373 static void
374 network_pex_send_ping(struct network *net, struct network_peer *peer)
375 {
376 if (peer->state.pinged || !peer->state.endpoint.sa.sa_family)
377 return;
378
379 pex_msg_init(net, PEX_MSG_PING);
380 pex_msg_send(net, peer);
381 peer->state.pinged = true;
382 }
383
384 static void
385 network_pex_send_update_request(struct network *net, struct network_peer *peer,
386 struct sockaddr_in6 *addr)
387 {
388 union network_endpoint ep = {};
389 uint64_t version = 0;
390
391 if (addr)
392 memcpy(&ep.in6, addr, sizeof(ep.in6));
393 else
394 pex_get_peer_addr(&ep.in6, net, peer);
395
396 if (net->net_data_len)
397 version = net->net_data_version;
398
399 if (!pex_msg_update_request_init(net->config.pubkey, net->config.key,
400 net->config.auth_key, &ep,
401 version, !!addr))
402 return;
403
404 pex_msg_send_ext(net, peer, addr);
405 }
406
407 void network_pex_event(struct network *net, struct network_peer *peer,
408 enum pex_event ev)
409 {
410 if (!network_pex_active(&net->pex))
411 return;
412
413 switch (ev) {
414 case PEX_EV_HANDSHAKE:
415 pex_send_hello(net, peer);
416 if (net->config.type == NETWORK_TYPE_DYNAMIC)
417 network_pex_send_update_request(net, peer, NULL);
418 break;
419 case PEX_EV_ENDPOINT_CHANGE:
420 network_pex_handle_endpoint_change(net, peer);
421 break;
422 case PEX_EV_QUERY:
423 network_pex_query_hosts(net);
424 break;
425 case PEX_EV_PING:
426 network_pex_send_ping(net, peer);
427 break;
428 }
429 }
430
431 static void
432 network_pex_recv_hello(struct network *net, struct network_peer *peer,
433 const struct pex_hello *data, size_t len)
434 {
435 char addrstr[INET6_ADDRSTRLEN];
436 uint16_t flags;
437 int af;
438
439 if (len < sizeof(*data))
440 return;
441
442 if (peer->state.has_local_ep_addr &&
443 !memcmp(&peer->state.local_ep_addr, data->local_addr, sizeof(data->local_addr)))
444 return;
445
446 flags = ntohs(data->flags);
447 af = (flags & PEER_EP_F_IPV6) ? AF_INET6 : AF_INET;
448 D_PEER(net, peer, "set local endpoint address to %s",
449 inet_ntop(af, data->local_addr, addrstr, sizeof(addrstr)));
450 peer->state.has_local_ep_addr = true;
451 memcpy(&peer->state.local_ep_addr, data->local_addr, sizeof(data->local_addr));
452 }
453
454 static void
455 network_pex_recv_peers(struct network *net, struct network_peer *peer,
456 const struct pex_peer_endpoint *data, size_t len)
457 {
458 struct network_peer *local = &net->net_config.local_host->peer;
459 struct network_peer *cur;
460
461 for (; len >= sizeof(*data); len -= sizeof(*data), data++) {
462 union network_endpoint *ep;
463 uint16_t flags;
464 void *addr;
465 int len;
466
467 if (!memcmp(data->peer_id, &local->key, PEX_ID_LEN)) {
468 network_stun_update_port(net, false, ntohs(data->port));
469 continue;
470 }
471
472 cur = pex_msg_peer(net, data->peer_id);
473 if (!cur || cur == peer)
474 continue;
475
476 D_PEER(net, peer, "received peer address for %s",
477 network_peer_name(cur));
478 flags = ntohs(data->flags);
479 ep = &cur->state.next_endpoint;
480 ep->sa.sa_family = (flags & PEER_EP_F_IPV6) ? AF_INET6 : AF_INET;
481 addr = network_endpoint_addr(ep, &len);
482 memcpy(addr, data->addr, len);
483 ep->in.sin_port = data->port;
484 }
485 }
486
487 static void
488 network_pex_recv_query(struct network *net, struct network_peer *peer,
489 const uint8_t *data, size_t len)
490 {
491 struct network_peer *cur;
492 int resp = 0;
493
494 pex_msg_init(net, PEX_MSG_NOTIFY_PEERS);
495 for (; len >= 8; data += 8, len -= 8) {
496 cur = pex_msg_peer(net, data);
497 if (!cur || !cur->state.connected)
498 continue;
499
500 if (!pex_msg_add_peer_endpoint(net, cur, peer))
501 resp++;
502 }
503
504 if (!resp)
505 return;
506
507 D_PEER(net, peer, "send query response with %d hosts", resp);
508 pex_msg_send(net, peer);
509 }
510
511 static void
512 network_pex_recv_ping(struct network *net, struct network_peer *peer)
513 {
514 time_t now = time(NULL);
515
516 if (peer->state.last_request == now)
517 return;
518
519 peer->state.last_request = now;
520 pex_msg_init(net, PEX_MSG_PONG);
521 pex_msg_send(net, peer);
522 }
523
524 static void
525 network_pex_recv_update_request(struct network *net, struct network_peer *peer,
526 const uint8_t *data, size_t len,
527 struct sockaddr_in6 *addr)
528 {
529 struct pex_update_request *req = (struct pex_update_request *)data;
530 struct pex_msg_update_send_ctx ctx = {};
531 uint64_t req_version = be64_to_cpu(req->cur_version);
532 int *query_count;
533 bool done = false;
534
535 if (len < sizeof(struct pex_update_request))
536 return;
537
538 if (net->config.type != NETWORK_TYPE_DYNAMIC)
539 return;
540
541 if (peer)
542 query_count = &peer->state.num_net_queries;
543 else
544 query_count = &net->num_net_queries;
545
546 if (++*query_count > 10)
547 return;
548
549 D("receive update request, local version=%"PRIu64", remote version=%"PRIu64, net->net_data_version, req_version);
550
551 if (req_version >= net->net_data_version) {
552 struct pex_update_response_no_data *res;
553
554 pex_msg_init_ext(net, PEX_MSG_UPDATE_RESPONSE_NO_DATA, !!addr);
555 res = pex_msg_append(sizeof(*res));
556 res->req_id = req->req_id;
557 res->cur_version = cpu_to_be64(net->net_data_version);
558 pex_msg_send_ext(net, peer, addr);
559 }
560
561 if (req_version > net->net_data_version)
562 network_pex_send_update_request(net, peer, addr);
563
564 if (!peer || !net->net_data_len)
565 return;
566
567 if (req_version >= net->net_data_version)
568 return;
569
570 pex_msg_update_response_init(&ctx, net->config.pubkey, net->config.auth_key,
571 peer->key, !!addr, (void *)data,
572 net->net_data, net->net_data_len);
573 while (!done) {
574 pex_msg_send_ext(net, peer, addr);
575 done = !pex_msg_update_response_continue(&ctx);
576 }
577 }
578
579 static void
580 network_pex_recv_update_response(struct network *net, const uint8_t *data, size_t len,
581 struct sockaddr_in6 *addr, enum pex_opcode op)
582 {
583 struct network_peer *peer;
584 void *net_data;
585 int net_data_len = 0;
586 uint64_t version = 0;
587 bool no_prev_data = !net->net_data_len;
588
589 if (net->config.type != NETWORK_TYPE_DYNAMIC)
590 return;
591
592 net_data = pex_msg_update_response_recv(data, len, op, &net_data_len, &version);
593 if (!net_data)
594 return;
595
596 if (version <= net->net_data_version) {
597 free(net_data);
598 return;
599 }
600
601 D_NET(net, "received updated network data, len=%d", net_data_len);
602 free(net->net_data);
603
604 net->net_data = net_data;
605 net->net_data_len = net_data_len;
606 net->net_data_version = version;
607 if (network_save_dynamic(net) < 0)
608 return;
609
610 uloop_timeout_set(&net->reload_timer, no_prev_data ? 1 : UNETD_DATA_UPDATE_DELAY);
611 vlist_for_each_element(&net->peers, peer, node) {
612 if (!peer->state.connected || !peer->pex_port)
613 continue;
614 network_pex_send_update_request(net, peer, NULL);
615 }
616 }
617
618 static void
619 network_pex_recv(struct network *net, struct network_peer *peer, struct pex_hdr *hdr)
620 {
621 const void *data = hdr + 1;
622
623 if (hdr->version != 0)
624 return;
625
626 D_PEER(net, peer, "PEX rx op=%d", hdr->opcode);
627 switch (hdr->opcode) {
628 case PEX_MSG_HELLO:
629 network_pex_recv_hello(net, peer, data, hdr->len);
630 break;
631 case PEX_MSG_NOTIFY_PEERS:
632 network_pex_recv_peers(net, peer, data, hdr->len);
633 break;
634 case PEX_MSG_QUERY:
635 network_pex_recv_query(net, peer, data, hdr->len);
636 break;
637 case PEX_MSG_PING:
638 network_pex_recv_ping(net, peer);
639 break;
640 case PEX_MSG_PONG:
641 break;
642 case PEX_MSG_UPDATE_REQUEST:
643 network_pex_recv_update_request(net, peer, data, hdr->len,
644 NULL);
645 break;
646 case PEX_MSG_UPDATE_RESPONSE:
647 case PEX_MSG_UPDATE_RESPONSE_DATA:
648 case PEX_MSG_UPDATE_RESPONSE_NO_DATA:
649 network_pex_recv_update_response(net, data, hdr->len,
650 NULL, hdr->opcode);
651 break;
652 case PEX_MSG_ENDPOINT_NOTIFY:
653 break;
654 }
655 }
656
657 static void
658 network_pex_fd_cb(struct uloop_fd *fd, unsigned int events)
659 {
660 struct network *net = container_of(fd, struct network, pex.fd);
661 struct network_peer *local = &net->net_config.local_host->peer;
662 struct network_peer *peer;
663 struct sockaddr_in6 sin6;
664 static char buf[PEX_BUF_SIZE];
665 struct pex_hdr *hdr = (struct pex_hdr *)buf;
666 ssize_t len;
667
668 while (1) {
669 socklen_t slen = sizeof(sin6);
670
671 len = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&sin6, &slen);
672 if (len < 0) {
673 if (errno == EINTR)
674 continue;
675
676 if (errno == EAGAIN)
677 break;
678
679 D_NET(net, "recvfrom failed: %s", strerror(errno));
680 network_pex_close(net);
681 return;
682 }
683
684 if (!len)
685 continue;
686
687 hdr = pex_rx_accept(buf, len, false);
688 if (!hdr)
689 continue;
690
691 peer = pex_msg_peer(net, hdr->id);
692 if (!peer)
693 continue;
694
695 if (memcmp(&sin6.sin6_addr, &peer->local_addr.in6, sizeof(sin6.sin6_addr)) != 0)
696 continue;
697
698 if (peer == local)
699 continue;
700
701 network_pex_recv(net, peer, hdr);
702 }
703 }
704
705 void network_pex_create_host(struct network *net, union network_endpoint *ep,
706 unsigned int timeout)
707 {
708 struct network_pex *pex = &net->pex;
709 struct network_pex_host *host;
710 uint64_t now = unet_gettime();
711 bool new_host = false;
712
713 list_for_each_entry(host, &pex->hosts, list) {
714 if (memcmp(&host->endpoint, ep, sizeof(host->endpoint)) != 0)
715 continue;
716
717 if (host->last_ping + 10 < now) {
718 list_move_tail(&host->list, &pex->hosts);
719 network_pex_host_request_update(net, host);
720 }
721 goto out;
722 }
723
724 host = calloc(1, sizeof(*host));
725 new_host = true;
726 memcpy(&host->endpoint, ep, sizeof(host->endpoint));
727 list_add_tail(&host->list, &pex->hosts);
728
729 out:
730 if (timeout && (new_host || host->timeout))
731 host->timeout = timeout + unet_gettime();
732 }
733
734 static void
735 network_pex_open_auth_connect(struct network *net)
736 {
737 struct network_pex *pex = &net->pex;
738 struct network_peer *peer;
739 struct blob_attr *cur;
740 int rem;
741
742 if (net->config.type != NETWORK_TYPE_DYNAMIC)
743 return;
744
745 uloop_timeout_set(&pex->request_update_timer, 5000);
746
747 vlist_for_each_element(&net->peers, peer, node) {
748 union network_endpoint ep = {};
749
750 if (!peer->endpoint || peer->dynamic)
751 continue;
752
753 if (network_get_endpoint(&ep, AF_UNSPEC, peer->endpoint,
754 UNETD_GLOBAL_PEX_PORT, 0) < 0)
755 continue;
756
757 ep.in.sin_port = htons(UNETD_GLOBAL_PEX_PORT);
758 network_pex_create_host(net, &ep, 0);
759 }
760
761 if (!net->config.auth_connect)
762 return;
763
764 blobmsg_for_each_attr(cur, net->config.auth_connect, rem) {
765 union network_endpoint ep = {};
766
767 if (network_get_endpoint(&ep, AF_UNSPEC, blobmsg_get_string(cur),
768 UNETD_GLOBAL_PEX_PORT, 0) < 0)
769 continue;
770
771 network_pex_create_host(net, &ep, 0);
772 }
773 }
774
775
776 int network_pex_open(struct network *net)
777 {
778 struct network_host *local_host = net->net_config.local_host;
779 struct network_peer *local;
780 struct network_pex *pex = &net->pex;
781 struct sockaddr_in6 sin6 = {};
782 int yes = 1;
783 int fd;
784
785 network_pex_open_auth_connect(net);
786
787 if (!local_host || !local_host->peer.pex_port)
788 return 0;
789
790 local = &local_host->peer;
791 fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
792 if (fd < 0)
793 return -1;
794
795 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
796 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
797
798 sin6.sin6_family = AF_INET6;
799 memcpy(&sin6.sin6_addr, &local->local_addr.in6,
800 sizeof(local->local_addr.in6));
801 sin6.sin6_port = htons(local_host->peer.pex_port);
802
803 if (bind(fd, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
804 perror("bind");
805 goto close;
806 }
807
808 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
809 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
810 #ifdef linux
811 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
812 network_name(net), strlen(network_name(net)));
813 #endif
814
815 pex->fd.fd = fd;
816 pex->fd.cb = network_pex_fd_cb;
817 uloop_fd_add(&pex->fd, ULOOP_READ);
818
819 return 0;
820
821 close:
822 close(fd);
823 return -1;
824 }
825
826 void network_pex_close(struct network *net)
827 {
828 struct network_pex *pex = &net->pex;
829 struct network_pex_host *host, *tmp;
830 uint64_t now = unet_gettime();
831
832 uloop_timeout_cancel(&pex->request_update_timer);
833 list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
834 if (host->timeout)
835 continue;
836
837 if (host->last_active + UNETD_PEX_HOST_ACITVE_TIMEOUT >= now)
838 continue;
839
840 list_del(&host->list);
841 free(host);
842 }
843
844 if (pex->fd.fd < 0)
845 return;
846
847 uloop_fd_delete(&pex->fd);
848 close(pex->fd.fd);
849 network_pex_init(net);
850 }
851
852 void network_pex_free(struct network *net)
853 {
854 struct network_pex *pex = &net->pex;
855 struct network_pex_host *host, *tmp;
856
857 list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
858 list_del(&host->list);
859 free(host);
860 }
861 }
862
863 static struct network *
864 global_pex_find_network(const uint8_t *id)
865 {
866 struct network *net;
867
868 avl_for_each_element(&networks, net, node) {
869 if (!memcmp(id, net->config.auth_key, PEX_ID_LEN))
870 return net;
871 }
872
873 return NULL;
874 }
875
876 static void
877 global_pex_set_active(struct network *net, struct sockaddr_in6 *addr)
878 {
879 struct network_pex *pex = &net->pex;
880 struct network_pex_host *host;
881
882 list_for_each_entry(host, &pex->hosts, list) {
883 if (memcmp(&host->endpoint.in6, addr, sizeof(*addr)) != 0)
884 continue;
885
886 host->last_active = unet_gettime();
887 }
888 }
889
890 static void
891 global_pex_recv(void *msg, size_t msg_len, struct sockaddr_in6 *addr)
892 {
893 struct pex_hdr *hdr;
894 struct pex_ext_hdr *ehdr;
895 struct network_peer *peer;
896 struct network *net;
897 char buf[INET6_ADDRSTRLEN];
898 void *data;
899 int addr_len;
900
901 if (stun_msg_is_valid(msg, msg_len)) {
902 avl_for_each_element(&networks, net, node)
903 network_stun_rx_packet(net, msg, msg_len);
904 }
905
906 hdr = pex_rx_accept(msg, msg_len, true);
907 if (!hdr)
908 return;
909
910 ehdr = (void *)(hdr + 1);
911 data = (void *)(ehdr + 1);
912
913 if (hdr->version != 0)
914 return;
915
916 net = global_pex_find_network(ehdr->auth_id);
917 if (!net || net->config.type != NETWORK_TYPE_DYNAMIC)
918 return;
919
920 *(uint64_t *)hdr->id ^= pex_network_hash(net->config.auth_key, ehdr->nonce);
921
922 global_pex_set_active(net, addr);
923
924 D("PEX global rx op=%d", hdr->opcode);
925 switch (hdr->opcode) {
926 case PEX_MSG_HELLO:
927 case PEX_MSG_NOTIFY_PEERS:
928 case PEX_MSG_QUERY:
929 case PEX_MSG_PING:
930 case PEX_MSG_PONG:
931 break;
932 case PEX_MSG_UPDATE_REQUEST:
933 peer = pex_msg_peer(net, hdr->id);
934 network_pex_recv_update_request(net, peer, data, hdr->len,
935 addr);
936 break;
937 case PEX_MSG_UPDATE_RESPONSE:
938 case PEX_MSG_UPDATE_RESPONSE_DATA:
939 case PEX_MSG_UPDATE_RESPONSE_NO_DATA:
940 network_pex_recv_update_response(net, data, hdr->len, addr, hdr->opcode);
941 break;
942 case PEX_MSG_ENDPOINT_PORT_NOTIFY:
943 if (hdr->len < sizeof(struct pex_endpoint_port_notify))
944 break;
945 case PEX_MSG_ENDPOINT_NOTIFY:
946 peer = pex_msg_peer(net, hdr->id);
947 if (!peer)
948 break;
949
950 D_PEER(net, peer, "receive endpoint notification from %s",
951 inet_ntop(addr->sin6_family, network_endpoint_addr((void *)addr, &addr_len),
952 buf, sizeof(buf)));
953
954 memcpy(&peer->state.next_endpoint, addr, sizeof(*addr));
955 if (hdr->opcode == PEX_MSG_ENDPOINT_PORT_NOTIFY) {
956 struct pex_endpoint_port_notify *port = data;
957
958 peer->state.next_endpoint.in.sin_port = port->port;
959 }
960 break;
961 }
962 }
963
964 static void
965 pex_recv_control(struct pex_msg_local_control *msg, int len)
966 {
967 struct network *net;
968
969 if (msg->msg_type != 0)
970 return;
971
972 net = global_pex_find_network(msg->auth_id);
973 if (!net)
974 return;
975
976 if (!msg->timeout)
977 msg->timeout = 60;
978 network_pex_create_host(net, &msg->ep, msg->timeout);
979 }
980
981 int global_pex_open(const char *unix_path)
982 {
983 struct sockaddr_in6 sin6 = {};
984 int ret;
985
986 sin6.sin6_family = AF_INET6;
987 sin6.sin6_port = htons(global_pex_port);
988
989 ret = pex_open(&sin6, sizeof(sin6), global_pex_recv, true);
990
991 if (unix_path)
992 pex_unix_open(unix_path, pex_recv_control);
993
994 return ret;
995 }