3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "splay_tree.h"
27 #include "connection.h"
44 static struct event device_ev;
46 bool read_rsa_public_key(connection_t *c) {
52 /* First, check for simple PublicKey statement */
54 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
55 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
60 /* Else, check for PublicKeyFile statement and read it */
62 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
63 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
65 fp = fopen(fname, "r");
68 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
69 fname, strerror(errno));
74 result = rsa_read_pem_public_key(&c->rsa, fp);
78 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
83 bool read_rsa_private_key() {
89 /* First, check for simple PrivateKey statement */
91 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
92 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &n)) {
93 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
97 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
103 /* Else, check for PrivateKeyFile statement and read it */
105 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
106 xasprintf(&fname, "%s/rsa_key.priv", confbase);
108 fp = fopen(fname, "r");
111 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
112 fname, strerror(errno));
117 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
120 if(fstat(fileno(fp), &s)) {
121 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
126 if(s.st_mode & ~0100700)
127 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
130 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
134 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
139 static struct event keyexpire_event;
141 static void keyexpire_handler(int fd, short events, void *data) {
145 void regenerate_key() {
146 if(timeout_initialized(&keyexpire_event)) {
147 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
148 event_del(&keyexpire_event);
149 send_key_changed(broadcast, myself);
151 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
154 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
158 Read Subnets from all host config files
160 static void load_all_subnets(void) {
165 splay_tree_t *config_tree;
171 xasprintf(&dname, "%s/hosts", confbase);
172 dir = opendir(dname);
174 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
179 while((ent = readdir(dir))) {
180 if(!check_id(ent->d_name))
183 n = lookup_node(ent->d_name);
187 #ifdef _DIRENT_HAVE_D_TYPE
188 //if(ent->d_type != DT_REG)
192 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
193 init_configuration(&config_tree);
194 result = read_config_file(config_tree, fname);
200 n->name = xstrdup(ent->d_name);
203 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
204 if(!get_config_subnet(cfg, &s))
210 exit_configuration(&config_tree);
217 Configure node_t myself and set up the local sockets (listen only)
219 bool setup_myself(void) {
222 char *name, *hostname, *mode, *afname, *cipher, *digest;
223 char *address = NULL;
225 struct addrinfo *ai, *aip, hint = {0};
230 myself->connection = new_connection();
231 init_configuration(&myself->connection->config_tree);
233 myself->hostname = xstrdup("MYSELF");
234 myself->connection->hostname = xstrdup("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 */
240 logger(LOG_ERR, "Name for tinc daemon required!");
244 if(!check_id(name)) {
245 logger(LOG_ERR, "Invalid name for myself!");
251 myself->connection->name = xstrdup(name);
253 if(!read_connection_config(myself->connection)) {
254 logger(LOG_ERR, "Cannot open host configuration file for myself!");
258 if(!read_rsa_private_key())
261 if(!get_config_string(lookup_config(config_tree, "Port"), &myport)
262 && !get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
263 myport = xstrdup("655");
265 /* Read in all the subnets specified in the host configuration file */
267 cfg = lookup_config(myself->connection->config_tree, "Subnet");
270 if(!get_config_subnet(cfg, &subnet))
273 subnet_add(myself, subnet);
275 cfg = lookup_config_next(myself->connection->config_tree, cfg);
278 /* Check some options */
280 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
281 myself->options |= OPTION_INDIRECT;
283 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
284 myself->options |= OPTION_TCPONLY;
286 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
287 myself->options |= OPTION_INDIRECT;
289 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
290 myself->options |= OPTION_TCPONLY;
292 if(myself->options & OPTION_TCPONLY)
293 myself->options |= OPTION_INDIRECT;
295 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
296 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
297 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
298 strictsubnets |= tunnelserver;
300 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
301 if(!strcasecmp(mode, "router"))
302 routing_mode = RMODE_ROUTER;
303 else if(!strcasecmp(mode, "switch"))
304 routing_mode = RMODE_SWITCH;
305 else if(!strcasecmp(mode, "hub"))
306 routing_mode = RMODE_HUB;
308 logger(LOG_ERR, "Invalid routing mode!");
314 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
315 if(!strcasecmp(mode, "off"))
316 forwarding_mode = FMODE_OFF;
317 else if(!strcasecmp(mode, "internal"))
318 forwarding_mode = FMODE_INTERNAL;
319 else if(!strcasecmp(mode, "kernel"))
320 forwarding_mode = FMODE_KERNEL;
322 logger(LOG_ERR, "Invalid forwarding mode!");
329 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
330 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
332 myself->options |= OPTION_PMTU_DISCOVERY;
335 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
336 get_config_bool(lookup_config(myself->connection->config_tree, "ClampMSS"), &choice);
338 myself->options |= OPTION_CLAMP_MSS;
340 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
342 #if !defined(SOL_IP) || !defined(IP_TOS)
343 if(priorityinheritance)
344 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
347 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
350 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
351 if(maxtimeout <= 0) {
352 logger(LOG_ERR, "Bogus maximum timeout!");
358 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
359 if(!strcasecmp(afname, "IPv4"))
360 addressfamily = AF_INET;
361 else if(!strcasecmp(afname, "IPv6"))
362 addressfamily = AF_INET6;
363 else if(!strcasecmp(afname, "any"))
364 addressfamily = AF_UNSPEC;
366 logger(LOG_ERR, "Invalid address family!");
372 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
374 /* Generate packet encryption key */
376 if(!get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
377 cipher = xstrdup("blowfish");
379 if(!cipher_open_by_name(&myself->incipher, cipher)) {
380 logger(LOG_ERR, "Unrecognized cipher type!");
384 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
389 /* Check if we want to use message authentication codes... */
391 if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
392 digest = xstrdup("sha1");
395 get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &maclength);
398 logger(LOG_ERR, "Bogus MAC length!");
402 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
403 logger(LOG_ERR, "Unrecognized digest type!");
409 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
410 if(myself->incompression < 0 || myself->incompression > 11) {
411 logger(LOG_ERR, "Bogus compression level!");
415 myself->incompression = 0;
417 myself->connection->outcompression = 0;
421 myself->nexthop = myself;
422 myself->via = myself;
423 myself->status.reachable = true;
437 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
439 if (event_add(&device_ev, NULL) < 0) {
440 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
446 /* Run tinc-up script to further initialize the tap interface */
447 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
448 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
449 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
450 xasprintf(&envp[3], "NAME=%s", myself->name);
453 execute_script("tinc-up", envp);
455 for(i = 0; i < 5; i++)
458 /* Run subnet-up scripts for our own subnets */
460 subnet_update(myself, NULL, true);
464 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
466 hint.ai_family = addressfamily;
467 hint.ai_socktype = SOCK_STREAM;
468 hint.ai_protocol = IPPROTO_TCP;
469 hint.ai_flags = AI_PASSIVE;
471 err = getaddrinfo(address, myport, &hint, &ai);
474 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
481 for(aip = ai; aip; aip = aip->ai_next) {
482 listen_socket[listen_sockets].tcp =
483 setup_listen_socket((sockaddr_t *) aip->ai_addr);
485 if(listen_socket[listen_sockets].tcp < 0)
488 listen_socket[listen_sockets].udp =
489 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
491 if(listen_socket[listen_sockets].udp < 0) {
492 close(listen_socket[listen_sockets].tcp);
496 event_set(&listen_socket[listen_sockets].ev_tcp,
497 listen_socket[listen_sockets].tcp,
499 handle_new_meta_connection, NULL);
500 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
501 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
505 event_set(&listen_socket[listen_sockets].ev_udp,
506 listen_socket[listen_sockets].udp,
508 handle_incoming_vpn_data, NULL);
509 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
510 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
514 ifdebug(CONNECTIONS) {
515 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
516 logger(LOG_NOTICE, "Listening on %s", hostname);
520 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
523 if(listen_sockets >= MAXSOCKETS) {
524 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
532 logger(LOG_NOTICE, "Ready");
534 logger(LOG_ERR, "Unable to create any listening socket!");
544 bool setup_network(void) {
551 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
552 if(pinginterval < 1) {
553 pinginterval = 86400;
558 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
560 if(pingtimeout < 1 || pingtimeout > pinginterval)
561 pingtimeout = pinginterval;
563 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
564 maxoutbufsize = 10 * MTU;
573 close all open network connections
575 void close_network_connections(void) {
576 splay_node_t *node, *next;
581 for(node = connection_tree->head; node; node = next) {
585 terminate_connection(c, false);
588 list_delete_list(outgoing_list);
590 if(myself && myself->connection) {
591 subnet_update(myself, NULL, false);
592 terminate_connection(myself->connection, false);
593 free_connection(myself->connection);
596 for(i = 0; i < listen_sockets; i++) {
597 event_del(&listen_socket[i].ev_tcp);
598 event_del(&listen_socket[i].ev_udp);
599 close(listen_socket[i].tcp);
600 close(listen_socket[i].udp);
603 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
604 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
605 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
606 xasprintf(&envp[3], "NAME=%s", myself->name);
615 execute_script("tinc-down", envp);
617 if(myport) free(myport);
619 for(i = 0; i < 4; i++)