sendme.c 24 KB

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