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.24 2002/09/09 19:39:58 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 fp = fopen(fname, "r");
108 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
109 fname, strerror(errno));
114 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
117 return 0; /* Woohoo. */
119 /* If it fails, try PEM_read_RSA_PUBKEY. */
120 fp = fopen(fname, "r");
123 syslog(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
124 fname, strerror(errno));
129 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
134 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
135 fname, strerror(errno));
145 /* Else, check if a harnessed public key is in the config file */
147 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
148 fp = fopen(fname, "r");
152 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
161 /* Try again with PEM_read_RSA_PUBKEY. */
163 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
164 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);
181 int read_rsa_private_key(void)
186 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
188 myself->connection->rsa_key = RSA_new();
189 BN_hex2bn(&myself->connection->rsa_key->d, key);
190 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
195 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
196 asprintf(&fname, "%s/rsa_key.priv", confbase);
198 if(is_safe_path(fname))
200 fp = fopen(fname, "r");
203 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
204 fname, strerror(errno));
209 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
211 if(!myself->connection->rsa_key)
213 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
214 fname, strerror(errno));
225 Configure node_t myself and set up the local sockets (listen only)
227 int setup_myself(void)
231 char *name, *hostname, *mode, *afname, *cipher, *digest;
232 char *address = NULL;
233 struct addrinfo hint, *ai, *aip;
237 myself->connection = new_connection();
238 init_configuration(&myself->connection->config_tree);
240 asprintf(&myself->hostname, _("MYSELF"));
241 asprintf(&myself->connection->hostname, _("MYSELF"));
243 myself->connection->options = 0;
244 myself->connection->protocol_version = PROT_CURRENT;
246 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
248 syslog(LOG_ERR, _("Name for tinc daemon required!"));
254 syslog(LOG_ERR, _("Invalid name for myself!"));
260 myself->connection->name = xstrdup(name);
263 if(read_rsa_private_key())
266 if(read_connection_config(myself->connection))
268 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
272 if(read_rsa_public_key(myself->connection))
276 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
277 asprintf(&myport, "655");
279 /* Read in all the subnets specified in the host configuration file */
281 cfg = lookup_config(myself->connection->config_tree, "Subnet");
285 if(!get_config_subnet(cfg, &subnet))
288 subnet_add(myself, subnet);
290 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;
304 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
306 myself->options |= OPTION_INDIRECT;
308 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
310 myself->options |= OPTION_TCPONLY;
312 if(myself->options & OPTION_TCPONLY)
313 myself->options |= OPTION_INDIRECT;
315 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
317 if(!strcasecmp(mode, "router"))
318 routing_mode = RMODE_ROUTER;
319 else if (!strcasecmp(mode, "switch"))
320 routing_mode = RMODE_SWITCH;
321 else if (!strcasecmp(mode, "hub"))
322 routing_mode = RMODE_HUB;
325 syslog(LOG_ERR, _("Invalid routing mode!"));
331 routing_mode = RMODE_ROUTER;
333 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
334 #if !defined(SOL_IP) || !defined(IP_TOS)
335 if(priorityinheritance)
336 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
339 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
342 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
346 syslog(LOG_ERR, _("Bogus maximum timeout!"));
353 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
355 if(!strcasecmp(afname, "IPv4"))
356 addressfamily = AF_INET;
357 else if (!strcasecmp(afname, "IPv6"))
358 addressfamily = AF_INET6;
359 else if (!strcasecmp(afname, "any"))
360 addressfamily = AF_UNSPEC;
363 syslog(LOG_ERR, _("Invalid address family!"));
369 addressfamily = AF_INET;
371 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
373 /* Generate packet encryption key */
375 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
377 if(!strcasecmp(cipher, "none"))
379 myself->cipher = NULL;
383 myself->cipher = EVP_get_cipherbyname(cipher);
387 syslog(LOG_ERR, _("Unrecognized cipher type!"));
393 myself->cipher = EVP_bf_cbc();
396 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
398 myself->keylength = 1;
400 myself->connection->outcipher = EVP_bf_ofb();
402 myself->key = (char *)xmalloc(myself->keylength);
403 RAND_pseudo_bytes(myself->key, myself->keylength);
405 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
408 keyexpires = now + keylifetime;
410 /* Check if we want to use message authentication codes... */
412 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
414 if(!strcasecmp(digest, "none"))
416 myself->digest = NULL;
420 myself->digest = EVP_get_digestbyname(digest);
424 syslog(LOG_ERR, _("Unrecognized digest type!"));
430 myself->digest = EVP_sha1();
432 myself->connection->outdigest = EVP_sha1();
434 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
438 if(myself->maclength > myself->digest->md_size)
440 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
443 else if (myself->maclength < 0)
445 syslog(LOG_ERR, _("Bogus MAC length!"));
451 myself->maclength = 4;
453 myself->connection->outmaclength = 0;
457 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
459 if(myself->compression < 0 || myself->compression > 9)
461 syslog(LOG_ERR, _("Bogus compression level!"));
466 myself->compression = 0;
468 myself->connection->outcompression = 0;
472 myself->nexthop = myself;
473 myself->via = myself;
474 myself->status.active = 1;
475 myself->status.reachable = 1;
483 memset(&hint, 0, sizeof(hint));
485 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
487 hint.ai_family = addressfamily;
488 hint.ai_socktype = SOCK_STREAM;
489 hint.ai_protocol = IPPROTO_TCP;
490 hint.ai_flags = AI_PASSIVE;
492 err = getaddrinfo(address, myport, &hint, &ai);
496 syslog(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo", gai_strerror(err));
502 for(aip = ai; aip; aip = aip->ai_next)
504 listen_socket[listen_sockets].tcp = setup_listen_socket((sockaddr_t *)aip->ai_addr);
506 if(listen_socket[listen_sockets].tcp < 0)
509 listen_socket[listen_sockets].udp = setup_vpn_in_socket((sockaddr_t *)aip->ai_addr);
511 if(listen_socket[listen_sockets].udp < 0)
514 if(debug_lvl >= DEBUG_CONNECTIONS)
516 hostname = sockaddr2hostname((sockaddr_t *)aip->ai_addr);
517 syslog(LOG_NOTICE, _("Listening on %s"), hostname);
521 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
528 syslog(LOG_NOTICE, _("Ready"));
531 syslog(LOG_ERR, _("Unable to create any listening socket!"));
539 setup all initial network connections
541 int setup_network_connections(void)
555 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
565 if(setup_device() < 0)
568 /* Run tinc-up script to further initialize the tap interface */
569 asprintf(&envp[0], "NETNAME=%s", netname?:"");
570 asprintf(&envp[1], "DEVICE=%s", device?:"");
571 asprintf(&envp[2], "INTERFACE=%s", interface?:"");
574 execute_script("tinc-up", envp);
576 for(i = 0; i < 4; i++)
579 if(setup_myself() < 0)
582 try_outgoing_connections();
588 close all open network connections
590 void close_network_connections(void)
592 avl_node_t *node, *next;
597 for(node = connection_tree->head; node; node = next)
600 c = (connection_t *)node->data;
602 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
603 terminate_connection(c, 0);
606 if(myself && myself->connection)
607 terminate_connection(myself->connection, 0);
609 for(i = 0; i < listen_sockets; i++)
611 close(listen_socket[i].tcp);
612 close(listen_socket[i].udp);
622 asprintf(&envp[0], "NETNAME=%s", netname?:"");
623 asprintf(&envp[1], "DEVICE=%s", device?:"");
624 asprintf(&envp[2], "INTERFACE=%s", interface?:"");
627 execute_script("tinc-down", envp);
629 for(i = 0; i < 4; i++)