protover.c 22 KB

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