1 /* SCTP kernel implementation
2 * (C) Copyright Red Hat Inc. 2017
4 * This file is part of the SCTP kernel implementation
6 * These functions implement sctp stream message interleaving, mostly
7 * including I-DATA and I-FORWARD-TSN chunks process.
9 * This SCTP implementation is free software;
10 * you can redistribute it and/or modify it under the terms of
11 * the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This SCTP implementation is distributed in the hope that it
16 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17 * ************************
18 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GNU CC; see the file COPYING. If not, see
23 * <http://www.gnu.org/licenses/>.
25 * Please send any bug reports or fixes you make to the
26 * email addresched(es):
29 * Written or modified by:
33 #include <net/busy_poll.h>
34 #include <net/sctp/sctp.h>
35 #include <net/sctp/sm.h>
36 #include <net/sctp/ulpevent.h>
37 #include <linux/sctp.h>
39 static struct sctp_chunk *sctp_make_idatafrag_empty(
40 const struct sctp_association *asoc,
41 const struct sctp_sndrcvinfo *sinfo,
42 int len, __u8 flags, gfp_t gfp)
44 struct sctp_chunk *retval;
45 struct sctp_idatahdr dp;
47 memset(&dp, 0, sizeof(dp));
48 dp.stream = htons(sinfo->sinfo_stream);
50 if (sinfo->sinfo_flags & SCTP_UNORDERED)
51 flags |= SCTP_DATA_UNORDERED;
53 retval = sctp_make_idata(asoc, flags, sizeof(dp) + len, gfp);
57 retval->subh.idata_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
58 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
63 static void sctp_chunk_assign_mid(struct sctp_chunk *chunk)
65 struct sctp_stream *stream;
66 struct sctp_chunk *lchunk;
73 sid = sctp_chunk_stream_no(chunk);
74 stream = &chunk->asoc->stream;
76 list_for_each_entry(lchunk, &chunk->msg->chunks, frag_list) {
77 struct sctp_idatahdr *hdr;
82 hdr = lchunk->subh.idata_hdr;
84 if (lchunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG)
85 hdr->ppid = lchunk->sinfo.sinfo_ppid;
87 hdr->fsn = htonl(cfsn++);
89 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
90 mid = lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG ?
91 sctp_mid_uo_next(stream, out, sid) :
92 sctp_mid_uo_peek(stream, out, sid);
94 mid = lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG ?
95 sctp_mid_next(stream, out, sid) :
96 sctp_mid_peek(stream, out, sid);
98 hdr->mid = htonl(mid);
102 static bool sctp_validate_data(struct sctp_chunk *chunk)
104 struct sctp_stream *stream;
107 if (chunk->chunk_hdr->type != SCTP_CID_DATA)
110 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
113 stream = &chunk->asoc->stream;
114 sid = sctp_chunk_stream_no(chunk);
115 ssn = ntohs(chunk->subh.data_hdr->ssn);
117 return !SSN_lt(ssn, sctp_ssn_peek(stream, in, sid));
120 static bool sctp_validate_idata(struct sctp_chunk *chunk)
122 struct sctp_stream *stream;
126 if (chunk->chunk_hdr->type != SCTP_CID_I_DATA)
129 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
132 stream = &chunk->asoc->stream;
133 sid = sctp_chunk_stream_no(chunk);
134 mid = ntohl(chunk->subh.idata_hdr->mid);
136 return !MID_lt(mid, sctp_mid_peek(stream, in, sid));
139 static void sctp_intl_store_reasm(struct sctp_ulpq *ulpq,
140 struct sctp_ulpevent *event)
142 struct sctp_ulpevent *cevent;
143 struct sk_buff *pos, *loc;
145 pos = skb_peek_tail(&ulpq->reasm);
147 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
151 cevent = sctp_skb2event(pos);
153 if (event->stream == cevent->stream &&
154 event->mid == cevent->mid &&
155 (cevent->msg_flags & SCTP_DATA_FIRST_FRAG ||
156 (!(event->msg_flags & SCTP_DATA_FIRST_FRAG) &&
157 event->fsn > cevent->fsn))) {
158 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
162 if ((event->stream == cevent->stream &&
163 MID_lt(cevent->mid, event->mid)) ||
164 event->stream > cevent->stream) {
165 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
170 skb_queue_walk(&ulpq->reasm, pos) {
171 cevent = sctp_skb2event(pos);
173 if (event->stream < cevent->stream ||
174 (event->stream == cevent->stream &&
175 MID_lt(event->mid, cevent->mid))) {
179 if (event->stream == cevent->stream &&
180 event->mid == cevent->mid &&
181 !(cevent->msg_flags & SCTP_DATA_FIRST_FRAG) &&
182 (event->msg_flags & SCTP_DATA_FIRST_FRAG ||
183 event->fsn < cevent->fsn)) {
190 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
192 __skb_queue_before(&ulpq->reasm, loc, sctp_event2skb(event));
195 static struct sctp_ulpevent *sctp_intl_retrieve_partial(
196 struct sctp_ulpq *ulpq,
197 struct sctp_ulpevent *event)
199 struct sk_buff *first_frag = NULL;
200 struct sk_buff *last_frag = NULL;
201 struct sctp_ulpevent *retval;
202 struct sctp_stream_in *sin;
207 sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
209 skb_queue_walk(&ulpq->reasm, pos) {
210 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
212 if (cevent->stream < event->stream)
215 if (cevent->stream > event->stream ||
216 cevent->mid != sin->mid)
219 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
220 case SCTP_DATA_FIRST_FRAG:
222 case SCTP_DATA_MIDDLE_FRAG:
224 if (cevent->fsn == sin->fsn) {
227 next_fsn = cevent->fsn + 1;
229 } else if (cevent->fsn == next_fsn) {
236 case SCTP_DATA_LAST_FRAG:
238 if (cevent->fsn == sin->fsn) {
244 } else if (cevent->fsn == next_fsn) {
259 retval = sctp_make_reassembled_event(sock_net(ulpq->asoc->base.sk),
260 &ulpq->reasm, first_frag,
265 retval->msg_flags |= MSG_EOR;
273 static struct sctp_ulpevent *sctp_intl_retrieve_reassembled(
274 struct sctp_ulpq *ulpq,
275 struct sctp_ulpevent *event)
277 struct sctp_association *asoc = ulpq->asoc;
278 struct sk_buff *pos, *first_frag = NULL;
279 struct sctp_ulpevent *retval = NULL;
280 struct sk_buff *pd_first = NULL;
281 struct sk_buff *pd_last = NULL;
282 struct sctp_stream_in *sin;
288 sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
290 skb_queue_walk(&ulpq->reasm, pos) {
291 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
293 if (cevent->stream < event->stream)
295 if (cevent->stream > event->stream)
298 if (MID_lt(cevent->mid, event->mid))
300 if (MID_lt(event->mid, cevent->mid))
303 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
304 case SCTP_DATA_FIRST_FRAG:
305 if (cevent->mid == sin->mid) {
316 case SCTP_DATA_MIDDLE_FRAG:
317 if (first_frag && cevent->mid == mid &&
318 cevent->fsn == next_fsn) {
329 case SCTP_DATA_LAST_FRAG:
330 if (first_frag && cevent->mid == mid &&
331 cevent->fsn == next_fsn)
342 pd_point = sctp_sk(asoc->base.sk)->pd_point;
343 if (pd_point && pd_point <= pd_len) {
344 retval = sctp_make_reassembled_event(sock_net(asoc->base.sk),
355 retval = sctp_make_reassembled_event(sock_net(asoc->base.sk),
359 retval->msg_flags |= MSG_EOR;
365 static struct sctp_ulpevent *sctp_intl_reasm(struct sctp_ulpq *ulpq,
366 struct sctp_ulpevent *event)
368 struct sctp_ulpevent *retval = NULL;
369 struct sctp_stream_in *sin;
371 if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
372 event->msg_flags |= MSG_EOR;
376 sctp_intl_store_reasm(ulpq, event);
378 sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
379 if (sin->pd_mode && event->mid == sin->mid &&
380 event->fsn == sin->fsn)
381 retval = sctp_intl_retrieve_partial(ulpq, event);
384 retval = sctp_intl_retrieve_reassembled(ulpq, event);
389 static void sctp_intl_store_ordered(struct sctp_ulpq *ulpq,
390 struct sctp_ulpevent *event)
392 struct sctp_ulpevent *cevent;
393 struct sk_buff *pos, *loc;
395 pos = skb_peek_tail(&ulpq->lobby);
397 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
401 cevent = (struct sctp_ulpevent *)pos->cb;
402 if (event->stream == cevent->stream &&
403 MID_lt(cevent->mid, event->mid)) {
404 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
408 if (event->stream > cevent->stream) {
409 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
414 skb_queue_walk(&ulpq->lobby, pos) {
415 cevent = (struct sctp_ulpevent *)pos->cb;
417 if (cevent->stream > event->stream) {
421 if (cevent->stream == event->stream &&
422 MID_lt(event->mid, cevent->mid)) {
429 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
431 __skb_queue_before(&ulpq->lobby, loc, sctp_event2skb(event));
434 static void sctp_intl_retrieve_ordered(struct sctp_ulpq *ulpq,
435 struct sctp_ulpevent *event)
437 struct sk_buff_head *event_list;
438 struct sctp_stream *stream;
439 struct sk_buff *pos, *tmp;
440 __u16 sid = event->stream;
442 stream = &ulpq->asoc->stream;
443 event_list = (struct sk_buff_head *)sctp_event2skb(event)->prev;
445 sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
446 struct sctp_ulpevent *cevent = (struct sctp_ulpevent *)pos->cb;
448 if (cevent->stream > sid)
451 if (cevent->stream < sid)
454 if (cevent->mid != sctp_mid_peek(stream, in, sid))
457 sctp_mid_next(stream, in, sid);
459 __skb_unlink(pos, &ulpq->lobby);
461 __skb_queue_tail(event_list, pos);
465 static struct sctp_ulpevent *sctp_intl_order(struct sctp_ulpq *ulpq,
466 struct sctp_ulpevent *event)
468 struct sctp_stream *stream;
471 stream = &ulpq->asoc->stream;
474 if (event->mid != sctp_mid_peek(stream, in, sid)) {
475 sctp_intl_store_ordered(ulpq, event);
479 sctp_mid_next(stream, in, sid);
481 sctp_intl_retrieve_ordered(ulpq, event);
486 static int sctp_enqueue_event(struct sctp_ulpq *ulpq,
487 struct sctp_ulpevent *event)
489 struct sk_buff *skb = sctp_event2skb(event);
490 struct sock *sk = ulpq->asoc->base.sk;
491 struct sctp_sock *sp = sctp_sk(sk);
492 struct sk_buff_head *skb_list;
494 skb_list = (struct sk_buff_head *)skb->prev;
496 if (sk->sk_shutdown & RCV_SHUTDOWN &&
497 (sk->sk_shutdown & SEND_SHUTDOWN ||
498 !sctp_ulpevent_is_notification(event)))
501 if (!sctp_ulpevent_is_notification(event)) {
502 sk_mark_napi_id(sk, skb);
503 sk_incoming_cpu_update(sk);
506 if (!sctp_ulpevent_is_enabled(event, ulpq->asoc->subscribe))
510 skb_queue_splice_tail_init(skb_list,
511 &sk->sk_receive_queue);
513 __skb_queue_tail(&sk->sk_receive_queue, skb);
515 if (!sp->data_ready_signalled) {
516 sp->data_ready_signalled = 1;
517 sk->sk_data_ready(sk);
524 sctp_queue_purge_ulpevents(skb_list);
526 sctp_ulpevent_free(event);
531 static void sctp_intl_store_reasm_uo(struct sctp_ulpq *ulpq,
532 struct sctp_ulpevent *event)
534 struct sctp_ulpevent *cevent;
537 pos = skb_peek_tail(&ulpq->reasm_uo);
539 __skb_queue_tail(&ulpq->reasm_uo, sctp_event2skb(event));
543 cevent = sctp_skb2event(pos);
545 if (event->stream == cevent->stream &&
546 event->mid == cevent->mid &&
547 (cevent->msg_flags & SCTP_DATA_FIRST_FRAG ||
548 (!(event->msg_flags & SCTP_DATA_FIRST_FRAG) &&
549 event->fsn > cevent->fsn))) {
550 __skb_queue_tail(&ulpq->reasm_uo, sctp_event2skb(event));
554 if ((event->stream == cevent->stream &&
555 MID_lt(cevent->mid, event->mid)) ||
556 event->stream > cevent->stream) {
557 __skb_queue_tail(&ulpq->reasm_uo, sctp_event2skb(event));
561 skb_queue_walk(&ulpq->reasm_uo, pos) {
562 cevent = sctp_skb2event(pos);
564 if (event->stream < cevent->stream ||
565 (event->stream == cevent->stream &&
566 MID_lt(event->mid, cevent->mid)))
569 if (event->stream == cevent->stream &&
570 event->mid == cevent->mid &&
571 !(cevent->msg_flags & SCTP_DATA_FIRST_FRAG) &&
572 (event->msg_flags & SCTP_DATA_FIRST_FRAG ||
573 event->fsn < cevent->fsn))
577 __skb_queue_before(&ulpq->reasm_uo, pos, sctp_event2skb(event));
580 static struct sctp_ulpevent *sctp_intl_retrieve_partial_uo(
581 struct sctp_ulpq *ulpq,
582 struct sctp_ulpevent *event)
584 struct sk_buff *first_frag = NULL;
585 struct sk_buff *last_frag = NULL;
586 struct sctp_ulpevent *retval;
587 struct sctp_stream_in *sin;
592 sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
594 skb_queue_walk(&ulpq->reasm_uo, pos) {
595 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
597 if (cevent->stream < event->stream)
599 if (cevent->stream > event->stream)
602 if (MID_lt(cevent->mid, sin->mid_uo))
604 if (MID_lt(sin->mid_uo, cevent->mid))
607 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
608 case SCTP_DATA_FIRST_FRAG:
610 case SCTP_DATA_MIDDLE_FRAG:
612 if (cevent->fsn == sin->fsn_uo) {
615 next_fsn = cevent->fsn + 1;
617 } else if (cevent->fsn == next_fsn) {
624 case SCTP_DATA_LAST_FRAG:
626 if (cevent->fsn == sin->fsn_uo) {
632 } else if (cevent->fsn == next_fsn) {
647 retval = sctp_make_reassembled_event(sock_net(ulpq->asoc->base.sk),
648 &ulpq->reasm_uo, first_frag,
651 sin->fsn_uo = next_fsn;
653 retval->msg_flags |= MSG_EOR;
661 static struct sctp_ulpevent *sctp_intl_retrieve_reassembled_uo(
662 struct sctp_ulpq *ulpq,
663 struct sctp_ulpevent *event)
665 struct sctp_association *asoc = ulpq->asoc;
666 struct sk_buff *pos, *first_frag = NULL;
667 struct sctp_ulpevent *retval = NULL;
668 struct sk_buff *pd_first = NULL;
669 struct sk_buff *pd_last = NULL;
670 struct sctp_stream_in *sin;
676 sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
678 skb_queue_walk(&ulpq->reasm_uo, pos) {
679 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
681 if (cevent->stream < event->stream)
683 if (cevent->stream > event->stream)
686 if (MID_lt(cevent->mid, event->mid))
688 if (MID_lt(event->mid, cevent->mid))
691 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
692 case SCTP_DATA_FIRST_FRAG:
693 if (!sin->pd_mode_uo) {
694 sin->mid_uo = cevent->mid;
705 case SCTP_DATA_MIDDLE_FRAG:
706 if (first_frag && cevent->mid == mid &&
707 cevent->fsn == next_fsn) {
718 case SCTP_DATA_LAST_FRAG:
719 if (first_frag && cevent->mid == mid &&
720 cevent->fsn == next_fsn)
731 pd_point = sctp_sk(asoc->base.sk)->pd_point;
732 if (pd_point && pd_point <= pd_len) {
733 retval = sctp_make_reassembled_event(sock_net(asoc->base.sk),
737 sin->fsn_uo = next_fsn;
744 retval = sctp_make_reassembled_event(sock_net(asoc->base.sk),
748 retval->msg_flags |= MSG_EOR;
754 static struct sctp_ulpevent *sctp_intl_reasm_uo(struct sctp_ulpq *ulpq,
755 struct sctp_ulpevent *event)
757 struct sctp_ulpevent *retval = NULL;
758 struct sctp_stream_in *sin;
760 if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
761 event->msg_flags |= MSG_EOR;
765 sctp_intl_store_reasm_uo(ulpq, event);
767 sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
768 if (sin->pd_mode_uo && event->mid == sin->mid_uo &&
769 event->fsn == sin->fsn_uo)
770 retval = sctp_intl_retrieve_partial_uo(ulpq, event);
773 retval = sctp_intl_retrieve_reassembled_uo(ulpq, event);
778 static struct sctp_ulpevent *sctp_intl_retrieve_first_uo(struct sctp_ulpq *ulpq)
780 struct sctp_stream_in *csin, *sin = NULL;
781 struct sk_buff *first_frag = NULL;
782 struct sk_buff *last_frag = NULL;
783 struct sctp_ulpevent *retval;
788 skb_queue_walk(&ulpq->reasm_uo, pos) {
789 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
791 csin = sctp_stream_in(&ulpq->asoc->stream, cevent->stream);
792 if (csin->pd_mode_uo)
795 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
796 case SCTP_DATA_FIRST_FRAG:
803 sid = cevent->stream;
804 sin->mid_uo = cevent->mid;
806 case SCTP_DATA_MIDDLE_FRAG:
809 if (cevent->stream == sid &&
810 cevent->mid == sin->mid_uo &&
811 cevent->fsn == next_fsn) {
818 case SCTP_DATA_LAST_FRAG:
831 retval = sctp_make_reassembled_event(sock_net(ulpq->asoc->base.sk),
832 &ulpq->reasm_uo, first_frag,
835 sin->fsn_uo = next_fsn;
842 static int sctp_ulpevent_idata(struct sctp_ulpq *ulpq,
843 struct sctp_chunk *chunk, gfp_t gfp)
845 struct sctp_ulpevent *event;
846 struct sk_buff_head temp;
849 event = sctp_ulpevent_make_rcvmsg(chunk->asoc, chunk, gfp);
853 event->mid = ntohl(chunk->subh.idata_hdr->mid);
854 if (event->msg_flags & SCTP_DATA_FIRST_FRAG)
855 event->ppid = chunk->subh.idata_hdr->ppid;
857 event->fsn = ntohl(chunk->subh.idata_hdr->fsn);
859 if (!(event->msg_flags & SCTP_DATA_UNORDERED)) {
860 event = sctp_intl_reasm(ulpq, event);
861 if (event && event->msg_flags & MSG_EOR) {
862 skb_queue_head_init(&temp);
863 __skb_queue_tail(&temp, sctp_event2skb(event));
865 event = sctp_intl_order(ulpq, event);
868 event = sctp_intl_reasm_uo(ulpq, event);
872 event_eor = (event->msg_flags & MSG_EOR) ? 1 : 0;
873 sctp_enqueue_event(ulpq, event);
879 static struct sctp_ulpevent *sctp_intl_retrieve_first(struct sctp_ulpq *ulpq)
881 struct sctp_stream_in *csin, *sin = NULL;
882 struct sk_buff *first_frag = NULL;
883 struct sk_buff *last_frag = NULL;
884 struct sctp_ulpevent *retval;
889 skb_queue_walk(&ulpq->reasm, pos) {
890 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
892 csin = sctp_stream_in(&ulpq->asoc->stream, cevent->stream);
896 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
897 case SCTP_DATA_FIRST_FRAG:
900 if (cevent->mid == csin->mid) {
905 sid = cevent->stream;
908 case SCTP_DATA_MIDDLE_FRAG:
911 if (cevent->stream == sid &&
912 cevent->mid == sin->mid &&
913 cevent->fsn == next_fsn) {
920 case SCTP_DATA_LAST_FRAG:
933 retval = sctp_make_reassembled_event(sock_net(ulpq->asoc->base.sk),
934 &ulpq->reasm, first_frag,
944 static void sctp_intl_start_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
946 struct sctp_ulpevent *event;
948 if (!skb_queue_empty(&ulpq->reasm)) {
950 event = sctp_intl_retrieve_first(ulpq);
952 sctp_enqueue_event(ulpq, event);
956 if (!skb_queue_empty(&ulpq->reasm_uo)) {
958 event = sctp_intl_retrieve_first_uo(ulpq);
960 sctp_enqueue_event(ulpq, event);
965 static void sctp_renege_events(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
968 struct sctp_association *asoc = ulpq->asoc;
972 needed = ntohs(chunk->chunk_hdr->length) -
973 sizeof(struct sctp_idata_chunk);
975 if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
976 freed = sctp_ulpq_renege_list(ulpq, &ulpq->lobby, needed);
978 freed += sctp_ulpq_renege_list(ulpq, &ulpq->reasm,
981 freed += sctp_ulpq_renege_list(ulpq, &ulpq->reasm_uo,
985 if (freed >= needed && sctp_ulpevent_idata(ulpq, chunk, gfp) <= 0)
986 sctp_intl_start_pd(ulpq, gfp);
988 sk_mem_reclaim(asoc->base.sk);
991 static void sctp_intl_stream_abort_pd(struct sctp_ulpq *ulpq, __u16 sid,
992 __u32 mid, __u16 flags, gfp_t gfp)
994 struct sock *sk = ulpq->asoc->base.sk;
995 struct sctp_ulpevent *ev = NULL;
997 if (!sctp_ulpevent_type_enabled(ulpq->asoc->subscribe,
998 SCTP_PARTIAL_DELIVERY_EVENT))
1001 ev = sctp_ulpevent_make_pdapi(ulpq->asoc, SCTP_PARTIAL_DELIVERY_ABORTED,
1002 sid, mid, flags, gfp);
1004 struct sctp_sock *sp = sctp_sk(sk);
1006 __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
1008 if (!sp->data_ready_signalled) {
1009 sp->data_ready_signalled = 1;
1010 sk->sk_data_ready(sk);
1015 static void sctp_intl_reap_ordered(struct sctp_ulpq *ulpq, __u16 sid)
1017 struct sctp_stream *stream = &ulpq->asoc->stream;
1018 struct sctp_ulpevent *cevent, *event = NULL;
1019 struct sk_buff_head *lobby = &ulpq->lobby;
1020 struct sk_buff *pos, *tmp;
1021 struct sk_buff_head temp;
1025 skb_queue_head_init(&temp);
1026 sctp_skb_for_each(pos, lobby, tmp) {
1027 cevent = (struct sctp_ulpevent *)pos->cb;
1028 csid = cevent->stream;
1037 if (!MID_lt(cmid, sctp_mid_peek(stream, in, csid)))
1040 __skb_unlink(pos, lobby);
1042 event = sctp_skb2event(pos);
1044 __skb_queue_tail(&temp, pos);
1047 if (!event && pos != (struct sk_buff *)lobby) {
1048 cevent = (struct sctp_ulpevent *)pos->cb;
1049 csid = cevent->stream;
1052 if (csid == sid && cmid == sctp_mid_peek(stream, in, csid)) {
1053 sctp_mid_next(stream, in, csid);
1054 __skb_unlink(pos, lobby);
1055 __skb_queue_tail(&temp, pos);
1056 event = sctp_skb2event(pos);
1061 sctp_intl_retrieve_ordered(ulpq, event);
1062 sctp_enqueue_event(ulpq, event);
1066 static void sctp_intl_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
1068 struct sctp_stream *stream = &ulpq->asoc->stream;
1071 for (sid = 0; sid < stream->incnt; sid++) {
1072 struct sctp_stream_in *sin = SCTP_SI(stream, sid);
1075 if (sin->pd_mode_uo) {
1076 sin->pd_mode_uo = 0;
1079 sctp_intl_stream_abort_pd(ulpq, sid, mid, 0x1, gfp);
1086 sctp_intl_stream_abort_pd(ulpq, sid, mid, 0, gfp);
1087 sctp_mid_skip(stream, in, sid, mid);
1089 sctp_intl_reap_ordered(ulpq, sid);
1093 /* intl abort pd happens only when all data needs to be cleaned */
1094 sctp_ulpq_flush(ulpq);
1097 static inline int sctp_get_skip_pos(struct sctp_ifwdtsn_skip *skiplist,
1098 int nskips, __be16 stream, __u8 flags)
1102 for (i = 0; i < nskips; i++)
1103 if (skiplist[i].stream == stream &&
1104 skiplist[i].flags == flags)
1110 #define SCTP_FTSN_U_BIT 0x1
1111 static void sctp_generate_iftsn(struct sctp_outq *q, __u32 ctsn)
1113 struct sctp_ifwdtsn_skip ftsn_skip_arr[10];
1114 struct sctp_association *asoc = q->asoc;
1115 struct sctp_chunk *ftsn_chunk = NULL;
1116 struct list_head *lchunk, *temp;
1117 int nskips = 0, skip_pos;
1118 struct sctp_chunk *chunk;
1121 if (!asoc->peer.prsctp_capable)
1124 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1125 asoc->adv_peer_ack_point = ctsn;
1127 list_for_each_safe(lchunk, temp, &q->abandoned) {
1128 chunk = list_entry(lchunk, struct sctp_chunk, transmitted_list);
1129 tsn = ntohl(chunk->subh.data_hdr->tsn);
1131 if (TSN_lte(tsn, ctsn)) {
1132 list_del_init(lchunk);
1133 sctp_chunk_free(chunk);
1134 } else if (TSN_lte(tsn, asoc->adv_peer_ack_point + 1)) {
1135 __be16 sid = chunk->subh.idata_hdr->stream;
1136 __be32 mid = chunk->subh.idata_hdr->mid;
1139 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
1140 flags |= SCTP_FTSN_U_BIT;
1142 asoc->adv_peer_ack_point = tsn;
1143 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0], nskips,
1145 ftsn_skip_arr[skip_pos].stream = sid;
1146 ftsn_skip_arr[skip_pos].reserved = 0;
1147 ftsn_skip_arr[skip_pos].flags = flags;
1148 ftsn_skip_arr[skip_pos].mid = mid;
1149 if (skip_pos == nskips)
1158 if (asoc->adv_peer_ack_point > ctsn)
1159 ftsn_chunk = sctp_make_ifwdtsn(asoc, asoc->adv_peer_ack_point,
1160 nskips, &ftsn_skip_arr[0]);
1163 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1164 SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_OUTCTRLCHUNKS);
1168 #define _sctp_walk_ifwdtsn(pos, chunk, end) \
1169 for (pos = chunk->subh.ifwdtsn_hdr->skip; \
1170 (void *)pos < (void *)chunk->subh.ifwdtsn_hdr->skip + (end); pos++)
1172 #define sctp_walk_ifwdtsn(pos, ch) \
1173 _sctp_walk_ifwdtsn((pos), (ch), ntohs((ch)->chunk_hdr->length) - \
1174 sizeof(struct sctp_ifwdtsn_chunk))
1176 static bool sctp_validate_fwdtsn(struct sctp_chunk *chunk)
1178 struct sctp_fwdtsn_skip *skip;
1181 if (chunk->chunk_hdr->type != SCTP_CID_FWD_TSN)
1184 incnt = chunk->asoc->stream.incnt;
1185 sctp_walk_fwdtsn(skip, chunk)
1186 if (ntohs(skip->stream) >= incnt)
1192 static bool sctp_validate_iftsn(struct sctp_chunk *chunk)
1194 struct sctp_ifwdtsn_skip *skip;
1197 if (chunk->chunk_hdr->type != SCTP_CID_I_FWD_TSN)
1200 incnt = chunk->asoc->stream.incnt;
1201 sctp_walk_ifwdtsn(skip, chunk)
1202 if (ntohs(skip->stream) >= incnt)
1208 static void sctp_report_fwdtsn(struct sctp_ulpq *ulpq, __u32 ftsn)
1210 /* Move the Cumulattive TSN Ack ahead. */
1211 sctp_tsnmap_skip(&ulpq->asoc->peer.tsn_map, ftsn);
1212 /* purge the fragmentation queue */
1213 sctp_ulpq_reasm_flushtsn(ulpq, ftsn);
1214 /* Abort any in progress partial delivery. */
1215 sctp_ulpq_abort_pd(ulpq, GFP_ATOMIC);
1218 static void sctp_intl_reasm_flushtsn(struct sctp_ulpq *ulpq, __u32 ftsn)
1220 struct sk_buff *pos, *tmp;
1222 skb_queue_walk_safe(&ulpq->reasm, pos, tmp) {
1223 struct sctp_ulpevent *event = sctp_skb2event(pos);
1224 __u32 tsn = event->tsn;
1226 if (TSN_lte(tsn, ftsn)) {
1227 __skb_unlink(pos, &ulpq->reasm);
1228 sctp_ulpevent_free(event);
1232 skb_queue_walk_safe(&ulpq->reasm_uo, pos, tmp) {
1233 struct sctp_ulpevent *event = sctp_skb2event(pos);
1234 __u32 tsn = event->tsn;
1236 if (TSN_lte(tsn, ftsn)) {
1237 __skb_unlink(pos, &ulpq->reasm_uo);
1238 sctp_ulpevent_free(event);
1243 static void sctp_report_iftsn(struct sctp_ulpq *ulpq, __u32 ftsn)
1245 /* Move the Cumulattive TSN Ack ahead. */
1246 sctp_tsnmap_skip(&ulpq->asoc->peer.tsn_map, ftsn);
1247 /* purge the fragmentation queue */
1248 sctp_intl_reasm_flushtsn(ulpq, ftsn);
1249 /* abort only when it's for all data */
1250 if (ftsn == sctp_tsnmap_get_max_tsn_seen(&ulpq->asoc->peer.tsn_map))
1251 sctp_intl_abort_pd(ulpq, GFP_ATOMIC);
1254 static void sctp_handle_fwdtsn(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk)
1256 struct sctp_fwdtsn_skip *skip;
1258 /* Walk through all the skipped SSNs */
1259 sctp_walk_fwdtsn(skip, chunk)
1260 sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
1263 static void sctp_intl_skip(struct sctp_ulpq *ulpq, __u16 sid, __u32 mid,
1266 struct sctp_stream_in *sin = sctp_stream_in(&ulpq->asoc->stream, sid);
1267 struct sctp_stream *stream = &ulpq->asoc->stream;
1269 if (flags & SCTP_FTSN_U_BIT) {
1270 if (sin->pd_mode_uo && MID_lt(sin->mid_uo, mid)) {
1271 sin->pd_mode_uo = 0;
1272 sctp_intl_stream_abort_pd(ulpq, sid, mid, 0x1,
1278 if (MID_lt(mid, sctp_mid_peek(stream, in, sid)))
1283 sctp_intl_stream_abort_pd(ulpq, sid, mid, 0x0, GFP_ATOMIC);
1286 sctp_mid_skip(stream, in, sid, mid);
1288 sctp_intl_reap_ordered(ulpq, sid);
1291 static void sctp_handle_iftsn(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk)
1293 struct sctp_ifwdtsn_skip *skip;
1295 /* Walk through all the skipped MIDs and abort stream pd if possible */
1296 sctp_walk_ifwdtsn(skip, chunk)
1297 sctp_intl_skip(ulpq, ntohs(skip->stream),
1298 ntohl(skip->mid), skip->flags);
1301 static struct sctp_stream_interleave sctp_stream_interleave_0 = {
1302 .data_chunk_len = sizeof(struct sctp_data_chunk),
1303 .ftsn_chunk_len = sizeof(struct sctp_fwdtsn_chunk),
1304 /* DATA process functions */
1305 .make_datafrag = sctp_make_datafrag_empty,
1306 .assign_number = sctp_chunk_assign_ssn,
1307 .validate_data = sctp_validate_data,
1308 .ulpevent_data = sctp_ulpq_tail_data,
1309 .enqueue_event = sctp_ulpq_tail_event,
1310 .renege_events = sctp_ulpq_renege,
1311 .start_pd = sctp_ulpq_partial_delivery,
1312 .abort_pd = sctp_ulpq_abort_pd,
1313 /* FORWARD-TSN process functions */
1314 .generate_ftsn = sctp_generate_fwdtsn,
1315 .validate_ftsn = sctp_validate_fwdtsn,
1316 .report_ftsn = sctp_report_fwdtsn,
1317 .handle_ftsn = sctp_handle_fwdtsn,
1320 static struct sctp_stream_interleave sctp_stream_interleave_1 = {
1321 .data_chunk_len = sizeof(struct sctp_idata_chunk),
1322 .ftsn_chunk_len = sizeof(struct sctp_ifwdtsn_chunk),
1323 /* I-DATA process functions */
1324 .make_datafrag = sctp_make_idatafrag_empty,
1325 .assign_number = sctp_chunk_assign_mid,
1326 .validate_data = sctp_validate_idata,
1327 .ulpevent_data = sctp_ulpevent_idata,
1328 .enqueue_event = sctp_enqueue_event,
1329 .renege_events = sctp_renege_events,
1330 .start_pd = sctp_intl_start_pd,
1331 .abort_pd = sctp_intl_abort_pd,
1332 /* I-FORWARD-TSN process functions */
1333 .generate_ftsn = sctp_generate_iftsn,
1334 .validate_ftsn = sctp_validate_iftsn,
1335 .report_ftsn = sctp_report_iftsn,
1336 .handle_ftsn = sctp_handle_iftsn,
1339 void sctp_stream_interleave_init(struct sctp_stream *stream)
1341 struct sctp_association *asoc;
1343 asoc = container_of(stream, struct sctp_association, stream);
1344 stream->si = asoc->intl_enable ? &sctp_stream_interleave_1
1345 : &sctp_stream_interleave_0;