sm_statefuns.c 171 KB

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