protover.c 22 KB

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