protover.c 23 KB

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