command.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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-2016, 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. return;
  311. }
  312. created_cell.cell_type = CELL_CREATED_FAST;
  313. created_cell.handshake_len = len;
  314. if (onionskin_answer(circ, &created_cell,
  315. (const char *)keys, rend_circ_nonce)<0) {
  316. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  317. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  318. return;
  319. }
  320. memwipe(keys, 0, sizeof(keys));
  321. }
  322. }
  323. /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
  324. * Find the circuit
  325. * that it's intended for. If we're not the origin of the circuit, package
  326. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  327. * are the origin of the circuit, send it to circuit_finish_handshake() to
  328. * finish processing keys, and then call circuit_send_next_onion_skin() to
  329. * extend to the next hop in the circuit if necessary.
  330. */
  331. static void
  332. command_process_created_cell(cell_t *cell, channel_t *chan)
  333. {
  334. circuit_t *circ;
  335. extended_cell_t extended_cell;
  336. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  337. if (!circ) {
  338. log_info(LD_OR,
  339. "(circID %u) unknown circ (probably got a destroy earlier). "
  340. "Dropping.", (unsigned)cell->circ_id);
  341. return;
  342. }
  343. if (circ->n_circ_id != cell->circ_id || circ->n_chan != chan) {
  344. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  345. "got created cell from Tor client? Closing.");
  346. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  347. return;
  348. }
  349. if (created_cell_parse(&extended_cell.created_cell, cell) < 0) {
  350. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unparseable created cell.");
  351. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  352. return;
  353. }
  354. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  355. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  356. int err_reason = 0;
  357. log_debug(LD_OR,"at OP. Finishing handshake.");
  358. if ((err_reason = circuit_finish_handshake(origin_circ,
  359. &extended_cell.created_cell)) < 0) {
  360. circuit_mark_for_close(circ, -err_reason);
  361. return;
  362. }
  363. log_debug(LD_OR,"Moving to next skin.");
  364. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  365. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  366. /* XXX push this circuit_close lower */
  367. circuit_mark_for_close(circ, -err_reason);
  368. return;
  369. }
  370. } else { /* pack it into an extended relay cell, and send it. */
  371. uint8_t command=0;
  372. uint16_t len=0;
  373. uint8_t payload[RELAY_PAYLOAD_SIZE];
  374. log_debug(LD_OR,
  375. "Converting created cell to extended relay cell, sending.");
  376. memset(payload, 0, sizeof(payload));
  377. if (extended_cell.created_cell.cell_type == CELL_CREATED2)
  378. extended_cell.cell_type = RELAY_COMMAND_EXTENDED2;
  379. else
  380. extended_cell.cell_type = RELAY_COMMAND_EXTENDED;
  381. if (extended_cell_format(&command, &len, payload, &extended_cell) < 0) {
  382. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Can't format extended cell.");
  383. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  384. return;
  385. }
  386. relay_send_command_from_edge(0, circ, command,
  387. (const char*)payload, len, NULL);
  388. }
  389. }
  390. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  391. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  392. * circuit_receive_relay_cell() for actual processing.
  393. */
  394. static void
  395. command_process_relay_cell(cell_t *cell, channel_t *chan)
  396. {
  397. const or_options_t *options = get_options();
  398. circuit_t *circ;
  399. int reason, direction;
  400. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  401. if (!circ) {
  402. log_debug(LD_OR,
  403. "unknown circuit %u on connection from %s. Dropping.",
  404. (unsigned)cell->circ_id,
  405. channel_get_canonical_remote_descr(chan));
  406. return;
  407. }
  408. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  409. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  410. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  411. return;
  412. }
  413. if (CIRCUIT_IS_ORIGIN(circ)) {
  414. /* if we're a relay and treating connections with recent local
  415. * traffic better, then this is one of them. */
  416. channel_timestamp_client(chan);
  417. }
  418. if (!CIRCUIT_IS_ORIGIN(circ) &&
  419. chan == TO_OR_CIRCUIT(circ)->p_chan &&
  420. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  421. direction = CELL_DIRECTION_OUT;
  422. else
  423. direction = CELL_DIRECTION_IN;
  424. /* If we have a relay_early cell, make sure that it's outbound, and we've
  425. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  426. if (cell->command == CELL_RELAY_EARLY) {
  427. if (direction == CELL_DIRECTION_IN) {
  428. /* Inbound early cells could once be encountered as a result of
  429. * bug 1038; but relays running versions before 0.2.1.19 are long
  430. * gone from the network, so any such cells now are surprising. */
  431. log_warn(LD_OR,
  432. "Received an inbound RELAY_EARLY cell on circuit %u."
  433. " Closing circuit. Please report this event,"
  434. " along with the following message.",
  435. (unsigned)cell->circ_id);
  436. if (CIRCUIT_IS_ORIGIN(circ)) {
  437. circuit_log_path(LOG_WARN, LD_OR, TO_ORIGIN_CIRCUIT(circ));
  438. } else if (circ->n_chan) {
  439. log_warn(LD_OR, " upstream=%s",
  440. channel_get_actual_remote_descr(circ->n_chan));
  441. }
  442. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  443. return;
  444. } else {
  445. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  446. if (or_circ->remaining_relay_early_cells == 0) {
  447. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  448. "Received too many RELAY_EARLY cells on circ %u from %s."
  449. " Closing circuit.",
  450. (unsigned)cell->circ_id,
  451. safe_str(channel_get_canonical_remote_descr(chan)));
  452. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  453. return;
  454. }
  455. --or_circ->remaining_relay_early_cells;
  456. }
  457. }
  458. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  459. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  460. "(%s) failed. Closing.",
  461. direction==CELL_DIRECTION_OUT?"forward":"backward");
  462. circuit_mark_for_close(circ, -reason);
  463. }
  464. /* If this is a cell in an RP circuit, count it as part of the
  465. hidden service stats */
  466. if (options->HiddenServiceStatistics &&
  467. !CIRCUIT_IS_ORIGIN(circ) &&
  468. TO_OR_CIRCUIT(circ)->circuit_carries_hs_traffic_stats) {
  469. rep_hist_seen_new_rp_cell();
  470. }
  471. }
  472. /** Process a 'destroy' <b>cell</b> that just arrived from
  473. * <b>chan</b>. Find the circ that it refers to (if any).
  474. *
  475. * If the circ is in state
  476. * onionskin_pending, then call onion_pending_remove() to remove it
  477. * from the pending onion list (note that if it's already being
  478. * processed by the cpuworker, it won't be in the list anymore; but
  479. * when the cpuworker returns it, the circuit will be gone, and the
  480. * cpuworker response will be dropped).
  481. *
  482. * Then mark the circuit for close (which marks all edges for close,
  483. * and passes the destroy cell onward if necessary).
  484. */
  485. static void
  486. command_process_destroy_cell(cell_t *cell, channel_t *chan)
  487. {
  488. circuit_t *circ;
  489. int reason;
  490. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  491. if (!circ) {
  492. log_info(LD_OR,"unknown circuit %u on connection from %s. Dropping.",
  493. (unsigned)cell->circ_id,
  494. channel_get_canonical_remote_descr(chan));
  495. return;
  496. }
  497. log_debug(LD_OR,"Received for circID %u.",(unsigned)cell->circ_id);
  498. reason = (uint8_t)cell->payload[0];
  499. circ->received_destroy = 1;
  500. if (!CIRCUIT_IS_ORIGIN(circ) &&
  501. chan == TO_OR_CIRCUIT(circ)->p_chan &&
  502. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  503. /* the destroy came from behind */
  504. circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ), 0, NULL);
  505. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  506. } else { /* the destroy came from ahead */
  507. circuit_set_n_circid_chan(circ, 0, NULL);
  508. if (CIRCUIT_IS_ORIGIN(circ)) {
  509. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  510. } else {
  511. char payload[1];
  512. log_debug(LD_OR, "Delivering 'truncated' back.");
  513. payload[0] = (char)reason;
  514. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  515. payload, sizeof(payload), NULL);
  516. }
  517. }
  518. }
  519. /** Callback to handle a new channel; call command_setup_channel() to give
  520. * it the right cell handlers.
  521. */
  522. static void
  523. command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
  524. {
  525. tor_assert(listener);
  526. tor_assert(chan);
  527. command_setup_channel(chan);
  528. }
  529. /** Given a channel, install the right handlers to process incoming
  530. * cells on it.
  531. */
  532. void
  533. command_setup_channel(channel_t *chan)
  534. {
  535. tor_assert(chan);
  536. channel_set_cell_handlers(chan,
  537. command_process_cell,
  538. command_process_var_cell);
  539. }
  540. /** Given a listener, install the right handler to process incoming
  541. * channels on it.
  542. */
  543. void
  544. command_setup_listener(channel_listener_t *listener)
  545. {
  546. tor_assert(listener);
  547. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
  548. channel_listener_set_listener_fn(listener, command_handle_incoming_channel);
  549. }