3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <openssl/pem.h>
25 #include <openssl/rsa.h>
26 #include <openssl/rand.h>
27 #include <openssl/err.h>
28 #include <openssl/evp.h>
32 #include "connection.h"
48 bool read_rsa_public_key(connection_t *c) {
54 c->rsa_key = RSA_new();
55 // RSA_blinding_on(c->rsa_key, NULL);
58 /* First, check for simple PublicKey statement */
60 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
61 BN_hex2bn(&c->rsa_key->n, key);
62 BN_hex2bn(&c->rsa_key->e, "FFFF");
67 /* Else, check for PublicKeyFile statement and read it */
69 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
70 fp = fopen(fname, "r");
73 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
74 fname, strerror(errno));
80 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
84 return true; /* Woohoo. */
86 /* If it fails, try PEM_read_RSA_PUBKEY. */
87 fp = fopen(fname, "r");
90 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
91 fname, strerror(errno));
97 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
101 // RSA_blinding_on(c->rsa_key, NULL);
105 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s",
106 fname, strerror(errno));
110 /* Else, check if a harnessed public key is in the config file */
112 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
113 fp = fopen(fname, "r");
116 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
125 /* Try again with PEM_read_RSA_PUBKEY. */
127 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
128 fp = fopen(fname, "r");
131 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
132 // RSA_blinding_on(c->rsa_key, NULL);
141 logger(LOG_ERR, "No public key for %s specified!", c->name);
146 bool read_rsa_private_key(void) {
148 char *fname, *key, *pubkey;
151 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
152 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &pubkey)) {
153 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
156 myself->connection->rsa_key = RSA_new();
157 // RSA_blinding_on(myself->connection->rsa_key, NULL);
158 BN_hex2bn(&myself->connection->rsa_key->d, key);
159 BN_hex2bn(&myself->connection->rsa_key->n, pubkey);
160 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
166 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
167 xasprintf(&fname, "%s/rsa_key.priv", confbase);
169 fp = fopen(fname, "r");
172 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
173 fname, strerror(errno));
178 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
179 if(fstat(fileno(fp), &s)) {
180 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
181 fname, strerror(errno));
186 if(s.st_mode & ~0100700)
187 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
190 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
193 if(!myself->connection->rsa_key) {
194 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
195 fname, strerror(errno));
205 Configure node_t myself and set up the local sockets (listen only)
207 bool setup_myself(void) {
210 char *name, *hostname, *mode, *afname, *cipher, *digest;
211 char *address = NULL;
213 struct addrinfo *ai, *aip, hint = {0};
218 myself->connection = new_connection();
219 init_configuration(&myself->connection->config_tree);
221 myself->hostname = xstrdup("MYSELF");
222 myself->connection->hostname = xstrdup("MYSELF");
224 myself->connection->options = 0;
225 myself->connection->protocol_version = PROT_CURRENT;
227 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
228 logger(LOG_ERR, "Name for tinc daemon required!");
232 if(!check_id(name)) {
233 logger(LOG_ERR, "Invalid name for myself!");
239 myself->connection->name = xstrdup(name);
241 if(!read_connection_config(myself->connection)) {
242 logger(LOG_ERR, "Cannot open host configuration file for myself!");
246 if(!read_rsa_private_key())
249 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)
250 && !get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
251 myport = xstrdup("655");
253 /* Read in all the subnets specified in the host configuration file */
255 cfg = lookup_config(myself->connection->config_tree, "Subnet");
258 if(!get_config_subnet(cfg, &subnet))
261 subnet_add(myself, subnet);
263 cfg = lookup_config_next(myself->connection->config_tree, cfg);
266 /* Check some options */
268 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
269 myself->options |= OPTION_INDIRECT;
271 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
272 myself->options |= OPTION_TCPONLY;
274 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
275 myself->options |= OPTION_INDIRECT;
277 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
278 myself->options |= OPTION_TCPONLY;
280 if(myself->options & OPTION_TCPONLY)
281 myself->options |= OPTION_INDIRECT;
283 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
285 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
286 if(!strcasecmp(mode, "router"))
287 routing_mode = RMODE_ROUTER;
288 else if(!strcasecmp(mode, "switch"))
289 routing_mode = RMODE_SWITCH;
290 else if(!strcasecmp(mode, "hub"))
291 routing_mode = RMODE_HUB;
293 logger(LOG_ERR, "Invalid routing mode!");
298 routing_mode = RMODE_ROUTER;
301 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
302 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
304 myself->options |= OPTION_PMTU_DISCOVERY;
307 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
308 get_config_bool(lookup_config(myself->connection->config_tree, "ClampMSS"), &choice);
310 myself->options |= OPTION_CLAMP_MSS;
312 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
314 #if !defined(SOL_IP) || !defined(IP_TOS)
315 if(priorityinheritance)
316 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
319 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
322 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
323 if(maxtimeout <= 0) {
324 logger(LOG_ERR, "Bogus maximum timeout!");
330 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
331 if(!strcasecmp(afname, "IPv4"))
332 addressfamily = AF_INET;
333 else if(!strcasecmp(afname, "IPv6"))
334 addressfamily = AF_INET6;
335 else if(!strcasecmp(afname, "any"))
336 addressfamily = AF_UNSPEC;
338 logger(LOG_ERR, "Invalid address family!");
344 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
346 /* Generate packet encryption key */
349 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
350 if(!strcasecmp(cipher, "none")) {
351 myself->incipher = NULL;
353 myself->incipher = EVP_get_cipherbyname(cipher);
355 if(!myself->incipher) {
356 logger(LOG_ERR, "Unrecognized cipher type!");
361 myself->incipher = EVP_bf_cbc();
364 myself->inkeylength = myself->incipher->key_len + myself->incipher->iv_len;
366 myself->inkeylength = 1;
368 myself->connection->outcipher = EVP_bf_ofb();
370 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
373 keyexpires = now + keylifetime;
375 /* Check if we want to use message authentication codes... */
377 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
378 if(!strcasecmp(digest, "none")) {
379 myself->indigest = NULL;
381 myself->indigest = EVP_get_digestbyname(digest);
383 if(!myself->indigest) {
384 logger(LOG_ERR, "Unrecognized digest type!");
389 myself->indigest = EVP_sha1();
391 myself->connection->outdigest = EVP_sha1();
393 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->inmaclength)) {
394 if(myself->indigest) {
395 if(myself->inmaclength > myself->indigest->md_size) {
396 logger(LOG_ERR, "MAC length exceeds size of digest!");
398 } else if(myself->inmaclength < 0) {
399 logger(LOG_ERR, "Bogus MAC length!");
404 myself->inmaclength = 4;
406 myself->connection->outmaclength = 0;
410 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
411 if(myself->incompression < 0 || myself->incompression > 11) {
412 logger(LOG_ERR, "Bogus compression level!");
416 myself->incompression = 0;
418 myself->connection->outcompression = 0;
422 myself->nexthop = myself;
423 myself->via = myself;
424 myself->status.reachable = true;
434 /* Run tinc-up script to further initialize the tap interface */
435 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
436 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
437 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
438 xasprintf(&envp[3], "NAME=%s", myself->name);
441 execute_script("tinc-up", envp);
443 for(i = 0; i < 5; i++)
446 /* Run subnet-up scripts for our own subnets */
448 subnet_update(myself, NULL, true);
452 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
454 hint.ai_family = addressfamily;
455 hint.ai_socktype = SOCK_STREAM;
456 hint.ai_protocol = IPPROTO_TCP;
457 hint.ai_flags = AI_PASSIVE;
459 err = getaddrinfo(address, myport, &hint, &ai);
462 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
469 for(aip = ai; aip; aip = aip->ai_next) {
470 listen_socket[listen_sockets].tcp =
471 setup_listen_socket((sockaddr_t *) aip->ai_addr);
473 if(listen_socket[listen_sockets].tcp < 0)
476 listen_socket[listen_sockets].udp =
477 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
479 if(listen_socket[listen_sockets].udp < 0)
482 ifdebug(CONNECTIONS) {
483 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
484 logger(LOG_NOTICE, "Listening on %s", hostname);
488 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
495 logger(LOG_NOTICE, "Ready");
497 logger(LOG_ERR, "Unable to create any listening socket!");
507 bool setup_network(void) {
517 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
518 if(pinginterval < 1) {
519 pinginterval = 86400;
524 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
526 if(pingtimeout < 1 || pingtimeout > pinginterval)
527 pingtimeout = pinginterval;
529 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
530 maxoutbufsize = 10 * MTU;
539 close all open network connections
541 void close_network_connections(void) {
542 avl_node_t *node, *next;
547 for(node = connection_tree->head; node; node = next) {
551 terminate_connection(c, false);
554 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
555 outgoing_t *outgoing = node->data;
558 event_del(outgoing->event);
561 list_delete_list(outgoing_list);
563 if(myself && myself->connection) {
564 subnet_update(myself, NULL, false);
565 terminate_connection(myself->connection, false);
566 free_connection(myself->connection);
569 for(i = 0; i < listen_sockets; i++) {
570 close(listen_socket[i].tcp);
571 close(listen_socket[i].udp);
574 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
575 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
576 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
577 xasprintf(&envp[3], "NAME=%s", myself->name);
587 execute_script("tinc-down", envp);
589 if(myport) free(myport);
591 for(i = 0; i < 4; i++)