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):
27 * lksctp developers <linux-sctp@vger.kernel.org>
29 * Written or modified by:
30 * Xin Long <lucien.xin@gmail.com>
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 const 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
;
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
));
169 skb_queue_walk(&ulpq
->reasm
, pos
) {
170 cevent
= sctp_skb2event(pos
);
172 if (event
->stream
< cevent
->stream
||
173 (event
->stream
== cevent
->stream
&&
174 MID_lt(event
->mid
, cevent
->mid
)))
177 if (event
->stream
== cevent
->stream
&&
178 event
->mid
== cevent
->mid
&&
179 !(cevent
->msg_flags
& SCTP_DATA_FIRST_FRAG
) &&
180 (event
->msg_flags
& SCTP_DATA_FIRST_FRAG
||
181 event
->fsn
< cevent
->fsn
))
185 __skb_queue_before(&ulpq
->reasm
, pos
, sctp_event2skb(event
));
188 static struct sctp_ulpevent
*sctp_intl_retrieve_partial(
189 struct sctp_ulpq
*ulpq
,
190 struct sctp_ulpevent
*event
)
192 struct sk_buff
*first_frag
= NULL
;
193 struct sk_buff
*last_frag
= NULL
;
194 struct sctp_ulpevent
*retval
;
195 struct sctp_stream_in
*sin
;
200 sin
= sctp_stream_in(ulpq
->asoc
, event
->stream
);
202 skb_queue_walk(&ulpq
->reasm
, pos
) {
203 struct sctp_ulpevent
*cevent
= sctp_skb2event(pos
);
205 if (cevent
->stream
< event
->stream
)
208 if (cevent
->stream
> event
->stream
||
209 cevent
->mid
!= sin
->mid
)
212 switch (cevent
->msg_flags
& SCTP_DATA_FRAG_MASK
) {
213 case SCTP_DATA_FIRST_FRAG
:
215 case SCTP_DATA_MIDDLE_FRAG
:
217 if (cevent
->fsn
== sin
->fsn
) {
220 next_fsn
= cevent
->fsn
+ 1;
222 } else if (cevent
->fsn
== next_fsn
) {
229 case SCTP_DATA_LAST_FRAG
:
231 if (cevent
->fsn
== sin
->fsn
) {
237 } else if (cevent
->fsn
== next_fsn
) {
252 retval
= sctp_make_reassembled_event(sock_net(ulpq
->asoc
->base
.sk
),
253 &ulpq
->reasm
, first_frag
,
258 retval
->msg_flags
|= MSG_EOR
;
266 static struct sctp_ulpevent
*sctp_intl_retrieve_reassembled(
267 struct sctp_ulpq
*ulpq
,
268 struct sctp_ulpevent
*event
)
270 struct sctp_association
*asoc
= ulpq
->asoc
;
271 struct sk_buff
*pos
, *first_frag
= NULL
;
272 struct sctp_ulpevent
*retval
= NULL
;
273 struct sk_buff
*pd_first
= NULL
;
274 struct sk_buff
*pd_last
= NULL
;
275 struct sctp_stream_in
*sin
;
281 sin
= sctp_stream_in(ulpq
->asoc
, event
->stream
);
283 skb_queue_walk(&ulpq
->reasm
, pos
) {
284 struct sctp_ulpevent
*cevent
= sctp_skb2event(pos
);
286 if (cevent
->stream
< event
->stream
)
288 if (cevent
->stream
> event
->stream
)
291 if (MID_lt(cevent
->mid
, event
->mid
))
293 if (MID_lt(event
->mid
, cevent
->mid
))
296 switch (cevent
->msg_flags
& SCTP_DATA_FRAG_MASK
) {
297 case SCTP_DATA_FIRST_FRAG
:
298 if (cevent
->mid
== sin
->mid
) {
309 case SCTP_DATA_MIDDLE_FRAG
:
310 if (first_frag
&& cevent
->mid
== mid
&&
311 cevent
->fsn
== next_fsn
) {
322 case SCTP_DATA_LAST_FRAG
:
323 if (first_frag
&& cevent
->mid
== mid
&&
324 cevent
->fsn
== next_fsn
)
335 pd_point
= sctp_sk(asoc
->base
.sk
)->pd_point
;
336 if (pd_point
&& pd_point
<= pd_len
) {
337 retval
= sctp_make_reassembled_event(sock_net(asoc
->base
.sk
),
348 retval
= sctp_make_reassembled_event(sock_net(asoc
->base
.sk
),
352 retval
->msg_flags
|= MSG_EOR
;
358 static struct sctp_ulpevent
*sctp_intl_reasm(struct sctp_ulpq
*ulpq
,
359 struct sctp_ulpevent
*event
)
361 struct sctp_ulpevent
*retval
= NULL
;
362 struct sctp_stream_in
*sin
;
364 if (SCTP_DATA_NOT_FRAG
== (event
->msg_flags
& SCTP_DATA_FRAG_MASK
)) {
365 event
->msg_flags
|= MSG_EOR
;
369 sctp_intl_store_reasm(ulpq
, event
);
371 sin
= sctp_stream_in(ulpq
->asoc
, event
->stream
);
372 if (sin
->pd_mode
&& event
->mid
== sin
->mid
&&
373 event
->fsn
== sin
->fsn
)
374 retval
= sctp_intl_retrieve_partial(ulpq
, event
);
377 retval
= sctp_intl_retrieve_reassembled(ulpq
, event
);
382 static void sctp_intl_store_ordered(struct sctp_ulpq
*ulpq
,
383 struct sctp_ulpevent
*event
)
385 struct sctp_ulpevent
*cevent
;
388 pos
= skb_peek_tail(&ulpq
->lobby
);
390 __skb_queue_tail(&ulpq
->lobby
, sctp_event2skb(event
));
394 cevent
= (struct sctp_ulpevent
*)pos
->cb
;
395 if (event
->stream
== cevent
->stream
&&
396 MID_lt(cevent
->mid
, event
->mid
)) {
397 __skb_queue_tail(&ulpq
->lobby
, sctp_event2skb(event
));
401 if (event
->stream
> cevent
->stream
) {
402 __skb_queue_tail(&ulpq
->lobby
, sctp_event2skb(event
));
406 skb_queue_walk(&ulpq
->lobby
, pos
) {
407 cevent
= (struct sctp_ulpevent
*)pos
->cb
;
409 if (cevent
->stream
> event
->stream
)
412 if (cevent
->stream
== event
->stream
&&
413 MID_lt(event
->mid
, cevent
->mid
))
417 __skb_queue_before(&ulpq
->lobby
, pos
, sctp_event2skb(event
));
420 static void sctp_intl_retrieve_ordered(struct sctp_ulpq
*ulpq
,
421 struct sctp_ulpevent
*event
)
423 struct sk_buff_head
*event_list
;
424 struct sctp_stream
*stream
;
425 struct sk_buff
*pos
, *tmp
;
426 __u16 sid
= event
->stream
;
428 stream
= &ulpq
->asoc
->stream
;
429 event_list
= (struct sk_buff_head
*)sctp_event2skb(event
)->prev
;
431 sctp_skb_for_each(pos
, &ulpq
->lobby
, tmp
) {
432 struct sctp_ulpevent
*cevent
= (struct sctp_ulpevent
*)pos
->cb
;
434 if (cevent
->stream
> sid
)
437 if (cevent
->stream
< sid
)
440 if (cevent
->mid
!= sctp_mid_peek(stream
, in
, sid
))
443 sctp_mid_next(stream
, in
, sid
);
445 __skb_unlink(pos
, &ulpq
->lobby
);
447 __skb_queue_tail(event_list
, pos
);
451 static struct sctp_ulpevent
*sctp_intl_order(struct sctp_ulpq
*ulpq
,
452 struct sctp_ulpevent
*event
)
454 struct sctp_stream
*stream
;
457 stream
= &ulpq
->asoc
->stream
;
460 if (event
->mid
!= sctp_mid_peek(stream
, in
, sid
)) {
461 sctp_intl_store_ordered(ulpq
, event
);
465 sctp_mid_next(stream
, in
, sid
);
467 sctp_intl_retrieve_ordered(ulpq
, event
);
472 static int sctp_enqueue_event(struct sctp_ulpq
*ulpq
,
473 struct sctp_ulpevent
*event
)
475 struct sk_buff
*skb
= sctp_event2skb(event
);
476 struct sock
*sk
= ulpq
->asoc
->base
.sk
;
477 struct sctp_sock
*sp
= sctp_sk(sk
);
478 struct sk_buff_head
*skb_list
;
480 skb_list
= (struct sk_buff_head
*)skb
->prev
;
482 if (sk
->sk_shutdown
& RCV_SHUTDOWN
&&
483 (sk
->sk_shutdown
& SEND_SHUTDOWN
||
484 !sctp_ulpevent_is_notification(event
)))
487 if (!sctp_ulpevent_is_notification(event
)) {
488 sk_mark_napi_id(sk
, skb
);
489 sk_incoming_cpu_update(sk
);
492 if (!sctp_ulpevent_is_enabled(event
, &sp
->subscribe
))
496 skb_queue_splice_tail_init(skb_list
,
497 &sk
->sk_receive_queue
);
499 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
501 if (!sp
->data_ready_signalled
) {
502 sp
->data_ready_signalled
= 1;
503 sk
->sk_data_ready(sk
);
510 sctp_queue_purge_ulpevents(skb_list
);
512 sctp_ulpevent_free(event
);
517 static void sctp_intl_store_reasm_uo(struct sctp_ulpq
*ulpq
,
518 struct sctp_ulpevent
*event
)
520 struct sctp_ulpevent
*cevent
;
523 pos
= skb_peek_tail(&ulpq
->reasm_uo
);
525 __skb_queue_tail(&ulpq
->reasm_uo
, sctp_event2skb(event
));
529 cevent
= sctp_skb2event(pos
);
531 if (event
->stream
== cevent
->stream
&&
532 event
->mid
== cevent
->mid
&&
533 (cevent
->msg_flags
& SCTP_DATA_FIRST_FRAG
||
534 (!(event
->msg_flags
& SCTP_DATA_FIRST_FRAG
) &&
535 event
->fsn
> cevent
->fsn
))) {
536 __skb_queue_tail(&ulpq
->reasm_uo
, sctp_event2skb(event
));
540 if ((event
->stream
== cevent
->stream
&&
541 MID_lt(cevent
->mid
, event
->mid
)) ||
542 event
->stream
> cevent
->stream
) {
543 __skb_queue_tail(&ulpq
->reasm_uo
, sctp_event2skb(event
));
547 skb_queue_walk(&ulpq
->reasm_uo
, pos
) {
548 cevent
= sctp_skb2event(pos
);
550 if (event
->stream
< cevent
->stream
||
551 (event
->stream
== cevent
->stream
&&
552 MID_lt(event
->mid
, cevent
->mid
)))
555 if (event
->stream
== cevent
->stream
&&
556 event
->mid
== cevent
->mid
&&
557 !(cevent
->msg_flags
& SCTP_DATA_FIRST_FRAG
) &&
558 (event
->msg_flags
& SCTP_DATA_FIRST_FRAG
||
559 event
->fsn
< cevent
->fsn
))
563 __skb_queue_before(&ulpq
->reasm_uo
, pos
, sctp_event2skb(event
));
566 static struct sctp_ulpevent
*sctp_intl_retrieve_partial_uo(
567 struct sctp_ulpq
*ulpq
,
568 struct sctp_ulpevent
*event
)
570 struct sk_buff
*first_frag
= NULL
;
571 struct sk_buff
*last_frag
= NULL
;
572 struct sctp_ulpevent
*retval
;
573 struct sctp_stream_in
*sin
;
578 sin
= sctp_stream_in(ulpq
->asoc
, event
->stream
);
580 skb_queue_walk(&ulpq
->reasm_uo
, pos
) {
581 struct sctp_ulpevent
*cevent
= sctp_skb2event(pos
);
583 if (cevent
->stream
< event
->stream
)
585 if (cevent
->stream
> event
->stream
)
588 if (MID_lt(cevent
->mid
, sin
->mid_uo
))
590 if (MID_lt(sin
->mid_uo
, cevent
->mid
))
593 switch (cevent
->msg_flags
& SCTP_DATA_FRAG_MASK
) {
594 case SCTP_DATA_FIRST_FRAG
:
596 case SCTP_DATA_MIDDLE_FRAG
:
598 if (cevent
->fsn
== sin
->fsn_uo
) {
601 next_fsn
= cevent
->fsn
+ 1;
603 } else if (cevent
->fsn
== next_fsn
) {
610 case SCTP_DATA_LAST_FRAG
:
612 if (cevent
->fsn
== sin
->fsn_uo
) {
618 } else if (cevent
->fsn
== next_fsn
) {
633 retval
= sctp_make_reassembled_event(sock_net(ulpq
->asoc
->base
.sk
),
634 &ulpq
->reasm_uo
, first_frag
,
637 sin
->fsn_uo
= next_fsn
;
639 retval
->msg_flags
|= MSG_EOR
;
647 static struct sctp_ulpevent
*sctp_intl_retrieve_reassembled_uo(
648 struct sctp_ulpq
*ulpq
,
649 struct sctp_ulpevent
*event
)
651 struct sctp_association
*asoc
= ulpq
->asoc
;
652 struct sk_buff
*pos
, *first_frag
= NULL
;
653 struct sctp_ulpevent
*retval
= NULL
;
654 struct sk_buff
*pd_first
= NULL
;
655 struct sk_buff
*pd_last
= NULL
;
656 struct sctp_stream_in
*sin
;
662 sin
= sctp_stream_in(ulpq
->asoc
, event
->stream
);
664 skb_queue_walk(&ulpq
->reasm_uo
, pos
) {
665 struct sctp_ulpevent
*cevent
= sctp_skb2event(pos
);
667 if (cevent
->stream
< event
->stream
)
669 if (cevent
->stream
> event
->stream
)
672 if (MID_lt(cevent
->mid
, event
->mid
))
674 if (MID_lt(event
->mid
, cevent
->mid
))
677 switch (cevent
->msg_flags
& SCTP_DATA_FRAG_MASK
) {
678 case SCTP_DATA_FIRST_FRAG
:
679 if (!sin
->pd_mode_uo
) {
680 sin
->mid_uo
= cevent
->mid
;
691 case SCTP_DATA_MIDDLE_FRAG
:
692 if (first_frag
&& cevent
->mid
== mid
&&
693 cevent
->fsn
== next_fsn
) {
704 case SCTP_DATA_LAST_FRAG
:
705 if (first_frag
&& cevent
->mid
== mid
&&
706 cevent
->fsn
== next_fsn
)
717 pd_point
= sctp_sk(asoc
->base
.sk
)->pd_point
;
718 if (pd_point
&& pd_point
<= pd_len
) {
719 retval
= sctp_make_reassembled_event(sock_net(asoc
->base
.sk
),
723 sin
->fsn_uo
= next_fsn
;
730 retval
= sctp_make_reassembled_event(sock_net(asoc
->base
.sk
),
734 retval
->msg_flags
|= MSG_EOR
;
740 static struct sctp_ulpevent
*sctp_intl_reasm_uo(struct sctp_ulpq
*ulpq
,
741 struct sctp_ulpevent
*event
)
743 struct sctp_ulpevent
*retval
= NULL
;
744 struct sctp_stream_in
*sin
;
746 if (SCTP_DATA_NOT_FRAG
== (event
->msg_flags
& SCTP_DATA_FRAG_MASK
)) {
747 event
->msg_flags
|= MSG_EOR
;
751 sctp_intl_store_reasm_uo(ulpq
, event
);
753 sin
= sctp_stream_in(ulpq
->asoc
, event
->stream
);
754 if (sin
->pd_mode_uo
&& event
->mid
== sin
->mid_uo
&&
755 event
->fsn
== sin
->fsn_uo
)
756 retval
= sctp_intl_retrieve_partial_uo(ulpq
, event
);
759 retval
= sctp_intl_retrieve_reassembled_uo(ulpq
, event
);
764 static struct sctp_ulpevent
*sctp_intl_retrieve_first_uo(struct sctp_ulpq
*ulpq
)
766 struct sctp_stream_in
*csin
, *sin
= NULL
;
767 struct sk_buff
*first_frag
= NULL
;
768 struct sk_buff
*last_frag
= NULL
;
769 struct sctp_ulpevent
*retval
;
774 skb_queue_walk(&ulpq
->reasm_uo
, pos
) {
775 struct sctp_ulpevent
*cevent
= sctp_skb2event(pos
);
777 csin
= sctp_stream_in(ulpq
->asoc
, cevent
->stream
);
778 if (csin
->pd_mode_uo
)
781 switch (cevent
->msg_flags
& SCTP_DATA_FRAG_MASK
) {
782 case SCTP_DATA_FIRST_FRAG
:
789 sid
= cevent
->stream
;
790 sin
->mid_uo
= cevent
->mid
;
792 case SCTP_DATA_MIDDLE_FRAG
:
795 if (cevent
->stream
== sid
&&
796 cevent
->mid
== sin
->mid_uo
&&
797 cevent
->fsn
== next_fsn
) {
804 case SCTP_DATA_LAST_FRAG
:
817 retval
= sctp_make_reassembled_event(sock_net(ulpq
->asoc
->base
.sk
),
818 &ulpq
->reasm_uo
, first_frag
,
821 sin
->fsn_uo
= next_fsn
;
828 static int sctp_ulpevent_idata(struct sctp_ulpq
*ulpq
,
829 struct sctp_chunk
*chunk
, gfp_t gfp
)
831 struct sctp_ulpevent
*event
;
832 struct sk_buff_head temp
;
835 event
= sctp_ulpevent_make_rcvmsg(chunk
->asoc
, chunk
, gfp
);
839 event
->mid
= ntohl(chunk
->subh
.idata_hdr
->mid
);
840 if (event
->msg_flags
& SCTP_DATA_FIRST_FRAG
)
841 event
->ppid
= chunk
->subh
.idata_hdr
->ppid
;
843 event
->fsn
= ntohl(chunk
->subh
.idata_hdr
->fsn
);
845 if (!(event
->msg_flags
& SCTP_DATA_UNORDERED
)) {
846 event
= sctp_intl_reasm(ulpq
, event
);
847 if (event
&& event
->msg_flags
& MSG_EOR
) {
848 skb_queue_head_init(&temp
);
849 __skb_queue_tail(&temp
, sctp_event2skb(event
));
851 event
= sctp_intl_order(ulpq
, event
);
854 event
= sctp_intl_reasm_uo(ulpq
, event
);
858 event_eor
= (event
->msg_flags
& MSG_EOR
) ? 1 : 0;
859 sctp_enqueue_event(ulpq
, event
);
865 static struct sctp_ulpevent
*sctp_intl_retrieve_first(struct sctp_ulpq
*ulpq
)
867 struct sctp_stream_in
*csin
, *sin
= NULL
;
868 struct sk_buff
*first_frag
= NULL
;
869 struct sk_buff
*last_frag
= NULL
;
870 struct sctp_ulpevent
*retval
;
875 skb_queue_walk(&ulpq
->reasm
, pos
) {
876 struct sctp_ulpevent
*cevent
= sctp_skb2event(pos
);
878 csin
= sctp_stream_in(ulpq
->asoc
, cevent
->stream
);
882 switch (cevent
->msg_flags
& SCTP_DATA_FRAG_MASK
) {
883 case SCTP_DATA_FIRST_FRAG
:
886 if (cevent
->mid
== csin
->mid
) {
891 sid
= cevent
->stream
;
894 case SCTP_DATA_MIDDLE_FRAG
:
897 if (cevent
->stream
== sid
&&
898 cevent
->mid
== sin
->mid
&&
899 cevent
->fsn
== next_fsn
) {
906 case SCTP_DATA_LAST_FRAG
:
919 retval
= sctp_make_reassembled_event(sock_net(ulpq
->asoc
->base
.sk
),
920 &ulpq
->reasm
, first_frag
,
930 static void sctp_intl_start_pd(struct sctp_ulpq
*ulpq
, gfp_t gfp
)
932 struct sctp_ulpevent
*event
;
934 if (!skb_queue_empty(&ulpq
->reasm
)) {
936 event
= sctp_intl_retrieve_first(ulpq
);
938 sctp_enqueue_event(ulpq
, event
);
942 if (!skb_queue_empty(&ulpq
->reasm_uo
)) {
944 event
= sctp_intl_retrieve_first_uo(ulpq
);
946 sctp_enqueue_event(ulpq
, event
);
951 static void sctp_renege_events(struct sctp_ulpq
*ulpq
, struct sctp_chunk
*chunk
,
954 struct sctp_association
*asoc
= ulpq
->asoc
;
958 needed
= ntohs(chunk
->chunk_hdr
->length
) -
959 sizeof(struct sctp_idata_chunk
);
961 if (skb_queue_empty(&asoc
->base
.sk
->sk_receive_queue
)) {
962 freed
= sctp_ulpq_renege_list(ulpq
, &ulpq
->lobby
, needed
);
964 freed
+= sctp_ulpq_renege_list(ulpq
, &ulpq
->reasm
,
967 freed
+= sctp_ulpq_renege_list(ulpq
, &ulpq
->reasm_uo
,
971 if (freed
>= needed
&& sctp_ulpevent_idata(ulpq
, chunk
, gfp
) <= 0)
972 sctp_intl_start_pd(ulpq
, gfp
);
974 sk_mem_reclaim(asoc
->base
.sk
);
977 static void sctp_intl_stream_abort_pd(struct sctp_ulpq
*ulpq
, __u16 sid
,
978 __u32 mid
, __u16 flags
, gfp_t gfp
)
980 struct sock
*sk
= ulpq
->asoc
->base
.sk
;
981 struct sctp_ulpevent
*ev
= NULL
;
983 if (!sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT
,
984 &sctp_sk(sk
)->subscribe
))
987 ev
= sctp_ulpevent_make_pdapi(ulpq
->asoc
, SCTP_PARTIAL_DELIVERY_ABORTED
,
988 sid
, mid
, flags
, gfp
);
990 __skb_queue_tail(&sk
->sk_receive_queue
, sctp_event2skb(ev
));
992 if (!sctp_sk(sk
)->data_ready_signalled
) {
993 sctp_sk(sk
)->data_ready_signalled
= 1;
994 sk
->sk_data_ready(sk
);
999 static void sctp_intl_reap_ordered(struct sctp_ulpq
*ulpq
, __u16 sid
)
1001 struct sctp_stream
*stream
= &ulpq
->asoc
->stream
;
1002 struct sctp_ulpevent
*cevent
, *event
= NULL
;
1003 struct sk_buff_head
*lobby
= &ulpq
->lobby
;
1004 struct sk_buff
*pos
, *tmp
;
1005 struct sk_buff_head temp
;
1009 skb_queue_head_init(&temp
);
1010 sctp_skb_for_each(pos
, lobby
, tmp
) {
1011 cevent
= (struct sctp_ulpevent
*)pos
->cb
;
1012 csid
= cevent
->stream
;
1021 if (!MID_lt(cmid
, sctp_mid_peek(stream
, in
, csid
)))
1024 __skb_unlink(pos
, lobby
);
1026 event
= sctp_skb2event(pos
);
1028 __skb_queue_tail(&temp
, pos
);
1031 if (!event
&& pos
!= (struct sk_buff
*)lobby
) {
1032 cevent
= (struct sctp_ulpevent
*)pos
->cb
;
1033 csid
= cevent
->stream
;
1036 if (csid
== sid
&& cmid
== sctp_mid_peek(stream
, in
, csid
)) {
1037 sctp_mid_next(stream
, in
, csid
);
1038 __skb_unlink(pos
, lobby
);
1039 __skb_queue_tail(&temp
, pos
);
1040 event
= sctp_skb2event(pos
);
1045 sctp_intl_retrieve_ordered(ulpq
, event
);
1046 sctp_enqueue_event(ulpq
, event
);
1050 static void sctp_intl_abort_pd(struct sctp_ulpq
*ulpq
, gfp_t gfp
)
1052 struct sctp_stream
*stream
= &ulpq
->asoc
->stream
;
1055 for (sid
= 0; sid
< stream
->incnt
; sid
++) {
1056 struct sctp_stream_in
*sin
= &stream
->in
[sid
];
1059 if (sin
->pd_mode_uo
) {
1060 sin
->pd_mode_uo
= 0;
1063 sctp_intl_stream_abort_pd(ulpq
, sid
, mid
, 0x1, gfp
);
1070 sctp_intl_stream_abort_pd(ulpq
, sid
, mid
, 0, gfp
);
1071 sctp_mid_skip(stream
, in
, sid
, mid
);
1073 sctp_intl_reap_ordered(ulpq
, sid
);
1077 /* intl abort pd happens only when all data needs to be cleaned */
1078 sctp_ulpq_flush(ulpq
);
1081 static inline int sctp_get_skip_pos(struct sctp_ifwdtsn_skip
*skiplist
,
1082 int nskips
, __be16 stream
, __u8 flags
)
1086 for (i
= 0; i
< nskips
; i
++)
1087 if (skiplist
[i
].stream
== stream
&&
1088 skiplist
[i
].flags
== flags
)
1094 #define SCTP_FTSN_U_BIT 0x1
1095 static void sctp_generate_iftsn(struct sctp_outq
*q
, __u32 ctsn
)
1097 struct sctp_ifwdtsn_skip ftsn_skip_arr
[10];
1098 struct sctp_association
*asoc
= q
->asoc
;
1099 struct sctp_chunk
*ftsn_chunk
= NULL
;
1100 struct list_head
*lchunk
, *temp
;
1101 int nskips
= 0, skip_pos
;
1102 struct sctp_chunk
*chunk
;
1105 if (!asoc
->peer
.prsctp_capable
)
1108 if (TSN_lt(asoc
->adv_peer_ack_point
, ctsn
))
1109 asoc
->adv_peer_ack_point
= ctsn
;
1111 list_for_each_safe(lchunk
, temp
, &q
->abandoned
) {
1112 chunk
= list_entry(lchunk
, struct sctp_chunk
, transmitted_list
);
1113 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1115 if (TSN_lte(tsn
, ctsn
)) {
1116 list_del_init(lchunk
);
1117 sctp_chunk_free(chunk
);
1118 } else if (TSN_lte(tsn
, asoc
->adv_peer_ack_point
+ 1)) {
1119 __be16 sid
= chunk
->subh
.idata_hdr
->stream
;
1120 __be32 mid
= chunk
->subh
.idata_hdr
->mid
;
1123 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
1124 flags
|= SCTP_FTSN_U_BIT
;
1126 asoc
->adv_peer_ack_point
= tsn
;
1127 skip_pos
= sctp_get_skip_pos(&ftsn_skip_arr
[0], nskips
,
1129 ftsn_skip_arr
[skip_pos
].stream
= sid
;
1130 ftsn_skip_arr
[skip_pos
].reserved
= 0;
1131 ftsn_skip_arr
[skip_pos
].flags
= flags
;
1132 ftsn_skip_arr
[skip_pos
].mid
= mid
;
1133 if (skip_pos
== nskips
)
1142 if (asoc
->adv_peer_ack_point
> ctsn
)
1143 ftsn_chunk
= sctp_make_ifwdtsn(asoc
, asoc
->adv_peer_ack_point
,
1144 nskips
, &ftsn_skip_arr
[0]);
1147 list_add_tail(&ftsn_chunk
->list
, &q
->control_chunk_list
);
1148 SCTP_INC_STATS(sock_net(asoc
->base
.sk
), SCTP_MIB_OUTCTRLCHUNKS
);
1152 #define _sctp_walk_ifwdtsn(pos, chunk, end) \
1153 for (pos = chunk->subh.ifwdtsn_hdr->skip; \
1154 (void *)pos < (void *)chunk->subh.ifwdtsn_hdr->skip + (end); pos++)
1156 #define sctp_walk_ifwdtsn(pos, ch) \
1157 _sctp_walk_ifwdtsn((pos), (ch), ntohs((ch)->chunk_hdr->length) - \
1158 sizeof(struct sctp_ifwdtsn_chunk))
1160 static bool sctp_validate_fwdtsn(struct sctp_chunk
*chunk
)
1162 struct sctp_fwdtsn_skip
*skip
;
1165 if (chunk
->chunk_hdr
->type
!= SCTP_CID_FWD_TSN
)
1168 incnt
= chunk
->asoc
->stream
.incnt
;
1169 sctp_walk_fwdtsn(skip
, chunk
)
1170 if (ntohs(skip
->stream
) >= incnt
)
1176 static bool sctp_validate_iftsn(struct sctp_chunk
*chunk
)
1178 struct sctp_ifwdtsn_skip
*skip
;
1181 if (chunk
->chunk_hdr
->type
!= SCTP_CID_I_FWD_TSN
)
1184 incnt
= chunk
->asoc
->stream
.incnt
;
1185 sctp_walk_ifwdtsn(skip
, chunk
)
1186 if (ntohs(skip
->stream
) >= incnt
)
1192 static void sctp_report_fwdtsn(struct sctp_ulpq
*ulpq
, __u32 ftsn
)
1194 /* Move the Cumulattive TSN Ack ahead. */
1195 sctp_tsnmap_skip(&ulpq
->asoc
->peer
.tsn_map
, ftsn
);
1196 /* purge the fragmentation queue */
1197 sctp_ulpq_reasm_flushtsn(ulpq
, ftsn
);
1198 /* Abort any in progress partial delivery. */
1199 sctp_ulpq_abort_pd(ulpq
, GFP_ATOMIC
);
1202 static void sctp_intl_reasm_flushtsn(struct sctp_ulpq
*ulpq
, __u32 ftsn
)
1204 struct sk_buff
*pos
, *tmp
;
1206 skb_queue_walk_safe(&ulpq
->reasm
, pos
, tmp
) {
1207 struct sctp_ulpevent
*event
= sctp_skb2event(pos
);
1208 __u32 tsn
= event
->tsn
;
1210 if (TSN_lte(tsn
, ftsn
)) {
1211 __skb_unlink(pos
, &ulpq
->reasm
);
1212 sctp_ulpevent_free(event
);
1216 skb_queue_walk_safe(&ulpq
->reasm_uo
, pos
, tmp
) {
1217 struct sctp_ulpevent
*event
= sctp_skb2event(pos
);
1218 __u32 tsn
= event
->tsn
;
1220 if (TSN_lte(tsn
, ftsn
)) {
1221 __skb_unlink(pos
, &ulpq
->reasm_uo
);
1222 sctp_ulpevent_free(event
);
1227 static void sctp_report_iftsn(struct sctp_ulpq
*ulpq
, __u32 ftsn
)
1229 /* Move the Cumulattive TSN Ack ahead. */
1230 sctp_tsnmap_skip(&ulpq
->asoc
->peer
.tsn_map
, ftsn
);
1231 /* purge the fragmentation queue */
1232 sctp_intl_reasm_flushtsn(ulpq
, ftsn
);
1233 /* abort only when it's for all data */
1234 if (ftsn
== sctp_tsnmap_get_max_tsn_seen(&ulpq
->asoc
->peer
.tsn_map
))
1235 sctp_intl_abort_pd(ulpq
, GFP_ATOMIC
);
1238 static void sctp_handle_fwdtsn(struct sctp_ulpq
*ulpq
, struct sctp_chunk
*chunk
)
1240 struct sctp_fwdtsn_skip
*skip
;
1242 /* Walk through all the skipped SSNs */
1243 sctp_walk_fwdtsn(skip
, chunk
)
1244 sctp_ulpq_skip(ulpq
, ntohs(skip
->stream
), ntohs(skip
->ssn
));
1247 static void sctp_intl_skip(struct sctp_ulpq
*ulpq
, __u16 sid
, __u32 mid
,
1250 struct sctp_stream_in
*sin
= sctp_stream_in(ulpq
->asoc
, sid
);
1251 struct sctp_stream
*stream
= &ulpq
->asoc
->stream
;
1253 if (flags
& SCTP_FTSN_U_BIT
) {
1254 if (sin
->pd_mode_uo
&& MID_lt(sin
->mid_uo
, mid
)) {
1255 sin
->pd_mode_uo
= 0;
1256 sctp_intl_stream_abort_pd(ulpq
, sid
, mid
, 0x1,
1262 if (MID_lt(mid
, sctp_mid_peek(stream
, in
, sid
)))
1267 sctp_intl_stream_abort_pd(ulpq
, sid
, mid
, 0x0, GFP_ATOMIC
);
1270 sctp_mid_skip(stream
, in
, sid
, mid
);
1272 sctp_intl_reap_ordered(ulpq
, sid
);
1275 static void sctp_handle_iftsn(struct sctp_ulpq
*ulpq
, struct sctp_chunk
*chunk
)
1277 struct sctp_ifwdtsn_skip
*skip
;
1279 /* Walk through all the skipped MIDs and abort stream pd if possible */
1280 sctp_walk_ifwdtsn(skip
, chunk
)
1281 sctp_intl_skip(ulpq
, ntohs(skip
->stream
),
1282 ntohl(skip
->mid
), skip
->flags
);
1285 static struct sctp_stream_interleave sctp_stream_interleave_0
= {
1286 .data_chunk_len
= sizeof(struct sctp_data_chunk
),
1287 .ftsn_chunk_len
= sizeof(struct sctp_fwdtsn_chunk
),
1288 /* DATA process functions */
1289 .make_datafrag
= sctp_make_datafrag_empty
,
1290 .assign_number
= sctp_chunk_assign_ssn
,
1291 .validate_data
= sctp_validate_data
,
1292 .ulpevent_data
= sctp_ulpq_tail_data
,
1293 .enqueue_event
= sctp_ulpq_tail_event
,
1294 .renege_events
= sctp_ulpq_renege
,
1295 .start_pd
= sctp_ulpq_partial_delivery
,
1296 .abort_pd
= sctp_ulpq_abort_pd
,
1297 /* FORWARD-TSN process functions */
1298 .generate_ftsn
= sctp_generate_fwdtsn
,
1299 .validate_ftsn
= sctp_validate_fwdtsn
,
1300 .report_ftsn
= sctp_report_fwdtsn
,
1301 .handle_ftsn
= sctp_handle_fwdtsn
,
1304 static struct sctp_stream_interleave sctp_stream_interleave_1
= {
1305 .data_chunk_len
= sizeof(struct sctp_idata_chunk
),
1306 .ftsn_chunk_len
= sizeof(struct sctp_ifwdtsn_chunk
),
1307 /* I-DATA process functions */
1308 .make_datafrag
= sctp_make_idatafrag_empty
,
1309 .assign_number
= sctp_chunk_assign_mid
,
1310 .validate_data
= sctp_validate_idata
,
1311 .ulpevent_data
= sctp_ulpevent_idata
,
1312 .enqueue_event
= sctp_enqueue_event
,
1313 .renege_events
= sctp_renege_events
,
1314 .start_pd
= sctp_intl_start_pd
,
1315 .abort_pd
= sctp_intl_abort_pd
,
1316 /* I-FORWARD-TSN process functions */
1317 .generate_ftsn
= sctp_generate_iftsn
,
1318 .validate_ftsn
= sctp_validate_iftsn
,
1319 .report_ftsn
= sctp_report_iftsn
,
1320 .handle_ftsn
= sctp_handle_iftsn
,
1323 void sctp_stream_interleave_init(struct sctp_stream
*stream
)
1325 struct sctp_association
*asoc
;
1327 asoc
= container_of(stream
, struct sctp_association
, stream
);
1328 stream
->si
= asoc
->intl_enable
? &sctp_stream_interleave_1
1329 : &sctp_stream_interleave_0
;