2 net_socket.c -- Handle various kinds of sockets.
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 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.
25 #include "splay_tree.h"
27 #include "connection.h"
38 /* Needed on Mac OS/X */
40 #define SOL_TCP IPPROTO_TCP
43 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) {
58 #if defined(SOL_TCP) && defined(TCP_NODELAY)
60 setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&option, sizeof option);
63 #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY)
64 option = IPTOS_LOWDELAY;
65 setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&option, sizeof option);
69 static bool bind_to_interface(int sd) {
72 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
75 #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
77 if(!get_config_string (lookup_config (config_tree, "BindToInterface"), &iface))
80 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
81 memset(&ifr, 0, sizeof(ifr));
82 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
83 ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
85 status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
87 logger(LOG_ERR, "Can't bind to interface %s: %s", iface,
91 #else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
92 logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
98 static bool bind_to_address(connection_t *c) {
100 struct addrinfo *ai_list;
101 struct addrinfo *ai_ptr;
102 struct addrinfo ai_hints;
106 assert(c->socket >= 0);
109 if(!get_config_string(lookup_config(config_tree, "BindToAddress"),
113 assert(node != NULL);
115 memset(&ai_hints, 0, sizeof(ai_hints));
116 ai_hints.ai_family = c->address.sa.sa_family;
117 /* We're called from `do_outgoing_connection' only. */
118 ai_hints.ai_socktype = SOCK_STREAM;
119 ai_hints.ai_protocol = IPPROTO_TCP;
123 status = getaddrinfo(node, /* service = */ NULL,
124 &ai_hints, &ai_list);
127 logger(LOG_WARNING, "Error looking up %s port %s: %s",
128 node, "any", gai_strerror(status));
131 assert(ai_list != NULL);
134 for(ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
135 status = bind(c->socket,
136 ai_list->ai_addr, ai_list->ai_addrlen);
143 logger(LOG_ERR, "Can't bind to %s/tcp: %s", node, sockstrerror(sockerrno));
144 } else ifdebug(CONNECTIONS) {
145 logger(LOG_DEBUG, "Successfully bound outgoing "
146 "TCP socket to %s", node);
150 freeaddrinfo(ai_list);
152 return status ? false : true;
155 int setup_listen_socket(const sockaddr_t *sa) {
161 nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
164 ifdebug(STATUS) logger(LOG_ERR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
168 /* Optimize TCP settings */
171 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof option);
173 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
174 if(sa->sa.sa_family == AF_INET6)
175 setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
179 (lookup_config(config_tree, "BindToInterface"), &iface)) {
180 #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
183 memset(&ifr, 0, sizeof ifr);
184 strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
186 if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof ifr)) {
188 logger(LOG_ERR, "Can't bind to interface %s: %s", iface,
189 strerror(sockerrno));
193 logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
197 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
199 addrstr = sockaddr2hostname(sa);
200 logger(LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
207 logger(LOG_ERR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
214 int setup_vpn_in_socket(const sockaddr_t *sa) {
219 nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
222 logger(LOG_ERR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
227 setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof option);
229 if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf)))
230 logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno));
232 if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf)))
233 logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno));
235 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
236 if(sa->sa.sa_family == AF_INET6)
237 setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
240 #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
241 #define IP_DONTFRAGMENT IP_DONTFRAG
244 #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
245 if(myself->options & OPTION_PMTU_DISCOVERY) {
246 option = IP_PMTUDISC_DO;
247 setsockopt(nfd, SOL_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
249 #elif defined(IPPROTO_IP) && defined(IP_DONTFRAGMENT)
250 if(myself->options & OPTION_PMTU_DISCOVERY) {
252 setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
255 #warning No way to disable IPv4 fragmentation
258 #if defined(SOL_IPV6) && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
259 if(myself->options & OPTION_PMTU_DISCOVERY) {
260 option = IPV6_PMTUDISC_DO;
261 setsockopt(nfd, SOL_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
263 #elif defined(IPPROTO_IPV6) && defined(IPV6_DONTFRAG)
264 if(myself->options & OPTION_PMTU_DISCOVERY) {
266 setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
269 #warning No way to disable IPv6 fragmentation
272 if (!bind_to_interface(nfd)) {
277 if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
279 addrstr = sockaddr2hostname(sa);
280 logger(LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
286 } /* int setup_vpn_in_socket */
288 static void retry_outgoing_handler(int fd, short events, void *data) {
289 setup_outgoing_connection(data);
292 void retry_outgoing(outgoing_t *outgoing) {
293 outgoing->timeout += 5;
295 if(outgoing->timeout > maxtimeout)
296 outgoing->timeout = maxtimeout;
298 timeout_set(&outgoing->ev, retry_outgoing_handler, outgoing);
299 event_add(&outgoing->ev, &(struct timeval){outgoing->timeout, 0});
301 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
302 "Trying to re-establish outgoing connection in %d seconds",
306 void finish_connecting(connection_t *c) {
307 ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
311 c->last_ping_time = time(NULL);
312 c->status.connecting = false;
317 void do_outgoing_connection(connection_t *c) {
318 char *address, *port, *space;
322 logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
327 if(!c->outgoing->ai) {
328 if(!c->outgoing->cfg) {
329 ifdebug(CONNECTIONS) logger(LOG_ERR, "Could not set up a meta connection to %s",
331 retry_outgoing(c->outgoing);
337 get_config_string(c->outgoing->cfg, &address);
339 space = strchr(address, ' ');
341 port = xstrdup(space + 1);
344 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
345 port = xstrdup("655");
348 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
352 c->outgoing->aip = c->outgoing->ai;
353 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
356 if(!c->outgoing->aip) {
358 freeaddrinfo(c->outgoing->ai);
359 c->outgoing->ai = NULL;
363 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
364 c->outgoing->aip = c->outgoing->aip->ai_next;
369 c->hostname = sockaddr2hostname(&c->address);
371 ifdebug(CONNECTIONS) logger(LOG_INFO, "Trying to connect to %s (%s)", c->name,
374 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
376 if(c->socket == -1) {
377 ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
381 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
383 if(c->address.sa.sa_family == AF_INET6)
384 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
387 bind_to_interface(c->socket);
390 /* Optimize TCP settings */
396 result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
399 if(sockinprogress(sockerrno)) {
400 c->status.connecting = true;
404 closesocket(c->socket);
406 ifdebug(CONNECTIONS) logger(LOG_ERR, "%s: %s", c->hostname, sockstrerror(sockerrno));
411 finish_connecting(c);
416 void handle_meta_read(struct bufferevent *event, void *data) {
417 logger(LOG_ERR, "handle_meta_read() called");
421 void handle_meta_write(struct bufferevent *event, void *data) {
422 ifdebug(META) logger(LOG_DEBUG, "handle_meta_write() called");
425 void handle_meta_connection_error(struct bufferevent *event, short what, void *data) {
426 connection_t *c = data;
427 logger(LOG_ERR, "handle_meta_connection_error() called: %d: %s", what, strerror(errno));
428 terminate_connection(c, c->status.active);
431 void setup_outgoing_connection(outgoing_t *outgoing) {
435 event_del(&outgoing->ev);
437 n = lookup_node(outgoing->name);
441 ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name);
443 n->connection->outgoing = outgoing;
447 c = new_connection();
448 c->name = xstrdup(outgoing->name);
449 c->outcipher = myself->connection->outcipher;
450 c->outdigest = myself->connection->outdigest;
451 c->outmaclength = myself->connection->outmaclength;
452 c->outcompression = myself->connection->outcompression;
454 init_configuration(&c->config_tree);
455 read_connection_config(c);
457 outgoing->cfg = lookup_config(c->config_tree, "Address");
460 logger(LOG_ERR, "No address specified for %s", c->name);
465 c->outgoing = outgoing;
466 c->last_ping_time = time(NULL);
470 do_outgoing_connection(c);
472 c->buffer = bufferevent_new(c->socket, handle_meta_read, handle_meta_write, handle_meta_connection_error, c);
474 logger(LOG_ERR, "bufferevent_new() failed: %s", strerror(errno));
477 bufferevent_disable(c->buffer, EV_READ);
479 if(!thread_create(&c->thread, handle_meta_connection_data, c)) {
480 logger(LOG_ERR, "create_thread() failed: %s", strerror(errno));
486 accept a new tcp connect and create a
489 void handle_new_meta_connection(int sock, short events, void *data) {
493 socklen_t len = sizeof sa;
495 fd = accept(sock, &sa.sa, &len);
498 logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
504 c = new_connection();
505 c->name = xstrdup("<unknown>");
506 c->outcipher = myself->connection->outcipher;
507 c->outdigest = myself->connection->outdigest;
508 c->outmaclength = myself->connection->outmaclength;
509 c->outcompression = myself->connection->outcompression;
512 c->hostname = sockaddr2hostname(&sa);
514 c->last_ping_time = time(NULL);
516 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
518 c->buffer = bufferevent_new(c->socket, NULL, handle_meta_write, handle_meta_connection_error, c);
520 logger(LOG_ERR, "bufferevent_new() failed: %s", strerror(errno));
523 bufferevent_disable(c->buffer, EV_READ);
529 c->allow_request = ID;
532 if(!thread_create(&c->thread, handle_meta_connection_data, c)) {
533 logger(LOG_ERR, "create_thread() failed: %s", strerror(errno));
538 void free_outgoing(outgoing_t *outgoing) {
540 freeaddrinfo(outgoing->ai);
543 free(outgoing->name);
548 void try_outgoing_connections(void) {
549 static config_t *cfg = NULL;
551 outgoing_t *outgoing;
553 outgoing_list = list_alloc((list_action_t)free_outgoing);
555 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
556 get_config_string(cfg, &name);
558 if(!check_id(name)) {
560 "Invalid name for outgoing connection in %s line %d",
561 cfg->file, cfg->line);
566 outgoing = xmalloc_and_zero(sizeof *outgoing);
567 outgoing->name = name;
568 list_insert_tail(outgoing_list, outgoing);
569 setup_outgoing_connection(outgoing);