circuitlist.c 48 KB

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