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