circuitlist.c 41 KB

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