circuitlist.c 40 KB

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