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