circuitlist.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. tor_free(ocirc->dest_address);
  481. if (ocirc->socks_username) {
  482. memset(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  483. tor_free(ocirc->socks_username);
  484. }
  485. if (ocirc->socks_password) {
  486. memset(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  487. tor_free(ocirc->socks_password);
  488. }
  489. } else {
  490. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  491. /* Remember cell statistics for this circuit before deallocating. */
  492. if (get_options()->CellStatistics)
  493. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  494. mem = ocirc;
  495. memlen = sizeof(or_circuit_t);
  496. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  497. crypto_free_cipher_env(ocirc->p_crypto);
  498. crypto_free_digest_env(ocirc->p_digest);
  499. crypto_free_cipher_env(ocirc->n_crypto);
  500. crypto_free_digest_env(ocirc->n_digest);
  501. if (ocirc->rend_splice) {
  502. or_circuit_t *other = ocirc->rend_splice;
  503. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  504. other->rend_splice = NULL;
  505. }
  506. /* remove from map. */
  507. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  508. /* Clear cell queue _after_ removing it from the map. Otherwise our
  509. * "active" checks will be violated. */
  510. cell_queue_clear(&ocirc->p_conn_cells);
  511. }
  512. extend_info_free(circ->n_hop);
  513. tor_free(circ->n_conn_onionskin);
  514. /* Remove from map. */
  515. circuit_set_n_circid_orconn(circ, 0, NULL);
  516. /* Clear cell queue _after_ removing it from the map. Otherwise our
  517. * "active" checks will be violated. */
  518. cell_queue_clear(&circ->n_conn_cells);
  519. memset(mem, 0xAA, memlen); /* poison memory */
  520. tor_free(mem);
  521. }
  522. /** Deallocate space associated with the linked list <b>cpath</b>. */
  523. static void
  524. circuit_free_cpath(crypt_path_t *cpath)
  525. {
  526. crypt_path_t *victim, *head=cpath;
  527. if (!cpath)
  528. return;
  529. /* it's a doubly linked list, so we have to notice when we've
  530. * gone through it once. */
  531. while (cpath->next && cpath->next != head) {
  532. victim = cpath;
  533. cpath = victim->next;
  534. circuit_free_cpath_node(victim);
  535. }
  536. circuit_free_cpath_node(cpath);
  537. }
  538. /** Release all storage held by circuits. */
  539. void
  540. circuit_free_all(void)
  541. {
  542. circuit_t *next;
  543. while (global_circuitlist) {
  544. next = global_circuitlist->next;
  545. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  546. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  547. while (or_circ->resolving_streams) {
  548. edge_connection_t *next_conn;
  549. next_conn = or_circ->resolving_streams->next_stream;
  550. connection_free(TO_CONN(or_circ->resolving_streams));
  551. or_circ->resolving_streams = next_conn;
  552. }
  553. }
  554. circuit_free(global_circuitlist);
  555. global_circuitlist = next;
  556. }
  557. smartlist_free(circuits_pending_or_conns);
  558. circuits_pending_or_conns = NULL;
  559. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  560. }
  561. /** Deallocate space associated with the cpath node <b>victim</b>. */
  562. static void
  563. circuit_free_cpath_node(crypt_path_t *victim)
  564. {
  565. if (!victim)
  566. return;
  567. crypto_free_cipher_env(victim->f_crypto);
  568. crypto_free_cipher_env(victim->b_crypto);
  569. crypto_free_digest_env(victim->f_digest);
  570. crypto_free_digest_env(victim->b_digest);
  571. crypto_dh_free(victim->dh_handshake_state);
  572. extend_info_free(victim->extend_info);
  573. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  574. tor_free(victim);
  575. }
  576. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  577. * of information about circuit <b>circ</b>.
  578. */
  579. static void
  580. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  581. const char *type, int this_circid, int other_circid)
  582. {
  583. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  584. "state %d (%s), born %ld:",
  585. conn_array_index, type, this_circid, other_circid, circ->state,
  586. circuit_state_to_string(circ->state),
  587. (long)circ->timestamp_created.tv_sec);
  588. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  589. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  590. }
  591. }
  592. /** Log, at severity <b>severity</b>, information about each circuit
  593. * that is connected to <b>conn</b>.
  594. */
  595. void
  596. circuit_dump_by_conn(connection_t *conn, int severity)
  597. {
  598. circuit_t *circ;
  599. edge_connection_t *tmpconn;
  600. for (circ=global_circuitlist;circ;circ = circ->next) {
  601. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  602. if (circ->marked_for_close)
  603. continue;
  604. if (! CIRCUIT_IS_ORIGIN(circ))
  605. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  606. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  607. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  608. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  609. p_circ_id, n_circ_id);
  610. if (CIRCUIT_IS_ORIGIN(circ)) {
  611. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  612. tmpconn=tmpconn->next_stream) {
  613. if (TO_CONN(tmpconn) == conn) {
  614. circuit_dump_details(severity, circ, conn->conn_array_index,
  615. "App-ward", p_circ_id, n_circ_id);
  616. }
  617. }
  618. }
  619. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  620. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  621. n_circ_id, p_circ_id);
  622. if (! CIRCUIT_IS_ORIGIN(circ)) {
  623. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  624. tmpconn=tmpconn->next_stream) {
  625. if (TO_CONN(tmpconn) == conn) {
  626. circuit_dump_details(severity, circ, conn->conn_array_index,
  627. "Exit-ward", n_circ_id, p_circ_id);
  628. }
  629. }
  630. }
  631. if (!circ->n_conn && circ->n_hop &&
  632. tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
  633. circ->n_hop->port == conn->port &&
  634. conn->type == CONN_TYPE_OR &&
  635. tor_memeq(TO_OR_CONN(conn)->identity_digest,
  636. circ->n_hop->identity_digest, DIGEST_LEN)) {
  637. circuit_dump_details(severity, circ, conn->conn_array_index,
  638. (circ->state == CIRCUIT_STATE_OPEN &&
  639. !CIRCUIT_IS_ORIGIN(circ)) ?
  640. "Endpoint" : "Pending",
  641. n_circ_id, p_circ_id);
  642. }
  643. }
  644. }
  645. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  646. * such circuit exists. */
  647. origin_circuit_t *
  648. circuit_get_by_global_id(uint32_t id)
  649. {
  650. circuit_t *circ;
  651. for (circ=global_circuitlist;circ;circ = circ->next) {
  652. if (CIRCUIT_IS_ORIGIN(circ) &&
  653. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  654. if (circ->marked_for_close)
  655. return NULL;
  656. else
  657. return TO_ORIGIN_CIRCUIT(circ);
  658. }
  659. }
  660. return NULL;
  661. }
  662. /** Return a circ such that:
  663. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  664. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  665. * Return NULL if no such circuit exists.
  666. */
  667. static INLINE circuit_t *
  668. circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
  669. {
  670. orconn_circid_circuit_map_t search;
  671. orconn_circid_circuit_map_t *found;
  672. if (_last_circid_orconn_ent &&
  673. circ_id == _last_circid_orconn_ent->circ_id &&
  674. conn == _last_circid_orconn_ent->or_conn) {
  675. found = _last_circid_orconn_ent;
  676. } else {
  677. search.circ_id = circ_id;
  678. search.or_conn = conn;
  679. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  680. _last_circid_orconn_ent = found;
  681. }
  682. if (found && found->circuit)
  683. return found->circuit;
  684. return NULL;
  685. /* The rest of this checks for bugs. Disabled by default. */
  686. /* We comment it out because coverity complains otherwise.
  687. {
  688. circuit_t *circ;
  689. for (circ=global_circuitlist;circ;circ = circ->next) {
  690. if (! CIRCUIT_IS_ORIGIN(circ)) {
  691. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  692. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  693. log_warn(LD_BUG,
  694. "circuit matches p_conn, but not in hash table (Bug!)");
  695. return circ;
  696. }
  697. }
  698. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  699. log_warn(LD_BUG,
  700. "circuit matches n_conn, but not in hash table (Bug!)");
  701. return circ;
  702. }
  703. }
  704. return NULL;
  705. } */
  706. }
  707. /** Return a circ such that:
  708. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  709. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  710. * - circ is not marked for close.
  711. * Return NULL if no such circuit exists.
  712. */
  713. circuit_t *
  714. circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
  715. {
  716. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  717. if (!circ || circ->marked_for_close)
  718. return NULL;
  719. else
  720. return circ;
  721. }
  722. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  723. * circuit, marked or not, on <b>conn</b>. */
  724. int
  725. circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
  726. {
  727. return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
  728. }
  729. /** Return the circuit that a given edge connection is using. */
  730. circuit_t *
  731. circuit_get_by_edge_conn(edge_connection_t *conn)
  732. {
  733. circuit_t *circ;
  734. circ = conn->on_circuit;
  735. tor_assert(!circ ||
  736. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  737. : circ->magic == OR_CIRCUIT_MAGIC));
  738. return circ;
  739. }
  740. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  741. * circuit from the orconn,circid map, and mark it for close if it hasn't
  742. * been marked already.
  743. */
  744. void
  745. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  746. {
  747. circuit_t *circ;
  748. connection_or_unlink_all_active_circs(conn);
  749. for (circ = global_circuitlist; circ; circ = circ->next) {
  750. int mark = 0;
  751. if (circ->n_conn == conn) {
  752. circuit_set_n_circid_orconn(circ, 0, NULL);
  753. mark = 1;
  754. }
  755. if (! CIRCUIT_IS_ORIGIN(circ)) {
  756. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  757. if (or_circ->p_conn == conn) {
  758. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  759. mark = 1;
  760. }
  761. }
  762. if (mark && !circ->marked_for_close)
  763. circuit_mark_for_close(circ, reason);
  764. }
  765. }
  766. /** Return a circ such that
  767. * - circ-\>rend_data-\>onion_address is equal to
  768. * <b>rend_data</b>-\>onion_address,
  769. * - circ-\>rend_data-\>rend_cookie is equal to
  770. * <b>rend_data</b>-\>rend_cookie, and
  771. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  772. *
  773. * Return NULL if no such circuit exists.
  774. */
  775. origin_circuit_t *
  776. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  777. {
  778. circuit_t *circ;
  779. for (circ = global_circuitlist; circ; circ = circ->next) {
  780. if (!circ->marked_for_close &&
  781. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  782. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  783. if (ocirc->rend_data &&
  784. !rend_cmp_service_ids(rend_data->onion_address,
  785. ocirc->rend_data->onion_address) &&
  786. tor_memeq(ocirc->rend_data->rend_cookie,
  787. rend_data->rend_cookie,
  788. REND_COOKIE_LEN))
  789. return ocirc;
  790. }
  791. }
  792. return NULL;
  793. }
  794. /** Return the first circuit originating here in global_circuitlist after
  795. * <b>start</b> whose purpose is <b>purpose</b>, and where
  796. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  797. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  798. */
  799. origin_circuit_t *
  800. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  801. const char *digest, uint8_t purpose)
  802. {
  803. circuit_t *circ;
  804. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  805. if (start == NULL)
  806. circ = global_circuitlist;
  807. else
  808. circ = TO_CIRCUIT(start)->next;
  809. for ( ; circ; circ = circ->next) {
  810. if (circ->marked_for_close)
  811. continue;
  812. if (circ->purpose != purpose)
  813. continue;
  814. if (!digest)
  815. return TO_ORIGIN_CIRCUIT(circ);
  816. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  817. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  818. digest, DIGEST_LEN))
  819. return TO_ORIGIN_CIRCUIT(circ);
  820. }
  821. return NULL;
  822. }
  823. /** Return the first OR circuit in the global list whose purpose is
  824. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  825. * <b>token</b>. */
  826. static or_circuit_t *
  827. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  828. size_t len)
  829. {
  830. circuit_t *circ;
  831. for (circ = global_circuitlist; circ; circ = circ->next) {
  832. if (! circ->marked_for_close &&
  833. circ->purpose == purpose &&
  834. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  835. return TO_OR_CIRCUIT(circ);
  836. }
  837. return NULL;
  838. }
  839. /** Return the circuit waiting for a rendezvous with the provided cookie.
  840. * Return NULL if no such circuit is found.
  841. */
  842. or_circuit_t *
  843. circuit_get_rendezvous(const char *cookie)
  844. {
  845. return circuit_get_by_rend_token_and_purpose(
  846. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  847. cookie, REND_COOKIE_LEN);
  848. }
  849. /** Return the circuit waiting for intro cells of the given digest.
  850. * Return NULL if no such circuit is found.
  851. */
  852. or_circuit_t *
  853. circuit_get_intro_point(const char *digest)
  854. {
  855. return circuit_get_by_rend_token_and_purpose(
  856. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  857. DIGEST_LEN);
  858. }
  859. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  860. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  861. * flags in <b>flags</b>, and if info is defined, does not already use info
  862. * as any of its hops; or NULL if no circuit fits this description.
  863. *
  864. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  865. * the circuit we want to create, not the purpose of the circuit we want to
  866. * cannibalize.
  867. *
  868. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  869. */
  870. origin_circuit_t *
  871. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  872. int flags)
  873. {
  874. circuit_t *_circ;
  875. origin_circuit_t *best=NULL;
  876. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  877. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  878. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  879. const or_options_t *options = get_options();
  880. /* Make sure we're not trying to create a onehop circ by
  881. * cannibalization. */
  882. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  883. log_debug(LD_CIRC,
  884. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  885. "capacity %d, internal %d",
  886. purpose, need_uptime, need_capacity, internal);
  887. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  888. if (CIRCUIT_IS_ORIGIN(_circ) &&
  889. _circ->state == CIRCUIT_STATE_OPEN &&
  890. !_circ->marked_for_close &&
  891. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  892. !_circ->timestamp_dirty) {
  893. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  894. if ((!need_uptime || circ->build_state->need_uptime) &&
  895. (!need_capacity || circ->build_state->need_capacity) &&
  896. (internal == circ->build_state->is_internal) &&
  897. circ->remaining_relay_early_cells &&
  898. !circ->build_state->onehop_tunnel &&
  899. !circ->isolation_values_set) {
  900. if (info) {
  901. /* need to make sure we don't duplicate hops */
  902. crypt_path_t *hop = circ->cpath;
  903. const node_t *ri1 = node_get_by_id(info->identity_digest);
  904. do {
  905. const node_t *ri2;
  906. if (tor_memeq(hop->extend_info->identity_digest,
  907. info->identity_digest, DIGEST_LEN))
  908. goto next;
  909. if (ri1 &&
  910. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  911. && nodes_in_same_family(ri1, ri2))
  912. goto next;
  913. hop=hop->next;
  914. } while (hop!=circ->cpath);
  915. }
  916. if (options->ExcludeNodes) {
  917. /* Make sure no existing nodes in the circuit are excluded for
  918. * general use. (This may be possible if StrictNodes is 0, and we
  919. * thought we needed to use an otherwise excluded node for, say, a
  920. * directory operation.) */
  921. crypt_path_t *hop = circ->cpath;
  922. do {
  923. if (routerset_contains_extendinfo(options->ExcludeNodes,
  924. hop->extend_info))
  925. goto next;
  926. hop = hop->next;
  927. } while (hop != circ->cpath);
  928. }
  929. if (!best || (best->build_state->need_uptime && !need_uptime))
  930. best = circ;
  931. next: ;
  932. }
  933. }
  934. }
  935. return best;
  936. }
  937. /** Return the number of hops in circuit's path. */
  938. int
  939. circuit_get_cpath_len(origin_circuit_t *circ)
  940. {
  941. int n = 0;
  942. if (circ && circ->cpath) {
  943. crypt_path_t *cpath, *cpath_next = NULL;
  944. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  945. cpath_next = cpath->next;
  946. ++n;
  947. }
  948. }
  949. return n;
  950. }
  951. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  952. * aren't that many hops in the list. */
  953. crypt_path_t *
  954. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  955. {
  956. if (circ && circ->cpath && hopnum > 0) {
  957. crypt_path_t *cpath, *cpath_next = NULL;
  958. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  959. cpath_next = cpath->next;
  960. if (--hopnum <= 0)
  961. return cpath;
  962. }
  963. }
  964. return NULL;
  965. }
  966. /** Go through the circuitlist; mark-for-close each circuit that starts
  967. * at us but has not yet been used. */
  968. void
  969. circuit_mark_all_unused_circs(void)
  970. {
  971. circuit_t *circ;
  972. for (circ=global_circuitlist; circ; circ = circ->next) {
  973. if (CIRCUIT_IS_ORIGIN(circ) &&
  974. !circ->marked_for_close &&
  975. !circ->timestamp_dirty)
  976. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  977. }
  978. }
  979. /** Go through the circuitlist; for each circuit that starts at us
  980. * and is dirty, frob its timestamp_dirty so we won't use it for any
  981. * new streams.
  982. *
  983. * This is useful for letting the user change pseudonyms, so new
  984. * streams will not be linkable to old streams.
  985. */
  986. /* XXX023 this is a bad name for what this function does */
  987. void
  988. circuit_expire_all_dirty_circs(void)
  989. {
  990. circuit_t *circ;
  991. const or_options_t *options = get_options();
  992. for (circ=global_circuitlist; circ; circ = circ->next) {
  993. if (CIRCUIT_IS_ORIGIN(circ) &&
  994. !circ->marked_for_close &&
  995. circ->timestamp_dirty)
  996. /* XXXX023 This is a screwed-up way to say "This is too dirty
  997. * for new circuits. */
  998. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  999. }
  1000. }
  1001. /** Mark <b>circ</b> to be closed next time we call
  1002. * circuit_close_all_marked(). Do any cleanup needed:
  1003. * - If state is onionskin_pending, remove circ from the onion_pending
  1004. * list.
  1005. * - If circ isn't open yet: call circuit_build_failed() if we're
  1006. * the origin, and in either case call circuit_rep_hist_note_result()
  1007. * to note stats.
  1008. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1009. * failure we just had to the hidden service client module.
  1010. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1011. * report to the hidden service client module that the intro point
  1012. * we just tried may be unreachable.
  1013. * - Send appropriate destroys and edge_destroys for conns and
  1014. * streams attached to circ.
  1015. * - If circ->rend_splice is set (we are the midpoint of a joined
  1016. * rendezvous stream), then mark the other circuit to close as well.
  1017. */
  1018. void
  1019. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  1020. const char *file)
  1021. {
  1022. int orig_reason = reason; /* Passed to the controller */
  1023. assert_circuit_ok(circ);
  1024. tor_assert(line);
  1025. tor_assert(file);
  1026. if (circ->marked_for_close) {
  1027. log(LOG_WARN,LD_BUG,
  1028. "Duplicate call to circuit_mark_for_close at %s:%d"
  1029. " (first at %s:%d)", file, line,
  1030. circ->marked_for_close_file, circ->marked_for_close);
  1031. return;
  1032. }
  1033. if (reason == END_CIRC_AT_ORIGIN) {
  1034. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1035. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1036. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1037. file, line, circ->purpose);
  1038. }
  1039. reason = END_CIRC_REASON_NONE;
  1040. }
  1041. if (CIRCUIT_IS_ORIGIN(circ)) {
  1042. /* We don't send reasons when closing circuits at the origin. */
  1043. reason = END_CIRC_REASON_NONE;
  1044. }
  1045. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1046. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1047. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  1048. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1049. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1050. reason = END_CIRC_REASON_NONE;
  1051. }
  1052. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1053. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1054. }
  1055. /* If the circuit ever became OPEN, we sent it to the reputation history
  1056. * module then. If it isn't OPEN, we send it there now to remember which
  1057. * links worked and which didn't.
  1058. */
  1059. if (circ->state != CIRCUIT_STATE_OPEN) {
  1060. if (CIRCUIT_IS_ORIGIN(circ)) {
  1061. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1062. circuit_build_failed(ocirc); /* take actions if necessary */
  1063. circuit_rep_hist_note_result(ocirc);
  1064. }
  1065. }
  1066. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  1067. if (circuits_pending_or_conns)
  1068. smartlist_remove(circuits_pending_or_conns, circ);
  1069. }
  1070. if (CIRCUIT_IS_ORIGIN(circ)) {
  1071. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1072. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1073. orig_reason);
  1074. }
  1075. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1076. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1077. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1078. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1079. tor_assert(ocirc->build_state->chosen_exit);
  1080. tor_assert(ocirc->rend_data);
  1081. /* treat this like getting a nack from it */
  1082. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1083. safe_str_client(ocirc->rend_data->onion_address),
  1084. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1085. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1086. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1087. ocirc->rend_data,
  1088. timed_out ?
  1089. INTRO_POINT_FAILURE_TIMEOUT :
  1090. INTRO_POINT_FAILURE_GENERIC);
  1091. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1092. reason != END_CIRC_REASON_TIMEOUT) {
  1093. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1094. tor_assert(ocirc->build_state->chosen_exit);
  1095. tor_assert(ocirc->rend_data);
  1096. log_info(LD_REND, "Failed intro circ %s to %s "
  1097. "(building circuit to intro point). "
  1098. "Marking intro point as possibly unreachable.",
  1099. safe_str_client(ocirc->rend_data->onion_address),
  1100. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1101. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1102. ocirc->rend_data,
  1103. INTRO_POINT_FAILURE_UNREACHABLE);
  1104. }
  1105. if (circ->n_conn) {
  1106. circuit_clear_cell_queue(circ, circ->n_conn);
  1107. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  1108. }
  1109. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1110. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1111. edge_connection_t *conn;
  1112. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1113. connection_edge_destroy(or_circ->p_circ_id, conn);
  1114. or_circ->n_streams = NULL;
  1115. while (or_circ->resolving_streams) {
  1116. conn = or_circ->resolving_streams;
  1117. or_circ->resolving_streams = conn->next_stream;
  1118. if (!conn->_base.marked_for_close) {
  1119. /* The client will see a DESTROY, and infer that the connections
  1120. * are closing because the circuit is getting torn down. No need
  1121. * to send an end cell. */
  1122. conn->edge_has_sent_end = 1;
  1123. conn->end_reason = END_STREAM_REASON_DESTROY;
  1124. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1125. connection_mark_for_close(TO_CONN(conn));
  1126. }
  1127. conn->on_circuit = NULL;
  1128. }
  1129. if (or_circ->p_conn) {
  1130. circuit_clear_cell_queue(circ, or_circ->p_conn);
  1131. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  1132. }
  1133. } else {
  1134. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1135. edge_connection_t *conn;
  1136. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1137. connection_edge_destroy(circ->n_circ_id, conn);
  1138. ocirc->p_streams = NULL;
  1139. }
  1140. circ->marked_for_close = line;
  1141. circ->marked_for_close_file = file;
  1142. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1143. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1144. if (or_circ->rend_splice) {
  1145. if (!or_circ->rend_splice->_base.marked_for_close) {
  1146. /* do this after marking this circuit, to avoid infinite recursion. */
  1147. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1148. }
  1149. or_circ->rend_splice = NULL;
  1150. }
  1151. }
  1152. }
  1153. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1154. * correct. Trigger an assert if anything is invalid.
  1155. */
  1156. void
  1157. assert_cpath_layer_ok(const crypt_path_t *cp)
  1158. {
  1159. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1160. // tor_assert(cp->port);
  1161. tor_assert(cp);
  1162. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1163. switch (cp->state)
  1164. {
  1165. case CPATH_STATE_OPEN:
  1166. tor_assert(cp->f_crypto);
  1167. tor_assert(cp->b_crypto);
  1168. /* fall through */
  1169. case CPATH_STATE_CLOSED:
  1170. tor_assert(!cp->dh_handshake_state);
  1171. break;
  1172. case CPATH_STATE_AWAITING_KEYS:
  1173. /* tor_assert(cp->dh_handshake_state); */
  1174. break;
  1175. default:
  1176. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1177. tor_assert(0);
  1178. }
  1179. tor_assert(cp->package_window >= 0);
  1180. tor_assert(cp->deliver_window >= 0);
  1181. }
  1182. /** Verify that cpath <b>cp</b> has all of its invariants
  1183. * correct. Trigger an assert if anything is invalid.
  1184. */
  1185. static void
  1186. assert_cpath_ok(const crypt_path_t *cp)
  1187. {
  1188. const crypt_path_t *start = cp;
  1189. do {
  1190. assert_cpath_layer_ok(cp);
  1191. /* layers must be in sequence of: "open* awaiting? closed*" */
  1192. if (cp != start) {
  1193. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1194. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1195. } else if (cp->state == CPATH_STATE_OPEN) {
  1196. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1197. }
  1198. }
  1199. cp = cp->next;
  1200. tor_assert(cp);
  1201. } while (cp != start);
  1202. }
  1203. /** Verify that circuit <b>c</b> has all of its invariants
  1204. * correct. Trigger an assert if anything is invalid.
  1205. */
  1206. void
  1207. assert_circuit_ok(const circuit_t *c)
  1208. {
  1209. edge_connection_t *conn;
  1210. const or_circuit_t *or_circ = NULL;
  1211. const origin_circuit_t *origin_circ = NULL;
  1212. tor_assert(c);
  1213. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1214. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1215. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1216. {
  1217. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1218. * never understand. -NM. */
  1219. circuit_t *nonconst_circ = (circuit_t*) c;
  1220. if (CIRCUIT_IS_ORIGIN(c))
  1221. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1222. else
  1223. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1224. }
  1225. if (c->n_conn) {
  1226. tor_assert(!c->n_hop);
  1227. if (c->n_circ_id) {
  1228. /* We use the _impl variant here to make sure we don't fail on marked
  1229. * circuits, which would not be returned by the regular function. */
  1230. circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
  1231. c->n_conn);
  1232. tor_assert(c == c2);
  1233. }
  1234. }
  1235. if (or_circ && or_circ->p_conn) {
  1236. if (or_circ->p_circ_id) {
  1237. /* ibid */
  1238. circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
  1239. or_circ->p_conn);
  1240. tor_assert(c == c2);
  1241. }
  1242. }
  1243. if (or_circ)
  1244. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1245. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1246. tor_assert(c->deliver_window >= 0);
  1247. tor_assert(c->package_window >= 0);
  1248. if (c->state == CIRCUIT_STATE_OPEN) {
  1249. tor_assert(!c->n_conn_onionskin);
  1250. if (or_circ) {
  1251. tor_assert(or_circ->n_crypto);
  1252. tor_assert(or_circ->p_crypto);
  1253. tor_assert(or_circ->n_digest);
  1254. tor_assert(or_circ->p_digest);
  1255. }
  1256. }
  1257. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1258. tor_assert(circuits_pending_or_conns &&
  1259. smartlist_isin(circuits_pending_or_conns, c));
  1260. } else {
  1261. tor_assert(!circuits_pending_or_conns ||
  1262. !smartlist_isin(circuits_pending_or_conns, c));
  1263. }
  1264. if (origin_circ && origin_circ->cpath) {
  1265. assert_cpath_ok(origin_circ->cpath);
  1266. }
  1267. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1268. tor_assert(or_circ);
  1269. if (!c->marked_for_close) {
  1270. tor_assert(or_circ->rend_splice);
  1271. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1272. }
  1273. tor_assert(or_circ->rend_splice != or_circ);
  1274. } else {
  1275. tor_assert(!or_circ || !or_circ->rend_splice);
  1276. }
  1277. }