3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 Guus Sliepen <guus@tinc-vpn.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.
25 #include "splay_tree.h"
28 #include "connection.h"
45 static struct event device_ev;
47 bool read_rsa_public_key(connection_t *c) {
55 /* First, check for simple PublicKey statement */
57 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
58 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
63 /* Else, check for PublicKeyFile statement and read it */
65 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
66 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
68 fp = fopen(fname, "r");
71 logger(LOG_ERR, _("Error reading RSA public key file `%s': %s"),
72 fname, strerror(errno));
77 result = rsa_read_pem_public_key(&c->rsa, fp);
81 logger(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"), fname, strerror(errno));
86 bool read_rsa_private_key() {
94 /* First, check for simple PrivateKey statement */
96 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
97 if(!get_config_string(lookup_config(myself->connection->config_tree, "PublicKey"), &n)) {
98 logger(LOG_ERR, _("PrivateKey used but no PublicKey found!"));
102 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
108 /* Else, check for PrivateKeyFile statement and read it */
110 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
111 xasprintf(&fname, "%s/rsa_key.priv", confbase);
113 fp = fopen(fname, "r");
116 logger(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
117 fname, strerror(errno));
122 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
125 if(fstat(fileno(fp), &s)) {
126 logger(LOG_ERR, _("Could not stat RSA private key file `%s': %s'"), fname, strerror(errno));
131 if(s.st_mode & ~0100700)
132 logger(LOG_WARNING, _("Warning: insecure file permissions for RSA private key file `%s'!"), fname);
135 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
139 logger(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"), fname, strerror(errno));
144 static struct event keyexpire_event;
146 static void keyexpire_handler(int fd, short events, void *data) {
150 void regenerate_key() {
151 if(timeout_initialized(&keyexpire_event)) {
152 ifdebug(STATUS) logger(LOG_INFO, _("Expiring symmetric keys"));
153 event_del(&keyexpire_event);
154 send_key_changed(broadcast, myself);
156 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
159 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
163 Configure node_t myself and set up the local sockets (listen only)
165 bool setup_myself(void) {
168 char *name, *hostname, *mode, *afname, *cipher, *digest;
169 char *address = NULL;
171 struct addrinfo *ai, *aip, hint = {0};
178 myself->connection = new_connection();
179 init_configuration(&myself->connection->config_tree);
181 xasprintf(&myself->hostname, _("MYSELF"));
182 xasprintf(&myself->connection->hostname, _("MYSELF"));
184 myself->connection->options = 0;
185 myself->connection->protocol_version = PROT_CURRENT;
187 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
188 logger(LOG_ERR, _("Name for tinc daemon required!"));
192 if(!check_id(name)) {
193 logger(LOG_ERR, _("Invalid name for myself!"));
199 myself->connection->name = xstrdup(name);
201 if(!read_connection_config(myself->connection)) {
202 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
206 if(!read_rsa_private_key())
209 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
210 xasprintf(&myport, "655");
212 /* Read in all the subnets specified in the host configuration file */
214 cfg = lookup_config(myself->connection->config_tree, "Subnet");
217 if(!get_config_subnet(cfg, &subnet))
220 subnet_add(myself, subnet);
222 cfg = lookup_config_next(myself->connection->config_tree, cfg);
225 /* Check some options */
227 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
228 myself->options |= OPTION_INDIRECT;
230 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
231 myself->options |= OPTION_TCPONLY;
233 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
234 myself->options |= OPTION_INDIRECT;
236 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
237 myself->options |= OPTION_TCPONLY;
239 if(myself->options & OPTION_TCPONLY)
240 myself->options |= OPTION_INDIRECT;
242 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
244 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
245 if(!strcasecmp(mode, "router"))
246 routing_mode = RMODE_ROUTER;
247 else if(!strcasecmp(mode, "switch"))
248 routing_mode = RMODE_SWITCH;
249 else if(!strcasecmp(mode, "hub"))
250 routing_mode = RMODE_HUB;
252 logger(LOG_ERR, _("Invalid routing mode!"));
257 routing_mode = RMODE_ROUTER;
259 // Enable PMTUDiscovery by default if we are in router mode.
261 choice = routing_mode == RMODE_ROUTER;
262 get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice);
264 myself->options |= OPTION_PMTU_DISCOVERY;
266 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
268 #if !defined(SOL_IP) || !defined(IP_TOS)
269 if(priorityinheritance)
270 logger(LOG_WARNING, _("%s not supported on this platform"), "PriorityInheritance");
273 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
276 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
277 if(maxtimeout <= 0) {
278 logger(LOG_ERR, _("Bogus maximum timeout!"));
284 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
285 if(!strcasecmp(afname, "IPv4"))
286 addressfamily = AF_INET;
287 else if(!strcasecmp(afname, "IPv6"))
288 addressfamily = AF_INET6;
289 else if(!strcasecmp(afname, "any"))
290 addressfamily = AF_UNSPEC;
292 logger(LOG_ERR, _("Invalid address family!"));
298 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
300 /* Generate packet encryption key */
302 if(!get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
303 cipher = xstrdup("aes256");
305 if(!cipher_open_by_name(&myself->incipher, cipher)) {
306 logger(LOG_ERR, _("Unrecognized cipher type!"));
310 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
315 /* Check if we want to use message authentication codes... */
317 if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
318 digest = xstrdup("sha256");
321 get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &maclength);
324 logger(LOG_ERR, _("Bogus MAC length!"));
328 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
329 logger(LOG_ERR, _("Unrecognized digest type!"));
335 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->incompression)) {
336 if(myself->incompression < 0 || myself->incompression > 11) {
337 logger(LOG_ERR, _("Bogus compression level!"));
341 myself->incompression = 0;
343 myself->connection->outcompression = 0;
347 myself->nexthop = myself;
348 myself->via = myself;
349 myself->status.reachable = true;
359 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
361 if (event_add(&device_ev, NULL) < 0) {
362 logger(LOG_ERR, _("event_add failed: %s"), strerror(errno));
367 /* Run tinc-up script to further initialize the tap interface */
368 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
369 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
370 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
371 xasprintf(&envp[3], "NAME=%s", myself->name);
374 execute_script("tinc-up", envp);
376 for(i = 0; i < 5; i++)
379 /* Run subnet-up scripts for our own subnets */
381 subnet_update(myself, NULL, true);
385 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
387 hint.ai_family = addressfamily;
388 hint.ai_socktype = SOCK_STREAM;
389 hint.ai_protocol = IPPROTO_TCP;
390 hint.ai_flags = AI_PASSIVE;
392 err = getaddrinfo(address, myport, &hint, &ai);
395 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
402 for(aip = ai; aip; aip = aip->ai_next) {
403 listen_socket[listen_sockets].tcp =
404 setup_listen_socket((sockaddr_t *) aip->ai_addr);
406 if(listen_socket[listen_sockets].tcp < 0)
409 listen_socket[listen_sockets].udp =
410 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
412 if(listen_socket[listen_sockets].udp < 0) {
413 close(listen_socket[listen_sockets].tcp);
417 event_set(&listen_socket[listen_sockets].ev_tcp,
418 listen_socket[listen_sockets].tcp,
420 handle_new_meta_connection, NULL);
421 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
422 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
426 event_set(&listen_socket[listen_sockets].ev_udp,
427 listen_socket[listen_sockets].udp,
429 handle_incoming_vpn_data, NULL);
430 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
431 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
435 ifdebug(CONNECTIONS) {
436 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
437 logger(LOG_NOTICE, _("Listening on %s"), hostname);
441 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
444 if(listen_sockets >= MAXSOCKETS) {
445 logger(LOG_WARNING, _("Maximum of %d listening sockets reached"), MAXSOCKETS);
453 logger(LOG_NOTICE, _("Ready"));
455 logger(LOG_ERR, _("Unable to create any listening socket!"));
465 bool setup_network(void)
475 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
476 if(pinginterval < 1) {
477 pinginterval = 86400;
482 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
484 if(pingtimeout < 1 || pingtimeout > pinginterval)
485 pingtimeout = pinginterval;
487 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
488 maxoutbufsize = 10 * MTU;
497 close all open network connections
499 void close_network_connections(void) {
500 splay_node_t *node, *next;
507 for(node = connection_tree->head; node; node = next) {
511 terminate_connection(c, false);
514 list_delete_list(outgoing_list);
516 if(myself && myself->connection) {
517 subnet_update(myself, NULL, false);
518 terminate_connection(myself->connection, false);
519 free_connection(myself->connection);
522 for(i = 0; i < listen_sockets; i++) {
523 event_del(&listen_socket[i].ev_tcp);
524 event_del(&listen_socket[i].ev_udp);
525 close(listen_socket[i].tcp);
526 close(listen_socket[i].udp);
529 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
530 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
531 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
532 xasprintf(&envp[3], "NAME=%s", myself->name);
541 execute_script("tinc-down", envp);
543 if(myport) free(myport);
545 for(i = 0; i < 4; i++)