circuitlist.c 40 KB

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