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 read_ecdsa_public_key(connection_t *c) {
54 /* First, check for simple ECDSAPublicKey statement */
56 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
57 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
62 /* Else, check for ECDSAPublicKeyFile statement and read it */
64 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
65 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
67 fp = fopen(fname, "r");
70 logger(LOG_ERR, "Error reading ECDSA public key file `%s': %s",
71 fname, strerror(errno));
76 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
80 logger(LOG_ERR, "Reading ECDSA public key file `%s' failed: %s", fname, strerror(errno));
85 bool read_rsa_public_key(connection_t *c) {
91 /* First, check for simple PublicKey statement */
93 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
94 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
99 /* Else, check for PublicKeyFile statement and read it */
101 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
102 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
104 fp = fopen(fname, "r");
107 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
108 fname, strerror(errno));
113 result = rsa_read_pem_public_key(&c->rsa, fp);
117 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
122 static bool read_ecdsa_private_key(void) {
127 /* Check for PrivateKeyFile statement and read it */
129 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
130 xasprintf(&fname, "%s/ecdsa_key.priv", confbase);
132 fp = fopen(fname, "r");
135 logger(LOG_ERR, "Error reading ECDSA private key file `%s': %s",
136 fname, strerror(errno));
141 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
144 if(fstat(fileno(fp), &s)) {
145 logger(LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
150 if(s.st_mode & ~0100700)
151 logger(LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
154 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
158 logger(LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
163 static bool read_rsa_private_key(void) {
169 /* First, check for simple PrivateKey statement */
171 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
172 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
173 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
177 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
183 /* Else, check for PrivateKeyFile statement and read it */
185 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
186 xasprintf(&fname, "%s/rsa_key.priv", confbase);
188 fp = fopen(fname, "r");
191 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
192 fname, strerror(errno));
197 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
200 if(fstat(fileno(fp), &s)) {
201 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
206 if(s.st_mode & ~0100700)
207 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
210 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
214 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
219 static struct event keyexpire_event;
221 static void keyexpire_handler(int fd, short events, void *data) {
225 void regenerate_key(void) {
226 if(timeout_initialized(&keyexpire_event)) {
227 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
228 event_del(&keyexpire_event);
231 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
234 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
238 Read Subnets from all host config files
240 void load_all_subnets(void) {
245 splay_tree_t *config_tree;
251 xasprintf(&dname, "%s/hosts", confbase);
252 dir = opendir(dname);
254 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
259 while((ent = readdir(dir))) {
260 if(!check_id(ent->d_name))
263 n = lookup_node(ent->d_name);
264 #ifdef _DIRENT_HAVE_D_TYPE
265 //if(ent->d_type != DT_REG)
269 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
270 init_configuration(&config_tree);
271 result = read_config_file(config_tree, fname);
278 n->name = xstrdup(ent->d_name);
282 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
283 if(!get_config_subnet(cfg, &s))
286 if((s2 = lookup_subnet(n, s))) {
293 exit_configuration(&config_tree);
300 Configure node_t myself and set up the local sockets (listen only)
302 static bool setup_myself(void) {
305 char *name, *hostname, *mode, *afname, *cipher, *digest;
307 char *address = NULL;
309 struct addrinfo *ai, *aip, hint = {0};
315 myself->connection = new_connection();
317 myself->hostname = xstrdup("MYSELF");
318 myself->connection->hostname = xstrdup("MYSELF");
320 myself->connection->options = 0;
321 myself->connection->protocol_major = PROT_MAJOR;
322 myself->connection->protocol_minor = PROT_MINOR;
324 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
325 logger(LOG_ERR, "Name for tinc daemon required!");
329 if(!check_id(name)) {
330 logger(LOG_ERR, "Invalid name for myself!");
336 myself->connection->name = xstrdup(name);
337 xasprintf(&fname, "%s/hosts/%s", confbase, name);
338 read_config_options(config_tree, name);
339 read_config_file(config_tree, fname);
342 if(!read_ecdsa_private_key())
345 if(!read_rsa_private_key())
348 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
349 myport = xstrdup("655");
352 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
354 if(!ai || !ai->ai_addr)
357 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
358 sockaddr2str(&sa, NULL, &myport);
361 /* Read in all the subnets specified in the host configuration file */
363 cfg = lookup_config(config_tree, "Subnet");
366 if(!get_config_subnet(cfg, &subnet))
369 subnet_add(myself, subnet);
371 cfg = lookup_config_next(config_tree, cfg);
374 /* Check some options */
376 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
377 myself->options |= OPTION_INDIRECT;
379 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
380 myself->options |= OPTION_TCPONLY;
382 if(myself->options & OPTION_TCPONLY)
383 myself->options |= OPTION_INDIRECT;
385 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
386 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
387 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
388 strictsubnets |= tunnelserver;
390 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
391 if(!strcasecmp(mode, "router"))
392 routing_mode = RMODE_ROUTER;
393 else if(!strcasecmp(mode, "switch"))
394 routing_mode = RMODE_SWITCH;
395 else if(!strcasecmp(mode, "hub"))
396 routing_mode = RMODE_HUB;
398 logger(LOG_ERR, "Invalid routing mode!");
404 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
405 if(!strcasecmp(mode, "off"))
406 forwarding_mode = FMODE_OFF;
407 else if(!strcasecmp(mode, "internal"))
408 forwarding_mode = FMODE_INTERNAL;
409 else if(!strcasecmp(mode, "kernel"))
410 forwarding_mode = FMODE_KERNEL;
412 logger(LOG_ERR, "Invalid forwarding mode!");
419 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
421 myself->options |= OPTION_PMTU_DISCOVERY;
424 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
426 myself->options |= OPTION_CLAMP_MSS;
428 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
430 #if !defined(SOL_IP) || !defined(IP_TOS)
431 if(priorityinheritance)
432 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
435 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
438 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
439 if(maxtimeout <= 0) {
440 logger(LOG_ERR, "Bogus maximum timeout!");
446 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
447 if(udp_rcvbuf <= 0) {
448 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
453 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
454 if(udp_sndbuf <= 0) {
455 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
460 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
461 if(replaywin_int < 0) {
462 logger(LOG_ERR, "ReplayWindow cannot be negative!");
465 replaywin = (unsigned)replaywin_int;
468 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
469 if(!strcasecmp(afname, "IPv4"))
470 addressfamily = AF_INET;
471 else if(!strcasecmp(afname, "IPv6"))
472 addressfamily = AF_INET6;
473 else if(!strcasecmp(afname, "any"))
474 addressfamily = AF_UNSPEC;
476 logger(LOG_ERR, "Invalid address family!");
482 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
484 /* Generate packet encryption key */
486 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
487 cipher = xstrdup("blowfish");
489 if(!cipher_open_by_name(&myself->incipher, cipher)) {
490 logger(LOG_ERR, "Unrecognized cipher type!");
494 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
499 /* Check if we want to use message authentication codes... */
501 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
502 digest = xstrdup("sha1");
505 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
508 logger(LOG_ERR, "Bogus MAC length!");
512 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
513 logger(LOG_ERR, "Unrecognized digest type!");
519 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
520 if(myself->incompression < 0 || myself->incompression > 11) {
521 logger(LOG_ERR, "Bogus compression level!");
525 myself->incompression = 0;
527 myself->connection->outcompression = 0;
531 myself->nexthop = myself;
532 myself->via = myself;
533 myself->status.reachable = true;
547 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
549 if (event_add(&device_ev, NULL) < 0) {
550 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
556 /* Run tinc-up script to further initialize the tap interface */
557 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
558 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
559 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
560 xasprintf(&envp[3], "NAME=%s", myself->name);
563 execute_script("tinc-up", envp);
565 for(i = 0; i < 4; i++)
568 /* Run subnet-up scripts for our own subnets */
570 subnet_update(myself, NULL, true);
574 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
576 hint.ai_family = addressfamily;
577 hint.ai_socktype = SOCK_STREAM;
578 hint.ai_protocol = IPPROTO_TCP;
579 hint.ai_flags = AI_PASSIVE;
581 err = getaddrinfo(address, myport, &hint, &ai);
584 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
591 for(aip = ai; aip; aip = aip->ai_next) {
592 listen_socket[listen_sockets].tcp =
593 setup_listen_socket((sockaddr_t *) aip->ai_addr);
595 if(listen_socket[listen_sockets].tcp < 0)
598 listen_socket[listen_sockets].udp =
599 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
601 if(listen_socket[listen_sockets].udp < 0) {
602 close(listen_socket[listen_sockets].tcp);
606 event_set(&listen_socket[listen_sockets].ev_tcp,
607 listen_socket[listen_sockets].tcp,
609 handle_new_meta_connection, NULL);
610 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
611 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
615 event_set(&listen_socket[listen_sockets].ev_udp,
616 listen_socket[listen_sockets].udp,
618 handle_incoming_vpn_data, NULL);
619 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
620 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
624 ifdebug(CONNECTIONS) {
625 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
626 logger(LOG_NOTICE, "Listening on %s", hostname);
630 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
633 if(listen_sockets >= MAXSOCKETS) {
634 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
642 logger(LOG_NOTICE, "Ready");
644 logger(LOG_ERR, "Unable to create any listening socket!");
654 bool setup_network(void) {
661 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
662 if(pinginterval < 1) {
663 pinginterval = 86400;
668 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
670 if(pingtimeout < 1 || pingtimeout > pinginterval)
671 pingtimeout = pinginterval;
673 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
674 maxoutbufsize = 10 * MTU;
683 close all open network connections
685 void close_network_connections(void) {
686 splay_node_t *node, *next;
691 for(node = connection_tree->head; node; node = next) {
695 terminate_connection(c, false);
698 list_delete_list(outgoing_list);
700 if(myself && myself->connection) {
701 subnet_update(myself, NULL, false);
702 terminate_connection(myself->connection, false);
703 free_connection(myself->connection);
706 for(i = 0; i < listen_sockets; i++) {
707 event_del(&listen_socket[i].ev_tcp);
708 event_del(&listen_socket[i].ev_udp);
709 close(listen_socket[i].tcp);
710 close(listen_socket[i].udp);
713 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
714 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
715 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
716 xasprintf(&envp[3], "NAME=%s", myself->name);
725 execute_script("tinc-down", envp);
727 if(myport) free(myport);
729 for(i = 0; i < 4; i++)