circuitlist.c 50 KB

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