3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "connection.h"
27 #include "meshlink_internal.h"
36 bool node_read_ecdsa_public_key(meshlink_handle_t *mesh, node_t *n) {
37 if(ecdsa_active(n->ecdsa)) {
41 splay_tree_t *config_tree;
44 init_configuration(&config_tree);
46 if(!read_host_config(mesh, config_tree, n->name)) {
50 /* First, check for simple ECDSAPublicKey statement */
52 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
53 n->ecdsa = ecdsa_set_base64_public_key(p);
58 exit_configuration(&config_tree);
62 bool read_ecdsa_public_key(meshlink_handle_t *mesh, connection_t *c) {
63 if(ecdsa_active(c->ecdsa)) {
70 init_configuration(&c->config_tree);
72 if(!read_host_config(mesh, c->config_tree, c->name)) {
77 /* First, check for simple ECDSAPublicKey statement */
79 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
80 c->ecdsa = ecdsa_set_base64_public_key(p);
88 bool read_ecdsa_private_key(meshlink_handle_t *mesh) {
90 char filename[PATH_MAX];
92 snprintf(filename, PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
93 fp = fopen(filename, "rb");
96 logger(mesh, MESHLINK_ERROR, "Error reading ECDSA private key file: %s", strerror(errno));
100 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
103 if(!mesh->self->connection->ecdsa) {
104 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file failed: %s", strerror(errno));
107 return mesh->self->connection->ecdsa;
110 static bool read_invitation_key(meshlink_handle_t *mesh) {
112 char filename[PATH_MAX];
114 if(mesh->invitation_key) {
115 ecdsa_free(mesh->invitation_key);
116 mesh->invitation_key = NULL;
119 snprintf(filename, PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
121 fp = fopen(filename, "rb");
124 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
127 if(!mesh->invitation_key) {
128 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno));
132 return mesh->invitation_key;
135 bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) {
137 splay_tree_t *config_tree;
140 init_configuration(&config_tree);
142 if(!read_host_config(mesh, config_tree, n->name)) {
146 if(get_config_string(lookup_config(config_tree, "DeviceClass"), &p)) {
147 n->devclass = atoi(p);
151 if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
152 n->devclass = _DEV_CLASS_MAX;
156 exit_configuration(&config_tree);
157 return n->devclass != 0;
160 bool node_read_submesh(meshlink_handle_t *mesh, node_t *n) {
162 splay_tree_t *config_tree;
165 init_configuration(&config_tree);
167 if(!read_host_config(mesh, config_tree, n->name)) {
171 if(get_config_string(lookup_config(config_tree, "SubMesh"), &p)) {
174 for list_each(submesh_t, s, mesh->submeshes) {
175 if(!strcmp(p, s->name)) {
182 n->submesh = (submesh_t *)meshlink_submesh_open(mesh, p);
189 exit_configuration(&config_tree);
190 return n->submesh != NULL;
193 bool node_read_blacklist_status(meshlink_handle_t *mesh, node_t *n) {
195 splay_tree_t *config_tree;
196 bool blacklist_status;
198 init_configuration(&config_tree);
200 if(!read_host_config(mesh, config_tree, n->name)) {
204 if(get_config_bool(lookup_config(config_tree, "blacklisted"), &blacklist_status)) {
205 n->status.blacklisted = blacklist_status;
209 exit_configuration(&config_tree);
210 return n->status.blacklisted;
213 bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) {
215 if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
221 splay_tree_t *config_tree;
222 init_configuration(&config_tree);
224 // ignore read errors; in case the file does not exist we will create it
225 read_host_config(mesh, config_tree, n->name);
227 config_t *cnf = lookup_config(config_tree, "DeviceClass");
231 cnf->variable = xstrdup("DeviceClass");
232 config_add(config_tree, cnf);
235 set_config_int(cnf, n->devclass);
237 if(!write_host_config(mesh, config_tree, n->name)) {
244 exit_configuration(&config_tree);
248 bool node_write_submesh(meshlink_handle_t *mesh, node_t *n) {
256 splay_tree_t *config_tree;
257 init_configuration(&config_tree);
259 // ignore read errors; in case the file does not exist we will create it
260 read_host_config(mesh, config_tree, n->name);
262 config_t *cnf = lookup_config(config_tree, "SubMesh");
266 cnf->variable = xstrdup("SubMesh");
267 config_add(config_tree, cnf);
270 set_config_string(cnf, n->submesh->name);
272 if(!write_host_config(mesh, config_tree, n->name)) {
279 exit_configuration(&config_tree);
283 void load_all_nodes(meshlink_handle_t *mesh) {
286 char dname[PATH_MAX];
288 snprintf(dname, PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
289 dir = opendir(dname);
292 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
296 while((ent = readdir(dir))) {
297 if(!check_id(ent->d_name)) {
301 node_t *n = lookup_node(mesh, ent->d_name);
304 if(n == mesh->self && !n->submesh) {
305 node_read_submesh(mesh, n);
312 n->name = xstrdup(ent->d_name);
313 node_read_devclass(mesh, n);
314 node_read_submesh(mesh, n);
315 node_read_blacklist_status(mesh, n);
323 char *get_name(meshlink_handle_t *mesh) {
326 get_config_string(lookup_config(mesh->config, "Name"), &name);
332 if(!check_id(name)) {
333 logger(mesh, MESHLINK_ERROR, "Invalid name for mesh->self!");
341 bool setup_myself_reloadable(meshlink_handle_t *mesh) {
342 mesh->localdiscovery = true;
343 keylifetime = 3600; // TODO: check if this can be removed as well
344 mesh->maxtimeout = 900;
345 mesh->self->options |= OPTION_PMTU_DISCOVERY;
347 read_invitation_key(mesh);
353 Add listening sockets.
355 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
356 char *port = mesh->myport;
359 char *space = strchr(address, ' ');
366 if(!strcmp(address, "*")) {
373 struct addrinfo hint = {
374 .ai_family = addressfamily,
375 .ai_socktype = SOCK_STREAM,
376 .ai_protocol = IPPROTO_TCP,
377 .ai_flags = AI_PASSIVE,
380 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
385 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
389 bool success = false;
391 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
392 // Ignore duplicate addresses
395 for(int i = 0; i < mesh->listen_sockets; i++)
396 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
405 if(mesh->listen_sockets >= MAXSOCKETS) {
406 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
410 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
416 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
423 io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
424 io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
426 if(mesh->log_level >= MESHLINK_INFO) {
427 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
428 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
432 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
433 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
434 mesh->listen_sockets++;
443 Configure node_t mesh->self and set up the local sockets (listen only)
445 bool setup_myself(meshlink_handle_t *mesh) {
447 char *address = NULL;
449 if(!(name = get_name(mesh))) {
450 logger(mesh, MESHLINK_ERROR, "Name for MeshLink instance required!");
454 mesh->self = new_node();
455 mesh->self->connection = new_connection();
456 mesh->self->name = name;
457 mesh->self->devclass = mesh->devclass;
458 mesh->self->connection->name = xstrdup(name);
459 read_host_config(mesh, mesh->config, name);
461 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) {
462 int port = check_port(mesh);
468 xasprintf(&mesh->myport, "%d", port);
471 mesh->self->connection->options = 0;
472 mesh->self->connection->protocol_major = PROT_MAJOR;
473 mesh->self->connection->protocol_minor = PROT_MINOR;
475 mesh->self->options |= PROT_MINOR << 24;
477 if(!read_ecdsa_private_key(mesh)) {
481 /* Ensure mesh->myport is numeric */
483 if(!atoi(mesh->myport)) {
484 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
487 if(!ai || !ai->ai_addr) {
492 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
493 sockaddr2str(&sa, NULL, &mesh->myport);
496 /* Check some options */
498 if(!setup_myself_reloadable(mesh)) {
504 // TODO: drop compression in the packet layer?
505 mesh->self->incompression = 0;
506 mesh->self->connection->outcompression = 0;
510 mesh->self->nexthop = mesh->self;
511 mesh->self->via = mesh->self;
512 mesh->self->status.reachable = true;
513 mesh->self->last_state_change = mesh->loop.now.tv_sec;
515 node_write_devclass(mesh, mesh->self);
516 node_add(mesh, mesh->self);
520 load_all_nodes(mesh);
524 mesh->listen_sockets = 0;
526 if(!add_listen_address(mesh, address, NULL)) {
527 if(strcmp(mesh->myport, "0")) {
528 logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
530 mesh->myport = strdup("0");
536 if(!add_listen_address(mesh, address, NULL)) {
544 if(!mesh->listen_sockets) {
545 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
551 mesh->last_config_check = mesh->loop.now.tv_sec;
559 bool setup_network(meshlink_handle_t *mesh) {
560 init_connections(mesh);
561 init_submeshes(mesh);
566 mesh->pinginterval = 60;
567 mesh->pingtimeout = 5;
568 maxoutbufsize = 10 * MTU;
570 if(!setup_myself(mesh)) {
578 close all open network connections
580 void close_network_connections(meshlink_handle_t *mesh) {
581 if(mesh->connections) {
582 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
584 connection_t *c = node->data;
586 terminate_connection(mesh, c, false);
590 if(mesh->outgoings) {
591 list_delete_list(mesh->outgoings);
594 if(mesh->self && mesh->self->connection) {
595 terminate_connection(mesh, mesh->self->connection, false);
596 free_connection(mesh->self->connection);
599 for(int i = 0; i < mesh->listen_sockets; i++) {
600 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
601 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
602 close(mesh->listen_socket[i].tcp.fd);
603 close(mesh->listen_socket[i].udp.fd);
609 exit_connections(mesh);