2 net.c -- most of the network code
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: net.c,v 1.35.4.175 2002/09/03 20:43:25 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IN_SYSTM_H
30 #include <netinet/in_systm.h>
32 #ifdef HAVE_NETINET_IP_H
33 #include <netinet/ip.h>
35 #ifdef HAVE_NETINET_TCP_H
36 #include <netinet/tcp.h>
43 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 /* SunOS really wants sys/socket.h BEFORE net/if.h,
48 and FreeBSD wants these lines below the rest. */
49 #include <arpa/inet.h>
50 #include <sys/socket.h>
53 #include <openssl/rand.h>
61 #include "connection.h"
75 #ifndef HAVE_RAND_PSEUDO_BYTES
76 #define RAND_pseudo_bytes RAND_bytes
85 /* Purge subnets of unreachable nodes. Use carefully. */
89 avl_node_t *nnode, *nnext, *snode, *snext, *cnode;
94 if(debug_lvl >= DEBUG_PROTOCOL)
95 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
97 for(nnode = node_tree->head; nnode; nnode = nnext)
100 n = (node_t *)nnode->data;
102 if(!n->status.reachable)
104 if(debug_lvl >= DEBUG_SCARY_THINGS)
105 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
107 for(snode = n->subnet_tree->head; snode; snode = snext)
110 s = (subnet_t *)snode->data;
112 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
114 c = (connection_t *)cnode->data;
116 send_del_subnet(c, s);
129 put all file descriptors in an fd_set array
130 While we're at it, purge stuff that needs to be removed.
132 void build_fdset(fd_set *fs)
134 avl_node_t *node, *next;
140 for(node = connection_tree->head; node; node = next)
143 c = (connection_t *)node->data;
148 if(!connection_tree->head)
152 FD_SET(c->socket, fs);
155 for(i = 0; i < listen_sockets; i++)
157 FD_SET(listen_socket[i].tcp, fs);
158 FD_SET(listen_socket[i].udp, fs);
161 FD_SET(device_fd, fs);
166 Terminate a connection:
168 - Tell other connections about it if report = 1
169 - Check if we need to retry making an outgoing connection
170 - Deactivate the host
172 void terminate_connection(connection_t *c, int report)
174 avl_node_t *node, *node2;
181 if(debug_lvl >= DEBUG_CONNECTIONS)
182 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
183 c->name, c->hostname);
185 c->status.remove = 1;
186 c->status.active = 0;
190 if(report && c->node->connection)
192 for(node = connection_tree->head; node; node = node->next)
194 other = (connection_t *)node->data;
197 for(node2 = node_tree->head; node2; node2 = node2->next)
199 n = (node_t *)node2->data;
200 if(n->nexthop == c->node)
202 send_del_node(other, n);
203 n->status.reachable = 0;
208 c->node->connection = NULL;
214 /* Check if this was our outgoing connection */
218 retry_outgoing(c->outgoing);
225 Check if the other end is active.
226 If we have sent packets, but didn't receive any,
227 then possibly the other end is dead. We send a
228 PING request over the meta connection. If the other
229 end does not reply in time, we consider them dead
230 and close the connection.
232 void check_dead_connections(void)
237 for(node = connection_tree->head; node; node = node->next)
239 c = (connection_t *)node->data;
240 if(c->last_ping_time + pingtimeout < now && !c->status.remove)
246 if(debug_lvl >= DEBUG_PROTOCOL)
247 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
248 c->name, c->hostname);
249 c->status.timeout = 1;
250 terminate_connection(c, 1);
259 if(debug_lvl >= DEBUG_CONNECTIONS)
260 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
261 c->name, c->hostname);
262 terminate_connection(c, 0);
270 check all connections to see if anything
271 happened on their sockets
273 void check_network_activity(fd_set *f)
278 int len = sizeof(result);
281 if(FD_ISSET(device_fd, f))
283 if(!read_packet(&packet))
284 route_outgoing(&packet);
287 for(node = connection_tree->head; node; node = node->next)
289 c = (connection_t *)node->data;
294 if(FD_ISSET(c->socket, f))
296 if(c->status.connecting)
298 c->status.connecting = 0;
299 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
301 finish_connecting(c);
304 if(debug_lvl >= DEBUG_CONNECTIONS)
305 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
307 do_outgoing_connection(c);
311 if(receive_meta(c) < 0)
313 terminate_connection(c, c->status.active);
319 for(i = 0; i < listen_sockets; i++)
321 if(FD_ISSET(listen_socket[i].udp, f))
322 handle_incoming_vpn_data(listen_socket[i].udp);
323 if(FD_ISSET(listen_socket[i].tcp, f))
324 handle_new_meta_connection(listen_socket[i].tcp);
330 this is where it all happens...
337 time_t last_ping_check;
340 last_ping_check = now;
348 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
353 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
355 if(errno != EINTR && errno != EAGAIN)
357 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
366 check_network_activity(&fset);
374 /* Let's check if everybody is still alive */
376 if(last_ping_check + pingtimeout < now)
378 check_dead_connections();
379 last_ping_check = now;
381 if(routing_mode== RMODE_SWITCH)
386 /* Should we regenerate our key? */
390 if(debug_lvl >= DEBUG_STATUS)
391 syslog(LOG_INFO, _("Regenerating symmetric key"));
393 RAND_pseudo_bytes(myself->key, myself->keylength);
394 send_key_changed(myself->connection, myself);
395 keyexpires = now + keylifetime;
400 while((event = get_expired_event()))
402 event->handler(event->data);
408 syslog(LOG_INFO, _("Flushing event queue"));
410 while(event_tree->head)
412 event = (event_t *)event_tree->head->data;
413 event->handler(event->data);
422 close_network_connections();
423 exit_configuration(&config_tree);
425 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
428 init_configuration(&config_tree);
430 if(read_server_config())
432 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
436 if(setup_network_connections())