sm_make_chunk.c 93 KB

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