2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2022 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "connection.h"
26 #include "control_common.h"
46 #include "ed25519/sha512.h"
49 /* If nonzero, use null ciphers and skip all key exchanges. */
50 bool bypass_security = false;
52 int invitation_lifetime;
53 ecdsa_t *invitation_key = NULL;
55 static bool send_proxyrequest(connection_t *c) {
61 sockaddr2str(&c->address, &host, &port);
62 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
69 if(c->address.sa.sa_family != AF_INET) {
70 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
74 const size_t s4reqlen = 9 + (proxyuser ? strlen(proxyuser) : 0);
75 uint8_t *s4req = alloca(s4reqlen);
78 memcpy(s4req + 2, &c->address.in.sin_port, 2);
79 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
82 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
85 s4req[s4reqlen - 1] = 0;
87 return send_meta(c, s4req, s4reqlen);
91 size_t len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
95 len += 3 + strlen(proxyuser) + strlen(proxypass);
98 uint8_t *s5req = alloca(len);
107 s5req[i++] = strlen(proxyuser);
108 memcpy(s5req + i, proxyuser, strlen(proxyuser));
109 i += strlen(proxyuser);
110 s5req[i++] = strlen(proxypass);
111 memcpy(s5req + i, proxypass, strlen(proxypass));
112 i += strlen(proxypass);
122 if(c->address.sa.sa_family == AF_INET) {
124 memcpy(s5req + i, &c->address.in.sin_addr, 4);
126 memcpy(s5req + i, &c->address.in.sin_port, 2);
129 } else if(c->address.sa.sa_family == AF_INET6) {
131 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
133 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
137 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %x not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
145 return send_meta(c, s5req, len);
149 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
156 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
161 bool send_id(connection_t *c) {
162 gettimeofday(&c->start, NULL);
167 if(c->outgoing && !read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name)) {
170 minor = myself->connection->protocol_minor;
174 if(proxytype && c->outgoing)
175 if(!send_proxyrequest(c)) {
179 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
182 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
185 if(strchr(data, '\n')) {
186 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
190 // Create a new host config file
191 char filename[PATH_MAX];
192 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
194 if(!access(filename, F_OK)) {
195 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
199 FILE *f = fopen(filename, "w");
202 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
206 fprintf(f, "Ed25519PublicKey = %s\n", data);
209 logger(DEBUG_CONNECTIONS, LOG_INFO, "Key successfully received from %s (%s)", c->name, c->hostname);
211 // Call invitation-accepted script
213 char *address, *port;
215 environment_init(&env);
216 environment_add(&env, "NODE=%s", c->name);
217 sockaddr2str(&c->address, &address, &port);
218 environment_add(&env, "REMOTEADDRESS=%s", address);
219 environment_add(&env, "NAME=%s", myself->name);
224 execute_script("invitation-accepted", &env);
226 environment_exit(&env);
228 sptps_send_record(&c->sptps, 2, data, 0);
232 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
233 connection_t *c = handle;
239 if(type == 1 && c->status.invitation_used) {
240 return finalize_invitation(c, data, len);
243 if(type != 0 || len != 18 || c->status.invitation_used) {
247 // Recover the filename from the cookie and the key
248 char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
249 const size_t hashbuflen = 18 + strlen(fingerprint);
250 char *hashbuf = alloca(hashbuflen);
252 memcpy(hashbuf, data, 18);
253 memcpy(hashbuf + 18, fingerprint, hashbuflen - 18);
254 sha512(hashbuf, hashbuflen, cookie);
255 b64encode_tinc_urlsafe(cookie, cookie, 18);
258 char filename[PATH_MAX], usedname[PATH_MAX];
259 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
260 snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
262 // Atomically rename the invitation file
263 if(rename(filename, usedname)) {
264 if(errno == ENOENT) {
265 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
267 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
273 // Check the timestamp of the invitation
276 if(stat(usedname, &st)) {
277 logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat %s", usedname);
281 if(st.st_mtime + invitation_lifetime < now.tv_sec) {
282 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use expired invitation %s", c->hostname, cookie);
286 // Open the renamed file
287 FILE *f = fopen(usedname, "r");
290 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
294 // Read the new node's Name from the file
297 if(!fgets(buf, sizeof(buf), f)) {
298 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
303 size_t buflen = strlen(buf);
305 // Strip whitespace at the end
306 while(buflen && strchr(" \t\r\n", buf[buflen - 1])) {
310 // Split the first line into variable and value
311 len = strcspn(buf, " \t=");
312 char *name = buf + len;
313 name += strspn(name, " \t");
317 name += strspn(name, " \t");
322 // Check that it is a valid Name
323 if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name) || !strcmp(name, myself->name)) {
324 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
330 c->name = xstrdup(name);
332 // Send the node the contents of the invitation file
336 while((result = fread(buf, 1, sizeof(buf), f))) {
337 sptps_send_record(&c->sptps, 0, buf, result);
341 logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
346 sptps_send_record(&c->sptps, 1, buf, 0);
350 c->status.invitation_used = true;
352 logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s successfully sent to %s (%s)", cookie, c->name, c->hostname);
356 bool id_h(connection_t *c, const char *request) {
357 char name[MAX_STRING_SIZE];
359 if(sscanf(request, "%*d " MAX_STRING " %2d.%3d", name, &c->protocol_major, &c->protocol_minor) < 2) {
360 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
365 /* Check if this is a control connection */
367 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
368 c->status.control = true;
369 c->allow_request = CONTROL;
370 c->last_ping_time = now.tv_sec + 3600;
373 c->name = xstrdup("<control>");
379 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
383 if(!invitation_key) {
384 logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
388 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
391 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
395 c->status.invitation = true;
396 char *mykey = ecdsa_get_base64_public_key(invitation_key);
406 if(!send_request(c, "%d %s", ACK, mykey)) {
412 c->protocol_minor = 2;
414 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
417 /* Check if identity is a valid name */
419 if(!check_id(name) || !strcmp(name, myself->name)) {
420 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
421 c->hostname, "invalid name");
425 /* If this is an outgoing connection, make sure we are connected to the right host */
428 if(strcmp(c->name, name)) {
429 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
435 c->name = xstrdup(name);
438 /* Check if version matches */
440 if(c->protocol_major != myself->connection->protocol_major) {
441 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
442 c->name, c->hostname, c->protocol_major, c->protocol_minor);
446 if(bypass_security) {
447 if(!c->config_tree) {
448 c->config_tree = create_configuration();
451 c->allow_request = ACK;
461 c->protocol_minor = 0;
464 if(!c->config_tree) {
465 c->config_tree = create_configuration();
467 if(!read_host_config(c->config_tree, c->name, false)) {
468 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
473 read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name);
476 /* Ignore failures if no key known yet */
479 if(c->protocol_minor && !ecdsa_active(c->ecdsa)) {
480 c->protocol_minor = 1;
483 /* Forbid version rollback for nodes whose Ed25519 key we know */
485 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 1) {
486 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
487 c->name, c->hostname, c->protocol_major, c->protocol_minor);
491 c->allow_request = METAKEY;
497 if(c->protocol_minor >= 2) {
498 c->allow_request = ACK;
500 const size_t labellen = 25 + strlen(myself->name) + strlen(c->name);
501 char *label = alloca(labellen);
504 snprintf(label, labellen, "tinc TCP key expansion %s %s", myself->name, c->name);
506 snprintf(label, labellen, "tinc TCP key expansion %s %s", c->name, myself->name);
509 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, labellen, send_meta_sptps, receive_meta_sptps);
511 return send_metakey(c);
515 #ifndef DISABLE_LEGACY
516 static const char *get_cipher_name(cipher_t *cipher) {
517 size_t keylen = cipher_keylength(cipher);
520 return "aes-128-cfb";
521 } else if(keylen <= 24) {
522 return "aes-192-cfb";
524 return "aes-256-cfb";
528 bool send_metakey(connection_t *c) {
529 if(!myself->connection->legacy) {
530 logger(DEBUG_CONNECTIONS, LOG_ERR, "Peer %s (%s) uses legacy protocol which we don't support", c->name, c->hostname);
534 rsa_t *rsa = read_rsa_public_key(c->config_tree, c->name);
540 legacy_ctx_t *ctx = new_legacy_ctx(rsa);
542 /* We need to use a stream mode for the meta protocol. Use AES for this,
543 but try to match the key size with the one from the cipher selected
547 const char *cipher_name = get_cipher_name(myself->incipher);
549 if(!init_crypto_by_name(&ctx->out, cipher_name, "sha256")) {
550 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest to %s (%s)", c->name, c->hostname);
551 free_legacy_ctx(ctx);
555 const size_t len = rsa_size(ctx->rsa);
556 char *key = alloca(len);
557 char *enckey = alloca(len);
558 char *hexkey = alloca(2 * len + 1);
560 /* Create a random key */
564 /* The message we send must be smaller than the modulus of the RSA key.
565 By definition, for a key of k bits, the following formula holds:
567 2^(k-1) <= modulus < 2^(k)
569 Where ^ means "to the power of", not "xor".
570 This means that to be sure, we must choose our message < 2^(k-1).
571 This can be done by setting the most significant bit to zero.
576 if(!cipher_set_key_from_rsa(&ctx->out.cipher, key, len, true)) {
577 free_legacy_ctx(ctx);
581 if(debug_level >= DEBUG_SCARY_THINGS) {
582 bin2hex(key, hexkey, len);
583 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
586 /* Encrypt the random data
588 We do not use one of the PKCS padding schemes here.
589 This is allowed, because we encrypt a totally random string
590 with a length equal to that of the modulus of the RSA key.
593 if(!rsa_public_encrypt(ctx->rsa, key, len, enckey)) {
594 free_legacy_ctx(ctx);
595 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
599 free_legacy_ctx(c->legacy);
602 /* Convert the encrypted random data to a hexadecimal formatted string */
604 bin2hex(enckey, hexkey, len);
606 /* Send the meta key */
608 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
609 cipher_get_nid(&c->legacy->out.cipher),
610 digest_get_nid(&c->legacy->out.digest), c->outmaclength,
611 c->outcompression, hexkey);
613 c->status.encryptout = true;
617 bool metakey_h(connection_t *c, const char *request) {
618 if(!myself->connection->legacy || !c->legacy) {
622 char hexkey[MAX_STRING_SIZE];
624 const size_t len = rsa_size(myself->connection->legacy->rsa);
625 char *enckey = alloca(len);
626 char *key = alloca(len);
628 if(sscanf(request, "%*d %d %d %*d %*d " MAX_STRING, &cipher, &digest, hexkey) != 3) {
629 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
633 /* Convert the challenge from hexadecimal back to binary */
635 size_t inlen = hex2bin(hexkey, enckey, len);
637 /* Check if the length of the meta key is all right */
640 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
644 /* Decrypt the meta key */
646 if(!rsa_private_decrypt(myself->connection->legacy->rsa, enckey, len, key)) {
647 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
651 if(debug_level >= DEBUG_SCARY_THINGS) {
652 bin2hex(key, hexkey, len);
653 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
656 /* Check and lookup cipher and digest algorithms */
658 if(!cipher || !digest) {
659 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): cipher %d, digest %d", c->name, c->hostname, cipher, digest);
663 if(!init_crypto_by_nid(&c->legacy->in, cipher, digest)) {
664 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher or digest from %s (%s)", c->name, c->hostname);
668 if(!cipher_set_key_from_rsa(&c->legacy->in.cipher, key, len, false)) {
669 logger(DEBUG_ALWAYS, LOG_ERR, "Error setting RSA key for %s (%s)", c->name, c->hostname);
674 c->status.decryptin = true;
676 c->allow_request = CHALLENGE;
678 return send_challenge(c);
681 bool send_challenge(connection_t *c) {
682 const size_t len = rsa_size(c->legacy->rsa);
683 char *buffer = alloca(len * 2 + 1);
685 c->hischallenge = xrealloc(c->hischallenge, len);
687 /* Copy random data to the buffer */
689 randomize(c->hischallenge, len);
693 bin2hex(c->hischallenge, buffer, len);
695 /* Send the challenge */
697 return send_request(c, "%d %s", CHALLENGE, buffer);
700 bool challenge_h(connection_t *c, const char *request) {
701 if(!myself->connection->legacy) {
705 char buffer[MAX_STRING_SIZE];
706 const size_t len = rsa_size(myself->connection->legacy->rsa);
708 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
709 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
713 /* Check if the length of the challenge is all right */
715 if(strlen(buffer) != (size_t)len * 2) {
716 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
720 c->mychallenge = xrealloc(c->mychallenge, len);
722 /* Convert the challenge from hexadecimal back to binary */
724 hex2bin(buffer, c->mychallenge, len);
726 /* The rest is done by send_chal_reply() */
728 c->allow_request = CHAL_REPLY;
731 return send_chal_reply(c);
737 bool send_chal_reply(connection_t *c) {
738 const size_t len = rsa_size(myself->connection->legacy->rsa);
739 size_t digestlen = digest_length(&c->legacy->in.digest);
740 char *digest = alloca(digestlen * 2 + 1);
742 /* Calculate the hash from the challenge we received */
744 if(!digest_create(&c->legacy->in.digest, c->mychallenge, len, digest)) {
748 free(c->mychallenge);
749 c->mychallenge = NULL;
751 /* Convert the hash to a hexadecimal formatted string */
753 bin2hex(digest, digest, digestlen);
757 return send_request(c, "%d %s", CHAL_REPLY, digest);
760 bool chal_reply_h(connection_t *c, const char *request) {
761 char hishash[MAX_STRING_SIZE];
763 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
764 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
769 /* Convert the hash to binary format */
771 size_t inlen = hex2bin(hishash, hishash, sizeof(hishash));
773 /* Check if the length of the hash is all right */
775 if(inlen != digest_length(&c->legacy->out.digest)) {
776 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
781 /* Verify the hash */
783 if(!digest_verify(&c->legacy->out.digest, c->hischallenge, rsa_size(c->legacy->rsa), hishash)) {
784 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
788 /* Identity has now been positively verified.
789 Send an acknowledgement with the rest of the information needed.
792 free(c->hischallenge);
793 c->hischallenge = NULL;
794 c->allow_request = ACK;
803 static bool send_upgrade(connection_t *c) {
804 /* Special case when protocol_minor is 1: the other end is Ed25519 capable,
805 * but doesn't know our key yet. So send it now. */
807 char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
813 bool result = send_request(c, "%d %s", ACK, pubkey);
818 bool send_metakey(connection_t *c) {
823 bool metakey_h(connection_t *c, const char *request) {
829 bool send_challenge(connection_t *c) {
834 bool challenge_h(connection_t *c, const char *request) {
840 bool send_chal_reply(connection_t *c) {
845 bool chal_reply_h(connection_t *c, const char *request) {
851 static bool send_upgrade(connection_t *c) {
857 bool send_ack(connection_t *c) {
858 if(c->protocol_minor == 1) {
859 return send_upgrade(c);
862 /* ACK message contains rest of the information the other end needs
863 to create node_t and edge_t structures. */
868 /* Estimate weight */
870 gettimeofday(&now, NULL);
871 c->estimated_weight = (int)((now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000);
873 /* Check some options */
875 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT) {
876 c->options |= OPTION_INDIRECT;
879 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY) {
880 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
883 if(myself->options & OPTION_PMTU_DISCOVERY && !(c->options & OPTION_TCPONLY)) {
884 c->options |= OPTION_PMTU_DISCOVERY;
887 choice = myself->options & OPTION_CLAMP_MSS;
888 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
891 c->options |= OPTION_CLAMP_MSS;
894 if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight)) {
895 get_config_int(lookup_config(&config_tree, "Weight"), &c->estimated_weight);
898 return send_request(c, "%d %s %d %x", ACK, myport.udp, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
901 static void send_everything(connection_t *c) {
902 /* Send all known subnets and edges */
904 if(disablebuggypeers) {
907 char pad[MAXBUFSIZE - MAXSIZE];
910 memset(&zeropkt, 0, sizeof(zeropkt));
911 zeropkt.pkt.len = MAXBUFSIZE;
912 send_tcppacket(c, &zeropkt.pkt);
916 for splay_each(subnet_t, s, &myself->subnet_tree) {
917 send_add_subnet(c, s);
923 for splay_each(node_t, n, &node_tree) {
924 for splay_each(subnet_t, s, &n->subnet_tree) {
925 send_add_subnet(c, s);
928 for splay_each(edge_t, e, &n->edge_tree) {
934 static bool upgrade_h(connection_t *c, const char *request) {
935 char pubkey[MAX_STRING_SIZE];
937 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
938 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
942 if(ecdsa_active(c->ecdsa) || read_ecdsa_public_key(&c->ecdsa, &c->config_tree, c->name)) {
943 char *knownkey = ecdsa_get_base64_public_key(c->ecdsa);
944 bool different = strcmp(knownkey, pubkey);
948 logger(DEBUG_ALWAYS, LOG_ERR, "Already have an Ed25519 public key from %s (%s) which is different from the one presented now!", c->name, c->hostname);
952 logger(DEBUG_ALWAYS, LOG_INFO, "Already have Ed25519 public key from %s (%s), ignoring.", c->name, c->hostname);
953 c->allow_request = TERMREQ;
954 return send_termreq(c);
957 c->ecdsa = ecdsa_set_base64_public_key(pubkey);
960 logger(DEBUG_ALWAYS, LOG_INFO, "Got bad Ed25519 public key from %s (%s), not upgrading.", c->name, c->hostname);
964 logger(DEBUG_ALWAYS, LOG_INFO, "Got Ed25519 public key from %s (%s), upgrading!", c->name, c->hostname);
965 append_config_file(c->name, "Ed25519PublicKey", pubkey);
966 c->allow_request = TERMREQ;
969 c->outgoing->timeout = 0;
972 return send_termreq(c);
975 bool ack_h(connection_t *c, const char *request) {
976 if(c->protocol_minor == 1) {
977 return upgrade_h(c, request);
980 char hisport[MAX_STRING_SIZE];
986 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
987 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
992 /* Check if we already have a node_t for him */
994 n = lookup_node(c->name);
998 n->name = xstrdup(c->name);
1002 /* Oh dear, we already have a connection to this node. */
1003 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
1005 if(n->connection->outgoing) {
1007 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
1009 c->outgoing = n->connection->outgoing;
1012 n->connection->outgoing = NULL;
1015 terminate_connection(n->connection, false);
1016 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
1024 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
1025 c->options &= ~OPTION_PMTU_DISCOVERY;
1026 options &= ~OPTION_PMTU_DISCOVERY;
1029 c->options |= options;
1031 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
1035 if(get_config_int(lookup_config(&config_tree, "PMTU"), &mtu) && mtu < n->mtu) {
1039 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
1041 c->options |= OPTION_CLAMP_MSS;
1043 c->options &= ~OPTION_CLAMP_MSS;
1047 /* Activate this connection */
1049 c->allow_request = ALL;
1051 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
1054 /* Send him everything we know */
1058 /* Create an edge_t for this connection */
1060 c->edge = new_edge();
1061 c->edge->from = myself;
1063 sockaddrcpy(&c->edge->address, &c->address);
1064 sockaddr_setport(&c->edge->address, hisport);
1065 sockaddr_t local_sa;
1066 socklen_t local_salen = sizeof(local_sa);
1068 if(getsockname(c->socket, &local_sa.sa, &local_salen) < 0) {
1069 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not get local socket address for connection with %s", c->name);
1071 sockaddr_setport(&local_sa, myport.udp);
1072 c->edge->local_address = local_sa;
1075 c->edge->weight = (weight + c->estimated_weight) / 2;
1076 c->edge->connection = c;
1077 c->edge->options = c->options;
1081 /* Notify everyone of the new edge */
1084 send_add_edge(c, c->edge);
1086 send_add_edge(everyone, c->edge);
1089 /* Run MST and SSSP algorithms */