sm_make_chunk.c 85 KB

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