sm_make_chunk.c 115 KB

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