command.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  45. }
  46. if (time_passed < 0) {
  47. info(LD_GENERAL,"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. info(LD_OR,"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_PROTOCOL_WARN, LD_PROTOCOL,
  132. "Cell of unknown type (%d) received. Dropping.", cell->command);
  133. break;
  134. }
  135. }
  136. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a new circuit
  137. * with the p_circ_id specified in cell. Put the circuit in state
  138. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will
  139. * get picked up again when the cpuworker finishes decrypting it.
  140. */
  141. static void
  142. command_process_create_cell(cell_t *cell, connection_t *conn)
  143. {
  144. circuit_t *circ;
  145. if (we_are_hibernating()) {
  146. info(LD_OR,"Received create cell but we're shutting down. Sending back destroy.");
  147. connection_send_destroy(cell->circ_id, conn);
  148. return;
  149. }
  150. /* If the high bit of the circuit ID is not as expected, then switch
  151. * which half of the space we'll use for our own CREATE cells.
  152. *
  153. * This can happen because Tor 0.0.9pre5 and earlier decide which
  154. * half to use based on nickname, and we now use identity keys.
  155. */
  156. if ((cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) {
  157. info(LD_OR, "Got a high circuit ID from %s (%d); switching to low circuit IDs.",
  158. conn->nickname ? conn->nickname : "client", conn->s);
  159. conn->circ_id_type = CIRC_ID_TYPE_LOWER;
  160. } else if (!(cell->circ_id & (1<<15)) && conn->circ_id_type == CIRC_ID_TYPE_LOWER) {
  161. info(LD_OR, "Got a low circuit ID from %s (%d); switching to high circuit IDs.",
  162. conn->nickname ? conn->nickname : "client", conn->s);
  163. conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
  164. }
  165. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  166. if (circ) {
  167. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  168. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  169. "received CREATE cell (circID %d) for known circ. Dropping (age %d).",
  170. cell->circ_id, (int)(time(NULL) - conn->timestamp_created));
  171. if (router)
  172. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  173. "Details: nickname '%s', platform '%s'.",
  174. router->nickname, router->platform);
  175. return;
  176. }
  177. circ = circuit_new(cell->circ_id, conn);
  178. circ->purpose = CIRCUIT_PURPOSE_OR;
  179. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  180. if (cell->command == CELL_CREATE) {
  181. circ->onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  182. memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  183. /* hand it off to the cpuworkers, and then return */
  184. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  185. warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  186. circuit_mark_for_close(circ);
  187. return;
  188. }
  189. debug(LD_OR,"success: handed off onionskin.");
  190. } else {
  191. char keys[CPATH_KEY_MATERIAL_LEN];
  192. char reply[DIGEST_LEN*2];
  193. tor_assert(cell->command == CELL_CREATE_FAST);
  194. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  195. warn(LD_OR,"Failed to generate key material. Closing.");
  196. circuit_mark_for_close(circ);
  197. return;
  198. }
  199. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  200. warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  201. circuit_mark_for_close(circ);
  202. return;
  203. }
  204. }
  205. }
  206. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>. Find the circuit
  207. * that it's intended for. If we're not the origin of the circuit, package
  208. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  209. * are the origin of the circuit, send it to circuit_finish_handshake() to
  210. * finish processing keys, and then call circuit_send_next_onion_skin() to
  211. * extend to the next hop in the circuit if necessary.
  212. */
  213. static void
  214. command_process_created_cell(cell_t *cell, connection_t *conn)
  215. {
  216. circuit_t *circ;
  217. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  218. if (!circ) {
  219. info(LD_OR,"(circID %d) unknown circ (probably got a destroy earlier). Dropping.", cell->circ_id);
  220. return;
  221. }
  222. if (circ->n_circ_id != cell->circ_id) {
  223. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  224. "got created cell from OPward? Closing.");
  225. circuit_mark_for_close(circ);
  226. return;
  227. }
  228. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  229. debug(LD_OR,"at OP. Finishing handshake.");
  230. if (circuit_finish_handshake(circ, cell->command, cell->payload) < 0) {
  231. warn(LD_OR,"circuit_finish_handshake failed.");
  232. circuit_mark_for_close(circ);
  233. return;
  234. }
  235. debug(LD_OR,"Moving to next skin.");
  236. if (circuit_send_next_onion_skin(circ) < 0) {
  237. info(LD_OR,"circuit_send_next_onion_skin failed.");
  238. circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
  239. return;
  240. }
  241. } else { /* pack it into an extended relay cell, and send it. */
  242. debug(LD_OR,"Converting created cell to extended relay cell, sending.");
  243. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  244. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  245. }
  246. }
  247. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  248. * it came in with a recognized circ_id. Pass it on to
  249. * circuit_receive_relay_cell() for actual processing.
  250. */
  251. static void
  252. command_process_relay_cell(cell_t *cell, connection_t *conn)
  253. {
  254. circuit_t *circ;
  255. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  256. if (!circ) {
  257. debug(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  258. cell->circ_id, conn->address, conn->port);
  259. return;
  260. }
  261. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  262. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  263. circuit_mark_for_close(circ);
  264. return;
  265. }
  266. if (cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
  267. if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  268. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell (forward) failed. Closing.");
  269. circuit_mark_for_close(circ);
  270. return;
  271. }
  272. } else { /* it's an ingoing cell */
  273. if (circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  274. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell (backward) failed. Closing.");
  275. circuit_mark_for_close(circ);
  276. return;
  277. }
  278. }
  279. }
  280. /** Process a 'destroy' <b>cell</b> that just arrived from
  281. * <b>conn</b>. Find the circ that it refers to (if any).
  282. *
  283. * If the circ is in state
  284. * onionskin_pending, then call onion_pending_remove() to remove it
  285. * from the pending onion list (note that if it's already being
  286. * processed by the cpuworker, it won't be in the list anymore; but
  287. * when the cpuworker returns it, the circuit will be gone, and the
  288. * cpuworker response will be dropped).
  289. *
  290. * Then mark the circuit for close (which marks all edges for close,
  291. * and passes the destroy cell onward if necessary).
  292. */
  293. static void
  294. command_process_destroy_cell(cell_t *cell, connection_t *conn)
  295. {
  296. circuit_t *circ;
  297. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  298. if (!circ) {
  299. info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  300. cell->circ_id, conn->address, conn->port);
  301. return;
  302. }
  303. debug(LD_OR,"Received for circID %d.",cell->circ_id);
  304. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  305. onion_pending_remove(circ);
  306. }
  307. if (cell->circ_id == circ->p_circ_id) {
  308. /* the destroy came from behind */
  309. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  310. circuit_mark_for_close(circ);
  311. } else { /* the destroy came from ahead */
  312. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  313. if (CIRCUIT_IS_ORIGIN(circ)) {
  314. circuit_mark_for_close(circ);
  315. } else {
  316. debug(LD_OR, "Delivering 'truncated' back.");
  317. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  318. NULL, 0, NULL);
  319. }
  320. }
  321. }