upgrade to linux 2.6.10-1.12_FC2
[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_shutdown_event),
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 /* Create and initialize a SCTP_ADAPTION_INDICATION notification.
617  *
618  * Socket Extensions for SCTP
619  * 5.3.1.6 SCTP_ADAPTION_INDICATION
620  */
621 struct sctp_ulpevent *sctp_ulpevent_make_adaption_indication(
622         const struct sctp_association *asoc, int gfp)
623 {
624         struct sctp_ulpevent *event;
625         struct sctp_adaption_event *sai;
626         struct sk_buff *skb;
627
628         event = sctp_ulpevent_new(sizeof(struct sctp_adaption_event),
629                                   MSG_NOTIFICATION, gfp);
630         if (!event)
631                 goto fail;
632
633         skb = sctp_event2skb(event);
634         sai = (struct sctp_adaption_event *)
635                 skb_put(skb, sizeof(struct sctp_adaption_event));
636
637         sai->sai_type = SCTP_ADAPTION_INDICATION;
638         sai->sai_flags = 0;
639         sai->sai_length = sizeof(struct sctp_adaption_event);
640         sai->sai_adaption_ind = asoc->peer.adaption_ind;
641         sctp_ulpevent_set_owner(event, asoc);
642         sai->sai_assoc_id = sctp_assoc2id(asoc);
643
644         return event;
645
646 fail:
647         return NULL;
648 }
649
650 /* A message has been received.  Package this message as a notification
651  * to pass it to the upper layers.  Go ahead and calculate the sndrcvinfo
652  * even if filtered out later.
653  *
654  * Socket Extensions for SCTP
655  * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
656  */
657 struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
658                                                 struct sctp_chunk *chunk,
659                                                 int gfp)
660 {
661         struct sctp_ulpevent *event = NULL;
662         struct sk_buff *skb;
663         size_t padding, len;
664
665         /* Clone the original skb, sharing the data.  */
666         skb = skb_clone(chunk->skb, gfp);
667         if (!skb)
668                 goto fail;
669
670         /* First calculate the padding, so we don't inadvertently
671          * pass up the wrong length to the user.
672          *
673          * RFC 2960 - Section 3.2  Chunk Field Descriptions
674          *
675          * The total length of a chunk(including Type, Length and Value fields)
676          * MUST be a multiple of 4 bytes.  If the length of the chunk is not a
677          * multiple of 4 bytes, the sender MUST pad the chunk with all zero
678          * bytes and this padding is not included in the chunk length field.
679          * The sender should never pad with more than 3 bytes.  The receiver
680          * MUST ignore the padding bytes.
681          */
682         len = ntohs(chunk->chunk_hdr->length);
683         padding = WORD_ROUND(len) - len;
684
685         /* Fixup cloned skb with just this chunks data.  */
686         skb_trim(skb, chunk->chunk_end - padding - skb->data);
687
688         /* Embed the event fields inside the cloned skb.  */
689         event = sctp_skb2event(skb);
690
691         /* Initialize event with flags 0.  */
692         sctp_ulpevent_init(event, 0);
693
694         sctp_ulpevent_receive_data(event, asoc);
695
696         event->stream = ntohs(chunk->subh.data_hdr->stream);
697         event->ssn = ntohs(chunk->subh.data_hdr->ssn);
698         event->ppid = chunk->subh.data_hdr->ppid;
699         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
700                 event->flags |= MSG_UNORDERED;
701                 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
702         }
703         event->tsn = ntohl(chunk->subh.data_hdr->tsn);
704         event->msg_flags |= chunk->chunk_hdr->flags;
705         event->iif = sctp_chunk_iif(chunk);
706
707 fail:
708         return event;
709 }
710
711 /* Create a partial delivery related event.
712  *
713  * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
714  *
715  *   When a receiver is engaged in a partial delivery of a
716  *   message this notification will be used to indicate
717  *   various events.
718  */
719 struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
720         const struct sctp_association *asoc, __u32 indication, int gfp)
721 {
722         struct sctp_ulpevent *event;
723         struct sctp_pdapi_event *pd;
724         struct sk_buff *skb;
725
726         event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
727                                   MSG_NOTIFICATION, gfp);
728         if (!event)
729                 goto fail;
730
731         skb = sctp_event2skb(event);
732         pd = (struct sctp_pdapi_event *)
733                 skb_put(skb, sizeof(struct sctp_pdapi_event));
734
735         /* pdapi_type
736          *   It should be SCTP_PARTIAL_DELIVERY_EVENT
737          *
738          * pdapi_flags: 16 bits (unsigned integer)
739          *   Currently unused.
740          */
741         pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
742         pd->pdapi_flags = 0;
743
744         /* pdapi_length: 32 bits (unsigned integer)
745          *
746          * This field is the total length of the notification data, including
747          * the notification header.  It will generally be sizeof (struct
748          * sctp_pdapi_event).
749          */
750         pd->pdapi_length = sizeof(struct sctp_pdapi_event);
751
752         /*  pdapi_indication: 32 bits (unsigned integer)
753          *
754          * This field holds the indication being sent to the application.
755          */
756         pd->pdapi_indication = indication;
757
758         /*  pdapi_assoc_id: sizeof (sctp_assoc_t)
759          *
760          * The association id field, holds the identifier for the association.
761          */
762         sctp_ulpevent_set_owner(event, asoc);
763         pd->pdapi_assoc_id = sctp_assoc2id(asoc);
764
765         return event;
766 fail:
767         return NULL;
768 }
769
770 /* Return the notification type, assuming this is a notification
771  * event.
772  */
773 __u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
774 {
775         union sctp_notification *notification;
776         struct sk_buff *skb;
777
778         skb = sctp_event2skb((struct sctp_ulpevent *)event);
779         notification = (union sctp_notification *) skb->data;
780         return notification->sn_header.sn_type;
781 }
782
783 /* Copy out the sndrcvinfo into a msghdr.  */
784 void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
785                                    struct msghdr *msghdr)
786 {
787         struct sctp_sndrcvinfo sinfo;
788
789         if (sctp_ulpevent_is_notification(event))
790                 return;
791
792         /* Sockets API Extensions for SCTP
793          * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
794          *
795          * sinfo_stream: 16 bits (unsigned integer)
796          *
797          * For recvmsg() the SCTP stack places the message's stream number in
798          * this value.
799         */
800         sinfo.sinfo_stream = event->stream;
801         /* sinfo_ssn: 16 bits (unsigned integer)
802          *
803          * For recvmsg() this value contains the stream sequence number that
804          * the remote endpoint placed in the DATA chunk.  For fragmented
805          * messages this is the same number for all deliveries of the message
806          * (if more than one recvmsg() is needed to read the message).
807          */
808         sinfo.sinfo_ssn = event->ssn;
809         /* sinfo_ppid: 32 bits (unsigned integer)
810          *
811          * In recvmsg() this value is
812          * the same information that was passed by the upper layer in the peer
813          * application.  Please note that byte order issues are NOT accounted
814          * for and this information is passed opaquely by the SCTP stack from
815          * one end to the other.
816          */
817         sinfo.sinfo_ppid = event->ppid;
818         /* sinfo_flags: 16 bits (unsigned integer)
819          *
820          * This field may contain any of the following flags and is composed of
821          * a bitwise OR of these values.
822          *
823          * recvmsg() flags:
824          *
825          * MSG_UNORDERED - This flag is present when the message was sent
826          *                 non-ordered.
827          */
828         sinfo.sinfo_flags = event->flags;
829         /* sinfo_tsn: 32 bit (unsigned integer)
830          *
831          * For the receiving side, this field holds a TSN that was 
832          * assigned to one of the SCTP Data Chunks.
833          */
834         sinfo.sinfo_tsn = event->tsn;
835         /* sinfo_cumtsn: 32 bit (unsigned integer)
836          *
837          * This field will hold the current cumulative TSN as
838          * known by the underlying SCTP layer.  Note this field is
839          * ignored when sending and only valid for a receive
840          * operation when sinfo_flags are set to MSG_UNORDERED.
841          */
842         sinfo.sinfo_cumtsn = event->cumtsn;
843         /* sinfo_assoc_id: sizeof (sctp_assoc_t)
844          *
845          * The association handle field, sinfo_assoc_id, holds the identifier
846          * for the association announced in the COMMUNICATION_UP notification.
847          * All notifications for a given association have the same identifier.
848          * Ignored for one-to-one style sockets.
849          */
850         sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
851
852         /* These fields are not used while receiving. */
853         sinfo.sinfo_context = 0;
854         sinfo.sinfo_timetolive = 0;
855
856         put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
857                  sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
858 }
859
860 /* Do accounting for bytes received and hold a reference to the association
861  * for each skb.
862  */
863 static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
864                                        struct sctp_association *asoc)
865 {
866         struct sk_buff *skb, *frag;
867
868         skb = sctp_event2skb(event);
869         /* Set the owner and charge rwnd for bytes received.  */
870         sctp_ulpevent_set_owner(event, asoc);
871         sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
872
873         if (!skb->data_len)
874                 return;
875
876         /* Note:  Not clearing the entire event struct as this is just a
877          * fragment of the real event.  However, we still need to do rwnd
878          * accounting.
879          * In general, the skb passed from IP can have only 1 level of
880          * fragments. But we allow multiple levels of fragments. 
881          */
882         for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
883                 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
884         }
885 }
886
887 /* Do accounting for bytes just read by user and release the references to
888  * the association.
889  */ 
890 static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
891 {
892         struct sk_buff *skb, *frag;
893
894         /* Current stack structures assume that the rcv buffer is
895          * per socket.   For UDP style sockets this is not true as
896          * multiple associations may be on a single UDP-style socket.
897          * Use the local private area of the skb to track the owning
898          * association.
899          */
900
901         skb = sctp_event2skb(event);
902         sctp_assoc_rwnd_increase(event->asoc, skb_headlen(skb));
903
904         if (!skb->data_len)
905                 goto done;
906
907         /* Don't forget the fragments. */
908         for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
909                 /* NOTE:  skb_shinfos are recursive. Although IP returns
910                  * skb's with only 1 level of fragments, SCTP reassembly can
911                  * increase the levels.
912                  */
913                 sctp_ulpevent_release_data(sctp_skb2event(frag));
914         }
915
916 done:
917         sctp_ulpevent_release_owner(event);
918 }
919
920 /* Free a ulpevent that has an owner.  It includes releasing the reference
921  * to the owner, updating the rwnd in case of a DATA event and freeing the
922  * skb.
923  * See comments in sctp_stub_rfree().
924  */
925 void sctp_ulpevent_free(struct sctp_ulpevent *event)
926 {
927         if (sctp_ulpevent_is_notification(event))
928                 sctp_ulpevent_release_owner(event);
929         else
930                 sctp_ulpevent_release_data(event);
931
932         kfree_skb(sctp_event2skb(event));
933 }
934
935 /* Purge the skb lists holding ulpevents. */
936 void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
937 {
938         struct sk_buff *skb;
939         while ((skb = skb_dequeue(list)) != NULL)
940                 sctp_ulpevent_free(sctp_skb2event(skb));
941 }