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 inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
52 const struct sctp_association *asoc);
53 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event);
54 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
55 struct sctp_association *asoc);
56 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
58 /* Create a new sctp_ulpevent. */
59 struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags, int gfp)
61 struct sctp_ulpevent *event;
64 skb = alloc_skb(size, gfp);
68 event = sctp_skb2event(skb);
69 sctp_ulpevent_init(event, msg_flags);
77 /* Initialize an ULP event from an given skb. */
78 void sctp_ulpevent_init(struct sctp_ulpevent *event, int msg_flags)
80 memset(event, 0, sizeof(struct sctp_ulpevent));
81 event->msg_flags = msg_flags;
84 /* Is this a MSG_NOTIFICATION? */
85 int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
87 return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
90 /* Create and initialize an SCTP_ASSOC_CHANGE event.
92 * 5.3.1.1 SCTP_ASSOC_CHANGE
94 * Communication notifications inform the ULP that an SCTP association
95 * has either begun or ended. The identifier for a new association is
96 * provided by this notification.
98 * Note: There is no field checking here. If a field is unused it will be
101 struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
102 const struct sctp_association *asoc,
103 __u16 flags, __u16 state, __u16 error, __u16 outbound,
104 __u16 inbound, int gfp)
106 struct sctp_ulpevent *event;
107 struct sctp_assoc_change *sac;
110 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
111 MSG_NOTIFICATION, gfp);
114 skb = sctp_event2skb(event);
115 sac = (struct sctp_assoc_change *)
116 skb_put(skb, sizeof(struct sctp_assoc_change));
118 /* Socket Extensions for SCTP
119 * 5.3.1.1 SCTP_ASSOC_CHANGE
122 * It should be SCTP_ASSOC_CHANGE.
124 sac->sac_type = SCTP_ASSOC_CHANGE;
126 /* Socket Extensions for SCTP
127 * 5.3.1.1 SCTP_ASSOC_CHANGE
129 * sac_state: 32 bits (signed integer)
130 * This field holds one of a number of values that communicate the
131 * event that happened to the association.
133 sac->sac_state = state;
135 /* Socket Extensions for SCTP
136 * 5.3.1.1 SCTP_ASSOC_CHANGE
138 * sac_flags: 16 bits (unsigned integer)
143 /* Socket Extensions for SCTP
144 * 5.3.1.1 SCTP_ASSOC_CHANGE
146 * sac_length: sizeof (__u32)
147 * This field is the total length of the notification data, including
148 * the notification header.
150 sac->sac_length = sizeof(struct sctp_assoc_change);
152 /* Socket Extensions for SCTP
153 * 5.3.1.1 SCTP_ASSOC_CHANGE
155 * sac_error: 32 bits (signed integer)
157 * If the state was reached due to a error condition (e.g.
158 * COMMUNICATION_LOST) any relevant error information is available in
159 * this field. This corresponds to the protocol error codes defined in
162 sac->sac_error = error;
164 /* Socket Extensions for SCTP
165 * 5.3.1.1 SCTP_ASSOC_CHANGE
167 * sac_outbound_streams: 16 bits (unsigned integer)
168 * sac_inbound_streams: 16 bits (unsigned integer)
170 * The maximum number of streams allowed in each direction are
171 * available in sac_outbound_streams and sac_inbound streams.
173 sac->sac_outbound_streams = outbound;
174 sac->sac_inbound_streams = inbound;
176 /* Socket Extensions for SCTP
177 * 5.3.1.1 SCTP_ASSOC_CHANGE
179 * sac_assoc_id: sizeof (sctp_assoc_t)
181 * The association id field, holds the identifier for the association.
182 * All notifications for a given association have the same association
183 * identifier. For TCP style socket, this field is ignored.
185 sctp_ulpevent_set_owner(event, asoc);
186 sac->sac_assoc_id = sctp_assoc2id(asoc);
194 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
196 * Socket Extensions for SCTP - draft-01
197 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
199 * When a destination address on a multi-homed peer encounters a change
200 * an interface details event is sent.
202 struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
203 const struct sctp_association *asoc,
204 const struct sockaddr_storage *aaddr,
205 int flags, int state, int error, int gfp)
207 struct sctp_ulpevent *event;
208 struct sctp_paddr_change *spc;
211 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
212 MSG_NOTIFICATION, gfp);
216 skb = sctp_event2skb(event);
217 spc = (struct sctp_paddr_change *)
218 skb_put(skb, sizeof(struct sctp_paddr_change));
220 /* Sockets API Extensions for SCTP
221 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
225 * It should be SCTP_PEER_ADDR_CHANGE.
227 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
229 /* Sockets API Extensions for SCTP
230 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
232 * spc_length: sizeof (__u32)
234 * This field is the total length of the notification data, including
235 * the notification header.
237 spc->spc_length = sizeof(struct sctp_paddr_change);
239 /* Sockets API Extensions for SCTP
240 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
242 * spc_flags: 16 bits (unsigned integer)
247 /* Sockets API Extensions for SCTP
248 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
250 * spc_state: 32 bits (signed integer)
252 * This field holds one of a number of values that communicate the
253 * event that happened to the address.
255 spc->spc_state = state;
257 /* Sockets API Extensions for SCTP
258 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
260 * spc_error: 32 bits (signed integer)
262 * If the state was reached due to any error condition (e.g.
263 * ADDRESS_UNREACHABLE) any relevant error information is available in
266 spc->spc_error = error;
268 /* Socket Extensions for SCTP
269 * 5.3.1.1 SCTP_ASSOC_CHANGE
271 * spc_assoc_id: sizeof (sctp_assoc_t)
273 * The association id field, holds the identifier for the association.
274 * All notifications for a given association have the same association
275 * identifier. For TCP style socket, this field is ignored.
277 sctp_ulpevent_set_owner(event, asoc);
278 spc->spc_assoc_id = sctp_assoc2id(asoc);
280 /* Sockets API Extensions for SCTP
281 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
283 * spc_aaddr: sizeof (struct sockaddr_storage)
285 * The affected address field, holds the remote peer's address that is
286 * encountering the change of state.
288 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
290 /* Map ipv4 address into v4-mapped-on-v6 address. */
291 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
292 sctp_sk(asoc->base.sk),
293 (union sctp_addr *)&spc->spc_aaddr);
301 /* Create and initialize an SCTP_REMOTE_ERROR notification.
303 * Note: This assumes that the chunk->skb->data already points to the
304 * operation error payload.
306 * Socket Extensions for SCTP - draft-01
307 * 5.3.1.3 SCTP_REMOTE_ERROR
309 * A remote peer may send an Operational Error message to its peer.
310 * This message indicates a variety of error conditions on an
311 * association. The entire error TLV as it appears on the wire is
312 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
313 * specification [SCTP] and any extensions for a list of possible
316 struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
317 const struct sctp_association *asoc, struct sctp_chunk *chunk,
318 __u16 flags, int gfp)
320 struct sctp_ulpevent *event;
321 struct sctp_remote_error *sre;
327 ch = (sctp_errhdr_t *)(chunk->skb->data);
329 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
331 /* Pull off the ERROR header. */
332 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
334 /* Copy the skb to a new skb with room for us to prepend
337 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
340 /* Pull off the rest of the cause TLV from the chunk. */
341 skb_pull(chunk->skb, elen);
345 /* Embed the event fields inside the cloned skb. */
346 event = sctp_skb2event(skb);
347 sctp_ulpevent_init(event, MSG_NOTIFICATION);
349 sre = (struct sctp_remote_error *)
350 skb_push(skb, sizeof(struct sctp_remote_error));
352 /* Trim the buffer to the right length. */
353 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
355 /* Socket Extensions for SCTP
356 * 5.3.1.3 SCTP_REMOTE_ERROR
359 * It should be SCTP_REMOTE_ERROR.
361 sre->sre_type = SCTP_REMOTE_ERROR;
364 * Socket Extensions for SCTP
365 * 5.3.1.3 SCTP_REMOTE_ERROR
367 * sre_flags: 16 bits (unsigned integer)
372 /* Socket Extensions for SCTP
373 * 5.3.1.3 SCTP_REMOTE_ERROR
375 * sre_length: sizeof (__u32)
377 * This field is the total length of the notification data,
378 * including the notification header.
380 sre->sre_length = skb->len;
382 /* Socket Extensions for SCTP
383 * 5.3.1.3 SCTP_REMOTE_ERROR
385 * sre_error: 16 bits (unsigned integer)
386 * This value represents one of the Operational Error causes defined in
387 * the SCTP specification, in network byte order.
389 sre->sre_error = cause;
391 /* Socket Extensions for SCTP
392 * 5.3.1.3 SCTP_REMOTE_ERROR
394 * sre_assoc_id: sizeof (sctp_assoc_t)
396 * The association id field, holds the identifier for the association.
397 * All notifications for a given association have the same association
398 * identifier. For TCP style socket, this field is ignored.
400 sctp_ulpevent_set_owner(event, asoc);
401 sre->sre_assoc_id = sctp_assoc2id(asoc);
409 /* Create and initialize a SCTP_SEND_FAILED notification.
411 * Socket Extensions for SCTP - draft-01
412 * 5.3.1.4 SCTP_SEND_FAILED
414 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
415 const struct sctp_association *asoc, struct sctp_chunk *chunk,
416 __u16 flags, __u32 error, int gfp)
418 struct sctp_ulpevent *event;
419 struct sctp_send_failed *ssf;
422 /* Pull off any padding. */
423 int len = ntohs(chunk->chunk_hdr->length);
425 /* Make skb with more room so we can prepend notification. */
426 skb = skb_copy_expand(chunk->skb,
427 sizeof(struct sctp_send_failed), /* headroom */
433 /* Pull off the common chunk header and DATA header. */
434 skb_pull(skb, sizeof(struct sctp_data_chunk));
435 len -= sizeof(struct sctp_data_chunk);
437 /* Embed the event fields inside the cloned skb. */
438 event = sctp_skb2event(skb);
439 sctp_ulpevent_init(event, MSG_NOTIFICATION);
441 ssf = (struct sctp_send_failed *)
442 skb_push(skb, sizeof(struct sctp_send_failed));
444 /* Socket Extensions for SCTP
445 * 5.3.1.4 SCTP_SEND_FAILED
448 * It should be SCTP_SEND_FAILED.
450 ssf->ssf_type = SCTP_SEND_FAILED;
452 /* Socket Extensions for SCTP
453 * 5.3.1.4 SCTP_SEND_FAILED
455 * ssf_flags: 16 bits (unsigned integer)
456 * The flag value will take one of the following values
458 * SCTP_DATA_UNSENT - Indicates that the data was never put on
461 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
462 * Note that this does not necessarily mean that the
463 * data was (or was not) successfully delivered.
465 ssf->ssf_flags = flags;
467 /* Socket Extensions for SCTP
468 * 5.3.1.4 SCTP_SEND_FAILED
470 * ssf_length: sizeof (__u32)
471 * This field is the total length of the notification data, including
472 * the notification header.
474 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
475 skb_trim(skb, ssf->ssf_length);
477 /* Socket Extensions for SCTP
478 * 5.3.1.4 SCTP_SEND_FAILED
480 * ssf_error: 16 bits (unsigned integer)
481 * This value represents the reason why the send failed, and if set,
482 * will be a SCTP protocol error code as defined in [SCTP] section
485 ssf->ssf_error = error;
487 /* Socket Extensions for SCTP
488 * 5.3.1.4 SCTP_SEND_FAILED
490 * ssf_info: sizeof (struct sctp_sndrcvinfo)
491 * The original send information associated with the undelivered
494 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
496 /* Per TSVWG discussion with Randy. Allow the application to
497 * ressemble a fragmented message.
499 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
501 /* Socket Extensions for SCTP
502 * 5.3.1.4 SCTP_SEND_FAILED
504 * ssf_assoc_id: sizeof (sctp_assoc_t)
505 * The association id field, sf_assoc_id, holds the identifier for the
506 * association. All notifications for a given association have the
507 * same association identifier. For TCP style socket, this field is
510 sctp_ulpevent_set_owner(event, asoc);
511 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
518 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
520 * Socket Extensions for SCTP - draft-01
521 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
523 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
524 const struct sctp_association *asoc,
525 __u16 flags, int gfp)
527 struct sctp_ulpevent *event;
528 struct sctp_shutdown_event *sse;
531 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
532 MSG_NOTIFICATION, gfp);
536 skb = sctp_event2skb(event);
537 sse = (struct sctp_shutdown_event *)
538 skb_put(skb, sizeof(struct sctp_shutdown_event));
540 /* Socket Extensions for SCTP
541 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
544 * It should be SCTP_SHUTDOWN_EVENT
546 sse->sse_type = SCTP_SHUTDOWN_EVENT;
548 /* Socket Extensions for SCTP
549 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
551 * sse_flags: 16 bits (unsigned integer)
556 /* Socket Extensions for SCTP
557 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
559 * sse_length: sizeof (__u32)
560 * This field is the total length of the notification data, including
561 * the notification header.
563 sse->sse_length = sizeof(struct sctp_shutdown_event);
565 /* Socket Extensions for SCTP
566 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
568 * sse_assoc_id: sizeof (sctp_assoc_t)
569 * The association id field, holds the identifier for the association.
570 * All notifications for a given association have the same association
571 * identifier. For TCP style socket, this field is ignored.
573 sctp_ulpevent_set_owner(event, asoc);
574 sse->sse_assoc_id = sctp_assoc2id(asoc);
582 /* A message has been received. Package this message as a notification
583 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
584 * even if filtered out later.
586 * Socket Extensions for SCTP
587 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
589 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
590 struct sctp_chunk *chunk,
593 struct sctp_ulpevent *event = NULL;
597 /* Clone the original skb, sharing the data. */
598 skb = skb_clone(chunk->skb, gfp);
602 /* First calculate the padding, so we don't inadvertently
603 * pass up the wrong length to the user.
605 * RFC 2960 - Section 3.2 Chunk Field Descriptions
607 * The total length of a chunk(including Type, Length and Value fields)
608 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
609 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
610 * bytes and this padding is not included in the chunk length field.
611 * The sender should never pad with more than 3 bytes. The receiver
612 * MUST ignore the padding bytes.
614 len = ntohs(chunk->chunk_hdr->length);
615 padding = WORD_ROUND(len) - len;
617 /* Fixup cloned skb with just this chunks data. */
618 skb_trim(skb, chunk->chunk_end - padding - skb->data);
620 /* Embed the event fields inside the cloned skb. */
621 event = sctp_skb2event(skb);
623 /* Initialize event with flags 0. */
624 sctp_ulpevent_init(event, 0);
626 sctp_ulpevent_receive_data(event, asoc);
628 event->stream = ntohs(chunk->subh.data_hdr->stream);
629 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
630 event->ppid = chunk->subh.data_hdr->ppid;
631 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
632 event->flags |= MSG_UNORDERED;
633 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
635 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
636 event->msg_flags |= chunk->chunk_hdr->flags;
637 event->iif = sctp_chunk_iif(chunk);
643 /* Create a partial delivery related event.
645 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
647 * When a reciever is engaged in a partial delivery of a
648 * message this notification will be used to inidicate
651 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
652 const struct sctp_association *asoc, __u32 indication, int gfp)
654 struct sctp_ulpevent *event;
655 struct sctp_pdapi_event *pd;
658 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
659 MSG_NOTIFICATION, gfp);
663 skb = sctp_event2skb(event);
664 pd = (struct sctp_pdapi_event *)
665 skb_put(skb, sizeof(struct sctp_pdapi_event));
668 * It should be SCTP_PARTIAL_DELIVERY_EVENT
670 * pdapi_flags: 16 bits (unsigned integer)
673 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
676 /* pdapi_length: 32 bits (unsigned integer)
678 * This field is the total length of the notification data, including
679 * the notification header. It will generally be sizeof (struct
682 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
684 /* pdapi_indication: 32 bits (unsigned integer)
686 * This field holds the indication being sent to the application.
688 pd->pdapi_indication = indication;
690 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
692 * The association id field, holds the identifier for the association.
694 sctp_ulpevent_set_owner(event, asoc);
695 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
702 /* Return the notification type, assuming this is a notification
705 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
707 union sctp_notification *notification;
710 skb = sctp_event2skb((struct sctp_ulpevent *)event);
711 notification = (union sctp_notification *) skb->data;
712 return notification->sn_header.sn_type;
715 /* Copy out the sndrcvinfo into a msghdr. */
716 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
717 struct msghdr *msghdr)
719 struct sctp_sndrcvinfo sinfo;
721 if (sctp_ulpevent_is_notification(event))
724 /* Sockets API Extensions for SCTP
725 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
727 * sinfo_stream: 16 bits (unsigned integer)
729 * For recvmsg() the SCTP stack places the message's stream number in
732 sinfo.sinfo_stream = event->stream;
733 /* sinfo_ssn: 16 bits (unsigned integer)
735 * For recvmsg() this value contains the stream sequence number that
736 * the remote endpoint placed in the DATA chunk. For fragmented
737 * messages this is the same number for all deliveries of the message
738 * (if more than one recvmsg() is needed to read the message).
740 sinfo.sinfo_ssn = event->ssn;
741 /* sinfo_ppid: 32 bits (unsigned integer)
743 * In recvmsg() this value is
744 * the same information that was passed by the upper layer in the peer
745 * application. Please note that byte order issues are NOT accounted
746 * for and this information is passed opaquely by the SCTP stack from
747 * one end to the other.
749 sinfo.sinfo_ppid = event->ppid;
750 /* sinfo_flags: 16 bits (unsigned integer)
752 * This field may contain any of the following flags and is composed of
753 * a bitwise OR of these values.
757 * MSG_UNORDERED - This flag is present when the message was sent
760 sinfo.sinfo_flags = event->flags;
761 /* sinfo_tsn: 32 bit (unsigned integer)
763 * For the receiving side, this field holds a TSN that was
764 * assigned to one of the SCTP Data Chunks.
766 sinfo.sinfo_tsn = event->tsn;
767 /* sinfo_cumtsn: 32 bit (unsigned integer)
769 * This field will hold the current cumulative TSN as
770 * known by the underlying SCTP layer. Note this field is
771 * ignored when sending and only valid for a receive
772 * operation when sinfo_flags are set to MSG_UNORDERED.
774 sinfo.sinfo_cumtsn = event->cumtsn;
775 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
777 * The association handle field, sinfo_assoc_id, holds the identifier
778 * for the association announced in the COMMUNICATION_UP notification.
779 * All notifications for a given association have the same identifier.
780 * Ignored for one-to-one style sockets.
782 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
784 /* These fields are not used while receiving. */
785 sinfo.sinfo_context = 0;
786 sinfo.sinfo_timetolive = 0;
788 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
789 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
792 /* Stub skb destructor. */
793 static void sctp_stub_rfree(struct sk_buff *skb)
795 /* WARNING: This function is just a warning not to use the
796 * skb destructor. If the skb is shared, we may get the destructor
797 * callback on some processor that does not own the sock_lock. This
798 * was occuring with PACKET socket applications that were monitoring
799 * our skbs. We can't take the sock_lock, because we can't risk
800 * recursing if we do really own the sock lock. Instead, do all
801 * of our rwnd manipulation while we own the sock_lock outright.
805 /* Hold the association in case the msg_name needs read out of
808 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
809 const struct sctp_association *asoc)
813 /* Cast away the const, as we are just wanting to
814 * bump the reference count.
816 sctp_association_hold((struct sctp_association *)asoc);
817 skb = sctp_event2skb(event);
818 skb->sk = asoc->base.sk;
819 event->asoc = (struct sctp_association *)asoc;
820 skb->destructor = sctp_stub_rfree;
823 /* A simple destructor to give up the reference to the association. */
824 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
826 sctp_association_put(event->asoc);
829 /* Do accounting for bytes received and hold a reference to the association
832 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
833 struct sctp_association *asoc)
835 struct sk_buff *skb, *frag;
837 skb = sctp_event2skb(event);
838 /* Set the owner and charge rwnd for bytes received. */
839 sctp_ulpevent_set_owner(event, asoc);
840 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
845 /* Note: Not clearing the entire event struct as this is just a
846 * fragment of the real event. However, we still need to do rwnd
848 * In general, the skb passed from IP can have only 1 level of
849 * fragments. But we allow multiple levels of fragments.
851 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
852 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
856 /* Do accounting for bytes just read by user and release the references to
859 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
861 struct sk_buff *skb, *frag;
863 /* Current stack structures assume that the rcv buffer is
864 * per socket. For UDP style sockets this is not true as
865 * multiple associations may be on a single UDP-style socket.
866 * Use the local private area of the skb to track the owning
870 skb = sctp_event2skb(event);
871 sctp_assoc_rwnd_increase(event->asoc, skb_headlen(skb));
876 /* Don't forget the fragments. */
877 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
878 /* NOTE: skb_shinfos are recursive. Although IP returns
879 * skb's with only 1 level of fragments, SCTP reassembly can
880 * increase the levels.
882 sctp_ulpevent_release_data(sctp_skb2event(frag));
886 sctp_ulpevent_release_owner(event);
889 /* Free a ulpevent that has an owner. It includes releasing the reference
890 * to the owner, updating the rwnd in case of a DATA event and freeing the
892 * See comments in sctp_stub_rfree().
894 void sctp_ulpevent_free(struct sctp_ulpevent *event)
896 if (sctp_ulpevent_is_notification(event))
897 sctp_ulpevent_release_owner(event);
899 sctp_ulpevent_release_data(event);
901 kfree_skb(sctp_event2skb(event));
904 /* Purge the skb lists holding ulpevents. */
905 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
908 while ((skb = skb_dequeue(list)) != NULL)
909 sctp_ulpevent_free(sctp_skb2event(skb));