sm_statefuns.c 188 KB

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