[TFRC]: Rename dccp_rx_ to tfrc_rx_
[linux-2.6.git] / net / sctp / ulpevent.c
blob307314356e1662317c25be57050f79f366a9395e
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;
688 int rx_count;
691 * check to see if we need to make space for this
692 * new skb, expand the rcvbuffer if needed, or drop
693 * the frame
695 if (asoc->ep->rcvbuf_policy)
696 rx_count = atomic_read(&asoc->rmem_alloc);
697 else
698 rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
700 if (rx_count >= asoc->base.sk->sk_rcvbuf) {
702 if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
703 (!sk_stream_rmem_schedule(asoc->base.sk, chunk->skb)))
704 goto fail;
707 /* Clone the original skb, sharing the data. */
708 skb = skb_clone(chunk->skb, gfp);
709 if (!skb)
710 goto fail;
712 /* First calculate the padding, so we don't inadvertently
713 * pass up the wrong length to the user.
715 * RFC 2960 - Section 3.2 Chunk Field Descriptions
717 * The total length of a chunk(including Type, Length and Value fields)
718 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
719 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
720 * bytes and this padding is not included in the chunk length field.
721 * The sender should never pad with more than 3 bytes. The receiver
722 * MUST ignore the padding bytes.
724 len = ntohs(chunk->chunk_hdr->length);
725 padding = WORD_ROUND(len) - len;
727 /* Fixup cloned skb with just this chunks data. */
728 skb_trim(skb, chunk->chunk_end - padding - skb->data);
730 /* Embed the event fields inside the cloned skb. */
731 event = sctp_skb2event(skb);
733 /* Initialize event with flags 0 and correct length
734 * Since this is a clone of the original skb, only account for
735 * the data of this chunk as other chunks will be accounted separately.
737 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
739 sctp_ulpevent_receive_data(event, asoc);
741 event->stream = ntohs(chunk->subh.data_hdr->stream);
742 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
743 event->ppid = chunk->subh.data_hdr->ppid;
744 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
745 event->flags |= SCTP_UNORDERED;
746 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
748 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
749 event->msg_flags |= chunk->chunk_hdr->flags;
750 event->iif = sctp_chunk_iif(chunk);
752 fail:
753 return event;
756 /* Create a partial delivery related event.
758 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
760 * When a receiver is engaged in a partial delivery of a
761 * message this notification will be used to indicate
762 * various events.
764 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
765 const struct sctp_association *asoc, __u32 indication,
766 gfp_t gfp)
768 struct sctp_ulpevent *event;
769 struct sctp_pdapi_event *pd;
770 struct sk_buff *skb;
772 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
773 MSG_NOTIFICATION, gfp);
774 if (!event)
775 goto fail;
777 skb = sctp_event2skb(event);
778 pd = (struct sctp_pdapi_event *)
779 skb_put(skb, sizeof(struct sctp_pdapi_event));
781 /* pdapi_type
782 * It should be SCTP_PARTIAL_DELIVERY_EVENT
784 * pdapi_flags: 16 bits (unsigned integer)
785 * Currently unused.
787 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
788 pd->pdapi_flags = 0;
790 /* pdapi_length: 32 bits (unsigned integer)
792 * This field is the total length of the notification data, including
793 * the notification header. It will generally be sizeof (struct
794 * sctp_pdapi_event).
796 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
798 /* pdapi_indication: 32 bits (unsigned integer)
800 * This field holds the indication being sent to the application.
802 pd->pdapi_indication = indication;
804 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
806 * The association id field, holds the identifier for the association.
808 sctp_ulpevent_set_owner(event, asoc);
809 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
811 return event;
812 fail:
813 return NULL;
816 struct sctp_ulpevent *sctp_ulpevent_make_authkey(
817 const struct sctp_association *asoc, __u16 key_id,
818 __u32 indication, gfp_t gfp)
820 struct sctp_ulpevent *event;
821 struct sctp_authkey_event *ak;
822 struct sk_buff *skb;
824 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
825 MSG_NOTIFICATION, gfp);
826 if (!event)
827 goto fail;
829 skb = sctp_event2skb(event);
830 ak = (struct sctp_authkey_event *)
831 skb_put(skb, sizeof(struct sctp_authkey_event));
833 ak->auth_type = SCTP_AUTHENTICATION_INDICATION;
834 ak->auth_flags = 0;
835 ak->auth_length = sizeof(struct sctp_authkey_event);
837 ak->auth_keynumber = key_id;
838 ak->auth_altkeynumber = 0;
839 ak->auth_indication = indication;
842 * The association id field, holds the identifier for the association.
844 sctp_ulpevent_set_owner(event, asoc);
845 ak->auth_assoc_id = sctp_assoc2id(asoc);
847 return event;
848 fail:
849 return NULL;
853 /* Return the notification type, assuming this is a notification
854 * event.
856 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
858 union sctp_notification *notification;
859 struct sk_buff *skb;
861 skb = sctp_event2skb((struct sctp_ulpevent *)event);
862 notification = (union sctp_notification *) skb->data;
863 return notification->sn_header.sn_type;
866 /* Copy out the sndrcvinfo into a msghdr. */
867 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
868 struct msghdr *msghdr)
870 struct sctp_sndrcvinfo sinfo;
872 if (sctp_ulpevent_is_notification(event))
873 return;
875 /* Sockets API Extensions for SCTP
876 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
878 * sinfo_stream: 16 bits (unsigned integer)
880 * For recvmsg() the SCTP stack places the message's stream number in
881 * this value.
883 sinfo.sinfo_stream = event->stream;
884 /* sinfo_ssn: 16 bits (unsigned integer)
886 * For recvmsg() this value contains the stream sequence number that
887 * the remote endpoint placed in the DATA chunk. For fragmented
888 * messages this is the same number for all deliveries of the message
889 * (if more than one recvmsg() is needed to read the message).
891 sinfo.sinfo_ssn = event->ssn;
892 /* sinfo_ppid: 32 bits (unsigned integer)
894 * In recvmsg() this value is
895 * the same information that was passed by the upper layer in the peer
896 * application. Please note that byte order issues are NOT accounted
897 * for and this information is passed opaquely by the SCTP stack from
898 * one end to the other.
900 sinfo.sinfo_ppid = event->ppid;
901 /* sinfo_flags: 16 bits (unsigned integer)
903 * This field may contain any of the following flags and is composed of
904 * a bitwise OR of these values.
906 * recvmsg() flags:
908 * SCTP_UNORDERED - This flag is present when the message was sent
909 * non-ordered.
911 sinfo.sinfo_flags = event->flags;
912 /* sinfo_tsn: 32 bit (unsigned integer)
914 * For the receiving side, this field holds a TSN that was
915 * assigned to one of the SCTP Data Chunks.
917 sinfo.sinfo_tsn = event->tsn;
918 /* sinfo_cumtsn: 32 bit (unsigned integer)
920 * This field will hold the current cumulative TSN as
921 * known by the underlying SCTP layer. Note this field is
922 * ignored when sending and only valid for a receive
923 * operation when sinfo_flags are set to SCTP_UNORDERED.
925 sinfo.sinfo_cumtsn = event->cumtsn;
926 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
928 * The association handle field, sinfo_assoc_id, holds the identifier
929 * for the association announced in the COMMUNICATION_UP notification.
930 * All notifications for a given association have the same identifier.
931 * Ignored for one-to-one style sockets.
933 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
935 /* context value that is set via SCTP_CONTEXT socket option. */
936 sinfo.sinfo_context = event->asoc->default_rcv_context;
938 /* These fields are not used while receiving. */
939 sinfo.sinfo_timetolive = 0;
941 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
942 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
945 /* Do accounting for bytes received and hold a reference to the association
946 * for each skb.
948 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
949 struct sctp_association *asoc)
951 struct sk_buff *skb, *frag;
953 skb = sctp_event2skb(event);
954 /* Set the owner and charge rwnd for bytes received. */
955 sctp_ulpevent_set_owner(event, asoc);
956 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
958 if (!skb->data_len)
959 return;
961 /* Note: Not clearing the entire event struct as this is just a
962 * fragment of the real event. However, we still need to do rwnd
963 * accounting.
964 * In general, the skb passed from IP can have only 1 level of
965 * fragments. But we allow multiple levels of fragments.
967 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
968 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
972 /* Do accounting for bytes just read by user and release the references to
973 * the association.
975 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
977 struct sk_buff *skb, *frag;
978 unsigned int len;
980 /* Current stack structures assume that the rcv buffer is
981 * per socket. For UDP style sockets this is not true as
982 * multiple associations may be on a single UDP-style socket.
983 * Use the local private area of the skb to track the owning
984 * association.
987 skb = sctp_event2skb(event);
988 len = skb->len;
990 if (!skb->data_len)
991 goto done;
993 /* Don't forget the fragments. */
994 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
995 /* NOTE: skb_shinfos are recursive. Although IP returns
996 * skb's with only 1 level of fragments, SCTP reassembly can
997 * increase the levels.
999 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1002 done:
1003 sctp_assoc_rwnd_increase(event->asoc, len);
1004 sctp_ulpevent_release_owner(event);
1007 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1009 struct sk_buff *skb, *frag;
1011 skb = sctp_event2skb(event);
1013 if (!skb->data_len)
1014 goto done;
1016 /* Don't forget the fragments. */
1017 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
1018 /* NOTE: skb_shinfos are recursive. Although IP returns
1019 * skb's with only 1 level of fragments, SCTP reassembly can
1020 * increase the levels.
1022 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1025 done:
1026 sctp_ulpevent_release_owner(event);
1029 /* Free a ulpevent that has an owner. It includes releasing the reference
1030 * to the owner, updating the rwnd in case of a DATA event and freeing the
1031 * skb.
1033 void sctp_ulpevent_free(struct sctp_ulpevent *event)
1035 if (sctp_ulpevent_is_notification(event))
1036 sctp_ulpevent_release_owner(event);
1037 else
1038 sctp_ulpevent_release_data(event);
1040 kfree_skb(sctp_event2skb(event));
1043 /* Purge the skb lists holding ulpevents. */
1044 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1046 struct sk_buff *skb;
1047 while ((skb = skb_dequeue(list)) != NULL)
1048 sctp_ulpevent_free(sctp_skb2event(skb));