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