command.c 25 KB

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