circuitlist.c 59 KB

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