2 This file is part of GNUnet.
3 (C) 2010-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 * @file transport/gnunet-service-transport_validation.c
23 * @brief address validation subsystem
24 * @author Christian Grothoff
27 #include "gnunet-service-transport_validation.h"
28 #include "gnunet-service-transport_plugins.h"
29 #include "gnunet-service-transport_hello.h"
30 #include "gnunet-service-transport_blacklist.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_ats_service.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_signatures.h"
40 * How long is a PONG signature valid? We'll recycle a signature until
41 * 1/4 of this time is remaining. PONGs should expire so that if our
42 * external addresses change an adversary cannot replay them indefinitely.
43 * OTOH, we don't want to spend too much time generating PONG signatures,
44 * so they must have some lifetime to reduce our CPU usage.
46 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
49 * After how long do we expire an address in a HELLO that we just
50 * validated? This value is also used for our own addresses when we
53 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
56 * How often do we allow PINGing an address that we have not yet
57 * validated? This also determines how long we track an address that
58 * we cannot validate (because after this time we can destroy the
61 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
64 * How often do we PING an address that we have successfully validated
65 * in the past but are not actively using? Should be (significantly)
66 * smaller than HELLO_ADDRESS_EXPIRATION.
68 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
71 * How often do we PING an address that we are currently using?
73 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
76 * How much delay is acceptable for sending the PING or PONG?
78 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
81 * Size of the validation map hashmap.
83 #define VALIDATION_MAP_SIZE 256
86 * Priority to use for PINGs
88 #define PING_PRIORITY 2
91 * Priority to use for PONGs
93 #define PONG_PRIORITY 4
96 GNUNET_NETWORK_STRUCT_BEGIN
99 * Message used to ask a peer to validate receipt (to check an address
100 * from a HELLO). Followed by the address we are trying to validate,
101 * or an empty address if we are just sending a PING to confirm that a
102 * connection which the receiver (of the PING) initiated is still valid.
104 struct TransportPingMessage
108 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
110 struct GNUNET_MessageHeader header;
113 * Challenge code (to ensure fresh reply).
115 uint32_t challenge GNUNET_PACKED;
118 * Who is the intended recipient?
120 struct GNUNET_PeerIdentity target;
126 * Message used to validate a HELLO. The challenge is included in the
127 * confirmation to make matching of replies to requests possible. The
128 * signature signs our public key, an expiration time and our address.<p>
130 * This message is followed by our transport address that the PING tried
131 * to confirm (if we liked it). The address can be empty (zero bytes)
132 * if the PING had not address either (and we received the request via
133 * a connection that we initiated).
135 struct TransportPongMessage
139 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
141 struct GNUNET_MessageHeader header;
144 * Challenge code from PING (showing freshness). Not part of what
145 * is signed so that we can re-use signatures.
147 uint32_t challenge GNUNET_PACKED;
152 struct GNUNET_CRYPTO_EddsaSignature signature;
155 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
156 * plausible address for the signing peer.
158 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
161 * When does this signature expire?
163 struct GNUNET_TIME_AbsoluteNBO expiration;
166 * Size of address appended to this message (part of what is
167 * being signed, hence not redundant).
169 uint32_t addrlen GNUNET_PACKED;
172 GNUNET_NETWORK_STRUCT_END
175 * Information about an address under validation
177 struct ValidationEntry
183 struct GNUNET_HELLO_Address *address;
186 * Handle to the blacklist check (if we're currently in it).
188 struct GST_BlacklistCheck *bc;
191 * Public key of the peer.
193 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
196 * The identity of the peer. FIXME: duplicated (also in 'address')
198 struct GNUNET_PeerIdentity pid;
201 * Cached PONG signature
203 struct GNUNET_CRYPTO_EddsaSignature pong_sig_cache;
206 * ID of task that will clean up this entry if nothing happens.
208 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
211 * ID of task that will trigger address revalidation.
213 GNUNET_SCHEDULER_TaskIdentifier revalidation_task;
216 * At what time did we send the latest validation request (PING)?
218 struct GNUNET_TIME_Absolute send_time;
221 * Until when is this address valid?
222 * ZERO if it is not currently considered valid.
224 struct GNUNET_TIME_Absolute valid_until;
227 * Until when is the cached PONG signature valid?
228 * ZERO if it is not currently considered valid.
230 struct GNUNET_TIME_Absolute pong_sig_valid_until;
233 * How long until we can try to validate this address again?
234 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
235 * ZERO if the address is considered valid (no validation needed)
236 * otherwise a time in the future if we're currently denying re-validation
238 struct GNUNET_TIME_Absolute revalidation_block;
241 * Last observed latency for this address (round-trip), delay between
242 * last PING sent and PONG received; FOREVER if we never got a PONG.
244 struct GNUNET_TIME_Relative latency;
247 * Challenge number we used.
252 * When passing the address in 'add_valid_peer_address', did we
253 * copy the address to the HELLO yet?
258 * Are we currently using this address for a connection?
263 * Are we expecting a PONG message for this validation entry?
267 /* FIXME: DEBUGGING */
268 int last_line_set_to_no;
269 int last_line_set_to_yes;
271 enum GNUNET_ATS_Network_Type network;
276 * Context of currently active requests to peerinfo
277 * for validation of HELLOs.
279 struct CheckHelloValidatedContext
283 * This is a doubly-linked list.
285 struct CheckHelloValidatedContext *next;
288 * This is a doubly-linked list.
290 struct CheckHelloValidatedContext *prev;
293 * Hello that we are validating.
295 const struct GNUNET_HELLO_Message *hello;
301 * Head of linked list of HELLOs awaiting validation.
303 static struct CheckHelloValidatedContext *chvc_head;
306 * Tail of linked list of HELLOs awaiting validation
308 static struct CheckHelloValidatedContext *chvc_tail;
311 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
312 * of the given peer that we are currently validating, have validated
313 * or are blocked from re-validation for a while).
315 static struct GNUNET_CONTAINER_MultiPeerMap *validation_map;
318 * Context for peerinfo iteration.
320 static struct GNUNET_PEERINFO_NotifyContext *pnc;
324 * Minimum delay between to validations
326 static struct GNUNET_TIME_Relative validation_delay;
329 * Number of validations running
331 static unsigned int validations_running;
334 * Validition fast start threshold
336 static unsigned int validations_fast_start_threshold;
339 * When is next validation allowed
341 static struct GNUNET_TIME_Absolute validation_next;
344 * Context for the validation entry match function.
346 struct ValidationEntryMatchContext
349 * Where to store the result?
351 struct ValidationEntry *ve;
354 * Address we're interested in.
356 const struct GNUNET_HELLO_Address *address;
362 * Iterate over validation entries until a matching one is found.
364 * @param cls the 'struct ValidationEntryMatchContext'
365 * @param key peer identity (unused)
366 * @param value a 'struct ValidationEntry' to match
367 * @return GNUNET_YES if the entry does not match,
368 * GNUNET_NO if the entry does match
371 validation_entry_match (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
373 struct ValidationEntryMatchContext *vemc = cls;
374 struct ValidationEntry *ve = value;
376 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
386 * Iterate over validation entries and free them.
388 * @param cls (unused)
389 * @param key peer identity (unused)
390 * @param value a 'struct ValidationEntry' to clean up
391 * @return GNUNET_YES (continue to iterate)
394 cleanup_validation_entry (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
396 struct ValidationEntry *ve = value;
400 GST_blacklist_test_cancel (ve->bc);
403 GNUNET_break (GNUNET_OK ==
404 GNUNET_CONTAINER_multipeermap_remove (validation_map,
406 GNUNET_HELLO_address_free (ve->address);
407 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
409 GNUNET_SCHEDULER_cancel (ve->timeout_task);
410 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
412 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
414 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
415 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
417 if ((GNUNET_YES == ve->expecting_pong) &&
418 (validations_running > 0))
420 validations_running --;
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Validation finished, %u validation processes running\n",
423 validations_running);
431 * Address validation cleanup task. Assesses if the record is no
432 * longer valid and then possibly triggers its removal.
434 * @param cls the 'struct ValidationEntry'
435 * @param tc scheduler context (unused)
438 timeout_hello_validation (void *cls,
439 const struct GNUNET_SCHEDULER_TaskContext *tc)
441 struct ValidationEntry *ve = cls;
442 struct GNUNET_TIME_Absolute max;
443 struct GNUNET_TIME_Relative left;
445 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
446 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
447 left = GNUNET_TIME_absolute_get_remaining (max);
448 if (left.rel_value_us > 0)
450 /* should wait a bit longer */
452 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
455 GNUNET_STATISTICS_update (GST_stats,
456 gettext_noop ("# address records discarded"), 1,
458 cleanup_validation_entry (NULL, &ve->pid, ve);
463 * Function called with the result from blacklisting.
464 * Send a PING to the other peer if a communication is allowed.
466 * @param cls our 'struct ValidationEntry'
467 * @param pid identity of the other peer
468 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
471 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
474 struct ValidationEntry *ve = cls;
475 struct TransportPingMessage ping;
476 struct GNUNET_TRANSPORT_PluginFunctions *papi;
477 struct GNUNET_TIME_Absolute next;
478 const struct GNUNET_MessageHeader *hello;
479 enum GNUNET_ATS_Network_Type network;
487 if (GNUNET_NO == result)
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist denies to send PING to `%s' %s %s\n",
490 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s %s\n",
495 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
497 next = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), validation_delay);
498 if (next.abs_value_us > validation_next.abs_value_us)
499 validation_next = next; /* We're going to send a PING so delay next validation */
501 slen = strlen (ve->address->transport_name) + 1;
502 hello = GST_hello_get ();
503 hsize = ntohs (hello->size);
505 sizeof (struct TransportPingMessage) + ve->address->address_length +
509 htons (sizeof (struct TransportPingMessage) +
510 ve->address->address_length + slen);
511 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
512 ping.challenge = htonl (ve->challenge);
515 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
519 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
520 "HELLO", "PING", (unsigned int) tsize);
521 /* message too big (!?), get rid of HELLO */
524 sizeof (struct TransportPingMessage) + ve->address->address_length +
528 char message_buf[tsize];
530 /* build message with structure:
531 * [HELLO][TransportPingMessage][Transport name][Address] */
532 memcpy (message_buf, hello, hsize);
533 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
534 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
535 ve->address->transport_name, slen);
536 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
537 ve->address->address, ve->address->address_length);
538 papi = GST_plugins_find (ve->address->transport_name);
543 GNUNET_assert (papi->send != NULL);
544 GNUNET_assert (papi->get_session != NULL);
545 struct Session * session = papi->get_session(papi->cls, ve->address);
549 ret = papi->send (papi->cls, session,
551 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
553 network = papi->get_network (ve->address, session);
554 if (GNUNET_ATS_NET_UNSPECIFIED == network)
556 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
557 "Could not obtain a valid network for `%s' %s\n",
558 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
561 GST_neighbours_notify_data_sent (pid, ve->address, session, tsize);
565 /* Could not get a valid session */
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
567 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
574 ve->send_time = GNUNET_TIME_absolute_get ();
575 GNUNET_STATISTICS_update (GST_stats,
577 ("# PING without HELLO messages sent"), 1,
580 ve->network = network;
581 ve->expecting_pong = GNUNET_YES;
582 validations_running ++;
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Validation started, %u validation processes running\n",
585 validations_running);
591 * Do address validation again to keep address valid.
593 * @param cls the 'struct ValidationEntry'
594 * @param tc scheduler context (unused)
597 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
599 struct ValidationEntry *ve = cls;
600 struct GNUNET_TIME_Relative canonical_delay;
601 struct GNUNET_TIME_Relative delay;
602 struct GNUNET_TIME_Relative blocked_for;
603 struct GST_BlacklistCheck *bc;
606 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
607 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
608 /* How long until we can possibly permit the next PING? */
611 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
612 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value_us >
613 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
614 if (delay.rel_value_us > canonical_delay.rel_value_us * 2)
616 /* situation changed, recalculate delay */
617 delay = canonical_delay;
618 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
620 if (delay.rel_value_us > 0)
622 /* should wait a bit longer */
623 ve->revalidation_task =
624 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
627 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
628 if ((validations_running > validations_fast_start_threshold) &&
629 (blocked_for.rel_value_us > 0))
631 /* Validations are blocked, have to wait for blocked_for time */
632 ve->revalidation_task =
633 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
636 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
638 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
640 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
641 canonical_delay.rel_value_us);
643 /* Debug code for mantis 0002726 */
644 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
645 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, rdelay).rel_value_us)
648 delay = canonical_delay;
652 delay = GNUNET_TIME_relative_add (canonical_delay,
653 GNUNET_TIME_relative_multiply
654 (GNUNET_TIME_UNIT_MICROSECONDS, rdelay));
656 /* End debug code for mantis 0002726*/
657 ve->revalidation_task =
658 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
660 /* start PINGing by checking blacklist */
661 GNUNET_STATISTICS_update (GST_stats,
662 gettext_noop ("# address revalidations started"), 1,
664 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
665 &transmit_ping_if_allowed, ve);
667 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
673 * Find a ValidationEntry entry for the given neighbour that matches
674 * the given address and transport. If none exists, create one (but
675 * without starting any validation).
677 * @param public_key public key of the peer, NULL for unknown
678 * @param address address to find
679 * @return validation entry matching the given specifications, NULL
680 * if we don't have an existing entry and no public key was given
682 static struct ValidationEntry *
683 find_validation_entry (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
684 const struct GNUNET_HELLO_Address *address)
686 struct ValidationEntryMatchContext vemc;
687 struct ValidationEntry *ve;
690 vemc.address = address;
691 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
693 &validation_entry_match, &vemc);
694 if (NULL != (ve = vemc.ve))
696 if (public_key == NULL)
698 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
699 ve->in_use = GNUNET_SYSERR; /* not defined */
700 ve->last_line_set_to_no = 0;
701 ve->last_line_set_to_yes = 0;
702 ve->address = GNUNET_HELLO_address_copy (address);
703 ve->public_key = *public_key;
704 ve->pid = address->peer;
705 ve->pong_sig_valid_until = GNUNET_TIME_absolute_get_zero_();
706 memset (&ve->pong_sig_cache, '\0', sizeof (struct GNUNET_CRYPTO_EddsaSignature));
707 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
709 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
711 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
712 &timeout_hello_validation, ve);
713 GNUNET_CONTAINER_multipeermap_put (validation_map, &address->peer,
715 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
716 ve->expecting_pong = GNUNET_NO;
722 * Iterator which adds the given address to the set of validated
725 * @param cls original HELLO message
726 * @param address the address
727 * @param expiration expiration time
728 * @return GNUNET_OK (keep the address)
731 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
732 struct GNUNET_TIME_Absolute expiration)
734 const struct GNUNET_HELLO_Message *hello = cls;
735 struct ValidationEntry *ve;
736 struct GNUNET_PeerIdentity pid;
737 struct GNUNET_ATS_Information ats;
738 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
740 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
741 return GNUNET_OK; /* expired */
742 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
743 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
746 return GNUNET_OK; /* invalid HELLO !? */
748 if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
750 /* Peerinfo returned own identity, skip validation */
754 ve = find_validation_entry (&public_key, address);
755 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
757 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
758 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
760 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
761 ats.value = htonl (ve->network);
762 GNUNET_ATS_address_add (GST_ats, address, NULL, &ats, 1);
769 * Function called for any HELLO known to PEERINFO.
772 * @param peer id of the peer, NULL for last call
773 * @param hello hello message for the peer (can be NULL)
774 * @param err_msg error message
777 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
778 const struct GNUNET_HELLO_Message *hello,
781 GNUNET_assert (NULL != peer);
784 GNUNET_assert (NULL ==
785 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
792 * Start the validation subsystem.
794 * @param max_fds maximum number of fds to use
797 GST_validation_start (unsigned int max_fds)
800 * Initialization for validation throttling
802 * We have a maximum number max_fds of connections we can use for validation
803 * We monitor the number of validations in parallel and start to throttle it
804 * when doing to many validations in parallel:
805 * if (running validations < (max_fds / 2))
806 * - "fast start": run validation immediately
807 * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2)
808 * (300 sec / ~150 == ~2 sec.) between two validations
811 validation_next = GNUNET_TIME_absolute_get();
812 validation_delay.rel_value_us = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2);
813 validations_fast_start_threshold = (max_fds / 2);
814 validations_running = 0;
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Validation uses a fast start threshold of %u connections and a delay between of %s\n ",
816 validations_fast_start_threshold,
817 GNUNET_STRINGS_relative_time_to_string (validation_delay,
819 validation_map = GNUNET_CONTAINER_multipeermap_create (VALIDATION_MAP_SIZE,
821 pnc = GNUNET_PEERINFO_notify (GST_cfg, GNUNET_YES, &process_peerinfo_hello, NULL);
826 * Stop the validation subsystem.
829 GST_validation_stop ()
831 struct CheckHelloValidatedContext *chvc;
833 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
834 &cleanup_validation_entry, NULL);
835 GNUNET_CONTAINER_multipeermap_destroy (validation_map);
836 validation_map = NULL;
837 while (NULL != (chvc = chvc_head))
839 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
842 GNUNET_PEERINFO_notify_cancel (pnc);
847 * Send the given PONG to the given address.
849 * @param cls the PONG message
850 * @param public_key public key for the peer, never NULL
851 * @param valid_until is ZERO if we never validated the address,
852 * otherwise a time up to when we consider it (or was) valid
853 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
854 * is ZERO if the address is considered valid (no validation needed)
855 * otherwise a time in the future if we're currently denying re-validation
856 * @param address target address
859 multicast_pong (void *cls,
860 const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
861 struct GNUNET_TIME_Absolute valid_until,
862 struct GNUNET_TIME_Absolute validation_block,
863 const struct GNUNET_HELLO_Address *address)
865 struct TransportPongMessage *pong = cls;
866 struct GNUNET_TRANSPORT_PluginFunctions *papi;
868 papi = GST_plugins_find (address->transport_name);
872 GNUNET_assert (papi->send != NULL);
873 GNUNET_assert (papi->get_session != NULL);
875 struct Session * session = papi->get_session(papi->cls, address);
882 papi->send (papi->cls, session,
883 (const char *) pong, ntohs (pong->header.size),
884 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
886 GST_neighbours_notify_data_sent (&address->peer,
887 address, session, pong->header.size);
893 * We've received a PING. If appropriate, generate a PONG.
895 * @param sender peer sending the PING
896 * @param hdr the PING
897 * @param sender_address the sender address as we got it
898 * @param session session we got the PING from
901 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
902 const struct GNUNET_MessageHeader *hdr,
903 const struct GNUNET_HELLO_Address *sender_address,
904 struct Session *session)
906 const struct TransportPingMessage *ping;
907 struct TransportPongMessage *pong;
908 struct GNUNET_TRANSPORT_PluginFunctions *papi;
909 struct GNUNET_CRYPTO_EddsaSignature *sig_cache;
910 struct GNUNET_TIME_Absolute *sig_cache_exp;
918 int buggy = GNUNET_NO;
919 struct GNUNET_HELLO_Address address;
921 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
926 ping = (const struct TransportPingMessage *) hdr;
928 memcmp (&ping->target, &GST_my_identity,
929 sizeof (struct GNUNET_PeerIdentity)))
931 GNUNET_STATISTICS_update (GST_stats,
933 ("# PING message for different peer received"), 1,
937 GNUNET_STATISTICS_update (GST_stats,
938 gettext_noop ("# PING messages received"), 1,
940 addr = (const char *) &ping[1];
941 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
942 /* peer wants to confirm that this is one of our addresses, this is what is
943 * used for address validation */
946 sig_cache_exp = NULL;
950 addrend = memchr (addr, '\0', alen);
957 slen = strlen (addr) + 1;
959 address.address = addrend;
960 address.address_length = alen;
961 address.transport_name = addr;
962 address.peer = GST_my_identity;
964 if (NULL == address.transport_name)
969 if (0 != strstr (address.transport_name, "_client"))
971 plugin_name = GNUNET_strdup (address.transport_name);
972 pos = strstr (plugin_name, "_client");
973 GNUNET_assert (NULL != pos);
974 GNUNET_snprintf (pos, strlen ("_server") + 1, "%s", "_server");
977 plugin_name = GNUNET_strdup (address.transport_name);
979 if (NULL == (papi = GST_plugins_find (plugin_name)))
981 /* we don't have the plugin for this address */
982 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Plugin `%s' not available, cannot confirm having this address \n",
984 GNUNET_free (plugin_name);
987 GNUNET_free (plugin_name);
988 if (GNUNET_OK != papi->check_address (papi->cls, addrend, alen))
990 GNUNET_STATISTICS_update (GST_stats,
992 ("# failed address checks during validation"), 1,
994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address `%s' is not one of my addresses, not confirming PING\n",
995 GST_plugins_a2s (&address));
1000 GNUNET_STATISTICS_update (GST_stats,
1002 ("# successful address checks during validation"), 1,
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Address `%s' is one of my addresses, confirming PING\n",
1005 GST_plugins_a2s (&address));
1008 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
1010 if (GNUNET_NO == buggy)
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n",
1014 GNUNET_i2s (sender),
1015 GST_plugins_a2s (&address));
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 _("Received a PING message with validation bug from `%s'\n"),
1022 GNUNET_i2s (sender));
1028 addrend = NULL; /* make gcc happy */
1030 static struct GNUNET_CRYPTO_EddsaSignature no_address_signature;
1031 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
1033 sig_cache = &no_address_signature;
1034 sig_cache_exp = &no_address_signature_expiration;
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "I am `%s', sending PONG to peer `%s'\n",
1039 GNUNET_i2s_full (&GST_my_identity),
1040 GNUNET_i2s (sender));
1042 /* message with structure:
1043 * [TransportPongMessage][Transport name][Address] */
1045 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
1047 htons (sizeof (struct TransportPongMessage) + alen + slen);
1048 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1049 pong->purpose.size =
1050 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1051 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1053 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
1054 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
1055 pong->addrlen = htonl (alen + slen);
1056 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
1059 GNUNET_assert (NULL != addrend);
1060 memcpy (&((char *) &pong[1])[slen], addrend, alen);
1062 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value_us <
1063 PONG_SIGNATURE_LIFETIME.rel_value_us / 4)
1065 /* create / update cached sig */
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "Creating PONG signature to indicate ownership.\n");
1068 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1069 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1071 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key, &pong->purpose,
1074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1075 _("Failed to create PONG signature for peer `%s'\n"), GNUNET_i2s (sender));
1080 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1082 pong->signature = *sig_cache;
1084 GNUNET_assert (sender_address != NULL);
1086 /* first see if the session we got this PING from can be used to transmit
1087 * a response reliably */
1092 GNUNET_assert (papi->send != NULL);
1093 GNUNET_assert (papi->get_session != NULL);
1095 if (session == NULL)
1097 session = papi->get_session (papi->cls, sender_address);
1099 if (session == NULL)
1106 ret = papi->send (papi->cls, session,
1107 (const char *) pong, ntohs (pong->header.size),
1108 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1111 GST_neighbours_notify_data_sent (sender,
1112 sender_address, session, pong->header.size);
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Transmitted PONG to `%s' via reliable mechanism\n",
1120 GNUNET_i2s (sender));
1122 GNUNET_STATISTICS_update (GST_stats,
1124 ("# PONGs unicast via reliable transport"), 1,
1130 /* no reliable method found, try transmission via all known addresses */
1131 GNUNET_STATISTICS_update (GST_stats,
1133 ("# PONGs multicast to all available addresses"), 1,
1135 GST_validation_get_addresses (sender, &multicast_pong, pong);
1141 * Context for the 'validate_address' function
1143 struct ValidateAddressContext
1146 * Hash of the public key of the peer whose address is being validated.
1148 struct GNUNET_PeerIdentity pid;
1151 * Public key of the peer whose address is being validated.
1153 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
1158 * Iterator callback to go over all addresses and try to validate them
1159 * (unless blocked or already validated).
1161 * @param cls pointer to a 'struct ValidateAddressContext'
1162 * @param address the address
1163 * @param expiration expiration time
1164 * @return GNUNET_OK (keep the address)
1167 validate_address_iterator (void *cls,
1168 const struct GNUNET_HELLO_Address *address,
1169 struct GNUNET_TIME_Absolute expiration)
1171 const struct ValidateAddressContext *vac = cls;
1172 struct ValidationEntry *ve;
1174 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
1175 return GNUNET_OK; /* expired */
1176 ve = find_validation_entry (&vac->public_key, address);
1177 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1178 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1184 * Add the validated peer address to the HELLO.
1186 * @param cls the 'struct ValidationEntry' with the validated address
1187 * @param max space in buf
1188 * @param buf where to add the address
1189 * @return number of bytes written, 0 to signal the
1190 * end of the iteration.
1193 add_valid_peer_address (void *cls, size_t max, void *buf)
1195 struct ValidationEntry *ve = cls;
1197 if (GNUNET_YES == ve->copied)
1198 return 0; /* terminate */
1199 ve->copied = GNUNET_YES;
1200 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1205 * We've received a PONG. Check if it matches a pending PING and
1206 * mark the respective address as confirmed.
1208 * @param sender peer sending the PONG
1209 * @param hdr the PONG
1212 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1213 const struct GNUNET_MessageHeader *hdr)
1215 const struct TransportPongMessage *pong;
1216 struct ValidationEntry *ve;
1222 struct GNUNET_HELLO_Message *hello;
1223 struct GNUNET_HELLO_Address address;
1227 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1229 GNUNET_break_op (0);
1232 GNUNET_STATISTICS_update (GST_stats,
1233 gettext_noop ("# PONG messages received"), 1,
1236 /* message with structure:
1237 * [TransportPongMessage][Transport name][Address] */
1239 pong = (const struct TransportPongMessage *) hdr;
1240 tname = (const char *) &pong[1];
1241 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1242 addr = memchr (tname, '\0', size);
1245 GNUNET_break_op (0);
1249 slen = strlen (tname) + 1;
1250 addrlen = size - slen;
1251 address.peer = *sender;
1252 address.address = addr;
1253 address.address_length = addrlen;
1254 address.transport_name = tname;
1255 ve = find_validation_entry (NULL, &address);
1256 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1258 GNUNET_STATISTICS_update (GST_stats,
1260 ("# PONGs dropped, no matching pending validation"),
1264 /* now check that PONG is well-formed */
1265 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1267 GNUNET_break_op (0);
1270 if (GNUNET_TIME_absolute_get_remaining
1271 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value_us == 0)
1273 GNUNET_STATISTICS_update (GST_stats,
1275 ("# PONGs dropped, signature expired"), 1,
1280 sig_res = GNUNET_SYSERR;
1281 do_verify = GNUNET_YES;
1282 if (0 != GNUNET_TIME_absolute_get_remaining(ve->pong_sig_valid_until).rel_value_us)
1284 /* We have a cached and valid signature for this peer,
1285 * try to compare instead of verify */
1286 if (0 == memcmp (&ve->pong_sig_cache, &pong->signature, sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
1288 /* signatures are identical, we can skip verification */
1289 sig_res = GNUNET_OK;
1290 do_verify = GNUNET_NO;
1294 sig_res = GNUNET_SYSERR;
1295 /* signatures do not match, we have to verify */
1299 if (GNUNET_YES == do_verify)
1301 /* Do expensive verification */
1302 sig_res = GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1303 &pong->purpose, &pong->signature,
1305 if (sig_res == GNUNET_SYSERR)
1306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1307 "Failed to verify: invalid signature on address %s:%s from peer `%s'\n",
1308 tname, GST_plugins_a2s (ve->address),GNUNET_i2s (sender));
1311 if (sig_res == GNUNET_SYSERR)
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1316 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1317 /* validity achieved, remember it! */
1318 ve->expecting_pong = GNUNET_NO;
1319 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1320 ve->pong_sig_cache = pong->signature;
1321 ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh (pong->expiration);
1322 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1324 struct GNUNET_ATS_Information ats[2];
1325 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1326 ats[0].value = htonl ((uint32_t) ve->latency.rel_value_us);
1327 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1328 ats[1].value = htonl ((uint32_t) ve->network);
1329 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, ats, 2);
1331 if (validations_running > 0)
1333 validations_running --;
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Validation finished, %u validation processes running\n",
1336 validations_running);
1341 /* build HELLO to store in PEERINFO */
1342 ve->copied = GNUNET_NO;
1343 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve, GNUNET_NO);
1344 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1345 GNUNET_free (hello);
1350 * We've received a HELLO, check which addresses are new and trigger
1353 * @param hello the HELLO we received
1356 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1358 const struct GNUNET_HELLO_Message *hm =
1359 (const struct GNUNET_HELLO_Message *) hello;
1360 struct ValidateAddressContext vac;
1361 struct GNUNET_HELLO_Message *h;
1364 friend = GNUNET_HELLO_is_friend_only (hm);
1365 if (((GNUNET_YES != friend) && (GNUNET_NO != friend)) ||
1366 (GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1367 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1369 /* malformed HELLO */
1374 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1376 /* Add peer identity without addresses to peerinfo service */
1377 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL, friend);
1378 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1382 GNUNET_i2s (&vac.pid));
1385 GNUNET_assert (NULL ==
1386 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1387 &validate_address_iterator,
1393 * Closure for 'iterate_addresses'
1395 struct IteratorContext
1398 * Function to call on each address.
1400 GST_ValidationAddressCallback cb;
1411 * Call the callback in the closure for each validation entry.
1413 * @param cls the 'struct GST_ValidationIteratorContext'
1414 * @param key the peer's identity
1415 * @param value the 'struct ValidationEntry'
1416 * @return #GNUNET_OK (continue to iterate)
1419 iterate_addresses (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1421 struct IteratorContext *ic = cls;
1422 struct ValidationEntry *ve = value;
1424 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1431 * Call the given function for each address for the given target.
1432 * Can either give a snapshot (synchronous API) or be continuous.
1434 * @param target peer information is requested for
1435 * @param cb function to call; will not be called after this function returns
1436 * @param cb_cls closure for 'cb'
1439 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1440 GST_ValidationAddressCallback cb, void *cb_cls)
1442 struct IteratorContext ic;
1446 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
1448 &iterate_addresses, &ic);
1453 * Update if we are using an address for a connection actively right now.
1454 * Based on this, the validation module will measure latency for the
1455 * address more or less often.
1457 * @param address the address
1458 * @param session the session
1459 * @param in_use GNUNET_YES if we are now using the address for a connection,
1460 * GNUNET_NO if we are no longer using the address for a connection
1461 * @param line line of caller just for DEBUGGING!
1464 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1465 struct Session *session,
1469 struct ValidationEntry *ve;
1471 if (NULL != address)
1472 ve = find_validation_entry (NULL, address);
1474 ve = NULL; /* FIXME: lookup based on session... */
1477 /* this can happen for inbound connections (sender_address_len == 0); */
1480 if (ve->in_use == in_use)
1483 if (GNUNET_YES == in_use)
1485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1486 "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1487 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1488 ve->last_line_set_to_yes, line);
1490 if (GNUNET_NO == in_use)
1492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1493 "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1494 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1495 ve->last_line_set_to_no, line);
1499 if (GNUNET_YES == in_use)
1501 ve->last_line_set_to_yes = line;
1503 if (GNUNET_NO == in_use)
1505 ve->last_line_set_to_no = line;
1508 GNUNET_break (ve->in_use != in_use); /* should be different... */
1509 ve->in_use = in_use;
1510 if (in_use == GNUNET_YES)
1512 /* from now on, higher frequeny, so reschedule now */
1513 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1514 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1520 * Query validation about the latest observed latency on a given
1523 * @param sender peer
1524 * @param address the address
1525 * @param session session
1526 * @return observed latency of the address, FOREVER if the address was
1527 * never successfully validated
1529 struct GNUNET_TIME_Relative
1530 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1531 const struct GNUNET_HELLO_Address *address,
1532 struct Session *session)
1534 struct ValidationEntry *ve;
1536 if (NULL == address)
1538 GNUNET_break (0); // FIXME: support having latency only with session...
1539 return GNUNET_TIME_UNIT_FOREVER_REL;
1541 ve = find_validation_entry (NULL, address);
1543 return GNUNET_TIME_UNIT_FOREVER_REL;
1548 /* end of file gnunet-service-transport_validation.c */