sm_make_chunk.c 116 KB

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