sm_make_chunk.c 101 KB

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