3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2014 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"
48 static io_t device_io;
50 bool device_standby = false;
56 proxytype_t proxytype;
58 bool disablebuggypeers;
60 char *scriptinterpreter;
61 char *scriptextension;
63 bool node_read_ecdsa_public_key(node_t *n) {
64 if(ecdsa_active(n->ecdsa))
67 splay_tree_t *config_tree;
72 init_configuration(&config_tree);
73 if(!read_host_config(config_tree, n->name))
76 /* First, check for simple Ed25519PublicKey statement */
78 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
79 n->ecdsa = ecdsa_set_base64_public_key(p);
84 /* Else, check for Ed25519PublicKeyFile statement and read it */
86 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname))
87 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
89 fp = fopen(pubname, "r");
94 n->ecdsa = ecdsa_read_pem_public_key(fp);
98 exit_configuration(&config_tree);
103 bool read_ecdsa_public_key(connection_t *c) {
104 if(ecdsa_active(c->ecdsa))
111 if(!c->config_tree) {
112 init_configuration(&c->config_tree);
113 if(!read_host_config(c->config_tree, c->name))
117 /* First, check for simple Ed25519PublicKey statement */
119 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
120 c->ecdsa = ecdsa_set_base64_public_key(p);
125 /* Else, check for Ed25519PublicKeyFile statement and read it */
127 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname))
128 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
130 fp = fopen(fname, "r");
133 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
134 fname, strerror(errno));
139 c->ecdsa = ecdsa_read_pem_public_key(fp);
143 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
148 bool read_rsa_public_key(connection_t *c) {
149 if(ecdsa_active(c->ecdsa))
156 /* First, check for simple PublicKey statement */
158 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
159 c->rsa = rsa_set_hex_public_key(n, "FFFF");
164 /* Else, check for PublicKeyFile statement and read it */
166 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
167 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
169 fp = fopen(fname, "r");
172 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
177 c->rsa = rsa_read_pem_public_key(fp);
181 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
186 static bool read_ecdsa_private_key(void) {
190 /* Check for PrivateKeyFile statement and read it */
192 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
193 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
195 fp = fopen(fname, "r");
198 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
200 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
205 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
208 if(fstat(fileno(fp), &s)) {
209 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
214 if(s.st_mode & ~0100700)
215 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
218 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
221 if(!myself->connection->ecdsa)
222 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
224 return myself->connection->ecdsa;
227 static bool read_invitation_key(void) {
232 ecdsa_free(invitation_key);
233 invitation_key = NULL;
236 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
238 fp = fopen(fname, "r");
241 invitation_key = ecdsa_read_pem_private_key(fp);
244 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
248 return invitation_key;
251 static bool read_rsa_private_key(void) {
256 /* First, check for simple PrivateKey statement */
258 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
259 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
264 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
267 return myself->connection->rsa;
270 /* Else, check for PrivateKeyFile statement and read it */
272 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
273 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
275 fp = fopen(fname, "r");
278 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
279 fname, strerror(errno));
284 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
287 if(fstat(fileno(fp), &s)) {
288 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
293 if(s.st_mode & ~0100700)
294 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
297 myself->connection->rsa = rsa_read_pem_private_key(fp);
300 if(!myself->connection->rsa)
301 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
303 return myself->connection->rsa;
306 static timeout_t keyexpire_timeout;
308 static void keyexpire_handler(void *data) {
310 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
313 void regenerate_key(void) {
314 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
319 Read Subnets from all host config files
321 void load_all_subnets(void) {
326 xasprintf(&dname, "%s" SLASH "hosts", confbase);
327 dir = opendir(dname);
329 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
334 while((ent = readdir(dir))) {
335 if(!check_id(ent->d_name))
338 node_t *n = lookup_node(ent->d_name);
339 #ifdef _DIRENT_HAVE_D_TYPE
340 //if(ent->d_type != DT_REG)
344 splay_tree_t *config_tree;
345 init_configuration(&config_tree);
346 read_config_options(config_tree, ent->d_name);
347 read_host_config(config_tree, ent->d_name);
351 n->name = xstrdup(ent->d_name);
355 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
358 if(!get_config_subnet(cfg, &s))
361 if((s2 = lookup_subnet(n, s))) {
368 exit_configuration(&config_tree);
374 void load_all_nodes(void) {
379 xasprintf(&dname, "%s" SLASH "hosts", confbase);
380 dir = opendir(dname);
382 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
387 while((ent = readdir(dir))) {
388 if(!check_id(ent->d_name))
391 node_t *n = lookup_node(ent->d_name);
396 n->name = xstrdup(ent->d_name);
404 char *get_name(void) {
408 get_config_string(lookup_config(config_tree, "Name"), &name);
413 returned_name = replace_name(name);
415 return returned_name;
418 bool setup_myself_reloadable(void) {
427 free(scriptinterpreter);
428 scriptinterpreter = NULL;
429 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
432 free(scriptextension);
433 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
434 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 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
565 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
566 subnet_t *s = new_subnet();
567 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
571 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
573 if (!get_config_subnet(cfg, &s))
578 #if !defined(SOL_IP) || !defined(IP_TOS)
579 if(priorityinheritance)
580 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
583 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
586 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
587 if(maxtimeout <= 0) {
588 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
594 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
595 if(!strcasecmp(afname, "IPv4"))
596 addressfamily = AF_INET;
597 else if(!strcasecmp(afname, "IPv6"))
598 addressfamily = AF_INET6;
599 else if(!strcasecmp(afname, "any"))
600 addressfamily = AF_UNSPEC;
602 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
608 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
610 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
613 config_t *cfg = lookup_config(config_tree, "AutoConnect");
615 if(!get_config_bool(cfg, &autoconnect)) {
616 // Some backwards compatibility with when this option was an int
618 get_config_int(cfg, &val);
623 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
625 read_invitation_key();
631 Add listening sockets.
633 static bool add_listen_address(char *address, bool bindto) {
637 char *space = strchr(address, ' ');
643 if(!strcmp(address, "*"))
647 struct addrinfo *ai, hint = {0};
648 hint.ai_family = addressfamily;
649 hint.ai_socktype = SOCK_STREAM;
650 hint.ai_protocol = IPPROTO_TCP;
651 hint.ai_flags = AI_PASSIVE;
653 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
657 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
661 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
662 // Ignore duplicate addresses
665 for(int i = 0; i < listen_sockets; i++)
666 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
674 if(listen_sockets >= MAXSOCKETS) {
675 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
679 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
684 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
691 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
692 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
694 if(debug_level >= DEBUG_CONNECTIONS) {
695 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
696 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
700 listen_socket[listen_sockets].bindto = bindto;
701 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
709 void device_enable(void) {
713 /* Run tinc-up script to further initialize the tap interface */
715 char *envp[5] = {NULL};
716 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
717 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
718 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
719 xasprintf(&envp[3], "NAME=%s", myname);
721 execute_script("tinc-up", envp);
723 for(int i = 0; i < 4; i++)
727 void device_disable(void) {
728 char *envp[5] = {NULL};
729 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
730 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
731 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
732 xasprintf(&envp[3], "NAME=%s", myname);
734 execute_script("tinc-down", envp);
736 for(int i = 0; i < 4; i++)
744 Configure node_t myself and set up the local sockets (listen only)
746 static bool setup_myself(void) {
747 char *name, *hostname, *cipher, *digest, *type;
748 char *address = NULL;
749 bool port_specified = false;
751 if(!(name = get_name())) {
752 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
756 myname = xstrdup(name);
758 myself->connection = new_connection();
760 myself->connection->name = xstrdup(name);
761 read_host_config(config_tree, name);
763 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
764 myport = xstrdup("655");
766 port_specified = true;
768 myself->connection->options = 0;
769 myself->connection->protocol_major = PROT_MAJOR;
770 myself->connection->protocol_minor = PROT_MINOR;
772 myself->options |= PROT_MINOR << 24;
774 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
775 experimental = read_ecdsa_private_key();
777 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
779 if(experimental && !read_ecdsa_private_key())
783 if(!read_rsa_private_key())
786 /* Ensure myport is numeric */
789 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
791 if(!ai || !ai->ai_addr)
794 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
795 sockaddr2str(&sa, NULL, &myport);
798 /* Read in all the subnets specified in the host configuration file */
800 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
803 if(!get_config_subnet(cfg, &subnet))
806 subnet_add(myself, subnet);
809 /* Check some options */
811 if(!setup_myself_reloadable())
814 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
815 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
816 strictsubnets |= tunnelserver;
818 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
819 if(max_connection_burst <= 0) {
820 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
825 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
826 if(udp_rcvbuf <= 0) {
827 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
832 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
833 if(udp_sndbuf <= 0) {
834 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
840 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
841 if(replaywin_int < 0) {
842 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
845 replaywin = (unsigned)replaywin_int;
846 sptps_replaywin = replaywin;
849 /* Generate packet encryption key */
851 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
852 cipher = xstrdup("blowfish");
854 if(!strcasecmp(cipher, "none")) {
855 myself->incipher = NULL;
856 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
857 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
863 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
865 /* Check if we want to use message authentication codes... */
868 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
871 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
875 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
876 digest = xstrdup("sha1");
878 if(!strcasecmp(digest, "none")) {
879 myself->indigest = NULL;
880 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
881 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
889 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
890 if(myself->incompression < 0 || myself->incompression > 11) {
891 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
895 myself->incompression = 0;
897 myself->connection->outcompression = 0;
901 myself->nexthop = myself;
902 myself->via = myself;
903 myself->status.reachable = true;
904 myself->last_state_change = now.tv_sec;
905 myself->status.sptps = experimental;
919 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
920 if(!strcasecmp(type, "dummy"))
921 devops = dummy_devops;
922 else if(!strcasecmp(type, "raw_socket"))
923 devops = raw_socket_devops;
924 else if(!strcasecmp(type, "multicast"))
925 devops = multicast_devops;
927 else if(!strcasecmp(type, "uml"))
931 else if(!strcasecmp(type, "vde"))
936 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
942 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
946 if(!do_detach && getenv("LISTEN_FDS")) {
950 listen_sockets = atoi(getenv("LISTEN_FDS"));
952 unsetenv("LISTEN_FDS");
955 if(listen_sockets > MAXSOCKETS) {
956 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
960 for(int i = 0; i < listen_sockets; i++) {
962 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
963 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
968 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
971 int udp_fd = setup_vpn_in_socket(&sa);
975 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
976 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
978 if(debug_level >= DEBUG_CONNECTIONS) {
979 hostname = sockaddr2hostname(&sa);
980 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
984 memcpy(&listen_socket[i].sa, &sa, salen);
990 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
992 get_config_string(cfg, &address);
993 if(!add_listen_address(address, true))
997 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
999 get_config_string(cfg, &address);
1000 if(!add_listen_address(address, false))
1005 if(!add_listen_address(address, NULL))
1009 if(!listen_sockets) {
1010 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1014 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1016 if(!port_specified || atoi(myport) == 0) {
1018 socklen_t salen = sizeof sa;
1019 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1021 sockaddr2str(&sa, NULL, &myport);
1023 myport = xstrdup("655");
1027 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1028 myself->connection->hostname = xstrdup(myself->hostname);
1032 last_config_check = now.tv_sec;
1040 bool setup_network(void) {
1047 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1048 if(pinginterval < 1) {
1049 pinginterval = 86400;
1054 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1056 if(pingtimeout < 1 || pingtimeout > pinginterval)
1057 pingtimeout = pinginterval;
1059 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1060 maxoutbufsize = 10 * MTU;
1068 if (!device_standby)
1071 /* Run subnet-up scripts for our own subnets */
1073 subnet_update(myself, NULL, true);
1079 close all open network connections
1081 void close_network_connections(void) {
1082 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1084 connection_t *c = node->data;
1085 /* Keep control connections open until the end, so they know when we really terminated */
1086 if(c->status.control)
1089 terminate_connection(c, false);
1093 list_delete_list(outgoing_list);
1095 if(myself && myself->connection) {
1096 subnet_update(myself, NULL, false);
1097 terminate_connection(myself->connection, false);
1098 free_connection(myself->connection);
1101 for(int i = 0; i < listen_sockets; i++) {
1102 io_del(&listen_socket[i].tcp);
1103 io_del(&listen_socket[i].udp);
1104 close(listen_socket[i].tcp.fd);
1105 close(listen_socket[i].udp.fd);
1114 if (!device_standby)
1117 if(myport) free(myport);