sm_statefuns.c 205 KB

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