3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 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;
48 bool node_read_ecdsa_public_key(node_t *n) {
49 if(ecdsa_active(&n->ecdsa))
52 splay_tree_t *config_tree;
58 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
60 init_configuration(&config_tree);
61 if(!read_config_file(config_tree, fname))
64 /* First, check for simple ECDSAPublicKey statement */
66 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
67 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
72 /* Else, check for ECDSAPublicKeyFile statement and read it */
76 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &fname))
77 xasprintf(&fname, "%s/hosts/%s", confbase, n->name);
79 fp = fopen(fname, "r");
82 logger(LOG_ERR, "Error reading ECDSA public key file `%s': %s", fname, strerror(errno));
86 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
90 exit_configuration(&config_tree);
95 bool read_ecdsa_public_key(connection_t *c) {
101 /* First, check for simple ECDSAPublicKey statement */
103 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
104 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
109 /* Else, check for ECDSAPublicKeyFile statement and read it */
111 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
112 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
114 fp = fopen(fname, "r");
117 logger(LOG_ERR, "Error reading ECDSA public key file `%s': %s",
118 fname, strerror(errno));
123 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
127 logger(LOG_ERR, "Reading ECDSA public key file `%s' failed: %s", fname, strerror(errno));
132 bool read_rsa_public_key(connection_t *c) {
138 /* First, check for simple PublicKey statement */
140 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
141 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
146 /* Else, check for PublicKeyFile statement and read it */
148 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
149 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
151 fp = fopen(fname, "r");
154 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
155 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",
183 fname, strerror(errno));
188 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
191 if(fstat(fileno(fp), &s)) {
192 logger(LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
197 if(s.st_mode & ~0100700)
198 logger(LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
201 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
205 logger(LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
210 static bool read_rsa_private_key(void) {
216 /* First, check for simple PrivateKey statement */
218 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
219 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
220 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
224 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
230 /* Else, check for PrivateKeyFile statement and read it */
232 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
233 xasprintf(&fname, "%s/rsa_key.priv", confbase);
235 fp = fopen(fname, "r");
238 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
239 fname, strerror(errno));
244 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
247 if(fstat(fileno(fp), &s)) {
248 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
253 if(s.st_mode & ~0100700)
254 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
257 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
261 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
266 static struct event keyexpire_event;
268 static void keyexpire_handler(int fd, short events, void *data) {
272 void regenerate_key(void) {
273 if(timeout_initialized(&keyexpire_event)) {
274 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
275 event_del(&keyexpire_event);
278 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
281 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
285 Read Subnets from all host config files
287 void load_all_subnets(void) {
292 splay_tree_t *config_tree;
298 xasprintf(&dname, "%s/hosts", confbase);
299 dir = opendir(dname);
301 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
306 while((ent = readdir(dir))) {
307 if(!check_id(ent->d_name))
310 n = lookup_node(ent->d_name);
311 #ifdef _DIRENT_HAVE_D_TYPE
312 //if(ent->d_type != DT_REG)
316 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
317 init_configuration(&config_tree);
318 result = read_config_file(config_tree, fname);
325 n->name = xstrdup(ent->d_name);
329 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
330 if(!get_config_subnet(cfg, &s))
333 if((s2 = lookup_subnet(n, s))) {
340 exit_configuration(&config_tree);
347 Configure node_t myself and set up the local sockets (listen only)
349 static bool setup_myself(void) {
352 char *name, *hostname, *mode, *afname, *cipher, *digest;
354 char *address = NULL;
356 struct addrinfo *ai, *aip, hint = {0};
362 myself->connection = new_connection();
364 myself->hostname = xstrdup("MYSELF");
365 myself->connection->hostname = xstrdup("MYSELF");
367 myself->connection->options = 0;
368 myself->connection->protocol_major = PROT_MAJOR;
369 myself->connection->protocol_minor = PROT_MINOR;
371 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
372 logger(LOG_ERR, "Name for tinc daemon required!");
376 if(!check_id(name)) {
377 logger(LOG_ERR, "Invalid name for myself!");
383 myself->connection->name = xstrdup(name);
384 xasprintf(&fname, "%s/hosts/%s", confbase, name);
385 read_config_options(config_tree, name);
386 read_config_file(config_tree, fname);
389 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
391 if(experimental && !read_ecdsa_private_key())
394 if(!read_rsa_private_key())
397 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
398 myport = xstrdup("655");
401 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
403 if(!ai || !ai->ai_addr)
406 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
407 sockaddr2str(&sa, NULL, &myport);
410 /* Read in all the subnets specified in the host configuration file */
412 cfg = lookup_config(config_tree, "Subnet");
415 if(!get_config_subnet(cfg, &subnet))
418 subnet_add(myself, subnet);
420 cfg = lookup_config_next(config_tree, cfg);
423 /* Check some options */
425 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
426 myself->options |= OPTION_INDIRECT;
428 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
429 myself->options |= OPTION_TCPONLY;
431 if(myself->options & OPTION_TCPONLY)
432 myself->options |= OPTION_INDIRECT;
434 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
435 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
436 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
437 strictsubnets |= tunnelserver;
439 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
440 if(!strcasecmp(mode, "router"))
441 routing_mode = RMODE_ROUTER;
442 else if(!strcasecmp(mode, "switch"))
443 routing_mode = RMODE_SWITCH;
444 else if(!strcasecmp(mode, "hub"))
445 routing_mode = RMODE_HUB;
447 logger(LOG_ERR, "Invalid routing mode!");
453 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
454 if(!strcasecmp(mode, "off"))
455 forwarding_mode = FMODE_OFF;
456 else if(!strcasecmp(mode, "internal"))
457 forwarding_mode = FMODE_INTERNAL;
458 else if(!strcasecmp(mode, "kernel"))
459 forwarding_mode = FMODE_KERNEL;
461 logger(LOG_ERR, "Invalid forwarding mode!");
468 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
470 myself->options |= OPTION_PMTU_DISCOVERY;
473 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
475 myself->options |= OPTION_CLAMP_MSS;
477 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
479 #if !defined(SOL_IP) || !defined(IP_TOS)
480 if(priorityinheritance)
481 logger(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(LOG_ERR, "Bogus maximum timeout!");
495 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
496 if(udp_rcvbuf <= 0) {
497 logger(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(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(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(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(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... */
550 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
551 digest = xstrdup("sha1");
554 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
557 logger(LOG_ERR, "Bogus MAC length!");
561 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
562 logger(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(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;
596 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
598 if (event_add(&device_ev, NULL) < 0) {
599 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
605 /* Run tinc-up script to further initialize the tap interface */
606 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
607 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
608 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
609 xasprintf(&envp[3], "NAME=%s", myself->name);
612 execute_script("tinc-up", envp);
614 for(i = 0; i < 4; i++)
617 /* Run subnet-up scripts for our own subnets */
619 subnet_update(myself, NULL, true);
623 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
625 hint.ai_family = addressfamily;
626 hint.ai_socktype = SOCK_STREAM;
627 hint.ai_protocol = IPPROTO_TCP;
628 hint.ai_flags = AI_PASSIVE;
630 err = getaddrinfo(address, myport, &hint, &ai);
633 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
640 for(aip = ai; aip; aip = aip->ai_next) {
641 listen_socket[listen_sockets].tcp =
642 setup_listen_socket((sockaddr_t *) aip->ai_addr);
644 if(listen_socket[listen_sockets].tcp < 0)
647 listen_socket[listen_sockets].udp =
648 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
650 if(listen_socket[listen_sockets].udp < 0) {
651 close(listen_socket[listen_sockets].tcp);
655 event_set(&listen_socket[listen_sockets].ev_tcp,
656 listen_socket[listen_sockets].tcp,
658 handle_new_meta_connection, NULL);
659 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
660 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
664 event_set(&listen_socket[listen_sockets].ev_udp,
665 listen_socket[listen_sockets].udp,
667 handle_incoming_vpn_data, NULL);
668 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
669 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
673 ifdebug(CONNECTIONS) {
674 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
675 logger(LOG_NOTICE, "Listening on %s", hostname);
679 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
682 if(listen_sockets >= MAXSOCKETS) {
683 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
691 logger(LOG_NOTICE, "Ready");
693 logger(LOG_ERR, "Unable to create any listening socket!");
703 bool setup_network(void) {
710 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
711 if(pinginterval < 1) {
712 pinginterval = 86400;
717 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
719 if(pingtimeout < 1 || pingtimeout > pinginterval)
720 pingtimeout = pinginterval;
722 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
723 maxoutbufsize = 10 * MTU;
732 close all open network connections
734 void close_network_connections(void) {
735 splay_node_t *node, *next;
740 for(node = connection_tree->head; node; node = next) {
744 terminate_connection(c, false);
747 list_delete_list(outgoing_list);
749 if(myself && myself->connection) {
750 subnet_update(myself, NULL, false);
751 terminate_connection(myself->connection, false);
752 free_connection(myself->connection);
755 for(i = 0; i < listen_sockets; i++) {
756 event_del(&listen_socket[i].ev_tcp);
757 event_del(&listen_socket[i].ev_udp);
758 close(listen_socket[i].tcp);
759 close(listen_socket[i].udp);
762 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
763 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
764 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
765 xasprintf(&envp[3], "NAME=%s", myself->name);
774 execute_script("tinc-down", envp);
776 if(myport) free(myport);
778 for(i = 0; i < 4; i++)