blob: 8b3560fd876d2b95b361460b16fa3d92291a3160 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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).
Vlad Yasevich60c778b2008-01-11 09:57:09 -050011 *
12 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
28 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 *
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
35 *
36 * Written or modified by:
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Ardelle Fan <ardelle.fan@intel.com>
40 * Sridhar Samudrala <sri@us.ibm.com>
41 *
42 * Any bugs reported given to us we will try to fix... any fixes shared will
43 * be incorporated into the next SCTP release.
44 */
45
46#include <linux/types.h>
47#include <linux/skbuff.h>
48#include <net/sctp/structs.h>
49#include <net/sctp/sctp.h>
50#include <net/sctp/sm.h>
51
52static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
53 struct sctp_association *asoc);
54static void sctp_ulpevent_release_data(struct sctp_ulpevent *event);
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -070055static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Initialize an ULP event from an given skb. */
Vlad Yasevich331c4ee2006-10-09 21:34:04 -070059SCTP_STATIC void sctp_ulpevent_init(struct sctp_ulpevent *event,
60 int msg_flags,
61 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 memset(event, 0, sizeof(struct sctp_ulpevent));
64 event->msg_flags = msg_flags;
Vlad Yasevich331c4ee2006-10-09 21:34:04 -070065 event->rmem_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/* Create a new sctp_ulpevent. */
69SCTP_STATIC struct sctp_ulpevent *sctp_ulpevent_new(int size, int msg_flags,
Al Virodd0fc662005-10-07 07:46:04 +010070 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct sctp_ulpevent *event;
73 struct sk_buff *skb;
74
75 skb = alloc_skb(size, gfp);
76 if (!skb)
77 goto fail;
78
79 event = sctp_skb2event(skb);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -070080 sctp_ulpevent_init(event, msg_flags, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 return event;
83
84fail:
85 return NULL;
86}
87
88/* Is this a MSG_NOTIFICATION? */
89int sctp_ulpevent_is_notification(const struct sctp_ulpevent *event)
90{
91 return MSG_NOTIFICATION == (event->msg_flags & MSG_NOTIFICATION);
92}
93
94/* Hold the association in case the msg_name needs read out of
95 * the association.
96 */
97static inline void sctp_ulpevent_set_owner(struct sctp_ulpevent *event,
98 const struct sctp_association *asoc)
99{
100 struct sk_buff *skb;
101
102 /* Cast away the const, as we are just wanting to
103 * bump the reference count.
104 */
105 sctp_association_hold((struct sctp_association *)asoc);
106 skb = sctp_event2skb(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 event->asoc = (struct sctp_association *)asoc;
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700108 atomic_add(event->rmem_len, &event->asoc->rmem_alloc);
109 sctp_skb_set_owner_r(skb, asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/* A simple destructor to give up the reference to the association. */
113static inline void sctp_ulpevent_release_owner(struct sctp_ulpevent *event)
114{
Neil Horman049b3ff2005-11-11 16:08:24 -0800115 struct sctp_association *asoc = event->asoc;
Neil Horman049b3ff2005-11-11 16:08:24 -0800116
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700117 atomic_sub(event->rmem_len, &asoc->rmem_alloc);
Neil Horman049b3ff2005-11-11 16:08:24 -0800118 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/* Create and initialize an SCTP_ASSOC_CHANGE event.
122 *
123 * 5.3.1.1 SCTP_ASSOC_CHANGE
124 *
125 * Communication notifications inform the ULP that an SCTP association
126 * has either begun or ended. The identifier for a new association is
127 * provided by this notification.
128 *
129 * Note: There is no field checking here. If a field is unused it will be
130 * zero'd out.
131 */
132struct sctp_ulpevent *sctp_ulpevent_make_assoc_change(
133 const struct sctp_association *asoc,
134 __u16 flags, __u16 state, __u16 error, __u16 outbound,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700135 __u16 inbound, struct sctp_chunk *chunk, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct sctp_ulpevent *event;
138 struct sctp_assoc_change *sac;
139 struct sk_buff *skb;
140
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700141 /* If the lower layer passed in the chunk, it will be
142 * an ABORT, so we need to include it in the sac_info.
143 */
144 if (chunk) {
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700145 /* Copy the chunk data to a new skb and reserve enough
146 * head room to use as notification.
147 */
148 skb = skb_copy_expand(chunk->skb,
149 sizeof(struct sctp_assoc_change), 0, gfp);
150
151 if (!skb)
152 goto fail;
153
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700154 /* Embed the event fields inside the cloned skb. */
155 event = sctp_skb2event(skb);
156 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
157
158 /* Include the notification structure */
159 sac = (struct sctp_assoc_change *)
160 skb_push(skb, sizeof(struct sctp_assoc_change));
161
162 /* Trim the buffer to the right length. */
163 skb_trim(skb, sizeof(struct sctp_assoc_change) +
Vlad Yasevichac40e412007-05-09 13:52:35 -0700164 ntohs(chunk->chunk_hdr->length) -
165 sizeof(sctp_chunkhdr_t));
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700166 } else {
167 event = sctp_ulpevent_new(sizeof(struct sctp_assoc_change),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 MSG_NOTIFICATION, gfp);
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700169 if (!event)
170 goto fail;
171
172 skb = sctp_event2skb(event);
173 sac = (struct sctp_assoc_change *) skb_put(skb,
174 sizeof(struct sctp_assoc_change));
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Socket Extensions for SCTP
178 * 5.3.1.1 SCTP_ASSOC_CHANGE
179 *
180 * sac_type:
181 * It should be SCTP_ASSOC_CHANGE.
182 */
183 sac->sac_type = SCTP_ASSOC_CHANGE;
184
185 /* Socket Extensions for SCTP
186 * 5.3.1.1 SCTP_ASSOC_CHANGE
187 *
188 * sac_state: 32 bits (signed integer)
189 * This field holds one of a number of values that communicate the
190 * event that happened to the association.
191 */
192 sac->sac_state = state;
193
194 /* Socket Extensions for SCTP
195 * 5.3.1.1 SCTP_ASSOC_CHANGE
196 *
197 * sac_flags: 16 bits (unsigned integer)
198 * Currently unused.
199 */
200 sac->sac_flags = 0;
201
202 /* Socket Extensions for SCTP
203 * 5.3.1.1 SCTP_ASSOC_CHANGE
204 *
205 * sac_length: sizeof (__u32)
206 * This field is the total length of the notification data, including
207 * the notification header.
208 */
Vlad Yasevichb90a1372008-02-14 10:18:20 -0500209 sac->sac_length = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Socket Extensions for SCTP
212 * 5.3.1.1 SCTP_ASSOC_CHANGE
213 *
214 * sac_error: 32 bits (signed integer)
215 *
216 * If the state was reached due to a error condition (e.g.
217 * COMMUNICATION_LOST) any relevant error information is available in
218 * this field. This corresponds to the protocol error codes defined in
219 * [SCTP].
220 */
221 sac->sac_error = error;
222
223 /* Socket Extensions for SCTP
224 * 5.3.1.1 SCTP_ASSOC_CHANGE
225 *
226 * sac_outbound_streams: 16 bits (unsigned integer)
227 * sac_inbound_streams: 16 bits (unsigned integer)
228 *
229 * The maximum number of streams allowed in each direction are
230 * available in sac_outbound_streams and sac_inbound streams.
231 */
232 sac->sac_outbound_streams = outbound;
233 sac->sac_inbound_streams = inbound;
234
235 /* Socket Extensions for SCTP
236 * 5.3.1.1 SCTP_ASSOC_CHANGE
237 *
238 * sac_assoc_id: sizeof (sctp_assoc_t)
239 *
240 * The association id field, holds the identifier for the association.
241 * All notifications for a given association have the same association
242 * identifier. For TCP style socket, this field is ignored.
243 */
244 sctp_ulpevent_set_owner(event, asoc);
245 sac->sac_assoc_id = sctp_assoc2id(asoc);
246
247 return event;
248
249fail:
250 return NULL;
251}
252
253/* Create and initialize an SCTP_PEER_ADDR_CHANGE event.
254 *
255 * Socket Extensions for SCTP - draft-01
256 * 5.3.1.2 SCTP_PEER_ADDR_CHANGE
257 *
258 * When a destination address on a multi-homed peer encounters a change
259 * an interface details event is sent.
260 */
261struct sctp_ulpevent *sctp_ulpevent_make_peer_addr_change(
262 const struct sctp_association *asoc,
263 const struct sockaddr_storage *aaddr,
Al Virodd0fc662005-10-07 07:46:04 +0100264 int flags, int state, int error, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct sctp_ulpevent *event;
267 struct sctp_paddr_change *spc;
268 struct sk_buff *skb;
269
270 event = sctp_ulpevent_new(sizeof(struct sctp_paddr_change),
271 MSG_NOTIFICATION, gfp);
272 if (!event)
273 goto fail;
274
275 skb = sctp_event2skb(event);
276 spc = (struct sctp_paddr_change *)
277 skb_put(skb, sizeof(struct sctp_paddr_change));
278
279 /* Sockets API Extensions for SCTP
280 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
281 *
282 * spc_type:
283 *
284 * It should be SCTP_PEER_ADDR_CHANGE.
285 */
286 spc->spc_type = SCTP_PEER_ADDR_CHANGE;
287
288 /* Sockets API Extensions for SCTP
289 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
290 *
291 * spc_length: sizeof (__u32)
292 *
293 * This field is the total length of the notification data, including
294 * the notification header.
295 */
296 spc->spc_length = sizeof(struct sctp_paddr_change);
297
298 /* Sockets API Extensions for SCTP
299 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
300 *
301 * spc_flags: 16 bits (unsigned integer)
302 * Currently unused.
303 */
304 spc->spc_flags = 0;
305
306 /* Sockets API Extensions for SCTP
307 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
308 *
309 * spc_state: 32 bits (signed integer)
310 *
311 * This field holds one of a number of values that communicate the
312 * event that happened to the address.
313 */
314 spc->spc_state = state;
315
316 /* Sockets API Extensions for SCTP
317 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
318 *
319 * spc_error: 32 bits (signed integer)
320 *
321 * If the state was reached due to any error condition (e.g.
322 * ADDRESS_UNREACHABLE) any relevant error information is available in
323 * this field.
324 */
325 spc->spc_error = error;
326
327 /* Socket Extensions for SCTP
328 * 5.3.1.1 SCTP_ASSOC_CHANGE
329 *
330 * spc_assoc_id: sizeof (sctp_assoc_t)
331 *
332 * The association id field, holds the identifier for the association.
333 * All notifications for a given association have the same association
334 * identifier. For TCP style socket, this field is ignored.
335 */
336 sctp_ulpevent_set_owner(event, asoc);
337 spc->spc_assoc_id = sctp_assoc2id(asoc);
338
339 /* Sockets API Extensions for SCTP
340 * Section 5.3.1.2 SCTP_PEER_ADDR_CHANGE
341 *
342 * spc_aaddr: sizeof (struct sockaddr_storage)
343 *
344 * The affected address field, holds the remote peer's address that is
345 * encountering the change of state.
346 */
347 memcpy(&spc->spc_aaddr, aaddr, sizeof(struct sockaddr_storage));
348
349 /* Map ipv4 address into v4-mapped-on-v6 address. */
350 sctp_get_pf_specific(asoc->base.sk->sk_family)->addr_v4map(
351 sctp_sk(asoc->base.sk),
352 (union sctp_addr *)&spc->spc_aaddr);
353
354 return event;
355
356fail:
357 return NULL;
358}
359
360/* Create and initialize an SCTP_REMOTE_ERROR notification.
361 *
362 * Note: This assumes that the chunk->skb->data already points to the
363 * operation error payload.
364 *
365 * Socket Extensions for SCTP - draft-01
366 * 5.3.1.3 SCTP_REMOTE_ERROR
367 *
368 * A remote peer may send an Operational Error message to its peer.
369 * This message indicates a variety of error conditions on an
370 * association. The entire error TLV as it appears on the wire is
371 * included in a SCTP_REMOTE_ERROR event. Please refer to the SCTP
372 * specification [SCTP] and any extensions for a list of possible
373 * error formats.
374 */
375struct sctp_ulpevent *sctp_ulpevent_make_remote_error(
376 const struct sctp_association *asoc, struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100377 __u16 flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct sctp_ulpevent *event;
380 struct sctp_remote_error *sre;
381 struct sk_buff *skb;
382 sctp_errhdr_t *ch;
Al Viro9f81bcd2006-11-20 17:26:34 -0800383 __be16 cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int elen;
385
386 ch = (sctp_errhdr_t *)(chunk->skb->data);
387 cause = ch->cause;
388 elen = WORD_ROUND(ntohs(ch->length)) - sizeof(sctp_errhdr_t);
389
390 /* Pull off the ERROR header. */
391 skb_pull(chunk->skb, sizeof(sctp_errhdr_t));
392
393 /* Copy the skb to a new skb with room for us to prepend
394 * notification with.
395 */
396 skb = skb_copy_expand(chunk->skb, sizeof(struct sctp_remote_error),
397 0, gfp);
398
399 /* Pull off the rest of the cause TLV from the chunk. */
400 skb_pull(chunk->skb, elen);
401 if (!skb)
402 goto fail;
403
404 /* Embed the event fields inside the cloned skb. */
405 event = sctp_skb2event(skb);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700406 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 sre = (struct sctp_remote_error *)
409 skb_push(skb, sizeof(struct sctp_remote_error));
410
411 /* Trim the buffer to the right length. */
412 skb_trim(skb, sizeof(struct sctp_remote_error) + elen);
413
414 /* Socket Extensions for SCTP
415 * 5.3.1.3 SCTP_REMOTE_ERROR
416 *
417 * sre_type:
418 * It should be SCTP_REMOTE_ERROR.
419 */
420 sre->sre_type = SCTP_REMOTE_ERROR;
421
422 /*
423 * Socket Extensions for SCTP
424 * 5.3.1.3 SCTP_REMOTE_ERROR
425 *
426 * sre_flags: 16 bits (unsigned integer)
427 * Currently unused.
428 */
429 sre->sre_flags = 0;
430
431 /* Socket Extensions for SCTP
432 * 5.3.1.3 SCTP_REMOTE_ERROR
433 *
434 * sre_length: sizeof (__u32)
435 *
436 * This field is the total length of the notification data,
437 * including the notification header.
438 */
439 sre->sre_length = skb->len;
440
441 /* Socket Extensions for SCTP
442 * 5.3.1.3 SCTP_REMOTE_ERROR
443 *
444 * sre_error: 16 bits (unsigned integer)
445 * This value represents one of the Operational Error causes defined in
446 * the SCTP specification, in network byte order.
447 */
448 sre->sre_error = cause;
449
450 /* Socket Extensions for SCTP
451 * 5.3.1.3 SCTP_REMOTE_ERROR
452 *
453 * sre_assoc_id: sizeof (sctp_assoc_t)
454 *
455 * The association id field, holds the identifier for the association.
456 * All notifications for a given association have the same association
457 * identifier. For TCP style socket, this field is ignored.
458 */
459 sctp_ulpevent_set_owner(event, asoc);
460 sre->sre_assoc_id = sctp_assoc2id(asoc);
461
462 return event;
463
464fail:
465 return NULL;
466}
467
468/* Create and initialize a SCTP_SEND_FAILED notification.
469 *
470 * Socket Extensions for SCTP - draft-01
471 * 5.3.1.4 SCTP_SEND_FAILED
472 */
473struct sctp_ulpevent *sctp_ulpevent_make_send_failed(
474 const struct sctp_association *asoc, struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100475 __u16 flags, __u32 error, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct sctp_ulpevent *event;
478 struct sctp_send_failed *ssf;
479 struct sk_buff *skb;
480
481 /* Pull off any padding. */
482 int len = ntohs(chunk->chunk_hdr->length);
483
484 /* Make skb with more room so we can prepend notification. */
485 skb = skb_copy_expand(chunk->skb,
486 sizeof(struct sctp_send_failed), /* headroom */
487 0, /* tailroom */
488 gfp);
489 if (!skb)
490 goto fail;
491
492 /* Pull off the common chunk header and DATA header. */
493 skb_pull(skb, sizeof(struct sctp_data_chunk));
494 len -= sizeof(struct sctp_data_chunk);
495
496 /* Embed the event fields inside the cloned skb. */
497 event = sctp_skb2event(skb);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700498 sctp_ulpevent_init(event, MSG_NOTIFICATION, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 ssf = (struct sctp_send_failed *)
501 skb_push(skb, sizeof(struct sctp_send_failed));
502
503 /* Socket Extensions for SCTP
504 * 5.3.1.4 SCTP_SEND_FAILED
505 *
506 * ssf_type:
507 * It should be SCTP_SEND_FAILED.
508 */
509 ssf->ssf_type = SCTP_SEND_FAILED;
510
511 /* Socket Extensions for SCTP
512 * 5.3.1.4 SCTP_SEND_FAILED
513 *
514 * ssf_flags: 16 bits (unsigned integer)
515 * The flag value will take one of the following values
516 *
517 * SCTP_DATA_UNSENT - Indicates that the data was never put on
518 * the wire.
519 *
520 * SCTP_DATA_SENT - Indicates that the data was put on the wire.
521 * Note that this does not necessarily mean that the
522 * data was (or was not) successfully delivered.
523 */
524 ssf->ssf_flags = flags;
525
526 /* Socket Extensions for SCTP
527 * 5.3.1.4 SCTP_SEND_FAILED
528 *
529 * ssf_length: sizeof (__u32)
530 * This field is the total length of the notification data, including
531 * the notification header.
532 */
533 ssf->ssf_length = sizeof(struct sctp_send_failed) + len;
534 skb_trim(skb, ssf->ssf_length);
535
536 /* Socket Extensions for SCTP
537 * 5.3.1.4 SCTP_SEND_FAILED
538 *
539 * ssf_error: 16 bits (unsigned integer)
540 * This value represents the reason why the send failed, and if set,
541 * will be a SCTP protocol error code as defined in [SCTP] section
542 * 3.3.10.
543 */
544 ssf->ssf_error = error;
545
546 /* Socket Extensions for SCTP
547 * 5.3.1.4 SCTP_SEND_FAILED
548 *
549 * ssf_info: sizeof (struct sctp_sndrcvinfo)
550 * The original send information associated with the undelivered
551 * message.
552 */
553 memcpy(&ssf->ssf_info, &chunk->sinfo, sizeof(struct sctp_sndrcvinfo));
554
555 /* Per TSVWG discussion with Randy. Allow the application to
556 * ressemble a fragmented message.
557 */
558 ssf->ssf_info.sinfo_flags = chunk->chunk_hdr->flags;
559
560 /* Socket Extensions for SCTP
561 * 5.3.1.4 SCTP_SEND_FAILED
562 *
563 * ssf_assoc_id: sizeof (sctp_assoc_t)
564 * The association id field, sf_assoc_id, holds the identifier for the
565 * association. All notifications for a given association have the
566 * same association identifier. For TCP style socket, this field is
567 * ignored.
568 */
569 sctp_ulpevent_set_owner(event, asoc);
570 ssf->ssf_assoc_id = sctp_assoc2id(asoc);
571 return event;
572
573fail:
574 return NULL;
575}
576
577/* Create and initialize a SCTP_SHUTDOWN_EVENT notification.
578 *
579 * Socket Extensions for SCTP - draft-01
580 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
581 */
582struct sctp_ulpevent *sctp_ulpevent_make_shutdown_event(
583 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +0100584 __u16 flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct sctp_ulpevent *event;
587 struct sctp_shutdown_event *sse;
588 struct sk_buff *skb;
589
590 event = sctp_ulpevent_new(sizeof(struct sctp_shutdown_event),
591 MSG_NOTIFICATION, gfp);
592 if (!event)
593 goto fail;
594
595 skb = sctp_event2skb(event);
596 sse = (struct sctp_shutdown_event *)
597 skb_put(skb, sizeof(struct sctp_shutdown_event));
598
599 /* Socket Extensions for SCTP
600 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
601 *
602 * sse_type
603 * It should be SCTP_SHUTDOWN_EVENT
604 */
605 sse->sse_type = SCTP_SHUTDOWN_EVENT;
606
607 /* Socket Extensions for SCTP
608 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
609 *
610 * sse_flags: 16 bits (unsigned integer)
611 * Currently unused.
612 */
613 sse->sse_flags = 0;
614
615 /* Socket Extensions for SCTP
616 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
617 *
618 * sse_length: sizeof (__u32)
619 * This field is the total length of the notification data, including
620 * the notification header.
621 */
622 sse->sse_length = sizeof(struct sctp_shutdown_event);
623
624 /* Socket Extensions for SCTP
625 * 5.3.1.5 SCTP_SHUTDOWN_EVENT
626 *
627 * sse_assoc_id: sizeof (sctp_assoc_t)
628 * The association id field, holds the identifier for the association.
629 * All notifications for a given association have the same association
630 * identifier. For TCP style socket, this field is ignored.
631 */
632 sctp_ulpevent_set_owner(event, asoc);
633 sse->sse_assoc_id = sctp_assoc2id(asoc);
634
635 return event;
636
637fail:
638 return NULL;
639}
640
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800641/* Create and initialize a SCTP_ADAPTATION_INDICATION notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * Socket Extensions for SCTP
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800644 * 5.3.1.6 SCTP_ADAPTATION_INDICATION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800646struct sctp_ulpevent *sctp_ulpevent_make_adaptation_indication(
Al Virodd0fc662005-10-07 07:46:04 +0100647 const struct sctp_association *asoc, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct sctp_ulpevent *event;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800650 struct sctp_adaptation_event *sai;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct sk_buff *skb;
652
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800653 event = sctp_ulpevent_new(sizeof(struct sctp_adaptation_event),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 MSG_NOTIFICATION, gfp);
655 if (!event)
656 goto fail;
657
658 skb = sctp_event2skb(event);
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800659 sai = (struct sctp_adaptation_event *)
660 skb_put(skb, sizeof(struct sctp_adaptation_event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800662 sai->sai_type = SCTP_ADAPTATION_INDICATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sai->sai_flags = 0;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800664 sai->sai_length = sizeof(struct sctp_adaptation_event);
665 sai->sai_adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 sctp_ulpevent_set_owner(event, asoc);
667 sai->sai_assoc_id = sctp_assoc2id(asoc);
668
669 return event;
670
671fail:
672 return NULL;
673}
674
675/* A message has been received. Package this message as a notification
676 * to pass it to the upper layers. Go ahead and calculate the sndrcvinfo
677 * even if filtered out later.
678 *
679 * Socket Extensions for SCTP
680 * 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
681 */
682struct sctp_ulpevent *sctp_ulpevent_make_rcvmsg(struct sctp_association *asoc,
683 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100684 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct sctp_ulpevent *event = NULL;
687 struct sk_buff *skb;
688 size_t padding, len;
Neil Horman4d93df02007-08-15 16:07:44 -0700689 int rx_count;
690
691 /*
692 * check to see if we need to make space for this
693 * new skb, expand the rcvbuffer if needed, or drop
694 * the frame
695 */
696 if (asoc->ep->rcvbuf_policy)
697 rx_count = atomic_read(&asoc->rmem_alloc);
698 else
699 rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc);
700
701 if (rx_count >= asoc->base.sk->sk_rcvbuf) {
702
703 if ((asoc->base.sk->sk_userlocks & SOCK_RCVBUF_LOCK) ||
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800704 (!sk_rmem_schedule(asoc->base.sk, chunk->skb->truesize)))
Neil Horman4d93df02007-08-15 16:07:44 -0700705 goto fail;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* Clone the original skb, sharing the data. */
709 skb = skb_clone(chunk->skb, gfp);
710 if (!skb)
711 goto fail;
712
Vlad Yasevich3888e9e2008-07-08 02:28:39 -0700713 /* Now that all memory allocations for this chunk succeeded, we
714 * can mark it as received so the tsn_map is updated correctly.
715 */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700716 if (sctp_tsnmap_mark(&asoc->peer.tsn_map,
717 ntohl(chunk->subh.data_hdr->tsn)))
718 goto fail_mark;
Vlad Yasevich3888e9e2008-07-08 02:28:39 -0700719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* First calculate the padding, so we don't inadvertently
721 * pass up the wrong length to the user.
722 *
723 * RFC 2960 - Section 3.2 Chunk Field Descriptions
724 *
725 * The total length of a chunk(including Type, Length and Value fields)
726 * MUST be a multiple of 4 bytes. If the length of the chunk is not a
727 * multiple of 4 bytes, the sender MUST pad the chunk with all zero
728 * bytes and this padding is not included in the chunk length field.
729 * The sender should never pad with more than 3 bytes. The receiver
730 * MUST ignore the padding bytes.
731 */
732 len = ntohs(chunk->chunk_hdr->length);
733 padding = WORD_ROUND(len) - len;
734
735 /* Fixup cloned skb with just this chunks data. */
736 skb_trim(skb, chunk->chunk_end - padding - skb->data);
737
738 /* Embed the event fields inside the cloned skb. */
739 event = sctp_skb2event(skb);
740
Vlad Yasevich331c4ee2006-10-09 21:34:04 -0700741 /* Initialize event with flags 0 and correct length
742 * Since this is a clone of the original skb, only account for
743 * the data of this chunk as other chunks will be accounted separately.
744 */
745 sctp_ulpevent_init(event, 0, skb->len + sizeof(struct sk_buff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 sctp_ulpevent_receive_data(event, asoc);
748
749 event->stream = ntohs(chunk->subh.data_hdr->stream);
750 event->ssn = ntohs(chunk->subh.data_hdr->ssn);
751 event->ppid = chunk->subh.data_hdr->ppid;
752 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700753 event->flags |= SCTP_UNORDERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 event->cumtsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
755 }
756 event->tsn = ntohl(chunk->subh.data_hdr->tsn);
757 event->msg_flags |= chunk->chunk_hdr->flags;
758 event->iif = sctp_chunk_iif(chunk);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return event;
Vlad Yasevich8e1ee182008-10-08 14:18:39 -0700761
762fail_mark:
763 kfree_skb(skb);
764fail:
765 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/* Create a partial delivery related event.
769 *
770 * 5.3.1.7 SCTP_PARTIAL_DELIVERY_EVENT
771 *
772 * When a receiver is engaged in a partial delivery of a
773 * message this notification will be used to indicate
774 * various events.
775 */
776struct sctp_ulpevent *sctp_ulpevent_make_pdapi(
Alexey Dobriyan3182cd82005-07-11 20:57:47 -0700777 const struct sctp_association *asoc, __u32 indication,
Al Virodd0fc662005-10-07 07:46:04 +0100778 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct sctp_ulpevent *event;
781 struct sctp_pdapi_event *pd;
782 struct sk_buff *skb;
783
784 event = sctp_ulpevent_new(sizeof(struct sctp_pdapi_event),
785 MSG_NOTIFICATION, gfp);
786 if (!event)
787 goto fail;
788
789 skb = sctp_event2skb(event);
790 pd = (struct sctp_pdapi_event *)
791 skb_put(skb, sizeof(struct sctp_pdapi_event));
792
793 /* pdapi_type
794 * It should be SCTP_PARTIAL_DELIVERY_EVENT
795 *
796 * pdapi_flags: 16 bits (unsigned integer)
797 * Currently unused.
798 */
799 pd->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
800 pd->pdapi_flags = 0;
801
802 /* pdapi_length: 32 bits (unsigned integer)
803 *
804 * This field is the total length of the notification data, including
805 * the notification header. It will generally be sizeof (struct
806 * sctp_pdapi_event).
807 */
808 pd->pdapi_length = sizeof(struct sctp_pdapi_event);
809
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900810 /* pdapi_indication: 32 bits (unsigned integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 *
812 * This field holds the indication being sent to the application.
813 */
814 pd->pdapi_indication = indication;
815
816 /* pdapi_assoc_id: sizeof (sctp_assoc_t)
817 *
818 * The association id field, holds the identifier for the association.
819 */
820 sctp_ulpevent_set_owner(event, asoc);
821 pd->pdapi_assoc_id = sctp_assoc2id(asoc);
822
823 return event;
824fail:
825 return NULL;
826}
827
Vlad Yasevich65b07e52007-09-16 19:34:00 -0700828struct sctp_ulpevent *sctp_ulpevent_make_authkey(
829 const struct sctp_association *asoc, __u16 key_id,
830 __u32 indication, gfp_t gfp)
831{
832 struct sctp_ulpevent *event;
833 struct sctp_authkey_event *ak;
834 struct sk_buff *skb;
835
836 event = sctp_ulpevent_new(sizeof(struct sctp_authkey_event),
837 MSG_NOTIFICATION, gfp);
838 if (!event)
839 goto fail;
840
841 skb = sctp_event2skb(event);
842 ak = (struct sctp_authkey_event *)
843 skb_put(skb, sizeof(struct sctp_authkey_event));
844
Vlad Yasevichf6917242008-01-07 00:27:16 -0800845 ak->auth_type = SCTP_AUTHENTICATION_INDICATION;
Vlad Yasevich65b07e52007-09-16 19:34:00 -0700846 ak->auth_flags = 0;
847 ak->auth_length = sizeof(struct sctp_authkey_event);
848
849 ak->auth_keynumber = key_id;
850 ak->auth_altkeynumber = 0;
851 ak->auth_indication = indication;
852
853 /*
854 * The association id field, holds the identifier for the association.
855 */
856 sctp_ulpevent_set_owner(event, asoc);
857 ak->auth_assoc_id = sctp_assoc2id(asoc);
858
859 return event;
860fail:
861 return NULL;
862}
863
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/* Return the notification type, assuming this is a notification
866 * event.
867 */
868__u16 sctp_ulpevent_get_notification_type(const struct sctp_ulpevent *event)
869{
870 union sctp_notification *notification;
871 struct sk_buff *skb;
872
Vlad Yasevichab38fb02008-04-12 18:40:06 -0700873 skb = sctp_event2skb(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 notification = (union sctp_notification *) skb->data;
875 return notification->sn_header.sn_type;
876}
877
878/* Copy out the sndrcvinfo into a msghdr. */
879void sctp_ulpevent_read_sndrcvinfo(const struct sctp_ulpevent *event,
880 struct msghdr *msghdr)
881{
882 struct sctp_sndrcvinfo sinfo;
883
884 if (sctp_ulpevent_is_notification(event))
885 return;
886
887 /* Sockets API Extensions for SCTP
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900888 * Section 5.2.2 SCTP Header Information Structure (SCTP_SNDRCV)
889 *
890 * sinfo_stream: 16 bits (unsigned integer)
891 *
892 * For recvmsg() the SCTP stack places the message's stream number in
893 * this value.
894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sinfo.sinfo_stream = event->stream;
896 /* sinfo_ssn: 16 bits (unsigned integer)
897 *
898 * For recvmsg() this value contains the stream sequence number that
899 * the remote endpoint placed in the DATA chunk. For fragmented
900 * messages this is the same number for all deliveries of the message
901 * (if more than one recvmsg() is needed to read the message).
902 */
903 sinfo.sinfo_ssn = event->ssn;
904 /* sinfo_ppid: 32 bits (unsigned integer)
905 *
906 * In recvmsg() this value is
907 * the same information that was passed by the upper layer in the peer
908 * application. Please note that byte order issues are NOT accounted
909 * for and this information is passed opaquely by the SCTP stack from
910 * one end to the other.
911 */
912 sinfo.sinfo_ppid = event->ppid;
913 /* sinfo_flags: 16 bits (unsigned integer)
914 *
915 * This field may contain any of the following flags and is composed of
916 * a bitwise OR of these values.
917 *
918 * recvmsg() flags:
919 *
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700920 * SCTP_UNORDERED - This flag is present when the message was sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * non-ordered.
922 */
923 sinfo.sinfo_flags = event->flags;
924 /* sinfo_tsn: 32 bit (unsigned integer)
925 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900926 * For the receiving side, this field holds a TSN that was
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * assigned to one of the SCTP Data Chunks.
928 */
929 sinfo.sinfo_tsn = event->tsn;
930 /* sinfo_cumtsn: 32 bit (unsigned integer)
931 *
932 * This field will hold the current cumulative TSN as
933 * known by the underlying SCTP layer. Note this field is
934 * ignored when sending and only valid for a receive
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700935 * operation when sinfo_flags are set to SCTP_UNORDERED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
937 sinfo.sinfo_cumtsn = event->cumtsn;
938 /* sinfo_assoc_id: sizeof (sctp_assoc_t)
939 *
940 * The association handle field, sinfo_assoc_id, holds the identifier
941 * for the association announced in the COMMUNICATION_UP notification.
942 * All notifications for a given association have the same identifier.
943 * Ignored for one-to-one style sockets.
944 */
945 sinfo.sinfo_assoc_id = sctp_assoc2id(event->asoc);
946
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -0800947 /* context value that is set via SCTP_CONTEXT socket option. */
948 sinfo.sinfo_context = event->asoc->default_rcv_context;
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* These fields are not used while receiving. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sinfo.sinfo_timetolive = 0;
952
953 put_cmsg(msghdr, IPPROTO_SCTP, SCTP_SNDRCV,
954 sizeof(struct sctp_sndrcvinfo), (void *)&sinfo);
955}
956
957/* Do accounting for bytes received and hold a reference to the association
958 * for each skb.
959 */
960static void sctp_ulpevent_receive_data(struct sctp_ulpevent *event,
961 struct sctp_association *asoc)
962{
963 struct sk_buff *skb, *frag;
964
965 skb = sctp_event2skb(event);
966 /* Set the owner and charge rwnd for bytes received. */
967 sctp_ulpevent_set_owner(event, asoc);
968 sctp_assoc_rwnd_decrease(asoc, skb_headlen(skb));
969
970 if (!skb->data_len)
971 return;
972
973 /* Note: Not clearing the entire event struct as this is just a
974 * fragment of the real event. However, we still need to do rwnd
975 * accounting.
976 * In general, the skb passed from IP can have only 1 level of
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900977 * fragments. But we allow multiple levels of fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 */
David S. Miller1b003be2009-06-09 00:22:35 -0700979 skb_walk_frags(skb, frag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sctp_ulpevent_receive_data(sctp_skb2event(frag), asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/* Do accounting for bytes just read by user and release the references to
984 * the association.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900985 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static void sctp_ulpevent_release_data(struct sctp_ulpevent *event)
987{
988 struct sk_buff *skb, *frag;
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -0700989 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* Current stack structures assume that the rcv buffer is
992 * per socket. For UDP style sockets this is not true as
993 * multiple associations may be on a single UDP-style socket.
994 * Use the local private area of the skb to track the owning
995 * association.
996 */
997
998 skb = sctp_event2skb(event);
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -0700999 len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (!skb->data_len)
1002 goto done;
1003
1004 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07001005 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* NOTE: skb_shinfos are recursive. Although IP returns
1007 * skb's with only 1 level of fragments, SCTP reassembly can
1008 * increase the levels.
1009 */
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -07001010 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
1011 }
1012
1013done:
1014 sctp_assoc_rwnd_increase(event->asoc, len);
1015 sctp_ulpevent_release_owner(event);
1016}
1017
1018static void sctp_ulpevent_release_frag_data(struct sctp_ulpevent *event)
1019{
1020 struct sk_buff *skb, *frag;
1021
1022 skb = sctp_event2skb(event);
1023
1024 if (!skb->data_len)
1025 goto done;
1026
1027 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07001028 skb_walk_frags(skb, frag) {
Tsutomu Fujiid7c2c9e2006-06-17 22:58:28 -07001029 /* NOTE: skb_shinfos are recursive. Although IP returns
1030 * skb's with only 1 level of fragments, SCTP reassembly can
1031 * increase the levels.
1032 */
1033 sctp_ulpevent_release_frag_data(sctp_skb2event(frag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036done:
1037 sctp_ulpevent_release_owner(event);
1038}
1039
1040/* Free a ulpevent that has an owner. It includes releasing the reference
1041 * to the owner, updating the rwnd in case of a DATA event and freeing the
1042 * skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044void sctp_ulpevent_free(struct sctp_ulpevent *event)
1045{
1046 if (sctp_ulpevent_is_notification(event))
1047 sctp_ulpevent_release_owner(event);
1048 else
1049 sctp_ulpevent_release_data(event);
1050
1051 kfree_skb(sctp_event2skb(event));
1052}
1053
1054/* Purge the skb lists holding ulpevents. */
1055void sctp_queue_purge_ulpevents(struct sk_buff_head *list)
1056{
1057 struct sk_buff *skb;
1058 while ((skb = skb_dequeue(list)) != NULL)
1059 sctp_ulpevent_free(sctp_skb2event(skb));
1060}