sm_make_chunk.c 93 KB

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