sm_make_chunk.c 83 KB

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