2 protocol.c -- handle the meta-protocol
3 Copyright (C) 1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000 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.28 2000/09/10 23:11:37 guus Exp $
25 #include <sys/types.h>
30 #include <sys/socket.h>
37 #include <netinet/in.h>
47 /* Generic outgoing request routine - takes care of logging and error detection as well */
49 int send_request(conn_list_t *cl, const char *format, int request, /*args*/ ...)
54 if(debug_lvl >= DEBUG_PROTOCOL)
55 syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), requestname[request], cl->id, cl->hostname);
57 va_start(args, format);
58 len = vasprintf(&buffer, format, args);
61 if(len < 0 || !buffer)
63 syslog(LOG_ERR, _("Error during vasprintf(): %m"));
67 if(debug_lvl >= DEBUG_META)
68 syslog(LOG_DEBUG, _("Sending meta data to %s (%s): %s"), cl->id, cl->hostname, buffer);
70 if(cl->status.encryptout)
72 /* FIXME: Do encryption */
75 if((write(cl->meta_socket, buffer, buflen)) < 0)
77 syslog(LOG_ERR, _("Sending meta data failed: %m"));
83 /* Connection protocol:
98 int send_id(conn_list_t *cl)
101 return send_request(cl, "%d %s %d %s", ID, myself->id, myself->version, opt2str(myself->options));
104 int id_h(conn_list_t *cl)
109 if(sscanf(cl->buffer, "%*d %as %d %as", &cl->id, &cl->version, &options) != 3)
111 syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
115 /* Check if version matches */
117 if(cl->version != myself->version)
119 syslog(LOG_ERR, _("Peer %s uses incompatible version %d"), cl->hostname, cl->min_version, cl->max_version);
123 /* Check if option string is valid */
125 if((cl->options = str2opt(options)) == -1)
127 syslog(LOG_ERR, _("Peer %s uses invalid option string"), cl->hostname);
131 /* Check if identity is a valid name */
133 if(!check_id(cl->id))
135 syslog(LOG_ERR, _("Peer %s uses invalid identity name"), cl->hostname);
139 /* Load information about peer */
143 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), cl->hostname, cl->id);
148 /* First check if the host we connected to is already in our
149 connection list. If so, we are probably making a loop, which
153 if(cl->status.outgoing)
155 if((old=lookup_id(cl->id)))
157 if(debug_lvl > DEBUG_CONNECTIONS)
158 syslog(LOG_NOTICE, _("Uplink %s (%s) is already in our connection list"), cl->id, cl->hostname);
159 cl->status.outgoing = 0;
160 old->status.outgoing = 1;
161 terminate_connection(cl);
166 /* Since we know the identity now, we can encrypt the meta channel */
168 cl->status.encryptout = 1;
170 /* Send a challenge to verify the identity */
172 cl->allow_request = CHAL_REPLY;
174 return send_challenge(cl);
177 int send_challenge(conn_list_t *cl)
186 /* Allocate buffers for the challenge and the hash */
188 cl->chal_hash = xmalloc(SHA_DIGEST_LEN);
189 keylength = BN_num_bytes(cl->metakey.n);
190 buffer = xmalloc(keylength*2);
192 /* Copy random data and the public key to the buffer */
194 RAND_bytes(buffer, keylength);
195 BN_bn2bin(cl->metakey.n, buffer+keylength);
197 /* Calculate the hash from that */
199 SHA1(buffer, keylength*2, cl->chal_hash);
201 /* Convert the random data to a hexadecimal formatted string */
203 bin2hex(buffer,buffer,keylength);
204 buffer[keylength*2] = '\0';
206 /* Send the challenge */
208 cl->allow_request = CHAL_REPLY;
209 x = send_request(cl, "%d %s", CHALLENGE, buffer);
215 int challenge_h(conn_list_t *cl)
219 if(sscanf(cl->buffer, "%*d %as", &cl->id, &challenge) != 1)
221 syslog(LOG_ERR, _("Got bad CHALLENGE from %s (%s)"), cl->id, cl->hostname);
225 /* Rest is done by send_chal_reply() */
227 x = send_chal_reply(cl, challenge);
233 int send_chal_reply(conn_list_t *cl, char *challenge)
240 keylength = BN_num_bytes(myself->meyakey.n);
242 /* Check if the length of the challenge is all right */
244 if(strlen(challenge) != keylength*2)
246 syslog(LOG_ERROR, _("Intruder: wrong challenge length from %s (%s)"), cl->id, cl->hostname);
250 /* Allocate buffers for the challenge and the hash */
252 buffer = xmalloc(keylength*2);
253 hash = xmalloc(SHA_DIGEST_LEN*2+1);
255 /* Copy the incoming random data and our public key to the buffer */
257 hex2bin(challenge, buffer, keylength);
258 BN_bn2bin(myself->metakey.n, buffer+keylength);
260 /* Calculate the hash from that */
262 SHA1(buffer, keylength*2, hash);
265 /* Convert the hash to a hexadecimal formatted string */
267 bin2hex(hash,hash,SHA_DIGEST_LEN);
268 hash[SHA_DIGEST_LEN*2] = '\0';
272 if(cl->status.outgoing)
273 cl->allow_resuest = ID;
275 cl->allow_request = ACK;
277 x = send_request(cl, "%d %s", CHAL_REPLY, hash);
283 int chal_reply_h(conn_list_t *cl)
287 if(sscanf(cl->buffer, "%*d %as", &cl->id, &hash) != 2)
289 syslog(LOG_ERR, _("Got bad CHAL_REPLY from %s (%s)"), cl->id, cl->hostname);
293 /* Check if the length of the hash is all right */
295 if(strlen(hash) != SHA_DIGEST_LEN*2)
297 syslog(LOG_ERROR, _("Intruder: wrong challenge reply length from %s (%s)"), cl->id, cl->hostname);
301 /* Convert the hash to binary format */
303 hex2bin(hash, hash, SHA_DIGEST_LEN);
305 /* Verify the incoming hash with the calculated hash */
307 if{!memcmp(hash, cl->chal_hash, SHA_DIGEST_LEN)}
309 syslog(LOG_ERROR, _("Intruder: wrong challenge reply from %s (%s)"), cl->id, cl->hostname);
313 /* Identity has now been positively verified.
314 If we are accepting this new connection, then send our identity,
315 if we are making this connecting, acknowledge.
322 if(cl->status.outgoing)
324 cl->allow_request = ACK;
329 cl->allow_request = CHALLENGE;
334 int send_ack(conn_list_t *cl)
337 return send_request(cl, "%d", ACK);
340 int ack_h(conn_list_t *cl)
344 /* Okay, before we active the connection, we check if there is another entry
345 in the connection list with the same vpn_ip. If so, it presumably is an
346 old connection that has timed out but we don't know it yet.
349 while((old = lookup_id(cl->id)))
351 if(debug_lvl > DEBUG_CONNECTIONS)
352 syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
353 cl->id, old->hostname, cl->hostname);
354 old->status.active = 0;
355 terminate_connection(old);
358 /* Activate this connection */
360 cl->allow_request = ALL;
361 cl->status.active = 1;
363 if(debug_lvl > DEBUG_CONNECTIONS)
364 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->id, cl->hostname);
366 /* Exchange information about other tinc daemons */
368 notify_others(cl, NULL, send_add_host);
374 if(cl->status.outgoing)
380 /* Address and subnet information exchange */
382 int send_add_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
385 return send_request(cl, "%d %s %d %s", ADD_SUBNET, other->id, subnet->type, net2str(subnet));
388 int add_subnet_h(conn_list_t *cl)
392 int send_del_subnet(conn_list_t *cl, conn_list_t *other, subnet_t *subnet)
395 return send_request(cl, "%d %s %d %s", DEL_SUBNET, other->id, subnet->type, net2str(subnet));
398 int del_subnet_h(conn_list_t *cl)
402 /* New and closed connections notification */
404 int send_add_host(conn_list_t *cl, conn_list_t *other)
407 return send_request(cl, "%d %s %lx:%d %s", ADD_HOST, other->id, other->address, other->port, opt2str(other->options));
410 int add_host_h(conn_list_t *cl)
413 conn_list_t *old, *new;
415 new = new_conn_list();
417 if(sscanf(cl->buffer, "%*d %as %lx:%d %as", &new->id, &new->address, &new->port, &options) != 4)
419 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"), cl->id, cl->hostname);
423 /* Check if option string is valid */
425 if((new->options = str2opt(options) == -1)
427 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid option string"), cl->hostname);
431 /* Check if identity is a valid name */
433 if(!check_id(new->id))
435 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s): invalid identity name"), cl->id, cl->hostname);
439 /* Check if somebody tries to add ourself */
441 if(!strcmp(new->id, myself->id))
443 syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"), cl->id, cl->hostname);
448 /* Fill in more of the new conn_list structure */
450 new->hostname = hostlookup(htonl(new->address));
452 /* Check if the new host already exists in the connnection list */
454 if((old = lookup_id(id))
456 if((new->address == old->address) && (new->port == old->port))
458 if(debug_lvl > DEBUG_CONNECTIONS)
459 syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"), old->id, old->hostname, new->id, new->hostname);
464 if(debug_lvl > DEBUG_CONNECTIONS)
465 syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"), old->id, old->hostname);
466 old->status.active = 0;
467 terminate_connection(old);
471 /* Fill in rest of conn_list structure */
474 new->status.active = 1;
476 /* Hook it up into the conn_list */
478 conn_list_add(conn_list, new);
480 /* Tell the rest about the new host */
482 notify_others(new, cl, send_add_host);
488 int send_del_host(conn_list_t *cl, conn_list_t *other)
491 return send_request(cl, "%d %s %lx:%d", DEL_HOST, other->id, other->address, other->port);
494 int del_host_h(conn_list_t *cl)
501 if(sscanf(cl->buffer, "%*d %as %lx:%d", &id, &address, &port) != 3)
503 syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"), cl->id, cl->hostname);
507 /* Check if somebody tries to delete ourself */
509 if(!strcmp(id, myself->id))
511 syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"), cl->id, cl->hostname);
516 /* Check if the new host already exists in the connnection list */
518 if((old = lookup_id(id))
520 if((address == old->address) && (port == old->port))
522 notify_others(old, cl, send_del_host);
524 fw->status.termreq = 1;
525 fw->status.active = 0;
527 terminate_connection(fw);
533 if(debug_lvl > DEBUG_CONNECTIONS)
535 syslog(LOG_NOTICE, _("Got DEL_HOST for %s from %s (%s) which is not in our connection list"), id, cl->id, cl->hostname);
541 /* Status and error notification routines */
543 int send_status(conn_list_t *cl, int statusno, char *statusstring)
547 statusstring = status_text[statusno];
549 return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
552 int status_h(conn_list_t *cl)
557 if(sscanf(cl->buffer, "%*d %d %as", &statusno, &statusstring) != 2)
559 syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"), cl->id, cl->hostname);
563 if(debug_lvl > DEBUG_STATUS)
565 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"), cl->id, cl->hostname, status_text[statusno], statusstring);
573 int send_error(conn_list_t *cl, int errno, char *errstring)
577 errorstring = error_text[errno];
578 return send_request(cl, "%d %d %s", ERROR, errno, errstring);
581 int error_h(conn_list_t *cl)
586 if(sscanf(cl->buffer, "%*d %d %as", &errno, &errorstring) != 2)
588 syslog(LOG_ERR, _("Got bad error from %s (%s)"), cl->id, cl->hostname);
592 if(debug_lvl > DEBUG_error)
594 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"), cl->id, cl->hostname, error_text[errno], errorstring);
598 cl->status.termreq = 1;
599 terminate_connection(cl);
604 int send_termreq(conn_list_t *cl)
607 return send_request(cl, "%d", TERMREQ);
610 int termreq_h(conn_list_t *cl)
613 cl->status.termreq = 1;
614 terminate_connection(cl);
619 /* Keepalive routines - FIXME: needs a closer look */
621 int send_ping(conn_list_t *cl)
623 cl->status.pinged = 1;
625 return send_request(cl, "%d", PING);
628 int ping_h(conn_list_t *cl)
631 return send_pong(cl);
634 int send_pong(conn_list_t *cl)
637 return send_request(cl, "%d", PONG);
640 int pong_h(conn_list_t *cl)
643 cl->status.got_pong = 1;
650 int send_key_changed(conn_list_t *from, conn_list_t *cl)
654 for(p = conn_list; p != NULL; p = p->next)
656 if(p!=cl && p->status.meta && p->status.active)
657 send_request(p, "%d %s", KEY_CHANGED, from->id);
663 int key_changed_h(conn_list_t *cl)
668 if(sscanf(cl->buffer, "%*d %as", &from_id) != 1)
670 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"), cl->id, cl->hostname);
674 if(!(from = lookup_id(from_id)))
676 syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
683 from->status.validkey = 0;
684 from->status.waitingforkey = 0;
686 send_key_changed(from, cl);
691 int send_req_key(conn_list_t *from, conn_list_t *to)
694 return send_request(to->nexthop, "%d %s %s", REQ_KEY, from->id, to->id);
697 int req_key_h(conn_list_t *cl)
699 char *from_id, *to_id;
700 conn_list_t *from, *to;
702 if(sscanf(cl->buffer, "%*d %as %as", &from_id, &to_id) != 2)
704 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"), cl->id, cl->hostname);
708 if(!(from = lookup_id(from_id)))
710 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
711 free(from_id); free(to_id);
715 /* Check if this key request is for us */
717 if(!strcmp(id, myself->strcmp))
719 send_ans_key(myself, from, myself->datakey);
723 if(!(to = lookup_id(to_id)))
725 syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"), cl->id, cl->hostname, to_id);
726 free(from_id); free(to_id);
729 send_req_key(from, to);
732 free(from_id); free(to_id);
737 int send_ans_key(conn_list_t *from, conn_list_t *to, char *datakey)
740 return send_request(to->nexthop, "%d %s %s %s", ANS_KEY, from->id, to->id, datakey);
743 int ans_key_h(conn_list_t *cl)
745 char *from_id, *to_id, *datakey;
747 conn_list_t *from, *to;
749 if(sscanf(cl->buffer, "%*d %as %as %as", &from_id, &to_id, &datakey) != 3)
751 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"), cl->id, cl->hostname);
755 if(!(from = lookup_id(from_id)))
757 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
758 free(from_id); free(to_id); free(datakey);
762 /* Check if this key request is for us */
764 if(!strcmp(id, myself->strcmp))
766 /* It is for us, convert it to binary and set the key with it. */
768 keylength = strlen(datakey);
770 if((keylength%1) || (keylength <= 0))
772 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"), cl->id, cl->hostname, from->id);
773 free(from_id); free(to_id); free(datakey);
777 hex2bin(datakey, datakey, keylength);
778 BF_set_key(cl->datakey, keylength, datakey);
782 if(!(to = lookup_id(to_id)))
784 syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"), cl->id, cl->hostname, to_id);
785 free(from_id); free(to_id); free(datakey);
788 send_ans_key(from, to, datakey);
791 free(from_id); free(to_id); free(datakey);
799 Notify all my direct connections of a new host
800 that was added to the vpn, with the exception
801 of the source of the announcement.
804 int notify_others(conn_list_t *new, conn_list_t *source,
805 int (*function)(conn_list_t*, conn_list_t*))
809 for(p = conn_list; p != NULL; p = p->next)
810 if(p != new && p != source && p->status.meta && p->status.active)
817 Notify one connection of everything
821 int notify_one(conn_list_t *new)
825 for(p = conn_list; p != NULL; p = p->next)
826 if(p != new && p->status.active)
827 send_add_host(new, p);
832 /* "Complete overhaul". */
834 int (*request_handlers[])(conn_list_t*) = {
835 id_h, challenge_h, chal_reply_h, ack_h,
836 status_h, error_h, termreq_h,
838 add_host_h, del_host_h,
839 add_subnet_h, del_subnet_h,
840 key_changed_h, req_key_h, ans_key_h,
843 char (*request_name[]) = {
844 "ID", "CHALLENGE", "CHAL_REPLY", "ACK",
845 "STATUS", "ERROR", "TERMREQ",
847 "ADD_HOST", "DEL_HOST",
848 "ADD_SUBNET", "DEL_SUBNET",
849 "KEY_CHANGED", "REQ_KEY", "ANS_KEY",