12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068 |
- #define RELAY_PRIVATE
- #include "or.h"
- #include "addressmap.h"
- #include "buffers.h"
- #include "channel.h"
- #include "circpathbias.h"
- #include "circuitbuild.h"
- #include "circuitlist.h"
- #include "circuituse.h"
- #include "config.h"
- #include "connection.h"
- #include "connection_edge.h"
- #include "connection_or.h"
- #include "control.h"
- #include "geoip.h"
- #include "main.h"
- #include "networkstatus.h"
- #include "nodelist.h"
- #include "onion.h"
- #include "policies.h"
- #include "reasons.h"
- #include "relay.h"
- #include "rendcache.h"
- #include "rendcommon.h"
- #include "router.h"
- #include "routerlist.h"
- #include "routerparse.h"
- #include "scheduler.h"
- static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
- cell_direction_t cell_direction,
- crypt_path_t *layer_hint);
- static int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
- edge_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(edge_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);
- static int circuit_queue_streams_are_blocked(circuit_t *circ);
- static void adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
- entry_connection_t *conn,
- node_t *node,
- const tor_addr_t *addr);
- #if 0
- static int get_max_middle_cells(void);
- #endif
- #define CELL_QUEUE_HIGHWATER_SIZE 256
- #define CELL_QUEUE_LOWWATER_SIZE 64
- uint64_t stats_n_relay_cells_relayed = 0;
- uint64_t stats_n_relay_cells_delivered = 0;
- static tor_weak_rng_t stream_choice_rng = TOR_WEAK_RNG_INIT;
- static void
- relay_set_digest(crypto_digest_t *digest, cell_t *cell)
- {
- char integrity[4];
- relay_header_t rh;
- crypto_digest_add_bytes(digest, (char*)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_t *digest, cell_t *cell)
- {
- uint32_t received_integrity, calculated_integrity;
- relay_header_t rh;
- crypto_digest_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, (char*) cell->payload, CELL_PAYLOAD_SIZE);
- crypto_digest_get_digest(digest, (char*) &calculated_integrity, 4);
- if (calculated_integrity != received_integrity) {
-
- crypto_digest_assign(digest, backup_digest);
-
- memcpy(rh.integrity, &received_integrity, 4);
- relay_header_pack(cell->payload, &rh);
- crypto_digest_free(backup_digest);
- return 0;
- }
- crypto_digest_free(backup_digest);
- return 1;
- }
- static int
- relay_crypt_one_payload(crypto_cipher_t *cipher, uint8_t *in,
- int encrypt_mode)
- {
- (void)encrypt_mode;
- crypto_cipher_crypt_inplace(cipher, (char*) in, CELL_PAYLOAD_SIZE);
- return 0;
- }
- int
- circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
- cell_direction_t cell_direction)
- {
- channel_t *chan = NULL;
- crypt_path_t *layer_hint=NULL;
- char recognized=0;
- int reason;
- 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) {
- log_warn(LD_BUG,"relay crypt failed. Dropping connection.");
- return -END_CIRC_REASON_INTERNAL;
- }
- if (recognized) {
- edge_connection_t *conn = NULL;
- if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
- pathbias_check_probe_response(circ, cell);
-
- return 0;
- }
- conn = relay_lookup_conn(circ, cell, cell_direction, layer_hint);
- if (cell_direction == CELL_DIRECTION_OUT) {
- ++stats_n_relay_cells_delivered;
- log_debug(LD_OR,"Sending away from origin.");
- if ((reason=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 reason;
- }
- }
- if (cell_direction == CELL_DIRECTION_IN) {
- ++stats_n_relay_cells_delivered;
- log_debug(LD_OR,"Sending to origin.");
- if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
- layer_hint)) < 0) {
- log_warn(LD_OR,
- "connection_edge_process_relay_cell (at origin) failed.");
- return reason;
- }
- }
- return 0;
- }
-
- if (cell_direction == CELL_DIRECTION_OUT) {
- cell->circ_id = circ->n_circ_id;
- chan = circ->n_chan;
- } else if (! CIRCUIT_IS_ORIGIN(circ)) {
- cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
- chan = TO_OR_CIRCUIT(circ)->p_chan;
- } else {
- log_fn(LOG_PROTOCOL_WARN, LD_OR,
- "Dropping unrecognized inbound cell on origin circuit.");
-
- if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
- TO_ORIGIN_CIRCUIT(circ)->path_state = PATH_STATE_USE_FAILED;
- return -END_CIRC_REASON_TORPROTOCOL;
- } else {
- return 0;
- }
- }
- if (!chan) {
-
- if (! CIRCUIT_IS_ORIGIN(circ) &&
- TO_OR_CIRCUIT(circ)->rend_splice &&
- cell_direction == CELL_DIRECTION_OUT) {
- or_circuit_t *splice_ = TO_OR_CIRCUIT(circ)->rend_splice;
- tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
- tor_assert(splice_->base_.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
- cell->circ_id = splice_->p_circ_id;
- cell->command = CELL_RELAY;
- if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice_),
- CELL_DIRECTION_IN)) < 0) {
- log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
- "circuits");
-
- circuit_mark_for_close(circ, -reason);
- return reason;
- }
- return 0;
- }
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Didn't recognize cell, but circ stops here! Closing circ.");
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- log_debug(LD_OR,"Passing on unrecognized cell.");
- ++stats_n_relay_cells_relayed;
- append_cell_to_circuit_queue(circ, chan, cell, cell_direction, 0);
- return 0;
- }
- int
- relay_crypt(circuit_t *circ, cell_t *cell, cell_direction_t cell_direction,
- crypt_path_t **layer_hint, char *recognized)
- {
- 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)) {
- crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
- thishop = 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 != cpath && thishop->state == CPATH_STATE_OPEN);
- log_fn(LOG_PROTOCOL_WARN, LD_OR,
- "Incoming cell at client not recognized. Closing.");
- return -1;
- } else {
- if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto,
- cell->payload, 1) < 0)
- return -1;
- }
- } else {
-
- if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto,
- cell->payload, 0) < 0)
- return -1;
- relay_header_unpack(&rh, cell->payload);
- if (rh.recognized == 0) {
-
- if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
- *recognized = 1;
- return 0;
- }
- }
- }
- return 0;
- }
- static int
- circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
- cell_direction_t cell_direction,
- crypt_path_t *layer_hint, streamid_t on_stream,
- const char *filename, int lineno)
- {
- channel_t *chan;
- if (circ->marked_for_close) {
-
- return 0;
- }
- if (cell_direction == CELL_DIRECTION_OUT) {
- crypt_path_t *thishop;
- chan = circ->n_chan;
- if (!chan) {
- log_warn(LD_BUG,"outgoing relay cell sent from %s:%d has n_chan==NULL."
- " Dropping.", filename, lineno);
- return 0;
- }
- if (!CIRCUIT_IS_ORIGIN(circ)) {
- log_warn(LD_BUG,"outgoing relay cell sent from %s:%d on non-origin "
- "circ. Dropping.", filename, lineno);
- return 0;
- }
- relay_set_digest(layer_hint->f_digest, cell);
- thishop = layer_hint;
-
- do {
- tor_assert(thishop);
-
- log_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 != TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
- } else {
- or_circuit_t *or_circ;
- if (CIRCUIT_IS_ORIGIN(circ)) {
-
- log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
- assert_circuit_ok(circ);
- return 0;
- }
- or_circ = TO_OR_CIRCUIT(circ);
- chan = or_circ->p_chan;
- relay_set_digest(or_circ->p_digest, cell);
- if (relay_crypt_one_payload(or_circ->p_crypto, cell->payload, 1) < 0)
- return -1;
- }
- ++stats_n_relay_cells_relayed;
- append_cell_to_circuit_queue(circ, chan, cell, cell_direction, on_stream);
- return 0;
- }
- static edge_connection_t *
- relay_lookup_conn(circuit_t *circ, cell_t *cell,
- cell_direction_t cell_direction, crypt_path_t *layer_hint)
- {
- edge_connection_t *tmpconn;
- relay_header_t rh;
- relay_header_unpack(&rh, cell->payload);
- if (!rh.stream_id)
- return NULL;
-
- if (CIRCUIT_IS_ORIGIN(circ)) {
- for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
- tmpconn=tmpconn->next_stream) {
- if (rh.stream_id == tmpconn->stream_id &&
- !tmpconn->base_.marked_for_close &&
- tmpconn->cpath_layer == layer_hint) {
- log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
- return tmpconn;
- }
- }
- } else {
- for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
- tmpconn=tmpconn->next_stream) {
- if (rh.stream_id == tmpconn->stream_id &&
- !tmpconn->base_.marked_for_close) {
- log_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 = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
- tmpconn=tmpconn->next_stream) {
- if (rh.stream_id == tmpconn->stream_id &&
- !tmpconn->base_.marked_for_close) {
- log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
- return tmpconn;
- }
- }
- }
- return NULL;
- }
- void
- relay_header_pack(uint8_t *dest, const relay_header_t *src)
- {
- set_uint8(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 uint8_t *src)
- {
- dest->command = get_uint8(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));
- }
- static const char *
- relay_command_to_string(uint8_t command)
- {
- static char buf[64];
- switch (command) {
- case RELAY_COMMAND_BEGIN: return "BEGIN";
- case RELAY_COMMAND_DATA: return "DATA";
- case RELAY_COMMAND_END: return "END";
- case RELAY_COMMAND_CONNECTED: return "CONNECTED";
- case RELAY_COMMAND_SENDME: return "SENDME";
- case RELAY_COMMAND_EXTEND: return "EXTEND";
- case RELAY_COMMAND_EXTENDED: return "EXTENDED";
- case RELAY_COMMAND_TRUNCATE: return "TRUNCATE";
- case RELAY_COMMAND_TRUNCATED: return "TRUNCATED";
- case RELAY_COMMAND_DROP: return "DROP";
- case RELAY_COMMAND_RESOLVE: return "RESOLVE";
- case RELAY_COMMAND_RESOLVED: return "RESOLVED";
- case RELAY_COMMAND_BEGIN_DIR: return "BEGIN_DIR";
- case RELAY_COMMAND_ESTABLISH_INTRO: return "ESTABLISH_INTRO";
- case RELAY_COMMAND_ESTABLISH_RENDEZVOUS: return "ESTABLISH_RENDEZVOUS";
- case RELAY_COMMAND_INTRODUCE1: return "INTRODUCE1";
- case RELAY_COMMAND_INTRODUCE2: return "INTRODUCE2";
- case RELAY_COMMAND_RENDEZVOUS1: return "RENDEZVOUS1";
- case RELAY_COMMAND_RENDEZVOUS2: return "RENDEZVOUS2";
- case RELAY_COMMAND_INTRO_ESTABLISHED: return "INTRO_ESTABLISHED";
- case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
- return "RENDEZVOUS_ESTABLISHED";
- case RELAY_COMMAND_INTRODUCE_ACK: return "INTRODUCE_ACK";
- case RELAY_COMMAND_EXTEND2: return "EXTEND2";
- case RELAY_COMMAND_EXTENDED2: return "EXTENDED2";
- default:
- tor_snprintf(buf, sizeof(buf), "Unrecognized relay command %u",
- (unsigned)command);
- return buf;
- }
- }
- int
- relay_send_command_from_edge_(streamid_t stream_id, circuit_t *circ,
- uint8_t relay_command, const char *payload,
- size_t payload_len, crypt_path_t *cpath_layer,
- const char *filename, int lineno)
- {
- cell_t cell;
- relay_header_t rh;
- cell_direction_t cell_direction;
-
- tor_assert(circ);
- tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
- 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 if (! CIRCUIT_IS_ORIGIN(circ)) {
- cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
- cell_direction = CELL_DIRECTION_IN;
- } else {
- return -1;
- }
- memset(&rh, 0, sizeof(rh));
- rh.command = relay_command;
- rh.stream_id = stream_id;
- rh.length = payload_len;
- relay_header_pack(cell.payload, &rh);
- if (payload_len)
- memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
- log_debug(LD_OR,"delivering %d cell %s.", relay_command,
- cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
-
- if (relay_command == RELAY_COMMAND_END && circ->dirreq_id)
- geoip_change_dirreq_state(circ->dirreq_id, DIRREQ_TUNNELED,
- DIRREQ_END_CELL_SENT);
- if (cell_direction == CELL_DIRECTION_OUT && circ->n_chan) {
-
- channel_timestamp_client(circ->n_chan);
- }
- if (cell_direction == CELL_DIRECTION_OUT) {
- origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
- if (origin_circ->remaining_relay_early_cells > 0 &&
- (relay_command == RELAY_COMMAND_EXTEND ||
- relay_command == RELAY_COMMAND_EXTEND2 ||
- cpath_layer != origin_circ->cpath)) {
-
- cell.command = CELL_RELAY_EARLY;
- --origin_circ->remaining_relay_early_cells;
- log_debug(LD_OR, "Sending a RELAY_EARLY cell; %d remaining.",
- (int)origin_circ->remaining_relay_early_cells);
-
- origin_circ->relay_early_commands[
- origin_circ->relay_early_cells_sent++] = relay_command;
- } else if (relay_command == RELAY_COMMAND_EXTEND ||
- relay_command == RELAY_COMMAND_EXTEND2) {
-
- smartlist_t *commands_list = smartlist_new();
- int i = 0;
- char *commands = NULL;
- for (; i < origin_circ->relay_early_cells_sent; i++)
- smartlist_add(commands_list, (char *)
- relay_command_to_string(origin_circ->relay_early_commands[i]));
- commands = smartlist_join_strings(commands_list, ",", 0, NULL);
- log_warn(LD_BUG, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
- "but we have run out of RELAY_EARLY cells on that circuit. "
- "Commands sent before: %s", commands);
- tor_free(commands);
- smartlist_free(commands_list);
- }
- }
- if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer,
- stream_id, filename, lineno) < 0) {
- log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
- circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
- return -1;
- }
- return 0;
- }
- int
- connection_edge_send_command(edge_connection_t *fromconn,
- uint8_t relay_command, const char *payload,
- size_t payload_len)
- {
-
- circuit_t *circ;
- crypt_path_t *cpath_layer = fromconn->cpath_layer;
- tor_assert(fromconn);
- circ = fromconn->on_circuit;
- if (fromconn->base_.marked_for_close) {
- log_warn(LD_BUG,
- "called on conn that's already marked for close at %s:%d.",
- fromconn->base_.marked_for_close_file,
- fromconn->base_.marked_for_close);
- return 0;
- }
- if (!circ) {
- if (fromconn->base_.type == CONN_TYPE_AP) {
- log_info(LD_APP,"no circ. Closing conn.");
- connection_mark_unattached_ap(EDGE_TO_ENTRY_CONN(fromconn),
- END_STREAM_REASON_INTERNAL);
- } else {
- log_info(LD_EXIT,"no circ. Closing conn.");
- fromconn->edge_has_sent_end = 1;
- fromconn->end_reason = END_STREAM_REASON_INTERNAL;
- connection_mark_for_close(TO_CONN(fromconn));
- }
- return -1;
- }
- if (circ->marked_for_close) {
-
- return -1;
- }
- return relay_send_command_from_edge(fromconn->stream_id, circ,
- relay_command, payload,
- payload_len, cpath_layer);
- }
- #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 ||
- reason == END_STREAM_REASON_NOROUTE;
- }
- static int
- connection_ap_process_end_not_open(
- relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
- entry_connection_t *conn, crypt_path_t *layer_hint)
- {
- node_t *exitrouter;
- int reason = *(cell->payload+RELAY_HEADER_SIZE);
- int control_reason;
- edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
- (void) layer_hint;
- if (rh->length > 0) {
- if (reason == END_STREAM_REASON_TORPROTOCOL ||
- reason == END_STREAM_REASON_DESTROY) {
-
- circ->path_state = PATH_STATE_USE_FAILED;
- return -END_CIRC_REASON_TORPROTOCOL;
- } else if (reason == END_STREAM_REASON_INTERNAL) {
-
- } else {
-
- pathbias_mark_use_success(circ);
- }
- }
- if (rh->length == 0) {
- reason = END_STREAM_REASON_MISC;
- }
- control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
- if (edge_reason_is_retriable(reason) &&
-
- !connection_edge_is_rendezvous_stream(edge_conn)) {
- const char *chosen_exit_digest =
- circ->build_state->chosen_exit->identity_digest;
- log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
- safe_str(conn->socks_request->address),
- stream_end_reason_to_string(reason));
- exitrouter = node_get_mutable_by_id(chosen_exit_digest);
- switch (reason) {
- case END_STREAM_REASON_EXITPOLICY: {
- tor_addr_t addr;
- tor_addr_make_unspec(&addr);
- if (rh->length >= 5) {
- int ttl = -1;
- tor_addr_make_unspec(&addr);
- if (rh->length == 5 || rh->length == 9) {
- tor_addr_from_ipv4n(&addr,
- get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
- if (rh->length == 9)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
- } else if (rh->length == 17 || rh->length == 21) {
- tor_addr_from_ipv6_bytes(&addr,
- (char*)(cell->payload+RELAY_HEADER_SIZE+1));
- if (rh->length == 21)
- ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+17));
- }
- if (tor_addr_is_null(&addr)) {
- log_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 ((tor_addr_family(&addr) == AF_INET &&
- !conn->entry_cfg.ipv4_traffic) ||
- (tor_addr_family(&addr) == AF_INET6 &&
- !conn->entry_cfg.ipv6_traffic)) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "Got an EXITPOLICY failure on a connection with a "
- "mismatched family. Closing.");
- connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- if (get_options()->ClientDNSRejectInternalAddresses &&
- tor_addr_is_internal(&addr, 0)) {
- log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
- safe_str(conn->socks_request->address));
- connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- client_dns_set_addressmap(conn,
- conn->socks_request->address, &addr,
- conn->chosen_exit_name, ttl);
- {
- char new_addr[TOR_ADDR_BUF_LEN];
- tor_addr_to_str(new_addr, &addr, sizeof(new_addr), 1);
- if (strcmp(conn->socks_request->address, new_addr)) {
- strlcpy(conn->socks_request->address, new_addr,
- sizeof(conn->socks_request->address));
- control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
- }
- }
- }
-
- adjust_exit_policy_from_exitpolicy_failure(circ,
- conn,
- exitrouter,
- &addr);
- if (conn->chosen_exit_optional ||
- conn->chosen_exit_retries) {
-
- conn->chosen_exit_optional = 0;
-
- conn->chosen_exit_retries = 0;
- tor_free(conn->chosen_exit_name);
- }
- if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
- return 0;
-
- break;
- }
- case END_STREAM_REASON_CONNECTREFUSED:
- if (!conn->chosen_exit_optional)
- break;
-
-
- case END_STREAM_REASON_RESOLVEFAILED:
- case END_STREAM_REASON_TIMEOUT:
- case END_STREAM_REASON_MISC:
- case END_STREAM_REASON_NOROUTE:
- if (client_dns_incr_failures(conn->socks_request->address)
- < MAX_RESOLVE_FAILURES) {
-
- circuit_log_path(LOG_INFO,LD_APP,circ);
-
- mark_circuit_unusable_for_new_conns(circ);
- if (conn->chosen_exit_optional) {
-
- conn->chosen_exit_optional = 0;
- tor_free(conn->chosen_exit_name);
- }
- if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
- return 0;
-
- } else {
- log_notice(LD_APP,
- "Have tried resolving or connecting to 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) {
- policies_set_node_exitpolicy_to_reject_all(exitrouter);
- }
- if (conn->chosen_exit_optional) {
-
- conn->chosen_exit_optional = 0;
- tor_free(conn->chosen_exit_name);
- }
- if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
- return 0;
-
- break;
- }
- log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
- }
- log_info(LD_APP,
- "Edge got end (%s) before we're connected. Marking for close.",
- stream_end_reason_to_string(rh->length > 0 ? reason : -1));
- circuit_log_path(LOG_INFO,LD_APP,circ);
-
- if (!ENTRY_TO_CONN(conn)->marked_for_close)
- connection_mark_unattached_ap(conn, control_reason);
- return 0;
- }
- static void
- adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
- entry_connection_t *conn,
- node_t *node,
- const tor_addr_t *addr)
- {
- int make_reject_all = 0;
- const sa_family_t family = tor_addr_family(addr);
- if (node) {
- tor_addr_t tmp;
- int asked_for_family = tor_addr_parse(&tmp, conn->socks_request->address);
- if (family == AF_UNSPEC) {
- make_reject_all = 1;
- } else if (node_exit_policy_is_exact(node, family) &&
- asked_for_family != -1 && !conn->chosen_exit_name) {
- make_reject_all = 1;
- }
- if (make_reject_all) {
- log_info(LD_APP,
- "Exitrouter %s seems to be more restrictive than its exit "
- "policy. Not using this router as exit for now.",
- node_describe(node));
- policies_set_node_exitpolicy_to_reject_all(node);
- }
- }
- if (family != AF_UNSPEC)
- addr_policy_append_reject_addr(&circ->prepend_policy, addr);
- }
- static void
- remap_event_helper(entry_connection_t *conn, const tor_addr_t *new_addr)
- {
- tor_addr_to_str(conn->socks_request->address, new_addr,
- sizeof(conn->socks_request->address),
- 1);
- control_event_stream_status(conn, STREAM_EVENT_REMAP,
- REMAP_STREAM_SOURCE_EXIT);
- }
- STATIC int
- connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
- tor_addr_t *addr_out, int *ttl_out)
- {
- uint32_t bytes;
- const uint8_t *payload = cell->payload + RELAY_HEADER_SIZE;
- tor_addr_make_unspec(addr_out);
- *ttl_out = -1;
- if (rh->length == 0)
- return 0;
- if (rh->length < 4)
- return -1;
- bytes = ntohl(get_uint32(payload));
-
- if (bytes != 0) {
-
- tor_addr_from_ipv4h(addr_out, bytes);
- if (rh->length >= 8) {
- bytes = ntohl(get_uint32(payload + 4));
- if (bytes <= INT32_MAX)
- *ttl_out = bytes;
- }
- } else {
- if (rh->length < 25)
- return -1;
- if (get_uint8(payload + 4) != 6)
- return -1;
- tor_addr_from_ipv6_bytes(addr_out, (char*)(payload + 5));
- bytes = ntohl(get_uint32(payload + 21));
- if (bytes <= INT32_MAX)
- *ttl_out = (int) bytes;
- }
- return 0;
- }
- STATIC void
- address_ttl_free(address_ttl_t *addr)
- {
- if (!addr)
- return;
- tor_free(addr->hostname);
- tor_free(addr);
- }
- STATIC int
- resolved_cell_parse(const cell_t *cell, const relay_header_t *rh,
- smartlist_t *addresses_out, int *errcode_out)
- {
- const uint8_t *cp;
- uint8_t answer_type;
- size_t answer_len;
- address_ttl_t *addr;
- size_t remaining;
- int errcode = 0;
- smartlist_t *addrs;
- tor_assert(cell);
- tor_assert(rh);
- tor_assert(addresses_out);
- tor_assert(errcode_out);
- *errcode_out = 0;
- if (rh->length > RELAY_PAYLOAD_SIZE)
- return -1;
- addrs = smartlist_new();
- cp = cell->payload + RELAY_HEADER_SIZE;
- remaining = rh->length;
- while (remaining) {
- const uint8_t *cp_orig = cp;
- if (remaining < 2)
- goto err;
- answer_type = *cp++;
- answer_len = *cp++;
- if (remaining < 2 + answer_len + 4) {
- goto err;
- }
- if (answer_type == RESOLVED_TYPE_IPV4) {
- if (answer_len != 4) {
- goto err;
- }
- addr = tor_malloc_zero(sizeof(*addr));
- tor_addr_from_ipv4n(&addr->addr, get_uint32(cp));
- cp += 4;
- addr->ttl = ntohl(get_uint32(cp));
- cp += 4;
- smartlist_add(addrs, addr);
- } else if (answer_type == RESOLVED_TYPE_IPV6) {
- if (answer_len != 16)
- goto err;
- addr = tor_malloc_zero(sizeof(*addr));
- tor_addr_from_ipv6_bytes(&addr->addr, (const char*) cp);
- cp += 16;
- addr->ttl = ntohl(get_uint32(cp));
- cp += 4;
- smartlist_add(addrs, addr);
- } else if (answer_type == RESOLVED_TYPE_HOSTNAME) {
- if (answer_len == 0) {
- goto err;
- }
- addr = tor_malloc_zero(sizeof(*addr));
- addr->hostname = tor_memdup_nulterm(cp, answer_len);
- cp += answer_len;
- addr->ttl = ntohl(get_uint32(cp));
- cp += 4;
- smartlist_add(addrs, addr);
- } else if (answer_type == RESOLVED_TYPE_ERROR_TRANSIENT ||
- answer_type == RESOLVED_TYPE_ERROR) {
- errcode = answer_type;
-
- cp += answer_len + 4;
- } else {
- cp += answer_len + 4;
- }
- tor_assert(((ssize_t)remaining) >= (cp - cp_orig));
- remaining -= (cp - cp_orig);
- }
- if (errcode && smartlist_len(addrs) == 0) {
-
- *errcode_out = errcode;
- }
- smartlist_add_all(addresses_out, addrs);
- smartlist_free(addrs);
- return 0;
- err:
-
- SMARTLIST_FOREACH(addrs, address_ttl_t *, a, address_ttl_free(a));
- smartlist_free(addrs);
- return -1;
- }
- static void
- connection_ap_handshake_socks_got_resolved_cell(entry_connection_t *conn,
- int error_code,
- smartlist_t *results)
- {
- address_ttl_t *addr_ipv4 = NULL;
- address_ttl_t *addr_ipv6 = NULL;
- address_ttl_t *addr_hostname = NULL;
- address_ttl_t *addr_best = NULL;
-
- if (error_code) {
- tor_assert(error_code == RESOLVED_TYPE_ERROR ||
- error_code == RESOLVED_TYPE_ERROR_TRANSIENT);
- connection_ap_handshake_socks_resolved(conn,
- error_code,0,NULL,-1,-1);
- return;
- }
-
- SMARTLIST_FOREACH_BEGIN(results, address_ttl_t *, addr) {
- if (addr->hostname) {
- if (!addr_hostname) {
- addr_hostname = addr;
- }
- } else if (tor_addr_family(&addr->addr) == AF_INET) {
- if (!addr_ipv4 && conn->entry_cfg.ipv4_traffic) {
- addr_ipv4 = addr;
- }
- } else if (tor_addr_family(&addr->addr) == AF_INET6) {
- if (!addr_ipv6 && conn->entry_cfg.ipv6_traffic) {
- addr_ipv6 = addr;
- }
- }
- } SMARTLIST_FOREACH_END(addr);
-
- if (conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
- if (addr_hostname) {
- connection_ap_handshake_socks_resolved(conn,
- RESOLVED_TYPE_HOSTNAME,
- strlen(addr_hostname->hostname),
- (uint8_t*)addr_hostname->hostname,
- addr_hostname->ttl,-1);
- } else {
- connection_ap_handshake_socks_resolved(conn,
- RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
- }
- return;
- }
- if (conn->entry_cfg.prefer_ipv6) {
- addr_best = addr_ipv6 ? addr_ipv6 : addr_ipv4;
- } else {
- addr_best = addr_ipv4 ? addr_ipv4 : addr_ipv6;
- }
-
- if (! addr_best) {
- connection_ap_handshake_socks_resolved(conn,
- RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
- return;
- }
- connection_ap_handshake_socks_resolved_addr(conn,
- &addr_best->addr,
- addr_best->ttl,
- -1);
- remap_event_helper(conn, &addr_best->addr);
- }
- STATIC int
- connection_edge_process_resolved_cell(edge_connection_t *conn,
- const cell_t *cell,
- const relay_header_t *rh)
- {
- entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
- smartlist_t *resolved_addresses = NULL;
- int errcode = 0;
- if (conn->base_.state != AP_CONN_STATE_RESOLVE_WAIT) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
- "not in state resolve_wait. Dropping.");
- return 0;
- }
- tor_assert(SOCKS_COMMAND_IS_RESOLVE(entry_conn->socks_request->command));
- resolved_addresses = smartlist_new();
- if (resolved_cell_parse(cell, rh, resolved_addresses, &errcode)) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Dropping malformed 'resolved' cell");
- connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
- goto done;
- }
- if (get_options()->ClientDNSRejectInternalAddresses) {
- int orig_len = smartlist_len(resolved_addresses);
- SMARTLIST_FOREACH_BEGIN(resolved_addresses, address_ttl_t *, addr) {
- if (addr->hostname == NULL && tor_addr_is_internal(&addr->addr, 0)) {
- log_info(LD_APP, "Got a resolved cell with answer %s; dropping that "
- "answer.",
- safe_str_client(fmt_addr(&addr->addr)));
- address_ttl_free(addr);
- SMARTLIST_DEL_CURRENT(resolved_addresses, addr);
- }
- } SMARTLIST_FOREACH_END(addr);
- if (orig_len && smartlist_len(resolved_addresses) == 0) {
- log_info(LD_APP, "Got a resolved cell with only private addresses; "
- "dropping it.");
- connection_ap_handshake_socks_resolved(entry_conn,
- RESOLVED_TYPE_ERROR_TRANSIENT,
- 0, NULL, 0, TIME_MAX);
- connection_mark_unattached_ap(entry_conn,
- END_STREAM_REASON_TORPROTOCOL);
- goto done;
- }
- }
- connection_ap_handshake_socks_got_resolved_cell(entry_conn,
- errcode,
- resolved_addresses);
- connection_mark_unattached_ap(entry_conn,
- END_STREAM_REASON_DONE |
- END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
- done:
- SMARTLIST_FOREACH(resolved_addresses, address_ttl_t *, addr,
- address_ttl_free(addr));
- smartlist_free(resolved_addresses);
- return 0;
- }
- static int
- connection_edge_process_relay_cell_not_open(
- relay_header_t *rh, cell_t *cell, circuit_t *circ,
- edge_connection_t *conn, crypt_path_t *layer_hint)
- {
- if (rh->command == RELAY_COMMAND_END) {
- if (CIRCUIT_IS_ORIGIN(circ) && conn->base_.type == CONN_TYPE_AP) {
- return connection_ap_process_end_not_open(rh, cell,
- TO_ORIGIN_CIRCUIT(circ),
- EDGE_TO_ENTRY_CONN(conn),
- layer_hint);
- } else {
-
- conn->edge_has_sent_end = 1;
- conn->end_reason = *(cell->payload+RELAY_HEADER_SIZE) |
- END_STREAM_REASON_FLAG_REMOTE;
- connection_mark_for_close(TO_CONN(conn));
- return 0;
- }
- }
- if (conn->base_.type == CONN_TYPE_AP &&
- rh->command == RELAY_COMMAND_CONNECTED) {
- tor_addr_t addr;
- int ttl;
- entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
- tor_assert(CIRCUIT_IS_ORIGIN(circ));
- if (conn->base_.state != AP_CONN_STATE_CONNECT_WAIT) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "Got 'connected' while not in state connect_wait. Dropping.");
- return 0;
- }
- CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
- conn->base_.state = AP_CONN_STATE_OPEN;
- log_info(LD_APP,"'connected' received for circid %u streamid %d "
- "after %d seconds.",
- (unsigned)circ->n_circ_id,
- rh->stream_id,
- (int)(time(NULL) - conn->base_.timestamp_lastread));
- if (connected_cell_parse(rh, cell, &addr, &ttl) < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "Got a badly formatted connected cell. Closing.");
- connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
- connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- if (tor_addr_family(&addr) != AF_UNSPEC) {
- const sa_family_t family = tor_addr_family(&addr);
- if (tor_addr_is_null(&addr) ||
- (get_options()->ClientDNSRejectInternalAddresses &&
- tor_addr_is_internal(&addr, 0))) {
- log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
- fmt_addr(&addr));
- connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
- connection_mark_unattached_ap(entry_conn,
- END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- if ((family == AF_INET && ! entry_conn->entry_cfg.ipv4_traffic) ||
- (family == AF_INET6 && ! entry_conn->entry_cfg.ipv6_traffic)) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "Got a connected cell to %s with unsupported address family."
- " Closing.", fmt_addr(&addr));
- connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
- connection_mark_unattached_ap(entry_conn,
- END_STREAM_REASON_TORPROTOCOL);
- return 0;
- }
- client_dns_set_addressmap(entry_conn,
- entry_conn->socks_request->address, &addr,
- entry_conn->chosen_exit_name, ttl);
- remap_event_helper(entry_conn, &addr);
- }
- circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
-
- tor_assert(entry_conn->socks_request != NULL);
- if (!entry_conn->socks_request->has_finished)
- connection_ap_handshake_socks_reply(entry_conn, NULL, 0, 0);
-
- log_debug(LD_APP, "considering");
- if (TO_CONN(conn)->linked_conn &&
- TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
- connection_t *dirconn = TO_CONN(conn)->linked_conn;
- log_debug(LD_APP, "it is! %d", dirconn->purpose);
- switch (dirconn->purpose) {
- case DIR_PURPOSE_FETCH_CERTIFICATE:
- if (consensus_is_waiting_for_certs())
- control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
- break;
- case DIR_PURPOSE_FETCH_CONSENSUS:
- control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
- break;
- case DIR_PURPOSE_FETCH_SERVERDESC:
- case DIR_PURPOSE_FETCH_MICRODESC:
- if (TO_DIR_CONN(dirconn)->router_purpose == ROUTER_PURPOSE_GENERAL)
- control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
- count_loading_descriptors_progress());
- break;
- }
- }
-
- if (entry_conn->pending_optimistic_data) {
- buf_free(entry_conn->pending_optimistic_data);
- entry_conn->pending_optimistic_data = NULL;
- }
-
- if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
-
- connection_mark_for_close(TO_CONN(conn));
- return 0;
- }
- return 0;
- }
- if (conn->base_.type == CONN_TYPE_AP &&
- rh->command == RELAY_COMMAND_RESOLVED) {
- return connection_edge_process_resolved_cell(conn, cell, rh);
- }
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Got an unexpected relay command %d, in state %d (%s). Dropping.",
- rh->command, conn->base_.state,
- conn_state_to_string(conn->base_.type, conn->base_.state));
- return 0;
- }
- static int
- connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
- edge_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;
- int reason;
- int optimistic_data = 0;
- tor_assert(cell);
- tor_assert(circ);
- relay_header_unpack(&rh, cell->payload);
- num_seen++;
- log_debug(domain, "Now seen %d relay cells here (command %d, stream %d).",
- num_seen, rh.command, rh.stream_id);
- if (rh.length > RELAY_PAYLOAD_SIZE) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Relay cell length field too long. Closing circuit.");
- return - END_CIRC_REASON_TORPROTOCOL;
- }
- if (rh.stream_id == 0) {
- switch (rh.command) {
- case RELAY_COMMAND_BEGIN:
- case RELAY_COMMAND_CONNECTED:
- case RELAY_COMMAND_END:
- case RELAY_COMMAND_RESOLVE:
- case RELAY_COMMAND_RESOLVED:
- case RELAY_COMMAND_BEGIN_DIR:
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay command %d with zero "
- "stream_id. Dropping.", (int)rh.command);
- return 0;
- default:
- ;
- }
- }
-
- if (conn && !connection_state_is_open(TO_CONN(conn))) {
- if (conn->base_.type == CONN_TYPE_EXIT &&
- (conn->base_.state == EXIT_CONN_STATE_CONNECTING ||
- conn->base_.state == EXIT_CONN_STATE_RESOLVING) &&
- rh.command == RELAY_COMMAND_DATA) {
-
- optimistic_data = 1;
- } else if (rh.stream_id == 0 && rh.command == RELAY_COMMAND_DATA) {
- log_warn(LD_BUG, "Somehow I had a connection that matched a "
- "data cell with stream ID 0.");
- } else {
- return connection_edge_process_relay_cell_not_open(
- &rh, cell, circ, conn, layer_hint);
- }
- }
- switch (rh.command) {
- case RELAY_COMMAND_DROP:
- return 0;
- case RELAY_COMMAND_BEGIN:
- case RELAY_COMMAND_BEGIN_DIR:
- if (layer_hint &&
- circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "Relay begin request unsupported at AP. Dropping.");
- return 0;
- }
- if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED &&
- layer_hint != TO_ORIGIN_CIRCUIT(circ)->cpath->prev) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "Relay begin request to Hidden Service "
- "from intermediary node. Dropping.");
- return 0;
- }
- if (conn) {
- log_fn(LOG_PROTOCOL_WARN, domain,
- "Begin cell for known stream. Dropping.");
- return 0;
- }
- if (rh.command == RELAY_COMMAND_BEGIN_DIR &&
- circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
-
- static uint64_t next_id = 0;
- circ->dirreq_id = ++next_id;
- TO_OR_CIRCUIT(circ)->p_chan->dirreq_id = circ->dirreq_id;
- }
- return connection_exit_begin_conn(cell, circ);
- 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.");
- if (conn) {
-
- connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
- connection_mark_for_close(TO_CONN(conn));
- }
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- log_debug(domain,"circ deliver_window now %d.", layer_hint ?
- layer_hint->deliver_window : circ->deliver_window);
- circuit_consider_sending_sendme(circ, layer_hint);
- if (rh.stream_id == 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay data cell with zero "
- "stream_id. Dropping.");
- return 0;
- } else if (!conn) {
- log_info(domain,"data cell dropped, unknown stream (streamid %d).",
- rh.stream_id);
- return 0;
- }
- if (--conn->deliver_window < 0) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "(relay data) conn deliver_window below 0. Killing.");
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- stats_n_data_bytes_received += rh.length;
- connection_write_to_buf((char*)(cell->payload + RELAY_HEADER_SIZE),
- rh.length, TO_CONN(conn));
- if (!optimistic_data) {
-
- connection_edge_consider_sending_sendme(conn);
- }
- return 0;
- case RELAY_COMMAND_END:
- reason = rh.length > 0 ?
- get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
- if (!conn) {
- log_info(domain,"end cell (%s) dropped, unknown stream.",
- stream_end_reason_to_string(reason));
- return 0;
- }
- log_info(domain,TOR_SOCKET_T_FORMAT": end cell (%s) for stream %d. "
- "Removing stream.",
- conn->base_.s,
- stream_end_reason_to_string(reason),
- conn->stream_id);
- if (conn->base_.type == CONN_TYPE_AP) {
- entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
- if (entry_conn->socks_request &&
- !entry_conn->socks_request->has_finished)
- log_warn(LD_BUG,
- "open stream hasn't sent socks answer yet? Closing.");
- }
-
- conn->edge_has_sent_end = 1;
- if (!conn->end_reason)
- conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
- if (!conn->base_.marked_for_close) {
-
- connection_mark_and_flush(TO_CONN(conn));
- }
- return 0;
- case RELAY_COMMAND_EXTEND:
- case RELAY_COMMAND_EXTEND2: {
- static uint64_t total_n_extend=0, total_nonearly=0;
- total_n_extend++;
- if (rh.stream_id) {
- log_fn(LOG_PROTOCOL_WARN, domain,
- "'extend' cell received for non-zero stream. Dropping.");
- return 0;
- }
- if (cell->command != CELL_RELAY_EARLY &&
- !networkstatus_get_param(NULL,"AllowNonearlyExtend",0,0,1)) {
- #define EARLY_WARNING_INTERVAL 3600
- static ratelim_t early_warning_limit =
- RATELIM_INIT(EARLY_WARNING_INTERVAL);
- char *m;
- if (cell->command == CELL_RELAY) {
- ++total_nonearly;
- if ((m = rate_limit_log(&early_warning_limit, approx_time()))) {
- double percentage = ((double)total_nonearly)/total_n_extend;
- percentage *= 100;
- log_fn(LOG_PROTOCOL_WARN, domain, "EXTEND cell received, "
- "but not via RELAY_EARLY. Dropping.%s", m);
- log_fn(LOG_PROTOCOL_WARN, domain, " (We have dropped %.02f%% of "
- "all EXTEND cells for this reason)", percentage);
- tor_free(m);
- }
- } else {
- log_fn(LOG_WARN, domain,
- "EXTEND cell received, in a cell with type %d! Dropping.",
- cell->command);
- }
- return 0;
- }
- return circuit_extend(cell, circ);
- }
- case RELAY_COMMAND_EXTENDED:
- case RELAY_COMMAND_EXTENDED2:
- if (!layer_hint) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "'extended' unsupported at non-origin. Dropping.");
- return 0;
- }
- log_debug(domain,"Got an extended cell! Yay.");
- {
- extended_cell_t extended_cell;
- if (extended_cell_parse(&extended_cell, rh.command,
- (const uint8_t*)cell->payload+RELAY_HEADER_SIZE,
- rh.length)<0) {
- log_warn(LD_PROTOCOL,
- "Can't parse EXTENDED cell; killing circuit.");
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
- &extended_cell.created_cell)) < 0) {
- circuit_mark_for_close(circ, -reason);
- return 0;
- }
- }
- if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
- log_info(domain,"circuit_send_next_onion_skin() failed.");
- return reason;
- }
- return 0;
- case RELAY_COMMAND_TRUNCATE:
- if (layer_hint) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "'truncate' unsupported at origin. Dropping.");
- return 0;
- }
- if (circ->n_hop) {
- if (circ->n_chan)
- log_warn(LD_BUG, "n_chan and n_hop set on the same circuit!");
- extend_info_free(circ->n_hop);
- circ->n_hop = NULL;
- tor_free(circ->n_chan_create_cell);
- circuit_set_state(circ, CIRCUIT_STATE_OPEN);
- }
- if (circ->n_chan) {
- uint8_t trunc_reason = get_uint8(cell->payload + RELAY_HEADER_SIZE);
- circuit_synchronize_written_or_bandwidth(circ, CIRCUIT_N_CHAN);
- circuit_clear_cell_queue(circ, circ->n_chan);
- channel_send_destroy(circ->n_circ_id, circ->n_chan,
- trunc_reason);
- circuit_set_n_circid_chan(circ, 0, NULL);
- }
- log_debug(LD_EXIT, "Processed 'truncate', replying.");
- {
- char payload[1];
- payload[0] = (char)END_CIRC_REASON_REQUESTED;
- relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
- payload, sizeof(payload), NULL);
- }
- return 0;
- case RELAY_COMMAND_TRUNCATED:
- if (!layer_hint) {
- log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
- "'truncated' unsupported at non-origin. Dropping.");
- return 0;
- }
- circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint,
- get_uint8(cell->payload + RELAY_HEADER_SIZE));
- return 0;
- case RELAY_COMMAND_CONNECTED:
- if (conn) {
- log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "'connected' unsupported while open. Closing circ.");
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- log_info(domain,
- "'connected' received on circid %u for streamid %d, "
- "no conn attached anymore. Ignoring.",
- (unsigned)circ->n_circ_id, rh.stream_id);
- return 0;
- case RELAY_COMMAND_SENDME:
- if (!rh.stream_id) {
- if (layer_hint) {
- if (layer_hint->package_window + CIRCWINDOW_INCREMENT >
- CIRCWINDOW_START_MAX) {
- static struct ratelim_t exit_warn_ratelim = RATELIM_INIT(600);
- log_fn_ratelim(&exit_warn_ratelim, LOG_WARN, LD_PROTOCOL,
- "Unexpected sendme cell from exit relay. "
- "Closing circ.");
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- layer_hint->package_window += CIRCWINDOW_INCREMENT;
- log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
- layer_hint->package_window);
- circuit_resume_edge_reading(circ, layer_hint);
- } else {
- if (circ->package_window + CIRCWINDOW_INCREMENT >
- CIRCWINDOW_START_MAX) {
- static struct ratelim_t client_warn_ratelim = RATELIM_INIT(600);
- log_fn_ratelim(&client_warn_ratelim,LOG_PROTOCOL_WARN, LD_PROTOCOL,
- "Unexpected sendme cell from client. "
- "Closing circ (window %d).",
- circ->package_window);
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- circ->package_window += CIRCWINDOW_INCREMENT;
- log_debug(LD_APP,
- "circ-level sendme at non-origin, packagewindow %d.",
- circ->package_window);
- circuit_resume_edge_reading(circ, layer_hint);
- }
- return 0;
- }
- if (!conn) {
- log_info(domain,"sendme cell dropped, unknown stream (streamid %d).",
- rh.stream_id);
- return 0;
- }
- conn->package_window += STREAMWINDOW_INCREMENT;
- log_debug(domain,"stream-level sendme, packagewindow now %d.",
- conn->package_window);
- if (circuit_queue_streams_are_blocked(circ)) {
-
- return 0;
- }
- connection_start_reading(TO_CONN(conn));
-
- if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
-
- connection_mark_for_close(TO_CONN(conn));
- return 0;
- }
- return 0;
- case RELAY_COMMAND_RESOLVE:
- if (layer_hint) {
- log_fn(LOG_PROTOCOL_WARN, LD_APP,
- "resolve request unsupported at AP; dropping.");
- return 0;
- } else if (conn) {
- log_fn(LOG_PROTOCOL_WARN, domain,
- "resolve request for known stream; dropping.");
- return 0;
- } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
- log_fn(LOG_PROTOCOL_WARN, domain,
- "resolve request on circ with purpose %d; dropping",
- circ->purpose);
- return 0;
- }
- connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
- return 0;
- case RELAY_COMMAND_RESOLVED:
- if (conn) {
- log_fn(LOG_PROTOCOL_WARN, domain,
- "'resolved' unsupported while open. Closing circ.");
- return -END_CIRC_REASON_TORPROTOCOL;
- }
- log_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, layer_hint,
- 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(edge_connection_t *conn, int package_partial,
- int *max_cells)
- {
- size_t bytes_to_process, length;
- char payload[CELL_PAYLOAD_SIZE];
- circuit_t *circ;
- const unsigned domain = conn->base_.type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
- int sending_from_optimistic = 0;
- entry_connection_t *entry_conn =
- conn->base_.type == CONN_TYPE_AP ? EDGE_TO_ENTRY_CONN(conn) : NULL;
- const int sending_optimistically =
- entry_conn &&
- conn->base_.type == CONN_TYPE_AP &&
- conn->base_.state != AP_CONN_STATE_OPEN;
- crypt_path_t *cpath_layer = conn->cpath_layer;
- tor_assert(conn);
- if (conn->base_.marked_for_close) {
- log_warn(LD_BUG,
- "called on conn that's already marked for close at %s:%d.",
- conn->base_.marked_for_close_file, conn->base_.marked_for_close);
- return 0;
- }
- if (max_cells && *max_cells <= 0)
- return 0;
- repeat_connection_edge_package_raw_inbuf:
- circ = circuit_get_by_edge_conn(conn);
- if (!circ) {
- log_info(domain,"conn has no circuit! Closing.");
- conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
- return -1;
- }
- if (circuit_consider_stop_edge_reading(circ, cpath_layer))
- return 0;
- if (conn->package_window <= 0) {
- log_info(domain,"called with package_window %d. Skipping.",
- conn->package_window);
- connection_stop_reading(TO_CONN(conn));
- return 0;
- }
- sending_from_optimistic = entry_conn &&
- entry_conn->sending_optimistic_data != NULL;
- if (PREDICT_UNLIKELY(sending_from_optimistic)) {
- bytes_to_process = buf_datalen(entry_conn->sending_optimistic_data);
- if (PREDICT_UNLIKELY(!bytes_to_process)) {
- log_warn(LD_BUG, "sending_optimistic_data was non-NULL but empty");
- bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
- sending_from_optimistic = 0;
- }
- } else {
- bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
- }
- if (!bytes_to_process)
- return 0;
- if (!package_partial && bytes_to_process < RELAY_PAYLOAD_SIZE)
- return 0;
- if (bytes_to_process > RELAY_PAYLOAD_SIZE) {
- length = RELAY_PAYLOAD_SIZE;
- } else {
- length = bytes_to_process;
- }
- stats_n_data_bytes_packaged += length;
- stats_n_data_cells_packaged += 1;
- if (PREDICT_UNLIKELY(sending_from_optimistic)) {
-
- fetch_from_buf(payload, length, entry_conn->sending_optimistic_data);
- if (!buf_datalen(entry_conn->sending_optimistic_data)) {
- buf_free(entry_conn->sending_optimistic_data);
- entry_conn->sending_optimistic_data = NULL;
- }
- } else {
- connection_fetch_from_buf(payload, length, TO_CONN(conn));
- }
- log_debug(domain,TOR_SOCKET_T_FORMAT": Packaging %d bytes (%d waiting).",
- conn->base_.s,
- (int)length, (int)connection_get_inbuf_len(TO_CONN(conn)));
- if (sending_optimistically && !sending_from_optimistic) {
-
- if (!entry_conn->pending_optimistic_data)
- entry_conn->pending_optimistic_data = buf_new();
- write_to_buf(payload, length, entry_conn->pending_optimistic_data);
- }
- if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
- payload, length) < 0 )
-
- return 0;
- if (!cpath_layer) {
- tor_assert(circ->package_window > 0);
- circ->package_window--;
- } else {
- tor_assert(cpath_layer->package_window > 0);
- cpath_layer->package_window--;
- }
- if (--conn->package_window <= 0) {
- connection_stop_reading(TO_CONN(conn));
- log_debug(domain,"conn->package_window reached 0.");
- circuit_consider_stop_edge_reading(circ, cpath_layer);
- return 0;
- }
- log_debug(domain,"conn->package_window is now %d",conn->package_window);
- if (max_cells) {
- *max_cells -= 1;
- if (*max_cells <= 0)
- return 0;
- }
-
- goto repeat_connection_edge_package_raw_inbuf;
- }
- void
- connection_edge_consider_sending_sendme(edge_connection_t *conn)
- {
- circuit_t *circ;
- if (connection_outbuf_too_full(TO_CONN(conn)))
- return;
- circ = circuit_get_by_edge_conn(conn);
- if (!circ) {
-
- log_info(LD_APP,"No circuit associated with conn. Skipping.");
- return;
- }
- while (conn->deliver_window <= STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
- log_debug(conn->base_.type == CONN_TYPE_AP ?LD_APP:LD_EXIT,
- "Outbuf %d, Queuing stream sendme.",
- (int)conn->base_.outbuf_flushlen);
- conn->deliver_window += STREAMWINDOW_INCREMENT;
- if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
- NULL, 0) < 0) {
- log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
- return;
- }
- }
- }
- static void
- circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
- {
- if (circuit_queue_streams_are_blocked(circ)) {
- log_debug(layer_hint?LD_APP:LD_EXIT,"Too big queue, no resuming");
- return;
- }
- log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
- if (CIRCUIT_IS_ORIGIN(circ))
- circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
- circ, layer_hint);
- else
- circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
- circ, layer_hint);
- }
- void
- stream_choice_seed_weak_rng(void)
- {
- crypto_seed_weak_rng(&stream_choice_rng);
- }
- static int
- circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
- circuit_t *circ,
- crypt_path_t *layer_hint)
- {
- edge_connection_t *conn;
- int n_packaging_streams, n_streams_left;
- int packaged_this_round;
- int cells_on_queue;
- int cells_per_conn;
- edge_connection_t *chosen_stream = NULL;
- int max_to_package;
- if (first_conn == NULL) {
-
- return 0;
- }
-
- max_to_package = circ->package_window;
- if (CIRCUIT_IS_ORIGIN(circ)) {
- cells_on_queue = circ->n_chan_cells.n;
- } else {
- or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
- cells_on_queue = or_circ->p_chan_cells.n;
- }
- if (CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue < max_to_package)
- max_to_package = CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue;
-
-
- {
- int num_streams = 0;
- for (conn = first_conn; conn; conn = conn->next_stream) {
- num_streams++;
- if (tor_weak_random_one_in_n(&stream_choice_rng, num_streams)) {
- chosen_stream = conn;
- }
-
- }
- }
-
- n_packaging_streams = 0;
-
- for (conn=chosen_stream; conn; conn = conn->next_stream) {
-
- if (conn->base_.marked_for_close || conn->package_window <= 0)
- continue;
- if (!layer_hint || conn->cpath_layer == layer_hint) {
- connection_start_reading(TO_CONN(conn));
- if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
- ++n_packaging_streams;
- }
- }
-
- for (conn = first_conn; conn != chosen_stream; conn = conn->next_stream) {
- if (conn->base_.marked_for_close || conn->package_window <= 0)
- continue;
- if (!layer_hint || conn->cpath_layer == layer_hint) {
- connection_start_reading(TO_CONN(conn));
- if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
- ++n_packaging_streams;
- }
- }
- if (n_packaging_streams == 0)
- return 0;
- again:
- cells_per_conn = CEIL_DIV(max_to_package, n_packaging_streams);
- packaged_this_round = 0;
- n_streams_left = 0;
-
- for (conn=first_conn; conn; conn=conn->next_stream) {
- if (conn->base_.marked_for_close || conn->package_window <= 0)
- continue;
- if (!layer_hint || conn->cpath_layer == layer_hint) {
- int n = cells_per_conn, r;
-
- r = connection_edge_package_raw_inbuf(conn, 1, &n);
-
- packaged_this_round += (cells_per_conn-n);
- if (r<0) {
-
- connection_mark_for_close(TO_CONN(conn));
- continue;
- }
-
- if (connection_get_inbuf_len(TO_CONN(conn)))
- ++n_streams_left;
-
- if (circuit_consider_stop_edge_reading(circ, layer_hint))
- return -1;
-
- }
- }
-
- if (packaged_this_round && packaged_this_round < max_to_package &&
- n_streams_left) {
- max_to_package -= packaged_this_round;
- n_packaging_streams = n_streams_left;
- goto again;
- }
- return 0;
- }
- static int
- circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
- {
- edge_connection_t *conn = NULL;
- unsigned domain = layer_hint ? LD_APP : LD_EXIT;
- if (!layer_hint) {
- or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
- log_debug(domain,"considering circ->package_window %d",
- circ->package_window);
- if (circ->package_window <= 0) {
- log_debug(domain,"yes, not-at-origin. stopped.");
- for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
- connection_stop_reading(TO_CONN(conn));
- return 1;
- }
- return 0;
- }
-
- log_debug(domain,"considering layer_hint->package_window %d",
- layer_hint->package_window);
- if (layer_hint->package_window <= 0) {
- log_debug(domain,"yes, at-origin. stopped.");
- for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
- conn=conn->next_stream) {
- if (conn->cpath_layer == layer_hint)
- connection_stop_reading(TO_CONN(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) {
- log_debug(LD_CIRC,"Queuing circuit sendme.");
- if (layer_hint)
- layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
- else
- circ->deliver_window += CIRCWINDOW_INCREMENT;
- if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
- NULL, 0, layer_hint) < 0) {
- log_warn(LD_CIRC,
- "relay_send_command_from_edge failed. Circuit's closed.");
- return;
- }
- }
- }
- #ifdef ACTIVE_CIRCUITS_PARANOIA
- #define assert_cmux_ok_paranoid(chan) \
- assert_circuit_mux_okay(chan)
- #else
- #define assert_cmux_ok_paranoid(chan)
- #endif
- static size_t total_cells_allocated = 0;
- static inline void
- packed_cell_free_unchecked(packed_cell_t *cell)
- {
- --total_cells_allocated;
- tor_free(cell);
- }
- STATIC packed_cell_t *
- packed_cell_new(void)
- {
- ++total_cells_allocated;
- return tor_malloc_zero(sizeof(packed_cell_t));
- }
- void
- packed_cell_free(packed_cell_t *cell)
- {
- if (!cell)
- return;
- packed_cell_free_unchecked(cell);
- }
- void
- dump_cell_pool_usage(int severity)
- {
- int n_circs = 0;
- int n_cells = 0;
- SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c) {
- n_cells += c->n_chan_cells.n;
- if (!CIRCUIT_IS_ORIGIN(c))
- n_cells += TO_OR_CIRCUIT(c)->p_chan_cells.n;
- ++n_circs;
- }
- SMARTLIST_FOREACH_END(c);
- tor_log(severity, LD_MM,
- "%d cells allocated on %d circuits. %d cells leaked.",
- n_cells, n_circs, (int)total_cells_allocated - n_cells);
- }
- static inline packed_cell_t *
- packed_cell_copy(const cell_t *cell, int wide_circ_ids)
- {
- packed_cell_t *c = packed_cell_new();
- cell_pack(c, cell, wide_circ_ids);
- return c;
- }
- void
- cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
- {
- TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
- ++queue->n;
- }
- void
- cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
- int exitward, const cell_t *cell,
- int wide_circ_ids, int use_stats)
- {
- packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
- (void)circ;
- (void)exitward;
- (void)use_stats;
- copy->inserted_time = (uint32_t) monotime_coarse_absolute_msec();
- cell_queue_append(queue, copy);
- }
- void
- cell_queue_init(cell_queue_t *queue)
- {
- memset(queue, 0, sizeof(cell_queue_t));
- TOR_SIMPLEQ_INIT(&queue->head);
- }
- void
- cell_queue_clear(cell_queue_t *queue)
- {
- packed_cell_t *cell;
- while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
- TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
- packed_cell_free_unchecked(cell);
- }
- TOR_SIMPLEQ_INIT(&queue->head);
- queue->n = 0;
- }
- STATIC packed_cell_t *
- cell_queue_pop(cell_queue_t *queue)
- {
- packed_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
- if (!cell)
- return NULL;
- TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
- --queue->n;
- return cell;
- }
- void
- destroy_cell_queue_init(destroy_cell_queue_t *queue)
- {
- memset(queue, 0, sizeof(destroy_cell_queue_t));
- TOR_SIMPLEQ_INIT(&queue->head);
- }
- void
- destroy_cell_queue_clear(destroy_cell_queue_t *queue)
- {
- destroy_cell_t *cell;
- while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
- TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
- tor_free(cell);
- }
- TOR_SIMPLEQ_INIT(&queue->head);
- queue->n = 0;
- }
- STATIC destroy_cell_t *
- destroy_cell_queue_pop(destroy_cell_queue_t *queue)
- {
- destroy_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
- if (!cell)
- return NULL;
- TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
- --queue->n;
- return cell;
- }
- void
- destroy_cell_queue_append(destroy_cell_queue_t *queue,
- circid_t circid,
- uint8_t reason)
- {
- destroy_cell_t *cell = tor_malloc_zero(sizeof(destroy_cell_t));
- cell->circid = circid;
- cell->reason = reason;
-
- cell->inserted_time = (uint32_t) monotime_coarse_absolute_msec();
- TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
- ++queue->n;
- }
- static packed_cell_t *
- destroy_cell_to_packed_cell(destroy_cell_t *inp, int wide_circ_ids)
- {
- packed_cell_t *packed = packed_cell_new();
- cell_t cell;
- memset(&cell, 0, sizeof(cell));
- cell.circ_id = inp->circid;
- cell.command = CELL_DESTROY;
- cell.payload[0] = inp->reason;
- cell_pack(packed, &cell, wide_circ_ids);
- tor_free(inp);
- return packed;
- }
- size_t
- packed_cell_mem_cost(void)
- {
- return sizeof(packed_cell_t);
- }
- STATIC size_t
- cell_queues_get_total_allocation(void)
- {
- return total_cells_allocated * packed_cell_mem_cost();
- }
- #define MEMORY_PRESSURE_INTERVAL (30*60)
- static time_t last_time_under_memory_pressure = 0;
- STATIC int
- cell_queues_check_size(void)
- {
- time_t now = time(NULL);
- size_t alloc = cell_queues_get_total_allocation();
- alloc += buf_get_total_allocation();
- alloc += tor_zlib_get_total_allocation();
- const size_t rend_cache_total = rend_cache_get_total_allocation();
- alloc += rend_cache_total;
- const size_t geoip_client_cache_total =
- geoip_client_cache_total_allocation();
- alloc += geoip_client_cache_total;
- if (alloc >= get_options()->MaxMemInQueues_low_threshold) {
- last_time_under_memory_pressure = approx_time();
- if (alloc >= get_options()->MaxMemInQueues) {
-
- if (rend_cache_total > get_options()->MaxMemInQueues / 5) {
- const size_t bytes_to_remove =
- rend_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
- rend_cache_clean_v2_descs_as_dir(now, bytes_to_remove);
- alloc -= rend_cache_total;
- alloc += rend_cache_get_total_allocation();
- }
- if (geoip_client_cache_total > get_options()->MaxMemInQueues / 5) {
- const size_t bytes_to_remove =
- geoip_client_cache_total -
- (size_t)(get_options()->MaxMemInQueues / 10);
- alloc -= geoip_client_cache_handle_oom(now, bytes_to_remove);
- }
- circuits_handle_oom(alloc);
- return 1;
- }
- }
- return 0;
- }
- int
- have_been_under_memory_pressure(void)
- {
- return last_time_under_memory_pressure + MEMORY_PRESSURE_INTERVAL
- < approx_time();
- }
- void
- update_circuit_on_cmux_(circuit_t *circ, cell_direction_t direction,
- const char *file, int lineno)
- {
- channel_t *chan = NULL;
- or_circuit_t *or_circ = NULL;
- circuitmux_t *cmux = NULL;
- tor_assert(circ);
-
- if (direction == CELL_DIRECTION_OUT) {
- chan = circ->n_chan;
- } else {
- or_circ = TO_OR_CIRCUIT(circ);
- chan = or_circ->p_chan;
- }
- tor_assert(chan);
- tor_assert(chan->cmux);
-
- cmux = chan->cmux;
-
- if (! circuitmux_is_circuit_attached(cmux, circ)) {
- log_warn(LD_BUG, "called on non-attached circuit from %s:%d",
- file, lineno);
- return;
- }
- tor_assert(circuitmux_attached_circuit_direction(cmux, circ) == direction);
- assert_cmux_ok_paranoid(chan);
-
- if (direction == CELL_DIRECTION_OUT) {
- circuitmux_set_num_cells(cmux, circ, circ->n_chan_cells.n);
- } else {
- circuitmux_set_num_cells(cmux, circ, or_circ->p_chan_cells.n);
- }
- assert_cmux_ok_paranoid(chan);
- }
- void
- channel_unlink_all_circuits(channel_t *chan, smartlist_t *circuits_out)
- {
- tor_assert(chan);
- tor_assert(chan->cmux);
- circuitmux_detach_all_circuits(chan->cmux, circuits_out);
- chan->num_n_circuits = 0;
- chan->num_p_circuits = 0;
- }
- static int
- set_streams_blocked_on_circ(circuit_t *circ, channel_t *chan,
- int block, streamid_t stream_id)
- {
- edge_connection_t *edge = NULL;
- int n = 0;
- if (circ->n_chan == chan) {
- circ->streams_blocked_on_n_chan = block;
- if (CIRCUIT_IS_ORIGIN(circ))
- edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
- } else {
- circ->streams_blocked_on_p_chan = block;
- tor_assert(!CIRCUIT_IS_ORIGIN(circ));
- edge = TO_OR_CIRCUIT(circ)->n_streams;
- }
- for (; edge; edge = edge->next_stream) {
- connection_t *conn = TO_CONN(edge);
- if (stream_id && edge->stream_id != stream_id)
- continue;
- if (edge->edge_blocked_on_circ != block) {
- ++n;
- edge->edge_blocked_on_circ = block;
- }
- if (!conn->read_event) {
-
- continue;
- }
- if (block) {
- if (connection_is_reading(conn))
- connection_stop_reading(conn);
- } else {
-
- if (!connection_is_reading(conn))
- connection_start_reading(conn);
- }
- }
- return n;
- }
- static uint8_t
- packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids)
- {
- if (wide_circ_ids) {
- return get_uint8(cell->body+4);
- } else {
- return get_uint8(cell->body+2);
- }
- }
- circid_t
- packed_cell_get_circid(const packed_cell_t *cell, int wide_circ_ids)
- {
- if (wide_circ_ids) {
- return ntohl(get_uint32(cell->body));
- } else {
- return ntohs(get_uint16(cell->body));
- }
- }
- MOCK_IMPL(int,
- channel_flush_from_first_active_circuit, (channel_t *chan, int max))
- {
- circuitmux_t *cmux = NULL;
- int n_flushed = 0;
- cell_queue_t *queue;
- destroy_cell_queue_t *destroy_queue=NULL;
- circuit_t *circ;
- or_circuit_t *or_circ;
- int streams_blocked;
- packed_cell_t *cell;
-
- tor_assert(chan);
- tor_assert(chan->cmux);
- cmux = chan->cmux;
-
- while (n_flushed < max) {
- circ = circuitmux_get_first_active_circuit(cmux, &destroy_queue);
- if (destroy_queue) {
- destroy_cell_t *dcell;
-
-
- tor_assert(destroy_queue->n > 0);
- dcell = destroy_cell_queue_pop(destroy_queue);
-
- tor_assert(dcell);
-
- cell = destroy_cell_to_packed_cell(dcell, chan->wide_circ_ids);
-
- channel_write_packed_cell(chan, cell);
-
- circuitmux_notify_xmit_destroy(cmux);
- cell = NULL;
- ++n_flushed;
- continue;
- }
-
- if (!circ) break;
- assert_cmux_ok_paranoid(chan);
- if (circ->n_chan == chan) {
- queue = &circ->n_chan_cells;
- streams_blocked = circ->streams_blocked_on_n_chan;
- } else {
- or_circ = TO_OR_CIRCUIT(circ);
- tor_assert(or_circ->p_chan == chan);
- queue = &TO_OR_CIRCUIT(circ)->p_chan_cells;
- streams_blocked = circ->streams_blocked_on_p_chan;
- }
-
- if ( queue->n == 0 ) {
- log_warn(LD_BUG, "Found a supposedly active circuit with no cells "
- "to send. Trying to recover.");
- circuitmux_set_num_cells(cmux, circ, 0);
- if (! circ->marked_for_close)
- circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
- continue;
- }
- tor_assert(queue->n > 0);
-
- cell = cell_queue_pop(queue);
-
- if (get_options()->CellStatistics ||
- get_options()->TestingEnableCellStatsEvent) {
- uint32_t msec_waiting;
- uint32_t msec_now = (uint32_t)monotime_coarse_absolute_msec();
- msec_waiting = msec_now - cell->inserted_time;
- if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
- or_circ = TO_OR_CIRCUIT(circ);
- or_circ->total_cell_waiting_time += msec_waiting;
- or_circ->processed_cells++;
- }
- if (get_options()->TestingEnableCellStatsEvent) {
- uint8_t command = packed_cell_get_command(cell, chan->wide_circ_ids);
- testing_cell_stats_entry_t *ent =
- tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
- ent->command = command;
- ent->waiting_time = msec_waiting / 10;
- ent->removed = 1;
- if (circ->n_chan == chan)
- ent->exitward = 1;
- if (!circ->testing_cell_stats)
- circ->testing_cell_stats = smartlist_new();
- smartlist_add(circ->testing_cell_stats, ent);
- }
- }
-
- if (queue->n == 0 && chan->dirreq_id)
- geoip_change_dirreq_state(chan->dirreq_id,
- DIRREQ_TUNNELED,
- DIRREQ_CIRC_QUEUE_FLUSHED);
-
- channel_write_packed_cell(chan, cell);
- cell = NULL;
-
-
- ++n_flushed;
-
- circuitmux_notify_xmit_cells(cmux, circ, 1);
- circuitmux_set_num_cells(cmux, circ, queue->n);
- if (queue->n == 0)
- log_debug(LD_GENERAL, "Made a circuit inactive.");
-
- if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
- set_streams_blocked_on_circ(circ, chan, 0, 0);
-
- }
-
- assert_cmux_ok_paranoid(chan);
- return n_flushed;
- }
- #if 0
- static int
- get_max_middle_cells(void)
- {
- return ORCIRC_MAX_MIDDLE_CELLS;
- }
- #endif
- void
- append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
- cell_t *cell, cell_direction_t direction,
- streamid_t fromstream)
- {
- or_circuit_t *orcirc = NULL;
- cell_queue_t *queue;
- int streams_blocked;
- #if 0
- uint32_t tgt_max_middle_cells, p_len, n_len, tmp, hard_max_middle_cells;
- #endif
- int exitward;
- if (circ->marked_for_close)
- return;
- exitward = (direction == CELL_DIRECTION_OUT);
- if (exitward) {
- queue = &circ->n_chan_cells;
- streams_blocked = circ->streams_blocked_on_n_chan;
- } else {
- orcirc = TO_OR_CIRCUIT(circ);
- queue = &orcirc->p_chan_cells;
- streams_blocked = circ->streams_blocked_on_p_chan;
- }
-
- #if 0
-
- if ((circ->n_chan != NULL) && CIRCUIT_IS_ORCIRC(circ)) {
- orcirc = TO_OR_CIRCUIT(circ);
- if (orcirc->p_chan) {
-
-
- tgt_max_middle_cells = get_max_middle_cells();
- if (tgt_max_middle_cells > 0) {
-
- if (orcirc->max_middle_cells == 0) {
- orcirc->max_middle_cells = tgt_max_middle_cells;
- } else {
- if (tgt_max_middle_cells > orcirc->max_middle_cells) {
-
- orcirc->max_middle_cells = tgt_max_middle_cells;
- } else if (tgt_max_middle_cells < orcirc->max_middle_cells) {
-
- n_len = circ->n_chan_cells.n;
- p_len = orcirc->p_chan_cells.n;
- tmp = tgt_max_middle_cells;
- if (tmp < n_len) tmp = n_len;
- if (tmp < p_len) tmp = p_len;
- orcirc->max_middle_cells = tmp;
- }
-
- }
- } else {
-
- orcirc->max_middle_cells = 0;
- }
-
- if (orcirc->max_middle_cells > 0) {
- hard_max_middle_cells =
- (uint32_t)(((double)orcirc->max_middle_cells) *
- ORCIRC_MAX_MIDDLE_KILL_THRESH);
- if ((unsigned)queue->n + 1 >= hard_max_middle_cells) {
-
- log_warn(LD_CIRC,
- "Got a cell exceeding the hard cap of %u in the "
- "%s direction on middle circ ID %u on chan ID "
- U64_FORMAT "; killing the circuit.",
- hard_max_middle_cells,
- (direction == CELL_DIRECTION_OUT) ? "n" : "p",
- (direction == CELL_DIRECTION_OUT) ?
- circ->n_circ_id : orcirc->p_circ_id,
- U64_PRINTF_ARG(
- (direction == CELL_DIRECTION_OUT) ?
- circ->n_chan->global_identifier :
- orcirc->p_chan->global_identifier));
- circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
- return;
- } else if ((unsigned)queue->n + 1 == orcirc->max_middle_cells) {
-
- log_warn(LD_CIRC,
- "While trying to queue a cell, reached the soft cap of %u "
- "in the %s direction on middle circ ID %u "
- "on chan ID " U64_FORMAT ".",
- orcirc->max_middle_cells,
- (direction == CELL_DIRECTION_OUT) ? "n" : "p",
- (direction == CELL_DIRECTION_OUT) ?
- circ->n_circ_id : orcirc->p_circ_id,
- U64_PRINTF_ARG(
- (direction == CELL_DIRECTION_OUT) ?
- circ->n_chan->global_identifier :
- orcirc->p_chan->global_identifier));
- }
- }
- }
- }
- #endif
- cell_queue_append_packed_copy(circ, queue, exitward, cell,
- chan->wide_circ_ids, 1);
- if (PREDICT_UNLIKELY(cell_queues_check_size())) {
-
- if (circ->marked_for_close)
- return;
- }
-
- if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
- set_streams_blocked_on_circ(circ, chan, 1, 0);
- if (streams_blocked && fromstream) {
-
- set_streams_blocked_on_circ(circ, chan, 1, fromstream);
- }
- update_circuit_on_cmux(circ, direction);
- if (queue->n == 1) {
-
- log_debug(LD_GENERAL, "Made a circuit active.");
- }
-
- scheduler_channel_has_waiting_cells(chan);
- }
- int
- append_address_to_payload(uint8_t *payload_out, const tor_addr_t *addr)
- {
- uint32_t a;
- switch (tor_addr_family(addr)) {
- case AF_INET:
- payload_out[0] = RESOLVED_TYPE_IPV4;
- payload_out[1] = 4;
- a = tor_addr_to_ipv4n(addr);
- memcpy(payload_out+2, &a, 4);
- return 6;
- case AF_INET6:
- payload_out[0] = RESOLVED_TYPE_IPV6;
- payload_out[1] = 16;
- memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
- return 18;
- case AF_UNSPEC:
- default:
- return -1;
- }
- }
- const uint8_t *
- decode_address_from_payload(tor_addr_t *addr_out, const uint8_t *payload,
- int payload_len)
- {
- if (payload_len < 2)
- return NULL;
- if (payload_len < 2+payload[1])
- return NULL;
- switch (payload[0]) {
- case RESOLVED_TYPE_IPV4:
- if (payload[1] != 4)
- return NULL;
- tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
- break;
- case RESOLVED_TYPE_IPV6:
- if (payload[1] != 16)
- return NULL;
- tor_addr_from_ipv6_bytes(addr_out, (char*)(payload+2));
- break;
- default:
- tor_addr_make_unspec(addr_out);
- break;
- }
- return payload + 2 + payload[1];
- }
- void
- circuit_clear_cell_queue(circuit_t *circ, channel_t *chan)
- {
- cell_queue_t *queue;
- cell_direction_t direction;
- if (circ->n_chan == chan) {
- queue = &circ->n_chan_cells;
- direction = CELL_DIRECTION_OUT;
- } else {
- or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
- tor_assert(orcirc->p_chan == chan);
- queue = &orcirc->p_chan_cells;
- direction = CELL_DIRECTION_IN;
- }
-
- cell_queue_clear(queue);
-
- if (chan->cmux && circuitmux_is_circuit_attached(chan->cmux, circ))
- update_circuit_on_cmux(circ, direction);
- }
- void
- assert_circuit_mux_okay(channel_t *chan)
- {
- tor_assert(chan);
- tor_assert(chan->cmux);
- circuitmux_assert_okay(chan->cmux);
- }
- static int
- circuit_queue_streams_are_blocked(circuit_t *circ)
- {
- if (CIRCUIT_IS_ORIGIN(circ)) {
- return circ->streams_blocked_on_n_chan;
- } else {
- return circ->streams_blocked_on_p_chan;
- }
- }
|