sm_make_chunk.c 83 KB

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