stream.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * These functions manipulate sctp tsn mapping array.
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  31. * Written or modified by:
  32. * Xin Long <lucien.xin@gmail.com>
  33. */
  34. #include <net/sctp/sctp.h>
  35. #include <net/sctp/sm.h>
  36. int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
  37. gfp_t gfp)
  38. {
  39. int i;
  40. /* Initial stream->out size may be very big, so free it and alloc
  41. * a new one with new outcnt to save memory.
  42. */
  43. kfree(stream->out);
  44. stream->out = kcalloc(outcnt, sizeof(*stream->out), gfp);
  45. if (!stream->out)
  46. return -ENOMEM;
  47. stream->outcnt = outcnt;
  48. for (i = 0; i < stream->outcnt; i++)
  49. stream->out[i].state = SCTP_STREAM_OPEN;
  50. if (!incnt)
  51. return 0;
  52. stream->in = kcalloc(incnt, sizeof(*stream->in), gfp);
  53. if (!stream->in) {
  54. kfree(stream->out);
  55. stream->out = NULL;
  56. return -ENOMEM;
  57. }
  58. stream->incnt = incnt;
  59. return 0;
  60. }
  61. void sctp_stream_free(struct sctp_stream *stream)
  62. {
  63. kfree(stream->out);
  64. kfree(stream->in);
  65. }
  66. void sctp_stream_clear(struct sctp_stream *stream)
  67. {
  68. int i;
  69. for (i = 0; i < stream->outcnt; i++)
  70. stream->out[i].ssn = 0;
  71. for (i = 0; i < stream->incnt; i++)
  72. stream->in[i].ssn = 0;
  73. }
  74. void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
  75. {
  76. sctp_stream_free(stream);
  77. stream->out = new->out;
  78. stream->in = new->in;
  79. stream->outcnt = new->outcnt;
  80. stream->incnt = new->incnt;
  81. new->out = NULL;
  82. new->in = NULL;
  83. }
  84. static int sctp_send_reconf(struct sctp_association *asoc,
  85. struct sctp_chunk *chunk)
  86. {
  87. struct net *net = sock_net(asoc->base.sk);
  88. int retval = 0;
  89. retval = sctp_primitive_RECONF(net, asoc, chunk);
  90. if (retval)
  91. sctp_chunk_free(chunk);
  92. return retval;
  93. }
  94. int sctp_send_reset_streams(struct sctp_association *asoc,
  95. struct sctp_reset_streams *params)
  96. {
  97. struct sctp_stream *stream = &asoc->stream;
  98. __u16 i, str_nums, *str_list;
  99. struct sctp_chunk *chunk;
  100. int retval = -EINVAL;
  101. __be16 *nstr_list;
  102. bool out, in;
  103. if (!asoc->peer.reconf_capable ||
  104. !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
  105. retval = -ENOPROTOOPT;
  106. goto out;
  107. }
  108. if (asoc->strreset_outstanding) {
  109. retval = -EINPROGRESS;
  110. goto out;
  111. }
  112. out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
  113. in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
  114. if (!out && !in)
  115. goto out;
  116. str_nums = params->srs_number_streams;
  117. str_list = params->srs_stream_list;
  118. if (out && str_nums)
  119. for (i = 0; i < str_nums; i++)
  120. if (str_list[i] >= stream->outcnt)
  121. goto out;
  122. if (in && str_nums)
  123. for (i = 0; i < str_nums; i++)
  124. if (str_list[i] >= stream->incnt)
  125. goto out;
  126. nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
  127. if (!nstr_list) {
  128. retval = -ENOMEM;
  129. goto out;
  130. }
  131. for (i = 0; i < str_nums; i++)
  132. nstr_list[i] = htons(str_list[i]);
  133. chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
  134. kfree(nstr_list);
  135. if (!chunk) {
  136. retval = -ENOMEM;
  137. goto out;
  138. }
  139. if (out) {
  140. if (str_nums)
  141. for (i = 0; i < str_nums; i++)
  142. stream->out[str_list[i]].state =
  143. SCTP_STREAM_CLOSED;
  144. else
  145. for (i = 0; i < stream->outcnt; i++)
  146. stream->out[i].state = SCTP_STREAM_CLOSED;
  147. }
  148. asoc->strreset_chunk = chunk;
  149. sctp_chunk_hold(asoc->strreset_chunk);
  150. retval = sctp_send_reconf(asoc, chunk);
  151. if (retval) {
  152. sctp_chunk_put(asoc->strreset_chunk);
  153. asoc->strreset_chunk = NULL;
  154. if (!out)
  155. goto out;
  156. if (str_nums)
  157. for (i = 0; i < str_nums; i++)
  158. stream->out[str_list[i]].state =
  159. SCTP_STREAM_OPEN;
  160. else
  161. for (i = 0; i < stream->outcnt; i++)
  162. stream->out[i].state = SCTP_STREAM_OPEN;
  163. goto out;
  164. }
  165. asoc->strreset_outstanding = out + in;
  166. out:
  167. return retval;
  168. }
  169. int sctp_send_reset_assoc(struct sctp_association *asoc)
  170. {
  171. struct sctp_stream *stream = &asoc->stream;
  172. struct sctp_chunk *chunk = NULL;
  173. int retval;
  174. __u16 i;
  175. if (!asoc->peer.reconf_capable ||
  176. !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  177. return -ENOPROTOOPT;
  178. if (asoc->strreset_outstanding)
  179. return -EINPROGRESS;
  180. chunk = sctp_make_strreset_tsnreq(asoc);
  181. if (!chunk)
  182. return -ENOMEM;
  183. /* Block further xmit of data until this request is completed */
  184. for (i = 0; i < stream->outcnt; i++)
  185. stream->out[i].state = SCTP_STREAM_CLOSED;
  186. asoc->strreset_chunk = chunk;
  187. sctp_chunk_hold(asoc->strreset_chunk);
  188. retval = sctp_send_reconf(asoc, chunk);
  189. if (retval) {
  190. sctp_chunk_put(asoc->strreset_chunk);
  191. asoc->strreset_chunk = NULL;
  192. for (i = 0; i < stream->outcnt; i++)
  193. stream->out[i].state = SCTP_STREAM_OPEN;
  194. return retval;
  195. }
  196. asoc->strreset_outstanding = 1;
  197. return 0;
  198. }
  199. int sctp_send_add_streams(struct sctp_association *asoc,
  200. struct sctp_add_streams *params)
  201. {
  202. struct sctp_stream *stream = &asoc->stream;
  203. struct sctp_chunk *chunk = NULL;
  204. int retval = -ENOMEM;
  205. __u32 outcnt, incnt;
  206. __u16 out, in;
  207. if (!asoc->peer.reconf_capable ||
  208. !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
  209. retval = -ENOPROTOOPT;
  210. goto out;
  211. }
  212. if (asoc->strreset_outstanding) {
  213. retval = -EINPROGRESS;
  214. goto out;
  215. }
  216. out = params->sas_outstrms;
  217. in = params->sas_instrms;
  218. outcnt = stream->outcnt + out;
  219. incnt = stream->incnt + in;
  220. if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
  221. (!out && !in)) {
  222. retval = -EINVAL;
  223. goto out;
  224. }
  225. if (out) {
  226. struct sctp_stream_out *streamout;
  227. streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
  228. GFP_KERNEL);
  229. if (!streamout)
  230. goto out;
  231. memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
  232. stream->out = streamout;
  233. }
  234. chunk = sctp_make_strreset_addstrm(asoc, out, in);
  235. if (!chunk)
  236. goto out;
  237. asoc->strreset_chunk = chunk;
  238. sctp_chunk_hold(asoc->strreset_chunk);
  239. retval = sctp_send_reconf(asoc, chunk);
  240. if (retval) {
  241. sctp_chunk_put(asoc->strreset_chunk);
  242. asoc->strreset_chunk = NULL;
  243. goto out;
  244. }
  245. stream->incnt = incnt;
  246. stream->outcnt = outcnt;
  247. asoc->strreset_outstanding = !!out + !!in;
  248. out:
  249. return retval;
  250. }
  251. static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
  252. struct sctp_association *asoc, __be32 resp_seq,
  253. __be16 type)
  254. {
  255. struct sctp_chunk *chunk = asoc->strreset_chunk;
  256. struct sctp_reconf_chunk *hdr;
  257. union sctp_params param;
  258. if (!chunk)
  259. return NULL;
  260. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  261. sctp_walk_params(param, hdr, params) {
  262. /* sctp_strreset_tsnreq is actually the basic structure
  263. * of all stream reconf params, so it's safe to use it
  264. * to access request_seq.
  265. */
  266. struct sctp_strreset_tsnreq *req = param.v;
  267. if ((!resp_seq || req->request_seq == resp_seq) &&
  268. (!type || type == req->param_hdr.type))
  269. return param.v;
  270. }
  271. return NULL;
  272. }
  273. static void sctp_update_strreset_result(struct sctp_association *asoc,
  274. __u32 result)
  275. {
  276. asoc->strreset_result[1] = asoc->strreset_result[0];
  277. asoc->strreset_result[0] = result;
  278. }
  279. struct sctp_chunk *sctp_process_strreset_outreq(
  280. struct sctp_association *asoc,
  281. union sctp_params param,
  282. struct sctp_ulpevent **evp)
  283. {
  284. struct sctp_strreset_outreq *outreq = param.v;
  285. struct sctp_stream *stream = &asoc->stream;
  286. __u32 result = SCTP_STRRESET_DENIED;
  287. __u16 i, nums, flags = 0;
  288. __be16 *str_p = NULL;
  289. __u32 request_seq;
  290. request_seq = ntohl(outreq->request_seq);
  291. if (ntohl(outreq->send_reset_at_tsn) >
  292. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
  293. result = SCTP_STRRESET_IN_PROGRESS;
  294. goto err;
  295. }
  296. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  297. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  298. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  299. goto err;
  300. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  301. i = asoc->strreset_inseq - request_seq - 1;
  302. result = asoc->strreset_result[i];
  303. goto err;
  304. }
  305. asoc->strreset_inseq++;
  306. /* Check strreset_enable after inseq inc, as sender cannot tell
  307. * the peer doesn't enable strreset after receiving response with
  308. * result denied, as well as to keep consistent with bsd.
  309. */
  310. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  311. goto out;
  312. if (asoc->strreset_chunk) {
  313. if (!sctp_chunk_lookup_strreset_param(
  314. asoc, outreq->response_seq,
  315. SCTP_PARAM_RESET_IN_REQUEST)) {
  316. /* same process with outstanding isn't 0 */
  317. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  318. goto out;
  319. }
  320. asoc->strreset_outstanding--;
  321. asoc->strreset_outseq++;
  322. if (!asoc->strreset_outstanding) {
  323. struct sctp_transport *t;
  324. t = asoc->strreset_chunk->transport;
  325. if (del_timer(&t->reconf_timer))
  326. sctp_transport_put(t);
  327. sctp_chunk_put(asoc->strreset_chunk);
  328. asoc->strreset_chunk = NULL;
  329. }
  330. flags = SCTP_STREAM_RESET_INCOMING_SSN;
  331. }
  332. nums = (ntohs(param.p->length) - sizeof(*outreq)) / 2;
  333. if (nums) {
  334. str_p = outreq->list_of_streams;
  335. for (i = 0; i < nums; i++) {
  336. if (ntohs(str_p[i]) >= stream->incnt) {
  337. result = SCTP_STRRESET_ERR_WRONG_SSN;
  338. goto out;
  339. }
  340. }
  341. for (i = 0; i < nums; i++)
  342. stream->in[ntohs(str_p[i])].ssn = 0;
  343. } else {
  344. for (i = 0; i < stream->incnt; i++)
  345. stream->in[i].ssn = 0;
  346. }
  347. result = SCTP_STRRESET_PERFORMED;
  348. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  349. flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
  350. GFP_ATOMIC);
  351. out:
  352. sctp_update_strreset_result(asoc, result);
  353. err:
  354. return sctp_make_strreset_resp(asoc, result, request_seq);
  355. }
  356. struct sctp_chunk *sctp_process_strreset_inreq(
  357. struct sctp_association *asoc,
  358. union sctp_params param,
  359. struct sctp_ulpevent **evp)
  360. {
  361. struct sctp_strreset_inreq *inreq = param.v;
  362. struct sctp_stream *stream = &asoc->stream;
  363. __u32 result = SCTP_STRRESET_DENIED;
  364. struct sctp_chunk *chunk = NULL;
  365. __u32 request_seq;
  366. __u16 i, nums;
  367. __be16 *str_p;
  368. request_seq = ntohl(inreq->request_seq);
  369. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  370. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  371. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  372. goto err;
  373. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  374. i = asoc->strreset_inseq - request_seq - 1;
  375. result = asoc->strreset_result[i];
  376. if (result == SCTP_STRRESET_PERFORMED)
  377. return NULL;
  378. goto err;
  379. }
  380. asoc->strreset_inseq++;
  381. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  382. goto out;
  383. if (asoc->strreset_outstanding) {
  384. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  385. goto out;
  386. }
  387. nums = (ntohs(param.p->length) - sizeof(*inreq)) / 2;
  388. str_p = inreq->list_of_streams;
  389. for (i = 0; i < nums; i++) {
  390. if (ntohs(str_p[i]) >= stream->outcnt) {
  391. result = SCTP_STRRESET_ERR_WRONG_SSN;
  392. goto out;
  393. }
  394. }
  395. chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
  396. if (!chunk)
  397. goto out;
  398. if (nums)
  399. for (i = 0; i < nums; i++)
  400. stream->out[ntohs(str_p[i])].state =
  401. SCTP_STREAM_CLOSED;
  402. else
  403. for (i = 0; i < stream->outcnt; i++)
  404. stream->out[i].state = SCTP_STREAM_CLOSED;
  405. asoc->strreset_chunk = chunk;
  406. asoc->strreset_outstanding = 1;
  407. sctp_chunk_hold(asoc->strreset_chunk);
  408. result = SCTP_STRRESET_PERFORMED;
  409. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  410. SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
  411. out:
  412. sctp_update_strreset_result(asoc, result);
  413. err:
  414. if (!chunk)
  415. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  416. return chunk;
  417. }
  418. struct sctp_chunk *sctp_process_strreset_tsnreq(
  419. struct sctp_association *asoc,
  420. union sctp_params param,
  421. struct sctp_ulpevent **evp)
  422. {
  423. __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
  424. struct sctp_strreset_tsnreq *tsnreq = param.v;
  425. struct sctp_stream *stream = &asoc->stream;
  426. __u32 result = SCTP_STRRESET_DENIED;
  427. __u32 request_seq;
  428. __u16 i;
  429. request_seq = ntohl(tsnreq->request_seq);
  430. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  431. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  432. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  433. goto err;
  434. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  435. i = asoc->strreset_inseq - request_seq - 1;
  436. result = asoc->strreset_result[i];
  437. if (result == SCTP_STRRESET_PERFORMED) {
  438. next_tsn = asoc->next_tsn;
  439. init_tsn =
  440. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
  441. }
  442. goto err;
  443. }
  444. asoc->strreset_inseq++;
  445. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  446. goto out;
  447. if (asoc->strreset_outstanding) {
  448. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  449. goto out;
  450. }
  451. /* G3: The same processing as though a SACK chunk with no gap report
  452. * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
  453. * received MUST be performed.
  454. */
  455. max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
  456. sctp_ulpq_reasm_flushtsn(&asoc->ulpq, max_tsn_seen);
  457. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  458. /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
  459. * TSN that the peer should use to send the next DATA chunk. The
  460. * value SHOULD be the smallest TSN not acknowledged by the
  461. * receiver of the request plus 2^31.
  462. */
  463. init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
  464. sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
  465. init_tsn, GFP_ATOMIC);
  466. /* G4: The same processing as though a FWD-TSN chunk (as defined in
  467. * [RFC3758]) with all streams affected and a new cumulative TSN
  468. * ACK of the Receiver's Next TSN minus 1 were received MUST be
  469. * performed.
  470. */
  471. sctp_outq_free(&asoc->outqueue);
  472. /* G2: Compute an appropriate value for the local endpoint's next TSN,
  473. * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
  474. * chunk. The value SHOULD be the highest TSN sent by the receiver
  475. * of the request plus 1.
  476. */
  477. next_tsn = asoc->next_tsn;
  478. asoc->ctsn_ack_point = next_tsn - 1;
  479. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  480. /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
  481. * incoming and outgoing streams.
  482. */
  483. for (i = 0; i < stream->outcnt; i++)
  484. stream->out[i].ssn = 0;
  485. for (i = 0; i < stream->incnt; i++)
  486. stream->in[i].ssn = 0;
  487. result = SCTP_STRRESET_PERFORMED;
  488. *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
  489. next_tsn, GFP_ATOMIC);
  490. out:
  491. sctp_update_strreset_result(asoc, result);
  492. err:
  493. return sctp_make_strreset_tsnresp(asoc, result, request_seq,
  494. next_tsn, init_tsn);
  495. }
  496. struct sctp_chunk *sctp_process_strreset_addstrm_out(
  497. struct sctp_association *asoc,
  498. union sctp_params param,
  499. struct sctp_ulpevent **evp)
  500. {
  501. struct sctp_strreset_addstrm *addstrm = param.v;
  502. struct sctp_stream *stream = &asoc->stream;
  503. __u32 result = SCTP_STRRESET_DENIED;
  504. struct sctp_stream_in *streamin;
  505. __u32 request_seq, incnt;
  506. __u16 in, i;
  507. request_seq = ntohl(addstrm->request_seq);
  508. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  509. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  510. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  511. goto err;
  512. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  513. i = asoc->strreset_inseq - request_seq - 1;
  514. result = asoc->strreset_result[i];
  515. goto err;
  516. }
  517. asoc->strreset_inseq++;
  518. if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
  519. goto out;
  520. if (asoc->strreset_chunk) {
  521. if (!sctp_chunk_lookup_strreset_param(
  522. asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
  523. /* same process with outstanding isn't 0 */
  524. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  525. goto out;
  526. }
  527. asoc->strreset_outstanding--;
  528. asoc->strreset_outseq++;
  529. if (!asoc->strreset_outstanding) {
  530. struct sctp_transport *t;
  531. t = asoc->strreset_chunk->transport;
  532. if (del_timer(&t->reconf_timer))
  533. sctp_transport_put(t);
  534. sctp_chunk_put(asoc->strreset_chunk);
  535. asoc->strreset_chunk = NULL;
  536. }
  537. }
  538. in = ntohs(addstrm->number_of_streams);
  539. incnt = stream->incnt + in;
  540. if (!in || incnt > SCTP_MAX_STREAM)
  541. goto out;
  542. streamin = krealloc(stream->in, incnt * sizeof(*streamin),
  543. GFP_ATOMIC);
  544. if (!streamin)
  545. goto out;
  546. memset(streamin + stream->incnt, 0, in * sizeof(*streamin));
  547. stream->in = streamin;
  548. stream->incnt = incnt;
  549. result = SCTP_STRRESET_PERFORMED;
  550. *evp = sctp_ulpevent_make_stream_change_event(asoc,
  551. 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
  552. out:
  553. sctp_update_strreset_result(asoc, result);
  554. err:
  555. return sctp_make_strreset_resp(asoc, result, request_seq);
  556. }
  557. struct sctp_chunk *sctp_process_strreset_addstrm_in(
  558. struct sctp_association *asoc,
  559. union sctp_params param,
  560. struct sctp_ulpevent **evp)
  561. {
  562. struct sctp_strreset_addstrm *addstrm = param.v;
  563. struct sctp_stream *stream = &asoc->stream;
  564. __u32 result = SCTP_STRRESET_DENIED;
  565. struct sctp_stream_out *streamout;
  566. struct sctp_chunk *chunk = NULL;
  567. __u32 request_seq, outcnt;
  568. __u16 out, i;
  569. request_seq = ntohl(addstrm->request_seq);
  570. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  571. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  572. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  573. goto err;
  574. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  575. i = asoc->strreset_inseq - request_seq - 1;
  576. result = asoc->strreset_result[i];
  577. if (result == SCTP_STRRESET_PERFORMED)
  578. return NULL;
  579. goto err;
  580. }
  581. asoc->strreset_inseq++;
  582. if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
  583. goto out;
  584. if (asoc->strreset_outstanding) {
  585. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  586. goto out;
  587. }
  588. out = ntohs(addstrm->number_of_streams);
  589. outcnt = stream->outcnt + out;
  590. if (!out || outcnt > SCTP_MAX_STREAM)
  591. goto out;
  592. streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
  593. GFP_ATOMIC);
  594. if (!streamout)
  595. goto out;
  596. memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
  597. stream->out = streamout;
  598. chunk = sctp_make_strreset_addstrm(asoc, out, 0);
  599. if (!chunk)
  600. goto out;
  601. asoc->strreset_chunk = chunk;
  602. asoc->strreset_outstanding = 1;
  603. sctp_chunk_hold(asoc->strreset_chunk);
  604. stream->outcnt = outcnt;
  605. result = SCTP_STRRESET_PERFORMED;
  606. *evp = sctp_ulpevent_make_stream_change_event(asoc,
  607. 0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);
  608. out:
  609. sctp_update_strreset_result(asoc, result);
  610. err:
  611. if (!chunk)
  612. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  613. return chunk;
  614. }
  615. struct sctp_chunk *sctp_process_strreset_resp(
  616. struct sctp_association *asoc,
  617. union sctp_params param,
  618. struct sctp_ulpevent **evp)
  619. {
  620. struct sctp_stream *stream = &asoc->stream;
  621. struct sctp_strreset_resp *resp = param.v;
  622. struct sctp_transport *t;
  623. __u16 i, nums, flags = 0;
  624. struct sctp_paramhdr *req;
  625. __u32 result;
  626. req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
  627. if (!req)
  628. return NULL;
  629. result = ntohl(resp->result);
  630. if (result != SCTP_STRRESET_PERFORMED) {
  631. /* if in progress, do nothing but retransmit */
  632. if (result == SCTP_STRRESET_IN_PROGRESS)
  633. return NULL;
  634. else if (result == SCTP_STRRESET_DENIED)
  635. flags = SCTP_STREAM_RESET_DENIED;
  636. else
  637. flags = SCTP_STREAM_RESET_FAILED;
  638. }
  639. if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
  640. struct sctp_strreset_outreq *outreq;
  641. __be16 *str_p;
  642. outreq = (struct sctp_strreset_outreq *)req;
  643. str_p = outreq->list_of_streams;
  644. nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) / 2;
  645. if (result == SCTP_STRRESET_PERFORMED) {
  646. if (nums) {
  647. for (i = 0; i < nums; i++)
  648. stream->out[ntohs(str_p[i])].ssn = 0;
  649. } else {
  650. for (i = 0; i < stream->outcnt; i++)
  651. stream->out[i].ssn = 0;
  652. }
  653. flags = SCTP_STREAM_RESET_OUTGOING_SSN;
  654. }
  655. for (i = 0; i < stream->outcnt; i++)
  656. stream->out[i].state = SCTP_STREAM_OPEN;
  657. *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
  658. nums, str_p, GFP_ATOMIC);
  659. } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
  660. struct sctp_strreset_inreq *inreq;
  661. __be16 *str_p;
  662. /* if the result is performed, it's impossible for inreq */
  663. if (result == SCTP_STRRESET_PERFORMED)
  664. return NULL;
  665. inreq = (struct sctp_strreset_inreq *)req;
  666. str_p = inreq->list_of_streams;
  667. nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) / 2;
  668. *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
  669. nums, str_p, GFP_ATOMIC);
  670. } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
  671. struct sctp_strreset_resptsn *resptsn;
  672. __u32 stsn, rtsn;
  673. /* check for resptsn, as sctp_verify_reconf didn't do it*/
  674. if (ntohs(param.p->length) != sizeof(*resptsn))
  675. return NULL;
  676. resptsn = (struct sctp_strreset_resptsn *)resp;
  677. stsn = ntohl(resptsn->senders_next_tsn);
  678. rtsn = ntohl(resptsn->receivers_next_tsn);
  679. if (result == SCTP_STRRESET_PERFORMED) {
  680. __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
  681. &asoc->peer.tsn_map);
  682. sctp_ulpq_reasm_flushtsn(&asoc->ulpq, mtsn);
  683. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  684. sctp_tsnmap_init(&asoc->peer.tsn_map,
  685. SCTP_TSN_MAP_INITIAL,
  686. stsn, GFP_ATOMIC);
  687. sctp_outq_free(&asoc->outqueue);
  688. asoc->next_tsn = rtsn;
  689. asoc->ctsn_ack_point = asoc->next_tsn - 1;
  690. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  691. for (i = 0; i < stream->outcnt; i++)
  692. stream->out[i].ssn = 0;
  693. for (i = 0; i < stream->incnt; i++)
  694. stream->in[i].ssn = 0;
  695. }
  696. for (i = 0; i < stream->outcnt; i++)
  697. stream->out[i].state = SCTP_STREAM_OPEN;
  698. *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
  699. stsn, rtsn, GFP_ATOMIC);
  700. } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
  701. struct sctp_strreset_addstrm *addstrm;
  702. __u16 number;
  703. addstrm = (struct sctp_strreset_addstrm *)req;
  704. nums = ntohs(addstrm->number_of_streams);
  705. number = stream->outcnt - nums;
  706. if (result == SCTP_STRRESET_PERFORMED)
  707. for (i = number; i < stream->outcnt; i++)
  708. stream->out[i].state = SCTP_STREAM_OPEN;
  709. else
  710. stream->outcnt = number;
  711. *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
  712. 0, nums, GFP_ATOMIC);
  713. } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
  714. struct sctp_strreset_addstrm *addstrm;
  715. /* if the result is performed, it's impossible for addstrm in
  716. * request.
  717. */
  718. if (result == SCTP_STRRESET_PERFORMED)
  719. return NULL;
  720. addstrm = (struct sctp_strreset_addstrm *)req;
  721. nums = ntohs(addstrm->number_of_streams);
  722. *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
  723. nums, 0, GFP_ATOMIC);
  724. }
  725. asoc->strreset_outstanding--;
  726. asoc->strreset_outseq++;
  727. /* remove everything for this reconf request */
  728. if (!asoc->strreset_outstanding) {
  729. t = asoc->strreset_chunk->transport;
  730. if (del_timer(&t->reconf_timer))
  731. sctp_transport_put(t);
  732. sctp_chunk_put(asoc->strreset_chunk);
  733. asoc->strreset_chunk = NULL;
  734. }
  735. return NULL;
  736. }