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