circuitlist.c 49 KB

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