command.c 19 KB

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