Tor 0.4.9.0-alpha-dev
protover.c
Go to the documentation of this file.
1/* Copyright (c) 2016-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file protover.c
6 * \brief Versioning information for different pieces of the Tor protocol.
7 *
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.
17 *
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.
22 **/
23
24#define PROTOVER_PRIVATE
25
26#include "core/or/or.h"
27#include "core/or/protover.h"
28#include "core/or/versions.h"
29#include "lib/tls/tortls.h"
30
32static int protocol_list_contains(const smartlist_t *protos,
33 protocol_type_t pr, uint32_t ver);
34static const proto_entry_t *find_entry_by_name(const smartlist_t *protos,
35 const char *name);
36
37/** Mapping between protocol type string and protocol type. */
38/// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
39static const struct {
40 protocol_type_t protover_type;
41 const char *name;
42/* If you add a new protocol here, you probably also want to add
43 * parsing for it in summarize_protover_flags(), so that it has a
44 * summary flag in routerstatus_t */
45} PROTOCOL_NAMES[] = {
46 { PRT_LINK, "Link" },
47 { PRT_LINKAUTH, "LinkAuth" },
48 { PRT_RELAY, "Relay" },
49 { PRT_DIRCACHE, "DirCache" },
50 { PRT_HSDIR, "HSDir" },
51 { PRT_HSINTRO, "HSIntro" },
52 { PRT_HSREND, "HSRend" },
53 { PRT_DESC, "Desc" },
54 { PRT_MICRODESC, "Microdesc"},
55 { PRT_PADDING, "Padding"},
56 { PRT_CONS, "Cons" },
57 { PRT_FLOWCTRL, "FlowCtrl"},
58 { PRT_CONFLUX, "Conflux"},
59};
60
61#define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
62
63/* Maximum allowed length of any single subprotocol name. */
64// C_RUST_COUPLED: src/rust/protover/protover.rs
65// `MAX_PROTOCOL_NAME_LENGTH`
66static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100;
67
68/**
69 * Given a protocol_type_t, return the corresponding string used in
70 * descriptors.
71 */
72STATIC const char *
74{
75 unsigned i;
76 for (i=0; i < N_PROTOCOL_NAMES; ++i) {
77 if (PROTOCOL_NAMES[i].protover_type == pr)
78 return PROTOCOL_NAMES[i].name;
79 }
80 /* LCOV_EXCL_START */
81 tor_assert_nonfatal_unreached_once();
82 return "UNKNOWN";
83 /* LCOV_EXCL_STOP */
84}
85
86/**
87 * Release all space held by a single proto_entry_t structure
88 */
89STATIC void
90proto_entry_free_(proto_entry_t *entry)
91{
92 if (!entry)
93 return;
94 tor_free(entry->name);
95 tor_free(entry);
96}
97
98/** The largest possible protocol version. */
99#define MAX_PROTOCOL_VERSION (63)
100
101/**
102 * Given a string <b>s</b> and optional end-of-string pointer
103 * <b>end_of_range</b>, parse the protocol range and store it in
104 * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
105 * U-U, where U is an unsigned integer between 0 and 63 inclusive.
106 */
107static int
108parse_version_range(const char *s, const char *end_of_range,
109 uint32_t *low_out, uint32_t *high_out)
110{
111 uint32_t low, high;
112 char *next = NULL;
113 int ok;
114
115 tor_assert(high_out);
116 tor_assert(low_out);
117
118 if (BUG(!end_of_range))
119 end_of_range = s + strlen(s); // LCOV_EXCL_LINE
120
121 /* A range must start with a digit. */
122 if (!TOR_ISDIGIT(*s)) {
123 goto error;
124 }
125
126 /* Note that this wouldn't be safe if we didn't know that eventually,
127 * we'd hit a NUL */
128 low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
129 if (!ok)
130 goto error;
131 if (next > end_of_range)
132 goto error;
133 if (next == end_of_range) {
134 high = low;
135 goto done;
136 }
137
138 if (*next != '-')
139 goto error;
140 s = next+1;
141
142 /* ibid */
143 if (!TOR_ISDIGIT(*s)) {
144 goto error;
145 }
146 high = (uint32_t) tor_parse_ulong(s, 10, 0,
147 MAX_PROTOCOL_VERSION, &ok, &next);
148 if (!ok)
149 goto error;
150 if (next != end_of_range)
151 goto error;
152
153 if (low > high)
154 goto error;
155
156 done:
157 *high_out = high;
158 *low_out = low;
159 return 0;
160
161 error:
162 return -1;
163}
164
165static int
166is_valid_keyword(const char *s, size_t n)
167{
168 for (size_t i = 0; i < n; i++) {
169 if (!TOR_ISALNUM(s[i]) && s[i] != '-')
170 return 0;
171 }
172 return 1;
173}
174
175/** The x'th bit in a bitmask. */
176#define BIT(x) (UINT64_C(1)<<(x))
177
178/**
179 * Return a bitmask so that bits 'low' through 'high' inclusive are set,
180 * and all other bits are cleared.
181 **/
182static uint64_t
183bitmask_for_range(uint32_t low, uint32_t high)
184{
185 uint64_t mask = ~(uint64_t)0;
186 mask <<= 63 - high;
187 mask >>= 63 - high + low;
188 mask <<= low;
189 return mask;
190}
191
192/** Parse a single protocol entry from <b>s</b> up to an optional
193 * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
194 * on error.
195 *
196 * A protocol entry has a keyword, an = sign, and zero or more ranges. */
197static proto_entry_t *
198parse_single_entry(const char *s, const char *end_of_entry)
199{
200 proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
201 const char *equals;
202
203 if (BUG (!end_of_entry))
204 end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
205
206 /* There must be an =. */
207 equals = memchr(s, '=', end_of_entry - s);
208 if (!equals)
209 goto error;
210
211 /* The name must be nonempty */
212 if (equals == s)
213 goto error;
214
215 /* The name must not be longer than MAX_PROTOCOL_NAME_LENGTH. */
216 if (equals - s > (int)MAX_PROTOCOL_NAME_LENGTH) {
217 log_warn(LD_NET, "When parsing a protocol entry, I got a very large "
218 "protocol name. This is possibly an attack or a bug, unless "
219 "the Tor network truly supports protocol names larger than "
220 "%ud characters. The offending string was: %s",
221 MAX_PROTOCOL_NAME_LENGTH, escaped(out->name));
222 goto error;
223 }
224
225 /* The name must contain only alphanumeric characters and hyphens. */
226 if (!is_valid_keyword(s, equals-s))
227 goto error;
228
229 out->name = tor_strndup(s, equals-s);
230
231 tor_assert(equals < end_of_entry);
232
233 s = equals + 1;
234 while (s < end_of_entry) {
235 const char *comma = memchr(s, ',', end_of_entry-s);
236 if (! comma)
237 comma = end_of_entry;
238
239 uint32_t low=0, high=0;
240 if (parse_version_range(s, comma, &low, &high) < 0) {
241 goto error;
242 }
243
244 out->bitmask |= bitmask_for_range(low,high);
245
246 s = comma;
247 // Skip the comma separator between ranges. Don't ignore a trailing comma.
248 if (s < (end_of_entry - 1))
249 ++s;
250 }
251
252 return out;
253
254 error:
255 proto_entry_free(out);
256 return NULL;
257}
258
259/**
260 * Parse the protocol list from <b>s</b> and return it as a smartlist of
261 * proto_entry_t
262 */
265{
266 smartlist_t *entries = smartlist_new();
267
268 while (*s) {
269 /* Find the next space or the NUL. */
270 const char *end_of_entry = strchr(s, ' ');
271 proto_entry_t *entry;
272 if (!end_of_entry)
273 end_of_entry = s + strlen(s);
274
275 entry = parse_single_entry(s, end_of_entry);
276
277 if (! entry)
278 goto error;
279
280 smartlist_add(entries, entry);
281
282 s = end_of_entry;
283 while (*s == ' ')
284 ++s;
285 }
286
287 return entries;
288
289 error:
290 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
291 smartlist_free(entries);
292 return NULL;
293}
294
295/**
296 * Return true if the unparsed protover list in <b>s</b> contains a
297 * parsing error, such as extra commas, a bad number, or an over-long
298 * name.
299 */
300bool
302{
304 if (!list)
305 return true; /* yes, has a dangerous name */
306 SMARTLIST_FOREACH(list, proto_entry_t *, ent, proto_entry_free(ent));
307 smartlist_free(list);
308 return false; /* no, looks fine */
309}
310
311/**
312 * Given a protocol type and version number, return true iff we know
313 * how to speak that protocol.
314 */
315int
317{
319 return protocol_list_contains(ours, pr, ver);
320}
321
322/**
323 * Return true iff "list" encodes a protocol list that includes support for
324 * the indicated protocol and version.
325 *
326 * If the protocol list is unparseable, treat it as if it defines no
327 * protocols, and return 0.
328 */
329int
331 uint32_t version)
332{
333 /* NOTE: This is a pretty inefficient implementation. If it ever shows
334 * up in profiles, we should memoize it.
335 */
336 smartlist_t *protocols = parse_protocol_list(list);
337 if (!protocols) {
338 return 0;
339 }
340 int contains = protocol_list_contains(protocols, tp, version);
341
342 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
343 smartlist_free(protocols);
344 return contains;
345}
346
347/**
348 * Return true iff "list" encodes a protocol list that includes support for
349 * the indicated protocol and version, or some later version.
350 *
351 * If the protocol list is unparseable, treat it as if it defines no
352 * protocols, and return 0.
353 */
354int
357 uint32_t version)
358{
359 /* NOTE: This is a pretty inefficient implementation. If it ever shows
360 * up in profiles, we should memoize it.
361 */
362 smartlist_t *protocols = parse_protocol_list(list);
363 if (!protocols) {
364 return 0;
365 }
366 const char *pr_name = protocol_type_to_str(tp);
367
368 int contains = 0;
369 const uint64_t mask = bitmask_for_range(version, 63);
370
371 SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
372 if (strcasecmp(proto->name, pr_name))
373 continue;
374 if (0 != (proto->bitmask & mask)) {
375 contains = 1;
376 goto found;
377 }
378 } SMARTLIST_FOREACH_END(proto);
379
380 found:
381 SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
382 smartlist_free(protocols);
383 return contains;
384}
385
386/*
387 * XXX START OF HAZARDOUS ZONE XXX
388 */
389/* All protocol version that this relay version supports. */
390#define PR_CONFLUX_V "1"
391#define PR_CONS_V "1-2"
392#define PR_DESC_V "1-2"
393#define PR_DIRCACHE_V "2"
394#define PR_FLOWCTRL_V "1-2"
395#define PR_HSDIR_V "2"
396#define PR_HSINTRO_V "4-5"
397#define PR_HSREND_V "1-2"
398#define PR_LINK_V "1-5"
399#ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
400#define PR_LINKAUTH_V "1,3"
401#else
402#define PR_LINKAUTH_V "3"
403#endif
404#define PR_MICRODESC_V "1-2"
405#define PR_PADDING_V "2"
406#define PR_RELAY_V "1-4"
407
408/** Return the string containing the supported version for the given protocol
409 * type. */
410const char *
412{
413 switch (type) {
414 case PRT_CONFLUX: return PR_CONFLUX_V;
415 case PRT_CONS: return PR_CONS_V;
416 case PRT_DESC: return PR_DESC_V;
417 case PRT_DIRCACHE: return PR_DIRCACHE_V;
418 case PRT_FLOWCTRL: return PR_FLOWCTRL_V;
419 case PRT_HSDIR: return PR_HSDIR_V;
420 case PRT_HSINTRO: return PR_HSINTRO_V;
421 case PRT_HSREND: return PR_HSREND_V;
422 case PRT_LINK: return PR_LINK_V;
423 case PRT_LINKAUTH: return PR_LINKAUTH_V;
424 case PRT_MICRODESC: return PR_MICRODESC_V;
425 case PRT_PADDING: return PR_PADDING_V;
426 case PRT_RELAY: return PR_RELAY_V;
427 default:
428 tor_assert_unreached();
429 }
430}
431
432/** Return the canonical string containing the list of protocols
433 * that we support.
434 **/
435/// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
436const char *
438{
439 /* WARNING!
440 *
441 * Remember to edit the SUPPORTED_PROTOCOLS list in protover.rs if you
442 * are editing this list.
443 */
444
445 /*
446 * XXX: WARNING!
447 *
448 * Be EXTREMELY CAREFUL when *removing* versions from this list. If you
449 * remove an entry while it still appears as "recommended" in the consensus,
450 * you'll cause all the instances without it to warn.
451 *
452 * If you remove an entry while it still appears as "required" in the
453 * consensus, you'll cause all the instances without it to refuse to connect
454 * to the network, and shut down.
455 *
456 * If you need to remove a version from this list, you need to make sure that
457 * it is not listed in the _current consensuses_: just removing it from the
458 * required list below is NOT ENOUGH. You need to remove it from the
459 * required list, and THEN let the authorities upgrade and vote on new
460 * consensuses without it. Only once those consensuses are out is it safe to
461 * remove from this list.
462 *
463 * One concrete example of a very dangerous race that could occur:
464 *
465 * Suppose that the client supports protocols "HsDir=1-2" and the consensus
466 * requires protocols "HsDir=1-2. If the client supported protocol list is
467 * then changed to "HSDir=2", while the consensus stills lists "HSDir=1-2",
468 * then these clients, even very recent ones, will shut down because they
469 * don't support "HSDir=1".
470 *
471 * And so, changes need to be done in strict sequence as described above.
472 *
473 * XXX: WARNING!
474 */
475
476 return
477 "Conflux=" PR_CONFLUX_V " "
478 "Cons=" PR_CONS_V " "
479 "Desc=" PR_DESC_V " "
480 "DirCache=" PR_DIRCACHE_V " "
481 "FlowCtrl=" PR_FLOWCTRL_V " "
482 "HSDir=" PR_HSDIR_V " "
483 "HSIntro=" PR_HSINTRO_V " "
484 "HSRend=" PR_HSREND_V " "
485 "Link=" PR_LINK_V " "
486 "LinkAuth=" PR_LINKAUTH_V " "
487 "Microdesc=" PR_MICRODESC_V " "
488 "Padding=" PR_PADDING_V " "
489 "Relay=" PR_RELAY_V;
490}
491
492/*
493 * XXX: WARNING!
494 *
495 * The recommended and required values are hardwired, to avoid disaster. Voting
496 * on the wrong subprotocols here has the potential to take down the network.
497 *
498 * In particular, you need to be EXTREMELY CAREFUL before adding new versions
499 * to the required protocol list. Doing so will cause every relay or client
500 * that doesn't support those versions to refuse to connect to the network and
501 * shut down.
502 *
503 * Note that this applies to versions, not just protocols! If you say that
504 * Foobar=8-9 is required, and the client only has Foobar=9, it will shut down.
505 *
506 * It is okay to do this only for SUPER OLD relays that are not supported on
507 * the network anyway. For clients, we really shouldn't kick them off the
508 * network unless their presence is causing serious active harm.
509 *
510 * The following required and recommended lists MUST be changed BEFORE the
511 * supported list above is changed, so that these lists appear in the
512 * consensus BEFORE clients need them.
513 *
514 * Please, see the warning in protocol_get_supported_versions().
515 *
516 * XXX: WARNING!
517 */
518
519/** Return the recommended client protocols list that directory authorities
520 * put in the consensus. */
521const char *
523{
524 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
525 "Link=4-5 Microdesc=2 Relay=2";
526}
527
528/** Return the recommended relay protocols list that directory authorities
529 * put in the consensus. */
530const char *
532{
533 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
534 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2";
535}
536
537/** Return the required client protocols list that directory authorities
538 * put in the consensus. */
539const char *
541{
542 return "Cons=2 Desc=2 Link=4 Microdesc=2 Relay=2";
543}
544
545/** Return the required relay protocols list that directory authorities
546 * put in the consensus. */
547const char *
549{
550 return "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 "
551 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2";
552}
553
554/*
555 * XXX END OF HAZARDOUS ZONE XXX
556 */
557
558/** The protocols from protover_get_supported_protocols(), as parsed into a
559 * list of proto_entry_t values. Access this via
560 * get_supported_protocol_list. */
562
563/** Return a pointer to a smartlist of proto_entry_t for the protocols
564 * we support. */
565static const smartlist_t *
567{
568 if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
571 }
573}
574
575/** Return the number of trailing zeros in x. Undefined if x is 0. */
576static int
577trailing_zeros(uint64_t x)
578{
579#ifdef __GNUC__
580 return __builtin_ctzll((unsigned long long)x);
581#else
582 int i;
583 for (i = 0; i <= 64; ++i) {
584 if (x&1)
585 return i;
586 x>>=1;
587 }
588 return i;
589#endif /* defined(__GNUC__) */
590}
591
592/**
593 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
594 * as one or more newly allocated strings.
595 */
596static void
597proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
598{
599 smartlist_add_asprintf(chunks, "%s=", entry->name);
600
601 uint64_t mask = entry->bitmask;
602 int shift = 0; // how much have we shifted by so far?
603 bool first = true;
604 while (mask) {
605 const char *comma = first ? "" : ",";
606 if (first) {
607 first = false;
608 }
609 int zeros = trailing_zeros(mask);
610 mask >>= zeros;
611 shift += zeros;
612 int ones = !mask ? 64 : trailing_zeros(~mask);
613 if (ones == 1) {
614 smartlist_add_asprintf(chunks, "%s%d", comma, shift);
615 } else {
616 smartlist_add_asprintf(chunks, "%s%d-%d", comma,
617 shift, shift + ones - 1);
618 }
619 if (ones == 64) {
620 break; // avoid undefined behavior; can't shift by 64.
621 }
622 mask >>= ones;
623 shift += ones;
624 }
625}
626
627/** Given a list of space-separated proto_entry_t items,
628 * encode it into a newly allocated space-separated string. */
629STATIC char *
631{
632 const char *separator = "";
633 smartlist_t *chunks = smartlist_new();
634 SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
635 smartlist_add_strdup(chunks, separator);
636
637 proto_entry_encode_into(chunks, ent);
638
639 separator = " ";
640 } SMARTLIST_FOREACH_END(ent);
641
642 char *result = smartlist_join_strings(chunks, "", 0, NULL);
643
644 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
645 smartlist_free(chunks);
646
647 return result;
648}
649
650/**
651 * Protocol voting implementation.
652 *
653 * Given a list of strings describing protocol versions, return a newly
654 * allocated string encoding all of the protocols that are listed by at
655 * least <b>threshold</b> of the inputs.
656 *
657 * The string is minimal and sorted according to the rules of
658 * contract_protocol_list above.
659 */
660char *
661protover_compute_vote(const smartlist_t *list_of_proto_strings,
662 int threshold)
663{
664 // we use u8 counters below.
665 tor_assert(smartlist_len(list_of_proto_strings) < 256);
666
667 if (smartlist_len(list_of_proto_strings) == 0) {
668 return tor_strdup("");
669 }
670
671 smartlist_t *parsed = smartlist_new(); // smartlist of smartlist of entries
672 smartlist_t *proto_names = smartlist_new(); // smartlist of strings
673 smartlist_t *result = smartlist_new(); // smartlist of entries
674
675 // First, parse the inputs, and accumulate a list of protocol names.
676 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
677 smartlist_t *unexpanded = parse_protocol_list(vote);
678 if (! unexpanded) {
679 log_warn(LD_NET, "I failed with parsing a protocol list from "
680 "an authority. The offending string was: %s",
681 escaped(vote));
682 continue;
683 }
684 SMARTLIST_FOREACH_BEGIN(unexpanded, const proto_entry_t *, ent) {
685 if (!smartlist_contains_string(proto_names,ent->name)) {
686 smartlist_add(proto_names, ent->name);
687 }
688 } SMARTLIST_FOREACH_END(ent);
689 smartlist_add(parsed, unexpanded);
690 } SMARTLIST_FOREACH_END(vote);
691
692 // Sort the list of names.
693 smartlist_sort_strings(proto_names);
694
695 // For each named protocol, compute the consensus.
696 //
697 // This is not super-efficient, but it's not critical path.
698 SMARTLIST_FOREACH_BEGIN(proto_names, const char *, name) {
699 uint8_t counts[64];
700 memset(counts, 0, sizeof(counts));
701 // Count how many votes we got for each bit.
702 SMARTLIST_FOREACH_BEGIN(parsed, const smartlist_t *, vote) {
703 const proto_entry_t *ent = find_entry_by_name(vote, name);
704 if (! ent)
705 continue;
706
707 for (int i = 0; i < 64; ++i) {
708 if ((ent->bitmask & BIT(i)) != 0) {
709 ++ counts[i];
710 }
711 }
712 } SMARTLIST_FOREACH_END(vote);
713
714 uint64_t result_bitmask = 0;
715 for (int i = 0; i < 64; ++i) {
716 if (counts[i] >= threshold) {
717 result_bitmask |= BIT(i);
718 }
719 }
720 if (result_bitmask != 0) {
721 proto_entry_t *newent = tor_malloc_zero(sizeof(proto_entry_t));
722 newent->name = tor_strdup(name);
723 newent->bitmask = result_bitmask;
724 smartlist_add(result, newent);
725 }
726 } SMARTLIST_FOREACH_END(name);
727
728 char *consensus = encode_protocol_list(result);
729
730 SMARTLIST_FOREACH(result, proto_entry_t *, ent, proto_entry_free(ent));
731 smartlist_free(result);
732 smartlist_free(proto_names); // no need to free members; they are aliases.
734 SMARTLIST_FOREACH(v, proto_entry_t *, ent, proto_entry_free(ent));
735 smartlist_free(v);
736 } SMARTLIST_FOREACH_END(v);
737 smartlist_free(parsed);
738
739 return consensus;
740}
741
742/** Return true if every protocol version described in the string <b>s</b> is
743 * one that we support, and false otherwise. If <b>missing_out</b> is
744 * provided, set it to the list of protocols we do not support.
745 *
746 * If the protocol version string is unparseable, treat it as if it defines no
747 * protocols, and return 1.
748 **/
749int
750protover_all_supported(const char *s, char **missing_out)
751{
752 if (!s) {
753 return 1;
754 }
755
756 smartlist_t *entries = parse_protocol_list(s);
757 if (BUG(entries == NULL)) {
758 log_warn(LD_NET, "Received an unparseable protocol list %s"
759 " from the consensus", escaped(s));
760 return 1;
761 }
762 const smartlist_t *supported = get_supported_protocol_list();
763 smartlist_t *missing = smartlist_new();
764
765 SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
766 const proto_entry_t *mine = find_entry_by_name(supported, ent->name);
767 if (mine == NULL) {
768 if (ent->bitmask != 0) {
769 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
770 m->name = tor_strdup(ent->name);
771 m->bitmask = ent->bitmask;
772 smartlist_add(missing, m);
773 }
774 continue;
775 }
776
777 uint64_t missing_mask = ent->bitmask & ~mine->bitmask;
778 if (missing_mask != 0) {
779 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
780 m->name = tor_strdup(ent->name);
781 m->bitmask = missing_mask;
782 smartlist_add(missing, m);
783 }
784 } SMARTLIST_FOREACH_END(ent);
785
786 const int all_supported = (smartlist_len(missing) == 0);
787 if (!all_supported && missing_out) {
788 *missing_out = encode_protocol_list(missing);
789 }
790
791 SMARTLIST_FOREACH(missing, proto_entry_t *, ent, proto_entry_free(ent));
792 smartlist_free(missing);
793
794 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
795 smartlist_free(entries);
796
797 return all_supported;
798}
799
800/** Helper: return the member of 'protos' whose name is
801 * 'name', or NULL if there is no such member. */
802static const proto_entry_t *
803find_entry_by_name(const smartlist_t *protos, const char *name)
804{
805 if (!protos) {
806 return NULL;
807 }
808 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
809 if (!strcmp(ent->name, name)) {
810 return ent;
811 }
812 } SMARTLIST_FOREACH_END(ent);
813
814 return NULL;
815}
816
817/** Helper: Given a list of proto_entry_t, return true iff
818 * <b>pr</b>=<b>ver</b> is included in that list. */
819static int
821 protocol_type_t pr, uint32_t ver)
822{
823 if (BUG(protos == NULL)) {
824 return 0; // LCOV_EXCL_LINE
825 }
826 const char *pr_name = protocol_type_to_str(pr);
827 if (BUG(pr_name == NULL)) {
828 return 0; // LCOV_EXCL_LINE
829 }
830 if (ver > MAX_PROTOCOL_VERSION) {
831 return 0;
832 }
833
834 const proto_entry_t *ent = find_entry_by_name(protos, pr_name);
835 if (ent) {
836 return (ent->bitmask & BIT(ver)) != 0;
837 }
838 return 0;
839}
840
841/** Return a string describing the protocols supported by tor version
842 * <b>version</b>, or an empty string if we cannot tell.
843 *
844 * Note that this is only used to infer protocols for Tor versions that
845 * can't declare their own.
846 **/
847/// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
848const char *
850{
851 if (version == NULL) {
852 /* No known version; guess the oldest series that is still supported. */
853 version = "0.2.5.15";
854 }
855
856 if (tor_version_as_new_as(version,
858 return "";
859 } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
860 /* 0.2.9.1-alpha HSRend=2 */
861 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
862 "Link=1-4 LinkAuth=1 "
863 "Microdesc=1-2 Relay=1-2";
864 } else if (tor_version_as_new_as(version, "0.2.7.5")) {
865 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
866 * ed25519 support. We'll call them present only in "stable" 027,
867 * though. */
868 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
869 "Link=1-4 LinkAuth=1 "
870 "Microdesc=1-2 Relay=1-2";
871 } else if (tor_version_as_new_as(version, "0.2.4.19")) {
872 /* No currently supported Tor server versions are older than this, or
873 * lack these protocols. */
874 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
875 "Link=1-4 LinkAuth=1 "
876 "Microdesc=1 Relay=1-2";
877 } else {
878 /* Cannot infer protocols. */
879 return "";
880 }
881}
882
883/**
884 * Release all storage held by static fields in protover.c
885 */
886void
888{
891 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
892 smartlist_free(entries);
894 }
895}
const char * name
Definition: config.c:2462
static conn_counts_t counts
Definition: connstats.c:72
const char * escaped(const char *s)
Definition: escape.c:126
#define LD_NET
Definition: log.h:66
#define tor_free(p)
Definition: malloc.h:56
Master header file for Tor-specific functionality.
unsigned long tor_parse_ulong(const char *s, int base, unsigned long min, unsigned long max, int *ok, char **next)
Definition: parse_int.c:78
static proto_entry_t * parse_single_entry(const char *s, const char *end_of_entry)
Definition: protover.c:198
STATIC char * encode_protocol_list(const smartlist_t *sl)
Definition: protover.c:630
static const proto_entry_t * find_entry_by_name(const smartlist_t *protos, const char *name)
Definition: protover.c:803
static uint64_t bitmask_for_range(uint32_t low, uint32_t high)
Definition: protover.c:183
bool protover_list_is_invalid(const char *s)
Definition: protover.c:301
STATIC void proto_entry_free_(proto_entry_t *entry)
Definition: protover.c:90
const char * protover_get_recommended_relay_protocols(void)
Definition: protover.c:531
const char * protover_get_required_relay_protocols(void)
Definition: protover.c:548
static smartlist_t * supported_protocol_list
Definition: protover.c:561
static const smartlist_t * get_supported_protocol_list(void)
Definition: protover.c:566
#define MAX_PROTOCOL_VERSION
Definition: protover.c:99
const char * protover_get_required_client_protocols(void)
Definition: protover.c:540
void protover_free_all(void)
Definition: protover.c:887
static const struct @12 PROTOCOL_NAMES[]
C_RUST_COUPLED: src/rust/protover/protover.rs PROTOCOL_NAMES
static void proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
Definition: protover.c:597
STATIC const char * protocol_type_to_str(protocol_type_t pr)
Definition: protover.c:73
int protover_is_supported_here(protocol_type_t pr, uint32_t ver)
Definition: protover.c:316
const char * protover_get_recommended_client_protocols(void)
Definition: protover.c:522
static int protocol_list_contains(const smartlist_t *protos, protocol_type_t pr, uint32_t ver)
Definition: protover.c:820
int protocol_list_supports_protocol(const char *list, protocol_type_t tp, uint32_t version)
Definition: protover.c:330
int protocol_list_supports_protocol_or_later(const char *list, protocol_type_t tp, uint32_t version)
Definition: protover.c:355
char * protover_compute_vote(const smartlist_t *list_of_proto_strings, int threshold)
Definition: protover.c:661
static int parse_version_range(const char *s, const char *end_of_range, uint32_t *low_out, uint32_t *high_out)
Definition: protover.c:108
const char * protover_get_supported_protocols(void)
C_RUST_COUPLED: src/rust/protover/protover.rs SUPPORTED_PROTOCOLS
Definition: protover.c:437
#define BIT(x)
Definition: protover.c:176
STATIC smartlist_t * parse_protocol_list(const char *s)
Definition: protover.c:264
const char * protover_compute_for_old_tor(const char *version)
C_RUST_COUPLED: src/rust/protover/protover.rs compute_for_old_tor
Definition: protover.c:849
static int trailing_zeros(uint64_t x)
Definition: protover.c:577
const char * protover_get_supported(const protocol_type_t type)
Definition: protover.c:411
int protover_all_supported(const char *s, char **missing_out)
Definition: protover.c:750
Headers and type declarations for protover.c.
#define FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS
Definition: protover.h:23
protocol_type_t
Definition: protover.h:64
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
void smartlist_sort_strings(smartlist_t *sl)
Definition: smartlist.c:549
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define STATIC
Definition: testsupport.h:32
Headers for tortls.c.
#define tor_assert(expr)
Definition: util_bug.h:103
int tor_version_as_new_as(const char *platform, const char *cutoff)
Definition: versions.c:171
Header file for versions.c.