sm_statefuns.c 185 KB

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