sctputil.c 245 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
  6. * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * a) Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * b) Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the distribution.
  17. *
  18. * c) Neither the name of Cisco Systems, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #if defined(__FreeBSD__) && !defined(__Userspace__)
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #endif
  38. #include <netinet/sctp_os.h>
  39. #include <netinet/sctp_pcb.h>
  40. #include <netinet/sctputil.h>
  41. #include <netinet/sctp_var.h>
  42. #include <netinet/sctp_sysctl.h>
  43. #ifdef INET6
  44. #if defined(__Userspace__) || defined(__FreeBSD__)
  45. #include <netinet6/sctp6_var.h>
  46. #endif
  47. #endif
  48. #include <netinet/sctp_header.h>
  49. #include <netinet/sctp_output.h>
  50. #include <netinet/sctp_uio.h>
  51. #include <netinet/sctp_timer.h>
  52. #include <netinet/sctp_indata.h>
  53. #include <netinet/sctp_auth.h>
  54. #include <netinet/sctp_asconf.h>
  55. #include <netinet/sctp_bsd_addr.h>
  56. #if defined(__Userspace__)
  57. #include <netinet/sctp_constants.h>
  58. #endif
  59. #if defined(__FreeBSD__) && !defined(__Userspace__)
  60. #include <netinet/sctp_kdtrace.h>
  61. #if defined(INET6) || defined(INET)
  62. #include <netinet/tcp_var.h>
  63. #endif
  64. #include <netinet/udp.h>
  65. #include <netinet/udp_var.h>
  66. #include <sys/proc.h>
  67. #ifdef INET6
  68. #include <netinet/icmp6.h>
  69. #endif
  70. #endif
  71. #if defined(_WIN32) && !defined(__Userspace__)
  72. #if !defined(SCTP_LOCAL_TRACE_BUF)
  73. #include "eventrace_netinet.h"
  74. #include "sctputil.tmh" /* this is the file that will be auto generated */
  75. #endif
  76. #else
  77. #ifndef KTR_SCTP
  78. #define KTR_SCTP KTR_SUBSYS
  79. #endif
  80. #endif
  81. extern const struct sctp_cc_functions sctp_cc_functions[];
  82. extern const struct sctp_ss_functions sctp_ss_functions[];
  83. void
  84. sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
  85. {
  86. #if defined(SCTP_LOCAL_TRACE_BUF)
  87. struct sctp_cwnd_log sctp_clog;
  88. sctp_clog.x.sb.stcb = stcb;
  89. sctp_clog.x.sb.so_sbcc = SCTP_SBAVAIL(sb);
  90. if (stcb)
  91. sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
  92. else
  93. sctp_clog.x.sb.stcb_sbcc = 0;
  94. sctp_clog.x.sb.incr = incr;
  95. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  96. SCTP_LOG_EVENT_SB,
  97. from,
  98. sctp_clog.x.misc.log1,
  99. sctp_clog.x.misc.log2,
  100. sctp_clog.x.misc.log3,
  101. sctp_clog.x.misc.log4);
  102. #endif
  103. }
  104. void
  105. sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
  106. {
  107. #if defined(SCTP_LOCAL_TRACE_BUF)
  108. struct sctp_cwnd_log sctp_clog;
  109. sctp_clog.x.close.inp = (void *)inp;
  110. sctp_clog.x.close.sctp_flags = inp->sctp_flags;
  111. if (stcb) {
  112. sctp_clog.x.close.stcb = (void *)stcb;
  113. sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
  114. } else {
  115. sctp_clog.x.close.stcb = 0;
  116. sctp_clog.x.close.state = 0;
  117. }
  118. sctp_clog.x.close.loc = loc;
  119. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  120. SCTP_LOG_EVENT_CLOSE,
  121. 0,
  122. sctp_clog.x.misc.log1,
  123. sctp_clog.x.misc.log2,
  124. sctp_clog.x.misc.log3,
  125. sctp_clog.x.misc.log4);
  126. #endif
  127. }
  128. void
  129. rto_logging(struct sctp_nets *net, int from)
  130. {
  131. #if defined(SCTP_LOCAL_TRACE_BUF)
  132. struct sctp_cwnd_log sctp_clog;
  133. memset(&sctp_clog, 0, sizeof(sctp_clog));
  134. sctp_clog.x.rto.net = (void *) net;
  135. sctp_clog.x.rto.rtt = net->rtt / 1000;
  136. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  137. SCTP_LOG_EVENT_RTT,
  138. from,
  139. sctp_clog.x.misc.log1,
  140. sctp_clog.x.misc.log2,
  141. sctp_clog.x.misc.log3,
  142. sctp_clog.x.misc.log4);
  143. #endif
  144. }
  145. void
  146. sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
  147. {
  148. #if defined(SCTP_LOCAL_TRACE_BUF)
  149. struct sctp_cwnd_log sctp_clog;
  150. sctp_clog.x.strlog.stcb = stcb;
  151. sctp_clog.x.strlog.n_tsn = tsn;
  152. sctp_clog.x.strlog.n_sseq = sseq;
  153. sctp_clog.x.strlog.e_tsn = 0;
  154. sctp_clog.x.strlog.e_sseq = 0;
  155. sctp_clog.x.strlog.strm = stream;
  156. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  157. SCTP_LOG_EVENT_STRM,
  158. from,
  159. sctp_clog.x.misc.log1,
  160. sctp_clog.x.misc.log2,
  161. sctp_clog.x.misc.log3,
  162. sctp_clog.x.misc.log4);
  163. #endif
  164. }
  165. void
  166. sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
  167. {
  168. #if defined(SCTP_LOCAL_TRACE_BUF)
  169. struct sctp_cwnd_log sctp_clog;
  170. sctp_clog.x.nagle.stcb = (void *)stcb;
  171. sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
  172. sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
  173. sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
  174. sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
  175. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  176. SCTP_LOG_EVENT_NAGLE,
  177. action,
  178. sctp_clog.x.misc.log1,
  179. sctp_clog.x.misc.log2,
  180. sctp_clog.x.misc.log3,
  181. sctp_clog.x.misc.log4);
  182. #endif
  183. }
  184. void
  185. sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
  186. {
  187. #if defined(SCTP_LOCAL_TRACE_BUF)
  188. struct sctp_cwnd_log sctp_clog;
  189. sctp_clog.x.sack.cumack = cumack;
  190. sctp_clog.x.sack.oldcumack = old_cumack;
  191. sctp_clog.x.sack.tsn = tsn;
  192. sctp_clog.x.sack.numGaps = gaps;
  193. sctp_clog.x.sack.numDups = dups;
  194. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  195. SCTP_LOG_EVENT_SACK,
  196. from,
  197. sctp_clog.x.misc.log1,
  198. sctp_clog.x.misc.log2,
  199. sctp_clog.x.misc.log3,
  200. sctp_clog.x.misc.log4);
  201. #endif
  202. }
  203. void
  204. sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
  205. {
  206. #if defined(SCTP_LOCAL_TRACE_BUF)
  207. struct sctp_cwnd_log sctp_clog;
  208. memset(&sctp_clog, 0, sizeof(sctp_clog));
  209. sctp_clog.x.map.base = map;
  210. sctp_clog.x.map.cum = cum;
  211. sctp_clog.x.map.high = high;
  212. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  213. SCTP_LOG_EVENT_MAP,
  214. from,
  215. sctp_clog.x.misc.log1,
  216. sctp_clog.x.misc.log2,
  217. sctp_clog.x.misc.log3,
  218. sctp_clog.x.misc.log4);
  219. #endif
  220. }
  221. void
  222. sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
  223. {
  224. #if defined(SCTP_LOCAL_TRACE_BUF)
  225. struct sctp_cwnd_log sctp_clog;
  226. memset(&sctp_clog, 0, sizeof(sctp_clog));
  227. sctp_clog.x.fr.largest_tsn = biggest_tsn;
  228. sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
  229. sctp_clog.x.fr.tsn = tsn;
  230. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  231. SCTP_LOG_EVENT_FR,
  232. from,
  233. sctp_clog.x.misc.log1,
  234. sctp_clog.x.misc.log2,
  235. sctp_clog.x.misc.log3,
  236. sctp_clog.x.misc.log4);
  237. #endif
  238. }
  239. #ifdef SCTP_MBUF_LOGGING
  240. void
  241. sctp_log_mb(struct mbuf *m, int from)
  242. {
  243. #if defined(SCTP_LOCAL_TRACE_BUF)
  244. struct sctp_cwnd_log sctp_clog;
  245. sctp_clog.x.mb.mp = m;
  246. sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
  247. sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
  248. sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
  249. if (SCTP_BUF_IS_EXTENDED(m)) {
  250. sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
  251. #if defined(__APPLE__) && !defined(__Userspace__)
  252. /* APPLE does not use a ref_cnt, but a forward/backward ref queue */
  253. #else
  254. sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
  255. #endif
  256. } else {
  257. sctp_clog.x.mb.ext = 0;
  258. sctp_clog.x.mb.refcnt = 0;
  259. }
  260. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  261. SCTP_LOG_EVENT_MBUF,
  262. from,
  263. sctp_clog.x.misc.log1,
  264. sctp_clog.x.misc.log2,
  265. sctp_clog.x.misc.log3,
  266. sctp_clog.x.misc.log4);
  267. #endif
  268. }
  269. void
  270. sctp_log_mbc(struct mbuf *m, int from)
  271. {
  272. struct mbuf *mat;
  273. for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
  274. sctp_log_mb(mat, from);
  275. }
  276. }
  277. #endif
  278. void
  279. sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
  280. {
  281. #if defined(SCTP_LOCAL_TRACE_BUF)
  282. struct sctp_cwnd_log sctp_clog;
  283. if (control == NULL) {
  284. SCTP_PRINTF("Gak log of NULL?\n");
  285. return;
  286. }
  287. sctp_clog.x.strlog.stcb = control->stcb;
  288. sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
  289. sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
  290. sctp_clog.x.strlog.strm = control->sinfo_stream;
  291. if (poschk != NULL) {
  292. sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
  293. sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
  294. } else {
  295. sctp_clog.x.strlog.e_tsn = 0;
  296. sctp_clog.x.strlog.e_sseq = 0;
  297. }
  298. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  299. SCTP_LOG_EVENT_STRM,
  300. from,
  301. sctp_clog.x.misc.log1,
  302. sctp_clog.x.misc.log2,
  303. sctp_clog.x.misc.log3,
  304. sctp_clog.x.misc.log4);
  305. #endif
  306. }
  307. void
  308. sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
  309. {
  310. #if defined(SCTP_LOCAL_TRACE_BUF)
  311. struct sctp_cwnd_log sctp_clog;
  312. sctp_clog.x.cwnd.net = net;
  313. if (stcb->asoc.send_queue_cnt > 255)
  314. sctp_clog.x.cwnd.cnt_in_send = 255;
  315. else
  316. sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
  317. if (stcb->asoc.stream_queue_cnt > 255)
  318. sctp_clog.x.cwnd.cnt_in_str = 255;
  319. else
  320. sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
  321. if (net) {
  322. sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
  323. sctp_clog.x.cwnd.inflight = net->flight_size;
  324. sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
  325. sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
  326. sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
  327. }
  328. if (SCTP_CWNDLOG_PRESEND == from) {
  329. sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
  330. }
  331. sctp_clog.x.cwnd.cwnd_augment = augment;
  332. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  333. SCTP_LOG_EVENT_CWND,
  334. from,
  335. sctp_clog.x.misc.log1,
  336. sctp_clog.x.misc.log2,
  337. sctp_clog.x.misc.log3,
  338. sctp_clog.x.misc.log4);
  339. #endif
  340. }
  341. #if !defined(__APPLE__) && !defined(__Userspace__)
  342. void
  343. sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
  344. {
  345. #if defined(SCTP_LOCAL_TRACE_BUF)
  346. struct sctp_cwnd_log sctp_clog;
  347. memset(&sctp_clog, 0, sizeof(sctp_clog));
  348. if (inp) {
  349. sctp_clog.x.lock.sock = (void *) inp->sctp_socket;
  350. } else {
  351. sctp_clog.x.lock.sock = (void *) NULL;
  352. }
  353. sctp_clog.x.lock.inp = (void *) inp;
  354. #if defined(__FreeBSD__)
  355. if (stcb) {
  356. sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
  357. } else {
  358. sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
  359. }
  360. if (inp) {
  361. sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
  362. sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
  363. } else {
  364. sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
  365. sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
  366. }
  367. sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
  368. if (inp && (inp->sctp_socket)) {
  369. sctp_clog.x.lock.sock_lock = mtx_owned(SOCK_MTX(inp->sctp_socket));
  370. sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(SOCKBUF_MTX(&inp->sctp_socket->so_rcv));
  371. sctp_clog.x.lock.socksndbuf_lock = mtx_owned(SOCKBUF_MTX(&inp->sctp_socket->so_snd));
  372. } else {
  373. sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
  374. sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
  375. sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
  376. }
  377. #endif
  378. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  379. SCTP_LOG_LOCK_EVENT,
  380. from,
  381. sctp_clog.x.misc.log1,
  382. sctp_clog.x.misc.log2,
  383. sctp_clog.x.misc.log3,
  384. sctp_clog.x.misc.log4);
  385. #endif
  386. }
  387. #endif
  388. void
  389. sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
  390. {
  391. #if defined(SCTP_LOCAL_TRACE_BUF)
  392. struct sctp_cwnd_log sctp_clog;
  393. memset(&sctp_clog, 0, sizeof(sctp_clog));
  394. sctp_clog.x.cwnd.net = net;
  395. sctp_clog.x.cwnd.cwnd_new_value = error;
  396. sctp_clog.x.cwnd.inflight = net->flight_size;
  397. sctp_clog.x.cwnd.cwnd_augment = burst;
  398. if (stcb->asoc.send_queue_cnt > 255)
  399. sctp_clog.x.cwnd.cnt_in_send = 255;
  400. else
  401. sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
  402. if (stcb->asoc.stream_queue_cnt > 255)
  403. sctp_clog.x.cwnd.cnt_in_str = 255;
  404. else
  405. sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
  406. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  407. SCTP_LOG_EVENT_MAXBURST,
  408. from,
  409. sctp_clog.x.misc.log1,
  410. sctp_clog.x.misc.log2,
  411. sctp_clog.x.misc.log3,
  412. sctp_clog.x.misc.log4);
  413. #endif
  414. }
  415. void
  416. sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
  417. {
  418. #if defined(SCTP_LOCAL_TRACE_BUF)
  419. struct sctp_cwnd_log sctp_clog;
  420. sctp_clog.x.rwnd.rwnd = peers_rwnd;
  421. sctp_clog.x.rwnd.send_size = snd_size;
  422. sctp_clog.x.rwnd.overhead = overhead;
  423. sctp_clog.x.rwnd.new_rwnd = 0;
  424. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  425. SCTP_LOG_EVENT_RWND,
  426. from,
  427. sctp_clog.x.misc.log1,
  428. sctp_clog.x.misc.log2,
  429. sctp_clog.x.misc.log3,
  430. sctp_clog.x.misc.log4);
  431. #endif
  432. }
  433. void
  434. sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
  435. {
  436. #if defined(SCTP_LOCAL_TRACE_BUF)
  437. struct sctp_cwnd_log sctp_clog;
  438. sctp_clog.x.rwnd.rwnd = peers_rwnd;
  439. sctp_clog.x.rwnd.send_size = flight_size;
  440. sctp_clog.x.rwnd.overhead = overhead;
  441. sctp_clog.x.rwnd.new_rwnd = a_rwndval;
  442. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  443. SCTP_LOG_EVENT_RWND,
  444. from,
  445. sctp_clog.x.misc.log1,
  446. sctp_clog.x.misc.log2,
  447. sctp_clog.x.misc.log3,
  448. sctp_clog.x.misc.log4);
  449. #endif
  450. }
  451. #ifdef SCTP_MBCNT_LOGGING
  452. static void
  453. sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
  454. {
  455. #if defined(SCTP_LOCAL_TRACE_BUF)
  456. struct sctp_cwnd_log sctp_clog;
  457. sctp_clog.x.mbcnt.total_queue_size = total_oq;
  458. sctp_clog.x.mbcnt.size_change = book;
  459. sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
  460. sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
  461. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  462. SCTP_LOG_EVENT_MBCNT,
  463. from,
  464. sctp_clog.x.misc.log1,
  465. sctp_clog.x.misc.log2,
  466. sctp_clog.x.misc.log3,
  467. sctp_clog.x.misc.log4);
  468. #endif
  469. }
  470. #endif
  471. void
  472. sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
  473. {
  474. #if defined(SCTP_LOCAL_TRACE_BUF)
  475. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  476. SCTP_LOG_MISC_EVENT,
  477. from,
  478. a, b, c, d);
  479. #endif
  480. }
  481. void
  482. sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
  483. {
  484. #if defined(SCTP_LOCAL_TRACE_BUF)
  485. struct sctp_cwnd_log sctp_clog;
  486. sctp_clog.x.wake.stcb = (void *)stcb;
  487. sctp_clog.x.wake.wake_cnt = wake_cnt;
  488. sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
  489. sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
  490. sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
  491. if (stcb->asoc.stream_queue_cnt < 0xff)
  492. sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
  493. else
  494. sctp_clog.x.wake.stream_qcnt = 0xff;
  495. if (stcb->asoc.chunks_on_out_queue < 0xff)
  496. sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
  497. else
  498. sctp_clog.x.wake.chunks_on_oque = 0xff;
  499. sctp_clog.x.wake.sctpflags = 0;
  500. /* set in the defered mode stuff */
  501. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
  502. sctp_clog.x.wake.sctpflags |= 1;
  503. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
  504. sctp_clog.x.wake.sctpflags |= 2;
  505. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
  506. sctp_clog.x.wake.sctpflags |= 4;
  507. /* what about the sb */
  508. if (stcb->sctp_socket) {
  509. struct socket *so = stcb->sctp_socket;
  510. sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
  511. } else {
  512. sctp_clog.x.wake.sbflags = 0xff;
  513. }
  514. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  515. SCTP_LOG_EVENT_WAKE,
  516. from,
  517. sctp_clog.x.misc.log1,
  518. sctp_clog.x.misc.log2,
  519. sctp_clog.x.misc.log3,
  520. sctp_clog.x.misc.log4);
  521. #endif
  522. }
  523. void
  524. sctp_log_block(uint8_t from, struct sctp_association *asoc, ssize_t sendlen)
  525. {
  526. #if defined(SCTP_LOCAL_TRACE_BUF)
  527. struct sctp_cwnd_log sctp_clog;
  528. sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
  529. sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
  530. sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
  531. sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
  532. sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
  533. sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight/1024);
  534. sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
  535. SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
  536. SCTP_LOG_EVENT_BLOCK,
  537. from,
  538. sctp_clog.x.misc.log1,
  539. sctp_clog.x.misc.log2,
  540. sctp_clog.x.misc.log3,
  541. sctp_clog.x.misc.log4);
  542. #endif
  543. }
  544. int
  545. sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
  546. {
  547. /* May need to fix this if ktrdump does not work */
  548. return (0);
  549. }
  550. #ifdef SCTP_AUDITING_ENABLED
  551. uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
  552. static int sctp_audit_indx = 0;
  553. static
  554. void
  555. sctp_print_audit_report(void)
  556. {
  557. int i;
  558. int cnt;
  559. cnt = 0;
  560. for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
  561. if ((sctp_audit_data[i][0] == 0xe0) &&
  562. (sctp_audit_data[i][1] == 0x01)) {
  563. cnt = 0;
  564. SCTP_PRINTF("\n");
  565. } else if (sctp_audit_data[i][0] == 0xf0) {
  566. cnt = 0;
  567. SCTP_PRINTF("\n");
  568. } else if ((sctp_audit_data[i][0] == 0xc0) &&
  569. (sctp_audit_data[i][1] == 0x01)) {
  570. SCTP_PRINTF("\n");
  571. cnt = 0;
  572. }
  573. SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
  574. (uint32_t) sctp_audit_data[i][1]);
  575. cnt++;
  576. if ((cnt % 14) == 0)
  577. SCTP_PRINTF("\n");
  578. }
  579. for (i = 0; i < sctp_audit_indx; i++) {
  580. if ((sctp_audit_data[i][0] == 0xe0) &&
  581. (sctp_audit_data[i][1] == 0x01)) {
  582. cnt = 0;
  583. SCTP_PRINTF("\n");
  584. } else if (sctp_audit_data[i][0] == 0xf0) {
  585. cnt = 0;
  586. SCTP_PRINTF("\n");
  587. } else if ((sctp_audit_data[i][0] == 0xc0) &&
  588. (sctp_audit_data[i][1] == 0x01)) {
  589. SCTP_PRINTF("\n");
  590. cnt = 0;
  591. }
  592. SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
  593. (uint32_t) sctp_audit_data[i][1]);
  594. cnt++;
  595. if ((cnt % 14) == 0)
  596. SCTP_PRINTF("\n");
  597. }
  598. SCTP_PRINTF("\n");
  599. }
  600. void
  601. sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  602. struct sctp_nets *net)
  603. {
  604. int resend_cnt, tot_out, rep, tot_book_cnt;
  605. struct sctp_nets *lnet;
  606. struct sctp_tmit_chunk *chk;
  607. sctp_audit_data[sctp_audit_indx][0] = 0xAA;
  608. sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
  609. sctp_audit_indx++;
  610. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  611. sctp_audit_indx = 0;
  612. }
  613. if (inp == NULL) {
  614. sctp_audit_data[sctp_audit_indx][0] = 0xAF;
  615. sctp_audit_data[sctp_audit_indx][1] = 0x01;
  616. sctp_audit_indx++;
  617. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  618. sctp_audit_indx = 0;
  619. }
  620. return;
  621. }
  622. if (stcb == NULL) {
  623. sctp_audit_data[sctp_audit_indx][0] = 0xAF;
  624. sctp_audit_data[sctp_audit_indx][1] = 0x02;
  625. sctp_audit_indx++;
  626. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  627. sctp_audit_indx = 0;
  628. }
  629. return;
  630. }
  631. sctp_audit_data[sctp_audit_indx][0] = 0xA1;
  632. sctp_audit_data[sctp_audit_indx][1] =
  633. (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
  634. sctp_audit_indx++;
  635. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  636. sctp_audit_indx = 0;
  637. }
  638. rep = 0;
  639. tot_book_cnt = 0;
  640. resend_cnt = tot_out = 0;
  641. TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  642. if (chk->sent == SCTP_DATAGRAM_RESEND) {
  643. resend_cnt++;
  644. } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
  645. tot_out += chk->book_size;
  646. tot_book_cnt++;
  647. }
  648. }
  649. if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
  650. sctp_audit_data[sctp_audit_indx][0] = 0xAF;
  651. sctp_audit_data[sctp_audit_indx][1] = 0xA1;
  652. sctp_audit_indx++;
  653. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  654. sctp_audit_indx = 0;
  655. }
  656. SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
  657. resend_cnt, stcb->asoc.sent_queue_retran_cnt);
  658. rep = 1;
  659. stcb->asoc.sent_queue_retran_cnt = resend_cnt;
  660. sctp_audit_data[sctp_audit_indx][0] = 0xA2;
  661. sctp_audit_data[sctp_audit_indx][1] =
  662. (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
  663. sctp_audit_indx++;
  664. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  665. sctp_audit_indx = 0;
  666. }
  667. }
  668. if (tot_out != stcb->asoc.total_flight) {
  669. sctp_audit_data[sctp_audit_indx][0] = 0xAF;
  670. sctp_audit_data[sctp_audit_indx][1] = 0xA2;
  671. sctp_audit_indx++;
  672. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  673. sctp_audit_indx = 0;
  674. }
  675. rep = 1;
  676. SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
  677. (int)stcb->asoc.total_flight);
  678. stcb->asoc.total_flight = tot_out;
  679. }
  680. if (tot_book_cnt != stcb->asoc.total_flight_count) {
  681. sctp_audit_data[sctp_audit_indx][0] = 0xAF;
  682. sctp_audit_data[sctp_audit_indx][1] = 0xA5;
  683. sctp_audit_indx++;
  684. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  685. sctp_audit_indx = 0;
  686. }
  687. rep = 1;
  688. SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
  689. stcb->asoc.total_flight_count = tot_book_cnt;
  690. }
  691. tot_out = 0;
  692. TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
  693. tot_out += lnet->flight_size;
  694. }
  695. if (tot_out != stcb->asoc.total_flight) {
  696. sctp_audit_data[sctp_audit_indx][0] = 0xAF;
  697. sctp_audit_data[sctp_audit_indx][1] = 0xA3;
  698. sctp_audit_indx++;
  699. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  700. sctp_audit_indx = 0;
  701. }
  702. rep = 1;
  703. SCTP_PRINTF("real flight:%d net total was %d\n",
  704. stcb->asoc.total_flight, tot_out);
  705. /* now corrective action */
  706. TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
  707. tot_out = 0;
  708. TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  709. if ((chk->whoTo == lnet) &&
  710. (chk->sent < SCTP_DATAGRAM_RESEND)) {
  711. tot_out += chk->book_size;
  712. }
  713. }
  714. if (lnet->flight_size != tot_out) {
  715. SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
  716. (void *)lnet, lnet->flight_size,
  717. tot_out);
  718. lnet->flight_size = tot_out;
  719. }
  720. }
  721. }
  722. if (rep) {
  723. sctp_print_audit_report();
  724. }
  725. }
  726. void
  727. sctp_audit_log(uint8_t ev, uint8_t fd)
  728. {
  729. sctp_audit_data[sctp_audit_indx][0] = ev;
  730. sctp_audit_data[sctp_audit_indx][1] = fd;
  731. sctp_audit_indx++;
  732. if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
  733. sctp_audit_indx = 0;
  734. }
  735. }
  736. #endif
  737. /*
  738. * The conversion from time to ticks and vice versa is done by rounding
  739. * upwards. This way we can test in the code the time to be positive and
  740. * know that this corresponds to a positive number of ticks.
  741. */
  742. uint32_t
  743. sctp_msecs_to_ticks(uint32_t msecs)
  744. {
  745. uint64_t temp;
  746. uint32_t ticks;
  747. if (hz == 1000) {
  748. ticks = msecs;
  749. } else {
  750. temp = (((uint64_t)msecs * hz) + 999) / 1000;
  751. if (temp > UINT32_MAX) {
  752. ticks = UINT32_MAX;
  753. } else {
  754. ticks = (uint32_t)temp;
  755. }
  756. }
  757. return (ticks);
  758. }
  759. uint32_t
  760. sctp_ticks_to_msecs(uint32_t ticks)
  761. {
  762. uint64_t temp;
  763. uint32_t msecs;
  764. if (hz == 1000) {
  765. msecs = ticks;
  766. } else {
  767. temp = (((uint64_t)ticks * 1000) + (hz - 1)) / hz;
  768. if (temp > UINT32_MAX) {
  769. msecs = UINT32_MAX;
  770. } else {
  771. msecs = (uint32_t)temp;
  772. }
  773. }
  774. return (msecs);
  775. }
  776. uint32_t
  777. sctp_secs_to_ticks(uint32_t secs)
  778. {
  779. uint64_t temp;
  780. uint32_t ticks;
  781. temp = (uint64_t)secs * hz;
  782. if (temp > UINT32_MAX) {
  783. ticks = UINT32_MAX;
  784. } else {
  785. ticks = (uint32_t)temp;
  786. }
  787. return (ticks);
  788. }
  789. uint32_t
  790. sctp_ticks_to_secs(uint32_t ticks)
  791. {
  792. uint64_t temp;
  793. uint32_t secs;
  794. temp = ((uint64_t)ticks + (hz - 1)) / hz;
  795. if (temp > UINT32_MAX) {
  796. secs = UINT32_MAX;
  797. } else {
  798. secs = (uint32_t)temp;
  799. }
  800. return (secs);
  801. }
  802. /*
  803. * sctp_stop_timers_for_shutdown() should be called
  804. * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
  805. * state to make sure that all timers are stopped.
  806. */
  807. void
  808. sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
  809. {
  810. struct sctp_inpcb *inp;
  811. struct sctp_nets *net;
  812. inp = stcb->sctp_ep;
  813. sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
  814. SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
  815. sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
  816. SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
  817. sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
  818. SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
  819. sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
  820. SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
  821. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  822. sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
  823. SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
  824. sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
  825. SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
  826. }
  827. }
  828. void
  829. sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
  830. {
  831. struct sctp_inpcb *inp;
  832. struct sctp_nets *net;
  833. inp = stcb->sctp_ep;
  834. sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
  835. SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
  836. sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
  837. SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
  838. if (stop_assoc_kill_timer) {
  839. sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
  840. SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
  841. }
  842. sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
  843. SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
  844. sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
  845. SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
  846. sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
  847. SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
  848. /* Mobility adaptation */
  849. sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
  850. SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
  851. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  852. sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
  853. SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
  854. sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
  855. SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
  856. sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
  857. SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
  858. sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
  859. SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
  860. sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
  861. SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
  862. sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
  863. SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
  864. sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
  865. SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
  866. }
  867. }
  868. /*
  869. * A list of sizes based on typical mtu's, used only if next hop size not
  870. * returned. These values MUST be multiples of 4 and MUST be ordered.
  871. */
  872. static uint32_t sctp_mtu_sizes[] = {
  873. 68,
  874. 296,
  875. 508,
  876. 512,
  877. 544,
  878. 576,
  879. 1004,
  880. 1492,
  881. 1500,
  882. 1536,
  883. 2000,
  884. 2048,
  885. 4352,
  886. 4464,
  887. 8168,
  888. 17912,
  889. 32000,
  890. 65532
  891. };
  892. /*
  893. * Return the largest MTU in sctp_mtu_sizes smaller than val.
  894. * If val is smaller than the minimum, just return the largest
  895. * multiple of 4 smaller or equal to val.
  896. * Ensure that the result is a multiple of 4.
  897. */
  898. uint32_t
  899. sctp_get_prev_mtu(uint32_t val)
  900. {
  901. uint32_t i;
  902. val &= 0xfffffffc;
  903. if (val <= sctp_mtu_sizes[0]) {
  904. return (val);
  905. }
  906. for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
  907. if (val <= sctp_mtu_sizes[i]) {
  908. break;
  909. }
  910. }
  911. KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
  912. ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
  913. return (sctp_mtu_sizes[i - 1]);
  914. }
  915. /*
  916. * Return the smallest MTU in sctp_mtu_sizes larger than val.
  917. * If val is larger than the maximum, just return the largest multiple of 4 smaller
  918. * or equal to val.
  919. * Ensure that the result is a multiple of 4.
  920. */
  921. uint32_t
  922. sctp_get_next_mtu(uint32_t val)
  923. {
  924. /* select another MTU that is just bigger than this one */
  925. uint32_t i;
  926. val &= 0xfffffffc;
  927. for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
  928. if (val < sctp_mtu_sizes[i]) {
  929. KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
  930. ("sctp_mtu_sizes[%u] not a multiple of 4", i));
  931. return (sctp_mtu_sizes[i]);
  932. }
  933. }
  934. return (val);
  935. }
  936. void
  937. sctp_fill_random_store(struct sctp_pcb *m)
  938. {
  939. /*
  940. * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
  941. * our counter. The result becomes our good random numbers and we
  942. * then setup to give these out. Note that we do no locking to
  943. * protect this. This is ok, since if competing folks call this we
  944. * will get more gobbled gook in the random store which is what we
  945. * want. There is a danger that two guys will use the same random
  946. * numbers, but thats ok too since that is random as well :->
  947. */
  948. m->store_at = 0;
  949. #if defined(__Userspace__) && defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  950. for (int i = 0; i < (int) (sizeof(m->random_store) / sizeof(m->random_store[0])); i++) {
  951. m->random_store[i] = (uint8_t) rand();
  952. }
  953. #else
  954. (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
  955. sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
  956. sizeof(m->random_counter), (uint8_t *)m->random_store);
  957. #endif
  958. m->random_counter++;
  959. }
  960. uint32_t
  961. sctp_select_initial_TSN(struct sctp_pcb *inp)
  962. {
  963. /*
  964. * A true implementation should use random selection process to get
  965. * the initial stream sequence number, using RFC1750 as a good
  966. * guideline
  967. */
  968. uint32_t x, *xp;
  969. uint8_t *p;
  970. int store_at, new_store;
  971. if (inp->initial_sequence_debug != 0) {
  972. uint32_t ret;
  973. ret = inp->initial_sequence_debug;
  974. inp->initial_sequence_debug++;
  975. return (ret);
  976. }
  977. retry:
  978. store_at = inp->store_at;
  979. new_store = store_at + sizeof(uint32_t);
  980. if (new_store >= (SCTP_SIGNATURE_SIZE-3)) {
  981. new_store = 0;
  982. }
  983. if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
  984. goto retry;
  985. }
  986. if (new_store == 0) {
  987. /* Refill the random store */
  988. sctp_fill_random_store(inp);
  989. }
  990. p = &inp->random_store[store_at];
  991. xp = (uint32_t *)p;
  992. x = *xp;
  993. return (x);
  994. }
  995. uint32_t
  996. sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
  997. {
  998. uint32_t x;
  999. struct timeval now;
  1000. if (check) {
  1001. (void)SCTP_GETTIME_TIMEVAL(&now);
  1002. }
  1003. for (;;) {
  1004. x = sctp_select_initial_TSN(&inp->sctp_ep);
  1005. if (x == 0) {
  1006. /* we never use 0 */
  1007. continue;
  1008. }
  1009. if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
  1010. break;
  1011. }
  1012. }
  1013. return (x);
  1014. }
  1015. int32_t
  1016. sctp_map_assoc_state(int kernel_state)
  1017. {
  1018. int32_t user_state;
  1019. if (kernel_state & SCTP_STATE_WAS_ABORTED) {
  1020. user_state = SCTP_CLOSED;
  1021. } else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
  1022. user_state = SCTP_SHUTDOWN_PENDING;
  1023. } else {
  1024. switch (kernel_state & SCTP_STATE_MASK) {
  1025. case SCTP_STATE_EMPTY:
  1026. user_state = SCTP_CLOSED;
  1027. break;
  1028. case SCTP_STATE_INUSE:
  1029. user_state = SCTP_CLOSED;
  1030. break;
  1031. case SCTP_STATE_COOKIE_WAIT:
  1032. user_state = SCTP_COOKIE_WAIT;
  1033. break;
  1034. case SCTP_STATE_COOKIE_ECHOED:
  1035. user_state = SCTP_COOKIE_ECHOED;
  1036. break;
  1037. case SCTP_STATE_OPEN:
  1038. user_state = SCTP_ESTABLISHED;
  1039. break;
  1040. case SCTP_STATE_SHUTDOWN_SENT:
  1041. user_state = SCTP_SHUTDOWN_SENT;
  1042. break;
  1043. case SCTP_STATE_SHUTDOWN_RECEIVED:
  1044. user_state = SCTP_SHUTDOWN_RECEIVED;
  1045. break;
  1046. case SCTP_STATE_SHUTDOWN_ACK_SENT:
  1047. user_state = SCTP_SHUTDOWN_ACK_SENT;
  1048. break;
  1049. default:
  1050. user_state = SCTP_CLOSED;
  1051. break;
  1052. }
  1053. }
  1054. return (user_state);
  1055. }
  1056. int
  1057. sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  1058. uint32_t override_tag, uint32_t initial_tsn, uint32_t vrf_id,
  1059. uint16_t o_strms)
  1060. {
  1061. struct sctp_association *asoc;
  1062. /*
  1063. * Anything set to zero is taken care of by the allocation routine's
  1064. * bzero
  1065. */
  1066. /*
  1067. * Up front select what scoping to apply on addresses I tell my peer
  1068. * Not sure what to do with these right now, we will need to come up
  1069. * with a way to set them. We may need to pass them through from the
  1070. * caller in the sctp_aloc_assoc() function.
  1071. */
  1072. int i;
  1073. #if defined(SCTP_DETAILED_STR_STATS)
  1074. int j;
  1075. #endif
  1076. asoc = &stcb->asoc;
  1077. /* init all variables to a known value. */
  1078. SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
  1079. asoc->max_burst = inp->sctp_ep.max_burst;
  1080. asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
  1081. asoc->heart_beat_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
  1082. asoc->cookie_life = inp->sctp_ep.def_cookie_life;
  1083. asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
  1084. asoc->ecn_supported = inp->ecn_supported;
  1085. asoc->prsctp_supported = inp->prsctp_supported;
  1086. asoc->auth_supported = inp->auth_supported;
  1087. asoc->asconf_supported = inp->asconf_supported;
  1088. asoc->reconfig_supported = inp->reconfig_supported;
  1089. asoc->nrsack_supported = inp->nrsack_supported;
  1090. asoc->pktdrop_supported = inp->pktdrop_supported;
  1091. asoc->idata_supported = inp->idata_supported;
  1092. asoc->sctp_cmt_pf = (uint8_t)0;
  1093. asoc->sctp_frag_point = inp->sctp_frag_point;
  1094. asoc->sctp_features = inp->sctp_features;
  1095. asoc->default_dscp = inp->sctp_ep.default_dscp;
  1096. asoc->max_cwnd = inp->max_cwnd;
  1097. #ifdef INET6
  1098. if (inp->sctp_ep.default_flowlabel) {
  1099. asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
  1100. } else {
  1101. if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
  1102. asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
  1103. asoc->default_flowlabel &= 0x000fffff;
  1104. asoc->default_flowlabel |= 0x80000000;
  1105. } else {
  1106. asoc->default_flowlabel = 0;
  1107. }
  1108. }
  1109. #endif
  1110. asoc->sb_send_resv = 0;
  1111. if (override_tag) {
  1112. asoc->my_vtag = override_tag;
  1113. } else {
  1114. asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
  1115. }
  1116. /* Get the nonce tags */
  1117. asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
  1118. asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
  1119. asoc->vrf_id = vrf_id;
  1120. #ifdef SCTP_ASOCLOG_OF_TSNS
  1121. asoc->tsn_in_at = 0;
  1122. asoc->tsn_out_at = 0;
  1123. asoc->tsn_in_wrapped = 0;
  1124. asoc->tsn_out_wrapped = 0;
  1125. asoc->cumack_log_at = 0;
  1126. asoc->cumack_log_atsnt = 0;
  1127. #endif
  1128. #ifdef SCTP_FS_SPEC_LOG
  1129. asoc->fs_index = 0;
  1130. #endif
  1131. asoc->refcnt = 0;
  1132. asoc->assoc_up_sent = 0;
  1133. if (override_tag) {
  1134. asoc->init_seq_number = initial_tsn;
  1135. } else {
  1136. asoc->init_seq_number = sctp_select_initial_TSN(&inp->sctp_ep);
  1137. }
  1138. asoc->asconf_seq_out = asoc->init_seq_number;
  1139. asoc->str_reset_seq_out = asoc->init_seq_number;
  1140. asoc->sending_seq = asoc->init_seq_number;
  1141. asoc->asconf_seq_out_acked = asoc->init_seq_number - 1;
  1142. /* we are optimistic here */
  1143. asoc->peer_supports_nat = 0;
  1144. asoc->sent_queue_retran_cnt = 0;
  1145. /* for CMT */
  1146. asoc->last_net_cmt_send_started = NULL;
  1147. asoc->last_acked_seq = asoc->init_seq_number - 1;
  1148. asoc->advanced_peer_ack_point = asoc->init_seq_number - 1;
  1149. asoc->asconf_seq_in = asoc->init_seq_number - 1;
  1150. /* here we are different, we hold the next one we expect */
  1151. asoc->str_reset_seq_in = asoc->init_seq_number;
  1152. asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
  1153. asoc->initial_rto = inp->sctp_ep.initial_rto;
  1154. asoc->default_mtu = inp->sctp_ep.default_mtu;
  1155. asoc->max_init_times = inp->sctp_ep.max_init_times;
  1156. asoc->max_send_times = inp->sctp_ep.max_send_times;
  1157. asoc->def_net_failure = inp->sctp_ep.def_net_failure;
  1158. asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
  1159. asoc->free_chunk_cnt = 0;
  1160. asoc->iam_blocking = 0;
  1161. asoc->context = inp->sctp_context;
  1162. asoc->local_strreset_support = inp->local_strreset_support;
  1163. asoc->def_send = inp->def_send;
  1164. asoc->delayed_ack = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
  1165. asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
  1166. asoc->pr_sctp_cnt = 0;
  1167. asoc->total_output_queue_size = 0;
  1168. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  1169. asoc->scope.ipv6_addr_legal = 1;
  1170. if (SCTP_IPV6_V6ONLY(inp) == 0) {
  1171. asoc->scope.ipv4_addr_legal = 1;
  1172. } else {
  1173. asoc->scope.ipv4_addr_legal = 0;
  1174. }
  1175. #if defined(__Userspace__)
  1176. asoc->scope.conn_addr_legal = 0;
  1177. #endif
  1178. } else {
  1179. asoc->scope.ipv6_addr_legal = 0;
  1180. #if defined(__Userspace__)
  1181. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
  1182. asoc->scope.conn_addr_legal = 1;
  1183. asoc->scope.ipv4_addr_legal = 0;
  1184. } else {
  1185. asoc->scope.conn_addr_legal = 0;
  1186. asoc->scope.ipv4_addr_legal = 1;
  1187. }
  1188. #else
  1189. asoc->scope.ipv4_addr_legal = 1;
  1190. #endif
  1191. }
  1192. asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
  1193. asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
  1194. asoc->smallest_mtu = 0;
  1195. asoc->minrto = inp->sctp_ep.sctp_minrto;
  1196. asoc->maxrto = inp->sctp_ep.sctp_maxrto;
  1197. asoc->stream_locked_on = 0;
  1198. asoc->ecn_echo_cnt_onq = 0;
  1199. asoc->stream_locked = 0;
  1200. asoc->send_sack = 1;
  1201. LIST_INIT(&asoc->sctp_restricted_addrs);
  1202. TAILQ_INIT(&asoc->nets);
  1203. TAILQ_INIT(&asoc->pending_reply_queue);
  1204. TAILQ_INIT(&asoc->asconf_ack_sent);
  1205. /* Setup to fill the hb random cache at first HB */
  1206. asoc->hb_random_idx = 4;
  1207. asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
  1208. stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
  1209. stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
  1210. stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
  1211. stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
  1212. /*
  1213. * Now the stream parameters, here we allocate space for all streams
  1214. * that we request by default.
  1215. */
  1216. asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
  1217. o_strms;
  1218. SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
  1219. asoc->streamoutcnt * sizeof(struct sctp_stream_out),
  1220. SCTP_M_STRMO);
  1221. if (asoc->strmout == NULL) {
  1222. /* big trouble no memory */
  1223. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
  1224. return (ENOMEM);
  1225. }
  1226. SCTP_TCB_LOCK(stcb);
  1227. for (i = 0; i < asoc->streamoutcnt; i++) {
  1228. /*
  1229. * inbound side must be set to 0xffff, also NOTE when we get
  1230. * the INIT-ACK back (for INIT sender) we MUST reduce the
  1231. * count (streamoutcnt) but first check if we sent to any of
  1232. * the upper streams that were dropped (if some were). Those
  1233. * that were dropped must be notified to the upper layer as
  1234. * failed to send.
  1235. */
  1236. TAILQ_INIT(&asoc->strmout[i].outqueue);
  1237. asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
  1238. asoc->strmout[i].chunks_on_queues = 0;
  1239. #if defined(SCTP_DETAILED_STR_STATS)
  1240. for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
  1241. asoc->strmout[i].abandoned_sent[j] = 0;
  1242. asoc->strmout[i].abandoned_unsent[j] = 0;
  1243. }
  1244. #else
  1245. asoc->strmout[i].abandoned_sent[0] = 0;
  1246. asoc->strmout[i].abandoned_unsent[0] = 0;
  1247. #endif
  1248. asoc->strmout[i].next_mid_ordered = 0;
  1249. asoc->strmout[i].next_mid_unordered = 0;
  1250. asoc->strmout[i].sid = i;
  1251. asoc->strmout[i].last_msg_incomplete = 0;
  1252. asoc->strmout[i].state = SCTP_STREAM_OPENING;
  1253. }
  1254. asoc->ss_functions.sctp_ss_init(stcb, asoc);
  1255. SCTP_TCB_UNLOCK(stcb);
  1256. /* Now the mapping array */
  1257. asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
  1258. SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
  1259. SCTP_M_MAP);
  1260. if (asoc->mapping_array == NULL) {
  1261. SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
  1262. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
  1263. return (ENOMEM);
  1264. }
  1265. memset(asoc->mapping_array, 0, asoc->mapping_array_size);
  1266. SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
  1267. SCTP_M_MAP);
  1268. if (asoc->nr_mapping_array == NULL) {
  1269. SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
  1270. SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
  1271. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
  1272. return (ENOMEM);
  1273. }
  1274. memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
  1275. /* Now the init of the other outqueues */
  1276. TAILQ_INIT(&asoc->free_chunks);
  1277. TAILQ_INIT(&asoc->control_send_queue);
  1278. TAILQ_INIT(&asoc->asconf_send_queue);
  1279. TAILQ_INIT(&asoc->send_queue);
  1280. TAILQ_INIT(&asoc->sent_queue);
  1281. TAILQ_INIT(&asoc->resetHead);
  1282. asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
  1283. TAILQ_INIT(&asoc->asconf_queue);
  1284. /* authentication fields */
  1285. asoc->authinfo.random = NULL;
  1286. asoc->authinfo.active_keyid = 0;
  1287. asoc->authinfo.assoc_key = NULL;
  1288. asoc->authinfo.assoc_keyid = 0;
  1289. asoc->authinfo.recv_key = NULL;
  1290. asoc->authinfo.recv_keyid = 0;
  1291. LIST_INIT(&asoc->shared_keys);
  1292. asoc->marked_retrans = 0;
  1293. asoc->port = inp->sctp_ep.port;
  1294. asoc->timoinit = 0;
  1295. asoc->timodata = 0;
  1296. asoc->timosack = 0;
  1297. asoc->timoshutdown = 0;
  1298. asoc->timoheartbeat = 0;
  1299. asoc->timocookie = 0;
  1300. asoc->timoshutdownack = 0;
  1301. (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
  1302. asoc->discontinuity_time = asoc->start_time;
  1303. for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
  1304. asoc->abandoned_unsent[i] = 0;
  1305. asoc->abandoned_sent[i] = 0;
  1306. }
  1307. /* sa_ignore MEMLEAK {memory is put in the assoc mapping array and freed later when
  1308. * the association is freed.
  1309. */
  1310. return (0);
  1311. }
  1312. void
  1313. sctp_print_mapping_array(struct sctp_association *asoc)
  1314. {
  1315. unsigned int i, limit;
  1316. SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
  1317. asoc->mapping_array_size,
  1318. asoc->mapping_array_base_tsn,
  1319. asoc->cumulative_tsn,
  1320. asoc->highest_tsn_inside_map,
  1321. asoc->highest_tsn_inside_nr_map);
  1322. for (limit = asoc->mapping_array_size; limit > 1; limit--) {
  1323. if (asoc->mapping_array[limit - 1] != 0) {
  1324. break;
  1325. }
  1326. }
  1327. SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
  1328. for (i = 0; i < limit; i++) {
  1329. SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
  1330. }
  1331. if (limit % 16)
  1332. SCTP_PRINTF("\n");
  1333. for (limit = asoc->mapping_array_size; limit > 1; limit--) {
  1334. if (asoc->nr_mapping_array[limit - 1]) {
  1335. break;
  1336. }
  1337. }
  1338. SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
  1339. for (i = 0; i < limit; i++) {
  1340. SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ': '\n');
  1341. }
  1342. if (limit % 16)
  1343. SCTP_PRINTF("\n");
  1344. }
  1345. int
  1346. sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
  1347. {
  1348. /* mapping array needs to grow */
  1349. uint8_t *new_array1, *new_array2;
  1350. uint32_t new_size;
  1351. new_size = asoc->mapping_array_size + ((needed+7)/8 + SCTP_MAPPING_ARRAY_INCR);
  1352. SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
  1353. SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
  1354. if ((new_array1 == NULL) || (new_array2 == NULL)) {
  1355. /* can't get more, forget it */
  1356. SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
  1357. if (new_array1) {
  1358. SCTP_FREE(new_array1, SCTP_M_MAP);
  1359. }
  1360. if (new_array2) {
  1361. SCTP_FREE(new_array2, SCTP_M_MAP);
  1362. }
  1363. return (-1);
  1364. }
  1365. memset(new_array1, 0, new_size);
  1366. memset(new_array2, 0, new_size);
  1367. memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
  1368. memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
  1369. SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
  1370. SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
  1371. asoc->mapping_array = new_array1;
  1372. asoc->nr_mapping_array = new_array2;
  1373. asoc->mapping_array_size = new_size;
  1374. return (0);
  1375. }
  1376. static void
  1377. sctp_iterator_work(struct sctp_iterator *it)
  1378. {
  1379. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1380. struct epoch_tracker et;
  1381. #endif
  1382. struct sctp_inpcb *tinp;
  1383. int iteration_count = 0;
  1384. int inp_skip = 0;
  1385. int first_in = 1;
  1386. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1387. NET_EPOCH_ENTER(et);
  1388. #endif
  1389. SCTP_INP_INFO_RLOCK();
  1390. SCTP_ITERATOR_LOCK();
  1391. sctp_it_ctl.cur_it = it;
  1392. if (it->inp) {
  1393. SCTP_INP_RLOCK(it->inp);
  1394. SCTP_INP_DECR_REF(it->inp);
  1395. }
  1396. if (it->inp == NULL) {
  1397. /* iterator is complete */
  1398. done_with_iterator:
  1399. sctp_it_ctl.cur_it = NULL;
  1400. SCTP_ITERATOR_UNLOCK();
  1401. SCTP_INP_INFO_RUNLOCK();
  1402. if (it->function_atend != NULL) {
  1403. (*it->function_atend) (it->pointer, it->val);
  1404. }
  1405. SCTP_FREE(it, SCTP_M_ITER);
  1406. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1407. NET_EPOCH_EXIT(et);
  1408. #endif
  1409. return;
  1410. }
  1411. select_a_new_ep:
  1412. if (first_in) {
  1413. first_in = 0;
  1414. } else {
  1415. SCTP_INP_RLOCK(it->inp);
  1416. }
  1417. while (((it->pcb_flags) &&
  1418. ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
  1419. ((it->pcb_features) &&
  1420. ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
  1421. /* endpoint flags or features don't match, so keep looking */
  1422. if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
  1423. SCTP_INP_RUNLOCK(it->inp);
  1424. goto done_with_iterator;
  1425. }
  1426. tinp = it->inp;
  1427. it->inp = LIST_NEXT(it->inp, sctp_list);
  1428. it->stcb = NULL;
  1429. SCTP_INP_RUNLOCK(tinp);
  1430. if (it->inp == NULL) {
  1431. goto done_with_iterator;
  1432. }
  1433. SCTP_INP_RLOCK(it->inp);
  1434. }
  1435. /* now go through each assoc which is in the desired state */
  1436. if (it->done_current_ep == 0) {
  1437. if (it->function_inp != NULL)
  1438. inp_skip = (*it->function_inp)(it->inp, it->pointer, it->val);
  1439. it->done_current_ep = 1;
  1440. }
  1441. if (it->stcb == NULL) {
  1442. /* run the per instance function */
  1443. it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
  1444. }
  1445. if ((inp_skip) || it->stcb == NULL) {
  1446. if (it->function_inp_end != NULL) {
  1447. inp_skip = (*it->function_inp_end)(it->inp,
  1448. it->pointer,
  1449. it->val);
  1450. }
  1451. SCTP_INP_RUNLOCK(it->inp);
  1452. goto no_stcb;
  1453. }
  1454. while (it->stcb != NULL) {
  1455. SCTP_TCB_LOCK(it->stcb);
  1456. if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
  1457. /* not in the right state... keep looking */
  1458. SCTP_TCB_UNLOCK(it->stcb);
  1459. goto next_assoc;
  1460. }
  1461. /* see if we have limited out the iterator loop */
  1462. iteration_count++;
  1463. if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
  1464. /* Pause to let others grab the lock */
  1465. atomic_add_int(&it->stcb->asoc.refcnt, 1);
  1466. SCTP_TCB_UNLOCK(it->stcb);
  1467. SCTP_INP_INCR_REF(it->inp);
  1468. SCTP_INP_RUNLOCK(it->inp);
  1469. SCTP_ITERATOR_UNLOCK();
  1470. SCTP_INP_INFO_RUNLOCK();
  1471. SCTP_INP_INFO_RLOCK();
  1472. SCTP_ITERATOR_LOCK();
  1473. if (sctp_it_ctl.iterator_flags) {
  1474. /* We won't be staying here */
  1475. SCTP_INP_DECR_REF(it->inp);
  1476. atomic_subtract_int(&it->stcb->asoc.refcnt, 1);
  1477. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  1478. if (sctp_it_ctl.iterator_flags &
  1479. SCTP_ITERATOR_MUST_EXIT) {
  1480. goto done_with_iterator;
  1481. }
  1482. #endif
  1483. if (sctp_it_ctl.iterator_flags &
  1484. SCTP_ITERATOR_STOP_CUR_IT) {
  1485. sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
  1486. goto done_with_iterator;
  1487. }
  1488. if (sctp_it_ctl.iterator_flags &
  1489. SCTP_ITERATOR_STOP_CUR_INP) {
  1490. sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
  1491. goto no_stcb;
  1492. }
  1493. /* If we reach here huh? */
  1494. SCTP_PRINTF("Unknown it ctl flag %x\n",
  1495. sctp_it_ctl.iterator_flags);
  1496. sctp_it_ctl.iterator_flags = 0;
  1497. }
  1498. SCTP_INP_RLOCK(it->inp);
  1499. SCTP_INP_DECR_REF(it->inp);
  1500. SCTP_TCB_LOCK(it->stcb);
  1501. atomic_subtract_int(&it->stcb->asoc.refcnt, 1);
  1502. iteration_count = 0;
  1503. }
  1504. KASSERT(it->inp == it->stcb->sctp_ep,
  1505. ("%s: stcb %p does not belong to inp %p, but inp %p",
  1506. __func__, it->stcb, it->inp, it->stcb->sctp_ep));
  1507. SCTP_INP_RLOCK_ASSERT(it->inp);
  1508. SCTP_TCB_LOCK_ASSERT(it->stcb);
  1509. /* run function on this one */
  1510. (*it->function_assoc)(it->inp, it->stcb, it->pointer, it->val);
  1511. SCTP_INP_RLOCK_ASSERT(it->inp);
  1512. SCTP_TCB_LOCK_ASSERT(it->stcb);
  1513. /*
  1514. * we lie here, it really needs to have its own type but
  1515. * first I must verify that this won't effect things :-0
  1516. */
  1517. if (it->no_chunk_output == 0) {
  1518. sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
  1519. SCTP_INP_RLOCK_ASSERT(it->inp);
  1520. SCTP_TCB_LOCK_ASSERT(it->stcb);
  1521. }
  1522. SCTP_TCB_UNLOCK(it->stcb);
  1523. next_assoc:
  1524. it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
  1525. if (it->stcb == NULL) {
  1526. /* Run last function */
  1527. if (it->function_inp_end != NULL) {
  1528. inp_skip = (*it->function_inp_end)(it->inp,
  1529. it->pointer,
  1530. it->val);
  1531. }
  1532. }
  1533. }
  1534. SCTP_INP_RUNLOCK(it->inp);
  1535. no_stcb:
  1536. /* done with all assocs on this endpoint, move on to next endpoint */
  1537. it->done_current_ep = 0;
  1538. if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
  1539. it->inp = NULL;
  1540. } else {
  1541. it->inp = LIST_NEXT(it->inp, sctp_list);
  1542. }
  1543. it->stcb = NULL;
  1544. if (it->inp == NULL) {
  1545. goto done_with_iterator;
  1546. }
  1547. goto select_a_new_ep;
  1548. }
  1549. void
  1550. sctp_iterator_worker(void)
  1551. {
  1552. struct sctp_iterator *it;
  1553. /* This function is called with the WQ lock in place */
  1554. sctp_it_ctl.iterator_running = 1;
  1555. while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) {
  1556. /* now lets work on this one */
  1557. TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
  1558. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  1559. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1560. CURVNET_SET(it->vn);
  1561. #endif
  1562. sctp_iterator_work(it);
  1563. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1564. CURVNET_RESTORE();
  1565. #endif
  1566. SCTP_IPI_ITERATOR_WQ_LOCK();
  1567. #if !defined(__FreeBSD__) && !defined(__Userspace__)
  1568. if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
  1569. break;
  1570. }
  1571. #endif
  1572. /*sa_ignore FREED_MEMORY*/
  1573. }
  1574. sctp_it_ctl.iterator_running = 0;
  1575. return;
  1576. }
  1577. static void
  1578. sctp_handle_addr_wq(void)
  1579. {
  1580. /* deal with the ADDR wq from the rtsock calls */
  1581. struct sctp_laddr *wi, *nwi;
  1582. struct sctp_asconf_iterator *asc;
  1583. SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
  1584. sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
  1585. if (asc == NULL) {
  1586. /* Try later, no memory */
  1587. sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
  1588. (struct sctp_inpcb *)NULL,
  1589. (struct sctp_tcb *)NULL,
  1590. (struct sctp_nets *)NULL);
  1591. return;
  1592. }
  1593. LIST_INIT(&asc->list_of_work);
  1594. asc->cnt = 0;
  1595. LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
  1596. LIST_REMOVE(wi, sctp_nxt_addr);
  1597. LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
  1598. asc->cnt++;
  1599. }
  1600. if (asc->cnt == 0) {
  1601. SCTP_FREE(asc, SCTP_M_ASC_IT);
  1602. } else {
  1603. int ret;
  1604. ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
  1605. sctp_asconf_iterator_stcb,
  1606. NULL, /* No ep end for boundall */
  1607. SCTP_PCB_FLAGS_BOUNDALL,
  1608. SCTP_PCB_ANY_FEATURES,
  1609. SCTP_ASOC_ANY_STATE,
  1610. (void *)asc, 0,
  1611. sctp_asconf_iterator_end, NULL, 0);
  1612. if (ret) {
  1613. SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
  1614. /* Freeing if we are stopping or put back on the addr_wq. */
  1615. if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
  1616. sctp_asconf_iterator_end(asc, 0);
  1617. } else {
  1618. LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
  1619. LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
  1620. }
  1621. SCTP_FREE(asc, SCTP_M_ASC_IT);
  1622. }
  1623. }
  1624. }
  1625. }
  1626. /*-
  1627. * The following table shows which pointers for the inp, stcb, or net are
  1628. * stored for each timer after it was started.
  1629. *
  1630. *|Name |Timer |inp |stcb|net |
  1631. *|-----------------------------|-----------------------------|----|----|----|
  1632. *|SCTP_TIMER_TYPE_SEND |net->rxt_timer |Yes |Yes |Yes |
  1633. *|SCTP_TIMER_TYPE_INIT |net->rxt_timer |Yes |Yes |Yes |
  1634. *|SCTP_TIMER_TYPE_RECV |stcb->asoc.dack_timer |Yes |Yes |No |
  1635. *|SCTP_TIMER_TYPE_SHUTDOWN |net->rxt_timer |Yes |Yes |Yes |
  1636. *|SCTP_TIMER_TYPE_HEARTBEAT |net->hb_timer |Yes |Yes |Yes |
  1637. *|SCTP_TIMER_TYPE_COOKIE |net->rxt_timer |Yes |Yes |Yes |
  1638. *|SCTP_TIMER_TYPE_NEWCOOKIE |inp->sctp_ep.signature_change|Yes |No |No |
  1639. *|SCTP_TIMER_TYPE_PATHMTURAISE |net->pmtu_timer |Yes |Yes |Yes |
  1640. *|SCTP_TIMER_TYPE_SHUTDOWNACK |net->rxt_timer |Yes |Yes |Yes |
  1641. *|SCTP_TIMER_TYPE_ASCONF |stcb->asoc.asconf_timer |Yes |Yes |Yes |
  1642. *|SCTP_TIMER_TYPE_SHUTDOWNGUARD|stcb->asoc.shut_guard_timer |Yes |Yes |No |
  1643. *|SCTP_TIMER_TYPE_AUTOCLOSE |stcb->asoc.autoclose_timer |Yes |Yes |No |
  1644. *|SCTP_TIMER_TYPE_STRRESET |stcb->asoc.strreset_timer |Yes |Yes |No |
  1645. *|SCTP_TIMER_TYPE_INPKILL |inp->sctp_ep.signature_change|Yes |No |No |
  1646. *|SCTP_TIMER_TYPE_ASOCKILL |stcb->asoc.strreset_timer |Yes |Yes |No |
  1647. *|SCTP_TIMER_TYPE_ADDR_WQ |SCTP_BASE_INFO(addr_wq_timer)|No |No |No |
  1648. *|SCTP_TIMER_TYPE_PRIM_DELETED |stcb->asoc.delete_prim_timer |Yes |Yes |No |
  1649. */
  1650. void
  1651. sctp_timeout_handler(void *t)
  1652. {
  1653. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1654. struct epoch_tracker et;
  1655. #endif
  1656. struct timeval tv;
  1657. struct sctp_inpcb *inp;
  1658. struct sctp_tcb *stcb;
  1659. struct sctp_nets *net;
  1660. struct sctp_timer *tmr;
  1661. struct mbuf *op_err;
  1662. #if defined(__APPLE__) && !defined(__Userspace__)
  1663. struct socket *so;
  1664. #endif
  1665. #if defined(__Userspace__)
  1666. struct socket *upcall_socket = NULL;
  1667. #endif
  1668. int type;
  1669. int i, secret;
  1670. bool did_output, released_asoc_reference;
  1671. /*
  1672. * If inp, stcb or net are not NULL, then references to these were
  1673. * added when the timer was started, and must be released before this
  1674. * function returns.
  1675. */
  1676. tmr = (struct sctp_timer *)t;
  1677. inp = (struct sctp_inpcb *)tmr->ep;
  1678. stcb = (struct sctp_tcb *)tmr->tcb;
  1679. net = (struct sctp_nets *)tmr->net;
  1680. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1681. CURVNET_SET((struct vnet *)tmr->vnet);
  1682. NET_EPOCH_ENTER(et);
  1683. #endif
  1684. released_asoc_reference = false;
  1685. #ifdef SCTP_AUDITING_ENABLED
  1686. sctp_audit_log(0xF0, (uint8_t) tmr->type);
  1687. sctp_auditing(3, inp, stcb, net);
  1688. #endif
  1689. /* sanity checks... */
  1690. KASSERT(tmr->self == NULL || tmr->self == tmr,
  1691. ("sctp_timeout_handler: tmr->self corrupted"));
  1692. KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
  1693. ("sctp_timeout_handler: invalid timer type %d", tmr->type));
  1694. type = tmr->type;
  1695. KASSERT(stcb == NULL || stcb->sctp_ep == inp,
  1696. ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
  1697. type, stcb, stcb->sctp_ep));
  1698. tmr->stopped_from = 0xa001;
  1699. if ((stcb != NULL) && (stcb->asoc.state == SCTP_STATE_EMPTY)) {
  1700. SCTPDBG(SCTP_DEBUG_TIMER2,
  1701. "Timer type %d handler exiting due to CLOSED association.\n",
  1702. type);
  1703. goto out_decr;
  1704. }
  1705. tmr->stopped_from = 0xa002;
  1706. SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d goes off.\n", type);
  1707. if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
  1708. SCTPDBG(SCTP_DEBUG_TIMER2,
  1709. "Timer type %d handler exiting due to not being active.\n",
  1710. type);
  1711. goto out_decr;
  1712. }
  1713. tmr->stopped_from = 0xa003;
  1714. if (stcb) {
  1715. SCTP_TCB_LOCK(stcb);
  1716. /*
  1717. * Release reference so that association can be freed if
  1718. * necessary below.
  1719. * This is safe now that we have acquired the lock.
  1720. */
  1721. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  1722. released_asoc_reference = true;
  1723. if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
  1724. ((stcb->asoc.state == SCTP_STATE_EMPTY) ||
  1725. (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
  1726. SCTPDBG(SCTP_DEBUG_TIMER2,
  1727. "Timer type %d handler exiting due to CLOSED association.\n",
  1728. type);
  1729. goto out;
  1730. }
  1731. } else if (inp != NULL) {
  1732. SCTP_INP_WLOCK(inp);
  1733. } else {
  1734. SCTP_WQ_ADDR_LOCK();
  1735. }
  1736. /* Record in stopped_from which timeout occurred. */
  1737. tmr->stopped_from = type;
  1738. /* mark as being serviced now */
  1739. if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
  1740. /*
  1741. * Callout has been rescheduled.
  1742. */
  1743. goto out;
  1744. }
  1745. if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
  1746. /*
  1747. * Not active, so no action.
  1748. */
  1749. goto out;
  1750. }
  1751. SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
  1752. #if defined(__Userspace__)
  1753. if ((stcb != NULL) &&
  1754. ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  1755. (stcb->sctp_socket != NULL)) {
  1756. upcall_socket = stcb->sctp_socket;
  1757. SOCK_LOCK(upcall_socket);
  1758. soref(upcall_socket);
  1759. SOCK_UNLOCK(upcall_socket);
  1760. }
  1761. #endif
  1762. /* call the handler for the appropriate timer type */
  1763. switch (type) {
  1764. case SCTP_TIMER_TYPE_SEND:
  1765. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1766. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1767. type, inp, stcb, net));
  1768. SCTP_STAT_INCR(sctps_timodata);
  1769. stcb->asoc.timodata++;
  1770. stcb->asoc.num_send_timers_up--;
  1771. if (stcb->asoc.num_send_timers_up < 0) {
  1772. stcb->asoc.num_send_timers_up = 0;
  1773. }
  1774. SCTP_TCB_LOCK_ASSERT(stcb);
  1775. if (sctp_t3rxt_timer(inp, stcb, net)) {
  1776. /* no need to unlock on tcb its gone */
  1777. goto out_decr;
  1778. }
  1779. SCTP_TCB_LOCK_ASSERT(stcb);
  1780. #ifdef SCTP_AUDITING_ENABLED
  1781. sctp_auditing(4, inp, stcb, net);
  1782. #endif
  1783. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
  1784. did_output = true;
  1785. if ((stcb->asoc.num_send_timers_up == 0) &&
  1786. (stcb->asoc.sent_queue_cnt > 0)) {
  1787. struct sctp_tmit_chunk *chk;
  1788. /*
  1789. * Safeguard. If there on some on the sent queue
  1790. * somewhere but no timers running something is
  1791. * wrong... so we start a timer on the first chunk
  1792. * on the send queue on whatever net it is sent to.
  1793. */
  1794. TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
  1795. if (chk->whoTo != NULL) {
  1796. break;
  1797. }
  1798. }
  1799. if (chk != NULL) {
  1800. sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
  1801. }
  1802. }
  1803. break;
  1804. case SCTP_TIMER_TYPE_INIT:
  1805. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1806. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1807. type, inp, stcb, net));
  1808. SCTP_STAT_INCR(sctps_timoinit);
  1809. stcb->asoc.timoinit++;
  1810. if (sctp_t1init_timer(inp, stcb, net)) {
  1811. /* no need to unlock on tcb its gone */
  1812. goto out_decr;
  1813. }
  1814. did_output = false;
  1815. break;
  1816. case SCTP_TIMER_TYPE_RECV:
  1817. KASSERT(inp != NULL && stcb != NULL && net == NULL,
  1818. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1819. type, inp, stcb, net));
  1820. SCTP_STAT_INCR(sctps_timosack);
  1821. stcb->asoc.timosack++;
  1822. sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
  1823. #ifdef SCTP_AUDITING_ENABLED
  1824. sctp_auditing(4, inp, stcb, NULL);
  1825. #endif
  1826. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
  1827. did_output = true;
  1828. break;
  1829. case SCTP_TIMER_TYPE_SHUTDOWN:
  1830. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1831. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1832. type, inp, stcb, net));
  1833. SCTP_STAT_INCR(sctps_timoshutdown);
  1834. stcb->asoc.timoshutdown++;
  1835. if (sctp_shutdown_timer(inp, stcb, net)) {
  1836. /* no need to unlock on tcb its gone */
  1837. goto out_decr;
  1838. }
  1839. #ifdef SCTP_AUDITING_ENABLED
  1840. sctp_auditing(4, inp, stcb, net);
  1841. #endif
  1842. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
  1843. did_output = true;
  1844. break;
  1845. case SCTP_TIMER_TYPE_HEARTBEAT:
  1846. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1847. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1848. type, inp, stcb, net));
  1849. SCTP_STAT_INCR(sctps_timoheartbeat);
  1850. stcb->asoc.timoheartbeat++;
  1851. if (sctp_heartbeat_timer(inp, stcb, net)) {
  1852. /* no need to unlock on tcb its gone */
  1853. goto out_decr;
  1854. }
  1855. #ifdef SCTP_AUDITING_ENABLED
  1856. sctp_auditing(4, inp, stcb, net);
  1857. #endif
  1858. if ((net->dest_state & SCTP_ADDR_NOHB) == 0) {
  1859. sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
  1860. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
  1861. did_output = true;
  1862. } else {
  1863. did_output = false;
  1864. }
  1865. break;
  1866. case SCTP_TIMER_TYPE_COOKIE:
  1867. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1868. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1869. type, inp, stcb, net));
  1870. SCTP_STAT_INCR(sctps_timocookie);
  1871. stcb->asoc.timocookie++;
  1872. if (sctp_cookie_timer(inp, stcb, net)) {
  1873. /* no need to unlock on tcb its gone */
  1874. goto out_decr;
  1875. }
  1876. #ifdef SCTP_AUDITING_ENABLED
  1877. sctp_auditing(4, inp, stcb, net);
  1878. #endif
  1879. /*
  1880. * We consider T3 and Cookie timer pretty much the same with
  1881. * respect to where from in chunk_output.
  1882. */
  1883. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
  1884. did_output = true;
  1885. break;
  1886. case SCTP_TIMER_TYPE_NEWCOOKIE:
  1887. KASSERT(inp != NULL && stcb == NULL && net == NULL,
  1888. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1889. type, inp, stcb, net));
  1890. SCTP_STAT_INCR(sctps_timosecret);
  1891. (void)SCTP_GETTIME_TIMEVAL(&tv);
  1892. inp->sctp_ep.time_of_secret_change = (unsigned int)tv.tv_sec;
  1893. inp->sctp_ep.last_secret_number =
  1894. inp->sctp_ep.current_secret_number;
  1895. inp->sctp_ep.current_secret_number++;
  1896. if (inp->sctp_ep.current_secret_number >=
  1897. SCTP_HOW_MANY_SECRETS) {
  1898. inp->sctp_ep.current_secret_number = 0;
  1899. }
  1900. secret = (int)inp->sctp_ep.current_secret_number;
  1901. for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
  1902. inp->sctp_ep.secret_key[secret][i] =
  1903. sctp_select_initial_TSN(&inp->sctp_ep);
  1904. }
  1905. sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
  1906. did_output = false;
  1907. break;
  1908. case SCTP_TIMER_TYPE_PATHMTURAISE:
  1909. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1910. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1911. type, inp, stcb, net));
  1912. SCTP_STAT_INCR(sctps_timopathmtu);
  1913. sctp_pathmtu_timer(inp, stcb, net);
  1914. did_output = false;
  1915. break;
  1916. case SCTP_TIMER_TYPE_SHUTDOWNACK:
  1917. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1918. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1919. type, inp, stcb, net));
  1920. if (sctp_shutdownack_timer(inp, stcb, net)) {
  1921. /* no need to unlock on tcb its gone */
  1922. goto out_decr;
  1923. }
  1924. SCTP_STAT_INCR(sctps_timoshutdownack);
  1925. stcb->asoc.timoshutdownack++;
  1926. #ifdef SCTP_AUDITING_ENABLED
  1927. sctp_auditing(4, inp, stcb, net);
  1928. #endif
  1929. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
  1930. did_output = true;
  1931. break;
  1932. case SCTP_TIMER_TYPE_ASCONF:
  1933. KASSERT(inp != NULL && stcb != NULL && net != NULL,
  1934. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1935. type, inp, stcb, net));
  1936. SCTP_STAT_INCR(sctps_timoasconf);
  1937. if (sctp_asconf_timer(inp, stcb, net)) {
  1938. /* no need to unlock on tcb its gone */
  1939. goto out_decr;
  1940. }
  1941. #ifdef SCTP_AUDITING_ENABLED
  1942. sctp_auditing(4, inp, stcb, net);
  1943. #endif
  1944. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
  1945. did_output = true;
  1946. break;
  1947. case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
  1948. KASSERT(inp != NULL && stcb != NULL && net == NULL,
  1949. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1950. type, inp, stcb, net));
  1951. SCTP_STAT_INCR(sctps_timoshutdownguard);
  1952. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  1953. "Shutdown guard timer expired");
  1954. sctp_abort_an_association(inp, stcb, op_err, true, SCTP_SO_NOT_LOCKED);
  1955. /* no need to unlock on tcb its gone */
  1956. goto out_decr;
  1957. case SCTP_TIMER_TYPE_AUTOCLOSE:
  1958. KASSERT(inp != NULL && stcb != NULL && net == NULL,
  1959. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1960. type, inp, stcb, net));
  1961. SCTP_STAT_INCR(sctps_timoautoclose);
  1962. sctp_autoclose_timer(inp, stcb);
  1963. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
  1964. did_output = true;
  1965. break;
  1966. case SCTP_TIMER_TYPE_STRRESET:
  1967. KASSERT(inp != NULL && stcb != NULL && net == NULL,
  1968. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1969. type, inp, stcb, net));
  1970. SCTP_STAT_INCR(sctps_timostrmrst);
  1971. if (sctp_strreset_timer(inp, stcb)) {
  1972. /* no need to unlock on tcb its gone */
  1973. goto out_decr;
  1974. }
  1975. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
  1976. did_output = true;
  1977. break;
  1978. case SCTP_TIMER_TYPE_INPKILL:
  1979. KASSERT(inp != NULL && stcb == NULL && net == NULL,
  1980. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  1981. type, inp, stcb, net));
  1982. SCTP_STAT_INCR(sctps_timoinpkill);
  1983. /*
  1984. * special case, take away our increment since WE are the
  1985. * killer
  1986. */
  1987. sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
  1988. SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
  1989. #if defined(__APPLE__) && !defined(__Userspace__)
  1990. SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
  1991. #endif
  1992. SCTP_INP_DECR_REF(inp);
  1993. SCTP_INP_WUNLOCK(inp);
  1994. sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  1995. SCTP_CALLED_FROM_INPKILL_TIMER);
  1996. #if defined(__APPLE__) && !defined(__Userspace__)
  1997. SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
  1998. #endif
  1999. inp = NULL;
  2000. goto out_decr;
  2001. case SCTP_TIMER_TYPE_ASOCKILL:
  2002. KASSERT(inp != NULL && stcb != NULL && net == NULL,
  2003. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  2004. type, inp, stcb, net));
  2005. SCTP_STAT_INCR(sctps_timoassockill);
  2006. /* Can we free it yet? */
  2007. sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
  2008. SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
  2009. #if defined(__APPLE__) && !defined(__Userspace__)
  2010. so = SCTP_INP_SO(inp);
  2011. atomic_add_int(&stcb->asoc.refcnt, 1);
  2012. SCTP_TCB_UNLOCK(stcb);
  2013. SCTP_SOCKET_LOCK(so, 1);
  2014. SCTP_TCB_LOCK(stcb);
  2015. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  2016. #endif
  2017. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  2018. SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
  2019. #if defined(__APPLE__) && !defined(__Userspace__)
  2020. SCTP_SOCKET_UNLOCK(so, 1);
  2021. #endif
  2022. /*
  2023. * free asoc, always unlocks (or destroy's) so prevent
  2024. * duplicate unlock or unlock of a free mtx :-0
  2025. */
  2026. stcb = NULL;
  2027. goto out_decr;
  2028. case SCTP_TIMER_TYPE_ADDR_WQ:
  2029. KASSERT(inp == NULL && stcb == NULL && net == NULL,
  2030. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  2031. type, inp, stcb, net));
  2032. sctp_handle_addr_wq();
  2033. did_output = true;
  2034. break;
  2035. case SCTP_TIMER_TYPE_PRIM_DELETED:
  2036. KASSERT(inp != NULL && stcb != NULL && net == NULL,
  2037. ("timeout of type %d: inp = %p, stcb = %p, net = %p",
  2038. type, inp, stcb, net));
  2039. SCTP_STAT_INCR(sctps_timodelprim);
  2040. sctp_delete_prim_timer(inp, stcb);
  2041. did_output = false;
  2042. break;
  2043. default:
  2044. #ifdef INVARIANTS
  2045. panic("Unknown timer type %d", type);
  2046. #else
  2047. goto out;
  2048. #endif
  2049. }
  2050. #ifdef SCTP_AUDITING_ENABLED
  2051. sctp_audit_log(0xF1, (uint8_t) type);
  2052. if (inp != NULL)
  2053. sctp_auditing(5, inp, stcb, net);
  2054. #endif
  2055. if (did_output && (stcb != NULL)) {
  2056. /*
  2057. * Now we need to clean up the control chunk chain if an
  2058. * ECNE is on it. It must be marked as UNSENT again so next
  2059. * call will continue to send it until such time that we get
  2060. * a CWR, to remove it. It is, however, less likely that we
  2061. * will find a ecn echo on the chain though.
  2062. */
  2063. sctp_fix_ecn_echo(&stcb->asoc);
  2064. }
  2065. out:
  2066. if (stcb != NULL) {
  2067. SCTP_TCB_UNLOCK(stcb);
  2068. } else if (inp != NULL) {
  2069. SCTP_INP_WUNLOCK(inp);
  2070. } else {
  2071. SCTP_WQ_ADDR_UNLOCK();
  2072. }
  2073. out_decr:
  2074. #if defined(__Userspace__)
  2075. if (upcall_socket != NULL) {
  2076. if ((upcall_socket->so_upcall != NULL) &&
  2077. (upcall_socket->so_error != 0)) {
  2078. (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
  2079. }
  2080. ACCEPT_LOCK();
  2081. SOCK_LOCK(upcall_socket);
  2082. sorele(upcall_socket);
  2083. }
  2084. #endif
  2085. /* These reference counts were incremented in sctp_timer_start(). */
  2086. if (inp != NULL) {
  2087. SCTP_INP_DECR_REF(inp);
  2088. }
  2089. if ((stcb != NULL) && !released_asoc_reference) {
  2090. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  2091. }
  2092. if (net != NULL) {
  2093. sctp_free_remote_addr(net);
  2094. }
  2095. SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d handler finished.\n", type);
  2096. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2097. CURVNET_RESTORE();
  2098. NET_EPOCH_EXIT(et);
  2099. #endif
  2100. }
  2101. /*-
  2102. * The following table shows which parameters must be provided
  2103. * when calling sctp_timer_start(). For parameters not being
  2104. * provided, NULL must be used.
  2105. *
  2106. * |Name |inp |stcb|net |
  2107. * |-----------------------------|----|----|----|
  2108. * |SCTP_TIMER_TYPE_SEND |Yes |Yes |Yes |
  2109. * |SCTP_TIMER_TYPE_INIT |Yes |Yes |Yes |
  2110. * |SCTP_TIMER_TYPE_RECV |Yes |Yes |No |
  2111. * |SCTP_TIMER_TYPE_SHUTDOWN |Yes |Yes |Yes |
  2112. * |SCTP_TIMER_TYPE_HEARTBEAT |Yes |Yes |Yes |
  2113. * |SCTP_TIMER_TYPE_COOKIE |Yes |Yes |Yes |
  2114. * |SCTP_TIMER_TYPE_NEWCOOKIE |Yes |No |No |
  2115. * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
  2116. * |SCTP_TIMER_TYPE_SHUTDOWNACK |Yes |Yes |Yes |
  2117. * |SCTP_TIMER_TYPE_ASCONF |Yes |Yes |Yes |
  2118. * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No |
  2119. * |SCTP_TIMER_TYPE_AUTOCLOSE |Yes |Yes |No |
  2120. * |SCTP_TIMER_TYPE_STRRESET |Yes |Yes |Yes |
  2121. * |SCTP_TIMER_TYPE_INPKILL |Yes |No |No |
  2122. * |SCTP_TIMER_TYPE_ASOCKILL |Yes |Yes |No |
  2123. * |SCTP_TIMER_TYPE_ADDR_WQ |No |No |No |
  2124. * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No |
  2125. *
  2126. */
  2127. void
  2128. sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  2129. struct sctp_nets *net)
  2130. {
  2131. struct sctp_timer *tmr;
  2132. uint32_t to_ticks;
  2133. uint32_t rndval, jitter;
  2134. KASSERT(stcb == NULL || stcb->sctp_ep == inp,
  2135. ("sctp_timer_start of type %d: inp = %p, stcb->sctp_ep %p",
  2136. t_type, stcb, stcb->sctp_ep));
  2137. tmr = NULL;
  2138. if (stcb != NULL) {
  2139. SCTP_TCB_LOCK_ASSERT(stcb);
  2140. } else if (inp != NULL) {
  2141. SCTP_INP_WLOCK_ASSERT(inp);
  2142. } else {
  2143. SCTP_WQ_ADDR_LOCK_ASSERT();
  2144. }
  2145. if (stcb != NULL) {
  2146. /* Don't restart timer on association that's about to be killed. */
  2147. if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
  2148. (t_type != SCTP_TIMER_TYPE_ASOCKILL)) {
  2149. SCTPDBG(SCTP_DEBUG_TIMER2,
  2150. "Timer type %d not started: inp=%p, stcb=%p, net=%p (stcb deleted).\n",
  2151. t_type, inp, stcb, net);
  2152. return;
  2153. }
  2154. /* Don't restart timer on net that's been removed. */
  2155. if (net != NULL && (net->dest_state & SCTP_ADDR_BEING_DELETED)) {
  2156. SCTPDBG(SCTP_DEBUG_TIMER2,
  2157. "Timer type %d not started: inp=%p, stcb=%p, net=%p (net deleted).\n",
  2158. t_type, inp, stcb, net);
  2159. return;
  2160. }
  2161. }
  2162. switch (t_type) {
  2163. case SCTP_TIMER_TYPE_SEND:
  2164. /* Here we use the RTO timer. */
  2165. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2166. #ifdef INVARIANTS
  2167. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2168. t_type, inp, stcb, net);
  2169. #else
  2170. return;
  2171. #endif
  2172. }
  2173. tmr = &net->rxt_timer;
  2174. if (net->RTO == 0) {
  2175. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2176. } else {
  2177. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2178. }
  2179. break;
  2180. case SCTP_TIMER_TYPE_INIT:
  2181. /*
  2182. * Here we use the INIT timer default usually about 1
  2183. * second.
  2184. */
  2185. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2186. #ifdef INVARIANTS
  2187. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2188. t_type, inp, stcb, net);
  2189. #else
  2190. return;
  2191. #endif
  2192. }
  2193. tmr = &net->rxt_timer;
  2194. if (net->RTO == 0) {
  2195. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2196. } else {
  2197. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2198. }
  2199. break;
  2200. case SCTP_TIMER_TYPE_RECV:
  2201. /*
  2202. * Here we use the Delayed-Ack timer value from the inp,
  2203. * usually about 200ms.
  2204. */
  2205. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2206. #ifdef INVARIANTS
  2207. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2208. t_type, inp, stcb, net);
  2209. #else
  2210. return;
  2211. #endif
  2212. }
  2213. tmr = &stcb->asoc.dack_timer;
  2214. to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
  2215. break;
  2216. case SCTP_TIMER_TYPE_SHUTDOWN:
  2217. /* Here we use the RTO of the destination. */
  2218. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2219. #ifdef INVARIANTS
  2220. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2221. t_type, inp, stcb, net);
  2222. #else
  2223. return;
  2224. #endif
  2225. }
  2226. tmr = &net->rxt_timer;
  2227. if (net->RTO == 0) {
  2228. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2229. } else {
  2230. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2231. }
  2232. break;
  2233. case SCTP_TIMER_TYPE_HEARTBEAT:
  2234. /*
  2235. * The net is used here so that we can add in the RTO. Even
  2236. * though we use a different timer. We also add the HB timer
  2237. * PLUS a random jitter.
  2238. */
  2239. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2240. #ifdef INVARIANTS
  2241. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2242. t_type, inp, stcb, net);
  2243. #else
  2244. return;
  2245. #endif
  2246. }
  2247. if ((net->dest_state & SCTP_ADDR_NOHB) &&
  2248. ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
  2249. SCTPDBG(SCTP_DEBUG_TIMER2,
  2250. "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
  2251. t_type, inp, stcb, net);
  2252. return;
  2253. }
  2254. tmr = &net->hb_timer;
  2255. if (net->RTO == 0) {
  2256. to_ticks = stcb->asoc.initial_rto;
  2257. } else {
  2258. to_ticks = net->RTO;
  2259. }
  2260. rndval = sctp_select_initial_TSN(&inp->sctp_ep);
  2261. jitter = rndval % to_ticks;
  2262. if (to_ticks > 1) {
  2263. to_ticks >>= 1;
  2264. }
  2265. if (jitter < (UINT32_MAX - to_ticks)) {
  2266. to_ticks += jitter;
  2267. } else {
  2268. to_ticks = UINT32_MAX;
  2269. }
  2270. if (!((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
  2271. (net->dest_state & SCTP_ADDR_REACHABLE)) &&
  2272. ((net->dest_state & SCTP_ADDR_PF) == 0)) {
  2273. if (net->heart_beat_delay < (UINT32_MAX - to_ticks)) {
  2274. to_ticks += net->heart_beat_delay;
  2275. } else {
  2276. to_ticks = UINT32_MAX;
  2277. }
  2278. }
  2279. /*
  2280. * Now we must convert the to_ticks that are now in
  2281. * ms to ticks.
  2282. */
  2283. to_ticks = sctp_msecs_to_ticks(to_ticks);
  2284. break;
  2285. case SCTP_TIMER_TYPE_COOKIE:
  2286. /*
  2287. * Here we can use the RTO timer from the network since one
  2288. * RTT was complete. If a retransmission happened then we will
  2289. * be using the RTO initial value.
  2290. */
  2291. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2292. #ifdef INVARIANTS
  2293. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2294. t_type, inp, stcb, net);
  2295. #else
  2296. return;
  2297. #endif
  2298. }
  2299. tmr = &net->rxt_timer;
  2300. if (net->RTO == 0) {
  2301. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2302. } else {
  2303. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2304. }
  2305. break;
  2306. case SCTP_TIMER_TYPE_NEWCOOKIE:
  2307. /*
  2308. * Nothing needed but the endpoint here usually about 60
  2309. * minutes.
  2310. */
  2311. if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
  2312. #ifdef INVARIANTS
  2313. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2314. t_type, inp, stcb, net);
  2315. #else
  2316. return;
  2317. #endif
  2318. }
  2319. tmr = &inp->sctp_ep.signature_change;
  2320. to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
  2321. break;
  2322. case SCTP_TIMER_TYPE_PATHMTURAISE:
  2323. /*
  2324. * Here we use the value found in the EP for PMTUD, usually
  2325. * about 10 minutes.
  2326. */
  2327. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2328. #ifdef INVARIANTS
  2329. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2330. t_type, inp, stcb, net);
  2331. #else
  2332. return;
  2333. #endif
  2334. }
  2335. if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
  2336. SCTPDBG(SCTP_DEBUG_TIMER2,
  2337. "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
  2338. t_type, inp, stcb, net);
  2339. return;
  2340. }
  2341. tmr = &net->pmtu_timer;
  2342. to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
  2343. break;
  2344. case SCTP_TIMER_TYPE_SHUTDOWNACK:
  2345. /* Here we use the RTO of the destination. */
  2346. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2347. #ifdef INVARIANTS
  2348. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2349. t_type, inp, stcb, net);
  2350. #else
  2351. return;
  2352. #endif
  2353. }
  2354. tmr = &net->rxt_timer;
  2355. if (net->RTO == 0) {
  2356. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2357. } else {
  2358. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2359. }
  2360. break;
  2361. case SCTP_TIMER_TYPE_ASCONF:
  2362. /*
  2363. * Here the timer comes from the stcb but its value is from
  2364. * the net's RTO.
  2365. */
  2366. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2367. #ifdef INVARIANTS
  2368. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2369. t_type, inp, stcb, net);
  2370. #else
  2371. return;
  2372. #endif
  2373. }
  2374. tmr = &stcb->asoc.asconf_timer;
  2375. if (net->RTO == 0) {
  2376. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2377. } else {
  2378. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2379. }
  2380. break;
  2381. case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
  2382. /*
  2383. * Here we use the endpoints shutdown guard timer usually
  2384. * about 3 minutes.
  2385. */
  2386. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2387. #ifdef INVARIANTS
  2388. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2389. t_type, inp, stcb, net);
  2390. #else
  2391. return;
  2392. #endif
  2393. }
  2394. tmr = &stcb->asoc.shut_guard_timer;
  2395. if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
  2396. if (stcb->asoc.maxrto < UINT32_MAX / 5) {
  2397. to_ticks = sctp_msecs_to_ticks(5 * stcb->asoc.maxrto);
  2398. } else {
  2399. to_ticks = sctp_msecs_to_ticks(UINT32_MAX);
  2400. }
  2401. } else {
  2402. to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
  2403. }
  2404. break;
  2405. case SCTP_TIMER_TYPE_AUTOCLOSE:
  2406. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2407. #ifdef INVARIANTS
  2408. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2409. t_type, inp, stcb, net);
  2410. #else
  2411. return;
  2412. #endif
  2413. }
  2414. tmr = &stcb->asoc.autoclose_timer;
  2415. to_ticks = stcb->asoc.sctp_autoclose_ticks;
  2416. break;
  2417. case SCTP_TIMER_TYPE_STRRESET:
  2418. /*
  2419. * Here the timer comes from the stcb but its value is from
  2420. * the net's RTO.
  2421. */
  2422. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2423. #ifdef INVARIANTS
  2424. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2425. t_type, inp, stcb, net);
  2426. #else
  2427. return;
  2428. #endif
  2429. }
  2430. tmr = &stcb->asoc.strreset_timer;
  2431. if (net->RTO == 0) {
  2432. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2433. } else {
  2434. to_ticks = sctp_msecs_to_ticks(net->RTO);
  2435. }
  2436. break;
  2437. case SCTP_TIMER_TYPE_INPKILL:
  2438. /*
  2439. * The inp is setup to die. We re-use the signature_change
  2440. * timer since that has stopped and we are in the GONE
  2441. * state.
  2442. */
  2443. if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
  2444. #ifdef INVARIANTS
  2445. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2446. t_type, inp, stcb, net);
  2447. #else
  2448. return;
  2449. #endif
  2450. }
  2451. tmr = &inp->sctp_ep.signature_change;
  2452. to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
  2453. break;
  2454. case SCTP_TIMER_TYPE_ASOCKILL:
  2455. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2456. #ifdef INVARIANTS
  2457. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2458. t_type, inp, stcb, net);
  2459. #else
  2460. return;
  2461. #endif
  2462. }
  2463. tmr = &stcb->asoc.strreset_timer;
  2464. to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
  2465. break;
  2466. case SCTP_TIMER_TYPE_ADDR_WQ:
  2467. if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
  2468. #ifdef INVARIANTS
  2469. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2470. t_type, inp, stcb, net);
  2471. #else
  2472. return;
  2473. #endif
  2474. }
  2475. /* Only 1 tick away :-) */
  2476. tmr = &SCTP_BASE_INFO(addr_wq_timer);
  2477. to_ticks = SCTP_ADDRESS_TICK_DELAY;
  2478. break;
  2479. case SCTP_TIMER_TYPE_PRIM_DELETED:
  2480. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2481. #ifdef INVARIANTS
  2482. panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
  2483. t_type, inp, stcb, net);
  2484. #else
  2485. return;
  2486. #endif
  2487. }
  2488. tmr = &stcb->asoc.delete_prim_timer;
  2489. to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
  2490. break;
  2491. default:
  2492. #ifdef INVARIANTS
  2493. panic("Unknown timer type %d", t_type);
  2494. #else
  2495. return;
  2496. #endif
  2497. }
  2498. KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
  2499. KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
  2500. if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
  2501. /*
  2502. * We do NOT allow you to have it already running. If it is,
  2503. * we leave the current one up unchanged.
  2504. */
  2505. SCTPDBG(SCTP_DEBUG_TIMER2,
  2506. "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
  2507. t_type, inp, stcb, net);
  2508. return;
  2509. }
  2510. /* At this point we can proceed. */
  2511. if (t_type == SCTP_TIMER_TYPE_SEND) {
  2512. stcb->asoc.num_send_timers_up++;
  2513. }
  2514. tmr->stopped_from = 0;
  2515. tmr->type = t_type;
  2516. tmr->ep = (void *)inp;
  2517. tmr->tcb = (void *)stcb;
  2518. if (t_type == SCTP_TIMER_TYPE_STRRESET) {
  2519. tmr->net = NULL;
  2520. } else {
  2521. tmr->net = (void *)net;
  2522. }
  2523. tmr->self = (void *)tmr;
  2524. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2525. tmr->vnet = (void *)curvnet;
  2526. #endif
  2527. tmr->ticks = sctp_get_tick_count();
  2528. if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
  2529. SCTPDBG(SCTP_DEBUG_TIMER2,
  2530. "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
  2531. t_type, to_ticks, inp, stcb, net);
  2532. /*
  2533. * If this is a newly scheduled callout, as opposed to a
  2534. * rescheduled one, increment relevant reference counts.
  2535. */
  2536. if (tmr->ep != NULL) {
  2537. SCTP_INP_INCR_REF(inp);
  2538. }
  2539. if (tmr->tcb != NULL) {
  2540. atomic_add_int(&stcb->asoc.refcnt, 1);
  2541. }
  2542. if (tmr->net != NULL) {
  2543. atomic_add_int(&net->ref_count, 1);
  2544. }
  2545. } else {
  2546. /*
  2547. * This should not happen, since we checked for pending
  2548. * above.
  2549. */
  2550. SCTPDBG(SCTP_DEBUG_TIMER2,
  2551. "Timer type %d restarted: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
  2552. t_type, to_ticks, inp, stcb, net);
  2553. }
  2554. return;
  2555. }
  2556. /*-
  2557. * The following table shows which parameters must be provided
  2558. * when calling sctp_timer_stop(). For parameters not being
  2559. * provided, NULL must be used.
  2560. *
  2561. * |Name |inp |stcb|net |
  2562. * |-----------------------------|----|----|----|
  2563. * |SCTP_TIMER_TYPE_SEND |Yes |Yes |Yes |
  2564. * |SCTP_TIMER_TYPE_INIT |Yes |Yes |Yes |
  2565. * |SCTP_TIMER_TYPE_RECV |Yes |Yes |No |
  2566. * |SCTP_TIMER_TYPE_SHUTDOWN |Yes |Yes |Yes |
  2567. * |SCTP_TIMER_TYPE_HEARTBEAT |Yes |Yes |Yes |
  2568. * |SCTP_TIMER_TYPE_COOKIE |Yes |Yes |Yes |
  2569. * |SCTP_TIMER_TYPE_NEWCOOKIE |Yes |No |No |
  2570. * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
  2571. * |SCTP_TIMER_TYPE_SHUTDOWNACK |Yes |Yes |Yes |
  2572. * |SCTP_TIMER_TYPE_ASCONF |Yes |Yes |No |
  2573. * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No |
  2574. * |SCTP_TIMER_TYPE_AUTOCLOSE |Yes |Yes |No |
  2575. * |SCTP_TIMER_TYPE_STRRESET |Yes |Yes |No |
  2576. * |SCTP_TIMER_TYPE_INPKILL |Yes |No |No |
  2577. * |SCTP_TIMER_TYPE_ASOCKILL |Yes |Yes |No |
  2578. * |SCTP_TIMER_TYPE_ADDR_WQ |No |No |No |
  2579. * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No |
  2580. *
  2581. */
  2582. void
  2583. sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  2584. struct sctp_nets *net, uint32_t from)
  2585. {
  2586. struct sctp_timer *tmr;
  2587. KASSERT(stcb == NULL || stcb->sctp_ep == inp,
  2588. ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
  2589. t_type, stcb, stcb->sctp_ep));
  2590. if (stcb != NULL) {
  2591. SCTP_TCB_LOCK_ASSERT(stcb);
  2592. } else if (inp != NULL) {
  2593. SCTP_INP_WLOCK_ASSERT(inp);
  2594. } else {
  2595. SCTP_WQ_ADDR_LOCK_ASSERT();
  2596. }
  2597. tmr = NULL;
  2598. switch (t_type) {
  2599. case SCTP_TIMER_TYPE_SEND:
  2600. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2601. #ifdef INVARIANTS
  2602. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2603. t_type, inp, stcb, net);
  2604. #else
  2605. return;
  2606. #endif
  2607. }
  2608. tmr = &net->rxt_timer;
  2609. break;
  2610. case SCTP_TIMER_TYPE_INIT:
  2611. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2612. #ifdef INVARIANTS
  2613. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2614. t_type, inp, stcb, net);
  2615. #else
  2616. return;
  2617. #endif
  2618. }
  2619. tmr = &net->rxt_timer;
  2620. break;
  2621. case SCTP_TIMER_TYPE_RECV:
  2622. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2623. #ifdef INVARIANTS
  2624. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2625. t_type, inp, stcb, net);
  2626. #else
  2627. return;
  2628. #endif
  2629. }
  2630. tmr = &stcb->asoc.dack_timer;
  2631. break;
  2632. case SCTP_TIMER_TYPE_SHUTDOWN:
  2633. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2634. #ifdef INVARIANTS
  2635. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2636. t_type, inp, stcb, net);
  2637. #else
  2638. return;
  2639. #endif
  2640. }
  2641. tmr = &net->rxt_timer;
  2642. break;
  2643. case SCTP_TIMER_TYPE_HEARTBEAT:
  2644. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2645. #ifdef INVARIANTS
  2646. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2647. t_type, inp, stcb, net);
  2648. #else
  2649. return;
  2650. #endif
  2651. }
  2652. tmr = &net->hb_timer;
  2653. break;
  2654. case SCTP_TIMER_TYPE_COOKIE:
  2655. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2656. #ifdef INVARIANTS
  2657. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2658. t_type, inp, stcb, net);
  2659. #else
  2660. return;
  2661. #endif
  2662. }
  2663. tmr = &net->rxt_timer;
  2664. break;
  2665. case SCTP_TIMER_TYPE_NEWCOOKIE:
  2666. if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
  2667. #ifdef INVARIANTS
  2668. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2669. t_type, inp, stcb, net);
  2670. #else
  2671. return;
  2672. #endif
  2673. }
  2674. tmr = &inp->sctp_ep.signature_change;
  2675. break;
  2676. case SCTP_TIMER_TYPE_PATHMTURAISE:
  2677. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2678. #ifdef INVARIANTS
  2679. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2680. t_type, inp, stcb, net);
  2681. #else
  2682. return;
  2683. #endif
  2684. }
  2685. tmr = &net->pmtu_timer;
  2686. break;
  2687. case SCTP_TIMER_TYPE_SHUTDOWNACK:
  2688. if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
  2689. #ifdef INVARIANTS
  2690. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2691. t_type, inp, stcb, net);
  2692. #else
  2693. return;
  2694. #endif
  2695. }
  2696. tmr = &net->rxt_timer;
  2697. break;
  2698. case SCTP_TIMER_TYPE_ASCONF:
  2699. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2700. #ifdef INVARIANTS
  2701. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2702. t_type, inp, stcb, net);
  2703. #else
  2704. return;
  2705. #endif
  2706. }
  2707. tmr = &stcb->asoc.asconf_timer;
  2708. break;
  2709. case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
  2710. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2711. #ifdef INVARIANTS
  2712. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2713. t_type, inp, stcb, net);
  2714. #else
  2715. return;
  2716. #endif
  2717. }
  2718. tmr = &stcb->asoc.shut_guard_timer;
  2719. break;
  2720. case SCTP_TIMER_TYPE_AUTOCLOSE:
  2721. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2722. #ifdef INVARIANTS
  2723. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2724. t_type, inp, stcb, net);
  2725. #else
  2726. return;
  2727. #endif
  2728. }
  2729. tmr = &stcb->asoc.autoclose_timer;
  2730. break;
  2731. case SCTP_TIMER_TYPE_STRRESET:
  2732. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2733. #ifdef INVARIANTS
  2734. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2735. t_type, inp, stcb, net);
  2736. #else
  2737. return;
  2738. #endif
  2739. }
  2740. tmr = &stcb->asoc.strreset_timer;
  2741. break;
  2742. case SCTP_TIMER_TYPE_INPKILL:
  2743. /*
  2744. * The inp is setup to die. We re-use the signature_change
  2745. * timer since that has stopped and we are in the GONE
  2746. * state.
  2747. */
  2748. if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
  2749. #ifdef INVARIANTS
  2750. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2751. t_type, inp, stcb, net);
  2752. #else
  2753. return;
  2754. #endif
  2755. }
  2756. tmr = &inp->sctp_ep.signature_change;
  2757. break;
  2758. case SCTP_TIMER_TYPE_ASOCKILL:
  2759. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2760. #ifdef INVARIANTS
  2761. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2762. t_type, inp, stcb, net);
  2763. #else
  2764. return;
  2765. #endif
  2766. }
  2767. tmr = &stcb->asoc.strreset_timer;
  2768. break;
  2769. case SCTP_TIMER_TYPE_ADDR_WQ:
  2770. if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
  2771. #ifdef INVARIANTS
  2772. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2773. t_type, inp, stcb, net);
  2774. #else
  2775. return;
  2776. #endif
  2777. }
  2778. tmr = &SCTP_BASE_INFO(addr_wq_timer);
  2779. break;
  2780. case SCTP_TIMER_TYPE_PRIM_DELETED:
  2781. if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
  2782. #ifdef INVARIANTS
  2783. panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
  2784. t_type, inp, stcb, net);
  2785. #else
  2786. return;
  2787. #endif
  2788. }
  2789. tmr = &stcb->asoc.delete_prim_timer;
  2790. break;
  2791. default:
  2792. #ifdef INVARIANTS
  2793. panic("Unknown timer type %d", t_type);
  2794. #else
  2795. return;
  2796. #endif
  2797. }
  2798. KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
  2799. if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
  2800. (tmr->type != t_type)) {
  2801. /*
  2802. * Ok we have a timer that is under joint use. Cookie timer
  2803. * per chance with the SEND timer. We therefore are NOT
  2804. * running the timer that the caller wants stopped. So just
  2805. * return.
  2806. */
  2807. SCTPDBG(SCTP_DEBUG_TIMER2,
  2808. "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
  2809. t_type, inp, stcb, net);
  2810. return;
  2811. }
  2812. if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
  2813. stcb->asoc.num_send_timers_up--;
  2814. if (stcb->asoc.num_send_timers_up < 0) {
  2815. stcb->asoc.num_send_timers_up = 0;
  2816. }
  2817. }
  2818. tmr->self = NULL;
  2819. tmr->stopped_from = from;
  2820. if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
  2821. KASSERT(tmr->ep == inp,
  2822. ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
  2823. t_type, inp, tmr->ep));
  2824. KASSERT(tmr->tcb == stcb,
  2825. ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
  2826. t_type, stcb, tmr->tcb));
  2827. KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
  2828. ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
  2829. ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
  2830. t_type, net, tmr->net));
  2831. SCTPDBG(SCTP_DEBUG_TIMER2,
  2832. "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
  2833. t_type, inp, stcb, net);
  2834. /*
  2835. * If the timer was actually stopped, decrement reference counts
  2836. * that were incremented in sctp_timer_start().
  2837. */
  2838. if (tmr->ep != NULL) {
  2839. tmr->ep = NULL;
  2840. SCTP_INP_DECR_REF(inp);
  2841. }
  2842. if (tmr->tcb != NULL) {
  2843. tmr->tcb = NULL;
  2844. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  2845. }
  2846. if (tmr->net != NULL) {
  2847. struct sctp_nets *tmr_net;
  2848. /*
  2849. * Can't use net, since it doesn't work for
  2850. * SCTP_TIMER_TYPE_ASCONF.
  2851. */
  2852. tmr_net = tmr->net;
  2853. tmr->net = NULL;
  2854. sctp_free_remote_addr(tmr_net);
  2855. }
  2856. } else {
  2857. SCTPDBG(SCTP_DEBUG_TIMER2,
  2858. "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
  2859. t_type, inp, stcb, net);
  2860. }
  2861. return;
  2862. }
  2863. uint32_t
  2864. sctp_calculate_len(struct mbuf *m)
  2865. {
  2866. struct mbuf *at;
  2867. uint32_t tlen;
  2868. tlen = 0;
  2869. for (at = m; at != NULL; at = SCTP_BUF_NEXT(at)) {
  2870. tlen += SCTP_BUF_LEN(at);
  2871. }
  2872. return (tlen);
  2873. }
  2874. /*
  2875. * Given an association and starting time of the current RTT period, update
  2876. * RTO in number of msecs. net should point to the current network.
  2877. * Return 1, if an RTO update was performed, return 0 if no update was
  2878. * performed due to invalid starting point.
  2879. */
  2880. int
  2881. sctp_calculate_rto(struct sctp_tcb *stcb,
  2882. struct sctp_association *asoc,
  2883. struct sctp_nets *net,
  2884. struct timeval *old,
  2885. int rtt_from_sack)
  2886. {
  2887. struct timeval now;
  2888. uint64_t rtt_us; /* RTT in us */
  2889. int32_t rtt; /* RTT in ms */
  2890. uint32_t new_rto;
  2891. int first_measure = 0;
  2892. /************************/
  2893. /* 1. calculate new RTT */
  2894. /************************/
  2895. /* get the current time */
  2896. if (stcb->asoc.use_precise_time) {
  2897. (void)SCTP_GETPTIME_TIMEVAL(&now);
  2898. } else {
  2899. (void)SCTP_GETTIME_TIMEVAL(&now);
  2900. }
  2901. if ((old->tv_sec > now.tv_sec) ||
  2902. ((old->tv_sec == now.tv_sec) && (old->tv_usec > now.tv_usec))) {
  2903. /* The starting point is in the future. */
  2904. return (0);
  2905. }
  2906. timevalsub(&now, old);
  2907. rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
  2908. if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
  2909. /* The RTT is larger than a sane value. */
  2910. return (0);
  2911. }
  2912. /* store the current RTT in us */
  2913. net->rtt = rtt_us;
  2914. /* compute rtt in ms */
  2915. rtt = (int32_t)(net->rtt / 1000);
  2916. if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
  2917. /* Tell the CC module that a new update has just occurred from a sack */
  2918. (*asoc->cc_functions.sctp_rtt_calculated)(stcb, net, &now);
  2919. }
  2920. /* Do we need to determine the lan? We do this only
  2921. * on sacks i.e. RTT being determined from data not
  2922. * non-data (HB/INIT->INITACK).
  2923. */
  2924. if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
  2925. (net->lan_type == SCTP_LAN_UNKNOWN)) {
  2926. if (net->rtt > SCTP_LOCAL_LAN_RTT) {
  2927. net->lan_type = SCTP_LAN_INTERNET;
  2928. } else {
  2929. net->lan_type = SCTP_LAN_LOCAL;
  2930. }
  2931. }
  2932. /***************************/
  2933. /* 2. update RTTVAR & SRTT */
  2934. /***************************/
  2935. /*-
  2936. * Compute the scaled average lastsa and the
  2937. * scaled variance lastsv as described in van Jacobson
  2938. * Paper "Congestion Avoidance and Control", Annex A.
  2939. *
  2940. * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
  2941. * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
  2942. */
  2943. if (net->RTO_measured) {
  2944. rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
  2945. net->lastsa += rtt;
  2946. if (rtt < 0) {
  2947. rtt = -rtt;
  2948. }
  2949. rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
  2950. net->lastsv += rtt;
  2951. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
  2952. rto_logging(net, SCTP_LOG_RTTVAR);
  2953. }
  2954. } else {
  2955. /* First RTO measurement */
  2956. net->RTO_measured = 1;
  2957. first_measure = 1;
  2958. net->lastsa = rtt << SCTP_RTT_SHIFT;
  2959. net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
  2960. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
  2961. rto_logging(net, SCTP_LOG_INITIAL_RTT);
  2962. }
  2963. }
  2964. if (net->lastsv == 0) {
  2965. net->lastsv = SCTP_CLOCK_GRANULARITY;
  2966. }
  2967. new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
  2968. if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
  2969. (stcb->asoc.sat_network_lockout == 0)) {
  2970. stcb->asoc.sat_network = 1;
  2971. } else if ((!first_measure) && stcb->asoc.sat_network) {
  2972. stcb->asoc.sat_network = 0;
  2973. stcb->asoc.sat_network_lockout = 1;
  2974. }
  2975. /* bound it, per C6/C7 in Section 5.3.1 */
  2976. if (new_rto < stcb->asoc.minrto) {
  2977. new_rto = stcb->asoc.minrto;
  2978. }
  2979. if (new_rto > stcb->asoc.maxrto) {
  2980. new_rto = stcb->asoc.maxrto;
  2981. }
  2982. net->RTO = new_rto;
  2983. return (1);
  2984. }
  2985. /*
  2986. * return a pointer to a contiguous piece of data from the given mbuf chain
  2987. * starting at 'off' for 'len' bytes. If the desired piece spans more than
  2988. * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
  2989. * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
  2990. */
  2991. caddr_t
  2992. sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
  2993. {
  2994. uint32_t count;
  2995. uint8_t *ptr;
  2996. ptr = in_ptr;
  2997. if ((off < 0) || (len <= 0))
  2998. return (NULL);
  2999. /* find the desired start location */
  3000. while ((m != NULL) && (off > 0)) {
  3001. if (off < SCTP_BUF_LEN(m))
  3002. break;
  3003. off -= SCTP_BUF_LEN(m);
  3004. m = SCTP_BUF_NEXT(m);
  3005. }
  3006. if (m == NULL)
  3007. return (NULL);
  3008. /* is the current mbuf large enough (eg. contiguous)? */
  3009. if ((SCTP_BUF_LEN(m) - off) >= len) {
  3010. return (mtod(m, caddr_t) + off);
  3011. } else {
  3012. /* else, it spans more than one mbuf, so save a temp copy... */
  3013. while ((m != NULL) && (len > 0)) {
  3014. count = min(SCTP_BUF_LEN(m) - off, len);
  3015. memcpy(ptr, mtod(m, caddr_t) + off, count);
  3016. len -= count;
  3017. ptr += count;
  3018. off = 0;
  3019. m = SCTP_BUF_NEXT(m);
  3020. }
  3021. if ((m == NULL) && (len > 0))
  3022. return (NULL);
  3023. else
  3024. return ((caddr_t)in_ptr);
  3025. }
  3026. }
  3027. struct sctp_paramhdr *
  3028. sctp_get_next_param(struct mbuf *m,
  3029. int offset,
  3030. struct sctp_paramhdr *pull,
  3031. int pull_limit)
  3032. {
  3033. /* This just provides a typed signature to Peter's Pull routine */
  3034. return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
  3035. (uint8_t *) pull));
  3036. }
  3037. struct mbuf *
  3038. sctp_add_pad_tombuf(struct mbuf *m, int padlen)
  3039. {
  3040. struct mbuf *m_last;
  3041. caddr_t dp;
  3042. if (padlen > 3) {
  3043. return (NULL);
  3044. }
  3045. if (padlen <= M_TRAILINGSPACE(m)) {
  3046. /*
  3047. * The easy way. We hope the majority of the time we hit
  3048. * here :)
  3049. */
  3050. m_last = m;
  3051. } else {
  3052. /* Hard way we must grow the mbuf chain */
  3053. m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
  3054. if (m_last == NULL) {
  3055. return (NULL);
  3056. }
  3057. SCTP_BUF_LEN(m_last) = 0;
  3058. SCTP_BUF_NEXT(m_last) = NULL;
  3059. SCTP_BUF_NEXT(m) = m_last;
  3060. }
  3061. dp = mtod(m_last, caddr_t) + SCTP_BUF_LEN(m_last);
  3062. SCTP_BUF_LEN(m_last) += padlen;
  3063. memset(dp, 0, padlen);
  3064. return (m_last);
  3065. }
  3066. struct mbuf *
  3067. sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
  3068. {
  3069. /* find the last mbuf in chain and pad it */
  3070. struct mbuf *m_at;
  3071. if (last_mbuf != NULL) {
  3072. return (sctp_add_pad_tombuf(last_mbuf, padval));
  3073. } else {
  3074. for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
  3075. if (SCTP_BUF_NEXT(m_at) == NULL) {
  3076. return (sctp_add_pad_tombuf(m_at, padval));
  3077. }
  3078. }
  3079. }
  3080. return (NULL);
  3081. }
  3082. static void
  3083. sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
  3084. uint16_t error, struct sctp_abort_chunk *abort,
  3085. bool from_peer, bool timedout, int so_locked)
  3086. {
  3087. struct mbuf *m_notify;
  3088. struct sctp_assoc_change *sac;
  3089. struct sctp_queued_to_read *control;
  3090. unsigned int notif_len;
  3091. uint16_t abort_len;
  3092. unsigned int i;
  3093. #if defined(__APPLE__) && !defined(__Userspace__)
  3094. struct socket *so;
  3095. #endif
  3096. KASSERT(abort == NULL || from_peer,
  3097. ("sctp_notify_assoc_change: ABORT chunk provided for local termination"));
  3098. KASSERT(!from_peer || !timedout,
  3099. ("sctp_notify_assoc_change: timeouts can only be local"));
  3100. if (stcb == NULL) {
  3101. return;
  3102. }
  3103. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
  3104. notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
  3105. if (abort != NULL) {
  3106. abort_len = ntohs(abort->ch.chunk_length);
  3107. /*
  3108. * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
  3109. * contiguous.
  3110. */
  3111. if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
  3112. abort_len = SCTP_CHUNK_BUFFER_SIZE;
  3113. }
  3114. } else {
  3115. abort_len = 0;
  3116. }
  3117. if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
  3118. notif_len += SCTP_ASSOC_SUPPORTS_MAX;
  3119. } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
  3120. notif_len += abort_len;
  3121. }
  3122. m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
  3123. if (m_notify == NULL) {
  3124. /* Retry with smaller value. */
  3125. notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
  3126. m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
  3127. if (m_notify == NULL) {
  3128. goto set_error;
  3129. }
  3130. }
  3131. SCTP_BUF_NEXT(m_notify) = NULL;
  3132. sac = mtod(m_notify, struct sctp_assoc_change *);
  3133. memset(sac, 0, notif_len);
  3134. sac->sac_type = SCTP_ASSOC_CHANGE;
  3135. sac->sac_flags = 0;
  3136. sac->sac_length = sizeof(struct sctp_assoc_change);
  3137. sac->sac_state = state;
  3138. sac->sac_error = error;
  3139. if (state == SCTP_CANT_STR_ASSOC) {
  3140. sac->sac_outbound_streams = 0;
  3141. sac->sac_inbound_streams = 0;
  3142. } else {
  3143. sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
  3144. sac->sac_inbound_streams = stcb->asoc.streamincnt;
  3145. }
  3146. sac->sac_assoc_id = sctp_get_associd(stcb);
  3147. if (notif_len > sizeof(struct sctp_assoc_change)) {
  3148. if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
  3149. i = 0;
  3150. if (stcb->asoc.prsctp_supported == 1) {
  3151. sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
  3152. }
  3153. if (stcb->asoc.auth_supported == 1) {
  3154. sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
  3155. }
  3156. if (stcb->asoc.asconf_supported == 1) {
  3157. sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
  3158. }
  3159. if (stcb->asoc.idata_supported == 1) {
  3160. sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
  3161. }
  3162. sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
  3163. if (stcb->asoc.reconfig_supported == 1) {
  3164. sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
  3165. }
  3166. sac->sac_length += i;
  3167. } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
  3168. memcpy(sac->sac_info, abort, abort_len);
  3169. sac->sac_length += abort_len;
  3170. }
  3171. }
  3172. SCTP_BUF_LEN(m_notify) = sac->sac_length;
  3173. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3174. 0, 0, stcb->asoc.context, 0, 0, 0,
  3175. m_notify);
  3176. if (control != NULL) {
  3177. control->length = SCTP_BUF_LEN(m_notify);
  3178. control->spec_flags = M_NOTIFICATION;
  3179. /* not that we need this */
  3180. control->tail_mbuf = m_notify;
  3181. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3182. control,
  3183. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD,
  3184. so_locked);
  3185. } else {
  3186. sctp_m_freem(m_notify);
  3187. }
  3188. }
  3189. /*
  3190. * For 1-to-1 style sockets, we send up and error when an ABORT
  3191. * comes in.
  3192. */
  3193. set_error:
  3194. if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  3195. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
  3196. ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
  3197. SOCK_LOCK(stcb->sctp_socket);
  3198. if (from_peer) {
  3199. if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
  3200. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
  3201. stcb->sctp_socket->so_error = ECONNREFUSED;
  3202. } else {
  3203. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
  3204. stcb->sctp_socket->so_error = ECONNRESET;
  3205. }
  3206. } else {
  3207. if (timedout) {
  3208. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
  3209. stcb->sctp_socket->so_error = ETIMEDOUT;
  3210. } else {
  3211. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
  3212. stcb->sctp_socket->so_error = ECONNABORTED;
  3213. }
  3214. }
  3215. SOCK_UNLOCK(stcb->sctp_socket);
  3216. }
  3217. /* Wake ANY sleepers */
  3218. #if defined(__APPLE__) && !defined(__Userspace__)
  3219. so = SCTP_INP_SO(stcb->sctp_ep);
  3220. if (!so_locked) {
  3221. atomic_add_int(&stcb->asoc.refcnt, 1);
  3222. SCTP_TCB_UNLOCK(stcb);
  3223. SCTP_SOCKET_LOCK(so, 1);
  3224. SCTP_TCB_LOCK(stcb);
  3225. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  3226. if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
  3227. SCTP_SOCKET_UNLOCK(so, 1);
  3228. return;
  3229. }
  3230. }
  3231. #endif
  3232. if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  3233. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
  3234. ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
  3235. socantrcvmore(stcb->sctp_socket);
  3236. }
  3237. sorwakeup(stcb->sctp_socket);
  3238. sowwakeup(stcb->sctp_socket);
  3239. #if defined(__APPLE__) && !defined(__Userspace__)
  3240. if (!so_locked) {
  3241. SCTP_SOCKET_UNLOCK(so, 1);
  3242. }
  3243. #endif
  3244. }
  3245. static void
  3246. sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
  3247. struct sockaddr *sa, uint32_t error, int so_locked)
  3248. {
  3249. struct mbuf *m_notify;
  3250. struct sctp_paddr_change *spc;
  3251. struct sctp_queued_to_read *control;
  3252. if ((stcb == NULL) ||
  3253. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
  3254. /* event not enabled */
  3255. return;
  3256. }
  3257. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
  3258. if (m_notify == NULL)
  3259. return;
  3260. SCTP_BUF_LEN(m_notify) = 0;
  3261. spc = mtod(m_notify, struct sctp_paddr_change *);
  3262. memset(spc, 0, sizeof(struct sctp_paddr_change));
  3263. spc->spc_type = SCTP_PEER_ADDR_CHANGE;
  3264. spc->spc_flags = 0;
  3265. spc->spc_length = sizeof(struct sctp_paddr_change);
  3266. switch (sa->sa_family) {
  3267. #ifdef INET
  3268. case AF_INET:
  3269. #ifdef INET6
  3270. if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
  3271. in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
  3272. (struct sockaddr_in6 *)&spc->spc_aaddr);
  3273. } else {
  3274. memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
  3275. }
  3276. #else
  3277. memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
  3278. #endif
  3279. break;
  3280. #endif
  3281. #ifdef INET6
  3282. case AF_INET6:
  3283. {
  3284. #ifdef SCTP_EMBEDDED_V6_SCOPE
  3285. struct sockaddr_in6 *sin6;
  3286. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  3287. memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
  3288. #ifdef SCTP_EMBEDDED_V6_SCOPE
  3289. sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
  3290. if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
  3291. if (sin6->sin6_scope_id == 0) {
  3292. /* recover scope_id for user */
  3293. #ifdef SCTP_KAME
  3294. (void)sa6_recoverscope(sin6);
  3295. #else
  3296. (void)in6_recoverscope(sin6, &sin6->sin6_addr,
  3297. NULL);
  3298. #endif
  3299. } else {
  3300. /* clear embedded scope_id for user */
  3301. in6_clearscope(&sin6->sin6_addr);
  3302. }
  3303. }
  3304. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  3305. break;
  3306. }
  3307. #endif
  3308. #if defined(__Userspace__)
  3309. case AF_CONN:
  3310. memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_conn));
  3311. break;
  3312. #endif
  3313. default:
  3314. /* TSNH */
  3315. break;
  3316. }
  3317. spc->spc_state = state;
  3318. spc->spc_error = error;
  3319. spc->spc_assoc_id = sctp_get_associd(stcb);
  3320. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
  3321. SCTP_BUF_NEXT(m_notify) = NULL;
  3322. /* append to socket */
  3323. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3324. 0, 0, stcb->asoc.context, 0, 0, 0,
  3325. m_notify);
  3326. if (control == NULL) {
  3327. /* no memory */
  3328. sctp_m_freem(m_notify);
  3329. return;
  3330. }
  3331. control->length = SCTP_BUF_LEN(m_notify);
  3332. control->spec_flags = M_NOTIFICATION;
  3333. /* not that we need this */
  3334. control->tail_mbuf = m_notify;
  3335. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3336. control,
  3337. &stcb->sctp_socket->so_rcv, 1,
  3338. SCTP_READ_LOCK_NOT_HELD,
  3339. so_locked);
  3340. }
  3341. static void
  3342. sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
  3343. struct sctp_tmit_chunk *chk, int so_locked)
  3344. {
  3345. struct mbuf *m_notify;
  3346. struct sctp_send_failed *ssf;
  3347. struct sctp_send_failed_event *ssfe;
  3348. struct sctp_queued_to_read *control;
  3349. struct sctp_chunkhdr *chkhdr;
  3350. int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
  3351. if ((stcb == NULL) ||
  3352. (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
  3353. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
  3354. /* event not enabled */
  3355. return;
  3356. }
  3357. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
  3358. notifhdr_len = sizeof(struct sctp_send_failed_event);
  3359. } else {
  3360. notifhdr_len = sizeof(struct sctp_send_failed);
  3361. }
  3362. m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
  3363. if (m_notify == NULL)
  3364. /* no space left */
  3365. return;
  3366. SCTP_BUF_LEN(m_notify) = notifhdr_len;
  3367. if (stcb->asoc.idata_supported) {
  3368. chkhdr_len = sizeof(struct sctp_idata_chunk);
  3369. } else {
  3370. chkhdr_len = sizeof(struct sctp_data_chunk);
  3371. }
  3372. /* Use some defaults in case we can't access the chunk header */
  3373. if (chk->send_size >= chkhdr_len) {
  3374. payload_len = chk->send_size - chkhdr_len;
  3375. } else {
  3376. payload_len = 0;
  3377. }
  3378. padding_len = 0;
  3379. if (chk->data != NULL) {
  3380. chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
  3381. if (chkhdr != NULL) {
  3382. chk_len = ntohs(chkhdr->chunk_length);
  3383. if ((chk_len >= chkhdr_len) &&
  3384. (chk->send_size >= chk_len) &&
  3385. (chk->send_size - chk_len < 4)) {
  3386. padding_len = chk->send_size - chk_len;
  3387. payload_len = chk->send_size - chkhdr_len - padding_len;
  3388. }
  3389. }
  3390. }
  3391. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
  3392. ssfe = mtod(m_notify, struct sctp_send_failed_event *);
  3393. memset(ssfe, 0, notifhdr_len);
  3394. ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
  3395. if (sent) {
  3396. ssfe->ssfe_flags = SCTP_DATA_SENT;
  3397. } else {
  3398. ssfe->ssfe_flags = SCTP_DATA_UNSENT;
  3399. }
  3400. ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
  3401. ssfe->ssfe_error = error;
  3402. /* not exactly what the user sent in, but should be close :) */
  3403. ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
  3404. ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
  3405. ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
  3406. ssfe->ssfe_info.snd_context = chk->rec.data.context;
  3407. ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
  3408. ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
  3409. } else {
  3410. ssf = mtod(m_notify, struct sctp_send_failed *);
  3411. memset(ssf, 0, notifhdr_len);
  3412. ssf->ssf_type = SCTP_SEND_FAILED;
  3413. if (sent) {
  3414. ssf->ssf_flags = SCTP_DATA_SENT;
  3415. } else {
  3416. ssf->ssf_flags = SCTP_DATA_UNSENT;
  3417. }
  3418. ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
  3419. ssf->ssf_error = error;
  3420. /* not exactly what the user sent in, but should be close :) */
  3421. ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
  3422. ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
  3423. ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
  3424. ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
  3425. ssf->ssf_info.sinfo_context = chk->rec.data.context;
  3426. ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
  3427. ssf->ssf_assoc_id = sctp_get_associd(stcb);
  3428. }
  3429. if (chk->data != NULL) {
  3430. /* Trim off the sctp chunk header (it should be there) */
  3431. if (chk->send_size == chkhdr_len + payload_len + padding_len) {
  3432. m_adj(chk->data, chkhdr_len);
  3433. m_adj(chk->data, -padding_len);
  3434. sctp_mbuf_crush(chk->data);
  3435. chk->send_size -= (chkhdr_len + padding_len);
  3436. }
  3437. }
  3438. SCTP_BUF_NEXT(m_notify) = chk->data;
  3439. /* Steal off the mbuf */
  3440. chk->data = NULL;
  3441. /*
  3442. * For this case, we check the actual socket buffer, since the assoc
  3443. * is going away we don't want to overfill the socket buffer for a
  3444. * non-reader
  3445. */
  3446. if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
  3447. sctp_m_freem(m_notify);
  3448. return;
  3449. }
  3450. /* append to socket */
  3451. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3452. 0, 0, stcb->asoc.context, 0, 0, 0,
  3453. m_notify);
  3454. if (control == NULL) {
  3455. /* no memory */
  3456. sctp_m_freem(m_notify);
  3457. return;
  3458. }
  3459. control->length = SCTP_BUF_LEN(m_notify);
  3460. control->spec_flags = M_NOTIFICATION;
  3461. /* not that we need this */
  3462. control->tail_mbuf = m_notify;
  3463. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3464. control,
  3465. &stcb->sctp_socket->so_rcv, 1,
  3466. SCTP_READ_LOCK_NOT_HELD,
  3467. so_locked);
  3468. }
  3469. static void
  3470. sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
  3471. struct sctp_stream_queue_pending *sp, int so_locked)
  3472. {
  3473. struct mbuf *m_notify;
  3474. struct sctp_send_failed *ssf;
  3475. struct sctp_send_failed_event *ssfe;
  3476. struct sctp_queued_to_read *control;
  3477. int notifhdr_len;
  3478. if ((stcb == NULL) ||
  3479. (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
  3480. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT))) {
  3481. /* event not enabled */
  3482. return;
  3483. }
  3484. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
  3485. notifhdr_len = sizeof(struct sctp_send_failed_event);
  3486. } else {
  3487. notifhdr_len = sizeof(struct sctp_send_failed);
  3488. }
  3489. m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
  3490. if (m_notify == NULL) {
  3491. /* no space left */
  3492. return;
  3493. }
  3494. SCTP_BUF_LEN(m_notify) = notifhdr_len;
  3495. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
  3496. ssfe = mtod(m_notify, struct sctp_send_failed_event *);
  3497. memset(ssfe, 0, notifhdr_len);
  3498. ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
  3499. ssfe->ssfe_flags = SCTP_DATA_UNSENT;
  3500. ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
  3501. ssfe->ssfe_error = error;
  3502. /* not exactly what the user sent in, but should be close :) */
  3503. ssfe->ssfe_info.snd_sid = sp->sid;
  3504. if (sp->some_taken) {
  3505. ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
  3506. } else {
  3507. ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
  3508. }
  3509. ssfe->ssfe_info.snd_ppid = sp->ppid;
  3510. ssfe->ssfe_info.snd_context = sp->context;
  3511. ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
  3512. ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
  3513. } else {
  3514. ssf = mtod(m_notify, struct sctp_send_failed *);
  3515. memset(ssf, 0, notifhdr_len);
  3516. ssf->ssf_type = SCTP_SEND_FAILED;
  3517. ssf->ssf_flags = SCTP_DATA_UNSENT;
  3518. ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
  3519. ssf->ssf_error = error;
  3520. /* not exactly what the user sent in, but should be close :) */
  3521. ssf->ssf_info.sinfo_stream = sp->sid;
  3522. ssf->ssf_info.sinfo_ssn = 0;
  3523. if (sp->some_taken) {
  3524. ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
  3525. } else {
  3526. ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
  3527. }
  3528. ssf->ssf_info.sinfo_ppid = sp->ppid;
  3529. ssf->ssf_info.sinfo_context = sp->context;
  3530. ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
  3531. ssf->ssf_assoc_id = sctp_get_associd(stcb);
  3532. }
  3533. SCTP_BUF_NEXT(m_notify) = sp->data;
  3534. /* Steal off the mbuf */
  3535. sp->data = NULL;
  3536. /*
  3537. * For this case, we check the actual socket buffer, since the assoc
  3538. * is going away we don't want to overfill the socket buffer for a
  3539. * non-reader
  3540. */
  3541. if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
  3542. sctp_m_freem(m_notify);
  3543. return;
  3544. }
  3545. /* append to socket */
  3546. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3547. 0, 0, stcb->asoc.context, 0, 0, 0,
  3548. m_notify);
  3549. if (control == NULL) {
  3550. /* no memory */
  3551. sctp_m_freem(m_notify);
  3552. return;
  3553. }
  3554. control->length = SCTP_BUF_LEN(m_notify);
  3555. control->spec_flags = M_NOTIFICATION;
  3556. /* not that we need this */
  3557. control->tail_mbuf = m_notify;
  3558. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3559. control,
  3560. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
  3561. }
  3562. static void
  3563. sctp_notify_adaptation_layer(struct sctp_tcb *stcb)
  3564. {
  3565. struct mbuf *m_notify;
  3566. struct sctp_adaptation_event *sai;
  3567. struct sctp_queued_to_read *control;
  3568. if ((stcb == NULL) ||
  3569. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
  3570. /* event not enabled */
  3571. return;
  3572. }
  3573. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
  3574. if (m_notify == NULL)
  3575. /* no space left */
  3576. return;
  3577. SCTP_BUF_LEN(m_notify) = 0;
  3578. sai = mtod(m_notify, struct sctp_adaptation_event *);
  3579. memset(sai, 0, sizeof(struct sctp_adaptation_event));
  3580. sai->sai_type = SCTP_ADAPTATION_INDICATION;
  3581. sai->sai_flags = 0;
  3582. sai->sai_length = sizeof(struct sctp_adaptation_event);
  3583. sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
  3584. sai->sai_assoc_id = sctp_get_associd(stcb);
  3585. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
  3586. SCTP_BUF_NEXT(m_notify) = NULL;
  3587. /* append to socket */
  3588. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3589. 0, 0, stcb->asoc.context, 0, 0, 0,
  3590. m_notify);
  3591. if (control == NULL) {
  3592. /* no memory */
  3593. sctp_m_freem(m_notify);
  3594. return;
  3595. }
  3596. control->length = SCTP_BUF_LEN(m_notify);
  3597. control->spec_flags = M_NOTIFICATION;
  3598. /* not that we need this */
  3599. control->tail_mbuf = m_notify;
  3600. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3601. control,
  3602. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
  3603. }
  3604. /* This always must be called with the read-queue LOCKED in the INP */
  3605. static void
  3606. sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
  3607. uint32_t val, int so_locked)
  3608. {
  3609. struct mbuf *m_notify;
  3610. struct sctp_pdapi_event *pdapi;
  3611. struct sctp_queued_to_read *control;
  3612. struct sockbuf *sb;
  3613. if ((stcb == NULL) ||
  3614. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
  3615. /* event not enabled */
  3616. return;
  3617. }
  3618. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
  3619. return;
  3620. }
  3621. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
  3622. if (m_notify == NULL)
  3623. /* no space left */
  3624. return;
  3625. SCTP_BUF_LEN(m_notify) = 0;
  3626. pdapi = mtod(m_notify, struct sctp_pdapi_event *);
  3627. memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
  3628. pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
  3629. pdapi->pdapi_flags = 0;
  3630. pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
  3631. pdapi->pdapi_indication = error;
  3632. pdapi->pdapi_stream = (val >> 16);
  3633. pdapi->pdapi_seq = (val & 0x0000ffff);
  3634. pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
  3635. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
  3636. SCTP_BUF_NEXT(m_notify) = NULL;
  3637. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3638. 0, 0, stcb->asoc.context, 0, 0, 0,
  3639. m_notify);
  3640. if (control == NULL) {
  3641. /* no memory */
  3642. sctp_m_freem(m_notify);
  3643. return;
  3644. }
  3645. control->length = SCTP_BUF_LEN(m_notify);
  3646. control->spec_flags = M_NOTIFICATION;
  3647. /* not that we need this */
  3648. control->tail_mbuf = m_notify;
  3649. sb = &stcb->sctp_socket->so_rcv;
  3650. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  3651. sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
  3652. }
  3653. sctp_sballoc(stcb, sb, m_notify);
  3654. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  3655. sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
  3656. }
  3657. control->end_added = 1;
  3658. if (stcb->asoc.control_pdapi)
  3659. TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, stcb->asoc.control_pdapi, control, next);
  3660. else {
  3661. /* we really should not see this case */
  3662. TAILQ_INSERT_TAIL(&stcb->sctp_ep->read_queue, control, next);
  3663. }
  3664. if (stcb->sctp_ep && stcb->sctp_socket) {
  3665. /* This should always be the case */
  3666. #if defined(__APPLE__) && !defined(__Userspace__)
  3667. struct socket *so;
  3668. so = SCTP_INP_SO(stcb->sctp_ep);
  3669. if (!so_locked) {
  3670. atomic_add_int(&stcb->asoc.refcnt, 1);
  3671. SCTP_TCB_UNLOCK(stcb);
  3672. SCTP_SOCKET_LOCK(so, 1);
  3673. SCTP_TCB_LOCK(stcb);
  3674. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  3675. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  3676. SCTP_SOCKET_UNLOCK(so, 1);
  3677. return;
  3678. }
  3679. }
  3680. #endif
  3681. sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
  3682. #if defined(__APPLE__) && !defined(__Userspace__)
  3683. if (!so_locked) {
  3684. SCTP_SOCKET_UNLOCK(so, 1);
  3685. }
  3686. #endif
  3687. }
  3688. }
  3689. static void
  3690. sctp_notify_shutdown_event(struct sctp_tcb *stcb)
  3691. {
  3692. struct mbuf *m_notify;
  3693. struct sctp_shutdown_event *sse;
  3694. struct sctp_queued_to_read *control;
  3695. /*
  3696. * For TCP model AND UDP connected sockets we will send an error up
  3697. * when an SHUTDOWN completes
  3698. */
  3699. if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  3700. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
  3701. /* mark socket closed for read/write and wakeup! */
  3702. #if defined(__APPLE__) && !defined(__Userspace__)
  3703. struct socket *so;
  3704. so = SCTP_INP_SO(stcb->sctp_ep);
  3705. atomic_add_int(&stcb->asoc.refcnt, 1);
  3706. SCTP_TCB_UNLOCK(stcb);
  3707. SCTP_SOCKET_LOCK(so, 1);
  3708. SCTP_TCB_LOCK(stcb);
  3709. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  3710. if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
  3711. SCTP_SOCKET_UNLOCK(so, 1);
  3712. return;
  3713. }
  3714. #endif
  3715. socantsendmore(stcb->sctp_socket);
  3716. #if defined(__APPLE__) && !defined(__Userspace__)
  3717. SCTP_SOCKET_UNLOCK(so, 1);
  3718. #endif
  3719. }
  3720. if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
  3721. /* event not enabled */
  3722. return;
  3723. }
  3724. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
  3725. if (m_notify == NULL)
  3726. /* no space left */
  3727. return;
  3728. sse = mtod(m_notify, struct sctp_shutdown_event *);
  3729. memset(sse, 0, sizeof(struct sctp_shutdown_event));
  3730. sse->sse_type = SCTP_SHUTDOWN_EVENT;
  3731. sse->sse_flags = 0;
  3732. sse->sse_length = sizeof(struct sctp_shutdown_event);
  3733. sse->sse_assoc_id = sctp_get_associd(stcb);
  3734. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
  3735. SCTP_BUF_NEXT(m_notify) = NULL;
  3736. /* append to socket */
  3737. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3738. 0, 0, stcb->asoc.context, 0, 0, 0,
  3739. m_notify);
  3740. if (control == NULL) {
  3741. /* no memory */
  3742. sctp_m_freem(m_notify);
  3743. return;
  3744. }
  3745. control->length = SCTP_BUF_LEN(m_notify);
  3746. control->spec_flags = M_NOTIFICATION;
  3747. /* not that we need this */
  3748. control->tail_mbuf = m_notify;
  3749. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3750. control,
  3751. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
  3752. }
  3753. static void
  3754. sctp_notify_sender_dry_event(struct sctp_tcb *stcb,
  3755. int so_locked)
  3756. {
  3757. struct mbuf *m_notify;
  3758. struct sctp_sender_dry_event *event;
  3759. struct sctp_queued_to_read *control;
  3760. if ((stcb == NULL) ||
  3761. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
  3762. /* event not enabled */
  3763. return;
  3764. }
  3765. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
  3766. if (m_notify == NULL) {
  3767. /* no space left */
  3768. return;
  3769. }
  3770. SCTP_BUF_LEN(m_notify) = 0;
  3771. event = mtod(m_notify, struct sctp_sender_dry_event *);
  3772. memset(event, 0, sizeof(struct sctp_sender_dry_event));
  3773. event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
  3774. event->sender_dry_flags = 0;
  3775. event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
  3776. event->sender_dry_assoc_id = sctp_get_associd(stcb);
  3777. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
  3778. SCTP_BUF_NEXT(m_notify) = NULL;
  3779. /* append to socket */
  3780. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3781. 0, 0, stcb->asoc.context, 0, 0, 0,
  3782. m_notify);
  3783. if (control == NULL) {
  3784. /* no memory */
  3785. sctp_m_freem(m_notify);
  3786. return;
  3787. }
  3788. control->length = SCTP_BUF_LEN(m_notify);
  3789. control->spec_flags = M_NOTIFICATION;
  3790. /* not that we need this */
  3791. control->tail_mbuf = m_notify;
  3792. sctp_add_to_readq(stcb->sctp_ep, stcb, control,
  3793. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
  3794. }
  3795. void
  3796. sctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin, uint16_t numberout, int flag)
  3797. {
  3798. struct mbuf *m_notify;
  3799. struct sctp_queued_to_read *control;
  3800. struct sctp_stream_change_event *stradd;
  3801. if ((stcb == NULL) ||
  3802. (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT))) {
  3803. /* event not enabled */
  3804. return;
  3805. }
  3806. if ((stcb->asoc.peer_req_out) && flag) {
  3807. /* Peer made the request, don't tell the local user */
  3808. stcb->asoc.peer_req_out = 0;
  3809. return;
  3810. }
  3811. stcb->asoc.peer_req_out = 0;
  3812. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
  3813. if (m_notify == NULL)
  3814. /* no space left */
  3815. return;
  3816. SCTP_BUF_LEN(m_notify) = 0;
  3817. stradd = mtod(m_notify, struct sctp_stream_change_event *);
  3818. memset(stradd, 0, sizeof(struct sctp_stream_change_event));
  3819. stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
  3820. stradd->strchange_flags = flag;
  3821. stradd->strchange_length = sizeof(struct sctp_stream_change_event);
  3822. stradd->strchange_assoc_id = sctp_get_associd(stcb);
  3823. stradd->strchange_instrms = numberin;
  3824. stradd->strchange_outstrms = numberout;
  3825. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
  3826. SCTP_BUF_NEXT(m_notify) = NULL;
  3827. if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
  3828. /* no space */
  3829. sctp_m_freem(m_notify);
  3830. return;
  3831. }
  3832. /* append to socket */
  3833. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3834. 0, 0, stcb->asoc.context, 0, 0, 0,
  3835. m_notify);
  3836. if (control == NULL) {
  3837. /* no memory */
  3838. sctp_m_freem(m_notify);
  3839. return;
  3840. }
  3841. control->length = SCTP_BUF_LEN(m_notify);
  3842. control->spec_flags = M_NOTIFICATION;
  3843. /* not that we need this */
  3844. control->tail_mbuf = m_notify;
  3845. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3846. control,
  3847. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
  3848. }
  3849. void
  3850. sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag)
  3851. {
  3852. struct mbuf *m_notify;
  3853. struct sctp_queued_to_read *control;
  3854. struct sctp_assoc_reset_event *strasoc;
  3855. if ((stcb == NULL) ||
  3856. (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT))) {
  3857. /* event not enabled */
  3858. return;
  3859. }
  3860. m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
  3861. if (m_notify == NULL)
  3862. /* no space left */
  3863. return;
  3864. SCTP_BUF_LEN(m_notify) = 0;
  3865. strasoc = mtod(m_notify, struct sctp_assoc_reset_event *);
  3866. memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
  3867. strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
  3868. strasoc->assocreset_flags = flag;
  3869. strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
  3870. strasoc->assocreset_assoc_id= sctp_get_associd(stcb);
  3871. strasoc->assocreset_local_tsn = sending_tsn;
  3872. strasoc->assocreset_remote_tsn = recv_tsn;
  3873. SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
  3874. SCTP_BUF_NEXT(m_notify) = NULL;
  3875. if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
  3876. /* no space */
  3877. sctp_m_freem(m_notify);
  3878. return;
  3879. }
  3880. /* append to socket */
  3881. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3882. 0, 0, stcb->asoc.context, 0, 0, 0,
  3883. m_notify);
  3884. if (control == NULL) {
  3885. /* no memory */
  3886. sctp_m_freem(m_notify);
  3887. return;
  3888. }
  3889. control->length = SCTP_BUF_LEN(m_notify);
  3890. control->spec_flags = M_NOTIFICATION;
  3891. /* not that we need this */
  3892. control->tail_mbuf = m_notify;
  3893. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3894. control,
  3895. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
  3896. }
  3897. static void
  3898. sctp_notify_stream_reset(struct sctp_tcb *stcb,
  3899. int number_entries, uint16_t * list, int flag)
  3900. {
  3901. struct mbuf *m_notify;
  3902. struct sctp_queued_to_read *control;
  3903. struct sctp_stream_reset_event *strreset;
  3904. int len;
  3905. if ((stcb == NULL) ||
  3906. (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT))) {
  3907. /* event not enabled */
  3908. return;
  3909. }
  3910. m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  3911. if (m_notify == NULL)
  3912. /* no space left */
  3913. return;
  3914. SCTP_BUF_LEN(m_notify) = 0;
  3915. len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
  3916. if (len > M_TRAILINGSPACE(m_notify)) {
  3917. /* never enough room */
  3918. sctp_m_freem(m_notify);
  3919. return;
  3920. }
  3921. strreset = mtod(m_notify, struct sctp_stream_reset_event *);
  3922. memset(strreset, 0, len);
  3923. strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
  3924. strreset->strreset_flags = flag;
  3925. strreset->strreset_length = len;
  3926. strreset->strreset_assoc_id = sctp_get_associd(stcb);
  3927. if (number_entries) {
  3928. int i;
  3929. for (i = 0; i < number_entries; i++) {
  3930. strreset->strreset_stream_list[i] = ntohs(list[i]);
  3931. }
  3932. }
  3933. SCTP_BUF_LEN(m_notify) = len;
  3934. SCTP_BUF_NEXT(m_notify) = NULL;
  3935. if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
  3936. /* no space */
  3937. sctp_m_freem(m_notify);
  3938. return;
  3939. }
  3940. /* append to socket */
  3941. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  3942. 0, 0, stcb->asoc.context, 0, 0, 0,
  3943. m_notify);
  3944. if (control == NULL) {
  3945. /* no memory */
  3946. sctp_m_freem(m_notify);
  3947. return;
  3948. }
  3949. control->length = SCTP_BUF_LEN(m_notify);
  3950. control->spec_flags = M_NOTIFICATION;
  3951. /* not that we need this */
  3952. control->tail_mbuf = m_notify;
  3953. sctp_add_to_readq(stcb->sctp_ep, stcb,
  3954. control,
  3955. &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
  3956. }
  3957. static void
  3958. sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error, struct sctp_error_chunk *chunk)
  3959. {
  3960. struct mbuf *m_notify;
  3961. struct sctp_remote_error *sre;
  3962. struct sctp_queued_to_read *control;
  3963. unsigned int notif_len;
  3964. uint16_t chunk_len;
  3965. if ((stcb == NULL) ||
  3966. sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
  3967. return;
  3968. }
  3969. if (chunk != NULL) {
  3970. chunk_len = ntohs(chunk->ch.chunk_length);
  3971. /*
  3972. * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
  3973. * contiguous.
  3974. */
  3975. if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
  3976. chunk_len = SCTP_CHUNK_BUFFER_SIZE;
  3977. }
  3978. } else {
  3979. chunk_len = 0;
  3980. }
  3981. notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
  3982. m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
  3983. if (m_notify == NULL) {
  3984. /* Retry with smaller value. */
  3985. notif_len = (unsigned int)sizeof(struct sctp_remote_error);
  3986. m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
  3987. if (m_notify == NULL) {
  3988. return;
  3989. }
  3990. }
  3991. SCTP_BUF_NEXT(m_notify) = NULL;
  3992. sre = mtod(m_notify, struct sctp_remote_error *);
  3993. memset(sre, 0, notif_len);
  3994. sre->sre_type = SCTP_REMOTE_ERROR;
  3995. sre->sre_flags = 0;
  3996. sre->sre_length = sizeof(struct sctp_remote_error);
  3997. sre->sre_error = error;
  3998. sre->sre_assoc_id = sctp_get_associd(stcb);
  3999. if (notif_len > sizeof(struct sctp_remote_error)) {
  4000. memcpy(sre->sre_data, chunk, chunk_len);
  4001. sre->sre_length += chunk_len;
  4002. }
  4003. SCTP_BUF_LEN(m_notify) = sre->sre_length;
  4004. control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
  4005. 0, 0, stcb->asoc.context, 0, 0, 0,
  4006. m_notify);
  4007. if (control != NULL) {
  4008. control->length = SCTP_BUF_LEN(m_notify);
  4009. control->spec_flags = M_NOTIFICATION;
  4010. /* not that we need this */
  4011. control->tail_mbuf = m_notify;
  4012. sctp_add_to_readq(stcb->sctp_ep, stcb,
  4013. control,
  4014. &stcb->sctp_socket->so_rcv, 1,
  4015. SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
  4016. } else {
  4017. sctp_m_freem(m_notify);
  4018. }
  4019. }
  4020. void
  4021. sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
  4022. uint32_t error, void *data, int so_locked)
  4023. {
  4024. if ((stcb == NULL) ||
  4025. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  4026. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  4027. (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
  4028. /* If the socket is gone we are out of here */
  4029. return;
  4030. }
  4031. #if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
  4032. if (stcb->sctp_socket->so_rcv.sb_state & SBS_CANTRCVMORE) {
  4033. #else
  4034. if (stcb->sctp_socket->so_state & SS_CANTRCVMORE) {
  4035. #endif
  4036. return;
  4037. }
  4038. #if defined(__APPLE__) && !defined(__Userspace__)
  4039. if (so_locked) {
  4040. sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
  4041. } else {
  4042. sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
  4043. }
  4044. #endif
  4045. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  4046. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  4047. if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
  4048. (notification == SCTP_NOTIFY_INTERFACE_UP) ||
  4049. (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
  4050. /* Don't report these in front states */
  4051. return;
  4052. }
  4053. }
  4054. switch (notification) {
  4055. case SCTP_NOTIFY_ASSOC_UP:
  4056. if (stcb->asoc.assoc_up_sent == 0) {
  4057. sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, false, false, so_locked);
  4058. stcb->asoc.assoc_up_sent = 1;
  4059. }
  4060. if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
  4061. sctp_notify_adaptation_layer(stcb);
  4062. }
  4063. if (stcb->asoc.auth_supported == 0) {
  4064. sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
  4065. NULL, so_locked);
  4066. }
  4067. break;
  4068. case SCTP_NOTIFY_ASSOC_DOWN:
  4069. sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, false, false, so_locked);
  4070. #if defined(__Userspace__)
  4071. if (stcb->sctp_ep->recv_callback) {
  4072. if (stcb->sctp_socket) {
  4073. union sctp_sockstore addr;
  4074. struct sctp_rcvinfo rcv;
  4075. memset(&addr, 0, sizeof(union sctp_sockstore));
  4076. memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
  4077. atomic_add_int(&stcb->asoc.refcnt, 1);
  4078. SCTP_TCB_UNLOCK(stcb);
  4079. stcb->sctp_ep->recv_callback(stcb->sctp_socket, addr, NULL, 0, rcv, 0, stcb->sctp_ep->ulp_info);
  4080. SCTP_TCB_LOCK(stcb);
  4081. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  4082. }
  4083. }
  4084. #endif
  4085. break;
  4086. case SCTP_NOTIFY_INTERFACE_DOWN:
  4087. {
  4088. struct sctp_nets *net;
  4089. net = (struct sctp_nets *)data;
  4090. sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
  4091. (struct sockaddr *)&net->ro._l_addr, error, so_locked);
  4092. break;
  4093. }
  4094. case SCTP_NOTIFY_INTERFACE_UP:
  4095. {
  4096. struct sctp_nets *net;
  4097. net = (struct sctp_nets *)data;
  4098. sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
  4099. (struct sockaddr *)&net->ro._l_addr, error, so_locked);
  4100. break;
  4101. }
  4102. case SCTP_NOTIFY_INTERFACE_CONFIRMED:
  4103. {
  4104. struct sctp_nets *net;
  4105. net = (struct sctp_nets *)data;
  4106. sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
  4107. (struct sockaddr *)&net->ro._l_addr, error, so_locked);
  4108. break;
  4109. }
  4110. case SCTP_NOTIFY_SPECIAL_SP_FAIL:
  4111. sctp_notify_send_failed2(stcb, error,
  4112. (struct sctp_stream_queue_pending *)data, so_locked);
  4113. break;
  4114. case SCTP_NOTIFY_SENT_DG_FAIL:
  4115. sctp_notify_send_failed(stcb, 1, error,
  4116. (struct sctp_tmit_chunk *)data, so_locked);
  4117. break;
  4118. case SCTP_NOTIFY_UNSENT_DG_FAIL:
  4119. sctp_notify_send_failed(stcb, 0, error,
  4120. (struct sctp_tmit_chunk *)data, so_locked);
  4121. break;
  4122. case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
  4123. {
  4124. uint32_t val;
  4125. val = *((uint32_t *)data);
  4126. sctp_notify_partial_delivery_indication(stcb, error, val, so_locked);
  4127. break;
  4128. }
  4129. case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
  4130. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  4131. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  4132. sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, false, false, so_locked);
  4133. } else {
  4134. sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, false, false, so_locked);
  4135. }
  4136. break;
  4137. case SCTP_NOTIFY_ASSOC_REM_ABORTED:
  4138. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  4139. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  4140. sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, true, false, so_locked);
  4141. } else {
  4142. sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, true, false, so_locked);
  4143. }
  4144. break;
  4145. case SCTP_NOTIFY_ASSOC_TIMEDOUT:
  4146. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  4147. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  4148. sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, false, true, so_locked);
  4149. } else {
  4150. sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, false, true, so_locked);
  4151. }
  4152. break;
  4153. case SCTP_NOTIFY_ASSOC_RESTART:
  4154. sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, false, false, so_locked);
  4155. if (stcb->asoc.auth_supported == 0) {
  4156. sctp_ulp_notify(SCTP_NOTIFY_NO_PEER_AUTH, stcb, 0,
  4157. NULL, so_locked);
  4158. }
  4159. break;
  4160. case SCTP_NOTIFY_STR_RESET_SEND:
  4161. sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN);
  4162. break;
  4163. case SCTP_NOTIFY_STR_RESET_RECV:
  4164. sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING);
  4165. break;
  4166. case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
  4167. sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
  4168. (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_FAILED));
  4169. break;
  4170. case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
  4171. sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
  4172. (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_DENIED));
  4173. break;
  4174. case SCTP_NOTIFY_STR_RESET_FAILED_IN:
  4175. sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
  4176. (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_FAILED));
  4177. break;
  4178. case SCTP_NOTIFY_STR_RESET_DENIED_IN:
  4179. sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
  4180. (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_DENIED));
  4181. break;
  4182. case SCTP_NOTIFY_ASCONF_ADD_IP:
  4183. sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
  4184. error, so_locked);
  4185. break;
  4186. case SCTP_NOTIFY_ASCONF_DELETE_IP:
  4187. sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
  4188. error, so_locked);
  4189. break;
  4190. case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
  4191. sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
  4192. error, so_locked);
  4193. break;
  4194. case SCTP_NOTIFY_PEER_SHUTDOWN:
  4195. sctp_notify_shutdown_event(stcb);
  4196. break;
  4197. case SCTP_NOTIFY_AUTH_NEW_KEY:
  4198. sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, error,
  4199. (uint16_t)(uintptr_t)data,
  4200. so_locked);
  4201. break;
  4202. case SCTP_NOTIFY_AUTH_FREE_KEY:
  4203. sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY, error,
  4204. (uint16_t)(uintptr_t)data,
  4205. so_locked);
  4206. break;
  4207. case SCTP_NOTIFY_NO_PEER_AUTH:
  4208. sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, error,
  4209. (uint16_t)(uintptr_t)data,
  4210. so_locked);
  4211. break;
  4212. case SCTP_NOTIFY_SENDER_DRY:
  4213. sctp_notify_sender_dry_event(stcb, so_locked);
  4214. break;
  4215. case SCTP_NOTIFY_REMOTE_ERROR:
  4216. sctp_notify_remote_error(stcb, error, data);
  4217. break;
  4218. default:
  4219. SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
  4220. __func__, notification, notification);
  4221. break;
  4222. } /* end switch */
  4223. }
  4224. void
  4225. sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int so_locked)
  4226. {
  4227. struct sctp_association *asoc;
  4228. struct sctp_stream_out *outs;
  4229. struct sctp_tmit_chunk *chk, *nchk;
  4230. struct sctp_stream_queue_pending *sp, *nsp;
  4231. int i;
  4232. if (stcb == NULL) {
  4233. return;
  4234. }
  4235. asoc = &stcb->asoc;
  4236. if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  4237. /* already being freed */
  4238. return;
  4239. }
  4240. #if defined(__APPLE__) && !defined(__Userspace__)
  4241. if (so_locked) {
  4242. sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
  4243. } else {
  4244. sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
  4245. }
  4246. #endif
  4247. if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  4248. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  4249. (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
  4250. return;
  4251. }
  4252. /* now through all the gunk freeing chunks */
  4253. /* sent queue SHOULD be empty */
  4254. TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
  4255. TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
  4256. asoc->sent_queue_cnt--;
  4257. if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
  4258. if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
  4259. asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
  4260. #ifdef INVARIANTS
  4261. } else {
  4262. panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
  4263. #endif
  4264. }
  4265. }
  4266. if (chk->data != NULL) {
  4267. sctp_free_bufspace(stcb, asoc, chk, 1);
  4268. sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
  4269. error, chk, so_locked);
  4270. if (chk->data) {
  4271. sctp_m_freem(chk->data);
  4272. chk->data = NULL;
  4273. }
  4274. }
  4275. sctp_free_a_chunk(stcb, chk, so_locked);
  4276. /*sa_ignore FREED_MEMORY*/
  4277. }
  4278. /* pending send queue SHOULD be empty */
  4279. TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
  4280. TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
  4281. asoc->send_queue_cnt--;
  4282. if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
  4283. asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
  4284. #ifdef INVARIANTS
  4285. } else {
  4286. panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
  4287. #endif
  4288. }
  4289. if (chk->data != NULL) {
  4290. sctp_free_bufspace(stcb, asoc, chk, 1);
  4291. sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
  4292. error, chk, so_locked);
  4293. if (chk->data) {
  4294. sctp_m_freem(chk->data);
  4295. chk->data = NULL;
  4296. }
  4297. }
  4298. sctp_free_a_chunk(stcb, chk, so_locked);
  4299. /*sa_ignore FREED_MEMORY*/
  4300. }
  4301. for (i = 0; i < asoc->streamoutcnt; i++) {
  4302. /* For each stream */
  4303. outs = &asoc->strmout[i];
  4304. /* clean up any sends there */
  4305. TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
  4306. atomic_subtract_int(&asoc->stream_queue_cnt, 1);
  4307. TAILQ_REMOVE(&outs->outqueue, sp, next);
  4308. stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
  4309. sctp_free_spbufspace(stcb, asoc, sp);
  4310. if (sp->data) {
  4311. sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
  4312. error, (void *)sp, so_locked);
  4313. if (sp->data) {
  4314. sctp_m_freem(sp->data);
  4315. sp->data = NULL;
  4316. sp->tail_mbuf = NULL;
  4317. sp->length = 0;
  4318. }
  4319. }
  4320. if (sp->net) {
  4321. sctp_free_remote_addr(sp->net);
  4322. sp->net = NULL;
  4323. }
  4324. /* Free the chunk */
  4325. sctp_free_a_strmoq(stcb, sp, so_locked);
  4326. /*sa_ignore FREED_MEMORY*/
  4327. }
  4328. }
  4329. }
  4330. void
  4331. sctp_abort_notification(struct sctp_tcb *stcb, bool from_peer, bool timeout,
  4332. uint16_t error, struct sctp_abort_chunk *abort,
  4333. int so_locked)
  4334. {
  4335. if (stcb == NULL) {
  4336. return;
  4337. }
  4338. #if defined(__APPLE__) && !defined(__Userspace__)
  4339. if (so_locked) {
  4340. sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
  4341. } else {
  4342. sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
  4343. }
  4344. #endif
  4345. SCTP_TCB_LOCK_ASSERT(stcb);
  4346. if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
  4347. ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
  4348. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
  4349. sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_WAS_ABORTED);
  4350. }
  4351. if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  4352. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  4353. (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
  4354. return;
  4355. }
  4356. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
  4357. /* Tell them we lost the asoc */
  4358. sctp_report_all_outbound(stcb, error, so_locked);
  4359. if (from_peer) {
  4360. sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
  4361. } else {
  4362. if (timeout) {
  4363. sctp_ulp_notify(SCTP_NOTIFY_ASSOC_TIMEDOUT, stcb, error, abort, so_locked);
  4364. } else {
  4365. sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
  4366. }
  4367. }
  4368. }
  4369. void
  4370. sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  4371. struct mbuf *m, int iphlen,
  4372. struct sockaddr *src, struct sockaddr *dst,
  4373. struct sctphdr *sh, struct mbuf *op_err,
  4374. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4375. uint8_t mflowtype, uint32_t mflowid,
  4376. #endif
  4377. uint32_t vrf_id, uint16_t port)
  4378. {
  4379. #if defined(__APPLE__) && !defined(__Userspace__)
  4380. struct socket *so;
  4381. #endif
  4382. struct sctp_gen_error_cause* cause;
  4383. uint32_t vtag;
  4384. uint16_t cause_code;
  4385. if (stcb != NULL) {
  4386. vtag = stcb->asoc.peer_vtag;
  4387. vrf_id = stcb->asoc.vrf_id;
  4388. if (op_err != NULL) {
  4389. /* Read the cause code from the error cause. */
  4390. cause = mtod(op_err, struct sctp_gen_error_cause *);
  4391. cause_code = ntohs(cause->code);
  4392. } else {
  4393. cause_code = 0;
  4394. }
  4395. } else {
  4396. vtag = 0;
  4397. }
  4398. sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
  4399. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4400. mflowtype, mflowid, inp->fibnum,
  4401. #endif
  4402. vrf_id, port);
  4403. if (stcb != NULL) {
  4404. /* We have a TCB to abort, send notification too */
  4405. sctp_abort_notification(stcb, false, false, cause_code, NULL, SCTP_SO_NOT_LOCKED);
  4406. /* Ok, now lets free it */
  4407. #if defined(__APPLE__) && !defined(__Userspace__)
  4408. so = SCTP_INP_SO(inp);
  4409. atomic_add_int(&stcb->asoc.refcnt, 1);
  4410. SCTP_TCB_UNLOCK(stcb);
  4411. SCTP_SOCKET_LOCK(so, 1);
  4412. SCTP_TCB_LOCK(stcb);
  4413. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  4414. #endif
  4415. SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  4416. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  4417. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  4418. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  4419. }
  4420. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  4421. SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
  4422. #if defined(__APPLE__) && !defined(__Userspace__)
  4423. SCTP_SOCKET_UNLOCK(so, 1);
  4424. #endif
  4425. }
  4426. }
  4427. #ifdef SCTP_ASOCLOG_OF_TSNS
  4428. void
  4429. sctp_print_out_track_log(struct sctp_tcb *stcb)
  4430. {
  4431. #ifdef NOSIY_PRINTS
  4432. int i;
  4433. SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
  4434. SCTP_PRINTF("IN bound TSN log-aaa\n");
  4435. if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
  4436. SCTP_PRINTF("None rcvd\n");
  4437. goto none_in;
  4438. }
  4439. if (stcb->asoc.tsn_in_wrapped) {
  4440. for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
  4441. SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
  4442. stcb->asoc.in_tsnlog[i].tsn,
  4443. stcb->asoc.in_tsnlog[i].strm,
  4444. stcb->asoc.in_tsnlog[i].seq,
  4445. stcb->asoc.in_tsnlog[i].flgs,
  4446. stcb->asoc.in_tsnlog[i].sz);
  4447. }
  4448. }
  4449. if (stcb->asoc.tsn_in_at) {
  4450. for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
  4451. SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
  4452. stcb->asoc.in_tsnlog[i].tsn,
  4453. stcb->asoc.in_tsnlog[i].strm,
  4454. stcb->asoc.in_tsnlog[i].seq,
  4455. stcb->asoc.in_tsnlog[i].flgs,
  4456. stcb->asoc.in_tsnlog[i].sz);
  4457. }
  4458. }
  4459. none_in:
  4460. SCTP_PRINTF("OUT bound TSN log-aaa\n");
  4461. if ((stcb->asoc.tsn_out_at == 0) &&
  4462. (stcb->asoc.tsn_out_wrapped == 0)) {
  4463. SCTP_PRINTF("None sent\n");
  4464. }
  4465. if (stcb->asoc.tsn_out_wrapped) {
  4466. for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
  4467. SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
  4468. stcb->asoc.out_tsnlog[i].tsn,
  4469. stcb->asoc.out_tsnlog[i].strm,
  4470. stcb->asoc.out_tsnlog[i].seq,
  4471. stcb->asoc.out_tsnlog[i].flgs,
  4472. stcb->asoc.out_tsnlog[i].sz);
  4473. }
  4474. }
  4475. if (stcb->asoc.tsn_out_at) {
  4476. for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
  4477. SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
  4478. stcb->asoc.out_tsnlog[i].tsn,
  4479. stcb->asoc.out_tsnlog[i].strm,
  4480. stcb->asoc.out_tsnlog[i].seq,
  4481. stcb->asoc.out_tsnlog[i].flgs,
  4482. stcb->asoc.out_tsnlog[i].sz);
  4483. }
  4484. }
  4485. #endif
  4486. }
  4487. #endif
  4488. void
  4489. sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  4490. struct mbuf *op_err, bool timedout, int so_locked)
  4491. {
  4492. #if defined(__APPLE__) && !defined(__Userspace__)
  4493. struct socket *so;
  4494. #endif
  4495. struct sctp_gen_error_cause* cause;
  4496. uint16_t cause_code;
  4497. #if defined(__APPLE__) && !defined(__Userspace__)
  4498. so = SCTP_INP_SO(inp);
  4499. #endif
  4500. #if defined(__APPLE__) && !defined(__Userspace__)
  4501. if (so_locked) {
  4502. sctp_lock_assert(SCTP_INP_SO(inp));
  4503. } else {
  4504. sctp_unlock_assert(SCTP_INP_SO(inp));
  4505. }
  4506. #endif
  4507. if (stcb == NULL) {
  4508. /* Got to have a TCB */
  4509. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  4510. if (LIST_EMPTY(&inp->sctp_asoc_list)) {
  4511. #if defined(__APPLE__) && !defined(__Userspace__)
  4512. if (!so_locked) {
  4513. SCTP_SOCKET_LOCK(so, 1);
  4514. }
  4515. #endif
  4516. sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  4517. SCTP_CALLED_DIRECTLY_NOCMPSET);
  4518. #if defined(__APPLE__) && !defined(__Userspace__)
  4519. if (!so_locked) {
  4520. SCTP_SOCKET_UNLOCK(so, 1);
  4521. }
  4522. #endif
  4523. }
  4524. }
  4525. return;
  4526. }
  4527. if (op_err != NULL) {
  4528. /* Read the cause code from the error cause. */
  4529. cause = mtod(op_err, struct sctp_gen_error_cause *);
  4530. cause_code = ntohs(cause->code);
  4531. } else {
  4532. cause_code = 0;
  4533. }
  4534. /* notify the peer */
  4535. sctp_send_abort_tcb(stcb, op_err, so_locked);
  4536. SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  4537. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  4538. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  4539. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  4540. }
  4541. /* notify the ulp */
  4542. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
  4543. sctp_abort_notification(stcb, false, timedout, cause_code, NULL, so_locked);
  4544. }
  4545. /* now free the asoc */
  4546. #ifdef SCTP_ASOCLOG_OF_TSNS
  4547. sctp_print_out_track_log(stcb);
  4548. #endif
  4549. #if defined(__APPLE__) && !defined(__Userspace__)
  4550. if (!so_locked) {
  4551. atomic_add_int(&stcb->asoc.refcnt, 1);
  4552. SCTP_TCB_UNLOCK(stcb);
  4553. SCTP_SOCKET_LOCK(so, 1);
  4554. SCTP_TCB_LOCK(stcb);
  4555. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  4556. }
  4557. #endif
  4558. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  4559. SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
  4560. #if defined(__APPLE__) && !defined(__Userspace__)
  4561. if (!so_locked) {
  4562. SCTP_SOCKET_UNLOCK(so, 1);
  4563. }
  4564. #endif
  4565. }
  4566. void
  4567. sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
  4568. struct sockaddr *src, struct sockaddr *dst,
  4569. struct sctphdr *sh, struct sctp_inpcb *inp,
  4570. struct mbuf *cause,
  4571. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4572. uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
  4573. #endif
  4574. uint32_t vrf_id, uint16_t port)
  4575. {
  4576. struct sctp_chunkhdr *ch, chunk_buf;
  4577. unsigned int chk_length;
  4578. int contains_init_chunk;
  4579. SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
  4580. /* Generate a TO address for future reference */
  4581. if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
  4582. if (LIST_EMPTY(&inp->sctp_asoc_list)) {
  4583. #if defined(__APPLE__) && !defined(__Userspace__)
  4584. SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
  4585. #endif
  4586. sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
  4587. SCTP_CALLED_DIRECTLY_NOCMPSET);
  4588. #if defined(__APPLE__) && !defined(__Userspace__)
  4589. SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
  4590. #endif
  4591. }
  4592. }
  4593. contains_init_chunk = 0;
  4594. ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
  4595. sizeof(*ch), (uint8_t *) & chunk_buf);
  4596. while (ch != NULL) {
  4597. chk_length = ntohs(ch->chunk_length);
  4598. if (chk_length < sizeof(*ch)) {
  4599. /* break to abort land */
  4600. break;
  4601. }
  4602. switch (ch->chunk_type) {
  4603. case SCTP_INIT:
  4604. contains_init_chunk = 1;
  4605. break;
  4606. case SCTP_PACKET_DROPPED:
  4607. /* we don't respond to pkt-dropped */
  4608. return;
  4609. case SCTP_ABORT_ASSOCIATION:
  4610. /* we don't respond with an ABORT to an ABORT */
  4611. return;
  4612. case SCTP_SHUTDOWN_COMPLETE:
  4613. /*
  4614. * we ignore it since we are not waiting for it and
  4615. * peer is gone
  4616. */
  4617. return;
  4618. case SCTP_SHUTDOWN_ACK:
  4619. sctp_send_shutdown_complete2(src, dst, sh,
  4620. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4621. mflowtype, mflowid, fibnum,
  4622. #endif
  4623. vrf_id, port);
  4624. return;
  4625. default:
  4626. break;
  4627. }
  4628. offset += SCTP_SIZE32(chk_length);
  4629. ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
  4630. sizeof(*ch), (uint8_t *) & chunk_buf);
  4631. }
  4632. if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
  4633. ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
  4634. (contains_init_chunk == 0))) {
  4635. sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
  4636. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4637. mflowtype, mflowid, fibnum,
  4638. #endif
  4639. vrf_id, port);
  4640. }
  4641. }
  4642. /*
  4643. * check the inbound datagram to make sure there is not an abort inside it,
  4644. * if there is return 1, else return 0.
  4645. */
  4646. int
  4647. sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtag)
  4648. {
  4649. struct sctp_chunkhdr *ch;
  4650. struct sctp_init_chunk *init_chk, chunk_buf;
  4651. int offset;
  4652. unsigned int chk_length;
  4653. offset = iphlen + sizeof(struct sctphdr);
  4654. ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
  4655. (uint8_t *) & chunk_buf);
  4656. while (ch != NULL) {
  4657. chk_length = ntohs(ch->chunk_length);
  4658. if (chk_length < sizeof(*ch)) {
  4659. /* packet is probably corrupt */
  4660. break;
  4661. }
  4662. /* we seem to be ok, is it an abort? */
  4663. if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
  4664. /* yep, tell them */
  4665. return (1);
  4666. }
  4667. if ((ch->chunk_type == SCTP_INITIATION) ||
  4668. (ch->chunk_type == SCTP_INITIATION_ACK)) {
  4669. /* need to update the Vtag */
  4670. init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
  4671. offset, sizeof(struct sctp_init_chunk), (uint8_t *) & chunk_buf);
  4672. if (init_chk != NULL) {
  4673. *vtag = ntohl(init_chk->init.initiate_tag);
  4674. }
  4675. }
  4676. /* Nope, move to the next chunk */
  4677. offset += SCTP_SIZE32(chk_length);
  4678. ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
  4679. sizeof(*ch), (uint8_t *) & chunk_buf);
  4680. }
  4681. return (0);
  4682. }
  4683. /*
  4684. * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
  4685. * set (i.e. it's 0) so, create this function to compare link local scopes
  4686. */
  4687. #ifdef INET6
  4688. uint32_t
  4689. sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
  4690. {
  4691. #if defined(__Userspace__)
  4692. /*__Userspace__ Returning 1 here always */
  4693. #endif
  4694. #if defined(SCTP_EMBEDDED_V6_SCOPE)
  4695. struct sockaddr_in6 a, b;
  4696. /* save copies */
  4697. a = *addr1;
  4698. b = *addr2;
  4699. if (a.sin6_scope_id == 0)
  4700. #ifdef SCTP_KAME
  4701. if (sa6_recoverscope(&a)) {
  4702. #else
  4703. if (in6_recoverscope(&a, &a.sin6_addr, NULL)) {
  4704. #endif /* SCTP_KAME */
  4705. /* can't get scope, so can't match */
  4706. return (0);
  4707. }
  4708. if (b.sin6_scope_id == 0)
  4709. #ifdef SCTP_KAME
  4710. if (sa6_recoverscope(&b)) {
  4711. #else
  4712. if (in6_recoverscope(&b, &b.sin6_addr, NULL)) {
  4713. #endif /* SCTP_KAME */
  4714. /* can't get scope, so can't match */
  4715. return (0);
  4716. }
  4717. if (a.sin6_scope_id != b.sin6_scope_id)
  4718. return (0);
  4719. #else
  4720. if (addr1->sin6_scope_id != addr2->sin6_scope_id)
  4721. return (0);
  4722. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4723. return (1);
  4724. }
  4725. #if defined(SCTP_EMBEDDED_V6_SCOPE)
  4726. /*
  4727. * returns a sockaddr_in6 with embedded scope recovered and removed
  4728. */
  4729. struct sockaddr_in6 *
  4730. sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
  4731. {
  4732. /* check and strip embedded scope junk */
  4733. if (addr->sin6_family == AF_INET6) {
  4734. if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
  4735. if (addr->sin6_scope_id == 0) {
  4736. *store = *addr;
  4737. #ifdef SCTP_KAME
  4738. if (!sa6_recoverscope(store)) {
  4739. #else
  4740. if (!in6_recoverscope(store, &store->sin6_addr,
  4741. NULL)) {
  4742. #endif /* SCTP_KAME */
  4743. /* use the recovered scope */
  4744. addr = store;
  4745. }
  4746. } else {
  4747. /* else, return the original "to" addr */
  4748. in6_clearscope(&addr->sin6_addr);
  4749. }
  4750. }
  4751. }
  4752. return (addr);
  4753. }
  4754. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4755. #endif
  4756. /*
  4757. * are the two addresses the same? currently a "scopeless" check returns: 1
  4758. * if same, 0 if not
  4759. */
  4760. int
  4761. sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
  4762. {
  4763. /* must be valid */
  4764. if (sa1 == NULL || sa2 == NULL)
  4765. return (0);
  4766. /* must be the same family */
  4767. if (sa1->sa_family != sa2->sa_family)
  4768. return (0);
  4769. switch (sa1->sa_family) {
  4770. #ifdef INET6
  4771. case AF_INET6:
  4772. {
  4773. /* IPv6 addresses */
  4774. struct sockaddr_in6 *sin6_1, *sin6_2;
  4775. sin6_1 = (struct sockaddr_in6 *)sa1;
  4776. sin6_2 = (struct sockaddr_in6 *)sa2;
  4777. return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
  4778. sin6_2));
  4779. }
  4780. #endif
  4781. #ifdef INET
  4782. case AF_INET:
  4783. {
  4784. /* IPv4 addresses */
  4785. struct sockaddr_in *sin_1, *sin_2;
  4786. sin_1 = (struct sockaddr_in *)sa1;
  4787. sin_2 = (struct sockaddr_in *)sa2;
  4788. return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
  4789. }
  4790. #endif
  4791. #if defined(__Userspace__)
  4792. case AF_CONN:
  4793. {
  4794. struct sockaddr_conn *sconn_1, *sconn_2;
  4795. sconn_1 = (struct sockaddr_conn *)sa1;
  4796. sconn_2 = (struct sockaddr_conn *)sa2;
  4797. return (sconn_1->sconn_addr == sconn_2->sconn_addr);
  4798. }
  4799. #endif
  4800. default:
  4801. /* we don't do these... */
  4802. return (0);
  4803. }
  4804. }
  4805. void
  4806. sctp_print_address(struct sockaddr *sa)
  4807. {
  4808. #ifdef INET6
  4809. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4810. char ip6buf[INET6_ADDRSTRLEN];
  4811. #endif
  4812. #endif
  4813. switch (sa->sa_family) {
  4814. #ifdef INET6
  4815. case AF_INET6:
  4816. {
  4817. struct sockaddr_in6 *sin6;
  4818. sin6 = (struct sockaddr_in6 *)sa;
  4819. #if defined(__Userspace__)
  4820. SCTP_PRINTF("IPv6 address: %x:%x:%x:%x:%x:%x:%x:%x:port:%d scope:%u\n",
  4821. ntohs(sin6->sin6_addr.s6_addr16[0]),
  4822. ntohs(sin6->sin6_addr.s6_addr16[1]),
  4823. ntohs(sin6->sin6_addr.s6_addr16[2]),
  4824. ntohs(sin6->sin6_addr.s6_addr16[3]),
  4825. ntohs(sin6->sin6_addr.s6_addr16[4]),
  4826. ntohs(sin6->sin6_addr.s6_addr16[5]),
  4827. ntohs(sin6->sin6_addr.s6_addr16[6]),
  4828. ntohs(sin6->sin6_addr.s6_addr16[7]),
  4829. ntohs(sin6->sin6_port),
  4830. sin6->sin6_scope_id);
  4831. #else
  4832. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4833. SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
  4834. ip6_sprintf(ip6buf, &sin6->sin6_addr),
  4835. ntohs(sin6->sin6_port),
  4836. sin6->sin6_scope_id);
  4837. #else
  4838. SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
  4839. ip6_sprintf(&sin6->sin6_addr),
  4840. ntohs(sin6->sin6_port),
  4841. sin6->sin6_scope_id);
  4842. #endif
  4843. #endif
  4844. break;
  4845. }
  4846. #endif
  4847. #ifdef INET
  4848. case AF_INET:
  4849. {
  4850. struct sockaddr_in *sin;
  4851. unsigned char *p;
  4852. sin = (struct sockaddr_in *)sa;
  4853. p = (unsigned char *)&sin->sin_addr;
  4854. SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
  4855. p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
  4856. break;
  4857. }
  4858. #endif
  4859. #if defined(__Userspace__)
  4860. case AF_CONN:
  4861. {
  4862. struct sockaddr_conn *sconn;
  4863. sconn = (struct sockaddr_conn *)sa;
  4864. SCTP_PRINTF("AF_CONN address: %p\n", sconn->sconn_addr);
  4865. break;
  4866. }
  4867. #endif
  4868. default:
  4869. SCTP_PRINTF("?\n");
  4870. break;
  4871. }
  4872. }
  4873. void
  4874. sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
  4875. struct sctp_inpcb *new_inp,
  4876. struct sctp_tcb *stcb,
  4877. int waitflags)
  4878. {
  4879. /*
  4880. * go through our old INP and pull off any control structures that
  4881. * belong to stcb and move then to the new inp.
  4882. */
  4883. struct socket *old_so, *new_so;
  4884. struct sctp_queued_to_read *control, *nctl;
  4885. struct sctp_readhead tmp_queue;
  4886. struct mbuf *m;
  4887. #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
  4888. int error = 0;
  4889. #endif
  4890. old_so = old_inp->sctp_socket;
  4891. new_so = new_inp->sctp_socket;
  4892. TAILQ_INIT(&tmp_queue);
  4893. #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
  4894. #if defined(__FreeBSD__)
  4895. error = SOCK_IO_RECV_LOCK(old_so, waitflags);
  4896. #else
  4897. error = sblock(&old_so->so_rcv, waitflags);
  4898. #endif
  4899. if (error) {
  4900. /* Gak, can't get I/O lock, we have a problem.
  4901. * data will be left stranded.. and we
  4902. * don't dare look at it since the
  4903. * other thread may be reading something.
  4904. * Oh well, its a screwed up app that does
  4905. * a peeloff OR a accept while reading
  4906. * from the main socket... actually its
  4907. * only the peeloff() case, since I think
  4908. * read will fail on a listening socket..
  4909. */
  4910. return;
  4911. }
  4912. #endif
  4913. /* lock the socket buffers */
  4914. SCTP_INP_READ_LOCK(old_inp);
  4915. TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
  4916. /* Pull off all for out target stcb */
  4917. if (control->stcb == stcb) {
  4918. /* remove it we want it */
  4919. TAILQ_REMOVE(&old_inp->read_queue, control, next);
  4920. TAILQ_INSERT_TAIL(&tmp_queue, control, next);
  4921. m = control->data;
  4922. while (m) {
  4923. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  4924. sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE,SCTP_BUF_LEN(m));
  4925. }
  4926. sctp_sbfree(control, stcb, &old_so->so_rcv, m);
  4927. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  4928. sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
  4929. }
  4930. m = SCTP_BUF_NEXT(m);
  4931. }
  4932. }
  4933. }
  4934. SCTP_INP_READ_UNLOCK(old_inp);
  4935. /* Remove the recv-lock on the old socket */
  4936. #if defined(__APPLE__) && !defined(__Userspace__)
  4937. sbunlock(&old_so->so_rcv, 1);
  4938. #endif
  4939. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4940. SOCK_IO_RECV_UNLOCK(old_so);
  4941. #endif
  4942. /* Now we move them over to the new socket buffer */
  4943. SCTP_INP_READ_LOCK(new_inp);
  4944. TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
  4945. TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
  4946. m = control->data;
  4947. while (m) {
  4948. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  4949. sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
  4950. }
  4951. sctp_sballoc(stcb, &new_so->so_rcv, m);
  4952. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  4953. sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
  4954. }
  4955. m = SCTP_BUF_NEXT(m);
  4956. }
  4957. }
  4958. SCTP_INP_READ_UNLOCK(new_inp);
  4959. }
  4960. void
  4961. sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
  4962. struct sctp_tcb *stcb,
  4963. int so_locked
  4964. #if !(defined(__APPLE__) && !defined(__Userspace__))
  4965. SCTP_UNUSED
  4966. #endif
  4967. )
  4968. {
  4969. if ((inp != NULL) &&
  4970. (inp->sctp_socket != NULL) &&
  4971. (((inp->sctp_flags & (SCTP_PCB_FLAGS_TCPTYPE | SCTP_PCB_FLAGS_IN_TCPPOOL)) == 0) ||
  4972. !SCTP_IS_LISTENING(inp))) {
  4973. #if defined(__APPLE__) && !defined(__Userspace__)
  4974. struct socket *so;
  4975. so = SCTP_INP_SO(inp);
  4976. if (!so_locked) {
  4977. if (stcb) {
  4978. atomic_add_int(&stcb->asoc.refcnt, 1);
  4979. SCTP_TCB_UNLOCK(stcb);
  4980. }
  4981. SCTP_SOCKET_LOCK(so, 1);
  4982. if (stcb) {
  4983. SCTP_TCB_LOCK(stcb);
  4984. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  4985. }
  4986. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  4987. SCTP_SOCKET_UNLOCK(so, 1);
  4988. return;
  4989. }
  4990. }
  4991. #endif
  4992. sctp_sorwakeup(inp, inp->sctp_socket);
  4993. #if defined(__APPLE__) && !defined(__Userspace__)
  4994. if (!so_locked) {
  4995. SCTP_SOCKET_UNLOCK(so, 1);
  4996. }
  4997. #endif
  4998. }
  4999. }
  5000. #if defined(__Userspace__)
  5001. void
  5002. sctp_invoke_recv_callback(struct sctp_inpcb *inp,
  5003. struct sctp_tcb *stcb,
  5004. struct sctp_queued_to_read *control,
  5005. int inp_read_lock_held)
  5006. {
  5007. uint32_t pd_point, length;
  5008. if ((inp->recv_callback == NULL) ||
  5009. (stcb == NULL) ||
  5010. (stcb->sctp_socket == NULL)) {
  5011. return;
  5012. }
  5013. length = control->length;
  5014. if (stcb != NULL && stcb->sctp_socket != NULL) {
  5015. pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
  5016. stcb->sctp_ep->partial_delivery_point);
  5017. } else {
  5018. pd_point = inp->partial_delivery_point;
  5019. }
  5020. if ((control->end_added == 1) || (length >= pd_point)) {
  5021. struct socket *so;
  5022. struct mbuf *m;
  5023. char *buffer;
  5024. struct sctp_rcvinfo rcv;
  5025. union sctp_sockstore addr;
  5026. int flags;
  5027. if ((buffer = malloc(length)) == NULL) {
  5028. return;
  5029. }
  5030. if (inp_read_lock_held == 0) {
  5031. SCTP_INP_READ_LOCK(inp);
  5032. }
  5033. so = stcb->sctp_socket;
  5034. for (m = control->data; m; m = SCTP_BUF_NEXT(m)) {
  5035. sctp_sbfree(control, control->stcb, &so->so_rcv, m);
  5036. }
  5037. m_copydata(control->data, 0, length, buffer);
  5038. memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
  5039. rcv.rcv_sid = control->sinfo_stream;
  5040. rcv.rcv_ssn = (uint16_t)control->mid;
  5041. rcv.rcv_flags = control->sinfo_flags;
  5042. rcv.rcv_ppid = control->sinfo_ppid;
  5043. rcv.rcv_tsn = control->sinfo_tsn;
  5044. rcv.rcv_cumtsn = control->sinfo_cumtsn;
  5045. rcv.rcv_context = control->sinfo_context;
  5046. rcv.rcv_assoc_id = control->sinfo_assoc_id;
  5047. memset(&addr, 0, sizeof(union sctp_sockstore));
  5048. switch (control->whoFrom->ro._l_addr.sa.sa_family) {
  5049. #ifdef INET
  5050. case AF_INET:
  5051. addr.sin = control->whoFrom->ro._l_addr.sin;
  5052. break;
  5053. #endif
  5054. #ifdef INET6
  5055. case AF_INET6:
  5056. addr.sin6 = control->whoFrom->ro._l_addr.sin6;
  5057. break;
  5058. #endif
  5059. case AF_CONN:
  5060. addr.sconn = control->whoFrom->ro._l_addr.sconn;
  5061. break;
  5062. default:
  5063. addr.sa = control->whoFrom->ro._l_addr.sa;
  5064. break;
  5065. }
  5066. flags = 0;
  5067. if (control->end_added == 1) {
  5068. flags |= MSG_EOR;
  5069. }
  5070. if (control->spec_flags & M_NOTIFICATION) {
  5071. flags |= MSG_NOTIFICATION;
  5072. }
  5073. sctp_m_freem(control->data);
  5074. control->data = NULL;
  5075. control->tail_mbuf = NULL;
  5076. control->length = 0;
  5077. if (control->end_added) {
  5078. TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
  5079. control->on_read_q = 0;
  5080. sctp_free_remote_addr(control->whoFrom);
  5081. control->whoFrom = NULL;
  5082. sctp_free_a_readq(stcb, control);
  5083. }
  5084. atomic_add_int(&stcb->asoc.refcnt, 1);
  5085. SCTP_TCB_UNLOCK(stcb);
  5086. if (inp_read_lock_held == 0) {
  5087. SCTP_INP_READ_UNLOCK(inp);
  5088. }
  5089. inp->recv_callback(so, addr, buffer, length, rcv, flags, inp->ulp_info);
  5090. SCTP_TCB_LOCK(stcb);
  5091. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  5092. }
  5093. }
  5094. #endif
  5095. void
  5096. sctp_add_to_readq(struct sctp_inpcb *inp,
  5097. struct sctp_tcb *stcb,
  5098. struct sctp_queued_to_read *control,
  5099. struct sockbuf *sb,
  5100. int end,
  5101. int inp_read_lock_held,
  5102. int so_locked)
  5103. {
  5104. /*
  5105. * Here we must place the control on the end of the socket read
  5106. * queue AND increment sb_cc so that select will work properly on
  5107. * read.
  5108. */
  5109. struct mbuf *m, *prev = NULL;
  5110. if (inp == NULL) {
  5111. /* Gak, TSNH!! */
  5112. #ifdef INVARIANTS
  5113. panic("Gak, inp NULL on add_to_readq");
  5114. #endif
  5115. return;
  5116. }
  5117. #if defined(__APPLE__) && !defined(__Userspace__)
  5118. if (so_locked) {
  5119. sctp_lock_assert(SCTP_INP_SO(inp));
  5120. } else {
  5121. sctp_unlock_assert(SCTP_INP_SO(inp));
  5122. }
  5123. #endif
  5124. if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
  5125. SCTP_INP_READ_LOCK(inp);
  5126. }
  5127. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
  5128. if (!control->on_strm_q) {
  5129. sctp_free_remote_addr(control->whoFrom);
  5130. if (control->data) {
  5131. sctp_m_freem(control->data);
  5132. control->data = NULL;
  5133. }
  5134. sctp_free_a_readq(stcb, control);
  5135. }
  5136. if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
  5137. SCTP_INP_READ_UNLOCK(inp);
  5138. }
  5139. return;
  5140. }
  5141. if ((control->spec_flags & M_NOTIFICATION) == 0) {
  5142. atomic_add_int(&inp->total_recvs, 1);
  5143. if (!control->do_not_ref_stcb) {
  5144. atomic_add_int(&stcb->total_recvs, 1);
  5145. }
  5146. }
  5147. m = control->data;
  5148. control->held_length = 0;
  5149. control->length = 0;
  5150. while (m != NULL) {
  5151. if (SCTP_BUF_LEN(m) == 0) {
  5152. /* Skip mbufs with NO length */
  5153. if (prev == NULL) {
  5154. /* First one */
  5155. control->data = sctp_m_free(m);
  5156. m = control->data;
  5157. } else {
  5158. SCTP_BUF_NEXT(prev) = sctp_m_free(m);
  5159. m = SCTP_BUF_NEXT(prev);
  5160. }
  5161. if (m == NULL) {
  5162. control->tail_mbuf = prev;
  5163. }
  5164. continue;
  5165. }
  5166. prev = m;
  5167. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  5168. sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
  5169. }
  5170. sctp_sballoc(stcb, sb, m);
  5171. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  5172. sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
  5173. }
  5174. atomic_add_int(&control->length, SCTP_BUF_LEN(m));
  5175. m = SCTP_BUF_NEXT(m);
  5176. }
  5177. if (prev != NULL) {
  5178. control->tail_mbuf = prev;
  5179. } else {
  5180. /* Everything got collapsed out?? */
  5181. if (!control->on_strm_q) {
  5182. sctp_free_remote_addr(control->whoFrom);
  5183. sctp_free_a_readq(stcb, control);
  5184. }
  5185. if (inp_read_lock_held == 0)
  5186. SCTP_INP_READ_UNLOCK(inp);
  5187. return;
  5188. }
  5189. if (end) {
  5190. control->end_added = 1;
  5191. }
  5192. TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
  5193. control->on_read_q = 1;
  5194. #if defined(__Userspace__)
  5195. sctp_invoke_recv_callback(inp, stcb, control, SCTP_READ_LOCK_HELD);
  5196. #endif
  5197. if ((inp != NULL) && (inp->sctp_socket != NULL)) {
  5198. sctp_wakeup_the_read_socket(inp, stcb, so_locked);
  5199. }
  5200. if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
  5201. SCTP_INP_READ_UNLOCK(inp);
  5202. }
  5203. }
  5204. /*************HOLD THIS COMMENT FOR PATCH FILE OF
  5205. *************ALTERNATE ROUTING CODE
  5206. */
  5207. /*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
  5208. *************ALTERNATE ROUTING CODE
  5209. */
  5210. struct mbuf *
  5211. sctp_generate_cause(uint16_t code, char *info)
  5212. {
  5213. struct mbuf *m;
  5214. struct sctp_gen_error_cause *cause;
  5215. size_t info_len;
  5216. uint16_t len;
  5217. if ((code == 0) || (info == NULL)) {
  5218. return (NULL);
  5219. }
  5220. info_len = strlen(info);
  5221. if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
  5222. return (NULL);
  5223. }
  5224. len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
  5225. m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
  5226. if (m != NULL) {
  5227. SCTP_BUF_LEN(m) = len;
  5228. cause = mtod(m, struct sctp_gen_error_cause *);
  5229. cause->code = htons(code);
  5230. cause->length = htons(len);
  5231. memcpy(cause->info, info, info_len);
  5232. }
  5233. return (m);
  5234. }
  5235. struct mbuf *
  5236. sctp_generate_no_user_data_cause(uint32_t tsn)
  5237. {
  5238. struct mbuf *m;
  5239. struct sctp_error_no_user_data *no_user_data_cause;
  5240. uint16_t len;
  5241. len = (uint16_t)sizeof(struct sctp_error_no_user_data);
  5242. m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
  5243. if (m != NULL) {
  5244. SCTP_BUF_LEN(m) = len;
  5245. no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
  5246. no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
  5247. no_user_data_cause->cause.length = htons(len);
  5248. no_user_data_cause->tsn = htonl(tsn);
  5249. }
  5250. return (m);
  5251. }
  5252. void
  5253. sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
  5254. struct sctp_tmit_chunk *tp1, int chk_cnt)
  5255. {
  5256. if (tp1->data == NULL) {
  5257. return;
  5258. }
  5259. atomic_subtract_int(&asoc->chunks_on_out_queue, chk_cnt);
  5260. #ifdef SCTP_MBCNT_LOGGING
  5261. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
  5262. sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
  5263. asoc->total_output_queue_size,
  5264. tp1->book_size,
  5265. 0,
  5266. tp1->mbcnt);
  5267. }
  5268. #endif
  5269. if (asoc->total_output_queue_size >= tp1->book_size) {
  5270. atomic_subtract_int(&asoc->total_output_queue_size, tp1->book_size);
  5271. } else {
  5272. asoc->total_output_queue_size = 0;
  5273. }
  5274. if ((stcb->sctp_socket != NULL) &&
  5275. (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
  5276. ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
  5277. if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) {
  5278. atomic_subtract_int(&((stcb)->sctp_socket->so_snd.sb_cc), tp1->book_size);
  5279. } else {
  5280. stcb->sctp_socket->so_snd.sb_cc = 0;
  5281. }
  5282. }
  5283. }
  5284. int
  5285. sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
  5286. uint8_t sent, int so_locked)
  5287. {
  5288. struct sctp_stream_out *strq;
  5289. struct sctp_tmit_chunk *chk = NULL, *tp2;
  5290. struct sctp_stream_queue_pending *sp;
  5291. uint32_t mid;
  5292. uint16_t sid;
  5293. uint8_t foundeom = 0;
  5294. int ret_sz = 0;
  5295. int notdone;
  5296. int do_wakeup_routine = 0;
  5297. #if defined(__APPLE__) && !defined(__Userspace__)
  5298. if (so_locked) {
  5299. sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
  5300. } else {
  5301. sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
  5302. }
  5303. #endif
  5304. SCTP_TCB_LOCK_ASSERT(stcb);
  5305. sid = tp1->rec.data.sid;
  5306. mid = tp1->rec.data.mid;
  5307. if (sent || ((tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0)) {
  5308. stcb->asoc.abandoned_sent[0]++;
  5309. stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
  5310. stcb->asoc.strmout[sid].abandoned_sent[0]++;
  5311. #if defined(SCTP_DETAILED_STR_STATS)
  5312. stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
  5313. #endif
  5314. } else {
  5315. stcb->asoc.abandoned_unsent[0]++;
  5316. stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
  5317. stcb->asoc.strmout[sid].abandoned_unsent[0]++;
  5318. #if defined(SCTP_DETAILED_STR_STATS)
  5319. stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
  5320. #endif
  5321. }
  5322. do {
  5323. ret_sz += tp1->book_size;
  5324. if (tp1->data != NULL) {
  5325. if (tp1->sent < SCTP_DATAGRAM_RESEND) {
  5326. sctp_flight_size_decrease(tp1);
  5327. sctp_total_flight_decrease(stcb, tp1);
  5328. }
  5329. sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
  5330. stcb->asoc.peers_rwnd += tp1->send_size;
  5331. stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
  5332. if (sent) {
  5333. sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
  5334. } else {
  5335. sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
  5336. }
  5337. if (tp1->data) {
  5338. sctp_m_freem(tp1->data);
  5339. tp1->data = NULL;
  5340. }
  5341. do_wakeup_routine = 1;
  5342. if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
  5343. stcb->asoc.sent_queue_cnt_removeable--;
  5344. }
  5345. }
  5346. tp1->sent = SCTP_FORWARD_TSN_SKIP;
  5347. if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
  5348. SCTP_DATA_NOT_FRAG) {
  5349. /* not frag'ed we ae done */
  5350. notdone = 0;
  5351. foundeom = 1;
  5352. } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
  5353. /* end of frag, we are done */
  5354. notdone = 0;
  5355. foundeom = 1;
  5356. } else {
  5357. /*
  5358. * Its a begin or middle piece, we must mark all of
  5359. * it
  5360. */
  5361. notdone = 1;
  5362. tp1 = TAILQ_NEXT(tp1, sctp_next);
  5363. }
  5364. } while (tp1 && notdone);
  5365. if (foundeom == 0) {
  5366. /*
  5367. * The multi-part message was scattered across the send and
  5368. * sent queue.
  5369. */
  5370. TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
  5371. if ((tp1->rec.data.sid != sid) ||
  5372. (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
  5373. break;
  5374. }
  5375. /* save to chk in case we have some on stream out
  5376. * queue. If so and we have an un-transmitted one
  5377. * we don't have to fudge the TSN.
  5378. */
  5379. chk = tp1;
  5380. ret_sz += tp1->book_size;
  5381. sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
  5382. if (sent) {
  5383. sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
  5384. } else {
  5385. sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
  5386. }
  5387. if (tp1->data) {
  5388. sctp_m_freem(tp1->data);
  5389. tp1->data = NULL;
  5390. }
  5391. /* No flight involved here book the size to 0 */
  5392. tp1->book_size = 0;
  5393. if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
  5394. foundeom = 1;
  5395. }
  5396. do_wakeup_routine = 1;
  5397. tp1->sent = SCTP_FORWARD_TSN_SKIP;
  5398. TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
  5399. /* on to the sent queue so we can wait for it to be passed by. */
  5400. TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
  5401. sctp_next);
  5402. stcb->asoc.send_queue_cnt--;
  5403. stcb->asoc.sent_queue_cnt++;
  5404. }
  5405. }
  5406. if (foundeom == 0) {
  5407. /*
  5408. * Still no eom found. That means there
  5409. * is stuff left on the stream out queue.. yuck.
  5410. */
  5411. strq = &stcb->asoc.strmout[sid];
  5412. sp = TAILQ_FIRST(&strq->outqueue);
  5413. if (sp != NULL) {
  5414. sp->discard_rest = 1;
  5415. /*
  5416. * We may need to put a chunk on the
  5417. * queue that holds the TSN that
  5418. * would have been sent with the LAST
  5419. * bit.
  5420. */
  5421. if (chk == NULL) {
  5422. /* Yep, we have to */
  5423. sctp_alloc_a_chunk(stcb, chk);
  5424. if (chk == NULL) {
  5425. /* we are hosed. All we can
  5426. * do is nothing.. which will
  5427. * cause an abort if the peer is
  5428. * paying attention.
  5429. */
  5430. goto oh_well;
  5431. }
  5432. memset(chk, 0, sizeof(*chk));
  5433. chk->rec.data.rcv_flags = 0;
  5434. chk->sent = SCTP_FORWARD_TSN_SKIP;
  5435. chk->asoc = &stcb->asoc;
  5436. if (stcb->asoc.idata_supported == 0) {
  5437. if (sp->sinfo_flags & SCTP_UNORDERED) {
  5438. chk->rec.data.mid = 0;
  5439. } else {
  5440. chk->rec.data.mid = strq->next_mid_ordered;
  5441. }
  5442. } else {
  5443. if (sp->sinfo_flags & SCTP_UNORDERED) {
  5444. chk->rec.data.mid = strq->next_mid_unordered;
  5445. } else {
  5446. chk->rec.data.mid = strq->next_mid_ordered;
  5447. }
  5448. }
  5449. chk->rec.data.sid = sp->sid;
  5450. chk->rec.data.ppid = sp->ppid;
  5451. chk->rec.data.context = sp->context;
  5452. chk->flags = sp->act_flags;
  5453. chk->whoTo = NULL;
  5454. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5455. chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
  5456. #else
  5457. chk->rec.data.tsn = stcb->asoc.sending_seq++;
  5458. #endif
  5459. strq->chunks_on_queues++;
  5460. TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
  5461. stcb->asoc.sent_queue_cnt++;
  5462. stcb->asoc.pr_sctp_cnt++;
  5463. }
  5464. chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
  5465. if (sp->sinfo_flags & SCTP_UNORDERED) {
  5466. chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
  5467. }
  5468. if (stcb->asoc.idata_supported == 0) {
  5469. if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
  5470. strq->next_mid_ordered++;
  5471. }
  5472. } else {
  5473. if (sp->sinfo_flags & SCTP_UNORDERED) {
  5474. strq->next_mid_unordered++;
  5475. } else {
  5476. strq->next_mid_ordered++;
  5477. }
  5478. }
  5479. oh_well:
  5480. if (sp->data) {
  5481. /* Pull any data to free up the SB and
  5482. * allow sender to "add more" while we
  5483. * will throw away :-)
  5484. */
  5485. sctp_free_spbufspace(stcb, &stcb->asoc, sp);
  5486. ret_sz += sp->length;
  5487. do_wakeup_routine = 1;
  5488. sp->some_taken = 1;
  5489. sctp_m_freem(sp->data);
  5490. sp->data = NULL;
  5491. sp->tail_mbuf = NULL;
  5492. sp->length = 0;
  5493. }
  5494. }
  5495. }
  5496. if (do_wakeup_routine) {
  5497. #if defined(__APPLE__) && !defined(__Userspace__)
  5498. struct socket *so;
  5499. so = SCTP_INP_SO(stcb->sctp_ep);
  5500. if (!so_locked) {
  5501. atomic_add_int(&stcb->asoc.refcnt, 1);
  5502. SCTP_TCB_UNLOCK(stcb);
  5503. SCTP_SOCKET_LOCK(so, 1);
  5504. SCTP_TCB_LOCK(stcb);
  5505. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  5506. if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
  5507. /* assoc was freed while we were unlocked */
  5508. SCTP_SOCKET_UNLOCK(so, 1);
  5509. return (ret_sz);
  5510. }
  5511. }
  5512. #endif
  5513. sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
  5514. #if defined(__APPLE__) && !defined(__Userspace__)
  5515. if (!so_locked) {
  5516. SCTP_SOCKET_UNLOCK(so, 1);
  5517. }
  5518. #endif
  5519. }
  5520. return (ret_sz);
  5521. }
  5522. /*
  5523. * checks to see if the given address, sa, is one that is currently known by
  5524. * the kernel note: can't distinguish the same address on multiple interfaces
  5525. * and doesn't handle multiple addresses with different zone/scope id's note:
  5526. * ifa_ifwithaddr() compares the entire sockaddr struct
  5527. */
  5528. struct sctp_ifa *
  5529. sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
  5530. int holds_lock)
  5531. {
  5532. struct sctp_laddr *laddr;
  5533. if (holds_lock == 0) {
  5534. SCTP_INP_RLOCK(inp);
  5535. }
  5536. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  5537. if (laddr->ifa == NULL)
  5538. continue;
  5539. if (addr->sa_family != laddr->ifa->address.sa.sa_family)
  5540. continue;
  5541. #ifdef INET
  5542. if (addr->sa_family == AF_INET) {
  5543. if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
  5544. laddr->ifa->address.sin.sin_addr.s_addr) {
  5545. /* found him. */
  5546. break;
  5547. }
  5548. }
  5549. #endif
  5550. #ifdef INET6
  5551. if (addr->sa_family == AF_INET6) {
  5552. if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
  5553. &laddr->ifa->address.sin6)) {
  5554. /* found him. */
  5555. break;
  5556. }
  5557. }
  5558. #endif
  5559. #if defined(__Userspace__)
  5560. if (addr->sa_family == AF_CONN) {
  5561. if (((struct sockaddr_conn *)addr)->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
  5562. /* found him. */
  5563. break;
  5564. }
  5565. }
  5566. #endif
  5567. }
  5568. if (holds_lock == 0) {
  5569. SCTP_INP_RUNLOCK(inp);
  5570. }
  5571. if (laddr != NULL) {
  5572. return (laddr->ifa);
  5573. } else {
  5574. return (NULL);
  5575. }
  5576. }
  5577. uint32_t
  5578. sctp_get_ifa_hash_val(struct sockaddr *addr)
  5579. {
  5580. switch (addr->sa_family) {
  5581. #ifdef INET
  5582. case AF_INET:
  5583. {
  5584. struct sockaddr_in *sin;
  5585. sin = (struct sockaddr_in *)addr;
  5586. return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
  5587. }
  5588. #endif
  5589. #ifdef INET6
  5590. case AF_INET6:
  5591. {
  5592. struct sockaddr_in6 *sin6;
  5593. uint32_t hash_of_addr;
  5594. sin6 = (struct sockaddr_in6 *)addr;
  5595. #if !defined(_WIN32) && !(defined(__FreeBSD__) && defined(__Userspace__)) && !defined(__APPLE__)
  5596. hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
  5597. sin6->sin6_addr.s6_addr32[1] +
  5598. sin6->sin6_addr.s6_addr32[2] +
  5599. sin6->sin6_addr.s6_addr32[3]);
  5600. #else
  5601. hash_of_addr = (((uint32_t *)&sin6->sin6_addr)[0] +
  5602. ((uint32_t *)&sin6->sin6_addr)[1] +
  5603. ((uint32_t *)&sin6->sin6_addr)[2] +
  5604. ((uint32_t *)&sin6->sin6_addr)[3]);
  5605. #endif
  5606. hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
  5607. return (hash_of_addr);
  5608. }
  5609. #endif
  5610. #if defined(__Userspace__)
  5611. case AF_CONN:
  5612. {
  5613. struct sockaddr_conn *sconn;
  5614. uintptr_t temp;
  5615. sconn = (struct sockaddr_conn *)addr;
  5616. temp = (uintptr_t)sconn->sconn_addr;
  5617. return ((uint32_t)(temp ^ (temp >> 16)));
  5618. }
  5619. #endif
  5620. default:
  5621. break;
  5622. }
  5623. return (0);
  5624. }
  5625. struct sctp_ifa *
  5626. sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
  5627. {
  5628. struct sctp_ifa *sctp_ifap;
  5629. struct sctp_vrf *vrf;
  5630. struct sctp_ifalist *hash_head;
  5631. uint32_t hash_of_addr;
  5632. if (holds_lock == 0) {
  5633. SCTP_IPI_ADDR_RLOCK();
  5634. } else {
  5635. SCTP_IPI_ADDR_LOCK_ASSERT();
  5636. }
  5637. vrf = sctp_find_vrf(vrf_id);
  5638. if (vrf == NULL) {
  5639. if (holds_lock == 0)
  5640. SCTP_IPI_ADDR_RUNLOCK();
  5641. return (NULL);
  5642. }
  5643. hash_of_addr = sctp_get_ifa_hash_val(addr);
  5644. hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
  5645. if (hash_head == NULL) {
  5646. SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
  5647. hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
  5648. (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
  5649. sctp_print_address(addr);
  5650. SCTP_PRINTF("No such bucket for address\n");
  5651. if (holds_lock == 0)
  5652. SCTP_IPI_ADDR_RUNLOCK();
  5653. return (NULL);
  5654. }
  5655. LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
  5656. if (addr->sa_family != sctp_ifap->address.sa.sa_family)
  5657. continue;
  5658. #ifdef INET
  5659. if (addr->sa_family == AF_INET) {
  5660. if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
  5661. sctp_ifap->address.sin.sin_addr.s_addr) {
  5662. /* found him. */
  5663. break;
  5664. }
  5665. }
  5666. #endif
  5667. #ifdef INET6
  5668. if (addr->sa_family == AF_INET6) {
  5669. if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
  5670. &sctp_ifap->address.sin6)) {
  5671. /* found him. */
  5672. break;
  5673. }
  5674. }
  5675. #endif
  5676. #if defined(__Userspace__)
  5677. if (addr->sa_family == AF_CONN) {
  5678. if (((struct sockaddr_conn *)addr)->sconn_addr == sctp_ifap->address.sconn.sconn_addr) {
  5679. /* found him. */
  5680. break;
  5681. }
  5682. }
  5683. #endif
  5684. }
  5685. if (holds_lock == 0)
  5686. SCTP_IPI_ADDR_RUNLOCK();
  5687. return (sctp_ifap);
  5688. }
  5689. static void
  5690. sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
  5691. uint32_t rwnd_req)
  5692. {
  5693. /* User pulled some data, do we need a rwnd update? */
  5694. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5695. struct epoch_tracker et;
  5696. #endif
  5697. int r_unlocked = 0;
  5698. uint32_t dif, rwnd;
  5699. struct socket *so = NULL;
  5700. if (stcb == NULL)
  5701. return;
  5702. atomic_add_int(&stcb->asoc.refcnt, 1);
  5703. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
  5704. (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
  5705. /* Pre-check If we are freeing no update */
  5706. goto no_lock;
  5707. }
  5708. SCTP_INP_INCR_REF(stcb->sctp_ep);
  5709. if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  5710. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
  5711. goto out;
  5712. }
  5713. so = stcb->sctp_socket;
  5714. if (so == NULL) {
  5715. goto out;
  5716. }
  5717. atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
  5718. /* Have you have freed enough to look */
  5719. *freed_so_far = 0;
  5720. /* Yep, its worth a look and the lock overhead */
  5721. /* Figure out what the rwnd would be */
  5722. rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
  5723. if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
  5724. dif = rwnd - stcb->asoc.my_last_reported_rwnd;
  5725. } else {
  5726. dif = 0;
  5727. }
  5728. if (dif >= rwnd_req) {
  5729. if (hold_rlock) {
  5730. SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
  5731. r_unlocked = 1;
  5732. }
  5733. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  5734. /*
  5735. * One last check before we allow the guy possibly
  5736. * to get in. There is a race, where the guy has not
  5737. * reached the gate. In that case
  5738. */
  5739. goto out;
  5740. }
  5741. SCTP_TCB_LOCK(stcb);
  5742. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  5743. /* No reports here */
  5744. SCTP_TCB_UNLOCK(stcb);
  5745. goto out;
  5746. }
  5747. SCTP_STAT_INCR(sctps_wu_sacks_sent);
  5748. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5749. NET_EPOCH_ENTER(et);
  5750. #endif
  5751. sctp_send_sack(stcb, SCTP_SO_LOCKED);
  5752. sctp_chunk_output(stcb->sctp_ep, stcb,
  5753. SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
  5754. /* make sure no timer is running */
  5755. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5756. NET_EPOCH_EXIT(et);
  5757. #endif
  5758. sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
  5759. SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
  5760. SCTP_TCB_UNLOCK(stcb);
  5761. } else {
  5762. /* Update how much we have pending */
  5763. stcb->freed_by_sorcv_sincelast = dif;
  5764. }
  5765. out:
  5766. if (so && r_unlocked && hold_rlock) {
  5767. SCTP_INP_READ_LOCK(stcb->sctp_ep);
  5768. }
  5769. SCTP_INP_DECR_REF(stcb->sctp_ep);
  5770. no_lock:
  5771. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  5772. return;
  5773. }
  5774. int
  5775. sctp_sorecvmsg(struct socket *so,
  5776. struct uio *uio,
  5777. struct mbuf **mp,
  5778. struct sockaddr *from,
  5779. int fromlen,
  5780. int *msg_flags,
  5781. struct sctp_sndrcvinfo *sinfo,
  5782. int filling_sinfo)
  5783. {
  5784. /*
  5785. * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
  5786. * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
  5787. * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
  5788. * On the way out we may send out any combination of:
  5789. * MSG_NOTIFICATION MSG_EOR
  5790. *
  5791. */
  5792. struct sctp_inpcb *inp = NULL;
  5793. ssize_t my_len = 0;
  5794. ssize_t cp_len = 0;
  5795. int error = 0;
  5796. struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
  5797. struct mbuf *m = NULL;
  5798. struct sctp_tcb *stcb = NULL;
  5799. int wakeup_read_socket = 0;
  5800. int freecnt_applied = 0;
  5801. int out_flags = 0, in_flags = 0;
  5802. int block_allowed = 1;
  5803. uint32_t freed_so_far = 0;
  5804. ssize_t copied_so_far = 0;
  5805. int in_eeor_mode = 0;
  5806. int no_rcv_needed = 0;
  5807. uint32_t rwnd_req = 0;
  5808. int hold_sblock = 0;
  5809. int hold_rlock = 0;
  5810. ssize_t slen = 0;
  5811. uint32_t held_length = 0;
  5812. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5813. int sockbuf_lock = 0;
  5814. #endif
  5815. if (uio == NULL) {
  5816. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  5817. return (EINVAL);
  5818. }
  5819. if (msg_flags) {
  5820. in_flags = *msg_flags;
  5821. if (in_flags & MSG_PEEK)
  5822. SCTP_STAT_INCR(sctps_read_peeks);
  5823. } else {
  5824. in_flags = 0;
  5825. }
  5826. #if defined(__APPLE__) && !defined(__Userspace__)
  5827. #if defined(APPLE_LEOPARD)
  5828. slen = uio->uio_resid;
  5829. #else
  5830. slen = uio_resid(uio);
  5831. #endif
  5832. #else
  5833. slen = uio->uio_resid;
  5834. #endif
  5835. /* Pull in and set up our int flags */
  5836. if (in_flags & MSG_OOB) {
  5837. /* Out of band's NOT supported */
  5838. return (EOPNOTSUPP);
  5839. }
  5840. if ((in_flags & MSG_PEEK) && (mp != NULL)) {
  5841. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  5842. return (EINVAL);
  5843. }
  5844. if ((in_flags & (MSG_DONTWAIT
  5845. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5846. | MSG_NBIO
  5847. #endif
  5848. )) ||
  5849. SCTP_SO_IS_NBIO(so)) {
  5850. block_allowed = 0;
  5851. }
  5852. /* setup the endpoint */
  5853. inp = (struct sctp_inpcb *)so->so_pcb;
  5854. if (inp == NULL) {
  5855. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
  5856. return (EFAULT);
  5857. }
  5858. rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
  5859. /* Must be at least a MTU's worth */
  5860. if (rwnd_req < SCTP_MIN_RWND)
  5861. rwnd_req = SCTP_MIN_RWND;
  5862. in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
  5863. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
  5864. #if defined(__APPLE__) && !defined(__Userspace__)
  5865. #if defined(APPLE_LEOPARD)
  5866. sctp_misc_ints(SCTP_SORECV_ENTER,
  5867. rwnd_req, in_eeor_mode, SCTP_SBAVAIL(&so->so_rcv), uio->uio_resid);
  5868. #else
  5869. sctp_misc_ints(SCTP_SORECV_ENTER,
  5870. rwnd_req, in_eeor_mode, SCTP_SBAVAIL(&so->so_rcv), uio_resid(uio));
  5871. #endif
  5872. #else
  5873. sctp_misc_ints(SCTP_SORECV_ENTER,
  5874. rwnd_req, in_eeor_mode, SCTP_SBAVAIL(&so->so_rcv), (uint32_t)uio->uio_resid);
  5875. #endif
  5876. }
  5877. #if defined(__Userspace__)
  5878. SOCKBUF_LOCK(&so->so_rcv);
  5879. hold_sblock = 1;
  5880. #endif
  5881. if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) {
  5882. #if defined(__APPLE__) && !defined(__Userspace__)
  5883. #if defined(APPLE_LEOPARD)
  5884. sctp_misc_ints(SCTP_SORECV_ENTERPL,
  5885. rwnd_req, block_allowed, SCTP_SBAVAIL(&so->so_rcv), uio->uio_resid);
  5886. #else
  5887. sctp_misc_ints(SCTP_SORECV_ENTERPL,
  5888. rwnd_req, block_allowed, SCTP_SBAVAIL(&so->so_rcv), uio_resid(uio));
  5889. #endif
  5890. #else
  5891. sctp_misc_ints(SCTP_SORECV_ENTERPL,
  5892. rwnd_req, block_allowed, SCTP_SBAVAIL(&so->so_rcv), (uint32_t)uio->uio_resid);
  5893. #endif
  5894. }
  5895. #if defined(__APPLE__) && !defined(__Userspace__)
  5896. error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
  5897. #endif
  5898. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5899. error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(in_flags));
  5900. #endif
  5901. if (error) {
  5902. goto release_unlocked;
  5903. }
  5904. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5905. sockbuf_lock = 1;
  5906. #endif
  5907. restart:
  5908. #if defined(__Userspace__)
  5909. if (hold_sblock == 0) {
  5910. SOCKBUF_LOCK(&so->so_rcv);
  5911. hold_sblock = 1;
  5912. }
  5913. #endif
  5914. #if defined(__APPLE__) && !defined(__Userspace__)
  5915. sbunlock(&so->so_rcv, 1);
  5916. #endif
  5917. restart_nosblocks:
  5918. if (hold_sblock == 0) {
  5919. SOCKBUF_LOCK(&so->so_rcv);
  5920. hold_sblock = 1;
  5921. }
  5922. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  5923. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
  5924. goto out;
  5925. }
  5926. #if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
  5927. if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && SCTP_SBAVAIL(&so->so_rcv) == 0) {
  5928. #else
  5929. if ((so->so_state & SS_CANTRCVMORE) && SCTP_SBAVAIL(&so->so_rcv) == 0) {
  5930. #endif
  5931. if (so->so_error) {
  5932. error = so->so_error;
  5933. if ((in_flags & MSG_PEEK) == 0)
  5934. so->so_error = 0;
  5935. goto out;
  5936. } else {
  5937. if (SCTP_SBAVAIL(&so->so_rcv) == 0) {
  5938. /* indicate EOF */
  5939. error = 0;
  5940. goto out;
  5941. }
  5942. }
  5943. }
  5944. if (SCTP_SBAVAIL(&so->so_rcv) <= held_length) {
  5945. if (so->so_error) {
  5946. error = so->so_error;
  5947. if ((in_flags & MSG_PEEK) == 0) {
  5948. so->so_error = 0;
  5949. }
  5950. goto out;
  5951. }
  5952. if ((SCTP_SBAVAIL(&so->so_rcv) == 0) &&
  5953. ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  5954. (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
  5955. if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
  5956. /* For active open side clear flags for re-use
  5957. * passive open is blocked by connect.
  5958. */
  5959. if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
  5960. /* You were aborted, passive side always hits here */
  5961. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
  5962. error = ECONNRESET;
  5963. }
  5964. so->so_state &= ~(SS_ISCONNECTING |
  5965. SS_ISDISCONNECTING |
  5966. SS_ISCONFIRMING |
  5967. SS_ISCONNECTED);
  5968. if (error == 0) {
  5969. if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
  5970. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
  5971. error = ENOTCONN;
  5972. }
  5973. }
  5974. goto out;
  5975. }
  5976. }
  5977. if (block_allowed) {
  5978. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5979. error = sbwait(so, SO_RCV);
  5980. #else
  5981. error = sbwait(&so->so_rcv);
  5982. #endif
  5983. if (error) {
  5984. goto out;
  5985. }
  5986. held_length = 0;
  5987. goto restart_nosblocks;
  5988. } else {
  5989. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
  5990. error = EWOULDBLOCK;
  5991. goto out;
  5992. }
  5993. }
  5994. if (hold_sblock == 1) {
  5995. SOCKBUF_UNLOCK(&so->so_rcv);
  5996. hold_sblock = 0;
  5997. }
  5998. #if defined(__APPLE__) && !defined(__Userspace__)
  5999. error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
  6000. #endif
  6001. /* we possibly have data we can read */
  6002. /*sa_ignore FREED_MEMORY*/
  6003. control = TAILQ_FIRST(&inp->read_queue);
  6004. if (control == NULL) {
  6005. /* This could be happening since
  6006. * the appender did the increment but as not
  6007. * yet did the tailq insert onto the read_queue
  6008. */
  6009. if (hold_rlock == 0) {
  6010. SCTP_INP_READ_LOCK(inp);
  6011. }
  6012. control = TAILQ_FIRST(&inp->read_queue);
  6013. if ((control == NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0)) {
  6014. #ifdef INVARIANTS
  6015. panic("Huh, its non zero and nothing on control?");
  6016. #endif
  6017. SCTP_SB_CLEAR(so->so_rcv);
  6018. }
  6019. SCTP_INP_READ_UNLOCK(inp);
  6020. hold_rlock = 0;
  6021. goto restart;
  6022. }
  6023. if ((control->length == 0) &&
  6024. (control->do_not_ref_stcb)) {
  6025. /* Clean up code for freeing assoc that left behind a pdapi..
  6026. * maybe a peer in EEOR that just closed after sending and
  6027. * never indicated a EOR.
  6028. */
  6029. if (hold_rlock == 0) {
  6030. hold_rlock = 1;
  6031. SCTP_INP_READ_LOCK(inp);
  6032. }
  6033. control->held_length = 0;
  6034. if (control->data) {
  6035. /* Hmm there is data here .. fix */
  6036. struct mbuf *m_tmp;
  6037. int cnt = 0;
  6038. m_tmp = control->data;
  6039. while (m_tmp) {
  6040. cnt += SCTP_BUF_LEN(m_tmp);
  6041. if (SCTP_BUF_NEXT(m_tmp) == NULL) {
  6042. control->tail_mbuf = m_tmp;
  6043. control->end_added = 1;
  6044. }
  6045. m_tmp = SCTP_BUF_NEXT(m_tmp);
  6046. }
  6047. control->length = cnt;
  6048. } else {
  6049. /* remove it */
  6050. TAILQ_REMOVE(&inp->read_queue, control, next);
  6051. /* Add back any hidden data */
  6052. sctp_free_remote_addr(control->whoFrom);
  6053. sctp_free_a_readq(stcb, control);
  6054. }
  6055. if (hold_rlock) {
  6056. hold_rlock = 0;
  6057. SCTP_INP_READ_UNLOCK(inp);
  6058. }
  6059. goto restart;
  6060. }
  6061. if ((control->length == 0) &&
  6062. (control->end_added == 1)) {
  6063. /* Do we also need to check for (control->pdapi_aborted == 1)? */
  6064. if (hold_rlock == 0) {
  6065. hold_rlock = 1;
  6066. SCTP_INP_READ_LOCK(inp);
  6067. }
  6068. TAILQ_REMOVE(&inp->read_queue, control, next);
  6069. if (control->data) {
  6070. #ifdef INVARIANTS
  6071. panic("control->data not null but control->length == 0");
  6072. #else
  6073. SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
  6074. sctp_m_freem(control->data);
  6075. control->data = NULL;
  6076. #endif
  6077. }
  6078. if (control->aux_data) {
  6079. sctp_m_free (control->aux_data);
  6080. control->aux_data = NULL;
  6081. }
  6082. #ifdef INVARIANTS
  6083. if (control->on_strm_q) {
  6084. panic("About to free ctl:%p so:%p and its in %d",
  6085. control, so, control->on_strm_q);
  6086. }
  6087. #endif
  6088. sctp_free_remote_addr(control->whoFrom);
  6089. sctp_free_a_readq(stcb, control);
  6090. if (hold_rlock) {
  6091. hold_rlock = 0;
  6092. SCTP_INP_READ_UNLOCK(inp);
  6093. }
  6094. goto restart;
  6095. }
  6096. if (control->length == 0) {
  6097. if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
  6098. (filling_sinfo)) {
  6099. /* find a more suitable one then this */
  6100. ctl = TAILQ_NEXT(control, next);
  6101. while (ctl) {
  6102. if ((ctl->stcb != control->stcb) && (ctl->length) &&
  6103. (ctl->some_taken ||
  6104. (ctl->spec_flags & M_NOTIFICATION) ||
  6105. ((ctl->do_not_ref_stcb == 0) &&
  6106. (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
  6107. ) {
  6108. /*-
  6109. * If we have a different TCB next, and there is data
  6110. * present. If we have already taken some (pdapi), OR we can
  6111. * ref the tcb and no delivery as started on this stream, we
  6112. * take it. Note we allow a notification on a different
  6113. * assoc to be delivered..
  6114. */
  6115. control = ctl;
  6116. goto found_one;
  6117. } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
  6118. (ctl->length) &&
  6119. ((ctl->some_taken) ||
  6120. ((ctl->do_not_ref_stcb == 0) &&
  6121. ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
  6122. (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
  6123. /*-
  6124. * If we have the same tcb, and there is data present, and we
  6125. * have the strm interleave feature present. Then if we have
  6126. * taken some (pdapi) or we can refer to tht tcb AND we have
  6127. * not started a delivery for this stream, we can take it.
  6128. * Note we do NOT allow a notification on the same assoc to
  6129. * be delivered.
  6130. */
  6131. control = ctl;
  6132. goto found_one;
  6133. }
  6134. ctl = TAILQ_NEXT(ctl, next);
  6135. }
  6136. }
  6137. /*
  6138. * if we reach here, not suitable replacement is available
  6139. * <or> fragment interleave is NOT on. So stuff the sb_cc
  6140. * into the our held count, and its time to sleep again.
  6141. */
  6142. held_length = SCTP_SBAVAIL(&so->so_rcv);
  6143. control->held_length = SCTP_SBAVAIL(&so->so_rcv);
  6144. goto restart;
  6145. }
  6146. /* Clear the held length since there is something to read */
  6147. control->held_length = 0;
  6148. found_one:
  6149. /*
  6150. * If we reach here, control has a some data for us to read off.
  6151. * Note that stcb COULD be NULL.
  6152. */
  6153. if (hold_rlock == 0) {
  6154. hold_rlock = 1;
  6155. SCTP_INP_READ_LOCK(inp);
  6156. }
  6157. control->some_taken++;
  6158. stcb = control->stcb;
  6159. if (stcb) {
  6160. if ((control->do_not_ref_stcb == 0) &&
  6161. (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
  6162. if (freecnt_applied == 0)
  6163. stcb = NULL;
  6164. } else if (control->do_not_ref_stcb == 0) {
  6165. /* you can't free it on me please */
  6166. /*
  6167. * The lock on the socket buffer protects us so the
  6168. * free code will stop. But since we used the socketbuf
  6169. * lock and the sender uses the tcb_lock to increment,
  6170. * we need to use the atomic add to the refcnt
  6171. */
  6172. if (freecnt_applied) {
  6173. #ifdef INVARIANTS
  6174. panic("refcnt already incremented");
  6175. #else
  6176. SCTP_PRINTF("refcnt already incremented?\n");
  6177. #endif
  6178. } else {
  6179. atomic_add_int(&stcb->asoc.refcnt, 1);
  6180. freecnt_applied = 1;
  6181. }
  6182. /*
  6183. * Setup to remember how much we have not yet told
  6184. * the peer our rwnd has opened up. Note we grab
  6185. * the value from the tcb from last time.
  6186. * Note too that sack sending clears this when a sack
  6187. * is sent, which is fine. Once we hit the rwnd_req,
  6188. * we then will go to the sctp_user_rcvd() that will
  6189. * not lock until it KNOWs it MUST send a WUP-SACK.
  6190. */
  6191. freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
  6192. stcb->freed_by_sorcv_sincelast = 0;
  6193. }
  6194. }
  6195. if (stcb &&
  6196. ((control->spec_flags & M_NOTIFICATION) == 0) &&
  6197. control->do_not_ref_stcb == 0) {
  6198. stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
  6199. }
  6200. /* First lets get off the sinfo and sockaddr info */
  6201. if ((sinfo != NULL) && (filling_sinfo != 0)) {
  6202. sinfo->sinfo_stream = control->sinfo_stream;
  6203. sinfo->sinfo_ssn = (uint16_t)control->mid;
  6204. sinfo->sinfo_flags = control->sinfo_flags;
  6205. sinfo->sinfo_ppid = control->sinfo_ppid;
  6206. sinfo->sinfo_context =control->sinfo_context;
  6207. sinfo->sinfo_timetolive = control->sinfo_timetolive;
  6208. sinfo->sinfo_tsn = control->sinfo_tsn;
  6209. sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
  6210. sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
  6211. nxt = TAILQ_NEXT(control, next);
  6212. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
  6213. sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
  6214. struct sctp_extrcvinfo *s_extra;
  6215. s_extra = (struct sctp_extrcvinfo *)sinfo;
  6216. if ((nxt) &&
  6217. (nxt->length)) {
  6218. s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
  6219. if (nxt->sinfo_flags & SCTP_UNORDERED) {
  6220. s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
  6221. }
  6222. if (nxt->spec_flags & M_NOTIFICATION) {
  6223. s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
  6224. }
  6225. s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
  6226. s_extra->serinfo_next_length = nxt->length;
  6227. s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
  6228. s_extra->serinfo_next_stream = nxt->sinfo_stream;
  6229. if (nxt->tail_mbuf != NULL) {
  6230. if (nxt->end_added) {
  6231. s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
  6232. }
  6233. }
  6234. } else {
  6235. /* we explicitly 0 this, since the memcpy got
  6236. * some other things beyond the older sinfo_
  6237. * that is on the control's structure :-D
  6238. */
  6239. nxt = NULL;
  6240. s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
  6241. s_extra->serinfo_next_aid = 0;
  6242. s_extra->serinfo_next_length = 0;
  6243. s_extra->serinfo_next_ppid = 0;
  6244. s_extra->serinfo_next_stream = 0;
  6245. }
  6246. }
  6247. /*
  6248. * update off the real current cum-ack, if we have an stcb.
  6249. */
  6250. if ((control->do_not_ref_stcb == 0) && stcb)
  6251. sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
  6252. /*
  6253. * mask off the high bits, we keep the actual chunk bits in
  6254. * there.
  6255. */
  6256. sinfo->sinfo_flags &= 0x00ff;
  6257. if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
  6258. sinfo->sinfo_flags |= SCTP_UNORDERED;
  6259. }
  6260. }
  6261. #ifdef SCTP_ASOCLOG_OF_TSNS
  6262. {
  6263. int index, newindex;
  6264. struct sctp_pcbtsn_rlog *entry;
  6265. do {
  6266. index = inp->readlog_index;
  6267. newindex = index + 1;
  6268. if (newindex >= SCTP_READ_LOG_SIZE) {
  6269. newindex = 0;
  6270. }
  6271. } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
  6272. entry = &inp->readlog[index];
  6273. entry->vtag = control->sinfo_assoc_id;
  6274. entry->strm = control->sinfo_stream;
  6275. entry->seq = (uint16_t)control->mid;
  6276. entry->sz = control->length;
  6277. entry->flgs = control->sinfo_flags;
  6278. }
  6279. #endif
  6280. if ((fromlen > 0) && (from != NULL)) {
  6281. union sctp_sockstore store;
  6282. size_t len;
  6283. switch (control->whoFrom->ro._l_addr.sa.sa_family) {
  6284. #ifdef INET6
  6285. case AF_INET6:
  6286. len = sizeof(struct sockaddr_in6);
  6287. store.sin6 = control->whoFrom->ro._l_addr.sin6;
  6288. store.sin6.sin6_port = control->port_from;
  6289. break;
  6290. #endif
  6291. #ifdef INET
  6292. case AF_INET:
  6293. #ifdef INET6
  6294. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
  6295. len = sizeof(struct sockaddr_in6);
  6296. in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
  6297. &store.sin6);
  6298. store.sin6.sin6_port = control->port_from;
  6299. } else {
  6300. len = sizeof(struct sockaddr_in);
  6301. store.sin = control->whoFrom->ro._l_addr.sin;
  6302. store.sin.sin_port = control->port_from;
  6303. }
  6304. #else
  6305. len = sizeof(struct sockaddr_in);
  6306. store.sin = control->whoFrom->ro._l_addr.sin;
  6307. store.sin.sin_port = control->port_from;
  6308. #endif
  6309. break;
  6310. #endif
  6311. #if defined(__Userspace__)
  6312. case AF_CONN:
  6313. len = sizeof(struct sockaddr_conn);
  6314. store.sconn = control->whoFrom->ro._l_addr.sconn;
  6315. store.sconn.sconn_port = control->port_from;
  6316. break;
  6317. #endif
  6318. default:
  6319. len = 0;
  6320. break;
  6321. }
  6322. memcpy(from, &store, min((size_t)fromlen, len));
  6323. #if defined(SCTP_EMBEDDED_V6_SCOPE)
  6324. #ifdef INET6
  6325. {
  6326. struct sockaddr_in6 lsa6, *from6;
  6327. from6 = (struct sockaddr_in6 *)from;
  6328. sctp_recover_scope_mac(from6, (&lsa6));
  6329. }
  6330. #endif
  6331. #endif
  6332. }
  6333. if (hold_rlock) {
  6334. SCTP_INP_READ_UNLOCK(inp);
  6335. hold_rlock = 0;
  6336. }
  6337. if (hold_sblock) {
  6338. SOCKBUF_UNLOCK(&so->so_rcv);
  6339. hold_sblock = 0;
  6340. }
  6341. /* now copy out what data we can */
  6342. if (mp == NULL) {
  6343. /* copy out each mbuf in the chain up to length */
  6344. get_more_data:
  6345. m = control->data;
  6346. while (m) {
  6347. /* Move out all we can */
  6348. #if defined(__APPLE__) && !defined(__Userspace__)
  6349. #if defined(APPLE_LEOPARD)
  6350. cp_len = uio->uio_resid;
  6351. #else
  6352. cp_len = uio_resid(uio);
  6353. #endif
  6354. #else
  6355. cp_len = uio->uio_resid;
  6356. #endif
  6357. my_len = SCTP_BUF_LEN(m);
  6358. if (cp_len > my_len) {
  6359. /* not enough in this buf */
  6360. cp_len = my_len;
  6361. }
  6362. if (hold_rlock) {
  6363. SCTP_INP_READ_UNLOCK(inp);
  6364. hold_rlock = 0;
  6365. }
  6366. #if defined(__APPLE__) && !defined(__Userspace__)
  6367. SCTP_SOCKET_UNLOCK(so, 0);
  6368. #endif
  6369. if (cp_len > 0)
  6370. error = uiomove(mtod(m, char *), (int)cp_len, uio);
  6371. #if defined(__APPLE__) && !defined(__Userspace__)
  6372. SCTP_SOCKET_LOCK(so, 0);
  6373. #endif
  6374. /* re-read */
  6375. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  6376. goto release;
  6377. }
  6378. if ((control->do_not_ref_stcb == 0) && stcb &&
  6379. stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  6380. no_rcv_needed = 1;
  6381. }
  6382. if (error) {
  6383. /* error we are out of here */
  6384. goto release;
  6385. }
  6386. SCTP_INP_READ_LOCK(inp);
  6387. hold_rlock = 1;
  6388. if (cp_len == SCTP_BUF_LEN(m)) {
  6389. if ((SCTP_BUF_NEXT(m)== NULL) &&
  6390. (control->end_added)) {
  6391. out_flags |= MSG_EOR;
  6392. if ((control->do_not_ref_stcb == 0) &&
  6393. (control->stcb != NULL) &&
  6394. ((control->spec_flags & M_NOTIFICATION) == 0))
  6395. control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
  6396. }
  6397. if (control->spec_flags & M_NOTIFICATION) {
  6398. out_flags |= MSG_NOTIFICATION;
  6399. }
  6400. /* we ate up the mbuf */
  6401. if (in_flags & MSG_PEEK) {
  6402. /* just looking */
  6403. m = SCTP_BUF_NEXT(m);
  6404. copied_so_far += cp_len;
  6405. } else {
  6406. /* dispose of the mbuf */
  6407. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  6408. sctp_sblog(&so->so_rcv,
  6409. control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
  6410. }
  6411. sctp_sbfree(control, stcb, &so->so_rcv, m);
  6412. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  6413. sctp_sblog(&so->so_rcv,
  6414. control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
  6415. }
  6416. copied_so_far += cp_len;
  6417. freed_so_far += (uint32_t)cp_len;
  6418. freed_so_far += MSIZE;
  6419. atomic_subtract_int(&control->length, (int)cp_len);
  6420. control->data = sctp_m_free(m);
  6421. m = control->data;
  6422. /* been through it all, must hold sb lock ok to null tail */
  6423. if (control->data == NULL) {
  6424. #ifdef INVARIANTS
  6425. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6426. if ((control->end_added == 0) ||
  6427. (TAILQ_NEXT(control, next) == NULL)) {
  6428. /* If the end is not added, OR the
  6429. * next is NOT null we MUST have the lock.
  6430. */
  6431. if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
  6432. panic("Hmm we don't own the lock?");
  6433. }
  6434. }
  6435. #endif
  6436. #endif
  6437. control->tail_mbuf = NULL;
  6438. #ifdef INVARIANTS
  6439. if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
  6440. panic("end_added, nothing left and no MSG_EOR");
  6441. }
  6442. #endif
  6443. }
  6444. }
  6445. } else {
  6446. /* Do we need to trim the mbuf? */
  6447. if (control->spec_flags & M_NOTIFICATION) {
  6448. out_flags |= MSG_NOTIFICATION;
  6449. }
  6450. if ((in_flags & MSG_PEEK) == 0) {
  6451. SCTP_BUF_RESV_UF(m, cp_len);
  6452. SCTP_BUF_LEN(m) -= (int)cp_len;
  6453. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  6454. sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, (int)cp_len);
  6455. }
  6456. atomic_subtract_int(&so->so_rcv.sb_cc, (int)cp_len);
  6457. if ((control->do_not_ref_stcb == 0) &&
  6458. stcb) {
  6459. atomic_subtract_int(&stcb->asoc.sb_cc, (int)cp_len);
  6460. }
  6461. copied_so_far += cp_len;
  6462. freed_so_far += (uint32_t)cp_len;
  6463. freed_so_far += MSIZE;
  6464. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  6465. sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb,
  6466. SCTP_LOG_SBRESULT, 0);
  6467. }
  6468. atomic_subtract_int(&control->length, (int)cp_len);
  6469. } else {
  6470. copied_so_far += cp_len;
  6471. }
  6472. }
  6473. #if defined(__APPLE__) && !defined(__Userspace__)
  6474. #if defined(APPLE_LEOPARD)
  6475. if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
  6476. #else
  6477. if ((out_flags & MSG_EOR) || (uio_resid(uio) == 0)) {
  6478. #endif
  6479. #else
  6480. if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
  6481. #endif
  6482. break;
  6483. }
  6484. if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
  6485. (control->do_not_ref_stcb == 0) &&
  6486. (freed_so_far >= rwnd_req)) {
  6487. sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
  6488. }
  6489. } /* end while(m) */
  6490. /*
  6491. * At this point we have looked at it all and we either have
  6492. * a MSG_EOR/or read all the user wants... <OR>
  6493. * control->length == 0.
  6494. */
  6495. if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
  6496. /* we are done with this control */
  6497. if (control->length == 0) {
  6498. if (control->data) {
  6499. #ifdef INVARIANTS
  6500. panic("control->data not null at read eor?");
  6501. #else
  6502. SCTP_PRINTF("Strange, data left in the control buffer .. invariants would panic?\n");
  6503. sctp_m_freem(control->data);
  6504. control->data = NULL;
  6505. #endif
  6506. }
  6507. done_with_control:
  6508. if (hold_rlock == 0) {
  6509. SCTP_INP_READ_LOCK(inp);
  6510. hold_rlock = 1;
  6511. }
  6512. TAILQ_REMOVE(&inp->read_queue, control, next);
  6513. /* Add back any hidden data */
  6514. if (control->held_length) {
  6515. held_length = 0;
  6516. control->held_length = 0;
  6517. wakeup_read_socket = 1;
  6518. }
  6519. if (control->aux_data) {
  6520. sctp_m_free (control->aux_data);
  6521. control->aux_data = NULL;
  6522. }
  6523. no_rcv_needed = control->do_not_ref_stcb;
  6524. sctp_free_remote_addr(control->whoFrom);
  6525. control->data = NULL;
  6526. #ifdef INVARIANTS
  6527. if (control->on_strm_q) {
  6528. panic("About to free ctl:%p so:%p and its in %d",
  6529. control, so, control->on_strm_q);
  6530. }
  6531. #endif
  6532. sctp_free_a_readq(stcb, control);
  6533. control = NULL;
  6534. if ((freed_so_far >= rwnd_req) &&
  6535. (no_rcv_needed == 0))
  6536. sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
  6537. } else {
  6538. /*
  6539. * The user did not read all of this
  6540. * message, turn off the returned MSG_EOR
  6541. * since we are leaving more behind on the
  6542. * control to read.
  6543. */
  6544. #ifdef INVARIANTS
  6545. if (control->end_added &&
  6546. (control->data == NULL) &&
  6547. (control->tail_mbuf == NULL)) {
  6548. panic("Gak, control->length is corrupt?");
  6549. }
  6550. #endif
  6551. no_rcv_needed = control->do_not_ref_stcb;
  6552. out_flags &= ~MSG_EOR;
  6553. }
  6554. }
  6555. if (out_flags & MSG_EOR) {
  6556. goto release;
  6557. }
  6558. #if defined(__APPLE__) && !defined(__Userspace__)
  6559. #if defined(APPLE_LEOPARD)
  6560. if ((uio->uio_resid == 0) ||
  6561. #else
  6562. if ((uio_resid(uio) == 0) ||
  6563. #endif
  6564. #else
  6565. if ((uio->uio_resid == 0) ||
  6566. #endif
  6567. ((in_eeor_mode) &&
  6568. (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
  6569. goto release;
  6570. }
  6571. /*
  6572. * If I hit here the receiver wants more and this message is
  6573. * NOT done (pd-api). So two questions. Can we block? if not
  6574. * we are done. Did the user NOT set MSG_WAITALL?
  6575. */
  6576. if (block_allowed == 0) {
  6577. goto release;
  6578. }
  6579. /*
  6580. * We need to wait for more data a few things:
  6581. * - We don't release the I/O lock so we don't get someone else
  6582. * reading.
  6583. * - We must be sure to account for the case where what is added
  6584. * is NOT to our control when we wakeup.
  6585. */
  6586. /* Do we need to tell the transport a rwnd update might be
  6587. * needed before we go to sleep?
  6588. */
  6589. if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
  6590. ((freed_so_far >= rwnd_req) &&
  6591. (control->do_not_ref_stcb == 0) &&
  6592. (no_rcv_needed == 0))) {
  6593. sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
  6594. }
  6595. wait_some_more:
  6596. #if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
  6597. if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
  6598. goto release;
  6599. }
  6600. #else
  6601. if (so->so_state & SS_CANTRCVMORE) {
  6602. goto release;
  6603. }
  6604. #endif
  6605. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
  6606. goto release;
  6607. if (hold_rlock == 1) {
  6608. SCTP_INP_READ_UNLOCK(inp);
  6609. hold_rlock = 0;
  6610. }
  6611. if (hold_sblock == 0) {
  6612. SOCKBUF_LOCK(&so->so_rcv);
  6613. hold_sblock = 1;
  6614. }
  6615. if ((copied_so_far) && (control->length == 0) &&
  6616. (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
  6617. goto release;
  6618. }
  6619. #if defined(__APPLE__) && !defined(__Userspace__)
  6620. sbunlock(&so->so_rcv, 1);
  6621. #endif
  6622. if (SCTP_SBAVAIL(&so->so_rcv) <= control->held_length) {
  6623. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6624. error = sbwait(so, SO_RCV);
  6625. #else
  6626. error = sbwait(&so->so_rcv);
  6627. #endif
  6628. if (error) {
  6629. #if defined(__APPLE__) && !defined(__Userspace__)
  6630. goto release_unlocked;
  6631. #else
  6632. goto release;
  6633. #endif
  6634. }
  6635. control->held_length = 0;
  6636. }
  6637. #if defined(__APPLE__) && !defined(__Userspace__)
  6638. error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
  6639. #endif
  6640. if (hold_sblock) {
  6641. SOCKBUF_UNLOCK(&so->so_rcv);
  6642. hold_sblock = 0;
  6643. }
  6644. if (control->length == 0) {
  6645. /* still nothing here */
  6646. if (control->end_added == 1) {
  6647. /* he aborted, or is done i.e.did a shutdown */
  6648. out_flags |= MSG_EOR;
  6649. if (control->pdapi_aborted) {
  6650. if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
  6651. control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
  6652. out_flags |= MSG_TRUNC;
  6653. } else {
  6654. if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
  6655. control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
  6656. }
  6657. goto done_with_control;
  6658. }
  6659. if (SCTP_SBAVAIL(&so->so_rcv) > held_length) {
  6660. control->held_length = SCTP_SBAVAIL(&so->so_rcv);
  6661. held_length = 0;
  6662. }
  6663. goto wait_some_more;
  6664. } else if (control->data == NULL) {
  6665. /* we must re-sync since data
  6666. * is probably being added
  6667. */
  6668. SCTP_INP_READ_LOCK(inp);
  6669. if ((control->length > 0) && (control->data == NULL)) {
  6670. /* big trouble.. we have the lock and its corrupt? */
  6671. #ifdef INVARIANTS
  6672. panic ("Impossible data==NULL length !=0");
  6673. #endif
  6674. out_flags |= MSG_EOR;
  6675. out_flags |= MSG_TRUNC;
  6676. control->length = 0;
  6677. SCTP_INP_READ_UNLOCK(inp);
  6678. goto done_with_control;
  6679. }
  6680. SCTP_INP_READ_UNLOCK(inp);
  6681. /* We will fall around to get more data */
  6682. }
  6683. goto get_more_data;
  6684. } else {
  6685. /*-
  6686. * Give caller back the mbuf chain,
  6687. * store in uio_resid the length
  6688. */
  6689. wakeup_read_socket = 0;
  6690. if ((control->end_added == 0) ||
  6691. (TAILQ_NEXT(control, next) == NULL)) {
  6692. /* Need to get rlock */
  6693. if (hold_rlock == 0) {
  6694. SCTP_INP_READ_LOCK(inp);
  6695. hold_rlock = 1;
  6696. }
  6697. }
  6698. if (control->end_added) {
  6699. out_flags |= MSG_EOR;
  6700. if ((control->do_not_ref_stcb == 0) &&
  6701. (control->stcb != NULL) &&
  6702. ((control->spec_flags & M_NOTIFICATION) == 0))
  6703. control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
  6704. }
  6705. if (control->spec_flags & M_NOTIFICATION) {
  6706. out_flags |= MSG_NOTIFICATION;
  6707. }
  6708. #if defined(__APPLE__) && !defined(__Userspace__)
  6709. #if defined(APPLE_LEOPARD)
  6710. uio->uio_resid = control->length;
  6711. #else
  6712. uio_setresid(uio, control->length);
  6713. #endif
  6714. #else
  6715. uio->uio_resid = control->length;
  6716. #endif
  6717. *mp = control->data;
  6718. m = control->data;
  6719. while (m) {
  6720. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  6721. sctp_sblog(&so->so_rcv,
  6722. control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
  6723. }
  6724. sctp_sbfree(control, stcb, &so->so_rcv, m);
  6725. freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
  6726. freed_so_far += MSIZE;
  6727. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
  6728. sctp_sblog(&so->so_rcv,
  6729. control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
  6730. }
  6731. m = SCTP_BUF_NEXT(m);
  6732. }
  6733. control->data = control->tail_mbuf = NULL;
  6734. control->length = 0;
  6735. if (out_flags & MSG_EOR) {
  6736. /* Done with this control */
  6737. goto done_with_control;
  6738. }
  6739. }
  6740. release:
  6741. if (hold_rlock == 1) {
  6742. SCTP_INP_READ_UNLOCK(inp);
  6743. hold_rlock = 0;
  6744. }
  6745. #if defined(__Userspace__)
  6746. if (hold_sblock == 0) {
  6747. SOCKBUF_LOCK(&so->so_rcv);
  6748. hold_sblock = 1;
  6749. }
  6750. #else
  6751. if (hold_sblock == 1) {
  6752. SOCKBUF_UNLOCK(&so->so_rcv);
  6753. hold_sblock = 0;
  6754. }
  6755. #endif
  6756. #if defined(__APPLE__) && !defined(__Userspace__)
  6757. sbunlock(&so->so_rcv, 1);
  6758. #endif
  6759. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6760. SOCK_IO_RECV_UNLOCK(so);
  6761. sockbuf_lock = 0;
  6762. #endif
  6763. release_unlocked:
  6764. if (hold_sblock) {
  6765. SOCKBUF_UNLOCK(&so->so_rcv);
  6766. hold_sblock = 0;
  6767. }
  6768. if ((stcb) && (in_flags & MSG_PEEK) == 0) {
  6769. if ((freed_so_far >= rwnd_req) &&
  6770. (control && (control->do_not_ref_stcb == 0)) &&
  6771. (no_rcv_needed == 0))
  6772. sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
  6773. }
  6774. out:
  6775. if (msg_flags) {
  6776. *msg_flags = out_flags;
  6777. }
  6778. if (((out_flags & MSG_EOR) == 0) &&
  6779. ((in_flags & MSG_PEEK) == 0) &&
  6780. (sinfo) &&
  6781. (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
  6782. sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
  6783. struct sctp_extrcvinfo *s_extra;
  6784. s_extra = (struct sctp_extrcvinfo *)sinfo;
  6785. s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
  6786. }
  6787. if (hold_rlock == 1) {
  6788. SCTP_INP_READ_UNLOCK(inp);
  6789. }
  6790. if (hold_sblock) {
  6791. SOCKBUF_UNLOCK(&so->so_rcv);
  6792. }
  6793. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6794. if (sockbuf_lock) {
  6795. SOCK_IO_RECV_UNLOCK(so);
  6796. }
  6797. #endif
  6798. if (freecnt_applied) {
  6799. /*
  6800. * The lock on the socket buffer protects us so the free
  6801. * code will stop. But since we used the socketbuf lock and
  6802. * the sender uses the tcb_lock to increment, we need to use
  6803. * the atomic add to the refcnt.
  6804. */
  6805. if (stcb == NULL) {
  6806. #ifdef INVARIANTS
  6807. panic("stcb for refcnt has gone NULL?");
  6808. goto stage_left;
  6809. #else
  6810. goto stage_left;
  6811. #endif
  6812. }
  6813. /* Save the value back for next time */
  6814. stcb->freed_by_sorcv_sincelast = freed_so_far;
  6815. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  6816. }
  6817. if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) {
  6818. if (stcb) {
  6819. sctp_misc_ints(SCTP_SORECV_DONE,
  6820. freed_so_far,
  6821. #if defined(__APPLE__) && !defined(__Userspace__)
  6822. #if defined(APPLE_LEOPARD)
  6823. ((uio) ? (slen - uio->uio_resid) : slen),
  6824. #else
  6825. ((uio) ? (slen - uio_resid(uio)) : slen),
  6826. #endif
  6827. #else
  6828. (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
  6829. #endif
  6830. stcb->asoc.my_rwnd,
  6831. SCTP_SBAVAIL(&so->so_rcv));
  6832. } else {
  6833. sctp_misc_ints(SCTP_SORECV_DONE,
  6834. freed_so_far,
  6835. #if defined(__APPLE__) && !defined(__Userspace__)
  6836. #if defined(APPLE_LEOPARD)
  6837. ((uio) ? (slen - uio->uio_resid) : slen),
  6838. #else
  6839. ((uio) ? (slen - uio_resid(uio)) : slen),
  6840. #endif
  6841. #else
  6842. (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
  6843. #endif
  6844. 0,
  6845. SCTP_SBAVAIL(&so->so_rcv));
  6846. }
  6847. }
  6848. stage_left:
  6849. if (wakeup_read_socket) {
  6850. sctp_sorwakeup(inp, so);
  6851. }
  6852. return (error);
  6853. }
  6854. #ifdef SCTP_MBUF_LOGGING
  6855. struct mbuf *
  6856. sctp_m_free(struct mbuf *m)
  6857. {
  6858. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  6859. sctp_log_mb(m, SCTP_MBUF_IFREE);
  6860. }
  6861. return (m_free(m));
  6862. }
  6863. void
  6864. sctp_m_freem(struct mbuf *mb)
  6865. {
  6866. while (mb != NULL)
  6867. mb = sctp_m_free(mb);
  6868. }
  6869. #endif
  6870. int
  6871. sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
  6872. {
  6873. /* Given a local address. For all associations
  6874. * that holds the address, request a peer-set-primary.
  6875. */
  6876. struct sctp_ifa *ifa;
  6877. struct sctp_laddr *wi;
  6878. ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
  6879. if (ifa == NULL) {
  6880. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
  6881. return (EADDRNOTAVAIL);
  6882. }
  6883. /* Now that we have the ifa we must awaken the
  6884. * iterator with this message.
  6885. */
  6886. wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
  6887. if (wi == NULL) {
  6888. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
  6889. return (ENOMEM);
  6890. }
  6891. /* Now incr the count and int wi structure */
  6892. SCTP_INCR_LADDR_COUNT();
  6893. memset(wi, 0, sizeof(*wi));
  6894. (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
  6895. wi->ifa = ifa;
  6896. wi->action = SCTP_SET_PRIM_ADDR;
  6897. atomic_add_int(&ifa->refcount, 1);
  6898. /* Now add it to the work queue */
  6899. SCTP_WQ_ADDR_LOCK();
  6900. /*
  6901. * Should this really be a tailq? As it is we will process the
  6902. * newest first :-0
  6903. */
  6904. LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
  6905. sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
  6906. (struct sctp_inpcb *)NULL,
  6907. (struct sctp_tcb *)NULL,
  6908. (struct sctp_nets *)NULL);
  6909. SCTP_WQ_ADDR_UNLOCK();
  6910. return (0);
  6911. }
  6912. #if defined(__Userspace__)
  6913. /* no sctp_soreceive for __Userspace__ now */
  6914. #endif
  6915. #if !defined(__Userspace__)
  6916. int
  6917. sctp_soreceive( struct socket *so,
  6918. struct sockaddr **psa,
  6919. struct uio *uio,
  6920. struct mbuf **mp0,
  6921. struct mbuf **controlp,
  6922. int *flagsp)
  6923. {
  6924. int error, fromlen;
  6925. uint8_t sockbuf[256];
  6926. struct sockaddr *from;
  6927. struct sctp_extrcvinfo sinfo;
  6928. int filling_sinfo = 1;
  6929. int flags;
  6930. struct sctp_inpcb *inp;
  6931. inp = (struct sctp_inpcb *)so->so_pcb;
  6932. /* pickup the assoc we are reading from */
  6933. if (inp == NULL) {
  6934. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  6935. return (EINVAL);
  6936. }
  6937. if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
  6938. sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
  6939. sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
  6940. (controlp == NULL)) {
  6941. /* user does not want the sndrcv ctl */
  6942. filling_sinfo = 0;
  6943. }
  6944. if (psa) {
  6945. from = (struct sockaddr *)sockbuf;
  6946. fromlen = sizeof(sockbuf);
  6947. #ifdef HAVE_SA_LEN
  6948. from->sa_len = 0;
  6949. #endif
  6950. } else {
  6951. from = NULL;
  6952. fromlen = 0;
  6953. }
  6954. #if defined(__APPLE__) && !defined(__Userspace__)
  6955. SCTP_SOCKET_LOCK(so, 1);
  6956. #endif
  6957. if (filling_sinfo) {
  6958. memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
  6959. }
  6960. if (flagsp != NULL) {
  6961. flags = *flagsp;
  6962. } else {
  6963. flags = 0;
  6964. }
  6965. error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
  6966. (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
  6967. if (flagsp != NULL) {
  6968. *flagsp = flags;
  6969. }
  6970. if (controlp != NULL) {
  6971. /* copy back the sinfo in a CMSG format */
  6972. if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
  6973. *controlp = sctp_build_ctl_nchunk(inp,
  6974. (struct sctp_sndrcvinfo *)&sinfo);
  6975. } else {
  6976. *controlp = NULL;
  6977. }
  6978. }
  6979. if (psa) {
  6980. /* copy back the address info */
  6981. #ifdef HAVE_SA_LEN
  6982. if (from && from->sa_len) {
  6983. #else
  6984. if (from) {
  6985. #endif
  6986. #if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
  6987. *psa = sodupsockaddr(from, M_NOWAIT);
  6988. #else
  6989. *psa = dup_sockaddr(from, mp0 == 0);
  6990. #endif
  6991. } else {
  6992. *psa = NULL;
  6993. }
  6994. }
  6995. #if defined(__APPLE__) && !defined(__Userspace__)
  6996. SCTP_SOCKET_UNLOCK(so, 1);
  6997. #endif
  6998. return (error);
  6999. }
  7000. #if defined(_WIN32) && !defined(__Userspace__)
  7001. /*
  7002. * General routine to allocate a hash table with control of memory flags.
  7003. * is in 7.0 and beyond for sure :-)
  7004. */
  7005. void *
  7006. sctp_hashinit_flags(int elements, struct malloc_type *type,
  7007. u_long *hashmask, int flags)
  7008. {
  7009. long hashsize;
  7010. LIST_HEAD(generic, generic) *hashtbl;
  7011. int i;
  7012. if (elements <= 0) {
  7013. #ifdef INVARIANTS
  7014. panic("hashinit: bad elements");
  7015. #else
  7016. SCTP_PRINTF("hashinit: bad elements?");
  7017. elements = 1;
  7018. #endif
  7019. }
  7020. for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
  7021. continue;
  7022. hashsize >>= 1;
  7023. if (flags & HASH_WAITOK)
  7024. hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
  7025. else if (flags & HASH_NOWAIT)
  7026. hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_NOWAIT);
  7027. else {
  7028. #ifdef INVARIANTS
  7029. panic("flag incorrect in hashinit_flags");
  7030. #else
  7031. return (NULL);
  7032. #endif
  7033. }
  7034. /* no memory? */
  7035. if (hashtbl == NULL)
  7036. return (NULL);
  7037. for (i = 0; i < hashsize; i++)
  7038. LIST_INIT(&hashtbl[i]);
  7039. *hashmask = hashsize - 1;
  7040. return (hashtbl);
  7041. }
  7042. #endif
  7043. #else /* __Userspace__ ifdef above sctp_soreceive */
  7044. /*
  7045. * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
  7046. * NOTE: We don't want multiple definitions here. So sctp_hashinit_flags() above for
  7047. *__FreeBSD__ must be excluded.
  7048. *
  7049. */
  7050. void *
  7051. sctp_hashinit_flags(int elements, struct malloc_type *type,
  7052. u_long *hashmask, int flags)
  7053. {
  7054. long hashsize;
  7055. LIST_HEAD(generic, generic) *hashtbl;
  7056. int i;
  7057. if (elements <= 0) {
  7058. SCTP_PRINTF("hashinit: bad elements?");
  7059. #ifdef INVARIANTS
  7060. return (NULL);
  7061. #else
  7062. elements = 1;
  7063. #endif
  7064. }
  7065. for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
  7066. continue;
  7067. hashsize >>= 1;
  7068. /*cannot use MALLOC here because it has to be declared or defined
  7069. using MALLOC_DECLARE or MALLOC_DEFINE first. */
  7070. if (flags & HASH_WAITOK)
  7071. hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
  7072. else if (flags & HASH_NOWAIT)
  7073. hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
  7074. else {
  7075. #ifdef INVARIANTS
  7076. SCTP_PRINTF("flag incorrect in hashinit_flags.\n");
  7077. #endif
  7078. return (NULL);
  7079. }
  7080. /* no memory? */
  7081. if (hashtbl == NULL)
  7082. return (NULL);
  7083. for (i = 0; i < hashsize; i++)
  7084. LIST_INIT(&hashtbl[i]);
  7085. *hashmask = hashsize - 1;
  7086. return (hashtbl);
  7087. }
  7088. void
  7089. sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
  7090. {
  7091. LIST_HEAD(generic, generic) *hashtbl, *hp;
  7092. hashtbl = vhashtbl;
  7093. for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
  7094. if (!LIST_EMPTY(hp)) {
  7095. SCTP_PRINTF("hashdestroy: hash not empty.\n");
  7096. return;
  7097. }
  7098. FREE(hashtbl, type);
  7099. }
  7100. void
  7101. sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
  7102. {
  7103. LIST_HEAD(generic, generic) *hashtbl/*, *hp*/;
  7104. /*
  7105. LIST_ENTRY(type) *start, *temp;
  7106. */
  7107. hashtbl = vhashtbl;
  7108. /* Apparently temp is not dynamically allocated, so attempts to
  7109. free it results in error.
  7110. for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
  7111. if (!LIST_EMPTY(hp)) {
  7112. start = LIST_FIRST(hp);
  7113. while (start != NULL) {
  7114. temp = start;
  7115. start = start->le_next;
  7116. SCTP_PRINTF("%s: %p \n", __func__, (void *)temp);
  7117. FREE(temp, type);
  7118. }
  7119. }
  7120. */
  7121. FREE(hashtbl, type);
  7122. }
  7123. #endif
  7124. int
  7125. sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
  7126. int totaddr, int *error)
  7127. {
  7128. int added = 0;
  7129. int i;
  7130. struct sctp_inpcb *inp;
  7131. struct sockaddr *sa;
  7132. size_t incr = 0;
  7133. #ifdef INET
  7134. struct sockaddr_in *sin;
  7135. #endif
  7136. #ifdef INET6
  7137. struct sockaddr_in6 *sin6;
  7138. #endif
  7139. sa = addr;
  7140. inp = stcb->sctp_ep;
  7141. *error = 0;
  7142. for (i = 0; i < totaddr; i++) {
  7143. switch (sa->sa_family) {
  7144. #ifdef INET
  7145. case AF_INET:
  7146. incr = sizeof(struct sockaddr_in);
  7147. sin = (struct sockaddr_in *)sa;
  7148. if ((sin->sin_addr.s_addr == INADDR_ANY) ||
  7149. (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
  7150. IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
  7151. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7152. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  7153. SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
  7154. *error = EINVAL;
  7155. goto out_now;
  7156. }
  7157. if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
  7158. SCTP_DONOT_SETSCOPE,
  7159. SCTP_ADDR_IS_CONFIRMED)) {
  7160. /* assoc gone no un-lock */
  7161. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
  7162. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  7163. SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
  7164. *error = ENOBUFS;
  7165. goto out_now;
  7166. }
  7167. added++;
  7168. break;
  7169. #endif
  7170. #ifdef INET6
  7171. case AF_INET6:
  7172. incr = sizeof(struct sockaddr_in6);
  7173. sin6 = (struct sockaddr_in6 *)sa;
  7174. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
  7175. IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
  7176. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7177. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  7178. SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
  7179. *error = EINVAL;
  7180. goto out_now;
  7181. }
  7182. if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
  7183. SCTP_DONOT_SETSCOPE,
  7184. SCTP_ADDR_IS_CONFIRMED)) {
  7185. /* assoc gone no un-lock */
  7186. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
  7187. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  7188. SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
  7189. *error = ENOBUFS;
  7190. goto out_now;
  7191. }
  7192. added++;
  7193. break;
  7194. #endif
  7195. #if defined(__Userspace__)
  7196. case AF_CONN:
  7197. incr = sizeof(struct sockaddr_conn);
  7198. if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
  7199. SCTP_DONOT_SETSCOPE,
  7200. SCTP_ADDR_IS_CONFIRMED)) {
  7201. /* assoc gone no un-lock */
  7202. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
  7203. (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  7204. SCTP_FROM_SCTPUTIL + SCTP_LOC_11);
  7205. *error = ENOBUFS;
  7206. goto out_now;
  7207. }
  7208. added++;
  7209. break;
  7210. #endif
  7211. default:
  7212. break;
  7213. }
  7214. sa = (struct sockaddr *)((caddr_t)sa + incr);
  7215. }
  7216. out_now:
  7217. return (added);
  7218. }
  7219. int
  7220. sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
  7221. unsigned int totaddr,
  7222. unsigned int *num_v4, unsigned int *num_v6,
  7223. unsigned int limit)
  7224. {
  7225. struct sockaddr *sa;
  7226. struct sctp_tcb *stcb;
  7227. unsigned int incr, at, i;
  7228. at = 0;
  7229. sa = addr;
  7230. *num_v6 = *num_v4 = 0;
  7231. /* account and validate addresses */
  7232. if (totaddr == 0) {
  7233. return (EINVAL);
  7234. }
  7235. for (i = 0; i < totaddr; i++) {
  7236. if (at + sizeof(struct sockaddr) > limit) {
  7237. return (EINVAL);
  7238. }
  7239. switch (sa->sa_family) {
  7240. #ifdef INET
  7241. case AF_INET:
  7242. incr = (unsigned int)sizeof(struct sockaddr_in);
  7243. #ifdef HAVE_SA_LEN
  7244. if (sa->sa_len != incr) {
  7245. return (EINVAL);
  7246. }
  7247. #endif
  7248. (*num_v4) += 1;
  7249. break;
  7250. #endif
  7251. #ifdef INET6
  7252. case AF_INET6:
  7253. {
  7254. struct sockaddr_in6 *sin6;
  7255. incr = (unsigned int)sizeof(struct sockaddr_in6);
  7256. #ifdef HAVE_SA_LEN
  7257. if (sa->sa_len != incr) {
  7258. return (EINVAL);
  7259. }
  7260. #endif
  7261. sin6 = (struct sockaddr_in6 *)sa;
  7262. if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  7263. /* Must be non-mapped for connectx */
  7264. return (EINVAL);
  7265. }
  7266. (*num_v6) += 1;
  7267. break;
  7268. }
  7269. #endif
  7270. default:
  7271. return (EINVAL);
  7272. }
  7273. if ((at + incr) > limit) {
  7274. return (EINVAL);
  7275. }
  7276. SCTP_INP_INCR_REF(inp);
  7277. stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
  7278. if (stcb != NULL) {
  7279. SCTP_TCB_UNLOCK(stcb);
  7280. return (EALREADY);
  7281. } else {
  7282. SCTP_INP_DECR_REF(inp);
  7283. }
  7284. at += incr;
  7285. sa = (struct sockaddr *)((caddr_t)sa + incr);
  7286. }
  7287. return (0);
  7288. }
  7289. /*
  7290. * sctp_bindx(ADD) for one address.
  7291. * assumes all arguments are valid/checked by caller.
  7292. */
  7293. void
  7294. sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
  7295. struct sockaddr *sa, uint32_t vrf_id, int *error,
  7296. void *p)
  7297. {
  7298. #if defined(INET) && defined(INET6)
  7299. struct sockaddr_in sin;
  7300. #endif
  7301. #ifdef INET6
  7302. struct sockaddr_in6 *sin6;
  7303. #endif
  7304. #ifdef INET
  7305. struct sockaddr_in *sinp;
  7306. #endif
  7307. struct sockaddr *addr_to_use;
  7308. struct sctp_inpcb *lep;
  7309. #ifdef SCTP_MVRF
  7310. int i;
  7311. #endif
  7312. uint16_t port;
  7313. /* see if we're bound all already! */
  7314. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  7315. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7316. *error = EINVAL;
  7317. return;
  7318. }
  7319. #ifdef SCTP_MVRF
  7320. /* Is the VRF one we have */
  7321. for (i = 0; i < inp->num_vrfs; i++) {
  7322. if (vrf_id == inp->m_vrf_ids[i]) {
  7323. break;
  7324. }
  7325. }
  7326. if (i == inp->num_vrfs) {
  7327. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7328. *error = EINVAL;
  7329. return;
  7330. }
  7331. #endif
  7332. switch (sa->sa_family) {
  7333. #ifdef INET6
  7334. case AF_INET6:
  7335. #ifdef HAVE_SA_LEN
  7336. if (sa->sa_len != sizeof(struct sockaddr_in6)) {
  7337. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7338. *error = EINVAL;
  7339. return;
  7340. }
  7341. #endif
  7342. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
  7343. /* can only bind v6 on PF_INET6 sockets */
  7344. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7345. *error = EINVAL;
  7346. return;
  7347. }
  7348. sin6 = (struct sockaddr_in6 *)sa;
  7349. port = sin6->sin6_port;
  7350. #ifdef INET
  7351. if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  7352. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  7353. SCTP_IPV6_V6ONLY(inp)) {
  7354. /* can't bind v4-mapped on PF_INET sockets */
  7355. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7356. *error = EINVAL;
  7357. return;
  7358. }
  7359. in6_sin6_2_sin(&sin, sin6);
  7360. addr_to_use = (struct sockaddr *)&sin;
  7361. } else {
  7362. addr_to_use = sa;
  7363. }
  7364. #else
  7365. addr_to_use = sa;
  7366. #endif
  7367. break;
  7368. #endif
  7369. #ifdef INET
  7370. case AF_INET:
  7371. #ifdef HAVE_SA_LEN
  7372. if (sa->sa_len != sizeof(struct sockaddr_in)) {
  7373. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7374. *error = EINVAL;
  7375. return;
  7376. }
  7377. #endif
  7378. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  7379. SCTP_IPV6_V6ONLY(inp)) {
  7380. /* can't bind v4 on PF_INET sockets */
  7381. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7382. *error = EINVAL;
  7383. return;
  7384. }
  7385. sinp = (struct sockaddr_in *)sa;
  7386. port = sinp->sin_port;
  7387. addr_to_use = sa;
  7388. break;
  7389. #endif
  7390. default:
  7391. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7392. *error = EINVAL;
  7393. return;
  7394. }
  7395. if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
  7396. #if !(defined(_WIN32) || defined(__Userspace__))
  7397. if (p == NULL) {
  7398. /* Can't get proc for Net/Open BSD */
  7399. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7400. *error = EINVAL;
  7401. return;
  7402. }
  7403. #endif
  7404. *error = sctp_inpcb_bind(so, addr_to_use, NULL, p);
  7405. return;
  7406. }
  7407. /* Validate the incoming port. */
  7408. if ((port != 0) && (port != inp->sctp_lport)) {
  7409. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7410. *error = EINVAL;
  7411. return;
  7412. }
  7413. lep = sctp_pcb_findep(addr_to_use, 1, 0, vrf_id);
  7414. if (lep == NULL) {
  7415. /* add the address */
  7416. *error = sctp_addr_mgmt_ep_sa(inp, addr_to_use,
  7417. SCTP_ADD_IP_ADDRESS, vrf_id);
  7418. } else {
  7419. if (lep != inp) {
  7420. *error = EADDRINUSE;
  7421. }
  7422. SCTP_INP_DECR_REF(lep);
  7423. }
  7424. }
  7425. /*
  7426. * sctp_bindx(DELETE) for one address.
  7427. * assumes all arguments are valid/checked by caller.
  7428. */
  7429. void
  7430. sctp_bindx_delete_address(struct sctp_inpcb *inp,
  7431. struct sockaddr *sa, uint32_t vrf_id, int *error)
  7432. {
  7433. struct sockaddr *addr_to_use;
  7434. #if defined(INET) && defined(INET6)
  7435. struct sockaddr_in6 *sin6;
  7436. struct sockaddr_in sin;
  7437. #endif
  7438. #ifdef SCTP_MVRF
  7439. int i;
  7440. #endif
  7441. /* see if we're bound all already! */
  7442. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  7443. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7444. *error = EINVAL;
  7445. return;
  7446. }
  7447. #ifdef SCTP_MVRF
  7448. /* Is the VRF one we have */
  7449. for (i = 0; i < inp->num_vrfs; i++) {
  7450. if (vrf_id == inp->m_vrf_ids[i]) {
  7451. break;
  7452. }
  7453. }
  7454. if (i == inp->num_vrfs) {
  7455. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7456. *error = EINVAL;
  7457. return;
  7458. }
  7459. #endif
  7460. switch (sa->sa_family) {
  7461. #ifdef INET6
  7462. case AF_INET6:
  7463. #ifdef HAVE_SA_LEN
  7464. if (sa->sa_len != sizeof(struct sockaddr_in6)) {
  7465. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7466. *error = EINVAL;
  7467. return;
  7468. }
  7469. #endif
  7470. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
  7471. /* can only bind v6 on PF_INET6 sockets */
  7472. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7473. *error = EINVAL;
  7474. return;
  7475. }
  7476. #ifdef INET
  7477. sin6 = (struct sockaddr_in6 *)sa;
  7478. if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  7479. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  7480. SCTP_IPV6_V6ONLY(inp)) {
  7481. /* can't bind mapped-v4 on PF_INET sockets */
  7482. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7483. *error = EINVAL;
  7484. return;
  7485. }
  7486. in6_sin6_2_sin(&sin, sin6);
  7487. addr_to_use = (struct sockaddr *)&sin;
  7488. } else {
  7489. addr_to_use = sa;
  7490. }
  7491. #else
  7492. addr_to_use = sa;
  7493. #endif
  7494. break;
  7495. #endif
  7496. #ifdef INET
  7497. case AF_INET:
  7498. #ifdef HAVE_SA_LEN
  7499. if (sa->sa_len != sizeof(struct sockaddr_in)) {
  7500. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7501. *error = EINVAL;
  7502. return;
  7503. }
  7504. #endif
  7505. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  7506. SCTP_IPV6_V6ONLY(inp)) {
  7507. /* can't bind v4 on PF_INET sockets */
  7508. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7509. *error = EINVAL;
  7510. return;
  7511. }
  7512. addr_to_use = sa;
  7513. break;
  7514. #endif
  7515. default:
  7516. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
  7517. *error = EINVAL;
  7518. return;
  7519. }
  7520. /* No lock required mgmt_ep_sa does its own locking. */
  7521. *error = sctp_addr_mgmt_ep_sa(inp, addr_to_use, SCTP_DEL_IP_ADDRESS,
  7522. vrf_id);
  7523. }
  7524. /*
  7525. * returns the valid local address count for an assoc, taking into account
  7526. * all scoping rules
  7527. */
  7528. int
  7529. sctp_local_addr_count(struct sctp_tcb *stcb)
  7530. {
  7531. int loopback_scope;
  7532. #if defined(INET)
  7533. int ipv4_local_scope, ipv4_addr_legal;
  7534. #endif
  7535. #if defined(INET6)
  7536. int local_scope, site_scope, ipv6_addr_legal;
  7537. #endif
  7538. #if defined(__Userspace__)
  7539. int conn_addr_legal;
  7540. #endif
  7541. struct sctp_vrf *vrf;
  7542. struct sctp_ifn *sctp_ifn;
  7543. struct sctp_ifa *sctp_ifa;
  7544. int count = 0;
  7545. /* Turn on all the appropriate scopes */
  7546. loopback_scope = stcb->asoc.scope.loopback_scope;
  7547. #if defined(INET)
  7548. ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
  7549. ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
  7550. #endif
  7551. #if defined(INET6)
  7552. local_scope = stcb->asoc.scope.local_scope;
  7553. site_scope = stcb->asoc.scope.site_scope;
  7554. ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
  7555. #endif
  7556. #if defined(__Userspace__)
  7557. conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
  7558. #endif
  7559. SCTP_IPI_ADDR_RLOCK();
  7560. vrf = sctp_find_vrf(stcb->asoc.vrf_id);
  7561. if (vrf == NULL) {
  7562. /* no vrf, no addresses */
  7563. SCTP_IPI_ADDR_RUNLOCK();
  7564. return (0);
  7565. }
  7566. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  7567. /*
  7568. * bound all case: go through all ifns on the vrf
  7569. */
  7570. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  7571. if ((loopback_scope == 0) &&
  7572. SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  7573. continue;
  7574. }
  7575. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  7576. if (sctp_is_addr_restricted(stcb, sctp_ifa))
  7577. continue;
  7578. switch (sctp_ifa->address.sa.sa_family) {
  7579. #ifdef INET
  7580. case AF_INET:
  7581. if (ipv4_addr_legal) {
  7582. struct sockaddr_in *sin;
  7583. sin = &sctp_ifa->address.sin;
  7584. if (sin->sin_addr.s_addr == 0) {
  7585. /* skip unspecified addrs */
  7586. continue;
  7587. }
  7588. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7589. if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
  7590. &sin->sin_addr) != 0) {
  7591. continue;
  7592. }
  7593. #endif
  7594. if ((ipv4_local_scope == 0) &&
  7595. (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
  7596. continue;
  7597. }
  7598. /* count this one */
  7599. count++;
  7600. } else {
  7601. continue;
  7602. }
  7603. break;
  7604. #endif
  7605. #ifdef INET6
  7606. case AF_INET6:
  7607. if (ipv6_addr_legal) {
  7608. struct sockaddr_in6 *sin6;
  7609. #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
  7610. struct sockaddr_in6 lsa6;
  7611. #endif
  7612. sin6 = &sctp_ifa->address.sin6;
  7613. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  7614. continue;
  7615. }
  7616. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7617. if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
  7618. &sin6->sin6_addr) != 0) {
  7619. continue;
  7620. }
  7621. #endif
  7622. if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  7623. if (local_scope == 0)
  7624. continue;
  7625. #if defined(SCTP_EMBEDDED_V6_SCOPE)
  7626. if (sin6->sin6_scope_id == 0) {
  7627. #ifdef SCTP_KAME
  7628. if (sa6_recoverscope(sin6) != 0)
  7629. /*
  7630. * bad link
  7631. * local
  7632. * address
  7633. */
  7634. continue;
  7635. #else
  7636. lsa6 = *sin6;
  7637. if (in6_recoverscope(&lsa6,
  7638. &lsa6.sin6_addr,
  7639. NULL))
  7640. /*
  7641. * bad link
  7642. * local
  7643. * address
  7644. */
  7645. continue;
  7646. sin6 = &lsa6;
  7647. #endif /* SCTP_KAME */
  7648. }
  7649. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  7650. }
  7651. if ((site_scope == 0) &&
  7652. (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
  7653. continue;
  7654. }
  7655. /* count this one */
  7656. count++;
  7657. }
  7658. break;
  7659. #endif
  7660. #if defined(__Userspace__)
  7661. case AF_CONN:
  7662. if (conn_addr_legal) {
  7663. count++;
  7664. }
  7665. break;
  7666. #endif
  7667. default:
  7668. /* TSNH */
  7669. break;
  7670. }
  7671. }
  7672. }
  7673. } else {
  7674. /*
  7675. * subset bound case
  7676. */
  7677. struct sctp_laddr *laddr;
  7678. LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
  7679. sctp_nxt_addr) {
  7680. if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
  7681. continue;
  7682. }
  7683. /* count this one */
  7684. count++;
  7685. }
  7686. }
  7687. SCTP_IPI_ADDR_RUNLOCK();
  7688. return (count);
  7689. }
  7690. #if defined(SCTP_LOCAL_TRACE_BUF)
  7691. void
  7692. sctp_log_trace(uint32_t subsys, const char *str SCTP_UNUSED, uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
  7693. {
  7694. uint32_t saveindex, newindex;
  7695. #if defined(_WIN32) && !defined(__Userspace__)
  7696. if (SCTP_BASE_SYSCTL(sctp_log) == NULL) {
  7697. return;
  7698. }
  7699. do {
  7700. saveindex = SCTP_BASE_SYSCTL(sctp_log)->index;
  7701. if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
  7702. newindex = 1;
  7703. } else {
  7704. newindex = saveindex + 1;
  7705. }
  7706. } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log)->index, saveindex, newindex) == 0);
  7707. if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
  7708. saveindex = 0;
  7709. }
  7710. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
  7711. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].subsys = subsys;
  7712. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[0] = a;
  7713. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[1] = b;
  7714. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[2] = c;
  7715. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[3] = d;
  7716. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[4] = e;
  7717. SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[5] = f;
  7718. #else
  7719. do {
  7720. saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
  7721. if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
  7722. newindex = 1;
  7723. } else {
  7724. newindex = saveindex + 1;
  7725. }
  7726. } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
  7727. if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
  7728. saveindex = 0;
  7729. }
  7730. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
  7731. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
  7732. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
  7733. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
  7734. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
  7735. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
  7736. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
  7737. SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
  7738. #endif
  7739. }
  7740. #endif
  7741. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7742. static bool
  7743. sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
  7744. const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
  7745. {
  7746. struct ip *iph;
  7747. #ifdef INET6
  7748. struct ip6_hdr *ip6;
  7749. #endif
  7750. struct mbuf *sp, *last;
  7751. struct udphdr *uhdr;
  7752. uint16_t port;
  7753. if ((m->m_flags & M_PKTHDR) == 0) {
  7754. /* Can't handle one that is not a pkt hdr */
  7755. goto out;
  7756. }
  7757. /* Pull the src port */
  7758. iph = mtod(m, struct ip *);
  7759. uhdr = (struct udphdr *)((caddr_t)iph + off);
  7760. port = uhdr->uh_sport;
  7761. /* Split out the mbuf chain. Leave the
  7762. * IP header in m, place the
  7763. * rest in the sp.
  7764. */
  7765. sp = m_split(m, off, M_NOWAIT);
  7766. if (sp == NULL) {
  7767. /* Gak, drop packet, we can't do a split */
  7768. goto out;
  7769. }
  7770. if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
  7771. /* Gak, packet can't have an SCTP header in it - too small */
  7772. m_freem(sp);
  7773. goto out;
  7774. }
  7775. /* Now pull up the UDP header and SCTP header together */
  7776. sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
  7777. if (sp == NULL) {
  7778. /* Gak pullup failed */
  7779. goto out;
  7780. }
  7781. /* Trim out the UDP header */
  7782. m_adj(sp, sizeof(struct udphdr));
  7783. /* Now reconstruct the mbuf chain */
  7784. for (last = m; last->m_next; last = last->m_next);
  7785. last->m_next = sp;
  7786. m->m_pkthdr.len += sp->m_pkthdr.len;
  7787. /*
  7788. * The CSUM_DATA_VALID flags indicates that the HW checked the
  7789. * UDP checksum and it was valid.
  7790. * Since CSUM_DATA_VALID == CSUM_SCTP_VALID this would imply that
  7791. * the HW also verified the SCTP checksum. Therefore, clear the bit.
  7792. */
  7793. SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
  7794. "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
  7795. m->m_pkthdr.len,
  7796. if_name(m->m_pkthdr.rcvif),
  7797. (int)m->m_pkthdr.csum_flags, CSUM_BITS);
  7798. m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
  7799. iph = mtod(m, struct ip *);
  7800. switch (iph->ip_v) {
  7801. #ifdef INET
  7802. case IPVERSION:
  7803. iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
  7804. sctp_input_with_port(m, off, port);
  7805. break;
  7806. #endif
  7807. #ifdef INET6
  7808. case IPV6_VERSION >> 4:
  7809. ip6 = mtod(m, struct ip6_hdr *);
  7810. ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
  7811. sctp6_input_with_port(&m, &off, port);
  7812. break;
  7813. #endif
  7814. default:
  7815. goto out;
  7816. break;
  7817. }
  7818. return (true);
  7819. out:
  7820. m_freem(m);
  7821. return (true);
  7822. }
  7823. #ifdef INET
  7824. static void
  7825. sctp_recv_icmp_tunneled_packet(int cmd, struct sockaddr *sa, void *vip, void *ctx SCTP_UNUSED)
  7826. {
  7827. struct ip *outer_ip, *inner_ip;
  7828. struct sctphdr *sh;
  7829. struct icmp *icmp;
  7830. struct udphdr *udp;
  7831. struct sctp_inpcb *inp;
  7832. struct sctp_tcb *stcb;
  7833. struct sctp_nets *net;
  7834. struct sctp_init_chunk *ch;
  7835. struct sockaddr_in src, dst;
  7836. uint8_t type, code;
  7837. inner_ip = (struct ip *)vip;
  7838. icmp = (struct icmp *)((caddr_t)inner_ip -
  7839. (sizeof(struct icmp) - sizeof(struct ip)));
  7840. outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
  7841. if (ntohs(outer_ip->ip_len) <
  7842. sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
  7843. return;
  7844. }
  7845. udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
  7846. sh = (struct sctphdr *)(udp + 1);
  7847. memset(&src, 0, sizeof(struct sockaddr_in));
  7848. src.sin_family = AF_INET;
  7849. #ifdef HAVE_SIN_LEN
  7850. src.sin_len = sizeof(struct sockaddr_in);
  7851. #endif
  7852. src.sin_port = sh->src_port;
  7853. src.sin_addr = inner_ip->ip_src;
  7854. memset(&dst, 0, sizeof(struct sockaddr_in));
  7855. dst.sin_family = AF_INET;
  7856. #ifdef HAVE_SIN_LEN
  7857. dst.sin_len = sizeof(struct sockaddr_in);
  7858. #endif
  7859. dst.sin_port = sh->dest_port;
  7860. dst.sin_addr = inner_ip->ip_dst;
  7861. /*
  7862. * 'dst' holds the dest of the packet that failed to be sent.
  7863. * 'src' holds our local endpoint address. Thus we reverse
  7864. * the dst and the src in the lookup.
  7865. */
  7866. inp = NULL;
  7867. net = NULL;
  7868. stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
  7869. (struct sockaddr *)&src,
  7870. &inp, &net, 1,
  7871. SCTP_DEFAULT_VRFID);
  7872. if ((stcb != NULL) &&
  7873. (net != NULL) &&
  7874. (inp != NULL)) {
  7875. /* Check the UDP port numbers */
  7876. if ((udp->uh_dport != net->port) ||
  7877. (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
  7878. SCTP_TCB_UNLOCK(stcb);
  7879. return;
  7880. }
  7881. /* Check the verification tag */
  7882. if (ntohl(sh->v_tag) != 0) {
  7883. /*
  7884. * This must be the verification tag used
  7885. * for sending out packets. We don't
  7886. * consider packets reflecting the
  7887. * verification tag.
  7888. */
  7889. if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
  7890. SCTP_TCB_UNLOCK(stcb);
  7891. return;
  7892. }
  7893. } else {
  7894. if (ntohs(outer_ip->ip_len) >=
  7895. sizeof(struct ip) +
  7896. 8 + (inner_ip->ip_hl << 2) + 8 + 20) {
  7897. /*
  7898. * In this case we can check if we
  7899. * got an INIT chunk and if the
  7900. * initiate tag matches.
  7901. */
  7902. ch = (struct sctp_init_chunk *)(sh + 1);
  7903. if ((ch->ch.chunk_type != SCTP_INITIATION) ||
  7904. (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
  7905. SCTP_TCB_UNLOCK(stcb);
  7906. return;
  7907. }
  7908. } else {
  7909. SCTP_TCB_UNLOCK(stcb);
  7910. return;
  7911. }
  7912. }
  7913. type = icmp->icmp_type;
  7914. code = icmp->icmp_code;
  7915. if ((type == ICMP_UNREACH) &&
  7916. (code == ICMP_UNREACH_PORT)) {
  7917. code = ICMP_UNREACH_PROTOCOL;
  7918. }
  7919. sctp_notify(inp, stcb, net, type, code,
  7920. ntohs(inner_ip->ip_len),
  7921. (uint32_t)ntohs(icmp->icmp_nextmtu));
  7922. #if defined(__Userspace__)
  7923. if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  7924. (stcb->sctp_socket != NULL)) {
  7925. struct socket *upcall_socket;
  7926. upcall_socket = stcb->sctp_socket;
  7927. SOCK_LOCK(upcall_socket);
  7928. soref(upcall_socket);
  7929. SOCK_UNLOCK(upcall_socket);
  7930. if ((upcall_socket->so_upcall != NULL) &&
  7931. (upcall_socket->so_error != 0)) {
  7932. (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
  7933. }
  7934. ACCEPT_LOCK();
  7935. SOCK_LOCK(upcall_socket);
  7936. sorele(upcall_socket);
  7937. }
  7938. #endif
  7939. } else {
  7940. if ((stcb == NULL) && (inp != NULL)) {
  7941. /* reduce ref-count */
  7942. SCTP_INP_WLOCK(inp);
  7943. SCTP_INP_DECR_REF(inp);
  7944. SCTP_INP_WUNLOCK(inp);
  7945. }
  7946. if (stcb) {
  7947. SCTP_TCB_UNLOCK(stcb);
  7948. }
  7949. }
  7950. return;
  7951. }
  7952. #endif
  7953. #ifdef INET6
  7954. static void
  7955. sctp_recv_icmp6_tunneled_packet(int cmd, struct sockaddr *sa, void *d, void *ctx SCTP_UNUSED)
  7956. {
  7957. struct ip6ctlparam *ip6cp;
  7958. struct sctp_inpcb *inp;
  7959. struct sctp_tcb *stcb;
  7960. struct sctp_nets *net;
  7961. struct sctphdr sh;
  7962. struct udphdr udp;
  7963. struct sockaddr_in6 src, dst;
  7964. uint8_t type, code;
  7965. ip6cp = (struct ip6ctlparam *)d;
  7966. /*
  7967. * XXX: We assume that when IPV6 is non NULL, M and OFF are
  7968. * valid.
  7969. */
  7970. if (ip6cp->ip6c_m == NULL) {
  7971. return;
  7972. }
  7973. /* Check if we can safely examine the ports and the
  7974. * verification tag of the SCTP common header.
  7975. */
  7976. if (ip6cp->ip6c_m->m_pkthdr.len <
  7977. ip6cp->ip6c_off + sizeof(struct udphdr)+ offsetof(struct sctphdr, checksum)) {
  7978. return;
  7979. }
  7980. /* Copy out the UDP header. */
  7981. memset(&udp, 0, sizeof(struct udphdr));
  7982. m_copydata(ip6cp->ip6c_m,
  7983. ip6cp->ip6c_off,
  7984. sizeof(struct udphdr),
  7985. (caddr_t)&udp);
  7986. /* Copy out the port numbers and the verification tag. */
  7987. memset(&sh, 0, sizeof(struct sctphdr));
  7988. m_copydata(ip6cp->ip6c_m,
  7989. ip6cp->ip6c_off + sizeof(struct udphdr),
  7990. sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
  7991. (caddr_t)&sh);
  7992. memset(&src, 0, sizeof(struct sockaddr_in6));
  7993. src.sin6_family = AF_INET6;
  7994. #ifdef HAVE_SIN6_LEN
  7995. src.sin6_len = sizeof(struct sockaddr_in6);
  7996. #endif
  7997. src.sin6_port = sh.src_port;
  7998. src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
  7999. #if defined(__FreeBSD__) && !defined(__Userspace__)
  8000. if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
  8001. return;
  8002. }
  8003. #endif
  8004. memset(&dst, 0, sizeof(struct sockaddr_in6));
  8005. dst.sin6_family = AF_INET6;
  8006. #ifdef HAVE_SIN6_LEN
  8007. dst.sin6_len = sizeof(struct sockaddr_in6);
  8008. #endif
  8009. dst.sin6_port = sh.dest_port;
  8010. dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
  8011. #if defined(__FreeBSD__) && !defined(__Userspace__)
  8012. if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
  8013. return;
  8014. }
  8015. #endif
  8016. inp = NULL;
  8017. net = NULL;
  8018. stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
  8019. (struct sockaddr *)&src,
  8020. &inp, &net, 1, SCTP_DEFAULT_VRFID);
  8021. if ((stcb != NULL) &&
  8022. (net != NULL) &&
  8023. (inp != NULL)) {
  8024. /* Check the UDP port numbers */
  8025. if ((udp.uh_dport != net->port) ||
  8026. (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
  8027. SCTP_TCB_UNLOCK(stcb);
  8028. return;
  8029. }
  8030. /* Check the verification tag */
  8031. if (ntohl(sh.v_tag) != 0) {
  8032. /*
  8033. * This must be the verification tag used for
  8034. * sending out packets. We don't consider
  8035. * packets reflecting the verification tag.
  8036. */
  8037. if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
  8038. SCTP_TCB_UNLOCK(stcb);
  8039. return;
  8040. }
  8041. } else {
  8042. #if defined(__FreeBSD__) && !defined(__Userspace__)
  8043. if (ip6cp->ip6c_m->m_pkthdr.len >=
  8044. ip6cp->ip6c_off + sizeof(struct udphdr) +
  8045. sizeof(struct sctphdr) +
  8046. sizeof(struct sctp_chunkhdr) +
  8047. offsetof(struct sctp_init, a_rwnd)) {
  8048. /*
  8049. * In this case we can check if we
  8050. * got an INIT chunk and if the
  8051. * initiate tag matches.
  8052. */
  8053. uint32_t initiate_tag;
  8054. uint8_t chunk_type;
  8055. m_copydata(ip6cp->ip6c_m,
  8056. ip6cp->ip6c_off +
  8057. sizeof(struct udphdr) +
  8058. sizeof(struct sctphdr),
  8059. sizeof(uint8_t),
  8060. (caddr_t)&chunk_type);
  8061. m_copydata(ip6cp->ip6c_m,
  8062. ip6cp->ip6c_off +
  8063. sizeof(struct udphdr) +
  8064. sizeof(struct sctphdr) +
  8065. sizeof(struct sctp_chunkhdr),
  8066. sizeof(uint32_t),
  8067. (caddr_t)&initiate_tag);
  8068. if ((chunk_type != SCTP_INITIATION) ||
  8069. (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
  8070. SCTP_TCB_UNLOCK(stcb);
  8071. return;
  8072. }
  8073. } else {
  8074. SCTP_TCB_UNLOCK(stcb);
  8075. return;
  8076. }
  8077. #else
  8078. SCTP_TCB_UNLOCK(stcb);
  8079. return;
  8080. #endif
  8081. }
  8082. type = ip6cp->ip6c_icmp6->icmp6_type;
  8083. code = ip6cp->ip6c_icmp6->icmp6_code;
  8084. if ((type == ICMP6_DST_UNREACH) &&
  8085. (code == ICMP6_DST_UNREACH_NOPORT)) {
  8086. type = ICMP6_PARAM_PROB;
  8087. code = ICMP6_PARAMPROB_NEXTHEADER;
  8088. }
  8089. sctp6_notify(inp, stcb, net, type, code,
  8090. ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
  8091. #if defined(__Userspace__)
  8092. if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
  8093. (stcb->sctp_socket != NULL)) {
  8094. struct socket *upcall_socket;
  8095. upcall_socket = stcb->sctp_socket;
  8096. SOCK_LOCK(upcall_socket);
  8097. soref(upcall_socket);
  8098. SOCK_UNLOCK(upcall_socket);
  8099. if ((upcall_socket->so_upcall != NULL) &&
  8100. (upcall_socket->so_error != 0)) {
  8101. (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
  8102. }
  8103. ACCEPT_LOCK();
  8104. SOCK_LOCK(upcall_socket);
  8105. sorele(upcall_socket);
  8106. }
  8107. #endif
  8108. } else {
  8109. if ((stcb == NULL) && (inp != NULL)) {
  8110. /* reduce inp's ref-count */
  8111. SCTP_INP_WLOCK(inp);
  8112. SCTP_INP_DECR_REF(inp);
  8113. SCTP_INP_WUNLOCK(inp);
  8114. }
  8115. if (stcb) {
  8116. SCTP_TCB_UNLOCK(stcb);
  8117. }
  8118. }
  8119. }
  8120. #endif
  8121. void
  8122. sctp_over_udp_stop(void)
  8123. {
  8124. /*
  8125. * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writing!
  8126. */
  8127. #ifdef INET
  8128. if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
  8129. soclose(SCTP_BASE_INFO(udp4_tun_socket));
  8130. SCTP_BASE_INFO(udp4_tun_socket) = NULL;
  8131. }
  8132. #endif
  8133. #ifdef INET6
  8134. if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
  8135. soclose(SCTP_BASE_INFO(udp6_tun_socket));
  8136. SCTP_BASE_INFO(udp6_tun_socket) = NULL;
  8137. }
  8138. #endif
  8139. }
  8140. int
  8141. sctp_over_udp_start(void)
  8142. {
  8143. uint16_t port;
  8144. int ret;
  8145. #ifdef INET
  8146. struct sockaddr_in sin;
  8147. #endif
  8148. #ifdef INET6
  8149. struct sockaddr_in6 sin6;
  8150. #endif
  8151. /*
  8152. * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writing!
  8153. */
  8154. port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
  8155. if (ntohs(port) == 0) {
  8156. /* Must have a port set */
  8157. return (EINVAL);
  8158. }
  8159. #ifdef INET
  8160. if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
  8161. /* Already running -- must stop first */
  8162. return (EALREADY);
  8163. }
  8164. #endif
  8165. #ifdef INET6
  8166. if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
  8167. /* Already running -- must stop first */
  8168. return (EALREADY);
  8169. }
  8170. #endif
  8171. #ifdef INET
  8172. if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
  8173. SOCK_DGRAM, IPPROTO_UDP,
  8174. curthread->td_ucred, curthread))) {
  8175. sctp_over_udp_stop();
  8176. return (ret);
  8177. }
  8178. /* Call the special UDP hook. */
  8179. if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
  8180. sctp_recv_udp_tunneled_packet,
  8181. sctp_recv_icmp_tunneled_packet,
  8182. NULL))) {
  8183. sctp_over_udp_stop();
  8184. return (ret);
  8185. }
  8186. /* Ok, we have a socket, bind it to the port. */
  8187. memset(&sin, 0, sizeof(struct sockaddr_in));
  8188. sin.sin_len = sizeof(struct sockaddr_in);
  8189. sin.sin_family = AF_INET;
  8190. sin.sin_port = htons(port);
  8191. if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
  8192. (struct sockaddr *)&sin, curthread))) {
  8193. sctp_over_udp_stop();
  8194. return (ret);
  8195. }
  8196. #endif
  8197. #ifdef INET6
  8198. if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
  8199. SOCK_DGRAM, IPPROTO_UDP,
  8200. curthread->td_ucred, curthread))) {
  8201. sctp_over_udp_stop();
  8202. return (ret);
  8203. }
  8204. /* Call the special UDP hook. */
  8205. if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
  8206. sctp_recv_udp_tunneled_packet,
  8207. sctp_recv_icmp6_tunneled_packet,
  8208. NULL))) {
  8209. sctp_over_udp_stop();
  8210. return (ret);
  8211. }
  8212. /* Ok, we have a socket, bind it to the port. */
  8213. memset(&sin6, 0, sizeof(struct sockaddr_in6));
  8214. sin6.sin6_len = sizeof(struct sockaddr_in6);
  8215. sin6.sin6_family = AF_INET6;
  8216. sin6.sin6_port = htons(port);
  8217. if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
  8218. (struct sockaddr *)&sin6, curthread))) {
  8219. sctp_over_udp_stop();
  8220. return (ret);
  8221. }
  8222. #endif
  8223. return (0);
  8224. }
  8225. #endif
  8226. /*
  8227. * sctp_min_mtu ()returns the minimum of all non-zero arguments.
  8228. * If all arguments are zero, zero is returned.
  8229. */
  8230. uint32_t
  8231. sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
  8232. {
  8233. if (mtu1 > 0) {
  8234. if (mtu2 > 0) {
  8235. if (mtu3 > 0) {
  8236. return (min(mtu1, min(mtu2, mtu3)));
  8237. } else {
  8238. return (min(mtu1, mtu2));
  8239. }
  8240. } else {
  8241. if (mtu3 > 0) {
  8242. return (min(mtu1, mtu3));
  8243. } else {
  8244. return (mtu1);
  8245. }
  8246. }
  8247. } else {
  8248. if (mtu2 > 0) {
  8249. if (mtu3 > 0) {
  8250. return (min(mtu2, mtu3));
  8251. } else {
  8252. return (mtu2);
  8253. }
  8254. } else {
  8255. return (mtu3);
  8256. }
  8257. }
  8258. }
  8259. #if defined(__FreeBSD__) && !defined(__Userspace__)
  8260. void
  8261. sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
  8262. {
  8263. struct in_conninfo inc;
  8264. memset(&inc, 0, sizeof(struct in_conninfo));
  8265. inc.inc_fibnum = fibnum;
  8266. switch (addr->sa.sa_family) {
  8267. #ifdef INET
  8268. case AF_INET:
  8269. inc.inc_faddr = addr->sin.sin_addr;
  8270. break;
  8271. #endif
  8272. #ifdef INET6
  8273. case AF_INET6:
  8274. inc.inc_flags |= INC_ISIPV6;
  8275. inc.inc6_faddr = addr->sin6.sin6_addr;
  8276. break;
  8277. #endif
  8278. default:
  8279. return;
  8280. }
  8281. tcp_hc_updatemtu(&inc, (u_long)mtu);
  8282. }
  8283. uint32_t
  8284. sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
  8285. {
  8286. struct in_conninfo inc;
  8287. memset(&inc, 0, sizeof(struct in_conninfo));
  8288. inc.inc_fibnum = fibnum;
  8289. switch (addr->sa.sa_family) {
  8290. #ifdef INET
  8291. case AF_INET:
  8292. inc.inc_faddr = addr->sin.sin_addr;
  8293. break;
  8294. #endif
  8295. #ifdef INET6
  8296. case AF_INET6:
  8297. inc.inc_flags |= INC_ISIPV6;
  8298. inc.inc6_faddr = addr->sin6.sin6_addr;
  8299. break;
  8300. #endif
  8301. default:
  8302. return (0);
  8303. }
  8304. return ((uint32_t)tcp_hc_getmtu(&inc));
  8305. }
  8306. #endif
  8307. void
  8308. sctp_set_state(struct sctp_tcb *stcb, int new_state)
  8309. {
  8310. #if defined(KDTRACE_HOOKS)
  8311. int old_state = stcb->asoc.state;
  8312. #endif
  8313. KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
  8314. ("sctp_set_state: Can't set substate (new_state = %x)",
  8315. new_state));
  8316. stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
  8317. if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
  8318. (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
  8319. (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  8320. SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
  8321. }
  8322. #if defined(KDTRACE_HOOKS)
  8323. if (((old_state & SCTP_STATE_MASK) != new_state) &&
  8324. !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
  8325. (new_state == SCTP_STATE_INUSE))) {
  8326. SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
  8327. }
  8328. #endif
  8329. }
  8330. void
  8331. sctp_add_substate(struct sctp_tcb *stcb, int substate)
  8332. {
  8333. #if defined(KDTRACE_HOOKS)
  8334. int old_state = stcb->asoc.state;
  8335. #endif
  8336. KASSERT((substate & SCTP_STATE_MASK) == 0,
  8337. ("sctp_add_substate: Can't set state (substate = %x)",
  8338. substate));
  8339. stcb->asoc.state |= substate;
  8340. #if defined(KDTRACE_HOOKS)
  8341. if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
  8342. ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
  8343. ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
  8344. ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
  8345. SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
  8346. }
  8347. #endif
  8348. }