3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2006 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 asprintf(&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 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
153 if(!cipher_regenerate_key(&myself->cipher, true)) {
154 logger(LOG_ERR, _("Error regenerating key!"));
158 if(timeout_initialized(&keyexpire_event)) {
159 event_del(&keyexpire_event);
160 send_key_changed(broadcast, myself);
162 timeout_set(&keyexpire_event, keyexpire_handler, NULL);
165 event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
169 Configure node_t myself and set up the local sockets (listen only)
171 bool setup_myself(void) {
174 char *name, *hostname, *mode, *afname, *cipher, *digest;
175 char *address = NULL;
177 struct addrinfo *ai, *aip, hint = {0};
184 myself->connection = new_connection();
185 init_configuration(&myself->connection->config_tree);
187 asprintf(&myself->hostname, _("MYSELF"));
188 asprintf(&myself->connection->hostname, _("MYSELF"));
190 myself->connection->options = 0;
191 myself->connection->protocol_version = PROT_CURRENT;
193 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) { /* Not acceptable */
194 logger(LOG_ERR, _("Name for tinc daemon required!"));
198 if(!check_id(name)) {
199 logger(LOG_ERR, _("Invalid name for myself!"));
205 myself->connection->name = xstrdup(name);
207 if(!read_connection_config(myself->connection)) {
208 logger(LOG_ERR, _("Cannot open host configuration file for myself!"));
212 if(!read_rsa_private_key())
215 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
216 asprintf(&myport, "655");
218 /* Read in all the subnets specified in the host configuration file */
220 cfg = lookup_config(myself->connection->config_tree, "Subnet");
223 if(!get_config_subnet(cfg, &subnet))
226 subnet_add(myself, subnet);
228 cfg = lookup_config_next(myself->connection->config_tree, cfg);
231 /* Check some options */
233 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
234 myself->options |= OPTION_INDIRECT;
236 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
237 myself->options |= OPTION_TCPONLY;
239 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice) && choice)
240 myself->options |= OPTION_INDIRECT;
242 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice) && choice)
243 myself->options |= OPTION_TCPONLY;
245 if(get_config_bool(lookup_config(myself->connection->config_tree, "PMTUDiscovery"), &choice) && choice)
246 myself->options |= OPTION_PMTU_DISCOVERY;
248 if(myself->options & OPTION_TCPONLY)
249 myself->options |= OPTION_INDIRECT;
251 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
253 if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
254 if(!strcasecmp(mode, "router"))
255 routing_mode = RMODE_ROUTER;
256 else if(!strcasecmp(mode, "switch"))
257 routing_mode = RMODE_SWITCH;
258 else if(!strcasecmp(mode, "hub"))
259 routing_mode = RMODE_HUB;
261 logger(LOG_ERR, _("Invalid routing mode!"));
266 routing_mode = RMODE_ROUTER;
268 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
270 #if !defined(SOL_IP) || !defined(IP_TOS)
271 if(priorityinheritance)
272 logger(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
275 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
278 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
279 if(maxtimeout <= 0) {
280 logger(LOG_ERR, _("Bogus maximum timeout!"));
286 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
287 if(!strcasecmp(afname, "IPv4"))
288 addressfamily = AF_INET;
289 else if(!strcasecmp(afname, "IPv6"))
290 addressfamily = AF_INET6;
291 else if(!strcasecmp(afname, "any"))
292 addressfamily = AF_UNSPEC;
294 logger(LOG_ERR, _("Invalid address family!"));
300 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
302 /* Generate packet encryption key */
304 if(!get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
305 cipher = xstrdup("blowfish");
307 if(!cipher_open_by_name(&myself->cipher, cipher)) {
308 logger(LOG_ERR, _("Unrecognized cipher type!"));
312 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
317 /* Check if we want to use message authentication codes... */
319 if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
320 digest = xstrdup("sha1");
322 if(!digest_open_by_name(&myself->digest, digest)) {
323 logger(LOG_ERR, _("Unrecognized digest type!"));
327 if(!get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
329 if(digest_active(&myself->digest)) {
330 if(myself->maclength > digest_length(&myself->digest)) {
331 logger(LOG_ERR, _("MAC length exceeds size of digest!"));
333 } else if(myself->maclength < 0) {
334 logger(LOG_ERR, _("Bogus MAC length!"));
341 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression)) {
342 if(myself->compression < 0 || myself->compression > 11) {
343 logger(LOG_ERR, _("Bogus compression level!"));
347 myself->compression = 0;
349 myself->connection->outcompression = 0;
353 myself->nexthop = myself;
354 myself->via = myself;
355 myself->status.reachable = true;
365 event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
367 if (event_add(&device_ev, NULL) < 0) {
368 logger(LOG_ERR, _("event_add failed: %s"), strerror(errno));
373 /* Run tinc-up script to further initialize the tap interface */
374 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
375 asprintf(&envp[1], "DEVICE=%s", device ? : "");
376 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
377 asprintf(&envp[3], "NAME=%s", myself->name);
380 execute_script("tinc-up", envp);
382 for(i = 0; i < 5; i++)
385 /* Run subnet-up scripts for our own subnets */
387 subnet_update(myself, NULL, true);
391 get_config_string(lookup_config(config_tree, "BindToAddress"), &address);
393 hint.ai_family = addressfamily;
394 hint.ai_socktype = SOCK_STREAM;
395 hint.ai_protocol = IPPROTO_TCP;
396 hint.ai_flags = AI_PASSIVE;
398 err = getaddrinfo(address, myport, &hint, &ai);
401 logger(LOG_ERR, _("System call `%s' failed: %s"), "getaddrinfo",
408 for(aip = ai; aip; aip = aip->ai_next) {
409 listen_socket[listen_sockets].tcp =
410 setup_listen_socket((sockaddr_t *) aip->ai_addr);
412 if(listen_socket[listen_sockets].tcp < 0)
415 listen_socket[listen_sockets].udp =
416 setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
418 if(listen_socket[listen_sockets].udp < 0) {
419 close(listen_socket[listen_sockets].tcp);
423 event_set(&listen_socket[listen_sockets].ev_tcp,
424 listen_socket[listen_sockets].tcp,
426 handle_new_meta_connection, NULL);
427 if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
428 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
432 event_set(&listen_socket[listen_sockets].ev_udp,
433 listen_socket[listen_sockets].udp,
435 handle_incoming_vpn_data, NULL);
436 if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
437 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
441 ifdebug(CONNECTIONS) {
442 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
443 logger(LOG_NOTICE, _("Listening on %s"), hostname);
447 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
450 if(listen_sockets >= MAXSOCKETS) {
451 logger(LOG_WARNING, _("Maximum of %d listening sockets reached"), MAXSOCKETS);
459 logger(LOG_NOTICE, _("Ready"));
461 logger(LOG_ERR, _("Unable to create any listening socket!"));
469 setup all initial network connections
471 bool setup_network_connections(void) {
480 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
481 if(pinginterval < 1) {
482 pinginterval = 86400;
487 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
489 if(pingtimeout < 1 || pingtimeout > pinginterval)
490 pingtimeout = pinginterval;
492 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
493 maxoutbufsize = 4 * MTU;
498 try_outgoing_connections();
504 close all open network connections
506 void close_network_connections(void) {
507 splay_node_t *node, *next;
514 for(node = connection_tree->head; node; node = next) {
520 freeaddrinfo(c->outgoing->ai);
521 free(c->outgoing->name);
526 terminate_connection(c, false);
529 if(myself && myself->connection) {
530 subnet_update(myself, NULL, false);
531 terminate_connection(myself->connection, false);
534 for(i = 0; i < listen_sockets; i++) {
535 event_del(&listen_socket[i].ev_tcp);
536 event_del(&listen_socket[i].ev_udp);
537 close(listen_socket[i].tcp);
538 close(listen_socket[i].udp);
541 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
542 asprintf(&envp[1], "DEVICE=%s", device ? : "");
543 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
544 asprintf(&envp[3], "NAME=%s", myself->name);
553 execute_script("tinc-down", envp);
555 for(i = 0; i < 4; i++)