command.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file command.c
  8. * \brief Functions for processing incoming cells.
  9. *
  10. * When we receive a cell from a client or a relay, it arrives on some
  11. * channel, and tells us what to do with it. In this module, we dispatch based
  12. * on the cell type using the functions command_process_cell() and
  13. * command_process_var_cell(), and deal with the cell accordingly. (These
  14. * handlers are installed on a channel with the command_setup_channel()
  15. * function.)
  16. *
  17. * Channels have a chance to handle some cell types on their own before they
  18. * are ever passed here --- typically, they do this for cells that are
  19. * specific to a given channel type. For example, in channeltls.c, the cells
  20. * for the initial connection handshake are handled before we get here. (Of
  21. * course, the fact that there _is_ only one channel type for now means that
  22. * we may have gotten the factoring wrong here.)
  23. *
  24. * Handling other cell types is mainly farmed off to other modules, after
  25. * initial sanity-checking. CREATE* cells are handled ultimately in onion.c,
  26. * CREATED* cells trigger circuit creation in circuitbuild.c, DESTROY cells
  27. * are handled here (since they're simple), and RELAY cells, in all their
  28. * complexity, are passed off to relay.c.
  29. **/
  30. /* In-points to command.c:
  31. *
  32. * - command_process_cell(), called from
  33. * incoming cell handlers of channel_t instances;
  34. * callbacks registered in command_setup_channel(),
  35. * called when channels are created in circuitbuild.c
  36. */
  37. #include "or.h"
  38. #include "channel.h"
  39. #include "circuitbuild.h"
  40. #include "circuitlist.h"
  41. #include "command.h"
  42. #include "connection.h"
  43. #include "connection_or.h"
  44. #include "config.h"
  45. #include "control.h"
  46. #include "cpuworker.h"
  47. #include "hibernate.h"
  48. #include "nodelist.h"
  49. #include "onion.h"
  50. #include "rephist.h"
  51. #include "relay.h"
  52. #include "router.h"
  53. #include "routerlist.h"
  54. /** How many CELL_CREATE cells have we received, ever? */
  55. uint64_t stats_n_create_cells_processed = 0;
  56. /** How many CELL_CREATED cells have we received, ever? */
  57. uint64_t stats_n_created_cells_processed = 0;
  58. /** How many CELL_RELAY cells have we received, ever? */
  59. uint64_t stats_n_relay_cells_processed = 0;
  60. /** How many CELL_DESTROY cells have we received, ever? */
  61. uint64_t stats_n_destroy_cells_processed = 0;
  62. /* Handle an incoming channel */
  63. static void command_handle_incoming_channel(channel_listener_t *listener,
  64. channel_t *chan);
  65. /* These are the main functions for processing cells */
  66. static void command_process_create_cell(cell_t *cell, channel_t *chan);
  67. static void command_process_created_cell(cell_t *cell, channel_t *chan);
  68. static void command_process_relay_cell(cell_t *cell, channel_t *chan);
  69. static void command_process_destroy_cell(cell_t *cell, channel_t *chan);
  70. /** Convert the cell <b>command</b> into a lower-case, human-readable
  71. * string. */
  72. const char *
  73. cell_command_to_string(uint8_t command)
  74. {
  75. switch (command) {
  76. case CELL_PADDING: return "padding";
  77. case CELL_CREATE: return "create";
  78. case CELL_CREATED: return "created";
  79. case CELL_RELAY: return "relay";
  80. case CELL_DESTROY: return "destroy";
  81. case CELL_CREATE_FAST: return "create_fast";
  82. case CELL_CREATED_FAST: return "created_fast";
  83. case CELL_VERSIONS: return "versions";
  84. case CELL_NETINFO: return "netinfo";
  85. case CELL_RELAY_EARLY: return "relay_early";
  86. case CELL_CREATE2: return "create2";
  87. case CELL_CREATED2: return "created2";
  88. case CELL_VPADDING: return "vpadding";
  89. case CELL_CERTS: return "certs";
  90. case CELL_AUTH_CHALLENGE: return "auth_challenge";
  91. case CELL_AUTHENTICATE: return "authenticate";
  92. case CELL_AUTHORIZE: return "authorize";
  93. default: return "unrecognized";
  94. }
  95. }
  96. #ifdef KEEP_TIMING_STATS
  97. /** This is a wrapper function around the actual function that processes the
  98. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  99. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  100. */
  101. static void
  102. command_time_process_cell(cell_t *cell, channel_t *chan, int *time,
  103. void (*func)(cell_t *, channel_t *))
  104. {
  105. struct timeval start, end;
  106. long time_passed;
  107. tor_gettimeofday(&start);
  108. (*func)(cell, chan);
  109. tor_gettimeofday(&end);
  110. time_passed = tv_udiff(&start, &end) ;
  111. if (time_passed > 10000) { /* more than 10ms */
  112. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  113. }
  114. if (time_passed < 0) {
  115. log_info(LD_GENERAL,"That call took us back in time!");
  116. time_passed = 0;
  117. }
  118. *time += time_passed;
  119. }
  120. #endif
  121. /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
  122. * statistics about how many of each cell we've processed so far
  123. * this second, and the total number of microseconds it took to
  124. * process each type of cell.
  125. */
  126. void
  127. command_process_cell(channel_t *chan, cell_t *cell)
  128. {
  129. #ifdef KEEP_TIMING_STATS
  130. /* how many of each cell have we seen so far this second? needs better
  131. * name. */
  132. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  133. /* how long has it taken to process each type of cell? */
  134. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  135. static time_t current_second = 0; /* from previous calls to time */
  136. time_t now = time(NULL);
  137. if (now > current_second) { /* the second has rolled over */
  138. /* print stats */
  139. log_info(LD_OR,
  140. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  141. "%d relays (%d ms), %d destroys (%d ms)",
  142. num_create, create_time/1000,
  143. num_created, created_time/1000,
  144. num_relay, relay_time/1000,
  145. num_destroy, destroy_time/1000);
  146. /* zero out stats */
  147. num_create = num_created = num_relay = num_destroy = 0;
  148. create_time = created_time = relay_time = destroy_time = 0;
  149. /* remember which second it is, for next time */
  150. current_second = now;
  151. }
  152. #endif
  153. #ifdef KEEP_TIMING_STATS
  154. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  155. ++num ## tp; \
  156. command_time_process_cell(cl, cn, & tp ## time , \
  157. command_process_ ## tp ## _cell); \
  158. } STMT_END
  159. #else
  160. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  161. #endif
  162. switch (cell->command) {
  163. case CELL_CREATE:
  164. case CELL_CREATE_FAST:
  165. case CELL_CREATE2:
  166. ++stats_n_create_cells_processed;
  167. PROCESS_CELL(create, cell, chan);
  168. break;
  169. case CELL_CREATED:
  170. case CELL_CREATED_FAST:
  171. case CELL_CREATED2:
  172. ++stats_n_created_cells_processed;
  173. PROCESS_CELL(created, cell, chan);
  174. break;
  175. case CELL_RELAY:
  176. case CELL_RELAY_EARLY:
  177. ++stats_n_relay_cells_processed;
  178. PROCESS_CELL(relay, cell, chan);
  179. break;
  180. case CELL_DESTROY:
  181. ++stats_n_destroy_cells_processed;
  182. PROCESS_CELL(destroy, cell, chan);
  183. break;
  184. default:
  185. log_fn(LOG_INFO, LD_PROTOCOL,
  186. "Cell of unknown or unexpected type (%d) received. "
  187. "Dropping.",
  188. cell->command);
  189. break;
  190. }
  191. }
  192. /** Process an incoming var_cell from a channel; in the current protocol all
  193. * the var_cells are handshake-related and handled below the channel layer,
  194. * so this just logs a warning and drops the cell.
  195. */
  196. void
  197. command_process_var_cell(channel_t *chan, var_cell_t *var_cell)
  198. {
  199. tor_assert(chan);
  200. tor_assert(var_cell);
  201. log_info(LD_PROTOCOL,
  202. "Received unexpected var_cell above the channel layer of type %d"
  203. "; dropping it.",
  204. var_cell->command);
  205. }
  206. /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
  207. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  208. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  209. * picked up again when the cpuworker finishes decrypting it.
  210. */
  211. static void
  212. command_process_create_cell(cell_t *cell, channel_t *chan)
  213. {
  214. or_circuit_t *circ;
  215. const or_options_t *options = get_options();
  216. int id_is_high;
  217. create_cell_t *create_cell;
  218. tor_assert(cell);
  219. tor_assert(chan);
  220. log_debug(LD_OR,
  221. "Got a CREATE cell for circ_id %u on channel " U64_FORMAT
  222. " (%p)",
  223. (unsigned)cell->circ_id,
  224. U64_PRINTF_ARG(chan->global_identifier), chan);
  225. /* We check for the conditions that would make us drop the cell before
  226. * we check for the conditions that would make us send a DESTROY back,
  227. * since those conditions would make a DESTROY nonsensical. */
  228. if (cell->circ_id == 0) {
  229. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  230. "Received a create cell (type %d) from %s with zero circID; "
  231. " ignoring.", (int)cell->command,
  232. channel_get_actual_remote_descr(chan));
  233. return;
  234. }
  235. if (circuit_id_in_use_on_channel(cell->circ_id, chan)) {
  236. const node_t *node = node_get_by_id(chan->identity_digest);
  237. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  238. "Received CREATE cell (circID %u) for known circ. "
  239. "Dropping (age %d).",
  240. (unsigned)cell->circ_id,
  241. (int)(time(NULL) - channel_when_created(chan)));
  242. if (node) {
  243. char *p = esc_for_log(node_get_platform(node));
  244. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  245. "Details: router %s, platform %s.",
  246. node_describe(node), p);
  247. tor_free(p);
  248. }
  249. return;
  250. }
  251. if (we_are_hibernating()) {
  252. log_info(LD_OR,
  253. "Received create cell but we're shutting down. Sending back "
  254. "destroy.");
  255. channel_send_destroy(cell->circ_id, chan,
  256. END_CIRC_REASON_HIBERNATING);
  257. return;
  258. }
  259. if (!server_mode(options) ||
  260. (!public_server_mode(options) && channel_is_outgoing(chan))) {
  261. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  262. "Received create cell (type %d) from %s, but we're connected "
  263. "to it as a client. "
  264. "Sending back a destroy.",
  265. (int)cell->command, channel_get_canonical_remote_descr(chan));
  266. channel_send_destroy(cell->circ_id, chan,
  267. END_CIRC_REASON_TORPROTOCOL);
  268. return;
  269. }
  270. /* If the high bit of the circuit ID is not as expected, close the
  271. * circ. */
  272. if (chan->wide_circ_ids)
  273. id_is_high = cell->circ_id & (1u<<31);
  274. else
  275. id_is_high = cell->circ_id & (1u<<15);
  276. if ((id_is_high &&
  277. chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  278. (!id_is_high &&
  279. chan->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  280. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  281. "Received create cell with unexpected circ_id %u. Closing.",
  282. (unsigned)cell->circ_id);
  283. channel_send_destroy(cell->circ_id, chan,
  284. END_CIRC_REASON_TORPROTOCOL);
  285. return;
  286. }
  287. circ = or_circuit_new(cell->circ_id, chan);
  288. circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  289. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  290. create_cell = tor_malloc_zero(sizeof(create_cell_t));
  291. if (create_cell_parse(create_cell, cell) < 0) {
  292. tor_free(create_cell);
  293. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  294. "Bogus/unrecognized create cell; closing.");
  295. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
  296. return;
  297. }
  298. if (connection_or_digest_is_known_relay(chan->identity_digest)) {
  299. rep_hist_note_circuit_handshake_requested(create_cell->handshake_type);
  300. // Needed for chutney: Sometimes relays aren't in the consensus yet, and
  301. // get marked as clients. This resets their channels once they appear.
  302. // Probably useful for normal operation wrt relay flapping, too.
  303. channel_clear_client(chan);
  304. } else {
  305. channel_mark_client(chan);
  306. }
  307. if (create_cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST) {
  308. /* hand it off to the cpuworkers, and then return. */
  309. if (assign_onionskin_to_cpuworker(circ, create_cell) < 0) {
  310. log_debug(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  311. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  312. return;
  313. }
  314. log_debug(LD_OR,"success: handed off onionskin.");
  315. } else {
  316. /* This is a CREATE_FAST cell; we can handle it immediately without using
  317. * a CPU worker. */
  318. uint8_t keys[CPATH_KEY_MATERIAL_LEN];
  319. uint8_t rend_circ_nonce[DIGEST_LEN];
  320. int len;
  321. created_cell_t created_cell;
  322. /* If the client used CREATE_FAST, it's probably a tor client or bridge
  323. * relay, and we must not use it for EXTEND requests (in most cases, we
  324. * won't have an authenticated peer ID for the extend).
  325. * Public relays on 0.2.9 and later will use CREATE_FAST if they have no
  326. * ntor onion key for this relay, but that should be a rare occurrence.
  327. * Clients on 0.3.1 and later avoid using CREATE_FAST as much as they can,
  328. * even during bootstrap, so the CREATE_FAST check is most accurate for
  329. * earlier tor client versions. */
  330. channel_mark_client(chan);
  331. memset(&created_cell, 0, sizeof(created_cell));
  332. len = onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST,
  333. create_cell->onionskin,
  334. create_cell->handshake_len,
  335. NULL,
  336. created_cell.reply,
  337. keys, CPATH_KEY_MATERIAL_LEN,
  338. rend_circ_nonce);
  339. tor_free(create_cell);
  340. if (len < 0) {
  341. log_warn(LD_OR,"Failed to generate key material. Closing.");
  342. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  343. return;
  344. }
  345. created_cell.cell_type = CELL_CREATED_FAST;
  346. created_cell.handshake_len = len;
  347. if (onionskin_answer(circ, &created_cell,
  348. (const char *)keys, sizeof(keys),
  349. rend_circ_nonce)<0) {
  350. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  351. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  352. return;
  353. }
  354. memwipe(keys, 0, sizeof(keys));
  355. }
  356. }
  357. /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
  358. * Find the circuit
  359. * that it's intended for. If we're not the origin of the circuit, package
  360. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  361. * are the origin of the circuit, send it to circuit_finish_handshake() to
  362. * finish processing keys, and then call circuit_send_next_onion_skin() to
  363. * extend to the next hop in the circuit if necessary.
  364. */
  365. static void
  366. command_process_created_cell(cell_t *cell, channel_t *chan)
  367. {
  368. circuit_t *circ;
  369. extended_cell_t extended_cell;
  370. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  371. if (!circ) {
  372. log_info(LD_OR,
  373. "(circID %u) unknown circ (probably got a destroy earlier). "
  374. "Dropping.", (unsigned)cell->circ_id);
  375. return;
  376. }
  377. if (circ->n_circ_id != cell->circ_id || circ->n_chan != chan) {
  378. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  379. "got created cell from Tor client? Closing.");
  380. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  381. return;
  382. }
  383. if (created_cell_parse(&extended_cell.created_cell, cell) < 0) {
  384. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unparseable created cell.");
  385. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  386. return;
  387. }
  388. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  389. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  390. int err_reason = 0;
  391. log_debug(LD_OR,"at OP. Finishing handshake.");
  392. if ((err_reason = circuit_finish_handshake(origin_circ,
  393. &extended_cell.created_cell)) < 0) {
  394. circuit_mark_for_close(circ, -err_reason);
  395. return;
  396. }
  397. log_debug(LD_OR,"Moving to next skin.");
  398. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  399. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  400. /* XXX push this circuit_close lower */
  401. circuit_mark_for_close(circ, -err_reason);
  402. return;
  403. }
  404. } else { /* pack it into an extended relay cell, and send it. */
  405. uint8_t command=0;
  406. uint16_t len=0;
  407. uint8_t payload[RELAY_PAYLOAD_SIZE];
  408. log_debug(LD_OR,
  409. "Converting created cell to extended relay cell, sending.");
  410. memset(payload, 0, sizeof(payload));
  411. if (extended_cell.created_cell.cell_type == CELL_CREATED2)
  412. extended_cell.cell_type = RELAY_COMMAND_EXTENDED2;
  413. else
  414. extended_cell.cell_type = RELAY_COMMAND_EXTENDED;
  415. if (extended_cell_format(&command, &len, payload, &extended_cell) < 0) {
  416. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Can't format extended cell.");
  417. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  418. return;
  419. }
  420. relay_send_command_from_edge(0, circ, command,
  421. (const char*)payload, len, NULL);
  422. }
  423. }
  424. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  425. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  426. * circuit_receive_relay_cell() for actual processing.
  427. */
  428. static void
  429. command_process_relay_cell(cell_t *cell, channel_t *chan)
  430. {
  431. const or_options_t *options = get_options();
  432. circuit_t *circ;
  433. int reason, direction;
  434. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  435. if (!circ) {
  436. log_debug(LD_OR,
  437. "unknown circuit %u on connection from %s. Dropping.",
  438. (unsigned)cell->circ_id,
  439. channel_get_canonical_remote_descr(chan));
  440. return;
  441. }
  442. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  443. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  444. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  445. return;
  446. }
  447. if (CIRCUIT_IS_ORIGIN(circ)) {
  448. /* if we're a relay and treating connections with recent local
  449. * traffic better, then this is one of them. */
  450. channel_timestamp_client(chan);
  451. }
  452. if (!CIRCUIT_IS_ORIGIN(circ) &&
  453. chan == TO_OR_CIRCUIT(circ)->p_chan &&
  454. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  455. direction = CELL_DIRECTION_OUT;
  456. else
  457. direction = CELL_DIRECTION_IN;
  458. /* If we have a relay_early cell, make sure that it's outbound, and we've
  459. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  460. if (cell->command == CELL_RELAY_EARLY) {
  461. if (direction == CELL_DIRECTION_IN) {
  462. /* Inbound early cells could once be encountered as a result of
  463. * bug 1038; but relays running versions before 0.2.1.19 are long
  464. * gone from the network, so any such cells now are surprising. */
  465. log_warn(LD_OR,
  466. "Received an inbound RELAY_EARLY cell on circuit %u."
  467. " Closing circuit. Please report this event,"
  468. " along with the following message.",
  469. (unsigned)cell->circ_id);
  470. if (CIRCUIT_IS_ORIGIN(circ)) {
  471. circuit_log_path(LOG_WARN, LD_OR, TO_ORIGIN_CIRCUIT(circ));
  472. } else if (circ->n_chan) {
  473. log_warn(LD_OR, " upstream=%s",
  474. channel_get_actual_remote_descr(circ->n_chan));
  475. }
  476. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  477. return;
  478. } else {
  479. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  480. if (or_circ->remaining_relay_early_cells == 0) {
  481. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  482. "Received too many RELAY_EARLY cells on circ %u from %s."
  483. " Closing circuit.",
  484. (unsigned)cell->circ_id,
  485. safe_str(channel_get_canonical_remote_descr(chan)));
  486. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  487. return;
  488. }
  489. --or_circ->remaining_relay_early_cells;
  490. }
  491. }
  492. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  493. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  494. "(%s) failed. Closing.",
  495. direction==CELL_DIRECTION_OUT?"forward":"backward");
  496. circuit_mark_for_close(circ, -reason);
  497. }
  498. /* If this is a cell in an RP circuit, count it as part of the
  499. hidden service stats */
  500. if (options->HiddenServiceStatistics &&
  501. !CIRCUIT_IS_ORIGIN(circ) &&
  502. TO_OR_CIRCUIT(circ)->circuit_carries_hs_traffic_stats) {
  503. rep_hist_seen_new_rp_cell();
  504. }
  505. }
  506. /** Process a 'destroy' <b>cell</b> that just arrived from
  507. * <b>chan</b>. Find the circ that it refers to (if any).
  508. *
  509. * If the circ is in state
  510. * onionskin_pending, then call onion_pending_remove() to remove it
  511. * from the pending onion list (note that if it's already being
  512. * processed by the cpuworker, it won't be in the list anymore; but
  513. * when the cpuworker returns it, the circuit will be gone, and the
  514. * cpuworker response will be dropped).
  515. *
  516. * Then mark the circuit for close (which marks all edges for close,
  517. * and passes the destroy cell onward if necessary).
  518. */
  519. static void
  520. command_process_destroy_cell(cell_t *cell, channel_t *chan)
  521. {
  522. circuit_t *circ;
  523. int reason;
  524. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  525. if (!circ) {
  526. log_info(LD_OR,"unknown circuit %u on connection from %s. Dropping.",
  527. (unsigned)cell->circ_id,
  528. channel_get_canonical_remote_descr(chan));
  529. return;
  530. }
  531. log_debug(LD_OR,"Received for circID %u.",(unsigned)cell->circ_id);
  532. reason = (uint8_t)cell->payload[0];
  533. circ->received_destroy = 1;
  534. if (!CIRCUIT_IS_ORIGIN(circ) &&
  535. chan == TO_OR_CIRCUIT(circ)->p_chan &&
  536. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  537. /* the destroy came from behind */
  538. circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ), 0, NULL);
  539. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  540. } else { /* the destroy came from ahead */
  541. circuit_set_n_circid_chan(circ, 0, NULL);
  542. if (CIRCUIT_IS_ORIGIN(circ)) {
  543. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  544. } else {
  545. char payload[1];
  546. log_debug(LD_OR, "Delivering 'truncated' back.");
  547. payload[0] = (char)reason;
  548. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  549. payload, sizeof(payload), NULL);
  550. }
  551. }
  552. }
  553. /** Callback to handle a new channel; call command_setup_channel() to give
  554. * it the right cell handlers.
  555. */
  556. static void
  557. command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
  558. {
  559. tor_assert(listener);
  560. tor_assert(chan);
  561. command_setup_channel(chan);
  562. }
  563. /** Given a channel, install the right handlers to process incoming
  564. * cells on it.
  565. */
  566. void
  567. command_setup_channel(channel_t *chan)
  568. {
  569. tor_assert(chan);
  570. channel_set_cell_handlers(chan,
  571. command_process_cell,
  572. command_process_var_cell);
  573. }
  574. /** Given a listener, install the right handler to process incoming
  575. * channels on it.
  576. */
  577. void
  578. command_setup_listener(channel_listener_t *listener)
  579. {
  580. tor_assert(listener);
  581. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
  582. channel_listener_set_listener_fn(listener, command_handle_incoming_channel);
  583. }