1 /* Copyright (c) 2016-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Versioning information for different pieces of the Tor protocol.
8 * Starting in version 0.2.9.3-alpha, Tor places separate version numbers on
9 * each of the different components of its protocol. Relays use these numbers
10 * to advertise what versions of the protocols they can support, and clients
11 * use them to find what they can ask a given relay to do. Authorities vote
12 * on the supported protocol versions for each relay, and also vote on the
13 * which protocols you should have to support in order to be on the Tor
14 * network. All Tor instances use these required/recommended protocol versions
15 * to tell what level of support for recent protocols each relay has, and
16 * to decide whether they should be running given their current protocols.
18 * The main advantage of these protocol versions numbers over using Tor
19 * version numbers is that they allow different implementations of the Tor
20 * protocols to develop independently, without having to claim compatibility
21 * with specific versions of Tor.
24 #define PROTOVER_PRIVATE
28 #include "routerparse.h"
32 static const smartlist_t
*get_supported_protocol_list(void);
33 static int protocol_list_contains(const smartlist_t
*protos
,
34 protocol_type_t pr
, uint32_t ver
);
36 /** Mapping between protocol type string and protocol type. */
37 /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
39 protocol_type_t protover_type
;
41 } PROTOCOL_NAMES
[] = {
43 { PRT_LINKAUTH
, "LinkAuth" },
44 { PRT_RELAY
, "Relay" },
45 { PRT_DIRCACHE
, "DirCache" },
46 { PRT_HSDIR
, "HSDir" },
47 { PRT_HSINTRO
, "HSIntro" },
48 { PRT_HSREND
, "HSRend" },
50 { PRT_MICRODESC
, "Microdesc"},
54 #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
57 * Given a protocol_type_t, return the corresponding string used in
61 protocol_type_to_str(protocol_type_t pr
)
64 for (i
=0; i
< N_PROTOCOL_NAMES
; ++i
) {
65 if (PROTOCOL_NAMES
[i
].protover_type
== pr
)
66 return PROTOCOL_NAMES
[i
].name
;
69 tor_assert_nonfatal_unreached_once();
75 * Given a string, find the corresponding protocol type and store it in
76 * <b>pr_out</b>. Return 0 on success, -1 on failure.
79 str_to_protocol_type(const char *s
, protocol_type_t
*pr_out
)
85 for (i
=0; i
< N_PROTOCOL_NAMES
; ++i
) {
86 if (0 == strcmp(s
, PROTOCOL_NAMES
[i
].name
)) {
87 *pr_out
= PROTOCOL_NAMES
[i
].protover_type
;
96 * Release all space held by a single proto_entry_t structure
99 proto_entry_free_(proto_entry_t
*entry
)
103 tor_free(entry
->name
);
104 SMARTLIST_FOREACH(entry
->ranges
, proto_range_t
*, r
, tor_free(r
));
105 smartlist_free(entry
->ranges
);
109 /** The largest possible protocol version. */
110 #define MAX_PROTOCOL_VERSION (UINT32_MAX-1)
113 * Given a string <b>s</b> and optional end-of-string pointer
114 * <b>end_of_range</b>, parse the protocol range and store it in
115 * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
116 * U-U, where U is an unsigned 32-bit integer.
119 parse_version_range(const char *s
, const char *end_of_range
,
120 uint32_t *low_out
, uint32_t *high_out
)
126 tor_assert(high_out
);
129 if (BUG(!end_of_range
))
130 end_of_range
= s
+ strlen(s
); // LCOV_EXCL_LINE
132 /* A range must start with a digit. */
133 if (!TOR_ISDIGIT(*s
)) {
137 /* Note that this wouldn't be safe if we didn't know that eventually,
139 low
= (uint32_t) tor_parse_ulong(s
, 10, 0, MAX_PROTOCOL_VERSION
, &ok
, &next
);
142 if (next
> end_of_range
)
144 if (next
== end_of_range
) {
154 if (!TOR_ISDIGIT(*s
)) {
157 high
= (uint32_t) tor_parse_ulong(s
, 10, 0,
158 MAX_PROTOCOL_VERSION
, &ok
, &next
);
161 if (next
!= end_of_range
)
176 /** Parse a single protocol entry from <b>s</b> up to an optional
177 * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
180 * A protocol entry has a keyword, an = sign, and zero or more ranges. */
181 static proto_entry_t
*
182 parse_single_entry(const char *s
, const char *end_of_entry
)
184 proto_entry_t
*out
= tor_malloc_zero(sizeof(proto_entry_t
));
187 out
->ranges
= smartlist_new();
189 if (BUG (!end_of_entry
))
190 end_of_entry
= s
+ strlen(s
); // LCOV_EXCL_LINE
192 /* There must be an =. */
193 equals
= memchr(s
, '=', end_of_entry
- s
);
197 /* The name must be nonempty */
201 out
->name
= tor_strndup(s
, equals
-s
);
203 tor_assert(equals
< end_of_entry
);
206 while (s
< end_of_entry
) {
207 const char *comma
= memchr(s
, ',', end_of_entry
-s
);
208 proto_range_t
*range
= tor_malloc_zero(sizeof(proto_range_t
));
210 comma
= end_of_entry
;
212 smartlist_add(out
->ranges
, range
);
213 if (parse_version_range(s
, comma
, &range
->low
, &range
->high
) < 0) {
218 while (*s
== ',' && s
< end_of_entry
)
225 proto_entry_free(out
);
230 * Parse the protocol list from <b>s</b> and return it as a smartlist of
234 parse_protocol_list(const char *s
)
236 smartlist_t
*entries
= smartlist_new();
239 /* Find the next space or the NUL. */
240 const char *end_of_entry
= strchr(s
, ' ');
241 proto_entry_t
*entry
;
243 end_of_entry
= s
+ strlen(s
);
245 entry
= parse_single_entry(s
, end_of_entry
);
250 smartlist_add(entries
, entry
);
260 SMARTLIST_FOREACH(entries
, proto_entry_t
*, ent
, proto_entry_free(ent
));
261 smartlist_free(entries
);
266 * Given a protocol type and version number, return true iff we know
267 * how to speak that protocol.
270 protover_is_supported_here(protocol_type_t pr
, uint32_t ver
)
272 const smartlist_t
*ours
= get_supported_protocol_list();
273 return protocol_list_contains(ours
, pr
, ver
);
277 * Return true iff "list" encodes a protocol list that includes support for
278 * the indicated protocol and version.
281 protocol_list_supports_protocol(const char *list
, protocol_type_t tp
,
284 /* NOTE: This is a pretty inefficient implementation. If it ever shows
285 * up in profiles, we should memoize it.
287 smartlist_t
*protocols
= parse_protocol_list(list
);
291 int contains
= protocol_list_contains(protocols
, tp
, version
);
293 SMARTLIST_FOREACH(protocols
, proto_entry_t
*, ent
, proto_entry_free(ent
));
294 smartlist_free(protocols
);
299 * Return true iff "list" encodes a protocol list that includes support for
300 * the indicated protocol and version, or some later version.
303 protocol_list_supports_protocol_or_later(const char *list
,
307 /* NOTE: This is a pretty inefficient implementation. If it ever shows
308 * up in profiles, we should memoize it.
310 smartlist_t
*protocols
= parse_protocol_list(list
);
314 const char *pr_name
= protocol_type_to_str(tp
);
317 SMARTLIST_FOREACH_BEGIN(protocols
, proto_entry_t
*, proto
) {
318 if (strcasecmp(proto
->name
, pr_name
))
320 SMARTLIST_FOREACH_BEGIN(proto
->ranges
, const proto_range_t
*, range
) {
321 if (range
->high
>= version
) {
325 } SMARTLIST_FOREACH_END(range
);
326 } SMARTLIST_FOREACH_END(proto
);
329 SMARTLIST_FOREACH(protocols
, proto_entry_t
*, ent
, proto_entry_free(ent
));
330 smartlist_free(protocols
);
334 /** Return the canonical string containing the list of protocols
335 * that we support. */
336 /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
338 protover_get_supported_protocols(void)
353 /** The protocols from protover_get_supported_protocols(), as parsed into a
354 * list of proto_entry_t values. Access this via
355 * get_supported_protocol_list. */
356 static smartlist_t
*supported_protocol_list
= NULL
;
358 /** Return a pointer to a smartlist of proto_entry_t for the protocols
360 static const smartlist_t
*
361 get_supported_protocol_list(void)
363 if (PREDICT_UNLIKELY(supported_protocol_list
== NULL
)) {
364 supported_protocol_list
=
365 parse_protocol_list(protover_get_supported_protocols());
367 return supported_protocol_list
;
371 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
372 * as one or more newly allocated strings.
375 proto_entry_encode_into(smartlist_t
*chunks
, const proto_entry_t
*entry
)
377 smartlist_add_asprintf(chunks
, "%s=", entry
->name
);
379 SMARTLIST_FOREACH_BEGIN(entry
->ranges
, proto_range_t
*, range
) {
380 const char *comma
= "";
381 if (range_sl_idx
!= 0)
384 if (range
->low
== range
->high
) {
385 smartlist_add_asprintf(chunks
, "%s%lu",
386 comma
, (unsigned long)range
->low
);
388 smartlist_add_asprintf(chunks
, "%s%lu-%lu",
389 comma
, (unsigned long)range
->low
,
390 (unsigned long)range
->high
);
392 } SMARTLIST_FOREACH_END(range
);
395 /** Given a list of space-separated proto_entry_t items,
396 * encode it into a newly allocated space-separated string. */
398 encode_protocol_list(const smartlist_t
*sl
)
400 const char *separator
= "";
401 smartlist_t
*chunks
= smartlist_new();
402 SMARTLIST_FOREACH_BEGIN(sl
, const proto_entry_t
*, ent
) {
403 smartlist_add_strdup(chunks
, separator
);
405 proto_entry_encode_into(chunks
, ent
);
408 } SMARTLIST_FOREACH_END(ent
);
410 char *result
= smartlist_join_strings(chunks
, "", 0, NULL
);
412 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
413 smartlist_free(chunks
);
418 /* We treat any protocol list with more than this many subprotocols in it
419 * as a DoS attempt. */
420 /// C_RUST_COUPLED: src/rust/protover/protover.rs
421 /// `MAX_PROTOCOLS_TO_EXPAND`
422 static const int MAX_PROTOCOLS_TO_EXPAND
= (1<<16);
424 /** Voting helper: Given a list of proto_entry_t, return a newly allocated
425 * smartlist of newly allocated strings, one for each included protocol
426 * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
429 * Do not list any protocol version more than once.
431 * Return NULL if the list would be too big.
434 expand_protocol_list(const smartlist_t
*protos
)
436 smartlist_t
*expanded
= smartlist_new();
440 SMARTLIST_FOREACH_BEGIN(protos
, const proto_entry_t
*, ent
) {
441 const char *name
= ent
->name
;
442 SMARTLIST_FOREACH_BEGIN(ent
->ranges
, const proto_range_t
*, range
) {
444 for (u
= range
->low
; u
<= range
->high
; ++u
) {
445 smartlist_add_asprintf(expanded
, "%s=%lu", name
, (unsigned long)u
);
446 if (smartlist_len(expanded
) > MAX_PROTOCOLS_TO_EXPAND
)
449 } SMARTLIST_FOREACH_END(range
);
450 } SMARTLIST_FOREACH_END(ent
);
452 smartlist_sort_strings(expanded
);
453 smartlist_uniq_strings(expanded
); // This makes voting work. do not remove
457 SMARTLIST_FOREACH(expanded
, char *, cp
, tor_free(cp
));
458 smartlist_free(expanded
);
462 /** Voting helper: compare two singleton proto_entry_t items by version
463 * alone. (A singleton item is one with a single range entry where
466 cmp_single_ent_by_version(const void **a_
, const void **b_
)
468 const proto_entry_t
*ent_a
= *a_
;
469 const proto_entry_t
*ent_b
= *b_
;
471 tor_assert(smartlist_len(ent_a
->ranges
) == 1);
472 tor_assert(smartlist_len(ent_b
->ranges
) == 1);
474 const proto_range_t
*a
= smartlist_get(ent_a
->ranges
, 0);
475 const proto_range_t
*b
= smartlist_get(ent_b
->ranges
, 0);
477 tor_assert(a
->low
== a
->high
);
478 tor_assert(b
->low
== b
->high
);
480 if (a
->low
< b
->low
) {
482 } else if (a
->low
== b
->low
) {
489 /** Voting helper: Given a list of singleton protocol strings (of the form
490 * Foo=7), return a canonical listing of all the protocol versions listed,
491 * with as few ranges as possible, with protocol versions sorted lexically and
492 * versions sorted in numerically increasing order, using as few range entries
496 contract_protocol_list(const smartlist_t
*proto_strings
)
498 // map from name to list of single-version entries
499 strmap_t
*entry_lists_by_name
= strmap_new();
500 // list of protocol names
501 smartlist_t
*all_names
= smartlist_new();
502 // list of strings for the output we're building
503 smartlist_t
*chunks
= smartlist_new();
505 // Parse each item and stick it entry_lists_by_name. Build
506 // 'all_names' at the same time.
507 SMARTLIST_FOREACH_BEGIN(proto_strings
, const char *, s
) {
509 continue;// LCOV_EXCL_LINE
510 proto_entry_t
*ent
= parse_single_entry(s
, s
+strlen(s
));
512 continue; // LCOV_EXCL_LINE
513 smartlist_t
*lst
= strmap_get(entry_lists_by_name
, ent
->name
);
515 smartlist_add(all_names
, ent
->name
);
516 lst
= smartlist_new();
517 strmap_set(entry_lists_by_name
, ent
->name
, lst
);
519 smartlist_add(lst
, ent
);
520 } SMARTLIST_FOREACH_END(s
);
522 // We want to output the protocols sorted by their name.
523 smartlist_sort_strings(all_names
);
525 SMARTLIST_FOREACH_BEGIN(all_names
, const char *, name
) {
526 const int first_entry
= (name_sl_idx
== 0);
527 smartlist_t
*lst
= strmap_get(entry_lists_by_name
, name
);
529 // Sort every entry with this name by version. They are
530 // singletons, so there can't be overlap.
531 smartlist_sort(lst
, cmp_single_ent_by_version
);
534 smartlist_add_strdup(chunks
, " ");
536 /* We're going to construct this entry from the ranges. */
537 proto_entry_t
*entry
= tor_malloc_zero(sizeof(proto_entry_t
));
538 entry
->ranges
= smartlist_new();
539 entry
->name
= tor_strdup(name
);
541 // Now, find all the ranges of versions start..end where
542 // all of start, start+1, start+2, ..end are included.
543 int start_of_cur_series
= 0;
544 while (start_of_cur_series
< smartlist_len(lst
)) {
545 const proto_entry_t
*ent
= smartlist_get(lst
, start_of_cur_series
);
546 const proto_range_t
*range
= smartlist_get(ent
->ranges
, 0);
547 const uint32_t ver_low
= range
->low
;
548 uint32_t ver_high
= ver_low
;
551 for (idx
= start_of_cur_series
+1; idx
< smartlist_len(lst
); ++idx
) {
552 ent
= smartlist_get(lst
, idx
);
553 range
= smartlist_get(ent
->ranges
, 0);
554 if (range
->low
!= ver_high
+ 1)
559 // Now idx is either off the end of the list, or the first sequence
560 // break in the list.
561 start_of_cur_series
= idx
;
563 proto_range_t
*new_range
= tor_malloc_zero(sizeof(proto_range_t
));
564 new_range
->low
= ver_low
;
565 new_range
->high
= ver_high
;
566 smartlist_add(entry
->ranges
, new_range
);
568 proto_entry_encode_into(chunks
, entry
);
569 proto_entry_free(entry
);
571 } SMARTLIST_FOREACH_END(name
);
573 // Build the result...
574 char *result
= smartlist_join_strings(chunks
, "", 0, NULL
);
576 // And free all the stuff we allocated.
577 SMARTLIST_FOREACH_BEGIN(all_names
, const char *, name
) {
578 smartlist_t
*lst
= strmap_get(entry_lists_by_name
, name
);
580 SMARTLIST_FOREACH(lst
, proto_entry_t
*, e
, proto_entry_free(e
));
582 } SMARTLIST_FOREACH_END(name
);
584 strmap_free(entry_lists_by_name
, NULL
);
585 smartlist_free(all_names
);
586 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
587 smartlist_free(chunks
);
593 * Protocol voting implementation.
595 * Given a list of strings describing protocol versions, return a newly
596 * allocated string encoding all of the protocols that are listed by at
597 * least <b>threshold</b> of the inputs.
599 * The string is minimal and sorted according to the rules of
600 * contract_protocol_list above.
603 protover_compute_vote(const smartlist_t
*list_of_proto_strings
,
606 smartlist_t
*all_entries
= smartlist_new();
608 // First, parse the inputs and break them into singleton entries.
609 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings
, const char *, vote
) {
610 smartlist_t
*unexpanded
= parse_protocol_list(vote
);
612 log_warn(LD_NET
, "I failed with parsing a protocol list from "
613 "an authority. The offending string was: %s",
617 smartlist_t
*this_vote
= expand_protocol_list(unexpanded
);
618 if (this_vote
== NULL
) {
619 log_warn(LD_NET
, "When expanding a protocol list from an authority, I "
620 "got too many protocols. This is possibly an attack or a bug, "
621 "unless the Tor network truly has expanded to support over %d "
622 "different subprotocol versions. The offending string was: %s",
623 MAX_PROTOCOLS_TO_EXPAND
, escaped(vote
));
625 smartlist_add_all(all_entries
, this_vote
);
626 smartlist_free(this_vote
);
628 SMARTLIST_FOREACH(unexpanded
, proto_entry_t
*, e
, proto_entry_free(e
));
629 smartlist_free(unexpanded
);
630 } SMARTLIST_FOREACH_END(vote
);
632 // Now sort the singleton entries
633 smartlist_sort_strings(all_entries
);
635 // Now find all the strings that appear at least 'threshold' times.
636 smartlist_t
*include_entries
= smartlist_new();
637 const char *cur_entry
= smartlist_get(all_entries
, 0);
639 SMARTLIST_FOREACH_BEGIN(all_entries
, const char *, ent
) {
640 if (!strcmp(ent
, cur_entry
)) {
643 if (n_times
>= threshold
&& cur_entry
)
644 smartlist_add(include_entries
, (void*)cur_entry
);
648 } SMARTLIST_FOREACH_END(ent
);
650 if (n_times
>= threshold
&& cur_entry
)
651 smartlist_add(include_entries
, (void*)cur_entry
);
653 // Finally, compress that list.
654 char *result
= contract_protocol_list(include_entries
);
655 smartlist_free(include_entries
);
656 SMARTLIST_FOREACH(all_entries
, char *, cp
, tor_free(cp
));
657 smartlist_free(all_entries
);
662 /** Return true if every protocol version described in the string <b>s</b> is
663 * one that we support, and false otherwise. If <b>missing_out</b> is
664 * provided, set it to the list of protocols we do not support.
666 * NOTE: This is quadratic, but we don't do it much: only a few times per
667 * consensus. Checking signatures should be way more expensive than this
671 protover_all_supported(const char *s
, char **missing_out
)
673 int all_supported
= 1;
674 smartlist_t
*missing
;
680 smartlist_t
*entries
= parse_protocol_list(s
);
681 if (BUG(entries
== NULL
)) {
682 log_warn(LD_NET
, "Received an unparseable protocol list %s"
683 " from the consensus", escaped(s
));
687 missing
= smartlist_new();
689 SMARTLIST_FOREACH_BEGIN(entries
, const proto_entry_t
*, ent
) {
691 if (str_to_protocol_type(ent
->name
, &tp
) < 0) {
692 if (smartlist_len(ent
->ranges
)) {
698 SMARTLIST_FOREACH_BEGIN(ent
->ranges
, const proto_range_t
*, range
) {
700 for (i
= range
->low
; i
<= range
->high
; ++i
) {
701 if (!protover_is_supported_here(tp
, i
)) {
705 } SMARTLIST_FOREACH_END(range
);
711 smartlist_add(missing
, (void*) ent
);
712 } SMARTLIST_FOREACH_END(ent
);
714 if (missing_out
&& !all_supported
) {
715 tor_assert(0 != smartlist_len(missing
));
716 *missing_out
= encode_protocol_list(missing
);
718 smartlist_free(missing
);
720 SMARTLIST_FOREACH(entries
, proto_entry_t
*, ent
, proto_entry_free(ent
));
721 smartlist_free(entries
);
723 return all_supported
;
726 /** Helper: Given a list of proto_entry_t, return true iff
727 * <b>pr</b>=<b>ver</b> is included in that list. */
729 protocol_list_contains(const smartlist_t
*protos
,
730 protocol_type_t pr
, uint32_t ver
)
732 if (BUG(protos
== NULL
)) {
733 return 0; // LCOV_EXCL_LINE
735 const char *pr_name
= protocol_type_to_str(pr
);
736 if (BUG(pr_name
== NULL
)) {
737 return 0; // LCOV_EXCL_LINE
740 SMARTLIST_FOREACH_BEGIN(protos
, const proto_entry_t
*, ent
) {
741 if (strcasecmp(ent
->name
, pr_name
))
743 /* name matches; check the ranges */
744 SMARTLIST_FOREACH_BEGIN(ent
->ranges
, const proto_range_t
*, range
) {
745 if (ver
>= range
->low
&& ver
<= range
->high
)
747 } SMARTLIST_FOREACH_END(range
);
748 } SMARTLIST_FOREACH_END(ent
);
753 /** Return a string describing the protocols supported by tor version
754 * <b>version</b>, or an empty string if we cannot tell.
756 * Note that this is only used to infer protocols for Tor versions that
757 * can't declare their own.
759 /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
761 protover_compute_for_old_tor(const char *version
)
763 if (version
== NULL
) {
764 /* No known version; guess the oldest series that is still supported. */
765 version
= "0.2.5.15";
768 if (tor_version_as_new_as(version
,
769 FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS
)) {
771 } else if (tor_version_as_new_as(version
, "0.2.9.1-alpha")) {
772 /* 0.2.9.1-alpha HSRend=2 */
773 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
774 "Link=1-4 LinkAuth=1 "
775 "Microdesc=1-2 Relay=1-2";
776 } else if (tor_version_as_new_as(version
, "0.2.7.5")) {
777 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
778 * ed25519 support. We'll call them present only in "stable" 027,
780 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
781 "Link=1-4 LinkAuth=1 "
782 "Microdesc=1-2 Relay=1-2";
783 } else if (tor_version_as_new_as(version
, "0.2.4.19")) {
784 /* No currently supported Tor server versions are older than this, or
785 * lack these protocols. */
786 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
787 "Link=1-4 LinkAuth=1 "
788 "Microdesc=1 Relay=1-2";
790 /* Cannot infer protocols. */
796 * Release all storage held by static fields in protover.c
799 protover_free_all(void)
801 if (supported_protocol_list
) {
802 smartlist_t
*entries
= supported_protocol_list
;
803 SMARTLIST_FOREACH(entries
, proto_entry_t
*, ent
, proto_entry_free(ent
));
804 smartlist_free(entries
);
805 supported_protocol_list
= NULL
;
809 #endif /* !defined(HAVE_RUST) */