sm_make_chunk.c 99 KB

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