circuituse.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /* Copyright 2001 Matej Pfajfar, 2001-2004 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file circuituse.c
  6. * \brief Launch the right sort of circuits, attach streams to them.
  7. **/
  8. #include "or.h"
  9. extern or_options_t options; /* command-line and config-file options */
  10. /********* START VARIABLES **********/
  11. extern circuit_t *global_circuitlist; /* from circuitlist.c */
  12. extern int has_fetched_directory; /* from main.c */
  13. /********* END VARIABLES ************/
  14. static void circuit_expire_old_circuits(void);
  15. static void circuit_increment_failure_count(void);
  16. /* Return 1 if <b>circ</b> could be returned by circuit_get_best().
  17. * Else return 0.
  18. */
  19. static int circuit_is_acceptable(circuit_t *circ,
  20. connection_t *conn,
  21. int must_be_open,
  22. uint8_t purpose,
  23. time_t now)
  24. {
  25. routerinfo_t *exitrouter;
  26. if (!CIRCUIT_IS_ORIGIN(circ))
  27. return 0; /* this circ doesn't start at us */
  28. if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  29. return 0; /* ignore non-open circs */
  30. if (circ->marked_for_close)
  31. return 0;
  32. /* if this circ isn't our purpose, skip. */
  33. if(purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
  34. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  35. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  36. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
  37. circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
  38. return 0;
  39. } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT && !must_be_open) {
  40. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  41. circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  42. return 0;
  43. } else {
  44. if(purpose != circ->purpose)
  45. return 0;
  46. }
  47. if(purpose == CIRCUIT_PURPOSE_C_GENERAL)
  48. if(circ->timestamp_dirty &&
  49. circ->timestamp_dirty+options.NewCircuitPeriod <= now)
  50. return 0;
  51. if(conn) {
  52. /* decide if this circ is suitable for this conn */
  53. /* for rend circs, circ->cpath->prev is not the last router in the
  54. * circuit, it's the magical extra bob hop. so just check the nickname
  55. * of the one we meant to finish at.
  56. */
  57. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  58. if(!exitrouter) {
  59. log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
  60. return 0; /* this circuit is screwed and doesn't know it yet */
  61. }
  62. if (conn->socks_request &&
  63. conn->socks_request->command == SOCKS_COMMAND_RESOLVE) {
  64. /* 0.0.7 servers and earlier don't support DNS resolution. There are no
  65. * ORs running code before 0.0.7, so we only worry about 0.0.7. Once all
  66. * servers are running 0.0.8, remove this check. */
  67. if (!strncmp(exitrouter->platform, "Tor 0.0.7", 9))
  68. return 0;
  69. } else if(purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  70. if(!connection_ap_can_use_exit(conn, exitrouter)) {
  71. /* can't exit from this router */
  72. return 0;
  73. }
  74. } else { /* not general */
  75. if(rend_cmp_service_ids(conn->rend_query, circ->rend_query) &&
  76. (circ->rend_query[0] || purpose != CIRCUIT_PURPOSE_C_REND_JOINED)) {
  77. /* this circ is not for this conn, and it's not suitable
  78. * for cannibalizing either */
  79. return 0;
  80. }
  81. }
  82. }
  83. return 1;
  84. }
  85. /* Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
  86. * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
  87. */
  88. static int circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
  89. {
  90. switch(purpose) {
  91. case CIRCUIT_PURPOSE_C_GENERAL:
  92. /* if it's used but less dirty it's best;
  93. * else if it's more recently created it's best
  94. */
  95. if(b->timestamp_dirty) {
  96. if(a->timestamp_dirty &&
  97. a->timestamp_dirty > b->timestamp_dirty)
  98. return 1;
  99. } else {
  100. if(a->timestamp_dirty ||
  101. a->timestamp_created > b->timestamp_created)
  102. return 1;
  103. }
  104. break;
  105. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  106. /* the closer it is to ack_wait the better it is */
  107. if(a->purpose > b->purpose)
  108. return 1;
  109. break;
  110. case CIRCUIT_PURPOSE_C_REND_JOINED:
  111. /* the closer it is to rend_joined the better it is */
  112. if(a->purpose > b->purpose)
  113. return 1;
  114. break;
  115. }
  116. return 0;
  117. }
  118. /** Find the best circ that conn can use, preferably one which is
  119. * dirty. Circ must not be too old.
  120. *
  121. * Conn must be defined.
  122. *
  123. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  124. *
  125. * circ_purpose specifies what sort of circuit we must have.
  126. * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
  127. *
  128. * If it's REND_JOINED and must_be_open==0, then return the closest
  129. * rendezvous-purposed circuit that you can find.
  130. *
  131. * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
  132. * closest introduce-purposed circuit that you can find.
  133. */
  134. static circuit_t *
  135. circuit_get_best(connection_t *conn, int must_be_open, uint8_t purpose)
  136. {
  137. circuit_t *circ, *best=NULL;
  138. time_t now = time(NULL);
  139. tor_assert(conn);
  140. tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  141. purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
  142. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  143. for (circ=global_circuitlist;circ;circ = circ->next) {
  144. if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,now))
  145. continue;
  146. /* now this is an acceptable circ to hand back. but that doesn't
  147. * mean it's the *best* circ to hand back. try to decide.
  148. */
  149. if(!best || circuit_is_better(circ,best,purpose))
  150. best = circ;
  151. }
  152. return best;
  153. }
  154. /** Circuits that were born at the end of their second might be expired
  155. * after 30.1 seconds; circuits born at the beginning might be expired
  156. * after closer to 31 seconds.
  157. */
  158. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 30
  159. /** Close all circuits that start at us, aren't open, and were born
  160. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago.
  161. */
  162. void circuit_expire_building(time_t now) {
  163. circuit_t *victim, *circ = global_circuitlist;
  164. while(circ) {
  165. victim = circ;
  166. circ = circ->next;
  167. if(!CIRCUIT_IS_ORIGIN(victim))
  168. continue; /* didn't originate here */
  169. if(victim->marked_for_close)
  170. continue; /* don't mess with marked circs */
  171. if(victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)
  172. continue; /* it's young still, don't mess with it */
  173. /* some debug logs, to help track bugs */
  174. if(victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  175. victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  176. if(!victim->timestamp_dirty)
  177. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). (clean).",
  178. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  179. victim->purpose, victim->build_state->chosen_exit_name,
  180. victim->n_circ_id);
  181. else
  182. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). %d secs since dirty.",
  183. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  184. victim->purpose, victim->build_state->chosen_exit_name,
  185. victim->n_circ_id,
  186. (int)(now - victim->timestamp_dirty));
  187. }
  188. /* if circ is !open, or if it's open but purpose is a non-finished
  189. * intro or rend, then mark it for close */
  190. if(victim->state != CIRCUIT_STATE_OPEN ||
  191. victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  192. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  193. victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  194. /* it's a rend_ready circ, but it's already picked a query */
  195. (victim->purpose == CIRCUIT_PURPOSE_C_REND_READY &&
  196. victim->rend_query[0]) ||
  197. /* c_rend_ready circs measure age since timestamp_dirty,
  198. * because that's set when they switch purposes
  199. */
  200. /* rend and intro circs become dirty each time they
  201. * make an introduction attempt. so timestamp_dirty
  202. * will reflect the time since the last attempt.
  203. */
  204. ((victim->purpose == CIRCUIT_PURPOSE_C_REND_READY ||
  205. victim->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED ||
  206. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) &&
  207. victim->timestamp_dirty + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)) {
  208. if(victim->n_conn)
  209. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
  210. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  211. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  212. else
  213. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s, purpose %d)", victim->n_circ_id,
  214. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  215. circuit_log_path(LOG_INFO,victim);
  216. circuit_mark_for_close(victim);
  217. }
  218. }
  219. }
  220. /** How many circuits do we want simultaneously in-progress to handle
  221. * a given stream?
  222. */
  223. #define MIN_CIRCUITS_HANDLING_STREAM 2
  224. /** Return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open
  225. * general-purpose circuits will have an acceptable exit node for
  226. * conn. Else return 0.
  227. */
  228. int circuit_stream_is_being_handled(connection_t *conn) {
  229. circuit_t *circ;
  230. routerinfo_t *exitrouter;
  231. int num=0;
  232. time_t now = time(NULL);
  233. for(circ=global_circuitlist;circ;circ = circ->next) {
  234. if(CIRCUIT_IS_ORIGIN(circ) && circ->state != CIRCUIT_STATE_OPEN &&
  235. !circ->marked_for_close && circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  236. (!circ->timestamp_dirty ||
  237. circ->timestamp_dirty + options.NewCircuitPeriod < now)) {
  238. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  239. if(exitrouter && connection_ap_can_use_exit(conn, exitrouter))
  240. if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
  241. return 1;
  242. }
  243. }
  244. return 0;
  245. }
  246. /** Build a new test circuit every 5 minutes */
  247. #define TESTING_CIRCUIT_INTERVAL 300
  248. /** This function is called once a second. Its job is to make sure
  249. * all services we offer have enough circuits available. Some
  250. * services just want enough circuits for current tasks, whereas
  251. * others want a minimum set of idle circuits hanging around.
  252. */
  253. void circuit_build_needed_circs(time_t now) {
  254. static long time_to_new_circuit = 0;
  255. circuit_t *circ;
  256. /* launch a new circ for any pending streams that need one */
  257. connection_ap_attach_pending();
  258. /* make sure any hidden services have enough intro points */
  259. if(has_fetched_directory)
  260. rend_services_introduce();
  261. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  262. if(time_to_new_circuit < now) {
  263. circuit_reset_failure_count(1);
  264. time_to_new_circuit = now + options.NewCircuitPeriod;
  265. if(proxy_mode())
  266. client_dns_clean();
  267. circuit_expire_old_circuits();
  268. if(options.RunTesting && circ &&
  269. circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
  270. log_fn(LOG_INFO,"Creating a new testing circuit.");
  271. circuit_launch_by_identity(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  272. }
  273. }
  274. /** How many simultaneous in-progress general-purpose circuits do we
  275. * want to be building at once, if there are no open general-purpose
  276. * circuits?
  277. */
  278. #define CIRCUIT_MIN_BUILDING_GENERAL 3
  279. /* if there's no open circ, and less than 3 are on the way,
  280. * go ahead and try another. */
  281. if(!circ && circuit_count_building(CIRCUIT_PURPOSE_C_GENERAL)
  282. < CIRCUIT_MIN_BUILDING_GENERAL) {
  283. circuit_launch_by_identity(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  284. }
  285. /* XXX count idle rendezvous circs and build more */
  286. }
  287. /** If the stream <b>conn</b> is a member of any of the linked
  288. * lists of <b>circ</b>, then remove it from the list.
  289. */
  290. void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
  291. connection_t *prevconn;
  292. tor_assert(circ && conn);
  293. if(conn == circ->p_streams) {
  294. circ->p_streams = conn->next_stream;
  295. return;
  296. }
  297. if(conn == circ->n_streams) {
  298. circ->n_streams = conn->next_stream;
  299. return;
  300. }
  301. if(conn == circ->resolving_streams) {
  302. circ->resolving_streams = conn->next_stream;
  303. return;
  304. }
  305. for(prevconn = circ->p_streams;
  306. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  307. prevconn = prevconn->next_stream)
  308. ;
  309. if(prevconn && prevconn->next_stream) {
  310. prevconn->next_stream = conn->next_stream;
  311. return;
  312. }
  313. for(prevconn = circ->n_streams;
  314. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  315. prevconn = prevconn->next_stream)
  316. ;
  317. if(prevconn && prevconn->next_stream) {
  318. prevconn->next_stream = conn->next_stream;
  319. return;
  320. }
  321. for(prevconn = circ->resolving_streams;
  322. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  323. prevconn = prevconn->next_stream)
  324. ;
  325. if(prevconn && prevconn->next_stream) {
  326. prevconn->next_stream = conn->next_stream;
  327. return;
  328. }
  329. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  330. tor_assert(0); /* should never get here */
  331. }
  332. /** Notify the global circuit list that <b>conn</b> is about to be
  333. * removed and then freed.
  334. *
  335. * If it's an OR conn, then mark-for-close all the circuits that use
  336. * that conn.
  337. *
  338. * If it's an edge conn, then detach it from its circ, so we don't
  339. * try to reference it later.
  340. */
  341. void circuit_about_to_close_connection(connection_t *conn) {
  342. /* currently, we assume it's too late to flush conn's buf here.
  343. * down the road, maybe we'll consider that eof doesn't mean can't-write
  344. */
  345. circuit_t *circ;
  346. switch(conn->type) {
  347. case CONN_TYPE_OR:
  348. if(conn->state != OR_CONN_STATE_OPEN) {
  349. /* Inform any pending (not attached) circs that they should give up. */
  350. circuit_n_conn_done(conn, 0);
  351. }
  352. /* Now close all the attached circuits on it. */
  353. while((circ = circuit_get_by_conn(conn))) {
  354. if(circ->n_conn == conn) /* it's closing in front of us */
  355. circ->n_conn = NULL;
  356. if(circ->p_conn == conn) /* it's closing behind us */
  357. circ->p_conn = NULL;
  358. circuit_mark_for_close(circ);
  359. }
  360. return;
  361. case CONN_TYPE_AP:
  362. case CONN_TYPE_EXIT:
  363. /* It's an edge conn. Need to remove it from the linked list of
  364. * conn's for this circuit. Confirm that 'end' relay command has
  365. * been sent. But don't kill the circuit.
  366. */
  367. circ = circuit_get_by_conn(conn);
  368. if(!circ)
  369. return;
  370. circuit_detach_stream(circ, conn);
  371. } /* end switch */
  372. }
  373. /** Don't keep more than 10 unused open circuits around. */
  374. #define MAX_UNUSED_OPEN_CIRCUITS 10
  375. /** Find each circuit that has been dirty for too long, and has
  376. * no streams on it: mark it for close.
  377. *
  378. * Also, if there are more than MAX_UNUSED_OPEN_CIRCUITS open and
  379. * unused circuits, then mark the excess circs for close.
  380. */
  381. static void
  382. circuit_expire_old_circuits(void)
  383. {
  384. circuit_t *circ;
  385. time_t now = time(NULL);
  386. smartlist_t *unused_open_circs;
  387. int i;
  388. unused_open_circs = smartlist_create();
  389. for (circ = global_circuitlist; circ; circ = circ->next) {
  390. if (circ->marked_for_close)
  391. continue;
  392. /* If the circuit has been dirty for too long, and there are no streams
  393. * on it, mark it for close.
  394. */
  395. if (circ->timestamp_dirty &&
  396. circ->timestamp_dirty + options.NewCircuitPeriod < now &&
  397. !circ->p_conn && /* we're the origin */
  398. !circ->p_streams /* nothing attached */ ) {
  399. log_fn(LOG_DEBUG,"Closing n_circ_id %d (dirty %d secs ago, purp %d)",circ->n_circ_id,
  400. (int)(now - circ->timestamp_dirty), circ->purpose);
  401. /* (only general and purpose_c circs can get dirty) */
  402. tor_assert(!circ->n_streams);
  403. tor_assert(circ->purpose <= CIRCUIT_PURPOSE_C_REND_JOINED);
  404. circuit_mark_for_close(circ);
  405. } else if (!circ->timestamp_dirty && CIRCUIT_IS_ORIGIN(circ) &&
  406. circ->state == CIRCUIT_STATE_OPEN &&
  407. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  408. /* Also, gather a list of open unused general circuits that we created.
  409. * Because we add elements to the front of global_circuitlist,
  410. * the last elements of unused_open_circs will be the oldest
  411. * ones.
  412. */
  413. smartlist_add(unused_open_circs, circ);
  414. }
  415. }
  416. for (i = MAX_UNUSED_OPEN_CIRCUITS; i < smartlist_len(unused_open_circs); ++i) {
  417. circuit_t *circ = smartlist_get(unused_open_circs, i);
  418. log_fn(LOG_DEBUG,"Expiring excess clean circ (n_circ_id %d, purp %d)",
  419. circ->n_circ_id, circ->purpose);
  420. circuit_mark_for_close(circ);
  421. }
  422. smartlist_free(unused_open_circs);
  423. }
  424. /** The circuit <b>circ</b> has just become open. Take the next
  425. * step: for rendezvous circuits, we pass circ to the appropriate
  426. * function in rendclient or rendservice. For general circuits, we
  427. * call connection_ap_attach_pending, which looks for pending streams
  428. * that could use circ.
  429. */
  430. void circuit_has_opened(circuit_t *circ) {
  431. switch(circ->purpose) {
  432. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  433. rend_client_rendcirc_has_opened(circ);
  434. break;
  435. case CIRCUIT_PURPOSE_C_INTRODUCING:
  436. rend_client_introcirc_has_opened(circ);
  437. break;
  438. case CIRCUIT_PURPOSE_C_GENERAL:
  439. /* Tell any AP connections that have been waiting for a new
  440. * circuit that one is ready. */
  441. connection_ap_attach_pending();
  442. break;
  443. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  444. /* at Bob, waiting for introductions */
  445. rend_service_intro_has_opened(circ);
  446. break;
  447. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  448. /* at Bob, connecting to rend point */
  449. rend_service_rendezvous_has_opened(circ);
  450. break;
  451. default:
  452. log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
  453. tor_assert(0);
  454. }
  455. }
  456. /*~ Called whenever a circuit could not be successfully built.
  457. */
  458. void circuit_build_failed(circuit_t *circ) {
  459. /* we should examine circ and see if it failed because of
  460. * the last hop or an earlier hop. then use this info below.
  461. */
  462. int failed_at_last_hop = 0;
  463. /* If the last hop isn't open, and the second-to-last is, we failed
  464. * at the last hop. */
  465. if (circ->cpath &&
  466. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  467. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  468. failed_at_last_hop = 1;
  469. }
  470. switch(circ->purpose) {
  471. case CIRCUIT_PURPOSE_C_GENERAL:
  472. if (circ->state != CIRCUIT_STATE_OPEN) {
  473. /* If we never built the circuit, note it as a failure. */
  474. /* Note that we can't just check circ->cpath here, because if
  475. * circuit-building failed immediately, it won't be set yet. */
  476. circuit_increment_failure_count();
  477. }
  478. break;
  479. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  480. /* at Bob, waiting for introductions */
  481. if (circ->state != CIRCUIT_STATE_OPEN) {
  482. circuit_increment_failure_count();
  483. }
  484. /* no need to care here, because bob will rebuild intro
  485. * points periodically. */
  486. break;
  487. case CIRCUIT_PURPOSE_C_INTRODUCING:
  488. /* at Alice, connecting to intro point */
  489. /* Don't increment failure count, since Bob may have picked
  490. * the introduction point maliciously */
  491. /* Alice will pick a new intro point when this one dies, if
  492. * the stream in question still cares. No need to act here. */
  493. break;
  494. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  495. /* at Alice, waiting for Bob */
  496. if (circ->state != CIRCUIT_STATE_OPEN) {
  497. circuit_increment_failure_count();
  498. }
  499. /* Alice will pick a new rend point when this one dies, if
  500. * the stream in question still cares. No need to act here. */
  501. break;
  502. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  503. /* at Bob, connecting to rend point */
  504. /* Don't increment failure count, since Alice may have picked
  505. * the rendezvous point maliciously */
  506. if (failed_at_last_hop) {
  507. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s. Sucks to be Alice.", circ->build_state->chosen_exit_name);
  508. } else {
  509. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s, because an earlier node failed.",
  510. circ->build_state->chosen_exit_name);
  511. rend_service_relaunch_rendezvous(circ);
  512. }
  513. break;
  514. default:
  515. /* Other cases are impossible, since this function is only called with
  516. * unbuilt circuits. */
  517. tor_assert(0);
  518. }
  519. }
  520. /** Number of consecutive failures so far; should only be touched by
  521. * circuit_launch_new and circuit_*_failure_count.
  522. */
  523. static int n_circuit_failures = 0;
  524. static int did_circs_fail_last_period = 0;
  525. /** Don't retry launching a new circuit if we try this many times with no
  526. * success. */
  527. #define MAX_CIRCUIT_FAILURES 5
  528. circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest)
  529. {
  530. if (!has_fetched_directory) {
  531. log_fn(LOG_DEBUG,"Haven't fetched directory yet; cancelling circuit launch.");
  532. return NULL;
  533. }
  534. if (did_circs_fail_last_period &&
  535. n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  536. /* too many failed circs in a row. don't try. */
  537. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  538. return NULL;
  539. }
  540. /* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
  541. return circuit_establish_circuit(purpose, exit_digest);
  542. }
  543. /** Launch a new circuit and return a pointer to it. Return NULL if you failed. */
  544. circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname)
  545. {
  546. const char *digest = NULL;
  547. if (exit_nickname) {
  548. routerinfo_t *r = router_get_by_nickname(exit_nickname);
  549. if (!r) {
  550. log_fn(LOG_WARN, "No such OR as '%s'", exit_nickname);
  551. return NULL;
  552. }
  553. digest = r->identity_digest;
  554. }
  555. return circuit_launch_by_identity(purpose, digest);
  556. }
  557. /** Record another failure at opening a general circuit. When we have
  558. * too many, we'll stop trying for the remainder of this minute.
  559. */
  560. static void circuit_increment_failure_count(void) {
  561. ++n_circuit_failures;
  562. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  563. }
  564. /** Reset the failure count for opening general circuits. This means
  565. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  566. * stopping again.
  567. */
  568. void circuit_reset_failure_count(int timeout) {
  569. if(timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
  570. did_circs_fail_last_period = 1;
  571. else
  572. did_circs_fail_last_period = 0;
  573. n_circuit_failures = 0;
  574. }
  575. /** Find an open circ that we're happy with: return 1. If there isn't
  576. * one, and there isn't one on the way, launch one and return 0. If it
  577. * will never work, return -1.
  578. *
  579. * Write the found or in-progress or launched circ into *circp.
  580. */
  581. static int
  582. circuit_get_open_circ_or_launch(connection_t *conn,
  583. uint8_t desired_circuit_purpose,
  584. circuit_t **circp) {
  585. circuit_t *circ;
  586. uint32_t addr;
  587. int is_resolve;
  588. tor_assert(conn);
  589. tor_assert(circp);
  590. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  591. is_resolve = conn->socks_request->command == SOCKS_COMMAND_RESOLVE;
  592. circ = circuit_get_best(conn, 1, desired_circuit_purpose);
  593. if(circ) {
  594. *circp = circ;
  595. return 1; /* we're happy */
  596. }
  597. /* Do we need to check exit policy? */
  598. if(!is_resolve && !connection_edge_is_rendezvous_stream(conn)) {
  599. addr = client_dns_lookup_entry(conn->socks_request->address);
  600. if(router_exit_policy_all_routers_reject(addr, conn->socks_request->port)) {
  601. log_fn(LOG_WARN,"No Tor server exists that allows exit to %s:%d. Rejecting.",
  602. conn->socks_request->address, conn->socks_request->port);
  603. return -1;
  604. }
  605. }
  606. /* is one already on the way? */
  607. circ = circuit_get_best(conn, 0, desired_circuit_purpose);
  608. if(!circ) {
  609. char *exitname=NULL;
  610. uint8_t new_circ_purpose;
  611. if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  612. /* need to pick an intro point */
  613. exitname = rend_client_get_random_intro(conn->rend_query);
  614. if(!exitname) {
  615. log_fn(LOG_WARN,"Couldn't get an intro point for '%s'. Closing conn.",
  616. conn->rend_query);
  617. return -1;
  618. }
  619. if(!router_get_by_nickname(exitname)) {
  620. log_fn(LOG_WARN,"Advertised intro point '%s' is not known. Closing.", exitname);
  621. return -1;
  622. }
  623. /* XXX if we failed, then refetch the descriptor */
  624. log_fn(LOG_INFO,"Chose %s as intro point for %s.", exitname, conn->rend_query);
  625. }
  626. if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  627. new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  628. else if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  629. new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  630. else
  631. new_circ_purpose = desired_circuit_purpose;
  632. circ = circuit_launch_by_nickname(new_circ_purpose, exitname);
  633. tor_free(exitname);
  634. if(circ &&
  635. (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)) {
  636. /* then write the service_id into circ */
  637. strcpy(circ->rend_query, conn->rend_query);
  638. }
  639. }
  640. if(!circ)
  641. log_fn(LOG_INFO,"No safe circuit (purpose %d) ready for edge connection; delaying.",
  642. desired_circuit_purpose);
  643. *circp = circ;
  644. return 0;
  645. }
  646. /** Attach the AP stream <b>apconn</b> to circ's linked list of
  647. * p_streams. Also set apconn's cpath_layer to the last hop in
  648. * circ's cpath.
  649. */
  650. static void link_apconn_to_circ(connection_t *apconn, circuit_t *circ) {
  651. /* add it into the linked list of streams on this circuit */
  652. log_fn(LOG_DEBUG,"attaching new conn to circ. n_circ_id %d.", circ->n_circ_id);
  653. apconn->next_stream = circ->p_streams;
  654. /* assert_connection_ok(conn, time(NULL)); */
  655. circ->p_streams = apconn;
  656. tor_assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath && circ->cpath->prev);
  657. tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  658. apconn->cpath_layer = circ->cpath->prev;
  659. }
  660. /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
  661. * we don't find one: if conn cannot be handled by any known nodes,
  662. * warn and return -1 (conn needs to die);
  663. * else launch new circuit (if necessary) and return 0.
  664. * Otherwise, associate conn with a safe live circuit, do the
  665. * right next step, and return 1.
  666. */
  667. int connection_ap_handshake_attach_circuit(connection_t *conn) {
  668. int retval;
  669. int conn_age;
  670. tor_assert(conn);
  671. tor_assert(conn->type == CONN_TYPE_AP);
  672. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  673. tor_assert(conn->socks_request);
  674. conn_age = time(NULL) - conn->timestamp_created;
  675. if(conn_age > 60) {
  676. /* XXX make this cleaner than '60' */
  677. log_fn(LOG_WARN,"Giving up on unattached conn (%d sec old).", conn_age);
  678. return -1;
  679. }
  680. if(!connection_edge_is_rendezvous_stream(conn)) { /* we're a general conn */
  681. circuit_t *circ=NULL;
  682. /* find the circuit that we should use, if there is one. */
  683. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
  684. if(retval < 1)
  685. return retval;
  686. /* We have found a suitable circuit for our conn. Hurray. */
  687. log_fn(LOG_DEBUG,"Attaching apconn to general circ %d (stream %d sec old).",
  688. circ->n_circ_id, conn_age);
  689. /* here, print the circ's path. so people can figure out which circs are sucking. */
  690. circuit_log_path(LOG_INFO,circ);
  691. if(!circ->timestamp_dirty)
  692. circ->timestamp_dirty = time(NULL);
  693. link_apconn_to_circ(conn, circ);
  694. tor_assert(conn->socks_request);
  695. if (conn->socks_request->command == SOCKS_COMMAND_CONNECT)
  696. connection_ap_handshake_send_begin(conn, circ);
  697. else
  698. connection_ap_handshake_send_resolve(conn, circ);
  699. return 1;
  700. } else { /* we're a rendezvous conn */
  701. circuit_t *rendcirc=NULL, *introcirc=NULL;
  702. tor_assert(!conn->cpath_layer);
  703. /* start by finding a rendezvous circuit for us */
  704. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
  705. if(retval < 0) return -1; /* failed */
  706. tor_assert(rendcirc);
  707. if(retval > 0) {
  708. /* one is already established, attach */
  709. log_fn(LOG_INFO,"rend joined circ %d already here. attaching. (stream %d sec old)",
  710. rendcirc->n_circ_id, conn_age);
  711. link_apconn_to_circ(conn, rendcirc);
  712. if(connection_ap_handshake_send_begin(conn, rendcirc) < 0)
  713. return 0; /* already marked, let them fade away */
  714. return 1;
  715. }
  716. if(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  717. log_fn(LOG_INFO,"pending-join circ %d already here, with intro ack. Stalling. (stream %d sec old)", rendcirc->n_circ_id, conn_age);
  718. return 0;
  719. }
  720. /* it's on its way. find an intro circ. */
  721. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
  722. if(retval < 0) return -1; /* failed */
  723. tor_assert(introcirc);
  724. if(retval > 0) {
  725. /* one has already sent the intro. keep waiting. */
  726. log_fn(LOG_INFO,"Intro circ %d present and awaiting ack (rend %d). Stalling. (stream %d sec old)",
  727. introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  728. return 0;
  729. }
  730. /* now both rendcirc and introcirc are defined, and neither is finished */
  731. if(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  732. log_fn(LOG_INFO,"ready rend circ %d already here (no intro-ack yet on intro %d). (stream %d sec old)",
  733. rendcirc->n_circ_id, introcirc->n_circ_id, conn_age);
  734. /* look around for any new intro circs that should introduce */
  735. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  736. if(introcirc->state == CIRCUIT_STATE_OPEN) {
  737. log_fn(LOG_INFO,"found open intro circ %d (rend %d); sending introduction. (stream %d sec old)",
  738. introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  739. /* XXX here we should cannibalize the rend circ if it's a zero service id */
  740. if(rend_client_send_introduction(introcirc, rendcirc) < 0) {
  741. return -1;
  742. }
  743. rendcirc->timestamp_dirty = time(NULL);
  744. introcirc->timestamp_dirty = time(NULL);
  745. assert_circuit_ok(rendcirc);
  746. assert_circuit_ok(introcirc);
  747. return 0;
  748. }
  749. }
  750. log_fn(LOG_INFO,"Intro (%d) and rend (%d) circs are not both ready. Stalling conn. (%d sec old)", introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  751. return 0;
  752. }
  753. }
  754. /*
  755. Local Variables:
  756. mode:c
  757. indent-tabs-mode:nil
  758. c-basic-offset:2
  759. End:
  760. */