3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <openssl/pem.h>
25 #include <openssl/rsa.h>
26 #include <openssl/rand.h>
27 #include <openssl/err.h>
28 #include <openssl/evp.h>
32 #include "connection.h"
48 bool read_rsa_public_key(connection_t *c) {
54 c->rsa_key = RSA_new();
55 // RSA_blinding_on(c->rsa_key, NULL);
58 /* First, check for simple PublicKey statement */
60 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
61 BN_hex2bn(&c->rsa_key->n, key);
62 BN_hex2bn(&c->rsa_key->e, "FFFF");
67 /* Else, check for PublicKeyFile statement and read it */
69 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
70 fp = fopen(fname, "r");
73 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
74 fname, strerror(errno));
80 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
84 return true; /* Woohoo. */
86 /* If it fails, try PEM_read_RSA_PUBKEY. */
87 fp = fopen(fname, "r");
90 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
91 fname, strerror(errno));
97 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
101 // RSA_blinding_on(c->rsa_key, NULL);
105 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s",
106 fname, strerror(errno));
110 /* Else, check if a harnessed public key is in the config file */
112 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
113 fp = fopen(fname, "r");
116 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
125 /* Try again with PEM_read_RSA_PUBKEY. */
127 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
128 fp = fopen(fname, "r");
131 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
132 // RSA_blinding_on(c->rsa_key, NULL);
141 logger(LOG_ERR, "No public key for %s specified!", c->name);
146 bool read_rsa_private_key(void) {
148 char *fname, *key, *pubkey;
151 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
152 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &pubkey)) {
153 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
156 myself->connection->rsa_key = RSA_new();
157 // RSA_blinding_on(myself->connection->rsa_key, NULL);
158 BN_hex2bn(&myself->connection->rsa_key->d, key);
159 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
160 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
166 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
167 xasprintf(&fname, "%s/rsa_key.priv", confbase);
169 fp = fopen(fname, "r");
172 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
173 fname, strerror(errno));
178 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
179 if(fstat(fileno(fp), &s)) {
180 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
181 fname, strerror(errno));
186 if(s.st_mode & ~0100700)
187 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
190 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
193 if(!myself->connection->rsa_key) {
194 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
195 fname, strerror(errno));
205 Read Subnets from all host config files
207 void load_all_subnets(void) {
212 avl_tree_t *config_tree;
218 xasprintf(&dname, "%s/hosts", confbase);
219 dir = opendir(dname);
221 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
226 while((ent = readdir(dir))) {
227 if(!check_id(ent->d_name))
230 n = lookup_node(ent->d_name);
234 #ifdef _DIRENT_HAVE_D_TYPE
235 //if(ent->d_type != DT_REG)
239 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
240 init_configuration(&config_tree);
241 result = read_config_file(config_tree, fname);
247 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;
274 char *address = NULL;
276 struct addrinfo *ai, *aip, hint = {0};
281 myself->connection = new_connection();
282 init_configuration(&myself->connection->config_tree);
284 myself->hostname = xstrdup("MYSELF");
285 myself->connection->hostname = xstrdup("MYSELF");
287 myself->connection->options = 0;
288 myself->connection->protocol_version = PROT_CURRENT;
290 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
291 logger(LOG_ERR, "Name for tinc daemon required!");
295 if(!check_id(name)) {
296 logger(LOG_ERR, "Invalid name for myself!");
302 myself->connection->name = xstrdup(name);
304 if(!read_connection_config(myself->connection)) {
305 logger(LOG_ERR, "Cannot open host configuration file for myself!");
309 if(!read_rsa_private_key())
312 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)
313 && !get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
314 myport = xstrdup("655");
317 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
319 if(!ai || !ai->ai_addr)
322 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
323 sockaddr2str(&sa, NULL, &myport);
326 /* Read in all the subnets specified in the host configuration file */
328 cfg = lookup_config(myself->connection->config_tree, "Subnet");
331 if(!get_config_subnet(cfg, &subnet))
334 subnet_add(myself, subnet);
336 cfg = lookup_config_next(myself->connection->config_tree, cfg);
339 /* Check some options */
341 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
342 myself->options |= OPTION_INDIRECT;
344 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
345 myself->options |= OPTION_TCPONLY;
347 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
348 myself->options |= OPTION_INDIRECT;
350 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
351 myself->options |= OPTION_TCPONLY;
353 if(myself->options & OPTION_TCPONLY)
354 myself->options |= OPTION_INDIRECT;
356 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
357 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
358 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
359 strictsubnets |= tunnelserver;
361 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
362 if(!strcasecmp(mode, "router"))
363 routing_mode = RMODE_ROUTER;
364 else if(!strcasecmp(mode, "switch"))
365 routing_mode = RMODE_SWITCH;
366 else if(!strcasecmp(mode, "hub"))
367 routing_mode = RMODE_HUB;
369 logger(LOG_ERR, "Invalid routing mode!");
375 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
376 if(!strcasecmp(mode, "off"))
377 forwarding_mode = FMODE_OFF;
378 else if(!strcasecmp(mode, "internal"))
379 forwarding_mode = FMODE_INTERNAL;
380 else if(!strcasecmp(mode, "kernel"))
381 forwarding_mode = FMODE_KERNEL;
383 logger(LOG_ERR, "Invalid forwarding mode!");
390 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
391 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
393 myself->options |= OPTION_PMTU_DISCOVERY;
396 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
397 get_config_bool(lookup_config(myself->connection->config_tree, "ClampMSS"), &choice);
399 myself->options |= OPTION_CLAMP_MSS;
401 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
403 #if !defined(SOL_IP) || !defined(IP_TOS)
404 if(priorityinheritance)
405 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
408 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
411 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
412 if(maxtimeout <= 0) {
413 logger(LOG_ERR, "Bogus maximum timeout!");
419 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
420 if(!strcasecmp(afname, "IPv4"))
421 addressfamily = AF_INET;
422 else if(!strcasecmp(afname, "IPv6"))
423 addressfamily = AF_INET6;
424 else if(!strcasecmp(afname, "any"))
425 addressfamily = AF_UNSPEC;
427 logger(LOG_ERR, "Invalid address family!");
433 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
435 /* Generate packet encryption key */
438 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
439 if(!strcasecmp(cipher, "none")) {
440 myself->incipher = NULL;
442 myself->incipher = EVP_get_cipherbyname(cipher);
444 if(!myself->incipher) {
445 logger(LOG_ERR, "Unrecognized cipher type!");
450 myself->incipher = EVP_bf_cbc();
453 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
455 myself->inkeylength = 1;
457 myself->connection->outcipher = EVP_bf_ofb();
459 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
462 keyexpires = now + keylifetime;
464 /* Check if we want to use message authentication codes... */
466 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
467 if(!strcasecmp(digest, "none")) {
468 myself->indigest = NULL;
470 myself->indigest = EVP_get_digestbyname(digest);
472 if(!myself->indigest) {
473 logger(LOG_ERR, "Unrecognized digest type!");
478 myself->indigest = EVP_sha1();
480 myself->connection->outdigest = EVP_sha1();
482 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength)) {
483 if(myself->indigest) {
484 if(myself->inmaclength > myself->indigest->md_size) {
485 logger(LOG_ERR, "MAC length exceeds size of digest!");
487 } else if(myself->inmaclength < 0) {
488 logger(LOG_ERR, "Bogus MAC length!");
493 myself->inmaclength = 4;
495 myself->connection->outmaclength = 0;
499 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
500 if(myself->incompression < 0 || myself->incompression > 11) {
501 logger(LOG_ERR, "Bogus compression level!");
505 myself->incompression = 0;
507 myself->connection->outcompression = 0;
511 myself->nexthop = myself;
512 myself->via = myself;
513 myself->status.reachable = true;
526 /* Run tinc-up script to further initialize the tap interface */
527 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
528 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
529 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
530 xasprintf(&envp[3], "NAME=%s", myself->name);
533 execute_script("tinc-up", envp);
535 for(i = 0; i < 5; i++)
538 /* Run subnet-up scripts for our own subnets */
540 subnet_update(myself, NULL, true);
544 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
546 hint.ai_family = addressfamily;
547 hint.ai_socktype = SOCK_STREAM;
548 hint.ai_protocol = IPPROTO_TCP;
549 hint.ai_flags = AI_PASSIVE;
551 err = getaddrinfo(address, myport, &hint, &ai);
554 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
561 for(aip = ai; aip; aip = aip->ai_next) {
562 listen_socket[listen_sockets].tcp =
563 setup_listen_socket((sockaddr_t *) aip->ai_addr);
565 if(listen_socket[listen_sockets].tcp < 0)
568 listen_socket[listen_sockets].udp =
569 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
571 if(listen_socket[listen_sockets].udp < 0)
574 ifdebug(CONNECTIONS) {
575 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
576 logger(LOG_NOTICE, "Listening on %s", hostname);
580 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
587 logger(LOG_NOTICE, "Ready");
589 logger(LOG_ERR, "Unable to create any listening socket!");
599 bool setup_network(void) {
609 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
610 if(pinginterval < 1) {
611 pinginterval = 86400;
616 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
618 if(pingtimeout < 1 || pingtimeout > pinginterval)
619 pingtimeout = pinginterval;
621 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
622 maxoutbufsize = 10 * MTU;
631 close all open network connections
633 void close_network_connections(void) {
634 avl_node_t *node, *next;
639 for(node = connection_tree->head; node; node = next) {
643 terminate_connection(c, false);
646 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
647 outgoing_t *outgoing = node->data;
650 event_del(outgoing->event);
653 list_delete_list(outgoing_list);
655 if(myself && myself->connection) {
656 subnet_update(myself, NULL, false);
657 terminate_connection(myself->connection, false);
658 free_connection(myself->connection);
661 for(i = 0; i < listen_sockets; i++) {
662 close(listen_socket[i].tcp);
663 close(listen_socket[i].udp);
666 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
667 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
668 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
669 xasprintf(&envp[3], "NAME=%s", myself->name);
679 execute_script("tinc-down", envp);
681 if(myport) free(myport);
683 for(i = 0; i < 4; i++)