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