circuitlist.c 64 KB

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