relay.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file relay.c
  8. * \brief Handle relay cell encryption/decryption, plus packaging and
  9. * receiving from circuits, plus queuing on circuits.
  10. *
  11. * This is a core modules that makes Tor work. It's responsible for
  12. * dealing with RELAY cells (the ones that travel more than one hop along a
  13. * circuit), by:
  14. * <ul>
  15. * <li>constructing relays cells,
  16. * <li>encrypting relay cells,
  17. * <li>decrypting relay cells,
  18. * <li>demultiplexing relay cells as they arrive on a connection,
  19. * <li>queueing relay cells for retransmission,
  20. * <li>or handling relay cells that are for us to receive (as an exit or a
  21. * client).
  22. * </ul>
  23. *
  24. * RELAY cells are generated throughout the code at the client or relay side,
  25. * using relay_send_command_from_edge() or one of the functions like
  26. * connection_edge_send_command() that calls it. Of particular interest is
  27. * connection_edge_package_raw_inbuf(), which takes information that has
  28. * arrived on an edge connection socket, and packages it as a RELAY_DATA cell
  29. * -- this is how information is actually sent across the Tor network. The
  30. * cryptography for these functions is handled deep in
  31. * circuit_package_relay_cell(), which either adds a single layer of
  32. * encryption (if we're an exit), or multiple layers (if we're the origin of
  33. * the circuit). After construction and encryption, the RELAY cells are
  34. * passed to append_cell_to_circuit_queue(), which queues them for
  35. * transmission and tells the circuitmux (see circuitmux.c) that the circuit
  36. * is waiting to send something.
  37. *
  38. * Incoming RELAY cells arrive at circuit_receive_relay_cell(), called from
  39. * command.c. There they are decrypted and, if they are for us, are passed to
  40. * connection_edge_process_relay_cell(). If they're not for us, they're
  41. * re-queued for retransmission again with append_cell_to_circuit_queue().
  42. *
  43. * The connection_edge_process_relay_cell() function handles all the different
  44. * types of relay cells, launching requests or transmitting data as needed.
  45. **/
  46. #define RELAY_PRIVATE
  47. #include "or/or.h"
  48. #include "or/addressmap.h"
  49. #include "lib/err/backtrace.h"
  50. #include "common/buffers.h"
  51. #include "or/channel.h"
  52. #include "or/circpathbias.h"
  53. #include "or/circuitbuild.h"
  54. #include "or/circuitlist.h"
  55. #include "or/circuituse.h"
  56. #include "lib/compress/compress.h"
  57. #include "or/config.h"
  58. #include "or/connection.h"
  59. #include "or/connection_edge.h"
  60. #include "or/connection_or.h"
  61. #include "or/control.h"
  62. #include "lib/crypt_ops/crypto_rand.h"
  63. #include "lib/crypt_ops/crypto_util.h"
  64. #include "or/directory.h"
  65. #include "or/geoip.h"
  66. #include "or/hs_cache.h"
  67. #include "or/main.h"
  68. #include "or/networkstatus.h"
  69. #include "or/nodelist.h"
  70. #include "or/onion.h"
  71. #include "or/policies.h"
  72. #include "or/reasons.h"
  73. #include "or/relay.h"
  74. #include "or/relay_crypto.h"
  75. #include "or/rendcache.h"
  76. #include "or/rendcommon.h"
  77. #include "or/router.h"
  78. #include "or/routerlist.h"
  79. #include "or/routerparse.h"
  80. #include "or/scheduler.h"
  81. #include "or/rephist.h"
  82. #include "or/cell_st.h"
  83. #include "or/cell_queue_st.h"
  84. #include "or/cpath_build_state_st.h"
  85. #include "or/dir_connection_st.h"
  86. #include "or/destroy_cell_queue_st.h"
  87. #include "or/entry_connection_st.h"
  88. #include "or/extend_info_st.h"
  89. #include "or/or_circuit_st.h"
  90. #include "or/origin_circuit_st.h"
  91. #include "or/routerinfo_st.h"
  92. #include "or/socks_request_st.h"
  93. static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
  94. cell_direction_t cell_direction,
  95. crypt_path_t *layer_hint);
  96. static void circuit_consider_sending_sendme(circuit_t *circ,
  97. crypt_path_t *layer_hint);
  98. static void circuit_resume_edge_reading(circuit_t *circ,
  99. crypt_path_t *layer_hint);
  100. static int circuit_resume_edge_reading_helper(edge_connection_t *conn,
  101. circuit_t *circ,
  102. crypt_path_t *layer_hint);
  103. static int circuit_consider_stop_edge_reading(circuit_t *circ,
  104. crypt_path_t *layer_hint);
  105. static int circuit_queue_streams_are_blocked(circuit_t *circ);
  106. static void adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
  107. entry_connection_t *conn,
  108. node_t *node,
  109. const tor_addr_t *addr);
  110. /** Stop reading on edge connections when we have this many cells
  111. * waiting on the appropriate queue. */
  112. #define CELL_QUEUE_HIGHWATER_SIZE 256
  113. /** Start reading from edge connections again when we get down to this many
  114. * cells. */
  115. #define CELL_QUEUE_LOWWATER_SIZE 64
  116. /** Stats: how many relay cells have originated at this hop, or have
  117. * been relayed onward (not recognized at this hop)?
  118. */
  119. uint64_t stats_n_relay_cells_relayed = 0;
  120. /** Stats: how many relay cells have been delivered to streams at this
  121. * hop?
  122. */
  123. uint64_t stats_n_relay_cells_delivered = 0;
  124. /** Stats: how many circuits have we closed due to the cell queue limit being
  125. * reached (see append_cell_to_circuit_queue()) */
  126. uint64_t stats_n_circ_max_cell_reached = 0;
  127. /** Used to tell which stream to read from first on a circuit. */
  128. static tor_weak_rng_t stream_choice_rng = TOR_WEAK_RNG_INIT;
  129. /**
  130. * Update channel usage state based on the type of relay cell and
  131. * circuit properties.
  132. *
  133. * This is needed to determine if a client channel is being
  134. * used for application traffic, and if a relay channel is being
  135. * used for multihop circuits and application traffic. The decision
  136. * to pad in channelpadding.c depends upon this info (as well as
  137. * consensus parameters) to decide what channels to pad.
  138. */
  139. static void
  140. circuit_update_channel_usage(circuit_t *circ, cell_t *cell)
  141. {
  142. if (CIRCUIT_IS_ORIGIN(circ)) {
  143. /*
  144. * The client state was first set much earlier in
  145. * circuit_send_next_onion_skin(), so we can start padding as early as
  146. * possible.
  147. *
  148. * However, if padding turns out to be expensive, we may want to not do
  149. * it until actual application traffic starts flowing (which is controlled
  150. * via consensus param nf_pad_before_usage).
  151. *
  152. * So: If we're an origin circuit and we've created a full length circuit,
  153. * then any CELL_RELAY cell means application data. Increase the usage
  154. * state of the channel to indicate this.
  155. *
  156. * We want to wait for CELL_RELAY specifically here, so we know that
  157. * the channel was definitely being used for data and not for extends.
  158. * By default, we pad as soon as a channel has been used for *any*
  159. * circuits, so this state is irrelevant to the padding decision in
  160. * the default case. However, if padding turns out to be expensive,
  161. * we would like the ability to avoid padding until we're absolutely
  162. * sure that a channel is used for enough application data to be worth
  163. * padding.
  164. *
  165. * (So it does not matter that CELL_RELAY_EARLY can actually contain
  166. * application data. This is only a load reducing option and that edge
  167. * case does not matter if we're desperately trying to reduce overhead
  168. * anyway. See also consensus parameter nf_pad_before_usage).
  169. */
  170. if (BUG(!circ->n_chan))
  171. return;
  172. if (circ->n_chan->channel_usage == CHANNEL_USED_FOR_FULL_CIRCS &&
  173. cell->command == CELL_RELAY) {
  174. circ->n_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
  175. }
  176. } else {
  177. /* If we're a relay circuit, the question is more complicated. Basically:
  178. * we only want to pad connections that carry multihop (anonymous)
  179. * circuits.
  180. *
  181. * We assume we're more than one hop if either the previous hop
  182. * is not a client, or if the previous hop is a client and there's
  183. * a next hop. Then, circuit traffic starts at RELAY_EARLY, and
  184. * user application traffic starts when we see RELAY cells.
  185. */
  186. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  187. if (BUG(!or_circ->p_chan))
  188. return;
  189. if (!channel_is_client(or_circ->p_chan) ||
  190. (channel_is_client(or_circ->p_chan) && circ->n_chan)) {
  191. if (cell->command == CELL_RELAY_EARLY) {
  192. if (or_circ->p_chan->channel_usage < CHANNEL_USED_FOR_FULL_CIRCS) {
  193. or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
  194. }
  195. } else if (cell->command == CELL_RELAY) {
  196. or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
  197. }
  198. }
  199. }
  200. }
  201. /** Receive a relay cell:
  202. * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
  203. * origin; decrypt if we're headed toward the exit).
  204. * - Check if recognized (if exitward).
  205. * - If recognized and the digest checks out, then find if there's a stream
  206. * that the cell is intended for, and deliver it to the right
  207. * connection_edge.
  208. * - If not recognized, then we need to relay it: append it to the appropriate
  209. * cell_queue on <b>circ</b>.
  210. *
  211. * Return -<b>reason</b> on failure.
  212. */
  213. int
  214. circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  215. cell_direction_t cell_direction)
  216. {
  217. channel_t *chan = NULL;
  218. crypt_path_t *layer_hint=NULL;
  219. char recognized=0;
  220. int reason;
  221. tor_assert(cell);
  222. tor_assert(circ);
  223. tor_assert(cell_direction == CELL_DIRECTION_OUT ||
  224. cell_direction == CELL_DIRECTION_IN);
  225. if (circ->marked_for_close)
  226. return 0;
  227. if (relay_decrypt_cell(circ, cell, cell_direction, &layer_hint, &recognized)
  228. < 0) {
  229. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  230. "relay crypt failed. Dropping connection.");
  231. return -END_CIRC_REASON_INTERNAL;
  232. }
  233. circuit_update_channel_usage(circ, cell);
  234. if (recognized) {
  235. edge_connection_t *conn = NULL;
  236. if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
  237. pathbias_check_probe_response(circ, cell);
  238. /* We need to drop this cell no matter what to avoid code that expects
  239. * a certain purpose (such as the hidserv code). */
  240. return 0;
  241. }
  242. conn = relay_lookup_conn(circ, cell, cell_direction, layer_hint);
  243. if (cell_direction == CELL_DIRECTION_OUT) {
  244. ++stats_n_relay_cells_delivered;
  245. log_debug(LD_OR,"Sending away from origin.");
  246. if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
  247. < 0) {
  248. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  249. "connection_edge_process_relay_cell (away from origin) "
  250. "failed.");
  251. return reason;
  252. }
  253. }
  254. if (cell_direction == CELL_DIRECTION_IN) {
  255. ++stats_n_relay_cells_delivered;
  256. log_debug(LD_OR,"Sending to origin.");
  257. if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
  258. layer_hint)) < 0) {
  259. /* If a client is trying to connect to unknown hidden service port,
  260. * END_CIRC_AT_ORIGIN is sent back so we can then close the circuit.
  261. * Do not log warn as this is an expected behavior for a service. */
  262. if (reason != END_CIRC_AT_ORIGIN) {
  263. log_warn(LD_OR,
  264. "connection_edge_process_relay_cell (at origin) failed.");
  265. }
  266. return reason;
  267. }
  268. }
  269. return 0;
  270. }
  271. /* not recognized. pass it on. */
  272. if (cell_direction == CELL_DIRECTION_OUT) {
  273. cell->circ_id = circ->n_circ_id; /* switch it */
  274. chan = circ->n_chan;
  275. } else if (! CIRCUIT_IS_ORIGIN(circ)) {
  276. cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
  277. chan = TO_OR_CIRCUIT(circ)->p_chan;
  278. } else {
  279. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  280. "Dropping unrecognized inbound cell on origin circuit.");
  281. /* If we see unrecognized cells on path bias testing circs,
  282. * it's bad mojo. Those circuits need to die.
  283. * XXX: Shouldn't they always die? */
  284. if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
  285. TO_ORIGIN_CIRCUIT(circ)->path_state = PATH_STATE_USE_FAILED;
  286. return -END_CIRC_REASON_TORPROTOCOL;
  287. } else {
  288. return 0;
  289. }
  290. }
  291. if (!chan) {
  292. // XXXX Can this splice stuff be done more cleanly?
  293. if (! CIRCUIT_IS_ORIGIN(circ) &&
  294. TO_OR_CIRCUIT(circ)->rend_splice &&
  295. cell_direction == CELL_DIRECTION_OUT) {
  296. or_circuit_t *splice_ = TO_OR_CIRCUIT(circ)->rend_splice;
  297. tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  298. tor_assert(splice_->base_.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  299. cell->circ_id = splice_->p_circ_id;
  300. cell->command = CELL_RELAY; /* can't be relay_early anyway */
  301. if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice_),
  302. CELL_DIRECTION_IN)) < 0) {
  303. log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
  304. "circuits");
  305. /* XXXX Do this here, or just return -1? */
  306. circuit_mark_for_close(circ, -reason);
  307. return reason;
  308. }
  309. return 0;
  310. }
  311. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  312. "Didn't recognize cell, but circ stops here! Closing circ.");
  313. return -END_CIRC_REASON_TORPROTOCOL;
  314. }
  315. log_debug(LD_OR,"Passing on unrecognized cell.");
  316. ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
  317. * we might kill the circ before we relay
  318. * the cells. */
  319. append_cell_to_circuit_queue(circ, chan, cell, cell_direction, 0);
  320. return 0;
  321. }
  322. /** Package a relay cell from an edge:
  323. * - Encrypt it to the right layer
  324. * - Append it to the appropriate cell_queue on <b>circ</b>.
  325. */
  326. static int
  327. circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  328. cell_direction_t cell_direction,
  329. crypt_path_t *layer_hint, streamid_t on_stream,
  330. const char *filename, int lineno)
  331. {
  332. channel_t *chan; /* where to send the cell */
  333. if (circ->marked_for_close) {
  334. /* Circuit is marked; send nothing. */
  335. return 0;
  336. }
  337. if (cell_direction == CELL_DIRECTION_OUT) {
  338. chan = circ->n_chan;
  339. if (!chan) {
  340. log_warn(LD_BUG,"outgoing relay cell sent from %s:%d has n_chan==NULL."
  341. " Dropping. Circuit is in state %s (%d), and is "
  342. "%smarked for close. (%s:%d, %d)", filename, lineno,
  343. circuit_state_to_string(circ->state), circ->state,
  344. circ->marked_for_close ? "" : "not ",
  345. circ->marked_for_close_file?circ->marked_for_close_file:"",
  346. circ->marked_for_close, circ->marked_for_close_reason);
  347. if (CIRCUIT_IS_ORIGIN(circ)) {
  348. circuit_log_path(LOG_WARN, LD_BUG, TO_ORIGIN_CIRCUIT(circ));
  349. }
  350. log_backtrace(LOG_WARN,LD_BUG,"");
  351. return 0; /* just drop it */
  352. }
  353. if (!CIRCUIT_IS_ORIGIN(circ)) {
  354. log_warn(LD_BUG,"outgoing relay cell sent from %s:%d on non-origin "
  355. "circ. Dropping.", filename, lineno);
  356. log_backtrace(LOG_WARN,LD_BUG,"");
  357. return 0; /* just drop it */
  358. }
  359. relay_encrypt_cell_outbound(cell, TO_ORIGIN_CIRCUIT(circ), layer_hint);
  360. /* Update circ written totals for control port */
  361. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  362. ocirc->n_written_circ_bw = tor_add_u32_nowrap(ocirc->n_written_circ_bw,
  363. CELL_PAYLOAD_SIZE);
  364. } else { /* incoming cell */
  365. if (CIRCUIT_IS_ORIGIN(circ)) {
  366. /* We should never package an _incoming_ cell from the circuit
  367. * origin; that means we messed up somewhere. */
  368. log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
  369. assert_circuit_ok(circ);
  370. return 0; /* just drop it */
  371. }
  372. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  373. relay_encrypt_cell_inbound(cell, or_circ);
  374. chan = or_circ->p_chan;
  375. }
  376. ++stats_n_relay_cells_relayed;
  377. append_cell_to_circuit_queue(circ, chan, cell, cell_direction, on_stream);
  378. return 0;
  379. }
  380. /** If cell's stream_id matches the stream_id of any conn that's
  381. * attached to circ, return that conn, else return NULL.
  382. */
  383. static edge_connection_t *
  384. relay_lookup_conn(circuit_t *circ, cell_t *cell,
  385. cell_direction_t cell_direction, crypt_path_t *layer_hint)
  386. {
  387. edge_connection_t *tmpconn;
  388. relay_header_t rh;
  389. relay_header_unpack(&rh, cell->payload);
  390. if (!rh.stream_id)
  391. return NULL;
  392. /* IN or OUT cells could have come from either direction, now
  393. * that we allow rendezvous *to* an OP.
  394. */
  395. if (CIRCUIT_IS_ORIGIN(circ)) {
  396. for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  397. tmpconn=tmpconn->next_stream) {
  398. if (rh.stream_id == tmpconn->stream_id &&
  399. !tmpconn->base_.marked_for_close &&
  400. tmpconn->cpath_layer == layer_hint) {
  401. log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
  402. return tmpconn;
  403. }
  404. }
  405. } else {
  406. for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  407. tmpconn=tmpconn->next_stream) {
  408. if (rh.stream_id == tmpconn->stream_id &&
  409. !tmpconn->base_.marked_for_close) {
  410. log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
  411. if (cell_direction == CELL_DIRECTION_OUT ||
  412. connection_edge_is_rendezvous_stream(tmpconn))
  413. return tmpconn;
  414. }
  415. }
  416. for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
  417. tmpconn=tmpconn->next_stream) {
  418. if (rh.stream_id == tmpconn->stream_id &&
  419. !tmpconn->base_.marked_for_close) {
  420. log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
  421. return tmpconn;
  422. }
  423. }
  424. }
  425. return NULL; /* probably a begin relay cell */
  426. }
  427. /** Pack the relay_header_t host-order structure <b>src</b> into
  428. * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
  429. * about the wire format.
  430. */
  431. void
  432. relay_header_pack(uint8_t *dest, const relay_header_t *src)
  433. {
  434. set_uint8(dest, src->command);
  435. set_uint16(dest+1, htons(src->recognized));
  436. set_uint16(dest+3, htons(src->stream_id));
  437. memcpy(dest+5, src->integrity, 4);
  438. set_uint16(dest+9, htons(src->length));
  439. }
  440. /** Unpack the network-order buffer <b>src</b> into a host-order
  441. * relay_header_t structure <b>dest</b>.
  442. */
  443. void
  444. relay_header_unpack(relay_header_t *dest, const uint8_t *src)
  445. {
  446. dest->command = get_uint8(src);
  447. dest->recognized = ntohs(get_uint16(src+1));
  448. dest->stream_id = ntohs(get_uint16(src+3));
  449. memcpy(dest->integrity, src+5, 4);
  450. dest->length = ntohs(get_uint16(src+9));
  451. }
  452. /** Convert the relay <b>command</b> into a human-readable string. */
  453. static const char *
  454. relay_command_to_string(uint8_t command)
  455. {
  456. static char buf[64];
  457. switch (command) {
  458. case RELAY_COMMAND_BEGIN: return "BEGIN";
  459. case RELAY_COMMAND_DATA: return "DATA";
  460. case RELAY_COMMAND_END: return "END";
  461. case RELAY_COMMAND_CONNECTED: return "CONNECTED";
  462. case RELAY_COMMAND_SENDME: return "SENDME";
  463. case RELAY_COMMAND_EXTEND: return "EXTEND";
  464. case RELAY_COMMAND_EXTENDED: return "EXTENDED";
  465. case RELAY_COMMAND_TRUNCATE: return "TRUNCATE";
  466. case RELAY_COMMAND_TRUNCATED: return "TRUNCATED";
  467. case RELAY_COMMAND_DROP: return "DROP";
  468. case RELAY_COMMAND_RESOLVE: return "RESOLVE";
  469. case RELAY_COMMAND_RESOLVED: return "RESOLVED";
  470. case RELAY_COMMAND_BEGIN_DIR: return "BEGIN_DIR";
  471. case RELAY_COMMAND_ESTABLISH_INTRO: return "ESTABLISH_INTRO";
  472. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS: return "ESTABLISH_RENDEZVOUS";
  473. case RELAY_COMMAND_INTRODUCE1: return "INTRODUCE1";
  474. case RELAY_COMMAND_INTRODUCE2: return "INTRODUCE2";
  475. case RELAY_COMMAND_RENDEZVOUS1: return "RENDEZVOUS1";
  476. case RELAY_COMMAND_RENDEZVOUS2: return "RENDEZVOUS2";
  477. case RELAY_COMMAND_INTRO_ESTABLISHED: return "INTRO_ESTABLISHED";
  478. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  479. return "RENDEZVOUS_ESTABLISHED";
  480. case RELAY_COMMAND_INTRODUCE_ACK: return "INTRODUCE_ACK";
  481. case RELAY_COMMAND_EXTEND2: return "EXTEND2";
  482. case RELAY_COMMAND_EXTENDED2: return "EXTENDED2";
  483. default:
  484. tor_snprintf(buf, sizeof(buf), "Unrecognized relay command %u",
  485. (unsigned)command);
  486. return buf;
  487. }
  488. }
  489. /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
  490. * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
  491. * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
  492. * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
  493. * destination hop for OP->OR cells.
  494. *
  495. * If you can't send the cell, mark the circuit for close and return -1. Else
  496. * return 0.
  497. */
  498. MOCK_IMPL(int,
  499. relay_send_command_from_edge_,(streamid_t stream_id, circuit_t *circ,
  500. uint8_t relay_command, const char *payload,
  501. size_t payload_len, crypt_path_t *cpath_layer,
  502. const char *filename, int lineno))
  503. {
  504. cell_t cell;
  505. relay_header_t rh;
  506. cell_direction_t cell_direction;
  507. /* XXXX NM Split this function into a separate versions per circuit type? */
  508. tor_assert(circ);
  509. tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
  510. memset(&cell, 0, sizeof(cell_t));
  511. cell.command = CELL_RELAY;
  512. if (CIRCUIT_IS_ORIGIN(circ)) {
  513. tor_assert(cpath_layer);
  514. cell.circ_id = circ->n_circ_id;
  515. cell_direction = CELL_DIRECTION_OUT;
  516. } else {
  517. tor_assert(! cpath_layer);
  518. cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  519. cell_direction = CELL_DIRECTION_IN;
  520. }
  521. memset(&rh, 0, sizeof(rh));
  522. rh.command = relay_command;
  523. rh.stream_id = stream_id;
  524. rh.length = payload_len;
  525. relay_header_pack(cell.payload, &rh);
  526. if (payload_len)
  527. memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
  528. log_debug(LD_OR,"delivering %d cell %s.", relay_command,
  529. cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  530. if (relay_command == RELAY_COMMAND_DROP)
  531. rep_hist_padding_count_write(PADDING_TYPE_DROP);
  532. /* If we are sending an END cell and this circuit is used for a tunneled
  533. * directory request, advance its state. */
  534. if (relay_command == RELAY_COMMAND_END && circ->dirreq_id)
  535. geoip_change_dirreq_state(circ->dirreq_id, DIRREQ_TUNNELED,
  536. DIRREQ_END_CELL_SENT);
  537. if (cell_direction == CELL_DIRECTION_OUT && circ->n_chan) {
  538. /* if we're using relaybandwidthrate, this conn wants priority */
  539. channel_timestamp_client(circ->n_chan);
  540. }
  541. if (cell_direction == CELL_DIRECTION_OUT) {
  542. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  543. if (origin_circ->remaining_relay_early_cells > 0 &&
  544. (relay_command == RELAY_COMMAND_EXTEND ||
  545. relay_command == RELAY_COMMAND_EXTEND2 ||
  546. cpath_layer != origin_circ->cpath)) {
  547. /* If we've got any relay_early cells left and (we're sending
  548. * an extend cell or we're not talking to the first hop), use
  549. * one of them. Don't worry about the conn protocol version:
  550. * append_cell_to_circuit_queue will fix it up. */
  551. cell.command = CELL_RELAY_EARLY;
  552. --origin_circ->remaining_relay_early_cells;
  553. log_debug(LD_OR, "Sending a RELAY_EARLY cell; %d remaining.",
  554. (int)origin_circ->remaining_relay_early_cells);
  555. /* Memorize the command that is sent as RELAY_EARLY cell; helps debug
  556. * task 878. */
  557. origin_circ->relay_early_commands[
  558. origin_circ->relay_early_cells_sent++] = relay_command;
  559. } else if (relay_command == RELAY_COMMAND_EXTEND ||
  560. relay_command == RELAY_COMMAND_EXTEND2) {
  561. /* If no RELAY_EARLY cells can be sent over this circuit, log which
  562. * commands have been sent as RELAY_EARLY cells before; helps debug
  563. * task 878. */
  564. smartlist_t *commands_list = smartlist_new();
  565. int i = 0;
  566. char *commands = NULL;
  567. for (; i < origin_circ->relay_early_cells_sent; i++)
  568. smartlist_add(commands_list, (char *)
  569. relay_command_to_string(origin_circ->relay_early_commands[i]));
  570. commands = smartlist_join_strings(commands_list, ",", 0, NULL);
  571. log_warn(LD_BUG, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
  572. "but we have run out of RELAY_EARLY cells on that circuit. "
  573. "Commands sent before: %s", commands);
  574. tor_free(commands);
  575. smartlist_free(commands_list);
  576. }
  577. /* Let's assume we're well-behaved: Anything that we decide to send is
  578. * valid, delivered data. */
  579. circuit_sent_valid_data(origin_circ, rh.length);
  580. }
  581. if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer,
  582. stream_id, filename, lineno) < 0) {
  583. log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
  584. circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
  585. return -1;
  586. }
  587. return 0;
  588. }
  589. /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
  590. * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
  591. * that's sending the relay cell, or NULL if it's a control cell.
  592. * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
  593. * for OP->OR cells.
  594. *
  595. * If you can't send the cell, mark the circuit for close and
  596. * return -1. Else return 0.
  597. */
  598. int
  599. connection_edge_send_command(edge_connection_t *fromconn,
  600. uint8_t relay_command, const char *payload,
  601. size_t payload_len)
  602. {
  603. /* XXXX NM Split this function into a separate versions per circuit type? */
  604. circuit_t *circ;
  605. crypt_path_t *cpath_layer = fromconn->cpath_layer;
  606. tor_assert(fromconn);
  607. circ = fromconn->on_circuit;
  608. if (fromconn->base_.marked_for_close) {
  609. log_warn(LD_BUG,
  610. "called on conn that's already marked for close at %s:%d.",
  611. fromconn->base_.marked_for_close_file,
  612. fromconn->base_.marked_for_close);
  613. return 0;
  614. }
  615. if (!circ) {
  616. if (fromconn->base_.type == CONN_TYPE_AP) {
  617. log_info(LD_APP,"no circ. Closing conn.");
  618. connection_mark_unattached_ap(EDGE_TO_ENTRY_CONN(fromconn),
  619. END_STREAM_REASON_INTERNAL);
  620. } else {
  621. log_info(LD_EXIT,"no circ. Closing conn.");
  622. fromconn->edge_has_sent_end = 1; /* no circ to send to */
  623. fromconn->end_reason = END_STREAM_REASON_INTERNAL;
  624. connection_mark_for_close(TO_CONN(fromconn));
  625. }
  626. return -1;
  627. }
  628. if (circ->marked_for_close) {
  629. /* The circuit has been marked, but not freed yet. When it's freed, it
  630. * will mark this connection for close. */
  631. return -1;
  632. }
  633. #ifdef MEASUREMENTS_21206
  634. /* Keep track of the number of RELAY_DATA cells sent for directory
  635. * connections. */
  636. connection_t *linked_conn = TO_CONN(fromconn)->linked_conn;
  637. if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
  638. ++(TO_DIR_CONN(linked_conn)->data_cells_sent);
  639. }
  640. #endif /* defined(MEASUREMENTS_21206) */
  641. return relay_send_command_from_edge(fromconn->stream_id, circ,
  642. relay_command, payload,
  643. payload_len, cpath_layer);
  644. }
  645. /** How many times will I retry a stream that fails due to DNS
  646. * resolve failure or misc error?
  647. */
  648. #define MAX_RESOLVE_FAILURES 3
  649. /** Return 1 if reason is something that you should retry if you
  650. * get the end cell before you've connected; else return 0. */
  651. static int
  652. edge_reason_is_retriable(int reason)
  653. {
  654. return reason == END_STREAM_REASON_HIBERNATING ||
  655. reason == END_STREAM_REASON_RESOURCELIMIT ||
  656. reason == END_STREAM_REASON_EXITPOLICY ||
  657. reason == END_STREAM_REASON_RESOLVEFAILED ||
  658. reason == END_STREAM_REASON_MISC ||
  659. reason == END_STREAM_REASON_NOROUTE;
  660. }
  661. /** Called when we receive an END cell on a stream that isn't open yet,
  662. * from the client side.
  663. * Arguments are as for connection_edge_process_relay_cell().
  664. */
  665. static int
  666. connection_ap_process_end_not_open(
  667. relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
  668. entry_connection_t *conn, crypt_path_t *layer_hint)
  669. {
  670. node_t *exitrouter;
  671. int reason = *(cell->payload+RELAY_HEADER_SIZE);
  672. int control_reason;
  673. edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
  674. (void) layer_hint; /* unused */
  675. if (rh->length > 0) {
  676. if (reason == END_STREAM_REASON_TORPROTOCOL ||
  677. reason == END_STREAM_REASON_DESTROY) {
  678. /* Both of these reasons could mean a failed tag
  679. * hit the exit and it complained. Do not probe.
  680. * Fail the circuit. */
  681. circ->path_state = PATH_STATE_USE_FAILED;
  682. return -END_CIRC_REASON_TORPROTOCOL;
  683. } else if (reason == END_STREAM_REASON_INTERNAL) {
  684. /* We can't infer success or failure, since older Tors report
  685. * ENETUNREACH as END_STREAM_REASON_INTERNAL. */
  686. } else {
  687. /* Path bias: If we get a valid reason code from the exit,
  688. * it wasn't due to tagging.
  689. *
  690. * We rely on recognized+digest being strong enough to make
  691. * tags unlikely to allow us to get tagged, yet 'recognized'
  692. * reason codes here. */
  693. pathbias_mark_use_success(circ);
  694. }
  695. }
  696. /* This end cell is now valid. */
  697. circuit_read_valid_data(circ, rh->length);
  698. if (rh->length == 0) {
  699. reason = END_STREAM_REASON_MISC;
  700. }
  701. control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
  702. if (edge_reason_is_retriable(reason) &&
  703. /* avoid retry if rend */
  704. !connection_edge_is_rendezvous_stream(edge_conn)) {
  705. const char *chosen_exit_digest =
  706. circ->build_state->chosen_exit->identity_digest;
  707. log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
  708. safe_str(conn->socks_request->address),
  709. stream_end_reason_to_string(reason));
  710. exitrouter = node_get_mutable_by_id(chosen_exit_digest);
  711. switch (reason) {
  712. case END_STREAM_REASON_EXITPOLICY: {
  713. tor_addr_t addr;
  714. tor_addr_make_unspec(&addr);
  715. if (rh->length >= 5) {
  716. int ttl = -1;
  717. tor_addr_make_unspec(&addr);
  718. if (rh->length == 5 || rh->length == 9) {
  719. tor_addr_from_ipv4n(&addr,
  720. get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
  721. if (rh->length == 9)
  722. ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
  723. } else if (rh->length == 17 || rh->length == 21) {
  724. tor_addr_from_ipv6_bytes(&addr,
  725. (char*)(cell->payload+RELAY_HEADER_SIZE+1));
  726. if (rh->length == 21)
  727. ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+17));
  728. }
  729. if (tor_addr_is_null(&addr)) {
  730. log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
  731. safe_str(conn->socks_request->address));
  732. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  733. return 0;
  734. }
  735. if ((tor_addr_family(&addr) == AF_INET &&
  736. !conn->entry_cfg.ipv4_traffic) ||
  737. (tor_addr_family(&addr) == AF_INET6 &&
  738. !conn->entry_cfg.ipv6_traffic)) {
  739. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  740. "Got an EXITPOLICY failure on a connection with a "
  741. "mismatched family. Closing.");
  742. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  743. return 0;
  744. }
  745. if (get_options()->ClientDNSRejectInternalAddresses &&
  746. tor_addr_is_internal(&addr, 0)) {
  747. log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
  748. safe_str(conn->socks_request->address));
  749. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  750. return 0;
  751. }
  752. client_dns_set_addressmap(conn,
  753. conn->socks_request->address, &addr,
  754. conn->chosen_exit_name, ttl);
  755. {
  756. char new_addr[TOR_ADDR_BUF_LEN];
  757. tor_addr_to_str(new_addr, &addr, sizeof(new_addr), 1);
  758. if (strcmp(conn->socks_request->address, new_addr)) {
  759. strlcpy(conn->socks_request->address, new_addr,
  760. sizeof(conn->socks_request->address));
  761. control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
  762. }
  763. }
  764. }
  765. /* check if the exit *ought* to have allowed it */
  766. adjust_exit_policy_from_exitpolicy_failure(circ,
  767. conn,
  768. exitrouter,
  769. &addr);
  770. if (conn->chosen_exit_optional ||
  771. conn->chosen_exit_retries) {
  772. /* stop wanting a specific exit */
  773. conn->chosen_exit_optional = 0;
  774. /* A non-zero chosen_exit_retries can happen if we set a
  775. * TrackHostExits for this address under a port that the exit
  776. * relay allows, but then try the same address with a different
  777. * port that it doesn't allow to exit. We shouldn't unregister
  778. * the mapping, since it is probably still wanted on the
  779. * original port. But now we give away to the exit relay that
  780. * we probably have a TrackHostExits on it. So be it. */
  781. conn->chosen_exit_retries = 0;
  782. tor_free(conn->chosen_exit_name); /* clears it */
  783. }
  784. if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
  785. return 0;
  786. /* else, conn will get closed below */
  787. break;
  788. }
  789. case END_STREAM_REASON_CONNECTREFUSED:
  790. if (!conn->chosen_exit_optional)
  791. break; /* break means it'll close, below */
  792. /* Else fall through: expire this circuit, clear the
  793. * chosen_exit_name field, and try again. */
  794. /* Falls through. */
  795. case END_STREAM_REASON_RESOLVEFAILED:
  796. case END_STREAM_REASON_TIMEOUT:
  797. case END_STREAM_REASON_MISC:
  798. case END_STREAM_REASON_NOROUTE:
  799. if (client_dns_incr_failures(conn->socks_request->address)
  800. < MAX_RESOLVE_FAILURES) {
  801. /* We haven't retried too many times; reattach the connection. */
  802. circuit_log_path(LOG_INFO,LD_APP,circ);
  803. /* Mark this circuit "unusable for new streams". */
  804. mark_circuit_unusable_for_new_conns(circ);
  805. if (conn->chosen_exit_optional) {
  806. /* stop wanting a specific exit */
  807. conn->chosen_exit_optional = 0;
  808. tor_free(conn->chosen_exit_name); /* clears it */
  809. }
  810. if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
  811. return 0;
  812. /* else, conn will get closed below */
  813. } else {
  814. log_notice(LD_APP,
  815. "Have tried resolving or connecting to address '%s' "
  816. "at %d different places. Giving up.",
  817. safe_str(conn->socks_request->address),
  818. MAX_RESOLVE_FAILURES);
  819. /* clear the failures, so it will have a full try next time */
  820. client_dns_clear_failures(conn->socks_request->address);
  821. }
  822. break;
  823. case END_STREAM_REASON_HIBERNATING:
  824. case END_STREAM_REASON_RESOURCELIMIT:
  825. if (exitrouter) {
  826. policies_set_node_exitpolicy_to_reject_all(exitrouter);
  827. }
  828. if (conn->chosen_exit_optional) {
  829. /* stop wanting a specific exit */
  830. conn->chosen_exit_optional = 0;
  831. tor_free(conn->chosen_exit_name); /* clears it */
  832. }
  833. if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
  834. return 0;
  835. /* else, will close below */
  836. break;
  837. } /* end switch */
  838. log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
  839. }
  840. log_info(LD_APP,
  841. "Edge got end (%s) before we're connected. Marking for close.",
  842. stream_end_reason_to_string(rh->length > 0 ? reason : -1));
  843. circuit_log_path(LOG_INFO,LD_APP,circ);
  844. /* need to test because of detach_retriable */
  845. if (!ENTRY_TO_CONN(conn)->marked_for_close)
  846. connection_mark_unattached_ap(conn, control_reason);
  847. return 0;
  848. }
  849. /** Called when we have gotten an END_REASON_EXITPOLICY failure on <b>circ</b>
  850. * for <b>conn</b>, while attempting to connect via <b>node</b>. If the node
  851. * told us which address it rejected, then <b>addr</b> is that address;
  852. * otherwise it is AF_UNSPEC.
  853. *
  854. * If we are sure the node should have allowed this address, mark the node as
  855. * having a reject *:* exit policy. Otherwise, mark the circuit as unusable
  856. * for this particular address.
  857. **/
  858. static void
  859. adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
  860. entry_connection_t *conn,
  861. node_t *node,
  862. const tor_addr_t *addr)
  863. {
  864. int make_reject_all = 0;
  865. const sa_family_t family = tor_addr_family(addr);
  866. if (node) {
  867. tor_addr_t tmp;
  868. int asked_for_family = tor_addr_parse(&tmp, conn->socks_request->address);
  869. if (family == AF_UNSPEC) {
  870. make_reject_all = 1;
  871. } else if (node_exit_policy_is_exact(node, family) &&
  872. asked_for_family != -1 && !conn->chosen_exit_name) {
  873. make_reject_all = 1;
  874. }
  875. if (make_reject_all) {
  876. log_info(LD_APP,
  877. "Exitrouter %s seems to be more restrictive than its exit "
  878. "policy. Not using this router as exit for now.",
  879. node_describe(node));
  880. policies_set_node_exitpolicy_to_reject_all(node);
  881. }
  882. }
  883. if (family != AF_UNSPEC)
  884. addr_policy_append_reject_addr(&circ->prepend_policy, addr);
  885. }
  886. /** Helper: change the socks_request-&gt;address field on conn to the
  887. * dotted-quad representation of <b>new_addr</b>,
  888. * and send an appropriate REMAP event. */
  889. static void
  890. remap_event_helper(entry_connection_t *conn, const tor_addr_t *new_addr)
  891. {
  892. tor_addr_to_str(conn->socks_request->address, new_addr,
  893. sizeof(conn->socks_request->address),
  894. 1);
  895. control_event_stream_status(conn, STREAM_EVENT_REMAP,
  896. REMAP_STREAM_SOURCE_EXIT);
  897. }
  898. /** Extract the contents of a connected cell in <b>cell</b>, whose relay
  899. * header has already been parsed into <b>rh</b>. On success, set
  900. * <b>addr_out</b> to the address we're connected to, and <b>ttl_out</b> to
  901. * the ttl of that address, in seconds, and return 0. On failure, return
  902. * -1.
  903. *
  904. * Note that the resulting address can be UNSPEC if the connected cell had no
  905. * address (as for a stream to an union service or a tunneled directory
  906. * connection), and that the ttl can be absent (in which case <b>ttl_out</b>
  907. * is set to -1). */
  908. STATIC int
  909. connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
  910. tor_addr_t *addr_out, int *ttl_out)
  911. {
  912. uint32_t bytes;
  913. const uint8_t *payload = cell->payload + RELAY_HEADER_SIZE;
  914. tor_addr_make_unspec(addr_out);
  915. *ttl_out = -1;
  916. if (rh->length == 0)
  917. return 0;
  918. if (rh->length < 4)
  919. return -1;
  920. bytes = ntohl(get_uint32(payload));
  921. /* If bytes is 0, this is maybe a v6 address. Otherwise it's a v4 address */
  922. if (bytes != 0) {
  923. /* v4 address */
  924. tor_addr_from_ipv4h(addr_out, bytes);
  925. if (rh->length >= 8) {
  926. bytes = ntohl(get_uint32(payload + 4));
  927. if (bytes <= INT32_MAX)
  928. *ttl_out = bytes;
  929. }
  930. } else {
  931. if (rh->length < 25) /* 4 bytes of 0s, 1 addr, 16 ipv4, 4 ttl. */
  932. return -1;
  933. if (get_uint8(payload + 4) != 6)
  934. return -1;
  935. tor_addr_from_ipv6_bytes(addr_out, (char*)(payload + 5));
  936. bytes = ntohl(get_uint32(payload + 21));
  937. if (bytes <= INT32_MAX)
  938. *ttl_out = (int) bytes;
  939. }
  940. return 0;
  941. }
  942. /** Drop all storage held by <b>addr</b>. */
  943. STATIC void
  944. address_ttl_free_(address_ttl_t *addr)
  945. {
  946. if (!addr)
  947. return;
  948. tor_free(addr->hostname);
  949. tor_free(addr);
  950. }
  951. /** Parse a resolved cell in <b>cell</b>, with parsed header in <b>rh</b>.
  952. * Return -1 on parse error. On success, add one or more newly allocated
  953. * address_ttl_t to <b>addresses_out</b>; set *<b>errcode_out</b> to
  954. * one of 0, RESOLVED_TYPE_ERROR, or RESOLVED_TYPE_ERROR_TRANSIENT, and
  955. * return 0. */
  956. STATIC int
  957. resolved_cell_parse(const cell_t *cell, const relay_header_t *rh,
  958. smartlist_t *addresses_out, int *errcode_out)
  959. {
  960. const uint8_t *cp;
  961. uint8_t answer_type;
  962. size_t answer_len;
  963. address_ttl_t *addr;
  964. size_t remaining;
  965. int errcode = 0;
  966. smartlist_t *addrs;
  967. tor_assert(cell);
  968. tor_assert(rh);
  969. tor_assert(addresses_out);
  970. tor_assert(errcode_out);
  971. *errcode_out = 0;
  972. if (rh->length > RELAY_PAYLOAD_SIZE)
  973. return -1;
  974. addrs = smartlist_new();
  975. cp = cell->payload + RELAY_HEADER_SIZE;
  976. remaining = rh->length;
  977. while (remaining) {
  978. const uint8_t *cp_orig = cp;
  979. if (remaining < 2)
  980. goto err;
  981. answer_type = *cp++;
  982. answer_len = *cp++;
  983. if (remaining < 2 + answer_len + 4) {
  984. goto err;
  985. }
  986. if (answer_type == RESOLVED_TYPE_IPV4) {
  987. if (answer_len != 4) {
  988. goto err;
  989. }
  990. addr = tor_malloc_zero(sizeof(*addr));
  991. tor_addr_from_ipv4n(&addr->addr, get_uint32(cp));
  992. cp += 4;
  993. addr->ttl = ntohl(get_uint32(cp));
  994. cp += 4;
  995. smartlist_add(addrs, addr);
  996. } else if (answer_type == RESOLVED_TYPE_IPV6) {
  997. if (answer_len != 16)
  998. goto err;
  999. addr = tor_malloc_zero(sizeof(*addr));
  1000. tor_addr_from_ipv6_bytes(&addr->addr, (const char*) cp);
  1001. cp += 16;
  1002. addr->ttl = ntohl(get_uint32(cp));
  1003. cp += 4;
  1004. smartlist_add(addrs, addr);
  1005. } else if (answer_type == RESOLVED_TYPE_HOSTNAME) {
  1006. if (answer_len == 0) {
  1007. goto err;
  1008. }
  1009. addr = tor_malloc_zero(sizeof(*addr));
  1010. addr->hostname = tor_memdup_nulterm(cp, answer_len);
  1011. cp += answer_len;
  1012. addr->ttl = ntohl(get_uint32(cp));
  1013. cp += 4;
  1014. smartlist_add(addrs, addr);
  1015. } else if (answer_type == RESOLVED_TYPE_ERROR_TRANSIENT ||
  1016. answer_type == RESOLVED_TYPE_ERROR) {
  1017. errcode = answer_type;
  1018. /* Ignore the error contents */
  1019. cp += answer_len + 4;
  1020. } else {
  1021. cp += answer_len + 4;
  1022. }
  1023. tor_assert(((ssize_t)remaining) >= (cp - cp_orig));
  1024. remaining -= (cp - cp_orig);
  1025. }
  1026. if (errcode && smartlist_len(addrs) == 0) {
  1027. /* Report an error only if there were no results. */
  1028. *errcode_out = errcode;
  1029. }
  1030. smartlist_add_all(addresses_out, addrs);
  1031. smartlist_free(addrs);
  1032. return 0;
  1033. err:
  1034. /* On parse error, don't report any results */
  1035. SMARTLIST_FOREACH(addrs, address_ttl_t *, a, address_ttl_free(a));
  1036. smartlist_free(addrs);
  1037. return -1;
  1038. }
  1039. /** Helper for connection_edge_process_resolved_cell: given an error code,
  1040. * an entry_connection, and a list of address_ttl_t *, report the best answer
  1041. * to the entry_connection. */
  1042. static void
  1043. connection_ap_handshake_socks_got_resolved_cell(entry_connection_t *conn,
  1044. int error_code,
  1045. smartlist_t *results)
  1046. {
  1047. address_ttl_t *addr_ipv4 = NULL;
  1048. address_ttl_t *addr_ipv6 = NULL;
  1049. address_ttl_t *addr_hostname = NULL;
  1050. address_ttl_t *addr_best = NULL;
  1051. /* If it's an error code, that's easy. */
  1052. if (error_code) {
  1053. tor_assert(error_code == RESOLVED_TYPE_ERROR ||
  1054. error_code == RESOLVED_TYPE_ERROR_TRANSIENT);
  1055. connection_ap_handshake_socks_resolved(conn,
  1056. error_code,0,NULL,-1,-1);
  1057. return;
  1058. }
  1059. /* Get the first answer of each type. */
  1060. SMARTLIST_FOREACH_BEGIN(results, address_ttl_t *, addr) {
  1061. if (addr->hostname) {
  1062. if (!addr_hostname) {
  1063. addr_hostname = addr;
  1064. }
  1065. } else if (tor_addr_family(&addr->addr) == AF_INET) {
  1066. if (!addr_ipv4 && conn->entry_cfg.ipv4_traffic) {
  1067. addr_ipv4 = addr;
  1068. }
  1069. } else if (tor_addr_family(&addr->addr) == AF_INET6) {
  1070. if (!addr_ipv6 && conn->entry_cfg.ipv6_traffic) {
  1071. addr_ipv6 = addr;
  1072. }
  1073. }
  1074. } SMARTLIST_FOREACH_END(addr);
  1075. /* Now figure out which type we wanted to deliver. */
  1076. if (conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
  1077. if (addr_hostname) {
  1078. connection_ap_handshake_socks_resolved(conn,
  1079. RESOLVED_TYPE_HOSTNAME,
  1080. strlen(addr_hostname->hostname),
  1081. (uint8_t*)addr_hostname->hostname,
  1082. addr_hostname->ttl,-1);
  1083. } else {
  1084. connection_ap_handshake_socks_resolved(conn,
  1085. RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
  1086. }
  1087. return;
  1088. }
  1089. if (conn->entry_cfg.prefer_ipv6) {
  1090. addr_best = addr_ipv6 ? addr_ipv6 : addr_ipv4;
  1091. } else {
  1092. addr_best = addr_ipv4 ? addr_ipv4 : addr_ipv6;
  1093. }
  1094. /* Now convert it to the ugly old interface */
  1095. if (! addr_best) {
  1096. connection_ap_handshake_socks_resolved(conn,
  1097. RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
  1098. return;
  1099. }
  1100. connection_ap_handshake_socks_resolved_addr(conn,
  1101. &addr_best->addr,
  1102. addr_best->ttl,
  1103. -1);
  1104. remap_event_helper(conn, &addr_best->addr);
  1105. }
  1106. /** Handle a RELAY_COMMAND_RESOLVED cell that we received on a non-open AP
  1107. * stream. */
  1108. STATIC int
  1109. connection_edge_process_resolved_cell(edge_connection_t *conn,
  1110. const cell_t *cell,
  1111. const relay_header_t *rh)
  1112. {
  1113. entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
  1114. smartlist_t *resolved_addresses = NULL;
  1115. int errcode = 0;
  1116. if (conn->base_.state != AP_CONN_STATE_RESOLVE_WAIT) {
  1117. log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
  1118. "not in state resolve_wait. Dropping.");
  1119. return 0;
  1120. }
  1121. tor_assert(SOCKS_COMMAND_IS_RESOLVE(entry_conn->socks_request->command));
  1122. resolved_addresses = smartlist_new();
  1123. if (resolved_cell_parse(cell, rh, resolved_addresses, &errcode)) {
  1124. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1125. "Dropping malformed 'resolved' cell");
  1126. connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
  1127. goto done;
  1128. }
  1129. if (get_options()->ClientDNSRejectInternalAddresses) {
  1130. int orig_len = smartlist_len(resolved_addresses);
  1131. SMARTLIST_FOREACH_BEGIN(resolved_addresses, address_ttl_t *, addr) {
  1132. if (addr->hostname == NULL && tor_addr_is_internal(&addr->addr, 0)) {
  1133. log_info(LD_APP, "Got a resolved cell with answer %s; dropping that "
  1134. "answer.",
  1135. safe_str_client(fmt_addr(&addr->addr)));
  1136. address_ttl_free(addr);
  1137. SMARTLIST_DEL_CURRENT(resolved_addresses, addr);
  1138. }
  1139. } SMARTLIST_FOREACH_END(addr);
  1140. if (orig_len && smartlist_len(resolved_addresses) == 0) {
  1141. log_info(LD_APP, "Got a resolved cell with only private addresses; "
  1142. "dropping it.");
  1143. connection_ap_handshake_socks_resolved(entry_conn,
  1144. RESOLVED_TYPE_ERROR_TRANSIENT,
  1145. 0, NULL, 0, TIME_MAX);
  1146. connection_mark_unattached_ap(entry_conn,
  1147. END_STREAM_REASON_TORPROTOCOL);
  1148. goto done;
  1149. }
  1150. }
  1151. /* This is valid data at this point. Count it */
  1152. if (conn->on_circuit && CIRCUIT_IS_ORIGIN(conn->on_circuit)) {
  1153. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(conn->on_circuit),
  1154. rh->length);
  1155. }
  1156. connection_ap_handshake_socks_got_resolved_cell(entry_conn,
  1157. errcode,
  1158. resolved_addresses);
  1159. connection_mark_unattached_ap(entry_conn,
  1160. END_STREAM_REASON_DONE |
  1161. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  1162. done:
  1163. SMARTLIST_FOREACH(resolved_addresses, address_ttl_t *, addr,
  1164. address_ttl_free(addr));
  1165. smartlist_free(resolved_addresses);
  1166. return 0;
  1167. }
  1168. /** An incoming relay cell has arrived from circuit <b>circ</b> to
  1169. * stream <b>conn</b>.
  1170. *
  1171. * The arguments here are the same as in
  1172. * connection_edge_process_relay_cell() below; this function is called
  1173. * from there when <b>conn</b> is defined and not in an open state.
  1174. */
  1175. static int
  1176. connection_edge_process_relay_cell_not_open(
  1177. relay_header_t *rh, cell_t *cell, circuit_t *circ,
  1178. edge_connection_t *conn, crypt_path_t *layer_hint)
  1179. {
  1180. if (rh->command == RELAY_COMMAND_END) {
  1181. if (CIRCUIT_IS_ORIGIN(circ) && conn->base_.type == CONN_TYPE_AP) {
  1182. return connection_ap_process_end_not_open(rh, cell,
  1183. TO_ORIGIN_CIRCUIT(circ),
  1184. EDGE_TO_ENTRY_CONN(conn),
  1185. layer_hint);
  1186. } else {
  1187. /* we just got an 'end', don't need to send one */
  1188. conn->edge_has_sent_end = 1;
  1189. conn->end_reason = *(cell->payload+RELAY_HEADER_SIZE) |
  1190. END_STREAM_REASON_FLAG_REMOTE;
  1191. connection_mark_for_close(TO_CONN(conn));
  1192. return 0;
  1193. }
  1194. }
  1195. if (conn->base_.type == CONN_TYPE_AP &&
  1196. rh->command == RELAY_COMMAND_CONNECTED) {
  1197. tor_addr_t addr;
  1198. int ttl;
  1199. entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
  1200. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  1201. if (conn->base_.state != AP_CONN_STATE_CONNECT_WAIT) {
  1202. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1203. "Got 'connected' while not in state connect_wait. Dropping.");
  1204. return 0;
  1205. }
  1206. CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
  1207. conn->base_.state = AP_CONN_STATE_OPEN;
  1208. log_info(LD_APP,"'connected' received for circid %u streamid %d "
  1209. "after %d seconds.",
  1210. (unsigned)circ->n_circ_id,
  1211. rh->stream_id,
  1212. (int)(time(NULL) - conn->base_.timestamp_last_read_allowed));
  1213. if (connected_cell_parse(rh, cell, &addr, &ttl) < 0) {
  1214. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1215. "Got a badly formatted connected cell. Closing.");
  1216. connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  1217. connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
  1218. return 0;
  1219. }
  1220. if (tor_addr_family(&addr) != AF_UNSPEC) {
  1221. /* The family is not UNSPEC: so we were given an address in the
  1222. * connected cell. (This is normal, except for BEGINDIR and onion
  1223. * service streams.) */
  1224. const sa_family_t family = tor_addr_family(&addr);
  1225. if (tor_addr_is_null(&addr) ||
  1226. (get_options()->ClientDNSRejectInternalAddresses &&
  1227. tor_addr_is_internal(&addr, 0))) {
  1228. log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
  1229. fmt_addr(&addr));
  1230. connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  1231. connection_mark_unattached_ap(entry_conn,
  1232. END_STREAM_REASON_TORPROTOCOL);
  1233. return 0;
  1234. }
  1235. if ((family == AF_INET && ! entry_conn->entry_cfg.ipv4_traffic) ||
  1236. (family == AF_INET6 && ! entry_conn->entry_cfg.ipv6_traffic)) {
  1237. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1238. "Got a connected cell to %s with unsupported address family."
  1239. " Closing.", fmt_addr(&addr));
  1240. connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  1241. connection_mark_unattached_ap(entry_conn,
  1242. END_STREAM_REASON_TORPROTOCOL);
  1243. return 0;
  1244. }
  1245. client_dns_set_addressmap(entry_conn,
  1246. entry_conn->socks_request->address, &addr,
  1247. entry_conn->chosen_exit_name, ttl);
  1248. remap_event_helper(entry_conn, &addr);
  1249. }
  1250. circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
  1251. /* don't send a socks reply to transparent conns */
  1252. tor_assert(entry_conn->socks_request != NULL);
  1253. if (!entry_conn->socks_request->has_finished) {
  1254. connection_ap_handshake_socks_reply(entry_conn, NULL, 0, 0);
  1255. }
  1256. /* Was it a linked dir conn? If so, a dir request just started to
  1257. * fetch something; this could be a bootstrap status milestone. */
  1258. log_debug(LD_APP, "considering");
  1259. if (TO_CONN(conn)->linked_conn &&
  1260. TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
  1261. connection_t *dirconn = TO_CONN(conn)->linked_conn;
  1262. log_debug(LD_APP, "it is! %d", dirconn->purpose);
  1263. switch (dirconn->purpose) {
  1264. case DIR_PURPOSE_FETCH_CERTIFICATE:
  1265. if (consensus_is_waiting_for_certs())
  1266. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
  1267. break;
  1268. case DIR_PURPOSE_FETCH_CONSENSUS:
  1269. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
  1270. break;
  1271. case DIR_PURPOSE_FETCH_SERVERDESC:
  1272. case DIR_PURPOSE_FETCH_MICRODESC:
  1273. if (TO_DIR_CONN(dirconn)->router_purpose == ROUTER_PURPOSE_GENERAL)
  1274. control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
  1275. count_loading_descriptors_progress());
  1276. break;
  1277. }
  1278. }
  1279. /* This is definitely a success, so forget about any pending data we
  1280. * had sent. */
  1281. if (entry_conn->pending_optimistic_data) {
  1282. buf_free(entry_conn->pending_optimistic_data);
  1283. entry_conn->pending_optimistic_data = NULL;
  1284. }
  1285. /* This is valid data at this point. Count it */
  1286. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh->length);
  1287. /* handle anything that might have queued */
  1288. if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
  1289. /* (We already sent an end cell if possible) */
  1290. connection_mark_for_close(TO_CONN(conn));
  1291. return 0;
  1292. }
  1293. return 0;
  1294. }
  1295. if (conn->base_.type == CONN_TYPE_AP &&
  1296. rh->command == RELAY_COMMAND_RESOLVED) {
  1297. return connection_edge_process_resolved_cell(conn, cell, rh);
  1298. }
  1299. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1300. "Got an unexpected relay command %d, in state %d (%s). Dropping.",
  1301. rh->command, conn->base_.state,
  1302. conn_state_to_string(conn->base_.type, conn->base_.state));
  1303. return 0; /* for forward compatibility, don't kill the circuit */
  1304. // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  1305. // connection_mark_for_close(conn);
  1306. // return -1;
  1307. }
  1308. /** An incoming relay cell has arrived on circuit <b>circ</b>. If
  1309. * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
  1310. * destined for <b>conn</b>.
  1311. *
  1312. * If <b>layer_hint</b> is defined, then we're the origin of the
  1313. * circuit, and it specifies the hop that packaged <b>cell</b>.
  1314. *
  1315. * Return -reason if you want to warn and tear down the circuit, else 0.
  1316. */
  1317. STATIC int
  1318. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  1319. edge_connection_t *conn,
  1320. crypt_path_t *layer_hint)
  1321. {
  1322. static int num_seen=0;
  1323. relay_header_t rh;
  1324. unsigned domain = layer_hint?LD_APP:LD_EXIT;
  1325. int reason;
  1326. int optimistic_data = 0; /* Set to 1 if we receive data on a stream
  1327. * that's in the EXIT_CONN_STATE_RESOLVING
  1328. * or EXIT_CONN_STATE_CONNECTING states. */
  1329. tor_assert(cell);
  1330. tor_assert(circ);
  1331. relay_header_unpack(&rh, cell->payload);
  1332. // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
  1333. num_seen++;
  1334. log_debug(domain, "Now seen %d relay cells here (command %d, stream %d).",
  1335. num_seen, rh.command, rh.stream_id);
  1336. if (rh.length > RELAY_PAYLOAD_SIZE) {
  1337. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1338. "Relay cell length field too long. Closing circuit.");
  1339. return - END_CIRC_REASON_TORPROTOCOL;
  1340. }
  1341. if (rh.stream_id == 0) {
  1342. switch (rh.command) {
  1343. case RELAY_COMMAND_BEGIN:
  1344. case RELAY_COMMAND_CONNECTED:
  1345. case RELAY_COMMAND_END:
  1346. case RELAY_COMMAND_RESOLVE:
  1347. case RELAY_COMMAND_RESOLVED:
  1348. case RELAY_COMMAND_BEGIN_DIR:
  1349. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay command %d with zero "
  1350. "stream_id. Dropping.", (int)rh.command);
  1351. return 0;
  1352. default:
  1353. ;
  1354. }
  1355. }
  1356. /* either conn is NULL, in which case we've got a control cell, or else
  1357. * conn points to the recognized stream. */
  1358. if (conn && !connection_state_is_open(TO_CONN(conn))) {
  1359. if (conn->base_.type == CONN_TYPE_EXIT &&
  1360. (conn->base_.state == EXIT_CONN_STATE_CONNECTING ||
  1361. conn->base_.state == EXIT_CONN_STATE_RESOLVING) &&
  1362. rh.command == RELAY_COMMAND_DATA) {
  1363. /* Allow DATA cells to be delivered to an exit node in state
  1364. * EXIT_CONN_STATE_CONNECTING or EXIT_CONN_STATE_RESOLVING.
  1365. * This speeds up HTTP, for example. */
  1366. optimistic_data = 1;
  1367. } else if (rh.stream_id == 0 && rh.command == RELAY_COMMAND_DATA) {
  1368. log_warn(LD_BUG, "Somehow I had a connection that matched a "
  1369. "data cell with stream ID 0.");
  1370. } else {
  1371. return connection_edge_process_relay_cell_not_open(
  1372. &rh, cell, circ, conn, layer_hint);
  1373. }
  1374. }
  1375. switch (rh.command) {
  1376. case RELAY_COMMAND_DROP:
  1377. rep_hist_padding_count_read(PADDING_TYPE_DROP);
  1378. // log_info(domain,"Got a relay-level padding cell. Dropping.");
  1379. return 0;
  1380. case RELAY_COMMAND_BEGIN:
  1381. case RELAY_COMMAND_BEGIN_DIR:
  1382. if (layer_hint &&
  1383. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
  1384. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1385. "Relay begin request unsupported at AP. Dropping.");
  1386. return 0;
  1387. }
  1388. if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED &&
  1389. layer_hint != TO_ORIGIN_CIRCUIT(circ)->cpath->prev) {
  1390. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1391. "Relay begin request to Hidden Service "
  1392. "from intermediary node. Dropping.");
  1393. return 0;
  1394. }
  1395. if (conn) {
  1396. log_fn(LOG_PROTOCOL_WARN, domain,
  1397. "Begin cell for known stream. Dropping.");
  1398. return 0;
  1399. }
  1400. if (rh.command == RELAY_COMMAND_BEGIN_DIR &&
  1401. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
  1402. /* Assign this circuit and its app-ward OR connection a unique ID,
  1403. * so that we can measure download times. The local edge and dir
  1404. * connection will be assigned the same ID when they are created
  1405. * and linked. */
  1406. static uint64_t next_id = 0;
  1407. circ->dirreq_id = ++next_id;
  1408. TO_OR_CIRCUIT(circ)->p_chan->dirreq_id = circ->dirreq_id;
  1409. }
  1410. return connection_exit_begin_conn(cell, circ);
  1411. case RELAY_COMMAND_DATA:
  1412. ++stats_n_data_cells_received;
  1413. if (( layer_hint && --layer_hint->deliver_window < 0) ||
  1414. (!layer_hint && --circ->deliver_window < 0)) {
  1415. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1416. "(relay data) circ deliver_window below 0. Killing.");
  1417. if (conn) {
  1418. /* XXXX Do we actually need to do this? Will killing the circuit
  1419. * not send an END and mark the stream for close as appropriate? */
  1420. connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  1421. connection_mark_for_close(TO_CONN(conn));
  1422. }
  1423. return -END_CIRC_REASON_TORPROTOCOL;
  1424. }
  1425. log_debug(domain,"circ deliver_window now %d.", layer_hint ?
  1426. layer_hint->deliver_window : circ->deliver_window);
  1427. circuit_consider_sending_sendme(circ, layer_hint);
  1428. if (rh.stream_id == 0) {
  1429. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay data cell with zero "
  1430. "stream_id. Dropping.");
  1431. return 0;
  1432. } else if (!conn) {
  1433. log_info(domain,"data cell dropped, unknown stream (streamid %d).",
  1434. rh.stream_id);
  1435. return 0;
  1436. }
  1437. if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  1438. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1439. "(relay data) conn deliver_window below 0. Killing.");
  1440. return -END_CIRC_REASON_TORPROTOCOL;
  1441. }
  1442. /* Total all valid application bytes delivered */
  1443. if (CIRCUIT_IS_ORIGIN(circ) && rh.length > 0) {
  1444. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  1445. }
  1446. stats_n_data_bytes_received += rh.length;
  1447. connection_buf_add((char*)(cell->payload + RELAY_HEADER_SIZE),
  1448. rh.length, TO_CONN(conn));
  1449. #ifdef MEASUREMENTS_21206
  1450. /* Count number of RELAY_DATA cells received on a linked directory
  1451. * connection. */
  1452. connection_t *linked_conn = TO_CONN(conn)->linked_conn;
  1453. if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
  1454. ++(TO_DIR_CONN(linked_conn)->data_cells_received);
  1455. }
  1456. #endif /* defined(MEASUREMENTS_21206) */
  1457. if (!optimistic_data) {
  1458. /* Only send a SENDME if we're not getting optimistic data; otherwise
  1459. * a SENDME could arrive before the CONNECTED.
  1460. */
  1461. connection_edge_consider_sending_sendme(conn);
  1462. }
  1463. return 0;
  1464. case RELAY_COMMAND_END:
  1465. reason = rh.length > 0 ?
  1466. get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
  1467. if (!conn) {
  1468. log_info(domain,"end cell (%s) dropped, unknown stream.",
  1469. stream_end_reason_to_string(reason));
  1470. return 0;
  1471. }
  1472. /* XXX add to this log_fn the exit node's nickname? */
  1473. log_info(domain,TOR_SOCKET_T_FORMAT": end cell (%s) for stream %d. "
  1474. "Removing stream.",
  1475. conn->base_.s,
  1476. stream_end_reason_to_string(reason),
  1477. conn->stream_id);
  1478. if (conn->base_.type == CONN_TYPE_AP) {
  1479. entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
  1480. if (entry_conn->socks_request &&
  1481. !entry_conn->socks_request->has_finished)
  1482. log_warn(LD_BUG,
  1483. "open stream hasn't sent socks answer yet? Closing.");
  1484. }
  1485. /* We just *got* an end; no reason to send one. */
  1486. conn->edge_has_sent_end = 1;
  1487. if (!conn->end_reason)
  1488. conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
  1489. if (!conn->base_.marked_for_close) {
  1490. /* only mark it if not already marked. it's possible to
  1491. * get the 'end' right around when the client hangs up on us. */
  1492. connection_mark_and_flush(TO_CONN(conn));
  1493. /* Total all valid application bytes delivered */
  1494. if (CIRCUIT_IS_ORIGIN(circ)) {
  1495. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  1496. }
  1497. }
  1498. return 0;
  1499. case RELAY_COMMAND_EXTEND:
  1500. case RELAY_COMMAND_EXTEND2: {
  1501. static uint64_t total_n_extend=0, total_nonearly=0;
  1502. total_n_extend++;
  1503. if (rh.stream_id) {
  1504. log_fn(LOG_PROTOCOL_WARN, domain,
  1505. "'extend' cell received for non-zero stream. Dropping.");
  1506. return 0;
  1507. }
  1508. if (cell->command != CELL_RELAY_EARLY &&
  1509. !networkstatus_get_param(NULL,"AllowNonearlyExtend",0,0,1)) {
  1510. #define EARLY_WARNING_INTERVAL 3600
  1511. static ratelim_t early_warning_limit =
  1512. RATELIM_INIT(EARLY_WARNING_INTERVAL);
  1513. char *m;
  1514. if (cell->command == CELL_RELAY) {
  1515. ++total_nonearly;
  1516. if ((m = rate_limit_log(&early_warning_limit, approx_time()))) {
  1517. double percentage = ((double)total_nonearly)/total_n_extend;
  1518. percentage *= 100;
  1519. log_fn(LOG_PROTOCOL_WARN, domain, "EXTEND cell received, "
  1520. "but not via RELAY_EARLY. Dropping.%s", m);
  1521. log_fn(LOG_PROTOCOL_WARN, domain, " (We have dropped %.02f%% of "
  1522. "all EXTEND cells for this reason)", percentage);
  1523. tor_free(m);
  1524. }
  1525. } else {
  1526. log_fn(LOG_WARN, domain,
  1527. "EXTEND cell received, in a cell with type %d! Dropping.",
  1528. cell->command);
  1529. }
  1530. return 0;
  1531. }
  1532. return circuit_extend(cell, circ);
  1533. }
  1534. case RELAY_COMMAND_EXTENDED:
  1535. case RELAY_COMMAND_EXTENDED2:
  1536. if (!layer_hint) {
  1537. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1538. "'extended' unsupported at non-origin. Dropping.");
  1539. return 0;
  1540. }
  1541. log_debug(domain,"Got an extended cell! Yay.");
  1542. {
  1543. extended_cell_t extended_cell;
  1544. if (extended_cell_parse(&extended_cell, rh.command,
  1545. (const uint8_t*)cell->payload+RELAY_HEADER_SIZE,
  1546. rh.length)<0) {
  1547. log_warn(LD_PROTOCOL,
  1548. "Can't parse EXTENDED cell; killing circuit.");
  1549. return -END_CIRC_REASON_TORPROTOCOL;
  1550. }
  1551. if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
  1552. &extended_cell.created_cell)) < 0) {
  1553. circuit_mark_for_close(circ, -reason);
  1554. return 0; /* We don't want to cause a warning, so we mark the circuit
  1555. * here. */
  1556. }
  1557. }
  1558. if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
  1559. log_info(domain,"circuit_send_next_onion_skin() failed.");
  1560. return reason;
  1561. }
  1562. /* Total all valid bytes delivered. */
  1563. if (CIRCUIT_IS_ORIGIN(circ)) {
  1564. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
  1565. }
  1566. return 0;
  1567. case RELAY_COMMAND_TRUNCATE:
  1568. if (layer_hint) {
  1569. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1570. "'truncate' unsupported at origin. Dropping.");
  1571. return 0;
  1572. }
  1573. if (circ->n_hop) {
  1574. if (circ->n_chan)
  1575. log_warn(LD_BUG, "n_chan and n_hop set on the same circuit!");
  1576. extend_info_free(circ->n_hop);
  1577. circ->n_hop = NULL;
  1578. tor_free(circ->n_chan_create_cell);
  1579. circuit_set_state(circ, CIRCUIT_STATE_OPEN);
  1580. }
  1581. if (circ->n_chan) {
  1582. uint8_t trunc_reason = get_uint8(cell->payload + RELAY_HEADER_SIZE);
  1583. circuit_clear_cell_queue(circ, circ->n_chan);
  1584. channel_send_destroy(circ->n_circ_id, circ->n_chan,
  1585. trunc_reason);
  1586. circuit_set_n_circid_chan(circ, 0, NULL);
  1587. }
  1588. log_debug(LD_EXIT, "Processed 'truncate', replying.");
  1589. {
  1590. char payload[1];
  1591. payload[0] = (char)END_CIRC_REASON_REQUESTED;
  1592. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  1593. payload, sizeof(payload), NULL);
  1594. }
  1595. return 0;
  1596. case RELAY_COMMAND_TRUNCATED:
  1597. if (!layer_hint) {
  1598. log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
  1599. "'truncated' unsupported at non-origin. Dropping.");
  1600. return 0;
  1601. }
  1602. circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint,
  1603. get_uint8(cell->payload + RELAY_HEADER_SIZE));
  1604. return 0;
  1605. case RELAY_COMMAND_CONNECTED:
  1606. if (conn) {
  1607. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1608. "'connected' unsupported while open. Closing circ.");
  1609. return -END_CIRC_REASON_TORPROTOCOL;
  1610. }
  1611. log_info(domain,
  1612. "'connected' received on circid %u for streamid %d, "
  1613. "no conn attached anymore. Ignoring.",
  1614. (unsigned)circ->n_circ_id, rh.stream_id);
  1615. return 0;
  1616. case RELAY_COMMAND_SENDME:
  1617. if (!rh.stream_id) {
  1618. if (layer_hint) {
  1619. if (layer_hint->package_window + CIRCWINDOW_INCREMENT >
  1620. CIRCWINDOW_START_MAX) {
  1621. static struct ratelim_t exit_warn_ratelim = RATELIM_INIT(600);
  1622. log_fn_ratelim(&exit_warn_ratelim, LOG_WARN, LD_PROTOCOL,
  1623. "Unexpected sendme cell from exit relay. "
  1624. "Closing circ.");
  1625. return -END_CIRC_REASON_TORPROTOCOL;
  1626. }
  1627. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  1628. log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
  1629. layer_hint->package_window);
  1630. circuit_resume_edge_reading(circ, layer_hint);
  1631. /* We count circuit-level sendme's as valid delivered data because
  1632. * they are rate limited.
  1633. */
  1634. if (CIRCUIT_IS_ORIGIN(circ)) {
  1635. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ),
  1636. rh.length);
  1637. }
  1638. } else {
  1639. if (circ->package_window + CIRCWINDOW_INCREMENT >
  1640. CIRCWINDOW_START_MAX) {
  1641. static struct ratelim_t client_warn_ratelim = RATELIM_INIT(600);
  1642. log_fn_ratelim(&client_warn_ratelim,LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1643. "Unexpected sendme cell from client. "
  1644. "Closing circ (window %d).",
  1645. circ->package_window);
  1646. return -END_CIRC_REASON_TORPROTOCOL;
  1647. }
  1648. circ->package_window += CIRCWINDOW_INCREMENT;
  1649. log_debug(LD_APP,
  1650. "circ-level sendme at non-origin, packagewindow %d.",
  1651. circ->package_window);
  1652. circuit_resume_edge_reading(circ, layer_hint);
  1653. }
  1654. return 0;
  1655. }
  1656. if (!conn) {
  1657. log_info(domain,"sendme cell dropped, unknown stream (streamid %d).",
  1658. rh.stream_id);
  1659. return 0;
  1660. }
  1661. /* Don't allow the other endpoint to request more than our maximum
  1662. * (i.e. initial) stream SENDME window worth of data. Well-behaved
  1663. * stock clients will not request more than this max (as per the check
  1664. * in the while loop of connection_edge_consider_sending_sendme()).
  1665. */
  1666. if (conn->package_window + STREAMWINDOW_INCREMENT >
  1667. STREAMWINDOW_START_MAX) {
  1668. static struct ratelim_t stream_warn_ratelim = RATELIM_INIT(600);
  1669. log_fn_ratelim(&stream_warn_ratelim, LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1670. "Unexpected stream sendme cell. Closing circ (window %d).",
  1671. conn->package_window);
  1672. return -END_CIRC_REASON_TORPROTOCOL;
  1673. }
  1674. /* At this point, the stream sendme is valid */
  1675. if (CIRCUIT_IS_ORIGIN(circ)) {
  1676. circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ),
  1677. rh.length);
  1678. }
  1679. conn->package_window += STREAMWINDOW_INCREMENT;
  1680. log_debug(domain,"stream-level sendme, packagewindow now %d.",
  1681. conn->package_window);
  1682. if (circuit_queue_streams_are_blocked(circ)) {
  1683. /* Still waiting for queue to flush; don't touch conn */
  1684. return 0;
  1685. }
  1686. connection_start_reading(TO_CONN(conn));
  1687. /* handle whatever might still be on the inbuf */
  1688. if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
  1689. /* (We already sent an end cell if possible) */
  1690. connection_mark_for_close(TO_CONN(conn));
  1691. return 0;
  1692. }
  1693. return 0;
  1694. case RELAY_COMMAND_RESOLVE:
  1695. if (layer_hint) {
  1696. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1697. "resolve request unsupported at AP; dropping.");
  1698. return 0;
  1699. } else if (conn) {
  1700. log_fn(LOG_PROTOCOL_WARN, domain,
  1701. "resolve request for known stream; dropping.");
  1702. return 0;
  1703. } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
  1704. log_fn(LOG_PROTOCOL_WARN, domain,
  1705. "resolve request on circ with purpose %d; dropping",
  1706. circ->purpose);
  1707. return 0;
  1708. }
  1709. connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
  1710. return 0;
  1711. case RELAY_COMMAND_RESOLVED:
  1712. if (conn) {
  1713. log_fn(LOG_PROTOCOL_WARN, domain,
  1714. "'resolved' unsupported while open. Closing circ.");
  1715. return -END_CIRC_REASON_TORPROTOCOL;
  1716. }
  1717. log_info(domain,
  1718. "'resolved' received, no conn attached anymore. Ignoring.");
  1719. return 0;
  1720. case RELAY_COMMAND_ESTABLISH_INTRO:
  1721. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  1722. case RELAY_COMMAND_INTRODUCE1:
  1723. case RELAY_COMMAND_INTRODUCE2:
  1724. case RELAY_COMMAND_INTRODUCE_ACK:
  1725. case RELAY_COMMAND_RENDEZVOUS1:
  1726. case RELAY_COMMAND_RENDEZVOUS2:
  1727. case RELAY_COMMAND_INTRO_ESTABLISHED:
  1728. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  1729. rend_process_relay_cell(circ, layer_hint,
  1730. rh.command, rh.length,
  1731. cell->payload+RELAY_HEADER_SIZE);
  1732. return 0;
  1733. }
  1734. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1735. "Received unknown relay command %d. Perhaps the other side is using "
  1736. "a newer version of Tor? Dropping.",
  1737. rh.command);
  1738. return 0; /* for forward compatibility, don't kill the circuit */
  1739. }
  1740. /** How many relay_data cells have we built, ever? */
  1741. uint64_t stats_n_data_cells_packaged = 0;
  1742. /** How many bytes of data have we put in relay_data cells have we built,
  1743. * ever? This would be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if
  1744. * every relay cell we ever sent were completely full of data. */
  1745. uint64_t stats_n_data_bytes_packaged = 0;
  1746. /** How many relay_data cells have we received, ever? */
  1747. uint64_t stats_n_data_cells_received = 0;
  1748. /** How many bytes of data have we received relay_data cells, ever? This would
  1749. * be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if every relay cell we
  1750. * ever received were completely full of data. */
  1751. uint64_t stats_n_data_bytes_received = 0;
  1752. /** If <b>conn</b> has an entire relay payload of bytes on its inbuf (or
  1753. * <b>package_partial</b> is true), and the appropriate package windows aren't
  1754. * empty, grab a cell and send it down the circuit.
  1755. *
  1756. * If *<b>max_cells</b> is given, package no more than max_cells. Decrement
  1757. * *<b>max_cells</b> by the number of cells packaged.
  1758. *
  1759. * Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should
  1760. * be marked for close, else return 0.
  1761. */
  1762. int
  1763. connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial,
  1764. int *max_cells)
  1765. {
  1766. size_t bytes_to_process, length;
  1767. char payload[CELL_PAYLOAD_SIZE];
  1768. circuit_t *circ;
  1769. const unsigned domain = conn->base_.type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
  1770. int sending_from_optimistic = 0;
  1771. entry_connection_t *entry_conn =
  1772. conn->base_.type == CONN_TYPE_AP ? EDGE_TO_ENTRY_CONN(conn) : NULL;
  1773. const int sending_optimistically =
  1774. entry_conn &&
  1775. conn->base_.type == CONN_TYPE_AP &&
  1776. conn->base_.state != AP_CONN_STATE_OPEN;
  1777. crypt_path_t *cpath_layer = conn->cpath_layer;
  1778. tor_assert(conn);
  1779. if (conn->base_.marked_for_close) {
  1780. log_warn(LD_BUG,
  1781. "called on conn that's already marked for close at %s:%d.",
  1782. conn->base_.marked_for_close_file, conn->base_.marked_for_close);
  1783. return 0;
  1784. }
  1785. if (max_cells && *max_cells <= 0)
  1786. return 0;
  1787. repeat_connection_edge_package_raw_inbuf:
  1788. circ = circuit_get_by_edge_conn(conn);
  1789. if (!circ) {
  1790. log_info(domain,"conn has no circuit! Closing.");
  1791. conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
  1792. return -1;
  1793. }
  1794. if (circuit_consider_stop_edge_reading(circ, cpath_layer))
  1795. return 0;
  1796. if (conn->package_window <= 0) {
  1797. log_info(domain,"called with package_window %d. Skipping.",
  1798. conn->package_window);
  1799. connection_stop_reading(TO_CONN(conn));
  1800. return 0;
  1801. }
  1802. sending_from_optimistic = entry_conn &&
  1803. entry_conn->sending_optimistic_data != NULL;
  1804. if (PREDICT_UNLIKELY(sending_from_optimistic)) {
  1805. bytes_to_process = buf_datalen(entry_conn->sending_optimistic_data);
  1806. if (PREDICT_UNLIKELY(!bytes_to_process)) {
  1807. log_warn(LD_BUG, "sending_optimistic_data was non-NULL but empty");
  1808. bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
  1809. sending_from_optimistic = 0;
  1810. }
  1811. } else {
  1812. bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
  1813. }
  1814. if (!bytes_to_process)
  1815. return 0;
  1816. if (!package_partial && bytes_to_process < RELAY_PAYLOAD_SIZE)
  1817. return 0;
  1818. if (bytes_to_process > RELAY_PAYLOAD_SIZE) {
  1819. length = RELAY_PAYLOAD_SIZE;
  1820. } else {
  1821. length = bytes_to_process;
  1822. }
  1823. stats_n_data_bytes_packaged += length;
  1824. stats_n_data_cells_packaged += 1;
  1825. if (PREDICT_UNLIKELY(sending_from_optimistic)) {
  1826. /* XXXX We could be more efficient here by sometimes packing
  1827. * previously-sent optimistic data in the same cell with data
  1828. * from the inbuf. */
  1829. buf_get_bytes(entry_conn->sending_optimistic_data, payload, length);
  1830. if (!buf_datalen(entry_conn->sending_optimistic_data)) {
  1831. buf_free(entry_conn->sending_optimistic_data);
  1832. entry_conn->sending_optimistic_data = NULL;
  1833. }
  1834. } else {
  1835. connection_buf_get_bytes(payload, length, TO_CONN(conn));
  1836. }
  1837. log_debug(domain,TOR_SOCKET_T_FORMAT": Packaging %d bytes (%d waiting).",
  1838. conn->base_.s,
  1839. (int)length, (int)connection_get_inbuf_len(TO_CONN(conn)));
  1840. if (sending_optimistically && !sending_from_optimistic) {
  1841. /* This is new optimistic data; remember it in case we need to detach and
  1842. retry */
  1843. if (!entry_conn->pending_optimistic_data)
  1844. entry_conn->pending_optimistic_data = buf_new();
  1845. buf_add(entry_conn->pending_optimistic_data, payload, length);
  1846. }
  1847. if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
  1848. payload, length) < 0 )
  1849. /* circuit got marked for close, don't continue, don't need to mark conn */
  1850. return 0;
  1851. if (!cpath_layer) { /* non-rendezvous exit */
  1852. tor_assert(circ->package_window > 0);
  1853. circ->package_window--;
  1854. } else { /* we're an AP, or an exit on a rendezvous circ */
  1855. tor_assert(cpath_layer->package_window > 0);
  1856. cpath_layer->package_window--;
  1857. }
  1858. if (--conn->package_window <= 0) { /* is it 0 after decrement? */
  1859. connection_stop_reading(TO_CONN(conn));
  1860. log_debug(domain,"conn->package_window reached 0.");
  1861. circuit_consider_stop_edge_reading(circ, cpath_layer);
  1862. return 0; /* don't process the inbuf any more */
  1863. }
  1864. log_debug(domain,"conn->package_window is now %d",conn->package_window);
  1865. if (max_cells) {
  1866. *max_cells -= 1;
  1867. if (*max_cells <= 0)
  1868. return 0;
  1869. }
  1870. /* handle more if there's more, or return 0 if there isn't */
  1871. goto repeat_connection_edge_package_raw_inbuf;
  1872. }
  1873. /** Called when we've just received a relay data cell, when
  1874. * we've just finished flushing all bytes to stream <b>conn</b>,
  1875. * or when we've flushed *some* bytes to the stream <b>conn</b>.
  1876. *
  1877. * If conn->outbuf is not too full, and our deliver window is
  1878. * low, send back a suitable number of stream-level sendme cells.
  1879. */
  1880. void
  1881. connection_edge_consider_sending_sendme(edge_connection_t *conn)
  1882. {
  1883. circuit_t *circ;
  1884. if (connection_outbuf_too_full(TO_CONN(conn)))
  1885. return;
  1886. circ = circuit_get_by_edge_conn(conn);
  1887. if (!circ) {
  1888. /* this can legitimately happen if the destroy has already
  1889. * arrived and torn down the circuit */
  1890. log_info(LD_APP,"No circuit associated with conn. Skipping.");
  1891. return;
  1892. }
  1893. while (conn->deliver_window <= STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  1894. log_debug(conn->base_.type == CONN_TYPE_AP ?LD_APP:LD_EXIT,
  1895. "Outbuf %d, Queuing stream sendme.",
  1896. (int)conn->base_.outbuf_flushlen);
  1897. conn->deliver_window += STREAMWINDOW_INCREMENT;
  1898. if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
  1899. NULL, 0) < 0) {
  1900. log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
  1901. return; /* the circuit's closed, don't continue */
  1902. }
  1903. }
  1904. }
  1905. /** The circuit <b>circ</b> has received a circuit-level sendme
  1906. * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
  1907. * attached streams and let them resume reading and packaging, if
  1908. * their stream windows allow it.
  1909. */
  1910. static void
  1911. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  1912. {
  1913. if (circuit_queue_streams_are_blocked(circ)) {
  1914. log_debug(layer_hint?LD_APP:LD_EXIT,"Too big queue, no resuming");
  1915. return;
  1916. }
  1917. log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
  1918. if (CIRCUIT_IS_ORIGIN(circ))
  1919. circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
  1920. circ, layer_hint);
  1921. else
  1922. circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
  1923. circ, layer_hint);
  1924. }
  1925. void
  1926. stream_choice_seed_weak_rng(void)
  1927. {
  1928. crypto_seed_weak_rng(&stream_choice_rng);
  1929. }
  1930. /** A helper function for circuit_resume_edge_reading() above.
  1931. * The arguments are the same, except that <b>conn</b> is the head
  1932. * of a linked list of edge streams that should each be considered.
  1933. */
  1934. static int
  1935. circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
  1936. circuit_t *circ,
  1937. crypt_path_t *layer_hint)
  1938. {
  1939. edge_connection_t *conn;
  1940. int n_packaging_streams, n_streams_left;
  1941. int packaged_this_round;
  1942. int cells_on_queue;
  1943. int cells_per_conn;
  1944. edge_connection_t *chosen_stream = NULL;
  1945. int max_to_package;
  1946. if (first_conn == NULL) {
  1947. /* Don't bother to try to do the rest of this if there are no connections
  1948. * to resume. */
  1949. return 0;
  1950. }
  1951. /* How many cells do we have space for? It will be the minimum of
  1952. * the number needed to exhaust the package window, and the minimum
  1953. * needed to fill the cell queue. */
  1954. max_to_package = circ->package_window;
  1955. if (CIRCUIT_IS_ORIGIN(circ)) {
  1956. cells_on_queue = circ->n_chan_cells.n;
  1957. } else {
  1958. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1959. cells_on_queue = or_circ->p_chan_cells.n;
  1960. }
  1961. if (CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue < max_to_package)
  1962. max_to_package = CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue;
  1963. /* Once we used to start listening on the streams in the order they
  1964. * appeared in the linked list. That leads to starvation on the
  1965. * streams that appeared later on the list, since the first streams
  1966. * would always get to read first. Instead, we just pick a random
  1967. * stream on the list, and enable reading for streams starting at that
  1968. * point (and wrapping around as if the list were circular). It would
  1969. * probably be better to actually remember which streams we've
  1970. * serviced in the past, but this is simple and effective. */
  1971. /* Select a stream uniformly at random from the linked list. We
  1972. * don't need cryptographic randomness here. */
  1973. {
  1974. int num_streams = 0;
  1975. for (conn = first_conn; conn; conn = conn->next_stream) {
  1976. num_streams++;
  1977. if (tor_weak_random_one_in_n(&stream_choice_rng, num_streams)) {
  1978. chosen_stream = conn;
  1979. }
  1980. /* Invariant: chosen_stream has been chosen uniformly at random from
  1981. * among the first num_streams streams on first_conn.
  1982. *
  1983. * (Note that we iterate over every stream on the circuit, so that after
  1984. * we've considered the first stream, we've chosen it with P=1; and
  1985. * after we consider the second stream, we've switched to it with P=1/2
  1986. * and stayed with the first stream with P=1/2; and after we've
  1987. * considered the third stream, we've switched to it with P=1/3 and
  1988. * remained with one of the first two streams with P=(2/3), giving each
  1989. * one P=(1/2)(2/3) )=(1/3).) */
  1990. }
  1991. }
  1992. /* Count how many non-marked streams there are that have anything on
  1993. * their inbuf, and enable reading on all of the connections. */
  1994. n_packaging_streams = 0;
  1995. /* Activate reading starting from the chosen stream */
  1996. for (conn=chosen_stream; conn; conn = conn->next_stream) {
  1997. /* Start reading for the streams starting from here */
  1998. if (conn->base_.marked_for_close || conn->package_window <= 0)
  1999. continue;
  2000. if (!layer_hint || conn->cpath_layer == layer_hint) {
  2001. connection_start_reading(TO_CONN(conn));
  2002. if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
  2003. ++n_packaging_streams;
  2004. }
  2005. }
  2006. /* Go back and do the ones we skipped, circular-style */
  2007. for (conn = first_conn; conn != chosen_stream; conn = conn->next_stream) {
  2008. if (conn->base_.marked_for_close || conn->package_window <= 0)
  2009. continue;
  2010. if (!layer_hint || conn->cpath_layer == layer_hint) {
  2011. connection_start_reading(TO_CONN(conn));
  2012. if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
  2013. ++n_packaging_streams;
  2014. }
  2015. }
  2016. if (n_packaging_streams == 0) /* avoid divide-by-zero */
  2017. return 0;
  2018. again:
  2019. cells_per_conn = CEIL_DIV(max_to_package, n_packaging_streams);
  2020. packaged_this_round = 0;
  2021. n_streams_left = 0;
  2022. /* Iterate over all connections. Package up to cells_per_conn cells on
  2023. * each. Update packaged_this_round with the total number of cells
  2024. * packaged, and n_streams_left with the number that still have data to
  2025. * package.
  2026. */
  2027. for (conn=first_conn; conn; conn=conn->next_stream) {
  2028. if (conn->base_.marked_for_close || conn->package_window <= 0)
  2029. continue;
  2030. if (!layer_hint || conn->cpath_layer == layer_hint) {
  2031. int n = cells_per_conn, r;
  2032. /* handle whatever might still be on the inbuf */
  2033. r = connection_edge_package_raw_inbuf(conn, 1, &n);
  2034. /* Note how many we packaged */
  2035. packaged_this_round += (cells_per_conn-n);
  2036. if (r<0) {
  2037. /* Problem while packaging. (We already sent an end cell if
  2038. * possible) */
  2039. connection_mark_for_close(TO_CONN(conn));
  2040. continue;
  2041. }
  2042. /* If there's still data to read, we'll be coming back to this stream. */
  2043. if (connection_get_inbuf_len(TO_CONN(conn)))
  2044. ++n_streams_left;
  2045. /* If the circuit won't accept any more data, return without looking
  2046. * at any more of the streams. Any connections that should be stopped
  2047. * have already been stopped by connection_edge_package_raw_inbuf. */
  2048. if (circuit_consider_stop_edge_reading(circ, layer_hint))
  2049. return -1;
  2050. /* XXXX should we also stop immediately if we fill up the cell queue?
  2051. * Probably. */
  2052. }
  2053. }
  2054. /* If we made progress, and we are willing to package more, and there are
  2055. * any streams left that want to package stuff... try again!
  2056. */
  2057. if (packaged_this_round && packaged_this_round < max_to_package &&
  2058. n_streams_left) {
  2059. max_to_package -= packaged_this_round;
  2060. n_packaging_streams = n_streams_left;
  2061. goto again;
  2062. }
  2063. return 0;
  2064. }
  2065. /** Check if the package window for <b>circ</b> is empty (at
  2066. * hop <b>layer_hint</b> if it's defined).
  2067. *
  2068. * If yes, tell edge streams to stop reading and return 1.
  2069. * Else return 0.
  2070. */
  2071. static int
  2072. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  2073. {
  2074. edge_connection_t *conn = NULL;
  2075. unsigned domain = layer_hint ? LD_APP : LD_EXIT;
  2076. if (!layer_hint) {
  2077. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  2078. log_debug(domain,"considering circ->package_window %d",
  2079. circ->package_window);
  2080. if (circ->package_window <= 0) {
  2081. log_debug(domain,"yes, not-at-origin. stopped.");
  2082. for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
  2083. connection_stop_reading(TO_CONN(conn));
  2084. return 1;
  2085. }
  2086. return 0;
  2087. }
  2088. /* else, layer hint is defined, use it */
  2089. log_debug(domain,"considering layer_hint->package_window %d",
  2090. layer_hint->package_window);
  2091. if (layer_hint->package_window <= 0) {
  2092. log_debug(domain,"yes, at-origin. stopped.");
  2093. for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
  2094. conn=conn->next_stream) {
  2095. if (conn->cpath_layer == layer_hint)
  2096. connection_stop_reading(TO_CONN(conn));
  2097. }
  2098. return 1;
  2099. }
  2100. return 0;
  2101. }
  2102. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  2103. * <b>layer_hint</b> if it's defined) is low enough that we should
  2104. * send a circuit-level sendme back down the circuit. If so, send
  2105. * enough sendmes that the window would be overfull if we sent any
  2106. * more.
  2107. */
  2108. static void
  2109. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
  2110. {
  2111. // log_fn(LOG_INFO,"Considering: layer_hint is %s",
  2112. // layer_hint ? "defined" : "null");
  2113. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <=
  2114. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  2115. log_debug(LD_CIRC,"Queuing circuit sendme.");
  2116. if (layer_hint)
  2117. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  2118. else
  2119. circ->deliver_window += CIRCWINDOW_INCREMENT;
  2120. if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
  2121. NULL, 0, layer_hint) < 0) {
  2122. log_warn(LD_CIRC,
  2123. "relay_send_command_from_edge failed. Circuit's closed.");
  2124. return; /* the circuit's closed, don't continue */
  2125. }
  2126. }
  2127. }
  2128. /** The total number of cells we have allocated. */
  2129. static size_t total_cells_allocated = 0;
  2130. /** Release storage held by <b>cell</b>. */
  2131. static inline void
  2132. packed_cell_free_unchecked(packed_cell_t *cell)
  2133. {
  2134. --total_cells_allocated;
  2135. tor_free(cell);
  2136. }
  2137. /** Allocate and return a new packed_cell_t. */
  2138. STATIC packed_cell_t *
  2139. packed_cell_new(void)
  2140. {
  2141. ++total_cells_allocated;
  2142. return tor_malloc_zero(sizeof(packed_cell_t));
  2143. }
  2144. /** Return a packed cell used outside by channel_t lower layer */
  2145. void
  2146. packed_cell_free_(packed_cell_t *cell)
  2147. {
  2148. if (!cell)
  2149. return;
  2150. packed_cell_free_unchecked(cell);
  2151. }
  2152. /** Log current statistics for cell pool allocation at log level
  2153. * <b>severity</b>. */
  2154. void
  2155. dump_cell_pool_usage(int severity)
  2156. {
  2157. int n_circs = 0;
  2158. int n_cells = 0;
  2159. SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c) {
  2160. n_cells += c->n_chan_cells.n;
  2161. if (!CIRCUIT_IS_ORIGIN(c))
  2162. n_cells += TO_OR_CIRCUIT(c)->p_chan_cells.n;
  2163. ++n_circs;
  2164. }
  2165. SMARTLIST_FOREACH_END(c);
  2166. tor_log(severity, LD_MM,
  2167. "%d cells allocated on %d circuits. %d cells leaked.",
  2168. n_cells, n_circs, (int)total_cells_allocated - n_cells);
  2169. }
  2170. /** Allocate a new copy of packed <b>cell</b>. */
  2171. static inline packed_cell_t *
  2172. packed_cell_copy(const cell_t *cell, int wide_circ_ids)
  2173. {
  2174. packed_cell_t *c = packed_cell_new();
  2175. cell_pack(c, cell, wide_circ_ids);
  2176. return c;
  2177. }
  2178. /** Append <b>cell</b> to the end of <b>queue</b>. */
  2179. void
  2180. cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
  2181. {
  2182. TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
  2183. ++queue->n;
  2184. }
  2185. /** Append a newly allocated copy of <b>cell</b> to the end of the
  2186. * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. If
  2187. * <b>use_stats</b> is true, record statistics about the cell.
  2188. */
  2189. void
  2190. cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
  2191. int exitward, const cell_t *cell,
  2192. int wide_circ_ids, int use_stats)
  2193. {
  2194. packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
  2195. (void)circ;
  2196. (void)exitward;
  2197. (void)use_stats;
  2198. copy->inserted_timestamp = monotime_coarse_get_stamp();
  2199. cell_queue_append(queue, copy);
  2200. }
  2201. /** Initialize <b>queue</b> as an empty cell queue. */
  2202. void
  2203. cell_queue_init(cell_queue_t *queue)
  2204. {
  2205. memset(queue, 0, sizeof(cell_queue_t));
  2206. TOR_SIMPLEQ_INIT(&queue->head);
  2207. }
  2208. /** Remove and free every cell in <b>queue</b>. */
  2209. void
  2210. cell_queue_clear(cell_queue_t *queue)
  2211. {
  2212. packed_cell_t *cell;
  2213. while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
  2214. TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
  2215. packed_cell_free_unchecked(cell);
  2216. }
  2217. TOR_SIMPLEQ_INIT(&queue->head);
  2218. queue->n = 0;
  2219. }
  2220. /** Extract and return the cell at the head of <b>queue</b>; return NULL if
  2221. * <b>queue</b> is empty. */
  2222. STATIC packed_cell_t *
  2223. cell_queue_pop(cell_queue_t *queue)
  2224. {
  2225. packed_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
  2226. if (!cell)
  2227. return NULL;
  2228. TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
  2229. --queue->n;
  2230. return cell;
  2231. }
  2232. /** Initialize <b>queue</b> as an empty cell queue. */
  2233. void
  2234. destroy_cell_queue_init(destroy_cell_queue_t *queue)
  2235. {
  2236. memset(queue, 0, sizeof(destroy_cell_queue_t));
  2237. TOR_SIMPLEQ_INIT(&queue->head);
  2238. }
  2239. /** Remove and free every cell in <b>queue</b>. */
  2240. void
  2241. destroy_cell_queue_clear(destroy_cell_queue_t *queue)
  2242. {
  2243. destroy_cell_t *cell;
  2244. while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
  2245. TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
  2246. tor_free(cell);
  2247. }
  2248. TOR_SIMPLEQ_INIT(&queue->head);
  2249. queue->n = 0;
  2250. }
  2251. /** Extract and return the cell at the head of <b>queue</b>; return NULL if
  2252. * <b>queue</b> is empty. */
  2253. STATIC destroy_cell_t *
  2254. destroy_cell_queue_pop(destroy_cell_queue_t *queue)
  2255. {
  2256. destroy_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
  2257. if (!cell)
  2258. return NULL;
  2259. TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
  2260. --queue->n;
  2261. return cell;
  2262. }
  2263. /** Append a destroy cell for <b>circid</b> to <b>queue</b>. */
  2264. void
  2265. destroy_cell_queue_append(destroy_cell_queue_t *queue,
  2266. circid_t circid,
  2267. uint8_t reason)
  2268. {
  2269. destroy_cell_t *cell = tor_malloc_zero(sizeof(destroy_cell_t));
  2270. cell->circid = circid;
  2271. cell->reason = reason;
  2272. /* Not yet used, but will be required for OOM handling. */
  2273. cell->inserted_timestamp = monotime_coarse_get_stamp();
  2274. TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
  2275. ++queue->n;
  2276. }
  2277. /** Convert a destroy_cell_t to a newly allocated cell_t. Frees its input. */
  2278. static packed_cell_t *
  2279. destroy_cell_to_packed_cell(destroy_cell_t *inp, int wide_circ_ids)
  2280. {
  2281. packed_cell_t *packed = packed_cell_new();
  2282. cell_t cell;
  2283. memset(&cell, 0, sizeof(cell));
  2284. cell.circ_id = inp->circid;
  2285. cell.command = CELL_DESTROY;
  2286. cell.payload[0] = inp->reason;
  2287. cell_pack(packed, &cell, wide_circ_ids);
  2288. tor_free(inp);
  2289. return packed;
  2290. }
  2291. /** Return the total number of bytes used for each packed_cell in a queue.
  2292. * Approximate. */
  2293. size_t
  2294. packed_cell_mem_cost(void)
  2295. {
  2296. return sizeof(packed_cell_t);
  2297. }
  2298. /* DOCDOC */
  2299. size_t
  2300. cell_queues_get_total_allocation(void)
  2301. {
  2302. return total_cells_allocated * packed_cell_mem_cost();
  2303. }
  2304. /** How long after we've been low on memory should we try to conserve it? */
  2305. #define MEMORY_PRESSURE_INTERVAL (30*60)
  2306. /** The time at which we were last low on memory. */
  2307. static time_t last_time_under_memory_pressure = 0;
  2308. /** Check whether we've got too much space used for cells. If so,
  2309. * call the OOM handler and return 1. Otherwise, return 0. */
  2310. STATIC int
  2311. cell_queues_check_size(void)
  2312. {
  2313. time_t now = time(NULL);
  2314. size_t alloc = cell_queues_get_total_allocation();
  2315. alloc += buf_get_total_allocation();
  2316. alloc += tor_compress_get_total_allocation();
  2317. const size_t rend_cache_total = rend_cache_get_total_allocation();
  2318. alloc += rend_cache_total;
  2319. const size_t geoip_client_cache_total =
  2320. geoip_client_cache_total_allocation();
  2321. alloc += geoip_client_cache_total;
  2322. if (alloc >= get_options()->MaxMemInQueues_low_threshold) {
  2323. last_time_under_memory_pressure = approx_time();
  2324. if (alloc >= get_options()->MaxMemInQueues) {
  2325. /* If we're spending over 20% of the memory limit on hidden service
  2326. * descriptors, free them until we're down to 10%. Do the same for geoip
  2327. * client cache. */
  2328. if (rend_cache_total > get_options()->MaxMemInQueues / 5) {
  2329. const size_t bytes_to_remove =
  2330. rend_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
  2331. alloc -= hs_cache_handle_oom(now, bytes_to_remove);
  2332. }
  2333. if (geoip_client_cache_total > get_options()->MaxMemInQueues / 5) {
  2334. const size_t bytes_to_remove =
  2335. geoip_client_cache_total -
  2336. (size_t)(get_options()->MaxMemInQueues / 10);
  2337. alloc -= geoip_client_cache_handle_oom(now, bytes_to_remove);
  2338. }
  2339. circuits_handle_oom(alloc);
  2340. return 1;
  2341. }
  2342. }
  2343. return 0;
  2344. }
  2345. /** Return true if we've been under memory pressure in the last
  2346. * MEMORY_PRESSURE_INTERVAL seconds. */
  2347. int
  2348. have_been_under_memory_pressure(void)
  2349. {
  2350. return last_time_under_memory_pressure + MEMORY_PRESSURE_INTERVAL
  2351. < approx_time();
  2352. }
  2353. /**
  2354. * Update the number of cells available on the circuit's n_chan or p_chan's
  2355. * circuit mux.
  2356. */
  2357. void
  2358. update_circuit_on_cmux_(circuit_t *circ, cell_direction_t direction,
  2359. const char *file, int lineno)
  2360. {
  2361. channel_t *chan = NULL;
  2362. or_circuit_t *or_circ = NULL;
  2363. circuitmux_t *cmux = NULL;
  2364. tor_assert(circ);
  2365. /* Okay, get the channel */
  2366. if (direction == CELL_DIRECTION_OUT) {
  2367. chan = circ->n_chan;
  2368. } else {
  2369. or_circ = TO_OR_CIRCUIT(circ);
  2370. chan = or_circ->p_chan;
  2371. }
  2372. tor_assert(chan);
  2373. tor_assert(chan->cmux);
  2374. /* Now get the cmux */
  2375. cmux = chan->cmux;
  2376. /* Cmux sanity check */
  2377. if (! circuitmux_is_circuit_attached(cmux, circ)) {
  2378. log_warn(LD_BUG, "called on non-attached circuit from %s:%d",
  2379. file, lineno);
  2380. return;
  2381. }
  2382. tor_assert(circuitmux_attached_circuit_direction(cmux, circ) == direction);
  2383. /* Update the number of cells we have for the circuit mux */
  2384. if (direction == CELL_DIRECTION_OUT) {
  2385. circuitmux_set_num_cells(cmux, circ, circ->n_chan_cells.n);
  2386. } else {
  2387. circuitmux_set_num_cells(cmux, circ, or_circ->p_chan_cells.n);
  2388. }
  2389. }
  2390. /** Remove all circuits from the cmux on <b>chan</b>.
  2391. *
  2392. * If <b>circuits_out</b> is non-NULL, add all detached circuits to
  2393. * <b>circuits_out</b>.
  2394. **/
  2395. void
  2396. channel_unlink_all_circuits(channel_t *chan, smartlist_t *circuits_out)
  2397. {
  2398. tor_assert(chan);
  2399. tor_assert(chan->cmux);
  2400. circuitmux_detach_all_circuits(chan->cmux, circuits_out);
  2401. chan->num_n_circuits = 0;
  2402. chan->num_p_circuits = 0;
  2403. }
  2404. /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
  2405. * every edge connection that is using <b>circ</b> to write to <b>chan</b>,
  2406. * and start or stop reading as appropriate.
  2407. *
  2408. * If <b>stream_id</b> is nonzero, block only the edge connection whose
  2409. * stream_id matches it.
  2410. *
  2411. * Returns the number of streams whose status we changed.
  2412. */
  2413. static int
  2414. set_streams_blocked_on_circ(circuit_t *circ, channel_t *chan,
  2415. int block, streamid_t stream_id)
  2416. {
  2417. edge_connection_t *edge = NULL;
  2418. int n = 0;
  2419. if (circ->n_chan == chan) {
  2420. circ->streams_blocked_on_n_chan = block;
  2421. if (CIRCUIT_IS_ORIGIN(circ))
  2422. edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
  2423. } else {
  2424. circ->streams_blocked_on_p_chan = block;
  2425. tor_assert(!CIRCUIT_IS_ORIGIN(circ));
  2426. edge = TO_OR_CIRCUIT(circ)->n_streams;
  2427. }
  2428. for (; edge; edge = edge->next_stream) {
  2429. connection_t *conn = TO_CONN(edge);
  2430. if (stream_id && edge->stream_id != stream_id)
  2431. continue;
  2432. if (edge->edge_blocked_on_circ != block) {
  2433. ++n;
  2434. edge->edge_blocked_on_circ = block;
  2435. }
  2436. if (!conn->read_event) {
  2437. /* This connection is a placeholder for something; probably a DNS
  2438. * request. It can't actually stop or start reading.*/
  2439. continue;
  2440. }
  2441. if (block) {
  2442. if (connection_is_reading(conn))
  2443. connection_stop_reading(conn);
  2444. } else {
  2445. /* Is this right? */
  2446. if (!connection_is_reading(conn))
  2447. connection_start_reading(conn);
  2448. }
  2449. }
  2450. return n;
  2451. }
  2452. /** Extract the command from a packed cell. */
  2453. static uint8_t
  2454. packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids)
  2455. {
  2456. if (wide_circ_ids) {
  2457. return get_uint8(cell->body+4);
  2458. } else {
  2459. return get_uint8(cell->body+2);
  2460. }
  2461. }
  2462. /** Extract the circuit ID from a packed cell. */
  2463. circid_t
  2464. packed_cell_get_circid(const packed_cell_t *cell, int wide_circ_ids)
  2465. {
  2466. if (wide_circ_ids) {
  2467. return ntohl(get_uint32(cell->body));
  2468. } else {
  2469. return ntohs(get_uint16(cell->body));
  2470. }
  2471. }
  2472. /** Pull as many cells as possible (but no more than <b>max</b>) from the
  2473. * queue of the first active circuit on <b>chan</b>, and write them to
  2474. * <b>chan</b>-&gt;outbuf. Return the number of cells written. Advance
  2475. * the active circuit pointer to the next active circuit in the ring. */
  2476. MOCK_IMPL(int,
  2477. channel_flush_from_first_active_circuit, (channel_t *chan, int max))
  2478. {
  2479. circuitmux_t *cmux = NULL;
  2480. int n_flushed = 0;
  2481. cell_queue_t *queue;
  2482. destroy_cell_queue_t *destroy_queue=NULL;
  2483. circuit_t *circ;
  2484. or_circuit_t *or_circ;
  2485. int streams_blocked;
  2486. packed_cell_t *cell;
  2487. /* Get the cmux */
  2488. tor_assert(chan);
  2489. tor_assert(chan->cmux);
  2490. cmux = chan->cmux;
  2491. /* Main loop: pick a circuit, send a cell, update the cmux */
  2492. while (n_flushed < max) {
  2493. circ = circuitmux_get_first_active_circuit(cmux, &destroy_queue);
  2494. if (destroy_queue) {
  2495. destroy_cell_t *dcell;
  2496. /* this code is duplicated from some of the logic below. Ugly! XXXX */
  2497. /* If we are given a destroy_queue here, then it is required to be
  2498. * nonempty... */
  2499. tor_assert(destroy_queue->n > 0);
  2500. dcell = destroy_cell_queue_pop(destroy_queue);
  2501. /* ...and pop() will always yield a cell from a nonempty queue. */
  2502. tor_assert(dcell);
  2503. /* frees dcell */
  2504. cell = destroy_cell_to_packed_cell(dcell, chan->wide_circ_ids);
  2505. /* Send the DESTROY cell. It is very unlikely that this fails but just
  2506. * in case, get rid of the channel. */
  2507. if (channel_write_packed_cell(chan, cell) < 0) {
  2508. /* The cell has been freed. */
  2509. channel_mark_for_close(chan);
  2510. continue;
  2511. }
  2512. /* Update the cmux destroy counter */
  2513. circuitmux_notify_xmit_destroy(cmux);
  2514. cell = NULL;
  2515. ++n_flushed;
  2516. continue;
  2517. }
  2518. /* If it returns NULL, no cells left to send */
  2519. if (!circ) break;
  2520. if (circ->n_chan == chan) {
  2521. queue = &circ->n_chan_cells;
  2522. streams_blocked = circ->streams_blocked_on_n_chan;
  2523. } else {
  2524. or_circ = TO_OR_CIRCUIT(circ);
  2525. tor_assert(or_circ->p_chan == chan);
  2526. queue = &TO_OR_CIRCUIT(circ)->p_chan_cells;
  2527. streams_blocked = circ->streams_blocked_on_p_chan;
  2528. }
  2529. /* Circuitmux told us this was active, so it should have cells */
  2530. if (/*BUG(*/ queue->n == 0 /*)*/) {
  2531. log_warn(LD_BUG, "Found a supposedly active circuit with no cells "
  2532. "to send. Trying to recover.");
  2533. circuitmux_set_num_cells(cmux, circ, 0);
  2534. if (! circ->marked_for_close)
  2535. circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
  2536. continue;
  2537. }
  2538. tor_assert(queue->n > 0);
  2539. /*
  2540. * Get just one cell here; once we've sent it, that can change the circuit
  2541. * selection, so we have to loop around for another even if this circuit
  2542. * has more than one.
  2543. */
  2544. cell = cell_queue_pop(queue);
  2545. /* Calculate the exact time that this cell has spent in the queue. */
  2546. if (get_options()->CellStatistics ||
  2547. get_options()->TestingEnableCellStatsEvent) {
  2548. uint32_t timestamp_now = monotime_coarse_get_stamp();
  2549. uint32_t msec_waiting =
  2550. (uint32_t) monotime_coarse_stamp_units_to_approx_msec(
  2551. timestamp_now - cell->inserted_timestamp);
  2552. if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
  2553. or_circ = TO_OR_CIRCUIT(circ);
  2554. or_circ->total_cell_waiting_time += msec_waiting;
  2555. or_circ->processed_cells++;
  2556. }
  2557. if (get_options()->TestingEnableCellStatsEvent) {
  2558. uint8_t command = packed_cell_get_command(cell, chan->wide_circ_ids);
  2559. testing_cell_stats_entry_t *ent =
  2560. tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
  2561. ent->command = command;
  2562. ent->waiting_time = msec_waiting / 10;
  2563. ent->removed = 1;
  2564. if (circ->n_chan == chan)
  2565. ent->exitward = 1;
  2566. if (!circ->testing_cell_stats)
  2567. circ->testing_cell_stats = smartlist_new();
  2568. smartlist_add(circ->testing_cell_stats, ent);
  2569. }
  2570. }
  2571. /* If we just flushed our queue and this circuit is used for a
  2572. * tunneled directory request, possibly advance its state. */
  2573. if (queue->n == 0 && chan->dirreq_id)
  2574. geoip_change_dirreq_state(chan->dirreq_id,
  2575. DIRREQ_TUNNELED,
  2576. DIRREQ_CIRC_QUEUE_FLUSHED);
  2577. /* Now send the cell. It is very unlikely that this fails but just in
  2578. * case, get rid of the channel. */
  2579. if (channel_write_packed_cell(chan, cell) < 0) {
  2580. /* The cell has been freed at this point. */
  2581. channel_mark_for_close(chan);
  2582. continue;
  2583. }
  2584. cell = NULL;
  2585. /*
  2586. * Don't packed_cell_free_unchecked(cell) here because the channel will
  2587. * do so when it gets out of the channel queue (probably already did, in
  2588. * which case that was an immediate double-free bug).
  2589. */
  2590. /* Update the counter */
  2591. ++n_flushed;
  2592. /*
  2593. * Now update the cmux; tell it we've just sent a cell, and how many
  2594. * we have left.
  2595. */
  2596. circuitmux_notify_xmit_cells(cmux, circ, 1);
  2597. circuitmux_set_num_cells(cmux, circ, queue->n);
  2598. if (queue->n == 0)
  2599. log_debug(LD_GENERAL, "Made a circuit inactive.");
  2600. /* Is the cell queue low enough to unblock all the streams that are waiting
  2601. * to write to this circuit? */
  2602. if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
  2603. set_streams_blocked_on_circ(circ, chan, 0, 0); /* unblock streams */
  2604. /* If n_flushed < max still, loop around and pick another circuit */
  2605. }
  2606. /* Okay, we're done sending now */
  2607. return n_flushed;
  2608. }
  2609. /* Minimum value is the maximum circuit window size.
  2610. *
  2611. * SENDME cells makes it that we can control how many cells can be inflight on
  2612. * a circuit from end to end. This logic makes it that on any circuit cell
  2613. * queue, we have a maximum of cells possible.
  2614. *
  2615. * Because the Tor protocol allows for a client to exit at any hop in a
  2616. * circuit and a circuit can be of a maximum of 8 hops, so in theory the
  2617. * normal worst case will be the circuit window start value times the maximum
  2618. * number of hops (8). Having more cells then that means something is wrong.
  2619. *
  2620. * However, because padding cells aren't counted in the package window, we set
  2621. * the maximum size to a reasonably large size for which we expect that we'll
  2622. * never reach in theory. And if we ever do because of future changes, we'll
  2623. * be able to control it with a consensus parameter.
  2624. *
  2625. * XXX: Unfortunately, END cells aren't accounted for in the circuit window
  2626. * which means that for instance if a client opens 8001 streams, the 8001
  2627. * following END cells will queue up in the circuit which will get closed if
  2628. * the max limit is 8000. Which is sad because it is allowed by the Tor
  2629. * protocol. But, we need an upper bound on circuit queue in order to avoid
  2630. * DoS memory pressure so the default size is a middle ground between not
  2631. * having any limit and having a very restricted one. This is why we can also
  2632. * control it through a consensus parameter. */
  2633. #define RELAY_CIRC_CELL_QUEUE_SIZE_MIN CIRCWINDOW_START_MAX
  2634. /* We can't have a consensus parameter above this value. */
  2635. #define RELAY_CIRC_CELL_QUEUE_SIZE_MAX INT32_MAX
  2636. /* Default value is set to a large value so we can handle padding cells
  2637. * properly which aren't accounted for in the SENDME window. Default is 50000
  2638. * allowed cells in the queue resulting in ~25MB. */
  2639. #define RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT \
  2640. (50 * RELAY_CIRC_CELL_QUEUE_SIZE_MIN)
  2641. /* The maximum number of cell a circuit queue can contain. This is updated at
  2642. * every new consensus and controlled by a parameter. */
  2643. static int32_t max_circuit_cell_queue_size =
  2644. RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT;
  2645. /* Called when the consensus has changed. At this stage, the global consensus
  2646. * object has NOT been updated. It is called from
  2647. * notify_before_networkstatus_changes(). */
  2648. void
  2649. relay_consensus_has_changed(const networkstatus_t *ns)
  2650. {
  2651. tor_assert(ns);
  2652. /* Update the circuit max cell queue size from the consensus. */
  2653. max_circuit_cell_queue_size =
  2654. networkstatus_get_param(ns, "circ_max_cell_queue_size",
  2655. RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT,
  2656. RELAY_CIRC_CELL_QUEUE_SIZE_MIN,
  2657. RELAY_CIRC_CELL_QUEUE_SIZE_MAX);
  2658. }
  2659. /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>chan</b>
  2660. * transmitting in <b>direction</b>.
  2661. *
  2662. * The given <b>cell</b> is copied onto the circuit queue so the caller must
  2663. * cleanup the memory.
  2664. *
  2665. * This function is part of the fast path. */
  2666. void
  2667. append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
  2668. cell_t *cell, cell_direction_t direction,
  2669. streamid_t fromstream)
  2670. {
  2671. or_circuit_t *orcirc = NULL;
  2672. cell_queue_t *queue;
  2673. int streams_blocked;
  2674. int exitward;
  2675. if (circ->marked_for_close)
  2676. return;
  2677. exitward = (direction == CELL_DIRECTION_OUT);
  2678. if (exitward) {
  2679. queue = &circ->n_chan_cells;
  2680. streams_blocked = circ->streams_blocked_on_n_chan;
  2681. } else {
  2682. orcirc = TO_OR_CIRCUIT(circ);
  2683. queue = &orcirc->p_chan_cells;
  2684. streams_blocked = circ->streams_blocked_on_p_chan;
  2685. }
  2686. if (PREDICT_UNLIKELY(queue->n >= max_circuit_cell_queue_size)) {
  2687. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  2688. "%s circuit has %d cells in its queue, maximum allowed is %d. "
  2689. "Closing circuit for safety reasons.",
  2690. (exitward) ? "Outbound" : "Inbound", queue->n,
  2691. max_circuit_cell_queue_size);
  2692. circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
  2693. stats_n_circ_max_cell_reached++;
  2694. return;
  2695. }
  2696. /* Very important that we copy to the circuit queue because all calls to
  2697. * this function use the stack for the cell memory. */
  2698. cell_queue_append_packed_copy(circ, queue, exitward, cell,
  2699. chan->wide_circ_ids, 1);
  2700. /* Check and run the OOM if needed. */
  2701. if (PREDICT_UNLIKELY(cell_queues_check_size())) {
  2702. /* We ran the OOM handler which might have closed this circuit. */
  2703. if (circ->marked_for_close)
  2704. return;
  2705. }
  2706. /* If we have too many cells on the circuit, we should stop reading from
  2707. * the edge streams for a while. */
  2708. if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
  2709. set_streams_blocked_on_circ(circ, chan, 1, 0); /* block streams */
  2710. if (streams_blocked && fromstream) {
  2711. /* This edge connection is apparently not blocked; block it. */
  2712. set_streams_blocked_on_circ(circ, chan, 1, fromstream);
  2713. }
  2714. update_circuit_on_cmux(circ, direction);
  2715. if (queue->n == 1) {
  2716. /* This was the first cell added to the queue. We just made this
  2717. * circuit active. */
  2718. log_debug(LD_GENERAL, "Made a circuit active.");
  2719. }
  2720. /* New way: mark this as having waiting cells for the scheduler */
  2721. scheduler_channel_has_waiting_cells(chan);
  2722. }
  2723. /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
  2724. * have at least 18 bytes of free space. The encoding is, as specified in
  2725. * tor-spec.txt:
  2726. * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
  2727. * LENGTH [1 byte]
  2728. * ADDRESS [length bytes]
  2729. * Return the number of bytes added, or -1 on error */
  2730. int
  2731. append_address_to_payload(uint8_t *payload_out, const tor_addr_t *addr)
  2732. {
  2733. uint32_t a;
  2734. switch (tor_addr_family(addr)) {
  2735. case AF_INET:
  2736. payload_out[0] = RESOLVED_TYPE_IPV4;
  2737. payload_out[1] = 4;
  2738. a = tor_addr_to_ipv4n(addr);
  2739. memcpy(payload_out+2, &a, 4);
  2740. return 6;
  2741. case AF_INET6:
  2742. payload_out[0] = RESOLVED_TYPE_IPV6;
  2743. payload_out[1] = 16;
  2744. memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
  2745. return 18;
  2746. case AF_UNSPEC:
  2747. default:
  2748. return -1;
  2749. }
  2750. }
  2751. /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
  2752. * encoded as by append_address_to_payload(), try to decode the address into
  2753. * *<b>addr_out</b>. Return the next byte in the payload after the address on
  2754. * success, or NULL on failure. */
  2755. const uint8_t *
  2756. decode_address_from_payload(tor_addr_t *addr_out, const uint8_t *payload,
  2757. int payload_len)
  2758. {
  2759. if (payload_len < 2)
  2760. return NULL;
  2761. if (payload_len < 2+payload[1])
  2762. return NULL;
  2763. switch (payload[0]) {
  2764. case RESOLVED_TYPE_IPV4:
  2765. if (payload[1] != 4)
  2766. return NULL;
  2767. tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
  2768. break;
  2769. case RESOLVED_TYPE_IPV6:
  2770. if (payload[1] != 16)
  2771. return NULL;
  2772. tor_addr_from_ipv6_bytes(addr_out, (char*)(payload+2));
  2773. break;
  2774. default:
  2775. tor_addr_make_unspec(addr_out);
  2776. break;
  2777. }
  2778. return payload + 2 + payload[1];
  2779. }
  2780. /** Remove all the cells queued on <b>circ</b> for <b>chan</b>. */
  2781. void
  2782. circuit_clear_cell_queue(circuit_t *circ, channel_t *chan)
  2783. {
  2784. cell_queue_t *queue;
  2785. cell_direction_t direction;
  2786. if (circ->n_chan == chan) {
  2787. queue = &circ->n_chan_cells;
  2788. direction = CELL_DIRECTION_OUT;
  2789. } else {
  2790. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  2791. tor_assert(orcirc->p_chan == chan);
  2792. queue = &orcirc->p_chan_cells;
  2793. direction = CELL_DIRECTION_IN;
  2794. }
  2795. /* Clear the queue */
  2796. cell_queue_clear(queue);
  2797. /* Update the cell counter in the cmux */
  2798. if (chan->cmux && circuitmux_is_circuit_attached(chan->cmux, circ))
  2799. update_circuit_on_cmux(circ, direction);
  2800. }
  2801. /** Return 1 if we shouldn't restart reading on this circuit, even if
  2802. * we get a SENDME. Else return 0.
  2803. */
  2804. static int
  2805. circuit_queue_streams_are_blocked(circuit_t *circ)
  2806. {
  2807. if (CIRCUIT_IS_ORIGIN(circ)) {
  2808. return circ->streams_blocked_on_n_chan;
  2809. } else {
  2810. return circ->streams_blocked_on_p_chan;
  2811. }
  2812. }