command.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 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, connection_t *conn);
  26. static void command_process_created_cell(cell_t *cell, connection_t *conn);
  27. static void command_process_relay_cell(cell_t *cell, connection_t *conn);
  28. static void command_process_destroy_cell(cell_t *cell, 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, connection_t *conn, int *time,
  36. void (*func)(cell_t *, 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. debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  46. }
  47. if (time_passed < 0) {
  48. 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, 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. info(LD_OR,"At end of second: %d creates (%d ms), %d createds (%d ms), "
  73. "%d relays (%d ms), %d destroys (%d ms)",
  74. num_create, create_time/1000,
  75. num_created, created_time/1000,
  76. num_relay, relay_time/1000,
  77. num_destroy, destroy_time/1000);
  78. /* zero out stats */
  79. num_create = num_created = num_relay = num_destroy = 0;
  80. create_time = created_time = relay_time = destroy_time = 0;
  81. /* remember which second it is, for next time */
  82. current_second = now;
  83. }
  84. #endif
  85. switch (cell->command) {
  86. case CELL_PADDING:
  87. ++stats_n_padding_cells_processed;
  88. /* do nothing */
  89. break;
  90. case CELL_CREATE:
  91. case CELL_CREATE_FAST:
  92. ++stats_n_create_cells_processed;
  93. #ifdef KEEP_TIMING_STATS
  94. ++num_create;
  95. command_time_process_cell(cell, conn, &create_time,
  96. command_process_create_cell);
  97. #else
  98. command_process_create_cell(cell, conn);
  99. #endif
  100. break;
  101. case CELL_CREATED:
  102. case CELL_CREATED_FAST:
  103. ++stats_n_created_cells_processed;
  104. #ifdef KEEP_TIMING_STATS
  105. ++num_created;
  106. command_time_process_cell(cell, conn, &created_time,
  107. command_process_created_cell);
  108. #else
  109. command_process_created_cell(cell, conn);
  110. #endif
  111. break;
  112. case CELL_RELAY:
  113. ++stats_n_relay_cells_processed;
  114. #ifdef KEEP_TIMING_STATS
  115. ++num_relay;
  116. command_time_process_cell(cell, conn, &relay_time,
  117. command_process_relay_cell);
  118. #else
  119. command_process_relay_cell(cell, conn);
  120. #endif
  121. break;
  122. case CELL_DESTROY:
  123. ++stats_n_destroy_cells_processed;
  124. #ifdef KEEP_TIMING_STATS
  125. ++num_destroy;
  126. command_time_process_cell(cell, conn, &destroy_time,
  127. command_process_destroy_cell);
  128. #else
  129. command_process_destroy_cell(cell, conn);
  130. #endif
  131. break;
  132. default:
  133. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  134. "Cell of unknown type (%d) received. Dropping.", cell->command);
  135. break;
  136. }
  137. }
  138. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  139. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  140. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  141. * picked up again when the cpuworker finishes decrypting it.
  142. */
  143. static void
  144. command_process_create_cell(cell_t *cell, connection_t *conn)
  145. {
  146. circuit_t *circ;
  147. int id_is_high;
  148. if (we_are_hibernating()) {
  149. info(LD_OR,"Received create cell but we're shutting down. Sending back "
  150. "destroy.");
  151. connection_send_destroy(cell->circ_id, conn);
  152. return;
  153. }
  154. /* If the high bit of the circuit ID is not as expected, then switch
  155. * which half of the space we'll use for our own CREATE cells.
  156. *
  157. * This can happen because Tor 0.0.9pre5 and earlier decide which
  158. * half to use based on nickname, and we now use identity keys.
  159. */
  160. id_is_high = cell->circ_id & (1<<15);
  161. if (id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) {
  162. info(LD_OR, "Got a high circuit ID from %s (%d); switching to "
  163. "low circuit IDs.",
  164. conn->nickname ? conn->nickname : "client", conn->s);
  165. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  166. } else if (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER) {
  167. info(LD_OR, "Got a low circuit ID from %s (%d); switching to "
  168. "high circuit IDs.",
  169. conn->nickname ? conn->nickname : "client", conn->s);
  170. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  171. }
  172. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  173. if (circ) {
  174. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  175. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  176. "received CREATE cell (circID %d) for known circ. "
  177. "Dropping (age %d).",
  178. cell->circ_id, (int)(time(NULL) - conn->timestamp_created));
  179. if (router)
  180. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  181. "Details: nickname '%s', platform '%s'.",
  182. router->nickname, router->platform);
  183. return;
  184. }
  185. circ = circuit_new(cell->circ_id, conn);
  186. circ->purpose = CIRCUIT_PURPOSE_OR;
  187. circuit_set_state(circ, CIRCUIT_STATE_ONIONSKIN_PENDING);
  188. if (cell->command == CELL_CREATE) {
  189. circ->onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  190. memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  191. /* hand it off to the cpuworkers, and then return */
  192. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  193. warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  194. circuit_mark_for_close(circ);
  195. return;
  196. }
  197. debug(LD_OR,"success: handed off onionskin.");
  198. } else {
  199. /* This is a CREATE_FAST cell; we can handle it immediately without using
  200. * a CPU worker.*/
  201. char keys[CPATH_KEY_MATERIAL_LEN];
  202. char reply[DIGEST_LEN*2];
  203. tor_assert(cell->command == CELL_CREATE_FAST);
  204. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  205. warn(LD_OR,"Failed to generate key material. Closing.");
  206. circuit_mark_for_close(circ);
  207. return;
  208. }
  209. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  210. warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  211. circuit_mark_for_close(circ);
  212. return;
  213. }
  214. }
  215. }
  216. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  217. * Find the circuit
  218. * that it's intended for. If we're not the origin of the circuit, package
  219. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  220. * are the origin of the circuit, send it to circuit_finish_handshake() to
  221. * finish processing keys, and then call circuit_send_next_onion_skin() to
  222. * extend to the next hop in the circuit if necessary.
  223. */
  224. static void
  225. command_process_created_cell(cell_t *cell, connection_t *conn)
  226. {
  227. circuit_t *circ;
  228. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  229. if (!circ) {
  230. info(LD_OR,"(circID %d) unknown circ (probably got a destroy earlier). "
  231. "Dropping.", cell->circ_id);
  232. return;
  233. }
  234. if (circ->n_circ_id != cell->circ_id) {
  235. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  236. "got created cell from OPward? Closing.");
  237. circuit_mark_for_close(circ);
  238. return;
  239. }
  240. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  241. debug(LD_OR,"at OP. Finishing handshake.");
  242. if (circuit_finish_handshake(circ, cell->command, cell->payload) < 0) {
  243. warn(LD_OR,"circuit_finish_handshake failed.");
  244. circuit_mark_for_close(circ);
  245. return;
  246. }
  247. debug(LD_OR,"Moving to next skin.");
  248. if (circuit_send_next_onion_skin(circ) < 0) {
  249. info(LD_OR,"circuit_send_next_onion_skin failed.");
  250. circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
  251. return;
  252. }
  253. } else { /* pack it into an extended relay cell, and send it. */
  254. debug(LD_OR,"Converting created cell to extended relay cell, sending.");
  255. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  256. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  257. }
  258. }
  259. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  260. * it came in with a recognized circ_id. Pass it on to
  261. * circuit_receive_relay_cell() for actual processing.
  262. */
  263. static void
  264. command_process_relay_cell(cell_t *cell, connection_t *conn)
  265. {
  266. circuit_t *circ;
  267. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  268. if (!circ) {
  269. debug(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  270. cell->circ_id, conn->address, conn->port);
  271. return;
  272. }
  273. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  274. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  275. circuit_mark_for_close(circ);
  276. return;
  277. }
  278. if (cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
  279. if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  280. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  281. "(forward) failed. Closing.");
  282. circuit_mark_for_close(circ);
  283. return;
  284. }
  285. } else { /* it's an ingoing cell */
  286. if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  287. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  288. "(backward) failed. Closing.");
  289. circuit_mark_for_close(circ);
  290. return;
  291. }
  292. }
  293. }
  294. /** Process a 'destroy' <b>cell</b> that just arrived from
  295. * <b>conn</b>. Find the circ that it refers to (if any).
  296. *
  297. * If the circ is in state
  298. * onionskin_pending, then call onion_pending_remove() to remove it
  299. * from the pending onion list (note that if it's already being
  300. * processed by the cpuworker, it won't be in the list anymore; but
  301. * when the cpuworker returns it, the circuit will be gone, and the
  302. * cpuworker response will be dropped).
  303. *
  304. * Then mark the circuit for close (which marks all edges for close,
  305. * and passes the destroy cell onward if necessary).
  306. */
  307. static void
  308. command_process_destroy_cell(cell_t *cell, connection_t *conn)
  309. {
  310. circuit_t *circ;
  311. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  312. if (!circ) {
  313. info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  314. cell->circ_id, conn->address, conn->port);
  315. return;
  316. }
  317. debug(LD_OR,"Received for circID %d.",cell->circ_id);
  318. if (cell->circ_id == circ->p_circ_id) {
  319. /* the destroy came from behind */
  320. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  321. circuit_mark_for_close(circ);
  322. } else { /* the destroy came from ahead */
  323. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  324. if (CIRCUIT_IS_ORIGIN(circ)) {
  325. circuit_mark_for_close(circ);
  326. } else {
  327. debug(LD_OR, "Delivering 'truncated' back.");
  328. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  329. NULL, 0, NULL);
  330. }
  331. }
  332. }