sm_statefuns.c 167 KB

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