circuitlist.c 15 KB

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