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.120 2001/11/04 23:48:27 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"
65 int check_id(char *id)
69 for (i = 0; i < strlen(id); i++)
70 if(!isalnum(id[i]) && id[i] != '_')
76 /* Generic request routines - takes care of logging and error
79 int send_request(connection_t *c, const char *format, ...)
82 char buffer[MAXBUFSIZE];
86 /* Use vsnprintf instead of vasprintf: faster, no memory
87 fragmentation, cleanup is automatic, and there is a limit on the
88 input buffer anyway */
90 va_start(args, format);
91 len = vsnprintf(buffer, MAXBUFSIZE, format, args);
92 request = va_arg(args, int);
95 if(len < 0 || len > MAXBUFSIZE-1)
97 syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
101 if(debug_lvl >= DEBUG_PROTOCOL)
103 if(debug_lvl >= DEBUG_META)
104 syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
106 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
109 buffer[len++] = '\n';
111 return send_meta(c, buffer, len);
114 int receive_request(connection_t *c)
118 if(sscanf(c->buffer, "%d", &request) == 1)
120 if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
122 if(debug_lvl >= DEBUG_META)
123 syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
124 c->name, c->hostname, c->buffer);
126 syslog(LOG_ERR, _("Unknown request from %s (%s)"),
127 c->name, c->hostname);
133 if(debug_lvl >= DEBUG_PROTOCOL)
135 if(debug_lvl >= DEBUG_META)
136 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
137 request_name[request], c->name, c->hostname, c->buffer);
139 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
140 request_name[request], c->name, c->hostname);
144 if((c->allow_request != ALL) && (c->allow_request != request))
146 syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
150 if(request_handlers[request](c))
151 /* Something went wrong. Probably scriptkiddies. Terminate. */
153 syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
154 request_name[request], c->name, c->hostname);
160 syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
161 c->name, c->hostname);
168 /* The authentication protocol is described in detail in doc/SECURITY2,
169 the rest will be described in doc/PROTOCOL. */
171 int send_id(connection_t *c)
174 return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
177 int id_h(connection_t *c)
179 char name[MAX_STRING_SIZE];
182 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
184 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
188 /* Check if identity is a valid name */
192 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
196 /* If we set c->name in advance, make sure we are connected to the right host */
200 if(strcmp(c->name, name))
202 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
207 c->name = xstrdup(name);
209 /* Check if version matches */
211 if(c->protocol_version != myself->connection->protocol_version)
213 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
214 c->name, c->hostname, c->protocol_version);
221 init_configuration(&c->config_tree);
222 c->allow_request = ACK;
228 init_configuration(&c->config_tree);
230 if((bla = read_connection_config(c)))
232 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
237 if(read_rsa_public_key(c))
242 c->allow_request = METAKEY;
244 return send_metakey(c);
247 int send_metakey(connection_t *c)
249 char buffer[MAX_STRING_SIZE];
252 len = RSA_size(c->rsa_key);
254 /* Allocate buffers for the meta key */
257 c->outkey = xmalloc(len);
260 c->outctx = xmalloc(sizeof(*c->outctx));
262 /* Copy random data to the buffer */
264 RAND_bytes(c->outkey, len);
266 /* The message we send must be smaller than the modulus of the RSA key.
267 By definition, for a key of k bits, the following formula holds:
269 2^(k-1) <= modulus < 2^(k)
271 Where ^ means "to the power of", not "xor".
272 This means that to be sure, we must choose our message < 2^(k-1).
273 This can be done by setting the most significant bit to zero.
276 c->outkey[0] &= 0x7F;
278 if(debug_lvl >= DEBUG_SCARY_THINGS)
280 bin2hex(c->outkey, buffer, len);
281 buffer[len*2] = '\0';
282 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
285 /* Encrypt the random data
287 We do not use one of the PKCS padding schemes here.
288 This is allowed, because we encrypt a totally random string
289 with a length equal to that of the modulus of the RSA key.
292 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
294 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
298 /* Convert the encrypted random data to a hexadecimal formatted string */
300 bin2hex(buffer, buffer, len);
301 buffer[len*2] = '\0';
303 /* Send the meta key */
305 x = send_request(c, "%d %s", METAKEY, buffer);
307 /* Further outgoing requests are encrypted with the key we just generated */
309 EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
310 c->outkey + len - EVP_bf_cfb()->key_len,
311 c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
313 c->status.encryptout = 1;
318 int metakey_h(connection_t *c)
320 char buffer[MAX_STRING_SIZE];
323 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
325 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
329 len = RSA_size(myself->connection->rsa_key);
331 /* Check if the length of the meta key is all right */
333 if(strlen(buffer) != len*2)
335 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
339 /* Allocate buffers for the meta key */
342 c->inkey = xmalloc(len);
345 c->inctx = xmalloc(sizeof(*c->inctx));
347 /* Convert the challenge from hexadecimal back to binary */
349 hex2bin(buffer,buffer,len);
351 /* Decrypt the meta key */
353 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
355 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
359 if(debug_lvl >= DEBUG_SCARY_THINGS)
361 bin2hex(c->inkey, buffer, len);
362 buffer[len*2] = '\0';
363 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
366 /* All incoming requests will now be encrypted. */
368 EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
369 c->inkey + len - EVP_bf_cfb()->key_len,
370 c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
372 c->status.decryptin = 1;
374 c->allow_request = CHALLENGE;
376 return send_challenge(c);
379 int send_challenge(connection_t *c)
381 char buffer[MAX_STRING_SIZE];
384 /* CHECKME: what is most reasonable value for len? */
386 len = RSA_size(c->rsa_key);
388 /* Allocate buffers for the challenge */
391 c->hischallenge = xmalloc(len);
393 /* Copy random data to the buffer */
395 RAND_bytes(c->hischallenge, len);
400 bin2hex(c->hischallenge, buffer, len);
401 buffer[len*2] = '\0';
404 /* Send the challenge */
406 x = send_request(c, "%d %s", CHALLENGE, buffer);
411 int challenge_h(connection_t *c)
413 char buffer[MAX_STRING_SIZE];
416 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
418 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
422 len = RSA_size(myself->connection->rsa_key);
424 /* Check if the length of the challenge is all right */
426 if(strlen(buffer) != len*2)
428 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
432 /* Allocate buffers for the challenge */
435 c->mychallenge = xmalloc(len);
437 /* Convert the challenge from hexadecimal back to binary */
439 hex2bin(buffer,c->mychallenge,len);
441 c->allow_request = CHAL_REPLY;
443 /* Rest is done by send_chal_reply() */
445 return send_chal_reply(c);
448 int send_chal_reply(connection_t *c)
450 char hash[SHA_DIGEST_LENGTH*2+1];
452 /* Calculate the hash from the challenge we received */
454 SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
456 /* Convert the hash to a hexadecimal formatted string */
458 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
459 hash[SHA_DIGEST_LENGTH*2] = '\0';
464 return send_request(c, "%d %s", CHAL_REPLY, hash);
467 int chal_reply_h(connection_t *c)
469 char hishash[MAX_STRING_SIZE];
470 char myhash[SHA_DIGEST_LENGTH];
472 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
474 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
478 /* Check if the length of the hash is all right */
480 if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
482 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
486 /* Convert the hash to binary format */
488 hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
490 /* Calculate the hash from the challenge we sent */
492 SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
494 /* Verify the incoming hash with the calculated hash */
496 if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
498 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
499 if(debug_lvl >= DEBUG_SCARY_THINGS)
501 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
502 hishash[SHA_DIGEST_LENGTH*2] = '\0';
503 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
508 /* Identity has now been positively verified.
509 Send an acknowledgement with the rest of the information needed.
512 c->allow_request = ACK;
517 int send_ack(connection_t *c)
519 /* ACK message contains rest of the information the other end needs
520 to create node_t and edge_t structures. */
524 /* Estimate weight */
526 gettimeofday(&now, NULL);
527 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
529 return send_request(c, "%d %hd %d", ACK, myself->port, c->estimated_weight);
532 int ack_h(connection_t *c)
540 avl_node_t *node, *node2;
542 if(sscanf(c->buffer, "%*d %hd %d", &port, &weight) != 2)
544 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
548 /* Check if we already have a node_t for him */
550 n = lookup_node(c->name);
555 n->name = xstrdup(c->name);
556 n->address = c->address;
557 n->hostname = xstrdup(c->hostname);
560 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
568 /* Oh dear, we already have a connection to this node. */
569 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
570 terminate_connection(n->connection, 0);
573 /* FIXME: check if information in existing node matches that of the other end of this connection */
579 /* Check some options
581 if((cfg = get_config_val(c->config, config_indirectdata)))
583 if(cfg->data.val == stupid_true)
584 c->options |= OPTION_INDIRECT;
587 if((cfg = get_config_val(c->config, config_tcponly)))
589 if(cfg->data.val == stupid_true)
590 c->options |= OPTION_TCPONLY;
593 if((myself->options | c->options) & OPTION_INDIRECT)
600 /* Create an edge_t for this connection */
602 c->edge = new_edge();
604 c->edge->from = myself;
606 c->edge->weight = (weight + c->estimated_weight) / 2;
607 c->edge->connection = c;
611 /* Activate this connection */
613 c->allow_request = ALL;
614 c->status.active = 1;
615 c->node->cipher = EVP_bf_cbc();
616 c->node->keylength = c->node->cipher->key_len + c->node->cipher->iv_len;
618 if(debug_lvl >= DEBUG_CONNECTIONS)
619 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
622 /* Send him our subnets */
624 for(node = myself->subnet_tree->head; node; node = node->next)
626 s = (subnet_t *)node->data;
627 send_add_subnet(c, s);
630 /* And send him all known nodes and their subnets */
632 for(node = node_tree->head; node; node = node->next)
634 n = (node_t *)node->data;
636 if(n == c->node || n == myself)
641 for(node2 = c->node->subnet_tree->head; node2; node2 = node2->next)
643 s = (subnet_t *)node2->data;
644 send_add_subnet(c, s);
648 /* Send all known edges */
650 for(node = edge_tree->head; node; node = node->next)
652 e = (edge_t *)node->data;
660 /* Notify others of this connection */
662 for(node = connection_tree->head; node; node = node->next)
664 other = (connection_t *)node->data;
666 if(other->status.active && other != c)
668 send_add_node(other, c->node);
669 send_add_edge(other, c->edge);
673 /* Run MST and SSSP algorithms */
683 /* Address and subnet information exchange */
685 int send_add_subnet(connection_t *c, subnet_t *subnet)
690 x = send_request(c, "%d %s %s", ADD_SUBNET,
691 subnet->owner->name, netstr = net2str(subnet));
697 int add_subnet_h(connection_t *c)
699 char subnetstr[MAX_STRING_SIZE];
700 char name[MAX_STRING_SIZE];
706 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 2)
708 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name, c->hostname);
712 /* Check if owner name is a valid */
716 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid name"));
720 /* Check if subnet string is valid */
722 if(!(s = str2net(subnetstr)))
724 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name, c->hostname, _("invalid subnet string"));
728 /* Check if the owner of the new subnet is in the connection list */
730 owner = lookup_node(name);
734 syslog(LOG_ERR, _("Got ADD_SUBNET from %s (%s) for %s which is not in our connection list"),
735 c->name, c->hostname, name);
739 /* Check if we already know this subnet */
741 if(lookup_subnet(owner, s))
747 /* If everything is correct, add the subnet to the list of the owner */
749 subnet_add(owner, s);
753 for(node = connection_tree->head; node; node = node->next)
755 other = (connection_t *)node->data;
756 if(other->status.active && other != c)
757 send_add_subnet(other, s);
763 int send_del_subnet(connection_t *c, subnet_t *s)
768 x = send_request(c, "%d %s %s", DEL_SUBNET, s->owner->name, netstr = net2str(s));
774 int del_subnet_h(connection_t *c)
776 char subnetstr[MAX_STRING_SIZE];
777 char name[MAX_STRING_SIZE];
783 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, subnetstr) != 3)
785 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name, c->hostname);
789 /* Check if owner name is a valid */
793 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid name"));
797 /* Check if the owner of the new subnet is in the connection list */
799 if(!(owner = lookup_node(name)))
801 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which is not in our connection list"),
802 "DEL_SUBNET", c->name, c->hostname, name);
806 /* Check if subnet string is valid */
808 if(!(s = str2net(subnetstr)))
810 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name, c->hostname, _("invalid subnet string"));
814 /* If everything is correct, delete the subnet from the list of the owner */
816 find = lookup_subnet(owner, s);
822 syslog(LOG_ERR, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
823 "DEL_SUBNET", c->name, c->hostname, name);
827 subnet_del(owner, find);
831 for(node = connection_tree->head; node; node = node->next)
833 other = (connection_t *)node->data;
834 if(other->status.active && other != c)
835 send_del_subnet(other, s);
841 /* New and closed connections notification */
843 int send_add_node(connection_t *c, node_t *n)
846 return send_request(c, "%d %s %lx:%d", ADD_NODE,
847 n->name, n->address, n->port);
850 int add_node_h(connection_t *c)
854 char name[MAX_STRING_SIZE];
859 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
861 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
865 /* Check if identity is a valid name */
869 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
873 /* Check if node already exists */
875 n = lookup_node(name);
879 /* Check if it matches */
881 if(n->address != address || n->port != port)
882 syslog(LOG_DEBUG, _("Got %s from %s (%s) for %s which does not match existing entry"), "ADD_NODE", c->name, c->hostname, n->name);
889 n->name = xstrdup(name);
890 n->address = address;
895 /* Tell the rest about the new node */
897 for(node = connection_tree->head; node; node = node->next)
899 other = (connection_t *)node->data;
900 if(other->status.active && other !=c)
901 send_add_node(other, n);
908 int send_del_node(connection_t *c, node_t *n)
911 return send_request(c, "%d %s %lx:%d", DEL_NODE,
912 n->name, n->address, n->port);
915 int del_node_h(connection_t *c)
918 char name[MAX_STRING_SIZE];
924 if(sscanf(c->buffer, "%*d "MAX_STRING" %lx:%hd", name, &address, &port) != 3)
926 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
927 c->name, c->hostname);
931 /* Check if identity is a valid name */
935 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
939 /* Check if somebody tries to delete ourself */
941 if(!strcmp(name, myself->name))
943 syslog(LOG_ERR, _("Got %s from %s (%s) for ourself!"), "DEL_NODE",
944 c->name, c->hostname);
948 /* Check if the deleted host exists */
950 n = lookup_node(name);
954 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not exist"), "DEL_NODE", c->name, c->hostname, n->name);
958 /* Check if the rest matches */
960 if(address != n->address || port != n->port)
962 syslog(LOG_WARNING, _("Got %s from %s (%s) for %s which does not match existing entry"), "DEL_NODE", c->name, c->hostname, n->name);
965 /* Tell the rest about the deleted node */
967 for(node = connection_tree->head; node; node = node->next)
969 other = (connection_t *)node->data;
970 if(other->status.active && other != c)
971 send_del_node(other, n);
974 /* Delete the node */
986 int send_add_edge(connection_t *c, edge_t *e)
989 return send_request(c, "%d %s %s %lx %d", ADD_EDGE,
990 e->from->name, e->to->name, e->options, e->weight);
993 int add_edge_h(connection_t *c)
998 char from_name[MAX_STRING_SIZE];
999 char to_name[MAX_STRING_SIZE];
1004 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
1006 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
1010 /* Check if names are valid */
1012 if(check_id(from_name))
1014 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
1018 if(check_id(to_name))
1020 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
1026 from = lookup_node(from_name);
1030 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1034 to = lookup_node(to_name);
1038 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("unknown node"));
1042 /* Check if edge already exists */
1044 e = lookup_edge(from, to);
1048 if(e->weight != weight || e->options != options)
1050 syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
1061 e->options = options;
1066 /* Tell the rest about the new edge */
1068 for(node = connection_tree->head; node; node = node->next)
1070 other = (connection_t *)node->data;
1071 if(other->status.active && other != c)
1072 send_add_edge(other, e);
1075 /* Run MST before or after we tell the rest? */
1083 int send_del_edge(connection_t *c, edge_t *e)
1086 return send_request(c, "%d %s %s %lx %d", DEL_EDGE,
1087 e->from->name, e->to->name, e->options, e->weight);
1090 int del_edge_h(connection_t *c)
1093 char from_name[MAX_STRING_SIZE];
1094 char to_name[MAX_STRING_SIZE];
1098 connection_t *other;
1101 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %lx %d", from_name, to_name, &options, &weight) != 4)
1103 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
1104 c->name, c->hostname);
1108 /* Check if names are valid */
1110 if(check_id(from_name))
1112 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1116 if(check_id(to_name))
1118 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
1124 from = lookup_node(from_name);
1128 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1132 to = lookup_node(to_name);
1136 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown node"));
1140 /* Check if edge exists */
1142 e = lookup_edge(from, to);
1146 if(e->weight != weight || e->options != options)
1148 syslog(LOG_ERR, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
1154 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("unknown edge"));
1158 /* Tell the rest about the deleted edge */
1160 for(node = connection_tree->head; node; node = node->next)
1162 other = (connection_t *)node->data;
1163 if(other->status.active && other != c)
1164 send_del_edge(other, e);
1167 /* Delete the edge */
1171 /* Run MST before or after we tell the rest? */
1180 /* Status and error notification routines */
1182 int send_status(connection_t *c, int statusno, char *statusstring)
1186 statusstring = status_text[statusno];
1188 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
1191 int status_h(connection_t *c)
1194 char statusstring[MAX_STRING_SIZE];
1196 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &statusno, statusstring) != 2)
1198 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
1199 c->name, c->hostname);
1203 if(debug_lvl >= DEBUG_STATUS)
1205 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"),
1206 c->name, c->hostname, status_text[statusno], statusstring);
1213 int send_error(connection_t *c, int err, char *errstring)
1217 errstring = strerror(err);
1218 return send_request(c, "%d %d %s", ERROR, err, errstring);
1221 int error_h(connection_t *c)
1224 char errorstring[MAX_STRING_SIZE];
1226 if(sscanf(c->buffer, "%*d %d "MAX_STRING, &err, errorstring) != 2)
1228 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
1229 c->name, c->hostname);
1233 if(debug_lvl >= DEBUG_ERROR)
1235 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"),
1236 c->name, c->hostname, strerror(err), errorstring);
1239 terminate_connection(c, c->status.active);
1244 int send_termreq(connection_t *c)
1247 return send_request(c, "%d", TERMREQ);
1250 int termreq_h(connection_t *c)
1253 terminate_connection(c, c->status.active);
1258 int send_ping(connection_t *c)
1260 char salt[SALTLEN*2+1];
1262 c->status.pinged = 1;
1263 c->last_ping_time = time(NULL);
1264 RAND_pseudo_bytes(salt, SALTLEN);
1265 bin2hex(salt, salt, SALTLEN);
1266 salt[SALTLEN*2] = '\0';
1268 return send_request(c, "%d %s", PING, salt);
1271 int ping_h(connection_t *c)
1274 return send_pong(c);
1277 int send_pong(connection_t *c)
1279 char salt[SALTLEN*2+1];
1281 RAND_pseudo_bytes(salt, SALTLEN);
1282 bin2hex(salt, salt, SALTLEN);
1283 salt[SALTLEN*2] = '\0';
1285 return send_request(c, "%d %s", PONG, salt);
1288 int pong_h(connection_t *c)
1291 c->status.pinged = 0;
1298 int send_key_changed(connection_t *c, node_t *n)
1300 connection_t *other;
1303 /* Only send this message if some other daemon requested our key previously.
1304 This reduces unnecessary key_changed broadcasts.
1307 if(n == myself && !mykeyused)
1310 for(node = connection_tree->head; node; node = node->next)
1312 other = (connection_t *)node->data;
1313 if(other->status.active && other != c)
1314 send_request(other, "%d %s", KEY_CHANGED, n->name);
1320 int key_changed_h(connection_t *c)
1322 char name[MAX_STRING_SIZE];
1325 if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
1327 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
1328 c->name, c->hostname);
1332 n = lookup_node(name);
1336 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
1337 c->name, c->hostname, name);
1341 n->status.validkey = 0;
1342 n->status.waitingforkey = 0;
1344 send_key_changed(c, n);
1349 int send_req_key(connection_t *c, node_t *from, node_t *to)
1352 return send_request(c, "%d %s %s", REQ_KEY,
1353 from->name, to->name);
1356 int req_key_h(connection_t *c)
1358 char from_name[MAX_STRING_SIZE];
1359 char to_name[MAX_STRING_SIZE];
1361 char key[MAX_STRING_SIZE];
1363 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
1365 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
1366 c->name, c->hostname);
1370 from = lookup_node(from_name);
1374 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
1375 c->name, c->hostname, from_name);
1379 to = lookup_node(to_name);
1383 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
1384 c->name, c->hostname, to_name);
1388 /* Check if this key request is for us */
1390 if(to == myself) /* Yes, send our own key back */
1392 bin2hex(myself->key, key, myself->keylength);
1393 key[myself->keylength * 2] = '\0';
1394 send_ans_key(c, myself, from, key);
1399 if(to->status.validkey) /* Proxy keys */
1401 bin2hex(to->key, key, to->keylength);
1402 key[to->keylength * 2] = '\0';
1403 send_ans_key(c, to, from, key);
1406 send_req_key(to->nexthop->connection, from, to);
1413 int send_ans_key(connection_t *c, node_t *from, node_t *to, char *key)
1416 return send_request(c, "%d %s %s %s", ANS_KEY,
1417 from->name, to->name, key);
1420 int ans_key_h(connection_t *c)
1422 char from_name[MAX_STRING_SIZE];
1423 char to_name[MAX_STRING_SIZE];
1424 char key[MAX_STRING_SIZE];
1428 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING, from_name, to_name, key) != 3)
1430 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
1431 c->name, c->hostname);
1435 from = lookup_node(from_name);
1439 syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
1440 c->name, c->hostname, from_name);
1444 to = lookup_node(to_name);
1448 syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
1449 c->name, c->hostname, to_name);
1453 /* Check correctness of packet key */
1455 keylength = strlen(key);
1457 if(keylength != from->keylength * 2)
1459 syslog(LOG_ERR, _("Got bad %s from %s (%s) origin %s: %s"), "ANS_KEY",
1460 c->name, c->hostname, from->name, _("invalid key length"));
1464 /* Forward it if necessary */
1468 send_ans_key(to->nexthop->connection, from, to, key);
1471 /* Update our copy of the origin's packet key */
1476 from->key = xstrdup(key);
1478 hex2bin(from->key, from->key, keylength);
1479 from->key[keylength] = '\0';
1481 from->status.validkey = 1;
1482 from->status.waitingforkey = 0;
1489 int send_tcppacket(connection_t *c, vpn_packet_t *packet)
1495 x = send_request(c, "%d %hd", PACKET, packet->len);
1500 return send_meta(c, packet->data, packet->len);
1503 int tcppacket_h(connection_t *c)
1507 if(sscanf(c->buffer, "%*d %hd", &len) != 1)
1509 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name, c->hostname);
1513 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
1520 /* Jumptable for the request handlers */
1522 int (*request_handlers[])(connection_t*) = {
1523 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
1524 status_h, error_h, termreq_h,
1526 add_node_h, del_node_h,
1527 add_subnet_h, del_subnet_h,
1528 add_edge_h, del_edge_h,
1529 key_changed_h, req_key_h, ans_key_h,
1535 char (*request_name[]) = {
1536 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
1537 "STATUS", "ERROR", "TERMREQ",
1539 "ADD_NODE", "DEL_NODE",
1540 "ADD_SUBNET", "DEL_SUBNET",
1541 "ADD_EDGE", "DEL_EDGE",
1542 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
1546 /* Status strings */
1548 char (*status_text[]) = {
1554 char (*error_text[]) = {