circuitlist.c 56 KB

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