stream.c 29 KB

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