3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 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.26 2002/09/09 22:32:44 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IN_SYSTM_H
30 #include <netinet/in_systm.h>
32 #ifdef HAVE_NETINET_IP_H
33 #include <netinet/ip.h>
35 #ifdef HAVE_NETINET_TCP_H
36 #include <netinet/tcp.h>
43 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 /* SunOS really wants sys/socket.h BEFORE net/if.h,
48 and FreeBSD wants these lines below the rest. */
49 #include <arpa/inet.h>
50 #include <sys/socket.h>
53 #include <openssl/pem.h>
54 #include <openssl/rsa.h>
55 #include <openssl/rand.h>
63 #include "connection.h"
80 int read_rsa_public_key(connection_t *c)
89 c->rsa_key = RSA_new();
91 /* First, check for simple PublicKey statement */
93 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
94 BN_hex2bn(&c->rsa_key->n, key);
95 BN_hex2bn(&c->rsa_key->e, "FFFF");
100 /* Else, check for PublicKeyFile statement and read it */
103 (lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
104 if(is_safe_path(fname)) {
105 fp = fopen(fname, "r");
108 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
109 fname, strerror(errno));
115 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
119 return 0; /* Woohoo. */
121 /* If it fails, try PEM_read_RSA_PUBKEY. */
122 fp = fopen(fname, "r");
125 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
126 fname, strerror(errno));
132 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
138 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
139 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 fp = fopen(fname, "r");
153 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
162 /* Try again with PEM_read_RSA_PUBKEY. */
164 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
165 fp = fopen(fname, "r");
168 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
177 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
182 int read_rsa_private_key(void)
189 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
190 myself->connection->rsa_key = RSA_new();
191 BN_hex2bn(&myself->connection->rsa_key->d, key);
192 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
197 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
198 asprintf(&fname, "%s/rsa_key.priv", confbase);
200 if(is_safe_path(fname)) {
201 fp = fopen(fname, "r");
204 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
205 fname, strerror(errno));
211 myself->connection->rsa_key =
212 PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
215 if(!myself->connection->rsa_key) {
216 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
217 fname, strerror(errno));
229 Configure node_t myself and set up the local sockets (listen only)
231 int setup_myself(void)
235 char *name, *hostname, *mode, *afname, *cipher, *digest;
236 char *address = NULL;
237 struct addrinfo hint, *ai, *aip;
243 myself->connection = new_connection();
244 init_configuration(&myself->connection->config_tree);
246 asprintf(&myself->hostname, _("MYSELF"));
247 asprintf(&myself->connection->hostname, _("MYSELF"));
249 myself->connection->options = 0;
250 myself->connection->protocol_version = PROT_CURRENT;
252 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
253 syslog(LOG_ERR, _("Name for tinc daemon required!"));
258 syslog(LOG_ERR, _("Invalid name for myself!"));
264 myself->connection->name = xstrdup(name);
266 if(read_rsa_private_key())
269 if(read_connection_config(myself->connection)) {
270 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
274 if(read_rsa_public_key(myself->connection))
277 if(!get_config_string
278 (lookup_config(myself->connection->config_tree, "Port"), &myport))
279 asprintf(&myport, "655");
281 /* Read in all the subnets specified in the host configuration file */
283 cfg = lookup_config(myself->connection->config_tree, "Subnet");
286 if(!get_config_subnet(cfg, &subnet))
289 subnet_add(myself, subnet);
291 cfg = lookup_config_next(myself->connection->config_tree, cfg);
294 /* Check some options */
296 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
298 myself->options |= OPTION_INDIRECT;
300 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
302 myself->options |= OPTION_TCPONLY;
305 (lookup_config(myself->connection->config_tree, "IndirectData"),
308 myself->options |= OPTION_INDIRECT;
311 (lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
313 myself->options |= OPTION_TCPONLY;
315 if(myself->options & OPTION_TCPONLY)
316 myself->options |= OPTION_INDIRECT;
318 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
319 if(!strcasecmp(mode, "router"))
320 routing_mode = RMODE_ROUTER;
321 else if(!strcasecmp(mode, "switch"))
322 routing_mode = RMODE_SWITCH;
323 else if(!strcasecmp(mode, "hub"))
324 routing_mode = RMODE_HUB;
326 syslog(LOG_ERR, _("Invalid routing mode!"));
331 routing_mode = RMODE_ROUTER;
333 get_config_bool(lookup_config(config_tree, "PriorityInheritance"),
334 &priorityinheritance);
335 #if !defined(SOL_IP) || !defined(IP_TOS)
336 if(priorityinheritance)
337 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
340 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
344 (lookup_config(myself->connection->config_tree, "MaxTimeout"),
346 if(maxtimeout <= 0) {
347 syslog(LOG_ERR, _("Bogus maximum timeout!"));
353 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
354 if(!strcasecmp(afname, "IPv4"))
355 addressfamily = AF_INET;
356 else if(!strcasecmp(afname, "IPv6"))
357 addressfamily = AF_INET6;
358 else if(!strcasecmp(afname, "any"))
359 addressfamily = AF_UNSPEC;
361 syslog(LOG_ERR, _("Invalid address family!"));
366 addressfamily = AF_INET;
368 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
370 /* Generate packet encryption key */
373 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
374 if(!strcasecmp(cipher, "none")) {
375 myself->cipher = NULL;
377 myself->cipher = EVP_get_cipherbyname(cipher);
379 if(!myself->cipher) {
380 syslog(LOG_ERR, _("Unrecognized cipher type!"));
385 myself->cipher = EVP_bf_cbc();
388 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
390 myself->keylength = 1;
392 myself->connection->outcipher = EVP_bf_ofb();
394 myself->key = (char *) xmalloc(myself->keylength);
395 RAND_pseudo_bytes(myself->key, myself->keylength);
397 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
400 keyexpires = now + keylifetime;
402 /* Check if we want to use message authentication codes... */
405 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
406 if(!strcasecmp(digest, "none")) {
407 myself->digest = NULL;
409 myself->digest = EVP_get_digestbyname(digest);
411 if(!myself->digest) {
412 syslog(LOG_ERR, _("Unrecognized digest type!"));
417 myself->digest = EVP_sha1();
419 myself->connection->outdigest = EVP_sha1();
422 (lookup_config(myself->connection->config_tree, "MACLength"),
423 &myself->maclength)) {
425 if(myself->maclength > myself->digest->md_size) {
426 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
428 } else if(myself->maclength < 0) {
429 syslog(LOG_ERR, _("Bogus MAC length!"));
434 myself->maclength = 4;
436 myself->connection->outmaclength = 0;
441 (lookup_config(myself->connection->config_tree, "Compression"),
442 &myself->compression)) {
443 if(myself->compression < 0 || myself->compression > 9) {
444 syslog(LOG_ERR, _("Bogus compression level!"));
448 myself->compression = 0;
450 myself->connection->outcompression = 0;
454 myself->nexthop = myself;
455 myself->via = myself;
456 myself->status.active = 1;
457 myself->status.reachable = 1;
464 memset(&hint, 0, sizeof(hint));
466 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
468 hint.ai_family = addressfamily;
469 hint.ai_socktype = SOCK_STREAM;
470 hint.ai_protocol = IPPROTO_TCP;
471 hint.ai_flags = AI_PASSIVE;
473 err = getaddrinfo(address, myport, &hint, &ai);
476 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
483 for(aip = ai; aip; aip = aip->ai_next) {
484 listen_socket[listen_sockets].tcp =
485 setup_listen_socket((sockaddr_t *) aip->ai_addr);
487 if(listen_socket[listen_sockets].tcp < 0)
490 listen_socket[listen_sockets].udp =
491 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
493 if(listen_socket[listen_sockets].udp < 0)
496 if(debug_lvl >= DEBUG_CONNECTIONS) {
497 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
498 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
502 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
509 syslog(LOG_NOTICE, _("Ready"));
511 syslog(LOG_ERR, _("Unable to create any listening socket!"));
519 setup all initial network connections
521 int setup_network_connections(void)
537 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
538 if(pingtimeout < 1) {
544 if(setup_device() < 0)
547 /* Run tinc-up script to further initialize the tap interface */
548 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
549 asprintf(&envp[1], "DEVICE=%s", device ? : "");
550 asprintf(&envp[2], "INTERFACE=%s", interface ? : "");
553 execute_script("tinc-up", envp);
555 for(i = 0; i < 4; i++)
558 if(setup_myself() < 0)
561 try_outgoing_connections();
567 close all open network connections
569 void close_network_connections(void)
571 avl_node_t *node, *next;
578 for(node = connection_tree->head; node; node = next) {
580 c = (connection_t *) node->data;
583 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
584 terminate_connection(c, 0);
587 if(myself && myself->connection)
588 terminate_connection(myself->connection, 0);
590 for(i = 0; i < listen_sockets; i++) {
591 close(listen_socket[i].tcp);
592 close(listen_socket[i].udp);
602 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
603 asprintf(&envp[1], "DEVICE=%s", device ? : "");
604 asprintf(&envp[2], "INTERFACE=%s", interface ? : "");
607 execute_script("tinc-down", envp);
609 for(i = 0; i < 4; i++)