sm_statefuns.c 201 KB

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