sm_make_chunk.c 92 KB

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