sm_make_chunk.c 101 KB

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