2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2014 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2009 Florian Forster <octo@verplant.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "connection.h"
37 /* Needed on Mac OS/X */
39 #define SOL_TCP IPPROTO_TCP
42 int addressfamily = AF_UNSPEC;
45 int seconds_till_retry = 5;
49 listen_socket_t listen_socket[MAXSOCKETS];
51 list_t *outgoing_list = NULL;
55 static void configure_tcp(connection_t *c) {
59 int flags = fcntl(c->socket, F_GETFL);
61 if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
62 logger(LOG_ERR, "fcntl for %s: %s", c->hostname, strerror(errno));
65 unsigned long arg = 1;
67 if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
68 logger(LOG_ERR, "ioctlsocket for %s: %s", c->hostname, sockstrerror(sockerrno));
72 #if defined(SOL_TCP) && defined(TCP_NODELAY)
74 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&option, sizeof(option));
77 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
78 option = IPTOS_LOWDELAY;
79 setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&option, sizeof(option));
82 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) && defined(IPTOS_LOWDELAY)
83 option = IPTOS_LOWDELAY;
84 setsockopt(c->socket, IPPROTO_IPV6, IPV6_TCLASS, (void *)&option, sizeof(option));
88 static bool bind_to_interface(int sd) {
91 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
94 #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
96 if(!get_config_string(lookup_config (config_tree, "BindToInterface"), &iface))
99 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
100 memset(&ifr, 0, sizeof(ifr));
101 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
102 ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
105 status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
107 logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(errno));
111 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
112 logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
118 int setup_listen_socket(const sockaddr_t *sa) {
124 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
127 ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
132 fcntl(nfd, F_SETFD, FD_CLOEXEC);
135 /* Optimize TCP settings */
138 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
140 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
141 if(sa->sa.sa_family == AF_INET6)
142 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
145 if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
146 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
149 memset(&ifr, 0, sizeof(ifr));
150 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
151 ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
154 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
156 logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(sockerrno));
161 logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
165 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
167 addrstr = sockaddr2hostname(sa);
168 logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
175 logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
182 int setup_vpn_in_socket(const sockaddr_t *sa) {
187 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
190 logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
195 fcntl(nfd, F_SETFD, FD_CLOEXEC);
200 int flags = fcntl(nfd, F_GETFL);
202 if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
204 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl",
211 unsigned long arg = 1;
212 if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
214 logger(LOG_ERR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
221 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
222 setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
224 if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf)))
225 logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno));
227 if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf)))
228 logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno));
230 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
231 if(sa->sa.sa_family == AF_INET6)
232 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
235 #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
236 #define IP_DONTFRAGMENT IP_DONTFRAG
239 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
240 if(myself->options & OPTION_PMTU_DISCOVERY) {
241 option = IP_PMTUDISC_DO;
242 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
244 #elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
245 if(myself->options & OPTION_PMTU_DISCOVERY) {
247 setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
251 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
252 if(myself->options & OPTION_PMTU_DISCOVERY) {
253 option = IPV6_PMTUDISC_DO;
254 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
256 #elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
257 if(myself->options & OPTION_PMTU_DISCOVERY) {
259 setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
263 if (!bind_to_interface(nfd)) {
268 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
270 addrstr = sockaddr2hostname(sa);
271 logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
277 } /* int setup_vpn_in_socket */
279 void retry_outgoing(outgoing_t *outgoing) {
280 outgoing->timeout += 5;
282 if(outgoing->timeout < mintimeout)
283 outgoing->timeout = mintimeout;
285 if(outgoing->timeout > maxtimeout)
286 outgoing->timeout = maxtimeout;
289 event_del(outgoing->event);
290 outgoing->event = new_event();
291 outgoing->event->handler = (event_handler_t) setup_outgoing_connection;
292 outgoing->event->time = now + outgoing->timeout;
293 outgoing->event->data = outgoing;
294 event_add(outgoing->event);
296 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
297 "Trying to re-establish outgoing connection in %d seconds",
301 void finish_connecting(connection_t *c) {
302 ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
304 c->last_ping_time = now;
309 static void do_outgoing_pipe(connection_t *c, char *command) {
313 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
314 logger(LOG_ERR, "Could not create socketpair: %s\n", strerror(errno));
321 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Using proxy %s", command);
332 // Other filedescriptors should be closed automatically by CLOEXEC
337 sockaddr2str(&c->address, &host, &port);
338 setenv("REMOTEADDRESS", host, true);
339 setenv("REMOTEPORT", port, true);
340 setenv("NODE", c->name, true);
341 setenv("NAME", myself->name, true);
343 setenv("NETNAME", netname, true);
345 int result = system(command);
347 logger(LOG_ERR, "Could not execute %s: %s\n", command, strerror(errno));
349 logger(LOG_ERR, "%s exited with non-zero status %d", command, result);
352 logger(LOG_ERR, "Proxy type exec not supported on this platform!");
357 void do_outgoing_connection(connection_t *c) {
358 char *address, *port, *space;
359 struct addrinfo *proxyai = NULL;
363 logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
368 if(!c->outgoing->ai) {
369 if(!c->outgoing->cfg) {
370 ifdebug(CONNECTIONS) logger(LOG_ERR, "Could not set up a meta connection to %s",
372 c->status.remove = true;
373 retry_outgoing(c->outgoing);
378 get_config_string(c->outgoing->cfg, &address);
380 space = strchr(address, ' ');
382 port = xstrdup(space + 1);
385 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
386 port = xstrdup("655");
389 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
393 c->outgoing->aip = c->outgoing->ai;
394 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
397 if(!c->outgoing->aip) {
399 freeaddrinfo(c->outgoing->ai);
400 c->outgoing->ai = NULL;
404 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
405 c->outgoing->aip = c->outgoing->aip->ai_next;
410 c->hostname = sockaddr2hostname(&c->address);
412 ifdebug(CONNECTIONS) logger(LOG_INFO, "Trying to connect to %s (%s)", c->name,
416 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
417 } else if(proxytype == PROXY_EXEC) {
418 do_outgoing_pipe(c, proxyhost);
420 proxyai = str2addrinfo(proxyhost, proxyport, SOCK_STREAM);
423 ifdebug(CONNECTIONS) logger(LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
424 c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
427 if(c->socket == -1) {
428 ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
432 if(proxytype != PROXY_EXEC)
436 fcntl(c->socket, F_SETFD, FD_CLOEXEC);
439 if(proxytype != PROXY_EXEC) {
440 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
442 if(c->address.sa.sa_family == AF_INET6)
443 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
446 bind_to_interface(c->socket);
452 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
453 } else if(proxytype == PROXY_EXEC) {
456 result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
457 freeaddrinfo(proxyai);
461 if(sockinprogress(sockerrno)) {
462 c->status.connecting = true;
466 closesocket(c->socket);
468 ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, sockstrerror(sockerrno));
473 finish_connecting(c);
478 void setup_outgoing_connection(outgoing_t *outgoing) {
482 outgoing->event = NULL;
484 n = lookup_node(outgoing->name);
488 ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name);
490 n->connection->outgoing = outgoing;
494 c = new_connection();
495 c->name = xstrdup(outgoing->name);
496 c->outcipher = myself->connection->outcipher;
497 c->outdigest = myself->connection->outdigest;
498 c->outmaclength = myself->connection->outmaclength;
499 c->outcompression = myself->connection->outcompression;
501 init_configuration(&c->config_tree);
502 read_connection_config(c);
504 outgoing->cfg = lookup_config(c->config_tree, "Address");
507 logger(LOG_ERR, "No address specified for %s", c->name);
512 c->outgoing = outgoing;
513 c->last_ping_time = now;
517 do_outgoing_connection(c);
521 accept a new tcp connect and create a
524 bool handle_new_meta_connection(int sock) {
528 socklen_t len = sizeof(sa);
530 fd = accept(sock, &sa.sa, &len);
533 logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
539 c = new_connection();
540 c->name = xstrdup("<unknown>");
541 c->outcipher = myself->connection->outcipher;
542 c->outdigest = myself->connection->outdigest;
543 c->outmaclength = myself->connection->outmaclength;
544 c->outcompression = myself->connection->outcompression;
547 c->hostname = sockaddr2hostname(&sa);
549 c->last_ping_time = now;
551 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
557 c->allow_request = ID;
563 static void free_outgoing(outgoing_t *outgoing) {
565 freeaddrinfo(outgoing->ai);
568 free(outgoing->name);
573 void try_outgoing_connections(void) {
574 static config_t *cfg = NULL;
576 outgoing_t *outgoing;
578 outgoing_list = list_alloc((list_action_t)free_outgoing);
580 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
581 get_config_string(cfg, &name);
583 if(!check_id(name)) {
585 "Invalid name for outgoing connection in %s line %d",
586 cfg->file, cfg->line);
591 outgoing = xmalloc_and_zero(sizeof(*outgoing));
592 outgoing->name = name;
593 list_insert_tail(outgoing_list, outgoing);
594 setup_outgoing_connection(outgoing);