command.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char command_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file command.c
  10. * \brief Functions for processing incoming cells.
  11. **/
  12. /* In-points to command.c:
  13. *
  14. * - command_process_cell(), called from
  15. * connection_or_process_cells_from_inbuf() in connection_or.c
  16. */
  17. #include "or.h"
  18. /** Keep statistics about how many of each type of cell we've received. */
  19. uint64_t stats_n_padding_cells_processed = 0;
  20. uint64_t stats_n_create_cells_processed = 0;
  21. uint64_t stats_n_created_cells_processed = 0;
  22. uint64_t stats_n_relay_cells_processed = 0;
  23. uint64_t stats_n_destroy_cells_processed = 0;
  24. /* These are the main four functions for processing cells */
  25. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  26. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  27. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  28. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  29. #ifdef KEEP_TIMING_STATS
  30. /** This is a wrapper function around the actual function that processes the
  31. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  32. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  33. */
  34. static void
  35. command_time_process_cell(cell_t *cell, or_connection_t *conn, int *time,
  36. void (*func)(cell_t *, or_connection_t *))
  37. {
  38. struct timeval start, end;
  39. long time_passed;
  40. tor_gettimeofday(&start);
  41. (*func)(cell, conn);
  42. tor_gettimeofday(&end);
  43. time_passed = tv_udiff(&start, &end) ;
  44. if (time_passed > 10000) { /* more than 10ms */
  45. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  46. }
  47. if (time_passed < 0) {
  48. log_info(LD_GENERAL,"That call took us back in time!");
  49. time_passed = 0;
  50. }
  51. *time += time_passed;
  52. }
  53. #endif
  54. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  55. * statistics about how many of each cell we've processed so far
  56. * this second, and the total number of microseconds it took to
  57. * process each type of cell.
  58. */
  59. void
  60. command_process_cell(cell_t *cell, or_connection_t *conn)
  61. {
  62. #ifdef KEEP_TIMING_STATS
  63. /* how many of each cell have we seen so far this second? needs better
  64. * name. */
  65. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  66. /* how long has it taken to process each type of cell? */
  67. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  68. static time_t current_second = 0; /* from previous calls to time */
  69. time_t now = time(NULL);
  70. if (now > current_second) { /* the second has rolled over */
  71. /* print stats */
  72. log_info(LD_OR,
  73. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  74. "%d relays (%d ms), %d destroys (%d ms)",
  75. num_create, create_time/1000,
  76. num_created, created_time/1000,
  77. num_relay, relay_time/1000,
  78. num_destroy, destroy_time/1000);
  79. /* zero out stats */
  80. num_create = num_created = num_relay = num_destroy = 0;
  81. create_time = created_time = relay_time = destroy_time = 0;
  82. /* remember which second it is, for next time */
  83. current_second = now;
  84. }
  85. #endif
  86. switch (cell->command) {
  87. case CELL_PADDING:
  88. ++stats_n_padding_cells_processed;
  89. /* do nothing */
  90. break;
  91. case CELL_CREATE:
  92. case CELL_CREATE_FAST:
  93. ++stats_n_create_cells_processed;
  94. #ifdef KEEP_TIMING_STATS
  95. ++num_create;
  96. command_time_process_cell(cell, conn, &create_time,
  97. command_process_create_cell);
  98. #else
  99. command_process_create_cell(cell, conn);
  100. #endif
  101. break;
  102. case CELL_CREATED:
  103. case CELL_CREATED_FAST:
  104. ++stats_n_created_cells_processed;
  105. #ifdef KEEP_TIMING_STATS
  106. ++num_created;
  107. command_time_process_cell(cell, conn, &created_time,
  108. command_process_created_cell);
  109. #else
  110. command_process_created_cell(cell, conn);
  111. #endif
  112. break;
  113. case CELL_RELAY:
  114. ++stats_n_relay_cells_processed;
  115. #ifdef KEEP_TIMING_STATS
  116. ++num_relay;
  117. command_time_process_cell(cell, conn, &relay_time,
  118. command_process_relay_cell);
  119. #else
  120. command_process_relay_cell(cell, conn);
  121. #endif
  122. break;
  123. case CELL_DESTROY:
  124. ++stats_n_destroy_cells_processed;
  125. #ifdef KEEP_TIMING_STATS
  126. ++num_destroy;
  127. command_time_process_cell(cell, conn, &destroy_time,
  128. command_process_destroy_cell);
  129. #else
  130. command_process_destroy_cell(cell, conn);
  131. #endif
  132. break;
  133. default:
  134. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  135. "Cell of unknown type (%d) received. Dropping.", cell->command);
  136. break;
  137. }
  138. }
  139. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  140. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  141. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  142. * picked up again when the cpuworker finishes decrypting it.
  143. */
  144. static void
  145. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  146. {
  147. or_circuit_t *circ;
  148. int id_is_high;
  149. if (we_are_hibernating()) {
  150. log_info(LD_OR,
  151. "Received create cell but we're shutting down. Sending back "
  152. "destroy.");
  153. connection_or_send_destroy(cell->circ_id, conn,
  154. END_CIRC_REASON_HIBERNATING);
  155. return;
  156. }
  157. if (!server_mode(get_options())) {
  158. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  159. "Received create cell (type %d) from %s:%d, but we're a client. "
  160. "Sending back a destroy.",
  161. (int)cell->command, conn->_base.address, conn->_base.port);
  162. connection_or_send_destroy(cell->circ_id, conn,
  163. END_CIRC_REASON_TORPROTOCOL);
  164. return;
  165. }
  166. /* If the high bit of the circuit ID is not as expected, close the
  167. * circ. */
  168. id_is_high = cell->circ_id & (1<<15);
  169. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  170. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  171. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  172. "Received create cell with unexpected circ_id %d. Closing.",
  173. cell->circ_id);
  174. connection_or_send_destroy(cell->circ_id, conn,
  175. END_CIRC_REASON_TORPROTOCOL);
  176. return;
  177. }
  178. if (circuit_get_by_circid_orconn(cell->circ_id, conn)) {
  179. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  180. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  181. "Received CREATE cell (circID %d) for known circ. "
  182. "Dropping (age %d).",
  183. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  184. if (router)
  185. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  186. "Details: nickname \"%s\", platform %s.",
  187. router->nickname, escaped(router->platform));
  188. return;
  189. }
  190. circ = or_circuit_new(cell->circ_id, conn);
  191. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  192. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  193. if (cell->command == CELL_CREATE) {
  194. circ->_base.onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  195. memcpy(circ->_base.onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  196. /* hand it off to the cpuworkers, and then return. */
  197. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  198. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  199. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  200. return;
  201. }
  202. log_debug(LD_OR,"success: handed off onionskin.");
  203. } else {
  204. /* This is a CREATE_FAST cell; we can handle it immediately without using
  205. * a CPU worker. */
  206. char keys[CPATH_KEY_MATERIAL_LEN];
  207. char reply[DIGEST_LEN*2];
  208. tor_assert(cell->command == CELL_CREATE_FAST);
  209. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  210. log_warn(LD_OR,"Failed to generate key material. Closing.");
  211. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  212. return;
  213. }
  214. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  215. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  216. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  217. return;
  218. }
  219. }
  220. }
  221. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  222. * Find the circuit
  223. * that it's intended for. If we're not the origin of the circuit, package
  224. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  225. * are the origin of the circuit, send it to circuit_finish_handshake() to
  226. * finish processing keys, and then call circuit_send_next_onion_skin() to
  227. * extend to the next hop in the circuit if necessary.
  228. */
  229. static void
  230. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  231. {
  232. circuit_t *circ;
  233. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  234. if (!circ) {
  235. log_info(LD_OR,
  236. "(circID %d) unknown circ (probably got a destroy earlier). "
  237. "Dropping.", cell->circ_id);
  238. return;
  239. }
  240. if (circ->n_circ_id != cell->circ_id) {
  241. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  242. "got created cell from Tor client? Closing.");
  243. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  244. return;
  245. }
  246. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  247. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  248. int err_reason = 0;
  249. log_debug(LD_OR,"at OP. Finishing handshake.");
  250. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  251. cell->payload)) < 0) {
  252. log_warn(LD_OR,"circuit_finish_handshake failed.");
  253. circuit_mark_for_close(circ, -err_reason);
  254. return;
  255. }
  256. log_debug(LD_OR,"Moving to next skin.");
  257. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  258. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  259. /* XXX push this circuit_close lower */
  260. circuit_mark_for_close(circ, -err_reason);
  261. return;
  262. }
  263. } else { /* pack it into an extended relay cell, and send it. */
  264. log_debug(LD_OR,
  265. "Converting created cell to extended relay cell, sending.");
  266. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  267. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  268. }
  269. }
  270. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  271. * it came in with a recognized circ_id. Pass it on to
  272. * circuit_receive_relay_cell() for actual processing.
  273. */
  274. static void
  275. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  276. {
  277. circuit_t *circ;
  278. int reason;
  279. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  280. if (!circ) {
  281. log_debug(LD_OR,
  282. "unknown circuit %d on connection from %s:%d. Dropping.",
  283. cell->circ_id, conn->_base.address, conn->_base.port);
  284. return;
  285. }
  286. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  287. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  288. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  289. return;
  290. }
  291. if (!CIRCUIT_IS_ORIGIN(circ) &&
  292. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  293. /* it's an outgoing cell */
  294. if ((reason = circuit_receive_relay_cell(cell, circ,
  295. CELL_DIRECTION_OUT)) < 0) {
  296. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  297. "(forward) failed. Closing.");
  298. circuit_mark_for_close(circ, -reason);
  299. return;
  300. }
  301. } else { /* it's an ingoing cell */
  302. if ((reason = circuit_receive_relay_cell(cell, circ,
  303. CELL_DIRECTION_IN)) < 0) {
  304. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  305. "(backward) failed. Closing.");
  306. circuit_mark_for_close(circ, -reason);
  307. return;
  308. }
  309. }
  310. }
  311. /** Process a 'destroy' <b>cell</b> that just arrived from
  312. * <b>conn</b>. Find the circ that it refers to (if any).
  313. *
  314. * If the circ is in state
  315. * onionskin_pending, then call onion_pending_remove() to remove it
  316. * from the pending onion list (note that if it's already being
  317. * processed by the cpuworker, it won't be in the list anymore; but
  318. * when the cpuworker returns it, the circuit will be gone, and the
  319. * cpuworker response will be dropped).
  320. *
  321. * Then mark the circuit for close (which marks all edges for close,
  322. * and passes the destroy cell onward if necessary).
  323. */
  324. static void
  325. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  326. {
  327. circuit_t *circ;
  328. int reason;
  329. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  330. reason = (uint8_t)cell->payload[0];
  331. if (!circ) {
  332. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  333. cell->circ_id, conn->_base.address, conn->_base.port);
  334. return;
  335. }
  336. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  337. if (!CIRCUIT_IS_ORIGIN(circ) &&
  338. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  339. /* the destroy came from behind */
  340. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  341. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  342. } else { /* the destroy came from ahead */
  343. circuit_set_n_circid_orconn(circ, 0, NULL);
  344. if (CIRCUIT_IS_ORIGIN(circ)) {
  345. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  346. } else {
  347. char payload[1];
  348. log_debug(LD_OR, "Delivering 'truncated' back.");
  349. payload[0] = (char)reason;
  350. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  351. payload, sizeof(payload), NULL);
  352. }
  353. }
  354. }