stream.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. }