circuitlist.c 51 KB

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