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