command.c 21 KB

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