circuitlist.c 44 KB

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