]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Remove most of the configuration options.
[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 "net.h"
30 #include "netutl.h"
31 #include "protocol.h"
32 #include "route.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 char *myport;
37
38 char *proxyhost;
39 char *proxyport;
40 char *proxyuser;
41 char *proxypass;
42 proxytype_t proxytype;
43 int autoconnect;
44 bool disablebuggypeers;
45
46 bool node_read_ecdsa_public_key(node_t *n) {
47         if(ecdsa_active(n->ecdsa))
48                 return true;
49
50         splay_tree_t *config_tree;
51         char *p;
52
53         init_configuration(&config_tree);
54         if(!read_host_config(config_tree, n->name))
55                 goto exit;
56
57         /* First, check for simple ECDSAPublicKey statement */
58
59         if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
60                 n->ecdsa = ecdsa_set_base64_public_key(p);
61                 free(p);
62         }
63
64 exit:
65         exit_configuration(&config_tree);
66         return n->ecdsa;
67 }
68
69 bool read_ecdsa_public_key(connection_t *c) {
70         if(ecdsa_active(c->ecdsa))
71                 return true;
72
73         char *p;
74
75         if(!c->config_tree) {
76                 init_configuration(&c->config_tree);
77                 if(!read_host_config(c->config_tree, c->name))
78                         return false;
79         }
80
81         /* First, check for simple ECDSAPublicKey statement */
82
83         if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
84                 c->ecdsa = ecdsa_set_base64_public_key(p);
85                 free(p);
86                 return c->ecdsa;
87         }
88
89         return false;
90 }
91
92 static bool read_ecdsa_private_key(void) {
93         FILE *fp;
94         char *fname;
95
96         xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
97         fp = fopen(fname, "r");
98         free(fname);
99
100         if(!fp) {
101                 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
102                 return false;
103         }
104
105         myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
106         fclose(fp);
107
108         if(!myself->connection->ecdsa)
109                 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
110
111         return myself->connection->ecdsa;
112 }
113
114 static bool read_invitation_key(void) {
115         FILE *fp;
116         char *fname;
117
118         if(invitation_key) {
119                 ecdsa_free(invitation_key);
120                 invitation_key = NULL;
121         }
122
123         xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
124
125         fp = fopen(fname, "r");
126
127         if(fp) {
128                 invitation_key = ecdsa_read_pem_private_key(fp);
129                 fclose(fp);
130                 if(!invitation_key)
131                         logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
132         }
133
134         free(fname);
135         return invitation_key;
136 }
137
138 static timeout_t keyexpire_timeout;
139
140 static void keyexpire_handler(void *data) {
141         regenerate_key();
142         timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
143 }
144
145 void regenerate_key(void) {
146         logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
147         send_key_changed();
148 }
149
150 void load_all_nodes(void) {
151         DIR *dir;
152         struct dirent *ent;
153         char *dname;
154
155         xasprintf(&dname, "%s" SLASH "hosts", confbase);
156         dir = opendir(dname);
157         if(!dir) {
158                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
159                 free(dname);
160                 return;
161         }
162
163         while((ent = readdir(dir))) {
164                 if(!check_id(ent->d_name))
165                         continue;
166
167                 node_t *n = lookup_node(ent->d_name);
168                 if(n)
169                         continue;
170
171                 n = new_node();
172                 n->name = xstrdup(ent->d_name);
173                 node_add(n);
174         }
175
176         closedir(dir);
177 }
178
179
180 char *get_name(void) {
181         char *name = NULL;
182
183         get_config_string(lookup_config(config_tree, "Name"), &name);
184
185         if(!name)
186                 return NULL;
187
188         if(!check_id(name)) {
189                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
190                 free(name);
191                 return NULL;
192         }
193
194         return name;
195 }
196
197 bool setup_myself_reloadable(void) {
198         localdiscovery = true;
199         macexpire = 600; // TODO: remove
200         keylifetime = 3600; // TODO: check if this can be removed as well
201         maxtimeout = 900;
202         autoconnect = 3;
203         myself->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 myself 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         myself = new_node();
303         myself->connection = new_connection();
304         myself->name = name;
305         myself->connection->name = xstrdup(name);
306         read_host_config(config_tree, name);
307
308         if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
309                 myport = xstrdup("655");
310         else
311                 port_specified = true;
312
313         myself->connection->options = 0;
314         myself->connection->protocol_major = PROT_MAJOR;
315         myself->connection->protocol_minor = PROT_MINOR;
316
317         myself->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         myself->incompression = 0;
346         myself->connection->outcompression = 0;
347
348         /* Done */
349
350         myself->nexthop = myself;
351         myself->via = myself;
352         myself->status.reachable = true;
353         myself->last_state_change = now.tv_sec;
354         node_add(myself);
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(&myself->hostname, "MYSELF port %s", myport);
380         myself->connection->hostname = xstrdup(myself->hostname);
381
382         /* Done. */
383
384         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 = connection_list->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(outgoing_list)
420                 list_delete_list(outgoing_list);
421
422         if(myself && myself->connection) {
423                 terminate_connection(myself->connection, false);
424                 free_connection(myself->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 }