3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 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 Configure node_t myself and set up the local sockets (listen only)
160 bool setup_myself(void) {
163 char *name, *hostname, *mode, *afname, *cipher, *digest;
164 char *address = NULL;
166 struct addrinfo *ai, *aip, hint = {0};
171 myself->connection = new_connection();
172 init_configuration(&myself->connection->config_tree);
174 xasprintf(&myself->hostname, "MYSELF");
175 xasprintf(&myself->connection->hostname, "MYSELF");
177 myself->connection->options = 0;
178 myself->connection->protocol_version = PROT_CURRENT;
180 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
181 logger(LOG_ERR, "Name for tinc daemon required!");
185 if(!check_id(name)) {
186 logger(LOG_ERR, "Invalid name for myself!");
192 myself->connection->name = xstrdup(name);
194 if(!read_connection_config(myself->connection)) {
195 logger(LOG_ERR, "Cannot open host configuration file for myself!");
199 if(!read_rsa_private_key())
202 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
203 xasprintf(&myport, "655");
205 /* Read in all the subnets specified in the host configuration file */
207 cfg = lookup_config(myself->connection->config_tree, "Subnet");
210 if(!get_config_subnet(cfg, &subnet))
213 subnet_add(myself, subnet);
215 cfg = lookup_config_next(myself->connection->config_tree, cfg);
218 /* Check some options */
220 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
221 myself->options |= OPTION_INDIRECT;
223 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
224 myself->options |= OPTION_TCPONLY;
226 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
227 myself->options |= OPTION_INDIRECT;
229 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
230 myself->options |= OPTION_TCPONLY;
232 if(myself->options & OPTION_TCPONLY)
233 myself->options |= OPTION_INDIRECT;
235 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
237 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
238 if(!strcasecmp(mode, "router"))
239 routing_mode = RMODE_ROUTER;
240 else if(!strcasecmp(mode, "switch"))
241 routing_mode = RMODE_SWITCH;
242 else if(!strcasecmp(mode, "hub"))
243 routing_mode = RMODE_HUB;
245 logger(LOG_ERR, "Invalid routing mode!");
250 routing_mode = RMODE_ROUTER;
252 // Enable PMTUDiscovery by default if we are in router mode.
254 choice = routing_mode == RMODE_ROUTER;
255 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
257 myself->options |= OPTION_PMTU_DISCOVERY;
259 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
261 #if !defined(SOL_IP) || !defined(IP_TOS)
262 if(priorityinheritance)
263 logger(LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
266 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
269 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
270 if(maxtimeout <= 0) {
271 logger(LOG_ERR, "Bogus maximum timeout!");
277 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
278 if(!strcasecmp(afname, "IPv4"))
279 addressfamily = AF_INET;
280 else if(!strcasecmp(afname, "IPv6"))
281 addressfamily = AF_INET6;
282 else if(!strcasecmp(afname, "any"))
283 addressfamily = AF_UNSPEC;
285 logger(LOG_ERR, "Invalid address family!");
291 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
293 /* Generate packet encryption key */
295 if(!get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
296 cipher = xstrdup("aes256");
298 if(!cipher_open_by_name(&myself->incipher, cipher)) {
299 logger(LOG_ERR, "Unrecognized cipher type!");
303 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
308 /* Check if we want to use message authentication codes... */
310 if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
311 digest = xstrdup("sha256");
314 get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &maclength);
317 logger(LOG_ERR, "Bogus MAC length!");
321 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
322 logger(LOG_ERR, "Unrecognized digest type!");
328 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
329 if(myself->incompression < 0 || myself->incompression > 11) {
330 logger(LOG_ERR, "Bogus compression level!");
334 myself->incompression = 0;
336 myself->connection->outcompression = 0;
340 myself->nexthop = myself;
341 myself->via = myself;
342 myself->status.reachable = true;
352 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
354 if (event_add(&device_ev, NULL) < 0) {
355 logger(LOG_ERR, "event_add failed: %s", strerror(errno));
360 /* Run tinc-up script to further initialize the tap interface */
361 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
362 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
363 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
364 xasprintf(&envp[3], "NAME=%s", myself->name);
367 execute_script("tinc-up", envp);
369 for(i = 0; i < 5; i++)
372 /* Run subnet-up scripts for our own subnets */
374 subnet_update(myself, NULL, true);
378 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
380 hint.ai_family = addressfamily;
381 hint.ai_socktype = SOCK_STREAM;
382 hint.ai_protocol = IPPROTO_TCP;
383 hint.ai_flags = AI_PASSIVE;
385 err = getaddrinfo(address, myport, &hint, &ai);
388 logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
395 for(aip = ai; aip; aip = aip->ai_next) {
396 listen_socket[listen_sockets].tcp =
397 setup_listen_socket((sockaddr_t *) aip->ai_addr);
399 if(listen_socket[listen_sockets].tcp < 0)
402 listen_socket[listen_sockets].udp =
403 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
405 if(listen_socket[listen_sockets].udp < 0) {
406 close(listen_socket[listen_sockets].tcp);
410 event_set(&listen_socket[listen_sockets].ev_tcp,
411 listen_socket[listen_sockets].tcp,
413 handle_new_meta_connection, NULL);
414 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
415 logger(LOG_EMERG, "event_add failed: %s", strerror(errno));
419 event_set(&listen_socket[listen_sockets].ev_udp,
420 listen_socket[listen_sockets].udp,
422 handle_incoming_vpn_data, NULL);
423 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
424 logger(LOG_EMERG, "event_add failed: %s", strerror(errno));
428 ifdebug(CONNECTIONS) {
429 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
430 logger(LOG_NOTICE, "Listening on %s", hostname);
434 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
437 if(listen_sockets >= MAXSOCKETS) {
438 logger(LOG_WARNING, "Maximum of %d listening sockets reached", MAXSOCKETS);
446 logger(LOG_NOTICE, "Ready");
448 logger(LOG_ERR, "Unable to create any listening socket!");
458 bool setup_network(void) {
465 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
466 if(pinginterval < 1) {
467 pinginterval = 86400;
472 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
474 if(pingtimeout < 1 || pingtimeout > pinginterval)
475 pingtimeout = pinginterval;
477 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
478 maxoutbufsize = 10 * MTU;
487 close all open network connections
489 void close_network_connections(void) {
490 splay_node_t *node, *next;
495 for(node = connection_tree->head; node; node = next) {
499 terminate_connection(c, false);
502 list_delete_list(outgoing_list);
504 if(myself && myself->connection) {
505 subnet_update(myself, NULL, false);
506 terminate_connection(myself->connection, false);
507 free_connection(myself->connection);
510 for(i = 0; i < listen_sockets; i++) {
511 event_del(&listen_socket[i].ev_tcp);
512 event_del(&listen_socket[i].ev_udp);
513 close(listen_socket[i].tcp);
514 close(listen_socket[i].udp);
517 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
518 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
519 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
520 xasprintf(&envp[3], "NAME=%s", myself->name);
529 execute_script("tinc-down", envp);
531 if(myport) free(myport);
533 for(i = 0; i < 4; i++)