circuitlist.c 15 KB

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