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