sm_make_chunk.c 82 KB

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