3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2011 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"
50 bool read_rsa_public_key(connection_t *c) {
56 c->rsa_key = RSA_new();
57 // RSA_blinding_on(c->rsa_key, NULL);
60 /* First, check for simple PublicKey statement */
62 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
63 BN_hex2bn(&c->rsa_key->n, key);
64 BN_hex2bn(&c->rsa_key->e, "FFFF");
69 /* Else, check for PublicKeyFile statement and read it */
71 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
72 fp = fopen(fname, "r");
75 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
76 fname, strerror(errno));
82 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
86 return true; /* Woohoo. */
88 /* If it fails, try PEM_read_RSA_PUBKEY. */
89 fp = fopen(fname, "r");
92 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
93 fname, strerror(errno));
99 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
103 // RSA_blinding_on(c->rsa_key, NULL);
107 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s",
108 fname, strerror(errno));
112 /* Else, check if a harnessed public key is in the config file */
114 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
115 fp = fopen(fname, "r");
118 logger(LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
123 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
130 /* Try again with PEM_read_RSA_PUBKEY. */
132 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
133 fp = fopen(fname, "r");
136 logger(LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
141 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
142 // RSA_blinding_on(c->rsa_key, NULL);
149 logger(LOG_ERR, "No public key for %s specified!", c->name);
154 static bool read_rsa_private_key(void) {
156 char *fname, *key, *pubkey;
159 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
160 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
161 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
164 myself->connection->rsa_key = RSA_new();
165 // RSA_blinding_on(myself->connection->rsa_key, NULL);
166 BN_hex2bn(&myself->connection->rsa_key->d, key);
167 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
168 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
174 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
175 xasprintf(&fname, "%s/rsa_key.priv", confbase);
177 fp = fopen(fname, "r");
180 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
181 fname, strerror(errno));
186 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
187 if(fstat(fileno(fp), &s)) {
188 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
189 fname, strerror(errno));
194 if(s.st_mode & ~0100700)
195 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
198 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
201 if(!myself->connection->rsa_key) {
202 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
203 fname, strerror(errno));
213 Read Subnets from all host config files
215 void load_all_subnets(void) {
220 avl_tree_t *config_tree;
226 xasprintf(&dname, "%s/hosts", confbase);
227 dir = opendir(dname);
229 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
234 while((ent = readdir(dir))) {
235 if(!check_id(ent->d_name))
238 n = lookup_node(ent->d_name);
239 #ifdef _DIRENT_HAVE_D_TYPE
240 //if(ent->d_type != DT_REG)
244 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
245 init_configuration(&config_tree);
246 result = read_config_file(config_tree, fname);
253 n->name = xstrdup(ent->d_name);
257 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
258 if(!get_config_subnet(cfg, &s))
261 if((s2 = lookup_subnet(n, s))) {
268 exit_configuration(&config_tree);
275 Configure node_t myself and set up the local sockets (listen only)
277 static bool setup_myself(void) {
280 char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
282 char *address = NULL;
284 struct addrinfo *ai, *aip, hint = {0};
290 myself->connection = new_connection();
292 myself->hostname = xstrdup("MYSELF");
293 myself->connection->hostname = xstrdup("MYSELF");
295 myself->connection->options = 0;
296 myself->connection->protocol_version = PROT_CURRENT;
298 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
299 logger(LOG_ERR, "Name for tinc daemon required!");
303 if(!check_id(name)) {
304 logger(LOG_ERR, "Invalid name for myself!");
310 myself->connection->name = xstrdup(name);
311 xasprintf(&fname, "%s/hosts/%s", confbase, name);
312 read_config_options(config_tree, name);
313 read_config_file(config_tree, fname);
316 if(!read_rsa_private_key())
319 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
320 myport = xstrdup("655");
323 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
325 if(!ai || !ai->ai_addr)
328 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
329 sockaddr2str(&sa, NULL, &myport);
332 /* Read in all the subnets specified in the host configuration file */
334 cfg = lookup_config(config_tree, "Subnet");
337 if(!get_config_subnet(cfg, &subnet))
340 subnet_add(myself, subnet);
342 cfg = lookup_config_next(config_tree, cfg);
345 /* Check some options */
347 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
348 myself->options |= OPTION_INDIRECT;
350 if(get_config_bool(lookup_config(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(config_tree, "PMTUDiscovery"), &choice);
392 myself->options |= OPTION_PMTU_DISCOVERY;
395 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
397 myself->options |= OPTION_CLAMP_MSS;
399 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
401 #if !defined(SOL_IP) || !defined(IP_TOS)
402 if(priorityinheritance)
403 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
406 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
409 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
410 if(maxtimeout <= 0) {
411 logger(LOG_ERR, "Bogus maximum timeout!");
417 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
418 if(udp_rcvbuf <= 0) {
419 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
424 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
425 if(udp_sndbuf <= 0) {
426 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
431 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
432 if(replaywin_int < 0) {
433 logger(LOG_ERR, "ReplayWindow cannot be negative!");
436 replaywin = (unsigned)replaywin_int;
439 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
440 if(!strcasecmp(afname, "IPv4"))
441 addressfamily = AF_INET;
442 else if(!strcasecmp(afname, "IPv6"))
443 addressfamily = AF_INET6;
444 else if(!strcasecmp(afname, "any"))
445 addressfamily = AF_UNSPEC;
447 logger(LOG_ERR, "Invalid address family!");
453 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
455 /* Generate packet encryption key */
458 (lookup_config(config_tree, "Cipher"), &cipher)) {
459 if(!strcasecmp(cipher, "none")) {
460 myself->incipher = NULL;
462 myself->incipher = EVP_get_cipherbyname(cipher);
464 if(!myself->incipher) {
465 logger(LOG_ERR, "Unrecognized cipher type!");
470 myself->incipher = EVP_bf_cbc();
473 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
475 myself->inkeylength = 1;
477 myself->connection->outcipher = EVP_bf_ofb();
479 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
482 keyexpires = now + keylifetime;
484 /* Check if we want to use message authentication codes... */
486 if(get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
487 if(!strcasecmp(digest, "none")) {
488 myself->indigest = NULL;
490 myself->indigest = EVP_get_digestbyname(digest);
492 if(!myself->indigest) {
493 logger(LOG_ERR, "Unrecognized digest type!");
498 myself->indigest = EVP_sha1();
500 myself->connection->outdigest = EVP_sha1();
502 if(get_config_int(lookup_config(config_tree, "MACLength"), &myself->inmaclength)) {
503 if(myself->indigest) {
504 if(myself->inmaclength > myself->indigest->md_size) {
505 logger(LOG_ERR, "MAC length exceeds size of digest!");
507 } else if(myself->inmaclength < 0) {
508 logger(LOG_ERR, "Bogus MAC length!");
513 myself->inmaclength = 4;
515 myself->connection->outmaclength = 0;
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;
543 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
544 if(!strcasecmp(type, "dummy"))
545 devops = dummy_devops;
546 else if(!strcasecmp(type, "raw_socket"))
547 devops = raw_socket_devops;
549 else if(!strcasecmp(type, "uml"))
553 else if(!strcasecmp(type, "vde"))
557 logger(LOG_ERR, "Unknown device type %s!", type);
567 /* Run tinc-up script to further initialize the tap interface */
568 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
569 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
570 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
571 xasprintf(&envp[3], "NAME=%s", myself->name);
574 execute_script("tinc-up", envp);
576 for(i = 0; i < 5; i++)
579 /* Run subnet-up scripts for our own subnets */
581 subnet_update(myself, NULL, true);
585 cfg = lookup_config(config_tree, "BindToAddress");
588 get_config_string(cfg, &address);
590 cfg = lookup_config_next(config_tree, cfg);
592 hint.ai_family = addressfamily;
593 hint.ai_socktype = SOCK_STREAM;
594 hint.ai_protocol = IPPROTO_TCP;
595 hint.ai_flags = AI_PASSIVE;
597 err = getaddrinfo(address, myport, &hint, &ai);
601 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
608 for(aip = ai; aip; aip = aip->ai_next) {
609 listen_socket[listen_sockets].tcp =
610 setup_listen_socket((sockaddr_t *) aip->ai_addr);
612 if(listen_socket[listen_sockets].tcp < 0)
615 listen_socket[listen_sockets].udp =
616 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
618 if(listen_socket[listen_sockets].udp < 0)
621 ifdebug(CONNECTIONS) {
622 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
623 logger(LOG_NOTICE, "Listening on %s", hostname);
627 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
635 logger(LOG_NOTICE, "Ready");
637 logger(LOG_ERR, "Unable to create any listening socket!");
647 bool setup_network(void) {
657 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
658 if(pinginterval < 1) {
659 pinginterval = 86400;
664 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
666 if(pingtimeout < 1 || pingtimeout > pinginterval)
667 pingtimeout = pinginterval;
669 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
670 maxoutbufsize = 10 * MTU;
679 close all open network connections
681 void close_network_connections(void) {
682 avl_node_t *node, *next;
687 for(node = connection_tree->head; node; node = next) {
691 terminate_connection(c, false);
694 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
695 outgoing_t *outgoing = node->data;
698 event_del(outgoing->event);
701 list_delete_list(outgoing_list);
703 if(myself && myself->connection) {
704 subnet_update(myself, NULL, false);
705 terminate_connection(myself->connection, false);
706 free_connection(myself->connection);
709 for(i = 0; i < listen_sockets; i++) {
710 close(listen_socket[i].tcp);
711 close(listen_socket[i].udp);
714 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
715 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
716 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
717 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++)