Run graph() after edge_del() when updating an edge.
[tinc] / src / net_socket.c
1 /*
2     net_socket.c -- Handle various kinds of sockets.
3     Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5
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.
10
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.
15
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.
19
20     $Id: net_socket.c,v 1.1.2.22 2002/09/15 14:55:53 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <syslog.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38 /* SunOS really wants sys/socket.h BEFORE net/if.h,
39    and FreeBSD wants these lines below the rest. */
40 #include <arpa/inet.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #ifdef HAVE_NETINET_IN_SYSTM_H
44 #include <netinet/in_systm.h>
45 #endif
46 #ifdef HAVE_NETINET_IP_H
47 #include <netinet/ip.h>
48 #endif
49 #ifdef HAVE_NETINET_TCP_H
50 #include <netinet/tcp.h>
51 #endif
52
53 #include <utils.h>
54 #include <xalloc.h>
55 #include <avl_tree.h>
56 #include <list.h>
57
58 #include "conf.h"
59 #include "connection.h"
60 #include "meta.h"
61 #include "net.h"
62 #include "netutl.h"
63 #include "process.h"
64 #include "protocol.h"
65 #include "subnet.h"
66 #include "graph.h"
67 #include "process.h"
68 #include "route.h"
69 #include "device.h"
70 #include "event.h"
71
72 #include "system.h"
73
74 #ifndef HAVE_RAND_PSEUDO_BYTES
75 #define RAND_pseudo_bytes RAND_bytes
76 #endif
77
78 int addressfamily = AF_INET;
79 int maxtimeout = 900;
80 int seconds_till_retry = 5;
81
82 listen_socket_t listen_socket[MAXSOCKETS];
83 int listen_sockets;
84
85 /* Setup sockets */
86
87 int setup_listen_socket(sockaddr_t *sa)
88 {
89         int nfd, flags;
90         char *addrstr;
91         int option;
92 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
93         char *interface;
94         struct ifreq ifr;
95 #endif
96
97         cp();
98
99         nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
100
101         if(nfd < 0) {
102                 syslog(LOG_ERR, _("Creating metasocket failed: %s"), strerror(errno));
103                 return -1;
104         }
105
106         flags = fcntl(nfd, F_GETFL);
107
108         if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
109                 close(nfd);
110                 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
111                            strerror(errno));
112                 return -1;
113         }
114
115         /* Optimize TCP settings */
116
117         option = 1;
118         setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
119
120 #if defined(SOL_TCP) && defined(TCP_NODELAY)
121         setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
122 #endif
123
124 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
125         option = IPTOS_LOWDELAY;
126         setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
127 #endif
128
129         if(get_config_string
130            (lookup_config(config_tree, "BindToInterface"), &interface)) {
131 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
132                 memset(&ifr, 0, sizeof(ifr));
133                 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
134
135                 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
136                         close(nfd);
137                         syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface,
138                                    strerror(errno));
139                         return -1;
140                 }
141 #else
142                 syslog(LOG_WARNING, _("BindToDevice not supported on this platform"));
143 #endif
144         }
145
146         if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
147                 close(nfd);
148                 addrstr = sockaddr2hostname(sa);
149                 syslog(LOG_ERR, _("Can't bind to %s/tcp: %s"), addrstr,
150                            strerror(errno));
151                 free(addrstr);
152                 return -1;
153         }
154
155         if(listen(nfd, 3)) {
156                 close(nfd);
157                 syslog(LOG_ERR, _("System call `%s' failed: %s"), "listen",
158                            strerror(errno));
159                 return -1;
160         }
161
162         return nfd;
163 }
164
165 int setup_vpn_in_socket(sockaddr_t *sa)
166 {
167         int nfd, flags;
168         char *addrstr;
169         int option;
170 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
171         char *interface;
172         struct ifreq ifr;
173 #endif
174
175         cp();
176
177         nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
178
179         if(nfd < 0) {
180                 syslog(LOG_ERR, _("Creating UDP socket failed: %s"), strerror(errno));
181                 return -1;
182         }
183
184         flags = fcntl(nfd, F_GETFL);
185         if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
186                 close(nfd);
187                 syslog(LOG_ERR, _("System call `%s' failed: %s"), "fcntl",
188                            strerror(errno));
189                 return -1;
190         }
191
192         option = 1;
193         setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
194
195 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
196         if(get_config_string
197            (lookup_config(config_tree, "BindToInterface"), &interface)) {
198                 memset(&ifr, 0, sizeof(ifr));
199                 strncpy(ifr.ifr_ifrn.ifrn_name, interface, IFNAMSIZ);
200
201                 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) {
202                         close(nfd);
203                         syslog(LOG_ERR, _("Can't bind to interface %s: %s"), interface,
204                                    strerror(errno));
205                         return -1;
206                 }
207         }
208 #endif
209
210         if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
211                 close(nfd);
212                 addrstr = sockaddr2hostname(sa);
213                 syslog(LOG_ERR, _("Can't bind to %s/udp: %s"), addrstr,
214                            strerror(errno));
215                 free(addrstr);
216                 return -1;
217         }
218
219         return nfd;
220 }
221
222 void retry_outgoing(outgoing_t *outgoing)
223 {
224         event_t *event;
225
226         cp();
227
228         outgoing->timeout += 5;
229
230         if(outgoing->timeout > maxtimeout)
231                 outgoing->timeout = maxtimeout;
232
233         event = new_event();
234         event->handler = (event_handler_t) setup_outgoing_connection;
235         event->time = now + outgoing->timeout;
236         event->data = outgoing;
237         event_add(event);
238
239         if(debug_lvl >= DEBUG_CONNECTIONS)
240                 syslog(LOG_NOTICE,
241                            _("Trying to re-establish outgoing connection in %d seconds"),
242                            outgoing->timeout);
243 }
244
245 int setup_outgoing_socket(connection_t *c)
246 {
247         int option;
248
249         cp();
250
251         if(debug_lvl >= DEBUG_CONNECTIONS)
252                 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
253                            c->hostname);
254
255         c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
256
257         if(c->socket == -1) {
258                 syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
259                            strerror(errno));
260                 return -1;
261         }
262
263         /* Optimize TCP settings */
264
265 #if defined(SOL_TCP) && defined(TCP_NODELAY)
266         option = 1;
267         setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
268 #endif
269
270 #if defined(SOL_IP) && defined(IP_TOS)
271         option = IPTOS_LOWDELAY;
272         setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
273 #endif
274
275         /* Connect */
276
277         if(connect(c->socket, &c->address.sa, SALEN(c->address.sa)) == -1) {
278                 close(c->socket);
279                 syslog(LOG_ERR, _("Error while connecting to %s (%s): %s"), c->name,
280                            c->hostname, strerror(errno));
281                 return -1;
282         }
283
284         if(debug_lvl >= DEBUG_CONNECTIONS)
285                 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
286
287         return 0;
288 }
289
290
291 void finish_connecting(connection_t *c)
292 {
293         cp();
294
295         if(debug_lvl >= DEBUG_CONNECTIONS)
296                 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
297
298         c->last_ping_time = now;
299
300         send_id(c);
301 }
302
303 void do_outgoing_connection(connection_t *c)
304 {
305         char *address, *port;
306         int option, result, flags;
307
308         cp();
309
310 begin:
311         if(!c->outgoing->ai) {
312                 if(!c->outgoing->cfg) {
313                         if(debug_lvl >= DEBUG_CONNECTIONS)
314                                 syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
315                                            c->name);
316                         c->status.remove = 1;
317                         retry_outgoing(c->outgoing);
318                         return;
319                 }
320
321                 get_config_string(c->outgoing->cfg, &address);
322
323                 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
324                         asprintf(&port, "655");
325
326                 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
327                 free(address);
328                 free(port);
329
330                 c->outgoing->aip = c->outgoing->ai;
331                 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
332         }
333
334         if(!c->outgoing->aip) {
335                 freeaddrinfo(c->outgoing->ai);
336                 c->outgoing->ai = NULL;
337                 goto begin;
338         }
339
340         memcpy(&c->address, c->outgoing->aip->ai_addr,
341                    c->outgoing->aip->ai_addrlen);
342         c->outgoing->aip = c->outgoing->aip->ai_next;
343
344         if(c->hostname)
345                 free(c->hostname);
346
347         c->hostname = sockaddr2hostname(&c->address);
348
349         if(debug_lvl >= DEBUG_CONNECTIONS)
350                 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
351                            c->hostname);
352
353         c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
354
355         if(c->socket == -1) {
356                 if(debug_lvl >= DEBUG_CONNECTIONS)
357                         syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
358                                    strerror(errno));
359
360                 goto begin;
361         }
362
363         /* Optimize TCP settings */
364
365 #if defined(SOL_TCP) && defined(TCP_NODELAY)
366         option = 1;
367         setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
368 #endif
369
370 #if defined(SOL_IP) && defined(IP_TOS)
371         option = IPTOS_LOWDELAY;
372         setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
373 #endif
374
375         /* Non-blocking */
376
377         flags = fcntl(c->socket, F_GETFL);
378
379         if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
380                 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
381         }
382
383         /* Connect */
384
385         result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
386
387         if(result == -1) {
388                 if(errno == EINPROGRESS) {
389                         c->status.connecting = 1;
390                         return;
391                 }
392
393                 close(c->socket);
394
395                 if(debug_lvl >= DEBUG_CONNECTIONS)
396                         syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
397
398                 goto begin;
399         }
400
401         finish_connecting(c);
402
403         return;
404 }
405
406 void setup_outgoing_connection(outgoing_t *outgoing)
407 {
408         connection_t *c;
409         node_t *n;
410
411         cp();
412
413         n = lookup_node(outgoing->name);
414
415         if(n)
416                 if(n->connection) {
417                         if(debug_lvl >= DEBUG_CONNECTIONS)
418                                 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
419
420                         n->connection->outgoing = outgoing;
421                         return;
422                 }
423
424         c = new_connection();
425         c->name = xstrdup(outgoing->name);
426         c->outcipher = myself->connection->outcipher;
427         c->outdigest = myself->connection->outdigest;
428         c->outmaclength = myself->connection->outmaclength;
429         c->outcompression = myself->connection->outcompression;
430
431         init_configuration(&c->config_tree);
432         read_connection_config(c);
433
434         outgoing->cfg = lookup_config(c->config_tree, "Address");
435
436         if(!outgoing->cfg) {
437                 syslog(LOG_ERR, _("No address specified for %s"), c->name);
438                 free_connection(c);
439                 free(outgoing->name);
440                 free(outgoing);
441                 return;
442         }
443
444         c->outgoing = outgoing;
445         c->last_ping_time = now;
446
447         connection_add(c);
448
449         do_outgoing_connection(c);
450 }
451
452 /*
453   accept a new tcp connect and create a
454   new connection
455 */
456 int handle_new_meta_connection(int sock)
457 {
458         connection_t *c;
459         sockaddr_t sa;
460         int fd, len = sizeof(sa);
461
462         cp();
463
464         fd = accept(sock, &sa.sa, &len);
465
466         if(fd < 0) {
467                 syslog(LOG_ERR, _("Accepting a new connection failed: %s"),
468                            strerror(errno));
469                 return -1;
470         }
471
472         sockaddrunmap(&sa);
473
474         c = new_connection();
475         c->outcipher = myself->connection->outcipher;
476         c->outdigest = myself->connection->outdigest;
477         c->outmaclength = myself->connection->outmaclength;
478         c->outcompression = myself->connection->outcompression;
479
480         c->address = sa;
481         c->hostname = sockaddr2hostname(&sa);
482         c->socket = fd;
483         c->last_ping_time = now;
484
485         if(debug_lvl >= DEBUG_CONNECTIONS)
486                 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
487
488         connection_add(c);
489
490         c->allow_request = ID;
491         send_id(c);
492
493         return 0;
494 }
495
496 void try_outgoing_connections(void)
497 {
498         static config_t *cfg = NULL;
499         char *name;
500         outgoing_t *outgoing;
501
502         cp();
503
504         for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
505                 cfg = lookup_config_next(config_tree, cfg)) {
506                 get_config_string(cfg, &name);
507
508                 if(check_id(name)) {
509                         syslog(LOG_ERR,
510                                    _("Invalid name for outgoing connection in %s line %d"),
511                                    cfg->file, cfg->line);
512                         free(name);
513                         continue;
514                 }
515
516                 outgoing = xmalloc_and_zero(sizeof(*outgoing));
517                 outgoing->name = name;
518                 setup_outgoing_connection(outgoing);
519         }
520 }