3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: net_setup.c,v 1.1.2.47 2003/12/07 14:28:39 guus Exp $
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 asprintf(&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 asprintf(&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)
158 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
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->e, "FFFF");
167 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
168 asprintf(&fname, "%s/rsa_key.priv", confbase);
170 fp = fopen(fname, "r");
173 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
174 fname, strerror(errno));
179 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
180 if(fstat(fileno(fp), &s)) {
181 logger(LOG_ERR, _("Could not stat RSA private key file `%s': %s'"),
182 fname, strerror(errno));
187 if(s.st_mode & ~0100700)
188 logger(LOG_WARNING, _("Warning: insecure file permissions for RSA private key file `%s'!"), fname);
191 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
194 if(!myself->connection->rsa_key) {
195 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
196 fname, strerror(errno));
206 Configure node_t myself and set up the local sockets (listen only)
208 bool setup_myself(void)
212 char *name, *hostname, *mode, *afname, *cipher, *digest;
213 char *address = NULL;
215 struct addrinfo *ai, *aip, hint = {0};
222 myself->connection = new_connection();
223 init_configuration(&myself->connection->config_tree);
225 asprintf(&myself->hostname, _("MYSELF"));
226 asprintf(&myself->connection->hostname, _("MYSELF"));
228 myself->connection->options = 0;
229 myself->connection->protocol_version = PROT_CURRENT;
231 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
232 logger(LOG_ERR, _("Name for tinc daemon required!"));
236 if(!check_id(name)) {
237 logger(LOG_ERR, _("Invalid name for myself!"));
243 myself->connection->name = xstrdup(name);
245 if(!read_rsa_private_key())
248 if(!read_connection_config(myself->connection)) {
249 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
253 if(!read_rsa_public_key(myself->connection))
256 if(!get_config_string
257 (lookup_config(myself->connection->config_tree, "Port"), &myport))
258 asprintf(&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))
277 myself->options |= OPTION_INDIRECT;
279 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
281 myself->options |= OPTION_TCPONLY;
283 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
285 myself->options |= OPTION_INDIRECT;
287 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
289 myself->options |= OPTION_TCPONLY;
291 if(myself->options & OPTION_TCPONLY)
292 myself->options |= OPTION_INDIRECT;
294 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
296 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
297 if(!strcasecmp(mode, "router"))
298 routing_mode = RMODE_ROUTER;
299 else if(!strcasecmp(mode, "switch"))
300 routing_mode = RMODE_SWITCH;
301 else if(!strcasecmp(mode, "hub"))
302 routing_mode = RMODE_HUB;
304 logger(LOG_ERR, _("Invalid routing mode!"));
309 routing_mode = RMODE_ROUTER;
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, _("PriorityInheritance not supported on this platform"));
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->cipher = NULL;
352 myself->cipher = EVP_get_cipherbyname(cipher);
354 if(!myself->cipher) {
355 logger(LOG_ERR, _("Unrecognized cipher type!"));
360 myself->cipher = EVP_bf_cbc();
363 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
365 myself->keylength = 1;
367 myself->connection->outcipher = EVP_bf_ofb();
369 myself->key = xmalloc(myself->keylength);
370 RAND_pseudo_bytes(myself->key, myself->keylength);
372 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
375 keyexpires = now + keylifetime;
378 EVP_CIPHER_CTX_init(&packet_ctx);
379 if(!EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len)) {
380 logger(LOG_ERR, _("Error during initialisation of cipher for %s (%s): %s"),
381 myself->name, myself->hostname, ERR_error_string(ERR_get_error(), NULL));
387 /* Check if we want to use message authentication codes... */
390 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
391 if(!strcasecmp(digest, "none")) {
392 myself->digest = NULL;
394 myself->digest = EVP_get_digestbyname(digest);
396 if(!myself->digest) {
397 logger(LOG_ERR, _("Unrecognized digest type!"));
402 myself->digest = EVP_sha1();
404 myself->connection->outdigest = EVP_sha1();
406 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"),
407 &myself->maclength)) {
409 if(myself->maclength > myself->digest->md_size) {
410 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
412 } else if(myself->maclength < 0) {
413 logger(LOG_ERR, _("Bogus MAC length!"));
418 myself->maclength = 4;
420 myself->connection->outmaclength = 0;
424 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"),
425 &myself->compression)) {
426 if(myself->compression < 0 || myself->compression > 11) {
427 logger(LOG_ERR, _("Bogus compression level!"));
431 myself->compression = 0;
433 myself->connection->outcompression = 0;
437 myself->nexthop = myself;
438 myself->via = myself;
439 myself->status.active = true;
440 myself->status.reachable = true;
450 /* Run tinc-up script to further initialize the tap interface */
451 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
452 asprintf(&envp[1], "DEVICE=%s", device ? : "");
453 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
454 asprintf(&envp[3], "NAME=%s", myself->name);
457 execute_script("tinc-up", envp);
459 for(i = 0; i < 5; i++)
464 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
466 hint.ai_family = addressfamily;
467 hint.ai_socktype = SOCK_STREAM;
468 hint.ai_protocol = IPPROTO_TCP;
469 hint.ai_flags = AI_PASSIVE;
471 err = getaddrinfo(address, myport, &hint, &ai);
474 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
481 for(aip = ai; aip; aip = aip->ai_next) {
482 listen_socket[listen_sockets].tcp =
483 setup_listen_socket((sockaddr_t *) aip->ai_addr);
485 if(listen_socket[listen_sockets].tcp < 0)
488 listen_socket[listen_sockets].udp =
489 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
491 if(listen_socket[listen_sockets].udp < 0)
494 ifdebug(CONNECTIONS) {
495 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
496 logger(LOG_NOTICE, _("Listening on %s"), hostname);
500 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
507 logger(LOG_NOTICE, _("Ready"));
509 logger(LOG_ERR, _("Unable to create any listening socket!"));
517 setup all initial network connections
519 bool setup_network_connections(void)
532 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
533 if(pingtimeout < 1) {
542 try_outgoing_connections();
548 close all open network connections
550 void close_network_connections(void)
552 avl_node_t *node, *next;
559 for(node = connection_tree->head; node; node = next) {
564 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
565 terminate_connection(c, false);
568 if(myself && myself->connection)
569 terminate_connection(myself->connection, false);
571 for(i = 0; i < listen_sockets; i++) {
572 close(listen_socket[i].tcp);
573 close(listen_socket[i].udp);
583 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
584 asprintf(&envp[1], "DEVICE=%s", device ? : "");
585 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
586 asprintf(&envp[3], "NAME=%s", myself->name);
589 execute_script("tinc-down", envp);
591 for(i = 0; i < 4; i++)