sm_make_chunk.c 117 KB

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