circuitlist.c 51 KB

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