perf tools: Fix libaudit test
[linux/fpc-iii.git] / net / sctp / ulpevent.c
blob81089ed654564db6367c27f174c9b8381557c512
1 /* SCTP kernel 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).
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <linux-sctp@vger.kernel.org>
33 * Written or modified by:
34 * Jon Grimm <jgrimm@us.ibm.com>
35 * La Monte H.P. Yarroll <piggy@acm.org>
36 * Ardelle Fan <ardelle.fan@intel.com>
37 * Sridhar Samudrala <sri@us.ibm.com>
40 #include <linux/slab.h>
41 #include <linux/types.h>
42 #include <linux/skbuff.h>
43 #include <net/sctp/structs.h>
44 #include <net/sctp/sctp.h>
45 #include <net/sctp/sm.h>
47 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
48 struct sctp_association *asoc);
49 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
50 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
53 /* Initialize an ULP event from an given skb. */
54 static void sctp_ulpevent_init(struct sctp_ulpevent *event,
55 int msg_flags,
56 unsigned int len)
58 memset(event, 0, sizeof(struct sctp_ulpevent));
59 event->msg_flags = msg_flags;
60 event->rmem_len = len;
63 /* Create a new sctp_ulpevent. */
64 static struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
65 gfp_t gfp)
67 struct sctp_ulpevent *event;
68 struct sk_buff *skb;
70 skb = alloc_skb(size, gfp);
71 if (!skb)
72 goto fail;
74 event = sctp_skb2event(skb);
75 sctp_ulpevent_init(event, msg_flags, skb->truesize);
77 return event;
79 fail:
80 return NULL;
83 /* Is this a MSG_NOTIFICATION? */
84 int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
86 return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
89 /* Hold the association in case the msg_name needs read out of
90 * the association.
92 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
93 const struct sctp_association *asoc)
95 struct sk_buff *skb;
97 /* Cast away the const, as we are just wanting to
98 * bump the reference count.
100 sctp_association_hold((struct sctp_association *)asoc);
101 skb = sctp_event2skb(event);
102 event->asoc = (struct sctp_association *)asoc;
103 atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
104 sctp_skb_set_owner_r(skb, asoc->base.sk);
107 /* A simple destructor to give up the reference to the association. */
108 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
110 struct sctp_association *asoc = event->asoc;
112 atomic_sub(event->rmem_len, &asoc->rmem_alloc);
113 sctp_association_put(asoc);
116 /* Create and initialize an SCTP_ASSOC_CHANGE event.
118 * 5.3.1.1 SCTP_ASSOC_CHANGE
120 * Communication notifications inform the ULP that an SCTP association
121 * has either begun or ended. The identifier for a new association is
122 * provided by this notification.
124 * Note: There is no field checking here. If a field is unused it will be
125 * zero'd out.
127 struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
128 const struct sctp_association *asoc,
129 __u16 flags, __u16 state, __u16 error, __u16 outbound,
130 __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
132 struct sctp_ulpevent *event;
133 struct sctp_assoc_change *sac;
134 struct sk_buff *skb;
136 /* If the lower layer passed in the chunk, it will be
137 * an ABORT, so we need to include it in the sac_info.
139 if (chunk) {
140 /* Copy the chunk data to a new skb and reserve enough
141 * head room to use as notification.
143 skb = skb_copy_expand(chunk->skb,
144 sizeof(struct sctp_assoc_change), 0, gfp);
146 if (!skb)
147 goto fail;
149 /* Embed the event fields inside the cloned skb. */
150 event = sctp_skb2event(skb);
151 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
153 /* Include the notification structure */
154 sac = (struct sctp_assoc_change *)
155 skb_push(skb, sizeof(struct sctp_assoc_change));
157 /* Trim the buffer to the right length. */
158 skb_trim(skb, sizeof(struct sctp_assoc_change) +
159 ntohs(chunk->chunk_hdr->length) -
160 sizeof(sctp_chunkhdr_t));
161 } else {
162 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
163 MSG_NOTIFICATION, gfp);
164 if (!event)
165 goto fail;
167 skb = sctp_event2skb(event);
168 sac = (struct sctp_assoc_change *) skb_put(skb,
169 sizeof(struct sctp_assoc_change));
172 /* Socket Extensions for SCTP
173 * 5.3.1.1 SCTP_ASSOC_CHANGE
175 * sac_type:
176 * It should be SCTP_ASSOC_CHANGE.
178 sac->sac_type = SCTP_ASSOC_CHANGE;
180 /* Socket Extensions for SCTP
181 * 5.3.1.1 SCTP_ASSOC_CHANGE
183 * sac_state: 32 bits (signed integer)
184 * This field holds one of a number of values that communicate the
185 * event that happened to the association.
187 sac->sac_state = state;
189 /* Socket Extensions for SCTP
190 * 5.3.1.1 SCTP_ASSOC_CHANGE
192 * sac_flags: 16 bits (unsigned integer)
193 * Currently unused.
195 sac->sac_flags = 0;
197 /* Socket Extensions for SCTP
198 * 5.3.1.1 SCTP_ASSOC_CHANGE
200 * sac_length: sizeof (__u32)
201 * This field is the total length of the notification data, including
202 * the notification header.
204 sac->sac_length = skb->len;
206 /* Socket Extensions for SCTP
207 * 5.3.1.1 SCTP_ASSOC_CHANGE
209 * sac_error: 32 bits (signed integer)
211 * If the state was reached due to a error condition (e.g.
212 * COMMUNICATION_LOST) any relevant error information is available in
213 * this field. This corresponds to the protocol error codes defined in
214 * [SCTP].
216 sac->sac_error = error;
218 /* Socket Extensions for SCTP
219 * 5.3.1.1 SCTP_ASSOC_CHANGE
221 * sac_outbound_streams: 16 bits (unsigned integer)
222 * sac_inbound_streams: 16 bits (unsigned integer)
224 * The maximum number of streams allowed in each direction are
225 * available in sac_outbound_streams and sac_inbound streams.
227 sac->sac_outbound_streams = outbound;
228 sac->sac_inbound_streams = inbound;
230 /* Socket Extensions for SCTP
231 * 5.3.1.1 SCTP_ASSOC_CHANGE
233 * sac_assoc_id: sizeof (sctp_assoc_t)
235 * The association id field, holds the identifier for the association.
236 * All notifications for a given association have the same association
237 * identifier. For TCP style socket, this field is ignored.
239 sctp_ulpevent_set_owner(event, asoc);
240 sac->sac_assoc_id = sctp_assoc2id(asoc);
242 return event;
244 fail:
245 return NULL;
248 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
250 * Socket Extensions for SCTP - draft-01
251 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
253 * When a destination address on a multi-homed peer encounters a change
254 * an interface details event is sent.
256 struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
257 const struct sctp_association *asoc,
258 const struct sockaddr_storage *aaddr,
259 int flags, int state, int error, gfp_t gfp)
261 struct sctp_ulpevent *event;
262 struct sctp_paddr_change *spc;
263 struct sk_buff *skb;
265 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
266 MSG_NOTIFICATION, gfp);
267 if (!event)
268 goto fail;
270 skb = sctp_event2skb(event);
271 spc = (struct sctp_paddr_change *)
272 skb_put(skb, sizeof(struct sctp_paddr_change));
274 /* Sockets API Extensions for SCTP
275 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
277 * spc_type:
279 * It should be SCTP_PEER_ADDR_CHANGE.
281 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
283 /* Sockets API Extensions for SCTP
284 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
286 * spc_length: sizeof (__u32)
288 * This field is the total length of the notification data, including
289 * the notification header.
291 spc->spc_length = sizeof(struct sctp_paddr_change);
293 /* Sockets API Extensions for SCTP
294 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
296 * spc_flags: 16 bits (unsigned integer)
297 * Currently unused.
299 spc->spc_flags = 0;
301 /* Sockets API Extensions for SCTP
302 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
304 * spc_state: 32 bits (signed integer)
306 * This field holds one of a number of values that communicate the
307 * event that happened to the address.
309 spc->spc_state = state;
311 /* Sockets API Extensions for SCTP
312 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
314 * spc_error: 32 bits (signed integer)
316 * If the state was reached due to any error condition (e.g.
317 * ADDRESS_UNREACHABLE) any relevant error information is available in
318 * this field.
320 spc->spc_error = error;
322 /* Socket Extensions for SCTP
323 * 5.3.1.1 SCTP_ASSOC_CHANGE
325 * spc_assoc_id: sizeof (sctp_assoc_t)
327 * The association id field, holds the identifier for the association.
328 * All notifications for a given association have the same association
329 * identifier. For TCP style socket, this field is ignored.
331 sctp_ulpevent_set_owner(event, asoc);
332 spc->spc_assoc_id = sctp_assoc2id(asoc);
334 /* Sockets API Extensions for SCTP
335 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
337 * spc_aaddr: sizeof (struct sockaddr_storage)
339 * The affected address field, holds the remote peer's address that is
340 * encountering the change of state.
342 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
344 /* Map ipv4 address into v4-mapped-on-v6 address. */
345 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
346 sctp_sk(asoc->base.sk),
347 (union sctp_addr *)&spc->spc_aaddr);
349 return event;
351 fail:
352 return NULL;
355 /* Create and initialize an SCTP_REMOTE_ERROR notification.
357 * Note: This assumes that the chunk->skb->data already points to the
358 * operation error payload.
360 * Socket Extensions for SCTP - draft-01
361 * 5.3.1.3 SCTP_REMOTE_ERROR
363 * A remote peer may send an Operational Error message to its peer.
364 * This message indicates a variety of error conditions on an
365 * association. The entire error TLV as it appears on the wire is
366 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
367 * specification [SCTP] and any extensions for a list of possible
368 * error formats.
370 struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
371 const struct sctp_association *asoc, struct sctp_chunk *chunk,
372 __u16 flags, gfp_t gfp)
374 struct sctp_ulpevent *event;
375 struct sctp_remote_error *sre;
376 struct sk_buff *skb;
377 sctp_errhdr_t *ch;
378 __be16 cause;
379 int elen;
381 ch = (sctp_errhdr_t *)(chunk->skb->data);
382 cause = ch->cause;
383 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
385 /* Pull off the ERROR header. */
386 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
388 /* Copy the skb to a new skb with room for us to prepend
389 * notification with.
391 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
392 0, gfp);
394 /* Pull off the rest of the cause TLV from the chunk. */
395 skb_pull(chunk->skb, elen);
396 if (!skb)
397 goto fail;
399 /* Embed the event fields inside the cloned skb. */
400 event = sctp_skb2event(skb);
401 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
403 sre = (struct sctp_remote_error *)
404 skb_push(skb, sizeof(struct sctp_remote_error));
406 /* Trim the buffer to the right length. */
407 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
409 /* Socket Extensions for SCTP
410 * 5.3.1.3 SCTP_REMOTE_ERROR
412 * sre_type:
413 * It should be SCTP_REMOTE_ERROR.
415 sre->sre_type = SCTP_REMOTE_ERROR;
418 * Socket Extensions for SCTP
419 * 5.3.1.3 SCTP_REMOTE_ERROR
421 * sre_flags: 16 bits (unsigned integer)
422 * Currently unused.
424 sre->sre_flags = 0;
426 /* Socket Extensions for SCTP
427 * 5.3.1.3 SCTP_REMOTE_ERROR
429 * sre_length: sizeof (__u32)
431 * This field is the total length of the notification data,
432 * including the notification header.
434 sre->sre_length = skb->len;
436 /* Socket Extensions for SCTP
437 * 5.3.1.3 SCTP_REMOTE_ERROR
439 * sre_error: 16 bits (unsigned integer)
440 * This value represents one of the Operational Error causes defined in
441 * the SCTP specification, in network byte order.
443 sre->sre_error = cause;
445 /* Socket Extensions for SCTP
446 * 5.3.1.3 SCTP_REMOTE_ERROR
448 * sre_assoc_id: sizeof (sctp_assoc_t)
450 * The association id field, holds the identifier for the association.
451 * All notifications for a given association have the same association
452 * identifier. For TCP style socket, this field is ignored.
454 sctp_ulpevent_set_owner(event, asoc);
455 sre->sre_assoc_id = sctp_assoc2id(asoc);
457 return event;
459 fail:
460 return NULL;
463 /* Create and initialize a SCTP_SEND_FAILED notification.
465 * Socket Extensions for SCTP - draft-01
466 * 5.3.1.4 SCTP_SEND_FAILED
468 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
469 const struct sctp_association *asoc, struct sctp_chunk *chunk,
470 __u16 flags, __u32 error, gfp_t gfp)
472 struct sctp_ulpevent *event;
473 struct sctp_send_failed *ssf;
474 struct sk_buff *skb;
476 /* Pull off any padding. */
477 int len = ntohs(chunk->chunk_hdr->length);
479 /* Make skb with more room so we can prepend notification. */
480 skb = skb_copy_expand(chunk->skb,
481 sizeof(struct sctp_send_failed), /* headroom */
482 0, /* tailroom */
483 gfp);
484 if (!skb)
485 goto fail;
487 /* Pull off the common chunk header and DATA header. */
488 skb_pull(skb, sizeof(struct sctp_data_chunk));
489 len -= sizeof(struct sctp_data_chunk);
491 /* Embed the event fields inside the cloned skb. */
492 event = sctp_skb2event(skb);
493 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
495 ssf = (struct sctp_send_failed *)
496 skb_push(skb, sizeof(struct sctp_send_failed));
498 /* Socket Extensions for SCTP
499 * 5.3.1.4 SCTP_SEND_FAILED
501 * ssf_type:
502 * It should be SCTP_SEND_FAILED.
504 ssf->ssf_type = SCTP_SEND_FAILED;
506 /* Socket Extensions for SCTP
507 * 5.3.1.4 SCTP_SEND_FAILED
509 * ssf_flags: 16 bits (unsigned integer)
510 * The flag value will take one of the following values
512 * SCTP_DATA_UNSENT - Indicates that the data was never put on
513 * the wire.
515 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
516 * Note that this does not necessarily mean that the
517 * data was (or was not) successfully delivered.
519 ssf->ssf_flags = flags;
521 /* Socket Extensions for SCTP
522 * 5.3.1.4 SCTP_SEND_FAILED
524 * ssf_length: sizeof (__u32)
525 * This field is the total length of the notification data, including
526 * the notification header.
528 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
529 skb_trim(skb, ssf->ssf_length);
531 /* Socket Extensions for SCTP
532 * 5.3.1.4 SCTP_SEND_FAILED
534 * ssf_error: 16 bits (unsigned integer)
535 * This value represents the reason why the send failed, and if set,
536 * will be a SCTP protocol error code as defined in [SCTP] section
537 * 3.3.10.
539 ssf->ssf_error = error;
541 /* Socket Extensions for SCTP
542 * 5.3.1.4 SCTP_SEND_FAILED
544 * ssf_info: sizeof (struct sctp_sndrcvinfo)
545 * The original send information associated with the undelivered
546 * message.
548 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
550 /* Per TSVWG discussion with Randy. Allow the application to
551 * reassemble a fragmented message.
553 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
555 /* Socket Extensions for SCTP
556 * 5.3.1.4 SCTP_SEND_FAILED
558 * ssf_assoc_id: sizeof (sctp_assoc_t)
559 * The association id field, sf_assoc_id, holds the identifier for the
560 * association. All notifications for a given association have the
561 * same association identifier. For TCP style socket, this field is
562 * ignored.
564 sctp_ulpevent_set_owner(event, asoc);
565 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
566 return event;
568 fail:
569 return NULL;
572 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
574 * Socket Extensions for SCTP - draft-01
575 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
577 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
578 const struct sctp_association *asoc,
579 __u16 flags, gfp_t gfp)
581 struct sctp_ulpevent *event;
582 struct sctp_shutdown_event *sse;
583 struct sk_buff *skb;
585 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
586 MSG_NOTIFICATION, gfp);
587 if (!event)
588 goto fail;
590 skb = sctp_event2skb(event);
591 sse = (struct sctp_shutdown_event *)
592 skb_put(skb, sizeof(struct sctp_shutdown_event));
594 /* Socket Extensions for SCTP
595 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
597 * sse_type
598 * It should be SCTP_SHUTDOWN_EVENT
600 sse->sse_type = SCTP_SHUTDOWN_EVENT;
602 /* Socket Extensions for SCTP
603 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
605 * sse_flags: 16 bits (unsigned integer)
606 * Currently unused.
608 sse->sse_flags = 0;
610 /* Socket Extensions for SCTP
611 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
613 * sse_length: sizeof (__u32)
614 * This field is the total length of the notification data, including
615 * the notification header.
617 sse->sse_length = sizeof(struct sctp_shutdown_event);
619 /* Socket Extensions for SCTP
620 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
622 * sse_assoc_id: sizeof (sctp_assoc_t)
623 * The association id field, holds the identifier for the association.
624 * All notifications for a given association have the same association
625 * identifier. For TCP style socket, this field is ignored.
627 sctp_ulpevent_set_owner(event, asoc);
628 sse->sse_assoc_id = sctp_assoc2id(asoc);
630 return event;
632 fail:
633 return NULL;
636 /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
638 * Socket Extensions for SCTP
639 * 5.3.1.6 SCTP_ADAPTATION_INDICATION
641 struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
642 const struct sctp_association *asoc, gfp_t gfp)
644 struct sctp_ulpevent *event;
645 struct sctp_adaptation_event *sai;
646 struct sk_buff *skb;
648 event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
649 MSG_NOTIFICATION, gfp);
650 if (!event)
651 goto fail;
653 skb = sctp_event2skb(event);
654 sai = (struct sctp_adaptation_event *)
655 skb_put(skb, sizeof(struct sctp_adaptation_event));
657 sai->sai_type = SCTP_ADAPTATION_INDICATION;
658 sai->sai_flags = 0;
659 sai->sai_length = sizeof(struct sctp_adaptation_event);
660 sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
661 sctp_ulpevent_set_owner(event, asoc);
662 sai->sai_assoc_id = sctp_assoc2id(asoc);
664 return event;
666 fail:
667 return NULL;
670 /* A message has been received. Package this message as a notification
671 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
672 * even if filtered out later.
674 * Socket Extensions for SCTP
675 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
677 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
678 struct sctp_chunk *chunk,
679 gfp_t gfp)
681 struct sctp_ulpevent *event = NULL;
682 struct sk_buff *skb;
683 size_t padding, len;
684 int rx_count;
687 * check to see if we need to make space for this
688 * new skb, expand the rcvbuffer if needed, or drop
689 * the frame
691 if (asoc->ep->rcvbuf_policy)
692 rx_count = atomic_read(&asoc->rmem_alloc);
693 else
694 rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
696 if (rx_count >= asoc->base.sk->sk_rcvbuf) {
698 if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
699 (!sk_rmem_schedule(asoc->base.sk, chunk->skb,
700 chunk->skb->truesize)))
701 goto fail;
704 /* Clone the original skb, sharing the data. */
705 skb = skb_clone(chunk->skb, gfp);
706 if (!skb)
707 goto fail;
709 /* Now that all memory allocations for this chunk succeeded, we
710 * can mark it as received so the tsn_map is updated correctly.
712 if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
713 ntohl(chunk->subh.data_hdr->tsn),
714 chunk->transport))
715 goto fail_mark;
717 /* First calculate the padding, so we don't inadvertently
718 * pass up the wrong length to the user.
720 * RFC 2960 - Section 3.2 Chunk Field Descriptions
722 * The total length of a chunk(including Type, Length and Value fields)
723 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
724 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
725 * bytes and this padding is not included in the chunk length field.
726 * The sender should never pad with more than 3 bytes. The receiver
727 * MUST ignore the padding bytes.
729 len = ntohs(chunk->chunk_hdr->length);
730 padding = WORD_ROUND(len) - len;
732 /* Fixup cloned skb with just this chunks data. */
733 skb_trim(skb, chunk->chunk_end - padding - skb->data);
735 /* Embed the event fields inside the cloned skb. */
736 event = sctp_skb2event(skb);
738 /* Initialize event with flags 0 and correct length
739 * Since this is a clone of the original skb, only account for
740 * the data of this chunk as other chunks will be accounted separately.
742 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
744 sctp_ulpevent_receive_data(event, asoc);
746 event->stream = ntohs(chunk->subh.data_hdr->stream);
747 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
748 event->ppid = chunk->subh.data_hdr->ppid;
749 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
750 event->flags |= SCTP_UNORDERED;
751 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
753 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
754 event->msg_flags |= chunk->chunk_hdr->flags;
755 event->iif = sctp_chunk_iif(chunk);
757 return event;
759 fail_mark:
760 kfree_skb(skb);
761 fail:
762 return NULL;
765 /* Create a partial delivery related event.
767 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
769 * When a receiver is engaged in a partial delivery of a
770 * message this notification will be used to indicate
771 * various events.
773 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
774 const struct sctp_association *asoc, __u32 indication,
775 gfp_t gfp)
777 struct sctp_ulpevent *event;
778 struct sctp_pdapi_event *pd;
779 struct sk_buff *skb;
781 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
782 MSG_NOTIFICATION, gfp);
783 if (!event)
784 goto fail;
786 skb = sctp_event2skb(event);
787 pd = (struct sctp_pdapi_event *)
788 skb_put(skb, sizeof(struct sctp_pdapi_event));
790 /* pdapi_type
791 * It should be SCTP_PARTIAL_DELIVERY_EVENT
793 * pdapi_flags: 16 bits (unsigned integer)
794 * Currently unused.
796 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
797 pd->pdapi_flags = 0;
799 /* pdapi_length: 32 bits (unsigned integer)
801 * This field is the total length of the notification data, including
802 * the notification header. It will generally be sizeof (struct
803 * sctp_pdapi_event).
805 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
807 /* pdapi_indication: 32 bits (unsigned integer)
809 * This field holds the indication being sent to the application.
811 pd->pdapi_indication = indication;
813 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
815 * The association id field, holds the identifier for the association.
817 sctp_ulpevent_set_owner(event, asoc);
818 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
820 return event;
821 fail:
822 return NULL;
825 struct sctp_ulpevent *sctp_ulpevent_make_authkey(
826 const struct sctp_association *asoc, __u16 key_id,
827 __u32 indication, gfp_t gfp)
829 struct sctp_ulpevent *event;
830 struct sctp_authkey_event *ak;
831 struct sk_buff *skb;
833 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
834 MSG_NOTIFICATION, gfp);
835 if (!event)
836 goto fail;
838 skb = sctp_event2skb(event);
839 ak = (struct sctp_authkey_event *)
840 skb_put(skb, sizeof(struct sctp_authkey_event));
842 ak->auth_type = SCTP_AUTHENTICATION_EVENT;
843 ak->auth_flags = 0;
844 ak->auth_length = sizeof(struct sctp_authkey_event);
846 ak->auth_keynumber = key_id;
847 ak->auth_altkeynumber = 0;
848 ak->auth_indication = indication;
851 * The association id field, holds the identifier for the association.
853 sctp_ulpevent_set_owner(event, asoc);
854 ak->auth_assoc_id = sctp_assoc2id(asoc);
856 return event;
857 fail:
858 return NULL;
862 * Socket Extensions for SCTP
863 * 6.3.10. SCTP_SENDER_DRY_EVENT
865 struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
866 const struct sctp_association *asoc, gfp_t gfp)
868 struct sctp_ulpevent *event;
869 struct sctp_sender_dry_event *sdry;
870 struct sk_buff *skb;
872 event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
873 MSG_NOTIFICATION, gfp);
874 if (!event)
875 return NULL;
877 skb = sctp_event2skb(event);
878 sdry = (struct sctp_sender_dry_event *)
879 skb_put(skb, sizeof(struct sctp_sender_dry_event));
881 sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
882 sdry->sender_dry_flags = 0;
883 sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
884 sctp_ulpevent_set_owner(event, asoc);
885 sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
887 return event;
890 /* Return the notification type, assuming this is a notification
891 * event.
893 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
895 union sctp_notification *notification;
896 struct sk_buff *skb;
898 skb = sctp_event2skb(event);
899 notification = (union sctp_notification *) skb->data;
900 return notification->sn_header.sn_type;
903 /* Copy out the sndrcvinfo into a msghdr. */
904 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
905 struct msghdr *msghdr)
907 struct sctp_sndrcvinfo sinfo;
909 if (sctp_ulpevent_is_notification(event))
910 return;
912 /* Sockets API Extensions for SCTP
913 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
915 * sinfo_stream: 16 bits (unsigned integer)
917 * For recvmsg() the SCTP stack places the message's stream number in
918 * this value.
920 sinfo.sinfo_stream = event->stream;
921 /* sinfo_ssn: 16 bits (unsigned integer)
923 * For recvmsg() this value contains the stream sequence number that
924 * the remote endpoint placed in the DATA chunk. For fragmented
925 * messages this is the same number for all deliveries of the message
926 * (if more than one recvmsg() is needed to read the message).
928 sinfo.sinfo_ssn = event->ssn;
929 /* sinfo_ppid: 32 bits (unsigned integer)
931 * In recvmsg() this value is
932 * the same information that was passed by the upper layer in the peer
933 * application. Please note that byte order issues are NOT accounted
934 * for and this information is passed opaquely by the SCTP stack from
935 * one end to the other.
937 sinfo.sinfo_ppid = event->ppid;
938 /* sinfo_flags: 16 bits (unsigned integer)
940 * This field may contain any of the following flags and is composed of
941 * a bitwise OR of these values.
943 * recvmsg() flags:
945 * SCTP_UNORDERED - This flag is present when the message was sent
946 * non-ordered.
948 sinfo.sinfo_flags = event->flags;
949 /* sinfo_tsn: 32 bit (unsigned integer)
951 * For the receiving side, this field holds a TSN that was
952 * assigned to one of the SCTP Data Chunks.
954 sinfo.sinfo_tsn = event->tsn;
955 /* sinfo_cumtsn: 32 bit (unsigned integer)
957 * This field will hold the current cumulative TSN as
958 * known by the underlying SCTP layer. Note this field is
959 * ignored when sending and only valid for a receive
960 * operation when sinfo_flags are set to SCTP_UNORDERED.
962 sinfo.sinfo_cumtsn = event->cumtsn;
963 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
965 * The association handle field, sinfo_assoc_id, holds the identifier
966 * for the association announced in the COMMUNICATION_UP notification.
967 * All notifications for a given association have the same identifier.
968 * Ignored for one-to-one style sockets.
970 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
972 /* context value that is set via SCTP_CONTEXT socket option. */
973 sinfo.sinfo_context = event->asoc->default_rcv_context;
975 /* These fields are not used while receiving. */
976 sinfo.sinfo_timetolive = 0;
978 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
979 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
982 /* Do accounting for bytes received and hold a reference to the association
983 * for each skb.
985 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
986 struct sctp_association *asoc)
988 struct sk_buff *skb, *frag;
990 skb = sctp_event2skb(event);
991 /* Set the owner and charge rwnd for bytes received. */
992 sctp_ulpevent_set_owner(event, asoc);
993 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
995 if (!skb->data_len)
996 return;
998 /* Note: Not clearing the entire event struct as this is just a
999 * fragment of the real event. However, we still need to do rwnd
1000 * accounting.
1001 * In general, the skb passed from IP can have only 1 level of
1002 * fragments. But we allow multiple levels of fragments.
1004 skb_walk_frags(skb, frag)
1005 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
1008 /* Do accounting for bytes just read by user and release the references to
1009 * the association.
1011 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
1013 struct sk_buff *skb, *frag;
1014 unsigned int len;
1016 /* Current stack structures assume that the rcv buffer is
1017 * per socket. For UDP style sockets this is not true as
1018 * multiple associations may be on a single UDP-style socket.
1019 * Use the local private area of the skb to track the owning
1020 * association.
1023 skb = sctp_event2skb(event);
1024 len = skb->len;
1026 if (!skb->data_len)
1027 goto done;
1029 /* Don't forget the fragments. */
1030 skb_walk_frags(skb, frag) {
1031 /* NOTE: skb_shinfos are recursive. Although IP returns
1032 * skb's with only 1 level of fragments, SCTP reassembly can
1033 * increase the levels.
1035 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1038 done:
1039 sctp_assoc_rwnd_increase(event->asoc, len);
1040 sctp_ulpevent_release_owner(event);
1043 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1045 struct sk_buff *skb, *frag;
1047 skb = sctp_event2skb(event);
1049 if (!skb->data_len)
1050 goto done;
1052 /* Don't forget the fragments. */
1053 skb_walk_frags(skb, frag) {
1054 /* NOTE: skb_shinfos are recursive. Although IP returns
1055 * skb's with only 1 level of fragments, SCTP reassembly can
1056 * increase the levels.
1058 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1061 done:
1062 sctp_ulpevent_release_owner(event);
1065 /* Free a ulpevent that has an owner. It includes releasing the reference
1066 * to the owner, updating the rwnd in case of a DATA event and freeing the
1067 * skb.
1069 void sctp_ulpevent_free(struct sctp_ulpevent *event)
1071 if (sctp_ulpevent_is_notification(event))
1072 sctp_ulpevent_release_owner(event);
1073 else
1074 sctp_ulpevent_release_data(event);
1076 kfree_skb(sctp_event2skb(event));
1079 /* Purge the skb lists holding ulpevents. */
1080 unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1082 struct sk_buff *skb;
1083 unsigned int data_unread = 0;
1085 while ((skb = skb_dequeue(list)) != NULL) {
1086 struct sctp_ulpevent *event = sctp_skb2event(skb);
1088 if (!sctp_ulpevent_is_notification(event))
1089 data_unread += skb->len;
1091 sctp_ulpevent_free(event);
1094 return data_unread;