protover.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file protover.c
  5. * \brief Versioning information for different pieces of the Tor protocol.
  6. *
  7. * Starting in version 0.2.9.3-alpha, Tor places separate version numbers on
  8. * each of the different components of its protocol. Relays use these numbers
  9. * to advertise what versions of the protocols they can support, and clients
  10. * use them to find what they can ask a given relay to do. Authorities vote
  11. * on the supported protocol versions for each relay, and also vote on the
  12. * which protocols you should have to support in order to be on the Tor
  13. * network. All Tor instances use these required/recommended protocol versions
  14. * to tell what level of support for recent protocols each relay has, and
  15. * to decide whether they should be running given their current protocols.
  16. *
  17. * The main advantage of these protocol versions numbers over using Tor
  18. * version numbers is that they allow different implementations of the Tor
  19. * protocols to develop independently, without having to claim compatibility
  20. * with specific versions of Tor.
  21. **/
  22. #define PROTOVER_PRIVATE
  23. #include "or.h"
  24. #include "protover.h"
  25. #include "routerparse.h"
  26. #ifndef HAVE_RUST
  27. static const smartlist_t *get_supported_protocol_list(void);
  28. static int protocol_list_contains(const smartlist_t *protos,
  29. protocol_type_t pr, uint32_t ver);
  30. /** Mapping between protocol type string and protocol type. */
  31. /// C_RUST_COUPLED: src/rust/protover/protover.rs `PROTOCOL_NAMES`
  32. static const struct {
  33. protocol_type_t protover_type;
  34. const char *name;
  35. } PROTOCOL_NAMES[] = {
  36. { PRT_LINK, "Link" },
  37. { PRT_LINKAUTH, "LinkAuth" },
  38. { PRT_RELAY, "Relay" },
  39. { PRT_DIRCACHE, "DirCache" },
  40. { PRT_HSDIR, "HSDir" },
  41. { PRT_HSINTRO, "HSIntro" },
  42. { PRT_HSREND, "HSRend" },
  43. { PRT_DESC, "Desc" },
  44. { PRT_MICRODESC, "Microdesc"},
  45. { PRT_CONS, "Cons" }
  46. };
  47. #define N_PROTOCOL_NAMES ARRAY_LENGTH(PROTOCOL_NAMES)
  48. /**
  49. * Given a protocol_type_t, return the corresponding string used in
  50. * descriptors.
  51. */
  52. STATIC const char *
  53. protocol_type_to_str(protocol_type_t pr)
  54. {
  55. unsigned i;
  56. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  57. if (PROTOCOL_NAMES[i].protover_type == pr)
  58. return PROTOCOL_NAMES[i].name;
  59. }
  60. /* LCOV_EXCL_START */
  61. tor_assert_nonfatal_unreached_once();
  62. return "UNKNOWN";
  63. /* LCOV_EXCL_STOP */
  64. }
  65. /**
  66. * Given a string, find the corresponding protocol type and store it in
  67. * <b>pr_out</b>. Return 0 on success, -1 on failure.
  68. */
  69. STATIC int
  70. str_to_protocol_type(const char *s, protocol_type_t *pr_out)
  71. {
  72. if (BUG(!pr_out))
  73. return -1;
  74. unsigned i;
  75. for (i=0; i < N_PROTOCOL_NAMES; ++i) {
  76. if (0 == strcmp(s, PROTOCOL_NAMES[i].name)) {
  77. *pr_out = PROTOCOL_NAMES[i].protover_type;
  78. return 0;
  79. }
  80. }
  81. return -1;
  82. }
  83. /**
  84. * Release all space held by a single proto_entry_t structure
  85. */
  86. STATIC void
  87. proto_entry_free_(proto_entry_t *entry)
  88. {
  89. if (!entry)
  90. return;
  91. tor_free(entry->name);
  92. SMARTLIST_FOREACH(entry->ranges, proto_range_t *, r, tor_free(r));
  93. smartlist_free(entry->ranges);
  94. tor_free(entry);
  95. }
  96. /** The largest possible protocol version. */
  97. #define MAX_PROTOCOL_VERSION (UINT32_MAX-1)
  98. /**
  99. * Given a string <b>s</b> and optional end-of-string pointer
  100. * <b>end_of_range</b>, parse the protocol range and store it in
  101. * <b>low_out</b> and <b>high_out</b>. A protocol range has the format U, or
  102. * U-U, where U is an unsigned 32-bit integer.
  103. */
  104. static int
  105. parse_version_range(const char *s, const char *end_of_range,
  106. uint32_t *low_out, uint32_t *high_out)
  107. {
  108. uint32_t low, high;
  109. char *next = NULL;
  110. int ok;
  111. tor_assert(high_out);
  112. tor_assert(low_out);
  113. if (BUG(!end_of_range))
  114. end_of_range = s + strlen(s); // LCOV_EXCL_LINE
  115. /* A range must start with a digit. */
  116. if (!TOR_ISDIGIT(*s)) {
  117. goto error;
  118. }
  119. /* Note that this wouldn't be safe if we didn't know that eventually,
  120. * we'd hit a NUL */
  121. low = (uint32_t) tor_parse_ulong(s, 10, 0, MAX_PROTOCOL_VERSION, &ok, &next);
  122. if (!ok)
  123. goto error;
  124. if (next > end_of_range)
  125. goto error;
  126. if (next == end_of_range) {
  127. high = low;
  128. goto done;
  129. }
  130. if (*next != '-')
  131. goto error;
  132. s = next+1;
  133. /* ibid */
  134. if (!TOR_ISDIGIT(*s)) {
  135. goto error;
  136. }
  137. high = (uint32_t) tor_parse_ulong(s, 10, 0,
  138. MAX_PROTOCOL_VERSION, &ok, &next);
  139. if (!ok)
  140. goto error;
  141. if (next != end_of_range)
  142. goto error;
  143. if (low > high)
  144. goto error;
  145. done:
  146. *high_out = high;
  147. *low_out = low;
  148. return 0;
  149. error:
  150. return -1;
  151. }
  152. /** Parse a single protocol entry from <b>s</b> up to an optional
  153. * <b>end_of_entry</b> pointer, and return that protocol entry. Return NULL
  154. * on error.
  155. *
  156. * A protocol entry has a keyword, an = sign, and zero or more ranges. */
  157. static proto_entry_t *
  158. parse_single_entry(const char *s, const char *end_of_entry)
  159. {
  160. proto_entry_t *out = tor_malloc_zero(sizeof(proto_entry_t));
  161. const char *equals;
  162. out->ranges = smartlist_new();
  163. if (BUG (!end_of_entry))
  164. end_of_entry = s + strlen(s); // LCOV_EXCL_LINE
  165. /* There must be an =. */
  166. equals = memchr(s, '=', end_of_entry - s);
  167. if (!equals)
  168. goto error;
  169. /* The name must be nonempty */
  170. if (equals == s)
  171. goto error;
  172. out->name = tor_strndup(s, equals-s);
  173. tor_assert(equals < end_of_entry);
  174. s = equals + 1;
  175. while (s < end_of_entry) {
  176. const char *comma = memchr(s, ',', end_of_entry-s);
  177. proto_range_t *range = tor_malloc_zero(sizeof(proto_range_t));
  178. if (! comma)
  179. comma = end_of_entry;
  180. smartlist_add(out->ranges, range);
  181. if (parse_version_range(s, comma, &range->low, &range->high) < 0) {
  182. goto error;
  183. }
  184. s = comma;
  185. while (*s == ',' && s < end_of_entry)
  186. ++s;
  187. }
  188. return out;
  189. error:
  190. proto_entry_free(out);
  191. return NULL;
  192. }
  193. /**
  194. * Parse the protocol list from <b>s</b> and return it as a smartlist of
  195. * proto_entry_t
  196. */
  197. STATIC smartlist_t *
  198. parse_protocol_list(const char *s)
  199. {
  200. smartlist_t *entries = smartlist_new();
  201. while (*s) {
  202. /* Find the next space or the NUL. */
  203. const char *end_of_entry = strchr(s, ' ');
  204. proto_entry_t *entry;
  205. if (!end_of_entry)
  206. end_of_entry = s + strlen(s);
  207. entry = parse_single_entry(s, end_of_entry);
  208. if (! entry)
  209. goto error;
  210. smartlist_add(entries, entry);
  211. s = end_of_entry;
  212. while (*s == ' ')
  213. ++s;
  214. }
  215. return entries;
  216. error:
  217. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  218. smartlist_free(entries);
  219. return NULL;
  220. }
  221. /**
  222. * Given a protocol type and version number, return true iff we know
  223. * how to speak that protocol.
  224. */
  225. int
  226. protover_is_supported_here(protocol_type_t pr, uint32_t ver)
  227. {
  228. const smartlist_t *ours = get_supported_protocol_list();
  229. return protocol_list_contains(ours, pr, ver);
  230. }
  231. /**
  232. * Return true iff "list" encodes a protocol list that includes support for
  233. * the indicated protocol and version.
  234. */
  235. int
  236. protocol_list_supports_protocol(const char *list, protocol_type_t tp,
  237. uint32_t version)
  238. {
  239. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  240. * up in profiles, we should memoize it.
  241. */
  242. smartlist_t *protocols = parse_protocol_list(list);
  243. if (!protocols) {
  244. return 0;
  245. }
  246. int contains = protocol_list_contains(protocols, tp, version);
  247. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  248. smartlist_free(protocols);
  249. return contains;
  250. }
  251. /**
  252. * Return true iff "list" encodes a protocol list that includes support for
  253. * the indicated protocol and version, or some later version.
  254. */
  255. int
  256. protocol_list_supports_protocol_or_later(const char *list,
  257. protocol_type_t tp,
  258. uint32_t version)
  259. {
  260. /* NOTE: This is a pretty inefficient implementation. If it ever shows
  261. * up in profiles, we should memoize it.
  262. */
  263. smartlist_t *protocols = parse_protocol_list(list);
  264. if (!protocols) {
  265. return 0;
  266. }
  267. const char *pr_name = protocol_type_to_str(tp);
  268. int contains = 0;
  269. SMARTLIST_FOREACH_BEGIN(protocols, proto_entry_t *, proto) {
  270. if (strcasecmp(proto->name, pr_name))
  271. continue;
  272. SMARTLIST_FOREACH_BEGIN(proto->ranges, const proto_range_t *, range) {
  273. if (range->high >= version) {
  274. contains = 1;
  275. goto found;
  276. }
  277. } SMARTLIST_FOREACH_END(range);
  278. } SMARTLIST_FOREACH_END(proto);
  279. found:
  280. SMARTLIST_FOREACH(protocols, proto_entry_t *, ent, proto_entry_free(ent));
  281. smartlist_free(protocols);
  282. return contains;
  283. }
  284. /** Return the canonical string containing the list of protocols
  285. * that we support. */
  286. /// C_RUST_COUPLED: src/rust/protover/protover.rs `SUPPORTED_PROTOCOLS`
  287. const char *
  288. protover_get_supported_protocols(void)
  289. {
  290. return
  291. "Cons=1-2 "
  292. "Desc=1-2 "
  293. "DirCache=1-2 "
  294. "HSDir=1-2 "
  295. "HSIntro=3-4 "
  296. "HSRend=1-2 "
  297. "Link=1-5 "
  298. "LinkAuth=1,3 "
  299. "Microdesc=1-2 "
  300. "Relay=1-2";
  301. }
  302. /** The protocols from protover_get_supported_protocols(), as parsed into a
  303. * list of proto_entry_t values. Access this via
  304. * get_supported_protocol_list. */
  305. static smartlist_t *supported_protocol_list = NULL;
  306. /** Return a pointer to a smartlist of proto_entry_t for the protocols
  307. * we support. */
  308. static const smartlist_t *
  309. get_supported_protocol_list(void)
  310. {
  311. if (PREDICT_UNLIKELY(supported_protocol_list == NULL)) {
  312. supported_protocol_list =
  313. parse_protocol_list(protover_get_supported_protocols());
  314. }
  315. return supported_protocol_list;
  316. }
  317. /**
  318. * Given a protocol entry, encode it at the end of the smartlist <b>chunks</b>
  319. * as one or more newly allocated strings.
  320. */
  321. static void
  322. proto_entry_encode_into(smartlist_t *chunks, const proto_entry_t *entry)
  323. {
  324. smartlist_add_asprintf(chunks, "%s=", entry->name);
  325. SMARTLIST_FOREACH_BEGIN(entry->ranges, proto_range_t *, range) {
  326. const char *comma = "";
  327. if (range_sl_idx != 0)
  328. comma = ",";
  329. if (range->low == range->high) {
  330. smartlist_add_asprintf(chunks, "%s%lu",
  331. comma, (unsigned long)range->low);
  332. } else {
  333. smartlist_add_asprintf(chunks, "%s%lu-%lu",
  334. comma, (unsigned long)range->low,
  335. (unsigned long)range->high);
  336. }
  337. } SMARTLIST_FOREACH_END(range);
  338. }
  339. /** Given a list of space-separated proto_entry_t items,
  340. * encode it into a newly allocated space-separated string. */
  341. STATIC char *
  342. encode_protocol_list(const smartlist_t *sl)
  343. {
  344. const char *separator = "";
  345. smartlist_t *chunks = smartlist_new();
  346. SMARTLIST_FOREACH_BEGIN(sl, const proto_entry_t *, ent) {
  347. smartlist_add_strdup(chunks, separator);
  348. proto_entry_encode_into(chunks, ent);
  349. separator = " ";
  350. } SMARTLIST_FOREACH_END(ent);
  351. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  352. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  353. smartlist_free(chunks);
  354. return result;
  355. }
  356. /* We treat any protocol list with more than this many subprotocols in it
  357. * as a DoS attempt. */
  358. /// C_RUST_COUPLED: src/rust/protover/protover.rs
  359. /// `MAX_PROTOCOLS_TO_EXPAND`
  360. static const int MAX_PROTOCOLS_TO_EXPAND = (1<<16);
  361. /** Voting helper: Given a list of proto_entry_t, return a newly allocated
  362. * smartlist of newly allocated strings, one for each included protocol
  363. * version. (So 'Foo=3,5-7' expands to a list of 'Foo=3', 'Foo=5', 'Foo=6',
  364. * 'Foo=7'.)
  365. *
  366. * Do not list any protocol version more than once.
  367. *
  368. * Return NULL if the list would be too big.
  369. */
  370. static smartlist_t *
  371. expand_protocol_list(const smartlist_t *protos)
  372. {
  373. smartlist_t *expanded = smartlist_new();
  374. if (!protos)
  375. return expanded;
  376. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  377. const char *name = ent->name;
  378. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  379. uint32_t u;
  380. for (u = range->low; u <= range->high; ++u) {
  381. smartlist_add_asprintf(expanded, "%s=%lu", name, (unsigned long)u);
  382. if (smartlist_len(expanded) > MAX_PROTOCOLS_TO_EXPAND)
  383. goto too_many;
  384. }
  385. } SMARTLIST_FOREACH_END(range);
  386. } SMARTLIST_FOREACH_END(ent);
  387. smartlist_sort_strings(expanded);
  388. smartlist_uniq_strings(expanded); // This makes voting work. do not remove
  389. return expanded;
  390. too_many:
  391. SMARTLIST_FOREACH(expanded, char *, cp, tor_free(cp));
  392. smartlist_free(expanded);
  393. return NULL;
  394. }
  395. /** Voting helper: compare two singleton proto_entry_t items by version
  396. * alone. (A singleton item is one with a single range entry where
  397. * low==high.) */
  398. static int
  399. cmp_single_ent_by_version(const void **a_, const void **b_)
  400. {
  401. const proto_entry_t *ent_a = *a_;
  402. const proto_entry_t *ent_b = *b_;
  403. tor_assert(smartlist_len(ent_a->ranges) == 1);
  404. tor_assert(smartlist_len(ent_b->ranges) == 1);
  405. const proto_range_t *a = smartlist_get(ent_a->ranges, 0);
  406. const proto_range_t *b = smartlist_get(ent_b->ranges, 0);
  407. tor_assert(a->low == a->high);
  408. tor_assert(b->low == b->high);
  409. if (a->low < b->low) {
  410. return -1;
  411. } else if (a->low == b->low) {
  412. return 0;
  413. } else {
  414. return 1;
  415. }
  416. }
  417. /** Voting helper: Given a list of singleton protocol strings (of the form
  418. * Foo=7), return a canonical listing of all the protocol versions listed,
  419. * with as few ranges as possible, with protocol versions sorted lexically and
  420. * versions sorted in numerically increasing order, using as few range entries
  421. * as possible.
  422. **/
  423. static char *
  424. contract_protocol_list(const smartlist_t *proto_strings)
  425. {
  426. // map from name to list of single-version entries
  427. strmap_t *entry_lists_by_name = strmap_new();
  428. // list of protocol names
  429. smartlist_t *all_names = smartlist_new();
  430. // list of strings for the output we're building
  431. smartlist_t *chunks = smartlist_new();
  432. // Parse each item and stick it entry_lists_by_name. Build
  433. // 'all_names' at the same time.
  434. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  435. if (BUG(!s))
  436. continue;// LCOV_EXCL_LINE
  437. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  438. if (BUG(!ent))
  439. continue; // LCOV_EXCL_LINE
  440. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  441. if (!lst) {
  442. smartlist_add(all_names, ent->name);
  443. lst = smartlist_new();
  444. strmap_set(entry_lists_by_name, ent->name, lst);
  445. }
  446. smartlist_add(lst, ent);
  447. } SMARTLIST_FOREACH_END(s);
  448. // We want to output the protocols sorted by their name.
  449. smartlist_sort_strings(all_names);
  450. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  451. const int first_entry = (name_sl_idx == 0);
  452. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  453. tor_assert(lst);
  454. // Sort every entry with this name by version. They are
  455. // singletons, so there can't be overlap.
  456. smartlist_sort(lst, cmp_single_ent_by_version);
  457. if (! first_entry)
  458. smartlist_add_strdup(chunks, " ");
  459. /* We're going to construct this entry from the ranges. */
  460. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  461. entry->ranges = smartlist_new();
  462. entry->name = tor_strdup(name);
  463. // Now, find all the ranges of versions start..end where
  464. // all of start, start+1, start+2, ..end are included.
  465. int start_of_cur_series = 0;
  466. while (start_of_cur_series < smartlist_len(lst)) {
  467. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  468. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  469. const uint32_t ver_low = range->low;
  470. uint32_t ver_high = ver_low;
  471. int idx;
  472. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  473. ent = smartlist_get(lst, idx);
  474. range = smartlist_get(ent->ranges, 0);
  475. if (range->low != ver_high + 1)
  476. break;
  477. ver_high += 1;
  478. }
  479. // Now idx is either off the end of the list, or the first sequence
  480. // break in the list.
  481. start_of_cur_series = idx;
  482. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  483. new_range->low = ver_low;
  484. new_range->high = ver_high;
  485. smartlist_add(entry->ranges, new_range);
  486. }
  487. proto_entry_encode_into(chunks, entry);
  488. proto_entry_free(entry);
  489. } SMARTLIST_FOREACH_END(name);
  490. // Build the result...
  491. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  492. // And free all the stuff we allocated.
  493. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  494. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  495. tor_assert(lst);
  496. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  497. smartlist_free(lst);
  498. } SMARTLIST_FOREACH_END(name);
  499. strmap_free(entry_lists_by_name, NULL);
  500. smartlist_free(all_names);
  501. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  502. smartlist_free(chunks);
  503. return result;
  504. }
  505. /**
  506. * Protocol voting implementation.
  507. *
  508. * Given a list of strings describing protocol versions, return a newly
  509. * allocated string encoding all of the protocols that are listed by at
  510. * least <b>threshold</b> of the inputs.
  511. *
  512. * The string is minimal and sorted according to the rules of
  513. * contract_protocol_list above.
  514. */
  515. char *
  516. protover_compute_vote(const smartlist_t *list_of_proto_strings,
  517. int threshold)
  518. {
  519. smartlist_t *all_entries = smartlist_new();
  520. // First, parse the inputs and break them into singleton entries.
  521. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  522. smartlist_t *unexpanded = parse_protocol_list(vote);
  523. if (! unexpanded) {
  524. log_warn(LD_NET, "I failed with parsing a protocol list from "
  525. "an authority. The offending string was: %s",
  526. escaped(vote));
  527. continue;
  528. }
  529. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  530. if (this_vote == NULL) {
  531. log_warn(LD_NET, "When expanding a protocol list from an authority, I "
  532. "got too many protocols. This is possibly an attack or a bug, "
  533. "unless the Tor network truly has expanded to support over %d "
  534. "different subprotocol versions. The offending string was: %s",
  535. MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
  536. } else {
  537. smartlist_add_all(all_entries, this_vote);
  538. smartlist_free(this_vote);
  539. }
  540. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  541. smartlist_free(unexpanded);
  542. } SMARTLIST_FOREACH_END(vote);
  543. // Now sort the singleton entries
  544. smartlist_sort_strings(all_entries);
  545. // Now find all the strings that appear at least 'threshold' times.
  546. smartlist_t *include_entries = smartlist_new();
  547. const char *cur_entry = smartlist_get(all_entries, 0);
  548. int n_times = 0;
  549. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  550. if (!strcmp(ent, cur_entry)) {
  551. n_times++;
  552. } else {
  553. if (n_times >= threshold && cur_entry)
  554. smartlist_add(include_entries, (void*)cur_entry);
  555. cur_entry = ent;
  556. n_times = 1 ;
  557. }
  558. } SMARTLIST_FOREACH_END(ent);
  559. if (n_times >= threshold && cur_entry)
  560. smartlist_add(include_entries, (void*)cur_entry);
  561. // Finally, compress that list.
  562. char *result = contract_protocol_list(include_entries);
  563. smartlist_free(include_entries);
  564. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  565. smartlist_free(all_entries);
  566. return result;
  567. }
  568. /** Return true if every protocol version described in the string <b>s</b> is
  569. * one that we support, and false otherwise. If <b>missing_out</b> is
  570. * provided, set it to the list of protocols we do not support.
  571. *
  572. * NOTE: This is quadratic, but we don't do it much: only a few times per
  573. * consensus. Checking signatures should be way more expensive than this
  574. * ever would be.
  575. **/
  576. int
  577. protover_all_supported(const char *s, char **missing_out)
  578. {
  579. int all_supported = 1;
  580. smartlist_t *missing_some;
  581. smartlist_t *missing_completely;
  582. smartlist_t *missing_all;
  583. if (!s) {
  584. return 1;
  585. }
  586. smartlist_t *entries = parse_protocol_list(s);
  587. if (BUG(entries == NULL)) {
  588. log_warn(LD_NET, "Received an unparseable protocol list %s"
  589. " from the consensus", escaped(s));
  590. return 1;
  591. }
  592. missing_some = smartlist_new();
  593. missing_completely = smartlist_new();
  594. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  595. protocol_type_t tp;
  596. if (str_to_protocol_type(ent->name, &tp) < 0) {
  597. if (smartlist_len(ent->ranges)) {
  598. goto unsupported;
  599. }
  600. continue;
  601. }
  602. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  603. proto_entry_t *unsupported = tor_malloc_zero(sizeof(proto_entry_t));
  604. proto_range_t *versions = tor_malloc_zero(sizeof(proto_range_t));
  605. uint32_t i;
  606. unsupported->name = tor_strdup(ent->name);
  607. unsupported->ranges = smartlist_new();
  608. for (i = range->low; i <= range->high; ++i) {
  609. if (!protover_is_supported_here(tp, i)) {
  610. if (versions->low == 0 && versions->high == 0) {
  611. versions->low = i;
  612. /* Pre-emptively add the high now, just in case we're in a single
  613. * version range (e.g. "Link=999"). */
  614. versions->high = i;
  615. }
  616. /* If the last one to be unsupported is one less than the current
  617. * one, we're in a continuous range, so set the high field. */
  618. if ((versions->high && versions->high == i - 1) ||
  619. /* Similarly, if the last high wasn't set and we're currently
  620. * one higher than the low, add current index as the highest
  621. * known high. */
  622. (!versions->high && versions->low == i - 1)) {
  623. versions->high = i;
  624. continue;
  625. }
  626. } else {
  627. /* If we hit a supported version, and we previously had a range,
  628. * we've hit a non-continuity. Copy the previous range and add it to
  629. * the unsupported->ranges list and zero-out the previous range for
  630. * the next iteration. */
  631. if (versions->low != 0 && versions->high != 0) {
  632. proto_range_t *versions_to_add = tor_malloc(sizeof(proto_range_t));
  633. versions_to_add->low = versions->low;
  634. versions_to_add->high = versions->high;
  635. smartlist_add(unsupported->ranges, versions_to_add);
  636. versions->low = 0;
  637. versions->high = 0;
  638. }
  639. }
  640. }
  641. /* Once we've run out of versions to check, see if we had any unsupported
  642. * ones and, if so, add them to unsupported->ranges. */
  643. if (versions->low != 0 && versions->high != 0) {
  644. smartlist_add(unsupported->ranges, versions);
  645. }
  646. /* Finally, if we had something unsupported, add it to the list of
  647. * missing_some things and mark that there was something missing. */
  648. if (smartlist_len(unsupported->ranges) != 0) {
  649. smartlist_add(missing_some, (void*) unsupported);
  650. all_supported = 0;
  651. } else {
  652. proto_entry_free(unsupported);
  653. tor_free(versions);
  654. }
  655. } SMARTLIST_FOREACH_END(range);
  656. continue;
  657. unsupported:
  658. all_supported = 0;
  659. smartlist_add(missing_completely, (void*) ent);
  660. } SMARTLIST_FOREACH_END(ent);
  661. /* We keep the two smartlists separate so that we can free the proto_entry_t
  662. * we created and put in missing_some, so here we add them together to build
  663. * the string. */
  664. missing_all = smartlist_new();
  665. smartlist_add_all(missing_all, missing_some);
  666. smartlist_add_all(missing_all, missing_completely);
  667. if (missing_out && !all_supported) {
  668. tor_assert(smartlist_len(missing_all) != 0);
  669. *missing_out = encode_protocol_list(missing_all);
  670. }
  671. SMARTLIST_FOREACH(missing_some, proto_entry_t *, ent, proto_entry_free(ent));
  672. smartlist_free(missing_some);
  673. smartlist_free(missing_completely);
  674. smartlist_free(missing_all);
  675. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  676. smartlist_free(entries);
  677. return all_supported;
  678. }
  679. /** Helper: Given a list of proto_entry_t, return true iff
  680. * <b>pr</b>=<b>ver</b> is included in that list. */
  681. static int
  682. protocol_list_contains(const smartlist_t *protos,
  683. protocol_type_t pr, uint32_t ver)
  684. {
  685. if (BUG(protos == NULL)) {
  686. return 0; // LCOV_EXCL_LINE
  687. }
  688. const char *pr_name = protocol_type_to_str(pr);
  689. if (BUG(pr_name == NULL)) {
  690. return 0; // LCOV_EXCL_LINE
  691. }
  692. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  693. if (strcasecmp(ent->name, pr_name))
  694. continue;
  695. /* name matches; check the ranges */
  696. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  697. if (ver >= range->low && ver <= range->high)
  698. return 1;
  699. } SMARTLIST_FOREACH_END(range);
  700. } SMARTLIST_FOREACH_END(ent);
  701. return 0;
  702. }
  703. /** Return a string describing the protocols supported by tor version
  704. * <b>version</b>, or an empty string if we cannot tell.
  705. *
  706. * Note that this is only used to infer protocols for Tor versions that
  707. * can't declare their own.
  708. **/
  709. /// C_RUST_COUPLED: src/rust/protover/protover.rs `compute_for_old_tor`
  710. const char *
  711. protover_compute_for_old_tor(const char *version)
  712. {
  713. if (version == NULL) {
  714. /* No known version; guess the oldest series that is still supported. */
  715. version = "0.2.5.15";
  716. }
  717. if (tor_version_as_new_as(version,
  718. FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
  719. return "";
  720. } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
  721. /* 0.2.9.1-alpha HSRend=2 */
  722. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
  723. "Link=1-4 LinkAuth=1 "
  724. "Microdesc=1-2 Relay=1-2";
  725. } else if (tor_version_as_new_as(version, "0.2.7.5")) {
  726. /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
  727. * ed25519 support. We'll call them present only in "stable" 027,
  728. * though. */
  729. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  730. "Link=1-4 LinkAuth=1 "
  731. "Microdesc=1-2 Relay=1-2";
  732. } else if (tor_version_as_new_as(version, "0.2.4.19")) {
  733. /* No currently supported Tor server versions are older than this, or
  734. * lack these protocols. */
  735. return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  736. "Link=1-4 LinkAuth=1 "
  737. "Microdesc=1 Relay=1-2";
  738. } else {
  739. /* Cannot infer protocols. */
  740. return "";
  741. }
  742. }
  743. /**
  744. * Release all storage held by static fields in protover.c
  745. */
  746. void
  747. protover_free_all(void)
  748. {
  749. if (supported_protocol_list) {
  750. smartlist_t *entries = supported_protocol_list;
  751. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  752. smartlist_free(entries);
  753. supported_protocol_list = NULL;
  754. }
  755. }
  756. #endif /* !defined(HAVE_RUST) */