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.15 2002/04/23 07:49:38 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);
112 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
113 fname, strerror(errno));
125 /* Else, check if a harnessed public key is in the config file */
127 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
128 if((fp = fopen(fname, "r")))
130 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
140 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
145 int read_rsa_private_key(void)
150 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
152 myself->connection->rsa_key = RSA_new();
153 BN_hex2bn(&myself->connection->rsa_key->d, key);
154 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
159 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
160 asprintf(&fname, "%s/rsa_key.priv", confbase);
162 if(is_safe_path(fname))
164 if((fp = fopen(fname, "r")) == NULL)
166 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
167 fname, strerror(errno));
172 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
174 if(!myself->connection->rsa_key)
176 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
177 fname, strerror(errno));
188 Configure node_t myself and set up the local sockets (listen only)
190 int setup_myself(void)
194 char *name, *hostname, *mode, *afname, *cipher, *digest;
195 char *address = NULL;
196 struct addrinfo hint, *ai, *aip;
200 myself->connection = new_connection();
201 init_configuration(&myself->connection->config_tree);
203 asprintf(&myself->hostname, _("MYSELF"));
204 asprintf(&myself->connection->hostname, _("MYSELF"));
206 myself->connection->options = 0;
207 myself->connection->protocol_version = PROT_CURRENT;
209 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
211 syslog(LOG_ERR, _("Name for tinc daemon required!"));
217 syslog(LOG_ERR, _("Invalid name for myself!"));
223 myself->connection->name = xstrdup(name);
226 if(read_rsa_private_key())
229 if(read_connection_config(myself->connection))
231 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
235 if(read_rsa_public_key(myself->connection))
239 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
240 asprintf(&myport, "655");
242 /* Read in all the subnets specified in the host configuration file */
244 cfg = lookup_config(myself->connection->config_tree, "Subnet");
248 if(!get_config_subnet(cfg, &subnet))
251 subnet_add(myself, subnet);
253 cfg = lookup_config_next(myself->connection->config_tree, cfg);
257 /* Check some options */
259 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
261 myself->options |= OPTION_INDIRECT;
263 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
265 myself->options |= OPTION_TCPONLY;
267 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
269 myself->options |= OPTION_INDIRECT;
271 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
273 myself->options |= OPTION_TCPONLY;
275 if(myself->options & OPTION_TCPONLY)
276 myself->options |= OPTION_INDIRECT;
278 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
280 if(!strcasecmp(mode, "router"))
281 routing_mode = RMODE_ROUTER;
282 else if (!strcasecmp(mode, "switch"))
283 routing_mode = RMODE_SWITCH;
284 else if (!strcasecmp(mode, "hub"))
285 routing_mode = RMODE_HUB;
288 syslog(LOG_ERR, _("Invalid routing mode!"));
294 routing_mode = RMODE_ROUTER;
296 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
297 #if !defined(SOL_IP) || !defined(IP_TOS)
298 if(priorityinheritance)
299 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
302 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
305 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
309 syslog(LOG_ERR, _("Bogus maximum timeout!"));
316 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;
326 syslog(LOG_ERR, _("Invalid address family!"));
332 addressfamily = AF_INET;
334 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
336 /* Generate packet encryption key */
338 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
340 if(!strcasecmp(cipher, "none"))
342 myself->cipher = NULL;
346 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
348 syslog(LOG_ERR, _("Unrecognized cipher type!"));
354 myself->cipher = EVP_bf_cbc();
357 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
359 myself->keylength = 1;
361 myself->connection->outcipher = EVP_bf_ofb();
363 myself->key = (char *)xmalloc(myself->keylength);
364 RAND_pseudo_bytes(myself->key, myself->keylength);
366 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
369 keyexpires = now + keylifetime;
371 /* Check if we want to use message authentication codes... */
373 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
375 if(!strcasecmp(digest, "none"))
377 myself->digest = NULL;
381 if(!(myself->digest = EVP_get_digestbyname(digest)))
383 syslog(LOG_ERR, _("Unrecognized digest type!"));
389 myself->digest = EVP_sha1();
391 myself->connection->outdigest = EVP_sha1();
393 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
397 if(myself->maclength > myself->digest->md_size)
399 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
402 else if (myself->maclength < 0)
404 syslog(LOG_ERR, _("Bogus MAC length!"));
410 myself->maclength = 4;
412 myself->connection->outmaclength = 0;
416 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
418 if(myself->compression < 0 || myself->compression > 9)
420 syslog(LOG_ERR, _("Bogus compression level!"));
425 myself->compression = 0;
427 myself->connection->outcompression = 0;
431 myself->nexthop = myself;
432 myself->via = myself;
433 myself->status.active = 1;
434 myself->status.reachable = 1;
442 memset(&hint, 0, sizeof(hint));
444 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
446 hint.ai_family = addressfamily;
447 hint.ai_socktype = SOCK_STREAM;
448 hint.ai_protocol = IPPROTO_TCP;
449 hint.ai_flags = AI_PASSIVE;
451 if((err = getaddrinfo(address, myport, &hint, &ai)) || !ai)
453 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
457 for(aip = ai; aip; aip = aip->ai_next)
459 if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
462 if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
465 if(debug_lvl >= DEBUG_CONNECTIONS)
467 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
468 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
472 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
479 syslog(LOG_NOTICE, _("Ready"));
482 syslog(LOG_ERR, _("Unable to create any listening socket!"));
490 setup all initial network connections
492 int setup_network_connections(void)
504 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
514 if(setup_device() < 0)
517 /* Run tinc-up script to further initialize the tap interface */
518 execute_script("tinc-up");
520 if(setup_myself() < 0)
523 try_outgoing_connections();
529 close all open network connections
531 void close_network_connections(void)
533 avl_node_t *node, *next;
537 for(node = connection_tree->head; node; node = next)
540 c = (connection_t *)node->data;
542 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
543 terminate_connection(c, 0);
546 if(myself && myself->connection)
547 terminate_connection(myself->connection, 0);
549 for(i = 0; i < listen_sockets; i++)
551 close(listen_socket[i].tcp);
552 close(listen_socket[i].udp);
562 execute_script("tinc-down");