stream.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001 Intel Corp.
  7. *
  8. * This file is part of the SCTP kernel implementation
  9. *
  10. * This file contains sctp stream maniuplation primitives and helpers.
  11. *
  12. * Please send any bug reports or fixes you make to the
  13. * email address(es):
  14. * lksctp developers <linux-sctp@vger.kernel.org>
  15. *
  16. * Written or modified by:
  17. * Xin Long <lucien.xin@gmail.com>
  18. */
  19. #include <linux/list.h>
  20. #include <net/sctp/sctp.h>
  21. #include <net/sctp/sm.h>
  22. #include <net/sctp/stream_sched.h>
  23. static void sctp_stream_shrink_out(struct sctp_stream *stream, __u16 outcnt)
  24. {
  25. struct sctp_association *asoc;
  26. struct sctp_chunk *ch, *temp;
  27. struct sctp_outq *outq;
  28. asoc = container_of(stream, struct sctp_association, stream);
  29. outq = &asoc->outqueue;
  30. list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
  31. __u16 sid = sctp_chunk_stream_no(ch);
  32. if (sid < outcnt)
  33. continue;
  34. sctp_sched_dequeue_common(outq, ch);
  35. /* No need to call dequeue_done here because
  36. * the chunks are not scheduled by now.
  37. */
  38. /* Mark as failed send. */
  39. sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
  40. if (asoc->peer.prsctp_capable &&
  41. SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
  42. asoc->sent_cnt_removable--;
  43. sctp_chunk_free(ch);
  44. }
  45. }
  46. /* Migrates chunks from stream queues to new stream queues if needed,
  47. * but not across associations. Also, removes those chunks to streams
  48. * higher than the new max.
  49. */
  50. static void sctp_stream_outq_migrate(struct sctp_stream *stream,
  51. struct sctp_stream *new, __u16 outcnt)
  52. {
  53. int i;
  54. if (stream->outcnt > outcnt)
  55. sctp_stream_shrink_out(stream, outcnt);
  56. if (new) {
  57. /* Here we actually move the old ext stuff into the new
  58. * buffer, because we want to keep it. Then
  59. * sctp_stream_update will swap ->out pointers.
  60. */
  61. for (i = 0; i < outcnt; i++) {
  62. kfree(SCTP_SO(new, i)->ext);
  63. SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
  64. SCTP_SO(stream, i)->ext = NULL;
  65. }
  66. }
  67. for (i = outcnt; i < stream->outcnt; i++) {
  68. kfree(SCTP_SO(stream, i)->ext);
  69. SCTP_SO(stream, i)->ext = NULL;
  70. }
  71. }
  72. static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
  73. gfp_t gfp)
  74. {
  75. int ret;
  76. if (outcnt <= stream->outcnt)
  77. goto out;
  78. ret = genradix_prealloc(&stream->out, outcnt, gfp);
  79. if (ret)
  80. return ret;
  81. out:
  82. stream->outcnt = outcnt;
  83. return 0;
  84. }
  85. static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
  86. gfp_t gfp)
  87. {
  88. int ret;
  89. if (incnt <= stream->incnt)
  90. goto out;
  91. ret = genradix_prealloc(&stream->in, incnt, gfp);
  92. if (ret)
  93. return ret;
  94. out:
  95. stream->incnt = incnt;
  96. return 0;
  97. }
  98. int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
  99. gfp_t gfp)
  100. {
  101. struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
  102. int i, ret = 0;
  103. gfp |= __GFP_NOWARN;
  104. /* Initial stream->out size may be very big, so free it and alloc
  105. * a new one with new outcnt to save memory if needed.
  106. */
  107. if (outcnt == stream->outcnt)
  108. goto handle_in;
  109. /* Filter out chunks queued on streams that won't exist anymore */
  110. sched->unsched_all(stream);
  111. sctp_stream_outq_migrate(stream, NULL, outcnt);
  112. sched->sched_all(stream);
  113. ret = sctp_stream_alloc_out(stream, outcnt, gfp);
  114. if (ret)
  115. goto out_err;
  116. for (i = 0; i < stream->outcnt; i++)
  117. SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
  118. handle_in:
  119. sctp_stream_interleave_init(stream);
  120. if (!incnt)
  121. goto out;
  122. ret = sctp_stream_alloc_in(stream, incnt, gfp);
  123. if (ret)
  124. goto in_err;
  125. goto out;
  126. in_err:
  127. sched->free(stream);
  128. genradix_free(&stream->in);
  129. out_err:
  130. genradix_free(&stream->out);
  131. stream->outcnt = 0;
  132. out:
  133. return ret;
  134. }
  135. int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
  136. {
  137. struct sctp_stream_out_ext *soute;
  138. int ret;
  139. soute = kzalloc(sizeof(*soute), GFP_KERNEL);
  140. if (!soute)
  141. return -ENOMEM;
  142. SCTP_SO(stream, sid)->ext = soute;
  143. ret = sctp_sched_init_sid(stream, sid, GFP_KERNEL);
  144. if (ret) {
  145. kfree(SCTP_SO(stream, sid)->ext);
  146. SCTP_SO(stream, sid)->ext = NULL;
  147. }
  148. return ret;
  149. }
  150. void sctp_stream_free(struct sctp_stream *stream)
  151. {
  152. struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
  153. int i;
  154. sched->free(stream);
  155. for (i = 0; i < stream->outcnt; i++)
  156. kfree(SCTP_SO(stream, i)->ext);
  157. genradix_free(&stream->out);
  158. genradix_free(&stream->in);
  159. }
  160. void sctp_stream_clear(struct sctp_stream *stream)
  161. {
  162. int i;
  163. for (i = 0; i < stream->outcnt; i++) {
  164. SCTP_SO(stream, i)->mid = 0;
  165. SCTP_SO(stream, i)->mid_uo = 0;
  166. }
  167. for (i = 0; i < stream->incnt; i++)
  168. SCTP_SI(stream, i)->mid = 0;
  169. }
  170. void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
  171. {
  172. struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
  173. sched->unsched_all(stream);
  174. sctp_stream_outq_migrate(stream, new, new->outcnt);
  175. sctp_stream_free(stream);
  176. stream->out = new->out;
  177. stream->in = new->in;
  178. stream->outcnt = new->outcnt;
  179. stream->incnt = new->incnt;
  180. sched->sched_all(stream);
  181. new->out.tree.root = NULL;
  182. new->in.tree.root = NULL;
  183. new->outcnt = 0;
  184. new->incnt = 0;
  185. }
  186. static int sctp_send_reconf(struct sctp_association *asoc,
  187. struct sctp_chunk *chunk)
  188. {
  189. int retval = 0;
  190. retval = sctp_primitive_RECONF(asoc->base.net, asoc, chunk);
  191. if (retval)
  192. sctp_chunk_free(chunk);
  193. return retval;
  194. }
  195. static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
  196. __u16 str_nums, __be16 *str_list)
  197. {
  198. struct sctp_association *asoc;
  199. __u16 i;
  200. asoc = container_of(stream, struct sctp_association, stream);
  201. if (!asoc->outqueue.out_qlen)
  202. return true;
  203. if (!str_nums)
  204. return false;
  205. for (i = 0; i < str_nums; i++) {
  206. __u16 sid = ntohs(str_list[i]);
  207. if (SCTP_SO(stream, sid)->ext &&
  208. !list_empty(&SCTP_SO(stream, sid)->ext->outq))
  209. return false;
  210. }
  211. return true;
  212. }
  213. int sctp_send_reset_streams(struct sctp_association *asoc,
  214. struct sctp_reset_streams *params)
  215. {
  216. struct sctp_stream *stream = &asoc->stream;
  217. __u16 i, str_nums, *str_list;
  218. struct sctp_chunk *chunk;
  219. int retval = -EINVAL;
  220. __be16 *nstr_list;
  221. bool out, in;
  222. if (!asoc->peer.reconf_capable ||
  223. !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
  224. retval = -ENOPROTOOPT;
  225. goto out;
  226. }
  227. if (asoc->strreset_outstanding) {
  228. retval = -EINPROGRESS;
  229. goto out;
  230. }
  231. out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
  232. in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
  233. if (!out && !in)
  234. goto out;
  235. str_nums = params->srs_number_streams;
  236. str_list = params->srs_stream_list;
  237. if (str_nums) {
  238. int param_len = 0;
  239. if (out) {
  240. for (i = 0; i < str_nums; i++)
  241. if (str_list[i] >= stream->outcnt)
  242. goto out;
  243. param_len = str_nums * sizeof(__u16) +
  244. sizeof(struct sctp_strreset_outreq);
  245. }
  246. if (in) {
  247. for (i = 0; i < str_nums; i++)
  248. if (str_list[i] >= stream->incnt)
  249. goto out;
  250. param_len += str_nums * sizeof(__u16) +
  251. sizeof(struct sctp_strreset_inreq);
  252. }
  253. if (param_len > SCTP_MAX_CHUNK_LEN -
  254. sizeof(struct sctp_reconf_chunk))
  255. goto out;
  256. }
  257. nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
  258. if (!nstr_list) {
  259. retval = -ENOMEM;
  260. goto out;
  261. }
  262. for (i = 0; i < str_nums; i++)
  263. nstr_list[i] = htons(str_list[i]);
  264. if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
  265. kfree(nstr_list);
  266. retval = -EAGAIN;
  267. goto out;
  268. }
  269. chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
  270. kfree(nstr_list);
  271. if (!chunk) {
  272. retval = -ENOMEM;
  273. goto out;
  274. }
  275. if (out) {
  276. if (str_nums)
  277. for (i = 0; i < str_nums; i++)
  278. SCTP_SO(stream, str_list[i])->state =
  279. SCTP_STREAM_CLOSED;
  280. else
  281. for (i = 0; i < stream->outcnt; i++)
  282. SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
  283. }
  284. asoc->strreset_chunk = chunk;
  285. sctp_chunk_hold(asoc->strreset_chunk);
  286. retval = sctp_send_reconf(asoc, chunk);
  287. if (retval) {
  288. sctp_chunk_put(asoc->strreset_chunk);
  289. asoc->strreset_chunk = NULL;
  290. if (!out)
  291. goto out;
  292. if (str_nums)
  293. for (i = 0; i < str_nums; i++)
  294. SCTP_SO(stream, str_list[i])->state =
  295. SCTP_STREAM_OPEN;
  296. else
  297. for (i = 0; i < stream->outcnt; i++)
  298. SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
  299. goto out;
  300. }
  301. asoc->strreset_outstanding = out + in;
  302. out:
  303. return retval;
  304. }
  305. int sctp_send_reset_assoc(struct sctp_association *asoc)
  306. {
  307. struct sctp_stream *stream = &asoc->stream;
  308. struct sctp_chunk *chunk = NULL;
  309. int retval;
  310. __u16 i;
  311. if (!asoc->peer.reconf_capable ||
  312. !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  313. return -ENOPROTOOPT;
  314. if (asoc->strreset_outstanding)
  315. return -EINPROGRESS;
  316. if (!sctp_outq_is_empty(&asoc->outqueue))
  317. return -EAGAIN;
  318. chunk = sctp_make_strreset_tsnreq(asoc);
  319. if (!chunk)
  320. return -ENOMEM;
  321. /* Block further xmit of data until this request is completed */
  322. for (i = 0; i < stream->outcnt; i++)
  323. SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
  324. asoc->strreset_chunk = chunk;
  325. sctp_chunk_hold(asoc->strreset_chunk);
  326. retval = sctp_send_reconf(asoc, chunk);
  327. if (retval) {
  328. sctp_chunk_put(asoc->strreset_chunk);
  329. asoc->strreset_chunk = NULL;
  330. for (i = 0; i < stream->outcnt; i++)
  331. SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
  332. return retval;
  333. }
  334. asoc->strreset_outstanding = 1;
  335. return 0;
  336. }
  337. int sctp_send_add_streams(struct sctp_association *asoc,
  338. struct sctp_add_streams *params)
  339. {
  340. struct sctp_stream *stream = &asoc->stream;
  341. struct sctp_chunk *chunk = NULL;
  342. int retval;
  343. __u32 outcnt, incnt;
  344. __u16 out, in;
  345. if (!asoc->peer.reconf_capable ||
  346. !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
  347. retval = -ENOPROTOOPT;
  348. goto out;
  349. }
  350. if (asoc->strreset_outstanding) {
  351. retval = -EINPROGRESS;
  352. goto out;
  353. }
  354. out = params->sas_outstrms;
  355. in = params->sas_instrms;
  356. outcnt = stream->outcnt + out;
  357. incnt = stream->incnt + in;
  358. if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
  359. (!out && !in)) {
  360. retval = -EINVAL;
  361. goto out;
  362. }
  363. if (out) {
  364. retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
  365. if (retval)
  366. goto out;
  367. }
  368. chunk = sctp_make_strreset_addstrm(asoc, out, in);
  369. if (!chunk) {
  370. retval = -ENOMEM;
  371. goto out;
  372. }
  373. asoc->strreset_chunk = chunk;
  374. sctp_chunk_hold(asoc->strreset_chunk);
  375. retval = sctp_send_reconf(asoc, chunk);
  376. if (retval) {
  377. sctp_chunk_put(asoc->strreset_chunk);
  378. asoc->strreset_chunk = NULL;
  379. goto out;
  380. }
  381. asoc->strreset_outstanding = !!out + !!in;
  382. out:
  383. return retval;
  384. }
  385. static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
  386. struct sctp_association *asoc, __be32 resp_seq,
  387. __be16 type)
  388. {
  389. struct sctp_chunk *chunk = asoc->strreset_chunk;
  390. struct sctp_reconf_chunk *hdr;
  391. union sctp_params param;
  392. if (!chunk)
  393. return NULL;
  394. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  395. sctp_walk_params(param, hdr, params) {
  396. /* sctp_strreset_tsnreq is actually the basic structure
  397. * of all stream reconf params, so it's safe to use it
  398. * to access request_seq.
  399. */
  400. struct sctp_strreset_tsnreq *req = param.v;
  401. if ((!resp_seq || req->request_seq == resp_seq) &&
  402. (!type || type == req->param_hdr.type))
  403. return param.v;
  404. }
  405. return NULL;
  406. }
  407. static void sctp_update_strreset_result(struct sctp_association *asoc,
  408. __u32 result)
  409. {
  410. asoc->strreset_result[1] = asoc->strreset_result[0];
  411. asoc->strreset_result[0] = result;
  412. }
  413. struct sctp_chunk *sctp_process_strreset_outreq(
  414. struct sctp_association *asoc,
  415. union sctp_params param,
  416. struct sctp_ulpevent **evp)
  417. {
  418. struct sctp_strreset_outreq *outreq = param.v;
  419. struct sctp_stream *stream = &asoc->stream;
  420. __u32 result = SCTP_STRRESET_DENIED;
  421. __be16 *str_p = NULL;
  422. __u32 request_seq;
  423. __u16 i, nums;
  424. request_seq = ntohl(outreq->request_seq);
  425. if (ntohl(outreq->send_reset_at_tsn) >
  426. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
  427. result = SCTP_STRRESET_IN_PROGRESS;
  428. goto err;
  429. }
  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. goto err;
  438. }
  439. asoc->strreset_inseq++;
  440. /* Check strreset_enable after inseq inc, as sender cannot tell
  441. * the peer doesn't enable strreset after receiving response with
  442. * result denied, as well as to keep consistent with bsd.
  443. */
  444. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  445. goto out;
  446. nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
  447. str_p = outreq->list_of_streams;
  448. for (i = 0; i < nums; i++) {
  449. if (ntohs(str_p[i]) >= stream->incnt) {
  450. result = SCTP_STRRESET_ERR_WRONG_SSN;
  451. goto out;
  452. }
  453. }
  454. if (asoc->strreset_chunk) {
  455. if (!sctp_chunk_lookup_strreset_param(
  456. asoc, outreq->response_seq,
  457. SCTP_PARAM_RESET_IN_REQUEST)) {
  458. /* same process with outstanding isn't 0 */
  459. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  460. goto out;
  461. }
  462. asoc->strreset_outstanding--;
  463. asoc->strreset_outseq++;
  464. if (!asoc->strreset_outstanding) {
  465. struct sctp_transport *t;
  466. t = asoc->strreset_chunk->transport;
  467. if (del_timer(&t->reconf_timer))
  468. sctp_transport_put(t);
  469. sctp_chunk_put(asoc->strreset_chunk);
  470. asoc->strreset_chunk = NULL;
  471. }
  472. }
  473. if (nums)
  474. for (i = 0; i < nums; i++)
  475. SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
  476. else
  477. for (i = 0; i < stream->incnt; i++)
  478. SCTP_SI(stream, i)->mid = 0;
  479. result = SCTP_STRRESET_PERFORMED;
  480. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  481. SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
  482. out:
  483. sctp_update_strreset_result(asoc, result);
  484. err:
  485. return sctp_make_strreset_resp(asoc, result, request_seq);
  486. }
  487. struct sctp_chunk *sctp_process_strreset_inreq(
  488. struct sctp_association *asoc,
  489. union sctp_params param,
  490. struct sctp_ulpevent **evp)
  491. {
  492. struct sctp_strreset_inreq *inreq = param.v;
  493. struct sctp_stream *stream = &asoc->stream;
  494. __u32 result = SCTP_STRRESET_DENIED;
  495. struct sctp_chunk *chunk = NULL;
  496. __u32 request_seq;
  497. __u16 i, nums;
  498. __be16 *str_p;
  499. request_seq = ntohl(inreq->request_seq);
  500. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  501. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  502. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  503. goto err;
  504. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  505. i = asoc->strreset_inseq - request_seq - 1;
  506. result = asoc->strreset_result[i];
  507. if (result == SCTP_STRRESET_PERFORMED)
  508. return NULL;
  509. goto err;
  510. }
  511. asoc->strreset_inseq++;
  512. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  513. goto out;
  514. if (asoc->strreset_outstanding) {
  515. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  516. goto out;
  517. }
  518. nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
  519. str_p = inreq->list_of_streams;
  520. for (i = 0; i < nums; i++) {
  521. if (ntohs(str_p[i]) >= stream->outcnt) {
  522. result = SCTP_STRRESET_ERR_WRONG_SSN;
  523. goto out;
  524. }
  525. }
  526. if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
  527. result = SCTP_STRRESET_IN_PROGRESS;
  528. asoc->strreset_inseq--;
  529. goto err;
  530. }
  531. chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
  532. if (!chunk)
  533. goto out;
  534. if (nums)
  535. for (i = 0; i < nums; i++)
  536. SCTP_SO(stream, ntohs(str_p[i]))->state =
  537. SCTP_STREAM_CLOSED;
  538. else
  539. for (i = 0; i < stream->outcnt; i++)
  540. SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
  541. asoc->strreset_chunk = chunk;
  542. asoc->strreset_outstanding = 1;
  543. sctp_chunk_hold(asoc->strreset_chunk);
  544. result = SCTP_STRRESET_PERFORMED;
  545. out:
  546. sctp_update_strreset_result(asoc, result);
  547. err:
  548. if (!chunk)
  549. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  550. return chunk;
  551. }
  552. struct sctp_chunk *sctp_process_strreset_tsnreq(
  553. struct sctp_association *asoc,
  554. union sctp_params param,
  555. struct sctp_ulpevent **evp)
  556. {
  557. __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
  558. struct sctp_strreset_tsnreq *tsnreq = param.v;
  559. struct sctp_stream *stream = &asoc->stream;
  560. __u32 result = SCTP_STRRESET_DENIED;
  561. __u32 request_seq;
  562. __u16 i;
  563. request_seq = ntohl(tsnreq->request_seq);
  564. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  565. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  566. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  567. goto err;
  568. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  569. i = asoc->strreset_inseq - request_seq - 1;
  570. result = asoc->strreset_result[i];
  571. if (result == SCTP_STRRESET_PERFORMED) {
  572. next_tsn = asoc->ctsn_ack_point + 1;
  573. init_tsn =
  574. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
  575. }
  576. goto err;
  577. }
  578. if (!sctp_outq_is_empty(&asoc->outqueue)) {
  579. result = SCTP_STRRESET_IN_PROGRESS;
  580. goto err;
  581. }
  582. asoc->strreset_inseq++;
  583. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  584. goto out;
  585. if (asoc->strreset_outstanding) {
  586. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  587. goto out;
  588. }
  589. /* G4: The same processing as though a FWD-TSN chunk (as defined in
  590. * [RFC3758]) with all streams affected and a new cumulative TSN
  591. * ACK of the Receiver's Next TSN minus 1 were received MUST be
  592. * performed.
  593. */
  594. max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
  595. asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
  596. /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
  597. * TSN that the peer should use to send the next DATA chunk. The
  598. * value SHOULD be the smallest TSN not acknowledged by the
  599. * receiver of the request plus 2^31.
  600. */
  601. init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
  602. sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
  603. init_tsn, GFP_ATOMIC);
  604. /* G3: The same processing as though a SACK chunk with no gap report
  605. * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
  606. * received MUST be performed.
  607. */
  608. sctp_outq_free(&asoc->outqueue);
  609. /* G2: Compute an appropriate value for the local endpoint's next TSN,
  610. * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
  611. * chunk. The value SHOULD be the highest TSN sent by the receiver
  612. * of the request plus 1.
  613. */
  614. next_tsn = asoc->next_tsn;
  615. asoc->ctsn_ack_point = next_tsn - 1;
  616. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  617. /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
  618. * incoming and outgoing streams.
  619. */
  620. for (i = 0; i < stream->outcnt; i++) {
  621. SCTP_SO(stream, i)->mid = 0;
  622. SCTP_SO(stream, i)->mid_uo = 0;
  623. }
  624. for (i = 0; i < stream->incnt; i++)
  625. SCTP_SI(stream, i)->mid = 0;
  626. result = SCTP_STRRESET_PERFORMED;
  627. *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
  628. next_tsn, GFP_ATOMIC);
  629. out:
  630. sctp_update_strreset_result(asoc, result);
  631. err:
  632. return sctp_make_strreset_tsnresp(asoc, result, request_seq,
  633. next_tsn, init_tsn);
  634. }
  635. struct sctp_chunk *sctp_process_strreset_addstrm_out(
  636. struct sctp_association *asoc,
  637. union sctp_params param,
  638. struct sctp_ulpevent **evp)
  639. {
  640. struct sctp_strreset_addstrm *addstrm = param.v;
  641. struct sctp_stream *stream = &asoc->stream;
  642. __u32 result = SCTP_STRRESET_DENIED;
  643. __u32 request_seq, incnt;
  644. __u16 in, i;
  645. request_seq = ntohl(addstrm->request_seq);
  646. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  647. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  648. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  649. goto err;
  650. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  651. i = asoc->strreset_inseq - request_seq - 1;
  652. result = asoc->strreset_result[i];
  653. goto err;
  654. }
  655. asoc->strreset_inseq++;
  656. if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
  657. goto out;
  658. in = ntohs(addstrm->number_of_streams);
  659. incnt = stream->incnt + in;
  660. if (!in || incnt > SCTP_MAX_STREAM)
  661. goto out;
  662. if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
  663. goto out;
  664. if (asoc->strreset_chunk) {
  665. if (!sctp_chunk_lookup_strreset_param(
  666. asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
  667. /* same process with outstanding isn't 0 */
  668. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  669. goto out;
  670. }
  671. asoc->strreset_outstanding--;
  672. asoc->strreset_outseq++;
  673. if (!asoc->strreset_outstanding) {
  674. struct sctp_transport *t;
  675. t = asoc->strreset_chunk->transport;
  676. if (del_timer(&t->reconf_timer))
  677. sctp_transport_put(t);
  678. sctp_chunk_put(asoc->strreset_chunk);
  679. asoc->strreset_chunk = NULL;
  680. }
  681. }
  682. stream->incnt = incnt;
  683. result = SCTP_STRRESET_PERFORMED;
  684. *evp = sctp_ulpevent_make_stream_change_event(asoc,
  685. 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
  686. out:
  687. sctp_update_strreset_result(asoc, result);
  688. err:
  689. return sctp_make_strreset_resp(asoc, result, request_seq);
  690. }
  691. struct sctp_chunk *sctp_process_strreset_addstrm_in(
  692. struct sctp_association *asoc,
  693. union sctp_params param,
  694. struct sctp_ulpevent **evp)
  695. {
  696. struct sctp_strreset_addstrm *addstrm = param.v;
  697. struct sctp_stream *stream = &asoc->stream;
  698. __u32 result = SCTP_STRRESET_DENIED;
  699. struct sctp_chunk *chunk = NULL;
  700. __u32 request_seq, outcnt;
  701. __u16 out, i;
  702. int ret;
  703. request_seq = ntohl(addstrm->request_seq);
  704. if (TSN_lt(asoc->strreset_inseq, request_seq) ||
  705. TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
  706. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  707. goto err;
  708. } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
  709. i = asoc->strreset_inseq - request_seq - 1;
  710. result = asoc->strreset_result[i];
  711. if (result == SCTP_STRRESET_PERFORMED)
  712. return NULL;
  713. goto err;
  714. }
  715. asoc->strreset_inseq++;
  716. if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
  717. goto out;
  718. if (asoc->strreset_outstanding) {
  719. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  720. goto out;
  721. }
  722. out = ntohs(addstrm->number_of_streams);
  723. outcnt = stream->outcnt + out;
  724. if (!out || outcnt > SCTP_MAX_STREAM)
  725. goto out;
  726. ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
  727. if (ret)
  728. goto out;
  729. chunk = sctp_make_strreset_addstrm(asoc, out, 0);
  730. if (!chunk)
  731. goto out;
  732. asoc->strreset_chunk = chunk;
  733. asoc->strreset_outstanding = 1;
  734. sctp_chunk_hold(asoc->strreset_chunk);
  735. stream->outcnt = outcnt;
  736. result = SCTP_STRRESET_PERFORMED;
  737. out:
  738. sctp_update_strreset_result(asoc, result);
  739. err:
  740. if (!chunk)
  741. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  742. return chunk;
  743. }
  744. struct sctp_chunk *sctp_process_strreset_resp(
  745. struct sctp_association *asoc,
  746. union sctp_params param,
  747. struct sctp_ulpevent **evp)
  748. {
  749. struct sctp_stream *stream = &asoc->stream;
  750. struct sctp_strreset_resp *resp = param.v;
  751. struct sctp_transport *t;
  752. __u16 i, nums, flags = 0;
  753. struct sctp_paramhdr *req;
  754. __u32 result;
  755. req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
  756. if (!req)
  757. return NULL;
  758. result = ntohl(resp->result);
  759. if (result != SCTP_STRRESET_PERFORMED) {
  760. /* if in progress, do nothing but retransmit */
  761. if (result == SCTP_STRRESET_IN_PROGRESS)
  762. return NULL;
  763. else if (result == SCTP_STRRESET_DENIED)
  764. flags = SCTP_STREAM_RESET_DENIED;
  765. else
  766. flags = SCTP_STREAM_RESET_FAILED;
  767. }
  768. if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
  769. struct sctp_strreset_outreq *outreq;
  770. __be16 *str_p;
  771. outreq = (struct sctp_strreset_outreq *)req;
  772. str_p = outreq->list_of_streams;
  773. nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
  774. sizeof(__u16);
  775. if (result == SCTP_STRRESET_PERFORMED) {
  776. struct sctp_stream_out *sout;
  777. if (nums) {
  778. for (i = 0; i < nums; i++) {
  779. sout = SCTP_SO(stream, ntohs(str_p[i]));
  780. sout->mid = 0;
  781. sout->mid_uo = 0;
  782. }
  783. } else {
  784. for (i = 0; i < stream->outcnt; i++) {
  785. sout = SCTP_SO(stream, i);
  786. sout->mid = 0;
  787. sout->mid_uo = 0;
  788. }
  789. }
  790. }
  791. flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
  792. for (i = 0; i < stream->outcnt; i++)
  793. SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
  794. *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
  795. nums, str_p, GFP_ATOMIC);
  796. } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
  797. struct sctp_strreset_inreq *inreq;
  798. __be16 *str_p;
  799. /* if the result is performed, it's impossible for inreq */
  800. if (result == SCTP_STRRESET_PERFORMED)
  801. return NULL;
  802. inreq = (struct sctp_strreset_inreq *)req;
  803. str_p = inreq->list_of_streams;
  804. nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
  805. sizeof(__u16);
  806. flags |= SCTP_STREAM_RESET_INCOMING_SSN;
  807. *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
  808. nums, str_p, GFP_ATOMIC);
  809. } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
  810. struct sctp_strreset_resptsn *resptsn;
  811. __u32 stsn, rtsn;
  812. /* check for resptsn, as sctp_verify_reconf didn't do it*/
  813. if (ntohs(param.p->length) != sizeof(*resptsn))
  814. return NULL;
  815. resptsn = (struct sctp_strreset_resptsn *)resp;
  816. stsn = ntohl(resptsn->senders_next_tsn);
  817. rtsn = ntohl(resptsn->receivers_next_tsn);
  818. if (result == SCTP_STRRESET_PERFORMED) {
  819. __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
  820. &asoc->peer.tsn_map);
  821. LIST_HEAD(temp);
  822. asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
  823. sctp_tsnmap_init(&asoc->peer.tsn_map,
  824. SCTP_TSN_MAP_INITIAL,
  825. stsn, GFP_ATOMIC);
  826. /* Clean up sacked and abandoned queues only. As the
  827. * out_chunk_list may not be empty, splice it to temp,
  828. * then get it back after sctp_outq_free is done.
  829. */
  830. list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
  831. sctp_outq_free(&asoc->outqueue);
  832. list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
  833. asoc->next_tsn = rtsn;
  834. asoc->ctsn_ack_point = asoc->next_tsn - 1;
  835. asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
  836. for (i = 0; i < stream->outcnt; i++) {
  837. SCTP_SO(stream, i)->mid = 0;
  838. SCTP_SO(stream, i)->mid_uo = 0;
  839. }
  840. for (i = 0; i < stream->incnt; i++)
  841. SCTP_SI(stream, i)->mid = 0;
  842. }
  843. for (i = 0; i < stream->outcnt; i++)
  844. SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
  845. *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
  846. stsn, rtsn, GFP_ATOMIC);
  847. } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
  848. struct sctp_strreset_addstrm *addstrm;
  849. __u16 number;
  850. addstrm = (struct sctp_strreset_addstrm *)req;
  851. nums = ntohs(addstrm->number_of_streams);
  852. number = stream->outcnt - nums;
  853. if (result == SCTP_STRRESET_PERFORMED) {
  854. for (i = number; i < stream->outcnt; i++)
  855. SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
  856. } else {
  857. sctp_stream_shrink_out(stream, number);
  858. stream->outcnt = number;
  859. }
  860. *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
  861. 0, nums, GFP_ATOMIC);
  862. } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
  863. struct sctp_strreset_addstrm *addstrm;
  864. /* if the result is performed, it's impossible for addstrm in
  865. * request.
  866. */
  867. if (result == SCTP_STRRESET_PERFORMED)
  868. return NULL;
  869. addstrm = (struct sctp_strreset_addstrm *)req;
  870. nums = ntohs(addstrm->number_of_streams);
  871. *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
  872. nums, 0, GFP_ATOMIC);
  873. }
  874. asoc->strreset_outstanding--;
  875. asoc->strreset_outseq++;
  876. /* remove everything for this reconf request */
  877. if (!asoc->strreset_outstanding) {
  878. t = asoc->strreset_chunk->transport;
  879. if (del_timer(&t->reconf_timer))
  880. sctp_transport_put(t);
  881. sctp_chunk_put(asoc->strreset_chunk);
  882. asoc->strreset_chunk = NULL;
  883. }
  884. return NULL;
  885. }