sm_make_chunk.c 83 KB

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