sm_make_chunk.c 103 KB

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