circuitlist.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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. }
  1422. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1423. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1424. edge_connection_t *conn;
  1425. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1426. connection_edge_destroy(or_circ->p_circ_id, conn);
  1427. or_circ->n_streams = NULL;
  1428. while (or_circ->resolving_streams) {
  1429. conn = or_circ->resolving_streams;
  1430. or_circ->resolving_streams = conn->next_stream;
  1431. if (!conn->base_.marked_for_close) {
  1432. /* The client will see a DESTROY, and infer that the connections
  1433. * are closing because the circuit is getting torn down. No need
  1434. * to send an end cell. */
  1435. conn->edge_has_sent_end = 1;
  1436. conn->end_reason = END_STREAM_REASON_DESTROY;
  1437. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1438. connection_mark_for_close(TO_CONN(conn));
  1439. }
  1440. conn->on_circuit = NULL;
  1441. }
  1442. if (or_circ->p_chan) {
  1443. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1444. /* Only send destroy if the channel isn't closing anyway */
  1445. if (!(or_circ->p_chan->state == CHANNEL_STATE_CLOSING ||
  1446. or_circ->p_chan->state == CHANNEL_STATE_CLOSED ||
  1447. or_circ->p_chan->state == CHANNEL_STATE_ERROR)) {
  1448. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1449. }
  1450. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1451. }
  1452. } else {
  1453. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1454. edge_connection_t *conn;
  1455. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1456. connection_edge_destroy(circ->n_circ_id, conn);
  1457. ocirc->p_streams = NULL;
  1458. }
  1459. circ->marked_for_close = line;
  1460. circ->marked_for_close_file = file;
  1461. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1462. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1463. if (or_circ->rend_splice) {
  1464. if (!or_circ->rend_splice->base_.marked_for_close) {
  1465. /* do this after marking this circuit, to avoid infinite recursion. */
  1466. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1467. }
  1468. or_circ->rend_splice = NULL;
  1469. }
  1470. }
  1471. }
  1472. /** Given a marked circuit <b>circ</b>, aggressively free its cell queues to
  1473. * recover memory. */
  1474. static void
  1475. marked_circuit_free_cells(circuit_t *circ)
  1476. {
  1477. if (!circ->marked_for_close) {
  1478. log_warn(LD_BUG, "Called on non-marked circuit");
  1479. return;
  1480. }
  1481. cell_queue_clear(&circ->n_chan_cells);
  1482. if (! CIRCUIT_IS_ORIGIN(circ))
  1483. cell_queue_clear(& TO_OR_CIRCUIT(circ)->p_chan_cells);
  1484. }
  1485. /** Aggressively free buffer contents on all the buffers of all streams in the
  1486. * list starting at <b>stream</b>. Return the number of bytes recovered. */
  1487. static size_t
  1488. marked_circuit_streams_free_bytes(edge_connection_t *stream)
  1489. {
  1490. size_t result = 0;
  1491. for ( ; stream; stream = stream->next_stream) {
  1492. connection_t *conn = TO_CONN(stream);
  1493. if (conn->inbuf) {
  1494. result += buf_allocation(conn->inbuf);
  1495. buf_clear(conn->inbuf);
  1496. }
  1497. if (conn->outbuf) {
  1498. result += buf_allocation(conn->outbuf);
  1499. buf_clear(conn->outbuf);
  1500. }
  1501. }
  1502. return result;
  1503. }
  1504. /** Aggressively free buffer contents on all the buffers of all streams on
  1505. * circuit <b>c</b>. Return the number of bytes recovered. */
  1506. static size_t
  1507. marked_circuit_free_stream_bytes(circuit_t *c)
  1508. {
  1509. if (CIRCUIT_IS_ORIGIN(c)) {
  1510. return marked_circuit_streams_free_bytes(TO_ORIGIN_CIRCUIT(c)->p_streams);
  1511. } else {
  1512. return marked_circuit_streams_free_bytes(TO_OR_CIRCUIT(c)->n_streams);
  1513. }
  1514. }
  1515. /** Return the number of cells used by the circuit <b>c</b>'s cell queues. */
  1516. STATIC size_t
  1517. n_cells_in_circ_queues(const circuit_t *c)
  1518. {
  1519. size_t n = c->n_chan_cells.n;
  1520. if (! CIRCUIT_IS_ORIGIN(c)) {
  1521. circuit_t *cc = (circuit_t *) c;
  1522. n += TO_OR_CIRCUIT(cc)->p_chan_cells.n;
  1523. }
  1524. return n;
  1525. }
  1526. /**
  1527. * Return the age of the oldest cell queued on <b>c</b>, in milliseconds.
  1528. * Return 0 if there are no cells queued on c. Requires that <b>now</b> be
  1529. * the current time in milliseconds since the epoch, truncated.
  1530. *
  1531. * This function will return incorrect results if the oldest cell queued on
  1532. * the circuit is older than 2**32 msec (about 49 days) old.
  1533. */
  1534. STATIC uint32_t
  1535. circuit_max_queued_cell_age(const circuit_t *c, uint32_t now)
  1536. {
  1537. uint32_t age = 0;
  1538. packed_cell_t *cell;
  1539. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&c->n_chan_cells.head)))
  1540. age = now - cell->inserted_time;
  1541. if (! CIRCUIT_IS_ORIGIN(c)) {
  1542. const or_circuit_t *orcirc = TO_OR_CIRCUIT((circuit_t*)c);
  1543. if (NULL != (cell = TOR_SIMPLEQ_FIRST(&orcirc->p_chan_cells.head))) {
  1544. uint32_t age2 = now - cell->inserted_time;
  1545. if (age2 > age)
  1546. return age2;
  1547. }
  1548. }
  1549. return age;
  1550. }
  1551. /** Return the age in milliseconds of the oldest buffer chunk on any stream in
  1552. * the linked list <b>stream</b>, where age is taken in milliseconds before
  1553. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1554. static uint32_t
  1555. circuit_get_streams_max_data_age(const edge_connection_t *stream, uint32_t now)
  1556. {
  1557. uint32_t age = 0, age2;
  1558. for (; stream; stream = stream->next_stream) {
  1559. const connection_t *conn = TO_CONN(stream);
  1560. if (conn->outbuf) {
  1561. age2 = buf_get_oldest_chunk_timestamp(conn->outbuf, now);
  1562. if (age2 > age)
  1563. age = age2;
  1564. }
  1565. if (conn->inbuf) {
  1566. age2 = buf_get_oldest_chunk_timestamp(conn->inbuf, now);
  1567. if (age2 > age)
  1568. age = age2;
  1569. }
  1570. }
  1571. return age;
  1572. }
  1573. /** Return the age in milliseconds of the oldest buffer chunk on any stream
  1574. * attached to the circuit <b>c</b>, where age is taken in milliseconds before
  1575. * the time <b>now</b> (in truncated milliseconds since the epoch). */
  1576. STATIC uint32_t
  1577. circuit_max_queued_data_age(const circuit_t *c, uint32_t now)
  1578. {
  1579. if (CIRCUIT_IS_ORIGIN(c)) {
  1580. return circuit_get_streams_max_data_age(
  1581. TO_ORIGIN_CIRCUIT((circuit_t*)c)->p_streams, now);
  1582. } else {
  1583. return circuit_get_streams_max_data_age(
  1584. TO_OR_CIRCUIT((circuit_t*)c)->n_streams, now);
  1585. }
  1586. }
  1587. /** Return the age of the oldest cell or stream buffer chunk on the circuit
  1588. * <b>c</b>, where age is taken in milliseconds before the time <b>now</b> (in
  1589. * truncated milliseconds since the epoch). */
  1590. STATIC uint32_t
  1591. circuit_max_queued_item_age(const circuit_t *c, uint32_t now)
  1592. {
  1593. uint32_t cell_age = circuit_max_queued_cell_age(c, now);
  1594. uint32_t data_age = circuit_max_queued_data_age(c, now);
  1595. if (cell_age > data_age)
  1596. return cell_age;
  1597. else
  1598. return data_age;
  1599. }
  1600. /** Helper to sort a list of circuit_t by age of oldest item, in descending
  1601. * order. */
  1602. static int
  1603. circuits_compare_by_oldest_queued_item_(const void **a_, const void **b_)
  1604. {
  1605. const circuit_t *a = *a_;
  1606. const circuit_t *b = *b_;
  1607. uint32_t age_a = a->age_tmp;
  1608. uint32_t age_b = b->age_tmp;
  1609. if (age_a < age_b)
  1610. return 1;
  1611. else if (age_a == age_b)
  1612. return 0;
  1613. else
  1614. return -1;
  1615. }
  1616. #define FRACTION_OF_DATA_TO_RETAIN_ON_OOM 0.90
  1617. /** We're out of memory for cells, having allocated <b>current_allocation</b>
  1618. * bytes' worth. Kill the 'worst' circuits until we're under
  1619. * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
  1620. void
  1621. circuits_handle_oom(size_t current_allocation)
  1622. {
  1623. /* Let's hope there's enough slack space for this allocation here... */
  1624. smartlist_t *circlist = smartlist_new();
  1625. circuit_t *circ;
  1626. size_t mem_to_recover;
  1627. size_t mem_recovered=0;
  1628. int n_circuits_killed=0;
  1629. struct timeval now;
  1630. uint32_t now_ms;
  1631. log_notice(LD_GENERAL, "We're low on memory. Killing circuits with "
  1632. "over-long queues. (This behavior is controlled by "
  1633. "MaxMemInQueues.)");
  1634. {
  1635. const size_t recovered = buf_shrink_freelists(1);
  1636. if (recovered >= current_allocation) {
  1637. log_warn(LD_BUG, "We somehow recovered more memory from freelists "
  1638. "than we thought we had allocated");
  1639. current_allocation = 0;
  1640. } else {
  1641. current_allocation -= recovered;
  1642. }
  1643. }
  1644. {
  1645. size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
  1646. FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
  1647. if (current_allocation <= mem_target)
  1648. return;
  1649. mem_to_recover = current_allocation - mem_target;
  1650. }
  1651. tor_gettimeofday_cached_monotonic(&now);
  1652. now_ms = (uint32_t)tv_to_msec(&now);
  1653. /* This algorithm itself assumes that you've got enough memory slack
  1654. * to actually run it. */
  1655. TOR_LIST_FOREACH(circ, &global_circuitlist, head) {
  1656. circ->age_tmp = circuit_max_queued_item_age(circ, now_ms);
  1657. smartlist_add(circlist, circ);
  1658. }
  1659. /* This is O(n log n); there are faster algorithms we could use instead.
  1660. * Let's hope this doesn't happen enough to be in the critical path. */
  1661. smartlist_sort(circlist, circuits_compare_by_oldest_queued_item_);
  1662. /* Okay, now the worst circuits are at the front of the list. Let's mark
  1663. * them, and reclaim their storage aggressively. */
  1664. SMARTLIST_FOREACH_BEGIN(circlist, circuit_t *, circ) {
  1665. size_t n = n_cells_in_circ_queues(circ);
  1666. size_t freed;
  1667. if (! circ->marked_for_close) {
  1668. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  1669. }
  1670. marked_circuit_free_cells(circ);
  1671. freed = marked_circuit_free_stream_bytes(circ);
  1672. ++n_circuits_killed;
  1673. mem_recovered += n * packed_cell_mem_cost();
  1674. mem_recovered += freed;
  1675. if (mem_recovered >= mem_to_recover)
  1676. break;
  1677. } SMARTLIST_FOREACH_END(circ);
  1678. clean_cell_pool(); /* In case this helps. */
  1679. buf_shrink_freelists(1); /* This is necessary to actually release buffer
  1680. chunks. */
  1681. log_notice(LD_GENERAL, "Removed "U64_FORMAT" bytes by killing %d circuits; "
  1682. "%d circuits remain alive.",
  1683. U64_PRINTF_ARG(mem_recovered),
  1684. n_circuits_killed,
  1685. smartlist_len(circlist) - n_circuits_killed);
  1686. smartlist_free(circlist);
  1687. }
  1688. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1689. * correct. Trigger an assert if anything is invalid.
  1690. */
  1691. void
  1692. assert_cpath_layer_ok(const crypt_path_t *cp)
  1693. {
  1694. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1695. // tor_assert(cp->port);
  1696. tor_assert(cp);
  1697. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1698. switch (cp->state)
  1699. {
  1700. case CPATH_STATE_OPEN:
  1701. tor_assert(cp->f_crypto);
  1702. tor_assert(cp->b_crypto);
  1703. /* fall through */
  1704. case CPATH_STATE_CLOSED:
  1705. /*XXXX Assert that there's no handshake_state either. */
  1706. tor_assert(!cp->rend_dh_handshake_state);
  1707. break;
  1708. case CPATH_STATE_AWAITING_KEYS:
  1709. /* tor_assert(cp->dh_handshake_state); */
  1710. break;
  1711. default:
  1712. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1713. tor_assert(0);
  1714. }
  1715. tor_assert(cp->package_window >= 0);
  1716. tor_assert(cp->deliver_window >= 0);
  1717. }
  1718. /** Verify that cpath <b>cp</b> has all of its invariants
  1719. * correct. Trigger an assert if anything is invalid.
  1720. */
  1721. static void
  1722. assert_cpath_ok(const crypt_path_t *cp)
  1723. {
  1724. const crypt_path_t *start = cp;
  1725. do {
  1726. assert_cpath_layer_ok(cp);
  1727. /* layers must be in sequence of: "open* awaiting? closed*" */
  1728. if (cp != start) {
  1729. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1730. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1731. } else if (cp->state == CPATH_STATE_OPEN) {
  1732. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1733. }
  1734. }
  1735. cp = cp->next;
  1736. tor_assert(cp);
  1737. } while (cp != start);
  1738. }
  1739. /** Verify that circuit <b>c</b> has all of its invariants
  1740. * correct. Trigger an assert if anything is invalid.
  1741. */
  1742. void
  1743. assert_circuit_ok(const circuit_t *c)
  1744. {
  1745. edge_connection_t *conn;
  1746. const or_circuit_t *or_circ = NULL;
  1747. const origin_circuit_t *origin_circ = NULL;
  1748. tor_assert(c);
  1749. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1750. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  1751. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  1752. {
  1753. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1754. * never understand. -NM. */
  1755. circuit_t *nonconst_circ = (circuit_t*) c;
  1756. if (CIRCUIT_IS_ORIGIN(c))
  1757. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1758. else
  1759. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1760. }
  1761. if (c->n_chan) {
  1762. tor_assert(!c->n_hop);
  1763. if (c->n_circ_id) {
  1764. /* We use the _impl variant here to make sure we don't fail on marked
  1765. * circuits, which would not be returned by the regular function. */
  1766. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  1767. c->n_chan, NULL);
  1768. tor_assert(c == c2);
  1769. }
  1770. }
  1771. if (or_circ && or_circ->p_chan) {
  1772. if (or_circ->p_circ_id) {
  1773. /* ibid */
  1774. circuit_t *c2 =
  1775. circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  1776. or_circ->p_chan, NULL);
  1777. tor_assert(c == c2);
  1778. }
  1779. }
  1780. if (or_circ)
  1781. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1782. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  1783. tor_assert(c->deliver_window >= 0);
  1784. tor_assert(c->package_window >= 0);
  1785. if (c->state == CIRCUIT_STATE_OPEN) {
  1786. tor_assert(!c->n_chan_create_cell);
  1787. if (or_circ) {
  1788. tor_assert(or_circ->n_crypto);
  1789. tor_assert(or_circ->p_crypto);
  1790. tor_assert(or_circ->n_digest);
  1791. tor_assert(or_circ->p_digest);
  1792. }
  1793. }
  1794. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  1795. tor_assert(circuits_pending_chans &&
  1796. smartlist_contains(circuits_pending_chans, c));
  1797. } else {
  1798. tor_assert(!circuits_pending_chans ||
  1799. !smartlist_contains(circuits_pending_chans, c));
  1800. }
  1801. if (origin_circ && origin_circ->cpath) {
  1802. assert_cpath_ok(origin_circ->cpath);
  1803. }
  1804. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1805. tor_assert(or_circ);
  1806. if (!c->marked_for_close) {
  1807. tor_assert(or_circ->rend_splice);
  1808. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1809. }
  1810. tor_assert(or_circ->rend_splice != or_circ);
  1811. } else {
  1812. tor_assert(!or_circ || !or_circ->rend_splice);
  1813. }
  1814. }