circuitlist.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. or_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. static void
  58. circuit_set_circid_orconn_helper(circuit_t *circ, uint16_t id,
  59. or_connection_t *conn,
  60. uint16_t old_id, or_connection_t *old_conn)
  61. {
  62. orconn_circid_circuit_map_t search;
  63. orconn_circid_circuit_map_t *found;
  64. if (_last_circid_orconn_ent &&
  65. ((old_id == _last_circid_orconn_ent->circ_id &&
  66. old_conn == _last_circid_orconn_ent->or_conn) ||
  67. (id == _last_circid_orconn_ent->circ_id &&
  68. conn == _last_circid_orconn_ent->or_conn))) {
  69. _last_circid_orconn_ent = NULL;
  70. }
  71. if (old_conn) { /* we may need to remove it from the conn-circid map */
  72. tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
  73. search.circ_id = old_id;
  74. search.or_conn = old_conn;
  75. found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
  76. if (found) {
  77. tor_free(found);
  78. --old_conn->n_circuits;
  79. }
  80. }
  81. if (conn == NULL)
  82. return;
  83. /* now add the new one to the conn-circid map */
  84. search.circ_id = id;
  85. search.or_conn = conn;
  86. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  87. if (found) {
  88. found->circuit = circ;
  89. } else {
  90. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  91. found->circ_id = id;
  92. found->or_conn = conn;
  93. found->circuit = circ;
  94. HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
  95. }
  96. ++conn->n_circuits;
  97. }
  98. /** Set the p_conn field of a circuit <b>circ</b>, along
  99. * with the corresponding circuit ID, and add the circuit as appropriate
  100. * to the (orconn,id)-\>circuit map. */
  101. void
  102. circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_t id,
  103. or_connection_t *conn)
  104. {
  105. uint16_t old_id;
  106. or_connection_t *old_conn;
  107. old_id = circ->p_circ_id;
  108. old_conn = circ->p_conn;
  109. circ->p_circ_id = id;
  110. circ->p_conn = conn;
  111. if (id == old_id && conn == old_conn)
  112. return;
  113. circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), id, conn,
  114. old_id, old_conn);
  115. }
  116. /** Set the n_conn field of a circuit <b>circ</b>, along
  117. * with the corresponding circuit ID, and add the circuit as appropriate
  118. * to the (orconn,id)-\>circuit map. */
  119. void
  120. circuit_set_n_circid_orconn(circuit_t *circ, uint16_t id,
  121. or_connection_t *conn)
  122. {
  123. uint16_t old_id;
  124. or_connection_t *old_conn;
  125. old_id = circ->n_circ_id;
  126. old_conn = circ->n_conn;
  127. circ->n_circ_id = id;
  128. circ->n_conn = conn;
  129. if (id == old_id && conn == old_conn)
  130. return;
  131. circuit_set_circid_orconn_helper(circ, id, conn, old_id, old_conn);
  132. }
  133. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  134. * it from lists as appropriate. */
  135. void
  136. circuit_set_state(circuit_t *circ, int state)
  137. {
  138. tor_assert(circ);
  139. if (state == circ->state)
  140. return;
  141. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  142. /* remove from waiting-circuit list. */
  143. if (circuits_pending_or_conns)
  144. smartlist_remove(circuits_pending_or_conns, circ);
  145. }
  146. if (state == CIRCUIT_STATE_OR_WAIT) {
  147. /* add to waiting-circuit list. */
  148. if (!circuits_pending_or_conns)
  149. circuits_pending_or_conns = smartlist_create();
  150. smartlist_add(circuits_pending_or_conns, circ);
  151. }
  152. circ->state = state;
  153. }
  154. /** Add <b>circ</b> to the global list of circuits. This is called only from
  155. * within circuit_new.
  156. */
  157. static void
  158. circuit_add(circuit_t *circ)
  159. {
  160. if (!global_circuitlist) { /* first one */
  161. global_circuitlist = circ;
  162. circ->next = NULL;
  163. } else {
  164. circ->next = global_circuitlist;
  165. global_circuitlist = circ;
  166. }
  167. }
  168. /** Detach from the global circuit list, and deallocate, all
  169. * circuits that have been marked for close.
  170. */
  171. void
  172. circuit_close_all_marked(void)
  173. {
  174. circuit_t *tmp,*m;
  175. while (global_circuitlist && global_circuitlist->marked_for_close) {
  176. tmp = global_circuitlist->next;
  177. circuit_free(global_circuitlist);
  178. global_circuitlist = tmp;
  179. }
  180. tmp = global_circuitlist;
  181. while (tmp && tmp->next) {
  182. if (tmp->next->marked_for_close) {
  183. m = tmp->next->next;
  184. circuit_free(tmp->next);
  185. tmp->next = m;
  186. /* Need to check new tmp->next; don't advance tmp. */
  187. } else {
  188. /* Advance tmp. */
  189. tmp = tmp->next;
  190. }
  191. }
  192. }
  193. /** Return the head of the global linked list of circuits. */
  194. circuit_t *
  195. _circuit_get_global_list(void)
  196. {
  197. return global_circuitlist;
  198. }
  199. /** Function to make circ-\>state human-readable */
  200. const char *
  201. circuit_state_to_string(int state)
  202. {
  203. static char buf[64];
  204. switch (state) {
  205. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  206. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  207. case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
  208. case CIRCUIT_STATE_OPEN: return "open";
  209. default:
  210. log_warn(LD_BUG, "Bug: unknown circuit state %d", state);
  211. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  212. return buf;
  213. }
  214. }
  215. /* DOCDOC */
  216. static void
  217. init_circuit_base(circuit_t *circ)
  218. {
  219. circ->timestamp_created = time(NULL);
  220. circ->package_window = CIRCWINDOW_START;
  221. circ->deliver_window = CIRCWINDOW_START;
  222. circuit_add(circ);
  223. }
  224. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  225. * and <b>p_conn</b>. Add it to the global circuit list.
  226. */
  227. origin_circuit_t *
  228. origin_circuit_new(void)
  229. {
  230. origin_circuit_t *circ;
  231. /* never zero, since a global ID of 0 is treated specially by the
  232. * controller */
  233. static uint32_t n_circuits_allocated = 1;
  234. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  235. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  236. circ->next_stream_id = crypto_rand_int(1<<16);
  237. circ->global_identifier = n_circuits_allocated++;
  238. init_circuit_base(TO_CIRCUIT(circ));
  239. return circ;
  240. }
  241. or_circuit_t *
  242. or_circuit_new(uint16_t p_circ_id, or_connection_t *p_conn)
  243. {
  244. /* CircIDs */
  245. or_circuit_t *circ;
  246. circ = tor_malloc_zero(sizeof(or_circuit_t));
  247. circ->_base.magic = OR_CIRCUIT_MAGIC;
  248. if (p_conn)
  249. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  250. init_circuit_base(TO_CIRCUIT(circ));
  251. return circ;
  252. }
  253. /** Deallocate space associated with circ.
  254. */
  255. static void
  256. circuit_free(circuit_t *circ)
  257. {
  258. void *mem;
  259. tor_assert(circ);
  260. if (CIRCUIT_IS_ORIGIN(circ)) {
  261. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  262. mem = ocirc;
  263. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  264. if (ocirc->build_state) {
  265. if (ocirc->build_state->chosen_exit)
  266. extend_info_free(ocirc->build_state->chosen_exit);
  267. if (ocirc->build_state->pending_final_cpath)
  268. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  269. }
  270. tor_free(ocirc->build_state);
  271. circuit_free_cpath(ocirc->cpath);
  272. } else {
  273. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  274. mem = ocirc;
  275. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  276. if (ocirc->p_crypto)
  277. crypto_free_cipher_env(ocirc->p_crypto);
  278. if (ocirc->p_digest)
  279. crypto_free_digest_env(ocirc->p_digest);
  280. if (ocirc->n_crypto)
  281. crypto_free_cipher_env(ocirc->n_crypto);
  282. if (ocirc->n_digest)
  283. crypto_free_digest_env(ocirc->n_digest);
  284. if (ocirc->rend_splice) {
  285. or_circuit_t *other = ocirc->rend_splice;
  286. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  287. other->rend_splice = NULL;
  288. }
  289. tor_free(circ->onionskin);
  290. /* remove from map. */
  291. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  292. }
  293. /* Remove from map. */
  294. circuit_set_n_circid_orconn(circ, 0, NULL);
  295. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  296. tor_free(mem);
  297. }
  298. /** Deallocate space associated with the linked list <b>cpath</b>. */
  299. static void
  300. circuit_free_cpath(crypt_path_t *cpath)
  301. {
  302. crypt_path_t *victim, *head=cpath;
  303. if (!cpath)
  304. return;
  305. /* it's a doubly linked list, so we have to notice when we've
  306. * gone through it once. */
  307. while (cpath->next && cpath->next != head) {
  308. victim = cpath;
  309. cpath = victim->next;
  310. circuit_free_cpath_node(victim);
  311. }
  312. circuit_free_cpath_node(cpath);
  313. }
  314. /** Release all storage held by circuits. */
  315. void
  316. circuit_free_all(void)
  317. {
  318. circuit_t *next;
  319. while (global_circuitlist) {
  320. next = global_circuitlist->next;
  321. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  322. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  323. while (or_circ->resolving_streams) {
  324. edge_connection_t *next;
  325. next = or_circ->resolving_streams->next_stream;
  326. connection_free(TO_CONN(or_circ->resolving_streams));
  327. or_circ->resolving_streams = next;
  328. }
  329. }
  330. circuit_free(global_circuitlist);
  331. global_circuitlist = next;
  332. }
  333. if (circuits_pending_or_conns) {
  334. smartlist_free(circuits_pending_or_conns);
  335. circuits_pending_or_conns = NULL;
  336. }
  337. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  338. }
  339. /** Deallocate space associated with the cpath node <b>victim</b>. */
  340. static void
  341. circuit_free_cpath_node(crypt_path_t *victim)
  342. {
  343. if (victim->f_crypto)
  344. crypto_free_cipher_env(victim->f_crypto);
  345. if (victim->b_crypto)
  346. crypto_free_cipher_env(victim->b_crypto);
  347. if (victim->f_digest)
  348. crypto_free_digest_env(victim->f_digest);
  349. if (victim->b_digest)
  350. crypto_free_digest_env(victim->b_digest);
  351. if (victim->dh_handshake_state)
  352. crypto_dh_free(victim->dh_handshake_state);
  353. if (victim->extend_info)
  354. extend_info_free(victim->extend_info);
  355. victim->magic = 0xDEADBEEFu;
  356. tor_free(victim);
  357. }
  358. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  359. * of information about circuit <b>circ</b>.
  360. */
  361. static void
  362. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  363. const char *type, int this_circid, int other_circid)
  364. {
  365. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  366. "state %d (%s), born %d:",
  367. conn_array_index, type, this_circid, other_circid, circ->state,
  368. circuit_state_to_string(circ->state), (int)circ->timestamp_created);
  369. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  370. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  371. }
  372. }
  373. /** Log, at severity <b>severity</b>, information about each circuit
  374. * that is connected to <b>conn</b>.
  375. */
  376. void
  377. circuit_dump_by_conn(connection_t *conn, int severity)
  378. {
  379. circuit_t *circ;
  380. edge_connection_t *tmpconn;
  381. for (circ=global_circuitlist;circ;circ = circ->next) {
  382. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  383. if (circ->marked_for_close)
  384. continue;
  385. if (! CIRCUIT_IS_ORIGIN(circ))
  386. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  387. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  388. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  389. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  390. p_circ_id, n_circ_id);
  391. if (CIRCUIT_IS_ORIGIN(circ)) {
  392. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  393. tmpconn=tmpconn->next_stream) {
  394. if (TO_CONN(tmpconn) == conn) {
  395. circuit_dump_details(severity, circ, conn->conn_array_index,
  396. "App-ward", p_circ_id, n_circ_id);
  397. }
  398. }
  399. }
  400. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  401. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  402. n_circ_id, p_circ_id);
  403. if (! CIRCUIT_IS_ORIGIN(circ)) {
  404. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  405. tmpconn=tmpconn->next_stream) {
  406. if (TO_CONN(tmpconn) == conn) {
  407. circuit_dump_details(severity, circ, conn->conn_array_index,
  408. "Exit-ward", n_circ_id, p_circ_id);
  409. }
  410. }
  411. }
  412. if (!circ->n_conn && circ->n_addr && circ->n_port &&
  413. circ->n_addr == conn->addr &&
  414. circ->n_port == conn->port &&
  415. conn->type == CONN_TYPE_OR &&
  416. !memcmp(TO_OR_CONN(conn)->identity_digest, circ->n_conn_id_digest,
  417. DIGEST_LEN)) {
  418. circuit_dump_details(severity, circ, conn->conn_array_index,
  419. (circ->state == CIRCUIT_STATE_OPEN &&
  420. !CIRCUIT_IS_ORIGIN(circ)) ?
  421. "Endpoint" : "Pending",
  422. n_circ_id, p_circ_id);
  423. }
  424. }
  425. }
  426. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  427. * such circuit exists. */
  428. origin_circuit_t *
  429. circuit_get_by_global_id(uint32_t id)
  430. {
  431. circuit_t *circ;
  432. for (circ=global_circuitlist;circ;circ = circ->next) {
  433. if (CIRCUIT_IS_ORIGIN(circ) &&
  434. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  435. if (circ->marked_for_close)
  436. return NULL;
  437. else
  438. return TO_ORIGIN_CIRCUIT(circ);
  439. }
  440. }
  441. return NULL;
  442. }
  443. /** Return a circ such that:
  444. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  445. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  446. * Return NULL if no such circuit exists.
  447. */
  448. static INLINE circuit_t *
  449. circuit_get_by_circid_orconn_impl(uint16_t circ_id, or_connection_t *conn)
  450. {
  451. orconn_circid_circuit_map_t search;
  452. orconn_circid_circuit_map_t *found;
  453. if (_last_circid_orconn_ent &&
  454. circ_id == _last_circid_orconn_ent->circ_id &&
  455. conn == _last_circid_orconn_ent->or_conn) {
  456. found = _last_circid_orconn_ent;
  457. } else {
  458. search.circ_id = circ_id;
  459. search.or_conn = conn;
  460. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  461. _last_circid_orconn_ent = found;
  462. }
  463. if (found && found->circuit)
  464. return found->circuit;
  465. return NULL;
  466. /* The rest of this checks for bugs. Disabled by default. */
  467. {
  468. circuit_t *circ;
  469. for (circ=global_circuitlist;circ;circ = circ->next) {
  470. if (! CIRCUIT_IS_ORIGIN(circ)) {
  471. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  472. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  473. log_warn(LD_BUG,
  474. "circuit matches p_conn, but not in hash table (Bug!)");
  475. return circ;
  476. }
  477. }
  478. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  479. log_warn(LD_BUG,
  480. "circuit matches n_conn, but not in hash table (Bug!)");
  481. return circ;
  482. }
  483. }
  484. return NULL;
  485. }
  486. }
  487. /** Return a circ such that:
  488. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  489. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  490. * - circ is not marked for close.
  491. * Return NULL if no such circuit exists.
  492. */
  493. circuit_t *
  494. circuit_get_by_circid_orconn(uint16_t circ_id, or_connection_t *conn)
  495. {
  496. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  497. if (!circ || circ->marked_for_close)
  498. return NULL;
  499. else
  500. return circ;
  501. }
  502. /** Return the circuit that a given edge connection is using. */
  503. circuit_t *
  504. circuit_get_by_edge_conn(edge_connection_t *conn)
  505. {
  506. circuit_t *circ;
  507. circ = conn->on_circuit;
  508. tor_assert(!circ ||
  509. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  510. : circ->magic == OR_CIRCUIT_MAGIC));
  511. return circ;
  512. }
  513. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  514. * circuit from the orconn,circid map, and mark it for close if it hasn't
  515. * been marked already.
  516. */
  517. void
  518. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  519. {
  520. circuit_t *circ;
  521. for (circ = global_circuitlist; circ; circ = circ->next) {
  522. int mark = 0;
  523. if (circ->n_conn == conn) {
  524. circuit_set_n_circid_orconn(circ, 0, NULL);
  525. mark = 1;
  526. }
  527. if (! CIRCUIT_IS_ORIGIN(circ)) {
  528. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  529. if (or_circ->p_conn == conn) {
  530. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  531. mark = 1;
  532. }
  533. }
  534. if (mark && !circ->marked_for_close)
  535. circuit_mark_for_close(circ, reason);
  536. }
  537. }
  538. /** Return a circ such that:
  539. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  540. * - circ-\>purpose is equal to <b>purpose</b>.
  541. *
  542. * Return NULL if no such circuit exists.
  543. */
  544. origin_circuit_t *
  545. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  546. {
  547. circuit_t *circ;
  548. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  549. for (circ = global_circuitlist; circ; circ = circ->next) {
  550. if (!circ->marked_for_close &&
  551. circ->purpose == purpose &&
  552. !rend_cmp_service_ids(rend_query, TO_ORIGIN_CIRCUIT(circ)->rend_query))
  553. return TO_ORIGIN_CIRCUIT(circ);
  554. }
  555. return NULL;
  556. }
  557. /** Return the first circuit in global_circuitlist after <b>start</b>
  558. * whose purpose is <b>purpose</b> is purpose, and (if set) whose
  559. * <b>digest</b> matches the rend_pk_digest field. Return NULL if no
  560. * circuit is found.
  561. * If <b>start</b> is NULL, begin at the start of the list.
  562. * DOCDOC origin.
  563. */
  564. origin_circuit_t *
  565. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  566. const char *digest, uint8_t purpose)
  567. {
  568. circuit_t *circ;
  569. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  570. if (start == NULL)
  571. circ = global_circuitlist;
  572. else
  573. circ = TO_CIRCUIT(start)->next;
  574. for ( ; circ; circ = circ->next) {
  575. if (circ->marked_for_close)
  576. continue;
  577. if (circ->purpose != purpose)
  578. continue;
  579. if (!digest)
  580. return TO_ORIGIN_CIRCUIT(circ);
  581. else if (!memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_pk_digest,
  582. digest, DIGEST_LEN))
  583. return TO_ORIGIN_CIRCUIT(circ);
  584. }
  585. return NULL;
  586. }
  587. /* DOCDOC */
  588. static or_circuit_t *
  589. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  590. size_t len)
  591. {
  592. circuit_t *circ;
  593. for (circ = global_circuitlist; circ; circ = circ->next) {
  594. if (! circ->marked_for_close &&
  595. circ->purpose == purpose &&
  596. ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  597. return TO_OR_CIRCUIT(circ);
  598. }
  599. return NULL;
  600. }
  601. /** Return the circuit waiting for a rendezvous with the provided cookie.
  602. * Return NULL if no such circuit is found.
  603. */
  604. or_circuit_t *
  605. circuit_get_rendezvous(const char *cookie)
  606. {
  607. return circuit_get_by_rend_token_and_purpose(
  608. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  609. cookie, REND_COOKIE_LEN);
  610. }
  611. /** Return the circuit waiting for intro cells of the given digest.
  612. * Return NULL if no such circuit is found.
  613. */
  614. or_circuit_t *
  615. circuit_get_intro_point(const char *digest)
  616. {
  617. return circuit_get_by_rend_token_and_purpose(
  618. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  619. DIGEST_LEN);
  620. }
  621. /** Return a circuit that is open, has specified <b>purpose</b>,
  622. * has a timestamp_dirty value of 0, is uptime/capacity/internal
  623. * if required, and if info is defined, does not already use info
  624. * as any of its hops; or NULL if no circuit fits this description.
  625. *
  626. * Return need_uptime circuits if that is requested; and if it's not
  627. * requested, return non-uptime circuits if possible, else either.
  628. *
  629. * Only return internal circuits if that is requested.
  630. */
  631. origin_circuit_t *
  632. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  633. int need_uptime,
  634. int need_capacity, int internal)
  635. {
  636. circuit_t *_circ;
  637. origin_circuit_t *best=NULL;
  638. log_debug(LD_CIRC,
  639. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  640. "capacity %d, internal %d",
  641. purpose, need_uptime, need_capacity, internal);
  642. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  643. if (CIRCUIT_IS_ORIGIN(_circ) &&
  644. _circ->state == CIRCUIT_STATE_OPEN &&
  645. !_circ->marked_for_close &&
  646. _circ->purpose == purpose &&
  647. !_circ->timestamp_dirty) {
  648. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  649. if ((!need_uptime || circ->build_state->need_uptime) &&
  650. (!need_capacity || circ->build_state->need_capacity) &&
  651. (internal == circ->build_state->is_internal)) {
  652. if (info) {
  653. /* need to make sure we don't duplicate hops */
  654. crypt_path_t *hop = circ->cpath;
  655. do {
  656. if (!memcmp(hop->extend_info->identity_digest,
  657. info->identity_digest, DIGEST_LEN))
  658. goto next;
  659. hop=hop->next;
  660. } while (hop!=circ->cpath);
  661. }
  662. if (!best || (best->build_state->need_uptime && !need_uptime))
  663. best = circ;
  664. next: ;
  665. }
  666. }
  667. }
  668. return best;
  669. }
  670. /** Return the number of hops in circuit's path. */
  671. int
  672. circuit_get_cpath_len(origin_circuit_t *circ)
  673. {
  674. int n = 0;
  675. if (circ && circ->cpath) {
  676. crypt_path_t *cpath, *cpath_next = NULL;
  677. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  678. cpath_next = cpath->next;
  679. ++n;
  680. }
  681. }
  682. return n;
  683. }
  684. /** Go through the circuitlist; mark-for-close each circuit that starts
  685. * at us but has not yet been used. */
  686. void
  687. circuit_mark_all_unused_circs(void)
  688. {
  689. circuit_t *circ;
  690. for (circ=global_circuitlist; circ; circ = circ->next) {
  691. if (CIRCUIT_IS_ORIGIN(circ) &&
  692. !circ->marked_for_close &&
  693. !circ->timestamp_dirty)
  694. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  695. }
  696. }
  697. /** Go through the circuitlist; for each circuit that starts at us
  698. * and is dirty, frob its timestamp_dirty so we won't use it for any
  699. * new streams.
  700. *
  701. * This is useful for letting the user change pseudonyms, so new
  702. * streams will not be linkable to old streams.
  703. */
  704. void
  705. circuit_expire_all_dirty_circs(void)
  706. {
  707. circuit_t *circ;
  708. or_options_t *options = get_options();
  709. for (circ=global_circuitlist; circ; circ = circ->next) {
  710. if (CIRCUIT_IS_ORIGIN(circ) &&
  711. !circ->marked_for_close &&
  712. circ->timestamp_dirty)
  713. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  714. }
  715. }
  716. /** Mark <b>circ</b> to be closed next time we call
  717. * circuit_close_all_marked(). Do any cleanup needed:
  718. * - If state is onionskin_pending, remove circ from the onion_pending
  719. * list.
  720. * - If circ isn't open yet: call circuit_build_failed() if we're
  721. * the origin, and in either case call circuit_rep_hist_note_result()
  722. * to note stats.
  723. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  724. * just tried from our list of intro points for that service
  725. * descriptor.
  726. * - Send appropriate destroys and edge_destroys for conns and
  727. * streams attached to circ.
  728. * - If circ->rend_splice is set (we are the midpoint of a joined
  729. * rendezvous stream), then mark the other circuit to close as well.
  730. */
  731. void
  732. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  733. const char *file)
  734. {
  735. int orig_reason = reason; /* Passed to the controller */
  736. assert_circuit_ok(circ);
  737. tor_assert(line);
  738. tor_assert(file);
  739. if (circ->marked_for_close) {
  740. log(LOG_WARN,LD_BUG,
  741. "Duplicate call to circuit_mark_for_close at %s:%d"
  742. " (first at %s:%d)", file, line,
  743. circ->marked_for_close_file, circ->marked_for_close);
  744. return;
  745. }
  746. if (reason == END_CIRC_AT_ORIGIN) {
  747. if (!CIRCUIT_IS_ORIGIN(circ)) {
  748. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  749. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  750. file, line, circ->purpose);
  751. }
  752. reason = END_CIRC_REASON_NONE;
  753. } else if (CIRCUIT_IS_ORIGIN(circ) && reason < _END_CIRC_REASON_MIN) {
  754. /* We don't send reasons when closing circuits at the origin, but we want
  755. * to track them anyway so we can give them to the controller. */
  756. reason = END_CIRC_REASON_NONE;
  757. }
  758. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  759. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  760. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  761. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  762. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  763. reason = END_CIRC_REASON_NONE;
  764. }
  765. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  766. onion_pending_remove(TO_OR_CIRCUIT(circ));
  767. }
  768. /* If the circuit ever became OPEN, we sent it to the reputation history
  769. * module then. If it isn't OPEN, we send it there now to remember which
  770. * links worked and which didn't.
  771. */
  772. if (circ->state != CIRCUIT_STATE_OPEN) {
  773. if (CIRCUIT_IS_ORIGIN(circ)) {
  774. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  775. circuit_build_failed(ocirc); /* take actions if necessary */
  776. circuit_rep_hist_note_result(ocirc);
  777. }
  778. }
  779. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  780. if (circuits_pending_or_conns)
  781. smartlist_remove(circuits_pending_or_conns, circ);
  782. }
  783. if (CIRCUIT_IS_ORIGIN(circ)) {
  784. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  785. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  786. orig_reason);
  787. }
  788. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  789. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  790. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  791. tor_assert(ocirc->build_state->chosen_exit);
  792. /* treat this like getting a nack from it */
  793. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  794. "Removing from descriptor.",
  795. safe_str(ocirc->rend_query),
  796. safe_str(build_state_get_exit_nickname(ocirc->build_state)));
  797. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  798. ocirc->rend_query);
  799. }
  800. if (circ->n_conn)
  801. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  802. if (! CIRCUIT_IS_ORIGIN(circ)) {
  803. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  804. edge_connection_t *conn;
  805. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  806. connection_edge_destroy(or_circ->p_circ_id, conn);
  807. while (or_circ->resolving_streams) {
  808. conn = or_circ->resolving_streams;
  809. or_circ->resolving_streams = conn->next_stream;
  810. if (!conn->_base.marked_for_close) {
  811. /* The other side will see a DESTROY, and infer that the connections
  812. * are closing because the circuit is getting torn down. No need
  813. * to send an end cell. */
  814. conn->_base.edge_has_sent_end = 1;
  815. conn->end_reason = END_STREAM_REASON_DESTROY;
  816. connection_mark_for_close(TO_CONN(conn));
  817. }
  818. conn->on_circuit = NULL;
  819. }
  820. if (or_circ->p_conn)
  821. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  822. } else {
  823. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  824. edge_connection_t *conn;
  825. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  826. connection_edge_destroy(circ->n_circ_id, conn);
  827. }
  828. circ->marked_for_close = line;
  829. circ->marked_for_close_file = file;
  830. if (! CIRCUIT_IS_ORIGIN(circ)) {
  831. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  832. if (or_circ->rend_splice) {
  833. if (!or_circ->rend_splice->_base.marked_for_close) {
  834. /* do this after marking this circuit, to avoid infinite recursion. */
  835. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  836. }
  837. or_circ->rend_splice = NULL;
  838. }
  839. }
  840. }
  841. /** Verify that cpath layer <b>cp</b> has all of its invariants
  842. * correct. Trigger an assert if anything is invalid.
  843. */
  844. void
  845. assert_cpath_layer_ok(const crypt_path_t *cp)
  846. {
  847. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  848. // tor_assert(cp->port);
  849. tor_assert(cp);
  850. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  851. switch (cp->state)
  852. {
  853. case CPATH_STATE_OPEN:
  854. tor_assert(cp->f_crypto);
  855. tor_assert(cp->b_crypto);
  856. /* fall through */
  857. case CPATH_STATE_CLOSED:
  858. tor_assert(!cp->dh_handshake_state);
  859. break;
  860. case CPATH_STATE_AWAITING_KEYS:
  861. /* tor_assert(cp->dh_handshake_state); */
  862. break;
  863. default:
  864. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  865. tor_assert(0);
  866. }
  867. tor_assert(cp->package_window >= 0);
  868. tor_assert(cp->deliver_window >= 0);
  869. }
  870. /** Verify that cpath <b>cp</b> has all of its invariants
  871. * correct. Trigger an assert if anything is invalid.
  872. */
  873. static void
  874. assert_cpath_ok(const crypt_path_t *cp)
  875. {
  876. const crypt_path_t *start = cp;
  877. do {
  878. assert_cpath_layer_ok(cp);
  879. /* layers must be in sequence of: "open* awaiting? closed*" */
  880. if (cp != start) {
  881. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  882. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  883. } else if (cp->state == CPATH_STATE_OPEN) {
  884. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  885. }
  886. }
  887. cp = cp->next;
  888. tor_assert(cp);
  889. } while (cp != start);
  890. }
  891. /** Verify that circuit <b>c</b> has all of its invariants
  892. * correct. Trigger an assert if anything is invalid.
  893. */
  894. void
  895. assert_circuit_ok(const circuit_t *c)
  896. {
  897. edge_connection_t *conn;
  898. const or_circuit_t *or_circ = NULL;
  899. const origin_circuit_t *origin_circ = NULL;
  900. tor_assert(c);
  901. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  902. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  903. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  904. if (CIRCUIT_IS_ORIGIN(c))
  905. origin_circ = TO_ORIGIN_CIRCUIT((circuit_t*)c);
  906. else
  907. or_circ = TO_OR_CIRCUIT((circuit_t*)c);
  908. if (c->n_conn) {
  909. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
  910. DIGEST_LEN));
  911. if (c->n_circ_id)
  912. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  913. }
  914. if (or_circ && or_circ->p_conn) {
  915. if (or_circ->p_circ_id)
  916. tor_assert(c == circuit_get_by_circid_orconn(or_circ->p_circ_id,
  917. or_circ->p_conn));
  918. }
  919. #if 0 /* false now that rendezvous exits are attached to p_streams */
  920. if (origin_circ)
  921. for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
  922. tor_assert(conn->_base.type == CONN_TYPE_AP);
  923. #endif
  924. if (or_circ)
  925. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  926. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  927. tor_assert(c->deliver_window >= 0);
  928. tor_assert(c->package_window >= 0);
  929. if (c->state == CIRCUIT_STATE_OPEN) {
  930. tor_assert(!c->onionskin);
  931. if (or_circ) {
  932. tor_assert(or_circ->n_crypto);
  933. tor_assert(or_circ->p_crypto);
  934. tor_assert(or_circ->n_digest);
  935. tor_assert(or_circ->p_digest);
  936. }
  937. }
  938. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  939. tor_assert(circuits_pending_or_conns &&
  940. smartlist_isin(circuits_pending_or_conns, c));
  941. } else {
  942. tor_assert(!circuits_pending_or_conns ||
  943. !smartlist_isin(circuits_pending_or_conns, c));
  944. }
  945. if (origin_circ && origin_circ->cpath) {
  946. assert_cpath_ok(origin_circ->cpath);
  947. }
  948. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  949. tor_assert(or_circ);
  950. if (!c->marked_for_close) {
  951. tor_assert(or_circ->rend_splice);
  952. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  953. }
  954. tor_assert(or_circ->rend_splice != or_circ);
  955. } else {
  956. tor_assert(!or_circ || !or_circ->rend_splice);
  957. }
  958. }