circuitlist.c 44 KB

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