1 /* SCTP kernel reference 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 * Copyright (c) 2001 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This abstraction carries sctp events to the ULP (sockets).
11 * The SCTP reference 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)
17 * The SCTP reference 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.
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING. If not, write to
25 * the Free Software Foundation, 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
35 * Written or modified by:
36 * Jon Grimm <jgrimm@us.ibm.com>
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Sridhar Samudrala <sri@us.ibm.com>
40 * Any bugs reported given to us we will try to fix... any fixes shared will
41 * be incorporated into the next SCTP release.
44 #include <linux/types.h>
45 #include <linux/skbuff.h>
47 #include <net/sctp/structs.h>
48 #include <net/sctp/sctp.h>
49 #include <net/sctp/sm.h>
51 /* Forward declarations for internal helpers. */
52 static struct sctp_ulpevent * sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
53 struct sctp_ulpevent *);
54 static struct sctp_ulpevent * sctp_ulpq_order(struct sctp_ulpq *,
55 struct sctp_ulpevent *);
57 /* 1st Level Abstractions */
59 /* Initialize a ULP queue from a block of memory. */
60 struct sctp_ulpq *sctp_ulpq_init(struct sctp_ulpq *ulpq,
61 struct sctp_association *asoc)
63 memset(ulpq, 0, sizeof(struct sctp_ulpq));
66 skb_queue_head_init(&ulpq->reasm);
67 skb_queue_head_init(&ulpq->lobby);
75 /* Flush the reassembly and ordering queues. */
76 static void sctp_ulpq_flush(struct sctp_ulpq *ulpq)
79 struct sctp_ulpevent *event;
81 while ((skb = __skb_dequeue(&ulpq->lobby)) != NULL) {
82 event = sctp_skb2event(skb);
83 sctp_ulpevent_free(event);
86 while ((skb = __skb_dequeue(&ulpq->reasm)) != NULL) {
87 event = sctp_skb2event(skb);
88 sctp_ulpevent_free(event);
93 /* Dispose of a ulpqueue. */
94 void sctp_ulpq_free(struct sctp_ulpq *ulpq)
96 sctp_ulpq_flush(ulpq);
101 /* Process an incoming DATA chunk. */
102 int sctp_ulpq_tail_data(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
105 struct sk_buff_head temp;
106 sctp_data_chunk_t *hdr;
107 struct sctp_ulpevent *event;
109 hdr = (sctp_data_chunk_t *) chunk->chunk_hdr;
111 /* Create an event from the incoming chunk. */
112 event = sctp_ulpevent_make_rcvmsg(chunk->asoc, chunk, gfp);
116 /* Do reassembly if needed. */
117 event = sctp_ulpq_reasm(ulpq, event);
119 /* Do ordering if needed. */
120 if ((event) && (event->msg_flags & MSG_EOR)){
121 /* Create a temporary list to collect chunks on. */
122 skb_queue_head_init(&temp);
123 __skb_queue_tail(&temp, sctp_event2skb(event));
125 event = sctp_ulpq_order(ulpq, event);
128 /* Send event to the ULP. */
130 sctp_ulpq_tail_event(ulpq, event);
135 /* Add a new event for propagation to the ULP. */
136 /* Clear the partial delivery mode for this socket. Note: This
137 * assumes that no association is currently in partial delivery mode.
139 int sctp_clear_pd(struct sock *sk)
145 if (!skb_queue_empty(&sp->pd_lobby)) {
146 struct list_head *list;
147 sctp_skb_list_tail(&sp->pd_lobby, &sk->receive_queue);
148 list = (struct list_head *)&sctp_sk(sk)->pd_lobby;
149 INIT_LIST_HEAD(list);
155 /* Clear the pd_mode and restart any pending messages waiting for delivery. */
156 static int sctp_ulpq_clear_pd(struct sctp_ulpq *ulpq)
159 return sctp_clear_pd(ulpq->asoc->base.sk);
164 int sctp_ulpq_tail_event(struct sctp_ulpq *ulpq, struct sctp_ulpevent *event)
166 struct sock *sk = ulpq->asoc->base.sk;
167 struct sk_buff_head *queue;
170 /* If the socket is just going to throw this away, do not
171 * even try to deliver it.
173 if (sk->dead || (sk->shutdown & RCV_SHUTDOWN))
176 /* Check if the user wishes to receive this event. */
177 if (!sctp_ulpevent_is_enabled(event, &sctp_sk(sk)->subscribe))
180 /* If we are in partial delivery mode, post to the lobby until
181 * partial delivery is cleared, unless, of course _this_ is
182 * the association the cause of the partial delivery.
185 if (!sctp_sk(sk)->pd_mode) {
186 queue = &sk->receive_queue;
187 } else if (ulpq->pd_mode) {
188 if (event->msg_flags & MSG_NOTIFICATION)
189 queue = &sctp_sk(sk)->pd_lobby;
191 clear_pd = event->msg_flags & MSG_EOR;
192 queue = &sk->receive_queue;
195 queue = &sctp_sk(sk)->pd_lobby;
198 /* If we are harvesting multiple skbs they will be
199 * collected on a list.
201 if (sctp_event2skb(event)->list)
202 sctp_skb_list_tail(sctp_event2skb(event)->list, queue);
204 __skb_queue_tail(queue, sctp_event2skb(event));
206 /* Did we just complete partial delivery and need to get
207 * rolling again? Move pending data to the receive
211 sctp_ulpq_clear_pd(ulpq);
213 if (queue == &sk->receive_queue)
214 sk->data_ready(sk, 0);
218 if (sctp_event2skb(event)->list)
219 sctp_queue_purge_ulpevents(sctp_event2skb(event)->list);
221 sctp_ulpevent_free(event);
225 /* 2nd Level Abstractions */
227 /* Helper function to store chunks that need to be reassembled. */
228 static inline void sctp_ulpq_store_reasm(struct sctp_ulpq *ulpq,
229 struct sctp_ulpevent *event)
232 struct sctp_ulpevent *cevent;
237 /* See if it belongs at the end. */
238 pos = skb_peek_tail(&ulpq->reasm);
240 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
244 /* Short circuit just dropping it at the end. */
245 cevent = sctp_skb2event(pos);
247 if (TSN_lt(ctsn, tsn)) {
248 __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
252 /* Find the right place in this list. We store them by TSN. */
253 skb_queue_walk(&ulpq->reasm, pos) {
254 cevent = sctp_skb2event(pos);
257 if (TSN_lt(tsn, ctsn))
261 /* Insert before pos. */
262 __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->reasm);
266 /* Helper function to return an event corresponding to the reassembled
268 * This routine creates a re-assembled skb given the first and last skb's
269 * as stored in the reassembly queue. The skb's may be non-linear if the sctp
270 * payload was fragmented on the way and ip had to reassemble them.
271 * We add the rest of skb's to the first skb's fraglist.
273 static struct sctp_ulpevent *sctp_make_reassembled_event(struct sk_buff *f_frag, struct sk_buff *l_frag)
276 struct sk_buff *new = NULL;
277 struct sctp_ulpevent *event;
278 struct sk_buff *pnext, *last;
279 struct sk_buff *list = skb_shinfo(f_frag)->frag_list;
281 /* Store the pointer to the 2nd skb */
282 if (f_frag == l_frag)
287 /* Get the last skb in the f_frag's frag_list if present. */
288 for (last = list; list; last = list, list = list->next);
290 /* Add the list of remaining fragments to the first fragments
296 if (skb_cloned(f_frag)) {
297 /* This is a cloned skb, we can't just modify
298 * the frag_list. We need a new skb to do that.
299 * Instead of calling skb_unshare(), we'll do it
300 * ourselves since we need to delay the free.
302 new = skb_copy(f_frag, GFP_ATOMIC);
304 return NULL; /* try again later */
306 new->sk = f_frag->sk;
308 skb_shinfo(new)->frag_list = pos;
310 skb_shinfo(f_frag)->frag_list = pos;
313 /* Remove the first fragment from the reassembly queue. */
314 __skb_unlink(f_frag, f_frag->list);
316 /* if we did unshare, then free the old skb and re-assign */
326 /* Update the len and data_len fields of the first fragment. */
327 f_frag->len += pos->len;
328 f_frag->data_len += pos->len;
330 /* Remove the fragment from the reassembly queue. */
331 __skb_unlink(pos, pos->list);
333 /* Break if we have reached the last fragment. */
340 event = sctp_skb2event(f_frag);
341 SCTP_INC_STATS(SctpReasmUsrMsgs);
347 /* Helper function to check if an incoming chunk has filled up the last
348 * missing fragment in a SCTP datagram and return the corresponding event.
350 static inline struct sctp_ulpevent *sctp_ulpq_retrieve_reassembled(struct sctp_ulpq *ulpq)
353 struct sctp_ulpevent *cevent;
354 struct sk_buff *first_frag = NULL;
355 __u32 ctsn, next_tsn;
356 struct sctp_ulpevent *retval = NULL;
358 /* Initialized to 0 just to avoid compiler warning message. Will
359 * never be used with this value. It is referenced only after it
360 * is set when we find the first fragment of a message.
364 /* The chunks are held in the reasm queue sorted by TSN.
365 * Walk through the queue sequentially and look for a sequence of
366 * fragmented chunks that complete a datagram.
367 * 'first_frag' and next_tsn are reset when we find a chunk which
368 * is the first fragment of a datagram. Once these 2 fields are set
369 * we expect to find the remaining middle fragments and the last
370 * fragment in order. If not, first_frag is reset to NULL and we
371 * start the next pass when we find another first fragment.
373 skb_queue_walk(&ulpq->reasm, pos) {
374 cevent = sctp_skb2event(pos);
377 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
378 case SCTP_DATA_FIRST_FRAG:
383 case SCTP_DATA_MIDDLE_FRAG:
384 if ((first_frag) && (ctsn == next_tsn))
390 case SCTP_DATA_LAST_FRAG:
391 if (first_frag && (ctsn == next_tsn))
402 retval = sctp_make_reassembled_event(first_frag, pos);
404 retval->msg_flags |= MSG_EOR;
408 /* Retrieve the next set of fragments of a partial message. */
409 static inline struct sctp_ulpevent *sctp_ulpq_retrieve_partial(struct sctp_ulpq *ulpq)
411 struct sk_buff *pos, *last_frag, *first_frag;
412 struct sctp_ulpevent *cevent;
413 __u32 ctsn, next_tsn;
415 struct sctp_ulpevent *retval;
417 /* The chunks are held in the reasm queue sorted by TSN.
418 * Walk through the queue sequentially and look for the first
419 * sequence of fragmented chunks.
422 if (skb_queue_empty(&ulpq->reasm))
425 last_frag = first_frag = NULL;
430 skb_queue_walk(&ulpq->reasm, pos) {
431 cevent = sctp_skb2event(pos);
434 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
435 case SCTP_DATA_MIDDLE_FRAG:
440 } else if (next_tsn == ctsn)
445 case SCTP_DATA_LAST_FRAG:
448 else if (ctsn != next_tsn)
458 /* We have the reassembled event. There is no need to look
462 retval = sctp_make_reassembled_event(first_frag, last_frag);
463 if (retval && is_last)
464 retval->msg_flags |= MSG_EOR;
470 /* Helper function to reassemble chunks. Hold chunks on the reasm queue that
473 static struct sctp_ulpevent *sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
474 struct sctp_ulpevent *event)
476 struct sctp_ulpevent *retval = NULL;
478 /* Check if this is part of a fragmented message. */
479 if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
480 event->msg_flags |= MSG_EOR;
484 sctp_ulpq_store_reasm(ulpq, event);
486 retval = sctp_ulpq_retrieve_reassembled(ulpq);
490 /* Do not even bother unless this is the next tsn to
494 ctsnap = sctp_tsnmap_get_ctsn(&ulpq->asoc->peer.tsn_map);
495 if (TSN_lte(ctsn, ctsnap))
496 retval = sctp_ulpq_retrieve_partial(ulpq);
502 /* Retrieve the first part (sequential fragments) for partial delivery. */
503 static inline struct sctp_ulpevent *sctp_ulpq_retrieve_first(struct sctp_ulpq *ulpq)
505 struct sk_buff *pos, *last_frag, *first_frag;
506 struct sctp_ulpevent *cevent;
507 __u32 ctsn, next_tsn;
508 struct sctp_ulpevent *retval;
510 /* The chunks are held in the reasm queue sorted by TSN.
511 * Walk through the queue sequentially and look for a sequence of
512 * fragmented chunks that start a datagram.
515 if (skb_queue_empty(&ulpq->reasm))
518 last_frag = first_frag = NULL;
522 skb_queue_walk(&ulpq->reasm, pos) {
523 cevent = sctp_skb2event(pos);
526 switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
527 case SCTP_DATA_FIRST_FRAG:
536 case SCTP_DATA_MIDDLE_FRAG:
539 if (ctsn == next_tsn) {
550 /* We have the reassembled event. There is no need to look
554 retval = sctp_make_reassembled_event(first_frag, last_frag);
558 /* Helper function to gather skbs that have possibly become
559 * ordered by an an incoming chunk.
561 static inline void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
562 struct sctp_ulpevent *event)
564 struct sk_buff *pos, *tmp;
565 struct sctp_ulpevent *cevent;
566 struct sctp_stream *in;
572 in = &ulpq->asoc->ssnmap->in;
574 /* We are holding the chunks by stream, by SSN. */
575 sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
576 cevent = (struct sctp_ulpevent *) pos->cb;
577 csid = cevent->stream;
580 /* Have we gone too far? */
584 /* Have we not gone far enough? */
588 if (cssn != sctp_ssn_peek(in, sid))
591 /* Found it, so mark in the ssnmap. */
592 sctp_ssn_next(in, sid);
594 __skb_unlink(pos, pos->list);
596 /* Attach all gathered skbs to the event. */
597 __skb_queue_tail(sctp_event2skb(event)->list, pos);
601 /* Helper function to store chunks needing ordering. */
602 static inline void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
603 struct sctp_ulpevent *event)
606 struct sctp_ulpevent *cevent;
610 pos = skb_peek_tail(&ulpq->lobby);
612 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
619 cevent = (struct sctp_ulpevent *) pos->cb;
620 csid = cevent->stream;
623 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
627 if ((sid == csid) && SSN_lt(cssn, ssn)) {
628 __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
632 /* Find the right place in this list. We store them by
633 * stream ID and then by SSN.
635 skb_queue_walk(&ulpq->lobby, pos) {
636 cevent = (struct sctp_ulpevent *) pos->cb;
637 csid = cevent->stream;
642 if (csid == sid && SSN_lt(ssn, cssn))
647 /* Insert before pos. */
648 __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->lobby);
652 static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
653 struct sctp_ulpevent *event)
656 struct sctp_stream *in;
658 /* Check if this message needs ordering. */
659 if (SCTP_DATA_UNORDERED & event->msg_flags)
662 /* Note: The stream ID must be verified before this routine. */
665 in = &ulpq->asoc->ssnmap->in;
667 /* Is this the expected SSN for this stream ID? */
668 if (ssn != sctp_ssn_peek(in, sid)) {
669 /* We've received something out of order, so find where it
670 * needs to be placed. We order by stream and then by SSN.
672 sctp_ulpq_store_ordered(ulpq, event);
676 /* Mark that the next chunk has been found. */
677 sctp_ssn_next(in, sid);
679 /* Go find any other chunks that were waiting for
682 sctp_ulpq_retrieve_ordered(ulpq, event);
687 /* Helper function to gather skbs that have possibly become
688 * ordered by forward tsn skipping their dependencies.
690 static inline void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq)
692 struct sk_buff *pos, *tmp;
693 struct sctp_ulpevent *cevent;
694 struct sctp_ulpevent *event = NULL;
695 struct sctp_stream *in;
696 struct sk_buff_head temp;
699 in = &ulpq->asoc->ssnmap->in;
701 /* We are holding the chunks by stream, by SSN. */
702 sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
703 cevent = (struct sctp_ulpevent *) pos->cb;
704 csid = cevent->stream;
707 if (cssn != sctp_ssn_peek(in, csid))
710 /* Found it, so mark in the ssnmap. */
711 sctp_ssn_next(in, csid);
713 __skb_unlink(pos, pos->list);
715 /* Create a temporary list to collect chunks on. */
716 event = sctp_skb2event(pos);
717 skb_queue_head_init(&temp);
718 __skb_queue_tail(&temp, sctp_event2skb(event));
720 /* Attach all gathered skbs to the event. */
721 __skb_queue_tail(sctp_event2skb(event)->list, pos);
725 /* Send event to the ULP. */
727 sctp_ulpq_tail_event(ulpq, event);
730 /* Skip over an SSN. */
731 void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
733 struct sctp_stream *in;
735 /* Note: The stream ID must be verified before this routine. */
736 in = &ulpq->asoc->ssnmap->in;
738 /* Is this an old SSN? If so ignore. */
739 if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
742 /* Mark that we are no longer expecting this SSN or lower. */
743 sctp_ssn_skip(in, sid, ssn);
745 /* Go find any other chunks that were waiting for
746 * ordering and deliver them if needed.
748 sctp_ulpq_reap_ordered(ulpq);
752 /* Renege 'needed' bytes from the ordering queue. */
753 static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
758 struct sctp_ulpevent *event;
759 struct sctp_tsnmap *tsnmap;
761 tsnmap = &ulpq->asoc->peer.tsn_map;
763 while ((skb = __skb_dequeue_tail(&ulpq->lobby)) != NULL) {
764 freed += skb_headlen(skb);
765 event = sctp_skb2event(skb);
768 sctp_ulpevent_free(event);
769 sctp_tsnmap_renege(tsnmap, tsn);
777 /* Renege 'needed' bytes from the reassembly queue. */
778 static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
783 struct sctp_ulpevent *event;
784 struct sctp_tsnmap *tsnmap;
786 tsnmap = &ulpq->asoc->peer.tsn_map;
788 /* Walk backwards through the list, reneges the newest tsns. */
789 while ((skb = __skb_dequeue_tail(&ulpq->reasm)) != NULL) {
790 freed += skb_headlen(skb);
791 event = sctp_skb2event(skb);
794 sctp_ulpevent_free(event);
795 sctp_tsnmap_renege(tsnmap, tsn);
803 /* Partial deliver the first message as there is pressure on rwnd. */
804 void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
805 struct sctp_chunk *chunk, int gfp)
807 struct sctp_ulpevent *event;
808 struct sctp_association *asoc;
812 /* Are we already in partial delivery mode? */
813 if (!sctp_sk(asoc->base.sk)->pd_mode) {
815 /* Is partial delivery possible? */
816 event = sctp_ulpq_retrieve_first(ulpq);
817 /* Send event to the ULP. */
819 sctp_ulpq_tail_event(ulpq, event);
820 sctp_sk(asoc->base.sk)->pd_mode = 1;
827 /* Renege some packets to make room for an incoming chunk. */
828 void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
831 struct sctp_association *asoc;
837 needed = ntohs(chunk->chunk_hdr->length);
838 needed -= sizeof(sctp_data_chunk_t);
840 needed = SCTP_DEFAULT_MAXWINDOW;
844 if (skb_queue_empty(&asoc->base.sk->receive_queue)) {
845 freed = sctp_ulpq_renege_order(ulpq, needed);
846 if (freed < needed) {
847 freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
850 /* If able to free enough room, accept this chunk. */
851 if (chunk && (freed >= needed)) {
853 tsn = ntohl(chunk->subh.data_hdr->tsn);
854 sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
855 sctp_ulpq_tail_data(ulpq, chunk, gfp);
857 sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
865 /* Notify the application if an association is aborted and in
866 * partial delivery mode. Send up any pending received messages.
868 void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, int gfp)
870 struct sctp_ulpevent *ev = NULL;
876 sk = ulpq->asoc->base.sk;
877 if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
878 &sctp_sk(sk)->subscribe))
879 ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
880 SCTP_PARTIAL_DELIVERY_ABORTED,
883 __skb_queue_tail(&sk->receive_queue, sctp_event2skb(ev));
885 /* If there is data waiting, send it up the socket now. */
886 if (sctp_ulpq_clear_pd(ulpq) || ev)
887 sk->data_ready(sk, 0);