sm_make_chunk.c 103 KB

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