sm_statefuns.c 188 KB

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