sm_make_chunk.c 95 KB

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