sm_make_chunk.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846
  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_endpoint *ep = asoc->ep;
  204. struct sctp_inithdr init;
  205. union sctp_params addrs;
  206. size_t chunksize;
  207. struct sctp_chunk *retval = NULL;
  208. int num_types, addrs_len = 0;
  209. struct sctp_sock *sp;
  210. struct sctp_supported_addrs_param sat;
  211. __be16 types[2];
  212. struct sctp_adaptation_ind_param aiparam;
  213. struct sctp_supported_ext_param ext_param;
  214. int num_ext = 0;
  215. __u8 extensions[4];
  216. struct sctp_paramhdr *auth_chunks = NULL,
  217. *auth_hmacs = NULL;
  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. retval = NULL;
  224. /* Convert the provided bind address list to raw format. */
  225. addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
  226. init.init_tag = htonl(asoc->c.my_vtag);
  227. init.a_rwnd = htonl(asoc->rwnd);
  228. init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
  229. init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
  230. init.initial_tsn = htonl(asoc->c.initial_tsn);
  231. /* How many address types are needed? */
  232. sp = sctp_sk(asoc->base.sk);
  233. num_types = sp->pf->supported_addrs(sp, types);
  234. chunksize = sizeof(init) + addrs_len;
  235. chunksize += SCTP_PAD4(SCTP_SAT_LEN(num_types));
  236. chunksize += sizeof(ecap_param);
  237. if (asoc->prsctp_enable)
  238. chunksize += sizeof(prsctp_param);
  239. /* ADDIP: Section 4.2.7:
  240. * An implementation supporting this extension [ADDIP] MUST list
  241. * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
  242. * INIT-ACK parameters.
  243. */
  244. if (net->sctp.addip_enable) {
  245. extensions[num_ext] = SCTP_CID_ASCONF;
  246. extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
  247. num_ext += 2;
  248. }
  249. if (asoc->reconf_enable) {
  250. extensions[num_ext] = SCTP_CID_RECONF;
  251. num_ext += 1;
  252. }
  253. if (sp->adaptation_ind)
  254. chunksize += sizeof(aiparam);
  255. chunksize += vparam_len;
  256. /* Account for AUTH related parameters */
  257. if (ep->auth_enable) {
  258. /* Add random parameter length*/
  259. chunksize += sizeof(asoc->c.auth_random);
  260. /* Add HMACS parameter length if any were defined */
  261. auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
  262. if (auth_hmacs->length)
  263. chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
  264. else
  265. auth_hmacs = NULL;
  266. /* Add CHUNKS parameter length */
  267. auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
  268. if (auth_chunks->length)
  269. chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
  270. else
  271. auth_chunks = NULL;
  272. extensions[num_ext] = SCTP_CID_AUTH;
  273. num_ext += 1;
  274. }
  275. /* If we have any extensions to report, account for that */
  276. if (num_ext)
  277. chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
  278. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  279. *
  280. * Note 3: An INIT chunk MUST NOT contain more than one Host
  281. * Name address parameter. Moreover, the sender of the INIT
  282. * MUST NOT combine any other address types with the Host Name
  283. * address in the INIT. The receiver of INIT MUST ignore any
  284. * other address types if the Host Name address parameter is
  285. * present in the received INIT chunk.
  286. *
  287. * PLEASE DO NOT FIXME [This version does not support Host Name.]
  288. */
  289. retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
  290. if (!retval)
  291. goto nodata;
  292. retval->subh.init_hdr =
  293. sctp_addto_chunk(retval, sizeof(init), &init);
  294. retval->param_hdr.v =
  295. sctp_addto_chunk(retval, addrs_len, addrs.v);
  296. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  297. *
  298. * Note 4: This parameter, when present, specifies all the
  299. * address types the sending endpoint can support. The absence
  300. * of this parameter indicates that the sending endpoint can
  301. * support any address type.
  302. */
  303. sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
  304. sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
  305. sctp_addto_chunk(retval, sizeof(sat), &sat);
  306. sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
  307. sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
  308. /* Add the supported extensions parameter. Be nice and add this
  309. * fist before addiding the parameters for the extensions themselves
  310. */
  311. if (num_ext) {
  312. ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
  313. ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
  314. sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
  315. sctp_addto_param(retval, num_ext, extensions);
  316. }
  317. if (asoc->prsctp_enable)
  318. sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
  319. if (sp->adaptation_ind) {
  320. aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
  321. aiparam.param_hdr.length = htons(sizeof(aiparam));
  322. aiparam.adaptation_ind = htonl(sp->adaptation_ind);
  323. sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
  324. }
  325. /* Add SCTP-AUTH chunks to the parameter list */
  326. if (ep->auth_enable) {
  327. sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
  328. asoc->c.auth_random);
  329. if (auth_hmacs)
  330. sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
  331. auth_hmacs);
  332. if (auth_chunks)
  333. sctp_addto_chunk(retval, ntohs(auth_chunks->length),
  334. auth_chunks);
  335. }
  336. nodata:
  337. kfree(addrs.v);
  338. return retval;
  339. }
  340. struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
  341. const struct sctp_chunk *chunk,
  342. gfp_t gfp, int unkparam_len)
  343. {
  344. struct sctp_inithdr initack;
  345. struct sctp_chunk *retval;
  346. union sctp_params addrs;
  347. struct sctp_sock *sp;
  348. int addrs_len;
  349. struct sctp_cookie_param *cookie;
  350. int cookie_len;
  351. size_t chunksize;
  352. struct sctp_adaptation_ind_param aiparam;
  353. struct sctp_supported_ext_param ext_param;
  354. int num_ext = 0;
  355. __u8 extensions[4];
  356. struct sctp_paramhdr *auth_chunks = NULL,
  357. *auth_hmacs = NULL,
  358. *auth_random = NULL;
  359. retval = NULL;
  360. /* Note: there may be no addresses to embed. */
  361. addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
  362. initack.init_tag = htonl(asoc->c.my_vtag);
  363. initack.a_rwnd = htonl(asoc->rwnd);
  364. initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
  365. initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
  366. initack.initial_tsn = htonl(asoc->c.initial_tsn);
  367. /* FIXME: We really ought to build the cookie right
  368. * into the packet instead of allocating more fresh memory.
  369. */
  370. cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
  371. addrs.v, addrs_len);
  372. if (!cookie)
  373. goto nomem_cookie;
  374. /* Calculate the total size of allocation, include the reserved
  375. * space for reporting unknown parameters if it is specified.
  376. */
  377. sp = sctp_sk(asoc->base.sk);
  378. chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
  379. /* Tell peer that we'll do ECN only if peer advertised such cap. */
  380. if (asoc->peer.ecn_capable)
  381. chunksize += sizeof(ecap_param);
  382. if (asoc->peer.prsctp_capable)
  383. chunksize += sizeof(prsctp_param);
  384. if (asoc->peer.asconf_capable) {
  385. extensions[num_ext] = SCTP_CID_ASCONF;
  386. extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
  387. num_ext += 2;
  388. }
  389. if (asoc->peer.reconf_capable) {
  390. extensions[num_ext] = SCTP_CID_RECONF;
  391. num_ext += 1;
  392. }
  393. if (sp->adaptation_ind)
  394. chunksize += sizeof(aiparam);
  395. if (asoc->peer.auth_capable) {
  396. auth_random = (struct sctp_paramhdr *)asoc->c.auth_random;
  397. chunksize += ntohs(auth_random->length);
  398. auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
  399. if (auth_hmacs->length)
  400. chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
  401. else
  402. auth_hmacs = NULL;
  403. auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
  404. if (auth_chunks->length)
  405. chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
  406. else
  407. auth_chunks = NULL;
  408. extensions[num_ext] = SCTP_CID_AUTH;
  409. num_ext += 1;
  410. }
  411. if (num_ext)
  412. chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
  413. /* Now allocate and fill out the chunk. */
  414. retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
  415. if (!retval)
  416. goto nomem_chunk;
  417. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  418. *
  419. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  420. * HEARTBEAT ACK, * etc.) to the same destination transport
  421. * address from which it received the DATA or control chunk
  422. * to which it is replying.
  423. *
  424. * [INIT ACK back to where the INIT came from.]
  425. */
  426. retval->transport = chunk->transport;
  427. retval->subh.init_hdr =
  428. sctp_addto_chunk(retval, sizeof(initack), &initack);
  429. retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
  430. sctp_addto_chunk(retval, cookie_len, cookie);
  431. if (asoc->peer.ecn_capable)
  432. sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
  433. if (num_ext) {
  434. ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
  435. ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
  436. sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
  437. sctp_addto_param(retval, num_ext, extensions);
  438. }
  439. if (asoc->peer.prsctp_capable)
  440. sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
  441. if (sp->adaptation_ind) {
  442. aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
  443. aiparam.param_hdr.length = htons(sizeof(aiparam));
  444. aiparam.adaptation_ind = htonl(sp->adaptation_ind);
  445. sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
  446. }
  447. if (asoc->peer.auth_capable) {
  448. sctp_addto_chunk(retval, ntohs(auth_random->length),
  449. auth_random);
  450. if (auth_hmacs)
  451. sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
  452. auth_hmacs);
  453. if (auth_chunks)
  454. sctp_addto_chunk(retval, ntohs(auth_chunks->length),
  455. auth_chunks);
  456. }
  457. /* We need to remove the const qualifier at this point. */
  458. retval->asoc = (struct sctp_association *) asoc;
  459. nomem_chunk:
  460. kfree(cookie);
  461. nomem_cookie:
  462. kfree(addrs.v);
  463. return retval;
  464. }
  465. /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
  466. *
  467. * This chunk is used only during the initialization of an association.
  468. * It is sent by the initiator of an association to its peer to complete
  469. * the initialization process. This chunk MUST precede any DATA chunk
  470. * sent within the association, but MAY be bundled with one or more DATA
  471. * chunks in the same packet.
  472. *
  473. * 0 1 2 3
  474. * 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
  475. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  476. * | Type = 10 |Chunk Flags | Length |
  477. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  478. * / Cookie /
  479. * \ \
  480. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  481. *
  482. * Chunk Flags: 8 bit
  483. *
  484. * Set to zero on transmit and ignored on receipt.
  485. *
  486. * Length: 16 bits (unsigned integer)
  487. *
  488. * Set to the size of the chunk in bytes, including the 4 bytes of
  489. * the chunk header and the size of the Cookie.
  490. *
  491. * Cookie: variable size
  492. *
  493. * This field must contain the exact cookie received in the
  494. * State Cookie parameter from the previous INIT ACK.
  495. *
  496. * An implementation SHOULD make the cookie as small as possible
  497. * to insure interoperability.
  498. */
  499. struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
  500. const struct sctp_chunk *chunk)
  501. {
  502. struct sctp_chunk *retval;
  503. void *cookie;
  504. int cookie_len;
  505. cookie = asoc->peer.cookie;
  506. cookie_len = asoc->peer.cookie_len;
  507. /* Build a cookie echo chunk. */
  508. retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
  509. cookie_len, GFP_ATOMIC);
  510. if (!retval)
  511. goto nodata;
  512. retval->subh.cookie_hdr =
  513. sctp_addto_chunk(retval, cookie_len, cookie);
  514. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  515. *
  516. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  517. * HEARTBEAT ACK, * etc.) to the same destination transport
  518. * address from which it * received the DATA or control chunk
  519. * to which it is replying.
  520. *
  521. * [COOKIE ECHO back to where the INIT ACK came from.]
  522. */
  523. if (chunk)
  524. retval->transport = chunk->transport;
  525. nodata:
  526. return retval;
  527. }
  528. /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
  529. *
  530. * This chunk is used only during the initialization of an
  531. * association. It is used to acknowledge the receipt of a COOKIE
  532. * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
  533. * within the association, but MAY be bundled with one or more DATA
  534. * chunks or SACK chunk in the same SCTP packet.
  535. *
  536. * 0 1 2 3
  537. * 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
  538. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  539. * | Type = 11 |Chunk Flags | Length = 4 |
  540. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  541. *
  542. * Chunk Flags: 8 bits
  543. *
  544. * Set to zero on transmit and ignored on receipt.
  545. */
  546. struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
  547. const struct sctp_chunk *chunk)
  548. {
  549. struct sctp_chunk *retval;
  550. retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
  551. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  552. *
  553. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  554. * HEARTBEAT ACK, * etc.) to the same destination transport
  555. * address from which it * received the DATA or control chunk
  556. * to which it is replying.
  557. *
  558. * [COOKIE ACK back to where the COOKIE ECHO came from.]
  559. */
  560. if (retval && chunk)
  561. retval->transport = chunk->transport;
  562. return retval;
  563. }
  564. /*
  565. * Appendix A: Explicit Congestion Notification:
  566. * CWR:
  567. *
  568. * RFC 2481 details a specific bit for a sender to send in the header of
  569. * its next outbound TCP segment to indicate to its peer that it has
  570. * reduced its congestion window. This is termed the CWR bit. For
  571. * SCTP the same indication is made by including the CWR chunk.
  572. * This chunk contains one data element, i.e. the TSN number that
  573. * was sent in the ECNE chunk. This element represents the lowest
  574. * TSN number in the datagram that was originally marked with the
  575. * CE bit.
  576. *
  577. * 0 1 2 3
  578. * 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
  579. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  580. * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
  581. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  582. * | Lowest TSN Number |
  583. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  584. *
  585. * Note: The CWR is considered a Control chunk.
  586. */
  587. struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
  588. const __u32 lowest_tsn,
  589. const struct sctp_chunk *chunk)
  590. {
  591. struct sctp_chunk *retval;
  592. sctp_cwrhdr_t cwr;
  593. cwr.lowest_tsn = htonl(lowest_tsn);
  594. retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
  595. sizeof(sctp_cwrhdr_t), GFP_ATOMIC);
  596. if (!retval)
  597. goto nodata;
  598. retval->subh.ecn_cwr_hdr =
  599. sctp_addto_chunk(retval, sizeof(cwr), &cwr);
  600. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  601. *
  602. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  603. * HEARTBEAT ACK, * etc.) to the same destination transport
  604. * address from which it * received the DATA or control chunk
  605. * to which it is replying.
  606. *
  607. * [Report a reduced congestion window back to where the ECNE
  608. * came from.]
  609. */
  610. if (chunk)
  611. retval->transport = chunk->transport;
  612. nodata:
  613. return retval;
  614. }
  615. /* Make an ECNE chunk. This is a congestion experienced report. */
  616. struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
  617. const __u32 lowest_tsn)
  618. {
  619. struct sctp_chunk *retval;
  620. sctp_ecnehdr_t ecne;
  621. ecne.lowest_tsn = htonl(lowest_tsn);
  622. retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
  623. sizeof(sctp_ecnehdr_t), GFP_ATOMIC);
  624. if (!retval)
  625. goto nodata;
  626. retval->subh.ecne_hdr =
  627. sctp_addto_chunk(retval, sizeof(ecne), &ecne);
  628. nodata:
  629. return retval;
  630. }
  631. /* Make a DATA chunk for the given association from the provided
  632. * parameters. However, do not populate the data payload.
  633. */
  634. struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
  635. const struct sctp_sndrcvinfo *sinfo,
  636. int data_len, __u8 flags, __u16 ssn,
  637. gfp_t gfp)
  638. {
  639. struct sctp_chunk *retval;
  640. struct sctp_datahdr dp;
  641. int chunk_len;
  642. /* We assign the TSN as LATE as possible, not here when
  643. * creating the chunk.
  644. */
  645. dp.tsn = 0;
  646. dp.stream = htons(sinfo->sinfo_stream);
  647. dp.ppid = sinfo->sinfo_ppid;
  648. /* Set the flags for an unordered send. */
  649. if (sinfo->sinfo_flags & SCTP_UNORDERED) {
  650. flags |= SCTP_DATA_UNORDERED;
  651. dp.ssn = 0;
  652. } else
  653. dp.ssn = htons(ssn);
  654. chunk_len = sizeof(dp) + data_len;
  655. retval = sctp_make_data(asoc, flags, chunk_len, gfp);
  656. if (!retval)
  657. goto nodata;
  658. retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
  659. memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
  660. nodata:
  661. return retval;
  662. }
  663. /* Create a selective ackowledgement (SACK) for the given
  664. * association. This reports on which TSN's we've seen to date,
  665. * including duplicates and gaps.
  666. */
  667. struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
  668. {
  669. struct sctp_chunk *retval;
  670. struct sctp_sackhdr sack;
  671. int len;
  672. __u32 ctsn;
  673. __u16 num_gabs, num_dup_tsns;
  674. struct sctp_association *aptr = (struct sctp_association *)asoc;
  675. struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
  676. struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
  677. struct sctp_transport *trans;
  678. memset(gabs, 0, sizeof(gabs));
  679. ctsn = sctp_tsnmap_get_ctsn(map);
  680. pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
  681. /* How much room is needed in the chunk? */
  682. num_gabs = sctp_tsnmap_num_gabs(map, gabs);
  683. num_dup_tsns = sctp_tsnmap_num_dups(map);
  684. /* Initialize the SACK header. */
  685. sack.cum_tsn_ack = htonl(ctsn);
  686. sack.a_rwnd = htonl(asoc->a_rwnd);
  687. sack.num_gap_ack_blocks = htons(num_gabs);
  688. sack.num_dup_tsns = htons(num_dup_tsns);
  689. len = sizeof(sack)
  690. + sizeof(struct sctp_gap_ack_block) * num_gabs
  691. + sizeof(__u32) * num_dup_tsns;
  692. /* Create the chunk. */
  693. retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
  694. if (!retval)
  695. goto nodata;
  696. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  697. *
  698. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  699. * HEARTBEAT ACK, etc.) to the same destination transport
  700. * address from which it received the DATA or control chunk to
  701. * which it is replying. This rule should also be followed if
  702. * the endpoint is bundling DATA chunks together with the
  703. * reply chunk.
  704. *
  705. * However, when acknowledging multiple DATA chunks received
  706. * in packets from different source addresses in a single
  707. * SACK, the SACK chunk may be transmitted to one of the
  708. * destination transport addresses from which the DATA or
  709. * control chunks being acknowledged were received.
  710. *
  711. * [BUG: We do not implement the following paragraph.
  712. * Perhaps we should remember the last transport we used for a
  713. * SACK and avoid that (if possible) if we have seen any
  714. * duplicates. --piggy]
  715. *
  716. * When a receiver of a duplicate DATA chunk sends a SACK to a
  717. * multi- homed endpoint it MAY be beneficial to vary the
  718. * destination address and not use the source address of the
  719. * DATA chunk. The reason being that receiving a duplicate
  720. * from a multi-homed endpoint might indicate that the return
  721. * path (as specified in the source address of the DATA chunk)
  722. * for the SACK is broken.
  723. *
  724. * [Send to the address from which we last received a DATA chunk.]
  725. */
  726. retval->transport = asoc->peer.last_data_from;
  727. retval->subh.sack_hdr =
  728. sctp_addto_chunk(retval, sizeof(sack), &sack);
  729. /* Add the gap ack block information. */
  730. if (num_gabs)
  731. sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
  732. gabs);
  733. /* Add the duplicate TSN information. */
  734. if (num_dup_tsns) {
  735. aptr->stats.idupchunks += num_dup_tsns;
  736. sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
  737. sctp_tsnmap_get_dups(map));
  738. }
  739. /* Once we have a sack generated, check to see what our sack
  740. * generation is, if its 0, reset the transports to 0, and reset
  741. * the association generation to 1
  742. *
  743. * The idea is that zero is never used as a valid generation for the
  744. * association so no transport will match after a wrap event like this,
  745. * Until the next sack
  746. */
  747. if (++aptr->peer.sack_generation == 0) {
  748. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  749. transports)
  750. trans->sack_generation = 0;
  751. aptr->peer.sack_generation = 1;
  752. }
  753. nodata:
  754. return retval;
  755. }
  756. /* Make a SHUTDOWN chunk. */
  757. struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
  758. const struct sctp_chunk *chunk)
  759. {
  760. struct sctp_shutdownhdr shut;
  761. struct sctp_chunk *retval;
  762. __u32 ctsn;
  763. ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  764. shut.cum_tsn_ack = htonl(ctsn);
  765. retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
  766. sizeof(shut), GFP_ATOMIC);
  767. if (!retval)
  768. goto nodata;
  769. retval->subh.shutdown_hdr =
  770. sctp_addto_chunk(retval, sizeof(shut), &shut);
  771. if (chunk)
  772. retval->transport = chunk->transport;
  773. nodata:
  774. return retval;
  775. }
  776. struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
  777. const struct sctp_chunk *chunk)
  778. {
  779. struct sctp_chunk *retval;
  780. retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
  781. GFP_ATOMIC);
  782. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  783. *
  784. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  785. * HEARTBEAT ACK, * etc.) to the same destination transport
  786. * address from which it * received the DATA or control chunk
  787. * to which it is replying.
  788. *
  789. * [ACK back to where the SHUTDOWN came from.]
  790. */
  791. if (retval && chunk)
  792. retval->transport = chunk->transport;
  793. return retval;
  794. }
  795. struct sctp_chunk *sctp_make_shutdown_complete(
  796. const struct sctp_association *asoc,
  797. const struct sctp_chunk *chunk)
  798. {
  799. struct sctp_chunk *retval;
  800. __u8 flags = 0;
  801. /* Set the T-bit if we have no association (vtag will be
  802. * reflected)
  803. */
  804. flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
  805. retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
  806. 0, GFP_ATOMIC);
  807. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  808. *
  809. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  810. * HEARTBEAT ACK, * etc.) to the same destination transport
  811. * address from which it * received the DATA or control chunk
  812. * to which it is replying.
  813. *
  814. * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
  815. * came from.]
  816. */
  817. if (retval && chunk)
  818. retval->transport = chunk->transport;
  819. return retval;
  820. }
  821. /* Create an ABORT. Note that we set the T bit if we have no
  822. * association, except when responding to an INIT (sctpimpguide 2.41).
  823. */
  824. struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
  825. const struct sctp_chunk *chunk,
  826. const size_t hint)
  827. {
  828. struct sctp_chunk *retval;
  829. __u8 flags = 0;
  830. /* Set the T-bit if we have no association and 'chunk' is not
  831. * an INIT (vtag will be reflected).
  832. */
  833. if (!asoc) {
  834. if (chunk && chunk->chunk_hdr &&
  835. chunk->chunk_hdr->type == SCTP_CID_INIT)
  836. flags = 0;
  837. else
  838. flags = SCTP_CHUNK_FLAG_T;
  839. }
  840. retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
  841. GFP_ATOMIC);
  842. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  843. *
  844. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  845. * HEARTBEAT ACK, * etc.) to the same destination transport
  846. * address from which it * received the DATA or control chunk
  847. * to which it is replying.
  848. *
  849. * [ABORT back to where the offender came from.]
  850. */
  851. if (retval && chunk)
  852. retval->transport = chunk->transport;
  853. return retval;
  854. }
  855. /* Helper to create ABORT with a NO_USER_DATA error. */
  856. struct sctp_chunk *sctp_make_abort_no_data(
  857. const struct sctp_association *asoc,
  858. const struct sctp_chunk *chunk, __u32 tsn)
  859. {
  860. struct sctp_chunk *retval;
  861. __be32 payload;
  862. retval = sctp_make_abort(asoc, chunk,
  863. sizeof(struct sctp_errhdr) + sizeof(tsn));
  864. if (!retval)
  865. goto no_mem;
  866. /* Put the tsn back into network byte order. */
  867. payload = htonl(tsn);
  868. sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
  869. sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
  870. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  871. *
  872. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  873. * HEARTBEAT ACK, * etc.) to the same destination transport
  874. * address from which it * received the DATA or control chunk
  875. * to which it is replying.
  876. *
  877. * [ABORT back to where the offender came from.]
  878. */
  879. if (chunk)
  880. retval->transport = chunk->transport;
  881. no_mem:
  882. return retval;
  883. }
  884. /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
  885. struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
  886. struct msghdr *msg,
  887. size_t paylen)
  888. {
  889. struct sctp_chunk *retval;
  890. void *payload = NULL;
  891. int err;
  892. retval = sctp_make_abort(asoc, NULL,
  893. sizeof(struct sctp_errhdr) + paylen);
  894. if (!retval)
  895. goto err_chunk;
  896. if (paylen) {
  897. /* Put the msg_iov together into payload. */
  898. payload = kmalloc(paylen, GFP_KERNEL);
  899. if (!payload)
  900. goto err_payload;
  901. err = memcpy_from_msg(payload, msg, paylen);
  902. if (err < 0)
  903. goto err_copy;
  904. }
  905. sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
  906. sctp_addto_chunk(retval, paylen, payload);
  907. if (paylen)
  908. kfree(payload);
  909. return retval;
  910. err_copy:
  911. kfree(payload);
  912. err_payload:
  913. sctp_chunk_free(retval);
  914. retval = NULL;
  915. err_chunk:
  916. return retval;
  917. }
  918. /* Append bytes to the end of a parameter. Will panic if chunk is not big
  919. * enough.
  920. */
  921. static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
  922. const void *data)
  923. {
  924. void *target;
  925. int chunklen = ntohs(chunk->chunk_hdr->length);
  926. target = skb_put(chunk->skb, len);
  927. if (data)
  928. memcpy(target, data, len);
  929. else
  930. memset(target, 0, len);
  931. /* Adjust the chunk length field. */
  932. chunk->chunk_hdr->length = htons(chunklen + len);
  933. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  934. return target;
  935. }
  936. /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
  937. struct sctp_chunk *sctp_make_abort_violation(
  938. const struct sctp_association *asoc,
  939. const struct sctp_chunk *chunk,
  940. const __u8 *payload,
  941. const size_t paylen)
  942. {
  943. struct sctp_chunk *retval;
  944. struct sctp_paramhdr phdr;
  945. retval = sctp_make_abort(asoc, chunk, sizeof(struct sctp_errhdr) +
  946. paylen + sizeof(phdr));
  947. if (!retval)
  948. goto end;
  949. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen +
  950. sizeof(phdr));
  951. phdr.type = htons(chunk->chunk_hdr->type);
  952. phdr.length = chunk->chunk_hdr->length;
  953. sctp_addto_chunk(retval, paylen, payload);
  954. sctp_addto_param(retval, sizeof(phdr), &phdr);
  955. end:
  956. return retval;
  957. }
  958. struct sctp_chunk *sctp_make_violation_paramlen(
  959. const struct sctp_association *asoc,
  960. const struct sctp_chunk *chunk,
  961. struct sctp_paramhdr *param)
  962. {
  963. struct sctp_chunk *retval;
  964. static const char error[] = "The following parameter had invalid length:";
  965. size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr) +
  966. sizeof(*param);
  967. retval = sctp_make_abort(asoc, chunk, payload_len);
  968. if (!retval)
  969. goto nodata;
  970. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
  971. sizeof(error) + sizeof(*param));
  972. sctp_addto_chunk(retval, sizeof(error), error);
  973. sctp_addto_param(retval, sizeof(*param), param);
  974. nodata:
  975. return retval;
  976. }
  977. struct sctp_chunk *sctp_make_violation_max_retrans(
  978. const struct sctp_association *asoc,
  979. const struct sctp_chunk *chunk)
  980. {
  981. struct sctp_chunk *retval;
  982. static const char error[] = "Association exceeded its max_retans count";
  983. size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr);
  984. retval = sctp_make_abort(asoc, chunk, payload_len);
  985. if (!retval)
  986. goto nodata;
  987. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
  988. sctp_addto_chunk(retval, sizeof(error), error);
  989. nodata:
  990. return retval;
  991. }
  992. /* Make a HEARTBEAT chunk. */
  993. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  994. const struct sctp_transport *transport)
  995. {
  996. struct sctp_chunk *retval;
  997. sctp_sender_hb_info_t hbinfo;
  998. retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
  999. sizeof(hbinfo), GFP_ATOMIC);
  1000. if (!retval)
  1001. goto nodata;
  1002. hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
  1003. hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
  1004. hbinfo.daddr = transport->ipaddr;
  1005. hbinfo.sent_at = jiffies;
  1006. hbinfo.hb_nonce = transport->hb_nonce;
  1007. /* Cast away the 'const', as this is just telling the chunk
  1008. * what transport it belongs to.
  1009. */
  1010. retval->transport = (struct sctp_transport *) transport;
  1011. retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
  1012. &hbinfo);
  1013. nodata:
  1014. return retval;
  1015. }
  1016. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  1017. const struct sctp_chunk *chunk,
  1018. const void *payload, 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_chunk *retval;
  1102. struct sctp_hmac *hmac_desc;
  1103. struct sctp_authhdr auth_hdr;
  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(sctp_authhdr_t),
  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(sctp_authhdr_t),
  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_chunk *retval;
  1187. struct sctp_chunkhdr *chunk_hdr;
  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. void *target;
  1264. int chunklen = ntohs(chunk->chunk_hdr->length);
  1265. int padlen = SCTP_PAD4(chunklen) - chunklen;
  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_datamsg *msg;
  1309. struct sctp_chunk *lchunk;
  1310. struct sctp_stream *stream;
  1311. __u16 ssn;
  1312. __u16 sid;
  1313. if (chunk->has_ssn)
  1314. return;
  1315. /* All fragments will be on the same stream */
  1316. sid = ntohs(chunk->subh.data_hdr->stream);
  1317. stream = &chunk->asoc->stream;
  1318. /* Now assign the sequence number to the entire message.
  1319. * All fragments must have the same stream sequence number.
  1320. */
  1321. msg = chunk->msg;
  1322. list_for_each_entry(lchunk, &msg->chunks, frag_list) {
  1323. if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  1324. ssn = 0;
  1325. } else {
  1326. if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
  1327. ssn = sctp_ssn_next(stream, out, sid);
  1328. else
  1329. ssn = sctp_ssn_peek(stream, out, sid);
  1330. }
  1331. lchunk->subh.data_hdr->ssn = htons(ssn);
  1332. lchunk->has_ssn = 1;
  1333. }
  1334. }
  1335. /* Helper function to assign a TSN if needed. This assumes that both
  1336. * the data_hdr and association have already been assigned.
  1337. */
  1338. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
  1339. {
  1340. if (!chunk->has_tsn) {
  1341. /* This is the last possible instant to
  1342. * assign a TSN.
  1343. */
  1344. chunk->subh.data_hdr->tsn =
  1345. htonl(sctp_association_get_next_tsn(chunk->asoc));
  1346. chunk->has_tsn = 1;
  1347. }
  1348. }
  1349. /* Create a CLOSED association to use with an incoming packet. */
  1350. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
  1351. struct sctp_chunk *chunk,
  1352. gfp_t gfp)
  1353. {
  1354. struct sctp_association *asoc;
  1355. struct sk_buff *skb;
  1356. sctp_scope_t scope;
  1357. /* Create the bare association. */
  1358. scope = sctp_scope(sctp_source(chunk));
  1359. asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1360. if (!asoc)
  1361. goto nodata;
  1362. asoc->temp = 1;
  1363. skb = chunk->skb;
  1364. /* Create an entry for the source address of the packet. */
  1365. SCTP_INPUT_CB(skb)->af->from_skb(&asoc->c.peer_addr, skb, 1);
  1366. nodata:
  1367. return asoc;
  1368. }
  1369. /* Build a cookie representing asoc.
  1370. * This INCLUDES the param header needed to put the cookie in the INIT ACK.
  1371. */
  1372. static struct sctp_cookie_param *sctp_pack_cookie(
  1373. const struct sctp_endpoint *ep,
  1374. const struct sctp_association *asoc,
  1375. const struct sctp_chunk *init_chunk,
  1376. int *cookie_len,
  1377. const __u8 *raw_addrs, int addrs_len)
  1378. {
  1379. struct sctp_signed_cookie *cookie;
  1380. struct sctp_cookie_param *retval;
  1381. int headersize, bodysize;
  1382. /* Header size is static data prior to the actual cookie, including
  1383. * any padding.
  1384. */
  1385. headersize = sizeof(struct sctp_paramhdr) +
  1386. (sizeof(struct sctp_signed_cookie) -
  1387. sizeof(struct sctp_cookie));
  1388. bodysize = sizeof(struct sctp_cookie)
  1389. + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
  1390. /* Pad out the cookie to a multiple to make the signature
  1391. * functions simpler to write.
  1392. */
  1393. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1394. bodysize += SCTP_COOKIE_MULTIPLE
  1395. - (bodysize % SCTP_COOKIE_MULTIPLE);
  1396. *cookie_len = headersize + bodysize;
  1397. /* Clear this memory since we are sending this data structure
  1398. * out on the network.
  1399. */
  1400. retval = kzalloc(*cookie_len, GFP_ATOMIC);
  1401. if (!retval)
  1402. goto nodata;
  1403. cookie = (struct sctp_signed_cookie *) retval->body;
  1404. /* Set up the parameter header. */
  1405. retval->p.type = SCTP_PARAM_STATE_COOKIE;
  1406. retval->p.length = htons(*cookie_len);
  1407. /* Copy the cookie part of the association itself. */
  1408. cookie->c = asoc->c;
  1409. /* Save the raw address list length in the cookie. */
  1410. cookie->c.raw_addr_list_len = addrs_len;
  1411. /* Remember PR-SCTP capability. */
  1412. cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
  1413. /* Save adaptation indication in the cookie. */
  1414. cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
  1415. /* Set an expiration time for the cookie. */
  1416. cookie->c.expiration = ktime_add(asoc->cookie_life,
  1417. ktime_get_real());
  1418. /* Copy the peer's init packet. */
  1419. memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
  1420. ntohs(init_chunk->chunk_hdr->length));
  1421. /* Copy the raw local address list of the association. */
  1422. memcpy((__u8 *)&cookie->c.peer_init[0] +
  1423. ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
  1424. if (sctp_sk(ep->base.sk)->hmac) {
  1425. SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
  1426. int err;
  1427. /* Sign the message. */
  1428. desc->tfm = sctp_sk(ep->base.sk)->hmac;
  1429. desc->flags = 0;
  1430. err = crypto_shash_setkey(desc->tfm, ep->secret_key,
  1431. sizeof(ep->secret_key)) ?:
  1432. crypto_shash_digest(desc, (u8 *)&cookie->c, bodysize,
  1433. cookie->signature);
  1434. shash_desc_zero(desc);
  1435. if (err)
  1436. goto free_cookie;
  1437. }
  1438. return retval;
  1439. free_cookie:
  1440. kfree(retval);
  1441. nodata:
  1442. *cookie_len = 0;
  1443. return NULL;
  1444. }
  1445. /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
  1446. struct sctp_association *sctp_unpack_cookie(
  1447. const struct sctp_endpoint *ep,
  1448. const struct sctp_association *asoc,
  1449. struct sctp_chunk *chunk, gfp_t gfp,
  1450. int *error, struct sctp_chunk **errp)
  1451. {
  1452. struct sctp_association *retval = NULL;
  1453. struct sctp_signed_cookie *cookie;
  1454. struct sctp_cookie *bear_cookie;
  1455. int headersize, bodysize, fixed_size;
  1456. __u8 *digest = ep->digest;
  1457. unsigned int len;
  1458. sctp_scope_t scope;
  1459. struct sk_buff *skb = chunk->skb;
  1460. ktime_t kt;
  1461. /* Header size is static data prior to the actual cookie, including
  1462. * any padding.
  1463. */
  1464. headersize = sizeof(struct sctp_chunkhdr) +
  1465. (sizeof(struct sctp_signed_cookie) -
  1466. sizeof(struct sctp_cookie));
  1467. bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
  1468. fixed_size = headersize + sizeof(struct sctp_cookie);
  1469. /* Verify that the chunk looks like it even has a cookie.
  1470. * There must be enough room for our cookie and our peer's
  1471. * INIT chunk.
  1472. */
  1473. len = ntohs(chunk->chunk_hdr->length);
  1474. if (len < fixed_size + sizeof(struct sctp_chunkhdr))
  1475. goto malformed;
  1476. /* Verify that the cookie has been padded out. */
  1477. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1478. goto malformed;
  1479. /* Process the cookie. */
  1480. cookie = chunk->subh.cookie_hdr;
  1481. bear_cookie = &cookie->c;
  1482. if (!sctp_sk(ep->base.sk)->hmac)
  1483. goto no_hmac;
  1484. /* Check the signature. */
  1485. {
  1486. SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
  1487. int err;
  1488. desc->tfm = sctp_sk(ep->base.sk)->hmac;
  1489. desc->flags = 0;
  1490. err = crypto_shash_setkey(desc->tfm, ep->secret_key,
  1491. sizeof(ep->secret_key)) ?:
  1492. crypto_shash_digest(desc, (u8 *)bear_cookie, bodysize,
  1493. digest);
  1494. shash_desc_zero(desc);
  1495. if (err) {
  1496. *error = -SCTP_IERROR_NOMEM;
  1497. goto fail;
  1498. }
  1499. }
  1500. if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
  1501. *error = -SCTP_IERROR_BAD_SIG;
  1502. goto fail;
  1503. }
  1504. no_hmac:
  1505. /* IG Section 2.35.2:
  1506. * 3) Compare the port numbers and the verification tag contained
  1507. * within the COOKIE ECHO chunk to the actual port numbers and the
  1508. * verification tag within the SCTP common header of the received
  1509. * packet. If these values do not match the packet MUST be silently
  1510. * discarded,
  1511. */
  1512. if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
  1513. *error = -SCTP_IERROR_BAD_TAG;
  1514. goto fail;
  1515. }
  1516. if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
  1517. ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
  1518. *error = -SCTP_IERROR_BAD_PORTS;
  1519. goto fail;
  1520. }
  1521. /* Check to see if the cookie is stale. If there is already
  1522. * an association, there is no need to check cookie's expiration
  1523. * for init collision case of lost COOKIE ACK.
  1524. * If skb has been timestamped, then use the stamp, otherwise
  1525. * use current time. This introduces a small possibility that
  1526. * that a cookie may be considered expired, but his would only slow
  1527. * down the new association establishment instead of every packet.
  1528. */
  1529. if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
  1530. kt = skb_get_ktime(skb);
  1531. else
  1532. kt = ktime_get_real();
  1533. if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
  1534. /*
  1535. * Section 3.3.10.3 Stale Cookie Error (3)
  1536. *
  1537. * Cause of error
  1538. * ---------------
  1539. * Stale Cookie Error: Indicates the receipt of a valid State
  1540. * Cookie that has expired.
  1541. */
  1542. len = ntohs(chunk->chunk_hdr->length);
  1543. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1544. if (*errp) {
  1545. suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
  1546. __be32 n = htonl(usecs);
  1547. sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
  1548. sizeof(n));
  1549. sctp_addto_chunk(*errp, sizeof(n), &n);
  1550. *error = -SCTP_IERROR_STALE_COOKIE;
  1551. } else
  1552. *error = -SCTP_IERROR_NOMEM;
  1553. goto fail;
  1554. }
  1555. /* Make a new base association. */
  1556. scope = sctp_scope(sctp_source(chunk));
  1557. retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1558. if (!retval) {
  1559. *error = -SCTP_IERROR_NOMEM;
  1560. goto fail;
  1561. }
  1562. /* Set up our peer's port number. */
  1563. retval->peer.port = ntohs(chunk->sctp_hdr->source);
  1564. /* Populate the association from the cookie. */
  1565. memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
  1566. if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
  1567. GFP_ATOMIC) < 0) {
  1568. *error = -SCTP_IERROR_NOMEM;
  1569. goto fail;
  1570. }
  1571. /* Also, add the destination address. */
  1572. if (list_empty(&retval->base.bind_addr.address_list)) {
  1573. sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
  1574. sizeof(chunk->dest), SCTP_ADDR_SRC,
  1575. GFP_ATOMIC);
  1576. }
  1577. retval->next_tsn = retval->c.initial_tsn;
  1578. retval->ctsn_ack_point = retval->next_tsn - 1;
  1579. retval->addip_serial = retval->c.initial_tsn;
  1580. retval->strreset_outseq = retval->c.initial_tsn;
  1581. retval->adv_peer_ack_point = retval->ctsn_ack_point;
  1582. retval->peer.prsctp_capable = retval->c.prsctp_capable;
  1583. retval->peer.adaptation_ind = retval->c.adaptation_ind;
  1584. /* The INIT stuff will be done by the side effects. */
  1585. return retval;
  1586. fail:
  1587. if (retval)
  1588. sctp_association_free(retval);
  1589. return NULL;
  1590. malformed:
  1591. /* Yikes! The packet is either corrupt or deliberately
  1592. * malformed.
  1593. */
  1594. *error = -SCTP_IERROR_MALFORMED;
  1595. goto fail;
  1596. }
  1597. /********************************************************************
  1598. * 3rd Level Abstractions
  1599. ********************************************************************/
  1600. struct __sctp_missing {
  1601. __be32 num_missing;
  1602. __be16 type;
  1603. } __packed;
  1604. /*
  1605. * Report a missing mandatory parameter.
  1606. */
  1607. static int sctp_process_missing_param(const struct sctp_association *asoc,
  1608. enum sctp_param paramtype,
  1609. struct sctp_chunk *chunk,
  1610. struct sctp_chunk **errp)
  1611. {
  1612. struct __sctp_missing report;
  1613. __u16 len;
  1614. len = SCTP_PAD4(sizeof(report));
  1615. /* Make an ERROR chunk, preparing enough room for
  1616. * returning multiple unknown parameters.
  1617. */
  1618. if (!*errp)
  1619. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1620. if (*errp) {
  1621. report.num_missing = htonl(1);
  1622. report.type = paramtype;
  1623. sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
  1624. sizeof(report));
  1625. sctp_addto_chunk(*errp, sizeof(report), &report);
  1626. }
  1627. /* Stop processing this chunk. */
  1628. return 0;
  1629. }
  1630. /* Report an Invalid Mandatory Parameter. */
  1631. static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
  1632. struct sctp_chunk *chunk,
  1633. struct sctp_chunk **errp)
  1634. {
  1635. /* Invalid Mandatory Parameter Error has no payload. */
  1636. if (!*errp)
  1637. *errp = sctp_make_op_error_space(asoc, chunk, 0);
  1638. if (*errp)
  1639. sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
  1640. /* Stop processing this chunk. */
  1641. return 0;
  1642. }
  1643. static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
  1644. struct sctp_paramhdr *param,
  1645. const struct sctp_chunk *chunk,
  1646. struct sctp_chunk **errp)
  1647. {
  1648. /* This is a fatal error. Any accumulated non-fatal errors are
  1649. * not reported.
  1650. */
  1651. if (*errp)
  1652. sctp_chunk_free(*errp);
  1653. /* Create an error chunk and fill it in with our payload. */
  1654. *errp = sctp_make_violation_paramlen(asoc, chunk, param);
  1655. return 0;
  1656. }
  1657. /* Do not attempt to handle the HOST_NAME parm. However, do
  1658. * send back an indicator to the peer.
  1659. */
  1660. static int sctp_process_hn_param(const struct sctp_association *asoc,
  1661. union sctp_params param,
  1662. struct sctp_chunk *chunk,
  1663. struct sctp_chunk **errp)
  1664. {
  1665. __u16 len = ntohs(param.p->length);
  1666. /* Processing of the HOST_NAME parameter will generate an
  1667. * ABORT. If we've accumulated any non-fatal errors, they
  1668. * would be unrecognized parameters and we should not include
  1669. * them in the ABORT.
  1670. */
  1671. if (*errp)
  1672. sctp_chunk_free(*errp);
  1673. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1674. if (*errp) {
  1675. sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
  1676. sctp_addto_chunk(*errp, len, param.v);
  1677. }
  1678. /* Stop processing this chunk. */
  1679. return 0;
  1680. }
  1681. static int sctp_verify_ext_param(struct net *net, union sctp_params param)
  1682. {
  1683. __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  1684. int have_auth = 0;
  1685. int have_asconf = 0;
  1686. int i;
  1687. for (i = 0; i < num_ext; i++) {
  1688. switch (param.ext->chunks[i]) {
  1689. case SCTP_CID_AUTH:
  1690. have_auth = 1;
  1691. break;
  1692. case SCTP_CID_ASCONF:
  1693. case SCTP_CID_ASCONF_ACK:
  1694. have_asconf = 1;
  1695. break;
  1696. }
  1697. }
  1698. /* ADD-IP Security: The draft requires us to ABORT or ignore the
  1699. * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
  1700. * only if ADD-IP is turned on and we are not backward-compatible
  1701. * mode.
  1702. */
  1703. if (net->sctp.addip_noauth)
  1704. return 1;
  1705. if (net->sctp.addip_enable && !have_auth && have_asconf)
  1706. return 0;
  1707. return 1;
  1708. }
  1709. static void sctp_process_ext_param(struct sctp_association *asoc,
  1710. union sctp_params param)
  1711. {
  1712. struct net *net = sock_net(asoc->base.sk);
  1713. __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  1714. int i;
  1715. for (i = 0; i < num_ext; i++) {
  1716. switch (param.ext->chunks[i]) {
  1717. case SCTP_CID_RECONF:
  1718. if (asoc->reconf_enable &&
  1719. !asoc->peer.reconf_capable)
  1720. asoc->peer.reconf_capable = 1;
  1721. break;
  1722. case SCTP_CID_FWD_TSN:
  1723. if (asoc->prsctp_enable && !asoc->peer.prsctp_capable)
  1724. asoc->peer.prsctp_capable = 1;
  1725. break;
  1726. case SCTP_CID_AUTH:
  1727. /* if the peer reports AUTH, assume that he
  1728. * supports AUTH.
  1729. */
  1730. if (asoc->ep->auth_enable)
  1731. asoc->peer.auth_capable = 1;
  1732. break;
  1733. case SCTP_CID_ASCONF:
  1734. case SCTP_CID_ASCONF_ACK:
  1735. if (net->sctp.addip_enable)
  1736. asoc->peer.asconf_capable = 1;
  1737. break;
  1738. default:
  1739. break;
  1740. }
  1741. }
  1742. }
  1743. /* RFC 3.2.1 & the Implementers Guide 2.2.
  1744. *
  1745. * The Parameter Types are encoded such that the
  1746. * highest-order two bits specify the action that must be
  1747. * taken if the processing endpoint does not recognize the
  1748. * Parameter Type.
  1749. *
  1750. * 00 - Stop processing this parameter; do not process any further
  1751. * parameters within this chunk
  1752. *
  1753. * 01 - Stop processing this parameter, do not process any further
  1754. * parameters within this chunk, and report the unrecognized
  1755. * parameter in an 'Unrecognized Parameter' ERROR chunk.
  1756. *
  1757. * 10 - Skip this parameter and continue processing.
  1758. *
  1759. * 11 - Skip this parameter and continue processing but
  1760. * report the unrecognized parameter in an
  1761. * 'Unrecognized Parameter' ERROR chunk.
  1762. *
  1763. * Return value:
  1764. * SCTP_IERROR_NO_ERROR - continue with the chunk
  1765. * SCTP_IERROR_ERROR - stop and report an error.
  1766. * SCTP_IERROR_NOMEME - out of memory.
  1767. */
  1768. static sctp_ierror_t sctp_process_unk_param(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 sctp_ierror_t 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. union sctp_params param;
  1983. struct sctp_transport *transport;
  1984. struct list_head *pos, *temp;
  1985. struct sctp_af *af;
  1986. union sctp_addr addr;
  1987. char *cookie;
  1988. int src_match = 0;
  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. union sctp_addr addr;
  2147. int i;
  2148. __u16 sat;
  2149. int retval = 1;
  2150. sctp_scope_t scope;
  2151. u32 stale;
  2152. struct sctp_af *af;
  2153. union sctp_addr_param *addr_param;
  2154. struct sctp_transport *t;
  2155. struct sctp_endpoint *ep = asoc->ep;
  2156. /* We maintain all INIT parameters in network byte order all the
  2157. * time. This allows us to not worry about whether the parameters
  2158. * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
  2159. */
  2160. switch (param.p->type) {
  2161. case SCTP_PARAM_IPV6_ADDRESS:
  2162. if (PF_INET6 != asoc->base.sk->sk_family)
  2163. break;
  2164. goto do_addr_param;
  2165. case SCTP_PARAM_IPV4_ADDRESS:
  2166. /* v4 addresses are not allowed on v6-only socket */
  2167. if (ipv6_only_sock(asoc->base.sk))
  2168. break;
  2169. do_addr_param:
  2170. af = sctp_get_af_specific(param_type2af(param.p->type));
  2171. af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
  2172. scope = sctp_scope(peer_addr);
  2173. if (sctp_in_scope(net, &addr, scope))
  2174. if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
  2175. return 0;
  2176. break;
  2177. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  2178. if (!net->sctp.cookie_preserve_enable)
  2179. break;
  2180. stale = ntohl(param.life->lifespan_increment);
  2181. /* Suggested Cookie Life span increment's unit is msec,
  2182. * (1/1000sec).
  2183. */
  2184. asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
  2185. break;
  2186. case SCTP_PARAM_HOST_NAME_ADDRESS:
  2187. pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
  2188. break;
  2189. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  2190. /* Turn off the default values first so we'll know which
  2191. * ones are really set by the peer.
  2192. */
  2193. asoc->peer.ipv4_address = 0;
  2194. asoc->peer.ipv6_address = 0;
  2195. /* Assume that peer supports the address family
  2196. * by which it sends a packet.
  2197. */
  2198. if (peer_addr->sa.sa_family == AF_INET6)
  2199. asoc->peer.ipv6_address = 1;
  2200. else if (peer_addr->sa.sa_family == AF_INET)
  2201. asoc->peer.ipv4_address = 1;
  2202. /* Cycle through address types; avoid divide by 0. */
  2203. sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  2204. if (sat)
  2205. sat /= sizeof(__u16);
  2206. for (i = 0; i < sat; ++i) {
  2207. switch (param.sat->types[i]) {
  2208. case SCTP_PARAM_IPV4_ADDRESS:
  2209. asoc->peer.ipv4_address = 1;
  2210. break;
  2211. case SCTP_PARAM_IPV6_ADDRESS:
  2212. if (PF_INET6 == asoc->base.sk->sk_family)
  2213. asoc->peer.ipv6_address = 1;
  2214. break;
  2215. case SCTP_PARAM_HOST_NAME_ADDRESS:
  2216. asoc->peer.hostname_address = 1;
  2217. break;
  2218. default: /* Just ignore anything else. */
  2219. break;
  2220. }
  2221. }
  2222. break;
  2223. case SCTP_PARAM_STATE_COOKIE:
  2224. asoc->peer.cookie_len =
  2225. ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
  2226. asoc->peer.cookie = param.cookie->body;
  2227. break;
  2228. case SCTP_PARAM_HEARTBEAT_INFO:
  2229. /* Would be odd to receive, but it causes no problems. */
  2230. break;
  2231. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  2232. /* Rejected during verify stage. */
  2233. break;
  2234. case SCTP_PARAM_ECN_CAPABLE:
  2235. asoc->peer.ecn_capable = 1;
  2236. break;
  2237. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  2238. asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
  2239. break;
  2240. case SCTP_PARAM_SET_PRIMARY:
  2241. if (!net->sctp.addip_enable)
  2242. goto fall_through;
  2243. addr_param = param.v + sizeof(sctp_addip_param_t);
  2244. af = sctp_get_af_specific(param_type2af(addr_param->p.type));
  2245. if (af == NULL)
  2246. break;
  2247. af->from_addr_param(&addr, addr_param,
  2248. htons(asoc->peer.port), 0);
  2249. /* if the address is invalid, we can't process it.
  2250. * XXX: see spec for what to do.
  2251. */
  2252. if (!af->addr_valid(&addr, NULL, NULL))
  2253. break;
  2254. t = sctp_assoc_lookup_paddr(asoc, &addr);
  2255. if (!t)
  2256. break;
  2257. sctp_assoc_set_primary(asoc, t);
  2258. break;
  2259. case SCTP_PARAM_SUPPORTED_EXT:
  2260. sctp_process_ext_param(asoc, param);
  2261. break;
  2262. case SCTP_PARAM_FWD_TSN_SUPPORT:
  2263. if (asoc->prsctp_enable) {
  2264. asoc->peer.prsctp_capable = 1;
  2265. break;
  2266. }
  2267. /* Fall Through */
  2268. goto fall_through;
  2269. case SCTP_PARAM_RANDOM:
  2270. if (!ep->auth_enable)
  2271. goto fall_through;
  2272. /* Save peer's random parameter */
  2273. asoc->peer.peer_random = kmemdup(param.p,
  2274. ntohs(param.p->length), gfp);
  2275. if (!asoc->peer.peer_random) {
  2276. retval = 0;
  2277. break;
  2278. }
  2279. break;
  2280. case SCTP_PARAM_HMAC_ALGO:
  2281. if (!ep->auth_enable)
  2282. goto fall_through;
  2283. /* Save peer's HMAC list */
  2284. asoc->peer.peer_hmacs = kmemdup(param.p,
  2285. ntohs(param.p->length), gfp);
  2286. if (!asoc->peer.peer_hmacs) {
  2287. retval = 0;
  2288. break;
  2289. }
  2290. /* Set the default HMAC the peer requested*/
  2291. sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
  2292. break;
  2293. case SCTP_PARAM_CHUNKS:
  2294. if (!ep->auth_enable)
  2295. goto fall_through;
  2296. asoc->peer.peer_chunks = kmemdup(param.p,
  2297. ntohs(param.p->length), gfp);
  2298. if (!asoc->peer.peer_chunks)
  2299. retval = 0;
  2300. break;
  2301. fall_through:
  2302. default:
  2303. /* Any unrecognized parameters should have been caught
  2304. * and handled by sctp_verify_param() which should be
  2305. * called prior to this routine. Simply log the error
  2306. * here.
  2307. */
  2308. pr_debug("%s: ignoring param:%d for association:%p.\n",
  2309. __func__, ntohs(param.p->type), asoc);
  2310. break;
  2311. }
  2312. return retval;
  2313. }
  2314. /* Select a new verification tag. */
  2315. __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
  2316. {
  2317. /* I believe that this random number generator complies with RFC1750.
  2318. * A tag of 0 is reserved for special cases (e.g. INIT).
  2319. */
  2320. __u32 x;
  2321. do {
  2322. get_random_bytes(&x, sizeof(__u32));
  2323. } while (x == 0);
  2324. return x;
  2325. }
  2326. /* Select an initial TSN to send during startup. */
  2327. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
  2328. {
  2329. __u32 retval;
  2330. get_random_bytes(&retval, sizeof(__u32));
  2331. return retval;
  2332. }
  2333. /*
  2334. * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
  2335. * 0 1 2 3
  2336. * 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
  2337. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2338. * | Type = 0xC1 | Chunk Flags | Chunk Length |
  2339. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2340. * | Serial Number |
  2341. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2342. * | Address Parameter |
  2343. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2344. * | ASCONF Parameter #1 |
  2345. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2346. * \ \
  2347. * / .... /
  2348. * \ \
  2349. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2350. * | ASCONF Parameter #N |
  2351. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2352. *
  2353. * Address Parameter and other parameter will not be wrapped in this function
  2354. */
  2355. static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
  2356. union sctp_addr *addr,
  2357. int vparam_len)
  2358. {
  2359. sctp_addiphdr_t asconf;
  2360. struct sctp_chunk *retval;
  2361. int length = sizeof(asconf) + vparam_len;
  2362. union sctp_addr_param addrparam;
  2363. int addrlen;
  2364. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  2365. addrlen = af->to_addr_param(addr, &addrparam);
  2366. if (!addrlen)
  2367. return NULL;
  2368. length += addrlen;
  2369. /* Create the chunk. */
  2370. retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
  2371. GFP_ATOMIC);
  2372. if (!retval)
  2373. return NULL;
  2374. asconf.serial = htonl(asoc->addip_serial++);
  2375. retval->subh.addip_hdr =
  2376. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2377. retval->param_hdr.v =
  2378. sctp_addto_chunk(retval, addrlen, &addrparam);
  2379. return retval;
  2380. }
  2381. /* ADDIP
  2382. * 3.2.1 Add IP Address
  2383. * 0 1 2 3
  2384. * 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
  2385. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2386. * | Type = 0xC001 | Length = Variable |
  2387. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2388. * | ASCONF-Request Correlation ID |
  2389. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2390. * | Address Parameter |
  2391. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2392. *
  2393. * 3.2.2 Delete IP Address
  2394. * 0 1 2 3
  2395. * 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
  2396. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2397. * | Type = 0xC002 | Length = Variable |
  2398. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2399. * | ASCONF-Request Correlation ID |
  2400. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2401. * | Address Parameter |
  2402. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2403. *
  2404. */
  2405. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  2406. union sctp_addr *laddr,
  2407. struct sockaddr *addrs,
  2408. int addrcnt,
  2409. __be16 flags)
  2410. {
  2411. sctp_addip_param_t param;
  2412. struct sctp_chunk *retval;
  2413. union sctp_addr_param addr_param;
  2414. union sctp_addr *addr;
  2415. void *addr_buf;
  2416. struct sctp_af *af;
  2417. int paramlen = sizeof(param);
  2418. int addr_param_len = 0;
  2419. int totallen = 0;
  2420. int i;
  2421. int del_pickup = 0;
  2422. /* Get total length of all the address parameters. */
  2423. addr_buf = addrs;
  2424. for (i = 0; i < addrcnt; i++) {
  2425. addr = addr_buf;
  2426. af = sctp_get_af_specific(addr->v4.sin_family);
  2427. addr_param_len = af->to_addr_param(addr, &addr_param);
  2428. totallen += paramlen;
  2429. totallen += addr_param_len;
  2430. addr_buf += af->sockaddr_len;
  2431. if (asoc->asconf_addr_del_pending && !del_pickup) {
  2432. /* reuse the parameter length from the same scope one */
  2433. totallen += paramlen;
  2434. totallen += addr_param_len;
  2435. del_pickup = 1;
  2436. pr_debug("%s: picked same-scope del_pending addr, "
  2437. "totallen for all addresses is %d\n",
  2438. __func__, totallen);
  2439. }
  2440. }
  2441. /* Create an asconf chunk with the required length. */
  2442. retval = sctp_make_asconf(asoc, laddr, totallen);
  2443. if (!retval)
  2444. return NULL;
  2445. /* Add the address parameters to the asconf chunk. */
  2446. addr_buf = addrs;
  2447. for (i = 0; i < addrcnt; i++) {
  2448. addr = addr_buf;
  2449. af = sctp_get_af_specific(addr->v4.sin_family);
  2450. addr_param_len = af->to_addr_param(addr, &addr_param);
  2451. param.param_hdr.type = flags;
  2452. param.param_hdr.length = htons(paramlen + addr_param_len);
  2453. param.crr_id = i;
  2454. sctp_addto_chunk(retval, paramlen, &param);
  2455. sctp_addto_chunk(retval, addr_param_len, &addr_param);
  2456. addr_buf += af->sockaddr_len;
  2457. }
  2458. if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
  2459. addr = asoc->asconf_addr_del_pending;
  2460. af = sctp_get_af_specific(addr->v4.sin_family);
  2461. addr_param_len = af->to_addr_param(addr, &addr_param);
  2462. param.param_hdr.type = SCTP_PARAM_DEL_IP;
  2463. param.param_hdr.length = htons(paramlen + addr_param_len);
  2464. param.crr_id = i;
  2465. sctp_addto_chunk(retval, paramlen, &param);
  2466. sctp_addto_chunk(retval, addr_param_len, &addr_param);
  2467. }
  2468. return retval;
  2469. }
  2470. /* ADDIP
  2471. * 3.2.4 Set Primary IP Address
  2472. * 0 1 2 3
  2473. * 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
  2474. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2475. * | Type =0xC004 | Length = Variable |
  2476. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2477. * | ASCONF-Request Correlation ID |
  2478. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2479. * | Address Parameter |
  2480. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2481. *
  2482. * Create an ASCONF chunk with Set Primary IP address parameter.
  2483. */
  2484. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  2485. union sctp_addr *addr)
  2486. {
  2487. sctp_addip_param_t param;
  2488. struct sctp_chunk *retval;
  2489. int len = sizeof(param);
  2490. union sctp_addr_param addrparam;
  2491. int addrlen;
  2492. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  2493. addrlen = af->to_addr_param(addr, &addrparam);
  2494. if (!addrlen)
  2495. return NULL;
  2496. len += addrlen;
  2497. /* Create the chunk and make asconf header. */
  2498. retval = sctp_make_asconf(asoc, addr, len);
  2499. if (!retval)
  2500. return NULL;
  2501. param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
  2502. param.param_hdr.length = htons(len);
  2503. param.crr_id = 0;
  2504. sctp_addto_chunk(retval, sizeof(param), &param);
  2505. sctp_addto_chunk(retval, addrlen, &addrparam);
  2506. return retval;
  2507. }
  2508. /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
  2509. * 0 1 2 3
  2510. * 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
  2511. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2512. * | Type = 0x80 | Chunk Flags | Chunk Length |
  2513. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2514. * | Serial Number |
  2515. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2516. * | ASCONF Parameter Response#1 |
  2517. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2518. * \ \
  2519. * / .... /
  2520. * \ \
  2521. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2522. * | ASCONF Parameter Response#N |
  2523. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2524. *
  2525. * Create an ASCONF_ACK chunk with enough space for the parameter responses.
  2526. */
  2527. static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
  2528. __u32 serial, int vparam_len)
  2529. {
  2530. sctp_addiphdr_t asconf;
  2531. struct sctp_chunk *retval;
  2532. int length = sizeof(asconf) + vparam_len;
  2533. /* Create the chunk. */
  2534. retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
  2535. GFP_ATOMIC);
  2536. if (!retval)
  2537. return NULL;
  2538. asconf.serial = htonl(serial);
  2539. retval->subh.addip_hdr =
  2540. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2541. return retval;
  2542. }
  2543. /* Add response parameters to an ASCONF_ACK chunk. */
  2544. static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
  2545. __be16 err_code, sctp_addip_param_t *asconf_param)
  2546. {
  2547. sctp_addip_param_t ack_param;
  2548. struct sctp_errhdr err_param;
  2549. int asconf_param_len = 0;
  2550. int err_param_len = 0;
  2551. __be16 response_type;
  2552. if (SCTP_ERROR_NO_ERROR == err_code) {
  2553. response_type = SCTP_PARAM_SUCCESS_REPORT;
  2554. } else {
  2555. response_type = SCTP_PARAM_ERR_CAUSE;
  2556. err_param_len = sizeof(err_param);
  2557. if (asconf_param)
  2558. asconf_param_len =
  2559. ntohs(asconf_param->param_hdr.length);
  2560. }
  2561. /* Add Success Indication or Error Cause Indication parameter. */
  2562. ack_param.param_hdr.type = response_type;
  2563. ack_param.param_hdr.length = htons(sizeof(ack_param) +
  2564. err_param_len +
  2565. asconf_param_len);
  2566. ack_param.crr_id = crr_id;
  2567. sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
  2568. if (SCTP_ERROR_NO_ERROR == err_code)
  2569. return;
  2570. /* Add Error Cause parameter. */
  2571. err_param.cause = err_code;
  2572. err_param.length = htons(err_param_len + asconf_param_len);
  2573. sctp_addto_chunk(chunk, err_param_len, &err_param);
  2574. /* Add the failed TLV copied from ASCONF chunk. */
  2575. if (asconf_param)
  2576. sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
  2577. }
  2578. /* Process a asconf parameter. */
  2579. static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
  2580. struct sctp_chunk *asconf,
  2581. sctp_addip_param_t *asconf_param)
  2582. {
  2583. struct sctp_transport *peer;
  2584. struct sctp_af *af;
  2585. union sctp_addr addr;
  2586. union sctp_addr_param *addr_param;
  2587. addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
  2588. if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
  2589. asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
  2590. asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
  2591. return SCTP_ERROR_UNKNOWN_PARAM;
  2592. switch (addr_param->p.type) {
  2593. case SCTP_PARAM_IPV6_ADDRESS:
  2594. if (!asoc->peer.ipv6_address)
  2595. return SCTP_ERROR_DNS_FAILED;
  2596. break;
  2597. case SCTP_PARAM_IPV4_ADDRESS:
  2598. if (!asoc->peer.ipv4_address)
  2599. return SCTP_ERROR_DNS_FAILED;
  2600. break;
  2601. default:
  2602. return SCTP_ERROR_DNS_FAILED;
  2603. }
  2604. af = sctp_get_af_specific(param_type2af(addr_param->p.type));
  2605. if (unlikely(!af))
  2606. return SCTP_ERROR_DNS_FAILED;
  2607. af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
  2608. /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
  2609. * or multicast address.
  2610. * (note: wildcard is permitted and requires special handling so
  2611. * make sure we check for that)
  2612. */
  2613. if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
  2614. return SCTP_ERROR_DNS_FAILED;
  2615. switch (asconf_param->param_hdr.type) {
  2616. case SCTP_PARAM_ADD_IP:
  2617. /* Section 4.2.1:
  2618. * If the address 0.0.0.0 or ::0 is provided, the source
  2619. * address of the packet MUST be added.
  2620. */
  2621. if (af->is_any(&addr))
  2622. memcpy(&addr, &asconf->source, sizeof(addr));
  2623. /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
  2624. * request and does not have the local resources to add this
  2625. * new address to the association, it MUST return an Error
  2626. * Cause TLV set to the new error code 'Operation Refused
  2627. * Due to Resource Shortage'.
  2628. */
  2629. peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
  2630. if (!peer)
  2631. return SCTP_ERROR_RSRC_LOW;
  2632. /* Start the heartbeat timer. */
  2633. sctp_transport_reset_hb_timer(peer);
  2634. asoc->new_transport = peer;
  2635. break;
  2636. case SCTP_PARAM_DEL_IP:
  2637. /* ADDIP 4.3 D7) If a request is received to delete the
  2638. * last remaining IP address of a peer endpoint, the receiver
  2639. * MUST send an Error Cause TLV with the error cause set to the
  2640. * new error code 'Request to Delete Last Remaining IP Address'.
  2641. */
  2642. if (asoc->peer.transport_count == 1)
  2643. return SCTP_ERROR_DEL_LAST_IP;
  2644. /* ADDIP 4.3 D8) If a request is received to delete an IP
  2645. * address which is also the source address of the IP packet
  2646. * which contained the ASCONF chunk, the receiver MUST reject
  2647. * this request. To reject the request the receiver MUST send
  2648. * an Error Cause TLV set to the new error code 'Request to
  2649. * Delete Source IP Address'
  2650. */
  2651. if (sctp_cmp_addr_exact(&asconf->source, &addr))
  2652. return SCTP_ERROR_DEL_SRC_IP;
  2653. /* Section 4.2.2
  2654. * If the address 0.0.0.0 or ::0 is provided, all
  2655. * addresses of the peer except the source address of the
  2656. * packet MUST be deleted.
  2657. */
  2658. if (af->is_any(&addr)) {
  2659. sctp_assoc_set_primary(asoc, asconf->transport);
  2660. sctp_assoc_del_nonprimary_peers(asoc,
  2661. asconf->transport);
  2662. return SCTP_ERROR_NO_ERROR;
  2663. }
  2664. /* If the address is not part of the association, the
  2665. * ASCONF-ACK with Error Cause Indication Parameter
  2666. * which including cause of Unresolvable Address should
  2667. * be sent.
  2668. */
  2669. peer = sctp_assoc_lookup_paddr(asoc, &addr);
  2670. if (!peer)
  2671. return SCTP_ERROR_DNS_FAILED;
  2672. sctp_assoc_rm_peer(asoc, peer);
  2673. break;
  2674. case SCTP_PARAM_SET_PRIMARY:
  2675. /* ADDIP Section 4.2.4
  2676. * If the address 0.0.0.0 or ::0 is provided, the receiver
  2677. * MAY mark the source address of the packet as its
  2678. * primary.
  2679. */
  2680. if (af->is_any(&addr))
  2681. memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
  2682. peer = sctp_assoc_lookup_paddr(asoc, &addr);
  2683. if (!peer)
  2684. return SCTP_ERROR_DNS_FAILED;
  2685. sctp_assoc_set_primary(asoc, peer);
  2686. break;
  2687. }
  2688. return SCTP_ERROR_NO_ERROR;
  2689. }
  2690. /* Verify the ASCONF packet before we process it. */
  2691. bool sctp_verify_asconf(const struct sctp_association *asoc,
  2692. struct sctp_chunk *chunk, bool addr_param_needed,
  2693. struct sctp_paramhdr **errp)
  2694. {
  2695. sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) chunk->chunk_hdr;
  2696. union sctp_params param;
  2697. bool addr_param_seen = false;
  2698. sctp_walk_params(param, addip, addip_hdr.params) {
  2699. size_t length = ntohs(param.p->length);
  2700. *errp = param.p;
  2701. switch (param.p->type) {
  2702. case SCTP_PARAM_ERR_CAUSE:
  2703. break;
  2704. case SCTP_PARAM_IPV4_ADDRESS:
  2705. if (length != sizeof(struct sctp_ipv4addr_param))
  2706. return false;
  2707. /* ensure there is only one addr param and it's in the
  2708. * beginning of addip_hdr params, or we reject it.
  2709. */
  2710. if (param.v != addip->addip_hdr.params)
  2711. return false;
  2712. addr_param_seen = true;
  2713. break;
  2714. case SCTP_PARAM_IPV6_ADDRESS:
  2715. if (length != sizeof(struct sctp_ipv6addr_param))
  2716. return false;
  2717. if (param.v != addip->addip_hdr.params)
  2718. return false;
  2719. addr_param_seen = true;
  2720. break;
  2721. case SCTP_PARAM_ADD_IP:
  2722. case SCTP_PARAM_DEL_IP:
  2723. case SCTP_PARAM_SET_PRIMARY:
  2724. /* In ASCONF chunks, these need to be first. */
  2725. if (addr_param_needed && !addr_param_seen)
  2726. return false;
  2727. length = ntohs(param.addip->param_hdr.length);
  2728. if (length < sizeof(sctp_addip_param_t) +
  2729. sizeof(**errp))
  2730. return false;
  2731. break;
  2732. case SCTP_PARAM_SUCCESS_REPORT:
  2733. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  2734. if (length != sizeof(sctp_addip_param_t))
  2735. return false;
  2736. break;
  2737. default:
  2738. /* This is unkown to us, reject! */
  2739. return false;
  2740. }
  2741. }
  2742. /* Remaining sanity checks. */
  2743. if (addr_param_needed && !addr_param_seen)
  2744. return false;
  2745. if (!addr_param_needed && addr_param_seen)
  2746. return false;
  2747. if (param.v != chunk->chunk_end)
  2748. return false;
  2749. return true;
  2750. }
  2751. /* Process an incoming ASCONF chunk with the next expected serial no. and
  2752. * return an ASCONF_ACK chunk to be sent in response.
  2753. */
  2754. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  2755. struct sctp_chunk *asconf)
  2756. {
  2757. sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) asconf->chunk_hdr;
  2758. bool all_param_pass = true;
  2759. union sctp_params param;
  2760. sctp_addiphdr_t *hdr;
  2761. union sctp_addr_param *addr_param;
  2762. struct sctp_chunk *asconf_ack;
  2763. __be16 err_code;
  2764. int length = 0;
  2765. int chunk_len;
  2766. __u32 serial;
  2767. chunk_len = ntohs(asconf->chunk_hdr->length) -
  2768. sizeof(struct sctp_chunkhdr);
  2769. hdr = (sctp_addiphdr_t *)asconf->skb->data;
  2770. serial = ntohl(hdr->serial);
  2771. /* Skip the addiphdr and store a pointer to address parameter. */
  2772. length = sizeof(sctp_addiphdr_t);
  2773. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2774. chunk_len -= length;
  2775. /* Skip the address parameter and store a pointer to the first
  2776. * asconf parameter.
  2777. */
  2778. length = ntohs(addr_param->p.length);
  2779. chunk_len -= length;
  2780. /* create an ASCONF_ACK chunk.
  2781. * Based on the definitions of parameters, we know that the size of
  2782. * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
  2783. * parameters.
  2784. */
  2785. asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
  2786. if (!asconf_ack)
  2787. goto done;
  2788. /* Process the TLVs contained within the ASCONF chunk. */
  2789. sctp_walk_params(param, addip, addip_hdr.params) {
  2790. /* Skip preceeding address parameters. */
  2791. if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
  2792. param.p->type == SCTP_PARAM_IPV6_ADDRESS)
  2793. continue;
  2794. err_code = sctp_process_asconf_param(asoc, asconf,
  2795. param.addip);
  2796. /* ADDIP 4.1 A7)
  2797. * If an error response is received for a TLV parameter,
  2798. * all TLVs with no response before the failed TLV are
  2799. * considered successful if not reported. All TLVs after
  2800. * the failed response are considered unsuccessful unless
  2801. * a specific success indication is present for the parameter.
  2802. */
  2803. if (err_code != SCTP_ERROR_NO_ERROR)
  2804. all_param_pass = false;
  2805. if (!all_param_pass)
  2806. sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
  2807. err_code, param.addip);
  2808. /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
  2809. * an IP address sends an 'Out of Resource' in its response, it
  2810. * MUST also fail any subsequent add or delete requests bundled
  2811. * in the ASCONF.
  2812. */
  2813. if (err_code == SCTP_ERROR_RSRC_LOW)
  2814. goto done;
  2815. }
  2816. done:
  2817. asoc->peer.addip_serial++;
  2818. /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
  2819. * after freeing the reference to old asconf ack if any.
  2820. */
  2821. if (asconf_ack) {
  2822. sctp_chunk_hold(asconf_ack);
  2823. list_add_tail(&asconf_ack->transmitted_list,
  2824. &asoc->asconf_ack_list);
  2825. }
  2826. return asconf_ack;
  2827. }
  2828. /* Process a asconf parameter that is successfully acked. */
  2829. static void sctp_asconf_param_success(struct sctp_association *asoc,
  2830. sctp_addip_param_t *asconf_param)
  2831. {
  2832. struct sctp_af *af;
  2833. union sctp_addr addr;
  2834. struct sctp_bind_addr *bp = &asoc->base.bind_addr;
  2835. union sctp_addr_param *addr_param;
  2836. struct sctp_transport *transport;
  2837. struct sctp_sockaddr_entry *saddr;
  2838. addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
  2839. /* We have checked the packet before, so we do not check again. */
  2840. af = sctp_get_af_specific(param_type2af(addr_param->p.type));
  2841. af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
  2842. switch (asconf_param->param_hdr.type) {
  2843. case SCTP_PARAM_ADD_IP:
  2844. /* This is always done in BH context with a socket lock
  2845. * held, so the list can not change.
  2846. */
  2847. local_bh_disable();
  2848. list_for_each_entry(saddr, &bp->address_list, list) {
  2849. if (sctp_cmp_addr_exact(&saddr->a, &addr))
  2850. saddr->state = SCTP_ADDR_SRC;
  2851. }
  2852. local_bh_enable();
  2853. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2854. transports) {
  2855. sctp_transport_dst_release(transport);
  2856. }
  2857. break;
  2858. case SCTP_PARAM_DEL_IP:
  2859. local_bh_disable();
  2860. sctp_del_bind_addr(bp, &addr);
  2861. if (asoc->asconf_addr_del_pending != NULL &&
  2862. sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
  2863. kfree(asoc->asconf_addr_del_pending);
  2864. asoc->asconf_addr_del_pending = NULL;
  2865. }
  2866. local_bh_enable();
  2867. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2868. transports) {
  2869. sctp_transport_dst_release(transport);
  2870. }
  2871. break;
  2872. default:
  2873. break;
  2874. }
  2875. }
  2876. /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
  2877. * for the given asconf parameter. If there is no response for this parameter,
  2878. * return the error code based on the third argument 'no_err'.
  2879. * ADDIP 4.1
  2880. * A7) If an error response is received for a TLV parameter, all TLVs with no
  2881. * response before the failed TLV are considered successful if not reported.
  2882. * All TLVs after the failed response are considered unsuccessful unless a
  2883. * specific success indication is present for the parameter.
  2884. */
  2885. static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
  2886. sctp_addip_param_t *asconf_param,
  2887. int no_err)
  2888. {
  2889. sctp_addip_param_t *asconf_ack_param;
  2890. struct sctp_errhdr *err_param;
  2891. int length;
  2892. int asconf_ack_len;
  2893. __be16 err_code;
  2894. if (no_err)
  2895. err_code = SCTP_ERROR_NO_ERROR;
  2896. else
  2897. err_code = SCTP_ERROR_REQ_REFUSED;
  2898. asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
  2899. sizeof(struct sctp_chunkhdr);
  2900. /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
  2901. * the first asconf_ack parameter.
  2902. */
  2903. length = sizeof(sctp_addiphdr_t);
  2904. asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
  2905. length);
  2906. asconf_ack_len -= length;
  2907. while (asconf_ack_len > 0) {
  2908. if (asconf_ack_param->crr_id == asconf_param->crr_id) {
  2909. switch (asconf_ack_param->param_hdr.type) {
  2910. case SCTP_PARAM_SUCCESS_REPORT:
  2911. return SCTP_ERROR_NO_ERROR;
  2912. case SCTP_PARAM_ERR_CAUSE:
  2913. length = sizeof(sctp_addip_param_t);
  2914. err_param = (void *)asconf_ack_param + length;
  2915. asconf_ack_len -= length;
  2916. if (asconf_ack_len > 0)
  2917. return err_param->cause;
  2918. else
  2919. return SCTP_ERROR_INV_PARAM;
  2920. break;
  2921. default:
  2922. return SCTP_ERROR_INV_PARAM;
  2923. }
  2924. }
  2925. length = ntohs(asconf_ack_param->param_hdr.length);
  2926. asconf_ack_param = (void *)asconf_ack_param + length;
  2927. asconf_ack_len -= length;
  2928. }
  2929. return err_code;
  2930. }
  2931. /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
  2932. int sctp_process_asconf_ack(struct sctp_association *asoc,
  2933. struct sctp_chunk *asconf_ack)
  2934. {
  2935. struct sctp_chunk *asconf = asoc->addip_last_asconf;
  2936. union sctp_addr_param *addr_param;
  2937. sctp_addip_param_t *asconf_param;
  2938. int length = 0;
  2939. int asconf_len = asconf->skb->len;
  2940. int all_param_pass = 0;
  2941. int no_err = 1;
  2942. int retval = 0;
  2943. __be16 err_code = SCTP_ERROR_NO_ERROR;
  2944. /* Skip the chunkhdr and addiphdr from the last asconf sent and store
  2945. * a pointer to address parameter.
  2946. */
  2947. length = sizeof(sctp_addip_chunk_t);
  2948. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2949. asconf_len -= length;
  2950. /* Skip the address parameter in the last asconf sent and store a
  2951. * pointer to the first asconf parameter.
  2952. */
  2953. length = ntohs(addr_param->p.length);
  2954. asconf_param = (void *)addr_param + length;
  2955. asconf_len -= length;
  2956. /* ADDIP 4.1
  2957. * A8) If there is no response(s) to specific TLV parameter(s), and no
  2958. * failures are indicated, then all request(s) are considered
  2959. * successful.
  2960. */
  2961. if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
  2962. all_param_pass = 1;
  2963. /* Process the TLVs contained in the last sent ASCONF chunk. */
  2964. while (asconf_len > 0) {
  2965. if (all_param_pass)
  2966. err_code = SCTP_ERROR_NO_ERROR;
  2967. else {
  2968. err_code = sctp_get_asconf_response(asconf_ack,
  2969. asconf_param,
  2970. no_err);
  2971. if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
  2972. no_err = 0;
  2973. }
  2974. switch (err_code) {
  2975. case SCTP_ERROR_NO_ERROR:
  2976. sctp_asconf_param_success(asoc, asconf_param);
  2977. break;
  2978. case SCTP_ERROR_RSRC_LOW:
  2979. retval = 1;
  2980. break;
  2981. case SCTP_ERROR_UNKNOWN_PARAM:
  2982. /* Disable sending this type of asconf parameter in
  2983. * future.
  2984. */
  2985. asoc->peer.addip_disabled_mask |=
  2986. asconf_param->param_hdr.type;
  2987. break;
  2988. case SCTP_ERROR_REQ_REFUSED:
  2989. case SCTP_ERROR_DEL_LAST_IP:
  2990. case SCTP_ERROR_DEL_SRC_IP:
  2991. default:
  2992. break;
  2993. }
  2994. /* Skip the processed asconf parameter and move to the next
  2995. * one.
  2996. */
  2997. length = ntohs(asconf_param->param_hdr.length);
  2998. asconf_param = (void *)asconf_param + length;
  2999. asconf_len -= length;
  3000. }
  3001. if (no_err && asoc->src_out_of_asoc_ok) {
  3002. asoc->src_out_of_asoc_ok = 0;
  3003. sctp_transport_immediate_rtx(asoc->peer.primary_path);
  3004. }
  3005. /* Free the cached last sent asconf chunk. */
  3006. list_del_init(&asconf->transmitted_list);
  3007. sctp_chunk_free(asconf);
  3008. asoc->addip_last_asconf = NULL;
  3009. return retval;
  3010. }
  3011. /* Make a FWD TSN chunk. */
  3012. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  3013. __u32 new_cum_tsn, size_t nstreams,
  3014. struct sctp_fwdtsn_skip *skiplist)
  3015. {
  3016. struct sctp_chunk *retval = NULL;
  3017. struct sctp_fwdtsn_hdr ftsn_hdr;
  3018. struct sctp_fwdtsn_skip skip;
  3019. size_t hint;
  3020. int i;
  3021. hint = (nstreams + 1) * sizeof(__u32);
  3022. retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
  3023. if (!retval)
  3024. return NULL;
  3025. ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
  3026. retval->subh.fwdtsn_hdr =
  3027. sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
  3028. for (i = 0; i < nstreams; i++) {
  3029. skip.stream = skiplist[i].stream;
  3030. skip.ssn = skiplist[i].ssn;
  3031. sctp_addto_chunk(retval, sizeof(skip), &skip);
  3032. }
  3033. return retval;
  3034. }
  3035. /* RE-CONFIG 3.1 (RE-CONFIG chunk)
  3036. * 0 1 2 3
  3037. * 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
  3038. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3039. * | Type = 130 | Chunk Flags | Chunk Length |
  3040. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3041. * \ \
  3042. * / Re-configuration Parameter /
  3043. * \ \
  3044. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3045. * \ \
  3046. * / Re-configuration Parameter (optional) /
  3047. * \ \
  3048. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3049. */
  3050. static struct sctp_chunk *sctp_make_reconf(
  3051. const struct sctp_association *asoc,
  3052. int length)
  3053. {
  3054. struct sctp_reconf_chunk *reconf;
  3055. struct sctp_chunk *retval;
  3056. retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
  3057. GFP_ATOMIC);
  3058. if (!retval)
  3059. return NULL;
  3060. reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
  3061. retval->param_hdr.v = reconf->params;
  3062. return retval;
  3063. }
  3064. /* RE-CONFIG 4.1 (STREAM OUT RESET)
  3065. * 0 1 2 3
  3066. * 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
  3067. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3068. * | Parameter Type = 13 | Parameter Length = 16 + 2 * N |
  3069. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3070. * | Re-configuration Request Sequence Number |
  3071. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3072. * | Re-configuration Response Sequence Number |
  3073. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3074. * | Sender's Last Assigned TSN |
  3075. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3076. * | Stream Number 1 (optional) | Stream Number 2 (optional) |
  3077. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3078. * / ...... /
  3079. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3080. * | Stream Number N-1 (optional) | Stream Number N (optional) |
  3081. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3082. *
  3083. * RE-CONFIG 4.2 (STREAM IN RESET)
  3084. * 0 1 2 3
  3085. * 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
  3086. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3087. * | Parameter Type = 14 | Parameter Length = 8 + 2 * N |
  3088. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3089. * | Re-configuration Request Sequence Number |
  3090. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3091. * | Stream Number 1 (optional) | Stream Number 2 (optional) |
  3092. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3093. * / ...... /
  3094. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3095. * | Stream Number N-1 (optional) | Stream Number N (optional) |
  3096. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3097. */
  3098. struct sctp_chunk *sctp_make_strreset_req(
  3099. const struct sctp_association *asoc,
  3100. __u16 stream_num, __u16 *stream_list,
  3101. bool out, bool in)
  3102. {
  3103. struct sctp_strreset_outreq outreq;
  3104. __u16 stream_len = stream_num * 2;
  3105. struct sctp_strreset_inreq inreq;
  3106. struct sctp_chunk *retval;
  3107. __u16 outlen, inlen;
  3108. outlen = (sizeof(outreq) + stream_len) * out;
  3109. inlen = (sizeof(inreq) + stream_len) * in;
  3110. retval = sctp_make_reconf(asoc, outlen + inlen);
  3111. if (!retval)
  3112. return NULL;
  3113. if (outlen) {
  3114. outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
  3115. outreq.param_hdr.length = htons(outlen);
  3116. outreq.request_seq = htonl(asoc->strreset_outseq);
  3117. outreq.response_seq = htonl(asoc->strreset_inseq - 1);
  3118. outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
  3119. sctp_addto_chunk(retval, sizeof(outreq), &outreq);
  3120. if (stream_len)
  3121. sctp_addto_chunk(retval, stream_len, stream_list);
  3122. }
  3123. if (inlen) {
  3124. inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
  3125. inreq.param_hdr.length = htons(inlen);
  3126. inreq.request_seq = htonl(asoc->strreset_outseq + out);
  3127. sctp_addto_chunk(retval, sizeof(inreq), &inreq);
  3128. if (stream_len)
  3129. sctp_addto_chunk(retval, stream_len, stream_list);
  3130. }
  3131. return retval;
  3132. }
  3133. /* RE-CONFIG 4.3 (SSN/TSN RESET ALL)
  3134. * 0 1 2 3
  3135. * 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
  3136. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3137. * | Parameter Type = 15 | Parameter Length = 8 |
  3138. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3139. * | Re-configuration Request Sequence Number |
  3140. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3141. */
  3142. struct sctp_chunk *sctp_make_strreset_tsnreq(
  3143. const struct sctp_association *asoc)
  3144. {
  3145. struct sctp_strreset_tsnreq tsnreq;
  3146. __u16 length = sizeof(tsnreq);
  3147. struct sctp_chunk *retval;
  3148. retval = sctp_make_reconf(asoc, length);
  3149. if (!retval)
  3150. return NULL;
  3151. tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
  3152. tsnreq.param_hdr.length = htons(length);
  3153. tsnreq.request_seq = htonl(asoc->strreset_outseq);
  3154. sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
  3155. return retval;
  3156. }
  3157. /* RE-CONFIG 4.5/4.6 (ADD STREAM)
  3158. * 0 1 2 3
  3159. * 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
  3160. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3161. * | Parameter Type = 17 | Parameter Length = 12 |
  3162. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3163. * | Re-configuration Request Sequence Number |
  3164. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3165. * | Number of new streams | Reserved |
  3166. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3167. */
  3168. struct sctp_chunk *sctp_make_strreset_addstrm(
  3169. const struct sctp_association *asoc,
  3170. __u16 out, __u16 in)
  3171. {
  3172. struct sctp_strreset_addstrm addstrm;
  3173. __u16 size = sizeof(addstrm);
  3174. struct sctp_chunk *retval;
  3175. retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
  3176. if (!retval)
  3177. return NULL;
  3178. if (out) {
  3179. addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
  3180. addstrm.param_hdr.length = htons(size);
  3181. addstrm.number_of_streams = htons(out);
  3182. addstrm.request_seq = htonl(asoc->strreset_outseq);
  3183. addstrm.reserved = 0;
  3184. sctp_addto_chunk(retval, size, &addstrm);
  3185. }
  3186. if (in) {
  3187. addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
  3188. addstrm.param_hdr.length = htons(size);
  3189. addstrm.number_of_streams = htons(in);
  3190. addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
  3191. addstrm.reserved = 0;
  3192. sctp_addto_chunk(retval, size, &addstrm);
  3193. }
  3194. return retval;
  3195. }
  3196. /* RE-CONFIG 4.4 (RESP)
  3197. * 0 1 2 3
  3198. * 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
  3199. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3200. * | Parameter Type = 16 | Parameter Length |
  3201. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3202. * | Re-configuration Response Sequence Number |
  3203. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3204. * | Result |
  3205. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3206. */
  3207. struct sctp_chunk *sctp_make_strreset_resp(
  3208. const struct sctp_association *asoc,
  3209. __u32 result, __u32 sn)
  3210. {
  3211. struct sctp_strreset_resp resp;
  3212. __u16 length = sizeof(resp);
  3213. struct sctp_chunk *retval;
  3214. retval = sctp_make_reconf(asoc, length);
  3215. if (!retval)
  3216. return NULL;
  3217. resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
  3218. resp.param_hdr.length = htons(length);
  3219. resp.response_seq = htonl(sn);
  3220. resp.result = htonl(result);
  3221. sctp_addto_chunk(retval, sizeof(resp), &resp);
  3222. return retval;
  3223. }
  3224. /* RE-CONFIG 4.4 OPTIONAL (TSNRESP)
  3225. * 0 1 2 3
  3226. * 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
  3227. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3228. * | Parameter Type = 16 | Parameter Length |
  3229. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3230. * | Re-configuration Response Sequence Number |
  3231. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3232. * | Result |
  3233. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3234. * | Sender's Next TSN (optional) |
  3235. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3236. * | Receiver's Next TSN (optional) |
  3237. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  3238. */
  3239. struct sctp_chunk *sctp_make_strreset_tsnresp(
  3240. struct sctp_association *asoc,
  3241. __u32 result, __u32 sn,
  3242. __u32 sender_tsn, __u32 receiver_tsn)
  3243. {
  3244. struct sctp_strreset_resptsn tsnresp;
  3245. __u16 length = sizeof(tsnresp);
  3246. struct sctp_chunk *retval;
  3247. retval = sctp_make_reconf(asoc, length);
  3248. if (!retval)
  3249. return NULL;
  3250. tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
  3251. tsnresp.param_hdr.length = htons(length);
  3252. tsnresp.response_seq = htonl(sn);
  3253. tsnresp.result = htonl(result);
  3254. tsnresp.senders_next_tsn = htonl(sender_tsn);
  3255. tsnresp.receivers_next_tsn = htonl(receiver_tsn);
  3256. sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
  3257. return retval;
  3258. }
  3259. bool sctp_verify_reconf(const struct sctp_association *asoc,
  3260. struct sctp_chunk *chunk,
  3261. struct sctp_paramhdr **errp)
  3262. {
  3263. struct sctp_reconf_chunk *hdr;
  3264. union sctp_params param;
  3265. __u16 last = 0, cnt = 0;
  3266. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  3267. sctp_walk_params(param, hdr, params) {
  3268. __u16 length = ntohs(param.p->length);
  3269. *errp = param.p;
  3270. if (cnt++ > 2)
  3271. return false;
  3272. switch (param.p->type) {
  3273. case SCTP_PARAM_RESET_OUT_REQUEST:
  3274. if (length < sizeof(struct sctp_strreset_outreq) ||
  3275. (last && last != SCTP_PARAM_RESET_RESPONSE &&
  3276. last != SCTP_PARAM_RESET_IN_REQUEST))
  3277. return false;
  3278. break;
  3279. case SCTP_PARAM_RESET_IN_REQUEST:
  3280. if (length < sizeof(struct sctp_strreset_inreq) ||
  3281. (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
  3282. return false;
  3283. break;
  3284. case SCTP_PARAM_RESET_RESPONSE:
  3285. if ((length != sizeof(struct sctp_strreset_resp) &&
  3286. length != sizeof(struct sctp_strreset_resptsn)) ||
  3287. (last && last != SCTP_PARAM_RESET_RESPONSE &&
  3288. last != SCTP_PARAM_RESET_OUT_REQUEST))
  3289. return false;
  3290. break;
  3291. case SCTP_PARAM_RESET_TSN_REQUEST:
  3292. if (length !=
  3293. sizeof(struct sctp_strreset_tsnreq) || last)
  3294. return false;
  3295. break;
  3296. case SCTP_PARAM_RESET_ADD_IN_STREAMS:
  3297. if (length != sizeof(struct sctp_strreset_addstrm) ||
  3298. (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
  3299. return false;
  3300. break;
  3301. case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
  3302. if (length != sizeof(struct sctp_strreset_addstrm) ||
  3303. (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
  3304. return false;
  3305. break;
  3306. default:
  3307. return false;
  3308. }
  3309. last = param.p->type;
  3310. }
  3311. return true;
  3312. }