sm_make_chunk.c 97 KB

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