circuitlist.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. log_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. log_warn(LD_BUG,
  362. "circuit matches p_conn, but not in hash table (Bug!)");
  363. return circ;
  364. }
  365. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  366. log_warn(LD_BUG,
  367. "circuit matches n_conn, but not in hash table (Bug!)");
  368. return circ;
  369. }
  370. }
  371. return NULL;
  372. }
  373. }
  374. /** Return a circ such that:
  375. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  376. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  377. * - circ is not marked for close.
  378. * Return NULL if no such circuit exists.
  379. */
  380. circuit_t *
  381. circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn)
  382. {
  383. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  384. if (!circ || circ->marked_for_close)
  385. return NULL;
  386. else
  387. return circ;
  388. }
  389. /** Return true iff there is a circ such that
  390. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  391. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  392. * Return NULL if no such circuit exists.
  393. */
  394. int
  395. circuit_id_used_on_conn(uint16_t circ_id, connection_t *conn)
  396. {
  397. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  398. if (circ && circ->marked_for_close)
  399. log_fn(LOG_NOTICE, LD_CIRC,
  400. "I was about to re-use a circuit ID that had been marked."
  401. " Good thing we fixed that bug!");
  402. return circ != NULL;
  403. }
  404. /** Return the circuit that a given edge connection is using. */
  405. circuit_t *
  406. circuit_get_by_edge_conn(connection_t *conn)
  407. {
  408. circuit_t *circ;
  409. tor_assert(CONN_IS_EDGE(conn));
  410. circ = conn->on_circuit;
  411. tor_assert(!circ || circ->magic == CIRCUIT_MAGIC);
  412. return circ;
  413. }
  414. /** For each circuits that have <b>conn</b> as n_conn or p_conn, unlink the
  415. * circuit from the orconn,circid map, and mark it for close if it hasn't
  416. * been marked already.
  417. */
  418. void
  419. circuit_unlink_all_from_or_conn(connection_t *conn, int reason)
  420. {
  421. circuit_t *circ;
  422. for (circ = global_circuitlist; circ; circ = circ->next) {
  423. if (circ->n_conn == conn || circ->p_conn == conn) {
  424. if (circ->n_conn == conn)
  425. circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
  426. if (circ->p_conn == conn)
  427. circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
  428. if (!circ->marked_for_close)
  429. circuit_mark_for_close(circ, reason);
  430. }
  431. }
  432. }
  433. /** Return a circ such that:
  434. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  435. * - circ-\>purpose is equal to <b>purpose</b>.
  436. *
  437. * Return NULL if no such circuit exists.
  438. */
  439. circuit_t *
  440. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  441. {
  442. circuit_t *circ;
  443. for (circ = global_circuitlist; circ; circ = circ->next) {
  444. if (!circ->marked_for_close &&
  445. circ->purpose == purpose &&
  446. !rend_cmp_service_ids(rend_query, circ->rend_query))
  447. return circ;
  448. }
  449. return NULL;
  450. }
  451. /** Return the first circuit in global_circuitlist after <b>start</b>
  452. * whose rend_pk_digest field is <b>digest</b> and whose purpose is
  453. * <b>purpose</b>. Returns NULL if no circuit is found.
  454. * If <b>start</b> is NULL, begin at the start of the list.
  455. */
  456. circuit_t *
  457. circuit_get_next_by_pk_and_purpose(circuit_t *start,
  458. const char *digest, uint8_t purpose)
  459. {
  460. circuit_t *circ;
  461. if (start == NULL)
  462. circ = global_circuitlist;
  463. else
  464. circ = start->next;
  465. for ( ; circ; circ = circ->next) {
  466. if (circ->marked_for_close)
  467. continue;
  468. if (circ->purpose != purpose)
  469. continue;
  470. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  471. return circ;
  472. }
  473. return NULL;
  474. }
  475. /** Return the circuit waiting for a rendezvous with the provided cookie.
  476. * Return NULL if no such circuit is found.
  477. */
  478. circuit_t *
  479. circuit_get_rendezvous(const char *cookie)
  480. {
  481. circuit_t *circ;
  482. for (circ = global_circuitlist; circ; circ = circ->next) {
  483. if (! circ->marked_for_close &&
  484. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  485. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  486. return circ;
  487. }
  488. return NULL;
  489. }
  490. /** Return a circuit that is open, has specified <b>purpose</b>,
  491. * has a timestamp_dirty value of 0, is uptime/capacity/internal
  492. * if required, and if info is defined, does not already use info
  493. * as any of its hops; or NULL if no circuit fits this description.
  494. *
  495. * Return need_uptime circuits if that is requested; and if it's not
  496. * requested, return non-uptime circuits if possible, else either.
  497. *
  498. * Only return internal circuits if that is requested.
  499. */
  500. circuit_t *
  501. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  502. int need_uptime,
  503. int need_capacity, int internal)
  504. {
  505. circuit_t *circ;
  506. circuit_t *best=NULL;
  507. log_debug(LD_CIRC,
  508. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  509. "capacity %d, internal %d",
  510. purpose, need_uptime, need_capacity, internal);
  511. for (circ=global_circuitlist; circ; circ = circ->next) {
  512. if (CIRCUIT_IS_ORIGIN(circ) &&
  513. circ->state == CIRCUIT_STATE_OPEN &&
  514. !circ->marked_for_close &&
  515. circ->purpose == purpose &&
  516. !circ->timestamp_dirty &&
  517. (!need_uptime || circ->build_state->need_uptime) &&
  518. (!need_capacity || circ->build_state->need_capacity) &&
  519. (internal == circ->build_state->is_internal)) {
  520. if (info) {
  521. /* need to make sure we don't duplicate hops */
  522. crypt_path_t *hop = circ->cpath;
  523. do {
  524. if (!memcmp(hop->extend_info->identity_digest,
  525. info->identity_digest, DIGEST_LEN))
  526. goto next;
  527. hop=hop->next;
  528. } while (hop!=circ->cpath);
  529. }
  530. if (!best || (best->build_state->need_uptime && !need_uptime))
  531. best = circ;
  532. next: ;
  533. }
  534. }
  535. return best;
  536. }
  537. /** Go through the circuitlist; mark-for-close each circuit that starts
  538. * at us but has not yet been used. */
  539. void
  540. circuit_mark_all_unused_circs(void)
  541. {
  542. circuit_t *circ;
  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. circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
  548. }
  549. }
  550. /** Go through the circuitlist; for each circuit that starts at us
  551. * and is dirty, frob its timestamp_dirty so we won't use it for any
  552. * new streams.
  553. *
  554. * This is useful for letting the user change pseudonyms, so new
  555. * streams will not be linkable to old streams.
  556. */
  557. void
  558. circuit_expire_all_dirty_circs(void)
  559. {
  560. circuit_t *circ;
  561. or_options_t *options = get_options();
  562. for (circ=global_circuitlist; circ; circ = circ->next) {
  563. if (CIRCUIT_IS_ORIGIN(circ) &&
  564. !circ->marked_for_close &&
  565. circ->timestamp_dirty)
  566. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  567. }
  568. }
  569. /** Mark <b>circ</b> to be closed next time we call
  570. * circuit_close_all_marked(). Do any cleanup needed:
  571. * - If state is onionskin_pending, remove circ from the onion_pending
  572. * list.
  573. * - If circ isn't open yet: call circuit_build_failed() if we're
  574. * the origin, and in either case call circuit_rep_hist_note_result()
  575. * to note stats.
  576. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  577. * just tried from our list of intro points for that service
  578. * descriptor.
  579. * - Send appropriate destroys and edge_destroys for conns and
  580. * streams attached to circ.
  581. * - If circ->rend_splice is set (we are the midpoint of a joined
  582. * rendezvous stream), then mark the other circuit to close as well.
  583. */
  584. void
  585. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  586. const char *file)
  587. {
  588. connection_t *conn;
  589. assert_circuit_ok(circ);
  590. tor_assert(line);
  591. tor_assert(file);
  592. if (circ->marked_for_close) {
  593. log(LOG_WARN,LD_BUG,
  594. "Duplicate call to circuit_mark_for_close at %s:%d"
  595. " (first at %s:%d)", file, line,
  596. circ->marked_for_close_file, circ->marked_for_close);
  597. return;
  598. }
  599. if (reason == END_CIRC_AT_ORIGIN) {
  600. if (!CIRCUIT_IS_ORIGIN(circ)) {
  601. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  602. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  603. file, line, circ->purpose);
  604. }
  605. reason = END_CIRC_REASON_NONE;
  606. } else if (CIRCUIT_IS_ORIGIN(circ) && reason != END_CIRC_REASON_NONE) {
  607. /* Don't warn about this; there are plenty of places where our code
  608. * is origin-agnosic. */
  609. reason = END_CIRC_REASON_NONE;
  610. }
  611. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  612. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  613. reason = END_CIRC_REASON_NONE;
  614. }
  615. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  616. onion_pending_remove(circ);
  617. }
  618. /* If the circuit ever became OPEN, we sent it to the reputation history
  619. * module then. If it isn't OPEN, we send it there now to remember which
  620. * links worked and which didn't.
  621. */
  622. if (circ->state != CIRCUIT_STATE_OPEN) {
  623. if (CIRCUIT_IS_ORIGIN(circ)) {
  624. circuit_build_failed(circ); /* take actions if necessary */
  625. }
  626. circuit_rep_hist_note_result(circ);
  627. }
  628. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  629. if (circuits_pending_or_conns)
  630. smartlist_remove(circuits_pending_or_conns, circ);
  631. }
  632. if (CIRCUIT_IS_ORIGIN(circ)) {
  633. control_event_circuit_status(circ,
  634. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
  635. }
  636. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  637. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  638. tor_assert(circ->build_state->chosen_exit);
  639. /* treat this like getting a nack from it */
  640. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  641. "Removing from descriptor.",
  642. safe_str(circ->rend_query),
  643. safe_str(build_state_get_exit_nickname(circ->build_state)));
  644. rend_client_remove_intro_point(circ->build_state->chosen_exit,
  645. circ->rend_query);
  646. }
  647. if (circ->n_conn)
  648. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  649. for (conn=circ->n_streams; conn; conn=conn->next_stream)
  650. connection_edge_destroy(circ->n_circ_id, conn);
  651. while (circ->resolving_streams) {
  652. conn = circ->resolving_streams;
  653. circ->resolving_streams = conn->next_stream;
  654. if (!conn->marked_for_close) {
  655. /* The other side will see a DESTROY, and infer that the connections
  656. * are closing because the circuit is getting torn down. No need
  657. * to send an end cell. */
  658. conn->has_sent_end = 1;
  659. connection_mark_for_close(conn);
  660. }
  661. conn->on_circuit = NULL;
  662. }
  663. if (circ->p_conn)
  664. connection_or_send_destroy(circ->p_circ_id, circ->p_conn, reason);
  665. for (conn=circ->p_streams; conn; conn=conn->next_stream)
  666. connection_edge_destroy(circ->p_circ_id, conn);
  667. circ->marked_for_close = line;
  668. circ->marked_for_close_file = file;
  669. if (circ->rend_splice) {
  670. if (!circ->rend_splice->marked_for_close) {
  671. /* do this after marking this circuit, to avoid infinite recursion. */
  672. circuit_mark_for_close(circ->rend_splice, reason);
  673. }
  674. circ->rend_splice = NULL;
  675. }
  676. }
  677. /** Verify that cpath layer <b>cp</b> has all of its invariants
  678. * correct. Trigger an assert if anything is invalid.
  679. */
  680. void
  681. assert_cpath_layer_ok(const crypt_path_t *cp)
  682. {
  683. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  684. // tor_assert(cp->port);
  685. tor_assert(cp);
  686. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  687. switch (cp->state)
  688. {
  689. case CPATH_STATE_OPEN:
  690. tor_assert(cp->f_crypto);
  691. tor_assert(cp->b_crypto);
  692. /* fall through */
  693. case CPATH_STATE_CLOSED:
  694. tor_assert(!cp->dh_handshake_state);
  695. break;
  696. case CPATH_STATE_AWAITING_KEYS:
  697. /* tor_assert(cp->dh_handshake_state); */
  698. break;
  699. default:
  700. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  701. tor_assert(0);
  702. }
  703. tor_assert(cp->package_window >= 0);
  704. tor_assert(cp->deliver_window >= 0);
  705. }
  706. /** Verify that cpath <b>cp</b> has all of its invariants
  707. * correct. Trigger an assert if anything is invalid.
  708. */
  709. static void
  710. assert_cpath_ok(const crypt_path_t *cp)
  711. {
  712. const crypt_path_t *start = cp;
  713. do {
  714. assert_cpath_layer_ok(cp);
  715. /* layers must be in sequence of: "open* awaiting? closed*" */
  716. if (cp != start) {
  717. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  718. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  719. } else if (cp->state == CPATH_STATE_OPEN) {
  720. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  721. }
  722. }
  723. cp = cp->next;
  724. tor_assert(cp);
  725. } while (cp != start);
  726. }
  727. /** Verify that circuit <b>c</b> has all of its invariants
  728. * correct. Trigger an assert if anything is invalid.
  729. */
  730. void
  731. assert_circuit_ok(const circuit_t *c)
  732. {
  733. connection_t *conn;
  734. tor_assert(c);
  735. tor_assert(c->magic == CIRCUIT_MAGIC);
  736. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  737. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  738. if (c->n_conn) {
  739. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  740. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
  741. DIGEST_LEN));
  742. if (c->n_circ_id)
  743. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  744. }
  745. if (c->p_conn) {
  746. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  747. if (c->p_circ_id)
  748. tor_assert(c == circuit_get_by_circid_orconn(c->p_circ_id, c->p_conn));
  749. }
  750. for (conn = c->p_streams; conn; conn = conn->next_stream)
  751. tor_assert(conn->type == CONN_TYPE_AP);
  752. for (conn = c->n_streams; conn; conn = conn->next_stream)
  753. tor_assert(conn->type == CONN_TYPE_EXIT);
  754. tor_assert(c->deliver_window >= 0);
  755. tor_assert(c->package_window >= 0);
  756. if (c->state == CIRCUIT_STATE_OPEN) {
  757. tor_assert(!c->onionskin);
  758. if (c->cpath) {
  759. tor_assert(CIRCUIT_IS_ORIGIN(c));
  760. tor_assert(!c->n_crypto);
  761. tor_assert(!c->p_crypto);
  762. tor_assert(!c->n_digest);
  763. tor_assert(!c->p_digest);
  764. } else {
  765. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  766. tor_assert(c->n_crypto);
  767. tor_assert(c->p_crypto);
  768. tor_assert(c->n_digest);
  769. tor_assert(c->p_digest);
  770. }
  771. }
  772. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  773. tor_assert(circuits_pending_or_conns &&
  774. smartlist_isin(circuits_pending_or_conns, c));
  775. } else {
  776. tor_assert(!circuits_pending_or_conns ||
  777. !smartlist_isin(circuits_pending_or_conns, c));
  778. }
  779. if (c->cpath) {
  780. assert_cpath_ok(c->cpath);
  781. }
  782. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  783. if (!c->marked_for_close) {
  784. tor_assert(c->rend_splice);
  785. tor_assert(c->rend_splice->rend_splice == c);
  786. }
  787. tor_assert(c->rend_splice != c);
  788. } else {
  789. tor_assert(!c->rend_splice);
  790. }
  791. }