sm_make_chunk.c 114 KB

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