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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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)
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 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
127 /* Try again with PEM_read_RSA_PUBKEY. */
129 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
134 // RSA_blinding_on(c->rsa_key, NULL);
143 logger(LOG_ERR, _("No public key for %s specified!"), c->name);
148 bool read_rsa_private_key(void)
151 char *fname, *key, *pubkey;
156 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
157 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &pubkey)) {
158 logger(LOG_ERR, _("PrivateKey used but no PublicKey found!"));
161 myself->connection->rsa_key = RSA_new();
162 // RSA_blinding_on(myself->connection->rsa_key, NULL);
163 BN_hex2bn(&myself->connection->rsa_key->d, key);
164 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
165 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
171 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
172 xasprintf(&fname, "%s/rsa_key.priv", confbase);
174 fp = fopen(fname, "r");
177 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
178 fname, strerror(errno));
183 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
184 if(fstat(fileno(fp), &s)) {
185 logger(LOG_ERR, _("Could not stat RSA private key file `%s': %s'"),
186 fname, strerror(errno));
191 if(s.st_mode & ~0100700)
192 logger(LOG_WARNING, _("Warning: insecure file permissions for RSA private key file `%s'!"), fname);
195 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
198 if(!myself->connection->rsa_key) {
199 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
200 fname, strerror(errno));
210 Configure node_t myself and set up the local sockets (listen only)
212 bool setup_myself(void)
216 char *name, *hostname, *mode, *afname, *cipher, *digest;
217 char *address = NULL;
219 struct addrinfo *ai, *aip, hint = {0};
226 myself->connection = new_connection();
227 init_configuration(&myself->connection->config_tree);
229 xasprintf(&myself->hostname, _("MYSELF"));
230 xasprintf(&myself->connection->hostname, _("MYSELF"));
232 myself->connection->options = 0;
233 myself->connection->protocol_version = PROT_CURRENT;
235 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
236 logger(LOG_ERR, _("Name for tinc daemon required!"));
240 if(!check_id(name)) {
241 logger(LOG_ERR, _("Invalid name for myself!"));
247 myself->connection->name = xstrdup(name);
249 if(!read_connection_config(myself->connection)) {
250 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
254 if(!read_rsa_private_key())
257 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
258 xasprintf(&myport, "655");
260 /* Read in all the subnets specified in the host configuration file */
262 cfg = lookup_config(myself->connection->config_tree, "Subnet");
265 if(!get_config_subnet(cfg, &subnet))
268 subnet_add(myself, subnet);
270 cfg = lookup_config_next(myself->connection->config_tree, cfg);
273 /* Check some options */
275 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
276 myself->options |= OPTION_INDIRECT;
278 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
279 myself->options |= OPTION_TCPONLY;
281 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
282 myself->options |= OPTION_INDIRECT;
284 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
285 myself->options |= OPTION_TCPONLY;
287 if(myself->options & OPTION_TCPONLY)
288 myself->options |= OPTION_INDIRECT;
290 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
292 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
293 if(!strcasecmp(mode, "router"))
294 routing_mode = RMODE_ROUTER;
295 else if(!strcasecmp(mode, "switch"))
296 routing_mode = RMODE_SWITCH;
297 else if(!strcasecmp(mode, "hub"))
298 routing_mode = RMODE_HUB;
300 logger(LOG_ERR, _("Invalid routing mode!"));
305 routing_mode = RMODE_ROUTER;
307 // Enable PMTUDiscovery by default if we are in router mode.
309 choice = routing_mode == RMODE_ROUTER;
310 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
312 myself->options |= OPTION_PMTU_DISCOVERY;
314 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
316 #if !defined(SOL_IP) || !defined(IP_TOS)
317 if(priorityinheritance)
318 logger(LOG_WARNING, _("%s not supported on this platform"), "PriorityInheritance");
321 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
324 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
325 if(maxtimeout <= 0) {
326 logger(LOG_ERR, _("Bogus maximum timeout!"));
332 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
333 if(!strcasecmp(afname, "IPv4"))
334 addressfamily = AF_INET;
335 else if(!strcasecmp(afname, "IPv6"))
336 addressfamily = AF_INET6;
337 else if(!strcasecmp(afname, "any"))
338 addressfamily = AF_UNSPEC;
340 logger(LOG_ERR, _("Invalid address family!"));
346 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
348 /* Generate packet encryption key */
351 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
352 if(!strcasecmp(cipher, "none")) {
353 myself->incipher = NULL;
355 myself->incipher = EVP_get_cipherbyname(cipher);
357 if(!myself->incipher) {
358 logger(LOG_ERR, _("Unrecognized cipher type!"));
363 myself->incipher = EVP_aes_256_cbc();
366 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
368 myself->inkeylength = 1;
370 myself->connection->outcipher = EVP_aes_256_ofb();
372 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
375 keyexpires = now + keylifetime;
377 /* Check if we want to use message authentication codes... */
379 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
380 if(!strcasecmp(digest, "none")) {
381 myself->indigest = NULL;
383 myself->indigest = EVP_get_digestbyname(digest);
385 if(!myself->indigest) {
386 logger(LOG_ERR, _("Unrecognized digest type!"));
391 myself->indigest = EVP_sha256();
393 myself->connection->outdigest = EVP_sha256();
395 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength)) {
396 if(myself->indigest) {
397 if(myself->inmaclength > myself->indigest->md_size) {
398 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
400 } else if(myself->inmaclength < 0) {
401 logger(LOG_ERR, _("Bogus MAC length!"));
406 myself->inmaclength = 4;
408 myself->connection->outmaclength = 0;
412 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
413 if(myself->incompression < 0 || myself->incompression > 11) {
414 logger(LOG_ERR, _("Bogus compression level!"));
418 myself->incompression = 0;
420 myself->connection->outcompression = 0;
424 myself->nexthop = myself;
425 myself->via = myself;
426 myself->status.reachable = true;
436 /* Run tinc-up script to further initialize the tap interface */
437 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
438 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
439 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
440 xasprintf(&envp[3], "NAME=%s", myself->name);
443 execute_script("tinc-up", envp);
445 for(i = 0; i < 5; i++)
448 /* Run subnet-up scripts for our own subnets */
450 subnet_update(myself, NULL, true);
454 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
456 hint.ai_family = addressfamily;
457 hint.ai_socktype = SOCK_STREAM;
458 hint.ai_protocol = IPPROTO_TCP;
459 hint.ai_flags = AI_PASSIVE;
461 err = getaddrinfo(address, myport, &hint, &ai);
464 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
471 for(aip = ai; aip; aip = aip->ai_next) {
472 listen_socket[listen_sockets].tcp =
473 setup_listen_socket((sockaddr_t *) aip->ai_addr);
475 if(listen_socket[listen_sockets].tcp < 0)
478 listen_socket[listen_sockets].udp =
479 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
481 if(listen_socket[listen_sockets].udp < 0)
484 ifdebug(CONNECTIONS) {
485 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
486 logger(LOG_NOTICE, _("Listening on %s"), hostname);
490 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
497 logger(LOG_NOTICE, _("Ready"));
499 logger(LOG_ERR, _("Unable to create any listening socket!"));
509 bool setup_network(void)
522 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
523 if(pinginterval < 1) {
524 pinginterval = 86400;
529 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
531 if(pingtimeout < 1 || pingtimeout > pinginterval)
532 pingtimeout = pinginterval;
534 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
535 maxoutbufsize = 10 * MTU;
544 close all open network connections
546 void close_network_connections(void)
548 avl_node_t *node, *next;
555 for(node = connection_tree->head; node; node = next) {
559 terminate_connection(c, false);
562 list_delete_list(outgoing_list);
564 if(myself && myself->connection) {
565 subnet_update(myself, NULL, false);
566 terminate_connection(myself->connection, false);
567 free_connection(myself->connection);
570 for(i = 0; i < listen_sockets; i++) {
571 close(listen_socket[i].tcp);
572 close(listen_socket[i].udp);
575 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
576 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
577 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
578 xasprintf(&envp[3], "NAME=%s", myself->name);
588 execute_script("tinc-down", envp);
590 if(myport) free(myport);
592 for(i = 0; i < 4; i++)