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.112 2001/10/28 22:42:49 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];
181 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
183 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
187 /* Check if identity is a valid name */
191 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
195 /* If we set c->name in advance, make sure we are connected to the right host */
199 if(strcmp(c->name, name))
201 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
206 c->name = xstrdup(name);
208 /* Check if version matches */
210 if(c->protocol_version != myself->connection->protocol_version)
212 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
213 c->name, c->hostname, c->protocol_version);
220 init_configuration(&c->config_tree);
221 c->allow_request = ACK;
227 init_configuration(&c->config_tree);
229 if((bla = read_connection_config(c)))
231 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
235 if(read_rsa_public_key(c))
241 c->allow_request = METAKEY;
243 return send_metakey(c);
246 int send_metakey(connection_t *c)
251 len = RSA_size(c->rsa_key);
253 /* Allocate buffers for the meta key */
255 buffer = xmalloc(len*2+1);
258 c->outkey = xmalloc(len);
261 c->outctx = xmalloc(sizeof(*c->outctx));
263 /* Copy random data to the buffer */
265 RAND_bytes(c->outkey, len);
267 /* The message we send must be smaller than the modulus of the RSA key.
268 By definition, for a key of k bits, the following formula holds:
270 2^(k-1) <= modulus < 2^(k)
272 Where ^ means "to the power of", not "xor".
273 This means that to be sure, we must choose our message < 2^(k-1).
274 This can be done by setting the most significant bit to zero.
277 c->outkey[0] &= 0x7F;
279 if(debug_lvl >= DEBUG_SCARY_THINGS)
281 bin2hex(c->outkey, buffer, len);
282 buffer[len*2] = '\0';
283 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
286 /* Encrypt the random data
288 We do not use one of the PKCS padding schemes here.
289 This is allowed, because we encrypt a totally random string
290 with a length equal to that of the modulus of the RSA key.
293 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
295 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
300 /* Convert the encrypted random data to a hexadecimal formatted string */
302 bin2hex(buffer, buffer, len);
303 buffer[len*2] = '\0';
305 /* Send the meta key */
307 x = send_request(c, "%d %s", METAKEY, buffer);
310 /* Further outgoing requests are encrypted with the key we just generated */
312 EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
313 c->outkey + len - EVP_bf_cfb()->key_len,
314 c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
316 c->status.encryptout = 1;
321 int metakey_h(connection_t *c)
323 char buffer[MAX_STRING_SIZE];
326 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
328 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
332 len = RSA_size(myself->connection->rsa_key);
334 /* Check if the length of the meta key is all right */
336 if(strlen(buffer) != len*2)
338 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
342 /* Allocate buffers for the meta key */
345 c->inkey = xmalloc(len);
348 c->inctx = xmalloc(sizeof(*c->inctx));
350 /* Convert the challenge from hexadecimal back to binary */
352 hex2bin(buffer,buffer,len);
354 /* Decrypt the meta key */
356 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
358 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
362 if(debug_lvl >= DEBUG_SCARY_THINGS)
364 bin2hex(c->inkey, buffer, len);
365 buffer[len*2] = '\0';
366 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
369 /* All incoming requests will now be encrypted. */
371 EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
372 c->inkey + len - EVP_bf_cfb()->key_len,
373 c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
375 c->status.decryptin = 1;
377 c->allow_request = CHALLENGE;
379 return send_challenge(c);
382 int send_challenge(connection_t *c)
387 /* CHECKME: what is most reasonable value for len? */
389 len = RSA_size(c->rsa_key);
391 /* Allocate buffers for the challenge */
393 buffer = xmalloc(len*2+1);
396 free(c->hischallenge);
398 c->hischallenge = xmalloc(len);
400 /* Copy random data to the buffer */
402 RAND_bytes(c->hischallenge, len);
407 bin2hex(c->hischallenge, buffer, len);
408 buffer[len*2] = '\0';
411 /* Send the challenge */
413 x = send_request(c, "%d %s", CHALLENGE, buffer);
419 int challenge_h(connection_t *c)
421 char buffer[MAX_STRING_SIZE];
424 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
426 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
430 len = RSA_size(myself->connection->rsa_key);
432 /* Check if the length of the challenge is all right */
434 if(strlen(buffer) != len*2)
436 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
440 /* Allocate buffers for the challenge */
443 c->mychallenge = xmalloc(len);
445 /* Convert the challenge from hexadecimal back to binary */
447 hex2bin(buffer,c->mychallenge,len);
449 c->allow_request = CHAL_REPLY;
451 /* Rest is done by send_chal_reply() */
453 return send_chal_reply(c);
456 int send_chal_reply(connection_t *c)
458 char hash[SHA_DIGEST_LENGTH*2+1];
460 /* Calculate the hash from the challenge we received */
462 SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
464 /* Convert the hash to a hexadecimal formatted string */
466 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
467 hash[SHA_DIGEST_LENGTH*2] = '\0';
472 return send_request(c, "%d %s", CHAL_REPLY, hash);
475 int chal_reply_h(connection_t *c)
477 char hishash[MAX_STRING_SIZE];
478 char myhash[SHA_DIGEST_LENGTH];
480 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
482 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
486 /* Check if the length of the hash is all right */
488 if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
490 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
494 /* Convert the hash to binary format */
496 hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
498 /* Calculate the hash from the challenge we sent */
500 SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
502 /* Verify the incoming hash with the calculated hash */
504 if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
506 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
507 if(debug_lvl >= DEBUG_SCARY_THINGS)
509 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
510 hishash[SHA_DIGEST_LENGTH*2] = '\0';
511 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
516 /* Identity has now been positively verified.
517 Send an acknowledgement with the rest of the information needed.
520 c->allow_request = ACK;
525 int send_ack(connection_t *c)
527 /* ACK message contains rest of the information the other end needs
528 to create node_t and edge_t structures. */
532 /* Estimate weight */
534 gettimeofday(&now, NULL);
535 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
537 return send_request(c, "%d %hd %d", ACK, myself->port, c->estimated_weight);
540 int ack_h(connection_t *c)
546 avl_node_t *node, *node2;
548 if(sscanf(c->buffer, "%*d %hd %d", &port, &weight) != 2)
550 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
554 /* Check if we already have a node_t for him */
556 n = lookup_node(c->name);
561 n->name = xstrdup(c->name);
562 n->hostname = xstrdup(c->hostname);
565 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
573 /* Oh dear, we already have a connection to this node. */
574 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
575 terminate_connection(n->connection, 0);
578 /* FIXME: check if information in existing node matches that of the other end of this connection */
584 /* Check some options
586 if((cfg = get_config_val(c->config, config_indirectdata)))
588 if(cfg->data.val == stupid_true)
589 c->options |= OPTION_INDIRECT;
592 if((cfg = get_config_val(c->config, config_tcponly)))
594 if(cfg->data.val == stupid_true)
595 c->options |= OPTION_TCPONLY;
598 if((myself->options | c->options) & OPTION_INDIRECT)
605 /* Create a edge_t for this connection */
607 c->edge = new_edge();
609 c->edge->from = myself;
611 c->edge->weight = (weight + c->estimated_weight) / 2;
612 c->edge->connection = c;
616 /* Activate this connection */
618 c->allow_request = ALL;
619 c->status.active = 1;
621 if(debug_lvl >= DEBUG_CONNECTIONS)
622 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
625 /* Send him our subnets */
627 for(node = myself->subnet_tree->head; node; node = node->next)
629 s = (subnet_t *)node->data;
630 send_add_subnet(c, s);
633 /* And send him all known nodes and their subnets */
635 for(node = node_tree->head; node; node = node->next)
637 n = (node_t *)node->data;
639 if(n == c->node || n == myself)
642 /* Notify others of this connection */
645 send_add_node(n->connection, c->node);
647 /* Notify new connection of everything we know */
651 for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
653 s = (subnet_t *)node2->data;
654 send_add_subnet(c, s);
663 /* Address and subnet information exchange */
665 int send_add_subnet(connection_t *c, subnet_t *subnet)
670 x = send_request(c, "%d %s %s", ADD_SUBNET,
671 subnet->owner->name, netstr = net2str(subnet));
677 int add_subnet_h(connection_t *c)
679 char subnetstr[MAX_STRING_SIZE];
680 char name[MAX_STRING_SIZE];
686 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
688 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name, c->hostname);
692 /* Check if owner name is a valid */
696 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name"));
700 /* Check if subnet string is valid */
702 if(!(s = str2net(subnetstr)))
704 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
708 /* Check if the owner of the new subnet is in the connection list */
710 if(!(owner = lookup_node(name)))
712 syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
713 name, c->name, c->hostname);
717 /* If everything is correct, add the subnet to the list of the owner */
719 subnet_add(owner, s);
723 for(node = connection_tree->head; node; node = node->next)
725 other = (connection_t *)node->data;
726 if(other->status.active && other != c)
727 send_add_subnet(other, s);
733 int send_del_subnet(connection_t *c, subnet_t *s)
738 x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
744 int del_subnet_h(connection_t *c)
746 char subnetstr[MAX_STRING_SIZE];
747 char name[MAX_STRING_SIZE];
753 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
755 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
759 /* Check if owner name is a valid */
763 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
767 /* Check if subnet string is valid */
769 if(!(s = str2net(subnetstr)))
771 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
775 /* Check if the owner of the new subnet is in the connection list */
777 if(!(owner = lookup_node(name)))
779 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
780 "DEL_SUBNET", c->name, c->hostname, name);
784 /* If everything is correct, delete the subnet from the list of the owner */
786 find = lookup_subnet(owner, s);
790 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
791 "DEL_SUBNET", c->name, c->hostname, name);
795 subnet_del(owner, s);
799 for(node = connection_tree->head; node; node = node->next)
801 other = (connection_t *)node->data;
802 if(other->status.active && other != c)
803 send_del_subnet(other, s);
809 /* New and closed connections notification */
811 int send_add_node(connection_t *c, node_t *n)
814 return send_request(c, "%d %s %lx:%d", ADD_NODE,
815 n->name, n->address, n->port);
818 int add_node_h(connection_t *c)
822 char name[MAX_STRING_SIZE];
827 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
829 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
833 /* Check if identity is a valid name */
837 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
841 /* Check if node already exists */
843 n = lookup_node(name);
847 /* Check if it matches */
852 n->name = xstrdup(name);
853 n->address = address;
858 /* Tell the rest about the new node */
860 for(node = connection_tree->head; node; node = node->next)
862 other = (connection_t *)node->data;
863 if(other->status.active && other !=c)
864 send_add_node(other, n);
871 int send_del_node(connection_t *c, node_t *n)
874 return send_request(c, "%d %s %lx:%d", DEL_NODE,
875 n->name, n->address, n->port);
878 int del_node_h(connection_t *c)
881 char name[MAX_STRING_SIZE];
887 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
889 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
890 c->name, c->hostname);
894 /* Check if identity is a valid name */
898 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
902 /* Check if somebody tries to delete ourself */
904 if(!strcmp(name, myself->name))
906 syslog(LOG_ERR, _("Got %s from %s (%s) for ourself!"), "DEL_NODE",
907 c->name, c->hostname);
911 /* Check if the deleted host exists */
913 n = lookup_node(name);
917 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not exist"), "DEL_NODE", c->name, c->hostname, n->name);
921 /* Check if the rest matches */
923 if(address != n->address || port != n->port)
925 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which doesn't match"), "DEL_NODE", c->name, c->hostname, n->name);
929 /* Tell the rest about the deleted node */
931 for(node = connection_tree->head; node; node = node->next)
933 other = (connection_t *)node->data;
934 if(other->status.active && other != c)
935 send_del_node(other, n);
938 /* Delete the node */
947 int send_add_edge(connection_t *c, edge_t *e)
950 return send_request(c, "%d %s %s %lx", ADD_NODE,
951 e->from->name, e->to->name, e->options);
954 int add_edge_h(connection_t *c)
959 char from_name[MAX_STRING_SIZE];
960 char to_name[MAX_STRING_SIZE];
964 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx", from_name, to_name, &options) != 3)
966 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
970 /* Check if names are valid */
972 if(check_id(from_name))
974 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
978 if(check_id(to_name))
980 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
986 from = lookup_node(from_name);
990 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
994 to = lookup_node(to_name);
998 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1002 /* Check if node already exists */
1004 e = lookup_edge(from, to);
1008 /* Check if it matches */
1015 e->options = options;
1019 /* Tell the rest about the new edge */
1021 for(node = connection_tree->head; node; node = node->next)
1023 other = (connection_t *)node->data;
1024 if(other->status.active && other != c)
1025 send_add_edge(other, e);
1032 int send_del_edge(connection_t *c, edge_t *e)
1035 return send_request(c, "%d %s %s %lx", DEL_EDGE,
1036 e->from->name, e->to->name, e->options);
1039 int del_edge_h(connection_t *c)
1042 char from_name[MAX_STRING_SIZE];
1043 char to_name[MAX_STRING_SIZE];
1046 connection_t *other;
1049 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx", from_name, to_name, &options) != 3)
1051 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
1052 c->name, c->hostname);
1056 /* Check if names are valid */
1058 if(check_id(from_name))
1060 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1064 if(check_id(to_name))
1066 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1072 from = lookup_node(from_name);
1076 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1080 to = lookup_node(to_name);
1084 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1088 /* Check if edge exists */
1090 e = lookup_edge(from, to);
1094 /* Check if it matches */
1098 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
1102 /* Tell the rest about the deleted edge */
1104 for(node = connection_tree->head; node; node = node->next)
1106 other = (connection_t *)node->data;
1107 if(other->status.active && other != c)
1108 send_del_edge(other, e);
1111 /* Delete the edge */
1119 /* Status and error notification routines */
1121 int send_status(connection_t *c, int statusno, char *statusstring)
1125 statusstring = status_text[statusno];
1127 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
1130 int status_h(connection_t *c)
1133 char statusstring[MAX_STRING_SIZE];
1135 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
1137 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
1138 c->name, c->hostname);
1142 if(debug_lvl >= DEBUG_STATUS)
1144 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
1145 c->name, c->hostname, status_text[statusno], statusstring);
1152 int send_error(connection_t *c, int err, char *errstring)
1156 errstring = strerror(err);
1157 return send_request(c, "%d %d %s", ERROR, err, errstring);
1160 int error_h(connection_t *c)
1163 char errorstring[MAX_STRING_SIZE];
1165 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
1167 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
1168 c->name, c->hostname);
1172 if(debug_lvl >= DEBUG_ERROR)
1174 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
1175 c->name, c->hostname, strerror(err), errorstring);
1178 terminate_connection(c, c->status.active);
1183 int send_termreq(connection_t *c)
1186 return send_request(c, "%d", TERMREQ);
1189 int termreq_h(connection_t *c)
1192 terminate_connection(c, c->status.active);
1197 int send_ping(connection_t *c)
1199 char salt[SALTLEN*2+1];
1201 c->status.pinged = 1;
1202 c->last_ping_time = time(NULL);
1203 RAND_pseudo_bytes(salt, SALTLEN);
1204 bin2hex(salt, salt, SALTLEN);
1205 salt[SALTLEN*2] = '\0';
1207 return send_request(c, "%d %s", PING, salt);
1210 int ping_h(connection_t *c)
1213 return send_pong(c);
1216 int send_pong(connection_t *c)
1218 char salt[SALTLEN*2+1];
1220 RAND_pseudo_bytes(salt, SALTLEN);
1221 bin2hex(salt, salt, SALTLEN);
1222 salt[SALTLEN*2] = '\0';
1224 return send_request(c, "%d %s", PONG, salt);
1227 int pong_h(connection_t *c)
1230 c->status.pinged = 0;
1237 int send_key_changed(connection_t *c, node_t *n)
1239 connection_t *other;
1242 /* Only send this message if some other daemon requested our key previously.
1243 This reduces unnecessary key_changed broadcasts.
1246 if(n == myself && !mykeyused)
1249 for(node = connection_tree->head; node; node = node->next)
1251 other = (connection_t *)node->data;
1252 if(other != c && other->status.active)
1253 send_request(other, "%d %s", KEY_CHANGED, n->name);
1259 int key_changed_h(connection_t *c)
1261 char name[MAX_STRING_SIZE];
1264 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
1266 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
1267 c->name, c->hostname);
1271 n = lookup_node(name);
1275 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
1276 c->name, c->hostname, name);
1280 n->status.validkey = 0;
1281 n->status.waitingforkey = 0;
1283 send_key_changed(c, n);
1288 int send_req_key(connection_t *c, node_t *from, node_t *to)
1291 return send_request(c, "%d %s %s", REQ_KEY,
1292 from->name, to->name);
1295 int req_key_h(connection_t *c)
1297 char from_name[MAX_STRING_SIZE];
1298 char to_name[MAX_STRING_SIZE];
1300 char key[MAX_STRING_SIZE];
1302 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
1304 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
1305 c->name, c->hostname);
1309 from = lookup_node(from_name);
1313 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
1314 c->name, c->hostname, from_name);
1318 to = lookup_node(to_name);
1322 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
1323 c->name, c->hostname, to_name);
1327 /* Check if this key request is for us */
1329 if(to == myself) /* Yes, send our own key back */
1331 bin2hex(myself->key, key, myself->keylength);
1332 key[myself->keylength * 2] = '\0';
1333 send_ans_key(c, myself, from, key);
1338 if(to->status.validkey) /* Proxy keys */
1340 bin2hex(to->key, key, to->keylength);
1341 key[to->keylength * 2] = '\0';
1342 send_ans_key(c, to, from, key);
1345 send_req_key(to->nexthop->connection, from, to);
1352 int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
1355 return send_request(c, "%d %s %s %s", ANS_KEY,
1356 from->name, to->name, key);
1359 int ans_key_h(connection_t *c)
1361 char from_name[MAX_STRING_SIZE];
1362 char to_name[MAX_STRING_SIZE];
1363 char key[MAX_STRING_SIZE];
1367 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
1369 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
1370 c->name, c->hostname);
1374 from = lookup_node(from_name);
1378 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
1379 c->name, c->hostname, from_name);
1383 to = lookup_node(to_name);
1387 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
1388 c->name, c->hostname, to_name);
1392 /* Check correctness of packet key */
1394 keylength = strlen(key);
1396 if(keylength != from->keylength * 2)
1398 syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
1399 c->name, c->hostname, from->name, _("invalid key length"));
1403 /* Forward it if necessary */
1407 send_ans_key(to->nexthop->connection, from, to, key);
1410 /* Update our copy of the origin's packet key */
1415 from->key = xstrdup(key);
1417 hex2bin(from->key, from->key, keylength);
1418 from->key[keylength] = '\0';
1420 from->status.validkey = 1;
1421 from->status.waitingforkey = 0;
1428 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
1434 x = send_request(c, "%d %hd", PACKET, packet->len);
1439 return send_meta(c, packet->data, packet->len);
1442 int tcppacket_h(connection_t *c)
1446 if(sscanf(c->buffer, "%*d %hd", &len) != 1)
1448 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
1452 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
1459 /* Jumptable for the request handlers */
1461 int (*request_handlers[])(connection_t*) = {
1462 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
1463 status_h, error_h, termreq_h,
1465 add_node_h, del_node_h,
1466 add_subnet_h, del_subnet_h,
1467 add_edge_h, del_edge_h,
1468 key_changed_h, req_key_h, ans_key_h,
1474 char (*request_name[]) = {
1475 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
1476 "STATUS", "ERROR", "TERMREQ",
1478 "ADD_NODE", "DEL_NODE",
1479 "ADD_SUBNET", "DEL_SUBNET",
1480 "ADD_EDGE", "DEL_EDGE",
1481 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
1485 /* Status strings */
1487 char (*status_text[]) = {
1493 char (*error_text[]) = {