circuitlist.c 52 KB

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