circuituse.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  1. /* Copyright (c) 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 circuituse.c
  8. * \brief Launch the right sort of circuits and attach streams to them.
  9. **/
  10. #include "or.h"
  11. #include "channel.h"
  12. #include "circuitbuild.h"
  13. #include "circuitlist.h"
  14. #include "circuitstats.h"
  15. #include "circuituse.h"
  16. #include "config.h"
  17. #include "connection.h"
  18. #include "connection_edge.h"
  19. #include "control.h"
  20. #include "entrynodes.h"
  21. #include "nodelist.h"
  22. #include "networkstatus.h"
  23. #include "policies.h"
  24. #include "rendclient.h"
  25. #include "rendcommon.h"
  26. #include "rendservice.h"
  27. #include "rephist.h"
  28. #include "router.h"
  29. #include "routerlist.h"
  30. /********* START VARIABLES **********/
  31. extern circuit_t *global_circuitlist; /* from circuitlist.c */
  32. /********* END VARIABLES ************/
  33. static void circuit_expire_old_circuits_clientside(void);
  34. static void circuit_increment_failure_count(void);
  35. /** Return 1 if <b>circ</b> could be returned by circuit_get_best().
  36. * Else return 0.
  37. */
  38. static int
  39. circuit_is_acceptable(const origin_circuit_t *origin_circ,
  40. const entry_connection_t *conn,
  41. int must_be_open, uint8_t purpose,
  42. int need_uptime, int need_internal,
  43. time_t now)
  44. {
  45. const circuit_t *circ = TO_CIRCUIT(origin_circ);
  46. const node_t *exitnode;
  47. cpath_build_state_t *build_state;
  48. tor_assert(circ);
  49. tor_assert(conn);
  50. tor_assert(conn->socks_request);
  51. if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_chan))
  52. return 0; /* ignore non-open circs */
  53. if (circ->marked_for_close)
  54. return 0;
  55. /* if this circ isn't our purpose, skip. */
  56. if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
  57. if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  58. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  59. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
  60. circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
  61. return 0;
  62. } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
  63. !must_be_open) {
  64. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  65. circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  66. return 0;
  67. } else {
  68. if (purpose != circ->purpose)
  69. return 0;
  70. }
  71. /* If this is a timed-out hidden service circuit, skip it. */
  72. if (origin_circ->hs_circ_has_timed_out) {
  73. return 0;
  74. }
  75. if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  76. purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  77. if (circ->timestamp_dirty &&
  78. circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
  79. return 0;
  80. /* decide if this circ is suitable for this conn */
  81. /* for rend circs, circ->cpath->prev is not the last router in the
  82. * circuit, it's the magical extra bob hop. so just check the nickname
  83. * of the one we meant to finish at.
  84. */
  85. build_state = origin_circ->build_state;
  86. exitnode = build_state_get_exit_node(build_state);
  87. if (need_uptime && !build_state->need_uptime)
  88. return 0;
  89. if (need_internal != build_state->is_internal)
  90. return 0;
  91. if (purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  92. if (!exitnode && !build_state->onehop_tunnel) {
  93. log_debug(LD_CIRC,"Not considering circuit with unknown router.");
  94. return 0; /* this circuit is screwed and doesn't know it yet,
  95. * or is a rendezvous circuit. */
  96. }
  97. if (build_state->onehop_tunnel) {
  98. if (!conn->want_onehop) {
  99. log_debug(LD_CIRC,"Skipping one-hop circuit.");
  100. return 0;
  101. }
  102. tor_assert(conn->chosen_exit_name);
  103. if (build_state->chosen_exit) {
  104. char digest[DIGEST_LEN];
  105. if (hexdigest_to_digest(conn->chosen_exit_name, digest) < 0)
  106. return 0; /* broken digest, we don't want it */
  107. if (tor_memneq(digest, build_state->chosen_exit->identity_digest,
  108. DIGEST_LEN))
  109. return 0; /* this is a circuit to somewhere else */
  110. if (tor_digest_is_zero(digest)) {
  111. /* we don't know the digest; have to compare addr:port */
  112. tor_addr_t addr;
  113. int r = tor_addr_parse(&addr, conn->socks_request->address);
  114. if (r < 0 ||
  115. !tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
  116. build_state->chosen_exit->port != conn->socks_request->port)
  117. return 0;
  118. }
  119. }
  120. } else {
  121. if (conn->want_onehop) {
  122. /* don't use three-hop circuits -- that could hurt our anonymity. */
  123. return 0;
  124. }
  125. }
  126. if (exitnode && !connection_ap_can_use_exit(conn, exitnode)) {
  127. /* can't exit from this router */
  128. return 0;
  129. }
  130. } else { /* not general */
  131. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
  132. if ((edge_conn->rend_data && !origin_circ->rend_data) ||
  133. (!edge_conn->rend_data && origin_circ->rend_data) ||
  134. (edge_conn->rend_data && origin_circ->rend_data &&
  135. rend_cmp_service_ids(edge_conn->rend_data->onion_address,
  136. origin_circ->rend_data->onion_address))) {
  137. /* this circ is not for this conn */
  138. return 0;
  139. }
  140. }
  141. if (!connection_edge_compatible_with_circuit(conn, origin_circ)) {
  142. /* conn needs to be isolated from other conns that have already used
  143. * origin_circ */
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. /** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
  149. * <b>conn</b>, and return 0 otherwise. Used by circuit_get_best.
  150. */
  151. static int
  152. circuit_is_better(const origin_circuit_t *oa, const origin_circuit_t *ob,
  153. const entry_connection_t *conn)
  154. {
  155. const circuit_t *a = TO_CIRCUIT(oa);
  156. const circuit_t *b = TO_CIRCUIT(ob);
  157. const uint8_t purpose = ENTRY_TO_CONN(conn)->purpose;
  158. int a_bits, b_bits;
  159. switch (purpose) {
  160. case CIRCUIT_PURPOSE_C_GENERAL:
  161. /* if it's used but less dirty it's best;
  162. * else if it's more recently created it's best
  163. */
  164. if (b->timestamp_dirty) {
  165. if (a->timestamp_dirty &&
  166. a->timestamp_dirty > b->timestamp_dirty)
  167. return 1;
  168. } else {
  169. if (a->timestamp_dirty ||
  170. timercmp(&a->timestamp_created, &b->timestamp_created, >))
  171. return 1;
  172. if (ob->build_state->is_internal)
  173. /* XXX023 what the heck is this internal thing doing here. I
  174. * think we can get rid of it. circuit_is_acceptable() already
  175. * makes sure that is_internal is exactly what we need it to
  176. * be. -RD */
  177. return 1;
  178. }
  179. break;
  180. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  181. /* the closer it is to ack_wait the better it is */
  182. if (a->purpose > b->purpose)
  183. return 1;
  184. break;
  185. case CIRCUIT_PURPOSE_C_REND_JOINED:
  186. /* the closer it is to rend_joined the better it is */
  187. if (a->purpose > b->purpose)
  188. return 1;
  189. break;
  190. }
  191. /* XXXX023 Maybe this check should get a higher priority to avoid
  192. * using up circuits too rapidly. */
  193. a_bits = connection_edge_update_circuit_isolation(conn,
  194. (origin_circuit_t*)oa, 1);
  195. b_bits = connection_edge_update_circuit_isolation(conn,
  196. (origin_circuit_t*)ob, 1);
  197. /* if x_bits < 0, then we have not used x for anything; better not to dirty
  198. * a connection if we can help it. */
  199. if (a_bits < 0) {
  200. return 0;
  201. } else if (b_bits < 0) {
  202. return 1;
  203. }
  204. a_bits &= ~ oa->isolation_flags_mixed;
  205. a_bits &= ~ ob->isolation_flags_mixed;
  206. if (n_bits_set_u8(a_bits) < n_bits_set_u8(b_bits)) {
  207. /* The fewer new restrictions we need to make on a circuit for stream
  208. * isolation, the better. */
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. /** Find the best circ that conn can use, preferably one which is
  214. * dirty. Circ must not be too old.
  215. *
  216. * Conn must be defined.
  217. *
  218. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  219. *
  220. * circ_purpose specifies what sort of circuit we must have.
  221. * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
  222. *
  223. * If it's REND_JOINED and must_be_open==0, then return the closest
  224. * rendezvous-purposed circuit that you can find.
  225. *
  226. * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
  227. * closest introduce-purposed circuit that you can find.
  228. */
  229. static origin_circuit_t *
  230. circuit_get_best(const entry_connection_t *conn,
  231. int must_be_open, uint8_t purpose,
  232. int need_uptime, int need_internal)
  233. {
  234. circuit_t *circ;
  235. origin_circuit_t *best=NULL;
  236. struct timeval now;
  237. int intro_going_on_but_too_old = 0;
  238. tor_assert(conn);
  239. tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  240. purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
  241. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  242. tor_gettimeofday(&now);
  243. for (circ=global_circuitlist;circ;circ = circ->next) {
  244. origin_circuit_t *origin_circ;
  245. if (!CIRCUIT_IS_ORIGIN(circ))
  246. continue;
  247. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  248. if (!circuit_is_acceptable(origin_circ,conn,must_be_open,purpose,
  249. need_uptime,need_internal,now.tv_sec))
  250. continue;
  251. if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
  252. !must_be_open && circ->state != CIRCUIT_STATE_OPEN &&
  253. tv_mdiff(&now, &circ->timestamp_created) > circ_times.timeout_ms) {
  254. intro_going_on_but_too_old = 1;
  255. continue;
  256. }
  257. /* now this is an acceptable circ to hand back. but that doesn't
  258. * mean it's the *best* circ to hand back. try to decide.
  259. */
  260. if (!best || circuit_is_better(origin_circ,best,conn))
  261. best = origin_circ;
  262. }
  263. if (!best && intro_going_on_but_too_old)
  264. log_info(LD_REND|LD_CIRC, "There is an intro circuit being created "
  265. "right now, but it has already taken quite a while. Starting "
  266. "one in parallel.");
  267. return best;
  268. }
  269. /** Return the number of not-yet-open general-purpose origin circuits. */
  270. static int
  271. count_pending_general_client_circuits(void)
  272. {
  273. const circuit_t *circ;
  274. int count = 0;
  275. for (circ = global_circuitlist; circ; circ = circ->next) {
  276. if (circ->marked_for_close ||
  277. circ->state == CIRCUIT_STATE_OPEN ||
  278. circ->purpose != CIRCUIT_PURPOSE_C_GENERAL ||
  279. !CIRCUIT_IS_ORIGIN(circ))
  280. continue;
  281. ++count;
  282. }
  283. return count;
  284. }
  285. #if 0
  286. /** Check whether, according to the policies in <b>options</b>, the
  287. * circuit <b>circ</b> makes sense. */
  288. /* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
  289. * Also, it doesn't have the right definition of an exit circuit. Also,
  290. * it's never called. */
  291. int
  292. circuit_conforms_to_options(const origin_circuit_t *circ,
  293. const or_options_t *options)
  294. {
  295. const crypt_path_t *cpath, *cpath_next = NULL;
  296. /* first check if it includes any excluded nodes */
  297. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  298. cpath_next = cpath->next;
  299. if (routerset_contains_extendinfo(options->ExcludeNodes,
  300. cpath->extend_info))
  301. return 0;
  302. }
  303. /* then consider the final hop */
  304. if (routerset_contains_extendinfo(options->ExcludeExitNodes,
  305. circ->cpath->prev->extend_info))
  306. return 0;
  307. return 1;
  308. }
  309. #endif
  310. /** Close all circuits that start at us, aren't open, and were born
  311. * at least CircuitBuildTimeout seconds ago.
  312. */
  313. void
  314. circuit_expire_building(void)
  315. {
  316. circuit_t *victim, *next_circ = global_circuitlist;
  317. /* circ_times.timeout_ms and circ_times.close_ms are from
  318. * circuit_build_times_get_initial_timeout() if we haven't computed
  319. * custom timeouts yet */
  320. struct timeval general_cutoff, begindir_cutoff, fourhop_cutoff,
  321. cannibalize_cutoff, close_cutoff, extremely_old_cutoff,
  322. hs_extremely_old_cutoff;
  323. const or_options_t *options = get_options();
  324. struct timeval now;
  325. cpath_build_state_t *build_state;
  326. tor_gettimeofday(&now);
  327. #define SET_CUTOFF(target, msec) do { \
  328. long ms = tor_lround(msec); \
  329. struct timeval diff; \
  330. diff.tv_sec = ms / 1000; \
  331. diff.tv_usec = (int)((ms % 1000) * 1000); \
  332. timersub(&now, &diff, &target); \
  333. } while (0)
  334. SET_CUTOFF(general_cutoff, circ_times.timeout_ms);
  335. SET_CUTOFF(begindir_cutoff, circ_times.timeout_ms);
  336. SET_CUTOFF(fourhop_cutoff, circ_times.timeout_ms * (4/3.0));
  337. SET_CUTOFF(cannibalize_cutoff, circ_times.timeout_ms / 2.0);
  338. SET_CUTOFF(close_cutoff, circ_times.close_ms);
  339. SET_CUTOFF(extremely_old_cutoff, circ_times.close_ms*2 + 1000);
  340. SET_CUTOFF(hs_extremely_old_cutoff,
  341. MAX(circ_times.close_ms*2 + 1000,
  342. options->SocksTimeout * 1000));
  343. while (next_circ) {
  344. struct timeval cutoff;
  345. victim = next_circ;
  346. next_circ = next_circ->next;
  347. if (!CIRCUIT_IS_ORIGIN(victim) || /* didn't originate here */
  348. victim->marked_for_close) /* don't mess with marked circs */
  349. continue;
  350. build_state = TO_ORIGIN_CIRCUIT(victim)->build_state;
  351. if (build_state && build_state->onehop_tunnel)
  352. cutoff = begindir_cutoff;
  353. else if (build_state && build_state->desired_path_len == 4
  354. && !TO_ORIGIN_CIRCUIT(victim)->has_opened)
  355. cutoff = fourhop_cutoff;
  356. else if (TO_ORIGIN_CIRCUIT(victim)->has_opened)
  357. cutoff = cannibalize_cutoff;
  358. else if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
  359. cutoff = close_cutoff;
  360. else
  361. cutoff = general_cutoff;
  362. if (TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)
  363. cutoff = hs_extremely_old_cutoff;
  364. if (timercmp(&victim->timestamp_created, &cutoff, >))
  365. continue; /* it's still young, leave it alone */
  366. #if 0
  367. /* some debug logs, to help track bugs */
  368. if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  369. victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  370. if (!victim->timestamp_dirty)
  371. log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d)."
  372. "(clean).",
  373. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  374. victim->purpose, victim->build_state->chosen_exit_name,
  375. victim->n_circ_id);
  376. else
  377. log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d). "
  378. "%d secs since dirty.",
  379. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  380. victim->purpose, victim->build_state->chosen_exit_name,
  381. victim->n_circ_id,
  382. (int)(now - victim->timestamp_dirty));
  383. }
  384. #endif
  385. /* if circ is !open, or if it's open but purpose is a non-finished
  386. * intro or rend, then mark it for close */
  387. if (victim->state == CIRCUIT_STATE_OPEN) {
  388. switch (victim->purpose) {
  389. default: /* most open circuits can be left alone. */
  390. continue; /* yes, continue inside a switch refers to the nearest
  391. * enclosing loop. C is smart. */
  392. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  393. case CIRCUIT_PURPOSE_C_INTRODUCING:
  394. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  395. break; /* too old, need to die */
  396. case CIRCUIT_PURPOSE_C_REND_READY:
  397. /* it's a rend_ready circ -- has it already picked a query? */
  398. /* c_rend_ready circs measure age since timestamp_dirty,
  399. * because that's set when they switch purposes
  400. */
  401. if (TO_ORIGIN_CIRCUIT(victim)->rend_data ||
  402. victim->timestamp_dirty > cutoff.tv_sec)
  403. continue;
  404. break;
  405. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  406. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  407. /* rend and intro circs become dirty each time they
  408. * make an introduction attempt. so timestamp_dirty
  409. * will reflect the time since the last attempt.
  410. */
  411. if (victim->timestamp_dirty > cutoff.tv_sec)
  412. continue;
  413. break;
  414. }
  415. } else { /* circuit not open, consider recording failure as timeout */
  416. int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath &&
  417. TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_OPEN;
  418. if (TO_ORIGIN_CIRCUIT(victim)->p_streams != NULL) {
  419. log_warn(LD_BUG, "Circuit %d (purpose %d, %s) has timed out, "
  420. "yet has attached streams!",
  421. TO_ORIGIN_CIRCUIT(victim)->global_identifier,
  422. victim->purpose,
  423. circuit_purpose_to_string(victim->purpose));
  424. tor_fragile_assert();
  425. continue;
  426. }
  427. if (circuit_timeout_want_to_count_circ(TO_ORIGIN_CIRCUIT(victim)) &&
  428. circuit_build_times_enough_to_compute(&circ_times)) {
  429. /* Circuits are allowed to last longer for measurement.
  430. * Switch their purpose and wait. */
  431. if (victim->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
  432. control_event_circuit_status(TO_ORIGIN_CIRCUIT(victim),
  433. CIRC_EVENT_FAILED,
  434. END_CIRC_REASON_TIMEOUT);
  435. circuit_change_purpose(victim, CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  436. /* Record this failure to check for too many timeouts
  437. * in a row. This function does not record a time value yet
  438. * (we do that later); it only counts the fact that we did
  439. * have a timeout. */
  440. circuit_build_times_count_timeout(&circ_times,
  441. first_hop_succeeded);
  442. continue;
  443. }
  444. /*
  445. * If the circuit build time is much greater than we would have cut
  446. * it off at, we probably had a suspend event along this codepath,
  447. * and we should discard the value.
  448. */
  449. if (timercmp(&victim->timestamp_created, &extremely_old_cutoff, <)) {
  450. log_notice(LD_CIRC,
  451. "Extremely large value for circuit build timeout: %lds. "
  452. "Assuming clock jump. Purpose %d (%s)",
  453. (long)(now.tv_sec - victim->timestamp_created.tv_sec),
  454. victim->purpose,
  455. circuit_purpose_to_string(victim->purpose));
  456. } else if (circuit_build_times_count_close(&circ_times,
  457. first_hop_succeeded,
  458. victim->timestamp_created.tv_sec)) {
  459. circuit_build_times_set_timeout(&circ_times);
  460. }
  461. }
  462. }
  463. /* If this is a hidden service client circuit which is far enough
  464. * along in connecting to its destination, and we haven't already
  465. * flagged it as 'timed out', and the user has not told us to
  466. * close such circs immediately on timeout, flag it as 'timed out'
  467. * so we'll launch another intro or rend circ, but don't mark it
  468. * for close yet.
  469. *
  470. * (Circs flagged as 'timed out' are given a much longer timeout
  471. * period above, so we won't close them in the next call to
  472. * circuit_expire_building.) */
  473. if (!(options->CloseHSClientCircuitsImmediatelyOnTimeout) &&
  474. !(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)) {
  475. switch (victim->purpose) {
  476. case CIRCUIT_PURPOSE_C_REND_READY:
  477. /* We only want to spare a rend circ if it has been specified in
  478. * an INTRODUCE1 cell sent to a hidden service. A circ's
  479. * pending_final_cpath field is non-NULL iff it is a rend circ
  480. * and we have tried to send an INTRODUCE1 cell specifying it.
  481. * Thus, if the pending_final_cpath field *is* NULL, then we
  482. * want to not spare it. */
  483. if (TO_ORIGIN_CIRCUIT(victim)->build_state->pending_final_cpath ==
  484. NULL)
  485. break;
  486. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  487. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  488. /* If we have reached this line, we want to spare the circ for now. */
  489. log_info(LD_CIRC,"Marking circ %d (state %d:%s, purpose %d) "
  490. "as timed-out HS circ",
  491. victim->n_circ_id,
  492. victim->state, circuit_state_to_string(victim->state),
  493. victim->purpose);
  494. TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out = 1;
  495. continue;
  496. default:
  497. break;
  498. }
  499. }
  500. /* If this is a service-side rendezvous circuit which is far
  501. * enough along in connecting to its destination, consider sparing
  502. * it. */
  503. if (!(options->CloseHSServiceRendCircuitsImmediatelyOnTimeout) &&
  504. !(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out) &&
  505. victim->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
  506. log_info(LD_CIRC,"Marking circ %d (state %d:%s, purpose %d) "
  507. "as timed-out HS circ; relaunching rendezvous attempt.",
  508. victim->n_circ_id,
  509. victim->state, circuit_state_to_string(victim->state),
  510. victim->purpose);
  511. TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out = 1;
  512. rend_service_relaunch_rendezvous(TO_ORIGIN_CIRCUIT(victim));
  513. continue;
  514. }
  515. if (victim->n_chan)
  516. log_info(LD_CIRC,"Abandoning circ %s:%d (state %d:%s, purpose %d)",
  517. channel_get_canonical_remote_descr(victim->n_chan),
  518. victim->n_circ_id,
  519. victim->state, circuit_state_to_string(victim->state),
  520. victim->purpose);
  521. else
  522. log_info(LD_CIRC,"Abandoning circ %d (state %d:%s, purpose %d)",
  523. victim->n_circ_id, victim->state,
  524. circuit_state_to_string(victim->state), victim->purpose);
  525. circuit_log_path(LOG_INFO,LD_CIRC,TO_ORIGIN_CIRCUIT(victim));
  526. if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
  527. circuit_mark_for_close(victim, END_CIRC_REASON_MEASUREMENT_EXPIRED);
  528. else
  529. circuit_mark_for_close(victim, END_CIRC_REASON_TIMEOUT);
  530. }
  531. }
  532. /** Remove any elements in <b>needed_ports</b> that are handled by an
  533. * open or in-progress circuit.
  534. */
  535. void
  536. circuit_remove_handled_ports(smartlist_t *needed_ports)
  537. {
  538. int i;
  539. uint16_t *port;
  540. for (i = 0; i < smartlist_len(needed_ports); ++i) {
  541. port = smartlist_get(needed_ports, i);
  542. tor_assert(*port);
  543. if (circuit_stream_is_being_handled(NULL, *port,
  544. MIN_CIRCUITS_HANDLING_STREAM)) {
  545. // log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
  546. smartlist_del(needed_ports, i--);
  547. tor_free(port);
  548. } else {
  549. log_debug(LD_CIRC,"Port %d is not handled.", *port);
  550. }
  551. }
  552. }
  553. /** Return 1 if at least <b>min</b> general-purpose non-internal circuits
  554. * will have an acceptable exit node for exit stream <b>conn</b> if it
  555. * is defined, else for "*:port".
  556. * Else return 0.
  557. */
  558. int
  559. circuit_stream_is_being_handled(entry_connection_t *conn,
  560. uint16_t port, int min)
  561. {
  562. circuit_t *circ;
  563. const node_t *exitnode;
  564. int num=0;
  565. time_t now = time(NULL);
  566. int need_uptime = smartlist_string_num_isin(get_options()->LongLivedPorts,
  567. conn ? conn->socks_request->port : port);
  568. for (circ=global_circuitlist;circ;circ = circ->next) {
  569. if (CIRCUIT_IS_ORIGIN(circ) &&
  570. !circ->marked_for_close &&
  571. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  572. (!circ->timestamp_dirty ||
  573. circ->timestamp_dirty + get_options()->MaxCircuitDirtiness > now)) {
  574. cpath_build_state_t *build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
  575. if (build_state->is_internal || build_state->onehop_tunnel)
  576. continue;
  577. exitnode = build_state_get_exit_node(build_state);
  578. if (exitnode && (!need_uptime || build_state->need_uptime)) {
  579. int ok;
  580. if (conn) {
  581. ok = connection_ap_can_use_exit(conn, exitnode);
  582. } else {
  583. addr_policy_result_t r;
  584. r = compare_tor_addr_to_node_policy(NULL, port, exitnode);
  585. ok = r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED;
  586. }
  587. if (ok) {
  588. if (++num >= min)
  589. return 1;
  590. }
  591. }
  592. }
  593. }
  594. return 0;
  595. }
  596. /** Don't keep more than this many unused open circuits around. */
  597. #define MAX_UNUSED_OPEN_CIRCUITS 14
  598. /** Figure out how many circuits we have open that are clean. Make
  599. * sure it's enough for all the upcoming behaviors we predict we'll have.
  600. * But put an upper bound on the total number of circuits.
  601. */
  602. static void
  603. circuit_predict_and_launch_new(void)
  604. {
  605. circuit_t *circ;
  606. int num=0, num_internal=0, num_uptime_internal=0;
  607. int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
  608. int port_needs_uptime=0, port_needs_capacity=1;
  609. time_t now = time(NULL);
  610. int flags = 0;
  611. /* First, count how many of each type of circuit we have already. */
  612. for (circ=global_circuitlist;circ;circ = circ->next) {
  613. cpath_build_state_t *build_state;
  614. if (!CIRCUIT_IS_ORIGIN(circ))
  615. continue;
  616. if (circ->marked_for_close)
  617. continue; /* don't mess with marked circs */
  618. if (circ->timestamp_dirty)
  619. continue; /* only count clean circs */
  620. if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL)
  621. continue; /* only pay attention to general-purpose circs */
  622. build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
  623. if (build_state->onehop_tunnel)
  624. continue;
  625. num++;
  626. if (build_state->is_internal)
  627. num_internal++;
  628. if (build_state->need_uptime && build_state->is_internal)
  629. num_uptime_internal++;
  630. }
  631. /* If that's enough, then stop now. */
  632. if (num >= MAX_UNUSED_OPEN_CIRCUITS)
  633. return; /* we already have many, making more probably will hurt */
  634. /* Second, see if we need any more exit circuits. */
  635. /* check if we know of a port that's been requested recently
  636. * and no circuit is currently available that can handle it. */
  637. if (!circuit_all_predicted_ports_handled(now, &port_needs_uptime,
  638. &port_needs_capacity)) {
  639. if (port_needs_uptime)
  640. flags |= CIRCLAUNCH_NEED_UPTIME;
  641. if (port_needs_capacity)
  642. flags |= CIRCLAUNCH_NEED_CAPACITY;
  643. log_info(LD_CIRC,
  644. "Have %d clean circs (%d internal), need another exit circ.",
  645. num, num_internal);
  646. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  647. return;
  648. }
  649. /* Third, see if we need any more hidden service (server) circuits. */
  650. if (num_rend_services() && num_uptime_internal < 3) {
  651. flags = (CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_NEED_UPTIME |
  652. CIRCLAUNCH_IS_INTERNAL);
  653. log_info(LD_CIRC,
  654. "Have %d clean circs (%d internal), need another internal "
  655. "circ for my hidden service.",
  656. num, num_internal);
  657. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  658. return;
  659. }
  660. /* Fourth, see if we need any more hidden service (client) circuits. */
  661. if (rep_hist_get_predicted_internal(now, &hidserv_needs_uptime,
  662. &hidserv_needs_capacity) &&
  663. ((num_uptime_internal<2 && hidserv_needs_uptime) ||
  664. num_internal<2)) {
  665. if (hidserv_needs_uptime)
  666. flags |= CIRCLAUNCH_NEED_UPTIME;
  667. if (hidserv_needs_capacity)
  668. flags |= CIRCLAUNCH_NEED_CAPACITY;
  669. flags |= CIRCLAUNCH_IS_INTERNAL;
  670. log_info(LD_CIRC,
  671. "Have %d clean circs (%d uptime-internal, %d internal), need"
  672. " another hidden service circ.",
  673. num, num_uptime_internal, num_internal);
  674. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  675. return;
  676. }
  677. /* Finally, check to see if we still need more circuits to learn
  678. * a good build timeout. But if we're close to our max number we
  679. * want, don't do another -- we want to leave a few slots open so
  680. * we can still build circuits preemptively as needed. */
  681. if (num < MAX_UNUSED_OPEN_CIRCUITS-2 &&
  682. get_options()->LearnCircuitBuildTimeout &&
  683. circuit_build_times_needs_circuits_now(&circ_times)) {
  684. flags = CIRCLAUNCH_NEED_CAPACITY;
  685. log_info(LD_CIRC,
  686. "Have %d clean circs need another buildtime test circ.", num);
  687. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  688. return;
  689. }
  690. }
  691. /** Build a new test circuit every 5 minutes */
  692. #define TESTING_CIRCUIT_INTERVAL 300
  693. /** This function is called once a second, if router_have_min_dir_info() is
  694. * true. Its job is to make sure all services we offer have enough circuits
  695. * available. Some services just want enough circuits for current tasks,
  696. * whereas others want a minimum set of idle circuits hanging around.
  697. */
  698. void
  699. circuit_build_needed_circs(time_t now)
  700. {
  701. static time_t time_to_new_circuit = 0;
  702. const or_options_t *options = get_options();
  703. /* launch a new circ for any pending streams that need one */
  704. connection_ap_attach_pending();
  705. /* make sure any hidden services have enough intro points */
  706. rend_services_introduce();
  707. if (time_to_new_circuit < now) {
  708. circuit_reset_failure_count(1);
  709. time_to_new_circuit = now + options->NewCircuitPeriod;
  710. if (proxy_mode(get_options()))
  711. addressmap_clean(now);
  712. circuit_expire_old_circuits_clientside();
  713. #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
  714. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  715. if (get_options()->RunTesting &&
  716. circ &&
  717. circ->timestamp_created.tv_sec + TESTING_CIRCUIT_INTERVAL < now) {
  718. log_fn(LOG_INFO,"Creating a new testing circuit.");
  719. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, 0);
  720. }
  721. #endif
  722. }
  723. if (!options->DisablePredictedCircuits)
  724. circuit_predict_and_launch_new();
  725. }
  726. /** If the stream <b>conn</b> is a member of any of the linked
  727. * lists of <b>circ</b>, then remove it from the list.
  728. */
  729. void
  730. circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
  731. {
  732. edge_connection_t *prevconn;
  733. tor_assert(circ);
  734. tor_assert(conn);
  735. if (conn->base_.type == CONN_TYPE_AP) {
  736. entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
  737. entry_conn->may_use_optimistic_data = 0;
  738. }
  739. conn->cpath_layer = NULL; /* don't keep a stale pointer */
  740. conn->on_circuit = NULL;
  741. if (CIRCUIT_IS_ORIGIN(circ)) {
  742. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  743. if (conn == origin_circ->p_streams) {
  744. origin_circ->p_streams = conn->next_stream;
  745. return;
  746. }
  747. for (prevconn = origin_circ->p_streams;
  748. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  749. prevconn = prevconn->next_stream)
  750. ;
  751. if (prevconn && prevconn->next_stream) {
  752. prevconn->next_stream = conn->next_stream;
  753. return;
  754. }
  755. } else {
  756. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  757. if (conn == or_circ->n_streams) {
  758. or_circ->n_streams = conn->next_stream;
  759. return;
  760. }
  761. if (conn == or_circ->resolving_streams) {
  762. or_circ->resolving_streams = conn->next_stream;
  763. return;
  764. }
  765. for (prevconn = or_circ->n_streams;
  766. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  767. prevconn = prevconn->next_stream)
  768. ;
  769. if (prevconn && prevconn->next_stream) {
  770. prevconn->next_stream = conn->next_stream;
  771. return;
  772. }
  773. for (prevconn = or_circ->resolving_streams;
  774. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  775. prevconn = prevconn->next_stream)
  776. ;
  777. if (prevconn && prevconn->next_stream) {
  778. prevconn->next_stream = conn->next_stream;
  779. return;
  780. }
  781. }
  782. log_warn(LD_BUG,"Edge connection not in circuit's list.");
  783. /* Don't give an error here; it's harmless. */
  784. tor_fragile_assert();
  785. }
  786. /** If we haven't yet decided on a good timeout value for circuit
  787. * building, we close idles circuits aggressively so we can get more
  788. * data points. */
  789. #define IDLE_TIMEOUT_WHILE_LEARNING (10*60)
  790. /** Find each circuit that has been unused for too long, or dirty
  791. * for too long and has no streams on it: mark it for close.
  792. */
  793. static void
  794. circuit_expire_old_circuits_clientside(void)
  795. {
  796. circuit_t *circ;
  797. struct timeval cutoff, now;
  798. tor_gettimeofday(&now);
  799. cutoff = now;
  800. if (get_options()->LearnCircuitBuildTimeout &&
  801. circuit_build_times_needs_circuits(&circ_times)) {
  802. /* Circuits should be shorter lived if we need more of them
  803. * for learning a good build timeout */
  804. cutoff.tv_sec -= IDLE_TIMEOUT_WHILE_LEARNING;
  805. } else {
  806. cutoff.tv_sec -= get_options()->CircuitIdleTimeout;
  807. }
  808. for (circ = global_circuitlist; circ; circ = circ->next) {
  809. if (circ->marked_for_close || !CIRCUIT_IS_ORIGIN(circ))
  810. continue;
  811. /* If the circuit has been dirty for too long, and there are no streams
  812. * on it, mark it for close.
  813. */
  814. if (circ->timestamp_dirty &&
  815. circ->timestamp_dirty + get_options()->MaxCircuitDirtiness <
  816. now.tv_sec &&
  817. !TO_ORIGIN_CIRCUIT(circ)->p_streams /* nothing attached */ ) {
  818. log_debug(LD_CIRC, "Closing n_circ_id %d (dirty %ld sec ago, "
  819. "purpose %d)",
  820. circ->n_circ_id, (long)(now.tv_sec - circ->timestamp_dirty),
  821. circ->purpose);
  822. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  823. } else if (!circ->timestamp_dirty && circ->state == CIRCUIT_STATE_OPEN) {
  824. if (timercmp(&circ->timestamp_created, &cutoff, <)) {
  825. if (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  826. circ->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT ||
  827. circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  828. circ->purpose == CIRCUIT_PURPOSE_TESTING ||
  829. (circ->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  830. circ->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) ||
  831. circ->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
  832. log_debug(LD_CIRC,
  833. "Closing circuit that has been unused for %ld msec.",
  834. tv_mdiff(&circ->timestamp_created, &now));
  835. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  836. } else if (!TO_ORIGIN_CIRCUIT(circ)->is_ancient) {
  837. /* Server-side rend joined circuits can end up really old, because
  838. * they are reused by clients for longer than normal. The client
  839. * controls their lifespan. (They never become dirty, because
  840. * connection_exit_begin_conn() never marks anything as dirty.)
  841. * Similarly, server-side intro circuits last a long time. */
  842. if (circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED &&
  843. circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  844. log_notice(LD_CIRC,
  845. "Ancient non-dirty circuit %d is still around after "
  846. "%ld milliseconds. Purpose: %d (%s)",
  847. TO_ORIGIN_CIRCUIT(circ)->global_identifier,
  848. tv_mdiff(&circ->timestamp_created, &now),
  849. circ->purpose,
  850. circuit_purpose_to_string(circ->purpose));
  851. TO_ORIGIN_CIRCUIT(circ)->is_ancient = 1;
  852. }
  853. }
  854. }
  855. }
  856. }
  857. }
  858. /** How long do we wait before killing circuits with the properties
  859. * described below?
  860. *
  861. * Probably we could choose a number here as low as 5 to 10 seconds,
  862. * since these circs are used for begindir, and a) generally you either
  863. * ask another begindir question right after or you don't for a long time,
  864. * b) clients at least through 0.2.1.x choose from the whole set of
  865. * directory mirrors at each choice, and c) re-establishing a one-hop
  866. * circuit via create-fast is a light operation assuming the TLS conn is
  867. * still there.
  868. *
  869. * I expect "b" to go away one day when we move to using directory
  870. * guards, but I think "a" and "c" are good enough reasons that a low
  871. * number is safe even then.
  872. */
  873. #define IDLE_ONE_HOP_CIRC_TIMEOUT 60
  874. /** Find each non-origin circuit that has been unused for too long,
  875. * has no streams on it, used a create_fast, and ends here: mark it
  876. * for close.
  877. */
  878. void
  879. circuit_expire_old_circuits_serverside(time_t now)
  880. {
  881. circuit_t *circ;
  882. or_circuit_t *or_circ;
  883. time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;
  884. for (circ = global_circuitlist; circ; circ = circ->next) {
  885. if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
  886. continue;
  887. or_circ = TO_OR_CIRCUIT(circ);
  888. /* If the circuit has been idle for too long, and there are no streams
  889. * on it, and it ends here, and it used a create_fast, mark it for close.
  890. */
  891. if (or_circ->is_first_hop && !circ->n_chan &&
  892. !or_circ->n_streams && !or_circ->resolving_streams &&
  893. or_circ->p_chan &&
  894. channel_when_last_xmit(or_circ->p_chan) <= cutoff) {
  895. log_info(LD_CIRC, "Closing circ_id %d (empty %d secs ago)",
  896. or_circ->p_circ_id,
  897. (int)(now - channel_when_last_xmit(or_circ->p_chan)));
  898. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  899. }
  900. }
  901. }
  902. /** Number of testing circuits we want open before testing our bandwidth. */
  903. #define NUM_PARALLEL_TESTING_CIRCS 4
  904. /** True iff we've ever had enough testing circuits open to test our
  905. * bandwidth. */
  906. static int have_performed_bandwidth_test = 0;
  907. /** Reset have_performed_bandwidth_test, so we'll start building
  908. * testing circuits again so we can exercise our bandwidth. */
  909. void
  910. reset_bandwidth_test(void)
  911. {
  912. have_performed_bandwidth_test = 0;
  913. }
  914. /** Return 1 if we've already exercised our bandwidth, or if we
  915. * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
  916. * established or on the way. Else return 0.
  917. */
  918. int
  919. circuit_enough_testing_circs(void)
  920. {
  921. circuit_t *circ;
  922. int num = 0;
  923. if (have_performed_bandwidth_test)
  924. return 1;
  925. for (circ = global_circuitlist; circ; circ = circ->next) {
  926. if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
  927. circ->purpose == CIRCUIT_PURPOSE_TESTING &&
  928. circ->state == CIRCUIT_STATE_OPEN)
  929. num++;
  930. }
  931. return num >= NUM_PARALLEL_TESTING_CIRCS;
  932. }
  933. /** A testing circuit has completed. Take whatever stats we want.
  934. * Noticing reachability is taken care of in onionskin_answer(),
  935. * so there's no need to record anything here. But if we still want
  936. * to do the bandwidth test, and we now have enough testing circuits
  937. * open, do it.
  938. */
  939. static void
  940. circuit_testing_opened(origin_circuit_t *circ)
  941. {
  942. if (have_performed_bandwidth_test ||
  943. !check_whether_orport_reachable()) {
  944. /* either we've already done everything we want with testing circuits,
  945. * or this testing circuit became open due to a fluke, e.g. we picked
  946. * a last hop where we already had the connection open due to an
  947. * outgoing local circuit. */
  948. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_AT_ORIGIN);
  949. } else if (circuit_enough_testing_circs()) {
  950. router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS, time(NULL));
  951. have_performed_bandwidth_test = 1;
  952. } else
  953. consider_testing_reachability(1, 0);
  954. }
  955. /** A testing circuit has failed to build. Take whatever stats we want. */
  956. static void
  957. circuit_testing_failed(origin_circuit_t *circ, int at_last_hop)
  958. {
  959. if (server_mode(get_options()) && check_whether_orport_reachable())
  960. return;
  961. log_info(LD_GENERAL,
  962. "Our testing circuit (to see if your ORPort is reachable) "
  963. "has failed. I'll try again later.");
  964. /* These aren't used yet. */
  965. (void)circ;
  966. (void)at_last_hop;
  967. }
  968. /** The circuit <b>circ</b> has just become open. Take the next
  969. * step: for rendezvous circuits, we pass circ to the appropriate
  970. * function in rendclient or rendservice. For general circuits, we
  971. * call connection_ap_attach_pending, which looks for pending streams
  972. * that could use circ.
  973. */
  974. void
  975. circuit_has_opened(origin_circuit_t *circ)
  976. {
  977. control_event_circuit_status(circ, CIRC_EVENT_BUILT, 0);
  978. /* Remember that this circuit has finished building. Now if we start
  979. * it building again later (e.g. by extending it), we will know not
  980. * to consider its build time. */
  981. circ->has_opened = 1;
  982. switch (TO_CIRCUIT(circ)->purpose) {
  983. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  984. rend_client_rendcirc_has_opened(circ);
  985. /* Start building an intro circ if we don't have one yet. */
  986. connection_ap_attach_pending();
  987. /* This isn't a call to circuit_try_attaching_streams because a
  988. * circuit in _C_ESTABLISH_REND state isn't connected to its
  989. * hidden service yet, thus we can't attach streams to it yet,
  990. * thus circuit_try_attaching_streams would always clear the
  991. * circuit's isolation state. circuit_try_attaching_streams is
  992. * called later, when the rend circ enters _C_REND_JOINED
  993. * state. */
  994. break;
  995. case CIRCUIT_PURPOSE_C_INTRODUCING:
  996. rend_client_introcirc_has_opened(circ);
  997. break;
  998. case CIRCUIT_PURPOSE_C_GENERAL:
  999. /* Tell any AP connections that have been waiting for a new
  1000. * circuit that one is ready. */
  1001. circuit_try_attaching_streams(circ);
  1002. break;
  1003. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1004. /* at Bob, waiting for introductions */
  1005. rend_service_intro_has_opened(circ);
  1006. break;
  1007. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1008. /* at Bob, connecting to rend point */
  1009. rend_service_rendezvous_has_opened(circ);
  1010. break;
  1011. case CIRCUIT_PURPOSE_TESTING:
  1012. circuit_testing_opened(circ);
  1013. break;
  1014. /* default:
  1015. * This won't happen in normal operation, but might happen if the
  1016. * controller did it. Just let it slide. */
  1017. }
  1018. }
  1019. /** If the stream-isolation state of <b>circ</b> can be cleared, clear
  1020. * it. Return non-zero iff <b>circ</b>'s isolation state was cleared. */
  1021. static int
  1022. circuit_try_clearing_isolation_state(origin_circuit_t *circ)
  1023. {
  1024. if (/* The circuit may have become non-open if it was cannibalized.*/
  1025. circ->base_.state == CIRCUIT_STATE_OPEN &&
  1026. /* If !isolation_values_set, there is nothing to clear. */
  1027. circ->isolation_values_set &&
  1028. /* It's not legal to clear a circuit's isolation info if it's ever had
  1029. * streams attached */
  1030. !circ->isolation_any_streams_attached) {
  1031. /* If we have any isolation information set on this circuit, and
  1032. * we didn't manage to attach any streams to it, then we can
  1033. * and should clear it and try again. */
  1034. circuit_clear_isolation(circ);
  1035. return 1;
  1036. } else {
  1037. return 0;
  1038. }
  1039. }
  1040. /** Called when a circuit becomes ready for streams to be attached to
  1041. * it. */
  1042. void
  1043. circuit_try_attaching_streams(origin_circuit_t *circ)
  1044. {
  1045. /* Attach streams to this circuit if we can. */
  1046. connection_ap_attach_pending();
  1047. /* The call to circuit_try_clearing_isolation_state here will do
  1048. * nothing and return 0 if we didn't attach any streams to circ
  1049. * above. */
  1050. if (circuit_try_clearing_isolation_state(circ)) {
  1051. /* Maybe *now* we can attach some streams to this circuit. */
  1052. connection_ap_attach_pending();
  1053. }
  1054. }
  1055. /** Called whenever a circuit could not be successfully built.
  1056. */
  1057. void
  1058. circuit_build_failed(origin_circuit_t *circ)
  1059. {
  1060. channel_t *n_chan = NULL;
  1061. /* we should examine circ and see if it failed because of
  1062. * the last hop or an earlier hop. then use this info below.
  1063. */
  1064. int failed_at_last_hop = 0;
  1065. /* If the last hop isn't open, and the second-to-last is, we failed
  1066. * at the last hop. */
  1067. if (circ->cpath &&
  1068. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  1069. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  1070. failed_at_last_hop = 1;
  1071. }
  1072. if (circ->cpath &&
  1073. circ->cpath->state != CPATH_STATE_OPEN) {
  1074. /* We failed at the first hop. If there's an OR connection
  1075. * to blame, blame it. Also, avoid this relay for a while, and
  1076. * fail any one-hop directory fetches destined for it. */
  1077. const char *n_chan_id = circ->cpath->extend_info->identity_digest;
  1078. int already_marked = 0;
  1079. if (circ->base_.n_chan) {
  1080. n_chan = circ->base_.n_chan;
  1081. if (n_chan->is_bad_for_new_circs) {
  1082. /* We only want to blame this router when a fresh healthy
  1083. * connection fails. So don't mark this router as newly failed,
  1084. * since maybe this was just an old circuit attempt that's
  1085. * finally timing out now. Also, there's no need to blow away
  1086. * circuits/streams/etc, since the failure of an unhealthy conn
  1087. * doesn't tell us much about whether a healthy conn would
  1088. * succeed. */
  1089. already_marked = 1;
  1090. }
  1091. log_info(LD_OR,
  1092. "Our circuit failed to get a response from the first hop "
  1093. "(%s). I'm going to try to rotate to a better connection.",
  1094. channel_get_canonical_remote_descr(n_chan));
  1095. n_chan->is_bad_for_new_circs = 1;
  1096. } else {
  1097. log_info(LD_OR,
  1098. "Our circuit died before the first hop with no connection");
  1099. }
  1100. if (n_chan_id && !already_marked) {
  1101. entry_guard_register_connect_status(n_chan_id, 0, 1, time(NULL));
  1102. /* if there are any one-hop streams waiting on this circuit, fail
  1103. * them now so they can retry elsewhere. */
  1104. connection_ap_fail_onehop(n_chan_id, circ->build_state);
  1105. }
  1106. }
  1107. switch (circ->base_.purpose) {
  1108. case CIRCUIT_PURPOSE_C_GENERAL:
  1109. /* If we never built the circuit, note it as a failure. */
  1110. circuit_increment_failure_count();
  1111. if (failed_at_last_hop) {
  1112. /* Make sure any streams that demand our last hop as their exit
  1113. * know that it's unlikely to happen. */
  1114. circuit_discard_optional_exit_enclaves(circ->cpath->prev->extend_info);
  1115. }
  1116. break;
  1117. case CIRCUIT_PURPOSE_TESTING:
  1118. circuit_testing_failed(circ, failed_at_last_hop);
  1119. break;
  1120. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1121. /* at Bob, waiting for introductions */
  1122. if (circ->base_.state != CIRCUIT_STATE_OPEN) {
  1123. circuit_increment_failure_count();
  1124. }
  1125. /* no need to care here, because bob will rebuild intro
  1126. * points periodically. */
  1127. break;
  1128. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1129. /* at Alice, connecting to intro point */
  1130. /* Don't increment failure count, since Bob may have picked
  1131. * the introduction point maliciously */
  1132. /* Alice will pick a new intro point when this one dies, if
  1133. * the stream in question still cares. No need to act here. */
  1134. break;
  1135. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1136. /* at Alice, waiting for Bob */
  1137. circuit_increment_failure_count();
  1138. /* Alice will pick a new rend point when this one dies, if
  1139. * the stream in question still cares. No need to act here. */
  1140. break;
  1141. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1142. /* at Bob, connecting to rend point */
  1143. /* Don't increment failure count, since Alice may have picked
  1144. * the rendezvous point maliciously */
  1145. log_info(LD_REND,
  1146. "Couldn't connect to Alice's chosen rend point %s "
  1147. "(%s hop failed).",
  1148. escaped(build_state_get_exit_nickname(circ->build_state)),
  1149. failed_at_last_hop?"last":"non-last");
  1150. rend_service_relaunch_rendezvous(circ);
  1151. break;
  1152. /* default:
  1153. * This won't happen in normal operation, but might happen if the
  1154. * controller did it. Just let it slide. */
  1155. }
  1156. }
  1157. /** Number of consecutive failures so far; should only be touched by
  1158. * circuit_launch_new and circuit_*_failure_count.
  1159. */
  1160. static int n_circuit_failures = 0;
  1161. /** Before the last time we called circuit_reset_failure_count(), were
  1162. * there a lot of failures? */
  1163. static int did_circs_fail_last_period = 0;
  1164. /** Don't retry launching a new circuit if we try this many times with no
  1165. * success. */
  1166. #define MAX_CIRCUIT_FAILURES 5
  1167. /** Launch a new circuit; see circuit_launch_by_extend_info() for
  1168. * details on arguments. */
  1169. origin_circuit_t *
  1170. circuit_launch(uint8_t purpose, int flags)
  1171. {
  1172. return circuit_launch_by_extend_info(purpose, NULL, flags);
  1173. }
  1174. /** Launch a new circuit with purpose <b>purpose</b> and exit node
  1175. * <b>extend_info</b> (or NULL to select a random exit node). If flags
  1176. * contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
  1177. * CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
  1178. * If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
  1179. * If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
  1180. * Return the newly allocated circuit on success, or NULL on failure. */
  1181. origin_circuit_t *
  1182. circuit_launch_by_extend_info(uint8_t purpose,
  1183. extend_info_t *extend_info,
  1184. int flags)
  1185. {
  1186. origin_circuit_t *circ;
  1187. int onehop_tunnel = (flags & CIRCLAUNCH_ONEHOP_TUNNEL) != 0;
  1188. if (!onehop_tunnel && !router_have_minimum_dir_info()) {
  1189. log_debug(LD_CIRC,"Haven't fetched enough directory info yet; canceling "
  1190. "circuit launch.");
  1191. return NULL;
  1192. }
  1193. if ((extend_info || purpose != CIRCUIT_PURPOSE_C_GENERAL) &&
  1194. purpose != CIRCUIT_PURPOSE_TESTING && !onehop_tunnel) {
  1195. /* see if there are appropriate circs available to cannibalize. */
  1196. /* XXX if we're planning to add a hop, perhaps we want to look for
  1197. * internal circs rather than exit circs? -RD */
  1198. circ = circuit_find_to_cannibalize(purpose, extend_info, flags);
  1199. if (circ) {
  1200. uint8_t old_purpose = circ->base_.purpose;
  1201. struct timeval old_timestamp_created;
  1202. log_info(LD_CIRC,"Cannibalizing circ '%s' for purpose %d (%s)",
  1203. build_state_get_exit_nickname(circ->build_state), purpose,
  1204. circuit_purpose_to_string(purpose));
  1205. circuit_change_purpose(TO_CIRCUIT(circ), purpose);
  1206. /* reset the birth date of this circ, else expire_building
  1207. * will see it and think it's been trying to build since it
  1208. * began. */
  1209. tor_gettimeofday(&circ->base_.timestamp_created);
  1210. control_event_circuit_cannibalized(circ, old_purpose,
  1211. &old_timestamp_created);
  1212. switch (purpose) {
  1213. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1214. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1215. /* it's ready right now */
  1216. break;
  1217. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1218. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1219. case CIRCUIT_PURPOSE_C_GENERAL:
  1220. /* need to add a new hop */
  1221. tor_assert(extend_info);
  1222. if (circuit_extend_to_new_exit(circ, extend_info) < 0)
  1223. return NULL;
  1224. break;
  1225. default:
  1226. log_warn(LD_BUG,
  1227. "unexpected purpose %d when cannibalizing a circ.",
  1228. purpose);
  1229. tor_fragile_assert();
  1230. return NULL;
  1231. }
  1232. return circ;
  1233. }
  1234. }
  1235. if (did_circs_fail_last_period &&
  1236. n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  1237. /* too many failed circs in a row. don't try. */
  1238. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  1239. return NULL;
  1240. }
  1241. /* try a circ. if it fails, circuit_mark_for_close will increment
  1242. * n_circuit_failures */
  1243. return circuit_establish_circuit(purpose, extend_info, flags);
  1244. }
  1245. /** Record another failure at opening a general circuit. When we have
  1246. * too many, we'll stop trying for the remainder of this minute.
  1247. */
  1248. static void
  1249. circuit_increment_failure_count(void)
  1250. {
  1251. ++n_circuit_failures;
  1252. log_debug(LD_CIRC,"n_circuit_failures now %d.",n_circuit_failures);
  1253. }
  1254. /** Reset the failure count for opening general circuits. This means
  1255. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  1256. * stopping again.
  1257. */
  1258. void
  1259. circuit_reset_failure_count(int timeout)
  1260. {
  1261. if (timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
  1262. did_circs_fail_last_period = 1;
  1263. else
  1264. did_circs_fail_last_period = 0;
  1265. n_circuit_failures = 0;
  1266. }
  1267. /** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
  1268. * there isn't one, and there isn't one on the way, launch one and return
  1269. * 0. If it will never work, return -1.
  1270. *
  1271. * Write the found or in-progress or launched circ into *circp.
  1272. */
  1273. static int
  1274. circuit_get_open_circ_or_launch(entry_connection_t *conn,
  1275. uint8_t desired_circuit_purpose,
  1276. origin_circuit_t **circp)
  1277. {
  1278. origin_circuit_t *circ;
  1279. int check_exit_policy;
  1280. int need_uptime, need_internal;
  1281. int want_onehop;
  1282. const or_options_t *options = get_options();
  1283. tor_assert(conn);
  1284. tor_assert(circp);
  1285. tor_assert(ENTRY_TO_CONN(conn)->state == AP_CONN_STATE_CIRCUIT_WAIT);
  1286. check_exit_policy =
  1287. conn->socks_request->command == SOCKS_COMMAND_CONNECT &&
  1288. !conn->use_begindir &&
  1289. !connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn));
  1290. want_onehop = conn->want_onehop;
  1291. need_uptime = !conn->want_onehop && !conn->use_begindir &&
  1292. smartlist_string_num_isin(options->LongLivedPorts,
  1293. conn->socks_request->port);
  1294. if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)
  1295. need_internal = 1;
  1296. else if (conn->use_begindir || conn->want_onehop)
  1297. need_internal = 1;
  1298. else
  1299. need_internal = 0;
  1300. circ = circuit_get_best(conn, 1, desired_circuit_purpose,
  1301. need_uptime, need_internal);
  1302. if (circ) {
  1303. *circp = circ;
  1304. return 1; /* we're happy */
  1305. }
  1306. if (!want_onehop && !router_have_minimum_dir_info()) {
  1307. if (!connection_get_by_type(CONN_TYPE_DIR)) {
  1308. int severity = LOG_NOTICE;
  1309. /* FFFF if this is a tunneled directory fetch, don't yell
  1310. * as loudly. the user doesn't even know it's happening. */
  1311. if (entry_list_is_constrained(options) &&
  1312. entries_known_but_down(options)) {
  1313. log_fn(severity, LD_APP|LD_DIR,
  1314. "Application request when we haven't used client functionality "
  1315. "lately. Optimistically trying known %s again.",
  1316. options->UseBridges ? "bridges" : "entrynodes");
  1317. entries_retry_all(options);
  1318. } else if (!options->UseBridges || any_bridge_descriptors_known()) {
  1319. log_fn(severity, LD_APP|LD_DIR,
  1320. "Application request when we haven't used client functionality "
  1321. "lately. Optimistically trying directory fetches again.");
  1322. routerlist_retry_directory_downloads(time(NULL));
  1323. }
  1324. }
  1325. /* the stream will be dealt with when router_have_minimum_dir_info becomes
  1326. * 1, or when all directory attempts fail and directory_all_unreachable()
  1327. * kills it.
  1328. */
  1329. return 0;
  1330. }
  1331. /* Do we need to check exit policy? */
  1332. if (check_exit_policy) {
  1333. if (!conn->chosen_exit_name) {
  1334. struct in_addr in;
  1335. tor_addr_t addr, *addrp=NULL;
  1336. if (tor_inet_aton(conn->socks_request->address, &in)) {
  1337. tor_addr_from_in(&addr, &in);
  1338. addrp = &addr;
  1339. }
  1340. if (router_exit_policy_all_nodes_reject(addrp,
  1341. conn->socks_request->port,
  1342. need_uptime)) {
  1343. log_notice(LD_APP,
  1344. "No Tor server allows exit to %s:%d. Rejecting.",
  1345. safe_str_client(conn->socks_request->address),
  1346. conn->socks_request->port);
  1347. return -1;
  1348. }
  1349. } else {
  1350. /* XXXX024 Duplicates checks in connection_ap_handshake_attach_circuit:
  1351. * refactor into a single function? */
  1352. const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 1);
  1353. int opt = conn->chosen_exit_optional;
  1354. if (node && !connection_ap_can_use_exit(conn, node)) {
  1355. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  1356. "Requested exit point '%s' is excluded or "
  1357. "would refuse request. %s.",
  1358. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  1359. if (opt) {
  1360. conn->chosen_exit_optional = 0;
  1361. tor_free(conn->chosen_exit_name);
  1362. /* Try again. */
  1363. return circuit_get_open_circ_or_launch(conn,
  1364. desired_circuit_purpose,
  1365. circp);
  1366. }
  1367. return -1;
  1368. }
  1369. }
  1370. }
  1371. /* is one already on the way? */
  1372. circ = circuit_get_best(conn, 0, desired_circuit_purpose,
  1373. need_uptime, need_internal);
  1374. if (circ)
  1375. log_debug(LD_CIRC, "one on the way!");
  1376. if (!circ) {
  1377. extend_info_t *extend_info=NULL;
  1378. uint8_t new_circ_purpose;
  1379. const int n_pending = count_pending_general_client_circuits();
  1380. if (n_pending >= options->MaxClientCircuitsPending) {
  1381. static ratelim_t delay_limit = RATELIM_INIT(10*60);
  1382. char *m;
  1383. if ((m = rate_limit_log(&delay_limit, approx_time()))) {
  1384. log_notice(LD_APP, "We'd like to launch a circuit to handle a "
  1385. "connection, but we already have %d general-purpose client "
  1386. "circuits pending. Waiting until some finish.%s",
  1387. n_pending, m);
  1388. tor_free(m);
  1389. }
  1390. return 0;
  1391. }
  1392. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1393. /* need to pick an intro point */
  1394. rend_data_t *rend_data = ENTRY_TO_EDGE_CONN(conn)->rend_data;
  1395. tor_assert(rend_data);
  1396. extend_info = rend_client_get_random_intro(rend_data);
  1397. if (!extend_info) {
  1398. log_info(LD_REND,
  1399. "No intro points for '%s': re-fetching service descriptor.",
  1400. safe_str_client(rend_data->onion_address));
  1401. rend_client_refetch_v2_renddesc(rend_data);
  1402. ENTRY_TO_CONN(conn)->state = AP_CONN_STATE_RENDDESC_WAIT;
  1403. return 0;
  1404. }
  1405. log_info(LD_REND,"Chose %s as intro point for '%s'.",
  1406. extend_info_describe(extend_info),
  1407. safe_str_client(rend_data->onion_address));
  1408. }
  1409. /* If we have specified a particular exit node for our
  1410. * connection, then be sure to open a circuit to that exit node.
  1411. */
  1412. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  1413. if (conn->chosen_exit_name) {
  1414. const node_t *r;
  1415. int opt = conn->chosen_exit_optional;
  1416. r = node_get_by_nickname(conn->chosen_exit_name, 1);
  1417. if (r && node_has_descriptor(r)) {
  1418. /* We might want to connect to an IPv6 bridge for loading
  1419. descriptors so we use the preferred address rather than
  1420. the primary. */
  1421. extend_info = extend_info_from_node(r, conn->want_onehop ? 1 : 0);
  1422. } else {
  1423. log_debug(LD_DIR, "considering %d, %s",
  1424. want_onehop, conn->chosen_exit_name);
  1425. if (want_onehop && conn->chosen_exit_name[0] == '$') {
  1426. /* We're asking for a one-hop circuit to a router that
  1427. * we don't have a routerinfo about. Make up an extend_info. */
  1428. char digest[DIGEST_LEN];
  1429. char *hexdigest = conn->chosen_exit_name+1;
  1430. tor_addr_t addr;
  1431. if (strlen(hexdigest) < HEX_DIGEST_LEN ||
  1432. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN)<0) {
  1433. log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
  1434. return -1;
  1435. }
  1436. if (tor_addr_parse(&addr, conn->socks_request->address) < 0) {
  1437. log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
  1438. escaped_safe_str_client(conn->socks_request->address));
  1439. return -1;
  1440. }
  1441. extend_info = extend_info_new(conn->chosen_exit_name+1,
  1442. digest, NULL, &addr,
  1443. conn->socks_request->port);
  1444. } else {
  1445. /* We will need an onion key for the router, and we
  1446. * don't have one. Refuse or relax requirements. */
  1447. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  1448. "Requested exit point '%s' is not known. %s.",
  1449. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  1450. if (opt) {
  1451. conn->chosen_exit_optional = 0;
  1452. tor_free(conn->chosen_exit_name);
  1453. /* Try again with no requested exit */
  1454. return circuit_get_open_circ_or_launch(conn,
  1455. desired_circuit_purpose,
  1456. circp);
  1457. }
  1458. return -1;
  1459. }
  1460. }
  1461. }
  1462. }
  1463. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  1464. new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  1465. else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  1466. new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  1467. else
  1468. new_circ_purpose = desired_circuit_purpose;
  1469. if (options->Tor2webMode &&
  1470. (new_circ_purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  1471. new_circ_purpose == CIRCUIT_PURPOSE_C_INTRODUCING)) {
  1472. want_onehop = 1;
  1473. }
  1474. {
  1475. int flags = CIRCLAUNCH_NEED_CAPACITY;
  1476. if (want_onehop) flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  1477. if (need_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
  1478. if (need_internal) flags |= CIRCLAUNCH_IS_INTERNAL;
  1479. circ = circuit_launch_by_extend_info(new_circ_purpose, extend_info,
  1480. flags);
  1481. }
  1482. extend_info_free(extend_info);
  1483. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  1484. /* We just caused a circuit to get built because of this stream.
  1485. * If this stream has caused a _lot_ of circuits to be built, that's
  1486. * a bad sign: we should tell the user. */
  1487. if (conn->num_circuits_launched < NUM_CIRCUITS_LAUNCHED_THRESHOLD &&
  1488. ++conn->num_circuits_launched == NUM_CIRCUITS_LAUNCHED_THRESHOLD)
  1489. log_info(LD_CIRC, "The application request to %s:%d has launched "
  1490. "%d circuits without finding one it likes.",
  1491. escaped_safe_str_client(conn->socks_request->address),
  1492. conn->socks_request->port,
  1493. conn->num_circuits_launched);
  1494. } else {
  1495. /* help predict this next time */
  1496. rep_hist_note_used_internal(time(NULL), need_uptime, 1);
  1497. if (circ) {
  1498. /* write the service_id into circ */
  1499. circ->rend_data = rend_data_dup(ENTRY_TO_EDGE_CONN(conn)->rend_data);
  1500. if (circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  1501. circ->base_.state == CIRCUIT_STATE_OPEN)
  1502. rend_client_rendcirc_has_opened(circ);
  1503. }
  1504. }
  1505. } /* endif (!circ) */
  1506. if (circ) {
  1507. /* Mark the circuit with the isolation fields for this connection.
  1508. * When the circuit arrives, we'll clear these flags: this is
  1509. * just some internal bookkeeping to make sure that we have
  1510. * launched enough circuits.
  1511. */
  1512. connection_edge_update_circuit_isolation(conn, circ, 0);
  1513. } else {
  1514. log_info(LD_APP,
  1515. "No safe circuit (purpose %d) ready for edge "
  1516. "connection; delaying.",
  1517. desired_circuit_purpose);
  1518. }
  1519. *circp = circ;
  1520. return 0;
  1521. }
  1522. /** Return true iff <b>crypt_path</b> is one of the crypt_paths for
  1523. * <b>circ</b>. */
  1524. static int
  1525. cpath_is_on_circuit(origin_circuit_t *circ, crypt_path_t *crypt_path)
  1526. {
  1527. crypt_path_t *cpath, *cpath_next = NULL;
  1528. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1529. cpath_next = cpath->next;
  1530. if (crypt_path == cpath)
  1531. return 1;
  1532. }
  1533. return 0;
  1534. }
  1535. /** Return true iff client-side optimistic data is supported. */
  1536. static int
  1537. optimistic_data_enabled(void)
  1538. {
  1539. const or_options_t *options = get_options();
  1540. if (options->OptimisticData < 0) {
  1541. /* XXX023 consider having auto default to 1 rather than 0 before
  1542. * the 0.2.3 branch goes stable. See bug 3617. -RD */
  1543. const int32_t enabled =
  1544. networkstatus_get_param(NULL, "UseOptimisticData", 0, 0, 1);
  1545. return (int)enabled;
  1546. }
  1547. return options->OptimisticData;
  1548. }
  1549. /** Attach the AP stream <b>apconn</b> to circ's linked list of
  1550. * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
  1551. * hop in circ's cpath if <b>cpath</b> is NULL.
  1552. */
  1553. static void
  1554. link_apconn_to_circ(entry_connection_t *apconn, origin_circuit_t *circ,
  1555. crypt_path_t *cpath)
  1556. {
  1557. const node_t *exitnode;
  1558. /* add it into the linked list of streams on this circuit */
  1559. log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %d.",
  1560. circ->base_.n_circ_id);
  1561. /* reset it, so we can measure circ timeouts */
  1562. ENTRY_TO_CONN(apconn)->timestamp_lastread = time(NULL);
  1563. ENTRY_TO_EDGE_CONN(apconn)->next_stream = circ->p_streams;
  1564. ENTRY_TO_EDGE_CONN(apconn)->on_circuit = TO_CIRCUIT(circ);
  1565. /* assert_connection_ok(conn, time(NULL)); */
  1566. circ->p_streams = ENTRY_TO_EDGE_CONN(apconn);
  1567. if (connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(apconn))) {
  1568. /* We are attaching a stream to a rendezvous circuit. That means
  1569. * that an attempt to connect to a hidden service just
  1570. * succeeded. Tell rendclient.c. */
  1571. rend_client_note_connection_attempt_ended(
  1572. ENTRY_TO_EDGE_CONN(apconn)->rend_data->onion_address);
  1573. }
  1574. if (cpath) { /* we were given one; use it */
  1575. tor_assert(cpath_is_on_circuit(circ, cpath));
  1576. } else {
  1577. /* use the last hop in the circuit */
  1578. tor_assert(circ->cpath);
  1579. tor_assert(circ->cpath->prev);
  1580. tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  1581. cpath = circ->cpath->prev;
  1582. }
  1583. ENTRY_TO_EDGE_CONN(apconn)->cpath_layer = cpath;
  1584. circ->isolation_any_streams_attached = 1;
  1585. connection_edge_update_circuit_isolation(apconn, circ, 0);
  1586. /* See if we can use optimistic data on this circuit */
  1587. if (cpath->extend_info &&
  1588. (exitnode = node_get_by_id(cpath->extend_info->identity_digest)) &&
  1589. exitnode->rs) {
  1590. /* Okay; we know what exit node this is. */
  1591. if (optimistic_data_enabled() &&
  1592. circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1593. exitnode->rs->version_supports_optimistic_data)
  1594. apconn->may_use_optimistic_data = 1;
  1595. else
  1596. apconn->may_use_optimistic_data = 0;
  1597. log_info(LD_APP, "Looks like completed circuit to %s %s allow "
  1598. "optimistic data for connection to %s",
  1599. safe_str_client(node_describe(exitnode)),
  1600. apconn->may_use_optimistic_data ? "does" : "doesn't",
  1601. safe_str_client(apconn->socks_request->address));
  1602. }
  1603. }
  1604. /** Return true iff <b>address</b> is matched by one of the entries in
  1605. * TrackHostExits. */
  1606. int
  1607. hostname_in_track_host_exits(const or_options_t *options, const char *address)
  1608. {
  1609. if (!options->TrackHostExits)
  1610. return 0;
  1611. SMARTLIST_FOREACH_BEGIN(options->TrackHostExits, const char *, cp) {
  1612. if (cp[0] == '.') { /* match end */
  1613. if (cp[1] == '\0' ||
  1614. !strcasecmpend(address, cp) ||
  1615. !strcasecmp(address, &cp[1]))
  1616. return 1;
  1617. } else if (strcasecmp(cp, address) == 0) {
  1618. return 1;
  1619. }
  1620. } SMARTLIST_FOREACH_END(cp);
  1621. return 0;
  1622. }
  1623. /** If an exit wasn't explicitly specified for <b>conn</b>, consider saving
  1624. * the exit that we *did* choose for use by future connections to
  1625. * <b>conn</b>'s destination.
  1626. */
  1627. static void
  1628. consider_recording_trackhost(const entry_connection_t *conn,
  1629. const origin_circuit_t *circ)
  1630. {
  1631. const or_options_t *options = get_options();
  1632. char *new_address = NULL;
  1633. char fp[HEX_DIGEST_LEN+1];
  1634. /* Search the addressmap for this conn's destination. */
  1635. /* If he's not in the address map.. */
  1636. if (!options->TrackHostExits ||
  1637. addressmap_have_mapping(conn->socks_request->address,
  1638. options->TrackHostExitsExpire))
  1639. return; /* nothing to track, or already mapped */
  1640. if (!hostname_in_track_host_exits(options, conn->socks_request->address) ||
  1641. !circ->build_state->chosen_exit)
  1642. return;
  1643. /* write down the fingerprint of the chosen exit, not the nickname,
  1644. * because the chosen exit might not be named. */
  1645. base16_encode(fp, sizeof(fp),
  1646. circ->build_state->chosen_exit->identity_digest, DIGEST_LEN);
  1647. /* Add this exit/hostname pair to the addressmap. */
  1648. tor_asprintf(&new_address, "%s.%s.exit",
  1649. conn->socks_request->address, fp);
  1650. addressmap_register(conn->socks_request->address, new_address,
  1651. time(NULL) + options->TrackHostExitsExpire,
  1652. ADDRMAPSRC_TRACKEXIT, 0, 0);
  1653. }
  1654. /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
  1655. * begin or resolve cell as appropriate. Return values are as for
  1656. * connection_ap_handshake_attach_circuit. The stream will exit from the hop
  1657. * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
  1658. * <b>cpath</b> is NULL. */
  1659. int
  1660. connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn,
  1661. origin_circuit_t *circ,
  1662. crypt_path_t *cpath)
  1663. {
  1664. connection_t *base_conn = ENTRY_TO_CONN(conn);
  1665. tor_assert(conn);
  1666. tor_assert(base_conn->state == AP_CONN_STATE_CIRCUIT_WAIT ||
  1667. base_conn->state == AP_CONN_STATE_CONTROLLER_WAIT);
  1668. tor_assert(conn->socks_request);
  1669. tor_assert(circ);
  1670. tor_assert(circ->base_.state == CIRCUIT_STATE_OPEN);
  1671. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  1672. if (!circ->base_.timestamp_dirty)
  1673. circ->base_.timestamp_dirty = time(NULL);
  1674. link_apconn_to_circ(conn, circ, cpath);
  1675. tor_assert(conn->socks_request);
  1676. if (conn->socks_request->command == SOCKS_COMMAND_CONNECT) {
  1677. if (!conn->use_begindir)
  1678. consider_recording_trackhost(conn, circ);
  1679. if (connection_ap_handshake_send_begin(conn) < 0)
  1680. return -1;
  1681. } else {
  1682. if (connection_ap_handshake_send_resolve(conn) < 0)
  1683. return -1;
  1684. }
  1685. return 1;
  1686. }
  1687. /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
  1688. * we don't find one: if conn cannot be handled by any known nodes,
  1689. * warn and return -1 (conn needs to die, and is maybe already marked);
  1690. * else launch new circuit (if necessary) and return 0.
  1691. * Otherwise, associate conn with a safe live circuit, do the
  1692. * right next step, and return 1.
  1693. */
  1694. /* XXXX this function should mark for close whenever it returns -1;
  1695. * its callers shouldn't have to worry about that. */
  1696. int
  1697. connection_ap_handshake_attach_circuit(entry_connection_t *conn)
  1698. {
  1699. connection_t *base_conn = ENTRY_TO_CONN(conn);
  1700. int retval;
  1701. int conn_age;
  1702. int want_onehop;
  1703. tor_assert(conn);
  1704. tor_assert(base_conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  1705. tor_assert(conn->socks_request);
  1706. want_onehop = conn->want_onehop;
  1707. conn_age = (int)(time(NULL) - base_conn->timestamp_created);
  1708. if (conn_age >= get_options()->SocksTimeout) {
  1709. int severity = (tor_addr_is_null(&base_conn->addr) && !base_conn->port) ?
  1710. LOG_INFO : LOG_NOTICE;
  1711. log_fn(severity, LD_APP,
  1712. "Tried for %d seconds to get a connection to %s:%d. Giving up.",
  1713. conn_age, safe_str_client(conn->socks_request->address),
  1714. conn->socks_request->port);
  1715. return -1;
  1716. }
  1717. if (!connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn))) {
  1718. /* we're a general conn */
  1719. origin_circuit_t *circ=NULL;
  1720. if (conn->chosen_exit_name) {
  1721. const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 1);
  1722. int opt = conn->chosen_exit_optional;
  1723. if (!node && !want_onehop) {
  1724. /* We ran into this warning when trying to extend a circuit to a
  1725. * hidden service directory for which we didn't have a router
  1726. * descriptor. See flyspray task 767 for more details. We should
  1727. * keep this in mind when deciding to use BEGIN_DIR cells for other
  1728. * directory requests as well. -KL*/
  1729. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  1730. "Requested exit point '%s' is not known. %s.",
  1731. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  1732. if (opt) {
  1733. conn->chosen_exit_optional = 0;
  1734. tor_free(conn->chosen_exit_name);
  1735. return 0;
  1736. }
  1737. return -1;
  1738. }
  1739. if (node && !connection_ap_can_use_exit(conn, node)) {
  1740. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  1741. "Requested exit point '%s' is excluded or "
  1742. "would refuse request. %s.",
  1743. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  1744. if (opt) {
  1745. conn->chosen_exit_optional = 0;
  1746. tor_free(conn->chosen_exit_name);
  1747. return 0;
  1748. }
  1749. return -1;
  1750. }
  1751. }
  1752. /* find the circuit that we should use, if there is one. */
  1753. retval = circuit_get_open_circ_or_launch(
  1754. conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
  1755. if (retval < 1) // XXX023 if we totally fail, this still returns 0 -RD
  1756. return retval;
  1757. log_debug(LD_APP|LD_CIRC,
  1758. "Attaching apconn to circ %d (stream %d sec old).",
  1759. circ->base_.n_circ_id, conn_age);
  1760. /* print the circ's path, so people can figure out which circs are
  1761. * sucking. */
  1762. circuit_log_path(LOG_INFO,LD_APP|LD_CIRC,circ);
  1763. /* We have found a suitable circuit for our conn. Hurray. */
  1764. return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
  1765. } else { /* we're a rendezvous conn */
  1766. origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
  1767. tor_assert(!ENTRY_TO_EDGE_CONN(conn)->cpath_layer);
  1768. /* start by finding a rendezvous circuit for us */
  1769. retval = circuit_get_open_circ_or_launch(
  1770. conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
  1771. if (retval < 0) return -1; /* failed */
  1772. if (retval > 0) {
  1773. tor_assert(rendcirc);
  1774. /* one is already established, attach */
  1775. log_info(LD_REND,
  1776. "rend joined circ %d already here. attaching. "
  1777. "(stream %d sec old)",
  1778. rendcirc->base_.n_circ_id, conn_age);
  1779. /* Mark rendezvous circuits as 'newly dirty' every time you use
  1780. * them, since the process of rebuilding a rendezvous circ is so
  1781. * expensive. There is a tradeoff between linkability and
  1782. * feasibility, at this point.
  1783. */
  1784. rendcirc->base_.timestamp_dirty = time(NULL);
  1785. link_apconn_to_circ(conn, rendcirc, NULL);
  1786. if (connection_ap_handshake_send_begin(conn) < 0)
  1787. return 0; /* already marked, let them fade away */
  1788. return 1;
  1789. }
  1790. if (rendcirc && (rendcirc->base_.purpose ==
  1791. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)) {
  1792. log_info(LD_REND,
  1793. "pending-join circ %d already here, with intro ack. "
  1794. "Stalling. (stream %d sec old)",
  1795. rendcirc->base_.n_circ_id, conn_age);
  1796. return 0;
  1797. }
  1798. /* it's on its way. find an intro circ. */
  1799. retval = circuit_get_open_circ_or_launch(
  1800. conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
  1801. if (retval < 0) return -1; /* failed */
  1802. if (retval > 0) {
  1803. /* one has already sent the intro. keep waiting. */
  1804. circuit_t *c = NULL;
  1805. tor_assert(introcirc);
  1806. log_info(LD_REND, "Intro circ %d present and awaiting ack (rend %d). "
  1807. "Stalling. (stream %d sec old)",
  1808. introcirc->base_.n_circ_id,
  1809. rendcirc ? rendcirc->base_.n_circ_id : 0,
  1810. conn_age);
  1811. /* abort parallel intro circs, if any */
  1812. for (c = global_circuitlist; c; c = c->next) {
  1813. if (c->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1814. !c->marked_for_close && CIRCUIT_IS_ORIGIN(c)) {
  1815. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(c);
  1816. if (oc->rend_data &&
  1817. !rend_cmp_service_ids(
  1818. ENTRY_TO_EDGE_CONN(conn)->rend_data->onion_address,
  1819. oc->rend_data->onion_address)) {
  1820. log_info(LD_REND|LD_CIRC, "Closing introduction circuit that we "
  1821. "built in parallel.");
  1822. circuit_mark_for_close(c, END_CIRC_REASON_TIMEOUT);
  1823. }
  1824. }
  1825. }
  1826. return 0;
  1827. }
  1828. /* now rendcirc and introcirc are each either undefined or not finished */
  1829. if (rendcirc && introcirc &&
  1830. rendcirc->base_.purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  1831. log_info(LD_REND,
  1832. "ready rend circ %d already here (no intro-ack yet on "
  1833. "intro %d). (stream %d sec old)",
  1834. rendcirc->base_.n_circ_id,
  1835. introcirc->base_.n_circ_id, conn_age);
  1836. tor_assert(introcirc->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  1837. if (introcirc->base_.state == CIRCUIT_STATE_OPEN) {
  1838. log_info(LD_REND,"found open intro circ %d (rend %d); sending "
  1839. "introduction. (stream %d sec old)",
  1840. introcirc->base_.n_circ_id, rendcirc->base_.n_circ_id,
  1841. conn_age);
  1842. switch (rend_client_send_introduction(introcirc, rendcirc)) {
  1843. case 0: /* success */
  1844. rendcirc->base_.timestamp_dirty = time(NULL);
  1845. introcirc->base_.timestamp_dirty = time(NULL);
  1846. assert_circuit_ok(TO_CIRCUIT(rendcirc));
  1847. assert_circuit_ok(TO_CIRCUIT(introcirc));
  1848. return 0;
  1849. case -1: /* transient error */
  1850. return 0;
  1851. case -2: /* permanent error */
  1852. return -1;
  1853. default: /* oops */
  1854. tor_fragile_assert();
  1855. return -1;
  1856. }
  1857. }
  1858. }
  1859. log_info(LD_REND, "Intro (%d) and rend (%d) circs are not both ready. "
  1860. "Stalling conn. (%d sec old)",
  1861. introcirc ? introcirc->base_.n_circ_id : 0,
  1862. rendcirc ? rendcirc->base_.n_circ_id : 0, conn_age);
  1863. return 0;
  1864. }
  1865. }
  1866. /** Change <b>circ</b>'s purpose to <b>new_purpose</b>. */
  1867. void
  1868. circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
  1869. {
  1870. uint8_t old_purpose;
  1871. /* Don't allow an OR circ to become an origin circ or vice versa. */
  1872. tor_assert(!!(CIRCUIT_IS_ORIGIN(circ)) ==
  1873. !!(CIRCUIT_PURPOSE_IS_ORIGIN(new_purpose)));
  1874. if (circ->purpose == new_purpose) return;
  1875. if (CIRCUIT_IS_ORIGIN(circ)) {
  1876. char old_purpose_desc[80] = "";
  1877. strncpy(old_purpose_desc, circuit_purpose_to_string(circ->purpose), 80-1);
  1878. old_purpose_desc[80-1] = '\0';
  1879. log_debug(LD_CIRC,
  1880. "changing purpose of origin circ %d "
  1881. "from \"%s\" (%d) to \"%s\" (%d)",
  1882. TO_ORIGIN_CIRCUIT(circ)->global_identifier,
  1883. old_purpose_desc,
  1884. circ->purpose,
  1885. circuit_purpose_to_string(new_purpose),
  1886. new_purpose);
  1887. }
  1888. old_purpose = circ->purpose;
  1889. circ->purpose = new_purpose;
  1890. if (CIRCUIT_IS_ORIGIN(circ)) {
  1891. control_event_circuit_purpose_changed(TO_ORIGIN_CIRCUIT(circ),
  1892. old_purpose);
  1893. }
  1894. }