protover.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. // map from name to list of single-version entries
  389. strmap_t *entry_lists_by_name = strmap_new();
  390. // list of protocol names
  391. smartlist_t *all_names = smartlist_new();
  392. // list of strings for the output we're building
  393. smartlist_t *chunks = smartlist_new();
  394. // Parse each item and stick it entry_lists_by_name. Build
  395. // 'all_names' at the same time.
  396. SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
  397. if (BUG(!s))
  398. continue;// LCOV_EXCL_LINE
  399. proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
  400. if (BUG(!ent))
  401. continue; // LCOV_EXCL_LINE
  402. smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
  403. if (!lst) {
  404. smartlist_add(all_names, ent->name);
  405. lst = smartlist_new();
  406. strmap_set(entry_lists_by_name, ent->name, lst);
  407. }
  408. smartlist_add(lst, ent);
  409. } SMARTLIST_FOREACH_END(s);
  410. // We want to output the protocols sorted by their name.
  411. smartlist_sort_strings(all_names);
  412. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  413. const int first_entry = (name_sl_idx == 0);
  414. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  415. tor_assert(lst);
  416. // Sort every entry with this name by version. They are
  417. // singletons, so there can't be overlap.
  418. smartlist_sort(lst, cmp_single_ent_by_version);
  419. if (! first_entry)
  420. smartlist_add(chunks, tor_strdup(" "));
  421. /* We're going to construct this entry from the ranges. */
  422. proto_entry_t *entry = tor_malloc_zero(sizeof(proto_entry_t));
  423. entry->ranges = smartlist_new();
  424. entry->name = tor_strdup(name);
  425. // Now, find all the ranges of versions start..end where
  426. // all of start, start+1, start+2, ..end are included.
  427. int start_of_cur_series = 0;
  428. while (start_of_cur_series < smartlist_len(lst)) {
  429. const proto_entry_t *ent = smartlist_get(lst, start_of_cur_series);
  430. const proto_range_t *range = smartlist_get(ent->ranges, 0);
  431. const uint32_t ver_low = range->low;
  432. uint32_t ver_high = ver_low;
  433. int idx;
  434. for (idx = start_of_cur_series+1; idx < smartlist_len(lst); ++idx) {
  435. ent = smartlist_get(lst, idx);
  436. range = smartlist_get(ent->ranges, 0);
  437. if (range->low != ver_high + 1)
  438. break;
  439. ver_high += 1;
  440. }
  441. // Now idx is either off the end of the list, or the first sequence
  442. // break in the list.
  443. start_of_cur_series = idx;
  444. proto_range_t *new_range = tor_malloc_zero(sizeof(proto_range_t));
  445. new_range->low = ver_low;
  446. new_range->high = ver_high;
  447. smartlist_add(entry->ranges, new_range);
  448. }
  449. proto_entry_encode_into(chunks, entry);
  450. proto_entry_free(entry);
  451. } SMARTLIST_FOREACH_END(name);
  452. // Build the result...
  453. char *result = smartlist_join_strings(chunks, "", 0, NULL);
  454. // And free all the stuff we allocated.
  455. SMARTLIST_FOREACH_BEGIN(all_names, const char *, name) {
  456. smartlist_t *lst = strmap_get(entry_lists_by_name, name);
  457. tor_assert(lst);
  458. SMARTLIST_FOREACH(lst, proto_entry_t *, e, proto_entry_free(e));
  459. smartlist_free(lst);
  460. } SMARTLIST_FOREACH_END(name);
  461. strmap_free(entry_lists_by_name, NULL);
  462. smartlist_free(all_names);
  463. SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
  464. smartlist_free(chunks);
  465. return result;
  466. }
  467. /**
  468. * Protocol voting implementation.
  469. *
  470. * Given a list of strings describing protocol versions, return a newly
  471. * allocated string encoding all of the protocols that are listed by at
  472. * least <b>threshold</b> of the inputs.
  473. *
  474. * The string is minimal and sorted according to the rules of
  475. * contract_protocol_list above.
  476. */
  477. char *
  478. protover_compute_vote(const smartlist_t *list_of_proto_strings,
  479. int threshold)
  480. {
  481. smartlist_t *all_entries = smartlist_new();
  482. // First, parse the inputs and break them into singleton entries.
  483. SMARTLIST_FOREACH_BEGIN(list_of_proto_strings, const char *, vote) {
  484. smartlist_t *unexpanded = parse_protocol_list(vote);
  485. if (! unexpanded) {
  486. log_warn(LD_NET, "I failed with parsing a protocol list from "
  487. "an authority. The offending string was: %s",
  488. escaped(vote));
  489. continue;
  490. }
  491. smartlist_t *this_vote = expand_protocol_list(unexpanded);
  492. if (this_vote == NULL) {
  493. log_warn(LD_NET, "When expanding a protocol list from an authority, I "
  494. "got too many protocols. This is possibly an attack or a bug, "
  495. "unless the Tor network truly has expanded to support over %d "
  496. "different subprotocol versions. The offending string was: %s",
  497. MAX_PROTOCOLS_TO_EXPAND, escaped(vote));
  498. } else {
  499. smartlist_add_all(all_entries, this_vote);
  500. smartlist_free(this_vote);
  501. }
  502. SMARTLIST_FOREACH(unexpanded, proto_entry_t *, e, proto_entry_free(e));
  503. smartlist_free(unexpanded);
  504. } SMARTLIST_FOREACH_END(vote);
  505. // Now sort the singleton entries
  506. smartlist_sort_strings(all_entries);
  507. // Now find all the strings that appear at least 'threshold' times.
  508. smartlist_t *include_entries = smartlist_new();
  509. const char *cur_entry = smartlist_get(all_entries, 0);
  510. int n_times = 0;
  511. SMARTLIST_FOREACH_BEGIN(all_entries, const char *, ent) {
  512. if (!strcmp(ent, cur_entry)) {
  513. n_times++;
  514. } else {
  515. if (n_times >= threshold && cur_entry)
  516. smartlist_add(include_entries, (void*)cur_entry);
  517. cur_entry = ent;
  518. n_times = 1 ;
  519. }
  520. } SMARTLIST_FOREACH_END(ent);
  521. if (n_times >= threshold && cur_entry)
  522. smartlist_add(include_entries, (void*)cur_entry);
  523. // Finally, compress that list.
  524. char *result = contract_protocol_list(include_entries);
  525. smartlist_free(include_entries);
  526. SMARTLIST_FOREACH(all_entries, char *, cp, tor_free(cp));
  527. smartlist_free(all_entries);
  528. return result;
  529. }
  530. /** Return true if every protocol version described in the string <b>s</b> is
  531. * one that we support, and false otherwise. If <b>missing_out</b> is
  532. * provided, set it to the list of protocols we do not support.
  533. *
  534. * NOTE: This is quadratic, but we don't do it much: only a few times per
  535. * consensus. Checking signatures should be way more expensive than this
  536. * ever would be.
  537. **/
  538. int
  539. protover_all_supported(const char *s, char **missing_out)
  540. {
  541. int all_supported = 1;
  542. smartlist_t *missing;
  543. if (!s) {
  544. return 1;
  545. }
  546. smartlist_t *entries = parse_protocol_list(s);
  547. if (BUG(entries == NULL)) {
  548. log_warn(LD_NET, "Received an unparseable protocol list %s"
  549. " from the consensus", escaped(s));
  550. return 1;
  551. }
  552. missing = smartlist_new();
  553. SMARTLIST_FOREACH_BEGIN(entries, const proto_entry_t *, ent) {
  554. protocol_type_t tp;
  555. if (str_to_protocol_type(ent->name, &tp) < 0) {
  556. if (smartlist_len(ent->ranges)) {
  557. goto unsupported;
  558. }
  559. continue;
  560. }
  561. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  562. uint32_t i;
  563. for (i = range->low; i <= range->high; ++i) {
  564. if (!protover_is_supported_here(tp, i)) {
  565. goto unsupported;
  566. }
  567. }
  568. } SMARTLIST_FOREACH_END(range);
  569. continue;
  570. unsupported:
  571. all_supported = 0;
  572. smartlist_add(missing, (void*) ent);
  573. } SMARTLIST_FOREACH_END(ent);
  574. if (missing_out && !all_supported) {
  575. tor_assert(0 != smartlist_len(missing));
  576. *missing_out = encode_protocol_list(missing);
  577. }
  578. smartlist_free(missing);
  579. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  580. smartlist_free(entries);
  581. return all_supported;
  582. }
  583. /** Helper: Given a list of proto_entry_t, return true iff
  584. * <b>pr</b>=<b>ver</b> is included in that list. */
  585. static int
  586. protocol_list_contains(const smartlist_t *protos,
  587. protocol_type_t pr, uint32_t ver)
  588. {
  589. if (BUG(protos == NULL)) {
  590. return 0; // LCOV_EXCL_LINE
  591. }
  592. const char *pr_name = protocol_type_to_str(pr);
  593. if (BUG(pr_name == NULL)) {
  594. return 0; // LCOV_EXCL_LINE
  595. }
  596. SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
  597. if (strcasecmp(ent->name, pr_name))
  598. continue;
  599. /* name matches; check the ranges */
  600. SMARTLIST_FOREACH_BEGIN(ent->ranges, const proto_range_t *, range) {
  601. if (ver >= range->low && ver <= range->high)
  602. return 1;
  603. } SMARTLIST_FOREACH_END(range);
  604. } SMARTLIST_FOREACH_END(ent);
  605. return 0;
  606. }
  607. /** Return a string describing the protocols supported by tor version
  608. * <b>version</b>, or an empty string if we cannot tell.
  609. *
  610. * Note that this is only used to infer protocols for Tor versions that
  611. * can't declare their own.
  612. **/
  613. const char *
  614. protover_compute_for_old_tor(const char *version)
  615. {
  616. if (version == NULL) {
  617. /* No known version; guess the oldest series that is still supported. */
  618. version = "0.2.5.15";
  619. }
  620. if (tor_version_as_new_as(version,
  621. FIRST_TOR_VERSION_TO_ADVERTISE_PROTOCOLS)) {
  622. return "";
  623. } else if (tor_version_as_new_as(version, "0.2.9.1-alpha")) {
  624. /* 0.2.9.1-alpha HSRend=2 */
  625. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1-2 "
  626. "Link=1-4 LinkAuth=1 "
  627. "Microdesc=1-2 Relay=1-2";
  628. } else if (tor_version_as_new_as(version, "0.2.7.5")) {
  629. /* 0.2.7-stable added Desc=2, Microdesc=2, Cons=2, which indicate
  630. * ed25519 support. We'll call them present only in "stable" 027,
  631. * though. */
  632. return "Cons=1-2 Desc=1-2 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  633. "Link=1-4 LinkAuth=1 "
  634. "Microdesc=1-2 Relay=1-2";
  635. } else if (tor_version_as_new_as(version, "0.2.4.19")) {
  636. /* No currently supported Tor server versions are older than this, or
  637. * lack these protocols. */
  638. return "Cons=1 Desc=1 DirCache=1 HSDir=1 HSIntro=3 HSRend=1 "
  639. "Link=1-4 LinkAuth=1 "
  640. "Microdesc=1 Relay=1-2";
  641. } else {
  642. /* Cannot infer protocols. */
  643. return "";
  644. }
  645. }
  646. /**
  647. * Release all storage held by static fields in protover.c
  648. */
  649. void
  650. protover_free_all(void)
  651. {
  652. if (supported_protocol_list) {
  653. smartlist_t *entries = supported_protocol_list;
  654. SMARTLIST_FOREACH(entries, proto_entry_t *, ent, proto_entry_free(ent));
  655. smartlist_free(entries);
  656. supported_protocol_list = NULL;
  657. }
  658. }