command.c 13 KB

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