sctp_output.c 402 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979
  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_header.h>
  45. #include <netinet/sctp_pcb.h>
  46. #include <netinet/sctputil.h>
  47. #include <netinet/sctp_output.h>
  48. #include <netinet/sctp_uio.h>
  49. #include <netinet/sctputil.h>
  50. #include <netinet/sctp_auth.h>
  51. #include <netinet/sctp_timer.h>
  52. #include <netinet/sctp_asconf.h>
  53. #include <netinet/sctp_indata.h>
  54. #include <netinet/sctp_bsd_addr.h>
  55. #include <netinet/sctp_input.h>
  56. #include <netinet/sctp_crc32.h>
  57. #if defined(__FreeBSD__) && !defined(__Userspace__)
  58. #include <netinet/sctp_kdtrace.h>
  59. #endif
  60. #if defined(__linux__)
  61. #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */
  62. #endif
  63. #if defined(INET) || defined(INET6)
  64. #if !defined(_WIN32)
  65. #include <netinet/udp.h>
  66. #endif
  67. #endif
  68. #if !defined(__Userspace__)
  69. #if defined(__APPLE__)
  70. #include <netinet/in.h>
  71. #endif
  72. #if defined(__FreeBSD__) && !defined(__Userspace__)
  73. #include <netinet/udp_var.h>
  74. #include <machine/in_cksum.h>
  75. #endif
  76. #endif
  77. #if defined(__Userspace__) && defined(INET6)
  78. #include <netinet6/sctp6_var.h>
  79. #endif
  80. #if defined(__APPLE__) && !defined(__Userspace__)
  81. #if !(defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD))
  82. #define SCTP_MAX_LINKHDR 16
  83. #endif
  84. #endif
  85. #define SCTP_MAX_GAPS_INARRAY 4
  86. struct sack_track {
  87. uint8_t right_edge; /* mergable on the right edge */
  88. uint8_t left_edge; /* mergable on the left edge */
  89. uint8_t num_entries;
  90. uint8_t spare;
  91. struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
  92. };
  93. const struct sack_track sack_array[256] = {
  94. {0, 0, 0, 0, /* 0x00 */
  95. {{0, 0},
  96. {0, 0},
  97. {0, 0},
  98. {0, 0}
  99. }
  100. },
  101. {1, 0, 1, 0, /* 0x01 */
  102. {{0, 0},
  103. {0, 0},
  104. {0, 0},
  105. {0, 0}
  106. }
  107. },
  108. {0, 0, 1, 0, /* 0x02 */
  109. {{1, 1},
  110. {0, 0},
  111. {0, 0},
  112. {0, 0}
  113. }
  114. },
  115. {1, 0, 1, 0, /* 0x03 */
  116. {{0, 1},
  117. {0, 0},
  118. {0, 0},
  119. {0, 0}
  120. }
  121. },
  122. {0, 0, 1, 0, /* 0x04 */
  123. {{2, 2},
  124. {0, 0},
  125. {0, 0},
  126. {0, 0}
  127. }
  128. },
  129. {1, 0, 2, 0, /* 0x05 */
  130. {{0, 0},
  131. {2, 2},
  132. {0, 0},
  133. {0, 0}
  134. }
  135. },
  136. {0, 0, 1, 0, /* 0x06 */
  137. {{1, 2},
  138. {0, 0},
  139. {0, 0},
  140. {0, 0}
  141. }
  142. },
  143. {1, 0, 1, 0, /* 0x07 */
  144. {{0, 2},
  145. {0, 0},
  146. {0, 0},
  147. {0, 0}
  148. }
  149. },
  150. {0, 0, 1, 0, /* 0x08 */
  151. {{3, 3},
  152. {0, 0},
  153. {0, 0},
  154. {0, 0}
  155. }
  156. },
  157. {1, 0, 2, 0, /* 0x09 */
  158. {{0, 0},
  159. {3, 3},
  160. {0, 0},
  161. {0, 0}
  162. }
  163. },
  164. {0, 0, 2, 0, /* 0x0a */
  165. {{1, 1},
  166. {3, 3},
  167. {0, 0},
  168. {0, 0}
  169. }
  170. },
  171. {1, 0, 2, 0, /* 0x0b */
  172. {{0, 1},
  173. {3, 3},
  174. {0, 0},
  175. {0, 0}
  176. }
  177. },
  178. {0, 0, 1, 0, /* 0x0c */
  179. {{2, 3},
  180. {0, 0},
  181. {0, 0},
  182. {0, 0}
  183. }
  184. },
  185. {1, 0, 2, 0, /* 0x0d */
  186. {{0, 0},
  187. {2, 3},
  188. {0, 0},
  189. {0, 0}
  190. }
  191. },
  192. {0, 0, 1, 0, /* 0x0e */
  193. {{1, 3},
  194. {0, 0},
  195. {0, 0},
  196. {0, 0}
  197. }
  198. },
  199. {1, 0, 1, 0, /* 0x0f */
  200. {{0, 3},
  201. {0, 0},
  202. {0, 0},
  203. {0, 0}
  204. }
  205. },
  206. {0, 0, 1, 0, /* 0x10 */
  207. {{4, 4},
  208. {0, 0},
  209. {0, 0},
  210. {0, 0}
  211. }
  212. },
  213. {1, 0, 2, 0, /* 0x11 */
  214. {{0, 0},
  215. {4, 4},
  216. {0, 0},
  217. {0, 0}
  218. }
  219. },
  220. {0, 0, 2, 0, /* 0x12 */
  221. {{1, 1},
  222. {4, 4},
  223. {0, 0},
  224. {0, 0}
  225. }
  226. },
  227. {1, 0, 2, 0, /* 0x13 */
  228. {{0, 1},
  229. {4, 4},
  230. {0, 0},
  231. {0, 0}
  232. }
  233. },
  234. {0, 0, 2, 0, /* 0x14 */
  235. {{2, 2},
  236. {4, 4},
  237. {0, 0},
  238. {0, 0}
  239. }
  240. },
  241. {1, 0, 3, 0, /* 0x15 */
  242. {{0, 0},
  243. {2, 2},
  244. {4, 4},
  245. {0, 0}
  246. }
  247. },
  248. {0, 0, 2, 0, /* 0x16 */
  249. {{1, 2},
  250. {4, 4},
  251. {0, 0},
  252. {0, 0}
  253. }
  254. },
  255. {1, 0, 2, 0, /* 0x17 */
  256. {{0, 2},
  257. {4, 4},
  258. {0, 0},
  259. {0, 0}
  260. }
  261. },
  262. {0, 0, 1, 0, /* 0x18 */
  263. {{3, 4},
  264. {0, 0},
  265. {0, 0},
  266. {0, 0}
  267. }
  268. },
  269. {1, 0, 2, 0, /* 0x19 */
  270. {{0, 0},
  271. {3, 4},
  272. {0, 0},
  273. {0, 0}
  274. }
  275. },
  276. {0, 0, 2, 0, /* 0x1a */
  277. {{1, 1},
  278. {3, 4},
  279. {0, 0},
  280. {0, 0}
  281. }
  282. },
  283. {1, 0, 2, 0, /* 0x1b */
  284. {{0, 1},
  285. {3, 4},
  286. {0, 0},
  287. {0, 0}
  288. }
  289. },
  290. {0, 0, 1, 0, /* 0x1c */
  291. {{2, 4},
  292. {0, 0},
  293. {0, 0},
  294. {0, 0}
  295. }
  296. },
  297. {1, 0, 2, 0, /* 0x1d */
  298. {{0, 0},
  299. {2, 4},
  300. {0, 0},
  301. {0, 0}
  302. }
  303. },
  304. {0, 0, 1, 0, /* 0x1e */
  305. {{1, 4},
  306. {0, 0},
  307. {0, 0},
  308. {0, 0}
  309. }
  310. },
  311. {1, 0, 1, 0, /* 0x1f */
  312. {{0, 4},
  313. {0, 0},
  314. {0, 0},
  315. {0, 0}
  316. }
  317. },
  318. {0, 0, 1, 0, /* 0x20 */
  319. {{5, 5},
  320. {0, 0},
  321. {0, 0},
  322. {0, 0}
  323. }
  324. },
  325. {1, 0, 2, 0, /* 0x21 */
  326. {{0, 0},
  327. {5, 5},
  328. {0, 0},
  329. {0, 0}
  330. }
  331. },
  332. {0, 0, 2, 0, /* 0x22 */
  333. {{1, 1},
  334. {5, 5},
  335. {0, 0},
  336. {0, 0}
  337. }
  338. },
  339. {1, 0, 2, 0, /* 0x23 */
  340. {{0, 1},
  341. {5, 5},
  342. {0, 0},
  343. {0, 0}
  344. }
  345. },
  346. {0, 0, 2, 0, /* 0x24 */
  347. {{2, 2},
  348. {5, 5},
  349. {0, 0},
  350. {0, 0}
  351. }
  352. },
  353. {1, 0, 3, 0, /* 0x25 */
  354. {{0, 0},
  355. {2, 2},
  356. {5, 5},
  357. {0, 0}
  358. }
  359. },
  360. {0, 0, 2, 0, /* 0x26 */
  361. {{1, 2},
  362. {5, 5},
  363. {0, 0},
  364. {0, 0}
  365. }
  366. },
  367. {1, 0, 2, 0, /* 0x27 */
  368. {{0, 2},
  369. {5, 5},
  370. {0, 0},
  371. {0, 0}
  372. }
  373. },
  374. {0, 0, 2, 0, /* 0x28 */
  375. {{3, 3},
  376. {5, 5},
  377. {0, 0},
  378. {0, 0}
  379. }
  380. },
  381. {1, 0, 3, 0, /* 0x29 */
  382. {{0, 0},
  383. {3, 3},
  384. {5, 5},
  385. {0, 0}
  386. }
  387. },
  388. {0, 0, 3, 0, /* 0x2a */
  389. {{1, 1},
  390. {3, 3},
  391. {5, 5},
  392. {0, 0}
  393. }
  394. },
  395. {1, 0, 3, 0, /* 0x2b */
  396. {{0, 1},
  397. {3, 3},
  398. {5, 5},
  399. {0, 0}
  400. }
  401. },
  402. {0, 0, 2, 0, /* 0x2c */
  403. {{2, 3},
  404. {5, 5},
  405. {0, 0},
  406. {0, 0}
  407. }
  408. },
  409. {1, 0, 3, 0, /* 0x2d */
  410. {{0, 0},
  411. {2, 3},
  412. {5, 5},
  413. {0, 0}
  414. }
  415. },
  416. {0, 0, 2, 0, /* 0x2e */
  417. {{1, 3},
  418. {5, 5},
  419. {0, 0},
  420. {0, 0}
  421. }
  422. },
  423. {1, 0, 2, 0, /* 0x2f */
  424. {{0, 3},
  425. {5, 5},
  426. {0, 0},
  427. {0, 0}
  428. }
  429. },
  430. {0, 0, 1, 0, /* 0x30 */
  431. {{4, 5},
  432. {0, 0},
  433. {0, 0},
  434. {0, 0}
  435. }
  436. },
  437. {1, 0, 2, 0, /* 0x31 */
  438. {{0, 0},
  439. {4, 5},
  440. {0, 0},
  441. {0, 0}
  442. }
  443. },
  444. {0, 0, 2, 0, /* 0x32 */
  445. {{1, 1},
  446. {4, 5},
  447. {0, 0},
  448. {0, 0}
  449. }
  450. },
  451. {1, 0, 2, 0, /* 0x33 */
  452. {{0, 1},
  453. {4, 5},
  454. {0, 0},
  455. {0, 0}
  456. }
  457. },
  458. {0, 0, 2, 0, /* 0x34 */
  459. {{2, 2},
  460. {4, 5},
  461. {0, 0},
  462. {0, 0}
  463. }
  464. },
  465. {1, 0, 3, 0, /* 0x35 */
  466. {{0, 0},
  467. {2, 2},
  468. {4, 5},
  469. {0, 0}
  470. }
  471. },
  472. {0, 0, 2, 0, /* 0x36 */
  473. {{1, 2},
  474. {4, 5},
  475. {0, 0},
  476. {0, 0}
  477. }
  478. },
  479. {1, 0, 2, 0, /* 0x37 */
  480. {{0, 2},
  481. {4, 5},
  482. {0, 0},
  483. {0, 0}
  484. }
  485. },
  486. {0, 0, 1, 0, /* 0x38 */
  487. {{3, 5},
  488. {0, 0},
  489. {0, 0},
  490. {0, 0}
  491. }
  492. },
  493. {1, 0, 2, 0, /* 0x39 */
  494. {{0, 0},
  495. {3, 5},
  496. {0, 0},
  497. {0, 0}
  498. }
  499. },
  500. {0, 0, 2, 0, /* 0x3a */
  501. {{1, 1},
  502. {3, 5},
  503. {0, 0},
  504. {0, 0}
  505. }
  506. },
  507. {1, 0, 2, 0, /* 0x3b */
  508. {{0, 1},
  509. {3, 5},
  510. {0, 0},
  511. {0, 0}
  512. }
  513. },
  514. {0, 0, 1, 0, /* 0x3c */
  515. {{2, 5},
  516. {0, 0},
  517. {0, 0},
  518. {0, 0}
  519. }
  520. },
  521. {1, 0, 2, 0, /* 0x3d */
  522. {{0, 0},
  523. {2, 5},
  524. {0, 0},
  525. {0, 0}
  526. }
  527. },
  528. {0, 0, 1, 0, /* 0x3e */
  529. {{1, 5},
  530. {0, 0},
  531. {0, 0},
  532. {0, 0}
  533. }
  534. },
  535. {1, 0, 1, 0, /* 0x3f */
  536. {{0, 5},
  537. {0, 0},
  538. {0, 0},
  539. {0, 0}
  540. }
  541. },
  542. {0, 0, 1, 0, /* 0x40 */
  543. {{6, 6},
  544. {0, 0},
  545. {0, 0},
  546. {0, 0}
  547. }
  548. },
  549. {1, 0, 2, 0, /* 0x41 */
  550. {{0, 0},
  551. {6, 6},
  552. {0, 0},
  553. {0, 0}
  554. }
  555. },
  556. {0, 0, 2, 0, /* 0x42 */
  557. {{1, 1},
  558. {6, 6},
  559. {0, 0},
  560. {0, 0}
  561. }
  562. },
  563. {1, 0, 2, 0, /* 0x43 */
  564. {{0, 1},
  565. {6, 6},
  566. {0, 0},
  567. {0, 0}
  568. }
  569. },
  570. {0, 0, 2, 0, /* 0x44 */
  571. {{2, 2},
  572. {6, 6},
  573. {0, 0},
  574. {0, 0}
  575. }
  576. },
  577. {1, 0, 3, 0, /* 0x45 */
  578. {{0, 0},
  579. {2, 2},
  580. {6, 6},
  581. {0, 0}
  582. }
  583. },
  584. {0, 0, 2, 0, /* 0x46 */
  585. {{1, 2},
  586. {6, 6},
  587. {0, 0},
  588. {0, 0}
  589. }
  590. },
  591. {1, 0, 2, 0, /* 0x47 */
  592. {{0, 2},
  593. {6, 6},
  594. {0, 0},
  595. {0, 0}
  596. }
  597. },
  598. {0, 0, 2, 0, /* 0x48 */
  599. {{3, 3},
  600. {6, 6},
  601. {0, 0},
  602. {0, 0}
  603. }
  604. },
  605. {1, 0, 3, 0, /* 0x49 */
  606. {{0, 0},
  607. {3, 3},
  608. {6, 6},
  609. {0, 0}
  610. }
  611. },
  612. {0, 0, 3, 0, /* 0x4a */
  613. {{1, 1},
  614. {3, 3},
  615. {6, 6},
  616. {0, 0}
  617. }
  618. },
  619. {1, 0, 3, 0, /* 0x4b */
  620. {{0, 1},
  621. {3, 3},
  622. {6, 6},
  623. {0, 0}
  624. }
  625. },
  626. {0, 0, 2, 0, /* 0x4c */
  627. {{2, 3},
  628. {6, 6},
  629. {0, 0},
  630. {0, 0}
  631. }
  632. },
  633. {1, 0, 3, 0, /* 0x4d */
  634. {{0, 0},
  635. {2, 3},
  636. {6, 6},
  637. {0, 0}
  638. }
  639. },
  640. {0, 0, 2, 0, /* 0x4e */
  641. {{1, 3},
  642. {6, 6},
  643. {0, 0},
  644. {0, 0}
  645. }
  646. },
  647. {1, 0, 2, 0, /* 0x4f */
  648. {{0, 3},
  649. {6, 6},
  650. {0, 0},
  651. {0, 0}
  652. }
  653. },
  654. {0, 0, 2, 0, /* 0x50 */
  655. {{4, 4},
  656. {6, 6},
  657. {0, 0},
  658. {0, 0}
  659. }
  660. },
  661. {1, 0, 3, 0, /* 0x51 */
  662. {{0, 0},
  663. {4, 4},
  664. {6, 6},
  665. {0, 0}
  666. }
  667. },
  668. {0, 0, 3, 0, /* 0x52 */
  669. {{1, 1},
  670. {4, 4},
  671. {6, 6},
  672. {0, 0}
  673. }
  674. },
  675. {1, 0, 3, 0, /* 0x53 */
  676. {{0, 1},
  677. {4, 4},
  678. {6, 6},
  679. {0, 0}
  680. }
  681. },
  682. {0, 0, 3, 0, /* 0x54 */
  683. {{2, 2},
  684. {4, 4},
  685. {6, 6},
  686. {0, 0}
  687. }
  688. },
  689. {1, 0, 4, 0, /* 0x55 */
  690. {{0, 0},
  691. {2, 2},
  692. {4, 4},
  693. {6, 6}
  694. }
  695. },
  696. {0, 0, 3, 0, /* 0x56 */
  697. {{1, 2},
  698. {4, 4},
  699. {6, 6},
  700. {0, 0}
  701. }
  702. },
  703. {1, 0, 3, 0, /* 0x57 */
  704. {{0, 2},
  705. {4, 4},
  706. {6, 6},
  707. {0, 0}
  708. }
  709. },
  710. {0, 0, 2, 0, /* 0x58 */
  711. {{3, 4},
  712. {6, 6},
  713. {0, 0},
  714. {0, 0}
  715. }
  716. },
  717. {1, 0, 3, 0, /* 0x59 */
  718. {{0, 0},
  719. {3, 4},
  720. {6, 6},
  721. {0, 0}
  722. }
  723. },
  724. {0, 0, 3, 0, /* 0x5a */
  725. {{1, 1},
  726. {3, 4},
  727. {6, 6},
  728. {0, 0}
  729. }
  730. },
  731. {1, 0, 3, 0, /* 0x5b */
  732. {{0, 1},
  733. {3, 4},
  734. {6, 6},
  735. {0, 0}
  736. }
  737. },
  738. {0, 0, 2, 0, /* 0x5c */
  739. {{2, 4},
  740. {6, 6},
  741. {0, 0},
  742. {0, 0}
  743. }
  744. },
  745. {1, 0, 3, 0, /* 0x5d */
  746. {{0, 0},
  747. {2, 4},
  748. {6, 6},
  749. {0, 0}
  750. }
  751. },
  752. {0, 0, 2, 0, /* 0x5e */
  753. {{1, 4},
  754. {6, 6},
  755. {0, 0},
  756. {0, 0}
  757. }
  758. },
  759. {1, 0, 2, 0, /* 0x5f */
  760. {{0, 4},
  761. {6, 6},
  762. {0, 0},
  763. {0, 0}
  764. }
  765. },
  766. {0, 0, 1, 0, /* 0x60 */
  767. {{5, 6},
  768. {0, 0},
  769. {0, 0},
  770. {0, 0}
  771. }
  772. },
  773. {1, 0, 2, 0, /* 0x61 */
  774. {{0, 0},
  775. {5, 6},
  776. {0, 0},
  777. {0, 0}
  778. }
  779. },
  780. {0, 0, 2, 0, /* 0x62 */
  781. {{1, 1},
  782. {5, 6},
  783. {0, 0},
  784. {0, 0}
  785. }
  786. },
  787. {1, 0, 2, 0, /* 0x63 */
  788. {{0, 1},
  789. {5, 6},
  790. {0, 0},
  791. {0, 0}
  792. }
  793. },
  794. {0, 0, 2, 0, /* 0x64 */
  795. {{2, 2},
  796. {5, 6},
  797. {0, 0},
  798. {0, 0}
  799. }
  800. },
  801. {1, 0, 3, 0, /* 0x65 */
  802. {{0, 0},
  803. {2, 2},
  804. {5, 6},
  805. {0, 0}
  806. }
  807. },
  808. {0, 0, 2, 0, /* 0x66 */
  809. {{1, 2},
  810. {5, 6},
  811. {0, 0},
  812. {0, 0}
  813. }
  814. },
  815. {1, 0, 2, 0, /* 0x67 */
  816. {{0, 2},
  817. {5, 6},
  818. {0, 0},
  819. {0, 0}
  820. }
  821. },
  822. {0, 0, 2, 0, /* 0x68 */
  823. {{3, 3},
  824. {5, 6},
  825. {0, 0},
  826. {0, 0}
  827. }
  828. },
  829. {1, 0, 3, 0, /* 0x69 */
  830. {{0, 0},
  831. {3, 3},
  832. {5, 6},
  833. {0, 0}
  834. }
  835. },
  836. {0, 0, 3, 0, /* 0x6a */
  837. {{1, 1},
  838. {3, 3},
  839. {5, 6},
  840. {0, 0}
  841. }
  842. },
  843. {1, 0, 3, 0, /* 0x6b */
  844. {{0, 1},
  845. {3, 3},
  846. {5, 6},
  847. {0, 0}
  848. }
  849. },
  850. {0, 0, 2, 0, /* 0x6c */
  851. {{2, 3},
  852. {5, 6},
  853. {0, 0},
  854. {0, 0}
  855. }
  856. },
  857. {1, 0, 3, 0, /* 0x6d */
  858. {{0, 0},
  859. {2, 3},
  860. {5, 6},
  861. {0, 0}
  862. }
  863. },
  864. {0, 0, 2, 0, /* 0x6e */
  865. {{1, 3},
  866. {5, 6},
  867. {0, 0},
  868. {0, 0}
  869. }
  870. },
  871. {1, 0, 2, 0, /* 0x6f */
  872. {{0, 3},
  873. {5, 6},
  874. {0, 0},
  875. {0, 0}
  876. }
  877. },
  878. {0, 0, 1, 0, /* 0x70 */
  879. {{4, 6},
  880. {0, 0},
  881. {0, 0},
  882. {0, 0}
  883. }
  884. },
  885. {1, 0, 2, 0, /* 0x71 */
  886. {{0, 0},
  887. {4, 6},
  888. {0, 0},
  889. {0, 0}
  890. }
  891. },
  892. {0, 0, 2, 0, /* 0x72 */
  893. {{1, 1},
  894. {4, 6},
  895. {0, 0},
  896. {0, 0}
  897. }
  898. },
  899. {1, 0, 2, 0, /* 0x73 */
  900. {{0, 1},
  901. {4, 6},
  902. {0, 0},
  903. {0, 0}
  904. }
  905. },
  906. {0, 0, 2, 0, /* 0x74 */
  907. {{2, 2},
  908. {4, 6},
  909. {0, 0},
  910. {0, 0}
  911. }
  912. },
  913. {1, 0, 3, 0, /* 0x75 */
  914. {{0, 0},
  915. {2, 2},
  916. {4, 6},
  917. {0, 0}
  918. }
  919. },
  920. {0, 0, 2, 0, /* 0x76 */
  921. {{1, 2},
  922. {4, 6},
  923. {0, 0},
  924. {0, 0}
  925. }
  926. },
  927. {1, 0, 2, 0, /* 0x77 */
  928. {{0, 2},
  929. {4, 6},
  930. {0, 0},
  931. {0, 0}
  932. }
  933. },
  934. {0, 0, 1, 0, /* 0x78 */
  935. {{3, 6},
  936. {0, 0},
  937. {0, 0},
  938. {0, 0}
  939. }
  940. },
  941. {1, 0, 2, 0, /* 0x79 */
  942. {{0, 0},
  943. {3, 6},
  944. {0, 0},
  945. {0, 0}
  946. }
  947. },
  948. {0, 0, 2, 0, /* 0x7a */
  949. {{1, 1},
  950. {3, 6},
  951. {0, 0},
  952. {0, 0}
  953. }
  954. },
  955. {1, 0, 2, 0, /* 0x7b */
  956. {{0, 1},
  957. {3, 6},
  958. {0, 0},
  959. {0, 0}
  960. }
  961. },
  962. {0, 0, 1, 0, /* 0x7c */
  963. {{2, 6},
  964. {0, 0},
  965. {0, 0},
  966. {0, 0}
  967. }
  968. },
  969. {1, 0, 2, 0, /* 0x7d */
  970. {{0, 0},
  971. {2, 6},
  972. {0, 0},
  973. {0, 0}
  974. }
  975. },
  976. {0, 0, 1, 0, /* 0x7e */
  977. {{1, 6},
  978. {0, 0},
  979. {0, 0},
  980. {0, 0}
  981. }
  982. },
  983. {1, 0, 1, 0, /* 0x7f */
  984. {{0, 6},
  985. {0, 0},
  986. {0, 0},
  987. {0, 0}
  988. }
  989. },
  990. {0, 1, 1, 0, /* 0x80 */
  991. {{7, 7},
  992. {0, 0},
  993. {0, 0},
  994. {0, 0}
  995. }
  996. },
  997. {1, 1, 2, 0, /* 0x81 */
  998. {{0, 0},
  999. {7, 7},
  1000. {0, 0},
  1001. {0, 0}
  1002. }
  1003. },
  1004. {0, 1, 2, 0, /* 0x82 */
  1005. {{1, 1},
  1006. {7, 7},
  1007. {0, 0},
  1008. {0, 0}
  1009. }
  1010. },
  1011. {1, 1, 2, 0, /* 0x83 */
  1012. {{0, 1},
  1013. {7, 7},
  1014. {0, 0},
  1015. {0, 0}
  1016. }
  1017. },
  1018. {0, 1, 2, 0, /* 0x84 */
  1019. {{2, 2},
  1020. {7, 7},
  1021. {0, 0},
  1022. {0, 0}
  1023. }
  1024. },
  1025. {1, 1, 3, 0, /* 0x85 */
  1026. {{0, 0},
  1027. {2, 2},
  1028. {7, 7},
  1029. {0, 0}
  1030. }
  1031. },
  1032. {0, 1, 2, 0, /* 0x86 */
  1033. {{1, 2},
  1034. {7, 7},
  1035. {0, 0},
  1036. {0, 0}
  1037. }
  1038. },
  1039. {1, 1, 2, 0, /* 0x87 */
  1040. {{0, 2},
  1041. {7, 7},
  1042. {0, 0},
  1043. {0, 0}
  1044. }
  1045. },
  1046. {0, 1, 2, 0, /* 0x88 */
  1047. {{3, 3},
  1048. {7, 7},
  1049. {0, 0},
  1050. {0, 0}
  1051. }
  1052. },
  1053. {1, 1, 3, 0, /* 0x89 */
  1054. {{0, 0},
  1055. {3, 3},
  1056. {7, 7},
  1057. {0, 0}
  1058. }
  1059. },
  1060. {0, 1, 3, 0, /* 0x8a */
  1061. {{1, 1},
  1062. {3, 3},
  1063. {7, 7},
  1064. {0, 0}
  1065. }
  1066. },
  1067. {1, 1, 3, 0, /* 0x8b */
  1068. {{0, 1},
  1069. {3, 3},
  1070. {7, 7},
  1071. {0, 0}
  1072. }
  1073. },
  1074. {0, 1, 2, 0, /* 0x8c */
  1075. {{2, 3},
  1076. {7, 7},
  1077. {0, 0},
  1078. {0, 0}
  1079. }
  1080. },
  1081. {1, 1, 3, 0, /* 0x8d */
  1082. {{0, 0},
  1083. {2, 3},
  1084. {7, 7},
  1085. {0, 0}
  1086. }
  1087. },
  1088. {0, 1, 2, 0, /* 0x8e */
  1089. {{1, 3},
  1090. {7, 7},
  1091. {0, 0},
  1092. {0, 0}
  1093. }
  1094. },
  1095. {1, 1, 2, 0, /* 0x8f */
  1096. {{0, 3},
  1097. {7, 7},
  1098. {0, 0},
  1099. {0, 0}
  1100. }
  1101. },
  1102. {0, 1, 2, 0, /* 0x90 */
  1103. {{4, 4},
  1104. {7, 7},
  1105. {0, 0},
  1106. {0, 0}
  1107. }
  1108. },
  1109. {1, 1, 3, 0, /* 0x91 */
  1110. {{0, 0},
  1111. {4, 4},
  1112. {7, 7},
  1113. {0, 0}
  1114. }
  1115. },
  1116. {0, 1, 3, 0, /* 0x92 */
  1117. {{1, 1},
  1118. {4, 4},
  1119. {7, 7},
  1120. {0, 0}
  1121. }
  1122. },
  1123. {1, 1, 3, 0, /* 0x93 */
  1124. {{0, 1},
  1125. {4, 4},
  1126. {7, 7},
  1127. {0, 0}
  1128. }
  1129. },
  1130. {0, 1, 3, 0, /* 0x94 */
  1131. {{2, 2},
  1132. {4, 4},
  1133. {7, 7},
  1134. {0, 0}
  1135. }
  1136. },
  1137. {1, 1, 4, 0, /* 0x95 */
  1138. {{0, 0},
  1139. {2, 2},
  1140. {4, 4},
  1141. {7, 7}
  1142. }
  1143. },
  1144. {0, 1, 3, 0, /* 0x96 */
  1145. {{1, 2},
  1146. {4, 4},
  1147. {7, 7},
  1148. {0, 0}
  1149. }
  1150. },
  1151. {1, 1, 3, 0, /* 0x97 */
  1152. {{0, 2},
  1153. {4, 4},
  1154. {7, 7},
  1155. {0, 0}
  1156. }
  1157. },
  1158. {0, 1, 2, 0, /* 0x98 */
  1159. {{3, 4},
  1160. {7, 7},
  1161. {0, 0},
  1162. {0, 0}
  1163. }
  1164. },
  1165. {1, 1, 3, 0, /* 0x99 */
  1166. {{0, 0},
  1167. {3, 4},
  1168. {7, 7},
  1169. {0, 0}
  1170. }
  1171. },
  1172. {0, 1, 3, 0, /* 0x9a */
  1173. {{1, 1},
  1174. {3, 4},
  1175. {7, 7},
  1176. {0, 0}
  1177. }
  1178. },
  1179. {1, 1, 3, 0, /* 0x9b */
  1180. {{0, 1},
  1181. {3, 4},
  1182. {7, 7},
  1183. {0, 0}
  1184. }
  1185. },
  1186. {0, 1, 2, 0, /* 0x9c */
  1187. {{2, 4},
  1188. {7, 7},
  1189. {0, 0},
  1190. {0, 0}
  1191. }
  1192. },
  1193. {1, 1, 3, 0, /* 0x9d */
  1194. {{0, 0},
  1195. {2, 4},
  1196. {7, 7},
  1197. {0, 0}
  1198. }
  1199. },
  1200. {0, 1, 2, 0, /* 0x9e */
  1201. {{1, 4},
  1202. {7, 7},
  1203. {0, 0},
  1204. {0, 0}
  1205. }
  1206. },
  1207. {1, 1, 2, 0, /* 0x9f */
  1208. {{0, 4},
  1209. {7, 7},
  1210. {0, 0},
  1211. {0, 0}
  1212. }
  1213. },
  1214. {0, 1, 2, 0, /* 0xa0 */
  1215. {{5, 5},
  1216. {7, 7},
  1217. {0, 0},
  1218. {0, 0}
  1219. }
  1220. },
  1221. {1, 1, 3, 0, /* 0xa1 */
  1222. {{0, 0},
  1223. {5, 5},
  1224. {7, 7},
  1225. {0, 0}
  1226. }
  1227. },
  1228. {0, 1, 3, 0, /* 0xa2 */
  1229. {{1, 1},
  1230. {5, 5},
  1231. {7, 7},
  1232. {0, 0}
  1233. }
  1234. },
  1235. {1, 1, 3, 0, /* 0xa3 */
  1236. {{0, 1},
  1237. {5, 5},
  1238. {7, 7},
  1239. {0, 0}
  1240. }
  1241. },
  1242. {0, 1, 3, 0, /* 0xa4 */
  1243. {{2, 2},
  1244. {5, 5},
  1245. {7, 7},
  1246. {0, 0}
  1247. }
  1248. },
  1249. {1, 1, 4, 0, /* 0xa5 */
  1250. {{0, 0},
  1251. {2, 2},
  1252. {5, 5},
  1253. {7, 7}
  1254. }
  1255. },
  1256. {0, 1, 3, 0, /* 0xa6 */
  1257. {{1, 2},
  1258. {5, 5},
  1259. {7, 7},
  1260. {0, 0}
  1261. }
  1262. },
  1263. {1, 1, 3, 0, /* 0xa7 */
  1264. {{0, 2},
  1265. {5, 5},
  1266. {7, 7},
  1267. {0, 0}
  1268. }
  1269. },
  1270. {0, 1, 3, 0, /* 0xa8 */
  1271. {{3, 3},
  1272. {5, 5},
  1273. {7, 7},
  1274. {0, 0}
  1275. }
  1276. },
  1277. {1, 1, 4, 0, /* 0xa9 */
  1278. {{0, 0},
  1279. {3, 3},
  1280. {5, 5},
  1281. {7, 7}
  1282. }
  1283. },
  1284. {0, 1, 4, 0, /* 0xaa */
  1285. {{1, 1},
  1286. {3, 3},
  1287. {5, 5},
  1288. {7, 7}
  1289. }
  1290. },
  1291. {1, 1, 4, 0, /* 0xab */
  1292. {{0, 1},
  1293. {3, 3},
  1294. {5, 5},
  1295. {7, 7}
  1296. }
  1297. },
  1298. {0, 1, 3, 0, /* 0xac */
  1299. {{2, 3},
  1300. {5, 5},
  1301. {7, 7},
  1302. {0, 0}
  1303. }
  1304. },
  1305. {1, 1, 4, 0, /* 0xad */
  1306. {{0, 0},
  1307. {2, 3},
  1308. {5, 5},
  1309. {7, 7}
  1310. }
  1311. },
  1312. {0, 1, 3, 0, /* 0xae */
  1313. {{1, 3},
  1314. {5, 5},
  1315. {7, 7},
  1316. {0, 0}
  1317. }
  1318. },
  1319. {1, 1, 3, 0, /* 0xaf */
  1320. {{0, 3},
  1321. {5, 5},
  1322. {7, 7},
  1323. {0, 0}
  1324. }
  1325. },
  1326. {0, 1, 2, 0, /* 0xb0 */
  1327. {{4, 5},
  1328. {7, 7},
  1329. {0, 0},
  1330. {0, 0}
  1331. }
  1332. },
  1333. {1, 1, 3, 0, /* 0xb1 */
  1334. {{0, 0},
  1335. {4, 5},
  1336. {7, 7},
  1337. {0, 0}
  1338. }
  1339. },
  1340. {0, 1, 3, 0, /* 0xb2 */
  1341. {{1, 1},
  1342. {4, 5},
  1343. {7, 7},
  1344. {0, 0}
  1345. }
  1346. },
  1347. {1, 1, 3, 0, /* 0xb3 */
  1348. {{0, 1},
  1349. {4, 5},
  1350. {7, 7},
  1351. {0, 0}
  1352. }
  1353. },
  1354. {0, 1, 3, 0, /* 0xb4 */
  1355. {{2, 2},
  1356. {4, 5},
  1357. {7, 7},
  1358. {0, 0}
  1359. }
  1360. },
  1361. {1, 1, 4, 0, /* 0xb5 */
  1362. {{0, 0},
  1363. {2, 2},
  1364. {4, 5},
  1365. {7, 7}
  1366. }
  1367. },
  1368. {0, 1, 3, 0, /* 0xb6 */
  1369. {{1, 2},
  1370. {4, 5},
  1371. {7, 7},
  1372. {0, 0}
  1373. }
  1374. },
  1375. {1, 1, 3, 0, /* 0xb7 */
  1376. {{0, 2},
  1377. {4, 5},
  1378. {7, 7},
  1379. {0, 0}
  1380. }
  1381. },
  1382. {0, 1, 2, 0, /* 0xb8 */
  1383. {{3, 5},
  1384. {7, 7},
  1385. {0, 0},
  1386. {0, 0}
  1387. }
  1388. },
  1389. {1, 1, 3, 0, /* 0xb9 */
  1390. {{0, 0},
  1391. {3, 5},
  1392. {7, 7},
  1393. {0, 0}
  1394. }
  1395. },
  1396. {0, 1, 3, 0, /* 0xba */
  1397. {{1, 1},
  1398. {3, 5},
  1399. {7, 7},
  1400. {0, 0}
  1401. }
  1402. },
  1403. {1, 1, 3, 0, /* 0xbb */
  1404. {{0, 1},
  1405. {3, 5},
  1406. {7, 7},
  1407. {0, 0}
  1408. }
  1409. },
  1410. {0, 1, 2, 0, /* 0xbc */
  1411. {{2, 5},
  1412. {7, 7},
  1413. {0, 0},
  1414. {0, 0}
  1415. }
  1416. },
  1417. {1, 1, 3, 0, /* 0xbd */
  1418. {{0, 0},
  1419. {2, 5},
  1420. {7, 7},
  1421. {0, 0}
  1422. }
  1423. },
  1424. {0, 1, 2, 0, /* 0xbe */
  1425. {{1, 5},
  1426. {7, 7},
  1427. {0, 0},
  1428. {0, 0}
  1429. }
  1430. },
  1431. {1, 1, 2, 0, /* 0xbf */
  1432. {{0, 5},
  1433. {7, 7},
  1434. {0, 0},
  1435. {0, 0}
  1436. }
  1437. },
  1438. {0, 1, 1, 0, /* 0xc0 */
  1439. {{6, 7},
  1440. {0, 0},
  1441. {0, 0},
  1442. {0, 0}
  1443. }
  1444. },
  1445. {1, 1, 2, 0, /* 0xc1 */
  1446. {{0, 0},
  1447. {6, 7},
  1448. {0, 0},
  1449. {0, 0}
  1450. }
  1451. },
  1452. {0, 1, 2, 0, /* 0xc2 */
  1453. {{1, 1},
  1454. {6, 7},
  1455. {0, 0},
  1456. {0, 0}
  1457. }
  1458. },
  1459. {1, 1, 2, 0, /* 0xc3 */
  1460. {{0, 1},
  1461. {6, 7},
  1462. {0, 0},
  1463. {0, 0}
  1464. }
  1465. },
  1466. {0, 1, 2, 0, /* 0xc4 */
  1467. {{2, 2},
  1468. {6, 7},
  1469. {0, 0},
  1470. {0, 0}
  1471. }
  1472. },
  1473. {1, 1, 3, 0, /* 0xc5 */
  1474. {{0, 0},
  1475. {2, 2},
  1476. {6, 7},
  1477. {0, 0}
  1478. }
  1479. },
  1480. {0, 1, 2, 0, /* 0xc6 */
  1481. {{1, 2},
  1482. {6, 7},
  1483. {0, 0},
  1484. {0, 0}
  1485. }
  1486. },
  1487. {1, 1, 2, 0, /* 0xc7 */
  1488. {{0, 2},
  1489. {6, 7},
  1490. {0, 0},
  1491. {0, 0}
  1492. }
  1493. },
  1494. {0, 1, 2, 0, /* 0xc8 */
  1495. {{3, 3},
  1496. {6, 7},
  1497. {0, 0},
  1498. {0, 0}
  1499. }
  1500. },
  1501. {1, 1, 3, 0, /* 0xc9 */
  1502. {{0, 0},
  1503. {3, 3},
  1504. {6, 7},
  1505. {0, 0}
  1506. }
  1507. },
  1508. {0, 1, 3, 0, /* 0xca */
  1509. {{1, 1},
  1510. {3, 3},
  1511. {6, 7},
  1512. {0, 0}
  1513. }
  1514. },
  1515. {1, 1, 3, 0, /* 0xcb */
  1516. {{0, 1},
  1517. {3, 3},
  1518. {6, 7},
  1519. {0, 0}
  1520. }
  1521. },
  1522. {0, 1, 2, 0, /* 0xcc */
  1523. {{2, 3},
  1524. {6, 7},
  1525. {0, 0},
  1526. {0, 0}
  1527. }
  1528. },
  1529. {1, 1, 3, 0, /* 0xcd */
  1530. {{0, 0},
  1531. {2, 3},
  1532. {6, 7},
  1533. {0, 0}
  1534. }
  1535. },
  1536. {0, 1, 2, 0, /* 0xce */
  1537. {{1, 3},
  1538. {6, 7},
  1539. {0, 0},
  1540. {0, 0}
  1541. }
  1542. },
  1543. {1, 1, 2, 0, /* 0xcf */
  1544. {{0, 3},
  1545. {6, 7},
  1546. {0, 0},
  1547. {0, 0}
  1548. }
  1549. },
  1550. {0, 1, 2, 0, /* 0xd0 */
  1551. {{4, 4},
  1552. {6, 7},
  1553. {0, 0},
  1554. {0, 0}
  1555. }
  1556. },
  1557. {1, 1, 3, 0, /* 0xd1 */
  1558. {{0, 0},
  1559. {4, 4},
  1560. {6, 7},
  1561. {0, 0}
  1562. }
  1563. },
  1564. {0, 1, 3, 0, /* 0xd2 */
  1565. {{1, 1},
  1566. {4, 4},
  1567. {6, 7},
  1568. {0, 0}
  1569. }
  1570. },
  1571. {1, 1, 3, 0, /* 0xd3 */
  1572. {{0, 1},
  1573. {4, 4},
  1574. {6, 7},
  1575. {0, 0}
  1576. }
  1577. },
  1578. {0, 1, 3, 0, /* 0xd4 */
  1579. {{2, 2},
  1580. {4, 4},
  1581. {6, 7},
  1582. {0, 0}
  1583. }
  1584. },
  1585. {1, 1, 4, 0, /* 0xd5 */
  1586. {{0, 0},
  1587. {2, 2},
  1588. {4, 4},
  1589. {6, 7}
  1590. }
  1591. },
  1592. {0, 1, 3, 0, /* 0xd6 */
  1593. {{1, 2},
  1594. {4, 4},
  1595. {6, 7},
  1596. {0, 0}
  1597. }
  1598. },
  1599. {1, 1, 3, 0, /* 0xd7 */
  1600. {{0, 2},
  1601. {4, 4},
  1602. {6, 7},
  1603. {0, 0}
  1604. }
  1605. },
  1606. {0, 1, 2, 0, /* 0xd8 */
  1607. {{3, 4},
  1608. {6, 7},
  1609. {0, 0},
  1610. {0, 0}
  1611. }
  1612. },
  1613. {1, 1, 3, 0, /* 0xd9 */
  1614. {{0, 0},
  1615. {3, 4},
  1616. {6, 7},
  1617. {0, 0}
  1618. }
  1619. },
  1620. {0, 1, 3, 0, /* 0xda */
  1621. {{1, 1},
  1622. {3, 4},
  1623. {6, 7},
  1624. {0, 0}
  1625. }
  1626. },
  1627. {1, 1, 3, 0, /* 0xdb */
  1628. {{0, 1},
  1629. {3, 4},
  1630. {6, 7},
  1631. {0, 0}
  1632. }
  1633. },
  1634. {0, 1, 2, 0, /* 0xdc */
  1635. {{2, 4},
  1636. {6, 7},
  1637. {0, 0},
  1638. {0, 0}
  1639. }
  1640. },
  1641. {1, 1, 3, 0, /* 0xdd */
  1642. {{0, 0},
  1643. {2, 4},
  1644. {6, 7},
  1645. {0, 0}
  1646. }
  1647. },
  1648. {0, 1, 2, 0, /* 0xde */
  1649. {{1, 4},
  1650. {6, 7},
  1651. {0, 0},
  1652. {0, 0}
  1653. }
  1654. },
  1655. {1, 1, 2, 0, /* 0xdf */
  1656. {{0, 4},
  1657. {6, 7},
  1658. {0, 0},
  1659. {0, 0}
  1660. }
  1661. },
  1662. {0, 1, 1, 0, /* 0xe0 */
  1663. {{5, 7},
  1664. {0, 0},
  1665. {0, 0},
  1666. {0, 0}
  1667. }
  1668. },
  1669. {1, 1, 2, 0, /* 0xe1 */
  1670. {{0, 0},
  1671. {5, 7},
  1672. {0, 0},
  1673. {0, 0}
  1674. }
  1675. },
  1676. {0, 1, 2, 0, /* 0xe2 */
  1677. {{1, 1},
  1678. {5, 7},
  1679. {0, 0},
  1680. {0, 0}
  1681. }
  1682. },
  1683. {1, 1, 2, 0, /* 0xe3 */
  1684. {{0, 1},
  1685. {5, 7},
  1686. {0, 0},
  1687. {0, 0}
  1688. }
  1689. },
  1690. {0, 1, 2, 0, /* 0xe4 */
  1691. {{2, 2},
  1692. {5, 7},
  1693. {0, 0},
  1694. {0, 0}
  1695. }
  1696. },
  1697. {1, 1, 3, 0, /* 0xe5 */
  1698. {{0, 0},
  1699. {2, 2},
  1700. {5, 7},
  1701. {0, 0}
  1702. }
  1703. },
  1704. {0, 1, 2, 0, /* 0xe6 */
  1705. {{1, 2},
  1706. {5, 7},
  1707. {0, 0},
  1708. {0, 0}
  1709. }
  1710. },
  1711. {1, 1, 2, 0, /* 0xe7 */
  1712. {{0, 2},
  1713. {5, 7},
  1714. {0, 0},
  1715. {0, 0}
  1716. }
  1717. },
  1718. {0, 1, 2, 0, /* 0xe8 */
  1719. {{3, 3},
  1720. {5, 7},
  1721. {0, 0},
  1722. {0, 0}
  1723. }
  1724. },
  1725. {1, 1, 3, 0, /* 0xe9 */
  1726. {{0, 0},
  1727. {3, 3},
  1728. {5, 7},
  1729. {0, 0}
  1730. }
  1731. },
  1732. {0, 1, 3, 0, /* 0xea */
  1733. {{1, 1},
  1734. {3, 3},
  1735. {5, 7},
  1736. {0, 0}
  1737. }
  1738. },
  1739. {1, 1, 3, 0, /* 0xeb */
  1740. {{0, 1},
  1741. {3, 3},
  1742. {5, 7},
  1743. {0, 0}
  1744. }
  1745. },
  1746. {0, 1, 2, 0, /* 0xec */
  1747. {{2, 3},
  1748. {5, 7},
  1749. {0, 0},
  1750. {0, 0}
  1751. }
  1752. },
  1753. {1, 1, 3, 0, /* 0xed */
  1754. {{0, 0},
  1755. {2, 3},
  1756. {5, 7},
  1757. {0, 0}
  1758. }
  1759. },
  1760. {0, 1, 2, 0, /* 0xee */
  1761. {{1, 3},
  1762. {5, 7},
  1763. {0, 0},
  1764. {0, 0}
  1765. }
  1766. },
  1767. {1, 1, 2, 0, /* 0xef */
  1768. {{0, 3},
  1769. {5, 7},
  1770. {0, 0},
  1771. {0, 0}
  1772. }
  1773. },
  1774. {0, 1, 1, 0, /* 0xf0 */
  1775. {{4, 7},
  1776. {0, 0},
  1777. {0, 0},
  1778. {0, 0}
  1779. }
  1780. },
  1781. {1, 1, 2, 0, /* 0xf1 */
  1782. {{0, 0},
  1783. {4, 7},
  1784. {0, 0},
  1785. {0, 0}
  1786. }
  1787. },
  1788. {0, 1, 2, 0, /* 0xf2 */
  1789. {{1, 1},
  1790. {4, 7},
  1791. {0, 0},
  1792. {0, 0}
  1793. }
  1794. },
  1795. {1, 1, 2, 0, /* 0xf3 */
  1796. {{0, 1},
  1797. {4, 7},
  1798. {0, 0},
  1799. {0, 0}
  1800. }
  1801. },
  1802. {0, 1, 2, 0, /* 0xf4 */
  1803. {{2, 2},
  1804. {4, 7},
  1805. {0, 0},
  1806. {0, 0}
  1807. }
  1808. },
  1809. {1, 1, 3, 0, /* 0xf5 */
  1810. {{0, 0},
  1811. {2, 2},
  1812. {4, 7},
  1813. {0, 0}
  1814. }
  1815. },
  1816. {0, 1, 2, 0, /* 0xf6 */
  1817. {{1, 2},
  1818. {4, 7},
  1819. {0, 0},
  1820. {0, 0}
  1821. }
  1822. },
  1823. {1, 1, 2, 0, /* 0xf7 */
  1824. {{0, 2},
  1825. {4, 7},
  1826. {0, 0},
  1827. {0, 0}
  1828. }
  1829. },
  1830. {0, 1, 1, 0, /* 0xf8 */
  1831. {{3, 7},
  1832. {0, 0},
  1833. {0, 0},
  1834. {0, 0}
  1835. }
  1836. },
  1837. {1, 1, 2, 0, /* 0xf9 */
  1838. {{0, 0},
  1839. {3, 7},
  1840. {0, 0},
  1841. {0, 0}
  1842. }
  1843. },
  1844. {0, 1, 2, 0, /* 0xfa */
  1845. {{1, 1},
  1846. {3, 7},
  1847. {0, 0},
  1848. {0, 0}
  1849. }
  1850. },
  1851. {1, 1, 2, 0, /* 0xfb */
  1852. {{0, 1},
  1853. {3, 7},
  1854. {0, 0},
  1855. {0, 0}
  1856. }
  1857. },
  1858. {0, 1, 1, 0, /* 0xfc */
  1859. {{2, 7},
  1860. {0, 0},
  1861. {0, 0},
  1862. {0, 0}
  1863. }
  1864. },
  1865. {1, 1, 2, 0, /* 0xfd */
  1866. {{0, 0},
  1867. {2, 7},
  1868. {0, 0},
  1869. {0, 0}
  1870. }
  1871. },
  1872. {0, 1, 1, 0, /* 0xfe */
  1873. {{1, 7},
  1874. {0, 0},
  1875. {0, 0},
  1876. {0, 0}
  1877. }
  1878. },
  1879. {1, 1, 1, 0, /* 0xff */
  1880. {{0, 7},
  1881. {0, 0},
  1882. {0, 0},
  1883. {0, 0}
  1884. }
  1885. }
  1886. };
  1887. int
  1888. sctp_is_address_in_scope(struct sctp_ifa *ifa,
  1889. struct sctp_scoping *scope,
  1890. int do_update)
  1891. {
  1892. if ((scope->loopback_scope == 0) &&
  1893. (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
  1894. /*
  1895. * skip loopback if not in scope *
  1896. */
  1897. return (0);
  1898. }
  1899. switch (ifa->address.sa.sa_family) {
  1900. #ifdef INET
  1901. case AF_INET:
  1902. if (scope->ipv4_addr_legal) {
  1903. struct sockaddr_in *sin;
  1904. sin = &ifa->address.sin;
  1905. if (sin->sin_addr.s_addr == 0) {
  1906. /* not in scope , unspecified */
  1907. return (0);
  1908. }
  1909. if ((scope->ipv4_local_scope == 0) &&
  1910. (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
  1911. /* private address not in scope */
  1912. return (0);
  1913. }
  1914. } else {
  1915. return (0);
  1916. }
  1917. break;
  1918. #endif
  1919. #ifdef INET6
  1920. case AF_INET6:
  1921. if (scope->ipv6_addr_legal) {
  1922. struct sockaddr_in6 *sin6;
  1923. /* Must update the flags, bummer, which
  1924. * means any IFA locks must now be applied HERE <->
  1925. */
  1926. if (do_update) {
  1927. sctp_gather_internal_ifa_flags(ifa);
  1928. }
  1929. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  1930. return (0);
  1931. }
  1932. /* ok to use deprecated addresses? */
  1933. sin6 = &ifa->address.sin6;
  1934. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  1935. /* skip unspecified addresses */
  1936. return (0);
  1937. }
  1938. if ( /* (local_scope == 0) && */
  1939. (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
  1940. return (0);
  1941. }
  1942. if ((scope->site_scope == 0) &&
  1943. (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
  1944. return (0);
  1945. }
  1946. } else {
  1947. return (0);
  1948. }
  1949. break;
  1950. #endif
  1951. #if defined(__Userspace__)
  1952. case AF_CONN:
  1953. if (!scope->conn_addr_legal) {
  1954. return (0);
  1955. }
  1956. break;
  1957. #endif
  1958. default:
  1959. return (0);
  1960. }
  1961. return (1);
  1962. }
  1963. static struct mbuf *
  1964. sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
  1965. {
  1966. #if defined(INET) || defined(INET6)
  1967. struct sctp_paramhdr *paramh;
  1968. struct mbuf *mret;
  1969. uint16_t plen;
  1970. #endif
  1971. switch (ifa->address.sa.sa_family) {
  1972. #ifdef INET
  1973. case AF_INET:
  1974. plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
  1975. break;
  1976. #endif
  1977. #ifdef INET6
  1978. case AF_INET6:
  1979. plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
  1980. break;
  1981. #endif
  1982. default:
  1983. return (m);
  1984. }
  1985. #if defined(INET) || defined(INET6)
  1986. if (M_TRAILINGSPACE(m) >= plen) {
  1987. /* easy side we just drop it on the end */
  1988. paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
  1989. mret = m;
  1990. } else {
  1991. /* Need more space */
  1992. mret = m;
  1993. while (SCTP_BUF_NEXT(mret) != NULL) {
  1994. mret = SCTP_BUF_NEXT(mret);
  1995. }
  1996. SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
  1997. if (SCTP_BUF_NEXT(mret) == NULL) {
  1998. /* We are hosed, can't add more addresses */
  1999. return (m);
  2000. }
  2001. mret = SCTP_BUF_NEXT(mret);
  2002. paramh = mtod(mret, struct sctp_paramhdr *);
  2003. }
  2004. /* now add the parameter */
  2005. switch (ifa->address.sa.sa_family) {
  2006. #ifdef INET
  2007. case AF_INET:
  2008. {
  2009. struct sctp_ipv4addr_param *ipv4p;
  2010. struct sockaddr_in *sin;
  2011. sin = &ifa->address.sin;
  2012. ipv4p = (struct sctp_ipv4addr_param *)paramh;
  2013. paramh->param_type = htons(SCTP_IPV4_ADDRESS);
  2014. paramh->param_length = htons(plen);
  2015. ipv4p->addr = sin->sin_addr.s_addr;
  2016. SCTP_BUF_LEN(mret) += plen;
  2017. break;
  2018. }
  2019. #endif
  2020. #ifdef INET6
  2021. case AF_INET6:
  2022. {
  2023. struct sctp_ipv6addr_param *ipv6p;
  2024. struct sockaddr_in6 *sin6;
  2025. sin6 = &ifa->address.sin6;
  2026. ipv6p = (struct sctp_ipv6addr_param *)paramh;
  2027. paramh->param_type = htons(SCTP_IPV6_ADDRESS);
  2028. paramh->param_length = htons(plen);
  2029. memcpy(ipv6p->addr, &sin6->sin6_addr,
  2030. sizeof(ipv6p->addr));
  2031. #if defined(SCTP_EMBEDDED_V6_SCOPE)
  2032. /* clear embedded scope in the address */
  2033. in6_clearscope((struct in6_addr *)ipv6p->addr);
  2034. #endif
  2035. SCTP_BUF_LEN(mret) += plen;
  2036. break;
  2037. }
  2038. #endif
  2039. default:
  2040. return (m);
  2041. }
  2042. if (len != NULL) {
  2043. *len += plen;
  2044. }
  2045. return (mret);
  2046. #endif
  2047. }
  2048. struct mbuf *
  2049. sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  2050. struct sctp_scoping *scope,
  2051. struct mbuf *m_at, int cnt_inits_to,
  2052. uint16_t *padding_len, uint16_t *chunk_len)
  2053. {
  2054. struct sctp_vrf *vrf = NULL;
  2055. int cnt, limit_out = 0, total_count;
  2056. uint32_t vrf_id;
  2057. vrf_id = inp->def_vrf_id;
  2058. SCTP_IPI_ADDR_RLOCK();
  2059. vrf = sctp_find_vrf(vrf_id);
  2060. if (vrf == NULL) {
  2061. SCTP_IPI_ADDR_RUNLOCK();
  2062. return (m_at);
  2063. }
  2064. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  2065. struct sctp_ifa *sctp_ifap;
  2066. struct sctp_ifn *sctp_ifnp;
  2067. cnt = cnt_inits_to;
  2068. if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
  2069. limit_out = 1;
  2070. cnt = SCTP_ADDRESS_LIMIT;
  2071. goto skip_count;
  2072. }
  2073. LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
  2074. if ((scope->loopback_scope == 0) &&
  2075. SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
  2076. /*
  2077. * Skip loopback devices if loopback_scope
  2078. * not set
  2079. */
  2080. continue;
  2081. }
  2082. LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
  2083. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2084. #ifdef INET
  2085. if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
  2086. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2087. &sctp_ifap->address.sin.sin_addr) != 0)) {
  2088. continue;
  2089. }
  2090. #endif
  2091. #ifdef INET6
  2092. if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
  2093. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2094. &sctp_ifap->address.sin6.sin6_addr) != 0)) {
  2095. continue;
  2096. }
  2097. #endif
  2098. #endif
  2099. if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
  2100. continue;
  2101. }
  2102. #if defined(__Userspace__)
  2103. if (sctp_ifap->address.sa.sa_family == AF_CONN) {
  2104. continue;
  2105. }
  2106. #endif
  2107. if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
  2108. continue;
  2109. }
  2110. cnt++;
  2111. if (cnt > SCTP_ADDRESS_LIMIT) {
  2112. break;
  2113. }
  2114. }
  2115. if (cnt > SCTP_ADDRESS_LIMIT) {
  2116. break;
  2117. }
  2118. }
  2119. skip_count:
  2120. if (cnt > 1) {
  2121. total_count = 0;
  2122. LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
  2123. cnt = 0;
  2124. if ((scope->loopback_scope == 0) &&
  2125. SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
  2126. /*
  2127. * Skip loopback devices if
  2128. * loopback_scope not set
  2129. */
  2130. continue;
  2131. }
  2132. LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
  2133. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2134. #ifdef INET
  2135. if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
  2136. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2137. &sctp_ifap->address.sin.sin_addr) != 0)) {
  2138. continue;
  2139. }
  2140. #endif
  2141. #ifdef INET6
  2142. if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
  2143. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2144. &sctp_ifap->address.sin6.sin6_addr) != 0)) {
  2145. continue;
  2146. }
  2147. #endif
  2148. #endif
  2149. if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
  2150. continue;
  2151. }
  2152. #if defined(__Userspace__)
  2153. if (sctp_ifap->address.sa.sa_family == AF_CONN) {
  2154. continue;
  2155. }
  2156. #endif
  2157. if (sctp_is_address_in_scope(sctp_ifap,
  2158. scope, 0) == 0) {
  2159. continue;
  2160. }
  2161. if ((chunk_len != NULL) &&
  2162. (padding_len != NULL) &&
  2163. (*padding_len > 0)) {
  2164. memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len);
  2165. SCTP_BUF_LEN(m_at) += *padding_len;
  2166. *chunk_len += *padding_len;
  2167. *padding_len = 0;
  2168. }
  2169. m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
  2170. if (limit_out) {
  2171. cnt++;
  2172. total_count++;
  2173. if (cnt >= 2) {
  2174. /* two from each address */
  2175. break;
  2176. }
  2177. if (total_count > SCTP_ADDRESS_LIMIT) {
  2178. /* No more addresses */
  2179. break;
  2180. }
  2181. }
  2182. }
  2183. }
  2184. }
  2185. } else {
  2186. struct sctp_laddr *laddr;
  2187. cnt = cnt_inits_to;
  2188. /* First, how many ? */
  2189. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  2190. if (laddr->ifa == NULL) {
  2191. continue;
  2192. }
  2193. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
  2194. /* Address being deleted by the system, dont
  2195. * list.
  2196. */
  2197. continue;
  2198. if (laddr->action == SCTP_DEL_IP_ADDRESS) {
  2199. /* Address being deleted on this ep
  2200. * don't list.
  2201. */
  2202. continue;
  2203. }
  2204. #if defined(__Userspace__)
  2205. if (laddr->ifa->address.sa.sa_family == AF_CONN) {
  2206. continue;
  2207. }
  2208. #endif
  2209. if (sctp_is_address_in_scope(laddr->ifa,
  2210. scope, 1) == 0) {
  2211. continue;
  2212. }
  2213. cnt++;
  2214. }
  2215. /*
  2216. * To get through a NAT we only list addresses if we have
  2217. * more than one. That way if you just bind a single address
  2218. * we let the source of the init dictate our address.
  2219. */
  2220. if (cnt > 1) {
  2221. cnt = cnt_inits_to;
  2222. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  2223. if (laddr->ifa == NULL) {
  2224. continue;
  2225. }
  2226. if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
  2227. continue;
  2228. }
  2229. #if defined(__Userspace__)
  2230. if (laddr->ifa->address.sa.sa_family == AF_CONN) {
  2231. continue;
  2232. }
  2233. #endif
  2234. if (sctp_is_address_in_scope(laddr->ifa,
  2235. scope, 0) == 0) {
  2236. continue;
  2237. }
  2238. if ((chunk_len != NULL) &&
  2239. (padding_len != NULL) &&
  2240. (*padding_len > 0)) {
  2241. memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len);
  2242. SCTP_BUF_LEN(m_at) += *padding_len;
  2243. *chunk_len += *padding_len;
  2244. *padding_len = 0;
  2245. }
  2246. m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
  2247. cnt++;
  2248. if (cnt >= SCTP_ADDRESS_LIMIT) {
  2249. break;
  2250. }
  2251. }
  2252. }
  2253. }
  2254. SCTP_IPI_ADDR_RUNLOCK();
  2255. return (m_at);
  2256. }
  2257. static struct sctp_ifa *
  2258. sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
  2259. uint8_t dest_is_loop,
  2260. uint8_t dest_is_priv,
  2261. sa_family_t fam)
  2262. {
  2263. uint8_t dest_is_global = 0;
  2264. /* dest_is_priv is true if destination is a private address */
  2265. /* dest_is_loop is true if destination is a loopback addresses */
  2266. /**
  2267. * Here we determine if its a preferred address. A preferred address
  2268. * means it is the same scope or higher scope then the destination.
  2269. * L = loopback, P = private, G = global
  2270. * -----------------------------------------
  2271. * src | dest | result
  2272. * ----------------------------------------
  2273. * L | L | yes
  2274. * -----------------------------------------
  2275. * P | L | yes-v4 no-v6
  2276. * -----------------------------------------
  2277. * G | L | yes-v4 no-v6
  2278. * -----------------------------------------
  2279. * L | P | no
  2280. * -----------------------------------------
  2281. * P | P | yes
  2282. * -----------------------------------------
  2283. * G | P | no
  2284. * -----------------------------------------
  2285. * L | G | no
  2286. * -----------------------------------------
  2287. * P | G | no
  2288. * -----------------------------------------
  2289. * G | G | yes
  2290. * -----------------------------------------
  2291. */
  2292. if (ifa->address.sa.sa_family != fam) {
  2293. /* forget mis-matched family */
  2294. return (NULL);
  2295. }
  2296. if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
  2297. dest_is_global = 1;
  2298. }
  2299. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
  2300. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
  2301. /* Ok the address may be ok */
  2302. #ifdef INET6
  2303. if (fam == AF_INET6) {
  2304. /* ok to use deprecated addresses? no lets not! */
  2305. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  2306. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
  2307. return (NULL);
  2308. }
  2309. if (ifa->src_is_priv && !ifa->src_is_loop) {
  2310. if (dest_is_loop) {
  2311. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
  2312. return (NULL);
  2313. }
  2314. }
  2315. if (ifa->src_is_glob) {
  2316. if (dest_is_loop) {
  2317. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
  2318. return (NULL);
  2319. }
  2320. }
  2321. }
  2322. #endif
  2323. /* Now that we know what is what, implement or table
  2324. * this could in theory be done slicker (it used to be), but this
  2325. * is straightforward and easier to validate :-)
  2326. */
  2327. SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
  2328. ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
  2329. SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
  2330. dest_is_loop, dest_is_priv, dest_is_global);
  2331. if ((ifa->src_is_loop) && (dest_is_priv)) {
  2332. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
  2333. return (NULL);
  2334. }
  2335. if ((ifa->src_is_glob) && (dest_is_priv)) {
  2336. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
  2337. return (NULL);
  2338. }
  2339. if ((ifa->src_is_loop) && (dest_is_global)) {
  2340. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
  2341. return (NULL);
  2342. }
  2343. if ((ifa->src_is_priv) && (dest_is_global)) {
  2344. SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
  2345. return (NULL);
  2346. }
  2347. SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
  2348. /* its a preferred address */
  2349. return (ifa);
  2350. }
  2351. static struct sctp_ifa *
  2352. sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
  2353. uint8_t dest_is_loop,
  2354. uint8_t dest_is_priv,
  2355. sa_family_t fam)
  2356. {
  2357. uint8_t dest_is_global = 0;
  2358. /**
  2359. * Here we determine if its a acceptable address. A acceptable
  2360. * address means it is the same scope or higher scope but we can
  2361. * allow for NAT which means its ok to have a global dest and a
  2362. * private src.
  2363. *
  2364. * L = loopback, P = private, G = global
  2365. * -----------------------------------------
  2366. * src | dest | result
  2367. * -----------------------------------------
  2368. * L | L | yes
  2369. * -----------------------------------------
  2370. * P | L | yes-v4 no-v6
  2371. * -----------------------------------------
  2372. * G | L | yes
  2373. * -----------------------------------------
  2374. * L | P | no
  2375. * -----------------------------------------
  2376. * P | P | yes
  2377. * -----------------------------------------
  2378. * G | P | yes - May not work
  2379. * -----------------------------------------
  2380. * L | G | no
  2381. * -----------------------------------------
  2382. * P | G | yes - May not work
  2383. * -----------------------------------------
  2384. * G | G | yes
  2385. * -----------------------------------------
  2386. */
  2387. if (ifa->address.sa.sa_family != fam) {
  2388. /* forget non matching family */
  2389. SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
  2390. ifa->address.sa.sa_family, fam);
  2391. return (NULL);
  2392. }
  2393. /* Ok the address may be ok */
  2394. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
  2395. SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
  2396. dest_is_loop, dest_is_priv);
  2397. if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
  2398. dest_is_global = 1;
  2399. }
  2400. #ifdef INET6
  2401. if (fam == AF_INET6) {
  2402. /* ok to use deprecated addresses? */
  2403. if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
  2404. return (NULL);
  2405. }
  2406. if (ifa->src_is_priv) {
  2407. /* Special case, linklocal to loop */
  2408. if (dest_is_loop)
  2409. return (NULL);
  2410. }
  2411. }
  2412. #endif
  2413. /*
  2414. * Now that we know what is what, implement our table.
  2415. * This could in theory be done slicker (it used to be), but this
  2416. * is straightforward and easier to validate :-)
  2417. */
  2418. SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
  2419. ifa->src_is_loop,
  2420. dest_is_priv);
  2421. if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
  2422. return (NULL);
  2423. }
  2424. SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
  2425. ifa->src_is_loop,
  2426. dest_is_global);
  2427. if ((ifa->src_is_loop == 1) && (dest_is_global)) {
  2428. return (NULL);
  2429. }
  2430. SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
  2431. /* its an acceptable address */
  2432. return (ifa);
  2433. }
  2434. int
  2435. sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
  2436. {
  2437. struct sctp_laddr *laddr;
  2438. if (stcb == NULL) {
  2439. /* There are no restrictions, no TCB :-) */
  2440. return (0);
  2441. }
  2442. LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
  2443. if (laddr->ifa == NULL) {
  2444. SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
  2445. __func__);
  2446. continue;
  2447. }
  2448. if (laddr->ifa == ifa) {
  2449. /* Yes it is on the list */
  2450. return (1);
  2451. }
  2452. }
  2453. return (0);
  2454. }
  2455. int
  2456. sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
  2457. {
  2458. struct sctp_laddr *laddr;
  2459. if (ifa == NULL)
  2460. return (0);
  2461. LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
  2462. if (laddr->ifa == NULL) {
  2463. SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
  2464. __func__);
  2465. continue;
  2466. }
  2467. if ((laddr->ifa == ifa) && laddr->action == 0)
  2468. /* same pointer */
  2469. return (1);
  2470. }
  2471. return (0);
  2472. }
  2473. static struct sctp_ifa *
  2474. sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
  2475. sctp_route_t *ro,
  2476. uint32_t vrf_id,
  2477. int non_asoc_addr_ok,
  2478. uint8_t dest_is_priv,
  2479. uint8_t dest_is_loop,
  2480. sa_family_t fam)
  2481. {
  2482. struct sctp_laddr *laddr, *starting_point;
  2483. void *ifn;
  2484. int resettotop = 0;
  2485. struct sctp_ifn *sctp_ifn;
  2486. struct sctp_ifa *sctp_ifa, *sifa;
  2487. struct sctp_vrf *vrf;
  2488. uint32_t ifn_index;
  2489. vrf = sctp_find_vrf(vrf_id);
  2490. if (vrf == NULL)
  2491. return (NULL);
  2492. ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
  2493. ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
  2494. sctp_ifn = sctp_find_ifn(ifn, ifn_index);
  2495. /*
  2496. * first question, is the ifn we will emit on in our list, if so, we
  2497. * want such an address. Note that we first looked for a
  2498. * preferred address.
  2499. */
  2500. if (sctp_ifn) {
  2501. /* is a preferred one on the interface we route out? */
  2502. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  2503. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2504. #ifdef INET
  2505. if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
  2506. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2507. &sctp_ifa->address.sin.sin_addr) != 0)) {
  2508. continue;
  2509. }
  2510. #endif
  2511. #ifdef INET6
  2512. if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
  2513. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2514. &sctp_ifa->address.sin6.sin6_addr) != 0)) {
  2515. continue;
  2516. }
  2517. #endif
  2518. #endif
  2519. if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
  2520. (non_asoc_addr_ok == 0))
  2521. continue;
  2522. sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
  2523. dest_is_loop,
  2524. dest_is_priv, fam);
  2525. if (sifa == NULL)
  2526. continue;
  2527. if (sctp_is_addr_in_ep(inp, sifa)) {
  2528. atomic_add_int(&sifa->refcount, 1);
  2529. return (sifa);
  2530. }
  2531. }
  2532. }
  2533. /*
  2534. * ok, now we now need to find one on the list of the addresses.
  2535. * We can't get one on the emitting interface so let's find first
  2536. * a preferred one. If not that an acceptable one otherwise...
  2537. * we return NULL.
  2538. */
  2539. starting_point = inp->next_addr_touse;
  2540. once_again:
  2541. if (inp->next_addr_touse == NULL) {
  2542. inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
  2543. resettotop = 1;
  2544. }
  2545. for (laddr = inp->next_addr_touse; laddr;
  2546. laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
  2547. if (laddr->ifa == NULL) {
  2548. /* address has been removed */
  2549. continue;
  2550. }
  2551. if (laddr->action == SCTP_DEL_IP_ADDRESS) {
  2552. /* address is being deleted */
  2553. continue;
  2554. }
  2555. sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
  2556. dest_is_priv, fam);
  2557. if (sifa == NULL)
  2558. continue;
  2559. atomic_add_int(&sifa->refcount, 1);
  2560. return (sifa);
  2561. }
  2562. if (resettotop == 0) {
  2563. inp->next_addr_touse = NULL;
  2564. goto once_again;
  2565. }
  2566. inp->next_addr_touse = starting_point;
  2567. resettotop = 0;
  2568. once_again_too:
  2569. if (inp->next_addr_touse == NULL) {
  2570. inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
  2571. resettotop = 1;
  2572. }
  2573. /* ok, what about an acceptable address in the inp */
  2574. for (laddr = inp->next_addr_touse; laddr;
  2575. laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
  2576. if (laddr->ifa == NULL) {
  2577. /* address has been removed */
  2578. continue;
  2579. }
  2580. if (laddr->action == SCTP_DEL_IP_ADDRESS) {
  2581. /* address is being deleted */
  2582. continue;
  2583. }
  2584. sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
  2585. dest_is_priv, fam);
  2586. if (sifa == NULL)
  2587. continue;
  2588. atomic_add_int(&sifa->refcount, 1);
  2589. return (sifa);
  2590. }
  2591. if (resettotop == 0) {
  2592. inp->next_addr_touse = NULL;
  2593. goto once_again_too;
  2594. }
  2595. /*
  2596. * no address bound can be a source for the destination we are in
  2597. * trouble
  2598. */
  2599. return (NULL);
  2600. }
  2601. static struct sctp_ifa *
  2602. sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
  2603. struct sctp_tcb *stcb,
  2604. sctp_route_t *ro,
  2605. uint32_t vrf_id,
  2606. uint8_t dest_is_priv,
  2607. uint8_t dest_is_loop,
  2608. int non_asoc_addr_ok,
  2609. sa_family_t fam)
  2610. {
  2611. struct sctp_laddr *laddr, *starting_point;
  2612. void *ifn;
  2613. struct sctp_ifn *sctp_ifn;
  2614. struct sctp_ifa *sctp_ifa, *sifa;
  2615. uint8_t start_at_beginning = 0;
  2616. struct sctp_vrf *vrf;
  2617. uint32_t ifn_index;
  2618. /*
  2619. * first question, is the ifn we will emit on in our list, if so, we
  2620. * want that one.
  2621. */
  2622. vrf = sctp_find_vrf(vrf_id);
  2623. if (vrf == NULL)
  2624. return (NULL);
  2625. ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
  2626. ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
  2627. sctp_ifn = sctp_find_ifn(ifn, ifn_index);
  2628. /*
  2629. * first question, is the ifn we will emit on in our list? If so,
  2630. * we want that one. First we look for a preferred. Second, we go
  2631. * for an acceptable.
  2632. */
  2633. if (sctp_ifn) {
  2634. /* first try for a preferred address on the ep */
  2635. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  2636. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2637. #ifdef INET
  2638. if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
  2639. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2640. &sctp_ifa->address.sin.sin_addr) != 0)) {
  2641. continue;
  2642. }
  2643. #endif
  2644. #ifdef INET6
  2645. if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
  2646. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2647. &sctp_ifa->address.sin6.sin6_addr) != 0)) {
  2648. continue;
  2649. }
  2650. #endif
  2651. #endif
  2652. if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
  2653. continue;
  2654. if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
  2655. sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
  2656. if (sifa == NULL)
  2657. continue;
  2658. if (((non_asoc_addr_ok == 0) &&
  2659. (sctp_is_addr_restricted(stcb, sifa))) ||
  2660. (non_asoc_addr_ok &&
  2661. (sctp_is_addr_restricted(stcb, sifa)) &&
  2662. (!sctp_is_addr_pending(stcb, sifa)))) {
  2663. /* on the no-no list */
  2664. continue;
  2665. }
  2666. atomic_add_int(&sifa->refcount, 1);
  2667. return (sifa);
  2668. }
  2669. }
  2670. /* next try for an acceptable address on the ep */
  2671. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  2672. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2673. #ifdef INET
  2674. if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
  2675. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2676. &sctp_ifa->address.sin.sin_addr) != 0)) {
  2677. continue;
  2678. }
  2679. #endif
  2680. #ifdef INET6
  2681. if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
  2682. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2683. &sctp_ifa->address.sin6.sin6_addr) != 0)) {
  2684. continue;
  2685. }
  2686. #endif
  2687. #endif
  2688. if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
  2689. continue;
  2690. if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
  2691. sifa= sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv,fam);
  2692. if (sifa == NULL)
  2693. continue;
  2694. if (((non_asoc_addr_ok == 0) &&
  2695. (sctp_is_addr_restricted(stcb, sifa))) ||
  2696. (non_asoc_addr_ok &&
  2697. (sctp_is_addr_restricted(stcb, sifa)) &&
  2698. (!sctp_is_addr_pending(stcb, sifa)))) {
  2699. /* on the no-no list */
  2700. continue;
  2701. }
  2702. atomic_add_int(&sifa->refcount, 1);
  2703. return (sifa);
  2704. }
  2705. }
  2706. }
  2707. /*
  2708. * if we can't find one like that then we must look at all
  2709. * addresses bound to pick one at first preferable then
  2710. * secondly acceptable.
  2711. */
  2712. starting_point = stcb->asoc.last_used_address;
  2713. sctp_from_the_top:
  2714. if (stcb->asoc.last_used_address == NULL) {
  2715. start_at_beginning = 1;
  2716. stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
  2717. }
  2718. /* search beginning with the last used address */
  2719. for (laddr = stcb->asoc.last_used_address; laddr;
  2720. laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
  2721. if (laddr->ifa == NULL) {
  2722. /* address has been removed */
  2723. continue;
  2724. }
  2725. if (laddr->action == SCTP_DEL_IP_ADDRESS) {
  2726. /* address is being deleted */
  2727. continue;
  2728. }
  2729. sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
  2730. if (sifa == NULL)
  2731. continue;
  2732. if (((non_asoc_addr_ok == 0) &&
  2733. (sctp_is_addr_restricted(stcb, sifa))) ||
  2734. (non_asoc_addr_ok &&
  2735. (sctp_is_addr_restricted(stcb, sifa)) &&
  2736. (!sctp_is_addr_pending(stcb, sifa)))) {
  2737. /* on the no-no list */
  2738. continue;
  2739. }
  2740. stcb->asoc.last_used_address = laddr;
  2741. atomic_add_int(&sifa->refcount, 1);
  2742. return (sifa);
  2743. }
  2744. if (start_at_beginning == 0) {
  2745. stcb->asoc.last_used_address = NULL;
  2746. goto sctp_from_the_top;
  2747. }
  2748. /* now try for any higher scope than the destination */
  2749. stcb->asoc.last_used_address = starting_point;
  2750. start_at_beginning = 0;
  2751. sctp_from_the_top2:
  2752. if (stcb->asoc.last_used_address == NULL) {
  2753. start_at_beginning = 1;
  2754. stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
  2755. }
  2756. /* search beginning with the last used address */
  2757. for (laddr = stcb->asoc.last_used_address; laddr;
  2758. laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
  2759. if (laddr->ifa == NULL) {
  2760. /* address has been removed */
  2761. continue;
  2762. }
  2763. if (laddr->action == SCTP_DEL_IP_ADDRESS) {
  2764. /* address is being deleted */
  2765. continue;
  2766. }
  2767. sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
  2768. dest_is_priv, fam);
  2769. if (sifa == NULL)
  2770. continue;
  2771. if (((non_asoc_addr_ok == 0) &&
  2772. (sctp_is_addr_restricted(stcb, sifa))) ||
  2773. (non_asoc_addr_ok &&
  2774. (sctp_is_addr_restricted(stcb, sifa)) &&
  2775. (!sctp_is_addr_pending(stcb, sifa)))) {
  2776. /* on the no-no list */
  2777. continue;
  2778. }
  2779. stcb->asoc.last_used_address = laddr;
  2780. atomic_add_int(&sifa->refcount, 1);
  2781. return (sifa);
  2782. }
  2783. if (start_at_beginning == 0) {
  2784. stcb->asoc.last_used_address = NULL;
  2785. goto sctp_from_the_top2;
  2786. }
  2787. return (NULL);
  2788. }
  2789. static struct sctp_ifa *
  2790. sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
  2791. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2792. struct sctp_inpcb *inp,
  2793. #else
  2794. struct sctp_inpcb *inp SCTP_UNUSED,
  2795. #endif
  2796. struct sctp_tcb *stcb,
  2797. int non_asoc_addr_ok,
  2798. uint8_t dest_is_loop,
  2799. uint8_t dest_is_priv,
  2800. int addr_wanted,
  2801. sa_family_t fam,
  2802. sctp_route_t *ro)
  2803. {
  2804. struct sctp_ifa *ifa, *sifa;
  2805. int num_eligible_addr = 0;
  2806. #ifdef INET6
  2807. #ifdef SCTP_EMBEDDED_V6_SCOPE
  2808. struct sockaddr_in6 sin6, lsa6;
  2809. if (fam == AF_INET6) {
  2810. memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
  2811. #ifdef SCTP_KAME
  2812. (void)sa6_recoverscope(&sin6);
  2813. #else
  2814. (void)in6_recoverscope(&sin6, &sin6.sin6_addr, NULL);
  2815. #endif /* SCTP_KAME */
  2816. }
  2817. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  2818. #endif /* INET6 */
  2819. LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
  2820. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2821. #ifdef INET
  2822. if ((ifa->address.sa.sa_family == AF_INET) &&
  2823. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2824. &ifa->address.sin.sin_addr) != 0)) {
  2825. continue;
  2826. }
  2827. #endif
  2828. #ifdef INET6
  2829. if ((ifa->address.sa.sa_family == AF_INET6) &&
  2830. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2831. &ifa->address.sin6.sin6_addr) != 0)) {
  2832. continue;
  2833. }
  2834. #endif
  2835. #endif
  2836. if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
  2837. (non_asoc_addr_ok == 0))
  2838. continue;
  2839. sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
  2840. dest_is_priv, fam);
  2841. if (sifa == NULL)
  2842. continue;
  2843. #ifdef INET6
  2844. if (fam == AF_INET6 &&
  2845. dest_is_loop &&
  2846. sifa->src_is_loop && sifa->src_is_priv) {
  2847. /* don't allow fe80::1 to be a src on loop ::1, we don't list it
  2848. * to the peer so we will get an abort.
  2849. */
  2850. continue;
  2851. }
  2852. #ifdef SCTP_EMBEDDED_V6_SCOPE
  2853. if (fam == AF_INET6 &&
  2854. IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
  2855. IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
  2856. /* link-local <-> link-local must belong to the same scope. */
  2857. memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
  2858. #ifdef SCTP_KAME
  2859. (void)sa6_recoverscope(&lsa6);
  2860. #else
  2861. (void)in6_recoverscope(&lsa6, &lsa6.sin6_addr, NULL);
  2862. #endif /* SCTP_KAME */
  2863. if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
  2864. continue;
  2865. }
  2866. }
  2867. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  2868. #endif /* INET6 */
  2869. #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
  2870. /* Check if the IPv6 address matches to next-hop.
  2871. In the mobile case, old IPv6 address may be not deleted
  2872. from the interface. Then, the interface has previous and
  2873. new addresses. We should use one corresponding to the
  2874. next-hop. (by micchie)
  2875. */
  2876. #ifdef INET6
  2877. if (stcb && fam == AF_INET6 &&
  2878. sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
  2879. if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
  2880. continue;
  2881. }
  2882. }
  2883. #endif
  2884. #ifdef INET
  2885. /* Avoid topologically incorrect IPv4 address */
  2886. if (stcb && fam == AF_INET &&
  2887. sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
  2888. if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
  2889. continue;
  2890. }
  2891. }
  2892. #endif
  2893. #endif
  2894. if (stcb) {
  2895. if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
  2896. continue;
  2897. }
  2898. if (((non_asoc_addr_ok == 0) &&
  2899. (sctp_is_addr_restricted(stcb, sifa))) ||
  2900. (non_asoc_addr_ok &&
  2901. (sctp_is_addr_restricted(stcb, sifa)) &&
  2902. (!sctp_is_addr_pending(stcb, sifa)))) {
  2903. /*
  2904. * It is restricted for some reason..
  2905. * probably not yet added.
  2906. */
  2907. continue;
  2908. }
  2909. }
  2910. if (num_eligible_addr >= addr_wanted) {
  2911. return (sifa);
  2912. }
  2913. num_eligible_addr++;
  2914. }
  2915. return (NULL);
  2916. }
  2917. static int
  2918. sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
  2919. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2920. struct sctp_inpcb *inp,
  2921. #else
  2922. struct sctp_inpcb *inp SCTP_UNUSED,
  2923. #endif
  2924. struct sctp_tcb *stcb,
  2925. int non_asoc_addr_ok,
  2926. uint8_t dest_is_loop,
  2927. uint8_t dest_is_priv,
  2928. sa_family_t fam)
  2929. {
  2930. struct sctp_ifa *ifa, *sifa;
  2931. int num_eligible_addr = 0;
  2932. LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
  2933. #if defined(__FreeBSD__) && !defined(__Userspace__)
  2934. #ifdef INET
  2935. if ((ifa->address.sa.sa_family == AF_INET) &&
  2936. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  2937. &ifa->address.sin.sin_addr) != 0)) {
  2938. continue;
  2939. }
  2940. #endif
  2941. #ifdef INET6
  2942. if ((ifa->address.sa.sa_family == AF_INET6) &&
  2943. (stcb != NULL) &&
  2944. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  2945. &ifa->address.sin6.sin6_addr) != 0)) {
  2946. continue;
  2947. }
  2948. #endif
  2949. #endif
  2950. if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
  2951. (non_asoc_addr_ok == 0)) {
  2952. continue;
  2953. }
  2954. sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
  2955. dest_is_priv, fam);
  2956. if (sifa == NULL) {
  2957. continue;
  2958. }
  2959. if (stcb) {
  2960. if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
  2961. continue;
  2962. }
  2963. if (((non_asoc_addr_ok == 0) &&
  2964. (sctp_is_addr_restricted(stcb, sifa))) ||
  2965. (non_asoc_addr_ok &&
  2966. (sctp_is_addr_restricted(stcb, sifa)) &&
  2967. (!sctp_is_addr_pending(stcb, sifa)))) {
  2968. /*
  2969. * It is restricted for some reason..
  2970. * probably not yet added.
  2971. */
  2972. continue;
  2973. }
  2974. }
  2975. num_eligible_addr++;
  2976. }
  2977. return (num_eligible_addr);
  2978. }
  2979. static struct sctp_ifa *
  2980. sctp_choose_boundall(struct sctp_inpcb *inp,
  2981. struct sctp_tcb *stcb,
  2982. struct sctp_nets *net,
  2983. sctp_route_t *ro,
  2984. uint32_t vrf_id,
  2985. uint8_t dest_is_priv,
  2986. uint8_t dest_is_loop,
  2987. int non_asoc_addr_ok,
  2988. sa_family_t fam)
  2989. {
  2990. int cur_addr_num = 0, num_preferred = 0;
  2991. void *ifn;
  2992. struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
  2993. struct sctp_ifa *sctp_ifa, *sifa;
  2994. uint32_t ifn_index;
  2995. struct sctp_vrf *vrf;
  2996. #ifdef INET
  2997. int retried = 0;
  2998. #endif
  2999. /*-
  3000. * For boundall we can use any address in the association.
  3001. * If non_asoc_addr_ok is set we can use any address (at least in
  3002. * theory). So we look for preferred addresses first. If we find one,
  3003. * we use it. Otherwise we next try to get an address on the
  3004. * interface, which we should be able to do (unless non_asoc_addr_ok
  3005. * is false and we are routed out that way). In these cases where we
  3006. * can't use the address of the interface we go through all the
  3007. * ifn's looking for an address we can use and fill that in. Punting
  3008. * means we send back address 0, which will probably cause problems
  3009. * actually since then IP will fill in the address of the route ifn,
  3010. * which means we probably already rejected it.. i.e. here comes an
  3011. * abort :-<.
  3012. */
  3013. vrf = sctp_find_vrf(vrf_id);
  3014. if (vrf == NULL)
  3015. return (NULL);
  3016. ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
  3017. ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
  3018. SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
  3019. emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
  3020. if (sctp_ifn == NULL) {
  3021. /* ?? We don't have this guy ?? */
  3022. SCTPDBG(SCTP_DEBUG_OUTPUT2,"No ifn emit interface?\n");
  3023. goto bound_all_plan_b;
  3024. }
  3025. SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn_index:%d name:%s is emit interface\n",
  3026. ifn_index, sctp_ifn->ifn_name);
  3027. if (net) {
  3028. cur_addr_num = net->indx_of_eligible_next_to_use;
  3029. }
  3030. num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
  3031. inp, stcb,
  3032. non_asoc_addr_ok,
  3033. dest_is_loop,
  3034. dest_is_priv, fam);
  3035. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
  3036. num_preferred, sctp_ifn->ifn_name);
  3037. if (num_preferred == 0) {
  3038. /*
  3039. * no eligible addresses, we must use some other interface
  3040. * address if we can find one.
  3041. */
  3042. goto bound_all_plan_b;
  3043. }
  3044. /*
  3045. * Ok we have num_eligible_addr set with how many we can use, this
  3046. * may vary from call to call due to addresses being deprecated
  3047. * etc..
  3048. */
  3049. if (cur_addr_num >= num_preferred) {
  3050. cur_addr_num = 0;
  3051. }
  3052. /*
  3053. * select the nth address from the list (where cur_addr_num is the
  3054. * nth) and 0 is the first one, 1 is the second one etc...
  3055. */
  3056. SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
  3057. sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
  3058. dest_is_priv, cur_addr_num, fam, ro);
  3059. /* if sctp_ifa is NULL something changed??, fall to plan b. */
  3060. if (sctp_ifa) {
  3061. atomic_add_int(&sctp_ifa->refcount, 1);
  3062. if (net) {
  3063. /* save off where the next one we will want */
  3064. net->indx_of_eligible_next_to_use = cur_addr_num + 1;
  3065. }
  3066. return (sctp_ifa);
  3067. }
  3068. /*
  3069. * plan_b: Look at all interfaces and find a preferred address. If
  3070. * no preferred fall through to plan_c.
  3071. */
  3072. bound_all_plan_b:
  3073. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
  3074. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  3075. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
  3076. sctp_ifn->ifn_name);
  3077. if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  3078. /* wrong base scope */
  3079. SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
  3080. continue;
  3081. }
  3082. if ((sctp_ifn == looked_at) && looked_at) {
  3083. /* already looked at this guy */
  3084. SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
  3085. continue;
  3086. }
  3087. num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
  3088. dest_is_loop, dest_is_priv, fam);
  3089. SCTPDBG(SCTP_DEBUG_OUTPUT2,
  3090. "Found ifn:%p %d preferred source addresses\n",
  3091. ifn, num_preferred);
  3092. if (num_preferred == 0) {
  3093. /* None on this interface. */
  3094. SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
  3095. continue;
  3096. }
  3097. SCTPDBG(SCTP_DEBUG_OUTPUT2,
  3098. "num preferred:%d on interface:%p cur_addr_num:%d\n",
  3099. num_preferred, (void *)sctp_ifn, cur_addr_num);
  3100. /*
  3101. * Ok we have num_eligible_addr set with how many we can
  3102. * use, this may vary from call to call due to addresses
  3103. * being deprecated etc..
  3104. */
  3105. if (cur_addr_num >= num_preferred) {
  3106. cur_addr_num = 0;
  3107. }
  3108. sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
  3109. dest_is_priv, cur_addr_num, fam, ro);
  3110. if (sifa == NULL)
  3111. continue;
  3112. if (net) {
  3113. net->indx_of_eligible_next_to_use = cur_addr_num + 1;
  3114. SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
  3115. cur_addr_num);
  3116. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
  3117. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
  3118. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
  3119. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
  3120. }
  3121. atomic_add_int(&sifa->refcount, 1);
  3122. return (sifa);
  3123. }
  3124. #ifdef INET
  3125. again_with_private_addresses_allowed:
  3126. #endif
  3127. /* plan_c: do we have an acceptable address on the emit interface */
  3128. sifa = NULL;
  3129. SCTPDBG(SCTP_DEBUG_OUTPUT2,"Trying Plan C: find acceptable on interface\n");
  3130. if (emit_ifn == NULL) {
  3131. SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jump to Plan D - no emit_ifn\n");
  3132. goto plan_d;
  3133. }
  3134. LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
  3135. SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
  3136. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3137. #ifdef INET
  3138. if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
  3139. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  3140. &sctp_ifa->address.sin.sin_addr) != 0)) {
  3141. SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n");
  3142. continue;
  3143. }
  3144. #endif
  3145. #ifdef INET6
  3146. if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
  3147. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  3148. &sctp_ifa->address.sin6.sin6_addr) != 0)) {
  3149. SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n");
  3150. continue;
  3151. }
  3152. #endif
  3153. #endif
  3154. if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
  3155. (non_asoc_addr_ok == 0)) {
  3156. SCTPDBG(SCTP_DEBUG_OUTPUT2,"Defer\n");
  3157. continue;
  3158. }
  3159. sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
  3160. dest_is_priv, fam);
  3161. if (sifa == NULL) {
  3162. SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
  3163. continue;
  3164. }
  3165. if (stcb) {
  3166. if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
  3167. SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
  3168. sifa = NULL;
  3169. continue;
  3170. }
  3171. if (((non_asoc_addr_ok == 0) &&
  3172. (sctp_is_addr_restricted(stcb, sifa))) ||
  3173. (non_asoc_addr_ok &&
  3174. (sctp_is_addr_restricted(stcb, sifa)) &&
  3175. (!sctp_is_addr_pending(stcb, sifa)))) {
  3176. /*
  3177. * It is restricted for some
  3178. * reason.. probably not yet added.
  3179. */
  3180. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
  3181. sifa = NULL;
  3182. continue;
  3183. }
  3184. }
  3185. atomic_add_int(&sifa->refcount, 1);
  3186. goto out;
  3187. }
  3188. plan_d:
  3189. /*
  3190. * plan_d: We are in trouble. No preferred address on the emit
  3191. * interface. And not even a preferred address on all interfaces.
  3192. * Go out and see if we can find an acceptable address somewhere
  3193. * amongst all interfaces.
  3194. */
  3195. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
  3196. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  3197. if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  3198. /* wrong base scope */
  3199. continue;
  3200. }
  3201. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  3202. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3203. #ifdef INET
  3204. if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
  3205. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  3206. &sctp_ifa->address.sin.sin_addr) != 0)) {
  3207. continue;
  3208. }
  3209. #endif
  3210. #ifdef INET6
  3211. if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
  3212. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  3213. &sctp_ifa->address.sin6.sin6_addr) != 0)) {
  3214. continue;
  3215. }
  3216. #endif
  3217. #endif
  3218. if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
  3219. (non_asoc_addr_ok == 0))
  3220. continue;
  3221. sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
  3222. dest_is_loop,
  3223. dest_is_priv, fam);
  3224. if (sifa == NULL)
  3225. continue;
  3226. if (stcb) {
  3227. if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
  3228. sifa = NULL;
  3229. continue;
  3230. }
  3231. if (((non_asoc_addr_ok == 0) &&
  3232. (sctp_is_addr_restricted(stcb, sifa))) ||
  3233. (non_asoc_addr_ok &&
  3234. (sctp_is_addr_restricted(stcb, sifa)) &&
  3235. (!sctp_is_addr_pending(stcb, sifa)))) {
  3236. /*
  3237. * It is restricted for some
  3238. * reason.. probably not yet added.
  3239. */
  3240. sifa = NULL;
  3241. continue;
  3242. }
  3243. }
  3244. goto out;
  3245. }
  3246. }
  3247. #ifdef INET
  3248. if (stcb) {
  3249. if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
  3250. stcb->asoc.scope.ipv4_local_scope = 1;
  3251. retried = 1;
  3252. goto again_with_private_addresses_allowed;
  3253. } else if (retried == 1) {
  3254. stcb->asoc.scope.ipv4_local_scope = 0;
  3255. }
  3256. }
  3257. #endif
  3258. out:
  3259. #ifdef INET
  3260. if (sifa) {
  3261. if (retried == 1) {
  3262. LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
  3263. if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
  3264. /* wrong base scope */
  3265. continue;
  3266. }
  3267. LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
  3268. struct sctp_ifa *tmp_sifa;
  3269. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3270. #ifdef INET
  3271. if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
  3272. (prison_check_ip4(inp->ip_inp.inp.inp_cred,
  3273. &sctp_ifa->address.sin.sin_addr) != 0)) {
  3274. continue;
  3275. }
  3276. #endif
  3277. #ifdef INET6
  3278. if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
  3279. (prison_check_ip6(inp->ip_inp.inp.inp_cred,
  3280. &sctp_ifa->address.sin6.sin6_addr) != 0)) {
  3281. continue;
  3282. }
  3283. #endif
  3284. #endif
  3285. if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
  3286. (non_asoc_addr_ok == 0))
  3287. continue;
  3288. tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
  3289. dest_is_loop,
  3290. dest_is_priv, fam);
  3291. if (tmp_sifa == NULL) {
  3292. continue;
  3293. }
  3294. if (tmp_sifa == sifa) {
  3295. continue;
  3296. }
  3297. if (stcb) {
  3298. if (sctp_is_address_in_scope(tmp_sifa,
  3299. &stcb->asoc.scope, 0) == 0) {
  3300. continue;
  3301. }
  3302. if (((non_asoc_addr_ok == 0) &&
  3303. (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
  3304. (non_asoc_addr_ok &&
  3305. (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
  3306. (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
  3307. /*
  3308. * It is restricted for some
  3309. * reason.. probably not yet added.
  3310. */
  3311. continue;
  3312. }
  3313. }
  3314. if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
  3315. (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
  3316. sctp_add_local_addr_restricted(stcb, tmp_sifa);
  3317. }
  3318. }
  3319. }
  3320. }
  3321. atomic_add_int(&sifa->refcount, 1);
  3322. }
  3323. #endif
  3324. return (sifa);
  3325. }
  3326. /* tcb may be NULL */
  3327. struct sctp_ifa *
  3328. sctp_source_address_selection(struct sctp_inpcb *inp,
  3329. struct sctp_tcb *stcb,
  3330. sctp_route_t *ro,
  3331. struct sctp_nets *net,
  3332. int non_asoc_addr_ok, uint32_t vrf_id)
  3333. {
  3334. struct sctp_ifa *answer;
  3335. uint8_t dest_is_priv, dest_is_loop;
  3336. sa_family_t fam;
  3337. #ifdef INET
  3338. struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
  3339. #endif
  3340. #ifdef INET6
  3341. struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
  3342. #endif
  3343. /**
  3344. * Rules:
  3345. * - Find the route if needed, cache if I can.
  3346. * - Look at interface address in route, Is it in the bound list. If so we
  3347. * have the best source.
  3348. * - If not we must rotate amongst the addresses.
  3349. *
  3350. * Caveats and issues
  3351. *
  3352. * Do we need to pay attention to scope. We can have a private address
  3353. * or a global address we are sourcing or sending to. So if we draw
  3354. * it out
  3355. * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
  3356. * For V4
  3357. * ------------------------------------------
  3358. * source * dest * result
  3359. * -----------------------------------------
  3360. * <a> Private * Global * NAT
  3361. * -----------------------------------------
  3362. * <b> Private * Private * No problem
  3363. * -----------------------------------------
  3364. * <c> Global * Private * Huh, How will this work?
  3365. * -----------------------------------------
  3366. * <d> Global * Global * No Problem
  3367. *------------------------------------------
  3368. * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
  3369. * For V6
  3370. *------------------------------------------
  3371. * source * dest * result
  3372. * -----------------------------------------
  3373. * <a> Linklocal * Global *
  3374. * -----------------------------------------
  3375. * <b> Linklocal * Linklocal * No problem
  3376. * -----------------------------------------
  3377. * <c> Global * Linklocal * Huh, How will this work?
  3378. * -----------------------------------------
  3379. * <d> Global * Global * No Problem
  3380. *------------------------------------------
  3381. * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
  3382. *
  3383. * And then we add to that what happens if there are multiple addresses
  3384. * assigned to an interface. Remember the ifa on a ifn is a linked
  3385. * list of addresses. So one interface can have more than one IP
  3386. * address. What happens if we have both a private and a global
  3387. * address? Do we then use context of destination to sort out which
  3388. * one is best? And what about NAT's sending P->G may get you a NAT
  3389. * translation, or should you select the G thats on the interface in
  3390. * preference.
  3391. *
  3392. * Decisions:
  3393. *
  3394. * - count the number of addresses on the interface.
  3395. * - if it is one, no problem except case <c>.
  3396. * For <a> we will assume a NAT out there.
  3397. * - if there are more than one, then we need to worry about scope P
  3398. * or G. We should prefer G -> G and P -> P if possible.
  3399. * Then as a secondary fall back to mixed types G->P being a last
  3400. * ditch one.
  3401. * - The above all works for bound all, but bound specific we need to
  3402. * use the same concept but instead only consider the bound
  3403. * addresses. If the bound set is NOT assigned to the interface then
  3404. * we must use rotation amongst the bound addresses..
  3405. */
  3406. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3407. if (ro->ro_nh == NULL) {
  3408. #else
  3409. if (ro->ro_rt == NULL) {
  3410. #endif
  3411. /*
  3412. * Need a route to cache.
  3413. */
  3414. SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
  3415. }
  3416. #if defined(__FreeBSD__) && !defined(__Userspace__)
  3417. if (ro->ro_nh == NULL) {
  3418. #else
  3419. if (ro->ro_rt == NULL) {
  3420. #endif
  3421. return (NULL);
  3422. }
  3423. #if defined(_WIN32)
  3424. /* On Windows the sa_family is U_SHORT or ADDRESS_FAMILY */
  3425. fam = (sa_family_t)ro->ro_dst.sa_family;
  3426. #else
  3427. fam = ro->ro_dst.sa_family;
  3428. #endif
  3429. dest_is_priv = dest_is_loop = 0;
  3430. /* Setup our scopes for the destination */
  3431. switch (fam) {
  3432. #ifdef INET
  3433. case AF_INET:
  3434. /* Scope based on outbound address */
  3435. if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
  3436. dest_is_loop = 1;
  3437. if (net != NULL) {
  3438. /* mark it as local */
  3439. net->addr_is_local = 1;
  3440. }
  3441. } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
  3442. dest_is_priv = 1;
  3443. }
  3444. break;
  3445. #endif
  3446. #ifdef INET6
  3447. case AF_INET6:
  3448. /* Scope based on outbound address */
  3449. #if defined(_WIN32)
  3450. if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
  3451. #else
  3452. if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
  3453. SCTP_ROUTE_IS_REAL_LOOP(ro)) {
  3454. #endif
  3455. /*
  3456. * If the address is a loopback address, which
  3457. * consists of "::1" OR "fe80::1%lo0", we are loopback
  3458. * scope. But we don't use dest_is_priv (link local
  3459. * addresses).
  3460. */
  3461. dest_is_loop = 1;
  3462. if (net != NULL) {
  3463. /* mark it as local */
  3464. net->addr_is_local = 1;
  3465. }
  3466. } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
  3467. dest_is_priv = 1;
  3468. }
  3469. break;
  3470. #endif
  3471. }
  3472. SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
  3473. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
  3474. SCTP_IPI_ADDR_RLOCK();
  3475. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
  3476. /*
  3477. * Bound all case
  3478. */
  3479. answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
  3480. dest_is_priv, dest_is_loop,
  3481. non_asoc_addr_ok, fam);
  3482. SCTP_IPI_ADDR_RUNLOCK();
  3483. return (answer);
  3484. }
  3485. /*
  3486. * Subset bound case
  3487. */
  3488. if (stcb) {
  3489. answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
  3490. vrf_id, dest_is_priv,
  3491. dest_is_loop,
  3492. non_asoc_addr_ok, fam);
  3493. } else {
  3494. answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
  3495. non_asoc_addr_ok,
  3496. dest_is_priv,
  3497. dest_is_loop, fam);
  3498. }
  3499. SCTP_IPI_ADDR_RUNLOCK();
  3500. return (answer);
  3501. }
  3502. static bool
  3503. sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
  3504. {
  3505. #if defined(_WIN32)
  3506. WSACMSGHDR cmh;
  3507. #else
  3508. struct cmsghdr cmh;
  3509. #endif
  3510. struct sctp_sndinfo sndinfo;
  3511. struct sctp_prinfo prinfo;
  3512. struct sctp_authinfo authinfo;
  3513. int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
  3514. bool found;
  3515. /*
  3516. * Independent of how many mbufs, find the c_type inside the control
  3517. * structure and copy out the data.
  3518. */
  3519. found = false;
  3520. tot_len = SCTP_BUF_LEN(control);
  3521. for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
  3522. rem_len = tot_len - off;
  3523. if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
  3524. /* There is not enough room for one more. */
  3525. return (found);
  3526. }
  3527. m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
  3528. if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
  3529. /* We dont't have a complete CMSG header. */
  3530. return (found);
  3531. }
  3532. if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
  3533. /* We don't have the complete CMSG. */
  3534. return (found);
  3535. }
  3536. cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
  3537. cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
  3538. if ((cmh.cmsg_level == IPPROTO_SCTP) &&
  3539. ((c_type == cmh.cmsg_type) ||
  3540. ((c_type == SCTP_SNDRCV) &&
  3541. ((cmh.cmsg_type == SCTP_SNDINFO) ||
  3542. (cmh.cmsg_type == SCTP_PRINFO) ||
  3543. (cmh.cmsg_type == SCTP_AUTHINFO))))) {
  3544. if (c_type == cmh.cmsg_type) {
  3545. if (cpsize > INT_MAX) {
  3546. return (found);
  3547. }
  3548. if (cmsg_data_len < (int)cpsize) {
  3549. return (found);
  3550. }
  3551. /* It is exactly what we want. Copy it out. */
  3552. m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
  3553. return (1);
  3554. } else {
  3555. struct sctp_sndrcvinfo *sndrcvinfo;
  3556. sndrcvinfo = (struct sctp_sndrcvinfo *)data;
  3557. if (!found) {
  3558. if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
  3559. return (found);
  3560. }
  3561. memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
  3562. }
  3563. switch (cmh.cmsg_type) {
  3564. case SCTP_SNDINFO:
  3565. if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
  3566. return (found);
  3567. }
  3568. m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
  3569. sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
  3570. sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
  3571. sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
  3572. sndrcvinfo->sinfo_context = sndinfo.snd_context;
  3573. sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
  3574. break;
  3575. case SCTP_PRINFO:
  3576. if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
  3577. return (found);
  3578. }
  3579. m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
  3580. if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
  3581. sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
  3582. } else {
  3583. sndrcvinfo->sinfo_timetolive = 0;
  3584. }
  3585. sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
  3586. break;
  3587. case SCTP_AUTHINFO:
  3588. if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
  3589. return (found);
  3590. }
  3591. m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
  3592. sndrcvinfo->sinfo_keynumber_valid = 1;
  3593. sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
  3594. break;
  3595. default:
  3596. return (found);
  3597. }
  3598. found = true;
  3599. }
  3600. }
  3601. }
  3602. return (found);
  3603. }
  3604. static int
  3605. sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
  3606. {
  3607. #if defined(_WIN32)
  3608. WSACMSGHDR cmh;
  3609. #else
  3610. struct cmsghdr cmh;
  3611. #endif
  3612. struct sctp_initmsg initmsg;
  3613. #ifdef INET
  3614. struct sockaddr_in sin;
  3615. #endif
  3616. #ifdef INET6
  3617. struct sockaddr_in6 sin6;
  3618. #endif
  3619. int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
  3620. tot_len = SCTP_BUF_LEN(control);
  3621. for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
  3622. rem_len = tot_len - off;
  3623. if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
  3624. /* There is not enough room for one more. */
  3625. *error = EINVAL;
  3626. return (1);
  3627. }
  3628. m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
  3629. if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
  3630. /* We dont't have a complete CMSG header. */
  3631. *error = EINVAL;
  3632. return (1);
  3633. }
  3634. if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
  3635. /* We don't have the complete CMSG. */
  3636. *error = EINVAL;
  3637. return (1);
  3638. }
  3639. cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
  3640. cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
  3641. if (cmh.cmsg_level == IPPROTO_SCTP) {
  3642. switch (cmh.cmsg_type) {
  3643. case SCTP_INIT:
  3644. if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
  3645. *error = EINVAL;
  3646. return (1);
  3647. }
  3648. m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
  3649. if (initmsg.sinit_max_attempts)
  3650. stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
  3651. if (initmsg.sinit_num_ostreams)
  3652. stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
  3653. if (initmsg.sinit_max_instreams)
  3654. stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
  3655. if (initmsg.sinit_max_init_timeo)
  3656. stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
  3657. if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
  3658. struct sctp_stream_out *tmp_str;
  3659. unsigned int i;
  3660. #if defined(SCTP_DETAILED_STR_STATS)
  3661. int j;
  3662. #endif
  3663. /* Default is NOT correct */
  3664. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
  3665. stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
  3666. SCTP_TCB_UNLOCK(stcb);
  3667. SCTP_MALLOC(tmp_str,
  3668. struct sctp_stream_out *,
  3669. (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
  3670. SCTP_M_STRMO);
  3671. SCTP_TCB_LOCK(stcb);
  3672. if (tmp_str != NULL) {
  3673. SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
  3674. stcb->asoc.strmout = tmp_str;
  3675. stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
  3676. } else {
  3677. stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
  3678. }
  3679. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  3680. TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
  3681. stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
  3682. stcb->asoc.strmout[i].chunks_on_queues = 0;
  3683. #if defined(SCTP_DETAILED_STR_STATS)
  3684. for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
  3685. stcb->asoc.strmout[i].abandoned_sent[j] = 0;
  3686. stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
  3687. }
  3688. #else
  3689. stcb->asoc.strmout[i].abandoned_sent[0] = 0;
  3690. stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
  3691. #endif
  3692. stcb->asoc.strmout[i].next_mid_ordered = 0;
  3693. stcb->asoc.strmout[i].next_mid_unordered = 0;
  3694. stcb->asoc.strmout[i].sid = i;
  3695. stcb->asoc.strmout[i].last_msg_incomplete = 0;
  3696. stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
  3697. }
  3698. }
  3699. break;
  3700. #ifdef INET
  3701. case SCTP_DSTADDRV4:
  3702. if (cmsg_data_len < (int)sizeof(struct in_addr)) {
  3703. *error = EINVAL;
  3704. return (1);
  3705. }
  3706. memset(&sin, 0, sizeof(struct sockaddr_in));
  3707. sin.sin_family = AF_INET;
  3708. #ifdef HAVE_SIN_LEN
  3709. sin.sin_len = sizeof(struct sockaddr_in);
  3710. #endif
  3711. sin.sin_port = stcb->rport;
  3712. m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
  3713. if ((sin.sin_addr.s_addr == INADDR_ANY) ||
  3714. (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
  3715. IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
  3716. *error = EINVAL;
  3717. return (1);
  3718. }
  3719. if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
  3720. SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
  3721. *error = ENOBUFS;
  3722. return (1);
  3723. }
  3724. break;
  3725. #endif
  3726. #ifdef INET6
  3727. case SCTP_DSTADDRV6:
  3728. if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
  3729. *error = EINVAL;
  3730. return (1);
  3731. }
  3732. memset(&sin6, 0, sizeof(struct sockaddr_in6));
  3733. sin6.sin6_family = AF_INET6;
  3734. #ifdef HAVE_SIN6_LEN
  3735. sin6.sin6_len = sizeof(struct sockaddr_in6);
  3736. #endif
  3737. sin6.sin6_port = stcb->rport;
  3738. m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
  3739. if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
  3740. IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
  3741. *error = EINVAL;
  3742. return (1);
  3743. }
  3744. #ifdef INET
  3745. if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
  3746. in6_sin6_2_sin(&sin, &sin6);
  3747. if ((sin.sin_addr.s_addr == INADDR_ANY) ||
  3748. (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
  3749. IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
  3750. *error = EINVAL;
  3751. return (1);
  3752. }
  3753. if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
  3754. SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
  3755. *error = ENOBUFS;
  3756. return (1);
  3757. }
  3758. } else
  3759. #endif
  3760. if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
  3761. SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
  3762. *error = ENOBUFS;
  3763. return (1);
  3764. }
  3765. break;
  3766. #endif
  3767. default:
  3768. break;
  3769. }
  3770. }
  3771. }
  3772. return (0);
  3773. }
  3774. #if defined(INET) || defined(INET6)
  3775. static struct sctp_tcb *
  3776. sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
  3777. uint16_t port,
  3778. struct mbuf *control,
  3779. struct sctp_nets **net_p,
  3780. int *error)
  3781. {
  3782. #if defined(_WIN32)
  3783. WSACMSGHDR cmh;
  3784. #else
  3785. struct cmsghdr cmh;
  3786. #endif
  3787. struct sctp_tcb *stcb;
  3788. struct sockaddr *addr;
  3789. #ifdef INET
  3790. struct sockaddr_in sin;
  3791. #endif
  3792. #ifdef INET6
  3793. struct sockaddr_in6 sin6;
  3794. #endif
  3795. int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
  3796. tot_len = SCTP_BUF_LEN(control);
  3797. for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
  3798. rem_len = tot_len - off;
  3799. if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
  3800. /* There is not enough room for one more. */
  3801. *error = EINVAL;
  3802. return (NULL);
  3803. }
  3804. m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
  3805. if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
  3806. /* We dont't have a complete CMSG header. */
  3807. *error = EINVAL;
  3808. return (NULL);
  3809. }
  3810. if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
  3811. /* We don't have the complete CMSG. */
  3812. *error = EINVAL;
  3813. return (NULL);
  3814. }
  3815. cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
  3816. cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
  3817. if (cmh.cmsg_level == IPPROTO_SCTP) {
  3818. switch (cmh.cmsg_type) {
  3819. #ifdef INET
  3820. case SCTP_DSTADDRV4:
  3821. if (cmsg_data_len < (int)sizeof(struct in_addr)) {
  3822. *error = EINVAL;
  3823. return (NULL);
  3824. }
  3825. memset(&sin, 0, sizeof(struct sockaddr_in));
  3826. sin.sin_family = AF_INET;
  3827. #ifdef HAVE_SIN_LEN
  3828. sin.sin_len = sizeof(struct sockaddr_in);
  3829. #endif
  3830. sin.sin_port = port;
  3831. m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
  3832. addr = (struct sockaddr *)&sin;
  3833. break;
  3834. #endif
  3835. #ifdef INET6
  3836. case SCTP_DSTADDRV6:
  3837. if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
  3838. *error = EINVAL;
  3839. return (NULL);
  3840. }
  3841. memset(&sin6, 0, sizeof(struct sockaddr_in6));
  3842. sin6.sin6_family = AF_INET6;
  3843. #ifdef HAVE_SIN6_LEN
  3844. sin6.sin6_len = sizeof(struct sockaddr_in6);
  3845. #endif
  3846. sin6.sin6_port = port;
  3847. m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
  3848. #ifdef INET
  3849. if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
  3850. in6_sin6_2_sin(&sin, &sin6);
  3851. addr = (struct sockaddr *)&sin;
  3852. } else
  3853. #endif
  3854. addr = (struct sockaddr *)&sin6;
  3855. break;
  3856. #endif
  3857. default:
  3858. addr = NULL;
  3859. break;
  3860. }
  3861. if (addr) {
  3862. stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
  3863. if (stcb != NULL) {
  3864. return (stcb);
  3865. }
  3866. }
  3867. }
  3868. }
  3869. return (NULL);
  3870. }
  3871. #endif
  3872. static struct mbuf *
  3873. sctp_add_cookie(struct mbuf *init, int init_offset,
  3874. struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
  3875. {
  3876. struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
  3877. struct sctp_state_cookie *stc;
  3878. struct sctp_paramhdr *ph;
  3879. uint16_t cookie_sz;
  3880. mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
  3881. sizeof(struct sctp_paramhdr)), 0,
  3882. M_NOWAIT, 1, MT_DATA);
  3883. if (mret == NULL) {
  3884. return (NULL);
  3885. }
  3886. copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
  3887. if (copy_init == NULL) {
  3888. sctp_m_freem(mret);
  3889. return (NULL);
  3890. }
  3891. #ifdef SCTP_MBUF_LOGGING
  3892. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  3893. sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
  3894. }
  3895. #endif
  3896. copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
  3897. M_NOWAIT);
  3898. if (copy_initack == NULL) {
  3899. sctp_m_freem(mret);
  3900. sctp_m_freem(copy_init);
  3901. return (NULL);
  3902. }
  3903. #ifdef SCTP_MBUF_LOGGING
  3904. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  3905. sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
  3906. }
  3907. #endif
  3908. /* easy side we just drop it on the end */
  3909. ph = mtod(mret, struct sctp_paramhdr *);
  3910. SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
  3911. sizeof(struct sctp_paramhdr);
  3912. stc = (struct sctp_state_cookie *)((caddr_t)ph +
  3913. sizeof(struct sctp_paramhdr));
  3914. ph->param_type = htons(SCTP_STATE_COOKIE);
  3915. ph->param_length = 0; /* fill in at the end */
  3916. /* Fill in the stc cookie data */
  3917. memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
  3918. /* tack the INIT and then the INIT-ACK onto the chain */
  3919. cookie_sz = 0;
  3920. for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
  3921. cookie_sz += SCTP_BUF_LEN(m_at);
  3922. if (SCTP_BUF_NEXT(m_at) == NULL) {
  3923. SCTP_BUF_NEXT(m_at) = copy_init;
  3924. break;
  3925. }
  3926. }
  3927. for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
  3928. cookie_sz += SCTP_BUF_LEN(m_at);
  3929. if (SCTP_BUF_NEXT(m_at) == NULL) {
  3930. SCTP_BUF_NEXT(m_at) = copy_initack;
  3931. break;
  3932. }
  3933. }
  3934. for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
  3935. cookie_sz += SCTP_BUF_LEN(m_at);
  3936. if (SCTP_BUF_NEXT(m_at) == NULL) {
  3937. break;
  3938. }
  3939. }
  3940. sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
  3941. if (sig == NULL) {
  3942. /* no space, so free the entire chain */
  3943. sctp_m_freem(mret);
  3944. return (NULL);
  3945. }
  3946. SCTP_BUF_NEXT(m_at) = sig;
  3947. SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
  3948. cookie_sz += SCTP_SIGNATURE_SIZE;
  3949. ph->param_length = htons(cookie_sz);
  3950. *signature = (uint8_t *)mtod(sig, caddr_t);
  3951. memset(*signature, 0, SCTP_SIGNATURE_SIZE);
  3952. return (mret);
  3953. }
  3954. static uint8_t
  3955. sctp_get_ect(struct sctp_tcb *stcb)
  3956. {
  3957. if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
  3958. return (SCTP_ECT0_BIT);
  3959. } else {
  3960. return (0);
  3961. }
  3962. }
  3963. #if defined(INET) || defined(INET6)
  3964. static void
  3965. sctp_handle_no_route(struct sctp_tcb *stcb,
  3966. struct sctp_nets *net,
  3967. int so_locked)
  3968. {
  3969. SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
  3970. if (net) {
  3971. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
  3972. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
  3973. if (net->dest_state & SCTP_ADDR_CONFIRMED) {
  3974. if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
  3975. SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
  3976. sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
  3977. stcb, 0,
  3978. (void *)net,
  3979. so_locked);
  3980. net->dest_state &= ~SCTP_ADDR_REACHABLE;
  3981. net->dest_state &= ~SCTP_ADDR_PF;
  3982. }
  3983. }
  3984. if (stcb) {
  3985. if (net == stcb->asoc.primary_destination) {
  3986. /* need a new primary */
  3987. struct sctp_nets *alt;
  3988. alt = sctp_find_alternate_net(stcb, net, 0);
  3989. if (alt != net) {
  3990. if (stcb->asoc.alternate) {
  3991. sctp_free_remote_addr(stcb->asoc.alternate);
  3992. }
  3993. stcb->asoc.alternate = alt;
  3994. atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
  3995. if (net->ro._s_addr) {
  3996. sctp_free_ifa(net->ro._s_addr);
  3997. net->ro._s_addr = NULL;
  3998. }
  3999. net->src_addr_selected = 0;
  4000. }
  4001. }
  4002. }
  4003. }
  4004. }
  4005. #endif
  4006. static int
  4007. sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
  4008. struct sctp_tcb *stcb, /* may be NULL */
  4009. struct sctp_nets *net,
  4010. struct sockaddr *to,
  4011. struct mbuf *m,
  4012. uint32_t auth_offset,
  4013. struct sctp_auth_chunk *auth,
  4014. uint16_t auth_keyid,
  4015. int nofragment_flag,
  4016. int ecn_ok,
  4017. int out_of_asoc_ok,
  4018. uint16_t src_port,
  4019. uint16_t dest_port,
  4020. uint32_t v_tag,
  4021. uint16_t port,
  4022. union sctp_sockstore *over_addr,
  4023. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4024. uint8_t mflowtype, uint32_t mflowid,
  4025. #endif
  4026. int so_locked)
  4027. /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
  4028. {
  4029. /**
  4030. * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
  4031. * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
  4032. * - fill in the HMAC digest of any AUTH chunk in the packet.
  4033. * - calculate and fill in the SCTP checksum.
  4034. * - prepend an IP address header.
  4035. * - if boundall use INADDR_ANY.
  4036. * - if boundspecific do source address selection.
  4037. * - set fragmentation option for ipV4.
  4038. * - On return from IP output, check/adjust mtu size of output
  4039. * interface and smallest_mtu size as well.
  4040. */
  4041. /* Will need ifdefs around this */
  4042. struct mbuf *newm;
  4043. struct sctphdr *sctphdr;
  4044. int packet_length;
  4045. int ret;
  4046. #if defined(INET) || defined(INET6)
  4047. uint32_t vrf_id;
  4048. #endif
  4049. #if defined(INET) || defined(INET6)
  4050. struct mbuf *o_pak;
  4051. sctp_route_t *ro = NULL;
  4052. struct udphdr *udp = NULL;
  4053. #endif
  4054. uint8_t tos_value;
  4055. #if defined(__APPLE__) && !defined(__Userspace__)
  4056. struct socket *so = NULL;
  4057. #endif
  4058. #if defined(__APPLE__) && !defined(__Userspace__)
  4059. if (so_locked) {
  4060. sctp_lock_assert(SCTP_INP_SO(inp));
  4061. SCTP_TCB_LOCK_ASSERT(stcb);
  4062. } else {
  4063. sctp_unlock_assert(SCTP_INP_SO(inp));
  4064. }
  4065. #endif
  4066. if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
  4067. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
  4068. sctp_m_freem(m);
  4069. return (EFAULT);
  4070. }
  4071. #if defined(INET) || defined(INET6)
  4072. if (stcb) {
  4073. vrf_id = stcb->asoc.vrf_id;
  4074. } else {
  4075. vrf_id = inp->def_vrf_id;
  4076. }
  4077. #endif
  4078. /* fill in the HMAC digest for any AUTH chunk in the packet */
  4079. if ((auth != NULL) && (stcb != NULL)) {
  4080. sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
  4081. }
  4082. if (net) {
  4083. tos_value = net->dscp;
  4084. } else if (stcb) {
  4085. tos_value = stcb->asoc.default_dscp;
  4086. } else {
  4087. tos_value = inp->sctp_ep.default_dscp;
  4088. }
  4089. switch (to->sa_family) {
  4090. #ifdef INET
  4091. case AF_INET:
  4092. {
  4093. struct ip *ip = NULL;
  4094. sctp_route_t iproute;
  4095. int len;
  4096. len = SCTP_MIN_V4_OVERHEAD;
  4097. if (port) {
  4098. len += sizeof(struct udphdr);
  4099. }
  4100. newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
  4101. if (newm == NULL) {
  4102. sctp_m_freem(m);
  4103. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  4104. return (ENOMEM);
  4105. }
  4106. SCTP_ALIGN_TO_END(newm, len);
  4107. SCTP_BUF_LEN(newm) = len;
  4108. SCTP_BUF_NEXT(newm) = m;
  4109. m = newm;
  4110. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4111. if (net != NULL) {
  4112. m->m_pkthdr.flowid = net->flowid;
  4113. M_HASHTYPE_SET(m, net->flowtype);
  4114. } else {
  4115. m->m_pkthdr.flowid = mflowid;
  4116. M_HASHTYPE_SET(m, mflowtype);
  4117. }
  4118. #endif
  4119. packet_length = sctp_calculate_len(m);
  4120. ip = mtod(m, struct ip *);
  4121. ip->ip_v = IPVERSION;
  4122. ip->ip_hl = (sizeof(struct ip) >> 2);
  4123. if (tos_value == 0) {
  4124. /*
  4125. * This means especially, that it is not set at the
  4126. * SCTP layer. So use the value from the IP layer.
  4127. */
  4128. tos_value = inp->ip_inp.inp.inp_ip_tos;
  4129. }
  4130. tos_value &= 0xfc;
  4131. if (ecn_ok) {
  4132. tos_value |= sctp_get_ect(stcb);
  4133. }
  4134. if ((nofragment_flag) && (port == 0)) {
  4135. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4136. ip->ip_off = htons(IP_DF);
  4137. #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__)
  4138. ip->ip_off = IP_DF;
  4139. #else
  4140. ip->ip_off = htons(IP_DF);
  4141. #endif
  4142. } else {
  4143. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4144. ip->ip_off = htons(0);
  4145. #else
  4146. ip->ip_off = 0;
  4147. #endif
  4148. }
  4149. #if defined(__Userspace__)
  4150. ip->ip_id = htons(SCTP_IP_ID(inp)++);
  4151. #elif defined(__FreeBSD__)
  4152. /* FreeBSD has a function for ip_id's */
  4153. ip_fillid(ip);
  4154. #elif defined(__APPLE__)
  4155. #if RANDOM_IP_ID
  4156. ip->ip_id = ip_randomid();
  4157. #else
  4158. ip->ip_id = htons(ip_id++);
  4159. #endif
  4160. #else
  4161. ip->ip_id = SCTP_IP_ID(inp)++;
  4162. #endif
  4163. ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
  4164. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4165. ip->ip_len = htons(packet_length);
  4166. #else
  4167. ip->ip_len = packet_length;
  4168. #endif
  4169. ip->ip_tos = tos_value;
  4170. if (port) {
  4171. ip->ip_p = IPPROTO_UDP;
  4172. } else {
  4173. ip->ip_p = IPPROTO_SCTP;
  4174. }
  4175. ip->ip_sum = 0;
  4176. if (net == NULL) {
  4177. ro = &iproute;
  4178. memset(&iproute, 0, sizeof(iproute));
  4179. #ifdef HAVE_SA_LEN
  4180. memcpy(&ro->ro_dst, to, to->sa_len);
  4181. #else
  4182. memcpy(&ro->ro_dst, to, sizeof(struct sockaddr_in));
  4183. #endif
  4184. } else {
  4185. ro = (sctp_route_t *)&net->ro;
  4186. }
  4187. /* Now the address selection part */
  4188. ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
  4189. /* call the routine to select the src address */
  4190. if (net && out_of_asoc_ok == 0) {
  4191. if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) {
  4192. sctp_free_ifa(net->ro._s_addr);
  4193. net->ro._s_addr = NULL;
  4194. net->src_addr_selected = 0;
  4195. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4196. RO_NHFREE(ro);
  4197. #else
  4198. if (ro->ro_rt) {
  4199. RTFREE(ro->ro_rt);
  4200. ro->ro_rt = NULL;
  4201. }
  4202. #endif
  4203. }
  4204. if (net->src_addr_selected == 0) {
  4205. /* Cache the source address */
  4206. net->ro._s_addr = sctp_source_address_selection(inp,stcb,
  4207. ro, net, 0,
  4208. vrf_id);
  4209. net->src_addr_selected = 1;
  4210. }
  4211. if (net->ro._s_addr == NULL) {
  4212. /* No route to host */
  4213. net->src_addr_selected = 0;
  4214. sctp_handle_no_route(stcb, net, so_locked);
  4215. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4216. sctp_m_freem(m);
  4217. return (EHOSTUNREACH);
  4218. }
  4219. ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
  4220. } else {
  4221. if (over_addr == NULL) {
  4222. struct sctp_ifa *_lsrc;
  4223. _lsrc = sctp_source_address_selection(inp, stcb, ro,
  4224. net,
  4225. out_of_asoc_ok,
  4226. vrf_id);
  4227. if (_lsrc == NULL) {
  4228. sctp_handle_no_route(stcb, net, so_locked);
  4229. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4230. sctp_m_freem(m);
  4231. return (EHOSTUNREACH);
  4232. }
  4233. ip->ip_src = _lsrc->address.sin.sin_addr;
  4234. sctp_free_ifa(_lsrc);
  4235. } else {
  4236. ip->ip_src = over_addr->sin.sin_addr;
  4237. SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
  4238. }
  4239. }
  4240. if (port) {
  4241. if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
  4242. sctp_handle_no_route(stcb, net, so_locked);
  4243. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4244. sctp_m_freem(m);
  4245. return (EHOSTUNREACH);
  4246. }
  4247. udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
  4248. udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
  4249. udp->uh_dport = port;
  4250. udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip)));
  4251. #if !defined(__Userspace__)
  4252. #if defined(__FreeBSD__)
  4253. if (V_udp_cksum) {
  4254. udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
  4255. } else {
  4256. udp->uh_sum = 0;
  4257. }
  4258. #else
  4259. udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
  4260. #endif
  4261. #else
  4262. udp->uh_sum = 0;
  4263. #endif
  4264. sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
  4265. } else {
  4266. sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
  4267. }
  4268. sctphdr->src_port = src_port;
  4269. sctphdr->dest_port = dest_port;
  4270. sctphdr->v_tag = v_tag;
  4271. sctphdr->checksum = 0;
  4272. /*
  4273. * If source address selection fails and we find no route
  4274. * then the ip_output should fail as well with a
  4275. * NO_ROUTE_TO_HOST type error. We probably should catch
  4276. * that somewhere and abort the association right away
  4277. * (assuming this is an INIT being sent).
  4278. */
  4279. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4280. if (ro->ro_nh == NULL) {
  4281. #else
  4282. if (ro->ro_rt == NULL) {
  4283. #endif
  4284. /*
  4285. * src addr selection failed to find a route (or
  4286. * valid source addr), so we can't get there from
  4287. * here (yet)!
  4288. */
  4289. sctp_handle_no_route(stcb, net, so_locked);
  4290. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4291. sctp_m_freem(m);
  4292. return (EHOSTUNREACH);
  4293. }
  4294. if (ro != &iproute) {
  4295. memcpy(&iproute, ro, sizeof(*ro));
  4296. }
  4297. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
  4298. (uint32_t) (ntohl(ip->ip_src.s_addr)));
  4299. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
  4300. (uint32_t)(ntohl(ip->ip_dst.s_addr)));
  4301. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4302. SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
  4303. (void *)ro->ro_nh);
  4304. #else
  4305. SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
  4306. (void *)ro->ro_rt);
  4307. #endif
  4308. if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
  4309. /* failed to prepend data, give up */
  4310. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  4311. sctp_m_freem(m);
  4312. return (ENOMEM);
  4313. }
  4314. SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
  4315. if (port) {
  4316. sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
  4317. SCTP_STAT_INCR(sctps_sendswcrc);
  4318. #if !defined(__Userspace__)
  4319. #if defined(__FreeBSD__)
  4320. if (V_udp_cksum) {
  4321. SCTP_ENABLE_UDP_CSUM(o_pak);
  4322. }
  4323. #else
  4324. SCTP_ENABLE_UDP_CSUM(o_pak);
  4325. #endif
  4326. #endif
  4327. } else {
  4328. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4329. m->m_pkthdr.csum_flags = CSUM_SCTP;
  4330. m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
  4331. SCTP_STAT_INCR(sctps_sendhwcrc);
  4332. #else
  4333. if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
  4334. (stcb) && (stcb->asoc.scope.loopback_scope))) {
  4335. sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip));
  4336. SCTP_STAT_INCR(sctps_sendswcrc);
  4337. } else {
  4338. SCTP_STAT_INCR(sctps_sendhwcrc);
  4339. }
  4340. #endif
  4341. }
  4342. #ifdef SCTP_PACKET_LOGGING
  4343. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
  4344. sctp_packet_log(o_pak);
  4345. #endif
  4346. /* send it out. table id is taken from stcb */
  4347. #if defined(__APPLE__) && !defined(__Userspace__)
  4348. if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
  4349. so = SCTP_INP_SO(inp);
  4350. SCTP_SOCKET_UNLOCK(so, 0);
  4351. }
  4352. #endif
  4353. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4354. SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
  4355. #endif
  4356. SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
  4357. #if defined(__APPLE__) && !defined(__Userspace__)
  4358. if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
  4359. atomic_add_int(&stcb->asoc.refcnt, 1);
  4360. SCTP_TCB_UNLOCK(stcb);
  4361. SCTP_SOCKET_LOCK(so, 0);
  4362. SCTP_TCB_LOCK(stcb);
  4363. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  4364. }
  4365. #endif
  4366. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4367. if (port) {
  4368. UDPSTAT_INC(udps_opackets);
  4369. }
  4370. #endif
  4371. SCTP_STAT_INCR(sctps_sendpackets);
  4372. SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
  4373. if (ret)
  4374. SCTP_STAT_INCR(sctps_senderrors);
  4375. SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
  4376. if (net == NULL) {
  4377. /* free tempy routes */
  4378. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4379. RO_NHFREE(ro);
  4380. #else
  4381. if (ro->ro_rt) {
  4382. RTFREE(ro->ro_rt);
  4383. ro->ro_rt = NULL;
  4384. }
  4385. #endif
  4386. } else {
  4387. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4388. if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
  4389. #else
  4390. if ((ro->ro_rt != NULL) && (net->ro._s_addr) &&
  4391. #endif
  4392. ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
  4393. uint32_t mtu;
  4394. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4395. mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
  4396. #else
  4397. mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
  4398. #endif
  4399. if (mtu > 0) {
  4400. if (net->port) {
  4401. mtu -= sizeof(struct udphdr);
  4402. }
  4403. if (mtu < net->mtu) {
  4404. net->mtu = mtu;
  4405. if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
  4406. sctp_pathmtu_adjustment(stcb, mtu, true);
  4407. }
  4408. }
  4409. }
  4410. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4411. } else if (ro->ro_nh == NULL) {
  4412. #else
  4413. } else if (ro->ro_rt == NULL) {
  4414. #endif
  4415. /* route was freed */
  4416. if (net->ro._s_addr &&
  4417. net->src_addr_selected) {
  4418. sctp_free_ifa(net->ro._s_addr);
  4419. net->ro._s_addr = NULL;
  4420. }
  4421. net->src_addr_selected = 0;
  4422. }
  4423. }
  4424. return (ret);
  4425. }
  4426. #endif
  4427. #ifdef INET6
  4428. case AF_INET6:
  4429. {
  4430. uint32_t flowlabel, flowinfo;
  4431. struct ip6_hdr *ip6h;
  4432. struct route_in6 ip6route;
  4433. #if !defined(__Userspace__)
  4434. struct ifnet *ifp;
  4435. #endif
  4436. struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
  4437. int prev_scope = 0;
  4438. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4439. struct sockaddr_in6 lsa6_storage;
  4440. int error;
  4441. #endif
  4442. u_short prev_port = 0;
  4443. int len;
  4444. if (net) {
  4445. flowlabel = net->flowlabel;
  4446. } else if (stcb) {
  4447. flowlabel = stcb->asoc.default_flowlabel;
  4448. } else {
  4449. flowlabel = inp->sctp_ep.default_flowlabel;
  4450. }
  4451. if (flowlabel == 0) {
  4452. /*
  4453. * This means especially, that it is not set at the
  4454. * SCTP layer. So use the value from the IP layer.
  4455. */
  4456. #if defined(__APPLE__) && !defined(__Userspace__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
  4457. flowlabel = ntohl(inp->ip_inp.inp.inp_flow);
  4458. #else
  4459. flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
  4460. #endif
  4461. }
  4462. flowlabel &= 0x000fffff;
  4463. len = SCTP_MIN_OVERHEAD;
  4464. if (port) {
  4465. len += sizeof(struct udphdr);
  4466. }
  4467. newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
  4468. if (newm == NULL) {
  4469. sctp_m_freem(m);
  4470. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  4471. return (ENOMEM);
  4472. }
  4473. SCTP_ALIGN_TO_END(newm, len);
  4474. SCTP_BUF_LEN(newm) = len;
  4475. SCTP_BUF_NEXT(newm) = m;
  4476. m = newm;
  4477. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4478. if (net != NULL) {
  4479. m->m_pkthdr.flowid = net->flowid;
  4480. M_HASHTYPE_SET(m, net->flowtype);
  4481. } else {
  4482. m->m_pkthdr.flowid = mflowid;
  4483. M_HASHTYPE_SET(m, mflowtype);
  4484. }
  4485. #endif
  4486. packet_length = sctp_calculate_len(m);
  4487. ip6h = mtod(m, struct ip6_hdr *);
  4488. /* protect *sin6 from overwrite */
  4489. sin6 = (struct sockaddr_in6 *)to;
  4490. tmp = *sin6;
  4491. sin6 = &tmp;
  4492. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4493. /* KAME hack: embed scopeid */
  4494. #if defined(__APPLE__) && !defined(__Userspace__)
  4495. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  4496. if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
  4497. #else
  4498. if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0)
  4499. #endif
  4500. #elif defined(SCTP_KAME)
  4501. if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0)
  4502. #else
  4503. if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
  4504. #endif
  4505. {
  4506. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  4507. sctp_m_freem(m);
  4508. return (EINVAL);
  4509. }
  4510. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4511. if (net == NULL) {
  4512. memset(&ip6route, 0, sizeof(ip6route));
  4513. ro = (sctp_route_t *)&ip6route;
  4514. #ifdef HAVE_SIN6_LEN
  4515. memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
  4516. #else
  4517. memcpy(&ro->ro_dst, sin6, sizeof(struct sockaddr_in6));
  4518. #endif
  4519. } else {
  4520. ro = (sctp_route_t *)&net->ro;
  4521. }
  4522. /*
  4523. * We assume here that inp_flow is in host byte order within
  4524. * the TCB!
  4525. */
  4526. if (tos_value == 0) {
  4527. /*
  4528. * This means especially, that it is not set at the
  4529. * SCTP layer. So use the value from the IP layer.
  4530. */
  4531. #if defined(__APPLE__) && !defined(__Userspace__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
  4532. tos_value = (ntohl(inp->ip_inp.inp.inp_flow) >> 20) & 0xff;
  4533. #else
  4534. tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
  4535. #endif
  4536. }
  4537. tos_value &= 0xfc;
  4538. if (ecn_ok) {
  4539. tos_value |= sctp_get_ect(stcb);
  4540. }
  4541. flowinfo = 0x06;
  4542. flowinfo <<= 8;
  4543. flowinfo |= tos_value;
  4544. flowinfo <<= 20;
  4545. flowinfo |= flowlabel;
  4546. ip6h->ip6_flow = htonl(flowinfo);
  4547. if (port) {
  4548. ip6h->ip6_nxt = IPPROTO_UDP;
  4549. } else {
  4550. ip6h->ip6_nxt = IPPROTO_SCTP;
  4551. }
  4552. ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
  4553. ip6h->ip6_dst = sin6->sin6_addr;
  4554. /*
  4555. * Add SRC address selection here: we can only reuse to a
  4556. * limited degree the kame src-addr-sel, since we can try
  4557. * their selection but it may not be bound.
  4558. */
  4559. memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
  4560. lsa6_tmp.sin6_family = AF_INET6;
  4561. #ifdef HAVE_SIN6_LEN
  4562. lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
  4563. #endif
  4564. lsa6 = &lsa6_tmp;
  4565. if (net && out_of_asoc_ok == 0) {
  4566. if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) {
  4567. sctp_free_ifa(net->ro._s_addr);
  4568. net->ro._s_addr = NULL;
  4569. net->src_addr_selected = 0;
  4570. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4571. RO_NHFREE(ro);
  4572. #else
  4573. if (ro->ro_rt) {
  4574. RTFREE(ro->ro_rt);
  4575. ro->ro_rt = NULL;
  4576. }
  4577. #endif
  4578. }
  4579. if (net->src_addr_selected == 0) {
  4580. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4581. sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  4582. /* KAME hack: embed scopeid */
  4583. #if defined(__APPLE__) && !defined(__Userspace__)
  4584. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  4585. if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
  4586. #else
  4587. if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0)
  4588. #endif
  4589. #elif defined(SCTP_KAME)
  4590. if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0)
  4591. #else
  4592. if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
  4593. #endif
  4594. {
  4595. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  4596. sctp_m_freem(m);
  4597. return (EINVAL);
  4598. }
  4599. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4600. /* Cache the source address */
  4601. net->ro._s_addr = sctp_source_address_selection(inp,
  4602. stcb,
  4603. ro,
  4604. net,
  4605. 0,
  4606. vrf_id);
  4607. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4608. #ifdef SCTP_KAME
  4609. (void)sa6_recoverscope(sin6);
  4610. #else
  4611. (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
  4612. #endif /* SCTP_KAME */
  4613. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4614. net->src_addr_selected = 1;
  4615. }
  4616. if (net->ro._s_addr == NULL) {
  4617. SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
  4618. net->src_addr_selected = 0;
  4619. sctp_handle_no_route(stcb, net, so_locked);
  4620. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4621. sctp_m_freem(m);
  4622. return (EHOSTUNREACH);
  4623. }
  4624. lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
  4625. } else {
  4626. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4627. sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
  4628. /* KAME hack: embed scopeid */
  4629. #if defined(__APPLE__) && !defined(__Userspace__)
  4630. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  4631. if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
  4632. #else
  4633. if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0)
  4634. #endif
  4635. #elif defined(SCTP_KAME)
  4636. if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0)
  4637. #else
  4638. if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
  4639. #endif
  4640. {
  4641. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  4642. sctp_m_freem(m);
  4643. return (EINVAL);
  4644. }
  4645. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4646. if (over_addr == NULL) {
  4647. struct sctp_ifa *_lsrc;
  4648. _lsrc = sctp_source_address_selection(inp, stcb, ro,
  4649. net,
  4650. out_of_asoc_ok,
  4651. vrf_id);
  4652. if (_lsrc == NULL) {
  4653. sctp_handle_no_route(stcb, net, so_locked);
  4654. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4655. sctp_m_freem(m);
  4656. return (EHOSTUNREACH);
  4657. }
  4658. lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
  4659. sctp_free_ifa(_lsrc);
  4660. } else {
  4661. lsa6->sin6_addr = over_addr->sin6.sin6_addr;
  4662. SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
  4663. }
  4664. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4665. #ifdef SCTP_KAME
  4666. (void)sa6_recoverscope(sin6);
  4667. #else
  4668. (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
  4669. #endif /* SCTP_KAME */
  4670. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4671. }
  4672. lsa6->sin6_port = inp->sctp_lport;
  4673. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4674. if (ro->ro_nh == NULL) {
  4675. #else
  4676. if (ro->ro_rt == NULL) {
  4677. #endif
  4678. /*
  4679. * src addr selection failed to find a route (or
  4680. * valid source addr), so we can't get there from
  4681. * here!
  4682. */
  4683. sctp_handle_no_route(stcb, net, so_locked);
  4684. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4685. sctp_m_freem(m);
  4686. return (EHOSTUNREACH);
  4687. }
  4688. #ifndef SCOPEDROUTING
  4689. #ifdef SCTP_EMBEDDED_V6_SCOPE
  4690. /*
  4691. * XXX: sa6 may not have a valid sin6_scope_id in the
  4692. * non-SCOPEDROUTING case.
  4693. */
  4694. memset(&lsa6_storage, 0, sizeof(lsa6_storage));
  4695. lsa6_storage.sin6_family = AF_INET6;
  4696. #ifdef HAVE_SIN6_LEN
  4697. lsa6_storage.sin6_len = sizeof(lsa6_storage);
  4698. #endif
  4699. #ifdef SCTP_KAME
  4700. lsa6_storage.sin6_addr = lsa6->sin6_addr;
  4701. if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
  4702. #else
  4703. if ((error = in6_recoverscope(&lsa6_storage, &lsa6->sin6_addr,
  4704. NULL)) != 0) {
  4705. #endif /* SCTP_KAME */
  4706. SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
  4707. sctp_m_freem(m);
  4708. return (error);
  4709. }
  4710. /* XXX */
  4711. lsa6_storage.sin6_addr = lsa6->sin6_addr;
  4712. lsa6_storage.sin6_port = inp->sctp_lport;
  4713. lsa6 = &lsa6_storage;
  4714. #endif /* SCTP_EMBEDDED_V6_SCOPE */
  4715. #endif /* SCOPEDROUTING */
  4716. ip6h->ip6_src = lsa6->sin6_addr;
  4717. if (port) {
  4718. if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
  4719. sctp_handle_no_route(stcb, net, so_locked);
  4720. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
  4721. sctp_m_freem(m);
  4722. return (EHOSTUNREACH);
  4723. }
  4724. udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
  4725. udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
  4726. udp->uh_dport = port;
  4727. udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
  4728. udp->uh_sum = 0;
  4729. sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
  4730. } else {
  4731. sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
  4732. }
  4733. sctphdr->src_port = src_port;
  4734. sctphdr->dest_port = dest_port;
  4735. sctphdr->v_tag = v_tag;
  4736. sctphdr->checksum = 0;
  4737. /*
  4738. * We set the hop limit now since there is a good chance
  4739. * that our ro pointer is now filled
  4740. */
  4741. ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
  4742. #if !defined(__Userspace__)
  4743. ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
  4744. #endif
  4745. #ifdef SCTP_DEBUG
  4746. /* Copy to be sure something bad is not happening */
  4747. sin6->sin6_addr = ip6h->ip6_dst;
  4748. lsa6->sin6_addr = ip6h->ip6_src;
  4749. #endif
  4750. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
  4751. SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
  4752. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
  4753. SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
  4754. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
  4755. if (net) {
  4756. sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
  4757. /* preserve the port and scope for link local send */
  4758. prev_scope = sin6->sin6_scope_id;
  4759. prev_port = sin6->sin6_port;
  4760. }
  4761. if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
  4762. /* failed to prepend data, give up */
  4763. sctp_m_freem(m);
  4764. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  4765. return (ENOMEM);
  4766. }
  4767. SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
  4768. if (port) {
  4769. sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
  4770. SCTP_STAT_INCR(sctps_sendswcrc);
  4771. #if !defined(__Userspace__)
  4772. #if defined(_WIN32)
  4773. udp->uh_sum = 0;
  4774. #else
  4775. if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
  4776. udp->uh_sum = 0xffff;
  4777. }
  4778. #endif
  4779. #endif
  4780. } else {
  4781. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4782. m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
  4783. m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
  4784. SCTP_STAT_INCR(sctps_sendhwcrc);
  4785. #else
  4786. if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
  4787. (stcb) && (stcb->asoc.scope.loopback_scope))) {
  4788. sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr));
  4789. SCTP_STAT_INCR(sctps_sendswcrc);
  4790. } else {
  4791. SCTP_STAT_INCR(sctps_sendhwcrc);
  4792. }
  4793. #endif
  4794. }
  4795. /* send it out. table id is taken from stcb */
  4796. #if defined(__APPLE__) && !defined(__Userspace__)
  4797. if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
  4798. so = SCTP_INP_SO(inp);
  4799. SCTP_SOCKET_UNLOCK(so, 0);
  4800. }
  4801. #endif
  4802. #ifdef SCTP_PACKET_LOGGING
  4803. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
  4804. sctp_packet_log(o_pak);
  4805. #endif
  4806. #if !defined(__Userspace__)
  4807. #if defined(__FreeBSD__)
  4808. SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
  4809. #endif
  4810. SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
  4811. #else
  4812. SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, NULL, inp, vrf_id);
  4813. #endif
  4814. #if defined(__APPLE__) && !defined(__Userspace__)
  4815. if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
  4816. atomic_add_int(&stcb->asoc.refcnt, 1);
  4817. SCTP_TCB_UNLOCK(stcb);
  4818. SCTP_SOCKET_LOCK(so, 0);
  4819. SCTP_TCB_LOCK(stcb);
  4820. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  4821. }
  4822. #endif
  4823. if (net) {
  4824. /* for link local this must be done */
  4825. sin6->sin6_scope_id = prev_scope;
  4826. sin6->sin6_port = prev_port;
  4827. }
  4828. SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
  4829. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4830. if (port) {
  4831. UDPSTAT_INC(udps_opackets);
  4832. }
  4833. #endif
  4834. SCTP_STAT_INCR(sctps_sendpackets);
  4835. SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
  4836. if (ret) {
  4837. SCTP_STAT_INCR(sctps_senderrors);
  4838. }
  4839. if (net == NULL) {
  4840. /* Now if we had a temp route free it */
  4841. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4842. RO_NHFREE(ro);
  4843. #else
  4844. if (ro->ro_rt) {
  4845. RTFREE(ro->ro_rt);
  4846. ro->ro_rt = NULL;
  4847. }
  4848. #endif
  4849. } else {
  4850. /* PMTU check versus smallest asoc MTU goes here */
  4851. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4852. if (ro->ro_nh == NULL) {
  4853. #else
  4854. if (ro->ro_rt == NULL) {
  4855. #endif
  4856. /* Route was freed */
  4857. if (net->ro._s_addr &&
  4858. net->src_addr_selected) {
  4859. sctp_free_ifa(net->ro._s_addr);
  4860. net->ro._s_addr = NULL;
  4861. }
  4862. net->src_addr_selected = 0;
  4863. }
  4864. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4865. if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
  4866. #else
  4867. if ((ro->ro_rt != NULL) && (net->ro._s_addr) &&
  4868. #endif
  4869. ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
  4870. uint32_t mtu;
  4871. #if defined(__FreeBSD__) && !defined(__Userspace__)
  4872. mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
  4873. #else
  4874. mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
  4875. #endif
  4876. if (mtu > 0) {
  4877. if (net->port) {
  4878. mtu -= sizeof(struct udphdr);
  4879. }
  4880. if (mtu < net->mtu) {
  4881. net->mtu = mtu;
  4882. if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
  4883. sctp_pathmtu_adjustment(stcb, mtu, false);
  4884. }
  4885. }
  4886. }
  4887. }
  4888. #if !defined(__Userspace__)
  4889. else if (ifp != NULL) {
  4890. #if defined(_WIN32)
  4891. #define ND_IFINFO(ifp) (ifp)
  4892. #define linkmtu if_mtu
  4893. #endif
  4894. if ((ND_IFINFO(ifp)->linkmtu > 0) &&
  4895. (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
  4896. sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
  4897. }
  4898. }
  4899. #endif
  4900. }
  4901. return (ret);
  4902. }
  4903. #endif
  4904. #if defined(__Userspace__)
  4905. case AF_CONN:
  4906. {
  4907. char *buffer;
  4908. struct sockaddr_conn *sconn;
  4909. int len;
  4910. sconn = (struct sockaddr_conn *)to;
  4911. len = sizeof(struct sctphdr);
  4912. newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
  4913. if (newm == NULL) {
  4914. sctp_m_freem(m);
  4915. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  4916. return (ENOMEM);
  4917. }
  4918. SCTP_ALIGN_TO_END(newm, len);
  4919. SCTP_BUF_LEN(newm) = len;
  4920. SCTP_BUF_NEXT(newm) = m;
  4921. m = newm;
  4922. packet_length = sctp_calculate_len(m);
  4923. m->m_pkthdr.len = packet_length;
  4924. sctphdr = mtod(m, struct sctphdr *);
  4925. sctphdr->src_port = src_port;
  4926. sctphdr->dest_port = dest_port;
  4927. sctphdr->v_tag = v_tag;
  4928. sctphdr->checksum = 0;
  4929. if (SCTP_BASE_VAR(crc32c_offloaded) == 0) {
  4930. sctphdr->checksum = sctp_calculate_cksum(m, 0);
  4931. SCTP_STAT_INCR(sctps_sendswcrc);
  4932. } else {
  4933. SCTP_STAT_INCR(sctps_sendhwcrc);
  4934. }
  4935. if (tos_value == 0) {
  4936. tos_value = inp->ip_inp.inp.inp_ip_tos;
  4937. }
  4938. tos_value &= 0xfc;
  4939. if (ecn_ok) {
  4940. tos_value |= sctp_get_ect(stcb);
  4941. }
  4942. /* Don't alloc/free for each packet */
  4943. if ((buffer = malloc(packet_length)) != NULL) {
  4944. m_copydata(m, 0, packet_length, buffer);
  4945. ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, packet_length, tos_value, nofragment_flag);
  4946. free(buffer);
  4947. } else {
  4948. ret = ENOMEM;
  4949. }
  4950. sctp_m_freem(m);
  4951. return (ret);
  4952. }
  4953. #endif
  4954. default:
  4955. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
  4956. ((struct sockaddr *)to)->sa_family);
  4957. sctp_m_freem(m);
  4958. SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
  4959. return (EFAULT);
  4960. }
  4961. }
  4962. void
  4963. sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
  4964. {
  4965. struct mbuf *m, *m_last;
  4966. struct sctp_nets *net;
  4967. struct sctp_init_chunk *init;
  4968. struct sctp_supported_addr_param *sup_addr;
  4969. struct sctp_adaptation_layer_indication *ali;
  4970. struct sctp_supported_chunk_types_param *pr_supported;
  4971. struct sctp_paramhdr *ph;
  4972. int cnt_inits_to = 0;
  4973. int error;
  4974. uint16_t num_ext, chunk_len, padding_len, parameter_len;
  4975. #if defined(__APPLE__) && !defined(__Userspace__)
  4976. if (so_locked) {
  4977. sctp_lock_assert(SCTP_INP_SO(inp));
  4978. } else {
  4979. sctp_unlock_assert(SCTP_INP_SO(inp));
  4980. }
  4981. #endif
  4982. /* INIT's always go to the primary (and usually ONLY address) */
  4983. net = stcb->asoc.primary_destination;
  4984. if (net == NULL) {
  4985. net = TAILQ_FIRST(&stcb->asoc.nets);
  4986. if (net == NULL) {
  4987. /* TSNH */
  4988. return;
  4989. }
  4990. /* we confirm any address we send an INIT to */
  4991. net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
  4992. (void)sctp_set_primary_addr(stcb, NULL, net);
  4993. } else {
  4994. /* we confirm any address we send an INIT to */
  4995. net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
  4996. }
  4997. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
  4998. #ifdef INET6
  4999. if (net->ro._l_addr.sa.sa_family == AF_INET6) {
  5000. /*
  5001. * special hook, if we are sending to link local it will not
  5002. * show up in our private address count.
  5003. */
  5004. if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
  5005. cnt_inits_to = 1;
  5006. }
  5007. #endif
  5008. if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
  5009. /* This case should not happen */
  5010. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
  5011. return;
  5012. }
  5013. /* start the INIT timer */
  5014. sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
  5015. m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
  5016. if (m == NULL) {
  5017. /* No memory, INIT timer will re-attempt. */
  5018. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
  5019. return;
  5020. }
  5021. chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
  5022. padding_len = 0;
  5023. /* Now lets put the chunk header in place */
  5024. init = mtod(m, struct sctp_init_chunk *);
  5025. /* now the chunk header */
  5026. init->ch.chunk_type = SCTP_INITIATION;
  5027. init->ch.chunk_flags = 0;
  5028. /* fill in later from mbuf we build */
  5029. init->ch.chunk_length = 0;
  5030. /* place in my tag */
  5031. init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
  5032. /* set up some of the credits. */
  5033. init->init.a_rwnd = htonl(max(inp->sctp_socket?SCTP_SB_LIMIT_RCV(inp->sctp_socket):0,
  5034. SCTP_MINIMAL_RWND));
  5035. init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
  5036. init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
  5037. init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
  5038. /* Adaptation layer indication parameter */
  5039. if (inp->sctp_ep.adaptation_layer_indicator_provided) {
  5040. parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
  5041. ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len);
  5042. ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
  5043. ali->ph.param_length = htons(parameter_len);
  5044. ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
  5045. chunk_len += parameter_len;
  5046. }
  5047. /* ECN parameter */
  5048. if (stcb->asoc.ecn_supported == 1) {
  5049. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  5050. ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
  5051. ph->param_type = htons(SCTP_ECN_CAPABLE);
  5052. ph->param_length = htons(parameter_len);
  5053. chunk_len += parameter_len;
  5054. }
  5055. /* PR-SCTP supported parameter */
  5056. if (stcb->asoc.prsctp_supported == 1) {
  5057. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  5058. ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
  5059. ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
  5060. ph->param_length = htons(parameter_len);
  5061. chunk_len += parameter_len;
  5062. }
  5063. /* Add NAT friendly parameter. */
  5064. if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
  5065. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  5066. ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
  5067. ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
  5068. ph->param_length = htons(parameter_len);
  5069. chunk_len += parameter_len;
  5070. }
  5071. /* And now tell the peer which extensions we support */
  5072. num_ext = 0;
  5073. pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len);
  5074. if (stcb->asoc.prsctp_supported == 1) {
  5075. pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
  5076. if (stcb->asoc.idata_supported) {
  5077. pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
  5078. }
  5079. }
  5080. if (stcb->asoc.auth_supported == 1) {
  5081. pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
  5082. }
  5083. if (stcb->asoc.asconf_supported == 1) {
  5084. pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
  5085. pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
  5086. }
  5087. if (stcb->asoc.reconfig_supported == 1) {
  5088. pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
  5089. }
  5090. if (stcb->asoc.idata_supported) {
  5091. pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
  5092. }
  5093. if (stcb->asoc.nrsack_supported == 1) {
  5094. pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
  5095. }
  5096. if (stcb->asoc.pktdrop_supported == 1) {
  5097. pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
  5098. }
  5099. if (num_ext > 0) {
  5100. parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
  5101. pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
  5102. pr_supported->ph.param_length = htons(parameter_len);
  5103. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  5104. chunk_len += parameter_len;
  5105. }
  5106. /* add authentication parameters */
  5107. if (stcb->asoc.auth_supported) {
  5108. /* attach RANDOM parameter, if available */
  5109. if (stcb->asoc.authinfo.random != NULL) {
  5110. struct sctp_auth_random *randp;
  5111. if (padding_len > 0) {
  5112. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  5113. chunk_len += padding_len;
  5114. padding_len = 0;
  5115. }
  5116. randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len);
  5117. parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
  5118. /* random key already contains the header */
  5119. memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
  5120. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  5121. chunk_len += parameter_len;
  5122. }
  5123. /* add HMAC_ALGO parameter */
  5124. if (stcb->asoc.local_hmacs != NULL) {
  5125. struct sctp_auth_hmac_algo *hmacs;
  5126. if (padding_len > 0) {
  5127. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  5128. chunk_len += padding_len;
  5129. padding_len = 0;
  5130. }
  5131. hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len);
  5132. parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
  5133. stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
  5134. hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
  5135. hmacs->ph.param_length = htons(parameter_len);
  5136. sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
  5137. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  5138. chunk_len += parameter_len;
  5139. }
  5140. /* add CHUNKS parameter */
  5141. if (stcb->asoc.local_auth_chunks != NULL) {
  5142. struct sctp_auth_chunk_list *chunks;
  5143. if (padding_len > 0) {
  5144. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  5145. chunk_len += padding_len;
  5146. padding_len = 0;
  5147. }
  5148. chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len);
  5149. parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
  5150. sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
  5151. chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
  5152. chunks->ph.param_length = htons(parameter_len);
  5153. sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
  5154. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  5155. chunk_len += parameter_len;
  5156. }
  5157. }
  5158. /* now any cookie time extensions */
  5159. if (stcb->asoc.cookie_preserve_req > 0) {
  5160. struct sctp_cookie_perserve_param *cookie_preserve;
  5161. if (padding_len > 0) {
  5162. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  5163. chunk_len += padding_len;
  5164. padding_len = 0;
  5165. }
  5166. parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
  5167. cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t) + chunk_len);
  5168. cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
  5169. cookie_preserve->ph.param_length = htons(parameter_len);
  5170. cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
  5171. stcb->asoc.cookie_preserve_req = 0;
  5172. chunk_len += parameter_len;
  5173. }
  5174. if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
  5175. uint8_t i;
  5176. if (padding_len > 0) {
  5177. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  5178. chunk_len += padding_len;
  5179. padding_len = 0;
  5180. }
  5181. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  5182. if (stcb->asoc.scope.ipv4_addr_legal) {
  5183. parameter_len += (uint16_t)sizeof(uint16_t);
  5184. }
  5185. if (stcb->asoc.scope.ipv6_addr_legal) {
  5186. parameter_len += (uint16_t)sizeof(uint16_t);
  5187. }
  5188. sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t) + chunk_len);
  5189. sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
  5190. sup_addr->ph.param_length = htons(parameter_len);
  5191. i = 0;
  5192. if (stcb->asoc.scope.ipv4_addr_legal) {
  5193. sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
  5194. }
  5195. if (stcb->asoc.scope.ipv6_addr_legal) {
  5196. sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
  5197. }
  5198. padding_len = 4 - 2 * i;
  5199. chunk_len += parameter_len;
  5200. }
  5201. SCTP_BUF_LEN(m) = chunk_len;
  5202. /* now the addresses */
  5203. /* To optimize this we could put the scoping stuff
  5204. * into a structure and remove the individual uint8's from
  5205. * the assoc structure. Then we could just sifa in the
  5206. * address within the stcb. But for now this is a quick
  5207. * hack to get the address stuff teased apart.
  5208. */
  5209. m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
  5210. m, cnt_inits_to,
  5211. &padding_len, &chunk_len);
  5212. init->ch.chunk_length = htons(chunk_len);
  5213. if (padding_len > 0) {
  5214. if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
  5215. sctp_m_freem(m);
  5216. return;
  5217. }
  5218. }
  5219. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
  5220. if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
  5221. (struct sockaddr *)&net->ro._l_addr,
  5222. m, 0, NULL, 0, 0, 0, 0,
  5223. inp->sctp_lport, stcb->rport, htonl(0),
  5224. net->port, NULL,
  5225. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5226. 0, 0,
  5227. #endif
  5228. so_locked))) {
  5229. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
  5230. if (error == ENOBUFS) {
  5231. stcb->asoc.ifp_had_enobuf = 1;
  5232. SCTP_STAT_INCR(sctps_lowlevelerr);
  5233. }
  5234. } else {
  5235. stcb->asoc.ifp_had_enobuf = 0;
  5236. }
  5237. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  5238. (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
  5239. }
  5240. struct mbuf *
  5241. sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
  5242. int param_offset, int *abort_processing,
  5243. struct sctp_chunkhdr *cp,
  5244. int *nat_friendly,
  5245. int *cookie_found)
  5246. {
  5247. /*
  5248. * Given a mbuf containing an INIT or INIT-ACK with the param_offset
  5249. * being equal to the beginning of the params i.e. (iphlen +
  5250. * sizeof(struct sctp_init_msg) parse through the parameters to the
  5251. * end of the mbuf verifying that all parameters are known.
  5252. *
  5253. * For unknown parameters build and return a mbuf with
  5254. * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
  5255. * processing this chunk stop, and set *abort_processing to 1.
  5256. *
  5257. * By having param_offset be pre-set to where parameters begin it is
  5258. * hoped that this routine may be reused in the future by new
  5259. * features.
  5260. */
  5261. struct sctp_paramhdr *phdr, params;
  5262. struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
  5263. int at, limit, pad_needed;
  5264. uint16_t ptype, plen, padded_size;
  5265. *abort_processing = 0;
  5266. if (cookie_found != NULL) {
  5267. *cookie_found = 0;
  5268. }
  5269. mat = in_initpkt;
  5270. limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
  5271. at = param_offset;
  5272. op_err = NULL;
  5273. op_err_last = NULL;
  5274. pad_needed = 0;
  5275. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
  5276. phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
  5277. while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
  5278. ptype = ntohs(phdr->param_type);
  5279. plen = ntohs(phdr->param_length);
  5280. if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
  5281. /* wacked parameter */
  5282. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
  5283. goto invalid_size;
  5284. }
  5285. limit -= SCTP_SIZE32(plen);
  5286. /*-
  5287. * All parameters for all chunks that we know/understand are
  5288. * listed here. We process them other places and make
  5289. * appropriate stop actions per the upper bits. However this
  5290. * is the generic routine processor's can call to get back
  5291. * an operr.. to either incorporate (init-ack) or send.
  5292. */
  5293. padded_size = SCTP_SIZE32(plen);
  5294. switch (ptype) {
  5295. /* Param's with variable size */
  5296. case SCTP_HEARTBEAT_INFO:
  5297. case SCTP_UNRECOG_PARAM:
  5298. case SCTP_ERROR_CAUSE_IND:
  5299. /* ok skip fwd */
  5300. at += padded_size;
  5301. break;
  5302. case SCTP_STATE_COOKIE:
  5303. if (cookie_found != NULL) {
  5304. *cookie_found = 1;
  5305. }
  5306. at += padded_size;
  5307. break;
  5308. /* Param's with variable size within a range */
  5309. case SCTP_CHUNK_LIST:
  5310. case SCTP_SUPPORTED_CHUNK_EXT:
  5311. if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
  5312. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
  5313. goto invalid_size;
  5314. }
  5315. at += padded_size;
  5316. break;
  5317. case SCTP_SUPPORTED_ADDRTYPE:
  5318. if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
  5319. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
  5320. goto invalid_size;
  5321. }
  5322. at += padded_size;
  5323. break;
  5324. case SCTP_RANDOM:
  5325. if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
  5326. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
  5327. goto invalid_size;
  5328. }
  5329. at += padded_size;
  5330. break;
  5331. case SCTP_SET_PRIM_ADDR:
  5332. case SCTP_DEL_IP_ADDRESS:
  5333. case SCTP_ADD_IP_ADDRESS:
  5334. if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
  5335. (padded_size != sizeof(struct sctp_asconf_addr_param))) {
  5336. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
  5337. goto invalid_size;
  5338. }
  5339. at += padded_size;
  5340. break;
  5341. /* Param's with a fixed size */
  5342. case SCTP_IPV4_ADDRESS:
  5343. if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
  5344. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
  5345. goto invalid_size;
  5346. }
  5347. at += padded_size;
  5348. break;
  5349. case SCTP_IPV6_ADDRESS:
  5350. if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
  5351. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
  5352. goto invalid_size;
  5353. }
  5354. at += padded_size;
  5355. break;
  5356. case SCTP_COOKIE_PRESERVE:
  5357. if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
  5358. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
  5359. goto invalid_size;
  5360. }
  5361. at += padded_size;
  5362. break;
  5363. case SCTP_HAS_NAT_SUPPORT:
  5364. *nat_friendly = 1;
  5365. /* fall through */
  5366. case SCTP_PRSCTP_SUPPORTED:
  5367. if (padded_size != sizeof(struct sctp_paramhdr)) {
  5368. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
  5369. goto invalid_size;
  5370. }
  5371. at += padded_size;
  5372. break;
  5373. case SCTP_ECN_CAPABLE:
  5374. if (padded_size != sizeof(struct sctp_paramhdr)) {
  5375. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
  5376. goto invalid_size;
  5377. }
  5378. at += padded_size;
  5379. break;
  5380. case SCTP_ULP_ADAPTATION:
  5381. if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
  5382. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
  5383. goto invalid_size;
  5384. }
  5385. at += padded_size;
  5386. break;
  5387. case SCTP_SUCCESS_REPORT:
  5388. if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
  5389. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
  5390. goto invalid_size;
  5391. }
  5392. at += padded_size;
  5393. break;
  5394. case SCTP_HOSTNAME_ADDRESS:
  5395. {
  5396. /* Hostname parameters are deprecated. */
  5397. struct sctp_gen_error_cause *cause;
  5398. int l_len;
  5399. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
  5400. *abort_processing = 1;
  5401. sctp_m_freem(op_err);
  5402. op_err = NULL;
  5403. op_err_last = NULL;
  5404. #ifdef INET6
  5405. l_len = SCTP_MIN_OVERHEAD;
  5406. #else
  5407. l_len = SCTP_MIN_V4_OVERHEAD;
  5408. #endif
  5409. l_len += sizeof(struct sctp_chunkhdr);
  5410. l_len += sizeof(struct sctp_gen_error_cause);
  5411. op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
  5412. if (op_err != NULL) {
  5413. /*
  5414. * Pre-reserve space for IP, SCTP, and
  5415. * chunk header.
  5416. */
  5417. #ifdef INET6
  5418. SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
  5419. #else
  5420. SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
  5421. #endif
  5422. SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
  5423. SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
  5424. SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
  5425. cause = mtod(op_err, struct sctp_gen_error_cause *);
  5426. cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
  5427. cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
  5428. SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
  5429. if (SCTP_BUF_NEXT(op_err) == NULL) {
  5430. sctp_m_freem(op_err);
  5431. op_err = NULL;
  5432. op_err_last = NULL;
  5433. }
  5434. }
  5435. return (op_err);
  5436. }
  5437. default:
  5438. /*
  5439. * we do not recognize the parameter figure out what
  5440. * we do.
  5441. */
  5442. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
  5443. if ((ptype & 0x4000) == 0x4000) {
  5444. /* Report bit is set?? */
  5445. SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
  5446. if (op_err == NULL) {
  5447. int l_len;
  5448. /* Ok need to try to get an mbuf */
  5449. #ifdef INET6
  5450. l_len = SCTP_MIN_OVERHEAD;
  5451. #else
  5452. l_len = SCTP_MIN_V4_OVERHEAD;
  5453. #endif
  5454. l_len += sizeof(struct sctp_chunkhdr);
  5455. l_len += sizeof(struct sctp_paramhdr);
  5456. op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
  5457. if (op_err) {
  5458. SCTP_BUF_LEN(op_err) = 0;
  5459. #ifdef INET6
  5460. SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
  5461. #else
  5462. SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
  5463. #endif
  5464. SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
  5465. SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
  5466. op_err_last = op_err;
  5467. }
  5468. }
  5469. if (op_err != NULL) {
  5470. /* If we have space */
  5471. struct sctp_paramhdr *param;
  5472. if (pad_needed > 0) {
  5473. op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
  5474. }
  5475. if (op_err_last == NULL) {
  5476. sctp_m_freem(op_err);
  5477. op_err = NULL;
  5478. op_err_last = NULL;
  5479. goto more_processing;
  5480. }
  5481. if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
  5482. m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
  5483. if (m_tmp == NULL) {
  5484. sctp_m_freem(op_err);
  5485. op_err = NULL;
  5486. op_err_last = NULL;
  5487. goto more_processing;
  5488. }
  5489. SCTP_BUF_LEN(m_tmp) = 0;
  5490. SCTP_BUF_NEXT(m_tmp) = NULL;
  5491. SCTP_BUF_NEXT(op_err_last) = m_tmp;
  5492. op_err_last = m_tmp;
  5493. }
  5494. param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t) + SCTP_BUF_LEN(op_err_last));
  5495. param->param_type = htons(SCTP_UNRECOG_PARAM);
  5496. param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
  5497. SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
  5498. SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
  5499. if (SCTP_BUF_NEXT(op_err_last) == NULL) {
  5500. sctp_m_freem(op_err);
  5501. op_err = NULL;
  5502. op_err_last = NULL;
  5503. goto more_processing;
  5504. } else {
  5505. while (SCTP_BUF_NEXT(op_err_last) != NULL) {
  5506. op_err_last = SCTP_BUF_NEXT(op_err_last);
  5507. }
  5508. }
  5509. if (plen % 4 != 0) {
  5510. pad_needed = 4 - (plen % 4);
  5511. } else {
  5512. pad_needed = 0;
  5513. }
  5514. }
  5515. }
  5516. more_processing:
  5517. if ((ptype & 0x8000) == 0x0000) {
  5518. SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
  5519. return (op_err);
  5520. } else {
  5521. /* skip this chunk and continue processing */
  5522. SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
  5523. at += SCTP_SIZE32(plen);
  5524. }
  5525. break;
  5526. }
  5527. phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
  5528. }
  5529. return (op_err);
  5530. invalid_size:
  5531. SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
  5532. *abort_processing = 1;
  5533. sctp_m_freem(op_err);
  5534. op_err = NULL;
  5535. op_err_last = NULL;
  5536. if (phdr != NULL) {
  5537. struct sctp_paramhdr *param;
  5538. int l_len;
  5539. #ifdef INET6
  5540. l_len = SCTP_MIN_OVERHEAD;
  5541. #else
  5542. l_len = SCTP_MIN_V4_OVERHEAD;
  5543. #endif
  5544. l_len += sizeof(struct sctp_chunkhdr);
  5545. l_len += (2 * sizeof(struct sctp_paramhdr));
  5546. op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
  5547. if (op_err) {
  5548. SCTP_BUF_LEN(op_err) = 0;
  5549. #ifdef INET6
  5550. SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
  5551. #else
  5552. SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
  5553. #endif
  5554. SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
  5555. SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
  5556. SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
  5557. param = mtod(op_err, struct sctp_paramhdr *);
  5558. param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
  5559. param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
  5560. param++;
  5561. param->param_type = htons(ptype);
  5562. param->param_length = htons(plen);
  5563. }
  5564. }
  5565. return (op_err);
  5566. }
  5567. /*
  5568. * Given a INIT chunk, look through the parameters to verify that there
  5569. * are no new addresses.
  5570. * Return true, if there is a new address or there is a problem parsing
  5571. the parameters. Provide an optional error cause used when sending an ABORT.
  5572. * Return false, if there are no new addresses and there is no problem in
  5573. parameter processing.
  5574. */
  5575. static bool
  5576. sctp_are_there_new_addresses(struct sctp_association *asoc,
  5577. struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
  5578. struct mbuf **op_err)
  5579. {
  5580. struct sockaddr *sa_touse;
  5581. struct sockaddr *sa;
  5582. struct sctp_paramhdr *phdr, params;
  5583. struct sctp_nets *net;
  5584. #ifdef INET
  5585. struct sockaddr_in sin4, *sa4;
  5586. #endif
  5587. #ifdef INET6
  5588. struct sockaddr_in6 sin6, *sa6;
  5589. #endif
  5590. #if defined(__Userspace__)
  5591. struct sockaddr_conn *sac;
  5592. #endif
  5593. uint16_t ptype, plen;
  5594. bool fnd, check_src;
  5595. *op_err = NULL;
  5596. #ifdef INET
  5597. memset(&sin4, 0, sizeof(sin4));
  5598. sin4.sin_family = AF_INET;
  5599. #ifdef HAVE_SIN_LEN
  5600. sin4.sin_len = sizeof(sin4);
  5601. #endif
  5602. #endif
  5603. #ifdef INET6
  5604. memset(&sin6, 0, sizeof(sin6));
  5605. sin6.sin6_family = AF_INET6;
  5606. #ifdef HAVE_SIN6_LEN
  5607. sin6.sin6_len = sizeof(sin6);
  5608. #endif
  5609. #endif
  5610. /* First what about the src address of the pkt ? */
  5611. check_src = false;
  5612. switch (src->sa_family) {
  5613. #ifdef INET
  5614. case AF_INET:
  5615. if (asoc->scope.ipv4_addr_legal) {
  5616. check_src = true;
  5617. }
  5618. break;
  5619. #endif
  5620. #ifdef INET6
  5621. case AF_INET6:
  5622. if (asoc->scope.ipv6_addr_legal) {
  5623. check_src = true;
  5624. }
  5625. break;
  5626. #endif
  5627. #if defined(__Userspace__)
  5628. case AF_CONN:
  5629. if (asoc->scope.conn_addr_legal) {
  5630. check_src = true;
  5631. }
  5632. break;
  5633. #endif
  5634. default:
  5635. /* TSNH */
  5636. break;
  5637. }
  5638. if (check_src) {
  5639. fnd = false;
  5640. TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
  5641. sa = (struct sockaddr *)&net->ro._l_addr;
  5642. if (sa->sa_family == src->sa_family) {
  5643. #ifdef INET
  5644. if (sa->sa_family == AF_INET) {
  5645. struct sockaddr_in *src4;
  5646. sa4 = (struct sockaddr_in *)sa;
  5647. src4 = (struct sockaddr_in *)src;
  5648. if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
  5649. fnd = true;
  5650. break;
  5651. }
  5652. }
  5653. #endif
  5654. #ifdef INET6
  5655. if (sa->sa_family == AF_INET6) {
  5656. struct sockaddr_in6 *src6;
  5657. sa6 = (struct sockaddr_in6 *)sa;
  5658. src6 = (struct sockaddr_in6 *)src;
  5659. if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
  5660. fnd = true;
  5661. break;
  5662. }
  5663. }
  5664. #endif
  5665. #if defined(__Userspace__)
  5666. if (sa->sa_family == AF_CONN) {
  5667. struct sockaddr_conn *srcc;
  5668. sac = (struct sockaddr_conn *)sa;
  5669. srcc = (struct sockaddr_conn *)src;
  5670. if (sac->sconn_addr == srcc->sconn_addr) {
  5671. fnd = true;
  5672. break;
  5673. }
  5674. }
  5675. #endif
  5676. }
  5677. }
  5678. if (!fnd) {
  5679. /*
  5680. * If sending an ABORT in case of an additional address,
  5681. * don't use the new address error cause.
  5682. * This looks no different than if no listener was
  5683. * present.
  5684. */
  5685. *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
  5686. return (true);
  5687. }
  5688. }
  5689. /* Ok so far lets munge through the rest of the packet */
  5690. offset += sizeof(struct sctp_init_chunk);
  5691. phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
  5692. while (phdr) {
  5693. sa_touse = NULL;
  5694. ptype = ntohs(phdr->param_type);
  5695. plen = ntohs(phdr->param_length);
  5696. if (offset + plen > limit) {
  5697. *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
  5698. return (true);
  5699. }
  5700. if (plen < sizeof(struct sctp_paramhdr)) {
  5701. *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
  5702. return (true);
  5703. }
  5704. switch (ptype) {
  5705. #ifdef INET
  5706. case SCTP_IPV4_ADDRESS:
  5707. {
  5708. struct sctp_ipv4addr_param *p4, p4_buf;
  5709. if (plen != sizeof(struct sctp_ipv4addr_param)) {
  5710. *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
  5711. return (true);
  5712. }
  5713. phdr = sctp_get_next_param(in_initpkt, offset,
  5714. (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
  5715. if (phdr == NULL) {
  5716. *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
  5717. return (true);
  5718. }
  5719. if (asoc->scope.ipv4_addr_legal) {
  5720. p4 = (struct sctp_ipv4addr_param *)phdr;
  5721. sin4.sin_addr.s_addr = p4->addr;
  5722. sa_touse = (struct sockaddr *)&sin4;
  5723. }
  5724. break;
  5725. }
  5726. #endif
  5727. #ifdef INET6
  5728. case SCTP_IPV6_ADDRESS:
  5729. {
  5730. struct sctp_ipv6addr_param *p6, p6_buf;
  5731. if (plen != sizeof(struct sctp_ipv6addr_param)) {
  5732. *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
  5733. return (true);
  5734. }
  5735. phdr = sctp_get_next_param(in_initpkt, offset,
  5736. (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
  5737. if (phdr == NULL) {
  5738. *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
  5739. return (true);
  5740. }
  5741. if (asoc->scope.ipv6_addr_legal) {
  5742. p6 = (struct sctp_ipv6addr_param *)phdr;
  5743. memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
  5744. sizeof(p6->addr));
  5745. sa_touse = (struct sockaddr *)&sin6;
  5746. }
  5747. break;
  5748. }
  5749. #endif
  5750. default:
  5751. sa_touse = NULL;
  5752. break;
  5753. }
  5754. if (sa_touse) {
  5755. /* ok, sa_touse points to one to check */
  5756. fnd = false;
  5757. TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
  5758. sa = (struct sockaddr *)&net->ro._l_addr;
  5759. if (sa->sa_family != sa_touse->sa_family) {
  5760. continue;
  5761. }
  5762. #ifdef INET
  5763. if (sa->sa_family == AF_INET) {
  5764. sa4 = (struct sockaddr_in *)sa;
  5765. if (sa4->sin_addr.s_addr ==
  5766. sin4.sin_addr.s_addr) {
  5767. fnd = true;
  5768. break;
  5769. }
  5770. }
  5771. #endif
  5772. #ifdef INET6
  5773. if (sa->sa_family == AF_INET6) {
  5774. sa6 = (struct sockaddr_in6 *)sa;
  5775. if (SCTP6_ARE_ADDR_EQUAL(
  5776. sa6, &sin6)) {
  5777. fnd = true;
  5778. break;
  5779. }
  5780. }
  5781. #endif
  5782. }
  5783. if (!fnd) {
  5784. /*
  5785. * If sending an ABORT in case of an additional
  5786. * address, don't use the new address error
  5787. * cause.
  5788. * This looks no different than if no listener
  5789. * was present.
  5790. */
  5791. *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
  5792. return (true);
  5793. }
  5794. }
  5795. offset += SCTP_SIZE32(plen);
  5796. if (offset >= limit) {
  5797. break;
  5798. }
  5799. phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
  5800. }
  5801. return (false);
  5802. }
  5803. /*
  5804. * Given a MBUF chain that was sent into us containing an INIT. Build a
  5805. * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
  5806. * a pullup to include IPv6/4header, SCTP header and initial part of INIT
  5807. * message (i.e. the struct sctp_init_msg).
  5808. */
  5809. void
  5810. sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
  5811. struct sctp_nets *src_net, struct mbuf *init_pkt,
  5812. int iphlen, int offset,
  5813. struct sockaddr *src, struct sockaddr *dst,
  5814. struct sctphdr *sh, struct sctp_init_chunk *init_chk,
  5815. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5816. uint8_t mflowtype, uint32_t mflowid,
  5817. #endif
  5818. uint32_t vrf_id, uint16_t port)
  5819. {
  5820. struct sctp_association *asoc;
  5821. struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
  5822. struct sctp_init_ack_chunk *initack;
  5823. struct sctp_adaptation_layer_indication *ali;
  5824. struct sctp_supported_chunk_types_param *pr_supported;
  5825. struct sctp_paramhdr *ph;
  5826. union sctp_sockstore *over_addr;
  5827. struct sctp_scoping scp;
  5828. struct timeval now;
  5829. #ifdef INET
  5830. struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
  5831. struct sockaddr_in *src4 = (struct sockaddr_in *)src;
  5832. struct sockaddr_in *sin;
  5833. #endif
  5834. #ifdef INET6
  5835. struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
  5836. struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
  5837. struct sockaddr_in6 *sin6;
  5838. #endif
  5839. #if defined(__Userspace__)
  5840. struct sockaddr_conn *dstconn = (struct sockaddr_conn *)dst;
  5841. struct sockaddr_conn *srcconn = (struct sockaddr_conn *)src;
  5842. struct sockaddr_conn *sconn;
  5843. #endif
  5844. struct sockaddr *to;
  5845. struct sctp_state_cookie stc;
  5846. struct sctp_nets *net = NULL;
  5847. uint8_t *signature = NULL;
  5848. int cnt_inits_to = 0;
  5849. uint16_t his_limit, i_want;
  5850. int abort_flag;
  5851. int nat_friendly = 0;
  5852. int error;
  5853. struct socket *so;
  5854. uint16_t num_ext, chunk_len, padding_len, parameter_len;
  5855. if (stcb) {
  5856. asoc = &stcb->asoc;
  5857. } else {
  5858. asoc = NULL;
  5859. }
  5860. if ((asoc != NULL) &&
  5861. (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
  5862. if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
  5863. /*
  5864. * new addresses, out of here in non-cookie-wait states
  5865. */
  5866. sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
  5867. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5868. mflowtype, mflowid, inp->fibnum,
  5869. #endif
  5870. vrf_id, port);
  5871. return;
  5872. }
  5873. if (src_net != NULL && (src_net->port != port)) {
  5874. /*
  5875. * change of remote encapsulation port, out of here in
  5876. * non-cookie-wait states
  5877. *
  5878. * Send an ABORT, without an specific error cause.
  5879. * This looks no different than if no listener
  5880. * was present.
  5881. */
  5882. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  5883. "Remote encapsulation port changed");
  5884. sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
  5885. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5886. mflowtype, mflowid, inp->fibnum,
  5887. #endif
  5888. vrf_id, port);
  5889. return;
  5890. }
  5891. }
  5892. abort_flag = 0;
  5893. op_err = sctp_arethere_unrecognized_parameters(init_pkt,
  5894. (offset + sizeof(struct sctp_init_chunk)),
  5895. &abort_flag,
  5896. (struct sctp_chunkhdr *)init_chk,
  5897. &nat_friendly, NULL);
  5898. if (abort_flag) {
  5899. do_a_abort:
  5900. if (op_err == NULL) {
  5901. char msg[SCTP_DIAG_INFO_LEN];
  5902. SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
  5903. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  5904. msg);
  5905. }
  5906. sctp_send_abort(init_pkt, iphlen, src, dst, sh,
  5907. init_chk->init.initiate_tag, op_err,
  5908. #if defined(__FreeBSD__) && !defined(__Userspace__)
  5909. mflowtype, mflowid, inp->fibnum,
  5910. #endif
  5911. vrf_id, port);
  5912. return;
  5913. }
  5914. m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  5915. if (m == NULL) {
  5916. /* No memory, INIT timer will re-attempt. */
  5917. sctp_m_freem(op_err);
  5918. return;
  5919. }
  5920. chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
  5921. padding_len = 0;
  5922. /*
  5923. * We might not overwrite the identification[] completely and on
  5924. * some platforms time_entered will contain some padding.
  5925. * Therefore zero out the cookie to avoid putting
  5926. * uninitialized memory on the wire.
  5927. */
  5928. memset(&stc, 0, sizeof(struct sctp_state_cookie));
  5929. /* the time I built cookie */
  5930. (void)SCTP_GETTIME_TIMEVAL(&now);
  5931. stc.time_entered.tv_sec = now.tv_sec;
  5932. stc.time_entered.tv_usec = now.tv_usec;
  5933. /* populate any tie tags */
  5934. if (asoc != NULL) {
  5935. /* unlock before tag selections */
  5936. stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
  5937. stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
  5938. stc.cookie_life = asoc->cookie_life;
  5939. net = asoc->primary_destination;
  5940. } else {
  5941. stc.tie_tag_my_vtag = 0;
  5942. stc.tie_tag_peer_vtag = 0;
  5943. /* life I will award this cookie */
  5944. stc.cookie_life = inp->sctp_ep.def_cookie_life;
  5945. }
  5946. /* copy in the ports for later check */
  5947. stc.myport = sh->dest_port;
  5948. stc.peerport = sh->src_port;
  5949. /*
  5950. * If we wanted to honor cookie life extensions, we would add to
  5951. * stc.cookie_life. For now we should NOT honor any extension
  5952. */
  5953. stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
  5954. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  5955. stc.ipv6_addr_legal = 1;
  5956. if (SCTP_IPV6_V6ONLY(inp)) {
  5957. stc.ipv4_addr_legal = 0;
  5958. } else {
  5959. stc.ipv4_addr_legal = 1;
  5960. }
  5961. #if defined(__Userspace__)
  5962. stc.conn_addr_legal = 0;
  5963. #endif
  5964. } else {
  5965. stc.ipv6_addr_legal = 0;
  5966. #if defined(__Userspace__)
  5967. if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
  5968. stc.conn_addr_legal = 1;
  5969. stc.ipv4_addr_legal = 0;
  5970. } else {
  5971. stc.conn_addr_legal = 0;
  5972. stc.ipv4_addr_legal = 1;
  5973. }
  5974. #else
  5975. stc.ipv4_addr_legal = 1;
  5976. #endif
  5977. }
  5978. stc.ipv4_scope = 0;
  5979. if (net == NULL) {
  5980. to = src;
  5981. switch (dst->sa_family) {
  5982. #ifdef INET
  5983. case AF_INET:
  5984. {
  5985. /* lookup address */
  5986. stc.address[0] = src4->sin_addr.s_addr;
  5987. stc.address[1] = 0;
  5988. stc.address[2] = 0;
  5989. stc.address[3] = 0;
  5990. stc.addr_type = SCTP_IPV4_ADDRESS;
  5991. /* local from address */
  5992. stc.laddress[0] = dst4->sin_addr.s_addr;
  5993. stc.laddress[1] = 0;
  5994. stc.laddress[2] = 0;
  5995. stc.laddress[3] = 0;
  5996. stc.laddr_type = SCTP_IPV4_ADDRESS;
  5997. /* scope_id is only for v6 */
  5998. stc.scope_id = 0;
  5999. if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
  6000. (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
  6001. stc.ipv4_scope = 1;
  6002. }
  6003. /* Must use the address in this case */
  6004. if (sctp_is_address_on_local_host(src, vrf_id)) {
  6005. stc.loopback_scope = 1;
  6006. stc.ipv4_scope = 1;
  6007. stc.site_scope = 1;
  6008. stc.local_scope = 0;
  6009. }
  6010. break;
  6011. }
  6012. #endif
  6013. #ifdef INET6
  6014. case AF_INET6:
  6015. {
  6016. stc.addr_type = SCTP_IPV6_ADDRESS;
  6017. memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
  6018. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6019. stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
  6020. #else
  6021. stc.scope_id = 0;
  6022. #endif
  6023. if (sctp_is_address_on_local_host(src, vrf_id)) {
  6024. stc.loopback_scope = 1;
  6025. stc.local_scope = 0;
  6026. stc.site_scope = 1;
  6027. stc.ipv4_scope = 1;
  6028. } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
  6029. IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
  6030. /*
  6031. * If the new destination or source is a
  6032. * LINK_LOCAL we must have common both site and
  6033. * local scope. Don't set local scope though
  6034. * since we must depend on the source to be
  6035. * added implicitly. We cannot assure just
  6036. * because we share one link that all links are
  6037. * common.
  6038. */
  6039. #if defined(__APPLE__) && !defined(__Userspace__)
  6040. /* Mac OS X currently doesn't have in6_getscope() */
  6041. stc.scope_id = src6->sin6_addr.s6_addr16[1];
  6042. #endif
  6043. stc.local_scope = 0;
  6044. stc.site_scope = 1;
  6045. stc.ipv4_scope = 1;
  6046. /*
  6047. * we start counting for the private address
  6048. * stuff at 1. since the link local we
  6049. * source from won't show up in our scoped
  6050. * count.
  6051. */
  6052. cnt_inits_to = 1;
  6053. /* pull out the scope_id from incoming pkt */
  6054. } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
  6055. IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
  6056. /*
  6057. * If the new destination or source is
  6058. * SITE_LOCAL then we must have site scope in
  6059. * common.
  6060. */
  6061. stc.site_scope = 1;
  6062. }
  6063. memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
  6064. stc.laddr_type = SCTP_IPV6_ADDRESS;
  6065. break;
  6066. }
  6067. #endif
  6068. #if defined(__Userspace__)
  6069. case AF_CONN:
  6070. {
  6071. /* lookup address */
  6072. stc.address[0] = 0;
  6073. stc.address[1] = 0;
  6074. stc.address[2] = 0;
  6075. stc.address[3] = 0;
  6076. memcpy(&stc.address, &srcconn->sconn_addr, sizeof(void *));
  6077. stc.addr_type = SCTP_CONN_ADDRESS;
  6078. /* local from address */
  6079. stc.laddress[0] = 0;
  6080. stc.laddress[1] = 0;
  6081. stc.laddress[2] = 0;
  6082. stc.laddress[3] = 0;
  6083. memcpy(&stc.laddress, &dstconn->sconn_addr, sizeof(void *));
  6084. stc.laddr_type = SCTP_CONN_ADDRESS;
  6085. /* scope_id is only for v6 */
  6086. stc.scope_id = 0;
  6087. break;
  6088. }
  6089. #endif
  6090. default:
  6091. /* TSNH */
  6092. goto do_a_abort;
  6093. break;
  6094. }
  6095. } else {
  6096. /* set the scope per the existing tcb */
  6097. #ifdef INET6
  6098. struct sctp_nets *lnet;
  6099. #endif
  6100. stc.loopback_scope = asoc->scope.loopback_scope;
  6101. stc.ipv4_scope = asoc->scope.ipv4_local_scope;
  6102. stc.site_scope = asoc->scope.site_scope;
  6103. stc.local_scope = asoc->scope.local_scope;
  6104. #ifdef INET6
  6105. /* Why do we not consider IPv4 LL addresses? */
  6106. TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
  6107. if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
  6108. if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
  6109. /*
  6110. * if we have a LL address, start
  6111. * counting at 1.
  6112. */
  6113. cnt_inits_to = 1;
  6114. }
  6115. }
  6116. }
  6117. #endif
  6118. /* use the net pointer */
  6119. to = (struct sockaddr *)&net->ro._l_addr;
  6120. switch (to->sa_family) {
  6121. #ifdef INET
  6122. case AF_INET:
  6123. sin = (struct sockaddr_in *)to;
  6124. stc.address[0] = sin->sin_addr.s_addr;
  6125. stc.address[1] = 0;
  6126. stc.address[2] = 0;
  6127. stc.address[3] = 0;
  6128. stc.addr_type = SCTP_IPV4_ADDRESS;
  6129. if (net->src_addr_selected == 0) {
  6130. /*
  6131. * strange case here, the INIT should have
  6132. * did the selection.
  6133. */
  6134. net->ro._s_addr = sctp_source_address_selection(inp,
  6135. stcb, (sctp_route_t *)&net->ro,
  6136. net, 0, vrf_id);
  6137. if (net->ro._s_addr == NULL) {
  6138. sctp_m_freem(op_err);
  6139. sctp_m_freem(m);
  6140. return;
  6141. }
  6142. net->src_addr_selected = 1;
  6143. }
  6144. stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
  6145. stc.laddress[1] = 0;
  6146. stc.laddress[2] = 0;
  6147. stc.laddress[3] = 0;
  6148. stc.laddr_type = SCTP_IPV4_ADDRESS;
  6149. /* scope_id is only for v6 */
  6150. stc.scope_id = 0;
  6151. break;
  6152. #endif
  6153. #ifdef INET6
  6154. case AF_INET6:
  6155. sin6 = (struct sockaddr_in6 *)to;
  6156. memcpy(&stc.address, &sin6->sin6_addr,
  6157. sizeof(struct in6_addr));
  6158. stc.addr_type = SCTP_IPV6_ADDRESS;
  6159. stc.scope_id = sin6->sin6_scope_id;
  6160. if (net->src_addr_selected == 0) {
  6161. /*
  6162. * strange case here, the INIT should have
  6163. * done the selection.
  6164. */
  6165. net->ro._s_addr = sctp_source_address_selection(inp,
  6166. stcb, (sctp_route_t *)&net->ro,
  6167. net, 0, vrf_id);
  6168. if (net->ro._s_addr == NULL) {
  6169. sctp_m_freem(op_err);
  6170. sctp_m_freem(m);
  6171. return;
  6172. }
  6173. net->src_addr_selected = 1;
  6174. }
  6175. memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
  6176. sizeof(struct in6_addr));
  6177. stc.laddr_type = SCTP_IPV6_ADDRESS;
  6178. break;
  6179. #endif
  6180. #if defined(__Userspace__)
  6181. case AF_CONN:
  6182. sconn = (struct sockaddr_conn *)to;
  6183. stc.address[0] = 0;
  6184. stc.address[1] = 0;
  6185. stc.address[2] = 0;
  6186. stc.address[3] = 0;
  6187. memcpy(&stc.address, &sconn->sconn_addr, sizeof(void *));
  6188. stc.addr_type = SCTP_CONN_ADDRESS;
  6189. stc.laddress[0] = 0;
  6190. stc.laddress[1] = 0;
  6191. stc.laddress[2] = 0;
  6192. stc.laddress[3] = 0;
  6193. memcpy(&stc.laddress, &sconn->sconn_addr, sizeof(void *));
  6194. stc.laddr_type = SCTP_CONN_ADDRESS;
  6195. stc.scope_id = 0;
  6196. break;
  6197. #endif
  6198. }
  6199. }
  6200. /* Now lets put the SCTP header in place */
  6201. initack = mtod(m, struct sctp_init_ack_chunk *);
  6202. /* Save it off for quick ref */
  6203. stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
  6204. /* who are we */
  6205. memcpy(stc.identification, SCTP_VERSION_STRING,
  6206. min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
  6207. memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
  6208. /* now the chunk header */
  6209. initack->ch.chunk_type = SCTP_INITIATION_ACK;
  6210. initack->ch.chunk_flags = 0;
  6211. /* fill in later from mbuf we build */
  6212. initack->ch.chunk_length = 0;
  6213. /* place in my tag */
  6214. if ((asoc != NULL) &&
  6215. ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  6216. (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
  6217. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
  6218. /* re-use the v-tags and init-seq here */
  6219. initack->init.initiate_tag = htonl(asoc->my_vtag);
  6220. initack->init.initial_tsn = htonl(asoc->init_seq_number);
  6221. } else {
  6222. uint32_t vtag, itsn;
  6223. if (asoc) {
  6224. atomic_add_int(&asoc->refcnt, 1);
  6225. SCTP_TCB_UNLOCK(stcb);
  6226. new_tag:
  6227. SCTP_INP_INFO_RLOCK();
  6228. vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
  6229. SCTP_INP_INFO_RUNLOCK();
  6230. if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
  6231. /* Got a duplicate vtag on some guy behind a nat
  6232. * make sure we don't use it.
  6233. */
  6234. goto new_tag;
  6235. }
  6236. initack->init.initiate_tag = htonl(vtag);
  6237. /* get a TSN to use too */
  6238. itsn = sctp_select_initial_TSN(&inp->sctp_ep);
  6239. initack->init.initial_tsn = htonl(itsn);
  6240. SCTP_TCB_LOCK(stcb);
  6241. atomic_subtract_int(&asoc->refcnt, 1);
  6242. } else {
  6243. SCTP_INP_INCR_REF(inp);
  6244. SCTP_INP_RUNLOCK(inp);
  6245. SCTP_INP_INFO_RLOCK();
  6246. vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
  6247. SCTP_INP_INFO_RUNLOCK();
  6248. initack->init.initiate_tag = htonl(vtag);
  6249. /* get a TSN to use too */
  6250. initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
  6251. SCTP_INP_RLOCK(inp);
  6252. SCTP_INP_DECR_REF(inp);
  6253. }
  6254. }
  6255. /* save away my tag to */
  6256. stc.my_vtag = initack->init.initiate_tag;
  6257. /* set up some of the credits. */
  6258. so = inp->sctp_socket;
  6259. if (so == NULL) {
  6260. /* memory problem */
  6261. sctp_m_freem(op_err);
  6262. sctp_m_freem(m);
  6263. return;
  6264. } else {
  6265. initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
  6266. }
  6267. /* set what I want */
  6268. his_limit = ntohs(init_chk->init.num_inbound_streams);
  6269. /* choose what I want */
  6270. if (asoc != NULL) {
  6271. if (asoc->streamoutcnt > asoc->pre_open_streams) {
  6272. i_want = asoc->streamoutcnt;
  6273. } else {
  6274. i_want = asoc->pre_open_streams;
  6275. }
  6276. } else {
  6277. i_want = inp->sctp_ep.pre_open_stream_count;
  6278. }
  6279. if (his_limit < i_want) {
  6280. /* I Want more :< */
  6281. initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
  6282. } else {
  6283. /* I can have what I want :> */
  6284. initack->init.num_outbound_streams = htons(i_want);
  6285. }
  6286. /* tell him his limit. */
  6287. initack->init.num_inbound_streams =
  6288. htons(inp->sctp_ep.max_open_streams_intome);
  6289. /* adaptation layer indication parameter */
  6290. if (inp->sctp_ep.adaptation_layer_indicator_provided) {
  6291. parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
  6292. ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len);
  6293. ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
  6294. ali->ph.param_length = htons(parameter_len);
  6295. ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
  6296. chunk_len += parameter_len;
  6297. }
  6298. /* ECN parameter */
  6299. if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
  6300. ((asoc == NULL) && (inp->ecn_supported == 1))) {
  6301. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  6302. ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
  6303. ph->param_type = htons(SCTP_ECN_CAPABLE);
  6304. ph->param_length = htons(parameter_len);
  6305. chunk_len += parameter_len;
  6306. }
  6307. /* PR-SCTP supported parameter */
  6308. if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
  6309. ((asoc == NULL) && (inp->prsctp_supported == 1))) {
  6310. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  6311. ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
  6312. ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
  6313. ph->param_length = htons(parameter_len);
  6314. chunk_len += parameter_len;
  6315. }
  6316. /* Add NAT friendly parameter */
  6317. if (nat_friendly) {
  6318. parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
  6319. ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
  6320. ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
  6321. ph->param_length = htons(parameter_len);
  6322. chunk_len += parameter_len;
  6323. }
  6324. /* And now tell the peer which extensions we support */
  6325. num_ext = 0;
  6326. pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len);
  6327. if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
  6328. ((asoc == NULL) && (inp->prsctp_supported == 1))) {
  6329. pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
  6330. if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
  6331. ((asoc == NULL) && (inp->idata_supported == 1))) {
  6332. pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
  6333. }
  6334. }
  6335. if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
  6336. ((asoc == NULL) && (inp->auth_supported == 1))) {
  6337. pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
  6338. }
  6339. if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
  6340. ((asoc == NULL) && (inp->asconf_supported == 1))) {
  6341. pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
  6342. pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
  6343. }
  6344. if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
  6345. ((asoc == NULL) && (inp->reconfig_supported == 1))) {
  6346. pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
  6347. }
  6348. if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
  6349. ((asoc == NULL) && (inp->idata_supported == 1))) {
  6350. pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
  6351. }
  6352. if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
  6353. ((asoc == NULL) && (inp->nrsack_supported == 1))) {
  6354. pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
  6355. }
  6356. if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
  6357. ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
  6358. pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
  6359. }
  6360. if (num_ext > 0) {
  6361. parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
  6362. pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
  6363. pr_supported->ph.param_length = htons(parameter_len);
  6364. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  6365. chunk_len += parameter_len;
  6366. }
  6367. /* add authentication parameters */
  6368. if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
  6369. ((asoc == NULL) && (inp->auth_supported == 1))) {
  6370. struct sctp_auth_random *randp;
  6371. struct sctp_auth_hmac_algo *hmacs;
  6372. struct sctp_auth_chunk_list *chunks;
  6373. if (padding_len > 0) {
  6374. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  6375. chunk_len += padding_len;
  6376. padding_len = 0;
  6377. }
  6378. /* generate and add RANDOM parameter */
  6379. randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len);
  6380. parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
  6381. SCTP_AUTH_RANDOM_SIZE_DEFAULT;
  6382. randp->ph.param_type = htons(SCTP_RANDOM);
  6383. randp->ph.param_length = htons(parameter_len);
  6384. SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
  6385. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  6386. chunk_len += parameter_len;
  6387. if (padding_len > 0) {
  6388. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  6389. chunk_len += padding_len;
  6390. padding_len = 0;
  6391. }
  6392. /* add HMAC_ALGO parameter */
  6393. hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len);
  6394. parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
  6395. sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
  6396. (uint8_t *)hmacs->hmac_ids);
  6397. hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
  6398. hmacs->ph.param_length = htons(parameter_len);
  6399. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  6400. chunk_len += parameter_len;
  6401. if (padding_len > 0) {
  6402. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  6403. chunk_len += padding_len;
  6404. padding_len = 0;
  6405. }
  6406. /* add CHUNKS parameter */
  6407. chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len);
  6408. parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
  6409. sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
  6410. chunks->chunk_types);
  6411. chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
  6412. chunks->ph.param_length = htons(parameter_len);
  6413. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  6414. chunk_len += parameter_len;
  6415. }
  6416. SCTP_BUF_LEN(m) = chunk_len;
  6417. m_last = m;
  6418. /* now the addresses */
  6419. /* To optimize this we could put the scoping stuff
  6420. * into a structure and remove the individual uint8's from
  6421. * the stc structure. Then we could just sifa in the
  6422. * address within the stc.. but for now this is a quick
  6423. * hack to get the address stuff teased apart.
  6424. */
  6425. scp.ipv4_addr_legal = stc.ipv4_addr_legal;
  6426. scp.ipv6_addr_legal = stc.ipv6_addr_legal;
  6427. #if defined(__Userspace__)
  6428. scp.conn_addr_legal = stc.conn_addr_legal;
  6429. #endif
  6430. scp.loopback_scope = stc.loopback_scope;
  6431. scp.ipv4_local_scope = stc.ipv4_scope;
  6432. scp.local_scope = stc.local_scope;
  6433. scp.site_scope = stc.site_scope;
  6434. m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
  6435. cnt_inits_to,
  6436. &padding_len, &chunk_len);
  6437. /* padding_len can only be positive, if no addresses have been added */
  6438. if (padding_len > 0) {
  6439. memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
  6440. chunk_len += padding_len;
  6441. SCTP_BUF_LEN(m) += padding_len;
  6442. padding_len = 0;
  6443. }
  6444. /* tack on the operational error if present */
  6445. if (op_err) {
  6446. parameter_len = 0;
  6447. for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
  6448. parameter_len += SCTP_BUF_LEN(m_tmp);
  6449. }
  6450. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  6451. SCTP_BUF_NEXT(m_last) = op_err;
  6452. while (SCTP_BUF_NEXT(m_last) != NULL) {
  6453. m_last = SCTP_BUF_NEXT(m_last);
  6454. }
  6455. chunk_len += parameter_len;
  6456. }
  6457. if (padding_len > 0) {
  6458. m_last = sctp_add_pad_tombuf(m_last, padding_len);
  6459. if (m_last == NULL) {
  6460. /* Houston we have a problem, no space */
  6461. sctp_m_freem(m);
  6462. return;
  6463. }
  6464. chunk_len += padding_len;
  6465. padding_len = 0;
  6466. }
  6467. /* Now we must build a cookie */
  6468. m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
  6469. if (m_cookie == NULL) {
  6470. /* memory problem */
  6471. sctp_m_freem(m);
  6472. return;
  6473. }
  6474. /* Now append the cookie to the end and update the space/size */
  6475. SCTP_BUF_NEXT(m_last) = m_cookie;
  6476. parameter_len = 0;
  6477. for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
  6478. parameter_len += SCTP_BUF_LEN(m_tmp);
  6479. if (SCTP_BUF_NEXT(m_tmp) == NULL) {
  6480. m_last = m_tmp;
  6481. }
  6482. }
  6483. padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
  6484. chunk_len += parameter_len;
  6485. /* Place in the size, but we don't include
  6486. * the last pad (if any) in the INIT-ACK.
  6487. */
  6488. initack->ch.chunk_length = htons(chunk_len);
  6489. /* Time to sign the cookie, we don't sign over the cookie
  6490. * signature though thus we set trailer.
  6491. */
  6492. (void)sctp_hmac_m(SCTP_HMAC,
  6493. (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
  6494. SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
  6495. (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
  6496. #if defined(__Userspace__)
  6497. /*
  6498. * Don't put AF_CONN addresses on the wire, in case this is critical
  6499. * for the application. However, they are protected by the HMAC and
  6500. * need to be reconstructed before checking the HMAC.
  6501. * Clearing is only done in the mbuf chain, since the local stc is
  6502. * not used anymore.
  6503. */
  6504. if (stc.addr_type == SCTP_CONN_ADDRESS) {
  6505. const void *p = NULL;
  6506. m_copyback(m_cookie, sizeof(struct sctp_paramhdr) + offsetof(struct sctp_state_cookie, address),
  6507. (int)sizeof(void *), (caddr_t)&p);
  6508. }
  6509. if (stc.laddr_type == SCTP_CONN_ADDRESS) {
  6510. const void *p = NULL;
  6511. m_copyback(m_cookie, sizeof(struct sctp_paramhdr) + offsetof(struct sctp_state_cookie, laddress),
  6512. (int)sizeof(void *), (caddr_t)&p);
  6513. }
  6514. #endif
  6515. /*
  6516. * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
  6517. * here since the timer will drive a retranmission.
  6518. */
  6519. if (padding_len > 0) {
  6520. if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
  6521. sctp_m_freem(m);
  6522. return;
  6523. }
  6524. }
  6525. if (stc.loopback_scope) {
  6526. over_addr = (union sctp_sockstore *)dst;
  6527. } else {
  6528. over_addr = NULL;
  6529. }
  6530. if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
  6531. 0, 0,
  6532. inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
  6533. port, over_addr,
  6534. #if defined(__FreeBSD__) && !defined(__Userspace__)
  6535. mflowtype, mflowid,
  6536. #endif
  6537. SCTP_SO_NOT_LOCKED))) {
  6538. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
  6539. if (error == ENOBUFS) {
  6540. if (asoc != NULL) {
  6541. asoc->ifp_had_enobuf = 1;
  6542. }
  6543. SCTP_STAT_INCR(sctps_lowlevelerr);
  6544. }
  6545. } else {
  6546. if (asoc != NULL) {
  6547. asoc->ifp_had_enobuf = 0;
  6548. }
  6549. }
  6550. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  6551. }
  6552. static void
  6553. sctp_prune_prsctp(struct sctp_tcb *stcb,
  6554. struct sctp_association *asoc,
  6555. struct sctp_nonpad_sndrcvinfo *srcv,
  6556. int dataout)
  6557. {
  6558. int freed_spc = 0;
  6559. struct sctp_tmit_chunk *chk, *nchk;
  6560. SCTP_TCB_LOCK_ASSERT(stcb);
  6561. if ((asoc->prsctp_supported) &&
  6562. (asoc->sent_queue_cnt_removeable > 0)) {
  6563. TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
  6564. /*
  6565. * Look for chunks marked with the PR_SCTP flag AND
  6566. * the buffer space flag. If the one being sent is
  6567. * equal or greater priority then purge the old one
  6568. * and free some space.
  6569. */
  6570. if (PR_SCTP_BUF_ENABLED(chk->flags)) {
  6571. /*
  6572. * This one is PR-SCTP AND buffer space
  6573. * limited type
  6574. */
  6575. if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
  6576. /*
  6577. * Lower numbers equates to higher
  6578. * priority. So if the one we are
  6579. * looking at has a larger priority,
  6580. * we want to drop the data and NOT
  6581. * retransmit it.
  6582. */
  6583. if (chk->data) {
  6584. /*
  6585. * We release the book_size
  6586. * if the mbuf is here
  6587. */
  6588. int ret_spc;
  6589. uint8_t sent;
  6590. if (chk->sent > SCTP_DATAGRAM_UNSENT)
  6591. sent = 1;
  6592. else
  6593. sent = 0;
  6594. ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
  6595. sent,
  6596. SCTP_SO_LOCKED);
  6597. freed_spc += ret_spc;
  6598. if (freed_spc >= dataout) {
  6599. return;
  6600. }
  6601. } /* if chunk was present */
  6602. } /* if of sufficient priority */
  6603. } /* if chunk has enabled */
  6604. } /* tailqforeach */
  6605. TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
  6606. /* Here we must move to the sent queue and mark */
  6607. if (PR_SCTP_BUF_ENABLED(chk->flags)) {
  6608. if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
  6609. if (chk->data) {
  6610. /*
  6611. * We release the book_size
  6612. * if the mbuf is here
  6613. */
  6614. int ret_spc;
  6615. ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
  6616. 0, SCTP_SO_LOCKED);
  6617. freed_spc += ret_spc;
  6618. if (freed_spc >= dataout) {
  6619. return;
  6620. }
  6621. } /* end if chk->data */
  6622. } /* end if right class */
  6623. } /* end if chk pr-sctp */
  6624. } /* tailqforeachsafe (chk) */
  6625. } /* if enabled in asoc */
  6626. }
  6627. uint32_t
  6628. sctp_get_frag_point(struct sctp_tcb *stcb)
  6629. {
  6630. struct sctp_association *asoc;
  6631. uint32_t frag_point, overhead;
  6632. asoc = &stcb->asoc;
  6633. /* Consider IP header and SCTP common header. */
  6634. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  6635. overhead = SCTP_MIN_OVERHEAD;
  6636. } else {
  6637. #if defined(__Userspace__)
  6638. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
  6639. overhead = sizeof(struct sctphdr);
  6640. } else {
  6641. overhead = SCTP_MIN_V4_OVERHEAD;
  6642. }
  6643. #else
  6644. overhead = SCTP_MIN_V4_OVERHEAD;
  6645. #endif
  6646. }
  6647. /* Consider DATA/IDATA chunk header and AUTH header, if needed. */
  6648. if (asoc->idata_supported) {
  6649. overhead += sizeof(struct sctp_idata_chunk);
  6650. if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
  6651. overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
  6652. }
  6653. } else {
  6654. overhead += sizeof(struct sctp_data_chunk);
  6655. if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
  6656. overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
  6657. }
  6658. }
  6659. KASSERT(overhead % 4 == 0,
  6660. ("overhead (%u) not a multiple of 4", overhead));
  6661. /* Consider padding. */
  6662. if (asoc->smallest_mtu % 4 > 0) {
  6663. overhead += (asoc->smallest_mtu % 4);
  6664. }
  6665. KASSERT(asoc->smallest_mtu > overhead,
  6666. ("Association MTU (%u) too small for overhead (%u)",
  6667. asoc->smallest_mtu, overhead));
  6668. frag_point = asoc->smallest_mtu - overhead;
  6669. KASSERT(frag_point % 4 == 0,
  6670. ("frag_point (%u) not a multiple of 4", frag_point));
  6671. /* Honor MAXSEG socket option. */
  6672. if ((asoc->sctp_frag_point > 0) &&
  6673. (asoc->sctp_frag_point < frag_point)) {
  6674. frag_point = asoc->sctp_frag_point;
  6675. }
  6676. return (frag_point);
  6677. }
  6678. static void
  6679. sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
  6680. {
  6681. /*
  6682. * We assume that the user wants PR_SCTP_TTL if the user
  6683. * provides a positive lifetime but does not specify any
  6684. * PR_SCTP policy.
  6685. */
  6686. if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
  6687. sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
  6688. } else if (sp->timetolive > 0) {
  6689. sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
  6690. sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
  6691. } else {
  6692. return;
  6693. }
  6694. switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
  6695. case CHUNK_FLAGS_PR_SCTP_BUF:
  6696. /*
  6697. * Time to live is a priority stored in tv_sec when
  6698. * doing the buffer drop thing.
  6699. */
  6700. sp->ts.tv_sec = sp->timetolive;
  6701. sp->ts.tv_usec = 0;
  6702. break;
  6703. case CHUNK_FLAGS_PR_SCTP_TTL:
  6704. {
  6705. struct timeval tv;
  6706. (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
  6707. tv.tv_sec = sp->timetolive / 1000;
  6708. tv.tv_usec = (sp->timetolive * 1000) % 1000000;
  6709. /* TODO sctp_constants.h needs alternative time macros when
  6710. * _KERNEL is undefined.
  6711. */
  6712. #if !(defined(__FreeBSD__) && !defined(__Userspace__))
  6713. timeradd(&sp->ts, &tv, &sp->ts);
  6714. #else
  6715. timevaladd(&sp->ts, &tv);
  6716. #endif
  6717. }
  6718. break;
  6719. case CHUNK_FLAGS_PR_SCTP_RTX:
  6720. /*
  6721. * Time to live is a the number or retransmissions
  6722. * stored in tv_sec.
  6723. */
  6724. sp->ts.tv_sec = sp->timetolive;
  6725. sp->ts.tv_usec = 0;
  6726. break;
  6727. default:
  6728. SCTPDBG(SCTP_DEBUG_USRREQ1,
  6729. "Unknown PR_SCTP policy %u.\n",
  6730. PR_SCTP_POLICY(sp->sinfo_flags));
  6731. break;
  6732. }
  6733. }
  6734. static int
  6735. sctp_msg_append(struct sctp_tcb *stcb,
  6736. struct sctp_nets *net,
  6737. struct mbuf *m,
  6738. struct sctp_nonpad_sndrcvinfo *srcv)
  6739. {
  6740. int error = 0;
  6741. struct mbuf *at;
  6742. struct sctp_stream_queue_pending *sp = NULL;
  6743. struct sctp_stream_out *strm;
  6744. SCTP_TCB_LOCK_ASSERT(stcb);
  6745. /* Given an mbuf chain, put it
  6746. * into the association send queue and
  6747. * place it on the wheel
  6748. */
  6749. if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
  6750. /* Invalid stream number */
  6751. SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  6752. error = EINVAL;
  6753. goto out_now;
  6754. }
  6755. if ((stcb->asoc.stream_locked) &&
  6756. (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
  6757. SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  6758. error = EINVAL;
  6759. goto out_now;
  6760. }
  6761. if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
  6762. (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
  6763. /*
  6764. * Can't queue any data while stream reset is underway.
  6765. */
  6766. if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
  6767. error = EAGAIN;
  6768. } else {
  6769. error = EINVAL;
  6770. }
  6771. goto out_now;
  6772. }
  6773. /* Now can we send this? */
  6774. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
  6775. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
  6776. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
  6777. (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
  6778. /* got data while shutting down */
  6779. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
  6780. error = EPIPE;
  6781. goto out_now;
  6782. }
  6783. sctp_alloc_a_strmoq(stcb, sp);
  6784. if (sp == NULL) {
  6785. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  6786. error = ENOMEM;
  6787. goto out_now;
  6788. }
  6789. sp->sinfo_flags = srcv->sinfo_flags;
  6790. sp->timetolive = srcv->sinfo_timetolive;
  6791. sp->ppid = srcv->sinfo_ppid;
  6792. sp->context = srcv->sinfo_context;
  6793. sp->fsn = 0;
  6794. if (sp->sinfo_flags & SCTP_ADDR_OVER) {
  6795. sp->net = net;
  6796. atomic_add_int(&sp->net->ref_count, 1);
  6797. } else {
  6798. sp->net = NULL;
  6799. }
  6800. (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
  6801. sp->sid = srcv->sinfo_stream;
  6802. sp->msg_is_complete = 1;
  6803. sp->sender_all_done = 1;
  6804. sp->some_taken = 0;
  6805. sp->data = m;
  6806. sp->tail_mbuf = NULL;
  6807. sctp_set_prsctp_policy(sp);
  6808. /* We could in theory (for sendall) sifa the length
  6809. * in, but we would still have to hunt through the
  6810. * chain since we need to setup the tail_mbuf
  6811. */
  6812. sp->length = 0;
  6813. for (at = m; at; at = SCTP_BUF_NEXT(at)) {
  6814. if (SCTP_BUF_NEXT(at) == NULL)
  6815. sp->tail_mbuf = at;
  6816. sp->length += SCTP_BUF_LEN(at);
  6817. }
  6818. if (srcv->sinfo_keynumber_valid) {
  6819. sp->auth_keyid = srcv->sinfo_keynumber;
  6820. } else {
  6821. sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
  6822. }
  6823. if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
  6824. sctp_auth_key_acquire(stcb, sp->auth_keyid);
  6825. sp->holds_key_ref = 1;
  6826. }
  6827. strm = &stcb->asoc.strmout[srcv->sinfo_stream];
  6828. sctp_snd_sb_alloc(stcb, sp->length);
  6829. atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
  6830. TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
  6831. stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
  6832. m = NULL;
  6833. out_now:
  6834. if (m) {
  6835. sctp_m_freem(m);
  6836. }
  6837. return (error);
  6838. }
  6839. static struct mbuf *
  6840. sctp_copy_mbufchain(struct mbuf *clonechain,
  6841. struct mbuf *outchain,
  6842. struct mbuf **endofchain,
  6843. int can_take_mbuf,
  6844. int sizeofcpy,
  6845. uint8_t copy_by_ref)
  6846. {
  6847. struct mbuf *m;
  6848. struct mbuf *appendchain;
  6849. caddr_t cp;
  6850. int len;
  6851. if (endofchain == NULL) {
  6852. /* error */
  6853. error_out:
  6854. if (outchain)
  6855. sctp_m_freem(outchain);
  6856. return (NULL);
  6857. }
  6858. if (can_take_mbuf) {
  6859. appendchain = clonechain;
  6860. } else {
  6861. if (!copy_by_ref &&
  6862. (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
  6863. /* Its not in a cluster */
  6864. if (*endofchain == NULL) {
  6865. /* lets get a mbuf cluster */
  6866. if (outchain == NULL) {
  6867. /* This is the general case */
  6868. new_mbuf:
  6869. outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
  6870. if (outchain == NULL) {
  6871. goto error_out;
  6872. }
  6873. SCTP_BUF_LEN(outchain) = 0;
  6874. *endofchain = outchain;
  6875. /* get the prepend space */
  6876. SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV+4));
  6877. } else {
  6878. /* We really should not get a NULL in endofchain */
  6879. /* find end */
  6880. m = outchain;
  6881. while (m) {
  6882. if (SCTP_BUF_NEXT(m) == NULL) {
  6883. *endofchain = m;
  6884. break;
  6885. }
  6886. m = SCTP_BUF_NEXT(m);
  6887. }
  6888. /* sanity */
  6889. if (*endofchain == NULL) {
  6890. /* huh, TSNH XXX maybe we should panic */
  6891. sctp_m_freem(outchain);
  6892. goto new_mbuf;
  6893. }
  6894. }
  6895. /* get the new end of length */
  6896. len = (int)M_TRAILINGSPACE(*endofchain);
  6897. } else {
  6898. /* how much is left at the end? */
  6899. len = (int)M_TRAILINGSPACE(*endofchain);
  6900. }
  6901. /* Find the end of the data, for appending */
  6902. cp = (mtod((*endofchain), caddr_t) + SCTP_BUF_LEN((*endofchain)));
  6903. /* Now lets copy it out */
  6904. if (len >= sizeofcpy) {
  6905. /* It all fits, copy it in */
  6906. m_copydata(clonechain, 0, sizeofcpy, cp);
  6907. SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
  6908. } else {
  6909. /* fill up the end of the chain */
  6910. if (len > 0) {
  6911. m_copydata(clonechain, 0, len, cp);
  6912. SCTP_BUF_LEN((*endofchain)) += len;
  6913. /* now we need another one */
  6914. sizeofcpy -= len;
  6915. }
  6916. m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
  6917. if (m == NULL) {
  6918. /* We failed */
  6919. goto error_out;
  6920. }
  6921. SCTP_BUF_NEXT((*endofchain)) = m;
  6922. *endofchain = m;
  6923. cp = mtod((*endofchain), caddr_t);
  6924. m_copydata(clonechain, len, sizeofcpy, cp);
  6925. SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
  6926. }
  6927. return (outchain);
  6928. } else {
  6929. /* copy the old fashion way */
  6930. appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
  6931. #ifdef SCTP_MBUF_LOGGING
  6932. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  6933. sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
  6934. }
  6935. #endif
  6936. }
  6937. }
  6938. if (appendchain == NULL) {
  6939. /* error */
  6940. if (outchain)
  6941. sctp_m_freem(outchain);
  6942. return (NULL);
  6943. }
  6944. if (outchain) {
  6945. /* tack on to the end */
  6946. if (*endofchain != NULL) {
  6947. SCTP_BUF_NEXT(((*endofchain))) = appendchain;
  6948. } else {
  6949. m = outchain;
  6950. while (m) {
  6951. if (SCTP_BUF_NEXT(m) == NULL) {
  6952. SCTP_BUF_NEXT(m) = appendchain;
  6953. break;
  6954. }
  6955. m = SCTP_BUF_NEXT(m);
  6956. }
  6957. }
  6958. /*
  6959. * save off the end and update the end-chain
  6960. * position
  6961. */
  6962. m = appendchain;
  6963. while (m) {
  6964. if (SCTP_BUF_NEXT(m) == NULL) {
  6965. *endofchain = m;
  6966. break;
  6967. }
  6968. m = SCTP_BUF_NEXT(m);
  6969. }
  6970. return (outchain);
  6971. } else {
  6972. /* save off the end and update the end-chain position */
  6973. m = appendchain;
  6974. while (m) {
  6975. if (SCTP_BUF_NEXT(m) == NULL) {
  6976. *endofchain = m;
  6977. break;
  6978. }
  6979. m = SCTP_BUF_NEXT(m);
  6980. }
  6981. return (appendchain);
  6982. }
  6983. }
  6984. static int
  6985. sctp_med_chunk_output(struct sctp_inpcb *inp,
  6986. struct sctp_tcb *stcb,
  6987. struct sctp_association *asoc,
  6988. int *num_out,
  6989. int *reason_code,
  6990. int control_only, int from_where,
  6991. struct timeval *now, int *now_filled,
  6992. uint32_t frag_point, int so_locked);
  6993. static void
  6994. sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
  6995. uint32_t val SCTP_UNUSED)
  6996. {
  6997. struct sctp_copy_all *ca;
  6998. struct mbuf *m;
  6999. int ret = 0;
  7000. int added_control = 0;
  7001. int un_sent, do_chunk_output = 1;
  7002. struct sctp_association *asoc;
  7003. struct sctp_nets *net;
  7004. ca = (struct sctp_copy_all *)ptr;
  7005. if (ca->m == NULL) {
  7006. return;
  7007. }
  7008. if (ca->inp != inp) {
  7009. /* TSNH */
  7010. return;
  7011. }
  7012. if (ca->sndlen > 0) {
  7013. m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
  7014. if (m == NULL) {
  7015. /* can't copy so we are done */
  7016. ca->cnt_failed++;
  7017. return;
  7018. }
  7019. #ifdef SCTP_MBUF_LOGGING
  7020. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  7021. sctp_log_mbc(m, SCTP_MBUF_ICOPY);
  7022. }
  7023. #endif
  7024. } else {
  7025. m = NULL;
  7026. }
  7027. SCTP_TCB_LOCK_ASSERT(stcb);
  7028. if (stcb->asoc.alternate) {
  7029. net = stcb->asoc.alternate;
  7030. } else {
  7031. net = stcb->asoc.primary_destination;
  7032. }
  7033. if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
  7034. /* Abort this assoc with m as the user defined reason */
  7035. if (m != NULL) {
  7036. SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
  7037. } else {
  7038. m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
  7039. 0, M_NOWAIT, 1, MT_DATA);
  7040. SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
  7041. }
  7042. if (m != NULL) {
  7043. struct sctp_paramhdr *ph;
  7044. ph = mtod(m, struct sctp_paramhdr *);
  7045. ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
  7046. ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
  7047. }
  7048. /* We add one here to keep the assoc from
  7049. * dis-appearing on us.
  7050. */
  7051. atomic_add_int(&stcb->asoc.refcnt, 1);
  7052. sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
  7053. /* sctp_abort_an_association calls sctp_free_asoc()
  7054. * free association will NOT free it since we
  7055. * incremented the refcnt .. we do this to prevent
  7056. * it being freed and things getting tricky since
  7057. * we could end up (from free_asoc) calling inpcb_free
  7058. * which would get a recursive lock call to the
  7059. * iterator lock.. But as a consequence of that the
  7060. * stcb will return to us un-locked.. since free_asoc
  7061. * returns with either no TCB or the TCB unlocked, we
  7062. * must relock.. to unlock in the iterator timer :-0
  7063. */
  7064. SCTP_TCB_LOCK(stcb);
  7065. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  7066. goto no_chunk_output;
  7067. } else {
  7068. if (m != NULL) {
  7069. ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
  7070. }
  7071. asoc = &stcb->asoc;
  7072. if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
  7073. /* shutdown this assoc */
  7074. if (TAILQ_EMPTY(&asoc->send_queue) &&
  7075. TAILQ_EMPTY(&asoc->sent_queue) &&
  7076. sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
  7077. if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
  7078. goto abort_anyway;
  7079. }
  7080. /* there is nothing queued to send, so I'm done... */
  7081. if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
  7082. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
  7083. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  7084. /* only send SHUTDOWN the first time through */
  7085. if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
  7086. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  7087. }
  7088. SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
  7089. sctp_stop_timers_for_shutdown(stcb);
  7090. sctp_send_shutdown(stcb, net);
  7091. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
  7092. net);
  7093. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
  7094. NULL);
  7095. added_control = 1;
  7096. do_chunk_output = 0;
  7097. }
  7098. } else {
  7099. /*
  7100. * we still got (or just got) data to send, so set
  7101. * SHUTDOWN_PENDING
  7102. */
  7103. /*
  7104. * XXX sockets draft says that SCTP_EOF should be
  7105. * sent with no data. currently, we will allow user
  7106. * data to be sent first and move to
  7107. * SHUTDOWN-PENDING
  7108. */
  7109. if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
  7110. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
  7111. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  7112. if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
  7113. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
  7114. }
  7115. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
  7116. if (TAILQ_EMPTY(&asoc->send_queue) &&
  7117. TAILQ_EMPTY(&asoc->sent_queue) &&
  7118. (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
  7119. struct mbuf *op_err;
  7120. char msg[SCTP_DIAG_INFO_LEN];
  7121. abort_anyway:
  7122. SCTP_SNPRINTF(msg, sizeof(msg),
  7123. "%s:%d at %s", __FILE__, __LINE__, __func__);
  7124. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  7125. msg);
  7126. atomic_add_int(&stcb->asoc.refcnt, 1);
  7127. sctp_abort_an_association(stcb->sctp_ep, stcb,
  7128. op_err, false, SCTP_SO_NOT_LOCKED);
  7129. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  7130. goto no_chunk_output;
  7131. }
  7132. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
  7133. NULL);
  7134. }
  7135. }
  7136. }
  7137. }
  7138. un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
  7139. (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
  7140. if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
  7141. (stcb->asoc.total_flight > 0) &&
  7142. (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
  7143. do_chunk_output = 0;
  7144. }
  7145. if (do_chunk_output)
  7146. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
  7147. else if (added_control) {
  7148. struct timeval now;
  7149. int num_out, reason, now_filled = 0;
  7150. (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
  7151. &reason, 1, 1, &now, &now_filled,
  7152. sctp_get_frag_point(stcb),
  7153. SCTP_SO_NOT_LOCKED);
  7154. }
  7155. no_chunk_output:
  7156. if (ret) {
  7157. ca->cnt_failed++;
  7158. } else {
  7159. ca->cnt_sent++;
  7160. }
  7161. }
  7162. static void
  7163. sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
  7164. {
  7165. struct sctp_copy_all *ca;
  7166. ca = (struct sctp_copy_all *)ptr;
  7167. /*
  7168. * Do a notify here? Kacheong suggests that the notify be done at
  7169. * the send time.. so you would push up a notification if any send
  7170. * failed. Don't know if this is feasible since the only failures we
  7171. * have is "memory" related and if you cannot get an mbuf to send
  7172. * the data you surely can't get an mbuf to send up to notify the
  7173. * user you can't send the data :->
  7174. */
  7175. /* now free everything */
  7176. if (ca->inp) {
  7177. /* Lets clear the flag to allow others to run. */
  7178. SCTP_INP_WLOCK(ca->inp);
  7179. ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
  7180. SCTP_INP_WUNLOCK(ca->inp);
  7181. }
  7182. sctp_m_freem(ca->m);
  7183. SCTP_FREE(ca, SCTP_M_COPYAL);
  7184. }
  7185. static struct mbuf *
  7186. sctp_copy_out_all(struct uio *uio, ssize_t len)
  7187. {
  7188. struct mbuf *ret, *at;
  7189. ssize_t left, willcpy, cancpy, error;
  7190. ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
  7191. if (ret == NULL) {
  7192. /* TSNH */
  7193. return (NULL);
  7194. }
  7195. left = len;
  7196. SCTP_BUF_LEN(ret) = 0;
  7197. /* save space for the data chunk header */
  7198. cancpy = (int)M_TRAILINGSPACE(ret);
  7199. willcpy = min(cancpy, left);
  7200. at = ret;
  7201. while (left > 0) {
  7202. /* Align data to the end */
  7203. error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
  7204. if (error) {
  7205. err_out_now:
  7206. sctp_m_freem(at);
  7207. return (NULL);
  7208. }
  7209. SCTP_BUF_LEN(at) = (int)willcpy;
  7210. SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
  7211. left -= willcpy;
  7212. if (left > 0) {
  7213. SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
  7214. if (SCTP_BUF_NEXT(at) == NULL) {
  7215. goto err_out_now;
  7216. }
  7217. at = SCTP_BUF_NEXT(at);
  7218. SCTP_BUF_LEN(at) = 0;
  7219. cancpy = (int)M_TRAILINGSPACE(at);
  7220. willcpy = min(cancpy, left);
  7221. }
  7222. }
  7223. return (ret);
  7224. }
  7225. static int
  7226. sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
  7227. struct sctp_nonpad_sndrcvinfo *srcv)
  7228. {
  7229. int ret;
  7230. struct sctp_copy_all *ca;
  7231. #if defined(__APPLE__) && !defined(__Userspace__)
  7232. #if defined(APPLE_LEOPARD)
  7233. if (uio->uio_resid > SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
  7234. #else
  7235. if (uio_resid(uio) > SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
  7236. #endif
  7237. #else
  7238. if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
  7239. #endif
  7240. /* You must not be larger than the limit! */
  7241. return (EMSGSIZE);
  7242. }
  7243. SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
  7244. SCTP_M_COPYAL);
  7245. if (ca == NULL) {
  7246. sctp_m_freem(m);
  7247. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  7248. return (ENOMEM);
  7249. }
  7250. memset(ca, 0, sizeof(struct sctp_copy_all));
  7251. ca->inp = inp;
  7252. if (srcv != NULL) {
  7253. memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
  7254. }
  7255. /* Serialize. */
  7256. SCTP_INP_WLOCK(inp);
  7257. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
  7258. SCTP_INP_WUNLOCK(inp);
  7259. sctp_m_freem(m);
  7260. SCTP_FREE(ca, SCTP_M_COPYAL);
  7261. return (EBUSY);
  7262. }
  7263. inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
  7264. SCTP_INP_WUNLOCK(inp);
  7265. /*
  7266. * take off the sendall flag, it would be bad if we failed to do
  7267. * this :-0
  7268. */
  7269. ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
  7270. /* get length and mbuf chain */
  7271. if (uio) {
  7272. #if defined(__APPLE__) && !defined(__Userspace__)
  7273. #if defined(APPLE_LEOPARD)
  7274. ca->sndlen = uio->uio_resid;
  7275. #else
  7276. ca->sndlen = uio_resid(uio);
  7277. #endif
  7278. #else
  7279. ca->sndlen = uio->uio_resid;
  7280. #endif
  7281. #if defined(__APPLE__) && !defined(__Userspace__)
  7282. SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 0);
  7283. #endif
  7284. ca->m = sctp_copy_out_all(uio, ca->sndlen);
  7285. #if defined(__APPLE__) && !defined(__Userspace__)
  7286. SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 0);
  7287. #endif
  7288. if (ca->m == NULL) {
  7289. SCTP_FREE(ca, SCTP_M_COPYAL);
  7290. sctp_m_freem(m);
  7291. SCTP_INP_WLOCK(inp);
  7292. inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
  7293. SCTP_INP_WUNLOCK(inp);
  7294. SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  7295. return (ENOMEM);
  7296. }
  7297. } else {
  7298. /* Gather the length of the send */
  7299. struct mbuf *mat;
  7300. ca->sndlen = 0;
  7301. for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
  7302. ca->sndlen += SCTP_BUF_LEN(mat);
  7303. }
  7304. }
  7305. ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
  7306. SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
  7307. SCTP_ASOC_ANY_STATE,
  7308. (void *)ca, 0,
  7309. sctp_sendall_completes, inp, 1);
  7310. if (ret) {
  7311. SCTP_INP_WLOCK(inp);
  7312. inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
  7313. SCTP_INP_WUNLOCK(inp);
  7314. SCTP_FREE(ca, SCTP_M_COPYAL);
  7315. SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
  7316. return (EFAULT);
  7317. }
  7318. return (0);
  7319. }
  7320. void
  7321. sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
  7322. {
  7323. struct sctp_tmit_chunk *chk, *nchk;
  7324. TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
  7325. if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
  7326. TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
  7327. asoc->ctrl_queue_cnt--;
  7328. if (chk->data) {
  7329. sctp_m_freem(chk->data);
  7330. chk->data = NULL;
  7331. }
  7332. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  7333. }
  7334. }
  7335. }
  7336. void
  7337. sctp_toss_old_asconf(struct sctp_tcb *stcb)
  7338. {
  7339. struct sctp_association *asoc;
  7340. struct sctp_tmit_chunk *chk, *nchk;
  7341. struct sctp_asconf_chunk *acp;
  7342. asoc = &stcb->asoc;
  7343. TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
  7344. /* find SCTP_ASCONF chunk in queue */
  7345. if (chk->rec.chunk_id.id == SCTP_ASCONF) {
  7346. if (chk->data) {
  7347. acp = mtod(chk->data, struct sctp_asconf_chunk *);
  7348. if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
  7349. /* Not Acked yet */
  7350. break;
  7351. }
  7352. }
  7353. TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
  7354. asoc->ctrl_queue_cnt--;
  7355. if (chk->data) {
  7356. sctp_m_freem(chk->data);
  7357. chk->data = NULL;
  7358. }
  7359. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  7360. }
  7361. }
  7362. }
  7363. static void
  7364. sctp_clean_up_datalist(struct sctp_tcb *stcb,
  7365. struct sctp_association *asoc,
  7366. struct sctp_tmit_chunk **data_list,
  7367. int bundle_at,
  7368. struct sctp_nets *net)
  7369. {
  7370. int i;
  7371. struct sctp_tmit_chunk *tp1;
  7372. for (i = 0; i < bundle_at; i++) {
  7373. /* off of the send queue */
  7374. TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
  7375. asoc->send_queue_cnt--;
  7376. if (i > 0) {
  7377. /*
  7378. * Any chunk NOT 0 you zap the time chunk 0 gets
  7379. * zapped or set based on if a RTO measurement is
  7380. * needed.
  7381. */
  7382. data_list[i]->do_rtt = 0;
  7383. }
  7384. /* record time */
  7385. data_list[i]->sent_rcv_time = net->last_sent_time;
  7386. data_list[i]->rec.data.cwnd_at_send = net->cwnd;
  7387. data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
  7388. if (data_list[i]->whoTo == NULL) {
  7389. data_list[i]->whoTo = net;
  7390. atomic_add_int(&net->ref_count, 1);
  7391. }
  7392. /* on to the sent queue */
  7393. tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
  7394. if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
  7395. struct sctp_tmit_chunk *tpp;
  7396. /* need to move back */
  7397. back_up_more:
  7398. tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
  7399. if (tpp == NULL) {
  7400. TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
  7401. goto all_done;
  7402. }
  7403. tp1 = tpp;
  7404. if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
  7405. goto back_up_more;
  7406. }
  7407. TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
  7408. } else {
  7409. TAILQ_INSERT_TAIL(&asoc->sent_queue,
  7410. data_list[i],
  7411. sctp_next);
  7412. }
  7413. all_done:
  7414. /* This does not lower until the cum-ack passes it */
  7415. asoc->sent_queue_cnt++;
  7416. if ((asoc->peers_rwnd <= 0) &&
  7417. (asoc->total_flight == 0) &&
  7418. (bundle_at == 1)) {
  7419. /* Mark the chunk as being a window probe */
  7420. SCTP_STAT_INCR(sctps_windowprobed);
  7421. }
  7422. #ifdef SCTP_AUDITING_ENABLED
  7423. sctp_audit_log(0xC2, 3);
  7424. #endif
  7425. data_list[i]->sent = SCTP_DATAGRAM_SENT;
  7426. data_list[i]->snd_count = 1;
  7427. data_list[i]->rec.data.chunk_was_revoked = 0;
  7428. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
  7429. sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
  7430. data_list[i]->whoTo->flight_size,
  7431. data_list[i]->book_size,
  7432. (uint32_t)(uintptr_t)data_list[i]->whoTo,
  7433. data_list[i]->rec.data.tsn);
  7434. }
  7435. sctp_flight_size_increase(data_list[i]);
  7436. sctp_total_flight_increase(stcb, data_list[i]);
  7437. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
  7438. sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
  7439. asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
  7440. }
  7441. asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
  7442. (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
  7443. if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
  7444. /* SWS sender side engages */
  7445. asoc->peers_rwnd = 0;
  7446. }
  7447. }
  7448. if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
  7449. (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted)(stcb, net);
  7450. }
  7451. }
  7452. static void
  7453. sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
  7454. {
  7455. struct sctp_tmit_chunk *chk, *nchk;
  7456. TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
  7457. if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
  7458. (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
  7459. (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
  7460. (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
  7461. (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
  7462. (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
  7463. (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
  7464. (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
  7465. (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
  7466. (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
  7467. (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
  7468. (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
  7469. /* Stray chunks must be cleaned up */
  7470. clean_up_anyway:
  7471. TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
  7472. asoc->ctrl_queue_cnt--;
  7473. if (chk->data) {
  7474. sctp_m_freem(chk->data);
  7475. chk->data = NULL;
  7476. }
  7477. if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
  7478. asoc->fwd_tsn_cnt--;
  7479. }
  7480. sctp_free_a_chunk(stcb, chk, so_locked);
  7481. } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
  7482. /* special handling, we must look into the param */
  7483. if (chk != asoc->str_reset) {
  7484. goto clean_up_anyway;
  7485. }
  7486. }
  7487. }
  7488. }
  7489. static uint32_t
  7490. sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
  7491. uint32_t space_left, uint32_t frag_point, int eeor_on)
  7492. {
  7493. /* Make a decision on if I should split a
  7494. * msg into multiple parts. This is only asked of
  7495. * incomplete messages.
  7496. */
  7497. if (eeor_on) {
  7498. /* If we are doing EEOR we need to always send
  7499. * it if its the entire thing, since it might
  7500. * be all the guy is putting in the hopper.
  7501. */
  7502. if (space_left >= length) {
  7503. /*-
  7504. * If we have data outstanding,
  7505. * we get another chance when the sack
  7506. * arrives to transmit - wait for more data
  7507. */
  7508. if (stcb->asoc.total_flight == 0) {
  7509. /* If nothing is in flight, we zero
  7510. * the packet counter.
  7511. */
  7512. return (length);
  7513. }
  7514. return (0);
  7515. } else {
  7516. /* You can fill the rest */
  7517. return (space_left);
  7518. }
  7519. }
  7520. /*-
  7521. * For those strange folk that make the send buffer
  7522. * smaller than our fragmentation point, we can't
  7523. * get a full msg in so we have to allow splitting.
  7524. */
  7525. if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
  7526. return (length);
  7527. }
  7528. if ((length <= space_left) ||
  7529. ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
  7530. /* Sub-optimal residual don't split in non-eeor mode. */
  7531. return (0);
  7532. }
  7533. /* If we reach here length is larger
  7534. * than the space_left. Do we wish to split
  7535. * it for the sake of packet putting together?
  7536. */
  7537. if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
  7538. /* Its ok to split it */
  7539. return (min(space_left, frag_point));
  7540. }
  7541. /* Nope, can't split */
  7542. return (0);
  7543. }
  7544. static uint32_t
  7545. sctp_move_to_outqueue(struct sctp_tcb *stcb,
  7546. struct sctp_nets *net,
  7547. struct sctp_stream_out *strq,
  7548. uint32_t space_left,
  7549. uint32_t frag_point,
  7550. int *giveup,
  7551. int eeor_mode,
  7552. int *bail,
  7553. int so_locked)
  7554. {
  7555. /* Move from the stream to the send_queue keeping track of the total */
  7556. struct sctp_association *asoc;
  7557. struct sctp_stream_queue_pending *sp;
  7558. struct sctp_tmit_chunk *chk;
  7559. struct sctp_data_chunk *dchkh=NULL;
  7560. struct sctp_idata_chunk *ndchkh=NULL;
  7561. uint32_t to_move, length;
  7562. int leading;
  7563. uint8_t rcv_flags = 0;
  7564. uint8_t some_taken;
  7565. SCTP_TCB_LOCK_ASSERT(stcb);
  7566. asoc = &stcb->asoc;
  7567. one_more_time:
  7568. /*sa_ignore FREED_MEMORY*/
  7569. sp = TAILQ_FIRST(&strq->outqueue);
  7570. if (sp == NULL) {
  7571. sp = TAILQ_FIRST(&strq->outqueue);
  7572. if (sp) {
  7573. goto one_more_time;
  7574. }
  7575. if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
  7576. (stcb->asoc.idata_supported == 0) &&
  7577. (strq->last_msg_incomplete)) {
  7578. SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
  7579. strq->sid,
  7580. strq->last_msg_incomplete);
  7581. strq->last_msg_incomplete = 0;
  7582. }
  7583. to_move = 0;
  7584. goto out_of;
  7585. }
  7586. if ((sp->msg_is_complete) && (sp->length == 0)) {
  7587. if (sp->sender_all_done) {
  7588. /* We are doing deferred cleanup. Last
  7589. * time through when we took all the data
  7590. * the sender_all_done was not set.
  7591. */
  7592. if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
  7593. SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
  7594. SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
  7595. sp->sender_all_done,
  7596. sp->length,
  7597. sp->msg_is_complete,
  7598. sp->put_last_out);
  7599. }
  7600. atomic_subtract_int(&asoc->stream_queue_cnt, 1);
  7601. TAILQ_REMOVE(&strq->outqueue, sp, next);
  7602. stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
  7603. if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
  7604. (strq->chunks_on_queues == 0) &&
  7605. TAILQ_EMPTY(&strq->outqueue)) {
  7606. stcb->asoc.trigger_reset = 1;
  7607. }
  7608. if (sp->net) {
  7609. sctp_free_remote_addr(sp->net);
  7610. sp->net = NULL;
  7611. }
  7612. if (sp->data) {
  7613. sctp_m_freem(sp->data);
  7614. sp->data = NULL;
  7615. }
  7616. sctp_free_a_strmoq(stcb, sp, so_locked);
  7617. /* back to get the next msg */
  7618. goto one_more_time;
  7619. } else {
  7620. /* sender just finished this but
  7621. * still holds a reference
  7622. */
  7623. *giveup = 1;
  7624. to_move = 0;
  7625. goto out_of;
  7626. }
  7627. } else {
  7628. /* is there some to get */
  7629. if (sp->length == 0) {
  7630. /* no */
  7631. *giveup = 1;
  7632. to_move = 0;
  7633. goto out_of;
  7634. } else if (sp->discard_rest) {
  7635. /* Whack down the size */
  7636. atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
  7637. if ((stcb->sctp_socket != NULL) &&
  7638. ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
  7639. (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
  7640. atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
  7641. }
  7642. if (sp->data) {
  7643. sctp_m_freem(sp->data);
  7644. sp->data = NULL;
  7645. sp->tail_mbuf = NULL;
  7646. }
  7647. sp->length = 0;
  7648. sp->some_taken = 1;
  7649. *giveup = 1;
  7650. to_move = 0;
  7651. goto out_of;
  7652. }
  7653. }
  7654. some_taken = sp->some_taken;
  7655. length = sp->length;
  7656. if (sp->msg_is_complete) {
  7657. /* The message is complete */
  7658. to_move = min(length, frag_point);
  7659. if (to_move == length) {
  7660. /* All of it fits in the MTU */
  7661. if (sp->some_taken) {
  7662. rcv_flags |= SCTP_DATA_LAST_FRAG;
  7663. } else {
  7664. rcv_flags |= SCTP_DATA_NOT_FRAG;
  7665. }
  7666. sp->put_last_out = 1;
  7667. if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
  7668. rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
  7669. }
  7670. } else {
  7671. /* Not all of it fits, we fragment */
  7672. if (sp->some_taken == 0) {
  7673. rcv_flags |= SCTP_DATA_FIRST_FRAG;
  7674. }
  7675. sp->some_taken = 1;
  7676. }
  7677. } else {
  7678. to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
  7679. if (to_move > 0) {
  7680. if (to_move >= length) {
  7681. to_move = length;
  7682. }
  7683. if (sp->some_taken == 0) {
  7684. rcv_flags |= SCTP_DATA_FIRST_FRAG;
  7685. sp->some_taken = 1;
  7686. }
  7687. } else {
  7688. /* Nothing to take. */
  7689. *giveup = 1;
  7690. to_move = 0;
  7691. goto out_of;
  7692. }
  7693. }
  7694. /* If we reach here, we can copy out a chunk */
  7695. sctp_alloc_a_chunk(stcb, chk);
  7696. if (chk == NULL) {
  7697. /* No chunk memory */
  7698. *giveup = 1;
  7699. to_move = 0;
  7700. goto out_of;
  7701. }
  7702. /* Setup for unordered if needed by looking
  7703. * at the user sent info flags.
  7704. */
  7705. if (sp->sinfo_flags & SCTP_UNORDERED) {
  7706. rcv_flags |= SCTP_DATA_UNORDERED;
  7707. }
  7708. if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
  7709. (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
  7710. rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
  7711. }
  7712. /* clear out the chunk before setting up */
  7713. memset(chk, 0, sizeof(*chk));
  7714. chk->rec.data.rcv_flags = rcv_flags;
  7715. if (to_move >= length) {
  7716. /* we think we can steal the whole thing */
  7717. if (to_move < sp->length) {
  7718. /* bail, it changed */
  7719. goto dont_do_it;
  7720. }
  7721. chk->data = sp->data;
  7722. chk->last_mbuf = sp->tail_mbuf;
  7723. /* register the stealing */
  7724. sp->data = sp->tail_mbuf = NULL;
  7725. } else {
  7726. struct mbuf *m;
  7727. dont_do_it:
  7728. chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
  7729. chk->last_mbuf = NULL;
  7730. if (chk->data == NULL) {
  7731. sp->some_taken = some_taken;
  7732. sctp_free_a_chunk(stcb, chk, so_locked);
  7733. *bail = 1;
  7734. to_move = 0;
  7735. goto out_of;
  7736. }
  7737. #ifdef SCTP_MBUF_LOGGING
  7738. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  7739. sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
  7740. }
  7741. #endif
  7742. /* Pull off the data */
  7743. m_adj(sp->data, to_move);
  7744. /* Now lets work our way down and compact it */
  7745. m = sp->data;
  7746. while (m && (SCTP_BUF_LEN(m) == 0)) {
  7747. sp->data = SCTP_BUF_NEXT(m);
  7748. SCTP_BUF_NEXT(m) = NULL;
  7749. if (sp->tail_mbuf == m) {
  7750. /*-
  7751. * Freeing tail? TSNH since
  7752. * we supposedly were taking less
  7753. * than the sp->length.
  7754. */
  7755. #ifdef INVARIANTS
  7756. panic("Huh, freeing tail? - TSNH");
  7757. #else
  7758. SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
  7759. sp->tail_mbuf = sp->data = NULL;
  7760. sp->length = 0;
  7761. #endif
  7762. }
  7763. sctp_m_free(m);
  7764. m = sp->data;
  7765. }
  7766. }
  7767. if (SCTP_BUF_IS_EXTENDED(chk->data)) {
  7768. chk->copy_by_ref = 1;
  7769. } else {
  7770. chk->copy_by_ref = 0;
  7771. }
  7772. /* get last_mbuf and counts of mb usage
  7773. * This is ugly but hopefully its only one mbuf.
  7774. */
  7775. if (chk->last_mbuf == NULL) {
  7776. chk->last_mbuf = chk->data;
  7777. while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
  7778. chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
  7779. }
  7780. }
  7781. if (to_move > length) {
  7782. /*- This should not happen either
  7783. * since we always lower to_move to the size
  7784. * of sp->length if its larger.
  7785. */
  7786. #ifdef INVARIANTS
  7787. panic("Huh, how can to_move be larger?");
  7788. #else
  7789. SCTP_PRINTF("Huh, how can to_move be larger?\n");
  7790. sp->length = 0;
  7791. #endif
  7792. } else {
  7793. atomic_subtract_int(&sp->length, to_move);
  7794. }
  7795. leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
  7796. if (M_LEADINGSPACE(chk->data) < leading) {
  7797. /* Not enough room for a chunk header, get some */
  7798. struct mbuf *m;
  7799. m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
  7800. if (m == NULL) {
  7801. /*
  7802. * we're in trouble here. _PREPEND below will free
  7803. * all the data if there is no leading space, so we
  7804. * must put the data back and restore.
  7805. */
  7806. if (sp->data == NULL) {
  7807. /* unsteal the data */
  7808. sp->data = chk->data;
  7809. sp->tail_mbuf = chk->last_mbuf;
  7810. } else {
  7811. struct mbuf *m_tmp;
  7812. /* reassemble the data */
  7813. m_tmp = sp->data;
  7814. sp->data = chk->data;
  7815. SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
  7816. }
  7817. sp->some_taken = some_taken;
  7818. atomic_add_int(&sp->length, to_move);
  7819. chk->data = NULL;
  7820. *bail = 1;
  7821. sctp_free_a_chunk(stcb, chk, so_locked);
  7822. to_move = 0;
  7823. goto out_of;
  7824. } else {
  7825. SCTP_BUF_LEN(m) = 0;
  7826. SCTP_BUF_NEXT(m) = chk->data;
  7827. chk->data = m;
  7828. M_ALIGN(chk->data, 4);
  7829. }
  7830. }
  7831. SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
  7832. if (chk->data == NULL) {
  7833. /* HELP, TSNH since we assured it would not above? */
  7834. #ifdef INVARIANTS
  7835. panic("prepend fails HELP?");
  7836. #else
  7837. SCTP_PRINTF("prepend fails HELP?\n");
  7838. sctp_free_a_chunk(stcb, chk, so_locked);
  7839. #endif
  7840. *bail = 1;
  7841. to_move = 0;
  7842. goto out_of;
  7843. }
  7844. sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
  7845. chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
  7846. chk->book_size_scale = 0;
  7847. chk->sent = SCTP_DATAGRAM_UNSENT;
  7848. chk->flags = 0;
  7849. chk->asoc = &stcb->asoc;
  7850. chk->pad_inplace = 0;
  7851. chk->no_fr_allowed = 0;
  7852. if (stcb->asoc.idata_supported == 0) {
  7853. if (rcv_flags & SCTP_DATA_UNORDERED) {
  7854. /* Just use 0. The receiver ignores the values. */
  7855. chk->rec.data.mid = 0;
  7856. } else {
  7857. chk->rec.data.mid = strq->next_mid_ordered;
  7858. if (rcv_flags & SCTP_DATA_LAST_FRAG) {
  7859. strq->next_mid_ordered++;
  7860. }
  7861. }
  7862. } else {
  7863. if (rcv_flags & SCTP_DATA_UNORDERED) {
  7864. chk->rec.data.mid = strq->next_mid_unordered;
  7865. if (rcv_flags & SCTP_DATA_LAST_FRAG) {
  7866. strq->next_mid_unordered++;
  7867. }
  7868. } else {
  7869. chk->rec.data.mid = strq->next_mid_ordered;
  7870. if (rcv_flags & SCTP_DATA_LAST_FRAG) {
  7871. strq->next_mid_ordered++;
  7872. }
  7873. }
  7874. }
  7875. chk->rec.data.sid = sp->sid;
  7876. chk->rec.data.ppid = sp->ppid;
  7877. chk->rec.data.context = sp->context;
  7878. chk->rec.data.doing_fast_retransmit = 0;
  7879. chk->rec.data.timetodrop = sp->ts;
  7880. chk->flags = sp->act_flags;
  7881. if (sp->net) {
  7882. chk->whoTo = sp->net;
  7883. atomic_add_int(&chk->whoTo->ref_count, 1);
  7884. } else
  7885. chk->whoTo = NULL;
  7886. if (sp->holds_key_ref) {
  7887. chk->auth_keyid = sp->auth_keyid;
  7888. sctp_auth_key_acquire(stcb, chk->auth_keyid);
  7889. chk->holds_key_ref = 1;
  7890. }
  7891. stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
  7892. #if defined(__FreeBSD__) && !defined(__Userspace__)
  7893. chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
  7894. #else
  7895. chk->rec.data.tsn = asoc->sending_seq++;
  7896. #endif
  7897. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
  7898. sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
  7899. (uint32_t)(uintptr_t)stcb, sp->length,
  7900. (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
  7901. chk->rec.data.tsn);
  7902. }
  7903. if (stcb->asoc.idata_supported == 0) {
  7904. dchkh = mtod(chk->data, struct sctp_data_chunk *);
  7905. } else {
  7906. ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
  7907. }
  7908. /*
  7909. * Put the rest of the things in place now. Size was done
  7910. * earlier in previous loop prior to padding.
  7911. */
  7912. SCTP_TCB_LOCK_ASSERT(stcb);
  7913. #ifdef SCTP_ASOCLOG_OF_TSNS
  7914. if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
  7915. asoc->tsn_out_at = 0;
  7916. asoc->tsn_out_wrapped = 1;
  7917. }
  7918. asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
  7919. asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
  7920. asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
  7921. asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
  7922. asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
  7923. asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
  7924. asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
  7925. asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
  7926. asoc->tsn_out_at++;
  7927. #endif
  7928. if (stcb->asoc.idata_supported == 0) {
  7929. dchkh->ch.chunk_type = SCTP_DATA;
  7930. dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
  7931. dchkh->dp.tsn = htonl(chk->rec.data.tsn);
  7932. dchkh->dp.sid = htons(strq->sid);
  7933. dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
  7934. dchkh->dp.ppid = chk->rec.data.ppid;
  7935. dchkh->ch.chunk_length = htons(chk->send_size);
  7936. } else {
  7937. ndchkh->ch.chunk_type = SCTP_IDATA;
  7938. ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
  7939. ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
  7940. ndchkh->dp.sid = htons(strq->sid);
  7941. ndchkh->dp.reserved = htons(0);
  7942. ndchkh->dp.mid = htonl(chk->rec.data.mid);
  7943. if (sp->fsn == 0)
  7944. ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
  7945. else
  7946. ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
  7947. sp->fsn++;
  7948. ndchkh->ch.chunk_length = htons(chk->send_size);
  7949. }
  7950. /* Now advance the chk->send_size by the actual pad needed. */
  7951. if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
  7952. /* need a pad */
  7953. struct mbuf *lm;
  7954. int pads;
  7955. pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
  7956. lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
  7957. if (lm != NULL) {
  7958. chk->last_mbuf = lm;
  7959. chk->pad_inplace = 1;
  7960. }
  7961. chk->send_size += pads;
  7962. }
  7963. if (PR_SCTP_ENABLED(chk->flags)) {
  7964. asoc->pr_sctp_cnt++;
  7965. }
  7966. if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
  7967. /* All done pull and kill the message */
  7968. if (sp->put_last_out == 0) {
  7969. SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
  7970. SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
  7971. sp->sender_all_done,
  7972. sp->length,
  7973. sp->msg_is_complete,
  7974. sp->put_last_out);
  7975. }
  7976. atomic_subtract_int(&asoc->stream_queue_cnt, 1);
  7977. TAILQ_REMOVE(&strq->outqueue, sp, next);
  7978. stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
  7979. if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
  7980. (strq->chunks_on_queues == 0) &&
  7981. TAILQ_EMPTY(&strq->outqueue)) {
  7982. stcb->asoc.trigger_reset = 1;
  7983. }
  7984. if (sp->net) {
  7985. sctp_free_remote_addr(sp->net);
  7986. sp->net = NULL;
  7987. }
  7988. if (sp->data) {
  7989. sctp_m_freem(sp->data);
  7990. sp->data = NULL;
  7991. }
  7992. sctp_free_a_strmoq(stcb, sp, so_locked);
  7993. }
  7994. asoc->chunks_on_out_queue++;
  7995. strq->chunks_on_queues++;
  7996. TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
  7997. asoc->send_queue_cnt++;
  7998. out_of:
  7999. return (to_move);
  8000. }
  8001. static void
  8002. sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
  8003. uint32_t frag_point, int eeor_mode, int *quit_now,
  8004. int so_locked)
  8005. {
  8006. struct sctp_association *asoc;
  8007. struct sctp_stream_out *strq;
  8008. uint32_t space_left, moved, total_moved;
  8009. int bail, giveup;
  8010. SCTP_TCB_LOCK_ASSERT(stcb);
  8011. asoc = &stcb->asoc;
  8012. total_moved = 0;
  8013. switch (net->ro._l_addr.sa.sa_family) {
  8014. #ifdef INET
  8015. case AF_INET:
  8016. space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
  8017. break;
  8018. #endif
  8019. #ifdef INET6
  8020. case AF_INET6:
  8021. space_left = net->mtu - SCTP_MIN_OVERHEAD;
  8022. break;
  8023. #endif
  8024. #if defined(__Userspace__)
  8025. case AF_CONN:
  8026. space_left = net->mtu - sizeof(struct sctphdr);
  8027. break;
  8028. #endif
  8029. default:
  8030. /* TSNH */
  8031. space_left = net->mtu;
  8032. break;
  8033. }
  8034. /* Need an allowance for the data chunk header too */
  8035. space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
  8036. /* must make even word boundary */
  8037. space_left &= 0xfffffffc;
  8038. strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
  8039. giveup = 0;
  8040. bail = 0;
  8041. while ((space_left > 0) && (strq != NULL)) {
  8042. moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
  8043. frag_point, &giveup, eeor_mode,
  8044. &bail, so_locked);
  8045. if ((giveup != 0) || (bail != 0)) {
  8046. break;
  8047. }
  8048. strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
  8049. total_moved += moved;
  8050. if (space_left >= moved) {
  8051. space_left -= moved;
  8052. } else {
  8053. space_left = 0;
  8054. }
  8055. if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
  8056. space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
  8057. } else {
  8058. space_left = 0;
  8059. }
  8060. space_left &= 0xfffffffc;
  8061. }
  8062. if (bail != 0)
  8063. *quit_now = 1;
  8064. stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
  8065. if (total_moved == 0) {
  8066. if ((stcb->asoc.sctp_cmt_on_off == 0) &&
  8067. (net == stcb->asoc.primary_destination)) {
  8068. /* ran dry for primary network net */
  8069. SCTP_STAT_INCR(sctps_primary_randry);
  8070. } else if (stcb->asoc.sctp_cmt_on_off > 0) {
  8071. /* ran dry with CMT on */
  8072. SCTP_STAT_INCR(sctps_cmt_randry);
  8073. }
  8074. }
  8075. }
  8076. void
  8077. sctp_fix_ecn_echo(struct sctp_association *asoc)
  8078. {
  8079. struct sctp_tmit_chunk *chk;
  8080. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  8081. if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
  8082. chk->sent = SCTP_DATAGRAM_UNSENT;
  8083. }
  8084. }
  8085. }
  8086. void
  8087. sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
  8088. {
  8089. struct sctp_association *asoc;
  8090. struct sctp_tmit_chunk *chk;
  8091. struct sctp_stream_queue_pending *sp;
  8092. unsigned int i;
  8093. if (net == NULL) {
  8094. return;
  8095. }
  8096. asoc = &stcb->asoc;
  8097. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  8098. TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
  8099. if (sp->net == net) {
  8100. sctp_free_remote_addr(sp->net);
  8101. sp->net = NULL;
  8102. }
  8103. }
  8104. }
  8105. TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
  8106. if (chk->whoTo == net) {
  8107. sctp_free_remote_addr(chk->whoTo);
  8108. chk->whoTo = NULL;
  8109. }
  8110. }
  8111. }
  8112. int
  8113. sctp_med_chunk_output(struct sctp_inpcb *inp,
  8114. struct sctp_tcb *stcb,
  8115. struct sctp_association *asoc,
  8116. int *num_out,
  8117. int *reason_code,
  8118. int control_only, int from_where,
  8119. struct timeval *now, int *now_filled,
  8120. uint32_t frag_point, int so_locked)
  8121. {
  8122. /**
  8123. * Ok this is the generic chunk service queue. we must do the
  8124. * following:
  8125. * - Service the stream queue that is next, moving any
  8126. * message (note I must get a complete message i.e. FIRST/MIDDLE and
  8127. * LAST to the out queue in one pass) and assigning TSN's. This
  8128. * only applies though if the peer does not support NDATA. For NDATA
  8129. * chunks its ok to not send the entire message ;-)
  8130. * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
  8131. * formulate and send the low level chunks. Making sure to combine
  8132. * any control in the control chunk queue also.
  8133. */
  8134. struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
  8135. struct mbuf *outchain, *endoutchain;
  8136. struct sctp_tmit_chunk *chk, *nchk;
  8137. /* temp arrays for unlinking */
  8138. struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
  8139. int no_fragmentflg, error;
  8140. unsigned int max_rwnd_per_dest, max_send_per_dest;
  8141. int one_chunk, hbflag, skip_data_for_this_net;
  8142. int asconf, cookie, no_out_cnt;
  8143. int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
  8144. unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
  8145. int tsns_sent = 0;
  8146. uint32_t auth_offset;
  8147. struct sctp_auth_chunk *auth;
  8148. uint16_t auth_keyid;
  8149. int override_ok = 1;
  8150. int skip_fill_up = 0;
  8151. int data_auth_reqd = 0;
  8152. /* JRS 5/14/07 - Add flag for whether a heartbeat is sent to
  8153. the destination. */
  8154. int quit_now = 0;
  8155. #if defined(__APPLE__) && !defined(__Userspace__)
  8156. if (so_locked) {
  8157. sctp_lock_assert(SCTP_INP_SO(inp));
  8158. } else {
  8159. sctp_unlock_assert(SCTP_INP_SO(inp));
  8160. }
  8161. #endif
  8162. *num_out = 0;
  8163. *reason_code = 0;
  8164. auth_keyid = stcb->asoc.authinfo.active_keyid;
  8165. if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
  8166. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
  8167. (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
  8168. eeor_mode = 1;
  8169. } else {
  8170. eeor_mode = 0;
  8171. }
  8172. ctl_cnt = no_out_cnt = asconf = cookie = 0;
  8173. /*
  8174. * First lets prime the pump. For each destination, if there is room
  8175. * in the flight size, attempt to pull an MTU's worth out of the
  8176. * stream queues into the general send_queue
  8177. */
  8178. #ifdef SCTP_AUDITING_ENABLED
  8179. sctp_audit_log(0xC2, 2);
  8180. #endif
  8181. SCTP_TCB_LOCK_ASSERT(stcb);
  8182. hbflag = 0;
  8183. if (control_only)
  8184. no_data_chunks = 1;
  8185. else
  8186. no_data_chunks = 0;
  8187. /* Nothing to possible to send? */
  8188. if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
  8189. (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
  8190. TAILQ_EMPTY(&asoc->asconf_send_queue) &&
  8191. TAILQ_EMPTY(&asoc->send_queue) &&
  8192. sctp_is_there_unsent_data(stcb, so_locked) == 0) {
  8193. nothing_to_send:
  8194. *reason_code = 9;
  8195. return (0);
  8196. }
  8197. if (asoc->peers_rwnd == 0) {
  8198. /* No room in peers rwnd */
  8199. *reason_code = 1;
  8200. if (asoc->total_flight > 0) {
  8201. /* we are allowed one chunk in flight */
  8202. no_data_chunks = 1;
  8203. }
  8204. }
  8205. if (stcb->asoc.ecn_echo_cnt_onq) {
  8206. /* Record where a sack goes, if any */
  8207. if (no_data_chunks &&
  8208. (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
  8209. /* Nothing but ECNe to send - we don't do that */
  8210. goto nothing_to_send;
  8211. }
  8212. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  8213. if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
  8214. (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
  8215. sack_goes_to = chk->whoTo;
  8216. break;
  8217. }
  8218. }
  8219. }
  8220. max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
  8221. if (stcb->sctp_socket)
  8222. max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
  8223. else
  8224. max_send_per_dest = 0;
  8225. if (no_data_chunks == 0) {
  8226. /* How many non-directed chunks are there? */
  8227. TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
  8228. if (chk->whoTo == NULL) {
  8229. /* We already have non-directed
  8230. * chunks on the queue, no need
  8231. * to do a fill-up.
  8232. */
  8233. skip_fill_up = 1;
  8234. break;
  8235. }
  8236. }
  8237. }
  8238. if ((no_data_chunks == 0) &&
  8239. (skip_fill_up == 0) &&
  8240. (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
  8241. TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
  8242. /*
  8243. * This for loop we are in takes in
  8244. * each net, if its's got space in cwnd and
  8245. * has data sent to it (when CMT is off) then it
  8246. * calls sctp_fill_outqueue for the net. This gets
  8247. * data on the send queue for that network.
  8248. *
  8249. * In sctp_fill_outqueue TSN's are assigned and
  8250. * data is copied out of the stream buffers. Note
  8251. * mostly copy by reference (we hope).
  8252. */
  8253. net->window_probe = 0;
  8254. if ((net != stcb->asoc.alternate) &&
  8255. ((net->dest_state & SCTP_ADDR_PF) ||
  8256. ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
  8257. (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
  8258. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  8259. sctp_log_cwnd(stcb, net, 1,
  8260. SCTP_CWND_LOG_FILL_OUTQ_CALLED);
  8261. }
  8262. continue;
  8263. }
  8264. if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
  8265. (net->flight_size == 0)) {
  8266. (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net);
  8267. }
  8268. if (net->flight_size >= net->cwnd) {
  8269. /* skip this network, no room - can't fill */
  8270. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  8271. sctp_log_cwnd(stcb, net, 3,
  8272. SCTP_CWND_LOG_FILL_OUTQ_CALLED);
  8273. }
  8274. continue;
  8275. }
  8276. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  8277. sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
  8278. }
  8279. sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
  8280. if (quit_now) {
  8281. /* memory alloc failure */
  8282. no_data_chunks = 1;
  8283. break;
  8284. }
  8285. }
  8286. }
  8287. /* now service each destination and send out what we can for it */
  8288. /* Nothing to send? */
  8289. if (TAILQ_EMPTY(&asoc->control_send_queue) &&
  8290. TAILQ_EMPTY(&asoc->asconf_send_queue) &&
  8291. TAILQ_EMPTY(&asoc->send_queue)) {
  8292. *reason_code = 8;
  8293. return (0);
  8294. }
  8295. if (asoc->sctp_cmt_on_off > 0) {
  8296. /* get the last start point */
  8297. start_at = asoc->last_net_cmt_send_started;
  8298. if (start_at == NULL) {
  8299. /* null so to beginning */
  8300. start_at = TAILQ_FIRST(&asoc->nets);
  8301. } else {
  8302. start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
  8303. if (start_at == NULL) {
  8304. start_at = TAILQ_FIRST(&asoc->nets);
  8305. }
  8306. }
  8307. asoc->last_net_cmt_send_started = start_at;
  8308. } else {
  8309. start_at = TAILQ_FIRST(&asoc->nets);
  8310. }
  8311. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  8312. if (chk->whoTo == NULL) {
  8313. if (asoc->alternate) {
  8314. chk->whoTo = asoc->alternate;
  8315. } else {
  8316. chk->whoTo = asoc->primary_destination;
  8317. }
  8318. atomic_add_int(&chk->whoTo->ref_count, 1);
  8319. }
  8320. }
  8321. old_start_at = NULL;
  8322. again_one_more_time:
  8323. for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
  8324. /* how much can we send? */
  8325. /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
  8326. if (old_start_at && (old_start_at == net)) {
  8327. /* through list completely. */
  8328. break;
  8329. }
  8330. tsns_sent = 0xa;
  8331. if (TAILQ_EMPTY(&asoc->control_send_queue) &&
  8332. TAILQ_EMPTY(&asoc->asconf_send_queue) &&
  8333. (net->flight_size >= net->cwnd)) {
  8334. /* Nothing on control or asconf and flight is full, we can skip
  8335. * even in the CMT case.
  8336. */
  8337. continue;
  8338. }
  8339. bundle_at = 0;
  8340. endoutchain = outchain = NULL;
  8341. auth = NULL;
  8342. auth_offset = 0;
  8343. no_fragmentflg = 1;
  8344. one_chunk = 0;
  8345. if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
  8346. skip_data_for_this_net = 1;
  8347. } else {
  8348. skip_data_for_this_net = 0;
  8349. }
  8350. switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
  8351. #ifdef INET
  8352. case AF_INET:
  8353. mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
  8354. break;
  8355. #endif
  8356. #ifdef INET6
  8357. case AF_INET6:
  8358. mtu = net->mtu - SCTP_MIN_OVERHEAD;
  8359. break;
  8360. #endif
  8361. #if defined(__Userspace__)
  8362. case AF_CONN:
  8363. mtu = net->mtu - sizeof(struct sctphdr);
  8364. break;
  8365. #endif
  8366. default:
  8367. /* TSNH */
  8368. mtu = net->mtu;
  8369. break;
  8370. }
  8371. mx_mtu = mtu;
  8372. to_out = 0;
  8373. if (mtu > asoc->peers_rwnd) {
  8374. if (asoc->total_flight > 0) {
  8375. /* We have a packet in flight somewhere */
  8376. r_mtu = asoc->peers_rwnd;
  8377. } else {
  8378. /* We are always allowed to send one MTU out */
  8379. one_chunk = 1;
  8380. r_mtu = mtu;
  8381. }
  8382. } else {
  8383. r_mtu = mtu;
  8384. }
  8385. error = 0;
  8386. /************************/
  8387. /* ASCONF transmission */
  8388. /************************/
  8389. /* Now first lets go through the asconf queue */
  8390. TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
  8391. if (chk->rec.chunk_id.id != SCTP_ASCONF) {
  8392. continue;
  8393. }
  8394. if (chk->whoTo == NULL) {
  8395. if (asoc->alternate == NULL) {
  8396. if (asoc->primary_destination != net) {
  8397. break;
  8398. }
  8399. } else {
  8400. if (asoc->alternate != net) {
  8401. break;
  8402. }
  8403. }
  8404. } else {
  8405. if (chk->whoTo != net) {
  8406. break;
  8407. }
  8408. }
  8409. if (chk->data == NULL) {
  8410. break;
  8411. }
  8412. if (chk->sent != SCTP_DATAGRAM_UNSENT &&
  8413. chk->sent != SCTP_DATAGRAM_RESEND) {
  8414. break;
  8415. }
  8416. /*
  8417. * if no AUTH is yet included and this chunk
  8418. * requires it, make sure to account for it. We
  8419. * don't apply the size until the AUTH chunk is
  8420. * actually added below in case there is no room for
  8421. * this chunk. NOTE: we overload the use of "omtu"
  8422. * here
  8423. */
  8424. if ((auth == NULL) &&
  8425. sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
  8426. stcb->asoc.peer_auth_chunks)) {
  8427. omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  8428. } else
  8429. omtu = 0;
  8430. /* Here we do NOT factor the r_mtu */
  8431. if ((chk->send_size < (int)(mtu - omtu)) ||
  8432. (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
  8433. /*
  8434. * We probably should glom the mbuf chain
  8435. * from the chk->data for control but the
  8436. * problem is it becomes yet one more level
  8437. * of tracking to do if for some reason
  8438. * output fails. Then I have got to
  8439. * reconstruct the merged control chain.. el
  8440. * yucko.. for now we take the easy way and
  8441. * do the copy
  8442. */
  8443. /*
  8444. * Add an AUTH chunk, if chunk requires it
  8445. * save the offset into the chain for AUTH
  8446. */
  8447. if ((auth == NULL) &&
  8448. (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
  8449. stcb->asoc.peer_auth_chunks))) {
  8450. outchain = sctp_add_auth_chunk(outchain,
  8451. &endoutchain,
  8452. &auth,
  8453. &auth_offset,
  8454. stcb,
  8455. chk->rec.chunk_id.id);
  8456. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  8457. }
  8458. outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
  8459. (int)chk->rec.chunk_id.can_take_data,
  8460. chk->send_size, chk->copy_by_ref);
  8461. if (outchain == NULL) {
  8462. *reason_code = 8;
  8463. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  8464. return (ENOMEM);
  8465. }
  8466. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  8467. /* update our MTU size */
  8468. if (mtu > (chk->send_size + omtu))
  8469. mtu -= (chk->send_size + omtu);
  8470. else
  8471. mtu = 0;
  8472. to_out += (chk->send_size + omtu);
  8473. /* Do clear IP_DF ? */
  8474. if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
  8475. no_fragmentflg = 0;
  8476. }
  8477. if (chk->rec.chunk_id.can_take_data)
  8478. chk->data = NULL;
  8479. /*
  8480. * set hb flag since we can
  8481. * use these for RTO
  8482. */
  8483. hbflag = 1;
  8484. asconf = 1;
  8485. /*
  8486. * should sysctl this: don't
  8487. * bundle data with ASCONF
  8488. * since it requires AUTH
  8489. */
  8490. no_data_chunks = 1;
  8491. chk->sent = SCTP_DATAGRAM_SENT;
  8492. if (chk->whoTo == NULL) {
  8493. chk->whoTo = net;
  8494. atomic_add_int(&net->ref_count, 1);
  8495. }
  8496. chk->snd_count++;
  8497. if (mtu == 0) {
  8498. /*
  8499. * Ok we are out of room but we can
  8500. * output without effecting the
  8501. * flight size since this little guy
  8502. * is a control only packet.
  8503. */
  8504. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
  8505. /*
  8506. * do NOT clear the asconf
  8507. * flag as it is used to do
  8508. * appropriate source address
  8509. * selection.
  8510. */
  8511. if (*now_filled == 0) {
  8512. (void)SCTP_GETTIME_TIMEVAL(now);
  8513. *now_filled = 1;
  8514. }
  8515. net->last_sent_time = *now;
  8516. hbflag = 0;
  8517. if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
  8518. (struct sockaddr *)&net->ro._l_addr,
  8519. outchain, auth_offset, auth,
  8520. stcb->asoc.authinfo.active_keyid,
  8521. no_fragmentflg, 0, asconf,
  8522. inp->sctp_lport, stcb->rport,
  8523. htonl(stcb->asoc.peer_vtag),
  8524. net->port, NULL,
  8525. #if defined(__FreeBSD__) && !defined(__Userspace__)
  8526. 0, 0,
  8527. #endif
  8528. so_locked))) {
  8529. /* error, we could not output */
  8530. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  8531. if (from_where == 0) {
  8532. SCTP_STAT_INCR(sctps_lowlevelerrusr);
  8533. }
  8534. if (error == ENOBUFS) {
  8535. asoc->ifp_had_enobuf = 1;
  8536. SCTP_STAT_INCR(sctps_lowlevelerr);
  8537. }
  8538. /* error, could not output */
  8539. if (error == EHOSTUNREACH) {
  8540. /*
  8541. * Destination went
  8542. * unreachable
  8543. * during this send
  8544. */
  8545. sctp_move_chunks_from_net(stcb, net);
  8546. }
  8547. *reason_code = 7;
  8548. break;
  8549. } else {
  8550. asoc->ifp_had_enobuf = 0;
  8551. }
  8552. /*
  8553. * increase the number we sent, if a
  8554. * cookie is sent we don't tell them
  8555. * any was sent out.
  8556. */
  8557. outchain = endoutchain = NULL;
  8558. auth = NULL;
  8559. auth_offset = 0;
  8560. if (!no_out_cnt)
  8561. *num_out += ctl_cnt;
  8562. /* recalc a clean slate and setup */
  8563. switch (net->ro._l_addr.sa.sa_family) {
  8564. #ifdef INET
  8565. case AF_INET:
  8566. mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
  8567. break;
  8568. #endif
  8569. #ifdef INET6
  8570. case AF_INET6:
  8571. mtu = net->mtu - SCTP_MIN_OVERHEAD;
  8572. break;
  8573. #endif
  8574. #if defined(__Userspace__)
  8575. case AF_CONN:
  8576. mtu = net->mtu - sizeof(struct sctphdr);
  8577. break;
  8578. #endif
  8579. default:
  8580. /* TSNH */
  8581. mtu = net->mtu;
  8582. break;
  8583. }
  8584. to_out = 0;
  8585. no_fragmentflg = 1;
  8586. }
  8587. }
  8588. }
  8589. if (error != 0) {
  8590. /* try next net */
  8591. continue;
  8592. }
  8593. /************************/
  8594. /* Control transmission */
  8595. /************************/
  8596. /* Now first lets go through the control queue */
  8597. TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
  8598. if ((sack_goes_to) &&
  8599. (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
  8600. (chk->whoTo != sack_goes_to)) {
  8601. /*
  8602. * if we have a sack in queue, and we are looking at an
  8603. * ecn echo that is NOT queued to where the sack is going..
  8604. */
  8605. if (chk->whoTo == net) {
  8606. /* Don't transmit it to where its going (current net) */
  8607. continue;
  8608. } else if (sack_goes_to == net) {
  8609. /* But do transmit it to this address */
  8610. goto skip_net_check;
  8611. }
  8612. }
  8613. if (chk->whoTo == NULL) {
  8614. if (asoc->alternate == NULL) {
  8615. if (asoc->primary_destination != net) {
  8616. continue;
  8617. }
  8618. } else {
  8619. if (asoc->alternate != net) {
  8620. continue;
  8621. }
  8622. }
  8623. } else {
  8624. if (chk->whoTo != net) {
  8625. continue;
  8626. }
  8627. }
  8628. skip_net_check:
  8629. if (chk->data == NULL) {
  8630. continue;
  8631. }
  8632. if (chk->sent != SCTP_DATAGRAM_UNSENT) {
  8633. /*
  8634. * It must be unsent. Cookies and ASCONF's
  8635. * hang around but there timers will force
  8636. * when marked for resend.
  8637. */
  8638. continue;
  8639. }
  8640. /*
  8641. * if no AUTH is yet included and this chunk
  8642. * requires it, make sure to account for it. We
  8643. * don't apply the size until the AUTH chunk is
  8644. * actually added below in case there is no room for
  8645. * this chunk. NOTE: we overload the use of "omtu"
  8646. * here
  8647. */
  8648. if ((auth == NULL) &&
  8649. sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
  8650. stcb->asoc.peer_auth_chunks)) {
  8651. omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  8652. } else
  8653. omtu = 0;
  8654. /* Here we do NOT factor the r_mtu */
  8655. if ((chk->send_size <= (int)(mtu - omtu)) ||
  8656. (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
  8657. /*
  8658. * We probably should glom the mbuf chain
  8659. * from the chk->data for control but the
  8660. * problem is it becomes yet one more level
  8661. * of tracking to do if for some reason
  8662. * output fails. Then I have got to
  8663. * reconstruct the merged control chain.. el
  8664. * yucko.. for now we take the easy way and
  8665. * do the copy
  8666. */
  8667. /*
  8668. * Add an AUTH chunk, if chunk requires it
  8669. * save the offset into the chain for AUTH
  8670. */
  8671. if ((auth == NULL) &&
  8672. (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
  8673. stcb->asoc.peer_auth_chunks))) {
  8674. outchain = sctp_add_auth_chunk(outchain,
  8675. &endoutchain,
  8676. &auth,
  8677. &auth_offset,
  8678. stcb,
  8679. chk->rec.chunk_id.id);
  8680. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  8681. }
  8682. outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
  8683. (int)chk->rec.chunk_id.can_take_data,
  8684. chk->send_size, chk->copy_by_ref);
  8685. if (outchain == NULL) {
  8686. *reason_code = 8;
  8687. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  8688. return (ENOMEM);
  8689. }
  8690. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  8691. /* update our MTU size */
  8692. if (mtu > (chk->send_size + omtu))
  8693. mtu -= (chk->send_size + omtu);
  8694. else
  8695. mtu = 0;
  8696. to_out += (chk->send_size + omtu);
  8697. /* Do clear IP_DF ? */
  8698. if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
  8699. no_fragmentflg = 0;
  8700. }
  8701. if (chk->rec.chunk_id.can_take_data)
  8702. chk->data = NULL;
  8703. /* Mark things to be removed, if needed */
  8704. if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
  8705. (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
  8706. (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
  8707. (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
  8708. (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
  8709. (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
  8710. (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
  8711. (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
  8712. (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
  8713. (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
  8714. (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
  8715. if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
  8716. hbflag = 1;
  8717. }
  8718. /* remove these chunks at the end */
  8719. if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
  8720. (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
  8721. /* turn off the timer */
  8722. if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
  8723. sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
  8724. inp, stcb, NULL,
  8725. SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
  8726. }
  8727. }
  8728. ctl_cnt++;
  8729. } else {
  8730. /*
  8731. * Other chunks, since they have
  8732. * timers running (i.e. COOKIE)
  8733. * we just "trust" that it
  8734. * gets sent or retransmitted.
  8735. */
  8736. ctl_cnt++;
  8737. if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
  8738. cookie = 1;
  8739. no_out_cnt = 1;
  8740. } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
  8741. /*
  8742. * Increment ecne send count here
  8743. * this means we may be over-zealous in
  8744. * our counting if the send fails, but its
  8745. * the best place to do it (we used to do
  8746. * it in the queue of the chunk, but that did
  8747. * not tell how many times it was sent.
  8748. */
  8749. SCTP_STAT_INCR(sctps_sendecne);
  8750. }
  8751. chk->sent = SCTP_DATAGRAM_SENT;
  8752. if (chk->whoTo == NULL) {
  8753. chk->whoTo = net;
  8754. atomic_add_int(&net->ref_count, 1);
  8755. }
  8756. chk->snd_count++;
  8757. }
  8758. if (mtu == 0) {
  8759. /*
  8760. * Ok we are out of room but we can
  8761. * output without effecting the
  8762. * flight size since this little guy
  8763. * is a control only packet.
  8764. */
  8765. if (asconf) {
  8766. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
  8767. /*
  8768. * do NOT clear the asconf
  8769. * flag as it is used to do
  8770. * appropriate source address
  8771. * selection.
  8772. */
  8773. }
  8774. if (cookie) {
  8775. sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
  8776. cookie = 0;
  8777. }
  8778. /* Only HB or ASCONF advances time */
  8779. if (hbflag) {
  8780. if (*now_filled == 0) {
  8781. (void)SCTP_GETTIME_TIMEVAL(now);
  8782. *now_filled = 1;
  8783. }
  8784. net->last_sent_time = *now;
  8785. hbflag = 0;
  8786. }
  8787. if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
  8788. (struct sockaddr *)&net->ro._l_addr,
  8789. outchain,
  8790. auth_offset, auth,
  8791. stcb->asoc.authinfo.active_keyid,
  8792. no_fragmentflg, 0, asconf,
  8793. inp->sctp_lport, stcb->rport,
  8794. htonl(stcb->asoc.peer_vtag),
  8795. net->port, NULL,
  8796. #if defined(__FreeBSD__) && !defined(__Userspace__)
  8797. 0, 0,
  8798. #endif
  8799. so_locked))) {
  8800. /* error, we could not output */
  8801. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  8802. if (from_where == 0) {
  8803. SCTP_STAT_INCR(sctps_lowlevelerrusr);
  8804. }
  8805. if (error == ENOBUFS) {
  8806. asoc->ifp_had_enobuf = 1;
  8807. SCTP_STAT_INCR(sctps_lowlevelerr);
  8808. }
  8809. if (error == EHOSTUNREACH) {
  8810. /*
  8811. * Destination went
  8812. * unreachable
  8813. * during this send
  8814. */
  8815. sctp_move_chunks_from_net(stcb, net);
  8816. }
  8817. *reason_code = 7;
  8818. break;
  8819. } else {
  8820. asoc->ifp_had_enobuf = 0;
  8821. }
  8822. /*
  8823. * increase the number we sent, if a
  8824. * cookie is sent we don't tell them
  8825. * any was sent out.
  8826. */
  8827. outchain = endoutchain = NULL;
  8828. auth = NULL;
  8829. auth_offset = 0;
  8830. if (!no_out_cnt)
  8831. *num_out += ctl_cnt;
  8832. /* recalc a clean slate and setup */
  8833. switch (net->ro._l_addr.sa.sa_family) {
  8834. #ifdef INET
  8835. case AF_INET:
  8836. mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
  8837. break;
  8838. #endif
  8839. #ifdef INET6
  8840. case AF_INET6:
  8841. mtu = net->mtu - SCTP_MIN_OVERHEAD;
  8842. break;
  8843. #endif
  8844. #if defined(__Userspace__)
  8845. case AF_CONN:
  8846. mtu = net->mtu - sizeof(struct sctphdr);
  8847. break;
  8848. #endif
  8849. default:
  8850. /* TSNH */
  8851. mtu = net->mtu;
  8852. break;
  8853. }
  8854. to_out = 0;
  8855. no_fragmentflg = 1;
  8856. }
  8857. }
  8858. }
  8859. if (error != 0) {
  8860. /* try next net */
  8861. continue;
  8862. }
  8863. /* JRI: if dest is in PF state, do not send data to it */
  8864. if ((asoc->sctp_cmt_on_off > 0) &&
  8865. (net != stcb->asoc.alternate) &&
  8866. (net->dest_state & SCTP_ADDR_PF)) {
  8867. goto no_data_fill;
  8868. }
  8869. if (net->flight_size >= net->cwnd) {
  8870. goto no_data_fill;
  8871. }
  8872. if ((asoc->sctp_cmt_on_off > 0) &&
  8873. (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
  8874. (net->flight_size > max_rwnd_per_dest)) {
  8875. goto no_data_fill;
  8876. }
  8877. /*
  8878. * We need a specific accounting for the usage of the
  8879. * send buffer. We also need to check the number of messages
  8880. * per net. For now, this is better than nothing and it
  8881. * disabled by default...
  8882. */
  8883. if ((asoc->sctp_cmt_on_off > 0) &&
  8884. (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
  8885. (max_send_per_dest > 0) &&
  8886. (net->flight_size > max_send_per_dest)) {
  8887. goto no_data_fill;
  8888. }
  8889. /*********************/
  8890. /* Data transmission */
  8891. /*********************/
  8892. /*
  8893. * if AUTH for DATA is required and no AUTH has been added
  8894. * yet, account for this in the mtu now... if no data can be
  8895. * bundled, this adjustment won't matter anyways since the
  8896. * packet will be going out...
  8897. */
  8898. data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
  8899. stcb->asoc.peer_auth_chunks);
  8900. if (data_auth_reqd && (auth == NULL)) {
  8901. mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  8902. }
  8903. /* now lets add any data within the MTU constraints */
  8904. switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
  8905. #ifdef INET
  8906. case AF_INET:
  8907. if (net->mtu > SCTP_MIN_V4_OVERHEAD)
  8908. omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
  8909. else
  8910. omtu = 0;
  8911. break;
  8912. #endif
  8913. #ifdef INET6
  8914. case AF_INET6:
  8915. if (net->mtu > SCTP_MIN_OVERHEAD)
  8916. omtu = net->mtu - SCTP_MIN_OVERHEAD;
  8917. else
  8918. omtu = 0;
  8919. break;
  8920. #endif
  8921. #if defined(__Userspace__)
  8922. case AF_CONN:
  8923. if (net->mtu > sizeof(struct sctphdr)) {
  8924. omtu = net->mtu - sizeof(struct sctphdr);
  8925. } else {
  8926. omtu = 0;
  8927. }
  8928. break;
  8929. #endif
  8930. default:
  8931. /* TSNH */
  8932. omtu = 0;
  8933. break;
  8934. }
  8935. if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
  8936. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
  8937. (skip_data_for_this_net == 0)) ||
  8938. (cookie)) {
  8939. TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
  8940. if (no_data_chunks) {
  8941. /* let only control go out */
  8942. *reason_code = 1;
  8943. break;
  8944. }
  8945. if (net->flight_size >= net->cwnd) {
  8946. /* skip this net, no room for data */
  8947. *reason_code = 2;
  8948. break;
  8949. }
  8950. if ((chk->whoTo != NULL) &&
  8951. (chk->whoTo != net)) {
  8952. /* Don't send the chunk on this net */
  8953. continue;
  8954. }
  8955. if (asoc->sctp_cmt_on_off == 0) {
  8956. if ((asoc->alternate) &&
  8957. (asoc->alternate != net) &&
  8958. (chk->whoTo == NULL)) {
  8959. continue;
  8960. } else if ((net != asoc->primary_destination) &&
  8961. (asoc->alternate == NULL) &&
  8962. (chk->whoTo == NULL)) {
  8963. continue;
  8964. }
  8965. }
  8966. if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
  8967. /*-
  8968. * strange, we have a chunk that is
  8969. * to big for its destination and
  8970. * yet no fragment ok flag.
  8971. * Something went wrong when the
  8972. * PMTU changed...we did not mark
  8973. * this chunk for some reason?? I
  8974. * will fix it here by letting IP
  8975. * fragment it for now and printing
  8976. * a warning. This really should not
  8977. * happen ...
  8978. */
  8979. SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
  8980. chk->send_size, mtu);
  8981. chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
  8982. }
  8983. if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
  8984. (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
  8985. struct sctp_data_chunk *dchkh;
  8986. dchkh = mtod(chk->data, struct sctp_data_chunk *);
  8987. dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
  8988. }
  8989. if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
  8990. ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
  8991. /* ok we will add this one */
  8992. /*
  8993. * Add an AUTH chunk, if chunk
  8994. * requires it, save the offset into
  8995. * the chain for AUTH
  8996. */
  8997. if (data_auth_reqd) {
  8998. if (auth == NULL) {
  8999. outchain = sctp_add_auth_chunk(outchain,
  9000. &endoutchain,
  9001. &auth,
  9002. &auth_offset,
  9003. stcb,
  9004. SCTP_DATA);
  9005. auth_keyid = chk->auth_keyid;
  9006. override_ok = 0;
  9007. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  9008. } else if (override_ok) {
  9009. /* use this data's keyid */
  9010. auth_keyid = chk->auth_keyid;
  9011. override_ok = 0;
  9012. } else if (auth_keyid != chk->auth_keyid) {
  9013. /* different keyid, so done bundling */
  9014. break;
  9015. }
  9016. }
  9017. outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
  9018. chk->send_size, chk->copy_by_ref);
  9019. if (outchain == NULL) {
  9020. SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
  9021. if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
  9022. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
  9023. }
  9024. *reason_code = 3;
  9025. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  9026. return (ENOMEM);
  9027. }
  9028. /* update our MTU size */
  9029. /* Do clear IP_DF ? */
  9030. if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
  9031. no_fragmentflg = 0;
  9032. }
  9033. /* unsigned subtraction of mtu */
  9034. if (mtu > chk->send_size)
  9035. mtu -= chk->send_size;
  9036. else
  9037. mtu = 0;
  9038. /* unsigned subtraction of r_mtu */
  9039. if (r_mtu > chk->send_size)
  9040. r_mtu -= chk->send_size;
  9041. else
  9042. r_mtu = 0;
  9043. to_out += chk->send_size;
  9044. if ((to_out > mx_mtu) && no_fragmentflg) {
  9045. #ifdef INVARIANTS
  9046. panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
  9047. #else
  9048. SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
  9049. mx_mtu, to_out);
  9050. #endif
  9051. }
  9052. chk->window_probe = 0;
  9053. data_list[bundle_at++] = chk;
  9054. if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
  9055. break;
  9056. }
  9057. if (chk->sent == SCTP_DATAGRAM_UNSENT) {
  9058. if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
  9059. SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
  9060. } else {
  9061. SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
  9062. }
  9063. if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
  9064. ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
  9065. /* Count number of user msg's that were fragmented
  9066. * we do this by counting when we see a LAST fragment
  9067. * only.
  9068. */
  9069. SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
  9070. }
  9071. if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
  9072. if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
  9073. data_list[0]->window_probe = 1;
  9074. net->window_probe = 1;
  9075. }
  9076. break;
  9077. }
  9078. } else {
  9079. /*
  9080. * Must be sent in order of the
  9081. * TSN's (on a network)
  9082. */
  9083. break;
  9084. }
  9085. } /* for (chunk gather loop for this net) */
  9086. } /* if asoc.state OPEN */
  9087. no_data_fill:
  9088. /* Is there something to send for this destination? */
  9089. if (outchain) {
  9090. /* We may need to start a control timer or two */
  9091. if (asconf) {
  9092. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
  9093. stcb, net);
  9094. /*
  9095. * do NOT clear the asconf flag as it is used
  9096. * to do appropriate source address selection.
  9097. */
  9098. }
  9099. if (cookie) {
  9100. sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
  9101. cookie = 0;
  9102. }
  9103. /* must start a send timer if data is being sent */
  9104. if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
  9105. /*
  9106. * no timer running on this destination
  9107. * restart it.
  9108. */
  9109. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
  9110. }
  9111. if (bundle_at || hbflag) {
  9112. /* For data/asconf and hb set time */
  9113. if (*now_filled == 0) {
  9114. (void)SCTP_GETTIME_TIMEVAL(now);
  9115. *now_filled = 1;
  9116. }
  9117. net->last_sent_time = *now;
  9118. }
  9119. /* Now send it, if there is anything to send :> */
  9120. if ((error = sctp_lowlevel_chunk_output(inp,
  9121. stcb,
  9122. net,
  9123. (struct sockaddr *)&net->ro._l_addr,
  9124. outchain,
  9125. auth_offset,
  9126. auth,
  9127. auth_keyid,
  9128. no_fragmentflg,
  9129. bundle_at,
  9130. asconf,
  9131. inp->sctp_lport, stcb->rport,
  9132. htonl(stcb->asoc.peer_vtag),
  9133. net->port, NULL,
  9134. #if defined(__FreeBSD__) && !defined(__Userspace__)
  9135. 0, 0,
  9136. #endif
  9137. so_locked))) {
  9138. /* error, we could not output */
  9139. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  9140. if (from_where == 0) {
  9141. SCTP_STAT_INCR(sctps_lowlevelerrusr);
  9142. }
  9143. if (error == ENOBUFS) {
  9144. asoc->ifp_had_enobuf = 1;
  9145. SCTP_STAT_INCR(sctps_lowlevelerr);
  9146. }
  9147. if (error == EHOSTUNREACH) {
  9148. /*
  9149. * Destination went unreachable
  9150. * during this send
  9151. */
  9152. sctp_move_chunks_from_net(stcb, net);
  9153. }
  9154. *reason_code = 6;
  9155. /*-
  9156. * I add this line to be paranoid. As far as
  9157. * I can tell the continue, takes us back to
  9158. * the top of the for, but just to make sure
  9159. * I will reset these again here.
  9160. */
  9161. ctl_cnt = 0;
  9162. continue; /* This takes us back to the for() for the nets. */
  9163. } else {
  9164. asoc->ifp_had_enobuf = 0;
  9165. }
  9166. endoutchain = NULL;
  9167. auth = NULL;
  9168. auth_offset = 0;
  9169. if (!no_out_cnt) {
  9170. *num_out += (ctl_cnt + bundle_at);
  9171. }
  9172. if (bundle_at) {
  9173. /* setup for a RTO measurement */
  9174. tsns_sent = data_list[0]->rec.data.tsn;
  9175. /* fill time if not already filled */
  9176. if (*now_filled == 0) {
  9177. (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
  9178. *now_filled = 1;
  9179. *now = asoc->time_last_sent;
  9180. } else {
  9181. asoc->time_last_sent = *now;
  9182. }
  9183. if (net->rto_needed) {
  9184. data_list[0]->do_rtt = 1;
  9185. net->rto_needed = 0;
  9186. }
  9187. SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
  9188. sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
  9189. }
  9190. if (one_chunk) {
  9191. break;
  9192. }
  9193. }
  9194. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  9195. sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
  9196. }
  9197. }
  9198. if (old_start_at == NULL) {
  9199. old_start_at = start_at;
  9200. start_at = TAILQ_FIRST(&asoc->nets);
  9201. if (old_start_at)
  9202. goto again_one_more_time;
  9203. }
  9204. /*
  9205. * At the end there should be no NON timed chunks hanging on this
  9206. * queue.
  9207. */
  9208. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  9209. sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
  9210. }
  9211. if ((*num_out == 0) && (*reason_code == 0)) {
  9212. *reason_code = 4;
  9213. } else {
  9214. *reason_code = 5;
  9215. }
  9216. sctp_clean_up_ctl(stcb, asoc, so_locked);
  9217. return (0);
  9218. }
  9219. void
  9220. sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
  9221. {
  9222. /*-
  9223. * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
  9224. * the control chunk queue.
  9225. */
  9226. struct sctp_chunkhdr *hdr;
  9227. struct sctp_tmit_chunk *chk;
  9228. struct mbuf *mat, *last_mbuf;
  9229. uint32_t chunk_length;
  9230. uint16_t padding_length;
  9231. SCTP_TCB_LOCK_ASSERT(stcb);
  9232. SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
  9233. if (op_err == NULL) {
  9234. return;
  9235. }
  9236. last_mbuf = NULL;
  9237. chunk_length = 0;
  9238. for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
  9239. chunk_length += SCTP_BUF_LEN(mat);
  9240. if (SCTP_BUF_NEXT(mat) == NULL) {
  9241. last_mbuf = mat;
  9242. }
  9243. }
  9244. if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
  9245. sctp_m_freem(op_err);
  9246. return;
  9247. }
  9248. padding_length = chunk_length % 4;
  9249. if (padding_length != 0) {
  9250. padding_length = 4 - padding_length;
  9251. }
  9252. if (padding_length != 0) {
  9253. if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
  9254. sctp_m_freem(op_err);
  9255. return;
  9256. }
  9257. }
  9258. sctp_alloc_a_chunk(stcb, chk);
  9259. if (chk == NULL) {
  9260. /* no memory */
  9261. sctp_m_freem(op_err);
  9262. return;
  9263. }
  9264. chk->copy_by_ref = 0;
  9265. chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
  9266. chk->rec.chunk_id.can_take_data = 0;
  9267. chk->flags = 0;
  9268. chk->send_size = (uint16_t)chunk_length;
  9269. chk->sent = SCTP_DATAGRAM_UNSENT;
  9270. chk->snd_count = 0;
  9271. chk->asoc = &stcb->asoc;
  9272. chk->data = op_err;
  9273. chk->whoTo = NULL;
  9274. hdr = mtod(op_err, struct sctp_chunkhdr *);
  9275. hdr->chunk_type = SCTP_OPERATION_ERROR;
  9276. hdr->chunk_flags = 0;
  9277. hdr->chunk_length = htons(chk->send_size);
  9278. TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
  9279. chk->asoc->ctrl_queue_cnt++;
  9280. }
  9281. int
  9282. sctp_send_cookie_echo(struct mbuf *m,
  9283. int offset, int limit,
  9284. struct sctp_tcb *stcb,
  9285. struct sctp_nets *net)
  9286. {
  9287. /*-
  9288. * pull out the cookie and put it at the front of the control chunk
  9289. * queue.
  9290. */
  9291. int at;
  9292. struct mbuf *cookie;
  9293. struct sctp_paramhdr param, *phdr;
  9294. struct sctp_chunkhdr *hdr;
  9295. struct sctp_tmit_chunk *chk;
  9296. uint16_t ptype, plen;
  9297. SCTP_TCB_LOCK_ASSERT(stcb);
  9298. /* First find the cookie in the param area */
  9299. cookie = NULL;
  9300. at = offset + sizeof(struct sctp_init_chunk);
  9301. for (;;) {
  9302. phdr = sctp_get_next_param(m, at, &param, sizeof(param));
  9303. if (phdr == NULL) {
  9304. return (-3);
  9305. }
  9306. ptype = ntohs(phdr->param_type);
  9307. plen = ntohs(phdr->param_length);
  9308. if (plen < sizeof(struct sctp_paramhdr)) {
  9309. return (-6);
  9310. }
  9311. if (ptype == SCTP_STATE_COOKIE) {
  9312. int pad;
  9313. /* found the cookie */
  9314. if (at + plen > limit) {
  9315. return (-7);
  9316. }
  9317. cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
  9318. if (cookie == NULL) {
  9319. /* No memory */
  9320. return (-2);
  9321. }
  9322. if ((pad = (plen % 4)) > 0) {
  9323. pad = 4 - pad;
  9324. }
  9325. if (pad > 0) {
  9326. if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
  9327. return (-8);
  9328. }
  9329. }
  9330. #ifdef SCTP_MBUF_LOGGING
  9331. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  9332. sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
  9333. }
  9334. #endif
  9335. break;
  9336. }
  9337. at += SCTP_SIZE32(plen);
  9338. }
  9339. /* ok, we got the cookie lets change it into a cookie echo chunk */
  9340. /* first the change from param to cookie */
  9341. hdr = mtod(cookie, struct sctp_chunkhdr *);
  9342. hdr->chunk_type = SCTP_COOKIE_ECHO;
  9343. hdr->chunk_flags = 0;
  9344. /* get the chunk stuff now and place it in the FRONT of the queue */
  9345. sctp_alloc_a_chunk(stcb, chk);
  9346. if (chk == NULL) {
  9347. /* no memory */
  9348. sctp_m_freem(cookie);
  9349. return (-5);
  9350. }
  9351. chk->copy_by_ref = 0;
  9352. chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
  9353. chk->rec.chunk_id.can_take_data = 0;
  9354. chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
  9355. chk->send_size = SCTP_SIZE32(plen);
  9356. chk->sent = SCTP_DATAGRAM_UNSENT;
  9357. chk->snd_count = 0;
  9358. chk->asoc = &stcb->asoc;
  9359. chk->data = cookie;
  9360. chk->whoTo = net;
  9361. atomic_add_int(&chk->whoTo->ref_count, 1);
  9362. TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
  9363. chk->asoc->ctrl_queue_cnt++;
  9364. return (0);
  9365. }
  9366. void
  9367. sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
  9368. struct mbuf *m,
  9369. int offset,
  9370. int chk_length,
  9371. struct sctp_nets *net)
  9372. {
  9373. /*
  9374. * take a HB request and make it into a HB ack and send it.
  9375. */
  9376. struct mbuf *outchain;
  9377. struct sctp_chunkhdr *chdr;
  9378. struct sctp_tmit_chunk *chk;
  9379. if (net == NULL)
  9380. /* must have a net pointer */
  9381. return;
  9382. outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
  9383. if (outchain == NULL) {
  9384. /* gak out of memory */
  9385. return;
  9386. }
  9387. #ifdef SCTP_MBUF_LOGGING
  9388. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  9389. sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
  9390. }
  9391. #endif
  9392. chdr = mtod(outchain, struct sctp_chunkhdr *);
  9393. chdr->chunk_type = SCTP_HEARTBEAT_ACK;
  9394. chdr->chunk_flags = 0;
  9395. if (chk_length % 4 != 0) {
  9396. sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
  9397. }
  9398. sctp_alloc_a_chunk(stcb, chk);
  9399. if (chk == NULL) {
  9400. /* no memory */
  9401. sctp_m_freem(outchain);
  9402. return;
  9403. }
  9404. chk->copy_by_ref = 0;
  9405. chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
  9406. chk->rec.chunk_id.can_take_data = 1;
  9407. chk->flags = 0;
  9408. chk->send_size = chk_length;
  9409. chk->sent = SCTP_DATAGRAM_UNSENT;
  9410. chk->snd_count = 0;
  9411. chk->asoc = &stcb->asoc;
  9412. chk->data = outchain;
  9413. chk->whoTo = net;
  9414. atomic_add_int(&chk->whoTo->ref_count, 1);
  9415. TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
  9416. chk->asoc->ctrl_queue_cnt++;
  9417. }
  9418. void
  9419. sctp_send_cookie_ack(struct sctp_tcb *stcb)
  9420. {
  9421. /* formulate and queue a cookie-ack back to sender */
  9422. struct mbuf *cookie_ack;
  9423. struct sctp_chunkhdr *hdr;
  9424. struct sctp_tmit_chunk *chk;
  9425. SCTP_TCB_LOCK_ASSERT(stcb);
  9426. cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
  9427. if (cookie_ack == NULL) {
  9428. /* no mbuf's */
  9429. return;
  9430. }
  9431. SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
  9432. sctp_alloc_a_chunk(stcb, chk);
  9433. if (chk == NULL) {
  9434. /* no memory */
  9435. sctp_m_freem(cookie_ack);
  9436. return;
  9437. }
  9438. chk->copy_by_ref = 0;
  9439. chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
  9440. chk->rec.chunk_id.can_take_data = 1;
  9441. chk->flags = 0;
  9442. chk->send_size = sizeof(struct sctp_chunkhdr);
  9443. chk->sent = SCTP_DATAGRAM_UNSENT;
  9444. chk->snd_count = 0;
  9445. chk->asoc = &stcb->asoc;
  9446. chk->data = cookie_ack;
  9447. if (chk->asoc->last_control_chunk_from != NULL) {
  9448. chk->whoTo = chk->asoc->last_control_chunk_from;
  9449. atomic_add_int(&chk->whoTo->ref_count, 1);
  9450. } else {
  9451. chk->whoTo = NULL;
  9452. }
  9453. hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
  9454. hdr->chunk_type = SCTP_COOKIE_ACK;
  9455. hdr->chunk_flags = 0;
  9456. hdr->chunk_length = htons(chk->send_size);
  9457. SCTP_BUF_LEN(cookie_ack) = chk->send_size;
  9458. TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
  9459. chk->asoc->ctrl_queue_cnt++;
  9460. return;
  9461. }
  9462. void
  9463. sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
  9464. {
  9465. /* formulate and queue a SHUTDOWN-ACK back to the sender */
  9466. struct mbuf *m_shutdown_ack;
  9467. struct sctp_shutdown_ack_chunk *ack_cp;
  9468. struct sctp_tmit_chunk *chk;
  9469. m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
  9470. if (m_shutdown_ack == NULL) {
  9471. /* no mbuf's */
  9472. return;
  9473. }
  9474. SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
  9475. sctp_alloc_a_chunk(stcb, chk);
  9476. if (chk == NULL) {
  9477. /* no memory */
  9478. sctp_m_freem(m_shutdown_ack);
  9479. return;
  9480. }
  9481. chk->copy_by_ref = 0;
  9482. chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
  9483. chk->rec.chunk_id.can_take_data = 1;
  9484. chk->flags = 0;
  9485. chk->send_size = sizeof(struct sctp_chunkhdr);
  9486. chk->sent = SCTP_DATAGRAM_UNSENT;
  9487. chk->snd_count = 0;
  9488. chk->asoc = &stcb->asoc;
  9489. chk->data = m_shutdown_ack;
  9490. chk->whoTo = net;
  9491. if (chk->whoTo) {
  9492. atomic_add_int(&chk->whoTo->ref_count, 1);
  9493. }
  9494. ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
  9495. ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
  9496. ack_cp->ch.chunk_flags = 0;
  9497. ack_cp->ch.chunk_length = htons(chk->send_size);
  9498. SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
  9499. TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
  9500. chk->asoc->ctrl_queue_cnt++;
  9501. return;
  9502. }
  9503. void
  9504. sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
  9505. {
  9506. /* formulate and queue a SHUTDOWN to the sender */
  9507. struct mbuf *m_shutdown;
  9508. struct sctp_shutdown_chunk *shutdown_cp;
  9509. struct sctp_tmit_chunk *chk;
  9510. TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
  9511. if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
  9512. /* We already have a SHUTDOWN queued. Reuse it. */
  9513. if (chk->whoTo) {
  9514. sctp_free_remote_addr(chk->whoTo);
  9515. chk->whoTo = NULL;
  9516. }
  9517. break;
  9518. }
  9519. }
  9520. if (chk == NULL) {
  9521. m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
  9522. if (m_shutdown == NULL) {
  9523. /* no mbuf's */
  9524. return;
  9525. }
  9526. SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
  9527. sctp_alloc_a_chunk(stcb, chk);
  9528. if (chk == NULL) {
  9529. /* no memory */
  9530. sctp_m_freem(m_shutdown);
  9531. return;
  9532. }
  9533. chk->copy_by_ref = 0;
  9534. chk->rec.chunk_id.id = SCTP_SHUTDOWN;
  9535. chk->rec.chunk_id.can_take_data = 1;
  9536. chk->flags = 0;
  9537. chk->send_size = sizeof(struct sctp_shutdown_chunk);
  9538. chk->sent = SCTP_DATAGRAM_UNSENT;
  9539. chk->snd_count = 0;
  9540. chk->asoc = &stcb->asoc;
  9541. chk->data = m_shutdown;
  9542. chk->whoTo = net;
  9543. if (chk->whoTo) {
  9544. atomic_add_int(&chk->whoTo->ref_count, 1);
  9545. }
  9546. shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
  9547. shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
  9548. shutdown_cp->ch.chunk_flags = 0;
  9549. shutdown_cp->ch.chunk_length = htons(chk->send_size);
  9550. shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
  9551. SCTP_BUF_LEN(m_shutdown) = chk->send_size;
  9552. TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
  9553. chk->asoc->ctrl_queue_cnt++;
  9554. } else {
  9555. TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
  9556. chk->whoTo = net;
  9557. if (chk->whoTo) {
  9558. atomic_add_int(&chk->whoTo->ref_count, 1);
  9559. }
  9560. shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
  9561. shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
  9562. TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
  9563. }
  9564. return;
  9565. }
  9566. void
  9567. sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
  9568. {
  9569. /*
  9570. * formulate and queue an ASCONF to the peer.
  9571. * ASCONF parameters should be queued on the assoc queue.
  9572. */
  9573. struct sctp_tmit_chunk *chk;
  9574. struct mbuf *m_asconf;
  9575. int len;
  9576. SCTP_TCB_LOCK_ASSERT(stcb);
  9577. if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
  9578. (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
  9579. /* can't send a new one if there is one in flight already */
  9580. return;
  9581. }
  9582. /* compose an ASCONF chunk, maximum length is PMTU */
  9583. m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
  9584. if (m_asconf == NULL) {
  9585. return;
  9586. }
  9587. sctp_alloc_a_chunk(stcb, chk);
  9588. if (chk == NULL) {
  9589. /* no memory */
  9590. sctp_m_freem(m_asconf);
  9591. return;
  9592. }
  9593. chk->copy_by_ref = 0;
  9594. chk->rec.chunk_id.id = SCTP_ASCONF;
  9595. chk->rec.chunk_id.can_take_data = 0;
  9596. chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
  9597. chk->data = m_asconf;
  9598. chk->send_size = len;
  9599. chk->sent = SCTP_DATAGRAM_UNSENT;
  9600. chk->snd_count = 0;
  9601. chk->asoc = &stcb->asoc;
  9602. chk->whoTo = net;
  9603. if (chk->whoTo) {
  9604. atomic_add_int(&chk->whoTo->ref_count, 1);
  9605. }
  9606. TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
  9607. chk->asoc->ctrl_queue_cnt++;
  9608. return;
  9609. }
  9610. void
  9611. sctp_send_asconf_ack(struct sctp_tcb *stcb)
  9612. {
  9613. /*
  9614. * formulate and queue a asconf-ack back to sender.
  9615. * the asconf-ack must be stored in the tcb.
  9616. */
  9617. struct sctp_tmit_chunk *chk;
  9618. struct sctp_asconf_ack *ack, *latest_ack;
  9619. struct mbuf *m_ack;
  9620. struct sctp_nets *net = NULL;
  9621. SCTP_TCB_LOCK_ASSERT(stcb);
  9622. /* Get the latest ASCONF-ACK */
  9623. latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
  9624. if (latest_ack == NULL) {
  9625. return;
  9626. }
  9627. if (latest_ack->last_sent_to != NULL &&
  9628. latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
  9629. /* we're doing a retransmission */
  9630. net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
  9631. if (net == NULL) {
  9632. /* no alternate */
  9633. if (stcb->asoc.last_control_chunk_from == NULL) {
  9634. if (stcb->asoc.alternate) {
  9635. net = stcb->asoc.alternate;
  9636. } else {
  9637. net = stcb->asoc.primary_destination;
  9638. }
  9639. } else {
  9640. net = stcb->asoc.last_control_chunk_from;
  9641. }
  9642. }
  9643. } else {
  9644. /* normal case */
  9645. if (stcb->asoc.last_control_chunk_from == NULL) {
  9646. if (stcb->asoc.alternate) {
  9647. net = stcb->asoc.alternate;
  9648. } else {
  9649. net = stcb->asoc.primary_destination;
  9650. }
  9651. } else {
  9652. net = stcb->asoc.last_control_chunk_from;
  9653. }
  9654. }
  9655. latest_ack->last_sent_to = net;
  9656. TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
  9657. if (ack->data == NULL) {
  9658. continue;
  9659. }
  9660. /* copy the asconf_ack */
  9661. m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
  9662. if (m_ack == NULL) {
  9663. /* couldn't copy it */
  9664. return;
  9665. }
  9666. #ifdef SCTP_MBUF_LOGGING
  9667. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
  9668. sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
  9669. }
  9670. #endif
  9671. sctp_alloc_a_chunk(stcb, chk);
  9672. if (chk == NULL) {
  9673. /* no memory */
  9674. if (m_ack)
  9675. sctp_m_freem(m_ack);
  9676. return;
  9677. }
  9678. chk->copy_by_ref = 0;
  9679. chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
  9680. chk->rec.chunk_id.can_take_data = 1;
  9681. chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
  9682. chk->whoTo = net;
  9683. if (chk->whoTo) {
  9684. atomic_add_int(&chk->whoTo->ref_count, 1);
  9685. }
  9686. chk->data = m_ack;
  9687. chk->send_size = ack->len;
  9688. chk->sent = SCTP_DATAGRAM_UNSENT;
  9689. chk->snd_count = 0;
  9690. chk->asoc = &stcb->asoc;
  9691. TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
  9692. chk->asoc->ctrl_queue_cnt++;
  9693. }
  9694. return;
  9695. }
  9696. static int
  9697. sctp_chunk_retransmission(struct sctp_inpcb *inp,
  9698. struct sctp_tcb *stcb,
  9699. struct sctp_association *asoc,
  9700. int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
  9701. {
  9702. /*-
  9703. * send out one MTU of retransmission. If fast_retransmit is
  9704. * happening we ignore the cwnd. Otherwise we obey the cwnd and
  9705. * rwnd. For a Cookie or Asconf in the control chunk queue we
  9706. * retransmit them by themselves.
  9707. *
  9708. * For data chunks we will pick out the lowest TSN's in the sent_queue
  9709. * marked for resend and bundle them all together (up to a MTU of
  9710. * destination). The address to send to should have been
  9711. * selected/changed where the retransmission was marked (i.e. in FR
  9712. * or t3-timeout routines).
  9713. */
  9714. struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
  9715. struct sctp_tmit_chunk *chk, *fwd;
  9716. struct mbuf *m, *endofchain;
  9717. struct sctp_nets *net = NULL;
  9718. uint32_t tsns_sent = 0;
  9719. int no_fragmentflg, bundle_at;
  9720. unsigned int mtu;
  9721. int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
  9722. struct sctp_auth_chunk *auth = NULL;
  9723. uint32_t auth_offset = 0;
  9724. uint16_t auth_keyid;
  9725. int override_ok = 1;
  9726. int data_auth_reqd = 0;
  9727. uint32_t dmtu = 0;
  9728. #if defined(__APPLE__) && !defined(__Userspace__)
  9729. if (so_locked) {
  9730. sctp_lock_assert(SCTP_INP_SO(inp));
  9731. } else {
  9732. sctp_unlock_assert(SCTP_INP_SO(inp));
  9733. }
  9734. #endif
  9735. SCTP_TCB_LOCK_ASSERT(stcb);
  9736. tmr_started = ctl_cnt = 0;
  9737. no_fragmentflg = 1;
  9738. fwd_tsn = 0;
  9739. *cnt_out = 0;
  9740. fwd = NULL;
  9741. endofchain = m = NULL;
  9742. auth_keyid = stcb->asoc.authinfo.active_keyid;
  9743. #ifdef SCTP_AUDITING_ENABLED
  9744. sctp_audit_log(0xC3, 1);
  9745. #endif
  9746. if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
  9747. (TAILQ_EMPTY(&asoc->control_send_queue))) {
  9748. SCTPDBG(SCTP_DEBUG_OUTPUT1,"SCTP hits empty queue with cnt set to %d?\n",
  9749. asoc->sent_queue_retran_cnt);
  9750. asoc->sent_queue_cnt = 0;
  9751. asoc->sent_queue_cnt_removeable = 0;
  9752. /* send back 0/0 so we enter normal transmission */
  9753. *cnt_out = 0;
  9754. return (0);
  9755. }
  9756. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  9757. if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
  9758. (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
  9759. (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
  9760. if (chk->sent != SCTP_DATAGRAM_RESEND) {
  9761. continue;
  9762. }
  9763. if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
  9764. if (chk != asoc->str_reset) {
  9765. /*
  9766. * not eligible for retran if its
  9767. * not ours
  9768. */
  9769. continue;
  9770. }
  9771. }
  9772. ctl_cnt++;
  9773. if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
  9774. fwd_tsn = 1;
  9775. }
  9776. /*
  9777. * Add an AUTH chunk, if chunk requires it save the
  9778. * offset into the chain for AUTH
  9779. */
  9780. if ((auth == NULL) &&
  9781. (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
  9782. stcb->asoc.peer_auth_chunks))) {
  9783. m = sctp_add_auth_chunk(m, &endofchain,
  9784. &auth, &auth_offset,
  9785. stcb,
  9786. chk->rec.chunk_id.id);
  9787. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  9788. }
  9789. m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
  9790. break;
  9791. }
  9792. }
  9793. one_chunk = 0;
  9794. /* do we have control chunks to retransmit? */
  9795. if (m != NULL) {
  9796. /* Start a timer no matter if we succeed or fail */
  9797. if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
  9798. sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
  9799. } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
  9800. sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
  9801. chk->snd_count++; /* update our count */
  9802. if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
  9803. (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
  9804. auth_offset, auth, stcb->asoc.authinfo.active_keyid,
  9805. no_fragmentflg, 0, 0,
  9806. inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
  9807. chk->whoTo->port, NULL,
  9808. #if defined(__FreeBSD__) && !defined(__Userspace__)
  9809. 0, 0,
  9810. #endif
  9811. so_locked))) {
  9812. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  9813. if (error == ENOBUFS) {
  9814. asoc->ifp_had_enobuf = 1;
  9815. SCTP_STAT_INCR(sctps_lowlevelerr);
  9816. }
  9817. return (error);
  9818. } else {
  9819. asoc->ifp_had_enobuf = 0;
  9820. }
  9821. endofchain = NULL;
  9822. auth = NULL;
  9823. auth_offset = 0;
  9824. /*
  9825. * We don't want to mark the net->sent time here since this
  9826. * we use this for HB and retrans cannot measure RTT
  9827. */
  9828. /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
  9829. *cnt_out += 1;
  9830. chk->sent = SCTP_DATAGRAM_SENT;
  9831. sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
  9832. if (fwd_tsn == 0) {
  9833. return (0);
  9834. } else {
  9835. /* Clean up the fwd-tsn list */
  9836. sctp_clean_up_ctl(stcb, asoc, so_locked);
  9837. return (0);
  9838. }
  9839. }
  9840. /*
  9841. * Ok, it is just data retransmission we need to do or that and a
  9842. * fwd-tsn with it all.
  9843. */
  9844. if (TAILQ_EMPTY(&asoc->sent_queue)) {
  9845. return (SCTP_RETRAN_DONE);
  9846. }
  9847. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
  9848. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
  9849. /* not yet open, resend the cookie and that is it */
  9850. return (1);
  9851. }
  9852. #ifdef SCTP_AUDITING_ENABLED
  9853. sctp_auditing(20, inp, stcb, NULL);
  9854. #endif
  9855. data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
  9856. TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
  9857. if (chk->sent != SCTP_DATAGRAM_RESEND) {
  9858. /* No, not sent to this net or not ready for rtx */
  9859. continue;
  9860. }
  9861. if (chk->data == NULL) {
  9862. SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
  9863. chk->rec.data.tsn, chk->snd_count, chk->sent);
  9864. continue;
  9865. }
  9866. if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
  9867. (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
  9868. struct mbuf *op_err;
  9869. char msg[SCTP_DIAG_INFO_LEN];
  9870. SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
  9871. chk->rec.data.tsn, chk->snd_count);
  9872. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  9873. msg);
  9874. atomic_add_int(&stcb->asoc.refcnt, 1);
  9875. sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
  9876. false, so_locked);
  9877. SCTP_TCB_LOCK(stcb);
  9878. atomic_subtract_int(&stcb->asoc.refcnt, 1);
  9879. return (SCTP_RETRAN_EXIT);
  9880. }
  9881. /* pick up the net */
  9882. net = chk->whoTo;
  9883. switch (net->ro._l_addr.sa.sa_family) {
  9884. #ifdef INET
  9885. case AF_INET:
  9886. mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
  9887. break;
  9888. #endif
  9889. #ifdef INET6
  9890. case AF_INET6:
  9891. mtu = net->mtu - SCTP_MIN_OVERHEAD;
  9892. break;
  9893. #endif
  9894. #if defined(__Userspace__)
  9895. case AF_CONN:
  9896. mtu = net->mtu - sizeof(struct sctphdr);
  9897. break;
  9898. #endif
  9899. default:
  9900. /* TSNH */
  9901. mtu = net->mtu;
  9902. break;
  9903. }
  9904. if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
  9905. /* No room in peers rwnd */
  9906. uint32_t tsn;
  9907. tsn = asoc->last_acked_seq + 1;
  9908. if (tsn == chk->rec.data.tsn) {
  9909. /*
  9910. * we make a special exception for this
  9911. * case. The peer has no rwnd but is missing
  9912. * the lowest chunk.. which is probably what
  9913. * is holding up the rwnd.
  9914. */
  9915. goto one_chunk_around;
  9916. }
  9917. return (1);
  9918. }
  9919. one_chunk_around:
  9920. if (asoc->peers_rwnd < mtu) {
  9921. one_chunk = 1;
  9922. if ((asoc->peers_rwnd == 0) &&
  9923. (asoc->total_flight == 0)) {
  9924. chk->window_probe = 1;
  9925. chk->whoTo->window_probe = 1;
  9926. }
  9927. }
  9928. #ifdef SCTP_AUDITING_ENABLED
  9929. sctp_audit_log(0xC3, 2);
  9930. #endif
  9931. bundle_at = 0;
  9932. m = NULL;
  9933. net->fast_retran_ip = 0;
  9934. if (chk->rec.data.doing_fast_retransmit == 0) {
  9935. /*
  9936. * if no FR in progress skip destination that have
  9937. * flight_size > cwnd.
  9938. */
  9939. if (net->flight_size >= net->cwnd) {
  9940. continue;
  9941. }
  9942. } else {
  9943. /*
  9944. * Mark the destination net to have FR recovery
  9945. * limits put on it.
  9946. */
  9947. *fr_done = 1;
  9948. net->fast_retran_ip = 1;
  9949. }
  9950. /*
  9951. * if no AUTH is yet included and this chunk requires it,
  9952. * make sure to account for it. We don't apply the size
  9953. * until the AUTH chunk is actually added below in case
  9954. * there is no room for this chunk.
  9955. */
  9956. if (data_auth_reqd && (auth == NULL)) {
  9957. dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  9958. } else
  9959. dmtu = 0;
  9960. if ((chk->send_size <= (mtu - dmtu)) ||
  9961. (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
  9962. /* ok we will add this one */
  9963. if (data_auth_reqd) {
  9964. if (auth == NULL) {
  9965. m = sctp_add_auth_chunk(m,
  9966. &endofchain,
  9967. &auth,
  9968. &auth_offset,
  9969. stcb,
  9970. SCTP_DATA);
  9971. auth_keyid = chk->auth_keyid;
  9972. override_ok = 0;
  9973. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  9974. } else if (override_ok) {
  9975. auth_keyid = chk->auth_keyid;
  9976. override_ok = 0;
  9977. } else if (chk->auth_keyid != auth_keyid) {
  9978. /* different keyid, so done bundling */
  9979. break;
  9980. }
  9981. }
  9982. m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
  9983. if (m == NULL) {
  9984. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  9985. return (ENOMEM);
  9986. }
  9987. /* Do clear IP_DF ? */
  9988. if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
  9989. no_fragmentflg = 0;
  9990. }
  9991. /* update our MTU size */
  9992. if (mtu > (chk->send_size + dmtu))
  9993. mtu -= (chk->send_size + dmtu);
  9994. else
  9995. mtu = 0;
  9996. data_list[bundle_at++] = chk;
  9997. if (one_chunk && (asoc->total_flight <= 0)) {
  9998. SCTP_STAT_INCR(sctps_windowprobed);
  9999. }
  10000. }
  10001. if (one_chunk == 0) {
  10002. /*
  10003. * now are there anymore forward from chk to pick
  10004. * up?
  10005. */
  10006. for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
  10007. if (fwd->sent != SCTP_DATAGRAM_RESEND) {
  10008. /* Nope, not for retran */
  10009. continue;
  10010. }
  10011. if (fwd->whoTo != net) {
  10012. /* Nope, not the net in question */
  10013. continue;
  10014. }
  10015. if (data_auth_reqd && (auth == NULL)) {
  10016. dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
  10017. } else
  10018. dmtu = 0;
  10019. if (fwd->send_size <= (mtu - dmtu)) {
  10020. if (data_auth_reqd) {
  10021. if (auth == NULL) {
  10022. m = sctp_add_auth_chunk(m,
  10023. &endofchain,
  10024. &auth,
  10025. &auth_offset,
  10026. stcb,
  10027. SCTP_DATA);
  10028. auth_keyid = fwd->auth_keyid;
  10029. override_ok = 0;
  10030. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  10031. } else if (override_ok) {
  10032. auth_keyid = fwd->auth_keyid;
  10033. override_ok = 0;
  10034. } else if (fwd->auth_keyid != auth_keyid) {
  10035. /* different keyid, so done bundling */
  10036. break;
  10037. }
  10038. }
  10039. m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
  10040. if (m == NULL) {
  10041. SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  10042. return (ENOMEM);
  10043. }
  10044. /* Do clear IP_DF ? */
  10045. if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
  10046. no_fragmentflg = 0;
  10047. }
  10048. /* update our MTU size */
  10049. if (mtu > (fwd->send_size + dmtu))
  10050. mtu -= (fwd->send_size + dmtu);
  10051. else
  10052. mtu = 0;
  10053. data_list[bundle_at++] = fwd;
  10054. if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
  10055. break;
  10056. }
  10057. } else {
  10058. /* can't fit so we are done */
  10059. break;
  10060. }
  10061. }
  10062. }
  10063. /* Is there something to send for this destination? */
  10064. if (m) {
  10065. /*
  10066. * No matter if we fail/or succeed we should start a
  10067. * timer. A failure is like a lost IP packet :-)
  10068. */
  10069. if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
  10070. /*
  10071. * no timer running on this destination
  10072. * restart it.
  10073. */
  10074. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
  10075. tmr_started = 1;
  10076. }
  10077. /* Now lets send it, if there is anything to send :> */
  10078. if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
  10079. (struct sockaddr *)&net->ro._l_addr, m,
  10080. auth_offset, auth, auth_keyid,
  10081. no_fragmentflg, 0, 0,
  10082. inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
  10083. net->port, NULL,
  10084. #if defined(__FreeBSD__) && !defined(__Userspace__)
  10085. 0, 0,
  10086. #endif
  10087. so_locked))) {
  10088. /* error, we could not output */
  10089. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  10090. if (error == ENOBUFS) {
  10091. asoc->ifp_had_enobuf = 1;
  10092. SCTP_STAT_INCR(sctps_lowlevelerr);
  10093. }
  10094. return (error);
  10095. } else {
  10096. asoc->ifp_had_enobuf = 0;
  10097. }
  10098. endofchain = NULL;
  10099. auth = NULL;
  10100. auth_offset = 0;
  10101. /* For HB's */
  10102. /*
  10103. * We don't want to mark the net->sent time here
  10104. * since this we use this for HB and retrans cannot
  10105. * measure RTT
  10106. */
  10107. /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
  10108. /* For auto-close */
  10109. if (*now_filled == 0) {
  10110. (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
  10111. *now = asoc->time_last_sent;
  10112. *now_filled = 1;
  10113. } else {
  10114. asoc->time_last_sent = *now;
  10115. }
  10116. *cnt_out += bundle_at;
  10117. #ifdef SCTP_AUDITING_ENABLED
  10118. sctp_audit_log(0xC4, bundle_at);
  10119. #endif
  10120. if (bundle_at) {
  10121. tsns_sent = data_list[0]->rec.data.tsn;
  10122. }
  10123. for (i = 0; i < bundle_at; i++) {
  10124. SCTP_STAT_INCR(sctps_sendretransdata);
  10125. data_list[i]->sent = SCTP_DATAGRAM_SENT;
  10126. /*
  10127. * When we have a revoked data, and we
  10128. * retransmit it, then we clear the revoked
  10129. * flag since this flag dictates if we
  10130. * subtracted from the fs
  10131. */
  10132. if (data_list[i]->rec.data.chunk_was_revoked) {
  10133. /* Deflate the cwnd */
  10134. data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
  10135. data_list[i]->rec.data.chunk_was_revoked = 0;
  10136. }
  10137. data_list[i]->snd_count++;
  10138. sctp_ucount_decr(asoc->sent_queue_retran_cnt);
  10139. /* record the time */
  10140. data_list[i]->sent_rcv_time = asoc->time_last_sent;
  10141. if (data_list[i]->book_size_scale) {
  10142. /*
  10143. * need to double the book size on
  10144. * this one
  10145. */
  10146. data_list[i]->book_size_scale = 0;
  10147. /* Since we double the booksize, we must
  10148. * also double the output queue size, since this
  10149. * get shrunk when we free by this amount.
  10150. */
  10151. atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
  10152. data_list[i]->book_size *= 2;
  10153. } else {
  10154. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
  10155. sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
  10156. asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
  10157. }
  10158. asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
  10159. (uint32_t) (data_list[i]->send_size +
  10160. SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
  10161. }
  10162. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
  10163. sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
  10164. data_list[i]->whoTo->flight_size,
  10165. data_list[i]->book_size,
  10166. (uint32_t)(uintptr_t)data_list[i]->whoTo,
  10167. data_list[i]->rec.data.tsn);
  10168. }
  10169. sctp_flight_size_increase(data_list[i]);
  10170. sctp_total_flight_increase(stcb, data_list[i]);
  10171. if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
  10172. /* SWS sender side engages */
  10173. asoc->peers_rwnd = 0;
  10174. }
  10175. if ((i == 0) &&
  10176. (data_list[i]->rec.data.doing_fast_retransmit)) {
  10177. SCTP_STAT_INCR(sctps_sendfastretrans);
  10178. if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
  10179. (tmr_started == 0)) {
  10180. /*-
  10181. * ok we just fast-retrans'd
  10182. * the lowest TSN, i.e the
  10183. * first on the list. In
  10184. * this case we want to give
  10185. * some more time to get a
  10186. * SACK back without a
  10187. * t3-expiring.
  10188. */
  10189. sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
  10190. SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
  10191. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
  10192. }
  10193. }
  10194. }
  10195. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  10196. sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
  10197. }
  10198. #ifdef SCTP_AUDITING_ENABLED
  10199. sctp_auditing(21, inp, stcb, NULL);
  10200. #endif
  10201. } else {
  10202. /* None will fit */
  10203. return (1);
  10204. }
  10205. if (asoc->sent_queue_retran_cnt <= 0) {
  10206. /* all done we have no more to retran */
  10207. asoc->sent_queue_retran_cnt = 0;
  10208. break;
  10209. }
  10210. if (one_chunk) {
  10211. /* No more room in rwnd */
  10212. return (1);
  10213. }
  10214. /* stop the for loop here. we sent out a packet */
  10215. break;
  10216. }
  10217. return (0);
  10218. }
  10219. static void
  10220. sctp_timer_validation(struct sctp_inpcb *inp,
  10221. struct sctp_tcb *stcb,
  10222. struct sctp_association *asoc)
  10223. {
  10224. struct sctp_nets *net;
  10225. /* Validate that a timer is running somewhere */
  10226. TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
  10227. if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
  10228. /* Here is a timer */
  10229. return;
  10230. }
  10231. }
  10232. SCTP_TCB_LOCK_ASSERT(stcb);
  10233. /* Gak, we did not have a timer somewhere */
  10234. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
  10235. if (asoc->alternate) {
  10236. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
  10237. } else {
  10238. sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
  10239. }
  10240. return;
  10241. }
  10242. void
  10243. sctp_chunk_output(struct sctp_inpcb *inp,
  10244. struct sctp_tcb *stcb,
  10245. int from_where,
  10246. int so_locked)
  10247. {
  10248. /*-
  10249. * Ok this is the generic chunk service queue. we must do the
  10250. * following:
  10251. * - See if there are retransmits pending, if so we must
  10252. * do these first.
  10253. * - Service the stream queue that is next, moving any
  10254. * message (note I must get a complete message i.e.
  10255. * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
  10256. * TSN's
  10257. * - Check to see if the cwnd/rwnd allows any output, if so we
  10258. * go ahead and formulate and send the low level chunks. Making sure
  10259. * to combine any control in the control chunk queue also.
  10260. */
  10261. struct sctp_association *asoc;
  10262. struct sctp_nets *net;
  10263. int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
  10264. unsigned int burst_cnt = 0;
  10265. struct timeval now;
  10266. int now_filled = 0;
  10267. int nagle_on;
  10268. uint32_t frag_point = sctp_get_frag_point(stcb);
  10269. int un_sent = 0;
  10270. int fr_done;
  10271. unsigned int tot_frs = 0;
  10272. #if defined(__APPLE__) && !defined(__Userspace__)
  10273. if (so_locked) {
  10274. sctp_lock_assert(SCTP_INP_SO(inp));
  10275. } else {
  10276. sctp_unlock_assert(SCTP_INP_SO(inp));
  10277. }
  10278. #endif
  10279. asoc = &stcb->asoc;
  10280. do_it_again:
  10281. /* The Nagle algorithm is only applied when handling a send call. */
  10282. if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
  10283. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
  10284. nagle_on = 0;
  10285. } else {
  10286. nagle_on = 1;
  10287. }
  10288. } else {
  10289. nagle_on = 0;
  10290. }
  10291. SCTP_TCB_LOCK_ASSERT(stcb);
  10292. un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
  10293. if ((un_sent <= 0) &&
  10294. (TAILQ_EMPTY(&asoc->control_send_queue)) &&
  10295. (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
  10296. (asoc->sent_queue_retran_cnt == 0) &&
  10297. (asoc->trigger_reset == 0)) {
  10298. /* Nothing to do unless there is something to be sent left */
  10299. return;
  10300. }
  10301. /* Do we have something to send, data or control AND
  10302. * a sack timer running, if so piggy-back the sack.
  10303. */
  10304. if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
  10305. sctp_send_sack(stcb, so_locked);
  10306. sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
  10307. SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
  10308. }
  10309. while (asoc->sent_queue_retran_cnt) {
  10310. /*-
  10311. * Ok, it is retransmission time only, we send out only ONE
  10312. * packet with a single call off to the retran code.
  10313. */
  10314. if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
  10315. /*-
  10316. * Special hook for handling cookies discarded
  10317. * by peer that carried data. Send cookie-ack only
  10318. * and then the next call with get the retran's.
  10319. */
  10320. (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
  10321. from_where,
  10322. &now, &now_filled, frag_point, so_locked);
  10323. return;
  10324. } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
  10325. /* if its not from a HB then do it */
  10326. fr_done = 0;
  10327. ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
  10328. if (fr_done) {
  10329. tot_frs++;
  10330. }
  10331. } else {
  10332. /*
  10333. * its from any other place, we don't allow retran
  10334. * output (only control)
  10335. */
  10336. ret = 1;
  10337. }
  10338. if (ret > 0) {
  10339. /* Can't send anymore */
  10340. /*-
  10341. * now lets push out control by calling med-level
  10342. * output once. this assures that we WILL send HB's
  10343. * if queued too.
  10344. */
  10345. (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
  10346. from_where,
  10347. &now, &now_filled, frag_point, so_locked);
  10348. #ifdef SCTP_AUDITING_ENABLED
  10349. sctp_auditing(8, inp, stcb, NULL);
  10350. #endif
  10351. sctp_timer_validation(inp, stcb, asoc);
  10352. return;
  10353. }
  10354. if (ret < 0) {
  10355. /*-
  10356. * The count was off.. retran is not happening so do
  10357. * the normal retransmission.
  10358. */
  10359. #ifdef SCTP_AUDITING_ENABLED
  10360. sctp_auditing(9, inp, stcb, NULL);
  10361. #endif
  10362. if (ret == SCTP_RETRAN_EXIT) {
  10363. return;
  10364. }
  10365. break;
  10366. }
  10367. if (from_where == SCTP_OUTPUT_FROM_T3) {
  10368. /* Only one transmission allowed out of a timeout */
  10369. #ifdef SCTP_AUDITING_ENABLED
  10370. sctp_auditing(10, inp, stcb, NULL);
  10371. #endif
  10372. /* Push out any control */
  10373. (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
  10374. &now, &now_filled, frag_point, so_locked);
  10375. return;
  10376. }
  10377. if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
  10378. /* Hit FR burst limit */
  10379. return;
  10380. }
  10381. if ((num_out == 0) && (ret == 0)) {
  10382. /* No more retrans to send */
  10383. break;
  10384. }
  10385. }
  10386. #ifdef SCTP_AUDITING_ENABLED
  10387. sctp_auditing(12, inp, stcb, NULL);
  10388. #endif
  10389. /* Check for bad destinations, if they exist move chunks around. */
  10390. TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
  10391. if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
  10392. /*-
  10393. * if possible move things off of this address we
  10394. * still may send below due to the dormant state but
  10395. * we try to find an alternate address to send to
  10396. * and if we have one we move all queued data on the
  10397. * out wheel to this alternate address.
  10398. */
  10399. if (net->ref_count > 1)
  10400. sctp_move_chunks_from_net(stcb, net);
  10401. } else {
  10402. /*-
  10403. * if ((asoc->sat_network) || (net->addr_is_local))
  10404. * { burst_limit = asoc->max_burst *
  10405. * SCTP_SAT_NETWORK_BURST_INCR; }
  10406. */
  10407. if (asoc->max_burst > 0) {
  10408. if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
  10409. if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
  10410. /* JRS - Use the congestion control given in the congestion control module */
  10411. asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
  10412. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
  10413. sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
  10414. }
  10415. SCTP_STAT_INCR(sctps_maxburstqueued);
  10416. }
  10417. net->fast_retran_ip = 0;
  10418. } else {
  10419. if (net->flight_size == 0) {
  10420. /* Should be decaying the cwnd here */
  10421. ;
  10422. }
  10423. }
  10424. }
  10425. }
  10426. }
  10427. burst_cnt = 0;
  10428. do {
  10429. error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
  10430. &reason_code, 0, from_where,
  10431. &now, &now_filled, frag_point, so_locked);
  10432. if (error) {
  10433. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
  10434. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
  10435. sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
  10436. }
  10437. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  10438. sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
  10439. sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
  10440. }
  10441. break;
  10442. }
  10443. SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
  10444. tot_out += num_out;
  10445. burst_cnt++;
  10446. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  10447. sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
  10448. if (num_out == 0) {
  10449. sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
  10450. }
  10451. }
  10452. if (nagle_on) {
  10453. /*
  10454. * When the Nagle algorithm is used, look at how much
  10455. * is unsent, then if its smaller than an MTU and we
  10456. * have data in flight we stop, except if we are
  10457. * handling a fragmented user message.
  10458. */
  10459. un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
  10460. if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
  10461. (stcb->asoc.total_flight > 0)) {
  10462. /* && sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
  10463. break;
  10464. }
  10465. }
  10466. if (TAILQ_EMPTY(&asoc->control_send_queue) &&
  10467. TAILQ_EMPTY(&asoc->send_queue) &&
  10468. sctp_is_there_unsent_data(stcb, so_locked) == 0) {
  10469. /* Nothing left to send */
  10470. break;
  10471. }
  10472. if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
  10473. /* Nothing left to send */
  10474. break;
  10475. }
  10476. } while (num_out &&
  10477. ((asoc->max_burst == 0) ||
  10478. SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
  10479. (burst_cnt < asoc->max_burst)));
  10480. if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
  10481. if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
  10482. SCTP_STAT_INCR(sctps_maxburstqueued);
  10483. asoc->burst_limit_applied = 1;
  10484. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
  10485. sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
  10486. }
  10487. } else {
  10488. asoc->burst_limit_applied = 0;
  10489. }
  10490. }
  10491. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
  10492. sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
  10493. }
  10494. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
  10495. tot_out);
  10496. /*-
  10497. * Now we need to clean up the control chunk chain if a ECNE is on
  10498. * it. It must be marked as UNSENT again so next call will continue
  10499. * to send it until such time that we get a CWR, to remove it.
  10500. */
  10501. if (stcb->asoc.ecn_echo_cnt_onq)
  10502. sctp_fix_ecn_echo(asoc);
  10503. if (stcb->asoc.trigger_reset) {
  10504. if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
  10505. goto do_it_again;
  10506. }
  10507. }
  10508. return;
  10509. }
  10510. int
  10511. sctp_output(
  10512. struct sctp_inpcb *inp,
  10513. struct mbuf *m,
  10514. struct sockaddr *addr,
  10515. struct mbuf *control,
  10516. #if defined(__FreeBSD__) && !defined(__Userspace__)
  10517. struct thread *p,
  10518. #elif defined(_WIN32) && !defined(__Userspace__)
  10519. PKTHREAD p,
  10520. #else
  10521. #if defined(__APPLE__) && !defined(__Userspace__)
  10522. struct proc *p SCTP_UNUSED,
  10523. #else
  10524. struct proc *p,
  10525. #endif
  10526. #endif
  10527. int flags)
  10528. {
  10529. if (inp == NULL) {
  10530. SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  10531. return (EINVAL);
  10532. }
  10533. if (inp->sctp_socket == NULL) {
  10534. SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  10535. return (EINVAL);
  10536. }
  10537. return (sctp_sosend(inp->sctp_socket,
  10538. addr,
  10539. (struct uio *)NULL,
  10540. m,
  10541. control,
  10542. #if defined(__APPLE__) && !defined(__Userspace__)
  10543. flags
  10544. #else
  10545. flags, p
  10546. #endif
  10547. ));
  10548. }
  10549. void
  10550. send_forward_tsn(struct sctp_tcb *stcb,
  10551. struct sctp_association *asoc)
  10552. {
  10553. struct sctp_tmit_chunk *chk, *at, *tp1, *last;
  10554. struct sctp_forward_tsn_chunk *fwdtsn;
  10555. struct sctp_strseq *strseq;
  10556. struct sctp_strseq_mid *strseq_m;
  10557. uint32_t advance_peer_ack_point;
  10558. unsigned int cnt_of_space, i, ovh;
  10559. unsigned int space_needed;
  10560. unsigned int cnt_of_skipped = 0;
  10561. SCTP_TCB_LOCK_ASSERT(stcb);
  10562. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  10563. if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
  10564. /* mark it to unsent */
  10565. chk->sent = SCTP_DATAGRAM_UNSENT;
  10566. chk->snd_count = 0;
  10567. /* Do we correct its output location? */
  10568. if (chk->whoTo) {
  10569. sctp_free_remote_addr(chk->whoTo);
  10570. chk->whoTo = NULL;
  10571. }
  10572. goto sctp_fill_in_rest;
  10573. }
  10574. }
  10575. /* Ok if we reach here we must build one */
  10576. sctp_alloc_a_chunk(stcb, chk);
  10577. if (chk == NULL) {
  10578. return;
  10579. }
  10580. asoc->fwd_tsn_cnt++;
  10581. chk->copy_by_ref = 0;
  10582. /*
  10583. * We don't do the old thing here since
  10584. * this is used not for on-wire but to
  10585. * tell if we are sending a fwd-tsn by
  10586. * the stack during output. And if its
  10587. * a IFORWARD or a FORWARD it is a fwd-tsn.
  10588. */
  10589. chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
  10590. chk->rec.chunk_id.can_take_data = 0;
  10591. chk->flags = 0;
  10592. chk->asoc = asoc;
  10593. chk->whoTo = NULL;
  10594. chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  10595. if (chk->data == NULL) {
  10596. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  10597. return;
  10598. }
  10599. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  10600. chk->sent = SCTP_DATAGRAM_UNSENT;
  10601. chk->snd_count = 0;
  10602. TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
  10603. asoc->ctrl_queue_cnt++;
  10604. sctp_fill_in_rest:
  10605. /*-
  10606. * Here we go through and fill out the part that deals with
  10607. * stream/seq of the ones we skip.
  10608. */
  10609. SCTP_BUF_LEN(chk->data) = 0;
  10610. TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
  10611. if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
  10612. (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
  10613. /* no more to look at */
  10614. break;
  10615. }
  10616. if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
  10617. /* We don't report these */
  10618. continue;
  10619. }
  10620. cnt_of_skipped++;
  10621. }
  10622. if (asoc->idata_supported) {
  10623. space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
  10624. (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
  10625. } else {
  10626. space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
  10627. (cnt_of_skipped * sizeof(struct sctp_strseq)));
  10628. }
  10629. cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
  10630. if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
  10631. ovh = SCTP_MIN_OVERHEAD;
  10632. } else {
  10633. ovh = SCTP_MIN_V4_OVERHEAD;
  10634. }
  10635. if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
  10636. /* trim to a mtu size */
  10637. cnt_of_space = asoc->smallest_mtu - ovh;
  10638. }
  10639. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
  10640. sctp_misc_ints(SCTP_FWD_TSN_CHECK,
  10641. 0xff, 0, cnt_of_skipped,
  10642. asoc->advanced_peer_ack_point);
  10643. }
  10644. advance_peer_ack_point = asoc->advanced_peer_ack_point;
  10645. if (cnt_of_space < space_needed) {
  10646. /*-
  10647. * ok we must trim down the chunk by lowering the
  10648. * advance peer ack point.
  10649. */
  10650. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
  10651. sctp_misc_ints(SCTP_FWD_TSN_CHECK,
  10652. 0xff, 0xff, cnt_of_space,
  10653. space_needed);
  10654. }
  10655. cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
  10656. if (asoc->idata_supported) {
  10657. cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
  10658. } else {
  10659. cnt_of_skipped /= sizeof(struct sctp_strseq);
  10660. }
  10661. /*-
  10662. * Go through and find the TSN that will be the one
  10663. * we report.
  10664. */
  10665. at = TAILQ_FIRST(&asoc->sent_queue);
  10666. if (at != NULL) {
  10667. for (i = 0; i < cnt_of_skipped; i++) {
  10668. tp1 = TAILQ_NEXT(at, sctp_next);
  10669. if (tp1 == NULL) {
  10670. break;
  10671. }
  10672. at = tp1;
  10673. }
  10674. }
  10675. if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
  10676. sctp_misc_ints(SCTP_FWD_TSN_CHECK,
  10677. 0xff, cnt_of_skipped, at->rec.data.tsn,
  10678. asoc->advanced_peer_ack_point);
  10679. }
  10680. last = at;
  10681. /*-
  10682. * last now points to last one I can report, update
  10683. * peer ack point
  10684. */
  10685. if (last) {
  10686. advance_peer_ack_point = last->rec.data.tsn;
  10687. }
  10688. if (asoc->idata_supported) {
  10689. space_needed = sizeof(struct sctp_forward_tsn_chunk) +
  10690. cnt_of_skipped * sizeof(struct sctp_strseq_mid);
  10691. } else {
  10692. space_needed = sizeof(struct sctp_forward_tsn_chunk) +
  10693. cnt_of_skipped * sizeof(struct sctp_strseq);
  10694. }
  10695. }
  10696. chk->send_size = space_needed;
  10697. /* Setup the chunk */
  10698. fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
  10699. fwdtsn->ch.chunk_length = htons(chk->send_size);
  10700. fwdtsn->ch.chunk_flags = 0;
  10701. if (asoc->idata_supported) {
  10702. fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
  10703. } else {
  10704. fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
  10705. }
  10706. fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
  10707. SCTP_BUF_LEN(chk->data) = chk->send_size;
  10708. fwdtsn++;
  10709. /*-
  10710. * Move pointer to after the fwdtsn and transfer to the
  10711. * strseq pointer.
  10712. */
  10713. if (asoc->idata_supported) {
  10714. strseq_m = (struct sctp_strseq_mid *)fwdtsn;
  10715. strseq = NULL;
  10716. } else {
  10717. strseq = (struct sctp_strseq *)fwdtsn;
  10718. strseq_m = NULL;
  10719. }
  10720. /*-
  10721. * Now populate the strseq list. This is done blindly
  10722. * without pulling out duplicate stream info. This is
  10723. * inefficient but won't harm the process since the peer will
  10724. * look at these in sequence and will thus release anything.
  10725. * It could mean we exceed the PMTU and chop off some that
  10726. * we could have included.. but this is unlikely (aka 1432/4
  10727. * would mean 300+ stream seq's would have to be reported in
  10728. * one FWD-TSN. With a bit of work we can later FIX this to
  10729. * optimize and pull out duplicates.. but it does add more
  10730. * overhead. So for now... not!
  10731. */
  10732. i = 0;
  10733. TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
  10734. if (i >= cnt_of_skipped) {
  10735. break;
  10736. }
  10737. if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
  10738. /* We don't report these */
  10739. continue;
  10740. }
  10741. if (at->rec.data.tsn == advance_peer_ack_point) {
  10742. at->rec.data.fwd_tsn_cnt = 0;
  10743. }
  10744. if (asoc->idata_supported) {
  10745. strseq_m->sid = htons(at->rec.data.sid);
  10746. if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
  10747. strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
  10748. } else {
  10749. strseq_m->flags = 0;
  10750. }
  10751. strseq_m->mid = htonl(at->rec.data.mid);
  10752. strseq_m++;
  10753. } else {
  10754. strseq->sid = htons(at->rec.data.sid);
  10755. strseq->ssn = htons((uint16_t)at->rec.data.mid);
  10756. strseq++;
  10757. }
  10758. i++;
  10759. }
  10760. return;
  10761. }
  10762. void
  10763. sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
  10764. {
  10765. /*-
  10766. * Queue up a SACK or NR-SACK in the control queue.
  10767. * We must first check to see if a SACK or NR-SACK is
  10768. * somehow on the control queue.
  10769. * If so, we will take and and remove the old one.
  10770. */
  10771. struct sctp_association *asoc;
  10772. struct sctp_tmit_chunk *chk, *a_chk;
  10773. struct sctp_sack_chunk *sack;
  10774. struct sctp_nr_sack_chunk *nr_sack;
  10775. struct sctp_gap_ack_block *gap_descriptor;
  10776. const struct sack_track *selector;
  10777. int mergeable = 0;
  10778. int offset;
  10779. caddr_t limit;
  10780. uint32_t *dup;
  10781. int limit_reached = 0;
  10782. unsigned int i, siz, j;
  10783. unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
  10784. int num_dups = 0;
  10785. int space_req;
  10786. uint32_t highest_tsn;
  10787. uint8_t flags;
  10788. uint8_t type;
  10789. uint8_t tsn_map;
  10790. if (stcb->asoc.nrsack_supported == 1) {
  10791. type = SCTP_NR_SELECTIVE_ACK;
  10792. } else {
  10793. type = SCTP_SELECTIVE_ACK;
  10794. }
  10795. a_chk = NULL;
  10796. asoc = &stcb->asoc;
  10797. SCTP_TCB_LOCK_ASSERT(stcb);
  10798. if (asoc->last_data_chunk_from == NULL) {
  10799. /* Hmm we never received anything */
  10800. return;
  10801. }
  10802. sctp_slide_mapping_arrays(stcb);
  10803. sctp_set_rwnd(stcb, asoc);
  10804. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  10805. if (chk->rec.chunk_id.id == type) {
  10806. /* Hmm, found a sack already on queue, remove it */
  10807. TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
  10808. asoc->ctrl_queue_cnt--;
  10809. a_chk = chk;
  10810. if (a_chk->data) {
  10811. sctp_m_freem(a_chk->data);
  10812. a_chk->data = NULL;
  10813. }
  10814. if (a_chk->whoTo) {
  10815. sctp_free_remote_addr(a_chk->whoTo);
  10816. a_chk->whoTo = NULL;
  10817. }
  10818. break;
  10819. }
  10820. }
  10821. if (a_chk == NULL) {
  10822. sctp_alloc_a_chunk(stcb, a_chk);
  10823. if (a_chk == NULL) {
  10824. /* No memory so we drop the idea, and set a timer */
  10825. if (stcb->asoc.delayed_ack) {
  10826. sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
  10827. stcb->sctp_ep, stcb, NULL,
  10828. SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
  10829. sctp_timer_start(SCTP_TIMER_TYPE_RECV,
  10830. stcb->sctp_ep, stcb, NULL);
  10831. } else {
  10832. stcb->asoc.send_sack = 1;
  10833. }
  10834. return;
  10835. }
  10836. a_chk->copy_by_ref = 0;
  10837. a_chk->rec.chunk_id.id = type;
  10838. a_chk->rec.chunk_id.can_take_data = 1;
  10839. }
  10840. /* Clear our pkt counts */
  10841. asoc->data_pkts_seen = 0;
  10842. a_chk->flags = 0;
  10843. a_chk->asoc = asoc;
  10844. a_chk->snd_count = 0;
  10845. a_chk->send_size = 0; /* fill in later */
  10846. a_chk->sent = SCTP_DATAGRAM_UNSENT;
  10847. a_chk->whoTo = NULL;
  10848. if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
  10849. /*-
  10850. * Ok, the destination for the SACK is unreachable, lets see if
  10851. * we can select an alternate to asoc->last_data_chunk_from
  10852. */
  10853. a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
  10854. if (a_chk->whoTo == NULL) {
  10855. /* Nope, no alternate */
  10856. a_chk->whoTo = asoc->last_data_chunk_from;
  10857. }
  10858. } else {
  10859. a_chk->whoTo = asoc->last_data_chunk_from;
  10860. }
  10861. if (a_chk->whoTo) {
  10862. atomic_add_int(&a_chk->whoTo->ref_count, 1);
  10863. }
  10864. if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
  10865. highest_tsn = asoc->highest_tsn_inside_map;
  10866. } else {
  10867. highest_tsn = asoc->highest_tsn_inside_nr_map;
  10868. }
  10869. if (highest_tsn == asoc->cumulative_tsn) {
  10870. /* no gaps */
  10871. if (type == SCTP_SELECTIVE_ACK) {
  10872. space_req = sizeof(struct sctp_sack_chunk);
  10873. } else {
  10874. space_req = sizeof(struct sctp_nr_sack_chunk);
  10875. }
  10876. } else {
  10877. /* gaps get a cluster */
  10878. space_req = MCLBYTES;
  10879. }
  10880. /* Ok now lets formulate a MBUF with our sack */
  10881. a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
  10882. if ((a_chk->data == NULL) ||
  10883. (a_chk->whoTo == NULL)) {
  10884. /* rats, no mbuf memory */
  10885. if (a_chk->data) {
  10886. /* was a problem with the destination */
  10887. sctp_m_freem(a_chk->data);
  10888. a_chk->data = NULL;
  10889. }
  10890. sctp_free_a_chunk(stcb, a_chk, so_locked);
  10891. /* sa_ignore NO_NULL_CHK */
  10892. if (stcb->asoc.delayed_ack) {
  10893. sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
  10894. stcb->sctp_ep, stcb, NULL,
  10895. SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
  10896. sctp_timer_start(SCTP_TIMER_TYPE_RECV,
  10897. stcb->sctp_ep, stcb, NULL);
  10898. } else {
  10899. stcb->asoc.send_sack = 1;
  10900. }
  10901. return;
  10902. }
  10903. /* ok, lets go through and fill it in */
  10904. SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
  10905. space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
  10906. if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
  10907. space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
  10908. }
  10909. limit = mtod(a_chk->data, caddr_t);
  10910. limit += space;
  10911. flags = 0;
  10912. if ((asoc->sctp_cmt_on_off > 0) &&
  10913. SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
  10914. /*-
  10915. * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
  10916. * received, then set high bit to 1, else 0. Reset
  10917. * pkts_rcvd.
  10918. */
  10919. flags |= (asoc->cmt_dac_pkts_rcvd << 6);
  10920. asoc->cmt_dac_pkts_rcvd = 0;
  10921. }
  10922. #ifdef SCTP_ASOCLOG_OF_TSNS
  10923. stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
  10924. stcb->asoc.cumack_log_atsnt++;
  10925. if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
  10926. stcb->asoc.cumack_log_atsnt = 0;
  10927. }
  10928. #endif
  10929. /* reset the readers interpretation */
  10930. stcb->freed_by_sorcv_sincelast = 0;
  10931. if (type == SCTP_SELECTIVE_ACK) {
  10932. sack = mtod(a_chk->data, struct sctp_sack_chunk *);
  10933. nr_sack = NULL;
  10934. gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
  10935. if (highest_tsn > asoc->mapping_array_base_tsn) {
  10936. siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
  10937. } else {
  10938. siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
  10939. }
  10940. } else {
  10941. sack = NULL;
  10942. nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
  10943. gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
  10944. if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
  10945. siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
  10946. } else {
  10947. siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
  10948. }
  10949. }
  10950. if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
  10951. offset = 1;
  10952. } else {
  10953. offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
  10954. }
  10955. if (((type == SCTP_SELECTIVE_ACK) &&
  10956. SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
  10957. ((type == SCTP_NR_SELECTIVE_ACK) &&
  10958. SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
  10959. /* we have a gap .. maybe */
  10960. for (i = 0; i < siz; i++) {
  10961. tsn_map = asoc->mapping_array[i];
  10962. if (type == SCTP_SELECTIVE_ACK) {
  10963. tsn_map |= asoc->nr_mapping_array[i];
  10964. }
  10965. if (i == 0) {
  10966. /*
  10967. * Clear all bits corresponding to TSNs
  10968. * smaller or equal to the cumulative TSN.
  10969. */
  10970. tsn_map &= (~0U << (1 - offset));
  10971. }
  10972. selector = &sack_array[tsn_map];
  10973. if (mergeable && selector->right_edge) {
  10974. /*
  10975. * Backup, left and right edges were ok to
  10976. * merge.
  10977. */
  10978. num_gap_blocks--;
  10979. gap_descriptor--;
  10980. }
  10981. if (selector->num_entries == 0)
  10982. mergeable = 0;
  10983. else {
  10984. for (j = 0; j < selector->num_entries; j++) {
  10985. if (mergeable && selector->right_edge) {
  10986. /*
  10987. * do a merge by NOT setting
  10988. * the left side
  10989. */
  10990. mergeable = 0;
  10991. } else {
  10992. /*
  10993. * no merge, set the left
  10994. * side
  10995. */
  10996. mergeable = 0;
  10997. gap_descriptor->start = htons((selector->gaps[j].start + offset));
  10998. }
  10999. gap_descriptor->end = htons((selector->gaps[j].end + offset));
  11000. num_gap_blocks++;
  11001. gap_descriptor++;
  11002. if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
  11003. /* no more room */
  11004. limit_reached = 1;
  11005. break;
  11006. }
  11007. }
  11008. if (selector->left_edge) {
  11009. mergeable = 1;
  11010. }
  11011. }
  11012. if (limit_reached) {
  11013. /* Reached the limit stop */
  11014. break;
  11015. }
  11016. offset += 8;
  11017. }
  11018. }
  11019. if ((type == SCTP_NR_SELECTIVE_ACK) &&
  11020. (limit_reached == 0)) {
  11021. mergeable = 0;
  11022. if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
  11023. siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
  11024. } else {
  11025. siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
  11026. }
  11027. if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
  11028. offset = 1;
  11029. } else {
  11030. offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
  11031. }
  11032. if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
  11033. /* we have a gap .. maybe */
  11034. for (i = 0; i < siz; i++) {
  11035. tsn_map = asoc->nr_mapping_array[i];
  11036. if (i == 0) {
  11037. /*
  11038. * Clear all bits corresponding to TSNs
  11039. * smaller or equal to the cumulative TSN.
  11040. */
  11041. tsn_map &= (~0U << (1 - offset));
  11042. }
  11043. selector = &sack_array[tsn_map];
  11044. if (mergeable && selector->right_edge) {
  11045. /*
  11046. * Backup, left and right edges were ok to
  11047. * merge.
  11048. */
  11049. num_nr_gap_blocks--;
  11050. gap_descriptor--;
  11051. }
  11052. if (selector->num_entries == 0)
  11053. mergeable = 0;
  11054. else {
  11055. for (j = 0; j < selector->num_entries; j++) {
  11056. if (mergeable && selector->right_edge) {
  11057. /*
  11058. * do a merge by NOT setting
  11059. * the left side
  11060. */
  11061. mergeable = 0;
  11062. } else {
  11063. /*
  11064. * no merge, set the left
  11065. * side
  11066. */
  11067. mergeable = 0;
  11068. gap_descriptor->start = htons((selector->gaps[j].start + offset));
  11069. }
  11070. gap_descriptor->end = htons((selector->gaps[j].end + offset));
  11071. num_nr_gap_blocks++;
  11072. gap_descriptor++;
  11073. if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
  11074. /* no more room */
  11075. limit_reached = 1;
  11076. break;
  11077. }
  11078. }
  11079. if (selector->left_edge) {
  11080. mergeable = 1;
  11081. }
  11082. }
  11083. if (limit_reached) {
  11084. /* Reached the limit stop */
  11085. break;
  11086. }
  11087. offset += 8;
  11088. }
  11089. }
  11090. }
  11091. /* now we must add any dups we are going to report. */
  11092. if ((limit_reached == 0) && (asoc->numduptsns)) {
  11093. dup = (uint32_t *) gap_descriptor;
  11094. for (i = 0; i < asoc->numduptsns; i++) {
  11095. *dup = htonl(asoc->dup_tsns[i]);
  11096. dup++;
  11097. num_dups++;
  11098. if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
  11099. /* no more room */
  11100. break;
  11101. }
  11102. }
  11103. asoc->numduptsns = 0;
  11104. }
  11105. /*
  11106. * now that the chunk is prepared queue it to the control chunk
  11107. * queue.
  11108. */
  11109. if (type == SCTP_SELECTIVE_ACK) {
  11110. a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
  11111. (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
  11112. num_dups * sizeof(int32_t));
  11113. SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
  11114. sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
  11115. sack->sack.a_rwnd = htonl(asoc->my_rwnd);
  11116. sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
  11117. sack->sack.num_dup_tsns = htons(num_dups);
  11118. sack->ch.chunk_type = type;
  11119. sack->ch.chunk_flags = flags;
  11120. sack->ch.chunk_length = htons(a_chk->send_size);
  11121. } else {
  11122. a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
  11123. (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
  11124. num_dups * sizeof(int32_t));
  11125. SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
  11126. nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
  11127. nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
  11128. nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
  11129. nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
  11130. nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
  11131. nr_sack->nr_sack.reserved = 0;
  11132. nr_sack->ch.chunk_type = type;
  11133. nr_sack->ch.chunk_flags = flags;
  11134. nr_sack->ch.chunk_length = htons(a_chk->send_size);
  11135. }
  11136. TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
  11137. asoc->my_last_reported_rwnd = asoc->my_rwnd;
  11138. asoc->ctrl_queue_cnt++;
  11139. asoc->send_sack = 0;
  11140. SCTP_STAT_INCR(sctps_sendsacks);
  11141. return;
  11142. }
  11143. void
  11144. sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
  11145. {
  11146. struct mbuf *m_abort, *m, *m_last;
  11147. struct mbuf *m_out, *m_end = NULL;
  11148. struct sctp_abort_chunk *abort;
  11149. struct sctp_auth_chunk *auth = NULL;
  11150. struct sctp_nets *net;
  11151. uint32_t vtag;
  11152. uint32_t auth_offset = 0;
  11153. int error;
  11154. uint16_t cause_len, chunk_len, padding_len;
  11155. #if defined(__APPLE__) && !defined(__Userspace__)
  11156. if (so_locked) {
  11157. sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
  11158. } else {
  11159. sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
  11160. }
  11161. #endif
  11162. SCTP_TCB_LOCK_ASSERT(stcb);
  11163. /*-
  11164. * Add an AUTH chunk, if chunk requires it and save the offset into
  11165. * the chain for AUTH
  11166. */
  11167. if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
  11168. stcb->asoc.peer_auth_chunks)) {
  11169. m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
  11170. stcb, SCTP_ABORT_ASSOCIATION);
  11171. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  11172. } else {
  11173. m_out = NULL;
  11174. }
  11175. m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
  11176. if (m_abort == NULL) {
  11177. if (m_out) {
  11178. sctp_m_freem(m_out);
  11179. }
  11180. if (operr) {
  11181. sctp_m_freem(operr);
  11182. }
  11183. return;
  11184. }
  11185. /* link in any error */
  11186. SCTP_BUF_NEXT(m_abort) = operr;
  11187. cause_len = 0;
  11188. m_last = NULL;
  11189. for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
  11190. cause_len += (uint16_t)SCTP_BUF_LEN(m);
  11191. if (SCTP_BUF_NEXT(m) == NULL) {
  11192. m_last = m;
  11193. }
  11194. }
  11195. SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
  11196. chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
  11197. padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
  11198. if (m_out == NULL) {
  11199. /* NO Auth chunk prepended, so reserve space in front */
  11200. SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
  11201. m_out = m_abort;
  11202. } else {
  11203. /* Put AUTH chunk at the front of the chain */
  11204. SCTP_BUF_NEXT(m_end) = m_abort;
  11205. }
  11206. if (stcb->asoc.alternate) {
  11207. net = stcb->asoc.alternate;
  11208. } else {
  11209. net = stcb->asoc.primary_destination;
  11210. }
  11211. /* Fill in the ABORT chunk header. */
  11212. abort = mtod(m_abort, struct sctp_abort_chunk *);
  11213. abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
  11214. if (stcb->asoc.peer_vtag == 0) {
  11215. /* This happens iff the assoc is in COOKIE-WAIT state. */
  11216. vtag = stcb->asoc.my_vtag;
  11217. abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
  11218. } else {
  11219. vtag = stcb->asoc.peer_vtag;
  11220. abort->ch.chunk_flags = 0;
  11221. }
  11222. abort->ch.chunk_length = htons(chunk_len);
  11223. /* Add padding, if necessary. */
  11224. if (padding_len > 0) {
  11225. if ((m_last == NULL) ||
  11226. (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
  11227. sctp_m_freem(m_out);
  11228. return;
  11229. }
  11230. }
  11231. if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
  11232. (struct sockaddr *)&net->ro._l_addr,
  11233. m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
  11234. stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
  11235. stcb->asoc.primary_destination->port, NULL,
  11236. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11237. 0, 0,
  11238. #endif
  11239. so_locked))) {
  11240. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  11241. if (error == ENOBUFS) {
  11242. stcb->asoc.ifp_had_enobuf = 1;
  11243. SCTP_STAT_INCR(sctps_lowlevelerr);
  11244. }
  11245. } else {
  11246. stcb->asoc.ifp_had_enobuf = 0;
  11247. }
  11248. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  11249. }
  11250. void
  11251. sctp_send_shutdown_complete(struct sctp_tcb *stcb,
  11252. struct sctp_nets *net,
  11253. int reflect_vtag)
  11254. {
  11255. /* formulate and SEND a SHUTDOWN-COMPLETE */
  11256. struct mbuf *m_shutdown_comp;
  11257. struct sctp_shutdown_complete_chunk *shutdown_complete;
  11258. uint32_t vtag;
  11259. int error;
  11260. uint8_t flags;
  11261. m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
  11262. if (m_shutdown_comp == NULL) {
  11263. /* no mbuf's */
  11264. return;
  11265. }
  11266. if (reflect_vtag) {
  11267. flags = SCTP_HAD_NO_TCB;
  11268. vtag = stcb->asoc.my_vtag;
  11269. } else {
  11270. flags = 0;
  11271. vtag = stcb->asoc.peer_vtag;
  11272. }
  11273. shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
  11274. shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
  11275. shutdown_complete->ch.chunk_flags = flags;
  11276. shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
  11277. SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
  11278. if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
  11279. (struct sockaddr *)&net->ro._l_addr,
  11280. m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
  11281. stcb->sctp_ep->sctp_lport, stcb->rport,
  11282. htonl(vtag),
  11283. net->port, NULL,
  11284. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11285. 0, 0,
  11286. #endif
  11287. SCTP_SO_NOT_LOCKED))) {
  11288. SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
  11289. if (error == ENOBUFS) {
  11290. stcb->asoc.ifp_had_enobuf = 1;
  11291. SCTP_STAT_INCR(sctps_lowlevelerr);
  11292. }
  11293. } else {
  11294. stcb->asoc.ifp_had_enobuf = 0;
  11295. }
  11296. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  11297. return;
  11298. }
  11299. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11300. static void
  11301. sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
  11302. struct sctphdr *sh, uint32_t vtag,
  11303. uint8_t type, struct mbuf *cause,
  11304. uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
  11305. uint32_t vrf_id, uint16_t port)
  11306. #else
  11307. static void
  11308. sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
  11309. struct sctphdr *sh, uint32_t vtag,
  11310. uint8_t type, struct mbuf *cause,
  11311. uint32_t vrf_id SCTP_UNUSED, uint16_t port)
  11312. #endif
  11313. {
  11314. struct mbuf *o_pak;
  11315. struct mbuf *mout;
  11316. struct sctphdr *shout;
  11317. struct sctp_chunkhdr *ch;
  11318. #if defined(INET) || defined(INET6)
  11319. struct udphdr *udp;
  11320. #endif
  11321. int ret, len, cause_len, padding_len;
  11322. #ifdef INET
  11323. #if defined(__APPLE__) && !defined(__Userspace__)
  11324. sctp_route_t ro;
  11325. #endif
  11326. struct sockaddr_in *src_sin, *dst_sin;
  11327. struct ip *ip;
  11328. #endif
  11329. #ifdef INET6
  11330. struct sockaddr_in6 *src_sin6, *dst_sin6;
  11331. struct ip6_hdr *ip6;
  11332. #endif
  11333. /* Compute the length of the cause and add final padding. */
  11334. cause_len = 0;
  11335. if (cause != NULL) {
  11336. struct mbuf *m_at, *m_last = NULL;
  11337. for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
  11338. if (SCTP_BUF_NEXT(m_at) == NULL)
  11339. m_last = m_at;
  11340. cause_len += SCTP_BUF_LEN(m_at);
  11341. }
  11342. padding_len = cause_len % 4;
  11343. if (padding_len != 0) {
  11344. padding_len = 4 - padding_len;
  11345. }
  11346. if (padding_len != 0) {
  11347. if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
  11348. sctp_m_freem(cause);
  11349. return;
  11350. }
  11351. }
  11352. } else {
  11353. padding_len = 0;
  11354. }
  11355. /* Get an mbuf for the header. */
  11356. len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
  11357. switch (dst->sa_family) {
  11358. #ifdef INET
  11359. case AF_INET:
  11360. len += sizeof(struct ip);
  11361. break;
  11362. #endif
  11363. #ifdef INET6
  11364. case AF_INET6:
  11365. len += sizeof(struct ip6_hdr);
  11366. break;
  11367. #endif
  11368. default:
  11369. break;
  11370. }
  11371. #if defined(INET) || defined(INET6)
  11372. if (port) {
  11373. len += sizeof(struct udphdr);
  11374. }
  11375. #endif
  11376. #if defined(__APPLE__) && !defined(__Userspace__)
  11377. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  11378. mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
  11379. #else
  11380. mout = sctp_get_mbuf_for_msg(len + SCTP_MAX_LINKHDR, 1, M_NOWAIT, 1, MT_DATA);
  11381. #endif
  11382. #else
  11383. mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
  11384. #endif
  11385. if (mout == NULL) {
  11386. if (cause) {
  11387. sctp_m_freem(cause);
  11388. }
  11389. return;
  11390. }
  11391. #if defined(__APPLE__) && !defined(__Userspace__)
  11392. #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
  11393. SCTP_BUF_RESV_UF(mout, max_linkhdr);
  11394. #else
  11395. SCTP_BUF_RESV_UF(mout, SCTP_MAX_LINKHDR);
  11396. #endif
  11397. #else
  11398. SCTP_BUF_RESV_UF(mout, max_linkhdr);
  11399. #endif
  11400. SCTP_BUF_LEN(mout) = len;
  11401. SCTP_BUF_NEXT(mout) = cause;
  11402. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11403. M_SETFIB(mout, fibnum);
  11404. mout->m_pkthdr.flowid = mflowid;
  11405. M_HASHTYPE_SET(mout, mflowtype);
  11406. #endif
  11407. #ifdef INET
  11408. ip = NULL;
  11409. #endif
  11410. #ifdef INET6
  11411. ip6 = NULL;
  11412. #endif
  11413. switch (dst->sa_family) {
  11414. #ifdef INET
  11415. case AF_INET:
  11416. src_sin = (struct sockaddr_in *)src;
  11417. dst_sin = (struct sockaddr_in *)dst;
  11418. ip = mtod(mout, struct ip *);
  11419. ip->ip_v = IPVERSION;
  11420. ip->ip_hl = (sizeof(struct ip) >> 2);
  11421. ip->ip_tos = 0;
  11422. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11423. ip->ip_off = htons(IP_DF);
  11424. #elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__)
  11425. ip->ip_off = IP_DF;
  11426. #else
  11427. ip->ip_off = htons(IP_DF);
  11428. #endif
  11429. #if defined(__Userspace__)
  11430. ip->ip_id = htons(ip_id++);
  11431. #elif defined(__FreeBSD__)
  11432. ip_fillid(ip);
  11433. #elif defined(__APPLE__)
  11434. #if RANDOM_IP_ID
  11435. ip->ip_id = ip_randomid();
  11436. #else
  11437. ip->ip_id = htons(ip_id++);
  11438. #endif
  11439. #else
  11440. ip->ip_id = ip_id++;
  11441. #endif
  11442. ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
  11443. if (port) {
  11444. ip->ip_p = IPPROTO_UDP;
  11445. } else {
  11446. ip->ip_p = IPPROTO_SCTP;
  11447. }
  11448. ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
  11449. ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
  11450. ip->ip_sum = 0;
  11451. len = sizeof(struct ip);
  11452. shout = (struct sctphdr *)((caddr_t)ip + len);
  11453. break;
  11454. #endif
  11455. #ifdef INET6
  11456. case AF_INET6:
  11457. src_sin6 = (struct sockaddr_in6 *)src;
  11458. dst_sin6 = (struct sockaddr_in6 *)dst;
  11459. ip6 = mtod(mout, struct ip6_hdr *);
  11460. ip6->ip6_flow = htonl(0x60000000);
  11461. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11462. if (V_ip6_auto_flowlabel) {
  11463. ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
  11464. }
  11465. #endif
  11466. #if defined(__Userspace__)
  11467. ip6->ip6_hlim = IPv6_HOP_LIMIT;
  11468. #else
  11469. ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
  11470. #endif
  11471. if (port) {
  11472. ip6->ip6_nxt = IPPROTO_UDP;
  11473. } else {
  11474. ip6->ip6_nxt = IPPROTO_SCTP;
  11475. }
  11476. ip6->ip6_src = dst_sin6->sin6_addr;
  11477. ip6->ip6_dst = src_sin6->sin6_addr;
  11478. len = sizeof(struct ip6_hdr);
  11479. shout = (struct sctphdr *)((caddr_t)ip6 + len);
  11480. break;
  11481. #endif
  11482. default:
  11483. len = 0;
  11484. shout = mtod(mout, struct sctphdr *);
  11485. break;
  11486. }
  11487. #if defined(INET) || defined(INET6)
  11488. if (port) {
  11489. if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
  11490. sctp_m_freem(mout);
  11491. return;
  11492. }
  11493. udp = (struct udphdr *)shout;
  11494. udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
  11495. udp->uh_dport = port;
  11496. udp->uh_sum = 0;
  11497. udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
  11498. sizeof(struct sctphdr) +
  11499. sizeof(struct sctp_chunkhdr) +
  11500. cause_len + padding_len));
  11501. len += sizeof(struct udphdr);
  11502. shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
  11503. } else {
  11504. udp = NULL;
  11505. }
  11506. #endif
  11507. shout->src_port = sh->dest_port;
  11508. shout->dest_port = sh->src_port;
  11509. shout->checksum = 0;
  11510. if (vtag) {
  11511. shout->v_tag = htonl(vtag);
  11512. } else {
  11513. shout->v_tag = sh->v_tag;
  11514. }
  11515. len += sizeof(struct sctphdr);
  11516. ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
  11517. ch->chunk_type = type;
  11518. if (vtag) {
  11519. ch->chunk_flags = 0;
  11520. } else {
  11521. ch->chunk_flags = SCTP_HAD_NO_TCB;
  11522. }
  11523. ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
  11524. len += sizeof(struct sctp_chunkhdr);
  11525. len += cause_len + padding_len;
  11526. if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
  11527. sctp_m_freem(mout);
  11528. return;
  11529. }
  11530. SCTP_ATTACH_CHAIN(o_pak, mout, len);
  11531. switch (dst->sa_family) {
  11532. #ifdef INET
  11533. case AF_INET:
  11534. #if defined(__APPLE__) && !defined(__Userspace__)
  11535. /* zap the stack pointer to the route */
  11536. memset(&ro, 0, sizeof(sctp_route_t));
  11537. #endif
  11538. if (port) {
  11539. #if !defined(_WIN32) && !defined(__Userspace__)
  11540. #if defined(__FreeBSD__)
  11541. if (V_udp_cksum) {
  11542. udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
  11543. } else {
  11544. udp->uh_sum = 0;
  11545. }
  11546. #else
  11547. udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
  11548. #endif
  11549. #else
  11550. udp->uh_sum = 0;
  11551. #endif
  11552. }
  11553. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11554. ip->ip_len = htons(len);
  11555. #elif defined(__APPLE__) || defined(__Userspace__)
  11556. ip->ip_len = len;
  11557. #else
  11558. ip->ip_len = htons(len);
  11559. #endif
  11560. if (port) {
  11561. shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
  11562. SCTP_STAT_INCR(sctps_sendswcrc);
  11563. #if !defined(_WIN32) && !defined(__Userspace__)
  11564. #if defined(__FreeBSD__)
  11565. if (V_udp_cksum) {
  11566. SCTP_ENABLE_UDP_CSUM(o_pak);
  11567. }
  11568. #else
  11569. SCTP_ENABLE_UDP_CSUM(o_pak);
  11570. #endif
  11571. #endif
  11572. } else {
  11573. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11574. mout->m_pkthdr.csum_flags = CSUM_SCTP;
  11575. mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
  11576. SCTP_STAT_INCR(sctps_sendhwcrc);
  11577. #else
  11578. shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip));
  11579. SCTP_STAT_INCR(sctps_sendswcrc);
  11580. #endif
  11581. }
  11582. #ifdef SCTP_PACKET_LOGGING
  11583. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
  11584. sctp_packet_log(o_pak);
  11585. }
  11586. #endif
  11587. #if defined(__APPLE__) && !defined(__Userspace__)
  11588. SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
  11589. /* Free the route if we got one back */
  11590. if (ro.ro_rt) {
  11591. RTFREE(ro.ro_rt);
  11592. ro.ro_rt = NULL;
  11593. }
  11594. #else
  11595. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11596. SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
  11597. #endif
  11598. SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
  11599. #endif
  11600. break;
  11601. #endif
  11602. #ifdef INET6
  11603. case AF_INET6:
  11604. ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
  11605. if (port) {
  11606. shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
  11607. SCTP_STAT_INCR(sctps_sendswcrc);
  11608. #if !defined(__Userspace__)
  11609. #if defined(_WIN32)
  11610. udp->uh_sum = 0;
  11611. #else
  11612. if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
  11613. udp->uh_sum = 0xffff;
  11614. }
  11615. #endif
  11616. #endif
  11617. } else {
  11618. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11619. mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
  11620. mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
  11621. SCTP_STAT_INCR(sctps_sendhwcrc);
  11622. #else
  11623. shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr));
  11624. SCTP_STAT_INCR(sctps_sendswcrc);
  11625. #endif
  11626. }
  11627. #ifdef SCTP_PACKET_LOGGING
  11628. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
  11629. sctp_packet_log(o_pak);
  11630. }
  11631. #endif
  11632. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11633. SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
  11634. #endif
  11635. SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
  11636. break;
  11637. #endif
  11638. #if defined(__Userspace__)
  11639. case AF_CONN:
  11640. {
  11641. char *buffer;
  11642. struct sockaddr_conn *sconn;
  11643. sconn = (struct sockaddr_conn *)src;
  11644. if (SCTP_BASE_VAR(crc32c_offloaded) == 0) {
  11645. shout->checksum = sctp_calculate_cksum(o_pak, 0);
  11646. SCTP_STAT_INCR(sctps_sendswcrc);
  11647. } else {
  11648. SCTP_STAT_INCR(sctps_sendhwcrc);
  11649. }
  11650. #ifdef SCTP_PACKET_LOGGING
  11651. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
  11652. sctp_packet_log(mout);
  11653. }
  11654. #endif
  11655. /* Don't alloc/free for each packet */
  11656. if ((buffer = malloc(len)) != NULL) {
  11657. m_copydata(o_pak, 0, len, buffer);
  11658. ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, len, 0, 0);
  11659. free(buffer);
  11660. } else {
  11661. ret = ENOMEM;
  11662. }
  11663. sctp_m_freem(o_pak);
  11664. break;
  11665. }
  11666. #endif
  11667. default:
  11668. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
  11669. dst->sa_family);
  11670. sctp_m_freem(mout);
  11671. SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
  11672. return;
  11673. }
  11674. SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
  11675. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11676. if (port) {
  11677. UDPSTAT_INC(udps_opackets);
  11678. }
  11679. #endif
  11680. SCTP_STAT_INCR(sctps_sendpackets);
  11681. SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
  11682. SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
  11683. if (ret) {
  11684. SCTP_STAT_INCR(sctps_senderrors);
  11685. }
  11686. return;
  11687. }
  11688. void
  11689. sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
  11690. struct sctphdr *sh,
  11691. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11692. uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
  11693. #endif
  11694. uint32_t vrf_id, uint16_t port)
  11695. {
  11696. sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
  11697. #if defined(__FreeBSD__) && !defined(__Userspace__)
  11698. mflowtype, mflowid, fibnum,
  11699. #endif
  11700. vrf_id, port);
  11701. }
  11702. void
  11703. sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net,int so_locked)
  11704. {
  11705. struct sctp_tmit_chunk *chk;
  11706. struct sctp_heartbeat_chunk *hb;
  11707. struct timeval now;
  11708. SCTP_TCB_LOCK_ASSERT(stcb);
  11709. if (net == NULL) {
  11710. return;
  11711. }
  11712. (void)SCTP_GETTIME_TIMEVAL(&now);
  11713. switch (net->ro._l_addr.sa.sa_family) {
  11714. #ifdef INET
  11715. case AF_INET:
  11716. break;
  11717. #endif
  11718. #ifdef INET6
  11719. case AF_INET6:
  11720. break;
  11721. #endif
  11722. #if defined(__Userspace__)
  11723. case AF_CONN:
  11724. break;
  11725. #endif
  11726. default:
  11727. return;
  11728. }
  11729. sctp_alloc_a_chunk(stcb, chk);
  11730. if (chk == NULL) {
  11731. SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
  11732. return;
  11733. }
  11734. chk->copy_by_ref = 0;
  11735. chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
  11736. chk->rec.chunk_id.can_take_data = 1;
  11737. chk->flags = 0;
  11738. chk->asoc = &stcb->asoc;
  11739. chk->send_size = sizeof(struct sctp_heartbeat_chunk);
  11740. chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
  11741. if (chk->data == NULL) {
  11742. sctp_free_a_chunk(stcb, chk, so_locked);
  11743. return;
  11744. }
  11745. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  11746. SCTP_BUF_LEN(chk->data) = chk->send_size;
  11747. chk->sent = SCTP_DATAGRAM_UNSENT;
  11748. chk->snd_count = 0;
  11749. chk->whoTo = net;
  11750. atomic_add_int(&chk->whoTo->ref_count, 1);
  11751. /* Now we have a mbuf that we can fill in with the details */
  11752. hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
  11753. memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
  11754. /* fill out chunk header */
  11755. hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
  11756. hb->ch.chunk_flags = 0;
  11757. hb->ch.chunk_length = htons(chk->send_size);
  11758. /* Fill out hb parameter */
  11759. hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
  11760. hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
  11761. hb->heartbeat.hb_info.time_value_1 = (uint32_t)now.tv_sec;
  11762. hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
  11763. /* Did our user request this one, put it in */
  11764. hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
  11765. #ifdef HAVE_SA_LEN
  11766. hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
  11767. #else
  11768. switch (net->ro._l_addr.sa.sa_family) {
  11769. #ifdef INET
  11770. case AF_INET:
  11771. hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in);
  11772. break;
  11773. #endif
  11774. #ifdef INET6
  11775. case AF_INET6:
  11776. hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in6);
  11777. break;
  11778. #endif
  11779. #if defined(__Userspace__)
  11780. case AF_CONN:
  11781. hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_conn);
  11782. break;
  11783. #endif
  11784. default:
  11785. hb->heartbeat.hb_info.addr_len = 0;
  11786. break;
  11787. }
  11788. #endif
  11789. if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
  11790. /*
  11791. * we only take from the entropy pool if the address is not
  11792. * confirmed.
  11793. */
  11794. net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
  11795. net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
  11796. } else {
  11797. net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
  11798. net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
  11799. }
  11800. switch (net->ro._l_addr.sa.sa_family) {
  11801. #ifdef INET
  11802. case AF_INET:
  11803. memcpy(hb->heartbeat.hb_info.address,
  11804. &net->ro._l_addr.sin.sin_addr,
  11805. sizeof(net->ro._l_addr.sin.sin_addr));
  11806. break;
  11807. #endif
  11808. #ifdef INET6
  11809. case AF_INET6:
  11810. memcpy(hb->heartbeat.hb_info.address,
  11811. &net->ro._l_addr.sin6.sin6_addr,
  11812. sizeof(net->ro._l_addr.sin6.sin6_addr));
  11813. break;
  11814. #endif
  11815. #if defined(__Userspace__)
  11816. case AF_CONN:
  11817. memcpy(hb->heartbeat.hb_info.address,
  11818. &net->ro._l_addr.sconn.sconn_addr,
  11819. sizeof(net->ro._l_addr.sconn.sconn_addr));
  11820. break;
  11821. #endif
  11822. default:
  11823. if (chk->data) {
  11824. sctp_m_freem(chk->data);
  11825. chk->data = NULL;
  11826. }
  11827. sctp_free_a_chunk(stcb, chk, so_locked);
  11828. return;
  11829. break;
  11830. }
  11831. net->hb_responded = 0;
  11832. TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
  11833. stcb->asoc.ctrl_queue_cnt++;
  11834. SCTP_STAT_INCR(sctps_sendheartbeat);
  11835. return;
  11836. }
  11837. void
  11838. sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
  11839. uint32_t high_tsn)
  11840. {
  11841. struct sctp_association *asoc;
  11842. struct sctp_ecne_chunk *ecne;
  11843. struct sctp_tmit_chunk *chk;
  11844. if (net == NULL) {
  11845. return;
  11846. }
  11847. asoc = &stcb->asoc;
  11848. SCTP_TCB_LOCK_ASSERT(stcb);
  11849. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  11850. if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
  11851. /* found a previous ECN_ECHO update it if needed */
  11852. uint32_t cnt, ctsn;
  11853. ecne = mtod(chk->data, struct sctp_ecne_chunk *);
  11854. ctsn = ntohl(ecne->tsn);
  11855. if (SCTP_TSN_GT(high_tsn, ctsn)) {
  11856. ecne->tsn = htonl(high_tsn);
  11857. SCTP_STAT_INCR(sctps_queue_upd_ecne);
  11858. }
  11859. cnt = ntohl(ecne->num_pkts_since_cwr);
  11860. cnt++;
  11861. ecne->num_pkts_since_cwr = htonl(cnt);
  11862. return;
  11863. }
  11864. }
  11865. /* nope could not find one to update so we must build one */
  11866. sctp_alloc_a_chunk(stcb, chk);
  11867. if (chk == NULL) {
  11868. return;
  11869. }
  11870. SCTP_STAT_INCR(sctps_queue_upd_ecne);
  11871. chk->copy_by_ref = 0;
  11872. chk->rec.chunk_id.id = SCTP_ECN_ECHO;
  11873. chk->rec.chunk_id.can_take_data = 0;
  11874. chk->flags = 0;
  11875. chk->asoc = &stcb->asoc;
  11876. chk->send_size = sizeof(struct sctp_ecne_chunk);
  11877. chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
  11878. if (chk->data == NULL) {
  11879. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  11880. return;
  11881. }
  11882. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  11883. SCTP_BUF_LEN(chk->data) = chk->send_size;
  11884. chk->sent = SCTP_DATAGRAM_UNSENT;
  11885. chk->snd_count = 0;
  11886. chk->whoTo = net;
  11887. atomic_add_int(&chk->whoTo->ref_count, 1);
  11888. stcb->asoc.ecn_echo_cnt_onq++;
  11889. ecne = mtod(chk->data, struct sctp_ecne_chunk *);
  11890. ecne->ch.chunk_type = SCTP_ECN_ECHO;
  11891. ecne->ch.chunk_flags = 0;
  11892. ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
  11893. ecne->tsn = htonl(high_tsn);
  11894. ecne->num_pkts_since_cwr = htonl(1);
  11895. TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
  11896. asoc->ctrl_queue_cnt++;
  11897. }
  11898. void
  11899. sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
  11900. struct mbuf *m, int len, int iphlen, int bad_crc)
  11901. {
  11902. struct sctp_association *asoc;
  11903. struct sctp_pktdrop_chunk *drp;
  11904. struct sctp_tmit_chunk *chk;
  11905. uint8_t *datap;
  11906. int was_trunc = 0;
  11907. int fullsz = 0;
  11908. long spc;
  11909. int offset;
  11910. struct sctp_chunkhdr *ch, chunk_buf;
  11911. unsigned int chk_length;
  11912. if (!stcb) {
  11913. return;
  11914. }
  11915. asoc = &stcb->asoc;
  11916. SCTP_TCB_LOCK_ASSERT(stcb);
  11917. if (asoc->pktdrop_supported == 0) {
  11918. /*-
  11919. * peer must declare support before I send one.
  11920. */
  11921. return;
  11922. }
  11923. if (stcb->sctp_socket == NULL) {
  11924. return;
  11925. }
  11926. sctp_alloc_a_chunk(stcb, chk);
  11927. if (chk == NULL) {
  11928. return;
  11929. }
  11930. chk->copy_by_ref = 0;
  11931. chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
  11932. chk->rec.chunk_id.can_take_data = 1;
  11933. chk->flags = 0;
  11934. len -= iphlen;
  11935. chk->send_size = len;
  11936. /* Validate that we do not have an ABORT in here. */
  11937. offset = iphlen + sizeof(struct sctphdr);
  11938. ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
  11939. sizeof(*ch), (uint8_t *) & chunk_buf);
  11940. while (ch != NULL) {
  11941. chk_length = ntohs(ch->chunk_length);
  11942. if (chk_length < sizeof(*ch)) {
  11943. /* break to abort land */
  11944. break;
  11945. }
  11946. switch (ch->chunk_type) {
  11947. case SCTP_PACKET_DROPPED:
  11948. case SCTP_ABORT_ASSOCIATION:
  11949. case SCTP_INITIATION_ACK:
  11950. /**
  11951. * We don't respond with an PKT-DROP to an ABORT
  11952. * or PKT-DROP. We also do not respond to an
  11953. * INIT-ACK, because we can't know if the initiation
  11954. * tag is correct or not.
  11955. */
  11956. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  11957. return;
  11958. default:
  11959. break;
  11960. }
  11961. offset += SCTP_SIZE32(chk_length);
  11962. ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
  11963. sizeof(*ch), (uint8_t *) & chunk_buf);
  11964. }
  11965. if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
  11966. min(stcb->asoc.smallest_mtu, MCLBYTES)) {
  11967. /* only send 1 mtu worth, trim off the
  11968. * excess on the end.
  11969. */
  11970. fullsz = len;
  11971. len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
  11972. was_trunc = 1;
  11973. }
  11974. chk->asoc = &stcb->asoc;
  11975. chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  11976. if (chk->data == NULL) {
  11977. jump_out:
  11978. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  11979. return;
  11980. }
  11981. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  11982. drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
  11983. if (drp == NULL) {
  11984. sctp_m_freem(chk->data);
  11985. chk->data = NULL;
  11986. goto jump_out;
  11987. }
  11988. chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
  11989. sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
  11990. chk->book_size_scale = 0;
  11991. if (was_trunc) {
  11992. drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
  11993. drp->trunc_len = htons(fullsz);
  11994. /* Len is already adjusted to size minus overhead above
  11995. * take out the pkt_drop chunk itself from it.
  11996. */
  11997. chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
  11998. len = chk->send_size;
  11999. } else {
  12000. /* no truncation needed */
  12001. drp->ch.chunk_flags = 0;
  12002. drp->trunc_len = htons(0);
  12003. }
  12004. if (bad_crc) {
  12005. drp->ch.chunk_flags |= SCTP_BADCRC;
  12006. }
  12007. chk->send_size += sizeof(struct sctp_pktdrop_chunk);
  12008. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12009. chk->sent = SCTP_DATAGRAM_UNSENT;
  12010. chk->snd_count = 0;
  12011. if (net) {
  12012. /* we should hit here */
  12013. chk->whoTo = net;
  12014. atomic_add_int(&chk->whoTo->ref_count, 1);
  12015. } else {
  12016. chk->whoTo = NULL;
  12017. }
  12018. drp->ch.chunk_type = SCTP_PACKET_DROPPED;
  12019. drp->ch.chunk_length = htons(chk->send_size);
  12020. spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
  12021. if (spc < 0) {
  12022. spc = 0;
  12023. }
  12024. drp->bottle_bw = htonl(spc);
  12025. if (asoc->my_rwnd) {
  12026. drp->current_onq = htonl(asoc->size_on_reasm_queue +
  12027. asoc->size_on_all_streams +
  12028. asoc->my_rwnd_control_len +
  12029. SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
  12030. } else {
  12031. /*-
  12032. * If my rwnd is 0, possibly from mbuf depletion as well as
  12033. * space used, tell the peer there is NO space aka onq == bw
  12034. */
  12035. drp->current_onq = htonl(spc);
  12036. }
  12037. drp->reserved = 0;
  12038. datap = drp->data;
  12039. m_copydata(m, iphlen, len, (caddr_t)datap);
  12040. TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
  12041. asoc->ctrl_queue_cnt++;
  12042. }
  12043. void
  12044. sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
  12045. {
  12046. struct sctp_association *asoc;
  12047. struct sctp_cwr_chunk *cwr;
  12048. struct sctp_tmit_chunk *chk;
  12049. SCTP_TCB_LOCK_ASSERT(stcb);
  12050. if (net == NULL) {
  12051. return;
  12052. }
  12053. asoc = &stcb->asoc;
  12054. TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
  12055. if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
  12056. /* found a previous CWR queued to same destination update it if needed */
  12057. uint32_t ctsn;
  12058. cwr = mtod(chk->data, struct sctp_cwr_chunk *);
  12059. ctsn = ntohl(cwr->tsn);
  12060. if (SCTP_TSN_GT(high_tsn, ctsn)) {
  12061. cwr->tsn = htonl(high_tsn);
  12062. }
  12063. if (override & SCTP_CWR_REDUCE_OVERRIDE) {
  12064. /* Make sure override is carried */
  12065. cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
  12066. }
  12067. return;
  12068. }
  12069. }
  12070. sctp_alloc_a_chunk(stcb, chk);
  12071. if (chk == NULL) {
  12072. return;
  12073. }
  12074. chk->copy_by_ref = 0;
  12075. chk->rec.chunk_id.id = SCTP_ECN_CWR;
  12076. chk->rec.chunk_id.can_take_data = 1;
  12077. chk->flags = 0;
  12078. chk->asoc = asoc;
  12079. chk->send_size = sizeof(struct sctp_cwr_chunk);
  12080. chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
  12081. if (chk->data == NULL) {
  12082. sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
  12083. return;
  12084. }
  12085. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  12086. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12087. chk->sent = SCTP_DATAGRAM_UNSENT;
  12088. chk->snd_count = 0;
  12089. chk->whoTo = net;
  12090. atomic_add_int(&chk->whoTo->ref_count, 1);
  12091. cwr = mtod(chk->data, struct sctp_cwr_chunk *);
  12092. cwr->ch.chunk_type = SCTP_ECN_CWR;
  12093. cwr->ch.chunk_flags = override;
  12094. cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
  12095. cwr->tsn = htonl(high_tsn);
  12096. TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
  12097. asoc->ctrl_queue_cnt++;
  12098. }
  12099. static int
  12100. sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
  12101. uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
  12102. {
  12103. uint16_t len, old_len, i;
  12104. struct sctp_stream_reset_out_request *req_out;
  12105. struct sctp_chunkhdr *ch;
  12106. int at;
  12107. int number_entries=0;
  12108. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12109. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12110. /* get to new offset for the param. */
  12111. req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
  12112. /* now how long will this param be? */
  12113. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  12114. if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
  12115. (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
  12116. TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
  12117. number_entries++;
  12118. }
  12119. }
  12120. if (number_entries == 0) {
  12121. return (0);
  12122. }
  12123. if (number_entries == stcb->asoc.streamoutcnt) {
  12124. number_entries = 0;
  12125. }
  12126. if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
  12127. number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
  12128. }
  12129. len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
  12130. req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
  12131. req_out->ph.param_length = htons(len);
  12132. req_out->request_seq = htonl(seq);
  12133. req_out->response_seq = htonl(resp_seq);
  12134. req_out->send_reset_at_tsn = htonl(last_sent);
  12135. at = 0;
  12136. if (number_entries) {
  12137. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  12138. if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
  12139. (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
  12140. TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
  12141. req_out->list_of_streams[at] = htons(i);
  12142. at++;
  12143. stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
  12144. if (at >= number_entries) {
  12145. break;
  12146. }
  12147. }
  12148. }
  12149. } else {
  12150. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  12151. stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
  12152. }
  12153. }
  12154. if (SCTP_SIZE32(len) > len) {
  12155. /*-
  12156. * Need to worry about the pad we may end up adding to the
  12157. * end. This is easy since the struct is either aligned to 4
  12158. * bytes or 2 bytes off.
  12159. */
  12160. req_out->list_of_streams[number_entries] = 0;
  12161. }
  12162. /* now fix the chunk length */
  12163. ch->chunk_length = htons(len + old_len);
  12164. chk->book_size = len + old_len;
  12165. chk->book_size_scale = 0;
  12166. chk->send_size = SCTP_SIZE32(chk->book_size);
  12167. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12168. return (1);
  12169. }
  12170. static void
  12171. sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
  12172. int number_entries, uint16_t *list,
  12173. uint32_t seq)
  12174. {
  12175. uint16_t len, old_len, i;
  12176. struct sctp_stream_reset_in_request *req_in;
  12177. struct sctp_chunkhdr *ch;
  12178. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12179. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12180. /* get to new offset for the param. */
  12181. req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
  12182. /* now how long will this param be? */
  12183. len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
  12184. req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
  12185. req_in->ph.param_length = htons(len);
  12186. req_in->request_seq = htonl(seq);
  12187. if (number_entries) {
  12188. for (i = 0; i < number_entries; i++) {
  12189. req_in->list_of_streams[i] = htons(list[i]);
  12190. }
  12191. }
  12192. if (SCTP_SIZE32(len) > len) {
  12193. /*-
  12194. * Need to worry about the pad we may end up adding to the
  12195. * end. This is easy since the struct is either aligned to 4
  12196. * bytes or 2 bytes off.
  12197. */
  12198. req_in->list_of_streams[number_entries] = 0;
  12199. }
  12200. /* now fix the chunk length */
  12201. ch->chunk_length = htons(len + old_len);
  12202. chk->book_size = len + old_len;
  12203. chk->book_size_scale = 0;
  12204. chk->send_size = SCTP_SIZE32(chk->book_size);
  12205. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12206. return;
  12207. }
  12208. static void
  12209. sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
  12210. uint32_t seq)
  12211. {
  12212. uint16_t len, old_len;
  12213. struct sctp_stream_reset_tsn_request *req_tsn;
  12214. struct sctp_chunkhdr *ch;
  12215. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12216. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12217. /* get to new offset for the param. */
  12218. req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
  12219. /* now how long will this param be? */
  12220. len = sizeof(struct sctp_stream_reset_tsn_request);
  12221. req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
  12222. req_tsn->ph.param_length = htons(len);
  12223. req_tsn->request_seq = htonl(seq);
  12224. /* now fix the chunk length */
  12225. ch->chunk_length = htons(len + old_len);
  12226. chk->send_size = len + old_len;
  12227. chk->book_size = SCTP_SIZE32(chk->send_size);
  12228. chk->book_size_scale = 0;
  12229. SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
  12230. return;
  12231. }
  12232. void
  12233. sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
  12234. uint32_t resp_seq, uint32_t result)
  12235. {
  12236. uint16_t len, old_len;
  12237. struct sctp_stream_reset_response *resp;
  12238. struct sctp_chunkhdr *ch;
  12239. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12240. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12241. /* get to new offset for the param. */
  12242. resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
  12243. /* now how long will this param be? */
  12244. len = sizeof(struct sctp_stream_reset_response);
  12245. resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
  12246. resp->ph.param_length = htons(len);
  12247. resp->response_seq = htonl(resp_seq);
  12248. resp->result = ntohl(result);
  12249. /* now fix the chunk length */
  12250. ch->chunk_length = htons(len + old_len);
  12251. chk->book_size = len + old_len;
  12252. chk->book_size_scale = 0;
  12253. chk->send_size = SCTP_SIZE32(chk->book_size);
  12254. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12255. return;
  12256. }
  12257. void
  12258. sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
  12259. struct sctp_stream_reset_list *ent,
  12260. int response)
  12261. {
  12262. struct sctp_association *asoc;
  12263. struct sctp_tmit_chunk *chk;
  12264. struct sctp_chunkhdr *ch;
  12265. asoc = &stcb->asoc;
  12266. /*
  12267. * Reset our last reset action to the new one IP -> response
  12268. * (PERFORMED probably). This assures that if we fail to send, a
  12269. * retran from the peer will get the new response.
  12270. */
  12271. asoc->last_reset_action[0] = response;
  12272. if (asoc->stream_reset_outstanding) {
  12273. return;
  12274. }
  12275. sctp_alloc_a_chunk(stcb, chk);
  12276. if (chk == NULL) {
  12277. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12278. return;
  12279. }
  12280. chk->copy_by_ref = 0;
  12281. chk->rec.chunk_id.id = SCTP_STREAM_RESET;
  12282. chk->rec.chunk_id.can_take_data = 0;
  12283. chk->flags = 0;
  12284. chk->asoc = &stcb->asoc;
  12285. chk->book_size = sizeof(struct sctp_chunkhdr);
  12286. chk->send_size = SCTP_SIZE32(chk->book_size);
  12287. chk->book_size_scale = 0;
  12288. chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  12289. if (chk->data == NULL) {
  12290. sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
  12291. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12292. return;
  12293. }
  12294. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  12295. /* setup chunk parameters */
  12296. chk->sent = SCTP_DATAGRAM_UNSENT;
  12297. chk->snd_count = 0;
  12298. if (stcb->asoc.alternate) {
  12299. chk->whoTo = stcb->asoc.alternate;
  12300. } else {
  12301. chk->whoTo = stcb->asoc.primary_destination;
  12302. }
  12303. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12304. ch->chunk_type = SCTP_STREAM_RESET;
  12305. ch->chunk_flags = 0;
  12306. ch->chunk_length = htons(chk->book_size);
  12307. atomic_add_int(&chk->whoTo->ref_count, 1);
  12308. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12309. sctp_add_stream_reset_result(chk, ent->seq, response);
  12310. /* insert the chunk for sending */
  12311. TAILQ_INSERT_TAIL(&asoc->control_send_queue,
  12312. chk,
  12313. sctp_next);
  12314. asoc->ctrl_queue_cnt++;
  12315. }
  12316. void
  12317. sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
  12318. uint32_t resp_seq, uint32_t result,
  12319. uint32_t send_una, uint32_t recv_next)
  12320. {
  12321. uint16_t len, old_len;
  12322. struct sctp_stream_reset_response_tsn *resp;
  12323. struct sctp_chunkhdr *ch;
  12324. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12325. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12326. /* get to new offset for the param. */
  12327. resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
  12328. /* now how long will this param be? */
  12329. len = sizeof(struct sctp_stream_reset_response_tsn);
  12330. resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
  12331. resp->ph.param_length = htons(len);
  12332. resp->response_seq = htonl(resp_seq);
  12333. resp->result = htonl(result);
  12334. resp->senders_next_tsn = htonl(send_una);
  12335. resp->receivers_next_tsn = htonl(recv_next);
  12336. /* now fix the chunk length */
  12337. ch->chunk_length = htons(len + old_len);
  12338. chk->book_size = len + old_len;
  12339. chk->send_size = SCTP_SIZE32(chk->book_size);
  12340. chk->book_size_scale = 0;
  12341. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12342. return;
  12343. }
  12344. static void
  12345. sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
  12346. uint32_t seq,
  12347. uint16_t adding)
  12348. {
  12349. uint16_t len, old_len;
  12350. struct sctp_chunkhdr *ch;
  12351. struct sctp_stream_reset_add_strm *addstr;
  12352. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12353. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12354. /* get to new offset for the param. */
  12355. addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
  12356. /* now how long will this param be? */
  12357. len = sizeof(struct sctp_stream_reset_add_strm);
  12358. /* Fill it out. */
  12359. addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
  12360. addstr->ph.param_length = htons(len);
  12361. addstr->request_seq = htonl(seq);
  12362. addstr->number_of_streams = htons(adding);
  12363. addstr->reserved = 0;
  12364. /* now fix the chunk length */
  12365. ch->chunk_length = htons(len + old_len);
  12366. chk->send_size = len + old_len;
  12367. chk->book_size = SCTP_SIZE32(chk->send_size);
  12368. chk->book_size_scale = 0;
  12369. SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
  12370. return;
  12371. }
  12372. static void
  12373. sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
  12374. uint32_t seq,
  12375. uint16_t adding)
  12376. {
  12377. uint16_t len, old_len;
  12378. struct sctp_chunkhdr *ch;
  12379. struct sctp_stream_reset_add_strm *addstr;
  12380. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12381. old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
  12382. /* get to new offset for the param. */
  12383. addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
  12384. /* now how long will this param be? */
  12385. len = sizeof(struct sctp_stream_reset_add_strm);
  12386. /* Fill it out. */
  12387. addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
  12388. addstr->ph.param_length = htons(len);
  12389. addstr->request_seq = htonl(seq);
  12390. addstr->number_of_streams = htons(adding);
  12391. addstr->reserved = 0;
  12392. /* now fix the chunk length */
  12393. ch->chunk_length = htons(len + old_len);
  12394. chk->send_size = len + old_len;
  12395. chk->book_size = SCTP_SIZE32(chk->send_size);
  12396. chk->book_size_scale = 0;
  12397. SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
  12398. return;
  12399. }
  12400. int
  12401. sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
  12402. {
  12403. struct sctp_association *asoc;
  12404. struct sctp_tmit_chunk *chk;
  12405. struct sctp_chunkhdr *ch;
  12406. uint32_t seq;
  12407. asoc = &stcb->asoc;
  12408. asoc->trigger_reset = 0;
  12409. if (asoc->stream_reset_outstanding) {
  12410. return (EALREADY);
  12411. }
  12412. sctp_alloc_a_chunk(stcb, chk);
  12413. if (chk == NULL) {
  12414. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12415. return (ENOMEM);
  12416. }
  12417. chk->copy_by_ref = 0;
  12418. chk->rec.chunk_id.id = SCTP_STREAM_RESET;
  12419. chk->rec.chunk_id.can_take_data = 0;
  12420. chk->flags = 0;
  12421. chk->asoc = &stcb->asoc;
  12422. chk->book_size = sizeof(struct sctp_chunkhdr);
  12423. chk->send_size = SCTP_SIZE32(chk->book_size);
  12424. chk->book_size_scale = 0;
  12425. chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  12426. if (chk->data == NULL) {
  12427. sctp_free_a_chunk(stcb, chk, so_locked);
  12428. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12429. return (ENOMEM);
  12430. }
  12431. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  12432. /* setup chunk parameters */
  12433. chk->sent = SCTP_DATAGRAM_UNSENT;
  12434. chk->snd_count = 0;
  12435. if (stcb->asoc.alternate) {
  12436. chk->whoTo = stcb->asoc.alternate;
  12437. } else {
  12438. chk->whoTo = stcb->asoc.primary_destination;
  12439. }
  12440. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12441. ch->chunk_type = SCTP_STREAM_RESET;
  12442. ch->chunk_flags = 0;
  12443. ch->chunk_length = htons(chk->book_size);
  12444. atomic_add_int(&chk->whoTo->ref_count, 1);
  12445. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12446. seq = stcb->asoc.str_reset_seq_out;
  12447. if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
  12448. seq++;
  12449. asoc->stream_reset_outstanding++;
  12450. } else {
  12451. m_freem(chk->data);
  12452. chk->data = NULL;
  12453. sctp_free_a_chunk(stcb, chk, so_locked);
  12454. return (ENOENT);
  12455. }
  12456. asoc->str_reset = chk;
  12457. /* insert the chunk for sending */
  12458. TAILQ_INSERT_TAIL(&asoc->control_send_queue,
  12459. chk,
  12460. sctp_next);
  12461. asoc->ctrl_queue_cnt++;
  12462. if (stcb->asoc.send_sack) {
  12463. sctp_send_sack(stcb, so_locked);
  12464. }
  12465. sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
  12466. return (0);
  12467. }
  12468. int
  12469. sctp_send_str_reset_req(struct sctp_tcb *stcb,
  12470. uint16_t number_entries, uint16_t *list,
  12471. uint8_t send_in_req,
  12472. uint8_t send_tsn_req,
  12473. uint8_t add_stream,
  12474. uint16_t adding_o,
  12475. uint16_t adding_i, uint8_t peer_asked)
  12476. {
  12477. struct sctp_association *asoc;
  12478. struct sctp_tmit_chunk *chk;
  12479. struct sctp_chunkhdr *ch;
  12480. int can_send_out_req=0;
  12481. uint32_t seq;
  12482. SCTP_TCB_LOCK_ASSERT(stcb);
  12483. asoc = &stcb->asoc;
  12484. if (asoc->stream_reset_outstanding) {
  12485. /*-
  12486. * Already one pending, must get ACK back to clear the flag.
  12487. */
  12488. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
  12489. return (EBUSY);
  12490. }
  12491. if ((send_in_req == 0) && (send_tsn_req == 0) &&
  12492. (add_stream == 0)) {
  12493. /* nothing to do */
  12494. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  12495. return (EINVAL);
  12496. }
  12497. if (send_tsn_req && send_in_req) {
  12498. /* error, can't do that */
  12499. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  12500. return (EINVAL);
  12501. } else if (send_in_req) {
  12502. can_send_out_req = 1;
  12503. }
  12504. if (number_entries > (MCLBYTES -
  12505. SCTP_MIN_OVERHEAD -
  12506. sizeof(struct sctp_chunkhdr) -
  12507. sizeof(struct sctp_stream_reset_out_request)) /
  12508. sizeof(uint16_t)) {
  12509. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12510. return (ENOMEM);
  12511. }
  12512. sctp_alloc_a_chunk(stcb, chk);
  12513. if (chk == NULL) {
  12514. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12515. return (ENOMEM);
  12516. }
  12517. chk->copy_by_ref = 0;
  12518. chk->rec.chunk_id.id = SCTP_STREAM_RESET;
  12519. chk->rec.chunk_id.can_take_data = 0;
  12520. chk->flags = 0;
  12521. chk->asoc = &stcb->asoc;
  12522. chk->book_size = sizeof(struct sctp_chunkhdr);
  12523. chk->send_size = SCTP_SIZE32(chk->book_size);
  12524. chk->book_size_scale = 0;
  12525. chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
  12526. if (chk->data == NULL) {
  12527. sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
  12528. SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12529. return (ENOMEM);
  12530. }
  12531. SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
  12532. /* setup chunk parameters */
  12533. chk->sent = SCTP_DATAGRAM_UNSENT;
  12534. chk->snd_count = 0;
  12535. if (stcb->asoc.alternate) {
  12536. chk->whoTo = stcb->asoc.alternate;
  12537. } else {
  12538. chk->whoTo = stcb->asoc.primary_destination;
  12539. }
  12540. atomic_add_int(&chk->whoTo->ref_count, 1);
  12541. ch = mtod(chk->data, struct sctp_chunkhdr *);
  12542. ch->chunk_type = SCTP_STREAM_RESET;
  12543. ch->chunk_flags = 0;
  12544. ch->chunk_length = htons(chk->book_size);
  12545. SCTP_BUF_LEN(chk->data) = chk->send_size;
  12546. seq = stcb->asoc.str_reset_seq_out;
  12547. if (can_send_out_req) {
  12548. int ret;
  12549. ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
  12550. if (ret) {
  12551. seq++;
  12552. asoc->stream_reset_outstanding++;
  12553. }
  12554. }
  12555. if ((add_stream & 1) &&
  12556. ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
  12557. /* Need to allocate more */
  12558. struct sctp_stream_out *oldstream;
  12559. struct sctp_stream_queue_pending *sp, *nsp;
  12560. int i;
  12561. #if defined(SCTP_DETAILED_STR_STATS)
  12562. int j;
  12563. #endif
  12564. oldstream = stcb->asoc.strmout;
  12565. /* get some more */
  12566. SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
  12567. (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
  12568. SCTP_M_STRMO);
  12569. if (stcb->asoc.strmout == NULL) {
  12570. uint8_t x;
  12571. stcb->asoc.strmout = oldstream;
  12572. /* Turn off the bit */
  12573. x = add_stream & 0xfe;
  12574. add_stream = x;
  12575. goto skip_stuff;
  12576. }
  12577. /* Ok now we proceed with copying the old out stuff and
  12578. * initializing the new stuff.
  12579. */
  12580. stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
  12581. for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
  12582. TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
  12583. /* FIX ME FIX ME */
  12584. /* This should be a SS_COPY operation FIX ME STREAM SCHEDULER EXPERT */
  12585. stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
  12586. stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
  12587. #if defined(SCTP_DETAILED_STR_STATS)
  12588. for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
  12589. stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
  12590. stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
  12591. }
  12592. #else
  12593. stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
  12594. stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
  12595. #endif
  12596. stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
  12597. stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
  12598. stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
  12599. stcb->asoc.strmout[i].sid = i;
  12600. stcb->asoc.strmout[i].state = oldstream[i].state;
  12601. /* now anything on those queues? */
  12602. TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
  12603. TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
  12604. TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
  12605. }
  12606. }
  12607. /* now the new streams */
  12608. stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
  12609. for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
  12610. TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
  12611. stcb->asoc.strmout[i].chunks_on_queues = 0;
  12612. #if defined(SCTP_DETAILED_STR_STATS)
  12613. for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
  12614. stcb->asoc.strmout[i].abandoned_sent[j] = 0;
  12615. stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
  12616. }
  12617. #else
  12618. stcb->asoc.strmout[i].abandoned_sent[0] = 0;
  12619. stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
  12620. #endif
  12621. stcb->asoc.strmout[i].next_mid_ordered = 0;
  12622. stcb->asoc.strmout[i].next_mid_unordered = 0;
  12623. stcb->asoc.strmout[i].sid = i;
  12624. stcb->asoc.strmout[i].last_msg_incomplete = 0;
  12625. stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
  12626. stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
  12627. }
  12628. stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
  12629. SCTP_FREE(oldstream, SCTP_M_STRMO);
  12630. }
  12631. skip_stuff:
  12632. if ((add_stream & 1) && (adding_o > 0)) {
  12633. asoc->strm_pending_add_size = adding_o;
  12634. asoc->peer_req_out = peer_asked;
  12635. sctp_add_an_out_stream(chk, seq, adding_o);
  12636. seq++;
  12637. asoc->stream_reset_outstanding++;
  12638. }
  12639. if ((add_stream & 2) && (adding_i > 0)) {
  12640. sctp_add_an_in_stream(chk, seq, adding_i);
  12641. seq++;
  12642. asoc->stream_reset_outstanding++;
  12643. }
  12644. if (send_in_req) {
  12645. sctp_add_stream_reset_in(chk, number_entries, list, seq);
  12646. seq++;
  12647. asoc->stream_reset_outstanding++;
  12648. }
  12649. if (send_tsn_req) {
  12650. sctp_add_stream_reset_tsn(chk, seq);
  12651. asoc->stream_reset_outstanding++;
  12652. }
  12653. asoc->str_reset = chk;
  12654. /* insert the chunk for sending */
  12655. TAILQ_INSERT_TAIL(&asoc->control_send_queue,
  12656. chk,
  12657. sctp_next);
  12658. asoc->ctrl_queue_cnt++;
  12659. if (stcb->asoc.send_sack) {
  12660. sctp_send_sack(stcb, SCTP_SO_LOCKED);
  12661. }
  12662. sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
  12663. return (0);
  12664. }
  12665. void
  12666. sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
  12667. struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
  12668. #if defined(__FreeBSD__) && !defined(__Userspace__)
  12669. uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
  12670. #endif
  12671. uint32_t vrf_id, uint16_t port)
  12672. {
  12673. /* Don't respond to an ABORT with an ABORT. */
  12674. if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
  12675. if (cause)
  12676. sctp_m_freem(cause);
  12677. return;
  12678. }
  12679. sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
  12680. #if defined(__FreeBSD__) && !defined(__Userspace__)
  12681. mflowtype, mflowid, fibnum,
  12682. #endif
  12683. vrf_id, port);
  12684. return;
  12685. }
  12686. void
  12687. sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
  12688. struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
  12689. #if defined(__FreeBSD__) && !defined(__Userspace__)
  12690. uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
  12691. #endif
  12692. uint32_t vrf_id, uint16_t port)
  12693. {
  12694. sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
  12695. #if defined(__FreeBSD__) && !defined(__Userspace__)
  12696. mflowtype, mflowid, fibnum,
  12697. #endif
  12698. vrf_id, port);
  12699. return;
  12700. }
  12701. static struct mbuf *
  12702. sctp_copy_resume(struct uio *uio,
  12703. int max_send_len,
  12704. #if defined(__FreeBSD__) || defined(__Userspace__)
  12705. int user_marks_eor,
  12706. #endif
  12707. int *error,
  12708. uint32_t *sndout,
  12709. struct mbuf **new_tail)
  12710. {
  12711. #if defined(__FreeBSD__) || defined(__Userspace__)
  12712. struct mbuf *m;
  12713. m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
  12714. (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
  12715. if (m == NULL) {
  12716. /* The only possible error is EFAULT. */
  12717. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
  12718. *error = EFAULT;
  12719. } else {
  12720. *sndout = m_length(m, NULL);
  12721. *new_tail = m_last(m);
  12722. }
  12723. return (m);
  12724. #else
  12725. int left, cancpy, willcpy;
  12726. struct mbuf *m, *head;
  12727. #if defined(__APPLE__) && !defined(__Userspace__)
  12728. #if defined(APPLE_LEOPARD)
  12729. left = (int)min(uio->uio_resid, max_send_len);
  12730. #else
  12731. left = (int)min(uio_resid(uio), max_send_len);
  12732. #endif
  12733. #else
  12734. left = (int)min(uio->uio_resid, max_send_len);
  12735. #endif
  12736. /* Always get a header just in case */
  12737. head = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA);
  12738. if (head == NULL) {
  12739. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
  12740. *error = ENOBUFS;
  12741. return (NULL);
  12742. }
  12743. cancpy = (int)M_TRAILINGSPACE(head);
  12744. willcpy = min(cancpy, left);
  12745. *error = uiomove(mtod(head, caddr_t), willcpy, uio);
  12746. if (*error != 0) {
  12747. sctp_m_freem(head);
  12748. return (NULL);
  12749. }
  12750. *sndout += willcpy;
  12751. left -= willcpy;
  12752. SCTP_BUF_LEN(head) = willcpy;
  12753. m = head;
  12754. *new_tail = head;
  12755. while (left > 0) {
  12756. /* move in user data */
  12757. SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA);
  12758. if (SCTP_BUF_NEXT(m) == NULL) {
  12759. sctp_m_freem(head);
  12760. *new_tail = NULL;
  12761. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
  12762. *error = ENOBUFS;
  12763. return (NULL);
  12764. }
  12765. m = SCTP_BUF_NEXT(m);
  12766. cancpy = (int)M_TRAILINGSPACE(m);
  12767. willcpy = min(cancpy, left);
  12768. *error = uiomove(mtod(m, caddr_t), willcpy, uio);
  12769. if (*error != 0) {
  12770. sctp_m_freem(head);
  12771. *new_tail = NULL;
  12772. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, *error);
  12773. return (NULL);
  12774. }
  12775. SCTP_BUF_LEN(m) = willcpy;
  12776. left -= willcpy;
  12777. *sndout += willcpy;
  12778. *new_tail = m;
  12779. if (left == 0) {
  12780. SCTP_BUF_NEXT(m) = NULL;
  12781. }
  12782. }
  12783. return (head);
  12784. #endif
  12785. }
  12786. static int
  12787. sctp_copy_one(struct sctp_stream_queue_pending *sp,
  12788. struct uio *uio,
  12789. int resv_upfront)
  12790. {
  12791. #if defined(__FreeBSD__) || defined(__Userspace__)
  12792. sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
  12793. if (sp->data == NULL) {
  12794. /* The only possible error is EFAULT. */
  12795. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
  12796. return (EFAULT);
  12797. }
  12798. sp->tail_mbuf = m_last(sp->data);
  12799. return (0);
  12800. #else
  12801. int left;
  12802. int cancpy, willcpy, error;
  12803. struct mbuf *m, *head;
  12804. int cpsz = 0;
  12805. /* First one gets a header */
  12806. left = sp->length;
  12807. head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 0, M_WAITOK, 0, MT_DATA);
  12808. if (m == NULL) {
  12809. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
  12810. return (ENOBUFS);
  12811. }
  12812. /*-
  12813. * Add this one for m in now, that way if the alloc fails we won't
  12814. * have a bad cnt.
  12815. */
  12816. SCTP_BUF_RESV_UF(m, resv_upfront);
  12817. cancpy = (int)M_TRAILINGSPACE(m);
  12818. willcpy = min(cancpy, left);
  12819. while (left > 0) {
  12820. /* move in user data */
  12821. error = uiomove(mtod(m, caddr_t), willcpy, uio);
  12822. if (error) {
  12823. sctp_m_freem(head);
  12824. return (error);
  12825. }
  12826. SCTP_BUF_LEN(m) = willcpy;
  12827. left -= willcpy;
  12828. cpsz += willcpy;
  12829. if (left > 0) {
  12830. SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA);
  12831. if (SCTP_BUF_NEXT(m) == NULL) {
  12832. /*
  12833. * the head goes back to caller, he can free
  12834. * the rest
  12835. */
  12836. sctp_m_freem(head);
  12837. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
  12838. return (ENOBUFS);
  12839. }
  12840. m = SCTP_BUF_NEXT(m);
  12841. cancpy = (int)M_TRAILINGSPACE(m);
  12842. willcpy = min(cancpy, left);
  12843. } else {
  12844. sp->tail_mbuf = m;
  12845. SCTP_BUF_NEXT(m) = NULL;
  12846. }
  12847. }
  12848. sp->data = head;
  12849. sp->length = cpsz;
  12850. return (0);
  12851. #endif
  12852. }
  12853. static struct sctp_stream_queue_pending *
  12854. sctp_copy_it_in(struct sctp_tcb *stcb,
  12855. struct sctp_association *asoc,
  12856. struct sctp_nonpad_sndrcvinfo *srcv,
  12857. struct uio *uio,
  12858. struct sctp_nets *net,
  12859. ssize_t max_send_len,
  12860. int user_marks_eor,
  12861. int *error)
  12862. {
  12863. /*-
  12864. * This routine must be very careful in its work. Protocol
  12865. * processing is up and running so care must be taken to spl...()
  12866. * when you need to do something that may effect the stcb/asoc. The
  12867. * sb is locked however. When data is copied the protocol processing
  12868. * should be enabled since this is a slower operation...
  12869. */
  12870. struct sctp_stream_queue_pending *sp;
  12871. int resv_in_first;
  12872. *error = 0;
  12873. sctp_alloc_a_strmoq(stcb, sp);
  12874. if (sp == NULL) {
  12875. SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
  12876. *error = ENOMEM;
  12877. goto out_now;
  12878. }
  12879. sp->act_flags = 0;
  12880. sp->sender_all_done = 0;
  12881. sp->sinfo_flags = srcv->sinfo_flags;
  12882. sp->timetolive = srcv->sinfo_timetolive;
  12883. sp->ppid = srcv->sinfo_ppid;
  12884. sp->context = srcv->sinfo_context;
  12885. sp->fsn = 0;
  12886. (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
  12887. sp->sid = srcv->sinfo_stream;
  12888. #if defined(__APPLE__) && !defined(__Userspace__)
  12889. #if defined(APPLE_LEOPARD)
  12890. sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
  12891. #else
  12892. sp->length = (uint32_t)min(uio_resid(uio), max_send_len);
  12893. #endif
  12894. #else
  12895. sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
  12896. #endif
  12897. #if defined(__APPLE__) && !defined(__Userspace__)
  12898. #if defined(APPLE_LEOPARD)
  12899. if ((sp->length == (uint32_t)uio->uio_resid) &&
  12900. #else
  12901. if ((sp->length == (uint32_t)uio_resid(uio)) &&
  12902. #endif
  12903. #else
  12904. if ((sp->length == (uint32_t)uio->uio_resid) &&
  12905. #endif
  12906. ((user_marks_eor == 0) ||
  12907. (srcv->sinfo_flags & SCTP_EOF) ||
  12908. (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
  12909. sp->msg_is_complete = 1;
  12910. } else {
  12911. sp->msg_is_complete = 0;
  12912. }
  12913. sp->sender_all_done = 0;
  12914. sp->some_taken = 0;
  12915. sp->put_last_out = 0;
  12916. resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
  12917. sp->data = sp->tail_mbuf = NULL;
  12918. if (sp->length == 0) {
  12919. goto skip_copy;
  12920. }
  12921. if (srcv->sinfo_keynumber_valid) {
  12922. sp->auth_keyid = srcv->sinfo_keynumber;
  12923. } else {
  12924. sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
  12925. }
  12926. if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
  12927. sctp_auth_key_acquire(stcb, sp->auth_keyid);
  12928. sp->holds_key_ref = 1;
  12929. }
  12930. #if defined(__APPLE__) && !defined(__Userspace__)
  12931. SCTP_SOCKET_UNLOCK(SCTP_INP_SO(stcb->sctp_ep), 0);
  12932. #endif
  12933. *error = sctp_copy_one(sp, uio, resv_in_first);
  12934. #if defined(__APPLE__) && !defined(__Userspace__)
  12935. SCTP_SOCKET_LOCK(SCTP_INP_SO(stcb->sctp_ep), 0);
  12936. #endif
  12937. skip_copy:
  12938. if (*error) {
  12939. #if defined(__Userspace__)
  12940. SCTP_TCB_LOCK(stcb);
  12941. #endif
  12942. sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
  12943. #if defined(__Userspace__)
  12944. SCTP_TCB_UNLOCK(stcb);
  12945. #endif
  12946. sp = NULL;
  12947. } else {
  12948. if (sp->sinfo_flags & SCTP_ADDR_OVER) {
  12949. sp->net = net;
  12950. atomic_add_int(&sp->net->ref_count, 1);
  12951. } else {
  12952. sp->net = NULL;
  12953. }
  12954. sctp_set_prsctp_policy(sp);
  12955. }
  12956. out_now:
  12957. return (sp);
  12958. }
  12959. int
  12960. sctp_sosend(struct socket *so,
  12961. struct sockaddr *addr,
  12962. struct uio *uio,
  12963. struct mbuf *top,
  12964. struct mbuf *control,
  12965. #if defined(__APPLE__) && !defined(__Userspace__)
  12966. int flags)
  12967. #else
  12968. int flags,
  12969. #if defined(__FreeBSD__) && !defined(__Userspace__)
  12970. struct thread *p)
  12971. #elif defined(_WIN32) && !defined(__Userspace__)
  12972. PKTHREAD p)
  12973. #else
  12974. #if defined(__Userspace__)
  12975. /*
  12976. * proc is a dummy in __Userspace__ and will not be passed
  12977. * to sctp_lower_sosend
  12978. */
  12979. #endif
  12980. struct proc *p)
  12981. #endif
  12982. #endif
  12983. {
  12984. struct sctp_sndrcvinfo sndrcvninfo;
  12985. #if defined(INET) && defined(INET6)
  12986. struct sockaddr_in sin;
  12987. #endif
  12988. struct sockaddr *addr_to_use;
  12989. #if defined(__APPLE__) && !defined(__Userspace__)
  12990. struct proc *p = current_proc();
  12991. #endif
  12992. int error;
  12993. bool use_sndinfo;
  12994. if (control != NULL) {
  12995. /* process cmsg snd/rcv info (maybe a assoc-id) */
  12996. use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
  12997. } else {
  12998. use_sndinfo = false;
  12999. }
  13000. #if defined(INET) && defined(INET6)
  13001. if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
  13002. struct sockaddr_in6 *sin6;
  13003. #ifdef HAVE_SA_LEN
  13004. if (addr->sa_len != sizeof(struct sockaddr_in6)) {
  13005. SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
  13006. return (EINVAL);
  13007. }
  13008. #endif
  13009. sin6 = (struct sockaddr_in6 *)addr;
  13010. if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  13011. in6_sin6_2_sin(&sin, sin6);
  13012. addr_to_use = (struct sockaddr *)&sin;
  13013. } else {
  13014. addr_to_use = addr;
  13015. }
  13016. } else {
  13017. addr_to_use = addr;
  13018. }
  13019. #else
  13020. addr_to_use = addr;
  13021. #endif
  13022. #if defined(__APPLE__) && !defined(__Userspace__)
  13023. SCTP_SOCKET_LOCK(so, 1);
  13024. #endif
  13025. error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
  13026. #if defined(__Userspace__)
  13027. use_sndinfo ? &sndrcvninfo : NULL);
  13028. #else
  13029. use_sndinfo ? &sndrcvninfo : NULL, p);
  13030. #endif
  13031. #if defined(__APPLE__) && !defined(__Userspace__)
  13032. SCTP_SOCKET_UNLOCK(so, 1);
  13033. #endif
  13034. return (error);
  13035. }
  13036. int
  13037. sctp_lower_sosend(struct socket *so,
  13038. struct sockaddr *addr,
  13039. struct uio *uio,
  13040. struct mbuf *top,
  13041. struct mbuf *control,
  13042. int flags,
  13043. #if defined(__Userspace__)
  13044. struct sctp_sndrcvinfo *srcv)
  13045. #else
  13046. struct sctp_sndrcvinfo *srcv,
  13047. #if defined(__FreeBSD__)
  13048. struct thread *p)
  13049. #elif defined(_WIN32)
  13050. PKTHREAD p)
  13051. #else
  13052. struct proc *p)
  13053. #endif
  13054. #endif
  13055. {
  13056. struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
  13057. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13058. struct epoch_tracker et;
  13059. #endif
  13060. struct timeval now;
  13061. struct sctp_block_entry be;
  13062. struct sctp_inpcb *inp;
  13063. struct sctp_tcb *stcb = NULL;
  13064. struct sctp_nets *net;
  13065. struct sctp_association *asoc;
  13066. struct sctp_inpcb *t_inp;
  13067. struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
  13068. ssize_t sndlen = 0, max_len, local_add_more;
  13069. ssize_t local_soresv = 0;
  13070. sctp_assoc_t sinfo_assoc_id;
  13071. int user_marks_eor;
  13072. int nagle_applies = 0;
  13073. int error;
  13074. int queue_only = 0, queue_only_for_init = 0;
  13075. int un_sent;
  13076. int now_filled = 0;
  13077. unsigned int inqueue_bytes = 0;
  13078. uint16_t port;
  13079. uint16_t sinfo_flags;
  13080. uint16_t sinfo_stream;
  13081. bool create_lock_applied = false;
  13082. bool free_cnt_applied = false;
  13083. bool some_on_control;
  13084. bool got_all_of_the_send = false;
  13085. bool non_blocking = false;
  13086. error = 0;
  13087. net = NULL;
  13088. stcb = NULL;
  13089. #if defined(__APPLE__) && !defined(__Userspace__)
  13090. sctp_lock_assert(so);
  13091. #endif
  13092. if ((uio == NULL) && (top == NULL)) {
  13093. error = EINVAL;
  13094. goto out_unlocked;
  13095. }
  13096. if (addr != NULL) {
  13097. union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
  13098. switch (raddr->sa.sa_family) {
  13099. #ifdef INET
  13100. case AF_INET:
  13101. #ifdef HAVE_SIN_LEN
  13102. if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
  13103. error = EINVAL;
  13104. goto out_unlocked;
  13105. }
  13106. #endif
  13107. port = raddr->sin.sin_port;
  13108. break;
  13109. #endif
  13110. #ifdef INET6
  13111. case AF_INET6:
  13112. #ifdef HAVE_SIN6_LEN
  13113. if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
  13114. error = EINVAL;
  13115. goto out_unlocked;
  13116. }
  13117. #endif
  13118. port = raddr->sin6.sin6_port;
  13119. break;
  13120. #endif
  13121. #if defined(__Userspace__)
  13122. case AF_CONN:
  13123. #ifdef HAVE_SCONN_LEN
  13124. if (raddr->sconn.sconn_len != sizeof(struct sockaddr_conn)) {
  13125. error = EINVAL;
  13126. goto out_unlocked;
  13127. }
  13128. #endif
  13129. port = raddr->sconn.sconn_port;
  13130. break;
  13131. #endif
  13132. default:
  13133. error = EAFNOSUPPORT;
  13134. goto out_unlocked;
  13135. }
  13136. } else {
  13137. port = 0;
  13138. }
  13139. if (uio != NULL) {
  13140. #if defined(__APPLE__) && !defined(__Userspace__)
  13141. #if defined(APPLE_LEOPARD)
  13142. if (uio->uio_resid < 0) {
  13143. #else
  13144. if (uio_resid(uio) < 0) {
  13145. #endif
  13146. #else
  13147. if (uio->uio_resid < 0) {
  13148. #endif
  13149. error = EINVAL;
  13150. goto out_unlocked;
  13151. }
  13152. #if defined(__APPLE__) && !defined(__Userspace__)
  13153. #if defined(APPLE_LEOPARD)
  13154. sndlen = uio->uio_resid;
  13155. #else
  13156. sndlen = uio_resid(uio);
  13157. #endif
  13158. #else
  13159. sndlen = uio->uio_resid;
  13160. #endif
  13161. } else {
  13162. sndlen = SCTP_HEADER_LEN(top);
  13163. }
  13164. SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
  13165. (void *)addr, sndlen);
  13166. t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
  13167. if (inp == NULL) {
  13168. error = EINVAL;
  13169. goto out_unlocked;
  13170. }
  13171. user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
  13172. if ((uio == NULL) && (user_marks_eor != 0)) {
  13173. /*-
  13174. * We do not support eeor mode for
  13175. * sending with mbuf chains (like sendfile).
  13176. */
  13177. error = EINVAL;
  13178. goto out_unlocked;
  13179. }
  13180. if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
  13181. SCTP_IS_LISTENING(inp)) {
  13182. /* The listener can NOT send. */
  13183. error = EINVAL;
  13184. goto out_unlocked;
  13185. }
  13186. atomic_add_int(&inp->total_sends, 1);
  13187. if (srcv != NULL) {
  13188. sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
  13189. sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
  13190. sinfo_flags = sndrcvninfo->sinfo_flags;
  13191. if (INVALID_SINFO_FLAG(sinfo_flags) ||
  13192. PR_SCTP_INVALID_POLICY(sinfo_flags)) {
  13193. error = EINVAL;
  13194. goto out_unlocked;
  13195. }
  13196. if (sinfo_flags != 0) {
  13197. SCTP_STAT_INCR(sctps_sends_with_flags);
  13198. }
  13199. } else {
  13200. sndrcvninfo = NULL;
  13201. sinfo_flags = inp->def_send.sinfo_flags;
  13202. sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
  13203. }
  13204. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13205. if (flags & MSG_EOR) {
  13206. sinfo_flags |= SCTP_EOR;
  13207. }
  13208. if (flags & MSG_EOF) {
  13209. sinfo_flags |= SCTP_EOF;
  13210. }
  13211. #endif
  13212. if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
  13213. error = EINVAL;
  13214. goto out_unlocked;
  13215. }
  13216. SCTP_INP_RLOCK(inp);
  13217. if ((sinfo_flags & SCTP_SENDALL) &&
  13218. (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
  13219. SCTP_INP_RUNLOCK(inp);
  13220. error = sctp_sendall(inp, uio, top, sndrcvninfo);
  13221. top = NULL;
  13222. goto out_unlocked;
  13223. }
  13224. /* Now we must find the association. */
  13225. if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
  13226. (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
  13227. stcb = LIST_FIRST(&inp->sctp_asoc_list);
  13228. if (stcb != NULL) {
  13229. SCTP_TCB_LOCK(stcb);
  13230. }
  13231. SCTP_INP_RUNLOCK(inp);
  13232. } else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
  13233. stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
  13234. SCTP_INP_RUNLOCK(inp);
  13235. if (stcb != NULL) {
  13236. SCTP_TCB_LOCK_ASSERT(stcb);
  13237. }
  13238. } else if (addr != NULL) {
  13239. /*-
  13240. * Since we did not use findep we must
  13241. * increment it, and if we don't find a tcb
  13242. * decrement it.
  13243. */
  13244. SCTP_INP_INCR_REF(inp);
  13245. SCTP_INP_RUNLOCK(inp);
  13246. stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
  13247. if (stcb == NULL) {
  13248. SCTP_INP_WLOCK(inp);
  13249. SCTP_INP_DECR_REF(inp);
  13250. SCTP_INP_WUNLOCK(inp);
  13251. } else {
  13252. SCTP_TCB_LOCK_ASSERT(stcb);
  13253. }
  13254. } else {
  13255. SCTP_INP_RUNLOCK(inp);
  13256. }
  13257. #ifdef INVARIANTS
  13258. if (stcb != NULL) {
  13259. SCTP_TCB_LOCK_ASSERT(stcb);
  13260. }
  13261. #endif
  13262. if ((stcb == NULL) && (addr != NULL)) {
  13263. /* Possible implicit send? */
  13264. SCTP_ASOC_CREATE_LOCK(inp);
  13265. create_lock_applied = true;
  13266. if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
  13267. (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
  13268. error = EINVAL;
  13269. goto out_unlocked;
  13270. }
  13271. if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
  13272. (addr->sa_family == AF_INET6)) {
  13273. error = EINVAL;
  13274. goto out_unlocked;
  13275. }
  13276. SCTP_INP_WLOCK(inp);
  13277. SCTP_INP_INCR_REF(inp);
  13278. SCTP_INP_WUNLOCK(inp);
  13279. /* With the lock applied look again */
  13280. stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
  13281. #if defined(INET) || defined(INET6)
  13282. if ((stcb == NULL) && (control != NULL) && (port > 0)) {
  13283. stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
  13284. }
  13285. #endif
  13286. if (stcb == NULL) {
  13287. SCTP_INP_WLOCK(inp);
  13288. SCTP_INP_DECR_REF(inp);
  13289. SCTP_INP_WUNLOCK(inp);
  13290. } else {
  13291. SCTP_TCB_LOCK_ASSERT(stcb);
  13292. SCTP_ASOC_CREATE_UNLOCK(inp);
  13293. create_lock_applied = false;
  13294. }
  13295. if (error != 0) {
  13296. goto out_unlocked;
  13297. }
  13298. if (t_inp != inp) {
  13299. error = ENOTCONN;
  13300. goto out_unlocked;
  13301. }
  13302. }
  13303. if (stcb == NULL) {
  13304. if (addr == NULL) {
  13305. error = ENOENT;
  13306. goto out_unlocked;
  13307. } else {
  13308. /* We must go ahead and start the INIT process */
  13309. uint32_t vrf_id;
  13310. if ((sinfo_flags & SCTP_ABORT) ||
  13311. ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
  13312. /*-
  13313. * User asks to abort a non-existent assoc,
  13314. * or EOF a non-existent assoc with no data
  13315. */
  13316. error = ENOENT;
  13317. goto out_unlocked;
  13318. }
  13319. /* get an asoc/stcb struct */
  13320. vrf_id = inp->def_vrf_id;
  13321. KASSERT(create_lock_applied, ("create_lock_applied is false"));
  13322. stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
  13323. inp->sctp_ep.pre_open_stream_count,
  13324. inp->sctp_ep.port,
  13325. #if !defined(__Userspace__)
  13326. p,
  13327. #else
  13328. (struct proc *)NULL,
  13329. #endif
  13330. SCTP_INITIALIZE_AUTH_PARAMS);
  13331. if (stcb == NULL) {
  13332. /* error is setup for us in the call. */
  13333. KASSERT(error != 0, ("error is 0 although stcb is NULL"));
  13334. goto out_unlocked;
  13335. }
  13336. SCTP_TCB_LOCK_ASSERT(stcb);
  13337. SCTP_ASOC_CREATE_UNLOCK(inp);
  13338. create_lock_applied = false;
  13339. /* Turn on queue only flag to prevent data from being sent */
  13340. queue_only = 1;
  13341. SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
  13342. (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
  13343. if (control != NULL) {
  13344. if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
  13345. sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
  13346. SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
  13347. stcb = NULL;
  13348. KASSERT(error != 0,
  13349. ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
  13350. goto out_unlocked;
  13351. }
  13352. }
  13353. /* out with the INIT */
  13354. queue_only_for_init = 1;
  13355. /*-
  13356. * we may want to dig in after this call and adjust the MTU
  13357. * value. It defaulted to 1500 (constant) but the ro
  13358. * structure may now have an update and thus we may need to
  13359. * change it BEFORE we append the message.
  13360. */
  13361. }
  13362. }
  13363. KASSERT(!create_lock_applied, ("create_lock_applied is true"));
  13364. KASSERT(stcb != NULL, ("stcb is NULL"));
  13365. SCTP_TCB_LOCK_ASSERT(stcb);
  13366. asoc = &stcb->asoc;
  13367. if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
  13368. (asoc->state & SCTP_STATE_WAS_ABORTED)) {
  13369. if (asoc->state & SCTP_STATE_WAS_ABORTED) {
  13370. /* XXX: Could also be ECONNABORTED, not enough info. */
  13371. error = ECONNRESET;
  13372. } else {
  13373. error = ENOTCONN;
  13374. }
  13375. goto out_unlocked;
  13376. }
  13377. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  13378. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  13379. queue_only = 1;
  13380. }
  13381. /* Keep the stcb from being freed under our feet. */
  13382. atomic_add_int(&asoc->refcnt, 1);
  13383. free_cnt_applied = true;
  13384. if (sndrcvninfo == NULL) {
  13385. /* Use a local copy to have a consistent view. */
  13386. sndrcvninfo_buf = asoc->def_send;
  13387. sndrcvninfo = &sndrcvninfo_buf;
  13388. sinfo_flags = sndrcvninfo->sinfo_flags;
  13389. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13390. if (flags & MSG_EOR) {
  13391. sinfo_flags |= SCTP_EOR;
  13392. }
  13393. if (flags & MSG_EOF) {
  13394. sinfo_flags |= SCTP_EOF;
  13395. }
  13396. #endif
  13397. }
  13398. /* Are we aborting? */
  13399. if (sinfo_flags & SCTP_ABORT) {
  13400. struct mbuf *mm;
  13401. struct sctp_paramhdr *ph;
  13402. ssize_t tot_demand, tot_out = 0, max_out;
  13403. SCTP_STAT_INCR(sctps_sends_with_abort);
  13404. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
  13405. (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
  13406. /* It has to be up before we abort. */
  13407. error = EINVAL;
  13408. goto out_unlocked;
  13409. }
  13410. /* How big is the user initiated abort? */
  13411. if (top != NULL) {
  13412. struct mbuf *cntm;
  13413. if (sndlen != 0) {
  13414. for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
  13415. tot_out += SCTP_BUF_LEN(cntm);
  13416. }
  13417. }
  13418. mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
  13419. } else {
  13420. /* Must fit in a MTU */
  13421. tot_out = sndlen;
  13422. tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
  13423. if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
  13424. error = EMSGSIZE;
  13425. goto out_unlocked;
  13426. }
  13427. mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
  13428. }
  13429. if (mm == NULL) {
  13430. error = ENOMEM;
  13431. goto out_unlocked;
  13432. }
  13433. max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
  13434. max_out -= sizeof(struct sctp_abort_msg);
  13435. if (tot_out > max_out) {
  13436. tot_out = max_out;
  13437. }
  13438. ph = mtod(mm, struct sctp_paramhdr *);
  13439. ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
  13440. ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
  13441. ph++;
  13442. SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
  13443. if (top == NULL) {
  13444. SCTP_TCB_UNLOCK(stcb);
  13445. #if defined(__APPLE__) && !defined(__Userspace__)
  13446. SCTP_SOCKET_UNLOCK(so, 0);
  13447. #endif
  13448. error = uiomove((caddr_t)ph, (int)tot_out, uio);
  13449. #if defined(__APPLE__) && !defined(__Userspace__)
  13450. SCTP_SOCKET_LOCK(so, 0);
  13451. #endif
  13452. SCTP_TCB_LOCK(stcb);
  13453. if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
  13454. (asoc->state & SCTP_STATE_WAS_ABORTED)) {
  13455. sctp_m_freem(mm);
  13456. if (asoc->state & SCTP_STATE_WAS_ABORTED) {
  13457. /* XXX: Could also be ECONNABORTED, not enough info. */
  13458. error = ECONNRESET;
  13459. } else {
  13460. error = ENOTCONN;
  13461. }
  13462. goto out_unlocked;
  13463. }
  13464. if (error != 0) {
  13465. /*-
  13466. * Here if we can't get his data we
  13467. * still abort we just don't get to
  13468. * send the users note :-0
  13469. */
  13470. sctp_m_freem(mm);
  13471. mm = NULL;
  13472. error = 0;
  13473. }
  13474. } else {
  13475. if (sndlen != 0) {
  13476. SCTP_BUF_NEXT(mm) = top;
  13477. }
  13478. }
  13479. atomic_subtract_int(&asoc->refcnt, 1);
  13480. free_cnt_applied = false;
  13481. /* release this lock, otherwise we hang on ourselves */
  13482. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13483. NET_EPOCH_ENTER(et);
  13484. #endif
  13485. sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
  13486. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13487. NET_EPOCH_EXIT(et);
  13488. #endif
  13489. stcb = NULL;
  13490. /* In this case top is already chained to mm
  13491. * avoid double free, since we free it below if
  13492. * top != NULL and driver would free it after sending
  13493. * the packet out
  13494. */
  13495. if (sndlen != 0) {
  13496. top = NULL;
  13497. }
  13498. goto out_unlocked;
  13499. }
  13500. KASSERT(stcb != NULL, ("stcb is NULL"));
  13501. SCTP_TCB_LOCK_ASSERT(stcb);
  13502. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  13503. ("Association about to be freed"));
  13504. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  13505. ("Association was aborted"));
  13506. if (sinfo_flags & SCTP_ADDR_OVER) {
  13507. if (addr != NULL) {
  13508. net = sctp_findnet(stcb, addr);
  13509. } else {
  13510. net = NULL;
  13511. }
  13512. if ((net == NULL) ||
  13513. ((port != 0) && (port != stcb->rport))) {
  13514. error = EINVAL;
  13515. goto out_unlocked;
  13516. }
  13517. } else {
  13518. if (asoc->alternate != NULL) {
  13519. net = asoc->alternate;
  13520. } else {
  13521. net = asoc->primary_destination;
  13522. }
  13523. }
  13524. if (sndlen == 0) {
  13525. if (sinfo_flags & SCTP_EOF) {
  13526. got_all_of_the_send = true;
  13527. goto dataless_eof;
  13528. } else {
  13529. error = EINVAL;
  13530. goto out_unlocked;
  13531. }
  13532. }
  13533. if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
  13534. if (sndlen > (ssize_t)asoc->smallest_mtu) {
  13535. error = EMSGSIZE;
  13536. goto out_unlocked;
  13537. }
  13538. }
  13539. sinfo_stream = sndrcvninfo->sinfo_stream;
  13540. /* Is the stream no. valid? */
  13541. if (sinfo_stream >= asoc->streamoutcnt) {
  13542. /* Invalid stream number */
  13543. error = EINVAL;
  13544. goto out_unlocked;
  13545. }
  13546. if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
  13547. (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
  13548. /*
  13549. * Can't queue any data while stream reset is underway.
  13550. */
  13551. if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
  13552. error = EAGAIN;
  13553. } else {
  13554. error = EINVAL;
  13555. }
  13556. goto out_unlocked;
  13557. }
  13558. atomic_add_int(&stcb->total_sends, 1);
  13559. #if defined(__Userspace__)
  13560. if (inp->recv_callback != NULL) {
  13561. non_blocking = true;
  13562. }
  13563. #endif
  13564. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13565. if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
  13566. #else
  13567. if (SCTP_SO_IS_NBIO(so)) {
  13568. #endif
  13569. non_blocking = true;
  13570. }
  13571. if (non_blocking) {
  13572. ssize_t amount;
  13573. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  13574. if (user_marks_eor == 0) {
  13575. amount = sndlen;
  13576. } else {
  13577. amount = 1;
  13578. }
  13579. if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
  13580. (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
  13581. if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
  13582. (user_marks_eor == 0)) {
  13583. error = EMSGSIZE;
  13584. } else {
  13585. error = EWOULDBLOCK;
  13586. }
  13587. goto out_unlocked;
  13588. }
  13589. }
  13590. atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
  13591. local_soresv = sndlen;
  13592. KASSERT(stcb != NULL, ("stcb is NULL"));
  13593. SCTP_TCB_LOCK_ASSERT(stcb);
  13594. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  13595. ("Association about to be freed"));
  13596. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  13597. ("Association was aborted"));
  13598. /* Ok, we will attempt a msgsnd :> */
  13599. #if !(defined(_WIN32) || defined(__Userspace__))
  13600. if (p != NULL) {
  13601. #if defined(__FreeBSD__)
  13602. p->td_ru.ru_msgsnd++;
  13603. #else
  13604. p->p_stats->p_ru.ru_msgsnd++;
  13605. #endif
  13606. }
  13607. #endif
  13608. /* Calculate the maximum we can send */
  13609. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  13610. if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
  13611. max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
  13612. } else {
  13613. max_len = 0;
  13614. }
  13615. /* Unless E_EOR mode is on, we must make a send FIT in one call. */
  13616. if ((user_marks_eor == 0) &&
  13617. (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
  13618. /* It will NEVER fit. */
  13619. error = EMSGSIZE;
  13620. goto out_unlocked;
  13621. }
  13622. if (user_marks_eor != 0) {
  13623. local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
  13624. } else {
  13625. /*-
  13626. * For non-eeor the whole message must fit in
  13627. * the socket send buffer.
  13628. */
  13629. local_add_more = sndlen;
  13630. }
  13631. if (non_blocking) {
  13632. goto skip_preblock;
  13633. }
  13634. if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
  13635. (max_len == 0) ||
  13636. ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
  13637. /* No room right now! */
  13638. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  13639. SOCKBUF_LOCK(&so->so_snd);
  13640. while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
  13641. ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
  13642. SCTPDBG(SCTP_DEBUG_OUTPUT1,"pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
  13643. (unsigned int)SCTP_SB_LIMIT_SND(so),
  13644. inqueue_bytes,
  13645. local_add_more,
  13646. asoc->stream_queue_cnt,
  13647. asoc->chunks_on_out_queue,
  13648. SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
  13649. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
  13650. sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
  13651. }
  13652. be.error = 0;
  13653. #if !(defined(_WIN32) && !defined(__Userspace__))
  13654. stcb->block_entry = &be;
  13655. #endif
  13656. SCTP_TCB_UNLOCK(stcb);
  13657. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13658. error = sbwait(so, SO_SND);
  13659. #else
  13660. error = sbwait(&so->so_snd);
  13661. #endif
  13662. if (error == 0) {
  13663. if (so->so_error != 0) {
  13664. error = so->so_error;
  13665. }
  13666. if (be.error != 0) {
  13667. error = be.error;
  13668. }
  13669. }
  13670. SOCKBUF_UNLOCK(&so->so_snd);
  13671. SCTP_TCB_LOCK(stcb);
  13672. stcb->block_entry = NULL;
  13673. if (error != 0) {
  13674. goto out_unlocked;
  13675. }
  13676. if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
  13677. (asoc->state & SCTP_STATE_WAS_ABORTED)) {
  13678. if (asoc->state & SCTP_STATE_WAS_ABORTED) {
  13679. /* XXX: Could also be ECONNABORTED, not enough info. */
  13680. error = ECONNRESET;
  13681. } else {
  13682. error = ENOTCONN;
  13683. }
  13684. goto out_unlocked;
  13685. }
  13686. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
  13687. sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
  13688. asoc, asoc->total_output_queue_size);
  13689. }
  13690. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  13691. SOCKBUF_LOCK(&so->so_snd);
  13692. }
  13693. if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
  13694. max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
  13695. } else {
  13696. max_len = 0;
  13697. }
  13698. SOCKBUF_UNLOCK(&so->so_snd);
  13699. }
  13700. skip_preblock:
  13701. KASSERT(stcb != NULL, ("stcb is NULL"));
  13702. SCTP_TCB_LOCK_ASSERT(stcb);
  13703. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  13704. ("Association about to be freed"));
  13705. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  13706. ("Association was aborted"));
  13707. #if defined(__APPLE__) && !defined(__Userspace__)
  13708. error = sblock(&so->so_snd, SBLOCKWAIT(flags));
  13709. if (error != 0) {
  13710. goto out_unlocked;
  13711. }
  13712. #endif
  13713. /* sndlen covers for mbuf case
  13714. * uio_resid covers for the non-mbuf case
  13715. * NOTE: uio will be null when top/mbuf is passed
  13716. */
  13717. if (top == NULL) {
  13718. struct sctp_stream_queue_pending *sp;
  13719. struct sctp_stream_out *strm;
  13720. uint32_t sndout;
  13721. if ((asoc->stream_locked) &&
  13722. (asoc->stream_locked_on != sinfo_stream)) {
  13723. error = EINVAL;
  13724. goto out;
  13725. }
  13726. strm = &asoc->strmout[sinfo_stream];
  13727. if (strm->last_msg_incomplete == 0) {
  13728. do_a_copy_in:
  13729. SCTP_TCB_UNLOCK(stcb);
  13730. sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
  13731. SCTP_TCB_LOCK(stcb);
  13732. if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
  13733. (asoc->state & SCTP_STATE_WAS_ABORTED)) {
  13734. if (asoc->state & SCTP_STATE_WAS_ABORTED) {
  13735. /* XXX: Could also be ECONNABORTED, not enough info. */
  13736. error = ECONNRESET;
  13737. } else {
  13738. error = ENOTCONN;
  13739. }
  13740. goto out;
  13741. }
  13742. if (error != 0) {
  13743. goto out;
  13744. }
  13745. /*
  13746. * Reject the sending of a new user message, if the
  13747. * association is about to be shut down.
  13748. */
  13749. if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
  13750. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
  13751. (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
  13752. (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
  13753. if (sp->data != 0) {
  13754. sctp_m_freem(sp->data);
  13755. sp->data = NULL;
  13756. sp->tail_mbuf = NULL;
  13757. sp->length = 0;
  13758. }
  13759. if (sp->net != NULL) {
  13760. sctp_free_remote_addr(sp->net);
  13761. sp->net = NULL;
  13762. }
  13763. sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
  13764. error = EPIPE;
  13765. goto out_unlocked;
  13766. }
  13767. /* The out streams might be reallocated. */
  13768. strm = &asoc->strmout[sinfo_stream];
  13769. if (sp->msg_is_complete) {
  13770. strm->last_msg_incomplete = 0;
  13771. asoc->stream_locked = 0;
  13772. } else {
  13773. /* Just got locked to this guy in
  13774. * case of an interrupt.
  13775. */
  13776. strm->last_msg_incomplete = 1;
  13777. if (asoc->idata_supported == 0) {
  13778. asoc->stream_locked = 1;
  13779. asoc->stream_locked_on = sinfo_stream;
  13780. }
  13781. sp->sender_all_done = 0;
  13782. }
  13783. sctp_snd_sb_alloc(stcb, sp->length);
  13784. atomic_add_int(&asoc->stream_queue_cnt, 1);
  13785. if (sinfo_flags & SCTP_UNORDERED) {
  13786. SCTP_STAT_INCR(sctps_sends_with_unord);
  13787. }
  13788. sp->processing = 1;
  13789. TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
  13790. asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
  13791. } else {
  13792. sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
  13793. if (sp == NULL) {
  13794. /* ???? Huh ??? last msg is gone */
  13795. #ifdef INVARIANTS
  13796. panic("Warning: Last msg marked incomplete, yet nothing left?");
  13797. #else
  13798. SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
  13799. strm->last_msg_incomplete = 0;
  13800. #endif
  13801. goto do_a_copy_in;
  13802. }
  13803. if (sp->processing != 0) {
  13804. error = EINVAL;
  13805. goto out;
  13806. } else {
  13807. sp->processing = 1;
  13808. }
  13809. }
  13810. KASSERT(stcb != NULL, ("stcb is NULL"));
  13811. SCTP_TCB_LOCK_ASSERT(stcb);
  13812. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  13813. ("Association about to be freed"));
  13814. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  13815. ("Association was aborted"));
  13816. #if defined(__APPLE__) && !defined(__Userspace__)
  13817. #if defined(APPLE_LEOPARD)
  13818. while (uio->uio_resid > 0) {
  13819. #else
  13820. while (uio_resid(uio) > 0) {
  13821. #endif
  13822. #else
  13823. while (uio->uio_resid > 0) {
  13824. #endif
  13825. /* How much room do we have? */
  13826. struct mbuf *new_tail, *mm;
  13827. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  13828. if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
  13829. max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
  13830. } else {
  13831. max_len = 0;
  13832. }
  13833. if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
  13834. ((max_len > 0 ) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
  13835. #if defined(__APPLE__) && !defined(__Userspace__)
  13836. #if defined(APPLE_LEOPARD)
  13837. (uio->uio_resid <= max_len)) {
  13838. #else
  13839. (uio_resid(uio) <= max_len)) {
  13840. #endif
  13841. #else
  13842. (uio->uio_resid <= max_len)) {
  13843. #endif
  13844. SCTP_TCB_UNLOCK(stcb);
  13845. #if defined(__APPLE__) && !defined(__Userspace__)
  13846. SCTP_SOCKET_UNLOCK(so, 0);
  13847. #endif
  13848. sndout = 0;
  13849. new_tail = NULL;
  13850. #if defined(__FreeBSD__) || defined(__Userspace__)
  13851. mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
  13852. #else
  13853. mm = sctp_copy_resume(uio, (int)max_len, &error, &sndout, &new_tail);
  13854. #endif
  13855. #if defined(__APPLE__) && !defined(__Userspace__)
  13856. SCTP_SOCKET_LOCK(so, 0);
  13857. #endif
  13858. SCTP_TCB_LOCK(stcb);
  13859. if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
  13860. (asoc->state & SCTP_STATE_WAS_ABORTED)) {
  13861. /* We need to get out.
  13862. * Peer probably aborted.
  13863. */
  13864. sctp_m_freem(mm);
  13865. if (asoc->state & SCTP_STATE_WAS_ABORTED) {
  13866. /* XXX: Could also be ECONNABORTED, not enough info. */
  13867. error = ECONNRESET;
  13868. } else {
  13869. error = ENOTCONN;
  13870. }
  13871. goto out;
  13872. }
  13873. if ((mm == NULL) || (error != 0)) {
  13874. if (mm != NULL) {
  13875. sctp_m_freem(mm);
  13876. }
  13877. if (sp != NULL) {
  13878. sp->processing = 0;
  13879. }
  13880. goto out;
  13881. }
  13882. /* Update the mbuf and count */
  13883. if (sp->tail_mbuf != NULL) {
  13884. /* Tack it to the end. */
  13885. SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
  13886. } else {
  13887. /* A stolen mbuf. */
  13888. sp->data = mm;
  13889. }
  13890. sp->tail_mbuf = new_tail;
  13891. sctp_snd_sb_alloc(stcb, sndout);
  13892. atomic_add_int(&sp->length, sndout);
  13893. if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
  13894. sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
  13895. }
  13896. /* Did we reach EOR? */
  13897. #if defined(__APPLE__) && !defined(__Userspace__)
  13898. #if defined(APPLE_LEOPARD)
  13899. if ((uio->uio_resid == 0) &&
  13900. #else
  13901. if ((uio_resid(uio) == 0) &&
  13902. #endif
  13903. #else
  13904. if ((uio->uio_resid == 0) &&
  13905. #endif
  13906. ((user_marks_eor == 0) ||
  13907. (sinfo_flags & SCTP_EOF) ||
  13908. (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
  13909. sp->msg_is_complete = 1;
  13910. } else {
  13911. sp->msg_is_complete = 0;
  13912. }
  13913. }
  13914. KASSERT(stcb != NULL, ("stcb is NULL"));
  13915. SCTP_TCB_LOCK_ASSERT(stcb);
  13916. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  13917. ("Association about to be freed"));
  13918. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  13919. ("Association was aborted"));
  13920. #if defined(__APPLE__) && !defined(__Userspace__)
  13921. #if defined(APPLE_LEOPARD)
  13922. if (uio->uio_resid == 0) {
  13923. #else
  13924. if (uio_resid(uio) == 0) {
  13925. #endif
  13926. #else
  13927. if (uio->uio_resid == 0) {
  13928. #endif
  13929. /* got it all? */
  13930. continue;
  13931. }
  13932. /* PR-SCTP? */
  13933. if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
  13934. /* This is ugly but we must assure locking order */
  13935. sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
  13936. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  13937. if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
  13938. max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
  13939. else
  13940. max_len = 0;
  13941. if (max_len > 0) {
  13942. continue;
  13943. }
  13944. }
  13945. /* wait for space now */
  13946. if (non_blocking) {
  13947. /* Non-blocking io in place out */
  13948. if (sp != NULL) {
  13949. sp->processing = 0;
  13950. }
  13951. goto skip_out_eof;
  13952. }
  13953. /* What about the INIT, send it maybe */
  13954. if (queue_only_for_init) {
  13955. if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
  13956. /* a collision took us forward? */
  13957. queue_only = 0;
  13958. } else {
  13959. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13960. NET_EPOCH_ENTER(et);
  13961. #endif
  13962. sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
  13963. #if defined(__FreeBSD__) && !defined(__Userspace__)
  13964. NET_EPOCH_EXIT(et);
  13965. #endif
  13966. SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
  13967. queue_only = 1;
  13968. }
  13969. }
  13970. if ((net->flight_size > net->cwnd) &&
  13971. (asoc->sctp_cmt_on_off == 0)) {
  13972. SCTP_STAT_INCR(sctps_send_cwnd_avoid);
  13973. queue_only = 1;
  13974. } else if (asoc->ifp_had_enobuf) {
  13975. SCTP_STAT_INCR(sctps_ifnomemqueued);
  13976. if (net->flight_size > (2 * net->mtu)) {
  13977. queue_only = 1;
  13978. }
  13979. asoc->ifp_had_enobuf = 0;
  13980. }
  13981. un_sent = asoc->total_output_queue_size - asoc->total_flight;
  13982. if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
  13983. (asoc->total_flight > 0) &&
  13984. (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
  13985. (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
  13986. /*-
  13987. * Ok, Nagle is set on and we have data outstanding.
  13988. * Don't send anything and let SACKs drive out the
  13989. * data unless we have a "full" segment to send.
  13990. */
  13991. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
  13992. sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
  13993. }
  13994. SCTP_STAT_INCR(sctps_naglequeued);
  13995. nagle_applies = 1;
  13996. } else {
  13997. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
  13998. if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
  13999. sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
  14000. }
  14001. SCTP_STAT_INCR(sctps_naglesent);
  14002. nagle_applies = 0;
  14003. }
  14004. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
  14005. sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
  14006. nagle_applies, un_sent);
  14007. sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
  14008. asoc->total_flight,
  14009. asoc->chunks_on_out_queue, asoc->total_flight_count);
  14010. }
  14011. if (queue_only_for_init) {
  14012. queue_only_for_init = 0;
  14013. }
  14014. if ((queue_only == 0) && (nagle_applies == 0)) {
  14015. /*-
  14016. * need to start chunk output
  14017. * before blocking.. note that if
  14018. * a lock is already applied, then
  14019. * the input via the net is happening
  14020. * and I don't need to start output :-D
  14021. */
  14022. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14023. NET_EPOCH_ENTER(et);
  14024. #endif
  14025. sctp_chunk_output(inp, stcb,
  14026. SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
  14027. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14028. NET_EPOCH_EXIT(et);
  14029. #endif
  14030. }
  14031. /*-
  14032. * This is a bit strange, but I think it will
  14033. * work. The total_output_queue_size is locked and
  14034. * protected by the TCB_LOCK, which we just released.
  14035. * There is a race that can occur between releasing it
  14036. * above, and me getting the socket lock, where sacks
  14037. * come in but we have not put the SB_WAIT on the
  14038. * so_snd buffer to get the wakeup. After the LOCK
  14039. * is applied the sack_processing will also need to
  14040. * LOCK the so->so_snd to do the actual sowwakeup(). So
  14041. * once we have the socket buffer lock if we recheck the
  14042. * size we KNOW we will get to sleep safely with the
  14043. * wakeup flag in place.
  14044. */
  14045. inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
  14046. SOCKBUF_LOCK(&so->so_snd);
  14047. if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
  14048. min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
  14049. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
  14050. #if defined(__APPLE__) && !defined(__Userspace__)
  14051. #if defined(APPLE_LEOPARD)
  14052. sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
  14053. asoc, uio->uio_resid);
  14054. #else
  14055. sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
  14056. asoc, uio_resid(uio));
  14057. #endif
  14058. #else
  14059. sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
  14060. asoc, uio->uio_resid);
  14061. #endif
  14062. }
  14063. be.error = 0;
  14064. #if !(defined(_WIN32) && !defined(__Userspace__))
  14065. stcb->block_entry = &be;
  14066. #endif
  14067. SCTP_TCB_UNLOCK(stcb);
  14068. #if defined(__APPLE__) && !defined(__Userspace__)
  14069. sbunlock(&so->so_snd, 1);
  14070. #endif
  14071. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14072. error = sbwait(so, SO_SND);
  14073. #else
  14074. error = sbwait(&so->so_snd);
  14075. #endif
  14076. if (error == 0) {
  14077. if (so->so_error != 0)
  14078. error = so->so_error;
  14079. if (be.error != 0) {
  14080. error = be.error;
  14081. }
  14082. }
  14083. SOCKBUF_UNLOCK(&so->so_snd);
  14084. SCTP_TCB_LOCK(stcb);
  14085. stcb->block_entry = NULL;
  14086. if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
  14087. (asoc->state & SCTP_STATE_WAS_ABORTED)) {
  14088. if (asoc->state & SCTP_STATE_WAS_ABORTED) {
  14089. /* XXX: Could also be ECONNABORTED, not enough info. */
  14090. error = ECONNRESET;
  14091. } else {
  14092. error = ENOTCONN;
  14093. }
  14094. goto out_unlocked;
  14095. }
  14096. if (error != 0) {
  14097. if (sp != NULL) {
  14098. sp->processing = 0;
  14099. }
  14100. goto out_unlocked;
  14101. }
  14102. #if defined(__APPLE__) && !defined(__Userspace__)
  14103. error = sblock(&so->so_snd, SBLOCKWAIT(flags));
  14104. if (error != 0) {
  14105. goto out_unlocked;
  14106. }
  14107. #endif
  14108. } else {
  14109. SOCKBUF_UNLOCK(&so->so_snd);
  14110. }
  14111. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
  14112. sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
  14113. asoc, asoc->total_output_queue_size);
  14114. }
  14115. }
  14116. KASSERT(stcb != NULL, ("stcb is NULL"));
  14117. SCTP_TCB_LOCK_ASSERT(stcb);
  14118. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  14119. ("Association about to be freed"));
  14120. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  14121. ("Association was aborted"));
  14122. /* The out streams might be reallocated. */
  14123. strm = &asoc->strmout[sinfo_stream];
  14124. if (sp != NULL) {
  14125. if (sp->msg_is_complete == 0) {
  14126. strm->last_msg_incomplete = 1;
  14127. if (asoc->idata_supported == 0) {
  14128. asoc->stream_locked = 1;
  14129. asoc->stream_locked_on = sinfo_stream;
  14130. }
  14131. } else {
  14132. sp->sender_all_done = 1;
  14133. strm->last_msg_incomplete = 0;
  14134. asoc->stream_locked = 0;
  14135. }
  14136. sp->processing = 0;
  14137. } else {
  14138. SCTP_PRINTF("Huh no sp TSNH?\n");
  14139. strm->last_msg_incomplete = 0;
  14140. asoc->stream_locked = 0;
  14141. }
  14142. #if defined(__APPLE__) && !defined(__Userspace__)
  14143. #if defined(APPLE_LEOPARD)
  14144. if (uio->uio_resid == 0) {
  14145. #else
  14146. if (uio_resid(uio) == 0) {
  14147. #endif
  14148. #else
  14149. if (uio->uio_resid == 0) {
  14150. #endif
  14151. got_all_of_the_send = true;
  14152. }
  14153. } else {
  14154. error = sctp_msg_append(stcb, net, top, sndrcvninfo);
  14155. top = NULL;
  14156. if ((sinfo_flags & SCTP_EOF) != 0) {
  14157. got_all_of_the_send = true;
  14158. }
  14159. }
  14160. if (error != 0) {
  14161. goto out;
  14162. }
  14163. dataless_eof:
  14164. KASSERT(stcb != NULL, ("stcb is NULL"));
  14165. SCTP_TCB_LOCK_ASSERT(stcb);
  14166. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  14167. ("Association about to be freed"));
  14168. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  14169. ("Association was aborted"));
  14170. /* EOF thing ? */
  14171. if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
  14172. SCTP_STAT_INCR(sctps_sends_with_eof);
  14173. error = 0;
  14174. if (TAILQ_EMPTY(&asoc->send_queue) &&
  14175. TAILQ_EMPTY(&asoc->sent_queue) &&
  14176. sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
  14177. if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
  14178. goto abort_anyway;
  14179. }
  14180. /* there is nothing queued to send, so I'm done... */
  14181. if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
  14182. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
  14183. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  14184. struct sctp_nets *netp;
  14185. /* only send SHUTDOWN the first time through */
  14186. if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
  14187. SCTP_STAT_DECR_GAUGE32(sctps_currestab);
  14188. }
  14189. SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
  14190. sctp_stop_timers_for_shutdown(stcb);
  14191. if (asoc->alternate != NULL) {
  14192. netp = asoc->alternate;
  14193. } else {
  14194. netp = asoc->primary_destination;
  14195. }
  14196. sctp_send_shutdown(stcb, netp);
  14197. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
  14198. netp);
  14199. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
  14200. NULL);
  14201. }
  14202. } else {
  14203. /*-
  14204. * we still got (or just got) data to send, so set
  14205. * SHUTDOWN_PENDING
  14206. */
  14207. /*-
  14208. * XXX sockets draft says that SCTP_EOF should be
  14209. * sent with no data. currently, we will allow user
  14210. * data to be sent first and move to
  14211. * SHUTDOWN-PENDING
  14212. */
  14213. if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
  14214. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
  14215. (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
  14216. if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
  14217. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
  14218. }
  14219. SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
  14220. if (TAILQ_EMPTY(&asoc->send_queue) &&
  14221. TAILQ_EMPTY(&asoc->sent_queue) &&
  14222. (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
  14223. struct mbuf *op_err;
  14224. char msg[SCTP_DIAG_INFO_LEN];
  14225. abort_anyway:
  14226. if (free_cnt_applied) {
  14227. atomic_subtract_int(&asoc->refcnt, 1);
  14228. free_cnt_applied = false;
  14229. }
  14230. SCTP_SNPRINTF(msg, sizeof(msg),
  14231. "%s:%d at %s", __FILE__, __LINE__, __func__);
  14232. op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
  14233. msg);
  14234. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14235. NET_EPOCH_ENTER(et);
  14236. #endif
  14237. sctp_abort_an_association(stcb->sctp_ep, stcb,
  14238. op_err, false, SCTP_SO_LOCKED);
  14239. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14240. NET_EPOCH_EXIT(et);
  14241. #endif
  14242. stcb = NULL;
  14243. error = ECONNABORTED;
  14244. goto out;
  14245. }
  14246. sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
  14247. NULL);
  14248. sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
  14249. }
  14250. }
  14251. }
  14252. skip_out_eof:
  14253. KASSERT(stcb != NULL, ("stcb is NULL"));
  14254. SCTP_TCB_LOCK_ASSERT(stcb);
  14255. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  14256. ("Association about to be freed"));
  14257. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  14258. ("Association was aborted"));
  14259. some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
  14260. if (queue_only_for_init) {
  14261. if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
  14262. /* a collision took us forward? */
  14263. queue_only = 0;
  14264. } else {
  14265. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14266. NET_EPOCH_ENTER(et);
  14267. #endif
  14268. sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
  14269. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14270. NET_EPOCH_EXIT(et);
  14271. #endif
  14272. SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
  14273. queue_only = 1;
  14274. }
  14275. }
  14276. KASSERT(stcb != NULL, ("stcb is NULL"));
  14277. SCTP_TCB_LOCK_ASSERT(stcb);
  14278. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  14279. ("Association about to be freed"));
  14280. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  14281. ("Association was aborted"));
  14282. if ((net->flight_size > net->cwnd) &&
  14283. (asoc->sctp_cmt_on_off == 0)) {
  14284. SCTP_STAT_INCR(sctps_send_cwnd_avoid);
  14285. queue_only = 1;
  14286. } else if (asoc->ifp_had_enobuf) {
  14287. SCTP_STAT_INCR(sctps_ifnomemqueued);
  14288. if (net->flight_size > (2 * net->mtu)) {
  14289. queue_only = 1;
  14290. }
  14291. asoc->ifp_had_enobuf = 0;
  14292. }
  14293. un_sent = asoc->total_output_queue_size - asoc->total_flight;
  14294. if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
  14295. (asoc->total_flight > 0) &&
  14296. (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
  14297. (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
  14298. /*-
  14299. * Ok, Nagle is set on and we have data outstanding.
  14300. * Don't send anything and let SACKs drive out the
  14301. * data unless wen have a "full" segment to send.
  14302. */
  14303. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
  14304. sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
  14305. }
  14306. SCTP_STAT_INCR(sctps_naglequeued);
  14307. nagle_applies = 1;
  14308. } else {
  14309. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
  14310. if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
  14311. sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
  14312. }
  14313. SCTP_STAT_INCR(sctps_naglesent);
  14314. nagle_applies = 0;
  14315. }
  14316. if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
  14317. sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
  14318. nagle_applies, un_sent);
  14319. sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
  14320. asoc->total_flight,
  14321. asoc->chunks_on_out_queue, asoc->total_flight_count);
  14322. }
  14323. KASSERT(stcb != NULL, ("stcb is NULL"));
  14324. SCTP_TCB_LOCK_ASSERT(stcb);
  14325. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  14326. ("Association about to be freed"));
  14327. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  14328. ("Association was aborted"));
  14329. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14330. NET_EPOCH_ENTER(et);
  14331. #endif
  14332. if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
  14333. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
  14334. } else if ((queue_only == 0) &&
  14335. (asoc->peers_rwnd == 0) &&
  14336. (asoc->total_flight == 0)) {
  14337. /* We get to have a probe outstanding */
  14338. sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
  14339. } else if (some_on_control) {
  14340. int num_out, reason;
  14341. /* Here we do control only */
  14342. (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
  14343. &reason, 1, 1, &now, &now_filled,
  14344. sctp_get_frag_point(stcb),
  14345. SCTP_SO_LOCKED);
  14346. }
  14347. #if defined(__FreeBSD__) && !defined(__Userspace__)
  14348. NET_EPOCH_EXIT(et);
  14349. #endif
  14350. SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
  14351. queue_only, asoc->peers_rwnd, un_sent,
  14352. asoc->total_flight, asoc->chunks_on_out_queue,
  14353. asoc->total_output_queue_size, error);
  14354. KASSERT(stcb != NULL, ("stcb is NULL"));
  14355. SCTP_TCB_LOCK_ASSERT(stcb);
  14356. KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
  14357. ("Association about to be freed"));
  14358. KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
  14359. ("Association was aborted"));
  14360. out:
  14361. #if defined(__APPLE__) && !defined(__Userspace__)
  14362. sbunlock(&so->so_snd, 1);
  14363. #endif
  14364. out_unlocked:
  14365. if (create_lock_applied) {
  14366. SCTP_ASOC_CREATE_UNLOCK(inp);
  14367. }
  14368. if (stcb != NULL) {
  14369. if (local_soresv) {
  14370. atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
  14371. }
  14372. if (free_cnt_applied) {
  14373. atomic_subtract_int(&asoc->refcnt, 1);
  14374. }
  14375. SCTP_TCB_UNLOCK(stcb);
  14376. }
  14377. if (top != NULL) {
  14378. sctp_m_freem(top);
  14379. }
  14380. if (control != NULL) {
  14381. sctp_m_freem(control);
  14382. }
  14383. SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
  14384. return (error);
  14385. }
  14386. /*
  14387. * generate an AUTHentication chunk, if required
  14388. */
  14389. struct mbuf *
  14390. sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
  14391. struct sctp_auth_chunk **auth_ret, uint32_t * offset,
  14392. struct sctp_tcb *stcb, uint8_t chunk)
  14393. {
  14394. struct mbuf *m_auth;
  14395. struct sctp_auth_chunk *auth;
  14396. int chunk_len;
  14397. struct mbuf *cn;
  14398. if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
  14399. (stcb == NULL))
  14400. return (m);
  14401. if (stcb->asoc.auth_supported == 0) {
  14402. return (m);
  14403. }
  14404. /* does the requested chunk require auth? */
  14405. if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
  14406. return (m);
  14407. }
  14408. m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
  14409. if (m_auth == NULL) {
  14410. /* no mbuf's */
  14411. return (m);
  14412. }
  14413. /* reserve some space if this will be the first mbuf */
  14414. if (m == NULL)
  14415. SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
  14416. /* fill in the AUTH chunk details */
  14417. auth = mtod(m_auth, struct sctp_auth_chunk *);
  14418. memset(auth, 0, sizeof(*auth));
  14419. auth->ch.chunk_type = SCTP_AUTHENTICATION;
  14420. auth->ch.chunk_flags = 0;
  14421. chunk_len = sizeof(*auth) +
  14422. sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
  14423. auth->ch.chunk_length = htons(chunk_len);
  14424. auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
  14425. /* key id and hmac digest will be computed and filled in upon send */
  14426. /* save the offset where the auth was inserted into the chain */
  14427. *offset = 0;
  14428. for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
  14429. *offset += SCTP_BUF_LEN(cn);
  14430. }
  14431. /* update length and return pointer to the auth chunk */
  14432. SCTP_BUF_LEN(m_auth) = chunk_len;
  14433. m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
  14434. if (auth_ret != NULL)
  14435. *auth_ret = auth;
  14436. return (m);
  14437. }
  14438. #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
  14439. #ifdef INET6
  14440. int
  14441. sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
  14442. {
  14443. struct nd_prefix *pfx = NULL;
  14444. struct nd_pfxrouter *pfxrtr = NULL;
  14445. struct sockaddr_in6 gw6;
  14446. #if defined(__FreeBSD__)
  14447. if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
  14448. #else
  14449. if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
  14450. #endif
  14451. return (0);
  14452. /* get prefix entry of address */
  14453. #if defined(__FreeBSD__)
  14454. ND6_RLOCK();
  14455. #endif
  14456. LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
  14457. if (pfx->ndpr_stateflags & NDPRF_DETACHED)
  14458. continue;
  14459. if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
  14460. &src6->sin6_addr, &pfx->ndpr_mask))
  14461. break;
  14462. }
  14463. /* no prefix entry in the prefix list */
  14464. if (pfx == NULL) {
  14465. #if defined(__FreeBSD__)
  14466. ND6_RUNLOCK();
  14467. #endif
  14468. SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
  14469. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
  14470. return (0);
  14471. }
  14472. SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
  14473. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
  14474. /* search installed gateway from prefix entry */
  14475. LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
  14476. memset(&gw6, 0, sizeof(struct sockaddr_in6));
  14477. gw6.sin6_family = AF_INET6;
  14478. #ifdef HAVE_SIN6_LEN
  14479. gw6.sin6_len = sizeof(struct sockaddr_in6);
  14480. #endif
  14481. memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
  14482. sizeof(struct in6_addr));
  14483. SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
  14484. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
  14485. SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
  14486. #if defined(__FreeBSD__)
  14487. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
  14488. #else
  14489. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
  14490. #endif
  14491. #if defined(__FreeBSD__)
  14492. if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
  14493. ND6_RUNLOCK();
  14494. #else
  14495. if (sctp_cmpaddr((struct sockaddr *)&gw6, ro->ro_rt->rt_gateway)) {
  14496. #endif
  14497. SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
  14498. return (1);
  14499. }
  14500. }
  14501. #if defined(__FreeBSD__)
  14502. ND6_RUNLOCK();
  14503. #endif
  14504. SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
  14505. return (0);
  14506. }
  14507. #endif
  14508. int
  14509. sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
  14510. {
  14511. #ifdef INET
  14512. struct sockaddr_in *sin, *mask;
  14513. struct ifaddr *ifa;
  14514. struct in_addr srcnetaddr, gwnetaddr;
  14515. #if defined(__FreeBSD__)
  14516. if (ro == NULL || ro->ro_nh == NULL ||
  14517. #else
  14518. if (ro == NULL || ro->ro_rt == NULL ||
  14519. #endif
  14520. sifa->address.sa.sa_family != AF_INET) {
  14521. return (0);
  14522. }
  14523. ifa = (struct ifaddr *)sifa->ifa;
  14524. mask = (struct sockaddr_in *)(ifa->ifa_netmask);
  14525. sin = &sifa->address.sin;
  14526. srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
  14527. SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
  14528. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
  14529. SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
  14530. #if defined(__FreeBSD__)
  14531. sin = &ro->ro_nh->gw4_sa;
  14532. #else
  14533. sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
  14534. #endif
  14535. gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
  14536. SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
  14537. #if defined(__FreeBSD__)
  14538. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
  14539. #else
  14540. SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
  14541. #endif
  14542. SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
  14543. if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
  14544. return (1);
  14545. }
  14546. #endif
  14547. return (0);
  14548. }
  14549. #elif defined(__Userspace__)
  14550. /* TODO __Userspace__ versions of sctp_vXsrc_match_nexthop(). */
  14551. int
  14552. sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
  14553. {
  14554. return (0);
  14555. }
  14556. int
  14557. sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
  14558. {
  14559. return (0);
  14560. }
  14561. #endif