circuitlist.c 59 KB

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