blob: e255a709f1b7ed867d4985b9da51f86fdee96cd5 [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);
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -070054static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* Initialize an ULP event from an given skb. */
Vlad Yasevich331c4ee2006-10-09 21:34:04 -070058SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,
59 int msg_flags,
60 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 memset(event, 0, sizeof(struct sctp_ulpevent));
63 event->msg_flags = msg_flags;
Vlad Yasevich331c4ee2006-10-09 21:34:04 -070064 event->rmem_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/* Create a new sctp_ulpevent. */
68SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
Al Virodd0fc662005-10-07 07:46:04 +010069 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
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);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -070079 sctp_ulpevent_init(event, msg_flags, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 return event;
82
83fail:
84 return NULL;
85}
86
87/* Is this a MSG_NOTIFICATION? */
88int 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 */
96static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 event->asoc = (struct sctp_association *)asoc;
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700107 atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
108 sctp_skb_set_owner_r(skb, asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111/* A simple destructor to give up the reference to the association. */
112static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
113{
Neil Horman049b3ff2005-11-11 16:08:24 -0800114 struct sctp_association *asoc = event->asoc;
Neil Horman049b3ff2005-11-11 16:08:24 -0800115
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700116 atomic_sub(event->rmem_len, &asoc->rmem_alloc);
Neil Horman049b3ff2005-11-11 16:08:24 -0800117 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
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 */
131struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
132 const struct sctp_association *asoc,
133 __u16 flags, __u16 state, __u16 error, __u16 outbound,
Al Virodd0fc662005-10-07 07:46:04 +0100134 __u16 inbound, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct sctp_ulpevent *event;
137 struct sctp_assoc_change *sac;
138 struct sk_buff *skb;
139
140 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
141 MSG_NOTIFICATION, gfp);
142 if (!event)
143 goto fail;
144 skb = sctp_event2skb(event);
145 sac = (struct sctp_assoc_change *)
146 skb_put(skb, sizeof(struct sctp_assoc_change));
147
148 /* Socket Extensions for SCTP
149 * 5.3.1.1 SCTP_ASSOC_CHANGE
150 *
151 * sac_type:
152 * It should be SCTP_ASSOC_CHANGE.
153 */
154 sac->sac_type = SCTP_ASSOC_CHANGE;
155
156 /* Socket Extensions for SCTP
157 * 5.3.1.1 SCTP_ASSOC_CHANGE
158 *
159 * sac_state: 32 bits (signed integer)
160 * This field holds one of a number of values that communicate the
161 * event that happened to the association.
162 */
163 sac->sac_state = state;
164
165 /* Socket Extensions for SCTP
166 * 5.3.1.1 SCTP_ASSOC_CHANGE
167 *
168 * sac_flags: 16 bits (unsigned integer)
169 * Currently unused.
170 */
171 sac->sac_flags = 0;
172
173 /* Socket Extensions for SCTP
174 * 5.3.1.1 SCTP_ASSOC_CHANGE
175 *
176 * sac_length: sizeof (__u32)
177 * This field is the total length of the notification data, including
178 * the notification header.
179 */
180 sac->sac_length = sizeof(struct sctp_assoc_change);
181
182 /* Socket Extensions for SCTP
183 * 5.3.1.1 SCTP_ASSOC_CHANGE
184 *
185 * sac_error: 32 bits (signed integer)
186 *
187 * If the state was reached due to a error condition (e.g.
188 * COMMUNICATION_LOST) any relevant error information is available in
189 * this field. This corresponds to the protocol error codes defined in
190 * [SCTP].
191 */
192 sac->sac_error = error;
193
194 /* Socket Extensions for SCTP
195 * 5.3.1.1 SCTP_ASSOC_CHANGE
196 *
197 * sac_outbound_streams: 16 bits (unsigned integer)
198 * sac_inbound_streams: 16 bits (unsigned integer)
199 *
200 * The maximum number of streams allowed in each direction are
201 * available in sac_outbound_streams and sac_inbound streams.
202 */
203 sac->sac_outbound_streams = outbound;
204 sac->sac_inbound_streams = inbound;
205
206 /* Socket Extensions for SCTP
207 * 5.3.1.1 SCTP_ASSOC_CHANGE
208 *
209 * sac_assoc_id: sizeof (sctp_assoc_t)
210 *
211 * The association id field, holds the identifier for the association.
212 * All notifications for a given association have the same association
213 * identifier. For TCP style socket, this field is ignored.
214 */
215 sctp_ulpevent_set_owner(event, asoc);
216 sac->sac_assoc_id = sctp_assoc2id(asoc);
217
218 return event;
219
220fail:
221 return NULL;
222}
223
224/* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
225 *
226 * Socket Extensions for SCTP - draft-01
227 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
228 *
229 * When a destination address on a multi-homed peer encounters a change
230 * an interface details event is sent.
231 */
232struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
233 const struct sctp_association *asoc,
234 const struct sockaddr_storage *aaddr,
Al Virodd0fc662005-10-07 07:46:04 +0100235 int flags, int state, int error, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct sctp_ulpevent *event;
238 struct sctp_paddr_change *spc;
239 struct sk_buff *skb;
240
241 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
242 MSG_NOTIFICATION, gfp);
243 if (!event)
244 goto fail;
245
246 skb = sctp_event2skb(event);
247 spc = (struct sctp_paddr_change *)
248 skb_put(skb, sizeof(struct sctp_paddr_change));
249
250 /* Sockets API Extensions for SCTP
251 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
252 *
253 * spc_type:
254 *
255 * It should be SCTP_PEER_ADDR_CHANGE.
256 */
257 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
258
259 /* Sockets API Extensions for SCTP
260 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
261 *
262 * spc_length: sizeof (__u32)
263 *
264 * This field is the total length of the notification data, including
265 * the notification header.
266 */
267 spc->spc_length = sizeof(struct sctp_paddr_change);
268
269 /* Sockets API Extensions for SCTP
270 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
271 *
272 * spc_flags: 16 bits (unsigned integer)
273 * Currently unused.
274 */
275 spc->spc_flags = 0;
276
277 /* Sockets API Extensions for SCTP
278 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
279 *
280 * spc_state: 32 bits (signed integer)
281 *
282 * This field holds one of a number of values that communicate the
283 * event that happened to the address.
284 */
285 spc->spc_state = state;
286
287 /* Sockets API Extensions for SCTP
288 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
289 *
290 * spc_error: 32 bits (signed integer)
291 *
292 * If the state was reached due to any error condition (e.g.
293 * ADDRESS_UNREACHABLE) any relevant error information is available in
294 * this field.
295 */
296 spc->spc_error = error;
297
298 /* Socket Extensions for SCTP
299 * 5.3.1.1 SCTP_ASSOC_CHANGE
300 *
301 * spc_assoc_id: sizeof (sctp_assoc_t)
302 *
303 * The association id field, holds the identifier for the association.
304 * All notifications for a given association have the same association
305 * identifier. For TCP style socket, this field is ignored.
306 */
307 sctp_ulpevent_set_owner(event, asoc);
308 spc->spc_assoc_id = sctp_assoc2id(asoc);
309
310 /* Sockets API Extensions for SCTP
311 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
312 *
313 * spc_aaddr: sizeof (struct sockaddr_storage)
314 *
315 * The affected address field, holds the remote peer's address that is
316 * encountering the change of state.
317 */
318 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
319
320 /* Map ipv4 address into v4-mapped-on-v6 address. */
321 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
322 sctp_sk(asoc->base.sk),
323 (union sctp_addr *)&spc->spc_aaddr);
324
325 return event;
326
327fail:
328 return NULL;
329}
330
331/* Create and initialize an SCTP_REMOTE_ERROR notification.
332 *
333 * Note: This assumes that the chunk->skb->data already points to the
334 * operation error payload.
335 *
336 * Socket Extensions for SCTP - draft-01
337 * 5.3.1.3 SCTP_REMOTE_ERROR
338 *
339 * A remote peer may send an Operational Error message to its peer.
340 * This message indicates a variety of error conditions on an
341 * association. The entire error TLV as it appears on the wire is
342 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
343 * specification [SCTP] and any extensions for a list of possible
344 * error formats.
345 */
346struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
347 const struct sctp_association *asoc, struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100348 __u16 flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct sctp_ulpevent *event;
351 struct sctp_remote_error *sre;
352 struct sk_buff *skb;
353 sctp_errhdr_t *ch;
Al Viro9f81bcd2006-11-20 17:26:34 -0800354 __be16 cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int elen;
356
357 ch = (sctp_errhdr_t *)(chunk->skb->data);
358 cause = ch->cause;
359 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
360
361 /* Pull off the ERROR header. */
362 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
363
364 /* Copy the skb to a new skb with room for us to prepend
365 * notification with.
366 */
367 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
368 0, gfp);
369
370 /* Pull off the rest of the cause TLV from the chunk. */
371 skb_pull(chunk->skb, elen);
372 if (!skb)
373 goto fail;
374
375 /* Embed the event fields inside the cloned skb. */
376 event = sctp_skb2event(skb);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700377 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 sre = (struct sctp_remote_error *)
380 skb_push(skb, sizeof(struct sctp_remote_error));
381
382 /* Trim the buffer to the right length. */
383 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
384
385 /* Socket Extensions for SCTP
386 * 5.3.1.3 SCTP_REMOTE_ERROR
387 *
388 * sre_type:
389 * It should be SCTP_REMOTE_ERROR.
390 */
391 sre->sre_type = SCTP_REMOTE_ERROR;
392
393 /*
394 * Socket Extensions for SCTP
395 * 5.3.1.3 SCTP_REMOTE_ERROR
396 *
397 * sre_flags: 16 bits (unsigned integer)
398 * Currently unused.
399 */
400 sre->sre_flags = 0;
401
402 /* Socket Extensions for SCTP
403 * 5.3.1.3 SCTP_REMOTE_ERROR
404 *
405 * sre_length: sizeof (__u32)
406 *
407 * This field is the total length of the notification data,
408 * including the notification header.
409 */
410 sre->sre_length = skb->len;
411
412 /* Socket Extensions for SCTP
413 * 5.3.1.3 SCTP_REMOTE_ERROR
414 *
415 * sre_error: 16 bits (unsigned integer)
416 * This value represents one of the Operational Error causes defined in
417 * the SCTP specification, in network byte order.
418 */
419 sre->sre_error = cause;
420
421 /* Socket Extensions for SCTP
422 * 5.3.1.3 SCTP_REMOTE_ERROR
423 *
424 * sre_assoc_id: sizeof (sctp_assoc_t)
425 *
426 * The association id field, holds the identifier for the association.
427 * All notifications for a given association have the same association
428 * identifier. For TCP style socket, this field is ignored.
429 */
430 sctp_ulpevent_set_owner(event, asoc);
431 sre->sre_assoc_id = sctp_assoc2id(asoc);
432
433 return event;
434
435fail:
436 return NULL;
437}
438
439/* Create and initialize a SCTP_SEND_FAILED notification.
440 *
441 * Socket Extensions for SCTP - draft-01
442 * 5.3.1.4 SCTP_SEND_FAILED
443 */
444struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
445 const struct sctp_association *asoc, struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100446 __u16 flags, __u32 error, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct sctp_ulpevent *event;
449 struct sctp_send_failed *ssf;
450 struct sk_buff *skb;
451
452 /* Pull off any padding. */
453 int len = ntohs(chunk->chunk_hdr->length);
454
455 /* Make skb with more room so we can prepend notification. */
456 skb = skb_copy_expand(chunk->skb,
457 sizeof(struct sctp_send_failed), /* headroom */
458 0, /* tailroom */
459 gfp);
460 if (!skb)
461 goto fail;
462
463 /* Pull off the common chunk header and DATA header. */
464 skb_pull(skb, sizeof(struct sctp_data_chunk));
465 len -= sizeof(struct sctp_data_chunk);
466
467 /* Embed the event fields inside the cloned skb. */
468 event = sctp_skb2event(skb);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700469 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 ssf = (struct sctp_send_failed *)
472 skb_push(skb, sizeof(struct sctp_send_failed));
473
474 /* Socket Extensions for SCTP
475 * 5.3.1.4 SCTP_SEND_FAILED
476 *
477 * ssf_type:
478 * It should be SCTP_SEND_FAILED.
479 */
480 ssf->ssf_type = SCTP_SEND_FAILED;
481
482 /* Socket Extensions for SCTP
483 * 5.3.1.4 SCTP_SEND_FAILED
484 *
485 * ssf_flags: 16 bits (unsigned integer)
486 * The flag value will take one of the following values
487 *
488 * SCTP_DATA_UNSENT - Indicates that the data was never put on
489 * the wire.
490 *
491 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
492 * Note that this does not necessarily mean that the
493 * data was (or was not) successfully delivered.
494 */
495 ssf->ssf_flags = flags;
496
497 /* Socket Extensions for SCTP
498 * 5.3.1.4 SCTP_SEND_FAILED
499 *
500 * ssf_length: sizeof (__u32)
501 * This field is the total length of the notification data, including
502 * the notification header.
503 */
504 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
505 skb_trim(skb, ssf->ssf_length);
506
507 /* Socket Extensions for SCTP
508 * 5.3.1.4 SCTP_SEND_FAILED
509 *
510 * ssf_error: 16 bits (unsigned integer)
511 * This value represents the reason why the send failed, and if set,
512 * will be a SCTP protocol error code as defined in [SCTP] section
513 * 3.3.10.
514 */
515 ssf->ssf_error = error;
516
517 /* Socket Extensions for SCTP
518 * 5.3.1.4 SCTP_SEND_FAILED
519 *
520 * ssf_info: sizeof (struct sctp_sndrcvinfo)
521 * The original send information associated with the undelivered
522 * message.
523 */
524 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
525
526 /* Per TSVWG discussion with Randy. Allow the application to
527 * ressemble a fragmented message.
528 */
529 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
530
531 /* Socket Extensions for SCTP
532 * 5.3.1.4 SCTP_SEND_FAILED
533 *
534 * ssf_assoc_id: sizeof (sctp_assoc_t)
535 * The association id field, sf_assoc_id, holds the identifier for the
536 * association. All notifications for a given association have the
537 * same association identifier. For TCP style socket, this field is
538 * ignored.
539 */
540 sctp_ulpevent_set_owner(event, asoc);
541 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
542 return event;
543
544fail:
545 return NULL;
546}
547
548/* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
549 *
550 * Socket Extensions for SCTP - draft-01
551 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
552 */
553struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
554 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +0100555 __u16 flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct sctp_ulpevent *event;
558 struct sctp_shutdown_event *sse;
559 struct sk_buff *skb;
560
561 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
562 MSG_NOTIFICATION, gfp);
563 if (!event)
564 goto fail;
565
566 skb = sctp_event2skb(event);
567 sse = (struct sctp_shutdown_event *)
568 skb_put(skb, sizeof(struct sctp_shutdown_event));
569
570 /* Socket Extensions for SCTP
571 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
572 *
573 * sse_type
574 * It should be SCTP_SHUTDOWN_EVENT
575 */
576 sse->sse_type = SCTP_SHUTDOWN_EVENT;
577
578 /* Socket Extensions for SCTP
579 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
580 *
581 * sse_flags: 16 bits (unsigned integer)
582 * Currently unused.
583 */
584 sse->sse_flags = 0;
585
586 /* Socket Extensions for SCTP
587 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
588 *
589 * sse_length: sizeof (__u32)
590 * This field is the total length of the notification data, including
591 * the notification header.
592 */
593 sse->sse_length = sizeof(struct sctp_shutdown_event);
594
595 /* Socket Extensions for SCTP
596 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
597 *
598 * sse_assoc_id: sizeof (sctp_assoc_t)
599 * The association id field, holds the identifier for the association.
600 * All notifications for a given association have the same association
601 * identifier. For TCP style socket, this field is ignored.
602 */
603 sctp_ulpevent_set_owner(event, asoc);
604 sse->sse_assoc_id = sctp_assoc2id(asoc);
605
606 return event;
607
608fail:
609 return NULL;
610}
611
612/* Create and initialize a SCTP_ADAPTION_INDICATION notification.
613 *
614 * Socket Extensions for SCTP
615 * 5.3.1.6 SCTP_ADAPTION_INDICATION
616 */
617struct sctp_ulpevent *sctp_ulpevent_make_adaption_indication(
Al Virodd0fc662005-10-07 07:46:04 +0100618 const struct sctp_association *asoc, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct sctp_ulpevent *event;
621 struct sctp_adaption_event *sai;
622 struct sk_buff *skb;
623
624 event = sctp_ulpevent_new(sizeof(struct sctp_adaption_event),
625 MSG_NOTIFICATION, gfp);
626 if (!event)
627 goto fail;
628
629 skb = sctp_event2skb(event);
630 sai = (struct sctp_adaption_event *)
631 skb_put(skb, sizeof(struct sctp_adaption_event));
632
633 sai->sai_type = SCTP_ADAPTION_INDICATION;
634 sai->sai_flags = 0;
635 sai->sai_length = sizeof(struct sctp_adaption_event);
636 sai->sai_adaption_ind = asoc->peer.adaption_ind;
637 sctp_ulpevent_set_owner(event, asoc);
638 sai->sai_assoc_id = sctp_assoc2id(asoc);
639
640 return event;
641
642fail:
643 return NULL;
644}
645
646/* A message has been received. Package this message as a notification
647 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
648 * even if filtered out later.
649 *
650 * Socket Extensions for SCTP
651 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
652 */
653struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
654 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100655 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct sctp_ulpevent *event = NULL;
658 struct sk_buff *skb;
659 size_t padding, len;
660
661 /* Clone the original skb, sharing the data. */
662 skb = skb_clone(chunk->skb, gfp);
663 if (!skb)
664 goto fail;
665
666 /* First calculate the padding, so we don't inadvertently
667 * pass up the wrong length to the user.
668 *
669 * RFC 2960 - Section 3.2 Chunk Field Descriptions
670 *
671 * The total length of a chunk(including Type, Length and Value fields)
672 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
673 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
674 * bytes and this padding is not included in the chunk length field.
675 * The sender should never pad with more than 3 bytes. The receiver
676 * MUST ignore the padding bytes.
677 */
678 len = ntohs(chunk->chunk_hdr->length);
679 padding = WORD_ROUND(len) - len;
680
681 /* Fixup cloned skb with just this chunks data. */
682 skb_trim(skb, chunk->chunk_end - padding - skb->data);
683
684 /* Embed the event fields inside the cloned skb. */
685 event = sctp_skb2event(skb);
686
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700687 /* Initialize event with flags 0 and correct length
688 * Since this is a clone of the original skb, only account for
689 * the data of this chunk as other chunks will be accounted separately.
690 */
691 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 sctp_ulpevent_receive_data(event, asoc);
694
695 event->stream = ntohs(chunk->subh.data_hdr->stream);
696 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
697 event->ppid = chunk->subh.data_hdr->ppid;
698 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700699 event->flags |= SCTP_UNORDERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
701 }
702 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
703 event->msg_flags |= chunk->chunk_hdr->flags;
704 event->iif = sctp_chunk_iif(chunk);
705
706fail:
707 return event;
708}
709
710/* Create a partial delivery related event.
711 *
712 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
713 *
714 * When a receiver is engaged in a partial delivery of a
715 * message this notification will be used to indicate
716 * various events.
717 */
718struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
Alexey Dobriyan3182cd82005-07-11 20:57:47 -0700719 const struct sctp_association *asoc, __u32 indication,
Al Virodd0fc662005-10-07 07:46:04 +0100720 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
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;
766fail:
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. */
784void 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 *
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700825 * SCTP_UNORDERED - This flag is present when the message was sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * 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
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700840 * operation when sinfo_flags are set to SCTP_UNORDERED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
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 */
863static 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 */
890static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
891{
892 struct sk_buff *skb, *frag;
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -0700893 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Current stack structures assume that the rcv buffer is
896 * per socket. For UDP style sockets this is not true as
897 * multiple associations may be on a single UDP-style socket.
898 * Use the local private area of the skb to track the owning
899 * association.
900 */
901
902 skb = sctp_event2skb(event);
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -0700903 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (!skb->data_len)
906 goto done;
907
908 /* Don't forget the fragments. */
909 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
910 /* NOTE: skb_shinfos are recursive. Although IP returns
911 * skb's with only 1 level of fragments, SCTP reassembly can
912 * increase the levels.
913 */
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -0700914 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
915 }
916
917done:
918 sctp_assoc_rwnd_increase(event->asoc, len);
919 sctp_ulpevent_release_owner(event);
920}
921
922static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
923{
924 struct sk_buff *skb, *frag;
925
926 skb = sctp_event2skb(event);
927
928 if (!skb->data_len)
929 goto done;
930
931 /* Don't forget the fragments. */
932 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
933 /* NOTE: skb_shinfos are recursive. Although IP returns
934 * skb's with only 1 level of fragments, SCTP reassembly can
935 * increase the levels.
936 */
937 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940done:
941 sctp_ulpevent_release_owner(event);
942}
943
944/* Free a ulpevent that has an owner. It includes releasing the reference
945 * to the owner, updating the rwnd in case of a DATA event and freeing the
946 * skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
948void sctp_ulpevent_free(struct sctp_ulpevent *event)
949{
950 if (sctp_ulpevent_is_notification(event))
951 sctp_ulpevent_release_owner(event);
952 else
953 sctp_ulpevent_release_data(event);
954
955 kfree_skb(sctp_event2skb(event));
956}
957
958/* Purge the skb lists holding ulpevents. */
959void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
960{
961 struct sk_buff *skb;
962 while ((skb = skb_dequeue(list)) != NULL)
963 sctp_ulpevent_free(sctp_skb2event(skb));
964}