sendme.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 accept SENDME version %u (from consensus). "
  114. "We only support <= %d. 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 (from consensus). Closing circuit.",
  122. cell_version, accept_version);
  123. goto invalid;
  124. }
  125. return 1;
  126. invalid:
  127. return 0;
  128. }
  129. /* Return true iff the encoded SENDME cell in cell_payload of length
  130. * cell_payload_len is valid. For each version:
  131. *
  132. * 0: No validation
  133. * 1: Authenticated with last cell digest.
  134. *
  135. * This is the main critical function to make sure we can continue to
  136. * send/recv cells on a circuit. If the SENDME is invalid, the circuit should
  137. * be mark for close. */
  138. STATIC bool
  139. sendme_is_valid(const circuit_t *circ, const uint8_t *cell_payload,
  140. size_t cell_payload_len)
  141. {
  142. uint8_t cell_version;
  143. sendme_cell_t *cell = NULL;
  144. tor_assert(circ);
  145. tor_assert(cell_payload);
  146. /* An empty payload means version 0 so skip trunnel parsing. We won't be
  147. * able to parse a 0 length buffer into a valid SENDME cell. */
  148. if (cell_payload_len == 0) {
  149. cell_version = 0;
  150. } else {
  151. /* First we'll decode the cell so we can get the version. */
  152. if (sendme_cell_parse(&cell, cell_payload, cell_payload_len) < 0) {
  153. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  154. "Unparseable SENDME cell received. Closing circuit.");
  155. goto invalid;
  156. }
  157. cell_version = sendme_cell_get_version(cell);
  158. }
  159. /* Validate that we can handle this cell version. */
  160. if (!cell_version_is_valid(cell_version)) {
  161. goto invalid;
  162. }
  163. /* Validate depending on the version now. */
  164. switch (cell_version) {
  165. case 0x01:
  166. if (!cell_v1_is_valid(cell, circ)) {
  167. goto invalid;
  168. }
  169. break;
  170. case 0x00:
  171. /* Fallthrough. Version 0, there is no work to be done on the payload so
  172. * it is necessarily valid if we pass the version validation. */
  173. default:
  174. /* Unknown version means we can't handle it so fallback to version 0. */
  175. break;
  176. }
  177. /* Valid cell. */
  178. sendme_cell_free(cell);
  179. return 1;
  180. invalid:
  181. sendme_cell_free(cell);
  182. return 0;
  183. }
  184. /* Build and encode a version 1 SENDME cell into payload, which must be at
  185. * least of RELAY_PAYLOAD_SIZE bytes, using the digest for the cell data.
  186. *
  187. * Return the size in bytes of the encoded cell in payload. A negative value
  188. * is returned on encoding failure. */
  189. STATIC ssize_t
  190. build_cell_payload_v1(const uint8_t *cell_digest, uint8_t *payload)
  191. {
  192. ssize_t len = -1;
  193. sendme_cell_t *cell = NULL;
  194. tor_assert(cell_digest);
  195. tor_assert(payload);
  196. cell = sendme_cell_new();
  197. /* Building a payload for version 1. */
  198. sendme_cell_set_version(cell, 0x01);
  199. /* Set the data length field for v1. */
  200. sendme_cell_set_data_len(cell, TRUNNEL_SENDME_V1_DIGEST_LEN);
  201. /* Copy the digest into the data payload. */
  202. memcpy(sendme_cell_getarray_data_v1_digest(cell), cell_digest,
  203. sendme_cell_get_data_len(cell));
  204. /* Finally, encode the cell into the payload. */
  205. len = sendme_cell_encode(payload, RELAY_PAYLOAD_SIZE, cell);
  206. sendme_cell_free(cell);
  207. return len;
  208. }
  209. /* Send a circuit-level SENDME on the given circuit using the layer_hint if
  210. * not NULL. The digest is only used for version 1.
  211. *
  212. * Return 0 on success else a negative value and the circuit will be closed
  213. * because we failed to send the cell on it. */
  214. static int
  215. send_circuit_level_sendme(circuit_t *circ, crypt_path_t *layer_hint,
  216. const uint8_t *cell_digest)
  217. {
  218. uint8_t emit_version;
  219. uint8_t payload[RELAY_PAYLOAD_SIZE];
  220. ssize_t payload_len;
  221. tor_assert(circ);
  222. tor_assert(cell_digest);
  223. emit_version = get_emit_min_version();
  224. switch (emit_version) {
  225. case 0x01:
  226. payload_len = build_cell_payload_v1(cell_digest, payload);
  227. if (BUG(payload_len < 0)) {
  228. /* Unable to encode the cell, abort. We can recover from this by closing
  229. * the circuit but in theory it should never happen. */
  230. return -1;
  231. }
  232. log_debug(LD_PROTOCOL, "Emitting SENDME version 1 cell.");
  233. break;
  234. case 0x00:
  235. /* Fallthrough because default is to use v0. */
  236. default:
  237. /* Unknown version, fallback to version 0 meaning no payload. */
  238. payload_len = 0;
  239. break;
  240. }
  241. if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
  242. (char *) payload, payload_len,
  243. layer_hint) < 0) {
  244. log_warn(LD_CIRC,
  245. "SENDME relay_send_command_from_edge failed. Circuit's closed.");
  246. return -1; /* the circuit's closed, don't continue */
  247. }
  248. return 0;
  249. }
  250. /*
  251. * Public API
  252. */
  253. /** Keep the current inbound cell digest for the next SENDME digest. This part
  254. * is only done by the client as the circuit came back from the Exit. */
  255. void
  256. sendme_circuit_record_outbound_cell(or_circuit_t *or_circ)
  257. {
  258. tor_assert(or_circ);
  259. relay_crypto_record_sendme_digest(&or_circ->crypto);
  260. }
  261. /** Keep the current inbound cell digest for the next SENDME digest. This part
  262. * is only done by the client as the circuit came back from the Exit. */
  263. void
  264. sendme_circuit_record_inbound_cell(crypt_path_t *cpath)
  265. {
  266. tor_assert(cpath);
  267. relay_crypto_record_sendme_digest(&cpath->crypto);
  268. }
  269. /** Return true iff the next cell for the given cell window is expected to be
  270. * a SENDME.
  271. *
  272. * We are able to know that because the package or deliver window value minus
  273. * one cell (the possible SENDME cell) should be a multiple of the increment
  274. * window value. */
  275. bool
  276. sendme_circuit_cell_is_next(int window)
  277. {
  278. /* Is this the last cell before a SENDME? The idea is that if the package or
  279. * deliver window reaches a multiple of the increment, after this cell, we
  280. * should expect a SENDME. */
  281. if (((window - 1) % CIRCWINDOW_INCREMENT) != 0) {
  282. return false;
  283. }
  284. /* Next cell is expected to be a SENDME. */
  285. return true;
  286. }
  287. /** Called when we've just received a relay data cell, when we've just
  288. * finished flushing all bytes to stream <b>conn</b>, or when we've flushed
  289. * *some* bytes to the stream <b>conn</b>.
  290. *
  291. * If conn->outbuf is not too full, and our deliver window is low, send back a
  292. * suitable number of stream-level sendme cells.
  293. */
  294. void
  295. sendme_connection_edge_consider_sending(edge_connection_t *conn)
  296. {
  297. tor_assert(conn);
  298. int log_domain = TO_CONN(conn)->type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
  299. /* Don't send it if we still have data to deliver. */
  300. if (connection_outbuf_too_full(TO_CONN(conn))) {
  301. goto end;
  302. }
  303. if (circuit_get_by_edge_conn(conn) == NULL) {
  304. /* This can legitimately happen if the destroy has already arrived and
  305. * torn down the circuit. */
  306. log_info(log_domain, "No circuit associated with edge connection. "
  307. "Skipping sending SENDME.");
  308. goto end;
  309. }
  310. while (conn->deliver_window <=
  311. (STREAMWINDOW_START - STREAMWINDOW_INCREMENT)) {
  312. log_debug(log_domain, "Outbuf %" TOR_PRIuSZ ", queuing stream SENDME.",
  313. TO_CONN(conn)->outbuf_flushlen);
  314. conn->deliver_window += STREAMWINDOW_INCREMENT;
  315. if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
  316. NULL, 0) < 0) {
  317. log_warn(LD_BUG, "connection_edge_send_command failed while sending "
  318. "a SENDME. Circuit probably closed, skipping.");
  319. goto end; /* The circuit's closed, don't continue */
  320. }
  321. }
  322. end:
  323. return;
  324. }
  325. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  326. * <b>layer_hint</b> if it's defined) is low enough that we should
  327. * send a circuit-level sendme back down the circuit. If so, send
  328. * enough sendmes that the window would be overfull if we sent any
  329. * more.
  330. */
  331. void
  332. sendme_circuit_consider_sending(circuit_t *circ, crypt_path_t *layer_hint)
  333. {
  334. const uint8_t *digest;
  335. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <=
  336. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  337. log_debug(LD_CIRC,"Queuing circuit sendme.");
  338. if (layer_hint) {
  339. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  340. digest = relay_crypto_get_sendme_digest(&layer_hint->crypto);
  341. } else {
  342. circ->deliver_window += CIRCWINDOW_INCREMENT;
  343. digest = relay_crypto_get_sendme_digest(&TO_OR_CIRCUIT(circ)->crypto);
  344. }
  345. if (send_circuit_level_sendme(circ, layer_hint, digest) < 0) {
  346. return; /* The circuit's closed, don't continue */
  347. }
  348. }
  349. }
  350. /* Process a circuit-level SENDME cell that we just received. The layer_hint,
  351. * if not NULL, is the Exit hop of the connection which means that we are a
  352. * client. In that case, circ must be an origin circuit. The cell_body_len is
  353. * the length of the SENDME cell payload (excluding the header). The
  354. * cell_payload is the payload.
  355. *
  356. * Return 0 on success that is the SENDME is valid and the package window has
  357. * been updated properly.
  358. *
  359. * On error, a negative value is returned which indicate that the circuit must
  360. * be closed using the value as the reason for it. */
  361. int
  362. sendme_process_circuit_level(crypt_path_t *layer_hint,
  363. circuit_t *circ, const uint8_t *cell_payload,
  364. uint16_t cell_payload_len)
  365. {
  366. tor_assert(circ);
  367. tor_assert(cell_payload);
  368. /* If we are the origin of the circuit, we are the Client so we use the
  369. * layer hint (the Exit hop) for the package window tracking. */
  370. if (CIRCUIT_IS_ORIGIN(circ)) {
  371. if ((layer_hint->package_window + CIRCWINDOW_INCREMENT) >
  372. CIRCWINDOW_START_MAX) {
  373. static struct ratelim_t exit_warn_ratelim = RATELIM_INIT(600);
  374. log_fn_ratelim(&exit_warn_ratelim, LOG_WARN, LD_PROTOCOL,
  375. "Unexpected sendme cell from exit relay. "
  376. "Closing circ.");
  377. return -END_CIRC_REASON_TORPROTOCOL;
  378. }
  379. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  380. log_debug(LD_APP, "circ-level sendme at origin, packagewindow %d.",
  381. layer_hint->package_window);
  382. /* We count circuit-level sendme's as valid delivered data because they
  383. * are rate limited. */
  384. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), cell_payload_len);
  385. } else {
  386. /* Validate the SENDME cell. Depending on the version, different
  387. * validation can be done. An invalid SENDME requires us to close the
  388. * circuit. It is only done if we are the Exit of the circuit. */
  389. if (!sendme_is_valid(circ, cell_payload, cell_payload_len)) {
  390. return -END_CIRC_REASON_TORPROTOCOL;
  391. }
  392. /* We aren't the origin of this circuit so we are the Exit and thus we
  393. * track the package window with the circuit object. */
  394. if ((circ->package_window + CIRCWINDOW_INCREMENT) >
  395. CIRCWINDOW_START_MAX) {
  396. static struct ratelim_t client_warn_ratelim = RATELIM_INIT(600);
  397. log_fn_ratelim(&client_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  398. "Unexpected sendme cell from client. "
  399. "Closing circ (window %d).", circ->package_window);
  400. return -END_CIRC_REASON_TORPROTOCOL;
  401. }
  402. circ->package_window += CIRCWINDOW_INCREMENT;
  403. log_debug(LD_EXIT, "circ-level sendme at non-origin, packagewindow %d.",
  404. circ->package_window);
  405. }
  406. return 0;
  407. }
  408. /* Process a stream-level SENDME cell that we just received. The conn is the
  409. * edge connection (stream) that the circuit circ is associated with. The
  410. * cell_body_len is the length of the payload (excluding the header).
  411. *
  412. * Return 0 on success that is the SENDME is valid and the package window has
  413. * been updated properly.
  414. *
  415. * On error, a negative value is returned which indicate that the circuit must
  416. * be closed using the value as the reason for it. */
  417. int
  418. sendme_process_stream_level(edge_connection_t *conn, circuit_t *circ,
  419. uint16_t cell_body_len)
  420. {
  421. tor_assert(conn);
  422. tor_assert(circ);
  423. /* Don't allow the other endpoint to request more than our maximum (i.e.
  424. * initial) stream SENDME window worth of data. Well-behaved stock clients
  425. * will not request more than this max (as per the check in the while loop
  426. * of sendme_connection_edge_consider_sending()). */
  427. if ((conn->package_window + STREAMWINDOW_INCREMENT) >
  428. STREAMWINDOW_START_MAX) {
  429. static struct ratelim_t stream_warn_ratelim = RATELIM_INIT(600);
  430. log_fn_ratelim(&stream_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  431. "Unexpected stream sendme cell. Closing circ (window %d).",
  432. conn->package_window);
  433. return -END_CIRC_REASON_TORPROTOCOL;
  434. }
  435. /* At this point, the stream sendme is valid */
  436. conn->package_window += STREAMWINDOW_INCREMENT;
  437. /* We count circuit-level sendme's as valid delivered data because they are
  438. * rate limited. */
  439. if (CIRCUIT_IS_ORIGIN(circ)) {
  440. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), cell_body_len);
  441. }
  442. log_debug(CIRCUIT_IS_ORIGIN(circ) ? LD_APP : LD_EXIT,
  443. "stream-level sendme, package_window now %d.",
  444. conn->package_window);
  445. return 0;
  446. }
  447. /* Called when a relay DATA cell is received on the given circuit. If
  448. * layer_hint is NULL, this means we are the Exit end point else we are the
  449. * Client. Update the deliver window and return its new value. */
  450. int
  451. sendme_circuit_data_received(circuit_t *circ, crypt_path_t *layer_hint)
  452. {
  453. int deliver_window, domain;
  454. if (CIRCUIT_IS_ORIGIN(circ)) {
  455. tor_assert(layer_hint);
  456. --layer_hint->deliver_window;
  457. deliver_window = layer_hint->deliver_window;
  458. domain = LD_APP;
  459. } else {
  460. tor_assert(!layer_hint);
  461. --circ->deliver_window;
  462. deliver_window = circ->deliver_window;
  463. domain = LD_EXIT;
  464. }
  465. log_debug(domain, "Circuit deliver_window now %d.", deliver_window);
  466. return deliver_window;
  467. }
  468. /* Called when a relay DATA cell is received for the given edge connection
  469. * conn. Update the deliver window and return its new value. */
  470. int
  471. sendme_stream_data_received(edge_connection_t *conn)
  472. {
  473. tor_assert(conn);
  474. return --conn->deliver_window;
  475. }
  476. /* Called when a relay DATA cell is packaged on the given circuit. If
  477. * layer_hint is NULL, this means we are the Exit end point else we are the
  478. * Client. Update the package window and return its new value. */
  479. int
  480. sendme_note_circuit_data_packaged(circuit_t *circ, crypt_path_t *layer_hint)
  481. {
  482. int package_window, domain;
  483. tor_assert(circ);
  484. if (CIRCUIT_IS_ORIGIN(circ)) {
  485. /* Client side. */
  486. tor_assert(layer_hint);
  487. --layer_hint->package_window;
  488. package_window = layer_hint->package_window;
  489. domain = LD_APP;
  490. } else {
  491. /* Exit side. */
  492. tor_assert(!layer_hint);
  493. --circ->package_window;
  494. package_window = circ->package_window;
  495. domain = LD_EXIT;
  496. }
  497. log_debug(domain, "Circuit package_window now %d.", package_window);
  498. return package_window;
  499. }
  500. /* Called when a relay DATA cell is packaged for the given edge connection
  501. * conn. Update the package window and return its new value. */
  502. int
  503. sendme_note_stream_data_packaged(edge_connection_t *conn)
  504. {
  505. tor_assert(conn);
  506. return --conn->package_window;
  507. }
  508. /* Note the cell digest in the circuit sendme last digests FIFO if applicable.
  509. * It is safe to pass a circuit that isn't meant to track those digests. */
  510. void
  511. sendme_record_cell_digest(circuit_t *circ)
  512. {
  513. const uint8_t *digest;
  514. tor_assert(circ);
  515. /* We only keep the cell digest if we are the Exit on that circuit and if
  516. * this cell is the last one before the client should send a SENDME. */
  517. if (CIRCUIT_IS_ORIGIN(circ)) {
  518. return;
  519. }
  520. /* Is this the last cell before a SENDME? The idea is that if the
  521. * package_window reaches a multiple of the increment, after this cell, we
  522. * should expect a SENDME. */
  523. if (!sendme_circuit_cell_is_next(circ->package_window)) {
  524. return;
  525. }
  526. /* Add the digest to the last seen list in the circuit. */
  527. digest = relay_crypto_get_sendme_digest(&TO_OR_CIRCUIT(circ)->crypto);
  528. if (circ->sendme_last_digests == NULL) {
  529. circ->sendme_last_digests = smartlist_new();
  530. }
  531. smartlist_add(circ->sendme_last_digests, tor_memdup(digest, DIGEST_LEN));
  532. }