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 #include "gnunet_util_lib.h"
30 #include "gnunet_signatures.h"
31 #include "cadet_protocol.h"
32 #include "cadet_path.h"
33 #include "gnunet-service-cadet-new.h"
34 #include "gnunet-service-cadet-new_channel.h"
35 #include "gnunet-service-cadet-new_connection.h"
36 #include "gnunet-service-cadet-new_tunnels.h"
37 #include "gnunet-service-cadet-new_peer.h"
41 * How long do we wait until tearing down an idle tunnel?
43 #define IDLE_DESTROY_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
47 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
49 struct CadetTunnelSkippedKey
54 struct CadetTunnelSkippedKey *next;
59 struct CadetTunnelSkippedKey *prev;
62 * When was this key stored (for timeout).
64 struct GNUNET_TIME_Absolute timestamp;
69 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
74 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
77 * Key number for a given HK.
84 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
86 struct CadetTunnelAxolotl
89 * A (double linked) list of stored message keys and associated header keys
90 * for "skipped" messages, i.e. messages that have not been
91 * received despite the reception of more recent messages, (head).
93 struct CadetTunnelSkippedKey *skipped_head;
96 * Skipped messages' keys DLL, tail.
98 struct CadetTunnelSkippedKey *skipped_tail;
101 * 32-byte root key which gets updated by DH ratchet.
103 struct GNUNET_CRYPTO_SymmetricSessionKey RK;
106 * 32-byte header key (send).
108 struct GNUNET_CRYPTO_SymmetricSessionKey HKs;
111 * 32-byte header key (recv)
113 struct GNUNET_CRYPTO_SymmetricSessionKey HKr;
116 * 32-byte next header key (send).
118 struct GNUNET_CRYPTO_SymmetricSessionKey NHKs;
121 * 32-byte next header key (recv).
123 struct GNUNET_CRYPTO_SymmetricSessionKey NHKr;
126 * 32-byte chain keys (used for forward-secrecy updating, send).
128 struct GNUNET_CRYPTO_SymmetricSessionKey CKs;
131 * 32-byte chain keys (used for forward-secrecy updating, recv).
133 struct GNUNET_CRYPTO_SymmetricSessionKey CKr;
136 * ECDH for key exchange (A0 / B0).
138 struct GNUNET_CRYPTO_EcdhePrivateKey *kx_0;
141 * ECDH Ratchet key (send).
143 struct GNUNET_CRYPTO_EcdhePrivateKey *DHRs;
146 * ECDH Ratchet key (recv).
148 struct GNUNET_CRYPTO_EcdhePublicKey DHRr;
151 * When does this ratchet expire and a new one is triggered.
153 struct GNUNET_TIME_Absolute ratchet_expiration;
156 * Number of elements in @a skipped_head <-> @a skipped_tail.
158 unsigned int skipped;
161 * Message number (reset to 0 with each new ratchet, next message to send).
166 * Message number (reset to 0 with each new ratchet, next message to recv).
171 * Previous message numbers (# of msgs sent under prev ratchet)
176 * True (#GNUNET_YES) if we have to send a new ratchet key in next msg.
181 * Number of messages recieved since our last ratchet advance.
182 * - If this counter = 0, we cannot send a new ratchet key in next msg.
183 * - If this counter > 0, we can (but don't yet have to) send a new key.
185 unsigned int ratchet_allowed;
188 * Number of messages recieved since our last ratchet advance.
189 * - If this counter = 0, we cannot send a new ratchet key in next msg.
190 * - If this counter > 0, we can (but don't yet have to) send a new key.
192 unsigned int ratchet_counter;
198 * Entry in list of connections used by tunnel, with metadata.
200 struct CadetTConnection
205 struct CadetTConnection *next;
210 struct CadetTConnection *prev;
215 struct CadetConnection *c;
218 * Creation time, to keep oldest connection alive.
220 struct GNUNET_TIME_Absolute created;
223 * Connection throughput, to keep fastest connection alive.
230 * Struct used to save messages in a non-ready tunnel to send once connected.
232 struct CadetTunnelQueueEntry
235 * We are entries in a DLL
237 struct CadetTunnelQueueEntry *next;
240 * We are entries in a DLL
242 struct CadetTunnelQueueEntry *prev;
245 * Tunnel these messages belong in.
247 struct CadetTunnel *t;
250 * Continuation to call once sent (on the channel layer).
252 GNUNET_SCHEDULER_TaskCallback cont;
255 * Closure for @c cont.
260 * (Encrypted) message to send follows.
262 /* struct GNUNET_MessageHeader *msg; */
267 * Struct containing all information regarding a tunnel to a peer.
272 * Endpoint of the tunnel.
274 struct CadetPeer *peer;
277 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own
278 * ephemeral key changes.
280 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
283 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
285 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
288 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
290 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
295 struct CadetTunnelAxolotl ax;
298 * State of the tunnel connectivity.
300 enum CadetTunnelCState cstate;
303 * State of the tunnel encryption.
305 enum CadetTunnelEState estate;
308 * Task to start the rekey process.
310 struct GNUNET_SCHEDULER_Task *rekey_task;
313 * DLL of connections that are actively used to reach the destination peer.
315 struct CadetTConnection *connection_head;
318 * DLL of connections that are actively used to reach the destination peer.
320 struct CadetTConnection *connection_tail;
323 * Channels inside this tunnel. Maps
324 * `struct GCT_ChannelTunnelNumber` to a `struct CadetChannel`.
326 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
329 * Channel ID for the next created channel in this tunnel.
331 struct GCT_ChannelTunnelNumber next_chid;
334 * Queued messages, to transmit once tunnel gets connected.
336 struct CadetTunnelQueueEntry *tq_head;
339 * Queued messages, to transmit once tunnel gets connected.
341 struct CadetTunnelQueueEntry *tq_tail;
344 * Task scheduled if there are no more channels using the tunnel.
346 struct GNUNET_SCHEDULER_Task *destroy_task;
349 * Task to trim connections if too many are present.
351 struct GNUNET_SCHEDULER_Task *trim_connections_task;
354 * Ephemeral message in the queue (to avoid queueing more than one).
356 struct CadetConnectionQueue *ephm_hKILL;
359 * Pong message in the queue.
361 struct CadetConnectionQueue *pong_hKILL;
364 * Number of connections in the @e connection_head DLL.
366 unsigned int num_connections;
369 * Number of entries in the @e tq_head DLL.
376 * Get the static string for the peer this tunnel is directed.
380 * @return Static string the destination peer's ID.
383 GCT_2s (const struct CadetTunnel *t)
390 GNUNET_snprintf (buf,
399 * Return the peer to which this tunnel goes.
402 * @return the destination of the tunnel
405 GCT_get_destination (struct CadetTunnel *t)
412 * Count channels of a tunnel.
414 * @param t Tunnel on which to count.
416 * @return Number of channels.
419 GCT_count_channels (struct CadetTunnel *t)
421 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
426 * Count all created connections of a tunnel. Not necessarily ready connections!
428 * @param t Tunnel on which to count.
430 * @return Number of connections created, either being established or ready.
433 GCT_count_any_connections (struct CadetTunnel *t)
435 return t->num_connections;
440 * Get the connectivity state of a tunnel.
444 * @return Tunnel's connectivity state.
446 enum CadetTunnelCState
447 GCT_get_cstate (struct CadetTunnel *t)
454 * Get the encryption state of a tunnel.
458 * @return Tunnel's encryption state.
460 enum CadetTunnelEState
461 GCT_get_estate (struct CadetTunnel *t)
468 * Add a channel to a tunnel.
472 * @return unique number identifying @a ch within @a t
474 struct GCT_ChannelTunnelNumber
475 GCT_add_channel (struct CadetTunnel *t,
476 struct CadetChannel *ch)
478 struct GCT_ChannelTunnelNumber ret;
481 chid = ntohl (t->next_chid.channel_in_tunnel);
483 GNUNET_CONTAINER_multihashmap32_get (t->channels,
486 GNUNET_assert (GNUNET_YES ==
487 GNUNET_CONTAINER_multihashmap32_put (t->channels,
490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
491 t->next_chid.channel_in_tunnel = htonl (chid + 1);
492 ret.channel_in_tunnel = htonl (chid);
498 * This tunnel is no longer used, destroy it.
500 * @param cls the idle tunnel
503 destroy_tunnel (void *cls)
505 struct CadetTunnel *t = cls;
507 t->destroy_task = NULL;
510 GCP_drop_tunnel (t->peer,
517 * Create a tunnel to @a destionation. Must only be called
518 * from within #GCP_get_tunnel().
520 * @param destination where to create the tunnel to
521 * @return new tunnel to @a destination
524 GCT_create_tunnel (struct CadetPeer *destination)
526 struct CadetTunnel *t;
528 t = GNUNET_new (struct CadetTunnel);
529 t->peer = destination;
530 t->channels = GNUNET_CONTAINER_multihashmap32_create (8);
537 * Remove a channel from a tunnel.
541 * @param gid unique number identifying @a ch within @a t
544 GCT_remove_channel (struct CadetTunnel *t,
545 struct CadetChannel *ch,
546 struct GCT_ChannelTunnelNumber gid)
548 GNUNET_assert (GNUNET_YES ==
549 GNUNET_CONTAINER_multihashmap32_remove (t->channels,
550 ntohl (gid.channel_in_tunnel),
553 GNUNET_CONTAINER_multihashmap32_size (t->channels))
555 t->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
563 * Sends an already built message on a tunnel, encrypting it and
564 * choosing the best connection if not provided.
566 * @param message Message to send. Function modifies it.
567 * @param t Tunnel on which this message is transmitted.
568 * @param cont Continuation to call once message is really sent.
569 * @param cont_cls Closure for @c cont.
570 * @return Handle to cancel message. NULL if @c cont is NULL.
572 struct CadetTunnelQueueEntry *
573 GCT_send (struct CadetTunnel *t,
574 const struct GNUNET_MessageHeader *message,
575 GNUNET_SCHEDULER_TaskCallback cont,
578 struct CadetTunnelQueueEntry *q;
579 uint16_t payload_size;
581 payload_size = ntohs (message->size);
583 q = GNUNET_malloc (sizeof (*q) +
585 /* FIXME: encrypt 'message' to end of 'q' */
588 q->cont_cls = cont_cls;
589 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head,
592 /* FIXME: initiate transmission process! */
598 * Cancel a previously sent message while it's in the queue.
600 * ONLY can be called before the continuation given to the send
601 * function is called. Once the continuation is called, the message is
602 * no longer in the queue!
604 * @param q Handle to the queue entry to cancel.
607 GCT_send_cancel (struct CadetTunnelQueueEntry *q)
609 struct CadetTunnel *t = q->t;
611 GNUNET_CONTAINER_DLL_remove (t->tq_head,
619 * Iterate over all connections of a tunnel.
621 * @param t Tunnel whose connections to iterate.
622 * @param iter Iterator.
623 * @param iter_cls Closure for @c iter.
626 GCT_iterate_connections (struct CadetTunnel *t,
627 GCT_ConnectionIterator iter,
630 for (struct CadetTConnection *ct = t->connection_head;
639 * Closure for #iterate_channels_cb.
646 GCT_ChannelIterator iter;
649 * Closure for @e iter.
656 * Helper function for #GCT_iterate_channels.
658 * @param cls the `struct ChanIterCls`
660 * @param value a `struct CadetChannel`
664 iterate_channels_cb (void *cls,
668 struct ChanIterCls *ctx = cls;
669 struct CadetChannel *ch = value;
671 ctx->iter (ctx->iter_cls,
678 * Iterate over all channels of a tunnel.
680 * @param t Tunnel whose channels to iterate.
681 * @param iter Iterator.
682 * @param iter_cls Closure for @c iter.
685 GCT_iterate_channels (struct CadetTunnel *t,
686 GCT_ChannelIterator iter,
689 struct ChanIterCls ctx;
692 ctx.iter_cls = iter_cls;
693 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
694 &iterate_channels_cb,
701 * Call #GCCH_debug() on a channel.
703 * @param cls points to the log level to use
705 * @param value the `struct CadetChannel` to dump
706 * @return #GNUNET_OK (continue iteration)
709 debug_channel (void *cls,
713 const enum GNUNET_ErrorType *level = cls;
714 struct CadetChannel *ch = value;
716 GCCH_debug (ch, *level);
722 * Get string description for tunnel connectivity state.
724 * @param cs Tunnel state.
726 * @return String representation.
729 cstate2s (enum CadetTunnelCState cs)
735 case CADET_TUNNEL_NEW:
736 return "CADET_TUNNEL_NEW";
737 case CADET_TUNNEL_SEARCHING:
738 return "CADET_TUNNEL_SEARCHING";
739 case CADET_TUNNEL_WAITING:
740 return "CADET_TUNNEL_WAITING";
741 case CADET_TUNNEL_READY:
742 return "CADET_TUNNEL_READY";
743 case CADET_TUNNEL_SHUTDOWN:
744 return "CADET_TUNNEL_SHUTDOWN";
746 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
753 * Get string description for tunnel encryption state.
755 * @param es Tunnel state.
757 * @return String representation.
760 estate2s (enum CadetTunnelEState es)
766 case CADET_TUNNEL_KEY_UNINITIALIZED:
767 return "CADET_TUNNEL_KEY_UNINITIALIZED";
768 case CADET_TUNNEL_KEY_SENT:
769 return "CADET_TUNNEL_KEY_SENT";
770 case CADET_TUNNEL_KEY_PING:
771 return "CADET_TUNNEL_KEY_PING";
772 case CADET_TUNNEL_KEY_OK:
773 return "CADET_TUNNEL_KEY_OK";
774 case CADET_TUNNEL_KEY_REKEY:
775 return "CADET_TUNNEL_KEY_REKEY";
777 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
783 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
787 * Log all possible info about the tunnel state.
789 * @param t Tunnel to debug.
790 * @param level Debug level to use.
793 GCT_debug (const struct CadetTunnel *t,
794 enum GNUNET_ErrorType level)
796 struct CadetTConnection *iter_c;
799 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
801 __FILE__, __FUNCTION__, __LINE__);
806 "TTT TUNNEL TOWARDS %s in cstate %s, estate %s tq_len: %u #cons: %u\n",
808 cstate2s (t->cstate),
809 estate2s (t->estate),
812 #if DUMP_KEYS_TO_STDERR
813 ax_debug (t->ax, level);
817 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
821 "TTT connections:\n");
822 for (iter_c = t->connection_head; NULL != iter_c; iter_c = iter_c->next)
823 GCC_debug (iter_c->c, level);
830 /* end of gnunet-service-cadet-new_tunnels.c */