protover.c (26362B)
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 31 static const smartlist_t *get_supported_protocol_list(void); 32 static int protocol_list_contains(const smartlist_t *protos, 33 protocol_type_t pr, uint32_t ver); 34 static 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` 39 static 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` 66 static const unsigned MAX_PROTOCOL_NAME_LENGTH = 100; 67 68 /** 69 * Given a protocol_type_t, return the corresponding string used in 70 * descriptors. 71 */ 72 STATIC const char * 73 protocol_type_to_str(protocol_type_t pr) 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 */ 89 STATIC void 90 proto_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 */ 107 static int 108 parse_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 165 static int 166 is_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 **/ 182 static uint64_t 183 bitmask_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. */ 197 static proto_entry_t * 198 parse_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 */ 263 STATIC smartlist_t * 264 parse_protocol_list(const char *s) 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 */ 300 bool 301 protover_list_is_invalid(const char *s) 302 { 303 smartlist_t *list = parse_protocol_list(s); 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 */ 315 int 316 protover_is_supported_here(protocol_type_t pr, uint32_t ver) 317 { 318 const smartlist_t *ours = get_supported_protocol_list(); 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 */ 329 int 330 protocol_list_supports_protocol(const char *list, protocol_type_t tp, 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 */ 354 int 355 protocol_list_supports_protocol_or_later(const char *list, 356 protocol_type_t tp, 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. */ 406 const char * 407 protover_get_supported(const protocol_type_t type) 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` 432 const char * 433 protover_get_supported_protocols(void) 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. */ 517 const char * 518 protover_get_recommended_client_protocols(void) 519 { 520 return "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4 HSRend=2 " 521 "Link=4-5 Microdesc=2 Relay=2-4"; 522 } 523 524 /** Return the recommended relay protocols list that directory authorities 525 * put in the consensus. */ 526 const char * 527 protover_get_recommended_relay_protocols(void) 528 { 529 return "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4-5 HSRend=2 " 530 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2-4"; 531 } 532 533 /** Return the required client protocols list that directory authorities 534 * put in the consensus. */ 535 const char * 536 protover_get_required_client_protocols(void) 537 { 538 return "Cons=2 Desc=2 FlowCtrl=1 Link=4 Microdesc=2 Relay=2"; 539 } 540 541 /** Return the required relay protocols list that directory authorities 542 * put in the consensus. */ 543 const char * 544 protover_get_required_relay_protocols(void) 545 { 546 return "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4-5 HSRend=2 " 547 "Link=4-5 LinkAuth=3 Microdesc=2 Relay=2-4"; 548 } 549 550 /* 551 * XXX END OF HAZARDOUS ZONE XXX 552 */ 553 554 /** The protocols from protover_get_supported_protocols(), as parsed into a 555 * list of proto_entry_t values. Access this via 556 * get_supported_protocol_list. */ 557 static smartlist_t *supported_protocol_list = NULL; 558 559 /** Return a pointer to a smartlist of proto_entry_t for the protocols 560 * we support. */ 561 static const smartlist_t * 562 get_supported_protocol_list(void) 563 { 564 if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) { 565 supported_protocol_list = 566 parse_protocol_list(protover_get_supported_protocols()); 567 } 568 return supported_protocol_list; 569 } 570 571 /** Return the number of trailing zeros in x. Undefined if x is 0. */ 572 static int 573 trailing_zeros(uint64_t x) 574 { 575 #ifdef __GNUC__ 576 return __builtin_ctzll((unsigned long long)x); 577 #else 578 int i; 579 for (i = 0; i <= 64; ++i) { 580 if (x&1) 581 return i; 582 x>>=1; 583 } 584 return i; 585 #endif /* defined(__GNUC__) */ 586 } 587 588 /** 589 * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b> 590 * as one or more newly allocated strings. 591 */ 592 static void 593 proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry) 594 { 595 smartlist_add_asprintf(chunks, "%s=", entry->name); 596 597 uint64_t mask = entry->bitmask; 598 int shift = 0; // how much have we shifted by so far? 599 bool first = true; 600 while (mask) { 601 const char *comma = first ? "" : ","; 602 if (first) { 603 first = false; 604 } 605 int zeros = trailing_zeros(mask); 606 mask >>= zeros; 607 shift += zeros; 608 int ones = !mask ? 64 : trailing_zeros(~mask); 609 if (ones == 1) { 610 smartlist_add_asprintf(chunks, "%s%d", comma, shift); 611 } else { 612 smartlist_add_asprintf(chunks, "%s%d-%d", comma, 613 shift, shift + ones - 1); 614 } 615 if (ones == 64) { 616 break; // avoid undefined behavior; can't shift by 64. 617 } 618 mask >>= ones; 619 shift += ones; 620 } 621 } 622 623 /** Given a list of space-separated proto_entry_t items, 624 * encode it into a newly allocated space-separated string. */ 625 STATIC char * 626 encode_protocol_list(const smartlist_t *sl) 627 { 628 const char *separator = ""; 629 smartlist_t *chunks = smartlist_new(); 630 SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) { 631 smartlist_add_strdup(chunks, separator); 632 633 proto_entry_encode_into(chunks, ent); 634 635 separator = " "; 636 } SMARTLIST_FOREACH_END(ent); 637 638 char *result = smartlist_join_strings(chunks, "", 0, NULL); 639 640 SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp)); 641 smartlist_free(chunks); 642 643 return result; 644 } 645 646 /** 647 * Protocol voting implementation. 648 * 649 * Given a list of strings describing protocol versions, return a newly 650 * allocated string encoding all of the protocols that are listed by at 651 * least <b>threshold</b> of the inputs. 652 * 653 * The string is minimal and sorted according to the rules of 654 * contract_protocol_list above. 655 */ 656 char * 657 protover_compute_vote(const smartlist_t *list_of_proto_strings, 658 int threshold) 659 { 660 // we use u8 counters below. 661 tor_assert(smartlist_len(list_of_proto_strings) < 256); 662 663 if (smartlist_len(list_of_proto_strings) == 0) { 664 return tor_strdup(""); 665 } 666 667 smartlist_t *parsed = smartlist_new(); // smartlist of smartlist of entries 668 smartlist_t *proto_names = smartlist_new(); // smartlist of strings 669 smartlist_t *result = smartlist_new(); // smartlist of entries 670 671 // First, parse the inputs, and accumulate a list of protocol names. 672 SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) { 673 smartlist_t *unexpanded = parse_protocol_list(vote); 674 if (! unexpanded) { 675 log_warn(LD_NET, "I failed with parsing a protocol list from " 676 "an authority. The offending string was: %s", 677 escaped(vote)); 678 continue; 679 } 680 SMARTLIST_FOREACH_BEGIN(unexpanded, const proto_entry_t *, ent) { 681 if (!smartlist_contains_string(proto_names,ent->name)) { 682 smartlist_add(proto_names, ent->name); 683 } 684 } SMARTLIST_FOREACH_END(ent); 685 smartlist_add(parsed, unexpanded); 686 } SMARTLIST_FOREACH_END(vote); 687 688 // Sort the list of names. 689 smartlist_sort_strings(proto_names); 690 691 // For each named protocol, compute the consensus. 692 // 693 // This is not super-efficient, but it's not critical path. 694 SMARTLIST_FOREACH_BEGIN(proto_names, const char *, name) { 695 uint8_t counts[64]; 696 memset(counts, 0, sizeof(counts)); 697 // Count how many votes we got for each bit. 698 SMARTLIST_FOREACH_BEGIN(parsed, const smartlist_t *, vote) { 699 const proto_entry_t *ent = find_entry_by_name(vote, name); 700 if (! ent) 701 continue; 702 703 for (int i = 0; i < 64; ++i) { 704 if ((ent->bitmask & BIT(i)) != 0) { 705 ++ counts[i]; 706 } 707 } 708 } SMARTLIST_FOREACH_END(vote); 709 710 uint64_t result_bitmask = 0; 711 for (int i = 0; i < 64; ++i) { 712 if (counts[i] >= threshold) { 713 result_bitmask |= BIT(i); 714 } 715 } 716 if (result_bitmask != 0) { 717 proto_entry_t *newent = tor_malloc_zero(sizeof(proto_entry_t)); 718 newent->name = tor_strdup(name); 719 newent->bitmask = result_bitmask; 720 smartlist_add(result, newent); 721 } 722 } SMARTLIST_FOREACH_END(name); 723 724 char *consensus = encode_protocol_list(result); 725 726 SMARTLIST_FOREACH(result, proto_entry_t *, ent, proto_entry_free(ent)); 727 smartlist_free(result); 728 smartlist_free(proto_names); // no need to free members; they are aliases. 729 SMARTLIST_FOREACH_BEGIN(parsed, smartlist_t *, v) { 730 SMARTLIST_FOREACH(v, proto_entry_t *, ent, proto_entry_free(ent)); 731 smartlist_free(v); 732 } SMARTLIST_FOREACH_END(v); 733 smartlist_free(parsed); 734 735 return consensus; 736 } 737 738 /** Return true if every protocol version described in the string <b>s</b> is 739 * one that we support, and false otherwise. If <b>missing_out</b> is 740 * provided, set it to the list of protocols we do not support. 741 * 742 * If the protocol version string is unparseable, treat it as if it defines no 743 * protocols, and return 1. 744 **/ 745 int 746 protover_all_supported(const char *s, char **missing_out) 747 { 748 if (!s) { 749 return 1; 750 } 751 752 smartlist_t *entries = parse_protocol_list(s); 753 if (BUG(entries == NULL)) { 754 log_warn(LD_NET, "Received an unparseable protocol list %s" 755 " from the consensus", escaped(s)); 756 return 1; 757 } 758 const smartlist_t *supported = get_supported_protocol_list(); 759 smartlist_t *missing = smartlist_new(); 760 761 SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) { 762 const proto_entry_t *mine = find_entry_by_name(supported, ent->name); 763 if (mine == NULL) { 764 if (ent->bitmask != 0) { 765 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t)); 766 m->name = tor_strdup(ent->name); 767 m->bitmask = ent->bitmask; 768 smartlist_add(missing, m); 769 } 770 continue; 771 } 772 773 uint64_t missing_mask = ent->bitmask & ~mine->bitmask; 774 if (missing_mask != 0) { 775 proto_entry_t *m = tor_malloc_zero(sizeof(proto_entry_t)); 776 m->name = tor_strdup(ent->name); 777 m->bitmask = missing_mask; 778 smartlist_add(missing, m); 779 } 780 } SMARTLIST_FOREACH_END(ent); 781 782 const int all_supported = (smartlist_len(missing) == 0); 783 if (!all_supported && missing_out) { 784 *missing_out = encode_protocol_list(missing); 785 } 786 787 SMARTLIST_FOREACH(missing, proto_entry_t *, ent, proto_entry_free(ent)); 788 smartlist_free(missing); 789 790 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent)); 791 smartlist_free(entries); 792 793 return all_supported; 794 } 795 796 /** Helper: return the member of 'protos' whose name is 797 * 'name', or NULL if there is no such member. */ 798 static const proto_entry_t * 799 find_entry_by_name(const smartlist_t *protos, const char *name) 800 { 801 if (!protos) { 802 return NULL; 803 } 804 SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) { 805 if (!strcmp(ent->name, name)) { 806 return ent; 807 } 808 } SMARTLIST_FOREACH_END(ent); 809 810 return NULL; 811 } 812 813 /** Helper: Given a list of proto_entry_t, return true iff 814 * <b>pr</b>=<b>ver</b> is included in that list. */ 815 static int 816 protocol_list_contains(const smartlist_t *protos, 817 protocol_type_t pr, uint32_t ver) 818 { 819 if (BUG(protos == NULL)) { 820 return 0; // LCOV_EXCL_LINE 821 } 822 const char *pr_name = protocol_type_to_str(pr); 823 if (BUG(pr_name == NULL)) { 824 return 0; // LCOV_EXCL_LINE 825 } 826 if (ver > MAX_PROTOCOL_VERSION) { 827 return 0; 828 } 829 830 const proto_entry_t *ent = find_entry_by_name(protos, pr_name); 831 if (ent) { 832 return (ent->bitmask & BIT(ver)) != 0; 833 } 834 return 0; 835 } 836 837 /** Return a string describing the protocols supported by tor version 838 * <b>version</b>, or an empty string if we cannot tell. 839 * 840 * Note that this is only used to infer protocols for Tor versions that 841 * can't declare their own. 842 **/ 843 /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor` 844 const char * 845 protover_compute_for_old_tor(const char *version) 846 { 847 if (version == NULL) { 848 /* No known version; guess the oldest series that is still supported. */ 849 version = "0.2.5.15"; 850 } 851 852 if (tor_version_as_new_as(version, 853 FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) { 854 return ""; 855 } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) { 856 /* 0.2.9.1-alpha HSRend=2 */ 857 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 " 858 "Link=1-4 LinkAuth=1 " 859 "Microdesc=1-2 Relay=1-2"; 860 } else if (tor_version_as_new_as(version, "0.2.7.5")) { 861 /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate 862 * ed25519 support. We'll call them present only in "stable" 027, 863 * though. */ 864 return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 " 865 "Link=1-4 LinkAuth=1 " 866 "Microdesc=1-2 Relay=1-2"; 867 } else if (tor_version_as_new_as(version, "0.2.4.19")) { 868 /* No currently supported Tor server versions are older than this, or 869 * lack these protocols. */ 870 return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 " 871 "Link=1-4 LinkAuth=1 " 872 "Microdesc=1 Relay=1-2"; 873 } else { 874 /* Cannot infer protocols. */ 875 return ""; 876 } 877 } 878 879 /** 880 * Release all storage held by static fields in protover.c 881 */ 882 void 883 protover_free_all(void) 884 { 885 if (supported_protocol_list) { 886 smartlist_t *entries = supported_protocol_list; 887 SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent)); 888 smartlist_free(entries); 889 supported_protocol_list = NULL; 890 } 891 }