circuitlist.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /* Copyright 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 circuitlist_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file circuitlist.c
  10. * \brief Manage the global circuit list.
  11. **/
  12. #include "or.h"
  13. #include "../common/ht.h"
  14. /********* START VARIABLES **********/
  15. /** A global list of all circuits at this hop. */
  16. circuit_t *global_circuitlist=NULL;
  17. /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
  18. smartlist_t *circuits_pending_or_conns=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. HT_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 hash tables: compare the OR connection and circuit ID for a and
  32. * b, and return less than, equal to, or greater than zero appropriately.
  33. */
  34. static INLINE int
  35. _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
  36. orconn_circid_circuit_map_t *b)
  37. {
  38. return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
  39. }
  40. static INLINE unsigned int
  41. _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
  42. {
  43. return (((unsigned)a->circ_id)<<16) ^ (unsigned)(uintptr_t)(a->or_conn);
  44. }
  45. static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
  46. orconn_circid_circuit_map = HT_INITIALIZER();
  47. HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  48. _orconn_circid_entry_hash, _orconn_circid_entries_eq);
  49. HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  50. _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
  51. malloc, realloc, free);
  52. /** The most recently returned entry from circuit_get_by_circid_orconn;
  53. * used to improve performance when many cells arrive in a row from the
  54. * same circuit.
  55. */
  56. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  57. /** Set the p_conn or n_conn field of a circuit <b>circ</b>, along
  58. * with the corresponding circuit ID, and add the circuit as appropriate
  59. * to the (orconn,id)-\>circuit map. */
  60. void
  61. circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
  62. connection_t *conn,
  63. enum which_conn_changed_t which)
  64. {
  65. uint16_t old_id;
  66. connection_t *old_conn;
  67. orconn_circid_circuit_map_t search;
  68. orconn_circid_circuit_map_t *found;
  69. tor_assert(!conn || conn->type == CONN_TYPE_OR);
  70. if (which == P_CONN_CHANGED) {
  71. old_id = circ->p_circ_id;
  72. old_conn = circ->p_conn;
  73. circ->p_circ_id = id;
  74. circ->p_conn = conn;
  75. } else {
  76. old_id = circ->n_circ_id;
  77. old_conn = circ->n_conn;
  78. circ->n_circ_id = id;
  79. circ->n_conn = conn;
  80. }
  81. if (conn == old_conn && old_id == id)
  82. return;
  83. if (_last_circid_orconn_ent &&
  84. ((old_id == _last_circid_orconn_ent->circ_id &&
  85. old_conn == _last_circid_orconn_ent->or_conn) ||
  86. (id == _last_circid_orconn_ent->circ_id &&
  87. conn == _last_circid_orconn_ent->or_conn))) {
  88. _last_circid_orconn_ent = NULL;
  89. }
  90. if (old_conn) { /* we may need to remove it from the conn-circid map */
  91. tor_assert(old_conn->magic == CONNECTION_MAGIC);
  92. search.circ_id = old_id;
  93. search.or_conn = old_conn;
  94. found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
  95. if (found) {
  96. tor_free(found);
  97. --old_conn->n_circuits;
  98. }
  99. }
  100. if (conn == NULL)
  101. return;
  102. /* now add the new one to the conn-circid map */
  103. search.circ_id = id;
  104. search.or_conn = conn;
  105. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  106. if (found) {
  107. found->circuit = circ;
  108. } else {
  109. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  110. found->circ_id = id;
  111. found->or_conn = conn;
  112. found->circuit = circ;
  113. HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
  114. }
  115. ++conn->n_circuits;
  116. }
  117. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  118. * it from lists as appropriate. */
  119. void
  120. circuit_set_state(circuit_t *circ, int state)
  121. {
  122. tor_assert(circ);
  123. if (state == circ->state)
  124. return;
  125. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  126. /* remove from waiting-circuit list. */
  127. if (circuits_pending_or_conns)
  128. smartlist_remove(circuits_pending_or_conns, circ);
  129. }
  130. if (state == CIRCUIT_STATE_OR_WAIT) {
  131. /* add to waiting-circuit list. */
  132. if (!circuits_pending_or_conns)
  133. circuits_pending_or_conns = smartlist_create();
  134. smartlist_add(circuits_pending_or_conns, circ);
  135. }
  136. circ->state = state;
  137. }
  138. /** Add <b>circ</b> to the global list of circuits. This is called only from
  139. * within circuit_new.
  140. */
  141. static void
  142. circuit_add(circuit_t *circ)
  143. {
  144. if (!global_circuitlist) { /* first one */
  145. global_circuitlist = circ;
  146. circ->next = NULL;
  147. } else {
  148. circ->next = global_circuitlist;
  149. global_circuitlist = circ;
  150. }
  151. }
  152. /** Detach from the global circuit list, and deallocate, all
  153. * circuits that have been marked for close.
  154. */
  155. void
  156. circuit_close_all_marked(void)
  157. {
  158. circuit_t *tmp,*m;
  159. while (global_circuitlist && global_circuitlist->marked_for_close) {
  160. tmp = global_circuitlist->next;
  161. circuit_free(global_circuitlist);
  162. global_circuitlist = tmp;
  163. }
  164. tmp = global_circuitlist;
  165. while (tmp && tmp->next) {
  166. if (tmp->next->marked_for_close) {
  167. m = tmp->next->next;
  168. circuit_free(tmp->next);
  169. tmp->next = m;
  170. /* Need to check new tmp->next; don't advance tmp. */
  171. } else {
  172. /* Advance tmp. */
  173. tmp = tmp->next;
  174. }
  175. }
  176. }
  177. /** Return the head of the global linked list of circuits. **/
  178. circuit_t *
  179. _circuit_get_global_list(void)
  180. {
  181. return global_circuitlist;
  182. }
  183. /** Function to make circ-\>state human-readable */
  184. const char *
  185. circuit_state_to_string(int state)
  186. {
  187. static char buf[64];
  188. switch (state) {
  189. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  190. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  191. case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
  192. case CIRCUIT_STATE_OPEN: return "open";
  193. default:
  194. warn(LD_BUG, "Bug: unknown circuit state %d", state);
  195. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  196. return buf;
  197. }
  198. }
  199. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  200. * and <b>p_conn</b>. Add it to the global circuit list.
  201. */
  202. circuit_t *
  203. circuit_new(uint16_t p_circ_id, connection_t *p_conn)
  204. {
  205. circuit_t *circ;
  206. static uint32_t n_circuits_allocated = 1;
  207. /* never zero, since a global ID of 0 is treated specially by the
  208. * controller */
  209. circ = tor_malloc_zero(sizeof(circuit_t));
  210. circ->magic = CIRCUIT_MAGIC;
  211. circ->timestamp_created = time(NULL);
  212. /* CircIDs */
  213. if (p_conn) {
  214. circuit_set_circid_orconn(circ, p_circ_id, p_conn, P_CONN_CHANGED);
  215. }
  216. /* circ->n_circ_id remains 0 because we haven't identified the next hop
  217. * yet */
  218. circ->package_window = CIRCWINDOW_START;
  219. circ->deliver_window = CIRCWINDOW_START;
  220. circ->next_stream_id = crypto_rand_int(1<<16);
  221. circ->global_identifier = n_circuits_allocated++;
  222. circuit_add(circ);
  223. return circ;
  224. }
  225. /** Deallocate space associated with circ.
  226. */
  227. static void
  228. circuit_free(circuit_t *circ)
  229. {
  230. tor_assert(circ);
  231. tor_assert(circ->magic == CIRCUIT_MAGIC);
  232. if (circ->n_crypto)
  233. crypto_free_cipher_env(circ->n_crypto);
  234. if (circ->p_crypto)
  235. crypto_free_cipher_env(circ->p_crypto);
  236. if (circ->n_digest)
  237. crypto_free_digest_env(circ->n_digest);
  238. if (circ->p_digest)
  239. crypto_free_digest_env(circ->p_digest);
  240. if (circ->build_state) {
  241. if (circ->build_state->chosen_exit)
  242. extend_info_free(circ->build_state->chosen_exit);
  243. if (circ->build_state->pending_final_cpath)
  244. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  245. }
  246. tor_free(circ->build_state);
  247. tor_free(circ->onionskin);
  248. circuit_free_cpath(circ->cpath);
  249. if (circ->rend_splice) {
  250. tor_assert(circ->rend_splice->magic == CIRCUIT_MAGIC);
  251. circ->rend_splice->rend_splice = NULL;
  252. }
  253. /* Remove from map. */
  254. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  255. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  256. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  257. tor_free(circ);
  258. }
  259. /** Deallocate space associated with the linked list <b>cpath</b>. */
  260. static void
  261. circuit_free_cpath(crypt_path_t *cpath)
  262. {
  263. crypt_path_t *victim, *head=cpath;
  264. if (!cpath)
  265. return;
  266. /* it's a doubly linked list, so we have to notice when we've
  267. * gone through it once. */
  268. while (cpath->next && cpath->next != head) {
  269. victim = cpath;
  270. cpath = victim->next;
  271. circuit_free_cpath_node(victim);
  272. }
  273. circuit_free_cpath_node(cpath);
  274. }
  275. /** Release all storage held by circuits. */
  276. void
  277. circuit_free_all(void)
  278. {
  279. circuit_t *next;
  280. while (global_circuitlist) {
  281. next = global_circuitlist->next;
  282. while (global_circuitlist->resolving_streams) {
  283. connection_t *next;
  284. next = global_circuitlist->resolving_streams->next_stream;
  285. connection_free(global_circuitlist->resolving_streams);
  286. global_circuitlist->resolving_streams = next;
  287. }
  288. circuit_free(global_circuitlist);
  289. global_circuitlist = next;
  290. }
  291. if (circuits_pending_or_conns) {
  292. smartlist_free(circuits_pending_or_conns);
  293. circuits_pending_or_conns = NULL;
  294. }
  295. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  296. }
  297. /** Deallocate space associated with the cpath node <b>victim</b>. */
  298. static void
  299. circuit_free_cpath_node(crypt_path_t *victim)
  300. {
  301. if (victim->f_crypto)
  302. crypto_free_cipher_env(victim->f_crypto);
  303. if (victim->b_crypto)
  304. crypto_free_cipher_env(victim->b_crypto);
  305. if (victim->f_digest)
  306. crypto_free_digest_env(victim->f_digest);
  307. if (victim->b_digest)
  308. crypto_free_digest_env(victim->b_digest);
  309. if (victim->dh_handshake_state)
  310. crypto_dh_free(victim->dh_handshake_state);
  311. if (victim->extend_info)
  312. extend_info_free(victim->extend_info);
  313. victim->magic = 0xDEADBEEFu;
  314. tor_free(victim);
  315. }
  316. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  317. * such circuit exists. */
  318. circuit_t *
  319. circuit_get_by_global_id(uint32_t id)
  320. {
  321. circuit_t *circ;
  322. for (circ=global_circuitlist;circ;circ = circ->next) {
  323. if (circ->global_identifier == id) {
  324. if (circ->marked_for_close)
  325. return NULL;
  326. else
  327. return circ;
  328. }
  329. }
  330. return NULL;
  331. }
  332. /** Return a circ such that:
  333. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  334. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  335. * Return NULL if no such circuit exists.
  336. */
  337. static INLINE circuit_t *
  338. circuit_get_by_circid_orconn_impl(uint16_t circ_id, connection_t *conn)
  339. {
  340. orconn_circid_circuit_map_t search;
  341. orconn_circid_circuit_map_t *found;
  342. tor_assert(conn->type == CONN_TYPE_OR);
  343. if (_last_circid_orconn_ent &&
  344. circ_id == _last_circid_orconn_ent->circ_id &&
  345. conn == _last_circid_orconn_ent->or_conn) {
  346. found = _last_circid_orconn_ent;
  347. } else {
  348. search.circ_id = circ_id;
  349. search.or_conn = conn;
  350. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  351. _last_circid_orconn_ent = found;
  352. }
  353. if (found && found->circuit)
  354. return found->circuit;
  355. return NULL;
  356. /* The rest of this checks for bugs. Disabled by default. */
  357. {
  358. circuit_t *circ;
  359. for (circ=global_circuitlist;circ;circ = circ->next) {
  360. if (circ->p_conn == conn && circ->p_circ_id == circ_id) {
  361. warn(LD_BUG, "circuit matches p_conn, but not in hash table (Bug!)");
  362. return circ;
  363. }
  364. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  365. warn(LD_BUG, "circuit matches n_conn, but not in hash table (Bug!)");
  366. return circ;
  367. }
  368. }
  369. return NULL;
  370. }
  371. }
  372. /** Return a circ such that:
  373. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  374. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  375. * - circ is not marked for close.
  376. * Return NULL if no such circuit exists.
  377. */
  378. circuit_t *
  379. circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn)
  380. {
  381. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  382. if (!circ || circ->marked_for_close)
  383. return NULL;
  384. else
  385. return circ;
  386. }
  387. /** Return true iff there is a circ such that
  388. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  389. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  390. * Return NULL if no such circuit exists.
  391. */
  392. int
  393. circuit_id_used_on_conn(uint16_t circ_id, connection_t *conn)
  394. {
  395. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  396. if (circ && circ->marked_for_close)
  397. log_fn(LOG_NOTICE, LD_CIRC,
  398. "I was about to re-use a circuit ID that had been marked."
  399. " Good thing we fixed that bug!");
  400. return circ != NULL;
  401. }
  402. /** Return the circuit that a given edge connection is using. */
  403. circuit_t *
  404. circuit_get_by_edge_conn(connection_t *conn)
  405. {
  406. circuit_t *circ;
  407. tor_assert(CONN_IS_EDGE(conn));
  408. circ = conn->on_circuit;
  409. tor_assert(!circ || circ->magic == CIRCUIT_MAGIC);
  410. return circ;
  411. }
  412. /** For each circuits that have <b>conn</b> as n_conn or p_conn, unlink the
  413. * circuit from the orconn,circid map, and mark it for close if it hasn't
  414. * been marked already.
  415. */
  416. void
  417. circuit_unlink_all_from_or_conn(connection_t *conn, int reason)
  418. {
  419. circuit_t *circ;
  420. for (circ = global_circuitlist; circ; circ = circ->next) {
  421. if (circ->n_conn == conn || circ->p_conn == conn) {
  422. if (circ->n_conn == conn)
  423. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  424. if (circ->p_conn == conn)
  425. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  426. if (!circ->marked_for_close)
  427. circuit_mark_for_close(circ, reason);
  428. }
  429. }
  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, is uptime/capacity/internal
  490. * if required, and if info is defined, does not already use info
  491. * as any of its hops; or NULL if no circuit fits this description.
  492. *
  493. * Return need_uptime circuits if that is requested; and if it's not
  494. * requested, return non-uptime circuits if possible, else either.
  495. *
  496. * Only return internal circuits if that is requested.
  497. */
  498. circuit_t *
  499. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  500. int need_uptime,
  501. int need_capacity, int internal)
  502. {
  503. circuit_t *circ;
  504. circuit_t *best=NULL;
  505. debug(LD_CIRC,"Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  506. "capacity %d, internal %d",
  507. purpose, need_uptime, need_capacity, internal);
  508. for (circ=global_circuitlist; circ; circ = circ->next) {
  509. if (CIRCUIT_IS_ORIGIN(circ) &&
  510. circ->state == CIRCUIT_STATE_OPEN &&
  511. !circ->marked_for_close &&
  512. circ->purpose == purpose &&
  513. !circ->timestamp_dirty &&
  514. (!need_uptime || circ->build_state->need_uptime) &&
  515. (!need_capacity || circ->build_state->need_capacity) &&
  516. (internal == circ->build_state->is_internal)) {
  517. if (info) {
  518. /* need to make sure we don't duplicate hops */
  519. crypt_path_t *hop = circ->cpath;
  520. do {
  521. if (!memcmp(hop->extend_info->identity_digest,
  522. info->identity_digest, DIGEST_LEN))
  523. goto next;
  524. hop=hop->next;
  525. } while (hop!=circ->cpath);
  526. }
  527. if (!best || (best->build_state->need_uptime && !need_uptime))
  528. best = circ;
  529. next: ;
  530. }
  531. }
  532. return best;
  533. }
  534. /** Go through the circuitlist; mark-for-close each circuit that starts
  535. * at us but has not yet been used. */
  536. void
  537. circuit_mark_all_unused_circs(void)
  538. {
  539. circuit_t *circ;
  540. for (circ=global_circuitlist; circ; circ = circ->next) {
  541. if (CIRCUIT_IS_ORIGIN(circ) &&
  542. !circ->marked_for_close &&
  543. !circ->timestamp_dirty)
  544. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  545. }
  546. }
  547. /** Go through the circuitlist; for each circuit that starts at us
  548. * and is dirty, frob its timestamp_dirty so we won't use it for any
  549. * new streams.
  550. *
  551. * This is useful for letting the user change pseudonyms, so new
  552. * streams will not be linkable to old streams.
  553. */
  554. void
  555. circuit_expire_all_dirty_circs(void)
  556. {
  557. circuit_t *circ;
  558. or_options_t *options = get_options();
  559. for (circ=global_circuitlist; circ; circ = circ->next) {
  560. if (CIRCUIT_IS_ORIGIN(circ) &&
  561. !circ->marked_for_close &&
  562. circ->timestamp_dirty)
  563. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  564. }
  565. }
  566. /** Mark <b>circ</b> to be closed next time we call
  567. * circuit_close_all_marked(). Do any cleanup needed:
  568. * - If state is onionskin_pending, remove circ from the onion_pending
  569. * list.
  570. * - If circ isn't open yet: call circuit_build_failed() if we're
  571. * the origin, and in either case call circuit_rep_hist_note_result()
  572. * to note stats.
  573. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  574. * just tried from our list of intro points for that service
  575. * descriptor.
  576. * - Send appropriate destroys and edge_destroys for conns and
  577. * streams attached to circ.
  578. * - If circ->rend_splice is set (we are the midpoint of a joined
  579. * rendezvous stream), then mark the other circuit to close as well.
  580. */
  581. void
  582. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  583. const char *file)
  584. {
  585. connection_t *conn;
  586. assert_circuit_ok(circ);
  587. tor_assert(line);
  588. tor_assert(file);
  589. if (circ->marked_for_close) {
  590. log(LOG_WARN,LD_BUG,
  591. "Duplicate call to circuit_mark_for_close at %s:%d"
  592. " (first at %s:%d)", file, line,
  593. circ->marked_for_close_file, circ->marked_for_close);
  594. return;
  595. }
  596. if (reason == END_CIRC_AT_ORIGIN) {
  597. if (!CIRCUIT_IS_ORIGIN(circ)) {
  598. warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  599. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  600. file, line, circ->purpose);
  601. }
  602. reason = END_CIRC_REASON_NONE;
  603. } else if (CIRCUIT_IS_ORIGIN(circ) && reason != END_CIRC_REASON_NONE) {
  604. /* Don't warn about this; there are plenty of places where our code
  605. * is origin-agnosic. */
  606. reason = END_CIRC_REASON_NONE;
  607. }
  608. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  609. warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  610. reason = END_CIRC_REASON_NONE;
  611. }
  612. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  613. onion_pending_remove(circ);
  614. }
  615. /* If the circuit ever became OPEN, we sent it to the reputation history
  616. * module then. If it isn't OPEN, we send it there now to remember which
  617. * links worked and which didn't.
  618. */
  619. if (circ->state != CIRCUIT_STATE_OPEN) {
  620. if (CIRCUIT_IS_ORIGIN(circ)) {
  621. circuit_build_failed(circ); /* take actions if necessary */
  622. }
  623. circuit_rep_hist_note_result(circ);
  624. }
  625. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  626. if (circuits_pending_or_conns)
  627. smartlist_remove(circuits_pending_or_conns, circ);
  628. }
  629. if (CIRCUIT_IS_ORIGIN(circ)) {
  630. control_event_circuit_status(circ,
  631. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
  632. }
  633. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  634. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  635. tor_assert(circ->build_state->chosen_exit);
  636. /* treat this like getting a nack from it */
  637. info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  638. "Removing from descriptor.",
  639. safe_str(circ->rend_query),
  640. safe_str(build_state_get_exit_nickname(circ->build_state)));
  641. rend_client_remove_intro_point(circ->build_state->chosen_exit,
  642. circ->rend_query);
  643. }
  644. if (circ->n_conn)
  645. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  646. for (conn=circ->n_streams; conn; conn=conn->next_stream)
  647. connection_edge_destroy(circ->n_circ_id, conn);
  648. while (circ->resolving_streams) {
  649. conn = circ->resolving_streams;
  650. circ->resolving_streams = conn->next_stream;
  651. if (!conn->marked_for_close) {
  652. /* The other side will see a DESTROY, and infer that the connections
  653. * are closing because the circuit is getting torn down. No need
  654. * to send an end cell. */
  655. conn->has_sent_end = 1;
  656. connection_mark_for_close(conn);
  657. }
  658. conn->on_circuit = NULL;
  659. }
  660. if (circ->p_conn)
  661. connection_or_send_destroy(circ->p_circ_id, circ->p_conn, reason);
  662. for (conn=circ->p_streams; conn; conn=conn->next_stream)
  663. connection_edge_destroy(circ->p_circ_id, conn);
  664. circ->marked_for_close = line;
  665. circ->marked_for_close_file = file;
  666. if (circ->rend_splice) {
  667. if (!circ->rend_splice->marked_for_close) {
  668. /* do this after marking this circuit, to avoid infinite recursion. */
  669. circuit_mark_for_close(circ->rend_splice, reason);
  670. }
  671. circ->rend_splice = NULL;
  672. }
  673. }
  674. /** Verify that cpath layer <b>cp</b> has all of its invariants
  675. * correct. Trigger an assert if anything is invalid.
  676. */
  677. void
  678. assert_cpath_layer_ok(const crypt_path_t *cp)
  679. {
  680. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  681. // tor_assert(cp->port);
  682. tor_assert(cp);
  683. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  684. switch (cp->state)
  685. {
  686. case CPATH_STATE_OPEN:
  687. tor_assert(cp->f_crypto);
  688. tor_assert(cp->b_crypto);
  689. /* fall through */
  690. case CPATH_STATE_CLOSED:
  691. tor_assert(!cp->dh_handshake_state);
  692. break;
  693. case CPATH_STATE_AWAITING_KEYS:
  694. /* tor_assert(cp->dh_handshake_state); */
  695. break;
  696. default:
  697. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  698. tor_assert(0);
  699. }
  700. tor_assert(cp->package_window >= 0);
  701. tor_assert(cp->deliver_window >= 0);
  702. }
  703. /** Verify that cpath <b>cp</b> has all of its invariants
  704. * correct. Trigger an assert if anything is invalid.
  705. */
  706. static void
  707. assert_cpath_ok(const crypt_path_t *cp)
  708. {
  709. const crypt_path_t *start = cp;
  710. do {
  711. assert_cpath_layer_ok(cp);
  712. /* layers must be in sequence of: "open* awaiting? closed*" */
  713. if (cp != start) {
  714. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  715. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  716. } else if (cp->state == CPATH_STATE_OPEN) {
  717. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  718. }
  719. }
  720. cp = cp->next;
  721. tor_assert(cp);
  722. } while (cp != start);
  723. }
  724. /** Verify that circuit <b>c</b> has all of its invariants
  725. * correct. Trigger an assert if anything is invalid.
  726. */
  727. void
  728. assert_circuit_ok(const circuit_t *c)
  729. {
  730. connection_t *conn;
  731. tor_assert(c);
  732. tor_assert(c->magic == CIRCUIT_MAGIC);
  733. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  734. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  735. if (c->n_conn) {
  736. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  737. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
  738. DIGEST_LEN));
  739. if (c->n_circ_id)
  740. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  741. }
  742. if (c->p_conn) {
  743. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  744. if (c->p_circ_id)
  745. tor_assert(c == circuit_get_by_circid_orconn(c->p_circ_id, c->p_conn));
  746. }
  747. for (conn = c->p_streams; conn; conn = conn->next_stream)
  748. tor_assert(conn->type == CONN_TYPE_AP);
  749. for (conn = c->n_streams; conn; conn = conn->next_stream)
  750. tor_assert(conn->type == CONN_TYPE_EXIT);
  751. tor_assert(c->deliver_window >= 0);
  752. tor_assert(c->package_window >= 0);
  753. if (c->state == CIRCUIT_STATE_OPEN) {
  754. tor_assert(!c->onionskin);
  755. if (c->cpath) {
  756. tor_assert(CIRCUIT_IS_ORIGIN(c));
  757. tor_assert(!c->n_crypto);
  758. tor_assert(!c->p_crypto);
  759. tor_assert(!c->n_digest);
  760. tor_assert(!c->p_digest);
  761. } else {
  762. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  763. tor_assert(c->n_crypto);
  764. tor_assert(c->p_crypto);
  765. tor_assert(c->n_digest);
  766. tor_assert(c->p_digest);
  767. }
  768. }
  769. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  770. tor_assert(circuits_pending_or_conns &&
  771. smartlist_isin(circuits_pending_or_conns, c));
  772. } else {
  773. tor_assert(!circuits_pending_or_conns ||
  774. !smartlist_isin(circuits_pending_or_conns, c));
  775. }
  776. if (c->cpath) {
  777. assert_cpath_ok(c->cpath);
  778. }
  779. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  780. if (!c->marked_for_close) {
  781. tor_assert(c->rend_splice);
  782. tor_assert(c->rend_splice->rend_splice == c);
  783. }
  784. tor_assert(c->rend_splice != c);
  785. } else {
  786. tor_assert(!c->rend_splice);
  787. }
  788. }