2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "gnunet_util_lib.h"
24 #include "gnunet_signatures.h"
25 #include "gnunet_statistics_service.h"
27 #include "cadet_protocol.h"
28 #include "cadet_path.h"
30 #include "gnunet-service-cadet_tunnel.h"
31 #include "gnunet-service-cadet_connection.h"
32 #include "gnunet-service-cadet_channel.h"
33 #include "gnunet-service-cadet_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"cadet-tun",__VA_ARGS__)
36 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
38 #define REKEY_WAIT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
40 #if !defined(GNUNET_CULL_LOGGING)
41 #define DUMP_KEYS_TO_STDERR GNUNET_YES
43 #define DUMP_KEYS_TO_STDERR GNUNET_YES
46 #define MIN_TUNNEL_BUFFER 8
47 #define MAX_TUNNEL_BUFFER 64
48 #define MAX_SKIPPED_KEYS 64
49 #define MAX_KEY_GAP 256
50 #define AX_HEADER_SIZE (sizeof (uint32_t) * 2\
51 + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey))
54 /******************************************************************************/
55 /******************************** STRUCTS **********************************/
56 /******************************************************************************/
60 struct CadetTChannel *next;
61 struct CadetTChannel *prev;
62 struct CadetChannel *ch;
67 * Connection list and metadata.
69 struct CadetTConnection
74 struct CadetTConnection *next;
79 struct CadetTConnection *prev;
84 struct CadetConnection *c;
87 * Creation time, to keep oldest connection alive.
89 struct GNUNET_TIME_Absolute created;
92 * Connection throughput, to keep fastest connection alive.
99 * Encryption systems possible.
101 enum CadetTunnelEncryption
104 * Default Axolotl system.
111 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
113 struct CadetTunnelSkippedKey
118 struct CadetTunnelSkippedKey *next;
123 struct CadetTunnelSkippedKey *prev;
126 * When was this key stored (for timeout).
128 struct GNUNET_TIME_Absolute timestamp;
133 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
138 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
141 * Key number for a given HK.
148 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
150 struct CadetTunnelAxolotl
153 * A (double linked) list of stored message keys and associated header keys
154 * for "skipped" messages, i.e. messages that have not been
155 * received despite the reception of more recent messages, (head).
157 struct CadetTunnelSkippedKey *skipped_head;
160 * Skipped messages' keys DLL, tail.
162 struct CadetTunnelSkippedKey *skipped_tail;
165 * Elements in @a skipped_head <-> @a skipped_tail.
167 unsigned int skipped;
170 * 32-byte root key which gets updated by DH ratchet.
172 struct GNUNET_CRYPTO_SymmetricSessionKey RK;
175 * 32-byte header key (send).
177 struct GNUNET_CRYPTO_SymmetricSessionKey HKs;
180 * 32-byte header key (recv)
182 struct GNUNET_CRYPTO_SymmetricSessionKey HKr;
185 * 32-byte next header key (send).
187 struct GNUNET_CRYPTO_SymmetricSessionKey NHKs;
190 * 32-byte next header key (recv).
192 struct GNUNET_CRYPTO_SymmetricSessionKey NHKr;
195 * 32-byte chain keys (used for forward-secrecy updating, send).
197 struct GNUNET_CRYPTO_SymmetricSessionKey CKs;
200 * 32-byte chain keys (used for forward-secrecy updating, recv).
202 struct GNUNET_CRYPTO_SymmetricSessionKey CKr;
205 * ECDH for key exchange (A0 / B0).
207 struct GNUNET_CRYPTO_EcdhePrivateKey *kx_0;
210 * ECDH Ratchet key (send).
212 struct GNUNET_CRYPTO_EcdhePrivateKey *DHRs;
215 * ECDH Ratchet key (recv).
217 struct GNUNET_CRYPTO_EcdhePublicKey DHRr;
220 * Message number (reset to 0 with each new ratchet, next message to send).
225 * Message number (reset to 0 with each new ratchet, next message to recv).
230 * Previous message numbers (# of msgs sent under prev ratchet)
235 * True (#GNUNET_YES) if we have to send a new ratchet key in next msg.
240 * Number of messages recieved since our last ratchet advance.
241 * - If this counter = 0, we cannot send a new ratchet key in next msg.
242 * - If this counter > 0, we can (but don't yet have to) send a new key.
244 unsigned int ratchet_allowed;
247 * Number of messages recieved since our last ratchet advance.
248 * - If this counter = 0, we cannot send a new ratchet key in next msg.
249 * - If this counter > 0, we can (but don't yet have to) send a new key.
251 unsigned int ratchet_counter;
254 * When does this ratchet expire and a new one is triggered.
256 struct GNUNET_TIME_Absolute ratchet_expiration;
260 * Struct containing all information regarding a tunnel to a peer.
265 * Endpoint of the tunnel.
267 struct CadetPeer *peer;
270 * Type of encryption used in the tunnel.
272 enum CadetTunnelEncryption enc_type;
277 struct CadetTunnelAxolotl *ax;
280 * State of the tunnel connectivity.
282 enum CadetTunnelCState cstate;
285 * State of the tunnel encryption.
287 enum CadetTunnelEState estate;
290 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own ephemeral
293 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
296 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
298 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
301 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
303 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
306 * Task to start the rekey process.
308 struct GNUNET_SCHEDULER_Task *rekey_task;
311 * Paths that are actively used to reach the destination peer.
313 struct CadetTConnection *connection_head;
314 struct CadetTConnection *connection_tail;
317 * Next connection number.
322 * Channels inside this tunnel.
324 struct CadetTChannel *channel_head;
325 struct CadetTChannel *channel_tail;
328 * Channel ID for the next created channel.
330 CADET_ChannelNumber next_chid;
333 * Destroy flag: if true, destroy on last message.
335 struct GNUNET_SCHEDULER_Task * destroy_task;
338 * Queued messages, to transmit once tunnel gets connected.
340 struct CadetTunnelDelayed *tq_head;
341 struct CadetTunnelDelayed *tq_tail;
344 * Task to trim connections if too many are present.
346 struct GNUNET_SCHEDULER_Task * trim_connections_task;
349 * Ephemeral message in the queue (to avoid queueing more than one).
351 struct CadetConnectionQueue *ephm_h;
354 * Pong message in the queue.
356 struct CadetConnectionQueue *pong_h;
361 * Struct used to save messages in a non-ready tunnel to send once connected.
363 struct CadetTunnelDelayed
368 struct CadetTunnelDelayed *next;
369 struct CadetTunnelDelayed *prev;
374 struct CadetTunnel *t;
377 * Tunnel queue given to the channel to cancel request. Update on send_queued.
379 struct CadetTunnelQueue *tq;
384 /* struct GNUNET_MessageHeader *msg; */
389 * Handle for messages queued but not yet sent.
391 struct CadetTunnelQueue
394 * Connection queue handle, to cancel if necessary.
396 struct CadetConnectionQueue *cq;
399 * Handle in case message hasn't been given to a connection yet.
401 struct CadetTunnelDelayed *tqd;
404 * Continuation to call once sent.
409 * Closure for @c cont.
415 /******************************************************************************/
416 /******************************* GLOBALS ***********************************/
417 /******************************************************************************/
420 * Global handle to the statistics service.
422 extern struct GNUNET_STATISTICS_Handle *stats;
425 * Local peer own ID (memory efficient handle).
427 extern GNUNET_PEER_Id myid;
430 * Local peer own ID (full value).
432 extern struct GNUNET_PeerIdentity my_full_id;
436 * Don't try to recover tunnels if shutting down.
438 extern int shutting_down;
442 * Set of all tunnels, in order to trigger a new exchange on rekey.
443 * Indexed by peer's ID.
445 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
448 * Own Peer ID private key.
450 const static struct GNUNET_CRYPTO_EddsaPrivateKey *id_key;
453 /******************************** AXOLOTL ************************************/
456 * How many messages are needed to trigger a ratchet advance.
458 static unsigned long long ratchet_messages;
461 * How long until we trigger a ratched advance.
463 static struct GNUNET_TIME_Relative ratchet_time;
466 /******************************************************************************/
467 /******************************** STATIC ***********************************/
468 /******************************************************************************/
471 * Get string description for tunnel connectivity state.
473 * @param cs Tunnel state.
475 * @return String representation.
478 cstate2s (enum CadetTunnelCState cs)
484 case CADET_TUNNEL_NEW:
485 return "CADET_TUNNEL_NEW";
486 case CADET_TUNNEL_SEARCHING:
487 return "CADET_TUNNEL_SEARCHING";
488 case CADET_TUNNEL_WAITING:
489 return "CADET_TUNNEL_WAITING";
490 case CADET_TUNNEL_READY:
491 return "CADET_TUNNEL_READY";
492 case CADET_TUNNEL_SHUTDOWN:
493 return "CADET_TUNNEL_SHUTDOWN";
495 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
503 * Get string description for tunnel encryption state.
505 * @param es Tunnel state.
507 * @return String representation.
510 estate2s (enum CadetTunnelEState es)
516 case CADET_TUNNEL_KEY_UNINITIALIZED:
517 return "CADET_TUNNEL_KEY_UNINITIALIZED";
518 case CADET_TUNNEL_KEY_SENT:
519 return "CADET_TUNNEL_KEY_SENT";
520 case CADET_TUNNEL_KEY_PING:
521 return "CADET_TUNNEL_KEY_PING";
522 case CADET_TUNNEL_KEY_OK:
523 return "CADET_TUNNEL_KEY_OK";
524 case CADET_TUNNEL_KEY_REKEY:
525 return "CADET_TUNNEL_KEY_REKEY";
527 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
535 * @brief Check if tunnel is ready to send traffic.
537 * Tunnel must be connected and with encryption correctly set up.
539 * @param t Tunnel to check.
541 * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
544 is_ready (struct CadetTunnel *t)
550 conn_ok = CADET_TUNNEL_READY == t->cstate;
551 enc_ok = CADET_TUNNEL_KEY_OK == t->estate
552 || CADET_TUNNEL_KEY_REKEY == t->estate
553 || (CADET_TUNNEL_KEY_PING == t->estate
554 && CADET_Axolotl == t->enc_type);
555 ready = conn_ok && enc_ok;
556 ready = ready || GCT_is_loopback (t);
562 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
564 * @param tch Tunnel's channel handle.
566 * @return Amount of messages the channel can still buffer towards the client.
569 get_channel_buffer (const struct CadetTChannel *tch)
573 /* If channel is incoming, is terminal in the FWD direction and fwd is YES */
574 fwd = GCCH_is_terminal (tch->ch, GNUNET_YES);
576 return GCCH_get_buffer (tch->ch, fwd);
581 * Get the channel's allowance status.
583 * @param tch Tunnel's channel handle.
585 * @return #GNUNET_YES if we allowed the client to send data to us.
588 get_channel_allowed (const struct CadetTChannel *tch)
592 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
593 fwd = GCCH_is_origin (tch->ch, GNUNET_YES);
595 return GCCH_get_allowed (tch->ch, fwd);
600 * Get the connection's buffer.
602 * @param tc Tunnel's connection handle.
604 * @return Amount of messages the connection can still buffer.
607 get_connection_buffer (const struct CadetTConnection *tc)
611 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
612 fwd = GCC_is_origin (tc->c, GNUNET_YES);
614 return GCC_get_buffer (tc->c, fwd);
619 * Get the connection's allowance.
621 * @param tc Tunnel's connection handle.
623 * @return Amount of messages we have allowed the next peer to send us.
626 get_connection_allowed (const struct CadetTConnection *tc)
630 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
631 fwd = GCC_is_origin (tc->c, GNUNET_YES);
633 return GCC_get_allowed (tc->c, fwd);
638 * Create a new Axolotl ephemeral (ratchet) key.
643 new_ephemeral (struct CadetTunnel *t)
645 GNUNET_free_non_null (t->ax->DHRs);
646 t->ax->DHRs = GNUNET_CRYPTO_ecdhe_key_create();
647 #if DUMP_KEYS_TO_STDERR
649 struct GNUNET_CRYPTO_EcdhePublicKey pub;
650 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &pub);
651 LOG (GNUNET_ERROR_TYPE_DEBUG, " new DHRs generated: pub %s\n",
652 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &pub));
661 * @param plaintext Content to HMAC.
662 * @param size Size of @c plaintext.
663 * @param iv Initialization vector for the message.
664 * @param key Key to use.
665 * @param hmac[out] Destination to store the HMAC.
668 t_hmac (const void *plaintext, size_t size,
669 uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
670 struct GNUNET_CADET_Hash *hmac)
672 static const char ctx[] = "cadet authentication key";
673 struct GNUNET_CRYPTO_AuthKey auth_key;
674 struct GNUNET_HashCode hash;
676 #if DUMP_KEYS_TO_STDERR
677 LOG (GNUNET_ERROR_TYPE_INFO, " HMAC %u bytes with key %s\n", size,
678 GNUNET_i2s ((struct GNUNET_PeerIdentity *) key));
680 GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
685 /* Two step: CADET_Hash is only 256 bits, HashCode is 512. */
686 GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
687 GNUNET_memcpy (hmac, &hash, sizeof (*hmac));
694 * @param key Key to use.
695 * @param hash[out] Resulting HMAC.
696 * @param source Source key material (data to HMAC).
697 * @param len Length of @a source.
700 t_ax_hmac_hash (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
701 struct GNUNET_HashCode *hash,
702 void *source, unsigned int len)
704 static const char ctx[] = "axolotl HMAC-HASH";
705 struct GNUNET_CRYPTO_AuthKey auth_key;
707 GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
710 GNUNET_CRYPTO_hmac (&auth_key, source, len, hash);
715 * Derive a key from a HMAC-HASH.
717 * @param key Key to use for the HMAC.
718 * @param out Key to generate.
719 * @param source Source key material (data to HMAC).
720 * @param len Length of @a source.
723 t_hmac_derive_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
724 struct GNUNET_CRYPTO_SymmetricSessionKey *out,
725 void *source, unsigned int len)
727 static const char ctx[] = "axolotl derive key";
728 struct GNUNET_HashCode h;
730 t_ax_hmac_hash (key, &h, source, len);
731 GNUNET_CRYPTO_kdf (out, sizeof (*out), ctx, sizeof (ctx),
732 &h, sizeof (h), NULL);
737 * Encrypt data with the axolotl tunnel key.
739 * @param t Tunnel whose key to use.
740 * @param dst Destination for the encrypted data.
741 * @param src Source of the plaintext. Can overlap with @c dst.
742 * @param size Size of the plaintext.
744 * @return Size of the encrypted data.
747 t_ax_encrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
749 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
750 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
751 struct CadetTunnelAxolotl *ax;
754 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_ax_encrypt start\n");
758 ax->ratchet_counter++;
759 if (GNUNET_YES == ax->ratchet_allowed
760 && (ratchet_messages <= ax->ratchet_counter
761 || 0 == GNUNET_TIME_absolute_get_remaining (ax->ratchet_expiration).rel_value_us))
763 ax->ratchet_flag = GNUNET_YES;
766 if (GNUNET_YES == ax->ratchet_flag)
768 /* Advance ratchet */
769 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3];
770 struct GNUNET_HashCode dh;
771 struct GNUNET_HashCode hmac;
772 static const char ctx[] = "axolotl ratchet";
777 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
778 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs, &ax->DHRr, &dh);
779 t_ax_hmac_hash (&ax->RK, &hmac, &dh, sizeof (dh));
780 GNUNET_CRYPTO_kdf (keys, sizeof (keys), ctx, sizeof (ctx),
781 &hmac, sizeof (hmac), NULL);
788 ax->ratchet_flag = GNUNET_NO;
789 ax->ratchet_allowed = GNUNET_NO;
790 ax->ratchet_counter = 0;
791 ax->ratchet_expiration =
792 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), ratchet_time);
795 t_hmac_derive_key (&ax->CKs, &MK, "0", 1);
796 GNUNET_CRYPTO_symmetric_derive_iv (&iv, &MK, NULL, 0, NULL);
798 #if DUMP_KEYS_TO_STDERR
799 LOG (GNUNET_ERROR_TYPE_DEBUG, " CKs: %s\n",
800 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKs));
801 LOG (GNUNET_ERROR_TYPE_INFO, " AX_ENC with key %u: %s\n", ax->Ns,
802 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &MK));
805 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, &MK, &iv, dst);
807 t_hmac_derive_key (&ax->CKs, &ax->CKs, "1", 1);
809 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_ax_encrypt end\n");
816 * Decrypt data with the axolotl tunnel key.
818 * @param t Tunnel whose key to use.
819 * @param dst Destination for the decrypted data.
820 * @param src Source of the ciphertext. Can overlap with @c dst.
821 * @param size Size of the ciphertext.
823 * @return Size of the decrypted data.
826 t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
828 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
829 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
830 struct CadetTunnelAxolotl *ax;
833 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_ax_decrypt start\n");
837 t_hmac_derive_key (&ax->CKr, &MK, "0", 1);
838 GNUNET_CRYPTO_symmetric_derive_iv (&iv, &MK, NULL, 0, NULL);
840 #if DUMP_KEYS_TO_STDERR
841 LOG (GNUNET_ERROR_TYPE_DEBUG, " CKr: %s\n",
842 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKr));
843 LOG (GNUNET_ERROR_TYPE_INFO, " AX_DEC with key %u: %s\n", ax->Nr,
844 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &MK));
847 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
848 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, &MK, &iv, dst);
849 GNUNET_assert (out_size == size);
851 t_hmac_derive_key (&ax->CKr, &ax->CKr, "1", 1);
853 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_ax_decrypt end\n");
860 * Encrypt header with the axolotl header key.
862 * @param t Tunnel whose key to use.
863 * @param msg Message whose header to encrypt.
866 t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_AX *msg)
868 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
869 struct CadetTunnelAxolotl *ax;
872 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_h_encrypt start\n");
875 GNUNET_CRYPTO_symmetric_derive_iv (&iv, &ax->HKs, NULL, 0, NULL);
877 #if DUMP_KEYS_TO_STDERR
878 LOG (GNUNET_ERROR_TYPE_INFO, " AX_ENC_H with key %s\n",
879 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKs));
882 out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->Ns, AX_HEADER_SIZE,
883 &ax->HKs, &iv, &msg->Ns);
885 GNUNET_assert (AX_HEADER_SIZE == out_size);
887 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_ax_encrypt end\n");
892 * Decrypt header with the current axolotl header key.
894 * @param t Tunnel whose current ax HK to use.
895 * @param src Message whose header to decrypt.
896 * @param dst Where to decrypt header to.
899 t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_AX *src,
900 struct GNUNET_CADET_AX *dst)
902 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
903 struct CadetTunnelAxolotl *ax;
906 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_h_decrypt start\n");
909 GNUNET_CRYPTO_symmetric_derive_iv (&iv, &ax->HKr, NULL, 0, NULL);
911 #if DUMP_KEYS_TO_STDERR
912 LOG (GNUNET_ERROR_TYPE_INFO, " AX_DEC_H with key %s\n",
913 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKr));
916 out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns, AX_HEADER_SIZE,
917 &ax->HKr, &iv, &dst->Ns);
919 GNUNET_assert (AX_HEADER_SIZE == out_size);
921 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_h_decrypt end\n");
926 * Decrypt and verify data with the appropriate tunnel key and verify that the
927 * data has not been altered since it was sent by the remote peer.
929 * @param t Tunnel whose key to use.
930 * @param dst Destination for the plaintext.
931 * @param src Source of the message. Can overlap with @c dst.
932 * @param size Size of the message.
934 * @return Size of the decrypted data, -1 if an error was encountered.
937 try_old_ax_keys (struct CadetTunnel *t, void *dst,
938 const struct GNUNET_CADET_AX *src, size_t size)
940 struct CadetTunnelSkippedKey *key;
941 struct GNUNET_CADET_Hash *hmac;
942 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
943 struct GNUNET_CADET_AX plaintext_header;
944 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n");
951 hmac = &plaintext_header.hmac;
952 esize = size - sizeof (struct GNUNET_CADET_AX);
954 /* Find a correct Header Key */
955 for (key = t->ax->skipped_head; NULL != key; key = key->next)
957 #if DUMP_KEYS_TO_STDERR
958 LOG (GNUNET_ERROR_TYPE_DEBUG, " Trying hmac with key %s\n",
959 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->HK));
961 t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &key->HK, hmac);
962 if (0 == memcmp (hmac, &src->hmac, sizeof (*hmac)))
964 LOG (GNUNET_ERROR_TYPE_DEBUG, " hmac correct\n");
972 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
973 GNUNET_assert (size > sizeof (struct GNUNET_CADET_AX));
974 len = size - sizeof (struct GNUNET_CADET_AX);
975 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
978 GNUNET_CRYPTO_symmetric_derive_iv (&iv, &key->HK, NULL, 0, NULL);
979 res = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns, AX_HEADER_SIZE,
980 &key->HK, &iv, &plaintext_header.Ns);
981 GNUNET_assert (AX_HEADER_SIZE == res);
982 LOG (GNUNET_ERROR_TYPE_DEBUG, " Message %u, previous: %u\n",
983 ntohl (plaintext_header.Ns), ntohl (plaintext_header.PNs));
985 /* Find the correct Message Key */
986 N = ntohl (plaintext_header.Ns);
987 while (NULL != key && N != key->Kn)
989 if (NULL == key || 0 != memcmp (&key->HK, valid_HK, sizeof (*valid_HK)))
992 #if DUMP_KEYS_TO_STDERR
993 LOG (GNUNET_ERROR_TYPE_INFO, " AX_DEC_H with skipped key %s\n",
994 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->HK));
995 LOG (GNUNET_ERROR_TYPE_INFO, " AX_DEC with skipped key %u: %s\n",
996 key->Kn, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
999 /* Decrypt payload */
1000 GNUNET_CRYPTO_symmetric_derive_iv (&iv, &key->MK, NULL, 0, NULL);
1001 res = GNUNET_CRYPTO_symmetric_decrypt (&src[1], len, &key->MK, &iv, dst);
1004 GNUNET_CONTAINER_DLL_remove (t->ax->skipped_head, t->ax->skipped_tail, key);
1006 GNUNET_free (key); /* GNUNET_free overwrites memory with 0xbaadf00d */
1013 * Delete a key from the list of skipped keys.
1015 * @param t Tunnel to delete from.
1016 * @param HKr Header Key to use.
1019 store_skipped_key (struct CadetTunnel *t,
1020 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
1022 struct CadetTunnelSkippedKey *key;
1024 key = GNUNET_new (struct CadetTunnelSkippedKey);
1025 key->timestamp = GNUNET_TIME_absolute_get ();
1026 key->Kn = t->ax->Nr;
1027 key->HK = t->ax->HKr;
1028 t_hmac_derive_key (&t->ax->CKr, &key->MK, "0", 1);
1029 #if DUMP_KEYS_TO_STDERR
1030 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing MK for Nr %u: %s\n",
1031 key->Kn, GNUNET_i2s ((struct GNUNET_PeerIdentity *) &key->MK));
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, " for CKr: %s\n",
1033 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->ax->CKr));
1035 t_hmac_derive_key (&t->ax->CKr, &t->ax->CKr, "1", 1);
1036 GNUNET_CONTAINER_DLL_insert (t->ax->skipped_head, t->ax->skipped_tail, key);
1043 * Delete a key from the list of skipped keys.
1045 * @param t Tunnel to delete from.
1046 * @param key Key to delete.
1049 delete_skipped_key (struct CadetTunnel *t, struct CadetTunnelSkippedKey *key)
1051 GNUNET_CONTAINER_DLL_remove (t->ax->skipped_head, t->ax->skipped_tail, key);
1058 * Stage skipped AX keys and calculate the message key.
1060 * Stores each HK and MK for skipped messages.
1062 * @param t Tunnel where to stage the keys.
1063 * @param HKr Header key.
1064 * @param Np Received meesage number.
1066 * @return GNUNET_OK if keys were stored.
1067 * GNUNET_SYSERR if an error ocurred (Np not expected).
1070 store_ax_keys (struct CadetTunnel *t,
1071 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
1077 gap = Np - t->ax->Nr;
1078 LOG (GNUNET_ERROR_TYPE_INFO, "Storing keys [%u, %u)\n", t->ax->Nr, Np);
1079 if (MAX_KEY_GAP < gap)
1081 /* Avoid DoS (forcing peer to do 2*33 chain HMAC operations) */
1082 /* TODO: start new key exchange on return */
1083 GNUNET_break_op (0);
1084 LOG (GNUNET_ERROR_TYPE_WARNING, "Got message %u, expected %u+\n",
1086 return GNUNET_SYSERR;
1090 /* Delayed message: don't store keys, flag to try old keys. */
1091 return GNUNET_SYSERR;
1094 while (t->ax->Nr < Np)
1095 store_skipped_key (t, HKr);
1097 while (t->ax->skipped > MAX_SKIPPED_KEYS)
1098 delete_skipped_key (t, t->ax->skipped_tail);
1105 * Decrypt and verify data with the appropriate tunnel key and verify that the
1106 * data has not been altered since it was sent by the remote peer.
1108 * @param t Tunnel whose key to use.
1109 * @param dst Destination for the plaintext.
1110 * @param src Source of the message. Can overlap with @c dst.
1111 * @param size Size of the message.
1113 * @return Size of the decrypted data, -1 if an error was encountered.
1116 t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
1117 const struct GNUNET_CADET_AX *src, size_t size)
1119 struct CadetTunnelAxolotl *ax;
1120 struct GNUNET_CADET_Hash msg_hmac;
1121 struct GNUNET_HashCode hmac;
1122 struct GNUNET_CADET_AX plaintext_header;
1129 esize = size - sizeof (struct GNUNET_CADET_AX);
1134 /* Try current HK */
1135 t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &ax->HKr, &msg_hmac);
1136 if (0 != memcmp (&msg_hmac, &src->hmac, sizeof (msg_hmac)))
1138 static const char ctx[] = "axolotl ratchet";
1139 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1140 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1141 struct GNUNET_HashCode dh;
1142 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " trying next HK\n");
1146 t_hmac (&src->Ns, AX_HEADER_SIZE + esize, 0, &ax->NHKr, &msg_hmac);
1147 if (0 != memcmp (&msg_hmac, &src->hmac, sizeof (msg_hmac)))
1149 /* Try the skipped keys, if that fails, we're out of luck. */
1150 return try_old_ax_keys (t, dst, src, size);
1152 LOG (GNUNET_ERROR_TYPE_INFO, "next HK worked\n");
1156 t_h_decrypt (t, src, &plaintext_header);
1157 Np = ntohl (plaintext_header.Ns);
1158 PNp = ntohl (plaintext_header.PNs);
1159 DHRp = &plaintext_header.DHRs;
1160 store_ax_keys (t, &HK, PNp);
1162 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1163 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs, DHRp, &dh);
1164 t_ax_hmac_hash (&ax->RK, &hmac, &dh, sizeof (dh));
1165 GNUNET_CRYPTO_kdf (keys, sizeof (keys), ctx, sizeof (ctx),
1166 &hmac, sizeof (hmac), NULL);
1168 /* Commit "purported" keys */
1174 ax->ratchet_allowed = GNUNET_YES;
1178 LOG (GNUNET_ERROR_TYPE_DEBUG, "current HK\n");
1179 t_h_decrypt (t, src, &plaintext_header);
1180 Np = ntohl (plaintext_header.Ns);
1181 PNp = ntohl (plaintext_header.PNs);
1183 LOG (GNUNET_ERROR_TYPE_INFO, " got AX Nr %u\n", Np);
1185 if (GNUNET_OK != store_ax_keys (t, &ax->HKr, Np))
1186 /* Try the skipped keys, if that fails, we're out of luck. */
1187 return try_old_ax_keys (t, dst, src, size);
1189 osize = t_ax_decrypt (t, dst, &src[1], esize);
1194 GNUNET_break_op (0);
1203 * Pick a connection on which send the next data message.
1205 * @param t Tunnel on which to send the message.
1207 * @return The connection on which to send the next message.
1209 static struct CadetConnection *
1210 tunnel_get_connection (struct CadetTunnel *t)
1212 struct CadetTConnection *iter;
1213 struct CadetConnection *best;
1215 unsigned int lowest_q;
1217 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GCT_2s (t));
1219 lowest_q = UINT_MAX;
1220 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1222 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
1223 GCC_2s (iter->c), GCC_get_state (iter->c));
1224 if (CADET_CONNECTION_READY == GCC_get_state (iter->c))
1226 qn = GCC_get_qn (iter->c, GCC_is_origin (iter->c, GNUNET_YES));
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
1235 LOG (GNUNET_ERROR_TYPE_DEBUG, " selected: connection %s\n", GCC_2s (best));
1241 * Callback called when a queued message is sent.
1243 * Calculates the average time and connection packet tracking.
1245 * @param cls Closure (TunnelQueue handle).
1246 * @param c Connection this message was on.
1247 * @param q Connection queue handle (unused).
1248 * @param type Type of message sent.
1249 * @param fwd Was this a FWD going message?
1250 * @param size Size of the message.
1253 tun_message_sent (void *cls,
1254 struct CadetConnection *c,
1255 struct CadetConnectionQueue *q,
1256 uint16_t type, int fwd, size_t size)
1258 struct CadetTunnelQueue *qt = cls;
1259 struct CadetTunnel *t;
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, "tun_message_sent\n");
1263 GNUNET_assert (NULL != qt->cont);
1264 t = NULL == c ? NULL : GCC_get_tunnel (c);
1265 qt->cont (qt->cont_cls, t, qt, type, size);
1271 count_queued_data (const struct CadetTunnel *t)
1273 struct CadetTunnelDelayed *iter;
1276 for (count = 0, iter = t->tq_head; iter != NULL; iter = iter->next)
1283 * Delete a queued message: either was sent or the channel was destroyed
1284 * before the tunnel's key exchange had a chance to finish.
1286 * @param tqd Delayed queue handle.
1289 unqueue_data (struct CadetTunnelDelayed *tqd)
1291 GNUNET_CONTAINER_DLL_remove (tqd->t->tq_head, tqd->t->tq_tail, tqd);
1297 * Cache a message to be sent once tunnel is online.
1299 * @param t Tunnel to hold the message.
1300 * @param msg Message itself (copy will be made).
1302 static struct CadetTunnelDelayed *
1303 queue_data (struct CadetTunnel *t, const struct GNUNET_MessageHeader *msg)
1305 struct CadetTunnelDelayed *tqd;
1306 uint16_t size = ntohs (msg->size);
1308 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GCT_2s (t));
1310 GNUNET_assert (GNUNET_NO == is_ready (t));
1312 tqd = GNUNET_malloc (sizeof (struct CadetTunnelDelayed) + size);
1315 GNUNET_memcpy (&tqd[1], msg, size);
1316 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tqd);
1322 * Sends an already built message on a tunnel, encrypting it and
1323 * choosing the best connection.
1325 * @param message Message to send. Function modifies it.
1326 * @param t Tunnel on which this message is transmitted.
1327 * @param c Connection to use (autoselect if NULL).
1328 * @param force Force the tunnel to take the message (buffer overfill).
1329 * @param cont Continuation to call once message is really sent.
1330 * @param cont_cls Closure for @c cont.
1331 * @param existing_q In case this a transmission of previously queued data,
1332 * this should be TunnelQueue given to the client.
1334 * @return Handle to cancel message.
1335 * NULL if @c cont is NULL or an error happens and message is dropped.
1337 static struct CadetTunnelQueue *
1338 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1339 struct CadetTunnel *t,
1340 struct CadetConnection *c,
1344 struct CadetTunnelQueue *existing_q)
1346 struct GNUNET_MessageHeader *msg;
1347 struct GNUNET_CADET_AX *ax_msg;
1348 struct CadetTunnelQueue *tq;
1349 size_t size = ntohs (message->size);
1350 char cbuf[sizeof (struct GNUNET_CADET_AX) + size] GNUNET_ALIGN;
1356 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GCT_2s (t));
1358 if (GNUNET_NO == is_ready (t))
1360 struct CadetTunnelDelayed *tqd;
1361 /* A non null existing_q indicates sending of queued data.
1362 * Should only happen after tunnel becomes ready.
1364 GNUNET_assert (NULL == existing_q);
1365 tqd = queue_data (t, message);
1368 tq = GNUNET_new (struct CadetTunnelQueue);
1372 tq->cont_cls = cont_cls;
1376 GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
1378 GNUNET_assert (CADET_Axolotl == t->enc_type);
1379 ax_msg = (struct GNUNET_CADET_AX *) cbuf;
1380 msg = &ax_msg->header;
1381 msg->size = htons (sizeof (struct GNUNET_CADET_AX) + size);
1382 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_AX);
1383 esize = t_ax_encrypt (t, &ax_msg[1], message, size);
1384 ax_msg->Ns = htonl (t->ax->Ns++);
1385 ax_msg->PNs = htonl (t->ax->PNs);
1386 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &ax_msg->DHRs);
1387 t_h_encrypt (t, ax_msg);
1388 t_hmac (&ax_msg->Ns, AX_HEADER_SIZE + esize, 0, &t->ax->HKs, &ax_msg->hmac);
1389 GNUNET_assert (esize == size);
1392 c = tunnel_get_connection (t);
1395 /* Why is tunnel 'ready'? Should have been queued! */
1396 if (NULL != t->destroy_task)
1399 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
1401 return NULL; /* Drop... */
1405 type = ntohs (message->type);
1408 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1409 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1410 if (GNUNET_MESSAGE_TYPE_CADET_DATA == type)
1411 mid = ntohl (((struct GNUNET_CADET_Data *) message)->mid);
1413 mid = ntohl (((struct GNUNET_CADET_DataACK *) message)->mid);
1415 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1416 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1417 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1418 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1419 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1423 LOG (GNUNET_ERROR_TYPE_ERROR, "type %s not valid\n", GC_m2s (type));
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s\n", GC_m2s (type));
1427 fwd = GCC_is_origin (c, GNUNET_YES);
1431 GNUNET_break (NULL == GCC_send_prebuilt_message (msg, type, mid, c, fwd,
1432 force, NULL, NULL));
1435 if (NULL == existing_q)
1437 tq = GNUNET_new (struct CadetTunnelQueue); /* FIXME valgrind: leak*/
1444 tq->cq = GCC_send_prebuilt_message (msg, type, mid, c, fwd, force,
1445 &tun_message_sent, tq);
1446 GNUNET_assert (NULL != tq->cq);
1448 tq->cont_cls = cont_cls;
1455 * Send all cached messages that we can, tunnel is online.
1457 * @param t Tunnel that holds the messages. Cannot be loopback.
1460 send_queued_data (struct CadetTunnel *t)
1462 struct CadetTunnelDelayed *tqd;
1463 struct CadetTunnelDelayed *next;
1466 LOG (GNUNET_ERROR_TYPE_INFO, "Send queued data, tunnel %s\n", GCT_2s (t));
1468 if (GCT_is_loopback (t))
1474 if (GNUNET_NO == is_ready (t))
1476 LOG (GNUNET_ERROR_TYPE_WARNING, " not ready yet: %s/%s\n",
1477 estate2s (t->estate), cstate2s (t->cstate));
1481 room = GCT_get_connections_buffer (t);
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq head: %p\n", t->tq_head);
1484 for (tqd = t->tq_head; NULL != tqd && room > 0; tqd = next)
1486 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending queued data\n");
1489 send_prebuilt_message ((struct GNUNET_MessageHeader *) &tqd[1],
1490 tqd->t, NULL, GNUNET_YES,
1491 NULL != tqd->tq ? tqd->tq->cont : NULL,
1492 NULL != tqd->tq ? tqd->tq->cont_cls : NULL,
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_send_queued_data end\n", GCP_2s (t->peer));
1501 * @brief Resend the AX KX until we complete the handshake.
1503 * @param cls Closure (tunnel).
1506 ax_kx_resend (void *cls)
1508 struct CadetTunnel *t = cls;
1510 t->rekey_task = NULL;
1511 if (CADET_TUNNEL_KEY_OK == t->estate)
1513 /* Should have been canceled on estate change */
1518 GCT_send_ax_kx (t, CADET_TUNNEL_KEY_SENT >= t->estate);
1523 * Callback called when a queued message is sent.
1525 * @param cls Closure.
1526 * @param c Connection this message was on.
1527 * @param type Type of message sent.
1528 * @param fwd Was this a FWD going message?
1529 * @param size Size of the message.
1532 ephm_sent (void *cls,
1533 struct CadetConnection *c,
1534 struct CadetConnectionQueue *q,
1535 uint16_t type, int fwd, size_t size)
1537 struct CadetTunnel *t = cls;
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, "ephemeral sent %s\n", GC_m2s (type));
1542 if (CADET_TUNNEL_KEY_OK == t->estate)
1545 if (CADET_Axolotl == t->enc_type)
1547 if (NULL != t->rekey_task)
1550 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
1551 GNUNET_SCHEDULER_cancel (t->rekey_task);
1553 t->rekey_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1560 * Sends key exchange message on a tunnel, choosing the best connection.
1561 * Should not be called on loopback tunnels.
1563 * @param t Tunnel on which this message is transmitted.
1564 * @param message Message to send. Function modifies it.
1566 * @return Handle to the message in the connection queue.
1568 static struct CadetConnectionQueue *
1569 send_kx (struct CadetTunnel *t,
1570 const struct GNUNET_MessageHeader *message)
1572 struct CadetConnection *c;
1573 struct GNUNET_CADET_KX *msg;
1574 size_t size = ntohs (message->size);
1575 char cbuf[sizeof (struct GNUNET_CADET_KX) + size];
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GCT_2s (t));
1581 /* Avoid loopback. */
1582 if (GCT_is_loopback (t))
1587 type = ntohs (message->type);
1589 /* Even if tunnel is "being destroyed", send anyway.
1590 * Could be a response to a rekey initiated by remote peer,
1591 * who is trying to create a new channel!
1594 /* Must have a connection, or be looking for one. */
1595 if (NULL == t->connection_head)
1597 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s with no connection\n", GC_m2s (type));
1598 if (CADET_TUNNEL_SEARCHING != t->cstate)
1601 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1606 msg = (struct GNUNET_CADET_KX *) cbuf;
1607 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX);
1608 msg->header.size = htons (sizeof (struct GNUNET_CADET_KX) + size);
1609 c = tunnel_get_connection (t);
1612 if (NULL == t->destroy_task && CADET_TUNNEL_READY == t->cstate)
1615 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1621 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1622 GNUNET_assert (NULL == t->ephm_h);
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n", GC_m2s (type));
1628 GNUNET_memcpy (&msg[1], message, size);
1630 fwd = GCC_is_origin (c, GNUNET_YES);
1632 return GCC_send_prebuilt_message (&msg->header, type, 0, c,
1639 * Called only on shutdown, destroy every tunnel.
1641 * @param cls Closure (unused).
1642 * @param key Current public key.
1643 * @param value Value in the hash map (tunnel).
1645 * @return #GNUNET_YES, so we should continue to iterate,
1648 destroy_iterator (void *cls,
1649 const struct GNUNET_PeerIdentity *key,
1652 struct CadetTunnel *t = value;
1654 LOG (GNUNET_ERROR_TYPE_DEBUG,
1655 "GCT_shutdown destroying tunnel at %p\n", t);
1662 * Notify remote peer that we don't know a channel he is talking about,
1663 * probably CHANNEL_DESTROY was missed.
1665 * @param t Tunnel on which to notify.
1666 * @param gid ID of the channel.
1669 send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
1671 struct GNUNET_CADET_ChannelManage msg;
1673 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1674 msg.header.size = htons (sizeof (msg));
1675 msg.chid = htonl (gid);
1677 LOG (GNUNET_ERROR_TYPE_DEBUG,
1678 "WARNING destroying unknown channel %u on tunnel %s\n",
1680 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1685 * Demultiplex data per channel and call appropriate channel handler.
1687 * @param t Tunnel on which the data came.
1688 * @param msg Data message.
1689 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1690 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1691 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1692 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1695 handle_data (struct CadetTunnel *t,
1696 const struct GNUNET_CADET_Data *msg,
1699 struct CadetChannel *ch;
1705 size = ntohs (msg->header.size);
1707 sizeof (struct GNUNET_CADET_Data) +
1708 sizeof (struct GNUNET_MessageHeader))
1713 type = ntohs (msg[1].header.type);
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n", GC_m2s (type));
1715 SPRINTF (buf, "# received payload of type %hu", type);
1716 GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
1720 ch = GCT_get_channel (t, ntohl (msg->chid));
1723 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
1725 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel 0x%X unknown\n", ntohl (msg->chid));
1726 send_channel_destroy (t, ntohl (msg->chid));
1730 GCCH_handle_data (ch, msg, fwd);
1735 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
1737 * @param t Tunnel on which the DATA ACK came.
1738 * @param msg DATA ACK message.
1739 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1740 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1741 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1742 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1745 handle_data_ack (struct CadetTunnel *t,
1746 const struct GNUNET_CADET_DataACK *msg,
1749 struct CadetChannel *ch;
1753 size = ntohs (msg->header.size);
1754 if (size != sizeof (struct GNUNET_CADET_DataACK))
1761 ch = GCT_get_channel (t, ntohl (msg->chid));
1764 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1766 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1771 GCCH_handle_data_ack (ch, msg, fwd);
1776 * Handle channel create.
1778 * @param t Tunnel on which the message came.
1779 * @param msg ChannelCreate message.
1782 handle_ch_create (struct CadetTunnel *t,
1783 const struct GNUNET_CADET_ChannelCreate *msg)
1785 struct CadetChannel *ch;
1789 size = ntohs (msg->header.size);
1790 if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
1792 GNUNET_break_op (0);
1797 ch = GCT_get_channel (t, ntohl (msg->chid));
1798 if (NULL != ch && ! GCT_is_loopback (t))
1800 /* Probably a retransmission, safe to ignore */
1801 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1803 ch = GCCH_handle_create (t, msg);
1805 GCT_add_channel (t, ch);
1811 * Handle channel NACK: check correctness and call channel handler for NACKs.
1813 * @param t Tunnel on which the NACK came.
1814 * @param msg NACK message.
1817 handle_ch_nack (struct CadetTunnel *t,
1818 const struct GNUNET_CADET_ChannelManage *msg)
1820 struct CadetChannel *ch;
1824 size = ntohs (msg->header.size);
1825 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1832 ch = GCT_get_channel (t, ntohl (msg->chid));
1835 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1837 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1842 GCCH_handle_nack (ch);
1847 * Handle a CHANNEL ACK (SYNACK/ACK).
1849 * @param t Tunnel on which the CHANNEL ACK came.
1850 * @param msg CHANNEL ACK message.
1851 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1852 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1853 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1854 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1857 handle_ch_ack (struct CadetTunnel *t,
1858 const struct GNUNET_CADET_ChannelManage *msg,
1861 struct CadetChannel *ch;
1865 size = ntohs (msg->header.size);
1866 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1873 ch = GCT_get_channel (t, ntohl (msg->chid));
1876 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1878 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1883 GCCH_handle_ack (ch, msg, fwd);
1888 * Handle a channel destruction message.
1890 * @param t Tunnel on which the message came.
1891 * @param msg Channel destroy message.
1892 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1893 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1894 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1895 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1898 handle_ch_destroy (struct CadetTunnel *t,
1899 const struct GNUNET_CADET_ChannelManage *msg,
1902 struct CadetChannel *ch;
1906 size = ntohs (msg->header.size);
1907 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1914 ch = GCT_get_channel (t, ntohl (msg->chid));
1917 /* Probably a retransmission, safe to ignore */
1921 GCCH_handle_destroy (ch, msg, fwd);
1926 * Free Axolotl data.
1931 destroy_ax (struct CadetTunnel *t)
1936 GNUNET_free_non_null (t->ax->DHRs);
1937 GNUNET_free_non_null (t->ax->kx_0);
1938 while (NULL != t->ax->skipped_head)
1939 delete_skipped_key (t, t->ax->skipped_head);
1940 GNUNET_assert (0 == t->ax->skipped);
1942 GNUNET_free (t->ax);
1945 if (NULL != t->rekey_task)
1947 GNUNET_SCHEDULER_cancel (t->rekey_task);
1948 t->rekey_task = NULL;
1950 if (NULL != t->ephm_h)
1952 GCC_cancel (t->ephm_h);
1959 * Handle Axolotl handshake.
1961 * @param t Tunnel this message came on.
1962 * @param msg Key eXchange Pong message.
1965 handle_kx_ax (struct CadetTunnel *t, const struct GNUNET_CADET_AX_KX *msg)
1967 struct CadetTunnelAxolotl *ax;
1968 struct GNUNET_HashCode key_material[3];
1969 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
1970 const char salt[] = "CADET Axolotl salt";
1971 const struct GNUNET_PeerIdentity *pid;
1974 LOG (GNUNET_ERROR_TYPE_INFO, "<== { AX_KX} on %s\n", GCT_2s (t));
1978 /* Something is wrong if ax is NULL. Whose fault it is? */
1979 GNUNET_break (CADET_Axolotl == t->enc_type);
1984 pid = GCT_get_destination (t);
1985 if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, pid))
1986 am_I_alice = GNUNET_YES;
1987 else if (0 < GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, pid))
1988 am_I_alice = GNUNET_NO;
1991 GNUNET_break_op (0);
1995 if (0 != (GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
1997 if (NULL != t->rekey_task)
1999 GNUNET_SCHEDULER_cancel (t->rekey_task);
2000 t->rekey_task = NULL;
2002 GCT_send_ax_kx (t, GNUNET_NO);
2005 if (0 == memcmp (&ax->DHRr, &msg->ratchet_key, sizeof(msg->ratchet_key)))
2007 LOG (GNUNET_ERROR_TYPE_INFO, " known ratchet key, exit\n");
2011 LOG (GNUNET_ERROR_TYPE_INFO, " is Alice? %s\n", am_I_alice ? "YES" : "NO");
2013 ax->DHRr = msg->ratchet_key;
2016 if (GNUNET_YES == am_I_alice)
2018 GNUNET_CRYPTO_eddsa_ecdh (id_key, /* A */
2019 &msg->ephemeral_key, /* B0 */
2024 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* B0 */
2025 &pid->public_key, /* A */
2030 if (GNUNET_YES == am_I_alice)
2032 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* A0 */
2033 &pid->public_key, /* B */
2038 GNUNET_CRYPTO_eddsa_ecdh (id_key, /* A */
2039 &msg->ephemeral_key, /* B0 */
2046 /* (This is the triple-DH, we could probably safely skip this,
2047 as A0/B0 are already in the key material.) */
2048 GNUNET_CRYPTO_ecc_ecdh (ax->kx_0, /* A0 or B0 */
2049 &msg->ephemeral_key, /* B0 or A0 */
2052 #if DUMP_KEYS_TO_STDERR
2055 for (i = 0; i < 3; i++)
2056 LOG (GNUNET_ERROR_TYPE_INFO, "km[%u]: %s\n",
2057 i, GNUNET_h2s (&key_material[i]));
2062 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
2063 salt, sizeof (salt),
2064 &key_material, sizeof (key_material), NULL);
2066 if (0 == memcmp (&ax->RK, &keys[0], sizeof(ax->RK)))
2068 LOG (GNUNET_ERROR_TYPE_INFO, " known handshake key, exit\n");
2072 if (GNUNET_YES == am_I_alice)
2078 ax->ratchet_flag = GNUNET_YES;
2086 ax->ratchet_flag = GNUNET_NO;
2087 ax->ratchet_allowed = GNUNET_NO;
2088 ax->ratchet_counter = 0;
2089 ax->ratchet_expiration =
2090 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), ratchet_time);
2095 GCT_change_estate (t, CADET_TUNNEL_KEY_PING);
2096 send_queued_data (t);
2101 * Demultiplex by message type and call appropriate handler for a message
2102 * towards a channel of a local tunnel.
2104 * @param t Tunnel this message came on.
2105 * @param msgh Message header.
2106 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2107 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2108 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2109 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2112 handle_decrypted (struct CadetTunnel *t,
2113 const struct GNUNET_MessageHeader *msgh,
2119 type = ntohs (msgh->type);
2120 LOG (GNUNET_ERROR_TYPE_DEBUG, "<-- %s on %s\n", GC_m2s (type), GCT_2s (t));
2121 SPRINTF (buf, "# received encrypted of type %hu (%s)", type, GC_m2s (type));
2122 GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
2126 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
2127 /* Do nothing, connection aleady got updated. */
2128 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
2131 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2132 /* Don't send hop ACK, wait for client to ACK */
2133 handle_data (t, (struct GNUNET_CADET_Data *) msgh, fwd);
2136 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2137 handle_data_ack (t, (struct GNUNET_CADET_DataACK *) msgh, fwd);
2140 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2141 handle_ch_create (t, (struct GNUNET_CADET_ChannelCreate *) msgh);
2144 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2145 handle_ch_nack (t, (struct GNUNET_CADET_ChannelManage *) msgh);
2148 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2149 handle_ch_ack (t, (struct GNUNET_CADET_ChannelManage *) msgh, fwd);
2152 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2153 handle_ch_destroy (t, (struct GNUNET_CADET_ChannelManage *) msgh, fwd);
2157 GNUNET_break_op (0);
2158 LOG (GNUNET_ERROR_TYPE_WARNING,
2159 "end-to-end message not known (%u)\n",
2160 ntohs (msgh->type));
2161 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
2166 /******************************************************************************/
2167 /******************************** API ***********************************/
2168 /******************************************************************************/
2170 * Decrypt old format and demultiplex by message type. Call appropriate handler
2171 * for a message towards a channel of a local tunnel.
2173 * @param t Tunnel this message came on.
2174 * @param msg Message header.
2177 GCT_handle_encrypted (struct CadetTunnel *t,
2178 const struct GNUNET_MessageHeader *msg)
2180 uint16_t size = ntohs (msg->size);
2184 const struct GNUNET_MessageHeader *msgh;
2187 type = ntohs (msg->type);
2190 case GNUNET_MESSAGE_TYPE_CADET_AX:
2192 const struct GNUNET_CADET_AX *emsg;
2194 GNUNET_STATISTICS_update (stats, "# received Axolotl", 1, GNUNET_NO);
2195 emsg = (const struct GNUNET_CADET_AX *) msg;
2196 decrypted_size = t_ax_decrypt_and_validate (t, cbuf, emsg, size);
2200 GNUNET_break_op (0);
2204 if (-1 == decrypted_size)
2206 GNUNET_STATISTICS_update (stats, "# unable to decrypt", 1, GNUNET_NO);
2207 if (CADET_TUNNEL_KEY_PING <= t->estate)
2209 GNUNET_break_op (0);
2210 LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong crypto, tunnel %s\n", GCT_2s (t));
2211 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
2215 GCT_change_estate (t, CADET_TUNNEL_KEY_OK);
2217 /* FIXME: this is bad, as the structs returned from
2218 this loop may be unaligned, see util's MST for
2219 how to do this right. */
2221 while (off + sizeof (struct GNUNET_MessageHeader) <= decrypted_size)
2225 msgh = (const struct GNUNET_MessageHeader *) &cbuf[off];
2226 msize = ntohs (msgh->size);
2227 if (msize < sizeof (struct GNUNET_MessageHeader))
2229 GNUNET_break_op (0);
2232 if (off + msize < decrypted_size)
2234 GNUNET_break_op (0);
2237 handle_decrypted (t, msgh, GNUNET_SYSERR);
2244 * Demultiplex an encapsulated KX message by message type.
2246 * @param t Tunnel on which the message came.
2247 * @param message Payload of KX message.
2249 * FIXME: not needed anymore
2250 * - substitute with call to kx_ax
2251 * - eliminate encapsulation
2254 GCT_handle_kx (struct CadetTunnel *t,
2255 const struct GNUNET_MessageHeader *message)
2260 type = ntohs (message->type);
2261 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received: %s\n", GC_m2s (type));
2262 sprintf (buf, "# received KX of type %hu (%s)", type, GC_m2s (type));
2263 GNUNET_STATISTICS_update (stats, buf, 1, GNUNET_NO);
2266 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
2267 handle_kx_ax (t, (const struct GNUNET_CADET_AX_KX *) message);
2270 GNUNET_break_op (0);
2271 LOG (GNUNET_ERROR_TYPE_WARNING, "kx message %s unknown\n", GC_m2s (type));
2276 * Initialize the tunnel subsystem.
2278 * @param c Configuration handle.
2279 * @param key ECC private key, to derive all other keys and do crypto.
2282 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
2283 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
2285 unsigned int expected_overhead;
2287 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2289 expected_overhead = 0;
2290 expected_overhead += sizeof (struct GNUNET_CADET_AX);
2291 expected_overhead += sizeof (struct GNUNET_CADET_Data);
2292 expected_overhead += sizeof (struct GNUNET_CADET_ACK);
2293 GNUNET_assert (GNUNET_CONSTANTS_CADET_P2P_OVERHEAD == expected_overhead);
2296 GNUNET_CONFIGURATION_get_value_number (c,
2301 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
2305 ratchet_messages = 64;
2308 GNUNET_CONFIGURATION_get_value_time (c,
2313 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
2314 "CADET", "RATCHET_TIME", "USING DEFAULT");
2315 ratchet_time = GNUNET_TIME_UNIT_HOURS;
2320 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
2325 * Shut down the tunnel subsystem.
2330 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down tunnels\n");
2331 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
2332 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
2339 * @param destination Peer this tunnel is towards.
2341 struct CadetTunnel *
2342 GCT_new (struct CadetPeer *destination)
2344 struct CadetTunnel *t;
2346 t = GNUNET_new (struct CadetTunnel);
2348 t->peer = destination;
2351 GNUNET_CONTAINER_multipeermap_put (tunnels, GCP_get_id (destination), t,
2352 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2358 t->ax = GNUNET_new (struct CadetTunnelAxolotl);
2360 t->ax->kx_0 = GNUNET_CRYPTO_ecdhe_key_create ();
2366 * Change the tunnel's connection state.
2368 * @param t Tunnel whose connection state to change.
2369 * @param cstate New connection state.
2372 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
2376 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s cstate %s => %s\n",
2377 GCP_2s (t->peer), cstate2s (t->cstate), cstate2s (cstate));
2378 if (myid != GCP_get_short_id (t->peer) &&
2379 CADET_TUNNEL_READY != t->cstate &&
2380 CADET_TUNNEL_READY == cstate)
2383 if (CADET_TUNNEL_KEY_OK == t->estate)
2385 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered send queued data\n");
2386 send_queued_data (t);
2388 else if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
2390 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered kx\n");
2391 GCT_send_ax_kx (t, GNUNET_NO);
2395 LOG (GNUNET_ERROR_TYPE_DEBUG, "estate %s\n", estate2s (t->estate));
2400 if (CADET_TUNNEL_READY == cstate
2401 && CONNECTIONS_PER_TUNNEL <= GCT_count_connections (t))
2403 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered stop dht\n");
2404 GCP_stop_search (t->peer);
2410 * Change the tunnel encryption state.
2412 * If the encryption state changes to OK, stop the rekey task.
2414 * @param t Tunnel whose encryption state to change, or NULL.
2415 * @param state New encryption state.
2418 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
2420 enum CadetTunnelEState old;
2427 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s estate was %s\n",
2428 GCP_2s (t->peer), estate2s (old));
2429 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s estate is now %s\n",
2430 GCP_2s (t->peer), estate2s (t->estate));
2432 if (CADET_TUNNEL_KEY_OK != old && CADET_TUNNEL_KEY_OK == t->estate)
2434 if (NULL != t->rekey_task)
2436 GNUNET_SCHEDULER_cancel (t->rekey_task);
2437 t->rekey_task = NULL;
2439 /* Send queued data if tunnel is not loopback */
2440 if (myid != GCP_get_short_id (t->peer))
2441 send_queued_data (t);
2447 * @brief Check if tunnel has too many connections, and remove one if necessary.
2449 * Currently this means the newest connection, unless it is a direct one.
2450 * Implemented as a task to avoid freeing a connection that is in the middle
2451 * of being created/processed.
2453 * @param cls Closure (Tunnel to check).
2456 trim_connections (void *cls)
2458 struct CadetTunnel *t = cls;
2460 t->trim_connections_task = NULL;
2461 if (GCT_count_connections (t) > 2 * CONNECTIONS_PER_TUNNEL)
2463 struct CadetTConnection *iter;
2464 struct CadetTConnection *c;
2466 for (c = iter = t->connection_head; NULL != iter; iter = iter->next)
2468 if ((iter->created.abs_value_us > c->created.abs_value_us)
2469 && GNUNET_NO == GCC_is_direct (iter->c))
2476 LOG (GNUNET_ERROR_TYPE_DEBUG, "Too many connections on tunnel %s\n",
2478 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying connection %s\n",
2491 * Add a connection to a tunnel.
2494 * @param c Connection.
2497 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c)
2499 struct CadetTConnection *aux;
2501 GNUNET_assert (NULL != c);
2503 LOG (GNUNET_ERROR_TYPE_DEBUG, "add connection %s\n", GCC_2s (c));
2504 LOG (GNUNET_ERROR_TYPE_DEBUG, " to tunnel %s\n", GCT_2s (t));
2505 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2509 aux = GNUNET_new (struct CadetTConnection);
2511 aux->created = GNUNET_TIME_absolute_get ();
2513 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, aux);
2515 if (CADET_TUNNEL_SEARCHING == t->cstate)
2516 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
2518 if (NULL != t->trim_connections_task)
2519 t->trim_connections_task = GNUNET_SCHEDULER_add_now (&trim_connections, t);
2524 * Remove a connection from a tunnel.
2527 * @param c Connection.
2530 GCT_remove_connection (struct CadetTunnel *t,
2531 struct CadetConnection *c)
2533 struct CadetTConnection *aux;
2534 struct CadetTConnection *next;
2537 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing connection %s from tunnel %s\n",
2538 GCC_2s (c), GCT_2s (t));
2539 for (aux = t->connection_head; aux != NULL; aux = next)
2544 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
2549 conns = GCT_count_connections (t);
2551 && NULL == t->destroy_task
2552 && CADET_TUNNEL_SHUTDOWN != t->cstate
2553 && GNUNET_NO == shutting_down)
2555 if (0 == GCT_count_any_connections (t))
2556 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2558 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
2561 /* Start new connections if needed */
2562 if (CONNECTIONS_PER_TUNNEL > conns
2563 && CADET_TUNNEL_SHUTDOWN != t->cstate
2564 && GNUNET_NO == shutting_down)
2566 LOG (GNUNET_ERROR_TYPE_DEBUG, " too few connections, getting new ones\n");
2567 GCP_connect (t->peer); /* Will change cstate to WAITING when possible */
2571 /* If not marked as ready, no change is needed */
2572 if (CADET_TUNNEL_READY != t->cstate)
2575 /* Check if any connection is ready to maintain cstate */
2576 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2577 if (CADET_CONNECTION_READY == GCC_get_state (aux->c))
2583 * Add a channel to a tunnel.
2586 * @param ch Channel.
2589 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2591 struct CadetTChannel *aux;
2593 GNUNET_assert (NULL != ch);
2595 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
2597 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2599 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
2604 aux = GNUNET_new (struct CadetTChannel);
2606 LOG (GNUNET_ERROR_TYPE_DEBUG,
2607 " adding %p to %p\n", aux, t->channel_head);
2608 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head,
2612 if (NULL != t->destroy_task)
2614 GNUNET_SCHEDULER_cancel (t->destroy_task);
2615 t->destroy_task = NULL;
2616 LOG (GNUNET_ERROR_TYPE_DEBUG, " undo destroy!\n");
2622 * Remove a channel from a tunnel.
2625 * @param ch Channel.
2628 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2630 struct CadetTChannel *aux;
2632 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
2633 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2637 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GCCH_2s (ch));
2638 GNUNET_CONTAINER_DLL_remove (t->channel_head,
2649 * Search for a channel by global ID.
2651 * @param t Tunnel containing the channel.
2652 * @param chid Public channel number.
2654 * @return channel handler, NULL if doesn't exist
2656 struct CadetChannel *
2657 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
2659 struct CadetTChannel *iter;
2664 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2666 if (GCCH_get_id (iter->ch) == chid)
2670 return NULL == iter ? NULL : iter->ch;
2675 * @brief Destroy a tunnel and free all resources.
2677 * Should only be called a while after the tunnel has been marked as destroyed,
2678 * in case there is a new channel added to the same peer shortly after marking
2679 * the tunnel. This way we avoid a new public key handshake.
2681 * @param cls Closure (tunnel to destroy).
2684 delayed_destroy (void *cls)
2686 struct CadetTunnel *t = cls;
2687 struct CadetTConnection *iter;
2689 t->destroy_task = NULL;
2690 LOG (GNUNET_ERROR_TYPE_DEBUG,
2691 "delayed destroying tunnel %p\n",
2693 t->cstate = CADET_TUNNEL_SHUTDOWN;
2694 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2696 GCC_send_destroy (iter->c);
2703 * Tunnel is empty: destroy it.
2705 * Notifies all connections about the destruction.
2707 * @param t Tunnel to destroy.
2710 GCT_destroy_empty (struct CadetTunnel *t)
2712 if (GNUNET_YES == shutting_down)
2713 return; /* Will be destroyed immediately anyway */
2715 if (NULL != t->destroy_task)
2717 LOG (GNUNET_ERROR_TYPE_WARNING,
2718 "Tunnel %s is already scheduled for destruction. Tunnel debug dump:\n",
2720 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
2722 /* should never happen, tunnel can only become empty once, and the
2723 * task identifier should be NO_TASK (cleaned when the tunnel was created
2724 * or became un-empty)
2729 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s empty: scheduling destruction\n",
2732 // FIXME make delay a config option
2733 t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
2734 &delayed_destroy, t);
2735 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %p\n",
2736 t, t->destroy_task);
2741 * Destroy tunnel if empty (no more channels).
2743 * @param t Tunnel to destroy if empty.
2746 GCT_destroy_if_empty (struct CadetTunnel *t)
2748 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s destroy if empty\n", GCT_2s (t));
2749 if (0 < GCT_count_channels (t))
2752 GCT_destroy_empty (t);
2757 * Destroy the tunnel.
2759 * This function does not generate any warning traffic to clients or peers.
2762 * Cancel messages belonging to this tunnel queued to neighbors.
2763 * Free any allocated resources linked to the tunnel.
2765 * @param t The tunnel to destroy.
2768 GCT_destroy (struct CadetTunnel *t)
2770 struct CadetTConnection *iter_c;
2771 struct CadetTConnection *next_c;
2772 struct CadetTChannel *iter_ch;
2773 struct CadetTChannel *next_ch;
2774 unsigned int keepalives_queued;
2779 LOG (GNUNET_ERROR_TYPE_DEBUG,
2780 "destroying tunnel %s\n",
2782 GNUNET_break (GNUNET_YES ==
2783 GNUNET_CONTAINER_multipeermap_remove (tunnels,
2784 GCP_get_id (t->peer), t));
2786 for (iter_c = t->connection_head; NULL != iter_c; iter_c = next_c)
2788 next_c = iter_c->next;
2789 GCC_destroy (iter_c->c);
2791 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = next_ch)
2793 next_ch = iter_ch->next;
2794 GCCH_destroy (iter_ch->ch);
2795 /* Should only happen on shutdown, but it's ok. */
2797 keepalives_queued = 0;
2798 while (NULL != t->tq_head)
2800 /* Should have been cleaned by destuction of channel. */
2801 struct GNUNET_MessageHeader *mh;
2804 mh = (struct GNUNET_MessageHeader *) &t->tq_head[1];
2805 type = ntohs (mh->type);
2806 if (0 == keepalives_queued && GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE == type)
2808 keepalives_queued = 1;
2809 LOG (GNUNET_ERROR_TYPE_DEBUG,
2810 "one keepalive left behind on tunnel shutdown\n");
2812 else if (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY == type)
2814 LOG (GNUNET_ERROR_TYPE_WARNING,
2815 "tunnel destroyed before a CHANNEL_DESTROY was sent to peer\n");
2820 LOG (GNUNET_ERROR_TYPE_ERROR,
2821 "message left behind on tunnel shutdown: %s\n",
2824 unqueue_data (t->tq_head);
2828 if (NULL != t->destroy_task)
2830 LOG (GNUNET_ERROR_TYPE_DEBUG,
2831 "cancelling dest: %p\n",
2833 GNUNET_SCHEDULER_cancel (t->destroy_task);
2834 t->destroy_task = NULL;
2837 if (NULL != t->trim_connections_task)
2839 LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling trim: %p\n",
2840 t->trim_connections_task);
2841 GNUNET_SCHEDULER_cancel (t->trim_connections_task);
2842 t->trim_connections_task = NULL;
2845 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2846 GCP_set_tunnel (t->peer, NULL);
2848 if (NULL != t->rekey_task)
2850 GNUNET_SCHEDULER_cancel (t->rekey_task);
2851 t->rekey_task = NULL;
2861 * @brief Use the given path for the tunnel.
2862 * Update the next and prev hops (and RCs).
2863 * (Re)start the path refresh in case the tunnel is locally owned.
2865 * @param t Tunnel to update.
2866 * @param p Path to use.
2868 * @return Connection created.
2870 struct CadetConnection *
2871 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *path)
2873 struct CadetConnection *c;
2874 struct GNUNET_CADET_Hash cid;
2875 unsigned int own_pos;
2877 if (NULL == t || NULL == path)
2883 if (CADET_TUNNEL_SHUTDOWN == t->cstate)
2889 for (own_pos = 0; own_pos < path->length; own_pos++)
2891 if (path->peers[own_pos] == myid)
2894 if (own_pos >= path->length)
2896 GNUNET_break_op (0);
2900 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
2901 c = GCC_new (&cid, t, path, own_pos);
2904 /* Path was flawed */
2907 GCT_add_connection (t, c);
2913 * Count all created connections of a tunnel. Not necessarily ready connections!
2915 * @param t Tunnel on which to count.
2917 * @return Number of connections created, either being established or ready.
2920 GCT_count_any_connections (struct CadetTunnel *t)
2922 struct CadetTConnection *iter;
2928 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2936 * Count established (ready) connections of a tunnel.
2938 * @param t Tunnel on which to count.
2940 * @return Number of connections.
2943 GCT_count_connections (struct CadetTunnel *t)
2945 struct CadetTConnection *iter;
2951 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2952 if (CADET_CONNECTION_READY == GCC_get_state (iter->c))
2960 * Count channels of a tunnel.
2962 * @param t Tunnel on which to count.
2964 * @return Number of channels.
2967 GCT_count_channels (struct CadetTunnel *t)
2969 struct CadetTChannel *iter;
2972 for (count = 0, iter = t->channel_head;
2974 iter = iter->next, count++) /* skip */;
2981 * Get the connectivity state of a tunnel.
2985 * @return Tunnel's connectivity state.
2987 enum CadetTunnelCState
2988 GCT_get_cstate (struct CadetTunnel *t)
2993 return (enum CadetTunnelCState) -1;
3000 * Get the encryption state of a tunnel.
3004 * @return Tunnel's encryption state.
3006 enum CadetTunnelEState
3007 GCT_get_estate (struct CadetTunnel *t)
3012 return (enum CadetTunnelEState) -1;
3018 * Get the maximum buffer space for a tunnel towards a local client.
3022 * @return Biggest buffer space offered by any channel in the tunnel.
3025 GCT_get_channels_buffer (struct CadetTunnel *t)
3027 struct CadetTChannel *iter;
3028 unsigned int buffer;
3029 unsigned int ch_buf;
3031 if (NULL == t->channel_head)
3033 /* Probably getting buffer for a channel create/handshake. */
3034 LOG (GNUNET_ERROR_TYPE_DEBUG, " no channels, allow max\n");
3035 return MIN_TUNNEL_BUFFER;
3039 for (iter = t->channel_head; NULL != iter; iter = iter->next)
3041 ch_buf = get_channel_buffer (iter);
3042 if (ch_buf > buffer)
3045 if (MIN_TUNNEL_BUFFER > buffer)
3046 return MIN_TUNNEL_BUFFER;
3048 if (MAX_TUNNEL_BUFFER < buffer)
3051 return MAX_TUNNEL_BUFFER;
3058 * Get the total buffer space for a tunnel for P2P traffic.
3062 * @return Buffer space offered by all connections in the tunnel.
3065 GCT_get_connections_buffer (struct CadetTunnel *t)
3067 struct CadetTConnection *iter;
3068 unsigned int buffer;
3070 if (GNUNET_NO == is_ready (t))
3072 if (count_queued_data (t) >= 3)
3079 for (iter = t->connection_head; NULL != iter; iter = iter->next)
3081 if (GCC_get_state (iter->c) != CADET_CONNECTION_READY)
3085 buffer += get_connection_buffer (iter);
3093 * Get the tunnel's destination.
3097 * @return ID of the destination peer.
3099 const struct GNUNET_PeerIdentity *
3100 GCT_get_destination (struct CadetTunnel *t)
3102 return GCP_get_id (t->peer);
3107 * Get the tunnel's next free global channel ID.
3111 * @return GID of a channel free to use.
3114 GCT_get_next_chid (struct CadetTunnel *t)
3116 CADET_ChannelNumber chid;
3117 CADET_ChannelNumber mask;
3120 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
3121 * If our ID is bigger or loopback tunnel, start at 0, bit 30 = 0
3122 * If peer's ID is bigger, start at 0x4... bit 30 = 1
3124 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (t->peer));
3129 t->next_chid |= mask;
3131 while (NULL != GCT_get_channel (t, t->next_chid))
3133 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
3134 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
3135 t->next_chid |= mask;
3137 chid = t->next_chid;
3138 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
3139 t->next_chid |= mask;
3146 * Send ACK on one or more channels due to buffer in connections.
3148 * @param t Channel which has some free buffer space.
3151 GCT_unchoke_channels (struct CadetTunnel *t)
3153 struct CadetTChannel *iter;
3154 unsigned int buffer;
3155 unsigned int channels = GCT_count_channels (t);
3156 unsigned int choked_n;
3157 struct CadetChannel *choked[channels];
3159 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_unchoke_channels on %s\n", GCT_2s (t));
3160 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
3161 if (NULL != t->channel_head)
3162 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
3164 if (NULL != t->tq_head)
3165 send_queued_data (t);
3167 /* Get buffer space */
3168 buffer = GCT_get_connections_buffer (t);
3174 /* Count and remember choked channels */
3176 for (iter = t->channel_head; NULL != iter; iter = iter->next)
3178 if (GNUNET_NO == get_channel_allowed (iter))
3180 choked[choked_n++] = iter->ch;
3184 /* Unchoke random channels */
3185 while (0 < buffer && 0 < choked_n)
3187 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3189 GCCH_allow_client (choked[r], GCCH_is_origin (choked[r], GNUNET_YES));
3192 choked[r] = choked[choked_n];
3198 * Send ACK on one or more connections due to buffer space to the client.
3200 * Iterates all connections of the tunnel and sends ACKs appropriately.
3205 GCT_send_connection_acks (struct CadetTunnel *t)
3207 struct CadetTConnection *iter;
3210 uint32_t allow_per_connection;
3212 unsigned int buffer;
3214 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel send connection ACKs on %s\n",
3223 if (CADET_TUNNEL_READY != t->cstate)
3226 buffer = GCT_get_channels_buffer (t);
3227 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer %u\n", buffer);
3229 /* Count connections, how many messages are already allowed */
3230 cs = GCT_count_connections (t);
3231 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
3233 allowed += get_connection_allowed (iter);
3235 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowed %u\n", allowed);
3237 /* Make sure there is no overflow */
3238 if (allowed > buffer)
3241 /* Authorize connections to send more data */
3242 to_allow = buffer - allowed;
3244 for (iter = t->connection_head;
3245 NULL != iter && to_allow > 0;
3248 if (CADET_CONNECTION_READY != GCC_get_state (iter->c)
3249 || get_connection_allowed (iter) > 64 / 3)
3253 GNUNET_assert(cs != 0);
3254 allow_per_connection = to_allow/cs;
3255 to_allow -= allow_per_connection;
3257 GCC_allow (iter->c, allow_per_connection,
3258 GCC_is_origin (iter->c, GNUNET_NO));
3263 /* Since we don't allow if it's allowed to send 64/3, this can happen. */
3264 LOG (GNUNET_ERROR_TYPE_DEBUG, " reminding to_allow: %u\n", to_allow);
3270 * Cancel a previously sent message while it's in the queue.
3272 * ONLY can be called before the continuation given to the send function
3273 * is called. Once the continuation is called, the message is no longer in the
3276 * @param q Handle to the queue.
3279 GCT_cancel (struct CadetTunnelQueue *q)
3283 GNUNET_assert (NULL == q->tqd);
3285 /* tun_message_sent() will be called and free q */
3287 else if (NULL != q->tqd)
3289 unqueue_data (q->tqd);
3291 if (NULL != q->cont)
3292 q->cont (q->cont_cls, NULL, q, 0, 0);
3303 * Check if the tunnel has queued traffic.
3305 * @param t Tunnel to check.
3307 * @return #GNUNET_YES if there is queued traffic
3308 * #GNUNET_NO otherwise
3311 GCT_has_queued_traffic (struct CadetTunnel *t)
3313 return (NULL != t->tq_head) ? GNUNET_YES : GNUNET_NO;
3318 * Sends an already built message on a tunnel, encrypting it and
3319 * choosing the best connection if not provided.
3321 * @param message Message to send. Function modifies it.
3322 * @param t Tunnel on which this message is transmitted.
3323 * @param c Connection to use (autoselect if NULL).
3324 * @param force Force the tunnel to take the message (buffer overfill).
3325 * @param cont Continuation to call once message is really sent.
3326 * @param cont_cls Closure for @c cont.
3328 * @return Handle to cancel message. NULL if @c cont is NULL.
3330 struct CadetTunnelQueue *
3331 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3332 struct CadetTunnel *t, struct CadetConnection *c,
3333 int force, GCT_sent cont, void *cont_cls)
3335 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
3340 * Send an Axolotl KX message.
3342 * @param t Tunnel on which to send it.
3343 * @param force_reply Force the other peer to reply with a KX message.
3346 GCT_send_ax_kx (struct CadetTunnel *t, int force_reply)
3348 struct GNUNET_CADET_AX_KX msg;
3349 enum GNUNET_CADET_AX_KX_Flags flags;
3351 LOG (GNUNET_ERROR_TYPE_INFO, "==> { AX_KX} on %s\n", GCT_2s (t));
3352 if (NULL != t->ephm_h)
3354 LOG (GNUNET_ERROR_TYPE_INFO, " already queued\n");
3358 msg.header.size = htons (sizeof (msg));
3359 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_AX_KX);
3360 flags = GNUNET_CADET_AX_KX_FLAG_NONE;
3362 flags |= GNUNET_CADET_AX_KX_FLAG_FORCE_REPLY;
3363 msg.flags = htonl (flags);
3364 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->kx_0, &msg.ephemeral_key);
3365 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &msg.ratchet_key);
3367 t->ephm_h = send_kx (t, &msg.header);
3368 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
3369 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
3374 * Is the tunnel directed towards the local peer?
3378 * @return #GNUNET_YES if it is loopback.
3381 GCT_is_loopback (const struct CadetTunnel *t)
3383 return (myid == GCP_get_short_id (t->peer));
3388 * Is the tunnel this path already?
3393 * @return #GNUNET_YES a connection uses this path.
3396 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p)
3398 struct CadetTConnection *iter;
3400 for (iter = t->connection_head; NULL != iter; iter = iter->next)
3401 if (path_equivalent (GCC_get_path (iter->c), p))
3409 * Get a cost of a path for a tunnel considering existing connections.
3412 * @param path Candidate path.
3414 * @return Cost of the path (path length + number of overlapping nodes)
3417 GCT_get_path_cost (const struct CadetTunnel *t,
3418 const struct CadetPeerPath *path)
3420 struct CadetTConnection *iter;
3421 const struct CadetPeerPath *aux;
3422 unsigned int overlap;
3430 GNUNET_assert (NULL != t);
3432 for (i = 0; i < path->length; i++)
3434 for (iter = t->connection_head; NULL != iter; iter = iter->next)
3436 aux = GCC_get_path (iter->c);
3440 for (j = 0; j < aux->length; j++)
3442 if (path->peers[i] == aux->peers[j])
3450 return path->length + overlap;
3455 * Get the static string for the peer this tunnel is directed.
3459 * @return Static string the destination peer's ID.
3462 GCT_2s (const struct CadetTunnel *t)
3467 return GCP_2s (t->peer);
3471 /******************************************************************************/
3472 /***************************** INFO/DEBUG *******************************/
3473 /******************************************************************************/
3476 ax_debug (const struct CadetTunnelAxolotl *ax, enum GNUNET_ErrorType level)
3478 struct GNUNET_CRYPTO_EcdhePublicKey pub;
3479 struct CadetTunnelSkippedKey *iter;
3481 LOG2 (level, "TTT RK \t %s\n",
3482 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->RK));
3484 LOG2 (level, "TTT HKs \t %s\n",
3485 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKs));
3486 LOG2 (level, "TTT HKr \t %s\n",
3487 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->HKr));
3488 LOG2 (level, "TTT NHKs\t %s\n",
3489 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->NHKs));
3490 LOG2 (level, "TTT NHKr\t %s\n",
3491 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->NHKr));
3493 LOG2 (level, "TTT CKs \t %s\n",
3494 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKs));
3495 LOG2 (level, "TTT CKr \t %s\n",
3496 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->CKr));
3498 GNUNET_CRYPTO_ecdhe_key_get_public (ax->DHRs, &pub);
3499 LOG2 (level, "TTT DHRs\t %s\n",
3500 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &pub));
3501 LOG2 (level, "TTT DHRr\t %s\n",
3502 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &ax->DHRr));
3504 LOG2 (level, "TTT Nr\t %u\tNs\t%u\n", ax->Nr, ax->Ns);
3505 LOG2 (level, "TTT PNs\t %u\tSkipped\t%u\n", ax->PNs, ax->skipped);
3506 LOG2 (level, "TTT Ratchet\t%u\n", ax->ratchet_flag);
3508 for (iter = ax->skipped_head; NULL != iter; iter = iter->next)
3510 LOG2 (level, "TTT HK\t %s\n",
3511 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &iter->HK));
3512 LOG2 (level, "TTT MK\t %s\n",
3513 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &iter->MK));
3518 * Log all possible info about the tunnel state.
3520 * @param t Tunnel to debug.
3521 * @param level Debug level to use.
3524 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level)
3526 struct CadetTChannel *iterch;
3527 struct CadetTConnection *iterc;
3530 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3532 __FILE__, __FUNCTION__, __LINE__);
3536 LOG2 (level, "TTT DEBUG TUNNEL TOWARDS %s\n", GCT_2s (t));
3537 LOG2 (level, "TTT cstate %s, estate %s\n",
3538 cstate2s (t->cstate), estate2s (t->estate));
3539 #if DUMP_KEYS_TO_STDERR
3540 if (CADET_Axolotl == t->enc_type)
3542 ax_debug (t->ax, level);
3546 LOG2 (level, "TTT peers EPHM:\t %s\n",
3547 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->peers_ephemeral_key));
3548 LOG2 (level, "TTT ENC key:\t %s\n",
3549 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->e_key));
3550 LOG2 (level, "TTT DEC key:\t %s\n",
3551 GNUNET_i2s ((struct GNUNET_PeerIdentity *) &t->d_key));
3554 LOG2 (level, "TTT tq_head %p, tq_tail %p\n", t->tq_head, t->tq_tail);
3555 LOG2 (level, "TTT destroy %p\n", t->destroy_task);
3557 LOG2 (level, "TTT channels:\n");
3558 for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
3560 GCCH_debug (iterch->ch, level);
3563 LOG2 (level, "TTT connections:\n");
3564 for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
3566 GCC_debug (iterc->c, level);
3569 LOG2 (level, "TTT DEBUG TUNNEL END\n");
3574 * Iterate all tunnels.
3576 * @param iter Iterator.
3577 * @param cls Closure for @c iter.
3580 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
3582 GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
3587 * Count all tunnels.
3589 * @return Number of tunnels to remote peers kept by this peer.
3592 GCT_count_all (void)
3594 return GNUNET_CONTAINER_multipeermap_size (tunnels);
3599 * Iterate all connections of a tunnel.
3601 * @param t Tunnel whose connections to iterate.
3602 * @param iter Iterator.
3603 * @param cls Closure for @c iter.
3606 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls)
3608 struct CadetTConnection *ct;
3610 for (ct = t->connection_head; NULL != ct; ct = ct->next)
3616 * Iterate all channels of a tunnel.
3618 * @param t Tunnel whose channels to iterate.
3619 * @param iter Iterator.
3620 * @param cls Closure for @c iter.
3623 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls)
3625 struct CadetTChannel *cht;
3627 for (cht = t->channel_head; NULL != cht; cht = cht->next)
3628 iter (cls, cht->ch);