2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file cadet/gnunet-service-cadet_tunnels.c
22 * @brief Information we track per tunnel.
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 * - proper connection evaluation during connection management:
28 * + consider quality (or quality spread?) of current connection set
29 * when deciding how often to do maintenance
30 * + interact with PEER to drive DHT GET/PUT operations based
31 * on how much we like our connections
34 #include "gnunet_util_lib.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_signatures.h"
37 #include "cadet_protocol.h"
38 #include "gnunet-service-cadet_channel.h"
39 #include "gnunet-service-cadet_connection.h"
40 #include "gnunet-service-cadet_tunnels.h"
41 #include "gnunet-service-cadet_peer.h"
42 #include "gnunet-service-cadet_paths.h"
45 #define LOG(level, ...) GNUNET_log_from (level, "cadet-tun", __VA_ARGS__)
48 * How often do we try to decrypt payload with unverified key
49 * material? Used to limit CPU increase upon receiving bogus
52 #define MAX_UNVERIFIED_ATTEMPTS 16
55 * How long do we wait until tearing down an idle tunnel?
57 #define IDLE_DESTROY_DELAY GNUNET_TIME_relative_multiply ( \
58 GNUNET_TIME_UNIT_SECONDS, 90)
61 * How long do we wait initially before retransmitting the KX?
62 * TODO: replace by 2 RTT if/once we have connection-level RTT data!
64 #define INITIAL_KX_RETRY_DELAY GNUNET_TIME_relative_multiply ( \
65 GNUNET_TIME_UNIT_MILLISECONDS, 250)
68 * Maximum number of skipped keys we keep in memory per tunnel.
70 #define MAX_SKIPPED_KEYS 64
73 * Maximum number of keys (and thus ratchet steps) we are willing to
74 * skip before we decide this is either a bogus packet or a DoS-attempt.
76 #define MAX_KEY_GAP 256
80 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
82 struct CadetTunnelSkippedKey
87 struct CadetTunnelSkippedKey *next;
92 struct CadetTunnelSkippedKey *prev;
95 * When was this key stored (for timeout).
97 struct GNUNET_TIME_Absolute timestamp;
102 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
107 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
110 * Key number for a given HK.
117 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
119 struct CadetTunnelAxolotl
122 * A (double linked) list of stored message keys and associated header keys
123 * for "skipped" messages, i.e. messages that have not been
124 * received despite the reception of more recent messages, (head).
126 struct CadetTunnelSkippedKey *skipped_head;
129 * Skipped messages' keys DLL, tail.
131 struct CadetTunnelSkippedKey *skipped_tail;
134 * 32-byte root key which gets updated by DH ratchet.
136 struct GNUNET_CRYPTO_SymmetricSessionKey RK;
139 * 32-byte header key (currently used for sending).
141 struct GNUNET_CRYPTO_SymmetricSessionKey HKs;
144 * 32-byte header key (currently used for receiving)
146 struct GNUNET_CRYPTO_SymmetricSessionKey HKr;
149 * 32-byte next header key (for sending), used once the
150 * ratchet advances. We are sure that the sender has this
151 * key as well only after @e ratchet_allowed is #GNUNET_YES.
153 struct GNUNET_CRYPTO_SymmetricSessionKey NHKs;
156 * 32-byte next header key (for receiving). To be tried
157 * when decrypting with @e HKr fails and thus the sender
158 * may have advanced the ratchet.
160 struct GNUNET_CRYPTO_SymmetricSessionKey NHKr;
163 * 32-byte chain keys (used for forward-secrecy) for
164 * sending messages. Updated for every message.
166 struct GNUNET_CRYPTO_SymmetricSessionKey CKs;
169 * 32-byte chain keys (used for forward-secrecy) for
170 * receiving messages. Updated for every message. If
171 * messages are skipped, the respective derived MKs
172 * (and the current @HKr) are kept in the @e skipped_head DLL.
174 struct GNUNET_CRYPTO_SymmetricSessionKey CKr;
177 * ECDH for key exchange (A0 / B0).
179 struct GNUNET_CRYPTO_EcdhePrivateKey kx_0;
182 * ECDH Ratchet key (our private key in the current DH).
184 struct GNUNET_CRYPTO_EcdhePrivateKey DHRs;
187 * ECDH Ratchet key (other peer's public key in the current DH).
189 struct GNUNET_CRYPTO_EcdhePublicKey DHRr;
192 * Last ephemeral public key received from the other peer,
193 * for duplicate detection.
195 struct GNUNET_CRYPTO_EcdhePublicKey last_ephemeral;
198 * Time when the current ratchet expires and a new one is triggered
199 * (if @e ratchet_allowed is #GNUNET_YES).
201 struct GNUNET_TIME_Absolute ratchet_expiration;
204 * Number of elements in @a skipped_head <-> @a skipped_tail.
206 unsigned int skipped;
209 * Message number (reset to 0 with each new ratchet, next message to send).
214 * Message number (reset to 0 with each new ratchet, next message to recv).
219 * Previous message numbers (# of msgs sent under prev ratchet)
224 * True (#GNUNET_YES) if we have to send a new ratchet key in next msg.
229 * True (#GNUNET_YES) if we have received a message from the
230 * other peer that uses the keys from our last ratchet step.
231 * This implies that we are again allowed to advance the ratchet,
232 * otherwise we have to wait until the other peer sees our current
233 * ephemeral key and advances first.
235 * #GNUNET_NO if we have advanced the ratched but lack any evidence
236 * that the other peer has noticed this.
241 * Number of messages recieved since our last ratchet advance.
243 * If this counter = 0, we cannot send a new ratchet key in the next
246 * If this counter > 0, we could (but don't have to) send a new key.
248 * Once the @e ratchet_counter is larger than
249 * #ratchet_messages (or @e ratchet_expiration time has past), and
250 * @e ratchet_allowed is #GNUNET_YES, we advance the ratchet.
252 unsigned int ratchet_counter;
257 * Struct used to save messages in a non-ready tunnel to send once connected.
259 struct CadetTunnelQueueEntry
262 * We are entries in a DLL
264 struct CadetTunnelQueueEntry *next;
267 * We are entries in a DLL
269 struct CadetTunnelQueueEntry *prev;
272 * Tunnel these messages belong in.
274 struct CadetTunnel *t;
277 * Continuation to call once sent (on the channel layer).
279 GCT_SendContinuation cont;
282 * Closure for @c cont.
287 * Envelope of message to send follows.
289 struct GNUNET_MQ_Envelope *env;
292 * Where to put the connection identifier into the payload
293 * of the message in @e env once we have it?
295 struct GNUNET_CADET_ConnectionTunnelIdentifier *cid;
300 * Struct containing all information regarding a tunnel to a peer.
305 * Destination of the tunnel.
307 struct CadetPeer *destination;
310 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own
311 * ephemeral key changes.
313 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
316 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
318 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
321 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
323 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
328 struct CadetTunnelAxolotl ax;
331 * Unverified Axolotl info, used only if we got a fresh KX (not a
332 * KX_AUTH) while our end of the tunnel was still up. In this case,
333 * we keep the fresh KX around but do not put it into action until
334 * we got encrypted payload that assures us of the authenticity of
337 struct CadetTunnelAxolotl *unverified_ax;
340 * Task scheduled if there are no more channels using the tunnel.
342 struct GNUNET_SCHEDULER_Task *destroy_task;
345 * Task to trim connections if too many are present.
347 struct GNUNET_SCHEDULER_Task *maintain_connections_task;
350 * Task to send messages from queue (if possible).
352 struct GNUNET_SCHEDULER_Task *send_task;
355 * Task to trigger KX.
357 struct GNUNET_SCHEDULER_Task *kx_task;
360 * Tokenizer for decrypted messages.
362 struct GNUNET_MessageStreamTokenizer *mst;
365 * Dispatcher for decrypted messages only (do NOT use for sending!).
367 struct GNUNET_MQ_Handle *mq;
370 * DLL of ready connections that are actively used to reach the destination peer.
372 struct CadetTConnection *connection_ready_head;
375 * DLL of ready connections that are actively used to reach the destination peer.
377 struct CadetTConnection *connection_ready_tail;
380 * DLL of connections that we maintain that might be used to reach the destination peer.
382 struct CadetTConnection *connection_busy_head;
385 * DLL of connections that we maintain that might be used to reach the destination peer.
387 struct CadetTConnection *connection_busy_tail;
390 * Channels inside this tunnel. Maps
391 * `struct GNUNET_CADET_ChannelTunnelNumber` to a `struct CadetChannel`.
393 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
396 * Channel ID for the next created channel in this tunnel.
398 struct GNUNET_CADET_ChannelTunnelNumber next_ctn;
401 * Queued messages, to transmit once tunnel gets connected.
403 struct CadetTunnelQueueEntry *tq_head;
406 * Queued messages, to transmit once tunnel gets connected.
408 struct CadetTunnelQueueEntry *tq_tail;
411 * Identification of the connection from which we are currently processing
412 * a message. Only valid (non-NULL) during #handle_decrypted() and the
413 * handle-*()-functions called from our @e mq during that function.
415 struct CadetTConnection *current_ct;
418 * How long do we wait until we retry the KX?
420 struct GNUNET_TIME_Relative kx_retry_delay;
423 * When do we try the next KX?
425 struct GNUNET_TIME_Absolute next_kx_attempt;
428 * Number of connections in the @e connection_ready_head DLL.
430 unsigned int num_ready_connections;
433 * Number of connections in the @e connection_busy_head DLL.
435 unsigned int num_busy_connections;
438 * How often have we tried and failed to decrypt a message using
439 * the unverified KX material from @e unverified_ax? Used to
440 * stop trying after #MAX_UNVERIFIED_ATTEMPTS.
442 unsigned int unverified_attempts;
445 * Number of entries in the @e tq_head DLL.
450 * State of the tunnel encryption.
452 enum CadetTunnelEState estate;
455 * Force triggering KX_AUTH independent of @e estate.
457 int kx_auth_requested;
462 * Am I Alice or Betty (some call her Bob), or talking to myself?
464 * @param other the other peer
465 * @return #GNUNET_YES for Alice, #GNUNET_NO for Betty, #GNUNET_SYSERR if talking to myself
468 alice_or_betty (const struct GNUNET_PeerIdentity *other)
470 if (0 > GNUNET_memcmp (&my_full_id,
473 else if (0 < GNUNET_memcmp (&my_full_id,
479 return GNUNET_SYSERR;
485 * Connection @a ct is now unready, clear it's ready flag
486 * and move it from the ready DLL to the busy DLL.
488 * @param ct connection to move to unready status
491 mark_connection_unready (struct CadetTConnection *ct)
493 struct CadetTunnel *t = ct->t;
495 GNUNET_assert (GNUNET_YES == ct->is_ready);
496 GNUNET_CONTAINER_DLL_remove (t->connection_ready_head,
497 t->connection_ready_tail,
499 GNUNET_assert (0 < t->num_ready_connections);
500 t->num_ready_connections--;
501 ct->is_ready = GNUNET_NO;
502 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head,
503 t->connection_busy_tail,
505 t->num_busy_connections++;
510 * Get the static string for the peer this tunnel is directed.
514 * @return Static string the destination peer's ID.
517 GCT_2s (const struct CadetTunnel *t)
522 return "Tunnel(NULL)";
523 GNUNET_snprintf (buf,
526 GNUNET_i2s (GCP_get_id (t->destination)));
532 * Get string description for tunnel encryption state.
534 * @param es Tunnel state.
536 * @return String representation.
539 estate2s (enum CadetTunnelEState es)
545 case CADET_TUNNEL_KEY_UNINITIALIZED:
546 return "CADET_TUNNEL_KEY_UNINITIALIZED";
547 case CADET_TUNNEL_KEY_AX_RECV:
548 return "CADET_TUNNEL_KEY_AX_RECV";
549 case CADET_TUNNEL_KEY_AX_SENT:
550 return "CADET_TUNNEL_KEY_AX_SENT";
551 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
552 return "CADET_TUNNEL_KEY_AX_SENT_AND_RECV";
553 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
554 return "CADET_TUNNEL_KEY_AX_AUTH_SENT";
555 case CADET_TUNNEL_KEY_OK:
556 return "CADET_TUNNEL_KEY_OK";
558 GNUNET_snprintf (buf,
560 "%u (UNKNOWN STATE)",
567 * Return the peer to which this tunnel goes.
570 * @return the destination of the tunnel
573 GCT_get_destination (struct CadetTunnel *t)
575 return t->destination;
580 * Count channels of a tunnel.
582 * @param t Tunnel on which to count.
584 * @return Number of channels.
587 GCT_count_channels (struct CadetTunnel *t)
589 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
594 * Lookup a channel by its @a ctn.
596 * @param t tunnel to look in
597 * @param ctn number of channel to find
598 * @return NULL if channel does not exist
600 struct CadetChannel *
601 lookup_channel (struct CadetTunnel *t,
602 struct GNUNET_CADET_ChannelTunnelNumber ctn)
604 return GNUNET_CONTAINER_multihashmap32_get (t->channels,
610 * Count all created connections of a tunnel. Not necessarily ready connections!
612 * @param t Tunnel on which to count.
614 * @return Number of connections created, either being established or ready.
617 GCT_count_any_connections (const struct CadetTunnel *t)
619 return t->num_ready_connections + t->num_busy_connections;
624 * Find first connection that is ready in the list of
625 * our connections. Picks ready connections round-robin.
627 * @param t tunnel to search
628 * @return NULL if we have no connection that is ready
630 static struct CadetTConnection *
631 get_ready_connection (struct CadetTunnel *t)
633 struct CadetTConnection *hd = t->connection_ready_head;
635 GNUNET_assert ((NULL == hd) ||
636 (GNUNET_YES == hd->is_ready));
642 * Get the encryption state of a tunnel.
646 * @return Tunnel's encryption state.
648 enum CadetTunnelEState
649 GCT_get_estate (struct CadetTunnel *t)
656 * Called when either we have a new connection, or a new message in the
657 * queue, or some existing connection has transmission capacity. Looks
658 * at our message queue and if there is a message, picks a connection
661 * @param cls the `struct CadetTunnel` to process messages on
664 trigger_transmissions (void *cls);
667 /* ************************************** start core crypto ***************************** */
671 * Create a new Axolotl ephemeral (ratchet) key.
673 * @param ax key material to update
676 new_ephemeral (struct CadetTunnelAxolotl *ax)
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 "Creating new ephemeral ratchet key (DHRs)\n");
680 GNUNET_CRYPTO_ecdhe_key_create (&ax->DHRs);
687 * @param plaintext Content to HMAC.
688 * @param size Size of @c plaintext.
689 * @param iv Initialization vector for the message.
690 * @param key Key to use.
691 * @param hmac[out] Destination to store the HMAC.
694 t_hmac (const void *plaintext,
697 const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
698 struct GNUNET_ShortHashCode *hmac)
700 static const char ctx[] = "cadet authentication key";
701 struct GNUNET_CRYPTO_AuthKey auth_key;
702 struct GNUNET_HashCode hash;
704 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
710 /* Two step: GNUNET_ShortHash is only 256 bits,
711 GNUNET_HashCode is 512, so we truncate. */
712 GNUNET_CRYPTO_hmac (&auth_key,
725 * @param key Key to use.
726 * @param[out] hash Resulting HMAC.
727 * @param source Source key material (data to HMAC).
728 * @param len Length of @a source.
731 t_ax_hmac_hash (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
732 struct GNUNET_HashCode *hash,
736 static const char ctx[] = "axolotl HMAC-HASH";
737 struct GNUNET_CRYPTO_AuthKey auth_key;
739 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
743 GNUNET_CRYPTO_hmac (&auth_key,
751 * Derive a symmetric encryption key from an HMAC-HASH.
753 * @param key Key to use for the HMAC.
754 * @param[out] out Key to generate.
755 * @param source Source key material (data to HMAC).
756 * @param len Length of @a source.
759 t_hmac_derive_key (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
760 struct GNUNET_CRYPTO_SymmetricSessionKey *out,
764 static const char ctx[] = "axolotl derive key";
765 struct GNUNET_HashCode h;
771 GNUNET_CRYPTO_kdf (out, sizeof(*out),
779 * Encrypt data with the axolotl tunnel key.
781 * @param ax key material to use.
782 * @param dst Destination with @a size bytes for the encrypted data.
783 * @param src Source of the plaintext. Can overlap with @c dst, must contain @a size bytes
784 * @param size Size of the buffers at @a src and @a dst
787 t_ax_encrypt (struct CadetTunnelAxolotl *ax,
792 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
793 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
796 ax->ratchet_counter++;
797 if ((GNUNET_YES == ax->ratchet_allowed) &&
798 ((ratchet_messages <= ax->ratchet_counter) ||
799 (0 == GNUNET_TIME_absolute_get_remaining (
800 ax->ratchet_expiration).rel_value_us)))
802 ax->ratchet_flag = GNUNET_YES;
804 if (GNUNET_YES == ax->ratchet_flag)
806 /* Advance ratchet */
807 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3];
808 struct GNUNET_HashCode dh;
809 struct GNUNET_HashCode hmac;
810 static const char ctx[] = "axolotl ratchet";
815 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
816 GNUNET_CRYPTO_ecc_ecdh (&ax->DHRs,
819 t_ax_hmac_hash (&ax->RK,
823 GNUNET_CRYPTO_kdf (keys, sizeof(keys),
833 ax->ratchet_flag = GNUNET_NO;
834 ax->ratchet_allowed = GNUNET_NO;
835 ax->ratchet_counter = 0;
836 ax->ratchet_expiration
837 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
841 t_hmac_derive_key (&ax->CKs,
845 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
850 out_size = GNUNET_CRYPTO_symmetric_encrypt (src,
855 GNUNET_assert (size == out_size);
856 t_hmac_derive_key (&ax->CKs,
864 * Decrypt data with the axolotl tunnel key.
866 * @param ax key material to use.
867 * @param dst Destination for the decrypted data, must contain @a size bytes.
868 * @param src Source of the ciphertext. Can overlap with @c dst, must contain @a size bytes.
869 * @param size Size of the @a src and @a dst buffers
872 t_ax_decrypt (struct CadetTunnelAxolotl *ax,
877 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
878 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
881 t_hmac_derive_key (&ax->CKr,
885 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
889 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
890 out_size = GNUNET_CRYPTO_symmetric_decrypt (src,
895 GNUNET_assert (out_size == size);
896 t_hmac_derive_key (&ax->CKr,
904 * Encrypt header with the axolotl header key.
906 * @param ax key material to use.
907 * @param[in|out] msg Message whose header to encrypt.
910 t_h_encrypt (struct CadetTunnelAxolotl *ax,
911 struct GNUNET_CADET_TunnelEncryptedMessage *msg)
913 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
916 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
920 out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->ax_header,
922 GNUNET_CADET_AxHeader),
926 GNUNET_assert (sizeof(struct GNUNET_CADET_AxHeader) == out_size);
931 * Decrypt header with the current axolotl header key.
933 * @param ax key material to use.
934 * @param src Message whose header to decrypt.
935 * @param dst Where to decrypt header to.
938 t_h_decrypt (struct CadetTunnelAxolotl *ax,
939 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
940 struct GNUNET_CADET_TunnelEncryptedMessage *dst)
942 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
945 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
949 out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->ax_header.Ns,
951 GNUNET_CADET_AxHeader),
955 GNUNET_assert (sizeof(struct GNUNET_CADET_AxHeader) == out_size);
960 * Delete a key from the list of skipped keys.
962 * @param ax key material to delete @a key from.
963 * @param key Key to delete.
966 delete_skipped_key (struct CadetTunnelAxolotl *ax,
967 struct CadetTunnelSkippedKey *key)
969 GNUNET_CONTAINER_DLL_remove (ax->skipped_head,
978 * Decrypt and verify data with the appropriate tunnel key and verify that the
979 * data has not been altered since it was sent by the remote peer.
981 * @param ax key material to use.
982 * @param dst Destination for the plaintext.
983 * @param src Source of the message. Can overlap with @c dst.
984 * @param size Size of the message.
985 * @return Size of the decrypted data, -1 if an error was encountered.
988 try_old_ax_keys (struct CadetTunnelAxolotl *ax,
990 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
993 struct CadetTunnelSkippedKey *key;
994 struct GNUNET_ShortHashCode *hmac;
995 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
996 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
997 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Trying skipped keys\n");
1005 hmac = &plaintext_header.hmac;
1006 esize = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1008 /* Find a correct Header Key */
1010 for (key = ax->skipped_head; NULL != key; key = key->next)
1012 t_hmac (&src->ax_header,
1013 sizeof(struct GNUNET_CADET_AxHeader) + esize,
1017 if (0 == GNUNET_memcmp (hmac,
1020 valid_HK = &key->HK;
1027 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
1028 GNUNET_assert (size > sizeof(struct GNUNET_CADET_TunnelEncryptedMessage));
1029 len = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1030 GNUNET_assert (len >= sizeof(struct GNUNET_MessageHeader));
1032 /* Decrypt header */
1033 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
1037 res = GNUNET_CRYPTO_symmetric_decrypt (&src->ax_header.Ns,
1038 sizeof(struct GNUNET_CADET_AxHeader),
1041 &plaintext_header.ax_header.Ns);
1042 GNUNET_assert (sizeof(struct GNUNET_CADET_AxHeader) == res);
1044 /* Find the correct message key */
1045 N = ntohl (plaintext_header.ax_header.Ns);
1046 while ((NULL != key) &&
1049 if ((NULL == key) ||
1050 (0 != GNUNET_memcmp (&key->HK,
1054 /* Decrypt payload */
1055 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
1060 res = GNUNET_CRYPTO_symmetric_decrypt (&src[1],
1065 delete_skipped_key (ax,
1072 * Delete a key from the list of skipped keys.
1074 * @param ax key material to delete from.
1075 * @param HKr Header Key to use.
1078 store_skipped_key (struct CadetTunnelAxolotl *ax,
1079 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
1081 struct CadetTunnelSkippedKey *key;
1083 key = GNUNET_new (struct CadetTunnelSkippedKey);
1084 key->timestamp = GNUNET_TIME_absolute_get ();
1087 t_hmac_derive_key (&ax->CKr,
1091 t_hmac_derive_key (&ax->CKr,
1095 GNUNET_CONTAINER_DLL_insert (ax->skipped_head,
1104 * Stage skipped AX keys and calculate the message key.
1105 * Stores each HK and MK for skipped messages.
1107 * @param ax key material to use
1108 * @param HKr Header key.
1109 * @param Np Received meesage number.
1110 * @return #GNUNET_OK if keys were stored.
1111 * #GNUNET_SYSERR if an error ocurred (@a Np not expected).
1114 store_ax_keys (struct CadetTunnelAxolotl *ax,
1115 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
1121 LOG (GNUNET_ERROR_TYPE_DEBUG,
1122 "Storing skipped keys [%u, %u)\n",
1125 if (MAX_KEY_GAP < gap)
1127 /* Avoid DoS (forcing peer to do more than #MAX_KEY_GAP HMAC operations) */
1128 /* TODO: start new key exchange on return */
1129 GNUNET_break_op (0);
1130 LOG (GNUNET_ERROR_TYPE_WARNING,
1131 "Got message %u, expected %u+\n",
1134 return GNUNET_SYSERR;
1138 /* Delayed message: don't store keys, flag to try old keys. */
1139 return GNUNET_SYSERR;
1143 store_skipped_key (ax,
1146 while (ax->skipped > MAX_SKIPPED_KEYS)
1147 delete_skipped_key (ax,
1154 * Decrypt and verify data with the appropriate tunnel key and verify that the
1155 * data has not been altered since it was sent by the remote peer.
1157 * @param ax key material to use
1158 * @param dst Destination for the plaintext.
1159 * @param src Source of the message. Can overlap with @c dst.
1160 * @param size Size of the message.
1161 * @return Size of the decrypted data, -1 if an error was encountered.
1164 t_ax_decrypt_and_validate (struct CadetTunnelAxolotl *ax,
1167 GNUNET_CADET_TunnelEncryptedMessage *src,
1170 struct GNUNET_ShortHashCode msg_hmac;
1171 struct GNUNET_HashCode hmac;
1172 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1175 size_t esize; /* Size of encryped payload */
1177 esize = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1179 /* Try current HK */
1180 t_hmac (&src->ax_header,
1181 sizeof(struct GNUNET_CADET_AxHeader) + esize,
1184 if (0 != GNUNET_memcmp (&msg_hmac,
1187 static const char ctx[] = "axolotl ratchet";
1188 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1189 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1190 struct GNUNET_HashCode dh;
1191 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1194 t_hmac (&src->ax_header,
1195 sizeof(struct GNUNET_CADET_AxHeader) + esize,
1199 if (0 != GNUNET_memcmp (&msg_hmac,
1202 /* Try the skipped keys, if that fails, we're out of luck. */
1203 return try_old_ax_keys (ax,
1213 Np = ntohl (plaintext_header.ax_header.Ns);
1214 PNp = ntohl (plaintext_header.ax_header.PNs);
1215 DHRp = &plaintext_header.ax_header.DHRs;
1220 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1221 GNUNET_CRYPTO_ecc_ecdh (&ax->DHRs,
1224 t_ax_hmac_hash (&ax->RK,
1227 GNUNET_CRYPTO_kdf (keys, sizeof(keys),
1229 &hmac, sizeof(hmac),
1232 /* Commit "purported" keys */
1238 ax->ratchet_allowed = GNUNET_YES;
1245 Np = ntohl (plaintext_header.ax_header.Ns);
1246 PNp = ntohl (plaintext_header.ax_header.PNs);
1248 if ((Np != ax->Nr) &&
1249 (GNUNET_OK != store_ax_keys (ax,
1253 /* Try the skipped keys, if that fails, we're out of luck. */
1254 return try_old_ax_keys (ax,
1270 * Our tunnel became ready for the first time, notify channels
1271 * that have been waiting.
1273 * @param cls our tunnel, not used
1274 * @param key unique ID of the channel, not used
1275 * @param value the `struct CadetChannel` to notify
1276 * @return #GNUNET_OK (continue to iterate)
1279 notify_tunnel_up_cb (void *cls,
1283 struct CadetChannel *ch = value;
1285 GCCH_tunnel_up (ch);
1291 * Change the tunnel encryption state.
1292 * If the encryption state changes to OK, stop the rekey task.
1294 * @param t Tunnel whose encryption state to change, or NULL.
1295 * @param state New encryption state.
1298 GCT_change_estate (struct CadetTunnel *t,
1299 enum CadetTunnelEState state)
1301 enum CadetTunnelEState old = t->estate;
1304 LOG (GNUNET_ERROR_TYPE_DEBUG,
1305 "%s estate changed from %s to %s\n",
1310 if ((CADET_TUNNEL_KEY_OK != old) &&
1311 (CADET_TUNNEL_KEY_OK == t->estate))
1313 if (NULL != t->kx_task)
1315 GNUNET_SCHEDULER_cancel (t->kx_task);
1318 /* notify all channels that have been waiting */
1319 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
1320 ¬ify_tunnel_up_cb,
1322 if (NULL != t->send_task)
1323 GNUNET_SCHEDULER_cancel (t->send_task);
1324 t->send_task = GNUNET_SCHEDULER_add_now (&trigger_transmissions,
1331 * Send a KX message.
1333 * @param t tunnel on which to send the KX_AUTH
1334 * @param ct Tunnel and connection on which to send the KX_AUTH, NULL if
1335 * we are to find one that is ready.
1336 * @param ax axolotl key context to use
1339 send_kx (struct CadetTunnel *t,
1340 struct CadetTConnection *ct,
1341 struct CadetTunnelAxolotl *ax)
1343 struct CadetConnection *cc;
1344 struct GNUNET_MQ_Envelope *env;
1345 struct GNUNET_CADET_TunnelKeyExchangeMessage *msg;
1346 enum GNUNET_CADET_KX_Flags flags;
1348 if (GNUNET_YES != alice_or_betty (GCP_get_id (t->destination)))
1349 return; /* only Alice may send KX */
1351 (GNUNET_NO == ct->is_ready))
1352 ct = get_ready_connection (t);
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "Wanted to send %s in state %s, but no connection is ready, deferring\n",
1358 estate2s (t->estate));
1359 t->next_kx_attempt = GNUNET_TIME_absolute_get ();
1363 env = GNUNET_MQ_msg (msg,
1364 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
1365 flags = GNUNET_CADET_KX_FLAG_FORCE_REPLY; /* always for KX */
1366 msg->flags = htonl (flags);
1367 msg->cid = *GCC_get_id (cc);
1368 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->kx_0,
1369 &msg->ephemeral_key);
1371 msg->ephemeral_key_XXX = ax->kx_0;
1372 msg->private_key_XXX = *my_private_key;
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 "Sending KX message to %s with ephemeral %s on CID %s\n",
1377 GNUNET_e2s (&msg->ephemeral_key),
1378 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
1379 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->DHRs,
1381 mark_connection_unready (ct);
1382 t->kx_retry_delay = GNUNET_TIME_STD_BACKOFF (t->kx_retry_delay);
1383 t->next_kx_attempt = GNUNET_TIME_relative_to_absolute (t->kx_retry_delay);
1384 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1385 GCT_change_estate (t,
1386 CADET_TUNNEL_KEY_AX_SENT);
1387 else if (CADET_TUNNEL_KEY_AX_RECV == t->estate)
1388 GCT_change_estate (t,
1389 CADET_TUNNEL_KEY_AX_SENT_AND_RECV);
1392 GNUNET_STATISTICS_update (stats,
1400 * Send a KX_AUTH message.
1402 * @param t tunnel on which to send the KX_AUTH
1403 * @param ct Tunnel and connection on which to send the KX_AUTH, NULL if
1404 * we are to find one that is ready.
1405 * @param ax axolotl key context to use
1406 * @param force_reply Force the other peer to reply with a KX_AUTH message
1407 * (set if we would like to transmit right now, but cannot)
1410 send_kx_auth (struct CadetTunnel *t,
1411 struct CadetTConnection *ct,
1412 struct CadetTunnelAxolotl *ax,
1415 struct CadetConnection *cc;
1416 struct GNUNET_MQ_Envelope *env;
1417 struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg;
1418 enum GNUNET_CADET_KX_Flags flags;
1421 (GNUNET_NO == ct->is_ready))
1422 ct = get_ready_connection (t);
1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1426 "Wanted to send KX_AUTH on %s, but no connection is ready, deferring\n",
1428 t->next_kx_attempt = GNUNET_TIME_absolute_get ();
1429 t->kx_auth_requested = GNUNET_YES; /* queue KX_AUTH independent of estate */
1432 t->kx_auth_requested = GNUNET_NO; /* clear flag */
1434 env = GNUNET_MQ_msg (msg,
1435 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH);
1436 flags = GNUNET_CADET_KX_FLAG_NONE;
1437 if (GNUNET_YES == force_reply)
1438 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
1439 msg->kx.flags = htonl (flags);
1440 msg->kx.cid = *GCC_get_id (cc);
1441 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->kx_0,
1442 &msg->kx.ephemeral_key);
1443 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->DHRs,
1444 &msg->kx.ratchet_key);
1446 msg->kx.ephemeral_key_XXX = ax->kx_0;
1447 msg->kx.private_key_XXX = *my_private_key;
1448 msg->r_ephemeral_key_XXX = ax->last_ephemeral;
1450 LOG (GNUNET_ERROR_TYPE_DEBUG,
1451 "Sending KX_AUTH message to %s with ephemeral %s on CID %s\n",
1453 GNUNET_e2s (&msg->kx.ephemeral_key),
1454 GNUNET_sh2s (&msg->kx.cid.connection_of_tunnel));
1456 /* Compute authenticator (this is the main difference to #send_kx()) */
1457 GNUNET_CRYPTO_hash (&ax->RK,
1460 /* Compute when to be triggered again; actual job will
1461 be scheduled via #connection_ready_cb() */
1463 = GNUNET_TIME_STD_BACKOFF (t->kx_retry_delay);
1465 = GNUNET_TIME_relative_to_absolute (t->kx_retry_delay);
1467 /* Send via cc, mark it as unready */
1468 mark_connection_unready (ct);
1470 /* Update state machine, unless we are already OK */
1471 if (CADET_TUNNEL_KEY_OK != t->estate)
1472 GCT_change_estate (t,
1473 CADET_TUNNEL_KEY_AX_AUTH_SENT);
1476 GNUNET_STATISTICS_update (stats,
1477 "# KX_AUTH transmitted",
1484 * Cleanup state used by @a ax.
1486 * @param ax state to free, but not memory of @a ax itself
1489 cleanup_ax (struct CadetTunnelAxolotl *ax)
1491 while (NULL != ax->skipped_head)
1492 delete_skipped_key (ax,
1494 GNUNET_assert (0 == ax->skipped);
1495 GNUNET_CRYPTO_ecdhe_key_clear (&ax->kx_0);
1496 GNUNET_CRYPTO_ecdhe_key_clear (&ax->DHRs);
1501 * Update our Axolotl key state based on the KX data we received.
1502 * Computes the new chain keys, and root keys, etc, and also checks
1503 * whether this is a replay of the current chain.
1505 * @param[in|out] axolotl chain key state to recompute
1506 * @param pid peer identity of the other peer
1507 * @param ephemeral_key ephemeral public key of the other peer
1508 * @param ratchet_key senders next ephemeral public key
1509 * @return #GNUNET_OK on success, #GNUNET_NO if the resulting
1510 * root key is already in @a ax and thus the KX is useless;
1511 * #GNUNET_SYSERR on hard errors (i.e. @a pid is #my_full_id)
1514 update_ax_by_kx (struct CadetTunnelAxolotl *ax,
1515 const struct GNUNET_PeerIdentity *pid,
1516 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key,
1517 const struct GNUNET_CRYPTO_EcdhePublicKey *ratchet_key)
1519 struct GNUNET_HashCode key_material[3];
1520 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
1521 const char salt[] = "CADET Axolotl salt";
1524 if (GNUNET_SYSERR == (am_I_alice = alice_or_betty (pid)))
1526 GNUNET_break_op (0);
1527 return GNUNET_SYSERR;
1529 if (0 == GNUNET_memcmp (&ax->DHRr,
1532 GNUNET_STATISTICS_update (stats,
1533 "# Ratchet key already known",
1536 LOG (GNUNET_ERROR_TYPE_DEBUG,
1537 "Ratchet key already known. Ignoring KX.\n");
1541 ax->DHRr = *ratchet_key;
1542 ax->last_ephemeral = *ephemeral_key;
1544 if (GNUNET_YES == am_I_alice)
1546 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* a */
1547 ephemeral_key, /* B0 */
1552 GNUNET_CRYPTO_ecdh_eddsa (&ax->kx_0, /* b0 */
1553 &pid->public_key, /* A */
1557 if (GNUNET_YES == am_I_alice)
1559 GNUNET_CRYPTO_ecdh_eddsa (&ax->kx_0, /* a0 */
1560 &pid->public_key, /* B */
1565 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* b */
1566 ephemeral_key, /* A0 */
1571 GNUNET_CRYPTO_ecc_ecdh (&ax->kx_0, /* a0 or b0 */
1572 ephemeral_key, /* B0 or A0 */
1575 GNUNET_CRYPTO_kdf (keys, sizeof(keys),
1577 &key_material, sizeof(key_material),
1580 if (0 == memcmp (&ax->RK,
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 "Root key already known. Ignoring KX.\n");
1586 GNUNET_STATISTICS_update (stats,
1587 "# Root key already known",
1594 if (GNUNET_YES == am_I_alice)
1600 ax->ratchet_flag = GNUNET_YES;
1608 ax->ratchet_flag = GNUNET_NO;
1609 ax->ratchet_expiration
1610 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1618 * Try to redo the KX or KX_AUTH handshake, if we can.
1620 * @param cls the `struct CadetTunnel` to do KX for.
1623 retry_kx (void *cls)
1625 struct CadetTunnel *t = cls;
1626 struct CadetTunnelAxolotl *ax;
1629 LOG (GNUNET_ERROR_TYPE_DEBUG,
1630 "Trying to make KX progress on %s in state %s\n",
1632 estate2s (t->estate));
1635 case CADET_TUNNEL_KEY_UNINITIALIZED: /* first attempt */
1636 case CADET_TUNNEL_KEY_AX_SENT: /* trying again */
1642 case CADET_TUNNEL_KEY_AX_RECV:
1643 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
1644 /* We are responding, so only require reply
1645 if WE have a channel waiting. */
1646 if (NULL != t->unverified_ax)
1648 /* Send AX_AUTH so we might get this one verified */
1649 ax = t->unverified_ax;
1653 /* How can this be? */
1660 (0 == GCT_count_channels (t))
1665 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
1666 /* We are responding, so only require reply
1667 if WE have a channel waiting. */
1668 if (NULL != t->unverified_ax)
1670 /* Send AX_AUTH so we might get this one verified */
1671 ax = t->unverified_ax;
1675 /* How can this be? */
1682 (0 == GCT_count_channels (t))
1687 case CADET_TUNNEL_KEY_OK:
1688 /* Must have been the *other* peer asking us to
1689 respond with a KX_AUTH. */
1690 if (NULL != t->unverified_ax)
1692 /* Sending AX_AUTH in response to AX so we might get this one verified */
1693 ax = t->unverified_ax;
1697 /* Sending AX_AUTH in response to AX_AUTH */
1710 * Handle KX message that lacks authentication (and which will thus
1711 * only be considered authenticated after we respond with our own
1712 * KX_AUTH and finally successfully decrypt payload).
1714 * @param ct connection/tunnel combo that received encrypted message
1715 * @param msg the key exchange message
1718 GCT_handle_kx (struct CadetTConnection *ct,
1719 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1721 struct CadetTunnel *t = ct->t;
1724 GNUNET_STATISTICS_update (stats,
1729 alice_or_betty (GCP_get_id (t->destination)))
1731 /* Betty/Bob is not allowed to send KX! */
1732 GNUNET_break_op (0);
1735 LOG (GNUNET_ERROR_TYPE_DEBUG,
1736 "Received KX message from %s with ephemeral %s from %s on connection %s\n",
1738 GNUNET_e2s (&msg->ephemeral_key),
1739 GNUNET_i2s (GCP_get_id (t->destination)),
1743 memcmp (&t->ax.DHRr,
1745 sizeof(msg->ratchet_key))) &&
1747 memcmp (&t->ax.last_ephemeral,
1748 &msg->ephemeral_key,
1749 sizeof(msg->ephemeral_key))))
1752 GNUNET_STATISTICS_update (stats,
1753 "# Duplicate KX received",
1763 /* We only keep ONE unverified KX around, so if there is an existing one,
1765 if (NULL != t->unverified_ax)
1768 memcmp (&t->unverified_ax->DHRr,
1770 sizeof(msg->ratchet_key))) &&
1772 memcmp (&t->unverified_ax->last_ephemeral,
1773 &msg->ephemeral_key,
1774 sizeof(msg->ephemeral_key))))
1776 GNUNET_STATISTICS_update (stats,
1777 "# Duplicate unverified KX received",
1788 LOG (GNUNET_ERROR_TYPE_DEBUG,
1789 "Dropping old unverified KX state.\n");
1790 GNUNET_STATISTICS_update (stats,
1791 "# Unverified KX dropped for fresh KX",
1794 GNUNET_break (NULL == t->unverified_ax->skipped_head);
1795 memset (t->unverified_ax,
1797 sizeof(struct CadetTunnelAxolotl));
1801 LOG (GNUNET_ERROR_TYPE_DEBUG,
1802 "Creating fresh unverified KX for %s\n",
1804 GNUNET_STATISTICS_update (stats,
1808 t->unverified_ax = GNUNET_new (struct CadetTunnelAxolotl);
1810 /* Set as the 'current' RK/DHRr the one we are currently using,
1811 so that the duplicate-detection logic of
1812 #update_ax_by_kx can work. */
1813 t->unverified_ax->RK = t->ax.RK;
1814 t->unverified_ax->DHRr = t->ax.DHRr;
1815 t->unverified_ax->DHRs = t->ax.DHRs;
1816 t->unverified_ax->kx_0 = t->ax.kx_0;
1817 t->unverified_attempts = 0;
1819 /* Update 'ax' by the new key material */
1820 ret = update_ax_by_kx (t->unverified_ax,
1821 GCP_get_id (t->destination),
1822 &msg->ephemeral_key,
1824 GNUNET_break (GNUNET_SYSERR != ret);
1825 if (GNUNET_OK != ret)
1827 GNUNET_STATISTICS_update (stats,
1831 return; /* duplicate KX, nothing to do */
1833 /* move ahead in our state machine */
1834 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1835 GCT_change_estate (t,
1836 CADET_TUNNEL_KEY_AX_RECV);
1837 else if (CADET_TUNNEL_KEY_AX_SENT == t->estate)
1838 GCT_change_estate (t,
1839 CADET_TUNNEL_KEY_AX_SENT_AND_RECV);
1841 /* KX is still not done, try again our end. */
1842 if (CADET_TUNNEL_KEY_OK != t->estate)
1844 if (NULL != t->kx_task)
1845 GNUNET_SCHEDULER_cancel (t->kx_task);
1847 = GNUNET_SCHEDULER_add_now (&retry_kx,
1855 check_ee (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1856 const struct GNUNET_CRYPTO_EcdhePrivateKey *e2)
1858 struct GNUNET_CRYPTO_EcdhePublicKey p1;
1859 struct GNUNET_CRYPTO_EcdhePublicKey p2;
1860 struct GNUNET_HashCode hc1;
1861 struct GNUNET_HashCode hc2;
1863 GNUNET_CRYPTO_ecdhe_key_get_public (e1,
1865 GNUNET_CRYPTO_ecdhe_key_get_public (e2,
1867 GNUNET_assert (GNUNET_OK ==
1868 GNUNET_CRYPTO_ecc_ecdh (e1,
1871 GNUNET_assert (GNUNET_OK ==
1872 GNUNET_CRYPTO_ecc_ecdh (e2,
1875 GNUNET_break (0 == GNUNET_memcmp (&hc1,
1881 check_ed (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1882 const struct GNUNET_CRYPTO_EddsaPrivateKey *e2)
1884 struct GNUNET_CRYPTO_EcdhePublicKey p1;
1885 struct GNUNET_CRYPTO_EddsaPublicKey p2;
1886 struct GNUNET_HashCode hc1;
1887 struct GNUNET_HashCode hc2;
1889 GNUNET_CRYPTO_ecdhe_key_get_public (e1,
1891 GNUNET_CRYPTO_eddsa_key_get_public (e2,
1893 GNUNET_assert (GNUNET_OK ==
1894 GNUNET_CRYPTO_ecdh_eddsa (e1,
1897 GNUNET_assert (GNUNET_OK ==
1898 GNUNET_CRYPTO_eddsa_ecdh (e2,
1901 GNUNET_break (0 == GNUNET_memcmp (&hc1,
1907 test_crypto_bug (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1908 const struct GNUNET_CRYPTO_EcdhePrivateKey *e2,
1909 const struct GNUNET_CRYPTO_EddsaPrivateKey *d1,
1910 const struct GNUNET_CRYPTO_EddsaPrivateKey *d2)
1922 * Handle KX_AUTH message.
1924 * @param ct connection/tunnel combo that received encrypted message
1925 * @param msg the key exchange message
1928 GCT_handle_kx_auth (struct CadetTConnection *ct,
1929 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
1931 struct CadetTunnel *t = ct->t;
1932 struct CadetTunnelAxolotl ax_tmp;
1933 struct GNUNET_HashCode kx_auth;
1936 GNUNET_STATISTICS_update (stats,
1937 "# KX_AUTH received",
1940 if ((CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) ||
1941 (CADET_TUNNEL_KEY_AX_RECV == t->estate))
1943 /* Confusing, we got a KX_AUTH before we even send our own
1944 KX. This should not happen. We'll send our own KX ASAP anyway,
1945 so let's ignore this here. */
1946 GNUNET_break_op (0);
1949 LOG (GNUNET_ERROR_TYPE_DEBUG,
1950 "Handling KX_AUTH message from %s with ephemeral %s\n",
1952 GNUNET_e2s (&msg->kx.ephemeral_key));
1953 /* We do everything in ax_tmp until we've checked the authentication
1954 so we don't clobber anything we care about by accident. */
1957 /* Update 'ax' by the new key material */
1958 ret = update_ax_by_kx (&ax_tmp,
1959 GCP_get_id (t->destination),
1960 &msg->kx.ephemeral_key,
1961 &msg->kx.ratchet_key);
1962 if (GNUNET_OK != ret)
1964 if (GNUNET_NO == ret)
1965 GNUNET_STATISTICS_update (stats,
1966 "# redundant KX_AUTH received",
1970 GNUNET_break (0); /* connect to self!? */
1973 GNUNET_CRYPTO_hash (&ax_tmp.RK,
1976 if (0 != GNUNET_memcmp (&kx_auth,
1979 /* This KX_AUTH is not using the latest KX/KX_AUTH data
1980 we transmitted to the sender, refuse it, try KX again. */
1981 GNUNET_STATISTICS_update (stats,
1982 "# KX_AUTH not using our last KX received (auth failure)",
1985 LOG (GNUNET_ERROR_TYPE_WARNING,
1986 "KX AUTH mismatch!\n");
1989 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
1991 GNUNET_CRYPTO_ecdhe_key_get_public (&ax_tmp.kx_0,
1993 if (0 != GNUNET_memcmp (&ephemeral_key,
1994 &msg->r_ephemeral_key_XXX))
1996 LOG (GNUNET_ERROR_TYPE_WARNING,
1997 "My ephemeral is %s!\n",
1998 GNUNET_e2s (&ephemeral_key));
1999 LOG (GNUNET_ERROR_TYPE_WARNING,
2000 "Response is for ephemeral %s!\n",
2001 GNUNET_e2s (&msg->r_ephemeral_key_XXX));
2005 test_crypto_bug (&ax_tmp.kx_0,
2006 &msg->kx.ephemeral_key_XXX,
2008 &msg->kx.private_key_XXX);
2012 if (NULL == t->kx_task)
2014 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2019 /* Yep, we're good. */
2021 if (NULL != t->unverified_ax)
2023 /* We got some "stale" KX before, drop that. */
2024 cleanup_ax (t->unverified_ax);
2025 GNUNET_free (t->unverified_ax);
2026 t->unverified_ax = NULL;
2029 /* move ahead in our state machine */
2032 case CADET_TUNNEL_KEY_UNINITIALIZED:
2033 case CADET_TUNNEL_KEY_AX_RECV:
2034 /* Checked above, this is impossible. */
2038 case CADET_TUNNEL_KEY_AX_SENT: /* This is the normal case */
2039 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: /* both peers started KX */
2040 case CADET_TUNNEL_KEY_AX_AUTH_SENT: /* both peers now did KX_AUTH */
2041 GCT_change_estate (t,
2042 CADET_TUNNEL_KEY_OK);
2045 case CADET_TUNNEL_KEY_OK:
2046 /* Did not expect another KX_AUTH, but so what, still acceptable.
2047 Nothing to do here. */
2050 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->kx.flags)))
2060 /* ************************************** end core crypto ***************************** */
2064 * Compute the next free channel tunnel number for this tunnel.
2066 * @param t the tunnel
2067 * @return unused number that can uniquely identify a channel in the tunnel
2069 static struct GNUNET_CADET_ChannelTunnelNumber
2070 get_next_free_ctn (struct CadetTunnel *t)
2072 #define HIGH_BIT 0x8000000
2073 struct GNUNET_CADET_ChannelTunnelNumber ret;
2078 cmp = GNUNET_memcmp (&my_full_id,
2079 GCP_get_id (GCT_get_destination (t)));
2085 GNUNET_assert (0); // loopback must never go here!
2086 ctn = ntohl (t->next_ctn.cn);
2088 GNUNET_CONTAINER_multihashmap32_get (t->channels,
2091 ctn = ((ctn + 1) & (~HIGH_BIT));
2093 t->next_ctn.cn = htonl ((ctn + 1) & (~HIGH_BIT));
2094 ret.cn = htonl (ctn | highbit);
2100 * Add a channel to a tunnel, and notify channel that we are ready
2101 * for transmission if we are already up. Otherwise that notification
2102 * will be done later in #notify_tunnel_up_cb().
2106 * @return unique number identifying @a ch within @a t
2108 struct GNUNET_CADET_ChannelTunnelNumber
2109 GCT_add_channel (struct CadetTunnel *t,
2110 struct CadetChannel *ch)
2112 struct GNUNET_CADET_ChannelTunnelNumber ctn;
2114 ctn = get_next_free_ctn (t);
2115 if (NULL != t->destroy_task)
2117 GNUNET_SCHEDULER_cancel (t->destroy_task);
2118 t->destroy_task = NULL;
2120 GNUNET_assert (GNUNET_YES ==
2121 GNUNET_CONTAINER_multihashmap32_put (t->channels,
2124 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2125 LOG (GNUNET_ERROR_TYPE_DEBUG,
2126 "Adding %s to %s\n",
2131 case CADET_TUNNEL_KEY_UNINITIALIZED:
2132 /* waiting for connection to start KX */
2135 case CADET_TUNNEL_KEY_AX_RECV:
2136 case CADET_TUNNEL_KEY_AX_SENT:
2137 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
2138 /* we're currently waiting for KX to complete */
2141 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
2142 /* waiting for OTHER peer to send us data,
2143 we might need to prompt more aggressively! */
2144 if (NULL == t->kx_task)
2146 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2151 case CADET_TUNNEL_KEY_OK:
2152 /* We are ready. Tell the new channel that we are up. */
2153 GCCH_tunnel_up (ch);
2161 * We lost a connection, remove it from our list and clean up
2162 * the connection object itself.
2164 * @param ct binding of connection to tunnel of the connection that was lost.
2167 GCT_connection_lost (struct CadetTConnection *ct)
2169 struct CadetTunnel *t = ct->t;
2171 if (GNUNET_YES == ct->is_ready)
2173 GNUNET_CONTAINER_DLL_remove (t->connection_ready_head,
2174 t->connection_ready_tail,
2176 t->num_ready_connections--;
2180 GNUNET_CONTAINER_DLL_remove (t->connection_busy_head,
2181 t->connection_busy_tail,
2183 t->num_busy_connections--;
2190 * Clean up connection @a ct of a tunnel.
2192 * @param cls the `struct CadetTunnel`
2193 * @param ct connection to clean up
2196 destroy_t_connection (void *cls,
2197 struct CadetTConnection *ct)
2199 struct CadetTunnel *t = cls;
2200 struct CadetConnection *cc = ct->cc;
2202 GNUNET_assert (ct->t == t);
2203 GCT_connection_lost (ct);
2204 GCC_destroy_without_tunnel (cc);
2209 * This tunnel is no longer used, destroy it.
2211 * @param cls the idle tunnel
2214 destroy_tunnel (void *cls)
2216 struct CadetTunnel *t = cls;
2217 struct CadetTunnelQueueEntry *tq;
2219 t->destroy_task = NULL;
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "Destroying idle %s\n",
2223 GNUNET_assert (0 == GCT_count_channels (t));
2224 GCT_iterate_connections (t,
2225 &destroy_t_connection,
2227 GNUNET_assert (NULL == t->connection_ready_head);
2228 GNUNET_assert (NULL == t->connection_busy_head);
2229 while (NULL != (tq = t->tq_head))
2231 if (NULL != tq->cont)
2232 tq->cont (tq->cont_cls,
2234 GCT_send_cancel (tq);
2236 GCP_drop_tunnel (t->destination,
2238 GNUNET_CONTAINER_multihashmap32_destroy (t->channels);
2239 if (NULL != t->maintain_connections_task)
2241 GNUNET_SCHEDULER_cancel (t->maintain_connections_task);
2242 t->maintain_connections_task = NULL;
2244 if (NULL != t->send_task)
2246 GNUNET_SCHEDULER_cancel (t->send_task);
2247 t->send_task = NULL;
2249 if (NULL != t->kx_task)
2251 GNUNET_SCHEDULER_cancel (t->kx_task);
2254 GNUNET_MST_destroy (t->mst);
2255 GNUNET_MQ_destroy (t->mq);
2256 if (NULL != t->unverified_ax)
2258 cleanup_ax (t->unverified_ax);
2259 GNUNET_free (t->unverified_ax);
2261 cleanup_ax (&t->ax);
2262 GNUNET_assert (NULL == t->destroy_task);
2268 * Remove a channel from a tunnel.
2272 * @param ctn unique number identifying @a ch within @a t
2275 GCT_remove_channel (struct CadetTunnel *t,
2276 struct CadetChannel *ch,
2277 struct GNUNET_CADET_ChannelTunnelNumber ctn)
2279 LOG (GNUNET_ERROR_TYPE_DEBUG,
2280 "Removing %s from %s\n",
2283 GNUNET_assert (GNUNET_YES ==
2284 GNUNET_CONTAINER_multihashmap32_remove (t->channels,
2288 GCT_count_channels (t)) &&
2289 (NULL == t->destroy_task))
2292 = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
2300 * Destroy remaining channels during shutdown.
2302 * @param cls the `struct CadetTunnel` of the channel
2303 * @param key key of the channel
2304 * @param value the `struct CadetChannel`
2305 * @return #GNUNET_OK (continue to iterate)
2308 destroy_remaining_channels (void *cls,
2312 struct CadetChannel *ch = value;
2314 GCCH_handle_remote_destroy (ch,
2321 * Destroys the tunnel @a t now, without delay. Used during shutdown.
2323 * @param t tunnel to destroy
2326 GCT_destroy_tunnel_now (struct CadetTunnel *t)
2328 GNUNET_assert (GNUNET_YES == shutting_down);
2329 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2330 &destroy_remaining_channels,
2333 GCT_count_channels (t));
2334 if (NULL != t->destroy_task)
2336 GNUNET_SCHEDULER_cancel (t->destroy_task);
2337 t->destroy_task = NULL;
2344 * Send normal payload from queue in @a t via connection @a ct.
2345 * Does nothing if our payload queue is empty.
2347 * @param t tunnel to send data from
2348 * @param ct connection to use for transmission (is ready)
2351 try_send_normal_payload (struct CadetTunnel *t,
2352 struct CadetTConnection *ct)
2354 struct CadetTunnelQueueEntry *tq;
2356 GNUNET_assert (GNUNET_YES == ct->is_ready);
2360 /* no messages pending right now */
2361 LOG (GNUNET_ERROR_TYPE_DEBUG,
2362 "Not sending payload of %s on ready %s (nothing pending)\n",
2367 /* ready to send message 'tq' on tunnel 'ct' */
2368 GNUNET_assert (t == tq->t);
2369 GNUNET_CONTAINER_DLL_remove (t->tq_head,
2372 if (NULL != tq->cid)
2373 *tq->cid = *GCC_get_id (ct->cc);
2374 mark_connection_unready (ct);
2375 LOG (GNUNET_ERROR_TYPE_DEBUG,
2376 "Sending payload of %s on %s\n",
2379 GCC_transmit (ct->cc,
2381 if (NULL != tq->cont)
2382 tq->cont (tq->cont_cls,
2383 GCC_get_id (ct->cc));
2389 * A connection is @a is_ready for transmission. Looks at our message
2390 * queue and if there is a message, sends it out via the connection.
2392 * @param cls the `struct CadetTConnection` that is @a is_ready
2393 * @param is_ready #GNUNET_YES if connection are now ready,
2394 * #GNUNET_NO if connection are no longer ready
2397 connection_ready_cb (void *cls,
2400 struct CadetTConnection *ct = cls;
2401 struct CadetTunnel *t = ct->t;
2403 if (GNUNET_NO == is_ready)
2405 LOG (GNUNET_ERROR_TYPE_DEBUG,
2406 "%s no longer ready for %s\n",
2409 mark_connection_unready (ct);
2412 GNUNET_assert (GNUNET_NO == ct->is_ready);
2413 GNUNET_CONTAINER_DLL_remove (t->connection_busy_head,
2414 t->connection_busy_tail,
2416 GNUNET_assert (0 < t->num_busy_connections);
2417 t->num_busy_connections--;
2418 ct->is_ready = GNUNET_YES;
2419 GNUNET_CONTAINER_DLL_insert_tail (t->connection_ready_head,
2420 t->connection_ready_tail,
2422 t->num_ready_connections++;
2424 LOG (GNUNET_ERROR_TYPE_DEBUG,
2425 "%s now ready for %s in state %s\n",
2428 estate2s (t->estate));
2431 case CADET_TUNNEL_KEY_UNINITIALIZED:
2432 /* Do not begin KX if WE have no channels waiting! */
2433 if (0 != GNUNET_TIME_absolute_get_remaining (
2434 t->next_kx_attempt).rel_value_us)
2435 return; /* wait for timeout before retrying */
2436 /* We are uninitialized, just transmit immediately,
2437 without undue delay. */
2438 if (NULL != t->kx_task)
2440 GNUNET_SCHEDULER_cancel (t->kx_task);
2447 GCT_count_channels (t)) &&
2448 (NULL == t->destroy_task))
2451 = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
2457 case CADET_TUNNEL_KEY_AX_RECV:
2458 case CADET_TUNNEL_KEY_AX_SENT:
2459 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
2460 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
2461 /* we're currently waiting for KX to complete, schedule job */
2462 if (NULL == t->kx_task)
2464 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2469 case CADET_TUNNEL_KEY_OK:
2470 if (GNUNET_YES == t->kx_auth_requested)
2472 if (0 != GNUNET_TIME_absolute_get_remaining (
2473 t->next_kx_attempt).rel_value_us)
2474 return; /* wait for timeout */
2475 if (NULL != t->kx_task)
2477 GNUNET_SCHEDULER_cancel (t->kx_task);
2486 try_send_normal_payload (t,
2494 * Called when either we have a new connection, or a new message in the
2495 * queue, or some existing connection has transmission capacity. Looks
2496 * at our message queue and if there is a message, picks a connection
2499 * @param cls the `struct CadetTunnel` to process messages on
2502 trigger_transmissions (void *cls)
2504 struct CadetTunnel *t = cls;
2505 struct CadetTConnection *ct;
2507 t->send_task = NULL;
2508 if (NULL == t->tq_head)
2509 return; /* no messages pending right now */
2510 ct = get_ready_connection (t);
2512 return; /* no connections ready */
2513 try_send_normal_payload (t,
2519 * Closure for #evaluate_connection. Used to assemble summary information
2520 * about the existing connections so we can evaluate a new path.
2522 struct EvaluationSummary
2525 * Minimum length of any of our connections, `UINT_MAX` if we have none.
2527 unsigned int min_length;
2530 * Maximum length of any of our connections, 0 if we have none.
2532 unsigned int max_length;
2535 * Minimum desirability of any of our connections, UINT64_MAX if we have none.
2537 GNUNET_CONTAINER_HeapCostType min_desire;
2540 * Maximum desirability of any of our connections, 0 if we have none.
2542 GNUNET_CONTAINER_HeapCostType max_desire;
2545 * Path we are comparing against for #evaluate_connection, can be NULL.
2547 struct CadetPeerPath *path;
2550 * Connection deemed the "worst" so far encountered by #evaluate_connection,
2551 * NULL if we did not yet encounter any connections.
2553 struct CadetTConnection *worst;
2556 * Numeric score of @e worst, only set if @e worst is non-NULL.
2561 * Set to #GNUNET_YES if we have a connection over @e path already.
2568 * Evaluate a connection, updating our summary information in @a cls about
2569 * what kinds of connections we have.
2571 * @param cls the `struct EvaluationSummary *` to update
2572 * @param ct a connection to include in the summary
2575 evaluate_connection (void *cls,
2576 struct CadetTConnection *ct)
2578 struct EvaluationSummary *es = cls;
2579 struct CadetConnection *cc = ct->cc;
2580 unsigned int ct_length;
2581 struct CadetPeerPath *ps;
2582 const struct CadetConnectionMetrics *metrics;
2583 GNUNET_CONTAINER_HeapCostType ct_desirability;
2584 struct GNUNET_TIME_Relative uptime;
2585 struct GNUNET_TIME_Relative last_use;
2587 double success_rate;
2589 ps = GCC_get_path (cc,
2591 LOG (GNUNET_ERROR_TYPE_DEBUG,
2592 "Evaluating path %s of existing %s\n",
2597 LOG (GNUNET_ERROR_TYPE_DEBUG,
2598 "Ignoring duplicate path %s.\n",
2599 GCPP_2s (es->path));
2600 es->duplicate = GNUNET_YES;
2603 if (NULL != es->path)
2605 int duplicate = GNUNET_YES;
2607 for (unsigned int i = 0; i < ct_length; i++)
2609 GNUNET_assert (GCPP_get_length (es->path) > i);
2610 if (GCPP_get_peer_at_offset (es->path,
2612 GCPP_get_peer_at_offset (ps,
2615 duplicate = GNUNET_NO;
2619 if (GNUNET_YES == duplicate)
2621 LOG (GNUNET_ERROR_TYPE_DEBUG,
2622 "Ignoring overlapping path %s.\n",
2623 GCPP_2s (es->path));
2624 es->duplicate = GNUNET_YES;
2629 LOG (GNUNET_ERROR_TYPE_DEBUG,
2630 "Known path %s differs from proposed path\n",
2635 ct_desirability = GCPP_get_desirability (ps);
2636 metrics = GCC_get_metrics (cc);
2637 uptime = GNUNET_TIME_absolute_get_duration (metrics->age);
2638 last_use = GNUNET_TIME_absolute_get_duration (metrics->last_use);
2639 /* We add 1.0 here to avoid division by zero. */
2640 success_rate = (metrics->num_acked_transmissions + 1.0)
2641 / (metrics->num_successes + 1.0);
2644 + 100.0 / (1.0 + ct_length) /* longer paths = better */
2645 + sqrt (uptime.rel_value_us / 60000000LL) /* larger uptime = better */
2646 - last_use.rel_value_us / 1000L; /* longer idle = worse */
2647 score *= success_rate; /* weigh overall by success rate */
2649 if ((NULL == es->worst) ||
2650 (score < es->worst_score))
2653 es->worst_score = score;
2655 es->min_length = GNUNET_MIN (es->min_length,
2657 es->max_length = GNUNET_MAX (es->max_length,
2659 es->min_desire = GNUNET_MIN (es->min_desire,
2661 es->max_desire = GNUNET_MAX (es->max_desire,
2667 * Consider using the path @a p for the tunnel @a t.
2668 * The tunnel destination is at offset @a off in path @a p.
2670 * @param cls our tunnel
2671 * @param path a path to our destination
2672 * @param off offset of the destination on path @a path
2673 * @return #GNUNET_YES (should keep iterating)
2676 consider_path_cb (void *cls,
2677 struct CadetPeerPath *path,
2680 struct CadetTunnel *t = cls;
2681 struct EvaluationSummary es;
2682 struct CadetTConnection *ct;
2684 GNUNET_assert (off < GCPP_get_length (path));
2685 GNUNET_assert (GCPP_get_peer_at_offset (path,
2686 off) == t->destination);
2687 es.min_length = UINT_MAX;
2690 es.min_desire = UINT64_MAX;
2692 es.duplicate = GNUNET_NO;
2695 /* Compute evaluation summary over existing connections. */
2696 LOG (GNUNET_ERROR_TYPE_DEBUG,
2697 "Evaluating proposed path %s for target %s\n",
2700 /* FIXME: suspect this does not ACTUALLY iterate
2701 over all existing paths, otherwise dup detection
2703 GCT_iterate_connections (t,
2704 &evaluate_connection,
2706 if (GNUNET_YES == es.duplicate)
2709 /* FIXME: not sure we should really just count
2710 'num_connections' here, as they may all have
2711 consistently failed to connect. */
2713 /* We iterate by increasing path length; if we have enough paths and
2714 this one is more than twice as long than what we are currently
2715 using, then ignore all of these super-long ones! */
2716 if ((GCT_count_any_connections (t) > DESIRED_CONNECTIONS_PER_TUNNEL) &&
2717 (es.min_length * 2 < off) &&
2718 (es.max_length < off))
2720 LOG (GNUNET_ERROR_TYPE_DEBUG,
2721 "Ignoring paths of length %u, they are way too long.\n",
2725 /* If we have enough paths and this one looks no better, ignore it. */
2726 if ((GCT_count_any_connections (t) >= DESIRED_CONNECTIONS_PER_TUNNEL) &&
2727 (es.min_length < GCPP_get_length (path)) &&
2728 (es.min_desire > GCPP_get_desirability (path)) &&
2729 (es.max_length < off))
2731 LOG (GNUNET_ERROR_TYPE_DEBUG,
2732 "Ignoring path (%u/%llu) to %s, got something better already.\n",
2733 GCPP_get_length (path),
2734 (unsigned long long) GCPP_get_desirability (path),
2735 GCP_2s (t->destination));
2739 /* Path is interesting (better by some metric, or we don't have
2740 enough paths yet). */
2741 ct = GNUNET_new (struct CadetTConnection);
2742 ct->created = GNUNET_TIME_absolute_get ();
2744 ct->cc = GCC_create (t->destination,
2748 &connection_ready_cb,
2751 /* FIXME: schedule job to kill connection (and path?) if it takes
2752 too long to get ready! (And track performance data on how long
2753 other connections took with the tunnel!)
2754 => Note: to be done within 'connection'-logic! */
2755 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head,
2756 t->connection_busy_tail,
2758 t->num_busy_connections++;
2759 LOG (GNUNET_ERROR_TYPE_DEBUG,
2760 "Found interesting path %s for %s, created %s\n",
2769 * Function called to maintain the connections underlying our tunnel.
2770 * Tries to maintain (incl. tear down) connections for the tunnel, and
2771 * if there is a significant change, may trigger transmissions.
2773 * Basically, needs to check if there are connections that perform
2774 * badly, and if so eventually kill them and trigger a replacement.
2775 * The strategy is to open one more connection than
2776 * #DESIRED_CONNECTIONS_PER_TUNNEL, and then periodically kick out the
2777 * least-performing one, and then inquire for new ones.
2779 * @param cls the `struct CadetTunnel`
2782 maintain_connections_cb (void *cls)
2784 struct CadetTunnel *t = cls;
2785 struct GNUNET_TIME_Relative delay;
2786 struct EvaluationSummary es;
2788 t->maintain_connections_task = NULL;
2789 LOG (GNUNET_ERROR_TYPE_DEBUG,
2790 "Performing connection maintenance for %s.\n",
2793 es.min_length = UINT_MAX;
2796 es.min_desire = UINT64_MAX;
2799 es.duplicate = GNUNET_NO;
2800 GCT_iterate_connections (t,
2801 &evaluate_connection,
2803 if ((NULL != es.worst) &&
2804 (GCT_count_any_connections (t) > DESIRED_CONNECTIONS_PER_TUNNEL))
2806 /* Clear out worst-performing connection 'es.worst'. */
2807 destroy_t_connection (t,
2811 /* Consider additional paths */
2812 (void) GCP_iterate_paths (t->destination,
2816 /* FIXME: calculate when to try again based on how well we are doing;
2817 in particular, if we have to few connections, we might be able
2818 to do without this (as PATHS should tell us whenever a new path
2819 is available instantly; however, need to make sure this job is
2820 restarted after that happens).
2821 Furthermore, if the paths we do know are in a reasonably narrow
2822 quality band and are plentyful, we might also consider us stabilized
2823 and then reduce the frequency accordingly. */delay = GNUNET_TIME_UNIT_MINUTES;
2824 t->maintain_connections_task
2825 = GNUNET_SCHEDULER_add_delayed (delay,
2826 &maintain_connections_cb,
2832 * Consider using the path @a p for the tunnel @a t.
2833 * The tunnel destination is at offset @a off in path @a p.
2835 * @param cls our tunnel
2836 * @param path a path to our destination
2837 * @param off offset of the destination on path @a path
2840 GCT_consider_path (struct CadetTunnel *t,
2841 struct CadetPeerPath *p,
2844 LOG (GNUNET_ERROR_TYPE_DEBUG,
2845 "Considering %s for %s (offset %u)\n",
2849 (void) consider_path_cb (t,
2856 * We got a keepalive. Track in statistics.
2858 * @param cls the `struct CadetTunnel` for which we decrypted the message
2859 * @param msg the message we received on the tunnel
2862 handle_plaintext_keepalive (void *cls,
2863 const struct GNUNET_MessageHeader *msg)
2865 struct CadetTunnel *t = cls;
2867 LOG (GNUNET_ERROR_TYPE_DEBUG,
2868 "Received KEEPALIVE on %s\n",
2870 GNUNET_STATISTICS_update (stats,
2871 "# keepalives received",
2878 * Check that @a msg is well-formed.
2880 * @param cls the `struct CadetTunnel` for which we decrypted the message
2881 * @param msg the message we received on the tunnel
2882 * @return #GNUNET_OK (any variable-size payload goes)
2885 check_plaintext_data (void *cls,
2886 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
2893 * We received payload data for a channel. Locate the channel
2894 * and process the data, or return an error if the channel is unknown.
2896 * @param cls the `struct CadetTunnel` for which we decrypted the message
2897 * @param msg the message we received on the tunnel
2900 handle_plaintext_data (void *cls,
2901 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
2903 struct CadetTunnel *t = cls;
2904 struct CadetChannel *ch;
2906 ch = lookup_channel (t,
2910 /* We don't know about such a channel, might have been destroyed on our
2911 end in the meantime, or never existed. Send back a DESTROY. */
2912 LOG (GNUNET_ERROR_TYPE_DEBUG,
2913 "Received %u bytes of application data for unknown channel %u, sending DESTROY\n",
2914 (unsigned int) (ntohs (msg->header.size) - sizeof(*msg)),
2915 ntohl (msg->ctn.cn));
2916 GCT_send_channel_destroy (t,
2920 GCCH_handle_channel_plaintext_data (ch,
2921 GCC_get_id (t->current_ct->cc),
2927 * We received an acknowledgement for data we sent on a channel.
2928 * Locate the channel and process it, or return an error if the
2929 * channel is unknown.
2931 * @param cls the `struct CadetTunnel` for which we decrypted the message
2932 * @param ack the message we received on the tunnel
2935 handle_plaintext_data_ack (void *cls,
2936 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
2938 struct CadetTunnel *t = cls;
2939 struct CadetChannel *ch;
2941 ch = lookup_channel (t,
2945 /* We don't know about such a channel, might have been destroyed on our
2946 end in the meantime, or never existed. Send back a DESTROY. */
2947 LOG (GNUNET_ERROR_TYPE_DEBUG,
2948 "Received DATA_ACK for unknown channel %u, sending DESTROY\n",
2949 ntohl (ack->ctn.cn));
2950 GCT_send_channel_destroy (t,
2954 GCCH_handle_channel_plaintext_data_ack (ch,
2955 GCC_get_id (t->current_ct->cc),
2961 * We have received a request to open a channel to a port from
2962 * another peer. Creates the incoming channel.
2964 * @param cls the `struct CadetTunnel` for which we decrypted the message
2965 * @param copen the message we received on the tunnel
2968 handle_plaintext_channel_open (void *cls,
2970 GNUNET_CADET_ChannelOpenMessage *copen)
2972 struct CadetTunnel *t = cls;
2973 struct CadetChannel *ch;
2975 ch = GNUNET_CONTAINER_multihashmap32_get (t->channels,
2976 ntohl (copen->ctn.cn));
2979 LOG (GNUNET_ERROR_TYPE_DEBUG,
2980 "Received duplicate channel CHANNEL_OPEN on h_port %s from %s (%s), resending ACK\n",
2981 GNUNET_h2s (&copen->h_port),
2984 GCCH_handle_duplicate_open (ch,
2985 GCC_get_id (t->current_ct->cc));
2988 LOG (GNUNET_ERROR_TYPE_DEBUG,
2989 "Received CHANNEL_OPEN on h_port %s from %s\n",
2990 GNUNET_h2s (&copen->h_port),
2992 ch = GCCH_channel_incoming_new (t,
2995 ntohl (copen->opt));
2996 if (NULL != t->destroy_task)
2998 GNUNET_SCHEDULER_cancel (t->destroy_task);
2999 t->destroy_task = NULL;
3001 GNUNET_assert (GNUNET_OK ==
3002 GNUNET_CONTAINER_multihashmap32_put (t->channels,
3003 ntohl (copen->ctn.cn),
3005 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3010 * Send a DESTROY message via the tunnel.
3012 * @param t the tunnel to transmit over
3013 * @param ctn ID of the channel to destroy
3016 GCT_send_channel_destroy (struct CadetTunnel *t,
3017 struct GNUNET_CADET_ChannelTunnelNumber ctn)
3019 struct GNUNET_CADET_ChannelDestroyMessage msg;
3021 LOG (GNUNET_ERROR_TYPE_DEBUG,
3022 "Sending DESTORY message for channel ID %u\n",
3024 msg.header.size = htons (sizeof(msg));
3025 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
3026 msg.reserved = htonl (0);
3036 * We have received confirmation from the target peer that the
3037 * given channel could be established (the port is open).
3040 * @param cls the `struct CadetTunnel` for which we decrypted the message
3041 * @param cm the message we received on the tunnel
3044 handle_plaintext_channel_open_ack (void *cls,
3046 GNUNET_CADET_ChannelOpenAckMessage *cm)
3048 struct CadetTunnel *t = cls;
3049 struct CadetChannel *ch;
3051 ch = lookup_channel (t,
3055 /* We don't know about such a channel, might have been destroyed on our
3056 end in the meantime, or never existed. Send back a DESTROY. */
3057 LOG (GNUNET_ERROR_TYPE_DEBUG,
3058 "Received channel OPEN_ACK for unknown channel %u, sending DESTROY\n",
3059 ntohl (cm->ctn.cn));
3060 GCT_send_channel_destroy (t,
3064 LOG (GNUNET_ERROR_TYPE_DEBUG,
3065 "Received channel OPEN_ACK on channel %s from %s\n",
3068 GCCH_handle_channel_open_ack (ch,
3069 GCC_get_id (t->current_ct->cc),
3075 * We received a message saying that a channel should be destroyed.
3076 * Pass it on to the correct channel.
3078 * @param cls the `struct CadetTunnel` for which we decrypted the message
3079 * @param cm the message we received on the tunnel
3082 handle_plaintext_channel_destroy (void *cls,
3084 GNUNET_CADET_ChannelDestroyMessage *cm)
3086 struct CadetTunnel *t = cls;
3087 struct CadetChannel *ch;
3089 ch = lookup_channel (t,
3093 /* We don't know about such a channel, might have been destroyed on our
3094 end in the meantime, or never existed. */
3095 LOG (GNUNET_ERROR_TYPE_DEBUG,
3096 "Received channel DESTORY for unknown channel %u. Ignoring.\n",
3097 ntohl (cm->ctn.cn));
3100 LOG (GNUNET_ERROR_TYPE_DEBUG,
3101 "Received channel DESTROY on %s from %s\n",
3104 GCCH_handle_remote_destroy (ch,
3105 GCC_get_id (t->current_ct->cc));
3110 * Handles a message we decrypted, by injecting it into
3111 * our message queue (which will do the dispatching).
3113 * @param cls the `struct CadetTunnel` that got the message
3114 * @param msg the message
3115 * @return #GNUNET_OK on success (always)
3116 * #GNUNET_NO to stop further processing (no error)
3117 * #GNUNET_SYSERR to stop further processing with error
3120 handle_decrypted (void *cls,
3121 const struct GNUNET_MessageHeader *msg)
3123 struct CadetTunnel *t = cls;
3125 GNUNET_assert (NULL != t->current_ct);
3126 GNUNET_MQ_inject_message (t->mq,
3133 * Function called if we had an error processing
3134 * an incoming decrypted message.
3136 * @param cls the `struct CadetTunnel`
3137 * @param error error code
3140 decrypted_error_cb (void *cls,
3141 enum GNUNET_MQ_Error error)
3143 GNUNET_break_op (0);
3148 * Create a tunnel to @a destionation. Must only be called
3149 * from within #GCP_get_tunnel().
3151 * @param destination where to create the tunnel to
3152 * @return new tunnel to @a destination
3154 struct CadetTunnel *
3155 GCT_create_tunnel (struct CadetPeer *destination)
3157 struct CadetTunnel *t = GNUNET_new (struct CadetTunnel);
3158 struct GNUNET_MQ_MessageHandler handlers[] = {
3159 GNUNET_MQ_hd_fixed_size (plaintext_keepalive,
3160 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE,
3161 struct GNUNET_MessageHeader,
3163 GNUNET_MQ_hd_var_size (plaintext_data,
3164 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA,
3165 struct GNUNET_CADET_ChannelAppDataMessage,
3167 GNUNET_MQ_hd_fixed_size (plaintext_data_ack,
3168 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK,
3169 struct GNUNET_CADET_ChannelDataAckMessage,
3171 GNUNET_MQ_hd_fixed_size (plaintext_channel_open,
3172 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
3173 struct GNUNET_CADET_ChannelOpenMessage,
3175 GNUNET_MQ_hd_fixed_size (plaintext_channel_open_ack,
3176 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK,
3177 struct GNUNET_CADET_ChannelOpenAckMessage,
3179 GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy,
3180 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
3181 struct GNUNET_CADET_ChannelDestroyMessage,
3183 GNUNET_MQ_handler_end ()
3186 t->kx_retry_delay = INITIAL_KX_RETRY_DELAY;
3187 new_ephemeral (&t->ax);
3188 GNUNET_CRYPTO_ecdhe_key_create (&t->ax.kx_0);
3189 t->destination = destination;
3190 t->channels = GNUNET_CONTAINER_multihashmap32_create (8);
3191 t->maintain_connections_task
3192 = GNUNET_SCHEDULER_add_now (&maintain_connections_cb,
3194 t->mq = GNUNET_MQ_queue_for_callbacks (NULL,
3199 &decrypted_error_cb,
3201 t->mst = GNUNET_MST_create (&handle_decrypted,
3208 * Add a @a connection to the @a tunnel.
3211 * @param cid connection identifer to use for the connection
3212 * @param options options for the connection
3213 * @param path path to use for the connection
3214 * @return #GNUNET_OK on success,
3215 * #GNUNET_SYSERR on failure (duplicate connection)
3218 GCT_add_inbound_connection (struct CadetTunnel *t,
3220 GNUNET_CADET_ConnectionTunnelIdentifier *cid,
3221 struct CadetPeerPath *path)
3223 struct CadetTConnection *ct;
3225 ct = GNUNET_new (struct CadetTConnection);
3226 ct->created = GNUNET_TIME_absolute_get ();
3228 ct->cc = GCC_create_inbound (t->destination,
3232 &connection_ready_cb,
3236 LOG (GNUNET_ERROR_TYPE_DEBUG,
3237 "%s refused inbound %s (duplicate)\n",
3241 return GNUNET_SYSERR;
3243 /* FIXME: schedule job to kill connection (and path?) if it takes
3244 too long to get ready! (And track performance data on how long
3245 other connections took with the tunnel!)
3246 => Note: to be done within 'connection'-logic! */
3247 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head,
3248 t->connection_busy_tail,
3250 t->num_busy_connections++;
3251 LOG (GNUNET_ERROR_TYPE_DEBUG,
3260 * Handle encrypted message.
3262 * @param ct connection/tunnel combo that received encrypted message
3263 * @param msg the encrypted message to decrypt
3266 GCT_handle_encrypted (struct CadetTConnection *ct,
3267 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
3269 struct CadetTunnel *t = ct->t;
3270 uint16_t size = ntohs (msg->header.size);
3271 char cbuf [size] GNUNET_ALIGN;
3272 ssize_t decrypted_size;
3274 LOG (GNUNET_ERROR_TYPE_DEBUG,
3275 "%s received %u bytes of encrypted data in state %d\n",
3277 (unsigned int) size,
3282 case CADET_TUNNEL_KEY_UNINITIALIZED:
3283 case CADET_TUNNEL_KEY_AX_RECV:
3284 /* We did not even SEND our KX, how can the other peer
3285 send us encrypted data? Must have been that we went
3286 down and the other peer still things we are up.
3287 Let's send it KX back. */
3288 GNUNET_STATISTICS_update (stats,
3289 "# received encrypted without any KX",
3292 if (NULL != t->kx_task)
3294 GNUNET_SCHEDULER_cancel (t->kx_task);
3302 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
3303 /* We send KX, and other peer send KX to us at the same time.
3304 Neither KX is AUTH'ed, so let's try KX_AUTH this time. */
3305 GNUNET_STATISTICS_update (stats,
3306 "# received encrypted without KX_AUTH",
3309 if (NULL != t->kx_task)
3311 GNUNET_SCHEDULER_cancel (t->kx_task);
3320 case CADET_TUNNEL_KEY_AX_SENT:
3321 /* We did not get the KX of the other peer, but that
3322 might have been lost. Send our KX again immediately. */
3323 GNUNET_STATISTICS_update (stats,
3324 "# received encrypted without KX",
3327 if (NULL != t->kx_task)
3329 GNUNET_SCHEDULER_cancel (t->kx_task);
3337 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
3338 /* Great, first payload, we might graduate to OK! */
3339 case CADET_TUNNEL_KEY_OK:
3340 /* We are up and running, all good. */
3344 decrypted_size = -1;
3345 if (CADET_TUNNEL_KEY_OK == t->estate)
3347 /* We have well-established key material available,
3348 try that. (This is the common case.) */
3349 decrypted_size = t_ax_decrypt_and_validate (&t->ax,
3355 if ((-1 == decrypted_size) &&
3356 (NULL != t->unverified_ax))
3358 /* We have un-authenticated KX material available. We should try
3359 this as a back-up option, in case the sender crashed and
3361 decrypted_size = t_ax_decrypt_and_validate (t->unverified_ax,
3365 if (-1 != decrypted_size)
3367 /* It worked! Treat this as authentication of the AX data! */
3368 cleanup_ax (&t->ax);
3369 t->ax = *t->unverified_ax;
3370 GNUNET_free (t->unverified_ax);
3371 t->unverified_ax = NULL;
3373 if (CADET_TUNNEL_KEY_AX_AUTH_SENT == t->estate)
3375 /* First time it worked, move tunnel into production! */
3376 GCT_change_estate (t,
3377 CADET_TUNNEL_KEY_OK);
3378 if (NULL != t->send_task)
3379 GNUNET_SCHEDULER_cancel (t->send_task);
3380 t->send_task = GNUNET_SCHEDULER_add_now (&trigger_transmissions,
3384 if (NULL != t->unverified_ax)
3386 /* We had unverified KX material that was useless; so increment
3387 counter and eventually move to ignore it. Note that we even do
3388 this increment if we successfully decrypted with the old KX
3389 material and thus didn't even both with the new one. This is
3390 the ideal case, as a malicious injection of bogus KX data
3391 basically only causes us to increment a counter a few times. */t->unverified_attempts++;
3392 LOG (GNUNET_ERROR_TYPE_DEBUG,
3393 "Failed to decrypt message with unverified KX data %u times\n",
3394 t->unverified_attempts);
3395 if (t->unverified_attempts > MAX_UNVERIFIED_ATTEMPTS)
3397 cleanup_ax (t->unverified_ax);
3398 GNUNET_free (t->unverified_ax);
3399 t->unverified_ax = NULL;
3403 if (-1 == decrypted_size)
3405 /* Decryption failed for good, complain. */
3406 LOG (GNUNET_ERROR_TYPE_WARNING,
3407 "%s failed to decrypt and validate encrypted data, retrying KX\n",
3409 GNUNET_STATISTICS_update (stats,
3410 "# unable to decrypt",
3413 if (NULL != t->kx_task)
3415 GNUNET_SCHEDULER_cancel (t->kx_task);
3423 GNUNET_STATISTICS_update (stats,
3424 "# decrypted bytes",
3428 /* The MST will ultimately call #handle_decrypted() on each message. */
3430 GNUNET_break_op (GNUNET_OK ==
3431 GNUNET_MST_from_buffer (t->mst,
3436 t->current_ct = NULL;
3441 * Sends an already built message on a tunnel, encrypting it and
3442 * choosing the best connection if not provided.
3444 * @param message Message to send. Function modifies it.
3445 * @param t Tunnel on which this message is transmitted.
3446 * @param cont Continuation to call once message is really sent.
3447 * @param cont_cls Closure for @c cont.
3448 * @return Handle to cancel message
3450 struct CadetTunnelQueueEntry *
3451 GCT_send (struct CadetTunnel *t,
3452 const struct GNUNET_MessageHeader *message,
3453 GCT_SendContinuation cont,
3456 struct CadetTunnelQueueEntry *tq;
3457 uint16_t payload_size;
3458 struct GNUNET_MQ_Envelope *env;
3459 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
3461 if (CADET_TUNNEL_KEY_OK != t->estate)
3466 payload_size = ntohs (message->size);
3467 LOG (GNUNET_ERROR_TYPE_DEBUG,
3468 "Encrypting %u bytes for %s\n",
3469 (unsigned int) payload_size,
3471 env = GNUNET_MQ_msg_extra (ax_msg,
3473 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
3474 t_ax_encrypt (&t->ax,
3478 GNUNET_STATISTICS_update (stats,
3479 "# encrypted bytes",
3482 ax_msg->ax_header.Ns = htonl (t->ax.Ns++);
3483 ax_msg->ax_header.PNs = htonl (t->ax.PNs);
3484 /* FIXME: we should do this once, not once per message;
3485 this is a point multiplication, and DHRs does not
3486 change all the time. */
3487 GNUNET_CRYPTO_ecdhe_key_get_public (&t->ax.DHRs,
3488 &ax_msg->ax_header.DHRs);
3489 t_h_encrypt (&t->ax,
3491 t_hmac (&ax_msg->ax_header,
3492 sizeof(struct GNUNET_CADET_AxHeader) + payload_size,
3497 tq = GNUNET_malloc (sizeof(*tq));
3500 tq->cid = &ax_msg->cid; /* will initialize 'ax_msg->cid' once we know the connection */
3502 tq->cont_cls = cont_cls;
3503 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head,
3506 if (NULL != t->send_task)
3507 GNUNET_SCHEDULER_cancel (t->send_task);
3509 = GNUNET_SCHEDULER_add_now (&trigger_transmissions,
3516 * Cancel a previously sent message while it's in the queue.
3518 * ONLY can be called before the continuation given to the send
3519 * function is called. Once the continuation is called, the message is
3520 * no longer in the queue!
3522 * @param tq Handle to the queue entry to cancel.
3525 GCT_send_cancel (struct CadetTunnelQueueEntry *tq)
3527 struct CadetTunnel *t = tq->t;
3529 GNUNET_CONTAINER_DLL_remove (t->tq_head,
3532 GNUNET_MQ_discard (tq->env);
3538 * Iterate over all connections of a tunnel.
3540 * @param t Tunnel whose connections to iterate.
3541 * @param iter Iterator.
3542 * @param iter_cls Closure for @c iter.
3545 GCT_iterate_connections (struct CadetTunnel *t,
3546 GCT_ConnectionIterator iter,
3549 struct CadetTConnection *n;
3551 for (struct CadetTConnection *ct = t->connection_ready_head;
3559 for (struct CadetTConnection *ct = t->connection_busy_head;
3571 * Closure for #iterate_channels_cb.
3578 GCT_ChannelIterator iter;
3581 * Closure for @e iter.
3588 * Helper function for #GCT_iterate_channels.
3590 * @param cls the `struct ChanIterCls`
3592 * @param value a `struct CadetChannel`
3593 * @return #GNUNET_OK
3596 iterate_channels_cb (void *cls,
3600 struct ChanIterCls *ctx = cls;
3601 struct CadetChannel *ch = value;
3603 ctx->iter (ctx->iter_cls,
3610 * Iterate over all channels of a tunnel.
3612 * @param t Tunnel whose channels to iterate.
3613 * @param iter Iterator.
3614 * @param iter_cls Closure for @c iter.
3617 GCT_iterate_channels (struct CadetTunnel *t,
3618 GCT_ChannelIterator iter,
3621 struct ChanIterCls ctx;
3624 ctx.iter_cls = iter_cls;
3625 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
3626 &iterate_channels_cb,
3632 * Call #GCCH_debug() on a channel.
3634 * @param cls points to the log level to use
3636 * @param value the `struct CadetChannel` to dump
3637 * @return #GNUNET_OK (continue iteration)
3640 debug_channel (void *cls,
3644 const enum GNUNET_ErrorType *level = cls;
3645 struct CadetChannel *ch = value;
3647 GCCH_debug (ch, *level);
3652 #define LOG2(level, ...) GNUNET_log_from_nocheck (level, "cadet-tun", \
3657 * Log all possible info about the tunnel state.
3659 * @param t Tunnel to debug.
3660 * @param level Debug level to use.
3663 GCT_debug (const struct CadetTunnel *t,
3664 enum GNUNET_ErrorType level)
3666 #if ! defined(GNUNET_CULL_LOGGING)
3667 struct CadetTConnection *iter_c;
3670 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3672 __FILE__, __FUNCTION__, __LINE__);
3677 "TTT TUNNEL TOWARDS %s in estate %s tq_len: %u #cons: %u\n",
3679 estate2s (t->estate),
3681 GCT_count_any_connections (t));
3684 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
3688 "TTT connections:\n");
3689 for (iter_c = t->connection_ready_head; NULL != iter_c; iter_c = iter_c->next)
3690 GCC_debug (iter_c->cc,
3692 for (iter_c = t->connection_busy_head; NULL != iter_c; iter_c = iter_c->next)
3693 GCC_debug (iter_c->cc,
3697 "TTT TUNNEL END\n");
3702 /* end of gnunet-service-cadet_tunnels.c */