sm_statefuns.c 201 KB

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