sm_statefuns.c 175 KB

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