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