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