command.c 12 KB

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