circuitlist.c 52 KB

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