circuitlist.c 41 KB

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