circuitlist.c 58 KB

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