circuitlist.c 50 KB

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