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.22 2002/09/03 20:43:25 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"
79 int read_rsa_public_key(connection_t *c)
86 c->rsa_key = RSA_new();
88 /* First, check for simple PublicKey statement */
90 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
92 BN_hex2bn(&c->rsa_key->n, key);
93 BN_hex2bn(&c->rsa_key->e, "FFFF");
98 /* Else, check for PublicKeyFile statement and read it */
100 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
102 if(is_safe_path(fname))
104 if((fp = fopen(fname, "r")) == NULL)
106 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
107 fname, strerror(errno));
112 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
115 return 0; /* Woohoo. */
117 /* If it fails, try PEM_read_RSA_PUBKEY. */
118 if((fp = fopen(fname, "r")) == NULL)
120 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
121 fname, strerror(errno));
126 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
131 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
132 fname, strerror(errno));
142 /* Else, check if a harnessed public key is in the config file */
144 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
145 if((fp = fopen(fname, "r")))
147 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
156 /* Try again with PEM_read_RSA_PUBKEY. */
158 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
159 if((fp = fopen(fname, "r")))
161 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
170 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
174 int read_rsa_private_key(void)
179 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
181 myself->connection->rsa_key = RSA_new();
182 BN_hex2bn(&myself->connection->rsa_key->d, key);
183 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
188 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
189 asprintf(&fname, "%s/rsa_key.priv", confbase);
191 if(is_safe_path(fname))
193 if((fp = fopen(fname, "r")) == NULL)
195 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
196 fname, strerror(errno));
201 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
203 if(!myself->connection->rsa_key)
205 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
206 fname, strerror(errno));
217 Configure node_t myself and set up the local sockets (listen only)
219 int setup_myself(void)
223 char *name, *hostname, *mode, *afname, *cipher, *digest;
224 char *address = NULL;
225 struct addrinfo hint, *ai, *aip;
229 myself->connection = new_connection();
230 init_configuration(&myself->connection->config_tree);
232 asprintf(&myself->hostname, _("MYSELF"));
233 asprintf(&myself->connection->hostname, _("MYSELF"));
235 myself->connection->options = 0;
236 myself->connection->protocol_version = PROT_CURRENT;
238 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
240 syslog(LOG_ERR, _("Name for tinc daemon required!"));
246 syslog(LOG_ERR, _("Invalid name for myself!"));
252 myself->connection->name = xstrdup(name);
255 if(read_rsa_private_key())
258 if(read_connection_config(myself->connection))
260 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
264 if(read_rsa_public_key(myself->connection))
268 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
269 asprintf(&myport, "655");
271 /* Read in all the subnets specified in the host configuration file */
273 cfg = lookup_config(myself->connection->config_tree, "Subnet");
277 if(!get_config_subnet(cfg, &subnet))
280 subnet_add(myself, subnet);
282 cfg = lookup_config_next(myself->connection->config_tree, cfg);
286 /* Check some options */
288 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
290 myself->options |= OPTION_INDIRECT;
292 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
294 myself->options |= OPTION_TCPONLY;
296 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
298 myself->options |= OPTION_INDIRECT;
300 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
302 myself->options |= OPTION_TCPONLY;
304 if(myself->options & OPTION_TCPONLY)
305 myself->options |= OPTION_INDIRECT;
307 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
309 if(!strcasecmp(mode, "router"))
310 routing_mode = RMODE_ROUTER;
311 else if (!strcasecmp(mode, "switch"))
312 routing_mode = RMODE_SWITCH;
313 else if (!strcasecmp(mode, "hub"))
314 routing_mode = RMODE_HUB;
317 syslog(LOG_ERR, _("Invalid routing mode!"));
323 routing_mode = RMODE_ROUTER;
325 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
326 #if !defined(SOL_IP) || !defined(IP_TOS)
327 if(priorityinheritance)
328 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
331 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
334 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
338 syslog(LOG_ERR, _("Bogus maximum timeout!"));
345 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
347 if(!strcasecmp(afname, "IPv4"))
348 addressfamily = AF_INET;
349 else if (!strcasecmp(afname, "IPv6"))
350 addressfamily = AF_INET6;
351 else if (!strcasecmp(afname, "any"))
352 addressfamily = AF_UNSPEC;
355 syslog(LOG_ERR, _("Invalid address family!"));
361 addressfamily = AF_INET;
363 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
365 /* Generate packet encryption key */
367 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
369 if(!strcasecmp(cipher, "none"))
371 myself->cipher = NULL;
375 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
377 syslog(LOG_ERR, _("Unrecognized cipher type!"));
383 myself->cipher = EVP_bf_cbc();
386 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
388 myself->keylength = 1;
390 myself->connection->outcipher = EVP_bf_ofb();
392 myself->key = (char *)xmalloc(myself->keylength);
393 RAND_pseudo_bytes(myself->key, myself->keylength);
395 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
398 keyexpires = now + keylifetime;
400 /* Check if we want to use message authentication codes... */
402 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
404 if(!strcasecmp(digest, "none"))
406 myself->digest = NULL;
410 if(!(myself->digest = EVP_get_digestbyname(digest)))
412 syslog(LOG_ERR, _("Unrecognized digest type!"));
418 myself->digest = EVP_sha1();
420 myself->connection->outdigest = EVP_sha1();
422 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
426 if(myself->maclength > myself->digest->md_size)
428 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
431 else if (myself->maclength < 0)
433 syslog(LOG_ERR, _("Bogus MAC length!"));
439 myself->maclength = 4;
441 myself->connection->outmaclength = 0;
445 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
447 if(myself->compression < 0 || myself->compression > 9)
449 syslog(LOG_ERR, _("Bogus compression level!"));
454 myself->compression = 0;
456 myself->connection->outcompression = 0;
460 myself->nexthop = myself;
461 myself->via = myself;
462 myself->status.active = 1;
463 myself->status.reachable = 1;
469 memset(&hint, 0, sizeof(hint));
471 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
473 hint.ai_family = addressfamily;
474 hint.ai_socktype = SOCK_STREAM;
475 hint.ai_protocol = IPPROTO_TCP;
476 hint.ai_flags = AI_PASSIVE;
478 if((err = getaddrinfo(address, myport, &hint, &ai)) || !ai)
480 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
486 for(aip = ai; aip; aip = aip->ai_next)
488 if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
491 if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
494 if(debug_lvl >= DEBUG_CONNECTIONS)
496 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
497 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
501 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
508 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)
534 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
544 if(setup_device() < 0)
547 /* Run tinc-up script to further initialize the tap interface */
548 asprintf(&envp[0], "NETNAME=%s", netname?netname:"");
549 asprintf(&envp[1], "DEVICE=%s", device?device:"");
550 asprintf(&envp[2], "INTERFACE=%s", interface?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;
576 for(node = connection_tree->head; node; node = next)
579 c = (connection_t *)node->data;
581 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
582 terminate_connection(c, 0);
585 if(myself && myself->connection)
586 terminate_connection(myself->connection, 0);
588 for(i = 0; i < listen_sockets; i++)
590 close(listen_socket[i].tcp);
591 close(listen_socket[i].udp);
600 asprintf(&envp[0], "NETNAME=%s", netname?netname:"");
601 asprintf(&envp[1], "DEVICE=%s", device?device:"");
602 asprintf(&envp[2], "INTERFACE=%s", interface?interface:"");
605 execute_script("tinc-down", envp);
607 for(i = 0; i < 4; i++)