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.
23 #include "connection.h"
27 #include "meshlink_internal.h"
37 bool node_read_ecdsa_public_key(node_t *n) {
38 if(ecdsa_active(n->ecdsa))
41 splay_tree_t *config_tree;
44 init_configuration(&config_tree);
45 if(!read_host_config(config_tree, n->name))
48 /* First, check for simple ECDSAPublicKey statement */
50 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
51 n->ecdsa = ecdsa_set_base64_public_key(p);
56 exit_configuration(&config_tree);
60 bool read_ecdsa_public_key(connection_t *c) {
61 if(ecdsa_active(c->ecdsa))
67 init_configuration(&c->config_tree);
68 if(!read_host_config(c->config_tree, c->name))
72 /* First, check for simple ECDSAPublicKey statement */
74 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
75 c->ecdsa = ecdsa_set_base64_public_key(p);
83 static bool read_ecdsa_private_key(void) {
87 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
88 fp = fopen(fname, "r");
92 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
96 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
99 if(!mesh->self->connection->ecdsa)
100 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
102 return mesh->self->connection->ecdsa;
105 static bool read_invitation_key(void) {
109 if(mesh->invitation_key) {
110 ecdsa_free(mesh->invitation_key);
111 mesh->invitation_key = NULL;
114 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
116 fp = fopen(fname, "r");
119 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
121 if(!mesh->invitation_key)
122 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
126 return mesh->invitation_key;
129 void load_all_nodes(void) {
134 xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase);
135 dir = opendir(dname);
137 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
142 while((ent = readdir(dir))) {
143 if(!check_id(ent->d_name))
146 node_t *n = lookup_node(ent->d_name);
151 n->name = xstrdup(ent->d_name);
159 char *get_name(void) {
162 get_config_string(lookup_config(mesh->config, "Name"), &name);
167 if(!check_id(name)) {
168 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
176 bool setup_myself_reloadable(void) {
177 mesh->localdiscovery = true;
178 keylifetime = 3600; // TODO: check if this can be removed as well
181 mesh->self->options |= OPTION_PMTU_DISCOVERY;
183 read_invitation_key();
189 Add listening sockets.
191 static bool add_listen_address(char *address, bool bindto) {
192 char *port = mesh->myport;
195 char *space = strchr(address, ' ');
201 if(!strcmp(address, "*"))
205 struct addrinfo *ai, hint = {0};
206 hint.ai_family = addressfamily;
207 hint.ai_socktype = SOCK_STREAM;
208 hint.ai_protocol = IPPROTO_TCP;
209 hint.ai_flags = AI_PASSIVE;
211 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
215 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
219 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
220 // Ignore duplicate addresses
223 for(int i = 0; i < mesh->listen_sockets; i++)
224 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
232 if(mesh->listen_sockets >= MAXSOCKETS) {
233 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
237 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
242 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
249 io_add(&mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
250 io_add(&mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
252 if(mesh->debug_level >= DEBUG_CONNECTIONS) {
253 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
254 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
258 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
259 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
260 mesh->listen_sockets++;
268 Configure node_t mesh->self and set up the local sockets (listen only)
270 bool setup_myself(void) {
271 char *name, *hostname, *cipher, *digest, *type;
272 char *address = NULL;
273 bool port_specified = false;
275 if(!(name = get_name())) {
276 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
280 mesh->self = new_node();
281 mesh->self->connection = new_connection();
282 mesh->self->name = name;
283 mesh->self->connection->name = xstrdup(name);
284 read_host_config(mesh->config, name);
286 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
287 mesh->myport = xstrdup("655");
289 port_specified = true;
291 mesh->self->connection->options = 0;
292 mesh->self->connection->protocol_major = PROT_MAJOR;
293 mesh->self->connection->protocol_minor = PROT_MINOR;
295 mesh->self->options |= PROT_MINOR << 24;
297 if(!read_ecdsa_private_key())
300 /* Ensure mesh->myport is numeric */
302 if(!atoi(mesh->myport)) {
303 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
305 if(!ai || !ai->ai_addr)
308 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
309 sockaddr2str(&sa, NULL, &mesh->myport);
312 /* Check some options */
314 if(!setup_myself_reloadable())
319 // TODO: drop compression in the packet layer?
320 mesh->self->incompression = 0;
321 mesh->self->connection->outcompression = 0;
325 mesh->self->nexthop = mesh->self;
326 mesh->self->via = mesh->self;
327 mesh->self->status.reachable = true;
328 mesh->self->last_state_change = now.tv_sec;
329 node_add(mesh->self);
338 mesh->listen_sockets = 0;
341 if(!add_listen_address(address, NULL))
344 if(!mesh->listen_sockets) {
345 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
349 // TODO: require Port to be set? Or use "0" and use getsockname()?
352 mesh->myport = xstrdup("655");
354 xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
355 mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
359 mesh->last_config_check = now.tv_sec;
367 bool setup_network(void) {
375 maxoutbufsize = 10 * MTU;
384 close all open network connections
386 void close_network_connections(void) {
387 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
389 connection_t *c = node->data;
391 terminate_connection(c, false);
395 list_delete_list(mesh->outgoings);
397 if(mesh->self && mesh->self->connection) {
398 terminate_connection(mesh->self->connection, false);
399 free_connection(mesh->self->connection);
402 for(int i = 0; i < mesh->listen_sockets; i++) {
403 io_del(&mesh->listen_socket[i].tcp);
404 io_del(&mesh->listen_socket[i].udp);
405 close(mesh->listen_socket[i].tcp.fd);
406 close(mesh->listen_socket[i].udp.fd);
414 if(mesh->myport) free(mesh->myport);