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