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.36 2003/07/18 13:45:06 guus Exp $
25 #include <openssl/pem.h>
26 #include <openssl/rsa.h>
27 #include <openssl/rand.h>
31 #include "connection.h"
47 int 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 */
72 (lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
73 if(is_safe_path(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 0; /* 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));
118 /* Else, check if a harnessed public key is in the config file */
120 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
121 fp = fopen(fname, "r");
124 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
133 /* Try again with PEM_read_RSA_PUBKEY. */
135 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
136 fp = fopen(fname, "r");
139 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
140 // RSA_blinding_on(c->rsa_key, NULL);
149 logger(LOG_ERR, _("No public key for %s specified!"), c->name);
154 int read_rsa_private_key(void)
161 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
162 myself->connection->rsa_key = RSA_new();
163 // RSA_blinding_on(myself->connection->rsa_key, NULL);
164 BN_hex2bn(&myself->connection->rsa_key->d, key);
165 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
170 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
171 asprintf(&fname, "%s/rsa_key.priv", confbase);
173 if(is_safe_path(fname)) {
174 fp = fopen(fname, "r");
177 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
178 fname, strerror(errno));
184 myself->connection->rsa_key =
185 PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
188 if(!myself->connection->rsa_key) {
189 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
190 fname, strerror(errno));
202 Configure node_t myself and set up the local sockets (listen only)
204 int setup_myself(void)
208 char *name, *hostname, *mode, *afname, *cipher, *digest;
209 char *address = NULL;
210 struct addrinfo hint, *ai, *aip;
216 myself->connection = new_connection();
217 init_configuration(&myself->connection->config_tree);
219 asprintf(&myself->hostname, _("MYSELF"));
220 asprintf(&myself->connection->hostname, _("MYSELF"));
222 myself->connection->options = 0;
223 myself->connection->protocol_version = PROT_CURRENT;
225 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
226 logger(LOG_ERR, _("Name for tinc daemon required!"));
231 logger(LOG_ERR, _("Invalid name for myself!"));
237 myself->connection->name = xstrdup(name);
239 if(read_rsa_private_key())
242 if(read_connection_config(myself->connection)) {
243 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
247 if(read_rsa_public_key(myself->connection))
250 if(!get_config_string
251 (lookup_config(myself->connection->config_tree, "Port"), &myport))
252 asprintf(&myport, "655");
254 /* Read in all the subnets specified in the host configuration file */
256 cfg = lookup_config(myself->connection->config_tree, "Subnet");
259 if(!get_config_subnet(cfg, &subnet))
262 subnet_add(myself, subnet);
264 cfg = lookup_config_next(myself->connection->config_tree, cfg);
267 /* Check some options */
269 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
271 myself->options |= OPTION_INDIRECT;
273 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
275 myself->options |= OPTION_TCPONLY;
278 (lookup_config(myself->connection->config_tree, "IndirectData"),
281 myself->options |= OPTION_INDIRECT;
284 (lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
286 myself->options |= OPTION_TCPONLY;
288 if(myself->options & OPTION_TCPONLY)
289 myself->options |= OPTION_INDIRECT;
291 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
292 if(!strcasecmp(mode, "router"))
293 routing_mode = RMODE_ROUTER;
294 else if(!strcasecmp(mode, "switch"))
295 routing_mode = RMODE_SWITCH;
296 else if(!strcasecmp(mode, "hub"))
297 routing_mode = RMODE_HUB;
299 logger(LOG_ERR, _("Invalid routing mode!"));
304 routing_mode = RMODE_ROUTER;
306 get_config_bool(lookup_config(config_tree, "PriorityInheritance"),
307 &priorityinheritance);
308 #if !defined(SOL_IP) || !defined(IP_TOS)
309 if(priorityinheritance)
310 logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
313 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
317 (lookup_config(myself->connection->config_tree, "MaxTimeout"),
319 if(maxtimeout <= 0) {
320 logger(LOG_ERR, _("Bogus maximum timeout!"));
326 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
327 if(!strcasecmp(afname, "IPv4"))
328 addressfamily = AF_INET;
329 else if(!strcasecmp(afname, "IPv6"))
330 addressfamily = AF_INET6;
331 else if(!strcasecmp(afname, "any"))
332 addressfamily = AF_UNSPEC;
334 logger(LOG_ERR, _("Invalid address family!"));
340 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
342 /* Generate packet encryption key */
345 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
346 if(!strcasecmp(cipher, "none")) {
347 myself->cipher = NULL;
349 myself->cipher = EVP_get_cipherbyname(cipher);
351 if(!myself->cipher) {
352 logger(LOG_ERR, _("Unrecognized cipher type!"));
357 myself->cipher = EVP_bf_cbc();
360 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
362 myself->keylength = 1;
364 myself->connection->outcipher = EVP_bf_ofb();
366 myself->key = (char *) xmalloc(myself->keylength);
367 RAND_pseudo_bytes(myself->key, myself->keylength);
369 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
372 keyexpires = now + keylifetime;
374 EVP_CIPHER_CTX_init(&packet_ctx);
375 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
377 /* Check if we want to use message authentication codes... */
380 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
381 if(!strcasecmp(digest, "none")) {
382 myself->digest = NULL;
384 myself->digest = EVP_get_digestbyname(digest);
386 if(!myself->digest) {
387 logger(LOG_ERR, _("Unrecognized digest type!"));
392 myself->digest = EVP_sha1();
394 myself->connection->outdigest = EVP_sha1();
397 (lookup_config(myself->connection->config_tree, "MACLength"),
398 &myself->maclength)) {
400 if(myself->maclength > myself->digest->md_size) {
401 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
403 } else if(myself->maclength < 0) {
404 logger(LOG_ERR, _("Bogus MAC length!"));
409 myself->maclength = 4;
411 myself->connection->outmaclength = 0;
416 (lookup_config(myself->connection->config_tree, "Compression"),
417 &myself->compression)) {
418 if(myself->compression < 0 || myself->compression > 11) {
419 logger(LOG_ERR, _("Bogus compression level!"));
423 myself->compression = 0;
425 myself->connection->outcompression = 0;
429 myself->nexthop = myself;
430 myself->via = myself;
431 myself->status.active = 1;
432 myself->status.reachable = 1;
439 memset(&hint, 0, sizeof(hint));
441 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
443 hint.ai_family = addressfamily;
444 hint.ai_socktype = SOCK_STREAM;
445 hint.ai_protocol = IPPROTO_TCP;
446 hint.ai_flags = AI_PASSIVE;
448 err = getaddrinfo(address, myport, &hint, &ai);
451 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
458 for(aip = ai; aip; aip = aip->ai_next) {
459 listen_socket[listen_sockets].tcp =
460 setup_listen_socket((sockaddr_t *) aip->ai_addr);
462 if(listen_socket[listen_sockets].tcp < 0)
465 listen_socket[listen_sockets].udp =
466 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
468 if(listen_socket[listen_sockets].udp < 0)
471 ifdebug(CONNECTIONS) {
472 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
473 logger(LOG_NOTICE, _("Listening on %s"), hostname);
477 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
484 logger(LOG_NOTICE, _("Ready"));
486 logger(LOG_ERR, _("Unable to create any listening socket!"));
494 setup all initial network connections
496 int setup_network_connections(void)
512 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
513 if(pingtimeout < 1) {
519 if(setup_device() < 0)
522 if(setup_myself() < 0)
525 /* Run tinc-up script to further initialize the tap interface */
526 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
527 asprintf(&envp[1], "DEVICE=%s", device ? : "");
528 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
529 asprintf(&envp[3], "NAME=%s", myself->name);
532 execute_script("tinc-up", envp);
534 for(i = 0; i < 5; i++)
537 try_outgoing_connections();
543 close all open network connections
545 void close_network_connections(void)
547 avl_node_t *node, *next;
554 for(node = connection_tree->head; node; node = next) {
556 c = (connection_t *) node->data;
559 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
560 terminate_connection(c, 0);
563 if(myself && myself->connection)
564 terminate_connection(myself->connection, 0);
566 for(i = 0; i < listen_sockets; i++) {
567 close(listen_socket[i].tcp);
568 close(listen_socket[i].udp);
578 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
579 asprintf(&envp[1], "DEVICE=%s", device ? : "");
580 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
581 asprintf(&envp[3], "NAME=%s", myself->name);
584 execute_script("tinc-down", envp);
586 for(i = 0; i < 4; i++)