circuitlist.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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-2012, 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 "connection.h"
  16. #include "config.h"
  17. #include "connection_edge.h"
  18. #include "connection_or.h"
  19. #include "control.h"
  20. #include "networkstatus.h"
  21. #include "nodelist.h"
  22. #include "onion.h"
  23. #include "relay.h"
  24. #include "rendclient.h"
  25. #include "rendcommon.h"
  26. #include "rephist.h"
  27. #include "routerlist.h"
  28. #include "routerset.h"
  29. #include "ht.h"
  30. /********* START VARIABLES **********/
  31. /** A global list of all circuits at this hop. */
  32. circuit_t *global_circuitlist=NULL;
  33. /** A list of all the circuits in CIRCUIT_STATE_CHAN_WAIT. */
  34. static smartlist_t *circuits_pending_chans = NULL;
  35. static void circuit_free(circuit_t *circ);
  36. static void circuit_free_cpath(crypt_path_t *cpath);
  37. static void circuit_free_cpath_node(crypt_path_t *victim);
  38. static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
  39. /********* END VARIABLES ************/
  40. /** A map from channel and circuit ID to circuit. (Lookup performance is
  41. * very important here, since we need to do it every time a cell arrives.) */
  42. typedef struct chan_circid_circuit_map_t {
  43. HT_ENTRY(chan_circid_circuit_map_t) node;
  44. channel_t *chan;
  45. circid_t circ_id;
  46. circuit_t *circuit;
  47. } chan_circid_circuit_map_t;
  48. /** Helper for hash tables: compare the channel and circuit ID for a and
  49. * b, and return less than, equal to, or greater than zero appropriately.
  50. */
  51. static INLINE int
  52. _chan_circid_entries_eq(chan_circid_circuit_map_t *a,
  53. chan_circid_circuit_map_t *b)
  54. {
  55. return a->chan == b->chan && a->circ_id == b->circ_id;
  56. }
  57. /** Helper: return a hash based on circuit ID and the pointer value of
  58. * chan in <b>a</b>. */
  59. static INLINE unsigned int
  60. _chan_circid_entry_hash(chan_circid_circuit_map_t *a)
  61. {
  62. return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->chan);
  63. }
  64. /** Map from [chan,circid] to circuit. */
  65. static HT_HEAD(chan_circid_map, chan_circid_circuit_map_t)
  66. chan_circid_map = HT_INITIALIZER();
  67. HT_PROTOTYPE(chan_circid_map, chan_circid_circuit_map_t, node,
  68. _chan_circid_entry_hash, _chan_circid_entries_eq)
  69. HT_GENERATE(chan_circid_map, chan_circid_circuit_map_t, node,
  70. _chan_circid_entry_hash, _chan_circid_entries_eq, 0.6,
  71. malloc, realloc, free)
  72. /** The most recently returned entry from circuit_get_by_circid_chan;
  73. * used to improve performance when many cells arrive in a row from the
  74. * same circuit.
  75. */
  76. chan_circid_circuit_map_t *_last_circid_chan_ent = NULL;
  77. /** Implementation helper for circuit_set_{p,n}_circid_channel: A circuit ID
  78. * and/or channel for circ has just changed from <b>old_chan, old_id</b>
  79. * to <b>chan, id</b>. Adjust the chan,circid map as appropriate, removing
  80. * the old entry (if any) and adding a new one. */
  81. static void
  82. circuit_set_circid_chan_helper(circuit_t *circ, int direction,
  83. circid_t id,
  84. channel_t *chan)
  85. {
  86. chan_circid_circuit_map_t search;
  87. chan_circid_circuit_map_t *found;
  88. channel_t *old_chan, **chan_ptr;
  89. circid_t old_id, *circid_ptr;
  90. int was_active, make_active, attached = 0;
  91. if (direction == CELL_DIRECTION_OUT) {
  92. chan_ptr = &circ->n_chan;
  93. circid_ptr = &circ->n_circ_id;
  94. was_active = circ->next_active_on_n_chan != NULL;
  95. make_active = circ->n_chan_cells.n > 0;
  96. } else {
  97. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  98. chan_ptr = &c->p_chan;
  99. circid_ptr = &c->p_circ_id;
  100. was_active = c->next_active_on_p_chan != NULL;
  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_onionskin);
  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. default:
  371. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  372. return buf;
  373. }
  374. }
  375. /** Return a string specifying the state of the hidden-service circuit
  376. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  377. * hidden-service-related circuit purpose. */
  378. const char *
  379. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  380. {
  381. switch (purpose)
  382. {
  383. default:
  384. log_fn(LOG_WARN, LD_BUG,
  385. "Unrecognized circuit purpose: %d",
  386. (int)purpose);
  387. tor_fragile_assert();
  388. /* fall through */
  389. case CIRCUIT_PURPOSE_OR:
  390. case CIRCUIT_PURPOSE_C_GENERAL:
  391. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  392. case CIRCUIT_PURPOSE_TESTING:
  393. case CIRCUIT_PURPOSE_CONTROLLER:
  394. return NULL;
  395. case CIRCUIT_PURPOSE_INTRO_POINT:
  396. return "OR_HSSI_ESTABLISHED";
  397. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  398. return "OR_HSCR_ESTABLISHED";
  399. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  400. return "OR_HS_R_JOINED";
  401. case CIRCUIT_PURPOSE_C_INTRODUCING:
  402. return "HSCI_CONNECTING";
  403. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  404. return "HSCI_INTRO_SENT";
  405. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  406. return "HSCI_DONE";
  407. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  408. return "HSCR_CONNECTING";
  409. case CIRCUIT_PURPOSE_C_REND_READY:
  410. return "HSCR_ESTABLISHED_IDLE";
  411. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  412. return "HSCR_ESTABLISHED_WAITING";
  413. case CIRCUIT_PURPOSE_C_REND_JOINED:
  414. return "HSCR_JOINED";
  415. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  416. return "HSSI_CONNECTING";
  417. case CIRCUIT_PURPOSE_S_INTRO:
  418. return "HSSI_ESTABLISHED";
  419. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  420. return "HSSR_CONNECTING";
  421. case CIRCUIT_PURPOSE_S_REND_JOINED:
  422. return "HSSR_JOINED";
  423. }
  424. }
  425. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  426. const char *
  427. circuit_purpose_to_string(uint8_t purpose)
  428. {
  429. static char buf[32];
  430. switch (purpose)
  431. {
  432. case CIRCUIT_PURPOSE_OR:
  433. return "Circuit at relay";
  434. case CIRCUIT_PURPOSE_INTRO_POINT:
  435. return "Acting as intro point";
  436. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  437. return "Acting as rendevous (pending)";
  438. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  439. return "Acting as rendevous (established)";
  440. case CIRCUIT_PURPOSE_C_GENERAL:
  441. return "General-purpose client";
  442. case CIRCUIT_PURPOSE_C_INTRODUCING:
  443. return "Hidden service client: Connecting to intro point";
  444. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  445. return "Hidden service client: Waiting for ack from intro point";
  446. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  447. return "Hidden service client: Received ack from intro point";
  448. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  449. return "Hidden service client: Establishing rendezvous point";
  450. case CIRCUIT_PURPOSE_C_REND_READY:
  451. return "Hidden service client: Pending rendezvous point";
  452. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  453. return "Hidden service client: Pending rendezvous point (ack received)";
  454. case CIRCUIT_PURPOSE_C_REND_JOINED:
  455. return "Hidden service client: Active rendezvous point";
  456. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  457. return "Measuring circuit timeout";
  458. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  459. return "Hidden service: Establishing introduction point";
  460. case CIRCUIT_PURPOSE_S_INTRO:
  461. return "Hidden service: Introduction point";
  462. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  463. return "Hidden service: Connecting to rendezvous point";
  464. case CIRCUIT_PURPOSE_S_REND_JOINED:
  465. return "Hidden service: Active rendezvous point";
  466. case CIRCUIT_PURPOSE_TESTING:
  467. return "Testing circuit";
  468. case CIRCUIT_PURPOSE_CONTROLLER:
  469. return "Circuit made by controller";
  470. default:
  471. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  472. return buf;
  473. }
  474. }
  475. /** Pick a reasonable package_window to start out for our circuits.
  476. * Originally this was hard-coded at 1000, but now the consensus votes
  477. * on the answer. See proposal 168. */
  478. int32_t
  479. circuit_initial_package_window(void)
  480. {
  481. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  482. CIRCWINDOW_START_MIN,
  483. CIRCWINDOW_START_MAX);
  484. /* If the consensus tells us a negative number, we'd assert. */
  485. if (num < 0)
  486. num = CIRCWINDOW_START;
  487. return num;
  488. }
  489. /** Initialize the common elements in a circuit_t, and add it to the global
  490. * list. */
  491. static void
  492. init_circuit_base(circuit_t *circ)
  493. {
  494. tor_gettimeofday(&circ->timestamp_created);
  495. circ->package_window = circuit_initial_package_window();
  496. circ->deliver_window = CIRCWINDOW_START;
  497. circuit_add(circ);
  498. }
  499. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  500. * and <b>p_conn</b>. Add it to the global circuit list.
  501. */
  502. origin_circuit_t *
  503. origin_circuit_new(void)
  504. {
  505. origin_circuit_t *circ;
  506. /* never zero, since a global ID of 0 is treated specially by the
  507. * controller */
  508. static uint32_t n_circuits_allocated = 1;
  509. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  510. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  511. circ->next_stream_id = crypto_rand_int(1<<16);
  512. circ->global_identifier = n_circuits_allocated++;
  513. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  514. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  515. init_circuit_base(TO_CIRCUIT(circ));
  516. circ_times.last_circ_at = approx_time();
  517. return circ;
  518. }
  519. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  520. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  521. or_circuit_t *
  522. or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
  523. {
  524. /* CircIDs */
  525. or_circuit_t *circ;
  526. circ = tor_malloc_zero(sizeof(or_circuit_t));
  527. circ->_base.magic = OR_CIRCUIT_MAGIC;
  528. if (p_chan)
  529. circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
  530. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  531. init_circuit_base(TO_CIRCUIT(circ));
  532. return circ;
  533. }
  534. /** Deallocate space associated with circ.
  535. */
  536. static void
  537. circuit_free(circuit_t *circ)
  538. {
  539. void *mem;
  540. size_t memlen;
  541. if (!circ)
  542. return;
  543. if (CIRCUIT_IS_ORIGIN(circ)) {
  544. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  545. mem = ocirc;
  546. memlen = sizeof(origin_circuit_t);
  547. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  548. if (ocirc->build_state) {
  549. extend_info_free(ocirc->build_state->chosen_exit);
  550. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  551. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  552. }
  553. tor_free(ocirc->build_state);
  554. circuit_free_cpath(ocirc->cpath);
  555. crypto_pk_free(ocirc->intro_key);
  556. rend_data_free(ocirc->rend_data);
  557. tor_free(ocirc->dest_address);
  558. if (ocirc->socks_username) {
  559. memset(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  560. tor_free(ocirc->socks_username);
  561. }
  562. if (ocirc->socks_password) {
  563. memset(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  564. tor_free(ocirc->socks_password);
  565. }
  566. } else {
  567. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  568. /* Remember cell statistics for this circuit before deallocating. */
  569. if (get_options()->CellStatistics)
  570. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  571. mem = ocirc;
  572. memlen = sizeof(or_circuit_t);
  573. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  574. crypto_cipher_free(ocirc->p_crypto);
  575. crypto_digest_free(ocirc->p_digest);
  576. crypto_cipher_free(ocirc->n_crypto);
  577. crypto_digest_free(ocirc->n_digest);
  578. if (ocirc->rend_splice) {
  579. or_circuit_t *other = ocirc->rend_splice;
  580. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  581. other->rend_splice = NULL;
  582. }
  583. /* remove from map. */
  584. circuit_set_p_circid_chan(ocirc, 0, NULL);
  585. /* Clear cell queue _after_ removing it from the map. Otherwise our
  586. * "active" checks will be violated. */
  587. cell_queue_clear(&ocirc->p_chan_cells);
  588. }
  589. extend_info_free(circ->n_hop);
  590. tor_free(circ->n_chan_onionskin);
  591. /* Remove from map. */
  592. circuit_set_n_circid_chan(circ, 0, NULL);
  593. /* Clear cell queue _after_ removing it from the map. Otherwise our
  594. * "active" checks will be violated. */
  595. cell_queue_clear(&circ->n_chan_cells);
  596. memset(mem, 0xAA, memlen); /* poison memory */
  597. tor_free(mem);
  598. }
  599. /** Deallocate space associated with the linked list <b>cpath</b>. */
  600. static void
  601. circuit_free_cpath(crypt_path_t *cpath)
  602. {
  603. crypt_path_t *victim, *head=cpath;
  604. if (!cpath)
  605. return;
  606. /* it's a doubly linked list, so we have to notice when we've
  607. * gone through it once. */
  608. while (cpath->next && cpath->next != head) {
  609. victim = cpath;
  610. cpath = victim->next;
  611. circuit_free_cpath_node(victim);
  612. }
  613. circuit_free_cpath_node(cpath);
  614. }
  615. /** Release all storage held by circuits. */
  616. void
  617. circuit_free_all(void)
  618. {
  619. circuit_t *next;
  620. while (global_circuitlist) {
  621. next = global_circuitlist->next;
  622. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  623. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  624. while (or_circ->resolving_streams) {
  625. edge_connection_t *next_conn;
  626. next_conn = or_circ->resolving_streams->next_stream;
  627. connection_free(TO_CONN(or_circ->resolving_streams));
  628. or_circ->resolving_streams = next_conn;
  629. }
  630. }
  631. circuit_free(global_circuitlist);
  632. global_circuitlist = next;
  633. }
  634. smartlist_free(circuits_pending_chans);
  635. circuits_pending_chans = NULL;
  636. HT_CLEAR(chan_circid_map, &chan_circid_map);
  637. }
  638. /** Deallocate space associated with the cpath node <b>victim</b>. */
  639. static void
  640. circuit_free_cpath_node(crypt_path_t *victim)
  641. {
  642. if (!victim)
  643. return;
  644. crypto_cipher_free(victim->f_crypto);
  645. crypto_cipher_free(victim->b_crypto);
  646. crypto_digest_free(victim->f_digest);
  647. crypto_digest_free(victim->b_digest);
  648. crypto_dh_free(victim->dh_handshake_state);
  649. extend_info_free(victim->extend_info);
  650. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  651. tor_free(victim);
  652. }
  653. /** Release a crypt_path_reference_t*, which may be NULL. */
  654. static void
  655. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  656. {
  657. if (cpath_ref != NULL) {
  658. if (--(cpath_ref->refcount) == 0) {
  659. circuit_free_cpath_node(cpath_ref->cpath);
  660. tor_free(cpath_ref);
  661. }
  662. }
  663. }
  664. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  665. * of information about circuit <b>circ</b>.
  666. */
  667. static void
  668. circuit_dump_conn_details(int severity,
  669. circuit_t *circ,
  670. int conn_array_index,
  671. const char *type,
  672. int this_circid,
  673. int other_circid)
  674. {
  675. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  676. "state %d (%s), born %ld:",
  677. conn_array_index, type, this_circid, other_circid, circ->state,
  678. circuit_state_to_string(circ->state),
  679. (long)circ->timestamp_created.tv_sec);
  680. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  681. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  682. }
  683. }
  684. /** Log, at severity <b>severity</b>, information about each circuit
  685. * that is connected to <b>conn</b>.
  686. */
  687. void
  688. circuit_dump_by_conn(connection_t *conn, int severity)
  689. {
  690. circuit_t *circ;
  691. edge_connection_t *tmpconn;
  692. for (circ = global_circuitlist; circ; circ = circ->next) {
  693. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  694. if (circ->marked_for_close) {
  695. continue;
  696. }
  697. if (!CIRCUIT_IS_ORIGIN(circ)) {
  698. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  699. }
  700. if (CIRCUIT_IS_ORIGIN(circ)) {
  701. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  702. tmpconn=tmpconn->next_stream) {
  703. if (TO_CONN(tmpconn) == conn) {
  704. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  705. "App-ward", p_circ_id, n_circ_id);
  706. }
  707. }
  708. }
  709. if (! CIRCUIT_IS_ORIGIN(circ)) {
  710. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  711. tmpconn=tmpconn->next_stream) {
  712. if (TO_CONN(tmpconn) == conn) {
  713. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  714. "Exit-ward", n_circ_id, p_circ_id);
  715. }
  716. }
  717. }
  718. }
  719. }
  720. /** A helper function for circuit_dump_by_chan() below. Log a bunch
  721. * of information about circuit <b>circ</b>.
  722. */
  723. static void
  724. circuit_dump_chan_details(int severity,
  725. circuit_t *circ,
  726. channel_t *chan,
  727. const char *type,
  728. int this_circid,
  729. int other_circid)
  730. {
  731. log(severity, LD_CIRC, "Conn %p has %s circuit: circID %d (other side %d), "
  732. "state %d (%s), born %ld:",
  733. chan, type, this_circid, other_circid, circ->state,
  734. circuit_state_to_string(circ->state),
  735. (long)circ->timestamp_created.tv_sec);
  736. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  737. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  738. }
  739. }
  740. /** Log, at severity <b>severity</b>, information about each circuit
  741. * that is connected to <b>chan</b>.
  742. */
  743. void
  744. circuit_dump_by_chan(channel_t *chan, int severity)
  745. {
  746. circuit_t *circ;
  747. tor_assert(chan);
  748. for (circ = global_circuitlist; circ; circ = circ->next) {
  749. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  750. if (circ->marked_for_close) {
  751. continue;
  752. }
  753. if (!CIRCUIT_IS_ORIGIN(circ)) {
  754. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  755. }
  756. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_chan &&
  757. TO_OR_CIRCUIT(circ)->p_chan == chan) {
  758. circuit_dump_chan_details(severity, circ, chan, "App-ward",
  759. p_circ_id, n_circ_id);
  760. }
  761. if (circ->n_chan && circ->n_chan == chan) {
  762. circuit_dump_chan_details(severity, circ, chan, "Exit-ward",
  763. n_circ_id, p_circ_id);
  764. }
  765. if (!circ->n_chan && circ->n_hop &&
  766. channel_matches_extend_info(chan, circ->n_hop) &&
  767. tor_memeq(chan->identity_digest,
  768. circ->n_hop->identity_digest, DIGEST_LEN)) {
  769. circuit_dump_chan_details(severity, circ, chan,
  770. (circ->state == CIRCUIT_STATE_OPEN &&
  771. !CIRCUIT_IS_ORIGIN(circ)) ?
  772. "Endpoint" : "Pending",
  773. n_circ_id, p_circ_id);
  774. }
  775. }
  776. }
  777. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  778. * such circuit exists. */
  779. origin_circuit_t *
  780. circuit_get_by_global_id(uint32_t id)
  781. {
  782. circuit_t *circ;
  783. for (circ=global_circuitlist;circ;circ = circ->next) {
  784. if (CIRCUIT_IS_ORIGIN(circ) &&
  785. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  786. if (circ->marked_for_close)
  787. return NULL;
  788. else
  789. return TO_ORIGIN_CIRCUIT(circ);
  790. }
  791. }
  792. return NULL;
  793. }
  794. /** Return a circ such that:
  795. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  796. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  797. * Return NULL if no such circuit exists.
  798. */
  799. static INLINE circuit_t *
  800. circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan)
  801. {
  802. chan_circid_circuit_map_t search;
  803. chan_circid_circuit_map_t *found;
  804. if (_last_circid_chan_ent &&
  805. circ_id == _last_circid_chan_ent->circ_id &&
  806. chan == _last_circid_chan_ent->chan) {
  807. found = _last_circid_chan_ent;
  808. } else {
  809. search.circ_id = circ_id;
  810. search.chan = chan;
  811. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  812. _last_circid_chan_ent = found;
  813. }
  814. if (found && found->circuit) {
  815. log_debug(LD_CIRC,
  816. "circuit_get_by_circid_channel_impl() returning circuit %p for"
  817. " circ_id %d, channel ID " U64_FORMAT " (%p)",
  818. found->circuit, circ_id,
  819. U64_PRINTF_ARG(chan->global_identifier), chan);
  820. return found->circuit;
  821. }
  822. log_debug(LD_CIRC,
  823. "circuit_get_by_circid_channel_impl() found nothing for"
  824. " circ_id %d, channel ID " U64_FORMAT " (%p)",
  825. circ_id,
  826. U64_PRINTF_ARG(chan->global_identifier), chan);
  827. return NULL;
  828. /* The rest of this checks for bugs. Disabled by default. */
  829. /* We comment it out because coverity complains otherwise.
  830. {
  831. circuit_t *circ;
  832. for (circ=global_circuitlist;circ;circ = circ->next) {
  833. if (! CIRCUIT_IS_ORIGIN(circ)) {
  834. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  835. if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
  836. log_warn(LD_BUG,
  837. "circuit matches p_chan, but not in hash table (Bug!)");
  838. return circ;
  839. }
  840. }
  841. if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
  842. log_warn(LD_BUG,
  843. "circuit matches n_chan, but not in hash table (Bug!)");
  844. return circ;
  845. }
  846. }
  847. return NULL;
  848. } */
  849. }
  850. /** Return a circ such that:
  851. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  852. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  853. * - circ is not marked for close.
  854. * Return NULL if no such circuit exists.
  855. */
  856. circuit_t *
  857. circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
  858. {
  859. circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan);
  860. if (!circ || circ->marked_for_close)
  861. return NULL;
  862. else
  863. return circ;
  864. }
  865. /** Return a circ such that:
  866. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  867. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  868. * Return NULL if no such circuit exists.
  869. */
  870. circuit_t *
  871. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  872. channel_t *chan)
  873. {
  874. return circuit_get_by_circid_channel_impl(circ_id, chan);
  875. }
  876. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  877. * circuit, marked or not, on <b>chan</b>. */
  878. int
  879. circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
  880. {
  881. return circuit_get_by_circid_channel_impl(circ_id, chan) != NULL;
  882. }
  883. /** Return the circuit that a given edge connection is using. */
  884. circuit_t *
  885. circuit_get_by_edge_conn(edge_connection_t *conn)
  886. {
  887. circuit_t *circ;
  888. circ = conn->on_circuit;
  889. tor_assert(!circ ||
  890. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  891. : circ->magic == OR_CIRCUIT_MAGIC));
  892. return circ;
  893. }
  894. /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
  895. * circuit from the chan,circid map, and mark it for close if it hasn't
  896. * been marked already.
  897. */
  898. void
  899. circuit_unlink_all_from_channel(channel_t *chan, int reason)
  900. {
  901. circuit_t *circ;
  902. channel_unlink_all_circuits(chan);
  903. for (circ = global_circuitlist; circ; circ = circ->next) {
  904. int mark = 0;
  905. if (circ->n_chan == chan) {
  906. circuit_set_n_circid_chan(circ, 0, NULL);
  907. mark = 1;
  908. }
  909. if (! CIRCUIT_IS_ORIGIN(circ)) {
  910. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  911. if (or_circ->p_chan == chan) {
  912. circuit_set_p_circid_chan(or_circ, 0, NULL);
  913. mark = 1;
  914. }
  915. }
  916. if (mark && !circ->marked_for_close)
  917. circuit_mark_for_close(circ, reason);
  918. }
  919. }
  920. /** Return a circ such that
  921. * - circ-\>rend_data-\>onion_address is equal to
  922. * <b>rend_data</b>-\>onion_address,
  923. * - circ-\>rend_data-\>rend_cookie is equal to
  924. * <b>rend_data</b>-\>rend_cookie, and
  925. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  926. *
  927. * Return NULL if no such circuit exists.
  928. */
  929. origin_circuit_t *
  930. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  931. {
  932. circuit_t *circ;
  933. for (circ = global_circuitlist; circ; circ = circ->next) {
  934. if (!circ->marked_for_close &&
  935. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  936. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  937. if (ocirc->rend_data &&
  938. !rend_cmp_service_ids(rend_data->onion_address,
  939. ocirc->rend_data->onion_address) &&
  940. tor_memeq(ocirc->rend_data->rend_cookie,
  941. rend_data->rend_cookie,
  942. REND_COOKIE_LEN))
  943. return ocirc;
  944. }
  945. }
  946. return NULL;
  947. }
  948. /** Return the first circuit originating here in global_circuitlist after
  949. * <b>start</b> whose purpose is <b>purpose</b>, and where
  950. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  951. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  952. */
  953. origin_circuit_t *
  954. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  955. const char *digest, uint8_t purpose)
  956. {
  957. circuit_t *circ;
  958. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  959. if (start == NULL)
  960. circ = global_circuitlist;
  961. else
  962. circ = TO_CIRCUIT(start)->next;
  963. for ( ; circ; circ = circ->next) {
  964. if (circ->marked_for_close)
  965. continue;
  966. if (circ->purpose != purpose)
  967. continue;
  968. if (!digest)
  969. return TO_ORIGIN_CIRCUIT(circ);
  970. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  971. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  972. digest, DIGEST_LEN))
  973. return TO_ORIGIN_CIRCUIT(circ);
  974. }
  975. return NULL;
  976. }
  977. /** Return the first OR circuit in the global list whose purpose is
  978. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  979. * <b>token</b>. */
  980. static or_circuit_t *
  981. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  982. size_t len)
  983. {
  984. circuit_t *circ;
  985. for (circ = global_circuitlist; circ; circ = circ->next) {
  986. if (! circ->marked_for_close &&
  987. circ->purpose == purpose &&
  988. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  989. return TO_OR_CIRCUIT(circ);
  990. }
  991. return NULL;
  992. }
  993. /** Return the circuit waiting for a rendezvous with the provided cookie.
  994. * Return NULL if no such circuit is found.
  995. */
  996. or_circuit_t *
  997. circuit_get_rendezvous(const char *cookie)
  998. {
  999. return circuit_get_by_rend_token_and_purpose(
  1000. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  1001. cookie, REND_COOKIE_LEN);
  1002. }
  1003. /** Return the circuit waiting for intro cells of the given digest.
  1004. * Return NULL if no such circuit is found.
  1005. */
  1006. or_circuit_t *
  1007. circuit_get_intro_point(const char *digest)
  1008. {
  1009. return circuit_get_by_rend_token_and_purpose(
  1010. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  1011. DIGEST_LEN);
  1012. }
  1013. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1014. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1015. * flags in <b>flags</b>, and if info is defined, does not already use info
  1016. * as any of its hops; or NULL if no circuit fits this description.
  1017. *
  1018. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  1019. * the circuit we want to create, not the purpose of the circuit we want to
  1020. * cannibalize.
  1021. *
  1022. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1023. */
  1024. origin_circuit_t *
  1025. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  1026. int flags)
  1027. {
  1028. circuit_t *_circ;
  1029. origin_circuit_t *best=NULL;
  1030. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1031. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1032. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1033. const or_options_t *options = get_options();
  1034. /* Make sure we're not trying to create a onehop circ by
  1035. * cannibalization. */
  1036. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1037. log_debug(LD_CIRC,
  1038. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1039. "capacity %d, internal %d",
  1040. purpose, need_uptime, need_capacity, internal);
  1041. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  1042. if (CIRCUIT_IS_ORIGIN(_circ) &&
  1043. _circ->state == CIRCUIT_STATE_OPEN &&
  1044. !_circ->marked_for_close &&
  1045. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1046. !_circ->timestamp_dirty) {
  1047. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  1048. if ((!need_uptime || circ->build_state->need_uptime) &&
  1049. (!need_capacity || circ->build_state->need_capacity) &&
  1050. (internal == circ->build_state->is_internal) &&
  1051. circ->remaining_relay_early_cells &&
  1052. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  1053. !circ->build_state->onehop_tunnel &&
  1054. !circ->isolation_values_set) {
  1055. if (info) {
  1056. /* need to make sure we don't duplicate hops */
  1057. crypt_path_t *hop = circ->cpath;
  1058. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1059. do {
  1060. const node_t *ri2;
  1061. if (tor_memeq(hop->extend_info->identity_digest,
  1062. info->identity_digest, DIGEST_LEN))
  1063. goto next;
  1064. if (ri1 &&
  1065. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1066. && nodes_in_same_family(ri1, ri2))
  1067. goto next;
  1068. hop=hop->next;
  1069. } while (hop!=circ->cpath);
  1070. }
  1071. if (options->ExcludeNodes) {
  1072. /* Make sure no existing nodes in the circuit are excluded for
  1073. * general use. (This may be possible if StrictNodes is 0, and we
  1074. * thought we needed to use an otherwise excluded node for, say, a
  1075. * directory operation.) */
  1076. crypt_path_t *hop = circ->cpath;
  1077. do {
  1078. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1079. hop->extend_info))
  1080. goto next;
  1081. hop = hop->next;
  1082. } while (hop != circ->cpath);
  1083. }
  1084. if (!best || (best->build_state->need_uptime && !need_uptime))
  1085. best = circ;
  1086. next: ;
  1087. }
  1088. }
  1089. }
  1090. return best;
  1091. }
  1092. /** Return the number of hops in circuit's path. */
  1093. int
  1094. circuit_get_cpath_len(origin_circuit_t *circ)
  1095. {
  1096. int n = 0;
  1097. if (circ && circ->cpath) {
  1098. crypt_path_t *cpath, *cpath_next = NULL;
  1099. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1100. cpath_next = cpath->next;
  1101. ++n;
  1102. }
  1103. }
  1104. return n;
  1105. }
  1106. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1107. * aren't that many hops in the list. */
  1108. crypt_path_t *
  1109. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1110. {
  1111. if (circ && circ->cpath && hopnum > 0) {
  1112. crypt_path_t *cpath, *cpath_next = NULL;
  1113. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1114. cpath_next = cpath->next;
  1115. if (--hopnum <= 0)
  1116. return cpath;
  1117. }
  1118. }
  1119. return NULL;
  1120. }
  1121. /** Go through the circuitlist; mark-for-close each circuit that starts
  1122. * at us but has not yet been used. */
  1123. void
  1124. circuit_mark_all_unused_circs(void)
  1125. {
  1126. circuit_t *circ;
  1127. for (circ=global_circuitlist; circ; circ = circ->next) {
  1128. if (CIRCUIT_IS_ORIGIN(circ) &&
  1129. !circ->marked_for_close &&
  1130. !circ->timestamp_dirty)
  1131. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1132. }
  1133. }
  1134. /** Go through the circuitlist; for each circuit that starts at us
  1135. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1136. * new streams.
  1137. *
  1138. * This is useful for letting the user change pseudonyms, so new
  1139. * streams will not be linkable to old streams.
  1140. */
  1141. /* XXX024 this is a bad name for what this function does */
  1142. void
  1143. circuit_expire_all_dirty_circs(void)
  1144. {
  1145. circuit_t *circ;
  1146. const or_options_t *options = get_options();
  1147. for (circ=global_circuitlist; circ; circ = circ->next) {
  1148. if (CIRCUIT_IS_ORIGIN(circ) &&
  1149. !circ->marked_for_close &&
  1150. circ->timestamp_dirty)
  1151. /* XXXX024 This is a screwed-up way to say "This is too dirty
  1152. * for new circuits. */
  1153. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  1154. }
  1155. }
  1156. /** Mark <b>circ</b> to be closed next time we call
  1157. * circuit_close_all_marked(). Do any cleanup needed:
  1158. * - If state is onionskin_pending, remove circ from the onion_pending
  1159. * list.
  1160. * - If circ isn't open yet: call circuit_build_failed() if we're
  1161. * the origin, and in either case call circuit_rep_hist_note_result()
  1162. * to note stats.
  1163. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1164. * failure we just had to the hidden service client module.
  1165. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1166. * report to the hidden service client module that the intro point
  1167. * we just tried may be unreachable.
  1168. * - Send appropriate destroys and edge_destroys for conns and
  1169. * streams attached to circ.
  1170. * - If circ->rend_splice is set (we are the midpoint of a joined
  1171. * rendezvous stream), then mark the other circuit to close as well.
  1172. */
  1173. void
  1174. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  1175. const char *file)
  1176. {
  1177. int orig_reason = reason; /* Passed to the controller */
  1178. assert_circuit_ok(circ);
  1179. tor_assert(line);
  1180. tor_assert(file);
  1181. if (circ->marked_for_close) {
  1182. log(LOG_WARN,LD_BUG,
  1183. "Duplicate call to circuit_mark_for_close at %s:%d"
  1184. " (first at %s:%d)", file, line,
  1185. circ->marked_for_close_file, circ->marked_for_close);
  1186. return;
  1187. }
  1188. if (reason == END_CIRC_AT_ORIGIN) {
  1189. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1190. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1191. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1192. file, line, circ->purpose);
  1193. }
  1194. reason = END_CIRC_REASON_NONE;
  1195. }
  1196. if (CIRCUIT_IS_ORIGIN(circ)) {
  1197. /* We don't send reasons when closing circuits at the origin. */
  1198. reason = END_CIRC_REASON_NONE;
  1199. }
  1200. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1201. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1202. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  1203. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1204. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1205. reason = END_CIRC_REASON_NONE;
  1206. }
  1207. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1208. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1209. }
  1210. /* If the circuit ever became OPEN, we sent it to the reputation history
  1211. * module then. If it isn't OPEN, we send it there now to remember which
  1212. * links worked and which didn't.
  1213. */
  1214. if (circ->state != CIRCUIT_STATE_OPEN) {
  1215. if (CIRCUIT_IS_ORIGIN(circ)) {
  1216. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1217. circuit_build_failed(ocirc); /* take actions if necessary */
  1218. circuit_rep_hist_note_result(ocirc);
  1219. }
  1220. }
  1221. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1222. if (circuits_pending_chans)
  1223. smartlist_remove(circuits_pending_chans, circ);
  1224. }
  1225. if (CIRCUIT_IS_ORIGIN(circ)) {
  1226. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1227. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1228. orig_reason);
  1229. }
  1230. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1231. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1232. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1233. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1234. tor_assert(ocirc->build_state->chosen_exit);
  1235. tor_assert(ocirc->rend_data);
  1236. /* treat this like getting a nack from it */
  1237. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1238. safe_str_client(ocirc->rend_data->onion_address),
  1239. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1240. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1241. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1242. ocirc->rend_data,
  1243. timed_out ?
  1244. INTRO_POINT_FAILURE_TIMEOUT :
  1245. INTRO_POINT_FAILURE_GENERIC);
  1246. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1247. reason != END_CIRC_REASON_TIMEOUT) {
  1248. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1249. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1250. log_info(LD_REND, "Failed intro circ %s to %s "
  1251. "(building circuit to intro point). "
  1252. "Marking intro point as possibly unreachable.",
  1253. safe_str_client(ocirc->rend_data->onion_address),
  1254. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1255. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1256. ocirc->rend_data,
  1257. INTRO_POINT_FAILURE_UNREACHABLE);
  1258. }
  1259. }
  1260. if (circ->n_chan) {
  1261. circuit_clear_cell_queue(circ, circ->n_chan);
  1262. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1263. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1264. }
  1265. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1266. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1267. edge_connection_t *conn;
  1268. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1269. connection_edge_destroy(or_circ->p_circ_id, conn);
  1270. or_circ->n_streams = NULL;
  1271. while (or_circ->resolving_streams) {
  1272. conn = or_circ->resolving_streams;
  1273. or_circ->resolving_streams = conn->next_stream;
  1274. if (!conn->_base.marked_for_close) {
  1275. /* The client will see a DESTROY, and infer that the connections
  1276. * are closing because the circuit is getting torn down. No need
  1277. * to send an end cell. */
  1278. conn->edge_has_sent_end = 1;
  1279. conn->end_reason = END_STREAM_REASON_DESTROY;
  1280. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1281. connection_mark_for_close(TO_CONN(conn));
  1282. }
  1283. conn->on_circuit = NULL;
  1284. }
  1285. if (or_circ->p_chan) {
  1286. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1287. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1288. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1289. }
  1290. } else {
  1291. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1292. edge_connection_t *conn;
  1293. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1294. connection_edge_destroy(circ->n_circ_id, conn);
  1295. ocirc->p_streams = NULL;
  1296. }
  1297. circ->marked_for_close = line;
  1298. circ->marked_for_close_file = file;
  1299. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1300. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1301. if (or_circ->rend_splice) {
  1302. if (!or_circ->rend_splice->_base.marked_for_close) {
  1303. /* do this after marking this circuit, to avoid infinite recursion. */
  1304. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1305. }
  1306. or_circ->rend_splice = NULL;
  1307. }
  1308. }
  1309. }
  1310. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1311. * correct. Trigger an assert if anything is invalid.
  1312. */
  1313. void
  1314. assert_cpath_layer_ok(const crypt_path_t *cp)
  1315. {
  1316. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1317. // tor_assert(cp->port);
  1318. tor_assert(cp);
  1319. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1320. switch (cp->state)
  1321. {
  1322. case CPATH_STATE_OPEN:
  1323. tor_assert(cp->f_crypto);
  1324. tor_assert(cp->b_crypto);
  1325. /* fall through */
  1326. case CPATH_STATE_CLOSED:
  1327. tor_assert(!cp->dh_handshake_state);
  1328. break;
  1329. case CPATH_STATE_AWAITING_KEYS:
  1330. /* tor_assert(cp->dh_handshake_state); */
  1331. break;
  1332. default:
  1333. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1334. tor_assert(0);
  1335. }
  1336. tor_assert(cp->package_window >= 0);
  1337. tor_assert(cp->deliver_window >= 0);
  1338. }
  1339. /** Verify that cpath <b>cp</b> has all of its invariants
  1340. * correct. Trigger an assert if anything is invalid.
  1341. */
  1342. static void
  1343. assert_cpath_ok(const crypt_path_t *cp)
  1344. {
  1345. const crypt_path_t *start = cp;
  1346. do {
  1347. assert_cpath_layer_ok(cp);
  1348. /* layers must be in sequence of: "open* awaiting? closed*" */
  1349. if (cp != start) {
  1350. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1351. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1352. } else if (cp->state == CPATH_STATE_OPEN) {
  1353. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1354. }
  1355. }
  1356. cp = cp->next;
  1357. tor_assert(cp);
  1358. } while (cp != start);
  1359. }
  1360. /** Verify that circuit <b>c</b> has all of its invariants
  1361. * correct. Trigger an assert if anything is invalid.
  1362. */
  1363. void
  1364. assert_circuit_ok(const circuit_t *c)
  1365. {
  1366. edge_connection_t *conn;
  1367. const or_circuit_t *or_circ = NULL;
  1368. const origin_circuit_t *origin_circ = NULL;
  1369. tor_assert(c);
  1370. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1371. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1372. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1373. {
  1374. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1375. * never understand. -NM. */
  1376. circuit_t *nonconst_circ = (circuit_t*) c;
  1377. if (CIRCUIT_IS_ORIGIN(c))
  1378. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1379. else
  1380. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1381. }
  1382. if (c->n_chan) {
  1383. tor_assert(!c->n_hop);
  1384. if (c->n_circ_id) {
  1385. /* We use the _impl variant here to make sure we don't fail on marked
  1386. * circuits, which would not be returned by the regular function. */
  1387. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  1388. c->n_chan);
  1389. tor_assert(c == c2);
  1390. }
  1391. }
  1392. if (or_circ && or_circ->p_chan) {
  1393. if (or_circ->p_circ_id) {
  1394. /* ibid */
  1395. circuit_t *c2 = circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  1396. or_circ->p_chan);
  1397. tor_assert(c == c2);
  1398. }
  1399. }
  1400. if (or_circ)
  1401. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1402. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1403. tor_assert(c->deliver_window >= 0);
  1404. tor_assert(c->package_window >= 0);
  1405. if (c->state == CIRCUIT_STATE_OPEN) {
  1406. tor_assert(!c->n_chan_onionskin);
  1407. if (or_circ) {
  1408. tor_assert(or_circ->n_crypto);
  1409. tor_assert(or_circ->p_crypto);
  1410. tor_assert(or_circ->n_digest);
  1411. tor_assert(or_circ->p_digest);
  1412. }
  1413. }
  1414. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  1415. tor_assert(circuits_pending_chans &&
  1416. smartlist_isin(circuits_pending_chans, c));
  1417. } else {
  1418. tor_assert(!circuits_pending_chans ||
  1419. !smartlist_isin(circuits_pending_chans, c));
  1420. }
  1421. if (origin_circ && origin_circ->cpath) {
  1422. assert_cpath_ok(origin_circ->cpath);
  1423. }
  1424. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1425. tor_assert(or_circ);
  1426. if (!c->marked_for_close) {
  1427. tor_assert(or_circ->rend_splice);
  1428. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1429. }
  1430. tor_assert(or_circ->rend_splice != or_circ);
  1431. } else {
  1432. tor_assert(!or_circ || !or_circ->rend_splice);
  1433. }
  1434. }