3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
24 #include "connection.h"
29 #include "meshlink_internal.h"
43 proxytype_t proxytype;
45 bool disablebuggypeers;
47 bool node_read_ecdsa_public_key(node_t *n) {
48 if(ecdsa_active(n->ecdsa))
51 splay_tree_t *config_tree;
54 init_configuration(&config_tree);
55 if(!read_host_config(config_tree, n->name))
58 /* First, check for simple ECDSAPublicKey statement */
60 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
61 n->ecdsa = ecdsa_set_base64_public_key(p);
66 exit_configuration(&config_tree);
70 bool read_ecdsa_public_key(connection_t *c) {
71 if(ecdsa_active(c->ecdsa))
77 init_configuration(&c->config_tree);
78 if(!read_host_config(c->config_tree, c->name))
82 /* First, check for simple ECDSAPublicKey statement */
84 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
85 c->ecdsa = ecdsa_set_base64_public_key(p);
93 static bool read_ecdsa_private_key(void) {
97 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
98 fp = fopen(fname, "r");
102 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
106 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
109 if(!mesh->self->connection->ecdsa)
110 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
112 return mesh->self->connection->ecdsa;
115 static bool read_invitation_key(void) {
120 ecdsa_free(invitation_key);
121 invitation_key = NULL;
124 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
126 fp = fopen(fname, "r");
129 invitation_key = ecdsa_read_pem_private_key(fp);
132 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
136 return invitation_key;
139 static timeout_t keyexpire_timeout;
141 static void keyexpire_handler(void *data) {
143 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
146 void regenerate_key(void) {
147 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
151 void load_all_nodes(void) {
156 xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase);
157 dir = opendir(dname);
159 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
164 while((ent = readdir(dir))) {
165 if(!check_id(ent->d_name))
168 node_t *n = lookup_node(ent->d_name);
173 n->name = xstrdup(ent->d_name);
181 char *get_name(void) {
184 get_config_string(lookup_config(mesh->config, "Name"), &name);
189 if(!check_id(name)) {
190 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
198 bool setup_myself_reloadable(void) {
199 localdiscovery = true;
200 keylifetime = 3600; // TODO: check if this can be removed as well
203 mesh->self->options |= OPTION_PMTU_DISCOVERY;
205 read_invitation_key();
211 Add listening sockets.
213 static bool add_listen_address(char *address, bool bindto) {
217 char *space = strchr(address, ' ');
223 if(!strcmp(address, "*"))
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;
233 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
237 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
241 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
242 // Ignore duplicate addresses
245 for(int i = 0; i < listen_sockets; i++)
246 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
254 if(listen_sockets >= MAXSOCKETS) {
255 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
259 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
264 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
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);
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);
280 listen_socket[listen_sockets].bindto = bindto;
281 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
290 Configure node_t mesh->self and set up the local sockets (listen only)
292 bool setup_myself(void) {
293 char *name, *hostname, *cipher, *digest, *type;
294 char *address = NULL;
295 bool port_specified = false;
297 if(!(name = get_name())) {
298 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
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);
308 if(!get_config_string(lookup_config(mesh->config, "Port"), &myport))
309 myport = xstrdup("655");
311 port_specified = true;
313 mesh->self->connection->options = 0;
314 mesh->self->connection->protocol_major = PROT_MAJOR;
315 mesh->self->connection->protocol_minor = PROT_MINOR;
317 mesh->self->options |= PROT_MINOR << 24;
319 if(!read_ecdsa_private_key())
322 /* Ensure myport is numeric */
325 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
327 if(!ai || !ai->ai_addr)
330 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
331 sockaddr2str(&sa, NULL, &myport);
334 /* Check some options */
336 if(!setup_myself_reloadable())
339 // TODO: check whether this is used at all
340 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
344 // TODO: drop compression in the packet layer?
345 mesh->self->incompression = 0;
346 mesh->self->connection->outcompression = 0;
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);
366 if(!add_listen_address(address, NULL))
369 if(!listen_sockets) {
370 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
374 // TODO: require Port to be set? Or use "0" and use getsockname()?
377 myport = xstrdup("655");
379 xasprintf(&mesh->self->hostname, "MYSELF port %s", myport);
380 mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
384 mesh->last_config_check = now.tv_sec;
392 bool setup_network(void) {
400 maxoutbufsize = 10 * MTU;
409 close all open network connections
411 void close_network_connections(void) {
412 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
414 connection_t *c = node->data;
416 terminate_connection(c, false);
420 list_delete_list(mesh->outgoings);
422 if(mesh->self && mesh->self->connection) {
423 terminate_connection(mesh->self->connection, false);
424 free_connection(mesh->self->connection);
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);
439 if(myport) free(myport);