123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128 |
- #include "or.h"
- extern or_options_t options;
- static void circuit_free_cpath_node(crypt_path_t *victim);
- static circ_id_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
- unsigned long stats_n_relay_cells_relayed = 0;
- unsigned long stats_n_relay_cells_delivered = 0;
- static circuit_t *global_circuitlist=NULL;
- char *circuit_state_to_string[] = {
- "doing handshakes",
- "processing the onion",
- "connecting to firsthop",
- "open"
- };
- 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_remove(circuit_t *circ) {
- circuit_t *tmpcirc;
- assert(circ && global_circuitlist);
- if(global_circuitlist == circ) {
- global_circuitlist = global_circuitlist->next;
- return;
- }
- for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
- if(tmpcirc->next == circ) {
- tmpcirc->next = circ->next;
- return;
- }
- }
- }
- circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn) {
- circuit_t *circ;
- circ = tor_malloc_zero(sizeof(circuit_t));
- 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;
- circuit_add(circ);
- return circ;
- }
- void circuit_free(circuit_t *circ) {
- assert(circ);
- if (circ->n_crypto)
- crypto_free_cipher_env(circ->n_crypto);
- if (circ->p_crypto)
- crypto_free_cipher_env(circ->p_crypto);
- if(circ->build_state)
- tor_free(circ->build_state->chosen_exit);
- tor_free(circ->build_state);
- circuit_free_cpath(circ->cpath);
- free(circ);
- }
- 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->handshake_state)
- crypto_dh_free(victim->handshake_state);
- free(victim);
- }
- static circ_id_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
- circ_id_t test_circ_id;
- uint16_t high_bit;
- assert(conn && conn->type == CONN_TYPE_OR);
- high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
- do {
-
-
- test_circ_id = conn->next_circ_id++;
- if (test_circ_id == 0 || test_circ_id >= 1<<15) {
- test_circ_id = 1;
- conn->next_circ_id = 2;
- }
- test_circ_id |= high_bit;
- } while(circuit_get_by_circ_id_conn(test_circ_id, conn));
- return test_circ_id;
- }
- circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn) {
- circuit_t *circ;
- connection_t *tmpconn;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- 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;
- }
- }
- }
- 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->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;
- }
- return NULL;
- }
- circuit_t *circuit_get_newest(connection_t *conn, int must_be_open) {
- circuit_t *circ, *newest=NULL, *leastdirty=NULL;
- routerinfo_t *exitrouter;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(!circ->cpath)
- continue;
- if(must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
- continue;
- if(conn) {
- if(circ->state == CIRCUIT_STATE_OPEN && circ->n_conn)
- exitrouter = router_get_by_addr_port(circ->cpath->prev->addr, circ->cpath->prev->port);
- else
- exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
- if(!exitrouter || connection_ap_can_use_exit(conn, exitrouter) < 0) {
-
- continue;
- }
- }
- if(!newest || newest->timestamp_created < circ->timestamp_created) {
- newest = circ;
- }
- if(conn && circ->timestamp_dirty &&
- (!leastdirty || leastdirty->timestamp_dirty < circ->timestamp_dirty)) {
- leastdirty = circ;
- }
- }
- if(leastdirty &&
- leastdirty->timestamp_dirty+options.NewCircuitPeriod > time(NULL)) {
- return leastdirty;
- }
- if(newest) {
- return newest;
- }
- return NULL;
- }
- #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 3
- void circuit_expire_building(void) {
- int now = time(NULL);
- circuit_t *victim, *circ = global_circuitlist;
- while(circ) {
- victim = circ;
- circ = circ->next;
- if(victim->cpath &&
- victim->state != CIRCUIT_STATE_OPEN &&
- victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC+1 < now) {
- if(victim->n_conn)
- log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s)",
- victim->n_conn->address, victim->n_port, victim->n_circ_id,
- victim->state, circuit_state_to_string[victim->state]);
- else
- log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s)", victim->n_circ_id,
- victim->state, circuit_state_to_string[victim->state]);
- circuit_close(victim);
- }
- }
- }
- int circuit_count_building(void) {
- circuit_t *circ;
- int num=0;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN)
- num++;
- }
- return num;
- }
- #define MIN_CIRCUITS_HANDLING_STREAM 2
- int circuit_stream_is_being_handled(connection_t *conn) {
- circuit_t *circ;
- routerinfo_t *exitrouter;
- int num=0;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN) {
- exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
- if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) >= 0)
- if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
- return 1;
- }
- }
- return 0;
- }
- int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
- int cell_direction, crypt_path_t *layer_hint) {
- connection_t *conn=NULL;
- char recognized=0;
- char buf[1+CELL_PAYLOAD_SIZE];
- assert(cell && circ);
- assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
- buf[0] = cell->length;
- memcpy(buf+1, cell->payload, CELL_PAYLOAD_SIZE);
- log_fn(LOG_DEBUG,"direction %d, streamid %d before crypt.", cell_direction, *(int*)(cell->payload+1));
- if(relay_crypt(circ, buf, 1+CELL_PAYLOAD_SIZE, cell_direction, &layer_hint, &recognized, &conn) < 0) {
- log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
- return -1;
- }
- cell->length = buf[0];
- memcpy(cell->payload, buf+1, CELL_PAYLOAD_SIZE);
- if(recognized) {
- if(cell_direction == CELL_DIRECTION_OUT) {
- ++stats_n_relay_cells_delivered;
- log_fn(LOG_DEBUG,"Sending to exit.");
- if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL) < 0) {
- log_fn(LOG_WARN,"connection_edge_process_relay_cell (at exit) failed.");
- return -1;
- }
- }
- if(cell_direction == CELL_DIRECTION_IN) {
- ++stats_n_relay_cells_delivered;
- log_fn(LOG_DEBUG,"Sending to AP.");
- if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint) < 0) {
- log_fn(LOG_WARN,"connection_edge_process_relay_cell (at AP) failed.");
- return -1;
- }
- }
- return 0;
- }
-
- if(cell_direction == CELL_DIRECTION_OUT)
- conn = circ->n_conn;
- else
- conn = circ->p_conn;
- if(!conn) {
- log_fn(LOG_INFO,"Didn't recognize cell (%d), but circ stops here! Dropping.",
- *(int *)(cell->payload+1));
- return 0;
- }
- log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
- ++stats_n_relay_cells_relayed;
- connection_or_write_cell_to_buf(cell, conn);
- return 0;
- }
- int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
- crypt_path_t **layer_hint, char *recognized, connection_t **conn) {
- crypt_path_t *thishop;
- char out[256];
- assert(circ && in && recognized && conn);
- assert(inlen < 256);
- if(cell_direction == CELL_DIRECTION_IN) {
- if(circ->cpath) {
- thishop = circ->cpath;
- if(thishop->state != CPATH_STATE_OPEN) {
- log_fn(LOG_WARN,"Relay cell before first created cell?");
- return -1;
- }
- do {
- assert(thishop);
- log_fn(LOG_DEBUG,"before decrypt: %d",*(int*)(in+2));
-
- if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
- log_fn(LOG_WARN,"Error performing onion decryption: %s", crypto_perror());
- return -1;
- }
- memcpy(in,out,inlen);
- log_fn(LOG_DEBUG,"after decrypt: %d",*(int*)(in+2));
- if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn))) {
- *layer_hint = thishop;
- return 0;
- }
- thishop = thishop->next;
- } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
- log_fn(LOG_INFO,"in-cell at OP not recognized. Dropping.");
- return 0;
- } else {
- log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
- if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
- log_fn(LOG_WARN,"Onion encryption failed for circID %u: %s",
- circ->p_circ_id, crypto_perror());
- return -1;
- }
- memcpy(in,out,inlen);
- log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
- log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
-
- }
- } else if(cell_direction == CELL_DIRECTION_OUT) {
- if(circ->cpath) {
- thishop = *layer_hint;
-
- do {
- assert(thishop);
- log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
- if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
- log_fn(LOG_WARN,"Error performing encryption: %s", crypto_perror());
- return -1;
- }
- memcpy(in,out,inlen);
- log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
- thishop = thishop->prev;
- } while(thishop != circ->cpath->prev);
- } else {
- if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
- log_fn(LOG_WARN,"Decryption failed for circID %u: %s",
- circ->n_circ_id, crypto_perror());
- return -1;
- }
- memcpy(in,out,inlen);
- if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
- return 0;
- }
- } else {
- log_fn(LOG_ERR,"unknown cell direction %d.", cell_direction);
- assert(0);
- }
- return 0;
- }
- int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
- connection_t *tmpconn;
- if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE)) {
- log_fn(LOG_DEBUG,"It's the zero stream. Recognized.");
- return 1;
- }
- log_fn(LOG_DEBUG,"not the zero stream.");
- if(cell_direction == CELL_DIRECTION_OUT)
- tmpconn = circ->n_streams;
- else
- tmpconn = circ->p_streams;
- if(!tmpconn) {
- log_fn(LOG_DEBUG,"No conns. Not recognized.");
- return 0;
- }
- for( ; tmpconn; tmpconn=tmpconn->next_stream) {
- if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
- log_fn(LOG_DEBUG,"recognized stream %d.", *(int*)stream);
- *conn = tmpconn;
- return 1;
- }
- log_fn(LOG_DEBUG,"considered stream %d, not it.",*(int*)tmpconn->stream_id);
- }
- log_fn(LOG_DEBUG,"Didn't recognize on this iteration of decryption.");
- return 0;
- }
- void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
- connection_t *conn;
- assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
- log_fn(LOG_DEBUG,"resuming");
- if(edge_type == EDGE_EXIT)
- conn = circ->n_streams;
- else
- conn = circ->p_streams;
- for( ; conn; conn=conn->next_stream) {
- if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
- (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
- connection_start_reading(conn);
- connection_edge_package_raw_inbuf(conn);
-
- if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
- return;
- }
- }
- }
- int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
- connection_t *conn = NULL;
- assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
- assert(edge_type == EDGE_EXIT || layer_hint);
- log_fn(LOG_DEBUG,"considering");
- if(edge_type == EDGE_EXIT && circ->package_window <= 0)
- conn = circ->n_streams;
- else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
- conn = circ->p_streams;
- else
- return 0;
- for( ; conn; conn=conn->next_stream)
- if(!layer_hint || conn->cpath_layer == layer_hint)
- connection_stop_reading(conn);
- log_fn(LOG_DEBUG,"yes. stopped.");
- return 1;
- }
- int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
- cell_t cell;
- assert(circ);
- memset(&cell, 0, sizeof(cell_t));
- cell.command = CELL_RELAY;
- SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
- SET_CELL_STREAM_ID(cell, ZERO_STREAM);
- cell.length = RELAY_HEADER_SIZE;
- if(edge_type == EDGE_AP) {
- cell.circ_id = circ->n_circ_id;
- while(layer_hint->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
- log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme forward.", layer_hint->deliver_window);
- layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
- if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, layer_hint) < 0) {
- log_fn(LOG_WARN,"At AP: circuit_deliver_relay_cell failed.");
- return -1;
- }
- }
- } else if(edge_type == EDGE_EXIT) {
- cell.circ_id = circ->p_circ_id;
- while(circ->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
- log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme back.", circ->deliver_window);
- circ->deliver_window += CIRCWINDOW_INCREMENT;
- if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, layer_hint) < 0) {
- log_fn(LOG_WARN,"At exit: circuit_deliver_relay_cell failed.");
- return -1;
- }
- }
- }
- return 0;
- }
- void circuit_close(circuit_t *circ) {
- connection_t *conn;
- assert(circ);
- circuit_remove(circ);
- if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
- onion_pending_remove(circ);
- }
- 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_send_destroy(circ->n_circ_id, conn);
- }
- if(circ->p_conn)
- connection_send_destroy(circ->n_circ_id, circ->p_conn);
- for(conn=circ->p_streams; conn; conn=conn->next_stream) {
- connection_send_destroy(circ->p_circ_id, conn);
- }
- if (circ->state == CIRCUIT_STATE_BUILDING ||
- circ->state == CIRCUIT_STATE_OR_WAIT) {
-
- circuit_increment_failure_count();
- }
- circuit_free(circ);
- }
- void circuit_about_to_close_connection(connection_t *conn) {
-
-
- circuit_t *circ;
- connection_t *prevconn;
- switch(conn->type) {
- case CONN_TYPE_OR:
-
- while((circ = circuit_get_by_conn(conn))) {
- if(circ->n_conn == conn)
- circ->n_conn = NULL;
- if(circ->p_conn == conn)
- circ->p_conn = NULL;
- circuit_close(circ);
- }
- return;
- case CONN_TYPE_AP:
- case CONN_TYPE_EXIT:
-
- circ = circuit_get_by_conn(conn);
- if(!circ)
- return;
- if(!conn->has_sent_end) {
- log_fn(LOG_INFO,"Edge connection hasn't sent end yet? Bug.");
- if(connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer) < 0)
- log_fn(LOG_WARN,"1: I called connection_edge_end redundantly.");
- }
- if(conn == circ->p_streams) {
- circ->p_streams = conn->next_stream;
- return;
- }
- if(conn == circ->n_streams) {
- circ->n_streams = conn->next_stream;
- return;
- }
- for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
- if(prevconn && prevconn->next_stream) {
- prevconn->next_stream = conn->next_stream;
- return;
- }
- for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
- if(prevconn && prevconn->next_stream) {
- prevconn->next_stream = conn->next_stream;
- return;
- }
- log_fn(LOG_ERR,"edge conn not in circuit's list?");
- assert(0);
- }
- }
- void circuit_dump_details(int severity, circuit_t *circ, int poll_index,
- char *type, int this_circid, int other_circid) {
- struct crypt_path_t *hop;
- log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
- poll_index, type, this_circid, other_circid, circ->state,
- circuit_state_to_string[circ->state], (int)circ->timestamp_created);
- if(circ->cpath) {
- if(circ->state == CIRCUIT_STATE_BUILDING)
- log(severity,"Building: desired len %d, planned exit node %s.",
- circ->build_state->desired_path_len, circ->build_state->chosen_exit);
- for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
- log(severity,"hop: state %d, addr %d, port %d", hop->state, hop->addr, hop->port);
- }
- }
- void circuit_dump_by_conn(connection_t *conn, int severity) {
- circuit_t *circ;
- connection_t *tmpconn;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(circ->p_conn == conn)
- circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
- circ->p_circ_id, circ->n_circ_id);
- for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
- if(tmpconn == conn) {
- circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
- circ->p_circ_id, circ->n_circ_id);
- }
- }
- if(circ->n_conn == conn)
- circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
- circ->n_circ_id, circ->p_circ_id);
- for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
- if(tmpconn == conn) {
- circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
- circ->n_circ_id, circ->p_circ_id);
- }
- }
- }
- }
- void circuit_expire_unused_circuits(void) {
- circuit_t *circ, *tmpcirc;
- time_t now = time(NULL);
- circ = global_circuitlist;
- while(circ) {
- tmpcirc = circ;
- circ = circ->next;
- if(tmpcirc->timestamp_dirty &&
- tmpcirc->timestamp_dirty + options.NewCircuitPeriod < now &&
- !tmpcirc->p_conn && !tmpcirc->p_streams) {
- log_fn(LOG_DEBUG,"Closing n_circ_id %d",tmpcirc->n_circ_id);
- circuit_close(tmpcirc);
- }
- }
- }
- static int n_circuit_failures = 0;
- int circuit_launch_new(void) {
- if(!options.SocksPort)
- return -1;
- if(n_circuit_failures > 5) {
- return -1;
- }
-
- circuit_establish_circuit();
- return 0;
- }
- void circuit_increment_failure_count(void) {
- ++n_circuit_failures;
- log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
- }
- void circuit_reset_failure_count(void) {
- n_circuit_failures = 0;
- }
- int circuit_establish_circuit(void) {
- routerinfo_t *firsthop;
- connection_t *n_conn;
- circuit_t *circ;
- circ = circuit_new(0, NULL);
- circ->state = CIRCUIT_STATE_OR_WAIT;
- circ->build_state = onion_new_cpath_build_state();
- if (! circ->build_state) {
- log_fn(LOG_INFO,"Generating cpath length failed.");
- circuit_close(circ);
- return -1;
- }
- onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
- if(!circ->cpath) {
- log_fn(LOG_INFO,"Generating first cpath hop failed.");
- circuit_close(circ);
- return -1;
- }
-
- log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
- firsthop->address,firsthop->or_port);
- n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
- if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) {
- circ->n_addr = firsthop->addr;
- circ->n_port = firsthop->or_port;
- if(options.ORPort) {
- log_fn(LOG_INFO,"Route's firsthop isn't connected.");
- circuit_close(circ);
- return -1;
- }
- if(!n_conn) {
- n_conn = connection_or_connect(firsthop);
- if(!n_conn) {
- log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
- circuit_close(circ);
- return -1;
- }
- }
- log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
- return 0;
-
- } else {
- circ->n_addr = n_conn->addr;
- circ->n_port = n_conn->port;
- circ->n_conn = n_conn;
- log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
- if(circuit_send_next_onion_skin(circ) < 0) {
- log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
- circuit_close(circ);
- return -1;
- }
- }
- return 0;
- }
- void circuit_n_conn_open(connection_t *or_conn) {
- circuit_t *circ;
- for(circ=global_circuitlist;circ;circ = circ->next) {
- if(circ->cpath && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
- assert(circ->state == CIRCUIT_STATE_OR_WAIT);
- log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
- circ->n_conn = or_conn;
- if(circuit_send_next_onion_skin(circ) < 0) {
- log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
- circuit_close(circ);
- continue;
-
- }
- }
- }
- }
- int circuit_send_next_onion_skin(circuit_t *circ) {
- cell_t cell;
- crypt_path_t *hop;
- routerinfo_t *router;
- int r;
- int circ_id_type;
- assert(circ && circ->cpath);
- if(circ->cpath->state == CPATH_STATE_CLOSED) {
- assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
- log_fn(LOG_DEBUG,"First skin; sending create cell.");
- circ_id_type = decide_circ_id_type(options.Nickname,
- circ->n_conn->nickname);
- circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
- memset(&cell, 0, sizeof(cell_t));
- cell.command = CELL_CREATE;
- cell.circ_id = circ->n_circ_id;
- cell.length = DH_ONIONSKIN_LEN;
- if(onion_skin_create(circ->n_conn->onion_pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
- log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
- return -1;
- }
- connection_or_write_cell_to_buf(&cell, circ->n_conn);
- circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
- circ->state = CIRCUIT_STATE_BUILDING;
- log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
- } else {
- assert(circ->cpath->state == CPATH_STATE_OPEN);
- assert(circ->state == CIRCUIT_STATE_BUILDING);
- log_fn(LOG_DEBUG,"starting to send subsequent skin.");
- r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
- if (r==1) {
-
- circ->state = CIRCUIT_STATE_OPEN;
- log_fn(LOG_INFO,"circuit built!");
- circuit_reset_failure_count();
-
- connection_ap_attach_pending();
- return 0;
- } else if (r<0) {
- log_fn(LOG_INFO,"Unable to extend circuit path.");
- return -1;
- }
- hop = circ->cpath->prev;
- memset(&cell, 0, sizeof(cell_t));
- cell.command = CELL_RELAY;
- cell.circ_id = circ->n_circ_id;
- SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_EXTEND);
- SET_CELL_STREAM_ID(cell, ZERO_STREAM);
- cell.length = RELAY_HEADER_SIZE + 6 + DH_ONIONSKIN_LEN;
- *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE) = htonl(hop->addr);
- *(uint16_t*)(cell.payload+RELAY_HEADER_SIZE+4) = htons(hop->port);
- if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), cell.payload+RELAY_HEADER_SIZE+6) < 0) {
- log_fn(LOG_WARN,"onion_skin_create failed.");
- return -1;
- }
- log_fn(LOG_DEBUG,"Sending extend relay cell.");
-
- if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, hop->prev) < 0) {
- log_fn(LOG_WARN,"failed to deliver extend cell. Closing.");
- return -1;
- }
- hop->state = CPATH_STATE_AWAITING_KEYS;
- }
- return 0;
- }
- int circuit_extend(cell_t *cell, circuit_t *circ) {
- connection_t *n_conn;
- int circ_id_type;
- cell_t newcell;
- if(circ->n_conn) {
- log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
- return -1;
- }
- circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
- circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
- n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
- if(!n_conn || n_conn->type != CONN_TYPE_OR) {
-
-
- struct in_addr in;
- in.s_addr = htonl(circ->n_addr);
- log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
- connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
- NULL, 0, NULL);
- return 0;
- }
- circ->n_addr = n_conn->addr;
- circ->n_port = n_conn->port;
- circ->n_conn = n_conn;
- log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
- circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
- log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
- circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
- if(!circ->n_circ_id) {
- log_fn(LOG_WARN,"failed to get unique circID.");
- return -1;
- }
- log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
- memset(&newcell, 0, sizeof(cell_t));
- newcell.command = CELL_CREATE;
- newcell.circ_id = circ->n_circ_id;
- newcell.length = DH_ONIONSKIN_LEN;
- memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
- connection_or_write_cell_to_buf(&newcell, circ->n_conn);
- return 0;
- }
- int circuit_finish_handshake(circuit_t *circ, char *reply) {
- unsigned char iv[16];
- unsigned char keys[32];
- crypt_path_t *hop;
- memset(iv, 0, 16);
- assert(circ->cpath);
- if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
- hop = circ->cpath;
- else {
- for(hop=circ->cpath->next;
- hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
- hop=hop->next) ;
- if(hop == circ->cpath) {
- log_fn(LOG_WARN,"got extended when circ already built? Closing.");
- return -1;
- }
- }
- assert(hop->state == CPATH_STATE_AWAITING_KEYS);
- if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 32) < 0) {
- log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
- return -1;
- }
- crypto_dh_free(hop->handshake_state);
- hop->handshake_state = NULL;
- log_fn(LOG_DEBUG,"hop %d init cipher forward %d, backward %d.", (uint32_t)hop, *(uint32_t*)keys, *(uint32_t*)(keys+16));
- if (!(hop->f_crypto =
- crypto_create_init_cipher(CIRCUIT_CIPHER,keys,iv,1))) {
- log(LOG_WARN,"forward cipher initialization failed.");
- return -1;
- }
- if (!(hop->b_crypto =
- crypto_create_init_cipher(CIRCUIT_CIPHER,keys+16,iv,0))) {
- log(LOG_WARN,"backward cipher initialization failed.");
- return -1;
- }
- hop->state = CPATH_STATE_OPEN;
- log_fn(LOG_INFO,"finished");
- return 0;
- }
- int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
- crypt_path_t *victim;
- connection_t *stream;
- assert(circ);
- assert(layer);
-
- circuit_close(circ);
- return 0;
- while(layer->next != circ->cpath) {
-
- victim = layer->next;
- log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
- for(stream = circ->p_streams; stream; stream=stream->next_stream) {
- if(stream->cpath_layer == victim) {
- log_fn(LOG_INFO, "Marking stream %d for close.", *(int*)stream->stream_id);
-
- stream->marked_for_close = 1;
- stream->has_sent_end = 1;
- }
- }
- layer->next = victim->next;
- circuit_free_cpath_node(victim);
- }
- log_fn(LOG_INFO, "finished");
- return 0;
- }
- void assert_cpath_layer_ok(const crypt_path_t *cp)
- {
- assert(cp->f_crypto);
- assert(cp->b_crypto);
- assert(cp->addr);
- assert(cp->port);
- switch(cp->state)
- {
- case CPATH_STATE_CLOSED:
- case CPATH_STATE_OPEN:
- assert(!cp->handshake_state);
- break;
- case CPATH_STATE_AWAITING_KEYS:
- assert(cp->handshake_state);
- break;
- default:
- assert(0);
- }
- assert(cp->package_window >= 0);
- assert(cp->deliver_window >= 0);
- }
- void assert_cpath_ok(const crypt_path_t *cp)
- {
- while(cp->prev)
- cp = cp->prev;
- while(cp->next) {
- assert_cpath_layer_ok(cp);
-
- if (cp->prev) {
- if (cp->prev->state == CPATH_STATE_OPEN) {
- assert(cp->state == CPATH_STATE_CLOSED ||
- cp->state == CPATH_STATE_AWAITING_KEYS);
- } else {
- assert(cp->state == CPATH_STATE_CLOSED);
- }
- }
- cp = cp->next;
- }
- }
- void assert_circuit_ok(const circuit_t *c)
- {
- connection_t *conn;
- assert(c->n_addr);
- assert(c->n_port);
- assert(c->n_conn);
- assert(c->n_conn->type == CONN_TYPE_OR);
- if (c->p_conn)
- assert(c->p_conn->type == CONN_TYPE_OR);
- for (conn = c->p_streams; conn; conn = conn->next_stream)
- assert(c->p_conn->type == CONN_TYPE_EXIT);
- for (conn = c->n_streams; conn; conn = conn->next_stream)
- assert(conn->type == CONN_TYPE_EXIT);
- assert(c->deliver_window >= 0);
- assert(c->package_window >= 0);
- if (c->state == CIRCUIT_STATE_OPEN) {
- if (c->cpath) {
- assert(!c->n_crypto);
- assert(!c->p_crypto);
- } else {
- assert(c->n_crypto);
- assert(c->p_crypto);
- }
- }
- if (c->cpath) {
- assert_cpath_ok(c->cpath);
- }
- }
|