Tor 0.4.9.13
Loading...
Searching...
No Matches
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 version of tor supports. */
390#define PR_CONFLUX_V "1"
391#define PR_CONS_V "1-2"
392#define PR_DESC_V "1-4"
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 "3-5"
399#define PR_LINKAUTH_V "3"
400#define PR_MICRODESC_V "1-3"
401#define PR_PADDING_V "2"
402#define PR_RELAY_V "2-6"
403
404/** Return the string containing the supported version for the given protocol
405 * type. */
406const char *
408{
409 switch (type) {
410 case PRT_CONFLUX: return PR_CONFLUX_V;
411 case PRT_CONS: return PR_CONS_V;
412 case PRT_DESC: return PR_DESC_V;
413 case PRT_DIRCACHE: return PR_DIRCACHE_V;
414 case PRT_FLOWCTRL: return PR_FLOWCTRL_V;
415 case PRT_HSDIR: return PR_HSDIR_V;
416 case PRT_HSINTRO: return PR_HSINTRO_V;
417 case PRT_HSREND: return PR_HSREND_V;
418 case PRT_LINK: return PR_LINK_V;
419 case PRT_LINKAUTH: return PR_LINKAUTH_V;
420 case PRT_MICRODESC: return PR_MICRODESC_V;
421 case PRT_PADDING: return PR_PADDING_V;
422 case PRT_RELAY: return PR_RELAY_V;
423 default:
424 tor_assert_unreached();
425 }
426}
427
428/** Return the canonical string containing the list of protocols
429 * that we support.
430 **/
431/// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
432const char *
434{
435 /* WARNING!
436 *
437 * Remember to edit the SUPPORTED_PROTOCOLS list in protover.rs if you
438 * are editing this list.
439 */
440
441 /*
442 * XXX: WARNING!
443 *
444 * Be EXTREMELY CAREFUL when *removing* versions from this list. If you
445 * remove an entry while it still appears as "recommended" in the consensus,
446 * you'll cause all the instances without it to warn.
447 *
448 * If you remove an entry while it still appears as "required" in the
449 * consensus, you'll cause all the instances without it to refuse to connect
450 * to the network, and shut down.
451 *
452 * If you need to remove a version from this list, you need to make sure that
453 * it is not listed in the _current consensuses_: just removing it from the
454 * required list below is NOT ENOUGH. You need to remove it from the
455 * required list, and THEN let the authorities upgrade and vote on new
456 * consensuses without it. Only once those consensuses are out is it safe to
457 * remove from this list.
458 *
459 * One concrete example of a very dangerous race that could occur:
460 *
461 * Suppose that the client supports protocols "HsDir=1-2" and the consensus
462 * requires protocols "HsDir=1-2. If the client supported protocol list is
463 * then changed to "HSDir=2", while the consensus stills lists "HSDir=1-2",
464 * then these clients, even very recent ones, will shut down because they
465 * don't support "HSDir=1".
466 *
467 * And so, changes need to be done in strict sequence as described above.
468 *
469 * XXX: WARNING!
470 */
471
472 return
473 "Conflux=" PR_CONFLUX_V " "
474 "Cons=" PR_CONS_V " "
475 "Desc=" PR_DESC_V " "
476 "DirCache=" PR_DIRCACHE_V " "
477 "FlowCtrl=" PR_FLOWCTRL_V " "
478 "HSDir=" PR_HSDIR_V " "
479 "HSIntro=" PR_HSINTRO_V " "
480 "HSRend=" PR_HSREND_V " "
481 "Link=" PR_LINK_V " "
482 "LinkAuth=" PR_LINKAUTH_V " "
483 "Microdesc=" PR_MICRODESC_V " "
484 "Padding=" PR_PADDING_V " "
485 "Relay=" PR_RELAY_V;
486}
487
488/*
489 * XXX: WARNING!
490 *
491 * The recommended and required values are hardwired, to avoid disaster. Voting
492 * on the wrong subprotocols here has the potential to take down the network.
493 *
494 * In particular, you need to be EXTREMELY CAREFUL before adding new versions
495 * to the required protocol list. Doing so will cause every relay or client
496 * that doesn't support those versions to refuse to connect to the network and
497 * shut down.
498 *
499 * Note that this applies to versions, not just protocols! If you say that
500 * Foobar=8-9 is required, and the client only has Foobar=9, it will shut down.
501 *
502 * It is okay to do this only for SUPER OLD relays that are not supported on
503 * the network anyway. For clients, we really shouldn't kick them off the
504 * network unless their presence is causing serious active harm.
505 *
506 * The following required and recommended lists MUST be changed BEFORE the
507 * supported list above is changed, so that these lists appear in the
508 * consensus BEFORE clients need them.
509 *
510 * Please, see the warning in protocol_get_supported_versions().
511 *
512 * XXX: WARNING!
513 */
514
515/** Return the recommended client protocols list that directory authorities
516 * put in the consensus. */
517const char *
519{
520 return
521 "Cons=2 "
522 "Desc=2-4 "
523 "DirCache=2 "
524 "FlowCtrl=1-2 "
525 "HSDir=2 "
526 "HSIntro=4 "
527 "HSRend=2 "
528 "Link=4-5 "
529 "Microdesc=2-3 "
530 "Relay=2-6";
531}
532
533/** Return the recommended relay protocols list that directory authorities
534 * put in the consensus. */
535const char *
537{
538 return
539 "Cons=2 "
540 "Desc=2-4 "
541 "DirCache=2 "
542 "FlowCtrl=1-2 "
543 "HSDir=2 "
544 "HSIntro=4-5 "
545 "HSRend=2 "
546 "Link=4-5 "
547 "LinkAuth=3 "
548 "Microdesc=2-3 "
549 "Relay=2-6";
550}
551
552/** Return the required client protocols list that directory authorities
553 * put in the consensus. */
554const char *
556{
557 return
558 "Cons=2 "
559 "Desc=2 "
560 "FlowCtrl=1 "
561 "Link=4 "
562 "Microdesc=2 "
563 "Relay=2";
564}
565
566/** Return the required relay protocols list that directory authorities
567 * put in the consensus. */
568const char *
570{
571 return
572 "Cons=2 "
573 "Desc=2 "
574 "DirCache=2 "
575 "FlowCtrl=1-2 "
576 "HSDir=2 "
577 "HSIntro=4-5 "
578 "HSRend=2 "
579 "Link=4-5 "
580 "LinkAuth=3 "
581 "Microdesc=2 "
582 "Relay=2-4";
583}
584
585/*
586 * XXX END OF HAZARDOUS ZONE XXX
587 */
588
589/** The protocols from protover_get_supported_protocols(), as parsed into a
590 * list of proto_entry_t values. Access this via
591 * get_supported_protocol_list. */
593
594/** Return a pointer to a smartlist of proto_entry_t for the protocols
595 * we support. */
596static const smartlist_t *
605
606/** Return the number of trailing zeros in x. Undefined if x is 0. */
607static int
608trailing_zeros(uint64_t x)
609{
610#ifdef __GNUC__
611 return __builtin_ctzll((unsigned long long)x);
612#else
613 int i;
614 for (i = 0; i <= 64; ++i) {
615 if (x&1)
616 return i;
617 x>>=1;
618 }
619 return i;
620#endif /* defined(__GNUC__) */
621}
622
623/**
624 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
625 * as one or more newly allocated strings.
626 */
627static void
628proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
629{
630 smartlist_add_asprintf(chunks, "%s=", entry->name);
631
632 uint64_t mask = entry->bitmask;
633 int shift = 0; // how much have we shifted by so far?
634 bool first = true;
635 while (mask) {
636 const char *comma = first ? "" : ",";
637 if (first) {
638 first = false;
639 }
640 int zeros = trailing_zeros(mask);
641 mask >>= zeros;
642 shift += zeros;
643 int ones = !mask ? 64 : trailing_zeros(~mask);
644 if (ones == 1) {
645 smartlist_add_asprintf(chunks, "%s%d", comma, shift);
646 } else {
647 smartlist_add_asprintf(chunks, "%s%d-%d", comma,
648 shift, shift + ones - 1);
649 }
650 if (ones == 64) {
651 break; // avoid undefined behavior; can't shift by 64.
652 }
653 mask >>= ones;
654 shift += ones;
655 }
656}
657
658/** Given a list of space-separated proto_entry_t items,
659 * encode it into a newly allocated space-separated string. */
660STATIC char *
662{
663 const char *separator = "";
664 smartlist_t *chunks = smartlist_new();
665 SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
666 smartlist_add_strdup(chunks, separator);
667
668 proto_entry_encode_into(chunks, ent);
669
670 separator = " ";
671 } SMARTLIST_FOREACH_END(ent);
672
673 char *result = smartlist_join_strings(chunks, "", 0, NULL);
674
675 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
676 smartlist_free(chunks);
677
678 return result;
679}
680
681/**
682 * Protocol voting implementation.
683 *
684 * Given a list of strings describing protocol versions, return a newly
685 * allocated string encoding all of the protocols that are listed by at
686 * least <b>threshold</b> of the inputs.
687 *
688 * The string is minimal and sorted according to the rules of
689 * contract_protocol_list above.
690 */
691char *
692protover_compute_vote(const smartlist_t *list_of_proto_strings,
693 int threshold)
694{
695 // we use u8 counters below.
696 tor_assert(smartlist_len(list_of_proto_strings) < 256);
697
698 if (smartlist_len(list_of_proto_strings) == 0) {
699 return tor_strdup("");
700 }
701
702 smartlist_t *parsed = smartlist_new(); // smartlist of smartlist of entries
703 smartlist_t *proto_names = smartlist_new(); // smartlist of strings
704 smartlist_t *result = smartlist_new(); // smartlist of entries
705
706 // First, parse the inputs, and accumulate a list of protocol names.
707 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
708 smartlist_t *unexpanded = parse_protocol_list(vote);
709 if (! unexpanded) {
710 log_warn(LD_NET, "I failed with parsing a protocol list from "
711 "an authority. The offending string was: %s",
712 escaped(vote));
713 continue;
714 }
715 SMARTLIST_FOREACH_BEGIN(unexpanded, const proto_entry_t *, ent) {
716 if (!smartlist_contains_string(proto_names,ent->name)) {
717 smartlist_add(proto_names, ent->name);
718 }
719 } SMARTLIST_FOREACH_END(ent);
720 smartlist_add(parsed, unexpanded);
721 } SMARTLIST_FOREACH_END(vote);
722
723 // Sort the list of names.
724 smartlist_sort_strings(proto_names);
725
726 // For each named protocol, compute the consensus.
727 //
728 // This is not super-efficient, but it's not critical path.
729 SMARTLIST_FOREACH_BEGIN(proto_names, const char *, name) {
730 uint8_t counts[64];
731 memset(counts, 0, sizeof(counts));
732 // Count how many votes we got for each bit.
733 SMARTLIST_FOREACH_BEGIN(parsed, const smartlist_t *, vote) {
734 const proto_entry_t *ent = find_entry_by_name(vote, name);
735 if (! ent)
736 continue;
737
738 for (int i = 0; i < 64; ++i) {
739 if ((ent->bitmask & BIT(i)) != 0) {
740 ++ counts[i];
741 }
742 }
743 } SMARTLIST_FOREACH_END(vote);
744
745 uint64_t result_bitmask = 0;
746 for (int i = 0; i < 64; ++i) {
747 if (counts[i] >= threshold) {
748 result_bitmask |= BIT(i);
749 }
750 }
751 if (result_bitmask != 0) {
752 proto_entry_t *newent = tor_malloc_zero(sizeof(proto_entry_t));
753 newent->name = tor_strdup(name);
754 newent->bitmask = result_bitmask;
755 smartlist_add(result, newent);
756 }
757 } SMARTLIST_FOREACH_END(name);
758
759 char *consensus = encode_protocol_list(result);
760
761 SMARTLIST_FOREACH(result, proto_entry_t *, ent, proto_entry_free(ent));
762 smartlist_free(result);
763 smartlist_free(proto_names); // no need to free members; they are aliases.
765 SMARTLIST_FOREACH(v, proto_entry_t *, ent, proto_entry_free(ent));
766 smartlist_free(v);
767 } SMARTLIST_FOREACH_END(v);
768 smartlist_free(parsed);
769
770 return consensus;
771}
772
773/** Return true if every protocol version described in the string <b>s</b> is
774 * one that we support, and false otherwise. If <b>missing_out</b> is
775 * provided, set it to the list of protocols we do not support.
776 *
777 * If the protocol version string is unparseable, treat it as if it defines no
778 * protocols, and return 1.
779 **/
780int
781protover_all_supported(const char *s, char **missing_out)
782{
783 if (!s) {
784 return 1;
785 }
786
787 smartlist_t *entries = parse_protocol_list(s);
788 if (BUG(entries == NULL)) {
789 log_warn(LD_NET, "Received an unparseable protocol list %s"
790 " from the consensus", escaped(s));
791 return 1;
792 }
793 const smartlist_t *supported = get_supported_protocol_list();
794 smartlist_t *missing = smartlist_new();
795
796 SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
797 const proto_entry_t *mine = find_entry_by_name(supported, ent->name);
798 if (mine == NULL) {
799 if (ent->bitmask != 0) {
800 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
801 m->name = tor_strdup(ent->name);
802 m->bitmask = ent->bitmask;
803 smartlist_add(missing, m);
804 }
805 continue;
806 }
807
808 uint64_t missing_mask = ent->bitmask & ~mine->bitmask;
809 if (missing_mask != 0) {
810 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t));
811 m->name = tor_strdup(ent->name);
812 m->bitmask = missing_mask;
813 smartlist_add(missing, m);
814 }
815 } SMARTLIST_FOREACH_END(ent);
816
817 const int all_supported = (smartlist_len(missing) == 0);
818 if (!all_supported && missing_out) {
819 *missing_out = encode_protocol_list(missing);
820 }
821
822 SMARTLIST_FOREACH(missing, proto_entry_t *, ent, proto_entry_free(ent));
823 smartlist_free(missing);
824
825 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
826 smartlist_free(entries);
827
828 return all_supported;
829}
830
831/** Helper: return the member of 'protos' whose name is
832 * 'name', or NULL if there is no such member. */
833static const proto_entry_t *
834find_entry_by_name(const smartlist_t *protos, const char *name)
835{
836 if (!protos) {
837 return NULL;
838 }
839 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
840 if (!strcmp(ent->name, name)) {
841 return ent;
842 }
843 } SMARTLIST_FOREACH_END(ent);
844
845 return NULL;
846}
847
848/** Helper: Given a list of proto_entry_t, return true iff
849 * <b>pr</b>=<b>ver</b> is included in that list. */
850static int
852 protocol_type_t pr, uint32_t ver)
853{
854 if (BUG(protos == NULL)) {
855 return 0; // LCOV_EXCL_LINE
856 }
857 const char *pr_name = protocol_type_to_str(pr);
858 if (BUG(pr_name == NULL)) {
859 return 0; // LCOV_EXCL_LINE
860 }
861 if (ver > MAX_PROTOCOL_VERSION) {
862 return 0;
863 }
864
865 const proto_entry_t *ent = find_entry_by_name(protos, pr_name);
866 if (ent) {
867 return (ent->bitmask & BIT(ver)) != 0;
868 }
869 return 0;
870}
871
872/** Return a string describing the protocols supported by tor version
873 * <b>version</b>, or an empty string if we cannot tell.
874 *
875 * Note that this is only used to infer protocols for Tor versions that
876 * can't declare their own.
877 **/
878/// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
879const char *
881{
882 if (version == NULL) {
883 /* No known version; guess the oldest series that is still supported. */
884 version = "0.2.5.15";
885 }
886
887 if (tor_version_as_new_as(version,
889 return "";
890 } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
891 /* 0.2.9.1-alpha HSRend=2 */
892 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
893 "Link=1-4 LinkAuth=1 "
894 "Microdesc=1-2 Relay=1-2";
895 } else if (tor_version_as_new_as(version, "0.2.7.5")) {
896 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
897 * ed25519 support. We'll call them present only in "stable" 027,
898 * though. */
899 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
900 "Link=1-4 LinkAuth=1 "
901 "Microdesc=1-2 Relay=1-2";
902 } else if (tor_version_as_new_as(version, "0.2.4.19")) {
903 /* No currently supported Tor server versions are older than this, or
904 * lack these protocols. */
905 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
906 "Link=1-4 LinkAuth=1 "
907 "Microdesc=1 Relay=1-2";
908 } else {
909 /* Cannot infer protocols. */
910 return "";
911 }
912}
913
914/**
915 * Release all storage held by static fields in protover.c
916 */
917void
919{
922 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
923 smartlist_free(entries);
925 }
926}
const char * name
Definition config.c:2475
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:661
static const proto_entry_t * find_entry_by_name(const smartlist_t *protos, const char *name)
Definition protover.c:834
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:536
const char * protover_get_required_relay_protocols(void)
Definition protover.c:569
static smartlist_t * supported_protocol_list
Definition protover.c:592
static const smartlist_t * get_supported_protocol_list(void)
Definition protover.c:597
#define MAX_PROTOCOL_VERSION
Definition protover.c:99
const char * protover_get_required_client_protocols(void)
Definition protover.c:555
void protover_free_all(void)
Definition protover.c:918
static void proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
Definition protover.c:628
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:518
static int protocol_list_contains(const smartlist_t *protos, protocol_type_t pr, uint32_t ver)
Definition protover.c:851
int protocol_list_supports_protocol(const char *list, protocol_type_t tp, uint32_t version)
Definition protover.c:330
static const struct @13 PROTOCOL_NAMES[]
C_RUST_COUPLED: src/rust/protover/protover.rs PROTOCOL_NAMES
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:692
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:433
#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:880
static int trailing_zeros(uint64_t x)
Definition protover.c:608
const char * protover_get_supported(const protocol_type_t type)
Definition protover.c:407
int protover_all_supported(const char *s, char **missing_out)
Definition protover.c:781
Headers and type declarations for protover.c.
#define FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS
Definition protover.h:23
protocol_type_t
Definition protover.h:72
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.