3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 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.44 2003/08/28 21:05:10 guus Exp $
25 #include <openssl/pem.h>
26 #include <openssl/rsa.h>
27 #include <openssl/rand.h>
31 #include "connection.h"
47 bool read_rsa_public_key(connection_t *c)
56 c->rsa_key = RSA_new();
57 // RSA_blinding_on(c->rsa_key, NULL);
60 /* First, check for simple PublicKey statement */
62 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
63 BN_hex2bn(&c->rsa_key->n, key);
64 BN_hex2bn(&c->rsa_key->e, "FFFF");
69 /* Else, check for PublicKeyFile statement and read it */
71 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
72 fp = fopen(fname, "r");
75 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
76 fname, strerror(errno));
82 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
86 return true; /* Woohoo. */
88 /* If it fails, try PEM_read_RSA_PUBKEY. */
89 fp = fopen(fname, "r");
92 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
93 fname, strerror(errno));
99 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
103 // RSA_blinding_on(c->rsa_key, NULL);
107 logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
108 fname, strerror(errno));
112 /* Else, check if a harnessed public key is in the config file */
114 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
115 fp = fopen(fname, "r");
118 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
127 /* Try again with PEM_read_RSA_PUBKEY. */
129 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
134 // RSA_blinding_on(c->rsa_key, NULL);
143 logger(LOG_ERR, _("No public key for %s specified!"), c->name);
148 bool read_rsa_private_key(void)
156 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
157 myself->connection->rsa_key = RSA_new();
158 // RSA_blinding_on(myself->connection->rsa_key, NULL);
159 BN_hex2bn(&myself->connection->rsa_key->d, key);
160 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
165 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
166 asprintf(&fname, "%s/rsa_key.priv", confbase);
168 fp = fopen(fname, "r");
171 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
172 fname, strerror(errno));
177 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
178 if(fstat(fileno(fp), &s)) {
179 logger(LOG_ERR, _("Could not stat RSA private key file `%s': %s'"),
180 fname, strerror(errno));
185 if(s.st_mode & ~0100700)
186 logger(LOG_WARNING, _("Warning: insecure file permissions for RSA private key file `%s'!"), fname);
189 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
192 if(!myself->connection->rsa_key) {
193 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
194 fname, strerror(errno));
204 Configure node_t myself and set up the local sockets (listen only)
206 bool setup_myself(void)
210 char *name, *hostname, *mode, *afname, *cipher, *digest;
211 char *address = NULL;
213 struct addrinfo *ai, *aip, hint = {0};
220 myself->connection = new_connection();
221 init_configuration(&myself->connection->config_tree);
223 asprintf(&myself->hostname, _("MYSELF"));
224 asprintf(&myself->connection->hostname, _("MYSELF"));
226 myself->connection->options = 0;
227 myself->connection->protocol_version = PROT_CURRENT;
229 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
230 logger(LOG_ERR, _("Name for tinc daemon required!"));
234 if(!check_id(name)) {
235 logger(LOG_ERR, _("Invalid name for myself!"));
241 myself->connection->name = xstrdup(name);
243 if(!read_rsa_private_key())
246 if(!read_connection_config(myself->connection)) {
247 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
251 if(!read_rsa_public_key(myself->connection))
254 if(!get_config_string
255 (lookup_config(myself->connection->config_tree, "Port"), &myport))
256 asprintf(&myport, "655");
258 /* Read in all the subnets specified in the host configuration file */
260 cfg = lookup_config(myself->connection->config_tree, "Subnet");
263 if(!get_config_subnet(cfg, &subnet))
266 subnet_add(myself, subnet);
268 cfg = lookup_config_next(myself->connection->config_tree, cfg);
271 /* Check some options */
273 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
275 myself->options |= OPTION_INDIRECT;
277 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
279 myself->options |= OPTION_TCPONLY;
281 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
283 myself->options |= OPTION_INDIRECT;
285 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
287 myself->options |= OPTION_TCPONLY;
289 if(myself->options & OPTION_TCPONLY)
290 myself->options |= OPTION_INDIRECT;
292 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
293 if(!strcasecmp(mode, "router"))
294 routing_mode = RMODE_ROUTER;
295 else if(!strcasecmp(mode, "switch"))
296 routing_mode = RMODE_SWITCH;
297 else if(!strcasecmp(mode, "hub"))
298 routing_mode = RMODE_HUB;
300 logger(LOG_ERR, _("Invalid routing mode!"));
305 routing_mode = RMODE_ROUTER;
307 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
309 #if !defined(SOL_IP) || !defined(IP_TOS)
310 if(priorityinheritance)
311 logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
314 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
317 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) {
318 if(maxtimeout <= 0) {
319 logger(LOG_ERR, _("Bogus maximum timeout!"));
325 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
326 if(!strcasecmp(afname, "IPv4"))
327 addressfamily = AF_INET;
328 else if(!strcasecmp(afname, "IPv6"))
329 addressfamily = AF_INET6;
330 else if(!strcasecmp(afname, "any"))
331 addressfamily = AF_UNSPEC;
333 logger(LOG_ERR, _("Invalid address family!"));
339 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
341 /* Generate packet encryption key */
344 (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
345 if(!strcasecmp(cipher, "none")) {
346 myself->cipher = NULL;
348 myself->cipher = EVP_get_cipherbyname(cipher);
350 if(!myself->cipher) {
351 logger(LOG_ERR, _("Unrecognized cipher type!"));
356 myself->cipher = EVP_bf_cbc();
359 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
361 myself->keylength = 1;
363 myself->connection->outcipher = EVP_bf_ofb();
365 myself->key = xmalloc(myself->keylength);
366 RAND_pseudo_bytes(myself->key, myself->keylength);
368 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
371 keyexpires = now + keylifetime;
374 EVP_CIPHER_CTX_init(&packet_ctx);
375 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
378 /* Check if we want to use message authentication codes... */
381 (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
382 if(!strcasecmp(digest, "none")) {
383 myself->digest = NULL;
385 myself->digest = EVP_get_digestbyname(digest);
387 if(!myself->digest) {
388 logger(LOG_ERR, _("Unrecognized digest type!"));
393 myself->digest = EVP_sha1();
395 myself->connection->outdigest = EVP_sha1();
397 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"),
398 &myself->maclength)) {
400 if(myself->maclength > myself->digest->md_size) {
401 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
403 } else if(myself->maclength < 0) {
404 logger(LOG_ERR, _("Bogus MAC length!"));
409 myself->maclength = 4;
411 myself->connection->outmaclength = 0;
415 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"),
416 &myself->compression)) {
417 if(myself->compression < 0 || myself->compression > 11) {
418 logger(LOG_ERR, _("Bogus compression level!"));
422 myself->compression = 0;
424 myself->connection->outcompression = 0;
428 myself->nexthop = myself;
429 myself->via = myself;
430 myself->status.active = true;
431 myself->status.reachable = true;
441 /* Run tinc-up script to further initialize the tap interface */
442 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
443 asprintf(&envp[1], "DEVICE=%s", device ? : "");
444 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
445 asprintf(&envp[3], "NAME=%s", myself->name);
448 execute_script("tinc-up", envp);
450 for(i = 0; i < 5; i++)
455 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
457 hint.ai_family = addressfamily;
458 hint.ai_socktype = SOCK_STREAM;
459 hint.ai_protocol = IPPROTO_TCP;
460 hint.ai_flags = AI_PASSIVE;
462 err = getaddrinfo(address, myport, &hint, &ai);
465 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
472 for(aip = ai; aip; aip = aip->ai_next) {
473 listen_socket[listen_sockets].tcp =
474 setup_listen_socket((sockaddr_t *) aip->ai_addr);
476 if(listen_socket[listen_sockets].tcp < 0)
479 listen_socket[listen_sockets].udp =
480 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
482 if(listen_socket[listen_sockets].udp < 0)
485 ifdebug(CONNECTIONS) {
486 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
487 logger(LOG_NOTICE, _("Listening on %s"), hostname);
491 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
498 logger(LOG_NOTICE, _("Ready"));
500 logger(LOG_ERR, _("Unable to create any listening socket!"));
508 setup all initial network connections
510 bool setup_network_connections(void)
523 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
524 if(pingtimeout < 1) {
533 try_outgoing_connections();
539 close all open network connections
541 void close_network_connections(void)
543 avl_node_t *node, *next;
550 for(node = connection_tree->head; node; node = next) {
555 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
556 terminate_connection(c, false);
559 if(myself && myself->connection)
560 terminate_connection(myself->connection, false);
562 for(i = 0; i < listen_sockets; i++) {
563 close(listen_socket[i].tcp);
564 close(listen_socket[i].udp);
574 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
575 asprintf(&envp[1], "DEVICE=%s", device ? : "");
576 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
577 asprintf(&envp[3], "NAME=%s", myself->name);
580 execute_script("tinc-down", envp);
582 for(i = 0; i < 4; i++)