sm_make_chunk.c 96 KB

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