1 /* SCTP kernel implementation
2 * (C) Copyright Red Hat Inc. 2017
4 * This file is part of the SCTP kernel implementation
6 * These functions manipulate sctp stream queue/scheduling.
8 * This SCTP implementation is free software;
9 * you can redistribute it and/or modify it under the terms of
10 * the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This SCTP implementation is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * ************************
17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 * See the GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GNU CC; see the file COPYING. If not, see
22 * <http://www.gnu.org/licenses/>.
24 * Please send any bug reports or fixes you make to the
25 * email addresched(es):
26 * lksctp developers <linux-sctp@vger.kernel.org>
28 * Written or modified by:
29 * Xin Long <lucien.xin@gmail.com>
32 #include <net/busy_poll.h>
33 #include <net/sctp/sctp.h>
34 #include <net/sctp/sm.h>
35 #include <net/sctp/ulpevent.h>
36 #include <linux/sctp.h>
38 static struct sctp_chunk *sctp_make_idatafrag_empty(
39 const struct sctp_association *asoc,
40 const struct sctp_sndrcvinfo *sinfo,
41 int len, __u8 flags, gfp_t gfp)
43 struct sctp_chunk *retval;
44 struct sctp_idatahdr dp;
46 memset(&dp, 0, sizeof(dp));
47 dp.stream = htons(sinfo->sinfo_stream);
49 if (sinfo->sinfo_flags & SCTP_UNORDERED)
50 flags |= SCTP_DATA_UNORDERED;
52 retval = sctp_make_idata(asoc, flags, sizeof(dp) + len, gfp);
56 retval->subh.idata_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
57 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
62 static void sctp_chunk_assign_mid(struct sctp_chunk *chunk)
64 struct sctp_stream *stream;
65 struct sctp_chunk *lchunk;
72 sid = sctp_chunk_stream_no(chunk);
73 stream = &chunk->asoc->stream;
75 list_for_each_entry(lchunk, &chunk->msg->chunks, frag_list) {
76 struct sctp_idatahdr *hdr;
80 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
83 hdr = lchunk->subh.idata_hdr;
85 if (lchunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG)
86 hdr->ppid = lchunk->sinfo.sinfo_ppid;
88 hdr->fsn = htonl(cfsn++);
90 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
91 hdr->mid = htonl(sctp_mid_next(stream, out, sid));
93 hdr->mid = htonl(sctp_mid_peek(stream, out, sid));
97 static bool sctp_validate_data(struct sctp_chunk *chunk)
99 const struct sctp_stream *stream;
102 if (chunk->chunk_hdr->type != SCTP_CID_DATA)
105 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
108 stream = &chunk->asoc->stream;
109 sid = sctp_chunk_stream_no(chunk);
110 ssn = ntohs(chunk->subh.data_hdr->ssn);
112 return !SSN_lt(ssn, sctp_ssn_peek(stream, in, sid));
115 static bool sctp_validate_idata(struct sctp_chunk *chunk)
117 struct sctp_stream *stream;
121 if (chunk->chunk_hdr->type != SCTP_CID_I_DATA)
124 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
127 stream = &chunk->asoc->stream;
128 sid = sctp_chunk_stream_no(chunk);
129 mid = ntohl(chunk->subh.idata_hdr->mid);
131 return !MID_lt(mid, sctp_mid_peek(stream, in, sid));
134 static void sctp_intl_store_reasm(struct sctp_ulpq *ulpq,
135 struct sctp_ulpevent *event)
137 struct sctp_ulpevent *cevent;
140 pos = skb_peek_tail(&ulpq->reasm);
142 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
146 cevent = sctp_skb2event(pos);
148 if (event->stream == cevent->stream &&
149 event->mid == cevent->mid &&
150 (cevent->msg_flags & SCTP_DATA_FIRST_FRAG ||
151 (!(event->msg_flags & SCTP_DATA_FIRST_FRAG) &&
152 event->fsn > cevent->fsn))) {
153 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
157 if ((event->stream == cevent->stream &&
158 MID_lt(cevent->mid, event->mid)) ||
159 event->stream > cevent->stream) {
160 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
164 skb_queue_walk(&ulpq->reasm, pos) {
165 cevent = sctp_skb2event(pos);
167 if (event->stream < cevent->stream ||
168 (event->stream == cevent->stream &&
169 MID_lt(event->mid, cevent->mid)))
172 if (event->stream == cevent->stream &&
173 event->mid == cevent->mid &&
174 !(cevent->msg_flags & SCTP_DATA_FIRST_FRAG) &&
175 (event->msg_flags & SCTP_DATA_FIRST_FRAG ||
176 event->fsn < cevent->fsn))
180 __skb_queue_before(&ulpq->reasm, pos, sctp_event2skb(event));
183 static struct sctp_ulpevent *sctp_intl_retrieve_partial(
184 struct sctp_ulpq *ulpq,
185 struct sctp_ulpevent *event)
187 struct sk_buff *first_frag = NULL;
188 struct sk_buff *last_frag = NULL;
189 struct sctp_ulpevent *retval;
190 struct sctp_stream_in *sin;
195 sin = sctp_stream_in(ulpq->asoc, event->stream);
197 skb_queue_walk(&ulpq->reasm, pos) {
198 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
200 if (cevent->stream < event->stream)
203 if (cevent->stream > event->stream ||
204 cevent->mid != sin->mid)
207 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
208 case SCTP_DATA_FIRST_FRAG:
210 case SCTP_DATA_MIDDLE_FRAG:
212 if (cevent->fsn == sin->fsn) {
215 next_fsn = cevent->fsn + 1;
217 } else if (cevent->fsn == next_fsn) {
224 case SCTP_DATA_LAST_FRAG:
226 if (cevent->fsn == sin->fsn) {
232 } else if (cevent->fsn == next_fsn) {
247 retval = sctp_make_reassembled_event(sock_net(ulpq->asoc->base.sk),
248 &ulpq->reasm, first_frag,
253 retval->msg_flags |= MSG_EOR;
261 static struct sctp_ulpevent *sctp_intl_retrieve_reassembled(
262 struct sctp_ulpq *ulpq,
263 struct sctp_ulpevent *event)
265 struct sctp_association *asoc = ulpq->asoc;
266 struct sk_buff *pos, *first_frag = NULL;
267 struct sctp_ulpevent *retval = NULL;
268 struct sk_buff *pd_first = NULL;
269 struct sk_buff *pd_last = NULL;
270 struct sctp_stream_in *sin;
276 sin = sctp_stream_in(ulpq->asoc, event->stream);
278 skb_queue_walk(&ulpq->reasm, pos) {
279 struct sctp_ulpevent *cevent = sctp_skb2event(pos);
281 if (cevent->stream < event->stream)
283 if (cevent->stream > event->stream)
286 if (MID_lt(cevent->mid, event->mid))
288 if (MID_lt(event->mid, cevent->mid))
291 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
292 case SCTP_DATA_FIRST_FRAG:
293 if (cevent->mid == sin->mid) {
304 case SCTP_DATA_MIDDLE_FRAG:
305 if (first_frag && cevent->mid == mid &&
306 cevent->fsn == next_fsn) {
317 case SCTP_DATA_LAST_FRAG:
318 if (first_frag && cevent->mid == mid &&
319 cevent->fsn == next_fsn)
330 pd_point = sctp_sk(asoc->base.sk)->pd_point;
331 if (pd_point && pd_point <= pd_len) {
332 retval = sctp_make_reassembled_event(sock_net(asoc->base.sk),
343 retval = sctp_make_reassembled_event(sock_net(asoc->base.sk),
347 retval->msg_flags |= MSG_EOR;
353 static struct sctp_ulpevent *sctp_intl_reasm(struct sctp_ulpq *ulpq,
354 struct sctp_ulpevent *event)
356 struct sctp_ulpevent *retval = NULL;
357 struct sctp_stream_in *sin;
359 if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
360 event->msg_flags |= MSG_EOR;
364 sctp_intl_store_reasm(ulpq, event);
366 sin = sctp_stream_in(ulpq->asoc, event->stream);
367 if (sin->pd_mode && event->mid == sin->mid &&
368 event->fsn == sin->fsn)
369 retval = sctp_intl_retrieve_partial(ulpq, event);
372 retval = sctp_intl_retrieve_reassembled(ulpq, event);
377 static void sctp_intl_store_ordered(struct sctp_ulpq *ulpq,
378 struct sctp_ulpevent *event)
380 struct sctp_ulpevent *cevent;
383 pos = skb_peek_tail(&ulpq->lobby);
385 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
389 cevent = (struct sctp_ulpevent *)pos->cb;
390 if (event->stream == cevent->stream &&
391 MID_lt(cevent->mid, event->mid)) {
392 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
396 if (event->stream > cevent->stream) {
397 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
401 skb_queue_walk(&ulpq->lobby, pos) {
402 cevent = (struct sctp_ulpevent *)pos->cb;
404 if (cevent->stream > event->stream)
407 if (cevent->stream == event->stream &&
408 MID_lt(event->mid, cevent->mid))
412 __skb_queue_before(&ulpq->lobby, pos, sctp_event2skb(event));
415 static void sctp_intl_retrieve_ordered(struct sctp_ulpq *ulpq,
416 struct sctp_ulpevent *event)
418 struct sk_buff_head *event_list;
419 struct sctp_stream *stream;
420 struct sk_buff *pos, *tmp;
421 __u16 sid = event->stream;
423 stream = &ulpq->asoc->stream;
424 event_list = (struct sk_buff_head *)sctp_event2skb(event)->prev;
426 sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
427 struct sctp_ulpevent *cevent = (struct sctp_ulpevent *)pos->cb;
429 if (cevent->stream > sid)
432 if (cevent->stream < sid)
435 if (cevent->mid != sctp_mid_peek(stream, in, sid))
438 sctp_mid_next(stream, in, sid);
440 __skb_unlink(pos, &ulpq->lobby);
442 __skb_queue_tail(event_list, pos);
446 static struct sctp_ulpevent *sctp_intl_order(struct sctp_ulpq *ulpq,
447 struct sctp_ulpevent *event)
449 struct sctp_stream *stream;
452 if (event->msg_flags & SCTP_DATA_UNORDERED)
455 stream = &ulpq->asoc->stream;
458 if (event->mid != sctp_mid_peek(stream, in, sid)) {
459 sctp_intl_store_ordered(ulpq, event);
463 sctp_mid_next(stream, in, sid);
465 sctp_intl_retrieve_ordered(ulpq, event);
470 static int sctp_enqueue_event(struct sctp_ulpq *ulpq,
471 struct sctp_ulpevent *event)
473 struct sk_buff *skb = sctp_event2skb(event);
474 struct sock *sk = ulpq->asoc->base.sk;
475 struct sctp_sock *sp = sctp_sk(sk);
476 struct sk_buff_head *skb_list;
478 skb_list = (struct sk_buff_head *)skb->prev;
480 if (sk->sk_shutdown & RCV_SHUTDOWN &&
481 (sk->sk_shutdown & SEND_SHUTDOWN ||
482 !sctp_ulpevent_is_notification(event)))
485 if (!sctp_ulpevent_is_notification(event)) {
486 sk_mark_napi_id(sk, skb);
487 sk_incoming_cpu_update(sk);
490 if (!sctp_ulpevent_is_enabled(event, &sp->subscribe))
494 skb_queue_splice_tail_init(skb_list,
495 &sk->sk_receive_queue);
497 __skb_queue_tail(&sk->sk_receive_queue, skb);
499 if (!sp->data_ready_signalled) {
500 sp->data_ready_signalled = 1;
501 sk->sk_data_ready(sk);
508 sctp_queue_purge_ulpevents(skb_list);
510 sctp_ulpevent_free(event);
515 static int sctp_ulpevent_idata(struct sctp_ulpq *ulpq,
516 struct sctp_chunk *chunk, gfp_t gfp)
518 struct sctp_ulpevent *event;
519 struct sk_buff_head temp;
522 event = sctp_ulpevent_make_rcvmsg(chunk->asoc, chunk, gfp);
526 event->mid = ntohl(chunk->subh.idata_hdr->mid);
527 if (event->msg_flags & SCTP_DATA_FIRST_FRAG)
528 event->ppid = chunk->subh.idata_hdr->ppid;
530 event->fsn = ntohl(chunk->subh.idata_hdr->fsn);
532 event = sctp_intl_reasm(ulpq, event);
533 if (event && event->msg_flags & MSG_EOR) {
534 skb_queue_head_init(&temp);
535 __skb_queue_tail(&temp, sctp_event2skb(event));
537 event = sctp_intl_order(ulpq, event);
541 event_eor = (event->msg_flags & MSG_EOR) ? 1 : 0;
542 sctp_enqueue_event(ulpq, event);
548 static struct sctp_stream_interleave sctp_stream_interleave_0 = {
549 .data_chunk_len = sizeof(struct sctp_data_chunk),
550 /* DATA process functions */
551 .make_datafrag = sctp_make_datafrag_empty,
552 .assign_number = sctp_chunk_assign_ssn,
553 .validate_data = sctp_validate_data,
554 .ulpevent_data = sctp_ulpq_tail_data,
555 .enqueue_event = sctp_ulpq_tail_event,
558 static struct sctp_stream_interleave sctp_stream_interleave_1 = {
559 .data_chunk_len = sizeof(struct sctp_idata_chunk),
560 /* I-DATA process functions */
561 .make_datafrag = sctp_make_idatafrag_empty,
562 .assign_number = sctp_chunk_assign_mid,
563 .validate_data = sctp_validate_idata,
564 .ulpevent_data = sctp_ulpevent_idata,
565 .enqueue_event = sctp_enqueue_event,
568 void sctp_stream_interleave_init(struct sctp_stream *stream)
570 struct sctp_association *asoc;
572 asoc = container_of(stream, struct sctp_association, stream);
573 stream->si = asoc->intl_enable ? &sctp_stream_interleave_1
574 : &sctp_stream_interleave_0;