command.c 25 KB

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