circuitlist.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circuitlist.c
  8. * \brief Manage the global circuit list.
  9. **/
  10. #define CIRCUITLIST_PRIVATE
  11. #include "or.h"
  12. #include "channel.h"
  13. #include "circpathbias.h"
  14. #include "circuitbuild.h"
  15. #include "circuitlist.h"
  16. #include "circuituse.h"
  17. #include "circuitstats.h"
  18. #include "connection.h"
  19. #include "config.h"
  20. #include "connection_edge.h"
  21. #include "connection_or.h"
  22. #include "control.h"
  23. #include "networkstatus.h"
  24. #include "nodelist.h"
  25. #include "onion.h"
  26. #include "onion_fast.h"
  27. #include "policies.h"
  28. #include "relay.h"
  29. #include "rendclient.h"
  30. #include "rendcommon.h"
  31. #include "rephist.h"
  32. #include "routerlist.h"
  33. #include "routerset.h"
  34. #include "ht.h"
  35. /********* START VARIABLES **********/
  36. /** A global list of all circuits at this hop. */
  37. struct global_circuitlist_s global_circuitlist =
  38. TOR_LIST_HEAD_INITIALIZER(global_circuitlist);
  39. /** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
  40. static smartlist_t *circuits_pending_chans = NULL;
  41. static void circuit_free_cpath_node(crypt_path_t *victim);
  42. static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
  43. /********* END VARIABLES ************/
  44. /** A map from channel and circuit ID to circuit. (Lookup performance is
  45. * very important here, since we need to do it every time a cell arrives.) */
  46. typedef struct chan_circid_circuit_map_t {
  47. HT_ENTRY(chan_circid_circuit_map_t) node;
  48. channel_t *chan;
  49. circid_t circ_id;
  50. circuit_t *circuit;
  51. } chan_circid_circuit_map_t;
  52. /** Helper for hash tables: compare the channel and circuit ID for a and
  53. * b, and return less than, equal to, or greater than zero appropriately.
  54. */
  55. static INLINE int
  56. chan_circid_entries_eq_(chan_circid_circuit_map_t *a,
  57. chan_circid_circuit_map_t *b)
  58. {
  59. return a->chan == b->chan && a->circ_id == b->circ_id;
  60. }
  61. /** Helper: return a hash based on circuit ID and the pointer value of
  62. * chan in <b>a</b>. */
  63. static INLINE unsigned int
  64. chan_circid_entry_hash_(chan_circid_circuit_map_t *a)
  65. {
  66. return ((unsigned)a->circ_id) ^ (unsigned)(uintptr_t)(a->chan);
  67. }
  68. /** Map from [chan,circid] to circuit. */
  69. static HT_HEAD(chan_circid_map, chan_circid_circuit_map_t)
  70. chan_circid_map = HT_INITIALIZER();
  71. HT_PROTOTYPE(chan_circid_map, chan_circid_circuit_map_t, node,
  72. chan_circid_entry_hash_, chan_circid_entries_eq_)
  73. HT_GENERATE(chan_circid_map, chan_circid_circuit_map_t, node,
  74. chan_circid_entry_hash_, chan_circid_entries_eq_, 0.6,
  75. malloc, realloc, free)
  76. /** The most recently returned entry from circuit_get_by_circid_chan;
  77. * used to improve performance when many cells arrive in a row from the
  78. * same circuit.
  79. */
  80. chan_circid_circuit_map_t *_last_circid_chan_ent = NULL;
  81. /** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
  82. * and/or channel for circ has just changed from <b>old_chan, old_id</b>
  83. * to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
  84. * the old entry (if any) and adding a new one. */
  85. static void
  86. circuit_set_circid_chan_helper(circuit_t *circ, int direction,
  87. circid_t id,
  88. channel_t *chan)
  89. {
  90. chan_circid_circuit_map_t search;
  91. chan_circid_circuit_map_t *found;
  92. channel_t *old_chan, **chan_ptr;
  93. circid_t old_id, *circid_ptr;
  94. int make_active, attached = 0;
  95. if (direction == CELL_DIRECTION_OUT) {
  96. chan_ptr = &circ->n_chan;
  97. circid_ptr = &circ->n_circ_id;
  98. make_active = circ->n_chan_cells.n > 0;
  99. } else {
  100. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  101. chan_ptr = &c->p_chan;
  102. circid_ptr = &c->p_circ_id;
  103. make_active = c->p_chan_cells.n > 0;
  104. }
  105. old_chan = *chan_ptr;
  106. old_id = *circid_ptr;
  107. if (id == old_id && chan == old_chan)
  108. return;
  109. if (_last_circid_chan_ent &&
  110. ((old_id == _last_circid_chan_ent->circ_id &&
  111. old_chan == _last_circid_chan_ent->chan) ||
  112. (id == _last_circid_chan_ent->circ_id &&
  113. chan == _last_circid_chan_ent->chan))) {
  114. _last_circid_chan_ent = NULL;
  115. }
  116. if (old_chan) {
  117. /*
  118. * If we're changing channels or ID and had an old channel and a non
  119. * zero old ID and weren't marked for close (i.e., we should have been
  120. * attached), detach the circuit. ID changes require this because
  121. * circuitmux hashes on (channel_id, circuit_id).
  122. */
  123. if (old_id != 0 && (old_chan != chan || old_id != id) &&
  124. !(circ->marked_for_close)) {
  125. tor_assert(old_chan->cmux);
  126. circuitmux_detach_circuit(old_chan->cmux, circ);
  127. }
  128. /* we may need to remove it from the conn-circid map */
  129. search.circ_id = old_id;
  130. search.chan = old_chan;
  131. found = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
  132. if (found) {
  133. tor_free(found);
  134. if (direction == CELL_DIRECTION_OUT) {
  135. /* One fewer circuits use old_chan as n_chan */
  136. --(old_chan->num_n_circuits);
  137. } else {
  138. /* One fewer circuits use old_chan as p_chan */
  139. --(old_chan->num_p_circuits);
  140. }
  141. }
  142. }
  143. /* Change the values only after we have possibly made the circuit inactive
  144. * on the previous chan. */
  145. *chan_ptr = chan;
  146. *circid_ptr = id;
  147. if (chan == NULL)
  148. return;
  149. /* now add the new one to the conn-circid map */
  150. search.circ_id = id;
  151. search.chan = chan;
  152. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  153. if (found) {
  154. found->circuit = circ;
  155. } else {
  156. found = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
  157. found->circ_id = id;
  158. found->chan = chan;
  159. found->circuit = circ;
  160. HT_INSERT(chan_circid_map, &chan_circid_map, found);
  161. }
  162. /*
  163. * Attach to the circuitmux if we're changing channels or IDs and
  164. * have a new channel and ID to use and the circuit is not marked for
  165. * close.
  166. */
  167. if (chan && id != 0 && (old_chan != chan || old_id != id) &&
  168. !(circ->marked_for_close)) {
  169. tor_assert(chan->cmux);
  170. circuitmux_attach_circuit(chan->cmux, circ, direction);
  171. attached = 1;
  172. }
  173. /*
  174. * This is a no-op if we have no cells, but if we do it marks us active to
  175. * the circuitmux
  176. */
  177. if (make_active && attached)
  178. update_circuit_on_cmux(circ, direction);
  179. /* Adjust circuit counts on new channel */
  180. if (direction == CELL_DIRECTION_OUT) {
  181. ++chan->num_n_circuits;
  182. } else {
  183. ++chan->num_p_circuits;
  184. }
  185. }
  186. /** Mark that circuit id <b>id</b> shouldn't be used on channel <b>chan</b>,
  187. * even if there is no circuit on the channel. We use this to keep the
  188. * circuit id from getting re-used while we have queued but not yet sent
  189. * a destroy cell. */
  190. void
  191. channel_mark_circid_unusable(channel_t *chan, circid_t id)
  192. {
  193. chan_circid_circuit_map_t search;
  194. chan_circid_circuit_map_t *ent;
  195. /* See if there's an entry there. That wouldn't be good. */
  196. memset(&search, 0, sizeof(search));
  197. search.chan = chan;
  198. search.circ_id = id;
  199. ent = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  200. if (ent && ent->circuit) {
  201. /* we have a problem. */
  202. log_warn(LD_BUG, "Tried to mark %u unusable on %p, but there was already "
  203. "a circuit there.", (unsigned)id, chan);
  204. } else if (ent) {
  205. /* It's already marked. */
  206. } else {
  207. ent = tor_malloc_zero(sizeof(chan_circid_circuit_map_t));
  208. ent->chan = chan;
  209. ent->circ_id = id;
  210. /* leave circuit at NULL */
  211. HT_INSERT(chan_circid_map, &chan_circid_map, ent);
  212. }
  213. }
  214. /** Mark that a circuit id <b>id</b> can be used again on <b>chan</b>.
  215. * We use this to re-enable the circuit ID after we've sent a destroy cell.
  216. */
  217. void
  218. channel_mark_circid_usable(channel_t *chan, circid_t id)
  219. {
  220. chan_circid_circuit_map_t search;
  221. chan_circid_circuit_map_t *ent;
  222. /* See if there's an entry there. That wouldn't be good. */
  223. memset(&search, 0, sizeof(search));
  224. search.chan = chan;
  225. search.circ_id = id;
  226. ent = HT_REMOVE(chan_circid_map, &chan_circid_map, &search);
  227. if (ent && ent->circuit) {
  228. log_warn(LD_BUG, "Tried to mark %u usable on %p, but there was already "
  229. "a circuit there.", (unsigned)id, chan);
  230. return;
  231. }
  232. if (_last_circid_chan_ent == ent)
  233. _last_circid_chan_ent = NULL;
  234. tor_free(ent);
  235. }
  236. /** Called to indicate that a DESTROY is pending on <b>chan</b> with
  237. * circuit ID <b>id</b>, but hasn't been sent yet. */
  238. void
  239. channel_note_destroy_pending(channel_t *chan, circid_t id)
  240. {
  241. circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
  242. if (circ) {
  243. if (circ->n_chan == chan && circ->n_circ_id == id) {
  244. circ->n_delete_pending = 1;
  245. } else {
  246. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  247. if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
  248. circ->p_delete_pending = 1;
  249. }
  250. }
  251. return;
  252. }
  253. channel_mark_circid_unusable(chan, id);
  254. }
  255. /** Called to indicate that a DESTROY is no longer pending on <b>chan</b> with
  256. * circuit ID <b>id</b> -- typically, because it has been sent. */
  257. void
  258. channel_note_destroy_not_pending(channel_t *chan, circid_t id)
  259. {
  260. circuit_t *circ = circuit_get_by_circid_channel_even_if_marked(id,chan);
  261. if (circ) {
  262. if (circ->n_chan == chan && circ->n_circ_id == id) {
  263. circ->n_delete_pending = 0;
  264. } else {
  265. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  266. if (orcirc->p_chan == chan && orcirc->p_circ_id == id) {
  267. circ->p_delete_pending = 0;
  268. }
  269. }
  270. /* XXXX this shouldn't happen; log a bug here. */
  271. return;
  272. }
  273. channel_mark_circid_usable(chan, id);
  274. }
  275. /** Set the p_conn field of a circuit <b>circ</b>, along
  276. * with the corresponding circuit ID, and add the circuit as appropriate
  277. * to the (chan,id)-\>circuit map. */
  278. void
  279. circuit_set_p_circid_chan(or_circuit_t *or_circ, circid_t id,
  280. channel_t *chan)
  281. {
  282. circuit_t *circ = TO_CIRCUIT(or_circ);
  283. channel_t *old_chan = or_circ->p_chan;
  284. circid_t old_id = or_circ->p_circ_id;
  285. circuit_set_circid_chan_helper(circ, CELL_DIRECTION_IN, id, chan);
  286. if (chan)
  287. tor_assert(bool_eq(or_circ->p_chan_cells.n,
  288. or_circ->next_active_on_p_chan));
  289. if (circ->p_delete_pending && old_chan) {
  290. channel_mark_circid_unusable(old_chan, old_id);
  291. circ->p_delete_pending = 0;
  292. }
  293. }
  294. /** Set the n_conn field of a circuit <b>circ</b>, along
  295. * with the corresponding circuit ID, and add the circuit as appropriate
  296. * to the (chan,id)-\>circuit map. */
  297. void
  298. circuit_set_n_circid_chan(circuit_t *circ, circid_t id,
  299. channel_t *chan)
  300. {
  301. channel_t *old_chan = circ->n_chan;
  302. circid_t old_id = circ->n_circ_id;
  303. circuit_set_circid_chan_helper(circ, CELL_DIRECTION_OUT, id, chan);
  304. if (chan)
  305. tor_assert(bool_eq(circ->n_chan_cells.n, circ->next_active_on_n_chan));
  306. if (circ->n_delete_pending && old_chan) {
  307. channel_mark_circid_unusable(old_chan, old_id);
  308. circ->n_delete_pending = 0;
  309. }
  310. }
  311. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  312. * it from lists as appropriate. */
  313. void
  314. circuit_set_state(circuit_t *circ, uint8_t state)
  315. {
  316. tor_assert(circ);
  317. if (state == circ->state)
  318. return;
  319. if (!circuits_pending_chans)
  320. circuits_pending_chans = smartlist_new();
  321. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  322. /* remove from waiting-circuit list. */
  323. smartlist_remove(circuits_pending_chans, circ);
  324. }
  325. if (state == CIRCUIT_STATE_CHAN_WAIT) {
  326. /* add to waiting-circuit list. */
  327. smartlist_add(circuits_pending_chans, circ);
  328. }
  329. if (state == CIRCUIT_STATE_OPEN)
  330. tor_assert(!circ->n_chan_create_cell);
  331. circ->state = state;
  332. }
  333. /** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
  334. * the given connection. */
  335. void
  336. circuit_get_all_pending_on_channel(smartlist_t *out, channel_t *chan)
  337. {
  338. tor_assert(out);
  339. tor_assert(chan);
  340. if (!circuits_pending_chans)
  341. return;
  342. SMARTLIST_FOREACH_BEGIN(circuits_pending_chans, circuit_t *, circ) {
  343. if (circ->marked_for_close)
  344. continue;
  345. if (!circ->n_hop)
  346. continue;
  347. tor_assert(circ->state == CIRCUIT_STATE_CHAN_WAIT);
  348. if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
  349. /* Look at addr/port. This is an unkeyed connection. */
  350. if (!channel_matches_extend_info(chan, circ->n_hop))
  351. continue;
  352. } else {
  353. /* We expected a key. See if it's the right one. */
  354. if (tor_memneq(chan->identity_digest,
  355. circ->n_hop->identity_digest, DIGEST_LEN))
  356. continue;
  357. }
  358. smartlist_add(out, circ);
  359. } SMARTLIST_FOREACH_END(circ);
  360. }
  361. /** Return the number of circuits in state CHAN_WAIT, waiting for the given
  362. * channel. */
  363. int
  364. circuit_count_pending_on_channel(channel_t *chan)
  365. {
  366. int cnt;
  367. smartlist_t *sl = smartlist_new();
  368. tor_assert(chan);
  369. circuit_get_all_pending_on_channel(sl, chan);
  370. cnt = smartlist_len(sl);
  371. smartlist_free(sl);
  372. log_debug(LD_CIRC,"or_conn to %s at %s, %d pending circs",
  373. chan->nickname ? chan->nickname : "NULL",
  374. channel_get_canonical_remote_descr(chan),
  375. cnt);
  376. return cnt;
  377. }
  378. /** Detach from the global circuit list, and deallocate, all
  379. * circuits that have been marked for close.
  380. */
  381. void
  382. circuit_close_all_marked(void)
  383. {
  384. circuit_t *circ, *tmp;
  385. TOR_LIST_FOREACH_SAFE(circ, &global_circuitlist, head, tmp)
  386. if (circ->marked_for_close)
  387. circuit_free(circ);
  388. }
  389. /** Return the head of the global linked list of circuits. */
  390. struct global_circuitlist_s *
  391. circuit_get_global_list(void)
  392. {
  393. return &global_circuitlist;
  394. }
  395. /** Function to make circ-\>state human-readable */
  396. const char *
  397. circuit_state_to_string(int state)
  398. {
  399. static char buf[64];
  400. switch (state) {
  401. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  402. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  403. case CIRCUIT_STATE_CHAN_WAIT: return "connecting to server";
  404. case CIRCUIT_STATE_OPEN: return "open";
  405. default:
  406. log_warn(LD_BUG, "Unknown circuit state %d", state);
  407. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  408. return buf;
  409. }
  410. }
  411. /** Map a circuit purpose to a string suitable to be displayed to a
  412. * controller. */
  413. const char *
  414. circuit_purpose_to_controller_string(uint8_t purpose)
  415. {
  416. static char buf[32];
  417. switch (purpose) {
  418. case CIRCUIT_PURPOSE_OR:
  419. case CIRCUIT_PURPOSE_INTRO_POINT:
  420. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  421. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  422. return "SERVER"; /* A controller should never see these, actually. */
  423. case CIRCUIT_PURPOSE_C_GENERAL:
  424. return "GENERAL";
  425. case CIRCUIT_PURPOSE_C_INTRODUCING:
  426. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  427. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  428. return "HS_CLIENT_INTRO";
  429. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  430. case CIRCUIT_PURPOSE_C_REND_READY:
  431. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  432. case CIRCUIT_PURPOSE_C_REND_JOINED:
  433. return "HS_CLIENT_REND";
  434. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  435. case CIRCUIT_PURPOSE_S_INTRO:
  436. return "HS_SERVICE_INTRO";
  437. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  438. case CIRCUIT_PURPOSE_S_REND_JOINED:
  439. return "HS_SERVICE_REND";
  440. case CIRCUIT_PURPOSE_TESTING:
  441. return "TESTING";
  442. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  443. return "MEASURE_TIMEOUT";
  444. case CIRCUIT_PURPOSE_CONTROLLER:
  445. return "CONTROLLER";
  446. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  447. return "PATH_BIAS_TESTING";
  448. default:
  449. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  450. return buf;
  451. }
  452. }
  453. /** Return a string specifying the state of the hidden-service circuit
  454. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  455. * hidden-service-related circuit purpose. */
  456. const char *
  457. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  458. {
  459. switch (purpose)
  460. {
  461. default:
  462. log_fn(LOG_WARN, LD_BUG,
  463. "Unrecognized circuit purpose: %d",
  464. (int)purpose);
  465. tor_fragile_assert();
  466. /* fall through */
  467. case CIRCUIT_PURPOSE_OR:
  468. case CIRCUIT_PURPOSE_C_GENERAL:
  469. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  470. case CIRCUIT_PURPOSE_TESTING:
  471. case CIRCUIT_PURPOSE_CONTROLLER:
  472. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  473. return NULL;
  474. case CIRCUIT_PURPOSE_INTRO_POINT:
  475. return "OR_HSSI_ESTABLISHED";
  476. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  477. return "OR_HSCR_ESTABLISHED";
  478. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  479. return "OR_HS_R_JOINED";
  480. case CIRCUIT_PURPOSE_C_INTRODUCING:
  481. return "HSCI_CONNECTING";
  482. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  483. return "HSCI_INTRO_SENT";
  484. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  485. return "HSCI_DONE";
  486. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  487. return "HSCR_CONNECTING";
  488. case CIRCUIT_PURPOSE_C_REND_READY:
  489. return "HSCR_ESTABLISHED_IDLE";
  490. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  491. return "HSCR_ESTABLISHED_WAITING";
  492. case CIRCUIT_PURPOSE_C_REND_JOINED:
  493. return "HSCR_JOINED";
  494. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  495. return "HSSI_CONNECTING";
  496. case CIRCUIT_PURPOSE_S_INTRO:
  497. return "HSSI_ESTABLISHED";
  498. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  499. return "HSSR_CONNECTING";
  500. case CIRCUIT_PURPOSE_S_REND_JOINED:
  501. return "HSSR_JOINED";
  502. }
  503. }
  504. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  505. const char *
  506. circuit_purpose_to_string(uint8_t purpose)
  507. {
  508. static char buf[32];
  509. switch (purpose)
  510. {
  511. case CIRCUIT_PURPOSE_OR:
  512. return "Circuit at relay";
  513. case CIRCUIT_PURPOSE_INTRO_POINT:
  514. return "Acting as intro point";
  515. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  516. return "Acting as rendevous (pending)";
  517. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  518. return "Acting as rendevous (established)";
  519. case CIRCUIT_PURPOSE_C_GENERAL:
  520. return "General-purpose client";
  521. case CIRCUIT_PURPOSE_C_INTRODUCING:
  522. return "Hidden service client: Connecting to intro point";
  523. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  524. return "Hidden service client: Waiting for ack from intro point";
  525. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  526. return "Hidden service client: Received ack from intro point";
  527. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  528. return "Hidden service client: Establishing rendezvous point";
  529. case CIRCUIT_PURPOSE_C_REND_READY:
  530. return "Hidden service client: Pending rendezvous point";
  531. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  532. return "Hidden service client: Pending rendezvous point (ack received)";
  533. case CIRCUIT_PURPOSE_C_REND_JOINED:
  534. return "Hidden service client: Active rendezvous point";
  535. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  536. return "Measuring circuit timeout";
  537. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  538. return "Hidden service: Establishing introduction point";
  539. case CIRCUIT_PURPOSE_S_INTRO:
  540. return "Hidden service: Introduction point";
  541. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  542. return "Hidden service: Connecting to rendezvous point";
  543. case CIRCUIT_PURPOSE_S_REND_JOINED:
  544. return "Hidden service: Active rendezvous point";
  545. case CIRCUIT_PURPOSE_TESTING:
  546. return "Testing circuit";
  547. case CIRCUIT_PURPOSE_CONTROLLER:
  548. return "Circuit made by controller";
  549. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  550. return "Path-bias testing circuit";
  551. default:
  552. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  553. return buf;
  554. }
  555. }
  556. /** Pick a reasonable package_window to start out for our circuits.
  557. * Originally this was hard-coded at 1000, but now the consensus votes
  558. * on the answer. See proposal 168. */
  559. int32_t
  560. circuit_initial_package_window(void)
  561. {
  562. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  563. CIRCWINDOW_START_MIN,
  564. CIRCWINDOW_START_MAX);
  565. /* If the consensus tells us a negative number, we'd assert. */
  566. if (num < 0)
  567. num = CIRCWINDOW_START;
  568. return num;
  569. }
  570. /** Initialize the common elements in a circuit_t, and add it to the global
  571. * list. */
  572. static void
  573. init_circuit_base(circuit_t *circ)
  574. {
  575. tor_gettimeofday(&circ->timestamp_created);
  576. // Gets reset when we send CREATE_FAST.
  577. // circuit_expire_building() expects these to be equal
  578. // until the orconn is built.
  579. circ->timestamp_began = circ->timestamp_created;
  580. circ->package_window = circuit_initial_package_window();
  581. circ->deliver_window = CIRCWINDOW_START;
  582. cell_queue_init(&circ->n_chan_cells);
  583. TOR_LIST_INSERT_HEAD(&global_circuitlist, circ, head);
  584. }
  585. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  586. * and <b>p_conn</b>. Add it to the global circuit list.
  587. */
  588. origin_circuit_t *
  589. origin_circuit_new(void)
  590. {
  591. origin_circuit_t *circ;
  592. /* never zero, since a global ID of 0 is treated specially by the
  593. * controller */
  594. static uint32_t n_circuits_allocated = 1;
  595. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  596. circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  597. circ->next_stream_id = crypto_rand_int(1<<16);
  598. circ->global_identifier = n_circuits_allocated++;
  599. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  600. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  601. init_circuit_base(TO_CIRCUIT(circ));
  602. circuit_build_times_update_last_circ(get_circuit_build_times_mutable());
  603. return circ;
  604. }
  605. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  606. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  607. or_circuit_t *
  608. or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
  609. {
  610. /* CircIDs */
  611. or_circuit_t *circ;
  612. circ = tor_malloc_zero(sizeof(or_circuit_t));
  613. circ->base_.magic = OR_CIRCUIT_MAGIC;
  614. if (p_chan)
  615. circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
  616. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  617. cell_queue_init(&circ->p_chan_cells);
  618. init_circuit_base(TO_CIRCUIT(circ));
  619. return circ;
  620. }
  621. /** Deallocate space associated with circ.
  622. */
  623. STATIC void
  624. circuit_free(circuit_t *circ)
  625. {
  626. void *mem;
  627. size_t memlen;
  628. if (!circ)
  629. return;
  630. if (CIRCUIT_IS_ORIGIN(circ)) {
  631. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  632. mem = ocirc;
  633. memlen = sizeof(origin_circuit_t);
  634. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  635. if (ocirc->build_state) {
  636. extend_info_free(ocirc->build_state->chosen_exit);
  637. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  638. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  639. }
  640. tor_free(ocirc->build_state);
  641. circuit_clear_cpath(ocirc);
  642. crypto_pk_free(ocirc->intro_key);
  643. rend_data_free(ocirc->rend_data);
  644. tor_free(ocirc->dest_address);
  645. if (ocirc->socks_username) {
  646. memwipe(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  647. tor_free(ocirc->socks_username);
  648. }
  649. if (ocirc->socks_password) {
  650. memwipe(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  651. tor_free(ocirc->socks_password);
  652. }
  653. addr_policy_list_free(ocirc->prepend_policy);
  654. } else {
  655. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  656. /* Remember cell statistics for this circuit before deallocating. */
  657. if (get_options()->CellStatistics)
  658. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  659. mem = ocirc;
  660. memlen = sizeof(or_circuit_t);
  661. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  662. crypto_cipher_free(ocirc->p_crypto);
  663. crypto_digest_free(ocirc->p_digest);
  664. crypto_cipher_free(ocirc->n_crypto);
  665. crypto_digest_free(ocirc->n_digest);
  666. if (ocirc->rend_splice) {
  667. or_circuit_t *other = ocirc->rend_splice;
  668. tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
  669. other->rend_splice = NULL;
  670. }
  671. /* remove from map. */
  672. circuit_set_p_circid_chan(ocirc, 0, NULL);
  673. /* Clear cell queue _after_ removing it from the map. Otherwise our
  674. * "active" checks will be violated. */
  675. cell_queue_clear(&ocirc->p_chan_cells);
  676. }
  677. extend_info_free(circ->n_hop);
  678. tor_free(circ->n_chan_create_cell);
  679. TOR_LIST_REMOVE(circ, head);
  680. /* Remove from map. */
  681. circuit_set_n_circid_chan(circ, 0, NULL);
  682. /* Clear cell queue _after_ removing it from the map. Otherwise our
  683. * "active" checks will be violated. */
  684. cell_queue_clear(&circ->n_chan_cells);
  685. memwipe(mem, 0xAA, memlen); /* poison memory */
  686. tor_free(mem);
  687. }
  688. /** Deallocate the linked list circ-><b>cpath</b>, and remove the cpath from
  689. * <b>circ</b>. */
  690. void
  691. circuit_clear_cpath(origin_circuit_t *circ)
  692. {
  693. crypt_path_t *victim, *head, *cpath;
  694. head = cpath = circ->cpath;
  695. if (!cpath)
  696. return;
  697. /* it's a circular list, so we have to notice when we've
  698. * gone through it once. */
  699. while (cpath->next && cpath->next != head) {
  700. victim = cpath;
  701. cpath = victim->next;
  702. circuit_free_cpath_node(victim);
  703. }
  704. circuit_free_cpath_node(cpath);
  705. circ->cpath = NULL;
  706. }
  707. /** Release all storage held by circuits. */
  708. void
  709. circuit_free_all(void)
  710. {
  711. circuit_t *tmp, *tmp2;
  712. TOR_LIST_FOREACH_SAFE(tmp, &global_circuitlist, head, tmp2) {
  713. if (! CIRCUIT_IS_ORIGIN(tmp)) {
  714. or_circuit_t *or_circ = TO_OR_CIRCUIT(tmp);
  715. while (or_circ->resolving_streams) {
  716. edge_connection_t *next_conn;
  717. next_conn = or_circ->resolving_streams->next_stream;
  718. connection_free(TO_CONN(or_circ->resolving_streams));
  719. or_circ->resolving_streams = next_conn;
  720. }
  721. }
  722. circuit_free(tmp);
  723. }
  724. smartlist_free(circuits_pending_chans);
  725. circuits_pending_chans = NULL;
  726. HT_CLEAR(chan_circid_map, &chan_circid_map);
  727. }
  728. /** Deallocate space associated with the cpath node <b>victim</b>. */
  729. static void
  730. circuit_free_cpath_node(crypt_path_t *victim)
  731. {
  732. if (!victim)
  733. return;
  734. crypto_cipher_free(victim->f_crypto);
  735. crypto_cipher_free(victim->b_crypto);
  736. crypto_digest_free(victim->f_digest);
  737. crypto_digest_free(victim->b_digest);
  738. onion_handshake_state_release(&victim->handshake_state);
  739. crypto_dh_free(victim->rend_dh_handshake_state);
  740. extend_info_free(victim->extend_info);
  741. memwipe(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  742. tor_free(victim);
  743. }
  744. /** Release a crypt_path_reference_t*, which may be NULL. */
  745. static void
  746. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  747. {
  748. if (cpath_ref != NULL) {
  749. if (--(cpath_ref->refcount) == 0) {
  750. circuit_free_cpath_node(cpath_ref->cpath);
  751. tor_free(cpath_ref);
  752. }
  753. }
  754. }
  755. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  756. * of information about circuit <b>circ</b>.
  757. */
  758. static void
  759. circuit_dump_conn_details(int severity,
  760. circuit_t *circ,
  761. int conn_array_index,
  762. const char *type,
  763. circid_t this_circid,
  764. circid_t other_circid)
  765. {
  766. tor_log(severity, LD_CIRC, "Conn %d has %s circuit: circID %u "
  767. "(other side %u), state %d (%s), born %ld:",
  768. conn_array_index, type, (unsigned)this_circid, (unsigned)other_circid,
  769. circ->state, circuit_state_to_string(circ->state),
  770. (long)circ->timestamp_began.tv_sec);
  771. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  772. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  773. }
  774. }
  775. /** Log, at severity <b>severity</b>, information about each circuit
  776. * that is connected to <b>conn</b>.
  777. */
  778. void
  779. circuit_dump_by_conn(connection_t *conn, int severity)
  780. {
  781. circuit_t *circ;
  782. edge_connection_t *tmpconn;
  783. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  784. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  785. if (circ->marked_for_close) {
  786. continue;
  787. }
  788. if (!CIRCUIT_IS_ORIGIN(circ)) {
  789. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  790. }
  791. if (CIRCUIT_IS_ORIGIN(circ)) {
  792. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  793. tmpconn=tmpconn->next_stream) {
  794. if (TO_CONN(tmpconn) == conn) {
  795. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  796. "App-ward", p_circ_id, n_circ_id);
  797. }
  798. }
  799. }
  800. if (! CIRCUIT_IS_ORIGIN(circ)) {
  801. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  802. tmpconn=tmpconn->next_stream) {
  803. if (TO_CONN(tmpconn) == conn) {
  804. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  805. "Exit-ward", n_circ_id, p_circ_id);
  806. }
  807. }
  808. }
  809. }
  810. }
  811. /** A helper function for circuit_dump_by_chan() below. Log a bunch
  812. * of information about circuit <b>circ</b>.
  813. */
  814. static void
  815. circuit_dump_chan_details(int severity,
  816. circuit_t *circ,
  817. channel_t *chan,
  818. const char *type,
  819. circid_t this_circid,
  820. circid_t other_circid)
  821. {
  822. tor_log(severity, LD_CIRC, "Conn %p has %s circuit: circID %u "
  823. "(other side %u), state %d (%s), born %ld:",
  824. chan, type, (unsigned)this_circid, (unsigned)other_circid, circ->state,
  825. circuit_state_to_string(circ->state),
  826. (long)circ->timestamp_began.tv_sec);
  827. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  828. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  829. }
  830. }
  831. /** Log, at severity <b>severity</b>, information about each circuit
  832. * that is connected to <b>chan</b>.
  833. */
  834. void
  835. circuit_dump_by_chan(channel_t *chan, int severity)
  836. {
  837. circuit_t *circ;
  838. tor_assert(chan);
  839. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  840. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  841. if (circ->marked_for_close) {
  842. continue;
  843. }
  844. if (!CIRCUIT_IS_ORIGIN(circ)) {
  845. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  846. }
  847. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_chan &&
  848. TO_OR_CIRCUIT(circ)->p_chan == chan) {
  849. circuit_dump_chan_details(severity, circ, chan, "App-ward",
  850. p_circ_id, n_circ_id);
  851. }
  852. if (circ->n_chan && circ->n_chan == chan) {
  853. circuit_dump_chan_details(severity, circ, chan, "Exit-ward",
  854. n_circ_id, p_circ_id);
  855. }
  856. if (!circ->n_chan && circ->n_hop &&
  857. channel_matches_extend_info(chan, circ->n_hop) &&
  858. tor_memeq(chan->identity_digest,
  859. circ->n_hop->identity_digest, DIGEST_LEN)) {
  860. circuit_dump_chan_details(severity, circ, chan,
  861. (circ->state == CIRCUIT_STATE_OPEN &&
  862. !CIRCUIT_IS_ORIGIN(circ)) ?
  863. "Endpoint" : "Pending",
  864. n_circ_id, p_circ_id);
  865. }
  866. }
  867. }
  868. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  869. * such circuit exists. */
  870. origin_circuit_t *
  871. circuit_get_by_global_id(uint32_t id)
  872. {
  873. circuit_t *circ;
  874. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  875. if (CIRCUIT_IS_ORIGIN(circ) &&
  876. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  877. if (circ->marked_for_close)
  878. return NULL;
  879. else
  880. return TO_ORIGIN_CIRCUIT(circ);
  881. }
  882. }
  883. return NULL;
  884. }
  885. /** Return a circ such that:
  886. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  887. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  888. * Return NULL if no such circuit exists.
  889. *
  890. * If <b>found_entry_out</b> is provided, set it to true if we have a
  891. * placeholder entry for circid/chan, and leave it unset otherwise.
  892. */
  893. static INLINE circuit_t *
  894. circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan,
  895. int *found_entry_out)
  896. {
  897. chan_circid_circuit_map_t search;
  898. chan_circid_circuit_map_t *found;
  899. if (_last_circid_chan_ent &&
  900. circ_id == _last_circid_chan_ent->circ_id &&
  901. chan == _last_circid_chan_ent->chan) {
  902. found = _last_circid_chan_ent;
  903. } else {
  904. search.circ_id = circ_id;
  905. search.chan = chan;
  906. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  907. _last_circid_chan_ent = found;
  908. }
  909. if (found && found->circuit) {
  910. log_debug(LD_CIRC,
  911. "circuit_get_by_circid_channel_impl() returning circuit %p for"
  912. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  913. found->circuit, (unsigned)circ_id,
  914. U64_PRINTF_ARG(chan->global_identifier), chan);
  915. if (found_entry_out)
  916. *found_entry_out = 1;
  917. return found->circuit;
  918. }
  919. log_debug(LD_CIRC,
  920. "circuit_get_by_circid_channel_impl() found %s for"
  921. " circ_id %u, channel ID " U64_FORMAT " (%p)",
  922. found ? "placeholder" : "nothing",
  923. (unsigned)circ_id,
  924. U64_PRINTF_ARG(chan->global_identifier), chan);
  925. if (found_entry_out)
  926. *found_entry_out = found ? 1 : 0;
  927. return NULL;
  928. /* The rest of this checks for bugs. Disabled by default. */
  929. /* We comment it out because coverity complains otherwise.
  930. {
  931. circuit_t *circ;
  932. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  933. if (! CIRCUIT_IS_ORIGIN(circ)) {
  934. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  935. if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
  936. log_warn(LD_BUG,
  937. "circuit matches p_chan, but not in hash table (Bug!)");
  938. return circ;
  939. }
  940. }
  941. if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
  942. log_warn(LD_BUG,
  943. "circuit matches n_chan, but not in hash table (Bug!)");
  944. return circ;
  945. }
  946. }
  947. return NULL;
  948. } */
  949. }
  950. /** Return a circ such that:
  951. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  952. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  953. * - circ is not marked for close.
  954. * Return NULL if no such circuit exists.
  955. */
  956. circuit_t *
  957. circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
  958. {
  959. circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  960. if (!circ || circ->marked_for_close)
  961. return NULL;
  962. else
  963. return circ;
  964. }
  965. /** Return a circ such that:
  966. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  967. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  968. * Return NULL if no such circuit exists.
  969. */
  970. circuit_t *
  971. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  972. channel_t *chan)
  973. {
  974. return circuit_get_by_circid_channel_impl(circ_id, chan, NULL);
  975. }
  976. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  977. * circuit, marked or not, on <b>chan</b>. */
  978. int
  979. circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
  980. {
  981. int found = 0;
  982. return circuit_get_by_circid_channel_impl(circ_id, chan, &found) != NULL
  983. || found;
  984. }
  985. /** Return the circuit that a given edge connection is using. */
  986. circuit_t *
  987. circuit_get_by_edge_conn(edge_connection_t *conn)
  988. {
  989. circuit_t *circ;
  990. circ = conn->on_circuit;
  991. tor_assert(!circ ||
  992. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  993. : circ->magic == OR_CIRCUIT_MAGIC));
  994. return circ;
  995. }
  996. /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
  997. * circuit from the chan,circid map, and mark it for close if it hasn't
  998. * been marked already.
  999. */
  1000. void
  1001. circuit_unlink_all_from_channel(channel_t *chan, int reason)
  1002. {
  1003. circuit_t *circ;
  1004. channel_unlink_all_circuits(chan);
  1005. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1006. int mark = 0;
  1007. if (circ->n_chan == chan) {
  1008. circuit_set_n_circid_chan(circ, 0, NULL);
  1009. mark = 1;
  1010. /* If we didn't request this closure, pass the remote
  1011. * bit to mark_for_close. */
  1012. if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
  1013. reason |= END_CIRC_REASON_FLAG_REMOTE;
  1014. }
  1015. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1016. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1017. if (or_circ->p_chan == chan) {
  1018. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1019. mark = 1;
  1020. }
  1021. }
  1022. if (mark && !circ->marked_for_close)
  1023. circuit_mark_for_close(circ, reason);
  1024. }
  1025. }
  1026. /** Return a circ such that
  1027. * - circ-\>rend_data-\>onion_address is equal to
  1028. * <b>rend_data</b>-\>onion_address,
  1029. * - circ-\>rend_data-\>rend_cookie is equal to
  1030. * <b>rend_data</b>-\>rend_cookie, and
  1031. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  1032. *
  1033. * Return NULL if no such circuit exists.
  1034. */
  1035. origin_circuit_t *
  1036. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  1037. {
  1038. circuit_t *circ;
  1039. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1040. if (!circ->marked_for_close &&
  1041. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  1042. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1043. if (ocirc->rend_data &&
  1044. !rend_cmp_service_ids(rend_data->onion_address,
  1045. ocirc->rend_data->onion_address) &&
  1046. tor_memeq(ocirc->rend_data->rend_cookie,
  1047. rend_data->rend_cookie,
  1048. REND_COOKIE_LEN))
  1049. return ocirc;
  1050. }
  1051. }
  1052. return NULL;
  1053. }
  1054. /** Return the first circuit originating here in global_circuitlist after
  1055. * <b>start</b> whose purpose is <b>purpose</b>, and where
  1056. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  1057. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  1058. */
  1059. origin_circuit_t *
  1060. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  1061. const char *digest, uint8_t purpose)
  1062. {
  1063. circuit_t *circ;
  1064. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  1065. if (start == NULL)
  1066. circ = TOR_LIST_FIRST(&global_circuitlist);
  1067. else
  1068. circ = TOR_LIST_NEXT(TO_CIRCUIT(start), head);
  1069. for ( ; circ; circ = TOR_LIST_NEXT(circ, head)) {
  1070. if (circ->marked_for_close)
  1071. continue;
  1072. if (circ->purpose != purpose)
  1073. continue;
  1074. if (!digest)
  1075. return TO_ORIGIN_CIRCUIT(circ);
  1076. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  1077. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  1078. digest, DIGEST_LEN))
  1079. return TO_ORIGIN_CIRCUIT(circ);
  1080. }
  1081. return NULL;
  1082. }
  1083. /** Return the first OR circuit in the global list whose purpose is
  1084. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  1085. * <b>token</b>. */
  1086. static or_circuit_t *
  1087. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  1088. size_t len)
  1089. {
  1090. circuit_t *circ;
  1091. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1092. if (! circ->marked_for_close &&
  1093. circ->purpose == purpose &&
  1094. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  1095. return TO_OR_CIRCUIT(circ);
  1096. }
  1097. return NULL;
  1098. }
  1099. /** Return the circuit waiting for a rendezvous with the provided cookie.
  1100. * Return NULL if no such circuit is found.
  1101. */
  1102. or_circuit_t *
  1103. circuit_get_rendezvous(const char *cookie)
  1104. {
  1105. return circuit_get_by_rend_token_and_purpose(
  1106. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  1107. cookie, REND_COOKIE_LEN);
  1108. }
  1109. /** Return the circuit waiting for intro cells of the given digest.
  1110. * Return NULL if no such circuit is found.
  1111. */
  1112. or_circuit_t *
  1113. circuit_get_intro_point(const char *digest)
  1114. {
  1115. return circuit_get_by_rend_token_and_purpose(
  1116. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  1117. DIGEST_LEN);
  1118. }
  1119. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1120. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1121. * flags in <b>flags</b>, and if info is defined, does not already use info
  1122. * as any of its hops; or NULL if no circuit fits this description.
  1123. *
  1124. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  1125. * the circuit we want to create, not the purpose of the circuit we want to
  1126. * cannibalize.
  1127. *
  1128. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1129. */
  1130. origin_circuit_t *
  1131. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  1132. int flags)
  1133. {
  1134. circuit_t *circ_;
  1135. origin_circuit_t *best=NULL;
  1136. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1137. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1138. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1139. const or_options_t *options = get_options();
  1140. /* Make sure we're not trying to create a onehop circ by
  1141. * cannibalization. */
  1142. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1143. log_debug(LD_CIRC,
  1144. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1145. "capacity %d, internal %d",
  1146. purpose, need_uptime, need_capacity, internal);
  1147. TOR_LIST_FOREACH(circ_, &global_circuitlist, head) {
  1148. if (CIRCUIT_IS_ORIGIN(circ_) &&
  1149. circ_->state == CIRCUIT_STATE_OPEN &&
  1150. !circ_->marked_for_close &&
  1151. circ_->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1152. !circ_->timestamp_dirty) {
  1153. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
  1154. if ((!need_uptime || circ->build_state->need_uptime) &&
  1155. (!need_capacity || circ->build_state->need_capacity) &&
  1156. (internal == circ->build_state->is_internal) &&
  1157. !circ->unusable_for_new_conns &&
  1158. circ->remaining_relay_early_cells &&
  1159. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  1160. !circ->build_state->onehop_tunnel &&
  1161. !circ->isolation_values_set) {
  1162. if (info) {
  1163. /* need to make sure we don't duplicate hops */
  1164. crypt_path_t *hop = circ->cpath;
  1165. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1166. do {
  1167. const node_t *ri2;
  1168. if (tor_memeq(hop->extend_info->identity_digest,
  1169. info->identity_digest, DIGEST_LEN))
  1170. goto next;
  1171. if (ri1 &&
  1172. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1173. && nodes_in_same_family(ri1, ri2))
  1174. goto next;
  1175. hop=hop->next;
  1176. } while (hop!=circ->cpath);
  1177. }
  1178. if (options->ExcludeNodes) {
  1179. /* Make sure no existing nodes in the circuit are excluded for
  1180. * general use. (This may be possible if StrictNodes is 0, and we
  1181. * thought we needed to use an otherwise excluded node for, say, a
  1182. * directory operation.) */
  1183. crypt_path_t *hop = circ->cpath;
  1184. do {
  1185. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1186. hop->extend_info))
  1187. goto next;
  1188. hop = hop->next;
  1189. } while (hop != circ->cpath);
  1190. }
  1191. if (!best || (best->build_state->need_uptime && !need_uptime))
  1192. best = circ;
  1193. next: ;
  1194. }
  1195. }
  1196. }
  1197. return best;
  1198. }
  1199. /** Return the number of hops in circuit's path. */
  1200. int
  1201. circuit_get_cpath_len(origin_circuit_t *circ)
  1202. {
  1203. int n = 0;
  1204. if (circ && circ->cpath) {
  1205. crypt_path_t *cpath, *cpath_next = NULL;
  1206. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1207. cpath_next = cpath->next;
  1208. ++n;
  1209. }
  1210. }
  1211. return n;
  1212. }
  1213. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1214. * aren't that many hops in the list. */
  1215. crypt_path_t *
  1216. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1217. {
  1218. if (circ && circ->cpath && hopnum > 0) {
  1219. crypt_path_t *cpath, *cpath_next = NULL;
  1220. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1221. cpath_next = cpath->next;
  1222. if (--hopnum <= 0)
  1223. return cpath;
  1224. }
  1225. }
  1226. return NULL;
  1227. }
  1228. /** Go through the circuitlist; mark-for-close each circuit that starts
  1229. * at us but has not yet been used. */
  1230. void
  1231. circuit_mark_all_unused_circs(void)
  1232. {
  1233. circuit_t *circ;
  1234. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1235. if (CIRCUIT_IS_ORIGIN(circ) &&
  1236. !circ->marked_for_close &&
  1237. !circ->timestamp_dirty)
  1238. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1239. }
  1240. }
  1241. /** Go through the circuitlist; for each circuit that starts at us
  1242. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1243. * new streams.
  1244. *
  1245. * This is useful for letting the user change pseudonyms, so new
  1246. * streams will not be linkable to old streams.
  1247. */
  1248. void
  1249. circuit_mark_all_dirty_circs_as_unusable(void)
  1250. {
  1251. circuit_t *circ;
  1252. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1253. if (CIRCUIT_IS_ORIGIN(circ) &&
  1254. !circ->marked_for_close &&
  1255. circ->timestamp_dirty) {
  1256. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  1257. }
  1258. }
  1259. }
  1260. /** Mark <b>circ</b> to be closed next time we call
  1261. * circuit_close_all_marked(). Do any cleanup needed:
  1262. * - If state is onionskin_pending, remove circ from the onion_pending
  1263. * list.
  1264. * - If circ isn't open yet: call circuit_build_failed() if we're
  1265. * the origin, and in either case call circuit_rep_hist_note_result()
  1266. * to note stats.
  1267. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1268. * failure we just had to the hidden service client module.
  1269. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1270. * report to the hidden service client module that the intro point
  1271. * we just tried may be unreachable.
  1272. * - Send appropriate destroys and edge_destroys for conns and
  1273. * streams attached to circ.
  1274. * - If circ->rend_splice is set (we are the midpoint of a joined
  1275. * rendezvous stream), then mark the other circuit to close as well.
  1276. */
  1277. MOCK_IMPL(void,
  1278. circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
  1279. const char *file))
  1280. {
  1281. int orig_reason = reason; /* Passed to the controller */
  1282. assert_circuit_ok(circ);
  1283. tor_assert(line);
  1284. tor_assert(file);
  1285. if (circ->marked_for_close) {
  1286. log_warn(LD_BUG,
  1287. "Duplicate call to circuit_mark_for_close at %s:%d"
  1288. " (first at %s:%d)", file, line,
  1289. circ->marked_for_close_file, circ->marked_for_close);
  1290. return;
  1291. }
  1292. if (reason == END_CIRC_AT_ORIGIN) {
  1293. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1294. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1295. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1296. file, line, circ->purpose);
  1297. }
  1298. reason = END_CIRC_REASON_NONE;
  1299. }
  1300. if (CIRCUIT_IS_ORIGIN(circ)) {
  1301. if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
  1302. /* Don't close it yet, we need to test it first */
  1303. return;
  1304. }
  1305. /* We don't send reasons when closing circuits at the origin. */
  1306. reason = END_CIRC_REASON_NONE;
  1307. }
  1308. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1309. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1310. if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
  1311. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1312. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1313. reason = END_CIRC_REASON_NONE;
  1314. }
  1315. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1316. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1317. }
  1318. /* If the circuit ever became OPEN, we sent it to the reputation history
  1319. * module then. If it isn't OPEN, we send it there now to remember which
  1320. * links worked and which didn't.
  1321. */
  1322. if (circ->state != CIRCUIT_STATE_OPEN) {
  1323. if (CIRCUIT_IS_ORIGIN(circ)) {
  1324. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1325. circuit_build_failed(ocirc); /* take actions if necessary */
  1326. circuit_rep_hist_note_result(ocirc);
  1327. }
  1328. }
  1329. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1330. if (circuits_pending_chans)
  1331. smartlist_remove(circuits_pending_chans, circ);
  1332. }
  1333. if (CIRCUIT_IS_ORIGIN(circ)) {
  1334. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1335. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1336. orig_reason);
  1337. }
  1338. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1339. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1340. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1341. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1342. tor_assert(ocirc->build_state->chosen_exit);
  1343. tor_assert(ocirc->rend_data);
  1344. /* treat this like getting a nack from it */
  1345. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1346. safe_str_client(ocirc->rend_data->onion_address),
  1347. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1348. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1349. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1350. ocirc->rend_data,
  1351. timed_out ?
  1352. INTRO_POINT_FAILURE_TIMEOUT :
  1353. INTRO_POINT_FAILURE_GENERIC);
  1354. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1355. reason != END_CIRC_REASON_TIMEOUT) {
  1356. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1357. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1358. log_info(LD_REND, "Failed intro circ %s to %s "
  1359. "(building circuit to intro point). "
  1360. "Marking intro point as possibly unreachable.",
  1361. safe_str_client(ocirc->rend_data->onion_address),
  1362. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1363. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1364. ocirc->rend_data,
  1365. INTRO_POINT_FAILURE_UNREACHABLE);
  1366. }
  1367. }
  1368. if (circ->n_chan) {
  1369. circuit_clear_cell_queue(circ, circ->n_chan);
  1370. /* Only send destroy if the channel isn't closing anyway */
  1371. if (!(circ->n_chan->state == CHANNEL_STATE_CLOSING ||
  1372. circ->n_chan->state == CHANNEL_STATE_CLOSED ||
  1373. circ->n_chan->state == CHANNEL_STATE_ERROR)) {
  1374. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1375. }
  1376. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1377. }
  1378. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1379. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1380. edge_connection_t *conn;
  1381. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1382. connection_edge_destroy(or_circ->p_circ_id, conn);
  1383. or_circ->n_streams = NULL;
  1384. while (or_circ->resolving_streams) {
  1385. conn = or_circ->resolving_streams;
  1386. or_circ->resolving_streams = conn->next_stream;
  1387. if (!conn->base_.marked_for_close) {
  1388. /* The client will see a DESTROY, and infer that the connections
  1389. * are closing because the circuit is getting torn down. No need
  1390. * to send an end cell. */
  1391. conn->edge_has_sent_end = 1;
  1392. conn->end_reason = END_STREAM_REASON_DESTROY;
  1393. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1394. connection_mark_for_close(TO_CONN(conn));
  1395. }
  1396. conn->on_circuit = NULL;
  1397. }
  1398. if (or_circ->p_chan) {
  1399. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1400. /* Only send destroy if the channel isn't closing anyway */
  1401. if (!(or_circ->p_chan->state == CHANNEL_STATE_CLOSING ||
  1402. or_circ->p_chan->state == CHANNEL_STATE_CLOSED ||
  1403. or_circ->p_chan->state == CHANNEL_STATE_ERROR)) {
  1404. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1405. }
  1406. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1407. }
  1408. } else {
  1409. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1410. edge_connection_t *conn;
  1411. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1412. connection_edge_destroy(circ->n_circ_id, conn);
  1413. ocirc->p_streams = NULL;
  1414. }
  1415. circ->marked_for_close = line;
  1416. circ->marked_for_close_file = file;
  1417. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1418. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1419. if (or_circ->rend_splice) {
  1420. if (!or_circ->rend_splice->base_.marked_for_close) {
  1421. /* do this after marking this circuit, to avoid infinite recursion. */
  1422. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1423. }
  1424. or_circ->rend_splice = NULL;
  1425. }
  1426. }
  1427. }
  1428. /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
  1429. * recover memory. */
  1430. static void
  1431. marked_circuit_free_cells(circuit_t *circ)
  1432. {
  1433. if (!circ->marked_for_close) {
  1434. log_warn(LD_BUG, "Called on non-marked circuit");
  1435. return;
  1436. }
  1437. cell_queue_clear(&circ->n_chan_cells);
  1438. if (! CIRCUIT_IS_ORIGIN(circ))
  1439. cell_queue_clear(& TO_OR_CIRCUIT(circ)->p_chan_cells);
  1440. }
  1441. /** Aggressively free buffer contents on all the buffers of all streams in the
  1442. * list starting at <b>stream</b>. Return the number of bytes recovered. */
  1443. static size_t
  1444. marked_circuit_streams_free_bytes(edge_connection_t *stream)
  1445. {
  1446. size_t result = 0;
  1447. for ( ; stream; stream = stream->next_stream) {
  1448. connection_t *conn = TO_CONN(stream);
  1449. if (conn->inbuf) {
  1450. result += buf_allocation(conn->inbuf);
  1451. buf_clear(conn->inbuf);
  1452. }
  1453. if (conn->outbuf) {
  1454. result += buf_allocation(conn->outbuf);
  1455. buf_clear(conn->outbuf);
  1456. }
  1457. }
  1458. return result;
  1459. }
  1460. /** Aggressively free buffer contents on all the buffers of all streams on
  1461. * circuit <b>c</b>. Return the number of bytes recovered. */
  1462. static size_t
  1463. marked_circuit_free_stream_bytes(circuit_t *c)
  1464. {
  1465. if (CIRCUIT_IS_ORIGIN(c)) {
  1466. return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
  1467. } else {
  1468. return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
  1469. }
  1470. }
  1471. /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
  1472. STATIC size_t
  1473. n_cells_in_circ_queues(const circuit_t *c)
  1474. {
  1475. size_t n = c->n_chan_cells.n;
  1476. if (! CIRCUIT_IS_ORIGIN(c)) {
  1477. circuit_t *cc = (circuit_t *) c;
  1478. n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
  1479. }
  1480. return n;
  1481. }
  1482. /**
  1483. * Return the age of the oldest cell queued on <b>c</b>, in milliseconds.
  1484. * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
  1485. * the current time in milliseconds since the epoch, truncated.
  1486. *
  1487. * This function will return incorrect results if the oldest cell queued on
  1488. * the circuit is older than 2**32 msec (about 49 days) old.
  1489. */
  1490. STATIC uint32_t
  1491. circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
  1492. {
  1493. uint32_t age = 0;
  1494. packed_cell_t *cell;
  1495. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
  1496. age = now - cell->inserted_time;
  1497. if (! CIRCUIT_IS_ORIGIN(c)) {
  1498. const or_circuit_t *orcirc = TO_OR_CIRCUIT((circuit_t*)c);
  1499. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
  1500. uint32_t age2 = now - cell->inserted_time;
  1501. if (age2 > age)
  1502. return age2;
  1503. }
  1504. }
  1505. return age;
  1506. }
  1507. /** Return the age in milliseconds of the oldest buffer chunk on any stream in
  1508. * the linked list <b>stream</b>, where age is taken in milliseconds before
  1509. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1510. static uint32_t
  1511. circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
  1512. {
  1513. uint32_t age = 0, age2;
  1514. for (; stream; stream = stream->next_stream) {
  1515. const connection_t *conn = TO_CONN(stream);
  1516. if (conn->outbuf) {
  1517. age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now);
  1518. if (age2 > age)
  1519. age = age2;
  1520. }
  1521. if (conn->inbuf) {
  1522. age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now);
  1523. if (age2 > age)
  1524. age = age2;
  1525. }
  1526. }
  1527. return age;
  1528. }
  1529. /** Return the age in milliseconds of the oldest buffer chunk on any stream
  1530. * attached to the circuit <b>c</b>, where age is taken in milliseconds before
  1531. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1532. STATIC uint32_t
  1533. circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
  1534. {
  1535. if (CIRCUIT_IS_ORIGIN(c)) {
  1536. return circuit_get_streams_max_data_age(
  1537. TO_ORIGIN_CIRCUIT((circuit_t*)c)->p_streams, now);
  1538. } else {
  1539. return circuit_get_streams_max_data_age(
  1540. TO_OR_CIRCUIT((circuit_t*)c)->n_streams, now);
  1541. }
  1542. }
  1543. /** Return the age of the oldest cell or stream buffer chunk on the circuit
  1544. * <b>c</b>, where age is taken in milliseconds before the time <b>now</b> (in
  1545. * truncated milliseconds since the epoch). */
  1546. STATIC uint32_t
  1547. circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
  1548. {
  1549. uint32_t cell_age = circuit_max_queued_cell_age(c, now);
  1550. uint32_t data_age = circuit_max_queued_data_age(c, now);
  1551. if (cell_age > data_age)
  1552. return cell_age;
  1553. else
  1554. return data_age;
  1555. }
  1556. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  1557. * order. */
  1558. static int
  1559. circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
  1560. {
  1561. const circuit_t *a = *a_;
  1562. const circuit_t *b = *b_;
  1563. uint32_t age_a = a->age_tmp;
  1564. uint32_t age_b = b->age_tmp;
  1565. if (age_a < age_b)
  1566. return 1;
  1567. else if (age_a == age_b)
  1568. return 0;
  1569. else
  1570. return -1;
  1571. }
  1572. #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
  1573. /** We're out of memory for cells, having allocated <b>current_allocation</b>
  1574. * bytes' worth. Kill the 'worst' circuits until we're under
  1575. * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
  1576. void
  1577. circuits_handle_oom(size_t current_allocation)
  1578. {
  1579. /* Let's hope there's enough slack space for this allocation here... */
  1580. smartlist_t *circlist = smartlist_new();
  1581. circuit_t *circ;
  1582. size_t mem_to_recover;
  1583. size_t mem_recovered=0;
  1584. int n_circuits_killed=0;
  1585. struct timeval now;
  1586. uint32_t now_ms;
  1587. log_notice(LD_GENERAL, "We're low on memory. Killing circuits with "
  1588. "over-long queues. (This behavior is controlled by "
  1589. "MaxMemInQueues.)");
  1590. {
  1591. const size_t recovered = buf_shrink_freelists(1);
  1592. if (recovered >= current_allocation) {
  1593. log_warn(LD_BUG, "We somehow recovered more memory from freelists "
  1594. "than we thought we had allocated");
  1595. current_allocation = 0;
  1596. } else {
  1597. current_allocation -= recovered;
  1598. }
  1599. }
  1600. {
  1601. size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
  1602. FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
  1603. if (current_allocation <= mem_target)
  1604. return;
  1605. mem_to_recover = current_allocation - mem_target;
  1606. }
  1607. tor_gettimeofday_cached(&now);
  1608. now_ms = (uint32_t)tv_to_msec(&now);
  1609. /* This algorithm itself assumes that you've got enough memory slack
  1610. * to actually run it. */
  1611. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1612. circ->age_tmp = circuit_max_queued_item_age(circ, now_ms);
  1613. smartlist_add(circlist, circ);
  1614. }
  1615. /* This is O(n log n); there are faster algorithms we could use instead.
  1616. * Let's hope this doesn't happen enough to be in the critical path. */
  1617. smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
  1618. /* Okay, now the worst circuits are at the front of the list. Let's mark
  1619. * them, and reclaim their storage aggressively. */
  1620. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  1621. size_t n = n_cells_in_circ_queues(circ);
  1622. size_t freed;
  1623. if (! circ->marked_for_close) {
  1624. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  1625. }
  1626. marked_circuit_free_cells(circ);
  1627. freed = marked_circuit_free_stream_bytes(circ);
  1628. ++n_circuits_killed;
  1629. mem_recovered += n * packed_cell_mem_cost();
  1630. mem_recovered += freed;
  1631. if (mem_recovered >= mem_to_recover)
  1632. break;
  1633. } SMARTLIST_FOREACH_END(circ);
  1634. clean_cell_pool(); /* In case this helps. */
  1635. buf_shrink_freelists(1); /* This is necessary to actually release buffer
  1636. chunks. */
  1637. log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits.",
  1638. U64_PRINTF_ARG(mem_recovered),
  1639. n_circuits_killed);
  1640. smartlist_free(circlist);
  1641. }
  1642. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1643. * correct. Trigger an assert if anything is invalid.
  1644. */
  1645. void
  1646. assert_cpath_layer_ok(const crypt_path_t *cp)
  1647. {
  1648. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1649. // tor_assert(cp->port);
  1650. tor_assert(cp);
  1651. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1652. switch (cp->state)
  1653. {
  1654. case CPATH_STATE_OPEN:
  1655. tor_assert(cp->f_crypto);
  1656. tor_assert(cp->b_crypto);
  1657. /* fall through */
  1658. case CPATH_STATE_CLOSED:
  1659. /*XXXX Assert that there's no handshake_state either. */
  1660. tor_assert(!cp->rend_dh_handshake_state);
  1661. break;
  1662. case CPATH_STATE_AWAITING_KEYS:
  1663. /* tor_assert(cp->dh_handshake_state); */
  1664. break;
  1665. default:
  1666. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1667. tor_assert(0);
  1668. }
  1669. tor_assert(cp->package_window >= 0);
  1670. tor_assert(cp->deliver_window >= 0);
  1671. }
  1672. /** Verify that cpath <b>cp</b> has all of its invariants
  1673. * correct. Trigger an assert if anything is invalid.
  1674. */
  1675. static void
  1676. assert_cpath_ok(const crypt_path_t *cp)
  1677. {
  1678. const crypt_path_t *start = cp;
  1679. do {
  1680. assert_cpath_layer_ok(cp);
  1681. /* layers must be in sequence of: "open* awaiting? closed*" */
  1682. if (cp != start) {
  1683. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1684. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1685. } else if (cp->state == CPATH_STATE_OPEN) {
  1686. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1687. }
  1688. }
  1689. cp = cp->next;
  1690. tor_assert(cp);
  1691. } while (cp != start);
  1692. }
  1693. /** Verify that circuit <b>c</b> has all of its invariants
  1694. * correct. Trigger an assert if anything is invalid.
  1695. */
  1696. void
  1697. assert_circuit_ok(const circuit_t *c)
  1698. {
  1699. edge_connection_t *conn;
  1700. const or_circuit_t *or_circ = NULL;
  1701. const origin_circuit_t *origin_circ = NULL;
  1702. tor_assert(c);
  1703. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1704. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  1705. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  1706. {
  1707. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1708. * never understand. -NM. */
  1709. circuit_t *nonconst_circ = (circuit_t*) c;
  1710. if (CIRCUIT_IS_ORIGIN(c))
  1711. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1712. else
  1713. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1714. }
  1715. if (c->n_chan) {
  1716. tor_assert(!c->n_hop);
  1717. if (c->n_circ_id) {
  1718. /* We use the _impl variant here to make sure we don't fail on marked
  1719. * circuits, which would not be returned by the regular function. */
  1720. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  1721. c->n_chan, NULL);
  1722. tor_assert(c == c2);
  1723. }
  1724. }
  1725. if (or_circ && or_circ->p_chan) {
  1726. if (or_circ->p_circ_id) {
  1727. /* ibid */
  1728. circuit_t *c2 =
  1729. circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  1730. or_circ->p_chan, NULL);
  1731. tor_assert(c == c2);
  1732. }
  1733. }
  1734. if (or_circ)
  1735. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1736. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  1737. tor_assert(c->deliver_window >= 0);
  1738. tor_assert(c->package_window >= 0);
  1739. if (c->state == CIRCUIT_STATE_OPEN) {
  1740. tor_assert(!c->n_chan_create_cell);
  1741. if (or_circ) {
  1742. tor_assert(or_circ->n_crypto);
  1743. tor_assert(or_circ->p_crypto);
  1744. tor_assert(or_circ->n_digest);
  1745. tor_assert(or_circ->p_digest);
  1746. }
  1747. }
  1748. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  1749. tor_assert(circuits_pending_chans &&
  1750. smartlist_contains(circuits_pending_chans, c));
  1751. } else {
  1752. tor_assert(!circuits_pending_chans ||
  1753. !smartlist_contains(circuits_pending_chans, c));
  1754. }
  1755. if (origin_circ && origin_circ->cpath) {
  1756. assert_cpath_ok(origin_circ->cpath);
  1757. }
  1758. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1759. tor_assert(or_circ);
  1760. if (!c->marked_for_close) {
  1761. tor_assert(or_circ->rend_splice);
  1762. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1763. }
  1764. tor_assert(or_circ->rend_splice != or_circ);
  1765. } else {
  1766. tor_assert(!or_circ || !or_circ->rend_splice);
  1767. }
  1768. }