circuitlist.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  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. smartlist_t *detached = smartlist_new();
  1005. #define DEBUG_CIRCUIT_UNLINK_ALL
  1006. channel_unlink_all_circuits(chan, detached);
  1007. #ifdef DEBUG_CIRCUIT_UNLINK_ALL
  1008. {
  1009. smartlist_t *detached_2 = smartlist_new();
  1010. int mismatch = 0, badlen = 0;
  1011. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1012. if (circ->n_chan == chan ||
  1013. (!CIRCUIT_IS_ORIGIN(circ) &&
  1014. TO_OR_CIRCUIT(circ)->p_chan == chan)) {
  1015. smartlist_add(detached_2, circ);
  1016. }
  1017. }
  1018. if (smartlist_len(detached) != smartlist_len(detached_2)) {
  1019. log_warn(LD_BUG, "List of detached circuits had the wrong length! "
  1020. "(got %d, should have gotten %d)",
  1021. (int)smartlist_len(detached),
  1022. (int)smartlist_len(detached_2));
  1023. badlen = 1;
  1024. }
  1025. smartlist_sort_pointers(detached);
  1026. smartlist_sort_pointers(detached_2);
  1027. SMARTLIST_FOREACH(detached, circuit_t *, c,
  1028. if (c != smartlist_get(detached_2, c_sl_idx))
  1029. mismatch = 1;
  1030. );
  1031. if (mismatch)
  1032. log_warn(LD_BUG, "Mismatch in list of detached circuits.");
  1033. if (badlen || mismatch) {
  1034. smartlist_free(detached);
  1035. detached = detached_2;
  1036. } else {
  1037. log_notice(LD_CIRC, "List of %d circuits was as expected.",
  1038. (int)smartlist_len(detached));
  1039. smartlist_free(detached_2);
  1040. }
  1041. }
  1042. #endif
  1043. SMARTLIST_FOREACH_BEGIN(detached, circuit_t *, circ) {
  1044. int mark = 0;
  1045. if (circ->n_chan == chan) {
  1046. circuit_set_n_circid_chan(circ, 0, NULL);
  1047. mark = 1;
  1048. /* If we didn't request this closure, pass the remote
  1049. * bit to mark_for_close. */
  1050. if (chan->reason_for_closing != CHANNEL_CLOSE_REQUESTED)
  1051. reason |= END_CIRC_REASON_FLAG_REMOTE;
  1052. }
  1053. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1054. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1055. if (or_circ->p_chan == chan) {
  1056. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1057. mark = 1;
  1058. }
  1059. }
  1060. if (!mark) {
  1061. log_warn(LD_BUG, "Circuit on detached list which I had no reason "
  1062. "to mark");
  1063. continue;
  1064. }
  1065. if (!circ->marked_for_close)
  1066. circuit_mark_for_close(circ, reason);
  1067. } SMARTLIST_FOREACH_END(circ);
  1068. smartlist_free(detached);
  1069. }
  1070. /** Return a circ such that
  1071. * - circ-\>rend_data-\>onion_address is equal to
  1072. * <b>rend_data</b>-\>onion_address,
  1073. * - circ-\>rend_data-\>rend_cookie is equal to
  1074. * <b>rend_data</b>-\>rend_cookie, and
  1075. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  1076. *
  1077. * Return NULL if no such circuit exists.
  1078. */
  1079. origin_circuit_t *
  1080. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  1081. {
  1082. circuit_t *circ;
  1083. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1084. if (!circ->marked_for_close &&
  1085. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  1086. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1087. if (ocirc->rend_data &&
  1088. !rend_cmp_service_ids(rend_data->onion_address,
  1089. ocirc->rend_data->onion_address) &&
  1090. tor_memeq(ocirc->rend_data->rend_cookie,
  1091. rend_data->rend_cookie,
  1092. REND_COOKIE_LEN))
  1093. return ocirc;
  1094. }
  1095. }
  1096. return NULL;
  1097. }
  1098. /** Return the first circuit originating here in global_circuitlist after
  1099. * <b>start</b> whose purpose is <b>purpose</b>, and where
  1100. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  1101. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  1102. */
  1103. origin_circuit_t *
  1104. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  1105. const char *digest, uint8_t purpose)
  1106. {
  1107. circuit_t *circ;
  1108. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  1109. if (start == NULL)
  1110. circ = TOR_LIST_FIRST(&global_circuitlist);
  1111. else
  1112. circ = TOR_LIST_NEXT(TO_CIRCUIT(start), head);
  1113. for ( ; circ; circ = TOR_LIST_NEXT(circ, head)) {
  1114. if (circ->marked_for_close)
  1115. continue;
  1116. if (circ->purpose != purpose)
  1117. continue;
  1118. if (!digest)
  1119. return TO_ORIGIN_CIRCUIT(circ);
  1120. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  1121. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  1122. digest, DIGEST_LEN))
  1123. return TO_ORIGIN_CIRCUIT(circ);
  1124. }
  1125. return NULL;
  1126. }
  1127. /** Return the first OR circuit in the global list whose purpose is
  1128. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  1129. * <b>token</b>. */
  1130. static or_circuit_t *
  1131. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  1132. size_t len)
  1133. {
  1134. circuit_t *circ;
  1135. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1136. if (! circ->marked_for_close &&
  1137. circ->purpose == purpose &&
  1138. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  1139. return TO_OR_CIRCUIT(circ);
  1140. }
  1141. return NULL;
  1142. }
  1143. /** Return the circuit waiting for a rendezvous with the provided cookie.
  1144. * Return NULL if no such circuit is found.
  1145. */
  1146. or_circuit_t *
  1147. circuit_get_rendezvous(const char *cookie)
  1148. {
  1149. return circuit_get_by_rend_token_and_purpose(
  1150. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  1151. cookie, REND_COOKIE_LEN);
  1152. }
  1153. /** Return the circuit waiting for intro cells of the given digest.
  1154. * Return NULL if no such circuit is found.
  1155. */
  1156. or_circuit_t *
  1157. circuit_get_intro_point(const char *digest)
  1158. {
  1159. return circuit_get_by_rend_token_and_purpose(
  1160. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  1161. DIGEST_LEN);
  1162. }
  1163. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1164. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1165. * flags in <b>flags</b>, and if info is defined, does not already use info
  1166. * as any of its hops; or NULL if no circuit fits this description.
  1167. *
  1168. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  1169. * the circuit we want to create, not the purpose of the circuit we want to
  1170. * cannibalize.
  1171. *
  1172. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1173. */
  1174. origin_circuit_t *
  1175. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  1176. int flags)
  1177. {
  1178. circuit_t *circ_;
  1179. origin_circuit_t *best=NULL;
  1180. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1181. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1182. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1183. const or_options_t *options = get_options();
  1184. /* Make sure we're not trying to create a onehop circ by
  1185. * cannibalization. */
  1186. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1187. log_debug(LD_CIRC,
  1188. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1189. "capacity %d, internal %d",
  1190. purpose, need_uptime, need_capacity, internal);
  1191. TOR_LIST_FOREACH(circ_, &global_circuitlist, head) {
  1192. if (CIRCUIT_IS_ORIGIN(circ_) &&
  1193. circ_->state == CIRCUIT_STATE_OPEN &&
  1194. !circ_->marked_for_close &&
  1195. circ_->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1196. !circ_->timestamp_dirty) {
  1197. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
  1198. if ((!need_uptime || circ->build_state->need_uptime) &&
  1199. (!need_capacity || circ->build_state->need_capacity) &&
  1200. (internal == circ->build_state->is_internal) &&
  1201. !circ->unusable_for_new_conns &&
  1202. circ->remaining_relay_early_cells &&
  1203. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  1204. !circ->build_state->onehop_tunnel &&
  1205. !circ->isolation_values_set) {
  1206. if (info) {
  1207. /* need to make sure we don't duplicate hops */
  1208. crypt_path_t *hop = circ->cpath;
  1209. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1210. do {
  1211. const node_t *ri2;
  1212. if (tor_memeq(hop->extend_info->identity_digest,
  1213. info->identity_digest, DIGEST_LEN))
  1214. goto next;
  1215. if (ri1 &&
  1216. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1217. && nodes_in_same_family(ri1, ri2))
  1218. goto next;
  1219. hop=hop->next;
  1220. } while (hop!=circ->cpath);
  1221. }
  1222. if (options->ExcludeNodes) {
  1223. /* Make sure no existing nodes in the circuit are excluded for
  1224. * general use. (This may be possible if StrictNodes is 0, and we
  1225. * thought we needed to use an otherwise excluded node for, say, a
  1226. * directory operation.) */
  1227. crypt_path_t *hop = circ->cpath;
  1228. do {
  1229. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1230. hop->extend_info))
  1231. goto next;
  1232. hop = hop->next;
  1233. } while (hop != circ->cpath);
  1234. }
  1235. if (!best || (best->build_state->need_uptime && !need_uptime))
  1236. best = circ;
  1237. next: ;
  1238. }
  1239. }
  1240. }
  1241. return best;
  1242. }
  1243. /** Return the number of hops in circuit's path. */
  1244. int
  1245. circuit_get_cpath_len(origin_circuit_t *circ)
  1246. {
  1247. int n = 0;
  1248. if (circ && circ->cpath) {
  1249. crypt_path_t *cpath, *cpath_next = NULL;
  1250. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1251. cpath_next = cpath->next;
  1252. ++n;
  1253. }
  1254. }
  1255. return n;
  1256. }
  1257. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1258. * aren't that many hops in the list. */
  1259. crypt_path_t *
  1260. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1261. {
  1262. if (circ && circ->cpath && hopnum > 0) {
  1263. crypt_path_t *cpath, *cpath_next = NULL;
  1264. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1265. cpath_next = cpath->next;
  1266. if (--hopnum <= 0)
  1267. return cpath;
  1268. }
  1269. }
  1270. return NULL;
  1271. }
  1272. /** Go through the circuitlist; mark-for-close each circuit that starts
  1273. * at us but has not yet been used. */
  1274. void
  1275. circuit_mark_all_unused_circs(void)
  1276. {
  1277. circuit_t *circ;
  1278. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1279. if (CIRCUIT_IS_ORIGIN(circ) &&
  1280. !circ->marked_for_close &&
  1281. !circ->timestamp_dirty)
  1282. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1283. }
  1284. }
  1285. /** Go through the circuitlist; for each circuit that starts at us
  1286. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1287. * new streams.
  1288. *
  1289. * This is useful for letting the user change pseudonyms, so new
  1290. * streams will not be linkable to old streams.
  1291. */
  1292. void
  1293. circuit_mark_all_dirty_circs_as_unusable(void)
  1294. {
  1295. circuit_t *circ;
  1296. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1297. if (CIRCUIT_IS_ORIGIN(circ) &&
  1298. !circ->marked_for_close &&
  1299. circ->timestamp_dirty) {
  1300. mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
  1301. }
  1302. }
  1303. }
  1304. /** Mark <b>circ</b> to be closed next time we call
  1305. * circuit_close_all_marked(). Do any cleanup needed:
  1306. * - If state is onionskin_pending, remove circ from the onion_pending
  1307. * list.
  1308. * - If circ isn't open yet: call circuit_build_failed() if we're
  1309. * the origin, and in either case call circuit_rep_hist_note_result()
  1310. * to note stats.
  1311. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1312. * failure we just had to the hidden service client module.
  1313. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1314. * report to the hidden service client module that the intro point
  1315. * we just tried may be unreachable.
  1316. * - Send appropriate destroys and edge_destroys for conns and
  1317. * streams attached to circ.
  1318. * - If circ->rend_splice is set (we are the midpoint of a joined
  1319. * rendezvous stream), then mark the other circuit to close as well.
  1320. */
  1321. MOCK_IMPL(void,
  1322. circuit_mark_for_close_, (circuit_t *circ, int reason, int line,
  1323. const char *file))
  1324. {
  1325. int orig_reason = reason; /* Passed to the controller */
  1326. assert_circuit_ok(circ);
  1327. tor_assert(line);
  1328. tor_assert(file);
  1329. if (circ->marked_for_close) {
  1330. log_warn(LD_BUG,
  1331. "Duplicate call to circuit_mark_for_close at %s:%d"
  1332. " (first at %s:%d)", file, line,
  1333. circ->marked_for_close_file, circ->marked_for_close);
  1334. return;
  1335. }
  1336. if (reason == END_CIRC_AT_ORIGIN) {
  1337. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1338. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1339. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1340. file, line, circ->purpose);
  1341. }
  1342. reason = END_CIRC_REASON_NONE;
  1343. }
  1344. if (CIRCUIT_IS_ORIGIN(circ)) {
  1345. if (pathbias_check_close(TO_ORIGIN_CIRCUIT(circ), reason) == -1) {
  1346. /* Don't close it yet, we need to test it first */
  1347. return;
  1348. }
  1349. /* We don't send reasons when closing circuits at the origin. */
  1350. reason = END_CIRC_REASON_NONE;
  1351. }
  1352. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1353. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1354. if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
  1355. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1356. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1357. reason = END_CIRC_REASON_NONE;
  1358. }
  1359. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1360. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1361. }
  1362. /* If the circuit ever became OPEN, we sent it to the reputation history
  1363. * module then. If it isn't OPEN, we send it there now to remember which
  1364. * links worked and which didn't.
  1365. */
  1366. if (circ->state != CIRCUIT_STATE_OPEN) {
  1367. if (CIRCUIT_IS_ORIGIN(circ)) {
  1368. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1369. circuit_build_failed(ocirc); /* take actions if necessary */
  1370. circuit_rep_hist_note_result(ocirc);
  1371. }
  1372. }
  1373. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1374. if (circuits_pending_chans)
  1375. smartlist_remove(circuits_pending_chans, circ);
  1376. }
  1377. if (CIRCUIT_IS_ORIGIN(circ)) {
  1378. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1379. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1380. orig_reason);
  1381. }
  1382. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1383. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1384. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1385. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1386. tor_assert(ocirc->build_state->chosen_exit);
  1387. tor_assert(ocirc->rend_data);
  1388. /* treat this like getting a nack from it */
  1389. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1390. safe_str_client(ocirc->rend_data->onion_address),
  1391. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1392. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1393. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1394. ocirc->rend_data,
  1395. timed_out ?
  1396. INTRO_POINT_FAILURE_TIMEOUT :
  1397. INTRO_POINT_FAILURE_GENERIC);
  1398. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1399. reason != END_CIRC_REASON_TIMEOUT) {
  1400. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1401. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1402. log_info(LD_REND, "Failed intro circ %s to %s "
  1403. "(building circuit to intro point). "
  1404. "Marking intro point as possibly unreachable.",
  1405. safe_str_client(ocirc->rend_data->onion_address),
  1406. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1407. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1408. ocirc->rend_data,
  1409. INTRO_POINT_FAILURE_UNREACHABLE);
  1410. }
  1411. }
  1412. if (circ->n_chan) {
  1413. circuit_clear_cell_queue(circ, circ->n_chan);
  1414. /* Only send destroy if the channel isn't closing anyway */
  1415. if (!(circ->n_chan->state == CHANNEL_STATE_CLOSING ||
  1416. circ->n_chan->state == CHANNEL_STATE_CLOSED ||
  1417. circ->n_chan->state == CHANNEL_STATE_ERROR)) {
  1418. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1419. }
  1420. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1421. circuit_set_n_circid_chan(circ, 0, NULL);
  1422. }
  1423. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1424. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1425. edge_connection_t *conn;
  1426. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1427. connection_edge_destroy(or_circ->p_circ_id, conn);
  1428. or_circ->n_streams = NULL;
  1429. while (or_circ->resolving_streams) {
  1430. conn = or_circ->resolving_streams;
  1431. or_circ->resolving_streams = conn->next_stream;
  1432. if (!conn->base_.marked_for_close) {
  1433. /* The client will see a DESTROY, and infer that the connections
  1434. * are closing because the circuit is getting torn down. No need
  1435. * to send an end cell. */
  1436. conn->edge_has_sent_end = 1;
  1437. conn->end_reason = END_STREAM_REASON_DESTROY;
  1438. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1439. connection_mark_for_close(TO_CONN(conn));
  1440. }
  1441. conn->on_circuit = NULL;
  1442. }
  1443. if (or_circ->p_chan) {
  1444. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1445. /* Only send destroy if the channel isn't closing anyway */
  1446. if (!(or_circ->p_chan->state == CHANNEL_STATE_CLOSING ||
  1447. or_circ->p_chan->state == CHANNEL_STATE_CLOSED ||
  1448. or_circ->p_chan->state == CHANNEL_STATE_ERROR)) {
  1449. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1450. }
  1451. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1452. circuit_set_p_circid_chan(or_circ, 0, NULL);
  1453. }
  1454. } else {
  1455. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1456. edge_connection_t *conn;
  1457. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1458. connection_edge_destroy(circ->n_circ_id, conn);
  1459. ocirc->p_streams = NULL;
  1460. }
  1461. circ->marked_for_close = line;
  1462. circ->marked_for_close_file = file;
  1463. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1464. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1465. if (or_circ->rend_splice) {
  1466. if (!or_circ->rend_splice->base_.marked_for_close) {
  1467. /* do this after marking this circuit, to avoid infinite recursion. */
  1468. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1469. }
  1470. or_circ->rend_splice = NULL;
  1471. }
  1472. }
  1473. }
  1474. /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
  1475. * recover memory. */
  1476. static void
  1477. marked_circuit_free_cells(circuit_t *circ)
  1478. {
  1479. if (!circ->marked_for_close) {
  1480. log_warn(LD_BUG, "Called on non-marked circuit");
  1481. return;
  1482. }
  1483. cell_queue_clear(&circ->n_chan_cells);
  1484. if (! CIRCUIT_IS_ORIGIN(circ))
  1485. cell_queue_clear(& TO_OR_CIRCUIT(circ)->p_chan_cells);
  1486. }
  1487. /** Aggressively free buffer contents on all the buffers of all streams in the
  1488. * list starting at <b>stream</b>. Return the number of bytes recovered. */
  1489. static size_t
  1490. marked_circuit_streams_free_bytes(edge_connection_t *stream)
  1491. {
  1492. size_t result = 0;
  1493. for ( ; stream; stream = stream->next_stream) {
  1494. connection_t *conn = TO_CONN(stream);
  1495. if (conn->inbuf) {
  1496. result += buf_allocation(conn->inbuf);
  1497. buf_clear(conn->inbuf);
  1498. }
  1499. if (conn->outbuf) {
  1500. result += buf_allocation(conn->outbuf);
  1501. buf_clear(conn->outbuf);
  1502. }
  1503. }
  1504. return result;
  1505. }
  1506. /** Aggressively free buffer contents on all the buffers of all streams on
  1507. * circuit <b>c</b>. Return the number of bytes recovered. */
  1508. static size_t
  1509. marked_circuit_free_stream_bytes(circuit_t *c)
  1510. {
  1511. if (CIRCUIT_IS_ORIGIN(c)) {
  1512. return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
  1513. } else {
  1514. return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
  1515. }
  1516. }
  1517. /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
  1518. STATIC size_t
  1519. n_cells_in_circ_queues(const circuit_t *c)
  1520. {
  1521. size_t n = c->n_chan_cells.n;
  1522. if (! CIRCUIT_IS_ORIGIN(c)) {
  1523. circuit_t *cc = (circuit_t *) c;
  1524. n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
  1525. }
  1526. return n;
  1527. }
  1528. /**
  1529. * Return the age of the oldest cell queued on <b>c</b>, in milliseconds.
  1530. * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
  1531. * the current time in milliseconds since the epoch, truncated.
  1532. *
  1533. * This function will return incorrect results if the oldest cell queued on
  1534. * the circuit is older than 2**32 msec (about 49 days) old.
  1535. */
  1536. STATIC uint32_t
  1537. circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
  1538. {
  1539. uint32_t age = 0;
  1540. packed_cell_t *cell;
  1541. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
  1542. age = now - cell->inserted_time;
  1543. if (! CIRCUIT_IS_ORIGIN(c)) {
  1544. const or_circuit_t *orcirc = TO_OR_CIRCUIT((circuit_t*)c);
  1545. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
  1546. uint32_t age2 = now - cell->inserted_time;
  1547. if (age2 > age)
  1548. return age2;
  1549. }
  1550. }
  1551. return age;
  1552. }
  1553. /** Return the age in milliseconds of the oldest buffer chunk on any stream in
  1554. * the linked list <b>stream</b>, where age is taken in milliseconds before
  1555. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1556. static uint32_t
  1557. circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
  1558. {
  1559. uint32_t age = 0, age2;
  1560. for (; stream; stream = stream->next_stream) {
  1561. const connection_t *conn = TO_CONN(stream);
  1562. if (conn->outbuf) {
  1563. age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now);
  1564. if (age2 > age)
  1565. age = age2;
  1566. }
  1567. if (conn->inbuf) {
  1568. age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now);
  1569. if (age2 > age)
  1570. age = age2;
  1571. }
  1572. }
  1573. return age;
  1574. }
  1575. /** Return the age in milliseconds of the oldest buffer chunk on any stream
  1576. * attached to the circuit <b>c</b>, where age is taken in milliseconds before
  1577. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1578. STATIC uint32_t
  1579. circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
  1580. {
  1581. if (CIRCUIT_IS_ORIGIN(c)) {
  1582. return circuit_get_streams_max_data_age(
  1583. TO_ORIGIN_CIRCUIT((circuit_t*)c)->p_streams, now);
  1584. } else {
  1585. return circuit_get_streams_max_data_age(
  1586. TO_OR_CIRCUIT((circuit_t*)c)->n_streams, now);
  1587. }
  1588. }
  1589. /** Return the age of the oldest cell or stream buffer chunk on the circuit
  1590. * <b>c</b>, where age is taken in milliseconds before the time <b>now</b> (in
  1591. * truncated milliseconds since the epoch). */
  1592. STATIC uint32_t
  1593. circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
  1594. {
  1595. uint32_t cell_age = circuit_max_queued_cell_age(c, now);
  1596. uint32_t data_age = circuit_max_queued_data_age(c, now);
  1597. if (cell_age > data_age)
  1598. return cell_age;
  1599. else
  1600. return data_age;
  1601. }
  1602. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  1603. * order. */
  1604. static int
  1605. circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
  1606. {
  1607. const circuit_t *a = *a_;
  1608. const circuit_t *b = *b_;
  1609. uint32_t age_a = a->age_tmp;
  1610. uint32_t age_b = b->age_tmp;
  1611. if (age_a < age_b)
  1612. return 1;
  1613. else if (age_a == age_b)
  1614. return 0;
  1615. else
  1616. return -1;
  1617. }
  1618. #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
  1619. /** We're out of memory for cells, having allocated <b>current_allocation</b>
  1620. * bytes' worth. Kill the 'worst' circuits until we're under
  1621. * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
  1622. void
  1623. circuits_handle_oom(size_t current_allocation)
  1624. {
  1625. /* Let's hope there's enough slack space for this allocation here... */
  1626. smartlist_t *circlist = smartlist_new();
  1627. circuit_t *circ;
  1628. size_t mem_to_recover;
  1629. size_t mem_recovered=0;
  1630. int n_circuits_killed=0;
  1631. struct timeval now;
  1632. uint32_t now_ms;
  1633. log_notice(LD_GENERAL, "We're low on memory. Killing circuits with "
  1634. "over-long queues. (This behavior is controlled by "
  1635. "MaxMemInQueues.)");
  1636. {
  1637. const size_t recovered = buf_shrink_freelists(1);
  1638. if (recovered >= current_allocation) {
  1639. log_warn(LD_BUG, "We somehow recovered more memory from freelists "
  1640. "than we thought we had allocated");
  1641. current_allocation = 0;
  1642. } else {
  1643. current_allocation -= recovered;
  1644. }
  1645. }
  1646. {
  1647. size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
  1648. FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
  1649. if (current_allocation <= mem_target)
  1650. return;
  1651. mem_to_recover = current_allocation - mem_target;
  1652. }
  1653. tor_gettimeofday_cached_monotonic(&now);
  1654. now_ms = (uint32_t)tv_to_msec(&now);
  1655. /* This algorithm itself assumes that you've got enough memory slack
  1656. * to actually run it. */
  1657. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1658. circ->age_tmp = circuit_max_queued_item_age(circ, now_ms);
  1659. smartlist_add(circlist, circ);
  1660. }
  1661. /* This is O(n log n); there are faster algorithms we could use instead.
  1662. * Let's hope this doesn't happen enough to be in the critical path. */
  1663. smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
  1664. /* Okay, now the worst circuits are at the front of the list. Let's mark
  1665. * them, and reclaim their storage aggressively. */
  1666. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  1667. size_t n = n_cells_in_circ_queues(circ);
  1668. size_t freed;
  1669. if (! circ->marked_for_close) {
  1670. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  1671. }
  1672. marked_circuit_free_cells(circ);
  1673. freed = marked_circuit_free_stream_bytes(circ);
  1674. ++n_circuits_killed;
  1675. mem_recovered += n * packed_cell_mem_cost();
  1676. mem_recovered += freed;
  1677. if (mem_recovered >= mem_to_recover)
  1678. break;
  1679. } SMARTLIST_FOREACH_END(circ);
  1680. clean_cell_pool(); /* In case this helps. */
  1681. buf_shrink_freelists(1); /* This is necessary to actually release buffer
  1682. chunks. */
  1683. log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits; "
  1684. "%d circuits remain alive.",
  1685. U64_PRINTF_ARG(mem_recovered),
  1686. n_circuits_killed,
  1687. smartlist_len(circlist) - n_circuits_killed);
  1688. smartlist_free(circlist);
  1689. }
  1690. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1691. * correct. Trigger an assert if anything is invalid.
  1692. */
  1693. void
  1694. assert_cpath_layer_ok(const crypt_path_t *cp)
  1695. {
  1696. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1697. // tor_assert(cp->port);
  1698. tor_assert(cp);
  1699. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1700. switch (cp->state)
  1701. {
  1702. case CPATH_STATE_OPEN:
  1703. tor_assert(cp->f_crypto);
  1704. tor_assert(cp->b_crypto);
  1705. /* fall through */
  1706. case CPATH_STATE_CLOSED:
  1707. /*XXXX Assert that there's no handshake_state either. */
  1708. tor_assert(!cp->rend_dh_handshake_state);
  1709. break;
  1710. case CPATH_STATE_AWAITING_KEYS:
  1711. /* tor_assert(cp->dh_handshake_state); */
  1712. break;
  1713. default:
  1714. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1715. tor_assert(0);
  1716. }
  1717. tor_assert(cp->package_window >= 0);
  1718. tor_assert(cp->deliver_window >= 0);
  1719. }
  1720. /** Verify that cpath <b>cp</b> has all of its invariants
  1721. * correct. Trigger an assert if anything is invalid.
  1722. */
  1723. static void
  1724. assert_cpath_ok(const crypt_path_t *cp)
  1725. {
  1726. const crypt_path_t *start = cp;
  1727. do {
  1728. assert_cpath_layer_ok(cp);
  1729. /* layers must be in sequence of: "open* awaiting? closed*" */
  1730. if (cp != start) {
  1731. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1732. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1733. } else if (cp->state == CPATH_STATE_OPEN) {
  1734. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1735. }
  1736. }
  1737. cp = cp->next;
  1738. tor_assert(cp);
  1739. } while (cp != start);
  1740. }
  1741. /** Verify that circuit <b>c</b> has all of its invariants
  1742. * correct. Trigger an assert if anything is invalid.
  1743. */
  1744. void
  1745. assert_circuit_ok(const circuit_t *c)
  1746. {
  1747. edge_connection_t *conn;
  1748. const or_circuit_t *or_circ = NULL;
  1749. const origin_circuit_t *origin_circ = NULL;
  1750. tor_assert(c);
  1751. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1752. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  1753. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  1754. {
  1755. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1756. * never understand. -NM. */
  1757. circuit_t *nonconst_circ = (circuit_t*) c;
  1758. if (CIRCUIT_IS_ORIGIN(c))
  1759. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1760. else
  1761. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1762. }
  1763. if (c->n_chan) {
  1764. tor_assert(!c->n_hop);
  1765. if (c->n_circ_id) {
  1766. /* We use the _impl variant here to make sure we don't fail on marked
  1767. * circuits, which would not be returned by the regular function. */
  1768. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  1769. c->n_chan, NULL);
  1770. tor_assert(c == c2);
  1771. }
  1772. }
  1773. if (or_circ && or_circ->p_chan) {
  1774. if (or_circ->p_circ_id) {
  1775. /* ibid */
  1776. circuit_t *c2 =
  1777. circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  1778. or_circ->p_chan, NULL);
  1779. tor_assert(c == c2);
  1780. }
  1781. }
  1782. if (or_circ)
  1783. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1784. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  1785. tor_assert(c->deliver_window >= 0);
  1786. tor_assert(c->package_window >= 0);
  1787. if (c->state == CIRCUIT_STATE_OPEN) {
  1788. tor_assert(!c->n_chan_create_cell);
  1789. if (or_circ) {
  1790. tor_assert(or_circ->n_crypto);
  1791. tor_assert(or_circ->p_crypto);
  1792. tor_assert(or_circ->n_digest);
  1793. tor_assert(or_circ->p_digest);
  1794. }
  1795. }
  1796. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  1797. tor_assert(circuits_pending_chans &&
  1798. smartlist_contains(circuits_pending_chans, c));
  1799. } else {
  1800. tor_assert(!circuits_pending_chans ||
  1801. !smartlist_contains(circuits_pending_chans, c));
  1802. }
  1803. if (origin_circ && origin_circ->cpath) {
  1804. assert_cpath_ok(origin_circ->cpath);
  1805. }
  1806. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1807. tor_assert(or_circ);
  1808. if (!c->marked_for_close) {
  1809. tor_assert(or_circ->rend_splice);
  1810. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1811. }
  1812. tor_assert(or_circ->rend_splice != or_circ);
  1813. } else {
  1814. tor_assert(!or_circ || !or_circ->rend_splice);
  1815. }
  1816. }