circuitlist.c 41 KB

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