3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <openssl/pem.h>
24 #include <openssl/rsa.h>
25 #include <openssl/rand.h>
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
31 #include "connection.h"
47 bool read_rsa_public_key(connection_t *c) {
53 c->rsa_key = RSA_new();
54 // RSA_blinding_on(c->rsa_key, NULL);
57 /* First, check for simple PublicKey statement */
59 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
60 BN_hex2bn(&c->rsa_key->n, key);
61 BN_hex2bn(&c->rsa_key->e, "FFFF");
66 /* Else, check for PublicKeyFile statement and read it */
68 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
69 fp = fopen(fname, "r");
72 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
73 fname, strerror(errno));
79 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
83 return true; /* Woohoo. */
85 /* If it fails, try PEM_read_RSA_PUBKEY. */
86 fp = fopen(fname, "r");
89 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
90 fname, strerror(errno));
96 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
100 // RSA_blinding_on(c->rsa_key, NULL);
104 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s",
105 fname, strerror(errno));
109 /* Else, check if a harnessed public key is in the config file */
111 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
112 fp = fopen(fname, "r");
115 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
124 /* Try again with PEM_read_RSA_PUBKEY. */
126 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
127 fp = fopen(fname, "r");
130 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
131 // RSA_blinding_on(c->rsa_key, NULL);
140 logger(LOG_ERR, "No public key for %s specified!", c->name);
145 bool read_rsa_private_key(void) {
147 char *fname, *key, *pubkey;
150 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
151 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &pubkey)) {
152 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
155 myself->connection->rsa_key = RSA_new();
156 // RSA_blinding_on(myself->connection->rsa_key, NULL);
157 BN_hex2bn(&myself->connection->rsa_key->d, key);
158 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
159 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
165 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
166 xasprintf(&fname, "%s/rsa_key.priv", confbase);
168 fp = fopen(fname, "r");
171 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
172 fname, strerror(errno));
177 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
178 if(fstat(fileno(fp), &s)) {
179 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
180 fname, strerror(errno));
185 if(s.st_mode & ~0100700)
186 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
189 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
192 if(!myself->connection->rsa_key) {
193 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
194 fname, strerror(errno));
204 Configure node_t myself and set up the local sockets (listen only)
206 bool setup_myself(void) {
209 char *name, *hostname, *mode, *afname, *cipher, *digest;
210 char *address = NULL;
212 struct addrinfo *ai, *aip, hint = {0};
217 myself->connection = new_connection();
218 init_configuration(&myself->connection->config_tree);
220 xasprintf(&myself->hostname, "MYSELF");
221 xasprintf(&myself->connection->hostname, "MYSELF");
223 myself->connection->options = 0;
224 myself->connection->protocol_version = PROT_CURRENT;
226 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
227 logger(LOG_ERR, "Name for tinc daemon required!");
231 if(!check_id(name)) {
232 logger(LOG_ERR, "Invalid name for myself!");
238 myself->connection->name = xstrdup(name);
240 if(!read_connection_config(myself->connection)) {
241 logger(LOG_ERR, "Cannot open host configuration file for myself!");
245 if(!read_rsa_private_key())
248 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
249 xasprintf(&myport, "655");
251 /* Read in all the subnets specified in the host configuration file */
253 cfg = lookup_config(myself->connection->config_tree, "Subnet");
256 if(!get_config_subnet(cfg, &subnet))
259 subnet_add(myself, subnet);
261 cfg = lookup_config_next(myself->connection->config_tree, cfg);
264 /* Check some options */
266 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
267 myself->options |= OPTION_INDIRECT;
269 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
270 myself->options |= OPTION_TCPONLY;
272 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
273 myself->options |= OPTION_INDIRECT;
275 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
276 myself->options |= OPTION_TCPONLY;
278 if(myself->options & OPTION_TCPONLY)
279 myself->options |= OPTION_INDIRECT;
281 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
283 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
284 if(!strcasecmp(mode, "router"))
285 routing_mode = RMODE_ROUTER;
286 else if(!strcasecmp(mode, "switch"))
287 routing_mode = RMODE_SWITCH;
288 else if(!strcasecmp(mode, "hub"))
289 routing_mode = RMODE_HUB;
291 logger(LOG_ERR, "Invalid routing mode!");
296 routing_mode = RMODE_ROUTER;
298 // Enable PMTUDiscovery by default if we are in router mode.
300 choice = routing_mode == RMODE_ROUTER;
301 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
303 myself->options |= OPTION_PMTU_DISCOVERY;
305 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
307 #if !defined(SOL_IP) || !defined(IP_TOS)
308 if(priorityinheritance)
309 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
312 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
315 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
316 if(maxtimeout <= 0) {
317 logger(LOG_ERR, "Bogus maximum timeout!");
323 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
324 if(!strcasecmp(afname, "IPv4"))
325 addressfamily = AF_INET;
326 else if(!strcasecmp(afname, "IPv6"))
327 addressfamily = AF_INET6;
328 else if(!strcasecmp(afname, "any"))
329 addressfamily = AF_UNSPEC;
331 logger(LOG_ERR, "Invalid address family!");
337 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
339 /* Generate packet encryption key */
342 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
343 if(!strcasecmp(cipher, "none")) {
344 myself->incipher = NULL;
346 myself->incipher = EVP_get_cipherbyname(cipher);
348 if(!myself->incipher) {
349 logger(LOG_ERR, "Unrecognized cipher type!");
354 myself->incipher = EVP_aes_256_cbc();
357 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
359 myself->inkeylength = 1;
361 myself->connection->outcipher = EVP_aes_256_ofb();
363 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
366 keyexpires = now + keylifetime;
368 /* Check if we want to use message authentication codes... */
370 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
371 if(!strcasecmp(digest, "none")) {
372 myself->indigest = NULL;
374 myself->indigest = EVP_get_digestbyname(digest);
376 if(!myself->indigest) {
377 logger(LOG_ERR, "Unrecognized digest type!");
382 myself->indigest = EVP_sha256();
384 myself->connection->outdigest = EVP_sha256();
386 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength)) {
387 if(myself->indigest) {
388 if(myself->inmaclength > myself->indigest->md_size) {
389 logger(LOG_ERR, "MAC length exceeds size of digest!");
391 } else if(myself->inmaclength < 0) {
392 logger(LOG_ERR, "Bogus MAC length!");
397 myself->inmaclength = 4;
399 myself->connection->outmaclength = 0;
403 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
404 if(myself->incompression < 0 || myself->incompression > 11) {
405 logger(LOG_ERR, "Bogus compression level!");
409 myself->incompression = 0;
411 myself->connection->outcompression = 0;
415 myself->nexthop = myself;
416 myself->via = myself;
417 myself->status.reachable = true;
427 /* Run tinc-up script to further initialize the tap interface */
428 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
429 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
430 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
431 xasprintf(&envp[3], "NAME=%s", myself->name);
434 execute_script("tinc-up", envp);
436 for(i = 0; i < 5; i++)
439 /* Run subnet-up scripts for our own subnets */
441 subnet_update(myself, NULL, true);
445 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
447 hint.ai_family = addressfamily;
448 hint.ai_socktype = SOCK_STREAM;
449 hint.ai_protocol = IPPROTO_TCP;
450 hint.ai_flags = AI_PASSIVE;
452 err = getaddrinfo(address, myport, &hint, &ai);
455 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
462 for(aip = ai; aip; aip = aip->ai_next) {
463 listen_socket[listen_sockets].tcp =
464 setup_listen_socket((sockaddr_t *) aip->ai_addr);
466 if(listen_socket[listen_sockets].tcp < 0)
469 listen_socket[listen_sockets].udp =
470 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
472 if(listen_socket[listen_sockets].udp < 0)
475 ifdebug(CONNECTIONS) {
476 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
477 logger(LOG_NOTICE, "Listening on %s", hostname);
481 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
488 logger(LOG_NOTICE, "Ready");
490 logger(LOG_ERR, "Unable to create any listening socket!");
500 bool setup_network(void) {
510 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
511 if(pinginterval < 1) {
512 pinginterval = 86400;
517 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
519 if(pingtimeout < 1 || pingtimeout > pinginterval)
520 pingtimeout = pinginterval;
522 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
523 maxoutbufsize = 10 * MTU;
532 close all open network connections
534 void close_network_connections(void) {
535 avl_node_t *node, *next;
540 for(node = connection_tree->head; node; node = next) {
544 terminate_connection(c, false);
547 list_delete_list(outgoing_list);
549 if(myself && myself->connection) {
550 subnet_update(myself, NULL, false);
551 terminate_connection(myself->connection, false);
552 free_connection(myself->connection);
555 for(i = 0; i < listen_sockets; i++) {
556 close(listen_socket[i].tcp);
557 close(listen_socket[i].udp);
560 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
561 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
562 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
563 xasprintf(&envp[3], "NAME=%s", myself->name);
573 execute_script("tinc-down", envp);
575 if(myport) free(myport);
577 for(i = 0; i < 4; i++)