sm_statefuns.c 190 KB

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