command.c 23 KB

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