sm_make_chunk.c 97 KB

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