Skip to content

Commit 639cdcd

Browse files
committed
pex: add support for figuring out the external data port via STUN servers
When establishing a direct connection on the auth/PEX port via DHT, both sides need to know the externally mapped data port number in order to establish a wireguard connection. If there is an existing data connection, the port can be queried via PEX over the tunnel. If that is not available, an external public server is needed in order to poke a hole in the NAT. The easiest way to do this is to use STUN, since there are a lot of public servers available. The servers can be configured via the network data, based on the assumption, that an auth exchange with network data update can be done directly Signed-off-by: Felix Fietkau <[email protected]>
1 parent e88f2cd commit 639cdcd

12 files changed

+734
-35
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PROJECT(unetd C)
44

55

66
SET(SOURCES
7-
main.c network.c host.c service.c pex.c
7+
main.c network.c host.c service.c pex.c pex-stun.c
88
wg.c wg-user.c
99
)
1010

@@ -43,7 +43,7 @@ ELSE()
4343
SET(ubus "")
4444
ENDIF()
4545

46-
ADD_LIBRARY(unet SHARED curve25519.c siphash.c sha512.c fprime.c f25519.c ed25519.c edsign.c auth-data.c chacha20.c pex-msg.c utils.c)
46+
ADD_LIBRARY(unet SHARED curve25519.c siphash.c sha512.c fprime.c f25519.c ed25519.c edsign.c auth-data.c chacha20.c pex-msg.c utils.c stun.c)
4747
TARGET_LINK_LIBRARIES(unet ubox)
4848

4949
ADD_EXECUTABLE(unetd ${SOURCES})

network.c

+19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum {
3232
NETCONF_ATTR_PORT,
3333
NETCONF_ATTR_PEX_PORT,
3434
NETCONF_ATTR_KEEPALIVE,
35+
NETCONF_ATTR_STUN_SERVERS,
3536
__NETCONF_ATTR_MAX
3637
};
3738

@@ -40,6 +41,7 @@ static const struct blobmsg_policy netconf_policy[__NETCONF_ATTR_MAX] = {
4041
[NETCONF_ATTR_PORT] = { "port", BLOBMSG_TYPE_INT32 },
4142
[NETCONF_ATTR_PEX_PORT] = { "peer-exchange-port", BLOBMSG_TYPE_INT32 },
4243
[NETCONF_ATTR_KEEPALIVE] = { "keepalive", BLOBMSG_TYPE_INT32 },
44+
[NETCONF_ATTR_STUN_SERVERS] = { "stun-servers", BLOBMSG_TYPE_ARRAY },
4345
};
4446

4547
const struct blobmsg_policy network_policy[__NETWORK_ATTR_MAX] = {
@@ -61,6 +63,15 @@ const struct blobmsg_policy network_policy[__NETWORK_ATTR_MAX] = {
6163
AVL_TREE(networks, avl_strcmp, false, NULL);
6264
static struct blob_buf b;
6365

66+
static void network_load_stun_servers(struct network *net, struct blob_attr *data)
67+
{
68+
struct blob_attr *cur;
69+
int rem;
70+
71+
blobmsg_for_each_attr(cur, data, rem)
72+
network_stun_server_add(net, blobmsg_get_string(cur));
73+
}
74+
6475
static void network_load_config_data(struct network *net, struct blob_attr *data)
6576
{
6677
struct blob_attr *tb[__NETCONF_ATTR_MAX];
@@ -95,6 +106,10 @@ static void network_load_config_data(struct network *net, struct blob_attr *data
95106
net->net_config.keepalive = blobmsg_get_u32(cur);
96107
else
97108
net->net_config.keepalive = 0;
109+
110+
if ((cur = tb[NETCONF_ATTR_STUN_SERVERS]) != NULL &&
111+
blobmsg_check_array(cur, BLOBMSG_TYPE_STRING) > 0)
112+
network_load_stun_servers(net, cur);
98113
}
99114

100115
static int network_load_data(struct network *net, struct blob_attr *data)
@@ -398,6 +413,7 @@ static void network_reload(struct uloop_timeout *t)
398413

399414
memset(&net->net_config, 0, sizeof(net->net_config));
400415

416+
network_stun_free(net);
401417
network_pex_close(net);
402418
network_services_free(net);
403419
network_hosts_update_start(net);
@@ -424,6 +440,7 @@ static void network_reload(struct uloop_timeout *t)
424440
unetd_write_hosts();
425441
network_do_update(net, true);
426442
network_pex_open(net);
443+
network_stun_start(net);
427444
unetd_ubus_notify(net);
428445
}
429446

@@ -469,6 +486,7 @@ static void network_teardown(struct network *net)
469486
uloop_timeout_cancel(&net->connect_timer);
470487
uloop_timeout_cancel(&net->reload_timer);
471488
network_do_update(net, false);
489+
network_stun_free(net);
472490
network_pex_close(net);
473491
network_pex_free(net);
474492
network_hosts_free(net);
@@ -600,6 +618,7 @@ network_alloc(const char *name)
600618
avl_insert(&networks, &net->node);
601619

602620
network_pex_init(net);
621+
network_stun_init(net);
603622
network_hosts_init(net);
604623
network_services_init(net);
605624

network.h

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct network {
4949
int port;
5050
int pex_port;
5151
bool local_host_changed;
52+
struct blob_attr *stun_list;
5253
} net_config;
5354

5455
void *net_data;
@@ -71,6 +72,7 @@ struct network {
7172
struct uloop_timeout connect_timer;
7273

7374
struct network_pex pex;
75+
struct network_stun stun;
7476
};
7577

7678
enum {

pex-msg.h

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum pex_opcode {
2424
PEX_MSG_UPDATE_RESPONSE_DATA,
2525
PEX_MSG_UPDATE_RESPONSE_NO_DATA,
2626
PEX_MSG_ENDPOINT_NOTIFY,
27+
PEX_MSG_ENDPOINT_PORT_NOTIFY,
2728
};
2829

2930
#define PEX_ID_LEN 8
@@ -76,6 +77,10 @@ struct pex_update_response_no_data {
7677
uint64_t cur_version;
7778
};
7879

80+
struct pex_endpoint_port_notify {
81+
uint16_t port;
82+
};
83+
7984
struct pex_msg_update_send_ctx {
8085
const uint8_t *pubkey;
8186
const uint8_t *auth_key;

0 commit comments

Comments
 (0)