]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Correctly handle incoming retransmissions of SYN packets.
[meshlink] / src / net_setup.c
1 /*
2     net_setup.c -- Setup.
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "connection.h"
24 #include "ecdsa.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "packmsg.h"
31 #include "protocol.h"
32 #include "route.h"
33 #include "utils.h"
34 #include "xalloc.h"
35 #include "submesh.h"
36
37 /// Helper function to start parsing a host config file
38 static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config, packmsg_input_t *in) {
39         if(!config_read(mesh, "current", n->name, config, mesh->config_key)) {
40                 return false;
41         }
42
43         in->ptr = config->buf;
44         in->len = config->len;
45
46         uint32_t version = packmsg_get_uint32(in);
47
48         if(version != MESHLINK_CONFIG_VERSION) {
49                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
50                 config_free(config);
51                 return false;
52         }
53
54         const char *name;
55         uint32_t len = packmsg_get_str_raw(in, &name);
56
57         if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) {
58                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
59                 config_free(config);
60                 return false;
61         }
62
63         return true;
64 }
65
66 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
67 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
68         if(ecdsa_active(n->ecdsa)) {
69                 return true;
70         }
71
72         config_t config;
73         packmsg_input_t in;
74
75         if(!node_get_config(mesh, n, &config, &in)) {
76                 return false;
77         }
78
79         packmsg_skip_element(&in); /* submesh */
80         packmsg_get_int32(&in); /* devclass */
81         packmsg_get_bool(&in); /* blacklisted */
82
83         const void *key;
84         uint32_t len = packmsg_get_bin_raw(&in, &key);
85
86         if(len != 32) {
87                 config_free(&config);
88                 return false;
89         }
90
91         n->ecdsa = ecdsa_set_public_key(key);
92
93         // While we are at it, read known address information
94         if(!n->canonical_address) {
95                 n->canonical_address = packmsg_get_str_dup(&in);
96         } else {
97                 packmsg_skip_element(&in);
98         }
99
100         // Append any known addresses in the config file to the list we currently have
101         uint32_t known_count = 0;
102
103         for(uint32_t i = 0; i < 5; i++) {
104                 if(n->recent[i].sa.sa_family) {
105                         known_count++;
106                 }
107         }
108
109         uint32_t count = packmsg_get_array(&in);
110
111         if(count > 5 - known_count) {
112                 count = 5 - known_count;
113         }
114
115         for(uint32_t i = 0; i < count; i++) {
116                 n->recent[i + known_count] = packmsg_get_sockaddr(&in);
117         }
118
119
120         config_free(&config);
121         return true;
122 }
123
124 /// Fill in node details from a config blob.
125 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
126         if(n->canonical_address) {
127                 return true;
128         }
129
130         packmsg_input_t in = {config->buf, config->len};
131         uint32_t version = packmsg_get_uint32(&in);
132
133         if(version != MESHLINK_CONFIG_VERSION) {
134                 return false;
135         }
136
137         char *name = packmsg_get_str_dup(&in);
138
139         if(!name) {
140                 return false;
141         }
142
143         if(n->name) {
144                 if(strcmp(n->name, name)) {
145                         free(name);
146                         return false;
147                 }
148
149                 free(name);
150         } else {
151                 n->name = name;
152         }
153
154         char *submesh_name = packmsg_get_str_dup(&in);
155
156         if(!strcmp(submesh_name, CORE_MESH)) {
157                 free(submesh_name);
158                 n->submesh = NULL;
159         } else {
160                 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
161                 free(submesh_name);
162
163                 if(!n->submesh) {
164                         return false;
165                 }
166         }
167
168         n->devclass = packmsg_get_int32(&in);
169         n->status.blacklisted = packmsg_get_bool(&in);
170         const void *key;
171         uint32_t len = packmsg_get_bin_raw(&in, &key);
172
173         if(len != 32) {
174                 return false;
175         }
176
177         if(!ecdsa_active(n->ecdsa)) {
178                 n->ecdsa = ecdsa_set_public_key(key);
179         }
180
181         n->canonical_address = packmsg_get_str_dup(&in);
182         uint32_t count = packmsg_get_array(&in);
183
184         if(count > 5) {
185                 count = 5;
186         }
187
188         for(uint32_t i = 0; i < count; i++) {
189                 n->recent[i] = packmsg_get_sockaddr(&in);
190         }
191
192         return packmsg_done(&in);
193 }
194
195 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
196         if(!mesh->confbase) {
197                 return true;
198         }
199
200         uint8_t buf[4096];
201         packmsg_output_t out = {buf, sizeof(buf)};
202
203         packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
204         packmsg_add_str(&out, n->name);
205         packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
206         packmsg_add_int32(&out, n->devclass);
207         packmsg_add_bool(&out, n->status.blacklisted);
208
209         if(ecdsa_active(n->ecdsa)) {
210                 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
211         } else {
212                 packmsg_add_bin(&out, "", 0);
213         }
214
215         packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
216
217         uint32_t count = 0;
218
219         for(uint32_t i = 0; i < 5; i++) {
220                 if(n->recent[i].sa.sa_family) {
221                         count++;
222                 } else {
223                         break;
224                 }
225         }
226
227         packmsg_add_array(&out, count);
228
229         for(uint32_t i = 0; i < count; i++) {
230                 packmsg_add_sockaddr(&out, &n->recent[i]);
231         }
232
233         if(!packmsg_output_ok(&out)) {
234                 return false;
235         }
236
237         config_t config = {buf, packmsg_output_size(&out, buf)};
238
239         if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
240                 call_error_cb(mesh, MESHLINK_ESTORAGE);
241                 return false;
242         }
243
244         return true;
245 }
246
247 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
248         (void)priv;
249
250         if(!check_id(name)) {
251                 return true;
252         }
253
254         node_t *n = lookup_node(mesh, name);
255
256         if(n) {
257                 return true;
258         }
259
260         n = new_node();
261         n->name = xstrdup(name);
262
263         config_t config;
264         packmsg_input_t in;
265
266         if(!node_get_config(mesh, n, &config, &in)) {
267                 free_node(n);
268                 return false;
269         }
270
271         if(!node_read_from_config(mesh, n, &config)) {
272                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
273                 config_free(&config);
274                 free_node(n);
275                 return false;
276         }
277
278         config_free(&config);
279
280         node_add(mesh, n);
281
282         return true;
283 }
284
285 /*
286   Add listening sockets.
287 */
288 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
289         char *port = mesh->myport;
290
291         if(address) {
292                 char *space = strchr(address, ' ');
293
294                 if(space) {
295                         *space++ = 0;
296                         port = space;
297                 }
298
299                 if(!strcmp(address, "*")) {
300                         *address = 0;
301                 }
302         }
303
304         struct addrinfo *ai;
305
306         struct addrinfo hint = {
307                 .ai_family = AF_UNSPEC,
308                 .ai_socktype = SOCK_STREAM,
309                 .ai_protocol = IPPROTO_TCP,
310                 .ai_flags = AI_PASSIVE,
311         };
312
313         int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
314
315         free(address);
316
317         if(err || !ai) {
318                 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
319                 return false;
320         }
321
322         bool success = false;
323
324         for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
325                 // Ignore duplicate addresses
326                 bool found = false;
327
328                 for(int i = 0; i < mesh->listen_sockets; i++)
329                         if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
330                                 found = true;
331                                 break;
332                         }
333
334                 if(found) {
335                         continue;
336                 }
337
338                 if(mesh->listen_sockets >= MAXSOCKETS) {
339                         logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
340                         return false;
341                 }
342
343                 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
344
345                 if(tcp_fd < 0) {
346                         continue;
347                 }
348
349                 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
350
351                 if(udp_fd < 0) {
352                         close(tcp_fd);
353                         continue;
354                 }
355
356                 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);
357                 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);
358
359                 if(mesh->log_level >= MESHLINK_INFO) {
360                         char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
361                         logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
362                         free(hostname);
363                 }
364
365                 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
366                 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
367                 memcpy(&mesh->listen_socket[mesh->listen_sockets].broadcast_sa, aip->ai_addr, aip->ai_addrlen);
368
369                 if(aip->ai_family == AF_INET6) {
370                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x0] = 0xff;
371                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x1] = 0x02;
372                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0xf] = 0x01;
373                 } else {
374                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in.sin_addr.s_addr = 0xffffffff;
375                 }
376
377                 mesh->listen_sockets++;
378                 success = true;
379         }
380
381         freeaddrinfo(ai);
382         return success;
383 }
384
385 /*
386   Configure node_t mesh->self and set up the local sockets (listen only)
387 */
388 bool setup_myself(meshlink_handle_t *mesh) {
389         /* Set some defaults */
390
391         mesh->maxtimeout = 900;
392
393         /* Done */
394
395         mesh->self->nexthop = mesh->self;
396         mesh->self->status.reachable = true;
397         mesh->self->last_state_change = mesh->loop.now.tv_sec;
398
399         node_add(mesh, mesh->self);
400
401         graph(mesh);
402
403         if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
404                 meshlink_errno = MESHLINK_ESTORAGE;
405                 return false;
406         }
407
408         /* Open sockets */
409
410         mesh->listen_sockets = 0;
411
412         if(!add_listen_address(mesh, NULL, NULL)) {
413                 if(strcmp(mesh->myport, "0")) {
414                         logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
415                         free(mesh->myport);
416                         mesh->myport = strdup("0");
417
418                         if(!mesh->myport) {
419                                 return false;
420                         }
421
422                         if(!add_listen_address(mesh, NULL, NULL)) {
423                                 return false;
424                         }
425                 } else {
426                         return false;
427                 }
428         }
429
430         if(!mesh->listen_sockets) {
431                 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
432                 return false;
433         }
434
435         /* Done. */
436
437         mesh->last_config_check = mesh->loop.now.tv_sec;
438
439         return true;
440 }
441
442 /*
443   initialize network
444 */
445 bool setup_network(meshlink_handle_t *mesh) {
446         init_connections(mesh);
447         init_submeshes(mesh);
448         init_nodes(mesh);
449         init_edges(mesh);
450         init_requests(mesh);
451
452         if(!setup_myself(mesh)) {
453                 return false;
454         }
455
456         return true;
457 }
458
459 /*
460   close all open network connections
461 */
462 void close_network_connections(meshlink_handle_t *mesh) {
463         if(mesh->connections) {
464                 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
465                         next = node->next;
466                         connection_t *c = node->data;
467                         c->outgoing = NULL;
468                         terminate_connection(mesh, c, false);
469                 }
470         }
471
472         for(int i = 0; i < mesh->listen_sockets; i++) {
473                 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
474                 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
475                 close(mesh->listen_socket[i].tcp.fd);
476                 close(mesh->listen_socket[i].udp.fd);
477         }
478
479         exit_requests(mesh);
480         exit_edges(mesh);
481         exit_nodes(mesh);
482         exit_submeshes(mesh);
483         exit_connections(mesh);
484
485         free(mesh->myport);
486         mesh->myport = NULL;
487
488         mesh->self = NULL;
489
490         return;
491 }