circuituse.c 77 KB

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