command.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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[] = "$Id$";
  7. /**
  8. * \file command.c
  9. * \brief Functions for processing incoming cells
  10. **/
  11. /* In-points to command.c:
  12. *
  13. * - command_process_cell(), called from
  14. * connection_or_process_cells_from_inbuf() in connection_or.c
  15. */
  16. #include "or.h"
  17. /** Keep statistics about how many of each type of cell we've received. */
  18. unsigned long stats_n_padding_cells_processed = 0;
  19. unsigned long stats_n_create_cells_processed = 0;
  20. unsigned long stats_n_created_cells_processed = 0;
  21. unsigned long stats_n_relay_cells_processed = 0;
  22. unsigned long stats_n_destroy_cells_processed = 0;
  23. /** These are the main four functions for processing cells */
  24. static void command_process_create_cell(cell_t *cell, connection_t *conn);
  25. static void command_process_created_cell(cell_t *cell, connection_t *conn);
  26. static void command_process_relay_cell(cell_t *cell, connection_t *conn);
  27. static void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  28. #ifdef KEEP_TIMING_STATS
  29. /** This is a wrapper function around the actual function that processes the
  30. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  31. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  32. */
  33. static void command_time_process_cell(cell_t *cell, connection_t *conn, int *time,
  34. void (*func)(cell_t *, connection_t *)) {
  35. struct timeval start, end;
  36. long time_passed;
  37. tor_gettimeofday(&start);
  38. (*func)(cell, conn);
  39. tor_gettimeofday(&end);
  40. time_passed = tv_udiff(&start, &end) ;
  41. if (time_passed > 10000) { /* more than 10ms */
  42. log_fn(LOG_DEBUG,"That call just took %ld ms.",time_passed/1000);
  43. }
  44. if (time_passed < 0) {
  45. log_fn(LOG_INFO,"That call took us back in time!");
  46. time_passed = 0;
  47. }
  48. *time += time_passed;
  49. }
  50. #endif
  51. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  52. * statistics about how many of each cell we've processed so far
  53. * this second, and the total number of microseconds it took to
  54. * process each type of cell.
  55. */
  56. void command_process_cell(cell_t *cell, connection_t *conn) {
  57. #ifdef KEEP_TIMING_STATS
  58. /* how many of each cell have we seen so far this second? needs better
  59. * name. */
  60. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  61. /* how long has it taken to process each type of cell? */
  62. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  63. static time_t current_second = 0; /* from previous calls to time */
  64. time_t now = time(NULL);
  65. if (now > current_second) { /* the second has rolled over */
  66. /* print stats */
  67. log(LOG_INFO,"At end of second: %d creates (%d ms), %d createds (%d ms), %d relays (%d ms), %d destroys (%d ms)",
  68. num_create, create_time/1000,
  69. num_created, created_time/1000,
  70. num_relay, relay_time/1000,
  71. num_destroy, destroy_time/1000);
  72. /* zero out stats */
  73. num_create = num_created = num_relay = num_destroy = 0;
  74. create_time = created_time = relay_time = destroy_time = 0;
  75. /* remember which second it is, for next time */
  76. current_second = now;
  77. }
  78. #endif
  79. switch (cell->command) {
  80. case CELL_PADDING:
  81. ++stats_n_padding_cells_processed;
  82. /* do nothing */
  83. break;
  84. case CELL_CREATE:
  85. ++stats_n_create_cells_processed;
  86. #ifdef KEEP_TIMING_STATS
  87. ++num_create;
  88. command_time_process_cell(cell, conn, &create_time,
  89. command_process_create_cell);
  90. #else
  91. command_process_create_cell(cell, conn);
  92. #endif
  93. break;
  94. case CELL_CREATED:
  95. ++stats_n_created_cells_processed;
  96. #ifdef KEEP_TIMING_STATS
  97. ++num_created;
  98. command_time_process_cell(cell, conn, &created_time,
  99. command_process_created_cell);
  100. #else
  101. command_process_created_cell(cell, conn);
  102. #endif
  103. break;
  104. case CELL_RELAY:
  105. ++stats_n_relay_cells_processed;
  106. #ifdef KEEP_TIMING_STATS
  107. ++num_relay;
  108. command_time_process_cell(cell, conn, &relay_time,
  109. command_process_relay_cell);
  110. #else
  111. command_process_relay_cell(cell, conn);
  112. #endif
  113. break;
  114. case CELL_DESTROY:
  115. ++stats_n_destroy_cells_processed;
  116. #ifdef KEEP_TIMING_STATS
  117. ++num_destroy;
  118. command_time_process_cell(cell, conn, &destroy_time,
  119. command_process_destroy_cell);
  120. #else
  121. command_process_destroy_cell(cell, conn);
  122. #endif
  123. break;
  124. default:
  125. log_fn(LOG_WARN,"Cell of unknown type (%d) received. Dropping.", cell->command);
  126. break;
  127. }
  128. }
  129. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a new circuit
  130. * with the p_circ_id specified in cell. Put the circuit in state
  131. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will
  132. * get picked up again when the cpuworker finishes decrypting it.
  133. */
  134. static void command_process_create_cell(cell_t *cell, connection_t *conn) {
  135. circuit_t *circ;
  136. if (we_are_hibernating()) {
  137. log_fn(LOG_INFO,"Received create cell but we're shutting down. Sending back destroy.");
  138. connection_send_destroy(cell->circ_id, conn);
  139. return;
  140. }
  141. /* If the high bit of the circuit ID is not as expected, then switch
  142. * which half of the space we'll use for our own CREATE cells.
  143. *
  144. * This can happen because Tor 0.0.9pre5 and earlier decide which
  145. * half to use based on nickname, and we now use identity keys.
  146. */
  147. if ((cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) {
  148. log_fn(LOG_INFO, "Got a high circuit ID from %s (%d); switching to low circuit IDs.",
  149. conn->nickname ? conn->nickname : "client", conn->s);
  150. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  151. } else if (!(cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_LOWER) {
  152. log_fn(LOG_INFO, "Got a low circuit ID from %s (%d); switching to high circuit IDs.",
  153. conn->nickname ? conn->nickname : "client", conn->s);
  154. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  155. }
  156. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  157. if (circ) {
  158. log_fn(LOG_WARN,"received CREATE cell (circID %d) for known circ. Dropping.", cell->circ_id);
  159. return;
  160. }
  161. circ = circuit_new(cell->circ_id, conn);
  162. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  163. circ->purpose = CIRCUIT_PURPOSE_OR;
  164. memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  165. /* hand it off to the cpuworkers, and then return */
  166. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  167. log_fn(LOG_WARN,"Failed to hand off onionskin. Closing.");
  168. circuit_mark_for_close(circ);
  169. return;
  170. }
  171. log_fn(LOG_DEBUG,"success: handed off onionskin.");
  172. }
  173. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>. Find the circuit
  174. * that it's intended for. If we're not the origin of the circuit, package
  175. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  176. * are the origin of the circuit, send it to circuit_finish_handshake() to
  177. * finish processing keys, and then call circuit_send_next_onion_skin() to
  178. * extend to the next hop in the circuit if necessary.
  179. */
  180. static void command_process_created_cell(cell_t *cell, connection_t *conn) {
  181. circuit_t *circ;
  182. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  183. if (!circ) {
  184. log_fn(LOG_INFO,"(circID %d) unknown circ (probably got a destroy earlier). Dropping.", cell->circ_id);
  185. return;
  186. }
  187. if (circ->n_circ_id != cell->circ_id) {
  188. log_fn(LOG_WARN,"got created cell from OPward? Closing.");
  189. circuit_mark_for_close(circ);
  190. return;
  191. }
  192. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  193. log_fn(LOG_DEBUG,"at OP. Finishing handshake.");
  194. if (circuit_finish_handshake(circ, cell->payload) < 0) {
  195. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  196. circuit_mark_for_close(circ);
  197. return;
  198. }
  199. log_fn(LOG_DEBUG,"Moving to next skin.");
  200. if (circuit_send_next_onion_skin(circ) < 0) {
  201. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  202. circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
  203. return;
  204. }
  205. } else { /* pack it into an extended relay cell, and send it. */
  206. log_fn(LOG_DEBUG,"Converting created cell to extended relay cell, sending.");
  207. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  208. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  209. }
  210. }
  211. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  212. * it came in with a recognized circ_id. Pass it on to
  213. * circuit_receive_relay_cell() for actual processing.
  214. */
  215. static void command_process_relay_cell(cell_t *cell, connection_t *conn) {
  216. circuit_t *circ;
  217. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  218. if (!circ) {
  219. log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
  220. cell->circ_id, conn->address, conn->port);
  221. return;
  222. }
  223. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  224. log_fn(LOG_WARN,"circuit in create_wait. Closing.");
  225. circuit_mark_for_close(circ);
  226. return;
  227. }
  228. if (cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
  229. if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  230. log_fn(LOG_WARN,"circuit_receive_relay_cell (forward) failed. Closing.");
  231. circuit_mark_for_close(circ);
  232. return;
  233. }
  234. } else { /* it's an ingoing cell */
  235. if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  236. log_fn(LOG_WARN,"circuit_receive_relay_cell (backward) failed. Closing.");
  237. circuit_mark_for_close(circ);
  238. return;
  239. }
  240. }
  241. }
  242. /** Process a 'destroy' <b>cell</b> that just arrived from
  243. * <b>conn</b>. Find the circ that it refers to (if any).
  244. *
  245. * If the circ is in state
  246. * onionskin_pending, then call onion_pending_remove() to remove it
  247. * from the pending onion list (note that if it's already being
  248. * processed by the cpuworker, it won't be in the list anymore; but
  249. * when the cpuworker returns it, the circuit will be gone, and the
  250. * cpuworker response will be dropped).
  251. *
  252. * Then mark the circuit for close (which marks all edges for close,
  253. * and passes the destroy cell onward if necessary).
  254. */
  255. static void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  256. circuit_t *circ;
  257. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  258. if (!circ) {
  259. log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
  260. cell->circ_id, conn->address, conn->port);
  261. return;
  262. }
  263. log_fn(LOG_INFO,"Received for circID %d.",cell->circ_id);
  264. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  265. onion_pending_remove(circ);
  266. }
  267. if (cell->circ_id == circ->p_circ_id) {
  268. /* the destroy came from behind */
  269. circ->p_conn = NULL;
  270. circuit_mark_for_close(circ);
  271. } else { /* the destroy came from ahead */
  272. circ->n_conn = NULL;
  273. #if 0
  274. if (!CIRCUIT_IS_ORIGIN(circ)) {
  275. log_fn(LOG_DEBUG, "Delivering 'truncated' back.");
  276. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  277. NULL, 0, NULL);
  278. }
  279. #endif
  280. circuit_mark_for_close(circ);
  281. }
  282. }