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"
42 proxytype_t proxytype;
44 bool disablebuggypeers;
46 bool node_read_ecdsa_public_key(node_t *n) {
47 if(ecdsa_active(n->ecdsa))
50 splay_tree_t *config_tree;
53 init_configuration(&config_tree);
54 if(!read_host_config(config_tree, n->name))
57 /* First, check for simple ECDSAPublicKey statement */
59 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
60 n->ecdsa = ecdsa_set_base64_public_key(p);
65 exit_configuration(&config_tree);
69 bool read_ecdsa_public_key(connection_t *c) {
70 if(ecdsa_active(c->ecdsa))
76 init_configuration(&c->config_tree);
77 if(!read_host_config(c->config_tree, c->name))
81 /* First, check for simple ECDSAPublicKey statement */
83 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
84 c->ecdsa = ecdsa_set_base64_public_key(p);
92 static bool read_ecdsa_private_key(void) {
96 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
97 fp = fopen(fname, "r");
101 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
105 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
108 if(!myself->connection->ecdsa)
109 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
111 return myself->connection->ecdsa;
114 static bool read_invitation_key(void) {
119 ecdsa_free(invitation_key);
120 invitation_key = NULL;
123 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
125 fp = fopen(fname, "r");
128 invitation_key = ecdsa_read_pem_private_key(fp);
131 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
135 return invitation_key;
138 static timeout_t keyexpire_timeout;
140 static void keyexpire_handler(void *data) {
142 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
145 void regenerate_key(void) {
146 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
150 void load_all_nodes(void) {
155 xasprintf(&dname, "%s" SLASH "hosts", confbase);
156 dir = opendir(dname);
158 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
163 while((ent = readdir(dir))) {
164 if(!check_id(ent->d_name))
167 node_t *n = lookup_node(ent->d_name);
172 n->name = xstrdup(ent->d_name);
180 char *get_name(void) {
183 get_config_string(lookup_config(config_tree, "Name"), &name);
188 if(!check_id(name)) {
189 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
197 bool setup_myself_reloadable(void) {
198 localdiscovery = true;
199 keylifetime = 3600; // TODO: check if this can be removed as well
202 myself->options |= OPTION_PMTU_DISCOVERY;
204 read_invitation_key();
210 Add listening sockets.
212 static bool add_listen_address(char *address, bool bindto) {
216 char *space = strchr(address, ' ');
222 if(!strcmp(address, "*"))
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;
232 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
236 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
240 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
241 // Ignore duplicate addresses
244 for(int i = 0; i < listen_sockets; i++)
245 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
253 if(listen_sockets >= MAXSOCKETS) {
254 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
258 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
263 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
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);
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);
279 listen_socket[listen_sockets].bindto = bindto;
280 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
289 Configure node_t myself and set up the local sockets (listen only)
291 bool setup_myself(void) {
292 char *name, *hostname, *cipher, *digest, *type;
293 char *address = NULL;
294 bool port_specified = false;
296 if(!(name = get_name())) {
297 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
302 myself->connection = new_connection();
304 myself->connection->name = xstrdup(name);
305 read_host_config(config_tree, name);
307 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
308 myport = xstrdup("655");
310 port_specified = true;
312 myself->connection->options = 0;
313 myself->connection->protocol_major = PROT_MAJOR;
314 myself->connection->protocol_minor = PROT_MINOR;
316 myself->options |= PROT_MINOR << 24;
318 if(!read_ecdsa_private_key())
321 /* Ensure myport is numeric */
324 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
326 if(!ai || !ai->ai_addr)
329 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
330 sockaddr2str(&sa, NULL, &myport);
333 /* Check some options */
335 if(!setup_myself_reloadable())
338 // TODO: check whether this is used at all
339 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
343 // TODO: drop compression in the packet layer?
344 myself->incompression = 0;
345 myself->connection->outcompression = 0;
349 myself->nexthop = myself;
350 myself->via = myself;
351 myself->status.reachable = true;
352 myself->last_state_change = now.tv_sec;
365 if(!add_listen_address(address, NULL))
368 if(!listen_sockets) {
369 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
373 // TODO: require Port to be set? Or use "0" and use getsockname()?
376 myport = xstrdup("655");
378 xasprintf(&myself->hostname, "MYSELF port %s", myport);
379 myself->connection->hostname = xstrdup(myself->hostname);
383 last_config_check = now.tv_sec;
391 bool setup_network(void) {
399 maxoutbufsize = 10 * MTU;
408 close all open network connections
410 void close_network_connections(void) {
411 for(list_node_t *node = connection_list->head, *next; node; node = next) {
413 connection_t *c = node->data;
415 terminate_connection(c, false);
419 list_delete_list(outgoing_list);
421 if(myself && myself->connection) {
422 terminate_connection(myself->connection, false);
423 free_connection(myself->connection);
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);
438 if(myport) free(myport);