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.182 2002/09/15 14:55:53 guus Exp $
33 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 /* SunOS really wants sys/socket.h BEFORE net/if.h,
38 and FreeBSD wants these lines below the rest. */
39 #include <arpa/inet.h>
40 #include <sys/socket.h>
42 #ifdef HAVE_NETINET_IN_SYSTM_H
43 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #ifdef HAVE_NETINET_IP_H
47 #include <netinet/ip.h>
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
53 #include <openssl/rand.h>
61 #include "connection.h"
76 #ifndef HAVE_RAND_PSEUDO_BYTES
77 #define RAND_pseudo_bytes RAND_bytes
86 /* Purge edges and subnets of unreachable nodes. Use carefully. */
90 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
97 if(debug_lvl >= DEBUG_PROTOCOL)
98 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
100 for(nnode = node_tree->head; nnode; nnode = nnext) {
102 n = (node_t *) nnode->data;
104 if(!n->status.reachable) {
105 if(debug_lvl >= DEBUG_SCARY_THINGS)
106 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
109 for(snode = n->subnet_tree->head; snode; snode = snext) {
111 s = (subnet_t *) snode->data;
112 send_del_subnet(broadcast, s);
116 for(enode = n->edge_tree->head; enode; enode = enext) {
118 e = (edge_t *) enode->data;
119 send_del_edge(broadcast, e);
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;
142 for(node = connection_tree->head; node; node = next) {
144 c = (connection_t *) node->data;
146 if(c->status.remove) {
148 if(!connection_tree->head)
151 FD_SET(c->socket, fs);
154 for(i = 0; i < listen_sockets; i++) {
155 FD_SET(listen_socket[i].tcp, fs);
156 FD_SET(listen_socket[i].udp, fs);
159 FD_SET(device_fd, fs);
163 Terminate a connection:
165 - Remove associated edge and tell other connections about it if report = 1
166 - Check if we need to retry making an outgoing connection
167 - Deactivate the host
169 void terminate_connection(connection_t *c, int report)
176 if(debug_lvl >= DEBUG_CONNECTIONS)
177 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
178 c->name, c->hostname);
180 c->status.remove = 1;
181 c->status.active = 0;
184 c->node->connection = NULL;
191 send_del_edge(broadcast, c->edge);
195 /* Run MST and SSSP algorithms */
200 /* Check if this was our outgoing connection */
203 retry_outgoing(c->outgoing);
209 Check if the other end is active.
210 If we have sent packets, but didn't receive any,
211 then possibly the other end is dead. We send a
212 PING request over the meta connection. If the other
213 end does not reply in time, we consider them dead
214 and close the connection.
216 void check_dead_connections(void)
218 avl_node_t *node, *next;
223 for(node = connection_tree->head; node; node = next) {
225 c = (connection_t *) node->data;
227 if(c->last_ping_time + pingtimeout < now) {
228 if(c->status.active) {
229 if(c->status.pinged) {
230 if(debug_lvl >= DEBUG_PROTOCOL)
231 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
232 c->name, c->hostname);
233 c->status.timeout = 1;
234 terminate_connection(c, 1);
239 if(c->status.remove) {
240 syslog(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
241 c->name, c->hostname, c->status);
245 if(debug_lvl >= DEBUG_CONNECTIONS)
246 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
247 c->name, c->hostname);
248 terminate_connection(c, 0);
255 check all connections to see if anything
256 happened on their sockets
258 void check_network_activity(fd_set * f)
263 int len = sizeof(result);
268 if(FD_ISSET(device_fd, f)) {
269 if(!read_packet(&packet))
270 route_outgoing(&packet);
273 for(node = connection_tree->head; node; node = node->next) {
274 c = (connection_t *) node->data;
279 if(FD_ISSET(c->socket, f)) {
280 if(c->status.connecting) {
281 c->status.connecting = 0;
282 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
285 finish_connecting(c);
287 if(debug_lvl >= DEBUG_CONNECTIONS)
289 _("Error while connecting to %s (%s): %s"),
290 c->name, c->hostname, strerror(result));
292 do_outgoing_connection(c);
297 if(receive_meta(c) < 0) {
298 terminate_connection(c, c->status.active);
304 for(i = 0; i < listen_sockets; i++) {
305 if(FD_ISSET(listen_socket[i].udp, f))
306 handle_incoming_vpn_data(listen_socket[i].udp);
308 if(FD_ISSET(listen_socket[i].tcp, f))
309 handle_new_meta_connection(listen_socket[i].tcp);
314 this is where it all happens...
321 time_t last_ping_check;
326 last_ping_check = now;
332 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
337 r = select(FD_SETSIZE, &fset, NULL, NULL, &tv);
340 if(errno != EINTR && errno != EAGAIN) {
341 syslog(LOG_ERR, _("Error while waiting for input: %s"),
351 check_network_activity(&fset);
358 /* Let's check if everybody is still alive */
360 if(last_ping_check + pingtimeout < now) {
361 check_dead_connections();
362 last_ping_check = now;
364 if(routing_mode == RMODE_SWITCH)
369 /* Should we regenerate our key? */
371 if(keyexpires < now) {
372 if(debug_lvl >= DEBUG_STATUS)
373 syslog(LOG_INFO, _("Regenerating symmetric key"));
375 RAND_pseudo_bytes(myself->key, myself->keylength);
376 send_key_changed(broadcast, myself);
377 keyexpires = now + keylifetime;
382 while((event = get_expired_event())) {
383 event->handler(event->data);
388 syslog(LOG_INFO, _("Flushing event queue"));
390 while(event_tree->head) {
391 event = (event_t *) event_tree->head->data;
392 event->handler(event->data);
400 close_network_connections();
401 exit_configuration(&config_tree);
403 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
406 init_configuration(&config_tree);
408 if(read_server_config()) {
410 _("Unable to reread configuration file, exitting."));
414 if(setup_network_connections())