circuituse.c 95 KB

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