sm_make_chunk.c 83 KB

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