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