3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2010 Brandon Black <blblack@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "splay_tree.h"
28 #include "connection.h"
45 static struct event device_ev;
47 bool read_rsa_public_key(connection_t *c) {
53 /* First, check for simple PublicKey statement */
55 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
56 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
61 /* Else, check for PublicKeyFile statement and read it */
63 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
64 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
66 fp = fopen(fname, "r");
69 logger(LOG_ERR, "Error reading RSA public key file `%s': %s",
70 fname, strerror(errno));
75 result = rsa_read_pem_public_key(&c->rsa, fp);
79 logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
84 bool read_rsa_private_key() {
90 /* First, check for simple PrivateKey statement */
92 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
93 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
94 logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
98 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
104 /* Else, check for PrivateKeyFile statement and read it */
106 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
107 xasprintf(&fname, "%s/rsa_key.priv", confbase);
109 fp = fopen(fname, "r");
112 logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
113 fname, strerror(errno));
118 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
121 if(fstat(fileno(fp), &s)) {
122 logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
127 if(s.st_mode & ~0100700)
128 logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
131 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
135 logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
140 static struct event keyexpire_event;
142 static void keyexpire_handler(int fd, short events, void *data) {
146 void regenerate_key() {
147 if(timeout_initialized(&keyexpire_event)) {
148 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
149 event_del(&keyexpire_event);
150 send_key_changed(broadcast, myself);
152 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
155 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
159 Read Subnets from all host config files
161 void load_all_subnets(void) {
166 splay_tree_t *config_tree;
172 xasprintf(&dname, "%s/hosts", confbase);
173 dir = opendir(dname);
175 logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
180 while((ent = readdir(dir))) {
181 if(!check_id(ent->d_name))
184 n = lookup_node(ent->d_name);
185 #ifdef _DIRENT_HAVE_D_TYPE
186 //if(ent->d_type != DT_REG)
190 xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
191 init_configuration(&config_tree);
192 result = read_config_file(config_tree, fname);
199 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))
207 if((s2 = lookup_subnet(n, s))) {
214 exit_configuration(&config_tree);
221 Configure node_t myself and set up the local sockets (listen only)
223 bool setup_myself(void) {
226 char *name, *hostname, *mode, *afname, *cipher, *digest;
228 char *address = NULL;
230 struct addrinfo *ai, *aip, hint = {0};
236 myself->connection = new_connection();
238 myself->hostname = xstrdup("MYSELF");
239 myself->connection->hostname = xstrdup("MYSELF");
241 myself->connection->options = 0;
242 myself->connection->protocol_version = PROT_CURRENT;
244 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
245 logger(LOG_ERR, "Name for tinc daemon required!");
249 if(!check_id(name)) {
250 logger(LOG_ERR, "Invalid name for myself!");
256 myself->connection->name = xstrdup(name);
257 xasprintf(&fname, "%s/hosts/%s", confbase, name);
258 read_config_options(config_tree, name);
259 read_config_file(config_tree, fname);
262 if(!read_rsa_private_key())
265 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
266 myport = xstrdup("655");
269 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
271 if(!ai || !ai->ai_addr)
274 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
275 sockaddr2str(&sa, NULL, &myport);
278 /* Read in all the subnets specified in the host configuration file */
280 cfg = lookup_config(config_tree, "Subnet");
283 if(!get_config_subnet(cfg, &subnet))
286 subnet_add(myself, subnet);
288 cfg = lookup_config_next(config_tree, cfg);
291 /* Check some options */
293 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
294 myself->options |= OPTION_INDIRECT;
296 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
297 myself->options |= OPTION_TCPONLY;
299 if(myself->options & OPTION_TCPONLY)
300 myself->options |= OPTION_INDIRECT;
302 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
303 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
304 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
305 strictsubnets |= tunnelserver;
307 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
308 if(!strcasecmp(mode, "router"))
309 routing_mode = RMODE_ROUTER;
310 else if(!strcasecmp(mode, "switch"))
311 routing_mode = RMODE_SWITCH;
312 else if(!strcasecmp(mode, "hub"))
313 routing_mode = RMODE_HUB;
315 logger(LOG_ERR, "Invalid routing mode!");
321 if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
322 if(!strcasecmp(mode, "off"))
323 forwarding_mode = FMODE_OFF;
324 else if(!strcasecmp(mode, "internal"))
325 forwarding_mode = FMODE_INTERNAL;
326 else if(!strcasecmp(mode, "kernel"))
327 forwarding_mode = FMODE_KERNEL;
329 logger(LOG_ERR, "Invalid forwarding mode!");
336 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
338 myself->options |= OPTION_PMTU_DISCOVERY;
341 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
343 myself->options |= OPTION_CLAMP_MSS;
345 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
347 #if !defined(SOL_IP) || !defined(IP_TOS)
348 if(priorityinheritance)
349 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
352 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
355 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
356 if(maxtimeout <= 0) {
357 logger(LOG_ERR, "Bogus maximum timeout!");
363 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
364 if(udp_rcvbuf <= 0) {
365 logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
370 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
371 if(udp_sndbuf <= 0) {
372 logger(LOG_ERR, "UDPSndBuf cannot be negative!");
377 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
378 if(replaywin_int < 0) {
379 logger(LOG_ERR, "ReplayWindow cannot be negative!");
382 replaywin = (unsigned)replaywin_int;
385 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
386 if(!strcasecmp(afname, "IPv4"))
387 addressfamily = AF_INET;
388 else if(!strcasecmp(afname, "IPv6"))
389 addressfamily = AF_INET6;
390 else if(!strcasecmp(afname, "any"))
391 addressfamily = AF_UNSPEC;
393 logger(LOG_ERR, "Invalid address family!");
399 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
401 /* Generate packet encryption key */
403 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
404 cipher = xstrdup("blowfish");
406 if(!cipher_open_by_name(&myself->incipher, cipher)) {
407 logger(LOG_ERR, "Unrecognized cipher type!");
411 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
416 /* Check if we want to use message authentication codes... */
418 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
419 digest = xstrdup("sha1");
422 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
425 logger(LOG_ERR, "Bogus MAC length!");
429 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
430 logger(LOG_ERR, "Unrecognized digest type!");
436 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
437 if(myself->incompression < 0 || myself->incompression > 11) {
438 logger(LOG_ERR, "Bogus compression level!");
442 myself->incompression = 0;
444 myself->connection->outcompression = 0;
448 myself->nexthop = myself;
449 myself->via = myself;
450 myself->status.reachable = true;
464 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
466 if (event_add(&device_ev, NULL) < 0) {
467 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
473 /* Run tinc-up script to further initialize the tap interface */
474 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
475 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
476 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
477 xasprintf(&envp[3], "NAME=%s", myself->name);
480 execute_script("tinc-up", envp);
482 for(i = 0; i < 4; i++)
485 /* Run subnet-up scripts for our own subnets */
487 subnet_update(myself, NULL, true);
491 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
493 hint.ai_family = addressfamily;
494 hint.ai_socktype = SOCK_STREAM;
495 hint.ai_protocol = IPPROTO_TCP;
496 hint.ai_flags = AI_PASSIVE;
498 err = getaddrinfo(address, myport, &hint, &ai);
501 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
508 for(aip = ai; aip; aip = aip->ai_next) {
509 listen_socket[listen_sockets].tcp =
510 setup_listen_socket((sockaddr_t *) aip->ai_addr);
512 if(listen_socket[listen_sockets].tcp < 0)
515 listen_socket[listen_sockets].udp =
516 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
518 if(listen_socket[listen_sockets].udp < 0) {
519 close(listen_socket[listen_sockets].tcp);
523 event_set(&listen_socket[listen_sockets].ev_tcp,
524 listen_socket[listen_sockets].tcp,
526 handle_new_meta_connection, NULL);
527 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
528 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
532 if(!thread_create(&listen_socket[listen_sockets].udp_thread, handle_incoming_vpn_data, &listen_socket[listen_sockets])) {
533 logger(LOG_ERR, "thread_create failed: %s", strerror(errno));
537 ifdebug(CONNECTIONS) {
538 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
539 logger(LOG_NOTICE, "Listening on %s", hostname);
543 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
546 if(listen_sockets >= MAXSOCKETS) {
547 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
555 logger(LOG_NOTICE, "Ready");
557 logger(LOG_ERR, "Unable to create any listening socket!");
567 bool setup_network(void) {
574 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
575 if(pinginterval < 1) {
576 pinginterval = 86400;
581 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
583 if(pingtimeout < 1 || pingtimeout > pinginterval)
584 pingtimeout = pinginterval;
586 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
587 maxoutbufsize = 10 * MTU;
596 close all open network connections
598 void close_network_connections(void) {
599 splay_node_t *node, *next;
604 for(node = connection_tree->head; node; node = next) {
608 terminate_connection(c, false);
611 list_delete_list(outgoing_list);
613 if(myself && myself->connection) {
614 subnet_update(myself, NULL, false);
615 terminate_connection(myself->connection, false);
616 free_connection(myself->connection);
619 for(i = 0; i < listen_sockets; i++) {
620 event_del(&listen_socket[i].ev_tcp);
621 event_del(&listen_socket[i].ev_udp);
622 close(listen_socket[i].tcp);
623 close(listen_socket[i].udp);
624 thread_destroy(&listen_socket[i].udp_thread);
627 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
628 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
629 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
630 xasprintf(&envp[3], "NAME=%s", myself->name);
639 execute_script("tinc-down", envp);
641 if(myport) free(myport);
643 for(i = 0; i < 4; i++)