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.25 2000/09/10 15:18:03 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)
343 /* Okay, before we active the connection, we check if there is another entry
344 in the connection list with the same vpn_ip. If so, it presumably is an
345 old connection that has timed out but we don't know it yet.
348 while((old = lookup_conn(cl->vpn_ip)))
351 syslog(LOG_NOTICE, _("Removing old entry for %s at %s in favour of new connection from %s"),
352 cl->vpn_hostname, old->real_hostname, cl->real_hostname);
353 old->status.active = 0;
354 terminate_connection(old);
357 /* Activate this connection */
359 cl->allow_request = ALL;
360 cl->status.active = 1;
362 if(debug_lvl > DEBUG_CONNECTIONS)
363 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->id, cl->hostname);
365 /* Exchange information about other tinc daemons */
367 notify_others(cl, NULL, send_add_host);
373 if(cl->status.outgoing)
379 /* Address and subnet information exchange */
381 /* New and closed connections notification */
383 /* Status and error notification routines */
385 int send_status(conn_list_t *cl, int statusno, char *statusstring)
388 return send_request(cl, "%d %d %s", STATUS, statusno, statusstring);
391 int send_error(conn_list_t *cl, int errno, char *errstring)
394 return send_request(cl, "%d %d %s", ERROR, errno, errstring);
400 int send_termreq(conn_list_t *cl)
404 syslog(LOG_DEBUG, _("Sending TERMREQ to %s (%s)"),
405 cl->vpn_hostname, cl->real_hostname);
407 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", TERMREQ, myself->vpn_ip);
409 if(write(cl->meta_socket, buffer, buflen) < 0)
412 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
419 int send_timeout(conn_list_t *cl)
423 syslog(LOG_DEBUG, _("Sending TIMEOUT to %s (%s)"),
424 cl->vpn_hostname, cl->real_hostname);
426 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", PINGTIMEOUT, myself->vpn_ip);
428 if((write(cl->meta_socket, buffer, buflen)) < 0)
430 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
437 int send_del_host(conn_list_t *cl, conn_list_t *new_host)
441 syslog(LOG_DEBUG, _("Sending DEL_HOST for %s (%s) to %s (%s)"),
442 new_host->vpn_hostname, new_host->real_hostname, cl->vpn_hostname, cl->real_hostname);
444 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", DEL_HOST, new_host->vpn_ip);
446 if((write(cl->meta_socket, buffer, buflen)) < 0)
448 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
455 int send_tcppacket(conn_list_t *cl, void *data, int len)
459 syslog(LOG_DEBUG, _("Sending PACKET to %s (%s)"),
460 cl->vpn_hostname, cl->real_hostname);
462 buflen = snprintf(buffer, MAXBUFSIZE, "%d %d\n", PACKET, len);
464 if((write(cl->meta_socket, buffer, buflen)) < 0)
466 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
470 if((write(cl->meta_socket, data, len)) < 0)
472 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
479 int send_ping(conn_list_t *cl)
483 syslog(LOG_DEBUG, _("Sending PING to %s (%s)"),
484 cl->vpn_hostname, cl->real_hostname);
486 buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", PING);
488 if((write(cl->meta_socket, buffer, buflen)) < 0)
490 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
497 int send_pong(conn_list_t *cl)
501 syslog(LOG_DEBUG, _("Sending PONG to %s (%s)"),
502 cl->vpn_hostname, cl->real_hostname);
504 buflen = snprintf(buffer, MAXBUFSIZE, "%d\n", PONG);
506 if((write(cl->meta_socket, buffer, buflen)) < 0)
508 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
515 int send_add_host(conn_list_t *cl, conn_list_t *new_host)
521 real_ip = new_host->real_ip;
522 hostname = new_host->real_hostname;
523 flags = new_host->flags;
525 /* If we need to propagate information about a new host that wants us to export
526 * it's indirectdata flag, we set the INDIRECTDATA flag and unset the EXPORT...
527 * flag, and set it's real_ip to our vpn_ip, so that net.c send_packet() will
531 if(flags & EXPORTINDIRECTDATA)
533 flags &= ~EXPORTINDIRECTDATA;
534 flags |= INDIRECTDATA;
535 real_ip = myself->vpn_ip;
536 hostname = myself->real_hostname;
540 syslog(LOG_DEBUG, _("Sending ADD_HOST for %s (%s) to %s (%s)"),
541 new_host->vpn_hostname, hostname, cl->vpn_hostname, cl->real_hostname);
543 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);
545 if((write(cl->meta_socket, buffer, buflen)) < 0)
547 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
554 int send_key_changed(conn_list_t *cl, conn_list_t *src)
558 syslog(LOG_DEBUG, _("Sending KEY_CHANGED origin %s to %s (%s)"),
559 src->vpn_hostname, cl->vpn_hostname, cl->real_hostname);
561 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx\n", KEY_CHANGED, src->vpn_ip);
563 if((write(cl->meta_socket, buffer, buflen)) < 0)
565 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
572 void send_key_changed_all(void)
576 for(p = conn_list; p != NULL; p = p->next)
577 if(p->status.meta && p->status.active)
578 send_key_changed(p, myself);
583 int send_key_request(ip_t to)
587 fw = lookup_conn(to);
590 syslog(LOG_ERR, _("Attempting to send REQ_KEY to %d.%d.%d.%d, which does not exist?"),
596 syslog(LOG_DEBUG, _("Sending REQ_KEY to %s (%s)"),
597 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
599 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx\n", REQ_KEY, to, myself->vpn_ip);
601 if((write(fw->nexthop->meta_socket, buffer, buflen)) < 0)
603 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
606 fw->status.waitingforkey = 1;
611 int send_key_answer(conn_list_t *cl, ip_t to)
616 fw = lookup_conn(to);
620 syslog(LOG_ERR, _("Attempting to send ANS_KEY to %d.%d.%d.%d, which does not exist?"),
626 syslog(LOG_DEBUG, _("Sending ANS_KEY to %s (%s)"),
627 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
629 buflen = snprintf(buffer, MAXBUFSIZE, "%d %lx %lx %d %s\n", ANS_KEY, to, myself->vpn_ip, my_key_expiry, my_public_key_base36);
631 if((write(fw->nexthop->meta_socket, buffer, buflen)) < 0)
633 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
641 notify all my direct connections of a new host
642 that was added to the vpn, with the exception
643 of the source of the announcement.
645 int notify_others(conn_list_t *new, conn_list_t *source,
646 int (*function)(conn_list_t*, conn_list_t*))
650 for(p = conn_list; p != NULL; p = p->next)
651 if(p != new && p != source && p->status.meta && p->status.active)
658 notify one connection of everything
661 int notify_one(conn_list_t *new)
665 for(p = conn_list; p != NULL; p = p->next)
666 if(p != new && p->status.active)
667 send_add_host(new, p);
673 The incoming request handlers
677 int termreq_h(conn_list_t *cl)
680 if(!cl->status.active)
682 syslog(LOG_ERR, _("Got unauthorized TERMREQ from %s (%s)"),
683 cl->vpn_hostname, cl->real_hostname);
688 syslog(LOG_DEBUG, _("Got TERMREQ from %s (%s)"),
689 cl->vpn_hostname, cl->real_hostname);
691 cl->status.termreq = 1;
693 terminate_connection(cl);
698 int timeout_h(conn_list_t *cl)
701 if(!cl->status.active)
703 syslog(LOG_ERR, _("Got unauthorized TIMEOUT from %s (%s)"),
704 cl->vpn_hostname, cl->real_hostname);
709 syslog(LOG_DEBUG, _("Got TIMEOUT from %s (%s)"),
710 cl->vpn_hostname, cl->real_hostname);
712 cl->status.termreq = 1;
713 terminate_connection(cl);
718 int del_host_h(conn_list_t *cl)
723 if(!cl->status.active)
725 syslog(LOG_ERR, _("Got unauthorized DEL_HOST from %s (%s)"),
726 cl->vpn_hostname, cl->real_hostname);
730 if(sscanf(cl->buffer, "%*d %lx", &vpn_ip) != 1)
732 syslog(LOG_ERR, _("Got bad DEL_HOST from %s (%s)"),
733 cl->vpn_hostname, cl->real_hostname);
737 if(!(fw = lookup_conn(vpn_ip)))
739 syslog(LOG_ERR, _("Got DEL_HOST for %d.%d.%d.%d from %s (%s) which does not exist?"),
740 IP_ADDR_V(vpn_ip), cl->vpn_hostname, cl->real_hostname);
744 /* Connections lists are really messed up if this happens */
745 if(vpn_ip == myself->vpn_ip)
747 syslog(LOG_ERR, _("Warning: got DEL_HOST from %s (%s) for ourself, restarting"),
748 cl->vpn_hostname, cl->real_hostname);
754 syslog(LOG_DEBUG, _("Got DEL_HOST for %s (%s) from %s (%s)"),
755 fw->vpn_hostname, fw->real_hostname, cl->vpn_hostname, cl->real_hostname);
757 notify_others(fw, cl, send_del_host);
759 fw->status.termreq = 1;
760 fw->status.active = 0;
762 terminate_connection(fw);
767 int tcppacket_h(conn_list_t *cl)
771 if(!cl->status.active)
773 syslog(LOG_ERR, _("Got unauthorized PACKET from %s (%s)"),
774 cl->vpn_hostname, cl->real_hostname);
778 if(sscanf(cl->buffer, "%*d %d", &len) != 1)
780 syslog(LOG_ERR, _("Got bad PACKET from %s (%s)"),
781 cl->vpn_hostname, cl->real_hostname);
787 syslog(LOG_ERR, _("Got too big PACKET from %s (%s)"),
788 cl->vpn_hostname, cl->real_hostname);
793 syslog(LOG_DEBUG, _("Got PACKET length %d from %s (%s)"), len,
794 cl->vpn_hostname, cl->real_hostname);
802 int ping_h(conn_list_t *cl)
805 if(!cl->status.active)
807 syslog(LOG_ERR, _("Got unauthorized PING from %s (%s)"),
808 cl->vpn_hostname, cl->real_hostname);
813 syslog(LOG_DEBUG, _("Got PING from %s (%s)"),
814 cl->vpn_hostname, cl->real_hostname);
816 cl->status.pinged = 0;
817 cl->status.got_pong = 1;
824 int pong_h(conn_list_t *cl)
827 if(!cl->status.active)
829 syslog(LOG_ERR, _("Got unauthorized PONG from %s (%s)"),
830 cl->vpn_hostname, cl->real_hostname);
835 syslog(LOG_DEBUG, _("Got PONG from %s (%s)"),
836 cl->vpn_hostname, cl->real_hostname);
838 cl->status.got_pong = 1;
843 int add_host_h(conn_list_t *cl)
850 conn_list_t *ncn, *old;
852 if(!cl->status.active)
854 syslog(LOG_ERR, _("Got unauthorized ADD_HOST from %s (%s)"),
855 cl->vpn_hostname, cl->real_hostname);
859 if(sscanf(cl->buffer, "%*d %lx %lx/%lx:%hx %d", &real_ip, &vpn_ip, &vpn_mask, &port, &flags) != 5)
861 syslog(LOG_ERR, _("Got bad ADD_HOST from %s (%s)"),
862 cl->vpn_hostname, cl->real_hostname);
866 if((old = lookup_conn(vpn_ip)))
868 if((real_ip==old->real_ip) && (vpn_mask==old->vpn_mask) && (port==old->port))
871 syslog(LOG_NOTICE, _("Got duplicate ADD_HOST for %s (%s) from %s (%s)"),
872 old->vpn_hostname, old->real_hostname, cl->vpn_hostname, cl->real_hostname);
873 goto skip_add_host; /* One goto a day keeps the deeply nested if constructions away. */
878 syslog(LOG_NOTICE, _("Removing old entry for %s (%s)"),
879 old->vpn_hostname, old->real_hostname);
880 old->status.active = 0;
881 terminate_connection(old);
885 /* Connections lists are really messed up if this happens */
886 if(vpn_ip == myself->vpn_ip)
888 syslog(LOG_ERR, _("Warning: got ADD_HOST from %s (%s) for ourself, restarting"),
889 cl->vpn_hostname, cl->real_hostname);
894 ncn = new_conn_list();
895 ncn->real_ip = real_ip;
896 ncn->real_hostname = hostlookup(htonl(real_ip));
897 ncn->vpn_ip = vpn_ip;
898 ncn->vpn_mask = vpn_mask;
899 ncn->vpn_hostname = hostlookup(htonl(vpn_ip));
903 ncn->next = conn_list;
905 ncn->status.active = 1;
908 syslog(LOG_DEBUG, _("Got ADD_HOST for %s (%s) from %s (%s)"),
909 ncn->vpn_hostname, ncn->real_hostname, cl->vpn_hostname, cl->real_hostname);
911 notify_others(ncn, cl, send_add_host);
918 int req_key_h(conn_list_t *cl)
924 if(!cl->status.active)
926 syslog(LOG_ERR, _("Got unauthorized REQ_KEY from %s (%s)"),
927 cl->vpn_hostname, cl->real_hostname);
931 if(sscanf(cl->buffer, "%*d %lx %lx", &to, &from) != 2)
933 syslog(LOG_ERR, _("Got bad REQ_KEY from %s (%s)"),
934 cl->vpn_hostname, cl->real_hostname);
939 syslog(LOG_DEBUG, _("Got REQ_KEY origin %d.%d.%d.%d destination %d.%d.%d.%d from %s (%s)"),
940 IP_ADDR_V(from), IP_ADDR_V(to), cl->vpn_hostname, cl->real_hostname);
942 if((to & myself->vpn_mask) == (myself->vpn_ip & myself->vpn_mask))
943 { /* hey! they want something from ME! :) */
944 send_key_answer(cl, from);
948 fw = lookup_conn(to);
952 syslog(LOG_ERR, _("Attempting to forward REQ_KEY to %d.%d.%d.%d, which does not exist?"),
958 syslog(LOG_DEBUG, _("Forwarding REQ_KEY to %s (%s)"),
959 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
961 cl->buffer[cl->reqlen-1] = '\n';
963 if(write(fw->nexthop->meta_socket, cl->buffer, cl->reqlen) < 0)
965 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
972 void set_keys(conn_list_t *cl, int expiry, char *key)
978 cl->public_key = xmalloc(sizeof(*cl->key));
979 cl->public_key->key = NULL;
982 if(cl->public_key->key)
983 free(cl->public_key->key);
984 cl->public_key->length = strlen(key);
985 cl->public_key->expiry = expiry;
986 cl->public_key->key = xmalloc(cl->public_key->length + 1);
987 strcpy(cl->public_key->key, key);
989 ek = make_shared_key(key);
993 cl->key = xmalloc(sizeof(*cl->key));
1000 cl->key->length = strlen(ek);
1001 cl->key->expiry = expiry;
1002 cl->key->key = xmalloc(cl->key->length + 1);
1003 strcpy(cl->key->key, ek);
1007 int ans_key_h(conn_list_t *cl)
1013 conn_list_t *fw, *gk;
1015 if(!cl->status.active)
1017 syslog(LOG_ERR, _("Got unauthorized ANS_KEY from %s (%s)"),
1018 cl->vpn_hostname, cl->real_hostname);
1022 if(sscanf(cl->buffer, "%*d %lx %lx %d %as", &to, &from, &expiry, &key) != 4)
1024 syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s)"),
1025 cl->vpn_hostname, cl->real_hostname);
1030 syslog(LOG_DEBUG, _("Got ANS_KEY origin %d.%d.%d.%d destination %d.%d.%d.%d from %s (%s)"),
1031 IP_ADDR_V(from), IP_ADDR_V(to), cl->vpn_hostname, cl->real_hostname);
1033 if(to == myself->vpn_ip)
1034 { /* hey! that key's for ME! :) */
1035 gk = lookup_conn(from);
1039 syslog(LOG_ERR, _("Receiving ANS_KEY origin %d.%d.%d.%d from %s (%s), which does not exist?"),
1040 IP_ADDR_V(from), cl->vpn_hostname, cl->real_hostname);
1044 set_keys(gk, expiry, key);
1045 gk->status.validkey = 1;
1046 gk->status.waitingforkey = 0;
1051 fw = lookup_conn(to);
1055 syslog(LOG_ERR, _("Attempting to forward ANS_KEY to %d.%d.%d.%d, which does not exist?"),
1061 syslog(LOG_DEBUG, _("Forwarding ANS_KEY to %s (%s)"),
1062 fw->nexthop->vpn_hostname, fw->nexthop->real_hostname);
1064 cl->buffer[cl->reqlen-1] = '\n';
1066 if((write(fw->nexthop->meta_socket, cl->buffer, cl->reqlen)) < 0)
1068 syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
1075 int key_changed_h(conn_list_t *cl)
1080 if(!cl->status.active)
1082 syslog(LOG_ERR, _("Got unauthorized KEY_CHANGED from %s (%s)"),
1083 cl->vpn_hostname, cl->real_hostname);
1087 if(sscanf(cl->buffer, "%*d %lx", &from) != 1)
1089 syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
1090 cl->vpn_hostname, cl->real_hostname);
1094 ik = lookup_conn(from);
1098 syslog(LOG_ERR, _("Got KEY_CHANGED origin %d.%d.%d.%d from %s (%s), which does not exist?"),
1099 IP_ADDR_V(from), cl->vpn_hostname, cl->real_hostname);
1104 syslog(LOG_DEBUG, _("Got KEY_CHANGED origin %s from %s (%s)"),
1105 ik->vpn_hostname, cl->vpn_hostname, cl->real_hostname);
1107 ik->status.validkey = 0;
1108 ik->status.waitingforkey = 0;
1110 notify_others(ik, cl, send_key_changed);
1115 /* "Complete overhaul". */
1117 int (*request_handlers[6])(conn_list_t*) = {
1118 id_h, challenge_h, chal_reply_h, ack_h,
1122 char (*request_name[6]) = {
1123 "ID", "CHALLENGE", "CHAL_REPLY", "ACK",