sm_make_chunk.c 102 KB

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