sm_make_chunk.c 102 KB

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