sm_make_chunk.c 97 KB

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