sm_make_chunk.c 103 KB

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