connection.c 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373
  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-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file connection.c
  8. * \brief General high-level functions to handle reading and writing
  9. * on connections.
  10. *
  11. * Each connection (ideally) represents a TLS connection, a TCP socket, a unix
  12. * socket, or a UDP socket on which reads and writes can occur. (But see
  13. * connection_edge.c for cases where connections can also represent streams
  14. * that do not have a corresponding socket.)
  15. *
  16. * The module implements the abstract type, connection_t. The subtypes are:
  17. * <ul>
  18. * <li>listener_connection_t, implemented here in connection.c
  19. * <li>dir_connection_t, implemented in directory.c
  20. * <li>or_connection_t, implemented in connection_or.c
  21. * <li>edge_connection_t, implemented in connection_edge.c, along with
  22. * its subtype(s):
  23. * <ul><li>entry_connection_t, also implemented in connection_edge.c
  24. * </ul>
  25. * <li>control_connection_t, implemented in control.c
  26. * </ul>
  27. *
  28. * The base type implemented in this module is responsible for basic
  29. * rate limiting, flow control, and marshalling bytes onto and off of the
  30. * network (either directly or via TLS).
  31. *
  32. * Connections are registered with the main loop with connection_add(). As
  33. * they become able to read or write register the fact with the event main
  34. * loop by calling connection_watch_events(), connection_start_reading(), or
  35. * connection_start_writing(). When they no longer want to read or write,
  36. * they call connection_stop_reading() or connection_stop_writing().
  37. *
  38. * To queue data to be written on a connection, call
  39. * connection_buf_add(). When data arrives, the
  40. * connection_process_inbuf() callback is invoked, which dispatches to a
  41. * type-specific function (such as connection_edge_process_inbuf() for
  42. * example). Connection types that need notice of when data has been written
  43. * receive notification via connection_flushed_some() and
  44. * connection_finished_flushing(). These functions all delegate to
  45. * type-specific implementations.
  46. *
  47. * Additionally, beyond the core of connection_t, this module also implements:
  48. * <ul>
  49. * <li>Listeners, which wait for incoming sockets and launch connections
  50. * <li>Outgoing SOCKS proxy support
  51. * <li>Outgoing HTTP proxy support
  52. * <li>An out-of-sockets handler for dealing with socket exhaustion
  53. * </ul>
  54. **/
  55. #define CONNECTION_PRIVATE
  56. #include "or.h"
  57. #include "bridges.h"
  58. #include "buffers.h"
  59. #include "buffers_tls.h"
  60. /*
  61. * Define this so we get channel internal functions, since we're implementing
  62. * part of a subclass (channel_tls_t).
  63. */
  64. #define TOR_CHANNEL_INTERNAL_
  65. #define CONNECTION_PRIVATE
  66. #include "backtrace.h"
  67. #include "channel.h"
  68. #include "channeltls.h"
  69. #include "circuitbuild.h"
  70. #include "circuitlist.h"
  71. #include "circuituse.h"
  72. #include "config.h"
  73. #include "connection.h"
  74. #include "connection_edge.h"
  75. #include "connection_or.h"
  76. #include "control.h"
  77. #include "directory.h"
  78. #include "dirserv.h"
  79. #include "dns.h"
  80. #include "dnsserv.h"
  81. #include "dos.h"
  82. #include "entrynodes.h"
  83. #include "ext_orport.h"
  84. #include "geoip.h"
  85. #include "main.h"
  86. #include "hs_common.h"
  87. #include "hs_ident.h"
  88. #include "nodelist.h"
  89. #include "proto_http.h"
  90. #include "proto_socks.h"
  91. #include "policies.h"
  92. #include "reasons.h"
  93. #include "relay.h"
  94. #include "rendclient.h"
  95. #include "rendcommon.h"
  96. #include "rephist.h"
  97. #include "router.h"
  98. #include "routerlist.h"
  99. #include "transports.h"
  100. #include "routerparse.h"
  101. #include "sandbox.h"
  102. #include "transports.h"
  103. #ifdef HAVE_PWD_H
  104. #include <pwd.h>
  105. #endif
  106. #ifdef HAVE_SYS_UN_H
  107. #include <sys/socket.h>
  108. #include <sys/un.h>
  109. #endif
  110. static connection_t *connection_listener_new(
  111. const struct sockaddr *listensockaddr,
  112. socklen_t listensocklen, int type,
  113. const char *address,
  114. const port_cfg_t *portcfg);
  115. static void connection_init(time_t now, connection_t *conn, int type,
  116. int socket_family);
  117. static int connection_handle_listener_read(connection_t *conn, int new_type);
  118. static int connection_bucket_should_increase(int bucket,
  119. or_connection_t *conn);
  120. static int connection_finished_flushing(connection_t *conn);
  121. static int connection_flushed_some(connection_t *conn);
  122. static int connection_finished_connecting(connection_t *conn);
  123. static int connection_reached_eof(connection_t *conn);
  124. static int connection_buf_read_from_socket(connection_t *conn,
  125. ssize_t *max_to_read,
  126. int *socket_error);
  127. static int connection_process_inbuf(connection_t *conn, int package_partial);
  128. static void client_check_address_changed(tor_socket_t sock);
  129. static void set_constrained_socket_buffers(tor_socket_t sock, int size);
  130. static const char *connection_proxy_state_to_string(int state);
  131. static int connection_read_https_proxy_response(connection_t *conn);
  132. static void connection_send_socks5_connect(connection_t *conn);
  133. static const char *proxy_type_to_string(int proxy_type);
  134. static int get_proxy_type(void);
  135. const tor_addr_t *conn_get_outbound_address(sa_family_t family,
  136. const or_options_t *options, unsigned int conn_type);
  137. /** The last addresses that our network interface seemed to have been
  138. * binding to. We use this as one way to detect when our IP changes.
  139. *
  140. * XXXX+ We should really use the entire list of interfaces here.
  141. **/
  142. static tor_addr_t *last_interface_ipv4 = NULL;
  143. /* DOCDOC last_interface_ipv6 */
  144. static tor_addr_t *last_interface_ipv6 = NULL;
  145. /** A list of tor_addr_t for addresses we've used in outgoing connections.
  146. * Used to detect IP address changes. */
  147. static smartlist_t *outgoing_addrs = NULL;
  148. #define CASE_ANY_LISTENER_TYPE \
  149. case CONN_TYPE_OR_LISTENER: \
  150. case CONN_TYPE_EXT_OR_LISTENER: \
  151. case CONN_TYPE_AP_LISTENER: \
  152. case CONN_TYPE_DIR_LISTENER: \
  153. case CONN_TYPE_CONTROL_LISTENER: \
  154. case CONN_TYPE_AP_TRANS_LISTENER: \
  155. case CONN_TYPE_AP_NATD_LISTENER: \
  156. case CONN_TYPE_AP_DNS_LISTENER: \
  157. case CONN_TYPE_AP_HTTP_CONNECT_LISTENER
  158. /**************************************************************/
  159. /**
  160. * Return the human-readable name for the connection type <b>type</b>
  161. */
  162. const char *
  163. conn_type_to_string(int type)
  164. {
  165. static char buf[64];
  166. switch (type) {
  167. case CONN_TYPE_OR_LISTENER: return "OR listener";
  168. case CONN_TYPE_OR: return "OR";
  169. case CONN_TYPE_EXIT: return "Exit";
  170. case CONN_TYPE_AP_LISTENER: return "Socks listener";
  171. case CONN_TYPE_AP_TRANS_LISTENER:
  172. return "Transparent pf/netfilter listener";
  173. case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
  174. case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
  175. case CONN_TYPE_AP: return "Socks";
  176. case CONN_TYPE_DIR_LISTENER: return "Directory listener";
  177. case CONN_TYPE_DIR: return "Directory";
  178. case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
  179. case CONN_TYPE_CONTROL: return "Control";
  180. case CONN_TYPE_EXT_OR: return "Extended OR";
  181. case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
  182. case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
  183. default:
  184. log_warn(LD_BUG, "unknown connection type %d", type);
  185. tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
  186. return buf;
  187. }
  188. }
  189. /**
  190. * Return the human-readable name for the connection state <b>state</b>
  191. * for the connection type <b>type</b>
  192. */
  193. const char *
  194. conn_state_to_string(int type, int state)
  195. {
  196. static char buf[96];
  197. switch (type) {
  198. CASE_ANY_LISTENER_TYPE:
  199. if (state == LISTENER_STATE_READY)
  200. return "ready";
  201. break;
  202. case CONN_TYPE_OR:
  203. switch (state) {
  204. case OR_CONN_STATE_CONNECTING: return "connect()ing";
  205. case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
  206. case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
  207. case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
  208. return "renegotiating (TLS, v2 handshake)";
  209. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  210. return "waiting for renegotiation or V3 handshake";
  211. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  212. return "handshaking (Tor, v2 handshake)";
  213. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  214. return "handshaking (Tor, v3 handshake)";
  215. case OR_CONN_STATE_OPEN: return "open";
  216. }
  217. break;
  218. case CONN_TYPE_EXT_OR:
  219. switch (state) {
  220. case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
  221. return "waiting for authentication type";
  222. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
  223. return "waiting for client nonce";
  224. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
  225. return "waiting for client hash";
  226. case EXT_OR_CONN_STATE_OPEN: return "open";
  227. case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
  228. }
  229. break;
  230. case CONN_TYPE_EXIT:
  231. switch (state) {
  232. case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
  233. case EXIT_CONN_STATE_CONNECTING: return "connecting";
  234. case EXIT_CONN_STATE_OPEN: return "open";
  235. case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
  236. }
  237. break;
  238. case CONN_TYPE_AP:
  239. switch (state) {
  240. case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
  241. case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
  242. case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
  243. case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
  244. case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
  245. case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
  246. case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
  247. case AP_CONN_STATE_OPEN: return "open";
  248. }
  249. break;
  250. case CONN_TYPE_DIR:
  251. switch (state) {
  252. case DIR_CONN_STATE_CONNECTING: return "connecting";
  253. case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
  254. case DIR_CONN_STATE_CLIENT_READING: return "client reading";
  255. case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
  256. case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
  257. case DIR_CONN_STATE_SERVER_WRITING: return "writing";
  258. }
  259. break;
  260. case CONN_TYPE_CONTROL:
  261. switch (state) {
  262. case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
  263. case CONTROL_CONN_STATE_NEEDAUTH:
  264. return "waiting for authentication (protocol v1)";
  265. }
  266. break;
  267. }
  268. log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
  269. tor_snprintf(buf, sizeof(buf),
  270. "unknown state [%d] on unknown [%s] connection",
  271. state, conn_type_to_string(type));
  272. return buf;
  273. }
  274. /** Allocate and return a new dir_connection_t, initialized as by
  275. * connection_init(). */
  276. dir_connection_t *
  277. dir_connection_new(int socket_family)
  278. {
  279. dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
  280. connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
  281. return dir_conn;
  282. }
  283. /** Allocate and return a new or_connection_t, initialized as by
  284. * connection_init().
  285. *
  286. * Initialize active_circuit_pqueue.
  287. *
  288. * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
  289. */
  290. or_connection_t *
  291. or_connection_new(int type, int socket_family)
  292. {
  293. or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  294. time_t now = time(NULL);
  295. tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
  296. connection_init(now, TO_CONN(or_conn), type, socket_family);
  297. connection_or_set_canonical(or_conn, 0);
  298. if (type == CONN_TYPE_EXT_OR)
  299. connection_or_set_ext_or_identifier(or_conn);
  300. return or_conn;
  301. }
  302. /** Allocate and return a new entry_connection_t, initialized as by
  303. * connection_init().
  304. *
  305. * Allocate space to store the socks_request.
  306. */
  307. entry_connection_t *
  308. entry_connection_new(int type, int socket_family)
  309. {
  310. entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
  311. tor_assert(type == CONN_TYPE_AP);
  312. connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
  313. entry_conn->socks_request = socks_request_new();
  314. /* If this is coming from a listener, we'll set it up based on the listener
  315. * in a little while. Otherwise, we're doing this as a linked connection
  316. * of some kind, and we should set it up here based on the socket family */
  317. if (socket_family == AF_INET)
  318. entry_conn->entry_cfg.ipv4_traffic = 1;
  319. else if (socket_family == AF_INET6)
  320. entry_conn->entry_cfg.ipv6_traffic = 1;
  321. else if (socket_family == AF_UNIX)
  322. entry_conn->is_socks_socket = 1;
  323. return entry_conn;
  324. }
  325. /** Allocate and return a new edge_connection_t, initialized as by
  326. * connection_init(). */
  327. edge_connection_t *
  328. edge_connection_new(int type, int socket_family)
  329. {
  330. edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
  331. tor_assert(type == CONN_TYPE_EXIT);
  332. connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  333. return edge_conn;
  334. }
  335. /** Allocate and return a new control_connection_t, initialized as by
  336. * connection_init(). */
  337. control_connection_t *
  338. control_connection_new(int socket_family)
  339. {
  340. control_connection_t *control_conn =
  341. tor_malloc_zero(sizeof(control_connection_t));
  342. connection_init(time(NULL),
  343. TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
  344. return control_conn;
  345. }
  346. /** Allocate and return a new listener_connection_t, initialized as by
  347. * connection_init(). */
  348. listener_connection_t *
  349. listener_connection_new(int type, int socket_family)
  350. {
  351. listener_connection_t *listener_conn =
  352. tor_malloc_zero(sizeof(listener_connection_t));
  353. connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
  354. return listener_conn;
  355. }
  356. /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
  357. * to make or receive connections of address family <b>socket_family</b>. The
  358. * type should be one of the CONN_TYPE_* constants. */
  359. connection_t *
  360. connection_new(int type, int socket_family)
  361. {
  362. switch (type) {
  363. case CONN_TYPE_OR:
  364. case CONN_TYPE_EXT_OR:
  365. return TO_CONN(or_connection_new(type, socket_family));
  366. case CONN_TYPE_EXIT:
  367. return TO_CONN(edge_connection_new(type, socket_family));
  368. case CONN_TYPE_AP:
  369. return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
  370. case CONN_TYPE_DIR:
  371. return TO_CONN(dir_connection_new(socket_family));
  372. case CONN_TYPE_CONTROL:
  373. return TO_CONN(control_connection_new(socket_family));
  374. CASE_ANY_LISTENER_TYPE:
  375. return TO_CONN(listener_connection_new(type, socket_family));
  376. default: {
  377. connection_t *conn = tor_malloc_zero(sizeof(connection_t));
  378. connection_init(time(NULL), conn, type, socket_family);
  379. return conn;
  380. }
  381. }
  382. }
  383. /** Initializes conn. (you must call connection_add() to link it into the main
  384. * array).
  385. *
  386. * Set conn-\>magic to the correct value.
  387. *
  388. * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
  389. * -1 to signify they are not yet assigned.
  390. *
  391. * Initialize conn's timestamps to now.
  392. */
  393. static void
  394. connection_init(time_t now, connection_t *conn, int type, int socket_family)
  395. {
  396. static uint64_t n_connections_allocated = 1;
  397. switch (type) {
  398. case CONN_TYPE_OR:
  399. case CONN_TYPE_EXT_OR:
  400. conn->magic = OR_CONNECTION_MAGIC;
  401. break;
  402. case CONN_TYPE_EXIT:
  403. conn->magic = EDGE_CONNECTION_MAGIC;
  404. break;
  405. case CONN_TYPE_AP:
  406. conn->magic = ENTRY_CONNECTION_MAGIC;
  407. break;
  408. case CONN_TYPE_DIR:
  409. conn->magic = DIR_CONNECTION_MAGIC;
  410. break;
  411. case CONN_TYPE_CONTROL:
  412. conn->magic = CONTROL_CONNECTION_MAGIC;
  413. break;
  414. CASE_ANY_LISTENER_TYPE:
  415. conn->magic = LISTENER_CONNECTION_MAGIC;
  416. break;
  417. default:
  418. conn->magic = BASE_CONNECTION_MAGIC;
  419. break;
  420. }
  421. conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
  422. conn->conn_array_index = -1; /* also default to 'not used' */
  423. conn->global_identifier = n_connections_allocated++;
  424. conn->type = type;
  425. conn->socket_family = socket_family;
  426. if (!connection_is_listener(conn)) {
  427. /* listeners never use their buf */
  428. conn->inbuf = buf_new();
  429. conn->outbuf = buf_new();
  430. }
  431. conn->timestamp_created = now;
  432. conn->timestamp_lastread = now;
  433. conn->timestamp_lastwritten = now;
  434. }
  435. /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
  436. void
  437. connection_link_connections(connection_t *conn_a, connection_t *conn_b)
  438. {
  439. tor_assert(! SOCKET_OK(conn_a->s));
  440. tor_assert(! SOCKET_OK(conn_b->s));
  441. conn_a->linked = 1;
  442. conn_b->linked = 1;
  443. conn_a->linked_conn = conn_b;
  444. conn_b->linked_conn = conn_a;
  445. }
  446. /** Return true iff the provided connection listener type supports AF_UNIX
  447. * sockets. */
  448. int
  449. conn_listener_type_supports_af_unix(int type)
  450. {
  451. /* For now only control ports or SOCKS ports can be Unix domain sockets
  452. * and listeners at the same time */
  453. switch (type) {
  454. case CONN_TYPE_CONTROL_LISTENER:
  455. case CONN_TYPE_AP_LISTENER:
  456. return 1;
  457. default:
  458. return 0;
  459. }
  460. }
  461. /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
  462. * necessary, close its socket if necessary, and mark the directory as dirty
  463. * if <b>conn</b> is an OR or OP connection.
  464. */
  465. STATIC void
  466. connection_free_minimal(connection_t *conn)
  467. {
  468. void *mem;
  469. size_t memlen;
  470. if (!conn)
  471. return;
  472. switch (conn->type) {
  473. case CONN_TYPE_OR:
  474. case CONN_TYPE_EXT_OR:
  475. tor_assert(conn->magic == OR_CONNECTION_MAGIC);
  476. mem = TO_OR_CONN(conn);
  477. memlen = sizeof(or_connection_t);
  478. break;
  479. case CONN_TYPE_AP:
  480. tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
  481. mem = TO_ENTRY_CONN(conn);
  482. memlen = sizeof(entry_connection_t);
  483. break;
  484. case CONN_TYPE_EXIT:
  485. tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
  486. mem = TO_EDGE_CONN(conn);
  487. memlen = sizeof(edge_connection_t);
  488. break;
  489. case CONN_TYPE_DIR:
  490. tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
  491. mem = TO_DIR_CONN(conn);
  492. memlen = sizeof(dir_connection_t);
  493. break;
  494. case CONN_TYPE_CONTROL:
  495. tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
  496. mem = TO_CONTROL_CONN(conn);
  497. memlen = sizeof(control_connection_t);
  498. break;
  499. CASE_ANY_LISTENER_TYPE:
  500. tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
  501. mem = TO_LISTENER_CONN(conn);
  502. memlen = sizeof(listener_connection_t);
  503. break;
  504. default:
  505. tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
  506. mem = conn;
  507. memlen = sizeof(connection_t);
  508. break;
  509. }
  510. if (conn->linked) {
  511. log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
  512. "bytes on inbuf, %d on outbuf.",
  513. conn_type_to_string(conn->type),
  514. conn_state_to_string(conn->type, conn->state),
  515. (int)connection_get_inbuf_len(conn),
  516. (int)connection_get_outbuf_len(conn));
  517. }
  518. if (!connection_is_listener(conn)) {
  519. buf_free(conn->inbuf);
  520. buf_free(conn->outbuf);
  521. } else {
  522. if (conn->socket_family == AF_UNIX) {
  523. /* For now only control and SOCKS ports can be Unix domain sockets
  524. * and listeners at the same time */
  525. tor_assert(conn_listener_type_supports_af_unix(conn->type));
  526. if (unlink(conn->address) < 0 && errno != ENOENT) {
  527. log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
  528. strerror(errno));
  529. }
  530. }
  531. }
  532. tor_free(conn->address);
  533. if (connection_speaks_cells(conn)) {
  534. or_connection_t *or_conn = TO_OR_CONN(conn);
  535. tor_tls_free(or_conn->tls);
  536. or_conn->tls = NULL;
  537. or_handshake_state_free(or_conn->handshake_state);
  538. or_conn->handshake_state = NULL;
  539. tor_free(or_conn->nickname);
  540. if (or_conn->chan) {
  541. /* Owww, this shouldn't happen, but... */
  542. log_info(LD_CHANNEL,
  543. "Freeing orconn at %p, saw channel %p with ID "
  544. U64_FORMAT " left un-NULLed",
  545. or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
  546. U64_PRINTF_ARG(
  547. TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
  548. if (!CHANNEL_FINISHED(TLS_CHAN_TO_BASE(or_conn->chan))) {
  549. channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
  550. }
  551. or_conn->chan->conn = NULL;
  552. or_conn->chan = NULL;
  553. }
  554. }
  555. if (conn->type == CONN_TYPE_AP) {
  556. entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
  557. tor_free(entry_conn->chosen_exit_name);
  558. tor_free(entry_conn->original_dest_address);
  559. if (entry_conn->socks_request)
  560. socks_request_free(entry_conn->socks_request);
  561. if (entry_conn->pending_optimistic_data) {
  562. buf_free(entry_conn->pending_optimistic_data);
  563. }
  564. if (entry_conn->sending_optimistic_data) {
  565. buf_free(entry_conn->sending_optimistic_data);
  566. }
  567. }
  568. if (CONN_IS_EDGE(conn)) {
  569. rend_data_free(TO_EDGE_CONN(conn)->rend_data);
  570. hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
  571. }
  572. if (conn->type == CONN_TYPE_CONTROL) {
  573. control_connection_t *control_conn = TO_CONTROL_CONN(conn);
  574. tor_free(control_conn->safecookie_client_hash);
  575. tor_free(control_conn->incoming_cmd);
  576. if (control_conn->ephemeral_onion_services) {
  577. SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
  578. memwipe(cp, 0, strlen(cp));
  579. tor_free(cp);
  580. });
  581. smartlist_free(control_conn->ephemeral_onion_services);
  582. }
  583. }
  584. /* Probably already freed by connection_free. */
  585. tor_event_free(conn->read_event);
  586. tor_event_free(conn->write_event);
  587. conn->read_event = conn->write_event = NULL;
  588. if (conn->type == CONN_TYPE_DIR) {
  589. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  590. tor_free(dir_conn->requested_resource);
  591. tor_compress_free(dir_conn->compress_state);
  592. if (dir_conn->spool) {
  593. SMARTLIST_FOREACH(dir_conn->spool, spooled_resource_t *, spooled,
  594. spooled_resource_free(spooled));
  595. smartlist_free(dir_conn->spool);
  596. }
  597. rend_data_free(dir_conn->rend_data);
  598. hs_ident_dir_conn_free(dir_conn->hs_ident);
  599. if (dir_conn->guard_state) {
  600. /* Cancel before freeing, if it's still there. */
  601. entry_guard_cancel(&dir_conn->guard_state);
  602. }
  603. circuit_guard_state_free(dir_conn->guard_state);
  604. }
  605. if (SOCKET_OK(conn->s)) {
  606. log_debug(LD_NET,"closing fd %d.",(int)conn->s);
  607. tor_close_socket(conn->s);
  608. conn->s = TOR_INVALID_SOCKET;
  609. }
  610. if (conn->type == CONN_TYPE_OR &&
  611. !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
  612. log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
  613. connection_or_clear_identity(TO_OR_CONN(conn));
  614. }
  615. if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
  616. connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
  617. tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
  618. tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
  619. tor_free(TO_OR_CONN(conn)->ext_or_transport);
  620. }
  621. memwipe(mem, 0xCC, memlen); /* poison memory */
  622. tor_free(mem);
  623. }
  624. /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
  625. */
  626. MOCK_IMPL(void,
  627. connection_free_,(connection_t *conn))
  628. {
  629. if (!conn)
  630. return;
  631. tor_assert(!connection_is_on_closeable_list(conn));
  632. tor_assert(!connection_in_array(conn));
  633. if (BUG(conn->linked_conn)) {
  634. conn->linked_conn->linked_conn = NULL;
  635. if (! conn->linked_conn->marked_for_close &&
  636. conn->linked_conn->reading_from_linked_conn)
  637. connection_start_reading(conn->linked_conn);
  638. conn->linked_conn = NULL;
  639. }
  640. if (connection_speaks_cells(conn)) {
  641. if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
  642. connection_or_clear_identity(TO_OR_CONN(conn));
  643. }
  644. }
  645. if (conn->type == CONN_TYPE_CONTROL) {
  646. connection_control_closed(TO_CONTROL_CONN(conn));
  647. }
  648. #if 1
  649. /* DEBUGGING */
  650. if (conn->type == CONN_TYPE_AP) {
  651. connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
  652. "connection_free");
  653. }
  654. #endif /* 1 */
  655. /* Notify the circuit creation DoS mitigation subsystem that an OR client
  656. * connection has been closed. And only do that if we track it. */
  657. if (conn->type == CONN_TYPE_OR) {
  658. dos_close_client_conn(TO_OR_CONN(conn));
  659. }
  660. connection_unregister_events(conn);
  661. connection_free_minimal(conn);
  662. }
  663. /**
  664. * Called when we're about to finally unlink and free a connection:
  665. * perform necessary accounting and cleanup
  666. * - Directory conns that failed to fetch a rendezvous descriptor
  667. * need to inform pending rendezvous streams.
  668. * - OR conns need to call rep_hist_note_*() to record status.
  669. * - AP conns need to send a socks reject if necessary.
  670. * - Exit conns need to call connection_dns_remove() if necessary.
  671. * - AP and Exit conns need to send an end cell if they can.
  672. * - DNS conns need to fail any resolves that are pending on them.
  673. * - OR and edge connections need to be unlinked from circuits.
  674. */
  675. void
  676. connection_about_to_close_connection(connection_t *conn)
  677. {
  678. tor_assert(conn->marked_for_close);
  679. switch (conn->type) {
  680. case CONN_TYPE_DIR:
  681. connection_dir_about_to_close(TO_DIR_CONN(conn));
  682. break;
  683. case CONN_TYPE_OR:
  684. case CONN_TYPE_EXT_OR:
  685. connection_or_about_to_close(TO_OR_CONN(conn));
  686. break;
  687. case CONN_TYPE_AP:
  688. connection_ap_about_to_close(TO_ENTRY_CONN(conn));
  689. break;
  690. case CONN_TYPE_EXIT:
  691. connection_exit_about_to_close(TO_EDGE_CONN(conn));
  692. break;
  693. }
  694. }
  695. /** Return true iff connection_close_immediate() has been called on this
  696. * connection. */
  697. #define CONN_IS_CLOSED(c) \
  698. ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
  699. /** Close the underlying socket for <b>conn</b>, so we don't try to
  700. * flush it. Must be used in conjunction with (right before)
  701. * connection_mark_for_close().
  702. */
  703. void
  704. connection_close_immediate(connection_t *conn)
  705. {
  706. assert_connection_ok(conn,0);
  707. if (CONN_IS_CLOSED(conn)) {
  708. log_err(LD_BUG,"Attempt to close already-closed connection.");
  709. tor_fragile_assert();
  710. return;
  711. }
  712. if (conn->outbuf_flushlen) {
  713. log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
  714. (int)conn->s, conn_type_to_string(conn->type),
  715. conn_state_to_string(conn->type, conn->state),
  716. (int)conn->outbuf_flushlen);
  717. }
  718. connection_unregister_events(conn);
  719. /* Prevent the event from getting unblocked. */
  720. conn->read_blocked_on_bw =
  721. conn->write_blocked_on_bw = 0;
  722. if (SOCKET_OK(conn->s))
  723. tor_close_socket(conn->s);
  724. conn->s = TOR_INVALID_SOCKET;
  725. if (conn->linked)
  726. conn->linked_conn_is_closed = 1;
  727. if (conn->outbuf)
  728. buf_clear(conn->outbuf);
  729. conn->outbuf_flushlen = 0;
  730. }
  731. /** Mark <b>conn</b> to be closed next time we loop through
  732. * conn_close_if_marked() in main.c. */
  733. void
  734. connection_mark_for_close_(connection_t *conn, int line, const char *file)
  735. {
  736. assert_connection_ok(conn,0);
  737. tor_assert(line);
  738. tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
  739. tor_assert(file);
  740. if (conn->type == CONN_TYPE_OR) {
  741. /*
  742. * An or_connection should have been closed through one of the channel-
  743. * aware functions in connection_or.c. We'll assume this is an error
  744. * close and do that, and log a bug warning.
  745. */
  746. log_warn(LD_CHANNEL | LD_BUG,
  747. "Something tried to close an or_connection_t without going "
  748. "through channels at %s:%d",
  749. file, line);
  750. connection_or_close_for_error(TO_OR_CONN(conn), 0);
  751. } else {
  752. /* Pass it down to the real function */
  753. connection_mark_for_close_internal_(conn, line, file);
  754. }
  755. }
  756. /** Mark <b>conn</b> to be closed next time we loop through
  757. * conn_close_if_marked() in main.c; the _internal version bypasses the
  758. * CONN_TYPE_OR checks; this should be called when you either are sure that
  759. * if this is an or_connection_t the controlling channel has been notified
  760. * (e.g. with connection_or_notify_error()), or you actually are the
  761. * connection_or_close_for_error() or connection_or_close_normally() function.
  762. * For all other cases, use connection_mark_and_flush() instead, which
  763. * checks for or_connection_t properly, instead. See below.
  764. */
  765. MOCK_IMPL(void,
  766. connection_mark_for_close_internal_, (connection_t *conn,
  767. int line, const char *file))
  768. {
  769. assert_connection_ok(conn,0);
  770. tor_assert(line);
  771. tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
  772. tor_assert(file);
  773. if (conn->marked_for_close) {
  774. log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
  775. " (first at %s:%d)", file, line, conn->marked_for_close_file,
  776. conn->marked_for_close);
  777. tor_fragile_assert();
  778. return;
  779. }
  780. if (conn->type == CONN_TYPE_OR) {
  781. /*
  782. * Bad news if this happens without telling the controlling channel; do
  783. * this so we can find things that call this wrongly when the asserts hit.
  784. */
  785. log_debug(LD_CHANNEL,
  786. "Calling connection_mark_for_close_internal_() on an OR conn "
  787. "at %s:%d",
  788. file, line);
  789. }
  790. conn->marked_for_close = line;
  791. conn->marked_for_close_file = file;
  792. add_connection_to_closeable_list(conn);
  793. /* in case we're going to be held-open-til-flushed, reset
  794. * the number of seconds since last successful write, so
  795. * we get our whole 15 seconds */
  796. conn->timestamp_lastwritten = time(NULL);
  797. }
  798. /** Find each connection that has hold_open_until_flushed set to
  799. * 1 but hasn't written in the past 15 seconds, and set
  800. * hold_open_until_flushed to 0. This means it will get cleaned
  801. * up in the next loop through close_if_marked() in main.c.
  802. */
  803. void
  804. connection_expire_held_open(void)
  805. {
  806. time_t now;
  807. smartlist_t *conns = get_connection_array();
  808. now = time(NULL);
  809. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  810. /* If we've been holding the connection open, but we haven't written
  811. * for 15 seconds...
  812. */
  813. if (conn->hold_open_until_flushed) {
  814. tor_assert(conn->marked_for_close);
  815. if (now - conn->timestamp_lastwritten >= 15) {
  816. int severity;
  817. if (conn->type == CONN_TYPE_EXIT ||
  818. (conn->type == CONN_TYPE_DIR &&
  819. conn->purpose == DIR_PURPOSE_SERVER))
  820. severity = LOG_INFO;
  821. else
  822. severity = LOG_NOTICE;
  823. log_fn(severity, LD_NET,
  824. "Giving up on marked_for_close conn that's been flushing "
  825. "for 15s (fd %d, type %s, state %s).",
  826. (int)conn->s, conn_type_to_string(conn->type),
  827. conn_state_to_string(conn->type, conn->state));
  828. conn->hold_open_until_flushed = 0;
  829. }
  830. }
  831. } SMARTLIST_FOREACH_END(conn);
  832. }
  833. #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
  834. /** Create an AF_UNIX listenaddr struct.
  835. * <b>listenaddress</b> provides the path to the Unix socket.
  836. *
  837. * Eventually <b>listenaddress</b> will also optionally contain user, group,
  838. * and file permissions for the new socket. But not yet. XXX
  839. * Also, since we do not create the socket here the information doesn't help
  840. * here.
  841. *
  842. * If not NULL <b>readable_address</b> will contain a copy of the path part of
  843. * <b>listenaddress</b>.
  844. *
  845. * The listenaddr struct has to be freed by the caller.
  846. */
  847. static struct sockaddr_un *
  848. create_unix_sockaddr(const char *listenaddress, char **readable_address,
  849. socklen_t *len_out)
  850. {
  851. struct sockaddr_un *sockaddr = NULL;
  852. sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
  853. sockaddr->sun_family = AF_UNIX;
  854. if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
  855. >= sizeof(sockaddr->sun_path)) {
  856. log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
  857. escaped(listenaddress));
  858. tor_free(sockaddr);
  859. return NULL;
  860. }
  861. if (readable_address)
  862. *readable_address = tor_strdup(listenaddress);
  863. *len_out = sizeof(struct sockaddr_un);
  864. return sockaddr;
  865. }
  866. #else /* !(defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)) */
  867. static struct sockaddr *
  868. create_unix_sockaddr(const char *listenaddress, char **readable_address,
  869. socklen_t *len_out)
  870. {
  871. (void)listenaddress;
  872. (void)readable_address;
  873. log_fn(LOG_ERR, LD_BUG,
  874. "Unix domain sockets not supported, yet we tried to create one.");
  875. *len_out = 0;
  876. tor_fragile_assert();
  877. return NULL;
  878. }
  879. #endif /* defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN) */
  880. /** Warn that an accept or a connect has failed because we're running out of
  881. * TCP sockets we can use on current system. Rate-limit these warnings so
  882. * that we don't spam the log. */
  883. static void
  884. warn_too_many_conns(void)
  885. {
  886. #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
  887. static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
  888. char *m;
  889. if ((m = rate_limit_log(&last_warned, approx_time()))) {
  890. int n_conns = get_n_open_sockets();
  891. log_warn(LD_NET,"Failing because we have %d connections already. Please "
  892. "read doc/TUNING for guidance.%s", n_conns, m);
  893. tor_free(m);
  894. control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
  895. n_conns);
  896. }
  897. }
  898. #ifdef HAVE_SYS_UN_H
  899. #define UNIX_SOCKET_PURPOSE_CONTROL_SOCKET 0
  900. #define UNIX_SOCKET_PURPOSE_SOCKS_SOCKET 1
  901. /** Check if the purpose isn't one of the ones we know what to do with */
  902. static int
  903. is_valid_unix_socket_purpose(int purpose)
  904. {
  905. int valid = 0;
  906. switch (purpose) {
  907. case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
  908. case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
  909. valid = 1;
  910. break;
  911. }
  912. return valid;
  913. }
  914. /** Return a string description of a unix socket purpose */
  915. static const char *
  916. unix_socket_purpose_to_string(int purpose)
  917. {
  918. const char *s = "unknown-purpose socket";
  919. switch (purpose) {
  920. case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
  921. s = "control socket";
  922. break;
  923. case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
  924. s = "SOCKS socket";
  925. break;
  926. }
  927. return s;
  928. }
  929. /** Check whether we should be willing to open an AF_UNIX socket in
  930. * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
  931. static int
  932. check_location_for_unix_socket(const or_options_t *options, const char *path,
  933. int purpose, const port_cfg_t *port)
  934. {
  935. int r = -1;
  936. char *p = NULL;
  937. tor_assert(is_valid_unix_socket_purpose(purpose));
  938. p = tor_strdup(path);
  939. cpd_check_t flags = CPD_CHECK_MODE_ONLY;
  940. if (get_parent_directory(p)<0 || p[0] != '/') {
  941. log_warn(LD_GENERAL, "Bad unix socket address '%s'. Tor does not support "
  942. "relative paths for unix sockets.", path);
  943. goto done;
  944. }
  945. if (port->is_world_writable) {
  946. /* World-writable sockets can go anywhere. */
  947. r = 0;
  948. goto done;
  949. }
  950. if (port->is_group_writable) {
  951. flags |= CPD_GROUP_OK;
  952. }
  953. if (port->relax_dirmode_check) {
  954. flags |= CPD_RELAX_DIRMODE_CHECK;
  955. }
  956. if (check_private_dir(p, flags, options->User) < 0) {
  957. char *escpath, *escdir;
  958. escpath = esc_for_log(path);
  959. escdir = esc_for_log(p);
  960. log_warn(LD_GENERAL, "Before Tor can create a %s in %s, the directory "
  961. "%s needs to exist, and to be accessible only by the user%s "
  962. "account that is running Tor. (On some Unix systems, anybody "
  963. "who can list a socket can connect to it, so Tor is being "
  964. "careful.)",
  965. unix_socket_purpose_to_string(purpose), escpath, escdir,
  966. port->is_group_writable ? " and group" : "");
  967. tor_free(escpath);
  968. tor_free(escdir);
  969. goto done;
  970. }
  971. r = 0;
  972. done:
  973. tor_free(p);
  974. return r;
  975. }
  976. #endif /* defined(HAVE_SYS_UN_H) */
  977. /** Tell the TCP stack that it shouldn't wait for a long time after
  978. * <b>sock</b> has closed before reusing its port. Return 0 on success,
  979. * -1 on failure. */
  980. static int
  981. make_socket_reuseable(tor_socket_t sock)
  982. {
  983. #ifdef _WIN32
  984. (void) sock;
  985. return 0;
  986. #else
  987. int one=1;
  988. /* REUSEADDR on normal places means you can rebind to the port
  989. * right after somebody else has let it go. But REUSEADDR on win32
  990. * means you can bind to the port _even when somebody else
  991. * already has it bound_. So, don't do that on Win32. */
  992. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
  993. (socklen_t)sizeof(one)) == -1) {
  994. return -1;
  995. }
  996. return 0;
  997. #endif /* defined(_WIN32) */
  998. }
  999. #ifdef _WIN32
  1000. /** Tell the Windows TCP stack to prevent other applications from receiving
  1001. * traffic from tor's open ports. Return 0 on success, -1 on failure. */
  1002. static int
  1003. make_win32_socket_exclusive(tor_socket_t sock)
  1004. {
  1005. #ifdef SO_EXCLUSIVEADDRUSE
  1006. int one=1;
  1007. /* Any socket that sets REUSEADDR on win32 can bind to a port _even when
  1008. * somebody else already has it bound_, and _even if the original socket
  1009. * didn't set REUSEADDR_. Use EXCLUSIVEADDRUSE to prevent this port-stealing
  1010. * on win32. */
  1011. if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void*) &one,
  1012. (socklen_t)sizeof(one))) {
  1013. return -1;
  1014. }
  1015. return 0;
  1016. #else /* !(defined(SO_EXCLUSIVEADDRUSE)) */
  1017. (void) sock;
  1018. return 0;
  1019. #endif /* defined(SO_EXCLUSIVEADDRUSE) */
  1020. }
  1021. #endif /* defined(_WIN32) */
  1022. /** Max backlog to pass to listen. We start at */
  1023. static int listen_limit = INT_MAX;
  1024. /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
  1025. static int
  1026. tor_listen(tor_socket_t fd)
  1027. {
  1028. int r;
  1029. if ((r = listen(fd, listen_limit)) < 0) {
  1030. if (listen_limit == SOMAXCONN)
  1031. return r;
  1032. if ((r = listen(fd, SOMAXCONN)) == 0) {
  1033. listen_limit = SOMAXCONN;
  1034. log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
  1035. "didn't work, but SOMAXCONN did. Lowering backlog limit.");
  1036. }
  1037. }
  1038. return r;
  1039. }
  1040. /** Bind a new non-blocking socket listening to the socket described
  1041. * by <b>listensockaddr</b>.
  1042. *
  1043. * <b>address</b> is only used for logging purposes and to add the information
  1044. * to the conn.
  1045. */
  1046. static connection_t *
  1047. connection_listener_new(const struct sockaddr *listensockaddr,
  1048. socklen_t socklen,
  1049. int type, const char *address,
  1050. const port_cfg_t *port_cfg)
  1051. {
  1052. listener_connection_t *lis_conn;
  1053. connection_t *conn = NULL;
  1054. tor_socket_t s = TOR_INVALID_SOCKET; /* the socket we're going to make */
  1055. or_options_t const *options = get_options();
  1056. (void) options; /* Windows doesn't use this. */
  1057. #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
  1058. const struct passwd *pw = NULL;
  1059. #endif
  1060. uint16_t usePort = 0, gotPort = 0;
  1061. int start_reading = 0;
  1062. static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
  1063. tor_addr_t addr;
  1064. int exhaustion = 0;
  1065. if (listensockaddr->sa_family == AF_INET ||
  1066. listensockaddr->sa_family == AF_INET6) {
  1067. int is_stream = (type != CONN_TYPE_AP_DNS_LISTENER);
  1068. if (is_stream)
  1069. start_reading = 1;
  1070. tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
  1071. log_notice(LD_NET, "Opening %s on %s",
  1072. conn_type_to_string(type), fmt_addrport(&addr, usePort));
  1073. s = tor_open_socket_nonblocking(tor_addr_family(&addr),
  1074. is_stream ? SOCK_STREAM : SOCK_DGRAM,
  1075. is_stream ? IPPROTO_TCP: IPPROTO_UDP);
  1076. if (!SOCKET_OK(s)) {
  1077. int e = tor_socket_errno(s);
  1078. if (ERRNO_IS_RESOURCE_LIMIT(e)) {
  1079. warn_too_many_conns();
  1080. /*
  1081. * We'll call the OOS handler at the error exit, so set the
  1082. * exhaustion flag for it.
  1083. */
  1084. exhaustion = 1;
  1085. } else {
  1086. log_warn(LD_NET, "Socket creation failed: %s",
  1087. tor_socket_strerror(e));
  1088. }
  1089. goto err;
  1090. }
  1091. if (make_socket_reuseable(s) < 0) {
  1092. log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
  1093. conn_type_to_string(type),
  1094. tor_socket_strerror(errno));
  1095. }
  1096. #ifdef _WIN32
  1097. if (make_win32_socket_exclusive(s) < 0) {
  1098. log_warn(LD_NET, "Error setting SO_EXCLUSIVEADDRUSE flag on %s: %s",
  1099. conn_type_to_string(type),
  1100. tor_socket_strerror(errno));
  1101. }
  1102. #endif /* defined(_WIN32) */
  1103. #if defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT)
  1104. if (options->TransProxyType_parsed == TPT_TPROXY &&
  1105. type == CONN_TYPE_AP_TRANS_LISTENER) {
  1106. int one = 1;
  1107. if (setsockopt(s, SOL_IP, IP_TRANSPARENT, (void*)&one,
  1108. (socklen_t)sizeof(one)) < 0) {
  1109. const char *extra = "";
  1110. int e = tor_socket_errno(s);
  1111. if (e == EPERM)
  1112. extra = "TransTPROXY requires root privileges or similar"
  1113. " capabilities.";
  1114. log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
  1115. tor_socket_strerror(e), extra);
  1116. }
  1117. }
  1118. #endif /* defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT) */
  1119. #ifdef IPV6_V6ONLY
  1120. if (listensockaddr->sa_family == AF_INET6) {
  1121. int one = 1;
  1122. /* We need to set IPV6_V6ONLY so that this socket can't get used for
  1123. * IPv4 connections. */
  1124. if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
  1125. (void*)&one, (socklen_t)sizeof(one)) < 0) {
  1126. int e = tor_socket_errno(s);
  1127. log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
  1128. tor_socket_strerror(e));
  1129. /* Keep going; probably not harmful. */
  1130. }
  1131. }
  1132. #endif /* defined(IPV6_V6ONLY) */
  1133. if (bind(s,listensockaddr,socklen) < 0) {
  1134. const char *helpfulhint = "";
  1135. int e = tor_socket_errno(s);
  1136. if (ERRNO_IS_EADDRINUSE(e))
  1137. helpfulhint = ". Is Tor already running?";
  1138. log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
  1139. tor_socket_strerror(e), helpfulhint);
  1140. goto err;
  1141. }
  1142. if (is_stream) {
  1143. if (tor_listen(s) < 0) {
  1144. log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
  1145. tor_socket_strerror(tor_socket_errno(s)));
  1146. goto err;
  1147. }
  1148. }
  1149. if (usePort != 0) {
  1150. gotPort = usePort;
  1151. } else {
  1152. tor_addr_t addr2;
  1153. struct sockaddr_storage ss;
  1154. socklen_t ss_len=sizeof(ss);
  1155. if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
  1156. log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
  1157. conn_type_to_string(type),
  1158. tor_socket_strerror(tor_socket_errno(s)));
  1159. gotPort = 0;
  1160. }
  1161. tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
  1162. }
  1163. #ifdef HAVE_SYS_UN_H
  1164. /*
  1165. * AF_UNIX generic setup stuff
  1166. */
  1167. } else if (listensockaddr->sa_family == AF_UNIX) {
  1168. /* We want to start reading for both AF_UNIX cases */
  1169. start_reading = 1;
  1170. tor_assert(conn_listener_type_supports_af_unix(type));
  1171. if (check_location_for_unix_socket(options, address,
  1172. (type == CONN_TYPE_CONTROL_LISTENER) ?
  1173. UNIX_SOCKET_PURPOSE_CONTROL_SOCKET :
  1174. UNIX_SOCKET_PURPOSE_SOCKS_SOCKET, port_cfg) < 0) {
  1175. goto err;
  1176. }
  1177. log_notice(LD_NET, "Opening %s on %s",
  1178. conn_type_to_string(type), address);
  1179. tor_addr_make_unspec(&addr);
  1180. if (unlink(address) < 0 && errno != ENOENT) {
  1181. log_warn(LD_NET, "Could not unlink %s: %s", address,
  1182. strerror(errno));
  1183. goto err;
  1184. }
  1185. s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
  1186. if (! SOCKET_OK(s)) {
  1187. int e = tor_socket_errno(s);
  1188. if (ERRNO_IS_RESOURCE_LIMIT(e)) {
  1189. warn_too_many_conns();
  1190. /*
  1191. * We'll call the OOS handler at the error exit, so set the
  1192. * exhaustion flag for it.
  1193. */
  1194. exhaustion = 1;
  1195. } else {
  1196. log_warn(LD_NET,"Socket creation failed: %s.", strerror(e));
  1197. }
  1198. goto err;
  1199. }
  1200. if (bind(s, listensockaddr,
  1201. (socklen_t)sizeof(struct sockaddr_un)) == -1) {
  1202. log_warn(LD_NET,"Bind to %s failed: %s.", address,
  1203. tor_socket_strerror(tor_socket_errno(s)));
  1204. goto err;
  1205. }
  1206. #ifdef HAVE_PWD_H
  1207. if (options->User) {
  1208. pw = tor_getpwnam(options->User);
  1209. struct stat st;
  1210. if (pw == NULL) {
  1211. log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
  1212. address, options->User);
  1213. goto err;
  1214. } else if (fstat(s, &st) == 0 &&
  1215. st.st_uid == pw->pw_uid && st.st_gid == pw->pw_gid) {
  1216. /* No change needed */
  1217. } else if (chown(sandbox_intern_string(address),
  1218. pw->pw_uid, pw->pw_gid) < 0) {
  1219. log_warn(LD_NET,"Unable to chown() %s socket: %s.",
  1220. address, strerror(errno));
  1221. goto err;
  1222. }
  1223. }
  1224. #endif /* defined(HAVE_PWD_H) */
  1225. {
  1226. unsigned mode;
  1227. const char *status;
  1228. struct stat st;
  1229. if (port_cfg->is_world_writable) {
  1230. mode = 0666;
  1231. status = "world-writable";
  1232. } else if (port_cfg->is_group_writable) {
  1233. mode = 0660;
  1234. status = "group-writable";
  1235. } else {
  1236. mode = 0600;
  1237. status = "private";
  1238. }
  1239. /* We need to use chmod; fchmod doesn't work on sockets on all
  1240. * platforms. */
  1241. if (fstat(s, &st) == 0 && (st.st_mode & 0777) == mode) {
  1242. /* no change needed */
  1243. } else if (chmod(sandbox_intern_string(address), mode) < 0) {
  1244. log_warn(LD_FS,"Unable to make %s %s.", address, status);
  1245. goto err;
  1246. }
  1247. }
  1248. if (listen(s, SOMAXCONN) < 0) {
  1249. log_warn(LD_NET, "Could not listen on %s: %s", address,
  1250. tor_socket_strerror(tor_socket_errno(s)));
  1251. goto err;
  1252. }
  1253. #endif /* defined(HAVE_SYS_UN_H) */
  1254. } else {
  1255. log_err(LD_BUG, "Got unexpected address family %d.",
  1256. listensockaddr->sa_family);
  1257. tor_assert(0);
  1258. }
  1259. lis_conn = listener_connection_new(type, listensockaddr->sa_family);
  1260. conn = TO_CONN(lis_conn);
  1261. conn->socket_family = listensockaddr->sa_family;
  1262. conn->s = s;
  1263. s = TOR_INVALID_SOCKET; /* Prevent double-close */
  1264. conn->address = tor_strdup(address);
  1265. conn->port = gotPort;
  1266. tor_addr_copy(&conn->addr, &addr);
  1267. memcpy(&lis_conn->entry_cfg, &port_cfg->entry_cfg, sizeof(entry_port_cfg_t));
  1268. if (port_cfg->entry_cfg.isolation_flags) {
  1269. lis_conn->entry_cfg.isolation_flags = port_cfg->entry_cfg.isolation_flags;
  1270. if (port_cfg->entry_cfg.session_group >= 0) {
  1271. lis_conn->entry_cfg.session_group = port_cfg->entry_cfg.session_group;
  1272. } else {
  1273. /* This can wrap after around INT_MAX listeners are opened. But I don't
  1274. * believe that matters, since you would need to open a ridiculous
  1275. * number of listeners while keeping the early ones open before you ever
  1276. * hit this. An OR with a dozen ports open, for example, would have to
  1277. * close and re-open its listeners every second for 4 years nonstop.
  1278. */
  1279. lis_conn->entry_cfg.session_group = global_next_session_group--;
  1280. }
  1281. }
  1282. if (type != CONN_TYPE_AP_LISTENER) {
  1283. lis_conn->entry_cfg.ipv4_traffic = 1;
  1284. lis_conn->entry_cfg.ipv6_traffic = 1;
  1285. lis_conn->entry_cfg.prefer_ipv6 = 0;
  1286. }
  1287. if (connection_add(conn) < 0) { /* no space, forget it */
  1288. log_warn(LD_NET,"connection_add for listener failed. Giving up.");
  1289. goto err;
  1290. }
  1291. log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
  1292. "%s listening on port %u.",
  1293. conn_type_to_string(type), gotPort);
  1294. conn->state = LISTENER_STATE_READY;
  1295. if (start_reading) {
  1296. connection_start_reading(conn);
  1297. } else {
  1298. tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
  1299. dnsserv_configure_listener(conn);
  1300. }
  1301. /*
  1302. * Normal exit; call the OOS handler since connection count just changed;
  1303. * the exhaustion flag will always be zero here though.
  1304. */
  1305. connection_check_oos(get_n_open_sockets(), 0);
  1306. return conn;
  1307. err:
  1308. if (SOCKET_OK(s))
  1309. tor_close_socket(s);
  1310. if (conn)
  1311. connection_free(conn);
  1312. /* Call the OOS handler, indicate if we saw an exhaustion-related error */
  1313. connection_check_oos(get_n_open_sockets(), exhaustion);
  1314. return NULL;
  1315. }
  1316. /** Do basic sanity checking on a newly received socket. Return 0
  1317. * if it looks ok, else return -1.
  1318. *
  1319. * Notably, some TCP stacks can erroneously have accept() return successfully
  1320. * with socklen 0, when the client sends an RST before the accept call (as
  1321. * nmap does). We want to detect that, and not go on with the connection.
  1322. */
  1323. static int
  1324. check_sockaddr(const struct sockaddr *sa, int len, int level)
  1325. {
  1326. int ok = 1;
  1327. if (sa->sa_family == AF_INET) {
  1328. struct sockaddr_in *sin=(struct sockaddr_in*)sa;
  1329. if (len != sizeof(struct sockaddr_in)) {
  1330. log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
  1331. len,(int)sizeof(struct sockaddr_in));
  1332. ok = 0;
  1333. }
  1334. if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
  1335. log_fn(level, LD_NET,
  1336. "Address for new connection has address/port equal to zero.");
  1337. ok = 0;
  1338. }
  1339. } else if (sa->sa_family == AF_INET6) {
  1340. struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
  1341. if (len != sizeof(struct sockaddr_in6)) {
  1342. log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
  1343. len,(int)sizeof(struct sockaddr_in6));
  1344. ok = 0;
  1345. }
  1346. if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
  1347. sin6->sin6_port == 0) {
  1348. log_fn(level, LD_NET,
  1349. "Address for new connection has address/port equal to zero.");
  1350. ok = 0;
  1351. }
  1352. } else if (sa->sa_family == AF_UNIX) {
  1353. ok = 1;
  1354. } else {
  1355. ok = 0;
  1356. }
  1357. return ok ? 0 : -1;
  1358. }
  1359. /** Check whether the socket family from an accepted socket <b>got</b> is the
  1360. * same as the one that <b>listener</b> is waiting for. If it isn't, log
  1361. * a useful message and return -1. Else return 0.
  1362. *
  1363. * This is annoying, but can apparently happen on some Darwins. */
  1364. static int
  1365. check_sockaddr_family_match(sa_family_t got, connection_t *listener)
  1366. {
  1367. if (got != listener->socket_family) {
  1368. log_info(LD_BUG, "A listener connection returned a socket with a "
  1369. "mismatched family. %s for addr_family %d gave us a socket "
  1370. "with address family %d. Dropping.",
  1371. conn_type_to_string(listener->type),
  1372. (int)listener->socket_family,
  1373. (int)got);
  1374. return -1;
  1375. }
  1376. return 0;
  1377. }
  1378. /** The listener connection <b>conn</b> told poll() it wanted to read.
  1379. * Call accept() on conn-\>s, and add the new connection if necessary.
  1380. */
  1381. static int
  1382. connection_handle_listener_read(connection_t *conn, int new_type)
  1383. {
  1384. tor_socket_t news; /* the new socket */
  1385. connection_t *newconn = 0;
  1386. /* information about the remote peer when connecting to other routers */
  1387. struct sockaddr_storage addrbuf;
  1388. struct sockaddr *remote = (struct sockaddr*)&addrbuf;
  1389. /* length of the remote address. Must be whatever accept() needs. */
  1390. socklen_t remotelen = (socklen_t)sizeof(addrbuf);
  1391. const or_options_t *options = get_options();
  1392. tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
  1393. memset(&addrbuf, 0, sizeof(addrbuf));
  1394. news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
  1395. if (!SOCKET_OK(news)) { /* accept() error */
  1396. int e = tor_socket_errno(conn->s);
  1397. if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
  1398. /*
  1399. * they hung up before we could accept(). that's fine.
  1400. *
  1401. * give the OOS handler a chance to run though
  1402. */
  1403. connection_check_oos(get_n_open_sockets(), 0);
  1404. return 0;
  1405. } else if (ERRNO_IS_RESOURCE_LIMIT(e)) {
  1406. warn_too_many_conns();
  1407. /* Exhaustion; tell the OOS handler */
  1408. connection_check_oos(get_n_open_sockets(), 1);
  1409. return 0;
  1410. }
  1411. /* else there was a real error. */
  1412. log_warn(LD_NET,"accept() failed: %s. Closing listener.",
  1413. tor_socket_strerror(e));
  1414. connection_mark_for_close(conn);
  1415. /* Tell the OOS handler about this too */
  1416. connection_check_oos(get_n_open_sockets(), 0);
  1417. return -1;
  1418. }
  1419. log_debug(LD_NET,
  1420. "Connection accepted on socket %d (child of fd %d).",
  1421. (int)news,(int)conn->s);
  1422. /* We accepted a new conn; run OOS handler */
  1423. connection_check_oos(get_n_open_sockets(), 0);
  1424. if (make_socket_reuseable(news) < 0) {
  1425. if (tor_socket_errno(news) == EINVAL) {
  1426. /* This can happen on OSX if we get a badly timed shutdown. */
  1427. log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
  1428. } else {
  1429. log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
  1430. conn_type_to_string(new_type),
  1431. tor_socket_strerror(errno));
  1432. }
  1433. tor_close_socket(news);
  1434. return 0;
  1435. }
  1436. if (options->ConstrainedSockets)
  1437. set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
  1438. if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
  1439. tor_close_socket(news);
  1440. return 0;
  1441. }
  1442. if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6 ||
  1443. (conn->socket_family == AF_UNIX && new_type == CONN_TYPE_AP)) {
  1444. tor_addr_t addr;
  1445. uint16_t port;
  1446. if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
  1447. log_info(LD_NET,
  1448. "accept() returned a strange address; closing connection.");
  1449. tor_close_socket(news);
  1450. return 0;
  1451. }
  1452. tor_addr_from_sockaddr(&addr, remote, &port);
  1453. /* process entrance policies here, before we even create the connection */
  1454. if (new_type == CONN_TYPE_AP) {
  1455. /* check sockspolicy to see if we should accept it */
  1456. if (socks_policy_permits_address(&addr) == 0) {
  1457. log_notice(LD_APP,
  1458. "Denying socks connection from untrusted address %s.",
  1459. fmt_and_decorate_addr(&addr));
  1460. tor_close_socket(news);
  1461. return 0;
  1462. }
  1463. }
  1464. if (new_type == CONN_TYPE_DIR) {
  1465. /* check dirpolicy to see if we should accept it */
  1466. if (dir_policy_permits_address(&addr) == 0) {
  1467. log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
  1468. fmt_and_decorate_addr(&addr));
  1469. tor_close_socket(news);
  1470. return 0;
  1471. }
  1472. }
  1473. if (new_type == CONN_TYPE_OR) {
  1474. /* Assess with the connection DoS mitigation subsystem if this address
  1475. * can open a new connection. */
  1476. if (dos_conn_addr_get_defense_type(&addr) == DOS_CONN_DEFENSE_CLOSE) {
  1477. tor_close_socket(news);
  1478. return 0;
  1479. }
  1480. }
  1481. newconn = connection_new(new_type, conn->socket_family);
  1482. newconn->s = news;
  1483. /* remember the remote address */
  1484. tor_addr_copy(&newconn->addr, &addr);
  1485. if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
  1486. newconn->port = 0;
  1487. newconn->address = tor_strdup(conn->address);
  1488. } else {
  1489. newconn->port = port;
  1490. newconn->address = tor_addr_to_str_dup(&addr);
  1491. }
  1492. if (new_type == CONN_TYPE_AP && conn->socket_family != AF_UNIX) {
  1493. log_info(LD_NET, "New SOCKS connection opened from %s.",
  1494. fmt_and_decorate_addr(&addr));
  1495. }
  1496. if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
  1497. log_info(LD_NET, "New SOCKS AF_UNIX connection opened");
  1498. }
  1499. if (new_type == CONN_TYPE_CONTROL) {
  1500. log_notice(LD_CONTROL, "New control connection opened from %s.",
  1501. fmt_and_decorate_addr(&addr));
  1502. }
  1503. } else if (conn->socket_family == AF_UNIX && conn->type != CONN_TYPE_AP) {
  1504. tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
  1505. tor_assert(new_type == CONN_TYPE_CONTROL);
  1506. log_notice(LD_CONTROL, "New control connection opened.");
  1507. newconn = connection_new(new_type, conn->socket_family);
  1508. newconn->s = news;
  1509. /* remember the remote address -- do we have anything sane to put here? */
  1510. tor_addr_make_unspec(&newconn->addr);
  1511. newconn->port = 1;
  1512. newconn->address = tor_strdup(conn->address);
  1513. } else {
  1514. tor_assert(0);
  1515. };
  1516. if (connection_add(newconn) < 0) { /* no space, forget it */
  1517. connection_free(newconn);
  1518. return 0; /* no need to tear down the parent */
  1519. }
  1520. if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
  1521. if (! newconn->marked_for_close)
  1522. connection_mark_for_close(newconn);
  1523. return 0;
  1524. }
  1525. return 0;
  1526. }
  1527. /** Initialize states for newly accepted connection <b>conn</b>.
  1528. *
  1529. * If conn is an OR, start the TLS handshake.
  1530. *
  1531. * If conn is a transparent AP, get its original destination
  1532. * and place it in circuit_wait.
  1533. *
  1534. * The <b>listener</b> parameter is only used for AP connections.
  1535. */
  1536. int
  1537. connection_init_accepted_conn(connection_t *conn,
  1538. const listener_connection_t *listener)
  1539. {
  1540. int rv;
  1541. connection_start_reading(conn);
  1542. switch (conn->type) {
  1543. case CONN_TYPE_EXT_OR:
  1544. /* Initiate Extended ORPort authentication. */
  1545. return connection_ext_or_start_auth(TO_OR_CONN(conn));
  1546. case CONN_TYPE_OR:
  1547. control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
  1548. rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
  1549. if (rv < 0) {
  1550. connection_or_close_for_error(TO_OR_CONN(conn), 0);
  1551. }
  1552. return rv;
  1553. break;
  1554. case CONN_TYPE_AP:
  1555. memcpy(&TO_ENTRY_CONN(conn)->entry_cfg, &listener->entry_cfg,
  1556. sizeof(entry_port_cfg_t));
  1557. TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
  1558. TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
  1559. switch (TO_CONN(listener)->type) {
  1560. case CONN_TYPE_AP_LISTENER:
  1561. conn->state = AP_CONN_STATE_SOCKS_WAIT;
  1562. TO_ENTRY_CONN(conn)->socks_request->socks_prefer_no_auth =
  1563. listener->entry_cfg.socks_prefer_no_auth;
  1564. break;
  1565. case CONN_TYPE_AP_TRANS_LISTENER:
  1566. TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
  1567. /* XXXX028 -- is this correct still, with the addition of
  1568. * pending_entry_connections ? */
  1569. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  1570. return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
  1571. case CONN_TYPE_AP_NATD_LISTENER:
  1572. TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
  1573. conn->state = AP_CONN_STATE_NATD_WAIT;
  1574. break;
  1575. case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
  1576. conn->state = AP_CONN_STATE_HTTP_CONNECT_WAIT;
  1577. }
  1578. break;
  1579. case CONN_TYPE_DIR:
  1580. conn->purpose = DIR_PURPOSE_SERVER;
  1581. conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
  1582. break;
  1583. case CONN_TYPE_CONTROL:
  1584. conn->state = CONTROL_CONN_STATE_NEEDAUTH;
  1585. break;
  1586. }
  1587. return 0;
  1588. }
  1589. /** Take conn, make a nonblocking socket; try to connect to
  1590. * sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if
  1591. * applicable put your best guess about errno into *<b>socket_error</b>.
  1592. * If connected return 1, if EAGAIN return 0.
  1593. */
  1594. MOCK_IMPL(STATIC int,
  1595. connection_connect_sockaddr,(connection_t *conn,
  1596. const struct sockaddr *sa,
  1597. socklen_t sa_len,
  1598. const struct sockaddr *bindaddr,
  1599. socklen_t bindaddr_len,
  1600. int *socket_error))
  1601. {
  1602. tor_socket_t s;
  1603. int inprogress = 0;
  1604. const or_options_t *options = get_options();
  1605. tor_assert(conn);
  1606. tor_assert(sa);
  1607. tor_assert(socket_error);
  1608. if (get_options()->DisableNetwork) {
  1609. /* We should never even try to connect anyplace if DisableNetwork is set.
  1610. * Warn if we do, and refuse to make the connection.
  1611. *
  1612. * We only check DisableNetwork here, not we_are_hibernating(), since
  1613. * we'll still try to fulfill client requests sometimes in the latter case
  1614. * (if it is soft hibernation) */
  1615. static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
  1616. *socket_error = SOCK_ERRNO(ENETUNREACH);
  1617. log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
  1618. "Tried to open a socket with DisableNetwork set.");
  1619. tor_fragile_assert();
  1620. return -1;
  1621. }
  1622. const int protocol_family = sa->sa_family;
  1623. const int proto = (sa->sa_family == AF_INET6 ||
  1624. sa->sa_family == AF_INET) ? IPPROTO_TCP : 0;
  1625. s = tor_open_socket_nonblocking(protocol_family, SOCK_STREAM, proto);
  1626. if (! SOCKET_OK(s)) {
  1627. /*
  1628. * Early OOS handler calls; it matters if it's an exhaustion-related
  1629. * error or not.
  1630. */
  1631. *socket_error = tor_socket_errno(s);
  1632. if (ERRNO_IS_RESOURCE_LIMIT(*socket_error)) {
  1633. warn_too_many_conns();
  1634. connection_check_oos(get_n_open_sockets(), 1);
  1635. } else {
  1636. log_warn(LD_NET,"Error creating network socket: %s",
  1637. tor_socket_strerror(*socket_error));
  1638. connection_check_oos(get_n_open_sockets(), 0);
  1639. }
  1640. return -1;
  1641. }
  1642. if (make_socket_reuseable(s) < 0) {
  1643. log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
  1644. tor_socket_strerror(errno));
  1645. }
  1646. /*
  1647. * We've got the socket open; give the OOS handler a chance to check
  1648. * against configured maximum socket number, but tell it no exhaustion
  1649. * failure.
  1650. */
  1651. connection_check_oos(get_n_open_sockets(), 0);
  1652. if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
  1653. *socket_error = tor_socket_errno(s);
  1654. log_warn(LD_NET,"Error binding network socket: %s",
  1655. tor_socket_strerror(*socket_error));
  1656. tor_close_socket(s);
  1657. return -1;
  1658. }
  1659. tor_assert(options);
  1660. if (options->ConstrainedSockets)
  1661. set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
  1662. if (connect(s, sa, sa_len) < 0) {
  1663. int e = tor_socket_errno(s);
  1664. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  1665. /* yuck. kill it. */
  1666. *socket_error = e;
  1667. log_info(LD_NET,
  1668. "connect() to socket failed: %s",
  1669. tor_socket_strerror(e));
  1670. tor_close_socket(s);
  1671. return -1;
  1672. } else {
  1673. inprogress = 1;
  1674. }
  1675. }
  1676. /* it succeeded. we're connected. */
  1677. log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
  1678. "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
  1679. inprogress ? "in progress" : "established", s);
  1680. conn->s = s;
  1681. if (connection_add_connecting(conn) < 0) {
  1682. /* no space, forget it */
  1683. *socket_error = SOCK_ERRNO(ENOBUFS);
  1684. return -1;
  1685. }
  1686. return inprogress ? 0 : 1;
  1687. }
  1688. /* Log a message if connection attempt is made when IPv4 or IPv6 is disabled.
  1689. * Log a less severe message if we couldn't conform to ClientPreferIPv6ORPort
  1690. * or ClientPreferIPv6ORPort. */
  1691. static void
  1692. connection_connect_log_client_use_ip_version(const connection_t *conn)
  1693. {
  1694. const or_options_t *options = get_options();
  1695. /* Only clients care about ClientUseIPv4/6, bail out early on servers, and
  1696. * on connections we don't care about */
  1697. if (server_mode(options) || !conn || conn->type == CONN_TYPE_EXIT) {
  1698. return;
  1699. }
  1700. /* We're only prepared to log OR and DIR connections here */
  1701. if (conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR) {
  1702. return;
  1703. }
  1704. const int must_ipv4 = !fascist_firewall_use_ipv6(options);
  1705. const int must_ipv6 = (options->ClientUseIPv4 == 0);
  1706. const int pref_ipv6 = (conn->type == CONN_TYPE_OR
  1707. ? fascist_firewall_prefer_ipv6_orport(options)
  1708. : fascist_firewall_prefer_ipv6_dirport(options));
  1709. tor_addr_t real_addr;
  1710. tor_addr_make_null(&real_addr, AF_UNSPEC);
  1711. /* OR conns keep the original address in real_addr, as addr gets overwritten
  1712. * with the descriptor address */
  1713. if (conn->type == CONN_TYPE_OR) {
  1714. const or_connection_t *or_conn = TO_OR_CONN((connection_t *)conn);
  1715. tor_addr_copy(&real_addr, &or_conn->real_addr);
  1716. } else if (conn->type == CONN_TYPE_DIR) {
  1717. tor_addr_copy(&real_addr, &conn->addr);
  1718. }
  1719. /* Check if we broke a mandatory address family restriction */
  1720. if ((must_ipv4 && tor_addr_family(&real_addr) == AF_INET6)
  1721. || (must_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
  1722. static int logged_backtrace = 0;
  1723. log_info(LD_BUG, "Outgoing %s connection to %s violated ClientUseIPv%s 0.",
  1724. conn->type == CONN_TYPE_OR ? "OR" : "Dir",
  1725. fmt_addr(&real_addr),
  1726. options->ClientUseIPv4 == 0 ? "4" : "6");
  1727. if (!logged_backtrace) {
  1728. log_backtrace(LOG_INFO, LD_BUG, "Address came from");
  1729. logged_backtrace = 1;
  1730. }
  1731. }
  1732. /* Bridges are allowed to break IPv4/IPv6 ORPort preferences to connect to
  1733. * the node's configured address when ClientPreferIPv6ORPort is auto */
  1734. if (options->UseBridges && conn->type == CONN_TYPE_OR
  1735. && options->ClientPreferIPv6ORPort == -1) {
  1736. return;
  1737. }
  1738. /* Check if we couldn't satisfy an address family preference */
  1739. if ((!pref_ipv6 && tor_addr_family(&real_addr) == AF_INET6)
  1740. || (pref_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
  1741. log_info(LD_NET, "Outgoing connection to %s doesn't satisfy "
  1742. "ClientPreferIPv6%sPort %d, with ClientUseIPv4 %d, and "
  1743. "fascist_firewall_use_ipv6 %d (ClientUseIPv6 %d and UseBridges "
  1744. "%d).",
  1745. fmt_addr(&real_addr),
  1746. conn->type == CONN_TYPE_OR ? "OR" : "Dir",
  1747. conn->type == CONN_TYPE_OR ? options->ClientPreferIPv6ORPort
  1748. : options->ClientPreferIPv6DirPort,
  1749. options->ClientUseIPv4, fascist_firewall_use_ipv6(options),
  1750. options->ClientUseIPv6, options->UseBridges);
  1751. }
  1752. }
  1753. /** Retrieve the outbound address depending on the protocol (IPv4 or IPv6)
  1754. * and the connection type (relay, exit, ...)
  1755. * Return a socket address or NULL in case nothing is configured.
  1756. **/
  1757. const tor_addr_t *
  1758. conn_get_outbound_address(sa_family_t family,
  1759. const or_options_t *options, unsigned int conn_type)
  1760. {
  1761. const tor_addr_t *ext_addr = NULL;
  1762. int fam_index;
  1763. switch (family) {
  1764. case AF_INET:
  1765. fam_index = 0;
  1766. break;
  1767. case AF_INET6:
  1768. fam_index = 1;
  1769. break;
  1770. default:
  1771. return NULL;
  1772. }
  1773. // If an exit connection, use the exit address (if present)
  1774. if (conn_type == CONN_TYPE_EXIT) {
  1775. if (!tor_addr_is_null(
  1776. &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT][fam_index])) {
  1777. ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT]
  1778. [fam_index];
  1779. } else if (!tor_addr_is_null(
  1780. &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
  1781. [fam_index])) {
  1782. ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
  1783. [fam_index];
  1784. }
  1785. } else { // All non-exit connections
  1786. if (!tor_addr_is_null(
  1787. &options->OutboundBindAddresses[OUTBOUND_ADDR_OR][fam_index])) {
  1788. ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_OR]
  1789. [fam_index];
  1790. } else if (!tor_addr_is_null(
  1791. &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
  1792. [fam_index])) {
  1793. ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
  1794. [fam_index];
  1795. }
  1796. }
  1797. return ext_addr;
  1798. }
  1799. /** Take conn, make a nonblocking socket; try to connect to
  1800. * addr:port (port arrives in *host order*). If fail, return -1 and if
  1801. * applicable put your best guess about errno into *<b>socket_error</b>.
  1802. * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
  1803. *
  1804. * addr:port can be different to conn->addr:conn->port if connecting through
  1805. * a proxy.
  1806. *
  1807. * address is used to make the logs useful.
  1808. *
  1809. * On success, add conn to the list of polled connections.
  1810. */
  1811. int
  1812. connection_connect(connection_t *conn, const char *address,
  1813. const tor_addr_t *addr, uint16_t port, int *socket_error)
  1814. {
  1815. struct sockaddr_storage addrbuf;
  1816. struct sockaddr_storage bind_addr_ss;
  1817. struct sockaddr *bind_addr = NULL;
  1818. struct sockaddr *dest_addr;
  1819. int dest_addr_len, bind_addr_len = 0;
  1820. /* Log if we didn't stick to ClientUseIPv4/6 or ClientPreferIPv6OR/DirPort
  1821. */
  1822. connection_connect_log_client_use_ip_version(conn);
  1823. if (!tor_addr_is_loopback(addr)) {
  1824. const tor_addr_t *ext_addr = NULL;
  1825. ext_addr = conn_get_outbound_address(tor_addr_family(addr), get_options(),
  1826. conn->type);
  1827. if (ext_addr) {
  1828. memset(&bind_addr_ss, 0, sizeof(bind_addr_ss));
  1829. bind_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
  1830. (struct sockaddr *) &bind_addr_ss,
  1831. sizeof(bind_addr_ss));
  1832. if (bind_addr_len == 0) {
  1833. log_warn(LD_NET,
  1834. "Error converting OutboundBindAddress %s into sockaddr. "
  1835. "Ignoring.", fmt_and_decorate_addr(ext_addr));
  1836. } else {
  1837. bind_addr = (struct sockaddr *)&bind_addr_ss;
  1838. }
  1839. }
  1840. }
  1841. memset(&addrbuf,0,sizeof(addrbuf));
  1842. dest_addr = (struct sockaddr*) &addrbuf;
  1843. dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
  1844. tor_assert(dest_addr_len > 0);
  1845. log_debug(LD_NET, "Connecting to %s:%u.",
  1846. escaped_safe_str_client(address), port);
  1847. return connection_connect_sockaddr(conn, dest_addr, dest_addr_len,
  1848. bind_addr, bind_addr_len, socket_error);
  1849. }
  1850. #ifdef HAVE_SYS_UN_H
  1851. /** Take conn, make a nonblocking socket; try to connect to
  1852. * an AF_UNIX socket at socket_path. If fail, return -1 and if applicable
  1853. * put your best guess about errno into *<b>socket_error</b>. Else assign s
  1854. * to conn-\>s: if connected return 1, if EAGAIN return 0.
  1855. *
  1856. * On success, add conn to the list of polled connections.
  1857. */
  1858. int
  1859. connection_connect_unix(connection_t *conn, const char *socket_path,
  1860. int *socket_error)
  1861. {
  1862. struct sockaddr_un dest_addr;
  1863. tor_assert(socket_path);
  1864. /* Check that we'll be able to fit it into dest_addr later */
  1865. if (strlen(socket_path) + 1 > sizeof(dest_addr.sun_path)) {
  1866. log_warn(LD_NET,
  1867. "Path %s is too long for an AF_UNIX socket\n",
  1868. escaped_safe_str_client(socket_path));
  1869. *socket_error = SOCK_ERRNO(ENAMETOOLONG);
  1870. return -1;
  1871. }
  1872. memset(&dest_addr, 0, sizeof(dest_addr));
  1873. dest_addr.sun_family = AF_UNIX;
  1874. strlcpy(dest_addr.sun_path, socket_path, sizeof(dest_addr.sun_path));
  1875. log_debug(LD_NET,
  1876. "Connecting to AF_UNIX socket at %s.",
  1877. escaped_safe_str_client(socket_path));
  1878. return connection_connect_sockaddr(conn,
  1879. (struct sockaddr *)&dest_addr, sizeof(dest_addr),
  1880. NULL, 0, socket_error);
  1881. }
  1882. #endif /* defined(HAVE_SYS_UN_H) */
  1883. /** Convert state number to string representation for logging purposes.
  1884. */
  1885. static const char *
  1886. connection_proxy_state_to_string(int state)
  1887. {
  1888. static const char *unknown = "???";
  1889. static const char *states[] = {
  1890. "PROXY_NONE",
  1891. "PROXY_INFANT",
  1892. "PROXY_HTTPS_WANT_CONNECT_OK",
  1893. "PROXY_SOCKS4_WANT_CONNECT_OK",
  1894. "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
  1895. "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
  1896. "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
  1897. "PROXY_SOCKS5_WANT_CONNECT_OK",
  1898. "PROXY_CONNECTED",
  1899. };
  1900. if (state < PROXY_NONE || state > PROXY_CONNECTED)
  1901. return unknown;
  1902. return states[state];
  1903. }
  1904. /** Returns the global proxy type used by tor. Use this function for
  1905. * logging or high-level purposes, don't use it to fill the
  1906. * <b>proxy_type</b> field of or_connection_t; use the actual proxy
  1907. * protocol instead.*/
  1908. static int
  1909. get_proxy_type(void)
  1910. {
  1911. const or_options_t *options = get_options();
  1912. if (options->ClientTransportPlugin)
  1913. return PROXY_PLUGGABLE;
  1914. else if (options->HTTPSProxy)
  1915. return PROXY_CONNECT;
  1916. else if (options->Socks4Proxy)
  1917. return PROXY_SOCKS4;
  1918. else if (options->Socks5Proxy)
  1919. return PROXY_SOCKS5;
  1920. else
  1921. return PROXY_NONE;
  1922. }
  1923. /* One byte for the version, one for the command, two for the
  1924. port, and four for the addr... and, one more for the
  1925. username NUL: */
  1926. #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
  1927. /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
  1928. * for conn->addr:conn->port, authenticating with the auth details given
  1929. * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
  1930. * support authentication.
  1931. *
  1932. * Returns -1 if conn->addr is incompatible with the proxy protocol, and
  1933. * 0 otherwise.
  1934. *
  1935. * Use connection_read_proxy_handshake() to complete the handshake.
  1936. */
  1937. int
  1938. connection_proxy_connect(connection_t *conn, int type)
  1939. {
  1940. const or_options_t *options;
  1941. tor_assert(conn);
  1942. options = get_options();
  1943. switch (type) {
  1944. case PROXY_CONNECT: {
  1945. char buf[1024];
  1946. char *base64_authenticator=NULL;
  1947. const char *authenticator = options->HTTPSProxyAuthenticator;
  1948. /* Send HTTP CONNECT and authentication (if available) in
  1949. * one request */
  1950. if (authenticator) {
  1951. base64_authenticator = alloc_http_authenticator(authenticator);
  1952. if (!base64_authenticator)
  1953. log_warn(LD_OR, "Encoding https authenticator failed");
  1954. }
  1955. if (base64_authenticator) {
  1956. const char *addrport = fmt_addrport(&conn->addr, conn->port);
  1957. tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
  1958. "Host: %s\r\n"
  1959. "Proxy-Authorization: Basic %s\r\n\r\n",
  1960. addrport,
  1961. addrport,
  1962. base64_authenticator);
  1963. tor_free(base64_authenticator);
  1964. } else {
  1965. tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
  1966. fmt_addrport(&conn->addr, conn->port));
  1967. }
  1968. connection_buf_add(buf, strlen(buf), conn);
  1969. conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
  1970. break;
  1971. }
  1972. case PROXY_SOCKS4: {
  1973. unsigned char *buf;
  1974. uint16_t portn;
  1975. uint32_t ip4addr;
  1976. size_t buf_size = 0;
  1977. char *socks_args_string = NULL;
  1978. /* Send a SOCKS4 connect request */
  1979. if (tor_addr_family(&conn->addr) != AF_INET) {
  1980. log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
  1981. return -1;
  1982. }
  1983. { /* If we are here because we are trying to connect to a
  1984. pluggable transport proxy, check if we have any SOCKS
  1985. arguments to transmit. If we do, compress all arguments to
  1986. a single string in 'socks_args_string': */
  1987. if (get_proxy_type() == PROXY_PLUGGABLE) {
  1988. socks_args_string =
  1989. pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
  1990. if (socks_args_string)
  1991. log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
  1992. socks_args_string);
  1993. }
  1994. }
  1995. { /* Figure out the buffer size we need for the SOCKS message: */
  1996. buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
  1997. /* If we have a SOCKS argument string, consider its size when
  1998. calculating the buffer size: */
  1999. if (socks_args_string)
  2000. buf_size += strlen(socks_args_string);
  2001. }
  2002. buf = tor_malloc_zero(buf_size);
  2003. ip4addr = tor_addr_to_ipv4n(&conn->addr);
  2004. portn = htons(conn->port);
  2005. buf[0] = 4; /* version */
  2006. buf[1] = SOCKS_COMMAND_CONNECT; /* command */
  2007. memcpy(buf + 2, &portn, 2); /* port */
  2008. memcpy(buf + 4, &ip4addr, 4); /* addr */
  2009. /* Next packet field is the userid. If we have pluggable
  2010. transport SOCKS arguments, we have to embed them
  2011. there. Otherwise, we use an empty userid. */
  2012. if (socks_args_string) { /* place the SOCKS args string: */
  2013. tor_assert(strlen(socks_args_string) > 0);
  2014. tor_assert(buf_size >=
  2015. SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
  2016. strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
  2017. tor_free(socks_args_string);
  2018. } else {
  2019. buf[8] = 0; /* no userid */
  2020. }
  2021. connection_buf_add((char *)buf, buf_size, conn);
  2022. tor_free(buf);
  2023. conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
  2024. break;
  2025. }
  2026. case PROXY_SOCKS5: {
  2027. unsigned char buf[4]; /* fields: vers, num methods, method list */
  2028. /* Send a SOCKS5 greeting (connect request must wait) */
  2029. buf[0] = 5; /* version */
  2030. /* We have to use SOCKS5 authentication, if we have a
  2031. Socks5ProxyUsername or if we want to pass arguments to our
  2032. pluggable transport proxy: */
  2033. if ((options->Socks5ProxyUsername) ||
  2034. (get_proxy_type() == PROXY_PLUGGABLE &&
  2035. (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
  2036. /* number of auth methods */
  2037. buf[1] = 2;
  2038. buf[2] = 0x00; /* no authentication */
  2039. buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
  2040. conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
  2041. } else {
  2042. buf[1] = 1;
  2043. buf[2] = 0x00; /* no authentication */
  2044. conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
  2045. }
  2046. connection_buf_add((char *)buf, 2 + buf[1], conn);
  2047. break;
  2048. }
  2049. default:
  2050. log_err(LD_BUG, "Invalid proxy protocol, %d", type);
  2051. tor_fragile_assert();
  2052. return -1;
  2053. }
  2054. log_debug(LD_NET, "set state %s",
  2055. connection_proxy_state_to_string(conn->proxy_state));
  2056. return 0;
  2057. }
  2058. /** Read conn's inbuf. If the http response from the proxy is all
  2059. * here, make sure it's good news, then return 1. If it's bad news,
  2060. * return -1. Else return 0 and hope for better luck next time.
  2061. */
  2062. static int
  2063. connection_read_https_proxy_response(connection_t *conn)
  2064. {
  2065. char *headers;
  2066. char *reason=NULL;
  2067. int status_code;
  2068. time_t date_header;
  2069. switch (fetch_from_buf_http(conn->inbuf,
  2070. &headers, MAX_HEADERS_SIZE,
  2071. NULL, NULL, 10000, 0)) {
  2072. case -1: /* overflow */
  2073. log_warn(LD_PROTOCOL,
  2074. "Your https proxy sent back an oversized response. Closing.");
  2075. return -1;
  2076. case 0:
  2077. log_info(LD_NET,"https proxy response not all here yet. Waiting.");
  2078. return 0;
  2079. /* case 1, fall through */
  2080. }
  2081. if (parse_http_response(headers, &status_code, &date_header,
  2082. NULL, &reason) < 0) {
  2083. log_warn(LD_NET,
  2084. "Unparseable headers from proxy (connecting to '%s'). Closing.",
  2085. conn->address);
  2086. tor_free(headers);
  2087. return -1;
  2088. }
  2089. tor_free(headers);
  2090. if (!reason) reason = tor_strdup("[no reason given]");
  2091. if (status_code == 200) {
  2092. log_info(LD_NET,
  2093. "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
  2094. conn->address, escaped(reason));
  2095. tor_free(reason);
  2096. return 1;
  2097. }
  2098. /* else, bad news on the status code */
  2099. switch (status_code) {
  2100. case 403:
  2101. log_warn(LD_NET,
  2102. "The https proxy refused to allow connection to %s "
  2103. "(status code %d, %s). Closing.",
  2104. conn->address, status_code, escaped(reason));
  2105. break;
  2106. default:
  2107. log_warn(LD_NET,
  2108. "The https proxy sent back an unexpected status code %d (%s). "
  2109. "Closing.",
  2110. status_code, escaped(reason));
  2111. break;
  2112. }
  2113. tor_free(reason);
  2114. return -1;
  2115. }
  2116. /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
  2117. * and <b>conn->port</b> into the request.
  2118. */
  2119. static void
  2120. connection_send_socks5_connect(connection_t *conn)
  2121. {
  2122. unsigned char buf[1024];
  2123. size_t reqsize = 6;
  2124. uint16_t port = htons(conn->port);
  2125. buf[0] = 5; /* version */
  2126. buf[1] = SOCKS_COMMAND_CONNECT; /* command */
  2127. buf[2] = 0; /* reserved */
  2128. if (tor_addr_family(&conn->addr) == AF_INET) {
  2129. uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
  2130. buf[3] = 1;
  2131. reqsize += 4;
  2132. memcpy(buf + 4, &addr, 4);
  2133. memcpy(buf + 8, &port, 2);
  2134. } else { /* AF_INET6 */
  2135. buf[3] = 4;
  2136. reqsize += 16;
  2137. memcpy(buf + 4, tor_addr_to_in6_addr8(&conn->addr), 16);
  2138. memcpy(buf + 20, &port, 2);
  2139. }
  2140. connection_buf_add((char *)buf, reqsize, conn);
  2141. conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
  2142. }
  2143. /** Wrapper around fetch_from_buf_socks_client: see that functions
  2144. * for documentation of its behavior. */
  2145. static int
  2146. connection_fetch_from_buf_socks_client(connection_t *conn,
  2147. int state, char **reason)
  2148. {
  2149. return fetch_from_buf_socks_client(conn->inbuf, state, reason);
  2150. }
  2151. /** Call this from connection_*_process_inbuf() to advance the proxy
  2152. * handshake.
  2153. *
  2154. * No matter what proxy protocol is used, if this function returns 1, the
  2155. * handshake is complete, and the data remaining on inbuf may contain the
  2156. * start of the communication with the requested server.
  2157. *
  2158. * Returns 0 if the current buffer contains an incomplete response, and -1
  2159. * on error.
  2160. */
  2161. int
  2162. connection_read_proxy_handshake(connection_t *conn)
  2163. {
  2164. int ret = 0;
  2165. char *reason = NULL;
  2166. log_debug(LD_NET, "enter state %s",
  2167. connection_proxy_state_to_string(conn->proxy_state));
  2168. switch (conn->proxy_state) {
  2169. case PROXY_HTTPS_WANT_CONNECT_OK:
  2170. ret = connection_read_https_proxy_response(conn);
  2171. if (ret == 1)
  2172. conn->proxy_state = PROXY_CONNECTED;
  2173. break;
  2174. case PROXY_SOCKS4_WANT_CONNECT_OK:
  2175. ret = connection_fetch_from_buf_socks_client(conn,
  2176. conn->proxy_state,
  2177. &reason);
  2178. if (ret == 1)
  2179. conn->proxy_state = PROXY_CONNECTED;
  2180. break;
  2181. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  2182. ret = connection_fetch_from_buf_socks_client(conn,
  2183. conn->proxy_state,
  2184. &reason);
  2185. /* no auth needed, do connect */
  2186. if (ret == 1) {
  2187. connection_send_socks5_connect(conn);
  2188. ret = 0;
  2189. }
  2190. break;
  2191. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  2192. ret = connection_fetch_from_buf_socks_client(conn,
  2193. conn->proxy_state,
  2194. &reason);
  2195. /* send auth if needed, otherwise do connect */
  2196. if (ret == 1) {
  2197. connection_send_socks5_connect(conn);
  2198. ret = 0;
  2199. } else if (ret == 2) {
  2200. unsigned char buf[1024];
  2201. size_t reqsize, usize, psize;
  2202. const char *user, *pass;
  2203. char *socks_args_string = NULL;
  2204. if (get_proxy_type() == PROXY_PLUGGABLE) {
  2205. socks_args_string =
  2206. pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
  2207. if (!socks_args_string) {
  2208. log_warn(LD_NET, "Could not create SOCKS args string.");
  2209. ret = -1;
  2210. break;
  2211. }
  2212. log_debug(LD_NET, "SOCKS5 arguments: %s", socks_args_string);
  2213. tor_assert(strlen(socks_args_string) > 0);
  2214. tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
  2215. if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
  2216. user = socks_args_string;
  2217. usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
  2218. pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
  2219. psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
  2220. } else {
  2221. user = socks_args_string;
  2222. usize = strlen(socks_args_string);
  2223. pass = "\0";
  2224. psize = 1;
  2225. }
  2226. } else if (get_options()->Socks5ProxyUsername) {
  2227. user = get_options()->Socks5ProxyUsername;
  2228. pass = get_options()->Socks5ProxyPassword;
  2229. tor_assert(user && pass);
  2230. usize = strlen(user);
  2231. psize = strlen(pass);
  2232. } else {
  2233. log_err(LD_BUG, "We entered %s for no reason!", __func__);
  2234. tor_fragile_assert();
  2235. ret = -1;
  2236. break;
  2237. }
  2238. /* Username and password lengths should have been checked
  2239. above and during torrc parsing. */
  2240. tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
  2241. psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
  2242. reqsize = 3 + usize + psize;
  2243. buf[0] = 1; /* negotiation version */
  2244. buf[1] = usize;
  2245. memcpy(buf + 2, user, usize);
  2246. buf[2 + usize] = psize;
  2247. memcpy(buf + 3 + usize, pass, psize);
  2248. if (socks_args_string)
  2249. tor_free(socks_args_string);
  2250. connection_buf_add((char *)buf, reqsize, conn);
  2251. conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
  2252. ret = 0;
  2253. }
  2254. break;
  2255. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  2256. ret = connection_fetch_from_buf_socks_client(conn,
  2257. conn->proxy_state,
  2258. &reason);
  2259. /* send the connect request */
  2260. if (ret == 1) {
  2261. connection_send_socks5_connect(conn);
  2262. ret = 0;
  2263. }
  2264. break;
  2265. case PROXY_SOCKS5_WANT_CONNECT_OK:
  2266. ret = connection_fetch_from_buf_socks_client(conn,
  2267. conn->proxy_state,
  2268. &reason);
  2269. if (ret == 1)
  2270. conn->proxy_state = PROXY_CONNECTED;
  2271. break;
  2272. default:
  2273. log_err(LD_BUG, "Invalid proxy_state for reading, %d",
  2274. conn->proxy_state);
  2275. tor_fragile_assert();
  2276. ret = -1;
  2277. break;
  2278. }
  2279. log_debug(LD_NET, "leaving state %s",
  2280. connection_proxy_state_to_string(conn->proxy_state));
  2281. if (ret < 0) {
  2282. if (reason) {
  2283. log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
  2284. conn->address, conn->port, escaped(reason));
  2285. tor_free(reason);
  2286. } else {
  2287. log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
  2288. conn->address, conn->port);
  2289. }
  2290. } else if (ret == 1) {
  2291. log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
  2292. conn->address, conn->port);
  2293. }
  2294. return ret;
  2295. }
  2296. /** Given a list of listener connections in <b>old_conns</b>, and list of
  2297. * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
  2298. * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
  2299. *
  2300. * Remove from <b>old_conns</b> every connection that has a corresponding
  2301. * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
  2302. * launch.
  2303. *
  2304. * If <b>control_listeners_only</b> is true, then we only open control
  2305. * listeners, and we do not remove any noncontrol listeners from old_conns.
  2306. *
  2307. * Return 0 on success, -1 on failure.
  2308. **/
  2309. static int
  2310. retry_listener_ports(smartlist_t *old_conns,
  2311. const smartlist_t *ports,
  2312. smartlist_t *new_conns,
  2313. int control_listeners_only)
  2314. {
  2315. smartlist_t *launch = smartlist_new();
  2316. int r = 0;
  2317. if (control_listeners_only) {
  2318. SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
  2319. if (p->type == CONN_TYPE_CONTROL_LISTENER)
  2320. smartlist_add(launch, p);
  2321. });
  2322. } else {
  2323. smartlist_add_all(launch, ports);
  2324. }
  2325. /* Iterate through old_conns, comparing it to launch: remove from both lists
  2326. * each pair of elements that corresponds to the same port. */
  2327. SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
  2328. const port_cfg_t *found_port = NULL;
  2329. /* Okay, so this is a listener. Is it configured? */
  2330. SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
  2331. if (conn->type != wanted->type)
  2332. continue;
  2333. if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
  2334. (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
  2335. continue;
  2336. if (wanted->server_cfg.no_listen)
  2337. continue; /* We don't want to open a listener for this one */
  2338. if (wanted->is_unix_addr) {
  2339. if (conn->socket_family == AF_UNIX &&
  2340. !strcmp(wanted->unix_addr, conn->address)) {
  2341. found_port = wanted;
  2342. break;
  2343. }
  2344. } else {
  2345. int port_matches;
  2346. if (wanted->port == CFG_AUTO_PORT) {
  2347. port_matches = 1;
  2348. } else {
  2349. port_matches = (wanted->port == conn->port);
  2350. }
  2351. if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
  2352. found_port = wanted;
  2353. break;
  2354. }
  2355. }
  2356. } SMARTLIST_FOREACH_END(wanted);
  2357. if (found_port) {
  2358. /* This listener is already running; we don't need to launch it. */
  2359. //log_debug(LD_NET, "Already have %s on %s:%d",
  2360. // conn_type_to_string(found_port->type), conn->address, conn->port);
  2361. smartlist_remove(launch, found_port);
  2362. /* And we can remove the connection from old_conns too. */
  2363. SMARTLIST_DEL_CURRENT(old_conns, conn);
  2364. }
  2365. } SMARTLIST_FOREACH_END(conn);
  2366. /* Now open all the listeners that are configured but not opened. */
  2367. SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
  2368. struct sockaddr *listensockaddr;
  2369. socklen_t listensocklen = 0;
  2370. char *address=NULL;
  2371. connection_t *conn;
  2372. int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
  2373. tor_assert(real_port <= UINT16_MAX);
  2374. if (port->server_cfg.no_listen)
  2375. continue;
  2376. #ifndef _WIN32
  2377. /* We don't need to be root to create a UNIX socket, so defer until after
  2378. * setuid. */
  2379. const or_options_t *options = get_options();
  2380. if (port->is_unix_addr && !geteuid() && (options->User) &&
  2381. strcmp(options->User, "root"))
  2382. continue;
  2383. #endif /* !defined(_WIN32) */
  2384. if (port->is_unix_addr) {
  2385. listensockaddr = (struct sockaddr *)
  2386. create_unix_sockaddr(port->unix_addr,
  2387. &address, &listensocklen);
  2388. } else {
  2389. listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
  2390. listensocklen = tor_addr_to_sockaddr(&port->addr,
  2391. real_port,
  2392. listensockaddr,
  2393. sizeof(struct sockaddr_storage));
  2394. address = tor_addr_to_str_dup(&port->addr);
  2395. }
  2396. if (listensockaddr) {
  2397. conn = connection_listener_new(listensockaddr, listensocklen,
  2398. port->type, address, port);
  2399. tor_free(listensockaddr);
  2400. tor_free(address);
  2401. } else {
  2402. conn = NULL;
  2403. }
  2404. if (!conn) {
  2405. r = -1;
  2406. } else {
  2407. if (new_conns)
  2408. smartlist_add(new_conns, conn);
  2409. }
  2410. } SMARTLIST_FOREACH_END(port);
  2411. smartlist_free(launch);
  2412. return r;
  2413. }
  2414. /** Launch listeners for each port you should have open. Only launch
  2415. * listeners who are not already open, and only close listeners we no longer
  2416. * want.
  2417. *
  2418. * Add all old conns that should be closed to <b>replaced_conns</b>.
  2419. * Add all new connections to <b>new_conns</b>.
  2420. *
  2421. * If <b>close_all_noncontrol</b> is true, then we only open control
  2422. * listeners, and we close all other listeners.
  2423. */
  2424. int
  2425. retry_all_listeners(smartlist_t *replaced_conns,
  2426. smartlist_t *new_conns, int close_all_noncontrol)
  2427. {
  2428. smartlist_t *listeners = smartlist_new();
  2429. const or_options_t *options = get_options();
  2430. int retval = 0;
  2431. const uint16_t old_or_port = router_get_advertised_or_port(options);
  2432. const uint16_t old_or_port_ipv6 =
  2433. router_get_advertised_or_port_by_af(options,AF_INET6);
  2434. const uint16_t old_dir_port = router_get_advertised_dir_port(options, 0);
  2435. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  2436. if (connection_is_listener(conn) && !conn->marked_for_close)
  2437. smartlist_add(listeners, conn);
  2438. } SMARTLIST_FOREACH_END(conn);
  2439. if (retry_listener_ports(listeners,
  2440. get_configured_ports(),
  2441. new_conns,
  2442. close_all_noncontrol) < 0)
  2443. retval = -1;
  2444. /* Any members that were still in 'listeners' don't correspond to
  2445. * any configured port. Kill 'em. */
  2446. SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
  2447. log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
  2448. conn_type_to_string(conn->type), conn->address, conn->port);
  2449. if (replaced_conns) {
  2450. smartlist_add(replaced_conns, conn);
  2451. } else {
  2452. connection_close_immediate(conn);
  2453. connection_mark_for_close(conn);
  2454. }
  2455. } SMARTLIST_FOREACH_END(conn);
  2456. smartlist_free(listeners);
  2457. if (old_or_port != router_get_advertised_or_port(options) ||
  2458. old_or_port_ipv6 != router_get_advertised_or_port_by_af(options,
  2459. AF_INET6) ||
  2460. old_dir_port != router_get_advertised_dir_port(options, 0)) {
  2461. /* Our chosen ORPort or DirPort is not what it used to be: the
  2462. * descriptor we had (if any) should be regenerated. (We won't
  2463. * automatically notice this because of changes in the option,
  2464. * since the value could be "auto".) */
  2465. mark_my_descriptor_dirty("Chosen Or/DirPort changed");
  2466. }
  2467. return retval;
  2468. }
  2469. /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
  2470. void
  2471. connection_mark_all_noncontrol_listeners(void)
  2472. {
  2473. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  2474. if (conn->marked_for_close)
  2475. continue;
  2476. if (conn->type == CONN_TYPE_CONTROL_LISTENER)
  2477. continue;
  2478. if (connection_is_listener(conn))
  2479. connection_mark_for_close(conn);
  2480. } SMARTLIST_FOREACH_END(conn);
  2481. }
  2482. /** Mark every external connection not used for controllers for close. */
  2483. void
  2484. connection_mark_all_noncontrol_connections(void)
  2485. {
  2486. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  2487. if (conn->marked_for_close)
  2488. continue;
  2489. switch (conn->type) {
  2490. case CONN_TYPE_CONTROL_LISTENER:
  2491. case CONN_TYPE_CONTROL:
  2492. break;
  2493. case CONN_TYPE_AP:
  2494. connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
  2495. END_STREAM_REASON_HIBERNATING);
  2496. break;
  2497. case CONN_TYPE_OR:
  2498. {
  2499. or_connection_t *orconn = TO_OR_CONN(conn);
  2500. if (orconn->chan) {
  2501. connection_or_close_normally(orconn, 0);
  2502. } else {
  2503. /*
  2504. * There should have been one, but mark for close and hope
  2505. * for the best..
  2506. */
  2507. connection_mark_for_close(conn);
  2508. }
  2509. }
  2510. break;
  2511. default:
  2512. connection_mark_for_close(conn);
  2513. break;
  2514. }
  2515. } SMARTLIST_FOREACH_END(conn);
  2516. }
  2517. /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
  2518. * otherwise.
  2519. * Right now this just checks if it's an internal IP address or an
  2520. * internal connection. We also should, but don't, check if the connection
  2521. * uses pluggable transports, since we should then limit it even if it
  2522. * comes from an internal IP address. */
  2523. static int
  2524. connection_is_rate_limited(connection_t *conn)
  2525. {
  2526. const or_options_t *options = get_options();
  2527. if (conn->linked)
  2528. return 0; /* Internal connection */
  2529. else if (! options->CountPrivateBandwidth &&
  2530. (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
  2531. tor_addr_family(&conn->addr) == AF_UNIX || /* no address */
  2532. tor_addr_is_internal(&conn->addr, 0)))
  2533. return 0; /* Internal address */
  2534. else
  2535. return 1;
  2536. }
  2537. /** Did either global write bucket run dry last second? If so,
  2538. * we are likely to run dry again this second, so be stingy with the
  2539. * tokens we just put in. */
  2540. static int write_buckets_empty_last_second = 0;
  2541. /** How many seconds of no active local circuits will make the
  2542. * connection revert to the "relayed" bandwidth class? */
  2543. #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
  2544. /** Return 1 if <b>conn</b> should use tokens from the "relayed"
  2545. * bandwidth rates, else 0. Currently, only OR conns with bandwidth
  2546. * class 1, and directory conns that are serving data out, count.
  2547. */
  2548. static int
  2549. connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
  2550. {
  2551. if (conn->type == CONN_TYPE_OR &&
  2552. connection_or_client_used(TO_OR_CONN(conn)) +
  2553. CLIENT_IDLE_TIME_FOR_PRIORITY < now)
  2554. return 1;
  2555. if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
  2556. return 1;
  2557. return 0;
  2558. }
  2559. /** Helper function to decide how many bytes out of <b>global_bucket</b>
  2560. * we're willing to use for this transaction. <b>base</b> is the size
  2561. * of a cell on the network; <b>priority</b> says whether we should
  2562. * write many of them or just a few; and <b>conn_bucket</b> (if
  2563. * non-negative) provides an upper limit for our answer. */
  2564. static ssize_t
  2565. connection_bucket_round_robin(int base, int priority,
  2566. ssize_t global_bucket, ssize_t conn_bucket)
  2567. {
  2568. ssize_t at_most;
  2569. ssize_t num_bytes_high = (priority ? 32 : 16) * base;
  2570. ssize_t num_bytes_low = (priority ? 4 : 2) * base;
  2571. /* Do a rudimentary round-robin so one circuit can't hog a connection.
  2572. * Pick at most 32 cells, at least 4 cells if possible, and if we're in
  2573. * the middle pick 1/8 of the available bandwidth. */
  2574. at_most = global_bucket / 8;
  2575. at_most -= (at_most % base); /* round down */
  2576. if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
  2577. at_most = num_bytes_high;
  2578. else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
  2579. at_most = num_bytes_low;
  2580. if (at_most > global_bucket)
  2581. at_most = global_bucket;
  2582. if (conn_bucket >= 0 && at_most > conn_bucket)
  2583. at_most = conn_bucket;
  2584. if (at_most < 0)
  2585. return 0;
  2586. return at_most;
  2587. }
  2588. /** How many bytes at most can we read onto this connection? */
  2589. static ssize_t
  2590. connection_bucket_read_limit(connection_t *conn, time_t now)
  2591. {
  2592. int base = RELAY_PAYLOAD_SIZE;
  2593. int priority = conn->type != CONN_TYPE_DIR;
  2594. int conn_bucket = -1;
  2595. int global_bucket = global_read_bucket;
  2596. if (connection_speaks_cells(conn)) {
  2597. or_connection_t *or_conn = TO_OR_CONN(conn);
  2598. if (conn->state == OR_CONN_STATE_OPEN)
  2599. conn_bucket = or_conn->read_bucket;
  2600. base = get_cell_network_size(or_conn->wide_circ_ids);
  2601. }
  2602. if (!connection_is_rate_limited(conn)) {
  2603. /* be willing to read on local conns even if our buckets are empty */
  2604. return conn_bucket>=0 ? conn_bucket : 1<<14;
  2605. }
  2606. if (connection_counts_as_relayed_traffic(conn, now) &&
  2607. global_relayed_read_bucket <= global_read_bucket)
  2608. global_bucket = global_relayed_read_bucket;
  2609. return connection_bucket_round_robin(base, priority,
  2610. global_bucket, conn_bucket);
  2611. }
  2612. /** How many bytes at most can we write onto this connection? */
  2613. ssize_t
  2614. connection_bucket_write_limit(connection_t *conn, time_t now)
  2615. {
  2616. int base = RELAY_PAYLOAD_SIZE;
  2617. int priority = conn->type != CONN_TYPE_DIR;
  2618. int conn_bucket = (int)conn->outbuf_flushlen;
  2619. int global_bucket = global_write_bucket;
  2620. if (!connection_is_rate_limited(conn)) {
  2621. /* be willing to write to local conns even if our buckets are empty */
  2622. return conn->outbuf_flushlen;
  2623. }
  2624. if (connection_speaks_cells(conn)) {
  2625. /* use the per-conn write limit if it's lower, but if it's less
  2626. * than zero just use zero */
  2627. or_connection_t *or_conn = TO_OR_CONN(conn);
  2628. if (conn->state == OR_CONN_STATE_OPEN)
  2629. if (or_conn->write_bucket < conn_bucket)
  2630. conn_bucket = or_conn->write_bucket >= 0 ?
  2631. or_conn->write_bucket : 0;
  2632. base = get_cell_network_size(or_conn->wide_circ_ids);
  2633. }
  2634. if (connection_counts_as_relayed_traffic(conn, now) &&
  2635. global_relayed_write_bucket <= global_write_bucket)
  2636. global_bucket = global_relayed_write_bucket;
  2637. return connection_bucket_round_robin(base, priority,
  2638. global_bucket, conn_bucket);
  2639. }
  2640. /** Return 1 if the global write buckets are low enough that we
  2641. * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
  2642. * out to <b>conn</b>. Else return 0.
  2643. * Priority was 1 for v1 requests (directories and running-routers),
  2644. * and 2 for v2 requests and later (statuses and descriptors).
  2645. *
  2646. * There are a lot of parameters we could use here:
  2647. * - global_relayed_write_bucket. Low is bad.
  2648. * - global_write_bucket. Low is bad.
  2649. * - bandwidthrate. Low is bad.
  2650. * - bandwidthburst. Not a big factor?
  2651. * - attempt. High is bad.
  2652. * - total bytes queued on outbufs. High is bad. But I'm wary of
  2653. * using this, since a few slow-flushing queues will pump up the
  2654. * number without meaning what we meant to mean. What we really
  2655. * mean is "total directory bytes added to outbufs recently", but
  2656. * that's harder to quantify and harder to keep track of.
  2657. */
  2658. int
  2659. global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
  2660. {
  2661. int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
  2662. global_write_bucket : global_relayed_write_bucket;
  2663. if (authdir_mode(get_options()) && priority>1)
  2664. return 0; /* there's always room to answer v2 if we're an auth dir */
  2665. if (!connection_is_rate_limited(conn))
  2666. return 0; /* local conns don't get limited */
  2667. if (smaller_bucket < (int)attempt)
  2668. return 1; /* not enough space no matter the priority */
  2669. if (write_buckets_empty_last_second)
  2670. return 1; /* we're already hitting our limits, no more please */
  2671. if (priority == 1) { /* old-style v1 query */
  2672. /* Could we handle *two* of these requests within the next two seconds? */
  2673. const or_options_t *options = get_options();
  2674. int64_t can_write = (int64_t)smaller_bucket
  2675. + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
  2676. options->BandwidthRate);
  2677. if (can_write < 2*(int64_t)attempt)
  2678. return 1;
  2679. } else { /* v2 query */
  2680. /* no further constraints yet */
  2681. }
  2682. return 0;
  2683. }
  2684. /** Helper: adjusts our bandwidth history and informs the controller as
  2685. * appropriate, given that we have just read <b>num_read</b> bytes and written
  2686. * <b>num_written</b> bytes on <b>conn</b>. */
  2687. static void
  2688. record_num_bytes_transferred_impl(connection_t *conn,
  2689. time_t now, size_t num_read, size_t num_written)
  2690. {
  2691. /* Count bytes of answering direct and tunneled directory requests */
  2692. if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
  2693. if (num_read > 0)
  2694. rep_hist_note_dir_bytes_read(num_read, now);
  2695. if (num_written > 0)
  2696. rep_hist_note_dir_bytes_written(num_written, now);
  2697. }
  2698. if (!connection_is_rate_limited(conn))
  2699. return; /* local IPs are free */
  2700. if (conn->type == CONN_TYPE_OR)
  2701. rep_hist_note_or_conn_bytes(conn->global_identifier, num_read,
  2702. num_written, now);
  2703. if (num_read > 0) {
  2704. rep_hist_note_bytes_read(num_read, now);
  2705. }
  2706. if (num_written > 0) {
  2707. rep_hist_note_bytes_written(num_written, now);
  2708. }
  2709. if (conn->type == CONN_TYPE_EXIT)
  2710. rep_hist_note_exit_bytes(conn->port, num_written, num_read);
  2711. }
  2712. /** Helper: convert given <b>tvnow</b> time value to milliseconds since
  2713. * midnight. */
  2714. static uint32_t
  2715. msec_since_midnight(const struct timeval *tvnow)
  2716. {
  2717. return (uint32_t)(((tvnow->tv_sec % 86400L) * 1000L) +
  2718. ((uint32_t)tvnow->tv_usec / (uint32_t)1000L));
  2719. }
  2720. /** Helper: return the time in milliseconds since <b>last_empty_time</b>
  2721. * when a bucket ran empty that previously had <b>tokens_before</b> tokens
  2722. * now has <b>tokens_after</b> tokens after refilling at timestamp
  2723. * <b>tvnow</b>, capped at <b>milliseconds_elapsed</b> milliseconds since
  2724. * last refilling that bucket. Return 0 if the bucket has not been empty
  2725. * since the last refill or has not been refilled. */
  2726. uint32_t
  2727. bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
  2728. int tokens_after, int milliseconds_elapsed,
  2729. const struct timeval *tvnow)
  2730. {
  2731. uint32_t result = 0, refilled;
  2732. if (tokens_before <= 0 && tokens_after > tokens_before) {
  2733. refilled = msec_since_midnight(tvnow);
  2734. result = (uint32_t)((refilled + 86400L * 1000L - last_empty_time) %
  2735. (86400L * 1000L));
  2736. if (result > (uint32_t)milliseconds_elapsed)
  2737. result = (uint32_t)milliseconds_elapsed;
  2738. }
  2739. return result;
  2740. }
  2741. /** Check if a bucket which had <b>tokens_before</b> tokens and which got
  2742. * <b>tokens_removed</b> tokens removed at timestamp <b>tvnow</b> has run
  2743. * out of tokens, and if so, note the milliseconds since midnight in
  2744. * <b>timestamp_var</b> for the next TB_EMPTY event. */
  2745. void
  2746. connection_buckets_note_empty_ts(uint32_t *timestamp_var,
  2747. int tokens_before, size_t tokens_removed,
  2748. const struct timeval *tvnow)
  2749. {
  2750. if (tokens_before > 0 && (uint32_t)tokens_before <= tokens_removed)
  2751. *timestamp_var = msec_since_midnight(tvnow);
  2752. }
  2753. /** Last time at which the global or relay buckets were emptied in msec
  2754. * since midnight. */
  2755. static uint32_t global_relayed_read_emptied = 0,
  2756. global_relayed_write_emptied = 0,
  2757. global_read_emptied = 0,
  2758. global_write_emptied = 0;
  2759. /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
  2760. * onto <b>conn</b>. Decrement buckets appropriately. */
  2761. static void
  2762. connection_buckets_decrement(connection_t *conn, time_t now,
  2763. size_t num_read, size_t num_written)
  2764. {
  2765. if (num_written >= INT_MAX || num_read >= INT_MAX) {
  2766. log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
  2767. "connection type=%s, state=%s",
  2768. (unsigned long)num_read, (unsigned long)num_written,
  2769. conn_type_to_string(conn->type),
  2770. conn_state_to_string(conn->type, conn->state));
  2771. tor_assert_nonfatal_unreached();
  2772. if (num_written >= INT_MAX)
  2773. num_written = 1;
  2774. if (num_read >= INT_MAX)
  2775. num_read = 1;
  2776. }
  2777. record_num_bytes_transferred_impl(conn, now, num_read, num_written);
  2778. if (!connection_is_rate_limited(conn))
  2779. return; /* local IPs are free */
  2780. /* If one or more of our token buckets ran dry just now, note the
  2781. * timestamp for TB_EMPTY events. */
  2782. if (get_options()->TestingEnableTbEmptyEvent) {
  2783. struct timeval tvnow;
  2784. tor_gettimeofday_cached(&tvnow);
  2785. if (connection_counts_as_relayed_traffic(conn, now)) {
  2786. connection_buckets_note_empty_ts(&global_relayed_read_emptied,
  2787. global_relayed_read_bucket, num_read, &tvnow);
  2788. connection_buckets_note_empty_ts(&global_relayed_write_emptied,
  2789. global_relayed_write_bucket, num_written, &tvnow);
  2790. }
  2791. connection_buckets_note_empty_ts(&global_read_emptied,
  2792. global_read_bucket, num_read, &tvnow);
  2793. connection_buckets_note_empty_ts(&global_write_emptied,
  2794. global_write_bucket, num_written, &tvnow);
  2795. if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
  2796. or_connection_t *or_conn = TO_OR_CONN(conn);
  2797. connection_buckets_note_empty_ts(&or_conn->read_emptied_time,
  2798. or_conn->read_bucket, num_read, &tvnow);
  2799. connection_buckets_note_empty_ts(&or_conn->write_emptied_time,
  2800. or_conn->write_bucket, num_written, &tvnow);
  2801. }
  2802. }
  2803. if (connection_counts_as_relayed_traffic(conn, now)) {
  2804. global_relayed_read_bucket -= (int)num_read;
  2805. global_relayed_write_bucket -= (int)num_written;
  2806. }
  2807. global_read_bucket -= (int)num_read;
  2808. global_write_bucket -= (int)num_written;
  2809. if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
  2810. TO_OR_CONN(conn)->read_bucket -= (int)num_read;
  2811. TO_OR_CONN(conn)->write_bucket -= (int)num_written;
  2812. }
  2813. }
  2814. /** If we have exhausted our global buckets, or the buckets for conn,
  2815. * stop reading. */
  2816. static void
  2817. connection_consider_empty_read_buckets(connection_t *conn)
  2818. {
  2819. const char *reason;
  2820. if (!connection_is_rate_limited(conn))
  2821. return; /* Always okay. */
  2822. if (global_read_bucket <= 0) {
  2823. reason = "global read bucket exhausted. Pausing.";
  2824. } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
  2825. global_relayed_read_bucket <= 0) {
  2826. reason = "global relayed read bucket exhausted. Pausing.";
  2827. } else if (connection_speaks_cells(conn) &&
  2828. conn->state == OR_CONN_STATE_OPEN &&
  2829. TO_OR_CONN(conn)->read_bucket <= 0) {
  2830. reason = "connection read bucket exhausted. Pausing.";
  2831. } else
  2832. return; /* all good, no need to stop it */
  2833. LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
  2834. conn->read_blocked_on_bw = 1;
  2835. connection_stop_reading(conn);
  2836. }
  2837. /** If we have exhausted our global buckets, or the buckets for conn,
  2838. * stop writing. */
  2839. static void
  2840. connection_consider_empty_write_buckets(connection_t *conn)
  2841. {
  2842. const char *reason;
  2843. if (!connection_is_rate_limited(conn))
  2844. return; /* Always okay. */
  2845. if (global_write_bucket <= 0) {
  2846. reason = "global write bucket exhausted. Pausing.";
  2847. } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
  2848. global_relayed_write_bucket <= 0) {
  2849. reason = "global relayed write bucket exhausted. Pausing.";
  2850. } else if (connection_speaks_cells(conn) &&
  2851. conn->state == OR_CONN_STATE_OPEN &&
  2852. TO_OR_CONN(conn)->write_bucket <= 0) {
  2853. reason = "connection write bucket exhausted. Pausing.";
  2854. } else
  2855. return; /* all good, no need to stop it */
  2856. LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
  2857. conn->write_blocked_on_bw = 1;
  2858. connection_stop_writing(conn);
  2859. }
  2860. /** Initialize the global read bucket to options-\>BandwidthBurst. */
  2861. void
  2862. connection_bucket_init(void)
  2863. {
  2864. const or_options_t *options = get_options();
  2865. /* start it at max traffic */
  2866. global_read_bucket = (int)options->BandwidthBurst;
  2867. global_write_bucket = (int)options->BandwidthBurst;
  2868. if (options->RelayBandwidthRate) {
  2869. global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
  2870. global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
  2871. } else {
  2872. global_relayed_read_bucket = (int)options->BandwidthBurst;
  2873. global_relayed_write_bucket = (int)options->BandwidthBurst;
  2874. }
  2875. }
  2876. /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate per
  2877. * second <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
  2878. * <b>milliseconds_elapsed</b> milliseconds have passed since the last
  2879. * call. */
  2880. static void
  2881. connection_bucket_refill_helper(int *bucket, int rate, int burst,
  2882. int milliseconds_elapsed,
  2883. const char *name)
  2884. {
  2885. int starting_bucket = *bucket;
  2886. if (starting_bucket < burst && milliseconds_elapsed > 0) {
  2887. int64_t incr = (((int64_t)rate) * milliseconds_elapsed) / 1000;
  2888. if ((burst - starting_bucket) < incr) {
  2889. *bucket = burst; /* We would overflow the bucket; just set it to
  2890. * the maximum. */
  2891. } else {
  2892. *bucket += (int)incr;
  2893. if (*bucket > burst || *bucket < starting_bucket) {
  2894. /* If we overflow the burst, or underflow our starting bucket,
  2895. * cap the bucket value to burst. */
  2896. /* XXXX this might be redundant now, but it doesn't show up
  2897. * in profiles. Remove it after analysis. */
  2898. *bucket = burst;
  2899. }
  2900. }
  2901. log_debug(LD_NET,"%s now %d.", name, *bucket);
  2902. }
  2903. }
  2904. /** Time has passed; increment buckets appropriately. */
  2905. void
  2906. connection_bucket_refill(int milliseconds_elapsed, time_t now)
  2907. {
  2908. const or_options_t *options = get_options();
  2909. smartlist_t *conns = get_connection_array();
  2910. int bandwidthrate, bandwidthburst, relayrate, relayburst;
  2911. int prev_global_read = global_read_bucket;
  2912. int prev_global_write = global_write_bucket;
  2913. int prev_relay_read = global_relayed_read_bucket;
  2914. int prev_relay_write = global_relayed_write_bucket;
  2915. struct timeval tvnow; /*< Only used if TB_EMPTY events are enabled. */
  2916. bandwidthrate = (int)options->BandwidthRate;
  2917. bandwidthburst = (int)options->BandwidthBurst;
  2918. if (options->RelayBandwidthRate) {
  2919. relayrate = (int)options->RelayBandwidthRate;
  2920. relayburst = (int)options->RelayBandwidthBurst;
  2921. } else {
  2922. relayrate = bandwidthrate;
  2923. relayburst = bandwidthburst;
  2924. }
  2925. tor_assert(milliseconds_elapsed >= 0);
  2926. write_buckets_empty_last_second =
  2927. global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
  2928. /* refill the global buckets */
  2929. connection_bucket_refill_helper(&global_read_bucket,
  2930. bandwidthrate, bandwidthburst,
  2931. milliseconds_elapsed,
  2932. "global_read_bucket");
  2933. connection_bucket_refill_helper(&global_write_bucket,
  2934. bandwidthrate, bandwidthburst,
  2935. milliseconds_elapsed,
  2936. "global_write_bucket");
  2937. connection_bucket_refill_helper(&global_relayed_read_bucket,
  2938. relayrate, relayburst,
  2939. milliseconds_elapsed,
  2940. "global_relayed_read_bucket");
  2941. connection_bucket_refill_helper(&global_relayed_write_bucket,
  2942. relayrate, relayburst,
  2943. milliseconds_elapsed,
  2944. "global_relayed_write_bucket");
  2945. /* If buckets were empty before and have now been refilled, tell any
  2946. * interested controllers. */
  2947. if (get_options()->TestingEnableTbEmptyEvent) {
  2948. uint32_t global_read_empty_time, global_write_empty_time,
  2949. relay_read_empty_time, relay_write_empty_time;
  2950. tor_gettimeofday_cached(&tvnow);
  2951. global_read_empty_time = bucket_millis_empty(prev_global_read,
  2952. global_read_emptied, global_read_bucket,
  2953. milliseconds_elapsed, &tvnow);
  2954. global_write_empty_time = bucket_millis_empty(prev_global_write,
  2955. global_write_emptied, global_write_bucket,
  2956. milliseconds_elapsed, &tvnow);
  2957. control_event_tb_empty("GLOBAL", global_read_empty_time,
  2958. global_write_empty_time, milliseconds_elapsed);
  2959. relay_read_empty_time = bucket_millis_empty(prev_relay_read,
  2960. global_relayed_read_emptied,
  2961. global_relayed_read_bucket,
  2962. milliseconds_elapsed, &tvnow);
  2963. relay_write_empty_time = bucket_millis_empty(prev_relay_write,
  2964. global_relayed_write_emptied,
  2965. global_relayed_write_bucket,
  2966. milliseconds_elapsed, &tvnow);
  2967. control_event_tb_empty("RELAY", relay_read_empty_time,
  2968. relay_write_empty_time, milliseconds_elapsed);
  2969. }
  2970. /* refill the per-connection buckets */
  2971. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  2972. if (connection_speaks_cells(conn)) {
  2973. or_connection_t *or_conn = TO_OR_CONN(conn);
  2974. int orbandwidthrate = or_conn->bandwidthrate;
  2975. int orbandwidthburst = or_conn->bandwidthburst;
  2976. int prev_conn_read = or_conn->read_bucket;
  2977. int prev_conn_write = or_conn->write_bucket;
  2978. if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
  2979. connection_bucket_refill_helper(&or_conn->read_bucket,
  2980. orbandwidthrate,
  2981. orbandwidthburst,
  2982. milliseconds_elapsed,
  2983. "or_conn->read_bucket");
  2984. }
  2985. if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
  2986. connection_bucket_refill_helper(&or_conn->write_bucket,
  2987. orbandwidthrate,
  2988. orbandwidthburst,
  2989. milliseconds_elapsed,
  2990. "or_conn->write_bucket");
  2991. }
  2992. /* If buckets were empty before and have now been refilled, tell any
  2993. * interested controllers. */
  2994. if (get_options()->TestingEnableTbEmptyEvent) {
  2995. char *bucket;
  2996. uint32_t conn_read_empty_time, conn_write_empty_time;
  2997. tor_asprintf(&bucket, "ORCONN ID="U64_FORMAT,
  2998. U64_PRINTF_ARG(or_conn->base_.global_identifier));
  2999. conn_read_empty_time = bucket_millis_empty(prev_conn_read,
  3000. or_conn->read_emptied_time,
  3001. or_conn->read_bucket,
  3002. milliseconds_elapsed, &tvnow);
  3003. conn_write_empty_time = bucket_millis_empty(prev_conn_write,
  3004. or_conn->write_emptied_time,
  3005. or_conn->write_bucket,
  3006. milliseconds_elapsed, &tvnow);
  3007. control_event_tb_empty(bucket, conn_read_empty_time,
  3008. conn_write_empty_time,
  3009. milliseconds_elapsed);
  3010. tor_free(bucket);
  3011. }
  3012. }
  3013. if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
  3014. && global_read_bucket > 0 /* and we're allowed to read */
  3015. && (!connection_counts_as_relayed_traffic(conn, now) ||
  3016. global_relayed_read_bucket > 0) /* even if we're relayed traffic */
  3017. && (!connection_speaks_cells(conn) ||
  3018. conn->state != OR_CONN_STATE_OPEN ||
  3019. TO_OR_CONN(conn)->read_bucket > 0)) {
  3020. /* and either a non-cell conn or a cell conn with non-empty bucket */
  3021. LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
  3022. "waking up conn (fd %d) for read", (int)conn->s));
  3023. conn->read_blocked_on_bw = 0;
  3024. connection_start_reading(conn);
  3025. }
  3026. if (conn->write_blocked_on_bw == 1
  3027. && global_write_bucket > 0 /* and we're allowed to write */
  3028. && (!connection_counts_as_relayed_traffic(conn, now) ||
  3029. global_relayed_write_bucket > 0) /* even if it's relayed traffic */
  3030. && (!connection_speaks_cells(conn) ||
  3031. conn->state != OR_CONN_STATE_OPEN ||
  3032. TO_OR_CONN(conn)->write_bucket > 0)) {
  3033. LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
  3034. "waking up conn (fd %d) for write", (int)conn->s));
  3035. conn->write_blocked_on_bw = 0;
  3036. connection_start_writing(conn);
  3037. }
  3038. } SMARTLIST_FOREACH_END(conn);
  3039. }
  3040. /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
  3041. * should add another pile of tokens to it?
  3042. */
  3043. static int
  3044. connection_bucket_should_increase(int bucket, or_connection_t *conn)
  3045. {
  3046. tor_assert(conn);
  3047. if (conn->base_.state != OR_CONN_STATE_OPEN)
  3048. return 0; /* only open connections play the rate limiting game */
  3049. if (bucket >= conn->bandwidthburst)
  3050. return 0;
  3051. return 1;
  3052. }
  3053. /** Read bytes from conn-\>s and process them.
  3054. *
  3055. * It calls connection_buf_read_from_socket() to bring in any new bytes,
  3056. * and then calls connection_process_inbuf() to process them.
  3057. *
  3058. * Mark the connection and return -1 if you want to close it, else
  3059. * return 0.
  3060. */
  3061. static int
  3062. connection_handle_read_impl(connection_t *conn)
  3063. {
  3064. ssize_t max_to_read=-1, try_to_read;
  3065. size_t before, n_read = 0;
  3066. int socket_error = 0;
  3067. if (conn->marked_for_close)
  3068. return 0; /* do nothing */
  3069. conn->timestamp_lastread = approx_time();
  3070. switch (conn->type) {
  3071. case CONN_TYPE_OR_LISTENER:
  3072. return connection_handle_listener_read(conn, CONN_TYPE_OR);
  3073. case CONN_TYPE_EXT_OR_LISTENER:
  3074. return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
  3075. case CONN_TYPE_AP_LISTENER:
  3076. case CONN_TYPE_AP_TRANS_LISTENER:
  3077. case CONN_TYPE_AP_NATD_LISTENER:
  3078. case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
  3079. return connection_handle_listener_read(conn, CONN_TYPE_AP);
  3080. case CONN_TYPE_DIR_LISTENER:
  3081. return connection_handle_listener_read(conn, CONN_TYPE_DIR);
  3082. case CONN_TYPE_CONTROL_LISTENER:
  3083. return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
  3084. case CONN_TYPE_AP_DNS_LISTENER:
  3085. /* This should never happen; eventdns.c handles the reads here. */
  3086. tor_fragile_assert();
  3087. return 0;
  3088. }
  3089. loop_again:
  3090. try_to_read = max_to_read;
  3091. tor_assert(!conn->marked_for_close);
  3092. before = buf_datalen(conn->inbuf);
  3093. if (connection_buf_read_from_socket(conn, &max_to_read, &socket_error) < 0) {
  3094. /* There's a read error; kill the connection.*/
  3095. if (conn->type == CONN_TYPE_OR) {
  3096. connection_or_notify_error(TO_OR_CONN(conn),
  3097. socket_error != 0 ?
  3098. errno_to_orconn_end_reason(socket_error) :
  3099. END_OR_CONN_REASON_CONNRESET,
  3100. socket_error != 0 ?
  3101. tor_socket_strerror(socket_error) :
  3102. "(unknown, errno was 0)");
  3103. }
  3104. if (CONN_IS_EDGE(conn)) {
  3105. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  3106. connection_edge_end_errno(edge_conn);
  3107. if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
  3108. /* broken, don't send a socks reply back */
  3109. TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
  3110. }
  3111. }
  3112. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  3113. /*
  3114. * This can bypass normal channel checking since we did
  3115. * connection_or_notify_error() above.
  3116. */
  3117. connection_mark_for_close_internal(conn);
  3118. return -1;
  3119. }
  3120. n_read += buf_datalen(conn->inbuf) - before;
  3121. if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
  3122. /* instruct it not to try to package partial cells. */
  3123. if (connection_process_inbuf(conn, 0) < 0) {
  3124. return -1;
  3125. }
  3126. if (!conn->marked_for_close &&
  3127. connection_is_reading(conn) &&
  3128. !conn->inbuf_reached_eof &&
  3129. max_to_read > 0)
  3130. goto loop_again; /* try reading again, in case more is here now */
  3131. }
  3132. /* one last try, packaging partial cells and all. */
  3133. if (!conn->marked_for_close &&
  3134. connection_process_inbuf(conn, 1) < 0) {
  3135. return -1;
  3136. }
  3137. if (conn->linked_conn) {
  3138. /* The other side's handle_write() will never actually get called, so
  3139. * we need to invoke the appropriate callbacks ourself. */
  3140. connection_t *linked = conn->linked_conn;
  3141. if (n_read) {
  3142. /* Probably a no-op, since linked conns typically don't count for
  3143. * bandwidth rate limiting. But do it anyway so we can keep stats
  3144. * accurately. Note that since we read the bytes from conn, and
  3145. * we're writing the bytes onto the linked connection, we count
  3146. * these as <i>written</i> bytes. */
  3147. connection_buckets_decrement(linked, approx_time(), 0, n_read);
  3148. if (connection_flushed_some(linked) < 0)
  3149. connection_mark_for_close(linked);
  3150. if (!connection_wants_to_flush(linked))
  3151. connection_finished_flushing(linked);
  3152. }
  3153. if (!buf_datalen(linked->outbuf) && conn->active_on_link)
  3154. connection_stop_reading_from_linked_conn(conn);
  3155. }
  3156. /* If we hit the EOF, call connection_reached_eof(). */
  3157. if (!conn->marked_for_close &&
  3158. conn->inbuf_reached_eof &&
  3159. connection_reached_eof(conn) < 0) {
  3160. return -1;
  3161. }
  3162. return 0;
  3163. }
  3164. /* DOCDOC connection_handle_read */
  3165. int
  3166. connection_handle_read(connection_t *conn)
  3167. {
  3168. int res;
  3169. tor_gettimeofday_cache_clear();
  3170. res = connection_handle_read_impl(conn);
  3171. return res;
  3172. }
  3173. /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
  3174. * either directly or via TLS. Reduce the token buckets by the number of bytes
  3175. * read.
  3176. *
  3177. * If *max_to_read is -1, then decide it ourselves, else go with the
  3178. * value passed to us. When returning, if it's changed, subtract the
  3179. * number of bytes we read from *max_to_read.
  3180. *
  3181. * Return -1 if we want to break conn, else return 0.
  3182. */
  3183. static int
  3184. connection_buf_read_from_socket(connection_t *conn, ssize_t *max_to_read,
  3185. int *socket_error)
  3186. {
  3187. int result;
  3188. ssize_t at_most = *max_to_read;
  3189. size_t slack_in_buf, more_to_read;
  3190. size_t n_read = 0, n_written = 0;
  3191. if (at_most == -1) { /* we need to initialize it */
  3192. /* how many bytes are we allowed to read? */
  3193. at_most = connection_bucket_read_limit(conn, approx_time());
  3194. }
  3195. slack_in_buf = buf_slack(conn->inbuf);
  3196. again:
  3197. if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
  3198. more_to_read = at_most - slack_in_buf;
  3199. at_most = slack_in_buf;
  3200. } else {
  3201. more_to_read = 0;
  3202. }
  3203. if (connection_speaks_cells(conn) &&
  3204. conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
  3205. int pending;
  3206. or_connection_t *or_conn = TO_OR_CONN(conn);
  3207. size_t initial_size;
  3208. if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
  3209. conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
  3210. /* continue handshaking even if global token bucket is empty */
  3211. return connection_tls_continue_handshake(or_conn);
  3212. }
  3213. log_debug(LD_NET,
  3214. "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
  3215. " at_most %ld.",
  3216. (int)conn->s,(long)buf_datalen(conn->inbuf),
  3217. tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
  3218. initial_size = buf_datalen(conn->inbuf);
  3219. /* else open, or closing */
  3220. result = buf_read_from_tls(conn->inbuf, or_conn->tls, at_most);
  3221. if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
  3222. or_conn->tls_error = result;
  3223. else
  3224. or_conn->tls_error = 0;
  3225. switch (result) {
  3226. case TOR_TLS_CLOSE:
  3227. case TOR_TLS_ERROR_IO:
  3228. log_debug(LD_NET,"TLS connection closed %son read. Closing. "
  3229. "(Nickname %s, address %s)",
  3230. result == TOR_TLS_CLOSE ? "cleanly " : "",
  3231. or_conn->nickname ? or_conn->nickname : "not set",
  3232. conn->address);
  3233. return result;
  3234. CASE_TOR_TLS_ERROR_ANY_NONIO:
  3235. log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
  3236. tor_tls_err_to_string(result),
  3237. or_conn->nickname ? or_conn->nickname : "not set",
  3238. conn->address);
  3239. return result;
  3240. case TOR_TLS_WANTWRITE:
  3241. connection_start_writing(conn);
  3242. return 0;
  3243. case TOR_TLS_WANTREAD:
  3244. if (conn->in_connection_handle_write) {
  3245. /* We've been invoked from connection_handle_write, because we're
  3246. * waiting for a TLS renegotiation, the renegotiation started, and
  3247. * SSL_read returned WANTWRITE. But now SSL_read is saying WANTREAD
  3248. * again. Stop waiting for write events now, or else we'll
  3249. * busy-loop until data arrives for us to read. */
  3250. connection_stop_writing(conn);
  3251. if (!connection_is_reading(conn))
  3252. connection_start_reading(conn);
  3253. }
  3254. /* we're already reading, one hopes */
  3255. break;
  3256. case TOR_TLS_DONE: /* no data read, so nothing to process */
  3257. break; /* so we call bucket_decrement below */
  3258. default:
  3259. break;
  3260. }
  3261. pending = tor_tls_get_pending_bytes(or_conn->tls);
  3262. if (pending) {
  3263. /* If we have any pending bytes, we read them now. This *can*
  3264. * take us over our read allotment, but really we shouldn't be
  3265. * believing that SSL bytes are the same as TCP bytes anyway. */
  3266. int r2 = buf_read_from_tls(conn->inbuf, or_conn->tls, pending);
  3267. if (BUG(r2<0)) {
  3268. log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
  3269. return -1;
  3270. }
  3271. }
  3272. result = (int)(buf_datalen(conn->inbuf)-initial_size);
  3273. tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
  3274. log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
  3275. result, (long)n_read, (long)n_written);
  3276. } else if (conn->linked) {
  3277. if (conn->linked_conn) {
  3278. result = buf_move_to_buf(conn->inbuf, conn->linked_conn->outbuf,
  3279. &conn->linked_conn->outbuf_flushlen);
  3280. } else {
  3281. result = 0;
  3282. }
  3283. //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
  3284. /* If the other side has disappeared, or if it's been marked for close and
  3285. * we flushed its outbuf, then we should set our inbuf_reached_eof. */
  3286. if (!conn->linked_conn ||
  3287. (conn->linked_conn->marked_for_close &&
  3288. buf_datalen(conn->linked_conn->outbuf) == 0))
  3289. conn->inbuf_reached_eof = 1;
  3290. n_read = (size_t) result;
  3291. } else {
  3292. /* !connection_speaks_cells, !conn->linked_conn. */
  3293. int reached_eof = 0;
  3294. CONN_LOG_PROTECT(conn,
  3295. result = buf_read_from_socket(conn->inbuf, conn->s,
  3296. at_most,
  3297. &reached_eof,
  3298. socket_error));
  3299. if (reached_eof)
  3300. conn->inbuf_reached_eof = 1;
  3301. // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
  3302. if (result < 0)
  3303. return -1;
  3304. n_read = (size_t) result;
  3305. }
  3306. if (n_read > 0) {
  3307. /* change *max_to_read */
  3308. *max_to_read = at_most - n_read;
  3309. /* Update edge_conn->n_read and ocirc->n_read_circ_bw */
  3310. if (conn->type == CONN_TYPE_AP) {
  3311. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  3312. circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
  3313. origin_circuit_t *ocirc;
  3314. /* Check for overflow: */
  3315. if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
  3316. edge_conn->n_read += (int)n_read;
  3317. else
  3318. edge_conn->n_read = UINT32_MAX;
  3319. if (circ && CIRCUIT_IS_ORIGIN(circ)) {
  3320. ocirc = TO_ORIGIN_CIRCUIT(circ);
  3321. if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read_circ_bw > n_read))
  3322. ocirc->n_read_circ_bw += (int)n_read;
  3323. else
  3324. ocirc->n_read_circ_bw = UINT32_MAX;
  3325. }
  3326. }
  3327. /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
  3328. * OR/DIR/EXIT connections, checking for overflow. */
  3329. if (get_options()->TestingEnableConnBwEvent &&
  3330. (conn->type == CONN_TYPE_OR ||
  3331. conn->type == CONN_TYPE_DIR ||
  3332. conn->type == CONN_TYPE_EXIT)) {
  3333. if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
  3334. conn->n_read_conn_bw += (int)n_read;
  3335. else
  3336. conn->n_read_conn_bw = UINT32_MAX;
  3337. }
  3338. }
  3339. connection_buckets_decrement(conn, approx_time(), n_read, n_written);
  3340. if (more_to_read && result == at_most) {
  3341. slack_in_buf = buf_slack(conn->inbuf);
  3342. at_most = more_to_read;
  3343. goto again;
  3344. }
  3345. /* Call even if result is 0, since the global read bucket may
  3346. * have reached 0 on a different conn, and this connection needs to
  3347. * know to stop reading. */
  3348. connection_consider_empty_read_buckets(conn);
  3349. if (n_written > 0 && connection_is_writing(conn))
  3350. connection_consider_empty_write_buckets(conn);
  3351. return 0;
  3352. }
  3353. /** A pass-through to fetch_from_buf. */
  3354. int
  3355. connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
  3356. {
  3357. return buf_get_bytes(conn->inbuf, string, len);
  3358. }
  3359. /** As buf_get_line(), but read from a connection's input buffer. */
  3360. int
  3361. connection_buf_get_line(connection_t *conn, char *data,
  3362. size_t *data_len)
  3363. {
  3364. return buf_get_line(conn->inbuf, data, data_len);
  3365. }
  3366. /** As fetch_from_buf_http, but fetches from a connection's input buffer_t as
  3367. * appropriate. */
  3368. int
  3369. connection_fetch_from_buf_http(connection_t *conn,
  3370. char **headers_out, size_t max_headerlen,
  3371. char **body_out, size_t *body_used,
  3372. size_t max_bodylen, int force_complete)
  3373. {
  3374. return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
  3375. body_out, body_used, max_bodylen, force_complete);
  3376. }
  3377. /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
  3378. * from its outbuf. */
  3379. int
  3380. connection_wants_to_flush(connection_t *conn)
  3381. {
  3382. return conn->outbuf_flushlen > 0;
  3383. }
  3384. /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
  3385. * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
  3386. * connection_edge_consider_sending_sendme().
  3387. */
  3388. int
  3389. connection_outbuf_too_full(connection_t *conn)
  3390. {
  3391. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  3392. }
  3393. /**
  3394. * On Windows Vista and Windows 7, tune the send buffer size according to a
  3395. * hint from the OS.
  3396. *
  3397. * This should help fix slow upload rates.
  3398. */
  3399. static void
  3400. update_send_buffer_size(tor_socket_t sock)
  3401. {
  3402. #ifdef _WIN32
  3403. /* We only do this on Vista and 7, because earlier versions of Windows
  3404. * don't have the SIO_IDEAL_SEND_BACKLOG_QUERY functionality, and on
  3405. * later versions it isn't necessary. */
  3406. static int isVistaOr7 = -1;
  3407. if (isVistaOr7 == -1) {
  3408. isVistaOr7 = 0;
  3409. OSVERSIONINFO osvi = { 0 };
  3410. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  3411. GetVersionEx(&osvi);
  3412. if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion < 2)
  3413. isVistaOr7 = 1;
  3414. }
  3415. if (!isVistaOr7)
  3416. return;
  3417. if (get_options()->ConstrainedSockets)
  3418. return;
  3419. ULONG isb = 0;
  3420. DWORD bytesReturned = 0;
  3421. if (!WSAIoctl(sock, SIO_IDEAL_SEND_BACKLOG_QUERY, NULL, 0,
  3422. &isb, sizeof(isb), &bytesReturned, NULL, NULL)) {
  3423. setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&isb, sizeof(isb));
  3424. }
  3425. #else
  3426. (void) sock;
  3427. #endif
  3428. }
  3429. /** Try to flush more bytes onto <b>conn</b>-\>s.
  3430. *
  3431. * This function gets called either from conn_write_callback() in main.c
  3432. * when libevent tells us that conn wants to write, or below
  3433. * from connection_buf_add() when an entire TLS record is ready.
  3434. *
  3435. * Update <b>conn</b>-\>timestamp_lastwritten to now, and call flush_buf
  3436. * or flush_buf_tls appropriately. If it succeeds and there are no more
  3437. * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
  3438. * on it too.
  3439. *
  3440. * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
  3441. * limits. (Used for flushing messages to controller connections on fatal
  3442. * errors.)
  3443. *
  3444. * Mark the connection and return -1 if you want to close it, else
  3445. * return 0.
  3446. */
  3447. static int
  3448. connection_handle_write_impl(connection_t *conn, int force)
  3449. {
  3450. int e;
  3451. socklen_t len=(socklen_t)sizeof(e);
  3452. int result;
  3453. ssize_t max_to_write;
  3454. time_t now = approx_time();
  3455. size_t n_read = 0, n_written = 0;
  3456. int dont_stop_writing = 0;
  3457. tor_assert(!connection_is_listener(conn));
  3458. if (conn->marked_for_close || !SOCKET_OK(conn->s))
  3459. return 0; /* do nothing */
  3460. if (conn->in_flushed_some) {
  3461. log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
  3462. return 0;
  3463. }
  3464. conn->timestamp_lastwritten = now;
  3465. /* Sometimes, "writable" means "connected". */
  3466. if (connection_state_is_connecting(conn)) {
  3467. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
  3468. log_warn(LD_BUG, "getsockopt() syscall failed");
  3469. if (conn->type == CONN_TYPE_OR) {
  3470. or_connection_t *orconn = TO_OR_CONN(conn);
  3471. connection_or_close_for_error(orconn, 0);
  3472. } else {
  3473. if (CONN_IS_EDGE(conn)) {
  3474. connection_edge_end_errno(TO_EDGE_CONN(conn));
  3475. }
  3476. connection_mark_for_close(conn);
  3477. }
  3478. return -1;
  3479. }
  3480. if (e) {
  3481. /* some sort of error, but maybe just inprogress still */
  3482. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  3483. log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
  3484. tor_socket_strerror(e));
  3485. if (CONN_IS_EDGE(conn))
  3486. connection_edge_end_errno(TO_EDGE_CONN(conn));
  3487. if (conn->type == CONN_TYPE_OR)
  3488. connection_or_notify_error(TO_OR_CONN(conn),
  3489. errno_to_orconn_end_reason(e),
  3490. tor_socket_strerror(e));
  3491. connection_close_immediate(conn);
  3492. /*
  3493. * This can bypass normal channel checking since we did
  3494. * connection_or_notify_error() above.
  3495. */
  3496. connection_mark_for_close_internal(conn);
  3497. return -1;
  3498. } else {
  3499. return 0; /* no change, see if next time is better */
  3500. }
  3501. }
  3502. /* The connection is successful. */
  3503. if (connection_finished_connecting(conn)<0)
  3504. return -1;
  3505. }
  3506. max_to_write = force ? (ssize_t)conn->outbuf_flushlen
  3507. : connection_bucket_write_limit(conn, now);
  3508. if (connection_speaks_cells(conn) &&
  3509. conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
  3510. or_connection_t *or_conn = TO_OR_CONN(conn);
  3511. size_t initial_size;
  3512. if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
  3513. conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
  3514. connection_stop_writing(conn);
  3515. if (connection_tls_continue_handshake(or_conn) < 0) {
  3516. /* Don't flush; connection is dead. */
  3517. connection_or_notify_error(or_conn,
  3518. END_OR_CONN_REASON_MISC,
  3519. "TLS error in connection_tls_"
  3520. "continue_handshake()");
  3521. connection_close_immediate(conn);
  3522. /*
  3523. * This can bypass normal channel checking since we did
  3524. * connection_or_notify_error() above.
  3525. */
  3526. connection_mark_for_close_internal(conn);
  3527. return -1;
  3528. }
  3529. return 0;
  3530. } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
  3531. return connection_handle_read(conn);
  3532. }
  3533. /* else open, or closing */
  3534. initial_size = buf_datalen(conn->outbuf);
  3535. result = buf_flush_to_tls(conn->outbuf, or_conn->tls,
  3536. max_to_write, &conn->outbuf_flushlen);
  3537. if (result >= 0)
  3538. update_send_buffer_size(conn->s);
  3539. /* If we just flushed the last bytes, tell the channel on the
  3540. * or_conn to check if it needs to geoip_change_dirreq_state() */
  3541. /* XXXX move this to flushed_some or finished_flushing -NM */
  3542. if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
  3543. channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
  3544. switch (result) {
  3545. CASE_TOR_TLS_ERROR_ANY:
  3546. case TOR_TLS_CLOSE:
  3547. log_info(LD_NET, result != TOR_TLS_CLOSE ?
  3548. "tls error. breaking.":"TLS connection closed on flush");
  3549. /* Don't flush; connection is dead. */
  3550. connection_or_notify_error(or_conn,
  3551. END_OR_CONN_REASON_MISC,
  3552. result != TOR_TLS_CLOSE ?
  3553. "TLS error in during flush" :
  3554. "TLS closed during flush");
  3555. connection_close_immediate(conn);
  3556. /*
  3557. * This can bypass normal channel checking since we did
  3558. * connection_or_notify_error() above.
  3559. */
  3560. connection_mark_for_close_internal(conn);
  3561. return -1;
  3562. case TOR_TLS_WANTWRITE:
  3563. log_debug(LD_NET,"wanted write.");
  3564. /* we're already writing */
  3565. dont_stop_writing = 1;
  3566. break;
  3567. case TOR_TLS_WANTREAD:
  3568. /* Make sure to avoid a loop if the receive buckets are empty. */
  3569. log_debug(LD_NET,"wanted read.");
  3570. if (!connection_is_reading(conn)) {
  3571. connection_stop_writing(conn);
  3572. conn->write_blocked_on_bw = 1;
  3573. /* we'll start reading again when we get more tokens in our
  3574. * read bucket; then we'll start writing again too.
  3575. */
  3576. }
  3577. /* else no problem, we're already reading */
  3578. return 0;
  3579. /* case TOR_TLS_DONE:
  3580. * for TOR_TLS_DONE, fall through to check if the flushlen
  3581. * is empty, so we can stop writing.
  3582. */
  3583. }
  3584. tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
  3585. log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
  3586. result, (long)n_read, (long)n_written);
  3587. or_conn->bytes_xmitted += result;
  3588. or_conn->bytes_xmitted_by_tls += n_written;
  3589. /* So we notice bytes were written even on error */
  3590. /* XXXX This cast is safe since we can never write INT_MAX bytes in a
  3591. * single set of TLS operations. But it looks kinda ugly. If we refactor
  3592. * the *_buf_tls functions, we should make them return ssize_t or size_t
  3593. * or something. */
  3594. result = (int)(initial_size-buf_datalen(conn->outbuf));
  3595. } else {
  3596. CONN_LOG_PROTECT(conn,
  3597. result = buf_flush_to_socket(conn->outbuf, conn->s,
  3598. max_to_write, &conn->outbuf_flushlen));
  3599. if (result < 0) {
  3600. if (CONN_IS_EDGE(conn))
  3601. connection_edge_end_errno(TO_EDGE_CONN(conn));
  3602. if (conn->type == CONN_TYPE_AP) {
  3603. /* writing failed; we couldn't send a SOCKS reply if we wanted to */
  3604. TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
  3605. }
  3606. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  3607. connection_mark_for_close(conn);
  3608. return -1;
  3609. }
  3610. update_send_buffer_size(conn->s);
  3611. n_written = (size_t) result;
  3612. }
  3613. if (n_written && conn->type == CONN_TYPE_AP) {
  3614. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  3615. circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
  3616. origin_circuit_t *ocirc;
  3617. /* Check for overflow: */
  3618. if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
  3619. edge_conn->n_written += (int)n_written;
  3620. else
  3621. edge_conn->n_written = UINT32_MAX;
  3622. if (circ && CIRCUIT_IS_ORIGIN(circ)) {
  3623. ocirc = TO_ORIGIN_CIRCUIT(circ);
  3624. if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written_circ_bw > n_written))
  3625. ocirc->n_written_circ_bw += (int)n_written;
  3626. else
  3627. ocirc->n_written_circ_bw = UINT32_MAX;
  3628. }
  3629. }
  3630. /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
  3631. * OR/DIR/EXIT connections, checking for overflow. */
  3632. if (n_written && get_options()->TestingEnableConnBwEvent &&
  3633. (conn->type == CONN_TYPE_OR ||
  3634. conn->type == CONN_TYPE_DIR ||
  3635. conn->type == CONN_TYPE_EXIT)) {
  3636. if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
  3637. conn->n_written_conn_bw += (int)n_written;
  3638. else
  3639. conn->n_written_conn_bw = UINT32_MAX;
  3640. }
  3641. connection_buckets_decrement(conn, approx_time(), n_read, n_written);
  3642. if (result > 0) {
  3643. /* If we wrote any bytes from our buffer, then call the appropriate
  3644. * functions. */
  3645. if (connection_flushed_some(conn) < 0) {
  3646. if (connection_speaks_cells(conn)) {
  3647. connection_or_notify_error(TO_OR_CONN(conn),
  3648. END_OR_CONN_REASON_MISC,
  3649. "Got error back from "
  3650. "connection_flushed_some()");
  3651. }
  3652. /*
  3653. * This can bypass normal channel checking since we did
  3654. * connection_or_notify_error() above.
  3655. */
  3656. connection_mark_for_close_internal(conn);
  3657. }
  3658. }
  3659. if (!connection_wants_to_flush(conn) &&
  3660. !dont_stop_writing) { /* it's done flushing */
  3661. if (connection_finished_flushing(conn) < 0) {
  3662. /* already marked */
  3663. return -1;
  3664. }
  3665. return 0;
  3666. }
  3667. /* Call even if result is 0, since the global write bucket may
  3668. * have reached 0 on a different conn, and this connection needs to
  3669. * know to stop writing. */
  3670. connection_consider_empty_write_buckets(conn);
  3671. if (n_read > 0 && connection_is_reading(conn))
  3672. connection_consider_empty_read_buckets(conn);
  3673. return 0;
  3674. }
  3675. /* DOCDOC connection_handle_write */
  3676. int
  3677. connection_handle_write(connection_t *conn, int force)
  3678. {
  3679. int res;
  3680. tor_gettimeofday_cache_clear();
  3681. conn->in_connection_handle_write = 1;
  3682. res = connection_handle_write_impl(conn, force);
  3683. conn->in_connection_handle_write = 0;
  3684. return res;
  3685. }
  3686. /**
  3687. * Try to flush data that's waiting for a write on <b>conn</b>. Return
  3688. * -1 on failure, 0 on success.
  3689. *
  3690. * Don't use this function for regular writing; the buffers
  3691. * system should be good enough at scheduling writes there. Instead, this
  3692. * function is for cases when we're about to exit or something and we want
  3693. * to report it right away.
  3694. */
  3695. int
  3696. connection_flush(connection_t *conn)
  3697. {
  3698. return connection_handle_write(conn, 1);
  3699. }
  3700. /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
  3701. *
  3702. * Return true iff it is okay to queue bytes on <b>conn</b>'s outbuf for
  3703. * writing.
  3704. */
  3705. static int
  3706. connection_may_write_to_buf(connection_t *conn)
  3707. {
  3708. /* if it's marked for close, only allow write if we mean to flush it */
  3709. if (conn->marked_for_close && !conn->hold_open_until_flushed)
  3710. return 0;
  3711. return 1;
  3712. }
  3713. /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
  3714. *
  3715. * Called when an attempt to add bytes on <b>conn</b>'s outbuf has failed;
  3716. * mark the connection and warn as appropriate.
  3717. */
  3718. static void
  3719. connection_write_to_buf_failed(connection_t *conn)
  3720. {
  3721. if (CONN_IS_EDGE(conn)) {
  3722. /* if it failed, it means we have our package/delivery windows set
  3723. wrong compared to our max outbuf size. close the whole circuit. */
  3724. log_warn(LD_NET,
  3725. "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
  3726. circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
  3727. END_CIRC_REASON_INTERNAL);
  3728. } else if (conn->type == CONN_TYPE_OR) {
  3729. or_connection_t *orconn = TO_OR_CONN(conn);
  3730. log_warn(LD_NET,
  3731. "write_to_buf failed on an orconn; notifying of error "
  3732. "(fd %d)", (int)(conn->s));
  3733. connection_or_close_for_error(orconn, 0);
  3734. } else {
  3735. log_warn(LD_NET,
  3736. "write_to_buf failed. Closing connection (fd %d).",
  3737. (int)conn->s);
  3738. connection_mark_for_close(conn);
  3739. }
  3740. }
  3741. /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
  3742. *
  3743. * Called when an attempt to add bytes on <b>conn</b>'s outbuf has succeeded:
  3744. * record the number of bytes added.
  3745. */
  3746. static void
  3747. connection_write_to_buf_commit(connection_t *conn, size_t len)
  3748. {
  3749. /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
  3750. * state, we don't want to try to write it right away, since
  3751. * conn->write_event won't be set yet. Otherwise, write data from
  3752. * this conn as the socket is available. */
  3753. if (conn->write_event) {
  3754. connection_start_writing(conn);
  3755. }
  3756. conn->outbuf_flushlen += len;
  3757. }
  3758. /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
  3759. * outbuf, and ask it to start writing.
  3760. *
  3761. * If <b>zlib</b> is nonzero, this is a directory connection that should get
  3762. * its contents compressed or decompressed as they're written. If zlib is
  3763. * negative, this is the last data to be compressed, and the connection's zlib
  3764. * state should be flushed.
  3765. */
  3766. MOCK_IMPL(void,
  3767. connection_write_to_buf_impl_,(const char *string, size_t len,
  3768. connection_t *conn, int zlib))
  3769. {
  3770. /* XXXX This function really needs to return -1 on failure. */
  3771. int r;
  3772. if (!len && !(zlib<0))
  3773. return;
  3774. if (!connection_may_write_to_buf(conn))
  3775. return;
  3776. size_t written;
  3777. if (zlib) {
  3778. size_t old_datalen = buf_datalen(conn->outbuf);
  3779. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  3780. int done = zlib < 0;
  3781. CONN_LOG_PROTECT(conn, r = buf_add_compress(conn->outbuf,
  3782. dir_conn->compress_state,
  3783. string, len, done));
  3784. written = buf_datalen(conn->outbuf) - old_datalen;
  3785. } else {
  3786. CONN_LOG_PROTECT(conn, r = buf_add(conn->outbuf, string, len));
  3787. written = len;
  3788. }
  3789. if (r < 0) {
  3790. connection_write_to_buf_failed(conn);
  3791. return;
  3792. }
  3793. connection_write_to_buf_commit(conn, written);
  3794. }
  3795. /**
  3796. * Add all bytes from <b>buf</b> to <b>conn</b>'s outbuf, draining them
  3797. * from <b>buf</b>. (If the connection is marked and will soon be closed,
  3798. * nothing is drained.)
  3799. */
  3800. void
  3801. connection_buf_add_buf(connection_t *conn, buf_t *buf)
  3802. {
  3803. tor_assert(conn);
  3804. tor_assert(buf);
  3805. size_t len = buf_datalen(buf);
  3806. if (len == 0)
  3807. return;
  3808. if (!connection_may_write_to_buf(conn))
  3809. return;
  3810. buf_move_all(conn->outbuf, buf);
  3811. connection_write_to_buf_commit(conn, len);
  3812. }
  3813. #define CONN_GET_ALL_TEMPLATE(var, test) \
  3814. STMT_BEGIN \
  3815. smartlist_t *conns = get_connection_array(); \
  3816. smartlist_t *ret_conns = smartlist_new(); \
  3817. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
  3818. if (var && (test) && !var->marked_for_close) \
  3819. smartlist_add(ret_conns, var); \
  3820. } SMARTLIST_FOREACH_END(var); \
  3821. return ret_conns; \
  3822. STMT_END
  3823. /* Return a list of connections that aren't close and matches the given type
  3824. * and state. The returned list can be empty and must be freed using
  3825. * smartlist_free(). The caller does NOT have ownership of the objects in the
  3826. * list so it must not free them nor reference them as they can disappear. */
  3827. smartlist_t *
  3828. connection_list_by_type_state(int type, int state)
  3829. {
  3830. CONN_GET_ALL_TEMPLATE(conn, (conn->type == type && conn->state == state));
  3831. }
  3832. /* Return a list of connections that aren't close and matches the given type
  3833. * and purpose. The returned list can be empty and must be freed using
  3834. * smartlist_free(). The caller does NOT have ownership of the objects in the
  3835. * list so it must not free them nor reference them as they can disappear. */
  3836. smartlist_t *
  3837. connection_list_by_type_purpose(int type, int purpose)
  3838. {
  3839. CONN_GET_ALL_TEMPLATE(conn,
  3840. (conn->type == type && conn->purpose == purpose));
  3841. }
  3842. /** Return a connection_t * from get_connection_array() that satisfies test on
  3843. * var, and that is not marked for close. */
  3844. #define CONN_GET_TEMPLATE(var, test) \
  3845. STMT_BEGIN \
  3846. smartlist_t *conns = get_connection_array(); \
  3847. SMARTLIST_FOREACH(conns, connection_t *, var, \
  3848. { \
  3849. if (var && (test) && !var->marked_for_close) \
  3850. return var; \
  3851. }); \
  3852. return NULL; \
  3853. STMT_END
  3854. /** Return a connection with given type, address, port, and purpose;
  3855. * or NULL if no such connection exists (or if all such connections are marked
  3856. * for close). */
  3857. MOCK_IMPL(connection_t *,
  3858. connection_get_by_type_addr_port_purpose,(int type,
  3859. const tor_addr_t *addr, uint16_t port,
  3860. int purpose))
  3861. {
  3862. CONN_GET_TEMPLATE(conn,
  3863. (conn->type == type &&
  3864. tor_addr_eq(&conn->addr, addr) &&
  3865. conn->port == port &&
  3866. conn->purpose == purpose));
  3867. }
  3868. /** Return the stream with id <b>id</b> if it is not already marked for
  3869. * close.
  3870. */
  3871. connection_t *
  3872. connection_get_by_global_id(uint64_t id)
  3873. {
  3874. CONN_GET_TEMPLATE(conn, conn->global_identifier == id);
  3875. }
  3876. /** Return a connection of type <b>type</b> that is not marked for close.
  3877. */
  3878. connection_t *
  3879. connection_get_by_type(int type)
  3880. {
  3881. CONN_GET_TEMPLATE(conn, conn->type == type);
  3882. }
  3883. /** Return a connection of type <b>type</b> that is in state <b>state</b>,
  3884. * and that is not marked for close.
  3885. */
  3886. connection_t *
  3887. connection_get_by_type_state(int type, int state)
  3888. {
  3889. CONN_GET_TEMPLATE(conn, conn->type == type && conn->state == state);
  3890. }
  3891. /** Return a connection of type <b>type</b> that has rendquery equal
  3892. * to <b>rendquery</b>, and that is not marked for close. If state
  3893. * is non-zero, conn must be of that state too.
  3894. */
  3895. connection_t *
  3896. connection_get_by_type_state_rendquery(int type, int state,
  3897. const char *rendquery)
  3898. {
  3899. tor_assert(type == CONN_TYPE_DIR ||
  3900. type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
  3901. tor_assert(rendquery);
  3902. CONN_GET_TEMPLATE(conn,
  3903. (conn->type == type &&
  3904. (!state || state == conn->state)) &&
  3905. (
  3906. (type == CONN_TYPE_DIR &&
  3907. TO_DIR_CONN(conn)->rend_data &&
  3908. !rend_cmp_service_ids(rendquery,
  3909. rend_data_get_address(TO_DIR_CONN(conn)->rend_data)))
  3910. ||
  3911. (CONN_IS_EDGE(conn) &&
  3912. TO_EDGE_CONN(conn)->rend_data &&
  3913. !rend_cmp_service_ids(rendquery,
  3914. rend_data_get_address(TO_EDGE_CONN(conn)->rend_data)))
  3915. ));
  3916. }
  3917. /** Return a new smartlist of dir_connection_t * from get_connection_array()
  3918. * that satisfy conn_test on connection_t *conn_var, and dirconn_test on
  3919. * dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not
  3920. * marked for close to be included in the list. */
  3921. #define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, \
  3922. dirconn_var, dirconn_test) \
  3923. STMT_BEGIN \
  3924. smartlist_t *conns = get_connection_array(); \
  3925. smartlist_t *dir_conns = smartlist_new(); \
  3926. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) { \
  3927. if (conn_var && (conn_test) \
  3928. && conn_var->type == CONN_TYPE_DIR \
  3929. && !conn_var->marked_for_close) { \
  3930. dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var); \
  3931. if (dirconn_var && (dirconn_test)) { \
  3932. smartlist_add(dir_conns, dirconn_var); \
  3933. } \
  3934. } \
  3935. } SMARTLIST_FOREACH_END(conn_var); \
  3936. return dir_conns; \
  3937. STMT_END
  3938. /** Return a list of directory connections that are fetching the item
  3939. * described by <b>purpose</b>/<b>resource</b>. If there are none,
  3940. * return an empty list. This list must be freed using smartlist_free,
  3941. * but the pointers in it must not be freed.
  3942. * Note that this list should not be cached, as the pointers in it can be
  3943. * freed if their connections close. */
  3944. smartlist_t *
  3945. connection_dir_list_by_purpose_and_resource(
  3946. int purpose,
  3947. const char *resource)
  3948. {
  3949. DIR_CONN_LIST_TEMPLATE(conn,
  3950. conn->purpose == purpose,
  3951. dirconn,
  3952. 0 == strcmp_opt(resource,
  3953. dirconn->requested_resource));
  3954. }
  3955. /** Return a list of directory connections that are fetching the item
  3956. * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. If there are
  3957. * none, return an empty list. This list must be freed using smartlist_free,
  3958. * but the pointers in it must not be freed.
  3959. * Note that this list should not be cached, as the pointers in it can be
  3960. * freed if their connections close. */
  3961. smartlist_t *
  3962. connection_dir_list_by_purpose_resource_and_state(
  3963. int purpose,
  3964. const char *resource,
  3965. int state)
  3966. {
  3967. DIR_CONN_LIST_TEMPLATE(conn,
  3968. conn->purpose == purpose && conn->state == state,
  3969. dirconn,
  3970. 0 == strcmp_opt(resource,
  3971. dirconn->requested_resource));
  3972. }
  3973. #undef DIR_CONN_LIST_TEMPLATE
  3974. /** Return an arbitrary active OR connection that isn't <b>this_conn</b>.
  3975. *
  3976. * We use this to guess if we should tell the controller that we
  3977. * didn't manage to connect to any of our bridges. */
  3978. static connection_t *
  3979. connection_get_another_active_or_conn(const or_connection_t *this_conn)
  3980. {
  3981. CONN_GET_TEMPLATE(conn,
  3982. conn != TO_CONN(this_conn) && conn->type == CONN_TYPE_OR);
  3983. }
  3984. /** Return 1 if there are any active OR connections apart from
  3985. * <b>this_conn</b>.
  3986. *
  3987. * We use this to guess if we should tell the controller that we
  3988. * didn't manage to connect to any of our bridges. */
  3989. int
  3990. any_other_active_or_conns(const or_connection_t *this_conn)
  3991. {
  3992. connection_t *conn = connection_get_another_active_or_conn(this_conn);
  3993. if (conn != NULL) {
  3994. log_debug(LD_DIR, "%s: Found an OR connection: %s",
  3995. __func__, conn->address);
  3996. return 1;
  3997. }
  3998. return 0;
  3999. }
  4000. #undef CONN_GET_TEMPLATE
  4001. /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
  4002. int
  4003. connection_is_listener(connection_t *conn)
  4004. {
  4005. if (conn->type == CONN_TYPE_OR_LISTENER ||
  4006. conn->type == CONN_TYPE_EXT_OR_LISTENER ||
  4007. conn->type == CONN_TYPE_AP_LISTENER ||
  4008. conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
  4009. conn->type == CONN_TYPE_AP_DNS_LISTENER ||
  4010. conn->type == CONN_TYPE_AP_NATD_LISTENER ||
  4011. conn->type == CONN_TYPE_AP_HTTP_CONNECT_LISTENER ||
  4012. conn->type == CONN_TYPE_DIR_LISTENER ||
  4013. conn->type == CONN_TYPE_CONTROL_LISTENER)
  4014. return 1;
  4015. return 0;
  4016. }
  4017. /** Return 1 if <b>conn</b> is in state "open" and is not marked
  4018. * for close, else return 0.
  4019. */
  4020. int
  4021. connection_state_is_open(connection_t *conn)
  4022. {
  4023. tor_assert(conn);
  4024. if (conn->marked_for_close)
  4025. return 0;
  4026. if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  4027. (conn->type == CONN_TYPE_EXT_OR) ||
  4028. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  4029. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
  4030. (conn->type == CONN_TYPE_CONTROL &&
  4031. conn->state == CONTROL_CONN_STATE_OPEN))
  4032. return 1;
  4033. return 0;
  4034. }
  4035. /** Return 1 if conn is in 'connecting' state, else return 0. */
  4036. int
  4037. connection_state_is_connecting(connection_t *conn)
  4038. {
  4039. tor_assert(conn);
  4040. if (conn->marked_for_close)
  4041. return 0;
  4042. switch (conn->type)
  4043. {
  4044. case CONN_TYPE_OR:
  4045. return conn->state == OR_CONN_STATE_CONNECTING;
  4046. case CONN_TYPE_EXIT:
  4047. return conn->state == EXIT_CONN_STATE_CONNECTING;
  4048. case CONN_TYPE_DIR:
  4049. return conn->state == DIR_CONN_STATE_CONNECTING;
  4050. }
  4051. return 0;
  4052. }
  4053. /** Allocates a base64'ed authenticator for use in http or https
  4054. * auth, based on the input string <b>authenticator</b>. Returns it
  4055. * if success, else returns NULL. */
  4056. char *
  4057. alloc_http_authenticator(const char *authenticator)
  4058. {
  4059. /* an authenticator in Basic authentication
  4060. * is just the string "username:password" */
  4061. const size_t authenticator_length = strlen(authenticator);
  4062. const size_t base64_authenticator_length =
  4063. base64_encode_size(authenticator_length, 0) + 1;
  4064. char *base64_authenticator = tor_malloc(base64_authenticator_length);
  4065. if (base64_encode(base64_authenticator, base64_authenticator_length,
  4066. authenticator, authenticator_length, 0) < 0) {
  4067. tor_free(base64_authenticator); /* free and set to null */
  4068. }
  4069. return base64_authenticator;
  4070. }
  4071. /** Given a socket handle, check whether the local address (sockname) of the
  4072. * socket is one that we've connected from before. If so, double-check
  4073. * whether our address has changed and we need to generate keys. If we do,
  4074. * call init_keys().
  4075. */
  4076. static void
  4077. client_check_address_changed(tor_socket_t sock)
  4078. {
  4079. struct sockaddr_storage out_sockaddr;
  4080. socklen_t out_addr_len = (socklen_t) sizeof(out_sockaddr);
  4081. tor_addr_t out_addr, iface_addr;
  4082. tor_addr_t **last_interface_ip_ptr;
  4083. sa_family_t family;
  4084. if (!outgoing_addrs)
  4085. outgoing_addrs = smartlist_new();
  4086. if (getsockname(sock, (struct sockaddr*)&out_sockaddr, &out_addr_len)<0) {
  4087. int e = tor_socket_errno(sock);
  4088. log_warn(LD_NET, "getsockname() to check for address change failed: %s",
  4089. tor_socket_strerror(e));
  4090. return;
  4091. }
  4092. tor_addr_from_sockaddr(&out_addr, (struct sockaddr*)&out_sockaddr, NULL);
  4093. family = tor_addr_family(&out_addr);
  4094. if (family == AF_INET)
  4095. last_interface_ip_ptr = &last_interface_ipv4;
  4096. else if (family == AF_INET6)
  4097. last_interface_ip_ptr = &last_interface_ipv6;
  4098. else
  4099. return;
  4100. if (! *last_interface_ip_ptr) {
  4101. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  4102. if (get_interface_address6(LOG_INFO, family, a)==0) {
  4103. *last_interface_ip_ptr = a;
  4104. } else {
  4105. tor_free(a);
  4106. }
  4107. }
  4108. /* If we've used this address previously, we're okay. */
  4109. SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
  4110. if (tor_addr_eq(a_ptr, &out_addr))
  4111. return;
  4112. );
  4113. /* Uh-oh. We haven't connected from this address before. Has the interface
  4114. * address changed? */
  4115. if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
  4116. return;
  4117. if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
  4118. /* Nope, it hasn't changed. Add this address to the list. */
  4119. smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
  4120. } else {
  4121. /* The interface changed. We're a client, so we need to regenerate our
  4122. * keys. First, reset the state. */
  4123. log_notice(LD_NET, "Our IP address has changed. Rotating keys...");
  4124. tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
  4125. SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
  4126. smartlist_clear(outgoing_addrs);
  4127. smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
  4128. /* We'll need to resolve ourselves again. */
  4129. reset_last_resolved_addr();
  4130. /* Okay, now change our keys. */
  4131. ip_address_changed(1);
  4132. }
  4133. }
  4134. /** Some systems have limited system buffers for recv and xmit on
  4135. * sockets allocated in a virtual server or similar environment. For a Tor
  4136. * server this can produce the "Error creating network socket: No buffer
  4137. * space available" error once all available TCP buffer space is consumed.
  4138. * This method will attempt to constrain the buffers allocated for the socket
  4139. * to the desired size to stay below system TCP buffer limits.
  4140. */
  4141. static void
  4142. set_constrained_socket_buffers(tor_socket_t sock, int size)
  4143. {
  4144. void *sz = (void*)&size;
  4145. socklen_t sz_sz = (socklen_t) sizeof(size);
  4146. if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
  4147. int e = tor_socket_errno(sock);
  4148. log_warn(LD_NET, "setsockopt() to constrain send "
  4149. "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
  4150. }
  4151. if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
  4152. int e = tor_socket_errno(sock);
  4153. log_warn(LD_NET, "setsockopt() to constrain recv "
  4154. "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
  4155. }
  4156. }
  4157. /** Process new bytes that have arrived on conn-\>inbuf.
  4158. *
  4159. * This function just passes conn to the connection-specific
  4160. * connection_*_process_inbuf() function. It also passes in
  4161. * package_partial if wanted.
  4162. */
  4163. static int
  4164. connection_process_inbuf(connection_t *conn, int package_partial)
  4165. {
  4166. tor_assert(conn);
  4167. switch (conn->type) {
  4168. case CONN_TYPE_OR:
  4169. return connection_or_process_inbuf(TO_OR_CONN(conn));
  4170. case CONN_TYPE_EXT_OR:
  4171. return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
  4172. case CONN_TYPE_EXIT:
  4173. case CONN_TYPE_AP:
  4174. return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
  4175. package_partial);
  4176. case CONN_TYPE_DIR:
  4177. return connection_dir_process_inbuf(TO_DIR_CONN(conn));
  4178. case CONN_TYPE_CONTROL:
  4179. return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
  4180. default:
  4181. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  4182. tor_fragile_assert();
  4183. return -1;
  4184. }
  4185. }
  4186. /** Called whenever we've written data on a connection. */
  4187. static int
  4188. connection_flushed_some(connection_t *conn)
  4189. {
  4190. int r = 0;
  4191. tor_assert(!conn->in_flushed_some);
  4192. conn->in_flushed_some = 1;
  4193. if (conn->type == CONN_TYPE_DIR &&
  4194. conn->state == DIR_CONN_STATE_SERVER_WRITING) {
  4195. r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
  4196. } else if (conn->type == CONN_TYPE_OR) {
  4197. r = connection_or_flushed_some(TO_OR_CONN(conn));
  4198. } else if (CONN_IS_EDGE(conn)) {
  4199. r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
  4200. }
  4201. conn->in_flushed_some = 0;
  4202. return r;
  4203. }
  4204. /** We just finished flushing bytes to the appropriately low network layer,
  4205. * and there are no more bytes remaining in conn-\>outbuf or
  4206. * conn-\>tls to be flushed.
  4207. *
  4208. * This function just passes conn to the connection-specific
  4209. * connection_*_finished_flushing() function.
  4210. */
  4211. static int
  4212. connection_finished_flushing(connection_t *conn)
  4213. {
  4214. tor_assert(conn);
  4215. /* If the connection is closed, don't try to do anything more here. */
  4216. if (CONN_IS_CLOSED(conn))
  4217. return 0;
  4218. // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
  4219. connection_stop_writing(conn);
  4220. switch (conn->type) {
  4221. case CONN_TYPE_OR:
  4222. return connection_or_finished_flushing(TO_OR_CONN(conn));
  4223. case CONN_TYPE_EXT_OR:
  4224. return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
  4225. case CONN_TYPE_AP:
  4226. case CONN_TYPE_EXIT:
  4227. return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
  4228. case CONN_TYPE_DIR:
  4229. return connection_dir_finished_flushing(TO_DIR_CONN(conn));
  4230. case CONN_TYPE_CONTROL:
  4231. return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
  4232. default:
  4233. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  4234. tor_fragile_assert();
  4235. return -1;
  4236. }
  4237. }
  4238. /** Called when our attempt to connect() to another server has just
  4239. * succeeded.
  4240. *
  4241. * This function just passes conn to the connection-specific
  4242. * connection_*_finished_connecting() function.
  4243. */
  4244. static int
  4245. connection_finished_connecting(connection_t *conn)
  4246. {
  4247. tor_assert(conn);
  4248. if (!server_mode(get_options())) {
  4249. /* See whether getsockname() says our address changed. We need to do this
  4250. * now that the connection has finished, because getsockname() on Windows
  4251. * won't work until then. */
  4252. client_check_address_changed(conn->s);
  4253. }
  4254. switch (conn->type)
  4255. {
  4256. case CONN_TYPE_OR:
  4257. return connection_or_finished_connecting(TO_OR_CONN(conn));
  4258. case CONN_TYPE_EXIT:
  4259. return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
  4260. case CONN_TYPE_DIR:
  4261. return connection_dir_finished_connecting(TO_DIR_CONN(conn));
  4262. default:
  4263. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  4264. tor_fragile_assert();
  4265. return -1;
  4266. }
  4267. }
  4268. /** Callback: invoked when a connection reaches an EOF event. */
  4269. static int
  4270. connection_reached_eof(connection_t *conn)
  4271. {
  4272. switch (conn->type) {
  4273. case CONN_TYPE_OR:
  4274. case CONN_TYPE_EXT_OR:
  4275. return connection_or_reached_eof(TO_OR_CONN(conn));
  4276. case CONN_TYPE_AP:
  4277. case CONN_TYPE_EXIT:
  4278. return connection_edge_reached_eof(TO_EDGE_CONN(conn));
  4279. case CONN_TYPE_DIR:
  4280. return connection_dir_reached_eof(TO_DIR_CONN(conn));
  4281. case CONN_TYPE_CONTROL:
  4282. return connection_control_reached_eof(TO_CONTROL_CONN(conn));
  4283. default:
  4284. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  4285. tor_fragile_assert();
  4286. return -1;
  4287. }
  4288. }
  4289. /** Comparator for the two-orconn case in OOS victim sort */
  4290. static int
  4291. oos_victim_comparator_for_orconns(or_connection_t *a, or_connection_t *b)
  4292. {
  4293. int a_circs, b_circs;
  4294. /* Fewer circuits == higher priority for OOS kill, sort earlier */
  4295. a_circs = connection_or_get_num_circuits(a);
  4296. b_circs = connection_or_get_num_circuits(b);
  4297. if (a_circs < b_circs) return 1;
  4298. else if (a_circs > b_circs) return -1;
  4299. else return 0;
  4300. }
  4301. /** Sort comparator for OOS victims; better targets sort before worse
  4302. * ones. */
  4303. static int
  4304. oos_victim_comparator(const void **a_v, const void **b_v)
  4305. {
  4306. connection_t *a = NULL, *b = NULL;
  4307. /* Get connection pointers out */
  4308. a = (connection_t *)(*a_v);
  4309. b = (connection_t *)(*b_v);
  4310. tor_assert(a != NULL);
  4311. tor_assert(b != NULL);
  4312. /*
  4313. * We always prefer orconns as victims currently; we won't even see
  4314. * these non-orconn cases, but if we do, sort them after orconns.
  4315. */
  4316. if (a->type == CONN_TYPE_OR && b->type == CONN_TYPE_OR) {
  4317. return oos_victim_comparator_for_orconns(TO_OR_CONN(a), TO_OR_CONN(b));
  4318. } else {
  4319. /*
  4320. * One isn't an orconn; if one is, it goes first. We currently have no
  4321. * opinions about cases where neither is an orconn.
  4322. */
  4323. if (a->type == CONN_TYPE_OR) return -1;
  4324. else if (b->type == CONN_TYPE_OR) return 1;
  4325. else return 0;
  4326. }
  4327. }
  4328. /** Pick n victim connections for the OOS handler and return them in a
  4329. * smartlist.
  4330. */
  4331. MOCK_IMPL(STATIC smartlist_t *,
  4332. pick_oos_victims, (int n))
  4333. {
  4334. smartlist_t *eligible = NULL, *victims = NULL;
  4335. smartlist_t *conns;
  4336. int conn_counts_by_type[CONN_TYPE_MAX_ + 1], i;
  4337. /*
  4338. * Big damn assumption (someone improve this someday!):
  4339. *
  4340. * Socket exhaustion normally happens on high-volume relays, and so
  4341. * most of the connections involved are orconns. We should pick victims
  4342. * by assembling a list of all orconns, and sorting them in order of
  4343. * how much 'damage' by some metric we'd be doing by dropping them.
  4344. *
  4345. * If we move on from orconns, we should probably think about incoming
  4346. * directory connections next, or exit connections. Things we should
  4347. * probably never kill are controller connections and listeners.
  4348. *
  4349. * This function will count how many connections of different types
  4350. * exist and log it for purposes of gathering data on typical OOS
  4351. * situations to guide future improvements.
  4352. */
  4353. /* First, get the connection array */
  4354. conns = get_connection_array();
  4355. /*
  4356. * Iterate it and pick out eligible connection types, and log some stats
  4357. * along the way.
  4358. */
  4359. eligible = smartlist_new();
  4360. memset(conn_counts_by_type, 0, sizeof(conn_counts_by_type));
  4361. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
  4362. /* Bump the counter */
  4363. tor_assert(c->type <= CONN_TYPE_MAX_);
  4364. ++(conn_counts_by_type[c->type]);
  4365. /* Skip anything without a socket we can free */
  4366. if (!(SOCKET_OK(c->s))) {
  4367. continue;
  4368. }
  4369. /* Skip anything we would count as moribund */
  4370. if (connection_is_moribund(c)) {
  4371. continue;
  4372. }
  4373. switch (c->type) {
  4374. case CONN_TYPE_OR:
  4375. /* We've got an orconn, it's eligible to be OOSed */
  4376. smartlist_add(eligible, c);
  4377. break;
  4378. default:
  4379. /* We don't know what to do with it, ignore it */
  4380. break;
  4381. }
  4382. } SMARTLIST_FOREACH_END(c);
  4383. /* Log some stats */
  4384. if (smartlist_len(conns) > 0) {
  4385. /* At least one counter must be non-zero */
  4386. log_info(LD_NET, "Some stats on conn types seen during OOS follow");
  4387. for (i = CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
  4388. /* Did we see any? */
  4389. if (conn_counts_by_type[i] > 0) {
  4390. log_info(LD_NET, "%s: %d conns",
  4391. conn_type_to_string(i),
  4392. conn_counts_by_type[i]);
  4393. }
  4394. }
  4395. log_info(LD_NET, "Done with OOS conn type stats");
  4396. }
  4397. /* Did we find more eligible targets than we want to kill? */
  4398. if (smartlist_len(eligible) > n) {
  4399. /* Sort the list in order of target preference */
  4400. smartlist_sort(eligible, oos_victim_comparator);
  4401. /* Pick first n as victims */
  4402. victims = smartlist_new();
  4403. for (i = 0; i < n; ++i) {
  4404. smartlist_add(victims, smartlist_get(eligible, i));
  4405. }
  4406. /* Free the original list */
  4407. smartlist_free(eligible);
  4408. } else {
  4409. /* No, we can just call them all victims */
  4410. victims = eligible;
  4411. }
  4412. return victims;
  4413. }
  4414. /** Kill a list of connections for the OOS handler. */
  4415. MOCK_IMPL(STATIC void,
  4416. kill_conn_list_for_oos, (smartlist_t *conns))
  4417. {
  4418. if (!conns) return;
  4419. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
  4420. /* Make sure the channel layer gets told about orconns */
  4421. if (c->type == CONN_TYPE_OR) {
  4422. connection_or_close_for_error(TO_OR_CONN(c), 1);
  4423. } else {
  4424. connection_mark_for_close(c);
  4425. }
  4426. } SMARTLIST_FOREACH_END(c);
  4427. log_notice(LD_NET,
  4428. "OOS handler marked %d connections",
  4429. smartlist_len(conns));
  4430. }
  4431. /** Out-of-Sockets handler; n_socks is the current number of open
  4432. * sockets, and failed is non-zero if a socket exhaustion related
  4433. * error immediately preceded this call. This is where to do
  4434. * circuit-killing heuristics as needed.
  4435. */
  4436. void
  4437. connection_check_oos(int n_socks, int failed)
  4438. {
  4439. int target_n_socks = 0, moribund_socks, socks_to_kill;
  4440. smartlist_t *conns;
  4441. /* Early exit: is OOS checking disabled? */
  4442. if (get_options()->DisableOOSCheck) {
  4443. return;
  4444. }
  4445. /* Sanity-check args */
  4446. tor_assert(n_socks >= 0);
  4447. /*
  4448. * Make some log noise; keep it at debug level since this gets a chance
  4449. * to run on every connection attempt.
  4450. */
  4451. log_debug(LD_NET,
  4452. "Running the OOS handler (%d open sockets, %s)",
  4453. n_socks, (failed != 0) ? "exhaustion seen" : "no exhaustion");
  4454. /*
  4455. * Check if we're really handling an OOS condition, and if so decide how
  4456. * many sockets we want to get down to. Be sure we check if the threshold
  4457. * is distinct from zero first; it's possible for this to be called a few
  4458. * times before we've finished reading the config.
  4459. */
  4460. if (n_socks >= get_options()->ConnLimit_high_thresh &&
  4461. get_options()->ConnLimit_high_thresh != 0 &&
  4462. get_options()->ConnLimit_ != 0) {
  4463. /* Try to get down to the low threshold */
  4464. target_n_socks = get_options()->ConnLimit_low_thresh;
  4465. log_notice(LD_NET,
  4466. "Current number of sockets %d is greater than configured "
  4467. "limit %d; OOS handler trying to get down to %d",
  4468. n_socks, get_options()->ConnLimit_high_thresh,
  4469. target_n_socks);
  4470. } else if (failed) {
  4471. /*
  4472. * If we're not at the limit but we hit a socket exhaustion error, try to
  4473. * drop some (but not as aggressively as ConnLimit_low_threshold, which is
  4474. * 3/4 of ConnLimit_)
  4475. */
  4476. target_n_socks = (n_socks * 9) / 10;
  4477. log_notice(LD_NET,
  4478. "We saw socket exhaustion at %d open sockets; OOS handler "
  4479. "trying to get down to %d",
  4480. n_socks, target_n_socks);
  4481. }
  4482. if (target_n_socks > 0) {
  4483. /*
  4484. * It's an OOS!
  4485. *
  4486. * Count moribund sockets; it's be important that anything we decide
  4487. * to get rid of here but don't immediately close get counted as moribund
  4488. * on subsequent invocations so we don't try to kill too many things if
  4489. * connection_check_oos() gets called multiple times.
  4490. */
  4491. moribund_socks = connection_count_moribund();
  4492. if (moribund_socks < n_socks - target_n_socks) {
  4493. socks_to_kill = n_socks - target_n_socks - moribund_socks;
  4494. conns = pick_oos_victims(socks_to_kill);
  4495. if (conns) {
  4496. kill_conn_list_for_oos(conns);
  4497. log_notice(LD_NET,
  4498. "OOS handler killed %d conns", smartlist_len(conns));
  4499. smartlist_free(conns);
  4500. } else {
  4501. log_notice(LD_NET, "OOS handler failed to pick any victim conns");
  4502. }
  4503. } else {
  4504. log_notice(LD_NET,
  4505. "Not killing any sockets for OOS because there are %d "
  4506. "already moribund, and we only want to eliminate %d",
  4507. moribund_socks, n_socks - target_n_socks);
  4508. }
  4509. }
  4510. }
  4511. /** Log how many bytes are used by buffers of different kinds and sizes. */
  4512. void
  4513. connection_dump_buffer_mem_stats(int severity)
  4514. {
  4515. uint64_t used_by_type[CONN_TYPE_MAX_+1];
  4516. uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
  4517. int n_conns_by_type[CONN_TYPE_MAX_+1];
  4518. uint64_t total_alloc = 0;
  4519. uint64_t total_used = 0;
  4520. int i;
  4521. smartlist_t *conns = get_connection_array();
  4522. memset(used_by_type, 0, sizeof(used_by_type));
  4523. memset(alloc_by_type, 0, sizeof(alloc_by_type));
  4524. memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
  4525. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
  4526. int tp = c->type;
  4527. ++n_conns_by_type[tp];
  4528. if (c->inbuf) {
  4529. used_by_type[tp] += buf_datalen(c->inbuf);
  4530. alloc_by_type[tp] += buf_allocation(c->inbuf);
  4531. }
  4532. if (c->outbuf) {
  4533. used_by_type[tp] += buf_datalen(c->outbuf);
  4534. alloc_by_type[tp] += buf_allocation(c->outbuf);
  4535. }
  4536. } SMARTLIST_FOREACH_END(c);
  4537. for (i=0; i <= CONN_TYPE_MAX_; ++i) {
  4538. total_used += used_by_type[i];
  4539. total_alloc += alloc_by_type[i];
  4540. }
  4541. tor_log(severity, LD_GENERAL,
  4542. "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
  4543. smartlist_len(conns),
  4544. U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
  4545. for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
  4546. if (!n_conns_by_type[i])
  4547. continue;
  4548. tor_log(severity, LD_GENERAL,
  4549. " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
  4550. n_conns_by_type[i], conn_type_to_string(i),
  4551. U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
  4552. }
  4553. }
  4554. /** Verify that connection <b>conn</b> has all of its invariants
  4555. * correct. Trigger an assert if anything is invalid.
  4556. */
  4557. void
  4558. assert_connection_ok(connection_t *conn, time_t now)
  4559. {
  4560. (void) now; /* XXXX unused. */
  4561. tor_assert(conn);
  4562. tor_assert(conn->type >= CONN_TYPE_MIN_);
  4563. tor_assert(conn->type <= CONN_TYPE_MAX_);
  4564. switch (conn->type) {
  4565. case CONN_TYPE_OR:
  4566. case CONN_TYPE_EXT_OR:
  4567. tor_assert(conn->magic == OR_CONNECTION_MAGIC);
  4568. break;
  4569. case CONN_TYPE_AP:
  4570. tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
  4571. break;
  4572. case CONN_TYPE_EXIT:
  4573. tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
  4574. break;
  4575. case CONN_TYPE_DIR:
  4576. tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
  4577. break;
  4578. case CONN_TYPE_CONTROL:
  4579. tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
  4580. break;
  4581. CASE_ANY_LISTENER_TYPE:
  4582. tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
  4583. break;
  4584. default:
  4585. tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
  4586. break;
  4587. }
  4588. if (conn->linked_conn) {
  4589. tor_assert(conn->linked_conn->linked_conn == conn);
  4590. tor_assert(conn->linked);
  4591. }
  4592. if (conn->linked)
  4593. tor_assert(!SOCKET_OK(conn->s));
  4594. if (conn->outbuf_flushlen > 0) {
  4595. /* With optimistic data, we may have queued data in
  4596. * EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
  4597. * */
  4598. tor_assert((conn->type == CONN_TYPE_EXIT &&
  4599. conn->state == EXIT_CONN_STATE_RESOLVING) ||
  4600. connection_is_writing(conn) ||
  4601. conn->write_blocked_on_bw ||
  4602. (CONN_IS_EDGE(conn) &&
  4603. TO_EDGE_CONN(conn)->edge_blocked_on_circ));
  4604. }
  4605. if (conn->hold_open_until_flushed)
  4606. tor_assert(conn->marked_for_close);
  4607. /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
  4608. * marked_for_close. */
  4609. /* buffers */
  4610. if (conn->inbuf)
  4611. buf_assert_ok(conn->inbuf);
  4612. if (conn->outbuf)
  4613. buf_assert_ok(conn->outbuf);
  4614. if (conn->type == CONN_TYPE_OR) {
  4615. or_connection_t *or_conn = TO_OR_CONN(conn);
  4616. if (conn->state == OR_CONN_STATE_OPEN) {
  4617. /* tor_assert(conn->bandwidth > 0); */
  4618. /* the above isn't necessarily true: if we just did a TLS
  4619. * handshake but we didn't recognize the other peer, or it
  4620. * gave a bad cert/etc, then we won't have assigned bandwidth,
  4621. * yet it will be open. -RD
  4622. */
  4623. // tor_assert(conn->read_bucket >= 0);
  4624. }
  4625. // tor_assert(conn->addr && conn->port);
  4626. tor_assert(conn->address);
  4627. if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
  4628. tor_assert(or_conn->tls);
  4629. }
  4630. if (CONN_IS_EDGE(conn)) {
  4631. /* XXX unchecked: package window, deliver window. */
  4632. if (conn->type == CONN_TYPE_AP) {
  4633. entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
  4634. if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
  4635. tor_assert(entry_conn->chosen_exit_name);
  4636. tor_assert(entry_conn->socks_request);
  4637. if (conn->state == AP_CONN_STATE_OPEN) {
  4638. tor_assert(entry_conn->socks_request->has_finished);
  4639. if (!conn->marked_for_close) {
  4640. tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
  4641. assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
  4642. }
  4643. }
  4644. }
  4645. if (conn->type == CONN_TYPE_EXIT) {
  4646. tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
  4647. conn->purpose == EXIT_PURPOSE_RESOLVE);
  4648. }
  4649. } else if (conn->type == CONN_TYPE_DIR) {
  4650. } else {
  4651. /* Purpose is only used for dir and exit types currently */
  4652. tor_assert(!conn->purpose);
  4653. }
  4654. switch (conn->type)
  4655. {
  4656. CASE_ANY_LISTENER_TYPE:
  4657. tor_assert(conn->state == LISTENER_STATE_READY);
  4658. break;
  4659. case CONN_TYPE_OR:
  4660. tor_assert(conn->state >= OR_CONN_STATE_MIN_);
  4661. tor_assert(conn->state <= OR_CONN_STATE_MAX_);
  4662. break;
  4663. case CONN_TYPE_EXT_OR:
  4664. tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
  4665. tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
  4666. break;
  4667. case CONN_TYPE_EXIT:
  4668. tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
  4669. tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
  4670. tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
  4671. tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
  4672. break;
  4673. case CONN_TYPE_AP:
  4674. tor_assert(conn->state >= AP_CONN_STATE_MIN_);
  4675. tor_assert(conn->state <= AP_CONN_STATE_MAX_);
  4676. tor_assert(TO_ENTRY_CONN(conn)->socks_request);
  4677. break;
  4678. case CONN_TYPE_DIR:
  4679. tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
  4680. tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
  4681. tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
  4682. tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
  4683. break;
  4684. case CONN_TYPE_CONTROL:
  4685. tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
  4686. tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
  4687. break;
  4688. default:
  4689. tor_assert(0);
  4690. }
  4691. }
  4692. /** Fills <b>addr</b> and <b>port</b> with the details of the global
  4693. * proxy server we are using.
  4694. * <b>conn</b> contains the connection we are using the proxy for.
  4695. *
  4696. * Return 0 on success, -1 on failure.
  4697. */
  4698. int
  4699. get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
  4700. const connection_t *conn)
  4701. {
  4702. const or_options_t *options = get_options();
  4703. /* Client Transport Plugins can use another proxy, but that should be hidden
  4704. * from the rest of tor (as the plugin is responsible for dealing with the
  4705. * proxy), check it first, then check the rest of the proxy types to allow
  4706. * the config to have unused ClientTransportPlugin entries.
  4707. */
  4708. if (options->ClientTransportPlugin) {
  4709. const transport_t *transport = NULL;
  4710. int r;
  4711. r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
  4712. if (r<0)
  4713. return -1;
  4714. if (transport) { /* transport found */
  4715. tor_addr_copy(addr, &transport->addr);
  4716. *port = transport->port;
  4717. *proxy_type = transport->socks_version;
  4718. return 0;
  4719. }
  4720. /* Unused ClientTransportPlugin. */
  4721. }
  4722. if (options->HTTPSProxy) {
  4723. tor_addr_copy(addr, &options->HTTPSProxyAddr);
  4724. *port = options->HTTPSProxyPort;
  4725. *proxy_type = PROXY_CONNECT;
  4726. return 0;
  4727. } else if (options->Socks4Proxy) {
  4728. tor_addr_copy(addr, &options->Socks4ProxyAddr);
  4729. *port = options->Socks4ProxyPort;
  4730. *proxy_type = PROXY_SOCKS4;
  4731. return 0;
  4732. } else if (options->Socks5Proxy) {
  4733. tor_addr_copy(addr, &options->Socks5ProxyAddr);
  4734. *port = options->Socks5ProxyPort;
  4735. *proxy_type = PROXY_SOCKS5;
  4736. return 0;
  4737. }
  4738. tor_addr_make_unspec(addr);
  4739. *port = 0;
  4740. *proxy_type = PROXY_NONE;
  4741. return 0;
  4742. }
  4743. /** Log a failed connection to a proxy server.
  4744. * <b>conn</b> is the connection we use the proxy server for. */
  4745. void
  4746. log_failed_proxy_connection(connection_t *conn)
  4747. {
  4748. tor_addr_t proxy_addr;
  4749. uint16_t proxy_port;
  4750. int proxy_type;
  4751. if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, conn) != 0)
  4752. return; /* if we have no proxy set up, leave this function. */
  4753. log_warn(LD_NET,
  4754. "The connection to the %s proxy server at %s just failed. "
  4755. "Make sure that the proxy server is up and running.",
  4756. proxy_type_to_string(proxy_type),
  4757. fmt_addrport(&proxy_addr, proxy_port));
  4758. }
  4759. /** Return string representation of <b>proxy_type</b>. */
  4760. static const char *
  4761. proxy_type_to_string(int proxy_type)
  4762. {
  4763. switch (proxy_type) {
  4764. case PROXY_CONNECT: return "HTTP";
  4765. case PROXY_SOCKS4: return "SOCKS4";
  4766. case PROXY_SOCKS5: return "SOCKS5";
  4767. case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
  4768. case PROXY_NONE: return "NULL";
  4769. default: tor_assert(0);
  4770. }
  4771. return NULL; /*Unreached*/
  4772. }
  4773. /** Call connection_free_minimal() on every connection in our array, and
  4774. * release all storage held by connection.c.
  4775. *
  4776. * Don't do the checks in connection_free(), because they will
  4777. * fail.
  4778. */
  4779. void
  4780. connection_free_all(void)
  4781. {
  4782. smartlist_t *conns = get_connection_array();
  4783. /* We don't want to log any messages to controllers. */
  4784. SMARTLIST_FOREACH(conns, connection_t *, conn,
  4785. if (conn->type == CONN_TYPE_CONTROL)
  4786. TO_CONTROL_CONN(conn)->event_mask = 0);
  4787. control_update_global_event_mask();
  4788. /* Unlink everything from the identity map. */
  4789. connection_or_clear_identity_map();
  4790. connection_or_clear_ext_or_id_map();
  4791. /* Clear out our list of broken connections */
  4792. clear_broken_connection_map(0);
  4793. SMARTLIST_FOREACH(conns, connection_t *, conn,
  4794. connection_free_minimal(conn));
  4795. if (outgoing_addrs) {
  4796. SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
  4797. smartlist_free(outgoing_addrs);
  4798. outgoing_addrs = NULL;
  4799. }
  4800. tor_free(last_interface_ipv4);
  4801. tor_free(last_interface_ipv6);
  4802. }
  4803. /** Log a warning, and possibly emit a control event, that <b>received</b> came
  4804. * at a skewed time. <b>trusted</b> indicates that the <b>source</b> was one
  4805. * that we had more faith in and therefore the warning level should have higher
  4806. * severity.
  4807. */
  4808. void
  4809. clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted,
  4810. log_domain_mask_t domain, const char *received,
  4811. const char *source)
  4812. {
  4813. char dbuf[64];
  4814. char *ext_source = NULL, *warn = NULL;
  4815. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  4816. if (conn)
  4817. tor_asprintf(&ext_source, "%s:%s:%d", source, conn->address, conn->port);
  4818. else
  4819. ext_source = tor_strdup(source);
  4820. log_fn(trusted ? LOG_WARN : LOG_INFO, domain,
  4821. "Received %s with skewed time (%s): "
  4822. "It seems that our clock is %s by %s, or that theirs is %s%s. "
  4823. "Tor requires an accurate clock to work: please check your time, "
  4824. "timezone, and date settings.", received, ext_source,
  4825. apparent_skew > 0 ? "ahead" : "behind", dbuf,
  4826. apparent_skew > 0 ? "behind" : "ahead",
  4827. (!conn || trusted) ? "" : ", or they are sending us the wrong time");
  4828. if (trusted) {
  4829. control_event_general_status(LOG_WARN, "CLOCK_SKEW SKEW=%ld SOURCE=%s",
  4830. apparent_skew, ext_source);
  4831. tor_asprintf(&warn, "Clock skew %ld in %s from %s", apparent_skew,
  4832. received, source);
  4833. control_event_bootstrap_problem(warn, "CLOCK_SKEW", conn, 1);
  4834. }
  4835. tor_free(warn);
  4836. tor_free(ext_source);
  4837. }