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.50 2003/12/20 21:25:17 guus Exp $
25 #include <gnutls/gnutls.h>
26 #include <gnutls/x509.h>
31 #include "connection.h"
48 bool read_rsa_public_key(connection_t *c)
57 c->rsa_key = RSA_new();
58 // RSA_blinding_on(c->rsa_key, NULL);
61 /* First, check for simple PublicKey statement */
63 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
64 BN_hex2bn(&c->rsa_key->n, key);
65 BN_hex2bn(&c->rsa_key->e, "FFFF");
70 /* Else, check for PublicKeyFile statement and read it */
72 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname)) {
73 fp = fopen(fname, "r");
76 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
77 fname, strerror(errno));
83 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
87 return true; /* Woohoo. */
89 /* If it fails, try PEM_read_RSA_PUBKEY. */
90 fp = fopen(fname, "r");
93 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
94 fname, strerror(errno));
100 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
104 // RSA_blinding_on(c->rsa_key, NULL);
108 logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
109 fname, strerror(errno));
113 /* Else, check if a harnessed public key is in the config file */
115 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
116 fp = fopen(fname, "r");
119 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
128 /* Try again with PEM_read_RSA_PUBKEY. */
130 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
131 fp = fopen(fname, "r");
134 c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
135 // RSA_blinding_on(c->rsa_key, NULL);
144 logger(LOG_ERR, _("No public key for %s specified!"), c->name);
150 bool setup_credentials(void)
152 char *trust = NULL, *crl = NULL;
153 char *key = NULL, *cert = NULL;
158 gnutls_certificate_allocate_credentials(&myself->connection->credentials);
160 if(get_config_string(lookup_config(config_tree, "TrustFile"), &trust)) {
161 result = gnutls_certificate_set_x509_trust_file(myself->connection->credentials, trust, GNUTLS_X509_FMT_PEM);
163 logger(LOG_ERR, _("Error reading trust file '%s': %s"), trust, gnutls_strerror(result));
170 if(get_config_string(lookup_config(config_tree, "CRLFile"), &crl)) {
171 result = gnutls_certificate_set_x509_crl_file(myself->connection->credentials, crl, GNUTLS_X509_FMT_PEM);
173 logger(LOG_ERR, _("Error reading CRL file '%s': %s"), crl, gnutls_strerror(result));
180 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &key))
181 asprintf(&key, "%s/rsa_key.priv", confbase);
183 if(!get_config_string(lookup_config(config_tree, "CertificateFile"), &cert))
184 asprintf(&cert, "%s/hosts/%s", confbase, myself->name);
187 gnutls_certificate_set_x509_trust_file(myself->connection->credentials, cert, GNUTLS_X509_FMT_PEM);
188 logger(LOG_DEBUG, _("JOEHOE"));
189 gnutls_certificate_set_verify_flags(myself->connection->credentials, GNUTLS_VERIFY_DISABLE_CA_SIGN);
191 result = gnutls_certificate_set_x509_key_file(myself->connection->credentials, cert, key, GNUTLS_X509_FMT_PEM);
194 logger(LOG_ERR, _("Error reading credentials from %s and %s: %s"), cert, key, gnutls_strerror(result));
207 Configure node_t myself and set up the local sockets (listen only)
209 bool setup_myself(void)
213 char *name, *hostname, *mode, *afname, *cipher, *digest;
214 char *address = NULL;
216 struct addrinfo *ai, *aip, hint = {0};
223 myself->connection = new_connection();
224 init_configuration(&myself->connection->config_tree);
226 asprintf(&myself->hostname, _("MYSELF"));
227 asprintf(&myself->connection->hostname, _("MYSELF"));
229 myself->connection->options = 0;
230 myself->connection->protocol_version = PROT_CURRENT;
232 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
233 logger(LOG_ERR, _("Name for tinc daemon required!"));
237 if(!check_id(name)) {
238 logger(LOG_ERR, _("Invalid name for myself!"));
244 myself->connection->name = xstrdup(name);
246 if(!setup_credentials())
249 if(!read_connection_config(myself->connection)) {
250 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
254 if(!get_config_string (lookup_config(myself->connection->config_tree, "Port"), &myport))
255 asprintf(&myport, "655");
257 /* Read in all the subnets specified in the host configuration file */
259 cfg = lookup_config(myself->connection->config_tree, "Subnet");
262 if(!get_config_subnet(cfg, &subnet))
265 subnet_add(myself, subnet);
267 cfg = lookup_config_next(myself->connection->config_tree, cfg);
270 /* Check some options */
272 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
273 myself->options |= OPTION_INDIRECT;
275 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
276 myself->options |= OPTION_TCPONLY;
278 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
279 myself->options |= OPTION_INDIRECT;
281 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
282 myself->options |= OPTION_TCPONLY;
284 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice)
285 myself->options |= OPTION_PMTU_DISCOVERY;
287 if(myself->options & OPTION_TCPONLY)
288 myself->options |= OPTION_INDIRECT;
290 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
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(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 */
343 if(get_config_string (lookup_config(myself->connection->config_tree, "Cipher"), &cipher)) {
344 if(!strcasecmp(cipher, "none")) {
345 myself->cipher = GCRY_CIPHER_NONE;
347 myself->cipher = gcry_cipher_map_name(cipher);
349 if(!myself->cipher) {
350 logger(LOG_ERR, _("Unrecognized cipher type!"));
355 myself->cipher = GCRY_CIPHER_AES;
358 result = gcry_cipher_open(&myself->cipher_ctx, myself->cipher, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_SECURE);
361 logger(LOG_ERR, _("Error during initialisation of cipher for %s (%s): %s"),
362 myself->name, myself->hostname, gcry_strerror(result));
369 myself->cipherkeylen = gcry_cipher_get_algo_keylen(myself->cipher);
370 myself->cipherblklen = gcry_cipher_get_algo_blklen(myself->cipher);
372 myself->cipherkeylen = 1;
375 logger(LOG_DEBUG, _("Key %s len %d"), gcry_cipher_algo_name(myself->cipher), myself->cipherkeylen);
376 myself->cipherkey = xmalloc(myself->cipherkeylen);
377 gcry_randomize(myself->cipherkey, myself->cipherkeylen, GCRY_STRONG_RANDOM);
379 gcry_cipher_setkey(myself->cipher_ctx, myself->cipherkey, myself->cipherkeylen);
381 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
384 keyexpires = now + keylifetime;
386 /* Check if we want to use message authentication codes... */
388 if(get_config_string (lookup_config(myself->connection->config_tree, "Digest"), &digest)) {
389 if(!strcasecmp(digest, "none")) {
390 myself->digest = GCRY_MD_NONE;
392 myself->digest = gcry_md_map_name(digest);
394 if(!myself->digest) {
395 logger(LOG_ERR, _("Unrecognized digest type!"));
400 myself->digest = GCRY_MD_SHA1;
404 result = gcry_md_open(&myself->digest_ctx, myself->digest, GCRY_MD_FLAG_SECURE | GCRY_MD_FLAG_HMAC);
407 logger(LOG_ERR, _("Error during initialisation of digest for %s (%s): %s"),
408 myself->name, myself->hostname, gcry_strerror(result));
415 myself->digestlen = gcry_md_get_algo_dlen(myself->digest);
417 myself->digestlen = 1;
420 myself->digestkey = xmalloc(myself->digestlen);
421 gcry_randomize(myself->digestkey, myself->digestlen, GCRY_STRONG_RANDOM);
423 gcry_md_setkey(myself->digest_ctx, myself->digestkey, myself->digestlen);
425 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength)) {
427 if(myself->maclength > myself->digestlen) {
428 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
430 } else if(myself->maclength < 0) {
431 logger(LOG_ERR, _("Bogus MAC length!"));
436 myself->maclength = 4;
440 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"),
441 &myself->compression)) {
442 if(myself->compression < 0 || myself->compression > 11) {
443 logger(LOG_ERR, _("Bogus compression level!"));
447 myself->compression = 0;
451 myself->nexthop = myself;
452 myself->via = myself;
453 myself->status.active = true;
454 myself->status.reachable = true;
464 /* Run tinc-up script to further initialize the tap interface */
465 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
466 asprintf(&envp[1], "DEVICE=%s", device ? : "");
467 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
468 asprintf(&envp[3], "NAME=%s", myself->name);
471 execute_script("tinc-up", envp);
473 for(i = 0; i < 5; i++)
478 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
480 hint.ai_family = addressfamily;
481 hint.ai_socktype = SOCK_STREAM;
482 hint.ai_protocol = IPPROTO_TCP;
483 hint.ai_flags = AI_PASSIVE;
485 err = getaddrinfo(address, myport, &hint, &ai);
488 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
495 for(aip = ai; aip; aip = aip->ai_next) {
496 listen_socket[listen_sockets].tcp =
497 setup_listen_socket((sockaddr_t *) aip->ai_addr);
499 if(listen_socket[listen_sockets].tcp < 0)
502 listen_socket[listen_sockets].udp =
503 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
505 if(listen_socket[listen_sockets].udp < 0)
508 ifdebug(CONNECTIONS) {
509 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
510 logger(LOG_NOTICE, _("Listening on %s"), hostname);
514 listen_socket[listen_sockets].sa.sa = *aip->ai_addr;
521 logger(LOG_NOTICE, _("Ready"));
523 logger(LOG_ERR, _("Unable to create any listening socket!"));
531 setup all initial network connections
533 bool setup_network_connections(void)
546 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
547 if(pingtimeout < 1) {
556 try_outgoing_connections();
562 close all open network connections
564 void close_network_connections(void)
566 avl_node_t *node, *next;
573 for(node = connection_tree->head; node; node = next) {
578 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
579 terminate_connection(c, false);
582 if(myself && myself->connection)
583 terminate_connection(myself->connection, false);
585 for(i = 0; i < listen_sockets; i++) {
586 close(listen_socket[i].tcp);
587 close(listen_socket[i].udp);
597 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
598 asprintf(&envp[1], "DEVICE=%s", device ? : "");
599 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
600 asprintf(&envp[3], "NAME=%s", myself->name);
603 execute_script("tinc-down", envp);
605 for(i = 0; i < 4; i++)