protover.c 22 KB

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