sm_statefuns.c 186 KB

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