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(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(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(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(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(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(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(LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
196 if(s.st_mode & ~0100700)
197 logger(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(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(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(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(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
252 if(s.st_mode & ~0100700)
253 logger(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(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 ifdebug(STATUS) logger(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;
297 xasprintf(&dname, "%s/hosts", confbase);
298 dir = opendir(dname);
300 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
305 while((ent = readdir(dir))) {
306 if(!check_id(ent->d_name))
309 n = lookup_node(ent->d_name);
310 #ifdef _DIRENT_HAVE_D_TYPE
311 //if(ent->d_type != DT_REG)
315 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
316 init_configuration(&config_tree);
317 result = read_config_file(config_tree, fname);
324 n->name = xstrdup(ent->d_name);
328 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
329 if(!get_config_subnet(cfg, &s))
332 if((s2 = lookup_subnet(n, s))) {
339 exit_configuration(&config_tree);
346 Configure node_t myself and set up the local sockets (listen only)
348 static bool setup_myself(void) {
351 char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
353 char *address = NULL;
355 struct addrinfo *ai, *aip, hint = {0};
361 myself->connection = new_connection();
363 myself->hostname = xstrdup("MYSELF");
364 myself->connection->hostname = xstrdup("MYSELF");
366 myself->connection->options = 0;
367 myself->connection->protocol_major = PROT_MAJOR;
368 myself->connection->protocol_minor = PROT_MINOR;
370 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
371 logger(LOG_ERR, "Name for tinc daemon required!");
375 if(!check_id(name)) {
376 logger(LOG_ERR, "Invalid name for myself!");
382 myself->connection->name = xstrdup(name);
383 xasprintf(&fname, "%s/hosts/%s", confbase, name);
384 read_config_options(config_tree, name);
385 read_config_file(config_tree, fname);
388 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
390 if(experimental && !read_ecdsa_private_key())
393 if(!read_rsa_private_key())
396 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
397 myport = xstrdup("655");
400 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
402 if(!ai || !ai->ai_addr)
405 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
406 sockaddr2str(&sa, NULL, &myport);
409 /* Read in all the subnets specified in the host configuration file */
411 cfg = lookup_config(config_tree, "Subnet");
414 if(!get_config_subnet(cfg, &subnet))
417 subnet_add(myself, subnet);
419 cfg = lookup_config_next(config_tree, cfg);
422 /* Check some options */
424 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
425 myself->options |= OPTION_INDIRECT;
427 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
428 myself->options |= OPTION_TCPONLY;
430 if(myself->options & OPTION_TCPONLY)
431 myself->options |= OPTION_INDIRECT;
433 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
434 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
435 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
436 strictsubnets |= tunnelserver;
438 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
439 if(!strcasecmp(mode, "router"))
440 routing_mode = RMODE_ROUTER;
441 else if(!strcasecmp(mode, "switch"))
442 routing_mode = RMODE_SWITCH;
443 else if(!strcasecmp(mode, "hub"))
444 routing_mode = RMODE_HUB;
446 logger(LOG_ERR, "Invalid routing mode!");
452 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
453 if(!strcasecmp(mode, "off"))
454 forwarding_mode = FMODE_OFF;
455 else if(!strcasecmp(mode, "internal"))
456 forwarding_mode = FMODE_INTERNAL;
457 else if(!strcasecmp(mode, "kernel"))
458 forwarding_mode = FMODE_KERNEL;
460 logger(LOG_ERR, "Invalid forwarding mode!");
467 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
469 myself->options |= OPTION_PMTU_DISCOVERY;
472 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
474 myself->options |= OPTION_CLAMP_MSS;
476 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
477 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
478 get_config_bool(lookup_config(config_tree, "Broadcast"), &broadcast);
480 #if !defined(SOL_IP) || !defined(IP_TOS)
481 if(priorityinheritance)
482 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
485 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
488 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
489 if(maxtimeout <= 0) {
490 logger(LOG_ERR, "Bogus maximum timeout!");
496 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
497 if(udp_rcvbuf <= 0) {
498 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
503 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
504 if(udp_sndbuf <= 0) {
505 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
510 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
511 if(replaywin_int < 0) {
512 logger(LOG_ERR, "ReplayWindow cannot be negative!");
515 replaywin = (unsigned)replaywin_int;
518 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
519 if(!strcasecmp(afname, "IPv4"))
520 addressfamily = AF_INET;
521 else if(!strcasecmp(afname, "IPv6"))
522 addressfamily = AF_INET6;
523 else if(!strcasecmp(afname, "any"))
524 addressfamily = AF_UNSPEC;
526 logger(LOG_ERR, "Invalid address family!");
532 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
534 /* Generate packet encryption key */
536 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
537 cipher = xstrdup("blowfish");
539 if(!cipher_open_by_name(&myself->incipher, cipher)) {
540 logger(LOG_ERR, "Unrecognized cipher type!");
544 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
549 /* Check if we want to use message authentication codes... */
552 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
555 logger(LOG_ERR, "Bogus MAC length!");
559 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
560 digest = xstrdup("sha1");
562 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
563 logger(LOG_ERR, "Unrecognized digest type!");
569 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
570 if(myself->incompression < 0 || myself->incompression > 11) {
571 logger(LOG_ERR, "Bogus compression level!");
575 myself->incompression = 0;
577 myself->connection->outcompression = 0;
581 myself->nexthop = myself;
582 myself->via = myself;
583 myself->status.reachable = true;
595 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
596 if(!strcasecmp(type, "dummy"))
597 devops = dummy_devops;
598 else if(!strcasecmp(type, "raw_socket"))
599 devops = raw_socket_devops;
601 else if(!strcasecmp(type, "uml"))
605 else if(!strcasecmp(type, "vde"))
614 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
616 if (event_add(&device_ev, NULL) < 0) {
617 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
623 /* Run tinc-up script to further initialize the tap interface */
624 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
625 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
626 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
627 xasprintf(&envp[3], "NAME=%s", myself->name);
630 execute_script("tinc-up", envp);
632 for(i = 0; i < 4; i++)
635 /* Run subnet-up scripts for our own subnets */
637 subnet_update(myself, NULL, true);
642 cfg = lookup_config(config_tree, "BindToAddress");
645 get_config_string(cfg, &address);
647 cfg = lookup_config_next(config_tree, cfg);
649 hint.ai_family = addressfamily;
650 hint.ai_socktype = SOCK_STREAM;
651 hint.ai_protocol = IPPROTO_TCP;
652 hint.ai_flags = AI_PASSIVE;
654 err = getaddrinfo(address, myport, &hint, &ai);
658 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
663 for(aip = ai; aip; aip = aip->ai_next) {
664 if(listen_sockets >= MAXSOCKETS) {
665 logger(LOG_ERR, "Too many listening sockets");
669 listen_socket[listen_sockets].tcp =
670 setup_listen_socket((sockaddr_t *) aip->ai_addr);
672 if(listen_socket[listen_sockets].tcp < 0)
675 listen_socket[listen_sockets].udp =
676 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
678 if(listen_socket[listen_sockets].udp < 0) {
679 close(listen_socket[listen_sockets].tcp);
683 event_set(&listen_socket[listen_sockets].ev_tcp,
684 listen_socket[listen_sockets].tcp,
686 handle_new_meta_connection, NULL);
687 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
688 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
692 event_set(&listen_socket[listen_sockets].ev_udp,
693 listen_socket[listen_sockets].udp,
695 handle_incoming_vpn_data, (void *)(intptr_t)listen_sockets);
696 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
697 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
701 ifdebug(CONNECTIONS) {
702 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
703 logger(LOG_NOTICE, "Listening on %s", hostname);
707 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
715 logger(LOG_NOTICE, "Ready");
717 logger(LOG_ERR, "Unable to create any listening socket!");
727 bool setup_network(void) {
734 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
735 if(pinginterval < 1) {
736 pinginterval = 86400;
741 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
743 if(pingtimeout < 1 || pingtimeout > pinginterval)
744 pingtimeout = pinginterval;
746 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
747 maxoutbufsize = 10 * MTU;
756 close all open network connections
758 void close_network_connections(void) {
759 splay_node_t *node, *next;
764 for(node = connection_tree->head; node; node = next) {
768 terminate_connection(c, false);
771 list_delete_list(outgoing_list);
773 if(myself && myself->connection) {
774 subnet_update(myself, NULL, false);
775 terminate_connection(myself->connection, false);
776 free_connection(myself->connection);
779 for(i = 0; i < listen_sockets; i++) {
780 event_del(&listen_socket[i].ev_tcp);
781 event_del(&listen_socket[i].ev_udp);
782 close(listen_socket[i].tcp);
783 close(listen_socket[i].udp);
786 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
787 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
788 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
789 xasprintf(&envp[3], "NAME=%s", myself->name);
798 execute_script("tinc-down", envp);
800 if(myport) free(myport);
802 for(i = 0; i < 4; i++)