sm_make_chunk.c 87 KB

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