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"
47 static io_t device_io;
49 bool device_standby = false;
55 proxytype_t proxytype;
57 bool disablebuggypeers;
59 char *scriptinterpreter;
60 char *scriptextension;
62 bool node_read_ecdsa_public_key(node_t *n) {
63 if(ecdsa_active(n->ecdsa))
66 splay_tree_t *config_tree;
71 init_configuration(&config_tree);
72 if(!read_host_config(config_tree, n->name))
75 /* First, check for simple Ed25519PublicKey statement */
77 if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) {
78 n->ecdsa = ecdsa_set_base64_public_key(p);
83 /* Else, check for Ed25519PublicKeyFile statement and read it */
85 if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname))
86 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
88 fp = fopen(pubname, "r");
93 n->ecdsa = ecdsa_read_pem_public_key(fp);
97 exit_configuration(&config_tree);
102 bool read_ecdsa_public_key(connection_t *c) {
103 if(ecdsa_active(c->ecdsa))
110 if(!c->config_tree) {
111 init_configuration(&c->config_tree);
112 if(!read_host_config(c->config_tree, c->name))
116 /* First, check for simple Ed25519PublicKey statement */
118 if(get_config_string(lookup_config(c->config_tree, "Ed25519PublicKey"), &p)) {
119 c->ecdsa = ecdsa_set_base64_public_key(p);
124 /* Else, check for Ed25519PublicKeyFile statement and read it */
126 if(!get_config_string(lookup_config(c->config_tree, "Ed25519PublicKeyFile"), &fname))
127 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
129 fp = fopen(fname, "r");
132 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 public key file `%s': %s",
133 fname, strerror(errno));
138 c->ecdsa = ecdsa_read_pem_public_key(fp);
142 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
147 bool read_rsa_public_key(connection_t *c) {
148 if(ecdsa_active(c->ecdsa))
155 /* First, check for simple PublicKey statement */
157 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
158 c->rsa = rsa_set_hex_public_key(n, "FFFF");
163 /* Else, check for PublicKeyFile statement and read it */
165 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
166 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
168 fp = fopen(fname, "r");
171 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
176 c->rsa = rsa_read_pem_public_key(fp);
180 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
185 static bool read_ecdsa_private_key(void) {
189 /* Check for PrivateKeyFile statement and read it */
191 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
192 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
194 fp = fopen(fname, "r");
197 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
199 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
204 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
207 if(fstat(fileno(fp), &s)) {
208 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
213 if(s.st_mode & ~0100700)
214 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
217 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
220 if(!myself->connection->ecdsa)
221 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
223 return myself->connection->ecdsa;
226 static bool read_invitation_key(void) {
231 ecdsa_free(invitation_key);
232 invitation_key = NULL;
235 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
237 fp = fopen(fname, "r");
240 invitation_key = ecdsa_read_pem_private_key(fp);
243 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
247 return invitation_key;
250 static bool read_rsa_private_key(void) {
255 /* First, check for simple PrivateKey statement */
257 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
258 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
259 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
263 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
266 return myself->connection->rsa;
269 /* Else, check for PrivateKeyFile statement and read it */
271 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
272 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
274 fp = fopen(fname, "r");
277 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
278 fname, strerror(errno));
283 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
286 if(fstat(fileno(fp), &s)) {
287 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
292 if(s.st_mode & ~0100700)
293 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
296 myself->connection->rsa = rsa_read_pem_private_key(fp);
299 if(!myself->connection->rsa)
300 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
302 return myself->connection->rsa;
305 static timeout_t keyexpire_timeout;
307 static void keyexpire_handler(void *data) {
309 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
312 void regenerate_key(void) {
313 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
318 Read Subnets from all host config files
320 void load_all_subnets(void) {
325 xasprintf(&dname, "%s" SLASH "hosts", confbase);
326 dir = opendir(dname);
328 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
333 while((ent = readdir(dir))) {
334 if(!check_id(ent->d_name))
337 node_t *n = lookup_node(ent->d_name);
338 #ifdef _DIRENT_HAVE_D_TYPE
339 //if(ent->d_type != DT_REG)
343 splay_tree_t *config_tree;
344 init_configuration(&config_tree);
345 read_config_options(config_tree, ent->d_name);
346 read_host_config(config_tree, ent->d_name);
350 n->name = xstrdup(ent->d_name);
354 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
357 if(!get_config_subnet(cfg, &s))
360 if((s2 = lookup_subnet(n, s))) {
367 exit_configuration(&config_tree);
373 void load_all_nodes(void) {
378 xasprintf(&dname, "%s" SLASH "hosts", confbase);
379 dir = opendir(dname);
381 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
386 while((ent = readdir(dir))) {
387 if(!check_id(ent->d_name))
390 node_t *n = lookup_node(ent->d_name);
395 n->name = xstrdup(ent->d_name);
403 char *get_name(void) {
406 get_config_string(lookup_config(config_tree, "Name"), &name);
412 char *envname = getenv(name + 1);
413 char hostname[32] = "";
415 if(strcmp(name + 1, "HOST")) {
416 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
419 if(gethostname(hostname, sizeof hostname) || !*hostname) {
420 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", strerror(errno));
427 name = xstrdup(envname);
428 for(char *c = name; *c; c++)
433 if(!check_id(name)) {
434 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
442 bool setup_myself_reloadable(void) {
448 char *address = NULL;
452 free(scriptinterpreter);
453 scriptinterpreter = NULL;
454 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
457 free(scriptextension);
458 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
459 scriptextension = xstrdup("");
461 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
463 if((space = strchr(proxy, ' ')))
466 if(!strcasecmp(proxy, "none")) {
467 proxytype = PROXY_NONE;
468 } else if(!strcasecmp(proxy, "socks4")) {
469 proxytype = PROXY_SOCKS4;
470 } else if(!strcasecmp(proxy, "socks4a")) {
471 proxytype = PROXY_SOCKS4A;
472 } else if(!strcasecmp(proxy, "socks5")) {
473 proxytype = PROXY_SOCKS5;
474 } else if(!strcasecmp(proxy, "http")) {
475 proxytype = PROXY_HTTP;
476 } else if(!strcasecmp(proxy, "exec")) {
477 proxytype = PROXY_EXEC;
479 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
489 if(!space || !*space) {
490 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
493 proxyhost = xstrdup(space);
501 if(space && (space = strchr(space, ' ')))
502 *space++ = 0, proxyport = space;
503 if(space && (space = strchr(space, ' ')))
504 *space++ = 0, proxyuser = space;
505 if(space && (space = strchr(space, ' ')))
506 *space++ = 0, proxypass = space;
507 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
508 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
511 proxyhost = xstrdup(proxyhost);
512 proxyport = xstrdup(proxyport);
513 if(proxyuser && *proxyuser)
514 proxyuser = xstrdup(proxyuser);
515 if(proxypass && *proxypass)
516 proxypass = xstrdup(proxypass);
523 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
524 myself->options |= OPTION_INDIRECT;
526 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
527 myself->options |= OPTION_TCPONLY;
529 if(myself->options & OPTION_TCPONLY)
530 myself->options |= OPTION_INDIRECT;
532 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
533 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
535 memset(&localdiscovery_address, 0, sizeof localdiscovery_address);
536 if(get_config_string(lookup_config(config_tree, "LocalDiscoveryAddress"), &address)) {
537 struct addrinfo *ai = str2addrinfo(address, myport, SOCK_DGRAM);
541 memcpy(&localdiscovery_address, ai->ai_addr, ai->ai_addrlen);
545 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
546 if(!strcasecmp(rmode, "router"))
547 routing_mode = RMODE_ROUTER;
548 else if(!strcasecmp(rmode, "switch"))
549 routing_mode = RMODE_SWITCH;
550 else if(!strcasecmp(rmode, "hub"))
551 routing_mode = RMODE_HUB;
553 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
559 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
560 if(!strcasecmp(fmode, "off"))
561 forwarding_mode = FMODE_OFF;
562 else if(!strcasecmp(fmode, "internal"))
563 forwarding_mode = FMODE_INTERNAL;
564 else if(!strcasecmp(fmode, "kernel"))
565 forwarding_mode = FMODE_KERNEL;
567 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
574 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
576 myself->options |= OPTION_PMTU_DISCOVERY;
579 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
581 myself->options |= OPTION_CLAMP_MSS;
583 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
584 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
585 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
586 if(!strcasecmp(bmode, "no"))
587 broadcast_mode = BMODE_NONE;
588 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
589 broadcast_mode = BMODE_MST;
590 else if(!strcasecmp(bmode, "direct"))
591 broadcast_mode = BMODE_DIRECT;
593 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
599 #if !defined(SOL_IP) || !defined(IP_TOS)
600 if(priorityinheritance)
601 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
604 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
607 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
608 if(maxtimeout <= 0) {
609 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
615 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
616 if(!strcasecmp(afname, "IPv4"))
617 addressfamily = AF_INET;
618 else if(!strcasecmp(afname, "IPv6"))
619 addressfamily = AF_INET6;
620 else if(!strcasecmp(afname, "any"))
621 addressfamily = AF_UNSPEC;
623 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
629 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
631 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
634 config_t *cfg = lookup_config(config_tree, "AutoConnect");
636 if(!get_config_bool(cfg, &autoconnect)) {
637 // Some backwards compatibility with when this option was an int
639 get_config_int(cfg, &val);
644 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
646 read_invitation_key();
652 Add listening sockets.
654 static bool add_listen_address(char *address, bool bindto) {
658 char *space = strchr(address, ' ');
664 if(!strcmp(address, "*"))
668 struct addrinfo *ai, hint = {0};
669 hint.ai_family = addressfamily;
670 hint.ai_socktype = SOCK_STREAM;
671 hint.ai_protocol = IPPROTO_TCP;
672 hint.ai_flags = AI_PASSIVE;
674 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
678 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
682 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
683 // Ignore duplicate addresses
686 for(int i = 0; i < listen_sockets; i++)
687 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
695 if(listen_sockets >= MAXSOCKETS) {
696 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
700 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
705 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
712 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
713 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
715 if(debug_level >= DEBUG_CONNECTIONS) {
716 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
717 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
721 listen_socket[listen_sockets].bindto = bindto;
722 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
730 void device_enable(void) {
731 /* Run tinc-up script to further initialize the tap interface */
733 char *envp[5] = {NULL};
734 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
735 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
736 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
737 xasprintf(&envp[3], "NAME=%s", myself->name);
739 execute_script("tinc-up", envp);
741 for(int i = 0; i < 4; i++)
745 void device_disable(void) {
746 char *envp[5] = {NULL};
747 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
748 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
749 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
750 xasprintf(&envp[3], "NAME=%s", myself->name);
752 execute_script("tinc-down", envp);
754 for(int i = 0; i < 4; i++)
759 Configure node_t myself and set up the local sockets (listen only)
761 static bool setup_myself(void) {
762 char *name, *hostname, *cipher, *digest, *type;
763 char *address = NULL;
764 bool port_specified = false;
766 if(!(name = get_name())) {
767 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
772 myself->connection = new_connection();
774 myself->connection->name = xstrdup(name);
775 read_host_config(config_tree, name);
777 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
778 myport = xstrdup("655");
780 port_specified = true;
782 myself->connection->options = 0;
783 myself->connection->protocol_major = PROT_MAJOR;
784 myself->connection->protocol_minor = PROT_MINOR;
786 myself->options |= PROT_MINOR << 24;
788 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
789 experimental = read_ecdsa_private_key();
791 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
793 if(experimental && !read_ecdsa_private_key())
797 if(!read_rsa_private_key())
800 /* Ensure myport is numeric */
803 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
805 if(!ai || !ai->ai_addr)
808 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
809 sockaddr2str(&sa, NULL, &myport);
812 /* Read in all the subnets specified in the host configuration file */
814 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
817 if(!get_config_subnet(cfg, &subnet))
820 subnet_add(myself, subnet);
823 /* Check some options */
825 if(!setup_myself_reloadable())
828 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
829 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
830 strictsubnets |= tunnelserver;
832 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
833 if(max_connection_burst <= 0) {
834 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
839 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
840 if(udp_rcvbuf <= 0) {
841 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
846 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
847 if(udp_sndbuf <= 0) {
848 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
854 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
855 if(replaywin_int < 0) {
856 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
859 replaywin = (unsigned)replaywin_int;
860 sptps_replaywin = replaywin;
863 /* Generate packet encryption key */
865 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
866 cipher = xstrdup("blowfish");
868 if(!strcasecmp(cipher, "none")) {
869 myself->incipher = NULL;
870 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
871 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
877 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
879 /* Check if we want to use message authentication codes... */
882 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
885 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
889 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
890 digest = xstrdup("sha1");
892 if(!strcasecmp(digest, "none")) {
893 myself->indigest = NULL;
894 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
895 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
903 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
904 if(myself->incompression < 0 || myself->incompression > 11) {
905 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
909 myself->incompression = 0;
911 myself->connection->outcompression = 0;
915 myself->nexthop = myself;
916 myself->via = myself;
917 myself->status.reachable = true;
918 myself->last_state_change = now.tv_sec;
919 myself->status.sptps = experimental;
933 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
934 if(!strcasecmp(type, "dummy"))
935 devops = dummy_devops;
936 else if(!strcasecmp(type, "raw_socket"))
937 devops = raw_socket_devops;
938 else if(!strcasecmp(type, "multicast"))
939 devops = multicast_devops;
941 else if(!strcasecmp(type, "uml"))
945 else if(!strcasecmp(type, "vde"))
950 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
956 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
960 if(!do_detach && getenv("LISTEN_FDS")) {
964 listen_sockets = atoi(getenv("LISTEN_FDS"));
966 unsetenv("LISTEN_FDS");
969 if(listen_sockets > MAXSOCKETS) {
970 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
974 for(int i = 0; i < listen_sockets; i++) {
976 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
977 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
982 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
985 int udp_fd = setup_vpn_in_socket(&sa);
989 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
990 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
992 if(debug_level >= DEBUG_CONNECTIONS) {
993 hostname = sockaddr2hostname(&sa);
994 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
998 memcpy(&listen_socket[i].sa, &sa, salen);
1004 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1006 get_config_string(cfg, &address);
1007 if(!add_listen_address(address, true))
1011 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1013 get_config_string(cfg, &address);
1014 if(!add_listen_address(address, false))
1019 if(!add_listen_address(address, NULL))
1023 if(!listen_sockets) {
1024 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1028 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1030 if(!port_specified) {
1032 socklen_t salen = sizeof sa;
1033 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1035 sockaddr2str(&sa, NULL, &myport);
1037 myport = xstrdup("655");
1041 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1042 myself->connection->hostname = xstrdup(myself->hostname);
1046 last_config_check = now.tv_sec;
1054 bool setup_network(void) {
1061 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1062 if(pinginterval < 1) {
1063 pinginterval = 86400;
1068 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1070 if(pingtimeout < 1 || pingtimeout > pinginterval)
1071 pingtimeout = pinginterval;
1073 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1074 maxoutbufsize = 10 * MTU;
1082 if (!device_standby)
1085 /* Run subnet-up scripts for our own subnets */
1087 subnet_update(myself, NULL, true);
1093 close all open network connections
1095 void close_network_connections(void) {
1096 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1098 connection_t *c = node->data;
1099 /* Keep control connections open until the end, so they know when we really terminated */
1100 if(c->status.control)
1103 terminate_connection(c, false);
1107 list_delete_list(outgoing_list);
1109 if(myself && myself->connection) {
1110 subnet_update(myself, NULL, false);
1111 terminate_connection(myself->connection, false);
1112 free_connection(myself->connection);
1115 for(int i = 0; i < listen_sockets; i++) {
1116 io_del(&listen_socket[i].tcp);
1117 io_del(&listen_socket[i].udp);
1118 close(listen_socket[i].tcp.fd);
1119 close(listen_socket[i].udp.fd);
1128 if (!device_standby)
1131 if(myport) free(myport);