command.c 23 KB

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