sm_make_chunk.c 117 KB

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