3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
6 2010 Brandon Black <blblack@gmail.com>
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"
46 static io_t device_io;
53 proxytype_t proxytype;
56 char *scriptinterpreter;
57 char *scriptextension;
59 bool node_read_ecdsa_public_key(node_t *n) {
60 if(ecdsa_active(&n->ecdsa))
63 splay_tree_t *config_tree;
65 char *pubname = NULL, *hcfname = NULL;
69 xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
71 init_configuration(&config_tree);
72 if(!read_config_file(config_tree, hcfname))
75 /* First, check for simple ECDSAPublicKey statement */
77 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
78 result = ecdsa_set_base64_public_key(&n->ecdsa, p);
83 /* Else, check for ECDSAPublicKeyFile statement and read it */
85 if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
86 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
88 fp = fopen(pubname, "r");
91 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
95 result = ecdsa_read_pem_public_key(&n->ecdsa, fp);
99 exit_configuration(&config_tree);
105 bool read_ecdsa_public_key(connection_t *c) {
111 /* First, check for simple ECDSAPublicKey statement */
113 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
114 result = ecdsa_set_base64_public_key(&c->ecdsa, p);
119 /* Else, check for ECDSAPublicKeyFile statement and read it */
121 if(!get_config_string(lookup_config(c->config_tree, "ECDSAPublicKeyFile"), &fname))
122 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
124 fp = fopen(fname, "r");
127 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s",
128 fname, strerror(errno));
133 result = ecdsa_read_pem_public_key(&c->ecdsa, fp);
137 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname);
142 bool read_rsa_public_key(connection_t *c) {
148 /* First, check for simple PublicKey statement */
150 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
151 result = rsa_set_hex_public_key(&c->rsa, n, "FFFF");
156 /* Else, check for PublicKeyFile statement and read it */
158 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
159 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
161 fp = fopen(fname, "r");
164 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
169 result = rsa_read_pem_public_key(&c->rsa, fp);
173 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
178 static bool read_ecdsa_private_key(void) {
183 /* Check for PrivateKeyFile statement and read it */
185 if(!get_config_string(lookup_config(config_tree, "ECDSAPrivateKeyFile"), &fname))
186 xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase);
188 fp = fopen(fname, "r");
191 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file `%s': %s", fname, strerror(errno));
196 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
199 if(fstat(fileno(fp), &s)) {
200 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat ECDSA private key file `%s': %s'", fname, strerror(errno));
205 if(s.st_mode & ~0100700)
206 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname);
209 result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp);
213 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
218 static bool read_rsa_private_key(void) {
224 /* First, check for simple PrivateKey statement */
226 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
227 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
228 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
232 result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d);
238 /* Else, check for PrivateKeyFile statement and read it */
240 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
241 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
243 fp = fopen(fname, "r");
246 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
247 fname, strerror(errno));
252 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
255 if(fstat(fileno(fp), &s)) {
256 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
261 if(s.st_mode & ~0100700)
262 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
265 result = rsa_read_pem_private_key(&myself->connection->rsa, fp);
269 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
274 static timeout_t keyexpire_timeout;
276 static void keyexpire_handler(void *data) {
278 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
281 void regenerate_key(void) {
282 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
287 Read Subnets from all host config files
289 void load_all_subnets(void) {
294 xasprintf(&dname, "%s" SLASH "hosts", confbase);
295 dir = opendir(dname);
297 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
302 while((ent = readdir(dir))) {
303 if(!check_id(ent->d_name))
306 node_t *n = lookup_node(ent->d_name);
307 #ifdef _DIRENT_HAVE_D_TYPE
308 //if(ent->d_type != DT_REG)
313 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
315 splay_tree_t *config_tree;
316 init_configuration(&config_tree);
317 read_config_options(config_tree, ent->d_name);
318 read_config_file(config_tree, fname);
323 n->name = xstrdup(ent->d_name);
327 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
330 if(!get_config_subnet(cfg, &s))
333 if((s2 = lookup_subnet(n, s))) {
340 exit_configuration(&config_tree);
346 void load_all_nodes(void) {
351 xasprintf(&dname, "%s" SLASH "hosts", confbase);
352 dir = opendir(dname);
354 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
359 while((ent = readdir(dir))) {
360 if(!check_id(ent->d_name))
363 node_t *n = lookup_node(ent->d_name);
368 n->name = xstrdup(ent->d_name);
376 char *get_name(void) {
379 get_config_string(lookup_config(config_tree, "Name"), &name);
385 char *envname = getenv(name + 1);
387 if(strcmp(name + 1, "HOST")) {
388 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
392 if(gethostname(envname, 32)) {
393 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
399 name = xstrdup(envname);
400 for(char *c = name; *c; c++)
405 if(!check_id(name)) {
406 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
414 bool setup_myself_reloadable(void) {
423 free(scriptinterpreter);
424 scriptinterpreter = NULL;
425 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
428 free(scriptextension);
429 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
431 scriptextension = xstrdup(".bat");
433 scriptextension = xstrdup("");
436 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
438 if((space = strchr(proxy, ' ')))
441 if(!strcasecmp(proxy, "none")) {
442 proxytype = PROXY_NONE;
443 } else if(!strcasecmp(proxy, "socks4")) {
444 proxytype = PROXY_SOCKS4;
445 } else if(!strcasecmp(proxy, "socks4a")) {
446 proxytype = PROXY_SOCKS4A;
447 } else if(!strcasecmp(proxy, "socks5")) {
448 proxytype = PROXY_SOCKS5;
449 } else if(!strcasecmp(proxy, "http")) {
450 proxytype = PROXY_HTTP;
451 } else if(!strcasecmp(proxy, "exec")) {
452 proxytype = PROXY_EXEC;
454 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
464 if(!space || !*space) {
465 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
468 proxyhost = xstrdup(space);
476 if(space && (space = strchr(space, ' ')))
477 *space++ = 0, proxyport = space;
478 if(space && (space = strchr(space, ' ')))
479 *space++ = 0, proxyuser = space;
480 if(space && (space = strchr(space, ' ')))
481 *space++ = 0, proxypass = space;
482 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
483 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
486 proxyhost = xstrdup(proxyhost);
487 proxyport = xstrdup(proxyport);
488 if(proxyuser && *proxyuser)
489 proxyuser = xstrdup(proxyuser);
490 if(proxypass && *proxypass)
491 proxypass = xstrdup(proxypass);
498 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
499 myself->options |= OPTION_INDIRECT;
501 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
502 myself->options |= OPTION_TCPONLY;
504 if(myself->options & OPTION_TCPONLY)
505 myself->options |= OPTION_INDIRECT;
507 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
508 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
510 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
511 if(!strcasecmp(rmode, "router"))
512 routing_mode = RMODE_ROUTER;
513 else if(!strcasecmp(rmode, "switch"))
514 routing_mode = RMODE_SWITCH;
515 else if(!strcasecmp(rmode, "hub"))
516 routing_mode = RMODE_HUB;
518 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
524 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
525 if(!strcasecmp(fmode, "off"))
526 forwarding_mode = FMODE_OFF;
527 else if(!strcasecmp(fmode, "internal"))
528 forwarding_mode = FMODE_INTERNAL;
529 else if(!strcasecmp(fmode, "kernel"))
530 forwarding_mode = FMODE_KERNEL;
532 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
539 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
541 myself->options |= OPTION_PMTU_DISCOVERY;
544 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
546 myself->options |= OPTION_CLAMP_MSS;
548 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
549 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
550 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
551 if(!strcasecmp(bmode, "no"))
552 broadcast_mode = BMODE_NONE;
553 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
554 broadcast_mode = BMODE_MST;
555 else if(!strcasecmp(bmode, "direct"))
556 broadcast_mode = BMODE_DIRECT;
558 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
564 #if !defined(SOL_IP) || !defined(IP_TOS)
565 if(priorityinheritance)
566 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
569 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
572 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
573 if(maxtimeout <= 0) {
574 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
580 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
581 if(!strcasecmp(afname, "IPv4"))
582 addressfamily = AF_INET;
583 else if(!strcasecmp(afname, "IPv6"))
584 addressfamily = AF_INET6;
585 else if(!strcasecmp(afname, "any"))
586 addressfamily = AF_UNSPEC;
588 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
594 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
596 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
599 get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect);
605 Configure node_t myself and set up the local sockets (listen only)
607 static bool setup_myself(void) {
608 char *name, *hostname, *cipher, *digest, *type;
610 char *address = NULL;
612 if(!(name = get_name())) {
613 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
618 myself->connection = new_connection();
620 myself->connection->name = xstrdup(name);
621 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
622 read_config_options(config_tree, name);
623 read_config_file(config_tree, fname);
626 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
627 myport = xstrdup("655");
629 xasprintf(&myself->hostname, "MYSELF port %s", myport);
630 myself->connection->hostname = xstrdup(myself->hostname);
632 myself->connection->options = 0;
633 myself->connection->protocol_major = PROT_MAJOR;
634 myself->connection->protocol_minor = PROT_MINOR;
636 myself->options |= PROT_MINOR << 24;
638 get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental);
640 if(experimental && !read_ecdsa_private_key())
643 if(!read_rsa_private_key())
647 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
649 if(!ai || !ai->ai_addr)
652 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
653 sockaddr2str(&sa, NULL, &myport);
656 /* Read in all the subnets specified in the host configuration file */
658 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
661 if(!get_config_subnet(cfg, &subnet))
664 subnet_add(myself, subnet);
667 /* Check some options */
669 if(!setup_myself_reloadable())
672 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
673 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
674 strictsubnets |= tunnelserver;
678 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
679 if(udp_rcvbuf <= 0) {
680 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
685 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
686 if(udp_sndbuf <= 0) {
687 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
693 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
694 if(replaywin_int < 0) {
695 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
698 replaywin = (unsigned)replaywin_int;
699 sptps_replaywin = replaywin;
702 /* Generate packet encryption key */
704 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
705 cipher = xstrdup("blowfish");
707 if(!cipher_open_by_name(&myself->incipher, cipher)) {
708 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
715 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
717 /* Check if we want to use message authentication codes... */
720 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
723 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
727 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
728 digest = xstrdup("sha1");
730 if(!digest_open_by_name(&myself->indigest, digest, maclength)) {
731 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
739 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
740 if(myself->incompression < 0 || myself->incompression > 11) {
741 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
745 myself->incompression = 0;
747 myself->connection->outcompression = 0;
751 myself->nexthop = myself;
752 myself->via = myself;
753 myself->status.reachable = true;
754 myself->last_state_change = now.tv_sec;
755 myself->status.sptps = experimental;
769 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
770 if(!strcasecmp(type, "dummy"))
771 devops = dummy_devops;
772 else if(!strcasecmp(type, "raw_socket"))
773 devops = raw_socket_devops;
774 else if(!strcasecmp(type, "multicast"))
775 devops = multicast_devops;
777 else if(!strcasecmp(type, "uml"))
781 else if(!strcasecmp(type, "vde"))
790 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
792 /* Run tinc-up script to further initialize the tap interface */
794 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
795 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
796 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
797 xasprintf(&envp[3], "NAME=%s", myself->name);
800 execute_script("tinc-up", envp);
802 for(int i = 0; i < 4; i++)
805 /* Run subnet-up scripts for our own subnets */
807 subnet_update(myself, NULL, true);
812 int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0);
814 logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno));
818 struct sockaddr_un sa;
819 sa.sun_family = AF_UNIX;
820 strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path);
822 if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) {
823 logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname);
827 unlink(unixsocketname);
829 if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) {
830 logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno));
834 if(listen(unix_fd, 3) < 0) {
835 logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno));
839 io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ);
842 if(!do_detach && getenv("LISTEN_FDS")) {
846 listen_sockets = atoi(getenv("LISTEN_FDS"));
848 unsetenv("LISTEN_FDS");
851 if(listen_sockets > MAXSOCKETS) {
852 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
856 for(int i = 0; i < listen_sockets; i++) {
858 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
859 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
864 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
867 int udp_fd = setup_vpn_in_socket(&sa);
871 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
872 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
874 if(debug_level >= DEBUG_CONNECTIONS) {
875 hostname = sockaddr2hostname(&sa);
876 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
880 memcpy(&listen_socket[i].sa, &sa, salen);
884 config_t *cfg = lookup_config(config_tree, "BindToAddress");
887 get_config_string(cfg, &address);
889 cfg = lookup_config_next(config_tree, cfg);
894 char *space = strchr(address, ' ');
900 if(!strcmp(address, "*"))
904 struct addrinfo *ai, hint = {0};
905 hint.ai_family = addressfamily;
906 hint.ai_socktype = SOCK_STREAM;
907 hint.ai_protocol = IPPROTO_TCP;
908 hint.ai_flags = AI_PASSIVE;
910 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
914 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
919 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
920 if(listen_sockets >= MAXSOCKETS) {
921 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
925 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
930 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
937 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
938 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
940 if(debug_level >= DEBUG_CONNECTIONS) {
941 hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
942 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
946 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
955 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
957 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
961 last_config_check = now.tv_sec;
969 bool setup_network(void) {
976 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
977 if(pinginterval < 1) {
978 pinginterval = 86400;
983 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
985 if(pingtimeout < 1 || pingtimeout > pinginterval)
986 pingtimeout = pinginterval;
988 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
989 maxoutbufsize = 10 * MTU;
998 close all open network connections
1000 void close_network_connections(void) {
1001 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1003 connection_t *c = node->data;
1004 /* Keep control connections open until the end, so they know when we really terminated */
1005 if(c->status.control)
1008 terminate_connection(c, false);
1011 list_delete_list(outgoing_list);
1013 if(myself && myself->connection) {
1014 subnet_update(myself, NULL, false);
1015 terminate_connection(myself->connection, false);
1016 free_connection(myself->connection);
1019 for(int i = 0; i < listen_sockets; i++) {
1020 io_del(&listen_socket[i].tcp);
1021 io_del(&listen_socket[i].udp);
1022 close(listen_socket[i].tcp.fd);
1023 close(listen_socket[i].udp.fd);
1027 io_del(&unix_socket);
1028 close(unix_socket.fd);
1032 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
1033 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
1034 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
1035 xasprintf(&envp[3], "NAME=%s", myself->name);
1044 execute_script("tinc-down", envp);
1046 if(myport) free(myport);
1048 for(int i = 0; i < 4; i++)