protover.c 22 KB

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