sm_make_chunk.c 102 KB

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