Linux 3.12.28
[linux/fpc-iii.git] / net / sctp / ulpevent.c
blob12c37cee80e5f95c7c8f3010656088be2e36661c
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 *
371 sctp_ulpevent_make_remote_error(const struct sctp_association *asoc,
372 struct sctp_chunk *chunk, __u16 flags,
373 gfp_t gfp)
375 struct sctp_ulpevent *event;
376 struct sctp_remote_error *sre;
377 struct sk_buff *skb;
378 sctp_errhdr_t *ch;
379 __be16 cause;
380 int elen;
382 ch = (sctp_errhdr_t *)(chunk->skb->data);
383 cause = ch->cause;
384 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
386 /* Pull off the ERROR header. */
387 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
389 /* Copy the skb to a new skb with room for us to prepend
390 * notification with.
392 skb = skb_copy_expand(chunk->skb, sizeof(*sre), 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 *) skb_push(skb, sizeof(*sre));
405 /* Trim the buffer to the right length. */
406 skb_trim(skb, sizeof(*sre) + elen);
408 /* RFC6458, Section 6.1.3. SCTP_REMOTE_ERROR */
409 memset(sre, 0, sizeof(*sre));
410 sre->sre_type = SCTP_REMOTE_ERROR;
411 sre->sre_flags = 0;
412 sre->sre_length = skb->len;
413 sre->sre_error = cause;
414 sctp_ulpevent_set_owner(event, asoc);
415 sre->sre_assoc_id = sctp_assoc2id(asoc);
417 return event;
418 fail:
419 return NULL;
422 /* Create and initialize a SCTP_SEND_FAILED notification.
424 * Socket Extensions for SCTP - draft-01
425 * 5.3.1.4 SCTP_SEND_FAILED
427 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
428 const struct sctp_association *asoc, struct sctp_chunk *chunk,
429 __u16 flags, __u32 error, gfp_t gfp)
431 struct sctp_ulpevent *event;
432 struct sctp_send_failed *ssf;
433 struct sk_buff *skb;
435 /* Pull off any padding. */
436 int len = ntohs(chunk->chunk_hdr->length);
438 /* Make skb with more room so we can prepend notification. */
439 skb = skb_copy_expand(chunk->skb,
440 sizeof(struct sctp_send_failed), /* headroom */
441 0, /* tailroom */
442 gfp);
443 if (!skb)
444 goto fail;
446 /* Pull off the common chunk header and DATA header. */
447 skb_pull(skb, sizeof(struct sctp_data_chunk));
448 len -= sizeof(struct sctp_data_chunk);
450 /* Embed the event fields inside the cloned skb. */
451 event = sctp_skb2event(skb);
452 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
454 ssf = (struct sctp_send_failed *)
455 skb_push(skb, sizeof(struct sctp_send_failed));
457 /* Socket Extensions for SCTP
458 * 5.3.1.4 SCTP_SEND_FAILED
460 * ssf_type:
461 * It should be SCTP_SEND_FAILED.
463 ssf->ssf_type = SCTP_SEND_FAILED;
465 /* Socket Extensions for SCTP
466 * 5.3.1.4 SCTP_SEND_FAILED
468 * ssf_flags: 16 bits (unsigned integer)
469 * The flag value will take one of the following values
471 * SCTP_DATA_UNSENT - Indicates that the data was never put on
472 * the wire.
474 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
475 * Note that this does not necessarily mean that the
476 * data was (or was not) successfully delivered.
478 ssf->ssf_flags = flags;
480 /* Socket Extensions for SCTP
481 * 5.3.1.4 SCTP_SEND_FAILED
483 * ssf_length: sizeof (__u32)
484 * This field is the total length of the notification data, including
485 * the notification header.
487 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
488 skb_trim(skb, ssf->ssf_length);
490 /* Socket Extensions for SCTP
491 * 5.3.1.4 SCTP_SEND_FAILED
493 * ssf_error: 16 bits (unsigned integer)
494 * This value represents the reason why the send failed, and if set,
495 * will be a SCTP protocol error code as defined in [SCTP] section
496 * 3.3.10.
498 ssf->ssf_error = error;
500 /* Socket Extensions for SCTP
501 * 5.3.1.4 SCTP_SEND_FAILED
503 * ssf_info: sizeof (struct sctp_sndrcvinfo)
504 * The original send information associated with the undelivered
505 * message.
507 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
509 /* Per TSVWG discussion with Randy. Allow the application to
510 * reassemble a fragmented message.
512 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
514 /* Socket Extensions for SCTP
515 * 5.3.1.4 SCTP_SEND_FAILED
517 * ssf_assoc_id: sizeof (sctp_assoc_t)
518 * The association id field, sf_assoc_id, holds the identifier for the
519 * association. All notifications for a given association have the
520 * same association identifier. For TCP style socket, this field is
521 * ignored.
523 sctp_ulpevent_set_owner(event, asoc);
524 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
525 return event;
527 fail:
528 return NULL;
531 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
533 * Socket Extensions for SCTP - draft-01
534 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
536 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
537 const struct sctp_association *asoc,
538 __u16 flags, gfp_t gfp)
540 struct sctp_ulpevent *event;
541 struct sctp_shutdown_event *sse;
542 struct sk_buff *skb;
544 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
545 MSG_NOTIFICATION, gfp);
546 if (!event)
547 goto fail;
549 skb = sctp_event2skb(event);
550 sse = (struct sctp_shutdown_event *)
551 skb_put(skb, sizeof(struct sctp_shutdown_event));
553 /* Socket Extensions for SCTP
554 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
556 * sse_type
557 * It should be SCTP_SHUTDOWN_EVENT
559 sse->sse_type = SCTP_SHUTDOWN_EVENT;
561 /* Socket Extensions for SCTP
562 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
564 * sse_flags: 16 bits (unsigned integer)
565 * Currently unused.
567 sse->sse_flags = 0;
569 /* Socket Extensions for SCTP
570 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
572 * sse_length: sizeof (__u32)
573 * This field is the total length of the notification data, including
574 * the notification header.
576 sse->sse_length = sizeof(struct sctp_shutdown_event);
578 /* Socket Extensions for SCTP
579 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
581 * sse_assoc_id: sizeof (sctp_assoc_t)
582 * The association id field, holds the identifier for the association.
583 * All notifications for a given association have the same association
584 * identifier. For TCP style socket, this field is ignored.
586 sctp_ulpevent_set_owner(event, asoc);
587 sse->sse_assoc_id = sctp_assoc2id(asoc);
589 return event;
591 fail:
592 return NULL;
595 /* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
597 * Socket Extensions for SCTP
598 * 5.3.1.6 SCTP_ADAPTATION_INDICATION
600 struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
601 const struct sctp_association *asoc, gfp_t gfp)
603 struct sctp_ulpevent *event;
604 struct sctp_adaptation_event *sai;
605 struct sk_buff *skb;
607 event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
608 MSG_NOTIFICATION, gfp);
609 if (!event)
610 goto fail;
612 skb = sctp_event2skb(event);
613 sai = (struct sctp_adaptation_event *)
614 skb_put(skb, sizeof(struct sctp_adaptation_event));
616 sai->sai_type = SCTP_ADAPTATION_INDICATION;
617 sai->sai_flags = 0;
618 sai->sai_length = sizeof(struct sctp_adaptation_event);
619 sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
620 sctp_ulpevent_set_owner(event, asoc);
621 sai->sai_assoc_id = sctp_assoc2id(asoc);
623 return event;
625 fail:
626 return NULL;
629 /* A message has been received. Package this message as a notification
630 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
631 * even if filtered out later.
633 * Socket Extensions for SCTP
634 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
636 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
637 struct sctp_chunk *chunk,
638 gfp_t gfp)
640 struct sctp_ulpevent *event = NULL;
641 struct sk_buff *skb;
642 size_t padding, len;
643 int rx_count;
646 * check to see if we need to make space for this
647 * new skb, expand the rcvbuffer if needed, or drop
648 * the frame
650 if (asoc->ep->rcvbuf_policy)
651 rx_count = atomic_read(&asoc->rmem_alloc);
652 else
653 rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
655 if (rx_count >= asoc->base.sk->sk_rcvbuf) {
657 if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
658 (!sk_rmem_schedule(asoc->base.sk, chunk->skb,
659 chunk->skb->truesize)))
660 goto fail;
663 /* Clone the original skb, sharing the data. */
664 skb = skb_clone(chunk->skb, gfp);
665 if (!skb)
666 goto fail;
668 /* Now that all memory allocations for this chunk succeeded, we
669 * can mark it as received so the tsn_map is updated correctly.
671 if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
672 ntohl(chunk->subh.data_hdr->tsn),
673 chunk->transport))
674 goto fail_mark;
676 /* First calculate the padding, so we don't inadvertently
677 * pass up the wrong length to the user.
679 * RFC 2960 - Section 3.2 Chunk Field Descriptions
681 * The total length of a chunk(including Type, Length and Value fields)
682 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
683 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
684 * bytes and this padding is not included in the chunk length field.
685 * The sender should never pad with more than 3 bytes. The receiver
686 * MUST ignore the padding bytes.
688 len = ntohs(chunk->chunk_hdr->length);
689 padding = WORD_ROUND(len) - len;
691 /* Fixup cloned skb with just this chunks data. */
692 skb_trim(skb, chunk->chunk_end - padding - skb->data);
694 /* Embed the event fields inside the cloned skb. */
695 event = sctp_skb2event(skb);
697 /* Initialize event with flags 0 and correct length
698 * Since this is a clone of the original skb, only account for
699 * the data of this chunk as other chunks will be accounted separately.
701 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
703 sctp_ulpevent_receive_data(event, asoc);
705 event->stream = ntohs(chunk->subh.data_hdr->stream);
706 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
707 event->ppid = chunk->subh.data_hdr->ppid;
708 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
709 event->flags |= SCTP_UNORDERED;
710 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
712 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
713 event->msg_flags |= chunk->chunk_hdr->flags;
714 event->iif = sctp_chunk_iif(chunk);
716 return event;
718 fail_mark:
719 kfree_skb(skb);
720 fail:
721 return NULL;
724 /* Create a partial delivery related event.
726 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
728 * When a receiver is engaged in a partial delivery of a
729 * message this notification will be used to indicate
730 * various events.
732 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
733 const struct sctp_association *asoc, __u32 indication,
734 gfp_t gfp)
736 struct sctp_ulpevent *event;
737 struct sctp_pdapi_event *pd;
738 struct sk_buff *skb;
740 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
741 MSG_NOTIFICATION, gfp);
742 if (!event)
743 goto fail;
745 skb = sctp_event2skb(event);
746 pd = (struct sctp_pdapi_event *)
747 skb_put(skb, sizeof(struct sctp_pdapi_event));
749 /* pdapi_type
750 * It should be SCTP_PARTIAL_DELIVERY_EVENT
752 * pdapi_flags: 16 bits (unsigned integer)
753 * Currently unused.
755 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
756 pd->pdapi_flags = 0;
758 /* pdapi_length: 32 bits (unsigned integer)
760 * This field is the total length of the notification data, including
761 * the notification header. It will generally be sizeof (struct
762 * sctp_pdapi_event).
764 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
766 /* pdapi_indication: 32 bits (unsigned integer)
768 * This field holds the indication being sent to the application.
770 pd->pdapi_indication = indication;
772 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
774 * The association id field, holds the identifier for the association.
776 sctp_ulpevent_set_owner(event, asoc);
777 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
779 return event;
780 fail:
781 return NULL;
784 struct sctp_ulpevent *sctp_ulpevent_make_authkey(
785 const struct sctp_association *asoc, __u16 key_id,
786 __u32 indication, gfp_t gfp)
788 struct sctp_ulpevent *event;
789 struct sctp_authkey_event *ak;
790 struct sk_buff *skb;
792 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
793 MSG_NOTIFICATION, gfp);
794 if (!event)
795 goto fail;
797 skb = sctp_event2skb(event);
798 ak = (struct sctp_authkey_event *)
799 skb_put(skb, sizeof(struct sctp_authkey_event));
801 ak->auth_type = SCTP_AUTHENTICATION_EVENT;
802 ak->auth_flags = 0;
803 ak->auth_length = sizeof(struct sctp_authkey_event);
805 ak->auth_keynumber = key_id;
806 ak->auth_altkeynumber = 0;
807 ak->auth_indication = indication;
810 * The association id field, holds the identifier for the association.
812 sctp_ulpevent_set_owner(event, asoc);
813 ak->auth_assoc_id = sctp_assoc2id(asoc);
815 return event;
816 fail:
817 return NULL;
821 * Socket Extensions for SCTP
822 * 6.3.10. SCTP_SENDER_DRY_EVENT
824 struct sctp_ulpevent *sctp_ulpevent_make_sender_dry_event(
825 const struct sctp_association *asoc, gfp_t gfp)
827 struct sctp_ulpevent *event;
828 struct sctp_sender_dry_event *sdry;
829 struct sk_buff *skb;
831 event = sctp_ulpevent_new(sizeof(struct sctp_sender_dry_event),
832 MSG_NOTIFICATION, gfp);
833 if (!event)
834 return NULL;
836 skb = sctp_event2skb(event);
837 sdry = (struct sctp_sender_dry_event *)
838 skb_put(skb, sizeof(struct sctp_sender_dry_event));
840 sdry->sender_dry_type = SCTP_SENDER_DRY_EVENT;
841 sdry->sender_dry_flags = 0;
842 sdry->sender_dry_length = sizeof(struct sctp_sender_dry_event);
843 sctp_ulpevent_set_owner(event, asoc);
844 sdry->sender_dry_assoc_id = sctp_assoc2id(asoc);
846 return event;
849 /* Return the notification type, assuming this is a notification
850 * event.
852 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
854 union sctp_notification *notification;
855 struct sk_buff *skb;
857 skb = sctp_event2skb(event);
858 notification = (union sctp_notification *) skb->data;
859 return notification->sn_header.sn_type;
862 /* RFC6458, Section 5.3.2. SCTP Header Information Structure
863 * (SCTP_SNDRCV, DEPRECATED)
865 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
866 struct msghdr *msghdr)
868 struct sctp_sndrcvinfo sinfo;
870 if (sctp_ulpevent_is_notification(event))
871 return;
873 memset(&sinfo, 0, sizeof(sinfo));
874 sinfo.sinfo_stream = event->stream;
875 sinfo.sinfo_ssn = event->ssn;
876 sinfo.sinfo_ppid = event->ppid;
877 sinfo.sinfo_flags = event->flags;
878 sinfo.sinfo_tsn = event->tsn;
879 sinfo.sinfo_cumtsn = event->cumtsn;
880 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
881 /* Context value that is set via SCTP_CONTEXT socket option. */
882 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(sinfo), &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 skb_walk_frags(skb, frag)
913 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
916 /* Do accounting for bytes just read by user and release the references to
917 * the association.
919 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
921 struct sk_buff *skb, *frag;
922 unsigned int len;
924 /* Current stack structures assume that the rcv buffer is
925 * per socket. For UDP style sockets this is not true as
926 * multiple associations may be on a single UDP-style socket.
927 * Use the local private area of the skb to track the owning
928 * association.
931 skb = sctp_event2skb(event);
932 len = skb->len;
934 if (!skb->data_len)
935 goto done;
937 /* Don't forget the fragments. */
938 skb_walk_frags(skb, frag) {
939 /* NOTE: skb_shinfos are recursive. Although IP returns
940 * skb's with only 1 level of fragments, SCTP reassembly can
941 * increase the levels.
943 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
946 done:
947 sctp_assoc_rwnd_increase(event->asoc, len);
948 sctp_ulpevent_release_owner(event);
951 static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
953 struct sk_buff *skb, *frag;
955 skb = sctp_event2skb(event);
957 if (!skb->data_len)
958 goto done;
960 /* Don't forget the fragments. */
961 skb_walk_frags(skb, frag) {
962 /* NOTE: skb_shinfos are recursive. Although IP returns
963 * skb's with only 1 level of fragments, SCTP reassembly can
964 * increase the levels.
966 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
969 done:
970 sctp_ulpevent_release_owner(event);
973 /* Free a ulpevent that has an owner. It includes releasing the reference
974 * to the owner, updating the rwnd in case of a DATA event and freeing the
975 * skb.
977 void sctp_ulpevent_free(struct sctp_ulpevent *event)
979 if (sctp_ulpevent_is_notification(event))
980 sctp_ulpevent_release_owner(event);
981 else
982 sctp_ulpevent_release_data(event);
984 kfree_skb(sctp_event2skb(event));
987 /* Purge the skb lists holding ulpevents. */
988 unsigned int sctp_queue_purge_ulpevents(struct sk_buff_head *list)
990 struct sk_buff *skb;
991 unsigned int data_unread = 0;
993 while ((skb = skb_dequeue(list)) != NULL) {
994 struct sctp_ulpevent *event = sctp_skb2event(skb);
996 if (!sctp_ulpevent_is_notification(event))
997 data_unread += skb->len;
999 sctp_ulpevent_free(event);
1002 return data_unread;