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.41 2003/07/30 11:50:45 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;
205 struct addrinfo *ai, *aip, hint = {0};
212 myself->connection = new_connection();
213 init_configuration(&myself->connection->config_tree);
215 asprintf(&myself->hostname, _("MYSELF"));
216 asprintf(&myself->connection->hostname, _("MYSELF"));
218 myself->connection->options = 0;
219 myself->connection->protocol_version = PROT_CURRENT;
221 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
222 logger(LOG_ERR, _("Name for tinc daemon required!"));
226 if(!check_id(name)) {
227 logger(LOG_ERR, _("Invalid name for myself!"));
233 myself->connection->name = xstrdup(name);
235 if(!read_rsa_private_key())
238 if(!read_connection_config(myself->connection)) {
239 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
243 if(!read_rsa_public_key(myself->connection))
246 if(!get_config_string
247 (lookup_config(myself->connection->config_tree, "Port"), &myport))
248 asprintf(&myport, "655");
250 /* Read in all the subnets specified in the host configuration file */
252 cfg = lookup_config(myself->connection->config_tree, "Subnet");
255 if(!get_config_subnet(cfg, &subnet))
258 subnet_add(myself, subnet);
260 cfg = lookup_config_next(myself->connection->config_tree, cfg);
263 /* Check some options */
265 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
267 myself->options |= OPTION_INDIRECT;
269 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
271 myself->options |= OPTION_TCPONLY;
273 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
275 myself->options |= OPTION_INDIRECT;
277 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
279 myself->options |= OPTION_TCPONLY;
281 if(myself->options & OPTION_TCPONLY)
282 myself->options |= OPTION_INDIRECT;
284 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
285 if(!strcasecmp(mode, "router"))
286 routing_mode = RMODE_ROUTER;
287 else if(!strcasecmp(mode, "switch"))
288 routing_mode = RMODE_SWITCH;
289 else if(!strcasecmp(mode, "hub"))
290 routing_mode = RMODE_HUB;
292 logger(LOG_ERR, _("Invalid routing mode!"));
297 routing_mode = RMODE_ROUTER;
299 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
301 #if !defined(SOL_IP) || !defined(IP_TOS)
302 if(priorityinheritance)
303 logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
306 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
309 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) {
310 if(maxtimeout <= 0) {
311 logger(LOG_ERR, _("Bogus maximum timeout!"));
317 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
318 if(!strcasecmp(afname, "IPv4"))
319 addressfamily = AF_INET;
320 else if(!strcasecmp(afname, "IPv6"))
321 addressfamily = AF_INET6;
322 else if(!strcasecmp(afname, "any"))
323 addressfamily = AF_UNSPEC;
325 logger(LOG_ERR, _("Invalid address family!"));
331 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
333 /* Generate packet encryption key */
336 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
337 if(!strcasecmp(cipher, "none")) {
338 myself->cipher = NULL;
340 myself->cipher = EVP_get_cipherbyname(cipher);
342 if(!myself->cipher) {
343 logger(LOG_ERR, _("Unrecognized cipher type!"));
348 myself->cipher = EVP_bf_cbc();
351 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
353 myself->keylength = 1;
355 myself->connection->outcipher = EVP_bf_ofb();
357 myself->key = (char *) xmalloc(myself->keylength);
358 RAND_pseudo_bytes(myself->key, myself->keylength);
360 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
363 keyexpires = now + keylifetime;
366 EVP_CIPHER_CTX_init(&packet_ctx);
367 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
370 /* Check if we want to use message authentication codes... */
373 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
374 if(!strcasecmp(digest, "none")) {
375 myself->digest = NULL;
377 myself->digest = EVP_get_digestbyname(digest);
379 if(!myself->digest) {
380 logger(LOG_ERR, _("Unrecognized digest type!"));
385 myself->digest = EVP_sha1();
387 myself->connection->outdigest = EVP_sha1();
389 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"),
390 &myself->maclength)) {
392 if(myself->maclength > myself->digest->md_size) {
393 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
395 } else if(myself->maclength < 0) {
396 logger(LOG_ERR, _("Bogus MAC length!"));
401 myself->maclength = 4;
403 myself->connection->outmaclength = 0;
407 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"),
408 &myself->compression)) {
409 if(myself->compression < 0 || myself->compression > 11) {
410 logger(LOG_ERR, _("Bogus compression level!"));
414 myself->compression = 0;
416 myself->connection->outcompression = 0;
420 myself->nexthop = myself;
421 myself->via = myself;
422 myself->status.active = true;
423 myself->status.reachable = true;
433 /* Run tinc-up script to further initialize the tap interface */
434 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
435 asprintf(&envp[1], "DEVICE=%s", device ? : "");
436 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
437 asprintf(&envp[3], "NAME=%s", myself->name);
440 execute_script("tinc-up", envp);
442 for(i = 0; i < 5; i++)
447 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
449 hint.ai_family = addressfamily;
450 hint.ai_socktype = SOCK_STREAM;
451 hint.ai_protocol = IPPROTO_TCP;
452 hint.ai_flags = AI_PASSIVE;
454 err = getaddrinfo(address, myport, &hint, &ai);
457 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
464 for(aip = ai; aip; aip = aip->ai_next) {
465 listen_socket[listen_sockets].tcp =
466 setup_listen_socket((sockaddr_t *) aip->ai_addr);
468 if(listen_socket[listen_sockets].tcp < 0)
471 listen_socket[listen_sockets].udp =
472 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
474 if(listen_socket[listen_sockets].udp < 0)
477 ifdebug(CONNECTIONS) {
478 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
479 logger(LOG_NOTICE, _("Listening on %s"), hostname);
483 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
490 logger(LOG_NOTICE, _("Ready"));
492 logger(LOG_ERR, _("Unable to create any listening socket!"));
500 setup all initial network connections
502 bool setup_network_connections(void)
515 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
516 if(pingtimeout < 1) {
525 try_outgoing_connections();
531 close all open network connections
533 void close_network_connections(void)
535 avl_node_t *node, *next;
542 for(node = connection_tree->head; node; node = next) {
544 c = (connection_t *) node->data;
547 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
548 terminate_connection(c, false);
551 if(myself && myself->connection)
552 terminate_connection(myself->connection, false);
554 for(i = 0; i < listen_sockets; i++) {
555 close(listen_socket[i].tcp);
556 close(listen_socket[i].udp);
566 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
567 asprintf(&envp[1], "DEVICE=%s", device ? : "");
568 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
569 asprintf(&envp[3], "NAME=%s", myself->name);
572 execute_script("tinc-down", envp);
574 for(i = 0; i < 4; i++)