sm_make_chunk.c 116 KB

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