sm_make_chunk.c 115 KB

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