stream.c 27 KB

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