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"
39 bool node_read_ecdsa_public_key(node_t *n) {
40 if(ecdsa_active(n->ecdsa))
43 splay_tree_t *config_tree;
46 init_configuration(&config_tree);
47 if(!read_host_config(config_tree, n->name))
50 /* First, check for simple ECDSAPublicKey statement */
52 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
53 n->ecdsa = ecdsa_set_base64_public_key(p);
58 exit_configuration(&config_tree);
62 bool read_ecdsa_public_key(connection_t *c) {
63 if(ecdsa_active(c->ecdsa))
69 init_configuration(&c->config_tree);
70 if(!read_host_config(c->config_tree, c->name))
74 /* First, check for simple ECDSAPublicKey statement */
76 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
77 c->ecdsa = ecdsa_set_base64_public_key(p);
85 static bool read_ecdsa_private_key(void) {
89 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
90 fp = fopen(fname, "r");
94 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
98 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
101 if(!mesh->self->connection->ecdsa)
102 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
104 return mesh->self->connection->ecdsa;
107 static bool read_invitation_key(void) {
111 if(mesh->invitation_key) {
112 ecdsa_free(mesh->invitation_key);
113 mesh->invitation_key = NULL;
116 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
118 fp = fopen(fname, "r");
121 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
123 if(!mesh->invitation_key)
124 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
128 return mesh->invitation_key;
131 void load_all_nodes(void) {
136 xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase);
137 dir = opendir(dname);
139 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
144 while((ent = readdir(dir))) {
145 if(!check_id(ent->d_name))
148 node_t *n = lookup_node(ent->d_name);
153 n->name = xstrdup(ent->d_name);
161 char *get_name(void) {
164 get_config_string(lookup_config(mesh->config, "Name"), &name);
169 if(!check_id(name)) {
170 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
178 bool setup_myself_reloadable(void) {
179 mesh->localdiscovery = true;
180 keylifetime = 3600; // TODO: check if this can be removed as well
183 mesh->self->options |= OPTION_PMTU_DISCOVERY;
185 read_invitation_key();
191 Add listening sockets.
193 static bool add_listen_address(char *address, bool bindto) {
194 char *port = mesh->myport;
197 char *space = strchr(address, ' ');
203 if(!strcmp(address, "*"))
207 struct addrinfo *ai, hint = {0};
208 hint.ai_family = addressfamily;
209 hint.ai_socktype = SOCK_STREAM;
210 hint.ai_protocol = IPPROTO_TCP;
211 hint.ai_flags = AI_PASSIVE;
213 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
217 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
221 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
222 // Ignore duplicate addresses
225 for(int i = 0; i < mesh->listen_sockets; i++)
226 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
234 if(mesh->listen_sockets >= MAXSOCKETS) {
235 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
239 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
244 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
251 io_add(&mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
252 io_add(&mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
254 if(debug_level >= DEBUG_CONNECTIONS) {
255 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
256 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
260 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
261 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
262 mesh->listen_sockets++;
270 Configure node_t mesh->self and set up the local sockets (listen only)
272 bool setup_myself(void) {
273 char *name, *hostname, *cipher, *digest, *type;
274 char *address = NULL;
275 bool port_specified = false;
277 if(!(name = get_name())) {
278 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
282 mesh->self = new_node();
283 mesh->self->connection = new_connection();
284 mesh->self->name = name;
285 mesh->self->connection->name = xstrdup(name);
286 read_host_config(mesh->config, name);
288 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
289 mesh->myport = xstrdup("655");
291 port_specified = true;
293 mesh->self->connection->options = 0;
294 mesh->self->connection->protocol_major = PROT_MAJOR;
295 mesh->self->connection->protocol_minor = PROT_MINOR;
297 mesh->self->options |= PROT_MINOR << 24;
299 if(!read_ecdsa_private_key())
302 /* Ensure mesh->myport is numeric */
304 if(!atoi(mesh->myport)) {
305 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
307 if(!ai || !ai->ai_addr)
310 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
311 sockaddr2str(&sa, NULL, &mesh->myport);
314 /* Check some options */
316 if(!setup_myself_reloadable())
321 // TODO: drop compression in the packet layer?
322 mesh->self->incompression = 0;
323 mesh->self->connection->outcompression = 0;
327 mesh->self->nexthop = mesh->self;
328 mesh->self->via = mesh->self;
329 mesh->self->status.reachable = true;
330 mesh->self->last_state_change = now.tv_sec;
331 node_add(mesh->self);
340 mesh->listen_sockets = 0;
343 if(!add_listen_address(address, NULL))
346 if(!mesh->listen_sockets) {
347 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
351 // TODO: require Port to be set? Or use "0" and use getsockname()?
354 mesh->myport = xstrdup("655");
356 xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
357 mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
361 mesh->last_config_check = now.tv_sec;
369 bool setup_network(void) {
377 maxoutbufsize = 10 * MTU;
386 close all open network connections
388 void close_network_connections(void) {
389 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
391 connection_t *c = node->data;
393 terminate_connection(c, false);
397 list_delete_list(mesh->outgoings);
399 if(mesh->self && mesh->self->connection) {
400 terminate_connection(mesh->self->connection, false);
401 free_connection(mesh->self->connection);
404 for(int i = 0; i < mesh->listen_sockets; i++) {
405 io_del(&mesh->listen_socket[i].tcp);
406 io_del(&mesh->listen_socket[i].udp);
407 close(mesh->listen_socket[i].tcp.fd);
408 close(mesh->listen_socket[i].udp.fd);
416 if(mesh->myport) free(mesh->myport);