circuituse.c 72 KB

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