sendme.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* Copyright (c) 2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file sendme.c
  5. * \brief Code that is related to SENDME cells both in terms of
  6. * creating/parsing cells and handling the content.
  7. */
  8. #define SENDME_PRIVATE
  9. #include "core/or/or.h"
  10. #include "app/config/config.h"
  11. #include "core/crypto/relay_crypto.h"
  12. #include "core/mainloop/connection.h"
  13. #include "core/or/cell_st.h"
  14. #include "core/or/circuitlist.h"
  15. #include "core/or/circuituse.h"
  16. #include "core/or/or_circuit_st.h"
  17. #include "core/or/relay.h"
  18. #include "core/or/sendme.h"
  19. #include "feature/nodelist/networkstatus.h"
  20. #include "lib/ctime/di_ops.h"
  21. #include "trunnel/sendme.h"
  22. /* The maximum supported version. Above that value, the cell can't be
  23. * recognized as a valid SENDME. */
  24. #define SENDME_MAX_SUPPORTED_VERSION 1
  25. /* The cell version constants for when emitting a cell. */
  26. #define SENDME_EMIT_MIN_VERSION_DEFAULT 0
  27. #define SENDME_EMIT_MIN_VERSION_MIN 0
  28. #define SENDME_EMIT_MIN_VERSION_MAX UINT8_MAX
  29. /* The cell version constants for when accepting a cell. */
  30. #define SENDME_ACCEPT_MIN_VERSION_DEFAULT 0
  31. #define SENDME_ACCEPT_MIN_VERSION_MIN 0
  32. #define SENDME_ACCEPT_MIN_VERSION_MAX UINT8_MAX
  33. /* Return the minimum version given by the consensus (if any) that should be
  34. * used when emitting a SENDME cell. */
  35. STATIC int
  36. get_emit_min_version(void)
  37. {
  38. return networkstatus_get_param(NULL, "sendme_emit_min_version",
  39. SENDME_EMIT_MIN_VERSION_DEFAULT,
  40. SENDME_EMIT_MIN_VERSION_MIN,
  41. SENDME_EMIT_MIN_VERSION_MAX);
  42. }
  43. /* Return the minimum version given by the consensus (if any) that should be
  44. * accepted when receiving a SENDME cell. */
  45. STATIC int
  46. get_accept_min_version(void)
  47. {
  48. return networkstatus_get_param(NULL, "sendme_accept_min_version",
  49. SENDME_ACCEPT_MIN_VERSION_DEFAULT,
  50. SENDME_ACCEPT_MIN_VERSION_MIN,
  51. SENDME_ACCEPT_MIN_VERSION_MAX);
  52. }
  53. /* Return true iff the given cell digest matches the first digest in the
  54. * circuit sendme list. */
  55. static bool
  56. v1_digest_matches(const circuit_t *circ, const uint8_t *cell_digest)
  57. {
  58. bool ret = false;
  59. uint8_t *circ_digest = NULL;
  60. tor_assert(circ);
  61. tor_assert(cell_digest);
  62. /* We shouldn't have received a SENDME if we have no digests. Log at
  63. * protocol warning because it can be tricked by sending many SENDMEs
  64. * without prior data cell. */
  65. if (circ->sendme_last_digests == NULL ||
  66. smartlist_len(circ->sendme_last_digests) == 0) {
  67. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  68. "We received a SENDME but we have no cell digests to match. "
  69. "Closing circuit.");
  70. goto no_match;
  71. }
  72. /* Pop the first element that was added (FIFO) and compare it. */
  73. circ_digest = smartlist_get(circ->sendme_last_digests, 0);
  74. smartlist_del_keeporder(circ->sendme_last_digests, 0);
  75. /* Compare the digest with the one in the SENDME. This cell is invalid
  76. * without a perfect match. */
  77. if (tor_memneq(circ_digest, cell_digest, TRUNNEL_SENDME_V1_DIGEST_LEN)) {
  78. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  79. "SENDME v1 cell digest do not match.");
  80. goto no_match;
  81. }
  82. /* Digests matches! */
  83. ret = true;
  84. no_match:
  85. /* This digest was popped from the circuit list. Regardless of what happens,
  86. * we have no more use for it. */
  87. tor_free(circ_digest);
  88. return ret;
  89. }
  90. /* Return true iff the given decoded SENDME version 1 cell is valid and
  91. * matches the expected digest on the circuit.
  92. *
  93. * Validation is done by comparing the digest in the cell from the previous
  94. * cell we saw which tells us that the other side has in fact seen that cell.
  95. * See proposal 289 for more details. */
  96. static bool
  97. cell_v1_is_valid(const sendme_cell_t *cell, const circuit_t *circ)
  98. {
  99. tor_assert(cell);
  100. tor_assert(circ);
  101. const uint8_t *cell_digest = sendme_cell_getconstarray_data_v1_digest(cell);
  102. return v1_digest_matches(circ, cell_digest);
  103. }
  104. /* Return true iff the given cell version can be handled or if the minimum
  105. * accepted version from the consensus is known to us. */
  106. STATIC bool
  107. cell_version_is_valid(uint8_t cell_version)
  108. {
  109. int accept_version = get_accept_min_version();
  110. /* Can we handle this version? */
  111. if (accept_version > SENDME_MAX_SUPPORTED_VERSION) {
  112. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  113. "Unable to handle SENDME version %u. We only support <= %d "
  114. "(from consensus). Probably your tor is too old?",
  115. accept_version, cell_version);
  116. goto invalid;
  117. }
  118. /* We only accept a SENDME cell from what the consensus tells us. */
  119. if (cell_version < accept_version) {
  120. log_info(LD_PROTOCOL, "Unacceptable SENDME version %d. Only "
  121. "accepting %u (taken from the consensus). "
  122. "Closing circuit.",
  123. cell_version, accept_version);
  124. goto invalid;
  125. }
  126. return 1;
  127. invalid:
  128. return 0;
  129. }
  130. /* Return true iff the encoded SENDME cell in cell_payload of length
  131. * cell_payload_len is valid. For each version:
  132. *
  133. * 0: No validation
  134. * 1: Authenticated with last cell digest.
  135. *
  136. * This is the main critical function to make sure we can continue to
  137. * send/recv cells on a circuit. If the SENDME is invalid, the circuit should
  138. * be mark for close. */
  139. STATIC bool
  140. sendme_is_valid(const circuit_t *circ, const uint8_t *cell_payload,
  141. size_t cell_payload_len)
  142. {
  143. uint8_t cell_version;
  144. sendme_cell_t *cell = NULL;
  145. tor_assert(circ);
  146. tor_assert(cell_payload);
  147. /* An empty payload means version 0 so skip trunnel parsing. We won't be
  148. * able to parse a 0 length buffer into a valid SENDME cell. */
  149. if (cell_payload_len == 0) {
  150. cell_version = 0;
  151. } else {
  152. /* First we'll decode the cell so we can get the version. */
  153. if (sendme_cell_parse(&cell, cell_payload, cell_payload_len) < 0) {
  154. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  155. "Unparseable SENDME cell received. Closing circuit.");
  156. goto invalid;
  157. }
  158. cell_version = sendme_cell_get_version(cell);
  159. }
  160. /* Validate that we can handle this cell version. */
  161. if (!cell_version_is_valid(cell_version)) {
  162. goto invalid;
  163. }
  164. /* Validate depending on the version now. */
  165. switch (cell_version) {
  166. case 0x01:
  167. if (!cell_v1_is_valid(cell, circ)) {
  168. goto invalid;
  169. }
  170. break;
  171. case 0x00:
  172. /* Fallthrough. Version 0, there is no work to be done on the payload so
  173. * it is necessarily valid if we pass the version validation. */
  174. default:
  175. /* Unknown version means we can't handle it so fallback to version 0. */
  176. break;
  177. }
  178. /* Valid cell. */
  179. sendme_cell_free(cell);
  180. return 1;
  181. invalid:
  182. sendme_cell_free(cell);
  183. return 0;
  184. }
  185. /* Build and encode a version 1 SENDME cell into payload, which must be at
  186. * least of RELAY_PAYLOAD_SIZE bytes, using the digest for the cell data.
  187. *
  188. * Return the size in bytes of the encoded cell in payload. A negative value
  189. * is returned on encoding failure. */
  190. STATIC ssize_t
  191. build_cell_payload_v1(const uint8_t *cell_digest, uint8_t *payload)
  192. {
  193. ssize_t len = -1;
  194. sendme_cell_t *cell = NULL;
  195. tor_assert(cell_digest);
  196. tor_assert(payload);
  197. cell = sendme_cell_new();
  198. /* Building a payload for version 1. */
  199. sendme_cell_set_version(cell, 0x01);
  200. /* Set the data length field for v1. */
  201. sendme_cell_set_data_len(cell, TRUNNEL_SENDME_V1_DIGEST_LEN);
  202. /* Copy the digest into the data payload. */
  203. memcpy(sendme_cell_getarray_data_v1_digest(cell), cell_digest,
  204. sendme_cell_get_data_len(cell));
  205. /* Finally, encode the cell into the payload. */
  206. len = sendme_cell_encode(payload, RELAY_PAYLOAD_SIZE, cell);
  207. sendme_cell_free(cell);
  208. return len;
  209. }
  210. /* Send a circuit-level SENDME on the given circuit using the layer_hint if
  211. * not NULL. The digest is only used for version 1.
  212. *
  213. * Return 0 on success else a negative value and the circuit will be closed
  214. * because we failed to send the cell on it. */
  215. static int
  216. send_circuit_level_sendme(circuit_t *circ, crypt_path_t *layer_hint,
  217. const uint8_t *cell_digest)
  218. {
  219. uint8_t emit_version;
  220. uint8_t payload[RELAY_PAYLOAD_SIZE];
  221. ssize_t payload_len;
  222. tor_assert(circ);
  223. tor_assert(cell_digest);
  224. emit_version = get_emit_min_version();
  225. switch (emit_version) {
  226. case 0x01:
  227. payload_len = build_cell_payload_v1(cell_digest, payload);
  228. if (BUG(payload_len < 0)) {
  229. /* Unable to encode the cell, abort. We can recover from this by closing
  230. * the circuit but in theory it should never happen. */
  231. return -1;
  232. }
  233. log_debug(LD_PROTOCOL, "Emitting SENDME version 1 cell.");
  234. break;
  235. case 0x00:
  236. /* Fallthrough because default is to use v0. */
  237. default:
  238. /* Unknown version, fallback to version 0 meaning no payload. */
  239. payload_len = 0;
  240. break;
  241. }
  242. if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
  243. (char *) payload, payload_len,
  244. layer_hint) < 0) {
  245. log_warn(LD_CIRC,
  246. "SENDME relay_send_command_from_edge failed. Circuit's closed.");
  247. return -1; /* the circuit's closed, don't continue */
  248. }
  249. return 0;
  250. }
  251. /*
  252. * Public API
  253. */
  254. /** Keep the current inbound cell digest for the next SENDME digest. This part
  255. * is only done by the client as the circuit came back from the Exit. */
  256. void
  257. sendme_circuit_record_outbound_cell(or_circuit_t *or_circ)
  258. {
  259. tor_assert(or_circ);
  260. relay_crypto_record_sendme_digest(&or_circ->crypto);
  261. }
  262. /** Keep the current inbound cell digest for the next SENDME digest. This part
  263. * is only done by the client as the circuit came back from the Exit. */
  264. void
  265. sendme_circuit_record_inbound_cell(crypt_path_t *cpath)
  266. {
  267. tor_assert(cpath);
  268. relay_crypto_record_sendme_digest(&cpath->crypto);
  269. }
  270. /** Return true iff the next cell for the given cell window is expected to be
  271. * a SENDME.
  272. *
  273. * We are able to know that because the package or deliver window value minus
  274. * one cell (the possible SENDME cell) should be a multiple of the increment
  275. * window value. */
  276. bool
  277. sendme_circuit_cell_is_next(int window)
  278. {
  279. /* Is this the last cell before a SENDME? The idea is that if the package or
  280. * deliver window reaches a multiple of the increment, after this cell, we
  281. * should expect a SENDME. */
  282. if (((window - 1) % CIRCWINDOW_INCREMENT) != 0) {
  283. return false;
  284. }
  285. /* Next cell is expected to be a SENDME. */
  286. return true;
  287. }
  288. /** Called when we've just received a relay data cell, when we've just
  289. * finished flushing all bytes to stream <b>conn</b>, or when we've flushed
  290. * *some* bytes to the stream <b>conn</b>.
  291. *
  292. * If conn->outbuf is not too full, and our deliver window is low, send back a
  293. * suitable number of stream-level sendme cells.
  294. */
  295. void
  296. sendme_connection_edge_consider_sending(edge_connection_t *conn)
  297. {
  298. tor_assert(conn);
  299. int log_domain = TO_CONN(conn)->type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
  300. /* Don't send it if we still have data to deliver. */
  301. if (connection_outbuf_too_full(TO_CONN(conn))) {
  302. goto end;
  303. }
  304. if (circuit_get_by_edge_conn(conn) == NULL) {
  305. /* This can legitimately happen if the destroy has already arrived and
  306. * torn down the circuit. */
  307. log_info(log_domain, "No circuit associated with edge connection. "
  308. "Skipping sending SENDME.");
  309. goto end;
  310. }
  311. while (conn->deliver_window <=
  312. (STREAMWINDOW_START - STREAMWINDOW_INCREMENT)) {
  313. log_debug(log_domain, "Outbuf %" TOR_PRIuSZ ", queuing stream SENDME.",
  314. TO_CONN(conn)->outbuf_flushlen);
  315. conn->deliver_window += STREAMWINDOW_INCREMENT;
  316. if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
  317. NULL, 0) < 0) {
  318. log_warn(LD_BUG, "connection_edge_send_command failed while sending "
  319. "a SENDME. Circuit probably closed, skipping.");
  320. goto end; /* The circuit's closed, don't continue */
  321. }
  322. }
  323. end:
  324. return;
  325. }
  326. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  327. * <b>layer_hint</b> if it's defined) is low enough that we should
  328. * send a circuit-level sendme back down the circuit. If so, send
  329. * enough sendmes that the window would be overfull if we sent any
  330. * more.
  331. */
  332. void
  333. sendme_circuit_consider_sending(circuit_t *circ, crypt_path_t *layer_hint)
  334. {
  335. const uint8_t *digest;
  336. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <=
  337. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  338. log_debug(LD_CIRC,"Queuing circuit sendme.");
  339. if (layer_hint) {
  340. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  341. digest = layer_hint->crypto.sendme_digest;
  342. } else {
  343. circ->deliver_window += CIRCWINDOW_INCREMENT;
  344. digest = TO_OR_CIRCUIT(circ)->crypto.sendme_digest;
  345. }
  346. if (send_circuit_level_sendme(circ, layer_hint, digest) < 0) {
  347. return; /* The circuit's closed, don't continue */
  348. }
  349. }
  350. }
  351. /* Process a circuit-level SENDME cell that we just received. The layer_hint,
  352. * if not NULL, is the Exit hop of the connection which means that we are a
  353. * client. In that case, circ must be an origin circuit. The cell_body_len is
  354. * the length of the SENDME cell payload (excluding the header). The
  355. * cell_payload is the payload.
  356. *
  357. * Return 0 on success that is the SENDME is valid and the package window has
  358. * been updated properly.
  359. *
  360. * On error, a negative value is returned which indicate that the circuit must
  361. * be closed using the value as the reason for it. */
  362. int
  363. sendme_process_circuit_level(crypt_path_t *layer_hint,
  364. circuit_t *circ, const uint8_t *cell_payload,
  365. uint16_t cell_payload_len)
  366. {
  367. tor_assert(circ);
  368. tor_assert(cell_payload);
  369. /* If we are the origin of the circuit, we are the Client so we use the
  370. * layer hint (the Exit hop) for the package window tracking. */
  371. if (CIRCUIT_IS_ORIGIN(circ)) {
  372. if ((layer_hint->package_window + CIRCWINDOW_INCREMENT) >
  373. CIRCWINDOW_START_MAX) {
  374. static struct ratelim_t exit_warn_ratelim = RATELIM_INIT(600);
  375. log_fn_ratelim(&exit_warn_ratelim, LOG_WARN, LD_PROTOCOL,
  376. "Unexpected sendme cell from exit relay. "
  377. "Closing circ.");
  378. return -END_CIRC_REASON_TORPROTOCOL;
  379. }
  380. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  381. log_debug(LD_APP, "circ-level sendme at origin, packagewindow %d.",
  382. layer_hint->package_window);
  383. /* We count circuit-level sendme's as valid delivered data because they
  384. * are rate limited. */
  385. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), cell_payload_len);
  386. } else {
  387. /* Validate the SENDME cell. Depending on the version, different
  388. * validation can be done. An invalid SENDME requires us to close the
  389. * circuit. It is only done if we are the Exit of the circuit. */
  390. if (!sendme_is_valid(circ, cell_payload, cell_payload_len)) {
  391. return -END_CIRC_REASON_TORPROTOCOL;
  392. }
  393. /* We aren't the origin of this circuit so we are the Exit and thus we
  394. * track the package window with the circuit object. */
  395. if ((circ->package_window + CIRCWINDOW_INCREMENT) >
  396. CIRCWINDOW_START_MAX) {
  397. static struct ratelim_t client_warn_ratelim = RATELIM_INIT(600);
  398. log_fn_ratelim(&client_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  399. "Unexpected sendme cell from client. "
  400. "Closing circ (window %d).", circ->package_window);
  401. return -END_CIRC_REASON_TORPROTOCOL;
  402. }
  403. circ->package_window += CIRCWINDOW_INCREMENT;
  404. log_debug(LD_EXIT, "circ-level sendme at non-origin, packagewindow %d.",
  405. circ->package_window);
  406. }
  407. return 0;
  408. }
  409. /* Process a stream-level SENDME cell that we just received. The conn is the
  410. * edge connection (stream) that the circuit circ is associated with. The
  411. * cell_body_len is the length of the payload (excluding the header).
  412. *
  413. * Return 0 on success that is the SENDME is valid and the package window has
  414. * been updated properly.
  415. *
  416. * On error, a negative value is returned which indicate that the circuit must
  417. * be closed using the value as the reason for it. */
  418. int
  419. sendme_process_stream_level(edge_connection_t *conn, circuit_t *circ,
  420. uint16_t cell_body_len)
  421. {
  422. tor_assert(conn);
  423. tor_assert(circ);
  424. /* Don't allow the other endpoint to request more than our maximum (i.e.
  425. * initial) stream SENDME window worth of data. Well-behaved stock clients
  426. * will not request more than this max (as per the check in the while loop
  427. * of sendme_connection_edge_consider_sending()). */
  428. if ((conn->package_window + STREAMWINDOW_INCREMENT) >
  429. STREAMWINDOW_START_MAX) {
  430. static struct ratelim_t stream_warn_ratelim = RATELIM_INIT(600);
  431. log_fn_ratelim(&stream_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  432. "Unexpected stream sendme cell. Closing circ (window %d).",
  433. conn->package_window);
  434. return -END_CIRC_REASON_TORPROTOCOL;
  435. }
  436. /* At this point, the stream sendme is valid */
  437. conn->package_window += STREAMWINDOW_INCREMENT;
  438. /* We count circuit-level sendme's as valid delivered data because they are
  439. * rate limited. */
  440. if (CIRCUIT_IS_ORIGIN(circ)) {
  441. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), cell_body_len);
  442. }
  443. log_debug(CIRCUIT_IS_ORIGIN(circ) ? LD_APP : LD_EXIT,
  444. "stream-level sendme, package_window now %d.",
  445. conn->package_window);
  446. return 0;
  447. }
  448. /* Called when a relay DATA cell is received on the given circuit. If
  449. * layer_hint is NULL, this means we are the Exit end point else we are the
  450. * Client. Update the deliver window and return its new value. */
  451. int
  452. sendme_circuit_data_received(circuit_t *circ, crypt_path_t *layer_hint)
  453. {
  454. int deliver_window, domain;
  455. if (CIRCUIT_IS_ORIGIN(circ)) {
  456. tor_assert(layer_hint);
  457. --layer_hint->deliver_window;
  458. deliver_window = layer_hint->deliver_window;
  459. domain = LD_APP;
  460. } else {
  461. tor_assert(!layer_hint);
  462. --circ->deliver_window;
  463. deliver_window = circ->deliver_window;
  464. domain = LD_EXIT;
  465. }
  466. log_debug(domain, "Circuit deliver_window now %d.", deliver_window);
  467. return deliver_window;
  468. }
  469. /* Called when a relay DATA cell is received for the given edge connection
  470. * conn. Update the deliver window and return its new value. */
  471. int
  472. sendme_stream_data_received(edge_connection_t *conn)
  473. {
  474. tor_assert(conn);
  475. return --conn->deliver_window;
  476. }
  477. /* Called when a relay DATA cell is packaged on the given circuit. If
  478. * layer_hint is NULL, this means we are the Exit end point else we are the
  479. * Client. Update the package window and return its new value. */
  480. int
  481. sendme_note_circuit_data_packaged(circuit_t *circ, crypt_path_t *layer_hint)
  482. {
  483. int package_window, domain;
  484. tor_assert(circ);
  485. if (CIRCUIT_IS_ORIGIN(circ)) {
  486. /* Client side. */
  487. tor_assert(layer_hint);
  488. --layer_hint->package_window;
  489. package_window = layer_hint->package_window;
  490. domain = LD_APP;
  491. } else {
  492. /* Exit side. */
  493. tor_assert(!layer_hint);
  494. --circ->package_window;
  495. package_window = circ->package_window;
  496. domain = LD_EXIT;
  497. }
  498. log_debug(domain, "Circuit package_window now %d.", package_window);
  499. return package_window;
  500. }
  501. /* Called when a relay DATA cell is packaged for the given edge connection
  502. * conn. Update the package window and return its new value. */
  503. int
  504. sendme_note_stream_data_packaged(edge_connection_t *conn)
  505. {
  506. tor_assert(conn);
  507. return --conn->package_window;
  508. }
  509. /* Note the cell digest in the circuit sendme last digests FIFO if applicable.
  510. * It is safe to pass a circuit that isn't meant to track those digests. */
  511. void
  512. sendme_record_cell_digest(circuit_t *circ)
  513. {
  514. const uint8_t *digest;
  515. tor_assert(circ);
  516. /* We only keep the cell digest if we are the Exit on that circuit and if
  517. * this cell is the last one before the client should send a SENDME. */
  518. if (CIRCUIT_IS_ORIGIN(circ)) {
  519. return;
  520. }
  521. /* Is this the last cell before a SENDME? The idea is that if the
  522. * package_window reaches a multiple of the increment, after this cell, we
  523. * should expect a SENDME. */
  524. if (!sendme_circuit_cell_is_next(circ->package_window)) {
  525. return;
  526. }
  527. /* Add the digest to the last seen list in the circuit. */
  528. digest = TO_OR_CIRCUIT(circ)->crypto.sendme_digest;
  529. if (circ->sendme_last_digests == NULL) {
  530. circ->sendme_last_digests = smartlist_new();
  531. }
  532. smartlist_add(circ->sendme_last_digests, tor_memdup(digest, DIGEST_LEN));
  533. }