circuitlist.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circuitlist.c
  8. * \brief Manage the global circuit list.
  9. **/
  10. #include "or.h"
  11. #include "circuitbuild.h"
  12. #include "circuitlist.h"
  13. #include "circuituse.h"
  14. #include "connection.h"
  15. #include "config.h"
  16. #include "connection_edge.h"
  17. #include "connection_or.h"
  18. #include "control.h"
  19. #include "networkstatus.h"
  20. #include "onion.h"
  21. #include "relay.h"
  22. #include "rendclient.h"
  23. #include "rendcommon.h"
  24. #include "rephist.h"
  25. #include "routerlist.h"
  26. #include "ht.h"
  27. /********* START VARIABLES **********/
  28. /** A global list of all circuits at this hop. */
  29. circuit_t *global_circuitlist=NULL;
  30. /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
  31. static smartlist_t *circuits_pending_or_conns=NULL;
  32. static void circuit_free(circuit_t *circ);
  33. static void circuit_free_cpath(crypt_path_t *cpath);
  34. static void circuit_free_cpath_node(crypt_path_t *victim);
  35. /********* END VARIABLES ************/
  36. /** A map from OR connection and circuit ID to circuit. (Lookup performance is
  37. * very important here, since we need to do it every time a cell arrives.) */
  38. typedef struct orconn_circid_circuit_map_t {
  39. HT_ENTRY(orconn_circid_circuit_map_t) node;
  40. or_connection_t *or_conn;
  41. circid_t circ_id;
  42. circuit_t *circuit;
  43. } orconn_circid_circuit_map_t;
  44. /** Helper for hash tables: compare the OR connection and circuit ID for a and
  45. * b, and return less than, equal to, or greater than zero appropriately.
  46. */
  47. static INLINE int
  48. _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
  49. orconn_circid_circuit_map_t *b)
  50. {
  51. return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
  52. }
  53. /** Helper: return a hash based on circuit ID and the pointer value of
  54. * or_conn in <b>a</b>. */
  55. static INLINE unsigned int
  56. _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
  57. {
  58. return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->or_conn);
  59. }
  60. /** Map from [orconn,circid] to circuit. */
  61. static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
  62. orconn_circid_circuit_map = HT_INITIALIZER();
  63. HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  64. _orconn_circid_entry_hash, _orconn_circid_entries_eq)
  65. HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  66. _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
  67. malloc, realloc, free)
  68. /** The most recently returned entry from circuit_get_by_circid_orconn;
  69. * used to improve performance when many cells arrive in a row from the
  70. * same circuit.
  71. */
  72. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  73. /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
  74. * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
  75. * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
  76. * the old entry (if any) and adding a new one. If <b>active</b> is true,
  77. * remove the circuit from the list of active circuits on old_conn and add it
  78. * to the list of active circuits on conn.
  79. * XXX "active" isn't an arg anymore */
  80. static void
  81. circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
  82. circid_t id,
  83. or_connection_t *conn)
  84. {
  85. orconn_circid_circuit_map_t search;
  86. orconn_circid_circuit_map_t *found;
  87. or_connection_t *old_conn, **conn_ptr;
  88. circid_t old_id, *circid_ptr;
  89. int was_active, make_active;
  90. if (direction == CELL_DIRECTION_OUT) {
  91. conn_ptr = &circ->n_conn;
  92. circid_ptr = &circ->n_circ_id;
  93. was_active = circ->next_active_on_n_conn != NULL;
  94. make_active = circ->n_conn_cells.n > 0;
  95. } else {
  96. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  97. conn_ptr = &c->p_conn;
  98. circid_ptr = &c->p_circ_id;
  99. was_active = c->next_active_on_p_conn != NULL;
  100. make_active = c->p_conn_cells.n > 0;
  101. }
  102. old_conn = *conn_ptr;
  103. old_id = *circid_ptr;
  104. if (id == old_id && conn == old_conn)
  105. return;
  106. if (_last_circid_orconn_ent &&
  107. ((old_id == _last_circid_orconn_ent->circ_id &&
  108. old_conn == _last_circid_orconn_ent->or_conn) ||
  109. (id == _last_circid_orconn_ent->circ_id &&
  110. conn == _last_circid_orconn_ent->or_conn))) {
  111. _last_circid_orconn_ent = NULL;
  112. }
  113. if (old_conn) { /* we may need to remove it from the conn-circid map */
  114. tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
  115. search.circ_id = old_id;
  116. search.or_conn = old_conn;
  117. found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
  118. if (found) {
  119. tor_free(found);
  120. --old_conn->n_circuits;
  121. }
  122. if (was_active && old_conn != conn)
  123. make_circuit_inactive_on_conn(circ,old_conn);
  124. }
  125. /* Change the values only after we have possibly made the circuit inactive
  126. * on the previous conn. */
  127. *conn_ptr = conn;
  128. *circid_ptr = id;
  129. if (conn == NULL)
  130. return;
  131. /* now add the new one to the conn-circid map */
  132. search.circ_id = id;
  133. search.or_conn = conn;
  134. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  135. if (found) {
  136. found->circuit = circ;
  137. } else {
  138. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  139. found->circ_id = id;
  140. found->or_conn = conn;
  141. found->circuit = circ;
  142. HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
  143. }
  144. if (make_active && old_conn != conn)
  145. make_circuit_active_on_conn(circ,conn);
  146. ++conn->n_circuits;
  147. }
  148. /** Set the p_conn field of a circuit <b>circ</b>, along
  149. * with the corresponding circuit ID, and add the circuit as appropriate
  150. * to the (orconn,id)-\>circuit map. */
  151. void
  152. circuit_set_p_circid_orconn(or_circuit_t *circ, circid_t id,
  153. or_connection_t *conn)
  154. {
  155. circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
  156. id, conn);
  157. if (conn)
  158. tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
  159. }
  160. /** Set the n_conn field of a circuit <b>circ</b>, along
  161. * with the corresponding circuit ID, and add the circuit as appropriate
  162. * to the (orconn,id)-\>circuit map. */
  163. void
  164. circuit_set_n_circid_orconn(circuit_t *circ, circid_t id,
  165. or_connection_t *conn)
  166. {
  167. circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
  168. if (conn)
  169. tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
  170. }
  171. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  172. * it from lists as appropriate. */
  173. void
  174. circuit_set_state(circuit_t *circ, uint8_t state)
  175. {
  176. tor_assert(circ);
  177. if (state == circ->state)
  178. return;
  179. if (!circuits_pending_or_conns)
  180. circuits_pending_or_conns = smartlist_create();
  181. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  182. /* remove from waiting-circuit list. */
  183. smartlist_remove(circuits_pending_or_conns, circ);
  184. }
  185. if (state == CIRCUIT_STATE_OR_WAIT) {
  186. /* add to waiting-circuit list. */
  187. smartlist_add(circuits_pending_or_conns, circ);
  188. }
  189. if (state == CIRCUIT_STATE_OPEN)
  190. tor_assert(!circ->n_conn_onionskin);
  191. circ->state = state;
  192. }
  193. /** Add <b>circ</b> to the global list of circuits. This is called only from
  194. * within circuit_new.
  195. */
  196. static void
  197. circuit_add(circuit_t *circ)
  198. {
  199. if (!global_circuitlist) { /* first one */
  200. global_circuitlist = circ;
  201. circ->next = NULL;
  202. } else {
  203. circ->next = global_circuitlist;
  204. global_circuitlist = circ;
  205. }
  206. }
  207. /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
  208. * the given connection. */
  209. void
  210. circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
  211. {
  212. tor_assert(out);
  213. tor_assert(or_conn);
  214. if (!circuits_pending_or_conns)
  215. return;
  216. SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns, circuit_t *, circ) {
  217. if (circ->marked_for_close)
  218. continue;
  219. if (!circ->n_hop)
  220. continue;
  221. tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  222. if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
  223. /* Look at addr/port. This is an unkeyed connection. */
  224. if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
  225. circ->n_hop->port != or_conn->_base.port)
  226. continue;
  227. } else {
  228. /* We expected a key. See if it's the right one. */
  229. if (memcmp(or_conn->identity_digest,
  230. circ->n_hop->identity_digest, DIGEST_LEN))
  231. continue;
  232. }
  233. smartlist_add(out, circ);
  234. } SMARTLIST_FOREACH_END(circ);
  235. }
  236. /** Return the number of circuits in state OR_WAIT, waiting for the given
  237. * connection. */
  238. int
  239. circuit_count_pending_on_or_conn(or_connection_t *or_conn)
  240. {
  241. int cnt;
  242. smartlist_t *sl = smartlist_create();
  243. circuit_get_all_pending_on_or_conn(sl, or_conn);
  244. cnt = smartlist_len(sl);
  245. smartlist_free(sl);
  246. log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
  247. or_conn->nickname ? or_conn->nickname : "NULL", cnt);
  248. return cnt;
  249. }
  250. /** Detach from the global circuit list, and deallocate, all
  251. * circuits that have been marked for close.
  252. */
  253. void
  254. circuit_close_all_marked(void)
  255. {
  256. circuit_t *tmp,*m;
  257. while (global_circuitlist && global_circuitlist->marked_for_close) {
  258. tmp = global_circuitlist->next;
  259. circuit_free(global_circuitlist);
  260. global_circuitlist = tmp;
  261. }
  262. tmp = global_circuitlist;
  263. while (tmp && tmp->next) {
  264. if (tmp->next->marked_for_close) {
  265. m = tmp->next->next;
  266. circuit_free(tmp->next);
  267. tmp->next = m;
  268. /* Need to check new tmp->next; don't advance tmp. */
  269. } else {
  270. /* Advance tmp. */
  271. tmp = tmp->next;
  272. }
  273. }
  274. }
  275. /** Return the head of the global linked list of circuits. */
  276. circuit_t *
  277. _circuit_get_global_list(void)
  278. {
  279. return global_circuitlist;
  280. }
  281. /** Function to make circ-\>state human-readable */
  282. const char *
  283. circuit_state_to_string(int state)
  284. {
  285. static char buf[64];
  286. switch (state) {
  287. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  288. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  289. case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
  290. case CIRCUIT_STATE_OPEN: return "open";
  291. default:
  292. log_warn(LD_BUG, "Unknown circuit state %d", state);
  293. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  294. return buf;
  295. }
  296. }
  297. /** Map a circuit purpose to a string suitable to be displayed to a
  298. * controller. */
  299. const char *
  300. circuit_purpose_to_controller_string(uint8_t purpose)
  301. {
  302. static char buf[32];
  303. switch (purpose) {
  304. case CIRCUIT_PURPOSE_OR:
  305. case CIRCUIT_PURPOSE_INTRO_POINT:
  306. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  307. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  308. return "SERVER"; /* A controller should never see these, actually. */
  309. case CIRCUIT_PURPOSE_C_GENERAL:
  310. return "GENERAL";
  311. case CIRCUIT_PURPOSE_C_INTRODUCING:
  312. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  313. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  314. return "HS_CLIENT_INTRO";
  315. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  316. case CIRCUIT_PURPOSE_C_REND_READY:
  317. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  318. case CIRCUIT_PURPOSE_C_REND_JOINED:
  319. return "HS_CLIENT_REND";
  320. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  321. case CIRCUIT_PURPOSE_S_INTRO:
  322. return "HS_SERVICE_INTRO";
  323. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  324. case CIRCUIT_PURPOSE_S_REND_JOINED:
  325. return "HS_SERVICE_REND";
  326. case CIRCUIT_PURPOSE_TESTING:
  327. return "TESTING";
  328. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  329. return "MEASURE_TIMEOUT";
  330. case CIRCUIT_PURPOSE_CONTROLLER:
  331. return "CONTROLLER";
  332. default:
  333. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  334. return buf;
  335. }
  336. }
  337. /** Pick a reasonable package_window to start out for our circuits.
  338. * Originally this was hard-coded at 1000, but now the consensus votes
  339. * on the answer. See proposal 168. */
  340. int32_t
  341. circuit_initial_package_window(void)
  342. {
  343. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START);
  344. /* If the consensus tells us a negative number, we'd assert. */
  345. if (num < 0)
  346. num = CIRCWINDOW_START;
  347. return num;
  348. }
  349. /** Initialize the common elements in a circuit_t, and add it to the global
  350. * list. */
  351. static void
  352. init_circuit_base(circuit_t *circ)
  353. {
  354. circ->timestamp_created = time(NULL);
  355. tor_gettimeofday(&circ->highres_created);
  356. circ->package_window = circuit_initial_package_window();
  357. circ->deliver_window = CIRCWINDOW_START;
  358. /* Initialize the cell_ewma_t structure */
  359. circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  360. circ->n_cell_ewma.cell_count = 0.0;
  361. circ->n_cell_ewma.heap_index = -1;
  362. circ->n_cell_ewma.is_for_p_conn = 0;
  363. circuit_add(circ);
  364. }
  365. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  366. * and <b>p_conn</b>. Add it to the global circuit list.
  367. */
  368. origin_circuit_t *
  369. origin_circuit_new(void)
  370. {
  371. origin_circuit_t *circ;
  372. /* never zero, since a global ID of 0 is treated specially by the
  373. * controller */
  374. static uint32_t n_circuits_allocated = 1;
  375. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  376. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  377. circ->next_stream_id = crypto_rand_int(1<<16);
  378. circ->global_identifier = n_circuits_allocated++;
  379. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  380. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  381. init_circuit_base(TO_CIRCUIT(circ));
  382. circ_times.last_circ_at = approx_time();
  383. return circ;
  384. }
  385. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  386. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  387. or_circuit_t *
  388. or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
  389. {
  390. /* CircIDs */
  391. or_circuit_t *circ;
  392. circ = tor_malloc_zero(sizeof(or_circuit_t));
  393. circ->_base.magic = OR_CIRCUIT_MAGIC;
  394. if (p_conn)
  395. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  396. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  397. init_circuit_base(TO_CIRCUIT(circ));
  398. /* Initialize the cell_ewma_t structure */
  399. /* Initialize the cell counts to 0 */
  400. circ->p_cell_ewma.cell_count = 0.0;
  401. circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  402. circ->p_cell_ewma.is_for_p_conn = 1;
  403. /* It's not in any heap yet. */
  404. circ->p_cell_ewma.heap_index = -1;
  405. return circ;
  406. }
  407. /** Deallocate space associated with circ.
  408. */
  409. static void
  410. circuit_free(circuit_t *circ)
  411. {
  412. void *mem;
  413. size_t memlen;
  414. if (!circ)
  415. return;
  416. if (CIRCUIT_IS_ORIGIN(circ)) {
  417. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  418. mem = ocirc;
  419. memlen = sizeof(origin_circuit_t);
  420. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  421. if (ocirc->build_state) {
  422. extend_info_free(ocirc->build_state->chosen_exit);
  423. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  424. }
  425. tor_free(ocirc->build_state);
  426. circuit_free_cpath(ocirc->cpath);
  427. crypto_free_pk_env(ocirc->intro_key);
  428. rend_data_free(ocirc->rend_data);
  429. } else {
  430. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  431. /* Remember cell statistics for this circuit before deallocating. */
  432. if (get_options()->CellStatistics)
  433. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  434. mem = ocirc;
  435. memlen = sizeof(or_circuit_t);
  436. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  437. crypto_free_cipher_env(ocirc->p_crypto);
  438. crypto_free_digest_env(ocirc->p_digest);
  439. crypto_free_cipher_env(ocirc->n_crypto);
  440. crypto_free_digest_env(ocirc->n_digest);
  441. if (ocirc->rend_splice) {
  442. or_circuit_t *other = ocirc->rend_splice;
  443. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  444. other->rend_splice = NULL;
  445. }
  446. /* remove from map. */
  447. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  448. /* Clear cell queue _after_ removing it from the map. Otherwise our
  449. * "active" checks will be violated. */
  450. cell_queue_clear(&ocirc->p_conn_cells);
  451. }
  452. extend_info_free(circ->n_hop);
  453. tor_free(circ->n_conn_onionskin);
  454. /* Remove from map. */
  455. circuit_set_n_circid_orconn(circ, 0, NULL);
  456. /* Clear cell queue _after_ removing it from the map. Otherwise our
  457. * "active" checks will be violated. */
  458. cell_queue_clear(&circ->n_conn_cells);
  459. memset(mem, 0xAA, memlen); /* poison memory */
  460. tor_free(mem);
  461. }
  462. /** Deallocate space associated with the linked list <b>cpath</b>. */
  463. static void
  464. circuit_free_cpath(crypt_path_t *cpath)
  465. {
  466. crypt_path_t *victim, *head=cpath;
  467. if (!cpath)
  468. return;
  469. /* it's a doubly linked list, so we have to notice when we've
  470. * gone through it once. */
  471. while (cpath->next && cpath->next != head) {
  472. victim = cpath;
  473. cpath = victim->next;
  474. circuit_free_cpath_node(victim);
  475. }
  476. circuit_free_cpath_node(cpath);
  477. }
  478. /** Release all storage held by circuits. */
  479. void
  480. circuit_free_all(void)
  481. {
  482. circuit_t *next;
  483. while (global_circuitlist) {
  484. next = global_circuitlist->next;
  485. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  486. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  487. while (or_circ->resolving_streams) {
  488. edge_connection_t *next_conn;
  489. next_conn = or_circ->resolving_streams->next_stream;
  490. connection_free(TO_CONN(or_circ->resolving_streams));
  491. or_circ->resolving_streams = next_conn;
  492. }
  493. }
  494. circuit_free(global_circuitlist);
  495. global_circuitlist = next;
  496. }
  497. smartlist_free(circuits_pending_or_conns);
  498. circuits_pending_or_conns = NULL;
  499. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  500. }
  501. /** Deallocate space associated with the cpath node <b>victim</b>. */
  502. static void
  503. circuit_free_cpath_node(crypt_path_t *victim)
  504. {
  505. if (!victim)
  506. return;
  507. crypto_free_cipher_env(victim->f_crypto);
  508. crypto_free_cipher_env(victim->b_crypto);
  509. crypto_free_digest_env(victim->f_digest);
  510. crypto_free_digest_env(victim->b_digest);
  511. crypto_dh_free(victim->dh_handshake_state);
  512. extend_info_free(victim->extend_info);
  513. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  514. tor_free(victim);
  515. }
  516. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  517. * of information about circuit <b>circ</b>.
  518. */
  519. static void
  520. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  521. const char *type, int this_circid, int other_circid)
  522. {
  523. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  524. "state %d (%s), born %d:",
  525. conn_array_index, type, this_circid, other_circid, circ->state,
  526. circuit_state_to_string(circ->state), (int)circ->timestamp_created);
  527. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  528. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  529. }
  530. }
  531. /** Log, at severity <b>severity</b>, information about each circuit
  532. * that is connected to <b>conn</b>.
  533. */
  534. void
  535. circuit_dump_by_conn(connection_t *conn, int severity)
  536. {
  537. circuit_t *circ;
  538. edge_connection_t *tmpconn;
  539. for (circ=global_circuitlist;circ;circ = circ->next) {
  540. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  541. if (circ->marked_for_close)
  542. continue;
  543. if (! CIRCUIT_IS_ORIGIN(circ))
  544. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  545. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  546. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  547. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  548. p_circ_id, n_circ_id);
  549. if (CIRCUIT_IS_ORIGIN(circ)) {
  550. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  551. tmpconn=tmpconn->next_stream) {
  552. if (TO_CONN(tmpconn) == conn) {
  553. circuit_dump_details(severity, circ, conn->conn_array_index,
  554. "App-ward", p_circ_id, n_circ_id);
  555. }
  556. }
  557. }
  558. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  559. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  560. n_circ_id, p_circ_id);
  561. if (! CIRCUIT_IS_ORIGIN(circ)) {
  562. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  563. tmpconn=tmpconn->next_stream) {
  564. if (TO_CONN(tmpconn) == conn) {
  565. circuit_dump_details(severity, circ, conn->conn_array_index,
  566. "Exit-ward", n_circ_id, p_circ_id);
  567. }
  568. }
  569. }
  570. if (!circ->n_conn && circ->n_hop &&
  571. tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
  572. circ->n_hop->port == conn->port &&
  573. conn->type == CONN_TYPE_OR &&
  574. !memcmp(TO_OR_CONN(conn)->identity_digest,
  575. circ->n_hop->identity_digest, DIGEST_LEN)) {
  576. circuit_dump_details(severity, circ, conn->conn_array_index,
  577. (circ->state == CIRCUIT_STATE_OPEN &&
  578. !CIRCUIT_IS_ORIGIN(circ)) ?
  579. "Endpoint" : "Pending",
  580. n_circ_id, p_circ_id);
  581. }
  582. }
  583. }
  584. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  585. * such circuit exists. */
  586. origin_circuit_t *
  587. circuit_get_by_global_id(uint32_t id)
  588. {
  589. circuit_t *circ;
  590. for (circ=global_circuitlist;circ;circ = circ->next) {
  591. if (CIRCUIT_IS_ORIGIN(circ) &&
  592. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  593. if (circ->marked_for_close)
  594. return NULL;
  595. else
  596. return TO_ORIGIN_CIRCUIT(circ);
  597. }
  598. }
  599. return NULL;
  600. }
  601. /** Return a circ such that:
  602. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  603. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  604. * Return NULL if no such circuit exists.
  605. */
  606. static INLINE circuit_t *
  607. circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
  608. {
  609. orconn_circid_circuit_map_t search;
  610. orconn_circid_circuit_map_t *found;
  611. if (_last_circid_orconn_ent &&
  612. circ_id == _last_circid_orconn_ent->circ_id &&
  613. conn == _last_circid_orconn_ent->or_conn) {
  614. found = _last_circid_orconn_ent;
  615. } else {
  616. search.circ_id = circ_id;
  617. search.or_conn = conn;
  618. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  619. _last_circid_orconn_ent = found;
  620. }
  621. if (found && found->circuit)
  622. return found->circuit;
  623. return NULL;
  624. /* The rest of this checks for bugs. Disabled by default. */
  625. {
  626. circuit_t *circ;
  627. for (circ=global_circuitlist;circ;circ = circ->next) {
  628. if (! CIRCUIT_IS_ORIGIN(circ)) {
  629. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  630. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  631. log_warn(LD_BUG,
  632. "circuit matches p_conn, but not in hash table (Bug!)");
  633. return circ;
  634. }
  635. }
  636. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  637. log_warn(LD_BUG,
  638. "circuit matches n_conn, but not in hash table (Bug!)");
  639. return circ;
  640. }
  641. }
  642. return NULL;
  643. }
  644. }
  645. /** Return a circ such that:
  646. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  647. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  648. * - circ is not marked for close.
  649. * Return NULL if no such circuit exists.
  650. */
  651. circuit_t *
  652. circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
  653. {
  654. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  655. if (!circ || circ->marked_for_close)
  656. return NULL;
  657. else
  658. return circ;
  659. }
  660. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  661. * circuit, marked or not, on <b>conn</b>. */
  662. int
  663. circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
  664. {
  665. return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
  666. }
  667. /** Return the circuit that a given edge connection is using. */
  668. circuit_t *
  669. circuit_get_by_edge_conn(edge_connection_t *conn)
  670. {
  671. circuit_t *circ;
  672. circ = conn->on_circuit;
  673. tor_assert(!circ ||
  674. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  675. : circ->magic == OR_CIRCUIT_MAGIC));
  676. return circ;
  677. }
  678. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  679. * circuit from the orconn,circid map, and mark it for close if it hasn't
  680. * been marked already.
  681. */
  682. void
  683. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  684. {
  685. circuit_t *circ;
  686. connection_or_unlink_all_active_circs(conn);
  687. for (circ = global_circuitlist; circ; circ = circ->next) {
  688. int mark = 0;
  689. if (circ->n_conn == conn) {
  690. circuit_set_n_circid_orconn(circ, 0, NULL);
  691. mark = 1;
  692. }
  693. if (! CIRCUIT_IS_ORIGIN(circ)) {
  694. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  695. if (or_circ->p_conn == conn) {
  696. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  697. mark = 1;
  698. }
  699. }
  700. if (mark && !circ->marked_for_close)
  701. circuit_mark_for_close(circ, reason);
  702. }
  703. }
  704. /** Return a circ such that:
  705. * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
  706. * - circ-\>purpose is equal to <b>purpose</b>.
  707. *
  708. * Return NULL if no such circuit exists.
  709. */
  710. origin_circuit_t *
  711. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  712. {
  713. circuit_t *circ;
  714. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  715. for (circ = global_circuitlist; circ; circ = circ->next) {
  716. if (!circ->marked_for_close &&
  717. circ->purpose == purpose) {
  718. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  719. if (ocirc->rend_data &&
  720. !rend_cmp_service_ids(rend_query,
  721. ocirc->rend_data->onion_address))
  722. return ocirc;
  723. }
  724. }
  725. return NULL;
  726. }
  727. /** Return the first circuit originating here in global_circuitlist after
  728. * <b>start</b> whose purpose is <b>purpose</b>, and where
  729. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  730. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  731. */
  732. origin_circuit_t *
  733. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  734. const char *digest, uint8_t purpose)
  735. {
  736. circuit_t *circ;
  737. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  738. if (start == NULL)
  739. circ = global_circuitlist;
  740. else
  741. circ = TO_CIRCUIT(start)->next;
  742. for ( ; circ; circ = circ->next) {
  743. if (circ->marked_for_close)
  744. continue;
  745. if (circ->purpose != purpose)
  746. continue;
  747. if (!digest)
  748. return TO_ORIGIN_CIRCUIT(circ);
  749. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  750. !memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  751. digest, DIGEST_LEN))
  752. return TO_ORIGIN_CIRCUIT(circ);
  753. }
  754. return NULL;
  755. }
  756. /** Return the first OR circuit in the global list whose purpose is
  757. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  758. * <b>token</b>. */
  759. static or_circuit_t *
  760. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  761. size_t len)
  762. {
  763. circuit_t *circ;
  764. for (circ = global_circuitlist; circ; circ = circ->next) {
  765. if (! circ->marked_for_close &&
  766. circ->purpose == purpose &&
  767. ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  768. return TO_OR_CIRCUIT(circ);
  769. }
  770. return NULL;
  771. }
  772. /** Return the circuit waiting for a rendezvous with the provided cookie.
  773. * Return NULL if no such circuit is found.
  774. */
  775. or_circuit_t *
  776. circuit_get_rendezvous(const char *cookie)
  777. {
  778. return circuit_get_by_rend_token_and_purpose(
  779. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  780. cookie, REND_COOKIE_LEN);
  781. }
  782. /** Return the circuit waiting for intro cells of the given digest.
  783. * Return NULL if no such circuit is found.
  784. */
  785. or_circuit_t *
  786. circuit_get_intro_point(const char *digest)
  787. {
  788. return circuit_get_by_rend_token_and_purpose(
  789. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  790. DIGEST_LEN);
  791. }
  792. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  793. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  794. * flags in <b>flags</b>, and if info is defined, does not already use info
  795. * as any of its hops; or NULL if no circuit fits this description.
  796. *
  797. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  798. * the circuit we want to create, not the purpose of the circuit we want to
  799. * cannibalize.
  800. *
  801. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  802. */
  803. origin_circuit_t *
  804. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  805. int flags)
  806. {
  807. circuit_t *_circ;
  808. origin_circuit_t *best=NULL;
  809. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  810. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  811. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  812. /* Make sure we're not trying to create a onehop circ by
  813. * cannibalization. */
  814. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  815. log_debug(LD_CIRC,
  816. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  817. "capacity %d, internal %d",
  818. purpose, need_uptime, need_capacity, internal);
  819. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  820. if (CIRCUIT_IS_ORIGIN(_circ) &&
  821. _circ->state == CIRCUIT_STATE_OPEN &&
  822. !_circ->marked_for_close &&
  823. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  824. !_circ->timestamp_dirty) {
  825. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  826. if ((!need_uptime || circ->build_state->need_uptime) &&
  827. (!need_capacity || circ->build_state->need_capacity) &&
  828. (internal == circ->build_state->is_internal) &&
  829. circ->remaining_relay_early_cells &&
  830. !circ->build_state->onehop_tunnel) {
  831. if (info) {
  832. /* need to make sure we don't duplicate hops */
  833. crypt_path_t *hop = circ->cpath;
  834. routerinfo_t *ri1 = router_get_by_digest(info->identity_digest);
  835. do {
  836. routerinfo_t *ri2;
  837. if (!memcmp(hop->extend_info->identity_digest,
  838. info->identity_digest, DIGEST_LEN))
  839. goto next;
  840. if (ri1 &&
  841. (ri2 = router_get_by_digest(hop->extend_info->identity_digest))
  842. && routers_in_same_family(ri1, ri2))
  843. goto next;
  844. hop=hop->next;
  845. } while (hop!=circ->cpath);
  846. }
  847. if (!best || (best->build_state->need_uptime && !need_uptime))
  848. best = circ;
  849. next: ;
  850. }
  851. }
  852. }
  853. return best;
  854. }
  855. /** Return the number of hops in circuit's path. */
  856. int
  857. circuit_get_cpath_len(origin_circuit_t *circ)
  858. {
  859. int n = 0;
  860. if (circ && circ->cpath) {
  861. crypt_path_t *cpath, *cpath_next = NULL;
  862. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  863. cpath_next = cpath->next;
  864. ++n;
  865. }
  866. }
  867. return n;
  868. }
  869. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  870. * aren't that many hops in the list. */
  871. crypt_path_t *
  872. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  873. {
  874. if (circ && circ->cpath && hopnum > 0) {
  875. crypt_path_t *cpath, *cpath_next = NULL;
  876. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  877. cpath_next = cpath->next;
  878. if (--hopnum <= 0)
  879. return cpath;
  880. }
  881. }
  882. return NULL;
  883. }
  884. /** Go through the circuitlist; mark-for-close each circuit that starts
  885. * at us but has not yet been used. */
  886. void
  887. circuit_mark_all_unused_circs(void)
  888. {
  889. circuit_t *circ;
  890. for (circ=global_circuitlist; circ; circ = circ->next) {
  891. if (CIRCUIT_IS_ORIGIN(circ) &&
  892. !circ->marked_for_close &&
  893. !circ->timestamp_dirty)
  894. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  895. }
  896. }
  897. /** Go through the circuitlist; for each circuit that starts at us
  898. * and is dirty, frob its timestamp_dirty so we won't use it for any
  899. * new streams.
  900. *
  901. * This is useful for letting the user change pseudonyms, so new
  902. * streams will not be linkable to old streams.
  903. */
  904. void
  905. circuit_expire_all_dirty_circs(void)
  906. {
  907. circuit_t *circ;
  908. or_options_t *options = get_options();
  909. for (circ=global_circuitlist; circ; circ = circ->next) {
  910. if (CIRCUIT_IS_ORIGIN(circ) &&
  911. !circ->marked_for_close &&
  912. circ->timestamp_dirty)
  913. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  914. }
  915. }
  916. /** Mark <b>circ</b> to be closed next time we call
  917. * circuit_close_all_marked(). Do any cleanup needed:
  918. * - If state is onionskin_pending, remove circ from the onion_pending
  919. * list.
  920. * - If circ isn't open yet: call circuit_build_failed() if we're
  921. * the origin, and in either case call circuit_rep_hist_note_result()
  922. * to note stats.
  923. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  924. * just tried from our list of intro points for that service
  925. * descriptor.
  926. * - Send appropriate destroys and edge_destroys for conns and
  927. * streams attached to circ.
  928. * - If circ->rend_splice is set (we are the midpoint of a joined
  929. * rendezvous stream), then mark the other circuit to close as well.
  930. */
  931. void
  932. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  933. const char *file)
  934. {
  935. int orig_reason = reason; /* Passed to the controller */
  936. assert_circuit_ok(circ);
  937. tor_assert(line);
  938. tor_assert(file);
  939. if (circ->marked_for_close) {
  940. log(LOG_WARN,LD_BUG,
  941. "Duplicate call to circuit_mark_for_close at %s:%d"
  942. " (first at %s:%d)", file, line,
  943. circ->marked_for_close_file, circ->marked_for_close);
  944. return;
  945. }
  946. if (reason == END_CIRC_AT_ORIGIN) {
  947. if (!CIRCUIT_IS_ORIGIN(circ)) {
  948. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  949. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  950. file, line, circ->purpose);
  951. }
  952. reason = END_CIRC_REASON_NONE;
  953. }
  954. if (CIRCUIT_IS_ORIGIN(circ)) {
  955. /* We don't send reasons when closing circuits at the origin. */
  956. reason = END_CIRC_REASON_NONE;
  957. }
  958. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  959. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  960. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  961. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  962. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  963. reason = END_CIRC_REASON_NONE;
  964. }
  965. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  966. onion_pending_remove(TO_OR_CIRCUIT(circ));
  967. }
  968. /* If the circuit ever became OPEN, we sent it to the reputation history
  969. * module then. If it isn't OPEN, we send it there now to remember which
  970. * links worked and which didn't.
  971. */
  972. if (circ->state != CIRCUIT_STATE_OPEN) {
  973. if (CIRCUIT_IS_ORIGIN(circ)) {
  974. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  975. circuit_build_failed(ocirc); /* take actions if necessary */
  976. circuit_rep_hist_note_result(ocirc);
  977. }
  978. }
  979. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  980. if (circuits_pending_or_conns)
  981. smartlist_remove(circuits_pending_or_conns, circ);
  982. }
  983. if (CIRCUIT_IS_ORIGIN(circ)) {
  984. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  985. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  986. orig_reason);
  987. }
  988. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  989. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  990. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  991. tor_assert(ocirc->build_state->chosen_exit);
  992. tor_assert(ocirc->rend_data);
  993. /* treat this like getting a nack from it */
  994. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  995. "Removing from descriptor.",
  996. safe_str_client(ocirc->rend_data->onion_address),
  997. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  998. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  999. ocirc->rend_data);
  1000. }
  1001. if (circ->n_conn) {
  1002. circuit_clear_cell_queue(circ, circ->n_conn);
  1003. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  1004. }
  1005. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1006. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1007. edge_connection_t *conn;
  1008. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1009. connection_edge_destroy(or_circ->p_circ_id, conn);
  1010. or_circ->n_streams = NULL;
  1011. while (or_circ->resolving_streams) {
  1012. conn = or_circ->resolving_streams;
  1013. or_circ->resolving_streams = conn->next_stream;
  1014. if (!conn->_base.marked_for_close) {
  1015. /* The client will see a DESTROY, and infer that the connections
  1016. * are closing because the circuit is getting torn down. No need
  1017. * to send an end cell. */
  1018. conn->edge_has_sent_end = 1;
  1019. conn->end_reason = END_STREAM_REASON_DESTROY;
  1020. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1021. connection_mark_for_close(TO_CONN(conn));
  1022. }
  1023. conn->on_circuit = NULL;
  1024. }
  1025. if (or_circ->p_conn) {
  1026. circuit_clear_cell_queue(circ, or_circ->p_conn);
  1027. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  1028. }
  1029. } else {
  1030. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1031. edge_connection_t *conn;
  1032. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1033. connection_edge_destroy(circ->n_circ_id, conn);
  1034. ocirc->p_streams = NULL;
  1035. }
  1036. circ->marked_for_close = line;
  1037. circ->marked_for_close_file = file;
  1038. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1039. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1040. if (or_circ->rend_splice) {
  1041. if (!or_circ->rend_splice->_base.marked_for_close) {
  1042. /* do this after marking this circuit, to avoid infinite recursion. */
  1043. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1044. }
  1045. or_circ->rend_splice = NULL;
  1046. }
  1047. }
  1048. }
  1049. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1050. * correct. Trigger an assert if anything is invalid.
  1051. */
  1052. void
  1053. assert_cpath_layer_ok(const crypt_path_t *cp)
  1054. {
  1055. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1056. // tor_assert(cp->port);
  1057. tor_assert(cp);
  1058. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1059. switch (cp->state)
  1060. {
  1061. case CPATH_STATE_OPEN:
  1062. tor_assert(cp->f_crypto);
  1063. tor_assert(cp->b_crypto);
  1064. /* fall through */
  1065. case CPATH_STATE_CLOSED:
  1066. tor_assert(!cp->dh_handshake_state);
  1067. break;
  1068. case CPATH_STATE_AWAITING_KEYS:
  1069. /* tor_assert(cp->dh_handshake_state); */
  1070. break;
  1071. default:
  1072. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1073. tor_assert(0);
  1074. }
  1075. tor_assert(cp->package_window >= 0);
  1076. tor_assert(cp->deliver_window >= 0);
  1077. }
  1078. /** Verify that cpath <b>cp</b> has all of its invariants
  1079. * correct. Trigger an assert if anything is invalid.
  1080. */
  1081. static void
  1082. assert_cpath_ok(const crypt_path_t *cp)
  1083. {
  1084. const crypt_path_t *start = cp;
  1085. do {
  1086. assert_cpath_layer_ok(cp);
  1087. /* layers must be in sequence of: "open* awaiting? closed*" */
  1088. if (cp != start) {
  1089. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1090. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1091. } else if (cp->state == CPATH_STATE_OPEN) {
  1092. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1093. }
  1094. }
  1095. cp = cp->next;
  1096. tor_assert(cp);
  1097. } while (cp != start);
  1098. }
  1099. /** Verify that circuit <b>c</b> has all of its invariants
  1100. * correct. Trigger an assert if anything is invalid.
  1101. */
  1102. void
  1103. assert_circuit_ok(const circuit_t *c)
  1104. {
  1105. edge_connection_t *conn;
  1106. const or_circuit_t *or_circ = NULL;
  1107. const origin_circuit_t *origin_circ = NULL;
  1108. tor_assert(c);
  1109. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1110. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1111. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1112. {
  1113. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1114. * never understand. -NM. */
  1115. circuit_t *nonconst_circ = (circuit_t*) c;
  1116. if (CIRCUIT_IS_ORIGIN(c))
  1117. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1118. else
  1119. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1120. }
  1121. if (c->n_conn) {
  1122. tor_assert(!c->n_hop);
  1123. if (c->n_circ_id) {
  1124. /* We use the _impl variant here to make sure we don't fail on marked
  1125. * circuits, which would not be returned by the regular function. */
  1126. circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
  1127. c->n_conn);
  1128. tor_assert(c == c2);
  1129. }
  1130. }
  1131. if (or_circ && or_circ->p_conn) {
  1132. if (or_circ->p_circ_id) {
  1133. /* ibid */
  1134. circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
  1135. or_circ->p_conn);
  1136. tor_assert(c == c2);
  1137. }
  1138. }
  1139. if (or_circ)
  1140. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1141. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1142. tor_assert(c->deliver_window >= 0);
  1143. tor_assert(c->package_window >= 0);
  1144. if (c->state == CIRCUIT_STATE_OPEN) {
  1145. tor_assert(!c->n_conn_onionskin);
  1146. if (or_circ) {
  1147. tor_assert(or_circ->n_crypto);
  1148. tor_assert(or_circ->p_crypto);
  1149. tor_assert(or_circ->n_digest);
  1150. tor_assert(or_circ->p_digest);
  1151. }
  1152. }
  1153. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1154. tor_assert(circuits_pending_or_conns &&
  1155. smartlist_isin(circuits_pending_or_conns, c));
  1156. } else {
  1157. tor_assert(!circuits_pending_or_conns ||
  1158. !smartlist_isin(circuits_pending_or_conns, c));
  1159. }
  1160. if (origin_circ && origin_circ->cpath) {
  1161. assert_cpath_ok(origin_circ->cpath);
  1162. }
  1163. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1164. tor_assert(or_circ);
  1165. if (!c->marked_for_close) {
  1166. tor_assert(or_circ->rend_splice);
  1167. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1168. }
  1169. tor_assert(or_circ->rend_splice != or_circ);
  1170. } else {
  1171. tor_assert(!or_circ || !or_circ->rend_splice);
  1172. }
  1173. }