sm_make_chunk.c 99 KB

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