circuitlist.c 49 KB

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