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
, struct sctp_chunk
*chunk
, gfp_t gfp
)
136 struct sctp_ulpevent
*event
;
137 struct sctp_assoc_change
*sac
;
140 /* If the lower layer passed in the chunk, it will be
141 * an ABORT, so we need to include it in the sac_info.
144 /* sctp_inqu_pop() has allready pulled off the chunk
145 * header. We need to put it back temporarily
147 skb_push(chunk
->skb
, sizeof(sctp_chunkhdr_t
));
149 /* Copy the chunk data to a new skb and reserve enough
150 * head room to use as notification.
152 skb
= skb_copy_expand(chunk
->skb
,
153 sizeof(struct sctp_assoc_change
), 0, gfp
);
158 /* put back the chunk header now that we have a copy */
159 skb_pull(chunk
->skb
, sizeof(sctp_chunkhdr_t
));
161 /* Embed the event fields inside the cloned skb. */
162 event
= sctp_skb2event(skb
);
163 sctp_ulpevent_init(event
, MSG_NOTIFICATION
, skb
->truesize
);
165 /* Include the notification structure */
166 sac
= (struct sctp_assoc_change
*)
167 skb_push(skb
, sizeof(struct sctp_assoc_change
));
169 /* Trim the buffer to the right length. */
170 skb_trim(skb
, sizeof(struct sctp_assoc_change
) +
171 ntohs(chunk
->chunk_hdr
->length
));
173 event
= sctp_ulpevent_new(sizeof(struct sctp_assoc_change
),
174 MSG_NOTIFICATION
, gfp
);
178 skb
= sctp_event2skb(event
);
179 sac
= (struct sctp_assoc_change
*) skb_put(skb
,
180 sizeof(struct sctp_assoc_change
));
183 /* Socket Extensions for SCTP
184 * 5.3.1.1 SCTP_ASSOC_CHANGE
187 * It should be SCTP_ASSOC_CHANGE.
189 sac
->sac_type
= SCTP_ASSOC_CHANGE
;
191 /* Socket Extensions for SCTP
192 * 5.3.1.1 SCTP_ASSOC_CHANGE
194 * sac_state: 32 bits (signed integer)
195 * This field holds one of a number of values that communicate the
196 * event that happened to the association.
198 sac
->sac_state
= state
;
200 /* Socket Extensions for SCTP
201 * 5.3.1.1 SCTP_ASSOC_CHANGE
203 * sac_flags: 16 bits (unsigned integer)
208 /* Socket Extensions for SCTP
209 * 5.3.1.1 SCTP_ASSOC_CHANGE
211 * sac_length: sizeof (__u32)
212 * This field is the total length of the notification data, including
213 * the notification header.
215 sac
->sac_length
= sizeof(struct sctp_assoc_change
);
217 /* Socket Extensions for SCTP
218 * 5.3.1.1 SCTP_ASSOC_CHANGE
220 * sac_error: 32 bits (signed integer)
222 * If the state was reached due to a error condition (e.g.
223 * COMMUNICATION_LOST) any relevant error information is available in
224 * this field. This corresponds to the protocol error codes defined in
227 sac
->sac_error
= error
;
229 /* Socket Extensions for SCTP
230 * 5.3.1.1 SCTP_ASSOC_CHANGE
232 * sac_outbound_streams: 16 bits (unsigned integer)
233 * sac_inbound_streams: 16 bits (unsigned integer)
235 * The maximum number of streams allowed in each direction are
236 * available in sac_outbound_streams and sac_inbound streams.
238 sac
->sac_outbound_streams
= outbound
;
239 sac
->sac_inbound_streams
= inbound
;
241 /* Socket Extensions for SCTP
242 * 5.3.1.1 SCTP_ASSOC_CHANGE
244 * sac_assoc_id: sizeof (sctp_assoc_t)
246 * The association id field, holds the identifier for the association.
247 * All notifications for a given association have the same association
248 * identifier. For TCP style socket, this field is ignored.
250 sctp_ulpevent_set_owner(event
, asoc
);
251 sac
->sac_assoc_id
= sctp_assoc2id(asoc
);
259 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
261 * Socket Extensions for SCTP - draft-01
262 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
264 * When a destination address on a multi-homed peer encounters a change
265 * an interface details event is sent.
267 struct sctp_ulpevent
*sctp_ulpevent_make_peer_addr_change(
268 const struct sctp_association
*asoc
,
269 const struct sockaddr_storage
*aaddr
,
270 int flags
, int state
, int error
, gfp_t gfp
)
272 struct sctp_ulpevent
*event
;
273 struct sctp_paddr_change
*spc
;
276 event
= sctp_ulpevent_new(sizeof(struct sctp_paddr_change
),
277 MSG_NOTIFICATION
, gfp
);
281 skb
= sctp_event2skb(event
);
282 spc
= (struct sctp_paddr_change
*)
283 skb_put(skb
, sizeof(struct sctp_paddr_change
));
285 /* Sockets API Extensions for SCTP
286 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
290 * It should be SCTP_PEER_ADDR_CHANGE.
292 spc
->spc_type
= SCTP_PEER_ADDR_CHANGE
;
294 /* Sockets API Extensions for SCTP
295 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
297 * spc_length: sizeof (__u32)
299 * This field is the total length of the notification data, including
300 * the notification header.
302 spc
->spc_length
= sizeof(struct sctp_paddr_change
);
304 /* Sockets API Extensions for SCTP
305 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
307 * spc_flags: 16 bits (unsigned integer)
312 /* Sockets API Extensions for SCTP
313 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
315 * spc_state: 32 bits (signed integer)
317 * This field holds one of a number of values that communicate the
318 * event that happened to the address.
320 spc
->spc_state
= state
;
322 /* Sockets API Extensions for SCTP
323 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
325 * spc_error: 32 bits (signed integer)
327 * If the state was reached due to any error condition (e.g.
328 * ADDRESS_UNREACHABLE) any relevant error information is available in
331 spc
->spc_error
= error
;
333 /* Socket Extensions for SCTP
334 * 5.3.1.1 SCTP_ASSOC_CHANGE
336 * spc_assoc_id: sizeof (sctp_assoc_t)
338 * The association id field, holds the identifier for the association.
339 * All notifications for a given association have the same association
340 * identifier. For TCP style socket, this field is ignored.
342 sctp_ulpevent_set_owner(event
, asoc
);
343 spc
->spc_assoc_id
= sctp_assoc2id(asoc
);
345 /* Sockets API Extensions for SCTP
346 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
348 * spc_aaddr: sizeof (struct sockaddr_storage)
350 * The affected address field, holds the remote peer's address that is
351 * encountering the change of state.
353 memcpy(&spc
->spc_aaddr
, aaddr
, sizeof(struct sockaddr_storage
));
355 /* Map ipv4 address into v4-mapped-on-v6 address. */
356 sctp_get_pf_specific(asoc
->base
.sk
->sk_family
)->addr_v4map(
357 sctp_sk(asoc
->base
.sk
),
358 (union sctp_addr
*)&spc
->spc_aaddr
);
366 /* Create and initialize an SCTP_REMOTE_ERROR notification.
368 * Note: This assumes that the chunk->skb->data already points to the
369 * operation error payload.
371 * Socket Extensions for SCTP - draft-01
372 * 5.3.1.3 SCTP_REMOTE_ERROR
374 * A remote peer may send an Operational Error message to its peer.
375 * This message indicates a variety of error conditions on an
376 * association. The entire error TLV as it appears on the wire is
377 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
378 * specification [SCTP] and any extensions for a list of possible
381 struct sctp_ulpevent
*sctp_ulpevent_make_remote_error(
382 const struct sctp_association
*asoc
, struct sctp_chunk
*chunk
,
383 __u16 flags
, gfp_t gfp
)
385 struct sctp_ulpevent
*event
;
386 struct sctp_remote_error
*sre
;
392 ch
= (sctp_errhdr_t
*)(chunk
->skb
->data
);
394 elen
= WORD_ROUND(ntohs(ch
->length
)) - sizeof(sctp_errhdr_t
);
396 /* Pull off the ERROR header. */
397 skb_pull(chunk
->skb
, sizeof(sctp_errhdr_t
));
399 /* Copy the skb to a new skb with room for us to prepend
402 skb
= skb_copy_expand(chunk
->skb
, sizeof(struct sctp_remote_error
),
405 /* Pull off the rest of the cause TLV from the chunk. */
406 skb_pull(chunk
->skb
, elen
);
410 /* Embed the event fields inside the cloned skb. */
411 event
= sctp_skb2event(skb
);
412 sctp_ulpevent_init(event
, MSG_NOTIFICATION
, skb
->truesize
);
414 sre
= (struct sctp_remote_error
*)
415 skb_push(skb
, sizeof(struct sctp_remote_error
));
417 /* Trim the buffer to the right length. */
418 skb_trim(skb
, sizeof(struct sctp_remote_error
) + elen
);
420 /* Socket Extensions for SCTP
421 * 5.3.1.3 SCTP_REMOTE_ERROR
424 * It should be SCTP_REMOTE_ERROR.
426 sre
->sre_type
= SCTP_REMOTE_ERROR
;
429 * Socket Extensions for SCTP
430 * 5.3.1.3 SCTP_REMOTE_ERROR
432 * sre_flags: 16 bits (unsigned integer)
437 /* Socket Extensions for SCTP
438 * 5.3.1.3 SCTP_REMOTE_ERROR
440 * sre_length: sizeof (__u32)
442 * This field is the total length of the notification data,
443 * including the notification header.
445 sre
->sre_length
= skb
->len
;
447 /* Socket Extensions for SCTP
448 * 5.3.1.3 SCTP_REMOTE_ERROR
450 * sre_error: 16 bits (unsigned integer)
451 * This value represents one of the Operational Error causes defined in
452 * the SCTP specification, in network byte order.
454 sre
->sre_error
= cause
;
456 /* Socket Extensions for SCTP
457 * 5.3.1.3 SCTP_REMOTE_ERROR
459 * sre_assoc_id: sizeof (sctp_assoc_t)
461 * The association id field, holds the identifier for the association.
462 * All notifications for a given association have the same association
463 * identifier. For TCP style socket, this field is ignored.
465 sctp_ulpevent_set_owner(event
, asoc
);
466 sre
->sre_assoc_id
= sctp_assoc2id(asoc
);
474 /* Create and initialize a SCTP_SEND_FAILED notification.
476 * Socket Extensions for SCTP - draft-01
477 * 5.3.1.4 SCTP_SEND_FAILED
479 struct sctp_ulpevent
*sctp_ulpevent_make_send_failed(
480 const struct sctp_association
*asoc
, struct sctp_chunk
*chunk
,
481 __u16 flags
, __u32 error
, gfp_t gfp
)
483 struct sctp_ulpevent
*event
;
484 struct sctp_send_failed
*ssf
;
487 /* Pull off any padding. */
488 int len
= ntohs(chunk
->chunk_hdr
->length
);
490 /* Make skb with more room so we can prepend notification. */
491 skb
= skb_copy_expand(chunk
->skb
,
492 sizeof(struct sctp_send_failed
), /* headroom */
498 /* Pull off the common chunk header and DATA header. */
499 skb_pull(skb
, sizeof(struct sctp_data_chunk
));
500 len
-= sizeof(struct sctp_data_chunk
);
502 /* Embed the event fields inside the cloned skb. */
503 event
= sctp_skb2event(skb
);
504 sctp_ulpevent_init(event
, MSG_NOTIFICATION
, skb
->truesize
);
506 ssf
= (struct sctp_send_failed
*)
507 skb_push(skb
, sizeof(struct sctp_send_failed
));
509 /* Socket Extensions for SCTP
510 * 5.3.1.4 SCTP_SEND_FAILED
513 * It should be SCTP_SEND_FAILED.
515 ssf
->ssf_type
= SCTP_SEND_FAILED
;
517 /* Socket Extensions for SCTP
518 * 5.3.1.4 SCTP_SEND_FAILED
520 * ssf_flags: 16 bits (unsigned integer)
521 * The flag value will take one of the following values
523 * SCTP_DATA_UNSENT - Indicates that the data was never put on
526 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
527 * Note that this does not necessarily mean that the
528 * data was (or was not) successfully delivered.
530 ssf
->ssf_flags
= flags
;
532 /* Socket Extensions for SCTP
533 * 5.3.1.4 SCTP_SEND_FAILED
535 * ssf_length: sizeof (__u32)
536 * This field is the total length of the notification data, including
537 * the notification header.
539 ssf
->ssf_length
= sizeof(struct sctp_send_failed
) + len
;
540 skb_trim(skb
, ssf
->ssf_length
);
542 /* Socket Extensions for SCTP
543 * 5.3.1.4 SCTP_SEND_FAILED
545 * ssf_error: 16 bits (unsigned integer)
546 * This value represents the reason why the send failed, and if set,
547 * will be a SCTP protocol error code as defined in [SCTP] section
550 ssf
->ssf_error
= error
;
552 /* Socket Extensions for SCTP
553 * 5.3.1.4 SCTP_SEND_FAILED
555 * ssf_info: sizeof (struct sctp_sndrcvinfo)
556 * The original send information associated with the undelivered
559 memcpy(&ssf
->ssf_info
, &chunk
->sinfo
, sizeof(struct sctp_sndrcvinfo
));
561 /* Per TSVWG discussion with Randy. Allow the application to
562 * ressemble a fragmented message.
564 ssf
->ssf_info
.sinfo_flags
= chunk
->chunk_hdr
->flags
;
566 /* Socket Extensions for SCTP
567 * 5.3.1.4 SCTP_SEND_FAILED
569 * ssf_assoc_id: sizeof (sctp_assoc_t)
570 * The association id field, sf_assoc_id, holds the identifier for the
571 * association. All notifications for a given association have the
572 * same association identifier. For TCP style socket, this field is
575 sctp_ulpevent_set_owner(event
, asoc
);
576 ssf
->ssf_assoc_id
= sctp_assoc2id(asoc
);
583 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
585 * Socket Extensions for SCTP - draft-01
586 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
588 struct sctp_ulpevent
*sctp_ulpevent_make_shutdown_event(
589 const struct sctp_association
*asoc
,
590 __u16 flags
, gfp_t gfp
)
592 struct sctp_ulpevent
*event
;
593 struct sctp_shutdown_event
*sse
;
596 event
= sctp_ulpevent_new(sizeof(struct sctp_shutdown_event
),
597 MSG_NOTIFICATION
, gfp
);
601 skb
= sctp_event2skb(event
);
602 sse
= (struct sctp_shutdown_event
*)
603 skb_put(skb
, sizeof(struct sctp_shutdown_event
));
605 /* Socket Extensions for SCTP
606 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
609 * It should be SCTP_SHUTDOWN_EVENT
611 sse
->sse_type
= SCTP_SHUTDOWN_EVENT
;
613 /* Socket Extensions for SCTP
614 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
616 * sse_flags: 16 bits (unsigned integer)
621 /* Socket Extensions for SCTP
622 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
624 * sse_length: sizeof (__u32)
625 * This field is the total length of the notification data, including
626 * the notification header.
628 sse
->sse_length
= sizeof(struct sctp_shutdown_event
);
630 /* Socket Extensions for SCTP
631 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
633 * sse_assoc_id: sizeof (sctp_assoc_t)
634 * The association id field, holds the identifier for the association.
635 * All notifications for a given association have the same association
636 * identifier. For TCP style socket, this field is ignored.
638 sctp_ulpevent_set_owner(event
, asoc
);
639 sse
->sse_assoc_id
= sctp_assoc2id(asoc
);
647 /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
649 * Socket Extensions for SCTP
650 * 5.3.1.6 SCTP_ADAPTATION_INDICATION
652 struct sctp_ulpevent
*sctp_ulpevent_make_adaptation_indication(
653 const struct sctp_association
*asoc
, gfp_t gfp
)
655 struct sctp_ulpevent
*event
;
656 struct sctp_adaptation_event
*sai
;
659 event
= sctp_ulpevent_new(sizeof(struct sctp_adaptation_event
),
660 MSG_NOTIFICATION
, gfp
);
664 skb
= sctp_event2skb(event
);
665 sai
= (struct sctp_adaptation_event
*)
666 skb_put(skb
, sizeof(struct sctp_adaptation_event
));
668 sai
->sai_type
= SCTP_ADAPTATION_INDICATION
;
670 sai
->sai_length
= sizeof(struct sctp_adaptation_event
);
671 sai
->sai_adaptation_ind
= asoc
->peer
.adaptation_ind
;
672 sctp_ulpevent_set_owner(event
, asoc
);
673 sai
->sai_assoc_id
= sctp_assoc2id(asoc
);
681 /* A message has been received. Package this message as a notification
682 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
683 * even if filtered out later.
685 * Socket Extensions for SCTP
686 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
688 struct sctp_ulpevent
*sctp_ulpevent_make_rcvmsg(struct sctp_association
*asoc
,
689 struct sctp_chunk
*chunk
,
692 struct sctp_ulpevent
*event
= NULL
;
696 /* Clone the original skb, sharing the data. */
697 skb
= skb_clone(chunk
->skb
, gfp
);
701 /* First calculate the padding, so we don't inadvertently
702 * pass up the wrong length to the user.
704 * RFC 2960 - Section 3.2 Chunk Field Descriptions
706 * The total length of a chunk(including Type, Length and Value fields)
707 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
708 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
709 * bytes and this padding is not included in the chunk length field.
710 * The sender should never pad with more than 3 bytes. The receiver
711 * MUST ignore the padding bytes.
713 len
= ntohs(chunk
->chunk_hdr
->length
);
714 padding
= WORD_ROUND(len
) - len
;
716 /* Fixup cloned skb with just this chunks data. */
717 skb_trim(skb
, chunk
->chunk_end
- padding
- skb
->data
);
719 /* Embed the event fields inside the cloned skb. */
720 event
= sctp_skb2event(skb
);
722 /* Initialize event with flags 0 and correct length
723 * Since this is a clone of the original skb, only account for
724 * the data of this chunk as other chunks will be accounted separately.
726 sctp_ulpevent_init(event
, 0, skb
->len
+ sizeof(struct sk_buff
));
728 sctp_ulpevent_receive_data(event
, asoc
);
730 event
->stream
= ntohs(chunk
->subh
.data_hdr
->stream
);
731 event
->ssn
= ntohs(chunk
->subh
.data_hdr
->ssn
);
732 event
->ppid
= chunk
->subh
.data_hdr
->ppid
;
733 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
) {
734 event
->flags
|= SCTP_UNORDERED
;
735 event
->cumtsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
);
737 event
->tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
738 event
->msg_flags
|= chunk
->chunk_hdr
->flags
;
739 event
->iif
= sctp_chunk_iif(chunk
);
745 /* Create a partial delivery related event.
747 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
749 * When a receiver is engaged in a partial delivery of a
750 * message this notification will be used to indicate
753 struct sctp_ulpevent
*sctp_ulpevent_make_pdapi(
754 const struct sctp_association
*asoc
, __u32 indication
,
757 struct sctp_ulpevent
*event
;
758 struct sctp_pdapi_event
*pd
;
761 event
= sctp_ulpevent_new(sizeof(struct sctp_pdapi_event
),
762 MSG_NOTIFICATION
, gfp
);
766 skb
= sctp_event2skb(event
);
767 pd
= (struct sctp_pdapi_event
*)
768 skb_put(skb
, sizeof(struct sctp_pdapi_event
));
771 * It should be SCTP_PARTIAL_DELIVERY_EVENT
773 * pdapi_flags: 16 bits (unsigned integer)
776 pd
->pdapi_type
= SCTP_PARTIAL_DELIVERY_EVENT
;
779 /* pdapi_length: 32 bits (unsigned integer)
781 * This field is the total length of the notification data, including
782 * the notification header. It will generally be sizeof (struct
785 pd
->pdapi_length
= sizeof(struct sctp_pdapi_event
);
787 /* pdapi_indication: 32 bits (unsigned integer)
789 * This field holds the indication being sent to the application.
791 pd
->pdapi_indication
= indication
;
793 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
795 * The association id field, holds the identifier for the association.
797 sctp_ulpevent_set_owner(event
, asoc
);
798 pd
->pdapi_assoc_id
= sctp_assoc2id(asoc
);
805 /* Return the notification type, assuming this is a notification
808 __u16
sctp_ulpevent_get_notification_type(const struct sctp_ulpevent
*event
)
810 union sctp_notification
*notification
;
813 skb
= sctp_event2skb((struct sctp_ulpevent
*)event
);
814 notification
= (union sctp_notification
*) skb
->data
;
815 return notification
->sn_header
.sn_type
;
818 /* Copy out the sndrcvinfo into a msghdr. */
819 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent
*event
,
820 struct msghdr
*msghdr
)
822 struct sctp_sndrcvinfo sinfo
;
824 if (sctp_ulpevent_is_notification(event
))
827 /* Sockets API Extensions for SCTP
828 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
830 * sinfo_stream: 16 bits (unsigned integer)
832 * For recvmsg() the SCTP stack places the message's stream number in
835 sinfo
.sinfo_stream
= event
->stream
;
836 /* sinfo_ssn: 16 bits (unsigned integer)
838 * For recvmsg() this value contains the stream sequence number that
839 * the remote endpoint placed in the DATA chunk. For fragmented
840 * messages this is the same number for all deliveries of the message
841 * (if more than one recvmsg() is needed to read the message).
843 sinfo
.sinfo_ssn
= event
->ssn
;
844 /* sinfo_ppid: 32 bits (unsigned integer)
846 * In recvmsg() this value is
847 * the same information that was passed by the upper layer in the peer
848 * application. Please note that byte order issues are NOT accounted
849 * for and this information is passed opaquely by the SCTP stack from
850 * one end to the other.
852 sinfo
.sinfo_ppid
= event
->ppid
;
853 /* sinfo_flags: 16 bits (unsigned integer)
855 * This field may contain any of the following flags and is composed of
856 * a bitwise OR of these values.
860 * SCTP_UNORDERED - This flag is present when the message was sent
863 sinfo
.sinfo_flags
= event
->flags
;
864 /* sinfo_tsn: 32 bit (unsigned integer)
866 * For the receiving side, this field holds a TSN that was
867 * assigned to one of the SCTP Data Chunks.
869 sinfo
.sinfo_tsn
= event
->tsn
;
870 /* sinfo_cumtsn: 32 bit (unsigned integer)
872 * This field will hold the current cumulative TSN as
873 * known by the underlying SCTP layer. Note this field is
874 * ignored when sending and only valid for a receive
875 * operation when sinfo_flags are set to SCTP_UNORDERED.
877 sinfo
.sinfo_cumtsn
= event
->cumtsn
;
878 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
880 * The association handle field, sinfo_assoc_id, holds the identifier
881 * for the association announced in the COMMUNICATION_UP notification.
882 * All notifications for a given association have the same identifier.
883 * Ignored for one-to-one style sockets.
885 sinfo
.sinfo_assoc_id
= sctp_assoc2id(event
->asoc
);
887 /* context value that is set via SCTP_CONTEXT socket option. */
888 sinfo
.sinfo_context
= event
->asoc
->default_rcv_context
;
890 /* These fields are not used while receiving. */
891 sinfo
.sinfo_timetolive
= 0;
893 put_cmsg(msghdr
, IPPROTO_SCTP
, SCTP_SNDRCV
,
894 sizeof(struct sctp_sndrcvinfo
), (void *)&sinfo
);
897 /* Do accounting for bytes received and hold a reference to the association
900 static void sctp_ulpevent_receive_data(struct sctp_ulpevent
*event
,
901 struct sctp_association
*asoc
)
903 struct sk_buff
*skb
, *frag
;
905 skb
= sctp_event2skb(event
);
906 /* Set the owner and charge rwnd for bytes received. */
907 sctp_ulpevent_set_owner(event
, asoc
);
908 sctp_assoc_rwnd_decrease(asoc
, skb_headlen(skb
));
913 /* Note: Not clearing the entire event struct as this is just a
914 * fragment of the real event. However, we still need to do rwnd
916 * In general, the skb passed from IP can have only 1 level of
917 * fragments. But we allow multiple levels of fragments.
919 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
) {
920 sctp_ulpevent_receive_data(sctp_skb2event(frag
), asoc
);
924 /* Do accounting for bytes just read by user and release the references to
927 static void sctp_ulpevent_release_data(struct sctp_ulpevent
*event
)
929 struct sk_buff
*skb
, *frag
;
932 /* Current stack structures assume that the rcv buffer is
933 * per socket. For UDP style sockets this is not true as
934 * multiple associations may be on a single UDP-style socket.
935 * Use the local private area of the skb to track the owning
939 skb
= sctp_event2skb(event
);
945 /* Don't forget the fragments. */
946 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
) {
947 /* NOTE: skb_shinfos are recursive. Although IP returns
948 * skb's with only 1 level of fragments, SCTP reassembly can
949 * increase the levels.
951 sctp_ulpevent_release_frag_data(sctp_skb2event(frag
));
955 sctp_assoc_rwnd_increase(event
->asoc
, len
);
956 sctp_ulpevent_release_owner(event
);
959 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent
*event
)
961 struct sk_buff
*skb
, *frag
;
963 skb
= sctp_event2skb(event
);
968 /* Don't forget the fragments. */
969 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
) {
970 /* NOTE: skb_shinfos are recursive. Although IP returns
971 * skb's with only 1 level of fragments, SCTP reassembly can
972 * increase the levels.
974 sctp_ulpevent_release_frag_data(sctp_skb2event(frag
));
978 sctp_ulpevent_release_owner(event
);
981 /* Free a ulpevent that has an owner. It includes releasing the reference
982 * to the owner, updating the rwnd in case of a DATA event and freeing the
985 void sctp_ulpevent_free(struct sctp_ulpevent
*event
)
987 if (sctp_ulpevent_is_notification(event
))
988 sctp_ulpevent_release_owner(event
);
990 sctp_ulpevent_release_data(event
);
992 kfree_skb(sctp_event2skb(event
));
995 /* Purge the skb lists holding ulpevents. */
996 void sctp_queue_purge_ulpevents(struct sk_buff_head
*list
)
999 while ((skb
= skb_dequeue(list
)) != NULL
)
1000 sctp_ulpevent_free(sctp_skb2event(skb
));