sm_statefuns.c 169 KB

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