circuitlist.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. circ->package_window = circuit_initial_package_window();
  339. circ->deliver_window = CIRCWINDOW_START;
  340. circuit_add(circ);
  341. }
  342. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  343. * and <b>p_conn</b>. Add it to the global circuit list.
  344. */
  345. origin_circuit_t *
  346. origin_circuit_new(void)
  347. {
  348. origin_circuit_t *circ;
  349. /* never zero, since a global ID of 0 is treated specially by the
  350. * controller */
  351. static uint32_t n_circuits_allocated = 1;
  352. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  353. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  354. circ->next_stream_id = crypto_rand_int(1<<16);
  355. circ->global_identifier = n_circuits_allocated++;
  356. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  357. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  358. init_circuit_base(TO_CIRCUIT(circ));
  359. return circ;
  360. }
  361. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  362. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  363. or_circuit_t *
  364. or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
  365. {
  366. /* CircIDs */
  367. or_circuit_t *circ;
  368. circ = tor_malloc_zero(sizeof(or_circuit_t));
  369. circ->_base.magic = OR_CIRCUIT_MAGIC;
  370. if (p_conn)
  371. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  372. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  373. init_circuit_base(TO_CIRCUIT(circ));
  374. return circ;
  375. }
  376. /** Deallocate space associated with circ.
  377. */
  378. static void
  379. circuit_free(circuit_t *circ)
  380. {
  381. void *mem;
  382. size_t memlen;
  383. tor_assert(circ);
  384. if (CIRCUIT_IS_ORIGIN(circ)) {
  385. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  386. mem = ocirc;
  387. memlen = sizeof(origin_circuit_t);
  388. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  389. if (ocirc->build_state) {
  390. if (ocirc->build_state->chosen_exit)
  391. extend_info_free(ocirc->build_state->chosen_exit);
  392. if (ocirc->build_state->pending_final_cpath)
  393. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  394. }
  395. tor_free(ocirc->build_state);
  396. circuit_free_cpath(ocirc->cpath);
  397. if (ocirc->intro_key)
  398. crypto_free_pk_env(ocirc->intro_key);
  399. if (ocirc->rend_data)
  400. rend_data_free(ocirc->rend_data);
  401. } else {
  402. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  403. mem = ocirc;
  404. memlen = sizeof(or_circuit_t);
  405. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  406. if (ocirc->p_crypto)
  407. crypto_free_cipher_env(ocirc->p_crypto);
  408. if (ocirc->p_digest)
  409. crypto_free_digest_env(ocirc->p_digest);
  410. if (ocirc->n_crypto)
  411. crypto_free_cipher_env(ocirc->n_crypto);
  412. if (ocirc->n_digest)
  413. crypto_free_digest_env(ocirc->n_digest);
  414. if (ocirc->rend_splice) {
  415. or_circuit_t *other = ocirc->rend_splice;
  416. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  417. other->rend_splice = NULL;
  418. }
  419. /* remove from map. */
  420. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  421. /* Clear cell queue _after_ removing it from the map. Otherwise our
  422. * "active" checks will be violated. */
  423. cell_queue_clear(&ocirc->p_conn_cells);
  424. }
  425. if (circ->n_hop)
  426. extend_info_free(circ->n_hop);
  427. tor_free(circ->n_conn_onionskin);
  428. /* Remove from map. */
  429. circuit_set_n_circid_orconn(circ, 0, NULL);
  430. /* Clear cell queue _after_ removing it from the map. Otherwise our
  431. * "active" checks will be violated. */
  432. cell_queue_clear(&circ->n_conn_cells);
  433. memset(circ, 0xAA, memlen); /* poison memory */
  434. tor_free(mem);
  435. }
  436. /** Deallocate space associated with the linked list <b>cpath</b>. */
  437. static void
  438. circuit_free_cpath(crypt_path_t *cpath)
  439. {
  440. crypt_path_t *victim, *head=cpath;
  441. if (!cpath)
  442. return;
  443. /* it's a doubly linked list, so we have to notice when we've
  444. * gone through it once. */
  445. while (cpath->next && cpath->next != head) {
  446. victim = cpath;
  447. cpath = victim->next;
  448. circuit_free_cpath_node(victim);
  449. }
  450. circuit_free_cpath_node(cpath);
  451. }
  452. /** Release all storage held by circuits. */
  453. void
  454. circuit_free_all(void)
  455. {
  456. circuit_t *next;
  457. while (global_circuitlist) {
  458. next = global_circuitlist->next;
  459. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  460. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  461. while (or_circ->resolving_streams) {
  462. edge_connection_t *next_conn;
  463. next_conn = or_circ->resolving_streams->next_stream;
  464. connection_free(TO_CONN(or_circ->resolving_streams));
  465. or_circ->resolving_streams = next_conn;
  466. }
  467. }
  468. circuit_free(global_circuitlist);
  469. global_circuitlist = next;
  470. }
  471. if (circuits_pending_or_conns) {
  472. smartlist_free(circuits_pending_or_conns);
  473. circuits_pending_or_conns = NULL;
  474. }
  475. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  476. }
  477. /** Deallocate space associated with the cpath node <b>victim</b>. */
  478. static void
  479. circuit_free_cpath_node(crypt_path_t *victim)
  480. {
  481. if (victim->f_crypto)
  482. crypto_free_cipher_env(victim->f_crypto);
  483. if (victim->b_crypto)
  484. crypto_free_cipher_env(victim->b_crypto);
  485. if (victim->f_digest)
  486. crypto_free_digest_env(victim->f_digest);
  487. if (victim->b_digest)
  488. crypto_free_digest_env(victim->b_digest);
  489. if (victim->dh_handshake_state)
  490. crypto_dh_free(victim->dh_handshake_state);
  491. if (victim->extend_info)
  492. extend_info_free(victim->extend_info);
  493. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  494. tor_free(victim);
  495. }
  496. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  497. * of information about circuit <b>circ</b>.
  498. */
  499. static void
  500. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  501. const char *type, int this_circid, int other_circid)
  502. {
  503. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  504. "state %d (%s), born %d:",
  505. conn_array_index, type, this_circid, other_circid, circ->state,
  506. circuit_state_to_string(circ->state), (int)circ->timestamp_created);
  507. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  508. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  509. }
  510. }
  511. /** Log, at severity <b>severity</b>, information about each circuit
  512. * that is connected to <b>conn</b>.
  513. */
  514. void
  515. circuit_dump_by_conn(connection_t *conn, int severity)
  516. {
  517. circuit_t *circ;
  518. edge_connection_t *tmpconn;
  519. for (circ=global_circuitlist;circ;circ = circ->next) {
  520. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  521. if (circ->marked_for_close)
  522. continue;
  523. if (! CIRCUIT_IS_ORIGIN(circ))
  524. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  525. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  526. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  527. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  528. p_circ_id, n_circ_id);
  529. if (CIRCUIT_IS_ORIGIN(circ)) {
  530. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  531. tmpconn=tmpconn->next_stream) {
  532. if (TO_CONN(tmpconn) == conn) {
  533. circuit_dump_details(severity, circ, conn->conn_array_index,
  534. "App-ward", p_circ_id, n_circ_id);
  535. }
  536. }
  537. }
  538. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  539. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  540. n_circ_id, p_circ_id);
  541. if (! CIRCUIT_IS_ORIGIN(circ)) {
  542. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  543. tmpconn=tmpconn->next_stream) {
  544. if (TO_CONN(tmpconn) == conn) {
  545. circuit_dump_details(severity, circ, conn->conn_array_index,
  546. "Exit-ward", n_circ_id, p_circ_id);
  547. }
  548. }
  549. }
  550. if (!circ->n_conn && circ->n_hop &&
  551. tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
  552. circ->n_hop->port == conn->port &&
  553. conn->type == CONN_TYPE_OR &&
  554. !memcmp(TO_OR_CONN(conn)->identity_digest,
  555. circ->n_hop->identity_digest, DIGEST_LEN)) {
  556. circuit_dump_details(severity, circ, conn->conn_array_index,
  557. (circ->state == CIRCUIT_STATE_OPEN &&
  558. !CIRCUIT_IS_ORIGIN(circ)) ?
  559. "Endpoint" : "Pending",
  560. n_circ_id, p_circ_id);
  561. }
  562. }
  563. }
  564. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  565. * such circuit exists. */
  566. origin_circuit_t *
  567. circuit_get_by_global_id(uint32_t id)
  568. {
  569. circuit_t *circ;
  570. for (circ=global_circuitlist;circ;circ = circ->next) {
  571. if (CIRCUIT_IS_ORIGIN(circ) &&
  572. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  573. if (circ->marked_for_close)
  574. return NULL;
  575. else
  576. return TO_ORIGIN_CIRCUIT(circ);
  577. }
  578. }
  579. return NULL;
  580. }
  581. /** Return a circ such that:
  582. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  583. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  584. * Return NULL if no such circuit exists.
  585. */
  586. static INLINE circuit_t *
  587. circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
  588. {
  589. orconn_circid_circuit_map_t search;
  590. orconn_circid_circuit_map_t *found;
  591. if (_last_circid_orconn_ent &&
  592. circ_id == _last_circid_orconn_ent->circ_id &&
  593. conn == _last_circid_orconn_ent->or_conn) {
  594. found = _last_circid_orconn_ent;
  595. } else {
  596. search.circ_id = circ_id;
  597. search.or_conn = conn;
  598. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  599. _last_circid_orconn_ent = found;
  600. }
  601. if (found && found->circuit)
  602. return found->circuit;
  603. return NULL;
  604. /* The rest of this checks for bugs. Disabled by default. */
  605. {
  606. circuit_t *circ;
  607. for (circ=global_circuitlist;circ;circ = circ->next) {
  608. if (! CIRCUIT_IS_ORIGIN(circ)) {
  609. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  610. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  611. log_warn(LD_BUG,
  612. "circuit matches p_conn, but not in hash table (Bug!)");
  613. return circ;
  614. }
  615. }
  616. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  617. log_warn(LD_BUG,
  618. "circuit matches n_conn, but not in hash table (Bug!)");
  619. return circ;
  620. }
  621. }
  622. return NULL;
  623. }
  624. }
  625. /** Return a circ such that:
  626. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  627. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  628. * - circ is not marked for close.
  629. * Return NULL if no such circuit exists.
  630. */
  631. circuit_t *
  632. circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
  633. {
  634. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  635. if (!circ || circ->marked_for_close)
  636. return NULL;
  637. else
  638. return circ;
  639. }
  640. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  641. * circuit, marked or not, on <b>conn</b>. */
  642. int
  643. circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
  644. {
  645. return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
  646. }
  647. /** Return the circuit that a given edge connection is using. */
  648. circuit_t *
  649. circuit_get_by_edge_conn(edge_connection_t *conn)
  650. {
  651. circuit_t *circ;
  652. circ = conn->on_circuit;
  653. tor_assert(!circ ||
  654. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  655. : circ->magic == OR_CIRCUIT_MAGIC));
  656. return circ;
  657. }
  658. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  659. * circuit from the orconn,circid map, and mark it for close if it hasn't
  660. * been marked already.
  661. */
  662. void
  663. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  664. {
  665. circuit_t *circ;
  666. connection_or_unlink_all_active_circs(conn);
  667. for (circ = global_circuitlist; circ; circ = circ->next) {
  668. int mark = 0;
  669. if (circ->n_conn == conn) {
  670. circuit_set_n_circid_orconn(circ, 0, NULL);
  671. mark = 1;
  672. }
  673. if (! CIRCUIT_IS_ORIGIN(circ)) {
  674. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  675. if (or_circ->p_conn == conn) {
  676. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  677. mark = 1;
  678. }
  679. }
  680. if (mark && !circ->marked_for_close)
  681. circuit_mark_for_close(circ, reason);
  682. }
  683. }
  684. /** Return a circ such that:
  685. * - circ-\>rend_data-\>query is equal to <b>rend_query</b>, and
  686. * - circ-\>purpose is equal to <b>purpose</b>.
  687. *
  688. * Return NULL if no such circuit exists.
  689. */
  690. origin_circuit_t *
  691. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  692. {
  693. circuit_t *circ;
  694. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  695. for (circ = global_circuitlist; circ; circ = circ->next) {
  696. if (!circ->marked_for_close &&
  697. circ->purpose == purpose) {
  698. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  699. if (ocirc->rend_data &&
  700. !rend_cmp_service_ids(rend_query,
  701. ocirc->rend_data->onion_address))
  702. return ocirc;
  703. }
  704. }
  705. return NULL;
  706. }
  707. /** Return the first circuit originating here in global_circuitlist after
  708. * <b>start</b> whose purpose is <b>purpose</b>, and where
  709. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  710. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  711. */
  712. origin_circuit_t *
  713. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  714. const char *digest, uint8_t purpose)
  715. {
  716. circuit_t *circ;
  717. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  718. if (start == NULL)
  719. circ = global_circuitlist;
  720. else
  721. circ = TO_CIRCUIT(start)->next;
  722. for ( ; circ; circ = circ->next) {
  723. if (circ->marked_for_close)
  724. continue;
  725. if (circ->purpose != purpose)
  726. continue;
  727. if (!digest)
  728. return TO_ORIGIN_CIRCUIT(circ);
  729. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  730. !memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  731. digest, DIGEST_LEN))
  732. return TO_ORIGIN_CIRCUIT(circ);
  733. }
  734. return NULL;
  735. }
  736. /** Return the first OR circuit in the global list whose purpose is
  737. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  738. * <b>token</b>. */
  739. static or_circuit_t *
  740. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  741. size_t len)
  742. {
  743. circuit_t *circ;
  744. for (circ = global_circuitlist; circ; circ = circ->next) {
  745. if (! circ->marked_for_close &&
  746. circ->purpose == purpose &&
  747. ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  748. return TO_OR_CIRCUIT(circ);
  749. }
  750. return NULL;
  751. }
  752. /** Return the circuit waiting for a rendezvous with the provided cookie.
  753. * Return NULL if no such circuit is found.
  754. */
  755. or_circuit_t *
  756. circuit_get_rendezvous(const char *cookie)
  757. {
  758. return circuit_get_by_rend_token_and_purpose(
  759. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  760. cookie, REND_COOKIE_LEN);
  761. }
  762. /** Return the circuit waiting for intro cells of the given digest.
  763. * Return NULL if no such circuit is found.
  764. */
  765. or_circuit_t *
  766. circuit_get_intro_point(const char *digest)
  767. {
  768. return circuit_get_by_rend_token_and_purpose(
  769. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  770. DIGEST_LEN);
  771. }
  772. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  773. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  774. * flags in <b>flags</b>, and if info is defined, does not already use info
  775. * as any of its hops; or NULL if no circuit fits this description.
  776. *
  777. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  778. * the circuit we want to create, not the purpose of the circuit we want to
  779. * cannibalize.
  780. *
  781. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  782. */
  783. origin_circuit_t *
  784. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  785. int flags)
  786. {
  787. circuit_t *_circ;
  788. origin_circuit_t *best=NULL;
  789. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  790. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  791. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  792. log_debug(LD_CIRC,
  793. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  794. "capacity %d, internal %d",
  795. purpose, need_uptime, need_capacity, internal);
  796. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  797. if (CIRCUIT_IS_ORIGIN(_circ) &&
  798. _circ->state == CIRCUIT_STATE_OPEN &&
  799. !_circ->marked_for_close &&
  800. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  801. !_circ->timestamp_dirty) {
  802. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  803. if ((!need_uptime || circ->build_state->need_uptime) &&
  804. (!need_capacity || circ->build_state->need_capacity) &&
  805. (internal == circ->build_state->is_internal) &&
  806. circ->remaining_relay_early_cells) {
  807. if (info) {
  808. /* need to make sure we don't duplicate hops */
  809. crypt_path_t *hop = circ->cpath;
  810. routerinfo_t *ri1 = router_get_by_digest(info->identity_digest);
  811. do {
  812. routerinfo_t *ri2;
  813. if (!memcmp(hop->extend_info->identity_digest,
  814. info->identity_digest, DIGEST_LEN))
  815. goto next;
  816. if (ri1 &&
  817. (ri2 = router_get_by_digest(hop->extend_info->identity_digest))
  818. && routers_in_same_family(ri1, ri2))
  819. goto next;
  820. hop=hop->next;
  821. } while (hop!=circ->cpath);
  822. }
  823. if (!best || (best->build_state->need_uptime && !need_uptime))
  824. best = circ;
  825. next: ;
  826. }
  827. }
  828. }
  829. return best;
  830. }
  831. /** Return the number of hops in circuit's path. */
  832. int
  833. circuit_get_cpath_len(origin_circuit_t *circ)
  834. {
  835. int n = 0;
  836. if (circ && circ->cpath) {
  837. crypt_path_t *cpath, *cpath_next = NULL;
  838. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  839. cpath_next = cpath->next;
  840. ++n;
  841. }
  842. }
  843. return n;
  844. }
  845. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  846. * aren't that many hops in the list. */
  847. crypt_path_t *
  848. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  849. {
  850. if (circ && circ->cpath && hopnum > 0) {
  851. crypt_path_t *cpath, *cpath_next = NULL;
  852. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  853. cpath_next = cpath->next;
  854. if (--hopnum <= 0)
  855. return cpath;
  856. }
  857. }
  858. return NULL;
  859. }
  860. /** Go through the circuitlist; mark-for-close each circuit that starts
  861. * at us but has not yet been used. */
  862. void
  863. circuit_mark_all_unused_circs(void)
  864. {
  865. circuit_t *circ;
  866. for (circ=global_circuitlist; circ; circ = circ->next) {
  867. if (CIRCUIT_IS_ORIGIN(circ) &&
  868. !circ->marked_for_close &&
  869. !circ->timestamp_dirty)
  870. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  871. }
  872. }
  873. /** Go through the circuitlist; for each circuit that starts at us
  874. * and is dirty, frob its timestamp_dirty so we won't use it for any
  875. * new streams.
  876. *
  877. * This is useful for letting the user change pseudonyms, so new
  878. * streams will not be linkable to old streams.
  879. */
  880. void
  881. circuit_expire_all_dirty_circs(void)
  882. {
  883. circuit_t *circ;
  884. or_options_t *options = get_options();
  885. for (circ=global_circuitlist; circ; circ = circ->next) {
  886. if (CIRCUIT_IS_ORIGIN(circ) &&
  887. !circ->marked_for_close &&
  888. circ->timestamp_dirty)
  889. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  890. }
  891. }
  892. /** Mark <b>circ</b> to be closed next time we call
  893. * circuit_close_all_marked(). Do any cleanup needed:
  894. * - If state is onionskin_pending, remove circ from the onion_pending
  895. * list.
  896. * - If circ isn't open yet: call circuit_build_failed() if we're
  897. * the origin, and in either case call circuit_rep_hist_note_result()
  898. * to note stats.
  899. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  900. * just tried from our list of intro points for that service
  901. * descriptor.
  902. * - Send appropriate destroys and edge_destroys for conns and
  903. * streams attached to circ.
  904. * - If circ->rend_splice is set (we are the midpoint of a joined
  905. * rendezvous stream), then mark the other circuit to close as well.
  906. */
  907. void
  908. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  909. const char *file)
  910. {
  911. int orig_reason = reason; /* Passed to the controller */
  912. assert_circuit_ok(circ);
  913. tor_assert(line);
  914. tor_assert(file);
  915. if (circ->marked_for_close) {
  916. log(LOG_WARN,LD_BUG,
  917. "Duplicate call to circuit_mark_for_close at %s:%d"
  918. " (first at %s:%d)", file, line,
  919. circ->marked_for_close_file, circ->marked_for_close);
  920. return;
  921. }
  922. if (reason == END_CIRC_AT_ORIGIN) {
  923. if (!CIRCUIT_IS_ORIGIN(circ)) {
  924. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  925. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  926. file, line, circ->purpose);
  927. }
  928. reason = END_CIRC_REASON_NONE;
  929. }
  930. if (CIRCUIT_IS_ORIGIN(circ)) {
  931. /* We don't send reasons when closing circuits at the origin. */
  932. reason = END_CIRC_REASON_NONE;
  933. }
  934. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  935. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  936. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  937. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  938. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  939. reason = END_CIRC_REASON_NONE;
  940. }
  941. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  942. onion_pending_remove(TO_OR_CIRCUIT(circ));
  943. }
  944. /* If the circuit ever became OPEN, we sent it to the reputation history
  945. * module then. If it isn't OPEN, we send it there now to remember which
  946. * links worked and which didn't.
  947. */
  948. if (circ->state != CIRCUIT_STATE_OPEN) {
  949. if (CIRCUIT_IS_ORIGIN(circ)) {
  950. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  951. circuit_build_failed(ocirc); /* take actions if necessary */
  952. circuit_rep_hist_note_result(ocirc);
  953. }
  954. }
  955. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  956. if (circuits_pending_or_conns)
  957. smartlist_remove(circuits_pending_or_conns, circ);
  958. }
  959. if (CIRCUIT_IS_ORIGIN(circ)) {
  960. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  961. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  962. orig_reason);
  963. }
  964. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  965. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  966. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  967. tor_assert(ocirc->build_state->chosen_exit);
  968. tor_assert(ocirc->rend_data);
  969. /* treat this like getting a nack from it */
  970. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  971. "Removing from descriptor.",
  972. safe_str(ocirc->rend_data->onion_address),
  973. safe_str(build_state_get_exit_nickname(ocirc->build_state)));
  974. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  975. ocirc->rend_data);
  976. }
  977. if (circ->n_conn)
  978. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  979. if (! CIRCUIT_IS_ORIGIN(circ)) {
  980. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  981. edge_connection_t *conn;
  982. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  983. connection_edge_destroy(or_circ->p_circ_id, conn);
  984. while (or_circ->resolving_streams) {
  985. conn = or_circ->resolving_streams;
  986. or_circ->resolving_streams = conn->next_stream;
  987. if (!conn->_base.marked_for_close) {
  988. /* The client will see a DESTROY, and infer that the connections
  989. * are closing because the circuit is getting torn down. No need
  990. * to send an end cell. */
  991. conn->edge_has_sent_end = 1;
  992. conn->end_reason = END_STREAM_REASON_DESTROY;
  993. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  994. connection_mark_for_close(TO_CONN(conn));
  995. }
  996. conn->on_circuit = NULL;
  997. }
  998. if (or_circ->p_conn)
  999. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  1000. } else {
  1001. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1002. edge_connection_t *conn;
  1003. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1004. connection_edge_destroy(circ->n_circ_id, conn);
  1005. }
  1006. circ->marked_for_close = line;
  1007. circ->marked_for_close_file = file;
  1008. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1009. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1010. if (or_circ->rend_splice) {
  1011. if (!or_circ->rend_splice->_base.marked_for_close) {
  1012. /* do this after marking this circuit, to avoid infinite recursion. */
  1013. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1014. }
  1015. or_circ->rend_splice = NULL;
  1016. }
  1017. }
  1018. }
  1019. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1020. * correct. Trigger an assert if anything is invalid.
  1021. */
  1022. void
  1023. assert_cpath_layer_ok(const crypt_path_t *cp)
  1024. {
  1025. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1026. // tor_assert(cp->port);
  1027. tor_assert(cp);
  1028. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1029. switch (cp->state)
  1030. {
  1031. case CPATH_STATE_OPEN:
  1032. tor_assert(cp->f_crypto);
  1033. tor_assert(cp->b_crypto);
  1034. /* fall through */
  1035. case CPATH_STATE_CLOSED:
  1036. tor_assert(!cp->dh_handshake_state);
  1037. break;
  1038. case CPATH_STATE_AWAITING_KEYS:
  1039. /* tor_assert(cp->dh_handshake_state); */
  1040. break;
  1041. default:
  1042. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1043. tor_assert(0);
  1044. }
  1045. tor_assert(cp->package_window >= 0);
  1046. tor_assert(cp->deliver_window >= 0);
  1047. }
  1048. /** Verify that cpath <b>cp</b> has all of its invariants
  1049. * correct. Trigger an assert if anything is invalid.
  1050. */
  1051. static void
  1052. assert_cpath_ok(const crypt_path_t *cp)
  1053. {
  1054. const crypt_path_t *start = cp;
  1055. do {
  1056. assert_cpath_layer_ok(cp);
  1057. /* layers must be in sequence of: "open* awaiting? closed*" */
  1058. if (cp != start) {
  1059. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1060. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1061. } else if (cp->state == CPATH_STATE_OPEN) {
  1062. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1063. }
  1064. }
  1065. cp = cp->next;
  1066. tor_assert(cp);
  1067. } while (cp != start);
  1068. }
  1069. /** Verify that circuit <b>c</b> has all of its invariants
  1070. * correct. Trigger an assert if anything is invalid.
  1071. */
  1072. void
  1073. assert_circuit_ok(const circuit_t *c)
  1074. {
  1075. edge_connection_t *conn;
  1076. const or_circuit_t *or_circ = NULL;
  1077. const origin_circuit_t *origin_circ = NULL;
  1078. tor_assert(c);
  1079. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1080. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1081. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1082. {
  1083. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1084. * never understand. -NM. */
  1085. circuit_t *nonconst_circ = (circuit_t*) c;
  1086. if (CIRCUIT_IS_ORIGIN(c))
  1087. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1088. else
  1089. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1090. }
  1091. if (c->n_conn) {
  1092. tor_assert(!c->n_hop);
  1093. if (c->n_circ_id) {
  1094. /* We use the _impl variant here to make sure we don't fail on marked
  1095. * circuits, which would not be returned by the regular function. */
  1096. circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
  1097. c->n_conn);
  1098. tor_assert(c == c2);
  1099. }
  1100. }
  1101. if (or_circ && or_circ->p_conn) {
  1102. if (or_circ->p_circ_id) {
  1103. /* ibid */
  1104. circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
  1105. or_circ->p_conn);
  1106. tor_assert(c == c2);
  1107. }
  1108. }
  1109. #if 0 /* false now that rendezvous exits are attached to p_streams */
  1110. if (origin_circ)
  1111. for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
  1112. tor_assert(conn->_base.type == CONN_TYPE_AP);
  1113. #endif
  1114. if (or_circ)
  1115. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1116. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1117. tor_assert(c->deliver_window >= 0);
  1118. tor_assert(c->package_window >= 0);
  1119. if (c->state == CIRCUIT_STATE_OPEN) {
  1120. tor_assert(!c->n_conn_onionskin);
  1121. if (or_circ) {
  1122. tor_assert(or_circ->n_crypto);
  1123. tor_assert(or_circ->p_crypto);
  1124. tor_assert(or_circ->n_digest);
  1125. tor_assert(or_circ->p_digest);
  1126. }
  1127. }
  1128. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1129. tor_assert(circuits_pending_or_conns &&
  1130. smartlist_isin(circuits_pending_or_conns, c));
  1131. } else {
  1132. tor_assert(!circuits_pending_or_conns ||
  1133. !smartlist_isin(circuits_pending_or_conns, c));
  1134. }
  1135. if (origin_circ && origin_circ->cpath) {
  1136. assert_cpath_ok(origin_circ->cpath);
  1137. }
  1138. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1139. tor_assert(or_circ);
  1140. if (!c->marked_for_close) {
  1141. tor_assert(or_circ->rend_splice);
  1142. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1143. }
  1144. tor_assert(or_circ->rend_splice != or_circ);
  1145. } else {
  1146. tor_assert(!or_circ || !or_circ->rend_splice);
  1147. }
  1148. }