sm_statefuns.c 182 KB

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