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.111 2001/10/28 10:16:18 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. */
530 return send_request(c, "%d %d", ACK, myself->port);
533 int ack_h(connection_t *c)
538 avl_node_t *node, *node2;
540 if(sscanf(c->buffer, "%*d %hd", &port) != 1)
542 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
546 /* Check if we already have a node_t for him */
548 n = lookup_node(c->name);
553 n->name = xstrdup(c->name);
554 n->hostname = xstrdup(c->hostname);
557 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
565 /* Oh dear, we already have a connection to this node. */
566 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
567 terminate_connection(n->connection, 0);
570 /* FIXME: check if information in existing node matches that of the other end of this connection */
576 /* Check some options
578 if((cfg = get_config_val(c->config, config_indirectdata)))
580 if(cfg->data.val == stupid_true)
581 c->options |= OPTION_INDIRECT;
584 if((cfg = get_config_val(c->config, config_tcponly)))
586 if(cfg->data.val == stupid_true)
587 c->options |= OPTION_TCPONLY;
590 if((myself->options | c->options) & OPTION_INDIRECT)
597 /* Create a edge_t for this connection */
599 c->edge = new_edge();
601 c->edge->from = myself;
604 c->edge->connection = c;
608 /* Activate this connection */
610 c->allow_request = ALL;
611 c->status.active = 1;
613 if(debug_lvl >= DEBUG_CONNECTIONS)
614 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
617 /* Send him our subnets */
619 for(node = myself->subnet_tree->head; node; node = node->next)
621 s = (subnet_t *)node->data;
622 send_add_subnet(c, s);
625 /* And send him all known nodes and their subnets */
627 for(node = node_tree->head; node; node = node->next)
629 n = (node_t *)node->data;
631 if(n == c->node || n == myself)
634 /* Notify others of this connection */
637 send_add_node(n->connection, c->node);
639 /* Notify new connection of everything we know */
643 for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
645 s = (subnet_t *)node2->data;
646 send_add_subnet(c, s);
655 /* Address and subnet information exchange */
657 int send_add_subnet(connection_t *c, subnet_t *subnet)
662 x = send_request(c, "%d %s %s", ADD_SUBNET,
663 subnet->owner->name, netstr = net2str(subnet));
669 int add_subnet_h(connection_t *c)
671 char subnetstr[MAX_STRING_SIZE];
672 char name[MAX_STRING_SIZE];
678 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
680 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name, c->hostname);
684 /* Check if owner name is a valid */
688 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name"));
692 /* Check if subnet string is valid */
694 if(!(s = str2net(subnetstr)))
696 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
700 /* Check if the owner of the new subnet is in the connection list */
702 if(!(owner = lookup_node(name)))
704 syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
705 name, c->name, c->hostname);
709 /* If everything is correct, add the subnet to the list of the owner */
711 subnet_add(owner, s);
715 for(node = connection_tree->head; node; node = node->next)
717 other = (connection_t *)node->data;
718 if(other->status.active && other != c)
719 send_add_subnet(other, s);
725 int send_del_subnet(connection_t *c, subnet_t *s)
730 x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
736 int del_subnet_h(connection_t *c)
738 char subnetstr[MAX_STRING_SIZE];
739 char name[MAX_STRING_SIZE];
745 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
747 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
751 /* Check if owner name is a valid */
755 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
759 /* Check if subnet string is valid */
761 if(!(s = str2net(subnetstr)))
763 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
767 /* Check if the owner of the new subnet is in the connection list */
769 if(!(owner = lookup_node(name)))
771 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
772 "DEL_SUBNET", c->name, c->hostname, name);
776 /* If everything is correct, delete the subnet from the list of the owner */
778 find = lookup_subnet(owner, s);
782 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
783 "DEL_SUBNET", c->name, c->hostname, name);
787 subnet_del(owner, s);
791 for(node = connection_tree->head; node; node = node->next)
793 other = (connection_t *)node->data;
794 if(other->status.active && other != c)
795 send_del_subnet(other, s);
801 /* New and closed connections notification */
803 int send_add_node(connection_t *c, node_t *n)
806 return send_request(c, "%d %s %lx:%d", ADD_NODE,
807 n->name, n->address, n->port);
810 int add_node_h(connection_t *c)
814 char name[MAX_STRING_SIZE];
819 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
821 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
825 /* Check if identity is a valid name */
829 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
833 /* Check if node already exists */
835 n = lookup_node(name);
839 /* Check if it matches */
844 n->name = xstrdup(name);
845 n->address = address;
850 /* Tell the rest about the new node */
852 for(node = connection_tree->head; node; node = node->next)
854 other = (connection_t *)node->data;
855 if(other->status.active && other !=c)
856 send_add_node(other, n);
863 int send_del_node(connection_t *c, node_t *n)
866 return send_request(c, "%d %s %lx:%d", DEL_NODE,
867 n->name, n->address, n->port);
870 int del_node_h(connection_t *c)
873 char name[MAX_STRING_SIZE];
879 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
881 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
882 c->name, c->hostname);
886 /* Check if identity is a valid name */
890 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
894 /* Check if somebody tries to delete ourself */
896 if(!strcmp(name, myself->name))
898 syslog(LOG_ERR, _("Got %s from %s (%s) for ourself!"), "DEL_NODE",
899 c->name, c->hostname);
903 /* Check if the deleted host exists */
905 n = lookup_node(name);
909 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not exist"), "DEL_NODE", c->name, c->hostname, n->name);
913 /* Check if the rest matches */
915 if(address != n->address || port != n->port)
917 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which doesn't match"), "DEL_NODE", c->name, c->hostname, n->name);
921 /* Tell the rest about the deleted node */
923 for(node = connection_tree->head; node; node = node->next)
925 other = (connection_t *)node->data;
926 if(other->status.active && other != c)
927 send_del_node(other, n);
930 /* Delete the node */
939 int send_add_edge(connection_t *c, edge_t *e)
942 return send_request(c, "%d %s %s %lx", ADD_NODE,
943 e->from->name, e->to->name, e->options);
946 int add_edge_h(connection_t *c)
951 char from_name[MAX_STRING_SIZE];
952 char to_name[MAX_STRING_SIZE];
956 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx", from_name, to_name, &options) != 3)
958 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
962 /* Check if names are valid */
964 if(check_id(from_name))
966 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
970 if(check_id(to_name))
972 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
978 from = lookup_node(from_name);
982 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
986 to = lookup_node(to_name);
990 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
994 /* Check if node already exists */
996 e = lookup_edge(from, to);
1000 /* Check if it matches */
1007 e->options = options;
1011 /* Tell the rest about the new edge */
1013 for(node = connection_tree->head; node; node = node->next)
1015 other = (connection_t *)node->data;
1016 if(other->status.active && other != c)
1017 send_add_edge(other, e);
1024 int send_del_edge(connection_t *c, edge_t *e)
1027 return send_request(c, "%d %s %s %lx", DEL_EDGE,
1028 e->from->name, e->to->name, e->options);
1031 int del_edge_h(connection_t *c)
1034 char from_name[MAX_STRING_SIZE];
1035 char to_name[MAX_STRING_SIZE];
1038 connection_t *other;
1041 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx", from_name, to_name, &options) != 3)
1043 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
1044 c->name, c->hostname);
1048 /* Check if names are valid */
1050 if(check_id(from_name))
1052 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1056 if(check_id(to_name))
1058 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1064 from = lookup_node(from_name);
1068 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1072 to = lookup_node(to_name);
1076 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1080 /* Check if edge exists */
1082 e = lookup_edge(from, to);
1086 /* Check if it matches */
1090 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
1094 /* Tell the rest about the deleted edge */
1096 for(node = connection_tree->head; node; node = node->next)
1098 other = (connection_t *)node->data;
1099 if(other->status.active && other != c)
1100 send_del_edge(other, e);
1103 /* Delete the edge */
1111 /* Status and error notification routines */
1113 int send_status(connection_t *c, int statusno, char *statusstring)
1117 statusstring = status_text[statusno];
1119 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
1122 int status_h(connection_t *c)
1125 char statusstring[MAX_STRING_SIZE];
1127 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
1129 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
1130 c->name, c->hostname);
1134 if(debug_lvl >= DEBUG_STATUS)
1136 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
1137 c->name, c->hostname, status_text[statusno], statusstring);
1144 int send_error(connection_t *c, int err, char *errstring)
1148 errstring = strerror(err);
1149 return send_request(c, "%d %d %s", ERROR, err, errstring);
1152 int error_h(connection_t *c)
1155 char errorstring[MAX_STRING_SIZE];
1157 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
1159 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
1160 c->name, c->hostname);
1164 if(debug_lvl >= DEBUG_ERROR)
1166 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
1167 c->name, c->hostname, strerror(err), errorstring);
1170 terminate_connection(c, c->status.active);
1175 int send_termreq(connection_t *c)
1178 return send_request(c, "%d", TERMREQ);
1181 int termreq_h(connection_t *c)
1184 terminate_connection(c, c->status.active);
1189 int send_ping(connection_t *c)
1191 char salt[SALTLEN*2+1];
1193 c->status.pinged = 1;
1194 c->last_ping_time = time(NULL);
1195 RAND_pseudo_bytes(salt, SALTLEN);
1196 bin2hex(salt, salt, SALTLEN);
1197 salt[SALTLEN*2] = '\0';
1199 return send_request(c, "%d %s", PING, salt);
1202 int ping_h(connection_t *c)
1205 return send_pong(c);
1208 int send_pong(connection_t *c)
1210 char salt[SALTLEN*2+1];
1212 RAND_pseudo_bytes(salt, SALTLEN);
1213 bin2hex(salt, salt, SALTLEN);
1214 salt[SALTLEN*2] = '\0';
1216 return send_request(c, "%d %s", PONG, salt);
1219 int pong_h(connection_t *c)
1222 c->status.pinged = 0;
1229 int send_key_changed(connection_t *c, node_t *n)
1231 connection_t *other;
1234 /* Only send this message if some other daemon requested our key previously.
1235 This reduces unnecessary key_changed broadcasts.
1238 if(n == myself && !mykeyused)
1241 for(node = connection_tree->head; node; node = node->next)
1243 other = (connection_t *)node->data;
1244 if(other != c && other->status.active)
1245 send_request(other, "%d %s", KEY_CHANGED, n->name);
1251 int key_changed_h(connection_t *c)
1253 char name[MAX_STRING_SIZE];
1256 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
1258 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
1259 c->name, c->hostname);
1263 n = lookup_node(name);
1267 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
1268 c->name, c->hostname, name);
1272 n->status.validkey = 0;
1273 n->status.waitingforkey = 0;
1275 send_key_changed(c, n);
1280 int send_req_key(connection_t *c, node_t *from, node_t *to)
1283 return send_request(c, "%d %s %s", REQ_KEY,
1284 from->name, to->name);
1287 int req_key_h(connection_t *c)
1289 char from_name[MAX_STRING_SIZE];
1290 char to_name[MAX_STRING_SIZE];
1292 char key[MAX_STRING_SIZE];
1294 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
1296 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
1297 c->name, c->hostname);
1301 from = lookup_node(from_name);
1305 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
1306 c->name, c->hostname, from_name);
1310 to = lookup_node(to_name);
1314 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
1315 c->name, c->hostname, to_name);
1319 /* Check if this key request is for us */
1321 if(to == myself) /* Yes, send our own key back */
1323 bin2hex(myself->key, key, myself->keylength);
1324 key[myself->keylength * 2] = '\0';
1325 send_ans_key(c, myself, from, key);
1330 if(to->status.validkey) /* Proxy keys */
1332 bin2hex(to->key, key, to->keylength);
1333 key[to->keylength * 2] = '\0';
1334 send_ans_key(c, to, from, key);
1337 send_req_key(to->nexthop->connection, from, to);
1344 int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
1347 return send_request(c, "%d %s %s %s", ANS_KEY,
1348 from->name, to->name, key);
1351 int ans_key_h(connection_t *c)
1353 char from_name[MAX_STRING_SIZE];
1354 char to_name[MAX_STRING_SIZE];
1355 char key[MAX_STRING_SIZE];
1359 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
1361 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
1362 c->name, c->hostname);
1366 from = lookup_node(from_name);
1370 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
1371 c->name, c->hostname, from_name);
1375 to = lookup_node(to_name);
1379 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
1380 c->name, c->hostname, to_name);
1384 /* Check correctness of packet key */
1386 keylength = strlen(key);
1388 if(keylength != from->keylength * 2)
1390 syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
1391 c->name, c->hostname, from->name, _("invalid key length"));
1395 /* Forward it if necessary */
1399 send_ans_key(to->nexthop->connection, from, to, key);
1402 /* Update our copy of the origin's packet key */
1407 from->key = xstrdup(key);
1409 hex2bin(from->key, from->key, keylength);
1410 from->key[keylength] = '\0';
1412 from->status.validkey = 1;
1413 from->status.waitingforkey = 0;
1420 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
1426 x = send_request(c, "%d %hd", PACKET, packet->len);
1431 return send_meta(c, packet->data, packet->len);
1434 int tcppacket_h(connection_t *c)
1438 if(sscanf(c->buffer, "%*d %hd", &len) != 1)
1440 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
1444 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
1451 /* Jumptable for the request handlers */
1453 int (*request_handlers[])(connection_t*) = {
1454 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
1455 status_h, error_h, termreq_h,
1457 add_node_h, del_node_h,
1458 add_subnet_h, del_subnet_h,
1459 add_edge_h, del_edge_h,
1460 key_changed_h, req_key_h, ans_key_h,
1466 char (*request_name[]) = {
1467 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
1468 "STATUS", "ERROR", "TERMREQ",
1470 "ADD_NODE", "DEL_NODE",
1471 "ADD_SUBNET", "DEL_SUBNET",
1472 "ADD_EDGE", "DEL_EDGE",
1473 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
1477 /* Status strings */
1479 char (*status_text[]) = {
1485 char (*error_text[]) = {