command.c 18 KB

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