2 net.c -- most of the network code
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 <openssl/rand.h>
29 #include "connection.h"
43 bool do_purge = false;
44 volatile bool running = false;
47 int contradicting_add_edge = 0;
48 int contradicting_del_edge = 0;
50 /* Purge edges and subnets of unreachable nodes. Use carefully. */
52 static void purge(void) {
53 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
58 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
60 /* Remove all edges and subnets owned by unreachable nodes. */
62 for(nnode = node_tree->head; nnode; nnode = nnext) {
66 if(!n->status.reachable) {
67 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
70 for(snode = n->subnet_tree->head; snode; snode = snext) {
73 send_del_subnet(broadcast, s);
78 for(enode = n->edge_tree->head; enode; enode = enext) {
82 send_del_edge(broadcast, e);
88 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
90 for(nnode = node_tree->head; nnode; nnode = nnext) {
94 if(!n->status.reachable) {
95 for(enode = edge_weight_tree->head; enode; enode = enext) {
103 if(!enode && (!strictsubnets || !n->subnet_tree->head))
104 /* in strictsubnets mode do not delete nodes with subnets */
111 put all file descriptors in an fd_set array
112 While we're at it, purge stuff that needs to be removed.
114 static int build_fdset(fd_set *readset, fd_set *writeset) {
115 avl_node_t *node, *next;
122 for(node = connection_tree->head; node; node = next) {
126 if(c->status.remove) {
128 if(!connection_tree->head)
131 FD_SET(c->socket, readset);
133 FD_SET(c->socket, writeset);
139 for(i = 0; i < listen_sockets; i++) {
140 FD_SET(listen_socket[i].tcp, readset);
141 if(listen_socket[i].tcp > max)
142 max = listen_socket[i].tcp;
143 FD_SET(listen_socket[i].udp, readset);
144 if(listen_socket[i].udp > max)
145 max = listen_socket[i].udp;
149 FD_SET(device_fd, readset);
157 Terminate a connection:
159 - Remove associated edge and tell other connections about it if report = true
160 - Check if we need to retry making an outgoing connection
161 - Deactivate the host
163 void terminate_connection(connection_t *c, bool report) {
167 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
168 c->name, c->hostname);
170 c->status.remove = true;
171 c->status.active = false;
174 c->node->connection = NULL;
177 closesocket(c->socket);
180 if(report && !tunnelserver)
181 send_del_edge(broadcast, c->edge);
185 /* Run MST and SSSP algorithms */
189 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
191 if(report && !c->node->status.reachable) {
193 e = lookup_edge(c->node, myself);
196 send_del_edge(broadcast, e);
202 /* Check if this was our outgoing connection */
205 retry_outgoing(c->outgoing);
217 Check if the other end is active.
218 If we have sent packets, but didn't receive any,
219 then possibly the other end is dead. We send a
220 PING request over the meta connection. If the other
221 end does not reply in time, we consider them dead
222 and close the connection.
224 static void check_dead_connections(void) {
225 avl_node_t *node, *next;
228 for(node = connection_tree->head; node; node = next) {
232 if(c->last_ping_time + pingtimeout < now) {
233 if(c->status.active) {
234 if(c->status.pinged) {
235 ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
236 c->name, c->hostname, now - c->last_ping_time);
237 c->status.timeout = true;
238 terminate_connection(c, true);
239 } else if(c->last_ping_time + pinginterval < now) {
243 if(c->status.remove) {
244 logger(LOG_WARNING, "Old connection_t for %s (%s) status %04x still lingering, deleting...",
245 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
249 ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication",
250 c->name, c->hostname);
251 if(c->status.connecting) {
252 c->status.connecting = false;
253 closesocket(c->socket);
254 do_outgoing_connection(c);
256 terminate_connection(c, false);
261 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
262 if(c->status.active) {
263 ifdebug(CONNECTIONS) logger(LOG_INFO,
264 "%s (%s) could not flush for %ld seconds (%d bytes remaining)",
265 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
266 c->status.timeout = true;
267 terminate_connection(c, true);
274 check all connections to see if anything
275 happened on their sockets
277 static void check_network_activity(fd_set * readset, fd_set * writeset) {
281 socklen_t len = sizeof(result);
283 static int errors = 0;
285 /* check input from kernel */
286 if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
287 if(read_packet(&packet)) {
290 route(myself, &packet);
292 usleep(errors * 50000);
295 logger(LOG_ERR, "Too many errors from %s, exiting!", device);
301 /* check meta connections */
302 for(node = connection_tree->head; node; node = node->next) {
308 if(FD_ISSET(c->socket, readset)) {
309 if(c->status.connecting) {
310 c->status.connecting = false;
311 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
314 finish_connecting(c);
316 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
317 "Error while connecting to %s (%s): %s",
318 c->name, c->hostname, sockstrerror(result));
319 closesocket(c->socket);
320 do_outgoing_connection(c);
325 if(!receive_meta(c)) {
326 terminate_connection(c, c->status.active);
331 if(FD_ISSET(c->socket, writeset)) {
333 terminate_connection(c, c->status.active);
339 for(i = 0; i < listen_sockets; i++) {
340 if(FD_ISSET(listen_socket[i].udp, readset))
341 handle_incoming_vpn_data(listen_socket[i].udp);
343 if(FD_ISSET(listen_socket[i].tcp, readset))
344 handle_new_meta_connection(listen_socket[i].tcp);
349 this is where it all happens...
351 int main_loop(void) {
352 fd_set readset, writeset;
355 time_t last_ping_check, last_config_check, last_graph_dump;
358 last_ping_check = now;
359 last_config_check = now;
360 last_graph_dump = now;
369 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
373 maxfd = build_fdset(&readset, &writeset);
376 LeaveCriticalSection(&mutex);
378 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
380 EnterCriticalSection(&mutex);
384 if(!sockwouldblock(sockerrno)) {
385 logger(LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno));
392 check_network_activity(&readset, &writeset);
399 /* Let's check if everybody is still alive */
401 if(last_ping_check + pingtimeout < now) {
402 check_dead_connections();
403 last_ping_check = now;
405 if(routing_mode == RMODE_SWITCH)
410 /* Should we regenerate our key? */
412 if(keyexpires < now) {
416 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
418 for(node = node_tree->head; node; node = node->next) {
426 send_key_changed(broadcast, myself);
427 keyexpires = now + keylifetime;
430 if(contradicting_del_edge > 10 && contradicting_add_edge > 10) {
431 logger(LOG_WARNING, "Possible node with same Name as us!");
433 if(rand() % 3 == 0) {
434 logger(LOG_ERR, "Shutting down, check configuration of all nodes for duplicate Names!");
439 contradicting_add_edge = 0;
440 contradicting_del_edge = 0;
446 logger(LOG_INFO, "Flushing event queue");
448 for(node = connection_tree->head; node; node = node->next) {
449 connection_t *c = node->data;
455 while((event = get_expired_event())) {
456 event->handler(event->data);
462 avl_node_t *node, *next;
468 /* Reread our own configuration file */
470 exit_configuration(&config_tree);
471 init_configuration(&config_tree);
473 if(!read_server_config()) {
474 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
478 /* Cancel non-active outgoing connections */
480 for(node = connection_tree->head; node; node = next) {
486 if(c->status.connecting) {
487 terminate_connection(c, false);
492 /* Wipe list of outgoing connections */
494 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
495 outgoing_t *outgoing = node->data;
498 event_del(outgoing->event);
501 list_delete_list(outgoing_list);
503 /* Close connections to hosts that have a changed or deleted host config file */
505 for(node = connection_tree->head; node; node = node->next) {
508 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
509 if(stat(fname, &s) || s.st_mtime > last_config_check)
510 terminate_connection(c, c->status.active);
514 last_config_check = now;
516 /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
521 for(node = subnet_tree->head; node; node = node->next) {
528 for(node = subnet_tree->head; node; node = next) {
531 if(subnet->expires == 1) {
532 send_del_subnet(broadcast, subnet);
533 if(subnet->owner->status.reachable)
534 subnet_update(subnet->owner, subnet, false);
535 subnet_del(subnet->owner, subnet);
536 } else if(subnet->expires == -1) {
539 send_add_subnet(broadcast, subnet);
540 if(subnet->owner->status.reachable)
541 subnet_update(subnet->owner, subnet, true);
546 /* Try to make outgoing connections */
548 try_outgoing_connections();
551 /* Dump graph if wanted every 60 seconds*/
553 if(last_graph_dump + 60 < now) {
555 last_graph_dump = now;