command.c 17 KB

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