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 * These functions manipulate an sctp event. The struct ulpevent is used
10 * to carry notifications and data 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 * Ardelle Fan <ardelle.fan@intel.com>
39 * Sridhar Samudrala <sri@us.ibm.com>
41 * Any bugs reported given to us we will try to fix... any fixes shared will
42 * be incorporated into the next SCTP release.
45 #include <linux/types.h>
46 #include <linux/skbuff.h>
47 #include <net/sctp/structs.h>
48 #include <net/sctp/sctp.h>
49 #include <net/sctp/sm.h>
51 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
52 struct sctp_association *asoc);
53 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
54 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
57 /* Initialize an ULP event from an given skb. */
58 SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,
62 memset(event, 0, sizeof(struct sctp_ulpevent));
63 event->msg_flags = msg_flags;
64 event->rmem_len = len;
67 /* Create a new sctp_ulpevent. */
68 SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
71 struct sctp_ulpevent *event;
74 skb = alloc_skb(size, gfp);
78 event = sctp_skb2event(skb);
79 sctp_ulpevent_init(event, msg_flags, skb->truesize);
87 /* Is this a MSG_NOTIFICATION? */
88 int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
90 return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
93 /* Hold the association in case the msg_name needs read out of
96 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
97 const struct sctp_association *asoc)
101 /* Cast away the const, as we are just wanting to
102 * bump the reference count.
104 sctp_association_hold((struct sctp_association *)asoc);
105 skb = sctp_event2skb(event);
106 event->asoc = (struct sctp_association *)asoc;
107 atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
108 sctp_skb_set_owner_r(skb, asoc->base.sk);
111 /* A simple destructor to give up the reference to the association. */
112 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
114 struct sctp_association *asoc = event->asoc;
116 atomic_sub(event->rmem_len, &asoc->rmem_alloc);
117 sctp_association_put(asoc);
120 /* Create and initialize an SCTP_ASSOC_CHANGE event.
122 * 5.3.1.1 SCTP_ASSOC_CHANGE
124 * Communication notifications inform the ULP that an SCTP association
125 * has either begun or ended. The identifier for a new association is
126 * provided by this notification.
128 * Note: There is no field checking here. If a field is unused it will be
131 struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
132 const struct sctp_association *asoc,
133 __u16 flags, __u16 state, __u16 error, __u16 outbound,
134 __u16 inbound, gfp_t gfp)
136 struct sctp_ulpevent *event;
137 struct sctp_assoc_change *sac;
140 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
141 MSG_NOTIFICATION, gfp);
144 skb = sctp_event2skb(event);
145 sac = (struct sctp_assoc_change *)
146 skb_put(skb, sizeof(struct sctp_assoc_change));
148 /* Socket Extensions for SCTP
149 * 5.3.1.1 SCTP_ASSOC_CHANGE
152 * It should be SCTP_ASSOC_CHANGE.
154 sac->sac_type = SCTP_ASSOC_CHANGE;
156 /* Socket Extensions for SCTP
157 * 5.3.1.1 SCTP_ASSOC_CHANGE
159 * sac_state: 32 bits (signed integer)
160 * This field holds one of a number of values that communicate the
161 * event that happened to the association.
163 sac->sac_state = state;
165 /* Socket Extensions for SCTP
166 * 5.3.1.1 SCTP_ASSOC_CHANGE
168 * sac_flags: 16 bits (unsigned integer)
173 /* Socket Extensions for SCTP
174 * 5.3.1.1 SCTP_ASSOC_CHANGE
176 * sac_length: sizeof (__u32)
177 * This field is the total length of the notification data, including
178 * the notification header.
180 sac->sac_length = sizeof(struct sctp_assoc_change);
182 /* Socket Extensions for SCTP
183 * 5.3.1.1 SCTP_ASSOC_CHANGE
185 * sac_error: 32 bits (signed integer)
187 * If the state was reached due to a error condition (e.g.
188 * COMMUNICATION_LOST) any relevant error information is available in
189 * this field. This corresponds to the protocol error codes defined in
192 sac->sac_error = error;
194 /* Socket Extensions for SCTP
195 * 5.3.1.1 SCTP_ASSOC_CHANGE
197 * sac_outbound_streams: 16 bits (unsigned integer)
198 * sac_inbound_streams: 16 bits (unsigned integer)
200 * The maximum number of streams allowed in each direction are
201 * available in sac_outbound_streams and sac_inbound streams.
203 sac->sac_outbound_streams = outbound;
204 sac->sac_inbound_streams = inbound;
206 /* Socket Extensions for SCTP
207 * 5.3.1.1 SCTP_ASSOC_CHANGE
209 * sac_assoc_id: sizeof (sctp_assoc_t)
211 * The association id field, holds the identifier for the association.
212 * All notifications for a given association have the same association
213 * identifier. For TCP style socket, this field is ignored.
215 sctp_ulpevent_set_owner(event, asoc);
216 sac->sac_assoc_id = sctp_assoc2id(asoc);
224 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
226 * Socket Extensions for SCTP - draft-01
227 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
229 * When a destination address on a multi-homed peer encounters a change
230 * an interface details event is sent.
232 struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
233 const struct sctp_association *asoc,
234 const struct sockaddr_storage *aaddr,
235 int flags, int state, int error, gfp_t gfp)
237 struct sctp_ulpevent *event;
238 struct sctp_paddr_change *spc;
241 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
242 MSG_NOTIFICATION, gfp);
246 skb = sctp_event2skb(event);
247 spc = (struct sctp_paddr_change *)
248 skb_put(skb, sizeof(struct sctp_paddr_change));
250 /* Sockets API Extensions for SCTP
251 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
255 * It should be SCTP_PEER_ADDR_CHANGE.
257 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
259 /* Sockets API Extensions for SCTP
260 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
262 * spc_length: sizeof (__u32)
264 * This field is the total length of the notification data, including
265 * the notification header.
267 spc->spc_length = sizeof(struct sctp_paddr_change);
269 /* Sockets API Extensions for SCTP
270 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
272 * spc_flags: 16 bits (unsigned integer)
277 /* Sockets API Extensions for SCTP
278 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
280 * spc_state: 32 bits (signed integer)
282 * This field holds one of a number of values that communicate the
283 * event that happened to the address.
285 spc->spc_state = state;
287 /* Sockets API Extensions for SCTP
288 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
290 * spc_error: 32 bits (signed integer)
292 * If the state was reached due to any error condition (e.g.
293 * ADDRESS_UNREACHABLE) any relevant error information is available in
296 spc->spc_error = error;
298 /* Socket Extensions for SCTP
299 * 5.3.1.1 SCTP_ASSOC_CHANGE
301 * spc_assoc_id: sizeof (sctp_assoc_t)
303 * The association id field, holds the identifier for the association.
304 * All notifications for a given association have the same association
305 * identifier. For TCP style socket, this field is ignored.
307 sctp_ulpevent_set_owner(event, asoc);
308 spc->spc_assoc_id = sctp_assoc2id(asoc);
310 /* Sockets API Extensions for SCTP
311 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
313 * spc_aaddr: sizeof (struct sockaddr_storage)
315 * The affected address field, holds the remote peer's address that is
316 * encountering the change of state.
318 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
320 /* Map ipv4 address into v4-mapped-on-v6 address. */
321 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
322 sctp_sk(asoc->base.sk),
323 (union sctp_addr *)&spc->spc_aaddr);
331 /* Create and initialize an SCTP_REMOTE_ERROR notification.
333 * Note: This assumes that the chunk->skb->data already points to the
334 * operation error payload.
336 * Socket Extensions for SCTP - draft-01
337 * 5.3.1.3 SCTP_REMOTE_ERROR
339 * A remote peer may send an Operational Error message to its peer.
340 * This message indicates a variety of error conditions on an
341 * association. The entire error TLV as it appears on the wire is
342 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
343 * specification [SCTP] and any extensions for a list of possible
346 struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
347 const struct sctp_association *asoc, struct sctp_chunk *chunk,
348 __u16 flags, gfp_t gfp)
350 struct sctp_ulpevent *event;
351 struct sctp_remote_error *sre;
357 ch = (sctp_errhdr_t *)(chunk->skb->data);
359 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
361 /* Pull off the ERROR header. */
362 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
364 /* Copy the skb to a new skb with room for us to prepend
367 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
370 /* Pull off the rest of the cause TLV from the chunk. */
371 skb_pull(chunk->skb, elen);
375 /* Embed the event fields inside the cloned skb. */
376 event = sctp_skb2event(skb);
377 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
379 sre = (struct sctp_remote_error *)
380 skb_push(skb, sizeof(struct sctp_remote_error));
382 /* Trim the buffer to the right length. */
383 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
385 /* Socket Extensions for SCTP
386 * 5.3.1.3 SCTP_REMOTE_ERROR
389 * It should be SCTP_REMOTE_ERROR.
391 sre->sre_type = SCTP_REMOTE_ERROR;
394 * Socket Extensions for SCTP
395 * 5.3.1.3 SCTP_REMOTE_ERROR
397 * sre_flags: 16 bits (unsigned integer)
402 /* Socket Extensions for SCTP
403 * 5.3.1.3 SCTP_REMOTE_ERROR
405 * sre_length: sizeof (__u32)
407 * This field is the total length of the notification data,
408 * including the notification header.
410 sre->sre_length = skb->len;
412 /* Socket Extensions for SCTP
413 * 5.3.1.3 SCTP_REMOTE_ERROR
415 * sre_error: 16 bits (unsigned integer)
416 * This value represents one of the Operational Error causes defined in
417 * the SCTP specification, in network byte order.
419 sre->sre_error = cause;
421 /* Socket Extensions for SCTP
422 * 5.3.1.3 SCTP_REMOTE_ERROR
424 * sre_assoc_id: sizeof (sctp_assoc_t)
426 * The association id field, holds the identifier for the association.
427 * All notifications for a given association have the same association
428 * identifier. For TCP style socket, this field is ignored.
430 sctp_ulpevent_set_owner(event, asoc);
431 sre->sre_assoc_id = sctp_assoc2id(asoc);
439 /* Create and initialize a SCTP_SEND_FAILED notification.
441 * Socket Extensions for SCTP - draft-01
442 * 5.3.1.4 SCTP_SEND_FAILED
444 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
445 const struct sctp_association *asoc, struct sctp_chunk *chunk,
446 __u16 flags, __u32 error, gfp_t gfp)
448 struct sctp_ulpevent *event;
449 struct sctp_send_failed *ssf;
452 /* Pull off any padding. */
453 int len = ntohs(chunk->chunk_hdr->length);
455 /* Make skb with more room so we can prepend notification. */
456 skb = skb_copy_expand(chunk->skb,
457 sizeof(struct sctp_send_failed), /* headroom */
463 /* Pull off the common chunk header and DATA header. */
464 skb_pull(skb, sizeof(struct sctp_data_chunk));
465 len -= sizeof(struct sctp_data_chunk);
467 /* Embed the event fields inside the cloned skb. */
468 event = sctp_skb2event(skb);
469 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
471 ssf = (struct sctp_send_failed *)
472 skb_push(skb, sizeof(struct sctp_send_failed));
474 /* Socket Extensions for SCTP
475 * 5.3.1.4 SCTP_SEND_FAILED
478 * It should be SCTP_SEND_FAILED.
480 ssf->ssf_type = SCTP_SEND_FAILED;
482 /* Socket Extensions for SCTP
483 * 5.3.1.4 SCTP_SEND_FAILED
485 * ssf_flags: 16 bits (unsigned integer)
486 * The flag value will take one of the following values
488 * SCTP_DATA_UNSENT - Indicates that the data was never put on
491 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
492 * Note that this does not necessarily mean that the
493 * data was (or was not) successfully delivered.
495 ssf->ssf_flags = flags;
497 /* Socket Extensions for SCTP
498 * 5.3.1.4 SCTP_SEND_FAILED
500 * ssf_length: sizeof (__u32)
501 * This field is the total length of the notification data, including
502 * the notification header.
504 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
505 skb_trim(skb, ssf->ssf_length);
507 /* Socket Extensions for SCTP
508 * 5.3.1.4 SCTP_SEND_FAILED
510 * ssf_error: 16 bits (unsigned integer)
511 * This value represents the reason why the send failed, and if set,
512 * will be a SCTP protocol error code as defined in [SCTP] section
515 ssf->ssf_error = error;
517 /* Socket Extensions for SCTP
518 * 5.3.1.4 SCTP_SEND_FAILED
520 * ssf_info: sizeof (struct sctp_sndrcvinfo)
521 * The original send information associated with the undelivered
524 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
526 /* Per TSVWG discussion with Randy. Allow the application to
527 * ressemble a fragmented message.
529 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
531 /* Socket Extensions for SCTP
532 * 5.3.1.4 SCTP_SEND_FAILED
534 * ssf_assoc_id: sizeof (sctp_assoc_t)
535 * The association id field, sf_assoc_id, holds the identifier for the
536 * association. All notifications for a given association have the
537 * same association identifier. For TCP style socket, this field is
540 sctp_ulpevent_set_owner(event, asoc);
541 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
548 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
550 * Socket Extensions for SCTP - draft-01
551 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
553 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
554 const struct sctp_association *asoc,
555 __u16 flags, gfp_t gfp)
557 struct sctp_ulpevent *event;
558 struct sctp_shutdown_event *sse;
561 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
562 MSG_NOTIFICATION, gfp);
566 skb = sctp_event2skb(event);
567 sse = (struct sctp_shutdown_event *)
568 skb_put(skb, sizeof(struct sctp_shutdown_event));
570 /* Socket Extensions for SCTP
571 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
574 * It should be SCTP_SHUTDOWN_EVENT
576 sse->sse_type = SCTP_SHUTDOWN_EVENT;
578 /* Socket Extensions for SCTP
579 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
581 * sse_flags: 16 bits (unsigned integer)
586 /* Socket Extensions for SCTP
587 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
589 * sse_length: sizeof (__u32)
590 * This field is the total length of the notification data, including
591 * the notification header.
593 sse->sse_length = sizeof(struct sctp_shutdown_event);
595 /* Socket Extensions for SCTP
596 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
598 * sse_assoc_id: sizeof (sctp_assoc_t)
599 * The association id field, holds the identifier for the association.
600 * All notifications for a given association have the same association
601 * identifier. For TCP style socket, this field is ignored.
603 sctp_ulpevent_set_owner(event, asoc);
604 sse->sse_assoc_id = sctp_assoc2id(asoc);
612 /* Create and initialize a SCTP_ADAPTION_INDICATION notification.
614 * Socket Extensions for SCTP
615 * 5.3.1.6 SCTP_ADAPTION_INDICATION
617 struct sctp_ulpevent *sctp_ulpevent_make_adaption_indication(
618 const struct sctp_association *asoc, gfp_t gfp)
620 struct sctp_ulpevent *event;
621 struct sctp_adaption_event *sai;
624 event = sctp_ulpevent_new(sizeof(struct sctp_adaption_event),
625 MSG_NOTIFICATION, gfp);
629 skb = sctp_event2skb(event);
630 sai = (struct sctp_adaption_event *)
631 skb_put(skb, sizeof(struct sctp_adaption_event));
633 sai->sai_type = SCTP_ADAPTION_INDICATION;
635 sai->sai_length = sizeof(struct sctp_adaption_event);
636 sai->sai_adaption_ind = asoc->peer.adaption_ind;
637 sctp_ulpevent_set_owner(event, asoc);
638 sai->sai_assoc_id = sctp_assoc2id(asoc);
646 /* A message has been received. Package this message as a notification
647 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
648 * even if filtered out later.
650 * Socket Extensions for SCTP
651 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
653 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
654 struct sctp_chunk *chunk,
657 struct sctp_ulpevent *event = NULL;
661 /* Clone the original skb, sharing the data. */
662 skb = skb_clone(chunk->skb, gfp);
666 /* First calculate the padding, so we don't inadvertently
667 * pass up the wrong length to the user.
669 * RFC 2960 - Section 3.2 Chunk Field Descriptions
671 * The total length of a chunk(including Type, Length and Value fields)
672 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
673 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
674 * bytes and this padding is not included in the chunk length field.
675 * The sender should never pad with more than 3 bytes. The receiver
676 * MUST ignore the padding bytes.
678 len = ntohs(chunk->chunk_hdr->length);
679 padding = WORD_ROUND(len) - len;
681 /* Fixup cloned skb with just this chunks data. */
682 skb_trim(skb, chunk->chunk_end - padding - skb->data);
684 /* Embed the event fields inside the cloned skb. */
685 event = sctp_skb2event(skb);
687 /* Initialize event with flags 0 and correct length
688 * Since this is a clone of the original skb, only account for
689 * the data of this chunk as other chunks will be accounted separately.
691 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
693 sctp_ulpevent_receive_data(event, asoc);
695 event->stream = ntohs(chunk->subh.data_hdr->stream);
696 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
697 event->ppid = chunk->subh.data_hdr->ppid;
698 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
699 event->flags |= SCTP_UNORDERED;
700 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
702 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
703 event->msg_flags |= chunk->chunk_hdr->flags;
704 event->iif = sctp_chunk_iif(chunk);
710 /* Create a partial delivery related event.
712 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
714 * When a receiver is engaged in a partial delivery of a
715 * message this notification will be used to indicate
718 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
719 const struct sctp_association *asoc, __u32 indication,
722 struct sctp_ulpevent *event;
723 struct sctp_pdapi_event *pd;
726 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
727 MSG_NOTIFICATION, gfp);
731 skb = sctp_event2skb(event);
732 pd = (struct sctp_pdapi_event *)
733 skb_put(skb, sizeof(struct sctp_pdapi_event));
736 * It should be SCTP_PARTIAL_DELIVERY_EVENT
738 * pdapi_flags: 16 bits (unsigned integer)
741 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
744 /* pdapi_length: 32 bits (unsigned integer)
746 * This field is the total length of the notification data, including
747 * the notification header. It will generally be sizeof (struct
750 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
752 /* pdapi_indication: 32 bits (unsigned integer)
754 * This field holds the indication being sent to the application.
756 pd->pdapi_indication = indication;
758 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
760 * The association id field, holds the identifier for the association.
762 sctp_ulpevent_set_owner(event, asoc);
763 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
770 /* Return the notification type, assuming this is a notification
773 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
775 union sctp_notification *notification;
778 skb = sctp_event2skb((struct sctp_ulpevent *)event);
779 notification = (union sctp_notification *) skb->data;
780 return notification->sn_header.sn_type;
783 /* Copy out the sndrcvinfo into a msghdr. */
784 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
785 struct msghdr *msghdr)
787 struct sctp_sndrcvinfo sinfo;
789 if (sctp_ulpevent_is_notification(event))
792 /* Sockets API Extensions for SCTP
793 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
795 * sinfo_stream: 16 bits (unsigned integer)
797 * For recvmsg() the SCTP stack places the message's stream number in
800 sinfo.sinfo_stream = event->stream;
801 /* sinfo_ssn: 16 bits (unsigned integer)
803 * For recvmsg() this value contains the stream sequence number that
804 * the remote endpoint placed in the DATA chunk. For fragmented
805 * messages this is the same number for all deliveries of the message
806 * (if more than one recvmsg() is needed to read the message).
808 sinfo.sinfo_ssn = event->ssn;
809 /* sinfo_ppid: 32 bits (unsigned integer)
811 * In recvmsg() this value is
812 * the same information that was passed by the upper layer in the peer
813 * application. Please note that byte order issues are NOT accounted
814 * for and this information is passed opaquely by the SCTP stack from
815 * one end to the other.
817 sinfo.sinfo_ppid = event->ppid;
818 /* sinfo_flags: 16 bits (unsigned integer)
820 * This field may contain any of the following flags and is composed of
821 * a bitwise OR of these values.
825 * SCTP_UNORDERED - This flag is present when the message was sent
828 sinfo.sinfo_flags = event->flags;
829 /* sinfo_tsn: 32 bit (unsigned integer)
831 * For the receiving side, this field holds a TSN that was
832 * assigned to one of the SCTP Data Chunks.
834 sinfo.sinfo_tsn = event->tsn;
835 /* sinfo_cumtsn: 32 bit (unsigned integer)
837 * This field will hold the current cumulative TSN as
838 * known by the underlying SCTP layer. Note this field is
839 * ignored when sending and only valid for a receive
840 * operation when sinfo_flags are set to SCTP_UNORDERED.
842 sinfo.sinfo_cumtsn = event->cumtsn;
843 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
845 * The association handle field, sinfo_assoc_id, holds the identifier
846 * for the association announced in the COMMUNICATION_UP notification.
847 * All notifications for a given association have the same identifier.
848 * Ignored for one-to-one style sockets.
850 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
852 /* These fields are not used while receiving. */
853 sinfo.sinfo_context = 0;
854 sinfo.sinfo_timetolive = 0;
856 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
857 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
860 /* Do accounting for bytes received and hold a reference to the association
863 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
864 struct sctp_association *asoc)
866 struct sk_buff *skb, *frag;
868 skb = sctp_event2skb(event);
869 /* Set the owner and charge rwnd for bytes received. */
870 sctp_ulpevent_set_owner(event, asoc);
871 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
876 /* Note: Not clearing the entire event struct as this is just a
877 * fragment of the real event. However, we still need to do rwnd
879 * In general, the skb passed from IP can have only 1 level of
880 * fragments. But we allow multiple levels of fragments.
882 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
883 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
887 /* Do accounting for bytes just read by user and release the references to
890 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
892 struct sk_buff *skb, *frag;
895 /* Current stack structures assume that the rcv buffer is
896 * per socket. For UDP style sockets this is not true as
897 * multiple associations may be on a single UDP-style socket.
898 * Use the local private area of the skb to track the owning
902 skb = sctp_event2skb(event);
908 /* Don't forget the fragments. */
909 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
910 /* NOTE: skb_shinfos are recursive. Although IP returns
911 * skb's with only 1 level of fragments, SCTP reassembly can
912 * increase the levels.
914 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
918 sctp_assoc_rwnd_increase(event->asoc, len);
919 sctp_ulpevent_release_owner(event);
922 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
924 struct sk_buff *skb, *frag;
926 skb = sctp_event2skb(event);
931 /* Don't forget the fragments. */
932 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
933 /* NOTE: skb_shinfos are recursive. Although IP returns
934 * skb's with only 1 level of fragments, SCTP reassembly can
935 * increase the levels.
937 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
941 sctp_ulpevent_release_owner(event);
944 /* Free a ulpevent that has an owner. It includes releasing the reference
945 * to the owner, updating the rwnd in case of a DATA event and freeing the
948 void sctp_ulpevent_free(struct sctp_ulpevent *event)
950 if (sctp_ulpevent_is_notification(event))
951 sctp_ulpevent_release_owner(event);
953 sctp_ulpevent_release_data(event);
955 kfree_skb(sctp_event2skb(event));
958 /* Purge the skb lists holding ulpevents. */
959 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
962 while ((skb = skb_dequeue(list)) != NULL)
963 sctp_ulpevent_free(sctp_skb2event(skb));