sm_statefuns.c 209 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001-2002 Intel Corp.
  7. * Copyright (c) 2002 Nokia Corp.
  8. *
  9. * This is part of the SCTP Linux Kernel Implementation.
  10. *
  11. * These are the state functions for the state machine.
  12. *
  13. * Please send any bug reports or fixes you make to the
  14. * email address(es):
  15. * lksctp developers <[email protected]>
  16. *
  17. * Written or modified by:
  18. * La Monte H.P. Yarroll <[email protected]>
  19. * Karl Knutson <[email protected]>
  20. * Mathew Kotowsky <[email protected]>
  21. * Sridhar Samudrala <[email protected]>
  22. * Jon Grimm <[email protected]>
  23. * Hui Huang <[email protected]>
  24. * Dajiang Zhang <[email protected]>
  25. * Daisy Chang <[email protected]>
  26. * Ardelle Fan <[email protected]>
  27. * Ryan Layer <[email protected]>
  28. * Kevin Gao <[email protected]>
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/types.h>
  32. #include <linux/kernel.h>
  33. #include <linux/ip.h>
  34. #include <linux/ipv6.h>
  35. #include <linux/net.h>
  36. #include <linux/inet.h>
  37. #include <linux/slab.h>
  38. #include <net/sock.h>
  39. #include <net/inet_ecn.h>
  40. #include <linux/skbuff.h>
  41. #include <net/sctp/sctp.h>
  42. #include <net/sctp/sm.h>
  43. #include <net/sctp/structs.h>
  44. #define CREATE_TRACE_POINTS
  45. #include <trace/events/sctp.h>
  46. static struct sctp_packet *sctp_abort_pkt_new(
  47. struct net *net,
  48. const struct sctp_endpoint *ep,
  49. const struct sctp_association *asoc,
  50. struct sctp_chunk *chunk,
  51. const void *payload, size_t paylen);
  52. static int sctp_eat_data(const struct sctp_association *asoc,
  53. struct sctp_chunk *chunk,
  54. struct sctp_cmd_seq *commands);
  55. static struct sctp_packet *sctp_ootb_pkt_new(
  56. struct net *net,
  57. const struct sctp_association *asoc,
  58. const struct sctp_chunk *chunk);
  59. static void sctp_send_stale_cookie_err(struct net *net,
  60. const struct sctp_endpoint *ep,
  61. const struct sctp_association *asoc,
  62. const struct sctp_chunk *chunk,
  63. struct sctp_cmd_seq *commands,
  64. struct sctp_chunk *err_chunk);
  65. static enum sctp_disposition sctp_sf_do_5_2_6_stale(
  66. struct net *net,
  67. const struct sctp_endpoint *ep,
  68. const struct sctp_association *asoc,
  69. const union sctp_subtype type,
  70. void *arg,
  71. struct sctp_cmd_seq *commands);
  72. static enum sctp_disposition sctp_sf_shut_8_4_5(
  73. struct net *net,
  74. const struct sctp_endpoint *ep,
  75. const struct sctp_association *asoc,
  76. const union sctp_subtype type,
  77. void *arg,
  78. struct sctp_cmd_seq *commands);
  79. static enum sctp_disposition sctp_sf_tabort_8_4_8(
  80. struct net *net,
  81. const struct sctp_endpoint *ep,
  82. const struct sctp_association *asoc,
  83. const union sctp_subtype type,
  84. void *arg,
  85. struct sctp_cmd_seq *commands);
  86. static enum sctp_disposition sctp_sf_new_encap_port(
  87. struct net *net,
  88. const struct sctp_endpoint *ep,
  89. const struct sctp_association *asoc,
  90. const union sctp_subtype type,
  91. void *arg,
  92. struct sctp_cmd_seq *commands);
  93. static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
  94. static enum sctp_disposition sctp_stop_t1_and_abort(
  95. struct net *net,
  96. struct sctp_cmd_seq *commands,
  97. __be16 error, int sk_err,
  98. const struct sctp_association *asoc,
  99. struct sctp_transport *transport);
  100. static enum sctp_disposition sctp_sf_abort_violation(
  101. struct net *net,
  102. const struct sctp_endpoint *ep,
  103. const struct sctp_association *asoc,
  104. void *arg,
  105. struct sctp_cmd_seq *commands,
  106. const __u8 *payload,
  107. const size_t paylen);
  108. static enum sctp_disposition sctp_sf_violation_chunklen(
  109. struct net *net,
  110. const struct sctp_endpoint *ep,
  111. const struct sctp_association *asoc,
  112. const union sctp_subtype type,
  113. void *arg,
  114. struct sctp_cmd_seq *commands);
  115. static enum sctp_disposition sctp_sf_violation_paramlen(
  116. struct net *net,
  117. const struct sctp_endpoint *ep,
  118. const struct sctp_association *asoc,
  119. const union sctp_subtype type,
  120. void *arg, void *ext,
  121. struct sctp_cmd_seq *commands);
  122. static enum sctp_disposition sctp_sf_violation_ctsn(
  123. struct net *net,
  124. const struct sctp_endpoint *ep,
  125. const struct sctp_association *asoc,
  126. const union sctp_subtype type,
  127. void *arg,
  128. struct sctp_cmd_seq *commands);
  129. static enum sctp_disposition sctp_sf_violation_chunk(
  130. struct net *net,
  131. const struct sctp_endpoint *ep,
  132. const struct sctp_association *asoc,
  133. const union sctp_subtype type,
  134. void *arg,
  135. struct sctp_cmd_seq *commands);
  136. static enum sctp_ierror sctp_sf_authenticate(
  137. const struct sctp_association *asoc,
  138. struct sctp_chunk *chunk);
  139. static enum sctp_disposition __sctp_sf_do_9_1_abort(
  140. struct net *net,
  141. const struct sctp_endpoint *ep,
  142. const struct sctp_association *asoc,
  143. const union sctp_subtype type,
  144. void *arg,
  145. struct sctp_cmd_seq *commands);
  146. static enum sctp_disposition
  147. __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
  148. const struct sctp_association *asoc,
  149. const union sctp_subtype type, void *arg,
  150. struct sctp_cmd_seq *commands);
  151. /* Small helper function that checks if the chunk length
  152. * is of the appropriate length. The 'required_length' argument
  153. * is set to be the size of a specific chunk we are testing.
  154. * Return Values: true = Valid length
  155. * false = Invalid length
  156. *
  157. */
  158. static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
  159. __u16 required_length)
  160. {
  161. __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
  162. /* Previously already marked? */
  163. if (unlikely(chunk->pdiscard))
  164. return false;
  165. if (unlikely(chunk_length < required_length))
  166. return false;
  167. return true;
  168. }
  169. /* Check for format error in an ABORT chunk */
  170. static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
  171. {
  172. struct sctp_errhdr *err;
  173. sctp_walk_errors(err, chunk->chunk_hdr);
  174. return (void *)err == (void *)chunk->chunk_end;
  175. }
  176. /**********************************************************
  177. * These are the state functions for handling chunk events.
  178. **********************************************************/
  179. /*
  180. * Process the final SHUTDOWN COMPLETE.
  181. *
  182. * Section: 4 (C) (diagram), 9.2
  183. * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
  184. * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
  185. * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
  186. * should stop the T2-shutdown timer and remove all knowledge of the
  187. * association (and thus the association enters the CLOSED state).
  188. *
  189. * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
  190. * C) Rules for packet carrying SHUTDOWN COMPLETE:
  191. * ...
  192. * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
  193. * if the Verification Tag field of the packet matches its own tag and
  194. * the T bit is not set
  195. * OR
  196. * it is set to its peer's tag and the T bit is set in the Chunk
  197. * Flags.
  198. * Otherwise, the receiver MUST silently discard the packet
  199. * and take no further action. An endpoint MUST ignore the
  200. * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
  201. *
  202. * Inputs
  203. * (endpoint, asoc, chunk)
  204. *
  205. * Outputs
  206. * (asoc, reply_msg, msg_up, timers, counters)
  207. *
  208. * The return value is the disposition of the chunk.
  209. */
  210. enum sctp_disposition sctp_sf_do_4_C(struct net *net,
  211. const struct sctp_endpoint *ep,
  212. const struct sctp_association *asoc,
  213. const union sctp_subtype type,
  214. void *arg, struct sctp_cmd_seq *commands)
  215. {
  216. struct sctp_chunk *chunk = arg;
  217. struct sctp_ulpevent *ev;
  218. if (!sctp_vtag_verify_either(chunk, asoc))
  219. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  220. /* RFC 2960 6.10 Bundling
  221. *
  222. * An endpoint MUST NOT bundle INIT, INIT ACK or
  223. * SHUTDOWN COMPLETE with any other chunks.
  224. */
  225. if (!chunk->singleton)
  226. return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
  227. /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
  228. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  229. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  230. commands);
  231. /* RFC 2960 10.2 SCTP-to-ULP
  232. *
  233. * H) SHUTDOWN COMPLETE notification
  234. *
  235. * When SCTP completes the shutdown procedures (section 9.2) this
  236. * notification is passed to the upper layer.
  237. */
  238. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
  239. 0, 0, 0, NULL, GFP_ATOMIC);
  240. if (ev)
  241. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  242. SCTP_ULPEVENT(ev));
  243. /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
  244. * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
  245. * not the chunk should be discarded. If the endpoint is in
  246. * the SHUTDOWN-ACK-SENT state the endpoint should stop the
  247. * T2-shutdown timer and remove all knowledge of the
  248. * association (and thus the association enters the CLOSED
  249. * state).
  250. */
  251. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  252. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  253. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  254. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  255. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  256. SCTP_STATE(SCTP_STATE_CLOSED));
  257. SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
  258. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  259. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  260. return SCTP_DISPOSITION_DELETE_TCB;
  261. }
  262. /*
  263. * Respond to a normal INIT chunk.
  264. * We are the side that is being asked for an association.
  265. *
  266. * Section: 5.1 Normal Establishment of an Association, B
  267. * B) "Z" shall respond immediately with an INIT ACK chunk. The
  268. * destination IP address of the INIT ACK MUST be set to the source
  269. * IP address of the INIT to which this INIT ACK is responding. In
  270. * the response, besides filling in other parameters, "Z" must set the
  271. * Verification Tag field to Tag_A, and also provide its own
  272. * Verification Tag (Tag_Z) in the Initiate Tag field.
  273. *
  274. * Verification Tag: Must be 0.
  275. *
  276. * Inputs
  277. * (endpoint, asoc, chunk)
  278. *
  279. * Outputs
  280. * (asoc, reply_msg, msg_up, timers, counters)
  281. *
  282. * The return value is the disposition of the chunk.
  283. */
  284. enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
  285. const struct sctp_endpoint *ep,
  286. const struct sctp_association *asoc,
  287. const union sctp_subtype type,
  288. void *arg,
  289. struct sctp_cmd_seq *commands)
  290. {
  291. struct sctp_chunk *chunk = arg, *repl, *err_chunk;
  292. struct sctp_unrecognized_param *unk_param;
  293. struct sctp_association *new_asoc;
  294. struct sctp_packet *packet;
  295. int len;
  296. /* 6.10 Bundling
  297. * An endpoint MUST NOT bundle INIT, INIT ACK or
  298. * SHUTDOWN COMPLETE with any other chunks.
  299. *
  300. * IG Section 2.11.2
  301. * Furthermore, we require that the receiver of an INIT chunk MUST
  302. * enforce these rules by silently discarding an arriving packet
  303. * with an INIT chunk that is bundled with other chunks.
  304. */
  305. if (!chunk->singleton)
  306. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  307. /* Make sure that the INIT chunk has a valid length.
  308. * Normally, this would cause an ABORT with a Protocol Violation
  309. * error, but since we don't have an association, we'll
  310. * just discard the packet.
  311. */
  312. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
  313. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  314. /* If the packet is an OOTB packet which is temporarily on the
  315. * control endpoint, respond with an ABORT.
  316. */
  317. if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
  318. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  319. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  320. }
  321. /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
  322. * Tag.
  323. */
  324. if (chunk->sctp_hdr->vtag != 0)
  325. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  326. /* If the INIT is coming toward a closing socket, we'll send back
  327. * and ABORT. Essentially, this catches the race of INIT being
  328. * backloged to the socket at the same time as the user issues close().
  329. * Since the socket and all its associations are going away, we
  330. * can treat this OOTB
  331. */
  332. if (sctp_sstate(ep->base.sk, CLOSING))
  333. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  334. /* Verify the INIT chunk before processing it. */
  335. err_chunk = NULL;
  336. if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
  337. (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
  338. &err_chunk)) {
  339. /* This chunk contains fatal error. It is to be discarded.
  340. * Send an ABORT, with causes if there is any.
  341. */
  342. if (err_chunk) {
  343. packet = sctp_abort_pkt_new(net, ep, asoc, arg,
  344. (__u8 *)(err_chunk->chunk_hdr) +
  345. sizeof(struct sctp_chunkhdr),
  346. ntohs(err_chunk->chunk_hdr->length) -
  347. sizeof(struct sctp_chunkhdr));
  348. sctp_chunk_free(err_chunk);
  349. if (packet) {
  350. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  351. SCTP_PACKET(packet));
  352. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  353. return SCTP_DISPOSITION_CONSUME;
  354. } else {
  355. return SCTP_DISPOSITION_NOMEM;
  356. }
  357. } else {
  358. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
  359. commands);
  360. }
  361. }
  362. /* Grab the INIT header. */
  363. chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
  364. /* Tag the variable length parameters. */
  365. chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
  366. new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
  367. if (!new_asoc)
  368. goto nomem;
  369. /* Update socket peer label if first association. */
  370. if (security_sctp_assoc_request(new_asoc, chunk->skb)) {
  371. sctp_association_free(new_asoc);
  372. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  373. }
  374. if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
  375. sctp_scope(sctp_source(chunk)),
  376. GFP_ATOMIC) < 0)
  377. goto nomem_init;
  378. /* The call, sctp_process_init(), can fail on memory allocation. */
  379. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
  380. (struct sctp_init_chunk *)chunk->chunk_hdr,
  381. GFP_ATOMIC))
  382. goto nomem_init;
  383. /* B) "Z" shall respond immediately with an INIT ACK chunk. */
  384. /* If there are errors need to be reported for unknown parameters,
  385. * make sure to reserve enough room in the INIT ACK for them.
  386. */
  387. len = 0;
  388. if (err_chunk)
  389. len = ntohs(err_chunk->chunk_hdr->length) -
  390. sizeof(struct sctp_chunkhdr);
  391. repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
  392. if (!repl)
  393. goto nomem_init;
  394. /* If there are errors need to be reported for unknown parameters,
  395. * include them in the outgoing INIT ACK as "Unrecognized parameter"
  396. * parameter.
  397. */
  398. if (err_chunk) {
  399. /* Get the "Unrecognized parameter" parameter(s) out of the
  400. * ERROR chunk generated by sctp_verify_init(). Since the
  401. * error cause code for "unknown parameter" and the
  402. * "Unrecognized parameter" type is the same, we can
  403. * construct the parameters in INIT ACK by copying the
  404. * ERROR causes over.
  405. */
  406. unk_param = (struct sctp_unrecognized_param *)
  407. ((__u8 *)(err_chunk->chunk_hdr) +
  408. sizeof(struct sctp_chunkhdr));
  409. /* Replace the cause code with the "Unrecognized parameter"
  410. * parameter type.
  411. */
  412. sctp_addto_chunk(repl, len, unk_param);
  413. sctp_chunk_free(err_chunk);
  414. }
  415. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
  416. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  417. /*
  418. * Note: After sending out INIT ACK with the State Cookie parameter,
  419. * "Z" MUST NOT allocate any resources, nor keep any states for the
  420. * new association. Otherwise, "Z" will be vulnerable to resource
  421. * attacks.
  422. */
  423. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  424. return SCTP_DISPOSITION_DELETE_TCB;
  425. nomem_init:
  426. sctp_association_free(new_asoc);
  427. nomem:
  428. if (err_chunk)
  429. sctp_chunk_free(err_chunk);
  430. return SCTP_DISPOSITION_NOMEM;
  431. }
  432. /*
  433. * Respond to a normal INIT ACK chunk.
  434. * We are the side that is initiating the association.
  435. *
  436. * Section: 5.1 Normal Establishment of an Association, C
  437. * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
  438. * timer and leave COOKIE-WAIT state. "A" shall then send the State
  439. * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
  440. * the T1-cookie timer, and enter the COOKIE-ECHOED state.
  441. *
  442. * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
  443. * DATA chunks, but it MUST be the first chunk in the packet and
  444. * until the COOKIE ACK is returned the sender MUST NOT send any
  445. * other packets to the peer.
  446. *
  447. * Verification Tag: 3.3.3
  448. * If the value of the Initiate Tag in a received INIT ACK chunk is
  449. * found to be 0, the receiver MUST treat it as an error and close the
  450. * association by transmitting an ABORT.
  451. *
  452. * Inputs
  453. * (endpoint, asoc, chunk)
  454. *
  455. * Outputs
  456. * (asoc, reply_msg, msg_up, timers, counters)
  457. *
  458. * The return value is the disposition of the chunk.
  459. */
  460. enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
  461. const struct sctp_endpoint *ep,
  462. const struct sctp_association *asoc,
  463. const union sctp_subtype type,
  464. void *arg,
  465. struct sctp_cmd_seq *commands)
  466. {
  467. struct sctp_init_chunk *initchunk;
  468. struct sctp_chunk *chunk = arg;
  469. struct sctp_chunk *err_chunk;
  470. struct sctp_packet *packet;
  471. if (!sctp_vtag_verify(chunk, asoc))
  472. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  473. /* 6.10 Bundling
  474. * An endpoint MUST NOT bundle INIT, INIT ACK or
  475. * SHUTDOWN COMPLETE with any other chunks.
  476. */
  477. if (!chunk->singleton)
  478. return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
  479. /* Make sure that the INIT-ACK chunk has a valid length */
  480. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
  481. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  482. commands);
  483. /* Grab the INIT header. */
  484. chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
  485. /* Verify the INIT chunk before processing it. */
  486. err_chunk = NULL;
  487. if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
  488. (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
  489. &err_chunk)) {
  490. enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
  491. /* This chunk contains fatal error. It is to be discarded.
  492. * Send an ABORT, with causes. If there are no causes,
  493. * then there wasn't enough memory. Just terminate
  494. * the association.
  495. */
  496. if (err_chunk) {
  497. packet = sctp_abort_pkt_new(net, ep, asoc, arg,
  498. (__u8 *)(err_chunk->chunk_hdr) +
  499. sizeof(struct sctp_chunkhdr),
  500. ntohs(err_chunk->chunk_hdr->length) -
  501. sizeof(struct sctp_chunkhdr));
  502. sctp_chunk_free(err_chunk);
  503. if (packet) {
  504. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  505. SCTP_PACKET(packet));
  506. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  507. error = SCTP_ERROR_INV_PARAM;
  508. }
  509. }
  510. /* SCTP-AUTH, Section 6.3:
  511. * It should be noted that if the receiver wants to tear
  512. * down an association in an authenticated way only, the
  513. * handling of malformed packets should not result in
  514. * tearing down the association.
  515. *
  516. * This means that if we only want to abort associations
  517. * in an authenticated way (i.e AUTH+ABORT), then we
  518. * can't destroy this association just because the packet
  519. * was malformed.
  520. */
  521. if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
  522. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  523. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  524. return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
  525. asoc, chunk->transport);
  526. }
  527. /* Tag the variable length parameters. Note that we never
  528. * convert the parameters in an INIT chunk.
  529. */
  530. chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
  531. initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
  532. sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
  533. SCTP_PEER_INIT(initchunk));
  534. /* Reset init error count upon receipt of INIT-ACK. */
  535. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
  536. /* 5.1 C) "A" shall stop the T1-init timer and leave
  537. * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
  538. * timer, and enter the COOKIE-ECHOED state.
  539. */
  540. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  541. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  542. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  543. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  544. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  545. SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
  546. /* SCTP-AUTH: generate the association shared keys so that
  547. * we can potentially sign the COOKIE-ECHO.
  548. */
  549. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
  550. /* 5.1 C) "A" shall then send the State Cookie received in the
  551. * INIT ACK chunk in a COOKIE ECHO chunk, ...
  552. */
  553. /* If there is any errors to report, send the ERROR chunk generated
  554. * for unknown parameters as well.
  555. */
  556. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
  557. SCTP_CHUNK(err_chunk));
  558. return SCTP_DISPOSITION_CONSUME;
  559. }
  560. static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
  561. const struct sctp_association *asoc)
  562. {
  563. struct sctp_chunk auth;
  564. if (!chunk->auth_chunk)
  565. return true;
  566. /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
  567. * is supposed to be authenticated and we have to do delayed
  568. * authentication. We've just recreated the association using
  569. * the information in the cookie and now it's much easier to
  570. * do the authentication.
  571. */
  572. /* Make sure that we and the peer are AUTH capable */
  573. if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
  574. return false;
  575. /* set-up our fake chunk so that we can process it */
  576. auth.skb = chunk->auth_chunk;
  577. auth.asoc = chunk->asoc;
  578. auth.sctp_hdr = chunk->sctp_hdr;
  579. auth.chunk_hdr = (struct sctp_chunkhdr *)
  580. skb_push(chunk->auth_chunk,
  581. sizeof(struct sctp_chunkhdr));
  582. skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
  583. auth.transport = chunk->transport;
  584. return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
  585. }
  586. /*
  587. * Respond to a normal COOKIE ECHO chunk.
  588. * We are the side that is being asked for an association.
  589. *
  590. * Section: 5.1 Normal Establishment of an Association, D
  591. * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
  592. * with a COOKIE ACK chunk after building a TCB and moving to
  593. * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
  594. * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
  595. * chunk MUST be the first chunk in the packet.
  596. *
  597. * IMPLEMENTATION NOTE: An implementation may choose to send the
  598. * Communication Up notification to the SCTP user upon reception
  599. * of a valid COOKIE ECHO chunk.
  600. *
  601. * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
  602. * D) Rules for packet carrying a COOKIE ECHO
  603. *
  604. * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
  605. * Initial Tag received in the INIT ACK.
  606. *
  607. * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
  608. *
  609. * Inputs
  610. * (endpoint, asoc, chunk)
  611. *
  612. * Outputs
  613. * (asoc, reply_msg, msg_up, timers, counters)
  614. *
  615. * The return value is the disposition of the chunk.
  616. */
  617. enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
  618. const struct sctp_endpoint *ep,
  619. const struct sctp_association *asoc,
  620. const union sctp_subtype type,
  621. void *arg,
  622. struct sctp_cmd_seq *commands)
  623. {
  624. struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
  625. struct sctp_association *new_asoc;
  626. struct sctp_init_chunk *peer_init;
  627. struct sctp_chunk *chunk = arg;
  628. struct sctp_chunk *err_chk_p;
  629. struct sctp_chunk *repl;
  630. struct sock *sk;
  631. int error = 0;
  632. if (asoc && !sctp_vtag_verify(chunk, asoc))
  633. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  634. /* If the packet is an OOTB packet which is temporarily on the
  635. * control endpoint, respond with an ABORT.
  636. */
  637. if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
  638. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  639. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  640. }
  641. /* Make sure that the COOKIE_ECHO chunk has a valid length.
  642. * In this case, we check that we have enough for at least a
  643. * chunk header. More detailed verification is done
  644. * in sctp_unpack_cookie().
  645. */
  646. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  647. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  648. commands);
  649. /* If the endpoint is not listening or if the number of associations
  650. * on the TCP-style socket exceed the max backlog, respond with an
  651. * ABORT.
  652. */
  653. sk = ep->base.sk;
  654. if (!sctp_sstate(sk, LISTENING) ||
  655. (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
  656. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  657. /* "Decode" the chunk. We have no optional parameters so we
  658. * are in good shape.
  659. */
  660. chunk->subh.cookie_hdr =
  661. (struct sctp_signed_cookie *)chunk->skb->data;
  662. if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
  663. sizeof(struct sctp_chunkhdr)))
  664. goto nomem;
  665. /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
  666. * "Z" will reply with a COOKIE ACK chunk after building a TCB
  667. * and moving to the ESTABLISHED state.
  668. */
  669. new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
  670. &err_chk_p);
  671. /* FIXME:
  672. * If the re-build failed, what is the proper error path
  673. * from here?
  674. *
  675. * [We should abort the association. --piggy]
  676. */
  677. if (!new_asoc) {
  678. /* FIXME: Several errors are possible. A bad cookie should
  679. * be silently discarded, but think about logging it too.
  680. */
  681. switch (error) {
  682. case -SCTP_IERROR_NOMEM:
  683. goto nomem;
  684. case -SCTP_IERROR_STALE_COOKIE:
  685. sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
  686. err_chk_p);
  687. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  688. case -SCTP_IERROR_BAD_SIG:
  689. default:
  690. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  691. }
  692. }
  693. if (security_sctp_assoc_request(new_asoc, chunk->head_skb ?: chunk->skb)) {
  694. sctp_association_free(new_asoc);
  695. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  696. }
  697. /* Delay state machine commands until later.
  698. *
  699. * Re-build the bind address for the association is done in
  700. * the sctp_unpack_cookie() already.
  701. */
  702. /* This is a brand-new association, so these are not yet side
  703. * effects--it is safe to run them here.
  704. */
  705. peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
  706. if (!sctp_process_init(new_asoc, chunk,
  707. &chunk->subh.cookie_hdr->c.peer_addr,
  708. peer_init, GFP_ATOMIC))
  709. goto nomem_init;
  710. /* SCTP-AUTH: Now that we've populate required fields in
  711. * sctp_process_init, set up the association shared keys as
  712. * necessary so that we can potentially authenticate the ACK
  713. */
  714. error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
  715. if (error)
  716. goto nomem_init;
  717. if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
  718. sctp_association_free(new_asoc);
  719. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  720. }
  721. repl = sctp_make_cookie_ack(new_asoc, chunk);
  722. if (!repl)
  723. goto nomem_init;
  724. /* RFC 2960 5.1 Normal Establishment of an Association
  725. *
  726. * D) IMPLEMENTATION NOTE: An implementation may choose to
  727. * send the Communication Up notification to the SCTP user
  728. * upon reception of a valid COOKIE ECHO chunk.
  729. */
  730. ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
  731. new_asoc->c.sinit_num_ostreams,
  732. new_asoc->c.sinit_max_instreams,
  733. NULL, GFP_ATOMIC);
  734. if (!ev)
  735. goto nomem_ev;
  736. /* Sockets API Draft Section 5.3.1.6
  737. * When a peer sends a Adaptation Layer Indication parameter , SCTP
  738. * delivers this notification to inform the application that of the
  739. * peers requested adaptation layer.
  740. */
  741. if (new_asoc->peer.adaptation_ind) {
  742. ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
  743. GFP_ATOMIC);
  744. if (!ai_ev)
  745. goto nomem_aiev;
  746. }
  747. if (!new_asoc->peer.auth_capable) {
  748. auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
  749. SCTP_AUTH_NO_AUTH,
  750. GFP_ATOMIC);
  751. if (!auth_ev)
  752. goto nomem_authev;
  753. }
  754. /* Add all the state machine commands now since we've created
  755. * everything. This way we don't introduce memory corruptions
  756. * during side-effect processing and correctly count established
  757. * associations.
  758. */
  759. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
  760. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  761. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  762. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  763. SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
  764. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
  765. if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  766. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  767. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  768. /* This will send the COOKIE ACK */
  769. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  770. /* Queue the ASSOC_CHANGE event */
  771. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  772. /* Send up the Adaptation Layer Indication event */
  773. if (ai_ev)
  774. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  775. SCTP_ULPEVENT(ai_ev));
  776. if (auth_ev)
  777. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  778. SCTP_ULPEVENT(auth_ev));
  779. return SCTP_DISPOSITION_CONSUME;
  780. nomem_authev:
  781. sctp_ulpevent_free(ai_ev);
  782. nomem_aiev:
  783. sctp_ulpevent_free(ev);
  784. nomem_ev:
  785. sctp_chunk_free(repl);
  786. nomem_init:
  787. sctp_association_free(new_asoc);
  788. nomem:
  789. return SCTP_DISPOSITION_NOMEM;
  790. }
  791. /*
  792. * Respond to a normal COOKIE ACK chunk.
  793. * We are the side that is asking for an association.
  794. *
  795. * RFC 2960 5.1 Normal Establishment of an Association
  796. *
  797. * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
  798. * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
  799. * timer. It may also notify its ULP about the successful
  800. * establishment of the association with a Communication Up
  801. * notification (see Section 10).
  802. *
  803. * Verification Tag:
  804. * Inputs
  805. * (endpoint, asoc, chunk)
  806. *
  807. * Outputs
  808. * (asoc, reply_msg, msg_up, timers, counters)
  809. *
  810. * The return value is the disposition of the chunk.
  811. */
  812. enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
  813. const struct sctp_endpoint *ep,
  814. const struct sctp_association *asoc,
  815. const union sctp_subtype type,
  816. void *arg,
  817. struct sctp_cmd_seq *commands)
  818. {
  819. struct sctp_chunk *chunk = arg;
  820. struct sctp_ulpevent *ev;
  821. if (!sctp_vtag_verify(chunk, asoc))
  822. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  823. /* Set peer label for connection. */
  824. if (security_sctp_assoc_established((struct sctp_association *)asoc,
  825. chunk->head_skb ?: chunk->skb))
  826. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  827. /* Verify that the chunk length for the COOKIE-ACK is OK.
  828. * If we don't do this, any bundled chunks may be junked.
  829. */
  830. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  831. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  832. commands);
  833. /* Reset init error count upon receipt of COOKIE-ACK,
  834. * to avoid problems with the management of this
  835. * counter in stale cookie situations when a transition back
  836. * from the COOKIE-ECHOED state to the COOKIE-WAIT
  837. * state is performed.
  838. */
  839. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
  840. /* RFC 2960 5.1 Normal Establishment of an Association
  841. *
  842. * E) Upon reception of the COOKIE ACK, endpoint "A" will move
  843. * from the COOKIE-ECHOED state to the ESTABLISHED state,
  844. * stopping the T1-cookie timer.
  845. */
  846. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  847. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  848. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  849. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  850. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  851. SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
  852. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
  853. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  854. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  855. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  856. /* It may also notify its ULP about the successful
  857. * establishment of the association with a Communication Up
  858. * notification (see Section 10).
  859. */
  860. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
  861. 0, asoc->c.sinit_num_ostreams,
  862. asoc->c.sinit_max_instreams,
  863. NULL, GFP_ATOMIC);
  864. if (!ev)
  865. goto nomem;
  866. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  867. /* Sockets API Draft Section 5.3.1.6
  868. * When a peer sends a Adaptation Layer Indication parameter , SCTP
  869. * delivers this notification to inform the application that of the
  870. * peers requested adaptation layer.
  871. */
  872. if (asoc->peer.adaptation_ind) {
  873. ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
  874. if (!ev)
  875. goto nomem;
  876. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  877. SCTP_ULPEVENT(ev));
  878. }
  879. if (!asoc->peer.auth_capable) {
  880. ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
  881. GFP_ATOMIC);
  882. if (!ev)
  883. goto nomem;
  884. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  885. SCTP_ULPEVENT(ev));
  886. }
  887. return SCTP_DISPOSITION_CONSUME;
  888. nomem:
  889. return SCTP_DISPOSITION_NOMEM;
  890. }
  891. /* Generate and sendout a heartbeat packet. */
  892. static enum sctp_disposition sctp_sf_heartbeat(
  893. const struct sctp_endpoint *ep,
  894. const struct sctp_association *asoc,
  895. const union sctp_subtype type,
  896. void *arg,
  897. struct sctp_cmd_seq *commands)
  898. {
  899. struct sctp_transport *transport = (struct sctp_transport *) arg;
  900. struct sctp_chunk *reply;
  901. /* Send a heartbeat to our peer. */
  902. reply = sctp_make_heartbeat(asoc, transport, 0);
  903. if (!reply)
  904. return SCTP_DISPOSITION_NOMEM;
  905. /* Set rto_pending indicating that an RTT measurement
  906. * is started with this heartbeat chunk.
  907. */
  908. sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
  909. SCTP_TRANSPORT(transport));
  910. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  911. return SCTP_DISPOSITION_CONSUME;
  912. }
  913. /* Generate a HEARTBEAT packet on the given transport. */
  914. enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
  915. const struct sctp_endpoint *ep,
  916. const struct sctp_association *asoc,
  917. const union sctp_subtype type,
  918. void *arg,
  919. struct sctp_cmd_seq *commands)
  920. {
  921. struct sctp_transport *transport = (struct sctp_transport *) arg;
  922. if (asoc->overall_error_count >= asoc->max_retrans) {
  923. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  924. SCTP_ERROR(ETIMEDOUT));
  925. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  926. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  927. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  928. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  929. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  930. return SCTP_DISPOSITION_DELETE_TCB;
  931. }
  932. /* Section 3.3.5.
  933. * The Sender-specific Heartbeat Info field should normally include
  934. * information about the sender's current time when this HEARTBEAT
  935. * chunk is sent and the destination transport address to which this
  936. * HEARTBEAT is sent (see Section 8.3).
  937. */
  938. if (transport->param_flags & SPP_HB_ENABLE) {
  939. if (SCTP_DISPOSITION_NOMEM ==
  940. sctp_sf_heartbeat(ep, asoc, type, arg,
  941. commands))
  942. return SCTP_DISPOSITION_NOMEM;
  943. /* Set transport error counter and association error counter
  944. * when sending heartbeat.
  945. */
  946. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
  947. SCTP_TRANSPORT(transport));
  948. }
  949. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
  950. SCTP_TRANSPORT(transport));
  951. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
  952. SCTP_TRANSPORT(transport));
  953. return SCTP_DISPOSITION_CONSUME;
  954. }
  955. /* resend asoc strreset_chunk. */
  956. enum sctp_disposition sctp_sf_send_reconf(struct net *net,
  957. const struct sctp_endpoint *ep,
  958. const struct sctp_association *asoc,
  959. const union sctp_subtype type,
  960. void *arg,
  961. struct sctp_cmd_seq *commands)
  962. {
  963. struct sctp_transport *transport = arg;
  964. if (asoc->overall_error_count >= asoc->max_retrans) {
  965. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  966. SCTP_ERROR(ETIMEDOUT));
  967. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  968. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  969. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  970. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  971. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  972. return SCTP_DISPOSITION_DELETE_TCB;
  973. }
  974. sctp_chunk_hold(asoc->strreset_chunk);
  975. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  976. SCTP_CHUNK(asoc->strreset_chunk));
  977. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
  978. return SCTP_DISPOSITION_CONSUME;
  979. }
  980. /* send hb chunk with padding for PLPMUTD. */
  981. enum sctp_disposition sctp_sf_send_probe(struct net *net,
  982. const struct sctp_endpoint *ep,
  983. const struct sctp_association *asoc,
  984. const union sctp_subtype type,
  985. void *arg,
  986. struct sctp_cmd_seq *commands)
  987. {
  988. struct sctp_transport *transport = (struct sctp_transport *)arg;
  989. struct sctp_chunk *reply;
  990. if (!sctp_transport_pl_enabled(transport))
  991. return SCTP_DISPOSITION_CONSUME;
  992. sctp_transport_pl_send(transport);
  993. reply = sctp_make_heartbeat(asoc, transport, transport->pl.probe_size);
  994. if (!reply)
  995. return SCTP_DISPOSITION_NOMEM;
  996. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  997. sctp_add_cmd_sf(commands, SCTP_CMD_PROBE_TIMER_UPDATE,
  998. SCTP_TRANSPORT(transport));
  999. return SCTP_DISPOSITION_CONSUME;
  1000. }
  1001. /*
  1002. * Process an heartbeat request.
  1003. *
  1004. * Section: 8.3 Path Heartbeat
  1005. * The receiver of the HEARTBEAT should immediately respond with a
  1006. * HEARTBEAT ACK that contains the Heartbeat Information field copied
  1007. * from the received HEARTBEAT chunk.
  1008. *
  1009. * Verification Tag: 8.5 Verification Tag [Normal verification]
  1010. * When receiving an SCTP packet, the endpoint MUST ensure that the
  1011. * value in the Verification Tag field of the received SCTP packet
  1012. * matches its own Tag. If the received Verification Tag value does not
  1013. * match the receiver's own tag value, the receiver shall silently
  1014. * discard the packet and shall not process it any further except for
  1015. * those cases listed in Section 8.5.1 below.
  1016. *
  1017. * Inputs
  1018. * (endpoint, asoc, chunk)
  1019. *
  1020. * Outputs
  1021. * (asoc, reply_msg, msg_up, timers, counters)
  1022. *
  1023. * The return value is the disposition of the chunk.
  1024. */
  1025. enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
  1026. const struct sctp_endpoint *ep,
  1027. const struct sctp_association *asoc,
  1028. const union sctp_subtype type,
  1029. void *arg, struct sctp_cmd_seq *commands)
  1030. {
  1031. struct sctp_paramhdr *param_hdr;
  1032. struct sctp_chunk *chunk = arg;
  1033. struct sctp_chunk *reply;
  1034. size_t paylen = 0;
  1035. if (!sctp_vtag_verify(chunk, asoc))
  1036. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1037. /* Make sure that the HEARTBEAT chunk has a valid length. */
  1038. if (!sctp_chunk_length_valid(chunk,
  1039. sizeof(struct sctp_heartbeat_chunk)))
  1040. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  1041. commands);
  1042. /* 8.3 The receiver of the HEARTBEAT should immediately
  1043. * respond with a HEARTBEAT ACK that contains the Heartbeat
  1044. * Information field copied from the received HEARTBEAT chunk.
  1045. */
  1046. chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
  1047. param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
  1048. paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
  1049. if (ntohs(param_hdr->length) > paylen)
  1050. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  1051. param_hdr, commands);
  1052. if (!pskb_pull(chunk->skb, paylen))
  1053. goto nomem;
  1054. reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
  1055. if (!reply)
  1056. goto nomem;
  1057. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  1058. return SCTP_DISPOSITION_CONSUME;
  1059. nomem:
  1060. return SCTP_DISPOSITION_NOMEM;
  1061. }
  1062. /*
  1063. * Process the returning HEARTBEAT ACK.
  1064. *
  1065. * Section: 8.3 Path Heartbeat
  1066. * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
  1067. * should clear the error counter of the destination transport
  1068. * address to which the HEARTBEAT was sent, and mark the destination
  1069. * transport address as active if it is not so marked. The endpoint may
  1070. * optionally report to the upper layer when an inactive destination
  1071. * address is marked as active due to the reception of the latest
  1072. * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
  1073. * clear the association overall error count as well (as defined
  1074. * in section 8.1).
  1075. *
  1076. * The receiver of the HEARTBEAT ACK should also perform an RTT
  1077. * measurement for that destination transport address using the time
  1078. * value carried in the HEARTBEAT ACK chunk.
  1079. *
  1080. * Verification Tag: 8.5 Verification Tag [Normal verification]
  1081. *
  1082. * Inputs
  1083. * (endpoint, asoc, chunk)
  1084. *
  1085. * Outputs
  1086. * (asoc, reply_msg, msg_up, timers, counters)
  1087. *
  1088. * The return value is the disposition of the chunk.
  1089. */
  1090. enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
  1091. const struct sctp_endpoint *ep,
  1092. const struct sctp_association *asoc,
  1093. const union sctp_subtype type,
  1094. void *arg,
  1095. struct sctp_cmd_seq *commands)
  1096. {
  1097. struct sctp_sender_hb_info *hbinfo;
  1098. struct sctp_chunk *chunk = arg;
  1099. struct sctp_transport *link;
  1100. unsigned long max_interval;
  1101. union sctp_addr from_addr;
  1102. if (!sctp_vtag_verify(chunk, asoc))
  1103. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1104. /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
  1105. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
  1106. sizeof(*hbinfo)))
  1107. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  1108. commands);
  1109. hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
  1110. /* Make sure that the length of the parameter is what we expect */
  1111. if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
  1112. return SCTP_DISPOSITION_DISCARD;
  1113. from_addr = hbinfo->daddr;
  1114. link = sctp_assoc_lookup_paddr(asoc, &from_addr);
  1115. /* This should never happen, but lets log it if so. */
  1116. if (unlikely(!link)) {
  1117. if (from_addr.sa.sa_family == AF_INET6) {
  1118. net_warn_ratelimited("%s association %p could not find address %pI6\n",
  1119. __func__,
  1120. asoc,
  1121. &from_addr.v6.sin6_addr);
  1122. } else {
  1123. net_warn_ratelimited("%s association %p could not find address %pI4\n",
  1124. __func__,
  1125. asoc,
  1126. &from_addr.v4.sin_addr.s_addr);
  1127. }
  1128. return SCTP_DISPOSITION_DISCARD;
  1129. }
  1130. /* Validate the 64-bit random nonce. */
  1131. if (hbinfo->hb_nonce != link->hb_nonce)
  1132. return SCTP_DISPOSITION_DISCARD;
  1133. if (hbinfo->probe_size) {
  1134. if (hbinfo->probe_size != link->pl.probe_size ||
  1135. !sctp_transport_pl_enabled(link))
  1136. return SCTP_DISPOSITION_DISCARD;
  1137. if (sctp_transport_pl_recv(link))
  1138. return SCTP_DISPOSITION_CONSUME;
  1139. return sctp_sf_send_probe(net, ep, asoc, type, link, commands);
  1140. }
  1141. max_interval = link->hbinterval + link->rto;
  1142. /* Check if the timestamp looks valid. */
  1143. if (time_after(hbinfo->sent_at, jiffies) ||
  1144. time_after(jiffies, hbinfo->sent_at + max_interval)) {
  1145. pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
  1146. "for transport:%p\n", __func__, link);
  1147. return SCTP_DISPOSITION_DISCARD;
  1148. }
  1149. /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
  1150. * the HEARTBEAT should clear the error counter of the
  1151. * destination transport address to which the HEARTBEAT was
  1152. * sent and mark the destination transport address as active if
  1153. * it is not so marked.
  1154. */
  1155. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
  1156. return SCTP_DISPOSITION_CONSUME;
  1157. }
  1158. /* Helper function to send out an abort for the restart
  1159. * condition.
  1160. */
  1161. static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
  1162. struct sctp_chunk *init,
  1163. struct sctp_cmd_seq *commands)
  1164. {
  1165. struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
  1166. union sctp_addr_param *addrparm;
  1167. struct sctp_errhdr *errhdr;
  1168. char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
  1169. struct sctp_endpoint *ep;
  1170. struct sctp_packet *pkt;
  1171. int len;
  1172. /* Build the error on the stack. We are way to malloc crazy
  1173. * throughout the code today.
  1174. */
  1175. errhdr = (struct sctp_errhdr *)buffer;
  1176. addrparm = (union sctp_addr_param *)errhdr->variable;
  1177. /* Copy into a parm format. */
  1178. len = af->to_addr_param(ssa, addrparm);
  1179. len += sizeof(*errhdr);
  1180. errhdr->cause = SCTP_ERROR_RESTART;
  1181. errhdr->length = htons(len);
  1182. /* Assign to the control socket. */
  1183. ep = sctp_sk(net->sctp.ctl_sock)->ep;
  1184. /* Association is NULL since this may be a restart attack and we
  1185. * want to send back the attacker's vtag.
  1186. */
  1187. pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
  1188. if (!pkt)
  1189. goto out;
  1190. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
  1191. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  1192. /* Discard the rest of the inbound packet. */
  1193. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  1194. out:
  1195. /* Even if there is no memory, treat as a failure so
  1196. * the packet will get dropped.
  1197. */
  1198. return 0;
  1199. }
  1200. static bool list_has_sctp_addr(const struct list_head *list,
  1201. union sctp_addr *ipaddr)
  1202. {
  1203. struct sctp_transport *addr;
  1204. list_for_each_entry(addr, list, transports) {
  1205. if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
  1206. return true;
  1207. }
  1208. return false;
  1209. }
  1210. /* A restart is occurring, check to make sure no new addresses
  1211. * are being added as we may be under a takeover attack.
  1212. */
  1213. static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
  1214. const struct sctp_association *asoc,
  1215. struct sctp_chunk *init,
  1216. struct sctp_cmd_seq *commands)
  1217. {
  1218. struct net *net = new_asoc->base.net;
  1219. struct sctp_transport *new_addr;
  1220. int ret = 1;
  1221. /* Implementor's Guide - Section 5.2.2
  1222. * ...
  1223. * Before responding the endpoint MUST check to see if the
  1224. * unexpected INIT adds new addresses to the association. If new
  1225. * addresses are added to the association, the endpoint MUST respond
  1226. * with an ABORT..
  1227. */
  1228. /* Search through all current addresses and make sure
  1229. * we aren't adding any new ones.
  1230. */
  1231. list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
  1232. transports) {
  1233. if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
  1234. &new_addr->ipaddr)) {
  1235. sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
  1236. commands);
  1237. ret = 0;
  1238. break;
  1239. }
  1240. }
  1241. /* Return success if all addresses were found. */
  1242. return ret;
  1243. }
  1244. /* Populate the verification/tie tags based on overlapping INIT
  1245. * scenario.
  1246. *
  1247. * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
  1248. */
  1249. static void sctp_tietags_populate(struct sctp_association *new_asoc,
  1250. const struct sctp_association *asoc)
  1251. {
  1252. switch (asoc->state) {
  1253. /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
  1254. case SCTP_STATE_COOKIE_WAIT:
  1255. new_asoc->c.my_vtag = asoc->c.my_vtag;
  1256. new_asoc->c.my_ttag = asoc->c.my_vtag;
  1257. new_asoc->c.peer_ttag = 0;
  1258. break;
  1259. case SCTP_STATE_COOKIE_ECHOED:
  1260. new_asoc->c.my_vtag = asoc->c.my_vtag;
  1261. new_asoc->c.my_ttag = asoc->c.my_vtag;
  1262. new_asoc->c.peer_ttag = asoc->c.peer_vtag;
  1263. break;
  1264. /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
  1265. * COOKIE-WAIT and SHUTDOWN-ACK-SENT
  1266. */
  1267. default:
  1268. new_asoc->c.my_ttag = asoc->c.my_vtag;
  1269. new_asoc->c.peer_ttag = asoc->c.peer_vtag;
  1270. break;
  1271. }
  1272. /* Other parameters for the endpoint SHOULD be copied from the
  1273. * existing parameters of the association (e.g. number of
  1274. * outbound streams) into the INIT ACK and cookie.
  1275. */
  1276. new_asoc->rwnd = asoc->rwnd;
  1277. new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
  1278. new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
  1279. new_asoc->c.initial_tsn = asoc->c.initial_tsn;
  1280. }
  1281. /*
  1282. * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
  1283. * handling action.
  1284. *
  1285. * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
  1286. *
  1287. * Returns value representing action to be taken. These action values
  1288. * correspond to Action/Description values in RFC 2960, Table 2.
  1289. */
  1290. static char sctp_tietags_compare(struct sctp_association *new_asoc,
  1291. const struct sctp_association *asoc)
  1292. {
  1293. /* In this case, the peer may have restarted. */
  1294. if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
  1295. (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
  1296. (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
  1297. (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
  1298. return 'A';
  1299. /* Collision case B. */
  1300. if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
  1301. ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
  1302. (0 == asoc->c.peer_vtag))) {
  1303. return 'B';
  1304. }
  1305. /* Collision case D. */
  1306. if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
  1307. (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
  1308. return 'D';
  1309. /* Collision case C. */
  1310. if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
  1311. (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
  1312. (0 == new_asoc->c.my_ttag) &&
  1313. (0 == new_asoc->c.peer_ttag))
  1314. return 'C';
  1315. /* No match to any of the special cases; discard this packet. */
  1316. return 'E';
  1317. }
  1318. /* Common helper routine for both duplicate and simultaneous INIT
  1319. * chunk handling.
  1320. */
  1321. static enum sctp_disposition sctp_sf_do_unexpected_init(
  1322. struct net *net,
  1323. const struct sctp_endpoint *ep,
  1324. const struct sctp_association *asoc,
  1325. const union sctp_subtype type,
  1326. void *arg,
  1327. struct sctp_cmd_seq *commands)
  1328. {
  1329. struct sctp_chunk *chunk = arg, *repl, *err_chunk;
  1330. struct sctp_unrecognized_param *unk_param;
  1331. struct sctp_association *new_asoc;
  1332. enum sctp_disposition retval;
  1333. struct sctp_packet *packet;
  1334. int len;
  1335. /* 6.10 Bundling
  1336. * An endpoint MUST NOT bundle INIT, INIT ACK or
  1337. * SHUTDOWN COMPLETE with any other chunks.
  1338. *
  1339. * IG Section 2.11.2
  1340. * Furthermore, we require that the receiver of an INIT chunk MUST
  1341. * enforce these rules by silently discarding an arriving packet
  1342. * with an INIT chunk that is bundled with other chunks.
  1343. */
  1344. if (!chunk->singleton)
  1345. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1346. /* Make sure that the INIT chunk has a valid length. */
  1347. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
  1348. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1349. /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
  1350. * Tag.
  1351. */
  1352. if (chunk->sctp_hdr->vtag != 0)
  1353. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  1354. if (SCTP_INPUT_CB(chunk->skb)->encap_port != chunk->transport->encap_port)
  1355. return sctp_sf_new_encap_port(net, ep, asoc, type, arg, commands);
  1356. /* Grab the INIT header. */
  1357. chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
  1358. /* Tag the variable length parameters. */
  1359. chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
  1360. /* Verify the INIT chunk before processing it. */
  1361. err_chunk = NULL;
  1362. if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
  1363. (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
  1364. &err_chunk)) {
  1365. /* This chunk contains fatal error. It is to be discarded.
  1366. * Send an ABORT, with causes if there is any.
  1367. */
  1368. if (err_chunk) {
  1369. packet = sctp_abort_pkt_new(net, ep, asoc, arg,
  1370. (__u8 *)(err_chunk->chunk_hdr) +
  1371. sizeof(struct sctp_chunkhdr),
  1372. ntohs(err_chunk->chunk_hdr->length) -
  1373. sizeof(struct sctp_chunkhdr));
  1374. if (packet) {
  1375. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  1376. SCTP_PACKET(packet));
  1377. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  1378. retval = SCTP_DISPOSITION_CONSUME;
  1379. } else {
  1380. retval = SCTP_DISPOSITION_NOMEM;
  1381. }
  1382. goto cleanup;
  1383. } else {
  1384. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
  1385. commands);
  1386. }
  1387. }
  1388. /*
  1389. * Other parameters for the endpoint SHOULD be copied from the
  1390. * existing parameters of the association (e.g. number of
  1391. * outbound streams) into the INIT ACK and cookie.
  1392. * FIXME: We are copying parameters from the endpoint not the
  1393. * association.
  1394. */
  1395. new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
  1396. if (!new_asoc)
  1397. goto nomem;
  1398. /* Update socket peer label if first association. */
  1399. if (security_sctp_assoc_request(new_asoc, chunk->skb)) {
  1400. sctp_association_free(new_asoc);
  1401. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1402. }
  1403. if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
  1404. sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
  1405. goto nomem;
  1406. /* In the outbound INIT ACK the endpoint MUST copy its current
  1407. * Verification Tag and Peers Verification tag into a reserved
  1408. * place (local tie-tag and per tie-tag) within the state cookie.
  1409. */
  1410. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
  1411. (struct sctp_init_chunk *)chunk->chunk_hdr,
  1412. GFP_ATOMIC))
  1413. goto nomem;
  1414. /* Make sure no new addresses are being added during the
  1415. * restart. Do not do this check for COOKIE-WAIT state,
  1416. * since there are no peer addresses to check against.
  1417. * Upon return an ABORT will have been sent if needed.
  1418. */
  1419. if (!sctp_state(asoc, COOKIE_WAIT)) {
  1420. if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
  1421. commands)) {
  1422. retval = SCTP_DISPOSITION_CONSUME;
  1423. goto nomem_retval;
  1424. }
  1425. }
  1426. sctp_tietags_populate(new_asoc, asoc);
  1427. /* B) "Z" shall respond immediately with an INIT ACK chunk. */
  1428. /* If there are errors need to be reported for unknown parameters,
  1429. * make sure to reserve enough room in the INIT ACK for them.
  1430. */
  1431. len = 0;
  1432. if (err_chunk) {
  1433. len = ntohs(err_chunk->chunk_hdr->length) -
  1434. sizeof(struct sctp_chunkhdr);
  1435. }
  1436. repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
  1437. if (!repl)
  1438. goto nomem;
  1439. /* If there are errors need to be reported for unknown parameters,
  1440. * include them in the outgoing INIT ACK as "Unrecognized parameter"
  1441. * parameter.
  1442. */
  1443. if (err_chunk) {
  1444. /* Get the "Unrecognized parameter" parameter(s) out of the
  1445. * ERROR chunk generated by sctp_verify_init(). Since the
  1446. * error cause code for "unknown parameter" and the
  1447. * "Unrecognized parameter" type is the same, we can
  1448. * construct the parameters in INIT ACK by copying the
  1449. * ERROR causes over.
  1450. */
  1451. unk_param = (struct sctp_unrecognized_param *)
  1452. ((__u8 *)(err_chunk->chunk_hdr) +
  1453. sizeof(struct sctp_chunkhdr));
  1454. /* Replace the cause code with the "Unrecognized parameter"
  1455. * parameter type.
  1456. */
  1457. sctp_addto_chunk(repl, len, unk_param);
  1458. }
  1459. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
  1460. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1461. /*
  1462. * Note: After sending out INIT ACK with the State Cookie parameter,
  1463. * "Z" MUST NOT allocate any resources for this new association.
  1464. * Otherwise, "Z" will be vulnerable to resource attacks.
  1465. */
  1466. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  1467. retval = SCTP_DISPOSITION_CONSUME;
  1468. return retval;
  1469. nomem:
  1470. retval = SCTP_DISPOSITION_NOMEM;
  1471. nomem_retval:
  1472. if (new_asoc)
  1473. sctp_association_free(new_asoc);
  1474. cleanup:
  1475. if (err_chunk)
  1476. sctp_chunk_free(err_chunk);
  1477. return retval;
  1478. }
  1479. /*
  1480. * Handle simultaneous INIT.
  1481. * This means we started an INIT and then we got an INIT request from
  1482. * our peer.
  1483. *
  1484. * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
  1485. * This usually indicates an initialization collision, i.e., each
  1486. * endpoint is attempting, at about the same time, to establish an
  1487. * association with the other endpoint.
  1488. *
  1489. * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
  1490. * endpoint MUST respond with an INIT ACK using the same parameters it
  1491. * sent in its original INIT chunk (including its Verification Tag,
  1492. * unchanged). These original parameters are combined with those from the
  1493. * newly received INIT chunk. The endpoint shall also generate a State
  1494. * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
  1495. * INIT to calculate the State Cookie.
  1496. *
  1497. * After that, the endpoint MUST NOT change its state, the T1-init
  1498. * timer shall be left running and the corresponding TCB MUST NOT be
  1499. * destroyed. The normal procedures for handling State Cookies when
  1500. * a TCB exists will resolve the duplicate INITs to a single association.
  1501. *
  1502. * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
  1503. * its Tie-Tags with the Tag information of itself and its peer (see
  1504. * section 5.2.2 for a description of the Tie-Tags).
  1505. *
  1506. * Verification Tag: Not explicit, but an INIT can not have a valid
  1507. * verification tag, so we skip the check.
  1508. *
  1509. * Inputs
  1510. * (endpoint, asoc, chunk)
  1511. *
  1512. * Outputs
  1513. * (asoc, reply_msg, msg_up, timers, counters)
  1514. *
  1515. * The return value is the disposition of the chunk.
  1516. */
  1517. enum sctp_disposition sctp_sf_do_5_2_1_siminit(
  1518. struct net *net,
  1519. const struct sctp_endpoint *ep,
  1520. const struct sctp_association *asoc,
  1521. const union sctp_subtype type,
  1522. void *arg,
  1523. struct sctp_cmd_seq *commands)
  1524. {
  1525. /* Call helper to do the real work for both simultaneous and
  1526. * duplicate INIT chunk handling.
  1527. */
  1528. return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
  1529. }
  1530. /*
  1531. * Handle duplicated INIT messages. These are usually delayed
  1532. * restransmissions.
  1533. *
  1534. * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
  1535. * COOKIE-ECHOED and COOKIE-WAIT
  1536. *
  1537. * Unless otherwise stated, upon reception of an unexpected INIT for
  1538. * this association, the endpoint shall generate an INIT ACK with a
  1539. * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
  1540. * current Verification Tag and peer's Verification Tag into a reserved
  1541. * place within the state cookie. We shall refer to these locations as
  1542. * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
  1543. * containing this INIT ACK MUST carry a Verification Tag value equal to
  1544. * the Initiation Tag found in the unexpected INIT. And the INIT ACK
  1545. * MUST contain a new Initiation Tag (randomly generated see Section
  1546. * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
  1547. * existing parameters of the association (e.g. number of outbound
  1548. * streams) into the INIT ACK and cookie.
  1549. *
  1550. * After sending out the INIT ACK, the endpoint shall take no further
  1551. * actions, i.e., the existing association, including its current state,
  1552. * and the corresponding TCB MUST NOT be changed.
  1553. *
  1554. * Note: Only when a TCB exists and the association is not in a COOKIE-
  1555. * WAIT state are the Tie-Tags populated. For a normal association INIT
  1556. * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
  1557. * set to 0 (indicating that no previous TCB existed). The INIT ACK and
  1558. * State Cookie are populated as specified in section 5.2.1.
  1559. *
  1560. * Verification Tag: Not specified, but an INIT has no way of knowing
  1561. * what the verification tag could be, so we ignore it.
  1562. *
  1563. * Inputs
  1564. * (endpoint, asoc, chunk)
  1565. *
  1566. * Outputs
  1567. * (asoc, reply_msg, msg_up, timers, counters)
  1568. *
  1569. * The return value is the disposition of the chunk.
  1570. */
  1571. enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
  1572. struct net *net,
  1573. const struct sctp_endpoint *ep,
  1574. const struct sctp_association *asoc,
  1575. const union sctp_subtype type,
  1576. void *arg,
  1577. struct sctp_cmd_seq *commands)
  1578. {
  1579. /* Call helper to do the real work for both simultaneous and
  1580. * duplicate INIT chunk handling.
  1581. */
  1582. return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
  1583. }
  1584. /*
  1585. * Unexpected INIT-ACK handler.
  1586. *
  1587. * Section 5.2.3
  1588. * If an INIT ACK received by an endpoint in any state other than the
  1589. * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
  1590. * An unexpected INIT ACK usually indicates the processing of an old or
  1591. * duplicated INIT chunk.
  1592. */
  1593. enum sctp_disposition sctp_sf_do_5_2_3_initack(
  1594. struct net *net,
  1595. const struct sctp_endpoint *ep,
  1596. const struct sctp_association *asoc,
  1597. const union sctp_subtype type,
  1598. void *arg,
  1599. struct sctp_cmd_seq *commands)
  1600. {
  1601. /* Per the above section, we'll discard the chunk if we have an
  1602. * endpoint. If this is an OOTB INIT-ACK, treat it as such.
  1603. */
  1604. if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
  1605. return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
  1606. else
  1607. return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
  1608. }
  1609. static int sctp_sf_do_assoc_update(struct sctp_association *asoc,
  1610. struct sctp_association *new,
  1611. struct sctp_cmd_seq *cmds)
  1612. {
  1613. struct net *net = asoc->base.net;
  1614. struct sctp_chunk *abort;
  1615. if (!sctp_assoc_update(asoc, new))
  1616. return 0;
  1617. abort = sctp_make_abort(asoc, NULL, sizeof(struct sctp_errhdr));
  1618. if (abort) {
  1619. sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
  1620. sctp_add_cmd_sf(cmds, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  1621. }
  1622. sctp_add_cmd_sf(cmds, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNABORTED));
  1623. sctp_add_cmd_sf(cmds, SCTP_CMD_ASSOC_FAILED,
  1624. SCTP_PERR(SCTP_ERROR_RSRC_LOW));
  1625. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  1626. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  1627. return -ENOMEM;
  1628. }
  1629. /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
  1630. *
  1631. * Section 5.2.4
  1632. * A) In this case, the peer may have restarted.
  1633. */
  1634. static enum sctp_disposition sctp_sf_do_dupcook_a(
  1635. struct net *net,
  1636. const struct sctp_endpoint *ep,
  1637. const struct sctp_association *asoc,
  1638. struct sctp_chunk *chunk,
  1639. struct sctp_cmd_seq *commands,
  1640. struct sctp_association *new_asoc)
  1641. {
  1642. struct sctp_init_chunk *peer_init;
  1643. enum sctp_disposition disposition;
  1644. struct sctp_ulpevent *ev;
  1645. struct sctp_chunk *repl;
  1646. struct sctp_chunk *err;
  1647. /* new_asoc is a brand-new association, so these are not yet
  1648. * side effects--it is safe to run them here.
  1649. */
  1650. peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
  1651. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
  1652. GFP_ATOMIC))
  1653. goto nomem;
  1654. if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
  1655. goto nomem;
  1656. if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
  1657. return SCTP_DISPOSITION_DISCARD;
  1658. /* Make sure no new addresses are being added during the
  1659. * restart. Though this is a pretty complicated attack
  1660. * since you'd have to get inside the cookie.
  1661. */
  1662. if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
  1663. return SCTP_DISPOSITION_CONSUME;
  1664. /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
  1665. * the peer has restarted (Action A), it MUST NOT setup a new
  1666. * association but instead resend the SHUTDOWN ACK and send an ERROR
  1667. * chunk with a "Cookie Received while Shutting Down" error cause to
  1668. * its peer.
  1669. */
  1670. if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
  1671. disposition = __sctp_sf_do_9_2_reshutack(net, ep, asoc,
  1672. SCTP_ST_CHUNK(chunk->chunk_hdr->type),
  1673. chunk, commands);
  1674. if (SCTP_DISPOSITION_NOMEM == disposition)
  1675. goto nomem;
  1676. err = sctp_make_op_error(asoc, chunk,
  1677. SCTP_ERROR_COOKIE_IN_SHUTDOWN,
  1678. NULL, 0, 0);
  1679. if (err)
  1680. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  1681. SCTP_CHUNK(err));
  1682. return SCTP_DISPOSITION_CONSUME;
  1683. }
  1684. /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
  1685. * data. Consider the optional choice of resending of this data.
  1686. */
  1687. sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
  1688. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1689. SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
  1690. sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
  1691. /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
  1692. * and ASCONF-ACK cache.
  1693. */
  1694. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1695. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  1696. sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
  1697. /* Update the content of current association. */
  1698. if (sctp_sf_do_assoc_update((struct sctp_association *)asoc, new_asoc, commands))
  1699. goto nomem;
  1700. repl = sctp_make_cookie_ack(asoc, chunk);
  1701. if (!repl)
  1702. goto nomem;
  1703. /* Report association restart to upper layer. */
  1704. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
  1705. asoc->c.sinit_num_ostreams,
  1706. asoc->c.sinit_max_instreams,
  1707. NULL, GFP_ATOMIC);
  1708. if (!ev)
  1709. goto nomem_ev;
  1710. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  1711. if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
  1712. sctp_state(asoc, SHUTDOWN_SENT)) &&
  1713. (sctp_sstate(asoc->base.sk, CLOSING) ||
  1714. sock_flag(asoc->base.sk, SOCK_DEAD))) {
  1715. /* If the socket has been closed by user, don't
  1716. * transition to ESTABLISHED. Instead trigger SHUTDOWN
  1717. * bundled with COOKIE_ACK.
  1718. */
  1719. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1720. return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
  1721. SCTP_ST_CHUNK(0), repl,
  1722. commands);
  1723. } else {
  1724. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  1725. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  1726. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1727. }
  1728. return SCTP_DISPOSITION_CONSUME;
  1729. nomem_ev:
  1730. sctp_chunk_free(repl);
  1731. nomem:
  1732. return SCTP_DISPOSITION_NOMEM;
  1733. }
  1734. /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
  1735. *
  1736. * Section 5.2.4
  1737. * B) In this case, both sides may be attempting to start an association
  1738. * at about the same time but the peer endpoint started its INIT
  1739. * after responding to the local endpoint's INIT
  1740. */
  1741. /* This case represents an initialization collision. */
  1742. static enum sctp_disposition sctp_sf_do_dupcook_b(
  1743. struct net *net,
  1744. const struct sctp_endpoint *ep,
  1745. const struct sctp_association *asoc,
  1746. struct sctp_chunk *chunk,
  1747. struct sctp_cmd_seq *commands,
  1748. struct sctp_association *new_asoc)
  1749. {
  1750. struct sctp_init_chunk *peer_init;
  1751. struct sctp_chunk *repl;
  1752. /* new_asoc is a brand-new association, so these are not yet
  1753. * side effects--it is safe to run them here.
  1754. */
  1755. peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
  1756. if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
  1757. GFP_ATOMIC))
  1758. goto nomem;
  1759. if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
  1760. goto nomem;
  1761. if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
  1762. return SCTP_DISPOSITION_DISCARD;
  1763. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  1764. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  1765. if (asoc->state < SCTP_STATE_ESTABLISHED)
  1766. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  1767. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
  1768. /* Update the content of current association. */
  1769. if (sctp_sf_do_assoc_update((struct sctp_association *)asoc, new_asoc, commands))
  1770. goto nomem;
  1771. repl = sctp_make_cookie_ack(asoc, chunk);
  1772. if (!repl)
  1773. goto nomem;
  1774. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1775. /* RFC 2960 5.1 Normal Establishment of an Association
  1776. *
  1777. * D) IMPLEMENTATION NOTE: An implementation may choose to
  1778. * send the Communication Up notification to the SCTP user
  1779. * upon reception of a valid COOKIE ECHO chunk.
  1780. *
  1781. * Sadly, this needs to be implemented as a side-effect, because
  1782. * we are not guaranteed to have set the association id of the real
  1783. * association and so these notifications need to be delayed until
  1784. * the association id is allocated.
  1785. */
  1786. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
  1787. /* Sockets API Draft Section 5.3.1.6
  1788. * When a peer sends a Adaptation Layer Indication parameter , SCTP
  1789. * delivers this notification to inform the application that of the
  1790. * peers requested adaptation layer.
  1791. *
  1792. * This also needs to be done as a side effect for the same reason as
  1793. * above.
  1794. */
  1795. if (asoc->peer.adaptation_ind)
  1796. sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
  1797. if (!asoc->peer.auth_capable)
  1798. sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
  1799. return SCTP_DISPOSITION_CONSUME;
  1800. nomem:
  1801. return SCTP_DISPOSITION_NOMEM;
  1802. }
  1803. /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
  1804. *
  1805. * Section 5.2.4
  1806. * C) In this case, the local endpoint's cookie has arrived late.
  1807. * Before it arrived, the local endpoint sent an INIT and received an
  1808. * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
  1809. * but a new tag of its own.
  1810. */
  1811. /* This case represents an initialization collision. */
  1812. static enum sctp_disposition sctp_sf_do_dupcook_c(
  1813. struct net *net,
  1814. const struct sctp_endpoint *ep,
  1815. const struct sctp_association *asoc,
  1816. struct sctp_chunk *chunk,
  1817. struct sctp_cmd_seq *commands,
  1818. struct sctp_association *new_asoc)
  1819. {
  1820. /* The cookie should be silently discarded.
  1821. * The endpoint SHOULD NOT change states and should leave
  1822. * any timers running.
  1823. */
  1824. return SCTP_DISPOSITION_DISCARD;
  1825. }
  1826. /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
  1827. *
  1828. * Section 5.2.4
  1829. *
  1830. * D) When both local and remote tags match the endpoint should always
  1831. * enter the ESTABLISHED state, if it has not already done so.
  1832. */
  1833. /* This case represents an initialization collision. */
  1834. static enum sctp_disposition sctp_sf_do_dupcook_d(
  1835. struct net *net,
  1836. const struct sctp_endpoint *ep,
  1837. const struct sctp_association *asoc,
  1838. struct sctp_chunk *chunk,
  1839. struct sctp_cmd_seq *commands,
  1840. struct sctp_association *new_asoc)
  1841. {
  1842. struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
  1843. struct sctp_chunk *repl;
  1844. /* Clarification from Implementor's Guide:
  1845. * D) When both local and remote tags match the endpoint should
  1846. * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
  1847. * It should stop any cookie timer that may be running and send
  1848. * a COOKIE ACK.
  1849. */
  1850. if (!sctp_auth_chunk_verify(net, chunk, asoc))
  1851. return SCTP_DISPOSITION_DISCARD;
  1852. /* Don't accidentally move back into established state. */
  1853. if (asoc->state < SCTP_STATE_ESTABLISHED) {
  1854. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  1855. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  1856. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  1857. SCTP_STATE(SCTP_STATE_ESTABLISHED));
  1858. SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
  1859. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
  1860. SCTP_NULL());
  1861. /* RFC 2960 5.1 Normal Establishment of an Association
  1862. *
  1863. * D) IMPLEMENTATION NOTE: An implementation may choose
  1864. * to send the Communication Up notification to the
  1865. * SCTP user upon reception of a valid COOKIE
  1866. * ECHO chunk.
  1867. */
  1868. ev = sctp_ulpevent_make_assoc_change(asoc, 0,
  1869. SCTP_COMM_UP, 0,
  1870. asoc->c.sinit_num_ostreams,
  1871. asoc->c.sinit_max_instreams,
  1872. NULL, GFP_ATOMIC);
  1873. if (!ev)
  1874. goto nomem;
  1875. /* Sockets API Draft Section 5.3.1.6
  1876. * When a peer sends a Adaptation Layer Indication parameter,
  1877. * SCTP delivers this notification to inform the application
  1878. * that of the peers requested adaptation layer.
  1879. */
  1880. if (asoc->peer.adaptation_ind) {
  1881. ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
  1882. GFP_ATOMIC);
  1883. if (!ai_ev)
  1884. goto nomem;
  1885. }
  1886. if (!asoc->peer.auth_capable) {
  1887. auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
  1888. SCTP_AUTH_NO_AUTH,
  1889. GFP_ATOMIC);
  1890. if (!auth_ev)
  1891. goto nomem;
  1892. }
  1893. }
  1894. repl = sctp_make_cookie_ack(asoc, chunk);
  1895. if (!repl)
  1896. goto nomem;
  1897. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  1898. if (ev)
  1899. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  1900. SCTP_ULPEVENT(ev));
  1901. if (ai_ev)
  1902. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  1903. SCTP_ULPEVENT(ai_ev));
  1904. if (auth_ev)
  1905. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  1906. SCTP_ULPEVENT(auth_ev));
  1907. return SCTP_DISPOSITION_CONSUME;
  1908. nomem:
  1909. if (auth_ev)
  1910. sctp_ulpevent_free(auth_ev);
  1911. if (ai_ev)
  1912. sctp_ulpevent_free(ai_ev);
  1913. if (ev)
  1914. sctp_ulpevent_free(ev);
  1915. return SCTP_DISPOSITION_NOMEM;
  1916. }
  1917. /*
  1918. * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
  1919. * chunk was retransmitted and then delayed in the network.
  1920. *
  1921. * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
  1922. *
  1923. * Verification Tag: None. Do cookie validation.
  1924. *
  1925. * Inputs
  1926. * (endpoint, asoc, chunk)
  1927. *
  1928. * Outputs
  1929. * (asoc, reply_msg, msg_up, timers, counters)
  1930. *
  1931. * The return value is the disposition of the chunk.
  1932. */
  1933. enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
  1934. struct net *net,
  1935. const struct sctp_endpoint *ep,
  1936. const struct sctp_association *asoc,
  1937. const union sctp_subtype type,
  1938. void *arg,
  1939. struct sctp_cmd_seq *commands)
  1940. {
  1941. struct sctp_association *new_asoc;
  1942. struct sctp_chunk *chunk = arg;
  1943. enum sctp_disposition retval;
  1944. struct sctp_chunk *err_chk_p;
  1945. int error = 0;
  1946. char action;
  1947. /* Make sure that the chunk has a valid length from the protocol
  1948. * perspective. In this case check to make sure we have at least
  1949. * enough for the chunk header. Cookie length verification is
  1950. * done later.
  1951. */
  1952. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr))) {
  1953. if (!sctp_vtag_verify(chunk, asoc))
  1954. asoc = NULL;
  1955. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg, commands);
  1956. }
  1957. /* "Decode" the chunk. We have no optional parameters so we
  1958. * are in good shape.
  1959. */
  1960. chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
  1961. if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
  1962. sizeof(struct sctp_chunkhdr)))
  1963. goto nomem;
  1964. /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
  1965. * of a duplicate COOKIE ECHO match the Verification Tags of the
  1966. * current association, consider the State Cookie valid even if
  1967. * the lifespan is exceeded.
  1968. */
  1969. new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
  1970. &err_chk_p);
  1971. /* FIXME:
  1972. * If the re-build failed, what is the proper error path
  1973. * from here?
  1974. *
  1975. * [We should abort the association. --piggy]
  1976. */
  1977. if (!new_asoc) {
  1978. /* FIXME: Several errors are possible. A bad cookie should
  1979. * be silently discarded, but think about logging it too.
  1980. */
  1981. switch (error) {
  1982. case -SCTP_IERROR_NOMEM:
  1983. goto nomem;
  1984. case -SCTP_IERROR_STALE_COOKIE:
  1985. sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
  1986. err_chk_p);
  1987. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1988. case -SCTP_IERROR_BAD_SIG:
  1989. default:
  1990. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1991. }
  1992. }
  1993. /* Update socket peer label if first association. */
  1994. if (security_sctp_assoc_request(new_asoc, chunk->head_skb ?: chunk->skb)) {
  1995. sctp_association_free(new_asoc);
  1996. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  1997. }
  1998. /* Set temp so that it won't be added into hashtable */
  1999. new_asoc->temp = 1;
  2000. /* Compare the tie_tag in cookie with the verification tag of
  2001. * current association.
  2002. */
  2003. action = sctp_tietags_compare(new_asoc, asoc);
  2004. switch (action) {
  2005. case 'A': /* Association restart. */
  2006. retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
  2007. new_asoc);
  2008. break;
  2009. case 'B': /* Collision case B. */
  2010. retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
  2011. new_asoc);
  2012. break;
  2013. case 'C': /* Collision case C. */
  2014. retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
  2015. new_asoc);
  2016. break;
  2017. case 'D': /* Collision case D. */
  2018. retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
  2019. new_asoc);
  2020. break;
  2021. default: /* Discard packet for all others. */
  2022. retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2023. break;
  2024. }
  2025. /* Delete the temporary new association. */
  2026. sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
  2027. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  2028. /* Restore association pointer to provide SCTP command interpreter
  2029. * with a valid context in case it needs to manipulate
  2030. * the queues */
  2031. sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
  2032. SCTP_ASOC((struct sctp_association *)asoc));
  2033. return retval;
  2034. nomem:
  2035. return SCTP_DISPOSITION_NOMEM;
  2036. }
  2037. /*
  2038. * Process an ABORT. (SHUTDOWN-PENDING state)
  2039. *
  2040. * See sctp_sf_do_9_1_abort().
  2041. */
  2042. enum sctp_disposition sctp_sf_shutdown_pending_abort(
  2043. struct net *net,
  2044. const struct sctp_endpoint *ep,
  2045. const struct sctp_association *asoc,
  2046. const union sctp_subtype type,
  2047. void *arg,
  2048. struct sctp_cmd_seq *commands)
  2049. {
  2050. struct sctp_chunk *chunk = arg;
  2051. if (!sctp_vtag_verify_either(chunk, asoc))
  2052. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2053. /* Make sure that the ABORT chunk has a valid length.
  2054. * Since this is an ABORT chunk, we have to discard it
  2055. * because of the following text:
  2056. * RFC 2960, Section 3.3.7
  2057. * If an endpoint receives an ABORT with a format error or for an
  2058. * association that doesn't exist, it MUST silently discard it.
  2059. * Because the length is "invalid", we can't really discard just
  2060. * as we do not know its true length. So, to be safe, discard the
  2061. * packet.
  2062. */
  2063. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  2064. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2065. /* ADD-IP: Special case for ABORT chunks
  2066. * F4) One special consideration is that ABORT Chunks arriving
  2067. * destined to the IP address being deleted MUST be
  2068. * ignored (see Section 5.3.1 for further details).
  2069. */
  2070. if (SCTP_ADDR_DEL ==
  2071. sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
  2072. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2073. if (!sctp_err_chunk_valid(chunk))
  2074. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2075. return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
  2076. }
  2077. /*
  2078. * Process an ABORT. (SHUTDOWN-SENT state)
  2079. *
  2080. * See sctp_sf_do_9_1_abort().
  2081. */
  2082. enum sctp_disposition sctp_sf_shutdown_sent_abort(
  2083. struct net *net,
  2084. const struct sctp_endpoint *ep,
  2085. const struct sctp_association *asoc,
  2086. const union sctp_subtype type,
  2087. void *arg,
  2088. struct sctp_cmd_seq *commands)
  2089. {
  2090. struct sctp_chunk *chunk = arg;
  2091. if (!sctp_vtag_verify_either(chunk, asoc))
  2092. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2093. /* Make sure that the ABORT chunk has a valid length.
  2094. * Since this is an ABORT chunk, we have to discard it
  2095. * because of the following text:
  2096. * RFC 2960, Section 3.3.7
  2097. * If an endpoint receives an ABORT with a format error or for an
  2098. * association that doesn't exist, it MUST silently discard it.
  2099. * Because the length is "invalid", we can't really discard just
  2100. * as we do not know its true length. So, to be safe, discard the
  2101. * packet.
  2102. */
  2103. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  2104. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2105. /* ADD-IP: Special case for ABORT chunks
  2106. * F4) One special consideration is that ABORT Chunks arriving
  2107. * destined to the IP address being deleted MUST be
  2108. * ignored (see Section 5.3.1 for further details).
  2109. */
  2110. if (SCTP_ADDR_DEL ==
  2111. sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
  2112. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2113. if (!sctp_err_chunk_valid(chunk))
  2114. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2115. /* Stop the T2-shutdown timer. */
  2116. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2117. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2118. /* Stop the T5-shutdown guard timer. */
  2119. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2120. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  2121. return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
  2122. }
  2123. /*
  2124. * Process an ABORT. (SHUTDOWN-ACK-SENT state)
  2125. *
  2126. * See sctp_sf_do_9_1_abort().
  2127. */
  2128. enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
  2129. struct net *net,
  2130. const struct sctp_endpoint *ep,
  2131. const struct sctp_association *asoc,
  2132. const union sctp_subtype type,
  2133. void *arg,
  2134. struct sctp_cmd_seq *commands)
  2135. {
  2136. /* The same T2 timer, so we should be able to use
  2137. * common function with the SHUTDOWN-SENT state.
  2138. */
  2139. return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
  2140. }
  2141. /*
  2142. * Handle an Error received in COOKIE_ECHOED state.
  2143. *
  2144. * Only handle the error type of stale COOKIE Error, the other errors will
  2145. * be ignored.
  2146. *
  2147. * Inputs
  2148. * (endpoint, asoc, chunk)
  2149. *
  2150. * Outputs
  2151. * (asoc, reply_msg, msg_up, timers, counters)
  2152. *
  2153. * The return value is the disposition of the chunk.
  2154. */
  2155. enum sctp_disposition sctp_sf_cookie_echoed_err(
  2156. struct net *net,
  2157. const struct sctp_endpoint *ep,
  2158. const struct sctp_association *asoc,
  2159. const union sctp_subtype type,
  2160. void *arg,
  2161. struct sctp_cmd_seq *commands)
  2162. {
  2163. struct sctp_chunk *chunk = arg;
  2164. struct sctp_errhdr *err;
  2165. if (!sctp_vtag_verify(chunk, asoc))
  2166. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2167. /* Make sure that the ERROR chunk has a valid length.
  2168. * The parameter walking depends on this as well.
  2169. */
  2170. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
  2171. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2172. commands);
  2173. /* Process the error here */
  2174. /* FUTURE FIXME: When PR-SCTP related and other optional
  2175. * parms are emitted, this will have to change to handle multiple
  2176. * errors.
  2177. */
  2178. sctp_walk_errors(err, chunk->chunk_hdr) {
  2179. if (SCTP_ERROR_STALE_COOKIE == err->cause)
  2180. return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
  2181. arg, commands);
  2182. }
  2183. /* It is possible to have malformed error causes, and that
  2184. * will cause us to end the walk early. However, since
  2185. * we are discarding the packet, there should be no adverse
  2186. * affects.
  2187. */
  2188. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2189. }
  2190. /*
  2191. * Handle a Stale COOKIE Error
  2192. *
  2193. * Section: 5.2.6 Handle Stale COOKIE Error
  2194. * If the association is in the COOKIE-ECHOED state, the endpoint may elect
  2195. * one of the following three alternatives.
  2196. * ...
  2197. * 3) Send a new INIT chunk to the endpoint, adding a Cookie
  2198. * Preservative parameter requesting an extension to the lifetime of
  2199. * the State Cookie. When calculating the time extension, an
  2200. * implementation SHOULD use the RTT information measured based on the
  2201. * previous COOKIE ECHO / ERROR exchange, and should add no more
  2202. * than 1 second beyond the measured RTT, due to long State Cookie
  2203. * lifetimes making the endpoint more subject to a replay attack.
  2204. *
  2205. * Verification Tag: Not explicit, but safe to ignore.
  2206. *
  2207. * Inputs
  2208. * (endpoint, asoc, chunk)
  2209. *
  2210. * Outputs
  2211. * (asoc, reply_msg, msg_up, timers, counters)
  2212. *
  2213. * The return value is the disposition of the chunk.
  2214. */
  2215. static enum sctp_disposition sctp_sf_do_5_2_6_stale(
  2216. struct net *net,
  2217. const struct sctp_endpoint *ep,
  2218. const struct sctp_association *asoc,
  2219. const union sctp_subtype type,
  2220. void *arg,
  2221. struct sctp_cmd_seq *commands)
  2222. {
  2223. int attempts = asoc->init_err_counter + 1;
  2224. struct sctp_chunk *chunk = arg, *reply;
  2225. struct sctp_cookie_preserve_param bht;
  2226. struct sctp_bind_addr *bp;
  2227. struct sctp_errhdr *err;
  2228. u32 stale;
  2229. if (attempts > asoc->max_init_attempts) {
  2230. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  2231. SCTP_ERROR(ETIMEDOUT));
  2232. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  2233. SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
  2234. return SCTP_DISPOSITION_DELETE_TCB;
  2235. }
  2236. err = (struct sctp_errhdr *)(chunk->skb->data);
  2237. /* When calculating the time extension, an implementation
  2238. * SHOULD use the RTT information measured based on the
  2239. * previous COOKIE ECHO / ERROR exchange, and should add no
  2240. * more than 1 second beyond the measured RTT, due to long
  2241. * State Cookie lifetimes making the endpoint more subject to
  2242. * a replay attack.
  2243. * Measure of Staleness's unit is usec. (1/1000000 sec)
  2244. * Suggested Cookie Life-span Increment's unit is msec.
  2245. * (1/1000 sec)
  2246. * In general, if you use the suggested cookie life, the value
  2247. * found in the field of measure of staleness should be doubled
  2248. * to give ample time to retransmit the new cookie and thus
  2249. * yield a higher probability of success on the reattempt.
  2250. */
  2251. stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
  2252. stale = (stale * 2) / 1000;
  2253. bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
  2254. bht.param_hdr.length = htons(sizeof(bht));
  2255. bht.lifespan_increment = htonl(stale);
  2256. /* Build that new INIT chunk. */
  2257. bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
  2258. reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
  2259. if (!reply)
  2260. goto nomem;
  2261. sctp_addto_chunk(reply, sizeof(bht), &bht);
  2262. /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
  2263. sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
  2264. /* Stop pending T3-rtx and heartbeat timers */
  2265. sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
  2266. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  2267. /* Delete non-primary peer ip addresses since we are transitioning
  2268. * back to the COOKIE-WAIT state
  2269. */
  2270. sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
  2271. /* If we've sent any data bundled with COOKIE-ECHO we will need to
  2272. * resend
  2273. */
  2274. sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
  2275. SCTP_TRANSPORT(asoc->peer.primary_path));
  2276. /* Cast away the const modifier, as we want to just
  2277. * rerun it through as a sideffect.
  2278. */
  2279. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
  2280. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2281. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  2282. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2283. SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
  2284. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  2285. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  2286. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  2287. return SCTP_DISPOSITION_CONSUME;
  2288. nomem:
  2289. return SCTP_DISPOSITION_NOMEM;
  2290. }
  2291. /*
  2292. * Process an ABORT.
  2293. *
  2294. * Section: 9.1
  2295. * After checking the Verification Tag, the receiving endpoint shall
  2296. * remove the association from its record, and shall report the
  2297. * termination to its upper layer.
  2298. *
  2299. * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
  2300. * B) Rules for packet carrying ABORT:
  2301. *
  2302. * - The endpoint shall always fill in the Verification Tag field of the
  2303. * outbound packet with the destination endpoint's tag value if it
  2304. * is known.
  2305. *
  2306. * - If the ABORT is sent in response to an OOTB packet, the endpoint
  2307. * MUST follow the procedure described in Section 8.4.
  2308. *
  2309. * - The receiver MUST accept the packet if the Verification Tag
  2310. * matches either its own tag, OR the tag of its peer. Otherwise, the
  2311. * receiver MUST silently discard the packet and take no further
  2312. * action.
  2313. *
  2314. * Inputs
  2315. * (endpoint, asoc, chunk)
  2316. *
  2317. * Outputs
  2318. * (asoc, reply_msg, msg_up, timers, counters)
  2319. *
  2320. * The return value is the disposition of the chunk.
  2321. */
  2322. enum sctp_disposition sctp_sf_do_9_1_abort(
  2323. struct net *net,
  2324. const struct sctp_endpoint *ep,
  2325. const struct sctp_association *asoc,
  2326. const union sctp_subtype type,
  2327. void *arg,
  2328. struct sctp_cmd_seq *commands)
  2329. {
  2330. struct sctp_chunk *chunk = arg;
  2331. if (!sctp_vtag_verify_either(chunk, asoc))
  2332. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2333. /* Make sure that the ABORT chunk has a valid length.
  2334. * Since this is an ABORT chunk, we have to discard it
  2335. * because of the following text:
  2336. * RFC 2960, Section 3.3.7
  2337. * If an endpoint receives an ABORT with a format error or for an
  2338. * association that doesn't exist, it MUST silently discard it.
  2339. * Because the length is "invalid", we can't really discard just
  2340. * as we do not know its true length. So, to be safe, discard the
  2341. * packet.
  2342. */
  2343. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  2344. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2345. /* ADD-IP: Special case for ABORT chunks
  2346. * F4) One special consideration is that ABORT Chunks arriving
  2347. * destined to the IP address being deleted MUST be
  2348. * ignored (see Section 5.3.1 for further details).
  2349. */
  2350. if (SCTP_ADDR_DEL ==
  2351. sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
  2352. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2353. if (!sctp_err_chunk_valid(chunk))
  2354. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2355. return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
  2356. }
  2357. static enum sctp_disposition __sctp_sf_do_9_1_abort(
  2358. struct net *net,
  2359. const struct sctp_endpoint *ep,
  2360. const struct sctp_association *asoc,
  2361. const union sctp_subtype type,
  2362. void *arg,
  2363. struct sctp_cmd_seq *commands)
  2364. {
  2365. __be16 error = SCTP_ERROR_NO_ERROR;
  2366. struct sctp_chunk *chunk = arg;
  2367. unsigned int len;
  2368. /* See if we have an error cause code in the chunk. */
  2369. len = ntohs(chunk->chunk_hdr->length);
  2370. if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
  2371. error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
  2372. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
  2373. /* ASSOC_FAILED will DELETE_TCB. */
  2374. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
  2375. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  2376. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  2377. return SCTP_DISPOSITION_ABORT;
  2378. }
  2379. /*
  2380. * Process an ABORT. (COOKIE-WAIT state)
  2381. *
  2382. * See sctp_sf_do_9_1_abort() above.
  2383. */
  2384. enum sctp_disposition sctp_sf_cookie_wait_abort(
  2385. struct net *net,
  2386. const struct sctp_endpoint *ep,
  2387. const struct sctp_association *asoc,
  2388. const union sctp_subtype type,
  2389. void *arg,
  2390. struct sctp_cmd_seq *commands)
  2391. {
  2392. __be16 error = SCTP_ERROR_NO_ERROR;
  2393. struct sctp_chunk *chunk = arg;
  2394. unsigned int len;
  2395. if (!sctp_vtag_verify_either(chunk, asoc))
  2396. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2397. /* Make sure that the ABORT chunk has a valid length.
  2398. * Since this is an ABORT chunk, we have to discard it
  2399. * because of the following text:
  2400. * RFC 2960, Section 3.3.7
  2401. * If an endpoint receives an ABORT with a format error or for an
  2402. * association that doesn't exist, it MUST silently discard it.
  2403. * Because the length is "invalid", we can't really discard just
  2404. * as we do not know its true length. So, to be safe, discard the
  2405. * packet.
  2406. */
  2407. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
  2408. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2409. /* See if we have an error cause code in the chunk. */
  2410. len = ntohs(chunk->chunk_hdr->length);
  2411. if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
  2412. error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
  2413. return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
  2414. chunk->transport);
  2415. }
  2416. /*
  2417. * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
  2418. */
  2419. enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
  2420. struct net *net,
  2421. const struct sctp_endpoint *ep,
  2422. const struct sctp_association *asoc,
  2423. const union sctp_subtype type,
  2424. void *arg,
  2425. struct sctp_cmd_seq *commands)
  2426. {
  2427. return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
  2428. ENOPROTOOPT, asoc,
  2429. (struct sctp_transport *)arg);
  2430. }
  2431. /*
  2432. * Process an ABORT. (COOKIE-ECHOED state)
  2433. */
  2434. enum sctp_disposition sctp_sf_cookie_echoed_abort(
  2435. struct net *net,
  2436. const struct sctp_endpoint *ep,
  2437. const struct sctp_association *asoc,
  2438. const union sctp_subtype type,
  2439. void *arg,
  2440. struct sctp_cmd_seq *commands)
  2441. {
  2442. /* There is a single T1 timer, so we should be able to use
  2443. * common function with the COOKIE-WAIT state.
  2444. */
  2445. return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
  2446. }
  2447. /*
  2448. * Stop T1 timer and abort association with "INIT failed".
  2449. *
  2450. * This is common code called by several sctp_sf_*_abort() functions above.
  2451. */
  2452. static enum sctp_disposition sctp_stop_t1_and_abort(
  2453. struct net *net,
  2454. struct sctp_cmd_seq *commands,
  2455. __be16 error, int sk_err,
  2456. const struct sctp_association *asoc,
  2457. struct sctp_transport *transport)
  2458. {
  2459. pr_debug("%s: ABORT received (INIT)\n", __func__);
  2460. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2461. SCTP_STATE(SCTP_STATE_CLOSED));
  2462. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  2463. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  2464. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  2465. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
  2466. /* CMD_INIT_FAILED will DELETE_TCB. */
  2467. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  2468. SCTP_PERR(error));
  2469. return SCTP_DISPOSITION_ABORT;
  2470. }
  2471. /*
  2472. * sctp_sf_do_9_2_shut
  2473. *
  2474. * Section: 9.2
  2475. * Upon the reception of the SHUTDOWN, the peer endpoint shall
  2476. * - enter the SHUTDOWN-RECEIVED state,
  2477. *
  2478. * - stop accepting new data from its SCTP user
  2479. *
  2480. * - verify, by checking the Cumulative TSN Ack field of the chunk,
  2481. * that all its outstanding DATA chunks have been received by the
  2482. * SHUTDOWN sender.
  2483. *
  2484. * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
  2485. * send a SHUTDOWN in response to a ULP request. And should discard
  2486. * subsequent SHUTDOWN chunks.
  2487. *
  2488. * If there are still outstanding DATA chunks left, the SHUTDOWN
  2489. * receiver shall continue to follow normal data transmission
  2490. * procedures defined in Section 6 until all outstanding DATA chunks
  2491. * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
  2492. * new data from its SCTP user.
  2493. *
  2494. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2495. *
  2496. * Inputs
  2497. * (endpoint, asoc, chunk)
  2498. *
  2499. * Outputs
  2500. * (asoc, reply_msg, msg_up, timers, counters)
  2501. *
  2502. * The return value is the disposition of the chunk.
  2503. */
  2504. enum sctp_disposition sctp_sf_do_9_2_shutdown(
  2505. struct net *net,
  2506. const struct sctp_endpoint *ep,
  2507. const struct sctp_association *asoc,
  2508. const union sctp_subtype type,
  2509. void *arg,
  2510. struct sctp_cmd_seq *commands)
  2511. {
  2512. enum sctp_disposition disposition;
  2513. struct sctp_chunk *chunk = arg;
  2514. struct sctp_shutdownhdr *sdh;
  2515. struct sctp_ulpevent *ev;
  2516. __u32 ctsn;
  2517. if (!sctp_vtag_verify(chunk, asoc))
  2518. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2519. /* Make sure that the SHUTDOWN chunk has a valid length. */
  2520. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
  2521. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2522. commands);
  2523. /* Convert the elaborate header. */
  2524. sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
  2525. skb_pull(chunk->skb, sizeof(*sdh));
  2526. chunk->subh.shutdown_hdr = sdh;
  2527. ctsn = ntohl(sdh->cum_tsn_ack);
  2528. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  2529. pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
  2530. asoc->ctsn_ack_point);
  2531. return SCTP_DISPOSITION_DISCARD;
  2532. }
  2533. /* If Cumulative TSN Ack beyond the max tsn currently
  2534. * send, terminating the association and respond to the
  2535. * sender with an ABORT.
  2536. */
  2537. if (!TSN_lt(ctsn, asoc->next_tsn))
  2538. return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
  2539. /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
  2540. * When a peer sends a SHUTDOWN, SCTP delivers this notification to
  2541. * inform the application that it should cease sending data.
  2542. */
  2543. ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
  2544. if (!ev) {
  2545. disposition = SCTP_DISPOSITION_NOMEM;
  2546. goto out;
  2547. }
  2548. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  2549. /* Upon the reception of the SHUTDOWN, the peer endpoint shall
  2550. * - enter the SHUTDOWN-RECEIVED state,
  2551. * - stop accepting new data from its SCTP user
  2552. *
  2553. * [This is implicit in the new state.]
  2554. */
  2555. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  2556. SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
  2557. disposition = SCTP_DISPOSITION_CONSUME;
  2558. if (sctp_outq_is_empty(&asoc->outqueue)) {
  2559. disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
  2560. arg, commands);
  2561. }
  2562. if (SCTP_DISPOSITION_NOMEM == disposition)
  2563. goto out;
  2564. /* - verify, by checking the Cumulative TSN Ack field of the
  2565. * chunk, that all its outstanding DATA chunks have been
  2566. * received by the SHUTDOWN sender.
  2567. */
  2568. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
  2569. SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
  2570. out:
  2571. return disposition;
  2572. }
  2573. /*
  2574. * sctp_sf_do_9_2_shut_ctsn
  2575. *
  2576. * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
  2577. * it MUST NOT send a SHUTDOWN in response to a ULP request.
  2578. * The Cumulative TSN Ack of the received SHUTDOWN chunk
  2579. * MUST be processed.
  2580. */
  2581. enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
  2582. struct net *net,
  2583. const struct sctp_endpoint *ep,
  2584. const struct sctp_association *asoc,
  2585. const union sctp_subtype type,
  2586. void *arg,
  2587. struct sctp_cmd_seq *commands)
  2588. {
  2589. struct sctp_chunk *chunk = arg;
  2590. struct sctp_shutdownhdr *sdh;
  2591. __u32 ctsn;
  2592. if (!sctp_vtag_verify(chunk, asoc))
  2593. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2594. /* Make sure that the SHUTDOWN chunk has a valid length. */
  2595. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
  2596. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2597. commands);
  2598. sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
  2599. ctsn = ntohl(sdh->cum_tsn_ack);
  2600. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  2601. pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
  2602. asoc->ctsn_ack_point);
  2603. return SCTP_DISPOSITION_DISCARD;
  2604. }
  2605. /* If Cumulative TSN Ack beyond the max tsn currently
  2606. * send, terminating the association and respond to the
  2607. * sender with an ABORT.
  2608. */
  2609. if (!TSN_lt(ctsn, asoc->next_tsn))
  2610. return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
  2611. /* verify, by checking the Cumulative TSN Ack field of the
  2612. * chunk, that all its outstanding DATA chunks have been
  2613. * received by the SHUTDOWN sender.
  2614. */
  2615. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
  2616. SCTP_BE32(sdh->cum_tsn_ack));
  2617. return SCTP_DISPOSITION_CONSUME;
  2618. }
  2619. /* RFC 2960 9.2
  2620. * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
  2621. * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
  2622. * transport addresses (either in the IP addresses or in the INIT chunk)
  2623. * that belong to this association, it should discard the INIT chunk and
  2624. * retransmit the SHUTDOWN ACK chunk.
  2625. */
  2626. static enum sctp_disposition
  2627. __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
  2628. const struct sctp_association *asoc,
  2629. const union sctp_subtype type, void *arg,
  2630. struct sctp_cmd_seq *commands)
  2631. {
  2632. struct sctp_chunk *chunk = arg;
  2633. struct sctp_chunk *reply;
  2634. /* Make sure that the chunk has a valid length */
  2635. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  2636. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2637. commands);
  2638. /* Since we are not going to really process this INIT, there
  2639. * is no point in verifying chunk boundaries. Just generate
  2640. * the SHUTDOWN ACK.
  2641. */
  2642. reply = sctp_make_shutdown_ack(asoc, chunk);
  2643. if (NULL == reply)
  2644. goto nomem;
  2645. /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
  2646. * the T2-SHUTDOWN timer.
  2647. */
  2648. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  2649. /* and restart the T2-shutdown timer. */
  2650. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2651. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2652. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  2653. return SCTP_DISPOSITION_CONSUME;
  2654. nomem:
  2655. return SCTP_DISPOSITION_NOMEM;
  2656. }
  2657. enum sctp_disposition
  2658. sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
  2659. const struct sctp_association *asoc,
  2660. const union sctp_subtype type, void *arg,
  2661. struct sctp_cmd_seq *commands)
  2662. {
  2663. struct sctp_chunk *chunk = arg;
  2664. if (!chunk->singleton)
  2665. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2666. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
  2667. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2668. if (chunk->sctp_hdr->vtag != 0)
  2669. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  2670. return __sctp_sf_do_9_2_reshutack(net, ep, asoc, type, arg, commands);
  2671. }
  2672. /*
  2673. * sctp_sf_do_ecn_cwr
  2674. *
  2675. * Section: Appendix A: Explicit Congestion Notification
  2676. *
  2677. * CWR:
  2678. *
  2679. * RFC 2481 details a specific bit for a sender to send in the header of
  2680. * its next outbound TCP segment to indicate to its peer that it has
  2681. * reduced its congestion window. This is termed the CWR bit. For
  2682. * SCTP the same indication is made by including the CWR chunk.
  2683. * This chunk contains one data element, i.e. the TSN number that
  2684. * was sent in the ECNE chunk. This element represents the lowest
  2685. * TSN number in the datagram that was originally marked with the
  2686. * CE bit.
  2687. *
  2688. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2689. * Inputs
  2690. * (endpoint, asoc, chunk)
  2691. *
  2692. * Outputs
  2693. * (asoc, reply_msg, msg_up, timers, counters)
  2694. *
  2695. * The return value is the disposition of the chunk.
  2696. */
  2697. enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
  2698. const struct sctp_endpoint *ep,
  2699. const struct sctp_association *asoc,
  2700. const union sctp_subtype type,
  2701. void *arg,
  2702. struct sctp_cmd_seq *commands)
  2703. {
  2704. struct sctp_chunk *chunk = arg;
  2705. struct sctp_cwrhdr *cwr;
  2706. u32 lowest_tsn;
  2707. if (!sctp_vtag_verify(chunk, asoc))
  2708. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2709. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
  2710. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2711. commands);
  2712. cwr = (struct sctp_cwrhdr *)chunk->skb->data;
  2713. skb_pull(chunk->skb, sizeof(*cwr));
  2714. lowest_tsn = ntohl(cwr->lowest_tsn);
  2715. /* Does this CWR ack the last sent congestion notification? */
  2716. if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
  2717. /* Stop sending ECNE. */
  2718. sctp_add_cmd_sf(commands,
  2719. SCTP_CMD_ECN_CWR,
  2720. SCTP_U32(lowest_tsn));
  2721. }
  2722. return SCTP_DISPOSITION_CONSUME;
  2723. }
  2724. /*
  2725. * sctp_sf_do_ecne
  2726. *
  2727. * Section: Appendix A: Explicit Congestion Notification
  2728. *
  2729. * ECN-Echo
  2730. *
  2731. * RFC 2481 details a specific bit for a receiver to send back in its
  2732. * TCP acknowledgements to notify the sender of the Congestion
  2733. * Experienced (CE) bit having arrived from the network. For SCTP this
  2734. * same indication is made by including the ECNE chunk. This chunk
  2735. * contains one data element, i.e. the lowest TSN associated with the IP
  2736. * datagram marked with the CE bit.....
  2737. *
  2738. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2739. * Inputs
  2740. * (endpoint, asoc, chunk)
  2741. *
  2742. * Outputs
  2743. * (asoc, reply_msg, msg_up, timers, counters)
  2744. *
  2745. * The return value is the disposition of the chunk.
  2746. */
  2747. enum sctp_disposition sctp_sf_do_ecne(struct net *net,
  2748. const struct sctp_endpoint *ep,
  2749. const struct sctp_association *asoc,
  2750. const union sctp_subtype type,
  2751. void *arg, struct sctp_cmd_seq *commands)
  2752. {
  2753. struct sctp_chunk *chunk = arg;
  2754. struct sctp_ecnehdr *ecne;
  2755. if (!sctp_vtag_verify(chunk, asoc))
  2756. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2757. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
  2758. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2759. commands);
  2760. ecne = (struct sctp_ecnehdr *)chunk->skb->data;
  2761. skb_pull(chunk->skb, sizeof(*ecne));
  2762. /* If this is a newer ECNE than the last CWR packet we sent out */
  2763. sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
  2764. SCTP_U32(ntohl(ecne->lowest_tsn)));
  2765. return SCTP_DISPOSITION_CONSUME;
  2766. }
  2767. /*
  2768. * Section: 6.2 Acknowledgement on Reception of DATA Chunks
  2769. *
  2770. * The SCTP endpoint MUST always acknowledge the reception of each valid
  2771. * DATA chunk.
  2772. *
  2773. * The guidelines on delayed acknowledgement algorithm specified in
  2774. * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
  2775. * acknowledgement SHOULD be generated for at least every second packet
  2776. * (not every second DATA chunk) received, and SHOULD be generated within
  2777. * 200 ms of the arrival of any unacknowledged DATA chunk. In some
  2778. * situations it may be beneficial for an SCTP transmitter to be more
  2779. * conservative than the algorithms detailed in this document allow.
  2780. * However, an SCTP transmitter MUST NOT be more aggressive than the
  2781. * following algorithms allow.
  2782. *
  2783. * A SCTP receiver MUST NOT generate more than one SACK for every
  2784. * incoming packet, other than to update the offered window as the
  2785. * receiving application consumes new data.
  2786. *
  2787. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2788. *
  2789. * Inputs
  2790. * (endpoint, asoc, chunk)
  2791. *
  2792. * Outputs
  2793. * (asoc, reply_msg, msg_up, timers, counters)
  2794. *
  2795. * The return value is the disposition of the chunk.
  2796. */
  2797. enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
  2798. const struct sctp_endpoint *ep,
  2799. const struct sctp_association *asoc,
  2800. const union sctp_subtype type,
  2801. void *arg,
  2802. struct sctp_cmd_seq *commands)
  2803. {
  2804. union sctp_arg force = SCTP_NOFORCE();
  2805. struct sctp_chunk *chunk = arg;
  2806. int error;
  2807. if (!sctp_vtag_verify(chunk, asoc)) {
  2808. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2809. SCTP_NULL());
  2810. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2811. }
  2812. if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
  2813. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2814. commands);
  2815. error = sctp_eat_data(asoc, chunk, commands);
  2816. switch (error) {
  2817. case SCTP_IERROR_NO_ERROR:
  2818. break;
  2819. case SCTP_IERROR_HIGH_TSN:
  2820. case SCTP_IERROR_BAD_STREAM:
  2821. SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
  2822. goto discard_noforce;
  2823. case SCTP_IERROR_DUP_TSN:
  2824. case SCTP_IERROR_IGNORE_TSN:
  2825. SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
  2826. goto discard_force;
  2827. case SCTP_IERROR_NO_DATA:
  2828. return SCTP_DISPOSITION_ABORT;
  2829. case SCTP_IERROR_PROTO_VIOLATION:
  2830. return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
  2831. (u8 *)chunk->subh.data_hdr,
  2832. sctp_datahdr_len(&asoc->stream));
  2833. default:
  2834. BUG();
  2835. }
  2836. if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
  2837. force = SCTP_FORCE();
  2838. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
  2839. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2840. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  2841. }
  2842. /* If this is the last chunk in a packet, we need to count it
  2843. * toward sack generation. Note that we need to SACK every
  2844. * OTHER packet containing data chunks, EVEN IF WE DISCARD
  2845. * THEM. We elect to NOT generate SACK's if the chunk fails
  2846. * the verification tag test.
  2847. *
  2848. * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
  2849. *
  2850. * The SCTP endpoint MUST always acknowledge the reception of
  2851. * each valid DATA chunk.
  2852. *
  2853. * The guidelines on delayed acknowledgement algorithm
  2854. * specified in Section 4.2 of [RFC2581] SHOULD be followed.
  2855. * Specifically, an acknowledgement SHOULD be generated for at
  2856. * least every second packet (not every second DATA chunk)
  2857. * received, and SHOULD be generated within 200 ms of the
  2858. * arrival of any unacknowledged DATA chunk. In some
  2859. * situations it may be beneficial for an SCTP transmitter to
  2860. * be more conservative than the algorithms detailed in this
  2861. * document allow. However, an SCTP transmitter MUST NOT be
  2862. * more aggressive than the following algorithms allow.
  2863. */
  2864. if (chunk->end_of_packet)
  2865. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
  2866. return SCTP_DISPOSITION_CONSUME;
  2867. discard_force:
  2868. /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
  2869. *
  2870. * When a packet arrives with duplicate DATA chunk(s) and with
  2871. * no new DATA chunk(s), the endpoint MUST immediately send a
  2872. * SACK with no delay. If a packet arrives with duplicate
  2873. * DATA chunk(s) bundled with new DATA chunks, the endpoint
  2874. * MAY immediately send a SACK. Normally receipt of duplicate
  2875. * DATA chunks will occur when the original SACK chunk was lost
  2876. * and the peer's RTO has expired. The duplicate TSN number(s)
  2877. * SHOULD be reported in the SACK as duplicate.
  2878. */
  2879. /* In our case, we split the MAY SACK advice up whether or not
  2880. * the last chunk is a duplicate.'
  2881. */
  2882. if (chunk->end_of_packet)
  2883. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  2884. return SCTP_DISPOSITION_DISCARD;
  2885. discard_noforce:
  2886. if (chunk->end_of_packet)
  2887. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
  2888. return SCTP_DISPOSITION_DISCARD;
  2889. }
  2890. /*
  2891. * sctp_sf_eat_data_fast_4_4
  2892. *
  2893. * Section: 4 (4)
  2894. * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
  2895. * DATA chunks without delay.
  2896. *
  2897. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2898. * Inputs
  2899. * (endpoint, asoc, chunk)
  2900. *
  2901. * Outputs
  2902. * (asoc, reply_msg, msg_up, timers, counters)
  2903. *
  2904. * The return value is the disposition of the chunk.
  2905. */
  2906. enum sctp_disposition sctp_sf_eat_data_fast_4_4(
  2907. struct net *net,
  2908. const struct sctp_endpoint *ep,
  2909. const struct sctp_association *asoc,
  2910. const union sctp_subtype type,
  2911. void *arg,
  2912. struct sctp_cmd_seq *commands)
  2913. {
  2914. struct sctp_chunk *chunk = arg;
  2915. int error;
  2916. if (!sctp_vtag_verify(chunk, asoc)) {
  2917. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  2918. SCTP_NULL());
  2919. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  2920. }
  2921. if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
  2922. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  2923. commands);
  2924. error = sctp_eat_data(asoc, chunk, commands);
  2925. switch (error) {
  2926. case SCTP_IERROR_NO_ERROR:
  2927. case SCTP_IERROR_HIGH_TSN:
  2928. case SCTP_IERROR_DUP_TSN:
  2929. case SCTP_IERROR_IGNORE_TSN:
  2930. case SCTP_IERROR_BAD_STREAM:
  2931. break;
  2932. case SCTP_IERROR_NO_DATA:
  2933. return SCTP_DISPOSITION_ABORT;
  2934. case SCTP_IERROR_PROTO_VIOLATION:
  2935. return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
  2936. (u8 *)chunk->subh.data_hdr,
  2937. sctp_datahdr_len(&asoc->stream));
  2938. default:
  2939. BUG();
  2940. }
  2941. /* Go a head and force a SACK, since we are shutting down. */
  2942. /* Implementor's Guide.
  2943. *
  2944. * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
  2945. * respond to each received packet containing one or more DATA chunk(s)
  2946. * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
  2947. */
  2948. if (chunk->end_of_packet) {
  2949. /* We must delay the chunk creation since the cumulative
  2950. * TSN has not been updated yet.
  2951. */
  2952. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
  2953. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  2954. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  2955. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  2956. }
  2957. return SCTP_DISPOSITION_CONSUME;
  2958. }
  2959. /*
  2960. * Section: 6.2 Processing a Received SACK
  2961. * D) Any time a SACK arrives, the endpoint performs the following:
  2962. *
  2963. * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
  2964. * then drop the SACK. Since Cumulative TSN Ack is monotonically
  2965. * increasing, a SACK whose Cumulative TSN Ack is less than the
  2966. * Cumulative TSN Ack Point indicates an out-of-order SACK.
  2967. *
  2968. * ii) Set rwnd equal to the newly received a_rwnd minus the number
  2969. * of bytes still outstanding after processing the Cumulative TSN Ack
  2970. * and the Gap Ack Blocks.
  2971. *
  2972. * iii) If the SACK is missing a TSN that was previously
  2973. * acknowledged via a Gap Ack Block (e.g., the data receiver
  2974. * reneged on the data), then mark the corresponding DATA chunk
  2975. * as available for retransmit: Mark it as missing for fast
  2976. * retransmit as described in Section 7.2.4 and if no retransmit
  2977. * timer is running for the destination address to which the DATA
  2978. * chunk was originally transmitted, then T3-rtx is started for
  2979. * that destination address.
  2980. *
  2981. * Verification Tag: 8.5 Verification Tag [Normal verification]
  2982. *
  2983. * Inputs
  2984. * (endpoint, asoc, chunk)
  2985. *
  2986. * Outputs
  2987. * (asoc, reply_msg, msg_up, timers, counters)
  2988. *
  2989. * The return value is the disposition of the chunk.
  2990. */
  2991. enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
  2992. const struct sctp_endpoint *ep,
  2993. const struct sctp_association *asoc,
  2994. const union sctp_subtype type,
  2995. void *arg,
  2996. struct sctp_cmd_seq *commands)
  2997. {
  2998. struct sctp_chunk *chunk = arg;
  2999. struct sctp_sackhdr *sackh;
  3000. __u32 ctsn;
  3001. if (!sctp_vtag_verify(chunk, asoc))
  3002. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3003. /* Make sure that the SACK chunk has a valid length. */
  3004. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
  3005. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3006. commands);
  3007. /* Pull the SACK chunk from the data buffer */
  3008. sackh = sctp_sm_pull_sack(chunk);
  3009. /* Was this a bogus SACK? */
  3010. if (!sackh)
  3011. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3012. chunk->subh.sack_hdr = sackh;
  3013. ctsn = ntohl(sackh->cum_tsn_ack);
  3014. /* If Cumulative TSN Ack beyond the max tsn currently
  3015. * send, terminating the association and respond to the
  3016. * sender with an ABORT.
  3017. */
  3018. if (TSN_lte(asoc->next_tsn, ctsn))
  3019. return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
  3020. trace_sctp_probe(ep, asoc, chunk);
  3021. /* i) If Cumulative TSN Ack is less than the Cumulative TSN
  3022. * Ack Point, then drop the SACK. Since Cumulative TSN
  3023. * Ack is monotonically increasing, a SACK whose
  3024. * Cumulative TSN Ack is less than the Cumulative TSN Ack
  3025. * Point indicates an out-of-order SACK.
  3026. */
  3027. if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
  3028. pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
  3029. asoc->ctsn_ack_point);
  3030. return SCTP_DISPOSITION_DISCARD;
  3031. }
  3032. /* Return this SACK for further processing. */
  3033. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
  3034. /* Note: We do the rest of the work on the PROCESS_SACK
  3035. * sideeffect.
  3036. */
  3037. return SCTP_DISPOSITION_CONSUME;
  3038. }
  3039. /*
  3040. * Generate an ABORT in response to a packet.
  3041. *
  3042. * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
  3043. *
  3044. * 8) The receiver should respond to the sender of the OOTB packet with
  3045. * an ABORT. When sending the ABORT, the receiver of the OOTB packet
  3046. * MUST fill in the Verification Tag field of the outbound packet
  3047. * with the value found in the Verification Tag field of the OOTB
  3048. * packet and set the T-bit in the Chunk Flags to indicate that the
  3049. * Verification Tag is reflected. After sending this ABORT, the
  3050. * receiver of the OOTB packet shall discard the OOTB packet and take
  3051. * no further action.
  3052. *
  3053. * Verification Tag:
  3054. *
  3055. * The return value is the disposition of the chunk.
  3056. */
  3057. static enum sctp_disposition sctp_sf_tabort_8_4_8(
  3058. struct net *net,
  3059. const struct sctp_endpoint *ep,
  3060. const struct sctp_association *asoc,
  3061. const union sctp_subtype type,
  3062. void *arg,
  3063. struct sctp_cmd_seq *commands)
  3064. {
  3065. struct sctp_packet *packet = NULL;
  3066. struct sctp_chunk *chunk = arg;
  3067. struct sctp_chunk *abort;
  3068. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  3069. if (!packet)
  3070. return SCTP_DISPOSITION_NOMEM;
  3071. /* Make an ABORT. The T bit will be set if the asoc
  3072. * is NULL.
  3073. */
  3074. abort = sctp_make_abort(asoc, chunk, 0);
  3075. if (!abort) {
  3076. sctp_ootb_pkt_free(packet);
  3077. return SCTP_DISPOSITION_NOMEM;
  3078. }
  3079. /* Reflect vtag if T-Bit is set */
  3080. if (sctp_test_T_bit(abort))
  3081. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  3082. /* Set the skb to the belonging sock for accounting. */
  3083. abort->skb->sk = ep->base.sk;
  3084. sctp_packet_append_chunk(packet, abort);
  3085. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(packet));
  3086. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  3087. sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3088. return SCTP_DISPOSITION_CONSUME;
  3089. }
  3090. /* Handling of SCTP Packets Containing an INIT Chunk Matching an
  3091. * Existing Associations when the UDP encap port is incorrect.
  3092. *
  3093. * From Section 4 at draft-tuexen-tsvwg-sctp-udp-encaps-cons-03.
  3094. */
  3095. static enum sctp_disposition sctp_sf_new_encap_port(
  3096. struct net *net,
  3097. const struct sctp_endpoint *ep,
  3098. const struct sctp_association *asoc,
  3099. const union sctp_subtype type,
  3100. void *arg,
  3101. struct sctp_cmd_seq *commands)
  3102. {
  3103. struct sctp_packet *packet = NULL;
  3104. struct sctp_chunk *chunk = arg;
  3105. struct sctp_chunk *abort;
  3106. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  3107. if (!packet)
  3108. return SCTP_DISPOSITION_NOMEM;
  3109. abort = sctp_make_new_encap_port(asoc, chunk);
  3110. if (!abort) {
  3111. sctp_ootb_pkt_free(packet);
  3112. return SCTP_DISPOSITION_NOMEM;
  3113. }
  3114. abort->skb->sk = ep->base.sk;
  3115. sctp_packet_append_chunk(packet, abort);
  3116. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  3117. SCTP_PACKET(packet));
  3118. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  3119. sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3120. return SCTP_DISPOSITION_CONSUME;
  3121. }
  3122. /*
  3123. * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
  3124. * event as ULP notification for each cause included in the chunk.
  3125. *
  3126. * API 5.3.1.3 - SCTP_REMOTE_ERROR
  3127. *
  3128. * The return value is the disposition of the chunk.
  3129. */
  3130. enum sctp_disposition sctp_sf_operr_notify(struct net *net,
  3131. const struct sctp_endpoint *ep,
  3132. const struct sctp_association *asoc,
  3133. const union sctp_subtype type,
  3134. void *arg,
  3135. struct sctp_cmd_seq *commands)
  3136. {
  3137. struct sctp_chunk *chunk = arg;
  3138. struct sctp_errhdr *err;
  3139. if (!sctp_vtag_verify(chunk, asoc))
  3140. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3141. /* Make sure that the ERROR chunk has a valid length. */
  3142. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
  3143. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3144. commands);
  3145. sctp_walk_errors(err, chunk->chunk_hdr);
  3146. if ((void *)err != (void *)chunk->chunk_end)
  3147. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3148. (void *)err, commands);
  3149. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
  3150. SCTP_CHUNK(chunk));
  3151. return SCTP_DISPOSITION_CONSUME;
  3152. }
  3153. /*
  3154. * Process an inbound SHUTDOWN ACK.
  3155. *
  3156. * From Section 9.2:
  3157. * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
  3158. * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
  3159. * peer, and remove all record of the association.
  3160. *
  3161. * The return value is the disposition.
  3162. */
  3163. enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
  3164. const struct sctp_endpoint *ep,
  3165. const struct sctp_association *asoc,
  3166. const union sctp_subtype type,
  3167. void *arg,
  3168. struct sctp_cmd_seq *commands)
  3169. {
  3170. struct sctp_chunk *chunk = arg;
  3171. struct sctp_chunk *reply;
  3172. struct sctp_ulpevent *ev;
  3173. if (!sctp_vtag_verify(chunk, asoc))
  3174. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3175. /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
  3176. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3177. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3178. commands);
  3179. /* 10.2 H) SHUTDOWN COMPLETE notification
  3180. *
  3181. * When SCTP completes the shutdown procedures (section 9.2) this
  3182. * notification is passed to the upper layer.
  3183. */
  3184. ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
  3185. 0, 0, 0, NULL, GFP_ATOMIC);
  3186. if (!ev)
  3187. goto nomem;
  3188. /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
  3189. reply = sctp_make_shutdown_complete(asoc, chunk);
  3190. if (!reply)
  3191. goto nomem_chunk;
  3192. /* Do all the commands now (after allocation), so that we
  3193. * have consistent state if memory allocation fails
  3194. */
  3195. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
  3196. /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
  3197. * stop the T2-shutdown timer,
  3198. */
  3199. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3200. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3201. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3202. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  3203. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  3204. SCTP_STATE(SCTP_STATE_CLOSED));
  3205. SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
  3206. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3207. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  3208. /* ...and remove all record of the association. */
  3209. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  3210. return SCTP_DISPOSITION_DELETE_TCB;
  3211. nomem_chunk:
  3212. sctp_ulpevent_free(ev);
  3213. nomem:
  3214. return SCTP_DISPOSITION_NOMEM;
  3215. }
  3216. /*
  3217. * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
  3218. *
  3219. * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
  3220. * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
  3221. * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
  3222. * packet must fill in the Verification Tag field of the outbound
  3223. * packet with the Verification Tag received in the SHUTDOWN ACK and
  3224. * set the T-bit in the Chunk Flags to indicate that the Verification
  3225. * Tag is reflected.
  3226. *
  3227. * 8) The receiver should respond to the sender of the OOTB packet with
  3228. * an ABORT. When sending the ABORT, the receiver of the OOTB packet
  3229. * MUST fill in the Verification Tag field of the outbound packet
  3230. * with the value found in the Verification Tag field of the OOTB
  3231. * packet and set the T-bit in the Chunk Flags to indicate that the
  3232. * Verification Tag is reflected. After sending this ABORT, the
  3233. * receiver of the OOTB packet shall discard the OOTB packet and take
  3234. * no further action.
  3235. */
  3236. enum sctp_disposition sctp_sf_ootb(struct net *net,
  3237. const struct sctp_endpoint *ep,
  3238. const struct sctp_association *asoc,
  3239. const union sctp_subtype type,
  3240. void *arg, struct sctp_cmd_seq *commands)
  3241. {
  3242. struct sctp_chunk *chunk = arg;
  3243. struct sk_buff *skb = chunk->skb;
  3244. struct sctp_chunkhdr *ch;
  3245. struct sctp_errhdr *err;
  3246. int ootb_cookie_ack = 0;
  3247. int ootb_shut_ack = 0;
  3248. __u8 *ch_end;
  3249. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  3250. if (asoc && !sctp_vtag_verify(chunk, asoc))
  3251. asoc = NULL;
  3252. ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
  3253. do {
  3254. /* Report violation if the chunk is less then minimal */
  3255. if (ntohs(ch->length) < sizeof(*ch))
  3256. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3257. commands);
  3258. /* Report violation if chunk len overflows */
  3259. ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
  3260. if (ch_end > skb_tail_pointer(skb))
  3261. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3262. commands);
  3263. /* Now that we know we at least have a chunk header,
  3264. * do things that are type appropriate.
  3265. */
  3266. if (SCTP_CID_SHUTDOWN_ACK == ch->type)
  3267. ootb_shut_ack = 1;
  3268. /* RFC 2960, Section 3.3.7
  3269. * Moreover, under any circumstances, an endpoint that
  3270. * receives an ABORT MUST NOT respond to that ABORT by
  3271. * sending an ABORT of its own.
  3272. */
  3273. if (SCTP_CID_ABORT == ch->type)
  3274. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3275. /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
  3276. * or a COOKIE ACK the SCTP Packet should be silently
  3277. * discarded.
  3278. */
  3279. if (SCTP_CID_COOKIE_ACK == ch->type)
  3280. ootb_cookie_ack = 1;
  3281. if (SCTP_CID_ERROR == ch->type) {
  3282. sctp_walk_errors(err, ch) {
  3283. if (SCTP_ERROR_STALE_COOKIE == err->cause) {
  3284. ootb_cookie_ack = 1;
  3285. break;
  3286. }
  3287. }
  3288. }
  3289. ch = (struct sctp_chunkhdr *)ch_end;
  3290. } while (ch_end < skb_tail_pointer(skb));
  3291. if (ootb_shut_ack)
  3292. return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
  3293. else if (ootb_cookie_ack)
  3294. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3295. else
  3296. return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
  3297. }
  3298. /*
  3299. * Handle an "Out of the blue" SHUTDOWN ACK.
  3300. *
  3301. * Section: 8.4 5, sctpimpguide 2.41.
  3302. *
  3303. * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
  3304. * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
  3305. * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
  3306. * packet must fill in the Verification Tag field of the outbound
  3307. * packet with the Verification Tag received in the SHUTDOWN ACK and
  3308. * set the T-bit in the Chunk Flags to indicate that the Verification
  3309. * Tag is reflected.
  3310. *
  3311. * Inputs
  3312. * (endpoint, asoc, type, arg, commands)
  3313. *
  3314. * Outputs
  3315. * (enum sctp_disposition)
  3316. *
  3317. * The return value is the disposition of the chunk.
  3318. */
  3319. static enum sctp_disposition sctp_sf_shut_8_4_5(
  3320. struct net *net,
  3321. const struct sctp_endpoint *ep,
  3322. const struct sctp_association *asoc,
  3323. const union sctp_subtype type,
  3324. void *arg,
  3325. struct sctp_cmd_seq *commands)
  3326. {
  3327. struct sctp_packet *packet = NULL;
  3328. struct sctp_chunk *chunk = arg;
  3329. struct sctp_chunk *shut;
  3330. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  3331. if (!packet)
  3332. return SCTP_DISPOSITION_NOMEM;
  3333. /* Make an SHUTDOWN_COMPLETE.
  3334. * The T bit will be set if the asoc is NULL.
  3335. */
  3336. shut = sctp_make_shutdown_complete(asoc, chunk);
  3337. if (!shut) {
  3338. sctp_ootb_pkt_free(packet);
  3339. return SCTP_DISPOSITION_NOMEM;
  3340. }
  3341. /* Reflect vtag if T-Bit is set */
  3342. if (sctp_test_T_bit(shut))
  3343. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  3344. /* Set the skb to the belonging sock for accounting. */
  3345. shut->skb->sk = ep->base.sk;
  3346. sctp_packet_append_chunk(packet, shut);
  3347. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  3348. SCTP_PACKET(packet));
  3349. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  3350. /* We need to discard the rest of the packet to prevent
  3351. * potential boomming attacks from additional bundled chunks.
  3352. * This is documented in SCTP Threats ID.
  3353. */
  3354. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3355. }
  3356. /*
  3357. * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
  3358. *
  3359. * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
  3360. * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
  3361. * procedures in section 8.4 SHOULD be followed, in other words it
  3362. * should be treated as an Out Of The Blue packet.
  3363. * [This means that we do NOT check the Verification Tag on these
  3364. * chunks. --piggy ]
  3365. *
  3366. */
  3367. enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
  3368. const struct sctp_endpoint *ep,
  3369. const struct sctp_association *asoc,
  3370. const union sctp_subtype type,
  3371. void *arg,
  3372. struct sctp_cmd_seq *commands)
  3373. {
  3374. struct sctp_chunk *chunk = arg;
  3375. if (!sctp_vtag_verify(chunk, asoc))
  3376. asoc = NULL;
  3377. /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
  3378. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  3379. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3380. commands);
  3381. /* Although we do have an association in this case, it corresponds
  3382. * to a restarted association. So the packet is treated as an OOTB
  3383. * packet and the state function that handles OOTB SHUTDOWN_ACK is
  3384. * called with a NULL association.
  3385. */
  3386. SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
  3387. return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
  3388. }
  3389. /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
  3390. enum sctp_disposition sctp_sf_do_asconf(struct net *net,
  3391. const struct sctp_endpoint *ep,
  3392. const struct sctp_association *asoc,
  3393. const union sctp_subtype type,
  3394. void *arg,
  3395. struct sctp_cmd_seq *commands)
  3396. {
  3397. struct sctp_paramhdr *err_param = NULL;
  3398. struct sctp_chunk *asconf_ack = NULL;
  3399. struct sctp_chunk *chunk = arg;
  3400. struct sctp_addiphdr *hdr;
  3401. __u32 serial;
  3402. if (!sctp_vtag_verify(chunk, asoc)) {
  3403. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3404. SCTP_NULL());
  3405. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3406. }
  3407. /* Make sure that the ASCONF ADDIP chunk has a valid length. */
  3408. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
  3409. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3410. commands);
  3411. /* ADD-IP: Section 4.1.1
  3412. * This chunk MUST be sent in an authenticated way by using
  3413. * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
  3414. * is received unauthenticated it MUST be silently discarded as
  3415. * described in [I-D.ietf-tsvwg-sctp-auth].
  3416. */
  3417. if (!asoc->peer.asconf_capable ||
  3418. (!net->sctp.addip_noauth && !chunk->auth))
  3419. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3420. hdr = (struct sctp_addiphdr *)chunk->skb->data;
  3421. serial = ntohl(hdr->serial);
  3422. /* Verify the ASCONF chunk before processing it. */
  3423. if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
  3424. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3425. (void *)err_param, commands);
  3426. /* ADDIP 5.2 E1) Compare the value of the serial number to the value
  3427. * the endpoint stored in a new association variable
  3428. * 'Peer-Serial-Number'.
  3429. */
  3430. if (serial == asoc->peer.addip_serial + 1) {
  3431. /* If this is the first instance of ASCONF in the packet,
  3432. * we can clean our old ASCONF-ACKs.
  3433. */
  3434. if (!chunk->has_asconf)
  3435. sctp_assoc_clean_asconf_ack_cache(asoc);
  3436. /* ADDIP 5.2 E4) When the Sequence Number matches the next one
  3437. * expected, process the ASCONF as described below and after
  3438. * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
  3439. * the response packet and cache a copy of it (in the event it
  3440. * later needs to be retransmitted).
  3441. *
  3442. * Essentially, do V1-V5.
  3443. */
  3444. asconf_ack = sctp_process_asconf((struct sctp_association *)
  3445. asoc, chunk);
  3446. if (!asconf_ack)
  3447. return SCTP_DISPOSITION_NOMEM;
  3448. } else if (serial < asoc->peer.addip_serial + 1) {
  3449. /* ADDIP 5.2 E2)
  3450. * If the value found in the Sequence Number is less than the
  3451. * ('Peer- Sequence-Number' + 1), simply skip to the next
  3452. * ASCONF, and include in the outbound response packet
  3453. * any previously cached ASCONF-ACK response that was
  3454. * sent and saved that matches the Sequence Number of the
  3455. * ASCONF. Note: It is possible that no cached ASCONF-ACK
  3456. * Chunk exists. This will occur when an older ASCONF
  3457. * arrives out of order. In such a case, the receiver
  3458. * should skip the ASCONF Chunk and not include ASCONF-ACK
  3459. * Chunk for that chunk.
  3460. */
  3461. asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
  3462. if (!asconf_ack)
  3463. return SCTP_DISPOSITION_DISCARD;
  3464. /* Reset the transport so that we select the correct one
  3465. * this time around. This is to make sure that we don't
  3466. * accidentally use a stale transport that's been removed.
  3467. */
  3468. asconf_ack->transport = NULL;
  3469. } else {
  3470. /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
  3471. * it must be either a stale packet or from an attacker.
  3472. */
  3473. return SCTP_DISPOSITION_DISCARD;
  3474. }
  3475. /* ADDIP 5.2 E6) The destination address of the SCTP packet
  3476. * containing the ASCONF-ACK Chunks MUST be the source address of
  3477. * the SCTP packet that held the ASCONF Chunks.
  3478. *
  3479. * To do this properly, we'll set the destination address of the chunk
  3480. * and at the transmit time, will try look up the transport to use.
  3481. * Since ASCONFs may be bundled, the correct transport may not be
  3482. * created until we process the entire packet, thus this workaround.
  3483. */
  3484. asconf_ack->dest = chunk->source;
  3485. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
  3486. if (asoc->new_transport) {
  3487. sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
  3488. ((struct sctp_association *)asoc)->new_transport = NULL;
  3489. }
  3490. return SCTP_DISPOSITION_CONSUME;
  3491. }
  3492. static enum sctp_disposition sctp_send_next_asconf(
  3493. struct net *net,
  3494. const struct sctp_endpoint *ep,
  3495. struct sctp_association *asoc,
  3496. const union sctp_subtype type,
  3497. struct sctp_cmd_seq *commands)
  3498. {
  3499. struct sctp_chunk *asconf;
  3500. struct list_head *entry;
  3501. if (list_empty(&asoc->addip_chunk_list))
  3502. return SCTP_DISPOSITION_CONSUME;
  3503. entry = asoc->addip_chunk_list.next;
  3504. asconf = list_entry(entry, struct sctp_chunk, list);
  3505. list_del_init(entry);
  3506. sctp_chunk_hold(asconf);
  3507. asoc->addip_last_asconf = asconf;
  3508. return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
  3509. }
  3510. /*
  3511. * ADDIP Section 4.3 General rules for address manipulation
  3512. * When building TLV parameters for the ASCONF Chunk that will add or
  3513. * delete IP addresses the D0 to D13 rules should be applied:
  3514. */
  3515. enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
  3516. const struct sctp_endpoint *ep,
  3517. const struct sctp_association *asoc,
  3518. const union sctp_subtype type,
  3519. void *arg,
  3520. struct sctp_cmd_seq *commands)
  3521. {
  3522. struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
  3523. struct sctp_paramhdr *err_param = NULL;
  3524. struct sctp_chunk *asconf_ack = arg;
  3525. struct sctp_addiphdr *addip_hdr;
  3526. __u32 sent_serial, rcvd_serial;
  3527. struct sctp_chunk *abort;
  3528. if (!sctp_vtag_verify(asconf_ack, asoc)) {
  3529. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3530. SCTP_NULL());
  3531. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3532. }
  3533. /* Make sure that the ADDIP chunk has a valid length. */
  3534. if (!sctp_chunk_length_valid(asconf_ack,
  3535. sizeof(struct sctp_addip_chunk)))
  3536. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3537. commands);
  3538. /* ADD-IP, Section 4.1.2:
  3539. * This chunk MUST be sent in an authenticated way by using
  3540. * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
  3541. * is received unauthenticated it MUST be silently discarded as
  3542. * described in [I-D.ietf-tsvwg-sctp-auth].
  3543. */
  3544. if (!asoc->peer.asconf_capable ||
  3545. (!net->sctp.addip_noauth && !asconf_ack->auth))
  3546. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3547. addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
  3548. rcvd_serial = ntohl(addip_hdr->serial);
  3549. /* Verify the ASCONF-ACK chunk before processing it. */
  3550. if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
  3551. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3552. (void *)err_param, commands);
  3553. if (last_asconf) {
  3554. addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
  3555. sent_serial = ntohl(addip_hdr->serial);
  3556. } else {
  3557. sent_serial = asoc->addip_serial - 1;
  3558. }
  3559. /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
  3560. * equal to the next serial number to be used but no ASCONF chunk is
  3561. * outstanding the endpoint MUST ABORT the association. Note that a
  3562. * sequence number is greater than if it is no more than 2^^31-1
  3563. * larger than the current sequence number (using serial arithmetic).
  3564. */
  3565. if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
  3566. !(asoc->addip_last_asconf)) {
  3567. abort = sctp_make_abort(asoc, asconf_ack,
  3568. sizeof(struct sctp_errhdr));
  3569. if (abort) {
  3570. sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
  3571. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3572. SCTP_CHUNK(abort));
  3573. }
  3574. /* We are going to ABORT, so we might as well stop
  3575. * processing the rest of the chunks in the packet.
  3576. */
  3577. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3578. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  3579. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3580. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  3581. SCTP_ERROR(ECONNABORTED));
  3582. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3583. SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
  3584. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  3585. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3586. return SCTP_DISPOSITION_ABORT;
  3587. }
  3588. if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
  3589. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  3590. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  3591. if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
  3592. asconf_ack))
  3593. return sctp_send_next_asconf(net, ep,
  3594. (struct sctp_association *)asoc,
  3595. type, commands);
  3596. abort = sctp_make_abort(asoc, asconf_ack,
  3597. sizeof(struct sctp_errhdr));
  3598. if (abort) {
  3599. sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
  3600. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3601. SCTP_CHUNK(abort));
  3602. }
  3603. /* We are going to ABORT, so we might as well stop
  3604. * processing the rest of the chunks in the packet.
  3605. */
  3606. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  3607. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  3608. SCTP_ERROR(ECONNABORTED));
  3609. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  3610. SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
  3611. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  3612. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  3613. return SCTP_DISPOSITION_ABORT;
  3614. }
  3615. return SCTP_DISPOSITION_DISCARD;
  3616. }
  3617. /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
  3618. enum sctp_disposition sctp_sf_do_reconf(struct net *net,
  3619. const struct sctp_endpoint *ep,
  3620. const struct sctp_association *asoc,
  3621. const union sctp_subtype type,
  3622. void *arg,
  3623. struct sctp_cmd_seq *commands)
  3624. {
  3625. struct sctp_paramhdr *err_param = NULL;
  3626. struct sctp_chunk *chunk = arg;
  3627. struct sctp_reconf_chunk *hdr;
  3628. union sctp_params param;
  3629. if (!sctp_vtag_verify(chunk, asoc)) {
  3630. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3631. SCTP_NULL());
  3632. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3633. }
  3634. /* Make sure that the RECONF chunk has a valid length. */
  3635. if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
  3636. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3637. commands);
  3638. if (!sctp_verify_reconf(asoc, chunk, &err_param))
  3639. return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
  3640. (void *)err_param, commands);
  3641. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  3642. sctp_walk_params(param, hdr, params) {
  3643. struct sctp_chunk *reply = NULL;
  3644. struct sctp_ulpevent *ev = NULL;
  3645. if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
  3646. reply = sctp_process_strreset_outreq(
  3647. (struct sctp_association *)asoc, param, &ev);
  3648. else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
  3649. reply = sctp_process_strreset_inreq(
  3650. (struct sctp_association *)asoc, param, &ev);
  3651. else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
  3652. reply = sctp_process_strreset_tsnreq(
  3653. (struct sctp_association *)asoc, param, &ev);
  3654. else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
  3655. reply = sctp_process_strreset_addstrm_out(
  3656. (struct sctp_association *)asoc, param, &ev);
  3657. else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
  3658. reply = sctp_process_strreset_addstrm_in(
  3659. (struct sctp_association *)asoc, param, &ev);
  3660. else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
  3661. reply = sctp_process_strreset_resp(
  3662. (struct sctp_association *)asoc, param, &ev);
  3663. if (ev)
  3664. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  3665. SCTP_ULPEVENT(ev));
  3666. if (reply)
  3667. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3668. SCTP_CHUNK(reply));
  3669. }
  3670. return SCTP_DISPOSITION_CONSUME;
  3671. }
  3672. /*
  3673. * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
  3674. *
  3675. * When a FORWARD TSN chunk arrives, the data receiver MUST first update
  3676. * its cumulative TSN point to the value carried in the FORWARD TSN
  3677. * chunk, and then MUST further advance its cumulative TSN point locally
  3678. * if possible.
  3679. * After the above processing, the data receiver MUST stop reporting any
  3680. * missing TSNs earlier than or equal to the new cumulative TSN point.
  3681. *
  3682. * Verification Tag: 8.5 Verification Tag [Normal verification]
  3683. *
  3684. * The return value is the disposition of the chunk.
  3685. */
  3686. enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
  3687. const struct sctp_endpoint *ep,
  3688. const struct sctp_association *asoc,
  3689. const union sctp_subtype type,
  3690. void *arg,
  3691. struct sctp_cmd_seq *commands)
  3692. {
  3693. struct sctp_fwdtsn_hdr *fwdtsn_hdr;
  3694. struct sctp_chunk *chunk = arg;
  3695. __u16 len;
  3696. __u32 tsn;
  3697. if (!sctp_vtag_verify(chunk, asoc)) {
  3698. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3699. SCTP_NULL());
  3700. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3701. }
  3702. if (!asoc->peer.prsctp_capable)
  3703. return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
  3704. /* Make sure that the FORWARD_TSN chunk has valid length. */
  3705. if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
  3706. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3707. commands);
  3708. fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
  3709. chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
  3710. len = ntohs(chunk->chunk_hdr->length);
  3711. len -= sizeof(struct sctp_chunkhdr);
  3712. skb_pull(chunk->skb, len);
  3713. tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
  3714. pr_debug("%s: TSN 0x%x\n", __func__, tsn);
  3715. /* The TSN is too high--silently discard the chunk and count on it
  3716. * getting retransmitted later.
  3717. */
  3718. if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
  3719. goto discard_noforce;
  3720. if (!asoc->stream.si->validate_ftsn(chunk))
  3721. goto discard_noforce;
  3722. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
  3723. if (len > sctp_ftsnhdr_len(&asoc->stream))
  3724. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
  3725. SCTP_CHUNK(chunk));
  3726. /* Count this as receiving DATA. */
  3727. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
  3728. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3729. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  3730. }
  3731. /* FIXME: For now send a SACK, but DATA processing may
  3732. * send another.
  3733. */
  3734. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
  3735. return SCTP_DISPOSITION_CONSUME;
  3736. discard_noforce:
  3737. return SCTP_DISPOSITION_DISCARD;
  3738. }
  3739. enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
  3740. struct net *net,
  3741. const struct sctp_endpoint *ep,
  3742. const struct sctp_association *asoc,
  3743. const union sctp_subtype type,
  3744. void *arg,
  3745. struct sctp_cmd_seq *commands)
  3746. {
  3747. struct sctp_fwdtsn_hdr *fwdtsn_hdr;
  3748. struct sctp_chunk *chunk = arg;
  3749. __u16 len;
  3750. __u32 tsn;
  3751. if (!sctp_vtag_verify(chunk, asoc)) {
  3752. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3753. SCTP_NULL());
  3754. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3755. }
  3756. if (!asoc->peer.prsctp_capable)
  3757. return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
  3758. /* Make sure that the FORWARD_TSN chunk has a valid length. */
  3759. if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
  3760. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3761. commands);
  3762. fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
  3763. chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
  3764. len = ntohs(chunk->chunk_hdr->length);
  3765. len -= sizeof(struct sctp_chunkhdr);
  3766. skb_pull(chunk->skb, len);
  3767. tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
  3768. pr_debug("%s: TSN 0x%x\n", __func__, tsn);
  3769. /* The TSN is too high--silently discard the chunk and count on it
  3770. * getting retransmitted later.
  3771. */
  3772. if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
  3773. goto gen_shutdown;
  3774. if (!asoc->stream.si->validate_ftsn(chunk))
  3775. goto gen_shutdown;
  3776. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
  3777. if (len > sctp_ftsnhdr_len(&asoc->stream))
  3778. sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
  3779. SCTP_CHUNK(chunk));
  3780. /* Go a head and force a SACK, since we are shutting down. */
  3781. gen_shutdown:
  3782. /* Implementor's Guide.
  3783. *
  3784. * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
  3785. * respond to each received packet containing one or more DATA chunk(s)
  3786. * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
  3787. */
  3788. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
  3789. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  3790. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  3791. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  3792. return SCTP_DISPOSITION_CONSUME;
  3793. }
  3794. /*
  3795. * SCTP-AUTH Section 6.3 Receiving authenticated chunks
  3796. *
  3797. * The receiver MUST use the HMAC algorithm indicated in the HMAC
  3798. * Identifier field. If this algorithm was not specified by the
  3799. * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
  3800. * during association setup, the AUTH chunk and all chunks after it MUST
  3801. * be discarded and an ERROR chunk SHOULD be sent with the error cause
  3802. * defined in Section 4.1.
  3803. *
  3804. * If an endpoint with no shared key receives a Shared Key Identifier
  3805. * other than 0, it MUST silently discard all authenticated chunks. If
  3806. * the endpoint has at least one endpoint pair shared key for the peer,
  3807. * it MUST use the key specified by the Shared Key Identifier if a
  3808. * key has been configured for that Shared Key Identifier. If no
  3809. * endpoint pair shared key has been configured for that Shared Key
  3810. * Identifier, all authenticated chunks MUST be silently discarded.
  3811. *
  3812. * Verification Tag: 8.5 Verification Tag [Normal verification]
  3813. *
  3814. * The return value is the disposition of the chunk.
  3815. */
  3816. static enum sctp_ierror sctp_sf_authenticate(
  3817. const struct sctp_association *asoc,
  3818. struct sctp_chunk *chunk)
  3819. {
  3820. struct sctp_shared_key *sh_key = NULL;
  3821. struct sctp_authhdr *auth_hdr;
  3822. __u8 *save_digest, *digest;
  3823. struct sctp_hmac *hmac;
  3824. unsigned int sig_len;
  3825. __u16 key_id;
  3826. /* Pull in the auth header, so we can do some more verification */
  3827. auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
  3828. chunk->subh.auth_hdr = auth_hdr;
  3829. skb_pull(chunk->skb, sizeof(*auth_hdr));
  3830. /* Make sure that we support the HMAC algorithm from the auth
  3831. * chunk.
  3832. */
  3833. if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
  3834. return SCTP_IERROR_AUTH_BAD_HMAC;
  3835. /* Make sure that the provided shared key identifier has been
  3836. * configured
  3837. */
  3838. key_id = ntohs(auth_hdr->shkey_id);
  3839. if (key_id != asoc->active_key_id) {
  3840. sh_key = sctp_auth_get_shkey(asoc, key_id);
  3841. if (!sh_key)
  3842. return SCTP_IERROR_AUTH_BAD_KEYID;
  3843. }
  3844. /* Make sure that the length of the signature matches what
  3845. * we expect.
  3846. */
  3847. sig_len = ntohs(chunk->chunk_hdr->length) -
  3848. sizeof(struct sctp_auth_chunk);
  3849. hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
  3850. if (sig_len != hmac->hmac_len)
  3851. return SCTP_IERROR_PROTO_VIOLATION;
  3852. /* Now that we've done validation checks, we can compute and
  3853. * verify the hmac. The steps involved are:
  3854. * 1. Save the digest from the chunk.
  3855. * 2. Zero out the digest in the chunk.
  3856. * 3. Compute the new digest
  3857. * 4. Compare saved and new digests.
  3858. */
  3859. digest = auth_hdr->hmac;
  3860. skb_pull(chunk->skb, sig_len);
  3861. save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
  3862. if (!save_digest)
  3863. goto nomem;
  3864. memset(digest, 0, sig_len);
  3865. sctp_auth_calculate_hmac(asoc, chunk->skb,
  3866. (struct sctp_auth_chunk *)chunk->chunk_hdr,
  3867. sh_key, GFP_ATOMIC);
  3868. /* Discard the packet if the digests do not match */
  3869. if (memcmp(save_digest, digest, sig_len)) {
  3870. kfree(save_digest);
  3871. return SCTP_IERROR_BAD_SIG;
  3872. }
  3873. kfree(save_digest);
  3874. chunk->auth = 1;
  3875. return SCTP_IERROR_NO_ERROR;
  3876. nomem:
  3877. return SCTP_IERROR_NOMEM;
  3878. }
  3879. enum sctp_disposition sctp_sf_eat_auth(struct net *net,
  3880. const struct sctp_endpoint *ep,
  3881. const struct sctp_association *asoc,
  3882. const union sctp_subtype type,
  3883. void *arg, struct sctp_cmd_seq *commands)
  3884. {
  3885. struct sctp_chunk *chunk = arg;
  3886. struct sctp_authhdr *auth_hdr;
  3887. struct sctp_chunk *err_chunk;
  3888. enum sctp_ierror error;
  3889. /* Make sure that the peer has AUTH capable */
  3890. if (!asoc->peer.auth_capable)
  3891. return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
  3892. if (!sctp_vtag_verify(chunk, asoc)) {
  3893. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
  3894. SCTP_NULL());
  3895. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3896. }
  3897. /* Make sure that the AUTH chunk has valid length. */
  3898. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
  3899. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3900. commands);
  3901. auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
  3902. error = sctp_sf_authenticate(asoc, chunk);
  3903. switch (error) {
  3904. case SCTP_IERROR_AUTH_BAD_HMAC:
  3905. /* Generate the ERROR chunk and discard the rest
  3906. * of the packet
  3907. */
  3908. err_chunk = sctp_make_op_error(asoc, chunk,
  3909. SCTP_ERROR_UNSUP_HMAC,
  3910. &auth_hdr->hmac_id,
  3911. sizeof(__u16), 0);
  3912. if (err_chunk) {
  3913. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3914. SCTP_CHUNK(err_chunk));
  3915. }
  3916. fallthrough;
  3917. case SCTP_IERROR_AUTH_BAD_KEYID:
  3918. case SCTP_IERROR_BAD_SIG:
  3919. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3920. case SCTP_IERROR_PROTO_VIOLATION:
  3921. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3922. commands);
  3923. case SCTP_IERROR_NOMEM:
  3924. return SCTP_DISPOSITION_NOMEM;
  3925. default: /* Prevent gcc warnings */
  3926. break;
  3927. }
  3928. if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
  3929. struct sctp_ulpevent *ev;
  3930. ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
  3931. SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
  3932. if (!ev)
  3933. return SCTP_DISPOSITION_NOMEM;
  3934. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
  3935. SCTP_ULPEVENT(ev));
  3936. }
  3937. return SCTP_DISPOSITION_CONSUME;
  3938. }
  3939. /*
  3940. * Process an unknown chunk.
  3941. *
  3942. * Section: 3.2. Also, 2.1 in the implementor's guide.
  3943. *
  3944. * Chunk Types are encoded such that the highest-order two bits specify
  3945. * the action that must be taken if the processing endpoint does not
  3946. * recognize the Chunk Type.
  3947. *
  3948. * 00 - Stop processing this SCTP packet and discard it, do not process
  3949. * any further chunks within it.
  3950. *
  3951. * 01 - Stop processing this SCTP packet and discard it, do not process
  3952. * any further chunks within it, and report the unrecognized
  3953. * chunk in an 'Unrecognized Chunk Type'.
  3954. *
  3955. * 10 - Skip this chunk and continue processing.
  3956. *
  3957. * 11 - Skip this chunk and continue processing, but report in an ERROR
  3958. * Chunk using the 'Unrecognized Chunk Type' cause of error.
  3959. *
  3960. * The return value is the disposition of the chunk.
  3961. */
  3962. enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
  3963. const struct sctp_endpoint *ep,
  3964. const struct sctp_association *asoc,
  3965. const union sctp_subtype type,
  3966. void *arg,
  3967. struct sctp_cmd_seq *commands)
  3968. {
  3969. struct sctp_chunk *unk_chunk = arg;
  3970. struct sctp_chunk *err_chunk;
  3971. struct sctp_chunkhdr *hdr;
  3972. pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
  3973. if (!sctp_vtag_verify(unk_chunk, asoc))
  3974. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3975. /* Make sure that the chunk has a valid length.
  3976. * Since we don't know the chunk type, we use a general
  3977. * chunkhdr structure to make a comparison.
  3978. */
  3979. if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
  3980. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  3981. commands);
  3982. switch (type.chunk & SCTP_CID_ACTION_MASK) {
  3983. case SCTP_CID_ACTION_DISCARD:
  3984. /* Discard the packet. */
  3985. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3986. case SCTP_CID_ACTION_DISCARD_ERR:
  3987. /* Generate an ERROR chunk as response. */
  3988. hdr = unk_chunk->chunk_hdr;
  3989. err_chunk = sctp_make_op_error(asoc, unk_chunk,
  3990. SCTP_ERROR_UNKNOWN_CHUNK, hdr,
  3991. SCTP_PAD4(ntohs(hdr->length)),
  3992. 0);
  3993. if (err_chunk) {
  3994. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  3995. SCTP_CHUNK(err_chunk));
  3996. }
  3997. /* Discard the packet. */
  3998. sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  3999. return SCTP_DISPOSITION_CONSUME;
  4000. case SCTP_CID_ACTION_SKIP:
  4001. /* Skip the chunk. */
  4002. return SCTP_DISPOSITION_DISCARD;
  4003. case SCTP_CID_ACTION_SKIP_ERR:
  4004. /* Generate an ERROR chunk as response. */
  4005. hdr = unk_chunk->chunk_hdr;
  4006. err_chunk = sctp_make_op_error(asoc, unk_chunk,
  4007. SCTP_ERROR_UNKNOWN_CHUNK, hdr,
  4008. SCTP_PAD4(ntohs(hdr->length)),
  4009. 0);
  4010. if (err_chunk) {
  4011. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  4012. SCTP_CHUNK(err_chunk));
  4013. }
  4014. /* Skip the chunk. */
  4015. return SCTP_DISPOSITION_CONSUME;
  4016. default:
  4017. break;
  4018. }
  4019. return SCTP_DISPOSITION_DISCARD;
  4020. }
  4021. /*
  4022. * Discard the chunk.
  4023. *
  4024. * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
  4025. * [Too numerous to mention...]
  4026. * Verification Tag: No verification needed.
  4027. * Inputs
  4028. * (endpoint, asoc, chunk)
  4029. *
  4030. * Outputs
  4031. * (asoc, reply_msg, msg_up, timers, counters)
  4032. *
  4033. * The return value is the disposition of the chunk.
  4034. */
  4035. enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
  4036. const struct sctp_endpoint *ep,
  4037. const struct sctp_association *asoc,
  4038. const union sctp_subtype type,
  4039. void *arg,
  4040. struct sctp_cmd_seq *commands)
  4041. {
  4042. struct sctp_chunk *chunk = arg;
  4043. if (asoc && !sctp_vtag_verify(chunk, asoc))
  4044. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  4045. /* Make sure that the chunk has a valid length.
  4046. * Since we don't know the chunk type, we use a general
  4047. * chunkhdr structure to make a comparison.
  4048. */
  4049. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  4050. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  4051. commands);
  4052. pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
  4053. return SCTP_DISPOSITION_DISCARD;
  4054. }
  4055. /*
  4056. * Discard the whole packet.
  4057. *
  4058. * Section: 8.4 2)
  4059. *
  4060. * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
  4061. * silently discard the OOTB packet and take no further action.
  4062. *
  4063. * Verification Tag: No verification necessary
  4064. *
  4065. * Inputs
  4066. * (endpoint, asoc, chunk)
  4067. *
  4068. * Outputs
  4069. * (asoc, reply_msg, msg_up, timers, counters)
  4070. *
  4071. * The return value is the disposition of the chunk.
  4072. */
  4073. enum sctp_disposition sctp_sf_pdiscard(struct net *net,
  4074. const struct sctp_endpoint *ep,
  4075. const struct sctp_association *asoc,
  4076. const union sctp_subtype type,
  4077. void *arg, struct sctp_cmd_seq *commands)
  4078. {
  4079. SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
  4080. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  4081. return SCTP_DISPOSITION_CONSUME;
  4082. }
  4083. /*
  4084. * The other end is violating protocol.
  4085. *
  4086. * Section: Not specified
  4087. * Verification Tag: Not specified
  4088. * Inputs
  4089. * (endpoint, asoc, chunk)
  4090. *
  4091. * Outputs
  4092. * (asoc, reply_msg, msg_up, timers, counters)
  4093. *
  4094. * We simply tag the chunk as a violation. The state machine will log
  4095. * the violation and continue.
  4096. */
  4097. enum sctp_disposition sctp_sf_violation(struct net *net,
  4098. const struct sctp_endpoint *ep,
  4099. const struct sctp_association *asoc,
  4100. const union sctp_subtype type,
  4101. void *arg,
  4102. struct sctp_cmd_seq *commands)
  4103. {
  4104. struct sctp_chunk *chunk = arg;
  4105. if (!sctp_vtag_verify(chunk, asoc))
  4106. return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
  4107. /* Make sure that the chunk has a valid length. */
  4108. if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
  4109. return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
  4110. commands);
  4111. return SCTP_DISPOSITION_VIOLATION;
  4112. }
  4113. /*
  4114. * Common function to handle a protocol violation.
  4115. */
  4116. static enum sctp_disposition sctp_sf_abort_violation(
  4117. struct net *net,
  4118. const struct sctp_endpoint *ep,
  4119. const struct sctp_association *asoc,
  4120. void *arg,
  4121. struct sctp_cmd_seq *commands,
  4122. const __u8 *payload,
  4123. const size_t paylen)
  4124. {
  4125. struct sctp_packet *packet = NULL;
  4126. struct sctp_chunk *chunk = arg;
  4127. struct sctp_chunk *abort = NULL;
  4128. /* SCTP-AUTH, Section 6.3:
  4129. * It should be noted that if the receiver wants to tear
  4130. * down an association in an authenticated way only, the
  4131. * handling of malformed packets should not result in
  4132. * tearing down the association.
  4133. *
  4134. * This means that if we only want to abort associations
  4135. * in an authenticated way (i.e AUTH+ABORT), then we
  4136. * can't destroy this association just because the packet
  4137. * was malformed.
  4138. */
  4139. if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
  4140. goto discard;
  4141. /* Make the abort chunk. */
  4142. abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
  4143. if (!abort)
  4144. goto nomem;
  4145. if (asoc) {
  4146. /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
  4147. if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
  4148. !asoc->peer.i.init_tag) {
  4149. struct sctp_initack_chunk *initack;
  4150. initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
  4151. if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
  4152. abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
  4153. else {
  4154. unsigned int inittag;
  4155. inittag = ntohl(initack->init_hdr.init_tag);
  4156. sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
  4157. SCTP_U32(inittag));
  4158. }
  4159. }
  4160. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4161. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  4162. if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
  4163. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4164. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4165. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4166. SCTP_ERROR(ECONNREFUSED));
  4167. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  4168. SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
  4169. } else {
  4170. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4171. SCTP_ERROR(ECONNABORTED));
  4172. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4173. SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
  4174. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  4175. }
  4176. } else {
  4177. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  4178. if (!packet)
  4179. goto nomem_pkt;
  4180. if (sctp_test_T_bit(abort))
  4181. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  4182. abort->skb->sk = ep->base.sk;
  4183. sctp_packet_append_chunk(packet, abort);
  4184. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  4185. SCTP_PACKET(packet));
  4186. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  4187. }
  4188. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4189. discard:
  4190. sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
  4191. return SCTP_DISPOSITION_ABORT;
  4192. nomem_pkt:
  4193. sctp_chunk_free(abort);
  4194. nomem:
  4195. return SCTP_DISPOSITION_NOMEM;
  4196. }
  4197. /*
  4198. * Handle a protocol violation when the chunk length is invalid.
  4199. * "Invalid" length is identified as smaller than the minimal length a
  4200. * given chunk can be. For example, a SACK chunk has invalid length
  4201. * if its length is set to be smaller than the size of struct sctp_sack_chunk.
  4202. *
  4203. * We inform the other end by sending an ABORT with a Protocol Violation
  4204. * error code.
  4205. *
  4206. * Section: Not specified
  4207. * Verification Tag: Nothing to do
  4208. * Inputs
  4209. * (endpoint, asoc, chunk)
  4210. *
  4211. * Outputs
  4212. * (reply_msg, msg_up, counters)
  4213. *
  4214. * Generate an ABORT chunk and terminate the association.
  4215. */
  4216. static enum sctp_disposition sctp_sf_violation_chunklen(
  4217. struct net *net,
  4218. const struct sctp_endpoint *ep,
  4219. const struct sctp_association *asoc,
  4220. const union sctp_subtype type,
  4221. void *arg,
  4222. struct sctp_cmd_seq *commands)
  4223. {
  4224. static const char err_str[] = "The following chunk had invalid length:";
  4225. return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
  4226. sizeof(err_str));
  4227. }
  4228. /*
  4229. * Handle a protocol violation when the parameter length is invalid.
  4230. * If the length is smaller than the minimum length of a given parameter,
  4231. * or accumulated length in multi parameters exceeds the end of the chunk,
  4232. * the length is considered as invalid.
  4233. */
  4234. static enum sctp_disposition sctp_sf_violation_paramlen(
  4235. struct net *net,
  4236. const struct sctp_endpoint *ep,
  4237. const struct sctp_association *asoc,
  4238. const union sctp_subtype type,
  4239. void *arg, void *ext,
  4240. struct sctp_cmd_seq *commands)
  4241. {
  4242. struct sctp_paramhdr *param = ext;
  4243. struct sctp_chunk *abort = NULL;
  4244. struct sctp_chunk *chunk = arg;
  4245. if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
  4246. goto discard;
  4247. /* Make the abort chunk. */
  4248. abort = sctp_make_violation_paramlen(asoc, chunk, param);
  4249. if (!abort)
  4250. goto nomem;
  4251. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4252. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  4253. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4254. SCTP_ERROR(ECONNABORTED));
  4255. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4256. SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
  4257. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  4258. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4259. discard:
  4260. sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
  4261. return SCTP_DISPOSITION_ABORT;
  4262. nomem:
  4263. return SCTP_DISPOSITION_NOMEM;
  4264. }
  4265. /* Handle a protocol violation when the peer trying to advance the
  4266. * cumulative tsn ack to a point beyond the max tsn currently sent.
  4267. *
  4268. * We inform the other end by sending an ABORT with a Protocol Violation
  4269. * error code.
  4270. */
  4271. static enum sctp_disposition sctp_sf_violation_ctsn(
  4272. struct net *net,
  4273. const struct sctp_endpoint *ep,
  4274. const struct sctp_association *asoc,
  4275. const union sctp_subtype type,
  4276. void *arg,
  4277. struct sctp_cmd_seq *commands)
  4278. {
  4279. static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
  4280. return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
  4281. sizeof(err_str));
  4282. }
  4283. /* Handle protocol violation of an invalid chunk bundling. For example,
  4284. * when we have an association and we receive bundled INIT-ACK, or
  4285. * SHUTDOWN-COMPLETE, our peer is clearly violating the "MUST NOT bundle"
  4286. * statement from the specs. Additionally, there might be an attacker
  4287. * on the path and we may not want to continue this communication.
  4288. */
  4289. static enum sctp_disposition sctp_sf_violation_chunk(
  4290. struct net *net,
  4291. const struct sctp_endpoint *ep,
  4292. const struct sctp_association *asoc,
  4293. const union sctp_subtype type,
  4294. void *arg,
  4295. struct sctp_cmd_seq *commands)
  4296. {
  4297. static const char err_str[] = "The following chunk violates protocol:";
  4298. return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
  4299. sizeof(err_str));
  4300. }
  4301. /***************************************************************************
  4302. * These are the state functions for handling primitive (Section 10) events.
  4303. ***************************************************************************/
  4304. /*
  4305. * sctp_sf_do_prm_asoc
  4306. *
  4307. * Section: 10.1 ULP-to-SCTP
  4308. * B) Associate
  4309. *
  4310. * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
  4311. * outbound stream count)
  4312. * -> association id [,destination transport addr list] [,outbound stream
  4313. * count]
  4314. *
  4315. * This primitive allows the upper layer to initiate an association to a
  4316. * specific peer endpoint.
  4317. *
  4318. * The peer endpoint shall be specified by one of the transport addresses
  4319. * which defines the endpoint (see Section 1.4). If the local SCTP
  4320. * instance has not been initialized, the ASSOCIATE is considered an
  4321. * error.
  4322. * [This is not relevant for the kernel implementation since we do all
  4323. * initialization at boot time. It we hadn't initialized we wouldn't
  4324. * get anywhere near this code.]
  4325. *
  4326. * An association id, which is a local handle to the SCTP association,
  4327. * will be returned on successful establishment of the association. If
  4328. * SCTP is not able to open an SCTP association with the peer endpoint,
  4329. * an error is returned.
  4330. * [In the kernel implementation, the struct sctp_association needs to
  4331. * be created BEFORE causing this primitive to run.]
  4332. *
  4333. * Other association parameters may be returned, including the
  4334. * complete destination transport addresses of the peer as well as the
  4335. * outbound stream count of the local endpoint. One of the transport
  4336. * address from the returned destination addresses will be selected by
  4337. * the local endpoint as default primary path for sending SCTP packets
  4338. * to this peer. The returned "destination transport addr list" can
  4339. * be used by the ULP to change the default primary path or to force
  4340. * sending a packet to a specific transport address. [All of this
  4341. * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
  4342. * function.]
  4343. *
  4344. * Mandatory attributes:
  4345. *
  4346. * o local SCTP instance name - obtained from the INITIALIZE operation.
  4347. * [This is the argument asoc.]
  4348. * o destination transport addr - specified as one of the transport
  4349. * addresses of the peer endpoint with which the association is to be
  4350. * established.
  4351. * [This is asoc->peer.active_path.]
  4352. * o outbound stream count - the number of outbound streams the ULP
  4353. * would like to open towards this peer endpoint.
  4354. * [BUG: This is not currently implemented.]
  4355. * Optional attributes:
  4356. *
  4357. * None.
  4358. *
  4359. * The return value is a disposition.
  4360. */
  4361. enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
  4362. const struct sctp_endpoint *ep,
  4363. const struct sctp_association *asoc,
  4364. const union sctp_subtype type,
  4365. void *arg,
  4366. struct sctp_cmd_seq *commands)
  4367. {
  4368. struct sctp_association *my_asoc;
  4369. struct sctp_chunk *repl;
  4370. /* The comment below says that we enter COOKIE-WAIT AFTER
  4371. * sending the INIT, but that doesn't actually work in our
  4372. * implementation...
  4373. */
  4374. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4375. SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
  4376. /* RFC 2960 5.1 Normal Establishment of an Association
  4377. *
  4378. * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
  4379. * must provide its Verification Tag (Tag_A) in the Initiate
  4380. * Tag field. Tag_A SHOULD be a random number in the range of
  4381. * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
  4382. */
  4383. repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
  4384. if (!repl)
  4385. goto nomem;
  4386. /* Choose transport for INIT. */
  4387. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  4388. SCTP_CHUNK(repl));
  4389. /* Cast away the const modifier, as we want to just
  4390. * rerun it through as a sideffect.
  4391. */
  4392. my_asoc = (struct sctp_association *)asoc;
  4393. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
  4394. /* After sending the INIT, "A" starts the T1-init timer and
  4395. * enters the COOKIE-WAIT state.
  4396. */
  4397. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4398. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4399. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  4400. return SCTP_DISPOSITION_CONSUME;
  4401. nomem:
  4402. return SCTP_DISPOSITION_NOMEM;
  4403. }
  4404. /*
  4405. * Process the SEND primitive.
  4406. *
  4407. * Section: 10.1 ULP-to-SCTP
  4408. * E) Send
  4409. *
  4410. * Format: SEND(association id, buffer address, byte count [,context]
  4411. * [,stream id] [,life time] [,destination transport address]
  4412. * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
  4413. * -> result
  4414. *
  4415. * This is the main method to send user data via SCTP.
  4416. *
  4417. * Mandatory attributes:
  4418. *
  4419. * o association id - local handle to the SCTP association
  4420. *
  4421. * o buffer address - the location where the user message to be
  4422. * transmitted is stored;
  4423. *
  4424. * o byte count - The size of the user data in number of bytes;
  4425. *
  4426. * Optional attributes:
  4427. *
  4428. * o context - an optional 32 bit integer that will be carried in the
  4429. * sending failure notification to the ULP if the transportation of
  4430. * this User Message fails.
  4431. *
  4432. * o stream id - to indicate which stream to send the data on. If not
  4433. * specified, stream 0 will be used.
  4434. *
  4435. * o life time - specifies the life time of the user data. The user data
  4436. * will not be sent by SCTP after the life time expires. This
  4437. * parameter can be used to avoid efforts to transmit stale
  4438. * user messages. SCTP notifies the ULP if the data cannot be
  4439. * initiated to transport (i.e. sent to the destination via SCTP's
  4440. * send primitive) within the life time variable. However, the
  4441. * user data will be transmitted if SCTP has attempted to transmit a
  4442. * chunk before the life time expired.
  4443. *
  4444. * o destination transport address - specified as one of the destination
  4445. * transport addresses of the peer endpoint to which this packet
  4446. * should be sent. Whenever possible, SCTP should use this destination
  4447. * transport address for sending the packets, instead of the current
  4448. * primary path.
  4449. *
  4450. * o unorder flag - this flag, if present, indicates that the user
  4451. * would like the data delivered in an unordered fashion to the peer
  4452. * (i.e., the U flag is set to 1 on all DATA chunks carrying this
  4453. * message).
  4454. *
  4455. * o no-bundle flag - instructs SCTP not to bundle this user data with
  4456. * other outbound DATA chunks. SCTP MAY still bundle even when
  4457. * this flag is present, when faced with network congestion.
  4458. *
  4459. * o payload protocol-id - A 32 bit unsigned integer that is to be
  4460. * passed to the peer indicating the type of payload protocol data
  4461. * being transmitted. This value is passed as opaque data by SCTP.
  4462. *
  4463. * The return value is the disposition.
  4464. */
  4465. enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
  4466. const struct sctp_endpoint *ep,
  4467. const struct sctp_association *asoc,
  4468. const union sctp_subtype type,
  4469. void *arg,
  4470. struct sctp_cmd_seq *commands)
  4471. {
  4472. struct sctp_datamsg *msg = arg;
  4473. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
  4474. return SCTP_DISPOSITION_CONSUME;
  4475. }
  4476. /*
  4477. * Process the SHUTDOWN primitive.
  4478. *
  4479. * Section: 10.1:
  4480. * C) Shutdown
  4481. *
  4482. * Format: SHUTDOWN(association id)
  4483. * -> result
  4484. *
  4485. * Gracefully closes an association. Any locally queued user data
  4486. * will be delivered to the peer. The association will be terminated only
  4487. * after the peer acknowledges all the SCTP packets sent. A success code
  4488. * will be returned on successful termination of the association. If
  4489. * attempting to terminate the association results in a failure, an error
  4490. * code shall be returned.
  4491. *
  4492. * Mandatory attributes:
  4493. *
  4494. * o association id - local handle to the SCTP association
  4495. *
  4496. * Optional attributes:
  4497. *
  4498. * None.
  4499. *
  4500. * The return value is the disposition.
  4501. */
  4502. enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
  4503. struct net *net,
  4504. const struct sctp_endpoint *ep,
  4505. const struct sctp_association *asoc,
  4506. const union sctp_subtype type,
  4507. void *arg,
  4508. struct sctp_cmd_seq *commands)
  4509. {
  4510. enum sctp_disposition disposition;
  4511. /* From 9.2 Shutdown of an Association
  4512. * Upon receipt of the SHUTDOWN primitive from its upper
  4513. * layer, the endpoint enters SHUTDOWN-PENDING state and
  4514. * remains there until all outstanding data has been
  4515. * acknowledged by its peer. The endpoint accepts no new data
  4516. * from its upper layer, but retransmits data to the far end
  4517. * if necessary to fill gaps.
  4518. */
  4519. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4520. SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
  4521. disposition = SCTP_DISPOSITION_CONSUME;
  4522. if (sctp_outq_is_empty(&asoc->outqueue)) {
  4523. disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
  4524. arg, commands);
  4525. }
  4526. return disposition;
  4527. }
  4528. /*
  4529. * Process the ABORT primitive.
  4530. *
  4531. * Section: 10.1:
  4532. * C) Abort
  4533. *
  4534. * Format: Abort(association id [, cause code])
  4535. * -> result
  4536. *
  4537. * Ungracefully closes an association. Any locally queued user data
  4538. * will be discarded and an ABORT chunk is sent to the peer. A success code
  4539. * will be returned on successful abortion of the association. If
  4540. * attempting to abort the association results in a failure, an error
  4541. * code shall be returned.
  4542. *
  4543. * Mandatory attributes:
  4544. *
  4545. * o association id - local handle to the SCTP association
  4546. *
  4547. * Optional attributes:
  4548. *
  4549. * o cause code - reason of the abort to be passed to the peer
  4550. *
  4551. * None.
  4552. *
  4553. * The return value is the disposition.
  4554. */
  4555. enum sctp_disposition sctp_sf_do_9_1_prm_abort(
  4556. struct net *net,
  4557. const struct sctp_endpoint *ep,
  4558. const struct sctp_association *asoc,
  4559. const union sctp_subtype type,
  4560. void *arg,
  4561. struct sctp_cmd_seq *commands)
  4562. {
  4563. /* From 9.1 Abort of an Association
  4564. * Upon receipt of the ABORT primitive from its upper
  4565. * layer, the endpoint enters CLOSED state and
  4566. * discard all outstanding data has been
  4567. * acknowledged by its peer. The endpoint accepts no new data
  4568. * from its upper layer, but retransmits data to the far end
  4569. * if necessary to fill gaps.
  4570. */
  4571. struct sctp_chunk *abort = arg;
  4572. if (abort)
  4573. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4574. /* Even if we can't send the ABORT due to low memory delete the
  4575. * TCB. This is a departure from our typical NOMEM handling.
  4576. */
  4577. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4578. SCTP_ERROR(ECONNABORTED));
  4579. /* Delete the established association. */
  4580. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  4581. SCTP_PERR(SCTP_ERROR_USER_ABORT));
  4582. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4583. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  4584. return SCTP_DISPOSITION_ABORT;
  4585. }
  4586. /* We tried an illegal operation on an association which is closed. */
  4587. enum sctp_disposition sctp_sf_error_closed(struct net *net,
  4588. const struct sctp_endpoint *ep,
  4589. const struct sctp_association *asoc,
  4590. const union sctp_subtype type,
  4591. void *arg,
  4592. struct sctp_cmd_seq *commands)
  4593. {
  4594. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
  4595. return SCTP_DISPOSITION_CONSUME;
  4596. }
  4597. /* We tried an illegal operation on an association which is shutting
  4598. * down.
  4599. */
  4600. enum sctp_disposition sctp_sf_error_shutdown(
  4601. struct net *net,
  4602. const struct sctp_endpoint *ep,
  4603. const struct sctp_association *asoc,
  4604. const union sctp_subtype type,
  4605. void *arg,
  4606. struct sctp_cmd_seq *commands)
  4607. {
  4608. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
  4609. SCTP_ERROR(-ESHUTDOWN));
  4610. return SCTP_DISPOSITION_CONSUME;
  4611. }
  4612. /*
  4613. * sctp_cookie_wait_prm_shutdown
  4614. *
  4615. * Section: 4 Note: 2
  4616. * Verification Tag:
  4617. * Inputs
  4618. * (endpoint, asoc)
  4619. *
  4620. * The RFC does not explicitly address this issue, but is the route through the
  4621. * state table when someone issues a shutdown while in COOKIE_WAIT state.
  4622. *
  4623. * Outputs
  4624. * (timers)
  4625. */
  4626. enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
  4627. struct net *net,
  4628. const struct sctp_endpoint *ep,
  4629. const struct sctp_association *asoc,
  4630. const union sctp_subtype type,
  4631. void *arg,
  4632. struct sctp_cmd_seq *commands)
  4633. {
  4634. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4635. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4636. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4637. SCTP_STATE(SCTP_STATE_CLOSED));
  4638. SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
  4639. sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
  4640. return SCTP_DISPOSITION_DELETE_TCB;
  4641. }
  4642. /*
  4643. * sctp_cookie_echoed_prm_shutdown
  4644. *
  4645. * Section: 4 Note: 2
  4646. * Verification Tag:
  4647. * Inputs
  4648. * (endpoint, asoc)
  4649. *
  4650. * The RFC does not explicitly address this issue, but is the route through the
  4651. * state table when someone issues a shutdown while in COOKIE_ECHOED state.
  4652. *
  4653. * Outputs
  4654. * (timers)
  4655. */
  4656. enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
  4657. struct net *net,
  4658. const struct sctp_endpoint *ep,
  4659. const struct sctp_association *asoc,
  4660. const union sctp_subtype type,
  4661. void *arg,
  4662. struct sctp_cmd_seq *commands)
  4663. {
  4664. /* There is a single T1 timer, so we should be able to use
  4665. * common function with the COOKIE-WAIT state.
  4666. */
  4667. return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
  4668. }
  4669. /*
  4670. * sctp_sf_cookie_wait_prm_abort
  4671. *
  4672. * Section: 4 Note: 2
  4673. * Verification Tag:
  4674. * Inputs
  4675. * (endpoint, asoc)
  4676. *
  4677. * The RFC does not explicitly address this issue, but is the route through the
  4678. * state table when someone issues an abort while in COOKIE_WAIT state.
  4679. *
  4680. * Outputs
  4681. * (timers)
  4682. */
  4683. enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
  4684. struct net *net,
  4685. const struct sctp_endpoint *ep,
  4686. const struct sctp_association *asoc,
  4687. const union sctp_subtype type,
  4688. void *arg,
  4689. struct sctp_cmd_seq *commands)
  4690. {
  4691. struct sctp_chunk *abort = arg;
  4692. /* Stop T1-init timer */
  4693. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4694. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  4695. if (abort)
  4696. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
  4697. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4698. SCTP_STATE(SCTP_STATE_CLOSED));
  4699. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  4700. /* Even if we can't send the ABORT due to low memory delete the
  4701. * TCB. This is a departure from our typical NOMEM handling.
  4702. */
  4703. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  4704. SCTP_ERROR(ECONNREFUSED));
  4705. /* Delete the established association. */
  4706. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  4707. SCTP_PERR(SCTP_ERROR_USER_ABORT));
  4708. return SCTP_DISPOSITION_ABORT;
  4709. }
  4710. /*
  4711. * sctp_sf_cookie_echoed_prm_abort
  4712. *
  4713. * Section: 4 Note: 3
  4714. * Verification Tag:
  4715. * Inputs
  4716. * (endpoint, asoc)
  4717. *
  4718. * The RFC does not explcitly address this issue, but is the route through the
  4719. * state table when someone issues an abort while in COOKIE_ECHOED state.
  4720. *
  4721. * Outputs
  4722. * (timers)
  4723. */
  4724. enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
  4725. struct net *net,
  4726. const struct sctp_endpoint *ep,
  4727. const struct sctp_association *asoc,
  4728. const union sctp_subtype type,
  4729. void *arg,
  4730. struct sctp_cmd_seq *commands)
  4731. {
  4732. /* There is a single T1 timer, so we should be able to use
  4733. * common function with the COOKIE-WAIT state.
  4734. */
  4735. return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
  4736. }
  4737. /*
  4738. * sctp_sf_shutdown_pending_prm_abort
  4739. *
  4740. * Inputs
  4741. * (endpoint, asoc)
  4742. *
  4743. * The RFC does not explicitly address this issue, but is the route through the
  4744. * state table when someone issues an abort while in SHUTDOWN-PENDING state.
  4745. *
  4746. * Outputs
  4747. * (timers)
  4748. */
  4749. enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
  4750. struct net *net,
  4751. const struct sctp_endpoint *ep,
  4752. const struct sctp_association *asoc,
  4753. const union sctp_subtype type,
  4754. void *arg,
  4755. struct sctp_cmd_seq *commands)
  4756. {
  4757. /* Stop the T5-shutdown guard timer. */
  4758. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4759. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4760. return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
  4761. }
  4762. /*
  4763. * sctp_sf_shutdown_sent_prm_abort
  4764. *
  4765. * Inputs
  4766. * (endpoint, asoc)
  4767. *
  4768. * The RFC does not explicitly address this issue, but is the route through the
  4769. * state table when someone issues an abort while in SHUTDOWN-SENT state.
  4770. *
  4771. * Outputs
  4772. * (timers)
  4773. */
  4774. enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
  4775. struct net *net,
  4776. const struct sctp_endpoint *ep,
  4777. const struct sctp_association *asoc,
  4778. const union sctp_subtype type,
  4779. void *arg,
  4780. struct sctp_cmd_seq *commands)
  4781. {
  4782. /* Stop the T2-shutdown timer. */
  4783. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4784. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  4785. /* Stop the T5-shutdown guard timer. */
  4786. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4787. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4788. return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
  4789. }
  4790. /*
  4791. * sctp_sf_cookie_echoed_prm_abort
  4792. *
  4793. * Inputs
  4794. * (endpoint, asoc)
  4795. *
  4796. * The RFC does not explcitly address this issue, but is the route through the
  4797. * state table when someone issues an abort while in COOKIE_ECHOED state.
  4798. *
  4799. * Outputs
  4800. * (timers)
  4801. */
  4802. enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
  4803. struct net *net,
  4804. const struct sctp_endpoint *ep,
  4805. const struct sctp_association *asoc,
  4806. const union sctp_subtype type,
  4807. void *arg,
  4808. struct sctp_cmd_seq *commands)
  4809. {
  4810. /* The same T2 timer, so we should be able to use
  4811. * common function with the SHUTDOWN-SENT state.
  4812. */
  4813. return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
  4814. }
  4815. /*
  4816. * Process the REQUESTHEARTBEAT primitive
  4817. *
  4818. * 10.1 ULP-to-SCTP
  4819. * J) Request Heartbeat
  4820. *
  4821. * Format: REQUESTHEARTBEAT(association id, destination transport address)
  4822. *
  4823. * -> result
  4824. *
  4825. * Instructs the local endpoint to perform a HeartBeat on the specified
  4826. * destination transport address of the given association. The returned
  4827. * result should indicate whether the transmission of the HEARTBEAT
  4828. * chunk to the destination address is successful.
  4829. *
  4830. * Mandatory attributes:
  4831. *
  4832. * o association id - local handle to the SCTP association
  4833. *
  4834. * o destination transport address - the transport address of the
  4835. * association on which a heartbeat should be issued.
  4836. */
  4837. enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
  4838. struct net *net,
  4839. const struct sctp_endpoint *ep,
  4840. const struct sctp_association *asoc,
  4841. const union sctp_subtype type,
  4842. void *arg,
  4843. struct sctp_cmd_seq *commands)
  4844. {
  4845. if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
  4846. (struct sctp_transport *)arg, commands))
  4847. return SCTP_DISPOSITION_NOMEM;
  4848. /*
  4849. * RFC 2960 (bis), section 8.3
  4850. *
  4851. * D) Request an on-demand HEARTBEAT on a specific destination
  4852. * transport address of a given association.
  4853. *
  4854. * The endpoint should increment the respective error counter of
  4855. * the destination transport address each time a HEARTBEAT is sent
  4856. * to that address and not acknowledged within one RTO.
  4857. *
  4858. */
  4859. sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
  4860. SCTP_TRANSPORT(arg));
  4861. return SCTP_DISPOSITION_CONSUME;
  4862. }
  4863. /*
  4864. * ADDIP Section 4.1 ASCONF Chunk Procedures
  4865. * When an endpoint has an ASCONF signaled change to be sent to the
  4866. * remote endpoint it should do A1 to A9
  4867. */
  4868. enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
  4869. const struct sctp_endpoint *ep,
  4870. const struct sctp_association *asoc,
  4871. const union sctp_subtype type,
  4872. void *arg,
  4873. struct sctp_cmd_seq *commands)
  4874. {
  4875. struct sctp_chunk *chunk = arg;
  4876. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
  4877. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4878. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  4879. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
  4880. return SCTP_DISPOSITION_CONSUME;
  4881. }
  4882. /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
  4883. enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
  4884. const struct sctp_endpoint *ep,
  4885. const struct sctp_association *asoc,
  4886. const union sctp_subtype type,
  4887. void *arg,
  4888. struct sctp_cmd_seq *commands)
  4889. {
  4890. struct sctp_chunk *chunk = arg;
  4891. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
  4892. return SCTP_DISPOSITION_CONSUME;
  4893. }
  4894. /*
  4895. * Ignore the primitive event
  4896. *
  4897. * The return value is the disposition of the primitive.
  4898. */
  4899. enum sctp_disposition sctp_sf_ignore_primitive(
  4900. struct net *net,
  4901. const struct sctp_endpoint *ep,
  4902. const struct sctp_association *asoc,
  4903. const union sctp_subtype type,
  4904. void *arg,
  4905. struct sctp_cmd_seq *commands)
  4906. {
  4907. pr_debug("%s: primitive type:%d is ignored\n", __func__,
  4908. type.primitive);
  4909. return SCTP_DISPOSITION_DISCARD;
  4910. }
  4911. /***************************************************************************
  4912. * These are the state functions for the OTHER events.
  4913. ***************************************************************************/
  4914. /*
  4915. * When the SCTP stack has no more user data to send or retransmit, this
  4916. * notification is given to the user. Also, at the time when a user app
  4917. * subscribes to this event, if there is no data to be sent or
  4918. * retransmit, the stack will immediately send up this notification.
  4919. */
  4920. enum sctp_disposition sctp_sf_do_no_pending_tsn(
  4921. struct net *net,
  4922. const struct sctp_endpoint *ep,
  4923. const struct sctp_association *asoc,
  4924. const union sctp_subtype type,
  4925. void *arg,
  4926. struct sctp_cmd_seq *commands)
  4927. {
  4928. struct sctp_ulpevent *event;
  4929. event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
  4930. if (!event)
  4931. return SCTP_DISPOSITION_NOMEM;
  4932. sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
  4933. return SCTP_DISPOSITION_CONSUME;
  4934. }
  4935. /*
  4936. * Start the shutdown negotiation.
  4937. *
  4938. * From Section 9.2:
  4939. * Once all its outstanding data has been acknowledged, the endpoint
  4940. * shall send a SHUTDOWN chunk to its peer including in the Cumulative
  4941. * TSN Ack field the last sequential TSN it has received from the peer.
  4942. * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
  4943. * state. If the timer expires, the endpoint must re-send the SHUTDOWN
  4944. * with the updated last sequential TSN received from its peer.
  4945. *
  4946. * The return value is the disposition.
  4947. */
  4948. enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
  4949. struct net *net,
  4950. const struct sctp_endpoint *ep,
  4951. const struct sctp_association *asoc,
  4952. const union sctp_subtype type,
  4953. void *arg,
  4954. struct sctp_cmd_seq *commands)
  4955. {
  4956. struct sctp_chunk *reply;
  4957. /* Once all its outstanding data has been acknowledged, the
  4958. * endpoint shall send a SHUTDOWN chunk to its peer including
  4959. * in the Cumulative TSN Ack field the last sequential TSN it
  4960. * has received from the peer.
  4961. */
  4962. reply = sctp_make_shutdown(asoc, arg);
  4963. if (!reply)
  4964. goto nomem;
  4965. /* Set the transport for the SHUTDOWN chunk and the timeout for the
  4966. * T2-shutdown timer.
  4967. */
  4968. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  4969. /* It shall then start the T2-shutdown timer */
  4970. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
  4971. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  4972. /* RFC 4960 Section 9.2
  4973. * The sender of the SHUTDOWN MAY also start an overall guard timer
  4974. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  4975. */
  4976. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  4977. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  4978. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  4979. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  4980. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  4981. /* and enter the SHUTDOWN-SENT state. */
  4982. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  4983. SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
  4984. /* sctp-implguide 2.10 Issues with Heartbeating and failover
  4985. *
  4986. * HEARTBEAT ... is discontinued after sending either SHUTDOWN
  4987. * or SHUTDOWN-ACK.
  4988. */
  4989. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  4990. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  4991. return SCTP_DISPOSITION_CONSUME;
  4992. nomem:
  4993. return SCTP_DISPOSITION_NOMEM;
  4994. }
  4995. /*
  4996. * Generate a SHUTDOWN ACK now that everything is SACK'd.
  4997. *
  4998. * From Section 9.2:
  4999. *
  5000. * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
  5001. * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
  5002. * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
  5003. * endpoint must re-send the SHUTDOWN ACK.
  5004. *
  5005. * The return value is the disposition.
  5006. */
  5007. enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
  5008. struct net *net,
  5009. const struct sctp_endpoint *ep,
  5010. const struct sctp_association *asoc,
  5011. const union sctp_subtype type,
  5012. void *arg,
  5013. struct sctp_cmd_seq *commands)
  5014. {
  5015. struct sctp_chunk *chunk = arg;
  5016. struct sctp_chunk *reply;
  5017. /* There are 2 ways of getting here:
  5018. * 1) called in response to a SHUTDOWN chunk
  5019. * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
  5020. *
  5021. * For the case (2), the arg parameter is set to NULL. We need
  5022. * to check that we have a chunk before accessing it's fields.
  5023. */
  5024. if (chunk) {
  5025. if (!sctp_vtag_verify(chunk, asoc))
  5026. return sctp_sf_pdiscard(net, ep, asoc, type, arg,
  5027. commands);
  5028. /* Make sure that the SHUTDOWN chunk has a valid length. */
  5029. if (!sctp_chunk_length_valid(
  5030. chunk, sizeof(struct sctp_shutdown_chunk)))
  5031. return sctp_sf_violation_chunklen(net, ep, asoc, type,
  5032. arg, commands);
  5033. }
  5034. /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
  5035. * shall send a SHUTDOWN ACK ...
  5036. */
  5037. reply = sctp_make_shutdown_ack(asoc, chunk);
  5038. if (!reply)
  5039. goto nomem;
  5040. /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
  5041. * the T2-shutdown timer.
  5042. */
  5043. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  5044. /* and start/restart a T2-shutdown timer of its own, */
  5045. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  5046. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  5047. if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
  5048. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  5049. SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
  5050. /* Enter the SHUTDOWN-ACK-SENT state. */
  5051. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  5052. SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
  5053. /* sctp-implguide 2.10 Issues with Heartbeating and failover
  5054. *
  5055. * HEARTBEAT ... is discontinued after sending either SHUTDOWN
  5056. * or SHUTDOWN-ACK.
  5057. */
  5058. sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
  5059. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  5060. return SCTP_DISPOSITION_CONSUME;
  5061. nomem:
  5062. return SCTP_DISPOSITION_NOMEM;
  5063. }
  5064. /*
  5065. * Ignore the event defined as other
  5066. *
  5067. * The return value is the disposition of the event.
  5068. */
  5069. enum sctp_disposition sctp_sf_ignore_other(struct net *net,
  5070. const struct sctp_endpoint *ep,
  5071. const struct sctp_association *asoc,
  5072. const union sctp_subtype type,
  5073. void *arg,
  5074. struct sctp_cmd_seq *commands)
  5075. {
  5076. pr_debug("%s: the event other type:%d is ignored\n",
  5077. __func__, type.other);
  5078. return SCTP_DISPOSITION_DISCARD;
  5079. }
  5080. /************************************************************
  5081. * These are the state functions for handling timeout events.
  5082. ************************************************************/
  5083. /*
  5084. * RTX Timeout
  5085. *
  5086. * Section: 6.3.3 Handle T3-rtx Expiration
  5087. *
  5088. * Whenever the retransmission timer T3-rtx expires for a destination
  5089. * address, do the following:
  5090. * [See below]
  5091. *
  5092. * The return value is the disposition of the chunk.
  5093. */
  5094. enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
  5095. const struct sctp_endpoint *ep,
  5096. const struct sctp_association *asoc,
  5097. const union sctp_subtype type,
  5098. void *arg,
  5099. struct sctp_cmd_seq *commands)
  5100. {
  5101. struct sctp_transport *transport = arg;
  5102. SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
  5103. if (asoc->overall_error_count >= asoc->max_retrans) {
  5104. if (asoc->peer.zero_window_announced &&
  5105. asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
  5106. /*
  5107. * We are here likely because the receiver had its rwnd
  5108. * closed for a while and we have not been able to
  5109. * transmit the locally queued data within the maximum
  5110. * retransmission attempts limit. Start the T5
  5111. * shutdown guard timer to give the receiver one last
  5112. * chance and some additional time to recover before
  5113. * aborting.
  5114. */
  5115. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
  5116. SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
  5117. } else {
  5118. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5119. SCTP_ERROR(ETIMEDOUT));
  5120. /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  5121. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5122. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5123. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5124. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5125. return SCTP_DISPOSITION_DELETE_TCB;
  5126. }
  5127. }
  5128. /* E1) For the destination address for which the timer
  5129. * expires, adjust its ssthresh with rules defined in Section
  5130. * 7.2.3 and set the cwnd <- MTU.
  5131. */
  5132. /* E2) For the destination address for which the timer
  5133. * expires, set RTO <- RTO * 2 ("back off the timer"). The
  5134. * maximum value discussed in rule C7 above (RTO.max) may be
  5135. * used to provide an upper bound to this doubling operation.
  5136. */
  5137. /* E3) Determine how many of the earliest (i.e., lowest TSN)
  5138. * outstanding DATA chunks for the address for which the
  5139. * T3-rtx has expired will fit into a single packet, subject
  5140. * to the MTU constraint for the path corresponding to the
  5141. * destination transport address to which the retransmission
  5142. * is being sent (this may be different from the address for
  5143. * which the timer expires [see Section 6.4]). Call this
  5144. * value K. Bundle and retransmit those K DATA chunks in a
  5145. * single packet to the destination endpoint.
  5146. *
  5147. * Note: Any DATA chunks that were sent to the address for
  5148. * which the T3-rtx timer expired but did not fit in one MTU
  5149. * (rule E3 above), should be marked for retransmission and
  5150. * sent as soon as cwnd allows (normally when a SACK arrives).
  5151. */
  5152. /* Do some failure management (Section 8.2). */
  5153. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
  5154. /* NB: Rules E4 and F1 are implicit in R1. */
  5155. sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
  5156. return SCTP_DISPOSITION_CONSUME;
  5157. }
  5158. /*
  5159. * Generate delayed SACK on timeout
  5160. *
  5161. * Section: 6.2 Acknowledgement on Reception of DATA Chunks
  5162. *
  5163. * The guidelines on delayed acknowledgement algorithm specified in
  5164. * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
  5165. * acknowledgement SHOULD be generated for at least every second packet
  5166. * (not every second DATA chunk) received, and SHOULD be generated
  5167. * within 200 ms of the arrival of any unacknowledged DATA chunk. In
  5168. * some situations it may be beneficial for an SCTP transmitter to be
  5169. * more conservative than the algorithms detailed in this document
  5170. * allow. However, an SCTP transmitter MUST NOT be more aggressive than
  5171. * the following algorithms allow.
  5172. */
  5173. enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
  5174. const struct sctp_endpoint *ep,
  5175. const struct sctp_association *asoc,
  5176. const union sctp_subtype type,
  5177. void *arg,
  5178. struct sctp_cmd_seq *commands)
  5179. {
  5180. SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
  5181. sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
  5182. return SCTP_DISPOSITION_CONSUME;
  5183. }
  5184. /*
  5185. * sctp_sf_t1_init_timer_expire
  5186. *
  5187. * Section: 4 Note: 2
  5188. * Verification Tag:
  5189. * Inputs
  5190. * (endpoint, asoc)
  5191. *
  5192. * RFC 2960 Section 4 Notes
  5193. * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
  5194. * and re-start the T1-init timer without changing state. This MUST
  5195. * be repeated up to 'Max.Init.Retransmits' times. After that, the
  5196. * endpoint MUST abort the initialization process and report the
  5197. * error to SCTP user.
  5198. *
  5199. * Outputs
  5200. * (timers, events)
  5201. *
  5202. */
  5203. enum sctp_disposition sctp_sf_t1_init_timer_expire(
  5204. struct net *net,
  5205. const struct sctp_endpoint *ep,
  5206. const struct sctp_association *asoc,
  5207. const union sctp_subtype type,
  5208. void *arg,
  5209. struct sctp_cmd_seq *commands)
  5210. {
  5211. int attempts = asoc->init_err_counter + 1;
  5212. struct sctp_chunk *repl = NULL;
  5213. struct sctp_bind_addr *bp;
  5214. pr_debug("%s: timer T1 expired (INIT)\n", __func__);
  5215. SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
  5216. if (attempts <= asoc->max_init_attempts) {
  5217. bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
  5218. repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
  5219. if (!repl)
  5220. return SCTP_DISPOSITION_NOMEM;
  5221. /* Choose transport for INIT. */
  5222. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  5223. SCTP_CHUNK(repl));
  5224. /* Issue a sideeffect to do the needed accounting. */
  5225. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
  5226. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
  5227. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  5228. } else {
  5229. pr_debug("%s: giving up on INIT, attempts:%d "
  5230. "max_init_attempts:%d\n", __func__, attempts,
  5231. asoc->max_init_attempts);
  5232. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5233. SCTP_ERROR(ETIMEDOUT));
  5234. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  5235. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5236. return SCTP_DISPOSITION_DELETE_TCB;
  5237. }
  5238. return SCTP_DISPOSITION_CONSUME;
  5239. }
  5240. /*
  5241. * sctp_sf_t1_cookie_timer_expire
  5242. *
  5243. * Section: 4 Note: 2
  5244. * Verification Tag:
  5245. * Inputs
  5246. * (endpoint, asoc)
  5247. *
  5248. * RFC 2960 Section 4 Notes
  5249. * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
  5250. * COOKIE ECHO and re-start the T1-cookie timer without changing
  5251. * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
  5252. * After that, the endpoint MUST abort the initialization process and
  5253. * report the error to SCTP user.
  5254. *
  5255. * Outputs
  5256. * (timers, events)
  5257. *
  5258. */
  5259. enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
  5260. struct net *net,
  5261. const struct sctp_endpoint *ep,
  5262. const struct sctp_association *asoc,
  5263. const union sctp_subtype type,
  5264. void *arg,
  5265. struct sctp_cmd_seq *commands)
  5266. {
  5267. int attempts = asoc->init_err_counter + 1;
  5268. struct sctp_chunk *repl = NULL;
  5269. pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
  5270. SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
  5271. if (attempts <= asoc->max_init_attempts) {
  5272. repl = sctp_make_cookie_echo(asoc, NULL);
  5273. if (!repl)
  5274. return SCTP_DISPOSITION_NOMEM;
  5275. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
  5276. SCTP_CHUNK(repl));
  5277. /* Issue a sideeffect to do the needed accounting. */
  5278. sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
  5279. SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
  5280. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
  5281. } else {
  5282. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5283. SCTP_ERROR(ETIMEDOUT));
  5284. sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
  5285. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5286. return SCTP_DISPOSITION_DELETE_TCB;
  5287. }
  5288. return SCTP_DISPOSITION_CONSUME;
  5289. }
  5290. /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
  5291. * with the updated last sequential TSN received from its peer.
  5292. *
  5293. * An endpoint should limit the number of retransmission of the
  5294. * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
  5295. * If this threshold is exceeded the endpoint should destroy the TCB and
  5296. * MUST report the peer endpoint unreachable to the upper layer (and
  5297. * thus the association enters the CLOSED state). The reception of any
  5298. * packet from its peer (i.e. as the peer sends all of its queued DATA
  5299. * chunks) should clear the endpoint's retransmission count and restart
  5300. * the T2-Shutdown timer, giving its peer ample opportunity to transmit
  5301. * all of its queued DATA chunks that have not yet been sent.
  5302. */
  5303. enum sctp_disposition sctp_sf_t2_timer_expire(
  5304. struct net *net,
  5305. const struct sctp_endpoint *ep,
  5306. const struct sctp_association *asoc,
  5307. const union sctp_subtype type,
  5308. void *arg,
  5309. struct sctp_cmd_seq *commands)
  5310. {
  5311. struct sctp_chunk *reply = NULL;
  5312. pr_debug("%s: timer T2 expired\n", __func__);
  5313. SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
  5314. ((struct sctp_association *)asoc)->shutdown_retries++;
  5315. if (asoc->overall_error_count >= asoc->max_retrans) {
  5316. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5317. SCTP_ERROR(ETIMEDOUT));
  5318. /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
  5319. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5320. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5321. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5322. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5323. return SCTP_DISPOSITION_DELETE_TCB;
  5324. }
  5325. switch (asoc->state) {
  5326. case SCTP_STATE_SHUTDOWN_SENT:
  5327. reply = sctp_make_shutdown(asoc, NULL);
  5328. break;
  5329. case SCTP_STATE_SHUTDOWN_ACK_SENT:
  5330. reply = sctp_make_shutdown_ack(asoc, NULL);
  5331. break;
  5332. default:
  5333. BUG();
  5334. break;
  5335. }
  5336. if (!reply)
  5337. goto nomem;
  5338. /* Do some failure management (Section 8.2).
  5339. * If we remove the transport an SHUTDOWN was last sent to, don't
  5340. * do failure management.
  5341. */
  5342. if (asoc->shutdown_last_sent_to)
  5343. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
  5344. SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
  5345. /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
  5346. * the T2-shutdown timer.
  5347. */
  5348. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
  5349. /* Restart the T2-shutdown timer. */
  5350. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  5351. SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
  5352. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  5353. return SCTP_DISPOSITION_CONSUME;
  5354. nomem:
  5355. return SCTP_DISPOSITION_NOMEM;
  5356. }
  5357. /*
  5358. * ADDIP Section 4.1 ASCONF Chunk Procedures
  5359. * If the T4 RTO timer expires the endpoint should do B1 to B5
  5360. */
  5361. enum sctp_disposition sctp_sf_t4_timer_expire(
  5362. struct net *net,
  5363. const struct sctp_endpoint *ep,
  5364. const struct sctp_association *asoc,
  5365. const union sctp_subtype type,
  5366. void *arg,
  5367. struct sctp_cmd_seq *commands)
  5368. {
  5369. struct sctp_chunk *chunk = asoc->addip_last_asconf;
  5370. struct sctp_transport *transport = chunk->transport;
  5371. SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
  5372. /* ADDIP 4.1 B1) Increment the error counters and perform path failure
  5373. * detection on the appropriate destination address as defined in
  5374. * RFC2960 [5] section 8.1 and 8.2.
  5375. */
  5376. if (transport)
  5377. sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
  5378. SCTP_TRANSPORT(transport));
  5379. /* Reconfig T4 timer and transport. */
  5380. sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
  5381. /* ADDIP 4.1 B2) Increment the association error counters and perform
  5382. * endpoint failure detection on the association as defined in
  5383. * RFC2960 [5] section 8.1 and 8.2.
  5384. * association error counter is incremented in SCTP_CMD_STRIKE.
  5385. */
  5386. if (asoc->overall_error_count >= asoc->max_retrans) {
  5387. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
  5388. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  5389. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5390. SCTP_ERROR(ETIMEDOUT));
  5391. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5392. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5393. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5394. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5395. return SCTP_DISPOSITION_ABORT;
  5396. }
  5397. /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
  5398. * the ASCONF chunk was sent by doubling the RTO timer value.
  5399. * This is done in SCTP_CMD_STRIKE.
  5400. */
  5401. /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
  5402. * choose an alternate destination address (please refer to RFC2960
  5403. * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
  5404. * chunk, it MUST be the same (including its serial number) as the last
  5405. * ASCONF sent.
  5406. */
  5407. sctp_chunk_hold(asoc->addip_last_asconf);
  5408. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  5409. SCTP_CHUNK(asoc->addip_last_asconf));
  5410. /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
  5411. * destination is selected, then the RTO used will be that of the new
  5412. * destination address.
  5413. */
  5414. sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
  5415. SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
  5416. return SCTP_DISPOSITION_CONSUME;
  5417. }
  5418. /* sctpimpguide-05 Section 2.12.2
  5419. * The sender of the SHUTDOWN MAY also start an overall guard timer
  5420. * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
  5421. * At the expiration of this timer the sender SHOULD abort the association
  5422. * by sending an ABORT chunk.
  5423. */
  5424. enum sctp_disposition sctp_sf_t5_timer_expire(
  5425. struct net *net,
  5426. const struct sctp_endpoint *ep,
  5427. const struct sctp_association *asoc,
  5428. const union sctp_subtype type,
  5429. void *arg,
  5430. struct sctp_cmd_seq *commands)
  5431. {
  5432. struct sctp_chunk *reply = NULL;
  5433. pr_debug("%s: timer T5 expired\n", __func__);
  5434. SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
  5435. reply = sctp_make_abort(asoc, NULL, 0);
  5436. if (!reply)
  5437. goto nomem;
  5438. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
  5439. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5440. SCTP_ERROR(ETIMEDOUT));
  5441. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5442. SCTP_PERR(SCTP_ERROR_NO_ERROR));
  5443. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5444. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5445. return SCTP_DISPOSITION_DELETE_TCB;
  5446. nomem:
  5447. return SCTP_DISPOSITION_NOMEM;
  5448. }
  5449. /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
  5450. * the association is automatically closed by starting the shutdown process.
  5451. * The work that needs to be done is same as when SHUTDOWN is initiated by
  5452. * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
  5453. */
  5454. enum sctp_disposition sctp_sf_autoclose_timer_expire(
  5455. struct net *net,
  5456. const struct sctp_endpoint *ep,
  5457. const struct sctp_association *asoc,
  5458. const union sctp_subtype type,
  5459. void *arg,
  5460. struct sctp_cmd_seq *commands)
  5461. {
  5462. enum sctp_disposition disposition;
  5463. SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
  5464. /* From 9.2 Shutdown of an Association
  5465. * Upon receipt of the SHUTDOWN primitive from its upper
  5466. * layer, the endpoint enters SHUTDOWN-PENDING state and
  5467. * remains there until all outstanding data has been
  5468. * acknowledged by its peer. The endpoint accepts no new data
  5469. * from its upper layer, but retransmits data to the far end
  5470. * if necessary to fill gaps.
  5471. */
  5472. sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
  5473. SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
  5474. disposition = SCTP_DISPOSITION_CONSUME;
  5475. if (sctp_outq_is_empty(&asoc->outqueue)) {
  5476. disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
  5477. NULL, commands);
  5478. }
  5479. return disposition;
  5480. }
  5481. /*****************************************************************************
  5482. * These are sa state functions which could apply to all types of events.
  5483. ****************************************************************************/
  5484. /*
  5485. * This table entry is not implemented.
  5486. *
  5487. * Inputs
  5488. * (endpoint, asoc, chunk)
  5489. *
  5490. * The return value is the disposition of the chunk.
  5491. */
  5492. enum sctp_disposition sctp_sf_not_impl(struct net *net,
  5493. const struct sctp_endpoint *ep,
  5494. const struct sctp_association *asoc,
  5495. const union sctp_subtype type,
  5496. void *arg, struct sctp_cmd_seq *commands)
  5497. {
  5498. return SCTP_DISPOSITION_NOT_IMPL;
  5499. }
  5500. /*
  5501. * This table entry represents a bug.
  5502. *
  5503. * Inputs
  5504. * (endpoint, asoc, chunk)
  5505. *
  5506. * The return value is the disposition of the chunk.
  5507. */
  5508. enum sctp_disposition sctp_sf_bug(struct net *net,
  5509. const struct sctp_endpoint *ep,
  5510. const struct sctp_association *asoc,
  5511. const union sctp_subtype type,
  5512. void *arg, struct sctp_cmd_seq *commands)
  5513. {
  5514. return SCTP_DISPOSITION_BUG;
  5515. }
  5516. /*
  5517. * This table entry represents the firing of a timer in the wrong state.
  5518. * Since timer deletion cannot be guaranteed a timer 'may' end up firing
  5519. * when the association is in the wrong state. This event should
  5520. * be ignored, so as to prevent any rearming of the timer.
  5521. *
  5522. * Inputs
  5523. * (endpoint, asoc, chunk)
  5524. *
  5525. * The return value is the disposition of the chunk.
  5526. */
  5527. enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
  5528. const struct sctp_endpoint *ep,
  5529. const struct sctp_association *asoc,
  5530. const union sctp_subtype type,
  5531. void *arg,
  5532. struct sctp_cmd_seq *commands)
  5533. {
  5534. pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
  5535. return SCTP_DISPOSITION_CONSUME;
  5536. }
  5537. /********************************************************************
  5538. * 2nd Level Abstractions
  5539. ********************************************************************/
  5540. /* Pull the SACK chunk based on the SACK header. */
  5541. static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
  5542. {
  5543. struct sctp_sackhdr *sack;
  5544. __u16 num_dup_tsns;
  5545. unsigned int len;
  5546. __u16 num_blocks;
  5547. /* Protect ourselves from reading too far into
  5548. * the skb from a bogus sender.
  5549. */
  5550. sack = (struct sctp_sackhdr *) chunk->skb->data;
  5551. num_blocks = ntohs(sack->num_gap_ack_blocks);
  5552. num_dup_tsns = ntohs(sack->num_dup_tsns);
  5553. len = sizeof(struct sctp_sackhdr);
  5554. len += (num_blocks + num_dup_tsns) * sizeof(__u32);
  5555. if (len > chunk->skb->len)
  5556. return NULL;
  5557. skb_pull(chunk->skb, len);
  5558. return sack;
  5559. }
  5560. /* Create an ABORT packet to be sent as a response, with the specified
  5561. * error causes.
  5562. */
  5563. static struct sctp_packet *sctp_abort_pkt_new(
  5564. struct net *net,
  5565. const struct sctp_endpoint *ep,
  5566. const struct sctp_association *asoc,
  5567. struct sctp_chunk *chunk,
  5568. const void *payload, size_t paylen)
  5569. {
  5570. struct sctp_packet *packet;
  5571. struct sctp_chunk *abort;
  5572. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  5573. if (packet) {
  5574. /* Make an ABORT.
  5575. * The T bit will be set if the asoc is NULL.
  5576. */
  5577. abort = sctp_make_abort(asoc, chunk, paylen);
  5578. if (!abort) {
  5579. sctp_ootb_pkt_free(packet);
  5580. return NULL;
  5581. }
  5582. /* Reflect vtag if T-Bit is set */
  5583. if (sctp_test_T_bit(abort))
  5584. packet->vtag = ntohl(chunk->sctp_hdr->vtag);
  5585. /* Add specified error causes, i.e., payload, to the
  5586. * end of the chunk.
  5587. */
  5588. sctp_addto_chunk(abort, paylen, payload);
  5589. /* Set the skb to the belonging sock for accounting. */
  5590. abort->skb->sk = ep->base.sk;
  5591. sctp_packet_append_chunk(packet, abort);
  5592. }
  5593. return packet;
  5594. }
  5595. /* Allocate a packet for responding in the OOTB conditions. */
  5596. static struct sctp_packet *sctp_ootb_pkt_new(
  5597. struct net *net,
  5598. const struct sctp_association *asoc,
  5599. const struct sctp_chunk *chunk)
  5600. {
  5601. struct sctp_transport *transport;
  5602. struct sctp_packet *packet;
  5603. __u16 sport, dport;
  5604. __u32 vtag;
  5605. /* Get the source and destination port from the inbound packet. */
  5606. sport = ntohs(chunk->sctp_hdr->dest);
  5607. dport = ntohs(chunk->sctp_hdr->source);
  5608. /* The V-tag is going to be the same as the inbound packet if no
  5609. * association exists, otherwise, use the peer's vtag.
  5610. */
  5611. if (asoc) {
  5612. /* Special case the INIT-ACK as there is no peer's vtag
  5613. * yet.
  5614. */
  5615. switch (chunk->chunk_hdr->type) {
  5616. case SCTP_CID_INIT:
  5617. case SCTP_CID_INIT_ACK:
  5618. {
  5619. struct sctp_initack_chunk *initack;
  5620. initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
  5621. vtag = ntohl(initack->init_hdr.init_tag);
  5622. break;
  5623. }
  5624. default:
  5625. vtag = asoc->peer.i.init_tag;
  5626. break;
  5627. }
  5628. } else {
  5629. /* Special case the INIT and stale COOKIE_ECHO as there is no
  5630. * vtag yet.
  5631. */
  5632. switch (chunk->chunk_hdr->type) {
  5633. case SCTP_CID_INIT:
  5634. {
  5635. struct sctp_init_chunk *init;
  5636. init = (struct sctp_init_chunk *)chunk->chunk_hdr;
  5637. vtag = ntohl(init->init_hdr.init_tag);
  5638. break;
  5639. }
  5640. default:
  5641. vtag = ntohl(chunk->sctp_hdr->vtag);
  5642. break;
  5643. }
  5644. }
  5645. /* Make a transport for the bucket, Eliza... */
  5646. transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
  5647. if (!transport)
  5648. goto nomem;
  5649. transport->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
  5650. /* Cache a route for the transport with the chunk's destination as
  5651. * the source address.
  5652. */
  5653. sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
  5654. sctp_sk(net->sctp.ctl_sock));
  5655. packet = &transport->packet;
  5656. sctp_packet_init(packet, transport, sport, dport);
  5657. sctp_packet_config(packet, vtag, 0);
  5658. return packet;
  5659. nomem:
  5660. return NULL;
  5661. }
  5662. /* Free the packet allocated earlier for responding in the OOTB condition. */
  5663. void sctp_ootb_pkt_free(struct sctp_packet *packet)
  5664. {
  5665. sctp_transport_free(packet->transport);
  5666. }
  5667. /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
  5668. static void sctp_send_stale_cookie_err(struct net *net,
  5669. const struct sctp_endpoint *ep,
  5670. const struct sctp_association *asoc,
  5671. const struct sctp_chunk *chunk,
  5672. struct sctp_cmd_seq *commands,
  5673. struct sctp_chunk *err_chunk)
  5674. {
  5675. struct sctp_packet *packet;
  5676. if (err_chunk) {
  5677. packet = sctp_ootb_pkt_new(net, asoc, chunk);
  5678. if (packet) {
  5679. struct sctp_signed_cookie *cookie;
  5680. /* Override the OOTB vtag from the cookie. */
  5681. cookie = chunk->subh.cookie_hdr;
  5682. packet->vtag = cookie->c.peer_vtag;
  5683. /* Set the skb to the belonging sock for accounting. */
  5684. err_chunk->skb->sk = ep->base.sk;
  5685. sctp_packet_append_chunk(packet, err_chunk);
  5686. sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
  5687. SCTP_PACKET(packet));
  5688. SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
  5689. } else
  5690. sctp_chunk_free (err_chunk);
  5691. }
  5692. }
  5693. /* Process a data chunk */
  5694. static int sctp_eat_data(const struct sctp_association *asoc,
  5695. struct sctp_chunk *chunk,
  5696. struct sctp_cmd_seq *commands)
  5697. {
  5698. struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
  5699. struct sock *sk = asoc->base.sk;
  5700. struct net *net = sock_net(sk);
  5701. struct sctp_datahdr *data_hdr;
  5702. struct sctp_chunk *err;
  5703. enum sctp_verb deliver;
  5704. size_t datalen;
  5705. __u32 tsn;
  5706. int tmp;
  5707. data_hdr = (struct sctp_datahdr *)chunk->skb->data;
  5708. chunk->subh.data_hdr = data_hdr;
  5709. skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
  5710. tsn = ntohl(data_hdr->tsn);
  5711. pr_debug("%s: TSN 0x%x\n", __func__, tsn);
  5712. /* ASSERT: Now skb->data is really the user data. */
  5713. /* Process ECN based congestion.
  5714. *
  5715. * Since the chunk structure is reused for all chunks within
  5716. * a packet, we use ecn_ce_done to track if we've already
  5717. * done CE processing for this packet.
  5718. *
  5719. * We need to do ECN processing even if we plan to discard the
  5720. * chunk later.
  5721. */
  5722. if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
  5723. struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
  5724. chunk->ecn_ce_done = 1;
  5725. if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
  5726. /* Do real work as side effect. */
  5727. sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
  5728. SCTP_U32(tsn));
  5729. }
  5730. }
  5731. tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
  5732. if (tmp < 0) {
  5733. /* The TSN is too high--silently discard the chunk and
  5734. * count on it getting retransmitted later.
  5735. */
  5736. if (chunk->asoc)
  5737. chunk->asoc->stats.outofseqtsns++;
  5738. return SCTP_IERROR_HIGH_TSN;
  5739. } else if (tmp > 0) {
  5740. /* This is a duplicate. Record it. */
  5741. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
  5742. return SCTP_IERROR_DUP_TSN;
  5743. }
  5744. /* This is a new TSN. */
  5745. /* Discard if there is no room in the receive window.
  5746. * Actually, allow a little bit of overflow (up to a MTU).
  5747. */
  5748. datalen = ntohs(chunk->chunk_hdr->length);
  5749. datalen -= sctp_datachk_len(&asoc->stream);
  5750. deliver = SCTP_CMD_CHUNK_ULP;
  5751. /* Think about partial delivery. */
  5752. if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
  5753. /* Even if we don't accept this chunk there is
  5754. * memory pressure.
  5755. */
  5756. sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
  5757. }
  5758. /* Spill over rwnd a little bit. Note: While allowed, this spill over
  5759. * seems a bit troublesome in that frag_point varies based on
  5760. * PMTU. In cases, such as loopback, this might be a rather
  5761. * large spill over.
  5762. */
  5763. if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
  5764. (datalen > asoc->rwnd + asoc->frag_point))) {
  5765. /* If this is the next TSN, consider reneging to make
  5766. * room. Note: Playing nice with a confused sender. A
  5767. * malicious sender can still eat up all our buffer
  5768. * space and in the future we may want to detect and
  5769. * do more drastic reneging.
  5770. */
  5771. if (sctp_tsnmap_has_gap(map) &&
  5772. (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
  5773. pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
  5774. deliver = SCTP_CMD_RENEGE;
  5775. } else {
  5776. pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
  5777. __func__, tsn, datalen, asoc->rwnd);
  5778. return SCTP_IERROR_IGNORE_TSN;
  5779. }
  5780. }
  5781. /*
  5782. * Also try to renege to limit our memory usage in the event that
  5783. * we are under memory pressure
  5784. * If we can't renege, don't worry about it, the sk_rmem_schedule
  5785. * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
  5786. * memory usage too much
  5787. */
  5788. if (sk_under_memory_pressure(sk)) {
  5789. if (sctp_tsnmap_has_gap(map) &&
  5790. (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
  5791. pr_debug("%s: under pressure, reneging for tsn:%u\n",
  5792. __func__, tsn);
  5793. deliver = SCTP_CMD_RENEGE;
  5794. }
  5795. }
  5796. /*
  5797. * Section 3.3.10.9 No User Data (9)
  5798. *
  5799. * Cause of error
  5800. * ---------------
  5801. * No User Data: This error cause is returned to the originator of a
  5802. * DATA chunk if a received DATA chunk has no user data.
  5803. */
  5804. if (unlikely(0 == datalen)) {
  5805. err = sctp_make_abort_no_data(asoc, chunk, tsn);
  5806. if (err) {
  5807. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  5808. SCTP_CHUNK(err));
  5809. }
  5810. /* We are going to ABORT, so we might as well stop
  5811. * processing the rest of the chunks in the packet.
  5812. */
  5813. sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
  5814. sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
  5815. SCTP_ERROR(ECONNABORTED));
  5816. sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
  5817. SCTP_PERR(SCTP_ERROR_NO_DATA));
  5818. SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  5819. SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
  5820. return SCTP_IERROR_NO_DATA;
  5821. }
  5822. chunk->data_accepted = 1;
  5823. /* Note: Some chunks may get overcounted (if we drop) or overcounted
  5824. * if we renege and the chunk arrives again.
  5825. */
  5826. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  5827. SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
  5828. if (chunk->asoc)
  5829. chunk->asoc->stats.iuodchunks++;
  5830. } else {
  5831. SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
  5832. if (chunk->asoc)
  5833. chunk->asoc->stats.iodchunks++;
  5834. }
  5835. /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
  5836. *
  5837. * If an endpoint receive a DATA chunk with an invalid stream
  5838. * identifier, it shall acknowledge the reception of the DATA chunk
  5839. * following the normal procedure, immediately send an ERROR chunk
  5840. * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
  5841. * and discard the DATA chunk.
  5842. */
  5843. if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
  5844. /* Mark tsn as received even though we drop it */
  5845. sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
  5846. err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
  5847. &data_hdr->stream,
  5848. sizeof(data_hdr->stream),
  5849. sizeof(u16));
  5850. if (err)
  5851. sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
  5852. SCTP_CHUNK(err));
  5853. return SCTP_IERROR_BAD_STREAM;
  5854. }
  5855. /* Check to see if the SSN is possible for this TSN.
  5856. * The biggest gap we can record is 4K wide. Since SSNs wrap
  5857. * at an unsigned short, there is no way that an SSN can
  5858. * wrap and for a valid TSN. We can simply check if the current
  5859. * SSN is smaller then the next expected one. If it is, it wrapped
  5860. * and is invalid.
  5861. */
  5862. if (!asoc->stream.si->validate_data(chunk))
  5863. return SCTP_IERROR_PROTO_VIOLATION;
  5864. /* Send the data up to the user. Note: Schedule the
  5865. * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
  5866. * chunk needs the updated rwnd.
  5867. */
  5868. sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
  5869. return SCTP_IERROR_NO_ERROR;
  5870. }