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 <openssl/pem.h>
26 #include <openssl/rsa.h>
27 #include <openssl/rand.h>
28 #include <openssl/err.h>
29 #include <openssl/evp.h>
33 #include "connection.h"
49 bool read_rsa_public_key(connection_t *c) {
55 c->rsa_key = RSA_new();
56 // RSA_blinding_on(c->rsa_key, NULL);
59 /* First, check for simple PublicKey statement */
61 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
62 BN_hex2bn(&c->rsa_key->n, key);
63 BN_hex2bn(&c->rsa_key->e, "FFFF");
68 /* Else, check for PublicKeyFile statement and read it */
70 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
71 fp = fopen(fname, "r");
74 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
75 fname, strerror(errno));
81 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
85 return true; /* Woohoo. */
87 /* If it fails, try PEM_read_RSA_PUBKEY. */
88 fp = fopen(fname, "r");
91 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
92 fname, strerror(errno));
98 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
102 // RSA_blinding_on(c->rsa_key, NULL);
106 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s",
107 fname, strerror(errno));
111 /* Else, check if a harnessed public key is in the config file */
113 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
114 fp = fopen(fname, "r");
117 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
126 /* Try again with PEM_read_RSA_PUBKEY. */
128 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
129 fp = fopen(fname, "r");
132 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
133 // RSA_blinding_on(c->rsa_key, NULL);
142 logger(LOG_ERR, "No public key for %s specified!", c->name);
147 bool read_rsa_private_key(void) {
149 char *fname, *key, *pubkey;
152 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
153 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
154 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
157 myself->connection->rsa_key = RSA_new();
158 // RSA_blinding_on(myself->connection->rsa_key, NULL);
159 BN_hex2bn(&myself->connection->rsa_key->d, key);
160 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
161 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
167 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
168 xasprintf(&fname, "%s/rsa_key.priv", confbase);
170 fp = fopen(fname, "r");
173 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
174 fname, strerror(errno));
179 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
180 if(fstat(fileno(fp), &s)) {
181 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
182 fname, strerror(errno));
187 if(s.st_mode & ~0100700)
188 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
191 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
194 if(!myself->connection->rsa_key) {
195 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
196 fname, strerror(errno));
206 Read Subnets from all host config files
208 void load_all_subnets(void) {
213 avl_tree_t *config_tree;
219 xasprintf(&dname, "%s/hosts", confbase);
220 dir = opendir(dname);
222 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
227 while((ent = readdir(dir))) {
228 if(!check_id(ent->d_name))
231 n = lookup_node(ent->d_name);
232 #ifdef _DIRENT_HAVE_D_TYPE
233 //if(ent->d_type != DT_REG)
237 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
238 init_configuration(&config_tree);
239 result = read_config_file(config_tree, fname);
246 n->name = xstrdup(ent->d_name);
250 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
251 if(!get_config_subnet(cfg, &s))
254 if((s2 = lookup_subnet(n, s))) {
261 exit_configuration(&config_tree);
268 Configure node_t myself and set up the local sockets (listen only)
270 bool setup_myself(void) {
273 char *name, *hostname, *mode, *afname, *cipher, *digest;
275 char *address = NULL;
277 struct addrinfo *ai, *aip, hint = {0};
283 myself->connection = new_connection();
285 myself->hostname = xstrdup("MYSELF");
286 myself->connection->hostname = xstrdup("MYSELF");
288 myself->connection->options = 0;
289 myself->connection->protocol_version = PROT_CURRENT;
291 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
292 logger(LOG_ERR, "Name for tinc daemon required!");
296 if(!check_id(name)) {
297 logger(LOG_ERR, "Invalid name for myself!");
303 myself->connection->name = xstrdup(name);
304 xasprintf(&fname, "%s/hosts/%s", confbase, name);
305 read_config_options(config_tree, name);
306 read_config_file(config_tree, fname);
309 if(!read_rsa_private_key())
312 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
313 myport = xstrdup("655");
316 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
318 if(!ai || !ai->ai_addr)
321 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
322 sockaddr2str(&sa, NULL, &myport);
325 /* Read in all the subnets specified in the host configuration file */
327 cfg = lookup_config(config_tree, "Subnet");
330 if(!get_config_subnet(cfg, &subnet))
333 subnet_add(myself, subnet);
335 cfg = lookup_config_next(config_tree, cfg);
338 /* Check some options */
340 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
341 myself->options |= OPTION_INDIRECT;
343 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
344 myself->options |= OPTION_TCPONLY;
346 if(myself->options & OPTION_TCPONLY)
347 myself->options |= OPTION_INDIRECT;
349 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
350 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
351 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
352 strictsubnets |= tunnelserver;
354 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
355 if(!strcasecmp(mode, "router"))
356 routing_mode = RMODE_ROUTER;
357 else if(!strcasecmp(mode, "switch"))
358 routing_mode = RMODE_SWITCH;
359 else if(!strcasecmp(mode, "hub"))
360 routing_mode = RMODE_HUB;
362 logger(LOG_ERR, "Invalid routing mode!");
368 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
369 if(!strcasecmp(mode, "off"))
370 forwarding_mode = FMODE_OFF;
371 else if(!strcasecmp(mode, "internal"))
372 forwarding_mode = FMODE_INTERNAL;
373 else if(!strcasecmp(mode, "kernel"))
374 forwarding_mode = FMODE_KERNEL;
376 logger(LOG_ERR, "Invalid forwarding mode!");
383 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
385 myself->options |= OPTION_PMTU_DISCOVERY;
388 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
390 myself->options |= OPTION_CLAMP_MSS;
392 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
394 #if !defined(SOL_IP) || !defined(IP_TOS)
395 if(priorityinheritance)
396 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
399 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
402 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
403 if(maxtimeout <= 0) {
404 logger(LOG_ERR, "Bogus maximum timeout!");
410 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
411 if(udp_rcvbuf <= 0) {
412 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
417 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
418 if(udp_sndbuf <= 0) {
419 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
424 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
425 if(replaywin_int < 0) {
426 logger(LOG_ERR, "ReplayWindow cannot be negative!");
429 replaywin = (unsigned)replaywin_int;
432 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
433 if(!strcasecmp(afname, "IPv4"))
434 addressfamily = AF_INET;
435 else if(!strcasecmp(afname, "IPv6"))
436 addressfamily = AF_INET6;
437 else if(!strcasecmp(afname, "any"))
438 addressfamily = AF_UNSPEC;
440 logger(LOG_ERR, "Invalid address family!");
446 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
448 /* Generate packet encryption key */
451 (lookup_config(config_tree, "Cipher"), &cipher)) {
452 if(!strcasecmp(cipher, "none")) {
453 myself->incipher = NULL;
455 myself->incipher = EVP_get_cipherbyname(cipher);
457 if(!myself->incipher) {
458 logger(LOG_ERR, "Unrecognized cipher type!");
463 myself->incipher = EVP_bf_cbc();
466 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
468 myself->inkeylength = 1;
470 myself->connection->outcipher = EVP_bf_ofb();
472 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
475 keyexpires = now + keylifetime;
477 /* Check if we want to use message authentication codes... */
479 if(get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
480 if(!strcasecmp(digest, "none")) {
481 myself->indigest = NULL;
483 myself->indigest = EVP_get_digestbyname(digest);
485 if(!myself->indigest) {
486 logger(LOG_ERR, "Unrecognized digest type!");
491 myself->indigest = EVP_sha1();
493 myself->connection->outdigest = EVP_sha1();
495 if(get_config_int(lookup_config(config_tree, "MACLength"), &myself->inmaclength)) {
496 if(myself->indigest) {
497 if(myself->inmaclength > myself->indigest->md_size) {
498 logger(LOG_ERR, "MAC length exceeds size of digest!");
500 } else if(myself->inmaclength < 0) {
501 logger(LOG_ERR, "Bogus MAC length!");
506 myself->inmaclength = 4;
508 myself->connection->outmaclength = 0;
512 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
513 if(myself->incompression < 0 || myself->incompression > 11) {
514 logger(LOG_ERR, "Bogus compression level!");
518 myself->incompression = 0;
520 myself->connection->outcompression = 0;
524 myself->nexthop = myself;
525 myself->via = myself;
526 myself->status.reachable = true;
539 /* Run tinc-up script to further initialize the tap interface */
540 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
541 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
542 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
543 xasprintf(&envp[3], "NAME=%s", myself->name);
546 execute_script("tinc-up", envp);
548 for(i = 0; i < 5; i++)
551 /* Run subnet-up scripts for our own subnets */
553 subnet_update(myself, NULL, true);
557 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
559 hint.ai_family = addressfamily;
560 hint.ai_socktype = SOCK_STREAM;
561 hint.ai_protocol = IPPROTO_TCP;
562 hint.ai_flags = AI_PASSIVE;
564 err = getaddrinfo(address, myport, &hint, &ai);
567 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
574 for(aip = ai; aip; aip = aip->ai_next) {
575 listen_socket[listen_sockets].tcp =
576 setup_listen_socket((sockaddr_t *) aip->ai_addr);
578 if(listen_socket[listen_sockets].tcp < 0)
581 listen_socket[listen_sockets].udp =
582 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
584 if(listen_socket[listen_sockets].udp < 0)
587 ifdebug(CONNECTIONS) {
588 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
589 logger(LOG_NOTICE, "Listening on %s", hostname);
593 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
600 logger(LOG_NOTICE, "Ready");
602 logger(LOG_ERR, "Unable to create any listening socket!");
612 bool setup_network(void) {
622 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
623 if(pinginterval < 1) {
624 pinginterval = 86400;
629 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
631 if(pingtimeout < 1 || pingtimeout > pinginterval)
632 pingtimeout = pinginterval;
634 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
635 maxoutbufsize = 10 * MTU;
644 close all open network connections
646 void close_network_connections(void) {
647 avl_node_t *node, *next;
652 for(node = connection_tree->head; node; node = next) {
656 terminate_connection(c, false);
659 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
660 outgoing_t *outgoing = node->data;
663 event_del(outgoing->event);
666 list_delete_list(outgoing_list);
668 if(myself && myself->connection) {
669 subnet_update(myself, NULL, false);
670 terminate_connection(myself->connection, false);
671 free_connection(myself->connection);
674 for(i = 0; i < listen_sockets; i++) {
675 close(listen_socket[i].tcp);
676 close(listen_socket[i].udp);
679 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
680 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
681 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
682 xasprintf(&envp[3], "NAME=%s", myself->name);
692 execute_script("tinc-down", envp);
694 if(myport) free(myport);
696 for(i = 0; i < 4; i++)