circuitlist.c 16 KB

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