sm_make_chunk.c 92 KB

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