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,
472 const struct GNUNET_PeerIdentity *pid,
475 struct ValidationEntry *ve = cls;
476 struct TransportPingMessage ping;
477 struct GNUNET_TRANSPORT_PluginFunctions *papi;
478 struct GNUNET_TIME_Absolute next;
479 const struct GNUNET_MessageHeader *hello;
480 enum GNUNET_ATS_Network_Type network;
487 if (GNUNET_NO == result)
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Blacklist denies to send PING to `%s' %s %s\n",
492 GST_plugins_a2s (ve->address),
493 ve->address->transport_name);
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Transmitting plain PING to `%s' %s %s\n",
500 GST_plugins_a2s (ve->address),
501 ve->address->transport_name);
503 next = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
505 if (next.abs_value_us > validation_next.abs_value_us)
506 validation_next = next; /* We're going to send a PING so delay next validation */
508 slen = strlen (ve->address->transport_name) + 1;
509 hello = GST_hello_get ();
510 hsize = ntohs (hello->size);
512 sizeof (struct TransportPingMessage) + ve->address->address_length +
516 htons (sizeof (struct TransportPingMessage) +
517 ve->address->address_length + slen);
518 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
519 ping.challenge = htonl (ve->challenge);
522 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
524 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
526 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
527 "HELLO", "PING", (unsigned int) tsize);
528 /* message too big (!?), get rid of HELLO */
531 sizeof (struct TransportPingMessage) + ve->address->address_length +
535 char message_buf[tsize];
537 /* build message with structure:
538 * [HELLO][TransportPingMessage][Transport name][Address] */
539 memcpy (message_buf, hello, hsize);
540 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
541 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
542 ve->address->transport_name, slen);
543 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
544 ve->address->address, ve->address->address_length);
545 papi = GST_plugins_find (ve->address->transport_name);
550 GNUNET_assert (papi->send != NULL);
551 GNUNET_assert (papi->get_session != NULL);
552 struct Session * session = papi->get_session(papi->cls, ve->address);
556 ret = papi->send (papi->cls, session,
558 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
560 network = papi->get_network (ve->address, session);
561 if (GNUNET_ATS_NET_UNSPECIFIED == network)
563 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
564 "Could not obtain a valid network for `%s' %s\n",
565 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
568 GST_neighbours_notify_data_sent (pid, ve->address, session, tsize);
572 /* Could not get a valid session */
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
574 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
581 ve->send_time = GNUNET_TIME_absolute_get ();
582 GNUNET_STATISTICS_update (GST_stats,
584 ("# PING without HELLO messages sent"), 1,
587 ve->network = network;
588 ve->expecting_pong = GNUNET_YES;
589 validations_running ++;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Validation started, %u validation processes running\n",
592 validations_running);
598 * Do address validation again to keep address valid.
600 * @param cls the 'struct ValidationEntry'
601 * @param tc scheduler context (unused)
604 revalidate_address (void *cls,
605 const struct GNUNET_SCHEDULER_TaskContext *tc)
607 struct ValidationEntry *ve = cls;
608 struct GNUNET_TIME_Relative canonical_delay;
609 struct GNUNET_TIME_Relative delay;
610 struct GNUNET_TIME_Relative blocked_for;
611 struct GST_BlacklistCheck *bc;
614 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
615 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
616 /* How long until we can possibly permit the next PING? */
619 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
620 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value_us >
621 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
622 if (delay.rel_value_us > canonical_delay.rel_value_us * 2)
624 /* situation changed, recalculate delay */
625 delay = canonical_delay;
626 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
628 if (delay.rel_value_us > 0)
630 /* should wait a bit longer */
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Waiting for %s longer before validating address %s\n",
633 GNUNET_STRINGS_relative_time_to_string (delay,
635 GST_plugins_a2s (ve->address));
636 ve->revalidation_task =
637 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
640 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
641 if ((validations_running > validations_fast_start_threshold) &&
642 (blocked_for.rel_value_us > 0))
644 /* Validations are blocked, have to wait for blocked_for time */
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Validations blocked for another %s, delaying validating address %s\n",
647 GNUNET_STRINGS_relative_time_to_string (blocked_for,
649 GST_plugins_a2s (ve->address));
650 ve->revalidation_task =
651 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
654 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
656 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
658 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
659 canonical_delay.rel_value_us);
661 /* Debug code for mantis 0002726 */
662 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
663 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, rdelay).rel_value_us)
666 delay = canonical_delay;
670 delay = GNUNET_TIME_relative_add (canonical_delay,
671 GNUNET_TIME_relative_multiply
672 (GNUNET_TIME_UNIT_MICROSECONDS, rdelay));
674 /* End debug code for mantis 0002726*/
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Validating now, next scheduled for %s, now validating address %s\n",
677 GNUNET_STRINGS_relative_time_to_string (blocked_for,
679 GST_plugins_a2s (ve->address));
680 ve->revalidation_task =
681 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
683 /* start PINGing by checking blacklist */
684 GNUNET_STATISTICS_update (GST_stats,
685 gettext_noop ("# address revalidations started"), 1,
687 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
688 &transmit_ping_if_allowed, ve);
690 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
696 * Find a ValidationEntry entry for the given neighbour that matches
697 * the given address and transport. If none exists, create one (but
698 * without starting any validation).
700 * @param public_key public key of the peer, NULL for unknown
701 * @param address address to find
702 * @return validation entry matching the given specifications, NULL
703 * if we don't have an existing entry and no public key was given
705 static struct ValidationEntry *
706 find_validation_entry (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
707 const struct GNUNET_HELLO_Address *address)
709 struct ValidationEntryMatchContext vemc;
710 struct ValidationEntry *ve;
713 vemc.address = address;
714 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
716 &validation_entry_match, &vemc);
717 if (NULL != (ve = vemc.ve))
719 if (public_key == NULL)
721 ve = GNUNET_new (struct ValidationEntry);
722 ve->in_use = GNUNET_SYSERR; /* not defined */
723 ve->last_line_set_to_no = 0;
724 ve->last_line_set_to_yes = 0;
725 ve->address = GNUNET_HELLO_address_copy (address);
726 ve->public_key = *public_key;
727 ve->pid = address->peer;
728 ve->pong_sig_valid_until = GNUNET_TIME_absolute_get_zero_();
729 memset (&ve->pong_sig_cache, '\0', sizeof (struct GNUNET_CRYPTO_EddsaSignature));
730 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
732 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
734 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
735 &timeout_hello_validation, ve);
736 GNUNET_CONTAINER_multipeermap_put (validation_map, &address->peer,
738 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
739 ve->expecting_pong = GNUNET_NO;
745 * Iterator which adds the given address to the set of validated
748 * @param cls original HELLO message
749 * @param address the address
750 * @param expiration expiration time
751 * @return #GNUNET_OK (keep the address)
754 add_valid_address (void *cls,
755 const struct GNUNET_HELLO_Address *address,
756 struct GNUNET_TIME_Absolute expiration)
758 const struct GNUNET_HELLO_Message *hello = cls;
759 struct ValidationEntry *ve;
760 struct GNUNET_PeerIdentity pid;
761 struct GNUNET_ATS_Information ats;
762 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
764 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
765 return GNUNET_OK; /* expired */
766 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
767 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
770 return GNUNET_OK; /* invalid HELLO !? */
772 if (0 == memcmp (&GST_my_identity,
774 sizeof (struct GNUNET_PeerIdentity)))
776 /* Peerinfo returned own identity, skip validation */
780 ve = find_validation_entry (&public_key, address);
781 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
783 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Starting revalidations for valid address %s\n",
787 GST_plugins_a2s (ve->address));
788 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
791 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
792 ats.value = htonl (ve->network);
793 GNUNET_ATS_address_add (GST_ats, address, NULL, &ats, 1);
800 * Function called for any HELLO known to PEERINFO.
803 * @param peer id of the peer, NULL for last call
804 * @param hello hello message for the peer (can be NULL)
805 * @param err_msg error message
808 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
809 const struct GNUNET_HELLO_Message *hello,
812 GNUNET_assert (NULL != peer);
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Handling HELLO for peer %s\n",
818 GNUNET_assert (NULL ==
819 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
826 * Start the validation subsystem.
828 * @param max_fds maximum number of fds to use
831 GST_validation_start (unsigned int max_fds)
834 * Initialization for validation throttling
836 * We have a maximum number max_fds of connections we can use for validation
837 * We monitor the number of validations in parallel and start to throttle it
838 * when doing to many validations in parallel:
839 * if (running validations < (max_fds / 2))
840 * - "fast start": run validation immediately
841 * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2)
842 * (300 sec / ~150 == ~2 sec.) between two validations
845 validation_next = GNUNET_TIME_absolute_get();
846 validation_delay.rel_value_us = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2);
847 validations_fast_start_threshold = (max_fds / 2);
848 validations_running = 0;
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Validation uses a fast start threshold of %u connections and a delay between of %s\n ",
850 validations_fast_start_threshold,
851 GNUNET_STRINGS_relative_time_to_string (validation_delay,
853 validation_map = GNUNET_CONTAINER_multipeermap_create (VALIDATION_MAP_SIZE,
855 pnc = GNUNET_PEERINFO_notify (GST_cfg, GNUNET_YES,
856 &process_peerinfo_hello, NULL);
861 * Stop the validation subsystem.
864 GST_validation_stop ()
866 struct CheckHelloValidatedContext *chvc;
868 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
869 &cleanup_validation_entry, NULL);
870 GNUNET_CONTAINER_multipeermap_destroy (validation_map);
871 validation_map = NULL;
872 while (NULL != (chvc = chvc_head))
874 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
877 GNUNET_PEERINFO_notify_cancel (pnc);
882 * Send the given PONG to the given address.
884 * @param cls the PONG message
885 * @param public_key public key for the peer, never NULL
886 * @param valid_until is ZERO if we never validated the address,
887 * otherwise a time up to when we consider it (or was) valid
888 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
889 * is ZERO if the address is considered valid (no validation needed)
890 * otherwise a time in the future if we're currently denying re-validation
891 * @param address target address
894 multicast_pong (void *cls,
895 const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
896 struct GNUNET_TIME_Absolute valid_until,
897 struct GNUNET_TIME_Absolute validation_block,
898 const struct GNUNET_HELLO_Address *address)
900 struct TransportPongMessage *pong = cls;
901 struct GNUNET_TRANSPORT_PluginFunctions *papi;
903 papi = GST_plugins_find (address->transport_name);
907 GNUNET_assert (papi->send != NULL);
908 GNUNET_assert (papi->get_session != NULL);
910 struct Session * session = papi->get_session(papi->cls, address);
917 papi->send (papi->cls, session,
918 (const char *) pong, ntohs (pong->header.size),
919 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
921 GST_neighbours_notify_data_sent (&address->peer,
922 address, session, pong->header.size);
928 * We've received a PING. If appropriate, generate a PONG.
930 * @param sender peer sending the PING
931 * @param hdr the PING
932 * @param sender_address the sender address as we got it
933 * @param session session we got the PING from
934 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
937 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
938 const struct GNUNET_MessageHeader *hdr,
939 const struct GNUNET_HELLO_Address *sender_address,
940 struct Session *session)
942 const struct TransportPingMessage *ping;
943 struct TransportPongMessage *pong;
944 struct GNUNET_TRANSPORT_PluginFunctions *papi;
945 struct GNUNET_CRYPTO_EddsaSignature *sig_cache;
946 struct GNUNET_TIME_Absolute *sig_cache_exp;
954 int buggy = GNUNET_NO;
955 struct GNUNET_HELLO_Address address;
957 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
960 return GNUNET_SYSERR;
962 ping = (const struct TransportPingMessage *) hdr;
964 memcmp (&ping->target, &GST_my_identity,
965 sizeof (struct GNUNET_PeerIdentity)))
967 GNUNET_STATISTICS_update (GST_stats,
969 ("# PING message for different peer received"), 1,
971 return GNUNET_SYSERR;
973 GNUNET_STATISTICS_update (GST_stats,
974 gettext_noop ("# PING messages received"), 1,
976 addr = (const char *) &ping[1];
977 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
978 /* peer wants to confirm that this is one of our addresses, this is what is
979 * used for address validation */
982 sig_cache_exp = NULL;
986 addrend = memchr (addr, '\0', alen);
990 return GNUNET_SYSERR;
993 slen = strlen (addr) + 1;
995 address.address = addrend;
996 address.address_length = alen;
997 address.transport_name = addr;
998 address.peer = GST_my_identity;
1000 if (NULL == address.transport_name)
1005 if (0 != strstr (address.transport_name, "_client"))
1007 plugin_name = GNUNET_strdup (address.transport_name);
1008 pos = strstr (plugin_name, "_client");
1009 GNUNET_assert (NULL != pos);
1010 GNUNET_snprintf (pos, strlen ("_server") + 1, "%s", "_server");
1013 plugin_name = GNUNET_strdup (address.transport_name);
1015 if (NULL == (papi = GST_plugins_find (plugin_name)))
1017 /* we don't have the plugin for this address */
1018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1019 _("Plugin `%s' not available, cannot confirm having this address\n"),
1021 GNUNET_free (plugin_name);
1022 return GNUNET_SYSERR;
1024 GNUNET_free (plugin_name);
1025 if (GNUNET_OK != papi->check_address (papi->cls, addrend, alen))
1027 GNUNET_STATISTICS_update (GST_stats,
1029 ("# failed address checks during validation"), 1,
1031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1032 _("Address `%s' is not one of my addresses, not confirming PING\n"),
1033 GST_plugins_a2s (&address));
1034 return GNUNET_SYSERR;
1038 GNUNET_STATISTICS_update (GST_stats,
1040 ("# successful address checks during validation"), 1,
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Address `%s' is one of my addresses, confirming PING\n",
1044 GST_plugins_a2s (&address));
1047 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
1049 if (GNUNET_NO == buggy)
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 _("Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n"),
1053 GNUNET_i2s (sender),
1054 GST_plugins_a2s (&address));
1055 return GNUNET_SYSERR;
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 _("Received a PING message with validation bug from `%s'\n"),
1061 GNUNET_i2s (sender));
1067 addrend = NULL; /* make gcc happy */
1069 static struct GNUNET_CRYPTO_EddsaSignature no_address_signature;
1070 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
1072 sig_cache = &no_address_signature;
1073 sig_cache_exp = &no_address_signature_expiration;
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "I am `%s', sending PONG to peer `%s'\n",
1078 GNUNET_i2s_full (&GST_my_identity),
1079 GNUNET_i2s (sender));
1081 /* message with structure:
1082 * [TransportPongMessage][Transport name][Address] */
1084 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
1086 htons (sizeof (struct TransportPongMessage) + alen + slen);
1087 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1088 pong->purpose.size =
1089 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1090 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1092 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
1093 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
1094 pong->addrlen = htonl (alen + slen);
1095 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
1098 GNUNET_assert (NULL != addrend);
1099 memcpy (&((char *) &pong[1])[slen], addrend, alen);
1101 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value_us <
1102 PONG_SIGNATURE_LIFETIME.rel_value_us / 4)
1104 /* create / update cached sig */
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Creating PONG signature to indicate ownership.\n");
1107 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1108 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1110 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key, &pong->purpose,
1113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1114 _("Failed to create PONG signature for peer `%s'\n"), GNUNET_i2s (sender));
1119 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1121 pong->signature = *sig_cache;
1123 GNUNET_assert (sender_address != NULL);
1125 /* first see if the session we got this PING from can be used to transmit
1126 * a response reliably */
1131 GNUNET_assert (papi->send != NULL);
1132 GNUNET_assert (papi->get_session != NULL);
1134 if (session == NULL)
1136 session = papi->get_session (papi->cls, sender_address);
1138 if (session == NULL)
1145 ret = papi->send (papi->cls, session,
1146 (const char *) pong, ntohs (pong->header.size),
1147 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1150 GST_neighbours_notify_data_sent (sender,
1151 sender_address, session,
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Transmitted PONG to `%s' via reliable mechanism\n",
1159 GNUNET_i2s (sender));
1161 GNUNET_STATISTICS_update (GST_stats,
1163 ("# PONGs unicast via reliable transport"), 1,
1169 /* no reliable method found, try transmission via all known addresses */
1170 GNUNET_STATISTICS_update (GST_stats,
1172 ("# PONGs multicast to all available addresses"), 1,
1174 GST_validation_get_addresses (sender,
1175 &multicast_pong, pong);
1182 * Context for the #validate_address_iterator() function
1184 struct ValidateAddressContext
1187 * Hash of the public key of the peer whose address is being validated.
1189 struct GNUNET_PeerIdentity pid;
1192 * Public key of the peer whose address is being validated.
1194 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
1200 * Iterator callback to go over all addresses and try to validate them
1201 * (unless blocked or already validated).
1203 * @param cls pointer to a `struct ValidateAddressContext`
1204 * @param address the address
1205 * @param expiration expiration time
1206 * @return #GNUNET_OK (keep the address)
1209 validate_address_iterator (void *cls,
1210 const struct GNUNET_HELLO_Address *address,
1211 struct GNUNET_TIME_Absolute expiration)
1213 const struct ValidateAddressContext *vac = cls;
1214 struct ValidationEntry *ve;
1216 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Skipping expired address from HELLO\n");
1220 return GNUNET_OK; /* expired */
1222 ve = find_validation_entry (&vac->public_key, address);
1223 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Starting validation for fresh address %s\n",
1227 GST_plugins_a2s (ve->address));
1228 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1235 * Add the validated peer address to the HELLO.
1237 * @param cls the 'struct ValidationEntry' with the validated address
1238 * @param max space in buf
1239 * @param buf where to add the address
1240 * @return number of bytes written, 0 to signal the
1241 * end of the iteration.
1244 add_valid_peer_address (void *cls, size_t max, void *buf)
1246 struct ValidationEntry *ve = cls;
1248 if (GNUNET_YES == ve->copied)
1249 return 0; /* terminate */
1250 ve->copied = GNUNET_YES;
1251 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1256 * We've received a PONG. Check if it matches a pending PING and
1257 * mark the respective address as confirmed.
1259 * @param sender peer sending the PONG
1260 * @param hdr the PONG
1261 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1264 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1265 const struct GNUNET_MessageHeader *hdr)
1267 const struct TransportPongMessage *pong;
1268 struct ValidationEntry *ve;
1274 struct GNUNET_HELLO_Message *hello;
1275 struct GNUNET_HELLO_Address address;
1279 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1281 GNUNET_break_op (0);
1282 return GNUNET_SYSERR;
1284 GNUNET_STATISTICS_update (GST_stats,
1285 gettext_noop ("# PONG messages received"), 1,
1288 /* message with structure:
1289 * [TransportPongMessage][Transport name][Address] */
1291 pong = (const struct TransportPongMessage *) hdr;
1292 tname = (const char *) &pong[1];
1293 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1294 addr = memchr (tname, '\0', size);
1297 GNUNET_break_op (0);
1298 return GNUNET_SYSERR;
1301 slen = strlen (tname) + 1;
1302 addrlen = size - slen;
1303 address.peer = *sender;
1304 address.address = addr;
1305 address.address_length = addrlen;
1306 address.transport_name = tname;
1307 ve = find_validation_entry (NULL, &address);
1308 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1310 GNUNET_STATISTICS_update (GST_stats,
1312 ("# PONGs dropped, no matching pending validation"),
1316 /* now check that PONG is well-formed */
1317 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1319 GNUNET_break_op (0);
1320 return GNUNET_SYSERR;
1322 if (GNUNET_TIME_absolute_get_remaining
1323 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value_us == 0)
1325 GNUNET_STATISTICS_update (GST_stats,
1327 ("# PONGs dropped, signature expired"), 1,
1329 return GNUNET_SYSERR;
1332 sig_res = GNUNET_SYSERR;
1333 do_verify = GNUNET_YES;
1334 if (0 != GNUNET_TIME_absolute_get_remaining(ve->pong_sig_valid_until).rel_value_us)
1336 /* We have a cached and valid signature for this peer,
1337 * try to compare instead of verify */
1338 if (0 == memcmp (&ve->pong_sig_cache, &pong->signature, sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
1340 /* signatures are identical, we can skip verification */
1341 sig_res = GNUNET_OK;
1342 do_verify = GNUNET_NO;
1346 sig_res = GNUNET_SYSERR;
1347 /* signatures do not match, we have to verify */
1351 if (GNUNET_YES == do_verify)
1353 /* Do expensive verification */
1354 sig_res = GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1355 &pong->purpose, &pong->signature,
1357 if (sig_res == GNUNET_SYSERR)
1359 GNUNET_break_op (0);
1360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1361 "Failed to verify: invalid signature on address %s:%s from peer `%s'\n",
1363 GST_plugins_a2s (ve->address),
1364 GNUNET_i2s (sender));
1367 if (sig_res == GNUNET_SYSERR)
1369 GNUNET_break_op (0);
1370 return GNUNET_SYSERR;
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1375 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1376 /* validity achieved, remember it! */
1377 ve->expecting_pong = GNUNET_NO;
1378 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1379 ve->pong_sig_cache = pong->signature;
1380 ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh (pong->expiration);
1381 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1383 struct GNUNET_ATS_Information ats[2];
1385 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1386 ats[0].value = htonl ((uint32_t) ve->latency.rel_value_us);
1387 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1388 ats[1].value = htonl ((uint32_t) ve->network);
1389 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, ats, 2);
1391 if (validations_running > 0)
1393 validations_running --;
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Validation finished, %u validation processes running\n",
1396 validations_running);
1403 /* build HELLO to store in PEERINFO */
1404 ve->copied = GNUNET_NO;
1405 hello = GNUNET_HELLO_create (&ve->public_key,
1406 &add_valid_peer_address, ve,
1408 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1409 GNUNET_free (hello);
1415 * We've received a HELLO, check which addresses are new and trigger
1418 * @param hello the HELLO we received
1419 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1422 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1424 const struct GNUNET_HELLO_Message *hm =
1425 (const struct GNUNET_HELLO_Message *) hello;
1426 struct ValidateAddressContext vac;
1427 struct GNUNET_HELLO_Message *h;
1430 friend = GNUNET_HELLO_is_friend_only (hm);
1431 if ( ( (GNUNET_YES != friend) &&
1432 (GNUNET_NO != friend) ) ||
1433 (GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1434 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1436 /* malformed HELLO */
1437 GNUNET_break_op (0);
1438 return GNUNET_SYSERR;
1441 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1443 /* Add peer identity without addresses to peerinfo service */
1444 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL, friend);
1445 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1449 GNUNET_i2s (&vac.pid));
1452 GNUNET_assert (NULL ==
1453 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1454 &validate_address_iterator,
1461 * Closure for #iterate_addresses().
1463 struct IteratorContext
1466 * Function to call on each address.
1468 GST_ValidationAddressCallback cb;
1471 * Closure for @e cb.
1479 * Call the callback in the closure for each validation entry.
1481 * @param cls the `struct IteratorContext`
1482 * @param key the peer's identity
1483 * @param value the `struct ValidationEntry`
1484 * @return #GNUNET_OK (continue to iterate)
1487 iterate_addresses (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1489 struct IteratorContext *ic = cls;
1490 struct ValidationEntry *ve = value;
1492 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1499 * Call the given function for each address for the given target.
1500 * Can either give a snapshot (synchronous API) or be continuous.
1502 * @param target peer information is requested for
1503 * @param cb function to call; will not be called after this function returns
1504 * @param cb_cls closure for 'cb'
1507 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1508 GST_ValidationAddressCallback cb, void *cb_cls)
1510 struct IteratorContext ic;
1514 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
1516 &iterate_addresses, &ic);
1521 * Update if we are using an address for a connection actively right now.
1522 * Based on this, the validation module will measure latency for the
1523 * address more or less often.
1525 * @param address the address
1526 * @param session the session
1527 * @param in_use #GNUNET_YES if we are now using the address for a connection,
1528 * #GNUNET_NO if we are no longer using the address for a connection
1529 * @param line line of caller just for DEBUGGING!
1532 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1533 struct Session *session,
1537 struct ValidationEntry *ve;
1539 if (NULL != address)
1540 ve = find_validation_entry (NULL, address);
1542 ve = NULL; /* FIXME: lookup based on session... */
1545 /* this can happen for inbound connections (sender_address_len == 0); */
1548 if (ve->in_use == in_use)
1551 if (GNUNET_YES == in_use)
1553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1554 "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1555 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1556 ve->last_line_set_to_yes, line);
1558 if (GNUNET_NO == in_use)
1560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1561 "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1562 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1563 ve->last_line_set_to_no, line);
1567 if (GNUNET_YES == in_use)
1569 ve->last_line_set_to_yes = line;
1571 if (GNUNET_NO == in_use)
1573 ve->last_line_set_to_no = line;
1576 GNUNET_break (ve->in_use != in_use); /* should be different... */
1577 ve->in_use = in_use;
1578 if (in_use == GNUNET_YES)
1580 /* from now on, higher frequeny, so reschedule now */
1581 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1582 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1588 * Query validation about the latest observed latency on a given
1591 * @param sender peer
1592 * @param address the address
1593 * @param session session
1594 * @return observed latency of the address, FOREVER if the address was
1595 * never successfully validated
1597 struct GNUNET_TIME_Relative
1598 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1599 const struct GNUNET_HELLO_Address *address,
1600 struct Session *session)
1602 struct ValidationEntry *ve;
1604 if (NULL == address)
1606 GNUNET_break (0); // FIXME: support having latency only with session...
1607 return GNUNET_TIME_UNIT_FOREVER_REL;
1609 ve = find_validation_entry (NULL, address);
1611 return GNUNET_TIME_UNIT_FOREVER_REL;
1616 /* end of file gnunet-service-transport_validation.c */