circuitlist.c 52 KB

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