]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Really stop using cipher.h and digest.h.
[meshlink] / src / net_setup.c
1 /*
2     net_setup.c -- Setup.
3     Copyright (C) 2014 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 "protocol.h"
31 #include "route.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 int autoconnect = 3;
36
37 bool node_read_ecdsa_public_key(node_t *n) {
38         if(ecdsa_active(n->ecdsa))
39                 return true;
40
41         splay_tree_t *config_tree;
42         char *p;
43
44         init_configuration(&config_tree);
45         if(!read_host_config(config_tree, n->name))
46                 goto exit;
47
48         /* First, check for simple ECDSAPublicKey statement */
49
50         if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
51                 n->ecdsa = ecdsa_set_base64_public_key(p);
52                 free(p);
53         }
54
55 exit:
56         exit_configuration(&config_tree);
57         return n->ecdsa;
58 }
59
60 bool read_ecdsa_public_key(connection_t *c) {
61         if(ecdsa_active(c->ecdsa))
62                 return true;
63
64         char *p;
65
66         if(!c->config_tree) {
67                 init_configuration(&c->config_tree);
68                 if(!read_host_config(c->config_tree, c->name))
69                         return false;
70         }
71
72         /* First, check for simple ECDSAPublicKey statement */
73
74         if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
75                 c->ecdsa = ecdsa_set_base64_public_key(p);
76                 free(p);
77                 return c->ecdsa;
78         }
79
80         return false;
81 }
82
83 static bool read_ecdsa_private_key(void) {
84         FILE *fp;
85         char *fname;
86
87         xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
88         fp = fopen(fname, "r");
89         free(fname);
90
91         if(!fp) {
92                 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
93                 return false;
94         }
95
96         mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
97         fclose(fp);
98
99         if(!mesh->self->connection->ecdsa)
100                 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
101
102         return mesh->self->connection->ecdsa;
103 }
104
105 static bool read_invitation_key(void) {
106         FILE *fp;
107         char *fname;
108
109         if(mesh->invitation_key) {
110                 ecdsa_free(mesh->invitation_key);
111                 mesh->invitation_key = NULL;
112         }
113
114         xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
115
116         fp = fopen(fname, "r");
117
118         if(fp) {
119                 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
120                 fclose(fp);
121                 if(!mesh->invitation_key)
122                         logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
123         }
124
125         free(fname);
126         return mesh->invitation_key;
127 }
128
129 void load_all_nodes(void) {
130         DIR *dir;
131         struct dirent *ent;
132         char *dname;
133
134         xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase);
135         dir = opendir(dname);
136         if(!dir) {
137                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
138                 free(dname);
139                 return;
140         }
141
142         while((ent = readdir(dir))) {
143                 if(!check_id(ent->d_name))
144                         continue;
145
146                 node_t *n = lookup_node(ent->d_name);
147                 if(n)
148                         continue;
149
150                 n = new_node();
151                 n->name = xstrdup(ent->d_name);
152                 node_add(n);
153         }
154
155         closedir(dir);
156 }
157
158
159 char *get_name(void) {
160         char *name = NULL;
161
162         get_config_string(lookup_config(mesh->config, "Name"), &name);
163
164         if(!name)
165                 return NULL;
166
167         if(!check_id(name)) {
168                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
169                 free(name);
170                 return NULL;
171         }
172
173         return name;
174 }
175
176 bool setup_myself_reloadable(void) {
177         mesh->localdiscovery = true;
178         keylifetime = 3600; // TODO: check if this can be removed as well
179         maxtimeout = 900;
180         autoconnect = 3;
181         mesh->self->options |= OPTION_PMTU_DISCOVERY;
182
183         read_invitation_key();
184
185         return true;
186 }
187
188 /*
189   Add listening sockets.
190 */
191 static bool add_listen_address(char *address, bool bindto) {
192         char *port = mesh->myport;
193
194         if(address) {
195                 char *space = strchr(address, ' ');
196                 if(space) {
197                         *space++ = 0;
198                         port = space;
199                 }
200
201                 if(!strcmp(address, "*"))
202                         *address = 0;
203         }
204
205         struct addrinfo *ai, hint = {0};
206         hint.ai_family = addressfamily;
207         hint.ai_socktype = SOCK_STREAM;
208         hint.ai_protocol = IPPROTO_TCP;
209         hint.ai_flags = AI_PASSIVE;
210
211         int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
212         free(address);
213
214         if(err || !ai) {
215                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
216                 return false;
217         }
218
219         for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
220                 // Ignore duplicate addresses
221                 bool found = false;
222
223                 for(int i = 0; i < mesh->listen_sockets; i++)
224                         if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
225                                 found = true;
226                                 break;
227                         }
228
229                 if(found)
230                         continue;
231
232                 if(mesh->listen_sockets >= MAXSOCKETS) {
233                         logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
234                         return false;
235                 }
236
237                 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
238
239                 if(tcp_fd < 0)
240                         continue;
241
242                 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
243
244                 if(tcp_fd < 0) {
245                         close(tcp_fd);
246                         continue;
247                 }
248
249                 io_add(&mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
250                 io_add(&mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
251
252                 if(debug_level >= DEBUG_CONNECTIONS) {
253                         char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
254                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
255                         free(hostname);
256                 }
257
258                 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
259                 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
260                 mesh->listen_sockets++;
261         }
262
263         freeaddrinfo(ai);
264         return true;
265 }
266
267 /*
268   Configure node_t mesh->self and set up the local sockets (listen only)
269 */
270 bool setup_myself(void) {
271         char *name, *hostname, *cipher, *digest, *type;
272         char *address = NULL;
273         bool port_specified = false;
274
275         if(!(name = get_name())) {
276                 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
277                 return false;
278         }
279
280         mesh->self = new_node();
281         mesh->self->connection = new_connection();
282         mesh->self->name = name;
283         mesh->self->connection->name = xstrdup(name);
284         read_host_config(mesh->config, name);
285
286         if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
287                 mesh->myport = xstrdup("655");
288         else
289                 port_specified = true;
290
291         mesh->self->connection->options = 0;
292         mesh->self->connection->protocol_major = PROT_MAJOR;
293         mesh->self->connection->protocol_minor = PROT_MINOR;
294
295         mesh->self->options |= PROT_MINOR << 24;
296
297         if(!read_ecdsa_private_key())
298                 return false;
299
300         /* Ensure mesh->myport is numeric */
301
302         if(!atoi(mesh->myport)) {
303                 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
304                 sockaddr_t sa;
305                 if(!ai || !ai->ai_addr)
306                         return false;
307                 free(mesh->myport);
308                 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
309                 sockaddr2str(&sa, NULL, &mesh->myport);
310         }
311
312         /* Check some options */
313
314         if(!setup_myself_reloadable())
315                 return false;
316
317         /* Compression */
318
319         // TODO: drop compression in the packet layer?
320         mesh->self->incompression = 0;
321         mesh->self->connection->outcompression = 0;
322
323         /* Done */
324
325         mesh->self->nexthop = mesh->self;
326         mesh->self->via = mesh->self;
327         mesh->self->status.reachable = true;
328         mesh->self->last_state_change = now.tv_sec;
329         node_add(mesh->self);
330
331         graph();
332
333         if(autoconnect)
334                 load_all_nodes();
335
336         /* Open sockets */
337
338         mesh->listen_sockets = 0;
339         int cfgs = 0;
340
341         if(!add_listen_address(address, NULL))
342                 return false;
343
344         if(!mesh->listen_sockets) {
345                 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
346                 return false;
347         }
348
349         // TODO: require Port to be set? Or use "0" and use getsockname()?
350
351         if(!mesh->myport)
352                 mesh->myport = xstrdup("655");
353
354         xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
355         mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
356
357         /* Done. */
358
359         mesh->last_config_check = now.tv_sec;
360
361         return true;
362 }
363
364 /*
365   initialize network
366 */
367 bool setup_network(void) {
368         init_connections();
369         init_nodes();
370         init_edges();
371         init_requests();
372
373         pinginterval = 60;
374         pingtimeout = 5;
375         maxoutbufsize = 10 * MTU;
376
377         if(!setup_myself())
378                 return false;
379
380         return true;
381 }
382
383 /*
384   close all open network connections
385 */
386 void close_network_connections(void) {
387         for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
388                 next = node->next;
389                 connection_t *c = node->data;
390                 c->outgoing = NULL;
391                 terminate_connection(c, false);
392         }
393
394         if(mesh->outgoings)
395                 list_delete_list(mesh->outgoings);
396
397         if(mesh->self && mesh->self->connection) {
398                 terminate_connection(mesh->self->connection, false);
399                 free_connection(mesh->self->connection);
400         }
401
402         for(int i = 0; i < mesh->listen_sockets; i++) {
403                 io_del(&mesh->listen_socket[i].tcp);
404                 io_del(&mesh->listen_socket[i].udp);
405                 close(mesh->listen_socket[i].tcp.fd);
406                 close(mesh->listen_socket[i].udp.fd);
407         }
408
409         exit_requests();
410         exit_edges();
411         exit_nodes();
412         exit_connections();
413
414         if(mesh->myport) free(mesh->myport);
415
416         return;
417 }