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