ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sctp / ulpevent.c
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
8  *
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.
16  *
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.
22  *
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.
27  *
28  * Please send any bug reports or fixes you make to the
29  * email address(es):
30  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
31  *
32  * Or submit a bug report through the following website:
33  *    http://www.sf.net/projects/lksctp
34  *
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>
40  *
41  * Any bugs reported given to us we will try to fix... any fixes shared will
42  * be incorporated into the next SCTP release.
43  */
44
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>
50
51 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
52                                            const struct sctp_association *asoc);
53 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event);
54 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
55                                        struct sctp_association *asoc);
56 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
57
58 /* Create a new sctp_ulpevent.  */
59 struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags, int gfp)
60 {
61         struct sctp_ulpevent *event;
62         struct sk_buff *skb;
63
64         skb = alloc_skb(size, gfp);
65         if (!skb)
66                 goto fail;
67
68         event = sctp_skb2event(skb);
69         sctp_ulpevent_init(event, msg_flags);
70
71         return event;
72
73 fail:
74         return NULL;
75 }
76
77 /* Initialize an ULP event from an given skb.  */
78 void sctp_ulpevent_init(struct sctp_ulpevent *event, int msg_flags)
79 {
80         memset(event, 0, sizeof(struct sctp_ulpevent));
81         event->msg_flags = msg_flags;
82 }
83
84 /* Is this a MSG_NOTIFICATION?  */
85 int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
86 {
87         return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
88 }
89
90 /* Create and initialize an SCTP_ASSOC_CHANGE event.
91  *
92  * 5.3.1.1 SCTP_ASSOC_CHANGE
93  *
94  * Communication notifications inform the ULP that an SCTP association
95  * has either begun or ended. The identifier for a new association is
96  * provided by this notification.
97  *
98  * Note: There is no field checking here.  If a field is unused it will be
99  * zero'd out.
100  */
101 struct sctp_ulpevent  *sctp_ulpevent_make_assoc_change(
102         const struct sctp_association *asoc,
103         __u16 flags, __u16 state, __u16 error, __u16 outbound,
104         __u16 inbound, int gfp)
105 {
106         struct sctp_ulpevent *event;
107         struct sctp_assoc_change *sac;
108         struct sk_buff *skb;
109
110         event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
111                                   MSG_NOTIFICATION, gfp);
112         if (!event)
113                 goto fail;
114         skb = sctp_event2skb(event);
115         sac = (struct sctp_assoc_change *)
116                 skb_put(skb, sizeof(struct sctp_assoc_change));
117
118         /* Socket Extensions for SCTP
119          * 5.3.1.1 SCTP_ASSOC_CHANGE
120          *
121          * sac_type:
122          * It should be SCTP_ASSOC_CHANGE.
123          */
124         sac->sac_type = SCTP_ASSOC_CHANGE;
125
126         /* Socket Extensions for SCTP
127          * 5.3.1.1 SCTP_ASSOC_CHANGE
128          *
129          * sac_state: 32 bits (signed integer)
130          * This field holds one of a number of values that communicate the
131          * event that happened to the association.
132          */
133         sac->sac_state = state;
134
135         /* Socket Extensions for SCTP
136          * 5.3.1.1 SCTP_ASSOC_CHANGE
137          *
138          * sac_flags: 16 bits (unsigned integer)
139          * Currently unused.
140          */
141         sac->sac_flags = 0;
142
143         /* Socket Extensions for SCTP
144          * 5.3.1.1 SCTP_ASSOC_CHANGE
145          *
146          * sac_length: sizeof (__u32)
147          * This field is the total length of the notification data, including
148          * the notification header.
149          */
150         sac->sac_length = sizeof(struct sctp_assoc_change);
151
152         /* Socket Extensions for SCTP
153          * 5.3.1.1 SCTP_ASSOC_CHANGE
154          *
155          * sac_error:  32 bits (signed integer)
156          *
157          * If the state was reached due to a error condition (e.g.
158          * COMMUNICATION_LOST) any relevant error information is available in
159          * this field. This corresponds to the protocol error codes defined in
160          * [SCTP].
161          */
162         sac->sac_error = error;
163
164         /* Socket Extensions for SCTP
165          * 5.3.1.1 SCTP_ASSOC_CHANGE
166          *
167          * sac_outbound_streams:  16 bits (unsigned integer)
168          * sac_inbound_streams:  16 bits (unsigned integer)
169          *
170          * The maximum number of streams allowed in each direction are
171          * available in sac_outbound_streams and sac_inbound streams.
172          */
173         sac->sac_outbound_streams = outbound;
174         sac->sac_inbound_streams = inbound;
175
176         /* Socket Extensions for SCTP
177          * 5.3.1.1 SCTP_ASSOC_CHANGE
178          *
179          * sac_assoc_id: sizeof (sctp_assoc_t)
180          *
181          * The association id field, holds the identifier for the association.
182          * All notifications for a given association have the same association
183          * identifier.  For TCP style socket, this field is ignored.
184          */
185         sctp_ulpevent_set_owner(event, asoc);
186         sac->sac_assoc_id = sctp_assoc2id(asoc);
187
188         return event;
189
190 fail:
191         return NULL;
192 }
193
194 /* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
195  *
196  * Socket Extensions for SCTP - draft-01
197  * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
198  *
199  * When a destination address on a multi-homed peer encounters a change
200  * an interface details event is sent.
201  */
202 struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
203         const struct sctp_association *asoc,
204         const struct sockaddr_storage *aaddr,
205         int flags, int state, int error, int gfp)
206 {
207         struct sctp_ulpevent *event;
208         struct sctp_paddr_change  *spc;
209         struct sk_buff *skb;
210
211         event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
212                                   MSG_NOTIFICATION, gfp);
213         if (!event)
214                 goto fail;
215
216         skb = sctp_event2skb(event);
217         spc = (struct sctp_paddr_change *)
218                 skb_put(skb, sizeof(struct sctp_paddr_change));
219
220         /* Sockets API Extensions for SCTP
221          * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
222          *
223          * spc_type:
224          *
225          *    It should be SCTP_PEER_ADDR_CHANGE.
226          */
227         spc->spc_type = SCTP_PEER_ADDR_CHANGE;
228
229         /* Sockets API Extensions for SCTP
230          * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
231          *
232          * spc_length: sizeof (__u32)
233          *
234          * This field is the total length of the notification data, including
235          * the notification header.
236          */
237         spc->spc_length = sizeof(struct sctp_paddr_change);
238
239         /* Sockets API Extensions for SCTP
240          * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
241          *
242          * spc_flags: 16 bits (unsigned integer)
243          * Currently unused.
244          */
245         spc->spc_flags = 0;
246
247         /* Sockets API Extensions for SCTP
248          * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
249          *
250          * spc_state:  32 bits (signed integer)
251          *
252          * This field holds one of a number of values that communicate the
253          * event that happened to the address.
254          */
255         spc->spc_state = state;
256
257         /* Sockets API Extensions for SCTP
258          * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
259          *
260          * spc_error:  32 bits (signed integer)
261          *
262          * If the state was reached due to any error condition (e.g.
263          * ADDRESS_UNREACHABLE) any relevant error information is available in
264          * this field.
265          */
266         spc->spc_error = error;
267
268         /* Socket Extensions for SCTP
269          * 5.3.1.1 SCTP_ASSOC_CHANGE
270          *
271          * spc_assoc_id: sizeof (sctp_assoc_t)
272          *
273          * The association id field, holds the identifier for the association.
274          * All notifications for a given association have the same association
275          * identifier.  For TCP style socket, this field is ignored.
276          */
277         sctp_ulpevent_set_owner(event, asoc);
278         spc->spc_assoc_id = sctp_assoc2id(asoc);
279
280         /* Sockets API Extensions for SCTP
281          * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
282          *
283          * spc_aaddr: sizeof (struct sockaddr_storage)
284          *
285          * The affected address field, holds the remote peer's address that is
286          * encountering the change of state.
287          */
288         memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
289
290         /* Map ipv4 address into v4-mapped-on-v6 address.  */
291         sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
292                                         sctp_sk(asoc->base.sk),
293                                         (union sctp_addr *)&spc->spc_aaddr);
294
295         return event;
296
297 fail:
298         return NULL;
299 }
300
301 /* Create and initialize an SCTP_REMOTE_ERROR notification.
302  *
303  * Note: This assumes that the chunk->skb->data already points to the
304  * operation error payload.
305  *
306  * Socket Extensions for SCTP - draft-01
307  * 5.3.1.3 SCTP_REMOTE_ERROR
308  *
309  * A remote peer may send an Operational Error message to its peer.
310  * This message indicates a variety of error conditions on an
311  * association. The entire error TLV as it appears on the wire is
312  * included in a SCTP_REMOTE_ERROR event.  Please refer to the SCTP
313  * specification [SCTP] and any extensions for a list of possible
314  * error formats.
315  */
316 struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
317         const struct sctp_association *asoc, struct sctp_chunk *chunk,
318         __u16 flags, int gfp)
319 {
320         struct sctp_ulpevent *event;
321         struct sctp_remote_error *sre;
322         struct sk_buff *skb;
323         sctp_errhdr_t *ch;
324         __u16 cause;
325         int elen;
326
327         ch = (sctp_errhdr_t *)(chunk->skb->data);
328         cause = ch->cause;
329         elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
330
331         /* Pull off the ERROR header.  */
332         skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
333
334         /* Copy the skb to a new skb with room for us to prepend
335          * notification with.
336          */
337         skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
338                               0, gfp);
339
340         /* Pull off the rest of the cause TLV from the chunk.  */
341         skb_pull(chunk->skb, elen);
342         if (!skb)
343                 goto fail;
344
345         /* Embed the event fields inside the cloned skb.  */
346         event = sctp_skb2event(skb);
347         sctp_ulpevent_init(event, MSG_NOTIFICATION);
348
349         sre = (struct sctp_remote_error *)
350                 skb_push(skb, sizeof(struct sctp_remote_error));
351
352         /* Trim the buffer to the right length.  */
353         skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
354
355         /* Socket Extensions for SCTP
356          * 5.3.1.3 SCTP_REMOTE_ERROR
357          *
358          * sre_type:
359          *   It should be SCTP_REMOTE_ERROR.
360          */
361         sre->sre_type = SCTP_REMOTE_ERROR;
362
363         /*
364          * Socket Extensions for SCTP
365          * 5.3.1.3 SCTP_REMOTE_ERROR
366          *
367          * sre_flags: 16 bits (unsigned integer)
368          *   Currently unused.
369          */
370         sre->sre_flags = 0;
371
372         /* Socket Extensions for SCTP
373          * 5.3.1.3 SCTP_REMOTE_ERROR
374          *
375          * sre_length: sizeof (__u32)
376          *
377          * This field is the total length of the notification data,
378          * including the notification header.
379          */
380         sre->sre_length = skb->len;
381
382         /* Socket Extensions for SCTP
383          * 5.3.1.3 SCTP_REMOTE_ERROR
384          *
385          * sre_error: 16 bits (unsigned integer)
386          * This value represents one of the Operational Error causes defined in
387          * the SCTP specification, in network byte order.
388          */
389         sre->sre_error = cause;
390
391         /* Socket Extensions for SCTP
392          * 5.3.1.3 SCTP_REMOTE_ERROR
393          *
394          * sre_assoc_id: sizeof (sctp_assoc_t)
395          *
396          * The association id field, holds the identifier for the association.
397          * All notifications for a given association have the same association
398          * identifier.  For TCP style socket, this field is ignored.
399          */
400         sctp_ulpevent_set_owner(event, asoc);
401         sre->sre_assoc_id = sctp_assoc2id(asoc);
402
403         return event;
404
405 fail:
406         return NULL;
407 }
408
409 /* Create and initialize a SCTP_SEND_FAILED notification.
410  *
411  * Socket Extensions for SCTP - draft-01
412  * 5.3.1.4 SCTP_SEND_FAILED
413  */
414 struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
415         const struct sctp_association *asoc, struct sctp_chunk *chunk,
416         __u16 flags, __u32 error, int gfp)
417 {
418         struct sctp_ulpevent *event;
419         struct sctp_send_failed *ssf;
420         struct sk_buff *skb;
421
422         /* Pull off any padding. */
423         int len = ntohs(chunk->chunk_hdr->length);
424
425         /* Make skb with more room so we can prepend notification.  */
426         skb = skb_copy_expand(chunk->skb,
427                               sizeof(struct sctp_send_failed), /* headroom */
428                               0,                               /* tailroom */
429                               gfp);
430         if (!skb)
431                 goto fail;
432
433         /* Pull off the common chunk header and DATA header.  */
434         skb_pull(skb, sizeof(struct sctp_data_chunk));
435         len -= sizeof(struct sctp_data_chunk);
436
437         /* Embed the event fields inside the cloned skb.  */
438         event = sctp_skb2event(skb);
439         sctp_ulpevent_init(event, MSG_NOTIFICATION);
440
441         ssf = (struct sctp_send_failed *)
442                 skb_push(skb, sizeof(struct sctp_send_failed));
443
444         /* Socket Extensions for SCTP
445          * 5.3.1.4 SCTP_SEND_FAILED
446          *
447          * ssf_type:
448          * It should be SCTP_SEND_FAILED.
449          */
450         ssf->ssf_type = SCTP_SEND_FAILED;
451
452         /* Socket Extensions for SCTP
453          * 5.3.1.4 SCTP_SEND_FAILED
454          *
455          * ssf_flags: 16 bits (unsigned integer)
456          * The flag value will take one of the following values
457          *
458          * SCTP_DATA_UNSENT - Indicates that the data was never put on
459          *                    the wire.
460          *
461          * SCTP_DATA_SENT   - Indicates that the data was put on the wire.
462          *                    Note that this does not necessarily mean that the
463          *                    data was (or was not) successfully delivered.
464          */
465         ssf->ssf_flags = flags;
466
467         /* Socket Extensions for SCTP
468          * 5.3.1.4 SCTP_SEND_FAILED
469          *
470          * ssf_length: sizeof (__u32)
471          * This field is the total length of the notification data, including
472          * the notification header.
473          */
474         ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
475         skb_trim(skb, ssf->ssf_length);
476
477         /* Socket Extensions for SCTP
478          * 5.3.1.4 SCTP_SEND_FAILED
479          *
480          * ssf_error: 16 bits (unsigned integer)
481          * This value represents the reason why the send failed, and if set,
482          * will be a SCTP protocol error code as defined in [SCTP] section
483          * 3.3.10.
484          */
485         ssf->ssf_error = error;
486
487         /* Socket Extensions for SCTP
488          * 5.3.1.4 SCTP_SEND_FAILED
489          *
490          * ssf_info: sizeof (struct sctp_sndrcvinfo)
491          * The original send information associated with the undelivered
492          * message.
493          */
494         memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
495
496         /* Per TSVWG discussion with Randy. Allow the application to
497          * ressemble a fragmented message.
498          */
499         ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
500
501         /* Socket Extensions for SCTP
502          * 5.3.1.4 SCTP_SEND_FAILED
503          *
504          * ssf_assoc_id: sizeof (sctp_assoc_t)
505          * The association id field, sf_assoc_id, holds the identifier for the
506          * association.  All notifications for a given association have the
507          * same association identifier.  For TCP style socket, this field is
508          * ignored.
509          */
510         sctp_ulpevent_set_owner(event, asoc);
511         ssf->ssf_assoc_id = sctp_assoc2id(asoc);
512         return event;
513
514 fail:
515         return NULL;
516 }
517
518 /* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
519  *
520  * Socket Extensions for SCTP - draft-01
521  * 5.3.1.5 SCTP_SHUTDOWN_EVENT
522  */
523 struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
524         const struct sctp_association *asoc,
525         __u16 flags, int gfp)
526 {
527         struct sctp_ulpevent *event;
528         struct sctp_shutdown_event *sse;
529         struct sk_buff *skb;
530
531         event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
532                                   MSG_NOTIFICATION, gfp);
533         if (!event)
534                 goto fail;
535
536         skb = sctp_event2skb(event);
537         sse = (struct sctp_shutdown_event *)
538                 skb_put(skb, sizeof(struct sctp_shutdown_event));
539
540         /* Socket Extensions for SCTP
541          * 5.3.1.5 SCTP_SHUTDOWN_EVENT
542          *
543          * sse_type
544          * It should be SCTP_SHUTDOWN_EVENT
545          */
546         sse->sse_type = SCTP_SHUTDOWN_EVENT;
547
548         /* Socket Extensions for SCTP
549          * 5.3.1.5 SCTP_SHUTDOWN_EVENT
550          *
551          * sse_flags: 16 bits (unsigned integer)
552          * Currently unused.
553          */
554         sse->sse_flags = 0;
555
556         /* Socket Extensions for SCTP
557          * 5.3.1.5 SCTP_SHUTDOWN_EVENT
558          *
559          * sse_length: sizeof (__u32)
560          * This field is the total length of the notification data, including
561          * the notification header.
562          */
563         sse->sse_length = sizeof(struct sctp_shutdown_event);
564
565         /* Socket Extensions for SCTP
566          * 5.3.1.5 SCTP_SHUTDOWN_EVENT
567          *
568          * sse_assoc_id: sizeof (sctp_assoc_t)
569          * The association id field, holds the identifier for the association.
570          * All notifications for a given association have the same association
571          * identifier.  For TCP style socket, this field is ignored.
572          */
573         sctp_ulpevent_set_owner(event, asoc);
574         sse->sse_assoc_id = sctp_assoc2id(asoc);
575
576         return event;
577
578 fail:
579         return NULL;
580 }
581
582 /* A message has been received.  Package this message as a notification
583  * to pass it to the upper layers.  Go ahead and calculate the sndrcvinfo
584  * even if filtered out later.
585  *
586  * Socket Extensions for SCTP
587  * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
588  */
589 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
590                                                 struct sctp_chunk *chunk,
591                                                 int gfp)
592 {
593         struct sctp_ulpevent *event = NULL;
594         struct sk_buff *skb;
595         size_t padding, len;
596
597         /* Clone the original skb, sharing the data.  */
598         skb = skb_clone(chunk->skb, gfp);
599         if (!skb)
600                 goto fail;
601
602         /* First calculate the padding, so we don't inadvertently
603          * pass up the wrong length to the user.
604          *
605          * RFC 2960 - Section 3.2  Chunk Field Descriptions
606          *
607          * The total length of a chunk(including Type, Length and Value fields)
608          * MUST be a multiple of 4 bytes.  If the length of the chunk is not a
609          * multiple of 4 bytes, the sender MUST pad the chunk with all zero
610          * bytes and this padding is not included in the chunk length field.
611          * The sender should never pad with more than 3 bytes.  The receiver
612          * MUST ignore the padding bytes.
613          */
614         len = ntohs(chunk->chunk_hdr->length);
615         padding = WORD_ROUND(len) - len;
616
617         /* Fixup cloned skb with just this chunks data.  */
618         skb_trim(skb, chunk->chunk_end - padding - skb->data);
619
620         /* Embed the event fields inside the cloned skb.  */
621         event = sctp_skb2event(skb);
622
623         /* Initialize event with flags 0.  */
624         sctp_ulpevent_init(event, 0);
625
626         sctp_ulpevent_receive_data(event, asoc);
627
628         event->stream = ntohs(chunk->subh.data_hdr->stream);
629         event->ssn = ntohs(chunk->subh.data_hdr->ssn);
630         event->ppid = chunk->subh.data_hdr->ppid;
631         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
632                 event->flags |= MSG_UNORDERED;
633                 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
634         }
635         event->tsn = ntohl(chunk->subh.data_hdr->tsn);
636         event->msg_flags |= chunk->chunk_hdr->flags;
637         event->iif = sctp_chunk_iif(chunk);
638
639 fail:
640         return event;
641 }
642
643 /* Create a partial delivery related event.
644  *
645  * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
646  *
647  *   When a reciever is engaged in a partial delivery of a
648  *   message this notification will be used to inidicate
649  *   various events.
650  */
651 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
652         const struct sctp_association *asoc, __u32 indication, int gfp)
653 {
654         struct sctp_ulpevent *event;
655         struct sctp_pdapi_event *pd;
656         struct sk_buff *skb;
657
658         event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
659                                   MSG_NOTIFICATION, gfp);
660         if (!event)
661                 goto fail;
662
663         skb = sctp_event2skb(event);
664         pd = (struct sctp_pdapi_event *)
665                 skb_put(skb, sizeof(struct sctp_pdapi_event));
666
667         /* pdapi_type
668          *   It should be SCTP_PARTIAL_DELIVERY_EVENT
669          *
670          * pdapi_flags: 16 bits (unsigned integer)
671          *   Currently unused.
672          */
673         pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
674         pd->pdapi_flags = 0;
675
676         /* pdapi_length: 32 bits (unsigned integer)
677          *
678          * This field is the total length of the notification data, including
679          * the notification header.  It will generally be sizeof (struct
680          * sctp_pdapi_event).
681          */
682         pd->pdapi_length = sizeof(struct sctp_pdapi_event);
683
684         /*  pdapi_indication: 32 bits (unsigned integer)
685          *
686          * This field holds the indication being sent to the application.
687          */
688         pd->pdapi_indication = indication;
689
690         /*  pdapi_assoc_id: sizeof (sctp_assoc_t)
691          *
692          * The association id field, holds the identifier for the association.
693          */
694         sctp_ulpevent_set_owner(event, asoc);
695         pd->pdapi_assoc_id = sctp_assoc2id(asoc);
696
697         return event;
698 fail:
699         return NULL;
700 }
701
702 /* Return the notification type, assuming this is a notification
703  * event.
704  */
705 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
706 {
707         union sctp_notification *notification;
708         struct sk_buff *skb;
709
710         skb = sctp_event2skb((struct sctp_ulpevent *)event);
711         notification = (union sctp_notification *) skb->data;
712         return notification->sn_header.sn_type;
713 }
714
715 /* Copy out the sndrcvinfo into a msghdr.  */
716 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
717                                    struct msghdr *msghdr)
718 {
719         struct sctp_sndrcvinfo sinfo;
720
721         if (sctp_ulpevent_is_notification(event))
722                 return;
723
724         /* Sockets API Extensions for SCTP
725          * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
726          *
727          * sinfo_stream: 16 bits (unsigned integer)
728          *
729          * For recvmsg() the SCTP stack places the message's stream number in
730          * this value.
731         */
732         sinfo.sinfo_stream = event->stream;
733         /* sinfo_ssn: 16 bits (unsigned integer)
734          *
735          * For recvmsg() this value contains the stream sequence number that
736          * the remote endpoint placed in the DATA chunk.  For fragmented
737          * messages this is the same number for all deliveries of the message
738          * (if more than one recvmsg() is needed to read the message).
739          */
740         sinfo.sinfo_ssn = event->ssn;
741         /* sinfo_ppid: 32 bits (unsigned integer)
742          *
743          * In recvmsg() this value is
744          * the same information that was passed by the upper layer in the peer
745          * application.  Please note that byte order issues are NOT accounted
746          * for and this information is passed opaquely by the SCTP stack from
747          * one end to the other.
748          */
749         sinfo.sinfo_ppid = event->ppid;
750         /* sinfo_flags: 16 bits (unsigned integer)
751          *
752          * This field may contain any of the following flags and is composed of
753          * a bitwise OR of these values.
754          *
755          * recvmsg() flags:
756          *
757          * MSG_UNORDERED - This flag is present when the message was sent
758          *                 non-ordered.
759          */
760         sinfo.sinfo_flags = event->flags;
761         /* sinfo_tsn: 32 bit (unsigned integer)
762          *
763          * For the receiving side, this field holds a TSN that was 
764          * assigned to one of the SCTP Data Chunks.
765          */
766         sinfo.sinfo_tsn = event->tsn;
767         /* sinfo_cumtsn: 32 bit (unsigned integer)
768          *
769          * This field will hold the current cumulative TSN as
770          * known by the underlying SCTP layer.  Note this field is
771          * ignored when sending and only valid for a receive
772          * operation when sinfo_flags are set to MSG_UNORDERED.
773          */
774         sinfo.sinfo_cumtsn = event->cumtsn;
775         /* sinfo_assoc_id: sizeof (sctp_assoc_t)
776          *
777          * The association handle field, sinfo_assoc_id, holds the identifier
778          * for the association announced in the COMMUNICATION_UP notification.
779          * All notifications for a given association have the same identifier.
780          * Ignored for one-to-one style sockets.
781          */
782         sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
783
784         /* These fields are not used while receiving. */
785         sinfo.sinfo_context = 0;
786         sinfo.sinfo_timetolive = 0;
787
788         put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
789                  sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
790 }
791
792 /* Stub skb destructor.  */
793 static void sctp_stub_rfree(struct sk_buff *skb)
794 {
795 /* WARNING:  This function is just a warning not to use the
796  * skb destructor.  If the skb is shared, we may get the destructor
797  * callback on some processor that does not own the sock_lock.  This
798  * was occuring with PACKET socket applications that were monitoring
799  * our skbs.   We can't take the sock_lock, because we can't risk
800  * recursing if we do really own the sock lock.  Instead, do all
801  * of our rwnd manipulation while we own the sock_lock outright.
802  */
803 }
804
805 /* Hold the association in case the msg_name needs read out of
806  * the association.
807  */
808 static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
809                                            const struct sctp_association *asoc)
810 {
811         struct sk_buff *skb;
812
813         /* Cast away the const, as we are just wanting to
814          * bump the reference count.
815          */
816         sctp_association_hold((struct sctp_association *)asoc);
817         skb = sctp_event2skb(event);
818         skb->sk = asoc->base.sk;
819         event->asoc = (struct sctp_association *)asoc;
820         skb->destructor = sctp_stub_rfree;
821 }
822
823 /* A simple destructor to give up the reference to the association. */
824 static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
825 {
826         sctp_association_put(event->asoc);
827 }
828
829 /* Do accounting for bytes received and hold a reference to the association
830  * for each skb.
831  */
832 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
833                                        struct sctp_association *asoc)
834 {
835         struct sk_buff *skb, *frag;
836
837         skb = sctp_event2skb(event);
838         /* Set the owner and charge rwnd for bytes received.  */
839         sctp_ulpevent_set_owner(event, asoc);
840         sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
841
842         if (!skb->data_len)
843                 return;
844
845         /* Note:  Not clearing the entire event struct as this is just a
846          * fragment of the real event.  However, we still need to do rwnd
847          * accounting.
848          * In general, the skb passed from IP can have only 1 level of
849          * fragments. But we allow multiple levels of fragments. 
850          */
851         for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
852                 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
853         }
854 }
855
856 /* Do accounting for bytes just read by user and release the references to
857  * the association.
858  */ 
859 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
860 {
861         struct sk_buff *skb, *frag;
862
863         /* Current stack structures assume that the rcv buffer is
864          * per socket.   For UDP style sockets this is not true as
865          * multiple associations may be on a single UDP-style socket.
866          * Use the local private area of the skb to track the owning
867          * association.
868          */
869
870         skb = sctp_event2skb(event);
871         sctp_assoc_rwnd_increase(event->asoc, skb_headlen(skb));
872
873         if (!skb->data_len)
874                 goto done;
875
876         /* Don't forget the fragments. */
877         for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
878                 /* NOTE:  skb_shinfos are recursive. Although IP returns
879                  * skb's with only 1 level of fragments, SCTP reassembly can
880                  * increase the levels.
881                  */
882                 sctp_ulpevent_release_data(sctp_skb2event(frag));
883         }
884
885 done:
886         sctp_ulpevent_release_owner(event);
887 }
888
889 /* Free a ulpevent that has an owner.  It includes releasing the reference
890  * to the owner, updating the rwnd in case of a DATA event and freeing the
891  * skb.
892  * See comments in sctp_stub_rfree().
893  */
894 void sctp_ulpevent_free(struct sctp_ulpevent *event)
895 {
896         if (sctp_ulpevent_is_notification(event))
897                 sctp_ulpevent_release_owner(event);
898         else
899                 sctp_ulpevent_release_data(event);
900
901         kfree_skb(sctp_event2skb(event));
902 }
903
904 /* Purge the skb lists holding ulpevents. */
905 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
906 {
907         struct sk_buff *skb;
908         while ((skb = skb_dequeue(list)) != NULL)
909                 sctp_ulpevent_free(sctp_skb2event(skb));
910 }