circuituse.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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) == ADDR_POLICY_REJECTED) {
  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) != ADDR_POLICY_REJECTED)
  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. rend_services_introduce();
  260. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  261. if(time_to_new_circuit < now) {
  262. circuit_reset_failure_count();
  263. time_to_new_circuit = now + options.NewCircuitPeriod;
  264. if(proxy_mode())
  265. client_dns_clean();
  266. circuit_expire_old_circuits();
  267. if(options.RunTesting && circ &&
  268. circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
  269. log_fn(LOG_INFO,"Creating a new testing circuit.");
  270. circuit_launch_by_identity(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  271. }
  272. }
  273. /** How many simultaneous in-progress general-purpose circuits do we
  274. * want to be building at once, if there are no open general-purpose
  275. * circuits?
  276. */
  277. #define CIRCUIT_MIN_BUILDING_GENERAL 3
  278. /* if there's no open circ, and less than 3 are on the way,
  279. * go ahead and try another. */
  280. if(!circ && circuit_count_building(CIRCUIT_PURPOSE_C_GENERAL)
  281. < CIRCUIT_MIN_BUILDING_GENERAL) {
  282. circuit_launch_by_identity(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  283. }
  284. /* XXX count idle rendezvous circs and build more */
  285. }
  286. /** If the stream <b>conn</b> is a member of any of the linked
  287. * lists of <b>circ</b>, then remove it from the list.
  288. */
  289. void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
  290. connection_t *prevconn;
  291. tor_assert(circ && conn);
  292. if(conn == circ->p_streams) {
  293. circ->p_streams = conn->next_stream;
  294. return;
  295. }
  296. if(conn == circ->n_streams) {
  297. circ->n_streams = conn->next_stream;
  298. return;
  299. }
  300. if(conn == circ->resolving_streams) {
  301. circ->resolving_streams = conn->next_stream;
  302. return;
  303. }
  304. for(prevconn = circ->p_streams;
  305. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  306. prevconn = prevconn->next_stream)
  307. ;
  308. if(prevconn && prevconn->next_stream) {
  309. prevconn->next_stream = conn->next_stream;
  310. return;
  311. }
  312. for(prevconn = circ->n_streams;
  313. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  314. prevconn = prevconn->next_stream)
  315. ;
  316. if(prevconn && prevconn->next_stream) {
  317. prevconn->next_stream = conn->next_stream;
  318. return;
  319. }
  320. for(prevconn = circ->resolving_streams;
  321. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  322. prevconn = prevconn->next_stream)
  323. ;
  324. if(prevconn && prevconn->next_stream) {
  325. prevconn->next_stream = conn->next_stream;
  326. return;
  327. }
  328. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  329. tor_assert(0); /* should never get here */
  330. }
  331. /** Notify the global circuit list that <b>conn</b> is about to be
  332. * removed and then freed.
  333. *
  334. * If it's an OR conn, then mark-for-close all the circuits that use
  335. * that conn.
  336. *
  337. * If it's an edge conn, then detach it from its circ, so we don't
  338. * try to reference it later.
  339. */
  340. void circuit_about_to_close_connection(connection_t *conn) {
  341. /* currently, we assume it's too late to flush conn's buf here.
  342. * down the road, maybe we'll consider that eof doesn't mean can't-write
  343. */
  344. circuit_t *circ;
  345. switch(conn->type) {
  346. case CONN_TYPE_OR:
  347. if(conn->state != OR_CONN_STATE_OPEN) {
  348. /* Inform any pending (not attached) circs that they should give up. */
  349. circuit_n_conn_done(conn, 0);
  350. }
  351. /* Now close all the attached circuits on it. */
  352. while((circ = circuit_get_by_conn(conn))) {
  353. if(circ->n_conn == conn) /* it's closing in front of us */
  354. circ->n_conn = NULL;
  355. if(circ->p_conn == conn) /* it's closing behind us */
  356. circ->p_conn = NULL;
  357. circuit_mark_for_close(circ);
  358. }
  359. return;
  360. case CONN_TYPE_AP:
  361. case CONN_TYPE_EXIT:
  362. /* It's an edge conn. Need to remove it from the linked list of
  363. * conn's for this circuit. Confirm that 'end' relay command has
  364. * been sent. But don't kill the circuit.
  365. */
  366. circ = circuit_get_by_conn(conn);
  367. if(!circ)
  368. return;
  369. circuit_detach_stream(circ, conn);
  370. } /* end switch */
  371. }
  372. /** Don't keep more than 10 unused open circuits around. */
  373. #define MAX_UNUSED_OPEN_CIRCUITS 10
  374. /** Find each circuit that has been dirty for too long, and has
  375. * no streams on it: mark it for close.
  376. *
  377. * Also, if there are more than MAX_UNUSED_OPEN_CIRCUITS open and
  378. * unused circuits, then mark the excess circs for close.
  379. */
  380. static void
  381. circuit_expire_old_circuits(void)
  382. {
  383. circuit_t *circ;
  384. time_t now = time(NULL);
  385. smartlist_t *unused_open_circs;
  386. int i;
  387. unused_open_circs = smartlist_create();
  388. for (circ = global_circuitlist; circ; circ = circ->next) {
  389. if (circ->marked_for_close)
  390. continue;
  391. /* If the circuit has been dirty for too long, and there are no streams
  392. * on it, mark it for close.
  393. */
  394. if (circ->timestamp_dirty &&
  395. circ->timestamp_dirty + options.NewCircuitPeriod < now &&
  396. !circ->p_conn && /* we're the origin */
  397. !circ->p_streams /* nothing attached */ ) {
  398. log_fn(LOG_DEBUG,"Closing n_circ_id %d (dirty %d secs ago, purp %d)",circ->n_circ_id,
  399. (int)(now - circ->timestamp_dirty), circ->purpose);
  400. /* (only general and purpose_c circs can get dirty) */
  401. tor_assert(!circ->n_streams);
  402. tor_assert(circ->purpose <= CIRCUIT_PURPOSE_C_REND_JOINED);
  403. circuit_mark_for_close(circ);
  404. } else if (!circ->timestamp_dirty && CIRCUIT_IS_ORIGIN(circ) &&
  405. circ->state == CIRCUIT_STATE_OPEN &&
  406. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  407. /* Also, gather a list of open unused general circuits that we created.
  408. * Because we add elements to the front of global_circuitlist,
  409. * the last elements of unused_open_circs will be the oldest
  410. * ones.
  411. */
  412. smartlist_add(unused_open_circs, circ);
  413. }
  414. }
  415. for (i = MAX_UNUSED_OPEN_CIRCUITS; i < smartlist_len(unused_open_circs); ++i) {
  416. circuit_t *circ = smartlist_get(unused_open_circs, i);
  417. log_fn(LOG_DEBUG,"Expiring excess clean circ (n_circ_id %d, purp %d)",
  418. circ->n_circ_id, circ->purpose);
  419. circuit_mark_for_close(circ);
  420. }
  421. smartlist_free(unused_open_circs);
  422. }
  423. /** The circuit <b>circ</b> has just become open. Take the next
  424. * step: for rendezvous circuits, we pass circ to the appropriate
  425. * function in rendclient or rendservice. For general circuits, we
  426. * call connection_ap_attach_pending, which looks for pending streams
  427. * that could use circ.
  428. */
  429. void circuit_has_opened(circuit_t *circ) {
  430. switch(circ->purpose) {
  431. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  432. rend_client_rendcirc_has_opened(circ);
  433. break;
  434. case CIRCUIT_PURPOSE_C_INTRODUCING:
  435. rend_client_introcirc_has_opened(circ);
  436. break;
  437. case CIRCUIT_PURPOSE_C_GENERAL:
  438. /* Tell any AP connections that have been waiting for a new
  439. * circuit that one is ready. */
  440. connection_ap_attach_pending();
  441. break;
  442. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  443. /* at Bob, waiting for introductions */
  444. rend_service_intro_has_opened(circ);
  445. break;
  446. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  447. /* at Bob, connecting to rend point */
  448. rend_service_rendezvous_has_opened(circ);
  449. break;
  450. default:
  451. log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
  452. tor_assert(0);
  453. }
  454. }
  455. /*~ Called whenever a circuit could not be successfully built.
  456. */
  457. void circuit_build_failed(circuit_t *circ) {
  458. /* we should examine circ and see if it failed because of
  459. * the last hop or an earlier hop. then use this info below.
  460. */
  461. int failed_at_last_hop = 0;
  462. /* If the last hop isn't open, and the second-to-last is, we failed
  463. * at the last hop. */
  464. if (circ->cpath &&
  465. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  466. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  467. failed_at_last_hop = 1;
  468. }
  469. switch(circ->purpose) {
  470. case CIRCUIT_PURPOSE_C_GENERAL:
  471. if (circ->state != CIRCUIT_STATE_OPEN) {
  472. /* If we never built the circuit, note it as a failure. */
  473. /* Note that we can't just check circ->cpath here, because if
  474. * circuit-building failed immediately, it won't be set yet. */
  475. circuit_increment_failure_count();
  476. }
  477. break;
  478. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  479. /* at Bob, waiting for introductions */
  480. if (circ->state != CIRCUIT_STATE_OPEN) {
  481. circuit_increment_failure_count();
  482. }
  483. /* no need to care here, because bob will rebuild intro
  484. * points periodically. */
  485. break;
  486. case CIRCUIT_PURPOSE_C_INTRODUCING:
  487. /* at Alice, connecting to intro point */
  488. /* Don't increment failure count, since Bob may have picked
  489. * the introduction point maliciously */
  490. /* Alice will pick a new intro point when this one dies, if
  491. * the stream in question still cares. No need to act here. */
  492. break;
  493. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  494. /* at Alice, waiting for Bob */
  495. if (circ->state != CIRCUIT_STATE_OPEN) {
  496. circuit_increment_failure_count();
  497. }
  498. /* Alice will pick a new rend point when this one dies, if
  499. * the stream in question still cares. No need to act here. */
  500. break;
  501. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  502. /* at Bob, connecting to rend point */
  503. /* Don't increment failure count, since Alice may have picked
  504. * the rendezvous point maliciously */
  505. if (failed_at_last_hop) {
  506. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s. Sucks to be Alice.", circ->build_state->chosen_exit_name);
  507. } else {
  508. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s, because an earlier node failed.",
  509. circ->build_state->chosen_exit_name);
  510. rend_service_relaunch_rendezvous(circ);
  511. }
  512. break;
  513. default:
  514. /* Other cases are impossible, since this function is only called with
  515. * unbuilt circuits. */
  516. tor_assert(0);
  517. }
  518. }
  519. /** Number of consecutive failures so far; should only be touched by
  520. * circuit_launch_new and circuit_*_failure_count.
  521. */
  522. static int n_circuit_failures = 0;
  523. /** Don't retry launching a new circuit if we try this many times with no
  524. * success. */
  525. #define MAX_CIRCUIT_FAILURES 5
  526. circuit_t *circuit_launch_by_identity(uint8_t purpose, const char *exit_digest)
  527. {
  528. if (!has_fetched_directory) {
  529. log_fn(LOG_DEBUG,"Haven't fetched directory yet; cancelling circuit launch.");
  530. return NULL;
  531. }
  532. if (n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  533. /* too many failed circs in a row. don't try. */
  534. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  535. return NULL;
  536. }
  537. /* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
  538. return circuit_establish_circuit(purpose, exit_digest);
  539. }
  540. /** Launch a new circuit and return a pointer to it. Return NULL if you failed. */
  541. circuit_t *circuit_launch_by_nickname(uint8_t purpose, const char *exit_nickname)
  542. {
  543. const char *digest = NULL;
  544. if (exit_nickname) {
  545. routerinfo_t *r = router_get_by_nickname(exit_nickname);
  546. if (!r) {
  547. log_fn(LOG_WARN, "No such OR as '%s'", exit_nickname);
  548. return NULL;
  549. }
  550. digest = r->identity_digest;
  551. }
  552. return circuit_launch_by_identity(purpose, digest);
  553. }
  554. /** Record another failure at opening a general circuit. When we have
  555. * too many, we'll stop trying for the remainder of this minute.
  556. */
  557. static void circuit_increment_failure_count(void) {
  558. ++n_circuit_failures;
  559. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  560. }
  561. /** Reset the failure count for opening general circuits. This means
  562. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  563. * stopping again.
  564. */
  565. void circuit_reset_failure_count(void) {
  566. n_circuit_failures = 0;
  567. }
  568. /** Find an open circ that we're happy with: return 1. If there isn't
  569. * one, and there isn't one on the way, launch one and return 0. If it
  570. * will never work, return -1.
  571. *
  572. * Write the found or in-progress or launched circ into *circp.
  573. */
  574. static int
  575. circuit_get_open_circ_or_launch(connection_t *conn,
  576. uint8_t desired_circuit_purpose,
  577. circuit_t **circp) {
  578. circuit_t *circ;
  579. uint32_t addr;
  580. int is_resolve;
  581. tor_assert(conn);
  582. tor_assert(circp);
  583. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  584. is_resolve = conn->socks_request->command == SOCKS_COMMAND_RESOLVE;
  585. circ = circuit_get_best(conn, 1, desired_circuit_purpose);
  586. if(circ) {
  587. *circp = circ;
  588. return 1; /* we're happy */
  589. }
  590. /* Do we need to check exit policy? */
  591. if(!is_resolve && !connection_edge_is_rendezvous_stream(conn)) {
  592. addr = client_dns_lookup_entry(conn->socks_request->address);
  593. if(router_exit_policy_all_routers_reject(addr, conn->socks_request->port)) {
  594. log_fn(LOG_WARN,"No Tor server exists that allows exit to %s:%d. Rejecting.",
  595. conn->socks_request->address, conn->socks_request->port);
  596. return -1;
  597. }
  598. }
  599. /* is one already on the way? */
  600. circ = circuit_get_best(conn, 0, desired_circuit_purpose);
  601. if(!circ) {
  602. char *exitname=NULL;
  603. uint8_t new_circ_purpose;
  604. if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  605. /* need to pick an intro point */
  606. exitname = rend_client_get_random_intro(conn->rend_query);
  607. if(!exitname) {
  608. log_fn(LOG_WARN,"Couldn't get an intro point for '%s'. Closing conn.",
  609. conn->rend_query);
  610. return -1;
  611. }
  612. if(!router_get_by_nickname(exitname)) {
  613. log_fn(LOG_WARN,"Advertised intro point '%s' is not known. Closing.", exitname);
  614. return -1;
  615. }
  616. /* XXX if we failed, then refetch the descriptor */
  617. log_fn(LOG_INFO,"Chose %s as intro point for %s.", exitname, conn->rend_query);
  618. }
  619. if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_REND_JOINED)
  620. new_circ_purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  621. else if(desired_circuit_purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  622. new_circ_purpose = CIRCUIT_PURPOSE_C_INTRODUCING;
  623. else
  624. new_circ_purpose = desired_circuit_purpose;
  625. circ = circuit_launch_by_nickname(new_circ_purpose, exitname);
  626. tor_free(exitname);
  627. if(circ &&
  628. (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL)) {
  629. /* then write the service_id into circ */
  630. strcpy(circ->rend_query, conn->rend_query);
  631. }
  632. }
  633. if(!circ)
  634. log_fn(LOG_INFO,"No safe circuit (purpose %d) ready for edge connection; delaying.",
  635. desired_circuit_purpose);
  636. *circp = circ;
  637. return 0;
  638. }
  639. /** Attach the AP stream <b>apconn</b> to circ's linked list of
  640. * p_streams. Also set apconn's cpath_layer to the last hop in
  641. * circ's cpath.
  642. */
  643. static void link_apconn_to_circ(connection_t *apconn, circuit_t *circ) {
  644. /* add it into the linked list of streams on this circuit */
  645. log_fn(LOG_DEBUG,"attaching new conn to circ. n_circ_id %d.", circ->n_circ_id);
  646. apconn->next_stream = circ->p_streams;
  647. /* assert_connection_ok(conn, time(NULL)); */
  648. circ->p_streams = apconn;
  649. tor_assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath && circ->cpath->prev);
  650. tor_assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  651. apconn->cpath_layer = circ->cpath->prev;
  652. }
  653. /** Try to find a safe live circuit for CONN_TYPE_AP connection conn. If
  654. * we don't find one: if conn cannot be handled by any known nodes,
  655. * warn and return -1 (conn needs to die);
  656. * else launch new circuit (if necessary) and return 0.
  657. * Otherwise, associate conn with a safe live circuit, do the
  658. * right next step, and return 1.
  659. */
  660. int connection_ap_handshake_attach_circuit(connection_t *conn) {
  661. int retval;
  662. int conn_age;
  663. tor_assert(conn);
  664. tor_assert(conn->type == CONN_TYPE_AP);
  665. tor_assert(conn->state == AP_CONN_STATE_CIRCUIT_WAIT);
  666. tor_assert(conn->socks_request);
  667. conn_age = time(NULL) - conn->timestamp_created;
  668. if(conn_age > 60) {
  669. /* XXX make this cleaner than '60' */
  670. log_fn(LOG_WARN,"Giving up on unattached conn (%d sec old).", conn_age);
  671. return -1;
  672. }
  673. if(!connection_edge_is_rendezvous_stream(conn)) { /* we're a general conn */
  674. circuit_t *circ=NULL;
  675. /* find the circuit that we should use, if there is one. */
  676. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_GENERAL, &circ);
  677. if(retval < 1)
  678. return retval;
  679. /* We have found a suitable circuit for our conn. Hurray. */
  680. log_fn(LOG_DEBUG,"Attaching apconn to general circ %d (stream %d sec old).",
  681. circ->n_circ_id, conn_age);
  682. /* here, print the circ's path. so people can figure out which circs are sucking. */
  683. circuit_log_path(LOG_INFO,circ);
  684. if(!circ->timestamp_dirty)
  685. circ->timestamp_dirty = time(NULL);
  686. link_apconn_to_circ(conn, circ);
  687. tor_assert(conn->socks_request);
  688. if (conn->socks_request->command == SOCKS_COMMAND_CONNECT)
  689. connection_ap_handshake_send_begin(conn, circ);
  690. else
  691. connection_ap_handshake_send_resolve(conn, circ);
  692. return 1;
  693. } else { /* we're a rendezvous conn */
  694. circuit_t *rendcirc=NULL, *introcirc=NULL;
  695. tor_assert(!conn->cpath_layer);
  696. /* start by finding a rendezvous circuit for us */
  697. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_REND_JOINED, &rendcirc);
  698. if(retval < 0) return -1; /* failed */
  699. tor_assert(rendcirc);
  700. if(retval > 0) {
  701. /* one is already established, attach */
  702. log_fn(LOG_INFO,"rend joined circ %d already here. attaching. (stream %d sec old)",
  703. rendcirc->n_circ_id, conn_age);
  704. link_apconn_to_circ(conn, rendcirc);
  705. if(connection_ap_handshake_send_begin(conn, rendcirc) < 0)
  706. return 0; /* already marked, let them fade away */
  707. return 1;
  708. }
  709. if(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  710. log_fn(LOG_INFO,"pending-join circ %d already here, with intro ack. Stalling. (stream %d sec old)", rendcirc->n_circ_id, conn_age);
  711. return 0;
  712. }
  713. /* it's on its way. find an intro circ. */
  714. retval = circuit_get_open_circ_or_launch(conn, CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, &introcirc);
  715. if(retval < 0) return -1; /* failed */
  716. tor_assert(introcirc);
  717. if(retval > 0) {
  718. /* one has already sent the intro. keep waiting. */
  719. log_fn(LOG_INFO,"Intro circ %d present and awaiting ack (rend %d). Stalling. (stream %d sec old)",
  720. introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  721. return 0;
  722. }
  723. /* now both rendcirc and introcirc are defined, and neither is finished */
  724. if(rendcirc->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  725. log_fn(LOG_INFO,"ready rend circ %d already here (no intro-ack yet on intro %d). (stream %d sec old)",
  726. rendcirc->n_circ_id, introcirc->n_circ_id, conn_age);
  727. /* look around for any new intro circs that should introduce */
  728. tor_assert(introcirc->purpose == CIRCUIT_PURPOSE_C_INTRODUCING);
  729. if(introcirc->state == CIRCUIT_STATE_OPEN) {
  730. log_fn(LOG_INFO,"found open intro circ %d (rend %d); sending introduction. (stream %d sec old)",
  731. introcirc->n_circ_id, rendcirc->n_circ_id, conn_age);
  732. /* XXX here we should cannibalize the rend circ if it's a zero service id */
  733. if(rend_client_send_introduction(introcirc, rendcirc) < 0) {
  734. return -1;
  735. }
  736. rendcirc->timestamp_dirty = time(NULL);
  737. introcirc->timestamp_dirty = time(NULL);
  738. assert_circuit_ok(rendcirc);
  739. assert_circuit_ok(introcirc);
  740. return 0;
  741. }
  742. }
  743. 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);
  744. return 0;
  745. }
  746. }
  747. /*
  748. Local Variables:
  749. mode:c
  750. indent-tabs-mode:nil
  751. c-basic-offset:2
  752. End:
  753. */