sm_statefuns.c 185 KB

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