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;
55 init_configuration(&config_tree);
56 if(!read_host_config(config_tree, n->name))
59 /* First, check for simple ECDSAPublicKey statement */
61 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
62 n->ecdsa = ecdsa_set_base64_public_key(p);
67 /* Else, check for ECDSAPublicKeyFile statement and read it */
69 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
70 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
72 fp = fopen(pubname, "r");
77 n->ecdsa = ecdsa_read_pem_public_key(fp);
81 exit_configuration(&config_tree);
86 bool read_ecdsa_public_key(connection_t *c) {
87 if(ecdsa_active(c->ecdsa))
95 init_configuration(&c->config_tree);
96 if(!read_host_config(c->config_tree, c->name))
100 /* First, check for simple ECDSAPublicKey statement */
102 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
103 c->ecdsa = ecdsa_set_base64_public_key(p);
108 /* Else, check for ECDSAPublicKeyFile statement and read it */
110 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
111 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
113 fp = fopen(fname, "r");
116 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
117 fname, strerror(errno));
122 c->ecdsa = ecdsa_read_pem_public_key(fp);
126 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
131 static bool read_ecdsa_private_key(void) {
135 /* Check for PrivateKeyFile statement and read it */
137 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
138 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
140 fp = fopen(fname, "r");
143 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
145 logger(DEBUG_ALWAYS, LOG_INFO, "Create an ECDSA keypair with `tinc generate-ecdsa-keys'.");
150 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
153 if(fstat(fileno(fp), &s)) {
154 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
159 if(s.st_mode & ~0100700)
160 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
163 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
166 if(!myself->connection->ecdsa)
167 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
169 return myself->connection->ecdsa;
172 static bool read_invitation_key(void) {
177 ecdsa_free(invitation_key);
178 invitation_key = NULL;
181 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase);
183 fp = fopen(fname, "r");
186 invitation_key = ecdsa_read_pem_private_key(fp);
189 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
193 return invitation_key;
196 static timeout_t keyexpire_timeout;
198 static void keyexpire_handler(void *data) {
200 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
203 void regenerate_key(void) {
204 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
208 void load_all_nodes(void) {
213 xasprintf(&dname, "%s" SLASH "hosts", confbase);
214 dir = opendir(dname);
216 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
221 while((ent = readdir(dir))) {
222 if(!check_id(ent->d_name))
225 node_t *n = lookup_node(ent->d_name);
230 n->name = xstrdup(ent->d_name);
238 char *get_name(void) {
241 get_config_string(lookup_config(config_tree, "Name"), &name);
247 char *envname = getenv(name + 1);
248 char hostname[32] = "";
250 if(strcmp(name + 1, "HOST")) {
251 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
254 if(gethostname(hostname, sizeof hostname) || !*hostname) {
255 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
262 name = xstrdup(envname);
263 for(char *c = name; *c; c++)
268 if(!check_id(name)) {
269 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
277 bool setup_myself_reloadable(void) {
283 char *address = NULL;
287 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
289 if((space = strchr(proxy, ' ')))
292 if(!strcasecmp(proxy, "none")) {
293 proxytype = PROXY_NONE;
294 } else if(!strcasecmp(proxy, "socks4")) {
295 proxytype = PROXY_SOCKS4;
296 } else if(!strcasecmp(proxy, "socks4a")) {
297 proxytype = PROXY_SOCKS4A;
298 } else if(!strcasecmp(proxy, "socks5")) {
299 proxytype = PROXY_SOCKS5;
300 } else if(!strcasecmp(proxy, "http")) {
301 proxytype = PROXY_HTTP;
302 } else if(!strcasecmp(proxy, "exec")) {
303 proxytype = PROXY_EXEC;
305 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
315 if(!space || !*space) {
316 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
319 proxyhost = xstrdup(space);
327 if(space && (space = strchr(space, ' ')))
328 *space++ = 0, proxyport = space;
329 if(space && (space = strchr(space, ' ')))
330 *space++ = 0, proxyuser = space;
331 if(space && (space = strchr(space, ' ')))
332 *space++ = 0, proxypass = space;
333 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
334 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
337 proxyhost = xstrdup(proxyhost);
338 proxyport = xstrdup(proxyport);
339 if(proxyuser && *proxyuser)
340 proxyuser = xstrdup(proxyuser);
341 if(proxypass && *proxypass)
342 proxypass = xstrdup(proxypass);
349 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
350 myself->options |= OPTION_INDIRECT;
352 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
353 myself->options |= OPTION_TCPONLY;
355 if(myself->options & OPTION_TCPONLY)
356 myself->options |= OPTION_INDIRECT;
358 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
359 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
361 memset(&localdiscovery_address, 0, sizeof localdiscovery_address);
362 if(get_config_string(lookup_config(config_tree, "LocalDiscoveryAddress"), &address)) {
363 struct addrinfo *ai = str2addrinfo(address, myport, SOCK_DGRAM);
367 memcpy(&localdiscovery_address, ai->ai_addr, ai->ai_addrlen);
371 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
372 if(!strcasecmp(rmode, "router"))
373 routing_mode = RMODE_ROUTER;
374 else if(!strcasecmp(rmode, "switch"))
375 routing_mode = RMODE_SWITCH;
376 else if(!strcasecmp(rmode, "hub"))
377 routing_mode = RMODE_HUB;
379 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
385 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
386 if(!strcasecmp(fmode, "off"))
387 forwarding_mode = FMODE_OFF;
388 else if(!strcasecmp(fmode, "internal"))
389 forwarding_mode = FMODE_INTERNAL;
390 else if(!strcasecmp(fmode, "kernel"))
391 forwarding_mode = FMODE_KERNEL;
393 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
400 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
402 myself->options |= OPTION_PMTU_DISCOVERY;
405 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
407 myself->options |= OPTION_CLAMP_MSS;
409 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
410 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
411 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
412 if(!strcasecmp(bmode, "no"))
413 broadcast_mode = BMODE_NONE;
414 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
415 broadcast_mode = BMODE_MST;
416 else if(!strcasecmp(bmode, "direct"))
417 broadcast_mode = BMODE_DIRECT;
419 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
425 #if !defined(SOL_IP) || !defined(IP_TOS)
426 if(priorityinheritance)
427 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
430 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
433 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
434 if(maxtimeout <= 0) {
435 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
441 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
442 if(!strcasecmp(afname, "IPv4"))
443 addressfamily = AF_INET;
444 else if(!strcasecmp(afname, "IPv6"))
445 addressfamily = AF_INET6;
446 else if(!strcasecmp(afname, "any"))
447 addressfamily = AF_UNSPEC;
449 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
455 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
457 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
460 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
464 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
466 read_invitation_key();
472 Add listening sockets.
474 static bool add_listen_address(char *address, bool bindto) {
478 char *space = strchr(address, ' ');
484 if(!strcmp(address, "*"))
488 struct addrinfo *ai, hint = {0};
489 hint.ai_family = addressfamily;
490 hint.ai_socktype = SOCK_STREAM;
491 hint.ai_protocol = IPPROTO_TCP;
492 hint.ai_flags = AI_PASSIVE;
494 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
498 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
502 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
503 // Ignore duplicate addresses
506 for(int i = 0; i < listen_sockets; i++)
507 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
515 if(listen_sockets >= MAXSOCKETS) {
516 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
520 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
525 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
532 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
533 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
535 if(debug_level >= DEBUG_CONNECTIONS) {
536 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
537 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
541 listen_socket[listen_sockets].bindto = bindto;
542 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
551 Configure node_t myself and set up the local sockets (listen only)
553 bool setup_myself(void) {
554 char *name, *hostname, *cipher, *digest, *type;
555 char *address = NULL;
556 bool port_specified = false;
558 if(!(name = get_name())) {
559 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
564 myself->connection = new_connection();
566 myself->connection->name = xstrdup(name);
567 read_host_config(config_tree, name);
569 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
570 myport = xstrdup("655");
572 port_specified = true;
574 myself->connection->options = 0;
575 myself->connection->protocol_major = PROT_MAJOR;
576 myself->connection->protocol_minor = PROT_MINOR;
578 myself->options |= PROT_MINOR << 24;
580 if(!read_ecdsa_private_key())
583 /* Ensure myport is numeric */
586 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
588 if(!ai || !ai->ai_addr)
591 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
592 sockaddr2str(&sa, NULL, &myport);
595 /* Check some options */
597 if(!setup_myself_reloadable())
600 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
601 if(max_connection_burst <= 0) {
602 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
608 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
609 if(replaywin_int < 0) {
610 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
613 replaywin = (unsigned)replaywin_int;
614 sptps_replaywin = replaywin;
617 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
621 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
622 if(myself->incompression < 0 || myself->incompression > 11) {
623 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
627 myself->incompression = 0;
629 myself->connection->outcompression = 0;
633 myself->nexthop = myself;
634 myself->via = myself;
635 myself->status.reachable = true;
636 myself->last_state_change = now.tv_sec;
649 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
651 get_config_string(cfg, &address);
652 if(!add_listen_address(address, true))
656 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
658 get_config_string(cfg, &address);
659 if(!add_listen_address(address, false))
664 if(!add_listen_address(address, NULL))
667 if(!listen_sockets) {
668 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
672 /* If no Port option was specified, set myport to the port used by the first listening socket. */
674 if(!port_specified) {
676 socklen_t salen = sizeof sa;
677 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
679 sockaddr2str(&sa, NULL, &myport);
681 myport = xstrdup("655");
685 xasprintf(&myself->hostname, "MYSELF port %s", myport);
686 myself->connection->hostname = xstrdup(myself->hostname);
690 last_config_check = now.tv_sec;
698 bool setup_network(void) {
704 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
705 if(pinginterval < 1) {
706 pinginterval = 86400;
711 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
713 if(pingtimeout < 1 || pingtimeout > pinginterval)
714 pingtimeout = pinginterval;
716 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
717 maxoutbufsize = 10 * MTU;
726 close all open network connections
728 void close_network_connections(void) {
729 for(list_node_t *node = connection_list->head, *next; node; node = next) {
731 connection_t *c = node->data;
733 terminate_connection(c, false);
737 list_delete_list(outgoing_list);
739 if(myself && myself->connection) {
740 terminate_connection(myself->connection, false);
741 free_connection(myself->connection);
744 for(int i = 0; i < listen_sockets; i++) {
745 io_del(&listen_socket[i].tcp);
746 io_del(&listen_socket[i].udp);
747 close(listen_socket[i].tcp.fd);
748 close(listen_socket[i].udp.fd);
756 if(myport) free(myport);