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.37 2003/07/22 20:55:20 guus Exp $
25 #include <openssl/pem.h>
26 #include <openssl/rsa.h>
27 #include <openssl/rand.h>
31 #include "connection.h"
47 bool 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 */
71 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
72 fp = fopen(fname, "r");
75 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
76 fname, strerror(errno));
82 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
86 return true; /* Woohoo. */
88 /* If it fails, try PEM_read_RSA_PUBKEY. */
89 fp = fopen(fname, "r");
92 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
93 fname, strerror(errno));
99 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
103 // RSA_blinding_on(c->rsa_key, NULL);
107 logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
108 fname, strerror(errno));
112 /* Else, check if a harnessed public key is in the config file */
114 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
115 fp = fopen(fname, "r");
118 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
127 /* Try again with PEM_read_RSA_PUBKEY. */
129 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
134 // RSA_blinding_on(c->rsa_key, NULL);
143 logger(LOG_ERR, _("No public key for %s specified!"), c->name);
148 bool read_rsa_private_key(void)
155 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
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->e, "FFFF");
164 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
165 asprintf(&fname, "%s/rsa_key.priv", confbase);
167 if(is_safe_path(fname)) {
168 fp = fopen(fname, "r");
171 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
172 fname, strerror(errno));
178 myself->connection->rsa_key =
179 PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
182 if(!myself->connection->rsa_key) {
183 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
184 fname, strerror(errno));
196 Configure node_t myself and set up the local sockets (listen only)
198 bool setup_myself(void)
202 char *name, *hostname, *mode, *afname, *cipher, *digest;
203 char *address = NULL;
204 struct addrinfo hint, *ai, *aip;
211 myself->connection = new_connection();
212 init_configuration(&myself->connection->config_tree);
214 asprintf(&myself->hostname, _("MYSELF"));
215 asprintf(&myself->connection->hostname, _("MYSELF"));
217 myself->connection->options = 0;
218 myself->connection->protocol_version = PROT_CURRENT;
220 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
221 logger(LOG_ERR, _("Name for tinc daemon required!"));
225 if(!check_id(name)) {
226 logger(LOG_ERR, _("Invalid name for myself!"));
232 myself->connection->name = xstrdup(name);
234 if(!read_rsa_private_key())
237 if(!read_connection_config(myself->connection)) {
238 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
242 if(!read_rsa_public_key(myself->connection))
245 if(!get_config_string
246 (lookup_config(myself->connection->config_tree, "Port"), &myport))
247 asprintf(&myport, "655");
249 /* Read in all the subnets specified in the host configuration file */
251 cfg = lookup_config(myself->connection->config_tree, "Subnet");
254 if(!get_config_subnet(cfg, &subnet))
257 subnet_add(myself, subnet);
259 cfg = lookup_config_next(myself->connection->config_tree, cfg);
262 /* Check some options */
264 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
266 myself->options |= OPTION_INDIRECT;
268 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
270 myself->options |= OPTION_TCPONLY;
272 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
274 myself->options |= OPTION_INDIRECT;
276 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
278 myself->options |= OPTION_TCPONLY;
280 if(myself->options & OPTION_TCPONLY)
281 myself->options |= OPTION_INDIRECT;
283 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
284 if(!strcasecmp(mode, "router"))
285 routing_mode = RMODE_ROUTER;
286 else if(!strcasecmp(mode, "switch"))
287 routing_mode = RMODE_SWITCH;
288 else if(!strcasecmp(mode, "hub"))
289 routing_mode = RMODE_HUB;
291 logger(LOG_ERR, _("Invalid routing mode!"));
296 routing_mode = RMODE_ROUTER;
298 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
300 #if !defined(SOL_IP) || !defined(IP_TOS)
301 if(priorityinheritance)
302 logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
305 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
308 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) {
309 if(maxtimeout <= 0) {
310 logger(LOG_ERR, _("Bogus maximum timeout!"));
316 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
317 if(!strcasecmp(afname, "IPv4"))
318 addressfamily = AF_INET;
319 else if(!strcasecmp(afname, "IPv6"))
320 addressfamily = AF_INET6;
321 else if(!strcasecmp(afname, "any"))
322 addressfamily = AF_UNSPEC;
324 logger(LOG_ERR, _("Invalid address family!"));
330 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
332 /* Generate packet encryption key */
335 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
336 if(!strcasecmp(cipher, "none")) {
337 myself->cipher = NULL;
339 myself->cipher = EVP_get_cipherbyname(cipher);
341 if(!myself->cipher) {
342 logger(LOG_ERR, _("Unrecognized cipher type!"));
347 myself->cipher = EVP_bf_cbc();
350 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
352 myself->keylength = 1;
354 myself->connection->outcipher = EVP_bf_ofb();
356 myself->key = (char *) xmalloc(myself->keylength);
357 RAND_pseudo_bytes(myself->key, myself->keylength);
359 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
362 keyexpires = now + keylifetime;
364 EVP_CIPHER_CTX_init(&packet_ctx);
365 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
367 /* Check if we want to use message authentication codes... */
370 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
371 if(!strcasecmp(digest, "none")) {
372 myself->digest = NULL;
374 myself->digest = EVP_get_digestbyname(digest);
376 if(!myself->digest) {
377 logger(LOG_ERR, _("Unrecognized digest type!"));
382 myself->digest = EVP_sha1();
384 myself->connection->outdigest = EVP_sha1();
387 (lookup_config(myself->connection->config_tree, "MACLength"),
388 &myself->maclength)) {
390 if(myself->maclength > myself->digest->md_size) {
391 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
393 } else if(myself->maclength < 0) {
394 logger(LOG_ERR, _("Bogus MAC length!"));
399 myself->maclength = 4;
401 myself->connection->outmaclength = 0;
406 (lookup_config(myself->connection->config_tree, "Compression"),
407 &myself->compression)) {
408 if(myself->compression < 0 || myself->compression > 11) {
409 logger(LOG_ERR, _("Bogus compression level!"));
413 myself->compression = 0;
415 myself->connection->outcompression = 0;
419 myself->nexthop = myself;
420 myself->via = myself;
421 myself->status.active = true;
422 myself->status.reachable = true;
429 memset(&hint, 0, sizeof(hint));
431 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
433 hint.ai_family = addressfamily;
434 hint.ai_socktype = SOCK_STREAM;
435 hint.ai_protocol = IPPROTO_TCP;
436 hint.ai_flags = AI_PASSIVE;
438 err = getaddrinfo(address, myport, &hint, &ai);
441 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
448 for(aip = ai; aip; aip = aip->ai_next) {
449 listen_socket[listen_sockets].tcp =
450 setup_listen_socket((sockaddr_t *) aip->ai_addr);
452 if(listen_socket[listen_sockets].tcp < 0)
455 listen_socket[listen_sockets].udp =
456 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
458 if(listen_socket[listen_sockets].udp < 0)
461 ifdebug(CONNECTIONS) {
462 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
463 logger(LOG_NOTICE, _("Listening on %s"), hostname);
467 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
474 logger(LOG_NOTICE, _("Ready"));
476 logger(LOG_ERR, _("Unable to create any listening socket!"));
484 setup all initial network connections
486 bool setup_network_connections(void)
502 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
503 if(pingtimeout < 1) {
515 /* Run tinc-up script to further initialize the tap interface */
516 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
517 asprintf(&envp[1], "DEVICE=%s", device ? : "");
518 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
519 asprintf(&envp[3], "NAME=%s", myself->name);
522 execute_script("tinc-up", envp);
524 for(i = 0; i < 5; i++)
527 try_outgoing_connections();
533 close all open network connections
535 void close_network_connections(void)
537 avl_node_t *node, *next;
544 for(node = connection_tree->head; node; node = next) {
546 c = (connection_t *) node->data;
549 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
550 terminate_connection(c, false);
553 if(myself && myself->connection)
554 terminate_connection(myself->connection, false);
556 for(i = 0; i < listen_sockets; i++) {
557 close(listen_socket[i].tcp);
558 close(listen_socket[i].udp);
568 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
569 asprintf(&envp[1], "DEVICE=%s", device ? : "");
570 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
571 asprintf(&envp[3], "NAME=%s", myself->name);
574 execute_script("tinc-down", envp);
576 for(i = 0; i < 4; i++)