]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
64088b3af1877f1fbf88d4d6826fb805c265cd90
[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, n->name, config)) {
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                 config_free(config);
50                 return false;
51         }
52
53         const char *name;
54         uint32_t len = packmsg_get_str_raw(in, &name);
55
56         if(len != strlen(n->name) || strncmp(name, n->name, len)) {
57                 config_free(config);
58                 return false;
59         }
60
61         return true;
62 }
63
64 /// Read device class, blacklist status and submesh from a host config file. Used at startup when reading all host config files.
65 bool node_read_partial(meshlink_handle_t *mesh, node_t *n) {
66         config_t config;
67         packmsg_input_t in;
68
69         if(!node_get_config(mesh, n, &config, &in)) {
70                 return false;
71         }
72
73         char *submesh_name = packmsg_get_str_dup(&in);
74
75         if(!strcmp(submesh_name, CORE_MESH)) {
76                 free(submesh_name);
77                 n->submesh = NULL;
78         } else {
79                 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
80                 free(submesh_name);
81
82                 if(!n->submesh) {
83                         config_free(&config);
84                         return false;
85                 }
86         }
87
88         int32_t devclass = packmsg_get_int32(&in);
89         bool blacklisted = packmsg_get_bool(&in);
90         config_free(&config);
91
92         if(!packmsg_input_ok(&in) || devclass < 0 || devclass > _DEV_CLASS_MAX) {
93                 return false;
94         }
95
96         n->devclass = devclass;
97         n->status.blacklisted = blacklisted;
98         return true;
99 }
100
101 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
102 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
103         if(ecdsa_active(n->ecdsa)) {
104                 return true;
105         }
106
107         config_t config;
108         packmsg_input_t in;
109
110         if(!node_get_config(mesh, n, &config, &in)) {
111                 return false;
112         }
113
114         packmsg_skip_element(&in); /* submesh */
115         packmsg_get_int32(&in); /* devclass */
116         packmsg_get_bool(&in); /* blacklisted */
117
118         const void *key;
119         uint32_t len = packmsg_get_bin_raw(&in, &key);
120
121         if(len != 32) {
122                 config_free(&config);
123                 return false;
124         }
125
126         n->ecdsa = ecdsa_set_public_key(key);
127
128         // While we are at it, read known address information
129         if(!n->canonical_address) {
130                 n->canonical_address = packmsg_get_str_dup(&in);
131         } else {
132                 packmsg_skip_element(&in);
133         }
134
135         // Append any known addresses in the config file to the list we currently have
136         uint32_t known_count = 0;
137
138         for(uint32_t i = 0; i < 5; i++) {
139                 if(n->recent[i].sa.sa_family) {
140                         known_count++;
141                 }
142         }
143
144         uint32_t count = packmsg_get_array(&in);
145
146         if(count > 5 - known_count) {
147                 count = 5 - known_count;
148         }
149
150         for(uint32_t i = 0; i < count; i++) {
151                 n->recent[i + known_count] = packmsg_get_sockaddr(&in);
152         }
153
154
155         config_free(&config);
156         return true;
157 }
158
159 /// Fill in node details from a config blob.
160 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
161         if(n->canonical_address) {
162                 return true;
163         }
164
165         packmsg_input_t in = {config->buf, config->len};
166         uint32_t version = packmsg_get_uint32(&in);
167
168         if(version != MESHLINK_CONFIG_VERSION) {
169                 return false;
170         }
171
172         char *name = packmsg_get_str_dup(&in);
173
174         if(!name) {
175                 return false;
176         }
177
178         if(n->name) {
179                 if(strcmp(n->name, name)) {
180                         free(name);
181                         return false;
182                 }
183
184                 free(name);
185         } else {
186                 n->name = name;
187         }
188
189         char *submesh_name = packmsg_get_str_dup(&in);
190
191         if(!strcmp(submesh_name, CORE_MESH)) {
192                 free(submesh_name);
193                 n->submesh = NULL;
194         } else {
195                 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
196                 free(submesh_name);
197
198                 if(!n->submesh) {
199                         return false;
200                 }
201         }
202
203         n->devclass = packmsg_get_int32(&in);
204         n->status.blacklisted = packmsg_get_bool(&in);
205         const void *key;
206         uint32_t len = packmsg_get_bin_raw(&in, &key);
207
208         if(len != 32) {
209                 return false;
210         }
211
212         if(!ecdsa_active(n->ecdsa)) {
213                 n->ecdsa = ecdsa_set_public_key(key);
214         }
215
216         n->canonical_address = packmsg_get_str_dup(&in);
217         uint32_t count = packmsg_get_array(&in);
218
219         if(count > 5) {
220                 count = 5;
221         }
222
223         for(uint32_t i = 0; i < count; i++) {
224                 n->recent[i] = packmsg_get_sockaddr(&in);
225         }
226
227         return packmsg_done(&in);
228 }
229
230 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
231         uint8_t buf[4096];
232         packmsg_output_t out = {buf, sizeof(buf)};
233
234         packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
235         packmsg_add_str(&out, n->name);
236         packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
237         packmsg_add_int32(&out, n->devclass);
238         packmsg_add_bool(&out, n->status.blacklisted);
239
240         if(ecdsa_active(n->ecdsa)) {
241                 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
242         } else {
243                 packmsg_add_bin(&out, "", 0);
244         }
245
246         packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
247
248         uint32_t count = 0;
249
250         for(uint32_t i = 0; i < 5; i++) {
251                 if(n->recent[i].sa.sa_family) {
252                         count++;
253                 } else {
254                         break;
255                 }
256         }
257
258         packmsg_add_array(&out, count);
259
260         for(uint32_t i = 0; i < count; i++) {
261                 packmsg_add_sockaddr(&out, &n->recent[i]);
262         }
263
264         if(!packmsg_output_ok(&out)) {
265                 return false;
266         }
267
268         config_t config = {buf, packmsg_output_size(&out, buf)};
269         return config_write(mesh, n->name, &config);
270 }
271
272 static void load_node(meshlink_handle_t *mesh, const char *name) {
273         if(!check_id(name)) {
274                 return;
275         }
276
277         node_t *n = lookup_node(mesh, name);
278
279         if(n) {
280                 return;
281         }
282
283         n = new_node();
284         n->name = xstrdup(name);
285
286         if(!node_read_partial(mesh, n)) {
287                 free_node(n);
288                 return;
289         }
290
291         node_add(mesh, n);
292 }
293
294 /*
295   Add listening sockets.
296 */
297 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
298         char *port = mesh->myport;
299
300         if(address) {
301                 char *space = strchr(address, ' ');
302
303                 if(space) {
304                         *space++ = 0;
305                         port = space;
306                 }
307
308                 if(!strcmp(address, "*")) {
309                         *address = 0;
310                 }
311         }
312
313         struct addrinfo *ai;
314
315         struct addrinfo hint = {
316                 .ai_family = addressfamily,
317                 .ai_socktype = SOCK_STREAM,
318                 .ai_protocol = IPPROTO_TCP,
319                 .ai_flags = AI_PASSIVE,
320         };
321
322         int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
323
324         free(address);
325
326         if(err || !ai) {
327                 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
328                 return false;
329         }
330
331         bool success = false;
332
333         for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
334                 // Ignore duplicate addresses
335                 bool found = false;
336
337                 for(int i = 0; i < mesh->listen_sockets; i++)
338                         if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
339                                 found = true;
340                                 break;
341                         }
342
343                 if(found) {
344                         continue;
345                 }
346
347                 if(mesh->listen_sockets >= MAXSOCKETS) {
348                         logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
349                         return false;
350                 }
351
352                 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
353
354                 if(tcp_fd < 0) {
355                         continue;
356                 }
357
358                 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
359
360                 if(udp_fd < 0) {
361                         close(tcp_fd);
362                         continue;
363                 }
364
365                 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);
366                 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);
367
368                 if(mesh->log_level >= MESHLINK_INFO) {
369                         char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
370                         logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
371                         free(hostname);
372                 }
373
374                 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
375                 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
376                 mesh->listen_sockets++;
377                 success = true;
378         }
379
380         freeaddrinfo(ai);
381         return success;
382 }
383
384 /*
385   Configure node_t mesh->self and set up the local sockets (listen only)
386 */
387 bool setup_myself(meshlink_handle_t *mesh) {
388         /* Set some defaults */
389
390         mesh->localdiscovery = true;
391         keylifetime = 3600; // TODO: check if this can be removed as well
392         mesh->maxtimeout = 900;
393         mesh->self->options |= OPTION_PMTU_DISCOVERY;
394
395         /* Done */
396
397         mesh->self->nexthop = mesh->self;
398         mesh->self->via = mesh->self;
399         mesh->self->status.reachable = true;
400         mesh->self->last_state_change = mesh->loop.now.tv_sec;
401
402         node_add(mesh, mesh->self);
403
404         graph(mesh);
405
406         config_scan_all(mesh, load_node);
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         mesh->pinginterval = 60;
453         mesh->pingtimeout = 5;
454         maxoutbufsize = 10 * MTU;
455
456         if(!setup_myself(mesh)) {
457                 return false;
458         }
459
460         return true;
461 }
462
463 /*
464   close all open network connections
465 */
466 void close_network_connections(meshlink_handle_t *mesh) {
467         if(mesh->connections) {
468                 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
469                         next = node->next;
470                         connection_t *c = node->data;
471                         c->outgoing = NULL;
472                         terminate_connection(mesh, c, false);
473                 }
474         }
475
476         for(int i = 0; i < mesh->listen_sockets; i++) {
477                 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
478                 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
479                 close(mesh->listen_socket[i].tcp.fd);
480                 close(mesh->listen_socket[i].udp.fd);
481         }
482
483         exit_requests(mesh);
484         exit_edges(mesh);
485         exit_nodes(mesh);
486         exit_submeshes(mesh);
487         exit_connections(mesh);
488
489         free(mesh->myport);
490         mesh->myport = NULL;
491
492         mesh->self = NULL;
493
494         return;
495 }