circuitlist.c 41 KB

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