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.26 2000/09/10 16:15:35 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-%d %s", ID, myself->id, myself->min_version, myself->max_version, opt2str(myself->options));
104 int id_h(conn_list_t *cl)
109 if(sscanf(cl->buffer, "%*d %as %d-%d %as", &cl->id, &cl->min_version, &cl->max_version, &options) != 4)
111 syslog(LOG_ERR, _("Got bad ID from %s"), cl->hostname);
115 /* Check if version ranges overlap */
117 if((cl->min_version > myself->max_version) || (cl->max_version < myself_min_version) || (cl->min_version > cl->max_version))
119 syslog(LOG_ERR, _("Peer %s uses incompatible version (%d-%d)"), cl->hostname, cl->min_version, cl->max_version);
123 /* Check if option string is valid */
125 if(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_meta(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_meta(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_meta(cl, "%d %lx:%d", ADD_HOST, other->id, other->address, other->port);
410 int add_host_h(conn_list_t *cl)
414 int send_del_host(conn_list_t *cl, conn_list_t *other)
417 return send_meta(cl, "%d %lx:%d", DEL_HOST, other->id, other->address, other->port);
420 int del_host_h(conn_list_t *cl)
424 /* Status and error notification routines */
426 int send_status(conn_list_t *cl, int statusno, char *statusstring)
430 statusstring = status_text[statusno];
432 return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
435 int status_h(conn_list_t *cl)
440 if(sscanf(cl->buffer, "%*d %d %as", &statusno, &statusstring) != 2)
442 syslog(LOG_ERR, _("Got bad STATUS from %s (%s)"), cl->id, cl->hostname);
446 if(debug_lvl > DEBUG_STATUS)
448 syslog(LOG_NOTICE, _("Status message from %s (%s): %s: %s"), cl->id, cl->hostname, status_text[statusno], statusstring);
456 int send_error(conn_list_t *cl, int errno, char *errstring)
460 errorstring = error_text[errno];
461 return send_request(cl, "%d %d %s", ERROR, errno, errstring);
464 int error_h(conn_list_t *cl)
469 if(sscanf(cl->buffer, "%*d %d %as", &errno, &errorstring) != 2)
471 syslog(LOG_ERR, _("Got bad error from %s (%s)"), cl->id, cl->hostname);
475 if(debug_lvl > DEBUG_error)
477 syslog(LOG_NOTICE, _("Error message from %s (%s): %s: %s"), cl->id, cl->hostname, error_text[errno], errorstring);
485 int send_termreq(conn_list_t *cl)
489 int termreq_h(conn_list_t *cl)
493 /* Keepalive routines */
495 int send_ping(conn_list_t *cl)
498 return send_meta(cl, "%d", PING);
501 int ping_h(conn_list_t *cl)
505 int send_pong(conn_list_t *cl)
508 return send_meta(cl, "%d", PONG);
511 int pong_h(conn_list_t *cl)
517 int send_req_key(conn_list_t *cl, conn_list_t *source)
520 return send_meta(cl, "%d %s", REQ_KEY, source->id);
523 int req_key_h(conn_list_t *cl)
527 int send_key(conn_list_t *cl)
531 int key_h(conn_list_t *cl)
541 int send_termreq(conn_list_t *cl)
545 syslog(LOG_DEBUG, _("Sending TERMREQ to %s (%s)"),
546 cl->vpn_hostname, cl->real_hostname);
548 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", TERMREQ, myself->vpn_ip);
550 if(write(cl->meta_socket, buffer, buflen) < 0)
553 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
560 int send_timeout(conn_list_t *cl)
564 syslog(LOG_DEBUG, _("Sending TIMEOUT to %s (%s)"),
565 cl->vpn_hostname, cl->real_hostname);
567 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", PINGTIMEOUT, myself->vpn_ip);
569 if((write(cl->meta_socket, buffer, buflen)) < 0)
571 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
578 int send_del_host(conn_list_t *cl, conn_list_t *new_host)
582 syslog(LOG_DEBUG, _("Sending DEL_HOST for %s (%s) to %s (%s)"),
583 new_host->vpn_hostname, new_host->real_hostname, cl->vpn_hostname, cl->real_hostname);
585 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", DEL_HOST, new_host->vpn_ip);
587 if((write(cl->meta_socket, buffer, buflen)) < 0)
589 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
596 int send_tcppacket(conn_list_t *cl, void *data, int len)
600 syslog(LOG_DEBUG, _("Sending PACKET to %s (%s)"),
601 cl->vpn_hostname, cl->real_hostname);
603 buflen = snprintf(buffer, MAXBUFSIZE, "%d %d\n", PACKET, len);
605 if((write(cl->meta_socket, buffer, buflen)) < 0)
607 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
611 if((write(cl->meta_socket, data, len)) < 0)
613 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
620 int send_ping(conn_list_t *cl)
624 syslog(LOG_DEBUG, _("Sending PING to %s (%s)"),
625 cl->vpn_hostname, cl->real_hostname);
627 buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", PING);
629 if((write(cl->meta_socket, buffer, buflen)) < 0)
631 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
638 int send_pong(conn_list_t *cl)
642 syslog(LOG_DEBUG, _("Sending PONG to %s (%s)"),
643 cl->vpn_hostname, cl->real_hostname);
645 buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", PONG);
647 if((write(cl->meta_socket, buffer, buflen)) < 0)
649 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
656 int send_add_host(conn_list_t *cl, conn_list_t *new_host)
662 real_ip = new_host->real_ip;
663 hostname = new_host->real_hostname;
664 flags = new_host->flags;
666 /* If we need to propagate information about a new host that wants us to export
667 * it's indirectdata flag, we set the INDIRECTDATA flag and unset the EXPORT...
668 * flag, and set it's real_ip to our vpn_ip, so that net.c send_packet() will
672 if(flags & EXPORTINDIRECTDATA)
674 flags &= ~EXPORTINDIRECTDATA;
675 flags |= INDIRECTDATA;
676 real_ip = myself->vpn_ip;
677 hostname = myself->real_hostname;
681 syslog(LOG_DEBUG, _("Sending ADD_HOST for %s (%s) to %s (%s)"),
682 new_host->vpn_hostname, hostname, cl->vpn_hostname, cl->real_hostname);
684 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx/%lx:%x %d\n", ADD_HOST, real_ip, new_host->vpn_ip, new_host->vpn_mask, new_host->port, flags);
686 if((write(cl->meta_socket, buffer, buflen)) < 0)
688 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
695 int send_key_changed(conn_list_t *cl, conn_list_t *src)
699 syslog(LOG_DEBUG, _("Sending KEY_CHANGED origin %s to %s (%s)"),
700 src->vpn_hostname, cl->vpn_hostname, cl->real_hostname);
702 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", KEY_CHANGED, src->vpn_ip);
704 if((write(cl->meta_socket, buffer, buflen)) < 0)
706 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
713 void send_key_changed_all(void)
717 for(p = conn_list; p != NULL; p = p->next)
718 if(p->status.meta && p->status.active)
719 send_key_changed(p, myself);
724 int send_key_request(ip_t to)
728 fw = lookup_conn(to);
731 syslog(LOG_ERR, _("Attempting to send REQ_KEY to %d.%d.%d.%d, which does not exist?"),
737 syslog(LOG_DEBUG, _("Sending REQ_KEY to %s (%s)"),
738 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
740 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx\n", REQ_KEY, to, myself->vpn_ip);
742 if((write(fw->nexthop->meta_socket, buffer, buflen)) < 0)
744 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
747 fw->status.waitingforkey = 1;
752 int send_key_answer(conn_list_t *cl, ip_t to)
757 fw = lookup_conn(to);
761 syslog(LOG_ERR, _("Attempting to send ANS_KEY to %d.%d.%d.%d, which does not exist?"),
767 syslog(LOG_DEBUG, _("Sending ANS_KEY to %s (%s)"),
768 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
770 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx %d %s\n", ANS_KEY, to, myself->vpn_ip, my_key_expiry, my_public_key_base36);
772 if((write(fw->nexthop->meta_socket, buffer, buflen)) < 0)
774 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
782 notify all my direct connections of a new host
783 that was added to the vpn, with the exception
784 of the source of the announcement.
786 int notify_others(conn_list_t *new, conn_list_t *source,
787 int (*function)(conn_list_t*, conn_list_t*))
791 for(p = conn_list; p != NULL; p = p->next)
792 if(p != new && p != source && p->status.meta && p->status.active)
799 notify one connection of everything
802 int notify_one(conn_list_t *new)
806 for(p = conn_list; p != NULL; p = p->next)
807 if(p != new && p->status.active)
808 send_add_host(new, p);
814 The incoming request handlers
818 int termreq_h(conn_list_t *cl)
821 if(!cl->status.active)
823 syslog(LOG_ERR, _("Got unauthorized TERMREQ from %s (%s)"),
824 cl->vpn_hostname, cl->real_hostname);
829 syslog(LOG_DEBUG, _("Got TERMREQ from %s (%s)"),
830 cl->vpn_hostname, cl->real_hostname);
832 cl->status.termreq = 1;
834 terminate_connection(cl);
839 int timeout_h(conn_list_t *cl)
842 if(!cl->status.active)
844 syslog(LOG_ERR, _("Got unauthorized TIMEOUT from %s (%s)"),
845 cl->vpn_hostname, cl->real_hostname);
850 syslog(LOG_DEBUG, _("Got TIMEOUT from %s (%s)"),
851 cl->vpn_hostname, cl->real_hostname);
853 cl->status.termreq = 1;
854 terminate_connection(cl);
859 int del_host_h(conn_list_t *cl)
864 if(!cl->status.active)
866 syslog(LOG_ERR, _("Got unauthorized DEL_HOST from %s (%s)"),
867 cl->vpn_hostname, cl->real_hostname);
871 if(sscanf(cl->buffer, "%*d %lx", &vpn_ip) != 1)
873 syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
874 cl->vpn_hostname, cl->real_hostname);
878 if(!(fw = lookup_conn(vpn_ip)))
880 syslog(LOG_ERR, _("Got DEL_HOST for %d.%d.%d.%d from %s (%s) which does not exist?"),
881 IP_ADDR_V(vpn_ip), cl->vpn_hostname, cl->real_hostname);
885 /* Connections lists are really messed up if this happens */
886 if(vpn_ip == myself->vpn_ip)
888 syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"),
889 cl->vpn_hostname, cl->real_hostname);
895 syslog(LOG_DEBUG, _("Got DEL_HOST for %s (%s) from %s (%s)"),
896 fw->vpn_hostname, fw->real_hostname, cl->vpn_hostname, cl->real_hostname);
898 notify_others(fw, cl, send_del_host);
900 fw->status.termreq = 1;
901 fw->status.active = 0;
903 terminate_connection(fw);
908 int tcppacket_h(conn_list_t *cl)
912 if(!cl->status.active)
914 syslog(LOG_ERR, _("Got unauthorized PACKET from %s (%s)"),
915 cl->vpn_hostname, cl->real_hostname);
919 if(sscanf(cl->buffer, "%*d %d", &len) != 1)
921 syslog(LOG_ERR, _("Got bad PACKET from %s (%s)"),
922 cl->vpn_hostname, cl->real_hostname);
928 syslog(LOG_ERR, _("Got too big PACKET from %s (%s)"),
929 cl->vpn_hostname, cl->real_hostname);
934 syslog(LOG_DEBUG, _("Got PACKET length %d from %s (%s)"), len,
935 cl->vpn_hostname, cl->real_hostname);
943 int ping_h(conn_list_t *cl)
946 if(!cl->status.active)
948 syslog(LOG_ERR, _("Got unauthorized PING from %s (%s)"),
949 cl->vpn_hostname, cl->real_hostname);
954 syslog(LOG_DEBUG, _("Got PING from %s (%s)"),
955 cl->vpn_hostname, cl->real_hostname);
957 cl->status.pinged = 0;
958 cl->status.got_pong = 1;
965 int pong_h(conn_list_t *cl)
968 if(!cl->status.active)
970 syslog(LOG_ERR, _("Got unauthorized PONG from %s (%s)"),
971 cl->vpn_hostname, cl->real_hostname);
976 syslog(LOG_DEBUG, _("Got PONG from %s (%s)"),
977 cl->vpn_hostname, cl->real_hostname);
979 cl->status.got_pong = 1;
984 int add_host_h(conn_list_t *cl)
991 conn_list_t *ncn, *old;
993 if(!cl->status.active)
995 syslog(LOG_ERR, _("Got unauthorized ADD_HOST from %s (%s)"),
996 cl->vpn_hostname, cl->real_hostname);
1000 if(sscanf(cl->buffer, "%*d %lx %lx/%lx:%hx %d", &real_ip, &vpn_ip, &vpn_mask, &port, &flags) != 5)
1002 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"),
1003 cl->vpn_hostname, cl->real_hostname);
1007 if((old = lookup_conn(vpn_ip)))
1009 if((real_ip==old->real_ip) && (vpn_mask==old->vpn_mask) && (port==old->port))
1012 syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"),
1013 old->vpn_hostname, old->real_hostname, cl->vpn_hostname, cl->real_hostname);
1014 goto skip_add_host; /* One goto a day keeps the deeply nested if constructions away. */
1019 syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"),
1020 old->vpn_hostname, old->real_hostname);
1021 old->status.active = 0;
1022 terminate_connection(old);
1026 /* Connections lists are really messed up if this happens */
1027 if(vpn_ip == myself->vpn_ip)
1029 syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"),
1030 cl->vpn_hostname, cl->real_hostname);
1035 ncn = new_conn_list();
1036 ncn->real_ip = real_ip;
1037 ncn->real_hostname = hostlookup(htonl(real_ip));
1038 ncn->vpn_ip = vpn_ip;
1039 ncn->vpn_mask = vpn_mask;
1040 ncn->vpn_hostname = hostlookup(htonl(vpn_ip));
1044 ncn->next = conn_list;
1046 ncn->status.active = 1;
1049 syslog(LOG_DEBUG, _("Got ADD_HOST for %s (%s) from %s (%s)"),
1050 ncn->vpn_hostname, ncn->real_hostname, cl->vpn_hostname, cl->real_hostname);
1052 notify_others(ncn, cl, send_add_host);
1059 int req_key_h(conn_list_t *cl)
1065 if(!cl->status.active)
1067 syslog(LOG_ERR, _("Got unauthorized REQ_KEY from %s (%s)"),
1068 cl->vpn_hostname, cl->real_hostname);
1072 if(sscanf(cl->buffer, "%*d %lx %lx", &to, &from) != 2)
1074 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"),
1075 cl->vpn_hostname, cl->real_hostname);
1080 syslog(LOG_DEBUG, _("Got REQ_KEY origin %d.%d.%d.%d destination %d.%d.%d.%d from %s (%s)"),
1081 IP_ADDR_V(from), IP_ADDR_V(to), cl->vpn_hostname, cl->real_hostname);
1083 if((to & myself->vpn_mask) == (myself->vpn_ip & myself->vpn_mask))
1084 { /* hey! they want something from ME! :) */
1085 send_key_answer(cl, from);
1089 fw = lookup_conn(to);
1093 syslog(LOG_ERR, _("Attempting to forward REQ_KEY to %d.%d.%d.%d, which does not exist?"),
1099 syslog(LOG_DEBUG, _("Forwarding REQ_KEY to %s (%s)"),
1100 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
1102 cl->buffer[cl->reqlen-1] = '\n';
1104 if(write(fw->nexthop->meta_socket, cl->buffer, cl->reqlen) < 0)
1106 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
1113 void set_keys(conn_list_t *cl, int expiry, char *key)
1119 cl->public_key = xmalloc(sizeof(*cl->key));
1120 cl->public_key->key = NULL;
1123 if(cl->public_key->key)
1124 free(cl->public_key->key);
1125 cl->public_key->length = strlen(key);
1126 cl->public_key->expiry = expiry;
1127 cl->public_key->key = xmalloc(cl->public_key->length + 1);
1128 strcpy(cl->public_key->key, key);
1130 ek = make_shared_key(key);
1134 cl->key = xmalloc(sizeof(*cl->key));
1135 cl->key->key = NULL;
1141 cl->key->length = strlen(ek);
1142 cl->key->expiry = expiry;
1143 cl->key->key = xmalloc(cl->key->length + 1);
1144 strcpy(cl->key->key, ek);
1148 int ans_key_h(conn_list_t *cl)
1154 conn_list_t *fw, *gk;
1156 if(!cl->status.active)
1158 syslog(LOG_ERR, _("Got unauthorized ANS_KEY from %s (%s)"),
1159 cl->vpn_hostname, cl->real_hostname);
1163 if(sscanf(cl->buffer, "%*d %lx %lx %d %as", &to, &from, &expiry, &key) != 4)
1165 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"),
1166 cl->vpn_hostname, cl->real_hostname);
1171 syslog(LOG_DEBUG, _("Got ANS_KEY origin %d.%d.%d.%d destination %d.%d.%d.%d from %s (%s)"),
1172 IP_ADDR_V(from), IP_ADDR_V(to), cl->vpn_hostname, cl->real_hostname);
1174 if(to == myself->vpn_ip)
1175 { /* hey! that key's for ME! :) */
1176 gk = lookup_conn(from);
1180 syslog(LOG_ERR, _("Receiving ANS_KEY origin %d.%d.%d.%d from %s (%s), which does not exist?"),
1181 IP_ADDR_V(from), cl->vpn_hostname, cl->real_hostname);
1185 set_keys(gk, expiry, key);
1186 gk->status.validkey = 1;
1187 gk->status.waitingforkey = 0;
1192 fw = lookup_conn(to);
1196 syslog(LOG_ERR, _("Attempting to forward ANS_KEY to %d.%d.%d.%d, which does not exist?"),
1202 syslog(LOG_DEBUG, _("Forwarding ANS_KEY to %s (%s)"),
1203 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
1205 cl->buffer[cl->reqlen-1] = '\n';
1207 if((write(fw->nexthop->meta_socket, cl->buffer, cl->reqlen)) < 0)
1209 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
1216 int key_changed_h(conn_list_t *cl)
1221 if(!cl->status.active)
1223 syslog(LOG_ERR, _("Got unauthorized KEY_CHANGED from %s (%s)"),
1224 cl->vpn_hostname, cl->real_hostname);
1228 if(sscanf(cl->buffer, "%*d %lx", &from) != 1)
1230 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
1231 cl->vpn_hostname, cl->real_hostname);
1235 ik = lookup_conn(from);
1239 syslog(LOG_ERR, _("Got KEY_CHANGED origin %d.%d.%d.%d from %s (%s), which does not exist?"),
1240 IP_ADDR_V(from), cl->vpn_hostname, cl->real_hostname);
1245 syslog(LOG_DEBUG, _("Got KEY_CHANGED origin %s from %s (%s)"),
1246 ik->vpn_hostname, cl->vpn_hostname, cl->real_hostname);
1248 ik->status.validkey = 0;
1249 ik->status.waitingforkey = 0;
1251 notify_others(ik, cl, send_key_changed);
1256 /* "Complete overhaul". */
1258 int (*request_handlers[])(conn_list_t*) = {
1259 id_h, challenge_h, chal_reply_h, ack_h,
1260 status_h, error_h, termreq_h,
1261 add_host_h, del_host_h,
1265 char (*request_name[]) = {
1266 "ID", "CHALLENGE", "CHAL_REPLY", "ACK",
1267 "STATUS", "ERROR", "TERMREQ",
1268 "ADD_HOST", "DEL_HOST",