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(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 outgoing->ev.handler = retry_outgoing_handler;
299 outgoing->ev.time = time(NULL) + outgoing->timeout;
300 event_add(&outgoing->ev);
302 ifdebug(CONNECTIONS) logger(LOG_NOTICE,
303 "Trying to re-establish outgoing connection in %d seconds",
307 void finish_connecting(connection_t *c) {
308 ifdebug(CONNECTIONS) logger(LOG_INFO, "Connected to %s (%s)", c->name, c->hostname);
312 c->last_ping_time = time(NULL);
313 c->status.connecting = false;
318 void do_outgoing_connection(connection_t *c) {
319 char *address, *port, *space;
323 logger(LOG_ERR, "do_outgoing_connection() for %s called without c->outgoing", c->name);
328 if(!c->outgoing->ai) {
329 if(!c->outgoing->cfg) {
330 ifdebug(CONNECTIONS) logger(LOG_ERR, "Could not set up a meta connection to %s",
332 retry_outgoing(c->outgoing);
338 get_config_string(c->outgoing->cfg, &address);
340 space = strchr(address, ' ');
342 port = xstrdup(space + 1);
345 if(!get_config_string(lookup_config(c->config_tree, "Port"), &port))
346 port = xstrdup("655");
349 c->outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
353 c->outgoing->aip = c->outgoing->ai;
354 c->outgoing->cfg = lookup_config_next(c->config_tree, c->outgoing->cfg);
357 if(!c->outgoing->aip) {
359 freeaddrinfo(c->outgoing->ai);
360 c->outgoing->ai = NULL;
364 memcpy(&c->address, c->outgoing->aip->ai_addr, c->outgoing->aip->ai_addrlen);
365 c->outgoing->aip = c->outgoing->aip->ai_next;
370 c->hostname = sockaddr2hostname(&c->address);
372 ifdebug(CONNECTIONS) logger(LOG_INFO, "Trying to connect to %s (%s)", c->name,
375 c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
377 if(c->socket == -1) {
378 ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
382 #if defined(SOL_IPV6) && defined(IPV6_V6ONLY)
384 if(c->address.sa.sa_family == AF_INET6)
385 setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
388 bind_to_interface(c->socket);
391 /* Optimize TCP settings */
395 c->status.connecting = true;
399 void setup_outgoing_connection(outgoing_t *outgoing) {
403 event_del(&outgoing->ev);
405 n = lookup_node(outgoing->name);
409 ifdebug(CONNECTIONS) logger(LOG_INFO, "Already connected to %s", outgoing->name);
411 n->connection->outgoing = outgoing;
415 c = new_connection();
416 c->name = xstrdup(outgoing->name);
417 c->outcipher = myself->connection->outcipher;
418 c->outdigest = myself->connection->outdigest;
419 c->outmaclength = myself->connection->outmaclength;
420 c->outcompression = myself->connection->outcompression;
422 init_configuration(&c->config_tree);
423 read_connection_config(c);
425 outgoing->cfg = lookup_config(c->config_tree, "Address");
428 logger(LOG_ERR, "No address specified for %s", c->name);
433 c->outgoing = outgoing;
434 c->last_ping_time = time(NULL);
438 do_outgoing_connection(c);
440 if(!thread_create(&c->thread, handle_meta_connection_data, c)) {
441 logger(LOG_ERR, "create_thread() failed: %s", strerror(errno));
447 accept a new tcp connect and create a
450 void handle_new_meta_connection(void *data) {
451 listen_socket_t *l = data;
455 socklen_t len = sizeof sa;
458 fd = accept(l->tcp, &sa.sa, &len);
461 logger(LOG_ERR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
467 c = new_connection();
468 c->name = xstrdup("<unknown>");
469 c->outcipher = myself->connection->outcipher;
470 c->outdigest = myself->connection->outdigest;
471 c->outmaclength = myself->connection->outmaclength;
472 c->outcompression = myself->connection->outcompression;
475 c->hostname = sockaddr2hostname(&sa);
477 c->last_ping_time = time(NULL);
479 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname);
486 c->allow_request = ID;
489 if(!thread_create(&c->thread, handle_meta_connection_data, c)) {
490 logger(LOG_ERR, "create_thread() failed: %s", strerror(errno));
493 mutex_unlock(&mutex);
497 void free_outgoing(outgoing_t *outgoing) {
499 freeaddrinfo(outgoing->ai);
502 free(outgoing->name);
507 void try_outgoing_connections(void) {
508 static config_t *cfg = NULL;
510 outgoing_t *outgoing;
512 outgoing_list = list_alloc((list_action_t)free_outgoing);
514 for(cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
515 get_config_string(cfg, &name);
517 if(!check_id(name)) {
519 "Invalid name for outgoing connection in %s line %d",
520 cfg->file, cfg->line);
525 outgoing = xmalloc_and_zero(sizeof *outgoing);
526 outgoing->name = name;
527 list_insert_tail(outgoing_list, outgoing);
528 setup_outgoing_connection(outgoing);