sm_make_chunk.c 83 KB

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