b17dd8db86ca1ffe26833b537fe23a40207c73c0
[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.23 2003/01/17 00:37:20 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, _("BindToInterface 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 void finish_connecting(connection_t *c)
246 {
247         cp();
248
249         if(debug_lvl >= DEBUG_CONNECTIONS)
250                 syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname);
251
252         c->last_ping_time = now;
253
254         send_id(c);
255 }
256
257 void do_outgoing_connection(connection_t *c)
258 {
259         char *address, *port;
260         int option, result, flags;
261
262         cp();
263
264 begin:
265         if(!c->outgoing->ai) {
266                 if(!c->outgoing->cfg) {
267                         if(debug_lvl >= DEBUG_CONNECTIONS)
268                                 syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
269                                            c->name);
270                         c->status.remove = 1;
271                         retry_outgoing(c->outgoing);
272                         return;
273                 }
274
275                 get_config_string(c->outgoing->cfg, &address);
276
277                 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
278                         asprintf(&port, "655");
279
280                 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
281                 free(address);
282                 free(port);
283
284                 c->outgoing->aip = c->outgoing->ai;
285                 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
286         }
287
288         if(!c->outgoing->aip) {
289                 freeaddrinfo(c->outgoing->ai);
290                 c->outgoing->ai = NULL;
291                 goto begin;
292         }
293
294         memcpy(&c->address, c->outgoing->aip->ai_addr,
295                    c->outgoing->aip->ai_addrlen);
296         c->outgoing->aip = c->outgoing->aip->ai_next;
297
298         if(c->hostname)
299                 free(c->hostname);
300
301         c->hostname = sockaddr2hostname(&c->address);
302
303         if(debug_lvl >= DEBUG_CONNECTIONS)
304                 syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name,
305                            c->hostname);
306
307         c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
308
309         if(c->socket == -1) {
310                 if(debug_lvl >= DEBUG_CONNECTIONS)
311                         syslog(LOG_ERR, _("Creating socket for %s failed: %s"), c->hostname,
312                                    strerror(errno));
313
314                 goto begin;
315         }
316
317         /* Optimize TCP settings */
318
319 #if defined(SOL_TCP) && defined(TCP_NODELAY)
320         option = 1;
321         setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
322 #endif
323
324 #if defined(SOL_IP) && defined(IP_TOS)
325         option = IPTOS_LOWDELAY;
326         setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option));
327 #endif
328
329         /* Non-blocking */
330
331         flags = fcntl(c->socket, F_GETFL);
332
333         if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
334                 syslog(LOG_ERR, _("fcntl for %s: %s"), c->hostname, strerror(errno));
335         }
336
337         /* Connect */
338
339         result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
340
341         if(result == -1) {
342                 if(errno == EINPROGRESS) {
343                         c->status.connecting = 1;
344                         return;
345                 }
346
347                 close(c->socket);
348
349                 if(debug_lvl >= DEBUG_CONNECTIONS)
350                         syslog(LOG_ERR, _("%s: %s"), c->hostname, strerror(errno));
351
352                 goto begin;
353         }
354
355         finish_connecting(c);
356
357         return;
358 }
359
360 void setup_outgoing_connection(outgoing_t *outgoing)
361 {
362         connection_t *c;
363         node_t *n;
364
365         cp();
366
367         n = lookup_node(outgoing->name);
368
369         if(n)
370                 if(n->connection) {
371                         if(debug_lvl >= DEBUG_CONNECTIONS)
372                                 syslog(LOG_INFO, _("Already connected to %s"), outgoing->name);
373
374                         n->connection->outgoing = outgoing;
375                         return;
376                 }
377
378         c = new_connection();
379         c->name = xstrdup(outgoing->name);
380         c->outcipher = myself->connection->outcipher;
381         c->outdigest = myself->connection->outdigest;
382         c->outmaclength = myself->connection->outmaclength;
383         c->outcompression = myself->connection->outcompression;
384
385         init_configuration(&c->config_tree);
386         read_connection_config(c);
387
388         outgoing->cfg = lookup_config(c->config_tree, "Address");
389
390         if(!outgoing->cfg) {
391                 syslog(LOG_ERR, _("No address specified for %s"), c->name);
392                 free_connection(c);
393                 free(outgoing->name);
394                 free(outgoing);
395                 return;
396         }
397
398         c->outgoing = outgoing;
399         c->last_ping_time = now;
400
401         connection_add(c);
402
403         do_outgoing_connection(c);
404 }
405
406 /*
407   accept a new tcp connect and create a
408   new connection
409 */
410 int handle_new_meta_connection(int sock)
411 {
412         connection_t *c;
413         sockaddr_t sa;
414         int fd, len = sizeof(sa);
415
416         cp();
417
418         fd = accept(sock, &sa.sa, &len);
419
420         if(fd < 0) {
421                 syslog(LOG_ERR, _("Accepting a new connection failed: %s"),
422                            strerror(errno));
423                 return -1;
424         }
425
426         sockaddrunmap(&sa);
427
428         c = new_connection();
429         c->outcipher = myself->connection->outcipher;
430         c->outdigest = myself->connection->outdigest;
431         c->outmaclength = myself->connection->outmaclength;
432         c->outcompression = myself->connection->outcompression;
433
434         c->address = sa;
435         c->hostname = sockaddr2hostname(&sa);
436         c->socket = fd;
437         c->last_ping_time = now;
438
439         if(debug_lvl >= DEBUG_CONNECTIONS)
440                 syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
441
442         connection_add(c);
443
444         c->allow_request = ID;
445         send_id(c);
446
447         return 0;
448 }
449
450 void try_outgoing_connections(void)
451 {
452         static config_t *cfg = NULL;
453         char *name;
454         outgoing_t *outgoing;
455
456         cp();
457
458         for(cfg = lookup_config(config_tree, "ConnectTo"); cfg;
459                 cfg = lookup_config_next(config_tree, cfg)) {
460                 get_config_string(cfg, &name);
461
462                 if(check_id(name)) {
463                         syslog(LOG_ERR,
464                                    _("Invalid name for outgoing connection in %s line %d"),
465                                    cfg->file, cfg->line);
466                         free(name);
467                         continue;
468                 }
469
470                 outgoing = xmalloc_and_zero(sizeof(*outgoing));
471                 outgoing->name = name;
472                 setup_outgoing_connection(outgoing);
473         }
474 }