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 logger(LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
122 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
129 /* Try again with PEM_read_RSA_PUBKEY. */
131 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
132 fp = fopen(fname, "r");
135 logger(LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
140 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
141 // RSA_blinding_on(c->rsa_key, NULL);
148 logger(LOG_ERR, "No public key for %s specified!", c->name);
153 static bool read_rsa_private_key(void) {
155 char *fname, *key, *pubkey;
158 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
159 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
160 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
163 myself->connection->rsa_key = RSA_new();
164 // RSA_blinding_on(myself->connection->rsa_key, NULL);
165 BN_hex2bn(&myself->connection->rsa_key->d, key);
166 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
167 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
173 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
174 xasprintf(&fname, "%s/rsa_key.priv", confbase);
176 fp = fopen(fname, "r");
179 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
180 fname, strerror(errno));
185 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
186 if(fstat(fileno(fp), &s)) {
187 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
188 fname, strerror(errno));
193 if(s.st_mode & ~0100700)
194 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
197 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
200 if(!myself->connection->rsa_key) {
201 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
202 fname, strerror(errno));
212 Read Subnets from all host config files
214 void load_all_subnets(void) {
219 avl_tree_t *config_tree;
225 xasprintf(&dname, "%s/hosts", confbase);
226 dir = opendir(dname);
228 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
233 while((ent = readdir(dir))) {
234 if(!check_id(ent->d_name))
237 n = lookup_node(ent->d_name);
238 #ifdef _DIRENT_HAVE_D_TYPE
239 //if(ent->d_type != DT_REG)
243 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
244 init_configuration(&config_tree);
245 result = read_config_file(config_tree, fname);
252 n->name = xstrdup(ent->d_name);
256 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
257 if(!get_config_subnet(cfg, &s))
260 if((s2 = lookup_subnet(n, s))) {
267 exit_configuration(&config_tree);
274 Configure node_t myself and set up the local sockets (listen only)
276 static bool setup_myself(void) {
279 char *name, *hostname, *mode, *afname, *cipher, *digest;
281 char *address = NULL;
283 struct addrinfo *ai, *aip, hint = {0};
289 myself->connection = new_connection();
291 myself->hostname = xstrdup("MYSELF");
292 myself->connection->hostname = xstrdup("MYSELF");
294 myself->connection->options = 0;
295 myself->connection->protocol_version = PROT_CURRENT;
297 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
298 logger(LOG_ERR, "Name for tinc daemon required!");
302 if(!check_id(name)) {
303 logger(LOG_ERR, "Invalid name for myself!");
309 myself->connection->name = xstrdup(name);
310 xasprintf(&fname, "%s/hosts/%s", confbase, name);
311 read_config_options(config_tree, name);
312 read_config_file(config_tree, fname);
315 if(!read_rsa_private_key())
318 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
319 myport = xstrdup("655");
322 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
324 if(!ai || !ai->ai_addr)
327 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
328 sockaddr2str(&sa, NULL, &myport);
331 /* Read in all the subnets specified in the host configuration file */
333 cfg = lookup_config(config_tree, "Subnet");
336 if(!get_config_subnet(cfg, &subnet))
339 subnet_add(myself, subnet);
341 cfg = lookup_config_next(config_tree, cfg);
344 /* Check some options */
346 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
347 myself->options |= OPTION_INDIRECT;
349 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
350 myself->options |= OPTION_TCPONLY;
352 if(myself->options & OPTION_TCPONLY)
353 myself->options |= OPTION_INDIRECT;
355 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
356 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
357 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
358 strictsubnets |= tunnelserver;
360 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
361 if(!strcasecmp(mode, "router"))
362 routing_mode = RMODE_ROUTER;
363 else if(!strcasecmp(mode, "switch"))
364 routing_mode = RMODE_SWITCH;
365 else if(!strcasecmp(mode, "hub"))
366 routing_mode = RMODE_HUB;
368 logger(LOG_ERR, "Invalid routing mode!");
374 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
375 if(!strcasecmp(mode, "off"))
376 forwarding_mode = FMODE_OFF;
377 else if(!strcasecmp(mode, "internal"))
378 forwarding_mode = FMODE_INTERNAL;
379 else if(!strcasecmp(mode, "kernel"))
380 forwarding_mode = FMODE_KERNEL;
382 logger(LOG_ERR, "Invalid forwarding mode!");
389 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
391 myself->options |= OPTION_PMTU_DISCOVERY;
394 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
396 myself->options |= OPTION_CLAMP_MSS;
398 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
400 #if !defined(SOL_IP) || !defined(IP_TOS)
401 if(priorityinheritance)
402 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
405 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
408 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
409 if(maxtimeout <= 0) {
410 logger(LOG_ERR, "Bogus maximum timeout!");
416 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
417 if(udp_rcvbuf <= 0) {
418 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
423 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
424 if(udp_sndbuf <= 0) {
425 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
430 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
431 if(replaywin_int < 0) {
432 logger(LOG_ERR, "ReplayWindow cannot be negative!");
435 replaywin = (unsigned)replaywin_int;
438 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
439 if(!strcasecmp(afname, "IPv4"))
440 addressfamily = AF_INET;
441 else if(!strcasecmp(afname, "IPv6"))
442 addressfamily = AF_INET6;
443 else if(!strcasecmp(afname, "any"))
444 addressfamily = AF_UNSPEC;
446 logger(LOG_ERR, "Invalid address family!");
452 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
454 /* Generate packet encryption key */
457 (lookup_config(config_tree, "Cipher"), &cipher)) {
458 if(!strcasecmp(cipher, "none")) {
459 myself->incipher = NULL;
461 myself->incipher = EVP_get_cipherbyname(cipher);
463 if(!myself->incipher) {
464 logger(LOG_ERR, "Unrecognized cipher type!");
469 myself->incipher = EVP_bf_cbc();
472 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
474 myself->inkeylength = 1;
476 myself->connection->outcipher = EVP_bf_ofb();
478 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
481 keyexpires = now + keylifetime;
483 /* Check if we want to use message authentication codes... */
485 if(get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
486 if(!strcasecmp(digest, "none")) {
487 myself->indigest = NULL;
489 myself->indigest = EVP_get_digestbyname(digest);
491 if(!myself->indigest) {
492 logger(LOG_ERR, "Unrecognized digest type!");
497 myself->indigest = EVP_sha1();
499 myself->connection->outdigest = EVP_sha1();
501 if(get_config_int(lookup_config(config_tree, "MACLength"), &myself->inmaclength)) {
502 if(myself->indigest) {
503 if(myself->inmaclength > myself->indigest->md_size) {
504 logger(LOG_ERR, "MAC length exceeds size of digest!");
506 } else if(myself->inmaclength < 0) {
507 logger(LOG_ERR, "Bogus MAC length!");
512 myself->inmaclength = 4;
514 myself->connection->outmaclength = 0;
518 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
519 if(myself->incompression < 0 || myself->incompression > 11) {
520 logger(LOG_ERR, "Bogus compression level!");
524 myself->incompression = 0;
526 myself->connection->outcompression = 0;
530 myself->nexthop = myself;
531 myself->via = myself;
532 myself->status.reachable = true;
545 /* Run tinc-up script to further initialize the tap interface */
546 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
547 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
548 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
549 xasprintf(&envp[3], "NAME=%s", myself->name);
552 execute_script("tinc-up", envp);
554 for(i = 0; i < 5; i++)
557 /* Run subnet-up scripts for our own subnets */
559 subnet_update(myself, NULL, true);
563 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
565 hint.ai_family = addressfamily;
566 hint.ai_socktype = SOCK_STREAM;
567 hint.ai_protocol = IPPROTO_TCP;
568 hint.ai_flags = AI_PASSIVE;
570 err = getaddrinfo(address, myport, &hint, &ai);
573 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
580 for(aip = ai; aip; aip = aip->ai_next) {
581 listen_socket[listen_sockets].tcp =
582 setup_listen_socket((sockaddr_t *) aip->ai_addr);
584 if(listen_socket[listen_sockets].tcp < 0)
587 listen_socket[listen_sockets].udp =
588 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
590 if(listen_socket[listen_sockets].udp < 0)
593 ifdebug(CONNECTIONS) {
594 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
595 logger(LOG_NOTICE, "Listening on %s", hostname);
599 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
606 logger(LOG_NOTICE, "Ready");
608 logger(LOG_ERR, "Unable to create any listening socket!");
618 bool setup_network(void) {
628 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
629 if(pinginterval < 1) {
630 pinginterval = 86400;
635 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
637 if(pingtimeout < 1 || pingtimeout > pinginterval)
638 pingtimeout = pinginterval;
640 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
641 maxoutbufsize = 10 * MTU;
650 close all open network connections
652 void close_network_connections(void) {
653 avl_node_t *node, *next;
658 for(node = connection_tree->head; node; node = next) {
662 terminate_connection(c, false);
665 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
666 outgoing_t *outgoing = node->data;
669 event_del(outgoing->event);
672 list_delete_list(outgoing_list);
674 if(myself && myself->connection) {
675 subnet_update(myself, NULL, false);
676 terminate_connection(myself->connection, false);
677 free_connection(myself->connection);
680 for(i = 0; i < listen_sockets; i++) {
681 close(listen_socket[i].tcp);
682 close(listen_socket[i].udp);
685 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
686 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
687 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
688 xasprintf(&envp[3], "NAME=%s", myself->name);
698 execute_script("tinc-down", envp);
700 if(myport) free(myport);
702 for(i = 0; i < 4; i++)