stream.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. bool out, in;
  102. if (!asoc->peer.reconf_capable ||
  103. !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
  104. retval = -ENOPROTOOPT;
  105. goto out;
  106. }
  107. if (asoc->strreset_outstanding) {
  108. retval = -EINPROGRESS;
  109. goto out;
  110. }
  111. out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
  112. in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
  113. if (!out && !in)
  114. goto out;
  115. str_nums = params->srs_number_streams;
  116. str_list = params->srs_stream_list;
  117. if (out && str_nums)
  118. for (i = 0; i < str_nums; i++)
  119. if (str_list[i] >= stream->outcnt)
  120. goto out;
  121. if (in && str_nums)
  122. for (i = 0; i < str_nums; i++)
  123. if (str_list[i] >= stream->incnt)
  124. goto out;
  125. for (i = 0; i < str_nums; i++)
  126. str_list[i] = htons(str_list[i]);
  127. chunk = sctp_make_strreset_req(asoc, str_nums, str_list, out, in);
  128. for (i = 0; i < str_nums; i++)
  129. str_list[i] = ntohs(str_list[i]);
  130. if (!chunk) {
  131. retval = -ENOMEM;
  132. goto out;
  133. }
  134. if (out) {
  135. if (str_nums)
  136. for (i = 0; i < str_nums; i++)
  137. stream->out[str_list[i]].state =
  138. SCTP_STREAM_CLOSED;
  139. else
  140. for (i = 0; i < stream->outcnt; i++)
  141. stream->out[i].state = SCTP_STREAM_CLOSED;
  142. }
  143. asoc->strreset_chunk = chunk;
  144. sctp_chunk_hold(asoc->strreset_chunk);
  145. retval = sctp_send_reconf(asoc, chunk);
  146. if (retval) {
  147. sctp_chunk_put(asoc->strreset_chunk);
  148. asoc->strreset_chunk = NULL;
  149. if (!out)
  150. goto out;
  151. if (str_nums)
  152. for (i = 0; i < str_nums; i++)
  153. stream->out[str_list[i]].state =
  154. SCTP_STREAM_OPEN;
  155. else
  156. for (i = 0; i < stream->outcnt; i++)
  157. stream->out[i].state = SCTP_STREAM_OPEN;
  158. goto out;
  159. }
  160. asoc->strreset_outstanding = out + in;
  161. out:
  162. return retval;
  163. }
  164. int sctp_send_reset_assoc(struct sctp_association *asoc)
  165. {
  166. struct sctp_stream *stream = &asoc->stream;
  167. struct sctp_chunk *chunk = NULL;
  168. int retval;
  169. __u16 i;
  170. if (!asoc->peer.reconf_capable ||
  171. !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  172. return -ENOPROTOOPT;
  173. if (asoc->strreset_outstanding)
  174. return -EINPROGRESS;
  175. chunk = sctp_make_strreset_tsnreq(asoc);
  176. if (!chunk)
  177. return -ENOMEM;
  178. /* Block further xmit of data until this request is completed */
  179. for (i = 0; i < stream->outcnt; i++)
  180. stream->out[i].state = SCTP_STREAM_CLOSED;
  181. asoc->strreset_chunk = chunk;
  182. sctp_chunk_hold(asoc->strreset_chunk);
  183. retval = sctp_send_reconf(asoc, chunk);
  184. if (retval) {
  185. sctp_chunk_put(asoc->strreset_chunk);
  186. asoc->strreset_chunk = NULL;
  187. for (i = 0; i < stream->outcnt; i++)
  188. stream->out[i].state = SCTP_STREAM_OPEN;
  189. return retval;
  190. }
  191. asoc->strreset_outstanding = 1;
  192. return 0;
  193. }
  194. int sctp_send_add_streams(struct sctp_association *asoc,
  195. struct sctp_add_streams *params)
  196. {
  197. struct sctp_stream *stream = &asoc->stream;
  198. struct sctp_chunk *chunk = NULL;
  199. int retval = -ENOMEM;
  200. __u32 outcnt, incnt;
  201. __u16 out, in;
  202. if (!asoc->peer.reconf_capable ||
  203. !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
  204. retval = -ENOPROTOOPT;
  205. goto out;
  206. }
  207. if (asoc->strreset_outstanding) {
  208. retval = -EINPROGRESS;
  209. goto out;
  210. }
  211. out = params->sas_outstrms;
  212. in = params->sas_instrms;
  213. outcnt = stream->outcnt + out;
  214. incnt = stream->incnt + in;
  215. if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
  216. (!out && !in)) {
  217. retval = -EINVAL;
  218. goto out;
  219. }
  220. if (out) {
  221. struct sctp_stream_out *streamout;
  222. streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
  223. GFP_KERNEL);
  224. if (!streamout)
  225. goto out;
  226. memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
  227. stream->out = streamout;
  228. }
  229. chunk = sctp_make_strreset_addstrm(asoc, out, in);
  230. if (!chunk)
  231. goto out;
  232. asoc->strreset_chunk = chunk;
  233. sctp_chunk_hold(asoc->strreset_chunk);
  234. retval = sctp_send_reconf(asoc, chunk);
  235. if (retval) {
  236. sctp_chunk_put(asoc->strreset_chunk);
  237. asoc->strreset_chunk = NULL;
  238. goto out;
  239. }
  240. stream->incnt = incnt;
  241. stream->outcnt = outcnt;
  242. asoc->strreset_outstanding = !!out + !!in;
  243. out:
  244. return retval;
  245. }
  246. static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
  247. struct sctp_association *asoc, __u32 resp_seq,
  248. __be16 type)
  249. {
  250. struct sctp_chunk *chunk = asoc->strreset_chunk;
  251. struct sctp_reconf_chunk *hdr;
  252. union sctp_params param;
  253. if (!chunk)
  254. return NULL;
  255. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  256. sctp_walk_params(param, hdr, params) {
  257. /* sctp_strreset_tsnreq is actually the basic structure
  258. * of all stream reconf params, so it's safe to use it
  259. * to access request_seq.
  260. */
  261. struct sctp_strreset_tsnreq *req = param.v;
  262. if ((!resp_seq || req->request_seq == resp_seq) &&
  263. (!type || type == req->param_hdr.type))
  264. return param.v;
  265. }
  266. return NULL;
  267. }
  268. static void sctp_update_strreset_result(struct sctp_association *asoc,
  269. __u32 result)
  270. {
  271. asoc->strreset_result[1] = asoc->strreset_result[0];
  272. asoc->strreset_result[0] = result;
  273. }
  274. struct sctp_chunk *sctp_process_strreset_outreq(
  275. struct sctp_association *asoc,
  276. union sctp_params param,
  277. struct sctp_ulpevent **evp)
  278. {
  279. struct sctp_strreset_outreq *outreq = param.v;
  280. struct sctp_stream *stream = &asoc->stream;
  281. __u16 i, nums, flags = 0, *str_p = NULL;
  282. __u32 result = SCTP_STRRESET_DENIED;
  283. __u32 request_seq;
  284. request_seq = ntohl(outreq->request_seq);
  285. if (ntohl(outreq->send_reset_at_tsn) >
  286. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
  287. result = SCTP_STRRESET_IN_PROGRESS;
  288. goto err;
  289. }
  290. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  291. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  292. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  293. goto err;
  294. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  295. i = asoc->strreset_inseq - request_seq - 1;
  296. result = asoc->strreset_result[i];
  297. goto err;
  298. }
  299. asoc->strreset_inseq++;
  300. /* Check strreset_enable after inseq inc, as sender cannot tell
  301. * the peer doesn't enable strreset after receiving response with
  302. * result denied, as well as to keep consistent with bsd.
  303. */
  304. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  305. goto out;
  306. if (asoc->strreset_chunk) {
  307. if (!sctp_chunk_lookup_strreset_param(
  308. asoc, outreq->response_seq,
  309. SCTP_PARAM_RESET_IN_REQUEST)) {
  310. /* same process with outstanding isn't 0 */
  311. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  312. goto out;
  313. }
  314. asoc->strreset_outstanding--;
  315. asoc->strreset_outseq++;
  316. if (!asoc->strreset_outstanding) {
  317. struct sctp_transport *t;
  318. t = asoc->strreset_chunk->transport;
  319. if (del_timer(&t->reconf_timer))
  320. sctp_transport_put(t);
  321. sctp_chunk_put(asoc->strreset_chunk);
  322. asoc->strreset_chunk = NULL;
  323. }
  324. flags = SCTP_STREAM_RESET_INCOMING_SSN;
  325. }
  326. nums = (ntohs(param.p->length) - sizeof(*outreq)) / 2;
  327. if (nums) {
  328. str_p = outreq->list_of_streams;
  329. for (i = 0; i < nums; i++) {
  330. if (ntohs(str_p[i]) >= stream->incnt) {
  331. result = SCTP_STRRESET_ERR_WRONG_SSN;
  332. goto out;
  333. }
  334. }
  335. for (i = 0; i < nums; i++)
  336. stream->in[ntohs(str_p[i])].ssn = 0;
  337. } else {
  338. for (i = 0; i < stream->incnt; i++)
  339. stream->in[i].ssn = 0;
  340. }
  341. result = SCTP_STRRESET_PERFORMED;
  342. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  343. flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
  344. GFP_ATOMIC);
  345. out:
  346. sctp_update_strreset_result(asoc, result);
  347. err:
  348. return sctp_make_strreset_resp(asoc, result, request_seq);
  349. }
  350. struct sctp_chunk *sctp_process_strreset_inreq(
  351. struct sctp_association *asoc,
  352. union sctp_params param,
  353. struct sctp_ulpevent **evp)
  354. {
  355. struct sctp_strreset_inreq *inreq = param.v;
  356. struct sctp_stream *stream = &asoc->stream;
  357. __u32 result = SCTP_STRRESET_DENIED;
  358. struct sctp_chunk *chunk = NULL;
  359. __u16 i, nums, *str_p;
  360. __u32 request_seq;
  361. request_seq = ntohl(inreq->request_seq);
  362. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  363. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  364. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  365. goto err;
  366. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  367. i = asoc->strreset_inseq - request_seq - 1;
  368. result = asoc->strreset_result[i];
  369. if (result == SCTP_STRRESET_PERFORMED)
  370. return NULL;
  371. goto err;
  372. }
  373. asoc->strreset_inseq++;
  374. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  375. goto out;
  376. if (asoc->strreset_outstanding) {
  377. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  378. goto out;
  379. }
  380. nums = (ntohs(param.p->length) - sizeof(*inreq)) / 2;
  381. str_p = inreq->list_of_streams;
  382. for (i = 0; i < nums; i++) {
  383. if (ntohs(str_p[i]) >= stream->outcnt) {
  384. result = SCTP_STRRESET_ERR_WRONG_SSN;
  385. goto out;
  386. }
  387. }
  388. chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
  389. if (!chunk)
  390. goto out;
  391. if (nums)
  392. for (i = 0; i < nums; i++)
  393. stream->out[ntohs(str_p[i])].state =
  394. SCTP_STREAM_CLOSED;
  395. else
  396. for (i = 0; i < stream->outcnt; i++)
  397. stream->out[i].state = SCTP_STREAM_CLOSED;
  398. asoc->strreset_chunk = chunk;
  399. asoc->strreset_outstanding = 1;
  400. sctp_chunk_hold(asoc->strreset_chunk);
  401. result = SCTP_STRRESET_PERFORMED;
  402. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  403. SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
  404. out:
  405. sctp_update_strreset_result(asoc, result);
  406. err:
  407. if (!chunk)
  408. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  409. return chunk;
  410. }
  411. struct sctp_chunk *sctp_process_strreset_tsnreq(
  412. struct sctp_association *asoc,
  413. union sctp_params param,
  414. struct sctp_ulpevent **evp)
  415. {
  416. __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
  417. struct sctp_strreset_tsnreq *tsnreq = param.v;
  418. struct sctp_stream *stream = &asoc->stream;
  419. __u32 result = SCTP_STRRESET_DENIED;
  420. __u32 request_seq;
  421. __u16 i;
  422. request_seq = ntohl(tsnreq->request_seq);
  423. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  424. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  425. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  426. goto err;
  427. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  428. i = asoc->strreset_inseq - request_seq - 1;
  429. result = asoc->strreset_result[i];
  430. if (result == SCTP_STRRESET_PERFORMED) {
  431. next_tsn = asoc->next_tsn;
  432. init_tsn =
  433. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
  434. }
  435. goto err;
  436. }
  437. asoc->strreset_inseq++;
  438. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  439. goto out;
  440. if (asoc->strreset_outstanding) {
  441. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  442. goto out;
  443. }
  444. /* G3: The same processing as though a SACK chunk with no gap report
  445. * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
  446. * received MUST be performed.
  447. */
  448. max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
  449. sctp_ulpq_reasm_flushtsn(&asoc->ulpq, max_tsn_seen);
  450. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  451. /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
  452. * TSN that the peer should use to send the next DATA chunk. The
  453. * value SHOULD be the smallest TSN not acknowledged by the
  454. * receiver of the request plus 2^31.
  455. */
  456. init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
  457. sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
  458. init_tsn, GFP_ATOMIC);
  459. /* G4: The same processing as though a FWD-TSN chunk (as defined in
  460. * [RFC3758]) with all streams affected and a new cumulative TSN
  461. * ACK of the Receiver's Next TSN minus 1 were received MUST be
  462. * performed.
  463. */
  464. sctp_outq_free(&asoc->outqueue);
  465. /* G2: Compute an appropriate value for the local endpoint's next TSN,
  466. * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
  467. * chunk. The value SHOULD be the highest TSN sent by the receiver
  468. * of the request plus 1.
  469. */
  470. next_tsn = asoc->next_tsn;
  471. asoc->ctsn_ack_point = next_tsn - 1;
  472. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  473. /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
  474. * incoming and outgoing streams.
  475. */
  476. for (i = 0; i < stream->outcnt; i++)
  477. stream->out[i].ssn = 0;
  478. for (i = 0; i < stream->incnt; i++)
  479. stream->in[i].ssn = 0;
  480. result = SCTP_STRRESET_PERFORMED;
  481. *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
  482. next_tsn, GFP_ATOMIC);
  483. out:
  484. sctp_update_strreset_result(asoc, result);
  485. err:
  486. return sctp_make_strreset_tsnresp(asoc, result, request_seq,
  487. next_tsn, init_tsn);
  488. }
  489. struct sctp_chunk *sctp_process_strreset_addstrm_out(
  490. struct sctp_association *asoc,
  491. union sctp_params param,
  492. struct sctp_ulpevent **evp)
  493. {
  494. struct sctp_strreset_addstrm *addstrm = param.v;
  495. struct sctp_stream *stream = &asoc->stream;
  496. __u32 result = SCTP_STRRESET_DENIED;
  497. struct sctp_stream_in *streamin;
  498. __u32 request_seq, incnt;
  499. __u16 in, i;
  500. request_seq = ntohl(addstrm->request_seq);
  501. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  502. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  503. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  504. goto err;
  505. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  506. i = asoc->strreset_inseq - request_seq - 1;
  507. result = asoc->strreset_result[i];
  508. goto err;
  509. }
  510. asoc->strreset_inseq++;
  511. if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
  512. goto out;
  513. if (asoc->strreset_chunk) {
  514. if (!sctp_chunk_lookup_strreset_param(
  515. asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
  516. /* same process with outstanding isn't 0 */
  517. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  518. goto out;
  519. }
  520. asoc->strreset_outstanding--;
  521. asoc->strreset_outseq++;
  522. if (!asoc->strreset_outstanding) {
  523. struct sctp_transport *t;
  524. t = asoc->strreset_chunk->transport;
  525. if (del_timer(&t->reconf_timer))
  526. sctp_transport_put(t);
  527. sctp_chunk_put(asoc->strreset_chunk);
  528. asoc->strreset_chunk = NULL;
  529. }
  530. }
  531. in = ntohs(addstrm->number_of_streams);
  532. incnt = stream->incnt + in;
  533. if (!in || incnt > SCTP_MAX_STREAM)
  534. goto out;
  535. streamin = krealloc(stream->in, incnt * sizeof(*streamin),
  536. GFP_ATOMIC);
  537. if (!streamin)
  538. goto out;
  539. memset(streamin + stream->incnt, 0, in * sizeof(*streamin));
  540. stream->in = streamin;
  541. stream->incnt = incnt;
  542. result = SCTP_STRRESET_PERFORMED;
  543. *evp = sctp_ulpevent_make_stream_change_event(asoc,
  544. 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
  545. out:
  546. sctp_update_strreset_result(asoc, result);
  547. err:
  548. return sctp_make_strreset_resp(asoc, result, request_seq);
  549. }
  550. struct sctp_chunk *sctp_process_strreset_addstrm_in(
  551. struct sctp_association *asoc,
  552. union sctp_params param,
  553. struct sctp_ulpevent **evp)
  554. {
  555. struct sctp_strreset_addstrm *addstrm = param.v;
  556. struct sctp_stream *stream = &asoc->stream;
  557. __u32 result = SCTP_STRRESET_DENIED;
  558. struct sctp_stream_out *streamout;
  559. struct sctp_chunk *chunk = NULL;
  560. __u32 request_seq, outcnt;
  561. __u16 out, i;
  562. request_seq = ntohl(addstrm->request_seq);
  563. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  564. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  565. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  566. goto err;
  567. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  568. i = asoc->strreset_inseq - request_seq - 1;
  569. result = asoc->strreset_result[i];
  570. if (result == SCTP_STRRESET_PERFORMED)
  571. return NULL;
  572. goto err;
  573. }
  574. asoc->strreset_inseq++;
  575. if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
  576. goto out;
  577. if (asoc->strreset_outstanding) {
  578. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  579. goto out;
  580. }
  581. out = ntohs(addstrm->number_of_streams);
  582. outcnt = stream->outcnt + out;
  583. if (!out || outcnt > SCTP_MAX_STREAM)
  584. goto out;
  585. streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
  586. GFP_ATOMIC);
  587. if (!streamout)
  588. goto out;
  589. memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
  590. stream->out = streamout;
  591. chunk = sctp_make_strreset_addstrm(asoc, out, 0);
  592. if (!chunk)
  593. goto out;
  594. asoc->strreset_chunk = chunk;
  595. asoc->strreset_outstanding = 1;
  596. sctp_chunk_hold(asoc->strreset_chunk);
  597. stream->outcnt = outcnt;
  598. result = SCTP_STRRESET_PERFORMED;
  599. *evp = sctp_ulpevent_make_stream_change_event(asoc,
  600. 0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);
  601. out:
  602. sctp_update_strreset_result(asoc, result);
  603. err:
  604. if (!chunk)
  605. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  606. return chunk;
  607. }
  608. struct sctp_chunk *sctp_process_strreset_resp(
  609. struct sctp_association *asoc,
  610. union sctp_params param,
  611. struct sctp_ulpevent **evp)
  612. {
  613. struct sctp_stream *stream = &asoc->stream;
  614. struct sctp_strreset_resp *resp = param.v;
  615. struct sctp_transport *t;
  616. __u16 i, nums, flags = 0;
  617. struct sctp_paramhdr *req;
  618. __u32 result;
  619. req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
  620. if (!req)
  621. return NULL;
  622. result = ntohl(resp->result);
  623. if (result != SCTP_STRRESET_PERFORMED) {
  624. /* if in progress, do nothing but retransmit */
  625. if (result == SCTP_STRRESET_IN_PROGRESS)
  626. return NULL;
  627. else if (result == SCTP_STRRESET_DENIED)
  628. flags = SCTP_STREAM_RESET_DENIED;
  629. else
  630. flags = SCTP_STREAM_RESET_FAILED;
  631. }
  632. if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
  633. struct sctp_strreset_outreq *outreq;
  634. __u16 *str_p;
  635. outreq = (struct sctp_strreset_outreq *)req;
  636. str_p = outreq->list_of_streams;
  637. nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) / 2;
  638. if (result == SCTP_STRRESET_PERFORMED) {
  639. if (nums) {
  640. for (i = 0; i < nums; i++)
  641. stream->out[ntohs(str_p[i])].ssn = 0;
  642. } else {
  643. for (i = 0; i < stream->outcnt; i++)
  644. stream->out[i].ssn = 0;
  645. }
  646. flags = SCTP_STREAM_RESET_OUTGOING_SSN;
  647. }
  648. for (i = 0; i < stream->outcnt; i++)
  649. stream->out[i].state = SCTP_STREAM_OPEN;
  650. *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
  651. nums, str_p, GFP_ATOMIC);
  652. } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
  653. struct sctp_strreset_inreq *inreq;
  654. __u16 *str_p;
  655. /* if the result is performed, it's impossible for inreq */
  656. if (result == SCTP_STRRESET_PERFORMED)
  657. return NULL;
  658. inreq = (struct sctp_strreset_inreq *)req;
  659. str_p = inreq->list_of_streams;
  660. nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) / 2;
  661. *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
  662. nums, str_p, GFP_ATOMIC);
  663. } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
  664. struct sctp_strreset_resptsn *resptsn;
  665. __u32 stsn, rtsn;
  666. /* check for resptsn, as sctp_verify_reconf didn't do it*/
  667. if (ntohs(param.p->length) != sizeof(*resptsn))
  668. return NULL;
  669. resptsn = (struct sctp_strreset_resptsn *)resp;
  670. stsn = ntohl(resptsn->senders_next_tsn);
  671. rtsn = ntohl(resptsn->receivers_next_tsn);
  672. if (result == SCTP_STRRESET_PERFORMED) {
  673. __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
  674. &asoc->peer.tsn_map);
  675. sctp_ulpq_reasm_flushtsn(&asoc->ulpq, mtsn);
  676. sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
  677. sctp_tsnmap_init(&asoc->peer.tsn_map,
  678. SCTP_TSN_MAP_INITIAL,
  679. stsn, GFP_ATOMIC);
  680. sctp_outq_free(&asoc->outqueue);
  681. asoc->next_tsn = rtsn;
  682. asoc->ctsn_ack_point = asoc->next_tsn - 1;
  683. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  684. for (i = 0; i < stream->outcnt; i++)
  685. stream->out[i].ssn = 0;
  686. for (i = 0; i < stream->incnt; i++)
  687. stream->in[i].ssn = 0;
  688. }
  689. for (i = 0; i < stream->outcnt; i++)
  690. stream->out[i].state = SCTP_STREAM_OPEN;
  691. *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
  692. stsn, rtsn, GFP_ATOMIC);
  693. } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
  694. struct sctp_strreset_addstrm *addstrm;
  695. __u16 number;
  696. addstrm = (struct sctp_strreset_addstrm *)req;
  697. nums = ntohs(addstrm->number_of_streams);
  698. number = stream->outcnt - nums;
  699. if (result == SCTP_STRRESET_PERFORMED)
  700. for (i = number; i < stream->outcnt; i++)
  701. stream->out[i].state = SCTP_STREAM_OPEN;
  702. else
  703. stream->outcnt = number;
  704. *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
  705. 0, nums, GFP_ATOMIC);
  706. } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
  707. struct sctp_strreset_addstrm *addstrm;
  708. /* if the result is performed, it's impossible for addstrm in
  709. * request.
  710. */
  711. if (result == SCTP_STRRESET_PERFORMED)
  712. return NULL;
  713. addstrm = (struct sctp_strreset_addstrm *)req;
  714. nums = ntohs(addstrm->number_of_streams);
  715. *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
  716. nums, 0, GFP_ATOMIC);
  717. }
  718. asoc->strreset_outstanding--;
  719. asoc->strreset_outseq++;
  720. /* remove everything for this reconf request */
  721. if (!asoc->strreset_outstanding) {
  722. t = asoc->strreset_chunk->transport;
  723. if (del_timer(&t->reconf_timer))
  724. sctp_transport_put(t);
  725. sctp_chunk_put(asoc->strreset_chunk);
  726. asoc->strreset_chunk = NULL;
  727. }
  728. return NULL;
  729. }