sm_make_chunk.c 101 KB

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