circuitlist.c 59 KB

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