circuitlist.c 14 KB

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