3 This file is part of GNUnet.
4 Copyright (C) 2013, 2017 GNUnet e.V.
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
23 * @file cadet/gnunet-service-cadet-new_tunnels.c
24 * @brief Information we track per tunnel.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
29 * - when managing connections, distinguish those that
30 * have (recently) had traffic from those that were
31 * never ready (or not recently)
34 #include "gnunet_util_lib.h"
35 #include "gnunet_signatures.h"
36 #include "cadet_protocol.h"
37 #include "cadet_path.h"
38 #include "gnunet-service-cadet-new.h"
39 #include "gnunet-service-cadet-new_channel.h"
40 #include "gnunet-service-cadet-new_connection.h"
41 #include "gnunet-service-cadet-new_tunnels.h"
42 #include "gnunet-service-cadet-new_peer.h"
43 #include "gnunet-service-cadet-new_paths.h"
47 * How long do we wait until tearing down an idle tunnel?
49 #define IDLE_DESTROY_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
53 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
55 struct CadetTunnelSkippedKey
60 struct CadetTunnelSkippedKey *next;
65 struct CadetTunnelSkippedKey *prev;
68 * When was this key stored (for timeout).
70 struct GNUNET_TIME_Absolute timestamp;
75 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
80 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
83 * Key number for a given HK.
90 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
92 struct CadetTunnelAxolotl
95 * A (double linked) list of stored message keys and associated header keys
96 * for "skipped" messages, i.e. messages that have not been
97 * received despite the reception of more recent messages, (head).
99 struct CadetTunnelSkippedKey *skipped_head;
102 * Skipped messages' keys DLL, tail.
104 struct CadetTunnelSkippedKey *skipped_tail;
107 * 32-byte root key which gets updated by DH ratchet.
109 struct GNUNET_CRYPTO_SymmetricSessionKey RK;
112 * 32-byte header key (send).
114 struct GNUNET_CRYPTO_SymmetricSessionKey HKs;
117 * 32-byte header key (recv)
119 struct GNUNET_CRYPTO_SymmetricSessionKey HKr;
122 * 32-byte next header key (send).
124 struct GNUNET_CRYPTO_SymmetricSessionKey NHKs;
127 * 32-byte next header key (recv).
129 struct GNUNET_CRYPTO_SymmetricSessionKey NHKr;
132 * 32-byte chain keys (used for forward-secrecy updating, send).
134 struct GNUNET_CRYPTO_SymmetricSessionKey CKs;
137 * 32-byte chain keys (used for forward-secrecy updating, recv).
139 struct GNUNET_CRYPTO_SymmetricSessionKey CKr;
142 * ECDH for key exchange (A0 / B0).
144 struct GNUNET_CRYPTO_EcdhePrivateKey *kx_0;
147 * ECDH Ratchet key (send).
149 struct GNUNET_CRYPTO_EcdhePrivateKey *DHRs;
152 * ECDH Ratchet key (recv).
154 struct GNUNET_CRYPTO_EcdhePublicKey DHRr;
157 * When does this ratchet expire and a new one is triggered.
159 struct GNUNET_TIME_Absolute ratchet_expiration;
162 * Number of elements in @a skipped_head <-> @a skipped_tail.
164 unsigned int skipped;
167 * Message number (reset to 0 with each new ratchet, next message to send).
172 * Message number (reset to 0 with each new ratchet, next message to recv).
177 * Previous message numbers (# of msgs sent under prev ratchet)
182 * True (#GNUNET_YES) if we have to send a new ratchet key in next msg.
187 * Number of messages recieved since our last ratchet advance.
188 * - If this counter = 0, we cannot send a new ratchet key in next msg.
189 * - If this counter > 0, we can (but don't yet have to) send a new key.
191 unsigned int ratchet_allowed;
194 * Number of messages recieved since our last ratchet advance.
195 * - If this counter = 0, we cannot send a new ratchet key in next msg.
196 * - If this counter > 0, we can (but don't yet have to) send a new key.
198 unsigned int ratchet_counter;
204 * Entry in list of connections used by tunnel, with metadata.
206 struct CadetTConnection
211 struct CadetTConnection *next;
216 struct CadetTConnection *prev;
221 struct CadetConnection *cc;
224 * Tunnel this connection belongs to.
226 struct CadetTunnel *t;
229 * Creation time, to keep oldest connection alive.
231 struct GNUNET_TIME_Absolute created;
234 * Connection throughput, to keep fastest connection alive.
241 * Struct used to save messages in a non-ready tunnel to send once connected.
243 struct CadetTunnelQueueEntry
246 * We are entries in a DLL
248 struct CadetTunnelQueueEntry *next;
251 * We are entries in a DLL
253 struct CadetTunnelQueueEntry *prev;
256 * Tunnel these messages belong in.
258 struct CadetTunnel *t;
261 * Continuation to call once sent (on the channel layer).
263 GNUNET_SCHEDULER_TaskCallback cont;
266 * Closure for @c cont.
271 * Envelope of message to send follows.
273 struct GNUNET_MQ_Envelope *env;
278 * Struct containing all information regarding a tunnel to a peer.
283 * Destination of the tunnel.
285 struct CadetPeer *destination;
288 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own
289 * ephemeral key changes.
291 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
294 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
296 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
299 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
301 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
306 struct CadetTunnelAxolotl ax;
309 * State of the tunnel connectivity.
311 enum CadetTunnelCState cstate;
314 * State of the tunnel encryption.
316 enum CadetTunnelEState estate;
319 * Task to start the rekey process.
321 struct GNUNET_SCHEDULER_Task *rekey_task;
324 * DLL of connections that are actively used to reach the destination peer.
326 struct CadetTConnection *connection_head;
329 * DLL of connections that are actively used to reach the destination peer.
331 struct CadetTConnection *connection_tail;
334 * Channels inside this tunnel. Maps
335 * `struct GCT_ChannelTunnelNumber` to a `struct CadetChannel`.
337 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
340 * Channel ID for the next created channel in this tunnel.
342 struct GCT_ChannelTunnelNumber next_chid;
345 * Queued messages, to transmit once tunnel gets connected.
347 struct CadetTunnelQueueEntry *tq_head;
350 * Queued messages, to transmit once tunnel gets connected.
352 struct CadetTunnelQueueEntry *tq_tail;
355 * Task scheduled if there are no more channels using the tunnel.
357 struct GNUNET_SCHEDULER_Task *destroy_task;
360 * Task to trim connections if too many are present.
362 struct GNUNET_SCHEDULER_Task *maintain_connections_task;
365 * Ephemeral message in the queue (to avoid queueing more than one).
367 struct CadetConnectionQueue *ephm_hKILL;
370 * Pong message in the queue.
372 struct CadetConnectionQueue *pong_hKILL;
375 * Number of connections in the @e connection_head DLL.
377 unsigned int num_connections;
380 * Number of entries in the @e tq_head DLL.
387 * Get the static string for the peer this tunnel is directed.
391 * @return Static string the destination peer's ID.
394 GCT_2s (const struct CadetTunnel *t)
401 GNUNET_snprintf (buf,
404 GCP_2s (t->destination));
410 * Return the peer to which this tunnel goes.
413 * @return the destination of the tunnel
416 GCT_get_destination (struct CadetTunnel *t)
418 return t->destination;
423 * Count channels of a tunnel.
425 * @param t Tunnel on which to count.
427 * @return Number of channels.
430 GCT_count_channels (struct CadetTunnel *t)
432 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
437 * Count all created connections of a tunnel. Not necessarily ready connections!
439 * @param t Tunnel on which to count.
441 * @return Number of connections created, either being established or ready.
444 GCT_count_any_connections (struct CadetTunnel *t)
446 return t->num_connections;
451 * Get the connectivity state of a tunnel.
455 * @return Tunnel's connectivity state.
457 enum CadetTunnelCState
458 GCT_get_cstate (struct CadetTunnel *t)
465 * Get the encryption state of a tunnel.
469 * @return Tunnel's encryption state.
471 enum CadetTunnelEState
472 GCT_get_estate (struct CadetTunnel *t)
479 * Add a channel to a tunnel.
483 * @return unique number identifying @a ch within @a t
485 struct GCT_ChannelTunnelNumber
486 GCT_add_channel (struct CadetTunnel *t,
487 struct CadetChannel *ch)
489 struct GCT_ChannelTunnelNumber ret;
492 chid = ntohl (t->next_chid.channel_in_tunnel);
494 GNUNET_CONTAINER_multihashmap32_get (t->channels,
497 GNUNET_assert (GNUNET_YES ==
498 GNUNET_CONTAINER_multihashmap32_put (t->channels,
501 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
502 t->next_chid.channel_in_tunnel = htonl (chid + 1);
503 ret.channel_in_tunnel = htonl (chid);
509 * This tunnel is no longer used, destroy it.
511 * @param cls the idle tunnel
514 destroy_tunnel (void *cls)
516 struct CadetTunnel *t = cls;
517 struct CadetTConnection *ct;
518 struct CadetTunnelQueueEntry *tqe;
520 t->destroy_task = NULL;
521 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (t->channels));
522 while (NULL != (ct = t->connection_head))
524 GNUNET_assert (ct->t == t);
525 GNUNET_CONTAINER_DLL_remove (t->connection_head,
528 GCC_destroy (ct->cc);
531 while (NULL != (tqe = t->tq_head))
533 GNUNET_CONTAINER_DLL_remove (t->tq_head,
536 GNUNET_MQ_discard (tqe->env);
539 GCP_drop_tunnel (t->destination,
541 GNUNET_CONTAINER_multihashmap32_destroy (t->channels);
542 if (NULL != t->maintain_connections_task)
544 GNUNET_SCHEDULER_cancel (t->maintain_connections_task);
545 t->maintain_connections_task = NULL;
552 * A connection is ready for transmission. Looks at our message queue
553 * and if there is a message, sends it out via the connection.
555 * @param cls the `struct CadetTConnection` that is ready
558 connection_ready_cb (void *cls)
560 struct CadetTConnection *ct = cls;
561 struct CadetTunnel *t = ct->t;
562 struct CadetTunnelQueueEntry *tq = t->tq_head;
565 return; /* no messages pending right now */
567 /* ready to send message 'tq' on tunnel 'ct' */
568 GNUNET_assert (t == tq->t);
569 GNUNET_CONTAINER_DLL_remove (t->tq_head,
572 GCC_transmit (ct->cc,
574 tq->cont (tq->cont_cls);
580 * Called when either we have a new connection, or a new message in the
581 * queue, or some existing connection has transmission capacity. Looks
582 * at our message queue and if there is a message, picks a connection
585 * @param t tunnel to process messages on
588 trigger_transmissions (struct CadetTunnel *t)
590 struct CadetTConnection *ct;
592 if (NULL == t->tq_head)
593 return; /* no messages pending right now */
594 for (ct = t->connection_head;
597 if (GNUNET_YES == GCC_is_ready (ct->cc))
600 return; /* no connections ready */
601 connection_ready_cb (ct);
606 * Function called to maintain the connections underlying our tunnel.
607 * Tries to maintain (incl. tear down) connections for the tunnel, and
608 * if there is a significant change, may trigger transmissions.
610 * Basically, needs to check if there are connections that perform
611 * badly, and if so eventually kill them and trigger a replacement.
612 * The strategy is to open one more connection than
613 * #DESIRED_CONNECTIONS_PER_TUNNEL, and then periodically kick out the
614 * least-performing one, and then inquire for new ones.
616 * @param cls the `struct CadetTunnel`
619 maintain_connections_cb (void *cls)
621 struct CadetTunnel *t = cls;
623 GNUNET_break (0); // FIXME: implement!
628 * Consider using the path @a p for the tunnel @a t.
629 * The tunnel destination is at offset @a off in path @a p.
631 * @param cls our tunnel
632 * @param path a path to our destination
633 * @param off offset of the destination on path @a path
634 * @return #GNUNET_YES (should keep iterating)
637 consider_path_cb (void *cls,
638 struct CadetPeerPath *path,
641 struct CadetTunnel *t = cls;
642 unsigned int min_length = UINT_MAX;
643 GNUNET_CONTAINER_HeapCostType max_desire = 0;
644 struct CadetTConnection *ct;
646 /* Check if we care about the new path. */
647 for (ct = t->connection_head;
651 struct CadetPeerPath *ps;
653 ps = GCC_get_path (ct->cc);
655 return GNUNET_YES; /* duplicate */
656 min_length = GNUNET_MIN (min_length,
657 GCPP_get_length (ps));
658 max_desire = GNUNET_MAX (max_desire,
659 GCPP_get_desirability (ps));
662 /* FIXME: not sure we should really just count
663 'num_connections' here, as they may all have
664 consistently failed to connect. */
666 /* We iterate by increasing path length; if we have enough paths and
667 this one is more than twice as long than what we are currently
668 using, then ignore all of these super-long ones! */
669 if ( (t->num_connections > DESIRED_CONNECTIONS_PER_TUNNEL) &&
670 (min_length * 2 < off) )
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Ignoring paths of length %u, they are way too long.\n",
677 /* If we have enough paths and this one looks no better, ignore it. */
678 if ( (t->num_connections >= DESIRED_CONNECTIONS_PER_TUNNEL) &&
679 (min_length < GCPP_get_length (path)) &&
680 (max_desire > GCPP_get_desirability (path)) )
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Ignoring path (%u/%llu) to %s, got something better already.\n",
684 GCPP_get_length (path),
685 (unsigned long long) GCPP_get_desirability (path),
686 GCP_2s (t->destination));
690 /* Path is interesting (better by some metric, or we don't have
691 enough paths yet). */
692 ct = GNUNET_new (struct CadetTConnection);
693 ct->created = GNUNET_TIME_absolute_get ();
695 ct->cc = GCC_create (t->destination,
697 &connection_ready_cb,
699 /* FIXME: schedule job to kill connection (and path?) if it takes
700 too long to get ready! (And track performance data on how long
701 other connections took with the tunnel!)
702 => Note: to be done within 'connection'-logic! */
703 GNUNET_CONTAINER_DLL_insert (t->connection_head,
706 t->num_connections++;
712 * Consider using the path @a p for the tunnel @a t.
713 * The tunnel destination is at offset @a off in path @a p.
715 * @param cls our tunnel
716 * @param path a path to our destination
717 * @param off offset of the destination on path @a path
720 GCT_consider_path (struct CadetTunnel *t,
721 struct CadetPeerPath *p,
724 (void) consider_path_cb (t,
731 * Create a tunnel to @a destionation. Must only be called
732 * from within #GCP_get_tunnel().
734 * @param destination where to create the tunnel to
735 * @return new tunnel to @a destination
738 GCT_create_tunnel (struct CadetPeer *destination)
740 struct CadetTunnel *t;
742 t = GNUNET_new (struct CadetTunnel);
743 t->destination = destination;
744 t->channels = GNUNET_CONTAINER_multihashmap32_create (8);
745 (void) GCP_iterate_paths (destination,
748 t->maintain_connections_task
749 = GNUNET_SCHEDULER_add_now (&maintain_connections_cb,
756 * Remove a channel from a tunnel.
760 * @param gid unique number identifying @a ch within @a t
763 GCT_remove_channel (struct CadetTunnel *t,
764 struct CadetChannel *ch,
765 struct GCT_ChannelTunnelNumber gid)
767 GNUNET_assert (GNUNET_YES ==
768 GNUNET_CONTAINER_multihashmap32_remove (t->channels,
769 ntohl (gid.channel_in_tunnel),
772 GNUNET_CONTAINER_multihashmap32_size (t->channels))
774 t->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
782 * Sends an already built message on a tunnel, encrypting it and
783 * choosing the best connection if not provided.
785 * @param message Message to send. Function modifies it.
786 * @param t Tunnel on which this message is transmitted.
787 * @param cont Continuation to call once message is really sent.
788 * @param cont_cls Closure for @c cont.
789 * @return Handle to cancel message. NULL if @c cont is NULL.
791 struct CadetTunnelQueueEntry *
792 GCT_send (struct CadetTunnel *t,
793 const struct GNUNET_MessageHeader *message,
794 GNUNET_SCHEDULER_TaskCallback cont,
797 struct CadetTunnelQueueEntry *q;
798 uint16_t payload_size;
800 payload_size = ntohs (message->size);
802 q = GNUNET_malloc (sizeof (*q) +
804 /* FIXME: encrypt 'message' to end of 'q' */
807 q->cont_cls = cont_cls;
808 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head,
811 /* FIXME: what about KX being ready? */
812 trigger_transmissions (t);
818 * Cancel a previously sent message while it's in the queue.
820 * ONLY can be called before the continuation given to the send
821 * function is called. Once the continuation is called, the message is
822 * no longer in the queue!
824 * @param q Handle to the queue entry to cancel.
827 GCT_send_cancel (struct CadetTunnelQueueEntry *q)
829 struct CadetTunnel *t = q->t;
831 GNUNET_CONTAINER_DLL_remove (t->tq_head,
839 * Iterate over all connections of a tunnel.
841 * @param t Tunnel whose connections to iterate.
842 * @param iter Iterator.
843 * @param iter_cls Closure for @c iter.
846 GCT_iterate_connections (struct CadetTunnel *t,
847 GCT_ConnectionIterator iter,
850 for (struct CadetTConnection *ct = t->connection_head;
859 * Closure for #iterate_channels_cb.
866 GCT_ChannelIterator iter;
869 * Closure for @e iter.
876 * Helper function for #GCT_iterate_channels.
878 * @param cls the `struct ChanIterCls`
880 * @param value a `struct CadetChannel`
884 iterate_channels_cb (void *cls,
888 struct ChanIterCls *ctx = cls;
889 struct CadetChannel *ch = value;
891 ctx->iter (ctx->iter_cls,
898 * Iterate over all channels of a tunnel.
900 * @param t Tunnel whose channels to iterate.
901 * @param iter Iterator.
902 * @param iter_cls Closure for @c iter.
905 GCT_iterate_channels (struct CadetTunnel *t,
906 GCT_ChannelIterator iter,
909 struct ChanIterCls ctx;
912 ctx.iter_cls = iter_cls;
913 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
914 &iterate_channels_cb,
921 * Call #GCCH_debug() on a channel.
923 * @param cls points to the log level to use
925 * @param value the `struct CadetChannel` to dump
926 * @return #GNUNET_OK (continue iteration)
929 debug_channel (void *cls,
933 const enum GNUNET_ErrorType *level = cls;
934 struct CadetChannel *ch = value;
936 GCCH_debug (ch, *level);
942 * Get string description for tunnel connectivity state.
944 * @param cs Tunnel state.
946 * @return String representation.
949 cstate2s (enum CadetTunnelCState cs)
955 case CADET_TUNNEL_NEW:
956 return "CADET_TUNNEL_NEW";
957 case CADET_TUNNEL_SEARCHING:
958 return "CADET_TUNNEL_SEARCHING";
959 case CADET_TUNNEL_WAITING:
960 return "CADET_TUNNEL_WAITING";
961 case CADET_TUNNEL_READY:
962 return "CADET_TUNNEL_READY";
963 case CADET_TUNNEL_SHUTDOWN:
964 return "CADET_TUNNEL_SHUTDOWN";
966 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
973 * Get string description for tunnel encryption state.
975 * @param es Tunnel state.
977 * @return String representation.
980 estate2s (enum CadetTunnelEState es)
986 case CADET_TUNNEL_KEY_UNINITIALIZED:
987 return "CADET_TUNNEL_KEY_UNINITIALIZED";
988 case CADET_TUNNEL_KEY_SENT:
989 return "CADET_TUNNEL_KEY_SENT";
990 case CADET_TUNNEL_KEY_PING:
991 return "CADET_TUNNEL_KEY_PING";
992 case CADET_TUNNEL_KEY_OK:
993 return "CADET_TUNNEL_KEY_OK";
994 case CADET_TUNNEL_KEY_REKEY:
995 return "CADET_TUNNEL_KEY_REKEY";
997 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
1003 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
1007 * Log all possible info about the tunnel state.
1009 * @param t Tunnel to debug.
1010 * @param level Debug level to use.
1013 GCT_debug (const struct CadetTunnel *t,
1014 enum GNUNET_ErrorType level)
1016 struct CadetTConnection *iter_c;
1019 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1021 __FILE__, __FUNCTION__, __LINE__);
1026 "TTT TUNNEL TOWARDS %s in cstate %s, estate %s tq_len: %u #cons: %u\n",
1028 cstate2s (t->cstate),
1029 estate2s (t->estate),
1031 t->num_connections);
1032 #if DUMP_KEYS_TO_STDERR
1033 ax_debug (t->ax, level);
1037 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
1041 "TTT connections:\n");
1042 for (iter_c = t->connection_head; NULL != iter_c; iter_c = iter_c->next)
1043 GCC_debug (iter_c->cc,
1047 "TTT TUNNEL END\n");
1051 /* end of gnunet-service-cadet-new_tunnels.c */