sm_make_chunk.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367
  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. if (data)
  865. memcpy(target, data, len);
  866. else
  867. memset(target, 0, len);
  868. /* Adjust the chunk length field. */
  869. chunk->chunk_hdr->length = htons(chunklen + len);
  870. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  871. return target;
  872. }
  873. /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
  874. struct sctp_chunk *sctp_make_abort_violation(
  875. const struct sctp_association *asoc,
  876. const struct sctp_chunk *chunk,
  877. const __u8 *payload,
  878. const size_t paylen)
  879. {
  880. struct sctp_chunk *retval;
  881. struct sctp_paramhdr phdr;
  882. retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
  883. + sizeof(sctp_paramhdr_t));
  884. if (!retval)
  885. goto end;
  886. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
  887. + sizeof(sctp_paramhdr_t));
  888. phdr.type = htons(chunk->chunk_hdr->type);
  889. phdr.length = chunk->chunk_hdr->length;
  890. sctp_addto_chunk(retval, paylen, payload);
  891. sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
  892. end:
  893. return retval;
  894. }
  895. struct sctp_chunk *sctp_make_violation_paramlen(
  896. const struct sctp_association *asoc,
  897. const struct sctp_chunk *chunk,
  898. struct sctp_paramhdr *param)
  899. {
  900. struct sctp_chunk *retval;
  901. static const char error[] = "The following parameter had invalid length:";
  902. size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
  903. sizeof(sctp_paramhdr_t);
  904. retval = sctp_make_abort(asoc, chunk, payload_len);
  905. if (!retval)
  906. goto nodata;
  907. sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
  908. sizeof(error) + sizeof(sctp_paramhdr_t));
  909. sctp_addto_chunk(retval, sizeof(error), error);
  910. sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
  911. nodata:
  912. return retval;
  913. }
  914. /* Make a HEARTBEAT chunk. */
  915. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  916. const struct sctp_transport *transport,
  917. const void *payload, const size_t paylen)
  918. {
  919. struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
  920. 0, paylen);
  921. if (!retval)
  922. goto nodata;
  923. /* Cast away the 'const', as this is just telling the chunk
  924. * what transport it belongs to.
  925. */
  926. retval->transport = (struct sctp_transport *) transport;
  927. retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
  928. nodata:
  929. return retval;
  930. }
  931. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  932. const struct sctp_chunk *chunk,
  933. const void *payload, const size_t paylen)
  934. {
  935. struct sctp_chunk *retval;
  936. retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
  937. if (!retval)
  938. goto nodata;
  939. retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
  940. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  941. *
  942. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  943. * HEARTBEAT ACK, * etc.) to the same destination transport
  944. * address from which it * received the DATA or control chunk
  945. * to which it is replying.
  946. *
  947. * [HBACK back to where the HEARTBEAT came from.]
  948. */
  949. if (chunk)
  950. retval->transport = chunk->transport;
  951. nodata:
  952. return retval;
  953. }
  954. /* Create an Operation Error chunk with the specified space reserved.
  955. * This routine can be used for containing multiple causes in the chunk.
  956. */
  957. static struct sctp_chunk *sctp_make_op_error_space(
  958. const struct sctp_association *asoc,
  959. const struct sctp_chunk *chunk,
  960. size_t size)
  961. {
  962. struct sctp_chunk *retval;
  963. retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
  964. sizeof(sctp_errhdr_t) + size);
  965. if (!retval)
  966. goto nodata;
  967. /* RFC 2960 6.4 Multi-homed SCTP Endpoints
  968. *
  969. * An endpoint SHOULD transmit reply chunks (e.g., SACK,
  970. * HEARTBEAT ACK, etc.) to the same destination transport
  971. * address from which it received the DATA or control chunk
  972. * to which it is replying.
  973. *
  974. */
  975. if (chunk)
  976. retval->transport = chunk->transport;
  977. nodata:
  978. return retval;
  979. }
  980. /* Create an Operation Error chunk. */
  981. struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
  982. const struct sctp_chunk *chunk,
  983. __be16 cause_code, const void *payload,
  984. size_t paylen, size_t reserve_tail)
  985. {
  986. struct sctp_chunk *retval;
  987. retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
  988. if (!retval)
  989. goto nodata;
  990. sctp_init_cause(retval, cause_code, paylen + reserve_tail);
  991. sctp_addto_chunk(retval, paylen, payload);
  992. if (reserve_tail)
  993. sctp_addto_param(retval, reserve_tail, NULL);
  994. nodata:
  995. return retval;
  996. }
  997. struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
  998. {
  999. struct sctp_chunk *retval;
  1000. struct sctp_hmac *hmac_desc;
  1001. struct sctp_authhdr auth_hdr;
  1002. __u8 *hmac;
  1003. /* Get the first hmac that the peer told us to use */
  1004. hmac_desc = sctp_auth_asoc_get_hmac(asoc);
  1005. if (unlikely(!hmac_desc))
  1006. return NULL;
  1007. retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
  1008. hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
  1009. if (!retval)
  1010. return NULL;
  1011. auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
  1012. auth_hdr.shkey_id = htons(asoc->active_key_id);
  1013. retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
  1014. &auth_hdr);
  1015. hmac = skb_put(retval->skb, hmac_desc->hmac_len);
  1016. memset(hmac, 0, hmac_desc->hmac_len);
  1017. /* Adjust the chunk header to include the empty MAC */
  1018. retval->chunk_hdr->length =
  1019. htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
  1020. retval->chunk_end = skb_tail_pointer(retval->skb);
  1021. return retval;
  1022. }
  1023. /********************************************************************
  1024. * 2nd Level Abstractions
  1025. ********************************************************************/
  1026. /* Turn an skb into a chunk.
  1027. * FIXME: Eventually move the structure directly inside the skb->cb[].
  1028. */
  1029. struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
  1030. const struct sctp_association *asoc,
  1031. struct sock *sk)
  1032. {
  1033. struct sctp_chunk *retval;
  1034. retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
  1035. if (!retval)
  1036. goto nodata;
  1037. if (!sk) {
  1038. SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
  1039. }
  1040. INIT_LIST_HEAD(&retval->list);
  1041. retval->skb = skb;
  1042. retval->asoc = (struct sctp_association *)asoc;
  1043. retval->resent = 0;
  1044. retval->has_tsn = 0;
  1045. retval->has_ssn = 0;
  1046. retval->rtt_in_progress = 0;
  1047. retval->sent_at = 0;
  1048. retval->singleton = 1;
  1049. retval->end_of_packet = 0;
  1050. retval->ecn_ce_done = 0;
  1051. retval->pdiscard = 0;
  1052. /* sctpimpguide-05.txt Section 2.8.2
  1053. * M1) Each time a new DATA chunk is transmitted
  1054. * set the 'TSN.Missing.Report' count for that TSN to 0. The
  1055. * 'TSN.Missing.Report' count will be used to determine missing chunks
  1056. * and when to fast retransmit.
  1057. */
  1058. retval->tsn_missing_report = 0;
  1059. retval->tsn_gap_acked = 0;
  1060. retval->fast_retransmit = SCTP_CAN_FRTX;
  1061. /* If this is a fragmented message, track all fragments
  1062. * of the message (for SEND_FAILED).
  1063. */
  1064. retval->msg = NULL;
  1065. /* Polish the bead hole. */
  1066. INIT_LIST_HEAD(&retval->transmitted_list);
  1067. INIT_LIST_HEAD(&retval->frag_list);
  1068. SCTP_DBG_OBJCNT_INC(chunk);
  1069. atomic_set(&retval->refcnt, 1);
  1070. nodata:
  1071. return retval;
  1072. }
  1073. /* Set chunk->source and dest based on the IP header in chunk->skb. */
  1074. void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
  1075. union sctp_addr *dest)
  1076. {
  1077. memcpy(&chunk->source, src, sizeof(union sctp_addr));
  1078. memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
  1079. }
  1080. /* Extract the source address from a chunk. */
  1081. const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
  1082. {
  1083. /* If we have a known transport, use that. */
  1084. if (chunk->transport) {
  1085. return &chunk->transport->ipaddr;
  1086. } else {
  1087. /* Otherwise, extract it from the IP header. */
  1088. return &chunk->source;
  1089. }
  1090. }
  1091. /* Create a new chunk, setting the type and flags headers from the
  1092. * arguments, reserving enough space for a 'paylen' byte payload.
  1093. */
  1094. SCTP_STATIC
  1095. struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
  1096. __u8 type, __u8 flags, int paylen)
  1097. {
  1098. struct sctp_chunk *retval;
  1099. sctp_chunkhdr_t *chunk_hdr;
  1100. struct sk_buff *skb;
  1101. struct sock *sk;
  1102. /* No need to allocate LL here, as this is only a chunk. */
  1103. skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
  1104. GFP_ATOMIC);
  1105. if (!skb)
  1106. goto nodata;
  1107. /* Make room for the chunk header. */
  1108. chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
  1109. chunk_hdr->type = type;
  1110. chunk_hdr->flags = flags;
  1111. chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
  1112. sk = asoc ? asoc->base.sk : NULL;
  1113. retval = sctp_chunkify(skb, asoc, sk);
  1114. if (!retval) {
  1115. kfree_skb(skb);
  1116. goto nodata;
  1117. }
  1118. retval->chunk_hdr = chunk_hdr;
  1119. retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
  1120. /* Determine if the chunk needs to be authenticated */
  1121. if (sctp_auth_send_cid(type, asoc))
  1122. retval->auth = 1;
  1123. /* Set the skb to the belonging sock for accounting. */
  1124. skb->sk = sk;
  1125. return retval;
  1126. nodata:
  1127. return NULL;
  1128. }
  1129. /* Release the memory occupied by a chunk. */
  1130. static void sctp_chunk_destroy(struct sctp_chunk *chunk)
  1131. {
  1132. BUG_ON(!list_empty(&chunk->list));
  1133. list_del_init(&chunk->transmitted_list);
  1134. /* Free the chunk skb data and the SCTP_chunk stub itself. */
  1135. dev_kfree_skb(chunk->skb);
  1136. SCTP_DBG_OBJCNT_DEC(chunk);
  1137. kmem_cache_free(sctp_chunk_cachep, chunk);
  1138. }
  1139. /* Possibly, free the chunk. */
  1140. void sctp_chunk_free(struct sctp_chunk *chunk)
  1141. {
  1142. /* Release our reference on the message tracker. */
  1143. if (chunk->msg)
  1144. sctp_datamsg_put(chunk->msg);
  1145. sctp_chunk_put(chunk);
  1146. }
  1147. /* Grab a reference to the chunk. */
  1148. void sctp_chunk_hold(struct sctp_chunk *ch)
  1149. {
  1150. atomic_inc(&ch->refcnt);
  1151. }
  1152. /* Release a reference to the chunk. */
  1153. void sctp_chunk_put(struct sctp_chunk *ch)
  1154. {
  1155. if (atomic_dec_and_test(&ch->refcnt))
  1156. sctp_chunk_destroy(ch);
  1157. }
  1158. /* Append bytes to the end of a chunk. Will panic if chunk is not big
  1159. * enough.
  1160. */
  1161. void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
  1162. {
  1163. void *target;
  1164. void *padding;
  1165. int chunklen = ntohs(chunk->chunk_hdr->length);
  1166. int padlen = WORD_ROUND(chunklen) - chunklen;
  1167. padding = skb_put(chunk->skb, padlen);
  1168. target = skb_put(chunk->skb, len);
  1169. memset(padding, 0, padlen);
  1170. memcpy(target, data, len);
  1171. /* Adjust the chunk length field. */
  1172. chunk->chunk_hdr->length = htons(chunklen + padlen + len);
  1173. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  1174. return target;
  1175. }
  1176. /* Append bytes from user space to the end of a chunk. Will panic if
  1177. * chunk is not big enough.
  1178. * Returns a kernel err value.
  1179. */
  1180. int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
  1181. struct iovec *data)
  1182. {
  1183. __u8 *target;
  1184. int err = 0;
  1185. /* Make room in chunk for data. */
  1186. target = skb_put(chunk->skb, len);
  1187. /* Copy data (whole iovec) into chunk */
  1188. if ((err = memcpy_fromiovecend(target, data, off, len)))
  1189. goto out;
  1190. /* Adjust the chunk length field. */
  1191. chunk->chunk_hdr->length =
  1192. htons(ntohs(chunk->chunk_hdr->length) + len);
  1193. chunk->chunk_end = skb_tail_pointer(chunk->skb);
  1194. out:
  1195. return err;
  1196. }
  1197. /* Helper function to assign a TSN if needed. This assumes that both
  1198. * the data_hdr and association have already been assigned.
  1199. */
  1200. void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
  1201. {
  1202. struct sctp_datamsg *msg;
  1203. struct sctp_chunk *lchunk;
  1204. struct sctp_stream *stream;
  1205. __u16 ssn;
  1206. __u16 sid;
  1207. if (chunk->has_ssn)
  1208. return;
  1209. /* All fragments will be on the same stream */
  1210. sid = ntohs(chunk->subh.data_hdr->stream);
  1211. stream = &chunk->asoc->ssnmap->out;
  1212. /* Now assign the sequence number to the entire message.
  1213. * All fragments must have the same stream sequence number.
  1214. */
  1215. msg = chunk->msg;
  1216. list_for_each_entry(lchunk, &msg->chunks, frag_list) {
  1217. if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  1218. ssn = 0;
  1219. } else {
  1220. if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
  1221. ssn = sctp_ssn_next(stream, sid);
  1222. else
  1223. ssn = sctp_ssn_peek(stream, sid);
  1224. }
  1225. lchunk->subh.data_hdr->ssn = htons(ssn);
  1226. lchunk->has_ssn = 1;
  1227. }
  1228. }
  1229. /* Helper function to assign a TSN if needed. This assumes that both
  1230. * the data_hdr and association have already been assigned.
  1231. */
  1232. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
  1233. {
  1234. if (!chunk->has_tsn) {
  1235. /* This is the last possible instant to
  1236. * assign a TSN.
  1237. */
  1238. chunk->subh.data_hdr->tsn =
  1239. htonl(sctp_association_get_next_tsn(chunk->asoc));
  1240. chunk->has_tsn = 1;
  1241. }
  1242. }
  1243. /* Create a CLOSED association to use with an incoming packet. */
  1244. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
  1245. struct sctp_chunk *chunk,
  1246. gfp_t gfp)
  1247. {
  1248. struct sctp_association *asoc;
  1249. struct sk_buff *skb;
  1250. sctp_scope_t scope;
  1251. struct sctp_af *af;
  1252. /* Create the bare association. */
  1253. scope = sctp_scope(sctp_source(chunk));
  1254. asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1255. if (!asoc)
  1256. goto nodata;
  1257. asoc->temp = 1;
  1258. skb = chunk->skb;
  1259. /* Create an entry for the source address of the packet. */
  1260. af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
  1261. if (unlikely(!af))
  1262. goto fail;
  1263. af->from_skb(&asoc->c.peer_addr, skb, 1);
  1264. nodata:
  1265. return asoc;
  1266. fail:
  1267. sctp_association_free(asoc);
  1268. return NULL;
  1269. }
  1270. /* Build a cookie representing asoc.
  1271. * This INCLUDES the param header needed to put the cookie in the INIT ACK.
  1272. */
  1273. static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
  1274. const struct sctp_association *asoc,
  1275. const struct sctp_chunk *init_chunk,
  1276. int *cookie_len,
  1277. const __u8 *raw_addrs, int addrs_len)
  1278. {
  1279. sctp_cookie_param_t *retval;
  1280. struct sctp_signed_cookie *cookie;
  1281. struct scatterlist sg;
  1282. int headersize, bodysize;
  1283. unsigned int keylen;
  1284. char *key;
  1285. /* Header size is static data prior to the actual cookie, including
  1286. * any padding.
  1287. */
  1288. headersize = sizeof(sctp_paramhdr_t) +
  1289. (sizeof(struct sctp_signed_cookie) -
  1290. sizeof(struct sctp_cookie));
  1291. bodysize = sizeof(struct sctp_cookie)
  1292. + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
  1293. /* Pad out the cookie to a multiple to make the signature
  1294. * functions simpler to write.
  1295. */
  1296. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1297. bodysize += SCTP_COOKIE_MULTIPLE
  1298. - (bodysize % SCTP_COOKIE_MULTIPLE);
  1299. *cookie_len = headersize + bodysize;
  1300. /* Clear this memory since we are sending this data structure
  1301. * out on the network.
  1302. */
  1303. retval = kzalloc(*cookie_len, GFP_ATOMIC);
  1304. if (!retval)
  1305. goto nodata;
  1306. cookie = (struct sctp_signed_cookie *) retval->body;
  1307. /* Set up the parameter header. */
  1308. retval->p.type = SCTP_PARAM_STATE_COOKIE;
  1309. retval->p.length = htons(*cookie_len);
  1310. /* Copy the cookie part of the association itself. */
  1311. cookie->c = asoc->c;
  1312. /* Save the raw address list length in the cookie. */
  1313. cookie->c.raw_addr_list_len = addrs_len;
  1314. /* Remember PR-SCTP capability. */
  1315. cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
  1316. /* Save adaptation indication in the cookie. */
  1317. cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
  1318. /* Set an expiration time for the cookie. */
  1319. do_gettimeofday(&cookie->c.expiration);
  1320. TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
  1321. /* Copy the peer's init packet. */
  1322. memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
  1323. ntohs(init_chunk->chunk_hdr->length));
  1324. /* Copy the raw local address list of the association. */
  1325. memcpy((__u8 *)&cookie->c.peer_init[0] +
  1326. ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
  1327. if (sctp_sk(ep->base.sk)->hmac) {
  1328. struct hash_desc desc;
  1329. /* Sign the message. */
  1330. sg_init_one(&sg, &cookie->c, bodysize);
  1331. keylen = SCTP_SECRET_SIZE;
  1332. key = (char *)ep->secret_key[ep->current_key];
  1333. desc.tfm = sctp_sk(ep->base.sk)->hmac;
  1334. desc.flags = 0;
  1335. if (crypto_hash_setkey(desc.tfm, key, keylen) ||
  1336. crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
  1337. goto free_cookie;
  1338. }
  1339. return retval;
  1340. free_cookie:
  1341. kfree(retval);
  1342. nodata:
  1343. *cookie_len = 0;
  1344. return NULL;
  1345. }
  1346. /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
  1347. struct sctp_association *sctp_unpack_cookie(
  1348. const struct sctp_endpoint *ep,
  1349. const struct sctp_association *asoc,
  1350. struct sctp_chunk *chunk, gfp_t gfp,
  1351. int *error, struct sctp_chunk **errp)
  1352. {
  1353. struct sctp_association *retval = NULL;
  1354. struct sctp_signed_cookie *cookie;
  1355. struct sctp_cookie *bear_cookie;
  1356. int headersize, bodysize, fixed_size;
  1357. __u8 *digest = ep->digest;
  1358. struct scatterlist sg;
  1359. unsigned int keylen, len;
  1360. char *key;
  1361. sctp_scope_t scope;
  1362. struct sk_buff *skb = chunk->skb;
  1363. struct timeval tv;
  1364. struct hash_desc desc;
  1365. /* Header size is static data prior to the actual cookie, including
  1366. * any padding.
  1367. */
  1368. headersize = sizeof(sctp_chunkhdr_t) +
  1369. (sizeof(struct sctp_signed_cookie) -
  1370. sizeof(struct sctp_cookie));
  1371. bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
  1372. fixed_size = headersize + sizeof(struct sctp_cookie);
  1373. /* Verify that the chunk looks like it even has a cookie.
  1374. * There must be enough room for our cookie and our peer's
  1375. * INIT chunk.
  1376. */
  1377. len = ntohs(chunk->chunk_hdr->length);
  1378. if (len < fixed_size + sizeof(struct sctp_chunkhdr))
  1379. goto malformed;
  1380. /* Verify that the cookie has been padded out. */
  1381. if (bodysize % SCTP_COOKIE_MULTIPLE)
  1382. goto malformed;
  1383. /* Process the cookie. */
  1384. cookie = chunk->subh.cookie_hdr;
  1385. bear_cookie = &cookie->c;
  1386. if (!sctp_sk(ep->base.sk)->hmac)
  1387. goto no_hmac;
  1388. /* Check the signature. */
  1389. keylen = SCTP_SECRET_SIZE;
  1390. sg_init_one(&sg, bear_cookie, bodysize);
  1391. key = (char *)ep->secret_key[ep->current_key];
  1392. desc.tfm = sctp_sk(ep->base.sk)->hmac;
  1393. desc.flags = 0;
  1394. memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
  1395. if (crypto_hash_setkey(desc.tfm, key, keylen) ||
  1396. crypto_hash_digest(&desc, &sg, bodysize, digest)) {
  1397. *error = -SCTP_IERROR_NOMEM;
  1398. goto fail;
  1399. }
  1400. if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
  1401. /* Try the previous key. */
  1402. key = (char *)ep->secret_key[ep->last_key];
  1403. memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
  1404. if (crypto_hash_setkey(desc.tfm, key, keylen) ||
  1405. crypto_hash_digest(&desc, &sg, bodysize, digest)) {
  1406. *error = -SCTP_IERROR_NOMEM;
  1407. goto fail;
  1408. }
  1409. if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
  1410. /* Yikes! Still bad signature! */
  1411. *error = -SCTP_IERROR_BAD_SIG;
  1412. goto fail;
  1413. }
  1414. }
  1415. no_hmac:
  1416. /* IG Section 2.35.2:
  1417. * 3) Compare the port numbers and the verification tag contained
  1418. * within the COOKIE ECHO chunk to the actual port numbers and the
  1419. * verification tag within the SCTP common header of the received
  1420. * packet. If these values do not match the packet MUST be silently
  1421. * discarded,
  1422. */
  1423. if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
  1424. *error = -SCTP_IERROR_BAD_TAG;
  1425. goto fail;
  1426. }
  1427. if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
  1428. ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
  1429. *error = -SCTP_IERROR_BAD_PORTS;
  1430. goto fail;
  1431. }
  1432. /* Check to see if the cookie is stale. If there is already
  1433. * an association, there is no need to check cookie's expiration
  1434. * for init collision case of lost COOKIE ACK.
  1435. * If skb has been timestamped, then use the stamp, otherwise
  1436. * use current time. This introduces a small possibility that
  1437. * that a cookie may be considered expired, but his would only slow
  1438. * down the new association establishment instead of every packet.
  1439. */
  1440. if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
  1441. skb_get_timestamp(skb, &tv);
  1442. else
  1443. do_gettimeofday(&tv);
  1444. if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
  1445. /*
  1446. * Section 3.3.10.3 Stale Cookie Error (3)
  1447. *
  1448. * Cause of error
  1449. * ---------------
  1450. * Stale Cookie Error: Indicates the receipt of a valid State
  1451. * Cookie that has expired.
  1452. */
  1453. len = ntohs(chunk->chunk_hdr->length);
  1454. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1455. if (*errp) {
  1456. suseconds_t usecs = (tv.tv_sec -
  1457. bear_cookie->expiration.tv_sec) * 1000000L +
  1458. tv.tv_usec - bear_cookie->expiration.tv_usec;
  1459. __be32 n = htonl(usecs);
  1460. sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
  1461. sizeof(n));
  1462. sctp_addto_chunk(*errp, sizeof(n), &n);
  1463. *error = -SCTP_IERROR_STALE_COOKIE;
  1464. } else
  1465. *error = -SCTP_IERROR_NOMEM;
  1466. goto fail;
  1467. }
  1468. /* Make a new base association. */
  1469. scope = sctp_scope(sctp_source(chunk));
  1470. retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
  1471. if (!retval) {
  1472. *error = -SCTP_IERROR_NOMEM;
  1473. goto fail;
  1474. }
  1475. /* Set up our peer's port number. */
  1476. retval->peer.port = ntohs(chunk->sctp_hdr->source);
  1477. /* Populate the association from the cookie. */
  1478. memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
  1479. if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
  1480. GFP_ATOMIC) < 0) {
  1481. *error = -SCTP_IERROR_NOMEM;
  1482. goto fail;
  1483. }
  1484. /* Also, add the destination address. */
  1485. if (list_empty(&retval->base.bind_addr.address_list)) {
  1486. sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
  1487. SCTP_ADDR_SRC, GFP_ATOMIC);
  1488. }
  1489. retval->next_tsn = retval->c.initial_tsn;
  1490. retval->ctsn_ack_point = retval->next_tsn - 1;
  1491. retval->addip_serial = retval->c.initial_tsn;
  1492. retval->adv_peer_ack_point = retval->ctsn_ack_point;
  1493. retval->peer.prsctp_capable = retval->c.prsctp_capable;
  1494. retval->peer.adaptation_ind = retval->c.adaptation_ind;
  1495. /* The INIT stuff will be done by the side effects. */
  1496. return retval;
  1497. fail:
  1498. if (retval)
  1499. sctp_association_free(retval);
  1500. return NULL;
  1501. malformed:
  1502. /* Yikes! The packet is either corrupt or deliberately
  1503. * malformed.
  1504. */
  1505. *error = -SCTP_IERROR_MALFORMED;
  1506. goto fail;
  1507. }
  1508. /********************************************************************
  1509. * 3rd Level Abstractions
  1510. ********************************************************************/
  1511. struct __sctp_missing {
  1512. __be32 num_missing;
  1513. __be16 type;
  1514. } __attribute__((packed));
  1515. /*
  1516. * Report a missing mandatory parameter.
  1517. */
  1518. static int sctp_process_missing_param(const struct sctp_association *asoc,
  1519. sctp_param_t paramtype,
  1520. struct sctp_chunk *chunk,
  1521. struct sctp_chunk **errp)
  1522. {
  1523. struct __sctp_missing report;
  1524. __u16 len;
  1525. len = WORD_ROUND(sizeof(report));
  1526. /* Make an ERROR chunk, preparing enough room for
  1527. * returning multiple unknown parameters.
  1528. */
  1529. if (!*errp)
  1530. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1531. if (*errp) {
  1532. report.num_missing = htonl(1);
  1533. report.type = paramtype;
  1534. sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
  1535. sizeof(report));
  1536. sctp_addto_chunk(*errp, sizeof(report), &report);
  1537. }
  1538. /* Stop processing this chunk. */
  1539. return 0;
  1540. }
  1541. /* Report an Invalid Mandatory Parameter. */
  1542. static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
  1543. struct sctp_chunk *chunk,
  1544. struct sctp_chunk **errp)
  1545. {
  1546. /* Invalid Mandatory Parameter Error has no payload. */
  1547. if (!*errp)
  1548. *errp = sctp_make_op_error_space(asoc, chunk, 0);
  1549. if (*errp)
  1550. sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
  1551. /* Stop processing this chunk. */
  1552. return 0;
  1553. }
  1554. static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
  1555. struct sctp_paramhdr *param,
  1556. const struct sctp_chunk *chunk,
  1557. struct sctp_chunk **errp)
  1558. {
  1559. /* This is a fatal error. Any accumulated non-fatal errors are
  1560. * not reported.
  1561. */
  1562. if (*errp)
  1563. sctp_chunk_free(*errp);
  1564. /* Create an error chunk and fill it in with our payload. */
  1565. *errp = sctp_make_violation_paramlen(asoc, chunk, param);
  1566. return 0;
  1567. }
  1568. /* Do not attempt to handle the HOST_NAME parm. However, do
  1569. * send back an indicator to the peer.
  1570. */
  1571. static int sctp_process_hn_param(const struct sctp_association *asoc,
  1572. union sctp_params param,
  1573. struct sctp_chunk *chunk,
  1574. struct sctp_chunk **errp)
  1575. {
  1576. __u16 len = ntohs(param.p->length);
  1577. /* Processing of the HOST_NAME parameter will generate an
  1578. * ABORT. If we've accumulated any non-fatal errors, they
  1579. * would be unrecognized parameters and we should not include
  1580. * them in the ABORT.
  1581. */
  1582. if (*errp)
  1583. sctp_chunk_free(*errp);
  1584. *errp = sctp_make_op_error_space(asoc, chunk, len);
  1585. if (*errp) {
  1586. sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
  1587. sctp_addto_chunk(*errp, len, param.v);
  1588. }
  1589. /* Stop processing this chunk. */
  1590. return 0;
  1591. }
  1592. static int sctp_verify_ext_param(union sctp_params param)
  1593. {
  1594. __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
  1595. int have_auth = 0;
  1596. int have_asconf = 0;
  1597. int i;
  1598. for (i = 0; i < num_ext; i++) {
  1599. switch (param.ext->chunks[i]) {
  1600. case SCTP_CID_AUTH:
  1601. have_auth = 1;
  1602. break;
  1603. case SCTP_CID_ASCONF:
  1604. case SCTP_CID_ASCONF_ACK:
  1605. have_asconf = 1;
  1606. break;
  1607. }
  1608. }
  1609. /* ADD-IP Security: The draft requires us to ABORT or ignore the
  1610. * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
  1611. * only if ADD-IP is turned on and we are not backward-compatible
  1612. * mode.
  1613. */
  1614. if (sctp_addip_noauth)
  1615. return 1;
  1616. if (sctp_addip_enable && !have_auth && have_asconf)
  1617. return 0;
  1618. return 1;
  1619. }
  1620. static void sctp_process_ext_param(struct sctp_association *asoc,
  1621. union sctp_params param)
  1622. {
  1623. __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
  1624. int i;
  1625. for (i = 0; i < num_ext; i++) {
  1626. switch (param.ext->chunks[i]) {
  1627. case SCTP_CID_FWD_TSN:
  1628. if (sctp_prsctp_enable &&
  1629. !asoc->peer.prsctp_capable)
  1630. asoc->peer.prsctp_capable = 1;
  1631. break;
  1632. case SCTP_CID_AUTH:
  1633. /* if the peer reports AUTH, assume that he
  1634. * supports AUTH.
  1635. */
  1636. if (sctp_auth_enable)
  1637. asoc->peer.auth_capable = 1;
  1638. break;
  1639. case SCTP_CID_ASCONF:
  1640. case SCTP_CID_ASCONF_ACK:
  1641. if (sctp_addip_enable)
  1642. asoc->peer.asconf_capable = 1;
  1643. break;
  1644. default:
  1645. break;
  1646. }
  1647. }
  1648. }
  1649. /* RFC 3.2.1 & the Implementers Guide 2.2.
  1650. *
  1651. * The Parameter Types are encoded such that the
  1652. * highest-order two bits specify the action that must be
  1653. * taken if the processing endpoint does not recognize the
  1654. * Parameter Type.
  1655. *
  1656. * 00 - Stop processing this parameter; do not process any further
  1657. * parameters within this chunk
  1658. *
  1659. * 01 - Stop processing this parameter, do not process any further
  1660. * parameters within this chunk, and report the unrecognized
  1661. * parameter in an 'Unrecognized Parameter' ERROR chunk.
  1662. *
  1663. * 10 - Skip this parameter and continue processing.
  1664. *
  1665. * 11 - Skip this parameter and continue processing but
  1666. * report the unrecognized parameter in an
  1667. * 'Unrecognized Parameter' ERROR chunk.
  1668. *
  1669. * Return value:
  1670. * SCTP_IERROR_NO_ERROR - continue with the chunk
  1671. * SCTP_IERROR_ERROR - stop and report an error.
  1672. * SCTP_IERROR_NOMEME - out of memory.
  1673. */
  1674. static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
  1675. union sctp_params param,
  1676. struct sctp_chunk *chunk,
  1677. struct sctp_chunk **errp)
  1678. {
  1679. int retval = SCTP_IERROR_NO_ERROR;
  1680. switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
  1681. case SCTP_PARAM_ACTION_DISCARD:
  1682. retval = SCTP_IERROR_ERROR;
  1683. break;
  1684. case SCTP_PARAM_ACTION_SKIP:
  1685. break;
  1686. case SCTP_PARAM_ACTION_DISCARD_ERR:
  1687. retval = SCTP_IERROR_ERROR;
  1688. /* Fall through */
  1689. case SCTP_PARAM_ACTION_SKIP_ERR:
  1690. /* Make an ERROR chunk, preparing enough room for
  1691. * returning multiple unknown parameters.
  1692. */
  1693. if (NULL == *errp)
  1694. *errp = sctp_make_op_error_space(asoc, chunk,
  1695. ntohs(chunk->chunk_hdr->length));
  1696. if (*errp) {
  1697. sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
  1698. WORD_ROUND(ntohs(param.p->length)));
  1699. sctp_addto_chunk(*errp,
  1700. WORD_ROUND(ntohs(param.p->length)),
  1701. param.v);
  1702. } else {
  1703. /* If there is no memory for generating the ERROR
  1704. * report as specified, an ABORT will be triggered
  1705. * to the peer and the association won't be
  1706. * established.
  1707. */
  1708. retval = SCTP_IERROR_NOMEM;
  1709. }
  1710. break;
  1711. default:
  1712. break;
  1713. }
  1714. return retval;
  1715. }
  1716. /* Verify variable length parameters
  1717. * Return values:
  1718. * SCTP_IERROR_ABORT - trigger an ABORT
  1719. * SCTP_IERROR_NOMEM - out of memory (abort)
  1720. * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
  1721. * SCTP_IERROR_NO_ERROR - continue with the chunk
  1722. */
  1723. static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
  1724. union sctp_params param,
  1725. sctp_cid_t cid,
  1726. struct sctp_chunk *chunk,
  1727. struct sctp_chunk **err_chunk)
  1728. {
  1729. struct sctp_hmac_algo_param *hmacs;
  1730. int retval = SCTP_IERROR_NO_ERROR;
  1731. __u16 n_elt, id = 0;
  1732. int i;
  1733. /* FIXME - This routine is not looking at each parameter per the
  1734. * chunk type, i.e., unrecognized parameters should be further
  1735. * identified based on the chunk id.
  1736. */
  1737. switch (param.p->type) {
  1738. case SCTP_PARAM_IPV4_ADDRESS:
  1739. case SCTP_PARAM_IPV6_ADDRESS:
  1740. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  1741. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  1742. case SCTP_PARAM_STATE_COOKIE:
  1743. case SCTP_PARAM_HEARTBEAT_INFO:
  1744. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  1745. case SCTP_PARAM_ECN_CAPABLE:
  1746. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  1747. break;
  1748. case SCTP_PARAM_SUPPORTED_EXT:
  1749. if (!sctp_verify_ext_param(param))
  1750. return SCTP_IERROR_ABORT;
  1751. break;
  1752. case SCTP_PARAM_SET_PRIMARY:
  1753. if (sctp_addip_enable)
  1754. break;
  1755. goto fallthrough;
  1756. case SCTP_PARAM_HOST_NAME_ADDRESS:
  1757. /* Tell the peer, we won't support this param. */
  1758. sctp_process_hn_param(asoc, param, chunk, err_chunk);
  1759. retval = SCTP_IERROR_ABORT;
  1760. break;
  1761. case SCTP_PARAM_FWD_TSN_SUPPORT:
  1762. if (sctp_prsctp_enable)
  1763. break;
  1764. goto fallthrough;
  1765. case SCTP_PARAM_RANDOM:
  1766. if (!sctp_auth_enable)
  1767. goto fallthrough;
  1768. /* SCTP-AUTH: Secion 6.1
  1769. * If the random number is not 32 byte long the association
  1770. * MUST be aborted. The ABORT chunk SHOULD contain the error
  1771. * cause 'Protocol Violation'.
  1772. */
  1773. if (SCTP_AUTH_RANDOM_LENGTH !=
  1774. ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
  1775. sctp_process_inv_paramlength(asoc, param.p,
  1776. chunk, err_chunk);
  1777. retval = SCTP_IERROR_ABORT;
  1778. }
  1779. break;
  1780. case SCTP_PARAM_CHUNKS:
  1781. if (!sctp_auth_enable)
  1782. goto fallthrough;
  1783. /* SCTP-AUTH: Section 3.2
  1784. * The CHUNKS parameter MUST be included once in the INIT or
  1785. * INIT-ACK chunk if the sender wants to receive authenticated
  1786. * chunks. Its maximum length is 260 bytes.
  1787. */
  1788. if (260 < ntohs(param.p->length)) {
  1789. sctp_process_inv_paramlength(asoc, param.p,
  1790. chunk, err_chunk);
  1791. retval = SCTP_IERROR_ABORT;
  1792. }
  1793. break;
  1794. case SCTP_PARAM_HMAC_ALGO:
  1795. if (!sctp_auth_enable)
  1796. goto fallthrough;
  1797. hmacs = (struct sctp_hmac_algo_param *)param.p;
  1798. n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
  1799. /* SCTP-AUTH: Section 6.1
  1800. * The HMAC algorithm based on SHA-1 MUST be supported and
  1801. * included in the HMAC-ALGO parameter.
  1802. */
  1803. for (i = 0; i < n_elt; i++) {
  1804. id = ntohs(hmacs->hmac_ids[i]);
  1805. if (id == SCTP_AUTH_HMAC_ID_SHA1)
  1806. break;
  1807. }
  1808. if (id != SCTP_AUTH_HMAC_ID_SHA1) {
  1809. sctp_process_inv_paramlength(asoc, param.p, chunk,
  1810. err_chunk);
  1811. retval = SCTP_IERROR_ABORT;
  1812. }
  1813. break;
  1814. fallthrough:
  1815. default:
  1816. SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
  1817. ntohs(param.p->type), cid);
  1818. retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
  1819. break;
  1820. }
  1821. return retval;
  1822. }
  1823. /* Verify the INIT packet before we process it. */
  1824. int sctp_verify_init(const struct sctp_association *asoc,
  1825. sctp_cid_t cid,
  1826. sctp_init_chunk_t *peer_init,
  1827. struct sctp_chunk *chunk,
  1828. struct sctp_chunk **errp)
  1829. {
  1830. union sctp_params param;
  1831. int has_cookie = 0;
  1832. int result;
  1833. /* Verify stream values are non-zero. */
  1834. if ((0 == peer_init->init_hdr.num_outbound_streams) ||
  1835. (0 == peer_init->init_hdr.num_inbound_streams) ||
  1836. (0 == peer_init->init_hdr.init_tag) ||
  1837. (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
  1838. return sctp_process_inv_mandatory(asoc, chunk, errp);
  1839. }
  1840. /* Check for missing mandatory parameters. */
  1841. sctp_walk_params(param, peer_init, init_hdr.params) {
  1842. if (SCTP_PARAM_STATE_COOKIE == param.p->type)
  1843. has_cookie = 1;
  1844. } /* for (loop through all parameters) */
  1845. /* There is a possibility that a parameter length was bad and
  1846. * in that case we would have stoped walking the parameters.
  1847. * The current param.p would point at the bad one.
  1848. * Current consensus on the mailing list is to generate a PROTOCOL
  1849. * VIOLATION error. We build the ERROR chunk here and let the normal
  1850. * error handling code build and send the packet.
  1851. */
  1852. if (param.v != (void*)chunk->chunk_end)
  1853. return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
  1854. /* The only missing mandatory param possible today is
  1855. * the state cookie for an INIT-ACK chunk.
  1856. */
  1857. if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
  1858. return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
  1859. chunk, errp);
  1860. /* Verify all the variable length parameters */
  1861. sctp_walk_params(param, peer_init, init_hdr.params) {
  1862. result = sctp_verify_param(asoc, param, cid, chunk, errp);
  1863. switch (result) {
  1864. case SCTP_IERROR_ABORT:
  1865. case SCTP_IERROR_NOMEM:
  1866. return 0;
  1867. case SCTP_IERROR_ERROR:
  1868. return 1;
  1869. case SCTP_IERROR_NO_ERROR:
  1870. default:
  1871. break;
  1872. }
  1873. } /* for (loop through all parameters) */
  1874. return 1;
  1875. }
  1876. /* Unpack the parameters in an INIT packet into an association.
  1877. * Returns 0 on failure, else success.
  1878. * FIXME: This is an association method.
  1879. */
  1880. int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
  1881. const union sctp_addr *peer_addr,
  1882. sctp_init_chunk_t *peer_init, gfp_t gfp)
  1883. {
  1884. union sctp_params param;
  1885. struct sctp_transport *transport;
  1886. struct list_head *pos, *temp;
  1887. char *cookie;
  1888. /* We must include the address that the INIT packet came from.
  1889. * This is the only address that matters for an INIT packet.
  1890. * When processing a COOKIE ECHO, we retrieve the from address
  1891. * of the INIT from the cookie.
  1892. */
  1893. /* This implementation defaults to making the first transport
  1894. * added as the primary transport. The source address seems to
  1895. * be a a better choice than any of the embedded addresses.
  1896. */
  1897. if (peer_addr) {
  1898. if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
  1899. goto nomem;
  1900. }
  1901. /* Process the initialization parameters. */
  1902. sctp_walk_params(param, peer_init, init_hdr.params) {
  1903. if (!sctp_process_param(asoc, param, peer_addr, gfp))
  1904. goto clean_up;
  1905. }
  1906. /* AUTH: After processing the parameters, make sure that we
  1907. * have all the required info to potentially do authentications.
  1908. */
  1909. if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
  1910. !asoc->peer.peer_hmacs))
  1911. asoc->peer.auth_capable = 0;
  1912. /* In a non-backward compatible mode, if the peer claims
  1913. * support for ADD-IP but not AUTH, the ADD-IP spec states
  1914. * that we MUST ABORT the association. Section 6. The section
  1915. * also give us an option to silently ignore the packet, which
  1916. * is what we'll do here.
  1917. */
  1918. if (!sctp_addip_noauth &&
  1919. (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
  1920. asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
  1921. SCTP_PARAM_DEL_IP |
  1922. SCTP_PARAM_SET_PRIMARY);
  1923. asoc->peer.asconf_capable = 0;
  1924. goto clean_up;
  1925. }
  1926. /* Walk list of transports, removing transports in the UNKNOWN state. */
  1927. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  1928. transport = list_entry(pos, struct sctp_transport, transports);
  1929. if (transport->state == SCTP_UNKNOWN) {
  1930. sctp_assoc_rm_peer(asoc, transport);
  1931. }
  1932. }
  1933. /* The fixed INIT headers are always in network byte
  1934. * order.
  1935. */
  1936. asoc->peer.i.init_tag =
  1937. ntohl(peer_init->init_hdr.init_tag);
  1938. asoc->peer.i.a_rwnd =
  1939. ntohl(peer_init->init_hdr.a_rwnd);
  1940. asoc->peer.i.num_outbound_streams =
  1941. ntohs(peer_init->init_hdr.num_outbound_streams);
  1942. asoc->peer.i.num_inbound_streams =
  1943. ntohs(peer_init->init_hdr.num_inbound_streams);
  1944. asoc->peer.i.initial_tsn =
  1945. ntohl(peer_init->init_hdr.initial_tsn);
  1946. /* Apply the upper bounds for output streams based on peer's
  1947. * number of inbound streams.
  1948. */
  1949. if (asoc->c.sinit_num_ostreams >
  1950. ntohs(peer_init->init_hdr.num_inbound_streams)) {
  1951. asoc->c.sinit_num_ostreams =
  1952. ntohs(peer_init->init_hdr.num_inbound_streams);
  1953. }
  1954. if (asoc->c.sinit_max_instreams >
  1955. ntohs(peer_init->init_hdr.num_outbound_streams)) {
  1956. asoc->c.sinit_max_instreams =
  1957. ntohs(peer_init->init_hdr.num_outbound_streams);
  1958. }
  1959. /* Copy Initiation tag from INIT to VT_peer in cookie. */
  1960. asoc->c.peer_vtag = asoc->peer.i.init_tag;
  1961. /* Peer Rwnd : Current calculated value of the peer's rwnd. */
  1962. asoc->peer.rwnd = asoc->peer.i.a_rwnd;
  1963. /* Copy cookie in case we need to resend COOKIE-ECHO. */
  1964. cookie = asoc->peer.cookie;
  1965. if (cookie) {
  1966. asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
  1967. if (!asoc->peer.cookie)
  1968. goto clean_up;
  1969. }
  1970. /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
  1971. * high (for example, implementations MAY use the size of the receiver
  1972. * advertised window).
  1973. */
  1974. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  1975. transports) {
  1976. transport->ssthresh = asoc->peer.i.a_rwnd;
  1977. }
  1978. /* Set up the TSN tracking pieces. */
  1979. if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
  1980. asoc->peer.i.initial_tsn, gfp))
  1981. goto clean_up;
  1982. /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
  1983. *
  1984. * The stream sequence number in all the streams shall start
  1985. * from 0 when the association is established. Also, when the
  1986. * stream sequence number reaches the value 65535 the next
  1987. * stream sequence number shall be set to 0.
  1988. */
  1989. /* Allocate storage for the negotiated streams if it is not a temporary
  1990. * association.
  1991. */
  1992. if (!asoc->temp) {
  1993. int error;
  1994. asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
  1995. asoc->c.sinit_num_ostreams, gfp);
  1996. if (!asoc->ssnmap)
  1997. goto clean_up;
  1998. error = sctp_assoc_set_id(asoc, gfp);
  1999. if (error)
  2000. goto clean_up;
  2001. }
  2002. /* ADDIP Section 4.1 ASCONF Chunk Procedures
  2003. *
  2004. * When an endpoint has an ASCONF signaled change to be sent to the
  2005. * remote endpoint it should do the following:
  2006. * ...
  2007. * A2) A serial number should be assigned to the Chunk. The serial
  2008. * number should be a monotonically increasing number. All serial
  2009. * numbers are defined to be initialized at the start of the
  2010. * association to the same value as the Initial TSN.
  2011. */
  2012. asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
  2013. return 1;
  2014. clean_up:
  2015. /* Release the transport structures. */
  2016. list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
  2017. transport = list_entry(pos, struct sctp_transport, transports);
  2018. if (transport->state != SCTP_ACTIVE)
  2019. sctp_assoc_rm_peer(asoc, transport);
  2020. }
  2021. nomem:
  2022. return 0;
  2023. }
  2024. /* Update asoc with the option described in param.
  2025. *
  2026. * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
  2027. *
  2028. * asoc is the association to update.
  2029. * param is the variable length parameter to use for update.
  2030. * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
  2031. * If the current packet is an INIT we want to minimize the amount of
  2032. * work we do. In particular, we should not build transport
  2033. * structures for the addresses.
  2034. */
  2035. static int sctp_process_param(struct sctp_association *asoc,
  2036. union sctp_params param,
  2037. const union sctp_addr *peer_addr,
  2038. gfp_t gfp)
  2039. {
  2040. union sctp_addr addr;
  2041. int i;
  2042. __u16 sat;
  2043. int retval = 1;
  2044. sctp_scope_t scope;
  2045. time_t stale;
  2046. struct sctp_af *af;
  2047. union sctp_addr_param *addr_param;
  2048. struct sctp_transport *t;
  2049. /* We maintain all INIT parameters in network byte order all the
  2050. * time. This allows us to not worry about whether the parameters
  2051. * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
  2052. */
  2053. switch (param.p->type) {
  2054. case SCTP_PARAM_IPV6_ADDRESS:
  2055. if (PF_INET6 != asoc->base.sk->sk_family)
  2056. break;
  2057. goto do_addr_param;
  2058. case SCTP_PARAM_IPV4_ADDRESS:
  2059. /* v4 addresses are not allowed on v6-only socket */
  2060. if (ipv6_only_sock(asoc->base.sk))
  2061. break;
  2062. do_addr_param:
  2063. af = sctp_get_af_specific(param_type2af(param.p->type));
  2064. af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
  2065. scope = sctp_scope(peer_addr);
  2066. if (sctp_in_scope(&addr, scope))
  2067. if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
  2068. return 0;
  2069. break;
  2070. case SCTP_PARAM_COOKIE_PRESERVATIVE:
  2071. if (!sctp_cookie_preserve_enable)
  2072. break;
  2073. stale = ntohl(param.life->lifespan_increment);
  2074. /* Suggested Cookie Life span increment's unit is msec,
  2075. * (1/1000sec).
  2076. */
  2077. asoc->cookie_life.tv_sec += stale / 1000;
  2078. asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
  2079. break;
  2080. case SCTP_PARAM_HOST_NAME_ADDRESS:
  2081. SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
  2082. break;
  2083. case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
  2084. /* Turn off the default values first so we'll know which
  2085. * ones are really set by the peer.
  2086. */
  2087. asoc->peer.ipv4_address = 0;
  2088. asoc->peer.ipv6_address = 0;
  2089. /* Assume that peer supports the address family
  2090. * by which it sends a packet.
  2091. */
  2092. if (peer_addr->sa.sa_family == AF_INET6)
  2093. asoc->peer.ipv6_address = 1;
  2094. else if (peer_addr->sa.sa_family == AF_INET)
  2095. asoc->peer.ipv4_address = 1;
  2096. /* Cycle through address types; avoid divide by 0. */
  2097. sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
  2098. if (sat)
  2099. sat /= sizeof(__u16);
  2100. for (i = 0; i < sat; ++i) {
  2101. switch (param.sat->types[i]) {
  2102. case SCTP_PARAM_IPV4_ADDRESS:
  2103. asoc->peer.ipv4_address = 1;
  2104. break;
  2105. case SCTP_PARAM_IPV6_ADDRESS:
  2106. if (PF_INET6 == asoc->base.sk->sk_family)
  2107. asoc->peer.ipv6_address = 1;
  2108. break;
  2109. case SCTP_PARAM_HOST_NAME_ADDRESS:
  2110. asoc->peer.hostname_address = 1;
  2111. break;
  2112. default: /* Just ignore anything else. */
  2113. break;
  2114. }
  2115. }
  2116. break;
  2117. case SCTP_PARAM_STATE_COOKIE:
  2118. asoc->peer.cookie_len =
  2119. ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
  2120. asoc->peer.cookie = param.cookie->body;
  2121. break;
  2122. case SCTP_PARAM_HEARTBEAT_INFO:
  2123. /* Would be odd to receive, but it causes no problems. */
  2124. break;
  2125. case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
  2126. /* Rejected during verify stage. */
  2127. break;
  2128. case SCTP_PARAM_ECN_CAPABLE:
  2129. asoc->peer.ecn_capable = 1;
  2130. break;
  2131. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  2132. asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
  2133. break;
  2134. case SCTP_PARAM_SET_PRIMARY:
  2135. if (!sctp_addip_enable)
  2136. goto fall_through;
  2137. addr_param = param.v + sizeof(sctp_addip_param_t);
  2138. af = sctp_get_af_specific(param_type2af(param.p->type));
  2139. af->from_addr_param(&addr, addr_param,
  2140. htons(asoc->peer.port), 0);
  2141. /* if the address is invalid, we can't process it.
  2142. * XXX: see spec for what to do.
  2143. */
  2144. if (!af->addr_valid(&addr, NULL, NULL))
  2145. break;
  2146. t = sctp_assoc_lookup_paddr(asoc, &addr);
  2147. if (!t)
  2148. break;
  2149. sctp_assoc_set_primary(asoc, t);
  2150. break;
  2151. case SCTP_PARAM_SUPPORTED_EXT:
  2152. sctp_process_ext_param(asoc, param);
  2153. break;
  2154. case SCTP_PARAM_FWD_TSN_SUPPORT:
  2155. if (sctp_prsctp_enable) {
  2156. asoc->peer.prsctp_capable = 1;
  2157. break;
  2158. }
  2159. /* Fall Through */
  2160. goto fall_through;
  2161. case SCTP_PARAM_RANDOM:
  2162. if (!sctp_auth_enable)
  2163. goto fall_through;
  2164. /* Save peer's random parameter */
  2165. asoc->peer.peer_random = kmemdup(param.p,
  2166. ntohs(param.p->length), gfp);
  2167. if (!asoc->peer.peer_random) {
  2168. retval = 0;
  2169. break;
  2170. }
  2171. break;
  2172. case SCTP_PARAM_HMAC_ALGO:
  2173. if (!sctp_auth_enable)
  2174. goto fall_through;
  2175. /* Save peer's HMAC list */
  2176. asoc->peer.peer_hmacs = kmemdup(param.p,
  2177. ntohs(param.p->length), gfp);
  2178. if (!asoc->peer.peer_hmacs) {
  2179. retval = 0;
  2180. break;
  2181. }
  2182. /* Set the default HMAC the peer requested*/
  2183. sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
  2184. break;
  2185. case SCTP_PARAM_CHUNKS:
  2186. if (!sctp_auth_enable)
  2187. goto fall_through;
  2188. asoc->peer.peer_chunks = kmemdup(param.p,
  2189. ntohs(param.p->length), gfp);
  2190. if (!asoc->peer.peer_chunks)
  2191. retval = 0;
  2192. break;
  2193. fall_through:
  2194. default:
  2195. /* Any unrecognized parameters should have been caught
  2196. * and handled by sctp_verify_param() which should be
  2197. * called prior to this routine. Simply log the error
  2198. * here.
  2199. */
  2200. SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
  2201. ntohs(param.p->type), asoc);
  2202. break;
  2203. }
  2204. return retval;
  2205. }
  2206. /* Select a new verification tag. */
  2207. __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
  2208. {
  2209. /* I believe that this random number generator complies with RFC1750.
  2210. * A tag of 0 is reserved for special cases (e.g. INIT).
  2211. */
  2212. __u32 x;
  2213. do {
  2214. get_random_bytes(&x, sizeof(__u32));
  2215. } while (x == 0);
  2216. return x;
  2217. }
  2218. /* Select an initial TSN to send during startup. */
  2219. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
  2220. {
  2221. __u32 retval;
  2222. get_random_bytes(&retval, sizeof(__u32));
  2223. return retval;
  2224. }
  2225. /*
  2226. * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
  2227. * 0 1 2 3
  2228. * 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
  2229. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2230. * | Type = 0xC1 | Chunk Flags | Chunk Length |
  2231. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2232. * | Serial Number |
  2233. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2234. * | Address Parameter |
  2235. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2236. * | ASCONF Parameter #1 |
  2237. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2238. * \ \
  2239. * / .... /
  2240. * \ \
  2241. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2242. * | ASCONF Parameter #N |
  2243. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2244. *
  2245. * Address Parameter and other parameter will not be wrapped in this function
  2246. */
  2247. static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
  2248. union sctp_addr *addr,
  2249. int vparam_len)
  2250. {
  2251. sctp_addiphdr_t asconf;
  2252. struct sctp_chunk *retval;
  2253. int length = sizeof(asconf) + vparam_len;
  2254. union sctp_addr_param addrparam;
  2255. int addrlen;
  2256. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  2257. addrlen = af->to_addr_param(addr, &addrparam);
  2258. if (!addrlen)
  2259. return NULL;
  2260. length += addrlen;
  2261. /* Create the chunk. */
  2262. retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
  2263. if (!retval)
  2264. return NULL;
  2265. asconf.serial = htonl(asoc->addip_serial++);
  2266. retval->subh.addip_hdr =
  2267. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2268. retval->param_hdr.v =
  2269. sctp_addto_chunk(retval, addrlen, &addrparam);
  2270. return retval;
  2271. }
  2272. /* ADDIP
  2273. * 3.2.1 Add IP Address
  2274. * 0 1 2 3
  2275. * 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
  2276. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2277. * | Type = 0xC001 | Length = Variable |
  2278. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2279. * | ASCONF-Request Correlation ID |
  2280. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2281. * | Address Parameter |
  2282. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2283. *
  2284. * 3.2.2 Delete IP Address
  2285. * 0 1 2 3
  2286. * 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
  2287. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2288. * | Type = 0xC002 | Length = Variable |
  2289. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2290. * | ASCONF-Request Correlation ID |
  2291. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2292. * | Address Parameter |
  2293. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2294. *
  2295. */
  2296. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  2297. union sctp_addr *laddr,
  2298. struct sockaddr *addrs,
  2299. int addrcnt,
  2300. __be16 flags)
  2301. {
  2302. sctp_addip_param_t param;
  2303. struct sctp_chunk *retval;
  2304. union sctp_addr_param addr_param;
  2305. union sctp_addr *addr;
  2306. void *addr_buf;
  2307. struct sctp_af *af;
  2308. int paramlen = sizeof(param);
  2309. int addr_param_len = 0;
  2310. int totallen = 0;
  2311. int i;
  2312. /* Get total length of all the address parameters. */
  2313. addr_buf = addrs;
  2314. for (i = 0; i < addrcnt; i++) {
  2315. addr = (union sctp_addr *)addr_buf;
  2316. af = sctp_get_af_specific(addr->v4.sin_family);
  2317. addr_param_len = af->to_addr_param(addr, &addr_param);
  2318. totallen += paramlen;
  2319. totallen += addr_param_len;
  2320. addr_buf += af->sockaddr_len;
  2321. }
  2322. /* Create an asconf chunk with the required length. */
  2323. retval = sctp_make_asconf(asoc, laddr, totallen);
  2324. if (!retval)
  2325. return NULL;
  2326. /* Add the address parameters to the asconf chunk. */
  2327. addr_buf = addrs;
  2328. for (i = 0; i < addrcnt; i++) {
  2329. addr = (union sctp_addr *)addr_buf;
  2330. af = sctp_get_af_specific(addr->v4.sin_family);
  2331. addr_param_len = af->to_addr_param(addr, &addr_param);
  2332. param.param_hdr.type = flags;
  2333. param.param_hdr.length = htons(paramlen + addr_param_len);
  2334. param.crr_id = i;
  2335. sctp_addto_chunk(retval, paramlen, &param);
  2336. sctp_addto_chunk(retval, addr_param_len, &addr_param);
  2337. addr_buf += af->sockaddr_len;
  2338. }
  2339. return retval;
  2340. }
  2341. /* ADDIP
  2342. * 3.2.4 Set Primary IP Address
  2343. * 0 1 2 3
  2344. * 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
  2345. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2346. * | Type =0xC004 | Length = Variable |
  2347. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2348. * | ASCONF-Request Correlation ID |
  2349. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2350. * | Address Parameter |
  2351. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2352. *
  2353. * Create an ASCONF chunk with Set Primary IP address parameter.
  2354. */
  2355. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  2356. union sctp_addr *addr)
  2357. {
  2358. sctp_addip_param_t param;
  2359. struct sctp_chunk *retval;
  2360. int len = sizeof(param);
  2361. union sctp_addr_param addrparam;
  2362. int addrlen;
  2363. struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
  2364. addrlen = af->to_addr_param(addr, &addrparam);
  2365. if (!addrlen)
  2366. return NULL;
  2367. len += addrlen;
  2368. /* Create the chunk and make asconf header. */
  2369. retval = sctp_make_asconf(asoc, addr, len);
  2370. if (!retval)
  2371. return NULL;
  2372. param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
  2373. param.param_hdr.length = htons(len);
  2374. param.crr_id = 0;
  2375. sctp_addto_chunk(retval, sizeof(param), &param);
  2376. sctp_addto_chunk(retval, addrlen, &addrparam);
  2377. return retval;
  2378. }
  2379. /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
  2380. * 0 1 2 3
  2381. * 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
  2382. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2383. * | Type = 0x80 | Chunk Flags | Chunk Length |
  2384. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2385. * | Serial Number |
  2386. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2387. * | ASCONF Parameter Response#1 |
  2388. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2389. * \ \
  2390. * / .... /
  2391. * \ \
  2392. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2393. * | ASCONF Parameter Response#N |
  2394. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2395. *
  2396. * Create an ASCONF_ACK chunk with enough space for the parameter responses.
  2397. */
  2398. static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
  2399. __u32 serial, int vparam_len)
  2400. {
  2401. sctp_addiphdr_t asconf;
  2402. struct sctp_chunk *retval;
  2403. int length = sizeof(asconf) + vparam_len;
  2404. /* Create the chunk. */
  2405. retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
  2406. if (!retval)
  2407. return NULL;
  2408. asconf.serial = htonl(serial);
  2409. retval->subh.addip_hdr =
  2410. sctp_addto_chunk(retval, sizeof(asconf), &asconf);
  2411. return retval;
  2412. }
  2413. /* Add response parameters to an ASCONF_ACK chunk. */
  2414. static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
  2415. __be16 err_code, sctp_addip_param_t *asconf_param)
  2416. {
  2417. sctp_addip_param_t ack_param;
  2418. sctp_errhdr_t err_param;
  2419. int asconf_param_len = 0;
  2420. int err_param_len = 0;
  2421. __be16 response_type;
  2422. if (SCTP_ERROR_NO_ERROR == err_code) {
  2423. response_type = SCTP_PARAM_SUCCESS_REPORT;
  2424. } else {
  2425. response_type = SCTP_PARAM_ERR_CAUSE;
  2426. err_param_len = sizeof(err_param);
  2427. if (asconf_param)
  2428. asconf_param_len =
  2429. ntohs(asconf_param->param_hdr.length);
  2430. }
  2431. /* Add Success Indication or Error Cause Indication parameter. */
  2432. ack_param.param_hdr.type = response_type;
  2433. ack_param.param_hdr.length = htons(sizeof(ack_param) +
  2434. err_param_len +
  2435. asconf_param_len);
  2436. ack_param.crr_id = crr_id;
  2437. sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
  2438. if (SCTP_ERROR_NO_ERROR == err_code)
  2439. return;
  2440. /* Add Error Cause parameter. */
  2441. err_param.cause = err_code;
  2442. err_param.length = htons(err_param_len + asconf_param_len);
  2443. sctp_addto_chunk(chunk, err_param_len, &err_param);
  2444. /* Add the failed TLV copied from ASCONF chunk. */
  2445. if (asconf_param)
  2446. sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
  2447. }
  2448. /* Process a asconf parameter. */
  2449. static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
  2450. struct sctp_chunk *asconf,
  2451. sctp_addip_param_t *asconf_param)
  2452. {
  2453. struct sctp_transport *peer;
  2454. struct sctp_af *af;
  2455. union sctp_addr addr;
  2456. union sctp_addr_param *addr_param;
  2457. addr_param = (union sctp_addr_param *)
  2458. ((void *)asconf_param + sizeof(sctp_addip_param_t));
  2459. if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
  2460. asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
  2461. asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
  2462. return SCTP_ERROR_UNKNOWN_PARAM;
  2463. switch (addr_param->v4.param_hdr.type) {
  2464. case SCTP_PARAM_IPV6_ADDRESS:
  2465. if (!asoc->peer.ipv6_address)
  2466. return SCTP_ERROR_DNS_FAILED;
  2467. break;
  2468. case SCTP_PARAM_IPV4_ADDRESS:
  2469. if (!asoc->peer.ipv4_address)
  2470. return SCTP_ERROR_DNS_FAILED;
  2471. break;
  2472. default:
  2473. return SCTP_ERROR_DNS_FAILED;
  2474. }
  2475. af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
  2476. if (unlikely(!af))
  2477. return SCTP_ERROR_DNS_FAILED;
  2478. af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
  2479. /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
  2480. * or multicast address.
  2481. * (note: wildcard is permitted and requires special handling so
  2482. * make sure we check for that)
  2483. */
  2484. if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
  2485. return SCTP_ERROR_DNS_FAILED;
  2486. switch (asconf_param->param_hdr.type) {
  2487. case SCTP_PARAM_ADD_IP:
  2488. /* Section 4.2.1:
  2489. * If the address 0.0.0.0 or ::0 is provided, the source
  2490. * address of the packet MUST be added.
  2491. */
  2492. if (af->is_any(&addr))
  2493. memcpy(&addr, &asconf->source, sizeof(addr));
  2494. /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
  2495. * request and does not have the local resources to add this
  2496. * new address to the association, it MUST return an Error
  2497. * Cause TLV set to the new error code 'Operation Refused
  2498. * Due to Resource Shortage'.
  2499. */
  2500. peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
  2501. if (!peer)
  2502. return SCTP_ERROR_RSRC_LOW;
  2503. /* Start the heartbeat timer. */
  2504. if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
  2505. sctp_transport_hold(peer);
  2506. break;
  2507. case SCTP_PARAM_DEL_IP:
  2508. /* ADDIP 4.3 D7) If a request is received to delete the
  2509. * last remaining IP address of a peer endpoint, the receiver
  2510. * MUST send an Error Cause TLV with the error cause set to the
  2511. * new error code 'Request to Delete Last Remaining IP Address'.
  2512. */
  2513. if (asoc->peer.transport_count == 1)
  2514. return SCTP_ERROR_DEL_LAST_IP;
  2515. /* ADDIP 4.3 D8) If a request is received to delete an IP
  2516. * address which is also the source address of the IP packet
  2517. * which contained the ASCONF chunk, the receiver MUST reject
  2518. * this request. To reject the request the receiver MUST send
  2519. * an Error Cause TLV set to the new error code 'Request to
  2520. * Delete Source IP Address'
  2521. */
  2522. if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
  2523. return SCTP_ERROR_DEL_SRC_IP;
  2524. /* Section 4.2.2
  2525. * If the address 0.0.0.0 or ::0 is provided, all
  2526. * addresses of the peer except the source address of the
  2527. * packet MUST be deleted.
  2528. */
  2529. if (af->is_any(&addr)) {
  2530. sctp_assoc_set_primary(asoc, asconf->transport);
  2531. sctp_assoc_del_nonprimary_peers(asoc,
  2532. asconf->transport);
  2533. } else
  2534. sctp_assoc_del_peer(asoc, &addr);
  2535. break;
  2536. case SCTP_PARAM_SET_PRIMARY:
  2537. /* ADDIP Section 4.2.4
  2538. * If the address 0.0.0.0 or ::0 is provided, the receiver
  2539. * MAY mark the source address of the packet as its
  2540. * primary.
  2541. */
  2542. if (af->is_any(&addr))
  2543. memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
  2544. peer = sctp_assoc_lookup_paddr(asoc, &addr);
  2545. if (!peer)
  2546. return SCTP_ERROR_DNS_FAILED;
  2547. sctp_assoc_set_primary(asoc, peer);
  2548. break;
  2549. }
  2550. return SCTP_ERROR_NO_ERROR;
  2551. }
  2552. /* Verify the ASCONF packet before we process it. */
  2553. int sctp_verify_asconf(const struct sctp_association *asoc,
  2554. struct sctp_paramhdr *param_hdr, void *chunk_end,
  2555. struct sctp_paramhdr **errp) {
  2556. sctp_addip_param_t *asconf_param;
  2557. union sctp_params param;
  2558. int length, plen;
  2559. param.v = (sctp_paramhdr_t *) param_hdr;
  2560. while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
  2561. length = ntohs(param.p->length);
  2562. *errp = param.p;
  2563. if (param.v > chunk_end - length ||
  2564. length < sizeof(sctp_paramhdr_t))
  2565. return 0;
  2566. switch (param.p->type) {
  2567. case SCTP_PARAM_ADD_IP:
  2568. case SCTP_PARAM_DEL_IP:
  2569. case SCTP_PARAM_SET_PRIMARY:
  2570. asconf_param = (sctp_addip_param_t *)param.v;
  2571. plen = ntohs(asconf_param->param_hdr.length);
  2572. if (plen < sizeof(sctp_addip_param_t) +
  2573. sizeof(sctp_paramhdr_t))
  2574. return 0;
  2575. break;
  2576. case SCTP_PARAM_SUCCESS_REPORT:
  2577. case SCTP_PARAM_ADAPTATION_LAYER_IND:
  2578. if (length != sizeof(sctp_addip_param_t))
  2579. return 0;
  2580. break;
  2581. default:
  2582. break;
  2583. }
  2584. param.v += WORD_ROUND(length);
  2585. }
  2586. if (param.v != chunk_end)
  2587. return 0;
  2588. return 1;
  2589. }
  2590. /* Process an incoming ASCONF chunk with the next expected serial no. and
  2591. * return an ASCONF_ACK chunk to be sent in response.
  2592. */
  2593. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  2594. struct sctp_chunk *asconf)
  2595. {
  2596. sctp_addiphdr_t *hdr;
  2597. union sctp_addr_param *addr_param;
  2598. sctp_addip_param_t *asconf_param;
  2599. struct sctp_chunk *asconf_ack;
  2600. __be16 err_code;
  2601. int length = 0;
  2602. int chunk_len;
  2603. __u32 serial;
  2604. int all_param_pass = 1;
  2605. chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
  2606. hdr = (sctp_addiphdr_t *)asconf->skb->data;
  2607. serial = ntohl(hdr->serial);
  2608. /* Skip the addiphdr and store a pointer to address parameter. */
  2609. length = sizeof(sctp_addiphdr_t);
  2610. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2611. chunk_len -= length;
  2612. /* Skip the address parameter and store a pointer to the first
  2613. * asconf parameter.
  2614. */
  2615. length = ntohs(addr_param->v4.param_hdr.length);
  2616. asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
  2617. chunk_len -= length;
  2618. /* create an ASCONF_ACK chunk.
  2619. * Based on the definitions of parameters, we know that the size of
  2620. * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
  2621. * parameters.
  2622. */
  2623. asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
  2624. if (!asconf_ack)
  2625. goto done;
  2626. /* Process the TLVs contained within the ASCONF chunk. */
  2627. while (chunk_len > 0) {
  2628. err_code = sctp_process_asconf_param(asoc, asconf,
  2629. asconf_param);
  2630. /* ADDIP 4.1 A7)
  2631. * If an error response is received for a TLV parameter,
  2632. * all TLVs with no response before the failed TLV are
  2633. * considered successful if not reported. All TLVs after
  2634. * the failed response are considered unsuccessful unless
  2635. * a specific success indication is present for the parameter.
  2636. */
  2637. if (SCTP_ERROR_NO_ERROR != err_code)
  2638. all_param_pass = 0;
  2639. if (!all_param_pass)
  2640. sctp_add_asconf_response(asconf_ack,
  2641. asconf_param->crr_id, err_code,
  2642. asconf_param);
  2643. /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
  2644. * an IP address sends an 'Out of Resource' in its response, it
  2645. * MUST also fail any subsequent add or delete requests bundled
  2646. * in the ASCONF.
  2647. */
  2648. if (SCTP_ERROR_RSRC_LOW == err_code)
  2649. goto done;
  2650. /* Move to the next ASCONF param. */
  2651. length = ntohs(asconf_param->param_hdr.length);
  2652. asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
  2653. length);
  2654. chunk_len -= length;
  2655. }
  2656. done:
  2657. asoc->peer.addip_serial++;
  2658. /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
  2659. * after freeing the reference to old asconf ack if any.
  2660. */
  2661. if (asconf_ack) {
  2662. sctp_chunk_hold(asconf_ack);
  2663. list_add_tail(&asconf_ack->transmitted_list,
  2664. &asoc->asconf_ack_list);
  2665. }
  2666. return asconf_ack;
  2667. }
  2668. /* Process a asconf parameter that is successfully acked. */
  2669. static void sctp_asconf_param_success(struct sctp_association *asoc,
  2670. sctp_addip_param_t *asconf_param)
  2671. {
  2672. struct sctp_af *af;
  2673. union sctp_addr addr;
  2674. struct sctp_bind_addr *bp = &asoc->base.bind_addr;
  2675. union sctp_addr_param *addr_param;
  2676. struct sctp_transport *transport;
  2677. struct sctp_sockaddr_entry *saddr;
  2678. addr_param = (union sctp_addr_param *)
  2679. ((void *)asconf_param + sizeof(sctp_addip_param_t));
  2680. /* We have checked the packet before, so we do not check again. */
  2681. af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
  2682. af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
  2683. switch (asconf_param->param_hdr.type) {
  2684. case SCTP_PARAM_ADD_IP:
  2685. /* This is always done in BH context with a socket lock
  2686. * held, so the list can not change.
  2687. */
  2688. local_bh_disable();
  2689. list_for_each_entry(saddr, &bp->address_list, list) {
  2690. if (sctp_cmp_addr_exact(&saddr->a, &addr))
  2691. saddr->state = SCTP_ADDR_SRC;
  2692. }
  2693. local_bh_enable();
  2694. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2695. transports) {
  2696. if (transport->state == SCTP_ACTIVE)
  2697. continue;
  2698. dst_release(transport->dst);
  2699. sctp_transport_route(transport, NULL,
  2700. sctp_sk(asoc->base.sk));
  2701. }
  2702. break;
  2703. case SCTP_PARAM_DEL_IP:
  2704. local_bh_disable();
  2705. sctp_del_bind_addr(bp, &addr);
  2706. local_bh_enable();
  2707. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  2708. transports) {
  2709. dst_release(transport->dst);
  2710. sctp_transport_route(transport, NULL,
  2711. sctp_sk(asoc->base.sk));
  2712. }
  2713. break;
  2714. default:
  2715. break;
  2716. }
  2717. }
  2718. /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
  2719. * for the given asconf parameter. If there is no response for this parameter,
  2720. * return the error code based on the third argument 'no_err'.
  2721. * ADDIP 4.1
  2722. * A7) If an error response is received for a TLV parameter, all TLVs with no
  2723. * response before the failed TLV are considered successful if not reported.
  2724. * All TLVs after the failed response are considered unsuccessful unless a
  2725. * specific success indication is present for the parameter.
  2726. */
  2727. static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
  2728. sctp_addip_param_t *asconf_param,
  2729. int no_err)
  2730. {
  2731. sctp_addip_param_t *asconf_ack_param;
  2732. sctp_errhdr_t *err_param;
  2733. int length;
  2734. int asconf_ack_len;
  2735. __be16 err_code;
  2736. if (no_err)
  2737. err_code = SCTP_ERROR_NO_ERROR;
  2738. else
  2739. err_code = SCTP_ERROR_REQ_REFUSED;
  2740. asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
  2741. sizeof(sctp_chunkhdr_t);
  2742. /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
  2743. * the first asconf_ack parameter.
  2744. */
  2745. length = sizeof(sctp_addiphdr_t);
  2746. asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
  2747. length);
  2748. asconf_ack_len -= length;
  2749. while (asconf_ack_len > 0) {
  2750. if (asconf_ack_param->crr_id == asconf_param->crr_id) {
  2751. switch(asconf_ack_param->param_hdr.type) {
  2752. case SCTP_PARAM_SUCCESS_REPORT:
  2753. return SCTP_ERROR_NO_ERROR;
  2754. case SCTP_PARAM_ERR_CAUSE:
  2755. length = sizeof(sctp_addip_param_t);
  2756. err_param = (sctp_errhdr_t *)
  2757. ((void *)asconf_ack_param + length);
  2758. asconf_ack_len -= length;
  2759. if (asconf_ack_len > 0)
  2760. return err_param->cause;
  2761. else
  2762. return SCTP_ERROR_INV_PARAM;
  2763. break;
  2764. default:
  2765. return SCTP_ERROR_INV_PARAM;
  2766. }
  2767. }
  2768. length = ntohs(asconf_ack_param->param_hdr.length);
  2769. asconf_ack_param = (sctp_addip_param_t *)
  2770. ((void *)asconf_ack_param + length);
  2771. asconf_ack_len -= length;
  2772. }
  2773. return err_code;
  2774. }
  2775. /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
  2776. int sctp_process_asconf_ack(struct sctp_association *asoc,
  2777. struct sctp_chunk *asconf_ack)
  2778. {
  2779. struct sctp_chunk *asconf = asoc->addip_last_asconf;
  2780. union sctp_addr_param *addr_param;
  2781. sctp_addip_param_t *asconf_param;
  2782. int length = 0;
  2783. int asconf_len = asconf->skb->len;
  2784. int all_param_pass = 0;
  2785. int no_err = 1;
  2786. int retval = 0;
  2787. __be16 err_code = SCTP_ERROR_NO_ERROR;
  2788. /* Skip the chunkhdr and addiphdr from the last asconf sent and store
  2789. * a pointer to address parameter.
  2790. */
  2791. length = sizeof(sctp_addip_chunk_t);
  2792. addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
  2793. asconf_len -= length;
  2794. /* Skip the address parameter in the last asconf sent and store a
  2795. * pointer to the first asconf parameter.
  2796. */
  2797. length = ntohs(addr_param->v4.param_hdr.length);
  2798. asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
  2799. asconf_len -= length;
  2800. /* ADDIP 4.1
  2801. * A8) If there is no response(s) to specific TLV parameter(s), and no
  2802. * failures are indicated, then all request(s) are considered
  2803. * successful.
  2804. */
  2805. if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
  2806. all_param_pass = 1;
  2807. /* Process the TLVs contained in the last sent ASCONF chunk. */
  2808. while (asconf_len > 0) {
  2809. if (all_param_pass)
  2810. err_code = SCTP_ERROR_NO_ERROR;
  2811. else {
  2812. err_code = sctp_get_asconf_response(asconf_ack,
  2813. asconf_param,
  2814. no_err);
  2815. if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
  2816. no_err = 0;
  2817. }
  2818. switch (err_code) {
  2819. case SCTP_ERROR_NO_ERROR:
  2820. sctp_asconf_param_success(asoc, asconf_param);
  2821. break;
  2822. case SCTP_ERROR_RSRC_LOW:
  2823. retval = 1;
  2824. break;
  2825. case SCTP_ERROR_UNKNOWN_PARAM:
  2826. /* Disable sending this type of asconf parameter in
  2827. * future.
  2828. */
  2829. asoc->peer.addip_disabled_mask |=
  2830. asconf_param->param_hdr.type;
  2831. break;
  2832. case SCTP_ERROR_REQ_REFUSED:
  2833. case SCTP_ERROR_DEL_LAST_IP:
  2834. case SCTP_ERROR_DEL_SRC_IP:
  2835. default:
  2836. break;
  2837. }
  2838. /* Skip the processed asconf parameter and move to the next
  2839. * one.
  2840. */
  2841. length = ntohs(asconf_param->param_hdr.length);
  2842. asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
  2843. length);
  2844. asconf_len -= length;
  2845. }
  2846. /* Free the cached last sent asconf chunk. */
  2847. list_del_init(&asconf->transmitted_list);
  2848. sctp_chunk_free(asconf);
  2849. asoc->addip_last_asconf = NULL;
  2850. /* Send the next asconf chunk from the addip chunk queue. */
  2851. if (!list_empty(&asoc->addip_chunk_list)) {
  2852. struct list_head *entry = asoc->addip_chunk_list.next;
  2853. asconf = list_entry(entry, struct sctp_chunk, list);
  2854. list_del_init(entry);
  2855. /* Hold the chunk until an ASCONF_ACK is received. */
  2856. sctp_chunk_hold(asconf);
  2857. if (sctp_primitive_ASCONF(asoc, asconf))
  2858. sctp_chunk_free(asconf);
  2859. else
  2860. asoc->addip_last_asconf = asconf;
  2861. }
  2862. return retval;
  2863. }
  2864. /* Make a FWD TSN chunk. */
  2865. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  2866. __u32 new_cum_tsn, size_t nstreams,
  2867. struct sctp_fwdtsn_skip *skiplist)
  2868. {
  2869. struct sctp_chunk *retval = NULL;
  2870. struct sctp_fwdtsn_chunk *ftsn_chunk;
  2871. struct sctp_fwdtsn_hdr ftsn_hdr;
  2872. struct sctp_fwdtsn_skip skip;
  2873. size_t hint;
  2874. int i;
  2875. hint = (nstreams + 1) * sizeof(__u32);
  2876. retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
  2877. if (!retval)
  2878. return NULL;
  2879. ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
  2880. ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
  2881. retval->subh.fwdtsn_hdr =
  2882. sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
  2883. for (i = 0; i < nstreams; i++) {
  2884. skip.stream = skiplist[i].stream;
  2885. skip.ssn = skiplist[i].ssn;
  2886. sctp_addto_chunk(retval, sizeof(skip), &skip);
  2887. }
  2888. return retval;
  2889. }