pex: add support for sending endpoint notification from the wg port via raw socket
[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_request_update(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 char addrstr[INET6_ADDRSTRLEN];
183 union network_endpoint dest_ep;
184 union network_addr local_addr = {};
185 uint64_t version = 0;
186 int len;
187
188 if (net->net_data_len)
189 version = net->net_data_version;
190
191 D("request network data from host %s",
192 inet_ntop(host->endpoint.sa.sa_family,
193 (host->endpoint.sa.sa_family == AF_INET6 ?
194 (const void *)&host->endpoint.in6.sin6_addr :
195 (const void *)&host->endpoint.in.sin_addr),
196 addrstr, sizeof(addrstr)));
197
198 if (!pex_msg_update_request_init(net->config.pubkey, net->config.key,
199 net->config.auth_key, &host->endpoint,
200 version, true))
201 return;
202
203 __pex_msg_send(-1, &host->endpoint, NULL, 0);
204
205 if (!net->net_config.local_host)
206 return;
207
208 pex_msg_init_ext(net, PEX_MSG_ENDPOINT_NOTIFY, true);
209
210 memcpy(&dest_ep, &host->endpoint, sizeof(dest_ep));
211
212 /* work around issue with local address lookup for local broadcast */
213 if (host->endpoint.sa.sa_family == AF_INET) {
214 uint8_t *data = (uint8_t *)&dest_ep.in.sin_addr;
215
216 if (data[3] == 0xff)
217 data[3] = 0xfe;
218 }
219 network_get_local_addr(&local_addr, &dest_ep);
220
221 memset(&dest_ep, 0, sizeof(dest_ep));
222 dest_ep.sa.sa_family = host->endpoint.sa.sa_family;
223 if (host->endpoint.sa.sa_family == AF_INET) {
224 packet.ipv4.ip = (struct ip){
225 .ip_hl = 5,
226 .ip_v = 4,
227 .ip_ttl = 64,
228 .ip_p = IPPROTO_UDP,
229 .ip_src = local_addr.in,
230 .ip_dst = host->endpoint.in.sin_addr,
231 };
232 dest_ep.in.sin_addr = host->endpoint.in.sin_addr;
233 udp = &packet.ipv4.udp;
234 len = sizeof(packet.ipv4);
235 } else {
236 packet.ipv6.ip = (struct ip6_hdr){
237 .ip6_flow = htonl(6 << 28),
238 .ip6_hops = 128,
239 .ip6_nxt = IPPROTO_UDP,
240 .ip6_src = local_addr.in6,
241 .ip6_dst = host->endpoint.in6.sin6_addr,
242 };
243 dest_ep.in6.sin6_addr = host->endpoint.in6.sin6_addr;
244 udp = &packet.ipv6.udp;
245 len = sizeof(packet.ipv6);
246 }
247
248 udp->uh_sport = htons(net->net_config.local_host->peer.port);
249 udp->uh_dport = host->endpoint.in6.sin6_port;
250
251 if (__pex_msg_send(-1, &dest_ep, &packet, len) < 0)
252 D_NET(net, "pex_msg_send_raw failed: %s", strerror(errno));
253 }
254
255 static void
256 network_pex_request_update_cb(struct uloop_timeout *t)
257 {
258 struct network *net = container_of(t, struct network, pex.request_update_timer);
259 struct network_pex *pex = &net->pex;
260 struct network_pex_host *host;
261
262 uloop_timeout_set(t, 5000);
263
264 if (list_empty(&pex->hosts))
265 return;
266
267 host = list_first_entry(&pex->hosts, struct network_pex_host, list);
268 list_move_tail(&host->list, &pex->hosts);
269 network_pex_host_request_update(net, host);
270 }
271
272 void network_pex_init(struct network *net)
273 {
274 struct network_pex *pex = &net->pex;
275
276 memset(pex, 0, sizeof(*pex));
277 pex->fd.fd = -1;
278 INIT_LIST_HEAD(&pex->hosts);
279 pex->request_update_timer.cb = network_pex_request_update_cb;
280 }
281
282 static void
283 network_pex_query_hosts(struct network *net)
284 {
285 struct network_host *host;
286 int rv = rand();
287 int hosts = 0;
288 int i;
289
290 pex_msg_init(net, PEX_MSG_QUERY);
291
292 avl_for_each_element(&net->hosts, host, node) {
293 struct network_peer *peer = &host->peer;
294 void *id;
295
296 if (host == net->net_config.local_host ||
297 peer->state.connected ||
298 peer->endpoint)
299 continue;
300
301 id = pex_msg_append(PEX_ID_LEN);
302 if (!id)
303 break;
304
305 memcpy(id, peer->key, PEX_ID_LEN);
306 hosts++;
307 }
308
309 if (!hosts)
310 return;
311
312 rv %= net->hosts.count;
313 for (i = 0; i < 2; i++) {
314 avl_for_each_element(&net->hosts, host, node) {
315 struct network_peer *peer = &host->peer;
316
317 if (rv > 0) {
318 rv--;
319 continue;
320 }
321
322 if (host == net->net_config.local_host)
323 continue;
324
325 if (!peer->state.connected)
326 continue;
327
328 D_PEER(net, peer, "send query for %d hosts", hosts);
329 pex_msg_send(net, peer);
330 return;
331 }
332 }
333
334 }
335
336 static void
337 network_pex_send_ping(struct network *net, struct network_peer *peer)
338 {
339 pex_msg_init(net, PEX_MSG_PING);
340 pex_msg_send(net, peer);
341 }
342
343 static void
344 network_pex_send_update_request(struct network *net, struct network_peer *peer,
345 struct sockaddr_in6 *addr)
346 {
347 union network_endpoint ep = {};
348 uint64_t version = 0;
349
350 if (addr)
351 memcpy(&ep.in6, addr, sizeof(ep.in6));
352 else
353 pex_get_peer_addr(&ep.in6, net, peer);
354
355 if (net->net_data_len)
356 version = net->net_data_version;
357
358 if (!pex_msg_update_request_init(net->config.pubkey, net->config.key,
359 net->config.auth_key, &ep,
360 version, !!addr))
361 return;
362
363 pex_msg_send_ext(net, peer, addr);
364 }
365
366 void network_pex_event(struct network *net, struct network_peer *peer,
367 enum pex_event ev)
368 {
369 if (!network_pex_active(&net->pex))
370 return;
371
372 if (peer)
373 D_PEER(net, peer, "PEX event type=%d", ev);
374 else
375 D_NET(net, "PEX event type=%d", ev);
376
377 switch (ev) {
378 case PEX_EV_HANDSHAKE:
379 pex_send_hello(net, peer);
380 if (net->config.type == NETWORK_TYPE_DYNAMIC)
381 network_pex_send_update_request(net, peer, NULL);
382 break;
383 case PEX_EV_ENDPOINT_CHANGE:
384 network_pex_handle_endpoint_change(net, peer);
385 break;
386 case PEX_EV_QUERY:
387 network_pex_query_hosts(net);
388 break;
389 case PEX_EV_PING:
390 network_pex_send_ping(net, peer);
391 break;
392 }
393 }
394
395 static void
396 network_pex_recv_hello(struct network *net, struct network_peer *peer,
397 const struct pex_hello *data, size_t len)
398 {
399 char addrstr[INET6_ADDRSTRLEN];
400 uint16_t flags;
401 int af;
402
403 if (len < sizeof(*data))
404 return;
405
406 if (peer->state.has_local_ep_addr &&
407 !memcmp(&peer->state.local_ep_addr, data->local_addr, sizeof(data->local_addr)))
408 return;
409
410 flags = ntohs(data->flags);
411 af = (flags & PEER_EP_F_IPV6) ? AF_INET6 : AF_INET;
412 D_PEER(net, peer, "set local endpoint address to %s",
413 inet_ntop(af, data->local_addr, addrstr, sizeof(addrstr)));
414 peer->state.has_local_ep_addr = true;
415 memcpy(&peer->state.local_ep_addr, data->local_addr, sizeof(data->local_addr));
416 }
417
418 static void
419 network_pex_recv_peers(struct network *net, struct network_peer *peer,
420 const struct pex_peer_endpoint *data, size_t len)
421 {
422 struct network_peer *local = &net->net_config.local_host->peer;
423 struct network_peer *cur;
424
425 for (; len >= sizeof(*data); len -= sizeof(*data), data++) {
426 union network_endpoint *ep;
427 uint16_t flags;
428 void *addr;
429 int len;
430
431 cur = pex_msg_peer(net, data->peer_id);
432 if (!cur)
433 continue;
434
435 if (cur == peer || cur == local)
436 continue;
437
438 D_PEER(net, peer, "received peer address for %s",
439 network_peer_name(cur));
440 flags = ntohs(data->flags);
441 ep = &cur->state.next_endpoint;
442 ep->sa.sa_family = (flags & PEER_EP_F_IPV6) ? AF_INET6 : AF_INET;
443 addr = network_endpoint_addr(ep, &len);
444 memcpy(addr, data->addr, len);
445 ep->in.sin_port = data->port;
446 }
447 }
448
449 static void
450 network_pex_recv_query(struct network *net, struct network_peer *peer,
451 const uint8_t *data, size_t len)
452 {
453 struct network_peer *cur;
454 int resp = 0;
455
456 pex_msg_init(net, PEX_MSG_NOTIFY_PEERS);
457 for (; len >= 8; data += 8, len -= 8) {
458 cur = pex_msg_peer(net, data);
459 if (!cur || !cur->state.connected)
460 continue;
461
462 if (!pex_msg_add_peer_endpoint(net, cur, peer))
463 resp++;
464 }
465
466 if (!resp)
467 return;
468
469 D_PEER(net, peer, "send query response with %d hosts", resp);
470 pex_msg_send(net, peer);
471 }
472
473 static void
474 network_pex_recv_ping(struct network *net, struct network_peer *peer)
475 {
476 time_t now = time(NULL);
477
478 if (peer->state.last_request == now)
479 return;
480
481 peer->state.last_request = now;
482 pex_msg_init(net, PEX_MSG_PONG);
483 pex_msg_send(net, peer);
484 }
485
486 static void
487 network_pex_recv_update_request(struct network *net, struct network_peer *peer,
488 const uint8_t *data, size_t len,
489 struct sockaddr_in6 *addr)
490 {
491 struct pex_update_request *req = (struct pex_update_request *)data;
492 struct pex_msg_update_send_ctx ctx = {};
493 uint64_t req_version = be64_to_cpu(req->cur_version);
494 int *query_count;
495 bool done = false;
496
497 if (len < sizeof(struct pex_update_request))
498 return;
499
500 if (net->config.type != NETWORK_TYPE_DYNAMIC)
501 return;
502
503 if (peer)
504 query_count = &peer->state.num_net_queries;
505 else
506 query_count = &net->num_net_queries;
507
508 if (++*query_count > 10)
509 return;
510
511 D("receive update request, local version=%"PRIu64", remote version=%"PRIu64, net->net_data_version, req_version);
512
513 if (req_version >= net->net_data_version) {
514 struct pex_update_response_no_data *res;
515
516 pex_msg_init_ext(net, PEX_MSG_UPDATE_RESPONSE_NO_DATA, !!addr);
517 res = pex_msg_append(sizeof(*res));
518 res->req_id = req->req_id;
519 res->cur_version = cpu_to_be64(net->net_data_version);
520 pex_msg_send_ext(net, peer, addr);
521 }
522
523 if (req_version > net->net_data_version)
524 network_pex_send_update_request(net, peer, addr);
525
526 if (!peer || !net->net_data_len)
527 return;
528
529 if (req_version >= net->net_data_version)
530 return;
531
532 pex_msg_update_response_init(&ctx, net->config.pubkey, net->config.auth_key,
533 peer->key, !!addr, (void *)data,
534 net->net_data, net->net_data_len);
535 while (!done) {
536 pex_msg_send_ext(net, peer, addr);
537 done = !pex_msg_update_response_continue(&ctx);
538 }
539 }
540
541 static void
542 network_pex_recv_update_response(struct network *net, const uint8_t *data, size_t len,
543 struct sockaddr_in6 *addr, enum pex_opcode op)
544 {
545 struct network_peer *peer;
546 void *net_data;
547 int net_data_len = 0;
548 uint64_t version = 0;
549 bool no_prev_data = !net->net_data_len;
550
551 if (net->config.type != NETWORK_TYPE_DYNAMIC)
552 return;
553
554 net_data = pex_msg_update_response_recv(data, len, op, &net_data_len, &version);
555 if (!net_data)
556 return;
557
558 if (version <= net->net_data_version) {
559 free(net_data);
560 return;
561 }
562
563 D_NET(net, "received updated network data, len=%d", net_data_len);
564 free(net->net_data);
565
566 net->net_data = net_data;
567 net->net_data_len = net_data_len;
568 net->net_data_version = version;
569 if (network_save_dynamic(net) < 0)
570 return;
571
572 uloop_timeout_set(&net->reload_timer, no_prev_data ? 1 : UNETD_DATA_UPDATE_DELAY);
573 vlist_for_each_element(&net->peers, peer, node) {
574 if (!peer->state.connected)
575 continue;
576 network_pex_send_update_request(net, peer, NULL);
577 }
578 }
579
580 static void
581 network_pex_recv(struct network *net, struct network_peer *peer, struct pex_hdr *hdr)
582 {
583 const void *data = hdr + 1;
584
585 if (hdr->version != 0)
586 return;
587
588 D_PEER(net, peer, "PEX rx op=%d", hdr->opcode);
589 switch (hdr->opcode) {
590 case PEX_MSG_HELLO:
591 network_pex_recv_hello(net, peer, data, hdr->len);
592 break;
593 case PEX_MSG_NOTIFY_PEERS:
594 network_pex_recv_peers(net, peer, data, hdr->len);
595 break;
596 case PEX_MSG_QUERY:
597 network_pex_recv_query(net, peer, data, hdr->len);
598 break;
599 case PEX_MSG_PING:
600 network_pex_recv_ping(net, peer);
601 break;
602 case PEX_MSG_PONG:
603 break;
604 case PEX_MSG_UPDATE_REQUEST:
605 network_pex_recv_update_request(net, peer, data, hdr->len,
606 NULL);
607 break;
608 case PEX_MSG_UPDATE_RESPONSE:
609 case PEX_MSG_UPDATE_RESPONSE_DATA:
610 case PEX_MSG_UPDATE_RESPONSE_NO_DATA:
611 network_pex_recv_update_response(net, data, hdr->len,
612 NULL, hdr->opcode);
613 break;
614 case PEX_MSG_ENDPOINT_NOTIFY:
615 break;
616 }
617 }
618
619 static void
620 network_pex_fd_cb(struct uloop_fd *fd, unsigned int events)
621 {
622 struct network *net = container_of(fd, struct network, pex.fd);
623 struct network_peer *local = &net->net_config.local_host->peer;
624 struct network_peer *peer;
625 struct sockaddr_in6 sin6;
626 static char buf[PEX_BUF_SIZE];
627 struct pex_hdr *hdr = (struct pex_hdr *)buf;
628 ssize_t len;
629
630 while (1) {
631 socklen_t slen = sizeof(sin6);
632
633 len = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&sin6, &slen);
634 if (len < 0) {
635 if (errno == EINTR)
636 continue;
637
638 if (errno == EAGAIN)
639 break;
640
641 D_NET(net, "recvfrom failed: %s", strerror(errno));
642 network_pex_close(net);
643 return;
644 }
645
646 if (!len)
647 continue;
648
649 if (len < sizeof(*hdr))
650 continue;
651
652 hdr->len = ntohs(hdr->len);
653 if (len - sizeof(hdr) < hdr->len)
654 continue;
655
656 peer = pex_msg_peer(net, hdr->id);
657 if (!peer)
658 continue;
659
660 if (memcmp(&sin6.sin6_addr, &peer->local_addr.in6, sizeof(sin6.sin6_addr)) != 0)
661 continue;
662
663 if (peer == local)
664 continue;
665
666 network_pex_recv(net, peer, hdr);
667 }
668 }
669
670 static void
671 network_pex_create_host(struct network *net, union network_endpoint *ep)
672 {
673 struct network_pex *pex = &net->pex;
674 struct network_pex_host *host;
675
676 host = calloc(1, sizeof(*host));
677 memcpy(&host->endpoint, ep, sizeof(host->endpoint));
678 list_add_tail(&host->list, &pex->hosts);
679 network_pex_host_request_update(net, host);
680 }
681
682 static void
683 network_pex_open_auth_connect(struct network *net)
684 {
685 struct network_pex *pex = &net->pex;
686 struct network_peer *peer;
687 struct blob_attr *cur;
688 int rem;
689
690 if (net->config.type != NETWORK_TYPE_DYNAMIC)
691 return;
692
693 uloop_timeout_set(&pex->request_update_timer, 5000);
694
695 vlist_for_each_element(&net->peers, peer, node) {
696 union network_endpoint ep = {};
697
698 if (!peer->endpoint)
699 continue;
700
701 if (network_get_endpoint(&ep, peer->endpoint,
702 UNETD_GLOBAL_PEX_PORT, 0) < 0)
703 continue;
704
705 ep.in.sin_port = htons(UNETD_GLOBAL_PEX_PORT);
706 network_pex_create_host(net, &ep);
707 }
708
709 if (!net->config.auth_connect)
710 return;
711
712 blobmsg_for_each_attr(cur, net->config.auth_connect, rem) {
713 union network_endpoint ep = {};
714
715 if (network_get_endpoint(&ep, blobmsg_get_string(cur),
716 UNETD_GLOBAL_PEX_PORT, 0) < 0)
717 continue;
718
719 network_pex_create_host(net, &ep);
720 }
721 }
722
723
724 int network_pex_open(struct network *net)
725 {
726 struct network_host *local_host = net->net_config.local_host;
727 struct network_peer *local;
728 struct network_pex *pex = &net->pex;
729 struct sockaddr_in6 sin6 = {};
730 int yes = 1;
731 int fd;
732
733 network_pex_open_auth_connect(net);
734
735 if (!local_host || !local_host->peer.pex_port)
736 return 0;
737
738 local = &local_host->peer;
739 fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
740 if (fd < 0)
741 return -1;
742
743 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
744 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
745
746 sin6.sin6_family = AF_INET6;
747 memcpy(&sin6.sin6_addr, &local->local_addr.in6,
748 sizeof(local->local_addr.in6));
749 sin6.sin6_port = htons(local_host->peer.pex_port);
750
751 if (bind(fd, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
752 perror("bind");
753 goto close;
754 }
755
756 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
757 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
758 #ifdef linux
759 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
760 network_name(net), strlen(network_name(net)));
761 #endif
762
763 pex->fd.fd = fd;
764 pex->fd.cb = network_pex_fd_cb;
765 uloop_fd_add(&pex->fd, ULOOP_READ);
766
767 return 0;
768
769 close:
770 close(fd);
771 return -1;
772 }
773
774 void network_pex_close(struct network *net)
775 {
776 struct network_pex *pex = &net->pex;
777 struct network_pex_host *host, *tmp;
778
779 uloop_timeout_cancel(&pex->request_update_timer);
780 list_for_each_entry_safe(host, tmp, &pex->hosts, list) {
781 list_del(&host->list);
782 free(host);
783 }
784
785 if (pex->fd.fd < 0)
786 return;
787
788 uloop_fd_delete(&pex->fd);
789 close(pex->fd.fd);
790 network_pex_init(net);
791 }
792
793 static struct network *
794 global_pex_find_network(const uint8_t *id)
795 {
796 struct network *net;
797
798 avl_for_each_element(&networks, net, node) {
799 if (!memcmp(id, net->config.auth_key, PEX_ID_LEN))
800 return net;
801 }
802
803 return NULL;
804 }
805
806 static void
807 global_pex_recv(struct pex_hdr *hdr, struct sockaddr_in6 *addr)
808 {
809 struct pex_ext_hdr *ehdr = (void *)(hdr + 1);
810 struct network_peer *peer;
811 struct network *net;
812 void *data = (void *)(ehdr + 1);
813 char buf[INET6_ADDRSTRLEN];
814 int addr_len;
815
816 if (hdr->version != 0)
817 return;
818
819 net = global_pex_find_network(ehdr->auth_id);
820 if (!net || net->config.type != NETWORK_TYPE_DYNAMIC)
821 return;
822
823 *(uint64_t *)hdr->id ^= pex_network_hash(net->config.auth_key, ehdr->nonce);
824
825 D("PEX global rx op=%d", hdr->opcode);
826 switch (hdr->opcode) {
827 case PEX_MSG_HELLO:
828 case PEX_MSG_NOTIFY_PEERS:
829 case PEX_MSG_QUERY:
830 case PEX_MSG_PING:
831 case PEX_MSG_PONG:
832 break;
833 case PEX_MSG_UPDATE_REQUEST:
834 peer = pex_msg_peer(net, hdr->id);
835 network_pex_recv_update_request(net, peer, data, hdr->len,
836 addr);
837 break;
838 case PEX_MSG_UPDATE_RESPONSE:
839 case PEX_MSG_UPDATE_RESPONSE_DATA:
840 case PEX_MSG_UPDATE_RESPONSE_NO_DATA:
841 network_pex_recv_update_response(net, data, hdr->len, addr, hdr->opcode);
842 break;
843 case PEX_MSG_ENDPOINT_NOTIFY:
844 peer = pex_msg_peer(net, hdr->id);
845 if (!peer)
846 break;
847
848 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
849 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
850 struct in_addr in = *(struct in_addr *)&addr->sin6_addr.s6_addr[12];
851 int port = addr->sin6_port;
852
853 memset(addr, 0, sizeof(*addr));
854 sin->sin_port = port;
855 sin->sin_family = AF_INET;
856 sin->sin_addr = in;
857 }
858
859 D_PEER(net, peer, "receive endpoint notification from %s",
860 inet_ntop(addr->sin6_family, network_endpoint_addr((void *)addr, &addr_len),
861 buf, sizeof(buf)));
862
863 memcpy(&peer->state.next_endpoint, addr, sizeof(*addr));
864 break;
865 }
866 }
867
868 int global_pex_open(void)
869 {
870 struct sockaddr_in6 sin6 = {};
871
872 sin6.sin6_family = AF_INET6;
873 sin6.sin6_port = htons(global_pex_port);
874
875 return pex_open(&sin6, sizeof(sin6), global_pex_recv, true);
876 }