circuitlist.c 44 KB

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