circuitlist.c 47 KB

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