command.c 12 KB

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