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 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
344 if(experimental && !read_ecdsa_private_key())
347 if(!read_rsa_private_key())
350 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
351 myport = xstrdup("655");
354 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
356 if(!ai || !ai->ai_addr)
359 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
360 sockaddr2str(&sa, NULL, &myport);
363 /* Read in all the subnets specified in the host configuration file */
365 cfg = lookup_config(config_tree, "Subnet");
368 if(!get_config_subnet(cfg, &subnet))
371 subnet_add(myself, subnet);
373 cfg = lookup_config_next(config_tree, cfg);
376 /* Check some options */
378 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
379 myself->options |= OPTION_INDIRECT;
381 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
382 myself->options |= OPTION_TCPONLY;
384 if(myself->options & OPTION_TCPONLY)
385 myself->options |= OPTION_INDIRECT;
387 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
388 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
389 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
390 strictsubnets |= tunnelserver;
392 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
393 if(!strcasecmp(mode, "router"))
394 routing_mode = RMODE_ROUTER;
395 else if(!strcasecmp(mode, "switch"))
396 routing_mode = RMODE_SWITCH;
397 else if(!strcasecmp(mode, "hub"))
398 routing_mode = RMODE_HUB;
400 logger(LOG_ERR, "Invalid routing mode!");
406 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
407 if(!strcasecmp(mode, "off"))
408 forwarding_mode = FMODE_OFF;
409 else if(!strcasecmp(mode, "internal"))
410 forwarding_mode = FMODE_INTERNAL;
411 else if(!strcasecmp(mode, "kernel"))
412 forwarding_mode = FMODE_KERNEL;
414 logger(LOG_ERR, "Invalid forwarding mode!");
421 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
423 myself->options |= OPTION_PMTU_DISCOVERY;
426 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
428 myself->options |= OPTION_CLAMP_MSS;
430 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
432 #if !defined(SOL_IP) || !defined(IP_TOS)
433 if(priorityinheritance)
434 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
437 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
440 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
441 if(maxtimeout <= 0) {
442 logger(LOG_ERR, "Bogus maximum timeout!");
448 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
449 if(udp_rcvbuf <= 0) {
450 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
455 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
456 if(udp_sndbuf <= 0) {
457 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
462 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
463 if(replaywin_int < 0) {
464 logger(LOG_ERR, "ReplayWindow cannot be negative!");
467 replaywin = (unsigned)replaywin_int;
470 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
471 if(!strcasecmp(afname, "IPv4"))
472 addressfamily = AF_INET;
473 else if(!strcasecmp(afname, "IPv6"))
474 addressfamily = AF_INET6;
475 else if(!strcasecmp(afname, "any"))
476 addressfamily = AF_UNSPEC;
478 logger(LOG_ERR, "Invalid address family!");
484 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
486 /* Generate packet encryption key */
488 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
489 cipher = xstrdup("blowfish");
491 if(!cipher_open_by_name(&myself->incipher, cipher)) {
492 logger(LOG_ERR, "Unrecognized cipher type!");
496 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
501 /* Check if we want to use message authentication codes... */
503 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
504 digest = xstrdup("sha1");
507 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
510 logger(LOG_ERR, "Bogus MAC length!");
514 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
515 logger(LOG_ERR, "Unrecognized digest type!");
521 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
522 if(myself->incompression < 0 || myself->incompression > 11) {
523 logger(LOG_ERR, "Bogus compression level!");
527 myself->incompression = 0;
529 myself->connection->outcompression = 0;
533 myself->nexthop = myself;
534 myself->via = myself;
535 myself->status.reachable = true;
549 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
551 if (event_add(&device_ev, NULL) < 0) {
552 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
558 /* Run tinc-up script to further initialize the tap interface */
559 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
560 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
561 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
562 xasprintf(&envp[3], "NAME=%s", myself->name);
565 execute_script("tinc-up", envp);
567 for(i = 0; i < 4; i++)
570 /* Run subnet-up scripts for our own subnets */
572 subnet_update(myself, NULL, true);
576 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
578 hint.ai_family = addressfamily;
579 hint.ai_socktype = SOCK_STREAM;
580 hint.ai_protocol = IPPROTO_TCP;
581 hint.ai_flags = AI_PASSIVE;
583 err = getaddrinfo(address, myport, &hint, &ai);
586 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
593 for(aip = ai; aip; aip = aip->ai_next) {
594 listen_socket[listen_sockets].tcp =
595 setup_listen_socket((sockaddr_t *) aip->ai_addr);
597 if(listen_socket[listen_sockets].tcp < 0)
600 listen_socket[listen_sockets].udp =
601 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
603 if(listen_socket[listen_sockets].udp < 0) {
604 close(listen_socket[listen_sockets].tcp);
608 event_set(&listen_socket[listen_sockets].ev_tcp,
609 listen_socket[listen_sockets].tcp,
611 handle_new_meta_connection, NULL);
612 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
613 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
617 event_set(&listen_socket[listen_sockets].ev_udp,
618 listen_socket[listen_sockets].udp,
620 handle_incoming_vpn_data, NULL);
621 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
622 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
626 ifdebug(CONNECTIONS) {
627 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
628 logger(LOG_NOTICE, "Listening on %s", hostname);
632 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
635 if(listen_sockets >= MAXSOCKETS) {
636 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
644 logger(LOG_NOTICE, "Ready");
646 logger(LOG_ERR, "Unable to create any listening socket!");
656 bool setup_network(void) {
663 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
664 if(pinginterval < 1) {
665 pinginterval = 86400;
670 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
672 if(pingtimeout < 1 || pingtimeout > pinginterval)
673 pingtimeout = pinginterval;
675 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
676 maxoutbufsize = 10 * MTU;
685 close all open network connections
687 void close_network_connections(void) {
688 splay_node_t *node, *next;
693 for(node = connection_tree->head; node; node = next) {
697 terminate_connection(c, false);
700 list_delete_list(outgoing_list);
702 if(myself && myself->connection) {
703 subnet_update(myself, NULL, false);
704 terminate_connection(myself->connection, false);
705 free_connection(myself->connection);
708 for(i = 0; i < listen_sockets; i++) {
709 event_del(&listen_socket[i].ev_tcp);
710 event_del(&listen_socket[i].ev_udp);
711 close(listen_socket[i].tcp);
712 close(listen_socket[i].udp);
715 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
716 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
717 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
718 xasprintf(&envp[3], "NAME=%s", myself->name);
727 execute_script("tinc-down", envp);
729 if(myport) free(myport);
731 for(i = 0; i < 4; i++)