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) {
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;
154 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
155 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &pubkey)) {
156 logger(LOG_ERR, _("PrivateKey used but no PublicKey found!"));
159 myself->connection->rsa_key = RSA_new();
160 // RSA_blinding_on(myself->connection->rsa_key, NULL);
161 BN_hex2bn(&myself->connection->rsa_key->d, key);
162 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
163 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
169 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
170 xasprintf(&fname, "%s/rsa_key.priv", confbase);
172 fp = fopen(fname, "r");
175 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
176 fname, strerror(errno));
181 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
182 if(fstat(fileno(fp), &s)) {
183 logger(LOG_ERR, _("Could not stat RSA private key file `%s': %s'"),
184 fname, strerror(errno));
189 if(s.st_mode & ~0100700)
190 logger(LOG_WARNING, _("Warning: insecure file permissions for RSA private key file `%s'!"), fname);
193 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
196 if(!myself->connection->rsa_key) {
197 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
198 fname, strerror(errno));
208 Configure node_t myself and set up the local sockets (listen only)
210 bool setup_myself(void) {
213 char *name, *hostname, *mode, *afname, *cipher, *digest;
214 char *address = NULL;
216 struct addrinfo *ai, *aip, hint = {0};
223 myself->connection = new_connection();
224 init_configuration(&myself->connection->config_tree);
226 xasprintf(&myself->hostname, _("MYSELF"));
227 xasprintf(&myself->connection->hostname, _("MYSELF"));
229 myself->connection->options = 0;
230 myself->connection->protocol_version = PROT_CURRENT;
232 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
233 logger(LOG_ERR, _("Name for tinc daemon required!"));
237 if(!check_id(name)) {
238 logger(LOG_ERR, _("Invalid name for myself!"));
244 myself->connection->name = xstrdup(name);
246 if(!read_connection_config(myself->connection)) {
247 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
251 if(!read_rsa_private_key())
254 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
255 xasprintf(&myport, "655");
257 /* Read in all the subnets specified in the host configuration file */
259 cfg = lookup_config(myself->connection->config_tree, "Subnet");
262 if(!get_config_subnet(cfg, &subnet))
265 subnet_add(myself, subnet);
267 cfg = lookup_config_next(myself->connection->config_tree, cfg);
270 /* Check some options */
272 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
273 myself->options |= OPTION_INDIRECT;
275 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
276 myself->options |= OPTION_TCPONLY;
278 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
279 myself->options |= OPTION_INDIRECT;
281 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
282 myself->options |= OPTION_TCPONLY;
284 if(myself->options & OPTION_TCPONLY)
285 myself->options |= OPTION_INDIRECT;
287 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
289 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
290 if(!strcasecmp(mode, "router"))
291 routing_mode = RMODE_ROUTER;
292 else if(!strcasecmp(mode, "switch"))
293 routing_mode = RMODE_SWITCH;
294 else if(!strcasecmp(mode, "hub"))
295 routing_mode = RMODE_HUB;
297 logger(LOG_ERR, _("Invalid routing mode!"));
302 routing_mode = RMODE_ROUTER;
304 // Enable PMTUDiscovery by default if we are in router mode.
306 choice = routing_mode == RMODE_ROUTER;
307 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
309 myself->options |= OPTION_PMTU_DISCOVERY;
311 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
313 #if !defined(SOL_IP) || !defined(IP_TOS)
314 if(priorityinheritance)
315 logger(LOG_WARNING, _("%s not supported on this platform"), "PriorityInheritance");
318 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
321 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
322 if(maxtimeout <= 0) {
323 logger(LOG_ERR, _("Bogus maximum timeout!"));
329 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
330 if(!strcasecmp(afname, "IPv4"))
331 addressfamily = AF_INET;
332 else if(!strcasecmp(afname, "IPv6"))
333 addressfamily = AF_INET6;
334 else if(!strcasecmp(afname, "any"))
335 addressfamily = AF_UNSPEC;
337 logger(LOG_ERR, _("Invalid address family!"));
343 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
345 /* Generate packet encryption key */
348 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
349 if(!strcasecmp(cipher, "none")) {
350 myself->incipher = NULL;
352 myself->incipher = EVP_get_cipherbyname(cipher);
354 if(!myself->incipher) {
355 logger(LOG_ERR, _("Unrecognized cipher type!"));
360 myself->incipher = EVP_aes_256_cbc();
363 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
365 myself->inkeylength = 1;
367 myself->connection->outcipher = EVP_aes_256_ofb();
369 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
372 keyexpires = now + keylifetime;
374 /* Check if we want to use message authentication codes... */
376 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
377 if(!strcasecmp(digest, "none")) {
378 myself->indigest = NULL;
380 myself->indigest = EVP_get_digestbyname(digest);
382 if(!myself->indigest) {
383 logger(LOG_ERR, _("Unrecognized digest type!"));
388 myself->indigest = EVP_sha256();
390 myself->connection->outdigest = EVP_sha256();
392 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength)) {
393 if(myself->indigest) {
394 if(myself->inmaclength > myself->indigest->md_size) {
395 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
397 } else if(myself->inmaclength < 0) {
398 logger(LOG_ERR, _("Bogus MAC length!"));
403 myself->inmaclength = 4;
405 myself->connection->outmaclength = 0;
409 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
410 if(myself->incompression < 0 || myself->incompression > 11) {
411 logger(LOG_ERR, _("Bogus compression level!"));
415 myself->incompression = 0;
417 myself->connection->outcompression = 0;
421 myself->nexthop = myself;
422 myself->via = myself;
423 myself->status.reachable = true;
433 /* Run tinc-up script to further initialize the tap interface */
434 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
435 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
436 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
437 xasprintf(&envp[3], "NAME=%s", myself->name);
440 execute_script("tinc-up", envp);
442 for(i = 0; i < 5; i++)
445 /* Run subnet-up scripts for our own subnets */
447 subnet_update(myself, NULL, true);
451 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
453 hint.ai_family = addressfamily;
454 hint.ai_socktype = SOCK_STREAM;
455 hint.ai_protocol = IPPROTO_TCP;
456 hint.ai_flags = AI_PASSIVE;
458 err = getaddrinfo(address, myport, &hint, &ai);
461 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
468 for(aip = ai; aip; aip = aip->ai_next) {
469 listen_socket[listen_sockets].tcp =
470 setup_listen_socket((sockaddr_t *) aip->ai_addr);
472 if(listen_socket[listen_sockets].tcp < 0)
475 listen_socket[listen_sockets].udp =
476 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
478 if(listen_socket[listen_sockets].udp < 0)
481 ifdebug(CONNECTIONS) {
482 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
483 logger(LOG_NOTICE, _("Listening on %s"), hostname);
487 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
494 logger(LOG_NOTICE, _("Ready"));
496 logger(LOG_ERR, _("Unable to create any listening socket!"));
506 bool setup_network(void) {
518 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
519 if(pinginterval < 1) {
520 pinginterval = 86400;
525 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
527 if(pingtimeout < 1 || pingtimeout > pinginterval)
528 pingtimeout = pinginterval;
530 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
531 maxoutbufsize = 10 * MTU;
540 close all open network connections
542 void close_network_connections(void) {
543 avl_node_t *node, *next;
550 for(node = connection_tree->head; node; node = next) {
554 terminate_connection(c, false);
557 list_delete_list(outgoing_list);
559 if(myself && myself->connection) {
560 subnet_update(myself, NULL, false);
561 terminate_connection(myself->connection, false);
562 free_connection(myself->connection);
565 for(i = 0; i < listen_sockets; i++) {
566 close(listen_socket[i].tcp);
567 close(listen_socket[i].udp);
570 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
571 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
572 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
573 xasprintf(&envp[3], "NAME=%s", myself->name);
583 execute_script("tinc-down", envp);
585 if(myport) free(myport);
587 for(i = 0; i < 4; i++)