circuitlist.c 54 KB

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