sm_make_chunk.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793
  1. /* SCTP kernel reference 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 reference 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. * The SCTP reference 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. * The SCTP reference 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 <asm/scatterlist.h>
  59. #include <linux/crypto.h>
  60. #include <net/sock.h>
  61. #include <linux/skbuff.h>
  62. #include <linux/random.h> /* for get_random_bytes */
  63. #include <net/sctp/sctp.h>
  64. #include <net/sctp/sm.h>
  65. extern kmem_cache_t *sctp_chunk_cachep;
  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. unsigned int __nocast gfp);
  78. /* What was the inbound interface for this chunk? */
  79. int sctp_chunk_iif(const struct sctp_chunk *chunk)
  80. {
  81. struct sctp_af *af;
  82. int iif = 0;
  83. af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
  84. if (af)
  85. iif = af->skb_iif(chunk->skb);
  86. return iif;
  87. }
  88. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  89. *
  90. * Note 2: The ECN capable field is reserved for future use of
  91. * Explicit Congestion Notification.
  92. */
  93. static const struct sctp_paramhdr ecap_param = {
  94. SCTP_PARAM_ECN_CAPABLE,
  95. __constant_htons(sizeof(struct sctp_paramhdr)),
  96. };
  97. static const struct sctp_paramhdr prsctp_param = {
  98. SCTP_PARAM_FWD_TSN_SUPPORT,
  99. __constant_htons(sizeof(struct sctp_paramhdr)),
  100. };
  101. /* A helper to initialize to initialize an op error inside a
  102. * provided chunk, as most cause codes will be embedded inside an
  103. * abort chunk.
  104. */
  105. void sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
  106. const void *payload, size_t paylen)
  107. {
  108. sctp_errhdr_t err;
  109. int padlen;
  110. __u16 len;
  111. /* Cause code constants are now defined in network order. */
  112. err.cause = cause_code;
  113. len = sizeof(sctp_errhdr_t) + paylen;
  114. padlen = len % 4;
  115. err.length = htons(len);
  116. len += padlen;
  117. sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
  118. chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
  119. }
  120. /* 3.3.2 Initiation (INIT) (1)
  121. *
  122. * This chunk is used to initiate a SCTP association between two
  123. * endpoints. The format of the INIT chunk is shown below:
  124. *
  125. * 0 1 2 3
  126. * 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
  127. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  128. * | Type = 1 | Chunk Flags | Chunk Length |
  129. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  130. * | Initiate Tag |
  131. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  132. * | Advertised Receiver Window Credit (a_rwnd) |
  133. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  134. * | Number of Outbound Streams | Number of Inbound Streams |
  135. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  136. * | Initial TSN |
  137. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  138. * \ \
  139. * / Optional/Variable-Length Parameters /
  140. * \ \
  141. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  142. *
  143. *
  144. * The INIT chunk contains the following parameters. Unless otherwise
  145. * noted, each parameter MUST only be included once in the INIT chunk.
  146. *
  147. * Fixed Parameters Status
  148. * ----------------------------------------------
  149. * Initiate Tag Mandatory
  150. * Advertised Receiver Window Credit Mandatory
  151. * Number of Outbound Streams Mandatory
  152. * Number of Inbound Streams Mandatory
  153. * Initial TSN Mandatory
  154. *
  155. * Variable Parameters Status Type Value
  156. * -------------------------------------------------------------
  157. * IPv4 Address (Note 1) Optional 5
  158. * IPv6 Address (Note 1) Optional 6
  159. * Cookie Preservative Optional 9
  160. * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
  161. * Host Name Address (Note 3) Optional 11
  162. * Supported Address Types (Note 4) Optional 12
  163. */
  164. struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
  165. const struct sctp_bind_addr *bp,
  166. unsigned int __nocast gfp, int vparam_len)
  167. {
  168. sctp_inithdr_t init;
  169. union sctp_params addrs;
  170. size_t chunksize;
  171. struct sctp_chunk *retval = NULL;
  172. int num_types, addrs_len = 0;
  173. struct sctp_sock *sp;
  174. sctp_supported_addrs_param_t sat;
  175. __u16 types[2];
  176. sctp_adaption_ind_param_t aiparam;
  177. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  178. *
  179. * Note 1: The INIT chunks can contain multiple addresses that
  180. * can be IPv4 and/or IPv6 in any combination.
  181. */
  182. retval = NULL;
  183. /* Convert the provided bind address list to raw format. */
  184. addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
  185. init.init_tag = htonl(asoc->c.my_vtag);
  186. init.a_rwnd = htonl(asoc->rwnd);
  187. init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
  188. init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
  189. init.initial_tsn = htonl(asoc->c.initial_tsn);
  190. /* How many address types are needed? */
  191. sp = sctp_sk(asoc->base.sk);
  192. num_types = sp->pf->supported_addrs(sp, types);
  193. chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
  194. chunksize += sizeof(ecap_param);
  195. if (sctp_prsctp_enable)
  196. chunksize += sizeof(prsctp_param);
  197. chunksize += sizeof(aiparam);
  198. chunksize += vparam_len;
  199. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  200. *
  201. * Note 3: An INIT chunk MUST NOT contain more than one Host
  202. * Name address parameter. Moreover, the sender of the INIT
  203. * MUST NOT combine any other address types with the Host Name
  204. * address in the INIT. The receiver of INIT MUST ignore any
  205. * other address types if the Host Name address parameter is
  206. * present in the received INIT chunk.
  207. *
  208. * PLEASE DO NOT FIXME [This version does not support Host Name.]
  209. */
  210. retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
  211. if (!retval)
  212. goto nodata;
  213. retval->subh.init_hdr =
  214. sctp_addto_chunk(retval, sizeof(init), &init);
  215. retval->param_hdr.v =
  216. sctp_addto_chunk(retval, addrs_len, addrs.v);
  217. /* RFC 2960 3.3.2 Initiation (INIT) (1)
  218. *
  219. * Note 4: This parameter, when present, specifies all the
  220. * address types the sending endpoint can support. The absence
  221. * of this parameter indicates that the sending endpoint can
  222. * support any address type.
  223. */
  224. sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
  225. sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
  226. sctp_addto_chunk(retval, sizeof(sat), &sat);
  227. sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
  228. sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
  229. if (sctp_prsctp_enable)
  230. sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
  231. aiparam.param_hdr.type = SCTP_PARAM_ADAPTION_LAYER_IND;
  232. aiparam.param_hdr.length = htons(sizeof(aiparam));
  233. aiparam.adaption_ind = htonl(sp->adaption_ind);
  234. sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
  235. nodata:
  236. if (addrs.v)
  237. kfree(addrs.v);
  238. return retval;
  239. }
  240. struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
  241. const struct sctp_chunk *chunk,
  242. unsigned int __nocast gfp, int unkparam_len)
  243. {
  244. sctp_inithdr_t initack;
  245. struct sctp_chunk *retval;
  246. union sctp_params addrs;
  247. int addrs_len;
  248. sctp_cookie_param_t *cookie;
  249. int cookie_len;
  250. size_t chunksize;
  251. sctp_adaption_ind_param_t aiparam;
  252. retval = NULL;
  253. /* Note: there may be no addresses to embed. */
  254. addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
  255. initack.init_tag = htonl(asoc->c.my_vtag);
  256. initack.a_rwnd = htonl(asoc->rwnd);
  257. initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
  258. initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
  259. initack.initial_tsn = htonl(asoc->c.initial_tsn);
  260. /* FIXME: We really ought to build the cookie right
  261. * into the packet instead of allocating more fresh memory.
  262. */
  263. cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
  264. addrs.v, addrs_len);
  265. if (!cookie)
  266. goto nomem_cookie;
  267. /* Calculate the total size of allocation, include the reserved
  268. * space for reporting unknown parameters if it is specified.
  269. */
  270. chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
  271. /* Tell peer that we'll do ECN only if peer advertised such cap. */
  272. if (asoc->peer.ecn_capable)
  273. chunksize += sizeof(ecap_param);
  274. /* Tell peer that we'll do PR-SCTP only if peer advertised. */
  275. if (asoc->peer.prsctp_capable)
  276. chunksize += sizeof(prsctp_param);
  277. chunksize += sizeof(aiparam);
  278. /* Now allocate and fill out the chunk. */
  279. retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
  280. if (!retval)
  281. goto nomem_chunk;
  282. /* Per the advice in RFC 2960 6.4, send this reply to
  283. * the source of the INIT packet.
  284. */
  285. retval->transport = chunk->transport;
  286. retval->subh.init_hdr =
  287. sctp_addto_chunk(retval, sizeof(initack), &initack);
  288. retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
  289. sctp_addto_chunk(retval, cookie_len, cookie);
  290. if (asoc->peer.ecn_capable)
  291. sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
  292. if (asoc->peer.prsctp_capable)
  293. sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
  294. aiparam.param_hdr.type = SCTP_PARAM_ADAPTION_LAYER_IND;
  295. aiparam.param_hdr.length = htons(sizeof(aiparam));
  296. aiparam.adaption_ind = htonl(sctp_sk(asoc->base.sk)->adaption_ind);
  297. sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
  298. /* We need to remove the const qualifier at this point. */
  299. retval->asoc = (struct sctp_association *) asoc;
  300. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  301. *
  302. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  303. * HEARTBEAT ACK, * etc.) to the same destination transport
  304. * address from which it received the DATA or control chunk
  305. * to which it is replying.
  306. *
  307. * [INIT ACK back to where the INIT came from.]
  308. */
  309. if (chunk)
  310. retval->transport = chunk->transport;
  311. nomem_chunk:
  312. kfree(cookie);
  313. nomem_cookie:
  314. if (addrs.v)
  315. kfree(addrs.v);
  316. return retval;
  317. }
  318. /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
  319. *
  320. * This chunk is used only during the initialization of an association.
  321. * It is sent by the initiator of an association to its peer to complete
  322. * the initialization process. This chunk MUST precede any DATA chunk
  323. * sent within the association, but MAY be bundled with one or more DATA
  324. * chunks in the same packet.
  325. *
  326. * 0 1 2 3
  327. * 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
  328. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  329. * | Type = 10 |Chunk Flags | Length |
  330. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  331. * / Cookie /
  332. * \ \
  333. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  334. *
  335. * Chunk Flags: 8 bit
  336. *
  337. * Set to zero on transmit and ignored on receipt.
  338. *
  339. * Length: 16 bits (unsigned integer)
  340. *
  341. * Set to the size of the chunk in bytes, including the 4 bytes of
  342. * the chunk header and the size of the Cookie.
  343. *
  344. * Cookie: variable size
  345. *
  346. * This field must contain the exact cookie received in the
  347. * State Cookie parameter from the previous INIT ACK.
  348. *
  349. * An implementation SHOULD make the cookie as small as possible
  350. * to insure interoperability.
  351. */
  352. struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
  353. const struct sctp_chunk *chunk)
  354. {
  355. struct sctp_chunk *retval;
  356. void *cookie;
  357. int cookie_len;
  358. cookie = asoc->peer.cookie;
  359. cookie_len = asoc->peer.cookie_len;
  360. /* Build a cookie echo chunk. */
  361. retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
  362. if (!retval)
  363. goto nodata;
  364. retval->subh.cookie_hdr =
  365. sctp_addto_chunk(retval, cookie_len, cookie);
  366. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  367. *
  368. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  369. * HEARTBEAT ACK, * etc.) to the same destination transport
  370. * address from which it * received the DATA or control chunk
  371. * to which it is replying.
  372. *
  373. * [COOKIE ECHO back to where the INIT ACK came from.]
  374. */
  375. if (chunk)
  376. retval->transport = chunk->transport;
  377. nodata:
  378. return retval;
  379. }
  380. /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
  381. *
  382. * This chunk is used only during the initialization of an
  383. * association. It is used to acknowledge the receipt of a COOKIE
  384. * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
  385. * within the association, but MAY be bundled with one or more DATA
  386. * chunks or SACK chunk in the same SCTP packet.
  387. *
  388. * 0 1 2 3
  389. * 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
  390. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  391. * | Type = 11 |Chunk Flags | Length = 4 |
  392. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  393. *
  394. * Chunk Flags: 8 bits
  395. *
  396. * Set to zero on transmit and ignored on receipt.
  397. */
  398. struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
  399. const struct sctp_chunk *chunk)
  400. {
  401. struct sctp_chunk *retval;
  402. retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
  403. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  404. *
  405. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  406. * HEARTBEAT ACK, * etc.) to the same destination transport
  407. * address from which it * received the DATA or control chunk
  408. * to which it is replying.
  409. *
  410. * [COOKIE ACK back to where the COOKIE ECHO came from.]
  411. */
  412. if (retval && chunk)
  413. retval->transport = chunk->transport;
  414. return retval;
  415. }
  416. /*
  417. * Appendix A: Explicit Congestion Notification:
  418. * CWR:
  419. *
  420. * RFC 2481 details a specific bit for a sender to send in the header of
  421. * its next outbound TCP segment to indicate to its peer that it has
  422. * reduced its congestion window. This is termed the CWR bit. For
  423. * SCTP the same indication is made by including the CWR chunk.
  424. * This chunk contains one data element, i.e. the TSN number that
  425. * was sent in the ECNE chunk. This element represents the lowest
  426. * TSN number in the datagram that was originally marked with the
  427. * CE bit.
  428. *
  429. * 0 1 2 3
  430. * 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
  431. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  432. * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
  433. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  434. * | Lowest TSN Number |
  435. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  436. *
  437. * Note: The CWR is considered a Control chunk.
  438. */
  439. struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
  440. const __u32 lowest_tsn,
  441. const struct sctp_chunk *chunk)
  442. {
  443. struct sctp_chunk *retval;
  444. sctp_cwrhdr_t cwr;
  445. cwr.lowest_tsn = htonl(lowest_tsn);
  446. retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
  447. sizeof(sctp_cwrhdr_t));
  448. if (!retval)
  449. goto nodata;
  450. retval->subh.ecn_cwr_hdr =
  451. sctp_addto_chunk(retval, sizeof(cwr), &cwr);
  452. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  453. *
  454. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  455. * HEARTBEAT ACK, * etc.) to the same destination transport
  456. * address from which it * received the DATA or control chunk
  457. * to which it is replying.
  458. *
  459. * [Report a reduced congestion window back to where the ECNE
  460. * came from.]
  461. */
  462. if (chunk)
  463. retval->transport = chunk->transport;
  464. nodata:
  465. return retval;
  466. }
  467. /* Make an ECNE chunk. This is a congestion experienced report. */
  468. struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
  469. const __u32 lowest_tsn)
  470. {
  471. struct sctp_chunk *retval;
  472. sctp_ecnehdr_t ecne;
  473. ecne.lowest_tsn = htonl(lowest_tsn);
  474. retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
  475. sizeof(sctp_ecnehdr_t));
  476. if (!retval)
  477. goto nodata;
  478. retval->subh.ecne_hdr =
  479. sctp_addto_chunk(retval, sizeof(ecne), &ecne);
  480. nodata:
  481. return retval;
  482. }
  483. /* Make a DATA chunk for the given association from the provided
  484. * parameters. However, do not populate the data payload.
  485. */
  486. struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
  487. const struct sctp_sndrcvinfo *sinfo,
  488. int data_len, __u8 flags, __u16 ssn)
  489. {
  490. struct sctp_chunk *retval;
  491. struct sctp_datahdr dp;
  492. int chunk_len;
  493. /* We assign the TSN as LATE as possible, not here when
  494. * creating the chunk.
  495. */
  496. dp.tsn = 0;
  497. dp.stream = htons(sinfo->sinfo_stream);
  498. dp.ppid = sinfo->sinfo_ppid;
  499. /* Set the flags for an unordered send. */
  500. if (sinfo->sinfo_flags & MSG_UNORDERED) {
  501. flags |= SCTP_DATA_UNORDERED;
  502. dp.ssn = 0;
  503. } else
  504. dp.ssn = htons(ssn);
  505. chunk_len = sizeof(dp) + data_len;
  506. retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
  507. if (!retval)
  508. goto nodata;
  509. retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
  510. memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
  511. nodata:
  512. return retval;
  513. }
  514. /* Create a selective ackowledgement (SACK) for the given
  515. * association. This reports on which TSN's we've seen to date,
  516. * including duplicates and gaps.
  517. */
  518. struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
  519. {
  520. struct sctp_chunk *retval;
  521. struct sctp_sackhdr sack;
  522. int len;
  523. __u32 ctsn;
  524. __u16 num_gabs, num_dup_tsns;
  525. struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
  526. ctsn = sctp_tsnmap_get_ctsn(map);
  527. SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
  528. /* How much room is needed in the chunk? */
  529. num_gabs = sctp_tsnmap_num_gabs(map);
  530. num_dup_tsns = sctp_tsnmap_num_dups(map);
  531. /* Initialize the SACK header. */
  532. sack.cum_tsn_ack = htonl(ctsn);
  533. sack.a_rwnd = htonl(asoc->a_rwnd);
  534. sack.num_gap_ack_blocks = htons(num_gabs);
  535. sack.num_dup_tsns = htons(num_dup_tsns);
  536. len = sizeof(sack)
  537. + sizeof(struct sctp_gap_ack_block) * num_gabs
  538. + sizeof(__u32) * num_dup_tsns;
  539. /* Create the chunk. */
  540. retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
  541. if (!retval)
  542. goto nodata;
  543. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  544. *
  545. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  546. * HEARTBEAT ACK, etc.) to the same destination transport
  547. * address from which it received the DATA or control chunk to
  548. * which it is replying. This rule should also be followed if
  549. * the endpoint is bundling DATA chunks together with the
  550. * reply chunk.
  551. *
  552. * However, when acknowledging multiple DATA chunks received
  553. * in packets from different source addresses in a single
  554. * SACK, the SACK chunk may be transmitted to one of the
  555. * destination transport addresses from which the DATA or
  556. * control chunks being acknowledged were received.
  557. *
  558. * [BUG: We do not implement the following paragraph.
  559. * Perhaps we should remember the last transport we used for a
  560. * SACK and avoid that (if possible) if we have seen any
  561. * duplicates. --piggy]
  562. *
  563. * When a receiver of a duplicate DATA chunk sends a SACK to a
  564. * multi- homed endpoint it MAY be beneficial to vary the
  565. * destination address and not use the source address of the
  566. * DATA chunk. The reason being that receiving a duplicate
  567. * from a multi-homed endpoint might indicate that the return
  568. * path (as specified in the source address of the DATA chunk)
  569. * for the SACK is broken.
  570. *
  571. * [Send to the address from which we last received a DATA chunk.]
  572. */
  573. retval->transport = asoc->peer.last_data_from;
  574. retval->subh.sack_hdr =
  575. sctp_addto_chunk(retval, sizeof(sack), &sack);
  576. /* Add the gap ack block information. */
  577. if (num_gabs)
  578. sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
  579. sctp_tsnmap_get_gabs(map));
  580. /* Add the duplicate TSN information. */
  581. if (num_dup_tsns)
  582. sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
  583. sctp_tsnmap_get_dups(map));
  584. nodata:
  585. return retval;
  586. }
  587. /* Make a SHUTDOWN chunk. */
  588. struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
  589. const struct sctp_chunk *chunk)
  590. {
  591. struct sctp_chunk *retval;
  592. sctp_shutdownhdr_t shut;
  593. __u32 ctsn;
  594. ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
  595. shut.cum_tsn_ack = htonl(ctsn);
  596. retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
  597. sizeof(sctp_shutdownhdr_t));
  598. if (!retval)
  599. goto nodata;
  600. retval->subh.shutdown_hdr =
  601. sctp_addto_chunk(retval, sizeof(shut), &shut);
  602. if (chunk)
  603. retval->transport = chunk->transport;
  604. nodata:
  605. return retval;
  606. }
  607. struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
  608. const struct sctp_chunk *chunk)
  609. {
  610. struct sctp_chunk *retval;
  611. retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
  612. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  613. *
  614. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  615. * HEARTBEAT ACK, * etc.) to the same destination transport
  616. * address from which it * received the DATA or control chunk
  617. * to which it is replying.
  618. *
  619. * [ACK back to where the SHUTDOWN came from.]
  620. */
  621. if (retval && chunk)
  622. retval->transport = chunk->transport;
  623. return retval;
  624. }
  625. struct sctp_chunk *sctp_make_shutdown_complete(
  626. const struct sctp_association *asoc,
  627. const struct sctp_chunk *chunk)
  628. {
  629. struct sctp_chunk *retval;
  630. __u8 flags = 0;
  631. /* Set the T-bit if we have no association (vtag will be
  632. * reflected)
  633. */
  634. flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
  635. retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
  636. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  637. *
  638. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  639. * HEARTBEAT ACK, * etc.) to the same destination transport
  640. * address from which it * received the DATA or control chunk
  641. * to which it is replying.
  642. *
  643. * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
  644. * came from.]
  645. */
  646. if (retval && chunk)
  647. retval->transport = chunk->transport;
  648. return retval;
  649. }
  650. /* Create an ABORT. Note that we set the T bit if we have no
  651. * association, except when responding to an INIT (sctpimpguide 2.41).
  652. */
  653. struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
  654. const struct sctp_chunk *chunk,
  655. const size_t hint)
  656. {
  657. struct sctp_chunk *retval;
  658. __u8 flags = 0;
  659. /* Set the T-bit if we have no association and 'chunk' is not
  660. * an INIT (vtag will be reflected).
  661. */
  662. if (!asoc) {
  663. if (chunk && chunk->chunk_hdr &&
  664. chunk->chunk_hdr->type == SCTP_CID_INIT)
  665. flags = 0;
  666. else
  667. flags = SCTP_CHUNK_FLAG_T;
  668. }
  669. retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
  670. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  671. *
  672. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  673. * HEARTBEAT ACK, * etc.) to the same destination transport
  674. * address from which it * received the DATA or control chunk
  675. * to which it is replying.
  676. *
  677. * [ABORT back to where the offender came from.]
  678. */
  679. if (retval && chunk)
  680. retval->transport = chunk->transport;
  681. return retval;
  682. }
  683. /* Helper to create ABORT with a NO_USER_DATA error. */
  684. struct sctp_chunk *sctp_make_abort_no_data(
  685. const struct sctp_association *asoc,
  686. const struct sctp_chunk *chunk, __u32 tsn)
  687. {
  688. struct sctp_chunk *retval;
  689. __u32 payload;
  690. retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
  691. + sizeof(tsn));
  692. if (!retval)
  693. goto no_mem;
  694. /* Put the tsn back into network byte order. */
  695. payload = htonl(tsn);
  696. sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
  697. sizeof(payload));
  698. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  699. *
  700. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  701. * HEARTBEAT ACK, * etc.) to the same destination transport
  702. * address from which it * received the DATA or control chunk
  703. * to which it is replying.
  704. *
  705. * [ABORT back to where the offender came from.]
  706. */
  707. if (chunk)
  708. retval->transport = chunk->transport;
  709. no_mem:
  710. return retval;
  711. }
  712. /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
  713. struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
  714. const struct sctp_chunk *chunk,
  715. const struct msghdr *msg)
  716. {
  717. struct sctp_chunk *retval;
  718. void *payload = NULL, *payoff;
  719. size_t paylen = 0;
  720. struct iovec *iov = NULL;
  721. int iovlen = 0;
  722. if (msg) {
  723. iov = msg->msg_iov;
  724. iovlen = msg->msg_iovlen;
  725. paylen = get_user_iov_size(iov, iovlen);
  726. }
  727. retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
  728. if (!retval)
  729. goto err_chunk;
  730. if (paylen) {
  731. /* Put the msg_iov together into payload. */
  732. payload = kmalloc(paylen, GFP_ATOMIC);
  733. if (!payload)
  734. goto err_payload;
  735. payoff = payload;
  736. for (; iovlen > 0; --iovlen) {
  737. if (copy_from_user(payoff, iov->iov_base,iov->iov_len))
  738. goto err_copy;
  739. payoff += iov->iov_len;
  740. iov++;
  741. }
  742. }
  743. sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
  744. if (paylen)
  745. kfree(payload);
  746. return retval;
  747. err_copy:
  748. kfree(payload);
  749. err_payload:
  750. sctp_chunk_free(retval);
  751. retval = NULL;
  752. err_chunk:
  753. return retval;
  754. }
  755. /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
  756. struct sctp_chunk *sctp_make_abort_violation(
  757. const struct sctp_association *asoc,
  758. const struct sctp_chunk *chunk,
  759. const __u8 *payload,
  760. const size_t paylen)
  761. {
  762. struct sctp_chunk *retval;
  763. struct sctp_paramhdr phdr;
  764. retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
  765. + sizeof(sctp_chunkhdr_t));
  766. if (!retval)
  767. goto end;
  768. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, payload, paylen);
  769. phdr.type = htons(chunk->chunk_hdr->type);
  770. phdr.length = chunk->chunk_hdr->length;
  771. sctp_addto_chunk(retval, sizeof(sctp_paramhdr_t), &phdr);
  772. end:
  773. return retval;
  774. }
  775. /* Make a HEARTBEAT chunk. */
  776. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  777. const struct sctp_transport *transport,
  778. const void *payload, const size_t paylen)
  779. {
  780. struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
  781. 0, paylen);
  782. if (!retval)
  783. goto nodata;
  784. /* Cast away the 'const', as this is just telling the chunk
  785. * what transport it belongs to.
  786. */
  787. retval->transport = (struct sctp_transport *) transport;
  788. retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
  789. nodata:
  790. return retval;
  791. }
  792. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  793. const struct sctp_chunk *chunk,
  794. const void *payload, const size_t paylen)
  795. {
  796. struct sctp_chunk *retval;
  797. retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
  798. if (!retval)
  799. goto nodata;
  800. retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
  801. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  802. *
  803. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  804. * HEARTBEAT ACK, * etc.) to the same destination transport
  805. * address from which it * received the DATA or control chunk
  806. * to which it is replying.
  807. *
  808. * [HBACK back to where the HEARTBEAT came from.]
  809. */
  810. if (chunk)
  811. retval->transport = chunk->transport;
  812. nodata:
  813. return retval;
  814. }
  815. /* Create an Operation Error chunk with the specified space reserved.
  816. * This routine can be used for containing multiple causes in the chunk.
  817. */
  818. static struct sctp_chunk *sctp_make_op_error_space(
  819. const struct sctp_association *asoc,
  820. const struct sctp_chunk *chunk,
  821. size_t size)
  822. {
  823. struct sctp_chunk *retval;
  824. retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
  825. sizeof(sctp_errhdr_t) + size);
  826. if (!retval)
  827. goto nodata;
  828. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  829. *
  830. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  831. * HEARTBEAT ACK, etc.) to the same destination transport
  832. * address from which it received the DATA or control chunk
  833. * to which it is replying.
  834. *
  835. */
  836. if (chunk)
  837. retval->transport = chunk->transport;
  838. nodata:
  839. return retval;
  840. }
  841. /* Create an Operation Error chunk. */
  842. struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
  843. const struct sctp_chunk *chunk,
  844. __u16 cause_code, const void *payload,
  845. size_t paylen)
  846. {
  847. struct sctp_chunk *retval;
  848. retval = sctp_make_op_error_space(asoc, chunk, paylen);
  849. if (!retval)
  850. goto nodata;
  851. sctp_init_cause(retval, cause_code, payload, paylen);
  852. nodata:
  853. return retval;
  854. }
  855. /********************************************************************
  856. * 2nd Level Abstractions
  857. ********************************************************************/
  858. /* Turn an skb into a chunk.
  859. * FIXME: Eventually move the structure directly inside the skb->cb[].
  860. */
  861. struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
  862. const struct sctp_association *asoc,
  863. struct sock *sk)
  864. {
  865. struct sctp_chunk *retval;
  866. retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
  867. if (!retval)
  868. goto nodata;
  869. memset(retval, 0, sizeof(struct sctp_chunk));
  870. if (!sk) {
  871. SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
  872. }
  873. INIT_LIST_HEAD(&retval->list);
  874. retval->skb = skb;
  875. retval->asoc = (struct sctp_association *)asoc;
  876. retval->resent = 0;
  877. retval->has_tsn = 0;
  878. retval->has_ssn = 0;
  879. retval->rtt_in_progress = 0;
  880. retval->sent_at = 0;
  881. retval->singleton = 1;
  882. retval->end_of_packet = 0;
  883. retval->ecn_ce_done = 0;
  884. retval->pdiscard = 0;
  885. /* sctpimpguide-05.txt Section 2.8.2
  886. * M1) Each time a new DATA chunk is transmitted
  887. * set the 'TSN.Missing.Report' count for that TSN to 0. The
  888. * 'TSN.Missing.Report' count will be used to determine missing chunks
  889. * and when to fast retransmit.
  890. */
  891. retval->tsn_missing_report = 0;
  892. retval->tsn_gap_acked = 0;
  893. retval->fast_retransmit = 0;
  894. /* If this is a fragmented message, track all fragments
  895. * of the message (for SEND_FAILED).
  896. */
  897. retval->msg = NULL;
  898. /* Polish the bead hole. */
  899. INIT_LIST_HEAD(&retval->transmitted_list);
  900. INIT_LIST_HEAD(&retval->frag_list);
  901. SCTP_DBG_OBJCNT_INC(chunk);
  902. atomic_set(&retval->refcnt, 1);
  903. nodata:
  904. return retval;
  905. }
  906. /* Set chunk->source and dest based on the IP header in chunk->skb. */
  907. void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
  908. union sctp_addr *dest)
  909. {
  910. memcpy(&chunk->source, src, sizeof(union sctp_addr));
  911. memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
  912. }
  913. /* Extract the source address from a chunk. */
  914. const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
  915. {
  916. /* If we have a known transport, use that. */
  917. if (chunk->transport) {
  918. return &chunk->transport->ipaddr;
  919. } else {
  920. /* Otherwise, extract it from the IP header. */
  921. return &chunk->source;
  922. }
  923. }
  924. /* Create a new chunk, setting the type and flags headers from the
  925. * arguments, reserving enough space for a 'paylen' byte payload.
  926. */
  927. SCTP_STATIC
  928. struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
  929. __u8 type, __u8 flags, int paylen)
  930. {
  931. struct sctp_chunk *retval;
  932. sctp_chunkhdr_t *chunk_hdr;
  933. struct sk_buff *skb;
  934. struct sock *sk;
  935. /* No need to allocate LL here, as this is only a chunk. */
  936. skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
  937. GFP_ATOMIC);
  938. if (!skb)
  939. goto nodata;
  940. /* Make room for the chunk header. */
  941. chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
  942. chunk_hdr->type = type;
  943. chunk_hdr->flags = flags;
  944. chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
  945. sk = asoc ? asoc->base.sk : NULL;
  946. retval = sctp_chunkify(skb, asoc, sk);
  947. if (!retval) {
  948. kfree_skb(skb);
  949. goto nodata;
  950. }
  951. retval->chunk_hdr = chunk_hdr;
  952. retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
  953. /* Set the skb to the belonging sock for accounting. */
  954. skb->sk = sk;
  955. return retval;
  956. nodata:
  957. return NULL;
  958. }
  959. /* Release the memory occupied by a chunk. */
  960. static void sctp_chunk_destroy(struct sctp_chunk *chunk)
  961. {
  962. /* Free the chunk skb data and the SCTP_chunk stub itself. */
  963. dev_kfree_skb(chunk->skb);
  964. SCTP_DBG_OBJCNT_DEC(chunk);
  965. kmem_cache_free(sctp_chunk_cachep, chunk);
  966. }
  967. /* Possibly, free the chunk. */
  968. void sctp_chunk_free(struct sctp_chunk *chunk)
  969. {
  970. BUG_ON(!list_empty(&chunk->list));
  971. list_del_init(&chunk->transmitted_list);
  972. /* Release our reference on the message tracker. */
  973. if (chunk->msg)
  974. sctp_datamsg_put(chunk->msg);
  975. sctp_chunk_put(chunk);
  976. }
  977. /* Grab a reference to the chunk. */
  978. void sctp_chunk_hold(struct sctp_chunk *ch)
  979. {
  980. atomic_inc(&ch->refcnt);
  981. }
  982. /* Release a reference to the chunk. */
  983. void sctp_chunk_put(struct sctp_chunk *ch)
  984. {
  985. if (atomic_dec_and_test(&ch->refcnt))
  986. sctp_chunk_destroy(ch);
  987. }
  988. /* Append bytes to the end of a chunk. Will panic if chunk is not big
  989. * enough.
  990. */
  991. void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
  992. {
  993. void *target;
  994. void *padding;
  995. int chunklen = ntohs(chunk->chunk_hdr->length);
  996. int padlen = chunklen % 4;
  997. padding = skb_put(chunk->skb, padlen);
  998. target = skb_put(chunk->skb, len);
  999. memset(padding, 0, padlen);
  1000. memcpy(target, data, len);
  1001. /* Adjust the chunk length field. */
  1002. chunk->chunk_hdr->length = htons(chunklen + padlen + len);
  1003. chunk->chunk_end = chunk->skb->tail;
  1004. return target;
  1005. }
  1006. /* Append bytes from user space to the end of a chunk. Will panic if
  1007. * chunk is not big enough.
  1008. * Returns a kernel err value.
  1009. */
  1010. int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
  1011. struct iovec *data)
  1012. {
  1013. __u8 *target;
  1014. int err = 0;
  1015. /* Make room in chunk for data. */
  1016. target = skb_put(chunk->skb, len);
  1017. /* Copy data (whole iovec) into chunk */
  1018. if ((err = memcpy_fromiovecend(target, data, off, len)))
  1019. goto out;
  1020. /* Adjust the chunk length field. */
  1021. chunk->chunk_hdr->length =
  1022. htons(ntohs(chunk->chunk_hdr->length) + len);
  1023. chunk->chunk_end = chunk->skb->tail;
  1024. out:
  1025. return err;
  1026. }
  1027. /* Helper function to assign a TSN if needed. This assumes that both
  1028. * the data_hdr and association have already been assigned.
  1029. */
  1030. void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
  1031. {
  1032. __u16 ssn;
  1033. __u16 sid;
  1034. if (chunk->has_ssn)
  1035. return;
  1036. /* This is the last possible instant to assign a SSN. */
  1037. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  1038. ssn = 0;
  1039. } else {
  1040. sid = htons(chunk->subh.data_hdr->stream);
  1041. if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
  1042. ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
  1043. else
  1044. ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
  1045. ssn = htons(ssn);
  1046. }
  1047. chunk->subh.data_hdr->ssn = ssn;
  1048. chunk->has_ssn = 1;
  1049. }
  1050. /* Helper function to assign a TSN if needed. This assumes that both
  1051. * the data_hdr and association have already been assigned.
  1052. */
  1053. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
  1054. {
  1055. if (!chunk->has_tsn) {
  1056. /* This is the last possible instant to
  1057. * assign a TSN.
  1058. */
  1059. chunk->subh.data_hdr->tsn =
  1060. htonl(sctp_association_get_next_tsn(chunk->asoc));
  1061. chunk->has_tsn = 1;
  1062. }
  1063. }
  1064. /* Create a CLOSED association to use with an incoming packet. */
  1065. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
  1066. struct sctp_chunk *chunk,
  1067. unsigned int __nocast gfp)
  1068. {
  1069. struct sctp_association *asoc;
  1070. struct sk_buff *skb;
  1071. sctp_scope_t scope;
  1072. struct sctp_af *af;
  1073. /* Create the bare association. */
  1074. scope = sctp_scope(sctp_source(chunk));
  1075. asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1076. if (!asoc)
  1077. goto nodata;
  1078. asoc->temp = 1;
  1079. skb = chunk->skb;
  1080. /* Create an entry for the source address of the packet. */
  1081. af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
  1082. if (unlikely(!af))
  1083. goto fail;
  1084. af->from_skb(&asoc->c.peer_addr, skb, 1);
  1085. nodata:
  1086. return asoc;
  1087. fail:
  1088. sctp_association_free(asoc);
  1089. return NULL;
  1090. }
  1091. /* Build a cookie representing asoc.
  1092. * This INCLUDES the param header needed to put the cookie in the INIT ACK.
  1093. */
  1094. static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
  1095. const struct sctp_association *asoc,
  1096. const struct sctp_chunk *init_chunk,
  1097. int *cookie_len,
  1098. const __u8 *raw_addrs, int addrs_len)
  1099. {
  1100. sctp_cookie_param_t *retval;
  1101. struct sctp_signed_cookie *cookie;
  1102. struct scatterlist sg;
  1103. int headersize, bodysize;
  1104. unsigned int keylen;
  1105. char *key;
  1106. headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
  1107. bodysize = sizeof(struct sctp_cookie)
  1108. + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
  1109. /* Pad out the cookie to a multiple to make the signature
  1110. * functions simpler to write.
  1111. */
  1112. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1113. bodysize += SCTP_COOKIE_MULTIPLE
  1114. - (bodysize % SCTP_COOKIE_MULTIPLE);
  1115. *cookie_len = headersize + bodysize;
  1116. retval = (sctp_cookie_param_t *)kmalloc(*cookie_len, GFP_ATOMIC);
  1117. if (!retval) {
  1118. *cookie_len = 0;
  1119. goto nodata;
  1120. }
  1121. /* Clear this memory since we are sending this data structure
  1122. * out on the network.
  1123. */
  1124. memset(retval, 0x00, *cookie_len);
  1125. cookie = (struct sctp_signed_cookie *) retval->body;
  1126. /* Set up the parameter header. */
  1127. retval->p.type = SCTP_PARAM_STATE_COOKIE;
  1128. retval->p.length = htons(*cookie_len);
  1129. /* Copy the cookie part of the association itself. */
  1130. cookie->c = asoc->c;
  1131. /* Save the raw address list length in the cookie. */
  1132. cookie->c.raw_addr_list_len = addrs_len;
  1133. /* Remember PR-SCTP capability. */
  1134. cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
  1135. /* Save adaption indication in the cookie. */
  1136. cookie->c.adaption_ind = asoc->peer.adaption_ind;
  1137. /* Set an expiration time for the cookie. */
  1138. do_gettimeofday(&cookie->c.expiration);
  1139. TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
  1140. /* Copy the peer's init packet. */
  1141. memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
  1142. ntohs(init_chunk->chunk_hdr->length));
  1143. /* Copy the raw local address list of the association. */
  1144. memcpy((__u8 *)&cookie->c.peer_init[0] +
  1145. ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
  1146. if (sctp_sk(ep->base.sk)->hmac) {
  1147. /* Sign the message. */
  1148. sg.page = virt_to_page(&cookie->c);
  1149. sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
  1150. sg.length = bodysize;
  1151. keylen = SCTP_SECRET_SIZE;
  1152. key = (char *)ep->secret_key[ep->current_key];
  1153. sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
  1154. &sg, 1, cookie->signature);
  1155. }
  1156. nodata:
  1157. return retval;
  1158. }
  1159. /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
  1160. struct sctp_association *sctp_unpack_cookie(
  1161. const struct sctp_endpoint *ep,
  1162. const struct sctp_association *asoc,
  1163. struct sctp_chunk *chunk, unsigned int __nocast gfp,
  1164. int *error, struct sctp_chunk **errp)
  1165. {
  1166. struct sctp_association *retval = NULL;
  1167. struct sctp_signed_cookie *cookie;
  1168. struct sctp_cookie *bear_cookie;
  1169. int headersize, bodysize, fixed_size;
  1170. __u8 digest[SCTP_SIGNATURE_SIZE];
  1171. struct scatterlist sg;
  1172. unsigned int keylen, len;
  1173. char *key;
  1174. sctp_scope_t scope;
  1175. struct sk_buff *skb = chunk->skb;
  1176. struct timeval tv;
  1177. headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
  1178. bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
  1179. fixed_size = headersize + sizeof(struct sctp_cookie);
  1180. /* Verify that the chunk looks like it even has a cookie.
  1181. * There must be enough room for our cookie and our peer's
  1182. * INIT chunk.
  1183. */
  1184. len = ntohs(chunk->chunk_hdr->length);
  1185. if (len < fixed_size + sizeof(struct sctp_chunkhdr))
  1186. goto malformed;
  1187. /* Verify that the cookie has been padded out. */
  1188. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1189. goto malformed;
  1190. /* Process the cookie. */
  1191. cookie = chunk->subh.cookie_hdr;
  1192. bear_cookie = &cookie->c;
  1193. if (!sctp_sk(ep->base.sk)->hmac)
  1194. goto no_hmac;
  1195. /* Check the signature. */
  1196. keylen = SCTP_SECRET_SIZE;
  1197. sg.page = virt_to_page(bear_cookie);
  1198. sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
  1199. sg.length = bodysize;
  1200. key = (char *)ep->secret_key[ep->current_key];
  1201. memset(digest, 0x00, sizeof(digest));
  1202. sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
  1203. 1, digest);
  1204. if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
  1205. /* Try the previous key. */
  1206. key = (char *)ep->secret_key[ep->last_key];
  1207. memset(digest, 0x00, sizeof(digest));
  1208. sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
  1209. &sg, 1, digest);
  1210. if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
  1211. /* Yikes! Still bad signature! */
  1212. *error = -SCTP_IERROR_BAD_SIG;
  1213. goto fail;
  1214. }
  1215. }
  1216. no_hmac:
  1217. /* IG Section 2.35.2:
  1218. * 3) Compare the port numbers and the verification tag contained
  1219. * within the COOKIE ECHO chunk to the actual port numbers and the
  1220. * verification tag within the SCTP common header of the received
  1221. * packet. If these values do not match the packet MUST be silently
  1222. * discarded,
  1223. */
  1224. if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
  1225. *error = -SCTP_IERROR_BAD_TAG;
  1226. goto fail;
  1227. }
  1228. if (ntohs(chunk->sctp_hdr->source) != bear_cookie->peer_addr.v4.sin_port ||
  1229. ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
  1230. *error = -SCTP_IERROR_BAD_PORTS;
  1231. goto fail;
  1232. }
  1233. /* Check to see if the cookie is stale. If there is already
  1234. * an association, there is no need to check cookie's expiration
  1235. * for init collision case of lost COOKIE ACK.
  1236. */
  1237. skb_get_timestamp(skb, &tv);
  1238. if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
  1239. __u16 len;
  1240. /*
  1241. * Section 3.3.10.3 Stale Cookie Error (3)
  1242. *
  1243. * Cause of error
  1244. * ---------------
  1245. * Stale Cookie Error: Indicates the receipt of a valid State
  1246. * Cookie that has expired.
  1247. */
  1248. len = ntohs(chunk->chunk_hdr->length);
  1249. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1250. if (*errp) {
  1251. suseconds_t usecs = (tv.tv_sec -
  1252. bear_cookie->expiration.tv_sec) * 1000000L +
  1253. tv.tv_usec - bear_cookie->expiration.tv_usec;
  1254. usecs = htonl(usecs);
  1255. sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
  1256. &usecs, sizeof(usecs));
  1257. *error = -SCTP_IERROR_STALE_COOKIE;
  1258. } else
  1259. *error = -SCTP_IERROR_NOMEM;
  1260. goto fail;
  1261. }
  1262. /* Make a new base association. */
  1263. scope = sctp_scope(sctp_source(chunk));
  1264. retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1265. if (!retval) {
  1266. *error = -SCTP_IERROR_NOMEM;
  1267. goto fail;
  1268. }
  1269. /* Set up our peer's port number. */
  1270. retval->peer.port = ntohs(chunk->sctp_hdr->source);
  1271. /* Populate the association from the cookie. */
  1272. memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
  1273. if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
  1274. GFP_ATOMIC) < 0) {
  1275. *error = -SCTP_IERROR_NOMEM;
  1276. goto fail;
  1277. }
  1278. /* Also, add the destination address. */
  1279. if (list_empty(&retval->base.bind_addr.address_list)) {
  1280. sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
  1281. GFP_ATOMIC);
  1282. }
  1283. retval->next_tsn = retval->c.initial_tsn;
  1284. retval->ctsn_ack_point = retval->next_tsn - 1;
  1285. retval->addip_serial = retval->c.initial_tsn;
  1286. retval->adv_peer_ack_point = retval->ctsn_ack_point;
  1287. retval->peer.prsctp_capable = retval->c.prsctp_capable;
  1288. retval->peer.adaption_ind = retval->c.adaption_ind;
  1289. /* The INIT stuff will be done by the side effects. */
  1290. return retval;
  1291. fail:
  1292. if (retval)
  1293. sctp_association_free(retval);
  1294. return NULL;
  1295. malformed:
  1296. /* Yikes! The packet is either corrupt or deliberately
  1297. * malformed.
  1298. */
  1299. *error = -SCTP_IERROR_MALFORMED;
  1300. goto fail;
  1301. }
  1302. /********************************************************************
  1303. * 3rd Level Abstractions
  1304. ********************************************************************/
  1305. struct __sctp_missing {
  1306. __u32 num_missing;
  1307. __u16 type;
  1308. } __attribute__((packed));
  1309. /*
  1310. * Report a missing mandatory parameter.
  1311. */
  1312. static int sctp_process_missing_param(const struct sctp_association *asoc,
  1313. sctp_param_t paramtype,
  1314. struct sctp_chunk *chunk,
  1315. struct sctp_chunk **errp)
  1316. {
  1317. struct __sctp_missing report;
  1318. __u16 len;
  1319. len = WORD_ROUND(sizeof(report));
  1320. /* Make an ERROR chunk, preparing enough room for
  1321. * returning multiple unknown parameters.
  1322. */
  1323. if (!*errp)
  1324. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1325. if (*errp) {
  1326. report.num_missing = htonl(1);
  1327. report.type = paramtype;
  1328. sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
  1329. &report, sizeof(report));
  1330. }
  1331. /* Stop processing this chunk. */
  1332. return 0;
  1333. }
  1334. /* Report an Invalid Mandatory Parameter. */
  1335. static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
  1336. struct sctp_chunk *chunk,
  1337. struct sctp_chunk **errp)
  1338. {
  1339. /* Invalid Mandatory Parameter Error has no payload. */
  1340. if (!*errp)
  1341. *errp = sctp_make_op_error_space(asoc, chunk, 0);
  1342. if (*errp)
  1343. sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
  1344. /* Stop processing this chunk. */
  1345. return 0;
  1346. }
  1347. static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
  1348. struct sctp_paramhdr *param,
  1349. const struct sctp_chunk *chunk,
  1350. struct sctp_chunk **errp)
  1351. {
  1352. char error[] = "The following parameter had invalid length:";
  1353. size_t payload_len = WORD_ROUND(sizeof(error)) +
  1354. sizeof(sctp_paramhdr_t);
  1355. /* Create an error chunk and fill it in with our payload. */
  1356. if (!*errp)
  1357. *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
  1358. if (*errp) {
  1359. sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION, error,
  1360. sizeof(error));
  1361. sctp_addto_chunk(*errp, sizeof(sctp_paramhdr_t), param);
  1362. }
  1363. return 0;
  1364. }
  1365. /* Do not attempt to handle the HOST_NAME parm. However, do
  1366. * send back an indicator to the peer.
  1367. */
  1368. static int sctp_process_hn_param(const struct sctp_association *asoc,
  1369. union sctp_params param,
  1370. struct sctp_chunk *chunk,
  1371. struct sctp_chunk **errp)
  1372. {
  1373. __u16 len = ntohs(param.p->length);
  1374. /* Make an ERROR chunk. */
  1375. if (!*errp)
  1376. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1377. if (*errp)
  1378. sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
  1379. param.v, len);
  1380. /* Stop processing this chunk. */
  1381. return 0;
  1382. }
  1383. /* RFC 3.2.1 & the Implementers Guide 2.2.
  1384. *
  1385. * The Parameter Types are encoded such that the
  1386. * highest-order two bits specify the action that must be
  1387. * taken if the processing endpoint does not recognize the
  1388. * Parameter Type.
  1389. *
  1390. * 00 - Stop processing this SCTP chunk and discard it,
  1391. * do not process any further chunks within it.
  1392. *
  1393. * 01 - Stop processing this SCTP chunk and discard it,
  1394. * do not process any further chunks within it, and report
  1395. * the unrecognized parameter in an 'Unrecognized
  1396. * Parameter Type' (in either an ERROR or in the INIT ACK).
  1397. *
  1398. * 10 - Skip this parameter and continue processing.
  1399. *
  1400. * 11 - Skip this parameter and continue processing but
  1401. * report the unrecognized parameter in an
  1402. * 'Unrecognized Parameter Type' (in either an ERROR or in
  1403. * the INIT ACK).
  1404. *
  1405. * Return value:
  1406. * 0 - discard the chunk
  1407. * 1 - continue with the chunk
  1408. */
  1409. static int sctp_process_unk_param(const struct sctp_association *asoc,
  1410. union sctp_params param,
  1411. struct sctp_chunk *chunk,
  1412. struct sctp_chunk **errp)
  1413. {
  1414. int retval = 1;
  1415. switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
  1416. case SCTP_PARAM_ACTION_DISCARD:
  1417. retval = 0;
  1418. break;
  1419. case SCTP_PARAM_ACTION_DISCARD_ERR:
  1420. retval = 0;
  1421. /* Make an ERROR chunk, preparing enough room for
  1422. * returning multiple unknown parameters.
  1423. */
  1424. if (NULL == *errp)
  1425. *errp = sctp_make_op_error_space(asoc, chunk,
  1426. ntohs(chunk->chunk_hdr->length));
  1427. if (*errp)
  1428. sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
  1429. param.v,
  1430. WORD_ROUND(ntohs(param.p->length)));
  1431. break;
  1432. case SCTP_PARAM_ACTION_SKIP:
  1433. break;
  1434. case SCTP_PARAM_ACTION_SKIP_ERR:
  1435. /* Make an ERROR chunk, preparing enough room for
  1436. * returning multiple unknown parameters.
  1437. */
  1438. if (NULL == *errp)
  1439. *errp = sctp_make_op_error_space(asoc, chunk,
  1440. ntohs(chunk->chunk_hdr->length));
  1441. if (*errp) {
  1442. sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
  1443. param.v,
  1444. WORD_ROUND(ntohs(param.p->length)));
  1445. } else {
  1446. /* If there is no memory for generating the ERROR
  1447. * report as specified, an ABORT will be triggered
  1448. * to the peer and the association won't be
  1449. * established.
  1450. */
  1451. retval = 0;
  1452. }
  1453. break;
  1454. default:
  1455. break;
  1456. }
  1457. return retval;
  1458. }
  1459. /* Find unrecognized parameters in the chunk.
  1460. * Return values:
  1461. * 0 - discard the chunk
  1462. * 1 - continue with the chunk
  1463. */
  1464. static int sctp_verify_param(const struct sctp_association *asoc,
  1465. union sctp_params param,
  1466. sctp_cid_t cid,
  1467. struct sctp_chunk *chunk,
  1468. struct sctp_chunk **err_chunk)
  1469. {
  1470. int retval = 1;
  1471. /* FIXME - This routine is not looking at each parameter per the
  1472. * chunk type, i.e., unrecognized parameters should be further
  1473. * identified based on the chunk id.
  1474. */
  1475. switch (param.p->type) {
  1476. case SCTP_PARAM_IPV4_ADDRESS:
  1477. case SCTP_PARAM_IPV6_ADDRESS:
  1478. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  1479. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  1480. case SCTP_PARAM_STATE_COOKIE:
  1481. case SCTP_PARAM_HEARTBEAT_INFO:
  1482. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  1483. case SCTP_PARAM_ECN_CAPABLE:
  1484. case SCTP_PARAM_ADAPTION_LAYER_IND:
  1485. break;
  1486. case SCTP_PARAM_HOST_NAME_ADDRESS:
  1487. /* Tell the peer, we won't support this param. */
  1488. return sctp_process_hn_param(asoc, param, chunk, err_chunk);
  1489. case SCTP_PARAM_FWD_TSN_SUPPORT:
  1490. if (sctp_prsctp_enable)
  1491. break;
  1492. /* Fall Through */
  1493. default:
  1494. SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
  1495. ntohs(param.p->type), cid);
  1496. return sctp_process_unk_param(asoc, param, chunk, err_chunk);
  1497. break;
  1498. }
  1499. return retval;
  1500. }
  1501. /* Verify the INIT packet before we process it. */
  1502. int sctp_verify_init(const struct sctp_association *asoc,
  1503. sctp_cid_t cid,
  1504. sctp_init_chunk_t *peer_init,
  1505. struct sctp_chunk *chunk,
  1506. struct sctp_chunk **errp)
  1507. {
  1508. union sctp_params param;
  1509. int has_cookie = 0;
  1510. /* Verify stream values are non-zero. */
  1511. if ((0 == peer_init->init_hdr.num_outbound_streams) ||
  1512. (0 == peer_init->init_hdr.num_inbound_streams)) {
  1513. sctp_process_inv_mandatory(asoc, chunk, errp);
  1514. return 0;
  1515. }
  1516. /* Check for missing mandatory parameters. */
  1517. sctp_walk_params(param, peer_init, init_hdr.params) {
  1518. if (SCTP_PARAM_STATE_COOKIE == param.p->type)
  1519. has_cookie = 1;
  1520. } /* for (loop through all parameters) */
  1521. /* There is a possibility that a parameter length was bad and
  1522. * in that case we would have stoped walking the parameters.
  1523. * The current param.p would point at the bad one.
  1524. * Current consensus on the mailing list is to generate a PROTOCOL
  1525. * VIOLATION error. We build the ERROR chunk here and let the normal
  1526. * error handling code build and send the packet.
  1527. */
  1528. if (param.v < (void*)chunk->chunk_end - sizeof(sctp_paramhdr_t)) {
  1529. sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
  1530. return 0;
  1531. }
  1532. /* The only missing mandatory param possible today is
  1533. * the state cookie for an INIT-ACK chunk.
  1534. */
  1535. if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
  1536. sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
  1537. chunk, errp);
  1538. return 0;
  1539. }
  1540. /* Find unrecognized parameters. */
  1541. sctp_walk_params(param, peer_init, init_hdr.params) {
  1542. if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
  1543. if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
  1544. return 0;
  1545. else
  1546. return 1;
  1547. }
  1548. } /* for (loop through all parameters) */
  1549. return 1;
  1550. }
  1551. /* Unpack the parameters in an INIT packet into an association.
  1552. * Returns 0 on failure, else success.
  1553. * FIXME: This is an association method.
  1554. */
  1555. int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
  1556. const union sctp_addr *peer_addr,
  1557. sctp_init_chunk_t *peer_init, unsigned int __nocast gfp)
  1558. {
  1559. union sctp_params param;
  1560. struct sctp_transport *transport;
  1561. struct list_head *pos, *temp;
  1562. char *cookie;
  1563. /* We must include the address that the INIT packet came from.
  1564. * This is the only address that matters for an INIT packet.
  1565. * When processing a COOKIE ECHO, we retrieve the from address
  1566. * of the INIT from the cookie.
  1567. */
  1568. /* This implementation defaults to making the first transport
  1569. * added as the primary transport. The source address seems to
  1570. * be a a better choice than any of the embedded addresses.
  1571. */
  1572. if (peer_addr)
  1573. if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
  1574. goto nomem;
  1575. /* Process the initialization parameters. */
  1576. sctp_walk_params(param, peer_init, init_hdr.params) {
  1577. if (!sctp_process_param(asoc, param, peer_addr, gfp))
  1578. goto clean_up;
  1579. }
  1580. /* Walk list of transports, removing transports in the UNKNOWN state. */
  1581. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  1582. transport = list_entry(pos, struct sctp_transport, transports);
  1583. if (transport->state == SCTP_UNKNOWN) {
  1584. sctp_assoc_rm_peer(asoc, transport);
  1585. }
  1586. }
  1587. /* The fixed INIT headers are always in network byte
  1588. * order.
  1589. */
  1590. asoc->peer.i.init_tag =
  1591. ntohl(peer_init->init_hdr.init_tag);
  1592. asoc->peer.i.a_rwnd =
  1593. ntohl(peer_init->init_hdr.a_rwnd);
  1594. asoc->peer.i.num_outbound_streams =
  1595. ntohs(peer_init->init_hdr.num_outbound_streams);
  1596. asoc->peer.i.num_inbound_streams =
  1597. ntohs(peer_init->init_hdr.num_inbound_streams);
  1598. asoc->peer.i.initial_tsn =
  1599. ntohl(peer_init->init_hdr.initial_tsn);
  1600. /* Apply the upper bounds for output streams based on peer's
  1601. * number of inbound streams.
  1602. */
  1603. if (asoc->c.sinit_num_ostreams >
  1604. ntohs(peer_init->init_hdr.num_inbound_streams)) {
  1605. asoc->c.sinit_num_ostreams =
  1606. ntohs(peer_init->init_hdr.num_inbound_streams);
  1607. }
  1608. if (asoc->c.sinit_max_instreams >
  1609. ntohs(peer_init->init_hdr.num_outbound_streams)) {
  1610. asoc->c.sinit_max_instreams =
  1611. ntohs(peer_init->init_hdr.num_outbound_streams);
  1612. }
  1613. /* Copy Initiation tag from INIT to VT_peer in cookie. */
  1614. asoc->c.peer_vtag = asoc->peer.i.init_tag;
  1615. /* Peer Rwnd : Current calculated value of the peer's rwnd. */
  1616. asoc->peer.rwnd = asoc->peer.i.a_rwnd;
  1617. /* Copy cookie in case we need to resend COOKIE-ECHO. */
  1618. cookie = asoc->peer.cookie;
  1619. if (cookie) {
  1620. asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
  1621. if (!asoc->peer.cookie)
  1622. goto clean_up;
  1623. memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
  1624. }
  1625. /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
  1626. * high (for example, implementations MAY use the size of the receiver
  1627. * advertised window).
  1628. */
  1629. list_for_each(pos, &asoc->peer.transport_addr_list) {
  1630. transport = list_entry(pos, struct sctp_transport, transports);
  1631. transport->ssthresh = asoc->peer.i.a_rwnd;
  1632. }
  1633. /* Set up the TSN tracking pieces. */
  1634. sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
  1635. asoc->peer.i.initial_tsn);
  1636. /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
  1637. *
  1638. * The stream sequence number in all the streams shall start
  1639. * from 0 when the association is established. Also, when the
  1640. * stream sequence number reaches the value 65535 the next
  1641. * stream sequence number shall be set to 0.
  1642. */
  1643. /* Allocate storage for the negotiated streams if it is not a temporary
  1644. * association.
  1645. */
  1646. if (!asoc->temp) {
  1647. int assoc_id;
  1648. int error;
  1649. asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
  1650. asoc->c.sinit_num_ostreams, gfp);
  1651. if (!asoc->ssnmap)
  1652. goto clean_up;
  1653. retry:
  1654. if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
  1655. goto clean_up;
  1656. spin_lock_bh(&sctp_assocs_id_lock);
  1657. error = idr_get_new_above(&sctp_assocs_id, (void *)asoc, 1,
  1658. &assoc_id);
  1659. spin_unlock_bh(&sctp_assocs_id_lock);
  1660. if (error == -EAGAIN)
  1661. goto retry;
  1662. else if (error)
  1663. goto clean_up;
  1664. asoc->assoc_id = (sctp_assoc_t) assoc_id;
  1665. }
  1666. /* ADDIP Section 4.1 ASCONF Chunk Procedures
  1667. *
  1668. * When an endpoint has an ASCONF signaled change to be sent to the
  1669. * remote endpoint it should do the following:
  1670. * ...
  1671. * A2) A serial number should be assigned to the Chunk. The serial
  1672. * number should be a monotonically increasing number. All serial
  1673. * numbers are defined to be initialized at the start of the
  1674. * association to the same value as the Initial TSN.
  1675. */
  1676. asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
  1677. return 1;
  1678. clean_up:
  1679. /* Release the transport structures. */
  1680. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  1681. transport = list_entry(pos, struct sctp_transport, transports);
  1682. list_del_init(pos);
  1683. sctp_transport_free(transport);
  1684. }
  1685. asoc->peer.transport_count = 0;
  1686. nomem:
  1687. return 0;
  1688. }
  1689. /* Update asoc with the option described in param.
  1690. *
  1691. * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
  1692. *
  1693. * asoc is the association to update.
  1694. * param is the variable length parameter to use for update.
  1695. * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
  1696. * If the current packet is an INIT we want to minimize the amount of
  1697. * work we do. In particular, we should not build transport
  1698. * structures for the addresses.
  1699. */
  1700. static int sctp_process_param(struct sctp_association *asoc,
  1701. union sctp_params param,
  1702. const union sctp_addr *peer_addr,
  1703. unsigned int __nocast gfp)
  1704. {
  1705. union sctp_addr addr;
  1706. int i;
  1707. __u16 sat;
  1708. int retval = 1;
  1709. sctp_scope_t scope;
  1710. time_t stale;
  1711. struct sctp_af *af;
  1712. /* We maintain all INIT parameters in network byte order all the
  1713. * time. This allows us to not worry about whether the parameters
  1714. * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
  1715. */
  1716. switch (param.p->type) {
  1717. case SCTP_PARAM_IPV6_ADDRESS:
  1718. if (PF_INET6 != asoc->base.sk->sk_family)
  1719. break;
  1720. /* Fall through. */
  1721. case SCTP_PARAM_IPV4_ADDRESS:
  1722. af = sctp_get_af_specific(param_type2af(param.p->type));
  1723. af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
  1724. scope = sctp_scope(peer_addr);
  1725. if (sctp_in_scope(&addr, scope))
  1726. if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_ACTIVE))
  1727. return 0;
  1728. break;
  1729. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  1730. if (!sctp_cookie_preserve_enable)
  1731. break;
  1732. stale = ntohl(param.life->lifespan_increment);
  1733. /* Suggested Cookie Life span increment's unit is msec,
  1734. * (1/1000sec).
  1735. */
  1736. asoc->cookie_life.tv_sec += stale / 1000;
  1737. asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
  1738. break;
  1739. case SCTP_PARAM_HOST_NAME_ADDRESS:
  1740. SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
  1741. break;
  1742. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  1743. /* Turn off the default values first so we'll know which
  1744. * ones are really set by the peer.
  1745. */
  1746. asoc->peer.ipv4_address = 0;
  1747. asoc->peer.ipv6_address = 0;
  1748. /* Cycle through address types; avoid divide by 0. */
  1749. sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
  1750. if (sat)
  1751. sat /= sizeof(__u16);
  1752. for (i = 0; i < sat; ++i) {
  1753. switch (param.sat->types[i]) {
  1754. case SCTP_PARAM_IPV4_ADDRESS:
  1755. asoc->peer.ipv4_address = 1;
  1756. break;
  1757. case SCTP_PARAM_IPV6_ADDRESS:
  1758. asoc->peer.ipv6_address = 1;
  1759. break;
  1760. case SCTP_PARAM_HOST_NAME_ADDRESS:
  1761. asoc->peer.hostname_address = 1;
  1762. break;
  1763. default: /* Just ignore anything else. */
  1764. break;
  1765. };
  1766. }
  1767. break;
  1768. case SCTP_PARAM_STATE_COOKIE:
  1769. asoc->peer.cookie_len =
  1770. ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
  1771. asoc->peer.cookie = param.cookie->body;
  1772. break;
  1773. case SCTP_PARAM_HEARTBEAT_INFO:
  1774. /* Would be odd to receive, but it causes no problems. */
  1775. break;
  1776. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  1777. /* Rejected during verify stage. */
  1778. break;
  1779. case SCTP_PARAM_ECN_CAPABLE:
  1780. asoc->peer.ecn_capable = 1;
  1781. break;
  1782. case SCTP_PARAM_ADAPTION_LAYER_IND:
  1783. asoc->peer.adaption_ind = param.aind->adaption_ind;
  1784. break;
  1785. case SCTP_PARAM_FWD_TSN_SUPPORT:
  1786. if (sctp_prsctp_enable) {
  1787. asoc->peer.prsctp_capable = 1;
  1788. break;
  1789. }
  1790. /* Fall Through */
  1791. default:
  1792. /* Any unrecognized parameters should have been caught
  1793. * and handled by sctp_verify_param() which should be
  1794. * called prior to this routine. Simply log the error
  1795. * here.
  1796. */
  1797. SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
  1798. ntohs(param.p->type), asoc);
  1799. break;
  1800. };
  1801. return retval;
  1802. }
  1803. /* Select a new verification tag. */
  1804. __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
  1805. {
  1806. /* I believe that this random number generator complies with RFC1750.
  1807. * A tag of 0 is reserved for special cases (e.g. INIT).
  1808. */
  1809. __u32 x;
  1810. do {
  1811. get_random_bytes(&x, sizeof(__u32));
  1812. } while (x == 0);
  1813. return x;
  1814. }
  1815. /* Select an initial TSN to send during startup. */
  1816. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
  1817. {
  1818. __u32 retval;
  1819. get_random_bytes(&retval, sizeof(__u32));
  1820. return retval;
  1821. }
  1822. /*
  1823. * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
  1824. * 0 1 2 3
  1825. * 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
  1826. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1827. * | Type = 0xC1 | Chunk Flags | Chunk Length |
  1828. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1829. * | Serial Number |
  1830. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1831. * | Address Parameter |
  1832. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1833. * | ASCONF Parameter #1 |
  1834. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1835. * \ \
  1836. * / .... /
  1837. * \ \
  1838. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1839. * | ASCONF Parameter #N |
  1840. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1841. *
  1842. * Address Parameter and other parameter will not be wrapped in this function
  1843. */
  1844. static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
  1845. union sctp_addr *addr,
  1846. int vparam_len)
  1847. {
  1848. sctp_addiphdr_t asconf;
  1849. struct sctp_chunk *retval;
  1850. int length = sizeof(asconf) + vparam_len;
  1851. union sctp_addr_param addrparam;
  1852. int addrlen;
  1853. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  1854. addrlen = af->to_addr_param(addr, &addrparam);
  1855. if (!addrlen)
  1856. return NULL;
  1857. length += addrlen;
  1858. /* Create the chunk. */
  1859. retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
  1860. if (!retval)
  1861. return NULL;
  1862. asconf.serial = htonl(asoc->addip_serial++);
  1863. retval->subh.addip_hdr =
  1864. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  1865. retval->param_hdr.v =
  1866. sctp_addto_chunk(retval, addrlen, &addrparam);
  1867. return retval;
  1868. }
  1869. /* ADDIP
  1870. * 3.2.1 Add IP Address
  1871. * 0 1 2 3
  1872. * 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
  1873. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1874. * | Type = 0xC001 | Length = Variable |
  1875. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1876. * | ASCONF-Request Correlation ID |
  1877. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1878. * | Address Parameter |
  1879. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1880. *
  1881. * 3.2.2 Delete IP Address
  1882. * 0 1 2 3
  1883. * 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
  1884. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1885. * | Type = 0xC002 | Length = Variable |
  1886. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1887. * | ASCONF-Request Correlation ID |
  1888. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1889. * | Address Parameter |
  1890. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1891. *
  1892. */
  1893. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  1894. union sctp_addr *laddr,
  1895. struct sockaddr *addrs,
  1896. int addrcnt,
  1897. __u16 flags)
  1898. {
  1899. sctp_addip_param_t param;
  1900. struct sctp_chunk *retval;
  1901. union sctp_addr_param addr_param;
  1902. union sctp_addr *addr;
  1903. void *addr_buf;
  1904. struct sctp_af *af;
  1905. int paramlen = sizeof(param);
  1906. int addr_param_len = 0;
  1907. int totallen = 0;
  1908. int i;
  1909. /* Get total length of all the address parameters. */
  1910. addr_buf = addrs;
  1911. for (i = 0; i < addrcnt; i++) {
  1912. addr = (union sctp_addr *)addr_buf;
  1913. af = sctp_get_af_specific(addr->v4.sin_family);
  1914. addr_param_len = af->to_addr_param(addr, &addr_param);
  1915. totallen += paramlen;
  1916. totallen += addr_param_len;
  1917. addr_buf += af->sockaddr_len;
  1918. }
  1919. /* Create an asconf chunk with the required length. */
  1920. retval = sctp_make_asconf(asoc, laddr, totallen);
  1921. if (!retval)
  1922. return NULL;
  1923. /* Add the address parameters to the asconf chunk. */
  1924. addr_buf = addrs;
  1925. for (i = 0; i < addrcnt; i++) {
  1926. addr = (union sctp_addr *)addr_buf;
  1927. af = sctp_get_af_specific(addr->v4.sin_family);
  1928. addr_param_len = af->to_addr_param(addr, &addr_param);
  1929. param.param_hdr.type = flags;
  1930. param.param_hdr.length = htons(paramlen + addr_param_len);
  1931. param.crr_id = i;
  1932. sctp_addto_chunk(retval, paramlen, &param);
  1933. sctp_addto_chunk(retval, addr_param_len, &addr_param);
  1934. addr_buf += af->sockaddr_len;
  1935. }
  1936. return retval;
  1937. }
  1938. /* ADDIP
  1939. * 3.2.4 Set Primary IP Address
  1940. * 0 1 2 3
  1941. * 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
  1942. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1943. * | Type =0xC004 | Length = Variable |
  1944. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1945. * | ASCONF-Request Correlation ID |
  1946. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1947. * | Address Parameter |
  1948. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1949. *
  1950. * Create an ASCONF chunk with Set Primary IP address parameter.
  1951. */
  1952. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  1953. union sctp_addr *addr)
  1954. {
  1955. sctp_addip_param_t param;
  1956. struct sctp_chunk *retval;
  1957. int len = sizeof(param);
  1958. union sctp_addr_param addrparam;
  1959. int addrlen;
  1960. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  1961. addrlen = af->to_addr_param(addr, &addrparam);
  1962. if (!addrlen)
  1963. return NULL;
  1964. len += addrlen;
  1965. /* Create the chunk and make asconf header. */
  1966. retval = sctp_make_asconf(asoc, addr, len);
  1967. if (!retval)
  1968. return NULL;
  1969. param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
  1970. param.param_hdr.length = htons(len);
  1971. param.crr_id = 0;
  1972. sctp_addto_chunk(retval, sizeof(param), &param);
  1973. sctp_addto_chunk(retval, addrlen, &addrparam);
  1974. return retval;
  1975. }
  1976. /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
  1977. * 0 1 2 3
  1978. * 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
  1979. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1980. * | Type = 0x80 | Chunk Flags | Chunk Length |
  1981. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1982. * | Serial Number |
  1983. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1984. * | ASCONF Parameter Response#1 |
  1985. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1986. * \ \
  1987. * / .... /
  1988. * \ \
  1989. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1990. * | ASCONF Parameter Response#N |
  1991. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1992. *
  1993. * Create an ASCONF_ACK chunk with enough space for the parameter responses.
  1994. */
  1995. static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
  1996. __u32 serial, int vparam_len)
  1997. {
  1998. sctp_addiphdr_t asconf;
  1999. struct sctp_chunk *retval;
  2000. int length = sizeof(asconf) + vparam_len;
  2001. /* Create the chunk. */
  2002. retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
  2003. if (!retval)
  2004. return NULL;
  2005. asconf.serial = htonl(serial);
  2006. retval->subh.addip_hdr =
  2007. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2008. return retval;
  2009. }
  2010. /* Add response parameters to an ASCONF_ACK chunk. */
  2011. static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
  2012. __u16 err_code, sctp_addip_param_t *asconf_param)
  2013. {
  2014. sctp_addip_param_t ack_param;
  2015. sctp_errhdr_t err_param;
  2016. int asconf_param_len = 0;
  2017. int err_param_len = 0;
  2018. __u16 response_type;
  2019. if (SCTP_ERROR_NO_ERROR == err_code) {
  2020. response_type = SCTP_PARAM_SUCCESS_REPORT;
  2021. } else {
  2022. response_type = SCTP_PARAM_ERR_CAUSE;
  2023. err_param_len = sizeof(err_param);
  2024. if (asconf_param)
  2025. asconf_param_len =
  2026. ntohs(asconf_param->param_hdr.length);
  2027. }
  2028. /* Add Success Indication or Error Cause Indication parameter. */
  2029. ack_param.param_hdr.type = response_type;
  2030. ack_param.param_hdr.length = htons(sizeof(ack_param) +
  2031. err_param_len +
  2032. asconf_param_len);
  2033. ack_param.crr_id = crr_id;
  2034. sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
  2035. if (SCTP_ERROR_NO_ERROR == err_code)
  2036. return;
  2037. /* Add Error Cause parameter. */
  2038. err_param.cause = err_code;
  2039. err_param.length = htons(err_param_len + asconf_param_len);
  2040. sctp_addto_chunk(chunk, err_param_len, &err_param);
  2041. /* Add the failed TLV copied from ASCONF chunk. */
  2042. if (asconf_param)
  2043. sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
  2044. }
  2045. /* Process a asconf parameter. */
  2046. static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
  2047. struct sctp_chunk *asconf,
  2048. sctp_addip_param_t *asconf_param)
  2049. {
  2050. struct sctp_transport *peer;
  2051. struct sctp_af *af;
  2052. union sctp_addr addr;
  2053. struct list_head *pos;
  2054. union sctp_addr_param *addr_param;
  2055. addr_param = (union sctp_addr_param *)
  2056. ((void *)asconf_param + sizeof(sctp_addip_param_t));
  2057. af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
  2058. if (unlikely(!af))
  2059. return SCTP_ERROR_INV_PARAM;
  2060. af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
  2061. switch (asconf_param->param_hdr.type) {
  2062. case SCTP_PARAM_ADD_IP:
  2063. /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
  2064. * request and does not have the local resources to add this
  2065. * new address to the association, it MUST return an Error
  2066. * Cause TLV set to the new error code 'Operation Refused
  2067. * Due to Resource Shortage'.
  2068. */
  2069. peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_ACTIVE);
  2070. if (!peer)
  2071. return SCTP_ERROR_RSRC_LOW;
  2072. /* Start the heartbeat timer. */
  2073. if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
  2074. sctp_transport_hold(peer);
  2075. break;
  2076. case SCTP_PARAM_DEL_IP:
  2077. /* ADDIP 4.3 D7) If a request is received to delete the
  2078. * last remaining IP address of a peer endpoint, the receiver
  2079. * MUST send an Error Cause TLV with the error cause set to the
  2080. * new error code 'Request to Delete Last Remaining IP Address'.
  2081. */
  2082. pos = asoc->peer.transport_addr_list.next;
  2083. if (pos->next == &asoc->peer.transport_addr_list)
  2084. return SCTP_ERROR_DEL_LAST_IP;
  2085. /* ADDIP 4.3 D8) If a request is received to delete an IP
  2086. * address which is also the source address of the IP packet
  2087. * which contained the ASCONF chunk, the receiver MUST reject
  2088. * this request. To reject the request the receiver MUST send
  2089. * an Error Cause TLV set to the new error code 'Request to
  2090. * Delete Source IP Address'
  2091. */
  2092. if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
  2093. return SCTP_ERROR_DEL_SRC_IP;
  2094. sctp_assoc_del_peer(asoc, &addr);
  2095. break;
  2096. case SCTP_PARAM_SET_PRIMARY:
  2097. peer = sctp_assoc_lookup_paddr(asoc, &addr);
  2098. if (!peer)
  2099. return SCTP_ERROR_INV_PARAM;
  2100. sctp_assoc_set_primary(asoc, peer);
  2101. break;
  2102. default:
  2103. return SCTP_ERROR_INV_PARAM;
  2104. break;
  2105. }
  2106. return SCTP_ERROR_NO_ERROR;
  2107. }
  2108. /* Process an incoming ASCONF chunk with the next expected serial no. and
  2109. * return an ASCONF_ACK chunk to be sent in response.
  2110. */
  2111. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  2112. struct sctp_chunk *asconf)
  2113. {
  2114. sctp_addiphdr_t *hdr;
  2115. union sctp_addr_param *addr_param;
  2116. sctp_addip_param_t *asconf_param;
  2117. struct sctp_chunk *asconf_ack;
  2118. __u16 err_code;
  2119. int length = 0;
  2120. int chunk_len = asconf->skb->len;
  2121. __u32 serial;
  2122. int all_param_pass = 1;
  2123. hdr = (sctp_addiphdr_t *)asconf->skb->data;
  2124. serial = ntohl(hdr->serial);
  2125. /* Skip the addiphdr and store a pointer to address parameter. */
  2126. length = sizeof(sctp_addiphdr_t);
  2127. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2128. chunk_len -= length;
  2129. /* Skip the address parameter and store a pointer to the first
  2130. * asconf paramter.
  2131. */
  2132. length = ntohs(addr_param->v4.param_hdr.length);
  2133. asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
  2134. chunk_len -= length;
  2135. /* create an ASCONF_ACK chunk.
  2136. * Based on the definitions of parameters, we know that the size of
  2137. * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
  2138. * paramters.
  2139. */
  2140. asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
  2141. if (!asconf_ack)
  2142. goto done;
  2143. /* Process the TLVs contained within the ASCONF chunk. */
  2144. while (chunk_len > 0) {
  2145. err_code = sctp_process_asconf_param(asoc, asconf,
  2146. asconf_param);
  2147. /* ADDIP 4.1 A7)
  2148. * If an error response is received for a TLV parameter,
  2149. * all TLVs with no response before the failed TLV are
  2150. * considered successful if not reported. All TLVs after
  2151. * the failed response are considered unsuccessful unless
  2152. * a specific success indication is present for the parameter.
  2153. */
  2154. if (SCTP_ERROR_NO_ERROR != err_code)
  2155. all_param_pass = 0;
  2156. if (!all_param_pass)
  2157. sctp_add_asconf_response(asconf_ack,
  2158. asconf_param->crr_id, err_code,
  2159. asconf_param);
  2160. /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
  2161. * an IP address sends an 'Out of Resource' in its response, it
  2162. * MUST also fail any subsequent add or delete requests bundled
  2163. * in the ASCONF.
  2164. */
  2165. if (SCTP_ERROR_RSRC_LOW == err_code)
  2166. goto done;
  2167. /* Move to the next ASCONF param. */
  2168. length = ntohs(asconf_param->param_hdr.length);
  2169. asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
  2170. length);
  2171. chunk_len -= length;
  2172. }
  2173. done:
  2174. asoc->peer.addip_serial++;
  2175. /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
  2176. * after freeing the reference to old asconf ack if any.
  2177. */
  2178. if (asconf_ack) {
  2179. if (asoc->addip_last_asconf_ack)
  2180. sctp_chunk_free(asoc->addip_last_asconf_ack);
  2181. sctp_chunk_hold(asconf_ack);
  2182. asoc->addip_last_asconf_ack = asconf_ack;
  2183. }
  2184. return asconf_ack;
  2185. }
  2186. /* Process a asconf parameter that is successfully acked. */
  2187. static int sctp_asconf_param_success(struct sctp_association *asoc,
  2188. sctp_addip_param_t *asconf_param)
  2189. {
  2190. struct sctp_af *af;
  2191. union sctp_addr addr;
  2192. struct sctp_bind_addr *bp = &asoc->base.bind_addr;
  2193. union sctp_addr_param *addr_param;
  2194. struct list_head *pos;
  2195. struct sctp_transport *transport;
  2196. int retval = 0;
  2197. addr_param = (union sctp_addr_param *)
  2198. ((void *)asconf_param + sizeof(sctp_addip_param_t));
  2199. /* We have checked the packet before, so we do not check again. */
  2200. af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
  2201. af->from_addr_param(&addr, addr_param, bp->port, 0);
  2202. switch (asconf_param->param_hdr.type) {
  2203. case SCTP_PARAM_ADD_IP:
  2204. sctp_local_bh_disable();
  2205. sctp_write_lock(&asoc->base.addr_lock);
  2206. retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
  2207. sctp_write_unlock(&asoc->base.addr_lock);
  2208. sctp_local_bh_enable();
  2209. break;
  2210. case SCTP_PARAM_DEL_IP:
  2211. sctp_local_bh_disable();
  2212. sctp_write_lock(&asoc->base.addr_lock);
  2213. retval = sctp_del_bind_addr(bp, &addr);
  2214. sctp_write_unlock(&asoc->base.addr_lock);
  2215. sctp_local_bh_enable();
  2216. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2217. transport = list_entry(pos, struct sctp_transport,
  2218. transports);
  2219. sctp_transport_route(transport, NULL,
  2220. sctp_sk(asoc->base.sk));
  2221. }
  2222. break;
  2223. default:
  2224. break;
  2225. }
  2226. return retval;
  2227. }
  2228. /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
  2229. * for the given asconf parameter. If there is no response for this parameter,
  2230. * return the error code based on the third argument 'no_err'.
  2231. * ADDIP 4.1
  2232. * A7) If an error response is received for a TLV parameter, all TLVs with no
  2233. * response before the failed TLV are considered successful if not reported.
  2234. * All TLVs after the failed response are considered unsuccessful unless a
  2235. * specific success indication is present for the parameter.
  2236. */
  2237. static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
  2238. sctp_addip_param_t *asconf_param,
  2239. int no_err)
  2240. {
  2241. sctp_addip_param_t *asconf_ack_param;
  2242. sctp_errhdr_t *err_param;
  2243. int length;
  2244. int asconf_ack_len = asconf_ack->skb->len;
  2245. __u16 err_code;
  2246. if (no_err)
  2247. err_code = SCTP_ERROR_NO_ERROR;
  2248. else
  2249. err_code = SCTP_ERROR_REQ_REFUSED;
  2250. /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
  2251. * the first asconf_ack parameter.
  2252. */
  2253. length = sizeof(sctp_addiphdr_t);
  2254. asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
  2255. length);
  2256. asconf_ack_len -= length;
  2257. while (asconf_ack_len > 0) {
  2258. if (asconf_ack_param->crr_id == asconf_param->crr_id) {
  2259. switch(asconf_ack_param->param_hdr.type) {
  2260. case SCTP_PARAM_SUCCESS_REPORT:
  2261. return SCTP_ERROR_NO_ERROR;
  2262. case SCTP_PARAM_ERR_CAUSE:
  2263. length = sizeof(sctp_addip_param_t);
  2264. err_param = (sctp_errhdr_t *)
  2265. ((void *)asconf_ack_param + length);
  2266. asconf_ack_len -= length;
  2267. if (asconf_ack_len > 0)
  2268. return err_param->cause;
  2269. else
  2270. return SCTP_ERROR_INV_PARAM;
  2271. break;
  2272. default:
  2273. return SCTP_ERROR_INV_PARAM;
  2274. }
  2275. }
  2276. length = ntohs(asconf_ack_param->param_hdr.length);
  2277. asconf_ack_param = (sctp_addip_param_t *)
  2278. ((void *)asconf_ack_param + length);
  2279. asconf_ack_len -= length;
  2280. }
  2281. return err_code;
  2282. }
  2283. /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
  2284. int sctp_process_asconf_ack(struct sctp_association *asoc,
  2285. struct sctp_chunk *asconf_ack)
  2286. {
  2287. struct sctp_chunk *asconf = asoc->addip_last_asconf;
  2288. union sctp_addr_param *addr_param;
  2289. sctp_addip_param_t *asconf_param;
  2290. int length = 0;
  2291. int asconf_len = asconf->skb->len;
  2292. int all_param_pass = 0;
  2293. int no_err = 1;
  2294. int retval = 0;
  2295. __u16 err_code = SCTP_ERROR_NO_ERROR;
  2296. /* Skip the chunkhdr and addiphdr from the last asconf sent and store
  2297. * a pointer to address parameter.
  2298. */
  2299. length = sizeof(sctp_addip_chunk_t);
  2300. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2301. asconf_len -= length;
  2302. /* Skip the address parameter in the last asconf sent and store a
  2303. * pointer to the first asconf paramter.
  2304. */
  2305. length = ntohs(addr_param->v4.param_hdr.length);
  2306. asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
  2307. asconf_len -= length;
  2308. /* ADDIP 4.1
  2309. * A8) If there is no response(s) to specific TLV parameter(s), and no
  2310. * failures are indicated, then all request(s) are considered
  2311. * successful.
  2312. */
  2313. if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
  2314. all_param_pass = 1;
  2315. /* Process the TLVs contained in the last sent ASCONF chunk. */
  2316. while (asconf_len > 0) {
  2317. if (all_param_pass)
  2318. err_code = SCTP_ERROR_NO_ERROR;
  2319. else {
  2320. err_code = sctp_get_asconf_response(asconf_ack,
  2321. asconf_param,
  2322. no_err);
  2323. if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
  2324. no_err = 0;
  2325. }
  2326. switch (err_code) {
  2327. case SCTP_ERROR_NO_ERROR:
  2328. retval = sctp_asconf_param_success(asoc, asconf_param);
  2329. break;
  2330. case SCTP_ERROR_RSRC_LOW:
  2331. retval = 1;
  2332. break;
  2333. case SCTP_ERROR_INV_PARAM:
  2334. /* Disable sending this type of asconf parameter in
  2335. * future.
  2336. */
  2337. asoc->peer.addip_disabled_mask |=
  2338. asconf_param->param_hdr.type;
  2339. break;
  2340. case SCTP_ERROR_REQ_REFUSED:
  2341. case SCTP_ERROR_DEL_LAST_IP:
  2342. case SCTP_ERROR_DEL_SRC_IP:
  2343. default:
  2344. break;
  2345. }
  2346. /* Skip the processed asconf parameter and move to the next
  2347. * one.
  2348. */
  2349. length = ntohs(asconf_param->param_hdr.length);
  2350. asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
  2351. length);
  2352. asconf_len -= length;
  2353. }
  2354. /* Free the cached last sent asconf chunk. */
  2355. sctp_chunk_free(asconf);
  2356. asoc->addip_last_asconf = NULL;
  2357. /* Send the next asconf chunk from the addip chunk queue. */
  2358. if (!list_empty(&asoc->addip_chunk_list)) {
  2359. struct list_head *entry = asoc->addip_chunk_list.next;
  2360. asconf = list_entry(entry, struct sctp_chunk, list);
  2361. list_del_init(entry);
  2362. /* Hold the chunk until an ASCONF_ACK is received. */
  2363. sctp_chunk_hold(asconf);
  2364. if (sctp_primitive_ASCONF(asoc, asconf))
  2365. sctp_chunk_free(asconf);
  2366. else
  2367. asoc->addip_last_asconf = asconf;
  2368. }
  2369. return retval;
  2370. }
  2371. /* Make a FWD TSN chunk. */
  2372. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  2373. __u32 new_cum_tsn, size_t nstreams,
  2374. struct sctp_fwdtsn_skip *skiplist)
  2375. {
  2376. struct sctp_chunk *retval = NULL;
  2377. struct sctp_fwdtsn_chunk *ftsn_chunk;
  2378. struct sctp_fwdtsn_hdr ftsn_hdr;
  2379. struct sctp_fwdtsn_skip skip;
  2380. size_t hint;
  2381. int i;
  2382. hint = (nstreams + 1) * sizeof(__u32);
  2383. retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
  2384. if (!retval)
  2385. return NULL;
  2386. ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
  2387. ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
  2388. retval->subh.fwdtsn_hdr =
  2389. sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
  2390. for (i = 0; i < nstreams; i++) {
  2391. skip.stream = skiplist[i].stream;
  2392. skip.ssn = skiplist[i].ssn;
  2393. sctp_addto_chunk(retval, sizeof(skip), &skip);
  2394. }
  2395. return retval;
  2396. }