command.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_fast.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. ++stats_n_create_cells_processed;
  120. PROCESS_CELL(create, cell, chan);
  121. break;
  122. case CELL_CREATED:
  123. case CELL_CREATED_FAST:
  124. ++stats_n_created_cells_processed;
  125. PROCESS_CELL(created, cell, chan);
  126. break;
  127. case CELL_RELAY:
  128. case CELL_RELAY_EARLY:
  129. ++stats_n_relay_cells_processed;
  130. PROCESS_CELL(relay, cell, chan);
  131. break;
  132. case CELL_DESTROY:
  133. ++stats_n_destroy_cells_processed;
  134. PROCESS_CELL(destroy, cell, chan);
  135. break;
  136. default:
  137. log_fn(LOG_INFO, LD_PROTOCOL,
  138. "Cell of unknown or unexpected type (%d) received. "
  139. "Dropping.",
  140. cell->command);
  141. break;
  142. }
  143. }
  144. /** Process an incoming var_cell from a channel; in the current protocol all
  145. * the var_cells are handshake-related and handled below the channel layer,
  146. * so this just logs a warning and drops the cell.
  147. */
  148. void
  149. command_process_var_cell(channel_t *chan, var_cell_t *var_cell)
  150. {
  151. tor_assert(chan);
  152. tor_assert(var_cell);
  153. log_info(LD_PROTOCOL,
  154. "Received unexpected var_cell above the channel layer of type %d"
  155. "; dropping it.",
  156. var_cell->command);
  157. }
  158. /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
  159. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  160. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  161. * picked up again when the cpuworker finishes decrypting it.
  162. */
  163. static void
  164. command_process_create_cell(cell_t *cell, channel_t *chan)
  165. {
  166. or_circuit_t *circ;
  167. const or_options_t *options = get_options();
  168. int id_is_high;
  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. if (cell->command == CELL_CREATE) {
  228. char *onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  229. memcpy(onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  230. /* hand it off to the cpuworkers, and then return. */
  231. if (assign_onionskin_to_cpuworker(NULL, circ, onionskin) < 0) {
  232. log_debug(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  233. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  234. return;
  235. }
  236. log_debug(LD_OR,"success: handed off onionskin.");
  237. } else {
  238. /* This is a CREATE_FAST cell; we can handle it immediately without using
  239. * a CPU worker. */
  240. char keys[CPATH_KEY_MATERIAL_LEN];
  241. char reply[DIGEST_LEN*2];
  242. tor_assert(cell->command == CELL_CREATE_FAST);
  243. /* Make sure we never try to use the OR connection on which we
  244. * received this cell to satisfy an EXTEND request, */
  245. channel_mark_client(chan);
  246. if (fast_server_handshake(cell->payload, (uint8_t*)reply,
  247. (uint8_t*)keys, sizeof(keys))<0) {
  248. log_warn(LD_OR,"Failed to generate key material. Closing.");
  249. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  250. return;
  251. }
  252. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  253. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  254. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  255. return;
  256. }
  257. }
  258. }
  259. /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
  260. * Find the circuit
  261. * that it's intended for. If we're not the origin of the circuit, package
  262. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  263. * are the origin of the circuit, send it to circuit_finish_handshake() to
  264. * finish processing keys, and then call circuit_send_next_onion_skin() to
  265. * extend to the next hop in the circuit if necessary.
  266. */
  267. static void
  268. command_process_created_cell(cell_t *cell, channel_t *chan)
  269. {
  270. circuit_t *circ;
  271. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  272. if (!circ) {
  273. log_info(LD_OR,
  274. "(circID %d) unknown circ (probably got a destroy earlier). "
  275. "Dropping.", cell->circ_id);
  276. return;
  277. }
  278. if (circ->n_circ_id != cell->circ_id) {
  279. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  280. "got created cell from Tor client? Closing.");
  281. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  282. return;
  283. }
  284. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  285. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  286. int err_reason = 0;
  287. log_debug(LD_OR,"at OP. Finishing handshake.");
  288. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  289. cell->payload)) < 0) {
  290. log_warn(LD_OR,"circuit_finish_handshake failed.");
  291. circuit_mark_for_close(circ, -err_reason);
  292. return;
  293. }
  294. log_debug(LD_OR,"Moving to next skin.");
  295. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  296. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  297. /* XXX push this circuit_close lower */
  298. circuit_mark_for_close(circ, -err_reason);
  299. return;
  300. }
  301. } else { /* pack it into an extended relay cell, and send it. */
  302. log_debug(LD_OR,
  303. "Converting created cell to extended relay cell, sending.");
  304. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  305. (char*)cell->payload, ONIONSKIN_REPLY_LEN,
  306. NULL);
  307. }
  308. }
  309. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  310. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  311. * circuit_receive_relay_cell() for actual processing.
  312. */
  313. static void
  314. command_process_relay_cell(cell_t *cell, channel_t *chan)
  315. {
  316. circuit_t *circ;
  317. int reason, direction;
  318. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  319. if (!circ) {
  320. log_debug(LD_OR,
  321. "unknown circuit %d on connection from %s. Dropping.",
  322. cell->circ_id, channel_get_canonical_remote_descr(chan));
  323. return;
  324. }
  325. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  326. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  327. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  328. return;
  329. }
  330. if (CIRCUIT_IS_ORIGIN(circ)) {
  331. /* if we're a relay and treating connections with recent local
  332. * traffic better, then this is one of them. */
  333. channel_timestamp_client(chan);
  334. }
  335. if (!CIRCUIT_IS_ORIGIN(circ) &&
  336. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  337. direction = CELL_DIRECTION_OUT;
  338. else
  339. direction = CELL_DIRECTION_IN;
  340. /* If we have a relay_early cell, make sure that it's outbound, and we've
  341. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  342. if (cell->command == CELL_RELAY_EARLY) {
  343. if (direction == CELL_DIRECTION_IN) {
  344. /* Allow an unlimited number of inbound relay_early cells,
  345. * for hidden service compatibility. There isn't any way to make
  346. * a long circuit through inbound relay_early cells anyway. See
  347. * bug 1038. -RD */
  348. } else {
  349. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  350. if (or_circ->remaining_relay_early_cells == 0) {
  351. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  352. "Received too many RELAY_EARLY cells on circ %d from %s."
  353. " Closing circuit.",
  354. cell->circ_id,
  355. safe_str(channel_get_canonical_remote_descr(chan)));
  356. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  357. return;
  358. }
  359. --or_circ->remaining_relay_early_cells;
  360. }
  361. }
  362. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  363. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  364. "(%s) failed. Closing.",
  365. direction==CELL_DIRECTION_OUT?"forward":"backward");
  366. circuit_mark_for_close(circ, -reason);
  367. }
  368. }
  369. /** Process a 'destroy' <b>cell</b> that just arrived from
  370. * <b>chan</b>. Find the circ that it refers to (if any).
  371. *
  372. * If the circ is in state
  373. * onionskin_pending, then call onion_pending_remove() to remove it
  374. * from the pending onion list (note that if it's already being
  375. * processed by the cpuworker, it won't be in the list anymore; but
  376. * when the cpuworker returns it, the circuit will be gone, and the
  377. * cpuworker response will be dropped).
  378. *
  379. * Then mark the circuit for close (which marks all edges for close,
  380. * and passes the destroy cell onward if necessary).
  381. */
  382. static void
  383. command_process_destroy_cell(cell_t *cell, channel_t *chan)
  384. {
  385. circuit_t *circ;
  386. int reason;
  387. circ = circuit_get_by_circid_channel(cell->circ_id, chan);
  388. if (!circ) {
  389. log_info(LD_OR,"unknown circuit %d on connection from %s. Dropping.",
  390. cell->circ_id, channel_get_canonical_remote_descr(chan));
  391. return;
  392. }
  393. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  394. reason = (uint8_t)cell->payload[0];
  395. if (!CIRCUIT_IS_ORIGIN(circ) &&
  396. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  397. /* the destroy came from behind */
  398. circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ), 0, NULL);
  399. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  400. } else { /* the destroy came from ahead */
  401. circuit_set_n_circid_chan(circ, 0, NULL);
  402. if (CIRCUIT_IS_ORIGIN(circ)) {
  403. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  404. } else {
  405. char payload[1];
  406. log_debug(LD_OR, "Delivering 'truncated' back.");
  407. payload[0] = (char)reason;
  408. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  409. payload, sizeof(payload), NULL);
  410. }
  411. }
  412. }
  413. /** Callback to handle a new channel; call command_setup_channel() to give
  414. * it the right cell handlers.
  415. */
  416. static void
  417. command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
  418. {
  419. tor_assert(listener);
  420. tor_assert(chan);
  421. command_setup_channel(chan);
  422. }
  423. /** Given a channel, install the right handlers to process incoming
  424. * cells on it.
  425. */
  426. void
  427. command_setup_channel(channel_t *chan)
  428. {
  429. tor_assert(chan);
  430. channel_set_cell_handlers(chan,
  431. command_process_cell,
  432. command_process_var_cell);
  433. }
  434. /** Given a listener, install the right handler to process incoming
  435. * channels on it.
  436. */
  437. void
  438. command_setup_listener(channel_listener_t *listener)
  439. {
  440. tor_assert(listener);
  441. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
  442. channel_listener_set_listener_fn(listener, command_handle_incoming_channel);
  443. }