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