sm_make_chunk.c 98 KB

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