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.30 2003/03/28 13:41:49 guus Exp $
28 #include <netinet/in.h>
34 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 /* SunOS really wants sys/socket.h BEFORE net/if.h,
39 and FreeBSD wants these lines below the rest. */
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
46 #ifdef HAVE_NETINET_IP_H
47 #include <netinet/ip.h>
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.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();
90 // RSA_blinding_on(c->rsa_key, NULL);
93 /* First, check for simple PublicKey statement */
95 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
96 BN_hex2bn(&c->rsa_key->n, key);
97 BN_hex2bn(&c->rsa_key->e, "FFFF");
102 /* Else, check for PublicKeyFile statement and read it */
105 (lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
106 if(is_safe_path(fname)) {
107 fp = fopen(fname, "r");
110 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
111 fname, strerror(errno));
117 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
121 return 0; /* Woohoo. */
123 /* If it fails, try PEM_read_RSA_PUBKEY. */
124 fp = fopen(fname, "r");
127 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
128 fname, strerror(errno));
134 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
138 // RSA_blinding_on(c->rsa_key, NULL);
142 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
143 fname, strerror(errno));
151 /* Else, check if a harnessed public key is in the config file */
153 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
154 fp = fopen(fname, "r");
157 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
166 /* Try again with PEM_read_RSA_PUBKEY. */
168 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
169 fp = fopen(fname, "r");
172 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
173 // RSA_blinding_on(c->rsa_key, NULL);
182 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
187 int read_rsa_private_key(void)
194 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
195 myself->connection->rsa_key = RSA_new();
196 // RSA_blinding_on(myself->connection->rsa_key, NULL);
197 BN_hex2bn(&myself->connection->rsa_key->d, key);
198 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
203 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
204 asprintf(&fname, "%s/rsa_key.priv", confbase);
206 if(is_safe_path(fname)) {
207 fp = fopen(fname, "r");
210 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
211 fname, strerror(errno));
217 myself->connection->rsa_key =
218 PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
221 if(!myself->connection->rsa_key) {
222 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
223 fname, strerror(errno));
235 Configure node_t myself and set up the local sockets (listen only)
237 int setup_myself(void)
241 char *name, *hostname, *mode, *afname, *cipher, *digest;
242 char *address = NULL;
243 struct addrinfo hint, *ai, *aip;
249 myself->connection = new_connection();
250 init_configuration(&myself->connection->config_tree);
252 asprintf(&myself->hostname, _("MYSELF"));
253 asprintf(&myself->connection->hostname, _("MYSELF"));
255 myself->connection->options = 0;
256 myself->connection->protocol_version = PROT_CURRENT;
258 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
259 syslog(LOG_ERR, _("Name for tinc daemon required!"));
264 syslog(LOG_ERR, _("Invalid name for myself!"));
270 myself->connection->name = xstrdup(name);
272 if(read_rsa_private_key())
275 if(read_connection_config(myself->connection)) {
276 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
280 if(read_rsa_public_key(myself->connection))
283 if(!get_config_string
284 (lookup_config(myself->connection->config_tree, "Port"), &myport))
285 asprintf(&myport, "655");
287 /* Read in all the subnets specified in the host configuration file */
289 cfg = lookup_config(myself->connection->config_tree, "Subnet");
292 if(!get_config_subnet(cfg, &subnet))
295 subnet_add(myself, subnet);
297 cfg = lookup_config_next(myself->connection->config_tree, cfg);
300 /* Check some options */
302 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
304 myself->options |= OPTION_INDIRECT;
306 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
308 myself->options |= OPTION_TCPONLY;
311 (lookup_config(myself->connection->config_tree, "IndirectData"),
314 myself->options |= OPTION_INDIRECT;
317 (lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
319 myself->options |= OPTION_TCPONLY;
321 if(myself->options & OPTION_TCPONLY)
322 myself->options |= OPTION_INDIRECT;
324 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
325 if(!strcasecmp(mode, "router"))
326 routing_mode = RMODE_ROUTER;
327 else if(!strcasecmp(mode, "switch"))
328 routing_mode = RMODE_SWITCH;
329 else if(!strcasecmp(mode, "hub"))
330 routing_mode = RMODE_HUB;
332 syslog(LOG_ERR, _("Invalid routing mode!"));
337 routing_mode = RMODE_ROUTER;
339 get_config_bool(lookup_config(config_tree, "PriorityInheritance"),
340 &priorityinheritance);
341 #if !defined(SOL_IP) || !defined(IP_TOS)
342 if(priorityinheritance)
343 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
346 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
350 (lookup_config(myself->connection->config_tree, "MaxTimeout"),
352 if(maxtimeout <= 0) {
353 syslog(LOG_ERR, _("Bogus maximum timeout!"));
359 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
360 if(!strcasecmp(afname, "IPv4"))
361 addressfamily = AF_INET;
362 else if(!strcasecmp(afname, "IPv6"))
363 addressfamily = AF_INET6;
364 else if(!strcasecmp(afname, "any"))
365 addressfamily = AF_UNSPEC;
367 syslog(LOG_ERR, _("Invalid address family!"));
372 addressfamily = AF_INET;
374 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
376 /* Generate packet encryption key */
379 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
380 if(!strcasecmp(cipher, "none")) {
381 myself->cipher = NULL;
383 myself->cipher = EVP_get_cipherbyname(cipher);
385 if(!myself->cipher) {
386 syslog(LOG_ERR, _("Unrecognized cipher type!"));
391 myself->cipher = EVP_bf_cbc();
394 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
396 myself->keylength = 1;
398 myself->connection->outcipher = EVP_bf_ofb();
400 myself->key = (char *) xmalloc(myself->keylength);
401 RAND_pseudo_bytes(myself->key, myself->keylength);
403 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
406 keyexpires = now + keylifetime;
408 EVP_CIPHER_CTX_init(&packet_ctx);
410 /* Check if we want to use message authentication codes... */
413 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
414 if(!strcasecmp(digest, "none")) {
415 myself->digest = NULL;
417 myself->digest = EVP_get_digestbyname(digest);
419 if(!myself->digest) {
420 syslog(LOG_ERR, _("Unrecognized digest type!"));
425 myself->digest = EVP_sha1();
427 myself->connection->outdigest = EVP_sha1();
430 (lookup_config(myself->connection->config_tree, "MACLength"),
431 &myself->maclength)) {
433 if(myself->maclength > myself->digest->md_size) {
434 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
436 } else if(myself->maclength < 0) {
437 syslog(LOG_ERR, _("Bogus MAC length!"));
442 myself->maclength = 4;
444 myself->connection->outmaclength = 0;
449 (lookup_config(myself->connection->config_tree, "Compression"),
450 &myself->compression)) {
451 if(myself->compression < 0 || myself->compression > 9) {
452 syslog(LOG_ERR, _("Bogus compression level!"));
456 myself->compression = 0;
458 myself->connection->outcompression = 0;
462 myself->nexthop = myself;
463 myself->via = myself;
464 myself->status.active = 1;
465 myself->status.reachable = 1;
472 memset(&hint, 0, sizeof(hint));
474 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
476 hint.ai_family = addressfamily;
477 hint.ai_socktype = SOCK_STREAM;
478 hint.ai_protocol = IPPROTO_TCP;
479 hint.ai_flags = AI_PASSIVE;
481 err = getaddrinfo(address, myport, &hint, &ai);
484 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
491 for(aip = ai; aip; aip = aip->ai_next) {
492 listen_socket[listen_sockets].tcp =
493 setup_listen_socket((sockaddr_t *) aip->ai_addr);
495 if(listen_socket[listen_sockets].tcp < 0)
498 listen_socket[listen_sockets].udp =
499 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
501 if(listen_socket[listen_sockets].udp < 0)
504 if(debug_lvl >= DEBUG_CONNECTIONS) {
505 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
506 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
510 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
517 syslog(LOG_NOTICE, _("Ready"));
519 syslog(LOG_ERR, _("Unable to create any listening socket!"));
527 setup all initial network connections
529 int setup_network_connections(void)
545 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
546 if(pingtimeout < 1) {
552 if(setup_device() < 0)
555 if(setup_myself() < 0)
558 /* Run tinc-up script to further initialize the tap interface */
559 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
560 asprintf(&envp[1], "DEVICE=%s", device ? : "");
561 asprintf(&envp[2], "INTERFACE=%s", interface ? : "");
562 asprintf(&envp[3], "NAME=%s", myself->name);
565 execute_script("tinc-up", envp);
567 for(i = 0; i < 5; i++)
570 try_outgoing_connections();
576 close all open network connections
578 void close_network_connections(void)
580 avl_node_t *node, *next;
587 for(node = connection_tree->head; node; node = next) {
589 c = (connection_t *) node->data;
592 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
593 terminate_connection(c, 0);
596 if(myself && myself->connection)
597 terminate_connection(myself->connection, 0);
599 for(i = 0; i < listen_sockets; i++) {
600 close(listen_socket[i].tcp);
601 close(listen_socket[i].udp);
611 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
612 asprintf(&envp[1], "DEVICE=%s", device ? : "");
613 asprintf(&envp[2], "INTERFACE=%s", interface ? : "");
614 asprintf(&envp[3], "NAME=%s", myself->name);
617 execute_script("tinc-down", envp);
619 for(i = 0; i < 4; i++)