stream.c 27 KB

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