sm_make_chunk.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843
  1. /* SCTP kernel 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. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * These functions work with the state functions in sctp_sm_statefuns.c
  10. * to implement the state operations. These functions implement the
  11. * steps which require modifying existing data structures.
  12. *
  13. * This SCTP implementation is free software;
  14. * you can redistribute it and/or modify it under the terms of
  15. * the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * This SCTP implementation is distributed in the hope that it
  20. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  21. * ************************
  22. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. * See the GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with GNU CC; see the file COPYING. If not, see
  27. * <http://www.gnu.org/licenses/>.
  28. *
  29. * Please send any bug reports or fixes you make to the
  30. * email address(es):
  31. * lksctp developers <linux-sctp@vger.kernel.org>
  32. *
  33. * Written or modified by:
  34. * La Monte H.P. Yarroll <piggy@acm.org>
  35. * Karl Knutson <karl@athena.chicago.il.us>
  36. * C. Robin <chris@hundredacre.ac.uk>
  37. * Jon Grimm <jgrimm@us.ibm.com>
  38. * Xingang Guo <xingang.guo@intel.com>
  39. * Dajiang Zhang <dajiang.zhang@nokia.com>
  40. * Sridhar Samudrala <sri@us.ibm.com>
  41. * Daisy Chang <daisyc@us.ibm.com>
  42. * Ardelle Fan <ardelle.fan@intel.com>
  43. * Kevin Gao <kevin.gao@intel.com>
  44. */
  45. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  46. #include <crypto/hash.h>
  47. #include <linux/types.h>
  48. #include <linux/kernel.h>
  49. #include <linux/ip.h>
  50. #include <linux/ipv6.h>
  51. #include <linux/net.h>
  52. #include <linux/inet.h>
  53. #include <linux/scatterlist.h>
  54. #include <linux/slab.h>
  55. #include <net/sock.h>
  56. #include <linux/skbuff.h>
  57. #include <linux/random.h> /* for get_random_bytes */
  58. #include <net/sctp/sctp.h>
  59. #include <net/sctp/sm.h>
  60. static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
  61. __u8 type, __u8 flags, int paylen,
  62. gfp_t gfp);
  63. static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
  64. __u8 flags, int paylen, gfp_t gfp);
  65. static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
  66. __u8 type, __u8 flags, int paylen,
  67. gfp_t gfp);
  68. static struct sctp_cookie_param *sctp_pack_cookie(
  69. const struct sctp_endpoint *ep,
  70. const struct sctp_association *asoc,
  71. const struct sctp_chunk *init_chunk,
  72. int *cookie_len,
  73. const __u8 *raw_addrs, int addrs_len);
  74. static int sctp_process_param(struct sctp_association *asoc,
  75. union sctp_params param,
  76. const union sctp_addr *peer_addr,
  77. gfp_t gfp);
  78. static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
  79. const void *data);
  80. static void *sctp_addto_chunk_fixed(struct sctp_chunk *, int len,
  81. const void *data);
  82. /* Control chunk destructor */
  83. static void sctp_control_release_owner(struct sk_buff *skb)
  84. {
  85. /*TODO: do memory release */
  86. }
  87. static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
  88. {
  89. struct sctp_association *asoc = chunk->asoc;
  90. struct sk_buff *skb = chunk->skb;
  91. /* TODO: properly account for control chunks.
  92. * To do it right we'll need:
  93. * 1) endpoint if association isn't known.
  94. * 2) proper memory accounting.
  95. *
  96. * For now don't do anything for now.
  97. */
  98. skb->sk = asoc ? asoc->base.sk : NULL;
  99. skb->destructor = sctp_control_release_owner;
  100. }
  101. /* What was the inbound interface for this chunk? */
  102. int sctp_chunk_iif(const struct sctp_chunk *chunk)
  103. {
  104. struct sk_buff *skb = chunk->skb;
  105. return SCTP_INPUT_CB(skb)->af->skb_iif(skb);
  106. }
  107. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  108. *
  109. * Note 2: The ECN capable field is reserved for future use of
  110. * Explicit Congestion Notification.
  111. */
  112. static const struct sctp_paramhdr ecap_param = {
  113. SCTP_PARAM_ECN_CAPABLE,
  114. cpu_to_be16(sizeof(struct sctp_paramhdr)),
  115. };
  116. static const struct sctp_paramhdr prsctp_param = {
  117. SCTP_PARAM_FWD_TSN_SUPPORT,
  118. cpu_to_be16(sizeof(struct sctp_paramhdr)),
  119. };
  120. /* A helper to initialize an op error inside a
  121. * provided chunk, as most cause codes will be embedded inside an
  122. * abort chunk.
  123. */
  124. void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
  125. size_t paylen)
  126. {
  127. struct sctp_errhdr err;
  128. __u16 len;
  129. /* Cause code constants are now defined in network order. */
  130. err.cause = cause_code;
  131. len = sizeof(err) + paylen;
  132. err.length = htons(len);
  133. chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(err), &err);
  134. }
  135. /* A helper to initialize an op error inside a
  136. * provided chunk, as most cause codes will be embedded inside an
  137. * abort chunk. Differs from sctp_init_cause in that it won't oops
  138. * if there isn't enough space in the op error chunk
  139. */
  140. static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
  141. size_t paylen)
  142. {
  143. struct sctp_errhdr err;
  144. __u16 len;
  145. /* Cause code constants are now defined in network order. */
  146. err.cause = cause_code;
  147. len = sizeof(err) + paylen;
  148. err.length = htons(len);
  149. if (skb_tailroom(chunk->skb) < len)
  150. return -ENOSPC;
  151. chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk, sizeof(err), &err);
  152. return 0;
  153. }
  154. /* 3.3.2 Initiation (INIT) (1)
  155. *
  156. * This chunk is used to initiate a SCTP association between two
  157. * endpoints. The format of the INIT chunk is shown below:
  158. *
  159. * 0 1 2 3
  160. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  161. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  162. * | Type = 1 | Chunk Flags | Chunk Length |
  163. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  164. * | Initiate Tag |
  165. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  166. * | Advertised Receiver Window Credit (a_rwnd) |
  167. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  168. * | Number of Outbound Streams | Number of Inbound Streams |
  169. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  170. * | Initial TSN |
  171. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  172. * \ \
  173. * / Optional/Variable-Length Parameters /
  174. * \ \
  175. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  176. *
  177. *
  178. * The INIT chunk contains the following parameters. Unless otherwise
  179. * noted, each parameter MUST only be included once in the INIT chunk.
  180. *
  181. * Fixed Parameters Status
  182. * ----------------------------------------------
  183. * Initiate Tag Mandatory
  184. * Advertised Receiver Window Credit Mandatory
  185. * Number of Outbound Streams Mandatory
  186. * Number of Inbound Streams Mandatory
  187. * Initial TSN Mandatory
  188. *
  189. * Variable Parameters Status Type Value
  190. * -------------------------------------------------------------
  191. * IPv4 Address (Note 1) Optional 5
  192. * IPv6 Address (Note 1) Optional 6
  193. * Cookie Preservative Optional 9
  194. * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
  195. * Host Name Address (Note 3) Optional 11
  196. * Supported Address Types (Note 4) Optional 12
  197. */
  198. struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
  199. const struct sctp_bind_addr *bp,
  200. gfp_t gfp, int vparam_len)
  201. {
  202. struct net *net = sock_net(asoc->base.sk);
  203. struct sctp_supported_ext_param ext_param;
  204. struct sctp_adaptation_ind_param aiparam;
  205. struct sctp_paramhdr *auth_chunks = NULL;
  206. struct sctp_paramhdr *auth_hmacs = NULL;
  207. struct sctp_supported_addrs_param sat;
  208. struct sctp_endpoint *ep = asoc->ep;
  209. struct sctp_chunk *retval = NULL;
  210. int num_types, addrs_len = 0;
  211. struct sctp_inithdr init;
  212. union sctp_params addrs;
  213. struct sctp_sock *sp;
  214. __u8 extensions[4];
  215. size_t chunksize;
  216. __be16 types[2];
  217. int num_ext = 0;
  218. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  219. *
  220. * Note 1: The INIT chunks can contain multiple addresses that
  221. * can be IPv4 and/or IPv6 in any combination.
  222. */
  223. /* Convert the provided bind address list to raw format. */
  224. addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
  225. init.init_tag = htonl(asoc->c.my_vtag);
  226. init.a_rwnd = htonl(asoc->rwnd);
  227. init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
  228. init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
  229. init.initial_tsn = htonl(asoc->c.initial_tsn);
  230. /* How many address types are needed? */
  231. sp = sctp_sk(asoc->base.sk);
  232. num_types = sp->pf->supported_addrs(sp, types);
  233. chunksize = sizeof(init) + addrs_len;
  234. chunksize += SCTP_PAD4(SCTP_SAT_LEN(num_types));
  235. chunksize += sizeof(ecap_param);
  236. if (asoc->prsctp_enable)
  237. chunksize += sizeof(prsctp_param);
  238. /* ADDIP: Section 4.2.7:
  239. * An implementation supporting this extension [ADDIP] MUST list
  240. * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
  241. * INIT-ACK parameters.
  242. */
  243. if (net->sctp.addip_enable) {
  244. extensions[num_ext] = SCTP_CID_ASCONF;
  245. extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
  246. num_ext += 2;
  247. }
  248. if (asoc->reconf_enable) {
  249. extensions[num_ext] = SCTP_CID_RECONF;
  250. num_ext += 1;
  251. }
  252. if (sp->adaptation_ind)
  253. chunksize += sizeof(aiparam);
  254. chunksize += vparam_len;
  255. /* Account for AUTH related parameters */
  256. if (ep->auth_enable) {
  257. /* Add random parameter length*/
  258. chunksize += sizeof(asoc->c.auth_random);
  259. /* Add HMACS parameter length if any were defined */
  260. auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
  261. if (auth_hmacs->length)
  262. chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
  263. else
  264. auth_hmacs = NULL;
  265. /* Add CHUNKS parameter length */
  266. auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
  267. if (auth_chunks->length)
  268. chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
  269. else
  270. auth_chunks = NULL;
  271. extensions[num_ext] = SCTP_CID_AUTH;
  272. num_ext += 1;
  273. }
  274. /* If we have any extensions to report, account for that */
  275. if (num_ext)
  276. chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
  277. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  278. *
  279. * Note 3: An INIT chunk MUST NOT contain more than one Host
  280. * Name address parameter. Moreover, the sender of the INIT
  281. * MUST NOT combine any other address types with the Host Name
  282. * address in the INIT. The receiver of INIT MUST ignore any
  283. * other address types if the Host Name address parameter is
  284. * present in the received INIT chunk.
  285. *
  286. * PLEASE DO NOT FIXME [This version does not support Host Name.]
  287. */
  288. retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
  289. if (!retval)
  290. goto nodata;
  291. retval->subh.init_hdr =
  292. sctp_addto_chunk(retval, sizeof(init), &init);
  293. retval->param_hdr.v =
  294. sctp_addto_chunk(retval, addrs_len, addrs.v);
  295. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  296. *
  297. * Note 4: This parameter, when present, specifies all the
  298. * address types the sending endpoint can support. The absence
  299. * of this parameter indicates that the sending endpoint can
  300. * support any address type.
  301. */
  302. sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
  303. sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
  304. sctp_addto_chunk(retval, sizeof(sat), &sat);
  305. sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
  306. sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
  307. /* Add the supported extensions parameter. Be nice and add this
  308. * fist before addiding the parameters for the extensions themselves
  309. */
  310. if (num_ext) {
  311. ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
  312. ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
  313. sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
  314. sctp_addto_param(retval, num_ext, extensions);
  315. }
  316. if (asoc->prsctp_enable)
  317. sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
  318. if (sp->adaptation_ind) {
  319. aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
  320. aiparam.param_hdr.length = htons(sizeof(aiparam));
  321. aiparam.adaptation_ind = htonl(sp->adaptation_ind);
  322. sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
  323. }
  324. /* Add SCTP-AUTH chunks to the parameter list */
  325. if (ep->auth_enable) {
  326. sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
  327. asoc->c.auth_random);
  328. if (auth_hmacs)
  329. sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
  330. auth_hmacs);
  331. if (auth_chunks)
  332. sctp_addto_chunk(retval, ntohs(auth_chunks->length),
  333. auth_chunks);
  334. }
  335. nodata:
  336. kfree(addrs.v);
  337. return retval;
  338. }
  339. struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
  340. const struct sctp_chunk *chunk,
  341. gfp_t gfp, int unkparam_len)
  342. {
  343. struct sctp_supported_ext_param ext_param;
  344. struct sctp_adaptation_ind_param aiparam;
  345. struct sctp_paramhdr *auth_chunks = NULL;
  346. struct sctp_paramhdr *auth_random = NULL;
  347. struct sctp_paramhdr *auth_hmacs = NULL;
  348. struct sctp_chunk *retval = NULL;
  349. struct sctp_cookie_param *cookie;
  350. struct sctp_inithdr initack;
  351. union sctp_params addrs;
  352. struct sctp_sock *sp;
  353. __u8 extensions[4];
  354. size_t chunksize;
  355. int num_ext = 0;
  356. int cookie_len;
  357. int addrs_len;
  358. /* Note: there may be no addresses to embed. */
  359. addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
  360. initack.init_tag = htonl(asoc->c.my_vtag);
  361. initack.a_rwnd = htonl(asoc->rwnd);
  362. initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
  363. initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
  364. initack.initial_tsn = htonl(asoc->c.initial_tsn);
  365. /* FIXME: We really ought to build the cookie right
  366. * into the packet instead of allocating more fresh memory.
  367. */
  368. cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
  369. addrs.v, addrs_len);
  370. if (!cookie)
  371. goto nomem_cookie;
  372. /* Calculate the total size of allocation, include the reserved
  373. * space for reporting unknown parameters if it is specified.
  374. */
  375. sp = sctp_sk(asoc->base.sk);
  376. chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
  377. /* Tell peer that we'll do ECN only if peer advertised such cap. */
  378. if (asoc->peer.ecn_capable)
  379. chunksize += sizeof(ecap_param);
  380. if (asoc->peer.prsctp_capable)
  381. chunksize += sizeof(prsctp_param);
  382. if (asoc->peer.asconf_capable) {
  383. extensions[num_ext] = SCTP_CID_ASCONF;
  384. extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
  385. num_ext += 2;
  386. }
  387. if (asoc->peer.reconf_capable) {
  388. extensions[num_ext] = SCTP_CID_RECONF;
  389. num_ext += 1;
  390. }
  391. if (sp->adaptation_ind)
  392. chunksize += sizeof(aiparam);
  393. if (asoc->peer.auth_capable) {
  394. auth_random = (struct sctp_paramhdr *)asoc->c.auth_random;
  395. chunksize += ntohs(auth_random->length);
  396. auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
  397. if (auth_hmacs->length)
  398. chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
  399. else
  400. auth_hmacs = NULL;
  401. auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
  402. if (auth_chunks->length)
  403. chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
  404. else
  405. auth_chunks = NULL;
  406. extensions[num_ext] = SCTP_CID_AUTH;
  407. num_ext += 1;
  408. }
  409. if (num_ext)
  410. chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
  411. /* Now allocate and fill out the chunk. */
  412. retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
  413. if (!retval)
  414. goto nomem_chunk;
  415. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  416. *
  417. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  418. * HEARTBEAT ACK, * etc.) to the same destination transport
  419. * address from which it received the DATA or control chunk
  420. * to which it is replying.
  421. *
  422. * [INIT ACK back to where the INIT came from.]
  423. */
  424. retval->transport = chunk->transport;
  425. retval->subh.init_hdr =
  426. sctp_addto_chunk(retval, sizeof(initack), &initack);
  427. retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
  428. sctp_addto_chunk(retval, cookie_len, cookie);
  429. if (asoc->peer.ecn_capable)
  430. sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
  431. if (num_ext) {
  432. ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
  433. ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
  434. sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
  435. sctp_addto_param(retval, num_ext, extensions);
  436. }
  437. if (asoc->peer.prsctp_capable)
  438. sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
  439. if (sp->adaptation_ind) {
  440. aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
  441. aiparam.param_hdr.length = htons(sizeof(aiparam));
  442. aiparam.adaptation_ind = htonl(sp->adaptation_ind);
  443. sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
  444. }
  445. if (asoc->peer.auth_capable) {
  446. sctp_addto_chunk(retval, ntohs(auth_random->length),
  447. auth_random);
  448. if (auth_hmacs)
  449. sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
  450. auth_hmacs);
  451. if (auth_chunks)
  452. sctp_addto_chunk(retval, ntohs(auth_chunks->length),
  453. auth_chunks);
  454. }
  455. /* We need to remove the const qualifier at this point. */
  456. retval->asoc = (struct sctp_association *) asoc;
  457. nomem_chunk:
  458. kfree(cookie);
  459. nomem_cookie:
  460. kfree(addrs.v);
  461. return retval;
  462. }
  463. /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
  464. *
  465. * This chunk is used only during the initialization of an association.
  466. * It is sent by the initiator of an association to its peer to complete
  467. * the initialization process. This chunk MUST precede any DATA chunk
  468. * sent within the association, but MAY be bundled with one or more DATA
  469. * chunks in the same packet.
  470. *
  471. * 0 1 2 3
  472. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  473. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  474. * | Type = 10 |Chunk Flags | Length |
  475. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  476. * / Cookie /
  477. * \ \
  478. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  479. *
  480. * Chunk Flags: 8 bit
  481. *
  482. * Set to zero on transmit and ignored on receipt.
  483. *
  484. * Length: 16 bits (unsigned integer)
  485. *
  486. * Set to the size of the chunk in bytes, including the 4 bytes of
  487. * the chunk header and the size of the Cookie.
  488. *
  489. * Cookie: variable size
  490. *
  491. * This field must contain the exact cookie received in the
  492. * State Cookie parameter from the previous INIT ACK.
  493. *
  494. * An implementation SHOULD make the cookie as small as possible
  495. * to insure interoperability.
  496. */
  497. struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
  498. const struct sctp_chunk *chunk)
  499. {
  500. struct sctp_chunk *retval;
  501. int cookie_len;
  502. void *cookie;
  503. cookie = asoc->peer.cookie;
  504. cookie_len = asoc->peer.cookie_len;
  505. /* Build a cookie echo chunk. */
  506. retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
  507. cookie_len, GFP_ATOMIC);
  508. if (!retval)
  509. goto nodata;
  510. retval->subh.cookie_hdr =
  511. sctp_addto_chunk(retval, cookie_len, cookie);
  512. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  513. *
  514. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  515. * HEARTBEAT ACK, * etc.) to the same destination transport
  516. * address from which it * received the DATA or control chunk
  517. * to which it is replying.
  518. *
  519. * [COOKIE ECHO back to where the INIT ACK came from.]
  520. */
  521. if (chunk)
  522. retval->transport = chunk->transport;
  523. nodata:
  524. return retval;
  525. }
  526. /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
  527. *
  528. * This chunk is used only during the initialization of an
  529. * association. It is used to acknowledge the receipt of a COOKIE
  530. * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
  531. * within the association, but MAY be bundled with one or more DATA
  532. * chunks or SACK chunk in the same SCTP packet.
  533. *
  534. * 0 1 2 3
  535. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  536. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  537. * | Type = 11 |Chunk Flags | Length = 4 |
  538. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  539. *
  540. * Chunk Flags: 8 bits
  541. *
  542. * Set to zero on transmit and ignored on receipt.
  543. */
  544. struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
  545. const struct sctp_chunk *chunk)
  546. {
  547. struct sctp_chunk *retval;
  548. retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
  549. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  550. *
  551. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  552. * HEARTBEAT ACK, * etc.) to the same destination transport
  553. * address from which it * received the DATA or control chunk
  554. * to which it is replying.
  555. *
  556. * [COOKIE ACK back to where the COOKIE ECHO came from.]
  557. */
  558. if (retval && chunk)
  559. retval->transport = chunk->transport;
  560. return retval;
  561. }
  562. /*
  563. * Appendix A: Explicit Congestion Notification:
  564. * CWR:
  565. *
  566. * RFC 2481 details a specific bit for a sender to send in the header of
  567. * its next outbound TCP segment to indicate to its peer that it has
  568. * reduced its congestion window. This is termed the CWR bit. For
  569. * SCTP the same indication is made by including the CWR chunk.
  570. * This chunk contains one data element, i.e. the TSN number that
  571. * was sent in the ECNE chunk. This element represents the lowest
  572. * TSN number in the datagram that was originally marked with the
  573. * CE bit.
  574. *
  575. * 0 1 2 3
  576. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  577. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  578. * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
  579. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  580. * | Lowest TSN Number |
  581. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  582. *
  583. * Note: The CWR is considered a Control chunk.
  584. */
  585. struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
  586. const __u32 lowest_tsn,
  587. const struct sctp_chunk *chunk)
  588. {
  589. struct sctp_chunk *retval;
  590. struct sctp_cwrhdr cwr;
  591. cwr.lowest_tsn = htonl(lowest_tsn);
  592. retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
  593. sizeof(cwr), GFP_ATOMIC);
  594. if (!retval)
  595. goto nodata;
  596. retval->subh.ecn_cwr_hdr =
  597. sctp_addto_chunk(retval, sizeof(cwr), &cwr);
  598. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  599. *
  600. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  601. * HEARTBEAT ACK, * etc.) to the same destination transport
  602. * address from which it * received the DATA or control chunk
  603. * to which it is replying.
  604. *
  605. * [Report a reduced congestion window back to where the ECNE
  606. * came from.]
  607. */
  608. if (chunk)
  609. retval->transport = chunk->transport;
  610. nodata:
  611. return retval;
  612. }
  613. /* Make an ECNE chunk. This is a congestion experienced report. */
  614. struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
  615. const __u32 lowest_tsn)
  616. {
  617. struct sctp_chunk *retval;
  618. struct sctp_ecnehdr ecne;
  619. ecne.lowest_tsn = htonl(lowest_tsn);
  620. retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
  621. sizeof(ecne), GFP_ATOMIC);
  622. if (!retval)
  623. goto nodata;
  624. retval->subh.ecne_hdr =
  625. sctp_addto_chunk(retval, sizeof(ecne), &ecne);
  626. nodata:
  627. return retval;
  628. }
  629. /* Make a DATA chunk for the given association from the provided
  630. * parameters. However, do not populate the data payload.
  631. */
  632. struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
  633. const struct sctp_sndrcvinfo *sinfo,
  634. int data_len, __u8 flags, __u16 ssn,
  635. gfp_t gfp)
  636. {
  637. struct sctp_chunk *retval;
  638. struct sctp_datahdr dp;
  639. int chunk_len;
  640. /* We assign the TSN as LATE as possible, not here when
  641. * creating the chunk.
  642. */
  643. dp.tsn = 0;
  644. dp.stream = htons(sinfo->sinfo_stream);
  645. dp.ppid = sinfo->sinfo_ppid;
  646. /* Set the flags for an unordered send. */
  647. if (sinfo->sinfo_flags & SCTP_UNORDERED) {
  648. flags |= SCTP_DATA_UNORDERED;
  649. dp.ssn = 0;
  650. } else
  651. dp.ssn = htons(ssn);
  652. chunk_len = sizeof(dp) + data_len;
  653. retval = sctp_make_data(asoc, flags, chunk_len, gfp);
  654. if (!retval)
  655. goto nodata;
  656. retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
  657. memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
  658. nodata:
  659. return retval;
  660. }
  661. /* Create a selective ackowledgement (SACK) for the given
  662. * association. This reports on which TSN's we've seen to date,
  663. * including duplicates and gaps.
  664. */
  665. struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
  666. {
  667. struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
  668. struct sctp_association *aptr = (struct sctp_association *)asoc;
  669. struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
  670. __u16 num_gabs, num_dup_tsns;
  671. struct sctp_transport *trans;
  672. struct sctp_chunk *retval;
  673. struct sctp_sackhdr sack;
  674. __u32 ctsn;
  675. int len;
  676. memset(gabs, 0, sizeof(gabs));
  677. ctsn = sctp_tsnmap_get_ctsn(map);
  678. pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
  679. /* How much room is needed in the chunk? */
  680. num_gabs = sctp_tsnmap_num_gabs(map, gabs);
  681. num_dup_tsns = sctp_tsnmap_num_dups(map);
  682. /* Initialize the SACK header. */
  683. sack.cum_tsn_ack = htonl(ctsn);
  684. sack.a_rwnd = htonl(asoc->a_rwnd);
  685. sack.num_gap_ack_blocks = htons(num_gabs);
  686. sack.num_dup_tsns = htons(num_dup_tsns);
  687. len = sizeof(sack)
  688. + sizeof(struct sctp_gap_ack_block) * num_gabs
  689. + sizeof(__u32) * num_dup_tsns;
  690. /* Create the chunk. */
  691. retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
  692. if (!retval)
  693. goto nodata;
  694. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  695. *
  696. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  697. * HEARTBEAT ACK, etc.) to the same destination transport
  698. * address from which it received the DATA or control chunk to
  699. * which it is replying. This rule should also be followed if
  700. * the endpoint is bundling DATA chunks together with the
  701. * reply chunk.
  702. *
  703. * However, when acknowledging multiple DATA chunks received
  704. * in packets from different source addresses in a single
  705. * SACK, the SACK chunk may be transmitted to one of the
  706. * destination transport addresses from which the DATA or
  707. * control chunks being acknowledged were received.
  708. *
  709. * [BUG: We do not implement the following paragraph.
  710. * Perhaps we should remember the last transport we used for a
  711. * SACK and avoid that (if possible) if we have seen any
  712. * duplicates. --piggy]
  713. *
  714. * When a receiver of a duplicate DATA chunk sends a SACK to a
  715. * multi- homed endpoint it MAY be beneficial to vary the
  716. * destination address and not use the source address of the
  717. * DATA chunk. The reason being that receiving a duplicate
  718. * from a multi-homed endpoint might indicate that the return
  719. * path (as specified in the source address of the DATA chunk)
  720. * for the SACK is broken.
  721. *
  722. * [Send to the address from which we last received a DATA chunk.]
  723. */
  724. retval->transport = asoc->peer.last_data_from;
  725. retval->subh.sack_hdr =
  726. sctp_addto_chunk(retval, sizeof(sack), &sack);
  727. /* Add the gap ack block information. */
  728. if (num_gabs)
  729. sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
  730. gabs);
  731. /* Add the duplicate TSN information. */
  732. if (num_dup_tsns) {
  733. aptr->stats.idupchunks += num_dup_tsns;
  734. sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
  735. sctp_tsnmap_get_dups(map));
  736. }
  737. /* Once we have a sack generated, check to see what our sack
  738. * generation is, if its 0, reset the transports to 0, and reset
  739. * the association generation to 1
  740. *
  741. * The idea is that zero is never used as a valid generation for the
  742. * association so no transport will match after a wrap event like this,
  743. * Until the next sack
  744. */
  745. if (++aptr->peer.sack_generation == 0) {
  746. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  747. transports)
  748. trans->sack_generation = 0;
  749. aptr->peer.sack_generation = 1;
  750. }
  751. nodata:
  752. return retval;
  753. }
  754. /* Make a SHUTDOWN chunk. */
  755. struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
  756. const struct sctp_chunk *chunk)
  757. {
  758. struct sctp_shutdownhdr shut;
  759. struct sctp_chunk *retval;
  760. __u32 ctsn;
  761. ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  762. shut.cum_tsn_ack = htonl(ctsn);
  763. retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
  764. sizeof(shut), GFP_ATOMIC);
  765. if (!retval)
  766. goto nodata;
  767. retval->subh.shutdown_hdr =
  768. sctp_addto_chunk(retval, sizeof(shut), &shut);
  769. if (chunk)
  770. retval->transport = chunk->transport;
  771. nodata:
  772. return retval;
  773. }
  774. struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
  775. const struct sctp_chunk *chunk)
  776. {
  777. struct sctp_chunk *retval;
  778. retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
  779. GFP_ATOMIC);
  780. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  781. *
  782. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  783. * HEARTBEAT ACK, * etc.) to the same destination transport
  784. * address from which it * received the DATA or control chunk
  785. * to which it is replying.
  786. *
  787. * [ACK back to where the SHUTDOWN came from.]
  788. */
  789. if (retval && chunk)
  790. retval->transport = chunk->transport;
  791. return retval;
  792. }
  793. struct sctp_chunk *sctp_make_shutdown_complete(
  794. const struct sctp_association *asoc,
  795. const struct sctp_chunk *chunk)
  796. {
  797. struct sctp_chunk *retval;
  798. __u8 flags = 0;
  799. /* Set the T-bit if we have no association (vtag will be
  800. * reflected)
  801. */
  802. flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
  803. retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
  804. 0, GFP_ATOMIC);
  805. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  806. *
  807. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  808. * HEARTBEAT ACK, * etc.) to the same destination transport
  809. * address from which it * received the DATA or control chunk
  810. * to which it is replying.
  811. *
  812. * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
  813. * came from.]
  814. */
  815. if (retval && chunk)
  816. retval->transport = chunk->transport;
  817. return retval;
  818. }
  819. /* Create an ABORT. Note that we set the T bit if we have no
  820. * association, except when responding to an INIT (sctpimpguide 2.41).
  821. */
  822. struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
  823. const struct sctp_chunk *chunk,
  824. const size_t hint)
  825. {
  826. struct sctp_chunk *retval;
  827. __u8 flags = 0;
  828. /* Set the T-bit if we have no association and 'chunk' is not
  829. * an INIT (vtag will be reflected).
  830. */
  831. if (!asoc) {
  832. if (chunk && chunk->chunk_hdr &&
  833. chunk->chunk_hdr->type == SCTP_CID_INIT)
  834. flags = 0;
  835. else
  836. flags = SCTP_CHUNK_FLAG_T;
  837. }
  838. retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
  839. GFP_ATOMIC);
  840. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  841. *
  842. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  843. * HEARTBEAT ACK, * etc.) to the same destination transport
  844. * address from which it * received the DATA or control chunk
  845. * to which it is replying.
  846. *
  847. * [ABORT back to where the offender came from.]
  848. */
  849. if (retval && chunk)
  850. retval->transport = chunk->transport;
  851. return retval;
  852. }
  853. /* Helper to create ABORT with a NO_USER_DATA error. */
  854. struct sctp_chunk *sctp_make_abort_no_data(
  855. const struct sctp_association *asoc,
  856. const struct sctp_chunk *chunk,
  857. __u32 tsn)
  858. {
  859. struct sctp_chunk *retval;
  860. __be32 payload;
  861. retval = sctp_make_abort(asoc, chunk,
  862. sizeof(struct sctp_errhdr) + sizeof(tsn));
  863. if (!retval)
  864. goto no_mem;
  865. /* Put the tsn back into network byte order. */
  866. payload = htonl(tsn);
  867. sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
  868. sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
  869. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  870. *
  871. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  872. * HEARTBEAT ACK, * etc.) to the same destination transport
  873. * address from which it * received the DATA or control chunk
  874. * to which it is replying.
  875. *
  876. * [ABORT back to where the offender came from.]
  877. */
  878. if (chunk)
  879. retval->transport = chunk->transport;
  880. no_mem:
  881. return retval;
  882. }
  883. /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
  884. struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
  885. struct msghdr *msg,
  886. size_t paylen)
  887. {
  888. struct sctp_chunk *retval;
  889. void *payload = NULL;
  890. int err;
  891. retval = sctp_make_abort(asoc, NULL,
  892. sizeof(struct sctp_errhdr) + paylen);
  893. if (!retval)
  894. goto err_chunk;
  895. if (paylen) {
  896. /* Put the msg_iov together into payload. */
  897. payload = kmalloc(paylen, GFP_KERNEL);
  898. if (!payload)
  899. goto err_payload;
  900. err = memcpy_from_msg(payload, msg, paylen);
  901. if (err < 0)
  902. goto err_copy;
  903. }
  904. sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
  905. sctp_addto_chunk(retval, paylen, payload);
  906. if (paylen)
  907. kfree(payload);
  908. return retval;
  909. err_copy:
  910. kfree(payload);
  911. err_payload:
  912. sctp_chunk_free(retval);
  913. retval = NULL;
  914. err_chunk:
  915. return retval;
  916. }
  917. /* Append bytes to the end of a parameter. Will panic if chunk is not big
  918. * enough.
  919. */
  920. static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
  921. const void *data)
  922. {
  923. int chunklen = ntohs(chunk->chunk_hdr->length);
  924. void *target;
  925. target = skb_put(chunk->skb, len);
  926. if (data)
  927. memcpy(target, data, len);
  928. else
  929. memset(target, 0, len);
  930. /* Adjust the chunk length field. */
  931. chunk->chunk_hdr->length = htons(chunklen + len);
  932. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  933. return target;
  934. }
  935. /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
  936. struct sctp_chunk *sctp_make_abort_violation(
  937. const struct sctp_association *asoc,
  938. const struct sctp_chunk *chunk,
  939. const __u8 *payload,
  940. const size_t paylen)
  941. {
  942. struct sctp_chunk *retval;
  943. struct sctp_paramhdr phdr;
  944. retval = sctp_make_abort(asoc, chunk, sizeof(struct sctp_errhdr) +
  945. paylen + sizeof(phdr));
  946. if (!retval)
  947. goto end;
  948. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen +
  949. sizeof(phdr));
  950. phdr.type = htons(chunk->chunk_hdr->type);
  951. phdr.length = chunk->chunk_hdr->length;
  952. sctp_addto_chunk(retval, paylen, payload);
  953. sctp_addto_param(retval, sizeof(phdr), &phdr);
  954. end:
  955. return retval;
  956. }
  957. struct sctp_chunk *sctp_make_violation_paramlen(
  958. const struct sctp_association *asoc,
  959. const struct sctp_chunk *chunk,
  960. struct sctp_paramhdr *param)
  961. {
  962. static const char error[] = "The following parameter had invalid length:";
  963. size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr) +
  964. sizeof(*param);
  965. struct sctp_chunk *retval;
  966. retval = sctp_make_abort(asoc, chunk, payload_len);
  967. if (!retval)
  968. goto nodata;
  969. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
  970. sizeof(error) + sizeof(*param));
  971. sctp_addto_chunk(retval, sizeof(error), error);
  972. sctp_addto_param(retval, sizeof(*param), param);
  973. nodata:
  974. return retval;
  975. }
  976. struct sctp_chunk *sctp_make_violation_max_retrans(
  977. const struct sctp_association *asoc,
  978. const struct sctp_chunk *chunk)
  979. {
  980. static const char error[] = "Association exceeded its max_retans count";
  981. size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr);
  982. struct sctp_chunk *retval;
  983. retval = sctp_make_abort(asoc, chunk, payload_len);
  984. if (!retval)
  985. goto nodata;
  986. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
  987. sctp_addto_chunk(retval, sizeof(error), error);
  988. nodata:
  989. return retval;
  990. }
  991. /* Make a HEARTBEAT chunk. */
  992. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  993. const struct sctp_transport *transport)
  994. {
  995. struct sctp_sender_hb_info hbinfo;
  996. struct sctp_chunk *retval;
  997. retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
  998. sizeof(hbinfo), GFP_ATOMIC);
  999. if (!retval)
  1000. goto nodata;
  1001. hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
  1002. hbinfo.param_hdr.length = htons(sizeof(hbinfo));
  1003. hbinfo.daddr = transport->ipaddr;
  1004. hbinfo.sent_at = jiffies;
  1005. hbinfo.hb_nonce = transport->hb_nonce;
  1006. /* Cast away the 'const', as this is just telling the chunk
  1007. * what transport it belongs to.
  1008. */
  1009. retval->transport = (struct sctp_transport *) transport;
  1010. retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
  1011. &hbinfo);
  1012. nodata:
  1013. return retval;
  1014. }
  1015. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  1016. const struct sctp_chunk *chunk,
  1017. const void *payload,
  1018. const size_t paylen)
  1019. {
  1020. struct sctp_chunk *retval;
  1021. retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
  1022. GFP_ATOMIC);
  1023. if (!retval)
  1024. goto nodata;
  1025. retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
  1026. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  1027. *
  1028. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  1029. * HEARTBEAT ACK, * etc.) to the same destination transport
  1030. * address from which it * received the DATA or control chunk
  1031. * to which it is replying.
  1032. *
  1033. * [HBACK back to where the HEARTBEAT came from.]
  1034. */
  1035. if (chunk)
  1036. retval->transport = chunk->transport;
  1037. nodata:
  1038. return retval;
  1039. }
  1040. /* Create an Operation Error chunk with the specified space reserved.
  1041. * This routine can be used for containing multiple causes in the chunk.
  1042. */
  1043. static struct sctp_chunk *sctp_make_op_error_space(
  1044. const struct sctp_association *asoc,
  1045. const struct sctp_chunk *chunk,
  1046. size_t size)
  1047. {
  1048. struct sctp_chunk *retval;
  1049. retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
  1050. sizeof(struct sctp_errhdr) + size,
  1051. GFP_ATOMIC);
  1052. if (!retval)
  1053. goto nodata;
  1054. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  1055. *
  1056. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  1057. * HEARTBEAT ACK, etc.) to the same destination transport
  1058. * address from which it received the DATA or control chunk
  1059. * to which it is replying.
  1060. *
  1061. */
  1062. if (chunk)
  1063. retval->transport = chunk->transport;
  1064. nodata:
  1065. return retval;
  1066. }
  1067. /* Create an Operation Error chunk of a fixed size,
  1068. * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
  1069. * This is a helper function to allocate an error chunk for
  1070. * for those invalid parameter codes in which we may not want
  1071. * to report all the errors, if the incoming chunk is large
  1072. */
  1073. static inline struct sctp_chunk *sctp_make_op_error_fixed(
  1074. const struct sctp_association *asoc,
  1075. const struct sctp_chunk *chunk)
  1076. {
  1077. size_t size = asoc ? asoc->pathmtu : 0;
  1078. if (!size)
  1079. size = SCTP_DEFAULT_MAXSEGMENT;
  1080. return sctp_make_op_error_space(asoc, chunk, size);
  1081. }
  1082. /* Create an Operation Error chunk. */
  1083. struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
  1084. const struct sctp_chunk *chunk,
  1085. __be16 cause_code, const void *payload,
  1086. size_t paylen, size_t reserve_tail)
  1087. {
  1088. struct sctp_chunk *retval;
  1089. retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
  1090. if (!retval)
  1091. goto nodata;
  1092. sctp_init_cause(retval, cause_code, paylen + reserve_tail);
  1093. sctp_addto_chunk(retval, paylen, payload);
  1094. if (reserve_tail)
  1095. sctp_addto_param(retval, reserve_tail, NULL);
  1096. nodata:
  1097. return retval;
  1098. }
  1099. struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
  1100. {
  1101. struct sctp_authhdr auth_hdr;
  1102. struct sctp_hmac *hmac_desc;
  1103. struct sctp_chunk *retval;
  1104. __u8 *hmac;
  1105. /* Get the first hmac that the peer told us to use */
  1106. hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  1107. if (unlikely(!hmac_desc))
  1108. return NULL;
  1109. retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
  1110. hmac_desc->hmac_len + sizeof(auth_hdr),
  1111. GFP_ATOMIC);
  1112. if (!retval)
  1113. return NULL;
  1114. auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
  1115. auth_hdr.shkey_id = htons(asoc->active_key_id);
  1116. retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(auth_hdr),
  1117. &auth_hdr);
  1118. hmac = skb_put_zero(retval->skb, hmac_desc->hmac_len);
  1119. /* Adjust the chunk header to include the empty MAC */
  1120. retval->chunk_hdr->length =
  1121. htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
  1122. retval->chunk_end = skb_tail_pointer(retval->skb);
  1123. return retval;
  1124. }
  1125. /********************************************************************
  1126. * 2nd Level Abstractions
  1127. ********************************************************************/
  1128. /* Turn an skb into a chunk.
  1129. * FIXME: Eventually move the structure directly inside the skb->cb[].
  1130. *
  1131. * sctpimpguide-05.txt Section 2.8.2
  1132. * M1) Each time a new DATA chunk is transmitted
  1133. * set the 'TSN.Missing.Report' count for that TSN to 0. The
  1134. * 'TSN.Missing.Report' count will be used to determine missing chunks
  1135. * and when to fast retransmit.
  1136. *
  1137. */
  1138. struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
  1139. const struct sctp_association *asoc,
  1140. struct sock *sk, gfp_t gfp)
  1141. {
  1142. struct sctp_chunk *retval;
  1143. retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
  1144. if (!retval)
  1145. goto nodata;
  1146. if (!sk)
  1147. pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
  1148. INIT_LIST_HEAD(&retval->list);
  1149. retval->skb = skb;
  1150. retval->asoc = (struct sctp_association *)asoc;
  1151. retval->singleton = 1;
  1152. retval->fast_retransmit = SCTP_CAN_FRTX;
  1153. /* Polish the bead hole. */
  1154. INIT_LIST_HEAD(&retval->transmitted_list);
  1155. INIT_LIST_HEAD(&retval->frag_list);
  1156. SCTP_DBG_OBJCNT_INC(chunk);
  1157. refcount_set(&retval->refcnt, 1);
  1158. nodata:
  1159. return retval;
  1160. }
  1161. /* Set chunk->source and dest based on the IP header in chunk->skb. */
  1162. void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
  1163. union sctp_addr *dest)
  1164. {
  1165. memcpy(&chunk->source, src, sizeof(union sctp_addr));
  1166. memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
  1167. }
  1168. /* Extract the source address from a chunk. */
  1169. const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
  1170. {
  1171. /* If we have a known transport, use that. */
  1172. if (chunk->transport) {
  1173. return &chunk->transport->ipaddr;
  1174. } else {
  1175. /* Otherwise, extract it from the IP header. */
  1176. return &chunk->source;
  1177. }
  1178. }
  1179. /* Create a new chunk, setting the type and flags headers from the
  1180. * arguments, reserving enough space for a 'paylen' byte payload.
  1181. */
  1182. static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
  1183. __u8 type, __u8 flags, int paylen,
  1184. gfp_t gfp)
  1185. {
  1186. struct sctp_chunkhdr *chunk_hdr;
  1187. struct sctp_chunk *retval;
  1188. struct sk_buff *skb;
  1189. struct sock *sk;
  1190. /* No need to allocate LL here, as this is only a chunk. */
  1191. skb = alloc_skb(SCTP_PAD4(sizeof(*chunk_hdr) + paylen), gfp);
  1192. if (!skb)
  1193. goto nodata;
  1194. /* Make room for the chunk header. */
  1195. chunk_hdr = (struct sctp_chunkhdr *)skb_put(skb, sizeof(*chunk_hdr));
  1196. chunk_hdr->type = type;
  1197. chunk_hdr->flags = flags;
  1198. chunk_hdr->length = htons(sizeof(*chunk_hdr));
  1199. sk = asoc ? asoc->base.sk : NULL;
  1200. retval = sctp_chunkify(skb, asoc, sk, gfp);
  1201. if (!retval) {
  1202. kfree_skb(skb);
  1203. goto nodata;
  1204. }
  1205. retval->chunk_hdr = chunk_hdr;
  1206. retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(*chunk_hdr);
  1207. /* Determine if the chunk needs to be authenticated */
  1208. if (sctp_auth_send_cid(type, asoc))
  1209. retval->auth = 1;
  1210. return retval;
  1211. nodata:
  1212. return NULL;
  1213. }
  1214. static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
  1215. __u8 flags, int paylen, gfp_t gfp)
  1216. {
  1217. return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
  1218. }
  1219. static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
  1220. __u8 type, __u8 flags, int paylen,
  1221. gfp_t gfp)
  1222. {
  1223. struct sctp_chunk *chunk;
  1224. chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
  1225. if (chunk)
  1226. sctp_control_set_owner_w(chunk);
  1227. return chunk;
  1228. }
  1229. /* Release the memory occupied by a chunk. */
  1230. static void sctp_chunk_destroy(struct sctp_chunk *chunk)
  1231. {
  1232. BUG_ON(!list_empty(&chunk->list));
  1233. list_del_init(&chunk->transmitted_list);
  1234. consume_skb(chunk->skb);
  1235. consume_skb(chunk->auth_chunk);
  1236. SCTP_DBG_OBJCNT_DEC(chunk);
  1237. kmem_cache_free(sctp_chunk_cachep, chunk);
  1238. }
  1239. /* Possibly, free the chunk. */
  1240. void sctp_chunk_free(struct sctp_chunk *chunk)
  1241. {
  1242. /* Release our reference on the message tracker. */
  1243. if (chunk->msg)
  1244. sctp_datamsg_put(chunk->msg);
  1245. sctp_chunk_put(chunk);
  1246. }
  1247. /* Grab a reference to the chunk. */
  1248. void sctp_chunk_hold(struct sctp_chunk *ch)
  1249. {
  1250. refcount_inc(&ch->refcnt);
  1251. }
  1252. /* Release a reference to the chunk. */
  1253. void sctp_chunk_put(struct sctp_chunk *ch)
  1254. {
  1255. if (refcount_dec_and_test(&ch->refcnt))
  1256. sctp_chunk_destroy(ch);
  1257. }
  1258. /* Append bytes to the end of a chunk. Will panic if chunk is not big
  1259. * enough.
  1260. */
  1261. void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
  1262. {
  1263. int chunklen = ntohs(chunk->chunk_hdr->length);
  1264. int padlen = SCTP_PAD4(chunklen) - chunklen;
  1265. void *target;
  1266. skb_put_zero(chunk->skb, padlen);
  1267. target = skb_put_data(chunk->skb, data, len);
  1268. /* Adjust the chunk length field. */
  1269. chunk->chunk_hdr->length = htons(chunklen + padlen + len);
  1270. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  1271. return target;
  1272. }
  1273. /* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
  1274. * space in the chunk
  1275. */
  1276. static void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
  1277. int len, const void *data)
  1278. {
  1279. if (skb_tailroom(chunk->skb) >= len)
  1280. return sctp_addto_chunk(chunk, len, data);
  1281. else
  1282. return NULL;
  1283. }
  1284. /* Append bytes from user space to the end of a chunk. Will panic if
  1285. * chunk is not big enough.
  1286. * Returns a kernel err value.
  1287. */
  1288. int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
  1289. struct iov_iter *from)
  1290. {
  1291. void *target;
  1292. /* Make room in chunk for data. */
  1293. target = skb_put(chunk->skb, len);
  1294. /* Copy data (whole iovec) into chunk */
  1295. if (!copy_from_iter_full(target, len, from))
  1296. return -EFAULT;
  1297. /* Adjust the chunk length field. */
  1298. chunk->chunk_hdr->length =
  1299. htons(ntohs(chunk->chunk_hdr->length) + len);
  1300. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  1301. return 0;
  1302. }
  1303. /* Helper function to assign a TSN if needed. This assumes that both
  1304. * the data_hdr and association have already been assigned.
  1305. */
  1306. void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
  1307. {
  1308. struct sctp_stream *stream;
  1309. struct sctp_chunk *lchunk;
  1310. struct sctp_datamsg *msg;
  1311. __u16 ssn, sid;
  1312. if (chunk->has_ssn)
  1313. return;
  1314. /* All fragments will be on the same stream */
  1315. sid = ntohs(chunk->subh.data_hdr->stream);
  1316. stream = &chunk->asoc->stream;
  1317. /* Now assign the sequence number to the entire message.
  1318. * All fragments must have the same stream sequence number.
  1319. */
  1320. msg = chunk->msg;
  1321. list_for_each_entry(lchunk, &msg->chunks, frag_list) {
  1322. if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  1323. ssn = 0;
  1324. } else {
  1325. if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
  1326. ssn = sctp_ssn_next(stream, out, sid);
  1327. else
  1328. ssn = sctp_ssn_peek(stream, out, sid);
  1329. }
  1330. lchunk->subh.data_hdr->ssn = htons(ssn);
  1331. lchunk->has_ssn = 1;
  1332. }
  1333. }
  1334. /* Helper function to assign a TSN if needed. This assumes that both
  1335. * the data_hdr and association have already been assigned.
  1336. */
  1337. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
  1338. {
  1339. if (!chunk->has_tsn) {
  1340. /* This is the last possible instant to
  1341. * assign a TSN.
  1342. */
  1343. chunk->subh.data_hdr->tsn =
  1344. htonl(sctp_association_get_next_tsn(chunk->asoc));
  1345. chunk->has_tsn = 1;
  1346. }
  1347. }
  1348. /* Create a CLOSED association to use with an incoming packet. */
  1349. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
  1350. struct sctp_chunk *chunk,
  1351. gfp_t gfp)
  1352. {
  1353. struct sctp_association *asoc;
  1354. enum sctp_scope scope;
  1355. struct sk_buff *skb;
  1356. /* Create the bare association. */
  1357. scope = sctp_scope(sctp_source(chunk));
  1358. asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1359. if (!asoc)
  1360. goto nodata;
  1361. asoc->temp = 1;
  1362. skb = chunk->skb;
  1363. /* Create an entry for the source address of the packet. */
  1364. SCTP_INPUT_CB(skb)->af->from_skb(&asoc->c.peer_addr, skb, 1);
  1365. nodata:
  1366. return asoc;
  1367. }
  1368. /* Build a cookie representing asoc.
  1369. * This INCLUDES the param header needed to put the cookie in the INIT ACK.
  1370. */
  1371. static struct sctp_cookie_param *sctp_pack_cookie(
  1372. const struct sctp_endpoint *ep,
  1373. const struct sctp_association *asoc,
  1374. const struct sctp_chunk *init_chunk,
  1375. int *cookie_len, const __u8 *raw_addrs,
  1376. int addrs_len)
  1377. {
  1378. struct sctp_signed_cookie *cookie;
  1379. struct sctp_cookie_param *retval;
  1380. int headersize, bodysize;
  1381. /* Header size is static data prior to the actual cookie, including
  1382. * any padding.
  1383. */
  1384. headersize = sizeof(struct sctp_paramhdr) +
  1385. (sizeof(struct sctp_signed_cookie) -
  1386. sizeof(struct sctp_cookie));
  1387. bodysize = sizeof(struct sctp_cookie)
  1388. + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
  1389. /* Pad out the cookie to a multiple to make the signature
  1390. * functions simpler to write.
  1391. */
  1392. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1393. bodysize += SCTP_COOKIE_MULTIPLE
  1394. - (bodysize % SCTP_COOKIE_MULTIPLE);
  1395. *cookie_len = headersize + bodysize;
  1396. /* Clear this memory since we are sending this data structure
  1397. * out on the network.
  1398. */
  1399. retval = kzalloc(*cookie_len, GFP_ATOMIC);
  1400. if (!retval)
  1401. goto nodata;
  1402. cookie = (struct sctp_signed_cookie *) retval->body;
  1403. /* Set up the parameter header. */
  1404. retval->p.type = SCTP_PARAM_STATE_COOKIE;
  1405. retval->p.length = htons(*cookie_len);
  1406. /* Copy the cookie part of the association itself. */
  1407. cookie->c = asoc->c;
  1408. /* Save the raw address list length in the cookie. */
  1409. cookie->c.raw_addr_list_len = addrs_len;
  1410. /* Remember PR-SCTP capability. */
  1411. cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
  1412. /* Save adaptation indication in the cookie. */
  1413. cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
  1414. /* Set an expiration time for the cookie. */
  1415. cookie->c.expiration = ktime_add(asoc->cookie_life,
  1416. ktime_get_real());
  1417. /* Copy the peer's init packet. */
  1418. memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
  1419. ntohs(init_chunk->chunk_hdr->length));
  1420. /* Copy the raw local address list of the association. */
  1421. memcpy((__u8 *)&cookie->c.peer_init[0] +
  1422. ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
  1423. if (sctp_sk(ep->base.sk)->hmac) {
  1424. SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
  1425. int err;
  1426. /* Sign the message. */
  1427. desc->tfm = sctp_sk(ep->base.sk)->hmac;
  1428. desc->flags = 0;
  1429. err = crypto_shash_setkey(desc->tfm, ep->secret_key,
  1430. sizeof(ep->secret_key)) ?:
  1431. crypto_shash_digest(desc, (u8 *)&cookie->c, bodysize,
  1432. cookie->signature);
  1433. shash_desc_zero(desc);
  1434. if (err)
  1435. goto free_cookie;
  1436. }
  1437. return retval;
  1438. free_cookie:
  1439. kfree(retval);
  1440. nodata:
  1441. *cookie_len = 0;
  1442. return NULL;
  1443. }
  1444. /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
  1445. struct sctp_association *sctp_unpack_cookie(
  1446. const struct sctp_endpoint *ep,
  1447. const struct sctp_association *asoc,
  1448. struct sctp_chunk *chunk, gfp_t gfp,
  1449. int *error, struct sctp_chunk **errp)
  1450. {
  1451. struct sctp_association *retval = NULL;
  1452. int headersize, bodysize, fixed_size;
  1453. struct sctp_signed_cookie *cookie;
  1454. struct sk_buff *skb = chunk->skb;
  1455. struct sctp_cookie *bear_cookie;
  1456. __u8 *digest = ep->digest;
  1457. enum sctp_scope scope;
  1458. unsigned int len;
  1459. ktime_t kt;
  1460. /* Header size is static data prior to the actual cookie, including
  1461. * any padding.
  1462. */
  1463. headersize = sizeof(struct sctp_chunkhdr) +
  1464. (sizeof(struct sctp_signed_cookie) -
  1465. sizeof(struct sctp_cookie));
  1466. bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
  1467. fixed_size = headersize + sizeof(struct sctp_cookie);
  1468. /* Verify that the chunk looks like it even has a cookie.
  1469. * There must be enough room for our cookie and our peer's
  1470. * INIT chunk.
  1471. */
  1472. len = ntohs(chunk->chunk_hdr->length);
  1473. if (len < fixed_size + sizeof(struct sctp_chunkhdr))
  1474. goto malformed;
  1475. /* Verify that the cookie has been padded out. */
  1476. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1477. goto malformed;
  1478. /* Process the cookie. */
  1479. cookie = chunk->subh.cookie_hdr;
  1480. bear_cookie = &cookie->c;
  1481. if (!sctp_sk(ep->base.sk)->hmac)
  1482. goto no_hmac;
  1483. /* Check the signature. */
  1484. {
  1485. SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
  1486. int err;
  1487. desc->tfm = sctp_sk(ep->base.sk)->hmac;
  1488. desc->flags = 0;
  1489. err = crypto_shash_setkey(desc->tfm, ep->secret_key,
  1490. sizeof(ep->secret_key)) ?:
  1491. crypto_shash_digest(desc, (u8 *)bear_cookie, bodysize,
  1492. digest);
  1493. shash_desc_zero(desc);
  1494. if (err) {
  1495. *error = -SCTP_IERROR_NOMEM;
  1496. goto fail;
  1497. }
  1498. }
  1499. if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
  1500. *error = -SCTP_IERROR_BAD_SIG;
  1501. goto fail;
  1502. }
  1503. no_hmac:
  1504. /* IG Section 2.35.2:
  1505. * 3) Compare the port numbers and the verification tag contained
  1506. * within the COOKIE ECHO chunk to the actual port numbers and the
  1507. * verification tag within the SCTP common header of the received
  1508. * packet. If these values do not match the packet MUST be silently
  1509. * discarded,
  1510. */
  1511. if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
  1512. *error = -SCTP_IERROR_BAD_TAG;
  1513. goto fail;
  1514. }
  1515. if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
  1516. ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
  1517. *error = -SCTP_IERROR_BAD_PORTS;
  1518. goto fail;
  1519. }
  1520. /* Check to see if the cookie is stale. If there is already
  1521. * an association, there is no need to check cookie's expiration
  1522. * for init collision case of lost COOKIE ACK.
  1523. * If skb has been timestamped, then use the stamp, otherwise
  1524. * use current time. This introduces a small possibility that
  1525. * that a cookie may be considered expired, but his would only slow
  1526. * down the new association establishment instead of every packet.
  1527. */
  1528. if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
  1529. kt = skb_get_ktime(skb);
  1530. else
  1531. kt = ktime_get_real();
  1532. if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
  1533. /*
  1534. * Section 3.3.10.3 Stale Cookie Error (3)
  1535. *
  1536. * Cause of error
  1537. * ---------------
  1538. * Stale Cookie Error: Indicates the receipt of a valid State
  1539. * Cookie that has expired.
  1540. */
  1541. len = ntohs(chunk->chunk_hdr->length);
  1542. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1543. if (*errp) {
  1544. suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
  1545. __be32 n = htonl(usecs);
  1546. sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
  1547. sizeof(n));
  1548. sctp_addto_chunk(*errp, sizeof(n), &n);
  1549. *error = -SCTP_IERROR_STALE_COOKIE;
  1550. } else
  1551. *error = -SCTP_IERROR_NOMEM;
  1552. goto fail;
  1553. }
  1554. /* Make a new base association. */
  1555. scope = sctp_scope(sctp_source(chunk));
  1556. retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1557. if (!retval) {
  1558. *error = -SCTP_IERROR_NOMEM;
  1559. goto fail;
  1560. }
  1561. /* Set up our peer's port number. */
  1562. retval->peer.port = ntohs(chunk->sctp_hdr->source);
  1563. /* Populate the association from the cookie. */
  1564. memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
  1565. if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
  1566. GFP_ATOMIC) < 0) {
  1567. *error = -SCTP_IERROR_NOMEM;
  1568. goto fail;
  1569. }
  1570. /* Also, add the destination address. */
  1571. if (list_empty(&retval->base.bind_addr.address_list)) {
  1572. sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
  1573. sizeof(chunk->dest), SCTP_ADDR_SRC,
  1574. GFP_ATOMIC);
  1575. }
  1576. retval->next_tsn = retval->c.initial_tsn;
  1577. retval->ctsn_ack_point = retval->next_tsn - 1;
  1578. retval->addip_serial = retval->c.initial_tsn;
  1579. retval->strreset_outseq = retval->c.initial_tsn;
  1580. retval->adv_peer_ack_point = retval->ctsn_ack_point;
  1581. retval->peer.prsctp_capable = retval->c.prsctp_capable;
  1582. retval->peer.adaptation_ind = retval->c.adaptation_ind;
  1583. /* The INIT stuff will be done by the side effects. */
  1584. return retval;
  1585. fail:
  1586. if (retval)
  1587. sctp_association_free(retval);
  1588. return NULL;
  1589. malformed:
  1590. /* Yikes! The packet is either corrupt or deliberately
  1591. * malformed.
  1592. */
  1593. *error = -SCTP_IERROR_MALFORMED;
  1594. goto fail;
  1595. }
  1596. /********************************************************************
  1597. * 3rd Level Abstractions
  1598. ********************************************************************/
  1599. struct __sctp_missing {
  1600. __be32 num_missing;
  1601. __be16 type;
  1602. } __packed;
  1603. /*
  1604. * Report a missing mandatory parameter.
  1605. */
  1606. static int sctp_process_missing_param(const struct sctp_association *asoc,
  1607. enum sctp_param paramtype,
  1608. struct sctp_chunk *chunk,
  1609. struct sctp_chunk **errp)
  1610. {
  1611. struct __sctp_missing report;
  1612. __u16 len;
  1613. len = SCTP_PAD4(sizeof(report));
  1614. /* Make an ERROR chunk, preparing enough room for
  1615. * returning multiple unknown parameters.
  1616. */
  1617. if (!*errp)
  1618. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1619. if (*errp) {
  1620. report.num_missing = htonl(1);
  1621. report.type = paramtype;
  1622. sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
  1623. sizeof(report));
  1624. sctp_addto_chunk(*errp, sizeof(report), &report);
  1625. }
  1626. /* Stop processing this chunk. */
  1627. return 0;
  1628. }
  1629. /* Report an Invalid Mandatory Parameter. */
  1630. static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
  1631. struct sctp_chunk *chunk,
  1632. struct sctp_chunk **errp)
  1633. {
  1634. /* Invalid Mandatory Parameter Error has no payload. */
  1635. if (!*errp)
  1636. *errp = sctp_make_op_error_space(asoc, chunk, 0);
  1637. if (*errp)
  1638. sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
  1639. /* Stop processing this chunk. */
  1640. return 0;
  1641. }
  1642. static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
  1643. struct sctp_paramhdr *param,
  1644. const struct sctp_chunk *chunk,
  1645. struct sctp_chunk **errp)
  1646. {
  1647. /* This is a fatal error. Any accumulated non-fatal errors are
  1648. * not reported.
  1649. */
  1650. if (*errp)
  1651. sctp_chunk_free(*errp);
  1652. /* Create an error chunk and fill it in with our payload. */
  1653. *errp = sctp_make_violation_paramlen(asoc, chunk, param);
  1654. return 0;
  1655. }
  1656. /* Do not attempt to handle the HOST_NAME parm. However, do
  1657. * send back an indicator to the peer.
  1658. */
  1659. static int sctp_process_hn_param(const struct sctp_association *asoc,
  1660. union sctp_params param,
  1661. struct sctp_chunk *chunk,
  1662. struct sctp_chunk **errp)
  1663. {
  1664. __u16 len = ntohs(param.p->length);
  1665. /* Processing of the HOST_NAME parameter will generate an
  1666. * ABORT. If we've accumulated any non-fatal errors, they
  1667. * would be unrecognized parameters and we should not include
  1668. * them in the ABORT.
  1669. */
  1670. if (*errp)
  1671. sctp_chunk_free(*errp);
  1672. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1673. if (*errp) {
  1674. sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
  1675. sctp_addto_chunk(*errp, len, param.v);
  1676. }
  1677. /* Stop processing this chunk. */
  1678. return 0;
  1679. }
  1680. static int sctp_verify_ext_param(struct net *net, union sctp_params param)
  1681. {
  1682. __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  1683. int have_asconf = 0;
  1684. int have_auth = 0;
  1685. int i;
  1686. for (i = 0; i < num_ext; i++) {
  1687. switch (param.ext->chunks[i]) {
  1688. case SCTP_CID_AUTH:
  1689. have_auth = 1;
  1690. break;
  1691. case SCTP_CID_ASCONF:
  1692. case SCTP_CID_ASCONF_ACK:
  1693. have_asconf = 1;
  1694. break;
  1695. }
  1696. }
  1697. /* ADD-IP Security: The draft requires us to ABORT or ignore the
  1698. * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
  1699. * only if ADD-IP is turned on and we are not backward-compatible
  1700. * mode.
  1701. */
  1702. if (net->sctp.addip_noauth)
  1703. return 1;
  1704. if (net->sctp.addip_enable && !have_auth && have_asconf)
  1705. return 0;
  1706. return 1;
  1707. }
  1708. static void sctp_process_ext_param(struct sctp_association *asoc,
  1709. union sctp_params param)
  1710. {
  1711. __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  1712. struct net *net = sock_net(asoc->base.sk);
  1713. int i;
  1714. for (i = 0; i < num_ext; i++) {
  1715. switch (param.ext->chunks[i]) {
  1716. case SCTP_CID_RECONF:
  1717. if (asoc->reconf_enable &&
  1718. !asoc->peer.reconf_capable)
  1719. asoc->peer.reconf_capable = 1;
  1720. break;
  1721. case SCTP_CID_FWD_TSN:
  1722. if (asoc->prsctp_enable && !asoc->peer.prsctp_capable)
  1723. asoc->peer.prsctp_capable = 1;
  1724. break;
  1725. case SCTP_CID_AUTH:
  1726. /* if the peer reports AUTH, assume that he
  1727. * supports AUTH.
  1728. */
  1729. if (asoc->ep->auth_enable)
  1730. asoc->peer.auth_capable = 1;
  1731. break;
  1732. case SCTP_CID_ASCONF:
  1733. case SCTP_CID_ASCONF_ACK:
  1734. if (net->sctp.addip_enable)
  1735. asoc->peer.asconf_capable = 1;
  1736. break;
  1737. default:
  1738. break;
  1739. }
  1740. }
  1741. }
  1742. /* RFC 3.2.1 & the Implementers Guide 2.2.
  1743. *
  1744. * The Parameter Types are encoded such that the
  1745. * highest-order two bits specify the action that must be
  1746. * taken if the processing endpoint does not recognize the
  1747. * Parameter Type.
  1748. *
  1749. * 00 - Stop processing this parameter; do not process any further
  1750. * parameters within this chunk
  1751. *
  1752. * 01 - Stop processing this parameter, do not process any further
  1753. * parameters within this chunk, and report the unrecognized
  1754. * parameter in an 'Unrecognized Parameter' ERROR chunk.
  1755. *
  1756. * 10 - Skip this parameter and continue processing.
  1757. *
  1758. * 11 - Skip this parameter and continue processing but
  1759. * report the unrecognized parameter in an
  1760. * 'Unrecognized Parameter' ERROR chunk.
  1761. *
  1762. * Return value:
  1763. * SCTP_IERROR_NO_ERROR - continue with the chunk
  1764. * SCTP_IERROR_ERROR - stop and report an error.
  1765. * SCTP_IERROR_NOMEME - out of memory.
  1766. */
  1767. static enum sctp_ierror sctp_process_unk_param(
  1768. const struct sctp_association *asoc,
  1769. union sctp_params param,
  1770. struct sctp_chunk *chunk,
  1771. struct sctp_chunk **errp)
  1772. {
  1773. int retval = SCTP_IERROR_NO_ERROR;
  1774. switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
  1775. case SCTP_PARAM_ACTION_DISCARD:
  1776. retval = SCTP_IERROR_ERROR;
  1777. break;
  1778. case SCTP_PARAM_ACTION_SKIP:
  1779. break;
  1780. case SCTP_PARAM_ACTION_DISCARD_ERR:
  1781. retval = SCTP_IERROR_ERROR;
  1782. /* Fall through */
  1783. case SCTP_PARAM_ACTION_SKIP_ERR:
  1784. /* Make an ERROR chunk, preparing enough room for
  1785. * returning multiple unknown parameters.
  1786. */
  1787. if (NULL == *errp)
  1788. *errp = sctp_make_op_error_fixed(asoc, chunk);
  1789. if (*errp) {
  1790. if (!sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
  1791. SCTP_PAD4(ntohs(param.p->length))))
  1792. sctp_addto_chunk_fixed(*errp,
  1793. SCTP_PAD4(ntohs(param.p->length)),
  1794. param.v);
  1795. } else {
  1796. /* If there is no memory for generating the ERROR
  1797. * report as specified, an ABORT will be triggered
  1798. * to the peer and the association won't be
  1799. * established.
  1800. */
  1801. retval = SCTP_IERROR_NOMEM;
  1802. }
  1803. break;
  1804. default:
  1805. break;
  1806. }
  1807. return retval;
  1808. }
  1809. /* Verify variable length parameters
  1810. * Return values:
  1811. * SCTP_IERROR_ABORT - trigger an ABORT
  1812. * SCTP_IERROR_NOMEM - out of memory (abort)
  1813. * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
  1814. * SCTP_IERROR_NO_ERROR - continue with the chunk
  1815. */
  1816. static enum sctp_ierror sctp_verify_param(struct net *net,
  1817. const struct sctp_endpoint *ep,
  1818. const struct sctp_association *asoc,
  1819. union sctp_params param,
  1820. enum sctp_cid cid,
  1821. struct sctp_chunk *chunk,
  1822. struct sctp_chunk **err_chunk)
  1823. {
  1824. struct sctp_hmac_algo_param *hmacs;
  1825. int retval = SCTP_IERROR_NO_ERROR;
  1826. __u16 n_elt, id = 0;
  1827. int i;
  1828. /* FIXME - This routine is not looking at each parameter per the
  1829. * chunk type, i.e., unrecognized parameters should be further
  1830. * identified based on the chunk id.
  1831. */
  1832. switch (param.p->type) {
  1833. case SCTP_PARAM_IPV4_ADDRESS:
  1834. case SCTP_PARAM_IPV6_ADDRESS:
  1835. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  1836. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  1837. case SCTP_PARAM_STATE_COOKIE:
  1838. case SCTP_PARAM_HEARTBEAT_INFO:
  1839. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  1840. case SCTP_PARAM_ECN_CAPABLE:
  1841. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  1842. break;
  1843. case SCTP_PARAM_SUPPORTED_EXT:
  1844. if (!sctp_verify_ext_param(net, param))
  1845. return SCTP_IERROR_ABORT;
  1846. break;
  1847. case SCTP_PARAM_SET_PRIMARY:
  1848. if (net->sctp.addip_enable)
  1849. break;
  1850. goto fallthrough;
  1851. case SCTP_PARAM_HOST_NAME_ADDRESS:
  1852. /* Tell the peer, we won't support this param. */
  1853. sctp_process_hn_param(asoc, param, chunk, err_chunk);
  1854. retval = SCTP_IERROR_ABORT;
  1855. break;
  1856. case SCTP_PARAM_FWD_TSN_SUPPORT:
  1857. if (ep->prsctp_enable)
  1858. break;
  1859. goto fallthrough;
  1860. case SCTP_PARAM_RANDOM:
  1861. if (!ep->auth_enable)
  1862. goto fallthrough;
  1863. /* SCTP-AUTH: Secion 6.1
  1864. * If the random number is not 32 byte long the association
  1865. * MUST be aborted. The ABORT chunk SHOULD contain the error
  1866. * cause 'Protocol Violation'.
  1867. */
  1868. if (SCTP_AUTH_RANDOM_LENGTH !=
  1869. ntohs(param.p->length) - sizeof(struct sctp_paramhdr)) {
  1870. sctp_process_inv_paramlength(asoc, param.p,
  1871. chunk, err_chunk);
  1872. retval = SCTP_IERROR_ABORT;
  1873. }
  1874. break;
  1875. case SCTP_PARAM_CHUNKS:
  1876. if (!ep->auth_enable)
  1877. goto fallthrough;
  1878. /* SCTP-AUTH: Section 3.2
  1879. * The CHUNKS parameter MUST be included once in the INIT or
  1880. * INIT-ACK chunk if the sender wants to receive authenticated
  1881. * chunks. Its maximum length is 260 bytes.
  1882. */
  1883. if (260 < ntohs(param.p->length)) {
  1884. sctp_process_inv_paramlength(asoc, param.p,
  1885. chunk, err_chunk);
  1886. retval = SCTP_IERROR_ABORT;
  1887. }
  1888. break;
  1889. case SCTP_PARAM_HMAC_ALGO:
  1890. if (!ep->auth_enable)
  1891. goto fallthrough;
  1892. hmacs = (struct sctp_hmac_algo_param *)param.p;
  1893. n_elt = (ntohs(param.p->length) -
  1894. sizeof(struct sctp_paramhdr)) >> 1;
  1895. /* SCTP-AUTH: Section 6.1
  1896. * The HMAC algorithm based on SHA-1 MUST be supported and
  1897. * included in the HMAC-ALGO parameter.
  1898. */
  1899. for (i = 0; i < n_elt; i++) {
  1900. id = ntohs(hmacs->hmac_ids[i]);
  1901. if (id == SCTP_AUTH_HMAC_ID_SHA1)
  1902. break;
  1903. }
  1904. if (id != SCTP_AUTH_HMAC_ID_SHA1) {
  1905. sctp_process_inv_paramlength(asoc, param.p, chunk,
  1906. err_chunk);
  1907. retval = SCTP_IERROR_ABORT;
  1908. }
  1909. break;
  1910. fallthrough:
  1911. default:
  1912. pr_debug("%s: unrecognized param:%d for chunk:%d\n",
  1913. __func__, ntohs(param.p->type), cid);
  1914. retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
  1915. break;
  1916. }
  1917. return retval;
  1918. }
  1919. /* Verify the INIT packet before we process it. */
  1920. int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
  1921. const struct sctp_association *asoc, enum sctp_cid cid,
  1922. struct sctp_init_chunk *peer_init,
  1923. struct sctp_chunk *chunk, struct sctp_chunk **errp)
  1924. {
  1925. union sctp_params param;
  1926. bool has_cookie = false;
  1927. int result;
  1928. /* Check for missing mandatory parameters. Note: Initial TSN is
  1929. * also mandatory, but is not checked here since the valid range
  1930. * is 0..2**32-1. RFC4960, section 3.3.3.
  1931. */
  1932. if (peer_init->init_hdr.num_outbound_streams == 0 ||
  1933. peer_init->init_hdr.num_inbound_streams == 0 ||
  1934. peer_init->init_hdr.init_tag == 0 ||
  1935. ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
  1936. return sctp_process_inv_mandatory(asoc, chunk, errp);
  1937. sctp_walk_params(param, peer_init, init_hdr.params) {
  1938. if (param.p->type == SCTP_PARAM_STATE_COOKIE)
  1939. has_cookie = true;
  1940. }
  1941. /* There is a possibility that a parameter length was bad and
  1942. * in that case we would have stoped walking the parameters.
  1943. * The current param.p would point at the bad one.
  1944. * Current consensus on the mailing list is to generate a PROTOCOL
  1945. * VIOLATION error. We build the ERROR chunk here and let the normal
  1946. * error handling code build and send the packet.
  1947. */
  1948. if (param.v != (void *)chunk->chunk_end)
  1949. return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
  1950. /* The only missing mandatory param possible today is
  1951. * the state cookie for an INIT-ACK chunk.
  1952. */
  1953. if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
  1954. return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
  1955. chunk, errp);
  1956. /* Verify all the variable length parameters */
  1957. sctp_walk_params(param, peer_init, init_hdr.params) {
  1958. result = sctp_verify_param(net, ep, asoc, param, cid,
  1959. chunk, errp);
  1960. switch (result) {
  1961. case SCTP_IERROR_ABORT:
  1962. case SCTP_IERROR_NOMEM:
  1963. return 0;
  1964. case SCTP_IERROR_ERROR:
  1965. return 1;
  1966. case SCTP_IERROR_NO_ERROR:
  1967. default:
  1968. break;
  1969. }
  1970. } /* for (loop through all parameters) */
  1971. return 1;
  1972. }
  1973. /* Unpack the parameters in an INIT packet into an association.
  1974. * Returns 0 on failure, else success.
  1975. * FIXME: This is an association method.
  1976. */
  1977. int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
  1978. const union sctp_addr *peer_addr,
  1979. struct sctp_init_chunk *peer_init, gfp_t gfp)
  1980. {
  1981. struct net *net = sock_net(asoc->base.sk);
  1982. struct sctp_transport *transport;
  1983. struct list_head *pos, *temp;
  1984. union sctp_params param;
  1985. union sctp_addr addr;
  1986. struct sctp_af *af;
  1987. int src_match = 0;
  1988. char *cookie;
  1989. /* We must include the address that the INIT packet came from.
  1990. * This is the only address that matters for an INIT packet.
  1991. * When processing a COOKIE ECHO, we retrieve the from address
  1992. * of the INIT from the cookie.
  1993. */
  1994. /* This implementation defaults to making the first transport
  1995. * added as the primary transport. The source address seems to
  1996. * be a a better choice than any of the embedded addresses.
  1997. */
  1998. if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
  1999. goto nomem;
  2000. if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
  2001. src_match = 1;
  2002. /* Process the initialization parameters. */
  2003. sctp_walk_params(param, peer_init, init_hdr.params) {
  2004. if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
  2005. param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
  2006. af = sctp_get_af_specific(param_type2af(param.p->type));
  2007. af->from_addr_param(&addr, param.addr,
  2008. chunk->sctp_hdr->source, 0);
  2009. if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
  2010. src_match = 1;
  2011. }
  2012. if (!sctp_process_param(asoc, param, peer_addr, gfp))
  2013. goto clean_up;
  2014. }
  2015. /* source address of chunk may not match any valid address */
  2016. if (!src_match)
  2017. goto clean_up;
  2018. /* AUTH: After processing the parameters, make sure that we
  2019. * have all the required info to potentially do authentications.
  2020. */
  2021. if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
  2022. !asoc->peer.peer_hmacs))
  2023. asoc->peer.auth_capable = 0;
  2024. /* In a non-backward compatible mode, if the peer claims
  2025. * support for ADD-IP but not AUTH, the ADD-IP spec states
  2026. * that we MUST ABORT the association. Section 6. The section
  2027. * also give us an option to silently ignore the packet, which
  2028. * is what we'll do here.
  2029. */
  2030. if (!net->sctp.addip_noauth &&
  2031. (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
  2032. asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
  2033. SCTP_PARAM_DEL_IP |
  2034. SCTP_PARAM_SET_PRIMARY);
  2035. asoc->peer.asconf_capable = 0;
  2036. goto clean_up;
  2037. }
  2038. /* Walk list of transports, removing transports in the UNKNOWN state. */
  2039. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  2040. transport = list_entry(pos, struct sctp_transport, transports);
  2041. if (transport->state == SCTP_UNKNOWN) {
  2042. sctp_assoc_rm_peer(asoc, transport);
  2043. }
  2044. }
  2045. /* The fixed INIT headers are always in network byte
  2046. * order.
  2047. */
  2048. asoc->peer.i.init_tag =
  2049. ntohl(peer_init->init_hdr.init_tag);
  2050. asoc->peer.i.a_rwnd =
  2051. ntohl(peer_init->init_hdr.a_rwnd);
  2052. asoc->peer.i.num_outbound_streams =
  2053. ntohs(peer_init->init_hdr.num_outbound_streams);
  2054. asoc->peer.i.num_inbound_streams =
  2055. ntohs(peer_init->init_hdr.num_inbound_streams);
  2056. asoc->peer.i.initial_tsn =
  2057. ntohl(peer_init->init_hdr.initial_tsn);
  2058. asoc->strreset_inseq = asoc->peer.i.initial_tsn;
  2059. /* Apply the upper bounds for output streams based on peer's
  2060. * number of inbound streams.
  2061. */
  2062. if (asoc->c.sinit_num_ostreams >
  2063. ntohs(peer_init->init_hdr.num_inbound_streams)) {
  2064. asoc->c.sinit_num_ostreams =
  2065. ntohs(peer_init->init_hdr.num_inbound_streams);
  2066. }
  2067. if (asoc->c.sinit_max_instreams >
  2068. ntohs(peer_init->init_hdr.num_outbound_streams)) {
  2069. asoc->c.sinit_max_instreams =
  2070. ntohs(peer_init->init_hdr.num_outbound_streams);
  2071. }
  2072. /* Copy Initiation tag from INIT to VT_peer in cookie. */
  2073. asoc->c.peer_vtag = asoc->peer.i.init_tag;
  2074. /* Peer Rwnd : Current calculated value of the peer's rwnd. */
  2075. asoc->peer.rwnd = asoc->peer.i.a_rwnd;
  2076. /* Copy cookie in case we need to resend COOKIE-ECHO. */
  2077. cookie = asoc->peer.cookie;
  2078. if (cookie) {
  2079. asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
  2080. if (!asoc->peer.cookie)
  2081. goto clean_up;
  2082. }
  2083. /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
  2084. * high (for example, implementations MAY use the size of the receiver
  2085. * advertised window).
  2086. */
  2087. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2088. transports) {
  2089. transport->ssthresh = asoc->peer.i.a_rwnd;
  2090. }
  2091. /* Set up the TSN tracking pieces. */
  2092. if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
  2093. asoc->peer.i.initial_tsn, gfp))
  2094. goto clean_up;
  2095. /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
  2096. *
  2097. * The stream sequence number in all the streams shall start
  2098. * from 0 when the association is established. Also, when the
  2099. * stream sequence number reaches the value 65535 the next
  2100. * stream sequence number shall be set to 0.
  2101. */
  2102. if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams,
  2103. asoc->c.sinit_max_instreams, gfp))
  2104. goto clean_up;
  2105. if (!asoc->temp && sctp_assoc_set_id(asoc, gfp))
  2106. goto clean_up;
  2107. /* ADDIP Section 4.1 ASCONF Chunk Procedures
  2108. *
  2109. * When an endpoint has an ASCONF signaled change to be sent to the
  2110. * remote endpoint it should do the following:
  2111. * ...
  2112. * A2) A serial number should be assigned to the Chunk. The serial
  2113. * number should be a monotonically increasing number. All serial
  2114. * numbers are defined to be initialized at the start of the
  2115. * association to the same value as the Initial TSN.
  2116. */
  2117. asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
  2118. return 1;
  2119. clean_up:
  2120. /* Release the transport structures. */
  2121. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  2122. transport = list_entry(pos, struct sctp_transport, transports);
  2123. if (transport->state != SCTP_ACTIVE)
  2124. sctp_assoc_rm_peer(asoc, transport);
  2125. }
  2126. nomem:
  2127. return 0;
  2128. }
  2129. /* Update asoc with the option described in param.
  2130. *
  2131. * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
  2132. *
  2133. * asoc is the association to update.
  2134. * param is the variable length parameter to use for update.
  2135. * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
  2136. * If the current packet is an INIT we want to minimize the amount of
  2137. * work we do. In particular, we should not build transport
  2138. * structures for the addresses.
  2139. */
  2140. static int sctp_process_param(struct sctp_association *asoc,
  2141. union sctp_params param,
  2142. const union sctp_addr *peer_addr,
  2143. gfp_t gfp)
  2144. {
  2145. struct net *net = sock_net(asoc->base.sk);
  2146. struct sctp_endpoint *ep = asoc->ep;
  2147. union sctp_addr_param *addr_param;
  2148. struct sctp_transport *t;
  2149. enum sctp_scope scope;
  2150. union sctp_addr addr;
  2151. struct sctp_af *af;
  2152. int retval = 1, i;
  2153. u32 stale;
  2154. __u16 sat;
  2155. /* We maintain all INIT parameters in network byte order all the
  2156. * time. This allows us to not worry about whether the parameters
  2157. * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
  2158. */
  2159. switch (param.p->type) {
  2160. case SCTP_PARAM_IPV6_ADDRESS:
  2161. if (PF_INET6 != asoc->base.sk->sk_family)
  2162. break;
  2163. goto do_addr_param;
  2164. case SCTP_PARAM_IPV4_ADDRESS:
  2165. /* v4 addresses are not allowed on v6-only socket */
  2166. if (ipv6_only_sock(asoc->base.sk))
  2167. break;
  2168. do_addr_param:
  2169. af = sctp_get_af_specific(param_type2af(param.p->type));
  2170. af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
  2171. scope = sctp_scope(peer_addr);
  2172. if (sctp_in_scope(net, &addr, scope))
  2173. if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
  2174. return 0;
  2175. break;
  2176. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  2177. if (!net->sctp.cookie_preserve_enable)
  2178. break;
  2179. stale = ntohl(param.life->lifespan_increment);
  2180. /* Suggested Cookie Life span increment's unit is msec,
  2181. * (1/1000sec).
  2182. */
  2183. asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
  2184. break;
  2185. case SCTP_PARAM_HOST_NAME_ADDRESS:
  2186. pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
  2187. break;
  2188. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  2189. /* Turn off the default values first so we'll know which
  2190. * ones are really set by the peer.
  2191. */
  2192. asoc->peer.ipv4_address = 0;
  2193. asoc->peer.ipv6_address = 0;
  2194. /* Assume that peer supports the address family
  2195. * by which it sends a packet.
  2196. */
  2197. if (peer_addr->sa.sa_family == AF_INET6)
  2198. asoc->peer.ipv6_address = 1;
  2199. else if (peer_addr->sa.sa_family == AF_INET)
  2200. asoc->peer.ipv4_address = 1;
  2201. /* Cycle through address types; avoid divide by 0. */
  2202. sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  2203. if (sat)
  2204. sat /= sizeof(__u16);
  2205. for (i = 0; i < sat; ++i) {
  2206. switch (param.sat->types[i]) {
  2207. case SCTP_PARAM_IPV4_ADDRESS:
  2208. asoc->peer.ipv4_address = 1;
  2209. break;
  2210. case SCTP_PARAM_IPV6_ADDRESS:
  2211. if (PF_INET6 == asoc->base.sk->sk_family)
  2212. asoc->peer.ipv6_address = 1;
  2213. break;
  2214. case SCTP_PARAM_HOST_NAME_ADDRESS:
  2215. asoc->peer.hostname_address = 1;
  2216. break;
  2217. default: /* Just ignore anything else. */
  2218. break;
  2219. }
  2220. }
  2221. break;
  2222. case SCTP_PARAM_STATE_COOKIE:
  2223. asoc->peer.cookie_len =
  2224. ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  2225. asoc->peer.cookie = param.cookie->body;
  2226. break;
  2227. case SCTP_PARAM_HEARTBEAT_INFO:
  2228. /* Would be odd to receive, but it causes no problems. */
  2229. break;
  2230. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  2231. /* Rejected during verify stage. */
  2232. break;
  2233. case SCTP_PARAM_ECN_CAPABLE:
  2234. asoc->peer.ecn_capable = 1;
  2235. break;
  2236. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  2237. asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
  2238. break;
  2239. case SCTP_PARAM_SET_PRIMARY:
  2240. if (!net->sctp.addip_enable)
  2241. goto fall_through;
  2242. addr_param = param.v + sizeof(struct sctp_addip_param);
  2243. af = sctp_get_af_specific(param_type2af(addr_param->p.type));
  2244. if (af == NULL)
  2245. break;
  2246. af->from_addr_param(&addr, addr_param,
  2247. htons(asoc->peer.port), 0);
  2248. /* if the address is invalid, we can't process it.
  2249. * XXX: see spec for what to do.
  2250. */
  2251. if (!af->addr_valid(&addr, NULL, NULL))
  2252. break;
  2253. t = sctp_assoc_lookup_paddr(asoc, &addr);
  2254. if (!t)
  2255. break;
  2256. sctp_assoc_set_primary(asoc, t);
  2257. break;
  2258. case SCTP_PARAM_SUPPORTED_EXT:
  2259. sctp_process_ext_param(asoc, param);
  2260. break;
  2261. case SCTP_PARAM_FWD_TSN_SUPPORT:
  2262. if (asoc->prsctp_enable) {
  2263. asoc->peer.prsctp_capable = 1;
  2264. break;
  2265. }
  2266. /* Fall Through */
  2267. goto fall_through;
  2268. case SCTP_PARAM_RANDOM:
  2269. if (!ep->auth_enable)
  2270. goto fall_through;
  2271. /* Save peer's random parameter */
  2272. asoc->peer.peer_random = kmemdup(param.p,
  2273. ntohs(param.p->length), gfp);
  2274. if (!asoc->peer.peer_random) {
  2275. retval = 0;
  2276. break;
  2277. }
  2278. break;
  2279. case SCTP_PARAM_HMAC_ALGO:
  2280. if (!ep->auth_enable)
  2281. goto fall_through;
  2282. /* Save peer's HMAC list */
  2283. asoc->peer.peer_hmacs = kmemdup(param.p,
  2284. ntohs(param.p->length), gfp);
  2285. if (!asoc->peer.peer_hmacs) {
  2286. retval = 0;
  2287. break;
  2288. }
  2289. /* Set the default HMAC the peer requested*/
  2290. sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
  2291. break;
  2292. case SCTP_PARAM_CHUNKS:
  2293. if (!ep->auth_enable)
  2294. goto fall_through;
  2295. asoc->peer.peer_chunks = kmemdup(param.p,
  2296. ntohs(param.p->length), gfp);
  2297. if (!asoc->peer.peer_chunks)
  2298. retval = 0;
  2299. break;
  2300. fall_through:
  2301. default:
  2302. /* Any unrecognized parameters should have been caught
  2303. * and handled by sctp_verify_param() which should be
  2304. * called prior to this routine. Simply log the error
  2305. * here.
  2306. */
  2307. pr_debug("%s: ignoring param:%d for association:%p.\n",
  2308. __func__, ntohs(param.p->type), asoc);
  2309. break;
  2310. }
  2311. return retval;
  2312. }
  2313. /* Select a new verification tag. */
  2314. __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
  2315. {
  2316. /* I believe that this random number generator complies with RFC1750.
  2317. * A tag of 0 is reserved for special cases (e.g. INIT).
  2318. */
  2319. __u32 x;
  2320. do {
  2321. get_random_bytes(&x, sizeof(__u32));
  2322. } while (x == 0);
  2323. return x;
  2324. }
  2325. /* Select an initial TSN to send during startup. */
  2326. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
  2327. {
  2328. __u32 retval;
  2329. get_random_bytes(&retval, sizeof(__u32));
  2330. return retval;
  2331. }
  2332. /*
  2333. * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
  2334. * 0 1 2 3
  2335. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  2336. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2337. * | Type = 0xC1 | Chunk Flags | Chunk Length |
  2338. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2339. * | Serial Number |
  2340. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2341. * | Address Parameter |
  2342. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2343. * | ASCONF Parameter #1 |
  2344. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2345. * \ \
  2346. * / .... /
  2347. * \ \
  2348. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2349. * | ASCONF Parameter #N |
  2350. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2351. *
  2352. * Address Parameter and other parameter will not be wrapped in this function
  2353. */
  2354. static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
  2355. union sctp_addr *addr,
  2356. int vparam_len)
  2357. {
  2358. struct sctp_addiphdr asconf;
  2359. struct sctp_chunk *retval;
  2360. int length = sizeof(asconf) + vparam_len;
  2361. union sctp_addr_param addrparam;
  2362. int addrlen;
  2363. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  2364. addrlen = af->to_addr_param(addr, &addrparam);
  2365. if (!addrlen)
  2366. return NULL;
  2367. length += addrlen;
  2368. /* Create the chunk. */
  2369. retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
  2370. GFP_ATOMIC);
  2371. if (!retval)
  2372. return NULL;
  2373. asconf.serial = htonl(asoc->addip_serial++);
  2374. retval->subh.addip_hdr =
  2375. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2376. retval->param_hdr.v =
  2377. sctp_addto_chunk(retval, addrlen, &addrparam);
  2378. return retval;
  2379. }
  2380. /* ADDIP
  2381. * 3.2.1 Add IP Address
  2382. * 0 1 2 3
  2383. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  2384. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2385. * | Type = 0xC001 | Length = Variable |
  2386. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2387. * | ASCONF-Request Correlation ID |
  2388. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2389. * | Address Parameter |
  2390. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2391. *
  2392. * 3.2.2 Delete IP Address
  2393. * 0 1 2 3
  2394. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  2395. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2396. * | Type = 0xC002 | Length = Variable |
  2397. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2398. * | ASCONF-Request Correlation ID |
  2399. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2400. * | Address Parameter |
  2401. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2402. *
  2403. */
  2404. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  2405. union sctp_addr *laddr,
  2406. struct sockaddr *addrs,
  2407. int addrcnt, __be16 flags)
  2408. {
  2409. union sctp_addr_param addr_param;
  2410. struct sctp_addip_param param;
  2411. int paramlen = sizeof(param);
  2412. struct sctp_chunk *retval;
  2413. int addr_param_len = 0;
  2414. union sctp_addr *addr;
  2415. int totallen = 0, i;
  2416. int del_pickup = 0;
  2417. struct sctp_af *af;
  2418. void *addr_buf;
  2419. /* Get total length of all the address parameters. */
  2420. addr_buf = addrs;
  2421. for (i = 0; i < addrcnt; i++) {
  2422. addr = addr_buf;
  2423. af = sctp_get_af_specific(addr->v4.sin_family);
  2424. addr_param_len = af->to_addr_param(addr, &addr_param);
  2425. totallen += paramlen;
  2426. totallen += addr_param_len;
  2427. addr_buf += af->sockaddr_len;
  2428. if (asoc->asconf_addr_del_pending && !del_pickup) {
  2429. /* reuse the parameter length from the same scope one */
  2430. totallen += paramlen;
  2431. totallen += addr_param_len;
  2432. del_pickup = 1;
  2433. pr_debug("%s: picked same-scope del_pending addr, "
  2434. "totallen for all addresses is %d\n",
  2435. __func__, totallen);
  2436. }
  2437. }
  2438. /* Create an asconf chunk with the required length. */
  2439. retval = sctp_make_asconf(asoc, laddr, totallen);
  2440. if (!retval)
  2441. return NULL;
  2442. /* Add the address parameters to the asconf chunk. */
  2443. addr_buf = addrs;
  2444. for (i = 0; i < addrcnt; i++) {
  2445. addr = addr_buf;
  2446. af = sctp_get_af_specific(addr->v4.sin_family);
  2447. addr_param_len = af->to_addr_param(addr, &addr_param);
  2448. param.param_hdr.type = flags;
  2449. param.param_hdr.length = htons(paramlen + addr_param_len);
  2450. param.crr_id = htonl(i);
  2451. sctp_addto_chunk(retval, paramlen, &param);
  2452. sctp_addto_chunk(retval, addr_param_len, &addr_param);
  2453. addr_buf += af->sockaddr_len;
  2454. }
  2455. if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
  2456. addr = asoc->asconf_addr_del_pending;
  2457. af = sctp_get_af_specific(addr->v4.sin_family);
  2458. addr_param_len = af->to_addr_param(addr, &addr_param);
  2459. param.param_hdr.type = SCTP_PARAM_DEL_IP;
  2460. param.param_hdr.length = htons(paramlen + addr_param_len);
  2461. param.crr_id = htonl(i);
  2462. sctp_addto_chunk(retval, paramlen, &param);
  2463. sctp_addto_chunk(retval, addr_param_len, &addr_param);
  2464. }
  2465. return retval;
  2466. }
  2467. /* ADDIP
  2468. * 3.2.4 Set Primary IP Address
  2469. * 0 1 2 3
  2470. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  2471. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2472. * | Type =0xC004 | Length = Variable |
  2473. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2474. * | ASCONF-Request Correlation ID |
  2475. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2476. * | Address Parameter |
  2477. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2478. *
  2479. * Create an ASCONF chunk with Set Primary IP address parameter.
  2480. */
  2481. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  2482. union sctp_addr *addr)
  2483. {
  2484. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  2485. union sctp_addr_param addrparam;
  2486. struct sctp_addip_param param;
  2487. struct sctp_chunk *retval;
  2488. int len = sizeof(param);
  2489. int addrlen;
  2490. addrlen = af->to_addr_param(addr, &addrparam);
  2491. if (!addrlen)
  2492. return NULL;
  2493. len += addrlen;
  2494. /* Create the chunk and make asconf header. */
  2495. retval = sctp_make_asconf(asoc, addr, len);
  2496. if (!retval)
  2497. return NULL;
  2498. param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
  2499. param.param_hdr.length = htons(len);
  2500. param.crr_id = 0;
  2501. sctp_addto_chunk(retval, sizeof(param), &param);
  2502. sctp_addto_chunk(retval, addrlen, &addrparam);
  2503. return retval;
  2504. }
  2505. /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
  2506. * 0 1 2 3
  2507. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  2508. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2509. * | Type = 0x80 | Chunk Flags | Chunk Length |
  2510. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2511. * | Serial Number |
  2512. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2513. * | ASCONF Parameter Response#1 |
  2514. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2515. * \ \
  2516. * / .... /
  2517. * \ \
  2518. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2519. * | ASCONF Parameter Response#N |
  2520. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2521. *
  2522. * Create an ASCONF_ACK chunk with enough space for the parameter responses.
  2523. */
  2524. static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
  2525. __u32 serial, int vparam_len)
  2526. {
  2527. struct sctp_addiphdr asconf;
  2528. struct sctp_chunk *retval;
  2529. int length = sizeof(asconf) + vparam_len;
  2530. /* Create the chunk. */
  2531. retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
  2532. GFP_ATOMIC);
  2533. if (!retval)
  2534. return NULL;
  2535. asconf.serial = htonl(serial);
  2536. retval->subh.addip_hdr =
  2537. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2538. return retval;
  2539. }
  2540. /* Add response parameters to an ASCONF_ACK chunk. */
  2541. static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
  2542. __be16 err_code,
  2543. struct sctp_addip_param *asconf_param)
  2544. {
  2545. struct sctp_addip_param ack_param;
  2546. struct sctp_errhdr err_param;
  2547. int asconf_param_len = 0;
  2548. int err_param_len = 0;
  2549. __be16 response_type;
  2550. if (SCTP_ERROR_NO_ERROR == err_code) {
  2551. response_type = SCTP_PARAM_SUCCESS_REPORT;
  2552. } else {
  2553. response_type = SCTP_PARAM_ERR_CAUSE;
  2554. err_param_len = sizeof(err_param);
  2555. if (asconf_param)
  2556. asconf_param_len =
  2557. ntohs(asconf_param->param_hdr.length);
  2558. }
  2559. /* Add Success Indication or Error Cause Indication parameter. */
  2560. ack_param.param_hdr.type = response_type;
  2561. ack_param.param_hdr.length = htons(sizeof(ack_param) +
  2562. err_param_len +
  2563. asconf_param_len);
  2564. ack_param.crr_id = crr_id;
  2565. sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
  2566. if (SCTP_ERROR_NO_ERROR == err_code)
  2567. return;
  2568. /* Add Error Cause parameter. */
  2569. err_param.cause = err_code;
  2570. err_param.length = htons(err_param_len + asconf_param_len);
  2571. sctp_addto_chunk(chunk, err_param_len, &err_param);
  2572. /* Add the failed TLV copied from ASCONF chunk. */
  2573. if (asconf_param)
  2574. sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
  2575. }
  2576. /* Process a asconf parameter. */
  2577. static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
  2578. struct sctp_chunk *asconf,
  2579. struct sctp_addip_param *asconf_param)
  2580. {
  2581. union sctp_addr_param *addr_param;
  2582. struct sctp_transport *peer;
  2583. union sctp_addr addr;
  2584. struct sctp_af *af;
  2585. addr_param = (void *)asconf_param + sizeof(*asconf_param);
  2586. if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
  2587. asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
  2588. asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
  2589. return SCTP_ERROR_UNKNOWN_PARAM;
  2590. switch (addr_param->p.type) {
  2591. case SCTP_PARAM_IPV6_ADDRESS:
  2592. if (!asoc->peer.ipv6_address)
  2593. return SCTP_ERROR_DNS_FAILED;
  2594. break;
  2595. case SCTP_PARAM_IPV4_ADDRESS:
  2596. if (!asoc->peer.ipv4_address)
  2597. return SCTP_ERROR_DNS_FAILED;
  2598. break;
  2599. default:
  2600. return SCTP_ERROR_DNS_FAILED;
  2601. }
  2602. af = sctp_get_af_specific(param_type2af(addr_param->p.type));
  2603. if (unlikely(!af))
  2604. return SCTP_ERROR_DNS_FAILED;
  2605. af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
  2606. /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
  2607. * or multicast address.
  2608. * (note: wildcard is permitted and requires special handling so
  2609. * make sure we check for that)
  2610. */
  2611. if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
  2612. return SCTP_ERROR_DNS_FAILED;
  2613. switch (asconf_param->param_hdr.type) {
  2614. case SCTP_PARAM_ADD_IP:
  2615. /* Section 4.2.1:
  2616. * If the address 0.0.0.0 or ::0 is provided, the source
  2617. * address of the packet MUST be added.
  2618. */
  2619. if (af->is_any(&addr))
  2620. memcpy(&addr, &asconf->source, sizeof(addr));
  2621. /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
  2622. * request and does not have the local resources to add this
  2623. * new address to the association, it MUST return an Error
  2624. * Cause TLV set to the new error code 'Operation Refused
  2625. * Due to Resource Shortage'.
  2626. */
  2627. peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
  2628. if (!peer)
  2629. return SCTP_ERROR_RSRC_LOW;
  2630. /* Start the heartbeat timer. */
  2631. sctp_transport_reset_hb_timer(peer);
  2632. asoc->new_transport = peer;
  2633. break;
  2634. case SCTP_PARAM_DEL_IP:
  2635. /* ADDIP 4.3 D7) If a request is received to delete the
  2636. * last remaining IP address of a peer endpoint, the receiver
  2637. * MUST send an Error Cause TLV with the error cause set to the
  2638. * new error code 'Request to Delete Last Remaining IP Address'.
  2639. */
  2640. if (asoc->peer.transport_count == 1)
  2641. return SCTP_ERROR_DEL_LAST_IP;
  2642. /* ADDIP 4.3 D8) If a request is received to delete an IP
  2643. * address which is also the source address of the IP packet
  2644. * which contained the ASCONF chunk, the receiver MUST reject
  2645. * this request. To reject the request the receiver MUST send
  2646. * an Error Cause TLV set to the new error code 'Request to
  2647. * Delete Source IP Address'
  2648. */
  2649. if (sctp_cmp_addr_exact(&asconf->source, &addr))
  2650. return SCTP_ERROR_DEL_SRC_IP;
  2651. /* Section 4.2.2
  2652. * If the address 0.0.0.0 or ::0 is provided, all
  2653. * addresses of the peer except the source address of the
  2654. * packet MUST be deleted.
  2655. */
  2656. if (af->is_any(&addr)) {
  2657. sctp_assoc_set_primary(asoc, asconf->transport);
  2658. sctp_assoc_del_nonprimary_peers(asoc,
  2659. asconf->transport);
  2660. return SCTP_ERROR_NO_ERROR;
  2661. }
  2662. /* If the address is not part of the association, the
  2663. * ASCONF-ACK with Error Cause Indication Parameter
  2664. * which including cause of Unresolvable Address should
  2665. * be sent.
  2666. */
  2667. peer = sctp_assoc_lookup_paddr(asoc, &addr);
  2668. if (!peer)
  2669. return SCTP_ERROR_DNS_FAILED;
  2670. sctp_assoc_rm_peer(asoc, peer);
  2671. break;
  2672. case SCTP_PARAM_SET_PRIMARY:
  2673. /* ADDIP Section 4.2.4
  2674. * If the address 0.0.0.0 or ::0 is provided, the receiver
  2675. * MAY mark the source address of the packet as its
  2676. * primary.
  2677. */
  2678. if (af->is_any(&addr))
  2679. memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
  2680. peer = sctp_assoc_lookup_paddr(asoc, &addr);
  2681. if (!peer)
  2682. return SCTP_ERROR_DNS_FAILED;
  2683. sctp_assoc_set_primary(asoc, peer);
  2684. break;
  2685. }
  2686. return SCTP_ERROR_NO_ERROR;
  2687. }
  2688. /* Verify the ASCONF packet before we process it. */
  2689. bool sctp_verify_asconf(const struct sctp_association *asoc,
  2690. struct sctp_chunk *chunk, bool addr_param_needed,
  2691. struct sctp_paramhdr **errp)
  2692. {
  2693. struct sctp_addip_chunk *addip;
  2694. bool addr_param_seen = false;
  2695. union sctp_params param;
  2696. addip = (struct sctp_addip_chunk *)chunk->chunk_hdr;
  2697. sctp_walk_params(param, addip, addip_hdr.params) {
  2698. size_t length = ntohs(param.p->length);
  2699. *errp = param.p;
  2700. switch (param.p->type) {
  2701. case SCTP_PARAM_ERR_CAUSE:
  2702. break;
  2703. case SCTP_PARAM_IPV4_ADDRESS:
  2704. if (length != sizeof(struct sctp_ipv4addr_param))
  2705. return false;
  2706. /* ensure there is only one addr param and it's in the
  2707. * beginning of addip_hdr params, or we reject it.
  2708. */
  2709. if (param.v != addip->addip_hdr.params)
  2710. return false;
  2711. addr_param_seen = true;
  2712. break;
  2713. case SCTP_PARAM_IPV6_ADDRESS:
  2714. if (length != sizeof(struct sctp_ipv6addr_param))
  2715. return false;
  2716. if (param.v != addip->addip_hdr.params)
  2717. return false;
  2718. addr_param_seen = true;
  2719. break;
  2720. case SCTP_PARAM_ADD_IP:
  2721. case SCTP_PARAM_DEL_IP:
  2722. case SCTP_PARAM_SET_PRIMARY:
  2723. /* In ASCONF chunks, these need to be first. */
  2724. if (addr_param_needed && !addr_param_seen)
  2725. return false;
  2726. length = ntohs(param.addip->param_hdr.length);
  2727. if (length < sizeof(struct sctp_addip_param) +
  2728. sizeof(**errp))
  2729. return false;
  2730. break;
  2731. case SCTP_PARAM_SUCCESS_REPORT:
  2732. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  2733. if (length != sizeof(struct sctp_addip_param))
  2734. return false;
  2735. break;
  2736. default:
  2737. /* This is unkown to us, reject! */
  2738. return false;
  2739. }
  2740. }
  2741. /* Remaining sanity checks. */
  2742. if (addr_param_needed && !addr_param_seen)
  2743. return false;
  2744. if (!addr_param_needed && addr_param_seen)
  2745. return false;
  2746. if (param.v != chunk->chunk_end)
  2747. return false;
  2748. return true;
  2749. }
  2750. /* Process an incoming ASCONF chunk with the next expected serial no. and
  2751. * return an ASCONF_ACK chunk to be sent in response.
  2752. */
  2753. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  2754. struct sctp_chunk *asconf)
  2755. {
  2756. union sctp_addr_param *addr_param;
  2757. struct sctp_addip_chunk *addip;
  2758. struct sctp_chunk *asconf_ack;
  2759. bool all_param_pass = true;
  2760. struct sctp_addiphdr *hdr;
  2761. int length = 0, chunk_len;
  2762. union sctp_params param;
  2763. __be16 err_code;
  2764. __u32 serial;
  2765. addip = (struct sctp_addip_chunk *)asconf->chunk_hdr;
  2766. chunk_len = ntohs(asconf->chunk_hdr->length) -
  2767. sizeof(struct sctp_chunkhdr);
  2768. hdr = (struct sctp_addiphdr *)asconf->skb->data;
  2769. serial = ntohl(hdr->serial);
  2770. /* Skip the addiphdr and store a pointer to address parameter. */
  2771. length = sizeof(*hdr);
  2772. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2773. chunk_len -= length;
  2774. /* Skip the address parameter and store a pointer to the first
  2775. * asconf parameter.
  2776. */
  2777. length = ntohs(addr_param->p.length);
  2778. chunk_len -= length;
  2779. /* create an ASCONF_ACK chunk.
  2780. * Based on the definitions of parameters, we know that the size of
  2781. * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
  2782. * parameters.
  2783. */
  2784. asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
  2785. if (!asconf_ack)
  2786. goto done;
  2787. /* Process the TLVs contained within the ASCONF chunk. */
  2788. sctp_walk_params(param, addip, addip_hdr.params) {
  2789. /* Skip preceeding address parameters. */
  2790. if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
  2791. param.p->type == SCTP_PARAM_IPV6_ADDRESS)
  2792. continue;
  2793. err_code = sctp_process_asconf_param(asoc, asconf,
  2794. param.addip);
  2795. /* ADDIP 4.1 A7)
  2796. * If an error response is received for a TLV parameter,
  2797. * all TLVs with no response before the failed TLV are
  2798. * considered successful if not reported. All TLVs after
  2799. * the failed response are considered unsuccessful unless
  2800. * a specific success indication is present for the parameter.
  2801. */
  2802. if (err_code != SCTP_ERROR_NO_ERROR)
  2803. all_param_pass = false;
  2804. if (!all_param_pass)
  2805. sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
  2806. err_code, param.addip);
  2807. /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
  2808. * an IP address sends an 'Out of Resource' in its response, it
  2809. * MUST also fail any subsequent add or delete requests bundled
  2810. * in the ASCONF.
  2811. */
  2812. if (err_code == SCTP_ERROR_RSRC_LOW)
  2813. goto done;
  2814. }
  2815. done:
  2816. asoc->peer.addip_serial++;
  2817. /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
  2818. * after freeing the reference to old asconf ack if any.
  2819. */
  2820. if (asconf_ack) {
  2821. sctp_chunk_hold(asconf_ack);
  2822. list_add_tail(&asconf_ack->transmitted_list,
  2823. &asoc->asconf_ack_list);
  2824. }
  2825. return asconf_ack;
  2826. }
  2827. /* Process a asconf parameter that is successfully acked. */
  2828. static void sctp_asconf_param_success(struct sctp_association *asoc,
  2829. struct sctp_addip_param *asconf_param)
  2830. {
  2831. struct sctp_bind_addr *bp = &asoc->base.bind_addr;
  2832. union sctp_addr_param *addr_param;
  2833. struct sctp_sockaddr_entry *saddr;
  2834. struct sctp_transport *transport;
  2835. union sctp_addr addr;
  2836. struct sctp_af *af;
  2837. addr_param = (void *)asconf_param + sizeof(*asconf_param);
  2838. /* We have checked the packet before, so we do not check again. */
  2839. af = sctp_get_af_specific(param_type2af(addr_param->p.type));
  2840. af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
  2841. switch (asconf_param->param_hdr.type) {
  2842. case SCTP_PARAM_ADD_IP:
  2843. /* This is always done in BH context with a socket lock
  2844. * held, so the list can not change.
  2845. */
  2846. local_bh_disable();
  2847. list_for_each_entry(saddr, &bp->address_list, list) {
  2848. if (sctp_cmp_addr_exact(&saddr->a, &addr))
  2849. saddr->state = SCTP_ADDR_SRC;
  2850. }
  2851. local_bh_enable();
  2852. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2853. transports) {
  2854. sctp_transport_dst_release(transport);
  2855. }
  2856. break;
  2857. case SCTP_PARAM_DEL_IP:
  2858. local_bh_disable();
  2859. sctp_del_bind_addr(bp, &addr);
  2860. if (asoc->asconf_addr_del_pending != NULL &&
  2861. sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
  2862. kfree(asoc->asconf_addr_del_pending);
  2863. asoc->asconf_addr_del_pending = NULL;
  2864. }
  2865. local_bh_enable();
  2866. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2867. transports) {
  2868. sctp_transport_dst_release(transport);
  2869. }
  2870. break;
  2871. default:
  2872. break;
  2873. }
  2874. }
  2875. /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
  2876. * for the given asconf parameter. If there is no response for this parameter,
  2877. * return the error code based on the third argument 'no_err'.
  2878. * ADDIP 4.1
  2879. * A7) If an error response is received for a TLV parameter, all TLVs with no
  2880. * response before the failed TLV are considered successful if not reported.
  2881. * All TLVs after the failed response are considered unsuccessful unless a
  2882. * specific success indication is present for the parameter.
  2883. */
  2884. static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
  2885. struct sctp_addip_param *asconf_param,
  2886. int no_err)
  2887. {
  2888. struct sctp_addip_param *asconf_ack_param;
  2889. struct sctp_errhdr *err_param;
  2890. int asconf_ack_len;
  2891. __be16 err_code;
  2892. int length;
  2893. if (no_err)
  2894. err_code = SCTP_ERROR_NO_ERROR;
  2895. else
  2896. err_code = SCTP_ERROR_REQ_REFUSED;
  2897. asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
  2898. sizeof(struct sctp_chunkhdr);
  2899. /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
  2900. * the first asconf_ack parameter.
  2901. */
  2902. length = sizeof(struct sctp_addiphdr);
  2903. asconf_ack_param = (struct sctp_addip_param *)(asconf_ack->skb->data +
  2904. length);
  2905. asconf_ack_len -= length;
  2906. while (asconf_ack_len > 0) {
  2907. if (asconf_ack_param->crr_id == asconf_param->crr_id) {
  2908. switch (asconf_ack_param->param_hdr.type) {
  2909. case SCTP_PARAM_SUCCESS_REPORT:
  2910. return SCTP_ERROR_NO_ERROR;
  2911. case SCTP_PARAM_ERR_CAUSE:
  2912. length = sizeof(*asconf_ack_param);
  2913. err_param = (void *)asconf_ack_param + length;
  2914. asconf_ack_len -= length;
  2915. if (asconf_ack_len > 0)
  2916. return err_param->cause;
  2917. else
  2918. return SCTP_ERROR_INV_PARAM;
  2919. break;
  2920. default:
  2921. return SCTP_ERROR_INV_PARAM;
  2922. }
  2923. }
  2924. length = ntohs(asconf_ack_param->param_hdr.length);
  2925. asconf_ack_param = (void *)asconf_ack_param + length;
  2926. asconf_ack_len -= length;
  2927. }
  2928. return err_code;
  2929. }
  2930. /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
  2931. int sctp_process_asconf_ack(struct sctp_association *asoc,
  2932. struct sctp_chunk *asconf_ack)
  2933. {
  2934. struct sctp_chunk *asconf = asoc->addip_last_asconf;
  2935. struct sctp_addip_param *asconf_param;
  2936. __be16 err_code = SCTP_ERROR_NO_ERROR;
  2937. union sctp_addr_param *addr_param;
  2938. int asconf_len = asconf->skb->len;
  2939. int all_param_pass = 0;
  2940. int length = 0;
  2941. int no_err = 1;
  2942. int retval = 0;
  2943. /* Skip the chunkhdr and addiphdr from the last asconf sent and store
  2944. * a pointer to address parameter.
  2945. */
  2946. length = sizeof(struct sctp_addip_chunk);
  2947. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2948. asconf_len -= length;
  2949. /* Skip the address parameter in the last asconf sent and store a
  2950. * pointer to the first asconf parameter.
  2951. */
  2952. length = ntohs(addr_param->p.length);
  2953. asconf_param = (void *)addr_param + length;
  2954. asconf_len -= length;
  2955. /* ADDIP 4.1
  2956. * A8) If there is no response(s) to specific TLV parameter(s), and no
  2957. * failures are indicated, then all request(s) are considered
  2958. * successful.
  2959. */
  2960. if (asconf_ack->skb->len == sizeof(struct sctp_addiphdr))
  2961. all_param_pass = 1;
  2962. /* Process the TLVs contained in the last sent ASCONF chunk. */
  2963. while (asconf_len > 0) {
  2964. if (all_param_pass)
  2965. err_code = SCTP_ERROR_NO_ERROR;
  2966. else {
  2967. err_code = sctp_get_asconf_response(asconf_ack,
  2968. asconf_param,
  2969. no_err);
  2970. if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
  2971. no_err = 0;
  2972. }
  2973. switch (err_code) {
  2974. case SCTP_ERROR_NO_ERROR:
  2975. sctp_asconf_param_success(asoc, asconf_param);
  2976. break;
  2977. case SCTP_ERROR_RSRC_LOW:
  2978. retval = 1;
  2979. break;
  2980. case SCTP_ERROR_UNKNOWN_PARAM:
  2981. /* Disable sending this type of asconf parameter in
  2982. * future.
  2983. */
  2984. asoc->peer.addip_disabled_mask |=
  2985. asconf_param->param_hdr.type;
  2986. break;
  2987. case SCTP_ERROR_REQ_REFUSED:
  2988. case SCTP_ERROR_DEL_LAST_IP:
  2989. case SCTP_ERROR_DEL_SRC_IP:
  2990. default:
  2991. break;
  2992. }
  2993. /* Skip the processed asconf parameter and move to the next
  2994. * one.
  2995. */
  2996. length = ntohs(asconf_param->param_hdr.length);
  2997. asconf_param = (void *)asconf_param + length;
  2998. asconf_len -= length;
  2999. }
  3000. if (no_err && asoc->src_out_of_asoc_ok) {
  3001. asoc->src_out_of_asoc_ok = 0;
  3002. sctp_transport_immediate_rtx(asoc->peer.primary_path);
  3003. }
  3004. /* Free the cached last sent asconf chunk. */
  3005. list_del_init(&asconf->transmitted_list);
  3006. sctp_chunk_free(asconf);
  3007. asoc->addip_last_asconf = NULL;
  3008. return retval;
  3009. }
  3010. /* Make a FWD TSN chunk. */
  3011. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  3012. __u32 new_cum_tsn, size_t nstreams,
  3013. struct sctp_fwdtsn_skip *skiplist)
  3014. {
  3015. struct sctp_chunk *retval = NULL;
  3016. struct sctp_fwdtsn_hdr ftsn_hdr;
  3017. struct sctp_fwdtsn_skip skip;
  3018. size_t hint;
  3019. int i;
  3020. hint = (nstreams + 1) * sizeof(__u32);
  3021. retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
  3022. if (!retval)
  3023. return NULL;
  3024. ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
  3025. retval->subh.fwdtsn_hdr =
  3026. sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
  3027. for (i = 0; i < nstreams; i++) {
  3028. skip.stream = skiplist[i].stream;
  3029. skip.ssn = skiplist[i].ssn;
  3030. sctp_addto_chunk(retval, sizeof(skip), &skip);
  3031. }
  3032. return retval;
  3033. }
  3034. /* RE-CONFIG 3.1 (RE-CONFIG chunk)
  3035. * 0 1 2 3
  3036. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3037. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3038. * | Type = 130 | Chunk Flags | Chunk Length |
  3039. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3040. * \ \
  3041. * / Re-configuration Parameter /
  3042. * \ \
  3043. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3044. * \ \
  3045. * / Re-configuration Parameter (optional) /
  3046. * \ \
  3047. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3048. */
  3049. static struct sctp_chunk *sctp_make_reconf(const struct sctp_association *asoc,
  3050. int length)
  3051. {
  3052. struct sctp_reconf_chunk *reconf;
  3053. struct sctp_chunk *retval;
  3054. retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
  3055. GFP_ATOMIC);
  3056. if (!retval)
  3057. return NULL;
  3058. reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
  3059. retval->param_hdr.v = reconf->params;
  3060. return retval;
  3061. }
  3062. /* RE-CONFIG 4.1 (STREAM OUT RESET)
  3063. * 0 1 2 3
  3064. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3065. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3066. * | Parameter Type = 13 | Parameter Length = 16 + 2 * N |
  3067. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3068. * | Re-configuration Request Sequence Number |
  3069. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3070. * | Re-configuration Response Sequence Number |
  3071. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3072. * | Sender's Last Assigned TSN |
  3073. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3074. * | Stream Number 1 (optional) | Stream Number 2 (optional) |
  3075. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3076. * / ...... /
  3077. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3078. * | Stream Number N-1 (optional) | Stream Number N (optional) |
  3079. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3080. *
  3081. * RE-CONFIG 4.2 (STREAM IN RESET)
  3082. * 0 1 2 3
  3083. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3084. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3085. * | Parameter Type = 14 | Parameter Length = 8 + 2 * N |
  3086. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3087. * | Re-configuration Request Sequence Number |
  3088. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3089. * | Stream Number 1 (optional) | Stream Number 2 (optional) |
  3090. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3091. * / ...... /
  3092. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3093. * | Stream Number N-1 (optional) | Stream Number N (optional) |
  3094. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3095. */
  3096. struct sctp_chunk *sctp_make_strreset_req(
  3097. const struct sctp_association *asoc,
  3098. __u16 stream_num, __be16 *stream_list,
  3099. bool out, bool in)
  3100. {
  3101. struct sctp_strreset_outreq outreq;
  3102. __u16 stream_len = stream_num * 2;
  3103. struct sctp_strreset_inreq inreq;
  3104. struct sctp_chunk *retval;
  3105. __u16 outlen, inlen;
  3106. outlen = (sizeof(outreq) + stream_len) * out;
  3107. inlen = (sizeof(inreq) + stream_len) * in;
  3108. retval = sctp_make_reconf(asoc, outlen + inlen);
  3109. if (!retval)
  3110. return NULL;
  3111. if (outlen) {
  3112. outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
  3113. outreq.param_hdr.length = htons(outlen);
  3114. outreq.request_seq = htonl(asoc->strreset_outseq);
  3115. outreq.response_seq = htonl(asoc->strreset_inseq - 1);
  3116. outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
  3117. sctp_addto_chunk(retval, sizeof(outreq), &outreq);
  3118. if (stream_len)
  3119. sctp_addto_chunk(retval, stream_len, stream_list);
  3120. }
  3121. if (inlen) {
  3122. inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
  3123. inreq.param_hdr.length = htons(inlen);
  3124. inreq.request_seq = htonl(asoc->strreset_outseq + out);
  3125. sctp_addto_chunk(retval, sizeof(inreq), &inreq);
  3126. if (stream_len)
  3127. sctp_addto_chunk(retval, stream_len, stream_list);
  3128. }
  3129. return retval;
  3130. }
  3131. /* RE-CONFIG 4.3 (SSN/TSN RESET ALL)
  3132. * 0 1 2 3
  3133. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3134. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3135. * | Parameter Type = 15 | Parameter Length = 8 |
  3136. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3137. * | Re-configuration Request Sequence Number |
  3138. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3139. */
  3140. struct sctp_chunk *sctp_make_strreset_tsnreq(
  3141. const struct sctp_association *asoc)
  3142. {
  3143. struct sctp_strreset_tsnreq tsnreq;
  3144. __u16 length = sizeof(tsnreq);
  3145. struct sctp_chunk *retval;
  3146. retval = sctp_make_reconf(asoc, length);
  3147. if (!retval)
  3148. return NULL;
  3149. tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
  3150. tsnreq.param_hdr.length = htons(length);
  3151. tsnreq.request_seq = htonl(asoc->strreset_outseq);
  3152. sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
  3153. return retval;
  3154. }
  3155. /* RE-CONFIG 4.5/4.6 (ADD STREAM)
  3156. * 0 1 2 3
  3157. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3158. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3159. * | Parameter Type = 17 | Parameter Length = 12 |
  3160. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3161. * | Re-configuration Request Sequence Number |
  3162. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3163. * | Number of new streams | Reserved |
  3164. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3165. */
  3166. struct sctp_chunk *sctp_make_strreset_addstrm(
  3167. const struct sctp_association *asoc,
  3168. __u16 out, __u16 in)
  3169. {
  3170. struct sctp_strreset_addstrm addstrm;
  3171. __u16 size = sizeof(addstrm);
  3172. struct sctp_chunk *retval;
  3173. retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
  3174. if (!retval)
  3175. return NULL;
  3176. if (out) {
  3177. addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
  3178. addstrm.param_hdr.length = htons(size);
  3179. addstrm.number_of_streams = htons(out);
  3180. addstrm.request_seq = htonl(asoc->strreset_outseq);
  3181. addstrm.reserved = 0;
  3182. sctp_addto_chunk(retval, size, &addstrm);
  3183. }
  3184. if (in) {
  3185. addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
  3186. addstrm.param_hdr.length = htons(size);
  3187. addstrm.number_of_streams = htons(in);
  3188. addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
  3189. addstrm.reserved = 0;
  3190. sctp_addto_chunk(retval, size, &addstrm);
  3191. }
  3192. return retval;
  3193. }
  3194. /* RE-CONFIG 4.4 (RESP)
  3195. * 0 1 2 3
  3196. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3197. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3198. * | Parameter Type = 16 | Parameter Length |
  3199. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3200. * | Re-configuration Response Sequence Number |
  3201. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3202. * | Result |
  3203. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3204. */
  3205. struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
  3206. __u32 result, __u32 sn)
  3207. {
  3208. struct sctp_strreset_resp resp;
  3209. __u16 length = sizeof(resp);
  3210. struct sctp_chunk *retval;
  3211. retval = sctp_make_reconf(asoc, length);
  3212. if (!retval)
  3213. return NULL;
  3214. resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
  3215. resp.param_hdr.length = htons(length);
  3216. resp.response_seq = htonl(sn);
  3217. resp.result = htonl(result);
  3218. sctp_addto_chunk(retval, sizeof(resp), &resp);
  3219. return retval;
  3220. }
  3221. /* RE-CONFIG 4.4 OPTIONAL (TSNRESP)
  3222. * 0 1 2 3
  3223. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  3224. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3225. * | Parameter Type = 16 | Parameter Length |
  3226. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3227. * | Re-configuration Response Sequence Number |
  3228. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3229. * | Result |
  3230. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3231. * | Sender's Next TSN (optional) |
  3232. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3233. * | Receiver's Next TSN (optional) |
  3234. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3235. */
  3236. struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
  3237. __u32 result, __u32 sn,
  3238. __u32 sender_tsn,
  3239. __u32 receiver_tsn)
  3240. {
  3241. struct sctp_strreset_resptsn tsnresp;
  3242. __u16 length = sizeof(tsnresp);
  3243. struct sctp_chunk *retval;
  3244. retval = sctp_make_reconf(asoc, length);
  3245. if (!retval)
  3246. return NULL;
  3247. tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
  3248. tsnresp.param_hdr.length = htons(length);
  3249. tsnresp.response_seq = htonl(sn);
  3250. tsnresp.result = htonl(result);
  3251. tsnresp.senders_next_tsn = htonl(sender_tsn);
  3252. tsnresp.receivers_next_tsn = htonl(receiver_tsn);
  3253. sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
  3254. return retval;
  3255. }
  3256. bool sctp_verify_reconf(const struct sctp_association *asoc,
  3257. struct sctp_chunk *chunk,
  3258. struct sctp_paramhdr **errp)
  3259. {
  3260. struct sctp_reconf_chunk *hdr;
  3261. union sctp_params param;
  3262. __be16 last = 0;
  3263. __u16 cnt = 0;
  3264. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  3265. sctp_walk_params(param, hdr, params) {
  3266. __u16 length = ntohs(param.p->length);
  3267. *errp = param.p;
  3268. if (cnt++ > 2)
  3269. return false;
  3270. switch (param.p->type) {
  3271. case SCTP_PARAM_RESET_OUT_REQUEST:
  3272. if (length < sizeof(struct sctp_strreset_outreq) ||
  3273. (last && last != SCTP_PARAM_RESET_RESPONSE &&
  3274. last != SCTP_PARAM_RESET_IN_REQUEST))
  3275. return false;
  3276. break;
  3277. case SCTP_PARAM_RESET_IN_REQUEST:
  3278. if (length < sizeof(struct sctp_strreset_inreq) ||
  3279. (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
  3280. return false;
  3281. break;
  3282. case SCTP_PARAM_RESET_RESPONSE:
  3283. if ((length != sizeof(struct sctp_strreset_resp) &&
  3284. length != sizeof(struct sctp_strreset_resptsn)) ||
  3285. (last && last != SCTP_PARAM_RESET_RESPONSE &&
  3286. last != SCTP_PARAM_RESET_OUT_REQUEST))
  3287. return false;
  3288. break;
  3289. case SCTP_PARAM_RESET_TSN_REQUEST:
  3290. if (length !=
  3291. sizeof(struct sctp_strreset_tsnreq) || last)
  3292. return false;
  3293. break;
  3294. case SCTP_PARAM_RESET_ADD_IN_STREAMS:
  3295. if (length != sizeof(struct sctp_strreset_addstrm) ||
  3296. (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
  3297. return false;
  3298. break;
  3299. case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
  3300. if (length != sizeof(struct sctp_strreset_addstrm) ||
  3301. (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
  3302. return false;
  3303. break;
  3304. default:
  3305. return false;
  3306. }
  3307. last = param.p->type;
  3308. }
  3309. return true;
  3310. }