sm_make_chunk.c 85 KB

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