command.c 13 KB

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