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.
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)
58 c->rsa_key = RSA_new();
59 // RSA_blinding_on(c->rsa_key, NULL);
62 /* First, check for simple PublicKey statement */
64 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
65 BN_hex2bn(&c->rsa_key->n, key);
66 BN_hex2bn(&c->rsa_key->e, "FFFF");
71 /* Else, check for PublicKeyFile statement and read it */
73 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
74 fp = fopen(fname, "r");
77 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
78 fname, strerror(errno));
84 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
88 return true; /* Woohoo. */
90 /* If it fails, try PEM_read_RSA_PUBKEY. */
91 fp = fopen(fname, "r");
94 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
95 fname, strerror(errno));
101 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
105 // RSA_blinding_on(c->rsa_key, NULL);
109 logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
110 fname, strerror(errno));
114 /* Else, check if a harnessed public key is in the config file */
116 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
117 fp = fopen(fname, "r");
120 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 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
136 // RSA_blinding_on(c->rsa_key, NULL);
145 logger(LOG_ERR, _("No public key for %s specified!"), c->name);
150 bool read_rsa_private_key(void)
153 char *fname, *key, *pubkey;
158 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
159 if(!get_config_string(lookup_config(myself->connection->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 Configure node_t myself and set up the local sockets (listen only)
214 bool setup_myself(void)
218 char *name, *hostname, *mode, *afname, *cipher, *digest;
219 char *address = NULL;
221 struct addrinfo *ai, *aip, hint = {0};
228 myself->connection = new_connection();
229 init_configuration(&myself->connection->config_tree);
231 xasprintf(&myself->hostname, _("MYSELF"));
232 xasprintf(&myself->connection->hostname, _("MYSELF"));
234 myself->connection->options = 0;
235 myself->connection->protocol_version = PROT_CURRENT;
237 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
238 logger(LOG_ERR, _("Name for tinc daemon required!"));
242 if(!check_id(name)) {
243 logger(LOG_ERR, _("Invalid name for myself!"));
249 myself->connection->name = xstrdup(name);
251 if(!read_connection_config(myself->connection)) {
252 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
256 if(!read_rsa_private_key())
259 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
260 xasprintf(&myport, "655");
262 /* Read in all the subnets specified in the host configuration file */
264 cfg = lookup_config(myself->connection->config_tree, "Subnet");
267 if(!get_config_subnet(cfg, &subnet))
270 subnet_add(myself, subnet);
272 cfg = lookup_config_next(myself->connection->config_tree, cfg);
275 /* Check some options */
277 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
278 myself->options |= OPTION_INDIRECT;
280 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
281 myself->options |= OPTION_TCPONLY;
283 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
284 myself->options |= OPTION_INDIRECT;
286 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
287 myself->options |= OPTION_TCPONLY;
289 if(myself->options & OPTION_TCPONLY)
290 myself->options |= OPTION_INDIRECT;
292 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
294 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
295 if(!strcasecmp(mode, "router"))
296 routing_mode = RMODE_ROUTER;
297 else if(!strcasecmp(mode, "switch"))
298 routing_mode = RMODE_SWITCH;
299 else if(!strcasecmp(mode, "hub"))
300 routing_mode = RMODE_HUB;
302 logger(LOG_ERR, _("Invalid routing mode!"));
307 routing_mode = RMODE_ROUTER;
309 // Enable PMTUDiscovery by default if we are in router mode.
311 choice = routing_mode == RMODE_ROUTER;
312 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
314 myself->options |= OPTION_PMTU_DISCOVERY;
316 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
318 #if !defined(SOL_IP) || !defined(IP_TOS)
319 if(priorityinheritance)
320 logger(LOG_WARNING, _("%s not supported on this platform"), "PriorityInheritance");
323 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
326 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
327 if(maxtimeout <= 0) {
328 logger(LOG_ERR, _("Bogus maximum timeout!"));
334 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
335 if(!strcasecmp(afname, "IPv4"))
336 addressfamily = AF_INET;
337 else if(!strcasecmp(afname, "IPv6"))
338 addressfamily = AF_INET6;
339 else if(!strcasecmp(afname, "any"))
340 addressfamily = AF_UNSPEC;
342 logger(LOG_ERR, _("Invalid address family!"));
348 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
350 /* Generate packet encryption key */
353 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
354 if(!strcasecmp(cipher, "none")) {
355 myself->incipher = NULL;
357 myself->incipher = EVP_get_cipherbyname(cipher);
359 if(!myself->incipher) {
360 logger(LOG_ERR, _("Unrecognized cipher type!"));
365 myself->incipher = EVP_aes_256_cbc();
368 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
370 myself->inkeylength = 1;
372 myself->connection->outcipher = EVP_aes_256_ofb();
374 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
377 keyexpires = now + keylifetime;
379 /* Check if we want to use message authentication codes... */
381 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
382 if(!strcasecmp(digest, "none")) {
383 myself->indigest = NULL;
385 myself->indigest = EVP_get_digestbyname(digest);
387 if(!myself->indigest) {
388 logger(LOG_ERR, _("Unrecognized digest type!"));
393 myself->indigest = EVP_sha256();
395 myself->connection->outdigest = EVP_sha256();
397 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength)) {
398 if(myself->indigest) {
399 if(myself->inmaclength > myself->indigest->md_size) {
400 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
402 } else if(myself->inmaclength < 0) {
403 logger(LOG_ERR, _("Bogus MAC length!"));
408 myself->inmaclength = 4;
410 myself->connection->outmaclength = 0;
414 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
415 if(myself->incompression < 0 || myself->incompression > 11) {
416 logger(LOG_ERR, _("Bogus compression level!"));
420 myself->incompression = 0;
422 myself->connection->outcompression = 0;
426 myself->nexthop = myself;
427 myself->via = myself;
428 myself->status.reachable = true;
438 /* Run tinc-up script to further initialize the tap interface */
439 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
440 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
441 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
442 xasprintf(&envp[3], "NAME=%s", myself->name);
445 execute_script("tinc-up", envp);
447 for(i = 0; i < 5; i++)
450 /* Run subnet-up scripts for our own subnets */
452 subnet_update(myself, NULL, true);
456 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
458 hint.ai_family = addressfamily;
459 hint.ai_socktype = SOCK_STREAM;
460 hint.ai_protocol = IPPROTO_TCP;
461 hint.ai_flags = AI_PASSIVE;
463 err = getaddrinfo(address, myport, &hint, &ai);
466 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
473 for(aip = ai; aip; aip = aip->ai_next) {
474 listen_socket[listen_sockets].tcp =
475 setup_listen_socket((sockaddr_t *) aip->ai_addr);
477 if(listen_socket[listen_sockets].tcp < 0)
480 listen_socket[listen_sockets].udp =
481 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
483 if(listen_socket[listen_sockets].udp < 0)
486 ifdebug(CONNECTIONS) {
487 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
488 logger(LOG_NOTICE, _("Listening on %s"), hostname);
492 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
499 logger(LOG_NOTICE, _("Ready"));
501 logger(LOG_ERR, _("Unable to create any listening socket!"));
511 bool setup_network(void)
524 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
525 if(pinginterval < 1) {
526 pinginterval = 86400;
531 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
533 if(pingtimeout < 1 || pingtimeout > pinginterval)
534 pingtimeout = pinginterval;
536 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
537 maxoutbufsize = 10 * MTU;
546 close all open network connections
548 void close_network_connections(void)
550 avl_node_t *node, *next;
557 for(node = connection_tree->head; node; node = next) {
561 terminate_connection(c, false);
564 list_delete_list(outgoing_list);
566 if(myself && myself->connection) {
567 subnet_update(myself, NULL, false);
568 terminate_connection(myself->connection, false);
569 free_connection(myself->connection);
572 for(i = 0; i < listen_sockets; i++) {
573 close(listen_socket[i].tcp);
574 close(listen_socket[i].udp);
577 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
578 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
579 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
580 xasprintf(&envp[3], "NAME=%s", myself->name);
590 execute_script("tinc-down", envp);
592 if(myport) free(myport);
594 for(i = 0; i < 4; i++)