circuitlist.c 69 KB

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