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.4 2002/04/28 12:46:26 zarq Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 /* SunOS really wants sys/socket.h BEFORE net/if.h,
42 and FreeBSD wants these lines below the rest. */
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
48 #include <openssl/pem.h>
49 #include <openssl/rsa.h>
50 #include <openssl/rand.h>
63 #include "connection.h"
81 int read_rsa_public_key(connection_t *c)
89 c->rsa_key = RSA_new();
93 /* First, check for simple PublicKey statement */
95 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
98 BN_hex2bn(&c->rsa_key->n, key);
99 BN_hex2bn(&c->rsa_key->e, "FFFF");
102 int rc = gcry_sexp_build(&c->rsa_key, NULL, "(public-key(rsa(n%s)(e%s)))",
106 syslog(LOG_ERR, _("gcry_sexp_build error: %d (%s)"),
107 rc, gcry_strerror(-1));
116 /* Else, check for PublicKeyFile statement and read it */
118 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
120 if(is_safe_path(fname))
122 if((fp = fopen(fname, "r")) == NULL)
124 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
125 fname, strerror(errno));
130 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
134 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
135 fname, strerror(errno));
147 /* Else, check if a harnessed public key is in the config file */
149 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
150 if((fp = fopen(fname, "r")))
152 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
162 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
167 syslog(LOG_ERR, _("Only PublicKey statements are supported when using gcrypt for now."));
172 int read_rsa_private_key(void)
180 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
183 myself->connection->rsa_key = RSA_new();
184 BN_hex2bn(&myself->connection->rsa_key->d, key);
185 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
188 int rc = gcry_sexp_build(&myself->connection->rsa_key, NULL,
189 "(public-key(rsa(n%s)(e%s)))",
193 syslog(LOG_ERR, _("gcry_sexp_build error: %d (%s)"),
194 rc, gcry_strerror(-1));
203 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
204 asprintf(&fname, "%s/rsa_key.priv", confbase);
206 if(is_safe_path(fname))
208 if((fp = fopen(fname, "r")) == NULL)
210 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
211 fname, strerror(errno));
216 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
218 if(!myself->connection->rsa_key)
220 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
221 fname, strerror(errno));
231 syslog(LOG_ERR, _("Only PrivateKey statements are supported when using gcrypt for now."));
237 Configure node_t myself and set up the local sockets (listen only)
239 int setup_myself(void)
243 char *name, *hostname, *mode, *afname, *cipher, *digest;
244 struct addrinfo hint, *ai, *aip;
248 myself->connection = new_connection();
249 init_configuration(&myself->connection->config_tree);
251 asprintf(&myself->hostname, _("MYSELF"));
252 asprintf(&myself->connection->hostname, _("MYSELF"));
254 myself->connection->options = 0;
255 myself->connection->protocol_version = PROT_CURRENT;
257 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
259 syslog(LOG_ERR, _("Name for tinc daemon required!"));
265 syslog(LOG_ERR, _("Invalid name for myself!"));
271 myself->connection->name = xstrdup(name);
274 if(read_rsa_private_key())
277 if(read_connection_config(myself->connection))
279 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
283 if(read_rsa_public_key(myself->connection))
287 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
288 asprintf(&myport, "655");
290 /* Read in all the subnets specified in the host configuration file */
292 cfg = lookup_config(myself->connection->config_tree, "Subnet");
296 if(!get_config_subnet(cfg, &subnet))
299 subnet_add(myself, subnet);
301 cfg = lookup_config_next(myself->connection->config_tree, cfg);
305 /* Check some options */
307 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
309 myself->options |= OPTION_INDIRECT;
311 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
313 myself->options |= OPTION_TCPONLY;
315 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
317 myself->options |= OPTION_INDIRECT;
319 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
321 myself->options |= OPTION_TCPONLY;
323 if(myself->options & OPTION_TCPONLY)
324 myself->options |= OPTION_INDIRECT;
326 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
328 if(!strcasecmp(mode, "router"))
329 routing_mode = RMODE_ROUTER;
330 else if (!strcasecmp(mode, "switch"))
331 routing_mode = RMODE_SWITCH;
332 else if (!strcasecmp(mode, "hub"))
333 routing_mode = RMODE_HUB;
336 syslog(LOG_ERR, _("Invalid routing mode!"));
342 routing_mode = RMODE_ROUTER;
344 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
345 #if !defined(SOL_IP) || !defined(IP_TOS)
346 if(priorityinheritance)
347 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
350 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
353 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
357 syslog(LOG_ERR, _("Bogus maximum timeout!"));
364 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
366 if(!strcasecmp(afname, "IPv4"))
367 addressfamily = AF_INET;
368 else if (!strcasecmp(afname, "IPv6"))
369 addressfamily = AF_INET6;
370 else if (!strcasecmp(afname, "any"))
371 addressfamily = AF_UNSPEC;
374 syslog(LOG_ERR, _("Invalid address family!"));
380 addressfamily = AF_INET;
382 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
384 /* Generate packet encryption key */
386 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
388 if(!strcasecmp(cipher, "none"))
391 myself->cipher = NULL;
394 myself->cipher = gcry_cipher_open(GCRY_CIPHER_NONE, GCRY_CIPHER_MODE_NONE, 0);
400 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
404 myself->cipher = gcry_cipher_open(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 0);
408 syslog(LOG_ERR, _("Unrecognized cipher type!"));
416 myself->cipher = EVP_bf_cbc();
419 myself->cipher = gcry_cipher_open(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC, 0);
425 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
429 myself->keylength = 16; /* FIXME */
432 myself->keylength = 1;
435 myself->connection->outcipher = EVP_bf_ofb();
438 /* FIXME: CHANGE this to something like aes - but openssl
439 compatibility mode for now */
440 myself->connection->outcipher = gcry_cipher_open(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_OFB, 0);
444 myself->key = (char *)xmalloc(myself->keylength);
445 RAND_pseudo_bytes(myself->key, myself->keylength);
448 myself->key = gcry_random_bytes(myself->keylength, GCRY_WEAK_RANDOM);
451 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
454 keyexpires = now + keylifetime;
456 /* Check if we want to use message authentication codes... */
458 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
460 if(!strcasecmp(digest, "none"))
463 myself->digest = NULL;
466 myself->digest = gcry_md_open(GCRY_MD_NONE, GCRY_MD_FLAG_HMAC);
472 if(!(myself->digest = EVP_get_digestbyname(digest)))
476 if(!(myself->digest = gcry_md_open(GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC)))
479 syslog(LOG_ERR, _("Unrecognized digest type!"));
486 myself->digest = EVP_sha1();
489 myself->digest = gcry_md_open(GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
493 myself->connection->outdigest = EVP_sha1();
496 myself->connection->outdigest = gcry_md_open(GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
499 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
504 if(myself->maclength > myself->digest->md_size)
506 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
509 else if (myself->maclength < 0)
511 syslog(LOG_ERR, _("Bogus MAC length!"));
517 myself->maclength = 12;
522 myself->maclength = 4;
524 myself->connection->outmaclength = 0;
528 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
530 if(myself->compression < 0 || myself->compression > 9)
532 syslog(LOG_ERR, _("Bogus compression level!"));
537 myself->compression = 0;
539 myself->connection->outcompression = 0;
543 myself->nexthop = myself;
544 myself->via = myself;
545 myself->status.active = 1;
546 myself->status.reachable = 1;
554 memset(&hint, 0, sizeof(hint));
556 hint.ai_family = addressfamily;
557 hint.ai_socktype = SOCK_STREAM;
558 hint.ai_protocol = IPPROTO_TCP;
559 hint.ai_flags = AI_PASSIVE;
561 if((err = getaddrinfo(NULL, myport, &hint, &ai)) || !ai)
563 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
567 for(aip = ai; aip; aip = aip->ai_next)
569 if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
572 if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
575 if(debug_lvl >= DEBUG_CONNECTIONS)
577 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
578 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
582 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
589 syslog(LOG_NOTICE, _("Ready"));
592 syslog(LOG_ERR, _("Unable to create any listening socket!"));
600 setup all initial network connections
602 int setup_network_connections(void)
614 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
624 if(setup_device() < 0)
627 /* Run tinc-up script to further initialize the tap interface */
628 execute_script("tinc-up");
630 if(setup_myself() < 0)
633 try_outgoing_connections();
639 close all open network connections
641 void close_network_connections(void)
643 avl_node_t *node, *next;
647 for(node = connection_tree->head; node; node = next)
650 c = (connection_t *)node->data;
652 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
653 terminate_connection(c, 0);
656 if(myself && myself->connection)
657 terminate_connection(myself->connection, 0);
659 for(i = 0; i < listen_sockets; i++)
661 close(listen_socket[i].tcp);
662 close(listen_socket[i].udp);
672 execute_script("tinc-down");