123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- #include "or.h"
- circuit_t *global_circuitlist=NULL;
- const char *circuit_state_to_string[] = {
- "doing handshakes",
- "processing the onion",
- "connecting to firsthop",
- "open"
- };
- static void circuit_free(circuit_t *circ);
- static void circuit_free_cpath(crypt_path_t *cpath);
- static void circuit_free_cpath_node(crypt_path_t *victim);
- static void circuit_add(circuit_t *circ) {
- if(!global_circuitlist) {
- global_circuitlist = circ;
- circ->next = NULL;
- } else {
- circ->next = global_circuitlist;
- global_circuitlist = circ;
- }
- }
- void circuit_close_all_marked(void)
- {
- circuit_t *tmp,*m;
- while (global_circuitlist && global_circuitlist->marked_for_close) {
- tmp = global_circuitlist->next;
- circuit_free(global_circuitlist);
- global_circuitlist = tmp;
- }
- tmp = global_circuitlist;
- while (tmp && tmp->next) {
- if (tmp->next->marked_for_close) {
- m = tmp->next->next;
- circuit_free(tmp->next);
- tmp->next = m;
-
- } else {
-
- tmp = tmp->next;
- }
- }
- }
- circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
- circuit_t *circ;
- static uint32_t n_circuits_allocated = 0;
- circ = tor_malloc_zero(sizeof(circuit_t));
- circ->magic = CIRCUIT_MAGIC;
- circ->timestamp_created = time(NULL);
- circ->p_circ_id = p_circ_id;
- circ->p_conn = p_conn;
- circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
-
- circ->p_circ_id = p_circ_id;
-
- circ->package_window = CIRCWINDOW_START;
- circ->deliver_window = CIRCWINDOW_START;
- circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
- circ->global_identifier = n_circuits_allocated++;
- circuit_add(circ);
- return circ;
- }
- static void circuit_free(circuit_t *circ) {
- tor_assert(circ);
- tor_assert(circ->magic == CIRCUIT_MAGIC);
- if (circ->n_crypto)
- crypto_free_cipher_env(circ->n_crypto);
- if (circ->p_crypto)
- crypto_free_cipher_env(circ->p_crypto);
- if (circ->n_digest)
- crypto_free_digest_env(circ->n_digest);
- if (circ->p_digest)
- crypto_free_digest_env(circ->p_digest);
- if(circ->build_state) {
- tor_free(circ->build_state->chosen_exit_name);
- if (circ->build_state->pending_final_cpath)
- circuit_free_cpath_node(circ->build_state->pending_final_cpath);
- }
- tor_free(circ->build_state);
- circuit_free_cpath(circ->cpath);
- if (circ->rend_splice) {
- circ->rend_splice->rend_splice = NULL;
- }
- memset(circ, 0xAA, sizeof(circuit_t));
- tor_free(circ);
- }
- static void circuit_free_cpath(crypt_path_t *cpath) {
- crypt_path_t *victim, *head=cpath;
- if(!cpath)
- return;
-
- while(cpath->next && cpath->next != head) {
- victim = cpath;
- cpath = victim->next;
- circuit_free_cpath_node(victim);
- }
- circuit_free_cpath_node(cpath);
- }
- static void
- circuit_free_cpath_node(crypt_path_t *victim) {
- if(victim->f_crypto)
- crypto_free_cipher_env(victim->f_crypto);
- if(victim->b_crypto)
- crypto_free_cipher_env(victim->b_crypto);
- if(victim->f_digest)
- crypto_free_digest_env(victim->f_digest);
- if(victim->b_digest)
- crypto_free_digest_env(victim->b_digest);
- if(victim->handshake_state)
- crypto_dh_free(victim->handshake_state);
- tor_free(victim);
- }
- circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
- circuit_t *circ;
- connection_t *tmpconn;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if (circ->marked_for_close)
- continue;
- if(circ->p_circ_id == circ_id) {
- if(circ->p_conn == conn)
- return circ;
- for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
- if(tmpconn == conn)
- return circ;
- }
- }
- if(circ->n_circ_id == circ_id) {
- if(circ->n_conn == conn)
- return circ;
- for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
- if(tmpconn == conn)
- return circ;
- }
- for(tmpconn = circ->resolving_streams; tmpconn; tmpconn = tmpconn->next_stream) {
- if(tmpconn == conn)
- return circ;
- }
- }
- }
- return NULL;
- }
- circuit_t *circuit_get_by_conn(connection_t *conn) {
- circuit_t *circ;
- connection_t *tmpconn;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if (circ->marked_for_close)
- continue;
- if(circ->p_conn == conn)
- return circ;
- if(circ->n_conn == conn)
- return circ;
- for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
- if(tmpconn == conn)
- return circ;
- for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
- if(tmpconn == conn)
- return circ;
- for(tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
- if(tmpconn == conn)
- return circ;
- }
- return NULL;
- }
- circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose) {
- circuit_t *circ;
- for (circ = global_circuitlist; circ; circ = circ->next) {
- if (!circ->marked_for_close &&
- circ->purpose == purpose &&
- !rend_cmp_service_ids(rend_query, circ->rend_query))
- return circ;
- }
- return NULL;
- }
- circuit_t *
- circuit_get_next_by_pk_and_purpose(circuit_t *start,
- const char *digest, uint8_t purpose)
- {
- circuit_t *circ;
- if (start == NULL)
- circ = global_circuitlist;
- else
- circ = start->next;
- for( ; circ; circ = circ->next) {
- if (circ->marked_for_close)
- continue;
- if (circ->purpose != purpose)
- continue;
- if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
- return circ;
- }
- return NULL;
- }
- circuit_t *circuit_get_rendezvous(const char *cookie)
- {
- circuit_t *circ;
- for (circ = global_circuitlist; circ; circ = circ->next) {
- if (! circ->marked_for_close &&
- circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
- ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
- return circ;
- }
- return NULL;
- }
- int circuit_count_building(uint8_t purpose) {
- circuit_t *circ;
- int num=0;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(CIRCUIT_IS_ORIGIN(circ) &&
- circ->state != CIRCUIT_STATE_OPEN &&
- circ->purpose == purpose &&
- !circ->marked_for_close)
- num++;
- }
- return num;
- }
- circuit_t *
- circuit_get_youngest_clean_open(uint8_t purpose) {
- circuit_t *circ;
- circuit_t *youngest=NULL;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(CIRCUIT_IS_ORIGIN(circ) && circ->state == CIRCUIT_STATE_OPEN &&
- !circ->marked_for_close && circ->purpose == purpose &&
- !circ->timestamp_dirty &&
- (!youngest || youngest->timestamp_created < circ->timestamp_created))
- youngest = circ;
- }
- return youngest;
- }
- int _circuit_mark_for_close(circuit_t *circ) {
- connection_t *conn;
- assert_circuit_ok(circ);
- if (circ->marked_for_close)
- return -1;
- if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
- onion_pending_remove(circ);
- }
-
- if (circ->state != CIRCUIT_STATE_OPEN) {
- if(CIRCUIT_IS_ORIGIN(circ)) {
- circuit_build_failed(circ);
- }
- circuit_rep_hist_note_result(circ);
- }
- if (CIRCUIT_IS_ORIGIN(circ)) {
- control_event_circuit_status(circ,
- (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
- }
- if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
- tor_assert(circ->state == CIRCUIT_STATE_OPEN);
-
- log_fn(LOG_INFO,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
- circ->rend_query, circ->build_state->chosen_exit_name);
- rend_client_remove_intro_point(circ->build_state->chosen_exit_name, circ->rend_query);
- }
- if(circ->n_conn)
- connection_send_destroy(circ->n_circ_id, circ->n_conn);
- for(conn=circ->n_streams; conn; conn=conn->next_stream)
- connection_edge_destroy(circ->n_circ_id, conn);
- while(circ->resolving_streams) {
- conn = circ->resolving_streams;
- circ->resolving_streams = conn->next_stream;
- connection_dns_remove(conn);
- log_fn(LOG_INFO,"Freeing resolving-conn.");
- connection_free(conn);
- }
- if(circ->p_conn)
- connection_send_destroy(circ->p_circ_id, circ->p_conn);
- for(conn=circ->p_streams; conn; conn=conn->next_stream)
- connection_edge_destroy(circ->p_circ_id, conn);
- circ->marked_for_close = 1;
- if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
-
- circuit_mark_for_close(circ->rend_splice);
- circ->rend_splice = NULL;
- }
- return 0;
- }
- void assert_cpath_layer_ok(const crypt_path_t *cp)
- {
- switch(cp->state)
- {
- case CPATH_STATE_OPEN:
- tor_assert(cp->f_crypto);
- tor_assert(cp->b_crypto);
-
- case CPATH_STATE_CLOSED:
- tor_assert(!cp->handshake_state);
- break;
- case CPATH_STATE_AWAITING_KEYS:
- tor_assert(cp->handshake_state);
- break;
- default:
- log_fn(LOG_ERR,"Unexpected state %d",cp->state);
- tor_assert(0);
- }
- tor_assert(cp->package_window >= 0);
- tor_assert(cp->deliver_window >= 0);
- }
- static void
- assert_cpath_ok(const crypt_path_t *cp)
- {
- const crypt_path_t *start = cp;
- do {
- assert_cpath_layer_ok(cp);
-
- if (cp != start) {
- if (cp->state == CPATH_STATE_AWAITING_KEYS) {
- tor_assert(cp->prev->state == CPATH_STATE_OPEN);
- } else if (cp->state == CPATH_STATE_OPEN) {
- tor_assert(cp->prev->state == CPATH_STATE_OPEN);
- }
- }
- cp = cp->next;
- tor_assert(cp);
- } while (cp != start);
- }
- void assert_circuit_ok(const circuit_t *c)
- {
- connection_t *conn;
- tor_assert(c);
- tor_assert(c->magic == CIRCUIT_MAGIC);
- tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
- c->purpose <= _CIRCUIT_PURPOSE_MAX);
- if (c->n_conn) {
- tor_assert(c->n_conn->type == CONN_TYPE_OR);
- tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest, DIGEST_LEN));
- }
- if (c->p_conn)
- tor_assert(c->p_conn->type == CONN_TYPE_OR);
- for (conn = c->p_streams; conn; conn = conn->next_stream)
- tor_assert(conn->type == CONN_TYPE_AP);
- for (conn = c->n_streams; conn; conn = conn->next_stream)
- tor_assert(conn->type == CONN_TYPE_EXIT);
- tor_assert(c->deliver_window >= 0);
- tor_assert(c->package_window >= 0);
- if (c->state == CIRCUIT_STATE_OPEN) {
- if (c->cpath) {
- tor_assert(CIRCUIT_IS_ORIGIN(c));
- tor_assert(!c->n_crypto);
- tor_assert(!c->p_crypto);
- tor_assert(!c->n_digest);
- tor_assert(!c->p_digest);
- } else {
- tor_assert(!CIRCUIT_IS_ORIGIN(c));
- tor_assert(c->n_crypto);
- tor_assert(c->p_crypto);
- tor_assert(c->n_digest);
- tor_assert(c->p_digest);
- }
- }
- if (c->cpath) {
- assert_cpath_ok(c->cpath);
- }
- if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
- if (!c->marked_for_close) {
- tor_assert(c->rend_splice);
- tor_assert(c->rend_splice->rend_splice == c);
- }
- tor_assert(c->rend_splice != c);
- } else {
- tor_assert(!c->rend_splice);
- }
- }
|