sm_statefuns.c 201 KB

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