stream.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * These functions manipulate sctp tsn mapping array.
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  31. * Written or modified by:
  32. * Xin Long <lucien.xin@gmail.com>
  33. */
  34. #include <net/sctp/sctp.h>
  35. #include <net/sctp/sm.h>
  36. struct sctp_stream *sctp_stream_new(__u16 incnt, __u16 outcnt, gfp_t gfp)
  37. {
  38. struct sctp_stream *stream;
  39. int i;
  40. stream = kzalloc(sizeof(*stream), gfp);
  41. if (!stream)
  42. return NULL;
  43. stream->outcnt = outcnt;
  44. stream->out = kcalloc(stream->outcnt, sizeof(*stream->out), gfp);
  45. if (!stream->out) {
  46. kfree(stream);
  47. return NULL;
  48. }
  49. for (i = 0; i < stream->outcnt; i++)
  50. stream->out[i].state = SCTP_STREAM_OPEN;
  51. stream->incnt = incnt;
  52. stream->in = kcalloc(stream->incnt, sizeof(*stream->in), gfp);
  53. if (!stream->in) {
  54. kfree(stream->out);
  55. kfree(stream);
  56. return NULL;
  57. }
  58. return stream;
  59. }
  60. void sctp_stream_free(struct sctp_stream *stream)
  61. {
  62. if (unlikely(!stream))
  63. return;
  64. kfree(stream->out);
  65. kfree(stream->in);
  66. kfree(stream);
  67. }
  68. void sctp_stream_clear(struct sctp_stream *stream)
  69. {
  70. int i;
  71. for (i = 0; i < stream->outcnt; i++)
  72. stream->out[i].ssn = 0;
  73. for (i = 0; i < stream->incnt; i++)
  74. stream->in[i].ssn = 0;
  75. }
  76. static int sctp_send_reconf(struct sctp_association *asoc,
  77. struct sctp_chunk *chunk)
  78. {
  79. struct net *net = sock_net(asoc->base.sk);
  80. int retval = 0;
  81. retval = sctp_primitive_RECONF(net, asoc, chunk);
  82. if (retval)
  83. sctp_chunk_free(chunk);
  84. return retval;
  85. }
  86. int sctp_send_reset_streams(struct sctp_association *asoc,
  87. struct sctp_reset_streams *params)
  88. {
  89. struct sctp_stream *stream = asoc->stream;
  90. __u16 i, str_nums, *str_list;
  91. struct sctp_chunk *chunk;
  92. int retval = -EINVAL;
  93. bool out, in;
  94. if (!asoc->peer.reconf_capable ||
  95. !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
  96. retval = -ENOPROTOOPT;
  97. goto out;
  98. }
  99. if (asoc->strreset_outstanding) {
  100. retval = -EINPROGRESS;
  101. goto out;
  102. }
  103. out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
  104. in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
  105. if (!out && !in)
  106. goto out;
  107. str_nums = params->srs_number_streams;
  108. str_list = params->srs_stream_list;
  109. if (out && str_nums)
  110. for (i = 0; i < str_nums; i++)
  111. if (str_list[i] >= stream->outcnt)
  112. goto out;
  113. if (in && str_nums)
  114. for (i = 0; i < str_nums; i++)
  115. if (str_list[i] >= stream->incnt)
  116. goto out;
  117. for (i = 0; i < str_nums; i++)
  118. str_list[i] = htons(str_list[i]);
  119. chunk = sctp_make_strreset_req(asoc, str_nums, str_list, out, in);
  120. for (i = 0; i < str_nums; i++)
  121. str_list[i] = ntohs(str_list[i]);
  122. if (!chunk) {
  123. retval = -ENOMEM;
  124. goto out;
  125. }
  126. if (out) {
  127. if (str_nums)
  128. for (i = 0; i < str_nums; i++)
  129. stream->out[str_list[i]].state =
  130. SCTP_STREAM_CLOSED;
  131. else
  132. for (i = 0; i < stream->outcnt; i++)
  133. stream->out[i].state = SCTP_STREAM_CLOSED;
  134. }
  135. asoc->strreset_chunk = chunk;
  136. sctp_chunk_hold(asoc->strreset_chunk);
  137. retval = sctp_send_reconf(asoc, chunk);
  138. if (retval) {
  139. sctp_chunk_put(asoc->strreset_chunk);
  140. asoc->strreset_chunk = NULL;
  141. if (!out)
  142. goto out;
  143. if (str_nums)
  144. for (i = 0; i < str_nums; i++)
  145. stream->out[str_list[i]].state =
  146. SCTP_STREAM_OPEN;
  147. else
  148. for (i = 0; i < stream->outcnt; i++)
  149. stream->out[i].state = SCTP_STREAM_OPEN;
  150. goto out;
  151. }
  152. asoc->strreset_outstanding = out + in;
  153. out:
  154. return retval;
  155. }
  156. int sctp_send_reset_assoc(struct sctp_association *asoc)
  157. {
  158. struct sctp_chunk *chunk = NULL;
  159. int retval;
  160. __u16 i;
  161. if (!asoc->peer.reconf_capable ||
  162. !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
  163. return -ENOPROTOOPT;
  164. if (asoc->strreset_outstanding)
  165. return -EINPROGRESS;
  166. chunk = sctp_make_strreset_tsnreq(asoc);
  167. if (!chunk)
  168. return -ENOMEM;
  169. /* Block further xmit of data until this request is completed */
  170. for (i = 0; i < asoc->stream->outcnt; i++)
  171. asoc->stream->out[i].state = SCTP_STREAM_CLOSED;
  172. asoc->strreset_chunk = chunk;
  173. sctp_chunk_hold(asoc->strreset_chunk);
  174. retval = sctp_send_reconf(asoc, chunk);
  175. if (retval) {
  176. sctp_chunk_put(asoc->strreset_chunk);
  177. asoc->strreset_chunk = NULL;
  178. for (i = 0; i < asoc->stream->outcnt; i++)
  179. asoc->stream->out[i].state = SCTP_STREAM_OPEN;
  180. return retval;
  181. }
  182. asoc->strreset_outstanding = 1;
  183. return 0;
  184. }
  185. int sctp_send_add_streams(struct sctp_association *asoc,
  186. struct sctp_add_streams *params)
  187. {
  188. struct sctp_stream *stream = asoc->stream;
  189. struct sctp_chunk *chunk = NULL;
  190. int retval = -ENOMEM;
  191. __u32 outcnt, incnt;
  192. __u16 out, in;
  193. if (!asoc->peer.reconf_capable ||
  194. !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
  195. retval = -ENOPROTOOPT;
  196. goto out;
  197. }
  198. if (asoc->strreset_outstanding) {
  199. retval = -EINPROGRESS;
  200. goto out;
  201. }
  202. out = params->sas_outstrms;
  203. in = params->sas_instrms;
  204. outcnt = stream->outcnt + out;
  205. incnt = stream->incnt + in;
  206. if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
  207. (!out && !in)) {
  208. retval = -EINVAL;
  209. goto out;
  210. }
  211. if (out) {
  212. struct sctp_stream_out *streamout;
  213. streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
  214. GFP_KERNEL);
  215. if (!streamout)
  216. goto out;
  217. memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
  218. stream->out = streamout;
  219. }
  220. if (in) {
  221. struct sctp_stream_in *streamin;
  222. streamin = krealloc(stream->in, incnt * sizeof(*streamin),
  223. GFP_KERNEL);
  224. if (!streamin)
  225. goto out;
  226. memset(streamin + stream->incnt, 0, in * sizeof(*streamin));
  227. stream->in = streamin;
  228. }
  229. chunk = sctp_make_strreset_addstrm(asoc, out, in);
  230. if (!chunk)
  231. goto out;
  232. asoc->strreset_chunk = chunk;
  233. sctp_chunk_hold(asoc->strreset_chunk);
  234. retval = sctp_send_reconf(asoc, chunk);
  235. if (retval) {
  236. sctp_chunk_put(asoc->strreset_chunk);
  237. asoc->strreset_chunk = NULL;
  238. goto out;
  239. }
  240. stream->incnt = incnt;
  241. stream->outcnt = outcnt;
  242. asoc->strreset_outstanding = !!out + !!in;
  243. out:
  244. return retval;
  245. }
  246. static sctp_paramhdr_t *sctp_chunk_lookup_strreset_param(
  247. struct sctp_association *asoc, __u32 resp_seq)
  248. {
  249. struct sctp_chunk *chunk = asoc->strreset_chunk;
  250. struct sctp_reconf_chunk *hdr;
  251. union sctp_params param;
  252. if (ntohl(resp_seq) != asoc->strreset_outseq || !chunk)
  253. return NULL;
  254. hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
  255. sctp_walk_params(param, hdr, params) {
  256. /* sctp_strreset_tsnreq is actually the basic structure
  257. * of all stream reconf params, so it's safe to use it
  258. * to access request_seq.
  259. */
  260. struct sctp_strreset_tsnreq *req = param.v;
  261. if (req->request_seq == resp_seq)
  262. return param.v;
  263. }
  264. return NULL;
  265. }
  266. struct sctp_chunk *sctp_process_strreset_outreq(
  267. struct sctp_association *asoc,
  268. union sctp_params param,
  269. struct sctp_ulpevent **evp)
  270. {
  271. struct sctp_strreset_outreq *outreq = param.v;
  272. struct sctp_stream *stream = asoc->stream;
  273. __u16 i, nums, flags = 0, *str_p = NULL;
  274. __u32 result = SCTP_STRRESET_DENIED;
  275. __u32 request_seq;
  276. request_seq = ntohl(outreq->request_seq);
  277. if (ntohl(outreq->send_reset_at_tsn) >
  278. sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
  279. result = SCTP_STRRESET_IN_PROGRESS;
  280. goto out;
  281. }
  282. if (request_seq > asoc->strreset_inseq) {
  283. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  284. goto out;
  285. } else if (request_seq == asoc->strreset_inseq) {
  286. asoc->strreset_inseq++;
  287. }
  288. /* Check strreset_enable after inseq inc, as sender cannot tell
  289. * the peer doesn't enable strreset after receiving response with
  290. * result denied, as well as to keep consistent with bsd.
  291. */
  292. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  293. goto out;
  294. if (asoc->strreset_chunk) {
  295. sctp_paramhdr_t *param_hdr;
  296. struct sctp_transport *t;
  297. param_hdr = sctp_chunk_lookup_strreset_param(
  298. asoc, outreq->response_seq);
  299. if (!param_hdr || param_hdr->type !=
  300. SCTP_PARAM_RESET_IN_REQUEST) {
  301. /* same process with outstanding isn't 0 */
  302. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  303. goto out;
  304. }
  305. asoc->strreset_outstanding--;
  306. asoc->strreset_outseq++;
  307. if (!asoc->strreset_outstanding) {
  308. t = asoc->strreset_chunk->transport;
  309. if (del_timer(&t->reconf_timer))
  310. sctp_transport_put(t);
  311. sctp_chunk_put(asoc->strreset_chunk);
  312. asoc->strreset_chunk = NULL;
  313. }
  314. flags = SCTP_STREAM_RESET_INCOMING_SSN;
  315. }
  316. nums = (ntohs(param.p->length) - sizeof(*outreq)) / 2;
  317. if (nums) {
  318. str_p = outreq->list_of_streams;
  319. for (i = 0; i < nums; i++) {
  320. if (ntohs(str_p[i]) >= stream->incnt) {
  321. result = SCTP_STRRESET_ERR_WRONG_SSN;
  322. goto out;
  323. }
  324. }
  325. for (i = 0; i < nums; i++)
  326. stream->in[ntohs(str_p[i])].ssn = 0;
  327. } else {
  328. for (i = 0; i < stream->incnt; i++)
  329. stream->in[i].ssn = 0;
  330. }
  331. result = SCTP_STRRESET_PERFORMED;
  332. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  333. flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
  334. GFP_ATOMIC);
  335. out:
  336. return sctp_make_strreset_resp(asoc, result, request_seq);
  337. }
  338. struct sctp_chunk *sctp_process_strreset_inreq(
  339. struct sctp_association *asoc,
  340. union sctp_params param,
  341. struct sctp_ulpevent **evp)
  342. {
  343. struct sctp_strreset_inreq *inreq = param.v;
  344. struct sctp_stream *stream = asoc->stream;
  345. __u32 result = SCTP_STRRESET_DENIED;
  346. struct sctp_chunk *chunk = NULL;
  347. __u16 i, nums, *str_p;
  348. __u32 request_seq;
  349. request_seq = ntohl(inreq->request_seq);
  350. if (request_seq > asoc->strreset_inseq) {
  351. result = SCTP_STRRESET_ERR_BAD_SEQNO;
  352. goto out;
  353. } else if (request_seq == asoc->strreset_inseq) {
  354. asoc->strreset_inseq++;
  355. }
  356. if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
  357. goto out;
  358. if (asoc->strreset_outstanding) {
  359. result = SCTP_STRRESET_ERR_IN_PROGRESS;
  360. goto out;
  361. }
  362. nums = (ntohs(param.p->length) - sizeof(*inreq)) / 2;
  363. str_p = inreq->list_of_streams;
  364. for (i = 0; i < nums; i++) {
  365. if (ntohs(str_p[i]) >= stream->outcnt) {
  366. result = SCTP_STRRESET_ERR_WRONG_SSN;
  367. goto out;
  368. }
  369. }
  370. chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
  371. if (!chunk)
  372. goto out;
  373. if (nums)
  374. for (i = 0; i < nums; i++)
  375. stream->out[ntohs(str_p[i])].state =
  376. SCTP_STREAM_CLOSED;
  377. else
  378. for (i = 0; i < stream->outcnt; i++)
  379. stream->out[i].state = SCTP_STREAM_CLOSED;
  380. asoc->strreset_chunk = chunk;
  381. asoc->strreset_outstanding = 1;
  382. sctp_chunk_hold(asoc->strreset_chunk);
  383. *evp = sctp_ulpevent_make_stream_reset_event(asoc,
  384. SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
  385. out:
  386. if (!chunk)
  387. chunk = sctp_make_strreset_resp(asoc, result, request_seq);
  388. return chunk;
  389. }