ALSA: fix ice1712 section mismatch
[usb.git] / net / sctp / ulpevent.c
blobbfecb353ab3da070f0b4784c6861eaef7de04d1c
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)
15 * any later version.
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
29 * email address(es):
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,
59 int msg_flags,
60 unsigned int len)
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,
69 gfp_t gfp)
71 struct sctp_ulpevent *event;
72 struct sk_buff *skb;
74 skb = alloc_skb(size, gfp);
75 if (!skb)
76 goto fail;
78 event = sctp_skb2event(skb);
79 sctp_ulpevent_init(event, msg_flags, skb->truesize);
81 return event;
83 fail:
84 return NULL;
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
94 * the association.
96 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
97 const struct sctp_association *asoc)
99 struct sk_buff *skb;
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
129 * zero'd out.
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;
138 struct sk_buff *skb;
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.
143 if (chunk) {
144 /* Copy the chunk data to a new skb and reserve enough
145 * head room to use as notification.
147 skb = skb_copy_expand(chunk->skb,
148 sizeof(struct sctp_assoc_change), 0, gfp);
150 if (!skb)
151 goto fail;
153 /* Embed the event fields inside the cloned skb. */
154 event = sctp_skb2event(skb);
155 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
157 /* Include the notification structure */
158 sac = (struct sctp_assoc_change *)
159 skb_push(skb, sizeof(struct sctp_assoc_change));
161 /* Trim the buffer to the right length. */
162 skb_trim(skb, sizeof(struct sctp_assoc_change) +
163 ntohs(chunk->chunk_hdr->length) -
164 sizeof(sctp_chunkhdr_t));
165 } else {
166 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
167 MSG_NOTIFICATION, gfp);
168 if (!event)
169 goto fail;
171 skb = sctp_event2skb(event);
172 sac = (struct sctp_assoc_change *) skb_put(skb,
173 sizeof(struct sctp_assoc_change));
176 /* Socket Extensions for SCTP
177 * 5.3.1.1 SCTP_ASSOC_CHANGE
179 * sac_type:
180 * It should be SCTP_ASSOC_CHANGE.
182 sac->sac_type = SCTP_ASSOC_CHANGE;
184 /* Socket Extensions for SCTP
185 * 5.3.1.1 SCTP_ASSOC_CHANGE
187 * sac_state: 32 bits (signed integer)
188 * This field holds one of a number of values that communicate the
189 * event that happened to the association.
191 sac->sac_state = state;
193 /* Socket Extensions for SCTP
194 * 5.3.1.1 SCTP_ASSOC_CHANGE
196 * sac_flags: 16 bits (unsigned integer)
197 * Currently unused.
199 sac->sac_flags = 0;
201 /* Socket Extensions for SCTP
202 * 5.3.1.1 SCTP_ASSOC_CHANGE
204 * sac_length: sizeof (__u32)
205 * This field is the total length of the notification data, including
206 * the notification header.
208 sac->sac_length = sizeof(struct sctp_assoc_change);
210 /* Socket Extensions for SCTP
211 * 5.3.1.1 SCTP_ASSOC_CHANGE
213 * sac_error: 32 bits (signed integer)
215 * If the state was reached due to a error condition (e.g.
216 * COMMUNICATION_LOST) any relevant error information is available in
217 * this field. This corresponds to the protocol error codes defined in
218 * [SCTP].
220 sac->sac_error = error;
222 /* Socket Extensions for SCTP
223 * 5.3.1.1 SCTP_ASSOC_CHANGE
225 * sac_outbound_streams: 16 bits (unsigned integer)
226 * sac_inbound_streams: 16 bits (unsigned integer)
228 * The maximum number of streams allowed in each direction are
229 * available in sac_outbound_streams and sac_inbound streams.
231 sac->sac_outbound_streams = outbound;
232 sac->sac_inbound_streams = inbound;
234 /* Socket Extensions for SCTP
235 * 5.3.1.1 SCTP_ASSOC_CHANGE
237 * sac_assoc_id: sizeof (sctp_assoc_t)
239 * The association id field, holds the identifier for the association.
240 * All notifications for a given association have the same association
241 * identifier. For TCP style socket, this field is ignored.
243 sctp_ulpevent_set_owner(event, asoc);
244 sac->sac_assoc_id = sctp_assoc2id(asoc);
246 return event;
248 fail:
249 return NULL;
252 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
254 * Socket Extensions for SCTP - draft-01
255 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
257 * When a destination address on a multi-homed peer encounters a change
258 * an interface details event is sent.
260 struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
261 const struct sctp_association *asoc,
262 const struct sockaddr_storage *aaddr,
263 int flags, int state, int error, gfp_t gfp)
265 struct sctp_ulpevent *event;
266 struct sctp_paddr_change *spc;
267 struct sk_buff *skb;
269 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
270 MSG_NOTIFICATION, gfp);
271 if (!event)
272 goto fail;
274 skb = sctp_event2skb(event);
275 spc = (struct sctp_paddr_change *)
276 skb_put(skb, sizeof(struct sctp_paddr_change));
278 /* Sockets API Extensions for SCTP
279 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
281 * spc_type:
283 * It should be SCTP_PEER_ADDR_CHANGE.
285 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
287 /* Sockets API Extensions for SCTP
288 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
290 * spc_length: sizeof (__u32)
292 * This field is the total length of the notification data, including
293 * the notification header.
295 spc->spc_length = sizeof(struct sctp_paddr_change);
297 /* Sockets API Extensions for SCTP
298 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
300 * spc_flags: 16 bits (unsigned integer)
301 * Currently unused.
303 spc->spc_flags = 0;
305 /* Sockets API Extensions for SCTP
306 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
308 * spc_state: 32 bits (signed integer)
310 * This field holds one of a number of values that communicate the
311 * event that happened to the address.
313 spc->spc_state = state;
315 /* Sockets API Extensions for SCTP
316 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
318 * spc_error: 32 bits (signed integer)
320 * If the state was reached due to any error condition (e.g.
321 * ADDRESS_UNREACHABLE) any relevant error information is available in
322 * this field.
324 spc->spc_error = error;
326 /* Socket Extensions for SCTP
327 * 5.3.1.1 SCTP_ASSOC_CHANGE
329 * spc_assoc_id: sizeof (sctp_assoc_t)
331 * The association id field, holds the identifier for the association.
332 * All notifications for a given association have the same association
333 * identifier. For TCP style socket, this field is ignored.
335 sctp_ulpevent_set_owner(event, asoc);
336 spc->spc_assoc_id = sctp_assoc2id(asoc);
338 /* Sockets API Extensions for SCTP
339 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
341 * spc_aaddr: sizeof (struct sockaddr_storage)
343 * The affected address field, holds the remote peer's address that is
344 * encountering the change of state.
346 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
348 /* Map ipv4 address into v4-mapped-on-v6 address. */
349 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
350 sctp_sk(asoc->base.sk),
351 (union sctp_addr *)&spc->spc_aaddr);
353 return event;
355 fail:
356 return NULL;
359 /* Create and initialize an SCTP_REMOTE_ERROR notification.
361 * Note: This assumes that the chunk->skb->data already points to the
362 * operation error payload.
364 * Socket Extensions for SCTP - draft-01
365 * 5.3.1.3 SCTP_REMOTE_ERROR
367 * A remote peer may send an Operational Error message to its peer.
368 * This message indicates a variety of error conditions on an
369 * association. The entire error TLV as it appears on the wire is
370 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
371 * specification [SCTP] and any extensions for a list of possible
372 * error formats.
374 struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
375 const struct sctp_association *asoc, struct sctp_chunk *chunk,
376 __u16 flags, gfp_t gfp)
378 struct sctp_ulpevent *event;
379 struct sctp_remote_error *sre;
380 struct sk_buff *skb;
381 sctp_errhdr_t *ch;
382 __be16 cause;
383 int elen;
385 ch = (sctp_errhdr_t *)(chunk->skb->data);
386 cause = ch->cause;
387 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
389 /* Pull off the ERROR header. */
390 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
392 /* Copy the skb to a new skb with room for us to prepend
393 * notification with.
395 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
396 0, gfp);
398 /* Pull off the rest of the cause TLV from the chunk. */
399 skb_pull(chunk->skb, elen);
400 if (!skb)
401 goto fail;
403 /* Embed the event fields inside the cloned skb. */
404 event = sctp_skb2event(skb);
405 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
407 sre = (struct sctp_remote_error *)
408 skb_push(skb, sizeof(struct sctp_remote_error));
410 /* Trim the buffer to the right length. */
411 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
413 /* Socket Extensions for SCTP
414 * 5.3.1.3 SCTP_REMOTE_ERROR
416 * sre_type:
417 * It should be SCTP_REMOTE_ERROR.
419 sre->sre_type = SCTP_REMOTE_ERROR;
422 * Socket Extensions for SCTP
423 * 5.3.1.3 SCTP_REMOTE_ERROR
425 * sre_flags: 16 bits (unsigned integer)
426 * Currently unused.
428 sre->sre_flags = 0;
430 /* Socket Extensions for SCTP
431 * 5.3.1.3 SCTP_REMOTE_ERROR
433 * sre_length: sizeof (__u32)
435 * This field is the total length of the notification data,
436 * including the notification header.
438 sre->sre_length = skb->len;
440 /* Socket Extensions for SCTP
441 * 5.3.1.3 SCTP_REMOTE_ERROR
443 * sre_error: 16 bits (unsigned integer)
444 * This value represents one of the Operational Error causes defined in
445 * the SCTP specification, in network byte order.
447 sre->sre_error = cause;
449 /* Socket Extensions for SCTP
450 * 5.3.1.3 SCTP_REMOTE_ERROR
452 * sre_assoc_id: sizeof (sctp_assoc_t)
454 * The association id field, holds the identifier for the association.
455 * All notifications for a given association have the same association
456 * identifier. For TCP style socket, this field is ignored.
458 sctp_ulpevent_set_owner(event, asoc);
459 sre->sre_assoc_id = sctp_assoc2id(asoc);
461 return event;
463 fail:
464 return NULL;
467 /* Create and initialize a SCTP_SEND_FAILED notification.
469 * Socket Extensions for SCTP - draft-01
470 * 5.3.1.4 SCTP_SEND_FAILED
472 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
473 const struct sctp_association *asoc, struct sctp_chunk *chunk,
474 __u16 flags, __u32 error, gfp_t gfp)
476 struct sctp_ulpevent *event;
477 struct sctp_send_failed *ssf;
478 struct sk_buff *skb;
480 /* Pull off any padding. */
481 int len = ntohs(chunk->chunk_hdr->length);
483 /* Make skb with more room so we can prepend notification. */
484 skb = skb_copy_expand(chunk->skb,
485 sizeof(struct sctp_send_failed), /* headroom */
486 0, /* tailroom */
487 gfp);
488 if (!skb)
489 goto fail;
491 /* Pull off the common chunk header and DATA header. */
492 skb_pull(skb, sizeof(struct sctp_data_chunk));
493 len -= sizeof(struct sctp_data_chunk);
495 /* Embed the event fields inside the cloned skb. */
496 event = sctp_skb2event(skb);
497 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
499 ssf = (struct sctp_send_failed *)
500 skb_push(skb, sizeof(struct sctp_send_failed));
502 /* Socket Extensions for SCTP
503 * 5.3.1.4 SCTP_SEND_FAILED
505 * ssf_type:
506 * It should be SCTP_SEND_FAILED.
508 ssf->ssf_type = SCTP_SEND_FAILED;
510 /* Socket Extensions for SCTP
511 * 5.3.1.4 SCTP_SEND_FAILED
513 * ssf_flags: 16 bits (unsigned integer)
514 * The flag value will take one of the following values
516 * SCTP_DATA_UNSENT - Indicates that the data was never put on
517 * the wire.
519 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
520 * Note that this does not necessarily mean that the
521 * data was (or was not) successfully delivered.
523 ssf->ssf_flags = flags;
525 /* Socket Extensions for SCTP
526 * 5.3.1.4 SCTP_SEND_FAILED
528 * ssf_length: sizeof (__u32)
529 * This field is the total length of the notification data, including
530 * the notification header.
532 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
533 skb_trim(skb, ssf->ssf_length);
535 /* Socket Extensions for SCTP
536 * 5.3.1.4 SCTP_SEND_FAILED
538 * ssf_error: 16 bits (unsigned integer)
539 * This value represents the reason why the send failed, and if set,
540 * will be a SCTP protocol error code as defined in [SCTP] section
541 * 3.3.10.
543 ssf->ssf_error = error;
545 /* Socket Extensions for SCTP
546 * 5.3.1.4 SCTP_SEND_FAILED
548 * ssf_info: sizeof (struct sctp_sndrcvinfo)
549 * The original send information associated with the undelivered
550 * message.
552 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
554 /* Per TSVWG discussion with Randy. Allow the application to
555 * ressemble a fragmented message.
557 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
559 /* Socket Extensions for SCTP
560 * 5.3.1.4 SCTP_SEND_FAILED
562 * ssf_assoc_id: sizeof (sctp_assoc_t)
563 * The association id field, sf_assoc_id, holds the identifier for the
564 * association. All notifications for a given association have the
565 * same association identifier. For TCP style socket, this field is
566 * ignored.
568 sctp_ulpevent_set_owner(event, asoc);
569 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
570 return event;
572 fail:
573 return NULL;
576 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
578 * Socket Extensions for SCTP - draft-01
579 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
581 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
582 const struct sctp_association *asoc,
583 __u16 flags, gfp_t gfp)
585 struct sctp_ulpevent *event;
586 struct sctp_shutdown_event *sse;
587 struct sk_buff *skb;
589 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
590 MSG_NOTIFICATION, gfp);
591 if (!event)
592 goto fail;
594 skb = sctp_event2skb(event);
595 sse = (struct sctp_shutdown_event *)
596 skb_put(skb, sizeof(struct sctp_shutdown_event));
598 /* Socket Extensions for SCTP
599 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
601 * sse_type
602 * It should be SCTP_SHUTDOWN_EVENT
604 sse->sse_type = SCTP_SHUTDOWN_EVENT;
606 /* Socket Extensions for SCTP
607 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
609 * sse_flags: 16 bits (unsigned integer)
610 * Currently unused.
612 sse->sse_flags = 0;
614 /* Socket Extensions for SCTP
615 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
617 * sse_length: sizeof (__u32)
618 * This field is the total length of the notification data, including
619 * the notification header.
621 sse->sse_length = sizeof(struct sctp_shutdown_event);
623 /* Socket Extensions for SCTP
624 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
626 * sse_assoc_id: sizeof (sctp_assoc_t)
627 * The association id field, holds the identifier for the association.
628 * All notifications for a given association have the same association
629 * identifier. For TCP style socket, this field is ignored.
631 sctp_ulpevent_set_owner(event, asoc);
632 sse->sse_assoc_id = sctp_assoc2id(asoc);
634 return event;
636 fail:
637 return NULL;
640 /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
642 * Socket Extensions for SCTP
643 * 5.3.1.6 SCTP_ADAPTATION_INDICATION
645 struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
646 const struct sctp_association *asoc, gfp_t gfp)
648 struct sctp_ulpevent *event;
649 struct sctp_adaptation_event *sai;
650 struct sk_buff *skb;
652 event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
653 MSG_NOTIFICATION, gfp);
654 if (!event)
655 goto fail;
657 skb = sctp_event2skb(event);
658 sai = (struct sctp_adaptation_event *)
659 skb_put(skb, sizeof(struct sctp_adaptation_event));
661 sai->sai_type = SCTP_ADAPTATION_INDICATION;
662 sai->sai_flags = 0;
663 sai->sai_length = sizeof(struct sctp_adaptation_event);
664 sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
665 sctp_ulpevent_set_owner(event, asoc);
666 sai->sai_assoc_id = sctp_assoc2id(asoc);
668 return event;
670 fail:
671 return NULL;
674 /* A message has been received. Package this message as a notification
675 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
676 * even if filtered out later.
678 * Socket Extensions for SCTP
679 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
681 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
682 struct sctp_chunk *chunk,
683 gfp_t gfp)
685 struct sctp_ulpevent *event = NULL;
686 struct sk_buff *skb;
687 size_t padding, len;
689 /* Clone the original skb, sharing the data. */
690 skb = skb_clone(chunk->skb, gfp);
691 if (!skb)
692 goto fail;
694 /* First calculate the padding, so we don't inadvertently
695 * pass up the wrong length to the user.
697 * RFC 2960 - Section 3.2 Chunk Field Descriptions
699 * The total length of a chunk(including Type, Length and Value fields)
700 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
701 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
702 * bytes and this padding is not included in the chunk length field.
703 * The sender should never pad with more than 3 bytes. The receiver
704 * MUST ignore the padding bytes.
706 len = ntohs(chunk->chunk_hdr->length);
707 padding = WORD_ROUND(len) - len;
709 /* Fixup cloned skb with just this chunks data. */
710 skb_trim(skb, chunk->chunk_end - padding - skb->data);
712 /* Embed the event fields inside the cloned skb. */
713 event = sctp_skb2event(skb);
715 /* Initialize event with flags 0 and correct length
716 * Since this is a clone of the original skb, only account for
717 * the data of this chunk as other chunks will be accounted separately.
719 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
721 sctp_ulpevent_receive_data(event, asoc);
723 event->stream = ntohs(chunk->subh.data_hdr->stream);
724 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
725 event->ppid = chunk->subh.data_hdr->ppid;
726 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
727 event->flags |= SCTP_UNORDERED;
728 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
730 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
731 event->msg_flags |= chunk->chunk_hdr->flags;
732 event->iif = sctp_chunk_iif(chunk);
734 fail:
735 return event;
738 /* Create a partial delivery related event.
740 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
742 * When a receiver is engaged in a partial delivery of a
743 * message this notification will be used to indicate
744 * various events.
746 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
747 const struct sctp_association *asoc, __u32 indication,
748 gfp_t gfp)
750 struct sctp_ulpevent *event;
751 struct sctp_pdapi_event *pd;
752 struct sk_buff *skb;
754 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
755 MSG_NOTIFICATION, gfp);
756 if (!event)
757 goto fail;
759 skb = sctp_event2skb(event);
760 pd = (struct sctp_pdapi_event *)
761 skb_put(skb, sizeof(struct sctp_pdapi_event));
763 /* pdapi_type
764 * It should be SCTP_PARTIAL_DELIVERY_EVENT
766 * pdapi_flags: 16 bits (unsigned integer)
767 * Currently unused.
769 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
770 pd->pdapi_flags = 0;
772 /* pdapi_length: 32 bits (unsigned integer)
774 * This field is the total length of the notification data, including
775 * the notification header. It will generally be sizeof (struct
776 * sctp_pdapi_event).
778 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
780 /* pdapi_indication: 32 bits (unsigned integer)
782 * This field holds the indication being sent to the application.
784 pd->pdapi_indication = indication;
786 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
788 * The association id field, holds the identifier for the association.
790 sctp_ulpevent_set_owner(event, asoc);
791 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
793 return event;
794 fail:
795 return NULL;
798 /* Return the notification type, assuming this is a notification
799 * event.
801 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
803 union sctp_notification *notification;
804 struct sk_buff *skb;
806 skb = sctp_event2skb((struct sctp_ulpevent *)event);
807 notification = (union sctp_notification *) skb->data;
808 return notification->sn_header.sn_type;
811 /* Copy out the sndrcvinfo into a msghdr. */
812 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
813 struct msghdr *msghdr)
815 struct sctp_sndrcvinfo sinfo;
817 if (sctp_ulpevent_is_notification(event))
818 return;
820 /* Sockets API Extensions for SCTP
821 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
823 * sinfo_stream: 16 bits (unsigned integer)
825 * For recvmsg() the SCTP stack places the message's stream number in
826 * this value.
828 sinfo.sinfo_stream = event->stream;
829 /* sinfo_ssn: 16 bits (unsigned integer)
831 * For recvmsg() this value contains the stream sequence number that
832 * the remote endpoint placed in the DATA chunk. For fragmented
833 * messages this is the same number for all deliveries of the message
834 * (if more than one recvmsg() is needed to read the message).
836 sinfo.sinfo_ssn = event->ssn;
837 /* sinfo_ppid: 32 bits (unsigned integer)
839 * In recvmsg() this value is
840 * the same information that was passed by the upper layer in the peer
841 * application. Please note that byte order issues are NOT accounted
842 * for and this information is passed opaquely by the SCTP stack from
843 * one end to the other.
845 sinfo.sinfo_ppid = event->ppid;
846 /* sinfo_flags: 16 bits (unsigned integer)
848 * This field may contain any of the following flags and is composed of
849 * a bitwise OR of these values.
851 * recvmsg() flags:
853 * SCTP_UNORDERED - This flag is present when the message was sent
854 * non-ordered.
856 sinfo.sinfo_flags = event->flags;
857 /* sinfo_tsn: 32 bit (unsigned integer)
859 * For the receiving side, this field holds a TSN that was
860 * assigned to one of the SCTP Data Chunks.
862 sinfo.sinfo_tsn = event->tsn;
863 /* sinfo_cumtsn: 32 bit (unsigned integer)
865 * This field will hold the current cumulative TSN as
866 * known by the underlying SCTP layer. Note this field is
867 * ignored when sending and only valid for a receive
868 * operation when sinfo_flags are set to SCTP_UNORDERED.
870 sinfo.sinfo_cumtsn = event->cumtsn;
871 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
873 * The association handle field, sinfo_assoc_id, holds the identifier
874 * for the association announced in the COMMUNICATION_UP notification.
875 * All notifications for a given association have the same identifier.
876 * Ignored for one-to-one style sockets.
878 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
880 /* context value that is set via SCTP_CONTEXT socket option. */
881 sinfo.sinfo_context = event->asoc->default_rcv_context;
883 /* These fields are not used while receiving. */
884 sinfo.sinfo_timetolive = 0;
886 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
887 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
890 /* Do accounting for bytes received and hold a reference to the association
891 * for each skb.
893 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
894 struct sctp_association *asoc)
896 struct sk_buff *skb, *frag;
898 skb = sctp_event2skb(event);
899 /* Set the owner and charge rwnd for bytes received. */
900 sctp_ulpevent_set_owner(event, asoc);
901 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
903 if (!skb->data_len)
904 return;
906 /* Note: Not clearing the entire event struct as this is just a
907 * fragment of the real event. However, we still need to do rwnd
908 * accounting.
909 * In general, the skb passed from IP can have only 1 level of
910 * fragments. But we allow multiple levels of fragments.
912 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
913 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
917 /* Do accounting for bytes just read by user and release the references to
918 * the association.
920 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
922 struct sk_buff *skb, *frag;
923 unsigned int len;
925 /* Current stack structures assume that the rcv buffer is
926 * per socket. For UDP style sockets this is not true as
927 * multiple associations may be on a single UDP-style socket.
928 * Use the local private area of the skb to track the owning
929 * association.
932 skb = sctp_event2skb(event);
933 len = skb->len;
935 if (!skb->data_len)
936 goto done;
938 /* Don't forget the fragments. */
939 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
940 /* NOTE: skb_shinfos are recursive. Although IP returns
941 * skb's with only 1 level of fragments, SCTP reassembly can
942 * increase the levels.
944 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
947 done:
948 sctp_assoc_rwnd_increase(event->asoc, len);
949 sctp_ulpevent_release_owner(event);
952 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
954 struct sk_buff *skb, *frag;
956 skb = sctp_event2skb(event);
958 if (!skb->data_len)
959 goto done;
961 /* Don't forget the fragments. */
962 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
963 /* NOTE: skb_shinfos are recursive. Although IP returns
964 * skb's with only 1 level of fragments, SCTP reassembly can
965 * increase the levels.
967 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
970 done:
971 sctp_ulpevent_release_owner(event);
974 /* Free a ulpevent that has an owner. It includes releasing the reference
975 * to the owner, updating the rwnd in case of a DATA event and freeing the
976 * skb.
978 void sctp_ulpevent_free(struct sctp_ulpevent *event)
980 if (sctp_ulpevent_is_notification(event))
981 sctp_ulpevent_release_owner(event);
982 else
983 sctp_ulpevent_release_data(event);
985 kfree_skb(sctp_event2skb(event));
988 /* Purge the skb lists holding ulpevents. */
989 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
991 struct sk_buff *skb;
992 while ((skb = skb_dequeue(list)) != NULL)
993 sctp_ulpevent_free(sctp_skb2event(skb));