2 protocol.c -- handle the meta-protocol
3 Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: protocol.c,v 1.28.4.109 2001/10/27 12:13:17 guus Exp $
25 #include <sys/types.h>
30 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <openssl/sha.h>
44 #include <openssl/rand.h>
45 #include <openssl/evp.h>
47 #ifndef HAVE_RAND_PSEUDO_BYTES
48 #define RAND_pseudo_bytes RAND_bytes
56 #include "connection.h"
64 int check_id(char *id)
68 for (i = 0; i < strlen(id); i++)
69 if(!isalnum(id[i]) && id[i] != '_')
75 /* Generic request routines - takes care of logging and error
78 int send_request(connection_t *c, const char *format, ...)
81 char buffer[MAXBUFSIZE];
85 /* Use vsnprintf instead of vasprintf: faster, no memory
86 fragmentation, cleanup is automatic, and there is a limit on the
87 input buffer anyway */
89 va_start(args, format);
90 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
91 request = va_arg(args, int);
94 if(len < 0 || len > MAXBUFSIZE-1)
96 syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
100 if(debug_lvl >= DEBUG_PROTOCOL)
102 if(debug_lvl >= DEBUG_META)
103 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
105 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
108 buffer[len++] = '\n';
110 return send_meta(c, buffer, len);
113 int receive_request(connection_t *c)
117 if(sscanf(c->buffer, "%d", &request) == 1)
119 if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
121 if(debug_lvl >= DEBUG_META)
122 syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
123 c->name, c->hostname, c->buffer);
125 syslog(LOG_ERR, _("Unknown request from %s (%s)"),
126 c->name, c->hostname);
132 if(debug_lvl >= DEBUG_PROTOCOL)
134 if(debug_lvl >= DEBUG_META)
135 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
136 request_name[request], c->name, c->hostname, c->buffer);
138 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
139 request_name[request], c->name, c->hostname);
143 if((c->allow_request != ALL) && (c->allow_request != request))
145 syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
149 if(request_handlers[request](c))
150 /* Something went wrong. Probably scriptkiddies. Terminate. */
152 syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
153 request_name[request], c->name, c->hostname);
159 syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
160 c->name, c->hostname);
167 /* The authentication protocol is described in detail in doc/SECURITY2,
168 the rest will be described in doc/PROTOCOL. */
170 int send_id(connection_t *c)
173 return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
176 int id_h(connection_t *c)
178 char name[MAX_STRING_SIZE];
180 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
182 syslog(LOG_ERR, _("Got bad %s from %s"), "ID", c->hostname);
186 /* Check if identity is a valid name */
190 syslog(LOG_ERR, _("Peer %s uses invalid identity name"), c->hostname);
194 /* If we set c->name in advance, make sure we are connected to the right host */
198 if(strcmp(c->name, name))
200 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
205 c->name = xstrdup(name);
207 /* Check if version matches */
209 if(c->protocol_version != myself->connection->protocol_version)
211 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
212 c->name, c->hostname, c->protocol_version);
218 if(read_connection_config(c))
220 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
224 if(read_rsa_public_key(c))
230 c->allow_request = METAKEY;
232 return send_metakey(c);
235 int send_metakey(connection_t *c)
240 len = RSA_size(c->rsa_key);
242 /* Allocate buffers for the meta key */
244 buffer = xmalloc(len*2+1);
247 c->outkey = xmalloc(len);
250 c->outctx = xmalloc(sizeof(*c->outctx));
252 /* Copy random data to the buffer */
254 RAND_bytes(c->outkey, len);
256 /* The message we send must be smaller than the modulus of the RSA key.
257 By definition, for a key of k bits, the following formula holds:
259 2^(k-1) <= modulus < 2^(k)
261 Where ^ means "to the power of", not "xor".
262 This means that to be sure, we must choose our message < 2^(k-1).
263 This can be done by setting the most significant bit to zero.
266 c->outkey[0] &= 0x7F;
268 if(debug_lvl >= DEBUG_SCARY_THINGS)
270 bin2hex(c->outkey, buffer, len);
271 buffer[len*2] = '\0';
272 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
275 /* Encrypt the random data
277 We do not use one of the PKCS padding schemes here.
278 This is allowed, because we encrypt a totally random string
279 with a length equal to that of the modulus of the RSA key.
282 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
284 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
289 /* Convert the encrypted random data to a hexadecimal formatted string */
291 bin2hex(buffer, buffer, len);
292 buffer[len*2] = '\0';
294 /* Send the meta key */
296 x = send_request(c, "%d %s", METAKEY, buffer);
299 /* Further outgoing requests are encrypted with the key we just generated */
301 EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
302 c->outkey + len - EVP_bf_cfb()->key_len,
303 c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
305 c->status.encryptout = 1;
310 int metakey_h(connection_t *c)
312 char buffer[MAX_STRING_SIZE];
315 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
317 syslog(LOG_ERR, _("Got bad METAKEY from %s (%s)"), c->name, c->hostname);
321 len = RSA_size(myself->connection->rsa_key);
323 /* Check if the length of the meta key is all right */
325 if(strlen(buffer) != len*2)
327 syslog(LOG_ERR, _("Intruder: wrong meta key length from %s (%s)"), c->name, c->hostname);
331 /* Allocate buffers for the meta key */
334 c->inkey = xmalloc(len);
337 c->inctx = xmalloc(sizeof(*c->inctx));
339 /* Convert the challenge from hexadecimal back to binary */
341 hex2bin(buffer,buffer,len);
343 /* Decrypt the meta key */
345 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
347 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
351 if(debug_lvl >= DEBUG_SCARY_THINGS)
353 bin2hex(c->inkey, buffer, len);
354 buffer[len*2] = '\0';
355 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
358 /* All incoming requests will now be encrypted. */
360 EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
361 c->inkey + len - EVP_bf_cfb()->key_len,
362 c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
364 c->status.decryptin = 1;
366 c->allow_request = CHALLENGE;
368 return send_challenge(c);
371 int send_challenge(connection_t *c)
376 /* CHECKME: what is most reasonable value for len? */
378 len = RSA_size(c->rsa_key);
380 /* Allocate buffers for the challenge */
382 buffer = xmalloc(len*2+1);
385 free(c->hischallenge);
387 c->hischallenge = xmalloc(len);
389 /* Copy random data to the buffer */
391 RAND_bytes(c->hischallenge, len);
396 bin2hex(c->hischallenge, buffer, len);
397 buffer[len*2] = '\0';
400 /* Send the challenge */
402 x = send_request(c, "%d %s", CHALLENGE, buffer);
408 int challenge_h(connection_t *c)
410 char buffer[MAX_STRING_SIZE];
413 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
415 syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), c->name, c->hostname);
419 len = RSA_size(myself->connection->rsa_key);
421 /* Check if the length of the challenge is all right */
423 if(strlen(buffer) != len*2)
425 syslog(LOG_ERR, _("Intruder: wrong challenge length from %s (%s)"), c->name, c->hostname);
429 /* Allocate buffers for the challenge */
432 c->mychallenge = xmalloc(len);
434 /* Convert the challenge from hexadecimal back to binary */
436 hex2bin(buffer,c->mychallenge,len);
438 c->allow_request = CHAL_REPLY;
440 /* Rest is done by send_chal_reply() */
442 return send_chal_reply(c);
445 int send_chal_reply(connection_t *c)
447 char hash[SHA_DIGEST_LENGTH*2+1];
451 syslog(LOG_ERR, _("Trying to send CHAL_REPLY to %s (%s) without a valid CHALLENGE"), c->name, c->hostname);
455 /* Calculate the hash from the challenge we received */
457 SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
459 /* Convert the hash to a hexadecimal formatted string */
461 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
462 hash[SHA_DIGEST_LENGTH*2] = '\0';
467 return send_request(c, "%d %s", CHAL_REPLY, hash);
470 int chal_reply_h(connection_t *c)
472 char hishash[MAX_STRING_SIZE];
473 char myhash[SHA_DIGEST_LENGTH];
475 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
477 syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), c->name, c->hostname);
481 /* Check if the length of the hash is all right */
483 if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
485 syslog(LOG_ERR, _("Intruder: wrong challenge reply length from %s (%s)"), c->name, c->hostname);
489 /* Convert the hash to binary format */
491 hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
493 /* Calculate the hash from the challenge we sent */
495 SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
497 /* Verify the incoming hash with the calculated hash */
499 if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
501 syslog(LOG_ERR, _("Intruder: wrong challenge reply from %s (%s)"), c->name, c->hostname);
502 if(debug_lvl >= DEBUG_SCARY_THINGS)
504 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
505 hishash[SHA_DIGEST_LENGTH*2] = '\0';
506 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
511 /* Identity has now been positively verified.
512 Send an acknowledgement with the rest of the information needed.
515 c->allow_request = ACK;
520 int send_ack(connection_t *c)
522 /* ACK message contains rest of the information the other end needs
523 to create node_t and vertex_t structures. */
525 return send_request(c, "%d %d", ACK, myself->port);
528 int ack_h(connection_t *c)
533 avl_node_t *node, *node2;
535 if(sscanf(c->buffer, "%*d %hd", &port) != 1)
537 syslog(LOG_ERR, _("Got bad %s from %s"), "ACK", c->hostname);
541 /* Check if we already have a node_t for him */
543 n = lookup_node(c->name);
548 n->name = xstrdup(c->name);
549 n->hostname = xstrdup(c->hostname);
552 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
560 /* Oh dear, we already have a connection to this node. */
561 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
562 terminate_connection(n->connection, 0);
565 /* FIXME: check if information in existing node matches that of the other end of this connection */
571 /* Check some options
573 if((cfg = get_config_val(c->config, config_indirectdata)))
575 if(cfg->data.val == stupid_true)
576 c->options |= OPTION_INDIRECT;
579 if((cfg = get_config_val(c->config, config_tcponly)))
581 if(cfg->data.val == stupid_true)
582 c->options |= OPTION_TCPONLY;
585 if((myself->options | c->options) & OPTION_INDIRECT)
592 /* Create a vertex_t for this connection */
594 c->vertex = new_vertex();
596 c->vertex->from = myself;
598 c->vertex->metric = 1;
599 c->vertex->connection = c;
601 vertex_add(c->vertex);
603 /* Activate this connection */
605 c->allow_request = ALL;
607 if(debug_lvl >= DEBUG_CONNECTIONS)
608 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
611 /* Send him our subnets */
613 for(node = myself->subnet_tree->head; node; node = node->next)
615 s = (subnet_t *)node->data;
616 send_add_subnet(c, s);
619 /* And send him all known nodes and their subnets */
621 for(node = node_tree->head; node; node = node->next)
623 n = (node_t *)node->data;
627 /* Notify others of this connection */
630 send_add_node(n->connection, c->node);
632 /* Notify new connection of everything we know */
636 for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
638 s = (subnet_t *)node2->data;
639 send_add_subnet(c, s);
649 /* Address and subnet information exchange */
651 int send_add_subnet(connection_t *c, subnet_t *subnet)
656 x = send_request(c, "%d %s %s", ADD_SUBNET,
657 subnet->owner->name, netstr = net2str(subnet));
663 int add_subnet_h(connection_t *c)
665 char subnetstr[MAX_STRING_SIZE];
666 char name[MAX_STRING_SIZE];
672 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
674 syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s)"), c->name, c->hostname);
678 /* Check if owner name is a valid */
682 syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s): invalid identity name"), c->name, c->hostname);
686 /* Check if subnet string is valid */
688 if(!(s = str2net(subnetstr)))
690 syslog(LOG_ERR, _("Got bad ADD_SUBNET from %s (%s): invalid subnet string"), c->name, c->hostname);
694 /* Check if the owner of the new subnet is in the connection list */
696 if(!(owner = lookup_node(name)))
698 syslog(LOG_ERR, _("Got ADD_SUBNET for %s from %s (%s) which is not in our connection list"),
699 name, c->name, c->hostname);
703 /* If everything is correct, add the subnet to the list of the owner */
705 subnet_add(owner, s);
709 for(node = connection_tree->head; node; node = node->next)
711 other = (connection_t *)node->data;
712 if(other->status.active && other != c)
713 send_add_subnet(other, s);
719 int send_del_subnet(connection_t *c, subnet_t *s)
724 x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
730 int del_subnet_h(connection_t *c)
732 char subnetstr[MAX_STRING_SIZE];
733 char name[MAX_STRING_SIZE];
739 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
741 syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s)"), c->name, c->hostname);
745 /* Check if owner name is a valid */
749 syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid identity name"), c->name, c->hostname);
753 /* Check if subnet string is valid */
755 if(!(s = str2net(subnetstr)))
757 syslog(LOG_ERR, _("Got bad DEL_SUBNET from %s (%s): invalid subnet string"), c->name, c->hostname);
761 /* Check if the owner of the new subnet is in the connection list */
763 if(!(owner = lookup_node(name)))
765 syslog(LOG_ERR, _("Got DEL_SUBNET for %s from %s (%s) which is not in our connection list"),
766 name, c->name, c->hostname);
770 /* If everything is correct, delete the subnet from the list of the owner */
772 find = lookup_subnet(owner, s);
776 syslog(LOG_ERR, _("Got DEL_SUBNET for %s from %s (%s) which does not appear in his subnet tree"),
777 name, c->name, c->hostname);
781 subnet_del(owner, s);
785 for(node = connection_tree->head; node; node = node->next)
787 other = (connection_t *)node->data;
788 if(other->status.active && other != c)
789 send_del_subnet(other, s);
795 /* New and closed connections notification */
797 int send_add_node(connection_t *c, node_t *n)
800 return send_request(c, "%d %s %lx:%d", ADD_NODE,
801 n->name, n->address, n->port);
804 int add_node_h(connection_t *c)
808 char name[MAX_STRING_SIZE];
813 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
815 syslog(LOG_ERR, _("Got bad ADD_NODE from %s (%s)"), c->name, c->hostname);
819 /* Check if identity is a valid name */
823 syslog(LOG_ERR, _("Got bad ADD_NODE from %s (%s): invalid identity name"), c->name, c->hostname);
827 /* Check if somebody tries to add ourself */
829 if(!strcmp(name, myself->name))
831 syslog(LOG_ERR, _("Got ADD_NODE from %s (%s) for ourself!"), c->name, c->hostname);
835 /* Check if node already exists */
837 n = lookup_node(name);
841 /* Check if it matches */
846 n->name = xstrdup(name);
847 n->address = address;
852 /* Tell the rest about the new node */
854 for(node = connection_tree->head; node; node = node->next)
856 other = (connection_t *)node->data;
857 if(other->status.active && other !=c)
858 send_add_node(other, n);
865 int send_del_node(connection_t *c, node_t *n)
868 return send_request(c, "%d %s %lx:%d", DEL_NODE,
869 n->name, n->address, n->port);
872 int del_node_h(connection_t *c)
875 char name[MAX_STRING_SIZE];
881 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
883 syslog(LOG_ERR, _("Got bad DEL_NODE from %s (%s)"),
884 c->name, c->hostname);
888 /* Check if identity is a valid name */
892 syslog(LOG_ERR, _("Got bad DEL_NODE from %s (%s): invalid identity name"), c->name, c->hostname);
896 /* Check if somebody tries to delete ourself */
898 if(!strcmp(name, myself->name))
900 syslog(LOG_ERR, _("Got DEL_NODE from %s (%s) for ourself!"),
901 c->name, c->hostname);
905 /* Check if the deleted host exists */
907 n = lookup_node(name);
911 syslog(LOG_WARNING, _("Got DEL_NODE from %s (%s) for %s which does not exist"), c->name, c->hostname, n->name);
915 /* Check if the rest matches */
917 if(address != n->address || port != n->port)
919 syslog(LOG_WARNING, _("Got DEL_NODE from %s (%s) for %s which doesn't match"), c->name, c->hostname, n->name);
923 /* Tell the rest about the deleted node */
925 for(node = connection_tree->head; node; node = node->next)
927 other = (connection_t *)node->data;
928 if(other->status.active && other != c)
929 send_del_node(other, n);
932 /* Delete the node */
939 /* Status and error notification routines */
941 int send_status(connection_t *c, int statusno, char *statusstring)
945 statusstring = status_text[statusno];
947 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
950 int status_h(connection_t *c)
953 char statusstring[MAX_STRING_SIZE];
955 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
957 syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"),
958 c->name, c->hostname);
962 if(debug_lvl >= DEBUG_STATUS)
964 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
965 c->name, c->hostname, status_text[statusno], statusstring);
972 int send_error(connection_t *c, int err, char *errstring)
976 errstring = strerror(err);
977 return send_request(c, "%d %d %s", ERROR, err, errstring);
980 int error_h(connection_t *c)
983 char errorstring[MAX_STRING_SIZE];
985 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
987 syslog(LOG_ERR, _("Got bad ERROR from %s (%s)"),
988 c->name, c->hostname);
992 if(debug_lvl >= DEBUG_ERROR)
994 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
995 c->name, c->hostname, strerror(err), errorstring);
998 terminate_connection(c, c->status.active);
1003 int send_termreq(connection_t *c)
1006 return send_request(c, "%d", TERMREQ);
1009 int termreq_h(connection_t *c)
1012 terminate_connection(c, c->status.active);
1017 int send_ping(connection_t *c)
1019 char salt[SALTLEN*2+1];
1021 c->status.pinged = 1;
1022 c->last_ping_time = time(NULL);
1023 RAND_pseudo_bytes(salt, SALTLEN);
1024 bin2hex(salt, salt, SALTLEN);
1025 salt[SALTLEN*2] = '\0';
1027 return send_request(c, "%d %s", PING, salt);
1030 int ping_h(connection_t *c)
1033 return send_pong(c);
1036 int send_pong(connection_t *c)
1038 char salt[SALTLEN*2+1];
1040 RAND_pseudo_bytes(salt, SALTLEN);
1041 bin2hex(salt, salt, SALTLEN);
1042 salt[SALTLEN*2] = '\0';
1044 return send_request(c, "%d %s", PONG, salt);
1047 int pong_h(connection_t *c)
1050 c->status.pinged = 0;
1057 int send_key_changed(connection_t *c, node_t *n)
1059 connection_t *other;
1062 /* Only send this message if some other daemon requested our key previously.
1063 This reduces unnecessary key_changed broadcasts.
1066 if(n == myself && !mykeyused)
1069 for(node = connection_tree->head; node; node = node->next)
1071 other = (connection_t *)node->data;
1072 if(other != c && other->status.active)
1073 send_request(other, "%d %s", KEY_CHANGED, n->name);
1079 int key_changed_h(connection_t *c)
1081 char name[MAX_STRING_SIZE];
1084 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
1086 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
1087 c->name, c->hostname);
1091 n = lookup_node(name);
1095 syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist"),
1096 c->name, c->hostname, name);
1100 n->status.validkey = 0;
1101 n->status.waitingforkey = 0;
1103 send_key_changed(c, n);
1108 int send_req_key(connection_t *c, node_t *from, node_t *to)
1111 return send_request(c, "%d %s %s", REQ_KEY,
1112 from->name, to->name);
1115 int req_key_h(connection_t *c)
1117 char from_name[MAX_STRING_SIZE];
1118 char to_name[MAX_STRING_SIZE];
1120 char key[MAX_STRING_SIZE];
1122 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
1124 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"),
1125 c->name, c->hostname);
1129 from = lookup_node(from_name);
1133 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"),
1134 c->name, c->hostname, from_name);
1138 to = lookup_node(to_name);
1142 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"),
1143 c->name, c->hostname, to_name);
1147 /* Check if this key request is for us */
1149 if(to == myself) /* Yes, send our own key back */
1151 bin2hex(myself->key, key, myself->keylength);
1152 key[myself->keylength * 2] = '\0';
1153 send_ans_key(c, myself, from, key);
1158 if(to->status.validkey) /* Proxy keys */
1160 bin2hex(to->key, key, to->keylength);
1161 key[to->keylength * 2] = '\0';
1162 send_ans_key(c, to, from, key);
1165 send_req_key(to->nexthop->connection, from, to);
1172 int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
1175 return send_request(c, "%d %s %s %s", ANS_KEY,
1176 from->name, to->name, key);
1179 int ans_key_h(connection_t *c)
1181 char from_name[MAX_STRING_SIZE];
1182 char to_name[MAX_STRING_SIZE];
1183 char key[MAX_STRING_SIZE];
1187 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
1189 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"),
1190 c->name, c->hostname);
1194 from = lookup_node(from_name);
1198 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"),
1199 c->name, c->hostname, from_name);
1203 to = lookup_node(to_name);
1207 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"),
1208 c->name, c->hostname, to_name);
1212 /* Check correctness of packet key */
1214 keylength = strlen(key);
1216 if(keylength != from->keylength * 2)
1218 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key length"),
1219 c->name, c->hostname, from->name);
1223 /* Forward it if necessary */
1227 send_ans_key(to->nexthop->connection, from, to, key);
1230 /* Update our copy of the origin's packet key */
1235 from->key = xstrdup(key);
1237 hex2bin(from->key, from->key, keylength);
1238 from->key[keylength] = '\0';
1240 from->status.validkey = 1;
1241 from->status.waitingforkey = 0;
1248 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
1254 x = send_request(c, "%d %hd", PACKET, packet->len);
1259 return send_meta(c, packet->data, packet->len);
1262 int tcppacket_h(connection_t *c)
1266 if(sscanf(c->buffer, "%*d %hd", &len) != 1)
1268 syslog(LOG_ERR, _("Got bad PACKET from %s (%s)"), c->name, c->hostname);
1272 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
1279 /* Jumptable for the request handlers */
1281 int (*request_handlers[])(connection_t*) = {
1282 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
1283 status_h, error_h, termreq_h,
1285 add_node_h, del_node_h,
1286 add_subnet_h, del_subnet_h,
1287 key_changed_h, req_key_h, ans_key_h,
1293 char (*request_name[]) = {
1294 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
1295 "STATUS", "ERROR", "TERMREQ",
1297 "ADD_NODE", "DEL_NODE",
1298 "ADD_SUBNET", "DEL_SUBNET",
1299 "ADD_VERTEX", "DEL_VERTEX",
1300 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
1304 /* Status strings */
1306 char (*status_text[]) = {
1312 char (*error_text[]) = {