3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.13 2002/03/25 13:54:49 guus Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43 and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
48 #include <openssl/pem.h>
49 #include <openssl/rsa.h>
50 #include <openssl/rand.h>
58 #include "connection.h"
75 int read_rsa_public_key(connection_t *c)
82 c->rsa_key = RSA_new();
84 /* First, check for simple PublicKey statement */
86 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
88 BN_hex2bn(&c->rsa_key->n, key);
89 BN_hex2bn(&c->rsa_key->e, "FFFF");
94 /* Else, check for PublicKeyFile statement and read it */
96 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
98 if(is_safe_path(fname))
100 if((fp = fopen(fname, "r")) == NULL)
102 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
103 fname, strerror(errno));
108 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
112 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
113 fname, strerror(errno));
125 /* Else, check if a harnessed public key is in the config file */
127 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
128 if((fp = fopen(fname, "r")))
130 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
140 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
145 int read_rsa_private_key(void)
150 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
152 myself->connection->rsa_key = RSA_new();
153 BN_hex2bn(&myself->connection->rsa_key->d, key);
154 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
159 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
160 asprintf(&fname, "%s/rsa_key.priv", confbase);
162 if(is_safe_path(fname))
164 if((fp = fopen(fname, "r")) == NULL)
166 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
167 fname, strerror(errno));
172 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
174 if(!myself->connection->rsa_key)
176 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
177 fname, strerror(errno));
187 int check_rsa_key(RSA *rsa_key)
189 char *test1, *test2, *test3;
191 if(rsa_key->p && rsa_key->q)
193 if(RSA_check_key(rsa_key) != 1)
198 test1 = xmalloc(RSA_size(rsa_key));
199 test2 = xmalloc(RSA_size(rsa_key));
200 test3 = xmalloc(RSA_size(rsa_key));
202 if(RSA_public_encrypt(RSA_size(rsa_key), test1, test2, rsa_key, RSA_NO_PADDING) != RSA_size(rsa_key))
205 if(RSA_private_decrypt(RSA_size(rsa_key), test2, test3, rsa_key, RSA_NO_PADDING) != RSA_size(rsa_key))
208 if(memcmp(test1, test3, RSA_size(rsa_key)))
216 Configure node_t myself and set up the local sockets (listen only)
218 int setup_myself(void)
222 char *name, *hostname, *mode, *afname, *cipher, *digest;
223 struct addrinfo hint, *ai, *aip;
227 myself->connection = new_connection();
228 init_configuration(&myself->connection->config_tree);
230 asprintf(&myself->hostname, _("MYSELF"));
231 asprintf(&myself->connection->hostname, _("MYSELF"));
233 myself->connection->options = 0;
234 myself->connection->protocol_version = PROT_CURRENT;
236 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
238 syslog(LOG_ERR, _("Name for tinc daemon required!"));
244 syslog(LOG_ERR, _("Invalid name for myself!"));
250 myself->connection->name = xstrdup(name);
253 if(read_rsa_private_key())
256 if(read_connection_config(myself->connection))
258 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
262 if(read_rsa_public_key(myself->connection))
266 if(check_rsa_key(myself->connection->rsa_key))
268 syslog(LOG_ERR, _("Invalid public/private keypair!"));
272 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
273 asprintf(&myport, "655");
275 /* Read in all the subnets specified in the host configuration file */
277 cfg = lookup_config(myself->connection->config_tree, "Subnet");
281 if(!get_config_subnet(cfg, &subnet))
284 subnet_add(myself, subnet);
286 cfg = lookup_config_next(myself->connection->config_tree, cfg);
290 /* Check some options */
292 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
294 myself->options |= OPTION_INDIRECT;
296 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
298 myself->options |= OPTION_TCPONLY;
300 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
302 myself->options |= OPTION_INDIRECT;
304 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
306 myself->options |= OPTION_TCPONLY;
308 if(myself->options & OPTION_TCPONLY)
309 myself->options |= OPTION_INDIRECT;
311 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
313 if(!strcasecmp(mode, "router"))
314 routing_mode = RMODE_ROUTER;
315 else if (!strcasecmp(mode, "switch"))
316 routing_mode = RMODE_SWITCH;
317 else if (!strcasecmp(mode, "hub"))
318 routing_mode = RMODE_HUB;
321 syslog(LOG_ERR, _("Invalid routing mode!"));
327 routing_mode = RMODE_ROUTER;
329 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
330 #if !defined(SOL_IP) || !defined(IP_TOS)
331 if(priorityinheritance)
332 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
335 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
338 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
342 syslog(LOG_ERR, _("Bogus maximum timeout!"));
349 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
351 if(!strcasecmp(afname, "IPv4"))
352 addressfamily = AF_INET;
353 else if (!strcasecmp(afname, "IPv6"))
354 addressfamily = AF_INET6;
355 else if (!strcasecmp(afname, "any"))
356 addressfamily = AF_UNSPEC;
359 syslog(LOG_ERR, _("Invalid address family!"));
365 addressfamily = AF_INET;
367 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
369 /* Generate packet encryption key */
371 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
373 if(!strcasecmp(cipher, "none"))
375 myself->cipher = NULL;
379 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
381 syslog(LOG_ERR, _("Unrecognized cipher type!"));
387 myself->cipher = EVP_bf_cbc();
390 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
392 myself->keylength = 1;
394 myself->connection->outcipher = EVP_bf_ofb();
396 myself->key = (char *)xmalloc(myself->keylength);
397 RAND_pseudo_bytes(myself->key, myself->keylength);
399 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
402 keyexpires = now + keylifetime;
404 /* Check if we want to use message authentication codes... */
406 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
408 if(!strcasecmp(digest, "none"))
410 myself->digest = NULL;
414 if(!(myself->digest = EVP_get_digestbyname(digest)))
416 syslog(LOG_ERR, _("Unrecognized digest type!"));
422 myself->digest = EVP_sha1();
424 myself->connection->outdigest = EVP_sha1();
426 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
430 if(myself->maclength > myself->digest->md_size)
432 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
435 else if (myself->maclength < 0)
437 syslog(LOG_ERR, _("Bogus MAC length!"));
443 myself->maclength = 4;
445 myself->connection->outmaclength = 0;
449 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
451 if(myself->compression < 0 || myself->compression > 9)
453 syslog(LOG_ERR, _("Bogus compression level!"));
458 myself->compression = 0;
460 myself->connection->outcompression = 0;
464 myself->nexthop = myself;
465 myself->via = myself;
466 myself->status.active = 1;
467 myself->status.reachable = 1;
475 memset(&hint, 0, sizeof(hint));
477 hint.ai_family = addressfamily;
478 hint.ai_socktype = SOCK_STREAM;
479 hint.ai_protocol = IPPROTO_TCP;
480 hint.ai_flags = AI_PASSIVE;
482 if((err = getaddrinfo(NULL, myport, &hint, &ai)) || !ai)
484 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
488 for(aip = ai; aip; aip = aip->ai_next)
490 if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
493 if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
496 if(debug_lvl >= DEBUG_CONNECTIONS)
498 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
499 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
503 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
510 syslog(LOG_NOTICE, _("Ready"));
513 syslog(LOG_ERR, _("Unable to create any listening socket!"));
521 setup all initial network connections
523 int setup_network_connections(void)
535 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
545 if(setup_device() < 0)
548 /* Run tinc-up script to further initialize the tap interface */
549 execute_script("tinc-up");
551 if(setup_myself() < 0)
554 try_outgoing_connections();
560 close all open network connections
562 void close_network_connections(void)
564 avl_node_t *node, *next;
568 for(node = connection_tree->head; node; node = next)
571 c = (connection_t *)node->data;
573 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
574 terminate_connection(c, 0);
577 if(myself && myself->connection)
578 terminate_connection(myself->connection, 0);
580 for(i = 0; i < listen_sockets; i++)
582 close(listen_socket[i].tcp);
583 close(listen_socket[i].udp);
593 execute_script("tinc-down");