sctp_pcb.c 229 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109
  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. #if defined(__FreeBSD__) && !defined(__Userspace__)
  40. #include <sys/proc.h>
  41. #endif
  42. #include <netinet/sctp_var.h>
  43. #include <netinet/sctp_sysctl.h>
  44. #include <netinet/sctp_pcb.h>
  45. #include <netinet/sctputil.h>
  46. #include <netinet/sctp.h>
  47. #include <netinet/sctp_header.h>
  48. #include <netinet/sctp_asconf.h>
  49. #include <netinet/sctp_output.h>
  50. #include <netinet/sctp_timer.h>
  51. #include <netinet/sctp_bsd_addr.h>
  52. #if defined(INET) || defined(INET6)
  53. #if !defined(_WIN32)
  54. #include <netinet/udp.h>
  55. #endif
  56. #endif
  57. #ifdef INET6
  58. #if defined(__Userspace__)
  59. #include "user_ip6_var.h"
  60. #else
  61. #include <netinet6/ip6_var.h>
  62. #endif
  63. #endif
  64. #if defined(__FreeBSD__) && !defined(__Userspace__)
  65. #include <sys/sched.h>
  66. #include <sys/smp.h>
  67. #include <sys/unistd.h>
  68. #endif
  69. #if defined(__Userspace__)
  70. #include <user_socketvar.h>
  71. #include <user_atomic.h>
  72. #if !defined(_WIN32)
  73. #include <netdb.h>
  74. #endif
  75. #endif
  76. #if !defined(__FreeBSD__) || defined(__Userspace__)
  77. struct sctp_base_info system_base_info;
  78. #endif
  79. /* FIX: we don't handle multiple link local scopes */
  80. /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
  81. #ifdef INET6
  82. int
  83. SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
  84. {
  85. #ifdef SCTP_EMBEDDED_V6_SCOPE
  86. #if defined(__APPLE__) && !defined(__Userspace__)
  87. struct in6_addr tmp_a, tmp_b;
  88. tmp_a = a->sin6_addr;
  89. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  90. if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) {
  91. #else
  92. if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) {
  93. #endif
  94. return (0);
  95. }
  96. tmp_b = b->sin6_addr;
  97. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  98. if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) {
  99. #else
  100. if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) {
  101. #endif
  102. return (0);
  103. }
  104. return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
  105. #elif defined(SCTP_KAME)
  106. struct sockaddr_in6 tmp_a, tmp_b;
  107. memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
  108. if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
  109. return (0);
  110. }
  111. memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
  112. if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
  113. return (0);
  114. }
  115. return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
  116. #else
  117. struct in6_addr tmp_a, tmp_b;
  118. tmp_a = a->sin6_addr;
  119. if (in6_embedscope(&tmp_a, a) != 0) {
  120. return (0);
  121. }
  122. tmp_b = b->sin6_addr;
  123. if (in6_embedscope(&tmp_b, b) != 0) {
  124. return (0);
  125. }
  126. return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
  127. #endif
  128. #else
  129. return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr)));
  130. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  131. }
  132. #endif
  133. void
  134. sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
  135. {
  136. /*
  137. * We really don't need to lock this, but I will just because it
  138. * does not hurt.
  139. */
  140. SCTP_INP_INFO_RLOCK();
  141. spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
  142. spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
  143. spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
  144. spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
  145. spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
  146. spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
  147. spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
  148. spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
  149. SCTP_INP_INFO_RUNLOCK();
  150. }
  151. /*-
  152. * Addresses are added to VRF's (Virtual Router's). For BSD we
  153. * have only the default VRF 0. We maintain a hash list of
  154. * VRF's. Each VRF has its own list of sctp_ifn's. Each of
  155. * these has a list of addresses. When we add a new address
  156. * to a VRF we lookup the ifn/ifn_index, if the ifn does
  157. * not exist we create it and add it to the list of IFN's
  158. * within the VRF. Once we have the sctp_ifn, we add the
  159. * address to the list. So we look something like:
  160. *
  161. * hash-vrf-table
  162. * vrf-> ifn-> ifn -> ifn
  163. * vrf |
  164. * ... +--ifa-> ifa -> ifa
  165. * vrf
  166. *
  167. * We keep these separate lists since the SCTP subsystem will
  168. * point to these from its source address selection nets structure.
  169. * When an address is deleted it does not happen right away on
  170. * the SCTP side, it gets scheduled. What we do when a
  171. * delete happens is immediately remove the address from
  172. * the master list and decrement the refcount. As our
  173. * addip iterator works through and frees the src address
  174. * selection pointing to the sctp_ifa, eventually the refcount
  175. * will reach 0 and we will delete it. Note that it is assumed
  176. * that any locking on system level ifn/ifa is done at the
  177. * caller of these functions and these routines will only
  178. * lock the SCTP structures as they add or delete things.
  179. *
  180. * Other notes on VRF concepts.
  181. * - An endpoint can be in multiple VRF's
  182. * - An association lives within a VRF and only one VRF.
  183. * - Any incoming packet we can deduce the VRF for by
  184. * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
  185. * - Any downward send call or connect call must supply the
  186. * VRF via ancillary data or via some sort of set default
  187. * VRF socket option call (again for BSD no brainer since
  188. * the VRF is always 0).
  189. * - An endpoint may add multiple VRF's to it.
  190. * - Listening sockets can accept associations in any
  191. * of the VRF's they are in but the assoc will end up
  192. * in only one VRF (gotten from the packet or connect/send).
  193. *
  194. */
  195. struct sctp_vrf *
  196. sctp_allocate_vrf(int vrf_id)
  197. {
  198. struct sctp_vrf *vrf = NULL;
  199. struct sctp_vrflist *bucket;
  200. /* First allocate the VRF structure */
  201. vrf = sctp_find_vrf(vrf_id);
  202. if (vrf) {
  203. /* Already allocated */
  204. return (vrf);
  205. }
  206. SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
  207. SCTP_M_VRF);
  208. if (vrf == NULL) {
  209. /* No memory */
  210. #ifdef INVARIANTS
  211. panic("No memory for VRF:%d", vrf_id);
  212. #endif
  213. return (NULL);
  214. }
  215. /* setup the VRF */
  216. memset(vrf, 0, sizeof(struct sctp_vrf));
  217. vrf->vrf_id = vrf_id;
  218. LIST_INIT(&vrf->ifnlist);
  219. vrf->total_ifa_count = 0;
  220. vrf->refcount = 0;
  221. /* now also setup table ids */
  222. SCTP_INIT_VRF_TABLEID(vrf);
  223. /* Init the HASH of addresses */
  224. vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
  225. &vrf->vrf_addr_hashmark);
  226. if (vrf->vrf_addr_hash == NULL) {
  227. /* No memory */
  228. #ifdef INVARIANTS
  229. panic("No memory for VRF:%d", vrf_id);
  230. #endif
  231. SCTP_FREE(vrf, SCTP_M_VRF);
  232. return (NULL);
  233. }
  234. /* Add it to the hash table */
  235. bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
  236. LIST_INSERT_HEAD(bucket, vrf, next_vrf);
  237. atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
  238. return (vrf);
  239. }
  240. struct sctp_ifn *
  241. sctp_find_ifn(void *ifn, uint32_t ifn_index)
  242. {
  243. struct sctp_ifn *sctp_ifnp;
  244. struct sctp_ifnlist *hash_ifn_head;
  245. /* We assume the lock is held for the addresses
  246. * if that's wrong problems could occur :-)
  247. */
  248. SCTP_IPI_ADDR_LOCK_ASSERT();
  249. hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
  250. LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
  251. if (sctp_ifnp->ifn_index == ifn_index) {
  252. return (sctp_ifnp);
  253. }
  254. if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
  255. return (sctp_ifnp);
  256. }
  257. }
  258. return (NULL);
  259. }
  260. struct sctp_vrf *
  261. sctp_find_vrf(uint32_t vrf_id)
  262. {
  263. struct sctp_vrflist *bucket;
  264. struct sctp_vrf *liste;
  265. bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
  266. LIST_FOREACH(liste, bucket, next_vrf) {
  267. if (vrf_id == liste->vrf_id) {
  268. return (liste);
  269. }
  270. }
  271. return (NULL);
  272. }
  273. void
  274. sctp_free_vrf(struct sctp_vrf *vrf)
  275. {
  276. if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
  277. if (vrf->vrf_addr_hash) {
  278. SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
  279. vrf->vrf_addr_hash = NULL;
  280. }
  281. /* We zero'd the count */
  282. LIST_REMOVE(vrf, next_vrf);
  283. SCTP_FREE(vrf, SCTP_M_VRF);
  284. atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
  285. }
  286. }
  287. void
  288. sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
  289. {
  290. if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
  291. /* We zero'd the count */
  292. if (sctp_ifnp->vrf) {
  293. sctp_free_vrf(sctp_ifnp->vrf);
  294. }
  295. SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
  296. atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
  297. }
  298. }
  299. void
  300. sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
  301. {
  302. struct sctp_ifn *sctp_ifnp;
  303. sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
  304. if (sctp_ifnp != NULL) {
  305. sctp_ifnp->ifn_mtu = mtu;
  306. }
  307. }
  308. void
  309. sctp_free_ifa(struct sctp_ifa *sctp_ifap)
  310. {
  311. if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
  312. /* We zero'd the count */
  313. if (sctp_ifap->ifn_p) {
  314. sctp_free_ifn(sctp_ifap->ifn_p);
  315. }
  316. SCTP_FREE(sctp_ifap, SCTP_M_IFA);
  317. atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
  318. }
  319. }
  320. static void
  321. sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
  322. {
  323. struct sctp_ifn *found;
  324. found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
  325. if (found == NULL) {
  326. /* Not in the list.. sorry */
  327. return;
  328. }
  329. if (hold_addr_lock == 0) {
  330. SCTP_IPI_ADDR_WLOCK();
  331. } else {
  332. SCTP_IPI_ADDR_WLOCK_ASSERT();
  333. }
  334. LIST_REMOVE(sctp_ifnp, next_bucket);
  335. LIST_REMOVE(sctp_ifnp, next_ifn);
  336. if (hold_addr_lock == 0) {
  337. SCTP_IPI_ADDR_WUNLOCK();
  338. }
  339. /* Take away the reference, and possibly free it */
  340. sctp_free_ifn(sctp_ifnp);
  341. }
  342. void
  343. sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
  344. const char *if_name, uint32_t ifn_index)
  345. {
  346. struct sctp_vrf *vrf;
  347. struct sctp_ifa *sctp_ifap;
  348. SCTP_IPI_ADDR_RLOCK();
  349. vrf = sctp_find_vrf(vrf_id);
  350. if (vrf == NULL) {
  351. SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
  352. goto out;
  353. }
  354. sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
  355. if (sctp_ifap == NULL) {
  356. SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
  357. goto out;
  358. }
  359. if (sctp_ifap->ifn_p == NULL) {
  360. SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
  361. goto out;
  362. }
  363. if (if_name) {
  364. if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
  365. SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
  366. sctp_ifap->ifn_p->ifn_name, if_name);
  367. goto out;
  368. }
  369. } else {
  370. if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
  371. SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
  372. sctp_ifap->ifn_p->ifn_index, ifn_index);
  373. goto out;
  374. }
  375. }
  376. sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
  377. sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
  378. out:
  379. SCTP_IPI_ADDR_RUNLOCK();
  380. }
  381. void
  382. sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
  383. const char *if_name, uint32_t ifn_index)
  384. {
  385. struct sctp_vrf *vrf;
  386. struct sctp_ifa *sctp_ifap;
  387. SCTP_IPI_ADDR_RLOCK();
  388. vrf = sctp_find_vrf(vrf_id);
  389. if (vrf == NULL) {
  390. SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
  391. goto out;
  392. }
  393. sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
  394. if (sctp_ifap == NULL) {
  395. SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
  396. goto out;
  397. }
  398. if (sctp_ifap->ifn_p == NULL) {
  399. SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
  400. goto out;
  401. }
  402. if (if_name) {
  403. if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
  404. SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
  405. sctp_ifap->ifn_p->ifn_name, if_name);
  406. goto out;
  407. }
  408. } else {
  409. if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
  410. SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
  411. sctp_ifap->ifn_p->ifn_index, ifn_index);
  412. goto out;
  413. }
  414. }
  415. sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
  416. sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
  417. out:
  418. SCTP_IPI_ADDR_RUNLOCK();
  419. }
  420. /*-
  421. * Add an ifa to an ifn.
  422. * Register the interface as necessary.
  423. * NOTE: ADDR write lock MUST be held.
  424. */
  425. static void
  426. sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
  427. {
  428. int ifa_af;
  429. LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
  430. sctp_ifap->ifn_p = sctp_ifnp;
  431. atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
  432. /* update address counts */
  433. sctp_ifnp->ifa_count++;
  434. ifa_af = sctp_ifap->address.sa.sa_family;
  435. switch (ifa_af) {
  436. #ifdef INET
  437. case AF_INET:
  438. sctp_ifnp->num_v4++;
  439. break;
  440. #endif
  441. #ifdef INET6
  442. case AF_INET6:
  443. sctp_ifnp->num_v6++;
  444. break;
  445. #endif
  446. default:
  447. break;
  448. }
  449. if (sctp_ifnp->ifa_count == 1) {
  450. /* register the new interface */
  451. sctp_ifnp->registered_af = ifa_af;
  452. }
  453. }
  454. /*-
  455. * Remove an ifa from its ifn.
  456. * If no more addresses exist, remove the ifn too. Otherwise, re-register
  457. * the interface based on the remaining address families left.
  458. * NOTE: ADDR write lock MUST be held.
  459. */
  460. static void
  461. sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
  462. {
  463. LIST_REMOVE(sctp_ifap, next_ifa);
  464. if (sctp_ifap->ifn_p) {
  465. /* update address counts */
  466. sctp_ifap->ifn_p->ifa_count--;
  467. switch (sctp_ifap->address.sa.sa_family) {
  468. #ifdef INET
  469. case AF_INET:
  470. sctp_ifap->ifn_p->num_v4--;
  471. break;
  472. #endif
  473. #ifdef INET6
  474. case AF_INET6:
  475. sctp_ifap->ifn_p->num_v6--;
  476. break;
  477. #endif
  478. default:
  479. break;
  480. }
  481. if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
  482. /* remove the ifn, possibly freeing it */
  483. sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
  484. } else {
  485. /* re-register address family type, if needed */
  486. if ((sctp_ifap->ifn_p->num_v6 == 0) &&
  487. (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
  488. sctp_ifap->ifn_p->registered_af = AF_INET;
  489. } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
  490. (sctp_ifap->ifn_p->registered_af == AF_INET)) {
  491. sctp_ifap->ifn_p->registered_af = AF_INET6;
  492. }
  493. /* free the ifn refcount */
  494. sctp_free_ifn(sctp_ifap->ifn_p);
  495. }
  496. sctp_ifap->ifn_p = NULL;
  497. }
  498. }
  499. struct sctp_ifa *
  500. sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
  501. uint32_t ifn_type, const char *if_name, void *ifa,
  502. struct sockaddr *addr, uint32_t ifa_flags,
  503. int dynamic_add)
  504. {
  505. struct sctp_vrf *vrf;
  506. struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp;
  507. struct sctp_ifa *sctp_ifap, *new_sctp_ifap;
  508. struct sctp_ifalist *hash_addr_head;
  509. struct sctp_ifnlist *hash_ifn_head;
  510. uint32_t hash_of_addr;
  511. int new_ifn_af = 0;
  512. #ifdef SCTP_DEBUG
  513. SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
  514. SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
  515. #endif
  516. SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *,
  517. sizeof(struct sctp_ifn), SCTP_M_IFN);
  518. if (new_sctp_ifnp == NULL) {
  519. #ifdef INVARIANTS
  520. panic("No memory for IFN");
  521. #endif
  522. return (NULL);
  523. }
  524. SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
  525. if (new_sctp_ifap == NULL) {
  526. #ifdef INVARIANTS
  527. panic("No memory for IFA");
  528. #endif
  529. SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
  530. return (NULL);
  531. }
  532. SCTP_IPI_ADDR_WLOCK();
  533. sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
  534. if (sctp_ifnp) {
  535. vrf = sctp_ifnp->vrf;
  536. } else {
  537. vrf = sctp_find_vrf(vrf_id);
  538. if (vrf == NULL) {
  539. vrf = sctp_allocate_vrf(vrf_id);
  540. if (vrf == NULL) {
  541. SCTP_IPI_ADDR_WUNLOCK();
  542. SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
  543. SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
  544. return (NULL);
  545. }
  546. }
  547. }
  548. if (sctp_ifnp == NULL) {
  549. /* build one and add it, can't hold lock
  550. * until after malloc done though.
  551. */
  552. sctp_ifnp = new_sctp_ifnp;
  553. new_sctp_ifnp = NULL;
  554. memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
  555. sctp_ifnp->ifn_index = ifn_index;
  556. sctp_ifnp->ifn_p = ifn;
  557. sctp_ifnp->ifn_type = ifn_type;
  558. sctp_ifnp->refcount = 0;
  559. sctp_ifnp->vrf = vrf;
  560. atomic_add_int(&vrf->refcount, 1);
  561. sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index);
  562. if (if_name != NULL) {
  563. SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
  564. } else {
  565. SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
  566. }
  567. hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
  568. LIST_INIT(&sctp_ifnp->ifalist);
  569. LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
  570. LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
  571. atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
  572. new_ifn_af = 1;
  573. }
  574. sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
  575. if (sctp_ifap) {
  576. /* Hmm, it already exists? */
  577. if ((sctp_ifap->ifn_p) &&
  578. (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
  579. SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
  580. sctp_ifap->ifn_p->ifn_name, ifn_index,
  581. (void *)sctp_ifap);
  582. if (new_ifn_af) {
  583. /* Remove the created one that we don't want */
  584. sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
  585. }
  586. if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
  587. /* easy to solve, just switch back to active */
  588. SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
  589. sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
  590. sctp_ifap->ifn_p = sctp_ifnp;
  591. atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
  592. }
  593. exit_stage_left:
  594. SCTP_IPI_ADDR_WUNLOCK();
  595. if (new_sctp_ifnp != NULL) {
  596. SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
  597. }
  598. SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
  599. return (sctp_ifap);
  600. } else {
  601. if (sctp_ifap->ifn_p) {
  602. /*
  603. * The last IFN gets the address, remove the
  604. * old one
  605. */
  606. SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
  607. (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name,
  608. sctp_ifap->ifn_p->ifn_index, if_name,
  609. ifn_index);
  610. /* remove the address from the old ifn */
  611. sctp_remove_ifa_from_ifn(sctp_ifap);
  612. /* move the address over to the new ifn */
  613. sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
  614. goto exit_stage_left;
  615. } else {
  616. /* repair ifnp which was NULL ? */
  617. sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
  618. SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
  619. (void *)sctp_ifnp, (void *)sctp_ifap);
  620. sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
  621. }
  622. goto exit_stage_left;
  623. }
  624. }
  625. sctp_ifap = new_sctp_ifap;
  626. memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
  627. sctp_ifap->ifn_p = sctp_ifnp;
  628. atomic_add_int(&sctp_ifnp->refcount, 1);
  629. sctp_ifap->vrf_id = vrf_id;
  630. sctp_ifap->ifa = ifa;
  631. #ifdef HAVE_SA_LEN
  632. memcpy(&sctp_ifap->address, addr, addr->sa_len);
  633. #else
  634. switch (addr->sa_family) {
  635. #ifdef INET
  636. case AF_INET:
  637. memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in));
  638. break;
  639. #endif
  640. #ifdef INET6
  641. case AF_INET6:
  642. memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6));
  643. break;
  644. #endif
  645. #if defined(__Userspace__)
  646. case AF_CONN:
  647. memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn));
  648. break;
  649. #endif
  650. default:
  651. /* TSNH */
  652. break;
  653. }
  654. #endif
  655. sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
  656. sctp_ifap->flags = ifa_flags;
  657. /* Set scope */
  658. switch (sctp_ifap->address.sa.sa_family) {
  659. #ifdef INET
  660. case AF_INET:
  661. {
  662. struct sockaddr_in *sin;
  663. sin = &sctp_ifap->address.sin;
  664. if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
  665. (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
  666. sctp_ifap->src_is_loop = 1;
  667. }
  668. if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
  669. sctp_ifap->src_is_priv = 1;
  670. }
  671. sctp_ifnp->num_v4++;
  672. if (new_ifn_af)
  673. new_ifn_af = AF_INET;
  674. break;
  675. }
  676. #endif
  677. #ifdef INET6
  678. case AF_INET6:
  679. {
  680. /* ok to use deprecated addresses? */
  681. struct sockaddr_in6 *sin6;
  682. sin6 = &sctp_ifap->address.sin6;
  683. if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
  684. (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
  685. sctp_ifap->src_is_loop = 1;
  686. }
  687. if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  688. sctp_ifap->src_is_priv = 1;
  689. }
  690. sctp_ifnp->num_v6++;
  691. if (new_ifn_af)
  692. new_ifn_af = AF_INET6;
  693. break;
  694. }
  695. #endif
  696. #if defined(__Userspace__)
  697. case AF_CONN:
  698. if (new_ifn_af)
  699. new_ifn_af = AF_CONN;
  700. break;
  701. #endif
  702. default:
  703. new_ifn_af = 0;
  704. break;
  705. }
  706. hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
  707. if ((sctp_ifap->src_is_priv == 0) &&
  708. (sctp_ifap->src_is_loop == 0)) {
  709. sctp_ifap->src_is_glob = 1;
  710. }
  711. hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
  712. LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
  713. sctp_ifap->refcount = 1;
  714. LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
  715. sctp_ifnp->ifa_count++;
  716. vrf->total_ifa_count++;
  717. atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
  718. if (new_ifn_af) {
  719. sctp_ifnp->registered_af = new_ifn_af;
  720. }
  721. SCTP_IPI_ADDR_WUNLOCK();
  722. if (new_sctp_ifnp != NULL) {
  723. SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
  724. }
  725. if (dynamic_add) {
  726. /* Bump up the refcount so that when the timer
  727. * completes it will drop back down.
  728. */
  729. struct sctp_laddr *wi;
  730. atomic_add_int(&sctp_ifap->refcount, 1);
  731. wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
  732. if (wi == NULL) {
  733. /*
  734. * Gak, what can we do? We have lost an address
  735. * change can you say HOSED?
  736. */
  737. SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
  738. /* Opps, must decrement the count */
  739. sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
  740. if_name);
  741. return (NULL);
  742. }
  743. SCTP_INCR_LADDR_COUNT();
  744. memset(wi, 0, sizeof(*wi));
  745. (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
  746. wi->ifa = sctp_ifap;
  747. wi->action = SCTP_ADD_IP_ADDRESS;
  748. SCTP_WQ_ADDR_LOCK();
  749. LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
  750. sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
  751. (struct sctp_inpcb *)NULL,
  752. (struct sctp_tcb *)NULL,
  753. (struct sctp_nets *)NULL);
  754. SCTP_WQ_ADDR_UNLOCK();
  755. } else {
  756. /* it's ready for use */
  757. sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
  758. }
  759. return (sctp_ifap);
  760. }
  761. void
  762. sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
  763. uint32_t ifn_index, const char *if_name)
  764. {
  765. struct sctp_vrf *vrf;
  766. struct sctp_ifa *sctp_ifap = NULL;
  767. SCTP_IPI_ADDR_WLOCK();
  768. vrf = sctp_find_vrf(vrf_id);
  769. if (vrf == NULL) {
  770. SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
  771. goto out_now;
  772. }
  773. #ifdef SCTP_DEBUG
  774. SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
  775. SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
  776. #endif
  777. sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
  778. if (sctp_ifap) {
  779. /* Validate the delete */
  780. if (sctp_ifap->ifn_p) {
  781. int valid = 0;
  782. /*-
  783. * The name has priority over the ifn_index
  784. * if its given.
  785. */
  786. if (if_name) {
  787. if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
  788. /* They match its a correct delete */
  789. valid = 1;
  790. }
  791. }
  792. if (!valid) {
  793. /* last ditch check ifn_index */
  794. if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
  795. valid = 1;
  796. }
  797. }
  798. if (!valid) {
  799. SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
  800. ifn_index, ((if_name == NULL) ? "NULL" : if_name));
  801. SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
  802. sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
  803. SCTP_IPI_ADDR_WUNLOCK();
  804. return;
  805. }
  806. }
  807. SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
  808. sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
  809. /*
  810. * We don't set the flag. This means that the structure will
  811. * hang around in EP's that have bound specific to it until
  812. * they close. This gives us TCP like behavior if someone
  813. * removes an address (or for that matter adds it right back).
  814. */
  815. /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
  816. vrf->total_ifa_count--;
  817. LIST_REMOVE(sctp_ifap, next_bucket);
  818. sctp_remove_ifa_from_ifn(sctp_ifap);
  819. }
  820. #ifdef SCTP_DEBUG
  821. else {
  822. SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
  823. ifn_index);
  824. SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
  825. }
  826. #endif
  827. out_now:
  828. SCTP_IPI_ADDR_WUNLOCK();
  829. if (sctp_ifap) {
  830. struct sctp_laddr *wi;
  831. wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
  832. if (wi == NULL) {
  833. /*
  834. * Gak, what can we do? We have lost an address
  835. * change can you say HOSED?
  836. */
  837. SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
  838. /* Oops, must decrement the count */
  839. sctp_free_ifa(sctp_ifap);
  840. return;
  841. }
  842. SCTP_INCR_LADDR_COUNT();
  843. memset(wi, 0, sizeof(*wi));
  844. (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
  845. wi->ifa = sctp_ifap;
  846. wi->action = SCTP_DEL_IP_ADDRESS;
  847. SCTP_WQ_ADDR_LOCK();
  848. /*
  849. * Should this really be a tailq? As it is we will process the
  850. * newest first :-0
  851. */
  852. LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
  853. sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
  854. (struct sctp_inpcb *)NULL,
  855. (struct sctp_tcb *)NULL,
  856. (struct sctp_nets *)NULL);
  857. SCTP_WQ_ADDR_UNLOCK();
  858. }
  859. return;
  860. }
  861. static int
  862. sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
  863. {
  864. int loopback_scope;
  865. #if defined(INET)
  866. int ipv4_local_scope, ipv4_addr_legal;
  867. #endif
  868. #if defined(INET6)
  869. int local_scope, site_scope, ipv6_addr_legal;
  870. #endif
  871. #if defined(__Userspace__)
  872. int conn_addr_legal;
  873. #endif
  874. struct sctp_vrf *vrf;
  875. struct sctp_ifn *sctp_ifn;
  876. struct sctp_ifa *sctp_ifa;
  877. loopback_scope = stcb->asoc.scope.loopback_scope;
  878. #if defined(INET)
  879. ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
  880. ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
  881. #endif
  882. #if defined(INET6)
  883. local_scope = stcb->asoc.scope.local_scope;
  884. site_scope = stcb->asoc.scope.site_scope;
  885. ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
  886. #endif
  887. #if defined(__Userspace__)
  888. conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
  889. #endif
  890. SCTP_IPI_ADDR_RLOCK();
  891. vrf = sctp_find_vrf(stcb->asoc.vrf_id);
  892. if (vrf == NULL) {
  893. /* no vrf, no addresses */
  894. SCTP_IPI_ADDR_RUNLOCK();
  895. return (0);
  896. }
  897. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  898. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  899. if ((loopback_scope == 0) &&
  900. SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  901. continue;
  902. }
  903. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  904. if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
  905. (!sctp_is_addr_pending(stcb, sctp_ifa))) {
  906. /* We allow pending addresses, where we
  907. * have sent an asconf-add to be considered
  908. * valid.
  909. */
  910. continue;
  911. }
  912. if (sctp_ifa->address.sa.sa_family != to->sa_family) {
  913. continue;
  914. }
  915. switch (sctp_ifa->address.sa.sa_family) {
  916. #ifdef INET
  917. case AF_INET:
  918. if (ipv4_addr_legal) {
  919. struct sockaddr_in *sin, *rsin;
  920. sin = &sctp_ifa->address.sin;
  921. rsin = (struct sockaddr_in *)to;
  922. if ((ipv4_local_scope == 0) &&
  923. IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
  924. continue;
  925. }
  926. #if defined(__FreeBSD__) && !defined(__Userspace__)
  927. if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
  928. &sin->sin_addr) != 0) {
  929. continue;
  930. }
  931. #endif
  932. if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
  933. SCTP_IPI_ADDR_RUNLOCK();
  934. return (1);
  935. }
  936. }
  937. break;
  938. #endif
  939. #ifdef INET6
  940. case AF_INET6:
  941. if (ipv6_addr_legal) {
  942. struct sockaddr_in6 *sin6, *rsin6;
  943. #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
  944. struct sockaddr_in6 lsa6;
  945. #endif
  946. sin6 = &sctp_ifa->address.sin6;
  947. rsin6 = (struct sockaddr_in6 *)to;
  948. #if defined(__FreeBSD__) && !defined(__Userspace__)
  949. if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
  950. &sin6->sin6_addr) != 0) {
  951. continue;
  952. }
  953. #endif
  954. if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  955. if (local_scope == 0)
  956. continue;
  957. #if defined(SCTP_EMBEDDED_V6_SCOPE)
  958. if (sin6->sin6_scope_id == 0) {
  959. #ifdef SCTP_KAME
  960. if (sa6_recoverscope(sin6) != 0)
  961. continue;
  962. #else
  963. lsa6 = *sin6;
  964. if (in6_recoverscope(&lsa6,
  965. &lsa6.sin6_addr,
  966. NULL))
  967. continue;
  968. sin6 = &lsa6;
  969. #endif /* SCTP_KAME */
  970. }
  971. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  972. }
  973. if ((site_scope == 0) &&
  974. (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
  975. continue;
  976. }
  977. if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
  978. SCTP_IPI_ADDR_RUNLOCK();
  979. return (1);
  980. }
  981. }
  982. break;
  983. #endif
  984. #if defined(__Userspace__)
  985. case AF_CONN:
  986. if (conn_addr_legal) {
  987. struct sockaddr_conn *sconn, *rsconn;
  988. sconn = &sctp_ifa->address.sconn;
  989. rsconn = (struct sockaddr_conn *)to;
  990. if (sconn->sconn_addr == rsconn->sconn_addr) {
  991. SCTP_IPI_ADDR_RUNLOCK();
  992. return (1);
  993. }
  994. }
  995. break;
  996. #endif
  997. default:
  998. /* TSNH */
  999. break;
  1000. }
  1001. }
  1002. }
  1003. } else {
  1004. struct sctp_laddr *laddr;
  1005. LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
  1006. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
  1007. SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
  1008. continue;
  1009. }
  1010. if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
  1011. (!sctp_is_addr_pending(stcb, laddr->ifa))) {
  1012. /* We allow pending addresses, where we
  1013. * have sent an asconf-add to be considered
  1014. * valid.
  1015. */
  1016. continue;
  1017. }
  1018. if (laddr->ifa->address.sa.sa_family != to->sa_family) {
  1019. continue;
  1020. }
  1021. switch (to->sa_family) {
  1022. #ifdef INET
  1023. case AF_INET:
  1024. {
  1025. struct sockaddr_in *sin, *rsin;
  1026. sin = &laddr->ifa->address.sin;
  1027. rsin = (struct sockaddr_in *)to;
  1028. if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
  1029. SCTP_IPI_ADDR_RUNLOCK();
  1030. return (1);
  1031. }
  1032. break;
  1033. }
  1034. #endif
  1035. #ifdef INET6
  1036. case AF_INET6:
  1037. {
  1038. struct sockaddr_in6 *sin6, *rsin6;
  1039. sin6 = &laddr->ifa->address.sin6;
  1040. rsin6 = (struct sockaddr_in6 *)to;
  1041. if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
  1042. SCTP_IPI_ADDR_RUNLOCK();
  1043. return (1);
  1044. }
  1045. break;
  1046. }
  1047. #endif
  1048. #if defined(__Userspace__)
  1049. case AF_CONN:
  1050. {
  1051. struct sockaddr_conn *sconn, *rsconn;
  1052. sconn = &laddr->ifa->address.sconn;
  1053. rsconn = (struct sockaddr_conn *)to;
  1054. if (sconn->sconn_addr == rsconn->sconn_addr) {
  1055. SCTP_IPI_ADDR_RUNLOCK();
  1056. return (1);
  1057. }
  1058. break;
  1059. }
  1060. #endif
  1061. default:
  1062. /* TSNH */
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. SCTP_IPI_ADDR_RUNLOCK();
  1068. return (0);
  1069. }
  1070. static struct sctp_tcb *
  1071. sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
  1072. struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
  1073. {
  1074. /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
  1075. /*
  1076. * If we support the TCP model, then we must now dig through to see
  1077. * if we can find our endpoint in the list of tcp ep's.
  1078. */
  1079. uint16_t lport, rport;
  1080. struct sctppcbhead *ephead;
  1081. struct sctp_inpcb *inp;
  1082. struct sctp_laddr *laddr;
  1083. struct sctp_tcb *stcb;
  1084. struct sctp_nets *net;
  1085. #ifdef SCTP_MVRF
  1086. int fnd, i;
  1087. #endif
  1088. if ((to == NULL) || (from == NULL)) {
  1089. return (NULL);
  1090. }
  1091. switch (to->sa_family) {
  1092. #ifdef INET
  1093. case AF_INET:
  1094. if (from->sa_family == AF_INET) {
  1095. lport = ((struct sockaddr_in *)to)->sin_port;
  1096. rport = ((struct sockaddr_in *)from)->sin_port;
  1097. } else {
  1098. return (NULL);
  1099. }
  1100. break;
  1101. #endif
  1102. #ifdef INET6
  1103. case AF_INET6:
  1104. if (from->sa_family == AF_INET6) {
  1105. lport = ((struct sockaddr_in6 *)to)->sin6_port;
  1106. rport = ((struct sockaddr_in6 *)from)->sin6_port;
  1107. } else {
  1108. return (NULL);
  1109. }
  1110. break;
  1111. #endif
  1112. #if defined(__Userspace__)
  1113. case AF_CONN:
  1114. if (from->sa_family == AF_CONN) {
  1115. lport = ((struct sockaddr_conn *)to)->sconn_port;
  1116. rport = ((struct sockaddr_conn *)from)->sconn_port;
  1117. } else {
  1118. return (NULL);
  1119. }
  1120. break;
  1121. #endif
  1122. default:
  1123. return (NULL);
  1124. }
  1125. ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
  1126. /*
  1127. * Ok now for each of the guys in this bucket we must look and see:
  1128. * - Does the remote port match. - Does there single association's
  1129. * addresses match this address (to). If so we update p_ep to point
  1130. * to this ep and return the tcb from it.
  1131. */
  1132. LIST_FOREACH(inp, ephead, sctp_hash) {
  1133. SCTP_INP_RLOCK(inp);
  1134. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  1135. SCTP_INP_RUNLOCK(inp);
  1136. continue;
  1137. }
  1138. if (lport != inp->sctp_lport) {
  1139. SCTP_INP_RUNLOCK(inp);
  1140. continue;
  1141. }
  1142. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1143. switch (to->sa_family) {
  1144. #ifdef INET
  1145. case AF_INET:
  1146. {
  1147. struct sockaddr_in *sin;
  1148. sin = (struct sockaddr_in *)to;
  1149. if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  1150. &sin->sin_addr) != 0) {
  1151. SCTP_INP_RUNLOCK(inp);
  1152. continue;
  1153. }
  1154. break;
  1155. }
  1156. #endif
  1157. #ifdef INET6
  1158. case AF_INET6:
  1159. {
  1160. struct sockaddr_in6 *sin6;
  1161. sin6 = (struct sockaddr_in6 *)to;
  1162. if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  1163. &sin6->sin6_addr) != 0) {
  1164. SCTP_INP_RUNLOCK(inp);
  1165. continue;
  1166. }
  1167. break;
  1168. }
  1169. #endif
  1170. default:
  1171. SCTP_INP_RUNLOCK(inp);
  1172. continue;
  1173. }
  1174. #endif
  1175. #ifdef SCTP_MVRF
  1176. fnd = 0;
  1177. for (i = 0; i < inp->num_vrfs; i++) {
  1178. if (inp->m_vrf_ids[i] == vrf_id) {
  1179. fnd = 1;
  1180. break;
  1181. }
  1182. }
  1183. if (fnd == 0) {
  1184. SCTP_INP_RUNLOCK(inp);
  1185. continue;
  1186. }
  1187. #else
  1188. if (inp->def_vrf_id != vrf_id) {
  1189. SCTP_INP_RUNLOCK(inp);
  1190. continue;
  1191. }
  1192. #endif
  1193. /* check to see if the ep has one of the addresses */
  1194. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
  1195. /* We are NOT bound all, so look further */
  1196. int match = 0;
  1197. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  1198. if (laddr->ifa == NULL) {
  1199. SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
  1200. continue;
  1201. }
  1202. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
  1203. SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
  1204. continue;
  1205. }
  1206. if (laddr->ifa->address.sa.sa_family ==
  1207. to->sa_family) {
  1208. /* see if it matches */
  1209. #ifdef INET
  1210. if (from->sa_family == AF_INET) {
  1211. struct sockaddr_in *intf_addr, *sin;
  1212. intf_addr = &laddr->ifa->address.sin;
  1213. sin = (struct sockaddr_in *)to;
  1214. if (sin->sin_addr.s_addr ==
  1215. intf_addr->sin_addr.s_addr) {
  1216. match = 1;
  1217. break;
  1218. }
  1219. }
  1220. #endif
  1221. #ifdef INET6
  1222. if (from->sa_family == AF_INET6) {
  1223. struct sockaddr_in6 *intf_addr6;
  1224. struct sockaddr_in6 *sin6;
  1225. sin6 = (struct sockaddr_in6 *)
  1226. to;
  1227. intf_addr6 = &laddr->ifa->address.sin6;
  1228. if (SCTP6_ARE_ADDR_EQUAL(sin6,
  1229. intf_addr6)) {
  1230. match = 1;
  1231. break;
  1232. }
  1233. }
  1234. #endif
  1235. #if defined(__Userspace__)
  1236. if (from->sa_family == AF_CONN) {
  1237. struct sockaddr_conn *intf_addr, *sconn;
  1238. intf_addr = &laddr->ifa->address.sconn;
  1239. sconn = (struct sockaddr_conn *)to;
  1240. if (sconn->sconn_addr ==
  1241. intf_addr->sconn_addr) {
  1242. match = 1;
  1243. break;
  1244. }
  1245. }
  1246. #endif
  1247. }
  1248. }
  1249. if (match == 0) {
  1250. /* This endpoint does not have this address */
  1251. SCTP_INP_RUNLOCK(inp);
  1252. continue;
  1253. }
  1254. }
  1255. /*
  1256. * Ok if we hit here the ep has the address, does it hold
  1257. * the tcb?
  1258. */
  1259. /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
  1260. stcb = LIST_FIRST(&inp->sctp_asoc_list);
  1261. if (stcb == NULL) {
  1262. SCTP_INP_RUNLOCK(inp);
  1263. continue;
  1264. }
  1265. SCTP_TCB_LOCK(stcb);
  1266. if (!sctp_does_stcb_own_this_addr(stcb, to)) {
  1267. SCTP_TCB_UNLOCK(stcb);
  1268. SCTP_INP_RUNLOCK(inp);
  1269. continue;
  1270. }
  1271. if (stcb->rport != rport) {
  1272. /* remote port does not match. */
  1273. SCTP_TCB_UNLOCK(stcb);
  1274. SCTP_INP_RUNLOCK(inp);
  1275. continue;
  1276. }
  1277. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  1278. SCTP_TCB_UNLOCK(stcb);
  1279. SCTP_INP_RUNLOCK(inp);
  1280. continue;
  1281. }
  1282. if (!sctp_does_stcb_own_this_addr(stcb, to)) {
  1283. SCTP_TCB_UNLOCK(stcb);
  1284. SCTP_INP_RUNLOCK(inp);
  1285. continue;
  1286. }
  1287. /* Does this TCB have a matching address? */
  1288. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1289. if (net->ro._l_addr.sa.sa_family != from->sa_family) {
  1290. /* not the same family, can't be a match */
  1291. continue;
  1292. }
  1293. switch (from->sa_family) {
  1294. #ifdef INET
  1295. case AF_INET:
  1296. {
  1297. struct sockaddr_in *sin, *rsin;
  1298. sin = (struct sockaddr_in *)&net->ro._l_addr;
  1299. rsin = (struct sockaddr_in *)from;
  1300. if (sin->sin_addr.s_addr ==
  1301. rsin->sin_addr.s_addr) {
  1302. /* found it */
  1303. if (netp != NULL) {
  1304. *netp = net;
  1305. }
  1306. /* Update the endpoint pointer */
  1307. *inp_p = inp;
  1308. SCTP_INP_RUNLOCK(inp);
  1309. return (stcb);
  1310. }
  1311. break;
  1312. }
  1313. #endif
  1314. #ifdef INET6
  1315. case AF_INET6:
  1316. {
  1317. struct sockaddr_in6 *sin6, *rsin6;
  1318. sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  1319. rsin6 = (struct sockaddr_in6 *)from;
  1320. if (SCTP6_ARE_ADDR_EQUAL(sin6,
  1321. rsin6)) {
  1322. /* found it */
  1323. if (netp != NULL) {
  1324. *netp = net;
  1325. }
  1326. /* Update the endpoint pointer */
  1327. *inp_p = inp;
  1328. SCTP_INP_RUNLOCK(inp);
  1329. return (stcb);
  1330. }
  1331. break;
  1332. }
  1333. #endif
  1334. #if defined(__Userspace__)
  1335. case AF_CONN:
  1336. {
  1337. struct sockaddr_conn *sconn, *rsconn;
  1338. sconn = (struct sockaddr_conn *)&net->ro._l_addr;
  1339. rsconn = (struct sockaddr_conn *)from;
  1340. if (sconn->sconn_addr == rsconn->sconn_addr) {
  1341. /* found it */
  1342. if (netp != NULL) {
  1343. *netp = net;
  1344. }
  1345. /* Update the endpoint pointer */
  1346. *inp_p = inp;
  1347. SCTP_INP_RUNLOCK(inp);
  1348. return (stcb);
  1349. }
  1350. break;
  1351. }
  1352. #endif
  1353. default:
  1354. /* TSNH */
  1355. break;
  1356. }
  1357. }
  1358. SCTP_TCB_UNLOCK(stcb);
  1359. SCTP_INP_RUNLOCK(inp);
  1360. }
  1361. return (NULL);
  1362. }
  1363. /*
  1364. * rules for use
  1365. *
  1366. * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
  1367. * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
  1368. * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
  1369. * NULL.
  1370. */
  1371. struct sctp_tcb *
  1372. sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
  1373. struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
  1374. {
  1375. struct sctpasochead *head;
  1376. struct sctp_inpcb *inp;
  1377. struct sctp_tcb *stcb = NULL;
  1378. struct sctp_nets *net;
  1379. uint16_t rport;
  1380. inp = *inp_p;
  1381. switch (remote->sa_family) {
  1382. #ifdef INET
  1383. case AF_INET:
  1384. rport = (((struct sockaddr_in *)remote)->sin_port);
  1385. break;
  1386. #endif
  1387. #ifdef INET6
  1388. case AF_INET6:
  1389. rport = (((struct sockaddr_in6 *)remote)->sin6_port);
  1390. break;
  1391. #endif
  1392. #if defined(__Userspace__)
  1393. case AF_CONN:
  1394. rport = (((struct sockaddr_conn *)remote)->sconn_port);
  1395. break;
  1396. #endif
  1397. default:
  1398. return (NULL);
  1399. }
  1400. if (locked_tcb) {
  1401. /*
  1402. * UN-lock so we can do proper locking here this occurs when
  1403. * called from load_addresses_from_init.
  1404. */
  1405. atomic_add_int(&locked_tcb->asoc.refcnt, 1);
  1406. SCTP_TCB_UNLOCK(locked_tcb);
  1407. }
  1408. SCTP_INP_INFO_RLOCK();
  1409. if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  1410. (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
  1411. /*-
  1412. * Now either this guy is our listener or it's the
  1413. * connector. If it is the one that issued the connect, then
  1414. * it's only chance is to be the first TCB in the list. If
  1415. * it is the acceptor, then do the special_lookup to hash
  1416. * and find the real inp.
  1417. */
  1418. if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
  1419. /* to is peer addr, from is my addr */
  1420. #ifndef SCTP_MVRF
  1421. stcb = sctp_tcb_special_locate(inp_p, remote, local,
  1422. netp, inp->def_vrf_id);
  1423. if ((stcb != NULL) && (locked_tcb == NULL)) {
  1424. /* we have a locked tcb, lower refcount */
  1425. SCTP_INP_DECR_REF(inp);
  1426. }
  1427. if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
  1428. SCTP_INP_RLOCK(locked_tcb->sctp_ep);
  1429. SCTP_TCB_LOCK(locked_tcb);
  1430. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1431. SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
  1432. }
  1433. #else
  1434. /*-
  1435. * MVRF is tricky, we must look in every VRF
  1436. * the endpoint has.
  1437. */
  1438. int i;
  1439. for (i = 0; i < inp->num_vrfs; i++) {
  1440. stcb = sctp_tcb_special_locate(inp_p, remote, local,
  1441. netp, inp->m_vrf_ids[i]);
  1442. if ((stcb != NULL) && (locked_tcb == NULL)) {
  1443. /* we have a locked tcb, lower refcount */
  1444. SCTP_INP_DECR_REF(inp);
  1445. break;
  1446. }
  1447. if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
  1448. SCTP_INP_RLOCK(locked_tcb->sctp_ep);
  1449. SCTP_TCB_LOCK(locked_tcb);
  1450. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1451. SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
  1452. break;
  1453. }
  1454. }
  1455. #endif
  1456. SCTP_INP_INFO_RUNLOCK();
  1457. return (stcb);
  1458. } else {
  1459. SCTP_INP_WLOCK(inp);
  1460. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  1461. goto null_return;
  1462. }
  1463. stcb = LIST_FIRST(&inp->sctp_asoc_list);
  1464. if (stcb == NULL) {
  1465. goto null_return;
  1466. }
  1467. SCTP_TCB_LOCK(stcb);
  1468. if (stcb->rport != rport) {
  1469. /* remote port does not match. */
  1470. SCTP_TCB_UNLOCK(stcb);
  1471. goto null_return;
  1472. }
  1473. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  1474. SCTP_TCB_UNLOCK(stcb);
  1475. goto null_return;
  1476. }
  1477. if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
  1478. SCTP_TCB_UNLOCK(stcb);
  1479. goto null_return;
  1480. }
  1481. /* now look at the list of remote addresses */
  1482. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1483. #ifdef INVARIANTS
  1484. if (net == (TAILQ_NEXT(net, sctp_next))) {
  1485. panic("Corrupt net list");
  1486. }
  1487. #endif
  1488. if (net->ro._l_addr.sa.sa_family !=
  1489. remote->sa_family) {
  1490. /* not the same family */
  1491. continue;
  1492. }
  1493. switch (remote->sa_family) {
  1494. #ifdef INET
  1495. case AF_INET:
  1496. {
  1497. struct sockaddr_in *sin, *rsin;
  1498. sin = (struct sockaddr_in *)
  1499. &net->ro._l_addr;
  1500. rsin = (struct sockaddr_in *)remote;
  1501. if (sin->sin_addr.s_addr ==
  1502. rsin->sin_addr.s_addr) {
  1503. /* found it */
  1504. if (netp != NULL) {
  1505. *netp = net;
  1506. }
  1507. if (locked_tcb == NULL) {
  1508. SCTP_INP_DECR_REF(inp);
  1509. } else if (locked_tcb != stcb) {
  1510. SCTP_TCB_LOCK(locked_tcb);
  1511. }
  1512. if (locked_tcb) {
  1513. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1514. }
  1515. SCTP_INP_WUNLOCK(inp);
  1516. SCTP_INP_INFO_RUNLOCK();
  1517. return (stcb);
  1518. }
  1519. break;
  1520. }
  1521. #endif
  1522. #ifdef INET6
  1523. case AF_INET6:
  1524. {
  1525. struct sockaddr_in6 *sin6, *rsin6;
  1526. sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  1527. rsin6 = (struct sockaddr_in6 *)remote;
  1528. if (SCTP6_ARE_ADDR_EQUAL(sin6,
  1529. rsin6)) {
  1530. /* found it */
  1531. if (netp != NULL) {
  1532. *netp = net;
  1533. }
  1534. if (locked_tcb == NULL) {
  1535. SCTP_INP_DECR_REF(inp);
  1536. } else if (locked_tcb != stcb) {
  1537. SCTP_TCB_LOCK(locked_tcb);
  1538. }
  1539. if (locked_tcb) {
  1540. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1541. }
  1542. SCTP_INP_WUNLOCK(inp);
  1543. SCTP_INP_INFO_RUNLOCK();
  1544. return (stcb);
  1545. }
  1546. break;
  1547. }
  1548. #endif
  1549. #if defined(__Userspace__)
  1550. case AF_CONN:
  1551. {
  1552. struct sockaddr_conn *sconn, *rsconn;
  1553. sconn = (struct sockaddr_conn *)&net->ro._l_addr;
  1554. rsconn = (struct sockaddr_conn *)remote;
  1555. if (sconn->sconn_addr == rsconn->sconn_addr) {
  1556. /* found it */
  1557. if (netp != NULL) {
  1558. *netp = net;
  1559. }
  1560. if (locked_tcb == NULL) {
  1561. SCTP_INP_DECR_REF(inp);
  1562. } else if (locked_tcb != stcb) {
  1563. SCTP_TCB_LOCK(locked_tcb);
  1564. }
  1565. if (locked_tcb) {
  1566. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1567. }
  1568. SCTP_INP_WUNLOCK(inp);
  1569. SCTP_INP_INFO_RUNLOCK();
  1570. return (stcb);
  1571. }
  1572. break;
  1573. }
  1574. #endif
  1575. default:
  1576. /* TSNH */
  1577. break;
  1578. }
  1579. }
  1580. SCTP_TCB_UNLOCK(stcb);
  1581. }
  1582. } else {
  1583. SCTP_INP_WLOCK(inp);
  1584. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  1585. goto null_return;
  1586. }
  1587. head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
  1588. inp->sctp_hashmark)];
  1589. LIST_FOREACH(stcb, head, sctp_tcbhash) {
  1590. if (stcb->rport != rport) {
  1591. /* remote port does not match */
  1592. continue;
  1593. }
  1594. SCTP_TCB_LOCK(stcb);
  1595. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  1596. SCTP_TCB_UNLOCK(stcb);
  1597. continue;
  1598. }
  1599. if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
  1600. SCTP_TCB_UNLOCK(stcb);
  1601. continue;
  1602. }
  1603. /* now look at the list of remote addresses */
  1604. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  1605. #ifdef INVARIANTS
  1606. if (net == (TAILQ_NEXT(net, sctp_next))) {
  1607. panic("Corrupt net list");
  1608. }
  1609. #endif
  1610. if (net->ro._l_addr.sa.sa_family !=
  1611. remote->sa_family) {
  1612. /* not the same family */
  1613. continue;
  1614. }
  1615. switch (remote->sa_family) {
  1616. #ifdef INET
  1617. case AF_INET:
  1618. {
  1619. struct sockaddr_in *sin, *rsin;
  1620. sin = (struct sockaddr_in *)
  1621. &net->ro._l_addr;
  1622. rsin = (struct sockaddr_in *)remote;
  1623. if (sin->sin_addr.s_addr ==
  1624. rsin->sin_addr.s_addr) {
  1625. /* found it */
  1626. if (netp != NULL) {
  1627. *netp = net;
  1628. }
  1629. if (locked_tcb == NULL) {
  1630. SCTP_INP_DECR_REF(inp);
  1631. } else if (locked_tcb != stcb) {
  1632. SCTP_TCB_LOCK(locked_tcb);
  1633. }
  1634. if (locked_tcb) {
  1635. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1636. }
  1637. SCTP_INP_WUNLOCK(inp);
  1638. SCTP_INP_INFO_RUNLOCK();
  1639. return (stcb);
  1640. }
  1641. break;
  1642. }
  1643. #endif
  1644. #ifdef INET6
  1645. case AF_INET6:
  1646. {
  1647. struct sockaddr_in6 *sin6, *rsin6;
  1648. sin6 = (struct sockaddr_in6 *)
  1649. &net->ro._l_addr;
  1650. rsin6 = (struct sockaddr_in6 *)remote;
  1651. if (SCTP6_ARE_ADDR_EQUAL(sin6,
  1652. rsin6)) {
  1653. /* found it */
  1654. if (netp != NULL) {
  1655. *netp = net;
  1656. }
  1657. if (locked_tcb == NULL) {
  1658. SCTP_INP_DECR_REF(inp);
  1659. } else if (locked_tcb != stcb) {
  1660. SCTP_TCB_LOCK(locked_tcb);
  1661. }
  1662. if (locked_tcb) {
  1663. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1664. }
  1665. SCTP_INP_WUNLOCK(inp);
  1666. SCTP_INP_INFO_RUNLOCK();
  1667. return (stcb);
  1668. }
  1669. break;
  1670. }
  1671. #endif
  1672. #if defined(__Userspace__)
  1673. case AF_CONN:
  1674. {
  1675. struct sockaddr_conn *sconn, *rsconn;
  1676. sconn = (struct sockaddr_conn *)&net->ro._l_addr;
  1677. rsconn = (struct sockaddr_conn *)remote;
  1678. if (sconn->sconn_addr == rsconn->sconn_addr) {
  1679. /* found it */
  1680. if (netp != NULL) {
  1681. *netp = net;
  1682. }
  1683. if (locked_tcb == NULL) {
  1684. SCTP_INP_DECR_REF(inp);
  1685. } else if (locked_tcb != stcb) {
  1686. SCTP_TCB_LOCK(locked_tcb);
  1687. }
  1688. if (locked_tcb) {
  1689. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1690. }
  1691. SCTP_INP_WUNLOCK(inp);
  1692. SCTP_INP_INFO_RUNLOCK();
  1693. return (stcb);
  1694. }
  1695. break;
  1696. }
  1697. #endif
  1698. default:
  1699. /* TSNH */
  1700. break;
  1701. }
  1702. }
  1703. SCTP_TCB_UNLOCK(stcb);
  1704. }
  1705. }
  1706. null_return:
  1707. /* clean up for returning null */
  1708. if (locked_tcb) {
  1709. SCTP_TCB_LOCK(locked_tcb);
  1710. atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
  1711. }
  1712. SCTP_INP_WUNLOCK(inp);
  1713. SCTP_INP_INFO_RUNLOCK();
  1714. /* not found */
  1715. return (NULL);
  1716. }
  1717. /*
  1718. * Find an association for a specific endpoint using the association id given
  1719. * out in the COMM_UP notification
  1720. */
  1721. struct sctp_tcb *
  1722. sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
  1723. {
  1724. /*
  1725. * Use my the assoc_id to find a endpoint
  1726. */
  1727. struct sctpasochead *head;
  1728. struct sctp_tcb *stcb;
  1729. uint32_t id;
  1730. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  1731. SCTP_PRINTF("TSNH ep_associd0\n");
  1732. return (NULL);
  1733. }
  1734. id = (uint32_t)asoc_id;
  1735. head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
  1736. if (head == NULL) {
  1737. /* invalid id TSNH */
  1738. SCTP_PRINTF("TSNH ep_associd1\n");
  1739. return (NULL);
  1740. }
  1741. LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
  1742. if (stcb->asoc.assoc_id == id) {
  1743. if (inp != stcb->sctp_ep) {
  1744. /*
  1745. * some other guy has the same id active (id
  1746. * collision ??).
  1747. */
  1748. SCTP_PRINTF("TSNH ep_associd2\n");
  1749. continue;
  1750. }
  1751. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  1752. continue;
  1753. }
  1754. if (want_lock) {
  1755. SCTP_TCB_LOCK(stcb);
  1756. }
  1757. return (stcb);
  1758. }
  1759. }
  1760. return (NULL);
  1761. }
  1762. struct sctp_tcb *
  1763. sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
  1764. {
  1765. struct sctp_tcb *stcb;
  1766. SCTP_INP_RLOCK(inp);
  1767. stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
  1768. SCTP_INP_RUNLOCK(inp);
  1769. return (stcb);
  1770. }
  1771. /*
  1772. * Endpoint probe expects that the INP_INFO is locked.
  1773. */
  1774. static struct sctp_inpcb *
  1775. sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
  1776. uint16_t lport, uint32_t vrf_id)
  1777. {
  1778. struct sctp_inpcb *inp;
  1779. struct sctp_laddr *laddr;
  1780. #ifdef INET
  1781. struct sockaddr_in *sin;
  1782. #endif
  1783. #ifdef INET6
  1784. struct sockaddr_in6 *sin6;
  1785. struct sockaddr_in6 *intf_addr6;
  1786. #endif
  1787. #if defined(__Userspace__)
  1788. struct sockaddr_conn *sconn;
  1789. #endif
  1790. #ifdef SCTP_MVRF
  1791. int i;
  1792. #endif
  1793. int fnd;
  1794. #ifdef INET
  1795. sin = NULL;
  1796. #endif
  1797. #ifdef INET6
  1798. sin6 = NULL;
  1799. #endif
  1800. #if defined(__Userspace__)
  1801. sconn = NULL;
  1802. #endif
  1803. switch (nam->sa_family) {
  1804. #ifdef INET
  1805. case AF_INET:
  1806. sin = (struct sockaddr_in *)nam;
  1807. break;
  1808. #endif
  1809. #ifdef INET6
  1810. case AF_INET6:
  1811. sin6 = (struct sockaddr_in6 *)nam;
  1812. break;
  1813. #endif
  1814. #if defined(__Userspace__)
  1815. case AF_CONN:
  1816. sconn = (struct sockaddr_conn *)nam;
  1817. break;
  1818. #endif
  1819. default:
  1820. /* unsupported family */
  1821. return (NULL);
  1822. }
  1823. if (head == NULL)
  1824. return (NULL);
  1825. LIST_FOREACH(inp, head, sctp_hash) {
  1826. SCTP_INP_RLOCK(inp);
  1827. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  1828. SCTP_INP_RUNLOCK(inp);
  1829. continue;
  1830. }
  1831. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
  1832. (inp->sctp_lport == lport)) {
  1833. /* got it */
  1834. switch (nam->sa_family) {
  1835. #ifdef INET
  1836. case AF_INET:
  1837. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  1838. SCTP_IPV6_V6ONLY(inp)) {
  1839. /* IPv4 on a IPv6 socket with ONLY IPv6 set */
  1840. SCTP_INP_RUNLOCK(inp);
  1841. continue;
  1842. }
  1843. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1844. if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  1845. &sin->sin_addr) != 0) {
  1846. SCTP_INP_RUNLOCK(inp);
  1847. continue;
  1848. }
  1849. #endif
  1850. break;
  1851. #endif
  1852. #ifdef INET6
  1853. case AF_INET6:
  1854. /* A V6 address and the endpoint is NOT bound V6 */
  1855. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
  1856. SCTP_INP_RUNLOCK(inp);
  1857. continue;
  1858. }
  1859. #if defined(__FreeBSD__) && !defined(__Userspace__)
  1860. if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  1861. &sin6->sin6_addr) != 0) {
  1862. SCTP_INP_RUNLOCK(inp);
  1863. continue;
  1864. }
  1865. #endif
  1866. break;
  1867. #endif
  1868. default:
  1869. break;
  1870. }
  1871. /* does a VRF id match? */
  1872. fnd = 0;
  1873. #ifdef SCTP_MVRF
  1874. for (i = 0; i < inp->num_vrfs; i++) {
  1875. if (inp->m_vrf_ids[i] == vrf_id) {
  1876. fnd = 1;
  1877. break;
  1878. }
  1879. }
  1880. #else
  1881. if (inp->def_vrf_id == vrf_id)
  1882. fnd = 1;
  1883. #endif
  1884. SCTP_INP_RUNLOCK(inp);
  1885. if (!fnd)
  1886. continue;
  1887. return (inp);
  1888. }
  1889. SCTP_INP_RUNLOCK(inp);
  1890. }
  1891. switch (nam->sa_family) {
  1892. #ifdef INET
  1893. case AF_INET:
  1894. if (sin->sin_addr.s_addr == INADDR_ANY) {
  1895. /* Can't hunt for one that has no address specified */
  1896. return (NULL);
  1897. }
  1898. break;
  1899. #endif
  1900. #ifdef INET6
  1901. case AF_INET6:
  1902. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  1903. /* Can't hunt for one that has no address specified */
  1904. return (NULL);
  1905. }
  1906. break;
  1907. #endif
  1908. #if defined(__Userspace__)
  1909. case AF_CONN:
  1910. if (sconn->sconn_addr == NULL) {
  1911. return (NULL);
  1912. }
  1913. break;
  1914. #endif
  1915. default:
  1916. break;
  1917. }
  1918. /*
  1919. * ok, not bound to all so see if we can find a EP bound to this
  1920. * address.
  1921. */
  1922. LIST_FOREACH(inp, head, sctp_hash) {
  1923. SCTP_INP_RLOCK(inp);
  1924. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  1925. SCTP_INP_RUNLOCK(inp);
  1926. continue;
  1927. }
  1928. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
  1929. SCTP_INP_RUNLOCK(inp);
  1930. continue;
  1931. }
  1932. /*
  1933. * Ok this could be a likely candidate, look at all of its
  1934. * addresses
  1935. */
  1936. if (inp->sctp_lport != lport) {
  1937. SCTP_INP_RUNLOCK(inp);
  1938. continue;
  1939. }
  1940. /* does a VRF id match? */
  1941. fnd = 0;
  1942. #ifdef SCTP_MVRF
  1943. for (i = 0; i < inp->num_vrfs; i++) {
  1944. if (inp->m_vrf_ids[i] == vrf_id) {
  1945. fnd = 1;
  1946. break;
  1947. }
  1948. }
  1949. #else
  1950. if (inp->def_vrf_id == vrf_id)
  1951. fnd = 1;
  1952. #endif
  1953. if (!fnd) {
  1954. SCTP_INP_RUNLOCK(inp);
  1955. continue;
  1956. }
  1957. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  1958. if (laddr->ifa == NULL) {
  1959. SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
  1960. __func__);
  1961. continue;
  1962. }
  1963. SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
  1964. (void *)laddr->ifa);
  1965. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
  1966. SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
  1967. continue;
  1968. }
  1969. if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
  1970. /* possible, see if it matches */
  1971. switch (nam->sa_family) {
  1972. #ifdef INET
  1973. case AF_INET:
  1974. #if defined(__APPLE__) && !defined(__Userspace__)
  1975. if (sin == NULL) {
  1976. /* TSNH */
  1977. break;
  1978. }
  1979. #endif
  1980. if (sin->sin_addr.s_addr ==
  1981. laddr->ifa->address.sin.sin_addr.s_addr) {
  1982. SCTP_INP_RUNLOCK(inp);
  1983. return (inp);
  1984. }
  1985. break;
  1986. #endif
  1987. #ifdef INET6
  1988. case AF_INET6:
  1989. intf_addr6 = &laddr->ifa->address.sin6;
  1990. if (SCTP6_ARE_ADDR_EQUAL(sin6,
  1991. intf_addr6)) {
  1992. SCTP_INP_RUNLOCK(inp);
  1993. return (inp);
  1994. }
  1995. break;
  1996. #endif
  1997. #if defined(__Userspace__)
  1998. case AF_CONN:
  1999. if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
  2000. SCTP_INP_RUNLOCK(inp);
  2001. return (inp);
  2002. }
  2003. break;
  2004. #endif
  2005. }
  2006. }
  2007. }
  2008. SCTP_INP_RUNLOCK(inp);
  2009. }
  2010. return (NULL);
  2011. }
  2012. static struct sctp_inpcb *
  2013. sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
  2014. {
  2015. struct sctppcbhead *head;
  2016. struct sctp_inpcb *t_inp;
  2017. #ifdef SCTP_MVRF
  2018. int i;
  2019. #endif
  2020. int fnd;
  2021. head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
  2022. SCTP_BASE_INFO(hashmark))];
  2023. LIST_FOREACH(t_inp, head, sctp_hash) {
  2024. if (t_inp->sctp_lport != lport) {
  2025. continue;
  2026. }
  2027. /* is it in the VRF in question */
  2028. fnd = 0;
  2029. #ifdef SCTP_MVRF
  2030. for (i = 0; i < inp->num_vrfs; i++) {
  2031. if (t_inp->m_vrf_ids[i] == vrf_id) {
  2032. fnd = 1;
  2033. break;
  2034. }
  2035. }
  2036. #else
  2037. if (t_inp->def_vrf_id == vrf_id)
  2038. fnd = 1;
  2039. #endif
  2040. if (!fnd)
  2041. continue;
  2042. /* This one is in use. */
  2043. /* check the v6/v4 binding issue */
  2044. if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  2045. SCTP_IPV6_V6ONLY(t_inp)) {
  2046. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  2047. /* collision in V6 space */
  2048. return (t_inp);
  2049. } else {
  2050. /* inp is BOUND_V4 no conflict */
  2051. continue;
  2052. }
  2053. } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  2054. /* t_inp is bound v4 and v6, conflict always */
  2055. return (t_inp);
  2056. } else {
  2057. /* t_inp is bound only V4 */
  2058. if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  2059. SCTP_IPV6_V6ONLY(inp)) {
  2060. /* no conflict */
  2061. continue;
  2062. }
  2063. /* else fall through to conflict */
  2064. }
  2065. return (t_inp);
  2066. }
  2067. return (NULL);
  2068. }
  2069. int
  2070. sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
  2071. {
  2072. /* For 1-2-1 with port reuse */
  2073. struct sctppcbhead *head;
  2074. struct sctp_inpcb *tinp, *ninp;
  2075. SCTP_INP_INFO_WLOCK_ASSERT();
  2076. SCTP_INP_WLOCK_ASSERT(inp);
  2077. if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
  2078. /* only works with port reuse on */
  2079. return (-1);
  2080. }
  2081. if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
  2082. return (0);
  2083. }
  2084. SCTP_INP_WUNLOCK(inp);
  2085. head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
  2086. SCTP_BASE_INFO(hashmark))];
  2087. /* Kick out all non-listeners to the TCP hash */
  2088. LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
  2089. if (tinp->sctp_lport != inp->sctp_lport) {
  2090. continue;
  2091. }
  2092. if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  2093. continue;
  2094. }
  2095. if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  2096. continue;
  2097. }
  2098. if (SCTP_IS_LISTENING(tinp)) {
  2099. continue;
  2100. }
  2101. SCTP_INP_WLOCK(tinp);
  2102. LIST_REMOVE(tinp, sctp_hash);
  2103. head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
  2104. tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
  2105. LIST_INSERT_HEAD(head, tinp, sctp_hash);
  2106. SCTP_INP_WUNLOCK(tinp);
  2107. }
  2108. SCTP_INP_WLOCK(inp);
  2109. /* Pull from where he was */
  2110. LIST_REMOVE(inp, sctp_hash);
  2111. inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
  2112. head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
  2113. LIST_INSERT_HEAD(head, inp, sctp_hash);
  2114. return (0);
  2115. }
  2116. struct sctp_inpcb *
  2117. sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
  2118. uint32_t vrf_id)
  2119. {
  2120. /*
  2121. * First we check the hash table to see if someone has this port
  2122. * bound with just the port.
  2123. */
  2124. struct sctp_inpcb *inp;
  2125. struct sctppcbhead *head;
  2126. int lport;
  2127. unsigned int i;
  2128. #ifdef INET
  2129. struct sockaddr_in *sin;
  2130. #endif
  2131. #ifdef INET6
  2132. struct sockaddr_in6 *sin6;
  2133. #endif
  2134. #if defined(__Userspace__)
  2135. struct sockaddr_conn *sconn;
  2136. #endif
  2137. switch (nam->sa_family) {
  2138. #ifdef INET
  2139. case AF_INET:
  2140. sin = (struct sockaddr_in *)nam;
  2141. lport = sin->sin_port;
  2142. break;
  2143. #endif
  2144. #ifdef INET6
  2145. case AF_INET6:
  2146. sin6 = (struct sockaddr_in6 *)nam;
  2147. lport = sin6->sin6_port;
  2148. break;
  2149. #endif
  2150. #if defined(__Userspace__)
  2151. case AF_CONN:
  2152. sconn = (struct sockaddr_conn *)nam;
  2153. lport = sconn->sconn_port;
  2154. break;
  2155. #endif
  2156. default:
  2157. return (NULL);
  2158. }
  2159. /*
  2160. * I could cheat here and just cast to one of the types but we will
  2161. * do it right. It also provides the check against an Unsupported
  2162. * type too.
  2163. */
  2164. /* Find the head of the ALLADDR chain */
  2165. if (have_lock == 0) {
  2166. SCTP_INP_INFO_RLOCK();
  2167. }
  2168. head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
  2169. SCTP_BASE_INFO(hashmark))];
  2170. inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
  2171. /*
  2172. * If the TCP model exists it could be that the main listening
  2173. * endpoint is gone but there still exists a connected socket for this
  2174. * guy. If so we can return the first one that we find. This may NOT
  2175. * be the correct one so the caller should be wary on the returned INP.
  2176. * Currently the only caller that sets find_tcp_pool is in bindx where
  2177. * we are verifying that a user CAN bind the address. He either
  2178. * has bound it already, or someone else has, or its open to bind,
  2179. * so this is good enough.
  2180. */
  2181. if (inp == NULL && find_tcp_pool) {
  2182. for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
  2183. head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
  2184. inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
  2185. if (inp) {
  2186. break;
  2187. }
  2188. }
  2189. }
  2190. if (inp) {
  2191. SCTP_INP_INCR_REF(inp);
  2192. }
  2193. if (have_lock == 0) {
  2194. SCTP_INP_INFO_RUNLOCK();
  2195. }
  2196. return (inp);
  2197. }
  2198. /*
  2199. * Find an association for an endpoint with the pointer to whom you want to
  2200. * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
  2201. * need to change the *to to some other struct like a mbuf...
  2202. */
  2203. struct sctp_tcb *
  2204. sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
  2205. struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
  2206. uint32_t vrf_id)
  2207. {
  2208. struct sctp_inpcb *inp = NULL;
  2209. struct sctp_tcb *stcb;
  2210. SCTP_INP_INFO_RLOCK();
  2211. if (find_tcp_pool) {
  2212. if (inp_p != NULL) {
  2213. stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
  2214. vrf_id);
  2215. } else {
  2216. stcb = sctp_tcb_special_locate(&inp, from, to, netp,
  2217. vrf_id);
  2218. }
  2219. if (stcb != NULL) {
  2220. SCTP_INP_INFO_RUNLOCK();
  2221. return (stcb);
  2222. }
  2223. }
  2224. inp = sctp_pcb_findep(to, 0, 1, vrf_id);
  2225. if (inp_p != NULL) {
  2226. *inp_p = inp;
  2227. }
  2228. SCTP_INP_INFO_RUNLOCK();
  2229. if (inp == NULL) {
  2230. return (NULL);
  2231. }
  2232. /*
  2233. * ok, we have an endpoint, now lets find the assoc for it (if any)
  2234. * we now place the source address or from in the to of the find
  2235. * endpoint call. Since in reality this chain is used from the
  2236. * inbound packet side.
  2237. */
  2238. if (inp_p != NULL) {
  2239. stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
  2240. NULL);
  2241. } else {
  2242. stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
  2243. NULL);
  2244. }
  2245. return (stcb);
  2246. }
  2247. /*
  2248. * This routine will grub through the mbuf that is a INIT or INIT-ACK and
  2249. * find all addresses that the sender has specified in any address list. Each
  2250. * address will be used to lookup the TCB and see if one exits.
  2251. */
  2252. static struct sctp_tcb *
  2253. sctp_findassociation_special_addr(struct mbuf *m, int offset,
  2254. struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
  2255. struct sockaddr *dst)
  2256. {
  2257. struct sctp_paramhdr *phdr, param_buf;
  2258. #if defined(INET) || defined(INET6)
  2259. struct sctp_tcb *stcb;
  2260. uint16_t ptype;
  2261. #endif
  2262. uint16_t plen;
  2263. #ifdef INET
  2264. struct sockaddr_in sin4;
  2265. #endif
  2266. #ifdef INET6
  2267. struct sockaddr_in6 sin6;
  2268. #endif
  2269. #ifdef INET
  2270. memset(&sin4, 0, sizeof(sin4));
  2271. #ifdef HAVE_SIN_LEN
  2272. sin4.sin_len = sizeof(sin4);
  2273. #endif
  2274. sin4.sin_family = AF_INET;
  2275. sin4.sin_port = sh->src_port;
  2276. #endif
  2277. #ifdef INET6
  2278. memset(&sin6, 0, sizeof(sin6));
  2279. #ifdef HAVE_SIN6_LEN
  2280. sin6.sin6_len = sizeof(sin6);
  2281. #endif
  2282. sin6.sin6_family = AF_INET6;
  2283. sin6.sin6_port = sh->src_port;
  2284. #endif
  2285. offset += sizeof(struct sctp_init_chunk);
  2286. phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
  2287. while (phdr != NULL) {
  2288. /* now we must see if we want the parameter */
  2289. #if defined(INET) || defined(INET6)
  2290. ptype = ntohs(phdr->param_type);
  2291. #endif
  2292. plen = ntohs(phdr->param_length);
  2293. if (plen == 0) {
  2294. break;
  2295. }
  2296. #ifdef INET
  2297. if (ptype == SCTP_IPV4_ADDRESS &&
  2298. plen == sizeof(struct sctp_ipv4addr_param)) {
  2299. /* Get the rest of the address */
  2300. struct sctp_ipv4addr_param ip4_param, *p4;
  2301. phdr = sctp_get_next_param(m, offset,
  2302. (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
  2303. if (phdr == NULL) {
  2304. return (NULL);
  2305. }
  2306. p4 = (struct sctp_ipv4addr_param *)phdr;
  2307. memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
  2308. /* look it up */
  2309. stcb = sctp_findassociation_ep_addr(inp_p,
  2310. (struct sockaddr *)&sin4, netp, dst, NULL);
  2311. if (stcb != NULL) {
  2312. return (stcb);
  2313. }
  2314. }
  2315. #endif
  2316. #ifdef INET6
  2317. if (ptype == SCTP_IPV6_ADDRESS &&
  2318. plen == sizeof(struct sctp_ipv6addr_param)) {
  2319. /* Get the rest of the address */
  2320. struct sctp_ipv6addr_param ip6_param, *p6;
  2321. phdr = sctp_get_next_param(m, offset,
  2322. (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
  2323. if (phdr == NULL) {
  2324. return (NULL);
  2325. }
  2326. p6 = (struct sctp_ipv6addr_param *)phdr;
  2327. memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
  2328. /* look it up */
  2329. stcb = sctp_findassociation_ep_addr(inp_p,
  2330. (struct sockaddr *)&sin6, netp, dst, NULL);
  2331. if (stcb != NULL) {
  2332. return (stcb);
  2333. }
  2334. }
  2335. #endif
  2336. offset += SCTP_SIZE32(plen);
  2337. phdr = sctp_get_next_param(m, offset, &param_buf,
  2338. sizeof(param_buf));
  2339. }
  2340. return (NULL);
  2341. }
  2342. static struct sctp_tcb *
  2343. sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
  2344. struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
  2345. uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
  2346. {
  2347. /*
  2348. * Use my vtag to hash. If we find it we then verify the source addr
  2349. * is in the assoc. If all goes well we save a bit on rec of a
  2350. * packet.
  2351. */
  2352. struct sctpasochead *head;
  2353. struct sctp_nets *net;
  2354. struct sctp_tcb *stcb;
  2355. #ifdef SCTP_MVRF
  2356. unsigned int i;
  2357. #endif
  2358. SCTP_INP_INFO_RLOCK();
  2359. head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
  2360. SCTP_BASE_INFO(hashasocmark))];
  2361. LIST_FOREACH(stcb, head, sctp_asocs) {
  2362. SCTP_INP_RLOCK(stcb->sctp_ep);
  2363. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  2364. SCTP_INP_RUNLOCK(stcb->sctp_ep);
  2365. continue;
  2366. }
  2367. #ifdef SCTP_MVRF
  2368. for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
  2369. if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
  2370. break;
  2371. }
  2372. }
  2373. if (i == stcb->sctp_ep->num_vrfs) {
  2374. SCTP_INP_RUNLOCK(inp);
  2375. continue;
  2376. }
  2377. #else
  2378. if (stcb->sctp_ep->def_vrf_id != vrf_id) {
  2379. SCTP_INP_RUNLOCK(stcb->sctp_ep);
  2380. continue;
  2381. }
  2382. #endif
  2383. SCTP_TCB_LOCK(stcb);
  2384. SCTP_INP_RUNLOCK(stcb->sctp_ep);
  2385. if (stcb->asoc.my_vtag == vtag) {
  2386. /* candidate */
  2387. if (stcb->rport != rport) {
  2388. SCTP_TCB_UNLOCK(stcb);
  2389. continue;
  2390. }
  2391. if (stcb->sctp_ep->sctp_lport != lport) {
  2392. SCTP_TCB_UNLOCK(stcb);
  2393. continue;
  2394. }
  2395. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  2396. SCTP_TCB_UNLOCK(stcb);
  2397. continue;
  2398. }
  2399. /* RRS:Need toaddr check here */
  2400. if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
  2401. /* Endpoint does not own this address */
  2402. SCTP_TCB_UNLOCK(stcb);
  2403. continue;
  2404. }
  2405. if (remote_tag) {
  2406. /* If we have both vtags that's all we match on */
  2407. if (stcb->asoc.peer_vtag == remote_tag) {
  2408. /* If both tags match we consider it conclusive
  2409. * and check NO source/destination addresses
  2410. */
  2411. goto conclusive;
  2412. }
  2413. }
  2414. if (skip_src_check) {
  2415. conclusive:
  2416. if (from) {
  2417. *netp = sctp_findnet(stcb, from);
  2418. } else {
  2419. *netp = NULL; /* unknown */
  2420. }
  2421. if (inp_p)
  2422. *inp_p = stcb->sctp_ep;
  2423. SCTP_INP_INFO_RUNLOCK();
  2424. return (stcb);
  2425. }
  2426. net = sctp_findnet(stcb, from);
  2427. if (net) {
  2428. /* yep its him. */
  2429. *netp = net;
  2430. SCTP_STAT_INCR(sctps_vtagexpress);
  2431. *inp_p = stcb->sctp_ep;
  2432. SCTP_INP_INFO_RUNLOCK();
  2433. return (stcb);
  2434. } else {
  2435. /*
  2436. * not him, this should only happen in rare
  2437. * cases so I peg it.
  2438. */
  2439. SCTP_STAT_INCR(sctps_vtagbogus);
  2440. }
  2441. }
  2442. SCTP_TCB_UNLOCK(stcb);
  2443. }
  2444. SCTP_INP_INFO_RUNLOCK();
  2445. return (NULL);
  2446. }
  2447. /*
  2448. * Find an association with the pointer to the inbound IP packet. This can be
  2449. * a IPv4 or IPv6 packet.
  2450. */
  2451. struct sctp_tcb *
  2452. sctp_findassociation_addr(struct mbuf *m, int offset,
  2453. struct sockaddr *src, struct sockaddr *dst,
  2454. struct sctphdr *sh, struct sctp_chunkhdr *ch,
  2455. struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
  2456. {
  2457. struct sctp_tcb *stcb;
  2458. struct sctp_inpcb *inp;
  2459. if (sh->v_tag) {
  2460. /* we only go down this path if vtag is non-zero */
  2461. stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
  2462. inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
  2463. if (stcb) {
  2464. return (stcb);
  2465. }
  2466. }
  2467. if (inp_p) {
  2468. stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
  2469. 1, vrf_id);
  2470. inp = *inp_p;
  2471. } else {
  2472. stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
  2473. 1, vrf_id);
  2474. }
  2475. SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
  2476. if (stcb == NULL && inp) {
  2477. /* Found a EP but not this address */
  2478. if ((ch->chunk_type == SCTP_INITIATION) ||
  2479. (ch->chunk_type == SCTP_INITIATION_ACK)) {
  2480. /*-
  2481. * special hook, we do NOT return linp or an
  2482. * association that is linked to an existing
  2483. * association that is under the TCP pool (i.e. no
  2484. * listener exists). The endpoint finding routine
  2485. * will always find a listener before examining the
  2486. * TCP pool.
  2487. */
  2488. if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
  2489. if (inp_p) {
  2490. *inp_p = NULL;
  2491. }
  2492. return (NULL);
  2493. }
  2494. stcb = sctp_findassociation_special_addr(m,
  2495. offset, sh, &inp, netp, dst);
  2496. if (inp_p != NULL) {
  2497. *inp_p = inp;
  2498. }
  2499. }
  2500. }
  2501. SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
  2502. return (stcb);
  2503. }
  2504. /*
  2505. * lookup an association by an ASCONF lookup address.
  2506. * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
  2507. */
  2508. struct sctp_tcb *
  2509. sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
  2510. struct sockaddr *dst, struct sctphdr *sh,
  2511. struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
  2512. {
  2513. struct sctp_tcb *stcb;
  2514. union sctp_sockstore remote_store;
  2515. struct sctp_paramhdr param_buf, *phdr;
  2516. int ptype;
  2517. int zero_address = 0;
  2518. #ifdef INET
  2519. struct sockaddr_in *sin;
  2520. #endif
  2521. #ifdef INET6
  2522. struct sockaddr_in6 *sin6;
  2523. #endif
  2524. memset(&remote_store, 0, sizeof(remote_store));
  2525. phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
  2526. &param_buf, sizeof(struct sctp_paramhdr));
  2527. if (phdr == NULL) {
  2528. SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
  2529. __func__);
  2530. return NULL;
  2531. }
  2532. ptype = (int)((uint32_t) ntohs(phdr->param_type));
  2533. /* get the correlation address */
  2534. switch (ptype) {
  2535. #ifdef INET6
  2536. case SCTP_IPV6_ADDRESS:
  2537. {
  2538. /* ipv6 address param */
  2539. struct sctp_ipv6addr_param *p6, p6_buf;
  2540. if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
  2541. return NULL;
  2542. }
  2543. p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
  2544. offset + sizeof(struct sctp_asconf_chunk),
  2545. &p6_buf.ph, sizeof(p6_buf));
  2546. if (p6 == NULL) {
  2547. SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
  2548. __func__);
  2549. return (NULL);
  2550. }
  2551. sin6 = &remote_store.sin6;
  2552. sin6->sin6_family = AF_INET6;
  2553. #ifdef HAVE_SIN6_LEN
  2554. sin6->sin6_len = sizeof(*sin6);
  2555. #endif
  2556. sin6->sin6_port = sh->src_port;
  2557. memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
  2558. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  2559. zero_address = 1;
  2560. break;
  2561. }
  2562. #endif
  2563. #ifdef INET
  2564. case SCTP_IPV4_ADDRESS:
  2565. {
  2566. /* ipv4 address param */
  2567. struct sctp_ipv4addr_param *p4, p4_buf;
  2568. if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
  2569. return NULL;
  2570. }
  2571. p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
  2572. offset + sizeof(struct sctp_asconf_chunk),
  2573. &p4_buf.ph, sizeof(p4_buf));
  2574. if (p4 == NULL) {
  2575. SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
  2576. __func__);
  2577. return (NULL);
  2578. }
  2579. sin = &remote_store.sin;
  2580. sin->sin_family = AF_INET;
  2581. #ifdef HAVE_SIN_LEN
  2582. sin->sin_len = sizeof(*sin);
  2583. #endif
  2584. sin->sin_port = sh->src_port;
  2585. memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
  2586. if (sin->sin_addr.s_addr == INADDR_ANY)
  2587. zero_address = 1;
  2588. break;
  2589. }
  2590. #endif
  2591. default:
  2592. /* invalid address param type */
  2593. return NULL;
  2594. }
  2595. if (zero_address) {
  2596. stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
  2597. netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
  2598. if (stcb != NULL) {
  2599. SCTP_INP_DECR_REF(*inp_p);
  2600. }
  2601. } else {
  2602. stcb = sctp_findassociation_ep_addr(inp_p,
  2603. &remote_store.sa, netp,
  2604. dst, NULL);
  2605. }
  2606. return (stcb);
  2607. }
  2608. /*
  2609. * allocate a sctp_inpcb and setup a temporary binding to a port/all
  2610. * addresses. This way if we don't get a bind we by default pick a ephemeral
  2611. * port with all addresses bound.
  2612. */
  2613. int
  2614. sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
  2615. {
  2616. /*
  2617. * we get called when a new endpoint starts up. We need to allocate
  2618. * the sctp_inpcb structure from the zone and init it. Mark it as
  2619. * unbound and find a port that we can use as an ephemeral with
  2620. * INADDR_ANY. If the user binds later no problem we can then add in
  2621. * the specific addresses. And setup the default parameters for the
  2622. * EP.
  2623. */
  2624. int i, error;
  2625. struct sctp_inpcb *inp;
  2626. struct sctp_pcb *m;
  2627. struct timeval time;
  2628. sctp_sharedkey_t *null_key;
  2629. error = 0;
  2630. SCTP_INP_INFO_WLOCK();
  2631. inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
  2632. if (inp == NULL) {
  2633. SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
  2634. SCTP_INP_INFO_WUNLOCK();
  2635. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
  2636. return (ENOBUFS);
  2637. }
  2638. /* zap it */
  2639. memset(inp, 0, sizeof(*inp));
  2640. /* bump generations */
  2641. #if defined(__APPLE__) && !defined(__Userspace__)
  2642. inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
  2643. #endif
  2644. /* setup socket pointers */
  2645. inp->sctp_socket = so;
  2646. inp->ip_inp.inp.inp_socket = so;
  2647. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2648. inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
  2649. #endif
  2650. #ifdef INET6
  2651. #if !defined(__Userspace__) && !defined(_WIN32)
  2652. if (INP_SOCKAF(so) == AF_INET6) {
  2653. if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
  2654. inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
  2655. }
  2656. if (MODULE_GLOBAL(ip6_v6only)) {
  2657. inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
  2658. }
  2659. }
  2660. #endif
  2661. #endif
  2662. inp->sctp_associd_counter = 1;
  2663. inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
  2664. inp->sctp_frag_point = 0;
  2665. inp->max_cwnd = 0;
  2666. inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
  2667. inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
  2668. inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
  2669. inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
  2670. inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
  2671. inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
  2672. inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
  2673. inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
  2674. inp->idata_supported = 0;
  2675. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2676. inp->fibnum = so->so_fibnum;
  2677. #else
  2678. inp->fibnum = 0;
  2679. #endif
  2680. #if defined(__Userspace__)
  2681. inp->ulp_info = NULL;
  2682. inp->recv_callback = NULL;
  2683. inp->send_callback = NULL;
  2684. inp->send_sb_threshold = 0;
  2685. #endif
  2686. /* init the small hash table we use to track asocid <-> tcb */
  2687. inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
  2688. if (inp->sctp_asocidhash == NULL) {
  2689. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2690. crfree(inp->ip_inp.inp.inp_cred);
  2691. #endif
  2692. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
  2693. SCTP_INP_INFO_WUNLOCK();
  2694. return (ENOBUFS);
  2695. }
  2696. SCTP_INCR_EP_COUNT();
  2697. inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
  2698. SCTP_INP_INFO_WUNLOCK();
  2699. so->so_pcb = (caddr_t)inp;
  2700. if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
  2701. /* UDP style socket */
  2702. inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
  2703. SCTP_PCB_FLAGS_UNBOUND);
  2704. /* Be sure it is NON-BLOCKING IO for UDP */
  2705. /* SCTP_SET_SO_NBIO(so); */
  2706. } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
  2707. /* TCP style socket */
  2708. inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
  2709. SCTP_PCB_FLAGS_UNBOUND);
  2710. /* Be sure we have blocking IO by default */
  2711. SOCK_LOCK(so);
  2712. SCTP_CLEAR_SO_NBIO(so);
  2713. SOCK_UNLOCK(so);
  2714. } else {
  2715. /*
  2716. * unsupported socket type (RAW, etc)- in case we missed it
  2717. * in protosw
  2718. */
  2719. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
  2720. so->so_pcb = NULL;
  2721. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2722. crfree(inp->ip_inp.inp.inp_cred);
  2723. #endif
  2724. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
  2725. return (EOPNOTSUPP);
  2726. }
  2727. if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
  2728. sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
  2729. sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
  2730. } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
  2731. sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
  2732. sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
  2733. } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
  2734. sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
  2735. sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
  2736. }
  2737. inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
  2738. &inp->sctp_hashmark);
  2739. if (inp->sctp_tcbhash == NULL) {
  2740. SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
  2741. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
  2742. so->so_pcb = NULL;
  2743. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2744. crfree(inp->ip_inp.inp.inp_cred);
  2745. #endif
  2746. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
  2747. return (ENOBUFS);
  2748. }
  2749. #ifdef SCTP_MVRF
  2750. inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
  2751. SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
  2752. (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
  2753. if (inp->m_vrf_ids == NULL) {
  2754. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
  2755. so->so_pcb = NULL;
  2756. SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
  2757. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2758. crfree(inp->ip_inp.inp.inp_cred);
  2759. #endif
  2760. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
  2761. return (ENOBUFS);
  2762. }
  2763. inp->m_vrf_ids[0] = vrf_id;
  2764. inp->num_vrfs = 1;
  2765. #endif
  2766. inp->def_vrf_id = vrf_id;
  2767. #if defined(__APPLE__) && !defined(__Userspace__)
  2768. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  2769. inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
  2770. if (inp->ip_inp.inp.inpcb_mtx == NULL) {
  2771. SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
  2772. #ifdef SCTP_MVRF
  2773. SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
  2774. #endif
  2775. SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
  2776. so->so_pcb = NULL;
  2777. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
  2778. SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock);
  2779. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
  2780. return (ENOMEM);
  2781. }
  2782. #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
  2783. lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
  2784. #else
  2785. lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
  2786. #endif
  2787. #endif
  2788. SCTP_INP_INFO_WLOCK();
  2789. SCTP_INP_LOCK_INIT(inp);
  2790. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2791. rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp",
  2792. RW_RECURSE | RW_DUPOK);
  2793. #endif
  2794. SCTP_INP_READ_INIT(inp);
  2795. SCTP_ASOC_CREATE_LOCK_INIT(inp);
  2796. /* lock the new ep */
  2797. SCTP_INP_WLOCK(inp);
  2798. /* add it to the info area */
  2799. LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
  2800. #if defined(__APPLE__) && !defined(__Userspace__)
  2801. inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
  2802. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
  2803. LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
  2804. #else
  2805. LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
  2806. #endif
  2807. #endif
  2808. SCTP_INP_INFO_WUNLOCK();
  2809. TAILQ_INIT(&inp->read_queue);
  2810. LIST_INIT(&inp->sctp_addr_list);
  2811. LIST_INIT(&inp->sctp_asoc_list);
  2812. #ifdef SCTP_TRACK_FREED_ASOCS
  2813. /* TEMP CODE */
  2814. LIST_INIT(&inp->sctp_asoc_free_list);
  2815. #endif
  2816. /* Init the timer structure for signature change */
  2817. SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
  2818. inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
  2819. /* now init the actual endpoint default data */
  2820. m = &inp->sctp_ep;
  2821. /* setup the base timeout information */
  2822. m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
  2823. m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
  2824. m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
  2825. m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
  2826. m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
  2827. m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
  2828. m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
  2829. /* all max/min max are in ms */
  2830. m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
  2831. m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
  2832. m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
  2833. m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
  2834. m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
  2835. m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
  2836. m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
  2837. m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
  2838. m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
  2839. m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
  2840. m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
  2841. m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
  2842. m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
  2843. m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
  2844. m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
  2845. m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
  2846. /* number of streams to pre-open on a association */
  2847. m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
  2848. m->default_mtu = 0;
  2849. /* Add adaptation cookie */
  2850. m->adaptation_layer_indicator = 0;
  2851. m->adaptation_layer_indicator_provided = 0;
  2852. /* seed random number generator */
  2853. m->random_counter = 1;
  2854. m->store_at = SCTP_SIGNATURE_SIZE;
  2855. SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
  2856. sctp_fill_random_store(m);
  2857. /* Minimum cookie size */
  2858. m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
  2859. sizeof(struct sctp_state_cookie);
  2860. m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
  2861. /* Setup the initial secret */
  2862. (void)SCTP_GETTIME_TIMEVAL(&time);
  2863. m->time_of_secret_change = (unsigned int)time.tv_sec;
  2864. for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
  2865. m->secret_key[0][i] = sctp_select_initial_TSN(m);
  2866. }
  2867. sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
  2868. /* How long is a cookie good for ? */
  2869. m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
  2870. /*
  2871. * Initialize authentication parameters
  2872. */
  2873. m->local_hmacs = sctp_default_supported_hmaclist();
  2874. m->local_auth_chunks = sctp_alloc_chunklist();
  2875. if (inp->asconf_supported) {
  2876. sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
  2877. sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
  2878. }
  2879. m->default_dscp = 0;
  2880. #ifdef INET6
  2881. m->default_flowlabel = 0;
  2882. #endif
  2883. m->port = 0; /* encapsulation disabled by default */
  2884. LIST_INIT(&m->shared_keys);
  2885. /* add default NULL key as key id 0 */
  2886. null_key = sctp_alloc_sharedkey();
  2887. sctp_insert_sharedkey(&m->shared_keys, null_key);
  2888. SCTP_INP_WUNLOCK(inp);
  2889. #ifdef SCTP_LOG_CLOSING
  2890. sctp_log_closing(inp, NULL, 12);
  2891. #endif
  2892. return (error);
  2893. }
  2894. void
  2895. sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
  2896. struct sctp_tcb *stcb)
  2897. {
  2898. struct sctp_nets *net;
  2899. uint16_t lport, rport;
  2900. struct sctppcbhead *head;
  2901. struct sctp_laddr *laddr, *oladdr;
  2902. atomic_add_int(&stcb->asoc.refcnt, 1);
  2903. SCTP_TCB_UNLOCK(stcb);
  2904. SCTP_INP_INFO_WLOCK();
  2905. SCTP_INP_WLOCK(old_inp);
  2906. SCTP_INP_WLOCK(new_inp);
  2907. SCTP_TCB_LOCK(stcb);
  2908. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  2909. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2910. #ifdef INET6
  2911. if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  2912. new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS;
  2913. if (old_inp->ip_inp.inp.in6p_outputopts) {
  2914. new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT);
  2915. }
  2916. }
  2917. #endif
  2918. #if defined(INET) && defined(INET6)
  2919. else
  2920. #endif
  2921. #ifdef INET
  2922. {
  2923. new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos;
  2924. new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl;
  2925. }
  2926. #endif
  2927. #endif
  2928. new_inp->sctp_ep.time_of_secret_change =
  2929. old_inp->sctp_ep.time_of_secret_change;
  2930. memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
  2931. sizeof(old_inp->sctp_ep.secret_key));
  2932. new_inp->sctp_ep.current_secret_number =
  2933. old_inp->sctp_ep.current_secret_number;
  2934. new_inp->sctp_ep.last_secret_number =
  2935. old_inp->sctp_ep.last_secret_number;
  2936. new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
  2937. /* make it so new data pours into the new socket */
  2938. stcb->sctp_socket = new_inp->sctp_socket;
  2939. stcb->sctp_ep = new_inp;
  2940. /* Copy the port across */
  2941. lport = new_inp->sctp_lport = old_inp->sctp_lport;
  2942. rport = stcb->rport;
  2943. /* Pull the tcb from the old association */
  2944. LIST_REMOVE(stcb, sctp_tcbhash);
  2945. LIST_REMOVE(stcb, sctp_tcblist);
  2946. if (stcb->asoc.in_asocid_hash) {
  2947. LIST_REMOVE(stcb, sctp_tcbasocidhash);
  2948. }
  2949. /* Now insert the new_inp into the TCP connected hash */
  2950. head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
  2951. LIST_INSERT_HEAD(head, new_inp, sctp_hash);
  2952. /* Its safe to access */
  2953. new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
  2954. /* Now move the tcb into the endpoint list */
  2955. LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
  2956. /*
  2957. * Question, do we even need to worry about the ep-hash since we
  2958. * only have one connection? Probably not :> so lets get rid of it
  2959. * and not suck up any kernel memory in that.
  2960. */
  2961. if (stcb->asoc.in_asocid_hash) {
  2962. struct sctpasochead *lhd;
  2963. lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
  2964. new_inp->hashasocidmark)];
  2965. LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
  2966. }
  2967. /* Ok. Let's restart timer. */
  2968. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  2969. sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
  2970. stcb, net);
  2971. }
  2972. SCTP_INP_INFO_WUNLOCK();
  2973. if (new_inp->sctp_tcbhash != NULL) {
  2974. SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
  2975. new_inp->sctp_tcbhash = NULL;
  2976. }
  2977. if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
  2978. /* Subset bound, so copy in the laddr list from the old_inp */
  2979. LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
  2980. laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
  2981. if (laddr == NULL) {
  2982. /*
  2983. * Gak, what can we do? This assoc is really
  2984. * HOSED. We probably should send an abort
  2985. * here.
  2986. */
  2987. SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
  2988. continue;
  2989. }
  2990. SCTP_INCR_LADDR_COUNT();
  2991. memset(laddr, 0, sizeof(*laddr));
  2992. (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
  2993. laddr->ifa = oladdr->ifa;
  2994. atomic_add_int(&laddr->ifa->refcount, 1);
  2995. LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
  2996. sctp_nxt_addr);
  2997. new_inp->laddr_count++;
  2998. if (oladdr == stcb->asoc.last_used_address) {
  2999. stcb->asoc.last_used_address = laddr;
  3000. }
  3001. }
  3002. }
  3003. /* Now any running timers need to be adjusted. */
  3004. if (stcb->asoc.dack_timer.ep == old_inp) {
  3005. SCTP_INP_DECR_REF(old_inp);
  3006. stcb->asoc.dack_timer.ep = new_inp;
  3007. SCTP_INP_INCR_REF(new_inp);
  3008. }
  3009. if (stcb->asoc.asconf_timer.ep == old_inp) {
  3010. SCTP_INP_DECR_REF(old_inp);
  3011. stcb->asoc.asconf_timer.ep = new_inp;
  3012. SCTP_INP_INCR_REF(new_inp);
  3013. }
  3014. if (stcb->asoc.strreset_timer.ep == old_inp) {
  3015. SCTP_INP_DECR_REF(old_inp);
  3016. stcb->asoc.strreset_timer.ep = new_inp;
  3017. SCTP_INP_INCR_REF(new_inp);
  3018. }
  3019. if (stcb->asoc.shut_guard_timer.ep == old_inp) {
  3020. SCTP_INP_DECR_REF(old_inp);
  3021. stcb->asoc.shut_guard_timer.ep = new_inp;
  3022. SCTP_INP_INCR_REF(new_inp);
  3023. }
  3024. if (stcb->asoc.autoclose_timer.ep == old_inp) {
  3025. SCTP_INP_DECR_REF(old_inp);
  3026. stcb->asoc.autoclose_timer.ep = new_inp;
  3027. SCTP_INP_INCR_REF(new_inp);
  3028. }
  3029. if (stcb->asoc.delete_prim_timer.ep == old_inp) {
  3030. SCTP_INP_DECR_REF(old_inp);
  3031. stcb->asoc.delete_prim_timer.ep = new_inp;
  3032. SCTP_INP_INCR_REF(new_inp);
  3033. }
  3034. /* now what about the nets? */
  3035. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  3036. if (net->pmtu_timer.ep == old_inp) {
  3037. SCTP_INP_DECR_REF(old_inp);
  3038. net->pmtu_timer.ep = new_inp;
  3039. SCTP_INP_INCR_REF(new_inp);
  3040. }
  3041. if (net->hb_timer.ep == old_inp) {
  3042. SCTP_INP_DECR_REF(old_inp);
  3043. net->hb_timer.ep = new_inp;
  3044. SCTP_INP_INCR_REF(new_inp);
  3045. }
  3046. if (net->rxt_timer.ep == old_inp) {
  3047. SCTP_INP_DECR_REF(old_inp);
  3048. net->rxt_timer.ep = new_inp;
  3049. SCTP_INP_INCR_REF(new_inp);
  3050. }
  3051. }
  3052. SCTP_INP_WUNLOCK(new_inp);
  3053. SCTP_INP_WUNLOCK(old_inp);
  3054. }
  3055. /*
  3056. * insert an laddr entry with the given ifa for the desired list
  3057. */
  3058. static int
  3059. sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
  3060. {
  3061. struct sctp_laddr *laddr;
  3062. laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
  3063. if (laddr == NULL) {
  3064. /* out of memory? */
  3065. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  3066. return (EINVAL);
  3067. }
  3068. SCTP_INCR_LADDR_COUNT();
  3069. memset(laddr, 0, sizeof(*laddr));
  3070. (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
  3071. laddr->ifa = ifa;
  3072. laddr->action = act;
  3073. atomic_add_int(&ifa->refcount, 1);
  3074. /* insert it */
  3075. LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
  3076. return (0);
  3077. }
  3078. /*
  3079. * Remove an laddr entry from the local address list (on an assoc)
  3080. */
  3081. static void
  3082. sctp_remove_laddr(struct sctp_laddr *laddr)
  3083. {
  3084. /* remove from the list */
  3085. LIST_REMOVE(laddr, sctp_nxt_addr);
  3086. sctp_free_ifa(laddr->ifa);
  3087. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
  3088. SCTP_DECR_LADDR_COUNT();
  3089. }
  3090. #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
  3091. /*
  3092. * Don't know why, but without this there is an unknown reference when
  3093. * compiling NetBSD... hmm
  3094. */
  3095. extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
  3096. #endif
  3097. /*
  3098. * Bind the socket, with the PCB and global info locks held. Note, if a
  3099. * socket address is specified, the PCB lock may be dropped and re-acquired.
  3100. *
  3101. * sctp_ifap is used to bypass normal local address validation checks.
  3102. */
  3103. int
  3104. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3105. sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
  3106. struct sctp_ifa *sctp_ifap, struct thread *td)
  3107. #elif defined(_WIN32) && !defined(__Userspace__)
  3108. sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
  3109. struct sctp_ifa *sctp_ifap, PKTHREAD p)
  3110. #else
  3111. sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
  3112. struct sctp_ifa *sctp_ifap, struct proc *p)
  3113. #endif
  3114. {
  3115. /* bind a ep to a socket address */
  3116. struct sctppcbhead *head;
  3117. struct sctp_inpcb *inp_tmp;
  3118. #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
  3119. struct inpcb *ip_inp;
  3120. #endif
  3121. int port_reuse_active = 0;
  3122. int bindall;
  3123. #ifdef SCTP_MVRF
  3124. int i;
  3125. #endif
  3126. uint16_t lport;
  3127. int error;
  3128. uint32_t vrf_id;
  3129. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3130. KASSERT(td != NULL, ("%s: null thread", __func__));
  3131. #endif
  3132. error = 0;
  3133. lport = 0;
  3134. bindall = 1;
  3135. #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
  3136. ip_inp = &inp->ip_inp.inp;
  3137. #endif
  3138. SCTP_INP_INFO_WLOCK_ASSERT();
  3139. SCTP_INP_WLOCK_ASSERT(inp);
  3140. #ifdef SCTP_DEBUG
  3141. if (addr) {
  3142. SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
  3143. ntohs(((struct sockaddr_in *)addr)->sin_port));
  3144. SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
  3145. SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
  3146. }
  3147. #endif
  3148. if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
  3149. error = EINVAL;
  3150. /* already did a bind, subsequent binds NOT allowed ! */
  3151. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3152. goto out;
  3153. }
  3154. if (addr != NULL) {
  3155. switch (addr->sa_family) {
  3156. #ifdef INET
  3157. case AF_INET:
  3158. {
  3159. struct sockaddr_in *sin;
  3160. /* IPV6_V6ONLY socket? */
  3161. if (SCTP_IPV6_V6ONLY(inp)) {
  3162. error = EINVAL;
  3163. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3164. goto out;
  3165. }
  3166. #ifdef HAVE_SA_LEN
  3167. if (addr->sa_len != sizeof(*sin)) {
  3168. error = EINVAL;
  3169. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3170. goto out;
  3171. }
  3172. #endif
  3173. sin = (struct sockaddr_in *)addr;
  3174. lport = sin->sin_port;
  3175. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3176. /*
  3177. * For LOOPBACK the prison_local_ip4() call will transmute the ip address
  3178. * to the proper value.
  3179. */
  3180. if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) {
  3181. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3182. goto out;
  3183. }
  3184. #endif
  3185. if (sin->sin_addr.s_addr != INADDR_ANY) {
  3186. bindall = 0;
  3187. }
  3188. break;
  3189. }
  3190. #endif
  3191. #ifdef INET6
  3192. case AF_INET6:
  3193. {
  3194. /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
  3195. struct sockaddr_in6 *sin6;
  3196. sin6 = (struct sockaddr_in6 *)addr;
  3197. #ifdef HAVE_SA_LEN
  3198. if (addr->sa_len != sizeof(*sin6)) {
  3199. error = EINVAL;
  3200. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3201. goto out;
  3202. }
  3203. #endif
  3204. lport = sin6->sin6_port;
  3205. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3206. /*
  3207. * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
  3208. * to the proper value.
  3209. */
  3210. if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr,
  3211. (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
  3212. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3213. goto out;
  3214. }
  3215. #endif
  3216. if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  3217. bindall = 0;
  3218. #ifdef SCTP_EMBEDDED_V6_SCOPE
  3219. /* KAME hack: embed scopeid */
  3220. #if defined(SCTP_KAME)
  3221. if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
  3222. error = EINVAL;
  3223. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3224. goto out;
  3225. }
  3226. #elif defined(__APPLE__) && !defined(__Userspace__)
  3227. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  3228. if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
  3229. #else
  3230. if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
  3231. #endif
  3232. error = EINVAL;
  3233. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3234. goto out;
  3235. }
  3236. #elif defined(__FreeBSD__) && !defined(__Userspace__)
  3237. error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
  3238. if (error != 0) {
  3239. error = EINVAL;
  3240. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3241. goto out;
  3242. }
  3243. #else
  3244. if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
  3245. error = EINVAL;
  3246. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3247. goto out;
  3248. }
  3249. #endif
  3250. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  3251. }
  3252. #ifndef SCOPEDROUTING
  3253. /* this must be cleared for ifa_ifwithaddr() */
  3254. sin6->sin6_scope_id = 0;
  3255. #endif /* SCOPEDROUTING */
  3256. break;
  3257. }
  3258. #endif
  3259. #if defined(__Userspace__)
  3260. case AF_CONN:
  3261. {
  3262. struct sockaddr_conn *sconn;
  3263. #ifdef HAVE_SA_LEN
  3264. if (addr->sa_len != sizeof(struct sockaddr_conn)) {
  3265. error = EINVAL;
  3266. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3267. goto out;
  3268. }
  3269. #endif
  3270. sconn = (struct sockaddr_conn *)addr;
  3271. lport = sconn->sconn_port;
  3272. if (sconn->sconn_addr != NULL) {
  3273. bindall = 0;
  3274. }
  3275. break;
  3276. }
  3277. #endif
  3278. default:
  3279. error = EAFNOSUPPORT;
  3280. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3281. goto out;
  3282. }
  3283. }
  3284. /* Setup a vrf_id to be the default for the non-bind-all case. */
  3285. vrf_id = inp->def_vrf_id;
  3286. if (lport) {
  3287. /*
  3288. * Did the caller specify a port? if so we must see if an ep
  3289. * already has this one bound.
  3290. */
  3291. /* got to be root to get at low ports */
  3292. #if !(defined(_WIN32) && !defined(__Userspace__))
  3293. if (ntohs(lport) < IPPORT_RESERVED &&
  3294. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3295. (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
  3296. #elif defined(__APPLE__) && !defined(__Userspace__)
  3297. (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
  3298. #elif defined(__Userspace__)
  3299. /* TODO ensure uid is 0, etc... */
  3300. 0) {
  3301. #else
  3302. (error = suser(p, 0)) != 0) {
  3303. #endif
  3304. goto out;
  3305. }
  3306. #endif
  3307. SCTP_INP_INCR_REF(inp);
  3308. SCTP_INP_WUNLOCK(inp);
  3309. if (bindall) {
  3310. #ifdef SCTP_MVRF
  3311. for (i = 0; i < inp->num_vrfs; i++) {
  3312. vrf_id = inp->m_vrf_ids[i];
  3313. #else
  3314. vrf_id = inp->def_vrf_id;
  3315. #endif
  3316. inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
  3317. if (inp_tmp != NULL) {
  3318. /*
  3319. * lock guy returned and lower count
  3320. * note that we are not bound so
  3321. * inp_tmp should NEVER be inp. And
  3322. * it is this inp (inp_tmp) that gets
  3323. * the reference bump, so we must
  3324. * lower it.
  3325. */
  3326. SCTP_INP_DECR_REF(inp_tmp);
  3327. /* unlock info */
  3328. if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
  3329. (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
  3330. /* Ok, must be one-2-one and allowing port re-use */
  3331. port_reuse_active = 1;
  3332. goto continue_anyway;
  3333. }
  3334. SCTP_INP_WLOCK(inp);
  3335. SCTP_INP_DECR_REF(inp);
  3336. error = EADDRINUSE;
  3337. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3338. goto out;
  3339. }
  3340. #ifdef SCTP_MVRF
  3341. }
  3342. #endif
  3343. } else {
  3344. inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
  3345. if (inp_tmp != NULL) {
  3346. /*
  3347. * lock guy returned and lower count note
  3348. * that we are not bound so inp_tmp should
  3349. * NEVER be inp. And it is this inp (inp_tmp)
  3350. * that gets the reference bump, so we must
  3351. * lower it.
  3352. */
  3353. SCTP_INP_DECR_REF(inp_tmp);
  3354. /* unlock info */
  3355. if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
  3356. (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
  3357. /* Ok, must be one-2-one and allowing port re-use */
  3358. port_reuse_active = 1;
  3359. goto continue_anyway;
  3360. }
  3361. SCTP_INP_WLOCK(inp);
  3362. SCTP_INP_DECR_REF(inp);
  3363. error = EADDRINUSE;
  3364. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3365. goto out;
  3366. }
  3367. }
  3368. continue_anyway:
  3369. SCTP_INP_WLOCK(inp);
  3370. SCTP_INP_DECR_REF(inp);
  3371. if (bindall) {
  3372. /* verify that no lport is not used by a singleton */
  3373. if ((port_reuse_active == 0) &&
  3374. (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
  3375. /* Sorry someone already has this one bound */
  3376. if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
  3377. (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
  3378. port_reuse_active = 1;
  3379. } else {
  3380. error = EADDRINUSE;
  3381. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3382. goto out;
  3383. }
  3384. }
  3385. }
  3386. } else {
  3387. uint16_t first, last, candidate;
  3388. uint16_t count;
  3389. #if defined(__Userspace__)
  3390. first = MODULE_GLOBAL(ipport_firstauto);
  3391. last = MODULE_GLOBAL(ipport_lastauto);
  3392. #elif defined(_WIN32)
  3393. first = 1;
  3394. last = 0xffff;
  3395. #elif defined(__FreeBSD__) || defined(__APPLE__)
  3396. if (ip_inp->inp_flags & INP_HIGHPORT) {
  3397. first = MODULE_GLOBAL(ipport_hifirstauto);
  3398. last = MODULE_GLOBAL(ipport_hilastauto);
  3399. } else if (ip_inp->inp_flags & INP_LOWPORT) {
  3400. #if defined(__FreeBSD__)
  3401. if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
  3402. #else
  3403. if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
  3404. #endif
  3405. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3406. goto out;
  3407. }
  3408. first = MODULE_GLOBAL(ipport_lowfirstauto);
  3409. last = MODULE_GLOBAL(ipport_lowlastauto);
  3410. } else {
  3411. first = MODULE_GLOBAL(ipport_firstauto);
  3412. last = MODULE_GLOBAL(ipport_lastauto);
  3413. }
  3414. #endif
  3415. if (first > last) {
  3416. uint16_t temp;
  3417. temp = first;
  3418. first = last;
  3419. last = temp;
  3420. }
  3421. count = last - first + 1; /* number of candidates */
  3422. candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
  3423. for (;;) {
  3424. #ifdef SCTP_MVRF
  3425. for (i = 0; i < inp->num_vrfs; i++) {
  3426. if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
  3427. break;
  3428. }
  3429. }
  3430. if (i == inp->num_vrfs) {
  3431. lport = htons(candidate);
  3432. break;
  3433. }
  3434. #else
  3435. if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
  3436. lport = htons(candidate);
  3437. break;
  3438. }
  3439. #endif
  3440. if (--count == 0) {
  3441. error = EADDRINUSE;
  3442. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3443. goto out;
  3444. }
  3445. if (candidate == last)
  3446. candidate = first;
  3447. else
  3448. candidate = candidate + 1;
  3449. }
  3450. }
  3451. if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
  3452. SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
  3453. /*
  3454. * this really should not happen. The guy did a non-blocking
  3455. * bind and then did a close at the same time.
  3456. */
  3457. error = EINVAL;
  3458. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3459. goto out;
  3460. }
  3461. /* ok we look clear to give out this port, so lets setup the binding */
  3462. if (bindall) {
  3463. /* binding to all addresses, so just set in the proper flags */
  3464. inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
  3465. /* set the automatic addr changes from kernel flag */
  3466. if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
  3467. sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
  3468. sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
  3469. } else {
  3470. sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
  3471. sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
  3472. }
  3473. if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
  3474. sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
  3475. } else {
  3476. sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
  3477. }
  3478. /* set the automatic mobility_base from kernel
  3479. flag (by micchie)
  3480. */
  3481. if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
  3482. sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
  3483. sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
  3484. } else {
  3485. sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
  3486. sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
  3487. }
  3488. /* set the automatic mobility_fasthandoff from kernel
  3489. flag (by micchie)
  3490. */
  3491. if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
  3492. sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
  3493. sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
  3494. } else {
  3495. sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
  3496. sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
  3497. }
  3498. } else {
  3499. /*
  3500. * bind specific, make sure flags is off and add a new
  3501. * address structure to the sctp_addr_list inside the ep
  3502. * structure.
  3503. *
  3504. * We will need to allocate one and insert it at the head. The
  3505. * socketopt call can just insert new addresses in there as
  3506. * well. It will also have to do the embed scope kame hack
  3507. * too (before adding).
  3508. */
  3509. struct sctp_ifa *ifa;
  3510. union sctp_sockstore store;
  3511. memset(&store, 0, sizeof(store));
  3512. switch (addr->sa_family) {
  3513. #ifdef INET
  3514. case AF_INET:
  3515. memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
  3516. store.sin.sin_port = 0;
  3517. break;
  3518. #endif
  3519. #ifdef INET6
  3520. case AF_INET6:
  3521. memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
  3522. store.sin6.sin6_port = 0;
  3523. break;
  3524. #endif
  3525. #if defined(__Userspace__)
  3526. case AF_CONN:
  3527. memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
  3528. store.sconn.sconn_port = 0;
  3529. break;
  3530. #endif
  3531. default:
  3532. break;
  3533. }
  3534. /*
  3535. * first find the interface with the bound address need to
  3536. * zero out the port to find the address! yuck! can't do
  3537. * this earlier since need port for sctp_pcb_findep()
  3538. */
  3539. if (sctp_ifap != NULL) {
  3540. ifa = sctp_ifap;
  3541. } else {
  3542. /* Note for BSD we hit here always other
  3543. * O/S's will pass things in via the
  3544. * sctp_ifap argument.
  3545. */
  3546. ifa = sctp_find_ifa_by_addr(&store.sa,
  3547. vrf_id, SCTP_ADDR_NOT_LOCKED);
  3548. }
  3549. if (ifa == NULL) {
  3550. error = EADDRNOTAVAIL;
  3551. /* Can't find an interface with that address */
  3552. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3553. goto out;
  3554. }
  3555. #ifdef INET6
  3556. if (addr->sa_family == AF_INET6) {
  3557. /* GAK, more FIXME IFA lock? */
  3558. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  3559. /* Can't bind a non-existent addr. */
  3560. error = EINVAL;
  3561. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
  3562. goto out;
  3563. }
  3564. }
  3565. #endif
  3566. /* we're not bound all */
  3567. inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
  3568. /* allow bindx() to send ASCONF's for binding changes */
  3569. sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
  3570. /* clear automatic addr changes from kernel flag */
  3571. sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
  3572. /* add this address to the endpoint list */
  3573. error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
  3574. if (error != 0)
  3575. goto out;
  3576. inp->laddr_count++;
  3577. }
  3578. /* find the bucket */
  3579. if (port_reuse_active) {
  3580. /* Put it into tcp 1-2-1 hash */
  3581. head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
  3582. inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
  3583. } else {
  3584. head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
  3585. }
  3586. /* put it in the bucket */
  3587. LIST_INSERT_HEAD(head, inp, sctp_hash);
  3588. SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
  3589. (void *)head, ntohs(lport), port_reuse_active);
  3590. /* set in the port */
  3591. inp->sctp_lport = lport;
  3592. /* turn off just the unbound flag */
  3593. KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0,
  3594. ("%s: inp %p is already bound", __func__, inp));
  3595. inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
  3596. out:
  3597. return (error);
  3598. }
  3599. int
  3600. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3601. sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
  3602. struct sctp_ifa *sctp_ifap, struct thread *td)
  3603. #elif defined(_WIN32) && !defined(__Userspace__)
  3604. sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
  3605. struct sctp_ifa *sctp_ifap, PKTHREAD p)
  3606. #else
  3607. sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
  3608. struct sctp_ifa *sctp_ifap, struct proc *p)
  3609. #endif
  3610. {
  3611. struct sctp_inpcb *inp;
  3612. int error;
  3613. inp = so->so_pcb;
  3614. SCTP_INP_INFO_WLOCK();
  3615. SCTP_INP_WLOCK(inp);
  3616. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3617. error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td);
  3618. #else
  3619. error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, p);
  3620. #endif
  3621. SCTP_INP_WUNLOCK(inp);
  3622. SCTP_INP_INFO_WUNLOCK();
  3623. return (error);
  3624. }
  3625. static void
  3626. sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
  3627. {
  3628. struct sctp_iterator *it, *nit;
  3629. /*
  3630. * We enter with the only the ITERATOR_LOCK in place and a write
  3631. * lock on the inp_info stuff.
  3632. */
  3633. it = sctp_it_ctl.cur_it;
  3634. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3635. if (it && (it->vn != curvnet)) {
  3636. /* Its not looking at our VNET */
  3637. return;
  3638. }
  3639. #endif
  3640. if (it && (it->inp == inp)) {
  3641. /*
  3642. * This is tricky and we hold the iterator lock,
  3643. * but when it returns and gets the lock (when we
  3644. * release it) the iterator will try to operate on
  3645. * inp. We need to stop that from happening. But
  3646. * of course the iterator has a reference on the
  3647. * stcb and inp. We can mark it and it will stop.
  3648. *
  3649. * If its a single iterator situation, we
  3650. * set the end iterator flag. Otherwise
  3651. * we set the iterator to go to the next inp.
  3652. *
  3653. */
  3654. if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
  3655. sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
  3656. } else {
  3657. sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
  3658. }
  3659. }
  3660. /* Now go through and remove any single reference to
  3661. * our inp that may be still pending on the list
  3662. */
  3663. SCTP_IPI_ITERATOR_WQ_LOCK();
  3664. TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
  3665. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3666. if (it->vn != curvnet) {
  3667. continue;
  3668. }
  3669. #endif
  3670. if (it->inp == inp) {
  3671. /* This one points to me is it inp specific? */
  3672. if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
  3673. /* Remove and free this one */
  3674. TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
  3675. it, sctp_nxt_itr);
  3676. if (it->function_atend != NULL) {
  3677. (*it->function_atend) (it->pointer, it->val);
  3678. }
  3679. SCTP_FREE(it, SCTP_M_ITER);
  3680. } else {
  3681. it->inp = LIST_NEXT(it->inp, sctp_list);
  3682. if (it->inp) {
  3683. SCTP_INP_INCR_REF(it->inp);
  3684. }
  3685. }
  3686. /* When its put in the refcnt is incremented so decr it */
  3687. SCTP_INP_DECR_REF(inp);
  3688. }
  3689. }
  3690. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  3691. }
  3692. /* release sctp_inpcb unbind the port */
  3693. void
  3694. sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
  3695. {
  3696. /*
  3697. * Here we free a endpoint. We must find it (if it is in the Hash
  3698. * table) and remove it from there. Then we must also find it in the
  3699. * overall list and remove it from there. After all removals are
  3700. * complete then any timer has to be stopped. Then start the actual
  3701. * freeing. a) Any local lists. b) Any associations. c) The hash of
  3702. * all associations. d) finally the ep itself.
  3703. */
  3704. struct sctp_tcb *stcb, *nstcb;
  3705. struct sctp_laddr *laddr, *nladdr;
  3706. struct inpcb *ip_pcb;
  3707. struct socket *so;
  3708. int being_refed = 0;
  3709. struct sctp_queued_to_read *sq, *nsq;
  3710. #if !defined(__Userspace__)
  3711. #if !defined(__FreeBSD__)
  3712. sctp_rtentry_t *rt;
  3713. #endif
  3714. #endif
  3715. int cnt;
  3716. sctp_sharedkey_t *shared_key, *nshared_key;
  3717. #if defined(__APPLE__) && !defined(__Userspace__)
  3718. sctp_lock_assert(SCTP_INP_SO(inp));
  3719. #endif
  3720. #ifdef SCTP_LOG_CLOSING
  3721. sctp_log_closing(inp, NULL, 0);
  3722. #endif
  3723. SCTP_ITERATOR_LOCK();
  3724. /* mark any iterators on the list or being processed */
  3725. sctp_iterator_inp_being_freed(inp);
  3726. SCTP_ITERATOR_UNLOCK();
  3727. SCTP_ASOC_CREATE_LOCK(inp);
  3728. SCTP_INP_INFO_WLOCK();
  3729. SCTP_INP_WLOCK(inp);
  3730. so = inp->sctp_socket;
  3731. KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
  3732. ("%s: inp %p still has socket", __func__, inp));
  3733. KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
  3734. ("%s: double free of inp %p", __func__, inp));
  3735. if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
  3736. inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
  3737. /* socket is gone, so no more wakeups allowed */
  3738. inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
  3739. inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
  3740. inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
  3741. }
  3742. /* First time through we have the socket lock, after that no more. */
  3743. sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
  3744. SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
  3745. if (inp->control) {
  3746. sctp_m_freem(inp->control);
  3747. inp->control = NULL;
  3748. }
  3749. if (inp->pkt) {
  3750. sctp_m_freem(inp->pkt);
  3751. inp->pkt = NULL;
  3752. }
  3753. ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
  3754. * here but I will be nice :> (i.e.
  3755. * ip_pcb = ep;) */
  3756. if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
  3757. int cnt_in_sd;
  3758. cnt_in_sd = 0;
  3759. LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
  3760. SCTP_TCB_LOCK(stcb);
  3761. /* Disconnect the socket please. */
  3762. stcb->sctp_socket = NULL;
  3763. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
  3764. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  3765. /* Skip guys being freed */
  3766. cnt_in_sd++;
  3767. if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
  3768. /*
  3769. * Special case - we did not start a kill
  3770. * timer on the asoc due to it was not
  3771. * closed. So go ahead and start it now.
  3772. */
  3773. SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
  3774. sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
  3775. }
  3776. SCTP_TCB_UNLOCK(stcb);
  3777. continue;
  3778. }
  3779. if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  3780. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) &&
  3781. (stcb->asoc.total_output_queue_size == 0)) {
  3782. /* If we have data in queue, we don't want to just
  3783. * free since the app may have done, send()/close
  3784. * or connect/send/close. And it wants the data
  3785. * to get across first.
  3786. */
  3787. /* Just abandon things in the front states */
  3788. if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE,
  3789. SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
  3790. cnt_in_sd++;
  3791. }
  3792. continue;
  3793. }
  3794. if ((stcb->asoc.size_on_reasm_queue > 0) ||
  3795. (stcb->asoc.control_pdapi) ||
  3796. (stcb->asoc.size_on_all_streams > 0) ||
  3797. ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0))) {
  3798. /* Left with Data unread */
  3799. struct mbuf *op_err;
  3800. op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
  3801. stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
  3802. sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
  3803. SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  3804. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  3805. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  3806. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  3807. }
  3808. if (sctp_free_assoc(inp, stcb,
  3809. SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
  3810. cnt_in_sd++;
  3811. }
  3812. continue;
  3813. } else if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
  3814. TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
  3815. (stcb->asoc.stream_queue_cnt == 0)) {
  3816. if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) {
  3817. goto abort_anyway;
  3818. }
  3819. if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
  3820. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  3821. struct sctp_nets *netp;
  3822. /*
  3823. * there is nothing queued to send,
  3824. * so I send shutdown
  3825. */
  3826. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  3827. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  3828. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  3829. }
  3830. SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
  3831. sctp_stop_timers_for_shutdown(stcb);
  3832. if (stcb->asoc.alternate) {
  3833. netp = stcb->asoc.alternate;
  3834. } else {
  3835. netp = stcb->asoc.primary_destination;
  3836. }
  3837. sctp_send_shutdown(stcb, netp);
  3838. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
  3839. netp);
  3840. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
  3841. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
  3842. }
  3843. } else {
  3844. /* mark into shutdown pending */
  3845. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
  3846. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
  3847. if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) {
  3848. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
  3849. }
  3850. if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
  3851. TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
  3852. (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
  3853. struct mbuf *op_err;
  3854. abort_anyway:
  3855. op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
  3856. stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
  3857. sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
  3858. SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  3859. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  3860. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  3861. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  3862. }
  3863. if (sctp_free_assoc(inp, stcb,
  3864. SCTP_PCBFREE_NOFORCE,
  3865. SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
  3866. cnt_in_sd++;
  3867. }
  3868. continue;
  3869. } else {
  3870. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
  3871. }
  3872. }
  3873. cnt_in_sd++;
  3874. SCTP_TCB_UNLOCK(stcb);
  3875. }
  3876. /* now is there some left in our SHUTDOWN state? */
  3877. if (cnt_in_sd) {
  3878. #ifdef SCTP_LOG_CLOSING
  3879. sctp_log_closing(inp, NULL, 2);
  3880. #endif
  3881. inp->sctp_socket = NULL;
  3882. SCTP_INP_WUNLOCK(inp);
  3883. SCTP_ASOC_CREATE_UNLOCK(inp);
  3884. SCTP_INP_INFO_WUNLOCK();
  3885. return;
  3886. }
  3887. }
  3888. inp->sctp_socket = NULL;
  3889. if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
  3890. /*
  3891. * ok, this guy has been bound. It's port is
  3892. * somewhere in the SCTP_BASE_INFO(hash table). Remove
  3893. * it!
  3894. */
  3895. LIST_REMOVE(inp, sctp_hash);
  3896. inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
  3897. }
  3898. /* If there is a timer running to kill us,
  3899. * forget it, since it may have a contest
  3900. * on the INP lock.. which would cause us
  3901. * to die ...
  3902. */
  3903. cnt = 0;
  3904. LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
  3905. SCTP_TCB_LOCK(stcb);
  3906. if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
  3907. /* Disconnect the socket please */
  3908. stcb->sctp_socket = NULL;
  3909. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
  3910. }
  3911. if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  3912. if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
  3913. SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
  3914. sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
  3915. }
  3916. cnt++;
  3917. SCTP_TCB_UNLOCK(stcb);
  3918. continue;
  3919. }
  3920. /* Free associations that are NOT killing us */
  3921. if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
  3922. ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
  3923. struct mbuf *op_err;
  3924. op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
  3925. stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
  3926. sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
  3927. SCTP_STAT_INCR_COUNTER32(sctps_aborted);
  3928. } else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
  3929. cnt++;
  3930. SCTP_TCB_UNLOCK(stcb);
  3931. continue;
  3932. }
  3933. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  3934. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
  3935. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  3936. }
  3937. if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
  3938. SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
  3939. cnt++;
  3940. }
  3941. }
  3942. if (cnt) {
  3943. /* Ok we have someone out there that will kill us */
  3944. #ifdef SCTP_LOG_CLOSING
  3945. sctp_log_closing(inp, NULL, 3);
  3946. #endif
  3947. SCTP_INP_WUNLOCK(inp);
  3948. SCTP_ASOC_CREATE_UNLOCK(inp);
  3949. SCTP_INP_INFO_WUNLOCK();
  3950. return;
  3951. }
  3952. if (SCTP_INP_LOCK_CONTENDED(inp))
  3953. being_refed++;
  3954. if (SCTP_INP_READ_CONTENDED(inp))
  3955. being_refed++;
  3956. if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
  3957. being_refed++;
  3958. /* NOTE: 0 refcount also means no timers are referencing us. */
  3959. if ((inp->refcount) ||
  3960. (being_refed) ||
  3961. (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
  3962. #ifdef SCTP_LOG_CLOSING
  3963. sctp_log_closing(inp, NULL, 4);
  3964. #endif
  3965. sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
  3966. SCTP_INP_WUNLOCK(inp);
  3967. SCTP_ASOC_CREATE_UNLOCK(inp);
  3968. SCTP_INP_INFO_WUNLOCK();
  3969. return;
  3970. }
  3971. inp->sctp_ep.signature_change.type = 0;
  3972. inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
  3973. /* Remove it from the list .. last thing we need a
  3974. * lock for.
  3975. */
  3976. LIST_REMOVE(inp, sctp_list);
  3977. SCTP_INP_WUNLOCK(inp);
  3978. SCTP_ASOC_CREATE_UNLOCK(inp);
  3979. SCTP_INP_INFO_WUNLOCK();
  3980. #ifdef SCTP_LOG_CLOSING
  3981. sctp_log_closing(inp, NULL, 5);
  3982. #endif
  3983. #if !(defined(_WIN32) || defined(__Userspace__))
  3984. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  3985. rt = ip_pcb->inp_route.ro_rt;
  3986. #endif
  3987. #endif
  3988. if ((inp->sctp_asocidhash) != NULL) {
  3989. SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
  3990. inp->sctp_asocidhash = NULL;
  3991. }
  3992. /*sa_ignore FREED_MEMORY*/
  3993. TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
  3994. /* Its only abandoned if it had data left */
  3995. if (sq->length)
  3996. SCTP_STAT_INCR(sctps_left_abandon);
  3997. TAILQ_REMOVE(&inp->read_queue, sq, next);
  3998. sctp_free_remote_addr(sq->whoFrom);
  3999. if (so)
  4000. so->so_rcv.sb_cc -= sq->length;
  4001. if (sq->data) {
  4002. sctp_m_freem(sq->data);
  4003. sq->data = NULL;
  4004. }
  4005. /*
  4006. * no need to free the net count, since at this point all
  4007. * assoc's are gone.
  4008. */
  4009. sctp_free_a_readq(NULL, sq);
  4010. }
  4011. /* Now the sctp_pcb things */
  4012. /*
  4013. * free each asoc if it is not already closed/free. we can't use the
  4014. * macro here since le_next will get freed as part of the
  4015. * sctp_free_assoc() call.
  4016. */
  4017. if (ip_pcb->inp_options) {
  4018. (void)sctp_m_free(ip_pcb->inp_options);
  4019. ip_pcb->inp_options = 0;
  4020. }
  4021. #if !(defined(_WIN32) || defined(__Userspace__))
  4022. #if !defined(__FreeBSD__)
  4023. if (rt) {
  4024. RTFREE(rt);
  4025. ip_pcb->inp_route.ro_rt = 0;
  4026. }
  4027. #endif
  4028. #endif
  4029. #ifdef INET6
  4030. #if !(defined(_WIN32) || defined(__Userspace__))
  4031. #if (defined(__FreeBSD__) || defined(__APPLE__) && !defined(__Userspace__))
  4032. if (ip_pcb->inp_vflag & INP_IPV6) {
  4033. #else
  4034. if (inp->inp_vflag & INP_IPV6) {
  4035. #endif
  4036. ip6_freepcbopts(ip_pcb->in6p_outputopts);
  4037. }
  4038. #endif
  4039. #endif /* INET6 */
  4040. ip_pcb->inp_vflag = 0;
  4041. /* free up authentication fields */
  4042. if (inp->sctp_ep.local_auth_chunks != NULL)
  4043. sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
  4044. if (inp->sctp_ep.local_hmacs != NULL)
  4045. sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
  4046. LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
  4047. LIST_REMOVE(shared_key, next);
  4048. sctp_free_sharedkey(shared_key);
  4049. /*sa_ignore FREED_MEMORY*/
  4050. }
  4051. #if defined(__APPLE__) && !defined(__Userspace__)
  4052. inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
  4053. if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
  4054. #ifdef INVARIANTS
  4055. panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING", (void *)inp);
  4056. #else
  4057. SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
  4058. #endif
  4059. }
  4060. inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
  4061. #endif
  4062. /*
  4063. * if we have an address list the following will free the list of
  4064. * ifaddr's that are set into this ep. Again macro limitations here,
  4065. * since the LIST_FOREACH could be a bad idea.
  4066. */
  4067. LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
  4068. sctp_remove_laddr(laddr);
  4069. }
  4070. #ifdef SCTP_TRACK_FREED_ASOCS
  4071. /* TEMP CODE */
  4072. LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) {
  4073. LIST_REMOVE(stcb, sctp_tcblist);
  4074. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
  4075. SCTP_DECR_ASOC_COUNT();
  4076. }
  4077. /* *** END TEMP CODE ****/
  4078. #endif
  4079. #ifdef SCTP_MVRF
  4080. SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
  4081. #endif
  4082. /* Now lets see about freeing the EP hash table. */
  4083. if (inp->sctp_tcbhash != NULL) {
  4084. SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
  4085. inp->sctp_tcbhash = NULL;
  4086. }
  4087. /* Now we must put the ep memory back into the zone pool */
  4088. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4089. crfree(inp->ip_inp.inp.inp_cred);
  4090. INP_LOCK_DESTROY(&inp->ip_inp.inp);
  4091. #endif
  4092. SCTP_INP_LOCK_DESTROY(inp);
  4093. SCTP_INP_READ_DESTROY(inp);
  4094. SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
  4095. #if !(defined(__APPLE__) && !defined(__Userspace__))
  4096. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
  4097. SCTP_DECR_EP_COUNT();
  4098. #else
  4099. /* For Tiger, we will do this later... */
  4100. #endif
  4101. }
  4102. struct sctp_nets *
  4103. sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
  4104. {
  4105. struct sctp_nets *net;
  4106. /* locate the address */
  4107. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  4108. if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
  4109. return (net);
  4110. }
  4111. return (NULL);
  4112. }
  4113. int
  4114. sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
  4115. {
  4116. struct sctp_ifa *sctp_ifa;
  4117. sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
  4118. if (sctp_ifa) {
  4119. return (1);
  4120. } else {
  4121. return (0);
  4122. }
  4123. }
  4124. /*
  4125. * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
  4126. * when a ASCONF arrives that adds it. It will also initialize all the cwnd
  4127. * stats of stuff.
  4128. */
  4129. int
  4130. sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
  4131. struct sctp_nets **netp, uint16_t port, int set_scope, int from)
  4132. {
  4133. /*
  4134. * The following is redundant to the same lines in the
  4135. * sctp_aloc_assoc() but is needed since others call the add
  4136. * address function
  4137. */
  4138. struct sctp_nets *net, *netfirst;
  4139. int addr_inscope;
  4140. SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
  4141. from);
  4142. SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
  4143. netfirst = sctp_findnet(stcb, newaddr);
  4144. if (netfirst) {
  4145. /*
  4146. * Lie and return ok, we don't want to make the association
  4147. * go away for this behavior. It will happen in the TCP
  4148. * model in a connected socket. It does not reach the hash
  4149. * table until after the association is built so it can't be
  4150. * found. Mark as reachable, since the initial creation will
  4151. * have been cleared and the NOT_IN_ASSOC flag will have
  4152. * been added... and we don't want to end up removing it
  4153. * back out.
  4154. */
  4155. if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
  4156. netfirst->dest_state = (SCTP_ADDR_REACHABLE |
  4157. SCTP_ADDR_UNCONFIRMED);
  4158. } else {
  4159. netfirst->dest_state = SCTP_ADDR_REACHABLE;
  4160. }
  4161. return (0);
  4162. }
  4163. addr_inscope = 1;
  4164. switch (newaddr->sa_family) {
  4165. #ifdef INET
  4166. case AF_INET:
  4167. {
  4168. struct sockaddr_in *sin;
  4169. sin = (struct sockaddr_in *)newaddr;
  4170. if (sin->sin_addr.s_addr == 0) {
  4171. /* Invalid address */
  4172. return (-1);
  4173. }
  4174. /* zero out the zero area */
  4175. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  4176. /* assure len is set */
  4177. #ifdef HAVE_SIN_LEN
  4178. sin->sin_len = sizeof(struct sockaddr_in);
  4179. #endif
  4180. if (set_scope) {
  4181. if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
  4182. stcb->asoc.scope.ipv4_local_scope = 1;
  4183. }
  4184. } else {
  4185. /* Validate the address is in scope */
  4186. if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
  4187. (stcb->asoc.scope.ipv4_local_scope == 0)) {
  4188. addr_inscope = 0;
  4189. }
  4190. }
  4191. break;
  4192. }
  4193. #endif
  4194. #ifdef INET6
  4195. case AF_INET6:
  4196. {
  4197. struct sockaddr_in6 *sin6;
  4198. sin6 = (struct sockaddr_in6 *)newaddr;
  4199. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  4200. /* Invalid address */
  4201. return (-1);
  4202. }
  4203. /* assure len is set */
  4204. #ifdef HAVE_SIN6_LEN
  4205. sin6->sin6_len = sizeof(struct sockaddr_in6);
  4206. #endif
  4207. if (set_scope) {
  4208. if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
  4209. stcb->asoc.scope.loopback_scope = 1;
  4210. stcb->asoc.scope.local_scope = 0;
  4211. stcb->asoc.scope.ipv4_local_scope = 1;
  4212. stcb->asoc.scope.site_scope = 1;
  4213. } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
  4214. /*
  4215. * If the new destination is a LINK_LOCAL we
  4216. * must have common site scope. Don't set
  4217. * the local scope since we may not share
  4218. * all links, only loopback can do this.
  4219. * Links on the local network would also be
  4220. * on our private network for v4 too.
  4221. */
  4222. stcb->asoc.scope.ipv4_local_scope = 1;
  4223. stcb->asoc.scope.site_scope = 1;
  4224. } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
  4225. /*
  4226. * If the new destination is SITE_LOCAL then
  4227. * we must have site scope in common.
  4228. */
  4229. stcb->asoc.scope.site_scope = 1;
  4230. }
  4231. } else {
  4232. /* Validate the address is in scope */
  4233. if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
  4234. (stcb->asoc.scope.loopback_scope == 0)) {
  4235. addr_inscope = 0;
  4236. } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
  4237. (stcb->asoc.scope.local_scope == 0)) {
  4238. addr_inscope = 0;
  4239. } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
  4240. (stcb->asoc.scope.site_scope == 0)) {
  4241. addr_inscope = 0;
  4242. }
  4243. }
  4244. break;
  4245. }
  4246. #endif
  4247. #if defined(__Userspace__)
  4248. case AF_CONN:
  4249. {
  4250. struct sockaddr_conn *sconn;
  4251. sconn = (struct sockaddr_conn *)newaddr;
  4252. if (sconn->sconn_addr == NULL) {
  4253. /* Invalid address */
  4254. return (-1);
  4255. }
  4256. #ifdef HAVE_SCONN_LEN
  4257. sconn->sconn_len = sizeof(struct sockaddr_conn);
  4258. #endif
  4259. break;
  4260. }
  4261. #endif
  4262. default:
  4263. /* not supported family type */
  4264. return (-1);
  4265. }
  4266. net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
  4267. if (net == NULL) {
  4268. return (-1);
  4269. }
  4270. SCTP_INCR_RADDR_COUNT();
  4271. memset(net, 0, sizeof(struct sctp_nets));
  4272. (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
  4273. #ifdef HAVE_SA_LEN
  4274. memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
  4275. #endif
  4276. switch (newaddr->sa_family) {
  4277. #ifdef INET
  4278. case AF_INET:
  4279. #ifndef HAVE_SA_LEN
  4280. memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
  4281. #endif
  4282. ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
  4283. break;
  4284. #endif
  4285. #ifdef INET6
  4286. case AF_INET6:
  4287. #ifndef HAVE_SA_LEN
  4288. memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
  4289. #endif
  4290. ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
  4291. break;
  4292. #endif
  4293. #if defined(__Userspace__)
  4294. case AF_CONN:
  4295. #ifndef HAVE_SA_LEN
  4296. memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
  4297. #endif
  4298. ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
  4299. break;
  4300. #endif
  4301. default:
  4302. break;
  4303. }
  4304. net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
  4305. if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
  4306. stcb->asoc.scope.loopback_scope = 1;
  4307. stcb->asoc.scope.ipv4_local_scope = 1;
  4308. stcb->asoc.scope.local_scope = 0;
  4309. stcb->asoc.scope.site_scope = 1;
  4310. addr_inscope = 1;
  4311. }
  4312. net->failure_threshold = stcb->asoc.def_net_failure;
  4313. net->pf_threshold = stcb->asoc.def_net_pf_threshold;
  4314. if (addr_inscope == 0) {
  4315. net->dest_state = (SCTP_ADDR_REACHABLE |
  4316. SCTP_ADDR_OUT_OF_SCOPE);
  4317. } else {
  4318. if (from == SCTP_ADDR_IS_CONFIRMED)
  4319. /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
  4320. net->dest_state = SCTP_ADDR_REACHABLE;
  4321. else
  4322. net->dest_state = SCTP_ADDR_REACHABLE |
  4323. SCTP_ADDR_UNCONFIRMED;
  4324. }
  4325. /* We set this to 0, the timer code knows that
  4326. * this means its an initial value
  4327. */
  4328. net->rto_needed = 1;
  4329. net->RTO = 0;
  4330. net->RTO_measured = 0;
  4331. stcb->asoc.numnets++;
  4332. net->ref_count = 1;
  4333. net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
  4334. net->port = port;
  4335. net->dscp = stcb->asoc.default_dscp;
  4336. #ifdef INET6
  4337. net->flowlabel = stcb->asoc.default_flowlabel;
  4338. #endif
  4339. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
  4340. net->dest_state |= SCTP_ADDR_NOHB;
  4341. } else {
  4342. net->dest_state &= ~SCTP_ADDR_NOHB;
  4343. }
  4344. if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
  4345. net->dest_state |= SCTP_ADDR_NO_PMTUD;
  4346. } else {
  4347. net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
  4348. }
  4349. net->heart_beat_delay = stcb->asoc.heart_beat_delay;
  4350. /* Init the timer structure */
  4351. SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
  4352. SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
  4353. SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
  4354. /* Now generate a route for this guy */
  4355. #ifdef INET6
  4356. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4357. /* KAME hack: embed scopeid */
  4358. if (newaddr->sa_family == AF_INET6) {
  4359. struct sockaddr_in6 *sin6;
  4360. sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  4361. #if defined(__APPLE__) && !defined(__Userspace__)
  4362. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  4363. (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
  4364. #else
  4365. (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
  4366. #endif
  4367. #elif defined(SCTP_KAME)
  4368. (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
  4369. #else
  4370. (void)in6_embedscope(&sin6->sin6_addr, sin6);
  4371. #endif
  4372. #ifndef SCOPEDROUTING
  4373. sin6->sin6_scope_id = 0;
  4374. #endif
  4375. }
  4376. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4377. #endif
  4378. SCTP_RTALLOC((sctp_route_t *)&net->ro,
  4379. stcb->asoc.vrf_id,
  4380. stcb->sctp_ep->fibnum);
  4381. net->src_addr_selected = 0;
  4382. #if !defined(__Userspace__)
  4383. if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
  4384. /* Get source address */
  4385. net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
  4386. stcb,
  4387. (sctp_route_t *)&net->ro,
  4388. net,
  4389. 0,
  4390. stcb->asoc.vrf_id);
  4391. if (stcb->asoc.default_mtu > 0) {
  4392. net->mtu = stcb->asoc.default_mtu;
  4393. switch (net->ro._l_addr.sa.sa_family) {
  4394. #ifdef INET
  4395. case AF_INET:
  4396. net->mtu += SCTP_MIN_V4_OVERHEAD;
  4397. break;
  4398. #endif
  4399. #ifdef INET6
  4400. case AF_INET6:
  4401. net->mtu += SCTP_MIN_OVERHEAD;
  4402. break;
  4403. #endif
  4404. #if defined(__Userspace__)
  4405. case AF_CONN:
  4406. net->mtu += sizeof(struct sctphdr);
  4407. break;
  4408. #endif
  4409. default:
  4410. break;
  4411. }
  4412. #if defined(INET) || defined(INET6)
  4413. if (net->port) {
  4414. net->mtu += (uint32_t)sizeof(struct udphdr);
  4415. }
  4416. #endif
  4417. } else if (net->ro._s_addr != NULL) {
  4418. uint32_t imtu, rmtu, hcmtu;
  4419. net->src_addr_selected = 1;
  4420. /* Now get the interface MTU */
  4421. if (net->ro._s_addr->ifn_p != NULL) {
  4422. /*
  4423. * XXX: Should we here just use
  4424. * net->ro._s_addr->ifn_p->ifn_mtu
  4425. */
  4426. imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p,
  4427. net->ro._s_addr->ifn_p->ifn_index);
  4428. } else {
  4429. imtu = 0;
  4430. }
  4431. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4432. rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh);
  4433. hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
  4434. #else
  4435. rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
  4436. hcmtu = 0;
  4437. #endif
  4438. net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
  4439. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4440. #else
  4441. if (rmtu == 0) {
  4442. /* Start things off to match mtu of interface please. */
  4443. SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
  4444. net->ro.ro_rt, net->mtu);
  4445. }
  4446. #endif
  4447. }
  4448. }
  4449. #endif
  4450. if (net->mtu == 0) {
  4451. if (stcb->asoc.default_mtu > 0) {
  4452. net->mtu = stcb->asoc.default_mtu;
  4453. switch (net->ro._l_addr.sa.sa_family) {
  4454. #ifdef INET
  4455. case AF_INET:
  4456. net->mtu += SCTP_MIN_V4_OVERHEAD;
  4457. break;
  4458. #endif
  4459. #ifdef INET6
  4460. case AF_INET6:
  4461. net->mtu += SCTP_MIN_OVERHEAD;
  4462. break;
  4463. #endif
  4464. #if defined(__Userspace__)
  4465. case AF_CONN:
  4466. net->mtu += sizeof(struct sctphdr);
  4467. break;
  4468. #endif
  4469. default:
  4470. break;
  4471. }
  4472. #if defined(INET) || defined(INET6)
  4473. if (net->port) {
  4474. net->mtu += (uint32_t)sizeof(struct udphdr);
  4475. }
  4476. #endif
  4477. } else {
  4478. switch (newaddr->sa_family) {
  4479. #ifdef INET
  4480. case AF_INET:
  4481. net->mtu = SCTP_DEFAULT_MTU;
  4482. break;
  4483. #endif
  4484. #ifdef INET6
  4485. case AF_INET6:
  4486. net->mtu = 1280;
  4487. break;
  4488. #endif
  4489. #if defined(__Userspace__)
  4490. case AF_CONN:
  4491. net->mtu = 1280;
  4492. break;
  4493. #endif
  4494. default:
  4495. break;
  4496. }
  4497. }
  4498. }
  4499. #if defined(INET) || defined(INET6)
  4500. if (net->port) {
  4501. net->mtu -= (uint32_t)sizeof(struct udphdr);
  4502. }
  4503. #endif
  4504. if (from == SCTP_ALLOC_ASOC) {
  4505. stcb->asoc.smallest_mtu = net->mtu;
  4506. }
  4507. if (stcb->asoc.smallest_mtu > net->mtu) {
  4508. sctp_pathmtu_adjustment(stcb, net->mtu, true);
  4509. }
  4510. #ifdef INET6
  4511. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4512. if (newaddr->sa_family == AF_INET6) {
  4513. struct sockaddr_in6 *sin6;
  4514. sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  4515. #ifdef SCTP_KAME
  4516. (void)sa6_recoverscope(sin6);
  4517. #else
  4518. (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
  4519. #endif /* SCTP_KAME */
  4520. }
  4521. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4522. #endif
  4523. /* JRS - Use the congestion control given in the CC module */
  4524. if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
  4525. (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
  4526. /*
  4527. * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
  4528. * of assoc (2005/06/27, iyengar@cis.udel.edu)
  4529. */
  4530. net->find_pseudo_cumack = 1;
  4531. net->find_rtx_pseudo_cumack = 1;
  4532. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4533. /* Choose an initial flowid. */
  4534. net->flowid = stcb->asoc.my_vtag ^
  4535. ntohs(stcb->rport) ^
  4536. ntohs(stcb->sctp_ep->sctp_lport);
  4537. net->flowtype = M_HASHTYPE_OPAQUE_HASH;
  4538. #endif
  4539. if (netp) {
  4540. *netp = net;
  4541. }
  4542. netfirst = TAILQ_FIRST(&stcb->asoc.nets);
  4543. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4544. if (net->ro.ro_nh == NULL) {
  4545. #else
  4546. if (net->ro.ro_rt == NULL) {
  4547. #endif
  4548. /* Since we have no route put it at the back */
  4549. TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
  4550. } else if (netfirst == NULL) {
  4551. /* We are the first one in the pool. */
  4552. TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
  4553. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4554. } else if (netfirst->ro.ro_nh == NULL) {
  4555. #else
  4556. } else if (netfirst->ro.ro_rt == NULL) {
  4557. #endif
  4558. /*
  4559. * First one has NO route. Place this one ahead of the first
  4560. * one.
  4561. */
  4562. TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
  4563. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4564. } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
  4565. #else
  4566. } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
  4567. #endif
  4568. /*
  4569. * This one has a different interface than the one at the
  4570. * top of the list. Place it ahead.
  4571. */
  4572. TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
  4573. } else {
  4574. /*
  4575. * Ok we have the same interface as the first one. Move
  4576. * forward until we find either a) one with a NULL route...
  4577. * insert ahead of that b) one with a different ifp.. insert
  4578. * after that. c) end of the list.. insert at the tail.
  4579. */
  4580. struct sctp_nets *netlook;
  4581. do {
  4582. netlook = TAILQ_NEXT(netfirst, sctp_next);
  4583. if (netlook == NULL) {
  4584. /* End of the list */
  4585. TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
  4586. break;
  4587. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4588. } else if (netlook->ro.ro_nh == NULL) {
  4589. #else
  4590. } else if (netlook->ro.ro_rt == NULL) {
  4591. #endif
  4592. /* next one has NO route */
  4593. TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
  4594. break;
  4595. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4596. } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
  4597. #else
  4598. } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
  4599. #endif
  4600. TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
  4601. net, sctp_next);
  4602. break;
  4603. }
  4604. /* Shift forward */
  4605. netfirst = netlook;
  4606. } while (netlook != NULL);
  4607. }
  4608. /* got to have a primary set */
  4609. if (stcb->asoc.primary_destination == 0) {
  4610. stcb->asoc.primary_destination = net;
  4611. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4612. } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
  4613. (net->ro.ro_nh) &&
  4614. #else
  4615. } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
  4616. (net->ro.ro_rt) &&
  4617. #endif
  4618. ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
  4619. /* No route to current primary adopt new primary */
  4620. stcb->asoc.primary_destination = net;
  4621. }
  4622. /* Validate primary is first */
  4623. net = TAILQ_FIRST(&stcb->asoc.nets);
  4624. if ((net != stcb->asoc.primary_destination) &&
  4625. (stcb->asoc.primary_destination)) {
  4626. /* first one on the list is NOT the primary
  4627. * sctp_cmpaddr() is much more efficient if
  4628. * the primary is the first on the list, make it
  4629. * so.
  4630. */
  4631. TAILQ_REMOVE(&stcb->asoc.nets,
  4632. stcb->asoc.primary_destination, sctp_next);
  4633. TAILQ_INSERT_HEAD(&stcb->asoc.nets,
  4634. stcb->asoc.primary_destination, sctp_next);
  4635. }
  4636. return (0);
  4637. }
  4638. static uint32_t
  4639. sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
  4640. {
  4641. uint32_t id;
  4642. struct sctpasochead *head;
  4643. struct sctp_tcb *lstcb;
  4644. try_again:
  4645. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  4646. /* TSNH */
  4647. return (0);
  4648. }
  4649. /*
  4650. * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
  4651. * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
  4652. */
  4653. if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
  4654. inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
  4655. }
  4656. id = inp->sctp_associd_counter;
  4657. inp->sctp_associd_counter++;
  4658. lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
  4659. if (lstcb) {
  4660. goto try_again;
  4661. }
  4662. head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
  4663. LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
  4664. stcb->asoc.in_asocid_hash = 1;
  4665. return (id);
  4666. }
  4667. /*
  4668. * allocate an association and add it to the endpoint. The caller must be
  4669. * careful to add all additional addresses once they are know right away or
  4670. * else the assoc will be may experience a blackout scenario.
  4671. */
  4672. static struct sctp_tcb *
  4673. sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
  4674. int *error, uint32_t override_tag, uint32_t initial_tsn,
  4675. uint32_t vrf_id, uint16_t o_streams, uint16_t port,
  4676. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4677. struct thread *p,
  4678. #elif defined(_WIN32) && !defined(__Userspace__)
  4679. PKTHREAD p,
  4680. #else
  4681. #if defined(__Userspace__)
  4682. /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
  4683. #endif
  4684. struct proc *p,
  4685. #endif
  4686. int initialize_auth_params)
  4687. {
  4688. /* note the p argument is only valid in unbound sockets */
  4689. struct sctp_tcb *stcb;
  4690. struct sctp_association *asoc;
  4691. struct sctpasochead *head;
  4692. uint16_t rport;
  4693. int err;
  4694. SCTP_INP_INFO_WLOCK_ASSERT();
  4695. SCTP_INP_WLOCK_ASSERT(inp);
  4696. /*
  4697. * Assumption made here: Caller has done a
  4698. * sctp_findassociation_ep_addr(ep, addr's); to make sure the
  4699. * address does not exist already.
  4700. */
  4701. if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
  4702. /* Hit max assoc, sorry no more */
  4703. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
  4704. *error = ENOBUFS;
  4705. return (NULL);
  4706. }
  4707. if (firstaddr == NULL) {
  4708. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4709. *error = EINVAL;
  4710. return (NULL);
  4711. }
  4712. if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
  4713. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4714. *error = EINVAL;
  4715. return (NULL);
  4716. }
  4717. if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
  4718. ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
  4719. (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
  4720. /*
  4721. * If its in the TCP pool, its NOT allowed to create an
  4722. * association. The parent listener needs to call
  4723. * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
  4724. * off, or connected one does this.. its an error.
  4725. */
  4726. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4727. *error = EINVAL;
  4728. return (NULL);
  4729. }
  4730. if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
  4731. (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
  4732. if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
  4733. (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
  4734. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4735. *error = EINVAL;
  4736. return (NULL);
  4737. }
  4738. }
  4739. SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
  4740. #ifdef SCTP_DEBUG
  4741. if (firstaddr) {
  4742. SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
  4743. switch (firstaddr->sa_family) {
  4744. #ifdef INET
  4745. case AF_INET:
  4746. SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
  4747. ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
  4748. break;
  4749. #endif
  4750. #ifdef INET6
  4751. case AF_INET6:
  4752. SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
  4753. ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
  4754. break;
  4755. #endif
  4756. #if defined(__Userspace__)
  4757. case AF_CONN:
  4758. SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
  4759. ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
  4760. break;
  4761. #endif
  4762. default:
  4763. break;
  4764. }
  4765. } else {
  4766. SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
  4767. }
  4768. #endif /* SCTP_DEBUG */
  4769. switch (firstaddr->sa_family) {
  4770. #ifdef INET
  4771. case AF_INET:
  4772. {
  4773. struct sockaddr_in *sin;
  4774. sin = (struct sockaddr_in *)firstaddr;
  4775. if ((ntohs(sin->sin_port) == 0) ||
  4776. (sin->sin_addr.s_addr == INADDR_ANY) ||
  4777. (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
  4778. IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
  4779. #if defined(__Userspace__)
  4780. ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) ||
  4781. ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  4782. (SCTP_IPV6_V6ONLY(inp) != 0)))) {
  4783. #else
  4784. ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
  4785. (SCTP_IPV6_V6ONLY(inp) != 0))) {
  4786. #endif
  4787. /* Invalid address */
  4788. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4789. *error = EINVAL;
  4790. return (NULL);
  4791. }
  4792. rport = sin->sin_port;
  4793. break;
  4794. }
  4795. #endif
  4796. #ifdef INET6
  4797. case AF_INET6:
  4798. {
  4799. struct sockaddr_in6 *sin6;
  4800. sin6 = (struct sockaddr_in6 *)firstaddr;
  4801. if ((ntohs(sin6->sin6_port) == 0) ||
  4802. IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
  4803. IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
  4804. ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) {
  4805. /* Invalid address */
  4806. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4807. *error = EINVAL;
  4808. return (NULL);
  4809. }
  4810. rport = sin6->sin6_port;
  4811. break;
  4812. }
  4813. #endif
  4814. #if defined(__Userspace__)
  4815. case AF_CONN:
  4816. {
  4817. struct sockaddr_conn *sconn;
  4818. sconn = (struct sockaddr_conn *)firstaddr;
  4819. if ((ntohs(sconn->sconn_port) == 0) ||
  4820. (sconn->sconn_addr == NULL) ||
  4821. ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) {
  4822. /* Invalid address */
  4823. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4824. *error = EINVAL;
  4825. return (NULL);
  4826. }
  4827. rport = sconn->sconn_port;
  4828. break;
  4829. }
  4830. #endif
  4831. default:
  4832. /* not supported family type */
  4833. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
  4834. *error = EINVAL;
  4835. return (NULL);
  4836. }
  4837. if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
  4838. /*
  4839. * If you have not performed a bind, then we need to do the
  4840. * ephemeral bind for you.
  4841. */
  4842. if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
  4843. /* bind error, probably perm */
  4844. *error = err;
  4845. return (NULL);
  4846. }
  4847. }
  4848. stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
  4849. if (stcb == NULL) {
  4850. /* out of memory? */
  4851. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
  4852. *error = ENOMEM;
  4853. return (NULL);
  4854. }
  4855. SCTP_INCR_ASOC_COUNT();
  4856. memset(stcb, 0, sizeof(*stcb));
  4857. asoc = &stcb->asoc;
  4858. SCTP_TCB_LOCK_INIT(stcb);
  4859. stcb->rport = rport;
  4860. /* setup back pointer's */
  4861. stcb->sctp_ep = inp;
  4862. stcb->sctp_socket = inp->sctp_socket;
  4863. if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) {
  4864. /* failed */
  4865. SCTP_TCB_LOCK_DESTROY(stcb);
  4866. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
  4867. SCTP_DECR_ASOC_COUNT();
  4868. *error = err;
  4869. return (NULL);
  4870. }
  4871. SCTP_TCB_LOCK(stcb);
  4872. asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
  4873. /* now that my_vtag is set, add it to the hash */
  4874. head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
  4875. /* put it in the bucket in the vtag hash of assoc's for the system */
  4876. LIST_INSERT_HEAD(head, stcb, sctp_asocs);
  4877. if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) {
  4878. /* failure.. memory error? */
  4879. if (asoc->strmout) {
  4880. SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
  4881. asoc->strmout = NULL;
  4882. }
  4883. if (asoc->mapping_array) {
  4884. SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
  4885. asoc->mapping_array = NULL;
  4886. }
  4887. if (asoc->nr_mapping_array) {
  4888. SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
  4889. asoc->nr_mapping_array = NULL;
  4890. }
  4891. SCTP_DECR_ASOC_COUNT();
  4892. SCTP_TCB_UNLOCK(stcb);
  4893. SCTP_TCB_LOCK_DESTROY(stcb);
  4894. LIST_REMOVE(stcb, sctp_asocs);
  4895. LIST_REMOVE(stcb, sctp_tcbasocidhash);
  4896. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
  4897. SCTP_INP_WUNLOCK(inp);
  4898. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
  4899. *error = ENOBUFS;
  4900. return (NULL);
  4901. }
  4902. /* Init all the timers */
  4903. SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
  4904. SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
  4905. SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
  4906. SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
  4907. SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
  4908. SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
  4909. LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
  4910. /* now file the port under the hash as well */
  4911. if (inp->sctp_tcbhash != NULL) {
  4912. head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
  4913. inp->sctp_hashmark)];
  4914. LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
  4915. }
  4916. if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
  4917. sctp_initialize_auth_params(inp, stcb);
  4918. }
  4919. SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
  4920. return (stcb);
  4921. }
  4922. struct sctp_tcb *
  4923. sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
  4924. int *error, uint32_t override_tag, uint32_t initial_tsn,
  4925. uint32_t vrf_id, uint16_t o_streams, uint16_t port,
  4926. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4927. struct thread *p,
  4928. #elif defined(_WIN32) && !defined(__Userspace__)
  4929. PKTHREAD p,
  4930. #else
  4931. struct proc *p,
  4932. #endif
  4933. int initialize_auth_params)
  4934. {
  4935. struct sctp_tcb *stcb;
  4936. SCTP_INP_INFO_WLOCK();
  4937. SCTP_INP_WLOCK(inp);
  4938. stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
  4939. initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
  4940. SCTP_INP_INFO_WUNLOCK();
  4941. SCTP_INP_WUNLOCK(inp);
  4942. return (stcb);
  4943. }
  4944. struct sctp_tcb *
  4945. sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
  4946. int *error, uint32_t override_tag, uint32_t initial_tsn,
  4947. uint32_t vrf_id, uint16_t o_streams, uint16_t port,
  4948. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4949. struct thread *p,
  4950. #elif defined(_WIN32) && !defined(__Userspace__)
  4951. PKTHREAD p,
  4952. #else
  4953. struct proc *p,
  4954. #endif
  4955. int initialize_auth_params)
  4956. {
  4957. struct sctp_tcb *stcb;
  4958. SCTP_INP_INFO_WLOCK();
  4959. SCTP_INP_WLOCK(inp);
  4960. if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
  4961. SCTP_IS_LISTENING(inp)) {
  4962. SCTP_INP_INFO_WUNLOCK();
  4963. SCTP_INP_WUNLOCK(inp);
  4964. *error = EINVAL;
  4965. return (NULL);
  4966. }
  4967. stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
  4968. initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
  4969. SCTP_INP_INFO_WUNLOCK();
  4970. if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
  4971. inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
  4972. soisconnecting(inp->sctp_socket);
  4973. }
  4974. SCTP_INP_WUNLOCK(inp);
  4975. return (stcb);
  4976. }
  4977. void
  4978. sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
  4979. {
  4980. struct sctp_inpcb *inp;
  4981. struct sctp_association *asoc;
  4982. inp = stcb->sctp_ep;
  4983. asoc = &stcb->asoc;
  4984. asoc->numnets--;
  4985. TAILQ_REMOVE(&asoc->nets, net, sctp_next);
  4986. if (net == asoc->primary_destination) {
  4987. /* Reset primary */
  4988. struct sctp_nets *lnet;
  4989. lnet = TAILQ_FIRST(&asoc->nets);
  4990. /* Mobility adaptation
  4991. Ideally, if deleted destination is the primary, it becomes
  4992. a fast retransmission trigger by the subsequent SET PRIMARY.
  4993. (by micchie)
  4994. */
  4995. if (sctp_is_mobility_feature_on(stcb->sctp_ep,
  4996. SCTP_MOBILITY_BASE) ||
  4997. sctp_is_mobility_feature_on(stcb->sctp_ep,
  4998. SCTP_MOBILITY_FASTHANDOFF)) {
  4999. SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
  5000. if (asoc->deleted_primary != NULL) {
  5001. SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
  5002. goto out;
  5003. }
  5004. asoc->deleted_primary = net;
  5005. atomic_add_int(&net->ref_count, 1);
  5006. memset(&net->lastsa, 0, sizeof(net->lastsa));
  5007. memset(&net->lastsv, 0, sizeof(net->lastsv));
  5008. sctp_mobility_feature_on(stcb->sctp_ep,
  5009. SCTP_MOBILITY_PRIM_DELETED);
  5010. sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
  5011. stcb->sctp_ep, stcb, NULL);
  5012. }
  5013. out:
  5014. /* Try to find a confirmed primary */
  5015. asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
  5016. }
  5017. if (net == asoc->last_data_chunk_from) {
  5018. /* Reset primary */
  5019. asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
  5020. }
  5021. if (net == asoc->last_control_chunk_from) {
  5022. /* Clear net */
  5023. asoc->last_control_chunk_from = NULL;
  5024. }
  5025. if (net == asoc->last_net_cmt_send_started) {
  5026. /* Clear net */
  5027. asoc->last_net_cmt_send_started = NULL;
  5028. }
  5029. if (net == stcb->asoc.alternate) {
  5030. sctp_free_remote_addr(stcb->asoc.alternate);
  5031. stcb->asoc.alternate = NULL;
  5032. }
  5033. sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
  5034. SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
  5035. sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
  5036. SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
  5037. net->dest_state |= SCTP_ADDR_BEING_DELETED;
  5038. sctp_free_remote_addr(net);
  5039. }
  5040. /*
  5041. * remove a remote endpoint address from an association, it will fail if the
  5042. * address does not exist.
  5043. */
  5044. int
  5045. sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
  5046. {
  5047. /*
  5048. * Here we need to remove a remote address. This is quite simple, we
  5049. * first find it in the list of address for the association
  5050. * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
  5051. * on that item. Note we do not allow it to be removed if there are
  5052. * no other addresses.
  5053. */
  5054. struct sctp_association *asoc;
  5055. struct sctp_nets *net, *nnet;
  5056. asoc = &stcb->asoc;
  5057. /* locate the address */
  5058. TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
  5059. if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
  5060. continue;
  5061. }
  5062. if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
  5063. remaddr)) {
  5064. /* we found the guy */
  5065. if (asoc->numnets < 2) {
  5066. /* Must have at LEAST two remote addresses */
  5067. return (-1);
  5068. } else {
  5069. sctp_remove_net(stcb, net);
  5070. return (0);
  5071. }
  5072. }
  5073. }
  5074. /* not found. */
  5075. return (-2);
  5076. }
  5077. static bool
  5078. sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, uint32_t now)
  5079. {
  5080. struct sctpvtaghead *chain;
  5081. struct sctp_tagblock *twait_block;
  5082. int i;
  5083. SCTP_INP_INFO_LOCK_ASSERT();
  5084. chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
  5085. LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
  5086. for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
  5087. if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) &&
  5088. (twait_block->vtag_block[i].v_tag == tag) &&
  5089. (twait_block->vtag_block[i].lport == lport) &&
  5090. (twait_block->vtag_block[i].rport == rport)) {
  5091. return (true);
  5092. }
  5093. }
  5094. }
  5095. return (false);
  5096. }
  5097. static void
  5098. sctp_set_vtag_block(struct sctp_timewait *vtag_block, uint32_t time,
  5099. uint32_t tag, uint16_t lport, uint16_t rport)
  5100. {
  5101. vtag_block->tv_sec_at_expire = time;
  5102. vtag_block->v_tag = tag;
  5103. vtag_block->lport = lport;
  5104. vtag_block->rport = rport;
  5105. }
  5106. static void
  5107. sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
  5108. {
  5109. struct sctpvtaghead *chain;
  5110. struct sctp_tagblock *twait_block;
  5111. struct timeval now;
  5112. uint32_t time;
  5113. int i;
  5114. bool set;
  5115. SCTP_INP_INFO_WLOCK_ASSERT();
  5116. (void)SCTP_GETTIME_TIMEVAL(&now);
  5117. time = (uint32_t)now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait);
  5118. chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
  5119. set = false;
  5120. LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
  5121. /* Block(s) present, lets find space, and expire on the fly */
  5122. for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
  5123. if ((twait_block->vtag_block[i].v_tag == 0) && !set) {
  5124. sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
  5125. set = true;
  5126. continue;
  5127. }
  5128. if ((twait_block->vtag_block[i].v_tag != 0) &&
  5129. (twait_block->vtag_block[i].tv_sec_at_expire < (uint32_t)now.tv_sec)) {
  5130. if (set) {
  5131. /* Audit expires this guy */
  5132. sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0);
  5133. } else {
  5134. /* Reuse it for the new tag */
  5135. sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
  5136. set = true;
  5137. }
  5138. }
  5139. }
  5140. if (set) {
  5141. /*
  5142. * We only do up to the block where we can
  5143. * place our tag for audits
  5144. */
  5145. break;
  5146. }
  5147. }
  5148. /* Need to add a new block to chain */
  5149. if (!set) {
  5150. SCTP_MALLOC(twait_block, struct sctp_tagblock *,
  5151. sizeof(struct sctp_tagblock), SCTP_M_TIMW);
  5152. if (twait_block == NULL) {
  5153. return;
  5154. }
  5155. memset(twait_block, 0, sizeof(struct sctp_tagblock));
  5156. LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
  5157. sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport);
  5158. }
  5159. }
  5160. void
  5161. sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
  5162. {
  5163. struct sctp_tmit_chunk *chk, *nchk;
  5164. struct sctp_queued_to_read *control, *ncontrol;
  5165. TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
  5166. TAILQ_REMOVE(rh, control, next_instrm);
  5167. control->on_strm_q = 0;
  5168. if (control->on_read_q == 0) {
  5169. sctp_free_remote_addr(control->whoFrom);
  5170. if (control->data) {
  5171. sctp_m_freem(control->data);
  5172. control->data = NULL;
  5173. }
  5174. }
  5175. /* Reassembly free? */
  5176. TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
  5177. TAILQ_REMOVE(&control->reasm, chk, sctp_next);
  5178. if (chk->data) {
  5179. sctp_m_freem(chk->data);
  5180. chk->data = NULL;
  5181. }
  5182. if (chk->holds_key_ref)
  5183. sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
  5184. sctp_free_remote_addr(chk->whoTo);
  5185. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
  5186. SCTP_DECR_CHK_COUNT();
  5187. /*sa_ignore FREED_MEMORY*/
  5188. }
  5189. /*
  5190. * We don't free the address here
  5191. * since all the net's were freed
  5192. * above.
  5193. */
  5194. if (control->on_read_q == 0) {
  5195. sctp_free_a_readq(stcb, control);
  5196. }
  5197. }
  5198. }
  5199. /*-
  5200. * Free the association after un-hashing the remote port. This
  5201. * function ALWAYS returns holding NO LOCK on the stcb. It DOES
  5202. * expect that the input to this function IS a locked TCB.
  5203. * It will return 0, if it did NOT destroy the association (instead
  5204. * it unlocks it. It will return NON-zero if it either destroyed the
  5205. * association OR the association is already destroyed.
  5206. */
  5207. int
  5208. sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
  5209. {
  5210. int i;
  5211. struct sctp_association *asoc;
  5212. struct sctp_nets *net, *nnet;
  5213. struct sctp_laddr *laddr, *naddr;
  5214. struct sctp_tmit_chunk *chk, *nchk;
  5215. struct sctp_asconf_addr *aparam, *naparam;
  5216. struct sctp_asconf_ack *aack, *naack;
  5217. struct sctp_stream_reset_list *strrst, *nstrrst;
  5218. struct sctp_queued_to_read *sq, *nsq;
  5219. struct sctp_stream_queue_pending *sp, *nsp;
  5220. sctp_sharedkey_t *shared_key, *nshared_key;
  5221. struct socket *so;
  5222. /* first, lets purge the entry from the hash table. */
  5223. #if defined(__APPLE__) && !defined(__Userspace__)
  5224. sctp_lock_assert(SCTP_INP_SO(inp));
  5225. #endif
  5226. SCTP_TCB_LOCK_ASSERT(stcb);
  5227. #ifdef SCTP_LOG_CLOSING
  5228. sctp_log_closing(inp, stcb, 6);
  5229. #endif
  5230. if (stcb->asoc.state == 0) {
  5231. #ifdef SCTP_LOG_CLOSING
  5232. sctp_log_closing(inp, NULL, 7);
  5233. #endif
  5234. /* there is no asoc, really TSNH :-0 */
  5235. return (1);
  5236. }
  5237. if (stcb->asoc.alternate) {
  5238. sctp_free_remote_addr(stcb->asoc.alternate);
  5239. stcb->asoc.alternate = NULL;
  5240. }
  5241. #if !(defined(__APPLE__) && !defined(__Userspace__))
  5242. /* TEMP CODE */
  5243. if (stcb->freed_from_where == 0) {
  5244. /* Only record the first place free happened from */
  5245. stcb->freed_from_where = from_location;
  5246. }
  5247. /* TEMP CODE */
  5248. #endif
  5249. asoc = &stcb->asoc;
  5250. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  5251. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
  5252. /* nothing around */
  5253. so = NULL;
  5254. else
  5255. so = inp->sctp_socket;
  5256. /*
  5257. * We used timer based freeing if a reader or writer is in the way.
  5258. * So we first check if we are actually being called from a timer,
  5259. * if so we abort early if a reader or writer is still in the way.
  5260. */
  5261. if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
  5262. (from_inpcbfree == SCTP_NORMAL_PROC)) {
  5263. /*
  5264. * is it the timer driving us? if so are the reader/writers
  5265. * gone?
  5266. */
  5267. if (stcb->asoc.refcnt) {
  5268. /* nope, reader or writer in the way */
  5269. sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
  5270. /* no asoc destroyed */
  5271. SCTP_TCB_UNLOCK(stcb);
  5272. #ifdef SCTP_LOG_CLOSING
  5273. sctp_log_closing(inp, stcb, 8);
  5274. #endif
  5275. return (0);
  5276. }
  5277. }
  5278. /* Now clean up any other timers */
  5279. sctp_stop_association_timers(stcb, false);
  5280. /* Now the read queue needs to be cleaned up (only once) */
  5281. if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
  5282. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
  5283. SCTP_INP_READ_LOCK(inp);
  5284. TAILQ_FOREACH(sq, &inp->read_queue, next) {
  5285. if (sq->stcb == stcb) {
  5286. sq->do_not_ref_stcb = 1;
  5287. sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
  5288. /* If there is no end, there never
  5289. * will be now.
  5290. */
  5291. if (sq->end_added == 0) {
  5292. /* Held for PD-API clear that. */
  5293. sq->pdapi_aborted = 1;
  5294. sq->held_length = 0;
  5295. if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
  5296. /*
  5297. * Need to add a PD-API aborted indication.
  5298. * Setting the control_pdapi assures that it will
  5299. * be added right after this msg.
  5300. */
  5301. uint32_t strseq;
  5302. stcb->asoc.control_pdapi = sq;
  5303. strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff);
  5304. sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
  5305. stcb,
  5306. SCTP_PARTIAL_DELIVERY_ABORTED,
  5307. (void *)&strseq,
  5308. SCTP_SO_LOCKED);
  5309. stcb->asoc.control_pdapi = NULL;
  5310. }
  5311. }
  5312. /* Add an end to wake them */
  5313. sq->end_added = 1;
  5314. }
  5315. }
  5316. SCTP_INP_READ_UNLOCK(inp);
  5317. if (stcb->block_entry) {
  5318. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
  5319. stcb->block_entry->error = ECONNRESET;
  5320. stcb->block_entry = NULL;
  5321. }
  5322. }
  5323. if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
  5324. /* Someone holds a reference OR the socket is unaccepted yet.
  5325. */
  5326. if ((stcb->asoc.refcnt) ||
  5327. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  5328. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
  5329. SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
  5330. sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
  5331. }
  5332. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  5333. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
  5334. /* nothing around */
  5335. so = NULL;
  5336. if (so) {
  5337. /* Wake any reader/writers */
  5338. sctp_sorwakeup(inp, so);
  5339. sctp_sowwakeup(inp, so);
  5340. }
  5341. SCTP_TCB_UNLOCK(stcb);
  5342. #ifdef SCTP_LOG_CLOSING
  5343. sctp_log_closing(inp, stcb, 9);
  5344. #endif
  5345. /* no asoc destroyed */
  5346. return (0);
  5347. }
  5348. #ifdef SCTP_LOG_CLOSING
  5349. sctp_log_closing(inp, stcb, 10);
  5350. #endif
  5351. /* When I reach here, no others want
  5352. * to kill the assoc yet.. and I own
  5353. * the lock. Now its possible an abort
  5354. * comes in when I do the lock exchange
  5355. * below to grab all the locks to do
  5356. * the final take out. to prevent this
  5357. * we increment the count, which will
  5358. * start a timer and blow out above thus
  5359. * assuring us that we hold exclusive
  5360. * killing of the asoc. Note that
  5361. * after getting back the TCB lock
  5362. * we will go ahead and increment the
  5363. * counter back up and stop any timer
  5364. * a passing stranger may have started :-S
  5365. */
  5366. if (from_inpcbfree == SCTP_NORMAL_PROC) {
  5367. atomic_add_int(&stcb->asoc.refcnt, 1);
  5368. SCTP_TCB_UNLOCK(stcb);
  5369. SCTP_INP_INFO_WLOCK();
  5370. SCTP_INP_WLOCK(inp);
  5371. SCTP_TCB_LOCK(stcb);
  5372. }
  5373. /* Double check the GONE flag */
  5374. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
  5375. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
  5376. /* nothing around */
  5377. so = NULL;
  5378. if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  5379. (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
  5380. /*
  5381. * For TCP type we need special handling when we are
  5382. * connected. We also include the peel'ed off ones to.
  5383. */
  5384. if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
  5385. inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
  5386. inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
  5387. if (so) {
  5388. SOCKBUF_LOCK(&so->so_rcv);
  5389. so->so_state &= ~(SS_ISCONNECTING |
  5390. SS_ISDISCONNECTING |
  5391. SS_ISCONFIRMING |
  5392. SS_ISCONNECTED);
  5393. so->so_state |= SS_ISDISCONNECTED;
  5394. #if defined(__APPLE__) && !defined(__Userspace__)
  5395. socantrcvmore(so);
  5396. #else
  5397. socantrcvmore_locked(so);
  5398. #endif
  5399. socantsendmore(so);
  5400. sctp_sowwakeup(inp, so);
  5401. sctp_sorwakeup(inp, so);
  5402. SCTP_SOWAKEUP(so);
  5403. }
  5404. }
  5405. }
  5406. /* Make it invalid too, that way if its
  5407. * about to run it will abort and return.
  5408. */
  5409. /* re-increment the lock */
  5410. if (from_inpcbfree == SCTP_NORMAL_PROC) {
  5411. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  5412. }
  5413. if (stcb->asoc.refcnt) {
  5414. SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
  5415. sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
  5416. if (from_inpcbfree == SCTP_NORMAL_PROC) {
  5417. SCTP_INP_INFO_WUNLOCK();
  5418. SCTP_INP_WUNLOCK(inp);
  5419. }
  5420. SCTP_TCB_UNLOCK(stcb);
  5421. return (0);
  5422. }
  5423. asoc->state = 0;
  5424. if (inp->sctp_tcbhash) {
  5425. LIST_REMOVE(stcb, sctp_tcbhash);
  5426. }
  5427. if (stcb->asoc.in_asocid_hash) {
  5428. LIST_REMOVE(stcb, sctp_tcbasocidhash);
  5429. }
  5430. if (inp->sctp_socket == NULL) {
  5431. stcb->sctp_socket = NULL;
  5432. }
  5433. /* Now lets remove it from the list of ALL associations in the EP */
  5434. LIST_REMOVE(stcb, sctp_tcblist);
  5435. if (from_inpcbfree == SCTP_NORMAL_PROC) {
  5436. SCTP_INP_INCR_REF(inp);
  5437. SCTP_INP_WUNLOCK(inp);
  5438. }
  5439. /* pull from vtag hash */
  5440. LIST_REMOVE(stcb, sctp_asocs);
  5441. sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport);
  5442. /* Now restop the timers to be sure
  5443. * this is paranoia at is finest!
  5444. */
  5445. sctp_stop_association_timers(stcb, true);
  5446. /*
  5447. * The chunk lists and such SHOULD be empty but we check them just
  5448. * in case.
  5449. */
  5450. /* anything on the wheel needs to be removed */
  5451. for (i = 0; i < asoc->streamoutcnt; i++) {
  5452. struct sctp_stream_out *outs;
  5453. outs = &asoc->strmout[i];
  5454. /* now clean up any chunks here */
  5455. TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
  5456. atomic_subtract_int(&asoc->stream_queue_cnt, 1);
  5457. TAILQ_REMOVE(&outs->outqueue, sp, next);
  5458. stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
  5459. sctp_free_spbufspace(stcb, asoc, sp);
  5460. if (sp->data) {
  5461. if (so) {
  5462. /* Still an open socket - report */
  5463. sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
  5464. 0, (void *)sp, SCTP_SO_LOCKED);
  5465. }
  5466. if (sp->data) {
  5467. sctp_m_freem(sp->data);
  5468. sp->data = NULL;
  5469. sp->tail_mbuf = NULL;
  5470. sp->length = 0;
  5471. }
  5472. }
  5473. if (sp->net) {
  5474. sctp_free_remote_addr(sp->net);
  5475. sp->net = NULL;
  5476. }
  5477. sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
  5478. }
  5479. }
  5480. /*sa_ignore FREED_MEMORY*/
  5481. TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
  5482. TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
  5483. SCTP_FREE(strrst, SCTP_M_STRESET);
  5484. }
  5485. TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
  5486. TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
  5487. if (sq->data) {
  5488. sctp_m_freem(sq->data);
  5489. sq->data = NULL;
  5490. }
  5491. sctp_free_remote_addr(sq->whoFrom);
  5492. sq->whoFrom = NULL;
  5493. sq->stcb = NULL;
  5494. /* Free the ctl entry */
  5495. sctp_free_a_readq(stcb, sq);
  5496. /*sa_ignore FREED_MEMORY*/
  5497. }
  5498. TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
  5499. TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
  5500. if (chk->data) {
  5501. sctp_m_freem(chk->data);
  5502. chk->data = NULL;
  5503. }
  5504. if (chk->holds_key_ref)
  5505. sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
  5506. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
  5507. SCTP_DECR_CHK_COUNT();
  5508. atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
  5509. asoc->free_chunk_cnt--;
  5510. /*sa_ignore FREED_MEMORY*/
  5511. }
  5512. /* pending send queue SHOULD be empty */
  5513. TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
  5514. if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
  5515. asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
  5516. #ifdef INVARIANTS
  5517. } else {
  5518. panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
  5519. #endif
  5520. }
  5521. TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
  5522. if (chk->data) {
  5523. if (so) {
  5524. /* Still a socket? */
  5525. sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
  5526. 0, chk, SCTP_SO_LOCKED);
  5527. }
  5528. if (chk->data) {
  5529. sctp_m_freem(chk->data);
  5530. chk->data = NULL;
  5531. }
  5532. }
  5533. if (chk->holds_key_ref)
  5534. sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
  5535. if (chk->whoTo) {
  5536. sctp_free_remote_addr(chk->whoTo);
  5537. chk->whoTo = NULL;
  5538. }
  5539. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
  5540. SCTP_DECR_CHK_COUNT();
  5541. /*sa_ignore FREED_MEMORY*/
  5542. }
  5543. /* sent queue SHOULD be empty */
  5544. TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
  5545. if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
  5546. if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
  5547. asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
  5548. #ifdef INVARIANTS
  5549. } else {
  5550. panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
  5551. #endif
  5552. }
  5553. }
  5554. TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
  5555. if (chk->data) {
  5556. if (so) {
  5557. /* Still a socket? */
  5558. sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
  5559. 0, chk, SCTP_SO_LOCKED);
  5560. }
  5561. if (chk->data) {
  5562. sctp_m_freem(chk->data);
  5563. chk->data = NULL;
  5564. }
  5565. }
  5566. if (chk->holds_key_ref)
  5567. sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
  5568. sctp_free_remote_addr(chk->whoTo);
  5569. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
  5570. SCTP_DECR_CHK_COUNT();
  5571. /*sa_ignore FREED_MEMORY*/
  5572. }
  5573. #ifdef INVARIANTS
  5574. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  5575. if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
  5576. panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
  5577. }
  5578. }
  5579. #endif
  5580. /* control queue MAY not be empty */
  5581. TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
  5582. TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
  5583. if (chk->data) {
  5584. sctp_m_freem(chk->data);
  5585. chk->data = NULL;
  5586. }
  5587. if (chk->holds_key_ref)
  5588. sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
  5589. sctp_free_remote_addr(chk->whoTo);
  5590. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
  5591. SCTP_DECR_CHK_COUNT();
  5592. /*sa_ignore FREED_MEMORY*/
  5593. }
  5594. /* ASCONF queue MAY not be empty */
  5595. TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
  5596. TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
  5597. if (chk->data) {
  5598. sctp_m_freem(chk->data);
  5599. chk->data = NULL;
  5600. }
  5601. if (chk->holds_key_ref)
  5602. sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
  5603. sctp_free_remote_addr(chk->whoTo);
  5604. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
  5605. SCTP_DECR_CHK_COUNT();
  5606. /*sa_ignore FREED_MEMORY*/
  5607. }
  5608. if (asoc->mapping_array) {
  5609. SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
  5610. asoc->mapping_array = NULL;
  5611. }
  5612. if (asoc->nr_mapping_array) {
  5613. SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
  5614. asoc->nr_mapping_array = NULL;
  5615. }
  5616. /* the stream outs */
  5617. if (asoc->strmout) {
  5618. SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
  5619. asoc->strmout = NULL;
  5620. }
  5621. asoc->strm_realoutsize = asoc->streamoutcnt = 0;
  5622. if (asoc->strmin) {
  5623. for (i = 0; i < asoc->streamincnt; i++) {
  5624. sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
  5625. sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
  5626. }
  5627. SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
  5628. asoc->strmin = NULL;
  5629. }
  5630. asoc->streamincnt = 0;
  5631. TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
  5632. #ifdef INVARIANTS
  5633. if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
  5634. panic("no net's left alloc'ed, or list points to itself");
  5635. }
  5636. #endif
  5637. TAILQ_REMOVE(&asoc->nets, net, sctp_next);
  5638. sctp_free_remote_addr(net);
  5639. }
  5640. LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
  5641. /*sa_ignore FREED_MEMORY*/
  5642. sctp_remove_laddr(laddr);
  5643. }
  5644. /* pending asconf (address) parameters */
  5645. TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
  5646. /*sa_ignore FREED_MEMORY*/
  5647. TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
  5648. SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
  5649. }
  5650. TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
  5651. /*sa_ignore FREED_MEMORY*/
  5652. TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
  5653. if (aack->data != NULL) {
  5654. sctp_m_freem(aack->data);
  5655. }
  5656. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
  5657. }
  5658. /* clean up auth stuff */
  5659. if (asoc->local_hmacs)
  5660. sctp_free_hmaclist(asoc->local_hmacs);
  5661. if (asoc->peer_hmacs)
  5662. sctp_free_hmaclist(asoc->peer_hmacs);
  5663. if (asoc->local_auth_chunks)
  5664. sctp_free_chunklist(asoc->local_auth_chunks);
  5665. if (asoc->peer_auth_chunks)
  5666. sctp_free_chunklist(asoc->peer_auth_chunks);
  5667. sctp_free_authinfo(&asoc->authinfo);
  5668. LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
  5669. LIST_REMOVE(shared_key, next);
  5670. sctp_free_sharedkey(shared_key);
  5671. /*sa_ignore FREED_MEMORY*/
  5672. }
  5673. /* Insert new items here :> */
  5674. /* Get rid of LOCK */
  5675. SCTP_TCB_UNLOCK(stcb);
  5676. SCTP_TCB_LOCK_DESTROY(stcb);
  5677. if (from_inpcbfree == SCTP_NORMAL_PROC) {
  5678. SCTP_INP_INFO_WUNLOCK();
  5679. SCTP_INP_RLOCK(inp);
  5680. }
  5681. #if defined(__APPLE__) && !defined(__Userspace__)
  5682. /* TEMP CODE */
  5683. stcb->freed_from_where = from_location;
  5684. #endif
  5685. #ifdef SCTP_TRACK_FREED_ASOCS
  5686. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  5687. /* now clean up the tasoc itself */
  5688. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
  5689. SCTP_DECR_ASOC_COUNT();
  5690. } else {
  5691. LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
  5692. }
  5693. #else
  5694. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
  5695. SCTP_DECR_ASOC_COUNT();
  5696. #endif
  5697. if (from_inpcbfree == SCTP_NORMAL_PROC) {
  5698. if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
  5699. /* If its NOT the inp_free calling us AND
  5700. * sctp_close as been called, we
  5701. * call back...
  5702. */
  5703. SCTP_INP_RUNLOCK(inp);
  5704. /* This will start the kill timer (if we are
  5705. * the last one) since we hold an increment yet. But
  5706. * this is the only safe way to do this
  5707. * since otherwise if the socket closes
  5708. * at the same time we are here we might
  5709. * collide in the cleanup.
  5710. */
  5711. sctp_inpcb_free(inp,
  5712. SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
  5713. SCTP_CALLED_DIRECTLY_NOCMPSET);
  5714. SCTP_INP_DECR_REF(inp);
  5715. } else {
  5716. /* The socket is still open. */
  5717. SCTP_INP_DECR_REF(inp);
  5718. SCTP_INP_RUNLOCK(inp);
  5719. }
  5720. }
  5721. /* destroyed the asoc */
  5722. #ifdef SCTP_LOG_CLOSING
  5723. sctp_log_closing(inp, NULL, 11);
  5724. #endif
  5725. return (1);
  5726. }
  5727. /*
  5728. * determine if a destination is "reachable" based upon the addresses bound
  5729. * to the current endpoint (e.g. only v4 or v6 currently bound)
  5730. */
  5731. /*
  5732. * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
  5733. * assoc level v4/v6 flags, as the assoc *may* not have the same address
  5734. * types bound as its endpoint
  5735. */
  5736. int
  5737. sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
  5738. {
  5739. struct sctp_inpcb *inp;
  5740. int answer;
  5741. /*
  5742. * No locks here, the TCB, in all cases is already locked and an
  5743. * assoc is up. There is either a INP lock by the caller applied (in
  5744. * asconf case when deleting an address) or NOT in the HB case,
  5745. * however if HB then the INP increment is up and the INP will not
  5746. * be removed (on top of the fact that we have a TCB lock). So we
  5747. * only want to read the sctp_flags, which is either bound-all or
  5748. * not.. no protection needed since once an assoc is up you can't be
  5749. * changing your binding.
  5750. */
  5751. inp = stcb->sctp_ep;
  5752. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  5753. /* if bound all, destination is not restricted */
  5754. /*
  5755. * RRS: Question during lock work: Is this correct? If you
  5756. * are bound-all you still might need to obey the V4--V6
  5757. * flags??? IMO this bound-all stuff needs to be removed!
  5758. */
  5759. return (1);
  5760. }
  5761. /* NOTE: all "scope" checks are done when local addresses are added */
  5762. switch (destaddr->sa_family) {
  5763. #ifdef INET6
  5764. case AF_INET6:
  5765. answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
  5766. break;
  5767. #endif
  5768. #ifdef INET
  5769. case AF_INET:
  5770. answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
  5771. break;
  5772. #endif
  5773. #if defined(__Userspace__)
  5774. case AF_CONN:
  5775. answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
  5776. break;
  5777. #endif
  5778. default:
  5779. /* invalid family, so it's unreachable */
  5780. answer = 0;
  5781. break;
  5782. }
  5783. return (answer);
  5784. }
  5785. /*
  5786. * update the inp_vflags on an endpoint
  5787. */
  5788. static void
  5789. sctp_update_ep_vflag(struct sctp_inpcb *inp)
  5790. {
  5791. struct sctp_laddr *laddr;
  5792. /* first clear the flag */
  5793. inp->ip_inp.inp.inp_vflag = 0;
  5794. /* set the flag based on addresses on the ep list */
  5795. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  5796. if (laddr->ifa == NULL) {
  5797. SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
  5798. __func__);
  5799. continue;
  5800. }
  5801. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
  5802. continue;
  5803. }
  5804. switch (laddr->ifa->address.sa.sa_family) {
  5805. #ifdef INET6
  5806. case AF_INET6:
  5807. inp->ip_inp.inp.inp_vflag |= INP_IPV6;
  5808. break;
  5809. #endif
  5810. #ifdef INET
  5811. case AF_INET:
  5812. inp->ip_inp.inp.inp_vflag |= INP_IPV4;
  5813. break;
  5814. #endif
  5815. #if defined(__Userspace__)
  5816. case AF_CONN:
  5817. inp->ip_inp.inp.inp_vflag |= INP_CONN;
  5818. break;
  5819. #endif
  5820. default:
  5821. break;
  5822. }
  5823. }
  5824. }
  5825. /*
  5826. * Add the address to the endpoint local address list There is nothing to be
  5827. * done if we are bound to all addresses
  5828. */
  5829. void
  5830. sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
  5831. {
  5832. struct sctp_laddr *laddr;
  5833. struct sctp_tcb *stcb;
  5834. int fnd, error = 0;
  5835. fnd = 0;
  5836. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  5837. /* You are already bound to all. You have it already */
  5838. return;
  5839. }
  5840. #ifdef INET6
  5841. if (ifa->address.sa.sa_family == AF_INET6) {
  5842. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  5843. /* Can't bind a non-useable addr. */
  5844. return;
  5845. }
  5846. }
  5847. #endif
  5848. /* first, is it already present? */
  5849. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  5850. if (laddr->ifa == ifa) {
  5851. fnd = 1;
  5852. break;
  5853. }
  5854. }
  5855. if (fnd == 0) {
  5856. /* Not in the ep list */
  5857. error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
  5858. if (error != 0)
  5859. return;
  5860. inp->laddr_count++;
  5861. /* update inp_vflag flags */
  5862. switch (ifa->address.sa.sa_family) {
  5863. #ifdef INET6
  5864. case AF_INET6:
  5865. inp->ip_inp.inp.inp_vflag |= INP_IPV6;
  5866. break;
  5867. #endif
  5868. #ifdef INET
  5869. case AF_INET:
  5870. inp->ip_inp.inp.inp_vflag |= INP_IPV4;
  5871. break;
  5872. #endif
  5873. #if defined(__Userspace__)
  5874. case AF_CONN:
  5875. inp->ip_inp.inp.inp_vflag |= INP_CONN;
  5876. break;
  5877. #endif
  5878. default:
  5879. break;
  5880. }
  5881. LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
  5882. sctp_add_local_addr_restricted(stcb, ifa);
  5883. }
  5884. }
  5885. return;
  5886. }
  5887. /*
  5888. * select a new (hopefully reachable) destination net (should only be used
  5889. * when we deleted an ep addr that is the only usable source address to reach
  5890. * the destination net)
  5891. */
  5892. static void
  5893. sctp_select_primary_destination(struct sctp_tcb *stcb)
  5894. {
  5895. struct sctp_nets *net;
  5896. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  5897. /* for now, we'll just pick the first reachable one we find */
  5898. if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
  5899. continue;
  5900. if (sctp_destination_is_reachable(stcb,
  5901. (struct sockaddr *)&net->ro._l_addr)) {
  5902. /* found a reachable destination */
  5903. stcb->asoc.primary_destination = net;
  5904. }
  5905. }
  5906. /* I can't there from here! ...we're gonna die shortly... */
  5907. }
  5908. /*
  5909. * Delete the address from the endpoint local address list. There is nothing
  5910. * to be done if we are bound to all addresses
  5911. */
  5912. void
  5913. sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
  5914. {
  5915. struct sctp_laddr *laddr;
  5916. int fnd;
  5917. fnd = 0;
  5918. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  5919. /* You are already bound to all. You have it already */
  5920. return;
  5921. }
  5922. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  5923. if (laddr->ifa == ifa) {
  5924. fnd = 1;
  5925. break;
  5926. }
  5927. }
  5928. if (fnd && (inp->laddr_count < 2)) {
  5929. /* can't delete unless there are at LEAST 2 addresses */
  5930. return;
  5931. }
  5932. if (fnd) {
  5933. /*
  5934. * clean up any use of this address go through our
  5935. * associations and clear any last_used_address that match
  5936. * this one for each assoc, see if a new primary_destination
  5937. * is needed
  5938. */
  5939. struct sctp_tcb *stcb;
  5940. /* clean up "next_addr_touse" */
  5941. if (inp->next_addr_touse == laddr)
  5942. /* delete this address */
  5943. inp->next_addr_touse = NULL;
  5944. /* clean up "last_used_address" */
  5945. LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
  5946. struct sctp_nets *net;
  5947. SCTP_TCB_LOCK(stcb);
  5948. if (stcb->asoc.last_used_address == laddr)
  5949. /* delete this address */
  5950. stcb->asoc.last_used_address = NULL;
  5951. /* Now spin through all the nets and purge any ref to laddr */
  5952. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  5953. if (net->ro._s_addr == laddr->ifa) {
  5954. /* Yep, purge src address selected */
  5955. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5956. RO_NHFREE(&net->ro);
  5957. #else
  5958. sctp_rtentry_t *rt;
  5959. /* delete this address if cached */
  5960. rt = net->ro.ro_rt;
  5961. if (rt != NULL) {
  5962. RTFREE(rt);
  5963. net->ro.ro_rt = NULL;
  5964. }
  5965. #endif
  5966. sctp_free_ifa(net->ro._s_addr);
  5967. net->ro._s_addr = NULL;
  5968. net->src_addr_selected = 0;
  5969. }
  5970. }
  5971. SCTP_TCB_UNLOCK(stcb);
  5972. } /* for each tcb */
  5973. /* remove it from the ep list */
  5974. sctp_remove_laddr(laddr);
  5975. inp->laddr_count--;
  5976. /* update inp_vflag flags */
  5977. sctp_update_ep_vflag(inp);
  5978. }
  5979. return;
  5980. }
  5981. /*
  5982. * Add the address to the TCB local address restricted list.
  5983. * This is a "pending" address list (eg. addresses waiting for an
  5984. * ASCONF-ACK response) and cannot be used as a valid source address.
  5985. */
  5986. void
  5987. sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
  5988. {
  5989. struct sctp_laddr *laddr;
  5990. struct sctpladdr *list;
  5991. /*
  5992. * Assumes TCB is locked.. and possibly the INP. May need to
  5993. * confirm/fix that if we need it and is not the case.
  5994. */
  5995. list = &stcb->asoc.sctp_restricted_addrs;
  5996. #ifdef INET6
  5997. if (ifa->address.sa.sa_family == AF_INET6) {
  5998. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  5999. /* Can't bind a non-existent addr. */
  6000. return;
  6001. }
  6002. }
  6003. #endif
  6004. /* does the address already exist? */
  6005. LIST_FOREACH(laddr, list, sctp_nxt_addr) {
  6006. if (laddr->ifa == ifa) {
  6007. return;
  6008. }
  6009. }
  6010. /* add to the list */
  6011. (void)sctp_insert_laddr(list, ifa, 0);
  6012. return;
  6013. }
  6014. /*
  6015. * Remove a local address from the TCB local address restricted list
  6016. */
  6017. void
  6018. sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
  6019. {
  6020. struct sctp_inpcb *inp;
  6021. struct sctp_laddr *laddr;
  6022. /*
  6023. * This is called by asconf work. It is assumed that a) The TCB is
  6024. * locked and b) The INP is locked. This is true in as much as I can
  6025. * trace through the entry asconf code where I did these locks.
  6026. * Again, the ASCONF code is a bit different in that it does lock
  6027. * the INP during its work often times. This must be since we don't
  6028. * want other proc's looking up things while what they are looking
  6029. * up is changing :-D
  6030. */
  6031. inp = stcb->sctp_ep;
  6032. /* if subset bound and don't allow ASCONF's, can't delete last */
  6033. if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
  6034. sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
  6035. if (stcb->sctp_ep->laddr_count < 2) {
  6036. /* can't delete last address */
  6037. return;
  6038. }
  6039. }
  6040. LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
  6041. /* remove the address if it exists */
  6042. if (laddr->ifa == NULL)
  6043. continue;
  6044. if (laddr->ifa == ifa) {
  6045. sctp_remove_laddr(laddr);
  6046. return;
  6047. }
  6048. }
  6049. /* address not found! */
  6050. return;
  6051. }
  6052. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6053. /* sysctl */
  6054. static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
  6055. static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
  6056. #endif
  6057. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6058. #if defined(SCTP_MCORE_INPUT) && defined(SMP)
  6059. struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
  6060. int *sctp_cpuarry = NULL;
  6061. void
  6062. sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
  6063. {
  6064. /* Queue a packet to a processor for the specified core */
  6065. struct sctp_mcore_queue *qent;
  6066. struct sctp_mcore_ctrl *wkq;
  6067. int need_wake = 0;
  6068. if (sctp_mcore_workers == NULL) {
  6069. /* Something went way bad during setup */
  6070. sctp_input_with_port(m, off, 0);
  6071. return;
  6072. }
  6073. SCTP_MALLOC(qent, struct sctp_mcore_queue *,
  6074. (sizeof(struct sctp_mcore_queue)),
  6075. SCTP_M_MCORE);
  6076. if (qent == NULL) {
  6077. /* This is trouble */
  6078. sctp_input_with_port(m, off, 0);
  6079. return;
  6080. }
  6081. qent->vn = curvnet;
  6082. qent->m = m;
  6083. qent->off = off;
  6084. qent->v6 = 0;
  6085. wkq = &sctp_mcore_workers[cpu_to_use];
  6086. SCTP_MCORE_QLOCK(wkq);
  6087. TAILQ_INSERT_TAIL(&wkq->que, qent, next);
  6088. if (wkq->running == 0) {
  6089. need_wake = 1;
  6090. }
  6091. SCTP_MCORE_QUNLOCK(wkq);
  6092. if (need_wake) {
  6093. wakeup(&wkq->running);
  6094. }
  6095. }
  6096. static void
  6097. sctp_mcore_thread(void *arg)
  6098. {
  6099. struct sctp_mcore_ctrl *wkq;
  6100. struct sctp_mcore_queue *qent;
  6101. wkq = (struct sctp_mcore_ctrl *)arg;
  6102. struct mbuf *m;
  6103. int off, v6;
  6104. /* Wait for first tickle */
  6105. SCTP_MCORE_LOCK(wkq);
  6106. wkq->running = 0;
  6107. msleep(&wkq->running,
  6108. &wkq->core_mtx,
  6109. 0, "wait for pkt", 0);
  6110. SCTP_MCORE_UNLOCK(wkq);
  6111. /* Bind to our cpu */
  6112. thread_lock(curthread);
  6113. sched_bind(curthread, wkq->cpuid);
  6114. thread_unlock(curthread);
  6115. /* Now lets start working */
  6116. SCTP_MCORE_LOCK(wkq);
  6117. /* Now grab lock and go */
  6118. for (;;) {
  6119. SCTP_MCORE_QLOCK(wkq);
  6120. skip_sleep:
  6121. wkq->running = 1;
  6122. qent = TAILQ_FIRST(&wkq->que);
  6123. if (qent) {
  6124. TAILQ_REMOVE(&wkq->que, qent, next);
  6125. SCTP_MCORE_QUNLOCK(wkq);
  6126. CURVNET_SET(qent->vn);
  6127. m = qent->m;
  6128. off = qent->off;
  6129. v6 = qent->v6;
  6130. SCTP_FREE(qent, SCTP_M_MCORE);
  6131. if (v6 == 0) {
  6132. sctp_input_with_port(m, off, 0);
  6133. } else {
  6134. SCTP_PRINTF("V6 not yet supported\n");
  6135. sctp_m_freem(m);
  6136. }
  6137. CURVNET_RESTORE();
  6138. SCTP_MCORE_QLOCK(wkq);
  6139. }
  6140. wkq->running = 0;
  6141. if (!TAILQ_EMPTY(&wkq->que)) {
  6142. goto skip_sleep;
  6143. }
  6144. SCTP_MCORE_QUNLOCK(wkq);
  6145. msleep(&wkq->running,
  6146. &wkq->core_mtx,
  6147. 0, "wait for pkt", 0);
  6148. }
  6149. }
  6150. static void
  6151. sctp_startup_mcore_threads(void)
  6152. {
  6153. int i, cpu;
  6154. if (mp_ncpus == 1)
  6155. return;
  6156. if (sctp_mcore_workers != NULL) {
  6157. /* Already been here in some previous
  6158. * vnet?
  6159. */
  6160. return;
  6161. }
  6162. SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
  6163. ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
  6164. SCTP_M_MCORE);
  6165. if (sctp_mcore_workers == NULL) {
  6166. /* TSNH I hope */
  6167. return;
  6168. }
  6169. memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
  6170. sizeof(struct sctp_mcore_ctrl)));
  6171. /* Init the structures */
  6172. for (i = 0; i<=mp_maxid; i++) {
  6173. TAILQ_INIT(&sctp_mcore_workers[i].que);
  6174. SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
  6175. SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
  6176. sctp_mcore_workers[i].cpuid = i;
  6177. }
  6178. if (sctp_cpuarry == NULL) {
  6179. SCTP_MALLOC(sctp_cpuarry, int *,
  6180. (mp_ncpus * sizeof(int)),
  6181. SCTP_M_MCORE);
  6182. i = 0;
  6183. CPU_FOREACH(cpu) {
  6184. sctp_cpuarry[i] = cpu;
  6185. i++;
  6186. }
  6187. }
  6188. /* Now start them all */
  6189. CPU_FOREACH(cpu) {
  6190. (void)kproc_create(sctp_mcore_thread,
  6191. (void *)&sctp_mcore_workers[cpu],
  6192. &sctp_mcore_workers[cpu].thread_proc,
  6193. 0,
  6194. SCTP_KTHREAD_PAGES,
  6195. SCTP_MCORE_NAME);
  6196. }
  6197. }
  6198. #endif
  6199. #endif
  6200. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6201. #if defined(SCTP_NOT_YET)
  6202. static struct mbuf *
  6203. sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
  6204. {
  6205. struct ip *ip;
  6206. struct sctphdr *sh;
  6207. int offset;
  6208. uint32_t flowid, tag;
  6209. /*
  6210. * No flow id built by lower layers fix it so we
  6211. * create one.
  6212. */
  6213. ip = mtod(m, struct ip *);
  6214. offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
  6215. if (SCTP_BUF_LEN(m) < offset) {
  6216. if ((m = m_pullup(m, offset)) == NULL) {
  6217. SCTP_STAT_INCR(sctps_hdrops);
  6218. return (NULL);
  6219. }
  6220. ip = mtod(m, struct ip *);
  6221. }
  6222. sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  6223. tag = htonl(sh->v_tag);
  6224. flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
  6225. m->m_pkthdr.flowid = flowid;
  6226. /* FIX ME */
  6227. m->m_flags |= M_FLOWID;
  6228. return (m);
  6229. }
  6230. #endif
  6231. #endif
  6232. void
  6233. #if defined(__Userspace__)
  6234. sctp_pcb_init(int start_threads)
  6235. #else
  6236. sctp_pcb_init(void)
  6237. #endif
  6238. {
  6239. /*
  6240. * SCTP initialization for the PCB structures should be called by
  6241. * the sctp_init() function.
  6242. */
  6243. int i;
  6244. struct timeval tv;
  6245. if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
  6246. /* error I was called twice */
  6247. return;
  6248. }
  6249. SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
  6250. #if defined(SCTP_PROCESS_LEVEL_LOCKS)
  6251. #if !defined(_WIN32)
  6252. pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
  6253. pthread_rwlockattr_init(&SCTP_BASE_VAR(rwlock_attr));
  6254. #ifdef INVARIANTS
  6255. pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
  6256. #endif
  6257. #endif
  6258. #endif
  6259. #if defined(SCTP_LOCAL_TRACE_BUF)
  6260. #if defined(_WIN32) && !defined(__Userspace__)
  6261. if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
  6262. memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
  6263. }
  6264. #else
  6265. memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
  6266. #endif
  6267. #endif
  6268. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6269. #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
  6270. SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
  6271. ((mp_maxid+1) * sizeof(struct sctpstat)),
  6272. SCTP_M_MCORE);
  6273. #endif
  6274. #endif
  6275. (void)SCTP_GETTIME_TIMEVAL(&tv);
  6276. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6277. #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
  6278. memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
  6279. SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
  6280. SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
  6281. #else
  6282. memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
  6283. SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
  6284. SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
  6285. #endif
  6286. #else
  6287. memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
  6288. SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
  6289. SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
  6290. #endif
  6291. /* init the empty list of (All) Endpoints */
  6292. LIST_INIT(&SCTP_BASE_INFO(listhead));
  6293. #if defined(__APPLE__) && !defined(__Userspace__)
  6294. LIST_INIT(&SCTP_BASE_INFO(inplisthead));
  6295. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
  6296. SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
  6297. SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
  6298. lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
  6299. SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
  6300. SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
  6301. lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
  6302. #else
  6303. SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
  6304. SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
  6305. lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
  6306. SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
  6307. SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
  6308. lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
  6309. #endif
  6310. #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
  6311. SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
  6312. in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
  6313. #endif
  6314. #endif
  6315. /* init the hash table of endpoints */
  6316. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6317. TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
  6318. TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
  6319. TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
  6320. #endif
  6321. SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
  6322. &SCTP_BASE_INFO(hashasocmark));
  6323. SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
  6324. &SCTP_BASE_INFO(hashmark));
  6325. SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
  6326. &SCTP_BASE_INFO(hashtcpmark));
  6327. SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
  6328. SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
  6329. &SCTP_BASE_INFO(hashvrfmark));
  6330. SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
  6331. &SCTP_BASE_INFO(vrf_ifn_hashmark));
  6332. /* init the zones */
  6333. /*
  6334. * FIX ME: Should check for NULL returns, but if it does fail we are
  6335. * doomed to panic anyways... add later maybe.
  6336. */
  6337. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
  6338. sizeof(struct sctp_inpcb), maxsockets);
  6339. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
  6340. sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
  6341. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
  6342. sizeof(struct sctp_laddr),
  6343. (sctp_max_number_of_assoc * sctp_scale_up_for_address));
  6344. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
  6345. sizeof(struct sctp_nets),
  6346. (sctp_max_number_of_assoc * sctp_scale_up_for_address));
  6347. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
  6348. sizeof(struct sctp_tmit_chunk),
  6349. (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
  6350. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
  6351. sizeof(struct sctp_queued_to_read),
  6352. (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
  6353. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
  6354. sizeof(struct sctp_stream_queue_pending),
  6355. (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
  6356. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
  6357. sizeof(struct sctp_asconf),
  6358. (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
  6359. SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
  6360. sizeof(struct sctp_asconf_ack),
  6361. (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
  6362. /* Master Lock INIT for info structure */
  6363. SCTP_INP_INFO_LOCK_INIT();
  6364. SCTP_STATLOG_INIT_LOCK();
  6365. SCTP_IPI_COUNT_INIT();
  6366. SCTP_IPI_ADDR_INIT();
  6367. #ifdef SCTP_PACKET_LOGGING
  6368. SCTP_IP_PKTLOG_INIT();
  6369. #endif
  6370. LIST_INIT(&SCTP_BASE_INFO(addr_wq));
  6371. SCTP_WQ_ADDR_INIT();
  6372. /* not sure if we need all the counts */
  6373. SCTP_BASE_INFO(ipi_count_ep) = 0;
  6374. /* assoc/tcb zone info */
  6375. SCTP_BASE_INFO(ipi_count_asoc) = 0;
  6376. /* local addrlist zone info */
  6377. SCTP_BASE_INFO(ipi_count_laddr) = 0;
  6378. /* remote addrlist zone info */
  6379. SCTP_BASE_INFO(ipi_count_raddr) = 0;
  6380. /* chunk info */
  6381. SCTP_BASE_INFO(ipi_count_chunk) = 0;
  6382. /* socket queue zone info */
  6383. SCTP_BASE_INFO(ipi_count_readq) = 0;
  6384. /* stream out queue cont */
  6385. SCTP_BASE_INFO(ipi_count_strmoq) = 0;
  6386. SCTP_BASE_INFO(ipi_free_strmoq) = 0;
  6387. SCTP_BASE_INFO(ipi_free_chunks) = 0;
  6388. SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
  6389. /* Init the TIMEWAIT list */
  6390. for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
  6391. LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
  6392. }
  6393. #if defined(SCTP_PROCESS_LEVEL_LOCKS)
  6394. #if defined(_WIN32)
  6395. InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
  6396. #else
  6397. (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
  6398. #endif
  6399. #endif
  6400. sctp_startup_iterator();
  6401. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6402. #if defined(SCTP_MCORE_INPUT) && defined(SMP)
  6403. sctp_startup_mcore_threads();
  6404. #endif
  6405. #endif
  6406. /*
  6407. * INIT the default VRF which for BSD is the only one, other O/S's
  6408. * may have more. But initially they must start with one and then
  6409. * add the VRF's as addresses are added.
  6410. */
  6411. sctp_init_vrf_list(SCTP_DEFAULT_VRF);
  6412. #if defined(__FreeBSD__) && !defined(__Userspace__) && defined(SCTP_NOT_YET)
  6413. if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
  6414. SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
  6415. }
  6416. #endif
  6417. #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
  6418. /* allocate the lock for the callout/timer queue */
  6419. SCTP_TIMERQ_LOCK_INIT();
  6420. TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
  6421. #endif
  6422. #if defined(__Userspace__)
  6423. mbuf_initialize(NULL);
  6424. atomic_init();
  6425. #if defined(INET) || defined(INET6)
  6426. if (start_threads)
  6427. recv_thread_init();
  6428. #endif
  6429. #endif
  6430. }
  6431. /*
  6432. * Assumes that the SCTP_BASE_INFO() lock is NOT held.
  6433. */
  6434. void
  6435. sctp_pcb_finish(void)
  6436. {
  6437. struct sctp_vrflist *vrf_bucket;
  6438. struct sctp_vrf *vrf, *nvrf;
  6439. struct sctp_ifn *ifn, *nifn;
  6440. struct sctp_ifa *ifa, *nifa;
  6441. struct sctpvtaghead *chain;
  6442. struct sctp_tagblock *twait_block, *prev_twait_block;
  6443. struct sctp_laddr *wi, *nwi;
  6444. int i;
  6445. struct sctp_iterator *it, *nit;
  6446. if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
  6447. SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
  6448. return;
  6449. }
  6450. SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
  6451. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  6452. /* Notify the iterator to exit. */
  6453. SCTP_IPI_ITERATOR_WQ_LOCK();
  6454. sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
  6455. sctp_wakeup_iterator();
  6456. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  6457. #endif
  6458. #if defined(__APPLE__) && !defined(__Userspace__)
  6459. #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
  6460. in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
  6461. #endif
  6462. SCTP_IPI_ITERATOR_WQ_LOCK();
  6463. do {
  6464. msleep(&sctp_it_ctl.iterator_flags,
  6465. sctp_it_ctl.ipi_iterator_wq_mtx,
  6466. 0, "waiting_for_work", 0);
  6467. } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
  6468. thread_deallocate(sctp_it_ctl.thread_proc);
  6469. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  6470. #endif
  6471. #if defined(_WIN32) && !defined(__Userspace__)
  6472. if (sctp_it_ctl.iterator_thread_obj != NULL) {
  6473. NTSTATUS status = STATUS_SUCCESS;
  6474. KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
  6475. status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
  6476. Executive,
  6477. KernelMode,
  6478. FALSE,
  6479. NULL);
  6480. ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
  6481. }
  6482. #endif
  6483. #if defined(__Userspace__)
  6484. if (SCTP_BASE_VAR(iterator_thread_started)) {
  6485. #if defined(_WIN32)
  6486. WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
  6487. CloseHandle(sctp_it_ctl.thread_proc);
  6488. sctp_it_ctl.thread_proc = NULL;
  6489. #else
  6490. pthread_join(sctp_it_ctl.thread_proc, NULL);
  6491. sctp_it_ctl.thread_proc = 0;
  6492. #endif
  6493. }
  6494. #endif
  6495. #if defined(SCTP_PROCESS_LEVEL_LOCKS)
  6496. #if defined(_WIN32)
  6497. DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
  6498. #else
  6499. pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
  6500. pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
  6501. pthread_rwlockattr_destroy(&SCTP_BASE_VAR(rwlock_attr));
  6502. #endif
  6503. #endif
  6504. /* In FreeBSD the iterator thread never exits
  6505. * but we do clean up.
  6506. * The only way FreeBSD reaches here is if we have VRF's
  6507. * but we still add the ifdef to make it compile on old versions.
  6508. */
  6509. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6510. retry:
  6511. #endif
  6512. SCTP_IPI_ITERATOR_WQ_LOCK();
  6513. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6514. /*
  6515. * sctp_iterator_worker() might be working on an it entry without
  6516. * holding the lock. We won't find it on the list either and
  6517. * continue and free/destroy it. While holding the lock, spin, to
  6518. * avoid the race condition as sctp_iterator_worker() will have to
  6519. * wait to re-acquire the lock.
  6520. */
  6521. if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
  6522. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  6523. SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
  6524. "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
  6525. DELAY(10);
  6526. goto retry;
  6527. }
  6528. #endif
  6529. TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
  6530. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6531. if (it->vn != curvnet) {
  6532. continue;
  6533. }
  6534. #endif
  6535. TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
  6536. if (it->function_atend != NULL) {
  6537. (*it->function_atend) (it->pointer, it->val);
  6538. }
  6539. SCTP_FREE(it,SCTP_M_ITER);
  6540. }
  6541. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  6542. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6543. SCTP_ITERATOR_LOCK();
  6544. if ((sctp_it_ctl.cur_it) &&
  6545. (sctp_it_ctl.cur_it->vn == curvnet)) {
  6546. sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
  6547. }
  6548. SCTP_ITERATOR_UNLOCK();
  6549. #endif
  6550. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  6551. SCTP_IPI_ITERATOR_WQ_DESTROY();
  6552. SCTP_ITERATOR_LOCK_DESTROY();
  6553. #endif
  6554. SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
  6555. SCTP_WQ_ADDR_LOCK();
  6556. LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
  6557. LIST_REMOVE(wi, sctp_nxt_addr);
  6558. SCTP_DECR_LADDR_COUNT();
  6559. if (wi->action == SCTP_DEL_IP_ADDRESS) {
  6560. SCTP_FREE(wi->ifa, SCTP_M_IFA);
  6561. }
  6562. SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
  6563. }
  6564. SCTP_WQ_ADDR_UNLOCK();
  6565. /*
  6566. * free the vrf/ifn/ifa lists and hashes (be sure address monitor
  6567. * is destroyed first).
  6568. */
  6569. SCTP_IPI_ADDR_WLOCK();
  6570. vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
  6571. LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
  6572. LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
  6573. LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
  6574. /* free the ifa */
  6575. LIST_REMOVE(ifa, next_bucket);
  6576. LIST_REMOVE(ifa, next_ifa);
  6577. SCTP_FREE(ifa, SCTP_M_IFA);
  6578. }
  6579. /* free the ifn */
  6580. LIST_REMOVE(ifn, next_bucket);
  6581. LIST_REMOVE(ifn, next_ifn);
  6582. SCTP_FREE(ifn, SCTP_M_IFN);
  6583. }
  6584. SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
  6585. /* free the vrf */
  6586. LIST_REMOVE(vrf, next_vrf);
  6587. SCTP_FREE(vrf, SCTP_M_VRF);
  6588. }
  6589. SCTP_IPI_ADDR_WUNLOCK();
  6590. /* free the vrf hashes */
  6591. SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
  6592. SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
  6593. /* free the TIMEWAIT list elements malloc'd in the function
  6594. * sctp_add_vtag_to_timewait()...
  6595. */
  6596. for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
  6597. chain = &SCTP_BASE_INFO(vtag_timewait)[i];
  6598. if (!LIST_EMPTY(chain)) {
  6599. prev_twait_block = NULL;
  6600. LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
  6601. if (prev_twait_block) {
  6602. SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
  6603. }
  6604. prev_twait_block = twait_block;
  6605. }
  6606. SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
  6607. }
  6608. }
  6609. /* free the locks and mutexes */
  6610. #if defined(__APPLE__) && !defined(__Userspace__)
  6611. SCTP_TIMERQ_LOCK_DESTROY();
  6612. #endif
  6613. #ifdef SCTP_PACKET_LOGGING
  6614. SCTP_IP_PKTLOG_DESTROY();
  6615. #endif
  6616. SCTP_IPI_ADDR_DESTROY();
  6617. #if defined(__APPLE__) && !defined(__Userspace__)
  6618. SCTP_IPI_COUNT_DESTROY();
  6619. #endif
  6620. SCTP_STATLOG_DESTROY();
  6621. SCTP_INP_INFO_LOCK_DESTROY();
  6622. SCTP_WQ_ADDR_DESTROY();
  6623. #if defined(__APPLE__) && !defined(__Userspace__)
  6624. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
  6625. lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
  6626. lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
  6627. lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
  6628. #else
  6629. lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
  6630. lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
  6631. lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
  6632. #endif
  6633. #endif
  6634. #if defined(__Userspace__)
  6635. SCTP_TIMERQ_LOCK_DESTROY();
  6636. SCTP_ZONE_DESTROY(zone_mbuf);
  6637. SCTP_ZONE_DESTROY(zone_clust);
  6638. SCTP_ZONE_DESTROY(zone_ext_refcnt);
  6639. #endif
  6640. /* Get rid of other stuff too. */
  6641. if (SCTP_BASE_INFO(sctp_asochash) != NULL)
  6642. SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
  6643. if (SCTP_BASE_INFO(sctp_ephash) != NULL)
  6644. SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
  6645. if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
  6646. SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
  6647. #if defined(_WIN32) || defined(__FreeBSD__) || defined(__Userspace__)
  6648. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
  6649. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
  6650. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
  6651. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
  6652. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
  6653. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
  6654. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
  6655. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
  6656. SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
  6657. #endif
  6658. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6659. #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
  6660. SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
  6661. #endif
  6662. #endif
  6663. }
  6664. int
  6665. sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
  6666. int offset, int limit,
  6667. struct sockaddr *src, struct sockaddr *dst,
  6668. struct sockaddr *altsa, uint16_t port)
  6669. {
  6670. /*
  6671. * grub through the INIT pulling addresses and loading them to the
  6672. * nets structure in the asoc. The from address in the mbuf should
  6673. * also be loaded (if it is not already). This routine can be called
  6674. * with either INIT or INIT-ACK's as long as the m points to the IP
  6675. * packet and the offset points to the beginning of the parameters.
  6676. */
  6677. struct sctp_inpcb *inp;
  6678. struct sctp_nets *net, *nnet, *net_tmp;
  6679. struct sctp_paramhdr *phdr, param_buf;
  6680. struct sctp_tcb *stcb_tmp;
  6681. uint16_t ptype, plen;
  6682. struct sockaddr *sa;
  6683. uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
  6684. struct sctp_auth_random *p_random = NULL;
  6685. uint16_t random_len = 0;
  6686. uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
  6687. struct sctp_auth_hmac_algo *hmacs = NULL;
  6688. uint16_t hmacs_len = 0;
  6689. uint8_t saw_asconf = 0;
  6690. uint8_t saw_asconf_ack = 0;
  6691. uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
  6692. struct sctp_auth_chunk_list *chunks = NULL;
  6693. uint16_t num_chunks = 0;
  6694. sctp_key_t *new_key;
  6695. uint32_t keylen;
  6696. int got_random = 0, got_hmacs = 0, got_chklist = 0;
  6697. uint8_t peer_supports_ecn;
  6698. uint8_t peer_supports_prsctp;
  6699. uint8_t peer_supports_auth;
  6700. uint8_t peer_supports_asconf;
  6701. uint8_t peer_supports_asconf_ack;
  6702. uint8_t peer_supports_reconfig;
  6703. uint8_t peer_supports_nrsack;
  6704. uint8_t peer_supports_pktdrop;
  6705. uint8_t peer_supports_idata;
  6706. #ifdef INET
  6707. struct sockaddr_in sin;
  6708. #endif
  6709. #ifdef INET6
  6710. struct sockaddr_in6 sin6;
  6711. #endif
  6712. /* First get the destination address setup too. */
  6713. #ifdef INET
  6714. memset(&sin, 0, sizeof(sin));
  6715. sin.sin_family = AF_INET;
  6716. #ifdef HAVE_SIN_LEN
  6717. sin.sin_len = sizeof(sin);
  6718. #endif
  6719. sin.sin_port = stcb->rport;
  6720. #endif
  6721. #ifdef INET6
  6722. memset(&sin6, 0, sizeof(sin6));
  6723. sin6.sin6_family = AF_INET6;
  6724. #ifdef HAVE_SIN6_LEN
  6725. sin6.sin6_len = sizeof(struct sockaddr_in6);
  6726. #endif
  6727. sin6.sin6_port = stcb->rport;
  6728. #endif
  6729. if (altsa) {
  6730. sa = altsa;
  6731. } else {
  6732. sa = src;
  6733. }
  6734. peer_supports_idata = 0;
  6735. peer_supports_ecn = 0;
  6736. peer_supports_prsctp = 0;
  6737. peer_supports_auth = 0;
  6738. peer_supports_asconf = 0;
  6739. peer_supports_asconf_ack = 0;
  6740. peer_supports_reconfig = 0;
  6741. peer_supports_nrsack = 0;
  6742. peer_supports_pktdrop = 0;
  6743. TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
  6744. /* mark all addresses that we have currently on the list */
  6745. net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
  6746. }
  6747. /* does the source address already exist? if so skip it */
  6748. inp = stcb->sctp_ep;
  6749. atomic_add_int(&stcb->asoc.refcnt, 1);
  6750. stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
  6751. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  6752. if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
  6753. /* we must add the source address */
  6754. /* no scope set here since we have a tcb already. */
  6755. switch (sa->sa_family) {
  6756. #ifdef INET
  6757. case AF_INET:
  6758. if (stcb->asoc.scope.ipv4_addr_legal) {
  6759. if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
  6760. return (-1);
  6761. }
  6762. }
  6763. break;
  6764. #endif
  6765. #ifdef INET6
  6766. case AF_INET6:
  6767. if (stcb->asoc.scope.ipv6_addr_legal) {
  6768. if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
  6769. return (-2);
  6770. }
  6771. }
  6772. break;
  6773. #endif
  6774. #if defined(__Userspace__)
  6775. case AF_CONN:
  6776. if (stcb->asoc.scope.conn_addr_legal) {
  6777. if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
  6778. return (-2);
  6779. }
  6780. }
  6781. break;
  6782. #endif
  6783. default:
  6784. break;
  6785. }
  6786. } else {
  6787. if (net_tmp != NULL && stcb_tmp == stcb) {
  6788. net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
  6789. } else if (stcb_tmp != stcb) {
  6790. /* It belongs to another association? */
  6791. if (stcb_tmp)
  6792. SCTP_TCB_UNLOCK(stcb_tmp);
  6793. return (-3);
  6794. }
  6795. }
  6796. if (stcb->asoc.state == 0) {
  6797. /* the assoc was freed? */
  6798. return (-4);
  6799. }
  6800. /* now we must go through each of the params. */
  6801. phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
  6802. while (phdr) {
  6803. ptype = ntohs(phdr->param_type);
  6804. plen = ntohs(phdr->param_length);
  6805. /*
  6806. * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
  6807. * (int)plen);
  6808. */
  6809. if (offset + plen > limit) {
  6810. break;
  6811. }
  6812. if (plen < sizeof(struct sctp_paramhdr)) {
  6813. break;
  6814. }
  6815. #ifdef INET
  6816. if (ptype == SCTP_IPV4_ADDRESS) {
  6817. if (stcb->asoc.scope.ipv4_addr_legal) {
  6818. struct sctp_ipv4addr_param *p4, p4_buf;
  6819. /* ok get the v4 address and check/add */
  6820. phdr = sctp_get_next_param(m, offset,
  6821. (struct sctp_paramhdr *)&p4_buf,
  6822. sizeof(p4_buf));
  6823. if (plen != sizeof(struct sctp_ipv4addr_param) ||
  6824. phdr == NULL) {
  6825. return (-5);
  6826. }
  6827. p4 = (struct sctp_ipv4addr_param *)phdr;
  6828. sin.sin_addr.s_addr = p4->addr;
  6829. if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
  6830. /* Skip multi-cast addresses */
  6831. goto next_param;
  6832. }
  6833. if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
  6834. (sin.sin_addr.s_addr == INADDR_ANY)) {
  6835. goto next_param;
  6836. }
  6837. sa = (struct sockaddr *)&sin;
  6838. inp = stcb->sctp_ep;
  6839. atomic_add_int(&stcb->asoc.refcnt, 1);
  6840. stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
  6841. dst, stcb);
  6842. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  6843. if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
  6844. inp == NULL) {
  6845. /* we must add the source address */
  6846. /*
  6847. * no scope set since we have a tcb
  6848. * already
  6849. */
  6850. /*
  6851. * we must validate the state again
  6852. * here
  6853. */
  6854. add_it_now:
  6855. if (stcb->asoc.state == 0) {
  6856. /* the assoc was freed? */
  6857. return (-7);
  6858. }
  6859. if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
  6860. return (-8);
  6861. }
  6862. } else if (stcb_tmp == stcb) {
  6863. if (stcb->asoc.state == 0) {
  6864. /* the assoc was freed? */
  6865. return (-10);
  6866. }
  6867. if (net != NULL) {
  6868. /* clear flag */
  6869. net->dest_state &=
  6870. ~SCTP_ADDR_NOT_IN_ASSOC;
  6871. }
  6872. } else {
  6873. /*
  6874. * strange, address is in another
  6875. * assoc? straighten out locks.
  6876. */
  6877. if (stcb_tmp) {
  6878. if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
  6879. struct mbuf *op_err;
  6880. char msg[SCTP_DIAG_INFO_LEN];
  6881. /* in setup state we abort this guy */
  6882. SCTP_SNPRINTF(msg, sizeof(msg),
  6883. "%s:%d at %s", __FILE__, __LINE__, __func__);
  6884. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  6885. msg);
  6886. sctp_abort_an_association(stcb_tmp->sctp_ep,
  6887. stcb_tmp, op_err, false,
  6888. SCTP_SO_NOT_LOCKED);
  6889. goto add_it_now;
  6890. }
  6891. SCTP_TCB_UNLOCK(stcb_tmp);
  6892. }
  6893. if (stcb->asoc.state == 0) {
  6894. /* the assoc was freed? */
  6895. return (-12);
  6896. }
  6897. return (-13);
  6898. }
  6899. }
  6900. } else
  6901. #endif
  6902. #ifdef INET6
  6903. if (ptype == SCTP_IPV6_ADDRESS) {
  6904. if (stcb->asoc.scope.ipv6_addr_legal) {
  6905. /* ok get the v6 address and check/add */
  6906. struct sctp_ipv6addr_param *p6, p6_buf;
  6907. phdr = sctp_get_next_param(m, offset,
  6908. (struct sctp_paramhdr *)&p6_buf,
  6909. sizeof(p6_buf));
  6910. if (plen != sizeof(struct sctp_ipv6addr_param) ||
  6911. phdr == NULL) {
  6912. return (-14);
  6913. }
  6914. p6 = (struct sctp_ipv6addr_param *)phdr;
  6915. memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
  6916. sizeof(p6->addr));
  6917. if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
  6918. /* Skip multi-cast addresses */
  6919. goto next_param;
  6920. }
  6921. if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
  6922. /* Link local make no sense without scope */
  6923. goto next_param;
  6924. }
  6925. sa = (struct sockaddr *)&sin6;
  6926. inp = stcb->sctp_ep;
  6927. atomic_add_int(&stcb->asoc.refcnt, 1);
  6928. stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
  6929. dst, stcb);
  6930. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  6931. if (stcb_tmp == NULL &&
  6932. (inp == stcb->sctp_ep || inp == NULL)) {
  6933. /*
  6934. * we must validate the state again
  6935. * here
  6936. */
  6937. add_it_now6:
  6938. if (stcb->asoc.state == 0) {
  6939. /* the assoc was freed? */
  6940. return (-16);
  6941. }
  6942. /*
  6943. * we must add the address, no scope
  6944. * set
  6945. */
  6946. if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
  6947. return (-17);
  6948. }
  6949. } else if (stcb_tmp == stcb) {
  6950. /*
  6951. * we must validate the state again
  6952. * here
  6953. */
  6954. if (stcb->asoc.state == 0) {
  6955. /* the assoc was freed? */
  6956. return (-19);
  6957. }
  6958. if (net != NULL) {
  6959. /* clear flag */
  6960. net->dest_state &=
  6961. ~SCTP_ADDR_NOT_IN_ASSOC;
  6962. }
  6963. } else {
  6964. /*
  6965. * strange, address is in another
  6966. * assoc? straighten out locks.
  6967. */
  6968. if (stcb_tmp) {
  6969. if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
  6970. struct mbuf *op_err;
  6971. char msg[SCTP_DIAG_INFO_LEN];
  6972. /* in setup state we abort this guy */
  6973. SCTP_SNPRINTF(msg, sizeof(msg),
  6974. "%s:%d at %s", __FILE__, __LINE__, __func__);
  6975. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  6976. msg);
  6977. sctp_abort_an_association(stcb_tmp->sctp_ep,
  6978. stcb_tmp, op_err, false,
  6979. SCTP_SO_NOT_LOCKED);
  6980. goto add_it_now6;
  6981. }
  6982. SCTP_TCB_UNLOCK(stcb_tmp);
  6983. }
  6984. if (stcb->asoc.state == 0) {
  6985. /* the assoc was freed? */
  6986. return (-21);
  6987. }
  6988. return (-22);
  6989. }
  6990. }
  6991. } else
  6992. #endif
  6993. if (ptype == SCTP_ECN_CAPABLE) {
  6994. peer_supports_ecn = 1;
  6995. } else if (ptype == SCTP_ULP_ADAPTATION) {
  6996. if (stcb->asoc.state != SCTP_STATE_OPEN) {
  6997. struct sctp_adaptation_layer_indication ai, *aip;
  6998. phdr = sctp_get_next_param(m, offset,
  6999. (struct sctp_paramhdr *)&ai, sizeof(ai));
  7000. aip = (struct sctp_adaptation_layer_indication *)phdr;
  7001. if (aip) {
  7002. stcb->asoc.peers_adaptation = ntohl(aip->indication);
  7003. stcb->asoc.adaptation_needed = 1;
  7004. }
  7005. }
  7006. } else if (ptype == SCTP_SET_PRIM_ADDR) {
  7007. struct sctp_asconf_addr_param lstore, *fee;
  7008. int lptype;
  7009. struct sockaddr *lsa = NULL;
  7010. #ifdef INET
  7011. struct sctp_asconf_addrv4_param *fii;
  7012. #endif
  7013. if (stcb->asoc.asconf_supported == 0) {
  7014. return (-100);
  7015. }
  7016. if (plen > sizeof(lstore)) {
  7017. return (-23);
  7018. }
  7019. if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
  7020. return (-101);
  7021. }
  7022. phdr = sctp_get_next_param(m, offset,
  7023. (struct sctp_paramhdr *)&lstore,
  7024. plen);
  7025. if (phdr == NULL) {
  7026. return (-24);
  7027. }
  7028. fee = (struct sctp_asconf_addr_param *)phdr;
  7029. lptype = ntohs(fee->addrp.ph.param_type);
  7030. switch (lptype) {
  7031. #ifdef INET
  7032. case SCTP_IPV4_ADDRESS:
  7033. if (plen !=
  7034. sizeof(struct sctp_asconf_addrv4_param)) {
  7035. SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
  7036. (int)sizeof(struct sctp_asconf_addrv4_param),
  7037. plen);
  7038. } else {
  7039. fii = (struct sctp_asconf_addrv4_param *)fee;
  7040. sin.sin_addr.s_addr = fii->addrp.addr;
  7041. lsa = (struct sockaddr *)&sin;
  7042. }
  7043. break;
  7044. #endif
  7045. #ifdef INET6
  7046. case SCTP_IPV6_ADDRESS:
  7047. if (plen !=
  7048. sizeof(struct sctp_asconf_addr_param)) {
  7049. SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
  7050. (int)sizeof(struct sctp_asconf_addr_param),
  7051. plen);
  7052. } else {
  7053. memcpy(sin6.sin6_addr.s6_addr,
  7054. fee->addrp.addr,
  7055. sizeof(fee->addrp.addr));
  7056. lsa = (struct sockaddr *)&sin6;
  7057. }
  7058. break;
  7059. #endif
  7060. default:
  7061. break;
  7062. }
  7063. if (lsa) {
  7064. (void)sctp_set_primary_addr(stcb, sa, NULL);
  7065. }
  7066. } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
  7067. stcb->asoc.peer_supports_nat = 1;
  7068. } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
  7069. /* Peer supports pr-sctp */
  7070. peer_supports_prsctp = 1;
  7071. } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
  7072. /* A supported extension chunk */
  7073. struct sctp_supported_chunk_types_param *pr_supported;
  7074. uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
  7075. int num_ent, i;
  7076. if (plen > sizeof(local_store)) {
  7077. return (-35);
  7078. }
  7079. phdr = sctp_get_next_param(m, offset,
  7080. (struct sctp_paramhdr *)&local_store, plen);
  7081. if (phdr == NULL) {
  7082. return (-25);
  7083. }
  7084. pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
  7085. num_ent = plen - sizeof(struct sctp_paramhdr);
  7086. for (i = 0; i < num_ent; i++) {
  7087. switch (pr_supported->chunk_types[i]) {
  7088. case SCTP_ASCONF:
  7089. peer_supports_asconf = 1;
  7090. break;
  7091. case SCTP_ASCONF_ACK:
  7092. peer_supports_asconf_ack = 1;
  7093. break;
  7094. case SCTP_FORWARD_CUM_TSN:
  7095. peer_supports_prsctp = 1;
  7096. break;
  7097. case SCTP_PACKET_DROPPED:
  7098. peer_supports_pktdrop = 1;
  7099. break;
  7100. case SCTP_NR_SELECTIVE_ACK:
  7101. peer_supports_nrsack = 1;
  7102. break;
  7103. case SCTP_STREAM_RESET:
  7104. peer_supports_reconfig = 1;
  7105. break;
  7106. case SCTP_AUTHENTICATION:
  7107. peer_supports_auth = 1;
  7108. break;
  7109. case SCTP_IDATA:
  7110. peer_supports_idata = 1;
  7111. break;
  7112. default:
  7113. /* one I have not learned yet */
  7114. break;
  7115. }
  7116. }
  7117. } else if (ptype == SCTP_RANDOM) {
  7118. if (plen > sizeof(random_store))
  7119. break;
  7120. if (got_random) {
  7121. /* already processed a RANDOM */
  7122. goto next_param;
  7123. }
  7124. phdr = sctp_get_next_param(m, offset,
  7125. (struct sctp_paramhdr *)random_store,
  7126. plen);
  7127. if (phdr == NULL)
  7128. return (-26);
  7129. p_random = (struct sctp_auth_random *)phdr;
  7130. random_len = plen - sizeof(*p_random);
  7131. /* enforce the random length */
  7132. if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
  7133. SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
  7134. return (-27);
  7135. }
  7136. got_random = 1;
  7137. } else if (ptype == SCTP_HMAC_LIST) {
  7138. uint16_t num_hmacs;
  7139. uint16_t i;
  7140. if (plen > sizeof(hmacs_store))
  7141. break;
  7142. if (got_hmacs) {
  7143. /* already processed a HMAC list */
  7144. goto next_param;
  7145. }
  7146. phdr = sctp_get_next_param(m, offset,
  7147. (struct sctp_paramhdr *)hmacs_store,
  7148. plen);
  7149. if (phdr == NULL)
  7150. return (-28);
  7151. hmacs = (struct sctp_auth_hmac_algo *)phdr;
  7152. hmacs_len = plen - sizeof(*hmacs);
  7153. num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
  7154. /* validate the hmac list */
  7155. if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
  7156. return (-29);
  7157. }
  7158. if (stcb->asoc.peer_hmacs != NULL)
  7159. sctp_free_hmaclist(stcb->asoc.peer_hmacs);
  7160. stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
  7161. if (stcb->asoc.peer_hmacs != NULL) {
  7162. for (i = 0; i < num_hmacs; i++) {
  7163. (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
  7164. ntohs(hmacs->hmac_ids[i]));
  7165. }
  7166. }
  7167. got_hmacs = 1;
  7168. } else if (ptype == SCTP_CHUNK_LIST) {
  7169. int i;
  7170. if (plen > sizeof(chunks_store))
  7171. break;
  7172. if (got_chklist) {
  7173. /* already processed a Chunks list */
  7174. goto next_param;
  7175. }
  7176. phdr = sctp_get_next_param(m, offset,
  7177. (struct sctp_paramhdr *)chunks_store,
  7178. plen);
  7179. if (phdr == NULL)
  7180. return (-30);
  7181. chunks = (struct sctp_auth_chunk_list *)phdr;
  7182. num_chunks = plen - sizeof(*chunks);
  7183. if (stcb->asoc.peer_auth_chunks != NULL)
  7184. sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
  7185. else
  7186. stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
  7187. for (i = 0; i < num_chunks; i++) {
  7188. (void)sctp_auth_add_chunk(chunks->chunk_types[i],
  7189. stcb->asoc.peer_auth_chunks);
  7190. /* record asconf/asconf-ack if listed */
  7191. if (chunks->chunk_types[i] == SCTP_ASCONF)
  7192. saw_asconf = 1;
  7193. if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
  7194. saw_asconf_ack = 1;
  7195. }
  7196. got_chklist = 1;
  7197. } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
  7198. (ptype == SCTP_STATE_COOKIE) ||
  7199. (ptype == SCTP_UNRECOG_PARAM) ||
  7200. (ptype == SCTP_COOKIE_PRESERVE) ||
  7201. (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
  7202. (ptype == SCTP_ADD_IP_ADDRESS) ||
  7203. (ptype == SCTP_DEL_IP_ADDRESS) ||
  7204. (ptype == SCTP_ERROR_CAUSE_IND) ||
  7205. (ptype == SCTP_SUCCESS_REPORT)) {
  7206. /* don't care */
  7207. } else {
  7208. if ((ptype & 0x8000) == 0x0000) {
  7209. /*
  7210. * must stop processing the rest of the
  7211. * param's. Any report bits were handled
  7212. * with the call to
  7213. * sctp_arethere_unrecognized_parameters()
  7214. * when the INIT or INIT-ACK was first seen.
  7215. */
  7216. break;
  7217. }
  7218. }
  7219. next_param:
  7220. offset += SCTP_SIZE32(plen);
  7221. if (offset >= limit) {
  7222. break;
  7223. }
  7224. phdr = sctp_get_next_param(m, offset, &param_buf,
  7225. sizeof(param_buf));
  7226. }
  7227. /* Now check to see if we need to purge any addresses */
  7228. TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
  7229. if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
  7230. SCTP_ADDR_NOT_IN_ASSOC) {
  7231. /* This address has been removed from the asoc */
  7232. /* remove and free it */
  7233. stcb->asoc.numnets--;
  7234. TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
  7235. if (net == stcb->asoc.alternate) {
  7236. sctp_free_remote_addr(stcb->asoc.alternate);
  7237. stcb->asoc.alternate = NULL;
  7238. }
  7239. if (net == stcb->asoc.primary_destination) {
  7240. stcb->asoc.primary_destination = NULL;
  7241. sctp_select_primary_destination(stcb);
  7242. }
  7243. sctp_free_remote_addr(net);
  7244. }
  7245. }
  7246. if ((stcb->asoc.ecn_supported == 1) &&
  7247. (peer_supports_ecn == 0)) {
  7248. stcb->asoc.ecn_supported = 0;
  7249. }
  7250. if ((stcb->asoc.prsctp_supported == 1) &&
  7251. (peer_supports_prsctp == 0)) {
  7252. stcb->asoc.prsctp_supported = 0;
  7253. }
  7254. if ((stcb->asoc.auth_supported == 1) &&
  7255. ((peer_supports_auth == 0) ||
  7256. (got_random == 0) || (got_hmacs == 0))) {
  7257. stcb->asoc.auth_supported = 0;
  7258. }
  7259. if ((stcb->asoc.asconf_supported == 1) &&
  7260. ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
  7261. (stcb->asoc.auth_supported == 0) ||
  7262. (saw_asconf == 0) || (saw_asconf_ack == 0))) {
  7263. stcb->asoc.asconf_supported = 0;
  7264. }
  7265. if ((stcb->asoc.reconfig_supported == 1) &&
  7266. (peer_supports_reconfig == 0)) {
  7267. stcb->asoc.reconfig_supported = 0;
  7268. }
  7269. if ((stcb->asoc.idata_supported == 1) &&
  7270. (peer_supports_idata == 0)) {
  7271. stcb->asoc.idata_supported = 0;
  7272. }
  7273. if ((stcb->asoc.nrsack_supported == 1) &&
  7274. (peer_supports_nrsack == 0)) {
  7275. stcb->asoc.nrsack_supported = 0;
  7276. }
  7277. if ((stcb->asoc.pktdrop_supported == 1) &&
  7278. (peer_supports_pktdrop == 0)) {
  7279. stcb->asoc.pktdrop_supported = 0;
  7280. }
  7281. /* validate authentication required parameters */
  7282. if ((peer_supports_auth == 0) && (got_chklist == 1)) {
  7283. /* peer does not support auth but sent a chunks list? */
  7284. return (-31);
  7285. }
  7286. if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
  7287. /* peer supports asconf but not auth? */
  7288. return (-32);
  7289. } else if ((peer_supports_asconf == 1) &&
  7290. (peer_supports_auth == 1) &&
  7291. ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
  7292. return (-33);
  7293. }
  7294. /* concatenate the full random key */
  7295. keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
  7296. if (chunks != NULL) {
  7297. keylen += sizeof(*chunks) + num_chunks;
  7298. }
  7299. new_key = sctp_alloc_key(keylen);
  7300. if (new_key != NULL) {
  7301. /* copy in the RANDOM */
  7302. if (p_random != NULL) {
  7303. keylen = sizeof(*p_random) + random_len;
  7304. memcpy(new_key->key, p_random, keylen);
  7305. } else {
  7306. keylen = 0;
  7307. }
  7308. /* append in the AUTH chunks */
  7309. if (chunks != NULL) {
  7310. memcpy(new_key->key + keylen, chunks,
  7311. sizeof(*chunks) + num_chunks);
  7312. keylen += sizeof(*chunks) + num_chunks;
  7313. }
  7314. /* append in the HMACs */
  7315. if (hmacs != NULL) {
  7316. memcpy(new_key->key + keylen, hmacs,
  7317. sizeof(*hmacs) + hmacs_len);
  7318. }
  7319. } else {
  7320. /* failed to get memory for the key */
  7321. return (-34);
  7322. }
  7323. if (stcb->asoc.authinfo.peer_random != NULL)
  7324. sctp_free_key(stcb->asoc.authinfo.peer_random);
  7325. stcb->asoc.authinfo.peer_random = new_key;
  7326. sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
  7327. sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
  7328. return (0);
  7329. }
  7330. int
  7331. sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
  7332. struct sctp_nets *net)
  7333. {
  7334. /* make sure the requested primary address exists in the assoc */
  7335. if (net == NULL && sa)
  7336. net = sctp_findnet(stcb, sa);
  7337. if (net == NULL) {
  7338. /* didn't find the requested primary address! */
  7339. return (-1);
  7340. } else {
  7341. /* set the primary address */
  7342. if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
  7343. /* Must be confirmed, so queue to set */
  7344. net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
  7345. return (0);
  7346. }
  7347. stcb->asoc.primary_destination = net;
  7348. if (((net->dest_state & SCTP_ADDR_PF) == 0) &&
  7349. (stcb->asoc.alternate != NULL)) {
  7350. sctp_free_remote_addr(stcb->asoc.alternate);
  7351. stcb->asoc.alternate = NULL;
  7352. }
  7353. net = TAILQ_FIRST(&stcb->asoc.nets);
  7354. if (net != stcb->asoc.primary_destination) {
  7355. /* first one on the list is NOT the primary
  7356. * sctp_cmpaddr() is much more efficient if
  7357. * the primary is the first on the list, make it
  7358. * so.
  7359. */
  7360. TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
  7361. TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
  7362. }
  7363. return (0);
  7364. }
  7365. }
  7366. bool
  7367. sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
  7368. {
  7369. struct sctpasochead *head;
  7370. struct sctp_tcb *stcb;
  7371. SCTP_INP_INFO_LOCK_ASSERT();
  7372. head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))];
  7373. LIST_FOREACH(stcb, head, sctp_asocs) {
  7374. /* We choose not to lock anything here. TCB's can't be
  7375. * removed since we have the read lock, so they can't
  7376. * be freed on us, same thing for the INP. I may
  7377. * be wrong with this assumption, but we will go
  7378. * with it for now :-)
  7379. */
  7380. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
  7381. continue;
  7382. }
  7383. if (stcb->asoc.my_vtag == tag) {
  7384. /* candidate */
  7385. if (stcb->rport != rport) {
  7386. continue;
  7387. }
  7388. if (stcb->sctp_ep->sctp_lport != lport) {
  7389. continue;
  7390. }
  7391. /* The tag is currently used, so don't use it. */
  7392. return (false);
  7393. }
  7394. }
  7395. return (!sctp_is_in_timewait(tag, lport, rport, (uint32_t)now->tv_sec));
  7396. }
  7397. static void
  7398. sctp_drain_mbufs(struct sctp_tcb *stcb)
  7399. {
  7400. /*
  7401. * We must hunt this association for MBUF's past the cumack (i.e.
  7402. * out of order data that we can renege on).
  7403. */
  7404. struct sctp_association *asoc;
  7405. struct sctp_tmit_chunk *chk, *nchk;
  7406. uint32_t cumulative_tsn_p1;
  7407. struct sctp_queued_to_read *control, *ncontrol;
  7408. int cnt, strmat;
  7409. uint32_t gap, i;
  7410. int fnd = 0;
  7411. /* We look for anything larger than the cum-ack + 1 */
  7412. asoc = &stcb->asoc;
  7413. if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
  7414. /* none we can reneg on. */
  7415. return;
  7416. }
  7417. SCTP_STAT_INCR(sctps_protocol_drains_done);
  7418. cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
  7419. cnt = 0;
  7420. /* Ok that was fun, now we will drain all the inbound streams? */
  7421. for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
  7422. TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
  7423. #ifdef INVARIANTS
  7424. if (control->on_strm_q != SCTP_ON_ORDERED) {
  7425. panic("Huh control: %p on_q: %d -- not ordered?",
  7426. control, control->on_strm_q);
  7427. }
  7428. #endif
  7429. if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
  7430. /* Yep it is above cum-ack */
  7431. cnt++;
  7432. SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
  7433. KASSERT(control->length > 0, ("control has zero length"));
  7434. if (asoc->size_on_all_streams >= control->length) {
  7435. asoc->size_on_all_streams -= control->length;
  7436. } else {
  7437. #ifdef INVARIANTS
  7438. panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
  7439. #else
  7440. asoc->size_on_all_streams = 0;
  7441. #endif
  7442. }
  7443. sctp_ucount_decr(asoc->cnt_on_all_streams);
  7444. SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
  7445. if (control->on_read_q) {
  7446. TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
  7447. control->on_read_q = 0;
  7448. }
  7449. TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
  7450. control->on_strm_q = 0;
  7451. if (control->data) {
  7452. sctp_m_freem(control->data);
  7453. control->data = NULL;
  7454. }
  7455. sctp_free_remote_addr(control->whoFrom);
  7456. /* Now its reasm? */
  7457. TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
  7458. cnt++;
  7459. SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
  7460. KASSERT(chk->send_size > 0, ("chunk has zero length"));
  7461. if (asoc->size_on_reasm_queue >= chk->send_size) {
  7462. asoc->size_on_reasm_queue -= chk->send_size;
  7463. } else {
  7464. #ifdef INVARIANTS
  7465. panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
  7466. #else
  7467. asoc->size_on_reasm_queue = 0;
  7468. #endif
  7469. }
  7470. sctp_ucount_decr(asoc->cnt_on_reasm_queue);
  7471. SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
  7472. TAILQ_REMOVE(&control->reasm, chk, sctp_next);
  7473. if (chk->data) {
  7474. sctp_m_freem(chk->data);
  7475. chk->data = NULL;
  7476. }
  7477. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  7478. }
  7479. sctp_free_a_readq(stcb, control);
  7480. }
  7481. }
  7482. TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
  7483. #ifdef INVARIANTS
  7484. if (control->on_strm_q != SCTP_ON_UNORDERED) {
  7485. panic("Huh control: %p on_q: %d -- not unordered?",
  7486. control, control->on_strm_q);
  7487. }
  7488. #endif
  7489. if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
  7490. /* Yep it is above cum-ack */
  7491. cnt++;
  7492. SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
  7493. KASSERT(control->length > 0, ("control has zero length"));
  7494. if (asoc->size_on_all_streams >= control->length) {
  7495. asoc->size_on_all_streams -= control->length;
  7496. } else {
  7497. #ifdef INVARIANTS
  7498. panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
  7499. #else
  7500. asoc->size_on_all_streams = 0;
  7501. #endif
  7502. }
  7503. sctp_ucount_decr(asoc->cnt_on_all_streams);
  7504. SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
  7505. if (control->on_read_q) {
  7506. TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
  7507. control->on_read_q = 0;
  7508. }
  7509. TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
  7510. control->on_strm_q = 0;
  7511. if (control->data) {
  7512. sctp_m_freem(control->data);
  7513. control->data = NULL;
  7514. }
  7515. sctp_free_remote_addr(control->whoFrom);
  7516. /* Now its reasm? */
  7517. TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
  7518. cnt++;
  7519. SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
  7520. KASSERT(chk->send_size > 0, ("chunk has zero length"));
  7521. if (asoc->size_on_reasm_queue >= chk->send_size) {
  7522. asoc->size_on_reasm_queue -= chk->send_size;
  7523. } else {
  7524. #ifdef INVARIANTS
  7525. panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
  7526. #else
  7527. asoc->size_on_reasm_queue = 0;
  7528. #endif
  7529. }
  7530. sctp_ucount_decr(asoc->cnt_on_reasm_queue);
  7531. SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
  7532. TAILQ_REMOVE(&control->reasm, chk, sctp_next);
  7533. if (chk->data) {
  7534. sctp_m_freem(chk->data);
  7535. chk->data = NULL;
  7536. }
  7537. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  7538. }
  7539. sctp_free_a_readq(stcb, control);
  7540. }
  7541. }
  7542. }
  7543. if (cnt) {
  7544. /* We must back down to see what the new highest is */
  7545. for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
  7546. SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
  7547. if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
  7548. asoc->highest_tsn_inside_map = i;
  7549. fnd = 1;
  7550. break;
  7551. }
  7552. }
  7553. if (!fnd) {
  7554. asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
  7555. }
  7556. /*
  7557. * Question, should we go through the delivery queue? The only
  7558. * reason things are on here is the app not reading OR a p-d-api up.
  7559. * An attacker COULD send enough in to initiate the PD-API and then
  7560. * send a bunch of stuff to other streams... these would wind up on
  7561. * the delivery queue.. and then we would not get to them. But in
  7562. * order to do this I then have to back-track and un-deliver
  7563. * sequence numbers in streams.. el-yucko. I think for now we will
  7564. * NOT look at the delivery queue and leave it to be something to
  7565. * consider later. An alternative would be to abort the P-D-API with
  7566. * a notification and then deliver the data.... Or another method
  7567. * might be to keep track of how many times the situation occurs and
  7568. * if we see a possible attack underway just abort the association.
  7569. */
  7570. #ifdef SCTP_DEBUG
  7571. SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
  7572. #endif
  7573. /*
  7574. * Now do we need to find a new
  7575. * asoc->highest_tsn_inside_map?
  7576. */
  7577. asoc->last_revoke_count = cnt;
  7578. sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
  7579. SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
  7580. /*sa_ignore NO_NULL_CHK*/
  7581. sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
  7582. sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
  7583. }
  7584. /*
  7585. * Another issue, in un-setting the TSN's in the mapping array we
  7586. * DID NOT adjust the highest_tsn marker. This will cause one of two
  7587. * things to occur. It may cause us to do extra work in checking for
  7588. * our mapping array movement. More importantly it may cause us to
  7589. * SACK every datagram. This may not be a bad thing though since we
  7590. * will recover once we get our cum-ack above and all this stuff we
  7591. * dumped recovered.
  7592. */
  7593. }
  7594. void
  7595. sctp_drain(void)
  7596. {
  7597. /*
  7598. * We must walk the PCB lists for ALL associations here. The system
  7599. * is LOW on MBUF's and needs help. This is where reneging will
  7600. * occur. We really hope this does NOT happen!
  7601. */
  7602. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7603. VNET_ITERATOR_DECL(vnet_iter);
  7604. #else
  7605. struct sctp_inpcb *inp;
  7606. struct sctp_tcb *stcb;
  7607. SCTP_STAT_INCR(sctps_protocol_drain_calls);
  7608. if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
  7609. return;
  7610. }
  7611. #endif
  7612. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7613. VNET_LIST_RLOCK_NOSLEEP();
  7614. VNET_FOREACH(vnet_iter) {
  7615. CURVNET_SET(vnet_iter);
  7616. struct sctp_inpcb *inp;
  7617. struct sctp_tcb *stcb;
  7618. #endif
  7619. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7620. SCTP_STAT_INCR(sctps_protocol_drain_calls);
  7621. if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
  7622. #ifdef VIMAGE
  7623. continue;
  7624. #else
  7625. return;
  7626. #endif
  7627. }
  7628. #endif
  7629. SCTP_INP_INFO_RLOCK();
  7630. LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
  7631. /* For each endpoint */
  7632. SCTP_INP_RLOCK(inp);
  7633. LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
  7634. /* For each association */
  7635. SCTP_TCB_LOCK(stcb);
  7636. sctp_drain_mbufs(stcb);
  7637. SCTP_TCB_UNLOCK(stcb);
  7638. }
  7639. SCTP_INP_RUNLOCK(inp);
  7640. }
  7641. SCTP_INP_INFO_RUNLOCK();
  7642. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7643. CURVNET_RESTORE();
  7644. }
  7645. VNET_LIST_RUNLOCK_NOSLEEP();
  7646. #endif
  7647. }
  7648. /*
  7649. * start a new iterator
  7650. * iterates through all endpoints and associations based on the pcb_state
  7651. * flags and asoc_state. "af" (mandatory) is executed for all matching
  7652. * assocs and "ef" (optional) is executed when the iterator completes.
  7653. * "inpf" (optional) is executed for each new endpoint as it is being
  7654. * iterated through. inpe (optional) is called when the inp completes
  7655. * its way through all the stcbs.
  7656. */
  7657. int
  7658. sctp_initiate_iterator(inp_func inpf,
  7659. asoc_func af,
  7660. inp_func inpe,
  7661. uint32_t pcb_state,
  7662. uint32_t pcb_features,
  7663. uint32_t asoc_state,
  7664. void *argp,
  7665. uint32_t argi,
  7666. end_func ef,
  7667. struct sctp_inpcb *s_inp,
  7668. uint8_t chunk_output_off)
  7669. {
  7670. struct sctp_iterator *it = NULL;
  7671. if (af == NULL) {
  7672. return (-1);
  7673. }
  7674. if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
  7675. SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
  7676. SCTP_BASE_VAR(sctp_pcb_initialized));
  7677. return (-1);
  7678. }
  7679. SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
  7680. SCTP_M_ITER);
  7681. if (it == NULL) {
  7682. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
  7683. return (-1);
  7684. }
  7685. memset(it, 0, sizeof(*it));
  7686. it->function_assoc = af;
  7687. it->function_inp = inpf;
  7688. if (inpf)
  7689. it->done_current_ep = 0;
  7690. else
  7691. it->done_current_ep = 1;
  7692. it->function_atend = ef;
  7693. it->pointer = argp;
  7694. it->val = argi;
  7695. it->pcb_flags = pcb_state;
  7696. it->pcb_features = pcb_features;
  7697. it->asoc_state = asoc_state;
  7698. it->function_inp_end = inpe;
  7699. it->no_chunk_output = chunk_output_off;
  7700. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7701. it->vn = curvnet;
  7702. #endif
  7703. if (s_inp) {
  7704. /* Assume lock is held here */
  7705. it->inp = s_inp;
  7706. SCTP_INP_INCR_REF(it->inp);
  7707. it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
  7708. } else {
  7709. SCTP_INP_INFO_RLOCK();
  7710. it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
  7711. if (it->inp) {
  7712. SCTP_INP_INCR_REF(it->inp);
  7713. }
  7714. SCTP_INP_INFO_RUNLOCK();
  7715. it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
  7716. }
  7717. SCTP_IPI_ITERATOR_WQ_LOCK();
  7718. if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
  7719. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  7720. SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
  7721. SCTP_BASE_VAR(sctp_pcb_initialized), it);
  7722. SCTP_FREE(it, SCTP_M_ITER);
  7723. return (-1);
  7724. }
  7725. TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
  7726. if (sctp_it_ctl.iterator_running == 0) {
  7727. sctp_wakeup_iterator();
  7728. }
  7729. SCTP_IPI_ITERATOR_WQ_UNLOCK();
  7730. /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
  7731. return (0);
  7732. }
  7733. /*
  7734. * Atomically add flags to the sctp_flags of an inp.
  7735. * To be used when the write lock of the inp is not held.
  7736. */
  7737. void
  7738. sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags)
  7739. {
  7740. uint32_t old_flags, new_flags;
  7741. do {
  7742. old_flags = inp->sctp_flags;
  7743. new_flags = old_flags | flags;
  7744. } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0);
  7745. }