3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2010 Brandon Black <blblack@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "splay_tree.h"
28 #include "connection.h"
46 static struct event device_ev;
49 bool node_read_ecdsa_public_key(node_t *n) {
50 if(ecdsa_active(&n->ecdsa))
53 splay_tree_t *config_tree;
59 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
61 init_configuration(&config_tree);
62 if(!read_config_file(config_tree, fname))
65 /* First, check for simple ECDSAPublicKey statement */
67 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
68 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
73 /* Else, check for ECDSAPublicKeyFile statement and read it */
77 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &fname))
78 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
80 fp = fopen(fname, "r");
83 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", fname, strerror(errno));
87 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
91 exit_configuration(&config_tree);
96 bool read_ecdsa_public_key(connection_t *c) {
102 /* First, check for simple ECDSAPublicKey statement */
104 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
105 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
110 /* Else, check for ECDSAPublicKeyFile statement and read it */
112 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
113 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
115 fp = fopen(fname, "r");
118 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
119 fname, strerror(errno));
124 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
128 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA public key file `%s' failed: %s", fname, strerror(errno));
133 bool read_rsa_public_key(connection_t *c) {
139 /* First, check for simple PublicKey statement */
141 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
142 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
147 /* Else, check for PublicKeyFile statement and read it */
149 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
150 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
152 fp = fopen(fname, "r");
155 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
160 result = rsa_read_pem_public_key(&c->rsa, fp);
164 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
169 static bool read_ecdsa_private_key(void) {
174 /* Check for PrivateKeyFile statement and read it */
176 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
177 xasprintf(&fname, "%s/ecdsa_key.priv", confbase);
179 fp = fopen(fname, "r");
182 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
187 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
190 if(fstat(fileno(fp), &s)) {
191 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
196 if(s.st_mode & ~0100700)
197 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
200 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
204 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
209 static bool read_rsa_private_key(void) {
215 /* First, check for simple PrivateKey statement */
217 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
218 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
219 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
223 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
229 /* Else, check for PrivateKeyFile statement and read it */
231 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
232 xasprintf(&fname, "%s/rsa_key.priv", confbase);
234 fp = fopen(fname, "r");
237 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
238 fname, strerror(errno));
243 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
246 if(fstat(fileno(fp), &s)) {
247 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
252 if(s.st_mode & ~0100700)
253 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
256 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
260 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
265 static struct event keyexpire_event;
267 static void keyexpire_handler(int fd, short events, void *data) {
271 void regenerate_key(void) {
272 if(timeout_initialized(&keyexpire_event)) {
273 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
274 event_del(&keyexpire_event);
277 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
280 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
284 Read Subnets from all host config files
286 void load_all_subnets(void) {
291 splay_tree_t *config_tree;
296 xasprintf(&dname, "%s/hosts", confbase);
297 dir = opendir(dname);
299 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
304 while((ent = readdir(dir))) {
305 if(!check_id(ent->d_name))
308 n = lookup_node(ent->d_name);
309 #ifdef _DIRENT_HAVE_D_TYPE
310 //if(ent->d_type != DT_REG)
314 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
315 init_configuration(&config_tree);
316 read_config_options(config_tree, ent->d_name);
317 read_config_file(config_tree, fname);
322 n->name = xstrdup(ent->d_name);
326 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
327 if(!get_config_subnet(cfg, &s))
330 if((s2 = lookup_subnet(n, s))) {
337 exit_configuration(&config_tree);
344 Configure node_t myself and set up the local sockets (listen only)
346 static bool setup_myself(void) {
349 char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
351 char *address = NULL;
353 struct addrinfo *ai, *aip, hint = {0};
359 myself->connection = new_connection();
361 myself->hostname = xstrdup("MYSELF");
362 myself->connection->hostname = xstrdup("MYSELF");
364 myself->connection->options = 0;
365 myself->connection->protocol_major = PROT_MAJOR;
366 myself->connection->protocol_minor = PROT_MINOR;
368 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
369 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
373 if(!check_id(name)) {
374 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
380 myself->connection->name = xstrdup(name);
381 xasprintf(&fname, "%s/hosts/%s", confbase, name);
382 read_config_options(config_tree, name);
383 read_config_file(config_tree, fname);
386 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
388 if(experimental && !read_ecdsa_private_key())
391 if(!read_rsa_private_key())
394 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
395 myport = xstrdup("655");
398 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
400 if(!ai || !ai->ai_addr)
403 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
404 sockaddr2str(&sa, NULL, &myport);
407 /* Read in all the subnets specified in the host configuration file */
409 cfg = lookup_config(config_tree, "Subnet");
412 if(!get_config_subnet(cfg, &subnet))
415 subnet_add(myself, subnet);
417 cfg = lookup_config_next(config_tree, cfg);
420 /* Check some options */
422 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
423 myself->options |= OPTION_INDIRECT;
425 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
426 myself->options |= OPTION_TCPONLY;
428 if(myself->options & OPTION_TCPONLY)
429 myself->options |= OPTION_INDIRECT;
431 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
432 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
433 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
434 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
435 strictsubnets |= tunnelserver;
437 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
438 if(!strcasecmp(mode, "router"))
439 routing_mode = RMODE_ROUTER;
440 else if(!strcasecmp(mode, "switch"))
441 routing_mode = RMODE_SWITCH;
442 else if(!strcasecmp(mode, "hub"))
443 routing_mode = RMODE_HUB;
445 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
451 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
452 if(!strcasecmp(mode, "off"))
453 forwarding_mode = FMODE_OFF;
454 else if(!strcasecmp(mode, "internal"))
455 forwarding_mode = FMODE_INTERNAL;
456 else if(!strcasecmp(mode, "kernel"))
457 forwarding_mode = FMODE_KERNEL;
459 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
466 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
468 myself->options |= OPTION_PMTU_DISCOVERY;
471 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
473 myself->options |= OPTION_CLAMP_MSS;
475 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
476 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
477 get_config_bool(lookup_config(config_tree, "Broadcast"), &broadcast);
479 #if !defined(SOL_IP) || !defined(IP_TOS)
480 if(priorityinheritance)
481 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
484 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
487 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
488 if(maxtimeout <= 0) {
489 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
495 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
496 if(udp_rcvbuf <= 0) {
497 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
502 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
503 if(udp_sndbuf <= 0) {
504 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
509 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
510 if(replaywin_int < 0) {
511 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
514 replaywin = (unsigned)replaywin_int;
517 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
518 if(!strcasecmp(afname, "IPv4"))
519 addressfamily = AF_INET;
520 else if(!strcasecmp(afname, "IPv6"))
521 addressfamily = AF_INET6;
522 else if(!strcasecmp(afname, "any"))
523 addressfamily = AF_UNSPEC;
525 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
531 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
533 /* Generate packet encryption key */
535 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
536 cipher = xstrdup("blowfish");
538 if(!cipher_open_by_name(&myself->incipher, cipher)) {
539 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
543 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
548 /* Check if we want to use message authentication codes... */
551 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
554 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
558 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
559 digest = xstrdup("sha1");
561 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
562 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
568 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
569 if(myself->incompression < 0 || myself->incompression > 11) {
570 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
574 myself->incompression = 0;
576 myself->connection->outcompression = 0;
580 myself->nexthop = myself;
581 myself->via = myself;
582 myself->status.reachable = true;
594 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
595 if(!strcasecmp(type, "dummy"))
596 devops = dummy_devops;
597 else if(!strcasecmp(type, "raw_socket"))
598 devops = raw_socket_devops;
599 else if(!strcasecmp(type, "multicast"))
600 devops = multicast_devops;
602 else if(!strcasecmp(type, "uml"))
606 else if(!strcasecmp(type, "vde"))
615 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
617 if (event_add(&device_ev, NULL) < 0) {
618 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
624 /* Run tinc-up script to further initialize the tap interface */
625 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
626 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
627 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
628 xasprintf(&envp[3], "NAME=%s", myself->name);
631 execute_script("tinc-up", envp);
633 for(i = 0; i < 4; i++)
636 /* Run subnet-up scripts for our own subnets */
638 subnet_update(myself, NULL, true);
643 cfg = lookup_config(config_tree, "BindToAddress");
646 get_config_string(cfg, &address);
648 cfg = lookup_config_next(config_tree, cfg);
653 char *space = strchr(address, ' ');
659 if(!strcmp(address, "*"))
663 hint.ai_family = addressfamily;
664 hint.ai_socktype = SOCK_STREAM;
665 hint.ai_protocol = IPPROTO_TCP;
666 hint.ai_flags = AI_PASSIVE;
668 err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
672 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
677 for(aip = ai; aip; aip = aip->ai_next) {
678 if(listen_sockets >= MAXSOCKETS) {
679 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
683 listen_socket[listen_sockets].tcp =
684 setup_listen_socket((sockaddr_t *) aip->ai_addr);
686 if(listen_socket[listen_sockets].tcp < 0)
689 listen_socket[listen_sockets].udp =
690 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
692 if(listen_socket[listen_sockets].udp < 0) {
693 close(listen_socket[listen_sockets].tcp);
697 event_set(&listen_socket[listen_sockets].ev_tcp,
698 listen_socket[listen_sockets].tcp,
700 handle_new_meta_connection, NULL);
701 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
702 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
706 event_set(&listen_socket[listen_sockets].ev_udp,
707 listen_socket[listen_sockets].udp,
709 handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
710 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
711 logger(DEBUG_ALWAYS, LOG_ERR, "event_add failed: %s", strerror(errno));
715 if(debug_level >= DEBUG_CONNECTIONS) {
716 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
717 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
721 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
729 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
731 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
741 bool setup_network(void) {
748 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
749 if(pinginterval < 1) {
750 pinginterval = 86400;
755 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
757 if(pingtimeout < 1 || pingtimeout > pinginterval)
758 pingtimeout = pinginterval;
760 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
761 maxoutbufsize = 10 * MTU;
770 close all open network connections
772 void close_network_connections(void) {
773 splay_node_t *node, *next;
778 for(node = connection_tree->head; node; node = next) {
782 terminate_connection(c, false);
785 list_delete_list(outgoing_list);
787 if(myself && myself->connection) {
788 subnet_update(myself, NULL, false);
789 terminate_connection(myself->connection, false);
790 free_connection(myself->connection);
793 for(i = 0; i < listen_sockets; i++) {
794 event_del(&listen_socket[i].ev_tcp);
795 event_del(&listen_socket[i].ev_udp);
796 close(listen_socket[i].tcp);
797 close(listen_socket[i].udp);
800 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
801 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
802 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
803 xasprintf(&envp[3], "NAME=%s", myself->name);
812 execute_script("tinc-down", envp);
814 if(myport) free(myport);
816 for(i = 0; i < 4; i++)