sm_make_chunk.c 84 KB

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