1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321 |
- const char relay_c_id[] = "$Id$";
- #include "or.h"
- static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
- crypt_path_t **layer_hint, char *recognized);
- static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
- int cell_direction);
- static int
- connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
- connection_t *conn,
- crypt_path_t *layer_hint);
- static void
- circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
- static void
- circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
- static int
- circuit_resume_edge_reading_helper(connection_t *conn,
- circuit_t *circ,
- crypt_path_t *layer_hint);
- static int
- circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
- unsigned long stats_n_relay_cells_relayed = 0;
- unsigned long stats_n_relay_cells_delivered = 0;
- static void
- relay_set_digest(crypto_digest_env_t *digest, cell_t *cell)
- {
- char integrity[4];
- relay_header_t rh;
- crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
- crypto_digest_get_digest(digest, integrity, 4);
- relay_header_unpack(&rh, cell->payload);
- memcpy(rh.integrity, integrity, 4);
- relay_header_pack(cell->payload, &rh);
- }
- static int
- relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell)
- {
- char received_integrity[4], calculated_integrity[4];
- relay_header_t rh;
- crypto_digest_env_t *backup_digest=NULL;
- backup_digest = crypto_digest_dup(digest);
- relay_header_unpack(&rh, cell->payload);
- memcpy(received_integrity, rh.integrity, 4);
- memset(rh.integrity, 0, 4);
- relay_header_pack(cell->payload, &rh);
- crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
- crypto_digest_get_digest(digest, calculated_integrity, 4);
- if (memcmp(received_integrity, calculated_integrity, 4)) {
-
- crypto_digest_assign(digest, backup_digest);
-
- memcpy(rh.integrity, received_integrity, 4);
- relay_header_pack(cell->payload, &rh);
- crypto_free_digest_env(backup_digest);
- return 0;
- }
- crypto_free_digest_env(backup_digest);
- return 1;
- }
- static int
- relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
- int encrypt_mode)
- {
- char out[CELL_PAYLOAD_SIZE];
- int r;
- if (encrypt_mode)
- r = crypto_cipher_encrypt(cipher, out, in, CELL_PAYLOAD_SIZE);
- else
- r = crypto_cipher_decrypt(cipher, out, in, CELL_PAYLOAD_SIZE);
- if (r) {
- warn(LD_BUG,"Error during relay encryption");
- return -1;
- }
- memcpy(in,out,CELL_PAYLOAD_SIZE);
- return 0;
- }
- int
- circuit_receive_relay_cell(cell_t *cell, circuit_t *circ, int cell_direction)
- {
- connection_t *conn=NULL;
- crypt_path_t *layer_hint=NULL;
- char recognized=0;
- tor_assert(cell);
- tor_assert(circ);
- tor_assert(cell_direction == CELL_DIRECTION_OUT ||
- cell_direction == CELL_DIRECTION_IN);
- if (circ->marked_for_close)
- return 0;
- if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
- warn(LD_BUG,"relay crypt failed. Dropping connection.");
- return -1;
- }
- if (recognized) {
- conn = relay_lookup_conn(circ, cell, cell_direction);
- if (cell_direction == CELL_DIRECTION_OUT) {
- ++stats_n_relay_cells_delivered;
- debug(LD_OR,"Sending away from origin.");
- if (connection_edge_process_relay_cell(cell, circ, conn, NULL) < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "connection_edge_process_relay_cell (away from origin) "
- "failed.");
- return -1;
- }
- }
- if (cell_direction == CELL_DIRECTION_IN) {
- ++stats_n_relay_cells_delivered;
- debug(LD_OR,"Sending to origin.");
- if (connection_edge_process_relay_cell(cell, circ, conn,
- layer_hint) < 0) {
- warn(LD_OR,"connection_edge_process_relay_cell (at origin) failed.");
- return -1;
- }
- }
- return 0;
- }
-
- if (cell_direction == CELL_DIRECTION_OUT) {
- cell->circ_id = circ->n_circ_id;
- conn = circ->n_conn;
- } else {
- cell->circ_id = circ->p_circ_id;
- conn = circ->p_conn;
- }
- if (!conn) {
- if (circ->rend_splice && cell_direction == CELL_DIRECTION_OUT) {
- tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
- tor_assert(circ->rend_splice->purpose ==
- CIRCUIT_PURPOSE_REND_ESTABLISHED);
- cell->circ_id = circ->rend_splice->p_circ_id;
- if (circuit_receive_relay_cell(cell, circ->rend_splice,
- CELL_DIRECTION_IN) < 0) {
- warn(LD_REND, "Error relaying cell across rendezvous; closing "
- "circuits");
-
- circuit_mark_for_close(circ);
- return -1;
- }
- return 0;
- }
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Didn't recognize cell, but circ stops here! Closing circ.");
- return -1;
- }
- debug(LD_OR,"Passing on unrecognized cell.");
- ++stats_n_relay_cells_relayed;
- connection_or_write_cell_to_buf(cell, conn);
- return 0;
- }
- static int
- relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
- crypt_path_t **layer_hint, char *recognized)
- {
- crypt_path_t *thishop;
- relay_header_t rh;
- tor_assert(circ);
- tor_assert(cell);
- tor_assert(recognized);
- tor_assert(cell_direction == CELL_DIRECTION_IN ||
- cell_direction == CELL_DIRECTION_OUT);
- if (cell_direction == CELL_DIRECTION_IN) {
- if (CIRCUIT_IS_ORIGIN(circ)) {
- tor_assert(circ->cpath);
- thishop = circ->cpath;
- if (thishop->state != CPATH_STATE_OPEN) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Relay cell before first created cell? Closing.");
- return -1;
- }
- do {
- tor_assert(thishop);
- if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
- return -1;
- relay_header_unpack(&rh, cell->payload);
- if (rh.recognized == 0) {
-
- if (relay_digest_matches(thishop->b_digest, cell)) {
- *recognized = 1;
- *layer_hint = thishop;
- return 0;
- }
- }
- thishop = thishop->next;
- } while (thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
- warn(LD_OR,"in-cell at OP not recognized. Closing.");
- return -1;
- } else {
- if (relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
- return -1;
- }
- } else {
-
- if (relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
- return -1;
- relay_header_unpack(&rh, cell->payload);
- if (rh.recognized == 0) {
-
- if (relay_digest_matches(circ->n_digest, cell)) {
- *recognized = 1;
- return 0;
- }
- }
- }
- return 0;
- }
- static int
- circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
- int cell_direction,
- crypt_path_t *layer_hint)
- {
- connection_t *conn;
- crypt_path_t *thishop;
- if (cell_direction == CELL_DIRECTION_OUT) {
- conn = circ->n_conn;
- if (!conn) {
- warn(LD_BUG,"outgoing relay cell has n_conn==NULL. Dropping.");
- return 0;
- }
- relay_set_digest(layer_hint->f_digest, cell);
- thishop = layer_hint;
-
- do {
- tor_assert(thishop);
-
- debug(LD_OR,"crypting a layer of the relay cell.");
- if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
- return -1;
- }
- thishop = thishop->prev;
- } while (thishop != circ->cpath->prev);
- } else {
- conn = circ->p_conn;
- if (!conn) {
-
- warn(LD_BUG,"incoming relay cell has p_conn==NULL. Dropping.");
- assert_circuit_ok(circ);
- return 0;
- }
- relay_set_digest(circ->p_digest, cell);
- if (relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
- return -1;
- }
- ++stats_n_relay_cells_relayed;
- connection_or_write_cell_to_buf(cell, conn);
- return 0;
- }
- static connection_t *
- relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
- {
- connection_t *tmpconn;
- relay_header_t rh;
- relay_header_unpack(&rh, cell->payload);
- if (!rh.stream_id)
- return NULL;
-
- for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
- if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
- debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
- if (cell_direction == CELL_DIRECTION_OUT ||
- connection_edge_is_rendezvous_stream(tmpconn))
- return tmpconn;
- }
- }
- for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
- if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
- debug(LD_APP,"found conn for stream %d.", rh.stream_id);
- return tmpconn;
- }
- }
- for (tmpconn = circ->resolving_streams; tmpconn;
- tmpconn=tmpconn->next_stream) {
- if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
- debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
- return tmpconn;
- }
- }
- return NULL;
- }
- void
- relay_header_pack(char *dest, const relay_header_t *src)
- {
- *(uint8_t*)(dest) = src->command;
- set_uint16(dest+1, htons(src->recognized));
- set_uint16(dest+3, htons(src->stream_id));
- memcpy(dest+5, src->integrity, 4);
- set_uint16(dest+9, htons(src->length));
- }
- void
- relay_header_unpack(relay_header_t *dest, const char *src)
- {
- dest->command = *(uint8_t*)(src);
- dest->recognized = ntohs(get_uint16(src+1));
- dest->stream_id = ntohs(get_uint16(src+3));
- memcpy(dest->integrity, src+5, 4);
- dest->length = ntohs(get_uint16(src+9));
- }
- int
- connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
- int relay_command, const char *payload,
- size_t payload_len, crypt_path_t *cpath_layer)
- {
- cell_t cell;
- relay_header_t rh;
- int cell_direction;
- if (fromconn && fromconn->marked_for_close) {
- warn(LD_BUG,
- "Bug: called on conn that's already marked for close at %s:%d.",
- fromconn->marked_for_close_file, fromconn->marked_for_close);
- return 0;
- }
- if (!circ) {
- tor_assert(fromconn);
- if (fromconn->type == CONN_TYPE_AP) {
- info(LD_APP,"no circ. Closing conn.");
- connection_mark_unattached_ap(fromconn, END_STREAM_REASON_INTERNAL);
- } else {
- info(LD_EXIT,"no circ. Closing conn.");
- fromconn->has_sent_end = 1;
- connection_mark_for_close(fromconn);
- }
- return -1;
- }
- memset(&cell, 0, sizeof(cell_t));
- cell.command = CELL_RELAY;
- if (cpath_layer) {
- cell.circ_id = circ->n_circ_id;
- cell_direction = CELL_DIRECTION_OUT;
- } else {
- cell.circ_id = circ->p_circ_id;
- cell_direction = CELL_DIRECTION_IN;
- }
- memset(&rh, 0, sizeof(rh));
- rh.command = relay_command;
- if (fromconn)
- rh.stream_id = fromconn->stream_id;
- rh.length = payload_len;
- relay_header_pack(cell.payload, &rh);
- if (payload_len) {
- tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
- memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
- }
- debug(LD_OR,"delivering %d cell %s.", relay_command,
- cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
- if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer)
- < 0) {
- warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
- circuit_mark_for_close(circ);
- return -1;
- }
- return 0;
- }
- static const char *
- connection_edge_end_reason_str(int reason)
- {
- switch (reason) {
- case -1:
- warn(LD_PROTOCOL,
- "End cell arrived with length 0. Should be at least 1.");
- return "MALFORMED";
- case END_STREAM_REASON_MISC: return "misc error";
- case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
- case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
- case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
- case END_STREAM_REASON_DESTROY: return "destroyed";
- case END_STREAM_REASON_DONE: return "closed normally";
- case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
- case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
- case END_STREAM_REASON_INTERNAL: return "internal error at server";
- case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
- case END_STREAM_REASON_CONNRESET: return "connection reset";
- case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
- default:
- warn(LD_PROTOCOL,"Reason for ending (%d) not recognized.",reason);
- return "unknown";
- }
- }
- socks5_reply_status_t
- connection_edge_end_reason_socks5_response(int reason)
- {
- switch (reason) {
- case END_STREAM_REASON_MISC:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_RESOLVEFAILED:
- return SOCKS5_HOST_UNREACHABLE;
- case END_STREAM_REASON_CONNECTREFUSED:
- return SOCKS5_CONNECTION_REFUSED;
- case END_STREAM_REASON_EXITPOLICY:
- return SOCKS5_NOT_ALLOWED;
- case END_STREAM_REASON_DESTROY:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_DONE:
- return SOCKS5_SUCCEEDED;
- case END_STREAM_REASON_TIMEOUT:
- return SOCKS5_TTL_EXPIRED;
- case END_STREAM_REASON_RESOURCELIMIT:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_HIBERNATING:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_INTERNAL:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_CONNRESET:
- return SOCKS5_CONNECTION_REFUSED;
- case END_STREAM_REASON_TORPROTOCOL:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_ALREADY_SOCKS_REPLIED:
- return SOCKS5_SUCCEEDED;
- case END_STREAM_REASON_CANT_ATTACH:
- return SOCKS5_GENERAL_ERROR;
- case END_STREAM_REASON_NET_UNREACHABLE:
- return SOCKS5_NET_UNREACHABLE;
- default:
- warn(LD_PROTOCOL,"Reason for ending (%d) not recognized.",reason);
- return SOCKS5_GENERAL_ERROR;
- }
- }
- #ifdef MS_WINDOWS
- #define E_CASE(s) case s: case WSA ## s
- #define S_CASE(s) case WSA ## s
- #else
- #define E_CASE(s) case s
- #define S_CASE(s) case s
- #endif
- int
- errno_to_end_reason(int e)
- {
- switch (e) {
- case EPIPE:
- return END_STREAM_REASON_DONE;
- E_CASE(EBADF):
- E_CASE(EFAULT):
- E_CASE(EINVAL):
- S_CASE(EISCONN):
- S_CASE(ENOTSOCK):
- S_CASE(EPROTONOSUPPORT):
- S_CASE(EAFNOSUPPORT):
- E_CASE(EACCES):
- S_CASE(ENOTCONN):
- S_CASE(ENETUNREACH):
- return END_STREAM_REASON_INTERNAL;
- S_CASE(ECONNREFUSED):
- return END_STREAM_REASON_CONNECTREFUSED;
- S_CASE(ECONNRESET):
- return END_STREAM_REASON_CONNRESET;
- S_CASE(ETIMEDOUT):
- return END_STREAM_REASON_TIMEOUT;
- S_CASE(ENOBUFS):
- case ENOMEM:
- case ENFILE:
- E_CASE(EMFILE):
- return END_STREAM_REASON_RESOURCELIMIT;
- default:
- info(LD_EXIT, "Didn't recognize errno %d (%s); telling the OP that "
- "we are ending a stream for 'misc' reason.",
- e, tor_socket_strerror(e));
- return END_STREAM_REASON_MISC;
- }
- }
- #define MAX_RESOLVE_FAILURES 3
- static int
- edge_reason_is_retriable(int reason)
- {
- return reason == END_STREAM_REASON_HIBERNATING ||
- reason == END_STREAM_REASON_RESOURCELIMIT ||
- reason == END_STREAM_REASON_EXITPOLICY ||
- reason == END_STREAM_REASON_RESOLVEFAILED ||
- reason == END_STREAM_REASON_MISC;
- }
- static int
- connection_edge_process_end_not_open(
- relay_header_t *rh, cell_t *cell, circuit_t *circ,
- connection_t *conn, crypt_path_t *layer_hint)
- {
- struct in_addr in;
- routerinfo_t *exitrouter;
- int reason = *(cell->payload+RELAY_HEADER_SIZE);
- if (rh->length > 0 && edge_reason_is_retriable(reason)) {
- if (conn->type != CONN_TYPE_AP) {
- warn(LD_PROTOCOL,
- "Got an end because of %s, but we're not an AP. Closing.",
- connection_edge_end_reason_str(reason));
- return -1;
- }
- info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
- safe_str(conn->socks_request->address),
- connection_edge_end_reason_str(reason));
- exitrouter =
- router_get_by_digest(circ->build_state->chosen_exit->identity_digest);
- switch (reason) {
- case END_STREAM_REASON_EXITPOLICY:
- if (rh->length >= 5) {
- uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
- int ttl;
- if (!addr) {
- info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
- safe_str(conn->socks_request->address));
- connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- if (rh->length >= 9)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
- else
- ttl = -1;
- client_dns_set_addressmap(conn->socks_request->address, addr,
- conn->chosen_exit_name, ttl);
- }
-
- if (exitrouter &&
- (rh->length < 5 ||
- (tor_inet_aton(conn->socks_request->address, &in) &&
- !conn->chosen_exit_name))) {
- notice(LD_APP,
- "Exitrouter '%s' seems to be more restrictive than its exit "
- "policy. Not using this router as exit for now.",
- exitrouter->nickname);
- addr_policy_free(exitrouter->exit_policy);
- exitrouter->exit_policy =
- router_parse_addr_policy_from_string("reject *:*", -1);
- }
-
- addressmap_rewrite(conn->socks_request->address,
- sizeof(conn->socks_request->address));
- if (connection_ap_detach_retriable(conn, circ) >= 0)
- return 0;
-
- break;
- case END_STREAM_REASON_RESOLVEFAILED:
- case END_STREAM_REASON_MISC:
- if (client_dns_incr_failures(conn->socks_request->address)
- < MAX_RESOLVE_FAILURES) {
-
- circuit_log_path(LOG_INFO,LD_APP,circ);
- tor_assert(circ->timestamp_dirty);
- circ->timestamp_dirty -= get_options()->MaxCircuitDirtiness;
- if (connection_ap_detach_retriable(conn, circ) >= 0)
- return 0;
-
- } else {
- notice(LD_APP,"Have tried resolving address '%s' at %d different "
- "places. Giving up.",
- safe_str(conn->socks_request->address), MAX_RESOLVE_FAILURES);
-
- client_dns_clear_failures(conn->socks_request->address);
- }
- break;
- case END_STREAM_REASON_HIBERNATING:
- case END_STREAM_REASON_RESOURCELIMIT:
- if (exitrouter) {
- addr_policy_free(exitrouter->exit_policy);
- exitrouter->exit_policy =
- router_parse_addr_policy_from_string("reject *:*", -1);
- }
- if (connection_ap_detach_retriable(conn, circ) >= 0)
- return 0;
-
- break;
- }
- info(LD_APP,"Giving up on retrying; conn can't be handled.");
- }
- info(LD_APP,"Edge got end (%s) before we're connected. Marking for close.",
- connection_edge_end_reason_str(rh->length > 0 ? reason : -1));
- if (conn->type == CONN_TYPE_AP) {
- circuit_log_path(LOG_INFO,LD_APP,circ);
- connection_mark_unattached_ap(conn, reason);
- } else {
- conn->has_sent_end = 1;
- connection_mark_for_close(conn);
- }
- return 0;
- }
- static int
- connection_edge_process_relay_cell_not_open(
- relay_header_t *rh, cell_t *cell, circuit_t *circ,
- connection_t *conn, crypt_path_t *layer_hint)
- {
- if (rh->command == RELAY_COMMAND_END)
- return connection_edge_process_end_not_open(rh, cell, circ, conn,
- layer_hint);
- if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_CONNECTED) {
- if (conn->state != AP_CONN_STATE_CONNECT_WAIT) {
- warn(LD_APP,"Got 'connected' while not in state connect_wait. "
- "Dropping.");
- return 0;
- }
- conn->state = AP_CONN_STATE_OPEN;
- info(LD_APP,"'connected' received after %d seconds.",
- (int)(time(NULL) - conn->timestamp_lastread));
- if (rh->length >= 4) {
- uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
- int ttl;
- if (!addr) {
- info(LD_APP,"...but it claims the IP address was 0.0.0.0. Closing.");
- connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL,
- conn->cpath_layer);
- connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- if (rh->length >= 8)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+4));
- else
- ttl = -1;
- client_dns_set_addressmap(conn->socks_request->address, addr,
- conn->chosen_exit_name, ttl);
- }
- circuit_log_path(LOG_INFO,LD_APP,circ);
- connection_ap_handshake_socks_reply(conn, NULL, 0, SOCKS5_SUCCEEDED);
-
- if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
-
- connection_mark_for_close(conn);
- return 0;
- }
- return 0;
- }
- if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_RESOLVED) {
- int ttl;
- int answer_len;
- if (conn->state != AP_CONN_STATE_RESOLVE_WAIT) {
- warn(LD_APP,"Got a 'resolved' cell while not in state resolve_wait. "
- "Dropping.");
- return 0;
- }
- tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE);
- answer_len = cell->payload[RELAY_HEADER_SIZE+1];
- if (rh->length < 2 || answer_len+2>rh->length) {
- warn(LD_PROTOCOL, "Dropping malformed 'resolved' cell");
- connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- if (rh->length >= answer_len+6)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+
- 2+answer_len));
- else
- ttl = -1;
- connection_ap_handshake_socks_resolved(conn,
- cell->payload[RELAY_HEADER_SIZE],
- cell->payload[RELAY_HEADER_SIZE+1],
- cell->payload+RELAY_HEADER_SIZE+2,
- ttl);
- connection_mark_unattached_ap(conn,
- END_STREAM_REASON_ALREADY_SOCKS_REPLIED);
- return 0;
- }
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Got an unexpected relay command %d, in state %d (%s). Dropping.",
- rh->command, conn->state,
- conn_state_to_string(conn->type, conn->state));
- return 0;
- }
- static int
- connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
- connection_t *conn,
- crypt_path_t *layer_hint)
- {
- static int num_seen=0;
- relay_header_t rh;
- unsigned domain = layer_hint?LD_APP:LD_EXIT;
- tor_assert(cell);
- tor_assert(circ);
- relay_header_unpack(&rh, cell->payload);
- num_seen++;
- debug(domain, "Now seen %d relay cells here.", num_seen);
- if (rh.length > RELAY_PAYLOAD_SIZE) {
- warn(LD_PROTOCOL, "Relay cell length field too long. Closing circuit.");
- return -1;
- }
-
- if (conn && !connection_state_is_open(conn))
- return connection_edge_process_relay_cell_not_open(
- &rh, cell, circ, conn, layer_hint);
- switch (rh.command) {
- case RELAY_COMMAND_DROP:
- info(domain,"Got a relay-level padding cell. Dropping.");
- return 0;
- case RELAY_COMMAND_BEGIN:
- if (layer_hint &&
- circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
- warn(LD_APP,"relay begin request unsupported at AP. Dropping.");
- return 0;
- }
- if (conn) {
- warn(domain,"begin cell for known stream. Dropping.");
- return 0;
- }
- connection_exit_begin_conn(cell, circ);
- return 0;
- case RELAY_COMMAND_DATA:
- ++stats_n_data_cells_received;
- if (( layer_hint && --layer_hint->deliver_window < 0) ||
- (!layer_hint && --circ->deliver_window < 0)) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "(relay data) circ deliver_window below 0. Killing.");
- connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL,
- conn->cpath_layer);
- connection_mark_for_close(conn);
- return -1;
- }
- debug(domain,"circ deliver_window now %d.", layer_hint ?
- layer_hint->deliver_window : circ->deliver_window);
- circuit_consider_sending_sendme(circ, layer_hint);
- if (!conn) {
- info(domain,"data cell dropped, unknown stream.");
- return 0;
- }
- if (--conn->deliver_window < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "(relay data) conn deliver_window below 0. Killing.");
- return -1;
- }
- stats_n_data_bytes_received += rh.length;
- connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
- rh.length, conn);
- connection_edge_consider_sending_sendme(conn);
- return 0;
- case RELAY_COMMAND_END:
- if (!conn) {
- info(domain,"end cell (%s) dropped, unknown stream.",
- connection_edge_end_reason_str(rh.length > 0 ?
- *(char *)(cell->payload+RELAY_HEADER_SIZE) : -1));
- return 0;
- }
- info(domain,"%d: end cell (%s) for stream %d. Removing stream.",
- conn->s,
- connection_edge_end_reason_str(rh.length > 0 ?
- *(char *)(cell->payload+RELAY_HEADER_SIZE) : -1),
- conn->stream_id);
- if (conn->socks_request && !conn->socks_request->has_finished)
- warn(LD_BUG,"Bug: open stream hasn't sent socks answer yet? Closing.");
- #ifdef HALF_OPEN
- conn->done_sending = 1;
- shutdown(conn->s, 1);
- if (conn->done_receiving) {
-
- conn->has_sent_end = 1;
- connection_mark_for_close(conn);
- conn->hold_open_until_flushed = 1;
- }
- #else
-
- conn->has_sent_end = 1;
- if (!conn->marked_for_close) {
-
- connection_mark_for_close(conn);
- conn->hold_open_until_flushed = 1;
- }
- #endif
- return 0;
- case RELAY_COMMAND_EXTEND:
- if (conn) {
- warn(domain,"'extend' for non-zero stream. Dropping.");
- return 0;
- }
- return circuit_extend(cell, circ);
- case RELAY_COMMAND_EXTENDED:
- if (!layer_hint) {
- warn(LD_PROTOCOL,"'extended' unsupported at non-origin. Dropping.");
- return 0;
- }
- debug(domain,"Got an extended cell! Yay.");
- if (circuit_finish_handshake(circ, CELL_CREATED,
- cell->payload+RELAY_HEADER_SIZE) < 0) {
- warn(domain,"circuit_finish_handshake failed.");
- return -1;
- }
- if (circuit_send_next_onion_skin(circ)<0) {
- info(domain,"circuit_send_next_onion_skin() failed.");
- return -1;
- }
- return 0;
- case RELAY_COMMAND_TRUNCATE:
- if (layer_hint) {
- warn(LD_APP,"'truncate' unsupported at origin. Dropping.");
- return 0;
- }
- if (circ->n_conn) {
- connection_send_destroy(circ->n_circ_id, circ->n_conn);
- circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
- }
- debug(LD_EXIT, "Processed 'truncate', replying.");
- connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
- NULL, 0, NULL);
- return 0;
- case RELAY_COMMAND_TRUNCATED:
- if (!layer_hint) {
- warn(LD_EXIT,"'truncated' unsupported at non-origin. Dropping.");
- return 0;
- }
- circuit_truncated(circ, layer_hint);
- return 0;
- case RELAY_COMMAND_CONNECTED:
- if (conn) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "'connected' unsupported while open. Closing circ.");
- return -1;
- }
- info(domain,"'connected' received, no conn attached anymore. Ignoring.");
- return 0;
- case RELAY_COMMAND_SENDME:
- if (!conn) {
- if (layer_hint) {
- layer_hint->package_window += CIRCWINDOW_INCREMENT;
- debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
- layer_hint->package_window);
- circuit_resume_edge_reading(circ, layer_hint);
- } else {
- circ->package_window += CIRCWINDOW_INCREMENT;
- debug(LD_APP,"circ-level sendme at non-origin, packagewindow %d.",
- circ->package_window);
- circuit_resume_edge_reading(circ, layer_hint);
- }
- return 0;
- }
- conn->package_window += STREAMWINDOW_INCREMENT;
- debug(domain,"stream-level sendme, packagewindow now %d.",
- conn->package_window);
- connection_start_reading(conn);
-
- if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
-
- connection_mark_for_close(conn);
- return 0;
- }
- return 0;
- case RELAY_COMMAND_RESOLVE:
- if (layer_hint) {
- warn(LD_APP,"resolve request unsupported at AP; dropping.");
- return 0;
- } else if (conn) {
- warn(domain, "resolve request for known stream; dropping.");
- return 0;
- } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
- warn(domain, "resolve request on circ with purpose %d; dropping",
- circ->purpose);
- return 0;
- }
- connection_exit_begin_resolve(cell, circ);
- return 0;
- case RELAY_COMMAND_RESOLVED:
- if (conn) {
- warn(domain,"'resolved' unsupported while open. Closing circ.");
- return -1;
- }
- info(domain,"'resolved' received, no conn attached anymore. Ignoring.");
- return 0;
- case RELAY_COMMAND_ESTABLISH_INTRO:
- case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
- case RELAY_COMMAND_INTRODUCE1:
- case RELAY_COMMAND_INTRODUCE2:
- case RELAY_COMMAND_INTRODUCE_ACK:
- case RELAY_COMMAND_RENDEZVOUS1:
- case RELAY_COMMAND_RENDEZVOUS2:
- case RELAY_COMMAND_INTRO_ESTABLISHED:
- case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
- rend_process_relay_cell(circ, rh.command, rh.length,
- cell->payload+RELAY_HEADER_SIZE);
- return 0;
- }
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Received unknown relay command %d. Perhaps the other side is using "
- "a newer version of Tor? Dropping.",
- rh.command);
- return 0;
- }
- uint64_t stats_n_data_cells_packaged = 0;
- uint64_t stats_n_data_bytes_packaged = 0;
- uint64_t stats_n_data_cells_received = 0;
- uint64_t stats_n_data_bytes_received = 0;
- int
- connection_edge_package_raw_inbuf(connection_t *conn, int package_partial)
- {
- size_t amount_to_process, length;
- char payload[CELL_PAYLOAD_SIZE];
- circuit_t *circ;
- unsigned domain = conn->cpath_layer ? LD_APP : LD_EXIT;
- tor_assert(conn);
- tor_assert(!connection_speaks_cells(conn));
- if (conn->marked_for_close) {
- warn(LD_BUG,
- "Bug: called on conn that's already marked for close at %s:%d.",
- conn->marked_for_close_file, conn->marked_for_close);
- return 0;
- }
- repeat_connection_edge_package_raw_inbuf:
- circ = circuit_get_by_edge_conn(conn);
- if (!circ) {
- info(domain,"conn has no circuit! Closing.");
- return -1;
- }
- if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
- return 0;
- if (conn->package_window <= 0) {
- info(domain,"called with package_window %d. Skipping.",
- conn->package_window);
- connection_stop_reading(conn);
- return 0;
- }
- amount_to_process = buf_datalen(conn->inbuf);
- if (!amount_to_process)
- return 0;
- if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
- return 0;
- if (amount_to_process > RELAY_PAYLOAD_SIZE) {
- length = RELAY_PAYLOAD_SIZE;
- } else {
- length = amount_to_process;
- }
- stats_n_data_bytes_packaged += length;
- stats_n_data_cells_packaged += 1;
- connection_fetch_from_buf(payload, length, conn);
- debug(domain,"(%d) Packaging %d bytes (%d waiting).", conn->s,
- (int)length, (int)buf_datalen(conn->inbuf));
- if (connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
- payload, length, conn->cpath_layer) < 0)
-
- return 0;
- if (!conn->cpath_layer) {
- tor_assert(circ->package_window > 0);
- circ->package_window--;
- } else {
- tor_assert(conn->cpath_layer->package_window > 0);
- conn->cpath_layer->package_window--;
- }
- if (--conn->package_window <= 0) {
- connection_stop_reading(conn);
- debug(domain,"conn->package_window reached 0.");
- circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
- return 0;
- }
- debug(domain,"conn->package_window is now %d",conn->package_window);
-
- goto repeat_connection_edge_package_raw_inbuf;
- }
- void
- connection_edge_consider_sending_sendme(connection_t *conn)
- {
- circuit_t *circ;
- if (connection_outbuf_too_full(conn))
- return;
- circ = circuit_get_by_edge_conn(conn);
- if (!circ) {
-
- info(LD_APP,"No circuit associated with conn. Skipping.");
- return;
- }
- while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
- debug(conn->cpath_layer?LD_APP:LD_EXIT,
- "Outbuf %d, Queueing stream sendme.", (int)conn->outbuf_flushlen);
- conn->deliver_window += STREAMWINDOW_INCREMENT;
- if (connection_edge_send_command(conn, circ, RELAY_COMMAND_SENDME,
- NULL, 0, conn->cpath_layer) < 0) {
- warn(LD_APP,"connection_edge_send_command failed. Returning.");
- return;
- }
- }
- }
- static void
- circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
- {
- debug(layer_hint?LD_APP:LD_EXIT,"resuming");
-
- if (circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint)
- >= 0)
- circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
- }
- static int
- circuit_resume_edge_reading_helper(connection_t *conn,
- circuit_t *circ,
- crypt_path_t *layer_hint)
- {
- for ( ; conn; conn=conn->next_stream) {
- if (conn->marked_for_close)
- continue;
- if ((!layer_hint && conn->package_window > 0) ||
- (layer_hint && conn->package_window > 0 &&
- conn->cpath_layer == layer_hint)) {
- connection_start_reading(conn);
-
- if (connection_edge_package_raw_inbuf(conn, 1)<0) {
-
- connection_mark_for_close(conn);
- continue;
- }
-
- if (circuit_consider_stop_edge_reading(circ, layer_hint))
- return -1;
- }
- }
- return 0;
- }
- static int
- circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
- {
- connection_t *conn = NULL;
- unsigned domain = layer_hint ? LD_APP : LD_EXIT;
- if (!layer_hint) {
- debug(domain,"considering circ->package_window %d", circ->package_window);
- if (circ->package_window <= 0) {
- debug(domain,"yes, not-at-origin. stopped.");
- for (conn = circ->n_streams; conn; conn=conn->next_stream)
- connection_stop_reading(conn);
- return 1;
- }
- return 0;
- }
-
- debug(domain,"considering layer_hint->package_window %d",
- layer_hint->package_window);
- if (layer_hint->package_window <= 0) {
- debug(domain,"yes, at-origin. stopped.");
- for (conn = circ->n_streams; conn; conn=conn->next_stream)
- if (conn->cpath_layer == layer_hint)
- connection_stop_reading(conn);
- for (conn = circ->p_streams; conn; conn=conn->next_stream)
- if (conn->cpath_layer == layer_hint)
- connection_stop_reading(conn);
- return 1;
- }
- return 0;
- }
- static void
- circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
- {
- while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
- CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
- debug(LD_CIRC,"Queueing circuit sendme.");
- if (layer_hint)
- layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
- else
- circ->deliver_window += CIRCWINDOW_INCREMENT;
- if (connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
- NULL, 0, layer_hint) < 0) {
- warn(LD_CIRC,"connection_edge_send_command failed. Circuit's closed.");
- return;
- }
- }
- }
|