circuitlist.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 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. /* Define RB_AUGMENT to avoid warnings about if statements with emtpy bodies.
  13. */
  14. #define RB_AUGMENT(x) do{}while(0)
  15. #include "tree.h"
  16. /********* START VARIABLES **********/
  17. /** A global list of all circuits at this hop. */
  18. circuit_t *global_circuitlist=NULL;
  19. static void circuit_free(circuit_t *circ);
  20. static void circuit_free_cpath(crypt_path_t *cpath);
  21. static void circuit_free_cpath_node(crypt_path_t *victim);
  22. /********* END VARIABLES ************/
  23. /** A map from OR connection and circuit ID to circuit. (Lookup performance is
  24. * very important here, since we need to do it every time a cell arrives.) */
  25. typedef struct orconn_circid_circuit_map_t {
  26. RB_ENTRY(orconn_circid_circuit_map_t) node;
  27. connection_t *or_conn;
  28. uint16_t circ_id;
  29. circuit_t *circuit;
  30. } orconn_circid_circuit_map_t;
  31. /** Helper for RB tree: compare the OR connection and circuit ID for a and b,
  32. * and return less than, equal to, or greater than zero appropriately.
  33. */
  34. static INLINE int
  35. compare_orconn_circid_entries(orconn_circid_circuit_map_t *a,
  36. orconn_circid_circuit_map_t *b)
  37. {
  38. if (a->or_conn < b->or_conn)
  39. return -1;
  40. else if (a->or_conn > b->or_conn)
  41. return 1;
  42. else
  43. return ((int)b->circ_id) - ((int)a->circ_id);
  44. };
  45. static RB_HEAD(orconn_circid_tree, orconn_circid_circuit_map_t) orconn_circid_circuit_map = RB_INITIALIZER(orconn_circid_circuit_map);
  46. RB_PROTOTYPE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
  47. RB_GENERATE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
  48. /** The most recently returned entry from circuit_get_by_circid_orconn;
  49. * used to improve performance when many cells arrive in a row from the
  50. * same circuit.
  51. */
  52. /* (We tried using splay trees, but round-robin turned out to make them
  53. * suck.) */
  54. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  55. /** Set the p_conn or n_conn field of a circuit <b>circ</b>, along
  56. * with the corresponding circuit ID, and add the circuit as appropriate
  57. * to the (orconn,id)-\>circuit map. */
  58. void
  59. circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
  60. connection_t *conn,
  61. enum which_conn_changed_t which)
  62. {
  63. uint16_t old_id;
  64. connection_t *old_conn;
  65. orconn_circid_circuit_map_t search;
  66. orconn_circid_circuit_map_t *found;
  67. tor_assert(!conn || conn->type == CONN_TYPE_OR);
  68. if (which == P_CONN_CHANGED) {
  69. old_id = circ->p_circ_id;
  70. old_conn = circ->p_conn;
  71. circ->p_circ_id = id;
  72. circ->p_conn = conn;
  73. } else {
  74. old_id = circ->n_circ_id;
  75. old_conn = circ->n_conn;
  76. circ->n_circ_id = id;
  77. circ->n_conn = conn;
  78. }
  79. if (_last_circid_orconn_ent &&
  80. ((old_id == _last_circid_orconn_ent->circ_id &&
  81. old_conn == _last_circid_orconn_ent->or_conn) ||
  82. (id == _last_circid_orconn_ent->circ_id &&
  83. conn == _last_circid_orconn_ent->or_conn))) {
  84. _last_circid_orconn_ent = NULL;
  85. }
  86. if (old_conn) {
  87. search.circ_id = old_id;
  88. search.or_conn = old_conn;
  89. found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
  90. if (found) {
  91. RB_REMOVE(orconn_circid_tree, &orconn_circid_circuit_map, found);
  92. }
  93. tor_free(found);
  94. }
  95. if (conn == NULL)
  96. return;
  97. search.circ_id = id;
  98. search.or_conn = conn;
  99. found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
  100. if (found) {
  101. found->circuit = circ;
  102. } else {
  103. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  104. found->circ_id = id;
  105. found->or_conn = conn;
  106. found->circuit = circ;
  107. RB_INSERT(orconn_circid_tree, &orconn_circid_circuit_map, found);
  108. }
  109. }
  110. /** Add <b>circ</b> to the global list of circuits. This is called only from
  111. * within circuit_new.
  112. */
  113. static void
  114. circuit_add(circuit_t *circ)
  115. {
  116. if (!global_circuitlist) { /* first one */
  117. global_circuitlist = circ;
  118. circ->next = NULL;
  119. } else {
  120. circ->next = global_circuitlist;
  121. global_circuitlist = circ;
  122. }
  123. }
  124. /** Detach from the global circuit list, and deallocate, all
  125. * circuits that have been marked for close.
  126. */
  127. void
  128. circuit_close_all_marked(void)
  129. {
  130. circuit_t *tmp,*m;
  131. while (global_circuitlist && global_circuitlist->marked_for_close) {
  132. tmp = global_circuitlist->next;
  133. circuit_free(global_circuitlist);
  134. global_circuitlist = tmp;
  135. }
  136. tmp = global_circuitlist;
  137. while (tmp && tmp->next) {
  138. if (tmp->next->marked_for_close) {
  139. m = tmp->next->next;
  140. circuit_free(tmp->next);
  141. tmp->next = m;
  142. /* Need to check new tmp->next; don't advance tmp. */
  143. } else {
  144. /* Advance tmp. */
  145. tmp = tmp->next;
  146. }
  147. }
  148. }
  149. /** Return the head of the global linked list of circuits. **/
  150. circuit_t *
  151. _circuit_get_global_list(void)
  152. {
  153. return global_circuitlist;
  154. }
  155. /** Function to make circ-\>state human-readable */
  156. const char *
  157. circuit_state_to_string(int state)
  158. {
  159. static char buf[64];
  160. switch (state) {
  161. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  162. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  163. case CIRCUIT_STATE_OR_WAIT: return "connecting to firsthop";
  164. case CIRCUIT_STATE_OPEN: return "open";
  165. default:
  166. warn(LD_BUG, "Bug: unknown circuit state %d", state);
  167. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  168. return buf;
  169. }
  170. }
  171. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  172. * and <b>p_conn</b>. Add it to the global circuit list.
  173. */
  174. circuit_t *
  175. circuit_new(uint16_t p_circ_id, connection_t *p_conn)
  176. {
  177. circuit_t *circ;
  178. static uint32_t n_circuits_allocated = 1;
  179. /* never zero, since a global ID of 0 is treated specially by the
  180. * controller */
  181. circ = tor_malloc_zero(sizeof(circuit_t));
  182. circ->magic = CIRCUIT_MAGIC;
  183. circ->timestamp_created = time(NULL);
  184. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  185. /* CircIDs */
  186. if (p_conn) {
  187. circuit_set_circid_orconn(circ, p_circ_id, p_conn, P_CONN_CHANGED);
  188. }
  189. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  190. circ->package_window = CIRCWINDOW_START;
  191. circ->deliver_window = CIRCWINDOW_START;
  192. circ->next_stream_id = crypto_rand_int(1<<16);
  193. circ->global_identifier = n_circuits_allocated++;
  194. circuit_add(circ);
  195. return circ;
  196. }
  197. /** Deallocate space associated with circ.
  198. */
  199. static void
  200. circuit_free(circuit_t *circ)
  201. {
  202. tor_assert(circ);
  203. tor_assert(circ->magic == CIRCUIT_MAGIC);
  204. if (circ->n_crypto)
  205. crypto_free_cipher_env(circ->n_crypto);
  206. if (circ->p_crypto)
  207. crypto_free_cipher_env(circ->p_crypto);
  208. if (circ->n_digest)
  209. crypto_free_digest_env(circ->n_digest);
  210. if (circ->p_digest)
  211. crypto_free_digest_env(circ->p_digest);
  212. if (circ->build_state) {
  213. if (circ->build_state->chosen_exit)
  214. extend_info_free(circ->build_state->chosen_exit);
  215. if (circ->build_state->pending_final_cpath)
  216. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  217. }
  218. tor_free(circ->build_state);
  219. tor_free(circ->onionskin);
  220. circuit_free_cpath(circ->cpath);
  221. if (circ->rend_splice) {
  222. circ->rend_splice->rend_splice = NULL;
  223. }
  224. /* Remove from map. */
  225. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  226. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  227. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  228. tor_free(circ);
  229. }
  230. /** Deallocate space associated with the linked list <b>cpath</b>. */
  231. static void
  232. circuit_free_cpath(crypt_path_t *cpath)
  233. {
  234. crypt_path_t *victim, *head=cpath;
  235. if (!cpath)
  236. return;
  237. /* it's a doubly linked list, so we have to notice when we've
  238. * gone through it once. */
  239. while (cpath->next && cpath->next != head) {
  240. victim = cpath;
  241. cpath = victim->next;
  242. circuit_free_cpath_node(victim);
  243. }
  244. circuit_free_cpath_node(cpath);
  245. }
  246. /** Release all storage held by circuits. */
  247. void
  248. circuit_free_all(void)
  249. {
  250. circuit_t *next;
  251. while (global_circuitlist) {
  252. next = global_circuitlist->next;
  253. while (global_circuitlist->resolving_streams) {
  254. connection_t *next;
  255. next = global_circuitlist->resolving_streams->next_stream;
  256. connection_free(global_circuitlist->resolving_streams);
  257. global_circuitlist->resolving_streams = next;
  258. }
  259. circuit_free(global_circuitlist);
  260. global_circuitlist = next;
  261. }
  262. }
  263. /** Deallocate space associated with the cpath node <b>victim</b>. */
  264. static void
  265. circuit_free_cpath_node(crypt_path_t *victim)
  266. {
  267. if (victim->f_crypto)
  268. crypto_free_cipher_env(victim->f_crypto);
  269. if (victim->b_crypto)
  270. crypto_free_cipher_env(victim->b_crypto);
  271. if (victim->f_digest)
  272. crypto_free_digest_env(victim->f_digest);
  273. if (victim->b_digest)
  274. crypto_free_digest_env(victim->b_digest);
  275. if (victim->dh_handshake_state)
  276. crypto_dh_free(victim->dh_handshake_state);
  277. if (victim->extend_info)
  278. extend_info_free(victim->extend_info);
  279. victim->magic = 0xDEADBEEFu;
  280. tor_free(victim);
  281. }
  282. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  283. * such circuit exists. */
  284. circuit_t *
  285. circuit_get_by_global_id(uint32_t id)
  286. {
  287. circuit_t *circ;
  288. for (circ=global_circuitlist;circ;circ = circ->next) {
  289. if (circ->global_identifier == id) {
  290. if (circ->marked_for_close)
  291. return NULL;
  292. else
  293. return circ;
  294. }
  295. }
  296. return NULL;
  297. }
  298. /** Return a circ such that:
  299. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  300. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  301. * Return NULL if no such circuit exists.
  302. */
  303. static INLINE circuit_t *
  304. circuit_get_by_circid_orconn_impl(uint16_t circ_id, connection_t *conn)
  305. {
  306. orconn_circid_circuit_map_t search;
  307. orconn_circid_circuit_map_t *found;
  308. tor_assert(conn->type == CONN_TYPE_OR);
  309. if (_last_circid_orconn_ent &&
  310. circ_id == _last_circid_orconn_ent->circ_id &&
  311. conn == _last_circid_orconn_ent->or_conn) {
  312. found = _last_circid_orconn_ent;
  313. } else {
  314. search.circ_id = circ_id;
  315. search.or_conn = conn;
  316. found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
  317. _last_circid_orconn_ent = found;
  318. }
  319. if (found && found->circuit)
  320. return found->circuit;
  321. /* The rest of this can be replaced with
  322. "return NULL;" once we believe the code works. */
  323. {
  324. circuit_t *circ;
  325. for (circ=global_circuitlist;circ;circ = circ->next) {
  326. if (circ->p_conn == conn && circ->p_circ_id == circ_id) {
  327. warn(LD_BUG, "circuit matches p_conn, but not in tree (Bug!)");
  328. return circ;
  329. }
  330. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  331. warn(LD_BUG, "circuit matches n_conn, but not in tree (Bug!)");
  332. return circ;
  333. }
  334. }
  335. return NULL;
  336. }
  337. }
  338. /** Return a circ such that:
  339. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  340. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  341. * - circ is not marked for close.
  342. * Return NULL if no such circuit exists.
  343. */
  344. circuit_t *
  345. circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn)
  346. {
  347. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  348. if (!circ || circ->marked_for_close)
  349. return NULL;
  350. else
  351. return circ;
  352. }
  353. /** Return true iff there is a circ such that
  354. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  355. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  356. * Return NULL if no such circuit exists.
  357. */
  358. int
  359. circuit_id_used_on_conn(uint16_t circ_id, connection_t *conn)
  360. {
  361. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  362. if (circ && circ->marked_for_close)
  363. log_fn(LOG_NOTICE, LD_CIRC,
  364. "I was about to re-use a circuit ID that had been marked."
  365. " Good thing we fixed that bug!");
  366. return circ != NULL;
  367. }
  368. /** Return the circuit that a given edge connection is using. */
  369. circuit_t *
  370. circuit_get_by_edge_conn(connection_t *conn)
  371. {
  372. circuit_t *circ;
  373. #if 0
  374. connection_t *tmpconn;
  375. #endif
  376. tor_assert(CONN_IS_EDGE(conn));
  377. if (! conn->on_circuit) {
  378. /* return NULL; */
  379. circ = circuit_get_by_conn(conn);
  380. if (circ) {
  381. warn(LD_BUG, "BUG: conn->on_circuit==NULL, but there was in fact a circuit there.");
  382. }
  383. return circ;
  384. }
  385. circ = conn->on_circuit;
  386. tor_assert(circ->magic == CIRCUIT_MAGIC);
  387. #if 0
  388. /* All this stuff here is sanity-checking. */
  389. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  390. if (tmpconn == conn)
  391. return circ;
  392. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  393. if (tmpconn == conn)
  394. return circ;
  395. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  396. if (tmpconn == conn)
  397. return circ;
  398. tor_assert(0);
  399. #endif
  400. return circ;
  401. }
  402. /** Return a circ such that circ is attached to <b>conn</b>, either as
  403. * p_conn, n_conn, or in p_streams or n_streams or resolving_streams.
  404. *
  405. * Return NULL if no such circuit exists.
  406. */
  407. circuit_t *
  408. circuit_get_by_conn(connection_t *conn)
  409. {
  410. circuit_t *circ;
  411. connection_t *tmpconn;
  412. for (circ=global_circuitlist;circ;circ = circ->next) {
  413. if (circ->marked_for_close)
  414. continue;
  415. if (circ->p_conn == conn)
  416. return circ;
  417. if (circ->n_conn == conn)
  418. return circ;
  419. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  420. if (tmpconn == conn)
  421. return circ;
  422. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  423. if (tmpconn == conn)
  424. return circ;
  425. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  426. if (tmpconn == conn)
  427. return circ;
  428. }
  429. return NULL;
  430. }
  431. /** Return a circ such that:
  432. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  433. * - circ-\>purpose is equal to <b>purpose</b>.
  434. *
  435. * Return NULL if no such circuit exists.
  436. */
  437. circuit_t *
  438. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  439. {
  440. circuit_t *circ;
  441. for (circ = global_circuitlist; circ; circ = circ->next) {
  442. if (!circ->marked_for_close &&
  443. circ->purpose == purpose &&
  444. !rend_cmp_service_ids(rend_query, circ->rend_query))
  445. return circ;
  446. }
  447. return NULL;
  448. }
  449. /** Return the first circuit in global_circuitlist after <b>start</b>
  450. * whose rend_pk_digest field is <b>digest</b> and whose purpose is
  451. * <b>purpose</b>. Returns NULL if no circuit is found.
  452. * If <b>start</b> is NULL, begin at the start of the list.
  453. */
  454. circuit_t *
  455. circuit_get_next_by_pk_and_purpose(circuit_t *start,
  456. const char *digest, uint8_t purpose)
  457. {
  458. circuit_t *circ;
  459. if (start == NULL)
  460. circ = global_circuitlist;
  461. else
  462. circ = start->next;
  463. for ( ; circ; circ = circ->next) {
  464. if (circ->marked_for_close)
  465. continue;
  466. if (circ->purpose != purpose)
  467. continue;
  468. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  469. return circ;
  470. }
  471. return NULL;
  472. }
  473. /** Return the circuit waiting for a rendezvous with the provided cookie.
  474. * Return NULL if no such circuit is found.
  475. */
  476. circuit_t *
  477. circuit_get_rendezvous(const char *cookie)
  478. {
  479. circuit_t *circ;
  480. for (circ = global_circuitlist; circ; circ = circ->next) {
  481. if (! circ->marked_for_close &&
  482. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  483. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  484. return circ;
  485. }
  486. return NULL;
  487. }
  488. /** Return a circuit that is open, has specified <b>purpose</b>,
  489. * has a timestamp_dirty value of 0, and is uptime/capacity/internal
  490. * if required; or NULL if no circuit fits this description.
  491. *
  492. * Avoid returning need_uptime circuits if not necessary.
  493. * FFFF As a more important goal, not yet implemented, avoid returning
  494. * internal circuits if not necessary.
  495. */
  496. circuit_t *
  497. circuit_get_clean_open(uint8_t purpose, int need_uptime,
  498. int need_capacity, int internal)
  499. {
  500. circuit_t *circ;
  501. circuit_t *best=NULL;
  502. debug(LD_CIRC,"Hunting for a circ to cannibalize: purpose %d, uptime %d, capacity %d, internal %d", purpose, need_uptime, need_capacity, internal);
  503. for (circ=global_circuitlist; circ; circ = circ->next) {
  504. if (CIRCUIT_IS_ORIGIN(circ) &&
  505. circ->state == CIRCUIT_STATE_OPEN &&
  506. !circ->marked_for_close &&
  507. circ->purpose == purpose &&
  508. !circ->timestamp_dirty &&
  509. (!need_uptime || circ->build_state->need_uptime) &&
  510. (!need_capacity || circ->build_state->need_capacity) &&
  511. (!internal || circ->build_state->is_internal)) {
  512. if (!best || (best->build_state->need_uptime && !need_uptime))
  513. best = circ;
  514. }
  515. }
  516. return best;
  517. }
  518. /** Go through the circuitlist; mark-for-close each circuit that starts
  519. * at us but has not yet been used. */
  520. void
  521. circuit_mark_all_unused_circs(void)
  522. {
  523. circuit_t *circ;
  524. for (circ=global_circuitlist; circ; circ = circ->next) {
  525. if (CIRCUIT_IS_ORIGIN(circ) &&
  526. !circ->marked_for_close &&
  527. !circ->timestamp_dirty)
  528. circuit_mark_for_close(circ);
  529. }
  530. }
  531. /** Go through the circuitlist; for each circuit that starts at us
  532. * and is dirty, frob its timestamp_dirty so we won't use it for any
  533. * new streams.
  534. *
  535. * This is useful for letting the user change pseudonyms, so new
  536. * streams will not be linkable to old streams.
  537. */
  538. void
  539. circuit_expire_all_dirty_circs(void)
  540. {
  541. circuit_t *circ;
  542. or_options_t *options = get_options();
  543. for (circ=global_circuitlist; circ; circ = circ->next) {
  544. if (CIRCUIT_IS_ORIGIN(circ) &&
  545. !circ->marked_for_close &&
  546. circ->timestamp_dirty)
  547. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  548. }
  549. }
  550. /** Mark <b>circ</b> to be closed next time we call
  551. * circuit_close_all_marked(). Do any cleanup needed:
  552. * - If state is onionskin_pending, remove circ from the onion_pending
  553. * list.
  554. * - If circ isn't open yet: call circuit_build_failed() if we're
  555. * the origin, and in either case call circuit_rep_hist_note_result()
  556. * to note stats.
  557. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  558. * just tried from our list of intro points for that service
  559. * descriptor.
  560. * - Send appropriate destroys and edge_destroys for conns and
  561. * streams attached to circ.
  562. * - If circ->rend_splice is set (we are the midpoint of a joined
  563. * rendezvous stream), then mark the other circuit to close as well.
  564. */
  565. void
  566. _circuit_mark_for_close(circuit_t *circ, int line, const char *file)
  567. {
  568. connection_t *conn;
  569. assert_circuit_ok(circ);
  570. tor_assert(line);
  571. tor_assert(file);
  572. if (circ->marked_for_close) {
  573. log(LOG_WARN,LD_BUG,
  574. "Duplicate call to circuit_mark_for_close at %s:%d"
  575. " (first at %s:%d)", file, line,
  576. circ->marked_for_close_file, circ->marked_for_close);
  577. return;
  578. }
  579. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  580. onion_pending_remove(circ);
  581. }
  582. /* If the circuit ever became OPEN, we sent it to the reputation history
  583. * module then. If it isn't OPEN, we send it there now to remember which
  584. * links worked and which didn't.
  585. */
  586. if (circ->state != CIRCUIT_STATE_OPEN) {
  587. if (CIRCUIT_IS_ORIGIN(circ)) {
  588. circuit_build_failed(circ); /* take actions if necessary */
  589. }
  590. circuit_rep_hist_note_result(circ);
  591. }
  592. if (CIRCUIT_IS_ORIGIN(circ)) {
  593. control_event_circuit_status(circ,
  594. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
  595. }
  596. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  597. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  598. tor_assert(circ->build_state->chosen_exit);
  599. /* treat this like getting a nack from it */
  600. info(LD_REND,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
  601. safe_str(circ->rend_query),
  602. safe_str(build_state_get_exit_nickname(circ->build_state)));
  603. rend_client_remove_intro_point(circ->build_state->chosen_exit,
  604. circ->rend_query);
  605. }
  606. if (circ->n_conn)
  607. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  608. for (conn=circ->n_streams; conn; conn=conn->next_stream)
  609. connection_edge_destroy(circ->n_circ_id, conn);
  610. while (circ->resolving_streams) {
  611. conn = circ->resolving_streams;
  612. circ->resolving_streams = conn->next_stream;
  613. if (!conn->marked_for_close) {
  614. /* The other side will see a DESTROY, and infer that the connections
  615. * are closing because the circuit is getting torn down. No need
  616. * to send an end cell*/
  617. conn->has_sent_end = 1; /* we're closing the circuit, nothing to send to */
  618. connection_mark_for_close(conn);
  619. }
  620. conn->on_circuit = NULL;
  621. }
  622. if (circ->p_conn)
  623. connection_send_destroy(circ->p_circ_id, circ->p_conn);
  624. for (conn=circ->p_streams; conn; conn=conn->next_stream)
  625. connection_edge_destroy(circ->p_circ_id, conn);
  626. circ->marked_for_close = line;
  627. circ->marked_for_close_file = file;
  628. if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
  629. /* do this after marking this circuit, to avoid infinite recursion. */
  630. circuit_mark_for_close(circ->rend_splice);
  631. circ->rend_splice = NULL;
  632. }
  633. }
  634. /** Verify that cpath layer <b>cp</b> has all of its invariants
  635. * correct. Trigger an assert if anything is invalid.
  636. */
  637. void
  638. assert_cpath_layer_ok(const crypt_path_t *cp)
  639. {
  640. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  641. // tor_assert(cp->port);
  642. tor_assert(cp);
  643. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  644. switch (cp->state)
  645. {
  646. case CPATH_STATE_OPEN:
  647. tor_assert(cp->f_crypto);
  648. tor_assert(cp->b_crypto);
  649. /* fall through */
  650. case CPATH_STATE_CLOSED:
  651. tor_assert(!cp->dh_handshake_state);
  652. break;
  653. case CPATH_STATE_AWAITING_KEYS:
  654. /* tor_assert(cp->dh_handshake_state); */
  655. break;
  656. default:
  657. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  658. tor_assert(0);
  659. }
  660. tor_assert(cp->package_window >= 0);
  661. tor_assert(cp->deliver_window >= 0);
  662. }
  663. /** Verify that cpath <b>cp</b> has all of its invariants
  664. * correct. Trigger an assert if anything is invalid.
  665. */
  666. static void
  667. assert_cpath_ok(const crypt_path_t *cp)
  668. {
  669. const crypt_path_t *start = cp;
  670. do {
  671. assert_cpath_layer_ok(cp);
  672. /* layers must be in sequence of: "open* awaiting? closed*" */
  673. if (cp != start) {
  674. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  675. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  676. } else if (cp->state == CPATH_STATE_OPEN) {
  677. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  678. }
  679. }
  680. cp = cp->next;
  681. tor_assert(cp);
  682. } while (cp != start);
  683. }
  684. /** Verify that circuit <b>c</b> has all of its invariants
  685. * correct. Trigger an assert if anything is invalid.
  686. */
  687. void
  688. assert_circuit_ok(const circuit_t *c)
  689. {
  690. connection_t *conn;
  691. tor_assert(c);
  692. tor_assert(c->magic == CIRCUIT_MAGIC);
  693. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  694. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  695. if (c->n_conn) {
  696. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  697. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest, DIGEST_LEN));
  698. if (c->n_circ_id)
  699. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  700. }
  701. if (c->p_conn) {
  702. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  703. if (c->p_circ_id)
  704. tor_assert(c == circuit_get_by_circid_orconn(c->p_circ_id, c->p_conn));
  705. }
  706. for (conn = c->p_streams; conn; conn = conn->next_stream)
  707. tor_assert(conn->type == CONN_TYPE_AP);
  708. for (conn = c->n_streams; conn; conn = conn->next_stream)
  709. tor_assert(conn->type == CONN_TYPE_EXIT);
  710. tor_assert(c->deliver_window >= 0);
  711. tor_assert(c->package_window >= 0);
  712. if (c->state == CIRCUIT_STATE_OPEN) {
  713. tor_assert(!c->onionskin);
  714. if (c->cpath) {
  715. tor_assert(CIRCUIT_IS_ORIGIN(c));
  716. tor_assert(!c->n_crypto);
  717. tor_assert(!c->p_crypto);
  718. tor_assert(!c->n_digest);
  719. tor_assert(!c->p_digest);
  720. } else {
  721. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  722. tor_assert(c->n_crypto);
  723. tor_assert(c->p_crypto);
  724. tor_assert(c->n_digest);
  725. tor_assert(c->p_digest);
  726. }
  727. }
  728. if (c->cpath) {
  729. assert_cpath_ok(c->cpath);
  730. }
  731. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  732. if (!c->marked_for_close) {
  733. tor_assert(c->rend_splice);
  734. tor_assert(c->rend_splice->rend_splice == c);
  735. }
  736. tor_assert(c->rend_splice != c);
  737. } else {
  738. tor_assert(!c->rend_splice);
  739. }
  740. }