circuitlist.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char circuitlist_c_id[] = "$Id$";
  7. /**
  8. * \file circuitlist.c
  9. * \brief Manage the global circuit list.
  10. **/
  11. #include "or.h"
  12. /********* START VARIABLES **********/
  13. /** A global list of all circuits at this hop. */
  14. circuit_t *global_circuitlist=NULL;
  15. /** Array of strings to make circ-\>state human-readable */
  16. const char *circuit_state_to_string[] = {
  17. "doing handshakes", /* 0 */
  18. "processing the onion", /* 1 */
  19. "connecting to firsthop", /* 2 */
  20. "open" /* 3 */
  21. };
  22. /********* END VARIABLES ************/
  23. static void circuit_free(circuit_t *circ);
  24. static void circuit_free_cpath(crypt_path_t *cpath);
  25. static void circuit_free_cpath_node(crypt_path_t *victim);
  26. /** Add <b>circ</b> to the global list of circuits. This is called only from
  27. * within circuit_new.
  28. */
  29. static void circuit_add(circuit_t *circ) {
  30. if (!global_circuitlist) { /* first one */
  31. global_circuitlist = circ;
  32. circ->next = NULL;
  33. } else {
  34. circ->next = global_circuitlist;
  35. global_circuitlist = circ;
  36. }
  37. }
  38. /** Detach from the global circuit list, and deallocate, all
  39. * circuits that have been marked for close.
  40. */
  41. void circuit_close_all_marked(void)
  42. {
  43. circuit_t *tmp,*m;
  44. while (global_circuitlist && global_circuitlist->marked_for_close) {
  45. tmp = global_circuitlist->next;
  46. circuit_free(global_circuitlist);
  47. global_circuitlist = tmp;
  48. }
  49. tmp = global_circuitlist;
  50. while (tmp && tmp->next) {
  51. if (tmp->next->marked_for_close) {
  52. m = tmp->next->next;
  53. circuit_free(tmp->next);
  54. tmp->next = m;
  55. /* Need to check new tmp->next; don't advance tmp. */
  56. } else {
  57. /* Advance tmp. */
  58. tmp = tmp->next;
  59. }
  60. }
  61. }
  62. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  63. * and <b>p_conn</b>. Add it to the global circuit list.
  64. */
  65. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
  66. circuit_t *circ;
  67. static uint32_t n_circuits_allocated = 0;
  68. circ = tor_malloc_zero(sizeof(circuit_t));
  69. circ->magic = CIRCUIT_MAGIC;
  70. circ->timestamp_created = time(NULL);
  71. circ->p_circ_id = p_circ_id;
  72. circ->p_conn = p_conn;
  73. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  74. /* CircIDs */
  75. circ->p_circ_id = p_circ_id;
  76. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  77. circ->package_window = CIRCWINDOW_START;
  78. circ->deliver_window = CIRCWINDOW_START;
  79. circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
  80. circ->global_identifier = n_circuits_allocated++;
  81. circuit_add(circ);
  82. return circ;
  83. }
  84. /** Deallocate space associated with circ.
  85. */
  86. static void circuit_free(circuit_t *circ) {
  87. tor_assert(circ);
  88. tor_assert(circ->magic == CIRCUIT_MAGIC);
  89. if (circ->n_crypto)
  90. crypto_free_cipher_env(circ->n_crypto);
  91. if (circ->p_crypto)
  92. crypto_free_cipher_env(circ->p_crypto);
  93. if (circ->n_digest)
  94. crypto_free_digest_env(circ->n_digest);
  95. if (circ->p_digest)
  96. crypto_free_digest_env(circ->p_digest);
  97. if (circ->build_state) {
  98. tor_free(circ->build_state->chosen_exit_name);
  99. if (circ->build_state->pending_final_cpath)
  100. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  101. }
  102. tor_free(circ->build_state);
  103. circuit_free_cpath(circ->cpath);
  104. if (circ->rend_splice) {
  105. circ->rend_splice->rend_splice = NULL;
  106. }
  107. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  108. tor_free(circ);
  109. }
  110. /** Deallocate space associated with the linked list <b>cpath</b>. */
  111. static void circuit_free_cpath(crypt_path_t *cpath) {
  112. crypt_path_t *victim, *head=cpath;
  113. if (!cpath)
  114. return;
  115. /* it's a doubly linked list, so we have to notice when we've
  116. * gone through it once. */
  117. while (cpath->next && cpath->next != head) {
  118. victim = cpath;
  119. cpath = victim->next;
  120. circuit_free_cpath_node(victim);
  121. }
  122. circuit_free_cpath_node(cpath);
  123. }
  124. /** Deallocate space associated with the cpath node <b>victim</b>. */
  125. static void
  126. circuit_free_cpath_node(crypt_path_t *victim) {
  127. if (victim->f_crypto)
  128. crypto_free_cipher_env(victim->f_crypto);
  129. if (victim->b_crypto)
  130. crypto_free_cipher_env(victim->b_crypto);
  131. if (victim->f_digest)
  132. crypto_free_digest_env(victim->f_digest);
  133. if (victim->b_digest)
  134. crypto_free_digest_env(victim->b_digest);
  135. if (victim->handshake_state)
  136. crypto_dh_free(victim->handshake_state);
  137. tor_free(victim);
  138. }
  139. /** Return a circ such that:
  140. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  141. * - circ is attached to <b>conn</b>, either as p_conn, n-conn, or
  142. * in p_streams or n_streams.
  143. * Return NULL if no such circuit exists.
  144. */
  145. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
  146. circuit_t *circ;
  147. connection_t *tmpconn;
  148. for (circ=global_circuitlist;circ;circ = circ->next) {
  149. if (circ->marked_for_close)
  150. continue;
  151. if (circ->p_circ_id == circ_id) {
  152. if (circ->p_conn == conn)
  153. return circ;
  154. for (tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  155. if (tmpconn == conn)
  156. return circ;
  157. }
  158. }
  159. if (circ->n_circ_id == circ_id) {
  160. if (circ->n_conn == conn)
  161. return circ;
  162. for (tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  163. if (tmpconn == conn)
  164. return circ;
  165. }
  166. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  167. if (tmpconn == conn)
  168. return circ;
  169. }
  170. }
  171. }
  172. return NULL;
  173. }
  174. /** Return a circ such that circ is attached to <b>conn</b>, either as
  175. * p_conn, n-conn, or in p_streams or n_streams or resolving_streams.
  176. *
  177. * Return NULL if no such circuit exists.
  178. */
  179. circuit_t *circuit_get_by_conn(connection_t *conn) {
  180. circuit_t *circ;
  181. connection_t *tmpconn;
  182. for (circ=global_circuitlist;circ;circ = circ->next) {
  183. if (circ->marked_for_close)
  184. continue;
  185. if (circ->p_conn == conn)
  186. return circ;
  187. if (circ->n_conn == conn)
  188. return circ;
  189. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  190. if (tmpconn == conn)
  191. return circ;
  192. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  193. if (tmpconn == conn)
  194. return circ;
  195. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  196. if (tmpconn == conn)
  197. return circ;
  198. }
  199. return NULL;
  200. }
  201. /** Return a circ such that:
  202. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  203. * - circ-\>purpose is equal to <b>purpose</b>.
  204. *
  205. * Return NULL if no such circuit exists.
  206. */
  207. circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose) {
  208. circuit_t *circ;
  209. for (circ = global_circuitlist; circ; circ = circ->next) {
  210. if (!circ->marked_for_close &&
  211. circ->purpose == purpose &&
  212. !rend_cmp_service_ids(rend_query, circ->rend_query))
  213. return circ;
  214. }
  215. return NULL;
  216. }
  217. /** Return the first circuit in global_circuitlist after <b>start</b> whose
  218. * rend_pk_digest field is <b>digest</b> and whose purpose is <b>purpose</b>. Returns
  219. * NULL if no circuit is found. If <b>start</b> is NULL, begin at the start of
  220. * the list.
  221. */
  222. circuit_t *
  223. circuit_get_next_by_pk_and_purpose(circuit_t *start,
  224. const char *digest, uint8_t purpose)
  225. {
  226. circuit_t *circ;
  227. if (start == NULL)
  228. circ = global_circuitlist;
  229. else
  230. circ = start->next;
  231. for ( ; circ; circ = circ->next) {
  232. if (circ->marked_for_close)
  233. continue;
  234. if (circ->purpose != purpose)
  235. continue;
  236. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  237. return circ;
  238. }
  239. return NULL;
  240. }
  241. /** Return the circuit waiting for a rendezvous with the provided cookie.
  242. * Return NULL if no such circuit is found.
  243. */
  244. circuit_t *circuit_get_rendezvous(const char *cookie)
  245. {
  246. circuit_t *circ;
  247. for (circ = global_circuitlist; circ; circ = circ->next) {
  248. if (! circ->marked_for_close &&
  249. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  250. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  251. return circ;
  252. }
  253. return NULL;
  254. }
  255. /** Count the number of circs originating here that aren't open, and
  256. * that have the specified <b>purpose</b>. */
  257. int circuit_count_building(uint8_t purpose) {
  258. circuit_t *circ;
  259. int num=0;
  260. for (circ=global_circuitlist;circ;circ = circ->next) {
  261. if (CIRCUIT_IS_ORIGIN(circ) &&
  262. circ->state != CIRCUIT_STATE_OPEN &&
  263. circ->purpose == purpose &&
  264. !circ->marked_for_close)
  265. num++;
  266. }
  267. return num;
  268. }
  269. /** Return the circuit that is open, has specified <b>purpose</b>,
  270. * has a timestamp_dirty value of 0, and was created most recently,
  271. * or NULL if no circuit fits this description.
  272. */
  273. circuit_t *
  274. circuit_get_youngest_clean_open(uint8_t purpose) {
  275. circuit_t *circ;
  276. circuit_t *youngest=NULL;
  277. for (circ=global_circuitlist;circ;circ = circ->next) {
  278. if (CIRCUIT_IS_ORIGIN(circ) &&
  279. circ->state == CIRCUIT_STATE_OPEN &&
  280. !circ->marked_for_close &&
  281. circ->purpose == purpose &&
  282. !circ->timestamp_dirty &&
  283. (!youngest || youngest->timestamp_created < circ->timestamp_created))
  284. youngest = circ;
  285. }
  286. return youngest;
  287. }
  288. /** Mark <b>circ</b> to be closed next time we call
  289. * circuit_close_all_marked(). Do any cleanup needed:
  290. * - If state is onionskin_pending, remove circ from the onion_pending
  291. * list.
  292. * - If circ isn't open yet: call circuit_build_failed() if we're
  293. * the origin, and in either case call circuit_rep_hist_note_result()
  294. * to note stats.
  295. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  296. * just tried from our list of intro points for that service
  297. * descriptor.
  298. * - Send appropriate destroys and edge_destroys for conns and
  299. * streams attached to circ.
  300. * - If circ->rend_splice is set (we are the midpoint of a joined
  301. * rendezvous stream), then mark the other circuit to close as well.
  302. */
  303. int _circuit_mark_for_close(circuit_t *circ) {
  304. connection_t *conn;
  305. assert_circuit_ok(circ);
  306. if (circ->marked_for_close)
  307. return -1;
  308. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  309. onion_pending_remove(circ);
  310. }
  311. /* If the circuit ever became OPEN, we sent it to the reputation history
  312. * module then. If it isn't OPEN, we send it there now to remember which
  313. * links worked and which didn't.
  314. */
  315. if (circ->state != CIRCUIT_STATE_OPEN) {
  316. if (CIRCUIT_IS_ORIGIN(circ)) {
  317. circuit_build_failed(circ); /* take actions if necessary */
  318. }
  319. circuit_rep_hist_note_result(circ);
  320. }
  321. if (CIRCUIT_IS_ORIGIN(circ)) {
  322. control_event_circuit_status(circ,
  323. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
  324. }
  325. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  326. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  327. /* treat this like getting a nack from it */
  328. log_fn(LOG_INFO,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
  329. circ->rend_query, circ->build_state->chosen_exit_name);
  330. rend_client_remove_intro_point(circ->build_state->chosen_exit_name, circ->rend_query);
  331. }
  332. if (circ->n_conn)
  333. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  334. for (conn=circ->n_streams; conn; conn=conn->next_stream)
  335. connection_edge_destroy(circ->n_circ_id, conn);
  336. while (circ->resolving_streams) {
  337. conn = circ->resolving_streams;
  338. circ->resolving_streams = conn->next_stream;
  339. connection_dns_remove(conn); /* remove it from resolve lists */
  340. log_fn(LOG_INFO,"Freeing resolving-conn.");
  341. connection_free(conn);
  342. }
  343. if (circ->p_conn)
  344. connection_send_destroy(circ->p_circ_id, circ->p_conn);
  345. for (conn=circ->p_streams; conn; conn=conn->next_stream)
  346. connection_edge_destroy(circ->p_circ_id, conn);
  347. circ->marked_for_close = 1;
  348. if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
  349. /* do this after marking this circuit, to avoid infinite recursion. */
  350. circuit_mark_for_close(circ->rend_splice);
  351. circ->rend_splice = NULL;
  352. }
  353. return 0;
  354. }
  355. /** Verify that cpath layer <b>cp</b> has all of its invariants
  356. * correct. Trigger an assert if anything is invalid.
  357. */
  358. void assert_cpath_layer_ok(const crypt_path_t *cp)
  359. {
  360. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  361. // tor_assert(cp->port);
  362. switch (cp->state)
  363. {
  364. case CPATH_STATE_OPEN:
  365. tor_assert(cp->f_crypto);
  366. tor_assert(cp->b_crypto);
  367. /* fall through */
  368. case CPATH_STATE_CLOSED:
  369. tor_assert(!cp->handshake_state);
  370. break;
  371. case CPATH_STATE_AWAITING_KEYS:
  372. tor_assert(cp->handshake_state);
  373. break;
  374. default:
  375. log_fn(LOG_ERR,"Unexpected state %d",cp->state);
  376. tor_assert(0);
  377. }
  378. tor_assert(cp->package_window >= 0);
  379. tor_assert(cp->deliver_window >= 0);
  380. }
  381. /** Verify that cpath <b>cp</b> has all of its invariants
  382. * correct. Trigger an assert if anything is invalid.
  383. */
  384. static void
  385. assert_cpath_ok(const crypt_path_t *cp)
  386. {
  387. const crypt_path_t *start = cp;
  388. do {
  389. assert_cpath_layer_ok(cp);
  390. /* layers must be in sequence of: "open* awaiting? closed*" */
  391. if (cp != start) {
  392. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  393. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  394. } else if (cp->state == CPATH_STATE_OPEN) {
  395. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  396. }
  397. }
  398. cp = cp->next;
  399. tor_assert(cp);
  400. } while (cp != start);
  401. }
  402. /** Verify that circuit <b>c</b> has all of its invariants
  403. * correct. Trigger an assert if anything is invalid.
  404. */
  405. void assert_circuit_ok(const circuit_t *c)
  406. {
  407. connection_t *conn;
  408. tor_assert(c);
  409. tor_assert(c->magic == CIRCUIT_MAGIC);
  410. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  411. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  412. if (c->n_conn) {
  413. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  414. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest, DIGEST_LEN));
  415. }
  416. if (c->p_conn)
  417. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  418. for (conn = c->p_streams; conn; conn = conn->next_stream)
  419. tor_assert(conn->type == CONN_TYPE_AP);
  420. for (conn = c->n_streams; conn; conn = conn->next_stream)
  421. tor_assert(conn->type == CONN_TYPE_EXIT);
  422. tor_assert(c->deliver_window >= 0);
  423. tor_assert(c->package_window >= 0);
  424. if (c->state == CIRCUIT_STATE_OPEN) {
  425. if (c->cpath) {
  426. tor_assert(CIRCUIT_IS_ORIGIN(c));
  427. tor_assert(!c->n_crypto);
  428. tor_assert(!c->p_crypto);
  429. tor_assert(!c->n_digest);
  430. tor_assert(!c->p_digest);
  431. } else {
  432. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  433. tor_assert(c->n_crypto);
  434. tor_assert(c->p_crypto);
  435. tor_assert(c->n_digest);
  436. tor_assert(c->p_digest);
  437. }
  438. }
  439. if (c->cpath) {
  440. assert_cpath_ok(c->cpath);
  441. }
  442. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  443. if (!c->marked_for_close) {
  444. tor_assert(c->rend_splice);
  445. tor_assert(c->rend_splice->rend_splice == c);
  446. }
  447. tor_assert(c->rend_splice != c);
  448. } else {
  449. tor_assert(!c->rend_splice);
  450. }
  451. }