protover.c 22 KB

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