sm_statefuns.c 169 KB

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