sm_statefuns.c 190 KB

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