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