circuituse.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805
  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-2016, 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 the right streams to
  9. * them.
  10. *
  11. * As distinct from circuitlist.c, which manages lookups to find circuits, and
  12. * circuitbuild.c, which handles the logistics of circuit construction, this
  13. * module keeps track of which streams can be attached to which circuits (in
  14. * circuit_get_best()), and attaches streams to circuits (with
  15. * circuit_try_attaching_streams(), connection_ap_handshake_attach_circuit(),
  16. * and connection_ap_handshake_attach_chosen_circuit() ).
  17. *
  18. * This module also makes sure that we are building circuits for all of the
  19. * predicted ports, using circuit_remove_handled_ports(),
  20. * circuit_stream_is_being_handled(), and circuit_build_needed_cirs(). It
  21. * handles launching circuits for specific targets using
  22. * circuit_launch_by_extend_info().
  23. *
  24. * This is also where we handle expiring circuits that have been around for
  25. * too long without actually completing, along with the circuit_build_timeout
  26. * logic in circuitstats.c.
  27. **/
  28. #include "or.h"
  29. #include "addressmap.h"
  30. #include "bridges.h"
  31. #include "channel.h"
  32. #include "circpathbias.h"
  33. #include "circuitbuild.h"
  34. #include "circuitlist.h"
  35. #include "circuitstats.h"
  36. #include "circuituse.h"
  37. #include "config.h"
  38. #include "connection.h"
  39. #include "connection_edge.h"
  40. #include "control.h"
  41. #include "entrynodes.h"
  42. #include "hs_common.h"
  43. #include "nodelist.h"
  44. #include "networkstatus.h"
  45. #include "policies.h"
  46. #include "rendclient.h"
  47. #include "rendcommon.h"
  48. #include "rendservice.h"
  49. #include "rephist.h"
  50. #include "router.h"
  51. #include "routerlist.h"
  52. static void circuit_expire_old_circuits_clientside(void);
  53. static void circuit_increment_failure_count(void);
  54. /** Return 1 if <b>circ</b> could be returned by circuit_get_best().
  55. * Else return 0.
  56. */
  57. static int
  58. circuit_is_acceptable(const origin_circuit_t *origin_circ,
  59. const entry_connection_t *conn,
  60. int must_be_open, uint8_t purpose,
  61. int need_uptime, int need_internal,
  62. time_t now)
  63. {
  64. const circuit_t *circ = TO_CIRCUIT(origin_circ);
  65. const node_t *exitnode;
  66. cpath_build_state_t *build_state;
  67. tor_assert(circ);
  68. tor_assert(conn);
  69. tor_assert(conn->socks_request);
  70. if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_chan))
  71. return 0; /* ignore non-open circs */
  72. if (circ->marked_for_close)
  73. return 0;
  74. /* if this circ isn't our purpose, skip. */
  75. if (purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
  76. if (circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  77. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  78. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
  79. circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
  80. return 0;
  81. } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
  82. !must_be_open) {
  83. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  84. circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  85. return 0;
  86. } else {
  87. if (purpose != circ->purpose)
  88. return 0;
  89. }
  90. /* If this is a timed-out hidden service circuit, skip it. */
  91. if (origin_circ->hs_circ_has_timed_out) {
  92. return 0;
  93. }
  94. if (purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  95. purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
  96. if (circ->timestamp_dirty &&
  97. circ->timestamp_dirty+get_options()->MaxCircuitDirtiness <= now)
  98. return 0;
  99. }
  100. if (origin_circ->unusable_for_new_conns)
  101. return 0;
  102. /* decide if this circ is suitable for this conn */
  103. /* for rend circs, circ->cpath->prev is not the last router in the
  104. * circuit, it's the magical extra service hop. so just check the nickname
  105. * of the one we meant to finish at.
  106. */
  107. build_state = origin_circ->build_state;
  108. exitnode = build_state_get_exit_node(build_state);
  109. if (need_uptime && !build_state->need_uptime)
  110. return 0;
  111. if (need_internal != build_state->is_internal)
  112. return 0;
  113. if (purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  114. tor_addr_t addr;
  115. const int family = tor_addr_parse(&addr, conn->socks_request->address);
  116. if (!exitnode && !build_state->onehop_tunnel) {
  117. log_debug(LD_CIRC,"Not considering circuit with unknown router.");
  118. return 0; /* this circuit is screwed and doesn't know it yet,
  119. * or is a rendezvous circuit. */
  120. }
  121. if (build_state->onehop_tunnel) {
  122. if (!conn->want_onehop) {
  123. log_debug(LD_CIRC,"Skipping one-hop circuit.");
  124. return 0;
  125. }
  126. tor_assert(conn->chosen_exit_name);
  127. if (build_state->chosen_exit) {
  128. char digest[DIGEST_LEN];
  129. if (hexdigest_to_digest(conn->chosen_exit_name, digest) < 0)
  130. return 0; /* broken digest, we don't want it */
  131. if (tor_memneq(digest, build_state->chosen_exit->identity_digest,
  132. DIGEST_LEN))
  133. return 0; /* this is a circuit to somewhere else */
  134. if (tor_digest_is_zero(digest)) {
  135. /* we don't know the digest; have to compare addr:port */
  136. if (family < 0 ||
  137. !tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
  138. build_state->chosen_exit->port != conn->socks_request->port)
  139. return 0;
  140. }
  141. }
  142. } else {
  143. if (conn->want_onehop) {
  144. /* don't use three-hop circuits -- that could hurt our anonymity. */
  145. return 0;
  146. }
  147. }
  148. if (origin_circ->prepend_policy && family != -1) {
  149. int r = compare_tor_addr_to_addr_policy(&addr,
  150. conn->socks_request->port,
  151. origin_circ->prepend_policy);
  152. if (r == ADDR_POLICY_REJECTED)
  153. return 0;
  154. }
  155. if (exitnode && !connection_ap_can_use_exit(conn, exitnode)) {
  156. /* can't exit from this router */
  157. return 0;
  158. }
  159. } else { /* not general */
  160. const edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
  161. if ((edge_conn->rend_data && !origin_circ->rend_data) ||
  162. (!edge_conn->rend_data && origin_circ->rend_data) ||
  163. (edge_conn->rend_data && origin_circ->rend_data &&
  164. rend_cmp_service_ids(rend_data_get_address(edge_conn->rend_data),
  165. rend_data_get_address(origin_circ->rend_data)))) {
  166. /* this circ is not for this conn */
  167. return 0;
  168. }
  169. }
  170. if (!connection_edge_compatible_with_circuit(conn, origin_circ)) {
  171. /* conn needs to be isolated from other conns that have already used
  172. * origin_circ */
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. /** Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
  178. * <b>conn</b>, and return 0 otherwise. Used by circuit_get_best.
  179. */
  180. static int
  181. circuit_is_better(const origin_circuit_t *oa, const origin_circuit_t *ob,
  182. const entry_connection_t *conn)
  183. {
  184. const circuit_t *a = TO_CIRCUIT(oa);
  185. const circuit_t *b = TO_CIRCUIT(ob);
  186. const uint8_t purpose = ENTRY_TO_CONN(conn)->purpose;
  187. int a_bits, b_bits;
  188. /* If one of the circuits was allowed to live due to relaxing its timeout,
  189. * it is definitely worse (it's probably a much slower path). */
  190. if (oa->relaxed_timeout && !ob->relaxed_timeout)
  191. return 0; /* ob is better. It's not relaxed. */
  192. if (!oa->relaxed_timeout && ob->relaxed_timeout)
  193. return 1; /* oa is better. It's not relaxed. */
  194. switch (purpose) {
  195. case CIRCUIT_PURPOSE_C_GENERAL:
  196. /* if it's used but less dirty it's best;
  197. * else if it's more recently created it's best
  198. */
  199. if (b->timestamp_dirty) {
  200. if (a->timestamp_dirty &&
  201. a->timestamp_dirty > b->timestamp_dirty)
  202. return 1;
  203. } else {
  204. if (a->timestamp_dirty ||
  205. timercmp(&a->timestamp_began, &b->timestamp_began, OP_GT))
  206. return 1;
  207. if (ob->build_state->is_internal)
  208. /* XXXX++ what the heck is this internal thing doing here. I
  209. * think we can get rid of it. circuit_is_acceptable() already
  210. * makes sure that is_internal is exactly what we need it to
  211. * be. -RD */
  212. return 1;
  213. }
  214. break;
  215. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  216. /* the closer it is to ack_wait the better it is */
  217. if (a->purpose > b->purpose)
  218. return 1;
  219. break;
  220. case CIRCUIT_PURPOSE_C_REND_JOINED:
  221. /* the closer it is to rend_joined the better it is */
  222. if (a->purpose > b->purpose)
  223. return 1;
  224. break;
  225. }
  226. /* XXXX Maybe this check should get a higher priority to avoid
  227. * using up circuits too rapidly. */
  228. a_bits = connection_edge_update_circuit_isolation(conn,
  229. (origin_circuit_t*)oa, 1);
  230. b_bits = connection_edge_update_circuit_isolation(conn,
  231. (origin_circuit_t*)ob, 1);
  232. /* if x_bits < 0, then we have not used x for anything; better not to dirty
  233. * a connection if we can help it. */
  234. if (a_bits < 0) {
  235. return 0;
  236. } else if (b_bits < 0) {
  237. return 1;
  238. }
  239. a_bits &= ~ oa->isolation_flags_mixed;
  240. a_bits &= ~ ob->isolation_flags_mixed;
  241. if (n_bits_set_u8(a_bits) < n_bits_set_u8(b_bits)) {
  242. /* The fewer new restrictions we need to make on a circuit for stream
  243. * isolation, the better. */
  244. return 1;
  245. }
  246. return 0;
  247. }
  248. /** Find the best circ that conn can use, preferably one which is
  249. * dirty. Circ must not be too old.
  250. *
  251. * Conn must be defined.
  252. *
  253. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  254. *
  255. * circ_purpose specifies what sort of circuit we must have.
  256. * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
  257. *
  258. * If it's REND_JOINED and must_be_open==0, then return the closest
  259. * rendezvous-purposed circuit that you can find.
  260. *
  261. * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
  262. * closest introduce-purposed circuit that you can find.
  263. */
  264. static origin_circuit_t *
  265. circuit_get_best(const entry_connection_t *conn,
  266. int must_be_open, uint8_t purpose,
  267. int need_uptime, int need_internal)
  268. {
  269. origin_circuit_t *best=NULL;
  270. struct timeval now;
  271. int intro_going_on_but_too_old = 0;
  272. tor_assert(conn);
  273. tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  274. purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
  275. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  276. tor_gettimeofday(&now);
  277. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  278. origin_circuit_t *origin_circ;
  279. if (!CIRCUIT_IS_ORIGIN(circ))
  280. continue;
  281. origin_circ = TO_ORIGIN_CIRCUIT(circ);
  282. /* Log an info message if we're going to launch a new intro circ in
  283. * parallel */
  284. if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT &&
  285. !must_be_open && origin_circ->hs_circ_has_timed_out) {
  286. intro_going_on_but_too_old = 1;
  287. continue;
  288. }
  289. if (!circuit_is_acceptable(origin_circ,conn,must_be_open,purpose,
  290. need_uptime,need_internal, (time_t)now.tv_sec))
  291. continue;
  292. /* now this is an acceptable circ to hand back. but that doesn't
  293. * mean it's the *best* circ to hand back. try to decide.
  294. */
  295. if (!best || circuit_is_better(origin_circ,best,conn))
  296. best = origin_circ;
  297. }
  298. SMARTLIST_FOREACH_END(circ);
  299. if (!best && intro_going_on_but_too_old)
  300. log_info(LD_REND|LD_CIRC, "There is an intro circuit being created "
  301. "right now, but it has already taken quite a while. Starting "
  302. "one in parallel.");
  303. return best;
  304. }
  305. /** Return the number of not-yet-open general-purpose origin circuits. */
  306. static int
  307. count_pending_general_client_circuits(void)
  308. {
  309. int count = 0;
  310. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  311. if (circ->marked_for_close ||
  312. circ->state == CIRCUIT_STATE_OPEN ||
  313. circ->purpose != CIRCUIT_PURPOSE_C_GENERAL ||
  314. !CIRCUIT_IS_ORIGIN(circ))
  315. continue;
  316. ++count;
  317. }
  318. SMARTLIST_FOREACH_END(circ);
  319. return count;
  320. }
  321. #if 0
  322. /** Check whether, according to the policies in <b>options</b>, the
  323. * circuit <b>circ</b> makes sense. */
  324. /* XXXX currently only checks Exclude{Exit}Nodes; it should check more.
  325. * Also, it doesn't have the right definition of an exit circuit. Also,
  326. * it's never called. */
  327. int
  328. circuit_conforms_to_options(const origin_circuit_t *circ,
  329. const or_options_t *options)
  330. {
  331. const crypt_path_t *cpath, *cpath_next = NULL;
  332. /* first check if it includes any excluded nodes */
  333. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  334. cpath_next = cpath->next;
  335. if (routerset_contains_extendinfo(options->ExcludeNodes,
  336. cpath->extend_info))
  337. return 0;
  338. }
  339. /* then consider the final hop */
  340. if (routerset_contains_extendinfo(options->ExcludeExitNodes,
  341. circ->cpath->prev->extend_info))
  342. return 0;
  343. return 1;
  344. }
  345. #endif
  346. /** Close all circuits that start at us, aren't open, and were born
  347. * at least CircuitBuildTimeout seconds ago.
  348. */
  349. void
  350. circuit_expire_building(void)
  351. {
  352. /* circ_times.timeout_ms and circ_times.close_ms are from
  353. * circuit_build_times_get_initial_timeout() if we haven't computed
  354. * custom timeouts yet */
  355. struct timeval general_cutoff, begindir_cutoff, fourhop_cutoff,
  356. close_cutoff, extremely_old_cutoff, hs_extremely_old_cutoff,
  357. cannibalized_cutoff, c_intro_cutoff, s_intro_cutoff, stream_cutoff;
  358. const or_options_t *options = get_options();
  359. struct timeval now;
  360. cpath_build_state_t *build_state;
  361. int any_opened_circs = 0;
  362. tor_gettimeofday(&now);
  363. /* Check to see if we have any opened circuits. If we don't,
  364. * we want to be more lenient with timeouts, in case the
  365. * user has relocated and/or changed network connections.
  366. * See bug #3443. */
  367. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, next_circ) {
  368. if (!CIRCUIT_IS_ORIGIN(next_circ) || /* didn't originate here */
  369. next_circ->marked_for_close) { /* don't mess with marked circs */
  370. continue;
  371. }
  372. if (TO_ORIGIN_CIRCUIT(next_circ)->has_opened &&
  373. next_circ->state == CIRCUIT_STATE_OPEN &&
  374. TO_ORIGIN_CIRCUIT(next_circ)->build_state &&
  375. TO_ORIGIN_CIRCUIT(next_circ)->build_state->desired_path_len
  376. == DEFAULT_ROUTE_LEN) {
  377. any_opened_circs = 1;
  378. break;
  379. }
  380. } SMARTLIST_FOREACH_END(next_circ);
  381. #define SET_CUTOFF(target, msec) do { \
  382. long ms = tor_lround(msec); \
  383. struct timeval diff; \
  384. diff.tv_sec = ms / 1000; \
  385. diff.tv_usec = (int)((ms % 1000) * 1000); \
  386. timersub(&now, &diff, &target); \
  387. } while (0)
  388. /**
  389. * Because circuit build timeout is calculated only based on 3 hop
  390. * general purpose circuit construction, we need to scale the timeout
  391. * to make it properly apply to longer circuits, and circuits of
  392. * certain usage types. The following diagram illustrates how we
  393. * derive the scaling below. In short, we calculate the number
  394. * of times our telescoping-based circuit construction causes cells
  395. * to traverse each link for the circuit purpose types in question,
  396. * and then assume each link is equivalent.
  397. *
  398. * OP --a--> A --b--> B --c--> C
  399. * OP --a--> A --b--> B --c--> C --d--> D
  400. *
  401. * Let h = a = b = c = d
  402. *
  403. * Three hops (general_cutoff)
  404. * RTTs = 3a + 2b + c
  405. * RTTs = 6h
  406. * Cannibalized:
  407. * RTTs = a+b+c+d
  408. * RTTs = 4h
  409. * Four hops:
  410. * RTTs = 4a + 3b + 2c + d
  411. * RTTs = 10h
  412. * Client INTRODUCE1+ACK: // XXX: correct?
  413. * RTTs = 5a + 4b + 3c + 2d
  414. * RTTs = 14h
  415. * Server intro:
  416. * RTTs = 4a + 3b + 2c
  417. * RTTs = 9h
  418. */
  419. SET_CUTOFF(general_cutoff, get_circuit_build_timeout_ms());
  420. SET_CUTOFF(begindir_cutoff, get_circuit_build_timeout_ms());
  421. /* > 3hop circs seem to have a 1.0 second delay on their cannibalized
  422. * 4th hop. */
  423. SET_CUTOFF(fourhop_cutoff, get_circuit_build_timeout_ms() * (10/6.0) + 1000);
  424. /* CIRCUIT_PURPOSE_C_ESTABLISH_REND behaves more like a RELAY cell.
  425. * Use the stream cutoff (more or less). */
  426. SET_CUTOFF(stream_cutoff, MAX(options->CircuitStreamTimeout,15)*1000 + 1000);
  427. /* Be lenient with cannibalized circs. They already survived the official
  428. * CBT, and they're usually not performance-critical. */
  429. SET_CUTOFF(cannibalized_cutoff,
  430. MAX(get_circuit_build_close_time_ms()*(4/6.0),
  431. options->CircuitStreamTimeout * 1000) + 1000);
  432. /* Intro circs have an extra round trip (and are also 4 hops long) */
  433. SET_CUTOFF(c_intro_cutoff, get_circuit_build_timeout_ms() * (14/6.0) + 1000);
  434. /* Server intro circs have an extra round trip */
  435. SET_CUTOFF(s_intro_cutoff, get_circuit_build_timeout_ms() * (9/6.0) + 1000);
  436. SET_CUTOFF(close_cutoff, get_circuit_build_close_time_ms());
  437. SET_CUTOFF(extremely_old_cutoff, get_circuit_build_close_time_ms()*2 + 1000);
  438. SET_CUTOFF(hs_extremely_old_cutoff,
  439. MAX(get_circuit_build_close_time_ms()*2 + 1000,
  440. options->SocksTimeout * 1000));
  441. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *,victim) {
  442. struct timeval cutoff;
  443. if (!CIRCUIT_IS_ORIGIN(victim) || /* didn't originate here */
  444. victim->marked_for_close) /* don't mess with marked circs */
  445. continue;
  446. /* If we haven't yet started the first hop, it means we don't have
  447. * any orconns available, and thus have not started counting time yet
  448. * for this circuit. See circuit_deliver_create_cell() and uses of
  449. * timestamp_began.
  450. *
  451. * Continue to wait in this case. The ORConn should timeout
  452. * independently and kill us then.
  453. */
  454. if (TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_CLOSED) {
  455. continue;
  456. }
  457. build_state = TO_ORIGIN_CIRCUIT(victim)->build_state;
  458. if (build_state && build_state->onehop_tunnel)
  459. cutoff = begindir_cutoff;
  460. else if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
  461. cutoff = close_cutoff;
  462. else if (victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  463. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  464. cutoff = c_intro_cutoff;
  465. else if (victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
  466. cutoff = s_intro_cutoff;
  467. else if (victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND)
  468. cutoff = stream_cutoff;
  469. else if (victim->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  470. cutoff = close_cutoff;
  471. else if (TO_ORIGIN_CIRCUIT(victim)->has_opened &&
  472. victim->state != CIRCUIT_STATE_OPEN)
  473. cutoff = cannibalized_cutoff;
  474. else if (build_state && build_state->desired_path_len >= 4)
  475. cutoff = fourhop_cutoff;
  476. else
  477. cutoff = general_cutoff;
  478. if (TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)
  479. cutoff = hs_extremely_old_cutoff;
  480. if (timercmp(&victim->timestamp_began, &cutoff, OP_GT))
  481. continue; /* it's still young, leave it alone */
  482. /* We need to double-check the opened state here because
  483. * we don't want to consider opened 1-hop dircon circuits for
  484. * deciding when to relax the timeout, but we *do* want to relax
  485. * those circuits too if nothing else is opened *and* they still
  486. * aren't either. */
  487. if (!any_opened_circs && victim->state != CIRCUIT_STATE_OPEN) {
  488. /* It's still young enough that we wouldn't close it, right? */
  489. if (timercmp(&victim->timestamp_began, &close_cutoff, OP_GT)) {
  490. if (!TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout) {
  491. int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath->state
  492. == CPATH_STATE_OPEN;
  493. log_info(LD_CIRC,
  494. "No circuits are opened. Relaxing timeout for circuit %d "
  495. "(a %s %d-hop circuit in state %s with channel state %s).",
  496. TO_ORIGIN_CIRCUIT(victim)->global_identifier,
  497. circuit_purpose_to_string(victim->purpose),
  498. TO_ORIGIN_CIRCUIT(victim)->build_state ?
  499. TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
  500. -1,
  501. circuit_state_to_string(victim->state),
  502. channel_state_to_string(victim->n_chan->state));
  503. /* We count the timeout here for CBT, because technically this
  504. * was a timeout, and the timeout value needs to reset if we
  505. * see enough of them. Note this means we also need to avoid
  506. * double-counting below, too. */
  507. circuit_build_times_count_timeout(get_circuit_build_times_mutable(),
  508. first_hop_succeeded);
  509. TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout = 1;
  510. }
  511. continue;
  512. } else {
  513. static ratelim_t relax_timeout_limit = RATELIM_INIT(3600);
  514. const double build_close_ms = get_circuit_build_close_time_ms();
  515. log_fn_ratelim(&relax_timeout_limit, LOG_NOTICE, LD_CIRC,
  516. "No circuits are opened. Relaxed timeout for circuit %d "
  517. "(a %s %d-hop circuit in state %s with channel state %s) to "
  518. "%ldms. However, it appears the circuit has timed out "
  519. "anyway.",
  520. TO_ORIGIN_CIRCUIT(victim)->global_identifier,
  521. circuit_purpose_to_string(victim->purpose),
  522. TO_ORIGIN_CIRCUIT(victim)->build_state ?
  523. TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
  524. -1,
  525. circuit_state_to_string(victim->state),
  526. channel_state_to_string(victim->n_chan->state),
  527. (long)build_close_ms);
  528. }
  529. }
  530. #if 0
  531. /* some debug logs, to help track bugs */
  532. if (victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  533. victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  534. if (!victim->timestamp_dirty)
  535. log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d)."
  536. "(clean).",
  537. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  538. victim->purpose, victim->build_state->chosen_exit_name,
  539. victim->n_circ_id);
  540. else
  541. log_fn(LOG_DEBUG,"Considering %sopen purpose %d to %s (circid %d). "
  542. "%d secs since dirty.",
  543. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  544. victim->purpose, victim->build_state->chosen_exit_name,
  545. victim->n_circ_id,
  546. (int)(now - victim->timestamp_dirty));
  547. }
  548. #endif
  549. /* if circ is !open, or if it's open but purpose is a non-finished
  550. * intro or rend, then mark it for close */
  551. if (victim->state == CIRCUIT_STATE_OPEN) {
  552. switch (victim->purpose) {
  553. default: /* most open circuits can be left alone. */
  554. continue; /* yes, continue inside a switch refers to the nearest
  555. * enclosing loop. C is smart. */
  556. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  557. break; /* too old, need to die */
  558. case CIRCUIT_PURPOSE_C_REND_READY:
  559. /* it's a rend_ready circ -- has it already picked a query? */
  560. /* c_rend_ready circs measure age since timestamp_dirty,
  561. * because that's set when they switch purposes
  562. */
  563. if (TO_ORIGIN_CIRCUIT(victim)->rend_data ||
  564. victim->timestamp_dirty > cutoff.tv_sec)
  565. continue;
  566. break;
  567. case CIRCUIT_PURPOSE_PATH_BIAS_TESTING:
  568. /* Open path bias testing circuits are given a long
  569. * time to complete the test, but not forever */
  570. TO_ORIGIN_CIRCUIT(victim)->path_state = PATH_STATE_USE_FAILED;
  571. break;
  572. case CIRCUIT_PURPOSE_C_INTRODUCING:
  573. /* We keep old introducing circuits around for
  574. * a while in parallel, and they can end up "opened".
  575. * We decide below if we're going to mark them timed
  576. * out and eventually close them.
  577. */
  578. break;
  579. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  580. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  581. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  582. /* rend and intro circs become dirty each time they
  583. * make an introduction attempt. so timestamp_dirty
  584. * will reflect the time since the last attempt.
  585. */
  586. if (victim->timestamp_dirty > cutoff.tv_sec)
  587. continue;
  588. break;
  589. }
  590. } else { /* circuit not open, consider recording failure as timeout */
  591. int first_hop_succeeded = TO_ORIGIN_CIRCUIT(victim)->cpath &&
  592. TO_ORIGIN_CIRCUIT(victim)->cpath->state == CPATH_STATE_OPEN;
  593. if (TO_ORIGIN_CIRCUIT(victim)->p_streams != NULL) {
  594. log_warn(LD_BUG, "Circuit %d (purpose %d, %s) has timed out, "
  595. "yet has attached streams!",
  596. TO_ORIGIN_CIRCUIT(victim)->global_identifier,
  597. victim->purpose,
  598. circuit_purpose_to_string(victim->purpose));
  599. tor_fragile_assert();
  600. continue;
  601. }
  602. if (circuit_timeout_want_to_count_circ(TO_ORIGIN_CIRCUIT(victim)) &&
  603. circuit_build_times_enough_to_compute(get_circuit_build_times())) {
  604. /* Circuits are allowed to last longer for measurement.
  605. * Switch their purpose and wait. */
  606. if (victim->purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
  607. control_event_circuit_status(TO_ORIGIN_CIRCUIT(victim),
  608. CIRC_EVENT_FAILED,
  609. END_CIRC_REASON_TIMEOUT);
  610. circuit_change_purpose(victim, CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  611. /* Record this failure to check for too many timeouts
  612. * in a row. This function does not record a time value yet
  613. * (we do that later); it only counts the fact that we did
  614. * have a timeout. We also want to avoid double-counting
  615. * already "relaxed" circuits, which are counted above. */
  616. if (!TO_ORIGIN_CIRCUIT(victim)->relaxed_timeout) {
  617. circuit_build_times_count_timeout(
  618. get_circuit_build_times_mutable(),
  619. first_hop_succeeded);
  620. }
  621. continue;
  622. }
  623. /*
  624. * If the circuit build time is much greater than we would have cut
  625. * it off at, we probably had a suspend event along this codepath,
  626. * and we should discard the value.
  627. */
  628. if (timercmp(&victim->timestamp_began, &extremely_old_cutoff, OP_LT)) {
  629. log_notice(LD_CIRC,
  630. "Extremely large value for circuit build timeout: %lds. "
  631. "Assuming clock jump. Purpose %d (%s)",
  632. (long)(now.tv_sec - victim->timestamp_began.tv_sec),
  633. victim->purpose,
  634. circuit_purpose_to_string(victim->purpose));
  635. } else if (circuit_build_times_count_close(
  636. get_circuit_build_times_mutable(),
  637. first_hop_succeeded,
  638. (time_t)victim->timestamp_created.tv_sec)) {
  639. circuit_build_times_set_timeout(get_circuit_build_times_mutable());
  640. }
  641. }
  642. }
  643. /* If this is a hidden service client circuit which is far enough
  644. * along in connecting to its destination, and we haven't already
  645. * flagged it as 'timed out', and the user has not told us to
  646. * close such circs immediately on timeout, flag it as 'timed out'
  647. * so we'll launch another intro or rend circ, but don't mark it
  648. * for close yet.
  649. *
  650. * (Circs flagged as 'timed out' are given a much longer timeout
  651. * period above, so we won't close them in the next call to
  652. * circuit_expire_building.) */
  653. if (!(options->CloseHSClientCircuitsImmediatelyOnTimeout) &&
  654. !(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out)) {
  655. switch (victim->purpose) {
  656. case CIRCUIT_PURPOSE_C_REND_READY:
  657. /* We only want to spare a rend circ if it has been specified in
  658. * an INTRODUCE1 cell sent to a hidden service. A circ's
  659. * pending_final_cpath field is non-NULL iff it is a rend circ
  660. * and we have tried to send an INTRODUCE1 cell specifying it.
  661. * Thus, if the pending_final_cpath field *is* NULL, then we
  662. * want to not spare it. */
  663. if (TO_ORIGIN_CIRCUIT(victim)->build_state &&
  664. TO_ORIGIN_CIRCUIT(victim)->build_state->pending_final_cpath ==
  665. NULL)
  666. break;
  667. /* fallthrough! */
  668. case CIRCUIT_PURPOSE_C_INTRODUCING:
  669. /* connection_ap_handshake_attach_circuit() will relaunch for us */
  670. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  671. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  672. /* If we have reached this line, we want to spare the circ for now. */
  673. log_info(LD_CIRC,"Marking circ %u (state %d:%s, purpose %d) "
  674. "as timed-out HS circ",
  675. (unsigned)victim->n_circ_id,
  676. victim->state, circuit_state_to_string(victim->state),
  677. victim->purpose);
  678. TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out = 1;
  679. continue;
  680. default:
  681. break;
  682. }
  683. }
  684. /* If this is a service-side rendezvous circuit which is far
  685. * enough along in connecting to its destination, consider sparing
  686. * it. */
  687. if (!(options->CloseHSServiceRendCircuitsImmediatelyOnTimeout) &&
  688. !(TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out) &&
  689. victim->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
  690. log_info(LD_CIRC,"Marking circ %u (state %d:%s, purpose %d) "
  691. "as timed-out HS circ; relaunching rendezvous attempt.",
  692. (unsigned)victim->n_circ_id,
  693. victim->state, circuit_state_to_string(victim->state),
  694. victim->purpose);
  695. TO_ORIGIN_CIRCUIT(victim)->hs_circ_has_timed_out = 1;
  696. rend_service_relaunch_rendezvous(TO_ORIGIN_CIRCUIT(victim));
  697. continue;
  698. }
  699. if (victim->n_chan)
  700. log_info(LD_CIRC,
  701. "Abandoning circ %u %s:%u (state %d,%d:%s, purpose %d, "
  702. "len %d)", TO_ORIGIN_CIRCUIT(victim)->global_identifier,
  703. channel_get_canonical_remote_descr(victim->n_chan),
  704. (unsigned)victim->n_circ_id,
  705. TO_ORIGIN_CIRCUIT(victim)->has_opened,
  706. victim->state, circuit_state_to_string(victim->state),
  707. victim->purpose,
  708. TO_ORIGIN_CIRCUIT(victim)->build_state ?
  709. TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
  710. -1);
  711. else
  712. log_info(LD_CIRC,
  713. "Abandoning circ %u %u (state %d,%d:%s, purpose %d, len %d)",
  714. TO_ORIGIN_CIRCUIT(victim)->global_identifier,
  715. (unsigned)victim->n_circ_id,
  716. TO_ORIGIN_CIRCUIT(victim)->has_opened,
  717. victim->state,
  718. circuit_state_to_string(victim->state), victim->purpose,
  719. TO_ORIGIN_CIRCUIT(victim)->build_state ?
  720. TO_ORIGIN_CIRCUIT(victim)->build_state->desired_path_len :
  721. -1);
  722. circuit_log_path(LOG_INFO,LD_CIRC,TO_ORIGIN_CIRCUIT(victim));
  723. if (victim->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT)
  724. circuit_mark_for_close(victim, END_CIRC_REASON_MEASUREMENT_EXPIRED);
  725. else
  726. circuit_mark_for_close(victim, END_CIRC_REASON_TIMEOUT);
  727. pathbias_count_timeout(TO_ORIGIN_CIRCUIT(victim));
  728. } SMARTLIST_FOREACH_END(victim);
  729. }
  730. /**
  731. * Mark for close all circuits that start here, that were built through a
  732. * guard we weren't sure if we wanted to use, and that have been waiting
  733. * around for way too long.
  734. */
  735. void
  736. circuit_expire_waiting_for_better_guard(void)
  737. {
  738. SMARTLIST_FOREACH_BEGIN(circuit_get_global_origin_circuit_list(),
  739. origin_circuit_t *, circ) {
  740. if (TO_CIRCUIT(circ)->marked_for_close)
  741. continue;
  742. if (circ->guard_state == NULL)
  743. continue;
  744. if (entry_guard_state_should_expire(circ->guard_state))
  745. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NONE);
  746. } SMARTLIST_FOREACH_END(circ);
  747. }
  748. /** For debugging #8387: track when we last called
  749. * circuit_expire_old_circuits_clientside. */
  750. static time_t last_expired_clientside_circuits = 0;
  751. /**
  752. * As a diagnostic for bug 8387, log information about how many one-hop
  753. * circuits we have around that have been there for at least <b>age</b>
  754. * seconds. Log a few of them.
  755. * Ignores Single Onion Service intro and Tor2web redezvous circuits, they are
  756. * expected to be long-term one-hop circuits.
  757. */
  758. void
  759. circuit_log_ancient_one_hop_circuits(int age)
  760. {
  761. #define MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG 10
  762. time_t now = time(NULL);
  763. time_t cutoff = now - age;
  764. int n_found = 0;
  765. smartlist_t *log_these = smartlist_new();
  766. const or_options_t *options = get_options();
  767. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  768. const origin_circuit_t *ocirc;
  769. if (! CIRCUIT_IS_ORIGIN(circ))
  770. continue;
  771. if (circ->timestamp_created.tv_sec >= cutoff)
  772. continue;
  773. /* Single Onion Services deliberately make long term one-hop intro
  774. * connections. We only ignore active intro point connections, if we take
  775. * a long time establishing, that's worth logging. */
  776. if (rend_service_allow_non_anonymous_connection(options) &&
  777. circ->purpose == CIRCUIT_PURPOSE_S_INTRO)
  778. continue;
  779. /* Tor2web deliberately makes long term one-hop rend connections,
  780. * particularly when Tor2webRendezvousPoints is used. We only ignore
  781. * active rend point connections, if we take a long time to rendezvous,
  782. * that's worth logging. */
  783. if (rend_client_allow_non_anonymous_connection(options) &&
  784. circ->purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  785. continue;
  786. ocirc = CONST_TO_ORIGIN_CIRCUIT(circ);
  787. if (ocirc->build_state && ocirc->build_state->onehop_tunnel) {
  788. ++n_found;
  789. if (smartlist_len(log_these) < MAX_ANCIENT_ONEHOP_CIRCUITS_TO_LOG)
  790. smartlist_add(log_these, (origin_circuit_t*) ocirc);
  791. }
  792. }
  793. SMARTLIST_FOREACH_END(circ);
  794. if (n_found == 0)
  795. goto done;
  796. log_notice(LD_HEARTBEAT,
  797. "Diagnostic for issue 8387: Found %d one-hop circuits more "
  798. "than %d seconds old! Logging %d...",
  799. n_found, age, smartlist_len(log_these));
  800. SMARTLIST_FOREACH_BEGIN(log_these, const origin_circuit_t *, ocirc) {
  801. char created[ISO_TIME_LEN+1];
  802. int stream_num;
  803. const edge_connection_t *conn;
  804. char *dirty = NULL;
  805. const circuit_t *circ = TO_CIRCUIT(ocirc);
  806. format_local_iso_time(created,
  807. (time_t)circ->timestamp_created.tv_sec);
  808. if (circ->timestamp_dirty) {
  809. char dirty_since[ISO_TIME_LEN+1];
  810. format_local_iso_time(dirty_since, circ->timestamp_dirty);
  811. tor_asprintf(&dirty, "Dirty since %s (%ld seconds vs %ld-second cutoff)",
  812. dirty_since, (long)(now - circ->timestamp_dirty),
  813. (long) options->MaxCircuitDirtiness);
  814. } else {
  815. dirty = tor_strdup("Not marked dirty");
  816. }
  817. log_notice(LD_HEARTBEAT, " #%d created at %s. %s, %s. %s for close. "
  818. "Package window: %d. "
  819. "%s for new conns. %s.",
  820. ocirc_sl_idx,
  821. created,
  822. circuit_state_to_string(circ->state),
  823. circuit_purpose_to_string(circ->purpose),
  824. circ->marked_for_close ? "Marked" : "Not marked",
  825. circ->package_window,
  826. ocirc->unusable_for_new_conns ? "Not usable" : "usable",
  827. dirty);
  828. tor_free(dirty);
  829. stream_num = 0;
  830. for (conn = ocirc->p_streams; conn; conn = conn->next_stream) {
  831. const connection_t *c = TO_CONN(conn);
  832. char stream_created[ISO_TIME_LEN+1];
  833. if (++stream_num >= 5)
  834. break;
  835. format_local_iso_time(stream_created, c->timestamp_created);
  836. log_notice(LD_HEARTBEAT, " Stream#%d created at %s. "
  837. "%s conn in state %s. "
  838. "It is %slinked and %sreading from a linked connection %p. "
  839. "Package window %d. "
  840. "%s for close (%s:%d). Hold-open is %sset. "
  841. "Has %ssent RELAY_END. %s on circuit.",
  842. stream_num,
  843. stream_created,
  844. conn_type_to_string(c->type),
  845. conn_state_to_string(c->type, c->state),
  846. c->linked ? "" : "not ",
  847. c->reading_from_linked_conn ? "": "not",
  848. c->linked_conn,
  849. conn->package_window,
  850. c->marked_for_close ? "Marked" : "Not marked",
  851. c->marked_for_close_file ? c->marked_for_close_file : "--",
  852. c->marked_for_close,
  853. c->hold_open_until_flushed ? "" : "not ",
  854. conn->edge_has_sent_end ? "" : "not ",
  855. conn->edge_blocked_on_circ ? "Blocked" : "Not blocked");
  856. if (! c->linked_conn)
  857. continue;
  858. c = c->linked_conn;
  859. log_notice(LD_HEARTBEAT, " Linked to %s connection in state %s "
  860. "(Purpose %d). %s for close (%s:%d). Hold-open is %sset. ",
  861. conn_type_to_string(c->type),
  862. conn_state_to_string(c->type, c->state),
  863. c->purpose,
  864. c->marked_for_close ? "Marked" : "Not marked",
  865. c->marked_for_close_file ? c->marked_for_close_file : "--",
  866. c->marked_for_close,
  867. c->hold_open_until_flushed ? "" : "not ");
  868. }
  869. } SMARTLIST_FOREACH_END(ocirc);
  870. log_notice(LD_HEARTBEAT, "It has been %ld seconds since I last called "
  871. "circuit_expire_old_circuits_clientside().",
  872. (long)(now - last_expired_clientside_circuits));
  873. done:
  874. smartlist_free(log_these);
  875. }
  876. /** Remove any elements in <b>needed_ports</b> that are handled by an
  877. * open or in-progress circuit.
  878. */
  879. void
  880. circuit_remove_handled_ports(smartlist_t *needed_ports)
  881. {
  882. int i;
  883. uint16_t *port;
  884. for (i = 0; i < smartlist_len(needed_ports); ++i) {
  885. port = smartlist_get(needed_ports, i);
  886. tor_assert(*port);
  887. if (circuit_stream_is_being_handled(NULL, *port,
  888. MIN_CIRCUITS_HANDLING_STREAM)) {
  889. // log_debug(LD_CIRC,"Port %d is already being handled; removing.", port);
  890. smartlist_del(needed_ports, i--);
  891. tor_free(port);
  892. } else {
  893. log_debug(LD_CIRC,"Port %d is not handled.", *port);
  894. }
  895. }
  896. }
  897. /** Return 1 if at least <b>min</b> general-purpose non-internal circuits
  898. * will have an acceptable exit node for exit stream <b>conn</b> if it
  899. * is defined, else for "*:port".
  900. * Else return 0.
  901. */
  902. int
  903. circuit_stream_is_being_handled(entry_connection_t *conn,
  904. uint16_t port, int min)
  905. {
  906. const node_t *exitnode;
  907. int num=0;
  908. time_t now = time(NULL);
  909. int need_uptime = smartlist_contains_int_as_string(
  910. get_options()->LongLivedPorts,
  911. conn ? conn->socks_request->port : port);
  912. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  913. if (CIRCUIT_IS_ORIGIN(circ) &&
  914. !circ->marked_for_close &&
  915. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  916. (!circ->timestamp_dirty ||
  917. circ->timestamp_dirty + get_options()->MaxCircuitDirtiness > now)) {
  918. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  919. cpath_build_state_t *build_state = origin_circ->build_state;
  920. if (build_state->is_internal || build_state->onehop_tunnel)
  921. continue;
  922. if (origin_circ->unusable_for_new_conns)
  923. continue;
  924. exitnode = build_state_get_exit_node(build_state);
  925. if (exitnode && (!need_uptime || build_state->need_uptime)) {
  926. int ok;
  927. if (conn) {
  928. ok = connection_ap_can_use_exit(conn, exitnode);
  929. } else {
  930. addr_policy_result_t r;
  931. r = compare_tor_addr_to_node_policy(NULL, port, exitnode);
  932. ok = r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED;
  933. }
  934. if (ok) {
  935. if (++num >= min)
  936. return 1;
  937. }
  938. }
  939. }
  940. }
  941. SMARTLIST_FOREACH_END(circ);
  942. return 0;
  943. }
  944. /** Don't keep more than this many unused open circuits around. */
  945. #define MAX_UNUSED_OPEN_CIRCUITS 14
  946. /* Return true if a circuit is available for use, meaning that it is open,
  947. * clean, usable for new multi-hop connections, and a general purpose origin
  948. * circuit.
  949. * Accept any kind of circuit, return false if the above conditions are not
  950. * met. */
  951. STATIC int
  952. circuit_is_available_for_use(const circuit_t *circ)
  953. {
  954. const origin_circuit_t *origin_circ;
  955. cpath_build_state_t *build_state;
  956. if (!CIRCUIT_IS_ORIGIN(circ))
  957. return 0; /* We first filter out only origin circuits before doing the
  958. following checks. */
  959. if (circ->marked_for_close)
  960. return 0; /* Don't mess with marked circs */
  961. if (circ->timestamp_dirty)
  962. return 0; /* Only count clean circs */
  963. if (circ->purpose != CIRCUIT_PURPOSE_C_GENERAL)
  964. return 0; /* We only pay attention to general purpose circuits.
  965. General purpose circuits are always origin circuits. */
  966. origin_circ = CONST_TO_ORIGIN_CIRCUIT(circ);
  967. if (origin_circ->unusable_for_new_conns)
  968. return 0;
  969. build_state = origin_circ->build_state;
  970. if (build_state->onehop_tunnel)
  971. return 0;
  972. return 1;
  973. }
  974. /* Return true if we need any more exit circuits.
  975. * needs_uptime and needs_capacity are set only if we need more exit circuits.
  976. * Check if we know of a port that's been requested recently and no circuit
  977. * is currently available that can handle it. */
  978. STATIC int
  979. needs_exit_circuits(time_t now, int *needs_uptime, int *needs_capacity)
  980. {
  981. return (!circuit_all_predicted_ports_handled(now, needs_uptime,
  982. needs_capacity) &&
  983. router_have_consensus_path() == CONSENSUS_PATH_EXIT);
  984. }
  985. /* Hidden services need at least this many internal circuits */
  986. #define SUFFICIENT_UPTIME_INTERNAL_HS_SERVERS 3
  987. /* Return true if we need any more hidden service server circuits.
  988. * HS servers only need an internal circuit. */
  989. STATIC int
  990. needs_hs_server_circuits(int num_uptime_internal)
  991. {
  992. return (num_rend_services() &&
  993. num_uptime_internal < SUFFICIENT_UPTIME_INTERNAL_HS_SERVERS &&
  994. router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN);
  995. }
  996. /* We need at least this many internal circuits for hidden service clients */
  997. #define SUFFICIENT_INTERNAL_HS_CLIENTS 3
  998. /* We need at least this much uptime for internal circuits for hidden service
  999. * clients */
  1000. #define SUFFICIENT_UPTIME_INTERNAL_HS_CLIENTS 2
  1001. /* Return true if we need any more hidden service client circuits.
  1002. * HS clients only need an internal circuit. */
  1003. STATIC int
  1004. needs_hs_client_circuits(time_t now, int *needs_uptime, int *needs_capacity,
  1005. int num_internal, int num_uptime_internal)
  1006. {
  1007. int used_internal_recently = rep_hist_get_predicted_internal(now,
  1008. needs_uptime,
  1009. needs_capacity);
  1010. int requires_uptime = num_uptime_internal <
  1011. SUFFICIENT_UPTIME_INTERNAL_HS_CLIENTS &&
  1012. needs_uptime;
  1013. return (used_internal_recently &&
  1014. (requires_uptime || num_internal < SUFFICIENT_INTERNAL_HS_CLIENTS) &&
  1015. router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN);
  1016. }
  1017. /* The minimum number of open slots we should keep in order to preemptively
  1018. * build circuits. */
  1019. #define CBT_MIN_REMAINING_PREEMPTIVE_CIRCUITS 2
  1020. /* Check to see if we need more circuits to have a good build timeout. However,
  1021. * leave a couple slots open so that we can still build circuits preemptively
  1022. * as needed. */
  1023. #define CBT_MAX_UNUSED_OPEN_CIRCUITS (MAX_UNUSED_OPEN_CIRCUITS - \
  1024. CBT_MIN_REMAINING_PREEMPTIVE_CIRCUITS)
  1025. /* Return true if we need more circuits for a good build timeout.
  1026. * XXXX make the assumption that build timeout streams should be
  1027. * created whenever we can build internal circuits. */
  1028. STATIC int
  1029. needs_circuits_for_build(int num)
  1030. {
  1031. if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN) {
  1032. if (num < CBT_MAX_UNUSED_OPEN_CIRCUITS &&
  1033. !circuit_build_times_disabled() &&
  1034. circuit_build_times_needs_circuits_now(get_circuit_build_times())) {
  1035. return 1;
  1036. }
  1037. }
  1038. return 0;
  1039. }
  1040. /** Determine how many circuits we have open that are clean,
  1041. * Make sure it's enough for all the upcoming behaviors we predict we'll have.
  1042. * But put an upper bound on the total number of circuits.
  1043. */
  1044. static void
  1045. circuit_predict_and_launch_new(void)
  1046. {
  1047. int num=0, num_internal=0, num_uptime_internal=0;
  1048. int hidserv_needs_uptime=0, hidserv_needs_capacity=1;
  1049. int port_needs_uptime=0, port_needs_capacity=1;
  1050. time_t now = time(NULL);
  1051. int flags = 0;
  1052. /* Count how many of each type of circuit we currently have. */
  1053. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1054. if (!circuit_is_available_for_use(circ))
  1055. continue;
  1056. num++;
  1057. cpath_build_state_t *build_state = TO_ORIGIN_CIRCUIT(circ)->build_state;
  1058. if (build_state->is_internal)
  1059. num_internal++;
  1060. if (build_state->need_uptime && build_state->is_internal)
  1061. num_uptime_internal++;
  1062. }
  1063. SMARTLIST_FOREACH_END(circ);
  1064. /* If that's enough, then stop now. */
  1065. if (num >= MAX_UNUSED_OPEN_CIRCUITS)
  1066. return;
  1067. if (needs_exit_circuits(now, &port_needs_uptime, &port_needs_capacity)) {
  1068. if (port_needs_uptime)
  1069. flags |= CIRCLAUNCH_NEED_UPTIME;
  1070. if (port_needs_capacity)
  1071. flags |= CIRCLAUNCH_NEED_CAPACITY;
  1072. log_info(LD_CIRC,
  1073. "Have %d clean circs (%d internal), need another exit circ.",
  1074. num, num_internal);
  1075. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  1076. return;
  1077. }
  1078. if (needs_hs_server_circuits(num_uptime_internal)) {
  1079. flags = (CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_NEED_UPTIME |
  1080. CIRCLAUNCH_IS_INTERNAL);
  1081. log_info(LD_CIRC,
  1082. "Have %d clean circs (%d internal), need another internal "
  1083. "circ for my hidden service.",
  1084. num, num_internal);
  1085. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  1086. return;
  1087. }
  1088. if (needs_hs_client_circuits(now, &hidserv_needs_uptime,
  1089. &hidserv_needs_capacity,
  1090. num_internal, num_uptime_internal))
  1091. {
  1092. if (hidserv_needs_uptime)
  1093. flags |= CIRCLAUNCH_NEED_UPTIME;
  1094. if (hidserv_needs_capacity)
  1095. flags |= CIRCLAUNCH_NEED_CAPACITY;
  1096. flags |= CIRCLAUNCH_IS_INTERNAL;
  1097. log_info(LD_CIRC,
  1098. "Have %d clean circs (%d uptime-internal, %d internal), need"
  1099. " another hidden service circ.",
  1100. num, num_uptime_internal, num_internal);
  1101. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  1102. return;
  1103. }
  1104. if (needs_circuits_for_build(num)) {
  1105. flags = CIRCLAUNCH_NEED_CAPACITY;
  1106. /* if there are no exits in the consensus, make timeout
  1107. * circuits internal */
  1108. if (router_have_consensus_path() == CONSENSUS_PATH_INTERNAL)
  1109. flags |= CIRCLAUNCH_IS_INTERNAL;
  1110. log_info(LD_CIRC,
  1111. "Have %d clean circs need another buildtime test circ.", num);
  1112. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, flags);
  1113. return;
  1114. }
  1115. }
  1116. /** Build a new test circuit every 5 minutes */
  1117. #define TESTING_CIRCUIT_INTERVAL 300
  1118. /** This function is called once a second, if router_have_min_dir_info() is
  1119. * true. Its job is to make sure all services we offer have enough circuits
  1120. * available. Some services just want enough circuits for current tasks,
  1121. * whereas others want a minimum set of idle circuits hanging around.
  1122. */
  1123. void
  1124. circuit_build_needed_circs(time_t now)
  1125. {
  1126. const or_options_t *options = get_options();
  1127. /* launch a new circ for any pending streams that need one
  1128. * XXXX make the assumption that (some) AP streams (i.e. HS clients)
  1129. * don't require an exit circuit, review in #13814.
  1130. * This allows HSs to function in a consensus without exits. */
  1131. if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN)
  1132. connection_ap_rescan_and_attach_pending();
  1133. /* make sure any hidden services have enough intro points
  1134. * HS intro point streams only require an internal circuit */
  1135. if (router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN)
  1136. rend_consider_services_intro_points();
  1137. circuit_expire_old_circs_as_needed(now);
  1138. if (!options->DisablePredictedCircuits)
  1139. circuit_predict_and_launch_new();
  1140. }
  1141. /**
  1142. * Called once a second either directly or from
  1143. * circuit_build_needed_circs(). As appropriate (once per NewCircuitPeriod)
  1144. * resets failure counts and expires old circuits.
  1145. */
  1146. void
  1147. circuit_expire_old_circs_as_needed(time_t now)
  1148. {
  1149. static time_t time_to_expire_and_reset = 0;
  1150. if (time_to_expire_and_reset < now) {
  1151. circuit_reset_failure_count(1);
  1152. time_to_expire_and_reset = now + get_options()->NewCircuitPeriod;
  1153. if (proxy_mode(get_options()))
  1154. addressmap_clean(now);
  1155. circuit_expire_old_circuits_clientside();
  1156. #if 0 /* disable for now, until predict-and-launch-new can cull leftovers */
  1157. /* If we ever re-enable, this has to move into
  1158. * circuit_build_needed_circs */
  1159. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  1160. if (get_options()->RunTesting &&
  1161. circ &&
  1162. circ->timestamp_began.tv_sec + TESTING_CIRCUIT_INTERVAL < now) {
  1163. log_fn(LOG_INFO,"Creating a new testing circuit.");
  1164. circuit_launch(CIRCUIT_PURPOSE_C_GENERAL, 0);
  1165. }
  1166. #endif
  1167. }
  1168. }
  1169. /** If the stream <b>conn</b> is a member of any of the linked
  1170. * lists of <b>circ</b>, then remove it from the list.
  1171. */
  1172. void
  1173. circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
  1174. {
  1175. edge_connection_t *prevconn;
  1176. tor_assert(circ);
  1177. tor_assert(conn);
  1178. if (conn->base_.type == CONN_TYPE_AP) {
  1179. entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
  1180. entry_conn->may_use_optimistic_data = 0;
  1181. }
  1182. conn->cpath_layer = NULL; /* don't keep a stale pointer */
  1183. conn->on_circuit = NULL;
  1184. if (CIRCUIT_IS_ORIGIN(circ)) {
  1185. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  1186. int removed = 0;
  1187. if (conn == origin_circ->p_streams) {
  1188. origin_circ->p_streams = conn->next_stream;
  1189. removed = 1;
  1190. } else {
  1191. for (prevconn = origin_circ->p_streams;
  1192. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  1193. prevconn = prevconn->next_stream)
  1194. ;
  1195. if (prevconn && prevconn->next_stream) {
  1196. prevconn->next_stream = conn->next_stream;
  1197. removed = 1;
  1198. }
  1199. }
  1200. if (removed) {
  1201. log_debug(LD_APP, "Removing stream %d from circ %u",
  1202. conn->stream_id, (unsigned)circ->n_circ_id);
  1203. /* If the stream was removed, and it was a rend stream, decrement the
  1204. * number of streams on the circuit associated with the rend service.
  1205. */
  1206. if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED) {
  1207. tor_assert(origin_circ->rend_data);
  1208. origin_circ->rend_data->nr_streams--;
  1209. }
  1210. return;
  1211. }
  1212. } else {
  1213. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1214. if (conn == or_circ->n_streams) {
  1215. or_circ->n_streams = conn->next_stream;
  1216. return;
  1217. }
  1218. if (conn == or_circ->resolving_streams) {
  1219. or_circ->resolving_streams = conn->next_stream;
  1220. return;
  1221. }
  1222. for (prevconn = or_circ->n_streams;
  1223. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  1224. prevconn = prevconn->next_stream)
  1225. ;
  1226. if (prevconn && prevconn->next_stream) {
  1227. prevconn->next_stream = conn->next_stream;
  1228. return;
  1229. }
  1230. for (prevconn = or_circ->resolving_streams;
  1231. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  1232. prevconn = prevconn->next_stream)
  1233. ;
  1234. if (prevconn && prevconn->next_stream) {
  1235. prevconn->next_stream = conn->next_stream;
  1236. return;
  1237. }
  1238. }
  1239. log_warn(LD_BUG,"Edge connection not in circuit's list.");
  1240. /* Don't give an error here; it's harmless. */
  1241. tor_fragile_assert();
  1242. }
  1243. /** If we haven't yet decided on a good timeout value for circuit
  1244. * building, we close idles circuits aggressively so we can get more
  1245. * data points. */
  1246. #define IDLE_TIMEOUT_WHILE_LEARNING (10*60)
  1247. /** Find each circuit that has been unused for too long, or dirty
  1248. * for too long and has no streams on it: mark it for close.
  1249. */
  1250. static void
  1251. circuit_expire_old_circuits_clientside(void)
  1252. {
  1253. struct timeval cutoff, now;
  1254. tor_gettimeofday(&now);
  1255. cutoff = now;
  1256. last_expired_clientside_circuits = now.tv_sec;
  1257. if (! circuit_build_times_disabled() &&
  1258. circuit_build_times_needs_circuits(get_circuit_build_times())) {
  1259. /* Circuits should be shorter lived if we need more of them
  1260. * for learning a good build timeout */
  1261. cutoff.tv_sec -= IDLE_TIMEOUT_WHILE_LEARNING;
  1262. } else {
  1263. cutoff.tv_sec -= get_options()->CircuitIdleTimeout;
  1264. }
  1265. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1266. if (circ->marked_for_close || !CIRCUIT_IS_ORIGIN(circ))
  1267. continue;
  1268. /* If the circuit has been dirty for too long, and there are no streams
  1269. * on it, mark it for close.
  1270. */
  1271. if (circ->timestamp_dirty &&
  1272. circ->timestamp_dirty + get_options()->MaxCircuitDirtiness <
  1273. now.tv_sec &&
  1274. !TO_ORIGIN_CIRCUIT(circ)->p_streams /* nothing attached */ ) {
  1275. log_debug(LD_CIRC, "Closing n_circ_id %u (dirty %ld sec ago, "
  1276. "purpose %d)",
  1277. (unsigned)circ->n_circ_id,
  1278. (long)(now.tv_sec - circ->timestamp_dirty),
  1279. circ->purpose);
  1280. /* Don't do this magic for testing circuits. Their death is governed
  1281. * by circuit_expire_building */
  1282. if (circ->purpose != CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  1283. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1284. } else if (!circ->timestamp_dirty && circ->state == CIRCUIT_STATE_OPEN) {
  1285. if (timercmp(&circ->timestamp_began, &cutoff, OP_LT)) {
  1286. if (circ->purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  1287. circ->purpose == CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT ||
  1288. circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  1289. circ->purpose == CIRCUIT_PURPOSE_TESTING ||
  1290. (circ->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  1291. circ->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) ||
  1292. circ->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND) {
  1293. log_debug(LD_CIRC,
  1294. "Closing circuit that has been unused for %ld msec.",
  1295. tv_mdiff(&circ->timestamp_began, &now));
  1296. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1297. } else if (!TO_ORIGIN_CIRCUIT(circ)->is_ancient) {
  1298. /* Server-side rend joined circuits can end up really old, because
  1299. * they are reused by clients for longer than normal. The client
  1300. * controls their lifespan. (They never become dirty, because
  1301. * connection_exit_begin_conn() never marks anything as dirty.)
  1302. * Similarly, server-side intro circuits last a long time. */
  1303. if (circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED &&
  1304. circ->purpose != CIRCUIT_PURPOSE_S_INTRO) {
  1305. log_notice(LD_CIRC,
  1306. "Ancient non-dirty circuit %d is still around after "
  1307. "%ld milliseconds. Purpose: %d (%s)",
  1308. TO_ORIGIN_CIRCUIT(circ)->global_identifier,
  1309. tv_mdiff(&circ->timestamp_began, &now),
  1310. circ->purpose,
  1311. circuit_purpose_to_string(circ->purpose));
  1312. TO_ORIGIN_CIRCUIT(circ)->is_ancient = 1;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. } SMARTLIST_FOREACH_END(circ);
  1318. }
  1319. /** How long do we wait before killing circuits with the properties
  1320. * described below?
  1321. *
  1322. * Probably we could choose a number here as low as 5 to 10 seconds,
  1323. * since these circs are used for begindir, and a) generally you either
  1324. * ask another begindir question right after or you don't for a long time,
  1325. * b) clients at least through 0.2.1.x choose from the whole set of
  1326. * directory mirrors at each choice, and c) re-establishing a one-hop
  1327. * circuit via create-fast is a light operation assuming the TLS conn is
  1328. * still there.
  1329. *
  1330. * I expect "b" to go away one day when we move to using directory
  1331. * guards, but I think "a" and "c" are good enough reasons that a low
  1332. * number is safe even then.
  1333. */
  1334. #define IDLE_ONE_HOP_CIRC_TIMEOUT 60
  1335. /** Find each non-origin circuit that has been unused for too long,
  1336. * has no streams on it, used a create_fast, and ends here: mark it
  1337. * for close.
  1338. */
  1339. void
  1340. circuit_expire_old_circuits_serverside(time_t now)
  1341. {
  1342. or_circuit_t *or_circ;
  1343. time_t cutoff = now - IDLE_ONE_HOP_CIRC_TIMEOUT;
  1344. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1345. if (circ->marked_for_close || CIRCUIT_IS_ORIGIN(circ))
  1346. continue;
  1347. or_circ = TO_OR_CIRCUIT(circ);
  1348. /* If the circuit has been idle for too long, and there are no streams
  1349. * on it, and it ends here, and it used a create_fast, mark it for close.
  1350. */
  1351. if (or_circ->is_first_hop && !circ->n_chan &&
  1352. !or_circ->n_streams && !or_circ->resolving_streams &&
  1353. or_circ->p_chan &&
  1354. channel_when_last_xmit(or_circ->p_chan) <= cutoff) {
  1355. log_info(LD_CIRC, "Closing circ_id %u (empty %d secs ago)",
  1356. (unsigned)or_circ->p_circ_id,
  1357. (int)(now - channel_when_last_xmit(or_circ->p_chan)));
  1358. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1359. }
  1360. }
  1361. SMARTLIST_FOREACH_END(circ);
  1362. }
  1363. /** Number of testing circuits we want open before testing our bandwidth. */
  1364. #define NUM_PARALLEL_TESTING_CIRCS 4
  1365. /** True iff we've ever had enough testing circuits open to test our
  1366. * bandwidth. */
  1367. static int have_performed_bandwidth_test = 0;
  1368. /** Reset have_performed_bandwidth_test, so we'll start building
  1369. * testing circuits again so we can exercise our bandwidth. */
  1370. void
  1371. reset_bandwidth_test(void)
  1372. {
  1373. have_performed_bandwidth_test = 0;
  1374. }
  1375. /** Return 1 if we've already exercised our bandwidth, or if we
  1376. * have fewer than NUM_PARALLEL_TESTING_CIRCS testing circuits
  1377. * established or on the way. Else return 0.
  1378. */
  1379. int
  1380. circuit_enough_testing_circs(void)
  1381. {
  1382. int num = 0;
  1383. if (have_performed_bandwidth_test)
  1384. return 1;
  1385. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
  1386. if (!circ->marked_for_close && CIRCUIT_IS_ORIGIN(circ) &&
  1387. circ->purpose == CIRCUIT_PURPOSE_TESTING &&
  1388. circ->state == CIRCUIT_STATE_OPEN)
  1389. num++;
  1390. }
  1391. SMARTLIST_FOREACH_END(circ);
  1392. return num >= NUM_PARALLEL_TESTING_CIRCS;
  1393. }
  1394. /** A testing circuit has completed. Take whatever stats we want.
  1395. * Noticing reachability is taken care of in onionskin_answer(),
  1396. * so there's no need to record anything here. But if we still want
  1397. * to do the bandwidth test, and we now have enough testing circuits
  1398. * open, do it.
  1399. */
  1400. static void
  1401. circuit_testing_opened(origin_circuit_t *circ)
  1402. {
  1403. if (have_performed_bandwidth_test ||
  1404. !check_whether_orport_reachable(get_options())) {
  1405. /* either we've already done everything we want with testing circuits,
  1406. * or this testing circuit became open due to a fluke, e.g. we picked
  1407. * a last hop where we already had the connection open due to an
  1408. * outgoing local circuit. */
  1409. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_AT_ORIGIN);
  1410. } else if (circuit_enough_testing_circs()) {
  1411. router_perform_bandwidth_test(NUM_PARALLEL_TESTING_CIRCS, time(NULL));
  1412. have_performed_bandwidth_test = 1;
  1413. } else
  1414. consider_testing_reachability(1, 0);
  1415. }
  1416. /** A testing circuit has failed to build. Take whatever stats we want. */
  1417. static void
  1418. circuit_testing_failed(origin_circuit_t *circ, int at_last_hop)
  1419. {
  1420. const or_options_t *options = get_options();
  1421. if (server_mode(options) && check_whether_orport_reachable(options))
  1422. return;
  1423. log_info(LD_GENERAL,
  1424. "Our testing circuit (to see if your ORPort is reachable) "
  1425. "has failed. I'll try again later.");
  1426. /* These aren't used yet. */
  1427. (void)circ;
  1428. (void)at_last_hop;
  1429. }
  1430. /** The circuit <b>circ</b> has just become open. Take the next
  1431. * step: for rendezvous circuits, we pass circ to the appropriate
  1432. * function in rendclient or rendservice. For general circuits, we
  1433. * call connection_ap_attach_pending, which looks for pending streams
  1434. * that could use circ.
  1435. */
  1436. void
  1437. circuit_has_opened(origin_circuit_t *circ)
  1438. {
  1439. control_event_circuit_status(circ, CIRC_EVENT_BUILT, 0);
  1440. /* Remember that this circuit has finished building. Now if we start
  1441. * it building again later (e.g. by extending it), we will know not
  1442. * to consider its build time. */
  1443. circ->has_opened = 1;
  1444. switch (TO_CIRCUIT(circ)->purpose) {
  1445. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1446. rend_client_rendcirc_has_opened(circ);
  1447. /* Start building an intro circ if we don't have one yet. */
  1448. connection_ap_attach_pending(1);
  1449. /* This isn't a call to circuit_try_attaching_streams because a
  1450. * circuit in _C_ESTABLISH_REND state isn't connected to its
  1451. * hidden service yet, thus we can't attach streams to it yet,
  1452. * thus circuit_try_attaching_streams would always clear the
  1453. * circuit's isolation state. circuit_try_attaching_streams is
  1454. * called later, when the rend circ enters _C_REND_JOINED
  1455. * state. */
  1456. break;
  1457. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1458. rend_client_introcirc_has_opened(circ);
  1459. break;
  1460. case CIRCUIT_PURPOSE_C_GENERAL:
  1461. /* Tell any AP connections that have been waiting for a new
  1462. * circuit that one is ready. */
  1463. circuit_try_attaching_streams(circ);
  1464. break;
  1465. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1466. /* at the service, waiting for introductions */
  1467. rend_service_intro_has_opened(circ);
  1468. break;
  1469. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1470. /* at the service, connecting to rend point */
  1471. rend_service_rendezvous_has_opened(circ);
  1472. break;
  1473. case CIRCUIT_PURPOSE_TESTING:
  1474. circuit_testing_opened(circ);
  1475. break;
  1476. /* default:
  1477. * This won't happen in normal operation, but might happen if the
  1478. * controller did it. Just let it slide. */
  1479. }
  1480. }
  1481. /** If the stream-isolation state of <b>circ</b> can be cleared, clear
  1482. * it. Return non-zero iff <b>circ</b>'s isolation state was cleared. */
  1483. static int
  1484. circuit_try_clearing_isolation_state(origin_circuit_t *circ)
  1485. {
  1486. if (/* The circuit may have become non-open if it was cannibalized.*/
  1487. circ->base_.state == CIRCUIT_STATE_OPEN &&
  1488. /* If !isolation_values_set, there is nothing to clear. */
  1489. circ->isolation_values_set &&
  1490. /* It's not legal to clear a circuit's isolation info if it's ever had
  1491. * streams attached */
  1492. !circ->isolation_any_streams_attached) {
  1493. /* If we have any isolation information set on this circuit, and
  1494. * we didn't manage to attach any streams to it, then we can
  1495. * and should clear it and try again. */
  1496. circuit_clear_isolation(circ);
  1497. return 1;
  1498. } else {
  1499. return 0;
  1500. }
  1501. }
  1502. /** Called when a circuit becomes ready for streams to be attached to
  1503. * it. */
  1504. void
  1505. circuit_try_attaching_streams(origin_circuit_t *circ)
  1506. {
  1507. /* Attach streams to this circuit if we can. */
  1508. connection_ap_attach_pending(1);
  1509. /* The call to circuit_try_clearing_isolation_state here will do
  1510. * nothing and return 0 if we didn't attach any streams to circ
  1511. * above. */
  1512. if (circuit_try_clearing_isolation_state(circ)) {
  1513. /* Maybe *now* we can attach some streams to this circuit. */
  1514. connection_ap_attach_pending(1);
  1515. }
  1516. }
  1517. /** Called whenever a circuit could not be successfully built.
  1518. */
  1519. void
  1520. circuit_build_failed(origin_circuit_t *circ)
  1521. {
  1522. channel_t *n_chan = NULL;
  1523. /* we should examine circ and see if it failed because of
  1524. * the last hop or an earlier hop. then use this info below.
  1525. */
  1526. int failed_at_last_hop = 0;
  1527. /* If the last hop isn't open, and the second-to-last is, we failed
  1528. * at the last hop. */
  1529. if (circ->cpath &&
  1530. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  1531. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  1532. failed_at_last_hop = 1;
  1533. }
  1534. if (circ->cpath &&
  1535. circ->cpath->state != CPATH_STATE_OPEN &&
  1536. ! circ->base_.received_destroy) {
  1537. /* We failed at the first hop for some reason other than a DESTROY cell.
  1538. * If there's an OR connection to blame, blame it. Also, avoid this relay
  1539. * for a while, and fail any one-hop directory fetches destined for it. */
  1540. const char *n_chan_id = circ->cpath->extend_info->identity_digest;
  1541. int already_marked = 0;
  1542. if (circ->base_.n_chan) {
  1543. n_chan = circ->base_.n_chan;
  1544. if (n_chan->is_bad_for_new_circs) {
  1545. /* We only want to blame this router when a fresh healthy
  1546. * connection fails. So don't mark this router as newly failed,
  1547. * since maybe this was just an old circuit attempt that's
  1548. * finally timing out now. Also, there's no need to blow away
  1549. * circuits/streams/etc, since the failure of an unhealthy conn
  1550. * doesn't tell us much about whether a healthy conn would
  1551. * succeed. */
  1552. already_marked = 1;
  1553. }
  1554. log_info(LD_OR,
  1555. "Our circuit failed to get a response from the first hop "
  1556. "(%s). I'm going to try to rotate to a better connection.",
  1557. channel_get_canonical_remote_descr(n_chan));
  1558. n_chan->is_bad_for_new_circs = 1;
  1559. } else {
  1560. log_info(LD_OR,
  1561. "Our circuit died before the first hop with no connection");
  1562. }
  1563. if (n_chan_id && !already_marked) {
  1564. /* New guard API: we failed. */
  1565. if (circ->guard_state)
  1566. entry_guard_failed(&circ->guard_state);
  1567. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  1568. /* Old guard API: we failed. */
  1569. entry_guard_register_connect_status(n_chan_id, 0, 1, time(NULL));
  1570. #endif
  1571. /* if there are any one-hop streams waiting on this circuit, fail
  1572. * them now so they can retry elsewhere. */
  1573. connection_ap_fail_onehop(n_chan_id, circ->build_state);
  1574. }
  1575. }
  1576. switch (circ->base_.purpose) {
  1577. case CIRCUIT_PURPOSE_C_GENERAL:
  1578. /* If we never built the circuit, note it as a failure. */
  1579. circuit_increment_failure_count();
  1580. if (failed_at_last_hop) {
  1581. /* Make sure any streams that demand our last hop as their exit
  1582. * know that it's unlikely to happen. */
  1583. circuit_discard_optional_exit_enclaves(circ->cpath->prev->extend_info);
  1584. }
  1585. break;
  1586. case CIRCUIT_PURPOSE_TESTING:
  1587. circuit_testing_failed(circ, failed_at_last_hop);
  1588. break;
  1589. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1590. /* at the service, waiting for introductions */
  1591. if (circ->base_.state != CIRCUIT_STATE_OPEN) {
  1592. circuit_increment_failure_count();
  1593. }
  1594. /* no need to care here, because the service will rebuild intro
  1595. * points periodically. */
  1596. break;
  1597. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1598. /* at the client, connecting to intro point */
  1599. /* Don't increment failure count, since the service may have picked
  1600. * the introduction point maliciously */
  1601. /* The client will pick a new intro point when this one dies, if
  1602. * the stream in question still cares. No need to act here. */
  1603. break;
  1604. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1605. /* at the client, waiting for the service */
  1606. circuit_increment_failure_count();
  1607. /* the client will pick a new rend point when this one dies, if
  1608. * the stream in question still cares. No need to act here. */
  1609. break;
  1610. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1611. /* at the service, connecting to rend point */
  1612. /* Don't increment failure count, since the client may have picked
  1613. * the rendezvous point maliciously */
  1614. log_info(LD_REND,
  1615. "Couldn't connect to the client's chosen rend point %s "
  1616. "(%s hop failed).",
  1617. escaped(build_state_get_exit_nickname(circ->build_state)),
  1618. failed_at_last_hop?"last":"non-last");
  1619. rend_service_relaunch_rendezvous(circ);
  1620. break;
  1621. /* default:
  1622. * This won't happen in normal operation, but might happen if the
  1623. * controller did it. Just let it slide. */
  1624. }
  1625. }
  1626. /** Number of consecutive failures so far; should only be touched by
  1627. * circuit_launch_new and circuit_*_failure_count.
  1628. */
  1629. static int n_circuit_failures = 0;
  1630. /** Before the last time we called circuit_reset_failure_count(), were
  1631. * there a lot of failures? */
  1632. static int did_circs_fail_last_period = 0;
  1633. /** Don't retry launching a new circuit if we try this many times with no
  1634. * success. */
  1635. #define MAX_CIRCUIT_FAILURES 5
  1636. /** Launch a new circuit; see circuit_launch_by_extend_info() for
  1637. * details on arguments. */
  1638. origin_circuit_t *
  1639. circuit_launch(uint8_t purpose, int flags)
  1640. {
  1641. return circuit_launch_by_extend_info(purpose, NULL, flags);
  1642. }
  1643. /* Do we have enough descriptors to build paths?
  1644. * If need_exit is true, return 1 if we can build exit paths.
  1645. * (We need at least one Exit in the consensus to build exit paths.)
  1646. * If need_exit is false, return 1 if we can build internal paths.
  1647. */
  1648. static int
  1649. have_enough_path_info(int need_exit)
  1650. {
  1651. if (need_exit)
  1652. return router_have_consensus_path() == CONSENSUS_PATH_EXIT;
  1653. else
  1654. return router_have_consensus_path() != CONSENSUS_PATH_UNKNOWN;
  1655. }
  1656. /** Launch a new circuit with purpose <b>purpose</b> and exit node
  1657. * <b>extend_info</b> (or NULL to select a random exit node). If flags
  1658. * contains CIRCLAUNCH_NEED_UPTIME, choose among routers with high uptime. If
  1659. * CIRCLAUNCH_NEED_CAPACITY is set, choose among routers with high bandwidth.
  1660. * If CIRCLAUNCH_IS_INTERNAL is true, the last hop need not be an exit node.
  1661. * If CIRCLAUNCH_ONEHOP_TUNNEL is set, the circuit will have only one hop.
  1662. * Return the newly allocated circuit on success, or NULL on failure. */
  1663. origin_circuit_t *
  1664. circuit_launch_by_extend_info(uint8_t purpose,
  1665. extend_info_t *extend_info,
  1666. int flags)
  1667. {
  1668. origin_circuit_t *circ;
  1669. int onehop_tunnel = (flags & CIRCLAUNCH_ONEHOP_TUNNEL) != 0;
  1670. int have_path = have_enough_path_info(! (flags & CIRCLAUNCH_IS_INTERNAL) );
  1671. int need_specific_rp = 0;
  1672. if (!onehop_tunnel && (!router_have_minimum_dir_info() || !have_path)) {
  1673. log_debug(LD_CIRC,"Haven't %s yet; canceling "
  1674. "circuit launch.",
  1675. !router_have_minimum_dir_info() ?
  1676. "fetched enough directory info" :
  1677. "received a consensus with exits");
  1678. return NULL;
  1679. }
  1680. /* If Tor2webRendezvousPoints is enabled and we are dealing with an
  1681. RP circuit, we want a specific RP node so we shouldn't canibalize
  1682. an already existing circuit. */
  1683. if (get_options()->Tor2webRendezvousPoints &&
  1684. purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND) {
  1685. need_specific_rp = 1;
  1686. }
  1687. if ((extend_info || purpose != CIRCUIT_PURPOSE_C_GENERAL) &&
  1688. purpose != CIRCUIT_PURPOSE_TESTING &&
  1689. !onehop_tunnel && !need_specific_rp) {
  1690. /* see if there are appropriate circs available to cannibalize. */
  1691. /* XXX if we're planning to add a hop, perhaps we want to look for
  1692. * internal circs rather than exit circs? -RD */
  1693. circ = circuit_find_to_cannibalize(purpose, extend_info, flags);
  1694. if (circ) {
  1695. uint8_t old_purpose = circ->base_.purpose;
  1696. struct timeval old_timestamp_began = circ->base_.timestamp_began;
  1697. log_info(LD_CIRC,"Cannibalizing circ '%s' for purpose %d (%s)",
  1698. build_state_get_exit_nickname(circ->build_state), purpose,
  1699. circuit_purpose_to_string(purpose));
  1700. if ((purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
  1701. purpose == CIRCUIT_PURPOSE_C_INTRODUCING) &&
  1702. circ->path_state == PATH_STATE_BUILD_SUCCEEDED) {
  1703. /* Path bias: Cannibalized rends pre-emptively count as a
  1704. * successfully built but unused closed circuit. We don't
  1705. * wait until the extend (or the close) because the rend
  1706. * point could be malicious.
  1707. *
  1708. * Same deal goes for client side introductions. Clients
  1709. * can be manipulated to connect repeatedly to them
  1710. * (especially web clients).
  1711. *
  1712. * If we decide to probe the initial portion of these circs,
  1713. * (up to the adversary's final hop), we need to remove this,
  1714. * or somehow mark the circuit with a special path state.
  1715. */
  1716. /* This must be called before the purpose change */
  1717. pathbias_check_close(circ, END_CIRC_REASON_FINISHED);
  1718. }
  1719. circuit_change_purpose(TO_CIRCUIT(circ), purpose);
  1720. /* Reset the start date of this circ, else expire_building
  1721. * will see it and think it's been trying to build since it
  1722. * began.
  1723. *
  1724. * Technically, the code should reset this when the
  1725. * create cell is finally sent, but we're close enough
  1726. * here. */
  1727. tor_gettimeofday(&circ->base_.timestamp_began);
  1728. control_event_circuit_cannibalized(circ, old_purpose,
  1729. &old_timestamp_began);
  1730. switch (purpose) {
  1731. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1732. /* it's ready right now */
  1733. break;
  1734. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1735. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1736. case CIRCUIT_PURPOSE_C_GENERAL:
  1737. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1738. /* need to add a new hop */
  1739. tor_assert(extend_info);
  1740. if (circuit_extend_to_new_exit(circ, extend_info) < 0)
  1741. return NULL;
  1742. break;
  1743. default:
  1744. log_warn(LD_BUG,
  1745. "unexpected purpose %d when cannibalizing a circ.",
  1746. purpose);
  1747. tor_fragile_assert();
  1748. return NULL;
  1749. }
  1750. return circ;
  1751. }
  1752. }
  1753. if (did_circs_fail_last_period &&
  1754. n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  1755. /* too many failed circs in a row. don't try. */
  1756. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  1757. return NULL;
  1758. }
  1759. /* try a circ. if it fails, circuit_mark_for_close will increment
  1760. * n_circuit_failures */
  1761. return circuit_establish_circuit(purpose, extend_info, flags);
  1762. }
  1763. /** Record another failure at opening a general circuit. When we have
  1764. * too many, we'll stop trying for the remainder of this minute.
  1765. */
  1766. static void
  1767. circuit_increment_failure_count(void)
  1768. {
  1769. ++n_circuit_failures;
  1770. log_debug(LD_CIRC,"n_circuit_failures now %d.",n_circuit_failures);
  1771. }
  1772. /** Reset the failure count for opening general circuits. This means
  1773. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  1774. * stopping again.
  1775. */
  1776. void
  1777. circuit_reset_failure_count(int timeout)
  1778. {
  1779. if (timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
  1780. did_circs_fail_last_period = 1;
  1781. else
  1782. did_circs_fail_last_period = 0;
  1783. n_circuit_failures = 0;
  1784. }
  1785. /** Find an open circ that we're happy to use for <b>conn</b> and return 1. If
  1786. * there isn't one, and there isn't one on the way, launch one and return
  1787. * 0. If it will never work, return -1.
  1788. *
  1789. * Write the found or in-progress or launched circ into *circp.
  1790. */
  1791. static int
  1792. circuit_get_open_circ_or_launch(entry_connection_t *conn,
  1793. uint8_t desired_circuit_purpose,
  1794. origin_circuit_t **circp)
  1795. {
  1796. origin_circuit_t *circ;
  1797. int check_exit_policy;
  1798. int need_uptime, need_internal;
  1799. int want_onehop;
  1800. const or_options_t *options = get_options();
  1801. tor_assert(conn);
  1802. tor_assert(circp);
  1803. if (ENTRY_TO_CONN(conn)->state != AP_CONN_STATE_CIRCUIT_WAIT) {
  1804. connection_t *c = ENTRY_TO_CONN(conn);
  1805. log_err(LD_BUG, "Connection state mismatch: wanted "
  1806. "AP_CONN_STATE_CIRCUIT_WAIT, but got %d (%s)",
  1807. c->state, conn_state_to_string(c->type, c->state));
  1808. }
  1809. tor_assert(ENTRY_TO_CONN(conn)->state == AP_CONN_STATE_CIRCUIT_WAIT);
  1810. /* Will the exit policy of the exit node apply to this stream? */
  1811. check_exit_policy =
  1812. conn->socks_request->command == SOCKS_COMMAND_CONNECT &&
  1813. !conn->use_begindir &&
  1814. !connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn));
  1815. /* Does this connection want a one-hop circuit? */
  1816. want_onehop = conn->want_onehop;
  1817. /* Do we need a high-uptime circuit? */
  1818. need_uptime = !conn->want_onehop && !conn->use_begindir &&
  1819. smartlist_contains_int_as_string(options->LongLivedPorts,
  1820. conn->socks_request->port);
  1821. /* Do we need an "internal" circuit? */
  1822. if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)
  1823. need_internal = 1;
  1824. else if (conn->use_begindir || conn->want_onehop)
  1825. need_internal = 1;
  1826. else
  1827. need_internal = 0;
  1828. /* We now know what kind of circuit we need. See if there is an
  1829. * open circuit that we can use for this stream */
  1830. circ = circuit_get_best(conn, 1 /* Insist on open circuits */,
  1831. desired_circuit_purpose,
  1832. need_uptime, need_internal);
  1833. if (circ) {
  1834. /* We got a circuit that will work for this stream! We can return it. */
  1835. *circp = circ;
  1836. return 1; /* we're happy */
  1837. }
  1838. /* Okay, there's no circuit open that will work for this stream. Let's
  1839. * see if there's an in-progress circuit or if we have to launch one */
  1840. /* Do we know enough directory info to build circuits at all? */
  1841. int have_path = have_enough_path_info(!need_internal);
  1842. if (!want_onehop && (!router_have_minimum_dir_info() || !have_path)) {
  1843. /* If we don't have enough directory information, we can't build
  1844. * multihop circuits.
  1845. */
  1846. if (!connection_get_by_type(CONN_TYPE_DIR)) {
  1847. int severity = LOG_NOTICE;
  1848. /* Retry some stuff that might help the connection work. */
  1849. if (entry_list_is_constrained(options) &&
  1850. guards_retry_optimistic(options)) {
  1851. log_fn(severity, LD_APP|LD_DIR,
  1852. "Application request when we haven't %s. "
  1853. "Optimistically trying known %s again.",
  1854. !router_have_minimum_dir_info() ?
  1855. "used client functionality lately" :
  1856. "received a consensus with exits",
  1857. options->UseBridges ? "bridges" : "entrynodes");
  1858. } else if (!options->UseBridges || any_bridge_descriptors_known()) {
  1859. log_fn(severity, LD_APP|LD_DIR,
  1860. "Application request when we haven't %s. "
  1861. "Optimistically trying directory fetches again.",
  1862. !router_have_minimum_dir_info() ?
  1863. "used client functionality lately" :
  1864. "received a consensus with exits");
  1865. routerlist_retry_directory_downloads(time(NULL));
  1866. }
  1867. }
  1868. /* Since we didn't have enough directory info, we can't attach now. The
  1869. * stream will be dealt with when router_have_minimum_dir_info becomes 1,
  1870. * or when all directory attempts fail and directory_all_unreachable()
  1871. * kills it.
  1872. */
  1873. return 0;
  1874. }
  1875. /* Check whether the exit policy of the chosen exit, or the exit policies
  1876. * of _all_ nodes, would forbid this node. */
  1877. if (check_exit_policy) {
  1878. if (!conn->chosen_exit_name) {
  1879. struct in_addr in;
  1880. tor_addr_t addr, *addrp=NULL;
  1881. if (tor_inet_aton(conn->socks_request->address, &in)) {
  1882. tor_addr_from_in(&addr, &in);
  1883. addrp = &addr;
  1884. }
  1885. if (router_exit_policy_all_nodes_reject(addrp,
  1886. conn->socks_request->port,
  1887. need_uptime)) {
  1888. log_notice(LD_APP,
  1889. "No Tor server allows exit to %s:%d. Rejecting.",
  1890. safe_str_client(conn->socks_request->address),
  1891. conn->socks_request->port);
  1892. return -1;
  1893. }
  1894. } else {
  1895. /* XXXX Duplicates checks in connection_ap_handshake_attach_circuit:
  1896. * refactor into a single function. */
  1897. const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 1);
  1898. int opt = conn->chosen_exit_optional;
  1899. if (node && !connection_ap_can_use_exit(conn, node)) {
  1900. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  1901. "Requested exit point '%s' is excluded or "
  1902. "would refuse request. %s.",
  1903. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  1904. if (opt) {
  1905. conn->chosen_exit_optional = 0;
  1906. tor_free(conn->chosen_exit_name);
  1907. /* Try again. */
  1908. return circuit_get_open_circ_or_launch(conn,
  1909. desired_circuit_purpose,
  1910. circp);
  1911. }
  1912. return -1;
  1913. }
  1914. }
  1915. }
  1916. /* Now, check whether there already a circuit on the way that could handle
  1917. * this stream. This check matches the one above, but this time we
  1918. * do not require that the circuit will work. */
  1919. circ = circuit_get_best(conn, 0 /* don't insist on open circuits */,
  1920. desired_circuit_purpose,
  1921. need_uptime, need_internal);
  1922. if (circ)
  1923. log_debug(LD_CIRC, "one on the way!");
  1924. if (!circ) {
  1925. /* No open or in-progress circuit could handle this stream! We
  1926. * will have to launch one!
  1927. */
  1928. /* THe chosen exit node, if there is one. */
  1929. extend_info_t *extend_info=NULL;
  1930. const int n_pending = count_pending_general_client_circuits();
  1931. /* Do we have too many pending circuits? */
  1932. if (n_pending >= options->MaxClientCircuitsPending) {
  1933. static ratelim_t delay_limit = RATELIM_INIT(10*60);
  1934. char *m;
  1935. if ((m = rate_limit_log(&delay_limit, approx_time()))) {
  1936. log_notice(LD_APP, "We'd like to launch a circuit to handle a "
  1937. "connection, but we already have %d general-purpose client "
  1938. "circuits pending. Waiting until some finish.%s",
  1939. n_pending, m);
  1940. tor_free(m);
  1941. }
  1942. return 0;
  1943. }
  1944. /* If this is a hidden service trying to start an introduction point,
  1945. * handle that case. */
  1946. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1947. /* need to pick an intro point */
  1948. rend_data_t *rend_data = ENTRY_TO_EDGE_CONN(conn)->rend_data;
  1949. tor_assert(rend_data);
  1950. extend_info = rend_client_get_random_intro(rend_data);
  1951. if (!extend_info) {
  1952. log_info(LD_REND,
  1953. "No intro points for '%s': re-fetching service descriptor.",
  1954. safe_str_client(rend_data_get_address(rend_data)));
  1955. rend_client_refetch_v2_renddesc(rend_data);
  1956. connection_ap_mark_as_non_pending_circuit(conn);
  1957. ENTRY_TO_CONN(conn)->state = AP_CONN_STATE_RENDDESC_WAIT;
  1958. return 0;
  1959. }
  1960. log_info(LD_REND,"Chose %s as intro point for '%s'.",
  1961. extend_info_describe(extend_info),
  1962. safe_str_client(rend_data_get_address(rend_data)));
  1963. }
  1964. /* If we have specified a particular exit node for our
  1965. * connection, then be sure to open a circuit to that exit node.
  1966. */
  1967. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  1968. if (conn->chosen_exit_name) {
  1969. const node_t *r;
  1970. int opt = conn->chosen_exit_optional;
  1971. r = node_get_by_nickname(conn->chosen_exit_name, 1);
  1972. if (r && node_has_descriptor(r)) {
  1973. /* We might want to connect to an IPv6 bridge for loading
  1974. descriptors so we use the preferred address rather than
  1975. the primary. */
  1976. extend_info = extend_info_from_node(r, conn->want_onehop ? 1 : 0);
  1977. if (!extend_info) {
  1978. log_warn(LD_CIRC,"Could not make a one-hop connection to %s. "
  1979. "Discarding this circuit.", conn->chosen_exit_name);
  1980. return -1;
  1981. }
  1982. } else { /* ! (r && node_has_descriptor(r)) */
  1983. log_debug(LD_DIR, "considering %d, %s",
  1984. want_onehop, conn->chosen_exit_name);
  1985. if (want_onehop && conn->chosen_exit_name[0] == '$') {
  1986. /* We're asking for a one-hop circuit to a router that
  1987. * we don't have a routerinfo about. Make up an extend_info. */
  1988. /* XXX prop220: we need to make chosen_exit_name able to
  1989. * encode both key formats. This is not absolutely critical
  1990. * since this is just for one-hop circuits, but we should
  1991. * still get it done */
  1992. char digest[DIGEST_LEN];
  1993. char *hexdigest = conn->chosen_exit_name+1;
  1994. tor_addr_t addr;
  1995. if (strlen(hexdigest) < HEX_DIGEST_LEN ||
  1996. base16_decode(digest,DIGEST_LEN,
  1997. hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN) {
  1998. log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
  1999. return -1;
  2000. }
  2001. if (tor_addr_parse(&addr, conn->socks_request->address) < 0) {
  2002. log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
  2003. escaped_safe_str_client(conn->socks_request->address));
  2004. return -1;
  2005. }
  2006. /* XXXX prop220 add a workaround for ed25519 ID below*/
  2007. extend_info = extend_info_new(conn->chosen_exit_name+1,
  2008. digest,
  2009. NULL, /* Ed25519 ID */
  2010. NULL, NULL, /* onion keys */
  2011. &addr, conn->socks_request->port);
  2012. } else { /* ! (want_onehop && conn->chosen_exit_name[0] == '$') */
  2013. /* We will need an onion key for the router, and we
  2014. * don't have one. Refuse or relax requirements. */
  2015. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  2016. "Requested exit point '%s' is not known. %s.",
  2017. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  2018. if (opt) {
  2019. conn->chosen_exit_optional = 0;
  2020. tor_free(conn->chosen_exit_name);
  2021. /* Try again with no requested exit */
  2022. return circuit_get_open_circ_or_launch(conn,
  2023. desired_circuit_purpose,
  2024. circp);
  2025. }
  2026. return -1;
  2027. }
  2028. }
  2029. }
  2030. } /* Done checking for general circutis with chosen exits. */
  2031. /* What purpose do we need to launch this circuit with? */
  2032. uint8_t new_circ_purpose;
  2033. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  2034. new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  2035. else if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  2036. new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  2037. else
  2038. new_circ_purpose = desired_circuit_purpose;
  2039. #ifdef ENABLE_TOR2WEB_MODE
  2040. /* If tor2Web is on, then hidden service requests should be one-hop.
  2041. */
  2042. if (options->Tor2webMode &&
  2043. (new_circ_purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  2044. new_circ_purpose == CIRCUIT_PURPOSE_C_INTRODUCING)) {
  2045. want_onehop = 1;
  2046. }
  2047. #endif
  2048. /* Determine what kind of a circuit to launch, and actually launch it. */
  2049. {
  2050. int flags = CIRCLAUNCH_NEED_CAPACITY;
  2051. if (want_onehop) flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
  2052. if (need_uptime) flags |= CIRCLAUNCH_NEED_UPTIME;
  2053. if (need_internal) flags |= CIRCLAUNCH_IS_INTERNAL;
  2054. circ = circuit_launch_by_extend_info(new_circ_purpose, extend_info,
  2055. flags);
  2056. }
  2057. extend_info_free(extend_info);
  2058. /* Now trigger things that need to happen when we launch circuits */
  2059. if (desired_circuit_purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  2060. /* We just caused a circuit to get built because of this stream.
  2061. * If this stream has caused a _lot_ of circuits to be built, that's
  2062. * a bad sign: we should tell the user. */
  2063. if (conn->num_circuits_launched < NUM_CIRCUITS_LAUNCHED_THRESHOLD &&
  2064. ++conn->num_circuits_launched == NUM_CIRCUITS_LAUNCHED_THRESHOLD)
  2065. log_info(LD_CIRC, "The application request to %s:%d has launched "
  2066. "%d circuits without finding one it likes.",
  2067. escaped_safe_str_client(conn->socks_request->address),
  2068. conn->socks_request->port,
  2069. conn->num_circuits_launched);
  2070. } else {
  2071. /* help predict this next time */
  2072. rep_hist_note_used_internal(time(NULL), need_uptime, 1);
  2073. if (circ) {
  2074. /* write the service_id into circ */
  2075. circ->rend_data = rend_data_dup(ENTRY_TO_EDGE_CONN(conn)->rend_data);
  2076. if (circ->base_.purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  2077. circ->base_.state == CIRCUIT_STATE_OPEN)
  2078. circuit_has_opened(circ);
  2079. }
  2080. }
  2081. } /* endif (!circ) */
  2082. /* We either found a good circuit, or launched a new circuit, or failed to
  2083. * do so. Report success, and delay. */
  2084. if (circ) {
  2085. /* Mark the circuit with the isolation fields for this connection.
  2086. * When the circuit arrives, we'll clear these flags: this is
  2087. * just some internal bookkeeping to make sure that we have
  2088. * launched enough circuits.
  2089. */
  2090. connection_edge_update_circuit_isolation(conn, circ, 0);
  2091. } else {
  2092. log_info(LD_APP,
  2093. "No safe circuit (purpose %d) ready for edge "
  2094. "connection; delaying.",
  2095. desired_circuit_purpose);
  2096. }
  2097. *circp = circ;
  2098. return 0;
  2099. }
  2100. /** Return true iff <b>crypt_path</b> is one of the crypt_paths for
  2101. * <b>circ</b>. */
  2102. static int
  2103. cpath_is_on_circuit(origin_circuit_t *circ, crypt_path_t *crypt_path)
  2104. {
  2105. crypt_path_t *cpath, *cpath_next = NULL;
  2106. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  2107. cpath_next = cpath->next;
  2108. if (crypt_path == cpath)
  2109. return 1;
  2110. }
  2111. return 0;
  2112. }
  2113. /** Return true iff client-side optimistic data is supported. */
  2114. static int
  2115. optimistic_data_enabled(void)
  2116. {
  2117. const or_options_t *options = get_options();
  2118. if (options->OptimisticData < 0) {
  2119. /* Note: this default was 0 before #18815 was merged. We can't take the
  2120. * parameter out of the consensus until versions before that are all
  2121. * obsolete. */
  2122. const int32_t enabled =
  2123. networkstatus_get_param(NULL, "UseOptimisticData", /*default*/ 1, 0, 1);
  2124. return (int)enabled;
  2125. }
  2126. return options->OptimisticData;
  2127. }
  2128. /** Attach the AP stream <b>apconn</b> to circ's linked list of
  2129. * p_streams. Also set apconn's cpath_layer to <b>cpath</b>, or to the last
  2130. * hop in circ's cpath if <b>cpath</b> is NULL.
  2131. */
  2132. static void
  2133. link_apconn_to_circ(entry_connection_t *apconn, origin_circuit_t *circ,
  2134. crypt_path_t *cpath)
  2135. {
  2136. const node_t *exitnode = NULL;
  2137. /* add it into the linked list of streams on this circuit */
  2138. log_debug(LD_APP|LD_CIRC, "attaching new conn to circ. n_circ_id %u.",
  2139. (unsigned)circ->base_.n_circ_id);
  2140. /* reset it, so we can measure circ timeouts */
  2141. ENTRY_TO_CONN(apconn)->timestamp_lastread = time(NULL);
  2142. ENTRY_TO_EDGE_CONN(apconn)->next_stream = circ->p_streams;
  2143. ENTRY_TO_EDGE_CONN(apconn)->on_circuit = TO_CIRCUIT(circ);
  2144. /* assert_connection_ok(conn, time(NULL)); */
  2145. circ->p_streams = ENTRY_TO_EDGE_CONN(apconn);
  2146. if (connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(apconn))) {
  2147. /* We are attaching a stream to a rendezvous circuit. That means
  2148. * that an attempt to connect to a hidden service just
  2149. * succeeded. Tell rendclient.c. */
  2150. rend_client_note_connection_attempt_ended(
  2151. ENTRY_TO_EDGE_CONN(apconn)->rend_data);
  2152. }
  2153. if (cpath) { /* we were given one; use it */
  2154. tor_assert(cpath_is_on_circuit(circ, cpath));
  2155. } else {
  2156. /* use the last hop in the circuit */
  2157. tor_assert(circ->cpath);
  2158. tor_assert(circ->cpath->prev);
  2159. tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  2160. cpath = circ->cpath->prev;
  2161. }
  2162. ENTRY_TO_EDGE_CONN(apconn)->cpath_layer = cpath;
  2163. circ->isolation_any_streams_attached = 1;
  2164. connection_edge_update_circuit_isolation(apconn, circ, 0);
  2165. /* Compute the exitnode if possible, for logging below */
  2166. if (cpath->extend_info)
  2167. exitnode = node_get_by_id(cpath->extend_info->identity_digest);
  2168. /* See if we can use optimistic data on this circuit */
  2169. if (optimistic_data_enabled() &&
  2170. (circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  2171. circ->base_.purpose == CIRCUIT_PURPOSE_C_REND_JOINED))
  2172. apconn->may_use_optimistic_data = 1;
  2173. else
  2174. apconn->may_use_optimistic_data = 0;
  2175. log_info(LD_APP, "Looks like completed circuit to %s %s allow "
  2176. "optimistic data for connection to %s",
  2177. circ->base_.purpose == CIRCUIT_PURPOSE_C_GENERAL ?
  2178. /* node_describe() does the right thing if exitnode is NULL */
  2179. safe_str_client(node_describe(exitnode)) :
  2180. "hidden service",
  2181. apconn->may_use_optimistic_data ? "does" : "doesn't",
  2182. safe_str_client(apconn->socks_request->address));
  2183. }
  2184. /** Return true iff <b>address</b> is matched by one of the entries in
  2185. * TrackHostExits. */
  2186. int
  2187. hostname_in_track_host_exits(const or_options_t *options, const char *address)
  2188. {
  2189. if (!options->TrackHostExits)
  2190. return 0;
  2191. SMARTLIST_FOREACH_BEGIN(options->TrackHostExits, const char *, cp) {
  2192. if (cp[0] == '.') { /* match end */
  2193. if (cp[1] == '\0' ||
  2194. !strcasecmpend(address, cp) ||
  2195. !strcasecmp(address, &cp[1]))
  2196. return 1;
  2197. } else if (strcasecmp(cp, address) == 0) {
  2198. return 1;
  2199. }
  2200. } SMARTLIST_FOREACH_END(cp);
  2201. return 0;
  2202. }
  2203. /** If an exit wasn't explicitly specified for <b>conn</b>, consider saving
  2204. * the exit that we *did* choose for use by future connections to
  2205. * <b>conn</b>'s destination.
  2206. */
  2207. static void
  2208. consider_recording_trackhost(const entry_connection_t *conn,
  2209. const origin_circuit_t *circ)
  2210. {
  2211. const or_options_t *options = get_options();
  2212. char *new_address = NULL;
  2213. char fp[HEX_DIGEST_LEN+1];
  2214. /* Search the addressmap for this conn's destination. */
  2215. /* If they're not in the address map.. */
  2216. if (!options->TrackHostExits ||
  2217. addressmap_have_mapping(conn->socks_request->address,
  2218. options->TrackHostExitsExpire))
  2219. return; /* nothing to track, or already mapped */
  2220. if (!hostname_in_track_host_exits(options, conn->socks_request->address) ||
  2221. !circ->build_state->chosen_exit)
  2222. return;
  2223. /* write down the fingerprint of the chosen exit, not the nickname,
  2224. * because the chosen exit might not be named. */
  2225. base16_encode(fp, sizeof(fp),
  2226. circ->build_state->chosen_exit->identity_digest, DIGEST_LEN);
  2227. /* Add this exit/hostname pair to the addressmap. */
  2228. tor_asprintf(&new_address, "%s.%s.exit",
  2229. conn->socks_request->address, fp);
  2230. addressmap_register(conn->socks_request->address, new_address,
  2231. time(NULL) + options->TrackHostExitsExpire,
  2232. ADDRMAPSRC_TRACKEXIT, 0, 0);
  2233. }
  2234. /** Attempt to attach the connection <b>conn</b> to <b>circ</b>, and send a
  2235. * begin or resolve cell as appropriate. Return values are as for
  2236. * connection_ap_handshake_attach_circuit. The stream will exit from the hop
  2237. * indicated by <b>cpath</b>, or from the last hop in circ's cpath if
  2238. * <b>cpath</b> is NULL. */
  2239. int
  2240. connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn,
  2241. origin_circuit_t *circ,
  2242. crypt_path_t *cpath)
  2243. {
  2244. connection_t *base_conn = ENTRY_TO_CONN(conn);
  2245. tor_assert(conn);
  2246. tor_assert(base_conn->state == AP_CONN_STATE_CIRCUIT_WAIT ||
  2247. base_conn->state == AP_CONN_STATE_CONTROLLER_WAIT);
  2248. tor_assert(conn->socks_request);
  2249. tor_assert(circ);
  2250. tor_assert(circ->base_.state == CIRCUIT_STATE_OPEN);
  2251. base_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  2252. if (!circ->base_.timestamp_dirty ||
  2253. ((conn->entry_cfg.isolation_flags & ISO_SOCKSAUTH) &&
  2254. (conn->entry_cfg.socks_iso_keep_alive) &&
  2255. (conn->socks_request->usernamelen ||
  2256. conn->socks_request->passwordlen))) {
  2257. /* When stream isolation is in use and controlled by an application
  2258. * we are willing to keep using the stream. */
  2259. circ->base_.timestamp_dirty = approx_time();
  2260. }
  2261. pathbias_count_use_attempt(circ);
  2262. /* Now, actually link the connection. */
  2263. link_apconn_to_circ(conn, circ, cpath);
  2264. tor_assert(conn->socks_request);
  2265. if (conn->socks_request->command == SOCKS_COMMAND_CONNECT) {
  2266. if (!conn->use_begindir)
  2267. consider_recording_trackhost(conn, circ);
  2268. if (connection_ap_handshake_send_begin(conn) < 0)
  2269. return -1;
  2270. } else {
  2271. if (connection_ap_handshake_send_resolve(conn) < 0)
  2272. return -1;
  2273. }
  2274. return 1;
  2275. }
  2276. /** Try to find a safe live circuit for stream <b>conn</b>. If we find one,
  2277. * attach the stream, send appropriate cells, and return 1. Otherwise,
  2278. * try to launch new circuit(s) for the stream. If we can launch
  2279. * circuits, return 0. Otherwise, if we simply can't proceed with
  2280. * this stream, return -1. (conn needs to die, and is maybe already marked).
  2281. */
  2282. /* XXXX this function should mark for close whenever it returns -1;
  2283. * its callers shouldn't have to worry about that. */
  2284. int
  2285. connection_ap_handshake_attach_circuit(entry_connection_t *conn)
  2286. {
  2287. connection_t *base_conn = ENTRY_TO_CONN(conn);
  2288. int retval;
  2289. int conn_age;
  2290. int want_onehop;
  2291. tor_assert(conn);
  2292. tor_assert(base_conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  2293. tor_assert(conn->socks_request);
  2294. want_onehop = conn->want_onehop;
  2295. conn_age = (int)(time(NULL) - base_conn->timestamp_created);
  2296. /* Is this connection so old that we should give up on it? */
  2297. if (conn_age >= get_options()->SocksTimeout) {
  2298. int severity = (tor_addr_is_null(&base_conn->addr) && !base_conn->port) ?
  2299. LOG_INFO : LOG_NOTICE;
  2300. log_fn(severity, LD_APP,
  2301. "Tried for %d seconds to get a connection to %s:%d. Giving up.",
  2302. conn_age, safe_str_client(conn->socks_request->address),
  2303. conn->socks_request->port);
  2304. return -1;
  2305. }
  2306. /* We handle "general" (non-onion) connections much more straightforwardly.
  2307. */
  2308. if (!connection_edge_is_rendezvous_stream(ENTRY_TO_EDGE_CONN(conn))) {
  2309. /* we're a general conn */
  2310. origin_circuit_t *circ=NULL;
  2311. /* Are we linked to a dir conn that aims to fetch a consensus?
  2312. * We check here because the conn might no longer be needed. */
  2313. if (base_conn->linked_conn &&
  2314. base_conn->linked_conn->type == CONN_TYPE_DIR &&
  2315. base_conn->linked_conn->purpose == DIR_PURPOSE_FETCH_CONSENSUS) {
  2316. /* Yes we are. Is there a consensus fetch farther along than us? */
  2317. if (networkstatus_consensus_is_already_downloading(
  2318. TO_DIR_CONN(base_conn->linked_conn)->requested_resource)) {
  2319. /* We're doing the "multiple consensus fetch attempts" game from
  2320. * proposal 210, and we're late to the party. Just close this conn.
  2321. * The circuit and TLS conn that we made will time out after a while
  2322. * if nothing else wants to use them. */
  2323. log_info(LD_DIR, "Closing extra consensus fetch (to %s) since one "
  2324. "is already downloading.", base_conn->linked_conn->address);
  2325. return -1;
  2326. }
  2327. }
  2328. /* If we have a chosen exit, we need to use a circuit that's
  2329. * open to that exit. See what exit we meant, and whether we can use it.
  2330. */
  2331. if (conn->chosen_exit_name) {
  2332. const node_t *node = node_get_by_nickname(conn->chosen_exit_name, 1);
  2333. int opt = conn->chosen_exit_optional;
  2334. if (!node && !want_onehop) {
  2335. /* We ran into this warning when trying to extend a circuit to a
  2336. * hidden service directory for which we didn't have a router
  2337. * descriptor. See flyspray task 767 for more details. We should
  2338. * keep this in mind when deciding to use BEGIN_DIR cells for other
  2339. * directory requests as well. -KL*/
  2340. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  2341. "Requested exit point '%s' is not known. %s.",
  2342. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  2343. if (opt) {
  2344. /* If we are allowed to ignore the .exit request, do so */
  2345. conn->chosen_exit_optional = 0;
  2346. tor_free(conn->chosen_exit_name);
  2347. return 0;
  2348. }
  2349. return -1;
  2350. }
  2351. if (node && !connection_ap_can_use_exit(conn, node)) {
  2352. log_fn(opt ? LOG_INFO : LOG_WARN, LD_APP,
  2353. "Requested exit point '%s' is excluded or "
  2354. "would refuse request. %s.",
  2355. conn->chosen_exit_name, opt ? "Trying others" : "Closing");
  2356. if (opt) {
  2357. /* If we are allowed to ignore the .exit request, do so */
  2358. conn->chosen_exit_optional = 0;
  2359. tor_free(conn->chosen_exit_name);
  2360. return 0;
  2361. }
  2362. return -1;
  2363. }
  2364. }
  2365. /* Find the circuit that we should use, if there is one. Otherwise
  2366. * launch it. */
  2367. retval = circuit_get_open_circ_or_launch(
  2368. conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
  2369. if (retval < 1) {
  2370. /* We were either told "-1" (complete failure) or 0 (circuit in
  2371. * progress); we can't attach this stream yet. */
  2372. return retval;
  2373. }
  2374. log_debug(LD_APP|LD_CIRC,
  2375. "Attaching apconn to circ %u (stream %d sec old).",
  2376. (unsigned)circ->base_.n_circ_id, conn_age);
  2377. /* print the circ's path, so clients can figure out which circs are
  2378. * sucking. */
  2379. circuit_log_path(LOG_INFO,LD_APP|LD_CIRC,circ);
  2380. /* We have found a suitable circuit for our conn. Hurray. Do
  2381. * the attachment. */
  2382. return connection_ap_handshake_attach_chosen_circuit(conn, circ, NULL);
  2383. } else { /* we're a rendezvous conn */
  2384. origin_circuit_t *rendcirc=NULL, *introcirc=NULL;
  2385. tor_assert(!ENTRY_TO_EDGE_CONN(conn)->cpath_layer);
  2386. /* start by finding a rendezvous circuit for us */
  2387. retval = circuit_get_open_circ_or_launch(
  2388. conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
  2389. if (retval < 0) return -1; /* failed */
  2390. if (retval > 0) {
  2391. tor_assert(rendcirc);
  2392. /* one is already established, attach */
  2393. log_info(LD_REND,
  2394. "rend joined circ %u already here. attaching. "
  2395. "(stream %d sec old)",
  2396. (unsigned)rendcirc->base_.n_circ_id, conn_age);
  2397. /* Mark rendezvous circuits as 'newly dirty' every time you use
  2398. * them, since the process of rebuilding a rendezvous circ is so
  2399. * expensive. There is a tradeoff between linkability and
  2400. * feasibility, at this point.
  2401. */
  2402. rendcirc->base_.timestamp_dirty = time(NULL);
  2403. /* We've also attempted to use them. If they fail, we need to
  2404. * probe them for path bias */
  2405. pathbias_count_use_attempt(rendcirc);
  2406. link_apconn_to_circ(conn, rendcirc, NULL);
  2407. if (connection_ap_handshake_send_begin(conn) < 0)
  2408. return 0; /* already marked, let them fade away */
  2409. return 1;
  2410. }
  2411. /* At this point we need to re-check the state, since it's possible that
  2412. * our call to circuit_get_open_circ_or_launch() changed the connection's
  2413. * state from "CIRCUIT_WAIT" to "RENDDESC_WAIT" because we decided to
  2414. * re-fetch the descriptor.
  2415. */
  2416. if (ENTRY_TO_CONN(conn)->state != AP_CONN_STATE_CIRCUIT_WAIT) {
  2417. log_info(LD_REND, "This connection is no longer ready to attach; its "
  2418. "state changed."
  2419. "(We probably have to re-fetch its descriptor.)");
  2420. return 0;
  2421. }
  2422. if (rendcirc && (rendcirc->base_.purpose ==
  2423. CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED)) {
  2424. log_info(LD_REND,
  2425. "pending-join circ %u already here, with intro ack. "
  2426. "Stalling. (stream %d sec old)",
  2427. (unsigned)rendcirc->base_.n_circ_id, conn_age);
  2428. return 0;
  2429. }
  2430. /* it's on its way. find an intro circ. */
  2431. retval = circuit_get_open_circ_or_launch(
  2432. conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
  2433. if (retval < 0) return -1; /* failed */
  2434. if (retval > 0) {
  2435. /* one has already sent the intro. keep waiting. */
  2436. tor_assert(introcirc);
  2437. log_info(LD_REND, "Intro circ %u present and awaiting ack (rend %u). "
  2438. "Stalling. (stream %d sec old)",
  2439. (unsigned)introcirc->base_.n_circ_id,
  2440. rendcirc ? (unsigned)rendcirc->base_.n_circ_id : 0,
  2441. conn_age);
  2442. return 0;
  2443. }
  2444. /* now rendcirc and introcirc are each either undefined or not finished */
  2445. if (rendcirc && introcirc &&
  2446. rendcirc->base_.purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  2447. log_info(LD_REND,
  2448. "ready rend circ %u already here (no intro-ack yet on "
  2449. "intro %u). (stream %d sec old)",
  2450. (unsigned)rendcirc->base_.n_circ_id,
  2451. (unsigned)introcirc->base_.n_circ_id, conn_age);
  2452. tor_assert(introcirc->base_.purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  2453. if (introcirc->base_.state == CIRCUIT_STATE_OPEN) {
  2454. log_info(LD_REND,"found open intro circ %u (rend %u); sending "
  2455. "introduction. (stream %d sec old)",
  2456. (unsigned)introcirc->base_.n_circ_id,
  2457. (unsigned)rendcirc->base_.n_circ_id,
  2458. conn_age);
  2459. switch (rend_client_send_introduction(introcirc, rendcirc)) {
  2460. case 0: /* success */
  2461. rendcirc->base_.timestamp_dirty = time(NULL);
  2462. introcirc->base_.timestamp_dirty = time(NULL);
  2463. pathbias_count_use_attempt(introcirc);
  2464. pathbias_count_use_attempt(rendcirc);
  2465. assert_circuit_ok(TO_CIRCUIT(rendcirc));
  2466. assert_circuit_ok(TO_CIRCUIT(introcirc));
  2467. return 0;
  2468. case -1: /* transient error */
  2469. return 0;
  2470. case -2: /* permanent error */
  2471. return -1;
  2472. default: /* oops */
  2473. tor_fragile_assert();
  2474. return -1;
  2475. }
  2476. }
  2477. }
  2478. log_info(LD_REND, "Intro (%u) and rend (%u) circs are not both ready. "
  2479. "Stalling conn. (%d sec old)",
  2480. introcirc ? (unsigned)introcirc->base_.n_circ_id : 0,
  2481. rendcirc ? (unsigned)rendcirc->base_.n_circ_id : 0, conn_age);
  2482. return 0;
  2483. }
  2484. }
  2485. /** Change <b>circ</b>'s purpose to <b>new_purpose</b>. */
  2486. void
  2487. circuit_change_purpose(circuit_t *circ, uint8_t new_purpose)
  2488. {
  2489. uint8_t old_purpose;
  2490. /* Don't allow an OR circ to become an origin circ or vice versa. */
  2491. tor_assert(!!(CIRCUIT_IS_ORIGIN(circ)) ==
  2492. !!(CIRCUIT_PURPOSE_IS_ORIGIN(new_purpose)));
  2493. if (circ->purpose == new_purpose) return;
  2494. if (CIRCUIT_IS_ORIGIN(circ)) {
  2495. char old_purpose_desc[80] = "";
  2496. strncpy(old_purpose_desc, circuit_purpose_to_string(circ->purpose), 80-1);
  2497. old_purpose_desc[80-1] = '\0';
  2498. log_debug(LD_CIRC,
  2499. "changing purpose of origin circ %d "
  2500. "from \"%s\" (%d) to \"%s\" (%d)",
  2501. TO_ORIGIN_CIRCUIT(circ)->global_identifier,
  2502. old_purpose_desc,
  2503. circ->purpose,
  2504. circuit_purpose_to_string(new_purpose),
  2505. new_purpose);
  2506. }
  2507. old_purpose = circ->purpose;
  2508. circ->purpose = new_purpose;
  2509. if (CIRCUIT_IS_ORIGIN(circ)) {
  2510. control_event_circuit_purpose_changed(TO_ORIGIN_CIRCUIT(circ),
  2511. old_purpose);
  2512. }
  2513. }
  2514. /** Mark <b>circ</b> so that no more connections can be attached to it. */
  2515. void
  2516. mark_circuit_unusable_for_new_conns(origin_circuit_t *circ)
  2517. {
  2518. const or_options_t *options = get_options();
  2519. tor_assert(circ);
  2520. /* XXXX This is a kludge; we're only keeping it around in case there's
  2521. * something that doesn't check unusable_for_new_conns, and to avoid
  2522. * deeper refactoring of our expiration logic. */
  2523. if (! circ->base_.timestamp_dirty)
  2524. circ->base_.timestamp_dirty = approx_time();
  2525. if (options->MaxCircuitDirtiness >= circ->base_.timestamp_dirty)
  2526. circ->base_.timestamp_dirty = 1; /* prevent underflow */
  2527. else
  2528. circ->base_.timestamp_dirty -= options->MaxCircuitDirtiness;
  2529. circ->unusable_for_new_conns = 1;
  2530. }