circuituse.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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.8 servers have buggy resolve support. */
  65. if (!tor_version_as_new_as(exitrouter->platform, "0.0.9pre1"))
  66. return 0;
  67. } else if(purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  68. if(!connection_ap_can_use_exit(conn, exitrouter)) {
  69. /* can't exit from this router */
  70. return 0;
  71. }
  72. } else { /* not general */
  73. if(rend_cmp_service_ids(conn->rend_query, circ->rend_query) &&
  74. (circ->rend_query[0] || purpose != CIRCUIT_PURPOSE_C_REND_JOINED)) {
  75. /* this circ is not for this conn, and it's not suitable
  76. * for cannibalizing either */
  77. return 0;
  78. }
  79. }
  80. }
  81. return 1;
  82. }
  83. /* Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
  84. * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
  85. */
  86. static int circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
  87. {
  88. switch(purpose) {
  89. case CIRCUIT_PURPOSE_C_GENERAL:
  90. /* if it's used but less dirty it's best;
  91. * else if it's more recently created it's best
  92. */
  93. if(b->timestamp_dirty) {
  94. if(a->timestamp_dirty &&
  95. a->timestamp_dirty > b->timestamp_dirty)
  96. return 1;
  97. } else {
  98. if(a->timestamp_dirty ||
  99. a->timestamp_created > b->timestamp_created)
  100. return 1;
  101. }
  102. break;
  103. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  104. /* the closer it is to ack_wait the better it is */
  105. if(a->purpose > b->purpose)
  106. return 1;
  107. break;
  108. case CIRCUIT_PURPOSE_C_REND_JOINED:
  109. /* the closer it is to rend_joined the better it is */
  110. if(a->purpose > b->purpose)
  111. return 1;
  112. break;
  113. }
  114. return 0;
  115. }
  116. /** Find the best circ that conn can use, preferably one which is
  117. * dirty. Circ must not be too old.
  118. *
  119. * Conn must be defined.
  120. *
  121. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  122. *
  123. * circ_purpose specifies what sort of circuit we must have.
  124. * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
  125. *
  126. * If it's REND_JOINED and must_be_open==0, then return the closest
  127. * rendezvous-purposed circuit that you can find.
  128. *
  129. * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
  130. * closest introduce-purposed circuit that you can find.
  131. */
  132. static circuit_t *
  133. circuit_get_best(connection_t *conn, int must_be_open, uint8_t purpose)
  134. {
  135. circuit_t *circ, *best=NULL;
  136. time_t now = time(NULL);
  137. tor_assert(conn);
  138. tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  139. purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
  140. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  141. for (circ=global_circuitlist;circ;circ = circ->next) {
  142. if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,now))
  143. continue;
  144. /* now this is an acceptable circ to hand back. but that doesn't
  145. * mean it's the *best* circ to hand back. try to decide.
  146. */
  147. if(!best || circuit_is_better(circ,best,purpose))
  148. best = circ;
  149. }
  150. return best;
  151. }
  152. /** Circuits that were born at the end of their second might be expired
  153. * after 30.1 seconds; circuits born at the beginning might be expired
  154. * after closer to 31 seconds.
  155. */
  156. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 30
  157. /** Close all circuits that start at us, aren't open, and were born
  158. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago.
  159. */
  160. void circuit_expire_building(time_t now) {
  161. circuit_t *victim, *circ = global_circuitlist;
  162. while(circ) {
  163. victim = circ;
  164. circ = circ->next;
  165. if(!CIRCUIT_IS_ORIGIN(victim))
  166. continue; /* didn't originate here */
  167. if(victim->marked_for_close)
  168. continue; /* don't mess with marked circs */
  169. if(victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)
  170. continue; /* it's young still, don't mess with it */
  171. /* some debug logs, to help track bugs */
  172. if(victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  173. victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  174. if(!victim->timestamp_dirty)
  175. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). (clean).",
  176. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  177. victim->purpose, victim->build_state->chosen_exit_name,
  178. victim->n_circ_id);
  179. else
  180. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). %d secs since dirty.",
  181. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  182. victim->purpose, victim->build_state->chosen_exit_name,
  183. victim->n_circ_id,
  184. (int)(now - victim->timestamp_dirty));
  185. }
  186. /* if circ is !open, or if it's open but purpose is a non-finished
  187. * intro or rend, then mark it for close */
  188. if(victim->state != CIRCUIT_STATE_OPEN ||
  189. victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  190. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  191. victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  192. /* it's a rend_ready circ, but it's already picked a query */
  193. (victim->purpose == CIRCUIT_PURPOSE_C_REND_READY &&
  194. victim->rend_query[0]) ||
  195. /* c_rend_ready circs measure age since timestamp_dirty,
  196. * because that's set when they switch purposes
  197. */
  198. /* rend and intro circs become dirty each time they
  199. * make an introduction attempt. so timestamp_dirty
  200. * will reflect the time since the last attempt.
  201. */
  202. ((victim->purpose == CIRCUIT_PURPOSE_C_REND_READY ||
  203. victim->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED ||
  204. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) &&
  205. victim->timestamp_dirty + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)) {
  206. if(victim->n_conn)
  207. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
  208. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  209. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  210. else
  211. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s, purpose %d)", victim->n_circ_id,
  212. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  213. circuit_log_path(LOG_INFO,victim);
  214. circuit_mark_for_close(victim);
  215. }
  216. }
  217. }
  218. /** How many circuits do we want simultaneously in-progress to handle
  219. * a given stream?
  220. */
  221. #define MIN_CIRCUITS_HANDLING_STREAM 2
  222. /** Return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open
  223. * general-purpose circuits will have an acceptable exit node for
  224. * conn. Else return 0.
  225. */
  226. int circuit_stream_is_being_handled(connection_t *conn) {
  227. circuit_t *circ;
  228. routerinfo_t *exitrouter;
  229. int num=0;
  230. time_t now = time(NULL);
  231. for(circ=global_circuitlist;circ;circ = circ->next) {
  232. if(CIRCUIT_IS_ORIGIN(circ) && circ->state != CIRCUIT_STATE_OPEN &&
  233. !circ->marked_for_close && circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  234. (!circ->timestamp_dirty ||
  235. circ->timestamp_dirty + options.NewCircuitPeriod < now)) {
  236. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  237. if(exitrouter && connection_ap_can_use_exit(conn, exitrouter))
  238. if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
  239. return 1;
  240. }
  241. }
  242. return 0;
  243. }
  244. /** Build a new test circuit every 5 minutes */
  245. #define TESTING_CIRCUIT_INTERVAL 300
  246. /** This function is called once a second. Its job is to make sure
  247. * all services we offer have enough circuits available. Some
  248. * services just want enough circuits for current tasks, whereas
  249. * others want a minimum set of idle circuits hanging around.
  250. */
  251. void circuit_build_needed_circs(time_t now) {
  252. static long time_to_new_circuit = 0;
  253. circuit_t *circ;
  254. /* launch a new circ for any pending streams that need one */
  255. connection_ap_attach_pending();
  256. /* make sure any hidden services have enough intro points */
  257. if(has_fetched_directory)
  258. rend_services_introduce();
  259. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  260. if(time_to_new_circuit < now) {
  261. circuit_reset_failure_count(1);
  262. time_to_new_circuit = now + options.NewCircuitPeriod;
  263. if(proxy_mode())
  264. client_dns_clean();
  265. circuit_expire_old_circuits();
  266. if(options.RunTesting && circ &&
  267. circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
  268. log_fn(LOG_INFO,"Creating a new testing circuit.");
  269. circuit_launch_by_identity(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  270. }
  271. }
  272. /** How many simultaneous in-progress general-purpose circuits do we
  273. * want to be building at once, if there are no open general-purpose
  274. * circuits?
  275. */
  276. #define CIRCUIT_MIN_BUILDING_GENERAL 5
  277. /* if there's no open circ, and less than 5 are on the way,
  278. * go ahead and try another. */
  279. if(!circ && circuit_count_building(CIRCUIT_PURPOSE_C_GENERAL)
  280. < CIRCUIT_MIN_BUILDING_GENERAL) {
  281. circuit_launch_by_identity(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  282. }
  283. /* XXX count idle rendezvous circs and build more */
  284. }
  285. /** If the stream <b>conn</b> is a member of any of the linked
  286. * lists of <b>circ</b>, then remove it from the list.
  287. */
  288. void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
  289. connection_t *prevconn;
  290. tor_assert(circ);
  291. tor_assert(conn);
  292. conn->cpath_layer = NULL; /* make sure we don't keep a stale pointer */
  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. control_event_circuit_status(circ, CIRC_EVENT_BUILT);
  432. switch(circ->purpose) {
  433. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  434. rend_client_rendcirc_has_opened(circ);
  435. break;
  436. case CIRCUIT_PURPOSE_C_INTRODUCING:
  437. rend_client_introcirc_has_opened(circ);
  438. break;
  439. case CIRCUIT_PURPOSE_C_GENERAL:
  440. /* Tell any AP connections that have been waiting for a new
  441. * circuit that one is ready. */
  442. connection_ap_attach_pending();
  443. break;
  444. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  445. /* at Bob, waiting for introductions */
  446. rend_service_intro_has_opened(circ);
  447. break;
  448. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  449. /* at Bob, connecting to rend point */
  450. rend_service_rendezvous_has_opened(circ);
  451. break;
  452. default:
  453. log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
  454. tor_assert(0);
  455. }
  456. }
  457. /*~ Called whenever a circuit could not be successfully built.
  458. */
  459. void circuit_build_failed(circuit_t *circ) {
  460. /* we should examine circ and see if it failed because of
  461. * the last hop or an earlier hop. then use this info below.
  462. */
  463. int failed_at_last_hop = 0;
  464. /* If the last hop isn't open, and the second-to-last is, we failed
  465. * at the last hop. */
  466. if (circ->cpath &&
  467. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  468. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  469. failed_at_last_hop = 1;
  470. }
  471. switch(circ->purpose) {
  472. case CIRCUIT_PURPOSE_C_GENERAL:
  473. if (circ->state != CIRCUIT_STATE_OPEN) {
  474. /* If we never built the circuit, note it as a failure. */
  475. /* Note that we can't just check circ->cpath here, because if
  476. * circuit-building failed immediately, it won't be set yet. */
  477. circuit_increment_failure_count();
  478. }
  479. break;
  480. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  481. /* at Bob, waiting for introductions */
  482. if (circ->state != CIRCUIT_STATE_OPEN) {
  483. circuit_increment_failure_count();
  484. }
  485. /* no need to care here, because bob will rebuild intro
  486. * points periodically. */
  487. break;
  488. case CIRCUIT_PURPOSE_C_INTRODUCING:
  489. /* at Alice, connecting to intro point */
  490. /* Don't increment failure count, since Bob may have picked
  491. * the introduction point maliciously */
  492. /* Alice will pick a new intro point when this one dies, if
  493. * the stream in question still cares. No need to act here. */
  494. break;
  495. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  496. /* at Alice, waiting for Bob */
  497. if (circ->state != CIRCUIT_STATE_OPEN) {
  498. circuit_increment_failure_count();
  499. }
  500. /* Alice will pick a new rend point when this one dies, if
  501. * the stream in question still cares. No need to act here. */
  502. break;
  503. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  504. /* at Bob, connecting to rend point */
  505. /* Don't increment failure count, since Alice may have picked
  506. * the rendezvous point maliciously */
  507. if (failed_at_last_hop) {
  508. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s. Sucks to be Alice.", circ->build_state->chosen_exit_name);
  509. } else {
  510. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s, because an earlier node failed.",
  511. circ->build_state->chosen_exit_name);
  512. rend_service_relaunch_rendezvous(circ);
  513. }
  514. break;
  515. default:
  516. /* Other cases are impossible, since this function is only called with
  517. * unbuilt circuits. */
  518. tor_assert(0);
  519. }
  520. }
  521. /** Number of consecutive failures so far; should only be touched by
  522. * circuit_launch_new and circuit_*_failure_count.
  523. */
  524. static int n_circuit_failures = 0;
  525. static int did_circs_fail_last_period = 0;
  526. /** Don't retry launching a new circuit if we try this many times with no
  527. * success. */
  528. #define MAX_CIRCUIT_FAILURES 5
  529. circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest)
  530. {
  531. if (!has_fetched_directory) {
  532. log_fn(LOG_DEBUG,"Haven't fetched directory yet; cancelling circuit launch.");
  533. return NULL;
  534. }
  535. if (did_circs_fail_last_period &&
  536. n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  537. /* too many failed circs in a row. don't try. */
  538. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  539. return NULL;
  540. }
  541. /* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
  542. return circuit_establish_circuit(purpose, exit_digest);
  543. }
  544. /** Launch a new circuit and return a pointer to it. Return NULL if you failed. */
  545. circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname)
  546. {
  547. const char *digest = NULL;
  548. if (exit_nickname) {
  549. routerinfo_t *r = router_get_by_nickname(exit_nickname);
  550. if (!r) {
  551. log_fn(LOG_WARN, "No such OR as '%s'", exit_nickname);
  552. return NULL;
  553. }
  554. digest = r->identity_digest;
  555. }
  556. return circuit_launch_by_identity(purpose, digest);
  557. }
  558. /** Record another failure at opening a general circuit. When we have
  559. * too many, we'll stop trying for the remainder of this minute.
  560. */
  561. static void circuit_increment_failure_count(void) {
  562. ++n_circuit_failures;
  563. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  564. }
  565. /** Reset the failure count for opening general circuits. This means
  566. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  567. * stopping again.
  568. */
  569. void circuit_reset_failure_count(int timeout) {
  570. if(timeout && n_circuit_failures > MAX_CIRCUIT_FAILURES)
  571. did_circs_fail_last_period = 1;
  572. else
  573. did_circs_fail_last_period = 0;
  574. n_circuit_failures = 0;
  575. }
  576. /** Find an open circ that we're happy with: return 1. If there isn't
  577. * one, and there isn't one on the way, launch one and return 0. If it
  578. * will never work, return -1.
  579. *
  580. * Write the found or in-progress or launched circ into *circp.
  581. */
  582. static int
  583. circuit_get_open_circ_or_launch(connection_t *conn,
  584. uint8_t desired_circuit_purpose,
  585. circuit_t **circp) {
  586. circuit_t *circ;
  587. uint32_t addr;
  588. int is_resolve;
  589. tor_assert(conn);
  590. tor_assert(circp);
  591. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  592. is_resolve = conn->socks_request->command == SOCKS_COMMAND_RESOLVE;
  593. circ = circuit_get_best(conn, 1, desired_circuit_purpose);
  594. if(circ) {
  595. *circp = circ;
  596. return 1; /* we're happy */
  597. }
  598. /* Do we need to check exit policy? */
  599. if(!is_resolve && !connection_edge_is_rendezvous_stream(conn)) {
  600. addr = client_dns_lookup_entry(conn->socks_request->address);
  601. if(router_exit_policy_all_routers_reject(addr, conn->socks_request->port)) {
  602. log_fn(LOG_WARN,"No Tor server exists that allows exit to %s:%d. Rejecting.",
  603. conn->socks_request->address, conn->socks_request->port);
  604. return -1;
  605. }
  606. }
  607. /* is one already on the way? */
  608. circ = circuit_get_best(conn, 0, desired_circuit_purpose);
  609. if(!circ) {
  610. char *exitname=NULL;
  611. uint8_t new_circ_purpose;
  612. if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  613. /* need to pick an intro point */
  614. exitname = rend_client_get_random_intro(conn->rend_query);
  615. if(!exitname) {
  616. log_fn(LOG_WARN,"Couldn't get an intro point for '%s'. Closing conn.",
  617. conn->rend_query);
  618. return -1;
  619. }
  620. if(!router_get_by_nickname(exitname)) {
  621. log_fn(LOG_WARN,"Advertised intro point '%s' is not known. Closing.", exitname);
  622. return -1;
  623. }
  624. /* XXX if we failed, then refetch the descriptor */
  625. log_fn(LOG_INFO,"Chose %s as intro point for %s.", exitname, conn->rend_query);
  626. }
  627. if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  628. new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  629. else if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  630. new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  631. else
  632. new_circ_purpose = desired_circuit_purpose;
  633. circ = circuit_launch_by_nickname(new_circ_purpose, exitname);
  634. tor_free(exitname);
  635. if(circ &&
  636. (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)) {
  637. /* then write the service_id into circ */
  638. strlcpy(circ->rend_query, conn->rend_query, sizeof(circ->rend_query));
  639. }
  640. }
  641. if(!circ)
  642. log_fn(LOG_INFO,"No safe circuit (purpose %d) ready for edge connection; delaying.",
  643. desired_circuit_purpose);
  644. *circp = circ;
  645. return 0;
  646. }
  647. /** Attach the AP stream <b>apconn</b> to circ's linked list of
  648. * p_streams. Also set apconn's cpath_layer to the last hop in
  649. * circ's cpath.
  650. */
  651. static void link_apconn_to_circ(connection_t *apconn, circuit_t *circ) {
  652. /* add it into the linked list of streams on this circuit */
  653. log_fn(LOG_DEBUG,"attaching new conn to circ. n_circ_id %d.", circ->n_circ_id);
  654. apconn->next_stream = circ->p_streams;
  655. /* assert_connection_ok(conn, time(NULL)); */
  656. circ->p_streams = apconn;
  657. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  658. tor_assert(circ->cpath);
  659. tor_assert(circ->cpath->prev);
  660. tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  661. apconn->cpath_layer = circ->cpath->prev;
  662. }
  663. /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
  664. * we don't find one: if conn cannot be handled by any known nodes,
  665. * warn and return -1 (conn needs to die);
  666. * else launch new circuit (if necessary) and return 0.
  667. * Otherwise, associate conn with a safe live circuit, do the
  668. * right next step, and return 1.
  669. */
  670. int connection_ap_handshake_attach_circuit(connection_t *conn) {
  671. int retval;
  672. int conn_age;
  673. tor_assert(conn);
  674. tor_assert(conn->type == CONN_TYPE_AP);
  675. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  676. tor_assert(conn->socks_request);
  677. conn_age = time(NULL) - conn->timestamp_created;
  678. if(conn_age > 60) {
  679. /* XXX make this cleaner than '60' */
  680. log_fn(LOG_WARN,"Giving up on unattached conn (%d sec old).", conn_age);
  681. return -1;
  682. }
  683. if(!connection_edge_is_rendezvous_stream(conn)) { /* we're a general conn */
  684. circuit_t *circ=NULL;
  685. /* find the circuit that we should use, if there is one. */
  686. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
  687. if(retval < 1)
  688. return retval;
  689. /* We have found a suitable circuit for our conn. Hurray. */
  690. tor_assert(circ);
  691. log_fn(LOG_DEBUG,"Attaching apconn to general circ %d (stream %d sec old).",
  692. circ->n_circ_id, conn_age);
  693. /* here, print the circ's path. so people can figure out which circs are sucking. */
  694. circuit_log_path(LOG_INFO,circ);
  695. if(!circ->timestamp_dirty)
  696. circ->timestamp_dirty = time(NULL);
  697. link_apconn_to_circ(conn, circ);
  698. tor_assert(conn->socks_request);
  699. if (conn->socks_request->command == SOCKS_COMMAND_CONNECT)
  700. connection_ap_handshake_send_begin(conn, circ);
  701. else
  702. connection_ap_handshake_send_resolve(conn, circ);
  703. return 1;
  704. } else { /* we're a rendezvous conn */
  705. circuit_t *rendcirc=NULL, *introcirc=NULL;
  706. tor_assert(!conn->cpath_layer);
  707. /* start by finding a rendezvous circuit for us */
  708. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
  709. if(retval < 0) return -1; /* failed */
  710. if(retval > 0) {
  711. tor_assert(rendcirc);
  712. /* one is already established, attach */
  713. log_fn(LOG_INFO,"rend joined circ %d already here. attaching. (stream %d sec old)",
  714. rendcirc->n_circ_id, conn_age);
  715. link_apconn_to_circ(conn, rendcirc);
  716. if(connection_ap_handshake_send_begin(conn, rendcirc) < 0)
  717. return 0; /* already marked, let them fade away */
  718. return 1;
  719. }
  720. if(rendcirc && rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  721. log_fn(LOG_INFO,"pending-join circ %d already here, with intro ack. Stalling. (stream %d sec old)", rendcirc->n_circ_id, conn_age);
  722. return 0;
  723. }
  724. /* it's on its way. find an intro circ. */
  725. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
  726. if(retval < 0) return -1; /* failed */
  727. if(retval > 0) {
  728. /* one has already sent the intro. keep waiting. */
  729. tor_assert(introcirc);
  730. log_fn(LOG_INFO,"Intro circ %d present and awaiting ack (rend %d). Stalling. (stream %d sec old)",
  731. introcirc->n_circ_id, rendcirc ? rendcirc->n_circ_id : 0, conn_age);
  732. return 0;
  733. }
  734. /* now rendcirc and introcirc are each either undefined or not finished */
  735. if(rendcirc && introcirc && rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  736. log_fn(LOG_INFO,"ready rend circ %d already here (no intro-ack yet on intro %d). (stream %d sec old)",
  737. rendcirc->n_circ_id, introcirc->n_circ_id, conn_age);
  738. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  739. if(introcirc->state == CIRCUIT_STATE_OPEN) {
  740. log_fn(LOG_INFO,"found open intro circ %d (rend %d); sending introduction. (stream %d sec old)",
  741. introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  742. /* XXX here we should cannibalize the rend circ if it's a zero service id */
  743. if(rend_client_send_introduction(introcirc, rendcirc) < 0) {
  744. return -1;
  745. }
  746. rendcirc->timestamp_dirty = time(NULL);
  747. introcirc->timestamp_dirty = time(NULL);
  748. assert_circuit_ok(rendcirc);
  749. assert_circuit_ok(introcirc);
  750. return 0;
  751. }
  752. }
  753. log_fn(LOG_INFO,"Intro (%d) and rend (%d) circs are not both ready. Stalling conn. (%d sec old)",
  754. introcirc ? introcirc->n_circ_id : 0,
  755. rendcirc ? rendcirc->n_circ_id : 0, conn_age);
  756. return 0;
  757. }
  758. }
  759. /*
  760. Local Variables:
  761. mode:c
  762. indent-tabs-mode:nil
  763. c-basic-offset:2
  764. End:
  765. */