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.16 2002/06/02 16:06:33 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);
111 return 0; /* Woohoo. */
113 /* If it fails, try PEM_read_RSA_PUBKEY. */
114 if((fp = fopen(fname, "r")) == NULL)
116 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
117 fname, strerror(errno));
122 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
127 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
128 fname, strerror(errno));
138 /* Else, check if a harnessed public key is in the config file */
140 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
141 if((fp = fopen(fname, "r")))
143 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
152 /* Try again with PEM_read_RSA_PUBKEY. */
154 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
155 if((fp = fopen(fname, "r")))
157 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
166 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
170 int read_rsa_private_key(void)
175 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
177 myself->connection->rsa_key = RSA_new();
178 BN_hex2bn(&myself->connection->rsa_key->d, key);
179 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
184 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
185 asprintf(&fname, "%s/rsa_key.priv", confbase);
187 if(is_safe_path(fname))
189 if((fp = fopen(fname, "r")) == NULL)
191 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
192 fname, strerror(errno));
197 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
199 if(!myself->connection->rsa_key)
201 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
202 fname, strerror(errno));
213 Configure node_t myself and set up the local sockets (listen only)
215 int setup_myself(void)
219 char *name, *hostname, *mode, *afname, *cipher, *digest;
220 char *address = NULL;
221 struct addrinfo hint, *ai, *aip;
225 myself->connection = new_connection();
226 init_configuration(&myself->connection->config_tree);
228 asprintf(&myself->hostname, _("MYSELF"));
229 asprintf(&myself->connection->hostname, _("MYSELF"));
231 myself->connection->options = 0;
232 myself->connection->protocol_version = PROT_CURRENT;
234 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
236 syslog(LOG_ERR, _("Name for tinc daemon required!"));
242 syslog(LOG_ERR, _("Invalid name for myself!"));
248 myself->connection->name = xstrdup(name);
251 if(read_rsa_private_key())
254 if(read_connection_config(myself->connection))
256 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
260 if(read_rsa_public_key(myself->connection))
264 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
265 asprintf(&myport, "655");
267 /* Read in all the subnets specified in the host configuration file */
269 cfg = lookup_config(myself->connection->config_tree, "Subnet");
273 if(!get_config_subnet(cfg, &subnet))
276 subnet_add(myself, subnet);
278 cfg = lookup_config_next(myself->connection->config_tree, cfg);
282 /* Check some options */
284 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
286 myself->options |= OPTION_INDIRECT;
288 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
290 myself->options |= OPTION_TCPONLY;
292 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
294 myself->options |= OPTION_INDIRECT;
296 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
298 myself->options |= OPTION_TCPONLY;
300 if(myself->options & OPTION_TCPONLY)
301 myself->options |= OPTION_INDIRECT;
303 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
305 if(!strcasecmp(mode, "router"))
306 routing_mode = RMODE_ROUTER;
307 else if (!strcasecmp(mode, "switch"))
308 routing_mode = RMODE_SWITCH;
309 else if (!strcasecmp(mode, "hub"))
310 routing_mode = RMODE_HUB;
313 syslog(LOG_ERR, _("Invalid routing mode!"));
319 routing_mode = RMODE_ROUTER;
321 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
322 #if !defined(SOL_IP) || !defined(IP_TOS)
323 if(priorityinheritance)
324 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
327 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
330 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
334 syslog(LOG_ERR, _("Bogus maximum timeout!"));
341 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
343 if(!strcasecmp(afname, "IPv4"))
344 addressfamily = AF_INET;
345 else if (!strcasecmp(afname, "IPv6"))
346 addressfamily = AF_INET6;
347 else if (!strcasecmp(afname, "any"))
348 addressfamily = AF_UNSPEC;
351 syslog(LOG_ERR, _("Invalid address family!"));
357 addressfamily = AF_INET;
359 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
361 /* Generate packet encryption key */
363 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
365 if(!strcasecmp(cipher, "none"))
367 myself->cipher = NULL;
371 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
373 syslog(LOG_ERR, _("Unrecognized cipher type!"));
379 myself->cipher = EVP_bf_cbc();
382 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
384 myself->keylength = 1;
386 myself->connection->outcipher = EVP_bf_ofb();
388 myself->key = (char *)xmalloc(myself->keylength);
389 RAND_pseudo_bytes(myself->key, myself->keylength);
391 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
394 keyexpires = now + keylifetime;
396 /* Check if we want to use message authentication codes... */
398 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
400 if(!strcasecmp(digest, "none"))
402 myself->digest = NULL;
406 if(!(myself->digest = EVP_get_digestbyname(digest)))
408 syslog(LOG_ERR, _("Unrecognized digest type!"));
414 myself->digest = EVP_sha1();
416 myself->connection->outdigest = EVP_sha1();
418 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
422 if(myself->maclength > myself->digest->md_size)
424 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
427 else if (myself->maclength < 0)
429 syslog(LOG_ERR, _("Bogus MAC length!"));
435 myself->maclength = 4;
437 myself->connection->outmaclength = 0;
441 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
443 if(myself->compression < 0 || myself->compression > 9)
445 syslog(LOG_ERR, _("Bogus compression level!"));
450 myself->compression = 0;
452 myself->connection->outcompression = 0;
456 myself->nexthop = myself;
457 myself->via = myself;
458 myself->status.active = 1;
459 myself->status.reachable = 1;
467 memset(&hint, 0, sizeof(hint));
469 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
471 hint.ai_family = addressfamily;
472 hint.ai_socktype = SOCK_STREAM;
473 hint.ai_protocol = IPPROTO_TCP;
474 hint.ai_flags = AI_PASSIVE;
476 if((err = getaddrinfo(address, myport, &hint, &ai)) || !ai)
478 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
482 for(aip = ai; aip; aip = aip->ai_next)
484 if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
487 if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
490 if(debug_lvl >= DEBUG_CONNECTIONS)
492 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
493 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
497 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
504 syslog(LOG_NOTICE, _("Ready"));
507 syslog(LOG_ERR, _("Unable to create any listening socket!"));
515 setup all initial network connections
517 int setup_network_connections(void)
529 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
539 if(setup_device() < 0)
542 /* Run tinc-up script to further initialize the tap interface */
543 execute_script("tinc-up");
545 if(setup_myself() < 0)
548 try_outgoing_connections();
554 close all open network connections
556 void close_network_connections(void)
558 avl_node_t *node, *next;
562 for(node = connection_tree->head; node; node = next)
565 c = (connection_t *)node->data;
567 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
568 terminate_connection(c, 0);
571 if(myself && myself->connection)
572 terminate_connection(myself->connection, 0);
574 for(i = 0; i < listen_sockets; i++)
576 close(listen_socket[i].tcp);
577 close(listen_socket[i].udp);
587 execute_script("tinc-down");