circuitlist.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char circuitlist_c_id[] = "$Id$";
  7. /**
  8. * \file circuitlist.c
  9. * \brief Manage the global circuit list.
  10. **/
  11. #include "or.h"
  12. /********* START VARIABLES **********/
  13. /** A global list of all circuits at this hop. */
  14. circuit_t *global_circuitlist=NULL;
  15. /** Array of strings to make circ-\>state human-readable */
  16. const char *circuit_state_to_string[] = {
  17. "doing handshakes", /* 0 */
  18. "processing the onion", /* 1 */
  19. "connecting to firsthop", /* 2 */
  20. "open" /* 3 */
  21. };
  22. /********* END VARIABLES ************/
  23. static void circuit_free(circuit_t *circ);
  24. static void circuit_free_cpath(crypt_path_t *cpath);
  25. static void circuit_free_cpath_node(crypt_path_t *victim);
  26. /** Add <b>circ</b> to the global list of circuits. This is called only from
  27. * within circuit_new.
  28. */
  29. static void circuit_add(circuit_t *circ) {
  30. if (!global_circuitlist) { /* first one */
  31. global_circuitlist = circ;
  32. circ->next = NULL;
  33. } else {
  34. circ->next = global_circuitlist;
  35. global_circuitlist = circ;
  36. }
  37. }
  38. /** Detach from the global circuit list, and deallocate, all
  39. * circuits that have been marked for close.
  40. */
  41. void circuit_close_all_marked(void)
  42. {
  43. circuit_t *tmp,*m;
  44. while (global_circuitlist && global_circuitlist->marked_for_close) {
  45. tmp = global_circuitlist->next;
  46. circuit_free(global_circuitlist);
  47. global_circuitlist = tmp;
  48. }
  49. tmp = global_circuitlist;
  50. while (tmp && tmp->next) {
  51. if (tmp->next->marked_for_close) {
  52. m = tmp->next->next;
  53. circuit_free(tmp->next);
  54. tmp->next = m;
  55. /* Need to check new tmp->next; don't advance tmp. */
  56. } else {
  57. /* Advance tmp. */
  58. tmp = tmp->next;
  59. }
  60. }
  61. }
  62. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  63. * and <b>p_conn</b>. Add it to the global circuit list.
  64. */
  65. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
  66. circuit_t *circ;
  67. static uint32_t n_circuits_allocated = 1;
  68. /* never zero, since a global ID of 0 is treated specially by the controller */
  69. circ = tor_malloc_zero(sizeof(circuit_t));
  70. circ->magic = CIRCUIT_MAGIC;
  71. circ->timestamp_created = time(NULL);
  72. circ->p_circ_id = p_circ_id;
  73. circ->p_conn = p_conn;
  74. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  75. /* CircIDs */
  76. circ->p_circ_id = p_circ_id;
  77. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  78. circ->package_window = CIRCWINDOW_START;
  79. circ->deliver_window = CIRCWINDOW_START;
  80. circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
  81. circ->global_identifier = n_circuits_allocated++;
  82. circuit_add(circ);
  83. return circ;
  84. }
  85. /** Deallocate space associated with circ.
  86. */
  87. static void circuit_free(circuit_t *circ) {
  88. tor_assert(circ);
  89. tor_assert(circ->magic == CIRCUIT_MAGIC);
  90. if (circ->n_crypto)
  91. crypto_free_cipher_env(circ->n_crypto);
  92. if (circ->p_crypto)
  93. crypto_free_cipher_env(circ->p_crypto);
  94. if (circ->n_digest)
  95. crypto_free_digest_env(circ->n_digest);
  96. if (circ->p_digest)
  97. crypto_free_digest_env(circ->p_digest);
  98. if (circ->build_state) {
  99. tor_free(circ->build_state->chosen_exit_name);
  100. if (circ->build_state->pending_final_cpath)
  101. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  102. }
  103. tor_free(circ->build_state);
  104. circuit_free_cpath(circ->cpath);
  105. if (circ->rend_splice) {
  106. circ->rend_splice->rend_splice = NULL;
  107. }
  108. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  109. tor_free(circ);
  110. }
  111. /** Deallocate space associated with the linked list <b>cpath</b>. */
  112. static void circuit_free_cpath(crypt_path_t *cpath) {
  113. crypt_path_t *victim, *head=cpath;
  114. if (!cpath)
  115. return;
  116. /* it's a doubly linked list, so we have to notice when we've
  117. * gone through it once. */
  118. while (cpath->next && cpath->next != head) {
  119. victim = cpath;
  120. cpath = victim->next;
  121. circuit_free_cpath_node(victim);
  122. }
  123. circuit_free_cpath_node(cpath);
  124. }
  125. /** Release all storage held by circuits. */
  126. void
  127. circuit_free_all(void)
  128. {
  129. circuit_t *next;
  130. while (global_circuitlist) {
  131. next = global_circuitlist->next;
  132. while (global_circuitlist->resolving_streams) {
  133. connection_t *next;
  134. next = global_circuitlist->resolving_streams->next_stream;
  135. connection_free(global_circuitlist->resolving_streams);
  136. global_circuitlist->resolving_streams = next;
  137. }
  138. circuit_free(global_circuitlist);
  139. global_circuitlist = next;
  140. }
  141. }
  142. /** Deallocate space associated with the cpath node <b>victim</b>. */
  143. static void
  144. circuit_free_cpath_node(crypt_path_t *victim) {
  145. if (victim->f_crypto)
  146. crypto_free_cipher_env(victim->f_crypto);
  147. if (victim->b_crypto)
  148. crypto_free_cipher_env(victim->b_crypto);
  149. if (victim->f_digest)
  150. crypto_free_digest_env(victim->f_digest);
  151. if (victim->b_digest)
  152. crypto_free_digest_env(victim->b_digest);
  153. if (victim->handshake_state)
  154. crypto_dh_free(victim->handshake_state);
  155. victim->magic = 0xDEADBEEFu;
  156. tor_free(victim);
  157. }
  158. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  159. * such circuit exists. */
  160. circuit_t *
  161. circuit_get_by_global_id(uint32_t id)
  162. {
  163. circuit_t *circ;
  164. for (circ=global_circuitlist;circ;circ = circ->next) {
  165. if (circ->global_identifier == id) {
  166. if (circ->marked_for_close)
  167. return NULL;
  168. else
  169. return circ;
  170. }
  171. }
  172. return NULL;
  173. }
  174. /** Return a circ such that:
  175. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  176. * - circ is attached to <b>conn</b>, either as p_conn, n-conn, or
  177. * in p_streams or n_streams.
  178. * Return NULL if no such circuit exists.
  179. */
  180. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
  181. circuit_t *circ;
  182. connection_t *tmpconn;
  183. for (circ=global_circuitlist;circ;circ = circ->next) {
  184. if (circ->marked_for_close)
  185. continue;
  186. if (circ->p_circ_id == circ_id) {
  187. if (circ->p_conn == conn)
  188. return circ;
  189. for (tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  190. if (tmpconn == conn)
  191. return circ;
  192. }
  193. }
  194. if (circ->n_circ_id == circ_id) {
  195. if (circ->n_conn == conn)
  196. return circ;
  197. for (tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  198. if (tmpconn == conn)
  199. return circ;
  200. }
  201. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  202. if (tmpconn == conn)
  203. return circ;
  204. }
  205. }
  206. }
  207. return NULL;
  208. }
  209. /** Return a circ such that circ is attached to <b>conn</b>, either as
  210. * p_conn, n-conn, or in p_streams or n_streams or resolving_streams.
  211. *
  212. * Return NULL if no such circuit exists.
  213. */
  214. circuit_t *circuit_get_by_conn(connection_t *conn) {
  215. circuit_t *circ;
  216. connection_t *tmpconn;
  217. for (circ=global_circuitlist;circ;circ = circ->next) {
  218. if (circ->marked_for_close)
  219. continue;
  220. if (circ->p_conn == conn)
  221. return circ;
  222. if (circ->n_conn == conn)
  223. return circ;
  224. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  225. if (tmpconn == conn)
  226. return circ;
  227. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  228. if (tmpconn == conn)
  229. return circ;
  230. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  231. if (tmpconn == conn)
  232. return circ;
  233. }
  234. return NULL;
  235. }
  236. /** Return a circ such that:
  237. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  238. * - circ-\>purpose is equal to <b>purpose</b>.
  239. *
  240. * Return NULL if no such circuit exists.
  241. */
  242. circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose) {
  243. circuit_t *circ;
  244. for (circ = global_circuitlist; circ; circ = circ->next) {
  245. if (!circ->marked_for_close &&
  246. circ->purpose == purpose &&
  247. !rend_cmp_service_ids(rend_query, circ->rend_query))
  248. return circ;
  249. }
  250. return NULL;
  251. }
  252. /** Return the first circuit in global_circuitlist after <b>start</b> whose
  253. * rend_pk_digest field is <b>digest</b> and whose purpose is <b>purpose</b>. Returns
  254. * NULL if no circuit is found. If <b>start</b> is NULL, begin at the start of
  255. * the list.
  256. */
  257. circuit_t *
  258. circuit_get_next_by_pk_and_purpose(circuit_t *start,
  259. const char *digest, uint8_t purpose)
  260. {
  261. circuit_t *circ;
  262. if (start == NULL)
  263. circ = global_circuitlist;
  264. else
  265. circ = start->next;
  266. for ( ; circ; circ = circ->next) {
  267. if (circ->marked_for_close)
  268. continue;
  269. if (circ->purpose != purpose)
  270. continue;
  271. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  272. return circ;
  273. }
  274. return NULL;
  275. }
  276. /** Return the circuit waiting for a rendezvous with the provided cookie.
  277. * Return NULL if no such circuit is found.
  278. */
  279. circuit_t *circuit_get_rendezvous(const char *cookie)
  280. {
  281. circuit_t *circ;
  282. for (circ = global_circuitlist; circ; circ = circ->next) {
  283. if (! circ->marked_for_close &&
  284. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  285. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  286. return circ;
  287. }
  288. return NULL;
  289. }
  290. /** Return a circuit that is open, has specified <b>purpose</b>,
  291. * has a timestamp_dirty value of 0, and is uptime/capacity/internal
  292. * if required; or NULL if no circuit fits this description.
  293. *
  294. * Avoid returning need_uptime circuits if not necessary.
  295. * FFFF As a more important goal, not yet implemented, avoid returning
  296. * internal circuits if not necessary.
  297. */
  298. circuit_t *
  299. circuit_get_clean_open(uint8_t purpose, int need_uptime,
  300. int need_capacity, int internal) {
  301. circuit_t *circ;
  302. circuit_t *best=NULL;
  303. log_fn(LOG_DEBUG,"Hunting for a circ to cannibalize: purpose %d, uptime %d, capacity %d, internal %d", purpose, need_uptime, need_capacity, internal);
  304. for (circ=global_circuitlist; circ; circ = circ->next) {
  305. if (CIRCUIT_IS_ORIGIN(circ) &&
  306. circ->state == CIRCUIT_STATE_OPEN &&
  307. !circ->marked_for_close &&
  308. circ->purpose == purpose &&
  309. !circ->timestamp_dirty &&
  310. (!need_uptime || circ->build_state->need_uptime) &&
  311. (!need_capacity || circ->build_state->need_capacity) &&
  312. (!internal || circ->build_state->is_internal)) {
  313. if (!best || (best->build_state->need_uptime && !need_uptime))
  314. best = circ;
  315. }
  316. }
  317. return best;
  318. }
  319. /** Go through the circuitlist; mark-for-close each circuit that starts
  320. * at us but has not yet been used. */
  321. void circuit_mark_all_unused_circs(void) {
  322. circuit_t *circ;
  323. for (circ=global_circuitlist; circ; circ = circ->next) {
  324. if (CIRCUIT_IS_ORIGIN(circ) &&
  325. !circ->marked_for_close &&
  326. !circ->timestamp_dirty)
  327. circuit_mark_for_close(circ);
  328. }
  329. }
  330. /** Mark <b>circ</b> to be closed next time we call
  331. * circuit_close_all_marked(). Do any cleanup needed:
  332. * - If state is onionskin_pending, remove circ from the onion_pending
  333. * list.
  334. * - If circ isn't open yet: call circuit_build_failed() if we're
  335. * the origin, and in either case call circuit_rep_hist_note_result()
  336. * to note stats.
  337. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  338. * just tried from our list of intro points for that service
  339. * descriptor.
  340. * - Send appropriate destroys and edge_destroys for conns and
  341. * streams attached to circ.
  342. * - If circ->rend_splice is set (we are the midpoint of a joined
  343. * rendezvous stream), then mark the other circuit to close as well.
  344. */
  345. int _circuit_mark_for_close(circuit_t *circ) {
  346. connection_t *conn;
  347. assert_circuit_ok(circ);
  348. if (circ->marked_for_close)
  349. return -1;
  350. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  351. onion_pending_remove(circ);
  352. }
  353. /* If the circuit ever became OPEN, we sent it to the reputation history
  354. * module then. If it isn't OPEN, we send it there now to remember which
  355. * links worked and which didn't.
  356. */
  357. if (circ->state != CIRCUIT_STATE_OPEN) {
  358. if (CIRCUIT_IS_ORIGIN(circ)) {
  359. circuit_build_failed(circ); /* take actions if necessary */
  360. }
  361. circuit_rep_hist_note_result(circ);
  362. }
  363. if (CIRCUIT_IS_ORIGIN(circ)) {
  364. control_event_circuit_status(circ,
  365. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
  366. }
  367. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  368. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  369. /* treat this like getting a nack from it */
  370. log_fn(LOG_INFO,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
  371. circ->rend_query, circ->build_state->chosen_exit_name);
  372. rend_client_remove_intro_point(circ->build_state->chosen_exit_name, circ->rend_query);
  373. }
  374. if (circ->n_conn)
  375. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  376. for (conn=circ->n_streams; conn; conn=conn->next_stream)
  377. connection_edge_destroy(circ->n_circ_id, conn);
  378. while (circ->resolving_streams) {
  379. conn = circ->resolving_streams;
  380. circ->resolving_streams = conn->next_stream;
  381. if (!conn->marked_for_close) {
  382. /* The other side will see a DESTROY, and infer that the connections
  383. * are closing because the circuit is getting torn down. No need
  384. * to send an end cell*/
  385. conn->has_sent_end = 1; /* we're closing the circuit, nothing to send to */
  386. connection_mark_for_close(conn);
  387. }
  388. }
  389. if (circ->p_conn)
  390. connection_send_destroy(circ->p_circ_id, circ->p_conn);
  391. for (conn=circ->p_streams; conn; conn=conn->next_stream)
  392. connection_edge_destroy(circ->p_circ_id, conn);
  393. circ->marked_for_close = 1;
  394. if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
  395. /* do this after marking this circuit, to avoid infinite recursion. */
  396. circuit_mark_for_close(circ->rend_splice);
  397. circ->rend_splice = NULL;
  398. }
  399. return 0;
  400. }
  401. /** Verify that cpath layer <b>cp</b> has all of its invariants
  402. * correct. Trigger an assert if anything is invalid.
  403. */
  404. void assert_cpath_layer_ok(const crypt_path_t *cp)
  405. {
  406. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  407. // tor_assert(cp->port);
  408. tor_assert(cp);
  409. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  410. switch (cp->state)
  411. {
  412. case CPATH_STATE_OPEN:
  413. tor_assert(cp->f_crypto);
  414. tor_assert(cp->b_crypto);
  415. /* fall through */
  416. case CPATH_STATE_CLOSED:
  417. tor_assert(!cp->handshake_state);
  418. break;
  419. case CPATH_STATE_AWAITING_KEYS:
  420. tor_assert(cp->handshake_state);
  421. break;
  422. default:
  423. log_fn(LOG_ERR,"Unexpected state %d",cp->state);
  424. tor_assert(0);
  425. }
  426. tor_assert(cp->package_window >= 0);
  427. tor_assert(cp->deliver_window >= 0);
  428. }
  429. /** Verify that cpath <b>cp</b> has all of its invariants
  430. * correct. Trigger an assert if anything is invalid.
  431. */
  432. static void
  433. assert_cpath_ok(const crypt_path_t *cp)
  434. {
  435. const crypt_path_t *start = cp;
  436. do {
  437. assert_cpath_layer_ok(cp);
  438. /* layers must be in sequence of: "open* awaiting? closed*" */
  439. if (cp != start) {
  440. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  441. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  442. } else if (cp->state == CPATH_STATE_OPEN) {
  443. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  444. }
  445. }
  446. cp = cp->next;
  447. tor_assert(cp);
  448. } while (cp != start);
  449. }
  450. /** Verify that circuit <b>c</b> has all of its invariants
  451. * correct. Trigger an assert if anything is invalid.
  452. */
  453. void assert_circuit_ok(const circuit_t *c)
  454. {
  455. connection_t *conn;
  456. tor_assert(c);
  457. tor_assert(c->magic == CIRCUIT_MAGIC);
  458. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  459. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  460. if (c->n_conn) {
  461. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  462. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest, DIGEST_LEN));
  463. }
  464. if (c->p_conn)
  465. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  466. for (conn = c->p_streams; conn; conn = conn->next_stream)
  467. tor_assert(conn->type == CONN_TYPE_AP);
  468. for (conn = c->n_streams; conn; conn = conn->next_stream)
  469. tor_assert(conn->type == CONN_TYPE_EXIT);
  470. tor_assert(c->deliver_window >= 0);
  471. tor_assert(c->package_window >= 0);
  472. if (c->state == CIRCUIT_STATE_OPEN) {
  473. if (c->cpath) {
  474. tor_assert(CIRCUIT_IS_ORIGIN(c));
  475. tor_assert(!c->n_crypto);
  476. tor_assert(!c->p_crypto);
  477. tor_assert(!c->n_digest);
  478. tor_assert(!c->p_digest);
  479. } else {
  480. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  481. tor_assert(c->n_crypto);
  482. tor_assert(c->p_crypto);
  483. tor_assert(c->n_digest);
  484. tor_assert(c->p_digest);
  485. }
  486. }
  487. if (c->cpath) {
  488. assert_cpath_ok(c->cpath);
  489. }
  490. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  491. if (!c->marked_for_close) {
  492. tor_assert(c->rend_splice);
  493. tor_assert(c->rend_splice->rend_splice == c);
  494. }
  495. tor_assert(c->rend_splice != c);
  496. } else {
  497. tor_assert(!c->rend_splice);
  498. }
  499. }