2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 #include "gnunet_util_lib.h"
24 #include "gnunet_signatures.h"
25 #include "gnunet_statistics_service.h"
27 #include "cadet_protocol.h"
28 #include "cadet_path.h"
30 #include "gnunet-service-cadet_tunnel.h"
31 #include "gnunet-service-cadet_connection.h"
32 #include "gnunet-service-cadet_channel.h"
33 #include "gnunet-service-cadet_peer.h"
35 #define LOG(level, ...) GNUNET_log_from(level,"cadet-tun",__VA_ARGS__)
36 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
38 #define REKEY_WAIT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
40 #if !defined(GNUNET_CULL_LOGGING)
41 #define DUMP_KEYS_TO_STDERR GNUNET_YES
43 #define DUMP_KEYS_TO_STDERR GNUNET_NO
46 /******************************************************************************/
47 /******************************** STRUCTS **********************************/
48 /******************************************************************************/
52 struct CadetTChannel *next;
53 struct CadetTChannel *prev;
54 struct CadetChannel *ch;
59 * Connection list and metadata.
61 struct CadetTConnection
66 struct CadetTConnection *next;
71 struct CadetTConnection *prev;
76 struct CadetConnection *c;
79 * Creation time, to keep oldest connection alive.
81 struct GNUNET_TIME_Absolute created;
84 * Connection throughput, to keep fastest connection alive.
90 * Structure used during a Key eXchange.
92 struct CadetTunnelKXCtx
95 * Encryption ("our") old "confirmed" key, for encrypting traffic sent by us
96 * end before the key exchange is finished or times out.
98 struct GNUNET_CRYPTO_SymmetricSessionKey e_key_old;
101 * Decryption ("their") old "confirmed" key, for decrypting traffic sent by
102 * the other end before the key exchange started.
104 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old;
107 * Same as @c e_key_old, for the case of two simultaneous KX.
108 * This can happen if cadet decides to start a re-key while the peer has also
109 * started its re-key (due to network delay this is impossible to avoid).
110 * In this case, the key material generated with the peer's old ephemeral
111 * *might* (but doesn't have to) be incorrect.
112 * Since no more than two re-keys can happen simultaneously, this is enough.
114 struct GNUNET_CRYPTO_SymmetricSessionKey e_key_old2;
117 * Same as @c d_key_old, for the case described in @c e_key_old2.
119 struct GNUNET_CRYPTO_SymmetricSessionKey d_key_old2;
122 * Challenge to send and expect in the PONG.
127 * When the rekey started. One minute after this the new key will be used.
129 struct GNUNET_TIME_Absolute rekey_start_time;
132 * Task for delayed destruction of the Key eXchange context, to allow delayed
133 * messages with the old key to be decrypted successfully.
135 struct GNUNET_SCHEDULER_Task * finish_task;
139 * Struct containing all information regarding a tunnel to a peer.
144 * Endpoint of the tunnel.
146 struct CadetPeer *peer;
149 * State of the tunnel connectivity.
151 enum CadetTunnelCState cstate;
154 * State of the tunnel encryption.
156 enum CadetTunnelEState estate;
159 * Key eXchange context.
161 struct CadetTunnelKXCtx *kx_ctx;
164 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own ephemeral
167 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
170 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
172 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
175 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
177 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
180 * Task to start the rekey process.
182 struct GNUNET_SCHEDULER_Task * rekey_task;
185 * Paths that are actively used to reach the destination peer.
187 struct CadetTConnection *connection_head;
188 struct CadetTConnection *connection_tail;
191 * Next connection number.
196 * Channels inside this tunnel.
198 struct CadetTChannel *channel_head;
199 struct CadetTChannel *channel_tail;
202 * Channel ID for the next created channel.
204 CADET_ChannelNumber next_chid;
207 * Destroy flag: if true, destroy on last message.
209 struct GNUNET_SCHEDULER_Task * destroy_task;
212 * Queued messages, to transmit once tunnel gets connected.
214 struct CadetTunnelDelayed *tq_head;
215 struct CadetTunnelDelayed *tq_tail;
218 * Task to trim connections if too many are present.
220 struct GNUNET_SCHEDULER_Task * trim_connections_task;
223 * Ephemeral message in the queue (to avoid queueing more than one).
225 struct CadetConnectionQueue *ephm_h;
228 * Pong message in the queue.
230 struct CadetConnectionQueue *pong_h;
235 * Struct used to save messages in a non-ready tunnel to send once connected.
237 struct CadetTunnelDelayed
242 struct CadetTunnelDelayed *next;
243 struct CadetTunnelDelayed *prev;
248 struct CadetTunnel *t;
251 * Tunnel queue given to the channel to cancel request. Update on send_queued.
253 struct CadetTunnelQueue *tq;
258 /* struct GNUNET_MessageHeader *msg; */
263 * Handle for messages queued but not yet sent.
265 struct CadetTunnelQueue
268 * Connection queue handle, to cancel if necessary.
270 struct CadetConnectionQueue *cq;
273 * Handle in case message hasn't been given to a connection yet.
275 struct CadetTunnelDelayed *tqd;
278 * Continuation to call once sent.
283 * Closure for @c cont.
289 /******************************************************************************/
290 /******************************* GLOBALS ***********************************/
291 /******************************************************************************/
294 * Global handle to the statistics service.
296 extern struct GNUNET_STATISTICS_Handle *stats;
299 * Local peer own ID (memory efficient handle).
301 extern GNUNET_PEER_Id myid;
304 * Local peer own ID (full value).
306 extern struct GNUNET_PeerIdentity my_full_id;
310 * Don't try to recover tunnels if shutting down.
312 extern int shutting_down;
316 * Set of all tunnels, in order to trigger a new exchange on rekey.
317 * Indexed by peer's ID.
319 static struct GNUNET_CONTAINER_MultiPeerMap *tunnels;
322 * Default TTL for payload packets.
324 static unsigned long long default_ttl;
329 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
332 * Own ephemeral private key.
334 static struct GNUNET_CRYPTO_EcdhePrivateKey *my_ephemeral_key;
337 * Cached message used to perform a key exchange.
339 static struct GNUNET_CADET_KX_Ephemeral kx_msg;
342 * Task to generate a new ephemeral key.
344 static struct GNUNET_SCHEDULER_Task * rekey_task;
349 static struct GNUNET_TIME_Relative rekey_period;
351 /******************************************************************************/
352 /******************************** STATIC ***********************************/
353 /******************************************************************************/
356 * Get string description for tunnel connectivity state.
358 * @param cs Tunnel state.
360 * @return String representation.
363 cstate2s (enum CadetTunnelCState cs)
369 case CADET_TUNNEL_NEW:
370 return "CADET_TUNNEL_NEW";
371 case CADET_TUNNEL_SEARCHING:
372 return "CADET_TUNNEL_SEARCHING";
373 case CADET_TUNNEL_WAITING:
374 return "CADET_TUNNEL_WAITING";
375 case CADET_TUNNEL_READY:
376 return "CADET_TUNNEL_READY";
377 case CADET_TUNNEL_SHUTDOWN:
378 return "CADET_TUNNEL_SHUTDOWN";
380 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
388 * Get string description for tunnel encryption state.
390 * @param es Tunnel state.
392 * @return String representation.
395 estate2s (enum CadetTunnelEState es)
401 case CADET_TUNNEL_KEY_UNINITIALIZED:
402 return "CADET_TUNNEL_KEY_UNINITIALIZED";
403 case CADET_TUNNEL_KEY_SENT:
404 return "CADET_TUNNEL_KEY_SENT";
405 case CADET_TUNNEL_KEY_PING:
406 return "CADET_TUNNEL_KEY_PING";
407 case CADET_TUNNEL_KEY_OK:
408 return "CADET_TUNNEL_KEY_OK";
409 case CADET_TUNNEL_KEY_REKEY:
410 return "CADET_TUNNEL_KEY_REKEY";
412 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
420 * @brief Check if tunnel is ready to send traffic.
422 * Tunnel must be connected and with encryption correctly set up.
424 * @param t Tunnel to check.
426 * @return #GNUNET_YES if ready, #GNUNET_NO otherwise
429 is_ready (struct CadetTunnel *t)
433 GCT_debug (t, GNUNET_ERROR_TYPE_DEBUG);
434 ready = CADET_TUNNEL_READY == t->cstate
435 && (CADET_TUNNEL_KEY_OK == t->estate
436 || CADET_TUNNEL_KEY_REKEY == t->estate);
437 ready = ready || GCT_is_loopback (t);
443 * Check if a key is invalid (NULL pointer or all 0)
445 * @param key Key to check.
447 * @return #GNUNET_YES if key is null, #GNUNET_NO if exists and is not 0.
450 is_key_null (struct GNUNET_CRYPTO_SymmetricSessionKey *key)
452 struct GNUNET_CRYPTO_SymmetricSessionKey null_key;
457 memset (&null_key, 0, sizeof (null_key));
458 if (0 == memcmp (key, &null_key, sizeof (null_key)))
465 * Ephemeral key message purpose size.
467 * @return Size of the part of the ephemeral key message that must be signed.
470 ephemeral_purpose_size (void)
472 return sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
473 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
474 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
475 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
476 sizeof (struct GNUNET_PeerIdentity);
481 * Size of the encrypted part of a ping message.
483 * @return Size of the encrypted part of a ping message.
486 ping_encryption_size (void)
488 return sizeof (uint32_t);
493 * Get the channel's buffer. ONLY FOR NON-LOOPBACK CHANNELS!!
495 * @param tch Tunnel's channel handle.
497 * @return Amount of messages the channel can still buffer towards the client.
500 get_channel_buffer (const struct CadetTChannel *tch)
504 /* If channel is incoming, is terminal in the FWD direction and fwd is YES */
505 fwd = GCCH_is_terminal (tch->ch, GNUNET_YES);
507 return GCCH_get_buffer (tch->ch, fwd);
512 * Get the channel's allowance status.
514 * @param tch Tunnel's channel handle.
516 * @return #GNUNET_YES if we allowed the client to send data to us.
519 get_channel_allowed (const struct CadetTChannel *tch)
523 /* If channel is outgoing, is origin in the FWD direction and fwd is YES */
524 fwd = GCCH_is_origin (tch->ch, GNUNET_YES);
526 return GCCH_get_allowed (tch->ch, fwd);
531 * Get the connection's buffer.
533 * @param tc Tunnel's connection handle.
535 * @return Amount of messages the connection can still buffer.
538 get_connection_buffer (const struct CadetTConnection *tc)
542 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
543 fwd = GCC_is_origin (tc->c, GNUNET_YES);
545 return GCC_get_buffer (tc->c, fwd);
550 * Get the connection's allowance.
552 * @param tc Tunnel's connection handle.
554 * @return Amount of messages we have allowed the next peer to send us.
557 get_connection_allowed (const struct CadetTConnection *tc)
561 /* If connection is outgoing, is origin in the FWD direction and fwd is YES */
562 fwd = GCC_is_origin (tc->c, GNUNET_YES);
564 return GCC_get_allowed (tc->c, fwd);
569 * Check that a ephemeral key message s well formed and correctly signed.
571 * @param t Tunnel on which the message came.
572 * @param msg The ephemeral key message.
574 * @return GNUNET_OK if message is fine, GNUNET_SYSERR otherwise.
577 check_ephemeral (struct CadetTunnel *t,
578 const struct GNUNET_CADET_KX_Ephemeral *msg)
580 /* Check message size */
581 if (ntohs (msg->header.size) != sizeof (struct GNUNET_CADET_KX_Ephemeral))
582 return GNUNET_SYSERR;
584 /* Check signature size */
585 if (ntohl (msg->purpose.size) != ephemeral_purpose_size ())
586 return GNUNET_SYSERR;
589 if (0 != memcmp (&msg->origin_identity,
590 GCP_get_id (t->peer),
591 sizeof (struct GNUNET_PeerIdentity)))
592 return GNUNET_SYSERR;
594 /* Check signature */
596 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_CADET_KX,
599 &msg->origin_identity.public_key))
600 return GNUNET_SYSERR;
607 * Select the best key to use for encryption (send), based on KX status.
609 * Normally, return the current key. If there is a KX in progress and the old
610 * key is fresh enough, return the old key.
612 * @param t Tunnel to choose the key from.
614 * @return The optimal key to encrypt/hmac outgoing traffic.
616 static const struct GNUNET_CRYPTO_SymmetricSessionKey *
617 select_key (const struct CadetTunnel *t)
619 const struct GNUNET_CRYPTO_SymmetricSessionKey *key;
621 if (NULL != t->kx_ctx
622 && NULL == t->kx_ctx->finish_task)
624 struct GNUNET_TIME_Relative age;
626 age = GNUNET_TIME_absolute_get_duration (t->kx_ctx->rekey_start_time);
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 " key exchange in progress, started %s ago\n",
629 GNUNET_STRINGS_relative_time_to_string (age, GNUNET_YES));
630 // FIXME make duration of old keys configurable
631 if (age.rel_value_us < GNUNET_TIME_UNIT_MINUTES.rel_value_us)
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " using old key\n");
634 key = &t->kx_ctx->e_key_old;
638 LOG (GNUNET_ERROR_TYPE_DEBUG, " using new key (old key too old)\n");
644 LOG (GNUNET_ERROR_TYPE_DEBUG, " no KX: using current key\n");
654 * @param plaintext Content to HMAC.
655 * @param size Size of @c plaintext.
656 * @param iv Initialization vector for the message.
657 * @param key Key to use.
658 * @param hmac[out] Destination to store the HMAC.
661 t_hmac (const void *plaintext, size_t size,
662 uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
663 struct GNUNET_CADET_Hash *hmac)
665 static const char ctx[] = "cadet authentication key";
666 struct GNUNET_CRYPTO_AuthKey auth_key;
667 struct GNUNET_HashCode hash;
669 #if DUMP_KEYS_TO_STDERR
670 LOG (GNUNET_ERROR_TYPE_INFO, " HMAC with key %s\n",
671 GNUNET_h2s ((struct GNUNET_HashCode *) key));
673 GNUNET_CRYPTO_hmac_derive_key (&auth_key, key,
678 /* Two step: CADET_Hash is only 256 bits, HashCode is 512. */
679 GNUNET_CRYPTO_hmac (&auth_key, plaintext, size, &hash);
680 memcpy (hmac, &hash, sizeof (*hmac));
685 * Encrypt daforce_newest_keyta with the tunnel key.
687 * @param t Tunnel whose key to use.
688 * @param dst Destination for the encrypted data.
689 * @param src Source of the plaintext. Can overlap with @c dst.
690 * @param size Size of the plaintext.
691 * @param iv Initialization Vector to use.
692 * @param force_newest_key Force the use of the newest key, otherwise
693 * CADET will use the old key when allowed.
694 * This can happen in the case when a KX is going on
695 * and the old one hasn't expired.
698 t_encrypt (struct CadetTunnel *t, void *dst, const void *src,
699 size_t size, uint32_t iv, int force_newest_key)
701 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
702 const struct GNUNET_CRYPTO_SymmetricSessionKey *key;
705 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt start\n");
707 key = GNUNET_YES == force_newest_key ? &t->e_key : select_key (t);
708 #if DUMP_KEYS_TO_STDERR
709 LOG (GNUNET_ERROR_TYPE_INFO, " ENC with key %s\n",
710 GNUNET_h2s ((struct GNUNET_HashCode *) key));
712 GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
713 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt IV derived\n");
714 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, size, key, &siv, dst);
715 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_encrypt end\n");
722 * Decrypt and verify data with the appropriate tunnel key.
724 * @param key Key to use.
725 * @param dst Destination for the plaintext.
726 * @param src Source of the encrypted data. Can overlap with @c dst.
727 * @param size Size of the encrypted data.
728 * @param iv Initialization Vector to use.
730 * @return Size of the decrypted data, -1 if an error was encountered.
733 decrypt (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
734 void *dst, const void *src, size_t size, uint32_t iv)
736 struct GNUNET_CRYPTO_SymmetricInitializationVector siv;
739 LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt start\n");
740 LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt iv\n");
741 GNUNET_CRYPTO_symmetric_derive_iv (&siv, key, &iv, sizeof (iv), NULL);
742 LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt iv done\n");
743 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, size, key, &siv, dst);
744 LOG (GNUNET_ERROR_TYPE_DEBUG, " decrypt end\n");
751 * Decrypt and verify data with the most recent tunnel key.
753 * @param t Tunnel whose key to use.
754 * @param dst Destination for the plaintext.
755 * @param src Source of the encrypted data. Can overlap with @c dst.
756 * @param size Size of the encrypted data.
757 * @param iv Initialization Vector to use.
759 * @return Size of the decrypted data, -1 if an error was encountered.
762 t_decrypt (struct CadetTunnel *t, void *dst, const void *src,
763 size_t size, uint32_t iv)
767 #if DUMP_KEYS_TO_STDERR
768 LOG (GNUNET_ERROR_TYPE_DEBUG, " t_decrypt with %s\n",
769 GNUNET_h2s ((struct GNUNET_HashCode *) &t->d_key));
771 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
773 GNUNET_STATISTICS_update (stats, "# non decryptable data", 1, GNUNET_NO);
774 LOG (GNUNET_ERROR_TYPE_WARNING,
775 "got data on %s without a valid key\n",
777 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
781 out_size = decrypt (&t->d_key, dst, src, size, iv);
788 * Decrypt and verify data with the appropriate tunnel key and verify that the
789 * data has not been altered since it was sent by the remote peer.
791 * @param t Tunnel whose key to use.
792 * @param dst Destination for the plaintext.
793 * @param src Source of the encrypted data. Can overlap with @c dst.
794 * @param size Size of the encrypted data.
795 * @param iv Initialization Vector to use.
796 * @param msg_hmac HMAC of the message, cannot be NULL.
798 * @return Size of the decrypted data, -1 if an error was encountered.
801 t_decrypt_and_validate (struct CadetTunnel *t,
802 void *dst, const void *src,
803 size_t size, uint32_t iv,
804 const struct GNUNET_CADET_Hash *msg_hmac)
806 struct GNUNET_CRYPTO_SymmetricSessionKey *key;
807 struct GNUNET_CADET_Hash hmac;
810 /* Try primary (newest) key */
812 decrypted_size = decrypt (key, dst, src, size, iv);
813 t_hmac (src, size, iv, key, &hmac);
814 if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
815 return decrypted_size;
817 /* If no key exchange is going on, we just failed. */
818 if (NULL == t->kx_ctx)
820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821 "Failed checksum validation on tunnel %s with no KX\n",
823 GNUNET_STATISTICS_update (stats, "# wrong HMAC no KX", 1, GNUNET_NO);
827 /* Try secondary key, from previous KX period. */
828 key = &t->kx_ctx->d_key_old;
829 decrypted_size = decrypt (key, dst, src, size, iv);
830 t_hmac (src, size, iv, key, &hmac);
831 if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
832 return decrypted_size;
834 /* Hail Mary, try tertiary, key, in case of parallel re-keys. */
835 key = &t->kx_ctx->d_key_old2;
836 decrypted_size = decrypt (key, dst, src, size, iv);
837 t_hmac (src, size, iv, key, &hmac);
838 if (0 == memcmp (msg_hmac, &hmac, sizeof (hmac)))
839 return decrypted_size;
841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
842 "Failed checksum validation on tunnel %s with KX\n",
844 GNUNET_STATISTICS_update (stats, "# wrong HMAC with KX", 1, GNUNET_NO);
850 * Create key material by doing ECDH on the local and remote ephemeral keys.
852 * @param key_material Where to store the key material.
853 * @param ephemeral_key Peer's public ephemeral key.
856 derive_key_material (struct GNUNET_HashCode *key_material,
857 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key)
860 GNUNET_CRYPTO_ecc_ecdh (my_ephemeral_key,
870 * Create a symmetic key from the identities of both ends and the key material
873 * @param key Destination for the generated key.
874 * @param sender ID of the peer that will encrypt with @c key.
875 * @param receiver ID of the peer that will decrypt with @c key.
876 * @param key_material Hash created with ECDH with the ephemeral keys.
879 derive_symmertic (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
880 const struct GNUNET_PeerIdentity *sender,
881 const struct GNUNET_PeerIdentity *receiver,
882 const struct GNUNET_HashCode *key_material)
884 const char salt[] = "CADET kx salt";
886 GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
888 key_material, sizeof (struct GNUNET_HashCode),
889 sender, sizeof (struct GNUNET_PeerIdentity),
890 receiver, sizeof (struct GNUNET_PeerIdentity),
896 * Derive the tunnel's keys using our own and the peer's ephemeral keys.
898 * @param t Tunnel for which to create the keys.
901 create_keys (struct CadetTunnel *t)
903 struct GNUNET_HashCode km;
905 derive_key_material (&km, &t->peers_ephemeral_key);
906 derive_symmertic (&t->e_key, &my_full_id, GCP_get_id (t->peer), &km);
907 derive_symmertic (&t->d_key, GCP_get_id (t->peer), &my_full_id, &km);
908 #if DUMP_KEYS_TO_STDERR
909 LOG (GNUNET_ERROR_TYPE_INFO, "ME: %s\n",
910 GNUNET_h2s ((struct GNUNET_HashCode *) &kx_msg.ephemeral_key));
911 LOG (GNUNET_ERROR_TYPE_INFO, "PE: %s\n",
912 GNUNET_h2s ((struct GNUNET_HashCode *) &t->peers_ephemeral_key));
913 LOG (GNUNET_ERROR_TYPE_INFO, "KM: %s\n", GNUNET_h2s (&km));
914 LOG (GNUNET_ERROR_TYPE_INFO, "EK: %s\n",
915 GNUNET_h2s ((struct GNUNET_HashCode *) &t->e_key));
916 LOG (GNUNET_ERROR_TYPE_INFO, "DK: %s\n",
917 GNUNET_h2s ((struct GNUNET_HashCode *) &t->d_key));
923 * Create a new Key eXchange context for the tunnel.
925 * If the old keys were verified, keep them for old traffic. Create a new KX
926 * timestamp and a new nonce.
928 * @param t Tunnel for which to create the KX ctx.
931 create_kx_ctx (struct CadetTunnel *t)
933 LOG (GNUNET_ERROR_TYPE_INFO, " new kx ctx for %s\n", GCT_2s (t));
935 if (NULL != t->kx_ctx)
937 if (NULL != t->kx_ctx->finish_task)
939 LOG (GNUNET_ERROR_TYPE_INFO, " resetting exisiting finish task\n");
940 GNUNET_SCHEDULER_cancel (t->kx_ctx->finish_task);
941 t->kx_ctx->finish_task = NULL;
946 t->kx_ctx = GNUNET_new (struct CadetTunnelKXCtx);
947 t->kx_ctx->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
951 if (CADET_TUNNEL_KEY_OK == t->estate)
953 LOG (GNUNET_ERROR_TYPE_INFO, " backing up keys\n");
954 t->kx_ctx->d_key_old = t->d_key;
955 t->kx_ctx->e_key_old = t->e_key;
958 LOG (GNUNET_ERROR_TYPE_INFO, " old keys not valid, not saving\n");
959 t->kx_ctx->rekey_start_time = GNUNET_TIME_absolute_get ();
965 * @brief Finish the Key eXchange and destroy the old keys.
967 * @param cls Closure (Tunnel for which to finish the KX).
968 * @param tc Task context.
971 finish_kx (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
973 struct CadetTunnel *t = cls;
975 LOG (GNUNET_ERROR_TYPE_INFO, "finish KX for %s\n", GCT_2s (t));
977 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
979 LOG (GNUNET_ERROR_TYPE_INFO, " shutdown\n");
983 GNUNET_free (t->kx_ctx);
989 * Destroy a Key eXchange context for the tunnel. This function only schedules
990 * the destruction, the freeing of the memory (and clearing of old key material)
991 * happens after a delay!
993 * @param t Tunnel whose KX ctx to destroy.
996 destroy_kx_ctx (struct CadetTunnel *t)
998 struct GNUNET_TIME_Relative delay;
1000 if (NULL == t->kx_ctx || NULL != t->kx_ctx->finish_task)
1003 if (is_key_null (&t->kx_ctx->e_key_old))
1005 t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_now (finish_kx, t);
1009 delay = GNUNET_TIME_relative_divide (rekey_period, 4);
1010 delay = GNUNET_TIME_relative_min (delay, GNUNET_TIME_UNIT_MINUTES);
1012 t->kx_ctx->finish_task = GNUNET_SCHEDULER_add_delayed (delay, finish_kx, t);
1018 * Pick a connection on which send the next data message.
1020 * @param t Tunnel on which to send the message.
1022 * @return The connection on which to send the next message.
1024 static struct CadetConnection *
1025 tunnel_get_connection (struct CadetTunnel *t)
1027 struct CadetTConnection *iter;
1028 struct CadetConnection *best;
1030 unsigned int lowest_q;
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GCT_2s (t));
1034 lowest_q = UINT_MAX;
1035 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1037 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
1038 GCC_2s (iter->c), GCC_get_state (iter->c));
1039 if (CADET_CONNECTION_READY == GCC_get_state (iter->c))
1041 qn = GCC_get_qn (iter->c, GCC_is_origin (iter->c, GNUNET_YES));
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " selected: connection %s\n", GCC_2s (best));
1056 * Callback called when a queued message is sent.
1058 * Calculates the average time and connection packet tracking.
1060 * @param cls Closure (TunnelQueue handle).
1061 * @param c Connection this message was on.
1062 * @param q Connection queue handle (unused).
1063 * @param type Type of message sent.
1064 * @param fwd Was this a FWD going message?
1065 * @param size Size of the message.
1068 tun_message_sent (void *cls,
1069 struct CadetConnection *c,
1070 struct CadetConnectionQueue *q,
1071 uint16_t type, int fwd, size_t size)
1073 struct CadetTunnelQueue *qt = cls;
1074 struct CadetTunnel *t;
1076 LOG (GNUNET_ERROR_TYPE_DEBUG, "tun_message_sent\n");
1078 GNUNET_assert (NULL != qt->cont);
1079 t = NULL == c ? NULL : GCC_get_tunnel (c);
1080 qt->cont (qt->cont_cls, t, qt, type, size);
1086 count_queued_data (const struct CadetTunnel *t)
1088 struct CadetTunnelDelayed *iter;
1091 for (count = 0, iter = t->tq_head; iter != NULL; iter = iter->next)
1098 * Delete a queued message: either was sent or the channel was destroyed
1099 * before the tunnel's key exchange had a chance to finish.
1101 * @param tqd Delayed queue handle.
1104 unqueue_data (struct CadetTunnelDelayed *tqd)
1106 GNUNET_CONTAINER_DLL_remove (tqd->t->tq_head, tqd->t->tq_tail, tqd);
1112 * Cache a message to be sent once tunnel is online.
1114 * @param t Tunnel to hold the message.
1115 * @param msg Message itself (copy will be made).
1117 static struct CadetTunnelDelayed *
1118 queue_data (struct CadetTunnel *t, const struct GNUNET_MessageHeader *msg)
1120 struct CadetTunnelDelayed *tqd;
1121 uint16_t size = ntohs (msg->size);
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue data on Tunnel %s\n", GCT_2s (t));
1125 if (GNUNET_YES == is_ready (t))
1131 tqd = GNUNET_malloc (sizeof (struct CadetTunnelDelayed) + size);
1134 memcpy (&tqd[1], msg, size);
1135 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tqd);
1141 * Sends an already built message on a tunnel, encrypting it and
1142 * choosing the best connection.
1144 * @param message Message to send. Function modifies it.
1145 * @param t Tunnel on which this message is transmitted.
1146 * @param c Connection to use (autoselect if NULL).
1147 * @param force Force the tunnel to take the message (buffer overfill).
1148 * @param cont Continuation to call once message is really sent.
1149 * @param cont_cls Closure for @c cont.
1150 * @param existing_q In case this a transmission of previously queued data,
1151 * this should be TunnelQueue given to the client.
1154 * @return Handle to cancel message.
1155 * NULL if @c cont is NULL or an error happens and message is dropped.
1157 static struct CadetTunnelQueue *
1158 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1159 struct CadetTunnel *t, struct CadetConnection *c,
1160 int force, GCT_sent cont, void *cont_cls,
1161 struct CadetTunnelQueue *existing_q)
1163 struct CadetTunnelQueue *tq;
1164 struct GNUNET_CADET_Encrypted *msg;
1165 size_t size = ntohs (message->size);
1166 char cbuf[sizeof (struct GNUNET_CADET_Encrypted) + size];
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GCT_2s (t));
1174 if (GNUNET_NO == is_ready (t))
1176 struct CadetTunnelDelayed *tqd;
1177 /* A non null existing_q indicates sending of queued data.
1178 * Should only happen after tunnel becomes ready.
1180 GNUNET_assert (NULL == existing_q);
1181 tqd = queue_data (t, message);
1184 tq = GNUNET_new (struct CadetTunnelQueue);
1188 tq->cont_cls = cont_cls;
1192 GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
1194 iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1195 msg = (struct GNUNET_CADET_Encrypted *) cbuf;
1196 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED);
1198 GNUNET_assert (t_encrypt (t, &msg[1], message, size, iv, GNUNET_NO) == size);
1199 t_hmac (&msg[1], size, iv, select_key (t), &msg->hmac);
1200 msg->header.size = htons (sizeof (struct GNUNET_CADET_Encrypted) + size);
1203 c = tunnel_get_connection (t);
1206 /* Why is tunnel 'ready'? Should have been queued! */
1207 if (NULL != t->destroy_task)
1210 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
1212 return NULL; /* Drop... */
1216 type = ntohs (message->type);
1219 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1220 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
1221 if (GNUNET_MESSAGE_TYPE_CADET_DATA == type)
1222 mid = ntohl (((struct GNUNET_CADET_Data *) message)->mid);
1224 mid = ntohl (((struct GNUNET_CADET_DataACK *) message)->mid);
1226 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1227 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1228 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1229 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1230 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1231 msg->cid = *GCC_get_id (c);
1232 msg->ttl = htonl (default_ttl);
1236 LOG (GNUNET_ERROR_TYPE_ERROR, "type %s not valid\n", GC_m2s (type));
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s\n", GC_m2s (type));
1240 fwd = GCC_is_origin (c, GNUNET_YES);
1244 GNUNET_break (NULL == GCC_send_prebuilt_message (&msg->header, type, mid, c,
1245 fwd, force, NULL, NULL));
1248 if (NULL == existing_q)
1250 tq = GNUNET_new (struct CadetTunnelQueue); /* FIXME valgrind: leak*/
1257 tq->cq = GCC_send_prebuilt_message (&msg->header, type, mid, c, fwd, force,
1258 &tun_message_sent, tq);
1259 GNUNET_assert (NULL != tq->cq);
1261 tq->cont_cls = cont_cls;
1268 * Send all cached messages that we can, tunnel is online.
1270 * @param t Tunnel that holds the messages. Cannot be loopback.
1273 send_queued_data (struct CadetTunnel *t)
1275 struct CadetTunnelDelayed *tqd;
1276 struct CadetTunnelDelayed *next;
1279 LOG (GNUNET_ERROR_TYPE_INFO, "Send queued data, tunnel %s\n", GCT_2s (t));
1281 if (GCT_is_loopback (t))
1287 if (GNUNET_NO == is_ready (t))
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, " not ready yet: %s/%s\n",
1290 estate2s (t->estate), cstate2s (t->cstate));
1294 room = GCT_get_connections_buffer (t);
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
1296 LOG (GNUNET_ERROR_TYPE_DEBUG, " tq head: %p\n", t->tq_head);
1297 for (tqd = t->tq_head; NULL != tqd && room > 0; tqd = next)
1299 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending queued data\n");
1302 send_prebuilt_message ((struct GNUNET_MessageHeader *) &tqd[1],
1303 tqd->t, NULL, GNUNET_YES,
1304 NULL != tqd->tq ? tqd->tq->cont : NULL,
1305 NULL != tqd->tq ? tqd->tq->cont_cls : NULL,
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_send_queued_data end\n", GCP_2s (t->peer));
1314 * Callback called when a queued message is sent.
1316 * @param cls Closure.
1317 * @param c Connection this message was on.
1318 * @param type Type of message sent.
1319 * @param fwd Was this a FWD going message?
1320 * @param size Size of the message.
1323 ephm_sent (void *cls,
1324 struct CadetConnection *c,
1325 struct CadetConnectionQueue *q,
1326 uint16_t type, int fwd, size_t size)
1328 struct CadetTunnel *t = cls;
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, "ephm_sent %s\n", GC_m2s (type));
1334 * Callback called when a queued message is sent.
1336 * @param cls Closure.
1337 * @param c Connection this message was on.
1338 * @param type Type of message sent.
1339 * @param fwd Was this a FWD going message?
1340 * @param size Size of the message.
1343 pong_sent (void *cls,
1344 struct CadetConnection *c,
1345 struct CadetConnectionQueue *q,
1346 uint16_t type, int fwd, size_t size)
1348 struct CadetTunnel *t = cls;
1349 LOG (GNUNET_ERROR_TYPE_DEBUG, "pong_sent %s\n", GC_m2s (type));
1355 * Sends key exchange message on a tunnel, choosing the best connection.
1356 * Should not be called on loopback tunnels.
1358 * @param t Tunnel on which this message is transmitted.
1359 * @param message Message to send. Function modifies it.
1361 * @return Handle to the message in the connection queue.
1363 static struct CadetConnectionQueue *
1364 send_kx (struct CadetTunnel *t,
1365 const struct GNUNET_MessageHeader *message)
1367 struct CadetConnection *c;
1368 struct GNUNET_CADET_KX *msg;
1369 size_t size = ntohs (message->size);
1370 char cbuf[sizeof (struct GNUNET_CADET_KX) + size];
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT KX on Tunnel %s\n", GCT_2s (t));
1377 /* Avoid loopback. */
1378 if (GCT_is_loopback (t))
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, " loopback!\n");
1384 type = ntohs (message->type);
1386 /* Even if tunnel is "being destroyed", send anyway.
1387 * Could be a response to a rekey initiated by remote peer,
1388 * who is trying to create a new channel!
1391 /* Must have a connection, or be looking for one. */
1392 if (NULL == t->connection_head)
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s while no connection\n", GC_m2s (type));
1395 if (CADET_TUNNEL_SEARCHING != t->cstate)
1398 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1399 GCP_debug (t->peer, GNUNET_ERROR_TYPE_ERROR);
1404 msg = (struct GNUNET_CADET_KX *) cbuf;
1405 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX);
1406 msg->header.size = htons (sizeof (struct GNUNET_CADET_KX) + size);
1407 c = tunnel_get_connection (t);
1410 if (NULL == t->destroy_task
1411 && CADET_TUNNEL_READY == t->cstate)
1414 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
1420 case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
1421 GNUNET_assert (NULL == t->ephm_h);
1423 memcpy (&msg[1], message, size);
1425 case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
1426 GNUNET_assert (NULL == t->pong_h);
1428 memcpy (&msg[1], message, size);
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n", GC_m2s (type));
1436 fwd = GCC_is_origin (t->connection_head->c, GNUNET_YES);
1438 return GCC_send_prebuilt_message (&msg->header, type, 0, c,
1445 * Send the ephemeral key on a tunnel.
1447 * @param t Tunnel on which to send the key.
1450 send_ephemeral (struct CadetTunnel *t)
1452 LOG (GNUNET_ERROR_TYPE_INFO, "===> EPHM for %s\n", GCT_2s (t));
1453 if (NULL != t->ephm_h)
1455 LOG (GNUNET_ERROR_TYPE_INFO, " already queued\n");
1459 kx_msg.sender_status = htonl (t->estate);
1460 kx_msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1461 kx_msg.nonce = t->kx_ctx->challenge;
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, " send nonce c %u\n", kx_msg.nonce);
1463 t_encrypt (t, &kx_msg.nonce, &kx_msg.nonce,
1464 ping_encryption_size(), kx_msg.iv, GNUNET_YES);
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " send nonce e %u\n", kx_msg.nonce);
1466 t->ephm_h = send_kx (t, &kx_msg.header);
1471 * Send a pong message on a tunnel.
1473 * @param t Tunnel on which to send the pong.
1474 * @param challenge Value sent in the ping that we have to send back.
1477 send_pong (struct CadetTunnel *t, uint32_t challenge)
1479 struct GNUNET_CADET_KX_Pong msg;
1481 LOG (GNUNET_ERROR_TYPE_INFO, "===> PONG for %s\n", GCT_2s (t));
1482 if (NULL != t->pong_h)
1484 LOG (GNUNET_ERROR_TYPE_INFO, " already queued\n");
1487 msg.header.size = htons (sizeof (msg));
1488 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_PONG);
1489 msg.iv = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1490 msg.nonce = challenge;
1491 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending %u\n", msg.nonce);
1492 t_encrypt (t, &msg.nonce, &msg.nonce,
1493 sizeof (msg.nonce), msg.iv, GNUNET_YES);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, " e sending %u\n", msg.nonce);
1496 t->pong_h = send_kx (t, &msg.header);
1501 * Initiate a rekey with the remote peer.
1503 * @param cls Closure (tunnel).
1504 * @param tc TaskContext.
1507 rekey_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1509 struct CadetTunnel *t = cls;
1511 t->rekey_task = NULL;
1513 LOG (GNUNET_ERROR_TYPE_INFO, "Re-key Tunnel %s\n", GCT_2s (t));
1514 if (NULL != tc && 0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1517 GNUNET_assert (NULL != t->kx_ctx);
1518 struct GNUNET_TIME_Relative duration;
1520 duration = GNUNET_TIME_absolute_get_duration (t->kx_ctx->rekey_start_time);
1521 LOG (GNUNET_ERROR_TYPE_DEBUG, " kx started %s ago\n",
1522 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
1524 // FIXME make duration of old keys configurable
1525 if (duration.rel_value_us >= GNUNET_TIME_UNIT_MINUTES.rel_value_us)
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, " deleting old keys\n");
1528 memset (&t->kx_ctx->d_key_old, 0, sizeof (t->kx_ctx->d_key_old));
1529 memset (&t->kx_ctx->e_key_old, 0, sizeof (t->kx_ctx->e_key_old));
1536 case CADET_TUNNEL_KEY_UNINITIALIZED:
1537 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
1540 case CADET_TUNNEL_KEY_SENT:
1543 case CADET_TUNNEL_KEY_OK:
1545 * - state should have changed during rekey_iterator
1546 * - task should have been canceled at pong_handle
1549 GCT_change_estate (t, CADET_TUNNEL_KEY_REKEY);
1552 case CADET_TUNNEL_KEY_PING:
1553 case CADET_TUNNEL_KEY_REKEY:
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unexpected state %u\n", t->estate);
1560 // FIXME exponential backoff
1561 struct GNUNET_TIME_Relative delay;
1563 delay = GNUNET_TIME_relative_divide (rekey_period, 16);
1564 delay = GNUNET_TIME_relative_min (delay, REKEY_WAIT);
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, " next call in %s\n",
1566 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1567 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
1572 * Our ephemeral key has changed, create new session key on all tunnels.
1574 * Each tunnel will start the Key Exchange with a random delay between
1575 * 0 and number_of_tunnels*100 milliseconds, so there are 10 key exchanges
1576 * per second, on average.
1578 * @param cls Closure (size of the hashmap).
1579 * @param key Current public key.
1580 * @param value Value in the hash map (tunnel).
1582 * @return #GNUNET_YES, so we should continue to iterate,
1585 rekey_iterator (void *cls,
1586 const struct GNUNET_PeerIdentity *key,
1589 struct CadetTunnel *t = value;
1590 struct GNUNET_TIME_Relative delay;
1591 long n = (long) cls;
1594 if (NULL != t->rekey_task)
1597 if (GNUNET_YES == GCT_is_loopback (t))
1600 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) n * 100);
1601 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, r);
1602 t->rekey_task = GNUNET_SCHEDULER_add_delayed (delay, &rekey_tunnel, t);
1604 GCT_change_estate (t, CADET_TUNNEL_KEY_REKEY);
1611 * Create a new ephemeral key and key message, schedule next rekeying.
1613 * @param cls Closure (unused).
1614 * @param tc TaskContext.
1617 rekey (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1619 struct GNUNET_TIME_Absolute time;
1624 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1627 GNUNET_free_non_null (my_ephemeral_key);
1628 my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
1630 time = GNUNET_TIME_absolute_get ();
1631 kx_msg.creation_time = GNUNET_TIME_absolute_hton (time);
1632 time = GNUNET_TIME_absolute_add (time, rekey_period);
1633 time = GNUNET_TIME_absolute_add (time, GNUNET_TIME_UNIT_MINUTES);
1634 kx_msg.expiration_time = GNUNET_TIME_absolute_hton (time);
1635 GNUNET_CRYPTO_ecdhe_key_get_public (my_ephemeral_key, &kx_msg.ephemeral_key);
1636 LOG (GNUNET_ERROR_TYPE_INFO, "GLOBAL RE-KEY, NEW EPHM: %s\n",
1637 GNUNET_h2s ((struct GNUNET_HashCode *) &kx_msg.ephemeral_key));
1639 GNUNET_assert (GNUNET_OK ==
1640 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1642 &kx_msg.signature));
1644 n = (long) GNUNET_CONTAINER_multipeermap_size (tunnels);
1645 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &rekey_iterator, (void *) n);
1647 rekey_task = GNUNET_SCHEDULER_add_delayed (rekey_period, &rekey, NULL);
1652 * Called only on shutdown, destroy every tunnel.
1654 * @param cls Closure (unused).
1655 * @param key Current public key.
1656 * @param value Value in the hash map (tunnel).
1658 * @return #GNUNET_YES, so we should continue to iterate,
1661 destroy_iterator (void *cls,
1662 const struct GNUNET_PeerIdentity *key,
1665 struct CadetTunnel *t = value;
1667 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_shutdown destroying tunnel at %p\n", t);
1674 * Notify remote peer that we don't know a channel he is talking about,
1675 * probably CHANNEL_DESTROY was missed.
1677 * @param t Tunnel on which to notify.
1678 * @param gid ID of the channel.
1681 send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
1683 struct GNUNET_CADET_ChannelManage msg;
1685 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1686 msg.header.size = htons (sizeof (msg));
1687 msg.chid = htonl (gid);
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "WARNING destroying unknown channel %u on tunnel %s\n",
1692 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1697 * Demultiplex data per channel and call appropriate channel handler.
1699 * @param t Tunnel on which the data came.
1700 * @param msg Data message.
1701 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1702 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1703 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1704 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1707 handle_data (struct CadetTunnel *t,
1708 const struct GNUNET_CADET_Data *msg,
1711 struct CadetChannel *ch;
1715 size = ntohs (msg->header.size);
1717 sizeof (struct GNUNET_CADET_Data) +
1718 sizeof (struct GNUNET_MessageHeader))
1723 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
1724 GC_m2s (ntohs (msg[1].header.type)));
1727 ch = GCT_get_channel (t, ntohl (msg->chid));
1730 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
1732 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel 0x%X unknown\n",
1734 send_channel_destroy (t, ntohl (msg->chid));
1738 GCCH_handle_data (ch, msg, fwd);
1743 * Demultiplex data ACKs per channel and update appropriate channel buffer info.
1745 * @param t Tunnel on which the DATA ACK came.
1746 * @param msg DATA ACK message.
1747 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1748 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1749 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1750 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1753 handle_data_ack (struct CadetTunnel *t,
1754 const struct GNUNET_CADET_DataACK *msg,
1757 struct CadetChannel *ch;
1761 size = ntohs (msg->header.size);
1762 if (size != sizeof (struct GNUNET_CADET_DataACK))
1769 ch = GCT_get_channel (t, ntohl (msg->chid));
1772 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1774 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1779 GCCH_handle_data_ack (ch, msg, fwd);
1784 * Handle channel create.
1786 * @param t Tunnel on which the data came.
1787 * @param msg Data message.
1790 handle_ch_create (struct CadetTunnel *t,
1791 const struct GNUNET_CADET_ChannelCreate *msg)
1793 struct CadetChannel *ch;
1797 size = ntohs (msg->header.size);
1798 if (size != sizeof (struct GNUNET_CADET_ChannelCreate))
1805 ch = GCT_get_channel (t, ntohl (msg->chid));
1806 if (NULL != ch && ! GCT_is_loopback (t))
1808 /* Probably a retransmission, safe to ignore */
1809 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
1811 ch = GCCH_handle_create (t, msg);
1813 GCT_add_channel (t, ch);
1819 * Handle channel NACK: check correctness and call channel handler for NACKs.
1821 * @param t Tunnel on which the NACK came.
1822 * @param msg NACK message.
1825 handle_ch_nack (struct CadetTunnel *t,
1826 const struct GNUNET_CADET_ChannelManage *msg)
1828 struct CadetChannel *ch;
1832 size = ntohs (msg->header.size);
1833 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1840 ch = GCT_get_channel (t, ntohl (msg->chid));
1843 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1845 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1850 GCCH_handle_nack (ch);
1855 * Handle a CHANNEL ACK (SYNACK/ACK).
1857 * @param t Tunnel on which the CHANNEL ACK came.
1858 * @param msg CHANNEL ACK message.
1859 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1860 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1861 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1862 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1865 handle_ch_ack (struct CadetTunnel *t,
1866 const struct GNUNET_CADET_ChannelManage *msg,
1869 struct CadetChannel *ch;
1873 size = ntohs (msg->header.size);
1874 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1881 ch = GCT_get_channel (t, ntohl (msg->chid));
1884 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
1886 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1891 GCCH_handle_ack (ch, msg, fwd);
1896 * Handle a channel destruction message.
1898 * @param t Tunnel on which the message came.
1899 * @param msg Channel destroy message.
1900 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1901 * #GNUNET_YES if message is FWD on the respective channel (loopback)
1902 * #GNUNET_NO if message is BCK on the respective channel (loopback)
1903 * #GNUNET_SYSERR if message on a one-ended channel (remote)
1906 handle_ch_destroy (struct CadetTunnel *t,
1907 const struct GNUNET_CADET_ChannelManage *msg,
1910 struct CadetChannel *ch;
1914 size = ntohs (msg->header.size);
1915 if (size != sizeof (struct GNUNET_CADET_ChannelManage))
1922 ch = GCT_get_channel (t, ntohl (msg->chid));
1925 /* Probably a retransmission, safe to ignore */
1929 GCCH_handle_destroy (ch, msg, fwd);
1934 * The peer's ephemeral key has changed: update the symmetrical keys.
1936 * @param t Tunnel this message came on.
1937 * @param msg Key eXchange message.
1940 handle_ephemeral (struct CadetTunnel *t,
1941 const struct GNUNET_CADET_KX_Ephemeral *msg)
1943 LOG (GNUNET_ERROR_TYPE_INFO, "<=== EPHM for %s\n", GCT_2s (t));
1945 if (GNUNET_OK != check_ephemeral (t, msg))
1947 GNUNET_break_op (0);
1952 * If the key is different from what we know, derive the new E/D keys.
1953 * Else destroy the rekey ctx (duplicate EPHM after successful KX).
1955 if (0 != memcmp (&t->peers_ephemeral_key, &msg->ephemeral_key,
1956 sizeof (msg->ephemeral_key)))
1958 #if DUMP_KEYS_TO_STDERR
1959 LOG (GNUNET_ERROR_TYPE_INFO, "OLD: %s\n",
1960 GNUNET_h2s ((struct GNUNET_HashCode *) &t->peers_ephemeral_key));
1961 LOG (GNUNET_ERROR_TYPE_INFO, "NEW: %s\n",
1962 GNUNET_h2s ((struct GNUNET_HashCode *) &msg->ephemeral_key));
1964 t->peers_ephemeral_key = msg->ephemeral_key;
1968 if (CADET_TUNNEL_KEY_OK == t->estate)
1970 GCT_change_estate (t, CADET_TUNNEL_KEY_REKEY);
1972 if (NULL != t->rekey_task)
1973 GNUNET_SCHEDULER_cancel (t->rekey_task);
1974 t->rekey_task = GNUNET_SCHEDULER_add_now (rekey_tunnel, t);
1976 if (CADET_TUNNEL_KEY_SENT == t->estate)
1978 LOG (GNUNET_ERROR_TYPE_DEBUG, " our key was sent, sending challenge\n");
1980 GCT_change_estate (t, CADET_TUNNEL_KEY_PING);
1983 if (CADET_TUNNEL_KEY_UNINITIALIZED != ntohl(msg->sender_status))
1987 LOG (GNUNET_ERROR_TYPE_DEBUG, " recv nonce e %u\n", msg->nonce);
1988 t_decrypt (t, &nonce, &msg->nonce, ping_encryption_size (), msg->iv);
1989 LOG (GNUNET_ERROR_TYPE_DEBUG, " recv nonce c %u\n", nonce);
1990 send_pong (t, nonce);
1996 * Peer has answer to our challenge.
1997 * If answer is successful, consider the key exchange finished and clean
1998 * up all related state.
2000 * @param t Tunnel this message came on.
2001 * @param msg Key eXchange Pong message.
2004 handle_pong (struct CadetTunnel *t,
2005 const struct GNUNET_CADET_KX_Pong *msg)
2009 LOG (GNUNET_ERROR_TYPE_INFO, "<=== PONG for %s\n", GCT_2s (t));
2010 if (NULL == t->rekey_task)
2012 GNUNET_STATISTICS_update (stats, "# duplicate PONG messages", 1, GNUNET_NO);
2015 if (NULL == t->kx_ctx)
2017 GNUNET_STATISTICS_update (stats, "# stray PONG messages", 1, GNUNET_NO);
2021 t_decrypt (t, &challenge, &msg->nonce, sizeof (uint32_t), msg->iv);
2022 if (challenge != t->kx_ctx->challenge)
2024 LOG (GNUNET_ERROR_TYPE_WARNING, "Wrong PONG challenge on %s\n", GCT_2s (t));
2025 LOG (GNUNET_ERROR_TYPE_DEBUG, "PONG: %u (e: %u). Expected: %u.\n",
2026 challenge, msg->nonce, t->kx_ctx->challenge);
2030 GNUNET_SCHEDULER_cancel (t->rekey_task);
2031 t->rekey_task = NULL;
2033 /* Don't free the old keys right away, but after a delay.
2034 * Rationale: the KX could have happened over a very fast connection,
2035 * with payload traffic still signed with the old key stuck in a slower
2037 * Don't keep the keys longer than 1/4 the rekey period, and no longer than
2041 GCT_change_estate (t, CADET_TUNNEL_KEY_OK);
2046 * Demultiplex by message type and call appropriate handler for a message
2047 * towards a channel of a local tunnel.
2049 * @param t Tunnel this message came on.
2050 * @param msgh Message header.
2051 * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2052 * #GNUNET_YES if message is FWD on the respective channel (loopback)
2053 * #GNUNET_NO if message is BCK on the respective channel (loopback)
2054 * #GNUNET_SYSERR if message on a one-ended channel (remote)
2057 handle_decrypted (struct CadetTunnel *t,
2058 const struct GNUNET_MessageHeader *msgh,
2063 type = ntohs (msgh->type);
2064 LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s on %s\n", GC_m2s (type), GCT_2s (t));
2068 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
2069 /* Do nothing, connection aleady got updated. */
2070 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
2073 case GNUNET_MESSAGE_TYPE_CADET_DATA:
2074 /* Don't send hop ACK, wait for client to ACK */
2075 handle_data (t, (struct GNUNET_CADET_Data *) msgh, fwd);
2078 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
2079 handle_data_ack (t, (struct GNUNET_CADET_DataACK *) msgh, fwd);
2082 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2083 handle_ch_create (t,
2084 (struct GNUNET_CADET_ChannelCreate *) msgh);
2087 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
2089 (struct GNUNET_CADET_ChannelManage *) msgh);
2092 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
2094 (struct GNUNET_CADET_ChannelManage *) msgh,
2098 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2099 handle_ch_destroy (t,
2100 (struct GNUNET_CADET_ChannelManage *) msgh,
2105 GNUNET_break_op (0);
2106 LOG (GNUNET_ERROR_TYPE_WARNING,
2107 "end-to-end message not known (%u)\n",
2108 ntohs (msgh->type));
2109 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
2113 /******************************************************************************/
2114 /******************************** API ***********************************/
2115 /******************************************************************************/
2118 * Decrypt and demultiplex by message type. Call appropriate handler
2119 * for every message.
2121 * @param t Tunnel this message came on.
2122 * @param msg Encrypted message.
2125 GCT_handle_encrypted (struct CadetTunnel *t,
2126 const struct GNUNET_CADET_Encrypted *msg)
2128 size_t size = ntohs (msg->header.size);
2129 size_t payload_size = size - sizeof (struct GNUNET_CADET_Encrypted);
2131 char cbuf [payload_size];
2132 struct GNUNET_MessageHeader *msgh;
2135 decrypted_size = t_decrypt_and_validate (t, cbuf, &msg[1], payload_size,
2136 msg->iv, &msg->hmac);
2138 if (-1 == decrypted_size)
2140 GNUNET_break_op (0);
2145 while (off < decrypted_size)
2149 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
2150 msize = ntohs (msgh->size);
2151 if (msize < sizeof (struct GNUNET_MessageHeader))
2153 GNUNET_break_op (0);
2156 handle_decrypted (t, msgh, GNUNET_SYSERR);
2163 * Demultiplex an encapsulated KX message by message type.
2165 * @param t Tunnel on which the message came.
2166 * @param message Payload of KX message.
2169 GCT_handle_kx (struct CadetTunnel *t,
2170 const struct GNUNET_MessageHeader *message)
2174 type = ntohs (message->type);
2175 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message received\n", type);
2178 case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
2179 handle_ephemeral (t, (struct GNUNET_CADET_KX_Ephemeral *) message);
2182 case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
2183 handle_pong (t, (struct GNUNET_CADET_KX_Pong *) message);
2187 GNUNET_break_op (0);
2188 LOG (GNUNET_ERROR_TYPE_DEBUG, "kx message not known (%u)\n", type);
2194 * Initialize the tunnel subsystem.
2196 * @param c Configuration handle.
2197 * @param key ECC private key, to derive all other keys and do crypto.
2200 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
2201 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
2203 int expected_overhead;
2205 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2207 expected_overhead = 0;
2208 expected_overhead += sizeof (struct GNUNET_CADET_Encrypted);
2209 expected_overhead += sizeof (struct GNUNET_CADET_Data);
2210 expected_overhead += sizeof (struct GNUNET_CADET_ACK);
2211 GNUNET_assert (GNUNET_CONSTANTS_CADET_P2P_OVERHEAD == expected_overhead);
2214 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DEFAULT_TTL",
2217 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
2218 "CADET", "DEFAULT_TTL", "USING DEFAULT");
2222 GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REKEY_PERIOD",
2225 rekey_period = GNUNET_TIME_UNIT_DAYS;
2228 my_private_key = key;
2229 kx_msg.header.size = htons (sizeof (kx_msg));
2230 kx_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL);
2231 kx_msg.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CADET_KX);
2232 kx_msg.purpose.size = htonl (ephemeral_purpose_size ());
2233 kx_msg.origin_identity = my_full_id;
2234 rekey_task = GNUNET_SCHEDULER_add_now (&rekey, NULL);
2236 tunnels = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
2241 * Shut down the tunnel subsystem.
2246 if (NULL != rekey_task)
2248 GNUNET_SCHEDULER_cancel (rekey_task);
2251 GNUNET_CONTAINER_multipeermap_iterate (tunnels, &destroy_iterator, NULL);
2252 GNUNET_CONTAINER_multipeermap_destroy (tunnels);
2259 * @param destination Peer this tunnel is towards.
2261 struct CadetTunnel *
2262 GCT_new (struct CadetPeer *destination)
2264 struct CadetTunnel *t;
2266 t = GNUNET_new (struct CadetTunnel);
2268 t->peer = destination;
2271 GNUNET_CONTAINER_multipeermap_put (tunnels, GCP_get_id (destination), t,
2272 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2283 * Change the tunnel's connection state.
2285 * @param t Tunnel whose connection state to change.
2286 * @param cstate New connection state.
2289 GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate)
2293 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s cstate %s => %s\n",
2294 GCP_2s (t->peer), cstate2s (t->cstate), cstate2s (cstate));
2295 if (myid != GCP_get_short_id (t->peer) &&
2296 CADET_TUNNEL_READY != t->cstate &&
2297 CADET_TUNNEL_READY == cstate)
2300 if (CADET_TUNNEL_KEY_OK == t->estate)
2302 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered send queued data\n");
2303 send_queued_data (t);
2305 else if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
2307 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered rekey\n");
2308 if (NULL != t->rekey_task)
2309 GNUNET_SCHEDULER_cancel (t->rekey_task);
2311 rekey_tunnel (t, NULL);
2316 if (CADET_TUNNEL_READY == cstate
2317 && CONNECTIONS_PER_TUNNEL <= GCT_count_connections (t))
2319 LOG (GNUNET_ERROR_TYPE_DEBUG, " cstate triggered stop dht\n");
2320 GCP_stop_search (t->peer);
2326 * Change the tunnel encryption state.
2328 * @param t Tunnel whose encryption state to change, or NULL.
2329 * @param state New encryption state.
2332 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state)
2334 enum CadetTunnelEState old;
2341 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s estate was %s\n",
2342 GCP_2s (t->peer), estate2s (old));
2343 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s estate is now %s\n",
2344 GCP_2s (t->peer), estate2s (t->estate));
2346 /* Send queued data if enc state changes to OK */
2347 if (myid != GCP_get_short_id (t->peer) &&
2348 CADET_TUNNEL_KEY_OK != old && CADET_TUNNEL_KEY_OK == t->estate)
2350 send_queued_data (t);
2356 * @brief Check if tunnel has too many connections, and remove one if necessary.
2358 * Currently this means the newest connection, unless it is a direct one.
2359 * Implemented as a task to avoid freeing a connection that is in the middle
2360 * of being created/processed.
2362 * @param cls Closure (Tunnel to check).
2363 * @param tc Task context.
2366 trim_connections (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2368 struct CadetTunnel *t = cls;
2370 t->trim_connections_task = NULL;
2372 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2375 if (GCT_count_connections (t) > 2 * CONNECTIONS_PER_TUNNEL)
2377 struct CadetTConnection *iter;
2378 struct CadetTConnection *c;
2380 for (c = iter = t->connection_head; NULL != iter; iter = iter->next)
2382 if ((iter->created.abs_value_us > c->created.abs_value_us)
2383 && GNUNET_NO == GCC_is_direct (iter->c))
2390 LOG (GNUNET_ERROR_TYPE_DEBUG, "Too many connections on tunnel %s\n",
2392 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying connection %s\n",
2405 * Add a connection to a tunnel.
2408 * @param c Connection.
2411 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c)
2413 struct CadetTConnection *aux;
2415 GNUNET_assert (NULL != c);
2417 LOG (GNUNET_ERROR_TYPE_DEBUG, "add connection %s\n", GCC_2s (c));
2418 LOG (GNUNET_ERROR_TYPE_DEBUG, " to tunnel %s\n", GCT_2s (t));
2419 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2423 aux = GNUNET_new (struct CadetTConnection);
2425 aux->created = GNUNET_TIME_absolute_get ();
2427 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, aux);
2429 if (CADET_TUNNEL_SEARCHING == t->cstate)
2430 GCT_change_estate (t, CADET_TUNNEL_WAITING);
2432 if (NULL != t->trim_connections_task)
2433 t->trim_connections_task = GNUNET_SCHEDULER_add_now (&trim_connections, t);
2438 * Remove a connection from a tunnel.
2441 * @param c Connection.
2444 GCT_remove_connection (struct CadetTunnel *t,
2445 struct CadetConnection *c)
2447 struct CadetTConnection *aux;
2448 struct CadetTConnection *next;
2451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing connection %s from tunnel %s\n",
2452 GCC_2s (c), GCT_2s (t));
2453 for (aux = t->connection_head; aux != NULL; aux = next)
2458 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
2463 conns = GCT_count_connections (t);
2465 && NULL == t->destroy_task
2466 && CADET_TUNNEL_SHUTDOWN != t->cstate
2467 && GNUNET_NO == shutting_down)
2469 if (0 == GCT_count_any_connections (t))
2470 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2472 GCT_change_cstate (t, CADET_TUNNEL_WAITING);
2475 /* Start new connections if needed */
2476 if (CONNECTIONS_PER_TUNNEL > conns
2477 && NULL == t->destroy_task
2478 && CADET_TUNNEL_SHUTDOWN != t->cstate
2479 && GNUNET_NO == shutting_down)
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, " too few connections, getting new ones\n");
2482 GCP_connect (t->peer); /* Will change cstate to WAITING when possible */
2486 /* If not marked as ready, no change is needed */
2487 if (CADET_TUNNEL_READY != t->cstate)
2490 /* Check if any connection is ready to maintain cstate */
2491 for (aux = t->connection_head; aux != NULL; aux = aux->next)
2492 if (CADET_CONNECTION_READY == GCC_get_state (aux->c))
2498 * Add a channel to a tunnel.
2501 * @param ch Channel.
2504 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2506 struct CadetTChannel *aux;
2508 GNUNET_assert (NULL != ch);
2510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding channel %p to tunnel %p\n", ch, t);
2512 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2514 LOG (GNUNET_ERROR_TYPE_DEBUG, " already there %p\n", aux->ch);
2519 aux = GNUNET_new (struct CadetTChannel);
2521 LOG (GNUNET_ERROR_TYPE_DEBUG, " adding %p to %p\n", aux, t->channel_head);
2522 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
2524 if (NULL != t->destroy_task)
2526 GNUNET_SCHEDULER_cancel (t->destroy_task);
2527 t->destroy_task = NULL;
2528 LOG (GNUNET_ERROR_TYPE_DEBUG, " undo destroy!\n");
2534 * Remove a channel from a tunnel.
2537 * @param ch Channel.
2540 GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2542 struct CadetTChannel *aux;
2544 LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing channel %p from tunnel %p\n", ch, t);
2545 for (aux = t->channel_head; aux != NULL; aux = aux->next)
2549 LOG (GNUNET_ERROR_TYPE_DEBUG, " found! %s\n", GCCH_2s (ch));
2550 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
2559 * Search for a channel by global ID.
2561 * @param t Tunnel containing the channel.
2562 * @param chid Public channel number.
2564 * @return channel handler, NULL if doesn't exist
2566 struct CadetChannel *
2567 GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
2569 struct CadetTChannel *iter;
2574 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2576 if (GCCH_get_id (iter->ch) == chid)
2580 return NULL == iter ? NULL : iter->ch;
2585 * @brief Destroy a tunnel and free all resources.
2587 * Should only be called a while after the tunnel has been marked as destroyed,
2588 * in case there is a new channel added to the same peer shortly after marking
2589 * the tunnel. This way we avoid a new public key handshake.
2591 * @param cls Closure (tunnel to destroy).
2592 * @param tc Task context.
2595 delayed_destroy (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2597 struct CadetTunnel *t = cls;
2598 struct CadetTConnection *iter;
2600 LOG (GNUNET_ERROR_TYPE_DEBUG, "delayed destroying tunnel %p\n", t);
2601 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2603 LOG (GNUNET_ERROR_TYPE_WARNING,
2604 "Not destroying tunnel, due to shutdown. "
2605 "Tunnel at %p should have been freed by GCT_shutdown\n", t);
2608 t->destroy_task = NULL;
2609 t->cstate = CADET_TUNNEL_SHUTDOWN;
2611 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2613 GCC_send_destroy (iter->c);
2620 * Tunnel is empty: destroy it.
2622 * Notifies all connections about the destruction.
2624 * @param t Tunnel to destroy.
2627 GCT_destroy_empty (struct CadetTunnel *t)
2629 if (GNUNET_YES == shutting_down)
2630 return; /* Will be destroyed immediately anyway */
2632 if (NULL != t->destroy_task)
2634 LOG (GNUNET_ERROR_TYPE_WARNING,
2635 "Tunnel %s is already scheduled for destruction. Tunnel debug dump:\n",
2637 GCT_debug (t, GNUNET_ERROR_TYPE_WARNING);
2639 /* should never happen, tunnel can only become empty once, and the
2640 * task identifier should be NO_TASK (cleaned when the tunnel was created
2641 * or became un-empty)
2646 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s empty: scheduling destruction\n",
2649 // FIXME make delay a config option
2650 t->destroy_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
2651 &delayed_destroy, t);
2652 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled destroy of %p as %llu\n",
2653 t, t->destroy_task);
2658 * Destroy tunnel if empty (no more channels).
2660 * @param t Tunnel to destroy if empty.
2663 GCT_destroy_if_empty (struct CadetTunnel *t)
2665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel %s destroy if empty\n", GCT_2s (t));
2666 if (0 < GCT_count_channels (t))
2669 GCT_destroy_empty (t);
2674 * Destroy the tunnel.
2676 * This function does not generate any warning traffic to clients or peers.
2679 * Cancel messages belonging to this tunnel queued to neighbors.
2680 * Free any allocated resources linked to the tunnel.
2682 * @param t The tunnel to destroy.
2685 GCT_destroy (struct CadetTunnel *t)
2687 struct CadetTConnection *iter_c;
2688 struct CadetTConnection *next_c;
2689 struct CadetTChannel *iter_ch;
2690 struct CadetTChannel *next_ch;
2695 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GCP_2s (t->peer));
2697 GNUNET_break (GNUNET_YES ==
2698 GNUNET_CONTAINER_multipeermap_remove (tunnels,
2699 GCP_get_id (t->peer), t));
2701 for (iter_c = t->connection_head; NULL != iter_c; iter_c = next_c)
2703 next_c = iter_c->next;
2704 GCC_destroy (iter_c->c);
2706 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = next_ch)
2708 next_ch = iter_ch->next;
2709 GCCH_destroy (iter_ch->ch);
2710 /* Should only happen on shutdown, but it's ok. */
2713 if (NULL != t->destroy_task)
2715 LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling dest: %llX\n", t->destroy_task);
2716 GNUNET_SCHEDULER_cancel (t->destroy_task);
2717 t->destroy_task = NULL;
2720 if (NULL != t->trim_connections_task)
2722 LOG (GNUNET_ERROR_TYPE_DEBUG, "cancelling trim: %llX\n",
2723 t->trim_connections_task);
2724 GNUNET_SCHEDULER_cancel (t->trim_connections_task);
2725 t->trim_connections_task = NULL;
2728 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2729 GCP_set_tunnel (t->peer, NULL);
2731 if (NULL != t->rekey_task)
2733 GNUNET_SCHEDULER_cancel (t->rekey_task);
2734 t->rekey_task = NULL;
2736 if (NULL != t->kx_ctx)
2738 if (NULL != t->kx_ctx->finish_task)
2739 GNUNET_SCHEDULER_cancel (t->kx_ctx->finish_task);
2740 GNUNET_free (t->kx_ctx);
2747 * @brief Use the given path for the tunnel.
2748 * Update the next and prev hops (and RCs).
2749 * (Re)start the path refresh in case the tunnel is locally owned.
2751 * @param t Tunnel to update.
2752 * @param p Path to use.
2754 * @return Connection created.
2756 struct CadetConnection *
2757 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p)
2759 struct CadetConnection *c;
2760 struct GNUNET_CADET_Hash cid;
2761 unsigned int own_pos;
2763 if (NULL == t || NULL == p)
2769 if (CADET_TUNNEL_SHUTDOWN == t->cstate)
2775 for (own_pos = 0; own_pos < p->length; own_pos++)
2777 if (p->peers[own_pos] == myid)
2780 if (own_pos >= p->length)
2782 GNUNET_break_op (0);
2786 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid));
2787 c = GCC_new (&cid, t, p, own_pos);
2790 /* Path was flawed */
2793 GCT_add_connection (t, c);
2799 * Count all created connections of a tunnel. Not necessarily ready connections!
2801 * @param t Tunnel on which to count.
2803 * @return Number of connections created, either being established or ready.
2806 GCT_count_any_connections (struct CadetTunnel *t)
2808 struct CadetTConnection *iter;
2814 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2822 * Count established (ready) connections of a tunnel.
2824 * @param t Tunnel on which to count.
2826 * @return Number of connections.
2829 GCT_count_connections (struct CadetTunnel *t)
2831 struct CadetTConnection *iter;
2837 for (count = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
2838 if (CADET_CONNECTION_READY == GCC_get_state (iter->c))
2846 * Count channels of a tunnel.
2848 * @param t Tunnel on which to count.
2850 * @return Number of channels.
2853 GCT_count_channels (struct CadetTunnel *t)
2855 struct CadetTChannel *iter;
2858 for (count = 0, iter = t->channel_head;
2860 iter = iter->next, count++) /* skip */;
2867 * Get the connectivity state of a tunnel.
2871 * @return Tunnel's connectivity state.
2873 enum CadetTunnelCState
2874 GCT_get_cstate (struct CadetTunnel *t)
2879 return (enum CadetTunnelCState) -1;
2886 * Get the encryption state of a tunnel.
2890 * @return Tunnel's encryption state.
2892 enum CadetTunnelEState
2893 GCT_get_estate (struct CadetTunnel *t)
2898 return (enum CadetTunnelEState) -1;
2904 * Get the maximum buffer space for a tunnel towards a local client.
2908 * @return Biggest buffer space offered by any channel in the tunnel.
2911 GCT_get_channels_buffer (struct CadetTunnel *t)
2913 struct CadetTChannel *iter;
2914 unsigned int buffer;
2915 unsigned int ch_buf;
2917 if (NULL == t->channel_head)
2919 /* Probably getting buffer for a channel create/handshake. */
2920 LOG (GNUNET_ERROR_TYPE_DEBUG, " no channels, allow max\n");
2925 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2927 ch_buf = get_channel_buffer (iter);
2928 if (ch_buf > buffer)
2936 * Get the total buffer space for a tunnel for P2P traffic.
2940 * @return Buffer space offered by all connections in the tunnel.
2943 GCT_get_connections_buffer (struct CadetTunnel *t)
2945 struct CadetTConnection *iter;
2946 unsigned int buffer;
2948 if (GNUNET_NO == is_ready (t))
2950 if (count_queued_data (t) > 3)
2957 for (iter = t->connection_head; NULL != iter; iter = iter->next)
2959 if (GCC_get_state (iter->c) != CADET_CONNECTION_READY)
2963 buffer += get_connection_buffer (iter);
2971 * Get the tunnel's destination.
2975 * @return ID of the destination peer.
2977 const struct GNUNET_PeerIdentity *
2978 GCT_get_destination (struct CadetTunnel *t)
2980 return GCP_get_id (t->peer);
2985 * Get the tunnel's next free global channel ID.
2989 * @return GID of a channel free to use.
2992 GCT_get_next_chid (struct CadetTunnel *t)
2994 CADET_ChannelNumber chid;
2995 CADET_ChannelNumber mask;
2998 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
2999 * If our ID is bigger or loopback tunnel, start at 0, bit 30 = 0
3000 * If peer's ID is bigger, start at 0x4... bit 30 = 1
3002 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (t->peer));
3007 t->next_chid |= mask;
3009 while (NULL != GCT_get_channel (t, t->next_chid))
3011 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
3012 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
3013 t->next_chid |= mask;
3015 chid = t->next_chid;
3016 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
3017 t->next_chid |= mask;
3024 * Send ACK on one or more channels due to buffer in connections.
3026 * @param t Channel which has some free buffer space.
3029 GCT_unchoke_channels (struct CadetTunnel *t)
3031 struct CadetTChannel *iter;
3032 unsigned int buffer;
3033 unsigned int channels = GCT_count_channels (t);
3034 unsigned int choked_n;
3035 struct CadetChannel *choked[channels];
3037 LOG (GNUNET_ERROR_TYPE_DEBUG, "GCT_unchoke_channels on %s\n", GCT_2s (t));
3038 LOG (GNUNET_ERROR_TYPE_DEBUG, " head: %p\n", t->channel_head);
3039 if (NULL != t->channel_head)
3040 LOG (GNUNET_ERROR_TYPE_DEBUG, " head ch: %p\n", t->channel_head->ch);
3042 /* Get buffer space */
3043 buffer = GCT_get_connections_buffer (t);
3049 /* Count and remember choked channels */
3051 for (iter = t->channel_head; NULL != iter; iter = iter->next)
3053 if (GNUNET_NO == get_channel_allowed (iter))
3055 choked[choked_n++] = iter->ch;
3059 /* Unchoke random channels */
3060 while (0 < buffer && 0 < choked_n)
3062 unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3064 GCCH_allow_client (choked[r], GCCH_is_origin (choked[r], GNUNET_YES));
3067 choked[r] = choked[choked_n];
3073 * Send ACK on one or more connections due to buffer space to the client.
3075 * Iterates all connections of the tunnel and sends ACKs appropriately.
3080 GCT_send_connection_acks (struct CadetTunnel *t)
3082 struct CadetTConnection *iter;
3085 uint32_t allow_per_connection;
3087 unsigned int buffer;
3089 LOG (GNUNET_ERROR_TYPE_DEBUG, "Tunnel send connection ACKs on %s\n",
3098 if (CADET_TUNNEL_READY != t->cstate)
3101 buffer = GCT_get_channels_buffer (t);
3102 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer %u\n", buffer);
3104 /* Count connections, how many messages are already allowed */
3105 cs = GCT_count_connections (t);
3106 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
3108 allowed += get_connection_allowed (iter);
3110 LOG (GNUNET_ERROR_TYPE_DEBUG, " allowed %u\n", allowed);
3112 /* Make sure there is no overflow */
3113 if (allowed > buffer)
3116 /* Authorize connections to send more data */
3117 to_allow = buffer - allowed;
3119 for (iter = t->connection_head;
3120 NULL != iter && to_allow > 0;
3123 if (CADET_CONNECTION_READY != GCC_get_state (iter->c)
3124 || get_connection_allowed (iter) > 64 / 3)
3128 allow_per_connection = to_allow/cs;
3129 to_allow -= allow_per_connection;
3131 GCC_allow (iter->c, allow_per_connection,
3132 GCC_is_origin (iter->c, GNUNET_NO));
3137 /* Since we don't allow if it's allowed to send 64/3, this can happen. */
3138 LOG (GNUNET_ERROR_TYPE_DEBUG, " reminding to_allow: %u\n", to_allow);
3144 * Cancel a previously sent message while it's in the queue.
3146 * ONLY can be called before the continuation given to the send function
3147 * is called. Once the continuation is called, the message is no longer in the
3150 * @param q Handle to the queue.
3153 GCT_cancel (struct CadetTunnelQueue *q)
3158 /* tun_message_sent() will be called and free q */
3160 else if (NULL != q->tqd)
3162 unqueue_data (q->tqd);
3164 if (NULL != q->cont)
3165 q->cont (q->cont_cls, NULL, q, 0, 0);
3176 * Sends an already built message on a tunnel, encrypting it and
3177 * choosing the best connection if not provided.
3179 * @param message Message to send. Function modifies it.
3180 * @param t Tunnel on which this message is transmitted.
3181 * @param c Connection to use (autoselect if NULL).
3182 * @param force Force the tunnel to take the message (buffer overfill).
3183 * @param cont Continuation to call once message is really sent.
3184 * @param cont_cls Closure for @c cont.
3186 * @return Handle to cancel message. NULL if @c cont is NULL.
3188 struct CadetTunnelQueue *
3189 GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3190 struct CadetTunnel *t, struct CadetConnection *c,
3191 int force, GCT_sent cont, void *cont_cls)
3193 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
3197 * Sends an already built and encrypted message on a tunnel, choosing the best
3198 * connection. Useful for re-queueing messages queued on a destroyed connection.
3200 * @param message Message to send. Function modifies it.
3201 * @param t Tunnel on which this message is transmitted.
3204 GCT_resend_message (const struct GNUNET_MessageHeader *message,
3205 struct CadetTunnel *t)
3207 struct CadetConnection *c;
3210 c = tunnel_get_connection (t);
3213 /* TODO queue in tunnel, marked as encrypted */
3214 LOG (GNUNET_ERROR_TYPE_DEBUG, "No connection available, dropping.\n");
3217 fwd = GCC_is_origin (c, GNUNET_YES);
3218 GNUNET_break (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
3219 GNUNET_YES, NULL, NULL));
3224 * Is the tunnel directed towards the local peer?
3228 * @return #GNUNET_YES if it is loopback.
3231 GCT_is_loopback (const struct CadetTunnel *t)
3233 return (myid == GCP_get_short_id (t->peer));
3238 * Is the tunnel this path already?
3243 * @return #GNUNET_YES a connection uses this path.
3246 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p)
3248 struct CadetTConnection *iter;
3250 for (iter = t->connection_head; NULL != iter; iter = iter->next)
3251 if (path_equivalent (GCC_get_path (iter->c), p))
3259 * Get a cost of a path for a tunnel considering existing connections.
3262 * @param path Candidate path.
3264 * @return Cost of the path (path length + number of overlapping nodes)
3267 GCT_get_path_cost (const struct CadetTunnel *t,
3268 const struct CadetPeerPath *path)
3270 struct CadetTConnection *iter;
3271 const struct CadetPeerPath *aux;
3272 unsigned int overlap;
3280 GNUNET_assert (NULL != t);
3282 for (i = 0; i < path->length; i++)
3284 for (iter = t->connection_head; NULL != iter; iter = iter->next)
3286 aux = GCC_get_path (iter->c);
3290 for (j = 0; j < aux->length; j++)
3292 if (path->peers[i] == aux->peers[j])
3300 return path->length + overlap;
3305 * Get the static string for the peer this tunnel is directed.
3309 * @return Static string the destination peer's ID.
3312 GCT_2s (const struct CadetTunnel *t)
3317 return GCP_2s (t->peer);
3321 /******************************************************************************/
3322 /***************************** INFO/DEBUG *******************************/
3323 /******************************************************************************/
3326 * Log all possible info about the tunnel state.
3328 * @param t Tunnel to debug.
3329 * @param level Debug level to use.
3332 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level)
3334 struct CadetTChannel *iterch;
3335 struct CadetTConnection *iterc;
3338 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
3340 __FILE__, __FUNCTION__, __LINE__);
3344 LOG2 (level, "TTT DEBUG TUNNEL TOWARDS %s\n", GCT_2s (t));
3345 LOG2 (level, "TTT cstate %s, estate %s\n",
3346 cstate2s (t->cstate), estate2s (t->estate));
3347 LOG2 (level, "TTT kx_ctx %p, rekey_task %u, finish task %u\n",
3348 t->kx_ctx, t->rekey_task, t->kx_ctx ? t->kx_ctx->finish_task : 0);
3349 #if DUMP_KEYS_TO_STDERR
3350 LOG2 (level, "TTT my EPHM\t %s\n",
3351 GNUNET_h2s ((struct GNUNET_HashCode *) &kx_msg.ephemeral_key));
3352 LOG2 (level, "TTT peers EPHM:\t %s\n",
3353 GNUNET_h2s ((struct GNUNET_HashCode *) &t->peers_ephemeral_key));
3354 LOG2 (level, "TTT ENC key:\t %s\n",
3355 GNUNET_h2s ((struct GNUNET_HashCode *) &t->e_key));
3356 LOG2 (level, "TTT DEC key:\t %s\n",
3357 GNUNET_h2s ((struct GNUNET_HashCode *) &t->d_key));
3360 LOG2 (level, "TTT OLD ENC key:\t %s\n",
3361 GNUNET_h2s ((struct GNUNET_HashCode *) &t->kx_ctx->e_key_old));
3362 LOG2 (level, "TTT OLD DEC key:\t %s\n",
3363 GNUNET_h2s ((struct GNUNET_HashCode *) &t->kx_ctx->d_key_old));
3366 LOG2 (level, "TTT tq_head %p, tq_tail %p\n", t->tq_head, t->tq_tail);
3367 LOG2 (level, "TTT destroy %u\n", t->destroy_task);
3369 LOG2 (level, "TTT channels:\n");
3370 for (iterch = t->channel_head; NULL != iterch; iterch = iterch->next)
3372 LOG2 (level, "TTT - %s\n", GCCH_2s (iterch->ch));
3375 LOG2 (level, "TTT connections:\n");
3376 for (iterc = t->connection_head; NULL != iterc; iterc = iterc->next)
3378 GCC_debug (iterc->c, level);
3381 LOG2 (level, "TTT DEBUG TUNNEL END\n");
3386 * Iterate all tunnels.
3388 * @param iter Iterator.
3389 * @param cls Closure for @c iter.
3392 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
3394 GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
3399 * Count all tunnels.
3401 * @return Number of tunnels to remote peers kept by this peer.
3404 GCT_count_all (void)
3406 return GNUNET_CONTAINER_multipeermap_size (tunnels);
3411 * Iterate all connections of a tunnel.
3413 * @param t Tunnel whose connections to iterate.
3414 * @param iter Iterator.
3415 * @param cls Closure for @c iter.
3418 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls)
3420 struct CadetTConnection *ct;
3422 for (ct = t->connection_head; NULL != ct; ct = ct->next)
3428 * Iterate all channels of a tunnel.
3430 * @param t Tunnel whose channels to iterate.
3431 * @param iter Iterator.
3432 * @param cls Closure for @c iter.
3435 GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls)
3437 struct CadetTChannel *cht;
3439 for (cht = t->channel_head; NULL != cht; cht = cht->next)
3440 iter (cls, cht->ch);