sm_make_chunk.c 117 KB

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