circuitlist.c 41 KB

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