connection.c 181 KB

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