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(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 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);
184 #ifdef _DIRENT_HAVE_D_TYPE
185 //if(ent->d_type != DT_REG)
189 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
190 init_configuration(&config_tree);
191 result = read_config_file(config_tree, fname);
198 n->name = xstrdup(ent->d_name);
202 for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
203 if(!get_config_subnet(cfg, &s))
206 if((s2 = lookup_subnet(n, s))) {
213 exit_configuration(&config_tree);
220 Configure node_t myself and set up the local sockets (listen only)
222 bool setup_myself(void) {
225 char *name, *hostname, *mode, *afname, *cipher, *digest;
227 char *address = NULL;
229 struct addrinfo *ai, *aip, hint = {0};
234 myself->connection = new_connection();
236 myself->hostname = xstrdup("MYSELF");
237 myself->connection->hostname = xstrdup("MYSELF");
239 myself->connection->options = 0;
240 myself->connection->protocol_version = PROT_CURRENT;
242 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
243 logger(LOG_ERR, "Name for tinc daemon required!");
247 if(!check_id(name)) {
248 logger(LOG_ERR, "Invalid name for myself!");
254 myself->connection->name = xstrdup(name);
255 xasprintf(&fname, "%s/hosts/%s", confbase, name);
256 read_config_options(config_tree, name);
257 read_config_file(config_tree, fname);
260 if(!read_rsa_private_key())
263 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
264 myport = xstrdup("655");
267 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
269 if(!ai || !ai->ai_addr)
272 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
273 sockaddr2str(&sa, NULL, &myport);
276 /* Read in all the subnets specified in the host configuration file */
278 cfg = lookup_config(config_tree, "Subnet");
281 if(!get_config_subnet(cfg, &subnet))
284 subnet_add(myself, subnet);
286 cfg = lookup_config_next(config_tree, cfg);
289 /* Check some options */
291 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
292 myself->options |= OPTION_INDIRECT;
294 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
295 myself->options |= OPTION_TCPONLY;
297 if(myself->options & OPTION_TCPONLY)
298 myself->options |= OPTION_INDIRECT;
300 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
301 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
302 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
303 strictsubnets |= tunnelserver;
305 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
306 if(!strcasecmp(mode, "router"))
307 routing_mode = RMODE_ROUTER;
308 else if(!strcasecmp(mode, "switch"))
309 routing_mode = RMODE_SWITCH;
310 else if(!strcasecmp(mode, "hub"))
311 routing_mode = RMODE_HUB;
313 logger(LOG_ERR, "Invalid routing mode!");
319 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
320 if(!strcasecmp(mode, "off"))
321 forwarding_mode = FMODE_OFF;
322 else if(!strcasecmp(mode, "internal"))
323 forwarding_mode = FMODE_INTERNAL;
324 else if(!strcasecmp(mode, "kernel"))
325 forwarding_mode = FMODE_KERNEL;
327 logger(LOG_ERR, "Invalid forwarding mode!");
334 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
336 myself->options |= OPTION_PMTU_DISCOVERY;
339 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
341 myself->options |= OPTION_CLAMP_MSS;
343 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
345 #if !defined(SOL_IP) || !defined(IP_TOS)
346 if(priorityinheritance)
347 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
350 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
353 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
354 if(maxtimeout <= 0) {
355 logger(LOG_ERR, "Bogus maximum timeout!");
361 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
362 if(!strcasecmp(afname, "IPv4"))
363 addressfamily = AF_INET;
364 else if(!strcasecmp(afname, "IPv6"))
365 addressfamily = AF_INET6;
366 else if(!strcasecmp(afname, "any"))
367 addressfamily = AF_UNSPEC;
369 logger(LOG_ERR, "Invalid address family!");
375 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
377 /* Generate packet encryption key */
379 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
380 cipher = xstrdup("blowfish");
382 if(!cipher_open_by_name(&myself->incipher, cipher)) {
383 logger(LOG_ERR, "Unrecognized cipher type!");
387 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
392 /* Check if we want to use message authentication codes... */
394 if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
395 digest = xstrdup("sha1");
398 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
401 logger(LOG_ERR, "Bogus MAC length!");
405 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
406 logger(LOG_ERR, "Unrecognized digest type!");
412 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
413 if(myself->incompression < 0 || myself->incompression > 11) {
414 logger(LOG_ERR, "Bogus compression level!");
418 myself->incompression = 0;
420 myself->connection->outcompression = 0;
424 myself->nexthop = myself;
425 myself->via = myself;
426 myself->status.reachable = true;
440 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
442 if (event_add(&device_ev, NULL) < 0) {
443 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
449 /* Run tinc-up script to further initialize the tap interface */
450 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
451 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
452 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
453 xasprintf(&envp[3], "NAME=%s", myself->name);
456 execute_script("tinc-up", envp);
458 for(i = 0; i < 4; i++)
461 /* Run subnet-up scripts for our own subnets */
463 subnet_update(myself, NULL, true);
467 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
469 hint.ai_family = addressfamily;
470 hint.ai_socktype = SOCK_STREAM;
471 hint.ai_protocol = IPPROTO_TCP;
472 hint.ai_flags = AI_PASSIVE;
474 err = getaddrinfo(address, myport, &hint, &ai);
477 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
484 for(aip = ai; aip; aip = aip->ai_next) {
485 listen_socket[listen_sockets].tcp =
486 setup_listen_socket((sockaddr_t *) aip->ai_addr);
488 if(listen_socket[listen_sockets].tcp < 0)
491 listen_socket[listen_sockets].udp =
492 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
494 if(listen_socket[listen_sockets].udp < 0) {
495 close(listen_socket[listen_sockets].tcp);
499 event_set(&listen_socket[listen_sockets].ev_tcp,
500 listen_socket[listen_sockets].tcp,
502 handle_new_meta_connection, NULL);
503 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
504 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
508 event_set(&listen_socket[listen_sockets].ev_udp,
509 listen_socket[listen_sockets].udp,
511 handle_incoming_vpn_data, NULL);
512 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
513 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
517 ifdebug(CONNECTIONS) {
518 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
519 logger(LOG_NOTICE, "Listening on %s", hostname);
523 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
526 if(listen_sockets >= MAXSOCKETS) {
527 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
535 logger(LOG_NOTICE, "Ready");
537 logger(LOG_ERR, "Unable to create any listening socket!");
547 bool setup_network(void) {
554 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
555 if(pinginterval < 1) {
556 pinginterval = 86400;
561 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
563 if(pingtimeout < 1 || pingtimeout > pinginterval)
564 pingtimeout = pinginterval;
566 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
567 maxoutbufsize = 10 * MTU;
576 close all open network connections
578 void close_network_connections(void) {
579 splay_node_t *node, *next;
584 for(node = connection_tree->head; node; node = next) {
588 terminate_connection(c, false);
591 list_delete_list(outgoing_list);
593 if(myself && myself->connection) {
594 subnet_update(myself, NULL, false);
595 terminate_connection(myself->connection, false);
596 free_connection(myself->connection);
599 for(i = 0; i < listen_sockets; i++) {
600 event_del(&listen_socket[i].ev_tcp);
601 event_del(&listen_socket[i].ev_udp);
602 close(listen_socket[i].tcp);
603 close(listen_socket[i].udp);
606 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
607 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
608 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
609 xasprintf(&envp[3], "NAME=%s", myself->name);
618 execute_script("tinc-down", envp);
620 if(myport) free(myport);
622 for(i = 0; i < 4; i++)