sm_statefuns.c 190 KB

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