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);
141 if(!c->ecdsa && errno != ENOENT)
142 logger(DEBUG_ALWAYS, LOG_ERR, "Parsing Ed25519 public key file `%s' failed.", fname);
149 #ifndef DISABLE_LEGACY
150 bool read_rsa_public_key(connection_t *c) {
151 if(ecdsa_active(c->ecdsa))
158 /* First, check for simple PublicKey statement */
160 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) {
161 c->rsa = rsa_set_hex_public_key(n, "FFFF");
166 /* Else, check for PublicKeyFile statement and read it */
168 if(!get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
169 xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
171 fp = fopen(fname, "r");
174 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA public key file `%s': %s", fname, strerror(errno));
179 c->rsa = rsa_read_pem_public_key(fp);
183 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno));
189 static bool read_ecdsa_private_key(void) {
193 /* Check for PrivateKeyFile statement and read it */
195 if(!get_config_string(lookup_config(config_tree, "Ed25519PrivateKeyFile"), &fname))
196 xasprintf(&fname, "%s" SLASH "ed25519_key.priv", confbase);
198 fp = fopen(fname, "r");
201 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading Ed25519 private key file `%s': %s", fname, strerror(errno));
203 logger(DEBUG_ALWAYS, LOG_INFO, "Create an Ed25519 keypair with `tinc -n %s generate-ed25519-keys'.", netname ?: ".");
208 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
211 if(fstat(fileno(fp), &s)) {
212 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat Ed25519 private key file `%s': %s'", fname, strerror(errno));
217 if(s.st_mode & ~0100700)
218 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for Ed25519 private key file `%s'!", fname);
221 myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
224 if(!myself->connection->ecdsa)
225 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
227 return myself->connection->ecdsa;
230 static bool read_invitation_key(void) {
235 ecdsa_free(invitation_key);
236 invitation_key = NULL;
239 xasprintf(&fname, "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
241 fp = fopen(fname, "r");
244 invitation_key = ecdsa_read_pem_private_key(fp);
247 logger(DEBUG_ALWAYS, LOG_ERR, "Reading Ed25519 private key file `%s' failed", fname);
251 return invitation_key;
254 #ifndef DISABLE_LEGACY
255 static bool read_rsa_private_key(void) {
260 /* First, check for simple PrivateKey statement */
262 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &d)) {
263 if(!get_config_string(lookup_config(config_tree, "PublicKey"), &n)) {
264 logger(DEBUG_ALWAYS, LOG_ERR, "PrivateKey used but no PublicKey found!");
268 myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d);
271 return myself->connection->rsa;
274 /* Else, check for PrivateKeyFile statement and read it */
276 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
277 xasprintf(&fname, "%s" SLASH "rsa_key.priv", confbase);
279 fp = fopen(fname, "r");
282 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading RSA private key file `%s': %s",
283 fname, strerror(errno));
285 logger(DEBUG_ALWAYS, LOG_INFO, "Create an RSA keypair with `tinc -n %s generate-rsa-keys'.", netname ?: ".");
290 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
293 if(fstat(fileno(fp), &s)) {
294 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
299 if(s.st_mode & ~0100700)
300 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
303 myself->connection->rsa = rsa_read_pem_private_key(fp);
306 if(!myself->connection->rsa)
307 logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno));
309 return myself->connection->rsa;
313 static timeout_t keyexpire_timeout;
315 static void keyexpire_handler(void *data) {
317 timeout_set(data, &(struct timeval){keylifetime, rand() % 100000});
320 void regenerate_key(void) {
321 logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys");
323 for splay_each(node_t, n, node_tree)
324 n->status.validkey_in = false;
328 Read Subnets from all host config files
330 void load_all_subnets(void) {
335 xasprintf(&dname, "%s" SLASH "hosts", confbase);
336 dir = opendir(dname);
338 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
343 while((ent = readdir(dir))) {
344 if(!check_id(ent->d_name))
347 node_t *n = lookup_node(ent->d_name);
348 #ifdef _DIRENT_HAVE_D_TYPE
349 //if(ent->d_type != DT_REG)
353 splay_tree_t *config_tree;
354 init_configuration(&config_tree);
355 read_config_options(config_tree, ent->d_name);
356 read_host_config(config_tree, ent->d_name);
360 n->name = xstrdup(ent->d_name);
364 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
367 if(!get_config_subnet(cfg, &s))
370 if((s2 = lookup_subnet(n, s))) {
377 exit_configuration(&config_tree);
383 void load_all_nodes(void) {
388 xasprintf(&dname, "%s" SLASH "hosts", confbase);
389 dir = opendir(dname);
391 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
396 while((ent = readdir(dir))) {
397 if(!check_id(ent->d_name))
400 node_t *n = lookup_node(ent->d_name);
405 n->name = xstrdup(ent->d_name);
413 char *get_name(void) {
417 get_config_string(lookup_config(config_tree, "Name"), &name);
422 returned_name = replace_name(name);
424 return returned_name;
427 bool setup_myself_reloadable(void) {
436 free(scriptinterpreter);
437 scriptinterpreter = NULL;
438 get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
441 free(scriptextension);
442 if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
443 scriptextension = xstrdup("");
445 get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
447 if((space = strchr(proxy, ' ')))
450 if(!strcasecmp(proxy, "none")) {
451 proxytype = PROXY_NONE;
452 } else if(!strcasecmp(proxy, "socks4")) {
453 proxytype = PROXY_SOCKS4;
454 } else if(!strcasecmp(proxy, "socks4a")) {
455 proxytype = PROXY_SOCKS4A;
456 } else if(!strcasecmp(proxy, "socks5")) {
457 proxytype = PROXY_SOCKS5;
458 } else if(!strcasecmp(proxy, "http")) {
459 proxytype = PROXY_HTTP;
460 } else if(!strcasecmp(proxy, "exec")) {
461 proxytype = PROXY_EXEC;
463 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type %s!", proxy);
473 if(!space || !*space) {
474 logger(DEBUG_ALWAYS, LOG_ERR, "Argument expected for proxy type exec!");
477 proxyhost = xstrdup(space);
485 if(space && (space = strchr(space, ' ')))
486 *space++ = 0, proxyport = space;
487 if(space && (space = strchr(space, ' ')))
488 *space++ = 0, proxyuser = space;
489 if(space && (space = strchr(space, ' ')))
490 *space++ = 0, proxypass = space;
491 if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
492 logger(DEBUG_ALWAYS, LOG_ERR, "Host and port argument expected for proxy!");
495 proxyhost = xstrdup(proxyhost);
496 proxyport = xstrdup(proxyport);
497 if(proxyuser && *proxyuser)
498 proxyuser = xstrdup(proxyuser);
499 if(proxypass && *proxypass)
500 proxypass = xstrdup(proxypass);
507 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice)
508 myself->options |= OPTION_INDIRECT;
510 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice)
511 myself->options |= OPTION_TCPONLY;
513 if(myself->options & OPTION_TCPONLY)
514 myself->options |= OPTION_INDIRECT;
516 get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery);
517 get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval);
518 get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval);
519 get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout);
521 get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
522 get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
524 if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
525 if(!strcasecmp(rmode, "router"))
526 routing_mode = RMODE_ROUTER;
527 else if(!strcasecmp(rmode, "switch"))
528 routing_mode = RMODE_SWITCH;
529 else if(!strcasecmp(rmode, "hub"))
530 routing_mode = RMODE_HUB;
532 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
538 if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
539 if(!strcasecmp(fmode, "off"))
540 forwarding_mode = FMODE_OFF;
541 else if(!strcasecmp(fmode, "internal"))
542 forwarding_mode = FMODE_INTERNAL;
543 else if(!strcasecmp(fmode, "kernel"))
544 forwarding_mode = FMODE_KERNEL;
546 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
553 get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
555 myself->options |= OPTION_PMTU_DISCOVERY;
558 get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
560 myself->options |= OPTION_CLAMP_MSS;
562 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
563 get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
564 if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
565 if(!strcasecmp(bmode, "no"))
566 broadcast_mode = BMODE_NONE;
567 else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
568 broadcast_mode = BMODE_MST;
569 else if(!strcasecmp(bmode, "direct"))
570 broadcast_mode = BMODE_DIRECT;
572 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
578 const char* const DEFAULT_BROADCAST_SUBNETS[] = { "ff:ff:ff:ff:ff:ff", "255.255.255.255", "224.0.0.0/4", "ff00::/8" };
579 for (size_t i = 0; i < sizeof(DEFAULT_BROADCAST_SUBNETS) / sizeof(*DEFAULT_BROADCAST_SUBNETS); i++) {
580 subnet_t *s = new_subnet();
581 if (!str2net(s, DEFAULT_BROADCAST_SUBNETS[i]))
585 for (config_t* cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
587 if (!get_config_subnet(cfg, &s))
592 #if !defined(SOL_IP) || !defined(IP_TOS)
593 if(priorityinheritance)
594 logger(DEBUG_ALWAYS, LOG_WARNING, "%s not supported on this platform", "PriorityInheritance");
597 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
600 if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
601 if(maxtimeout <= 0) {
602 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!");
608 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
609 if(!strcasecmp(afname, "IPv4"))
610 addressfamily = AF_INET;
611 else if(!strcasecmp(afname, "IPv6"))
612 addressfamily = AF_INET6;
613 else if(!strcasecmp(afname, "any"))
614 addressfamily = AF_UNSPEC;
616 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid address family!");
622 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
624 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
627 config_t *cfg = lookup_config(config_tree, "AutoConnect");
629 if(!get_config_bool(cfg, &autoconnect)) {
630 // Some backwards compatibility with when this option was an int
632 get_config_int(cfg, &val);
637 get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers);
639 read_invitation_key();
645 Add listening sockets.
647 static bool add_listen_address(char *address, bool bindto) {
651 char *space = strchr(address, ' ');
657 if(!strcmp(address, "*"))
661 struct addrinfo *ai, hint = {0};
662 hint.ai_family = addressfamily;
663 hint.ai_socktype = SOCK_STREAM;
664 hint.ai_protocol = IPPROTO_TCP;
665 hint.ai_flags = AI_PASSIVE;
667 #if HAVE_DECL_RES_INIT
670 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
674 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
678 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
679 // Ignore duplicate addresses
682 for(int i = 0; i < listen_sockets; i++)
683 if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
691 if(listen_sockets >= MAXSOCKETS) {
692 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
696 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
701 int udp_fd = setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
708 io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
709 io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
711 if(debug_level >= DEBUG_CONNECTIONS) {
712 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
713 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
717 listen_socket[listen_sockets].bindto = bindto;
718 memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
726 void device_enable(void) {
730 /* Run tinc-up script to further initialize the tap interface */
732 char *envp[5] = {NULL};
733 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
734 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
735 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
736 xasprintf(&envp[3], "NAME=%s", myname);
738 execute_script("tinc-up", envp);
740 for(int i = 0; i < 4; i++)
744 void device_disable(void) {
745 char *envp[5] = {NULL};
746 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
747 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
748 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
749 xasprintf(&envp[3], "NAME=%s", myname);
751 execute_script("tinc-down", envp);
753 for(int i = 0; i < 4; i++)
761 Configure node_t myself and set up the local sockets (listen only)
763 static bool setup_myself(void) {
764 char *name, *hostname, *cipher, *digest, *type;
765 char *address = NULL;
766 bool port_specified = false;
768 if(!(name = get_name())) {
769 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
773 myname = xstrdup(name);
775 myself->connection = new_connection();
777 myself->connection->name = xstrdup(name);
778 read_host_config(config_tree, name);
780 if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
781 myport = xstrdup("655");
783 port_specified = true;
785 myself->connection->options = 0;
786 myself->connection->protocol_major = PROT_MAJOR;
787 myself->connection->protocol_minor = PROT_MINOR;
789 myself->options |= PROT_MINOR << 24;
791 #ifdef DISABLE_LEGACY
792 experimental = read_ecdsa_private_key();
794 logger(DEBUG_ALWAYS, LOG_ERR, "No private key available, cannot start tinc!");
798 if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) {
799 experimental = read_ecdsa_private_key();
801 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for SPTPS disabled.");
803 if(experimental && !read_ecdsa_private_key())
807 if(!read_rsa_private_key()) {
809 logger(DEBUG_ALWAYS, LOG_WARNING, "Support for legacy protocol disabled.");
811 logger(DEBUG_ALWAYS, LOG_ERR, "No private keys available, cannot start tinc!");
817 /* Ensure myport is numeric */
820 struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
822 if(!ai || !ai->ai_addr)
825 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
826 sockaddr2str(&sa, NULL, &myport);
829 /* Read in all the subnets specified in the host configuration file */
831 for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
834 if(!get_config_subnet(cfg, &subnet))
837 subnet_add(myself, subnet);
840 /* Check some options */
842 if(!setup_myself_reloadable())
845 get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
846 get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
847 strictsubnets |= tunnelserver;
849 if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) {
850 if(max_connection_burst <= 0) {
851 logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!");
856 if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
857 if(udp_rcvbuf <= 0) {
858 logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!");
863 if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
864 if(udp_sndbuf <= 0) {
865 logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!");
871 if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
872 if(replaywin_int < 0) {
873 logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!");
876 replaywin = (unsigned)replaywin_int;
877 sptps_replaywin = replaywin;
880 #ifndef DISABLE_LEGACY
881 /* Generate packet encryption key */
883 if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher))
884 cipher = xstrdup("blowfish");
886 if(!strcasecmp(cipher, "none")) {
887 myself->incipher = NULL;
888 } else if(!(myself->incipher = cipher_open_by_name(cipher))) {
889 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!");
895 timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000});
897 /* Check if we want to use message authentication codes... */
900 get_config_int(lookup_config(config_tree, "MACLength"), &maclength);
903 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!");
907 if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
908 digest = xstrdup("sha1");
910 if(!strcasecmp(digest, "none")) {
911 myself->indigest = NULL;
912 } else if(!(myself->indigest = digest_open_by_name(digest, maclength))) {
913 logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!");
922 if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
923 if(myself->incompression < 0 || myself->incompression > 11) {
924 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus compression level!");
928 myself->incompression = 0;
930 myself->connection->outcompression = 0;
934 myself->nexthop = myself;
935 myself->via = myself;
936 myself->status.reachable = true;
937 myself->last_state_change = now.tv_sec;
938 myself->status.sptps = experimental;
952 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
953 if(!strcasecmp(type, "dummy"))
954 devops = dummy_devops;
955 else if(!strcasecmp(type, "raw_socket"))
956 devops = raw_socket_devops;
957 else if(!strcasecmp(type, "multicast"))
958 devops = multicast_devops;
960 else if(!strcasecmp(type, "uml"))
964 else if(!strcasecmp(type, "vde"))
969 get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby);
975 io_add(&device_io, handle_device_data, NULL, device_fd, IO_READ);
979 if(!do_detach && getenv("LISTEN_FDS")) {
983 listen_sockets = atoi(getenv("LISTEN_FDS"));
985 unsetenv("LISTEN_FDS");
988 if(listen_sockets > MAXSOCKETS) {
989 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
993 for(int i = 0; i < listen_sockets; i++) {
995 if(getsockname(i + 3, &sa.sa, &salen) < 0) {
996 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(sockerrno));
1001 fcntl(i + 3, F_SETFD, FD_CLOEXEC);
1004 int udp_fd = setup_vpn_in_socket(&sa);
1008 io_add(&listen_socket[i].tcp, (io_cb_t)handle_new_meta_connection, &listen_socket[i], i + 3, IO_READ);
1009 io_add(&listen_socket[i].udp, (io_cb_t)handle_incoming_vpn_data, &listen_socket[i], udp_fd, IO_READ);
1011 if(debug_level >= DEBUG_CONNECTIONS) {
1012 hostname = sockaddr2hostname(&sa);
1013 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
1017 memcpy(&listen_socket[i].sa, &sa, salen);
1023 for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1025 get_config_string(cfg, &address);
1026 if(!add_listen_address(address, true))
1030 for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
1032 get_config_string(cfg, &address);
1033 if(!add_listen_address(address, false))
1038 if(!add_listen_address(address, NULL))
1042 if(!listen_sockets) {
1043 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
1047 /* If no Port option was specified, set myport to the port used by the first listening socket. */
1049 if(!port_specified || atoi(myport) == 0) {
1051 socklen_t salen = sizeof sa;
1052 if(!getsockname(listen_socket[0].udp.fd, &sa.sa, &salen)) {
1054 sockaddr2str(&sa, NULL, &myport);
1056 myport = xstrdup("655");
1060 xasprintf(&myself->hostname, "MYSELF port %s", myport);
1061 myself->connection->hostname = xstrdup(myself->hostname);
1065 last_config_check = now.tv_sec;
1073 bool setup_network(void) {
1080 if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
1081 if(pinginterval < 1) {
1082 pinginterval = 86400;
1087 if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
1089 if(pingtimeout < 1 || pingtimeout > pinginterval)
1090 pingtimeout = pinginterval;
1092 if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize))
1093 maxoutbufsize = 10 * MTU;
1101 if (!device_standby)
1104 /* Run subnet-up scripts for our own subnets */
1106 subnet_update(myself, NULL, true);
1112 close all open network connections
1114 void close_network_connections(void) {
1115 for(list_node_t *node = connection_list->head, *next; node; node = next) {
1117 connection_t *c = node->data;
1118 /* Keep control connections open until the end, so they know when we really terminated */
1119 if(c->status.control)
1122 terminate_connection(c, false);
1126 list_delete_list(outgoing_list);
1128 if(myself && myself->connection) {
1129 subnet_update(myself, NULL, false);
1130 terminate_connection(myself->connection, false);
1131 free_connection(myself->connection);
1134 for(int i = 0; i < listen_sockets; i++) {
1135 io_del(&listen_socket[i].tcp);
1136 io_del(&listen_socket[i].udp);
1137 close(listen_socket[i].tcp.fd);
1138 close(listen_socket[i].udp.fd);
1147 if (!device_standby)
1160 free(scriptextension);
1161 free(scriptinterpreter);