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"
30 static const smartlist_t
*get_supported_protocol_list(void);
31 static int protocol_list_contains(const smartlist_t
*protos
,
32 protocol_type_t pr
, uint32_t ver
);
34 /** Mapping between protocol type string and protocol type. */
36 protocol_type_t protover_type
;
38 } PROTOCOL_NAMES
[] = {
40 { PRT_LINKAUTH
, "LinkAuth" },
41 { PRT_RELAY
, "Relay" },
42 { PRT_DIRCACHE
, "DirCache" },
43 { PRT_HSDIR
, "HSDir" },
44 { PRT_HSINTRO
, "HSIntro" },
45 { PRT_HSREND
, "HSRend" },
47 { PRT_MICRODESC
, "Microdesc"},
51 #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
54 * Given a protocol_type_t, return the corresponding string used in
58 protocol_type_to_str(protocol_type_t pr
)
61 for (i
=0; i
< N_PROTOCOL_NAMES
; ++i
) {
62 if (PROTOCOL_NAMES
[i
].protover_type
== pr
)
63 return PROTOCOL_NAMES
[i
].name
;
66 tor_assert_nonfatal_unreached_once();
72 * Given a string, find the corresponding protocol type and store it in
73 * <b>pr_out</b>. Return 0 on success, -1 on failure.
76 str_to_protocol_type(const char *s
, protocol_type_t
*pr_out
)
82 for (i
=0; i
< N_PROTOCOL_NAMES
; ++i
) {
83 if (0 == strcmp(s
, PROTOCOL_NAMES
[i
].name
)) {
84 *pr_out
= PROTOCOL_NAMES
[i
].protover_type
;
93 * Release all space held by a single proto_entry_t structure
96 proto_entry_free(proto_entry_t
*entry
)
100 tor_free(entry
->name
);
101 SMARTLIST_FOREACH(entry
->ranges
, proto_range_t
*, r
, tor_free(r
));
102 smartlist_free(entry
->ranges
);
107 * Given a string <b>s</b> and optional end-of-string pointer
108 * <b>end_of_range</b>, parse the protocol range and store it in
109 * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
110 * U-U, where U is an unsigned 32-bit integer.
113 parse_version_range(const char *s
, const char *end_of_range
,
114 uint32_t *low_out
, uint32_t *high_out
)
120 tor_assert(high_out
);
123 if (BUG(!end_of_range
))
124 end_of_range
= s
+ strlen(s
); // LCOV_EXCL_LINE
126 /* Note that this wouldn't be safe if we didn't know that eventually,
128 low
= (uint32_t) tor_parse_ulong(s
, 10, 0, UINT32_MAX
, &ok
, &next
);
131 if (next
> end_of_range
)
133 if (next
== end_of_range
) {
142 high
= (uint32_t) tor_parse_ulong(s
, 10, 0, UINT32_MAX
, &ok
, &next
);
145 if (next
!= end_of_range
)
157 /** Parse a single protocol entry from <b>s</b> up to an optional
158 * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
161 * A protocol entry has a keyword, an = sign, and zero or more ranges. */
162 static proto_entry_t
*
163 parse_single_entry(const char *s
, const char *end_of_entry
)
165 proto_entry_t
*out
= tor_malloc_zero(sizeof(proto_entry_t
));
168 out
->ranges
= smartlist_new();
170 if (BUG (!end_of_entry
))
171 end_of_entry
= s
+ strlen(s
); // LCOV_EXCL_LINE
173 /* There must be an =. */
174 equals
= memchr(s
, '=', end_of_entry
- s
);
178 /* The name must be nonempty */
182 out
->name
= tor_strndup(s
, equals
-s
);
184 tor_assert(equals
< end_of_entry
);
187 while (s
< end_of_entry
) {
188 const char *comma
= memchr(s
, ',', end_of_entry
-s
);
189 proto_range_t
*range
= tor_malloc_zero(sizeof(proto_range_t
));
191 comma
= end_of_entry
;
193 smartlist_add(out
->ranges
, range
);
194 if (parse_version_range(s
, comma
, &range
->low
, &range
->high
) < 0) {
198 if (range
->low
> range
->high
) {
203 while (*s
== ',' && s
< end_of_entry
)
210 proto_entry_free(out
);
215 * Parse the protocol list from <b>s</b> and return it as a smartlist of
219 parse_protocol_list(const char *s
)
221 smartlist_t
*entries
= smartlist_new();
224 /* Find the next space or the NUL. */
225 const char *end_of_entry
= strchr(s
, ' ');
226 proto_entry_t
*entry
;
228 end_of_entry
= s
+ strlen(s
);
230 entry
= parse_single_entry(s
, end_of_entry
);
235 smartlist_add(entries
, entry
);
245 SMARTLIST_FOREACH(entries
, proto_entry_t
*, ent
, proto_entry_free(ent
));
246 smartlist_free(entries
);
251 * Given a protocol type and version number, return true iff we know
252 * how to speak that protocol.
255 protover_is_supported_here(protocol_type_t pr
, uint32_t ver
)
257 const smartlist_t
*ours
= get_supported_protocol_list();
258 return protocol_list_contains(ours
, pr
, ver
);
262 * Return true iff "list" encodes a protocol list that includes support for
263 * the indicated protocol and version.
266 protocol_list_supports_protocol(const char *list
, protocol_type_t tp
,
269 /* NOTE: This is a pretty inefficient implementation. If it ever shows
270 * up in profiles, we should memoize it.
272 smartlist_t
*protocols
= parse_protocol_list(list
);
276 int contains
= protocol_list_contains(protocols
, tp
, version
);
278 SMARTLIST_FOREACH(protocols
, proto_entry_t
*, ent
, proto_entry_free(ent
));
279 smartlist_free(protocols
);
283 /** Return the canonical string containing the list of protocols
284 * that we support. */
286 protover_get_supported_protocols(void)
301 /** The protocols from protover_get_supported_protocols(), as parsed into a
302 * list of proto_entry_t values. Access this via
303 * get_supported_protocol_list. */
304 static smartlist_t
*supported_protocol_list
= NULL
;
306 /** Return a pointer to a smartlist of proto_entry_t for the protocols
308 static const smartlist_t
*
309 get_supported_protocol_list(void)
311 if (PREDICT_UNLIKELY(supported_protocol_list
== NULL
)) {
312 supported_protocol_list
=
313 parse_protocol_list(protover_get_supported_protocols());
315 return supported_protocol_list
;
319 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
320 * as one or more newly allocated strings.
323 proto_entry_encode_into(smartlist_t
*chunks
, const proto_entry_t
*entry
)
325 smartlist_add_asprintf(chunks
, "%s=", entry
->name
);
327 SMARTLIST_FOREACH_BEGIN(entry
->ranges
, proto_range_t
*, range
) {
328 const char *comma
= "";
329 if (range_sl_idx
!= 0)
332 if (range
->low
== range
->high
) {
333 smartlist_add_asprintf(chunks
, "%s%lu",
334 comma
, (unsigned long)range
->low
);
336 smartlist_add_asprintf(chunks
, "%s%lu-%lu",
337 comma
, (unsigned long)range
->low
,
338 (unsigned long)range
->high
);
340 } SMARTLIST_FOREACH_END(range
);
343 /** Given a list of space-separated proto_entry_t items,
344 * encode it into a newly allocated space-separated string. */
346 encode_protocol_list(const smartlist_t
*sl
)
348 const char *separator
= "";
349 smartlist_t
*chunks
= smartlist_new();
350 SMARTLIST_FOREACH_BEGIN(sl
, const proto_entry_t
*, ent
) {
351 smartlist_add_strdup(chunks
, separator
);
353 proto_entry_encode_into(chunks
, ent
);
356 } SMARTLIST_FOREACH_END(ent
);
358 char *result
= smartlist_join_strings(chunks
, "", 0, NULL
);
360 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
361 smartlist_free(chunks
);
366 /* We treat any protocol list with more than this many subprotocols in it
367 * as a DoS attempt. */
368 static const int MAX_PROTOCOLS_TO_EXPAND
= (1<<16);
370 /** Voting helper: Given a list of proto_entry_t, return a newly allocated
371 * smartlist of newly allocated strings, one for each included protocol
372 * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
375 * Do not list any protocol version more than once.
377 * Return NULL if the list would be too big.
380 expand_protocol_list(const smartlist_t
*protos
)
382 smartlist_t
*expanded
= smartlist_new();
386 SMARTLIST_FOREACH_BEGIN(protos
, const proto_entry_t
*, ent
) {
387 const char *name
= ent
->name
;
388 SMARTLIST_FOREACH_BEGIN(ent
->ranges
, const proto_range_t
*, range
) {
390 for (u
= range
->low
; u
<= range
->high
; ++u
) {
391 smartlist_add_asprintf(expanded
, "%s=%lu", name
, (unsigned long)u
);
392 if (smartlist_len(expanded
) > MAX_PROTOCOLS_TO_EXPAND
)
395 } SMARTLIST_FOREACH_END(range
);
396 } SMARTLIST_FOREACH_END(ent
);
398 smartlist_sort_strings(expanded
);
399 smartlist_uniq_strings(expanded
); // This makes voting work. do not remove
403 SMARTLIST_FOREACH(expanded
, char *, cp
, tor_free(cp
));
404 smartlist_free(expanded
);
408 /** Voting helper: compare two singleton proto_entry_t items by version
409 * alone. (A singleton item is one with a single range entry where
412 cmp_single_ent_by_version(const void **a_
, const void **b_
)
414 const proto_entry_t
*ent_a
= *a_
;
415 const proto_entry_t
*ent_b
= *b_
;
417 tor_assert(smartlist_len(ent_a
->ranges
) == 1);
418 tor_assert(smartlist_len(ent_b
->ranges
) == 1);
420 const proto_range_t
*a
= smartlist_get(ent_a
->ranges
, 0);
421 const proto_range_t
*b
= smartlist_get(ent_b
->ranges
, 0);
423 tor_assert(a
->low
== a
->high
);
424 tor_assert(b
->low
== b
->high
);
426 if (a
->low
< b
->low
) {
428 } else if (a
->low
== b
->low
) {
435 /** Voting helper: Given a list of singleton protocol strings (of the form
436 * Foo=7), return a canonical listing of all the protocol versions listed,
437 * with as few ranges as possible, with protocol versions sorted lexically and
438 * versions sorted in numerically increasing order, using as few range entries
442 contract_protocol_list(const smartlist_t
*proto_strings
)
444 // map from name to list of single-version entries
445 strmap_t
*entry_lists_by_name
= strmap_new();
446 // list of protocol names
447 smartlist_t
*all_names
= smartlist_new();
448 // list of strings for the output we're building
449 smartlist_t
*chunks
= smartlist_new();
451 // Parse each item and stick it entry_lists_by_name. Build
452 // 'all_names' at the same time.
453 SMARTLIST_FOREACH_BEGIN(proto_strings
, const char *, s
) {
455 continue;// LCOV_EXCL_LINE
456 proto_entry_t
*ent
= parse_single_entry(s
, s
+strlen(s
));
458 continue; // LCOV_EXCL_LINE
459 smartlist_t
*lst
= strmap_get(entry_lists_by_name
, ent
->name
);
461 smartlist_add(all_names
, ent
->name
);
462 lst
= smartlist_new();
463 strmap_set(entry_lists_by_name
, ent
->name
, lst
);
465 smartlist_add(lst
, ent
);
466 } SMARTLIST_FOREACH_END(s
);
468 // We want to output the protocols sorted by their name.
469 smartlist_sort_strings(all_names
);
471 SMARTLIST_FOREACH_BEGIN(all_names
, const char *, name
) {
472 const int first_entry
= (name_sl_idx
== 0);
473 smartlist_t
*lst
= strmap_get(entry_lists_by_name
, name
);
475 // Sort every entry with this name by version. They are
476 // singletons, so there can't be overlap.
477 smartlist_sort(lst
, cmp_single_ent_by_version
);
480 smartlist_add_strdup(chunks
, " ");
482 /* We're going to construct this entry from the ranges. */
483 proto_entry_t
*entry
= tor_malloc_zero(sizeof(proto_entry_t
));
484 entry
->ranges
= smartlist_new();
485 entry
->name
= tor_strdup(name
);
487 // Now, find all the ranges of versions start..end where
488 // all of start, start+1, start+2, ..end are included.
489 int start_of_cur_series
= 0;
490 while (start_of_cur_series
< smartlist_len(lst
)) {
491 const proto_entry_t
*ent
= smartlist_get(lst
, start_of_cur_series
);
492 const proto_range_t
*range
= smartlist_get(ent
->ranges
, 0);
493 const uint32_t ver_low
= range
->low
;
494 uint32_t ver_high
= ver_low
;
497 for (idx
= start_of_cur_series
+1; idx
< smartlist_len(lst
); ++idx
) {
498 ent
= smartlist_get(lst
, idx
);
499 range
= smartlist_get(ent
->ranges
, 0);
500 if (range
->low
!= ver_high
+ 1)
505 // Now idx is either off the end of the list, or the first sequence
506 // break in the list.
507 start_of_cur_series
= idx
;
509 proto_range_t
*new_range
= tor_malloc_zero(sizeof(proto_range_t
));
510 new_range
->low
= ver_low
;
511 new_range
->high
= ver_high
;
512 smartlist_add(entry
->ranges
, new_range
);
514 proto_entry_encode_into(chunks
, entry
);
515 proto_entry_free(entry
);
517 } SMARTLIST_FOREACH_END(name
);
519 // Build the result...
520 char *result
= smartlist_join_strings(chunks
, "", 0, NULL
);
522 // And free all the stuff we allocated.
523 SMARTLIST_FOREACH_BEGIN(all_names
, const char *, name
) {
524 smartlist_t
*lst
= strmap_get(entry_lists_by_name
, name
);
526 SMARTLIST_FOREACH(lst
, proto_entry_t
*, e
, proto_entry_free(e
));
528 } SMARTLIST_FOREACH_END(name
);
530 strmap_free(entry_lists_by_name
, NULL
);
531 smartlist_free(all_names
);
532 SMARTLIST_FOREACH(chunks
, char *, cp
, tor_free(cp
));
533 smartlist_free(chunks
);
539 * Protocol voting implementation.
541 * Given a list of strings describing protocol versions, return a newly
542 * allocated string encoding all of the protocols that are listed by at
543 * least <b>threshold</b> of the inputs.
545 * The string is minimal and sorted according to the rules of
546 * contract_protocol_list above.
549 protover_compute_vote(const smartlist_t
*list_of_proto_strings
,
552 smartlist_t
*all_entries
= smartlist_new();
554 // First, parse the inputs and break them into singleton entries.
555 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings
, const char *, vote
) {
556 smartlist_t
*unexpanded
= parse_protocol_list(vote
);
557 smartlist_t
*this_vote
= expand_protocol_list(unexpanded
);
558 if (this_vote
== NULL
) {
559 log_warn(LD_NET
, "When expanding a protocol list from an authority, I "
560 "got too many protocols. This is possibly an attack or a bug, "
561 "unless the Tor network truly has expanded to support over %d "
562 "different subprotocol versions. The offending string was: %s",
563 MAX_PROTOCOLS_TO_EXPAND
, escaped(vote
));
565 smartlist_add_all(all_entries
, this_vote
);
566 smartlist_free(this_vote
);
568 SMARTLIST_FOREACH(unexpanded
, proto_entry_t
*, e
, proto_entry_free(e
));
569 smartlist_free(unexpanded
);
570 } SMARTLIST_FOREACH_END(vote
);
572 // Now sort the singleton entries
573 smartlist_sort_strings(all_entries
);
575 // Now find all the strings that appear at least 'threshold' times.
576 smartlist_t
*include_entries
= smartlist_new();
577 const char *cur_entry
= smartlist_get(all_entries
, 0);
579 SMARTLIST_FOREACH_BEGIN(all_entries
, const char *, ent
) {
580 if (!strcmp(ent
, cur_entry
)) {
583 if (n_times
>= threshold
&& cur_entry
)
584 smartlist_add(include_entries
, (void*)cur_entry
);
588 } SMARTLIST_FOREACH_END(ent
);
590 if (n_times
>= threshold
&& cur_entry
)
591 smartlist_add(include_entries
, (void*)cur_entry
);
593 // Finally, compress that list.
594 char *result
= contract_protocol_list(include_entries
);
595 smartlist_free(include_entries
);
596 SMARTLIST_FOREACH(all_entries
, char *, cp
, tor_free(cp
));
597 smartlist_free(all_entries
);
602 /** Return true if every protocol version described in the string <b>s</b> is
603 * one that we support, and false otherwise. If <b>missing_out</b> is
604 * provided, set it to the list of protocols we do not support.
606 * NOTE: This is quadratic, but we don't do it much: only a few times per
607 * consensus. Checking signatures should be way more expensive than this
611 protover_all_supported(const char *s
, char **missing_out
)
613 int all_supported
= 1;
614 smartlist_t
*missing
;
620 smartlist_t
*entries
= parse_protocol_list(s
);
622 missing
= smartlist_new();
624 SMARTLIST_FOREACH_BEGIN(entries
, const proto_entry_t
*, ent
) {
626 if (str_to_protocol_type(ent
->name
, &tp
) < 0) {
627 if (smartlist_len(ent
->ranges
)) {
633 SMARTLIST_FOREACH_BEGIN(ent
->ranges
, const proto_range_t
*, range
) {
635 for (i
= range
->low
; i
<= range
->high
; ++i
) {
636 if (!protover_is_supported_here(tp
, i
)) {
640 } SMARTLIST_FOREACH_END(range
);
646 smartlist_add(missing
, (void*) ent
);
647 } SMARTLIST_FOREACH_END(ent
);
649 if (missing_out
&& !all_supported
) {
650 tor_assert(0 != smartlist_len(missing
));
651 *missing_out
= encode_protocol_list(missing
);
653 smartlist_free(missing
);
655 SMARTLIST_FOREACH(entries
, proto_entry_t
*, ent
, proto_entry_free(ent
));
656 smartlist_free(entries
);
658 return all_supported
;
661 /** Helper: Given a list of proto_entry_t, return true iff
662 * <b>pr</b>=<b>ver</b> is included in that list. */
664 protocol_list_contains(const smartlist_t
*protos
,
665 protocol_type_t pr
, uint32_t ver
)
667 if (BUG(protos
== NULL
)) {
668 return 0; // LCOV_EXCL_LINE
670 const char *pr_name
= protocol_type_to_str(pr
);
671 if (BUG(pr_name
== NULL
)) {
672 return 0; // LCOV_EXCL_LINE
675 SMARTLIST_FOREACH_BEGIN(protos
, const proto_entry_t
*, ent
) {
676 if (strcasecmp(ent
->name
, pr_name
))
678 /* name matches; check the ranges */
679 SMARTLIST_FOREACH_BEGIN(ent
->ranges
, const proto_range_t
*, range
) {
680 if (ver
>= range
->low
&& ver
<= range
->high
)
682 } SMARTLIST_FOREACH_END(range
);
683 } SMARTLIST_FOREACH_END(ent
);
688 /** Return a string describing the protocols supported by tor version
689 * <b>version</b>, or an empty string if we cannot tell.
691 * Note that this is only used to infer protocols for Tor versions that
692 * can't declare their own.
695 protover_compute_for_old_tor(const char *version
)
697 if (tor_version_as_new_as(version
,
698 FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS
)) {
700 } else if (tor_version_as_new_as(version
, "0.2.9.1-alpha")) {
701 /* 0.2.9.1-alpha HSRend=2 */
702 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
703 "Link=1-4 LinkAuth=1 "
704 "Microdesc=1-2 Relay=1-2";
705 } else if (tor_version_as_new_as(version
, "0.2.7.5")) {
706 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
707 * ed25519 support. We'll call them present only in "stable" 027,
709 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
710 "Link=1-4 LinkAuth=1 "
711 "Microdesc=1-2 Relay=1-2";
712 } else if (tor_version_as_new_as(version
, "0.2.4.19")) {
713 /* No currently supported Tor server versions are older than this, or
714 * lack these protocols. */
715 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
716 "Link=1-4 LinkAuth=1 "
717 "Microdesc=1 Relay=1-2";
719 /* Cannot infer protocols. */
725 * Release all storage held by static fields in protover.c
728 protover_free_all(void)
730 if (supported_protocol_list
) {
731 smartlist_t
*entries
= supported_protocol_list
;
732 SMARTLIST_FOREACH(entries
, proto_entry_t
*, ent
, proto_entry_free(ent
));
733 smartlist_free(entries
);
734 supported_protocol_list
= NULL
;