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.19 2002/06/13 16:12:40 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)
87 c->rsa_key = RSA_new();
89 /* First, check for simple PublicKey statement */
91 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
93 BN_hex2bn(&c->rsa_key->n, key);
94 BN_hex2bn(&c->rsa_key->e, "FFFF");
99 /* Else, check for PublicKeyFile statement and read it */
101 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
103 if(is_safe_path(fname))
105 if((fp = fopen(fname, "r")) == NULL)
107 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
108 fname, strerror(errno));
113 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
116 return 0; /* Woohoo. */
118 /* If it fails, try PEM_read_RSA_PUBKEY. */
119 if((fp = fopen(fname, "r")) == NULL)
121 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
122 fname, strerror(errno));
127 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
132 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
133 fname, strerror(errno));
143 /* Else, check if a harnessed public key is in the config file */
145 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
146 if((fp = fopen(fname, "r")))
148 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
157 /* Try again with PEM_read_RSA_PUBKEY. */
159 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
160 if((fp = fopen(fname, "r")))
162 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
171 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
175 int read_rsa_private_key(void)
180 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
182 myself->connection->rsa_key = RSA_new();
183 BN_hex2bn(&myself->connection->rsa_key->d, key);
184 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
189 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
190 asprintf(&fname, "%s/rsa_key.priv", confbase);
192 if(is_safe_path(fname))
194 if((fp = fopen(fname, "r")) == NULL)
196 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
197 fname, strerror(errno));
202 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
204 if(!myself->connection->rsa_key)
206 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
207 fname, strerror(errno));
218 Configure node_t myself and set up the local sockets (listen only)
220 int setup_myself(void)
224 char *name, *hostname, *mode, *afname, *cipher, *digest;
225 char *address = NULL;
226 struct addrinfo hint, *ai, *aip;
230 myself->connection = new_connection();
231 init_configuration(&myself->connection->config_tree);
233 asprintf(&myself->hostname, _("MYSELF"));
234 asprintf(&myself->connection->hostname, _("MYSELF"));
236 myself->connection->options = 0;
237 myself->connection->protocol_version = PROT_CURRENT;
239 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
241 syslog(LOG_ERR, _("Name for tinc daemon required!"));
247 syslog(LOG_ERR, _("Invalid name for myself!"));
253 myself->connection->name = xstrdup(name);
256 if(read_rsa_private_key())
259 if(read_connection_config(myself->connection))
261 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
265 if(read_rsa_public_key(myself->connection))
269 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
270 asprintf(&myport, "655");
272 /* Read in all the subnets specified in the host configuration file */
274 cfg = lookup_config(myself->connection->config_tree, "Subnet");
278 if(!get_config_subnet(cfg, &subnet))
281 subnet_add(myself, subnet);
283 cfg = lookup_config_next(myself->connection->config_tree, cfg);
287 /* Check some options */
289 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
291 myself->options |= OPTION_INDIRECT;
293 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
295 myself->options |= OPTION_TCPONLY;
297 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
299 myself->options |= OPTION_INDIRECT;
301 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
303 myself->options |= OPTION_TCPONLY;
305 if(myself->options & OPTION_TCPONLY)
306 myself->options |= OPTION_INDIRECT;
308 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
310 if(!strcasecmp(mode, "router"))
311 routing_mode = RMODE_ROUTER;
312 else if (!strcasecmp(mode, "switch"))
313 routing_mode = RMODE_SWITCH;
314 else if (!strcasecmp(mode, "hub"))
315 routing_mode = RMODE_HUB;
318 syslog(LOG_ERR, _("Invalid routing mode!"));
324 routing_mode = RMODE_ROUTER;
326 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
327 #if !defined(SOL_IP) || !defined(IP_TOS)
328 if(priorityinheritance)
329 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
332 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
335 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
339 syslog(LOG_ERR, _("Bogus maximum timeout!"));
346 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
348 if(!strcasecmp(afname, "IPv4"))
349 addressfamily = AF_INET;
350 else if (!strcasecmp(afname, "IPv6"))
351 addressfamily = AF_INET6;
352 else if (!strcasecmp(afname, "any"))
353 addressfamily = AF_UNSPEC;
356 syslog(LOG_ERR, _("Invalid address family!"));
362 addressfamily = AF_INET;
364 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
366 /* Generate packet encryption key */
368 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
370 if(!strcasecmp(cipher, "none"))
372 myself->cipher = NULL;
376 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
378 syslog(LOG_ERR, _("Unrecognized cipher type!"));
384 myself->cipher = EVP_bf_cbc();
387 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
389 myself->keylength = 1;
391 myself->connection->outcipher = EVP_bf_ofb();
393 myself->key = (char *)xmalloc(myself->keylength);
394 RAND_pseudo_bytes(myself->key, myself->keylength);
396 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
399 keyexpires = now + keylifetime;
401 /* Check if we want to use message authentication codes... */
403 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
405 if(!strcasecmp(digest, "none"))
407 myself->digest = NULL;
411 if(!(myself->digest = EVP_get_digestbyname(digest)))
413 syslog(LOG_ERR, _("Unrecognized digest type!"));
419 myself->digest = EVP_sha1();
421 myself->connection->outdigest = EVP_sha1();
423 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
427 if(myself->maclength > myself->digest->md_size)
429 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
432 else if (myself->maclength < 0)
434 syslog(LOG_ERR, _("Bogus MAC length!"));
440 myself->maclength = 4;
442 myself->connection->outmaclength = 0;
446 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
448 if(myself->compression < 0 || myself->compression > 9)
450 syslog(LOG_ERR, _("Bogus compression level!"));
455 myself->compression = 0;
457 myself->connection->outcompression = 0;
461 myself->nexthop = myself;
462 myself->via = myself;
463 myself->status.active = 1;
464 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 if((err = getaddrinfo(address, myport, &hint, &ai)) || !ai)
483 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
489 for(aip = ai; aip; aip = aip->ai_next)
491 if((listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr)) < 0)
494 if((listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr)) < 0)
497 if(debug_lvl >= DEBUG_CONNECTIONS)
499 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
500 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
504 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
511 syslog(LOG_NOTICE, _("Ready"));
514 syslog(LOG_ERR, _("Unable to create any listening socket!"));
522 setup all initial network connections
524 int setup_network_connections(void)
536 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
546 if(setup_device() < 0)
549 /* Run tinc-up script to further initialize the tap interface */
550 execute_script("tinc-up");
552 if(setup_myself() < 0)
555 try_outgoing_connections();
561 close all open network connections
563 void close_network_connections(void)
565 avl_node_t *node, *next;
569 for(node = connection_tree->head; node; node = next)
572 c = (connection_t *)node->data;
574 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
575 terminate_connection(c, 0);
578 if(myself && myself->connection)
579 terminate_connection(myself->connection, 0);
581 for(i = 0; i < listen_sockets; i++)
583 close(listen_socket[i].tcp);
584 close(listen_socket[i].udp);
594 execute_script("tinc-down");